Skip to content

Commit 5558c72

Browse files
committed
fix
1 parent b6bda05 commit 5558c72

62 files changed

Lines changed: 8726 additions & 13794 deletions

Some content is hidden

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

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ under the licensing terms detailed in LICENSE:
6363
* Mopsgamer <79159094+Mopsgamer@users.noreply.github.com>
6464
* EDM115 <github@edm115.dev>
6565
* Weixie Cui <cuiweixie@gmail.com>
66+
* Rui Jin <jinrui_0322@163.com>
6667

6768
Portions of this software are derived from third-party works licensed under
6869
the following terms:

src/compiler.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ import {
5858
isConstExpressionNaN,
5959
ensureType,
6060
createType,
61-
getConstValueInteger
61+
getConstValueInteger,
62+
isConstZero
6263
} from "./module";
6364

6465
import {
@@ -10118,6 +10119,13 @@ export class Compiler extends DiagnosticEmitter {
1011810119

1011910120
// === Specialized code generation ==============================================================
1012010121

10122+
/** Check if possible to optimize the active initialization away if it's zero */
10123+
canOptimizeZeroInitialization(valueExpr: ExpressionRef): bool {
10124+
const runtime = this.options.runtime;
10125+
// Memory will be filled with 0 on itcms.__new
10126+
return runtime == Runtime.Incremental ? isConstZero(valueExpr) : false;
10127+
}
10128+
1012110129
/** Makes a constant zero of the specified type. */
1012210130
makeZero(type: Type): ExpressionRef {
1012310131
let module = this.module;
@@ -10465,6 +10473,7 @@ export class Compiler extends DiagnosticEmitter {
1046510473
let parameterIndex = fieldPrototype.parameterIndex;
1046610474

1046710475
// Defer non-parameter fields until parameter fields are initialized
10476+
// Since non-parameter may depend on parameter fields
1046810477
if (parameterIndex < 0) {
1046910478
if (!nonParameterFields) nonParameterFields = new Array();
1047010479
nonParameterFields.push(property);
@@ -10493,23 +10502,40 @@ export class Compiler extends DiagnosticEmitter {
1049310502

1049410503
// Initialize deferred non-parameter fields
1049510504
if (nonParameterFields) {
10505+
const unmanagedClass = classInstance.type.isUnmanaged;
1049610506
for (let i = 0, k = nonParameterFields.length; i < k; ++i) {
1049710507
let field = unchecked(nonParameterFields[i]);
1049810508
let fieldType = field.type;
1049910509
let fieldPrototype = field.prototype;
1050010510
let initializerNode = fieldPrototype.initializerNode;
1050110511
assert(fieldPrototype.parameterIndex < 0);
1050210512
let setterInstance = assert(field.setterInstance);
10503-
let expr = this.makeCallDirect(setterInstance, [
10504-
module.local_get(thisLocalIndex, sizeTypeRef),
10505-
initializerNode // use initializer if present, otherwise initialize with zero
10506-
? this.compileExpression(initializerNode, fieldType, Constraints.ConvImplicit)
10507-
: this.makeZero(fieldType)
10508-
], field.identifierNode, true);
10509-
if (this.currentType != Type.void) { // in case
10510-
expr = module.drop(expr);
10513+
10514+
if (initializerNode){
10515+
const valueExpr: ExpressionRef = this.compileExpression(initializerNode, fieldType, Constraints.ConvImplicit);
10516+
if(unmanagedClass || !this.canOptimizeZeroInitialization(valueExpr)) {
10517+
let expr = this.makeCallDirect(setterInstance, [
10518+
module.local_get(thisLocalIndex, sizeTypeRef),
10519+
valueExpr
10520+
], field.identifierNode, true);
10521+
if (this.currentType != Type.void) { // in case
10522+
expr = module.drop(expr);
10523+
}
10524+
stmts.push(expr);
10525+
}
10526+
} else {
10527+
if(unmanagedClass || (this.options.runtime != Runtime.Incremental)) {
10528+
let expr = this.makeCallDirect(setterInstance, [
10529+
module.local_get(thisLocalIndex, sizeTypeRef),
10530+
// Create only when necessary since makeZero will allocte persistent memory by Binaryen.
10531+
this.makeZero(fieldType)
10532+
], field.identifierNode, true);
10533+
if (this.currentType != Type.void) { // in case
10534+
expr = module.drop(expr);
10535+
}
10536+
stmts.push(expr);
10537+
}
1051110538
}
10512-
stmts.push(expr);
1051310539
}
1051410540
}
1051510541

tests/compiler/assignment-chain.debug.wat

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,35 +2253,30 @@
22532253
local.get $ptr
22542254
return
22552255
)
2256+
(func $assignment-chain/A#set:y (param $this i32) (param $y i64)
2257+
local.get $this
2258+
local.get $y
2259+
i64.store offset=8
2260+
)
22562261
(func $assignment-chain/A#set:x (param $this i32) (param $x i64)
22572262
local.get $this
22582263
local.get $x
22592264
i64.store
22602265
)
2261-
(func $assignment-chain/A#set:y (param $this i32) (param $y i64)
2266+
(func $assignment-chain/B#get:_setter_cnt (param $this i32) (result i32)
22622267
local.get $this
2263-
local.get $y
2264-
i64.store offset=8
2268+
i32.load
22652269
)
22662270
(func $assignment-chain/B#set:_setter_cnt (param $this i32) (param $_setter_cnt i32)
22672271
local.get $this
22682272
local.get $_setter_cnt
22692273
i32.store
22702274
)
2271-
(func $assignment-chain/B#set:_getter_cnt (param $this i32) (param $_getter_cnt i32)
2272-
local.get $this
2273-
local.get $_getter_cnt
2274-
i32.store offset=4
2275-
)
22762275
(func $assignment-chain/B#set:_y (param $this i32) (param $_y f64)
22772276
local.get $this
22782277
local.get $_y
22792278
f64.store offset=8
22802279
)
2281-
(func $assignment-chain/B#get:_setter_cnt (param $this i32) (result i32)
2282-
local.get $this
2283-
i32.load
2284-
)
22852280
(func $assignment-chain/B#get:_getter_cnt (param $this i32) (result i32)
22862281
local.get $this
22872282
i32.load offset=4
@@ -2437,22 +2432,6 @@
24372432
local.get $this
24382433
local.set $1
24392434
global.get $~lib/memory/__stack_pointer
2440-
local.get $1
2441-
i32.store offset=4
2442-
local.get $1
2443-
i64.const 0
2444-
call $assignment-chain/A#set:x
2445-
local.get $this
2446-
local.set $1
2447-
global.get $~lib/memory/__stack_pointer
2448-
local.get $1
2449-
i32.store offset=4
2450-
local.get $1
2451-
i64.const 0
2452-
call $assignment-chain/A#set:y
2453-
local.get $this
2454-
local.set $1
2455-
global.get $~lib/memory/__stack_pointer
24562435
i32.const 8
24572436
i32.add
24582437
global.set $~lib/memory/__stack_pointer
@@ -2555,30 +2534,6 @@
25552534
local.get $this
25562535
local.set $1
25572536
global.get $~lib/memory/__stack_pointer
2558-
local.get $1
2559-
i32.store offset=4
2560-
local.get $1
2561-
i32.const 0
2562-
call $assignment-chain/B#set:_setter_cnt
2563-
local.get $this
2564-
local.set $1
2565-
global.get $~lib/memory/__stack_pointer
2566-
local.get $1
2567-
i32.store offset=4
2568-
local.get $1
2569-
i32.const 0
2570-
call $assignment-chain/B#set:_getter_cnt
2571-
local.get $this
2572-
local.set $1
2573-
global.get $~lib/memory/__stack_pointer
2574-
local.get $1
2575-
i32.store offset=4
2576-
local.get $1
2577-
f64.const 0
2578-
call $assignment-chain/B#set:_y
2579-
local.get $this
2580-
local.set $1
2581-
global.get $~lib/memory/__stack_pointer
25822537
i32.const 8
25832538
i32.add
25842539
global.set $~lib/memory/__stack_pointer

tests/compiler/assignment-chain.release.wat

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
local.get $0
7878
global.set $~lib/rt/itcms/iter
7979
end
80-
block $__inlined_func$~lib/rt/itcms/Object#unlink$129
80+
block $__inlined_func$~lib/rt/itcms/Object#unlink$124
8181
local.get $1
8282
i32.load offset=4
8383
i32.const -4
@@ -101,7 +101,7 @@
101101
call $~lib/builtins/abort
102102
unreachable
103103
end
104-
br $__inlined_func$~lib/rt/itcms/Object#unlink$129
104+
br $__inlined_func$~lib/rt/itcms/Object#unlink$124
105105
end
106106
local.get $1
107107
i32.load offset=8
@@ -1396,24 +1396,6 @@
13961396
local.tee $1
13971397
i32.store
13981398
global.get $~lib/memory/__stack_pointer
1399-
local.get $1
1400-
i32.store offset=4
1401-
local.get $1
1402-
i32.const 0
1403-
i32.store
1404-
global.get $~lib/memory/__stack_pointer
1405-
local.get $1
1406-
i32.store offset=4
1407-
local.get $1
1408-
i32.const 0
1409-
i32.store offset=4
1410-
global.get $~lib/memory/__stack_pointer
1411-
local.get $1
1412-
i32.store offset=4
1413-
local.get $1
1414-
f64.const 0
1415-
f64.store offset=8
1416-
global.get $~lib/memory/__stack_pointer
14171399
i32.const 8
14181400
i32.add
14191401
global.set $~lib/memory/__stack_pointer
@@ -1515,18 +1497,6 @@
15151497
local.tee $1
15161498
i32.store
15171499
global.get $~lib/memory/__stack_pointer
1518-
local.get $1
1519-
i32.store offset=4
1520-
local.get $1
1521-
i64.const 0
1522-
i64.store
1523-
global.get $~lib/memory/__stack_pointer
1524-
local.get $1
1525-
i32.store offset=4
1526-
local.get $1
1527-
i64.const 0
1528-
i64.store offset=8
1529-
global.get $~lib/memory/__stack_pointer
15301500
i32.const 8
15311501
i32.add
15321502
global.set $~lib/memory/__stack_pointer

0 commit comments

Comments
 (0)