Skip to content

Commit eaa7240

Browse files
authored
JS/Wasm BigInt support for wasm-emscripten-finalize (#2726)
If wasm-emscripten-finalize is given the BigInt flag, then we will be using BigInts on the JS side, and need no legalization at all since i64s will just be BigInts.
1 parent ac8b0b7 commit eaa7240

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

scripts/test/lld.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def args_for_finalize(filename):
2626
ret += ['--side-module']
2727
if 'standalone-wasm' in filename:
2828
ret += ['--standalone-wasm']
29+
if 'bigint' in filename:
30+
ret += ['--bigint']
2931
return ret
3032

3133

src/tools/wasm-emscripten-finalize.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ int main(int argc, const char* argv[]) {
5252
bool DWARF = false;
5353
bool sideModule = false;
5454
bool legalizeJavaScriptFFI = true;
55+
bool bigInt = false;
5556
bool checkStackOverflow = false;
5657
uint64_t globalBase = INVALID_BASE;
5758
bool standaloneWasm = false;
@@ -118,6 +119,13 @@ int main(int argc, const char* argv[]) {
118119
[&legalizeJavaScriptFFI](Options* o, const std::string&) {
119120
legalizeJavaScriptFFI = false;
120121
})
122+
.add("--bigint",
123+
"-bi",
124+
"Assume JS will use wasm/JS BigInt integration, so wasm i64s will "
125+
"turn into JS BigInts, and there is no need for any legalization at "
126+
"all (not even minimal legalization of dynCalls)",
127+
Options::Arguments::Zero,
128+
[&bigInt](Options* o, const std::string&) { bigInt = true; })
121129
.add("--output-source-map",
122130
"-osm",
123131
"Emit source map to the specified file",
@@ -272,8 +280,8 @@ int main(int argc, const char* argv[]) {
272280
generator.generateDynCallThunks();
273281
}
274282

275-
// Legalize the wasm.
276-
{
283+
// Legalize the wasm, if BigInts don't make that moot.
284+
if (!bigInt) {
277285
BYN_TRACE("legalizing types\n");
278286
PassRunner passRunner(&wasm);
279287
passRunner.setOptions(options.passOptions);

test/lld/bigint.wat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(table $0 1 1 funcref)
3+
(elem (i32.const 1) $foo)
4+
(export "__data_end" (global $global$1))
5+
(global $global$0 (mut i32) (i32.const 66208))
6+
(global $global$1 i32 (i32.const 658))
7+
(func $foo (param i64) (result i64)
8+
(unreachable)
9+
)
10+
)

test/lld/bigint.wat.out

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
(module
2+
(type $i32_=>_i32 (func (param i32) (result i32)))
3+
(type $i64_=>_i64 (func (param i64) (result i64)))
4+
(type $i32_=>_none (func (param i32)))
5+
(type $none_=>_i32 (func (result i32)))
6+
(type $i32_i64_=>_i64 (func (param i32 i64) (result i64)))
7+
(table $0 1 1 funcref)
8+
(elem (i32.const 1) $foo)
9+
(global $global$0 (mut i32) (i32.const 66208))
10+
(global $global$1 i32 (i32.const 658))
11+
(export "__data_end" (global $global$1))
12+
(export "stackSave" (func $stackSave))
13+
(export "stackAlloc" (func $stackAlloc))
14+
(export "stackRestore" (func $stackRestore))
15+
(export "__growWasmMemory" (func $__growWasmMemory))
16+
(export "dynCall_jj" (func $dynCall_jj))
17+
(func $foo (; 0 ;) (param $0 i64) (result i64)
18+
(unreachable)
19+
)
20+
(func $stackSave (; 1 ;) (result i32)
21+
(global.get $global$0)
22+
)
23+
(func $stackAlloc (; 2 ;) (param $0 i32) (result i32)
24+
(local $1 i32)
25+
(global.set $global$0
26+
(local.tee $1
27+
(i32.and
28+
(i32.sub
29+
(global.get $global$0)
30+
(local.get $0)
31+
)
32+
(i32.const -16)
33+
)
34+
)
35+
)
36+
(local.get $1)
37+
)
38+
(func $stackRestore (; 3 ;) (param $0 i32)
39+
(global.set $global$0
40+
(local.get $0)
41+
)
42+
)
43+
(func $__growWasmMemory (; 4 ;) (param $newSize i32) (result i32)
44+
(memory.grow
45+
(local.get $newSize)
46+
)
47+
)
48+
(func $dynCall_jj (; 5 ;) (param $fptr i32) (param $0 i64) (result i64)
49+
(call_indirect (type $i64_=>_i64)
50+
(local.get $0)
51+
(local.get $fptr)
52+
)
53+
)
54+
)
55+
(;
56+
--BEGIN METADATA --
57+
{
58+
"staticBump": 90,
59+
"tableSize": 1,
60+
"declares": [
61+
],
62+
"externs": [
63+
],
64+
"implementedFunctions": [
65+
"_stackSave",
66+
"_stackAlloc",
67+
"_stackRestore",
68+
"___growWasmMemory",
69+
"_dynCall_jj"
70+
],
71+
"exports": [
72+
"stackSave",
73+
"stackAlloc",
74+
"stackRestore",
75+
"__growWasmMemory",
76+
"dynCall_jj"
77+
],
78+
"namedGlobals": {
79+
"__data_end" : "658"
80+
},
81+
"invokeFuncs": [
82+
],
83+
"features": [
84+
],
85+
"mainReadsParams": 0
86+
}
87+
-- END METADATA --
88+
;)

0 commit comments

Comments
 (0)