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); arg0 = compiler.compileExpression(operands[0], Type.i32, ConversionKind.NONE);
type = compiler.currentType; type = compiler.currentType;
arg1 = operands.length == 2 ? compiler.compileExpression(operands[1], compiler.options.usizeType) : compiler.options.usizeType.toNativeZero(module);
compiler.currentType = type.nonNullableType; compiler.currentType = type.nonNullableType;
// just return ifTrueish if assertions are disabled, or simplify if dropped anyway // 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; 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 if (contextualType == Type.void) { // simplify if dropped anyway
switch (compiler.currentType.kind) { switch (compiler.currentType.kind) {
@ -1587,7 +1601,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI32, module.createUnary(UnaryOp.EqzI32,
arg0 arg0
), ),
module.createUnreachable() abort
); );
break; break;
@ -1597,7 +1611,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI64, module.createUnary(UnaryOp.EqzI64,
arg0 arg0
), ),
module.createUnreachable() abort
); );
break; break;
@ -1607,7 +1621,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(compiler.options.target == Target.WASM64 ? UnaryOp.EqzI64 : UnaryOp.EqzI32, module.createUnary(compiler.options.target == Target.WASM64 ? UnaryOp.EqzI64 : UnaryOp.EqzI32,
arg0 arg0
), ),
module.createUnreachable() abort
); );
break; break;
@ -1619,7 +1633,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
arg0, arg0,
module.createF32(0) module.createF32(0)
), ),
module.createUnreachable() abort
); );
break; break;
@ -1629,13 +1643,13 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
arg0, arg0,
module.createF64(0) module.createF64(0)
), ),
module.createUnreachable() abort
); );
break; break;
case TypeKind.VOID: case TypeKind.VOID:
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range); compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
ret = module.createUnreachable(); ret = abort;
break; break;
} }
compiler.currentType = Type.void; compiler.currentType = Type.void;
@ -1648,7 +1662,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI32, module.createUnary(UnaryOp.EqzI32,
module.createTeeLocal(tempLocal0.index, arg0) module.createTeeLocal(tempLocal0.index, arg0)
), ),
module.createUnreachable(), abort,
module.createGetLocal(tempLocal0.index, NativeType.I32) module.createGetLocal(tempLocal0.index, NativeType.I32)
); );
break; break;
@ -1660,7 +1674,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createUnary(UnaryOp.EqzI64, module.createUnary(UnaryOp.EqzI64,
module.createTeeLocal(tempLocal0.index, arg0) module.createTeeLocal(tempLocal0.index, arg0)
), ),
module.createUnreachable(), abort,
module.createGetLocal(tempLocal0.index, NativeType.I64) module.createGetLocal(tempLocal0.index, NativeType.I64)
); );
break; 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.createUnary(compiler.options.target == Target.WASM64 ? UnaryOp.EqzI64 : UnaryOp.EqzI32,
module.createTeeLocal(tempLocal0.index, arg0) module.createTeeLocal(tempLocal0.index, arg0)
), ),
module.createUnreachable(), abort,
module.createGetLocal(tempLocal0.index, compiler.options.nativeSizeType) module.createGetLocal(tempLocal0.index, compiler.options.nativeSizeType)
); );
break; break;
@ -1684,7 +1698,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createTeeLocal(tempLocal0.index, arg0), module.createTeeLocal(tempLocal0.index, arg0),
module.createF32(0) module.createF32(0)
), ),
module.createUnreachable(), abort,
module.createGetLocal(tempLocal0.index, NativeType.F32) module.createGetLocal(tempLocal0.index, NativeType.F32)
); );
break; break;
@ -1696,14 +1710,14 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
module.createTeeLocal(tempLocal0.index, arg0), module.createTeeLocal(tempLocal0.index, arg0),
module.createF64(0) module.createF64(0)
), ),
module.createUnreachable(), abort,
module.createGetLocal(tempLocal0.index, NativeType.F64) module.createGetLocal(tempLocal0.index, NativeType.F64)
); );
break; break;
case TypeKind.VOID: case TypeKind.VOID:
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range); compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
ret = module.createUnreachable(); ret = abort;
break; break;
} }
} }

View File

@ -2862,7 +2862,16 @@ export class Compiler extends DiagnosticEmitter {
return this.module.createI32(intValue.toI32()); return this.module.createI32(intValue.toI32());
case LiteralKind.STRING: case LiteralKind.STRING:
var stringValue = (<StringLiteralExpression>expression).value; return this.compileStaticString((<StringLiteralExpression>expression).value);
// case LiteralKind.OBJECT:
// case LiteralKind.REGEXP:
// case LiteralKind.STRING:
}
throw new Error("not implemented");
}
compileStaticString(stringValue: string): ExpressionRef {
var stringSegment: MemorySegment | null = this.stringSegments.get(stringValue); var stringSegment: MemorySegment | null = this.stringSegments.get(stringValue);
if (!stringSegment) { if (!stringSegment) {
var stringLength = stringValue.length; var stringLength = stringValue.length;
@ -2883,12 +2892,6 @@ export class Compiler extends DiagnosticEmitter {
return this.options.isWasm64 return this.options.isWasm64
? this.module.createI64(stringOffset.lo, stringOffset.hi) ? this.module.createI64(stringOffset.lo, stringOffset.hi)
: this.module.createI32(stringOffset.lo); : this.module.createI32(stringOffset.lo);
// case LiteralKind.OBJECT:
// case LiteralKind.REGEXP:
// case LiteralKind.STRING:
}
throw new Error("not implemented");
} }
compileNewExpression(expression: NewExpression, contextualType: Type): ExpressionRef { compileNewExpression(expression: NewExpression, contextualType: Type): ExpressionRef {

View File

@ -121,20 +121,12 @@ export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: b
sb.push(context); sb.push(context);
} }
sb.push("\n"); 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(" in ");
sb.push(range.source.path); sb.push(range.source.path);
sb.push("("); sb.push("(");
sb.push(line.toString(10)); sb.push(range.line.toString(10));
sb.push(","); sb.push(",");
sb.push(column.toString(10)); sb.push(range.column.toString(10));
sb.push(")"); sb.push(")");
} }
return sb.join(""); 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 atStart(): Range { return new Range(this.source, this.start, this.start); }
get atEnd(): Range { return new Range(this.source, this.end, this.end); } 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 { toString(): string {
return this.source.text.substring(this.start, this.end); return this.source.text.substring(this.start, this.end);
} }

View File

@ -1,12 +1,14 @@
const EMPTY: String = changetype<String>(""); const EMPTY: String = changetype<String>("");
const HEAD: usize = 4;
function allocate(length: i32): String { function allocate(length: i32): String {
var ptr = allocate_memory(4 + length * 2); assert(<u32>length >= 1);
var ptr = allocate_memory(HEAD + length * 2);
store<i32>(ptr, length); store<i32>(ptr, length);
return changetype<String>(ptr); return changetype<String>(ptr);
} }
@unmanaged
export class String { export class String {
readonly length: i32; readonly length: i32;
@ -16,23 +18,27 @@ export class String {
if (<u32>pos >= this.length) if (<u32>pos >= this.length)
return EMPTY; return EMPTY;
var out = allocate(1); var out = allocate(1);
store<u16>(changetype<usize>(out), load<u16>(changetype<usize>(this) + (<usize>pos << 1), 4), 4); store<u16>(
changetype<usize>(out),
load<u16>(changetype<usize>(this) + (<usize>pos << 1), HEAD),
HEAD
);
return out; return out;
} }
charCodeAt(pos: i32): i32 { charCodeAt(pos: i32): i32 {
if (<u32>pos >= this.length) if (<u32>pos >= this.length)
return -1; // NaN return -1; // NaN
return load<u16>(changetype<usize>(this) + (<usize>pos << 1), 4); return load<u16>(changetype<usize>(this) + (<usize>pos << 1), HEAD);
} }
codePointAt(pos: i32): i32 { codePointAt(pos: i32): i32 {
if (<u32>pos >= this.length) if (<u32>pos >= this.length)
return -1; // undefined return -1; // undefined
var first = <i32>load<u16>(changetype<usize>(this) + (<usize>pos << 1), 4); var first = <i32>load<u16>(changetype<usize>(this) + (<usize>pos << 1), HEAD);
if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length) if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length)
return first; return first;
var second = <i32>load<u16>(changetype<usize>(this) + ((<usize>pos + 1) << 1), 4); var second = <i32>load<u16>(changetype<usize>(this) + ((<usize>pos + 1) << 1), HEAD);
if (second < 0xDC00 || second > 0xDFFF) if (second < 0xDC00 || second > 0xDFFF)
return first; return first;
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000; return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
@ -45,9 +51,19 @@ export class String {
var thisLen: isize = this.length; var thisLen: isize = this.length;
var otherLen: isize = other.length; var otherLen: isize = other.length;
var len: usize = thisLen + otherLen; var len: usize = thisLen + otherLen;
if (len == 0)
return EMPTY;
var out = allocate(len); var out = allocate(len);
move_memory(changetype<usize>(out) + 4, changetype<usize>(this) + 4, thisLen << 1); move_memory(
move_memory(changetype<usize>(out) + 4 + (thisLen << 1), changetype<usize>(other) + 4, otherLen << 1); changetype<usize>(out) + HEAD,
changetype<usize>(this) + HEAD,
thisLen << 1
);
move_memory(
changetype<usize>(out) + HEAD + (thisLen << 1),
changetype<usize>(other) + HEAD,
otherLen << 1
);
return out; return out;
} }
@ -58,7 +74,11 @@ export class String {
var start: isize = end - searchLength; var start: isize = end - searchLength;
if (start < 0) if (start < 0)
return false; return false;
return !compare_memory(changetype<usize>(this) + 4 + (start << 1), changetype<usize>(searchString) + 4, searchLength << 1); return !compare_memory(
changetype<usize>(this) + HEAD + (start << 1),
changetype<usize>(searchString) + HEAD,
searchLength << 1
);
} }
@operator("==") @operator("==")
@ -69,7 +89,11 @@ export class String {
return false; return false;
if (this.length != other.length) if (this.length != other.length)
return false; return false;
return !compare_memory(changetype<usize>(this) + 4, changetype<usize>(other) + 4, <usize>(this.length << 1)); return !compare_memory(
changetype<usize>(this) + HEAD,
changetype<usize>(other) + HEAD,
<usize>(this.length << 1)
);
} }
includes(searchString: String, position: i32 = 0): bool { includes(searchString: String, position: i32 = 0): bool {
@ -83,7 +107,11 @@ export class String {
var start: isize = min<isize>(max<isize>(pos, 0), len); var start: isize = min<isize>(max<isize>(pos, 0), len);
var searchLen: isize = searchString.length; var searchLen: isize = searchString.length;
for (var k: usize = start; <isize>k + searchLen <= len; ++k) for (var k: usize = start; <isize>k + searchLen <= len; ++k)
if (!compare_memory(changetype<usize>(this) + 4 + (k << 1), changetype<usize>(searchString) + 4, searchLen << 1)) if (!compare_memory(
changetype<usize>(this) + HEAD + (k << 1),
changetype<usize>(searchString) + HEAD,
searchLen << 1)
)
return <i32>k; return <i32>k;
return -1; return -1;
} }
@ -97,7 +125,11 @@ export class String {
var searchLength: isize = searchString.length; var searchLength: isize = searchString.length;
if (searchLength + start > len) if (searchLength + start > len)
return false; return false;
return !compare_memory(changetype<usize>(this) + 4 + (start << 1), changetype<usize>(searchString) + 4, searchLength << 1); return !compare_memory(
changetype<usize>(this) + HEAD + (start << 1),
changetype<usize>(searchString) + HEAD,
searchLength << 1
);
} }
substr(start: i32, length: i32 = i32.MAX_VALUE): String { substr(start: i32, length: i32 = i32.MAX_VALUE): String {
@ -108,10 +140,14 @@ export class String {
if (intStart < 0) if (intStart < 0)
intStart = max<isize>(size + intStart, 0); intStart = max<isize>(size + intStart, 0);
var resultLength: isize = min<isize>(max<isize>(end, 0), size - intStart); var resultLength: isize = min<isize>(max<isize>(end, 0), size - intStart);
if (resultLength < 0) if (resultLength <= 0)
return EMPTY; return EMPTY;
var out = allocate(resultLength); var out = allocate(resultLength);
move_memory(changetype<usize>(out) + 4, changetype<usize>(this) + 4 + (intStart << 1), <usize>resultLength << 1); move_memory(
changetype<usize>(out) + HEAD,
changetype<usize>(this) + HEAD + (intStart << 1),
<usize>resultLength << 1
);
return out; return out;
} }
@ -128,17 +164,21 @@ export class String {
if (!from && to == this.length) if (!from && to == this.length)
return this; return this;
var out = allocate(len); var out = allocate(len);
move_memory(changetype<usize>(out) + 4, changetype<usize>(this) + 4 + (from << 1), len << 1); move_memory(
changetype<usize>(out) + HEAD,
changetype<usize>(this) + HEAD + (from << 1),
len << 1
);
return out; return out;
} }
trim(): String { trim(): String {
assert(this != null); assert(this != null);
var length: usize = this.length; var length: usize = this.length;
while (length && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (length << 1), 4))) while (length && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (length << 1), HEAD)))
--length; --length;
var start: usize = 0; var start: usize = 0;
while (start < length && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (start << 1), 4))) { while (start < length && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (start << 1), HEAD))) {
++start; --length; ++start; --length;
} }
if (!length) if (!length)
@ -146,7 +186,11 @@ export class String {
if (!start && length == this.length) if (!start && length == this.length)
return this; return this;
var out = allocate(length); var out = allocate(length);
move_memory(changetype<usize>(out) + 4, changetype<usize>(this) + 4 + (start << 1), length << 1); move_memory(
changetype<usize>(out) + HEAD,
changetype<usize>(this) + HEAD + (start << 1),
length << 1
);
return out; return out;
} }
@ -154,27 +198,37 @@ export class String {
assert(this != null); assert(this != null);
var start: isize = 0; var start: isize = 0;
var len: isize = this.length; var len: isize = this.length;
while (start < len && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (start << 1), 4))) while (start < len && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (start << 1), HEAD)))
++start; ++start;
if (!start) if (!start)
return this; return this;
var outLen = len - start; var outLen = len - start;
if (!outLen)
return EMPTY;
var out = allocate(outLen); var out = allocate(outLen);
move_memory(changetype<usize>(out) + 4, changetype<usize>(this) + 4 + (start << 1), outLen << 1); move_memory(
changetype<usize>(out) + HEAD,
changetype<usize>(this) + HEAD + (start << 1),
outLen << 1
);
return out; return out;
} }
trimRight(): String { trimRight(): String {
assert(this != null); assert(this != null);
var len: isize = this.length; var len: isize = this.length;
while (len > 0 && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (len << 1), 4))) while (len > 0 && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (len << 1), HEAD)))
--len; --len;
if (len <= 0) if (len <= 0)
return EMPTY; return EMPTY;
if (<i32>len == this.length) if (<i32>len == this.length)
return this; return this;
var out = allocate(len); var out = allocate(len);
move_memory(changetype<usize>(out) + 4, changetype<usize>(this) + 4, len << 1); move_memory(
changetype<usize>(out) + HEAD,
changetype<usize>(this) + HEAD,
len << 1
);
return out; return out;
} }
} }

View File

@ -0,0 +1,11 @@
var abortCalled = false;
@global
function abort(message: string | null, filename: string, line: i32, column: i32): void {
abortCalled = true;
}
assert(false, "this is ok");
if (!abortCalled)
unreachable();