summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2017-10-10 18:53:39 -0700
committerGitHub <noreply@github.com>2017-10-10 18:53:39 -0700
commit110f9bdf13d141253c19fb3c46a4dd07b61232ab (patch)
treecc41bc14de588e29f9296aa3762187e16d35a562 /src
parent76c6883057d1249a626468bea7af4dcfd32e5ae7 (diff)
downloadbinaryen-110f9bdf13d141253c19fb3c46a4dd07b61232ab.tar.gz
binaryen-110f9bdf13d141253c19fb3c46a4dd07b61232ab.tar.bz2
binaryen-110f9bdf13d141253c19fb3c46a4dd07b61232ab.zip
Add Builder::makeGlobal for nicer global creation (#1221)
Diffstat (limited to 'src')
-rw-r--r--src/asm2wasm.h41
-rw-r--r--src/passes/LegalizeJSInterface.cpp13
-rw-r--r--src/tools/translate-to-fuzz.h22
-rw-r--r--src/wasm-builder.h15
-rw-r--r--src/wasm/wasm-binary.cpp14
5 files changed, 61 insertions, 44 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index fc21b945b..8f3c771be 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -33,6 +33,7 @@
#include "parsing.h"
#include "ast_utils.h"
#include "ast/branch-utils.h"
+#include "ast/literal-utils.h"
#include "ast/trapping.h"
#include "wasm-builder.h"
#include "wasm-emscripten.h"
@@ -392,17 +393,12 @@ private:
void allocateGlobal(IString name, WasmType type) {
assert(mappedGlobals.find(name) == mappedGlobals.end());
mappedGlobals.emplace(name, MappedGlobal(type));
- auto global = new Global();
- global->name = name;
- global->type = type;
- Literal value;
- if (type == i32) value = Literal(uint32_t(0));
- else if (type == f32) value = Literal(float(0));
- else if (type == f64) value = Literal(double(0));
- else WASM_UNREACHABLE();
- global->init = wasm.allocator.alloc<Const>()->set(value);
- global->mutable_ = true;
- wasm.addGlobal(global);
+ wasm.addGlobal(builder.makeGlobal(
+ name,
+ type,
+ LiteralUtils::makeZero(type, wasm),
+ Builder::Mutable
+ ));
}
struct View {
@@ -838,12 +834,12 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
// 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);
+ wasm.addGlobal(builder.makeGlobal(
+ name,
+ type,
+ builder.makeGetGlobal(import->name, type),
+ Builder::Mutable
+ ));
}
}
} else {
@@ -1076,11 +1072,12 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
assert(pair[1]->isNumber());
assert(exported.count(key) == 0);
auto value = pair[1]->getInteger();
- auto global = new Global();
- global->name = key;
- global->type = i32;
- global->init = builder.makeConst(Literal(int32_t(value)));
- global->mutable_ = false;
+ auto* global = builder.makeGlobal(
+ key,
+ i32,
+ builder.makeConst(Literal(int32_t(value))),
+ Builder::Immutable
+ );
wasm.addGlobal(global);
auto* export_ = new Export;
export_->name = key;
diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp
index b52c1d330..cbd34e407 100644
--- a/src/passes/LegalizeJSInterface.cpp
+++ b/src/passes/LegalizeJSInterface.cpp
@@ -30,6 +30,7 @@
#include <pass.h>
#include <wasm-builder.h>
#include <ast_utils.h>
+#include <ast/literal-utils.h>
namespace wasm {
@@ -226,12 +227,12 @@ private:
void ensureTempRet0(Module* module) {
if (!module->getGlobalOrNull(TEMP_RET_0)) {
- Global* global = new Global;
- global->name = TEMP_RET_0;
- global->type = i32;
- global->init = module->allocator.alloc<Const>()->set(Literal(int32_t(0)));
- global->mutable_ = true;
- module->addGlobal(global);
+ module->addGlobal(Builder::makeGlobal(
+ TEMP_RET_0,
+ i32,
+ LiteralUtils::makeZero(i32, *module),
+ Builder::Mutable
+ ));
}
}
};
diff --git a/src/tools/translate-to-fuzz.h b/src/tools/translate-to-fuzz.h
index 415d90ac0..a316e6528 100644
--- a/src/tools/translate-to-fuzz.h
+++ b/src/tools/translate-to-fuzz.h
@@ -180,11 +180,12 @@ private:
for (auto type : { i32, i64, f32, f64 }) {
auto num = upTo(3);
for (size_t i = 0; i < num; i++) {
- auto* glob = new Global;
- glob->name = std::string("global$") + std::to_string(index++);
- glob->type = type;
- glob->init = makeConst(type);
- glob->mutable_ = true;
+ auto* glob = builder.makeGlobal(
+ std::string("global$") + std::to_string(index++),
+ type,
+ makeConst(type),
+ Builder::Mutable
+ );
wasm.addGlobal(glob);
globalsByType[type].push_back(glob->name);
}
@@ -199,11 +200,12 @@ private:
const Name HANG_LIMIT_GLOBAL = "hangLimit";
void addHangLimitSupport() {
- auto* glob = new Global;
- glob->name = HANG_LIMIT_GLOBAL;
- glob->type = i32;
- glob->init = builder.makeConst(Literal(int32_t(HANG_LIMIT)));
- glob->mutable_ = true;
+ auto* glob = builder.makeGlobal(
+ HANG_LIMIT_GLOBAL,
+ i32,
+ builder.makeConst(Literal(int32_t(HANG_LIMIT))),
+ Builder::Mutable
+ );
wasm.addGlobal(glob);
auto* func = new Function;
diff --git a/src/wasm-builder.h b/src/wasm-builder.h
index cca97a869..bd756297e 100644
--- a/src/wasm-builder.h
+++ b/src/wasm-builder.h
@@ -445,6 +445,21 @@ public:
return makeConst(value);
}
+ // Module-level helpers
+
+ enum Mutability {
+ Mutable,
+ Immutable
+ };
+
+ static Global* makeGlobal(Name name, WasmType type, Expression* init, Mutability mutable_) {
+ auto* glob = new Global;
+ glob->name = name;
+ glob->type = type;
+ glob->init = init;
+ glob->mutable_ = mutable_ == Mutable;
+ return glob;
+ }
};
} // namespace wasm
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index f7e10d39b..ceb230f73 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -1823,14 +1823,16 @@ void WasmBinaryBuilder::readGlobals() {
if (debug) std::cerr << "num: " << num << std::endl;
for (size_t i = 0; i < num; i++) {
if (debug) std::cerr << "read one" << std::endl;
- auto curr = new Global;
- curr->type = getWasmType();
+ auto type = getWasmType();
auto mutable_ = getU32LEB();
if (bool(mutable_) != mutable_) throw ParseException("Global mutability must be 0 or 1");
- curr->mutable_ = mutable_;
- curr->init = readExpression();
- curr->name = Name("global$" + std::to_string(wasm.globals.size()));
- wasm.addGlobal(curr);
+ auto* init = readExpression();
+ wasm.addGlobal(Builder::makeGlobal(
+ "global$" + std::to_string(wasm.globals.size()),
+ type,
+ init,
+ mutable_ ? Builder::Mutable : Builder::Immutable
+ ));
}
}