Skip to content

Commit 0dedb22

Browse files
committed
Merge branch 'master' into move-codefolding
2 parents 3228f1e + a5d11ed commit 0dedb22

File tree

57 files changed

+11567
-8725
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+11567
-8725
lines changed

src/builtins.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,8 @@ function builtin_popcnt(ctx: BuiltinContext): ExpressionRef {
11261126
var type = compiler.currentType;
11271127
if (type.isValue) {
11281128
switch (compiler.currentType.kind) {
1129-
case TypeKind.BOOL: // not wrapped
1130-
case TypeKind.I8:
1129+
case TypeKind.BOOL: return arg0;
1130+
case TypeKind.I8: // not wrapped
11311131
case TypeKind.U8:
11321132
case TypeKind.I16:
11331133
case TypeKind.U16:
@@ -1171,14 +1171,27 @@ function builtin_rotl(ctx: BuiltinContext): ExpressionRef {
11711171
if (type.isValue) {
11721172
let arg1 = compiler.compileExpression(operands[1], type, Constraints.CONV_IMPLICIT);
11731173
switch (type.kind) {
1174+
case TypeKind.BOOL: return arg0;
11741175
case TypeKind.I8:
11751176
case TypeKind.I16:
11761177
case TypeKind.U8:
1177-
case TypeKind.U16:
1178-
case TypeKind.BOOL: {
1179-
return compiler.ensureSmallIntegerWrap(
1180-
module.binary(BinaryOp.RotlI32, arg0, arg1),
1181-
type
1178+
case TypeKind.U16: {
1179+
// (value << (shift & mask)) | (value >>> ((0 - shift) & mask))
1180+
return module.binary(BinaryOp.OrI32,
1181+
module.binary(
1182+
BinaryOp.ShlI32,
1183+
arg0,
1184+
module.binary(BinaryOp.AndI32, arg1, module.i32(type.size - 1))
1185+
),
1186+
module.binary(
1187+
BinaryOp.ShrU32,
1188+
arg0,
1189+
module.binary(
1190+
BinaryOp.AndI32,
1191+
module.binary(BinaryOp.SubI32, module.i32(0), arg1),
1192+
module.i32(type.size - 1)
1193+
)
1194+
)
11821195
);
11831196
}
11841197
case TypeKind.I32:
@@ -1221,14 +1234,27 @@ function builtin_rotr(ctx: BuiltinContext): ExpressionRef {
12211234
if (type.isValue) {
12221235
let arg1 = compiler.compileExpression(operands[1], type, Constraints.CONV_IMPLICIT);
12231236
switch (type.kind) {
1237+
case TypeKind.BOOL: return arg0;
12241238
case TypeKind.I8:
12251239
case TypeKind.I16:
12261240
case TypeKind.U8:
1227-
case TypeKind.U16:
1228-
case TypeKind.BOOL: {
1229-
return compiler.ensureSmallIntegerWrap(
1230-
module.binary(BinaryOp.RotrI32, arg0, arg1),
1231-
type
1241+
case TypeKind.U16: {
1242+
// (value >>> (shift & mask)) | (value << ((0 - shift) & mask))
1243+
return module.binary(BinaryOp.OrI32,
1244+
module.binary(
1245+
BinaryOp.ShrU32,
1246+
arg0,
1247+
module.binary(BinaryOp.AndI32, arg1, module.i32(type.size - 1))
1248+
),
1249+
module.binary(
1250+
BinaryOp.ShlI32,
1251+
arg0,
1252+
module.binary(
1253+
BinaryOp.AndI32,
1254+
module.binary(BinaryOp.SubI32, module.i32(0), arg1),
1255+
module.i32(type.size - 1)
1256+
)
1257+
)
12321258
);
12331259
}
12341260
case TypeKind.I32:
@@ -2129,7 +2155,7 @@ function builtin_add(ctx: BuiltinContext): ExpressionRef {
21292155
{
21302156
op = BinaryOp.AddI32;
21312157
break;
2132-
}
2158+
}
21332159
case TypeKind.I64:
21342160
case TypeKind.U64: {
21352161
op = BinaryOp.AddI64;
@@ -2220,7 +2246,7 @@ function builtin_sub(ctx: BuiltinContext): ExpressionRef {
22202246
{
22212247
op = BinaryOp.SubI32;
22222248
break;
2223-
}
2249+
}
22242250
case TypeKind.I64:
22252251
case TypeKind.U64: {
22262252
op = BinaryOp.SubI64;
@@ -2311,7 +2337,7 @@ function builtin_mul(ctx: BuiltinContext): ExpressionRef {
23112337
{
23122338
op = BinaryOp.MulI32;
23132339
break;
2314-
}
2340+
}
23152341
case TypeKind.I64:
23162342
case TypeKind.U64: {
23172343
op = BinaryOp.MulI64;
@@ -3041,7 +3067,7 @@ function builtin_assert(ctx: BuiltinContext): ExpressionRef {
30413067
case TypeKind.EXTERNREF:
30423068
case TypeKind.EXNREF:
30433069
case TypeKind.ANYREF: return module.if(module.ref_is_null(arg0), abort);
3044-
3070+
30453071
}
30463072
} else {
30473073
compiler.currentType = type.nonNullableType;

src/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,9 +1496,9 @@ export class Module {
14961496
this.setAllowInliningFunctionsWithLoops(optimizeLevel >= 3);
14971497
} else {
14981498
this.setAlwaysInlineMaxSize(
1499-
optimizeLevel == 0 && shrinkLevel >= 0
1499+
optimizeLevel <= 1 || shrinkLevel >= 2
15001500
? 2
1501-
: 4
1501+
: 6
15021502
);
15031503
this.setFlexibleInlineMaxSize(65);
15041504
this.setOneCallerInlineMaxSize(80);

tests/compiler/binary.optimized.wat

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
(module
22
(type $none_=>_none (func))
33
(type $i32_=>_i32 (func (param i32) (result i32)))
4-
(type $f32_=>_f32 (func (param f32) (result f32)))
5-
(type $f64_=>_f64 (func (param f64) (result f64)))
64
(memory $0 0)
75
(global $binary/i (mut i32) (i32.const 0))
86
(global $binary/I (mut i64) (i64.const 0))
@@ -42,23 +40,9 @@
4240
end
4341
local.get $2
4442
)
45-
(func $~lib/math/NativeMathf.mod (param $0 f32) (result f32)
46-
local.get $0
47-
local.get $0
48-
f32.trunc
49-
f32.sub
50-
local.get $0
51-
f32.copysign
52-
)
53-
(func $~lib/math/NativeMath.mod (param $0 f64) (result f64)
54-
local.get $0
55-
local.get $0
56-
f64.trunc
57-
f64.sub
58-
local.get $0
59-
f64.copysign
60-
)
6143
(func $start:binary
44+
(local $0 f32)
45+
(local $1 f64)
6246
global.get $binary/i
6347
call $~lib/math/ipow32
6448
drop
@@ -174,9 +158,6 @@
174158
i64.const 0
175159
global.set $binary/I
176160
global.get $binary/f
177-
call $~lib/math/NativeMathf.mod
178-
drop
179-
global.get $binary/f
180161
f32.const 1
181162
f32.add
182163
global.set $binary/f
@@ -185,7 +166,12 @@
185166
f32.sub
186167
global.set $binary/f
187168
global.get $binary/f
188-
call $~lib/math/NativeMathf.mod
169+
local.tee $0
170+
local.get $0
171+
f32.trunc
172+
f32.sub
173+
local.get $0
174+
f32.copysign
189175
global.set $binary/f
190176
global.get $binary/f
191177
f32.const 1
@@ -196,12 +182,14 @@
196182
f32.sub
197183
global.set $binary/f
198184
global.get $binary/f
199-
call $~lib/math/NativeMathf.mod
185+
local.tee $0
186+
local.get $0
187+
f32.trunc
188+
f32.sub
189+
local.get $0
190+
f32.copysign
200191
global.set $binary/f
201192
global.get $binary/F
202-
call $~lib/math/NativeMath.mod
203-
drop
204-
global.get $binary/F
205193
f64.const 1
206194
f64.add
207195
global.set $binary/F
@@ -210,7 +198,12 @@
210198
f64.sub
211199
global.set $binary/F
212200
global.get $binary/F
213-
call $~lib/math/NativeMath.mod
201+
local.tee $1
202+
local.get $1
203+
f64.trunc
204+
f64.sub
205+
local.get $1
206+
f64.copysign
214207
global.set $binary/F
215208
global.get $binary/F
216209
f64.const 1
@@ -221,7 +214,12 @@
221214
f64.sub
222215
global.set $binary/F
223216
global.get $binary/F
224-
call $~lib/math/NativeMath.mod
217+
local.tee $1
218+
local.get $1
219+
f64.trunc
220+
f64.sub
221+
local.get $1
222+
f64.copysign
225223
global.set $binary/F
226224
)
227225
(func $~start

0 commit comments

Comments
 (0)