Skip to content

Commit bc17bcb

Browse files
committed
start func
1 parent 6716ad7 commit bc17bcb

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

src/passes/LLVM.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ struct LLVMOpt : public Pass {
5050
for (auto& e : module->exports) {
5151
originalExports.insert(e->name);
5252
}
53+
// We will add additional exports, some of which we can forget about
54+
// imediately, but others we need to keep around until almost the very end
55+
// because we need to use them.
56+
// For example, we add an export for the start function if there is one, and
57+
// we find it in the output wasm and use it to find the start function. We
58+
// can then remove the export
59+
std::set<Name> usedExports;
5360

5461
// Add exports for more things that we need, either for the wasm2c runtime,
5562
// or to make it easy for us to find what we need afterwards. (We will
@@ -65,10 +72,18 @@ struct LLVMOpt : public Pass {
6572
}
6673
// Ensure a _start is exported, which wasm2c expects.
6774
if (!module->getExportOrNull("_start")) {
75+
Name name("byn_llvm_runtime_start");
6876
module->addFunction(builder.makeFunction(
69-
"byn$llvm-start", {Type::none, Type::none}, {}, builder.makeNop()));
77+
name, {Type::none, Type::none}, {}, builder.makeNop()));
7078
module->addExport(
71-
builder.makeExport("_start", "byn$llvm-start", ExternalKind::Function));
79+
builder.makeExport("_start", name, ExternalKind::Function));
80+
}
81+
// Export the wasm start function, if there is one.
82+
Name wasmStartExport("byn_llvm_wasm_start");
83+
if (module->start.is()) {
84+
module->addExport(
85+
builder.makeExport(wasmStartExport, module->start, ExternalKind::Function));
86+
usedExports.insert(wasmStartExport);
7287
}
7388

7489
// Write the module to a temp file.
@@ -103,14 +118,18 @@ struct LLVMOpt : public Pass {
103118
std::string cmd =
104119
"emcc " + tempC + " -o " + tempWasmC + " -O1 -s EXPORTED_FUNCTIONS=";
105120
bool first = true;
106-
for (auto e : originalExports) {
107-
if (first) {
108-
first = false;
109-
} else {
110-
cmd += ',';
121+
auto addExports = [&](const std::set<Name>& names) {
122+
for (auto e : names) {
123+
if (first) {
124+
first = false;
125+
} else {
126+
cmd += ',';
127+
}
128+
cmd += std::string("_w2c_") + e.str;
111129
}
112-
cmd += std::string("_w2c_") + e.str;
113-
}
130+
};
131+
addExports(originalExports);
132+
addExports(usedExports);
114133
ProgramResult c2wasm(cmd);
115134
if (c2wasm.failed()) {
116135
c2wasm.dump(std::cout);
@@ -135,6 +154,11 @@ struct LLVMOpt : public Pass {
135154
return false;
136155
}),
137156
module->exports.end());
157+
module->updateMaps();
158+
// Find the important things we exported so that we could find them later.
159+
if (auto* e = module->getExportOrNull(wasmStartExport)) {
160+
module->start = e->value;
161+
}
138162
// Remove the table: the "native" table contains things the new sandboxing
139163
// layer in C added and needs. We want to look into the wasm in that
140164
// sandbox.

test/passes/llvm.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,20 @@
77
[LLVMOpt] postprocessing...
88
(module
99
(type $none_=>_i32 (func (result i32)))
10+
(type $none_=>_none (func))
11+
(memory $0 256 256)
12+
(data (i32.const 1024) "-+ 0X0x\00(null)\00[wasm trap %d, halting]\n\00\00\00\00\00\00\00\11\00\n\00\11\11\11\00\00\00\00\05\00\00\00\00\00\00\t\00\00\00\00\0b\00\00\00\00\00\00\00\00\11\00\0f\n\11\11\11\03\n\07\00\01\00\t\0b\0b\00\00\t\06\0b\00\00\0b\00\06\11\00\00\00\11\11\11\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0b\00\00\00\00\00\00\00\00\11\00\n\n\11\11\11\00\n\00\00\02\00\t\0b\00\00\00\t\00\0b\00\00\0b\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0c\00\00\00\00\00\00\00\00\00\00\00\0c\00\00\00\00\0c\00\00\00\00\t\0c\00\00\00\00\00\0c\00\00\0c\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0e\00\00\00\00\00\00\00\00\00\00\00\0d\00\00\00\04\0d\00\00\00\00\t\0e\00\00\00\00\00\0e\00\00\0e\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\00\00\00\00\0f\00\00\00\00\0f\00\00\00\00\t\10\00\00\00\00\00\10\00\00\10\00\00\12\00\00\00\12\12\12\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\12\00\00\00\12\12\12\00\00\00\00\00\00\t\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0b\00\00\00\00\00\00\00\00\00\00\00\n\00\00\00\00\n\00\00\00\00\t\0b\00\00\00\00\00\0b\00\00\0b\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0c\00\00\00\00\00\00\00\00\00\00\00\0c\00\00\00\00\0c\00\00\00\00\t\0c\00\00\00\00\00\0c\00\00\0c\00\000123456789ABCDEF\18\06\00\00")
13+
(data (i32.const 1560) "\05\00\00\00\00\00\00\00\00\00\00\00\08\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\t\00\00\00\n\00\00\00\98\18\00\00\00\04\00\00\00\00\00\00\00\00\00\00\01\00\00\00\00\00\00\00\00\00\00\00\00\00\00\n\ff\ff\ff\ff\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\18\06\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\c0\1c\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\f0\1eP\00")
1014
(export "foo" (func $2))
15+
(export "byn_llvm_wasm_start" (func $3))
16+
(start $3)
1117
(func $2 (result i32)
1218
(i32.const 42)
1319
)
20+
(func $3
21+
(i32.store offset=1960
22+
(i32.const 0)
23+
(i32.const 2)
24+
)
25+
)
1426
)

test/passes/llvm.wast

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
(module
2+
(global $global (mut i32) (i32.const 1))
3+
(start $startey)
24
(func "foo" (result i32)
35
(i32.add
46
(i32.const 41)
57
(i32.const 1)
68
)
79
)
10+
(func $startey
11+
(global.set $global
12+
(i32.const 2)
13+
)
14+
)
815
)

0 commit comments

Comments
 (0)