summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast_utils.h12
-rw-r--r--src/passes/Print.cpp13
-rw-r--r--src/wasm-binary.h14
-rw-r--r--src/wasm-builder.h8
-rw-r--r--src/wasm-interpreter.h20
-rw-r--r--src/wasm-s-parser.h22
-rw-r--r--src/wasm.h4
7 files changed, 39 insertions, 54 deletions
diff --git a/src/ast_utils.h b/src/ast_utils.h
index ea27c640f..f68f776af 100644
--- a/src/ast_utils.h
+++ b/src/ast_utils.h
@@ -284,10 +284,10 @@ struct ExpressionManipulator {
}
}
Expression* visitGetGlobal(GetGlobal *curr) {
- return builder.makeGetGlobal(curr->index, curr->type);
+ return builder.makeGetGlobal(curr->name, curr->type);
}
Expression* visitSetGlobal(SetGlobal *curr) {
- return builder.makeSetGlobal(curr->index, copy(curr->value));
+ return builder.makeSetGlobal(curr->name, copy(curr->value));
}
Expression* visitLoad(Load *curr) {
return builder.makeLoad(curr->bytes, curr->signed_, curr->offset, curr->align, copy(curr->ptr), curr->type);
@@ -493,11 +493,11 @@ struct ExpressionAnalyzer {
break;
}
case Expression::Id::GetGlobalId: {
- CHECK(GetGlobal, index);
+ CHECK(GetGlobal, name);
break;
}
case Expression::Id::SetGlobalId: {
- CHECK(SetGlobal, index);
+ CHECK(SetGlobal, name);
PUSH(SetGlobal, value);
break;
}
@@ -708,11 +708,11 @@ struct ExpressionAnalyzer {
break;
}
case Expression::Id::GetGlobalId: {
- HASH(GetGlobal, index);
+ HASH_NAME(GetGlobal, name);
break;
}
case Expression::Id::SetGlobalId: {
- HASH(SetGlobal, index);
+ HASH_NAME(SetGlobal, name);
PUSH(SetGlobal, value);
break;
}
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 3e5339932..fcd155650 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -82,11 +82,6 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
return name;
}
- Name printableGlobal(Index index) {
- if (currModule) return currModule->getGlobal(index)->name;
- return Name::fromInt(index);
- }
-
std::ostream& printName(Name name) {
// we need to quote names if they have tricky chars
if (strpbrk(name.str, "()")) {
@@ -250,10 +245,12 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
decIndent();
}
void visitGetGlobal(GetGlobal *curr) {
- printOpening(o, "get_global ") << printableGlobal(curr->index) << ')';
+ printOpening(o, "get_global ");
+ printName(curr->name) << ')';
}
void visitSetGlobal(SetGlobal *curr) {
- printOpening(o, "set_global ") << printableGlobal(curr->index);
+ printOpening(o, "set_global ");
+ printName(curr->name);
incIndent();
printFullLine(curr->value);
decIndent();
@@ -568,7 +565,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
void visitGlobal(Global *curr) {
printOpening(o, "global ");
printName(curr->name) << ' ' << printWasmType(curr->type);
- printFullLine(curr->init);
+ visit(curr->init);
o << ')';
}
void visitFunction(Function *curr) {
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 4db728fd6..d6671be01 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -971,12 +971,12 @@ public:
}
void visitGetGlobal(GetGlobal *curr) {
if (debug) std::cerr << "zz node: GetGlobal " << (o.size() + 1) << std::endl;
- o << int8_t(BinaryConsts::GetGlobal) << U32LEB(curr->index);
+ o << int8_t(BinaryConsts::GetGlobal) << U32LEB(getGlobalIndex(curr->name));
}
void visitSetGlobal(SetGlobal *curr) {
if (debug) std::cerr << "zz node: SetGlobal" << std::endl;
recurse(curr->value);
- o << int8_t(BinaryConsts::SetGlobal) << U32LEB(curr->index);
+ o << int8_t(BinaryConsts::SetGlobal) << U32LEB(getGlobalIndex(curr->name));
}
void emitMemoryAccess(size_t alignment, size_t bytes, uint32_t offset) {
@@ -1974,14 +1974,14 @@ public:
}
void visitGetGlobal(GetGlobal *curr) {
if (debug) std::cerr << "zz node: GetGlobal " << pos << std::endl;
- curr->index = getU32LEB();
- assert(curr->index < wasm.globals.size());
- curr->type = wasm.globals[curr->index]->type;
+ auto index = getU32LEB();
+ curr->name = wasm.getGlobal(index)->name;
+ curr->type = wasm.getGlobal(index)->type;
}
void visitSetGlobal(SetGlobal *curr) {
if (debug) std::cerr << "zz node: SetGlobal" << std::endl;
- curr->index = getU32LEB();
- assert(curr->index < wasm.globals.size());
+ auto index = getU32LEB();
+ curr->name = wasm.getGlobal(index)->name;
curr->value = popExpression();
}
diff --git a/src/wasm-builder.h b/src/wasm-builder.h
index d5e94a25b..2841585e6 100644
--- a/src/wasm-builder.h
+++ b/src/wasm-builder.h
@@ -152,15 +152,15 @@ public:
ret->type = value->type;
return ret;
}
- GetGlobal* makeGetGlobal(Index index, WasmType type) {
+ GetGlobal* makeGetGlobal(Name name, WasmType type) {
auto* ret = allocator.alloc<GetGlobal>();
- ret->index = index;
+ ret->name = name;
ret->type = type;
return ret;
}
- SetGlobal* makeSetGlobal(Index index, Expression* value) {
+ SetGlobal* makeSetGlobal(Name name, Expression* value) {
auto* ret = allocator.alloc<SetGlobal>();
- ret->index = index;
+ ret->name = name;
ret->value = value;
return ret;
}
diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h
index ae42ca648..db55cd143 100644
--- a/src/wasm-interpreter.h
+++ b/src/wasm-interpreter.h
@@ -529,12 +529,12 @@ public:
Module& wasm;
// Values of globals
- std::vector<Literal> globals;
+ std::map<Name, Literal> globals;
ModuleInstance(Module& wasm, ExternalInterface* externalInterface) : wasm(wasm), externalInterface(externalInterface) {
memorySize = wasm.memory.initial;
- for (Index i = 0; i < wasm.globals.size(); i++) {
- globals.push_back(ConstantExpressionRunner().visit(wasm.globals[i]->init).value);
+ for (auto& global : wasm.globals) {
+ globals[global->name] = ConstantExpressionRunner().visit(global->init).value;
}
externalInterface->init(wasm);
if (wasm.start.is()) {
@@ -682,19 +682,19 @@ public:
Flow visitGetGlobal(GetGlobal *curr) {
NOTE_ENTER("GetGlobal");
- auto index = curr->index;
- NOTE_EVAL1(index);
- NOTE_EVAL1(instance.globals[index]);
- return instance.globals[index];
+ auto name = curr->name;
+ NOTE_EVAL1(name);
+ NOTE_EVAL1(instance.globals[name]);
+ return instance.globals[name];
}
Flow visitSetGlobal(SetGlobal *curr) {
NOTE_ENTER("SetGlobal");
- auto index = curr->index;
+ auto name = curr->name;
Flow flow = visit(curr->value);
if (flow.breaking()) return flow;
- NOTE_EVAL1(index);
+ NOTE_EVAL1(name);
NOTE_EVAL1(flow.value);
- instance.globals[index] = flow.value;
+ instance.globals[name] = flow.value;
return Flow();
}
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h
index 50d566baf..60cc21e26 100644
--- a/src/wasm-s-parser.h
+++ b/src/wasm-s-parser.h
@@ -945,30 +945,18 @@ private:
return ret;
}
- Index getGlobalIndex(Element& s) {
- if (s.dollared()) {
- auto name = s.str();
- for (Index i = 0; i < wasm.globals.size(); i++) {
- if (wasm.globals[i]->name == name) return i;
- }
- throw ParseException("bad global name", s.line, s.col);
- }
- // this is a numeric index
- Index ret = atoi(s.c_str());
- if (!wasm.checkGlobal(ret)) throw ParseException("bad global index", s.line, s.col);
- return ret;
- }
-
Expression* makeGetGlobal(Element& s) {
auto ret = allocator.alloc<GetGlobal>();
- ret->index = getGlobalIndex(*s[1]);
- ret->type = wasm.getGlobal(ret->index)->type;
+ ret->name = s[1]->str();
+ auto* global = wasm.checkGlobal(ret->name);
+ if (!global) throw ParseException("bad get_global name", s.line, s.col);
+ ret->type = global->type;
return ret;
}
Expression* makeSetGlobal(Element& s) {
auto ret = allocator.alloc<SetGlobal>();
- ret->index = getGlobalIndex(*s[1]);
+ ret->name = s[1]->str();
ret->value = parseExpression(s[2]);
return ret;
}
diff --git a/src/wasm.h b/src/wasm.h
index a2f6a74db..1cf00cc36 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -1125,7 +1125,7 @@ public:
GetGlobal() {}
GetGlobal(MixedArena& allocator) {}
- Index index;
+ Name name;
};
class SetGlobal : public SpecificExpression<Expression::SetGlobalId> {
@@ -1133,7 +1133,7 @@ public:
SetGlobal() {}
SetGlobal(MixedArena& allocator) {}
- Index index;
+ Name name;
Expression *value;
};