summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/asm2wasm.h33
-rw-r--r--src/binaryen-c.cpp1
-rw-r--r--src/passes/Print.cpp8
-rw-r--r--src/wasm-binary.h1
-rw-r--r--src/wasm/wasm-binary.cpp9
5 files changed, 30 insertions, 22 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index 6ed13b79a..fe9bd0f3f 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -649,19 +649,24 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
type = WasmType::f64;
}
if (type != WasmType::none) {
- // we need imported globals to be mutable, but wasm doesn't support that yet, so we must
- // import an immutable and create a mutable global initialized to its value
- import->name = Name(std::string(import->name.str) + "$asm2wasm$import");
+ // this is a global
import->kind = ExternalKind::Global;
import->globalType = type;
mappedGlobals.emplace(name, type);
- {
- auto global = new Global();
- global->name = name;
- global->type = type;
- global->init = builder.makeGetGlobal(import->name, type);
- global->mutable_ = true;
- wasm.addGlobal(global);
+ // tableBase and memoryBase are used as segment/element offsets, and must be constant;
+ // otherwise, an asm.js import of a constant is mutable, e.g. STACKTOP
+ if (name != "tableBase" && name != "memoryBase") {
+ // we need imported globals to be mutable, but wasm doesn't support that yet, so we must
+ // import an immutable and create a mutable global initialized to its value
+ import->name = Name(std::string(import->name.str) + "$asm2wasm$import");
+ {
+ auto global = new Global();
+ global->name = name;
+ global->type = type;
+ global->init = builder.makeGetGlobal(import->name, type);
+ global->mutable_ = true;
+ wasm.addGlobal(global);
+ }
}
} else {
import->kind = ExternalKind::Function;
@@ -1059,8 +1064,8 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
wasm.table.exists = true;
wasm.table.imported = true;
- // Import memory offset
- {
+ // Import memory offset, if not already there
+ if (!wasm.checkImport("memoryBase") && !wasm.checkGlobal("memoryBase")) {
auto* import = new Import;
import->name = Name("memoryBase");
import->module = Name("env");
@@ -1070,8 +1075,8 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
wasm.addImport(import);
}
- // Import table offset
- {
+ // Import table offset, if not already there
+ if (!wasm.checkImport("tableBase") && !wasm.checkGlobal("tableBase")) {
auto* import = new Import;
import->name = Name("tableBase");
import->module = Name("env");
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index dc68d8439..c618f9d14 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -808,6 +808,7 @@ void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, Binaryen
auto* wasm = (Module*)module;
wasm->memory.initial = initial;
wasm->memory.max = maximum;
+ wasm->memory.exists = true;
if (exportName) {
auto memoryExport = make_unique<Export>();
memoryExport->name = exportName;
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index c6fca0256..3c847809f 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -698,15 +698,15 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
visitImport(child.get());
o << maybeNewLine;
}
- if (curr->table.exists) {
- visitTable(&curr->table); // Prints its own newlines
- }
- visitMemory(&curr->memory);
for (auto& child : curr->globals) {
doIndent(o, indent);
visitGlobal(child.get());
o << maybeNewLine;
}
+ if (curr->table.exists) {
+ visitTable(&curr->table); // Prints its own newlines
+ }
+ visitMemory(&curr->memory);
for (auto& child : curr->exports) {
doIndent(o, indent);
visitExport(child.get());
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 3f5b799fe..75733a6b5 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -690,6 +690,7 @@ public:
void readFunctions();
std::map<Export*, Index> exportIndexes;
+ std::vector<Export*> exportOrder;
void readExports();
Expression* readExpression();
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 65bf0a268..6b5878904 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -1281,6 +1281,7 @@ void WasmBinaryBuilder::readExports() {
curr->kind = (ExternalKind)getU32LEB();
auto index = getU32LEB();
exportIndexes[curr] = index;
+ exportOrder.push_back(curr);
}
}
@@ -1383,16 +1384,16 @@ void WasmBinaryBuilder::processFunctions() {
wasm.start = getFunctionIndexName(startIndex);
}
- for (auto& iter : exportIndexes) {
- Export* curr = iter.first;
+ for (auto* curr : exportOrder) {
+ auto index = exportIndexes[curr];
switch (curr->kind) {
case ExternalKind::Function: {
- curr->value = getFunctionIndexName(iter.second);
+ curr->value = getFunctionIndexName(index);
break;
}
case ExternalKind::Table: curr->value = Name::fromInt(0); break;
case ExternalKind::Memory: curr->value = Name::fromInt(0); break;
- case ExternalKind::Global: curr->value = getGlobalName(iter.second); break;
+ case ExternalKind::Global: curr->value = getGlobalName(index); break;
default: WASM_UNREACHABLE();
}
wasm.addExport(curr);