summaryrefslogtreecommitdiff
path: root/src/emscripten-optimizer
diff options
context:
space:
mode:
authorAlon Zakai (kripken) <alonzakai@gmail.com>2017-01-30 12:13:56 -0800
committerAlon Zakai (kripken) <alonzakai@gmail.com>2017-01-30 15:25:56 -0800
commit818fa1dc19b0edcaaf1a8aa78b94bf052420252d (patch)
tree9e5a30bc0ccff4cb7637b5d4b2ffbd8caa82b2c4 /src/emscripten-optimizer
parent64284970344ce5463adada38e348230256b31226 (diff)
downloadbinaryen-818fa1dc19b0edcaaf1a8aa78b94bf052420252d.tar.gz
binaryen-818fa1dc19b0edcaaf1a8aa78b94bf052420252d.tar.bz2
binaryen-818fa1dc19b0edcaaf1a8aa78b94bf052420252d.zip
refactor asm.js ast to use a number node directly instead of [NUM, number]
Diffstat (limited to 'src/emscripten-optimizer')
-rw-r--r--src/emscripten-optimizer/optimizer-shared.cpp22
-rw-r--r--src/emscripten-optimizer/parser.cpp1
-rw-r--r--src/emscripten-optimizer/parser.h1
-rw-r--r--src/emscripten-optimizer/simple_ast.cpp2
-rw-r--r--src/emscripten-optimizer/simple_ast.h24
5 files changed, 30 insertions, 20 deletions
diff --git a/src/emscripten-optimizer/optimizer-shared.cpp b/src/emscripten-optimizer/optimizer-shared.cpp
index 57b7921fa..b2c03cf28 100644
--- a/src/emscripten-optimizer/optimizer-shared.cpp
+++ b/src/emscripten-optimizer/optimizer-shared.cpp
@@ -53,12 +53,13 @@ HeapInfo parseHeap(const char *name) {
}
AsmType detectType(Ref node, AsmData *asmData, bool inVarDef, IString minifiedFround, bool allowI64) {
+ if (node->isNumber()) {
+ if (!wasm::isInteger(node->getNumber())) return ASM_DOUBLE;
+ return ASM_INT;
+ }
switch (node[0]->getCString()[0]) {
case 'n': {
- if (node[0] == NUM) {
- if (!wasm::isInteger(node[1]->getNumber())) return ASM_DOUBLE;
- return ASM_INT;
- } else if (node[0] == NAME) {
+ if (node[0] == NAME) {
if (asmData) {
AsmType ret = asmData->getType(node[1]->getCString());
if (ret != ASM_NONE) return ret;
@@ -141,6 +142,13 @@ static void abort_on(Ref node) {
}
AsmSign detectSign(Ref node, IString minifiedFround) {
+ if (node->isNumber()) {
+ double value = node->getNumber();
+ if (value < 0) return ASM_SIGNED;
+ if (value > uint32_t(-1) || fmod(value, 1) != 0) return ASM_NONSIGNED;
+ if (wasm::isSInteger32(value)) return ASM_FLEXIBLE;
+ return ASM_UNSIGNED;
+ }
IString type = node[0]->getIString();
if (type == BINARY) {
IString op = node[1]->getIString();
@@ -162,12 +170,6 @@ AsmSign detectSign(Ref node, IString minifiedFround) {
case '~': return ASM_SIGNED;
default: abort_on(node);
}
- } else if (type == NUM) {
- double value = node[1]->getNumber();
- if (value < 0) return ASM_SIGNED;
- if (value > uint32_t(-1) || fmod(value, 1) != 0) return ASM_NONSIGNED;
- if (wasm::isSInteger32(value)) return ASM_FLEXIBLE;
- return ASM_UNSIGNED;
} else if (type == NAME) {
return ASM_FLEXIBLE;
} else if (type == CONDITIONAL) {
diff --git a/src/emscripten-optimizer/parser.cpp b/src/emscripten-optimizer/parser.cpp
index 064c0efcf..d0d5aac54 100644
--- a/src/emscripten-optimizer/parser.cpp
+++ b/src/emscripten-optimizer/parser.cpp
@@ -39,7 +39,6 @@ IString TOPLEVEL("toplevel"),
SEQ("seq"),
SUB("sub"),
CALL("call"),
- NUM("num"),
LABEL("label"),
BREAK("break"),
CONTINUE("continue"),
diff --git a/src/emscripten-optimizer/parser.h b/src/emscripten-optimizer/parser.h
index 060b71272..e43709f9b 100644
--- a/src/emscripten-optimizer/parser.h
+++ b/src/emscripten-optimizer/parser.h
@@ -54,7 +54,6 @@ extern IString TOPLEVEL,
SEQ,
SUB,
CALL,
- NUM,
LABEL,
BREAK,
CONTINUE,
diff --git a/src/emscripten-optimizer/simple_ast.cpp b/src/emscripten-optimizer/simple_ast.cpp
index dddaeab02..4ef6c7f2a 100644
--- a/src/emscripten-optimizer/simple_ast.cpp
+++ b/src/emscripten-optimizer/simple_ast.cpp
@@ -277,6 +277,6 @@ void traverseFunctions(Ref ast, std::function<void (Ref)> visit) {
// ValueBuilder
-IStringSet ValueBuilder::statable("assign call binary unary-prefix name num conditional dot new sub seq string object array");
+IStringSet ValueBuilder::statable("assign call binary unary-prefix name conditional dot new sub seq string object array");
} // namespace cashew
diff --git a/src/emscripten-optimizer/simple_ast.h b/src/emscripten-optimizer/simple_ast.h
index 20de952c1..bb757e260 100644
--- a/src/emscripten-optimizer/simple_ast.h
+++ b/src/emscripten-optimizer/simple_ast.h
@@ -204,6 +204,14 @@ struct Value {
bool isBool(bool b) { return type == Bool && b == boo; } // avoid overloading == as it might overload over int
+ // convenience function to check if something is an array and
+ // also has a certain string as the first element. This is a
+ // very common operation as the first element defines the node
+ // type for most ast nodes
+ bool isArray(IString name) {
+ return isArray() && (*this)[0] == name;
+ }
+
const char* getCString() {
assert(isString());
return str.str;
@@ -655,6 +663,10 @@ struct JSPrinter {
void print(Ref node) {
ensure();
+ if (node->isNumber()) {
+ printNum(node);
+ return;
+ }
IString type = node[0]->getIString();
//fprintf(stderr, "printing %s\n", type.str);
switch (type.str[0]) {
@@ -697,7 +709,6 @@ struct JSPrinter {
}
case 'n': {
if (type == NAME) printName(node);
- else if (type == NUM) printNum(node);
else if (type == NEW) printNew(node);
else abort();
break;
@@ -957,7 +968,7 @@ struct JSPrinter {
}
void printNum(Ref node) {
- emit(numToString(node[1]->getNumber(), finalize));
+ emit(numToString(node->getNumber(), finalize));
}
void printString(Ref node) {
@@ -1030,8 +1041,8 @@ struct JSPrinter {
}
void printUnaryPrefix(Ref node) {
- if (finalize && node[1] == PLUS && (node[2][0] == NUM ||
- (node[2][0] == UNARY_PREFIX && node[2][1] == MINUS && node[2][2][0] == NUM))) {
+ if (finalize && node[1] == PLUS && (node[2]->isNumber() ||
+ (node[2][0] == UNARY_PREFIX && node[2][1] == MINUS && node[2][2]->isNumber()))) {
// emit a finalized number
int last = used;
print(node[2]);
@@ -1449,7 +1460,7 @@ public:
}
static Ref makeStatement(Ref contents) {
- if (statable.has(contents[0]->getIString())) {
+ if (contents->isNumber() || statable.has(contents[0]->getIString())) {
return &makeRawArray(2)->push_back(makeRawString(STAT))
.push_back(contents);
} else {
@@ -1458,8 +1469,7 @@ public:
}
static Ref makeDouble(double num) {
- return &makeRawArray(2)->push_back(makeRawString(NUM))
- .push_back(&arena.alloc()->setNumber(num));
+ return &arena.alloc()->setNumber(num);
}
static Ref makeInt(uint32_t num) {
return makeDouble(double(num));