diff options
Diffstat (limited to 'src/asm2wasm.h')
-rw-r--r-- | src/asm2wasm.h | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index c852885f8..1da59d2f2 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -216,41 +216,41 @@ private: // function types. we fill in this information as we see // uses, in the first pass - std::map<IString, FunctionType> importedFunctionTypes; + std::map<IString, FunctionType*> importedFunctionTypes; std::map<IString, std::vector<CallImport*>> importedFunctionCalls; void noteImportedFunctionCall(Ref ast, WasmType resultType, AsmData *asmData, CallImport* call) { assert(ast[0] == CALL && ast[1][0] == NAME); IString importName = ast[1][1]->getIString(); - FunctionType type; - type.name = IString((std::string("type$") + importName.str).c_str(), false); // TODO: make a list of such types - type.result = resultType; + auto* type = allocator.alloc<FunctionType>(); + type->name = IString((std::string("type$") + importName.str).c_str(), false); // TODO: make a list of such types + type->result = resultType; Ref args = ast[2]; for (unsigned i = 0; i < args->size(); i++) { - type.params.push_back(detectWasmType(args[i], asmData)); + type->params.push_back(detectWasmType(args[i], asmData)); } // if we already saw this signature, verify it's the same (or else handle that) if (importedFunctionTypes.find(importName) != importedFunctionTypes.end()) { - FunctionType& previous = importedFunctionTypes[importName]; + FunctionType* previous = importedFunctionTypes[importName]; #if 0 std::cout << "compare " << importName.str << "\nfirst: "; type.print(std::cout, 0); std::cout << "\nsecond: "; previous.print(std::cout, 0) << ".\n"; #endif - if (type != previous) { + if (*type != *previous) { // merge it in. we'll add on extra 0 parameters for ones not actually used, etc. - for (size_t i = 0; i < type.params.size(); i++) { - if (previous.params.size() > i) { - if (previous.params[i] == none) { - previous.params[i] = type.params[i]; // use a more concrete type + for (size_t i = 0; i < type->params.size(); i++) { + if (previous->params.size() > i) { + if (previous->params[i] == none) { + previous->params[i] = type->params[i]; // use a more concrete type } } else { - previous.params.push_back(type.params[i]); // add a new param + previous->params.push_back(type->params[i]); // add a new param } } - if (previous.result == none) { - previous.result = type.result; // use a more concrete type + if (previous->result == none) { + previous->result = type->result; // use a more concrete type } } } else { @@ -262,7 +262,7 @@ private: FunctionType* getFunctionType(Ref parent, ExpressionList& operands) { // generate signature WasmType result = !!parent ? detectWasmType(parent, nullptr) : none; - return ensureFunctionType(getSig(result, operands), &wasm, allocator); + return ensureFunctionType(getSig(result, operands), &wasm); } public: @@ -423,9 +423,9 @@ private: if (base == ABS) { assert(operands && operands->size() == 1); WasmType type = (*operands)[0]->type; - if (type == i32) return ensureFunctionType("ii", &wasm, allocator); - if (type == f32) return ensureFunctionType("ff", &wasm, allocator); - if (type == f64) return ensureFunctionType("dd", &wasm, allocator); + if (type == i32) return ensureFunctionType("ii", &wasm); + if (type == f32) return ensureFunctionType("ff", &wasm); + if (type == f64) return ensureFunctionType("dd", &wasm); } } return nullptr; @@ -699,7 +699,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) { import->type = builtin; continue; } - import->type = ensureFunctionType(getSig(&importedFunctionTypes[name]), &wasm, allocator); + import->type = ensureFunctionType(getSig(importedFunctionTypes[name]), &wasm); } else if (import->module != ASM2WASM) { // special-case the special module // never actually used toErase.push_back(name); @@ -716,9 +716,9 @@ void Asm2WasmBuilder::processAsm(Ref ast) { auto& list = pair.second; auto type = importedFunctionTypes[name]; for (auto* call : list) { - for (size_t i = call->operands.size(); i < type.params.size(); i++) { + for (size_t i = call->operands.size(); i < type->params.size(); i++) { auto val = allocator.alloc<Const>(); - val->type = val->value.type = type.params[i]; + val->type = val->value.type = type->params[i]; call->operands.push_back(val); } } @@ -1021,7 +1021,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->name = F64_REM; import->module = ASM2WASM; import->base = F64_REM; - import->type = ensureFunctionType("ddd", &wasm, allocator); + import->type = ensureFunctionType("ddd", &wasm); wasm.addImport(import); } return call; @@ -1059,7 +1059,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->name = DEBUGGER; import->module = ASM2WASM; import->base = DEBUGGER; - import->type = ensureFunctionType("v", &wasm, allocator); + import->type = ensureFunctionType("v", &wasm); wasm.addImport(import); } return call; @@ -1172,7 +1172,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->name = F64_TO_INT; import->module = ASM2WASM; import->base = F64_TO_INT; - import->type = ensureFunctionType("id", &wasm, allocator); + import->type = ensureFunctionType("id", &wasm); wasm.addImport(import); } return ret; @@ -1303,20 +1303,25 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { abort(); } } - Call* ret; + Expression* ret; + ExpressionList* operands; if (wasm.checkImport(name)) { Ref parent = astStackHelper.getParent(); WasmType type = !!parent ? detectWasmType(parent, &asmData) : none; auto specific = allocator.alloc<CallImport>(); noteImportedFunctionCall(ast, type, &asmData, specific); + specific->target = name; + operands = &specific->operands; ret = specific; } else { - ret = allocator.alloc<Call>(); + auto specific = allocator.alloc<Call>(); + specific->target = name; + operands = &specific->operands; + ret = specific; } - ret->target = name; Ref args = ast[2]; for (unsigned i = 0; i < args->size(); i++) { - ret->operands.push_back(process(args[i])); + operands->push_back(process(args[i])); } return ret; } |