Skip to content

Commit ad92d91

Browse files
committed
Move built-in declarations to actual sources; Remove declaration is null checks; Resolve calls
1 parent a7e815d commit ad92d91

24 files changed

+645
-492
lines changed

bin/asc.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function main(argv, options, callback) {
4848
if (!callback) callback = function defaultCallback(err) {
4949
var code = 0;
5050
if (err) {
51-
stderr.write(err + os.EOL);
51+
stderr.write(err.stack + os.EOL);
5252
code = 1;
5353
}
5454
return code;
@@ -195,7 +195,11 @@ function main(argv, options, callback) {
195195

196196
var module;
197197
stats.compileCount++;
198-
stats.compileTime += measure(() => module = assemblyscript.compile(parser, compilerOptions));
198+
try {
199+
stats.compileTime += measure(() => module = assemblyscript.compile(parser, compilerOptions));
200+
} catch (e) {
201+
return callback(e);
202+
}
199203
if (checkDiagnostics(parser, stderr)) {
200204
if (module) module.dispose();
201205
return callback(Error("Compile error"));

examples/tlsf/assembly/tlsf.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,7 @@ class Root {
383383
// merge with current tail if adjacent
384384
if (start - Block.INFO == tailRef) {
385385
start -= Block.INFO;
386-
let tail = changetype<Block>(tailRef);
387-
tailInfo = tail.info;
386+
tailInfo = changetype<Block>(tailRef).info;
388387
}
389388

390389
} else
@@ -407,7 +406,7 @@ class Root {
407406
tail.info = 0 | LEFT_FREE;
408407
this.tailRef = changetype<usize>(tail);
409408

410-
this.insert(left); // also sets jump
409+
this.insert(left); // also merges with free left before tail / sets jump
411410

412411
return true;
413412
}

examples/tlsf/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fs = require("fs");
2+
module.exports = new WebAssembly.Instance(
3+
new WebAssembly.Module(fs.readFileSync(__dirname + "/tlsf.optimized.wasm")),
4+
{ env: { abort: function() { throw Error("abort called"); } } }
5+
).exports;

examples/tlsf/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"build": "npm run build:untouched && npm run build:optimized",
77
"build:untouched": "asc assembly/tlsf.ts -t tlsf.untouched.wast -b tlsf.untouched.wasm --validate --sourceMap --measure",
8-
"build:optimized": "asc -O3 assembly/tlsf.ts -b tlsf.optimized.wasm -t tlsf.optimized.wast --validate --noDebug --noAssert --sourceMap --measure",
8+
"build:optimized": "asc assembly/tlsf.ts -t tlsf.optimized.wast -b tlsf.optimized.wasm --validate --sourceMap --measure --noDebug --noAssert --optimize",
99
"test": "node tests",
1010
"test:forever": "node tests/forever"
1111
}

examples/tlsf/tests/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function test(file) {
2020
return String.fromCharCode.apply(String, str);
2121
}
2222

23-
runner(exports, 5, 20000); // picked so I/O isn't the bottleneck
23+
runner(exports, 20, 20000); // picked so I/O isn't the bottleneck
2424
console.log("mem final: " + exports.memory.buffer.byteLength);
2525
console.log();
2626
}

src/ast.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,15 @@ export abstract class Node {
560560
return elem;
561561
}
562562

563-
static createParameter(identifier: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, isRest: bool, range: Range): Parameter {
563+
static createParameter(identifier: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, kind: ParameterKind, range: Range): Parameter {
564564
var elem = new Parameter();
565565
elem.range = range;
566566
(elem.name = identifier).parent = elem;
567567
if (elem.type = type)
568568
(<TypeNode>type).parent = elem;
569569
if (elem.initializer = initializer)
570570
(<Expression>initializer).parent = elem;
571-
elem.isRest = isRest;
571+
elem.parameterKind = kind;
572572
return elem;
573573
}
574574

@@ -1050,7 +1050,7 @@ export class Source extends Node {
10501050
/** Tests if this source is an entry file. */
10511051
get isEntry(): bool { return this.sourceKind == SourceKind.ENTRY; }
10521052
/** Tests if this source is a stdlib file. */
1053-
get isStdlib(): bool { return this.sourceKind == SourceKind.LIBRARY; }
1053+
get isLibrary(): bool { return this.sourceKind == SourceKind.LIBRARY; }
10541054
}
10551055

10561056
/** Base class of all declaration statements. */
@@ -1352,18 +1352,28 @@ export class NamespaceDeclaration extends DeclarationStatement {
13521352
members: Statement[];
13531353
}
13541354

1355+
/** Represents the kind of a parameter. */
1356+
export enum ParameterKind {
1357+
/** No specific flags. */
1358+
DEFAULT,
1359+
/** Is an optional parameter. */
1360+
OPTIONAL,
1361+
/** Is a rest parameter. */
1362+
REST
1363+
}
1364+
13551365
/** Represents a function parameter. */
13561366
export class Parameter extends Node {
13571367
kind = NodeKind.PARAMETER;
13581368

13591369
/** Parameter name. */
1360-
name: IdentifierExpression | null;
1370+
name: IdentifierExpression;
13611371
/** Parameter type. */
13621372
type: TypeNode | null;
1373+
/** Parameter kind. */
1374+
parameterKind: ParameterKind;
13631375
/** Initializer expression, if present. */
13641376
initializer: Expression | null;
1365-
/** Whether a rest parameter or not. */
1366-
isRest: bool;
13671377
}
13681378

13691379
/** Represents a single modifier. */

src/builtins.ts

Lines changed: 2 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -46,147 +46,6 @@ import {
4646
ElementKind
4747
} from "./program";
4848

49-
/** Initializes the specified program with built-in constants and functions. */
50-
export function initialize(program: Program): void {
51-
52-
// math
53-
addConstant(program, "NaN", Type.f64);
54-
addConstant(program, "Infinity", Type.f64);
55-
56-
addFunction(program, "isNaN", true);
57-
addFunction(program, "isFinite", true);
58-
addFunction(program, "clz", true);
59-
addFunction(program, "ctz", true);
60-
addFunction(program, "popcnt", true);
61-
addFunction(program, "rotl", true);
62-
addFunction(program, "rotr", true);
63-
addFunction(program, "abs", true);
64-
addFunction(program, "max", true);
65-
addFunction(program, "min", true);
66-
addFunction(program, "ceil", true);
67-
addFunction(program, "floor", true);
68-
addFunction(program, "copysign", true);
69-
addFunction(program, "nearest", true);
70-
addFunction(program, "reinterpret", true);
71-
addFunction(program, "sqrt", true);
72-
addFunction(program, "trunc", true);
73-
74-
// memory access
75-
addFunction(program, "load", true);
76-
addFunction(program, "store", true);
77-
addFunction(program, "sizeof", true);
78-
79-
// control flow
80-
addFunction(program, "select", true);
81-
addFunction(program, "unreachable");
82-
83-
// host operations
84-
addFunction(program, "current_memory");
85-
addFunction(program, "grow_memory");
86-
// addFunction(program, "move_memory");
87-
// addFunction(program, "set_memory");
88-
89-
// other
90-
addFunction(program, "changetype", true);
91-
addFunction(program, "assert");
92-
93-
// abort is special in that it is imported conditionally. for example, when
94-
// compiling with noAssert=true, it isn't necessary that it is present, that
95-
// is if a user doesn't call it manually.
96-
var abortPrototype = addFunction(program, "abort");
97-
abortPrototype.set(ElementFlags.DECLARED);
98-
abortPrototype.instances.set("", new Function(abortPrototype, "abort", null, [
99-
new Parameter(null, program.options.usizeType), // message (string)
100-
new Parameter(null, program.options.usizeType), // file name (string)
101-
new Parameter(null, Type.u32), // line number
102-
new Parameter(null, Type.u32) // column number
103-
], Type.void, null));
104-
105-
// conversions and limits
106-
var i32Func: FunctionPrototype,
107-
u32Func: FunctionPrototype,
108-
i64Func: FunctionPrototype,
109-
u64Func: FunctionPrototype;
110-
addFunction(program, "i8").members = new Map([
111-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i8.MIN_VALUE", null, Type.i8).withConstantIntegerValue(-128, -1) ],
112-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i8.MAX_VALUE", null, Type.i8).withConstantIntegerValue(127, 0) ]
113-
]);
114-
addFunction(program, "i16").members = new Map([
115-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i16.MIN_VALUE", null, Type.i16).withConstantIntegerValue(-32768, -1) ],
116-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i16.MAX_VALUE", null, Type.i16).withConstantIntegerValue(32767, 0) ]
117-
]);
118-
(i32Func = addFunction(program, "i32")).members = new Map([
119-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i32.MIN_VALUE", null, Type.i32).withConstantIntegerValue(-2147483648, -1) ],
120-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i32.MAX_VALUE", null, Type.i32).withConstantIntegerValue(2147483647, 0) ]
121-
]);
122-
(i64Func = addFunction(program, "i64")).members = new Map([
123-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i64.MIN_VALUE", null, Type.i64).withConstantIntegerValue(0, -2147483648) ],
124-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, 2147483647) ]
125-
]);
126-
addFunction(program, "u8").members = new Map([
127-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u8.MIN_VALUE", null, Type.u8).withConstantIntegerValue(0, 0) ],
128-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u8.MAX_VALUE", null, Type.u8).withConstantIntegerValue(255, 0) ]
129-
]);
130-
addFunction(program, "u16").members = new Map([
131-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u16.MIN_VALUE", null, Type.u16).withConstantIntegerValue(0, 0) ],
132-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u16.MAX_VALUE", null, Type.u16).withConstantIntegerValue(65535, 0) ]
133-
]);
134-
(u32Func = addFunction(program, "u32")).members = new Map([
135-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u32.MIN_VALUE", null, Type.u32).withConstantIntegerValue(0, 0) ],
136-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u32.MAX_VALUE", null, Type.u32).withConstantIntegerValue(-1, 0) ]
137-
]);
138-
(u64Func = addFunction(program, "u64")).members = new Map([
139-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u64.MIN_VALUE", null, Type.u64).withConstantIntegerValue(0, 0) ],
140-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, -1) ]
141-
]);
142-
addFunction(program, "bool").members = new Map([
143-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "bool.MIN_VALUE", null, Type.bool).withConstantIntegerValue(0, 0) ],
144-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "bool.MAX_VALUE", null, Type.bool).withConstantIntegerValue(1, 0) ]
145-
]);
146-
addFunction(program, "f32").members = new Map([
147-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "f32.MIN_VALUE", null, Type.f32).withConstantFloatValue(-3.40282347e+38) ],
148-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "f32.MAX_VALUE", null, Type.f32).withConstantFloatValue(3.40282347e+38) ],
149-
[ "MIN_SAFE_INTEGER", new Global(program, "MIN_SAFE_INTEGER", "f32.MIN_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(-16777215) ],
150-
[ "MAX_SAFE_INTEGER", new Global(program, "MAX_SAFE_INTEGER", "f32.MAX_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(16777215) ],
151-
[ "EPSILON", new Global(program, "EPSILON", "f32.EPSILON", null, Type.f32).withConstantFloatValue(1.19209290e-07) ]
152-
]);
153-
addFunction(program, "f64").members = new Map([
154-
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "f64.MIN_VALUE", null, Type.f64).withConstantFloatValue(-1.7976931348623157e+308) ],
155-
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "f64.MAX_VALUE", null, Type.f64).withConstantFloatValue(1.7976931348623157e+308) ],
156-
[ "MIN_SAFE_INTEGER", new Global(program, "MIN_SAFE_INTEGER", "f64.MIN_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(-9007199254740991) ],
157-
[ "MAX_SAFE_INTEGER", new Global(program, "MAX_SAFE_INTEGER", "f64.MAX_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(9007199254740991) ],
158-
[ "EPSILON", new Global(program, "EPSILON", "f64.EPSILON", null, Type.f64).withConstantFloatValue(2.2204460492503131e-16) ]
159-
]);
160-
if (program.options.isWasm64) {
161-
program.elements.set("isize", i64Func);
162-
program.elements.set("usize", u64Func);
163-
addConstant(program, "HEAP_BASE", Type.usize64);
164-
} else {
165-
program.elements.set("isize", i32Func);
166-
program.elements.set("usize", u32Func);
167-
addConstant(program, "HEAP_BASE", Type.usize32);
168-
}
169-
}
170-
171-
/** Adds a built-in constant to the specified program. */
172-
function addConstant(program: Program, name: string, type: Type): Global {
173-
var global = new Global(program, name, name, null, type);
174-
global.set(ElementFlags.BUILTIN);
175-
global.set(ElementFlags.CONSTANT);
176-
program.elements.set(name, global);
177-
return global;
178-
}
179-
180-
/** Adds a built-in function to the specified program. */
181-
function addFunction(program: Program, name: string, isGeneric: bool = false): FunctionPrototype {
182-
var prototype = new FunctionPrototype(program, name, name, null, null);
183-
prototype.set(ElementFlags.BUILTIN);
184-
if (isGeneric)
185-
prototype.set(ElementFlags.GENERIC);
186-
program.elements.set(name, prototype);
187-
return prototype;
188-
}
189-
19049
/** Compiles a get of a built-in global. */
19150
export function compileGetConstant(compiler: Compiler, global: Global, reportNode: Node): ExpressionRef {
19251
switch (global.internalName) { // switching on strings should become a compiler optimization eventually
@@ -204,7 +63,8 @@ export function compileGetConstant(compiler: Compiler, global: Global, reportNod
20463
return compiler.module.createF64(Infinity);
20564

20665
case "HEAP_BASE": // constant, but never inlined
207-
return compiler.module.createGetGlobal("HEAP_BASE", (compiler.currentType = <Type>global.type).toNativeType());
66+
compiler.currentType = compiler.options.usizeType;
67+
return compiler.module.createGetGlobal("HEAP_BASE", compiler.options.nativeSizeType);
20868
}
20969
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
21070
return compiler.module.createUnreachable();

0 commit comments

Comments
 (0)