Skip to content

Commit d0a2c9e

Browse files
authored
fix: Fix local confusion in builtin min/max/rotl/rotr (#1540)
1 parent 0204ff3 commit d0a2c9e

16 files changed

+801
-2540
lines changed

package-lock.json

+29-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builtins.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import {
8888
} from "./program";
8989

9090
import {
91+
findUsedLocals,
9192
FlowFlags,
9293
LocalFlags
9394
} from "./flow";
@@ -1185,7 +1186,7 @@ function builtin_rotl(ctx: BuiltinContext): ExpressionRef {
11851186
case TypeKind.U16: {
11861187
// (value << (shift & mask)) | (value >>> ((0 - shift) & mask))
11871188
let flow = compiler.currentFlow;
1188-
let temp1 = flow.getTempLocal(type);
1189+
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
11891190
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
11901191
let temp2 = flow.getTempLocal(type);
11911192
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);
@@ -1266,7 +1267,7 @@ function builtin_rotr(ctx: BuiltinContext): ExpressionRef {
12661267
case TypeKind.U16: {
12671268
// (value >>> (shift & mask)) | (value << ((0 - shift) & mask))
12681269
let flow = compiler.currentFlow;
1269-
let temp1 = flow.getTempLocal(type);
1270+
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
12701271
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
12711272
let temp2 = flow.getTempLocal(type);
12721273
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);
@@ -1484,7 +1485,7 @@ function builtin_max(ctx: BuiltinContext): ExpressionRef {
14841485
if (op != -1) {
14851486
let flow = compiler.currentFlow;
14861487
let nativeType = type.toNativeType();
1487-
let temp1 = flow.getTempLocal(type);
1488+
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
14881489
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
14891490
let temp2 = flow.getTempLocal(type);
14901491
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);
@@ -1563,7 +1564,7 @@ function builtin_min(ctx: BuiltinContext): ExpressionRef {
15631564
if (op != -1) {
15641565
let flow = compiler.currentFlow;
15651566
let nativeType = type.toNativeType();
1566-
let temp1 = flow.getTempLocal(type);
1567+
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
15671568
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
15681569
let temp2 = flow.getTempLocal(type);
15691570
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);

tests/compiler/builtins.ts

+22
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,25 @@ assert(isInteger<ReturnType<() => i32>>());
561561
assert(isInteger<returnof<() => i32>>());
562562
assert(isManaged<returnof<() => C>>());
563563
assert(isManaged<ReturnType<() => C>>());
564+
565+
// Issue #1537: Make sure temp. locals are not confused
566+
567+
function max3(a: i32, b: i32, c: i32): i32 {
568+
return max(a, max(b, c));
569+
}
570+
assert(max3(3, 2, 1) == 3);
571+
572+
function min3(a: i32, b: i32, c: i32): i32 {
573+
return min(a, min(b, c));
574+
}
575+
assert(min3(1, 2, 3) == 1);
576+
577+
function rotl3(a: i8, b: i8, c: i8): i32 {
578+
return rotl(a, rotl(b, c));
579+
}
580+
assert(rotl3(3, 2, 1) == 48);
581+
582+
function rotr3(a: i8, b: i8, c: i8): i32 {
583+
return rotr(a, rotr(b, c));
584+
}
585+
assert(rotr3(48, 8, 1) == 3);

0 commit comments

Comments
 (0)