summaryrefslogtreecommitdiff
path: root/src/emscripten-optimizer
diff options
context:
space:
mode:
authorAlon Zakai (kripken) <alonzakai@gmail.com>2017-01-31 20:55:42 -0800
committerAlon Zakai (kripken) <alonzakai@gmail.com>2017-01-31 20:55:42 -0800
commitae6612e4c394af30bb0871ad9735ac853811f807 (patch)
tree891b0e439ff706d55f261c27101d11e5ada0b372 /src/emscripten-optimizer
parentf6c0bc26b65c6cfd58d1bb011c1587d343c42ab5 (diff)
downloadbinaryen-ae6612e4c394af30bb0871ad9735ac853811f807.tar.gz
binaryen-ae6612e4c394af30bb0871ad9735ac853811f807.tar.bz2
binaryen-ae6612e4c394af30bb0871ad9735ac853811f807.zip
refactor asm.js ast to use an AssignName node, for the common case of assigning to a name and not an arbitrary ast node
Diffstat (limited to 'src/emscripten-optimizer')
-rw-r--r--src/emscripten-optimizer/simple_ast.cpp18
-rw-r--r--src/emscripten-optimizer/simple_ast.h39
2 files changed, 48 insertions, 9 deletions
diff --git a/src/emscripten-optimizer/simple_ast.cpp b/src/emscripten-optimizer/simple_ast.cpp
index 618b0d9dd..75685f80e 100644
--- a/src/emscripten-optimizer/simple_ast.cpp
+++ b/src/emscripten-optimizer/simple_ast.cpp
@@ -64,11 +64,22 @@ Value& Value::setAssign(Ref target, Ref value) {
return *this;
}
+Value& Value::setAssignName(IString target, Ref value) {
+ asAssignName()->target() = target;
+ asAssignName()->value() = value;
+ return *this;
+}
+
Assign* Value::asAssign() {
assert(isAssign());
return static_cast<Assign*>(this);
}
+AssignName* Value::asAssignName() {
+ assert(isAssignName());
+ return static_cast<AssignName*>(this);
+}
+
void Value::stringify(std::ostream &os, bool pretty) {
static int indent = 0;
#define indentify() { for (int i_ = 0; i_ < indent; i_++) os << " "; }
@@ -153,6 +164,13 @@ void Value::stringify(std::ostream &os, bool pretty) {
os << "]";
break;
}
+ case AssignName_: {
+ os << "[\"" << asAssignName()->target().str << "\"";
+ os << ", ";
+ asAssignName()->value()->stringify(os, pretty);
+ os << "]";
+ break;
+ }
}
}
diff --git a/src/emscripten-optimizer/simple_ast.h b/src/emscripten-optimizer/simple_ast.h
index aa02dd8fc..efbd7b6f6 100644
--- a/src/emscripten-optimizer/simple_ast.h
+++ b/src/emscripten-optimizer/simple_ast.h
@@ -97,6 +97,7 @@ public:
};
struct Assign;
+struct AssignName;
// Main value type
struct Value {
@@ -107,7 +108,8 @@ struct Value {
Null = 3,
Bool = 4,
Object = 5,
- Assign_ = 6 // ref = target
+ Assign_ = 6, // ref = target
+ AssignName_ = 7
};
Type type;
@@ -203,6 +205,7 @@ struct Value {
return *this;
}
Value& setAssign(Ref target, Ref value);
+ Value& setAssignName(IString target, Ref value);
bool isString() { return type == String; }
bool isNumber() { return type == Number; }
@@ -211,6 +214,7 @@ struct Value {
bool isBool() { return type == Bool; }
bool isObject() { return type == Object; }
bool isAssign() { return type == Assign_; }
+ bool isAssignName() { return type == AssignName_; }
bool isBool(bool b) { return type == Bool && b == boo; } // avoid overloading == as it might overload over int
@@ -244,6 +248,7 @@ struct Value {
}
Assign* asAssign();
+ AssignName* asAssignName();
int32_t getInteger() { // convenience function to get a known integer
assert(fmod(getNumber(), 1) == 0);
@@ -496,6 +501,25 @@ struct Assign : public Value {
}
};
+struct AssignName : public Value {
+ IString target_;
+
+ AssignName(IString targetInit, Ref valueInit) {
+ type = AssignName_;
+ target() = targetInit;
+ value() = valueInit;
+ }
+
+ AssignName() : AssignName(IString(), nullptr) {}
+
+ IString& target() {
+ return target_;
+ }
+ Ref& value() {
+ return ref;
+ }
+};
+
// AST traversals
// Traverse, calling visit before the children
@@ -791,7 +815,11 @@ public:
static Ref makeBinary(Ref left, IString op, Ref right) {
if (op == SET) {
- return &arena.alloc<Assign>()->setAssign(left, right);
+ if (left->isString()) {
+ return &arena.alloc<AssignName>()->setAssignName(left->getIString(), right);
+ } else {
+ return &arena.alloc<Assign>()->setAssign(left, right);
+ }
} else if (op == COMMA) {
return &makeRawArray(3)->push_back(makeRawString(SEQ))
.push_back(left)
@@ -968,13 +996,6 @@ public:
.push_back(value));
}
- static Ref makeAssign(Ref target, Ref value) {
- return &arena.alloc<Assign>()->setAssign(target, value);
- }
- static Ref makeAssign(IString target, Ref value) {
- return &arena.alloc<Assign>()->setAssign(makeName(target), value);
- }
-
static Ref makeSub(Ref obj, Ref index) {
return &makeRawArray(2)->push_back(makeRawString(SUB))
.push_back(obj)