Skip to content

fix: polyfills for small types in builtin_rotl/rotr should use temp variables #1505

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 1 commit into from
Oct 15, 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: 46 additions & 10 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1177,22 +1177,40 @@ function builtin_rotl(ctx: BuiltinContext): ExpressionRef {
case TypeKind.U8:
case TypeKind.U16: {
// (value << (shift & mask)) | (value >>> ((0 - shift) & mask))
return module.binary(BinaryOp.OrI32,
let flow = compiler.currentFlow;
let temp1 = flow.getTempLocal(type);
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
let temp2 = flow.getTempLocal(type);
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);

let ret = module.binary(BinaryOp.OrI32,
module.binary(
BinaryOp.ShlI32,
arg0,
module.binary(BinaryOp.AndI32, arg1, module.i32(type.size - 1))
module.local_tee(temp1.index, arg0),
module.binary(
BinaryOp.AndI32,
module.local_tee(temp2.index, arg1),
module.i32(type.size - 1)
)
),
module.binary(
BinaryOp.ShrU32,
arg0,
module.local_get(temp1.index, NativeType.I32),
module.binary(
BinaryOp.AndI32,
module.binary(BinaryOp.SubI32, module.i32(0), arg1),
module.binary(
BinaryOp.SubI32,
module.i32(0),
module.local_get(temp2.index, NativeType.I32)
),
module.i32(type.size - 1)
)
)
);
flow.freeTempLocal(temp2);
flow.freeTempLocal(temp1);

return ret;
}
case TypeKind.I32:
case TypeKind.U32: return module.binary(BinaryOp.RotlI32, arg0, arg1);
Expand Down Expand Up @@ -1240,22 +1258,40 @@ function builtin_rotr(ctx: BuiltinContext): ExpressionRef {
case TypeKind.U8:
case TypeKind.U16: {
// (value >>> (shift & mask)) | (value << ((0 - shift) & mask))
return module.binary(BinaryOp.OrI32,
let flow = compiler.currentFlow;
let temp1 = flow.getTempLocal(type);
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
let temp2 = flow.getTempLocal(type);
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);

let ret = module.binary(BinaryOp.OrI32,
module.binary(
BinaryOp.ShrU32,
arg0,
module.binary(BinaryOp.AndI32, arg1, module.i32(type.size - 1))
module.local_tee(temp1.index, arg0),
module.binary(
BinaryOp.AndI32,
module.local_tee(temp2.index, arg1),
module.i32(type.size - 1)
)
),
module.binary(
BinaryOp.ShlI32,
arg0,
module.local_get(temp1.index, NativeType.I32),
module.binary(
BinaryOp.AndI32,
module.binary(BinaryOp.SubI32, module.i32(0), arg1),
module.binary(
BinaryOp.SubI32,
module.i32(0),
module.local_get(temp2.index, NativeType.I32)
),
module.i32(type.size - 1)
)
)
);
flow.freeTempLocal(temp2);
flow.freeTempLocal(temp1);

return ret;
}
case TypeKind.I32:
case TypeKind.U32: return module.binary(BinaryOp.RotrI32, arg0, arg1);
Expand Down
12 changes: 8 additions & 4 deletions tests/compiler/builtins.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,15 @@
unreachable
end
i32.const 143
local.tee $0
i32.const 3
local.tee $1
i32.const 7
i32.and
i32.shl
i32.const 143
local.get $0
i32.const 0
i32.const 3
local.get $1
i32.sub
i32.const 7
i32.and
Expand All @@ -530,13 +532,15 @@
unreachable
end
i32.const 170
local.tee $0
i32.const 1
local.tee $1
i32.const 7
i32.and
i32.shr_u
i32.const 170
local.get $0
i32.const 0
i32.const 1
local.get $1
i32.sub
i32.const 7
i32.and
Expand Down