Skip to content

Commit 00d02f7

Browse files
authored
Refactor stack IR / binary writer (NFC) (#2250)
Previously `StackWriter` and its subclasses had routines for all three modes (`Binaryen2Binary`, `Binaryen2Stack`, and `Stack2Binary`) within a single class. This splits routines for each in a separate class and also factors out binary writing into a separate class (`BinaryInstWriter`) so other classes can make use of it. The new classes are: - `BinaryInstWriter`: Binary instruction writer. Only responsible for emitting binary contents and no other logic - `BinaryenIRWriter`: Converts binaryen IR into something else - `BinaryenIRToBinaryWriter`: Writes binaryen IR to binary - `StackIRGenerator`: Converts binaryen IR to stack IR - `StackIRToBinaryWriter`: Writes stack IR to binary
1 parent ff2b10b commit 00d02f7

6 files changed

Lines changed: 2014 additions & 1984 deletions

File tree

build-js.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ mkdir -p ${OUT}
167167
$BINARYEN_SRC/wasm/wasm-interpreter.cpp \
168168
$BINARYEN_SRC/wasm/wasm-io.cpp \
169169
$BINARYEN_SRC/wasm/wasm-s-parser.cpp \
170+
$BINARYEN_SRC/wasm/wasm-stack.cpp \
170171
$BINARYEN_SRC/wasm/wasm-type.cpp \
171172
$BINARYEN_SRC/wasm/wasm-validator.cpp \
172173
$BINARYEN_SRC/wasm/wasm.cpp \

src/passes/StackIR.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,10 @@ struct GenerateStackIR : public WalkerPass<PostWalker<GenerateStackIR>> {
3636
bool modifiesBinaryenIR() override { return false; }
3737

3838
void doWalkFunction(Function* func) {
39-
BufferWithRandomAccess buffer;
40-
// a shim for the parent that a stackWriter expects - we don't need
41-
// it to do anything, as we are just writing to Stack IR
42-
struct Parent {
43-
Module* module;
44-
Parent(Module* module) : module(module) {}
45-
46-
Module* getModule() { return module; }
47-
void writeDebugLocation(Expression* curr, Function* func) {
48-
WASM_UNREACHABLE();
49-
}
50-
Index getFunctionIndex(Name name) { WASM_UNREACHABLE(); }
51-
Index getFunctionTypeIndex(Name name) { WASM_UNREACHABLE(); }
52-
Index getGlobalIndex(Name name) { WASM_UNREACHABLE(); }
53-
} parent(getModule());
54-
StackWriter<StackWriterMode::Binaryen2Stack, Parent> stackWriter(
55-
parent, buffer, false);
56-
stackWriter.setFunction(func);
57-
stackWriter.visitPossibleBlockContents(func->body);
39+
StackIRGenerator stackIRGen(getModule()->allocator, func);
40+
stackIRGen.write();
5841
func->stackIR = make_unique<StackIR>();
59-
func->stackIR->swap(stackWriter.stackIR);
42+
func->stackIR->swap(stackIRGen.getStackIR());
6043
}
6144
};
6245

0 commit comments

Comments
 (0)