Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions src/asm2wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -392,17 +393,12 @@ class Asm2WasmBuilder {
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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions src/passes/LegalizeJSInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <pass.h>
#include <wasm-builder.h>
#include <ast_utils.h>
#include <ast/literal-utils.h>

namespace wasm {

Expand Down Expand Up @@ -226,12 +227,12 @@ struct LegalizeJSInterface : public Pass {

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
));
}
}
};
Expand Down
22 changes: 12 additions & 10 deletions src/tools/translate-to-fuzz.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ class TranslateToFuzzReader {
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);
}
Expand All @@ -199,11 +200,12 @@ class TranslateToFuzzReader {
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;
Expand Down
15 changes: 15 additions & 0 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,21 @@ class Builder {
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
Expand Down
14 changes: 8 additions & 6 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}
}

Expand Down