Skip to content

Commit

Permalink
operator: add shr operator
Browse files Browse the repository at this point in the history
Signed-off-by: zhenweijin <[email protected]>
  • Loading branch information
kylo5aby committed Dec 18, 2023
1 parent 6ace1f1 commit 03ccc9e
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 61 deletions.
178 changes: 163 additions & 15 deletions src/backend/binaryen/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,58 @@ export namespace FunctionalFuncs {
case ts.SyntaxKind.GreaterThanEqualsToken: {
return module.f64.ge(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.GreaterThanGreaterThanToken: {
return convertTypeToF64(
module,
module.i32.shr_s(
convertTypeToI32(
module,
convertTypeToI64(
module,
leftValueRef,
binaryen.f64,
),
binaryen.i64,
),
convertTypeToI32(
module,
convertTypeToI64(
module,
rightValueRef,
binaryen.f64,
),
binaryen.i64,
),
),
binaryen.i32,
);
}
case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken: {
return convertTypeToF64(
module,
module.i32.shr_u(
convertTypeToI32(
module,
convertTypeToI64(
module,
leftValueRef,
binaryen.f64,
),
binaryen.i64,
),
convertTypeToI32(
module,
convertTypeToI64(
module,
rightValueRef,
binaryen.f64,
),
binaryen.i64,
),
),
binaryen.i32,
);
}
case ts.SyntaxKind.LessThanToken: {
return module.f64.lt(leftValueRef, rightValueRef);
}
Expand All @@ -1125,11 +1177,27 @@ export namespace FunctionalFuncs {
case ts.SyntaxKind.LessThanLessThanToken: {
return convertTypeToF64(
module,
module.i64.shl(
convertTypeToI64(module, leftValueRef, binaryen.f64),
convertTypeToI64(module, rightValueRef, binaryen.f64),
module.i32.shl(
convertTypeToI32(
module,
convertTypeToI64(
module,
leftValueRef,
binaryen.f64,
),
binaryen.i64,
),
convertTypeToI32(
module,
convertTypeToI64(
module,
rightValueRef,
binaryen.f64,
),
binaryen.i64,
),
),
binaryen.i64,
binaryen.i32,
);
}
case ts.SyntaxKind.EqualsEqualsToken:
Expand Down Expand Up @@ -1159,21 +1227,53 @@ export namespace FunctionalFuncs {
case ts.SyntaxKind.AmpersandToken: {
return convertTypeToF64(
module,
module.i64.and(
convertTypeToI64(module, leftValueRef, binaryen.f64),
convertTypeToI64(module, rightValueRef, binaryen.f64),
module.i32.and(
convertTypeToI32(
module,
convertTypeToI64(
module,
leftValueRef,
binaryen.f64,
),
binaryen.i64,
),
convertTypeToI32(
module,
convertTypeToI64(
module,
rightValueRef,
binaryen.f64,
),
binaryen.i64,
),
),
binaryen.i64,
binaryen.i32,
);
}
case ts.SyntaxKind.BarToken: {
return convertTypeToF64(
module,
module.i64.or(
convertTypeToI64(module, leftValueRef, binaryen.f64),
convertTypeToI64(module, rightValueRef, binaryen.f64),
module.i32.or(
convertTypeToI32(
module,
convertTypeToI64(
module,
leftValueRef,
binaryen.f64,
),
binaryen.i64,
),
convertTypeToI32(
module,
convertTypeToI64(
module,
rightValueRef,
binaryen.f64,
),
binaryen.i64,
),
),
binaryen.i64,
binaryen.i32,
);
}
case ts.SyntaxKind.PercentToken: {
Expand All @@ -1188,9 +1288,25 @@ export namespace FunctionalFuncs {
case ts.SyntaxKind.CaretToken: {
return convertTypeToF64(
module,
module.i64.xor(
convertTypeToI64(module, leftValueRef, binaryen.f64),
convertTypeToI64(module, rightValueRef, binaryen.f64),
module.i32.xor(
convertTypeToI32(
module,
convertTypeToI64(
module,
leftValueRef,
binaryen.f64,
),
binaryen.i64,
),
convertTypeToI32(
module,
convertTypeToI64(
module,
rightValueRef,
binaryen.f64,
),
binaryen.i64,
),
),
);
}
Expand Down Expand Up @@ -1454,6 +1570,12 @@ export namespace FunctionalFuncs {
case ts.SyntaxKind.GreaterThanEqualsToken: {
return module.i32.ge_s(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.GreaterThanGreaterThanToken: {
return module.i32.shr_s(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken: {
return module.i32.shr_u(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.LessThanToken: {
return module.i32.lt_s(leftValueRef, rightValueRef);
}
Expand Down Expand Up @@ -1531,6 +1653,12 @@ export namespace FunctionalFuncs {
case ts.SyntaxKind.GreaterThanEqualsToken: {
return module.i64.ge_s(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.GreaterThanGreaterThanToken: {
return module.i64.shr_s(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken: {
return module.i64.shr_u(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.LessThanToken: {
return module.i64.lt_s(leftValueRef, rightValueRef);
}
Expand Down Expand Up @@ -1608,6 +1736,26 @@ export namespace FunctionalFuncs {
case ts.SyntaxKind.GreaterThanEqualsToken: {
return module.f32.ge(leftValueRef, rightValueRef);
}
case ts.SyntaxKind.GreaterThanGreaterThanToken: {
return convertTypeToF32(
module,
module.i32.shr_s(
convertTypeToI32(module, leftValueRef, binaryen.f32),
convertTypeToI32(module, rightValueRef, binaryen.f32),
),
binaryen.i32,
);
}
case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken: {
return convertTypeToF32(
module,
module.i32.shr_u(
convertTypeToI32(module, leftValueRef, binaryen.f32),
convertTypeToI32(module, rightValueRef, binaryen.f32),
),
binaryen.i32,
);
}
case ts.SyntaxKind.LessThanToken: {
return module.f32.lt(leftValueRef, rightValueRef);
}
Expand Down
7 changes: 5 additions & 2 deletions src/backend/binaryen/wasm_expr_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,11 @@ export class WASMExpressionGen {
return this.module.i32.const(value.value as number);
}
case ValueTypeKind.WASM_I64: {
// TODO: split value.value as two i32 values, put into low and high
return this.module.i64.const(value.value as number, 0);
const val = value.value as number;
return this.module.i64.const(
val & 0xffffffff,
(val / Math.pow(2, 32)) & 0xffffffff,
);
}
case ValueTypeKind.WASM_F32: {
return this.module.f32.const(value.value as number);
Expand Down
5 changes: 1 addition & 4 deletions src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
addSourceMapLoc,
isTypeGeneric,
processEscape,
decimalization,
} from './utils.js';
import {
TSArray,
Expand Down Expand Up @@ -501,9 +500,7 @@ export default class ExpressionProcessor {
}
case ts.SyntaxKind.NumericLiteral: {
res = new NumberLiteralExpression(
parseFloat(
decimalization((<ts.NumericLiteral>node).getText()),
),
parseFloat((<ts.NumericLiteral>node).text),
);
res.setExprType(this.typeResolver.generateNodeType(node));
break;
Expand Down
33 changes: 0 additions & 33 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,39 +462,6 @@ export function processEscape(str: string) {
return newStr;
}

export function decimalization(value: string) {
let systemNumeration = 0;
if (value.length < 2) {
return value;
}
if (value[0] == '0') {
switch (value[1]) {
case 'b':
case 'B': {
systemNumeration = 2;
break;
}
case 'o':
case 'O': {
systemNumeration = 8;
break;
}
case 'x':
case 'X': {
systemNumeration = 16;
break;
}
}
}
if (systemNumeration == 0) {
return value;
}
return decimalizationInternal(
value.substring(2, value.length),
systemNumeration,
);
}

function decimalizationInternal(value: string, systemNumeration: number) {
let decimal = 0;
let num = 0;
Expand Down
58 changes: 56 additions & 2 deletions tests/samples/expression_binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,60 @@ export function divEq() {
}

export function xor() {
const x = 4 ^ 0x1234;
return x;
let x = 4 ^ 0x1234;
console.log(x);
x = 2147483649 ^ 1;
console.log(x);
x = -4 ^ -0x1234;
console.log(x);
x = -2147483649 ^ -1;
console.log(x);
}

export function shl() {
const x = 9 << 2;
console.log(x);
const y = 2147483649 << 1;
console.log(y);
const z = -1 << 2;
console.log(z);
const m = -2147483649 << 1;
console.log(m);
}

export function shr() {
const x = 9 >> 2;
console.log(x);
let y = 2147483649 >> 1;
console.log(y);
const z = -1 >> 2;
console.log(z);
let m = -2147483649 >> 1;
console.log(m);
y = 2147483649 >>> 1;
console.log(y);
m = -2147483649 >>> 1;
console.log(m);
}

export function and() {
let x = 0x1234 & 0x2345;
console.log(x);
x = 2147483649 & 1;
console.log(x);
x = -0x1234 & -0x2345;
console.log(x);
x = -2147483649 & -1;
console.log(x);
}

export function or() {
let x = 0x1234 | 0x2345;
console.log(x);
x = 2147483649 | 2147483649;
console.log(x);
x = -0x1234 & -0x2345;
console.log(x);
x = -2147483649 & -2147483649;
console.log(x);
}
Loading

0 comments on commit 03ccc9e

Please sign in to comment.