Skip to content
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

Refactor binary expression compilation to be reusable in builtins #1489

Merged
merged 4 commits 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
199 changes: 36 additions & 163 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2070,11 +2070,8 @@ function builtin_store(ctx: BuiltinContext): ExpressionRef {
inType.size < type.size // int to larger int (clear garbage bits)
)
) {
arg1 = compiler.convertExpression(arg1,
inType, type,
false, false, // still clears garbage bits when not wrapping
operands[1]
);
// either conversion or memory operation clears garbage bits
arg1 = compiler.convertExpression(arg1, inType, type, false, operands[1]);
inType = type;
}
var immOffset = 0;
Expand Down Expand Up @@ -2102,81 +2099,44 @@ builtins.set(BuiltinNames.store, builtin_store);
function builtin_add(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
var module = compiler.module;
if (checkTypeOptional(ctx, true) | checkArgsRequired(ctx, 2))
if (checkTypeOptional(ctx, true) | checkArgsRequired(ctx, 2)) {
return module.unreachable();
}
var operands = ctx.operands;
var typeArguments = ctx.typeArguments;
var left = operands[0];
var arg0 = typeArguments
? compiler.compileExpression(
left,
typeArguments[0],
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
)
: compiler.compileExpression(operands[0], Type.auto, Constraints.MUST_WRAP);
: compiler.compileExpression(operands[0], Type.auto);
var type = compiler.currentType;
if (type.isValue) {
let arg1: ExpressionRef;
if (!typeArguments && left.isNumericLiteral) {
// prefer right type
arg1 = compiler.compileExpression(
operands[1],
type,
Constraints.MUST_WRAP
type
);
if (compiler.currentType != type) {
arg0 = compiler.compileExpression(
left,
(type = compiler.currentType),
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
);
}
} else {
arg1 = compiler.compileExpression(
operands[1],
type,
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
);
}
let op: BinaryOp = -1;
switch (type.kind) {
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.BOOL: {
return compiler.ensureSmallIntegerWrap(
module.binary(BinaryOp.AddI32, arg0, arg1),
type
);
}
case TypeKind.I32:
case TypeKind.U32:
{
op = BinaryOp.AddI32;
break;
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.AddI64;
break;
}
case TypeKind.ISIZE:
case TypeKind.USIZE: {
op = compiler.options.isWasm64 ? BinaryOp.AddI64 : BinaryOp.AddI32;
break;
}
case TypeKind.F32: {
op = BinaryOp.AddF32;
break;
}
case TypeKind.F64: {
op = BinaryOp.AddF64;
break;
}
}
if (op != -1) {
return module.binary(op, arg0, arg1);
if (type.isNumericValue) {
return compiler.makeAdd(arg0, arg1, type);
}
}
compiler.error(
Expand All @@ -2193,81 +2153,44 @@ builtins.set(BuiltinNames.add, builtin_add);
function builtin_sub(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
var module = compiler.module;
if (checkTypeOptional(ctx, true) | checkArgsRequired(ctx, 2))
if (checkTypeOptional(ctx, true) | checkArgsRequired(ctx, 2)) {
return module.unreachable();
}
var operands = ctx.operands;
var typeArguments = ctx.typeArguments;
var left = operands[0];
var arg0 = typeArguments
? compiler.compileExpression(
left,
typeArguments[0],
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
)
: compiler.compileExpression(operands[0], Type.auto, Constraints.MUST_WRAP);
: compiler.compileExpression(operands[0], Type.auto);
var type = compiler.currentType;
if (type.isValue) {
let arg1: ExpressionRef;
if (!typeArguments && left.isNumericLiteral) {
// prefer right type
arg1 = compiler.compileExpression(
operands[1],
type,
Constraints.MUST_WRAP
type
);
if (compiler.currentType != type) {
arg0 = compiler.compileExpression(
left,
(type = compiler.currentType),
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
);
}
} else {
arg1 = compiler.compileExpression(
operands[1],
type,
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
);
}
let op: BinaryOp = -1;
switch (type.kind) {
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.BOOL: {
return compiler.ensureSmallIntegerWrap(
module.binary(BinaryOp.SubI32, arg0, arg1),
type
);
}
case TypeKind.I32:
case TypeKind.U32:
{
op = BinaryOp.SubI32;
break;
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.SubI64;
break;
}
case TypeKind.ISIZE:
case TypeKind.USIZE: {
op = compiler.options.isWasm64 ? BinaryOp.SubI64 : BinaryOp.SubI32;
break;
}
case TypeKind.F32: {
op = BinaryOp.SubF32;
break;
}
case TypeKind.F64: {
op = BinaryOp.SubF64;
break;
}
}
if (op != -1) {
return module.binary(op, arg0, arg1);
if (type.isNumericValue) {
return compiler.makeSub(arg0, arg1, type);
}
}
compiler.error(
Expand All @@ -2284,81 +2207,44 @@ builtins.set(BuiltinNames.sub, builtin_sub);
function builtin_mul(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
var module = compiler.module;
if (checkTypeOptional(ctx, true) | checkArgsRequired(ctx, 2))
if (checkTypeOptional(ctx, true) | checkArgsRequired(ctx, 2)) {
return module.unreachable();
}
var operands = ctx.operands;
var typeArguments = ctx.typeArguments;
var left = operands[0];
var arg0 = typeArguments
? compiler.compileExpression(
left,
typeArguments[0],
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
)
: compiler.compileExpression(operands[0], Type.auto, Constraints.MUST_WRAP);
: compiler.compileExpression(operands[0], Type.auto);
var type = compiler.currentType;
if (type.isValue) {
let arg1: ExpressionRef;
if (!typeArguments && left.isNumericLiteral) {
// prefer right type
arg1 = compiler.compileExpression(
operands[1],
type,
Constraints.MUST_WRAP
type
);
if (compiler.currentType != type) {
arg0 = compiler.compileExpression(
left,
(type = compiler.currentType),
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
);
}
} else {
arg1 = compiler.compileExpression(
operands[1],
type,
Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP
Constraints.CONV_IMPLICIT
);
}
let op: BinaryOp = -1;
switch (type.kind) {
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.BOOL: {
return compiler.ensureSmallIntegerWrap(
module.binary(BinaryOp.MulI32, arg0, arg1),
type
);
}
case TypeKind.I32:
case TypeKind.U32:
{
op = BinaryOp.MulI32;
break;
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.MulI64;
break;
}
case TypeKind.ISIZE:
case TypeKind.USIZE: {
op = compiler.options.isWasm64 ? BinaryOp.MulI64 : BinaryOp.MulI32;
break;
}
case TypeKind.F32: {
op = BinaryOp.MulF32;
break;
}
case TypeKind.F64: {
op = BinaryOp.MulF64;
break;
}
}
if (op != -1) {
return module.binary(op, arg0, arg1);
if (type.isNumericValue) {
return compiler.makeMul(arg0, arg1, type);
}
}
compiler.error(
Expand Down Expand Up @@ -2458,11 +2344,8 @@ function builtin_atomic_store(ctx: BuiltinContext): ExpressionRef {
inType.size < type.size // int to larger int (clear garbage bits)
)
) {
arg1 = compiler.convertExpression(arg1,
inType, type,
false, false, // still clears garbage bits when not wrapping
operands[1]
);
// either conversion or memory operation clears garbage bits
arg1 = compiler.convertExpression(arg1, inType, type, false, operands[1]);
inType = type;
}
var immOffset = operands.length == 3 ? evaluateImmediateOffset(operands[2], compiler) : 0; // reports
Expand Down Expand Up @@ -2519,11 +2402,8 @@ function builtin_atomic_binary(ctx: BuiltinContext, op: AtomicRMWOp, opName: str
inType.size < type.size // int to larger int (clear garbage bits)
)
) {
arg1 = compiler.convertExpression(arg1,
inType, type,
false, false, // still clears garbage bits when not wrapping
operands[1]
);
// either conversion or memory operation clears garbage bits
arg1 = compiler.convertExpression(arg1, inType, type, false, operands[1]);
inType = type;
}
var immOffset = operands.length == 3 ? evaluateImmediateOffset(operands[2], compiler) : 0; // reports
Expand Down Expand Up @@ -2619,16 +2499,9 @@ function builtin_atomic_cmpxchg(ctx: BuiltinContext): ExpressionRef {
inType.size < type.size // int to larger int (clear garbage bits)
)
) {
arg1 = compiler.convertExpression(arg1,
inType, type,
false, false, // still clears garbage bits when not wrapping
operands[1]
);
arg2 = compiler.convertExpression(arg2,
inType, type,
false, false, // still clears garbage bits when not wrapping
operands[2]
);
// either conversion or memory operation clears garbage bits
arg1 = compiler.convertExpression(arg1, inType, type, false, operands[1]);
arg2 = compiler.convertExpression(arg2, inType, type, false, operands[2]);
inType = type;
}
var immOffset = operands.length == 4 ? evaluateImmediateOffset(operands[3], compiler) : 0; // reports
Expand Down
Loading