Skip to content

Commit 110f9bd

Browse files
authored
Add Builder::makeGlobal for nicer global creation (#1221)
1 parent 76c6883 commit 110f9bd

File tree

5 files changed

+61
-44
lines changed

5 files changed

+61
-44
lines changed

src/asm2wasm.h

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "parsing.h"
3434
#include "ast_utils.h"
3535
#include "ast/branch-utils.h"
36+
#include "ast/literal-utils.h"
3637
#include "ast/trapping.h"
3738
#include "wasm-builder.h"
3839
#include "wasm-emscripten.h"
@@ -392,17 +393,12 @@ class Asm2WasmBuilder {
392393
void allocateGlobal(IString name, WasmType type) {
393394
assert(mappedGlobals.find(name) == mappedGlobals.end());
394395
mappedGlobals.emplace(name, MappedGlobal(type));
395-
auto global = new Global();
396-
global->name = name;
397-
global->type = type;
398-
Literal value;
399-
if (type == i32) value = Literal(uint32_t(0));
400-
else if (type == f32) value = Literal(float(0));
401-
else if (type == f64) value = Literal(double(0));
402-
else WASM_UNREACHABLE();
403-
global->init = wasm.allocator.alloc<Const>()->set(value);
404-
global->mutable_ = true;
405-
wasm.addGlobal(global);
396+
wasm.addGlobal(builder.makeGlobal(
397+
name,
398+
type,
399+
LiteralUtils::makeZero(type, wasm),
400+
Builder::Mutable
401+
));
406402
}
407403

408404
struct View {
@@ -838,12 +834,12 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
838834
// import an immutable and create a mutable global initialized to its value
839835
import->name = Name(std::string(import->name.str) + "$asm2wasm$import");
840836
{
841-
auto global = new Global();
842-
global->name = name;
843-
global->type = type;
844-
global->init = builder.makeGetGlobal(import->name, type);
845-
global->mutable_ = true;
846-
wasm.addGlobal(global);
837+
wasm.addGlobal(builder.makeGlobal(
838+
name,
839+
type,
840+
builder.makeGetGlobal(import->name, type),
841+
Builder::Mutable
842+
));
847843
}
848844
}
849845
} else {
@@ -1076,11 +1072,12 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
10761072
assert(pair[1]->isNumber());
10771073
assert(exported.count(key) == 0);
10781074
auto value = pair[1]->getInteger();
1079-
auto global = new Global();
1080-
global->name = key;
1081-
global->type = i32;
1082-
global->init = builder.makeConst(Literal(int32_t(value)));
1083-
global->mutable_ = false;
1075+
auto* global = builder.makeGlobal(
1076+
key,
1077+
i32,
1078+
builder.makeConst(Literal(int32_t(value))),
1079+
Builder::Immutable
1080+
);
10841081
wasm.addGlobal(global);
10851082
auto* export_ = new Export;
10861083
export_->name = key;

src/passes/LegalizeJSInterface.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <pass.h>
3131
#include <wasm-builder.h>
3232
#include <ast_utils.h>
33+
#include <ast/literal-utils.h>
3334

3435
namespace wasm {
3536

@@ -226,12 +227,12 @@ struct LegalizeJSInterface : public Pass {
226227

227228
void ensureTempRet0(Module* module) {
228229
if (!module->getGlobalOrNull(TEMP_RET_0)) {
229-
Global* global = new Global;
230-
global->name = TEMP_RET_0;
231-
global->type = i32;
232-
global->init = module->allocator.alloc<Const>()->set(Literal(int32_t(0)));
233-
global->mutable_ = true;
234-
module->addGlobal(global);
230+
module->addGlobal(Builder::makeGlobal(
231+
TEMP_RET_0,
232+
i32,
233+
LiteralUtils::makeZero(i32, *module),
234+
Builder::Mutable
235+
));
235236
}
236237
}
237238
};

src/tools/translate-to-fuzz.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ class TranslateToFuzzReader {
180180
for (auto type : { i32, i64, f32, f64 }) {
181181
auto num = upTo(3);
182182
for (size_t i = 0; i < num; i++) {
183-
auto* glob = new Global;
184-
glob->name = std::string("global$") + std::to_string(index++);
185-
glob->type = type;
186-
glob->init = makeConst(type);
187-
glob->mutable_ = true;
183+
auto* glob = builder.makeGlobal(
184+
std::string("global$") + std::to_string(index++),
185+
type,
186+
makeConst(type),
187+
Builder::Mutable
188+
);
188189
wasm.addGlobal(glob);
189190
globalsByType[type].push_back(glob->name);
190191
}
@@ -199,11 +200,12 @@ class TranslateToFuzzReader {
199200
const Name HANG_LIMIT_GLOBAL = "hangLimit";
200201

201202
void addHangLimitSupport() {
202-
auto* glob = new Global;
203-
glob->name = HANG_LIMIT_GLOBAL;
204-
glob->type = i32;
205-
glob->init = builder.makeConst(Literal(int32_t(HANG_LIMIT)));
206-
glob->mutable_ = true;
203+
auto* glob = builder.makeGlobal(
204+
HANG_LIMIT_GLOBAL,
205+
i32,
206+
builder.makeConst(Literal(int32_t(HANG_LIMIT))),
207+
Builder::Mutable
208+
);
207209
wasm.addGlobal(glob);
208210

209211
auto* func = new Function;

src/wasm-builder.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,21 @@ class Builder {
445445
return makeConst(value);
446446
}
447447

448+
// Module-level helpers
449+
450+
enum Mutability {
451+
Mutable,
452+
Immutable
453+
};
454+
455+
static Global* makeGlobal(Name name, WasmType type, Expression* init, Mutability mutable_) {
456+
auto* glob = new Global;
457+
glob->name = name;
458+
glob->type = type;
459+
glob->init = init;
460+
glob->mutable_ = mutable_ == Mutable;
461+
return glob;
462+
}
448463
};
449464

450465
} // namespace wasm

src/wasm/wasm-binary.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,14 +1823,16 @@ void WasmBinaryBuilder::readGlobals() {
18231823
if (debug) std::cerr << "num: " << num << std::endl;
18241824
for (size_t i = 0; i < num; i++) {
18251825
if (debug) std::cerr << "read one" << std::endl;
1826-
auto curr = new Global;
1827-
curr->type = getWasmType();
1826+
auto type = getWasmType();
18281827
auto mutable_ = getU32LEB();
18291828
if (bool(mutable_) != mutable_) throw ParseException("Global mutability must be 0 or 1");
1830-
curr->mutable_ = mutable_;
1831-
curr->init = readExpression();
1832-
curr->name = Name("global$" + std::to_string(wasm.globals.size()));
1833-
wasm.addGlobal(curr);
1829+
auto* init = readExpression();
1830+
wasm.addGlobal(Builder::makeGlobal(
1831+
"global$" + std::to_string(wasm.globals.size()),
1832+
type,
1833+
init,
1834+
mutable_ ? Builder::Mutable : Builder::Immutable
1835+
));
18341836
}
18351837
}
18361838

0 commit comments

Comments
 (0)