Skip to content

Make sure main start function is compiled last #1607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2021
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
60 changes: 31 additions & 29 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,35 +452,6 @@ export class Compiler extends DiagnosticEmitter {
}
}

// compile the start function if not empty or if explicitly requested
var startIsEmpty = !startFunctionBody.length;
var explicitStart = program.isWasi || options.explicitStart;
if (!startIsEmpty || explicitStart) {
let signature = startFunctionInstance.signature;
if (!startIsEmpty && explicitStart) {
module.addGlobal(BuiltinNames.started, NativeType.I32, true, module.i32(0));
startFunctionBody.unshift(
module.global_set(BuiltinNames.started, module.i32(1))
);
startFunctionBody.unshift(
module.if(
module.global_get(BuiltinNames.started, NativeType.I32),
module.return()
)
);
}
let funcRef = module.addFunction(
startFunctionInstance.internalName,
signature.nativeParams,
signature.nativeResults,
typesToNativeTypes(startFunctionInstance.additionalLocals),
module.flatten(startFunctionBody)
);
startFunctionInstance.finalize(module, funcRef);
if (!explicitStart) module.setStart(funcRef);
else module.addFunctionExport(startFunctionInstance.internalName, ExportNames.start);
}

// check if the entire program is acyclic
var cyclicClasses = program.findCyclicClasses();
if (cyclicClasses.size) {
Expand Down Expand Up @@ -520,6 +491,37 @@ export class Compiler extends DiagnosticEmitter {
compileClassInstanceOf(this, prototype);
}

// NOTE: no more element compiles from here. may go to the start function!

// compile the start function if not empty or if explicitly requested
var startIsEmpty = !startFunctionBody.length;
var explicitStart = program.isWasi || options.explicitStart;
if (!startIsEmpty || explicitStart) {
let signature = startFunctionInstance.signature;
if (!startIsEmpty && explicitStart) {
module.addGlobal(BuiltinNames.started, NativeType.I32, true, module.i32(0));
startFunctionBody.unshift(
module.global_set(BuiltinNames.started, module.i32(1))
);
startFunctionBody.unshift(
module.if(
module.global_get(BuiltinNames.started, NativeType.I32),
module.return()
)
);
}
let funcRef = module.addFunction(
startFunctionInstance.internalName,
signature.nativeParams,
signature.nativeResults,
typesToNativeTypes(startFunctionInstance.additionalLocals),
module.flatten(startFunctionBody)
);
startFunctionInstance.finalize(module, funcRef);
if (!explicitStart) module.setStart(funcRef);
else module.addFunctionExport(startFunctionInstance.internalName, ExportNames.start);
}

// set up virtual lookup tables
var functionTable = this.functionTable;
for (let i = 0, k = functionTable.length; i < k; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/do.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1347,9 +1347,6 @@
unreachable
end
)
(func $~start
call $start:do
)
(func $~lib/rt/pure/decrement (param $0 i32)
(local $1 i32)
(local $2 i32)
Expand Down Expand Up @@ -1444,4 +1441,7 @@
i32.store offset=4
end
)
(func $~start
call $start:do
)
)
6 changes: 3 additions & 3 deletions tests/compiler/do.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2231,9 +2231,6 @@
unreachable
end
)
(func $~start
call $start:do
)
(func $~lib/rt/tlsf/freeBlock (param $0 i32) (param $1 i32)
local.get $1
local.get $1
Expand Down Expand Up @@ -2340,6 +2337,9 @@
i32.store offset=4
end
)
(func $~start
call $start:do
)
(func $~lib/rt/pure/__visit (param $0 i32) (param $1 i32)
local.get $0
global.get $~lib/memory/__heap_base
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/exports-lazy.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
(global $exports-lazy/lazyGlobalUsed i32 (i32.const 1088))
(export "memory" (memory $0))
(export "lazyGlobalUsed" (global $exports-lazy/lazyGlobalUsed))
(export "lazyFuncUsed" (func $~start))
(func $~start
(export "lazyFuncUsed" (func $exports-lazy/lazyFuncUsed))
(func $exports-lazy/lazyFuncUsed
nop
)
)
6 changes: 3 additions & 3 deletions tests/compiler/exports-lazy.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
drop
call $exports-lazy/lazyFuncUsed
)
(func $~start
call $start:exports-lazy
)
(func $exports-lazy/lazyFuncUsed
nop
)
(func $~start
call $start:exports-lazy
)
)
6 changes: 3 additions & 3 deletions tests/compiler/extends-baseaggregate.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1718,9 +1718,6 @@
local.get $0
call $~lib/rt/pure/__release
)
(func $~start
call $start:extends-baseaggregate
)
(func $~lib/rt/pure/decrement (param $0 i32)
(local $1 i32)
(local $2 i32)
Expand Down Expand Up @@ -1886,6 +1883,9 @@
end
end
)
(func $~start
call $start:extends-baseaggregate
)
(func $~lib/rt/pure/scanBlack (param $0 i32)
local.get $0
local.get $0
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/extends-baseaggregate.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3354,9 +3354,6 @@
local.get $0
call $~lib/rt/pure/__release
)
(func $~start
call $start:extends-baseaggregate
)
(func $~lib/rt/pure/finalize (param $0 i32)
i32.const 0
drop
Expand Down Expand Up @@ -3586,6 +3583,9 @@
end
end
)
(func $~start
call $start:extends-baseaggregate
)
(func $~lib/rt/pure/markGray (param $0 i32)
(local $1 i32)
local.get $0
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/for.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1359,9 +1359,6 @@
unreachable
end
)
(func $~start
call $start:for
)
(func $~lib/rt/pure/decrement (param $0 i32)
(local $1 i32)
(local $2 i32)
Expand Down Expand Up @@ -1456,4 +1453,7 @@
i32.store offset=4
end
)
(func $~start
call $start:for
)
)
6 changes: 3 additions & 3 deletions tests/compiler/for.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2276,9 +2276,6 @@
unreachable
end
)
(func $~start
call $start:for
)
(func $~lib/rt/tlsf/freeBlock (param $0 i32) (param $1 i32)
local.get $1
local.get $1
Expand Down Expand Up @@ -2385,6 +2382,9 @@
i32.store offset=4
end
)
(func $~start
call $start:for
)
(func $~lib/rt/pure/__visit (param $0 i32) (param $1 i32)
local.get $0
global.get $~lib/memory/__heap_base
Expand Down
60 changes: 30 additions & 30 deletions tests/compiler/instanceof-class.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@
i32.const 16
i32.add
)
(func $~lib/rt/__instanceof (param $0 i32) (param $1 i32) (result i32)
local.get $0
i32.const 20
i32.sub
i32.load offset=12
local.tee $0
i32.const 1104
i32.load
i32.le_u
if
loop $do-continue|0
local.get $0
local.get $1
i32.eq
if
i32.const 1
return
end
local.get $0
i32.const 3
i32.shl
i32.const 1108
i32.add
i32.load offset=4
local.tee $0
br_if $do-continue|0
end
end
i32.const 0
)
(func $~start
(local $0 i32)
i32.const 1164
Expand Down Expand Up @@ -140,34 +170,4 @@
unreachable
end
)
(func $~lib/rt/__instanceof (param $0 i32) (param $1 i32) (result i32)
local.get $0
i32.const 20
i32.sub
i32.load offset=12
local.tee $0
i32.const 1104
i32.load
i32.le_u
if
loop $do-continue|0
local.get $0
local.get $1
i32.eq
if
i32.const 1
return
end
local.get $0
i32.const 3
i32.shl
i32.const 1108
i32.add
i32.load offset=4
local.tee $0
br_if $do-continue|0
end
end
i32.const 0
)
)
6 changes: 3 additions & 3 deletions tests/compiler/instanceof-class.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@
i32.eqz
drop
)
(func $~start
call $start:instanceof-class
)
(func $~lib/rt/__instanceof (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
Expand Down Expand Up @@ -321,4 +318,7 @@
i32.const 0
return
)
(func $~start
call $start:instanceof-class
)
)
Loading