Skip to content

Commit

Permalink
fix: self-increment and self-decrement operators return wrong values …
Browse files Browse the repository at this point in the history
…in the return statement

Signed-off-by: ganjing <[email protected]>
  • Loading branch information
Shanks0224 committed Dec 27, 2023
1 parent e3976c1 commit 6b025f4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/backend/binaryen/wasm_expr_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ export class WASMExpressionGen {
if (!value.flattenExprValue) {
throw new UnimplementError(`wasmPostUnaryExpr: ${value.opKind}`);
}
const exprTypeRef = this.wasmTypeGen.getWASMValueType(value.type);
const unaryOp = this.wasmExprGen(
value.flattenExprValue as BinaryExprValue,
);
Expand Down Expand Up @@ -871,7 +872,7 @@ export class WASMExpressionGen {
break;
}
}
return this.module.block(null, [unaryOp, getOriValueOp]);
return this.module.block(null, [unaryOp, getOriValueOp], exprTypeRef);
}

private wasmPreUnaryExpr(
Expand All @@ -886,11 +887,18 @@ export class WASMExpressionGen {
`wasmPreUnaryExpr: ${value.opKind}`,
);
}
const exprTypeRef = this.wasmTypeGen.getWASMValueType(
value.type,
);
const unaryOp = this.wasmExprGen(
value.flattenExprValue as BinaryExprValue,
);
const getValueOp = this.wasmExprGen(value.target);
return this.module.block(null, [unaryOp, getValueOp]);
return this.module.block(
null,
[unaryOp, getValueOp],
exprTypeRef,
);
}
case ts.SyntaxKind.ExclamationToken: {
const operandValueRef = this.wasmExprGen(value.target);
Expand Down
69 changes: 69 additions & 0 deletions tests/samples/unary_operator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2023 Xiaomi Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

let globalIndex: number = 3
function increasePre(): number {
return ++globalIndex;
}
function increasePost(): number {
return globalIndex++;
}
function decreasePre(): number {
return --globalIndex;
}
function decreasePost(): number {
return globalIndex--;
}

function plusplusTest() {
let ret = increasePre();
console.log(ret);
console.log(globalIndex);

ret = increasePost();
console.log(ret);
console.log(globalIndex);

let localIndex = 0;
console.log(localIndex++);
console.log(++localIndex);
}

function minusminusTest() {
let ret = decreasePre();
console.log(ret);
console.log(globalIndex);

ret = decreasePost();
console.log(ret);
console.log(globalIndex);

let localIndex = 2;
console.log(localIndex--);
console.log(--localIndex);
}

function exclamationTest() {
const f = 0;
console.log(!f);

const t = 1;
console.log(!t);
}

function minusTest() {
const a = 1;
console.log(-a);

const b = -2;
console.log(-b);
}

export function test() {
plusplusTest();
minusminusTest();
exclamationTest();
minusTest();
}
10 changes: 10 additions & 0 deletions tools/validate/wamr/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,16 @@
}
]
},
{
"module": "unary_operator",
"entries": [
{
"name": "test",
"args": [],
"result": "4\n4\n4\n5\n0\n2\n4\n4\n4\n3\n2\n0\n1\n0\n-1\n2"
}
]
},
{
"module": "union_assign",
"entries": [
Expand Down

0 comments on commit 6b025f4

Please sign in to comment.