Skip to content

Commit

Permalink
use error(DiagnosticCode.Not_implemented_0) instead of assert(false)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayphelps committed Jul 15, 2020
1 parent 007590a commit 2ff2bff
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
11 changes: 6 additions & 5 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,8 @@ function builtin_select(ctx: BuiltinContext): ExpressionRef {
var arg1 = compiler.compileExpression(operands[1], type, Constraints.CONV_IMPLICIT);
var arg2 = compiler.makeIsTrueish(
compiler.compileExpression(operands[2], Type.bool),
compiler.currentType // ^
compiler.currentType, // ^
operands[2]
);
compiler.currentType = type;
return module.select(arg0, arg1, arg2);
Expand Down Expand Up @@ -2577,9 +2578,9 @@ function builtin_memory_data(ctx: BuiltinContext): ExpressionRef {
let exprs = new Array<ExpressionRef>(numElements);
let isStatic = true;
for (let i = 0; i < numElements; ++i) {
let expression = expressions[i];
if (expression) {
let expr = compiler.compileExpression(expression, elementType,
let elementExpression = expressions[i];
if (elementExpression) {
let expr = compiler.compileExpression(elementExpression, elementType,
Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN
);
let precomp = module.runExpression(expr, ExpressionRunnerFlags.PreserveSideeffects);
Expand All @@ -2590,7 +2591,7 @@ function builtin_memory_data(ctx: BuiltinContext): ExpressionRef {
}
exprs[i] = expr;
} else {
exprs[i] = compiler.makeZero(elementType);
exprs[i] = compiler.makeZero(elementType, valuesOperand);
}
}
if (!isStatic) {
Expand Down
100 changes: 57 additions & 43 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ export class Compiler extends DiagnosticEmitter {
if (global.is(CommonFlags.INLINED)) {
initExpr = this.compileInlineConstant(global, global.type, Constraints.PREFER_STATIC | Constraints.WILL_RETAIN);
} else {
initExpr = this.makeZero(type);
initExpr = this.makeZero(type, global.declaration);
}
}

Expand All @@ -1214,7 +1214,7 @@ export class Compiler extends DiagnosticEmitter {
findDecorator(DecoratorKind.INLINE, global.decoratorNodes)!.range, "inline"
);
}
module.addGlobal(internalName, nativeType, true, this.makeZero(type));
module.addGlobal(internalName, nativeType, true, this.makeZero(type, global.declaration));
if (type.isManaged && !this.skippedAutoreleases.has(initExpr)) initExpr = this.makeRetain(initExpr, type);
this.currentBody.push(
module.global_set(internalName, initExpr)
Expand Down Expand Up @@ -2361,7 +2361,8 @@ export class Compiler extends DiagnosticEmitter {
this.currentFlow = condFlow;
let condExpr = this.makeIsTrueish(
this.compileExpression(statement.condition, Type.i32),
this.currentType
this.currentType,
statement.condition
);
let condKind = this.evaluateCondition(condExpr);

Expand Down Expand Up @@ -2505,7 +2506,8 @@ export class Compiler extends DiagnosticEmitter {
if (condition) {
condExpr = this.makeIsTrueish(
this.compileExpression(condition, Type.bool),
this.currentType
this.currentType,
condition
);
condKind = this.evaluateCondition(condExpr);

Expand Down Expand Up @@ -2665,7 +2667,8 @@ export class Compiler extends DiagnosticEmitter {
// Precompute the condition (always executes)
var condExpr = this.makeIsTrueish(
this.compileExpression(statement.condition, Type.bool),
this.currentType
this.currentType,
statement.condition
);
var condKind = this.evaluateCondition(condExpr);

Expand Down Expand Up @@ -3186,7 +3189,7 @@ export class Compiler extends DiagnosticEmitter {
// TODO: Detect this condition inside of a loop instead?
initializers.push(
module.local_set(local.index,
this.makeZero(type)
this.makeZero(type, declaration)
)
);
flow.setLocalFlag(local.index, LocalFlags.CONDITIONALLY_RETAINED);
Expand Down Expand Up @@ -3255,7 +3258,8 @@ export class Compiler extends DiagnosticEmitter {
this.currentFlow = condFlow;
var condExpr = this.makeIsTrueish(
this.compileExpression(statement.condition, Type.bool),
this.currentType
this.currentType,
statement.condition
);
var condKind = this.evaluateCondition(condExpr);

Expand Down Expand Up @@ -5798,8 +5802,8 @@ export class Compiler extends DiagnosticEmitter {
rightFlow.freeScopedLocals();
this.currentFlow = flow;
expr = module.if(
this.makeIsTrueish(leftExpr, leftType),
this.makeIsTrueish(rightExpr, rightType),
this.makeIsTrueish(leftExpr, leftType, left),
this.makeIsTrueish(rightExpr, rightType, right),
module.i32(0)
);
this.currentType = Type.bool;
Expand Down Expand Up @@ -5843,7 +5847,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentFlow = flow;

expr = module.if(
this.makeIsTrueish(leftExpr, leftType),
this.makeIsTrueish(leftExpr, leftType, left),
rightExpr,
retainLeftInElse
? this.makeRetain(
Expand All @@ -5864,7 +5868,7 @@ export class Compiler extends DiagnosticEmitter {
// simplify if cloning left without side effects is possible
if (expr = module.cloneExpression(leftExpr, true, 0)) {
expr = module.if(
this.makeIsTrueish(leftExpr, this.currentType),
this.makeIsTrueish(leftExpr, this.currentType, left),
rightExpr,
expr
);
Expand All @@ -5875,7 +5879,7 @@ export class Compiler extends DiagnosticEmitter {
if (!flow.canOverflow(leftExpr, leftType)) flow.setLocalFlag(tempLocal.index, LocalFlags.WRAPPED);
if (flow.isNonnull(leftExpr, leftType)) flow.setLocalFlag(tempLocal.index, LocalFlags.NONNULL);
expr = module.if(
this.makeIsTrueish(module.local_tee(tempLocal.index, leftExpr), leftType),
this.makeIsTrueish(module.local_tee(tempLocal.index, leftExpr), leftType, left),
rightExpr,
module.local_get(tempLocal.index, leftType.toNativeType())
);
Expand Down Expand Up @@ -5904,9 +5908,9 @@ export class Compiler extends DiagnosticEmitter {
rightFlow.freeScopedLocals();
this.currentFlow = flow;
expr = module.if(
this.makeIsTrueish(leftExpr, leftType),
this.makeIsTrueish(leftExpr, leftType, left),
module.i32(1),
this.makeIsTrueish(rightExpr, rightType)
this.makeIsTrueish(rightExpr, rightType, right)
);
this.currentType = Type.bool;

Expand Down Expand Up @@ -5951,7 +5955,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentFlow = flow;

expr = module.if(
this.makeIsTrueish(leftExpr, leftType),
this.makeIsTrueish(leftExpr, leftType, left),
retainLeftInThen
? this.makeRetain(
module.local_get(temp.index, leftType.toNativeType()),
Expand All @@ -5972,7 +5976,7 @@ export class Compiler extends DiagnosticEmitter {
// simplify if cloning left without side effects is possible
if (expr = module.cloneExpression(leftExpr, true, 0)) {
expr = module.if(
this.makeIsTrueish(leftExpr, leftType),
this.makeIsTrueish(leftExpr, leftType, left),
expr,
rightExpr
);
Expand All @@ -5983,7 +5987,7 @@ export class Compiler extends DiagnosticEmitter {
if (!flow.canOverflow(leftExpr, leftType)) flow.setLocalFlag(temp.index, LocalFlags.WRAPPED);
if (flow.isNonnull(leftExpr, leftType)) flow.setLocalFlag(temp.index, LocalFlags.NONNULL);
expr = module.if(
this.makeIsTrueish(module.local_tee(temp.index, leftExpr), leftType),
this.makeIsTrueish(module.local_tee(temp.index, leftExpr), leftType, left),
module.local_get(temp.index, leftType.toNativeType()),
rightExpr
);
Expand Down Expand Up @@ -7389,7 +7393,7 @@ export class Compiler extends DiagnosticEmitter {
let needsVarargsStub = false;
for (let n = numParameters; n < overloadNumParameters; ++n) {
// TODO: inline constant initializers and skip varargs stub
paramExprs[1 + n] = this.makeZero(overloadParameterTypes[n]);
paramExprs[1 + n] = this.makeZero(overloadParameterTypes[n], overloadInstance.declaration);
needsVarargsStub = true;
}
let calledName = needsVarargsStub
Expand Down Expand Up @@ -7874,7 +7878,7 @@ export class Compiler extends DiagnosticEmitter {
}
}
}
operands.push(this.makeZero(parameterTypes[i]));
operands.push(this.makeZero(parameterTypes[i], instance.declaration));
allOptionalsAreConstant = false;
}
if (!allOptionalsAreConstant) {
Expand Down Expand Up @@ -7966,15 +7970,16 @@ export class Compiler extends DiagnosticEmitter {
);
}
assert(index == numArgumentsInclThis);
return this.makeCallIndirect(signature, indexArg, operands, immediatelyDropped);
return this.makeCallIndirect(signature, indexArg, operands, immediatelyDropped, reportNode);
}

/** Creates an indirect call to the function at `indexArg` in the function table. */
makeCallIndirect(
signature: Signature,
indexArg: ExpressionRef,
operands: ExpressionRef[] | null = null,
immediatelyDropped: bool = false
immediatelyDropped: bool = false,
reportNode: Node
): ExpressionRef {
var module = this.module;
var numOperands = operands ? operands.length : 0;
Expand All @@ -8000,7 +8005,7 @@ export class Compiler extends DiagnosticEmitter {
}
let parameterTypes = signature.parameterTypes;
for (let i = numArguments; i < maxArguments; ++i) {
operands.push(this.makeZero(parameterTypes[i]));
operands.push(this.makeZero(parameterTypes[i], reportNode));
}
}

Expand Down Expand Up @@ -8546,7 +8551,7 @@ export class Compiler extends DiagnosticEmitter {
? BinaryOp.NeI64
: BinaryOp.NeI32,
expr,
this.makeZero(actualType)
this.makeZero(actualType, expression.expression)
);
}

Expand Down Expand Up @@ -8653,7 +8658,7 @@ export class Compiler extends DiagnosticEmitter {
? BinaryOp.NeI64
: BinaryOp.NeI32,
expr,
this.makeZero(actualType)
this.makeZero(actualType, expression.expression)
);

// <nonNullable> is just `true`
Expand Down Expand Up @@ -8792,9 +8797,9 @@ export class Compiler extends DiagnosticEmitter {
var isStatic = true;
var nativeElementType = elementType.toNativeType();
for (let i = 0; i < length; ++i) {
let expression = expressions[i];
if (expression) {
let expr = this.compileExpression(<Expression>expression, elementType,
let elementExpression = expressions[i];
if (elementExpression) {
let expr = this.compileExpression(<Expression>elementExpression, elementType,
Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN
);
let precomp = module.runExpression(expr, ExpressionRunnerFlags.PreserveSideeffects);
Expand All @@ -8805,7 +8810,7 @@ export class Compiler extends DiagnosticEmitter {
}
values[i] = expr;
} else {
values[i] = this.makeZero(elementType);
values[i] = this.makeZero(elementType, expression);
}
}

Expand Down Expand Up @@ -8962,9 +8967,9 @@ export class Compiler extends DiagnosticEmitter {
var nativeElementType = elementType.toNativeType();
var isStatic = true;
for (let i = 0; i < length; ++i) {
let expression = expressions[i];
if (expression) {
let expr = this.compileExpression(expression, elementType,
let elementExpression = expressions[i];
if (elementExpression) {
let expr = this.compileExpression(elementExpression, elementType,
Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN
);
let precomp = module.runExpression(expr, ExpressionRunnerFlags.PreserveSideeffects);
Expand All @@ -8975,7 +8980,7 @@ export class Compiler extends DiagnosticEmitter {
}
values[i] = expr;
} else {
values[i] = this.makeZero(elementType);
values[i] = this.makeZero(elementType, expression);
}
}

Expand Down Expand Up @@ -9247,7 +9252,7 @@ export class Compiler extends DiagnosticEmitter {
module.store( // TODO: handle setters as well
fieldType.byteSize,
module.local_get(tempLocal.index, nativeClassType),
this.makeZero(fieldType),
this.makeZero(fieldType, expression),
fieldType.toNativeType(),
fieldInstance.memoryOffset
)
Expand Down Expand Up @@ -9536,7 +9541,7 @@ export class Compiler extends DiagnosticEmitter {
ctorInstance,
argumentExpressions,
reportNode,
this.makeZero(this.options.usizeType),
this.makeZero(this.options.usizeType, reportNode),
constraints
);
if (getExpressionType(expr) != NativeType.None) { // possibly WILL_DROP
Expand Down Expand Up @@ -9702,7 +9707,8 @@ export class Compiler extends DiagnosticEmitter {

var condExpr = this.makeIsTrueish(
this.compileExpression(expression.condition, Type.bool),
this.currentType
this.currentType,
expression.condition
);
// Try to eliminate unnecesssary branches if the condition is constant
// FIXME: skips common denominator, inconsistently picking branch type
Expand Down Expand Up @@ -10129,7 +10135,7 @@ export class Compiler extends DiagnosticEmitter {
this.options.isWasm64
? BinaryOp.SubI64
: BinaryOp.SubI32,
this.makeZero(this.currentType),
this.makeZero(this.currentType, expression.operand),
expr
);
break;
Expand Down Expand Up @@ -10316,7 +10322,7 @@ export class Compiler extends DiagnosticEmitter {
// allow '!' for references even without an overload
}

expr = module.unary(UnaryOp.EqzI32, this.makeIsTrueish(expr, this.currentType));
expr = module.unary(UnaryOp.EqzI32, this.makeIsTrueish(expr, this.currentType, expression.operand));
this.currentType = Type.bool;
break;
}
Expand Down Expand Up @@ -10640,7 +10646,7 @@ export class Compiler extends DiagnosticEmitter {
// === Specialized code generation ==============================================================

/** Makes a constant zero of the specified type. */
makeZero(type: Type): ExpressionRef {
makeZero(type: Type, reportNode: Node): ExpressionRef {
var module = this.module;
switch (type.kind) {
default: assert(false);
Expand All @@ -10660,7 +10666,11 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.V128: return module.v128(v128_zero);
case TypeKind.EXTERNREF:
// TODO: return null ref for both externref as well as funcref
assert(false, 'null for externref is not yet supported');
this.error(
DiagnosticCode.Not_implemented_0,
reportNode.range,
"ref.null<externref>"
);
return module.unreachable();
}
}
Expand Down Expand Up @@ -10707,7 +10717,7 @@ export class Compiler extends DiagnosticEmitter {
}

/** Creates a comparison whether an expression is 'true' in a broader sense. */
makeIsTrueish(expr: ExpressionRef, type: Type): ExpressionRef {
makeIsTrueish(expr: ExpressionRef, type: Type, reportNode: Node): ExpressionRef {
var module = this.module;
switch (type.kind) {
case TypeKind.I8:
Expand Down Expand Up @@ -10764,7 +10774,11 @@ export class Compiler extends DiagnosticEmitter {
// TODO: non-null object might still be considered falseish
// i.e. a ref to Boolean(false), Number(0), String("") etc.
// TODO: return module.unary(UnaryOp.EqzI32, module.ref_is_null(expr));
assert(false, 'Truthy checks for externref are not yet supported');
this.error(
DiagnosticCode.Not_implemented_0,
reportNode.range,
"ref.is_null"
);
}
default: {
assert(false);
Expand Down Expand Up @@ -10855,7 +10869,7 @@ export class Compiler extends DiagnosticEmitter {

// otherwise initialize with zero
} else {
initExpr = this.makeZero(fieldType);
initExpr = this.makeZero(fieldType, fieldPrototype.declaration);
}

stmts.push(
Expand Down Expand Up @@ -10890,7 +10904,7 @@ export class Compiler extends DiagnosticEmitter {
// essentially ignoring the message GC-wise. Doesn't matter anyway on a crash.
messageArg = this.compileExpression(message, stringInstance.type, Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN);
} else {
messageArg = this.makeZero(stringInstance.type);
messageArg = this.makeZero(stringInstance.type, codeLocation);
}

return this.makeStaticAbort(messageArg, codeLocation);
Expand Down

0 comments on commit 2ff2bff

Please sign in to comment.