Skip to content

Commit

Permalink
Warn on constant locals not being actual constants; Simplify changetype
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 28, 2017
1 parent 2f12c7f commit 4207f64
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 74 deletions.
2 changes: 1 addition & 1 deletion bin/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var assemblyscript;
var isDev = true;
try {
assemblyscript = require("../dist/assemblyscript.js");
require("source-map-support").install();
try { require("source-map-support").install(); } catch (e) {} // optional
isDev = false;
} catch (e) {
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"dependencies": {
"binaryen": "40.0.0-nightly.20171209",
"glob": "^7.1.2",
"minimist": "^1.2.0",
"source-map-support": "^0.5.0"
"minimist": "^1.2.0"
},
"devDependencies": {
"@types/chalk": "^2.2.0",
Expand All @@ -26,6 +25,7 @@
"chalk": "^2.3.0",
"diff": "^3.4.0",
"long": "^3.2.0",
"source-map-support": "^0.5.0",
"ts-loader": "^3.2.0",
"ts-node": "^4.0.2",
"tslint": "^5.8.0",
Expand All @@ -42,15 +42,15 @@
},
"scripts": {
"build": "webpack",
"clean": "rm dist/assemblyscript.*",
"clean": "node scripts/clean",
"lint": "tslint --project src",
"test:config": "npm run test:config:assembly --scripts-prepend-node-path && npm run test:config:portable --scripts-prepend-node-path && npm run test:config:src --scripts-prepend-node-path",
"test:config:assembly": "tsc --noEmit -p std/assembly --diagnostics --listFiles",
"test:config:portable": "tsc --noEmit -p std/portable --diagnostics --listFiles",
"test:config:src": "tsc --noEmit -p src --diagnostics --listFiles",
"test:parser": "node tests/parser",
"test:compiler": "node tests/compiler",
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path",
"lint": "tslint --project src"
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path"
},
"files": [
"bin/",
Expand Down
16 changes: 16 additions & 0 deletions scripts/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var fs = require("fs");
var glob = require("glob");

glob("*", { cwd: __dirname + "/../dist" }, (err, matches) => {
if (err)
console.log("Failed to list files in 'dist/': " + err.message);
else
matches.forEach(match => {
fs.unlink(__dirname + "/../dist/" + match, err => {
if (err)
console.log("Failed to delete 'dist/" + match + "': " + err.message);
else
console.log("Deleted 'dist/" + match + "'");
});
});
});
13 changes: 6 additions & 7 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2185,15 +2185,14 @@ function builderEndsWith(sb: string[], code: CharCode): bool {
export function escapeString(str: string): string {
var k = str.length;
var ret = new Array<string>(k);
ret.length = 0;
for (var i = 0, c: string; i < k; ++i) {
switch (c = str.charAt(i)) {
case "\\": ret.push("\\\\"); break;
case "\"": ret.push("\\\""); break;
case "\r": ret.push("\\r"); break;
case "\n": ret.push("\\n"); break;
case "\0": ret.push("\\0"); break;
default: ret.push(c);
case "\\": ret[i] = "\\\\"; break;
case "\"": ret[i] = "\\\""; break;
case "\r": ret[i] = "\\r"; break;
case "\n": ret[i] = "\\n"; break;
case "\0": ret[i] = "\\0"; break;
default: ret[i] = c;
}
}
return "\"" + ret.join("") + "\"";
Expand Down
8 changes: 4 additions & 4 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,11 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty

// other

case "changetype": // changetype<T1,T2>(value: T1) -> T2
if (!validateCall(compiler, typeArguments, 2, operands, 1, reportNode))
case "changetype": // changetype<T>(value: *) -> T
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
return module.createUnreachable();
if ((typeArguments[0] == usizeType && typeArguments[1].classType) || (typeArguments[0].classType && typeArguments[1] == usizeType)) {
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
arg0 = compiler.compileExpression(operands[0], Type.void, ConversionKind.NONE);
if ((compiler.currentType == usizeType && typeArguments[1].classType) || (compiler.currentType.classType && typeArguments[1] == usizeType)) {
compiler.currentType = typeArguments[1];
return arg0;
}
Expand Down
9 changes: 5 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class Compiler extends DiagnosticEmitter {
if (!global.isMutable) {
initExpr = this.precomputeExpressionRef(initExpr);
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
initializeInStart = true;
}
} else
Expand Down Expand Up @@ -461,7 +461,7 @@ export class Compiler extends DiagnosticEmitter {
initExpr = this.precomputeExpressionRef(initExpr);
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
if (element.isConstant)
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
initInStart = true;
}
}
Expand All @@ -476,7 +476,7 @@ export class Compiler extends DiagnosticEmitter {
this.module.createI32(1)
);
if (element.isConstant)
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
initInStart = true;
}
if (initInStart) {
Expand Down Expand Up @@ -1029,7 +1029,8 @@ export class Compiler extends DiagnosticEmitter {
}
this.currentFunction.locals.set(name, local);
continue;
}
} else
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
} else {
this.error(DiagnosticCode._const_declarations_must_be_initialized, declaration.range);
}
Expand Down
4 changes: 2 additions & 2 deletions src/diagnosticMessages.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export enum DiagnosticCode {
Operation_not_supported = 102,
Operation_is_unsafe = 103,
Cannot_export_a_mutable_global = 104,
Compiling_constant_global_with_non_constant_initializer_as_mutable = 105,
Compiling_constant_with_non_constant_initializer_as_mutable = 105,
Type_0_cannot_be_changed_to_type_1 = 106,
Unterminated_string_literal = 1002,
Identifier_expected = 1003,
Expand Down Expand Up @@ -87,7 +87,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
case 102: return "Operation not supported.";
case 103: return "Operation is unsafe.";
case 104: return "Cannot export a mutable global.";
case 105: return "Compiling constant global with non-constant initializer as mutable.";
case 105: return "Compiling constant with non-constant initializer as mutable.";
case 106: return "Type '{0}' cannot be changed to type '{1}'.";
case 1002: return "Unterminated string literal.";
case 1003: return "Identifier expected.";
Expand Down
2 changes: 1 addition & 1 deletion src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Operation not supported.": 102,
"Operation is unsafe.": 103,
"Cannot export a mutable global.": 104,
"Compiling constant global with non-constant initializer as mutable.": 105,
"Compiling constant with non-constant initializer as mutable.": 105,
"Type '{0}' cannot be changed to type '{1}'.": 106,

"Unterminated string literal.": 1002,
Expand Down
10 changes: 5 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class Module {
static create(): Module {
var module = new Module();
module.ref = _BinaryenModuleCreate();
module.lit = changetype<usize,BinaryenLiteral>(Heap.allocate(16));
module.lit = changetype<BinaryenLiteral>(Heap.allocate(16));
module.noEmit = false;
return module;
}
Expand All @@ -274,18 +274,18 @@ export class Module {
try {
var module = new Module();
module.ref = _BinaryenModuleRead(cArr, buffer.length);
module.lit = changetype<usize,BinaryenLiteral>(Heap.allocate(16));
module.lit = changetype<BinaryenLiteral>(Heap.allocate(16));
module.noEmit = false;
return module;
} finally {
Heap.dispose(changetype<usize,usize>(cArr));
Heap.dispose(changetype<usize>(cArr));
}
}

static createStub(): Module {
var module = new Module();
module.ref = 0;
module.lit = changetype<usize,BinaryenLiteral>(0);
module.lit = changetype<BinaryenLiteral>(0);
module.noEmit = true;
return module;
}
Expand Down Expand Up @@ -806,7 +806,7 @@ export class Module {
dispose(): void {
if (!this.ref) return; // sic
_BinaryenModuleDispose(this.ref);
Heap.dispose(changetype<BinaryenLiteral, usize>(this.lit));
Heap.dispose(changetype<usize>(this.lit));
}

createRelooper(): Relooper {
Expand Down
2 changes: 1 addition & 1 deletion std/assembly.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ declare const HEAP_BASE: usize;
/** Determines the byte size of the specified core or class type. Compiles to a constant. */
declare function sizeof<T>(): usize;
/** Changes the type of a value to another one. Useful for casting class instances to their pointer values and vice-versa. */
declare function changetype<T1,T2>(value: T1): T2;
declare function changetype<T>(value: any): T;
/** Tests if a 32-bit or 64-bit float is `NaN`. */
declare function isNaN<T = f32 | f64>(value: T): bool;
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */
Expand Down
4 changes: 2 additions & 2 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export class Array<T> {
}

dispose(): void {
store<i64>(changetype<this,usize>(this), 0);
store<i64>(changetype<usize>(this), 0);
Heap.dispose(this.ptr);
this.ptr = 0;
Heap.dispose(changetype<this,usize>(this));
Heap.dispose(changetype<usize>(this));
}

// TODO
Expand Down
18 changes: 9 additions & 9 deletions std/assembly/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const ALIGN_LOG2: usize = 3;
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;
const ALIGN_MASK: usize = ALIGN_SIZE - 1;

let HEAP_OFFSET: usize = HEAP_BASE; // HEAP_BASE is a constant generated by the compiler
var HEAP_OFFSET: usize = HEAP_BASE; // HEAP_BASE is a constant generated by the compiler

// TODO: maybe tlsf

Expand All @@ -15,11 +15,11 @@ export class Heap {

static allocate(size: usize): usize {
if (!size) return 0;
const len: i32 = current_memory();
var len: i32 = current_memory();
if (HEAP_OFFSET + size > <usize>len << 16)
if(grow_memory(max<i32>(<i32>ceil<f64>(<f64>size / 65536), len * 2 - len)) < 0)
unreachable();
const ptr: usize = HEAP_OFFSET;
var ptr: usize = HEAP_OFFSET;
if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
return ptr;
Expand All @@ -33,8 +33,8 @@ export class Heap {
assert(dest >= HEAP_BASE);

// the following is based on musl's implementation of memcpy
let dst: usize = dest;
let w: u32, x: u32;
var dst: usize = dest;
var w: u32, x: u32;

// copy 1 byte each until src is aligned to 4 bytes
while (n && src % 4) {
Expand Down Expand Up @@ -180,7 +180,7 @@ export class Heap {
// the following is based on musl's implementation of memset
if (!n) return dest;

let s: usize = dest;
var s: usize = dest;

// Fill head and tail with minimal branching
store<u8>(s, c); store<u8>(s + n - 1, c);
Expand All @@ -192,12 +192,12 @@ export class Heap {
if (n <= 8) return dest;

// Align to 4 bytes
let k: usize = -s & 3;
var k: usize = -s & 3;
s += k;
n -= k;
n &= -4;

let c32: u32 = -1 / 255 * c;
var c32: u32 = -1 / 255 * c;

// Fill head and tail in preparation of setting 32 bytes at a time
store<u32>(s, c32);
Expand All @@ -223,7 +223,7 @@ export class Heap {
n -= k;

// Set 32 bytes at a time
let c64: u64 = <u64>c32 | (<u64>c32 << 32);
var c64: u64 = <u64>c32 | (<u64>c32 << 32);
while (n >= 32) {
store<u64>(s, c64);
store<u64>(s + 8, c64);
Expand Down
Loading

0 comments on commit 4207f64

Please sign in to comment.