Skip to content

fix(compiler): wrap RHS shifting value by maximum size of type in bits for small integers #1511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5128,7 +5128,7 @@ export class Compiler extends DiagnosticEmitter {
module.binary(BinaryOp.EqI8x16, leftExpr, rightExpr)
);
}
case TypeKind.FUNCREF:
case TypeKind.FUNCREF:
case TypeKind.EXTERNREF:
case TypeKind.EXNREF:
case TypeKind.ANYREF: {
Expand Down Expand Up @@ -5641,14 +5641,18 @@ export class Compiler extends DiagnosticEmitter {
// Cares about garbage bits on the RHS, but only for types smaller than 5 bits
var module = this.module;
switch (type.kind) {
case TypeKind.BOOL: {
rightExpr = this.ensureSmallIntegerWrap(rightExpr, type);
// falls through
}
case TypeKind.BOOL: return leftExpr;
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.U16: {
// leftExpr << (rightExpr & (7|15))
return module.binary(
BinaryOp.ShlI32,
leftExpr,
module.binary(BinaryOp.AndI32, rightExpr, module.i32(type.size - 1))
);
}
case TypeKind.I32:
case TypeKind.U32: {
return module.binary(BinaryOp.ShlI32, leftExpr, rightExpr);
Expand Down Expand Up @@ -5677,10 +5681,24 @@ export class Compiler extends DiagnosticEmitter {
// and signedness
var module = this.module;
switch (type.kind) {
case TypeKind.BOOL: return leftExpr;
case TypeKind.I8:
case TypeKind.I16: {
leftExpr = this.ensureSmallIntegerWrap(leftExpr, type);
// falls through
// leftExpr >> (rightExpr & (7|15))
return module.binary(
BinaryOp.ShrI32,
this.ensureSmallIntegerWrap(leftExpr, type),
module.binary(BinaryOp.AndI32, rightExpr, module.i32(type.size - 1))
);
}
case TypeKind.U8:
case TypeKind.U16: {
// leftExpr >>> (rightExpr & (7|15))
return module.binary(
BinaryOp.ShrU32,
this.ensureSmallIntegerWrap(leftExpr, type),
module.binary(BinaryOp.AndI32, rightExpr, module.i32(type.size - 1))
);
}
case TypeKind.I32: {
return module.binary(BinaryOp.ShrI32, leftExpr, rightExpr);
Expand All @@ -5697,15 +5715,6 @@ export class Compiler extends DiagnosticEmitter {
rightExpr
);
}
case TypeKind.BOOL: {
rightExpr = this.ensureSmallIntegerWrap(rightExpr, type);
// falls through
}
case TypeKind.U8:
case TypeKind.U16: {
leftExpr = this.ensureSmallIntegerWrap(leftExpr, type);
// falls through
}
case TypeKind.U32: {
return module.binary(BinaryOp.ShrU32, leftExpr, rightExpr);
}
Expand All @@ -5730,16 +5739,17 @@ export class Compiler extends DiagnosticEmitter {
// Cares about garbage bits on the LHS, but on the RHS only for types smaller than 5 bits
var module = this.module;
switch (type.kind) {
case TypeKind.BOOL: {
rightExpr = this.ensureSmallIntegerWrap(rightExpr, type);
// falls through
}
case TypeKind.BOOL: return leftExpr;
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.U8:
case TypeKind.U16: {
leftExpr = this.ensureSmallIntegerWrap(leftExpr, type);
// falls through
// leftExpr >>> (rightExpr & (7|15))
return module.binary(
BinaryOp.ShrU32,
this.ensureSmallIntegerWrap(leftExpr, type),
module.binary(BinaryOp.AndI32, rightExpr, module.i32(type.size - 1))
);
}
case TypeKind.I32:
case TypeKind.U32: {
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/abi.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
i32.const 24
i32.shr_s
i32.const 24
i32.const 7
i32.and
i32.shr_s
local.set $0
else
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/retain-i32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function test(a: u32, b: u32): void {
assert(<i8>(a & b) == <i8>(<i8>a & <i8>b));
assert(<i8>(a | b) == <i8>(<i8>a | <i8>b));
assert(<i8>(a ^ b) == <i8>(<i8>a ^ <i8>b));
assert(<i8>(a << b) == <i8>(<i8>a << <i8>b));
assert(<i8>(a << (b & 7)) == <i8>(<i8>a << <i8>b));

// unsigned
assert(<u8>(a + b) == <u8>(<u8>a + <u8>b));
Expand All @@ -16,7 +16,7 @@ function test(a: u32, b: u32): void {
assert(<u8>(a & b) == <u8>(<u8>a & <u8>b));
assert(<u8>(a | b) == <u8>(<u8>a | <u8>b));
assert(<u8>(a ^ b) == <u8>(<u8>a ^ <u8>b));
assert(<u8>(a << b) == <u8>(<u8>a << <u8>b));
assert(<u8>(a << (b & 7)) == <u8>(<u8>a << <u8>b));
}

// signed
Expand Down
8 changes: 8 additions & 0 deletions tests/compiler/retain-i32.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,17 @@
end
local.get $0
local.get $1
i32.const 7
i32.and
i32.shl
i32.const 24
i32.shl
i32.const 24
i32.shr_s
local.get $0
local.get $1
i32.const 7
i32.and
i32.shl
i32.const 24
i32.shl
Expand Down Expand Up @@ -311,11 +315,15 @@
end
local.get $0
local.get $1
i32.const 7
i32.and
i32.shl
i32.const 255
i32.and
local.get $0
local.get $1
i32.const 7
i32.and
i32.shl
i32.const 255
i32.and
Expand Down
8 changes: 8 additions & 0 deletions tests/compiler/std/dataview.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2124,13 +2124,17 @@
drop
local.get $0
i32.const 8
i32.const 15
i32.and
i32.shl
local.get $0
i32.const 16
i32.shl
i32.const 16
i32.shr_s
i32.const 8
i32.const 15
i32.and
i32.shr_s
i32.const 255
i32.and
Expand Down Expand Up @@ -2344,11 +2348,15 @@
drop
local.get $0
i32.const 8
i32.const 15
i32.and
i32.shl
local.get $0
i32.const 65535
i32.and
i32.const 8
i32.const 15
i32.and
i32.shr_u
i32.const 255
i32.and
Expand Down
16 changes: 16 additions & 0 deletions tests/compiler/std/polyfills.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@
drop
local.get $0
i32.const 8
i32.const 15
i32.and
i32.shl
local.get $0
i32.const 65535
i32.and
i32.const 8
i32.const 15
i32.and
i32.shr_u
i32.const 255
i32.and
Expand All @@ -74,13 +78,17 @@
drop
local.get $0
i32.const 8
i32.const 15
i32.and
i32.shl
local.get $0
i32.const 16
i32.shl
i32.const 16
i32.shr_s
i32.const 8
i32.const 15
i32.and
i32.shr_s
i32.const 255
i32.and
Expand Down Expand Up @@ -347,11 +355,15 @@
drop
local.get $0
i32.const 8
i32.const 15
i32.and
i32.shl
local.get $0
i32.const 65535
i32.and
i32.const 8
i32.const 15
i32.and
i32.shr_u
i32.const 255
i32.and
Expand All @@ -374,13 +386,17 @@
drop
local.get $0
i32.const 8
i32.const 15
i32.and
i32.shl
local.get $0
i32.const 16
i32.shl
i32.const 16
i32.shr_s
i32.const 8
i32.const 15
i32.and
i32.shr_s
i32.const 255
i32.and
Expand Down