Rework compileUnaryPostfixExpression

Previously this tried to reuse too much code, making it hard to implement operator overloading
This commit is contained in:
dcodeIO
2018-10-29 20:06:21 +01:00
parent 0bb5cb829e
commit 6f8a0fe03a
20 changed files with 2532 additions and 3313 deletions

View File

@ -6882,19 +6882,29 @@ export class Compiler extends DiagnosticEmitter {
ConversionKind.NONE,
WrapMode.NONE
);
// shortcut if compiling the getter already failed
if (getExpressionId(getValue) == ExpressionId.Unreachable) return getValue;
var currentType = this.currentType;
var op: BinaryOp;
var nativeType: NativeType;
var nativeOne: ExpressionRef;
// if the value isn't dropped, a temp. local is required to remember the original value
var tempLocal: Local | null = null;
if (contextualType != Type.void) {
tempLocal = currentFunction.getTempLocal(currentType, false);
getValue = module.createTeeLocal(
tempLocal.index,
getValue
);
}
var calcValue: ExpressionRef;
switch (expression.operator) {
case Token.PLUS_PLUS: {
// TODO: check operator overload
if (this.currentType.is(TypeFlags.REFERENCE)) {
if (currentType.is(TypeFlags.REFERENCE)) {
this.error(
DiagnosticCode.Operation_not_supported,
expression.range
@ -6910,38 +6920,48 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.U16:
case TypeKind.U32:
case TypeKind.BOOL: {
op = BinaryOp.AddI32;
nativeType = NativeType.I32;
nativeOne = module.createI32(1);
calcValue = module.createBinary(
BinaryOp.AddI32,
getValue,
module.createI32(1)
);
break;
}
case TypeKind.USIZE: // TODO: check operator overload
case TypeKind.USIZE:
case TypeKind.ISIZE: {
let options = this.options;
op = options.isWasm64
? BinaryOp.AddI64
: BinaryOp.AddI32;
nativeType = options.nativeSizeType;
nativeOne = currentType.toNativeOne(module);
calcValue = module.createBinary(
options.isWasm64
? BinaryOp.AddI64
: BinaryOp.AddI32,
getValue,
currentType.toNativeOne(module)
);
break;
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.AddI64;
nativeType = NativeType.I64;
nativeOne = module.createI64(1);
calcValue = module.createBinary(
BinaryOp.AddI64,
getValue,
module.createI64(1)
);
break;
}
case TypeKind.F32: {
op = BinaryOp.AddF32;
nativeType = NativeType.F32;
nativeOne = module.createF32(1);
calcValue = module.createBinary(
BinaryOp.AddF32,
getValue,
module.createF32(1)
);
break;
}
case TypeKind.F64: {
op = BinaryOp.AddF64;
nativeType = NativeType.F64;
nativeOne = module.createF64(1);
calcValue = module.createBinary(
BinaryOp.AddF64,
getValue,
module.createF64(1)
);
break;
}
default: {
@ -6970,38 +6990,48 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.U16:
case TypeKind.U32:
case TypeKind.BOOL: {
op = BinaryOp.SubI32;
nativeType = NativeType.I32;
nativeOne = module.createI32(1);
calcValue = module.createBinary(
BinaryOp.SubI32,
getValue,
module.createI32(1)
);
break;
}
case TypeKind.USIZE: // TODO: check operator overload
case TypeKind.USIZE:
case TypeKind.ISIZE: {
let options = this.options;
op = options.isWasm64
? BinaryOp.SubI64
: BinaryOp.SubI32;
nativeType = options.nativeSizeType;
nativeOne = currentType.toNativeOne(module);
calcValue = module.createBinary(
options.isWasm64
? BinaryOp.SubI64
: BinaryOp.SubI32,
getValue,
currentType.toNativeOne(module)
);
break;
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.SubI64;
nativeType = NativeType.I64;
nativeOne = module.createI64(1);
calcValue = module.createBinary(
BinaryOp.SubI64,
getValue,
module.createI64(1)
);
break;
}
case TypeKind.F32: {
op = BinaryOp.SubF32;
nativeType = NativeType.F32;
nativeOne = module.createF32(1);
calcValue = module.createBinary(
BinaryOp.SubF32,
getValue,
module.createF32(1)
);
break;
}
case TypeKind.F64: {
op = BinaryOp.SubF64;
nativeType = NativeType.F64;
nativeOne = module.createF64(1);
calcValue = module.createBinary(
BinaryOp.SubF64,
getValue,
module.createF64(1)
);
break;
}
default: {
@ -7018,33 +7048,25 @@ export class Compiler extends DiagnosticEmitter {
}
// simplify if dropped anyway
if (contextualType == Type.void) {
if (!tempLocal) {
this.currentType = Type.void;
return this.compileAssignmentWithValue(expression.operand,
module.createBinary(op,
getValue,
nativeOne
),
calcValue,
false
);
}
// otherwise use a temp local for the intermediate value (always possibly overflows)
var tempLocal = currentFunction.getTempLocal(currentType, false);
// otherwise use the temp. local for the intermediate value (always possibly overflows)
var setValue = this.compileAssignmentWithValue(expression.operand,
module.createBinary(op,
this.module.createGetLocal(tempLocal.index, nativeType),
nativeOne
),
calcValue, // also tees getValue to tempLocal
false
);
this.currentType = assert(tempLocal).type;
currentFunction.freeTempLocal(<Local>tempLocal);
var localIndex = (<Local>tempLocal).index;
this.currentType = tempLocal.type;
currentFunction.freeTempLocal(tempLocal);
var nativeType = tempLocal.type.toNativeType();
return module.createBlock(null, [
module.createSetLocal(localIndex, getValue),
setValue,
module.createGetLocal(localIndex, nativeType)
module.createGetLocal(tempLocal.index, nativeType)
], nativeType); // result of 'x++' / 'x--' might overflow
}

View File

@ -18,12 +18,11 @@
(block
(set_global $comma/b
(block (result i32)
(set_local $0
(get_global $comma/a)
)
(set_global $comma/a
(i32.add
(get_local $0)
(tee_local $0
(get_global $comma/a)
)
(i32.const 1)
)
)

View File

@ -78,12 +78,11 @@
(nop)
(br_if $continue|1
(block (result i32)
(set_local $0
(get_global $do/n)
)
(set_global $do/n
(i32.sub
(get_local $0)
(tee_local $0
(get_global $do/n)
)
(i32.const 1)
)
)

File diff suppressed because it is too large Load Diff

View File

@ -71,12 +71,11 @@
)
(i32.store8
(block (result i32)
(set_local $4
(get_local $0)
)
(set_local $0
(i32.add
(get_local $4)
(tee_local $4
(get_local $0)
)
(i32.const 1)
)
)
@ -84,12 +83,11 @@
)
(i32.load8_u
(block (result i32)
(set_local $4
(get_local $1)
)
(set_local $1
(i32.add
(get_local $4)
(tee_local $4
(get_local $1)
)
(i32.const 1)
)
)
@ -152,12 +150,11 @@
(block
(i32.store8
(block (result i32)
(set_local $4
(get_local $0)
)
(set_local $0
(i32.add
(get_local $4)
(tee_local $4
(get_local $0)
)
(i32.const 1)
)
)
@ -165,12 +162,11 @@
)
(i32.load8_u
(block (result i32)
(set_local $4
(get_local $1)
)
(set_local $1
(i32.add
(get_local $4)
(tee_local $4
(get_local $1)
)
(i32.const 1)
)
)

View File

@ -78,12 +78,11 @@
)
(set_local $1
(block (result i32)
(set_local $2
(get_local $0)
)
(set_local $0
(i32.add
(get_local $2)
(tee_local $2
(get_local $0)
)
(i32.const 1)
)
)
@ -115,12 +114,11 @@
)
(set_local $1
(block (result i32)
(set_local $2
(get_local $0)
)
(set_local $0
(i32.sub
(get_local $2)
(tee_local $2
(get_local $0)
)
(i32.const 1)
)
)
@ -361,12 +359,11 @@
)
(set_local $0
(block (result i32)
(set_local $2
(get_local $1)
)
(set_local $1
(i32.add
(get_local $2)
(tee_local $2
(get_local $1)
)
(i32.const 1)
)
)
@ -398,12 +395,11 @@
)
(set_local $0
(block (result i32)
(set_local $2
(get_local $1)
)
(set_local $1
(i32.sub
(get_local $2)
(tee_local $2
(get_local $1)
)
(i32.const 1)
)
)
@ -638,12 +634,11 @@
)
(set_local $1
(block (result i32)
(set_local $2
(get_local $0)
)
(set_local $0
(i32.sub
(get_local $2)
(tee_local $2
(get_local $0)
)
(i32.const 1)
)
)
@ -672,12 +667,11 @@
)
(set_local $1
(block (result i32)
(set_local $2
(get_local $0)
)
(set_local $0
(i32.add
(get_local $2)
(tee_local $2
(get_local $0)
)
(i32.const 1)
)
)
@ -894,12 +888,11 @@
)
(set_local $0
(block (result i32)
(set_local $2
(get_local $1)
)
(set_local $1
(i32.sub
(get_local $2)
(tee_local $2
(get_local $1)
)
(i32.const 1)
)
)
@ -928,12 +921,11 @@
)
(set_local $0
(block (result i32)
(set_local $2
(get_local $1)
)
(set_local $1
(i32.add
(get_local $2)
(tee_local $2
(get_local $1)
)
(i32.const 1)
)
)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1096,15 +1096,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -2294,15 +2293,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -3505,15 +3503,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -4703,15 +4700,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -5926,15 +5922,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -7061,15 +7056,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -8327,15 +8321,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -9477,15 +9470,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -10633,15 +10625,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -11793,15 +11784,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)

File diff suppressed because it is too large Load Diff

View File

@ -1083,15 +1083,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -2090,15 +2089,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -3131,15 +3129,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -4138,15 +4135,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -5197,15 +5193,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -6192,15 +6187,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -7318,15 +7312,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -8314,15 +8307,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -9314,15 +9306,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -10316,15 +10307,14 @@
)
(i32.mul
(block (result i32)
(set_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $5)
(tee_local $5
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -54,12 +54,11 @@
(local $2 i32)
(set_local $2
(block (result i32)
(set_local $1
(get_global $~lib/symbol/nextId)
)
(set_global $~lib/symbol/nextId
(i32.add
(get_local $1)
(tee_local $1
(get_global $~lib/symbol/nextId)
)
(i32.const 1)
)
)
@ -1389,15 +1388,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -1866,15 +1864,14 @@
)
(i32.mul
(block (result i32)
(set_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.store offset=16
(get_local $0)
(i32.add
(get_local $6)
(tee_local $6
(i32.load offset=16
(get_local $0)
)
)
(i32.const 1)
)
)
@ -1966,12 +1963,11 @@
)
(set_local $2
(block (result i32)
(set_local $1
(get_global $~lib/symbol/nextId)
)
(set_global $~lib/symbol/nextId
(i32.add
(get_local $1)
(tee_local $1
(get_global $~lib/symbol/nextId)
)
(i32.const 1)
)
)

View File

@ -2866,12 +2866,11 @@
(set_local $8
(i32.add
(block (result i32)
(set_local $8
(get_local $6)
)
(set_local $6
(i32.sub
(get_local $8)
(tee_local $8
(get_local $6)
)
(i32.const 1)
)
)

View File

@ -150,12 +150,11 @@
)
(set_global $unary/i
(block (result i32)
(set_local $0
(get_global $unary/i)
)
(set_global $unary/i
(i32.add
(get_local $0)
(tee_local $0
(get_global $unary/i)
)
(i32.const 1)
)
)
@ -164,12 +163,11 @@
)
(set_global $unary/i
(block (result i32)
(set_local $0
(get_global $unary/i)
)
(set_global $unary/i
(i32.sub
(get_local $0)
(tee_local $0
(get_global $unary/i)
)
(i32.const 1)
)
)
@ -285,12 +283,11 @@
)
(set_global $unary/I
(block (result i64)
(set_local $1
(get_global $unary/I)
)
(set_global $unary/I
(i64.add
(get_local $1)
(tee_local $1
(get_global $unary/I)
)
(i64.const 1)
)
)
@ -299,12 +296,11 @@
)
(set_global $unary/I
(block (result i64)
(set_local $1
(get_global $unary/I)
)
(set_global $unary/I
(i64.sub
(get_local $1)
(tee_local $1
(get_global $unary/I)
)
(i64.const 1)
)
)
@ -399,12 +395,11 @@
)
(set_global $unary/f
(block (result f32)
(set_local $2
(get_global $unary/f)
)
(set_global $unary/f
(f32.add
(get_local $2)
(tee_local $2
(get_global $unary/f)
)
(f32.const 1)
)
)
@ -413,12 +408,11 @@
)
(set_global $unary/f
(block (result f32)
(set_local $2
(get_global $unary/f)
)
(set_global $unary/f
(f32.sub
(get_local $2)
(tee_local $2
(get_global $unary/f)
)
(f32.const 1)
)
)
@ -517,12 +511,11 @@
)
(set_global $unary/F
(block (result f64)
(set_local $3
(get_global $unary/F)
)
(set_global $unary/F
(f64.add
(get_local $3)
(tee_local $3
(get_global $unary/F)
)
(f64.const 1)
)
)
@ -531,12 +524,11 @@
)
(set_global $unary/F
(block (result f64)
(set_local $3
(get_global $unary/F)
)
(set_global $unary/F
(f64.sub
(get_local $3)
(tee_local $3
(get_global $unary/F)
)
(f64.const 1)
)
)

View File

@ -224,12 +224,11 @@
(if (result i32)
(tee_local $0
(block (result i32)
(set_local $0
(get_global $while/n)
)
(set_global $while/n
(i32.sub
(get_local $0)
(tee_local $0
(get_global $while/n)
)
(i32.const 1)
)
)