summaryrefslogtreecommitdiff
path: root/src/asm2wasm.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/asm2wasm.cpp')
-rw-r--r--src/asm2wasm.cpp44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/asm2wasm.cpp b/src/asm2wasm.cpp
index 4273793ba..3e109e010 100644
--- a/src/asm2wasm.cpp
+++ b/src/asm2wasm.cpp
@@ -8,7 +8,7 @@
using namespace cashew;
using namespace wasm;
-bool debug;
+int debug;
// Utilities
@@ -337,12 +337,30 @@ private:
case ASM_INT: return BasicType::i32;
case ASM_DOUBLE: return BasicType::f64;
case ASM_NONE: return BasicType::none;
- default: abort_on("confused detectWasmType", asmType);
+ default: abort_on("confused asmType", asmType);
}
}
+ AsmType wasmToAsmType(BasicType type) {
+ switch (type) {
+ case BasicType::i32: return ASM_INT;
+ case BasicType::f64: return ASM_DOUBLE;
+ case BasicType::none: return ASM_NONE;
+ default: abort_on("confused wasmType", type);
+ }
+ }
+
+ AsmType detectAsmType(Ref ast, AsmData *data) {
+ if (ast[0] == NAME) {
+ IString name = ast[1]->getIString();
+ if (mappedGlobals.find(name) != mappedGlobals.end()) {
+ return wasmToAsmType(mappedGlobals[name].type);
+ }
+ }
+ return detectType(ast, data);
+ }
BasicType detectWasmType(Ref ast, AsmData *data) {
- return asmToWasmType(detectType(ast, data));
+ return asmToWasmType(detectAsmType(ast, data));
}
bool isUnsignedCoercion(Ref ast) { // TODO: use detectSign?
@@ -631,12 +649,14 @@ void Asm2WasmModule::processAsm(Ref ast) {
}
Function* Asm2WasmModule::processFunction(Ref ast) {
- //if (ast[1] !=IString("_fmod")) return nullptr;
+ //if (ast[1] != IString("qta")) return nullptr;
if (debug) {
- std::cout << "\nfunc: ";
- ast->stringify(std::cout);
- std::cout << '\n';
+ std::cout << "\nfunc: " << ast[1]->getIString().str << '\n';
+ if (debug >= 2) {
+ ast->stringify(std::cout);
+ std::cout << '\n';
+ }
}
auto function = allocator.alloc<Function>();
@@ -698,7 +718,7 @@ Function* Asm2WasmModule::processFunction(Ref ast) {
std::function<Expression* (Ref)> process = [&](Ref ast) -> Expression* {
AstStackHelper astStackHelper(ast); // TODO: only create one when we need it?
- if (debug) {
+ if (debug >= 2) {
std::cout << "at: ";
ast->stringify(std::cout);
std::cout << '\n';
@@ -831,7 +851,7 @@ Function* Asm2WasmModule::processFunction(Ref ast) {
ret->type = ret->value.type;
return ret;
}
- AsmType childType = detectType(ast[2], &asmData);
+ AsmType childType = detectAsmType(ast[2], &asmData);
if (childType == ASM_INT) {
auto ret = allocator.alloc<Convert>();
ret->op = isUnsignedCoercion(ast[2]) ? ConvertUInt32 : ConvertSInt32;
@@ -850,7 +870,7 @@ Function* Asm2WasmModule::processFunction(Ref ast) {
ret->type = ret->value.type;
return ret;
}
- AsmType asmType = detectType(ast[2], &asmData);
+ AsmType asmType = detectAsmType(ast[2], &asmData);
if (asmType == ASM_INT) {
// wasm has no unary negation for int, so do 0-
auto ret = allocator.alloc<Binary>();
@@ -920,7 +940,7 @@ Function* Asm2WasmModule::processFunction(Ref ast) {
Call* ret;
if (imports.find(name) != imports.end()) {
// no imports yet in reference interpreter, fake it
- AsmType asmType = detectType(astStackHelper.getParent(), &asmData);
+ AsmType asmType = detectAsmType(astStackHelper.getParent(), &asmData);
if (asmType == ASM_NONE) return allocator.alloc<Nop>();
if (asmType == ASM_INT) return allocator.alloc<Const>()->set(Literal((int32_t)0));
if (asmType == ASM_DOUBLE) return allocator.alloc<Const>()->set(Literal((double)0.0));
@@ -1196,7 +1216,7 @@ void Asm2WasmModule::optimize() {
// main
int main(int argc, char **argv) {
- debug = !!getenv("ASM2WASM_DEBUG") && getenv("ASM2WASM_DEBUG")[0] != '0';
+ debug = getenv("ASM2WASM_DEBUG") ? getenv("ASM2WASM_DEBUG")[0] - '0' : 0;
char *infile = argv[1];