Skip to content

Commit 23d015b

Browse files
authored
wasm2asm fixes for coercion on rotl/rotr/ctz/popcnt calls, and refactor the name of constants to include the WASM_ prefix so as not to collide with constants for the unprefixed values (#1199)
1 parent de54837 commit 23d015b

4 files changed

Lines changed: 52 additions & 33 deletions

File tree

src/asmjs/shared-constants.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ cashew::IString GLOBAL("global"),
6262
MATH_IMUL("Math_imul"),
6363
MATH_ABS("Math_abs"),
6464
MATH_CEIL("Math_ceil"),
65+
MATH_CLZ32("Math_clz32"),
6566
MATH_FLOOR("Math_floor"),
6667
MATH_TRUNC("Math_trunc"),
6768
MATH_NEAREST("Math_NEAREST"),
6869
MATH_SQRT("Math_sqrt"),
6970
MATH_MIN("Math_min"),
7071
MATH_MAX("Math_max"),
71-
CTZ32("__wasm_ctz_i32"),
72-
CTZ64("__wasm_ctz_i64"),
73-
POPCNT32("__wasm_popcnt_i32"),
74-
POPCNT64("__wasm_popcnt_i64"),
75-
ROTL32("__wasm_rotl_i32"),
76-
ROTL64("__wasm_rotl_i64"),
77-
ROTR32("__wasm_rotr_i32"),
78-
ROTR64("__wasm_rotr_i64");
72+
WASM_CTZ32("__wasm_ctz_i32"),
73+
WASM_CTZ64("__wasm_ctz_i64"),
74+
WASM_CLZ32("__wasm_clz_i32"),
75+
WASM_CLZ64("__wasm_clz_i64"),
76+
WASM_POPCNT32("__wasm_popcnt_i32"),
77+
WASM_POPCNT64("__wasm_popcnt_i64"),
78+
WASM_ROTL32("__wasm_rotl_i32"),
79+
WASM_ROTL64("__wasm_rotl_i64"),
80+
WASM_ROTR32("__wasm_rotr_i32"),
81+
WASM_ROTR64("__wasm_rotr_i64");
7982
}

src/asmjs/shared-constants.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,23 @@ extern cashew::IString GLOBAL,
6565
MATH_IMUL,
6666
MATH_ABS,
6767
MATH_CEIL,
68+
MATH_CLZ32,
6869
MATH_FLOOR,
6970
MATH_TRUNC,
7071
MATH_NEAREST,
7172
MATH_SQRT,
7273
MATH_MIN,
7374
MATH_MAX,
74-
CTZ32,
75-
CTZ64,
76-
POPCNT32,
77-
POPCNT64,
78-
ROTL32,
79-
ROTL64,
80-
ROTR32,
81-
ROTR64;
75+
WASM_CTZ32,
76+
WASM_CTZ64,
77+
WASM_CLZ32,
78+
WASM_CLZ64,
79+
WASM_POPCNT32,
80+
WASM_POPCNT64,
81+
WASM_ROTL32,
82+
WASM_ROTL64,
83+
WASM_ROTR32,
84+
WASM_ROTR64;
8285
}
8386

8487
#endif // wasm_asmjs_shared_constants_h

src/wasm2asm.h

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static Function* makeCtzFunc(MixedArena& allocator, UnaryOp op) {
227227
Builder b(allocator);
228228
// if eqz(x) then 32 else (32 - clz(x ^ (x - 1)))
229229
bool is32Bit = (op == CtzInt32);
230-
Name funcName = is32Bit ? Name(CTZ32) : Name(CTZ64);
230+
Name funcName = is32Bit ? Name(WASM_CTZ32) : Name(WASM_CTZ64);
231231
BinaryOp subOp = is32Bit ? SubInt32 : SubInt64;
232232
BinaryOp xorOp = is32Bit ? XorInt32 : XorInt64;
233233
UnaryOp clzOp = is32Bit ? ClzInt32 : ClzInt64;
@@ -270,7 +270,7 @@ static Function* makePopcntFunc(MixedArena& allocator, UnaryOp op) {
270270
// popcnt implemented as:
271271
// int c; for (c = 0; x != 0; c++) { x = x & (x - 1) }; return c
272272
bool is32Bit = (op == PopcntInt32);
273-
Name funcName = is32Bit ? Name(POPCNT32) : Name(POPCNT64);
273+
Name funcName = is32Bit ? Name(WASM_POPCNT32) : Name(WASM_POPCNT64);
274274
BinaryOp addOp = is32Bit ? AddInt32 : AddInt64;
275275
BinaryOp subOp = is32Bit ? SubInt32 : SubInt64;
276276
BinaryOp andOp = is32Bit ? AndInt32 : AndInt64;
@@ -328,8 +328,8 @@ Function* makeRotFunc(MixedArena& allocator, BinaryOp op) {
328328
// where k is shift modulo w. reverse shifts for right rotate
329329
bool is32Bit = (op == RotLInt32 || op == RotRInt32);
330330
bool isLRot = (op == RotLInt32 || op == RotLInt64);
331-
static Name names[2][2] = {{Name(ROTR64), Name(ROTR32)},
332-
{Name(ROTL64), Name(ROTL32)}};
331+
static Name names[2][2] = {{Name(WASM_ROTR64), Name(WASM_ROTR32)},
332+
{Name(WASM_ROTL64), Name(WASM_ROTL32)}};
333333
static BinaryOp shifters[2][2] = {{ShrUInt64, ShrUInt32},
334334
{ShlInt64, ShlInt32}};
335335
Name funcName = names[isLRot][is32Bit];
@@ -1267,15 +1267,26 @@ Ref Wasm2AsmBuilder::processFunctionBody(Function* func, IString result) {
12671267
case i32: {
12681268
switch (curr->op) {
12691269
case ClzInt32:
1270-
return ValueBuilder::makeCall(MATH_CLZ32,
1271-
visit(curr->value,
1272-
EXPRESSION_RESULT));
1270+
return ValueBuilder::makeCall(
1271+
MATH_CLZ32,
1272+
visit(curr->value, EXPRESSION_RESULT)
1273+
);
12731274
case CtzInt32:
1274-
return ValueBuilder::makeCall(CTZ32, visit(curr->value,
1275-
EXPRESSION_RESULT));
1275+
return makeSigning(
1276+
ValueBuilder::makeCall(
1277+
WASM_CTZ32,
1278+
visit(curr->value, EXPRESSION_RESULT)
1279+
),
1280+
ASM_SIGNED
1281+
);
12761282
case PopcntInt32:
1277-
return ValueBuilder::makeCall(POPCNT32, visit(curr->value,
1278-
EXPRESSION_RESULT));
1283+
return makeSigning(
1284+
ValueBuilder::makeCall(
1285+
WASM_POPCNT32,
1286+
visit(curr->value, EXPRESSION_RESULT)
1287+
),
1288+
ASM_SIGNED
1289+
);
12791290
case EqZInt32:
12801291
return ValueBuilder::makeBinary(
12811292
makeAsmCoercion(visit(curr->value,
@@ -1476,9 +1487,11 @@ Ref Wasm2AsmBuilder::processFunctionBody(Function* func, IString result) {
14761487
return ValueBuilder::makeBinary(makeSigning(left, ASM_UNSIGNED), GE,
14771488
makeSigning(right, ASM_UNSIGNED));
14781489
case RotLInt32:
1479-
return ValueBuilder::makeCall(ROTL32, left, right);
1490+
return makeSigning(ValueBuilder::makeCall(WASM_ROTL32, left, right),
1491+
ASM_SIGNED);
14801492
case RotRInt32:
1481-
return ValueBuilder::makeCall(ROTR32, left, right);
1493+
return makeSigning(ValueBuilder::makeCall(WASM_ROTR32, left, right),
1494+
ASM_SIGNED);
14821495
default: {
14831496
std::cerr << "Unhandled binary operator: " << curr << std::endl;
14841497
abort();

test/i32.2asm.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ function asmFunc(global, env, buffer) {
9393
function $$13(x, y) {
9494
x = x | 0;
9595
y = y | 0;
96-
return __wasm_rotl_i32(x, y) | 0;
96+
return __wasm_rotl_i32(x, y) | 0 | 0;
9797
}
9898

9999
function $$14(x, y) {
100100
x = x | 0;
101101
y = y | 0;
102-
return __wasm_rotr_i32(x, y) | 0;
102+
return __wasm_rotr_i32(x, y) | 0 | 0;
103103
}
104104

105105
function $$15(x) {
@@ -109,12 +109,12 @@ function asmFunc(global, env, buffer) {
109109

110110
function $$16(x) {
111111
x = x | 0;
112-
return __wasm_ctz_i32(x) | 0;
112+
return __wasm_ctz_i32(x) | 0 | 0;
113113
}
114114

115115
function $$17(x) {
116116
x = x | 0;
117-
return __wasm_popcnt_i32(x) | 0;
117+
return __wasm_popcnt_i32(x) | 0 | 0;
118118
}
119119

120120
function $$18(x) {

0 commit comments

Comments
 (0)