summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-10-30 12:19:06 -0700
committerAlon Zakai <alonzakai@gmail.com>2015-10-30 12:19:06 -0700
commit418ebad61c099737b4fd56f3030b1fd1e0e89a2b (patch)
treec550367180648658d38bdd5649066600bf093826
parentdbb84c00b4bf99340fab321d7e6a8105ab56d643 (diff)
downloadbinaryen-418ebad61c099737b4fd56f3030b1fd1e0e89a2b.tar.gz
binaryen-418ebad61c099737b4fd56f3030b1fd1e0e89a2b.tar.bz2
binaryen-418ebad61c099737b4fd56f3030b1fd1e0e89a2b.zip
fix type detection of globals
-rw-r--r--README.md2
-rw-r--r--src/asm2wasm.cpp44
-rw-r--r--test/unit.asm.js8
-rw-r--r--test/unit.wast27
4 files changed, 68 insertions, 13 deletions
diff --git a/README.md b/README.md
index 18e733afd..789613ef9 100644
--- a/README.md
+++ b/README.md
@@ -46,7 +46,7 @@ You should see something like this:
On Linux and Mac you should see pretty colors as in that image. Set `COLORS=0` in the env to disable colors if you prefer that. Set `COLORS=1` in the env to force colors (useful when piping to `more`, for example).
-Set `ASM2WASM_DEBUG=1` in the env to see debug info, about asm.js nodes as they are parsed, etc.
+Set `ASM2WASM_DEBUG=1` in the env to see debug info, about asm.js functions as they are parsed, etc. `2` will show even more info.
## Starting from C/C++ Source
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];
diff --git a/test/unit.asm.js b/test/unit.asm.js
index c8d52f3e0..922923ea3 100644
--- a/test/unit.asm.js
+++ b/test/unit.asm.js
@@ -1,11 +1,19 @@
function () {
"use asm";
+
+ var t = global.NaN, u = global.Infinity;
+
function big_negative() {
var temp = 0.0;
temp = +-2147483648;
temp = -2147483648.0;
temp = -21474836480.0;
}
+ function importedDoubles() {
+ var temp = 0.0;
+ temp = t + u + (-u) + (-t);
+ }
+
return { big_negative: big_negative };
}
diff --git a/test/unit.wast b/test/unit.wast
index 914cbf8bd..5fecdcad3 100644
--- a/test/unit.wast
+++ b/test/unit.wast
@@ -17,4 +17,31 @@
)
)
)
+ (func $importedDoubles
+ (local $temp f64)
+ (set_local $temp
+ (f64.add
+ (f64.add
+ (f64.add
+ (f64.load align=8
+ (i32.const 8)
+ )
+ (f64.load align=8
+ (i32.const 16)
+ )
+ )
+ (f64.neg
+ (f64.load align=8
+ (i32.const 16)
+ )
+ )
+ )
+ (f64.neg
+ (f64.load align=8
+ (i32.const 8)
+ )
+ )
+ )
+ )
+ )
)