Wire assertions to global abort, see #8

This commit is contained in:
dcodeIO
2018-01-27 16:23:00 +01:00
parent de066fc128
commit 5d76ba9437
6 changed files with 161 additions and 66 deletions

View File

@ -1567,7 +1567,6 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
arg0 = compiler.compileExpression(operands[0], Type.i32, ConversionKind.NONE);
type = compiler.currentType;
arg1 = operands.length == 2 ? compiler.compileExpression(operands[1], compiler.options.usizeType) : compiler.options.usizeType.toNativeZero(module);
compiler.currentType = type.nonNullableType;
// just return ifTrueish if assertions are disabled, or simplify if dropped anyway
@ -1579,6 +1578,21 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
return arg0;
}
var abort: ExpressionRef = module.createUnreachable();
var abortPrototype = compiler.program.elements.get("abort");
if (abortPrototype && abortPrototype.kind == ElementKind.FUNCTION_PROTOTYPE) {
var abortInstance = (<FunctionPrototype>abortPrototype).resolve();
if (abortInstance && compiler.compileFunction(abortInstance)) {
abort = compiler.makeCall(abortInstance, [
operands.length == 2 ? compiler.compileExpression(operands[1], compiler.options.usizeType) : compiler.options.usizeType.toNativeZero(module),
compiler.compileStaticString(reportNode.range.source.path),
module.createI32(reportNode.range.line),
module.createI32(reportNode.range.column)
]);
}
}
compiler.currentType = type.nonNullableType;
if (contextualType == Type.void) { // simplify if dropped anyway
switch (compiler.currentType.kind) {
@ -1587,7 +1601,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI32,
arg0
),
module.createUnreachable()
abort
);
break;
@ -1597,7 +1611,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI64,
arg0
),
module.createUnreachable()
abort
);
break;
@ -1607,7 +1621,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(compiler.options.target == Target.WASM64 ? UnaryOp.EqzI64 : UnaryOp.EqzI32,
arg0
),
module.createUnreachable()
abort
);
break;
@ -1619,7 +1633,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
arg0,
module.createF32(0)
),
module.createUnreachable()
abort
);
break;
@ -1629,13 +1643,13 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
arg0,
module.createF64(0)
),
module.createUnreachable()
abort
);
break;
case TypeKind.VOID:
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
ret = module.createUnreachable();
ret = abort;
break;
}
compiler.currentType = Type.void;
@ -1648,7 +1662,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI32,
module.createTeeLocal(tempLocal0.index, arg0)
),
module.createUnreachable(),
abort,
module.createGetLocal(tempLocal0.index, NativeType.I32)
);
break;
@ -1660,7 +1674,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI64,
module.createTeeLocal(tempLocal0.index, arg0)
),
module.createUnreachable(),
abort,
module.createGetLocal(tempLocal0.index, NativeType.I64)
);
break;
@ -1672,7 +1686,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(compiler.options.target == Target.WASM64 ? UnaryOp.EqzI64 : UnaryOp.EqzI32,
module.createTeeLocal(tempLocal0.index, arg0)
),
module.createUnreachable(),
abort,
module.createGetLocal(tempLocal0.index, compiler.options.nativeSizeType)
);
break;
@ -1684,7 +1698,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createTeeLocal(tempLocal0.index, arg0),
module.createF32(0)
),
module.createUnreachable(),
abort,
module.createGetLocal(tempLocal0.index, NativeType.F32)
);
break;
@ -1696,14 +1710,14 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createTeeLocal(tempLocal0.index, arg0),
module.createF64(0)
),
module.createUnreachable(),
abort,
module.createGetLocal(tempLocal0.index, NativeType.F64)
);
break;
case TypeKind.VOID:
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
ret = module.createUnreachable();
ret = abort;
break;
}
}

View File

@ -2862,27 +2862,7 @@ export class Compiler extends DiagnosticEmitter {
return this.module.createI32(intValue.toI32());
case LiteralKind.STRING:
var stringValue = (<StringLiteralExpression>expression).value;
var stringSegment: MemorySegment | null = this.stringSegments.get(stringValue);
if (!stringSegment) {
var stringLength = stringValue.length;
var stringBuffer = new Uint8Array(4 + stringLength * 2);
stringBuffer[0] = stringLength & 0xff;
stringBuffer[1] = (stringLength >>> 8) & 0xff;
stringBuffer[2] = (stringLength >>> 16) & 0xff;
stringBuffer[3] = (stringLength >>> 24) & 0xff;
for (var i = 0; i < stringLength; ++i) {
stringBuffer[4 + i * 2] = stringValue.charCodeAt(i) & 0xff;
stringBuffer[5 + i * 2] = (stringValue.charCodeAt(i) >>> 8) & 0xff;
}
stringSegment = this.addMemorySegment(stringBuffer);
this.stringSegments.set(stringValue, stringSegment);
}
var stringOffset = stringSegment.offset;
this.currentType = this.options.usizeType;
return this.options.isWasm64
? this.module.createI64(stringOffset.lo, stringOffset.hi)
: this.module.createI32(stringOffset.lo);
return this.compileStaticString((<StringLiteralExpression>expression).value);
// case LiteralKind.OBJECT:
// case LiteralKind.REGEXP:
@ -2891,6 +2871,29 @@ export class Compiler extends DiagnosticEmitter {
throw new Error("not implemented");
}
compileStaticString(stringValue: string): ExpressionRef {
var stringSegment: MemorySegment | null = this.stringSegments.get(stringValue);
if (!stringSegment) {
var stringLength = stringValue.length;
var stringBuffer = new Uint8Array(4 + stringLength * 2);
stringBuffer[0] = stringLength & 0xff;
stringBuffer[1] = (stringLength >>> 8) & 0xff;
stringBuffer[2] = (stringLength >>> 16) & 0xff;
stringBuffer[3] = (stringLength >>> 24) & 0xff;
for (var i = 0; i < stringLength; ++i) {
stringBuffer[4 + i * 2] = stringValue.charCodeAt(i) & 0xff;
stringBuffer[5 + i * 2] = (stringValue.charCodeAt(i) >>> 8) & 0xff;
}
stringSegment = this.addMemorySegment(stringBuffer);
this.stringSegments.set(stringValue, stringSegment);
}
var stringOffset = stringSegment.offset;
this.currentType = this.options.usizeType;
return this.options.isWasm64
? this.module.createI64(stringOffset.lo, stringOffset.hi)
: this.module.createI32(stringOffset.lo);
}
compileNewExpression(expression: NewExpression, contextualType: Type): ExpressionRef {
var resolved = this.program.resolveExpression(expression.expression, this.currentFunction); // reports
if (resolved) {

View File

@ -121,20 +121,12 @@ export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: b
sb.push(context);
}
sb.push("\n");
var pos = range.start;
var line = 1;
var column = 1;
while (pos-- > 0)
if (text.charCodeAt(pos) == CharCode.LINEFEED)
line++;
else if (line == 1)
column++;
sb.push(" in ");
sb.push(range.source.path);
sb.push("(");
sb.push(line.toString(10));
sb.push(range.line.toString(10));
sb.push(",");
sb.push(column.toString(10));
sb.push(range.column.toString(10));
sb.push(")");
}
return sb.join("");

View File

@ -293,6 +293,27 @@ export class Range {
get atStart(): Range { return new Range(this.source, this.start, this.start); }
get atEnd(): Range { return new Range(this.source, this.end, this.end); }
get line(): i32 {
var text = this.source.text;
var pos = this.start;
var line = 1;
while (pos-- > 0)
if (text.charCodeAt(pos) == CharCode.LINEFEED)
line++;
return line;
}
get column(): i32 {
var text = this.source.text;
var pos = this.start;
var column = 1;
while (pos-- > 0)
if (text.charCodeAt(pos) == CharCode.LINEFEED)
break;
column++;
return column;
}
toString(): string {
return this.source.text.substring(this.start, this.end);
}