Skip to content

Commit a971694

Browse files
committed
chore: refactor unary postfix code emitter to reduce nested tostack
1 parent 50bf2b7 commit a971694

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

src/compiler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import {
4848
getBlockChildCount,
4949
getBlockChildAt,
5050
getBlockName,
51-
getLocalSetValue,
5251
getGlobalGetName,
5352
isGlobalMutable,
5453
getSideEffects,
@@ -9231,11 +9230,12 @@ export class Compiler extends DiagnosticEmitter {
92319230
let flow = this.currentFlow;
92329231

92339232
// make a getter for the expression (also obtains the type)
9234-
let getValue = this.compileExpression( // reports
9233+
const getValueOrigin = this.compileExpression( // reports
92359234
expression.operand,
92369235
contextualType.exceptVoid,
92379236
Constraints.None
92389237
);
9238+
let getValue: ExpressionRef;
92399239

92409240
// if the value isn't dropped, a temp. local is required to remember the original value,
92419241
// except if a static overload is found, which reverses the use of a temp. (see below)
@@ -9244,24 +9244,25 @@ export class Compiler extends DiagnosticEmitter {
92449244
tempLocal = flow.getTempLocal(this.currentType);
92459245
getValue = module.local_tee(
92469246
tempLocal.index,
9247-
getValue,
9247+
getValueOrigin,
92489248
this.currentType.isManaged
92499249
);
9250+
} else {
9251+
getValue = getValueOrigin;
92509252
}
92519253

92529254
let expr: ExpressionRef;
92539255

92549256
switch (expression.operator) {
92559257
case Token.Plus_Plus: {
9256-
92579258
// check operator overload
92589259
let classReference = this.currentType.getClassOrWrapper(this.program);
92599260
if (classReference) {
92609261
let overload = classReference.lookupOverload(OperatorKind.PostfixInc);
92619262
if (overload) {
92629263
let isInstance = overload.is(CommonFlags.Instance);
92639264
if (tempLocal && !isInstance) { // revert: static overload simply returns
9264-
getValue = getLocalSetValue(getValue);
9265+
getValue = getValueOrigin;
92659266
tempLocal = null;
92669267
}
92679268
expr = this.compileUnaryOverload(overload, expression.operand, getValue, expression);
@@ -9337,19 +9338,18 @@ export class Compiler extends DiagnosticEmitter {
93379338
break;
93389339
}
93399340
case Token.Minus_Minus: {
9340-
93419341
// check operator overload
93429342
let classReference = this.currentType.getClassOrWrapper(this.program);
93439343
if (classReference) {
93449344
let overload = classReference.lookupOverload(OperatorKind.PostfixDec);
93459345
if (overload) {
93469346
let isInstance = overload.is(CommonFlags.Instance);
93479347
if (tempLocal && !isInstance) { // revert: static overload simply returns
9348-
getValue = getLocalSetValue(getValue);
9348+
getValue = getValueOrigin;
93499349
tempLocal = null;
93509350
}
93519351
expr = this.compileUnaryOverload(overload, expression.operand, getValue, expression);
9352-
if (overload.is(CommonFlags.Instance)) break;
9352+
if (isInstance) break;
93539353
return expr; // here
93549354
}
93559355
}

tests/compiler/std/operator-overloading.debug.wat

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6190,11 +6190,6 @@
61906190
block $std/operator-overloading/TesterInlineStatic.postInc|inlined.0 (result i32)
61916191
global.get $~lib/memory/__stack_pointer
61926192
global.get $std/operator-overloading/ais1
6193-
local.set $9
6194-
global.get $~lib/memory/__stack_pointer
6195-
local.get $9
6196-
i32.store
6197-
local.get $9
61986193
local.tee $3
61996194
i32.store offset=16
62006195
i32.const 0

tests/compiler/std/operator-overloading.release.wat

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,9 +4550,6 @@
45504550
global.get $~lib/memory/__stack_pointer
45514551
global.get $std/operator-overloading/ais1
45524552
local.tee $0
4553-
i32.store
4554-
global.get $~lib/memory/__stack_pointer
4555-
local.get $0
45564553
i32.store offset=16
45574554
global.get $~lib/memory/__stack_pointer
45584555
local.get $0

0 commit comments

Comments
 (0)