Skip to content

Commit caa5801

Browse files
authored
fix: Make sure the main start function is assembled last (#1607)
1 parent 485cfee commit caa5801

File tree

65 files changed

+690
-688
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+690
-688
lines changed

src/compiler.ts

+31-29
Original file line numberDiff line numberDiff line change
@@ -452,35 +452,6 @@ export class Compiler extends DiagnosticEmitter {
452452
}
453453
}
454454

455-
// compile the start function if not empty or if explicitly requested
456-
var startIsEmpty = !startFunctionBody.length;
457-
var explicitStart = program.isWasi || options.explicitStart;
458-
if (!startIsEmpty || explicitStart) {
459-
let signature = startFunctionInstance.signature;
460-
if (!startIsEmpty && explicitStart) {
461-
module.addGlobal(BuiltinNames.started, NativeType.I32, true, module.i32(0));
462-
startFunctionBody.unshift(
463-
module.global_set(BuiltinNames.started, module.i32(1))
464-
);
465-
startFunctionBody.unshift(
466-
module.if(
467-
module.global_get(BuiltinNames.started, NativeType.I32),
468-
module.return()
469-
)
470-
);
471-
}
472-
let funcRef = module.addFunction(
473-
startFunctionInstance.internalName,
474-
signature.nativeParams,
475-
signature.nativeResults,
476-
typesToNativeTypes(startFunctionInstance.additionalLocals),
477-
module.flatten(startFunctionBody)
478-
);
479-
startFunctionInstance.finalize(module, funcRef);
480-
if (!explicitStart) module.setStart(funcRef);
481-
else module.addFunctionExport(startFunctionInstance.internalName, ExportNames.start);
482-
}
483-
484455
// check if the entire program is acyclic
485456
var cyclicClasses = program.findCyclicClasses();
486457
if (cyclicClasses.size) {
@@ -520,6 +491,37 @@ export class Compiler extends DiagnosticEmitter {
520491
compileClassInstanceOf(this, prototype);
521492
}
522493

494+
// NOTE: no more element compiles from here. may go to the start function!
495+
496+
// compile the start function if not empty or if explicitly requested
497+
var startIsEmpty = !startFunctionBody.length;
498+
var explicitStart = program.isWasi || options.explicitStart;
499+
if (!startIsEmpty || explicitStart) {
500+
let signature = startFunctionInstance.signature;
501+
if (!startIsEmpty && explicitStart) {
502+
module.addGlobal(BuiltinNames.started, NativeType.I32, true, module.i32(0));
503+
startFunctionBody.unshift(
504+
module.global_set(BuiltinNames.started, module.i32(1))
505+
);
506+
startFunctionBody.unshift(
507+
module.if(
508+
module.global_get(BuiltinNames.started, NativeType.I32),
509+
module.return()
510+
)
511+
);
512+
}
513+
let funcRef = module.addFunction(
514+
startFunctionInstance.internalName,
515+
signature.nativeParams,
516+
signature.nativeResults,
517+
typesToNativeTypes(startFunctionInstance.additionalLocals),
518+
module.flatten(startFunctionBody)
519+
);
520+
startFunctionInstance.finalize(module, funcRef);
521+
if (!explicitStart) module.setStart(funcRef);
522+
else module.addFunctionExport(startFunctionInstance.internalName, ExportNames.start);
523+
}
524+
523525
// set up virtual lookup tables
524526
var functionTable = this.functionTable;
525527
for (let i = 0, k = functionTable.length; i < k; ++i) {

tests/compiler/do.optimized.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -1347,9 +1347,6 @@
13471347
unreachable
13481348
end
13491349
)
1350-
(func $~start
1351-
call $start:do
1352-
)
13531350
(func $~lib/rt/pure/decrement (param $0 i32)
13541351
(local $1 i32)
13551352
(local $2 i32)
@@ -1444,4 +1441,7 @@
14441441
i32.store offset=4
14451442
end
14461443
)
1444+
(func $~start
1445+
call $start:do
1446+
)
14471447
)

tests/compiler/do.untouched.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -2231,9 +2231,6 @@
22312231
unreachable
22322232
end
22332233
)
2234-
(func $~start
2235-
call $start:do
2236-
)
22372234
(func $~lib/rt/tlsf/freeBlock (param $0 i32) (param $1 i32)
22382235
local.get $1
22392236
local.get $1
@@ -2340,6 +2337,9 @@
23402337
i32.store offset=4
23412338
end
23422339
)
2340+
(func $~start
2341+
call $start:do
2342+
)
23432343
(func $~lib/rt/pure/__visit (param $0 i32) (param $1 i32)
23442344
local.get $0
23452345
global.get $~lib/memory/__heap_base

tests/compiler/exports-lazy.optimized.wat

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
(global $exports-lazy/lazyGlobalUsed i32 (i32.const 1088))
88
(export "memory" (memory $0))
99
(export "lazyGlobalUsed" (global $exports-lazy/lazyGlobalUsed))
10-
(export "lazyFuncUsed" (func $~start))
11-
(func $~start
10+
(export "lazyFuncUsed" (func $exports-lazy/lazyFuncUsed))
11+
(func $exports-lazy/lazyFuncUsed
1212
nop
1313
)
1414
)

tests/compiler/exports-lazy.untouched.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
drop
1717
call $exports-lazy/lazyFuncUsed
1818
)
19-
(func $~start
20-
call $start:exports-lazy
21-
)
2219
(func $exports-lazy/lazyFuncUsed
2320
nop
2421
)
22+
(func $~start
23+
call $start:exports-lazy
24+
)
2525
)

tests/compiler/extends-baseaggregate.optimized.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -1718,9 +1718,6 @@
17181718
local.get $0
17191719
call $~lib/rt/pure/__release
17201720
)
1721-
(func $~start
1722-
call $start:extends-baseaggregate
1723-
)
17241721
(func $~lib/rt/pure/decrement (param $0 i32)
17251722
(local $1 i32)
17261723
(local $2 i32)
@@ -1886,6 +1883,9 @@
18861883
end
18871884
end
18881885
)
1886+
(func $~start
1887+
call $start:extends-baseaggregate
1888+
)
18891889
(func $~lib/rt/pure/scanBlack (param $0 i32)
18901890
local.get $0
18911891
local.get $0

tests/compiler/extends-baseaggregate.untouched.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -3354,9 +3354,6 @@
33543354
local.get $0
33553355
call $~lib/rt/pure/__release
33563356
)
3357-
(func $~start
3358-
call $start:extends-baseaggregate
3359-
)
33603357
(func $~lib/rt/pure/finalize (param $0 i32)
33613358
i32.const 0
33623359
drop
@@ -3586,6 +3583,9 @@
35863583
end
35873584
end
35883585
)
3586+
(func $~start
3587+
call $start:extends-baseaggregate
3588+
)
35893589
(func $~lib/rt/pure/markGray (param $0 i32)
35903590
(local $1 i32)
35913591
local.get $0

tests/compiler/for.optimized.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -1359,9 +1359,6 @@
13591359
unreachable
13601360
end
13611361
)
1362-
(func $~start
1363-
call $start:for
1364-
)
13651362
(func $~lib/rt/pure/decrement (param $0 i32)
13661363
(local $1 i32)
13671364
(local $2 i32)
@@ -1456,4 +1453,7 @@
14561453
i32.store offset=4
14571454
end
14581455
)
1456+
(func $~start
1457+
call $start:for
1458+
)
14591459
)

tests/compiler/for.untouched.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -2276,9 +2276,6 @@
22762276
unreachable
22772277
end
22782278
)
2279-
(func $~start
2280-
call $start:for
2281-
)
22822279
(func $~lib/rt/tlsf/freeBlock (param $0 i32) (param $1 i32)
22832280
local.get $1
22842281
local.get $1
@@ -2385,6 +2382,9 @@
23852382
i32.store offset=4
23862383
end
23872384
)
2385+
(func $~start
2386+
call $start:for
2387+
)
23882388
(func $~lib/rt/pure/__visit (param $0 i32) (param $1 i32)
23892389
local.get $0
23902390
global.get $~lib/memory/__heap_base

tests/compiler/instanceof-class.optimized.wat

+30-30
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@
8787
i32.const 16
8888
i32.add
8989
)
90+
(func $~lib/rt/__instanceof (param $0 i32) (param $1 i32) (result i32)
91+
local.get $0
92+
i32.const 20
93+
i32.sub
94+
i32.load offset=12
95+
local.tee $0
96+
i32.const 1104
97+
i32.load
98+
i32.le_u
99+
if
100+
loop $do-continue|0
101+
local.get $0
102+
local.get $1
103+
i32.eq
104+
if
105+
i32.const 1
106+
return
107+
end
108+
local.get $0
109+
i32.const 3
110+
i32.shl
111+
i32.const 1108
112+
i32.add
113+
i32.load offset=4
114+
local.tee $0
115+
br_if $do-continue|0
116+
end
117+
end
118+
i32.const 0
119+
)
90120
(func $~start
91121
(local $0 i32)
92122
i32.const 1164
@@ -140,34 +170,4 @@
140170
unreachable
141171
end
142172
)
143-
(func $~lib/rt/__instanceof (param $0 i32) (param $1 i32) (result i32)
144-
local.get $0
145-
i32.const 20
146-
i32.sub
147-
i32.load offset=12
148-
local.tee $0
149-
i32.const 1104
150-
i32.load
151-
i32.le_u
152-
if
153-
loop $do-continue|0
154-
local.get $0
155-
local.get $1
156-
i32.eq
157-
if
158-
i32.const 1
159-
return
160-
end
161-
local.get $0
162-
i32.const 3
163-
i32.shl
164-
i32.const 1108
165-
i32.add
166-
i32.load offset=4
167-
local.tee $0
168-
br_if $do-continue|0
169-
end
170-
end
171-
i32.const 0
172-
)
173173
)

tests/compiler/instanceof-class.untouched.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,6 @@
254254
i32.eqz
255255
drop
256256
)
257-
(func $~start
258-
call $start:instanceof-class
259-
)
260257
(func $~lib/rt/__instanceof (param $0 i32) (param $1 i32) (result i32)
261258
(local $2 i32)
262259
(local $3 i32)
@@ -321,4 +318,7 @@
321318
i32.const 0
322319
return
323320
)
321+
(func $~start
322+
call $start:instanceof-class
323+
)
324324
)

0 commit comments

Comments
 (0)