Moved noEmit to compiler frontend; Added I/O and compile time measuring

This commit is contained in:
dcodeIO 2018-01-18 15:45:34 +01:00
parent 27a66c1a7a
commit 1c4b0ddc57
4 changed files with 174 additions and 169 deletions

View File

@ -63,7 +63,7 @@ if (args.help || args._.length < 1) {
}); });
console.log([ console.log([
"Version " + version, "Version " + version,
"Syntax: asc [options] [entryFile ...]", "Syntax: asc [entryFile ...] [options]",
"", "",
"Examples: asc hello.ts", "Examples: asc hello.ts",
" asc hello.ts -b hello.wasm -t hello.wast", " asc hello.ts -b hello.wasm -t hello.wast",
@ -76,6 +76,21 @@ if (args.help || args._.length < 1) {
var parser = null; var parser = null;
var readTime = 0;
var readCount = 0;
var writeTime = 0;
var parseTime = 0;
var compileTime = 0;
var validateTime = 0;
var optimizeTime = 0;
function measure(fn) {
var start = process.hrtime();
fn();
var times = process.hrtime(start);
return times[0] * 1e9 + times[1];
}
function checkDiagnostics(parser) { function checkDiagnostics(parser) {
var diagnostic; var diagnostic;
var hasErrors = false; var hasErrors = false;
@ -92,10 +107,18 @@ function checkDiagnostics(parser) {
// Include standard library // Include standard library
if (!args.noLib) { if (!args.noLib) {
var stdlibDir = path.join(__dirname, "..", "std", "assembly"); var stdlibDir = path.join(__dirname, "..", "std", "assembly");
glob.sync("*.ts", { cwd: stdlibDir }).forEach(file => { var notIoTime = 0;
var nextText = fs.readFileSync(path.join(stdlibDir, file), { encoding: "utf8" }); readTime += measure(() => {
parser = assemblyscript.parseFile(nextText, "std:" + file, parser, false); glob.sync("*.ts", { cwd: stdlibDir }).forEach(file => {
}); var nextText = fs.readFileSync(path.join(stdlibDir, file), { encoding: "utf8" });
++readCount;
var time = measure(() => {
parser = assemblyscript.parseFile(nextText, "std:" + file, parser, false);
});
parseTime += time;
notIoTime += time;
});
}) - notIoTime;
} }
// Include entry files // Include entry files
@ -105,10 +128,16 @@ args._.forEach(filename => {
var entryText; var entryText;
try { try {
entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" }); readTime += measure(() => {
entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" });
});
++readCount;
} catch (e) { } catch (e) {
try { try {
entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" }); readTime += measure(() => {
entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" });
});
++readCount;
entryPath = entryPath + "/index"; entryPath = entryPath + "/index";
} catch (e) { } catch (e) {
console.error("File '" + entryPath + ".ts' not found."); console.error("File '" + entryPath + ".ts' not found.");
@ -120,21 +149,31 @@ args._.forEach(filename => {
var nextText; var nextText;
// Load entry text // Load entry text
parser = assemblyscript.parseFile(entryText, entryPath, parser, true); parseTime += measure(() => {
parser = assemblyscript.parseFile(entryText, entryPath, parser, true);
});
while ((nextPath = parser.nextFile()) != null) { while ((nextPath = parser.nextFile()) != null) {
try { try {
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" }); readTime += measure(() => {
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
});
++readCount;
} catch (e) { } catch (e) {
try { try {
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" }); readTime += measure(() => {
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
});
++readCount;
nextPath = nextPath + "/index"; nextPath = nextPath + "/index";
} catch (e) { } catch (e) {
console.error("Imported file '" + nextPath + ".ts' not found."); console.error("Imported file '" + nextPath + ".ts' not found.");
process.exit(1); process.exit(1);
} }
} }
assemblyscript.parseFile(nextText, nextPath, parser); parseTime += measure(() => {
assemblyscript.parseFile(nextText, nextPath, parser);
});
} }
checkDiagnostics(parser); checkDiagnostics(parser);
}); });
@ -144,21 +183,29 @@ assemblyscript.setTarget(options, 0);
assemblyscript.setNoTreeShaking(options, args.noTreeShaking); assemblyscript.setNoTreeShaking(options, args.noTreeShaking);
assemblyscript.setNoAssert(options, args.noAssert); assemblyscript.setNoAssert(options, args.noAssert);
assemblyscript.setNoMemory(options, args.noMemory); assemblyscript.setNoMemory(options, args.noMemory);
// TODO: noDebug binaryen feature, removing names the debug section
var module = assemblyscript.compile(parser, options); var module;
compileTime += measure(() => {
module = assemblyscript.compile(parser, options);
});
checkDiagnostics(parser); checkDiagnostics(parser);
if (args.validate) if (args.validate)
if (!module.validate()) { validateTime += measure(() => {
module.dispose(); if (!module.validate()) {
process.exit(1); module.dispose();
} process.exit(1);
}
});
if (args.trapMode === "clamp") if (args.trapMode === "clamp")
module.runPasses([ "trap-mode-clamp" ]); optimizeTime += measure(() => {
module.runPasses([ "trap-mode-clamp" ]);
});
else if (args.trapMode === "js") else if (args.trapMode === "js")
module.runPasses([ "trap-mode-js" ]); optimizeTime += measure(() => {
module.runPasses([ "trap-mode-js" ]);
});
else if (args.trapMode !== "allow") { else if (args.trapMode !== "allow") {
console.log("Unsupported trap mode: " + args.trapMode); console.log("Unsupported trap mode: " + args.trapMode);
process.exit(1); process.exit(1);
@ -200,10 +247,6 @@ if (typeof args.optimizeLevel === "number")
if (typeof args.shrinkLevel === "number") if (typeof args.shrinkLevel === "number")
shrinkLevel = args.shrinkLevel; shrinkLevel = args.shrinkLevel;
// Workaround for inlining not being performed (42.0.0)
// if ((optimizeLevel >= 2 || shrinkLevel >= 2) && !debugInfo)
// runPasses = [ "inlining", "inlining-optimizing" ];
// Check additional passes // Check additional passes
if (args.runPasses) { if (args.runPasses) {
if (typeof args.runPasses === "string") if (typeof args.runPasses === "string")
@ -220,39 +263,67 @@ module.setShrinkLevel(shrinkLevel);
module.setDebugInfo(debugInfo); module.setDebugInfo(debugInfo);
if (optimizeLevel >= 0) if (optimizeLevel >= 0)
module.optimize(); optimizeTime += measure(() => {
module.optimize();
});
if (runPasses.length) if (runPasses.length)
module.runPasses(runPasses.map(pass => pass.trim())); optimizeTime += measure(() => {
module.runPasses(runPasses.map(pass => pass.trim()));
});
var hasOutput = false; if (!args.noEmit) {
var hasOutput = false;
if (args.outFile != null) { if (args.outFile != null) {
if (/\.wast$/.test(args.outFile) && args.textFile == null) if (/\.wast$/.test(args.outFile) && args.textFile == null)
args.textFile = args.outFile; args.textFile = args.outFile;
else if (/\.js$/.test(args.outFile) && args.asmjsFile == null) else if (/\.js$/.test(args.outFile) && args.asmjsFile == null)
args.asmjsFile = args.outFile; args.asmjsFile = args.outFile;
else if (args.binaryFile == null) else if (args.binaryFile == null)
args.binaryFile = args.outFile; args.binaryFile = args.outFile;
} }
if (args.binaryFile != null && args.binaryFile.length) { if (args.binaryFile != null && args.binaryFile.length) {
fs.writeFileSync(args.binaryFile, module.toBinary()); writeTime += measure(() => {
hasOutput = true; fs.writeFileSync(args.binaryFile, module.toBinary());
} });
if (args.textFile != null && args.textFile.length) { hasOutput = true;
fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" }); }
hasOutput = true; if (args.textFile != null && args.textFile.length) {
} writeTime += measure(() => {
if (args.asmjsFile != null && args.asmjsFile.length) { fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" });
fs.writeFileSync(args.asmjsFile, module.toAsmjs(), { encoding: "utf8" }); });
hasOutput = true; hasOutput = true;
} }
if (!hasOutput) { if (args.asmjsFile != null && args.asmjsFile.length) {
if (args.binaryFile === "") writeTime += measure(() => {
process.stdout.write(Buffer.from(module.toBinary())); fs.writeFileSync(args.asmjsFile, module.toAsmjs(), { encoding: "utf8" });
else if (args.asmjsFile === "") });
module.printAsmjs(); hasOutput = true;
else }
module.print(); if (!hasOutput) {
if (args.binaryFile === "")
writeTime += measure(() => {
process.stdout.write(Buffer.from(module.toBinary()));
});
else if (args.asmjsFile === "")
writeTime += measure(() => {
module.printAsmjs();
});
else
writeTime += measure(() => {
module.print();
});
}
} }
module.dispose(); module.dispose();
if (args.measure)
console.error([
"I/O Read : " + (readTime ? (readTime / 1e6).toFixed(3) + " ms (" + readCount + " files)" : "N/A"),
"I/O Write : " + (writeTime ? (writeTime / 1e6).toFixed(3) + " ms" : "N/A"),
"Parse : " + (parseTime ? (parseTime / 1e6).toFixed(3) + " ms" : "N/A"),
"Compile : " + (compileTime ? (compileTime / 1e6).toFixed(3) + " ms" : "N/A"),
"Validate : " + (validateTime ? (validateTime / 1e6).toFixed(3) + " ms" : "N/A"),
"Optimize : " + (optimizeTime ? (optimizeTime / 1e6).toFixed(3) + " ms" : "N/A")
].join("\n"));

View File

@ -13,11 +13,11 @@
"desc": [ "desc": [
"Optimizes the module. Also accepts the optimize level:", "Optimizes the module. Also accepts the optimize level:",
"", "",
" -O Defaults: Equivalent to -O2s", " -O Uses defaults. Equivalent to -O2s",
" -O0 Equivalent to --optimizeLevel 0", " -O0 Equivalent to --optimizeLevel 0",
" -O1 Equivalent to --optimizeLevel 1", " -O1 Equivalent to --optimizeLevel 1",
" -O2 Equivalent to --optimizeLevel 2", " -O2 Equivalent to --optimizeLevel 2",
" -O2 Equivalent to --optimizeLevel 3", " -O3 Equivalent to --optimizeLevel 3",
" -Oz Equivalent to -O but with --shrinkLevel 2", " -Oz Equivalent to -O but with --shrinkLevel 2",
" -O3s Equivalent to -O3 with --shrinkLevel 1 etc.", " -O3s Equivalent to -O3 with --shrinkLevel 1 etc.",
"" ""
@ -39,7 +39,7 @@
"aliases": [ "c", "check" ] "aliases": [ "c", "check" ]
}, },
"outFile": { "outFile": {
"desc": "Specifies the output file. Format is determined by file extension.", "desc": "Specifies the output file. File extension indicates format.",
"type": "string", "type": "string",
"aliases": [ "o" ] "aliases": [ "o" ]
}, },
@ -59,15 +59,19 @@
"aliases": [ "a" ] "aliases": [ "a" ]
}, },
"noTreeShaking": { "noTreeShaking": {
"desc": "Disables tree-shaking.", "desc": "Disables compiler-level tree-shaking.",
"type": "boolean" "type": "boolean"
}, },
"noDebug": { "noDebug": {
"desc": "Disables debug information in binaries.", "desc": "Disables maintaining debug information in binaries.",
"type": "boolean" "type": "boolean"
}, },
"noAssert": { "noAssert": {
"desc": "Disables assertions.", "desc": "Replaces assertions with NOPs.",
"type": "boolean"
},
"noEmit": {
"desc": "Performs compilation as usual without emitting code.",
"type": "boolean" "type": "boolean"
}, },
"noLib": { "noLib": {
@ -91,7 +95,14 @@
"default": "allow" "default": "allow"
}, },
"runPasses": { "runPasses": {
"desc": "Specifies additional Binaryen passes to run.", "desc": [
"Specifies additional Binaryen passes to run after other",
"optimizations, if any. See: Binaryen/src/passes/pass.cpp"
],
"type": "string" "type": "string"
},
"measure": {
"desc": "Prints measuring information on I/O and compile times.",
"type": "boolean"
} }
} }

View File

@ -136,8 +136,6 @@ export enum Target {
export class Options { export class Options {
/** WebAssembly target. Defaults to {@link Target.WASM32}. */ /** WebAssembly target. Defaults to {@link Target.WASM32}. */
target: Target = Target.WASM32; target: Target = Target.WASM32;
/** If true, performs compilation as usual but doesn't produce any output (all calls to module become nops). */
noEmit: bool = false;
/** If true, compiles everything instead of just reachable code. */ /** If true, compiles everything instead of just reachable code. */
noTreeShaking: bool = false; noTreeShaking: bool = false;
/** If true, replaces assertions with nops. */ /** If true, replaces assertions with nops. */
@ -199,7 +197,7 @@ export class Compiler extends DiagnosticEmitter {
this.program = program; this.program = program;
this.options = options ? options : new Options(); this.options = options ? options : new Options();
this.memoryOffset = new U64(this.options.target == Target.WASM64 ? 8 : 4, 0); // leave space for `null` this.memoryOffset = new U64(this.options.target == Target.WASM64 ? 8 : 4, 0); // leave space for `null`
this.module = this.options.noEmit ? Module.createStub() : Module.create(); this.module = Module.create();
// set up start function // set up start function
var startFunctionTemplate = new FunctionPrototype(program, "start", "start", null); var startFunctionTemplate = new FunctionPrototype(program, "start", "start", null);
@ -315,8 +313,7 @@ export class Compiler extends DiagnosticEmitter {
var previousFunction = this.currentFunction; var previousFunction = this.currentFunction;
this.currentFunction = this.startFunction; this.currentFunction = this.startFunction;
var expr = this.compileStatement(statement); var expr = this.compileStatement(statement);
if (!this.module.noEmit) this.startFunctionBody.push(expr);
this.startFunctionBody.push(expr);
this.currentFunction = previousFunction; this.currentFunction = previousFunction;
break; break;
} }
@ -388,7 +385,7 @@ export class Compiler extends DiagnosticEmitter {
if (declaration.initializer) { if (declaration.initializer) {
if (!initExpr) if (!initExpr)
initExpr = this.compileExpression(declaration.initializer, global.type); initExpr = this.compileExpression(declaration.initializer, global.type);
if (!this.module.noEmit && _BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
if (!global.isMutable) { if (!global.isMutable) {
initExpr = this.precomputeExpressionRef(initExpr); initExpr = this.precomputeExpressionRef(initExpr);
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
@ -407,33 +404,30 @@ export class Compiler extends DiagnosticEmitter {
if (initializeInStart) { if (initializeInStart) {
this.module.addGlobal(internalName, nativeType, true, global.type.toNativeZero(this.module)); this.module.addGlobal(internalName, nativeType, true, global.type.toNativeZero(this.module));
var setExpr = this.module.createSetGlobal(internalName, initExpr); var setExpr = this.module.createSetGlobal(internalName, initExpr);
if (!this.module.noEmit) this.startFunctionBody.push(setExpr);
this.startFunctionBody.push(setExpr);
} else { } else {
if (!global.isMutable) { if (!global.isMutable) {
if (!this.module.noEmit) { var exprType = _BinaryenExpressionGetType(initExpr);
var exprType = _BinaryenExpressionGetType(initExpr); switch (exprType) {
switch (exprType) {
case NativeType.I32: case NativeType.I32:
global.constantIntegerValue = new I64(_BinaryenConstGetValueI32(initExpr), 0); global.constantIntegerValue = new I64(_BinaryenConstGetValueI32(initExpr), 0);
break; break;
case NativeType.I64: case NativeType.I64:
global.constantIntegerValue = new I64(_BinaryenConstGetValueI64Low(initExpr), _BinaryenConstGetValueI64High(initExpr)); global.constantIntegerValue = new I64(_BinaryenConstGetValueI64Low(initExpr), _BinaryenConstGetValueI64High(initExpr));
break; break;
case NativeType.F32: case NativeType.F32:
global.constantFloatValue = _BinaryenConstGetValueF32(initExpr); global.constantFloatValue = _BinaryenConstGetValueF32(initExpr);
break; break;
case NativeType.F64: case NativeType.F64:
global.constantFloatValue = _BinaryenConstGetValueF64(initExpr); global.constantFloatValue = _BinaryenConstGetValueF64(initExpr);
break; break;
default: default:
throw new Error("concrete type expected"); throw new Error("concrete type expected");
}
} }
global.hasConstantValue = true; global.hasConstantValue = true;
if (!declaration || declaration.isTopLevel) { // might be re-exported if (!declaration || declaration.isTopLevel) { // might be re-exported
@ -477,7 +471,7 @@ export class Compiler extends DiagnosticEmitter {
var initExpr: ExpressionRef; var initExpr: ExpressionRef;
if (valueDeclaration.value) { if (valueDeclaration.value) {
initExpr = this.compileExpression(<Expression>valueDeclaration.value, Type.i32); initExpr = this.compileExpression(<Expression>valueDeclaration.value, Type.i32);
if (!this.module.noEmit && _BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
initExpr = this.precomputeExpressionRef(initExpr); initExpr = this.precomputeExpressionRef(initExpr);
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
if (element.isConstant) if (element.isConstant)
@ -502,17 +496,14 @@ export class Compiler extends DiagnosticEmitter {
if (initInStart) { if (initInStart) {
this.module.addGlobal(val.internalName, NativeType.I32, true, this.module.createI32(0)); this.module.addGlobal(val.internalName, NativeType.I32, true, this.module.createI32(0));
var setExpr = this.module.createSetGlobal(val.internalName, initExpr); var setExpr = this.module.createSetGlobal(val.internalName, initExpr);
if (!this.module.noEmit) this.startFunctionBody.push(setExpr);
this.startFunctionBody.push(setExpr);
} else { } else {
this.module.addGlobal(val.internalName, NativeType.I32, false, initExpr); this.module.addGlobal(val.internalName, NativeType.I32, false, initExpr);
if (!this.module.noEmit) { if (_BinaryenExpressionGetType(initExpr) == NativeType.I32) {
if (_BinaryenExpressionGetType(initExpr) == NativeType.I32) { val.constantValue = _BinaryenConstGetValueI32(initExpr);
val.constantValue = _BinaryenConstGetValueI32(initExpr); val.hasConstantValue = true;
val.hasConstantValue = true; } else
} else throw new Error("i32 expected");
throw new Error("i32 expected");
}
} }
} else } else
throw new Error("declaration expected"); throw new Error("declaration expected");

View File

@ -239,7 +239,6 @@ export class Module {
ref: ModuleRef; ref: ModuleRef;
lit: BinaryenLiteral; lit: BinaryenLiteral;
noEmit: bool;
static MAX_MEMORY_WASM32: Index = 0xffff; static MAX_MEMORY_WASM32: Index = 0xffff;
@ -247,7 +246,6 @@ export class Module {
var module = new Module(); var module = new Module();
module.ref = _BinaryenModuleCreate(); module.ref = _BinaryenModuleCreate();
module.lit = changetype<BinaryenLiteral>(allocate_memory(16)); module.lit = changetype<BinaryenLiteral>(allocate_memory(16));
module.noEmit = false;
return module; return module;
} }
@ -257,27 +255,17 @@ export class Module {
var module = new Module(); var module = new Module();
module.ref = _BinaryenModuleRead(cArr, buffer.length); module.ref = _BinaryenModuleRead(cArr, buffer.length);
module.lit = changetype<BinaryenLiteral>(allocate_memory(16)); module.lit = changetype<BinaryenLiteral>(allocate_memory(16));
module.noEmit = false; return module;
return module;
} finally { } finally {
free_memory(changetype<usize>(cArr)); free_memory(changetype<usize>(cArr));
} }
} }
static createStub(): Module {
var module = new Module();
module.ref = 0;
module.lit = changetype<BinaryenLiteral>(0);
module.noEmit = true;
return module;
}
private constructor() { } private constructor() { }
// types // types
addFunctionType(name: string, result: NativeType, paramTypes: NativeType[]): FunctionRef { addFunctionType(name: string, result: NativeType, paramTypes: NativeType[]): FunctionRef {
if (this.noEmit) return 0;
var cStr = allocString(name); var cStr = allocString(name);
var cArr = allocI32Array(paramTypes); var cArr = allocI32Array(paramTypes);
try { try {
@ -289,7 +277,6 @@ export class Module {
} }
getFunctionTypeBySignature(result: NativeType, paramTypes: NativeType[]): FunctionTypeRef { getFunctionTypeBySignature(result: NativeType, paramTypes: NativeType[]): FunctionTypeRef {
if (this.noEmit) return 0;
var cArr = allocI32Array(paramTypes); var cArr = allocI32Array(paramTypes);
try { try {
return _BinaryenGetFunctionTypeBySignature(this.ref, result, cArr, paramTypes.length); return _BinaryenGetFunctionTypeBySignature(this.ref, result, cArr, paramTypes.length);
@ -301,41 +288,34 @@ export class Module {
// expressions // expressions
createI32(value: i32): ExpressionRef { createI32(value: i32): ExpressionRef {
if (this.noEmit) return 0;
_BinaryenLiteralInt32(this.lit, value); _BinaryenLiteralInt32(this.lit, value);
return _BinaryenConst(this.ref, this.lit); return _BinaryenConst(this.ref, this.lit);
} }
createI64(lo: i32, hi: i32 = 0): ExpressionRef { createI64(lo: i32, hi: i32 = 0): ExpressionRef {
if (this.noEmit) return 0;
_BinaryenLiteralInt64(this.lit, lo, hi); _BinaryenLiteralInt64(this.lit, lo, hi);
return _BinaryenConst(this.ref, this.lit); return _BinaryenConst(this.ref, this.lit);
} }
createF32(value: f32): ExpressionRef { createF32(value: f32): ExpressionRef {
if (this.noEmit) return 0;
_BinaryenLiteralFloat32(this.lit, value); _BinaryenLiteralFloat32(this.lit, value);
return _BinaryenConst(this.ref, this.lit); return _BinaryenConst(this.ref, this.lit);
} }
createF64(value: f64): ExpressionRef { createF64(value: f64): ExpressionRef {
if (this.noEmit) return 0;
_BinaryenLiteralFloat64(this.lit, value); _BinaryenLiteralFloat64(this.lit, value);
return _BinaryenConst(this.ref, this.lit); return _BinaryenConst(this.ref, this.lit);
} }
createUnary(op: UnaryOp, expr: ExpressionRef): ExpressionRef { createUnary(op: UnaryOp, expr: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenUnary(this.ref, op, expr); return _BinaryenUnary(this.ref, op, expr);
} }
createBinary(op: BinaryOp, left: ExpressionRef, right: ExpressionRef): ExpressionRef { createBinary(op: BinaryOp, left: ExpressionRef, right: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenBinary(this.ref, op, left, right); return _BinaryenBinary(this.ref, op, left, right);
} }
createHost(op: HostOp, name: string | null = null, operands: ExpressionRef[] | null = null): ExpressionRef { createHost(op: HostOp, name: string | null = null, operands: ExpressionRef[] | null = null): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(name); var cStr = allocString(name);
var cArr = allocI32Array(operands); var cArr = allocI32Array(operands);
try { try {
@ -347,17 +327,14 @@ export class Module {
} }
createGetLocal(index: i32, type: NativeType): ExpressionRef { createGetLocal(index: i32, type: NativeType): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenGetLocal(this.ref, index, type); return _BinaryenGetLocal(this.ref, index, type);
} }
createTeeLocal(index: i32, value: ExpressionRef): ExpressionRef { createTeeLocal(index: i32, value: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenTeeLocal(this.ref, index, value); return _BinaryenTeeLocal(this.ref, index, value);
} }
createGetGlobal(name: string, type: NativeType): ExpressionRef { createGetGlobal(name: string, type: NativeType): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(name); var cStr = allocString(name);
try { try {
return _BinaryenGetGlobal(this.ref, cStr, type); return _BinaryenGetGlobal(this.ref, cStr, type);
@ -367,54 +344,44 @@ export class Module {
} }
createLoad(bytes: Index, signed: bool, ptr: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef { createLoad(bytes: Index, signed: bool, ptr: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenLoad(this.ref, bytes, signed ? 1 : 0, offset, /* always aligned */ bytes, type, ptr); return _BinaryenLoad(this.ref, bytes, signed ? 1 : 0, offset, /* always aligned */ bytes, type, ptr);
} }
createStore(bytes: Index, ptr: ExpressionRef, value: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef { createStore(bytes: Index, ptr: ExpressionRef, value: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenStore(this.ref, bytes, offset, /* always aligned */ bytes, ptr, value, type); return _BinaryenStore(this.ref, bytes, offset, /* always aligned */ bytes, ptr, value, type);
} }
createAtomicLoad(bytes: Index, ptr: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef { createAtomicLoad(bytes: Index, ptr: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenAtomicLoad(this.ref, bytes, offset, type, ptr); return _BinaryenAtomicLoad(this.ref, bytes, offset, type, ptr);
} }
createAtomicStore(bytes: Index, ptr: ExpressionRef, value: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef { createAtomicStore(bytes: Index, ptr: ExpressionRef, value: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenAtomicStore(this.ref, bytes, offset, ptr, value, type); return _BinaryenAtomicStore(this.ref, bytes, offset, ptr, value, type);
} }
createAtomicRMW(op: AtomicRMWOp, bytes: Index, offset: Index, ptr: ExpressionRef, value: ExpressionRef, type: NativeType): ExpressionRef { createAtomicRMW(op: AtomicRMWOp, bytes: Index, offset: Index, ptr: ExpressionRef, value: ExpressionRef, type: NativeType): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenAtomicRMW(this.ref, op, bytes, offset, ptr, value, type); return _BinaryenAtomicRMW(this.ref, op, bytes, offset, ptr, value, type);
} }
createAtomicCmpxchg(bytes: Index, offset: Index, ptr: ExpressionRef, expected: ExpressionRef, replacement: ExpressionRef, type: NativeType): ExpressionRef { createAtomicCmpxchg(bytes: Index, offset: Index, ptr: ExpressionRef, expected: ExpressionRef, replacement: ExpressionRef, type: NativeType): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenAtomicCmpxchg(this.ref, bytes, offset, ptr, expected, replacement, type); return _BinaryenAtomicCmpxchg(this.ref, bytes, offset, ptr, expected, replacement, type);
} }
createAtomicWait(ptr: ExpressionRef, expected: ExpressionRef, timeout: ExpressionRef, expectedType: NativeType): ExpressionRef { createAtomicWait(ptr: ExpressionRef, expected: ExpressionRef, timeout: ExpressionRef, expectedType: NativeType): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenAtomicWait(this.ref, ptr, expected, timeout, expectedType); return _BinaryenAtomicWait(this.ref, ptr, expected, timeout, expectedType);
} }
createAtomicWake(ptr: ExpressionRef, wakeCount: ExpressionRef): ExpressionRef { createAtomicWake(ptr: ExpressionRef, wakeCount: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenAtomicWake(this.ref, ptr, wakeCount); return _BinaryenAtomicWake(this.ref, ptr, wakeCount);
} }
// statements // statements
createSetLocal(index: Index, value: ExpressionRef): ExpressionRef { createSetLocal(index: Index, value: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenSetLocal(this.ref, index, value); return _BinaryenSetLocal(this.ref, index, value);
} }
createSetGlobal(name: string, value: ExpressionRef): ExpressionRef { createSetGlobal(name: string, value: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(name); var cStr = allocString(name);
try { try {
return _BinaryenSetGlobal(this.ref, cStr, value); return _BinaryenSetGlobal(this.ref, cStr, value);
@ -424,7 +391,6 @@ export class Module {
} }
createBlock(label: string | null, children: ExpressionRef[], type: NativeType = NativeType.Auto): ExpressionRef { createBlock(label: string | null, children: ExpressionRef[], type: NativeType = NativeType.Auto): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(label); var cStr = allocString(label);
var cArr = allocI32Array(children); var cArr = allocI32Array(children);
try { try {
@ -436,7 +402,6 @@ export class Module {
} }
createBreak(label: string | null, condition: ExpressionRef = 0, value: ExpressionRef = 0): ExpressionRef { createBreak(label: string | null, condition: ExpressionRef = 0, value: ExpressionRef = 0): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(label); var cStr = allocString(label);
try { try {
return _BinaryenBreak(this.ref, cStr, condition, value); return _BinaryenBreak(this.ref, cStr, condition, value);
@ -446,12 +411,10 @@ export class Module {
} }
createDrop(expression: ExpressionRef): ExpressionRef { createDrop(expression: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenDrop(this.ref, expression); return _BinaryenDrop(this.ref, expression);
} }
createLoop(label: string | null, body: ExpressionRef): ExpressionRef { createLoop(label: string | null, body: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(label); var cStr = allocString(label);
try { try {
return _BinaryenLoop(this.ref, cStr, body); return _BinaryenLoop(this.ref, cStr, body);
@ -461,27 +424,22 @@ export class Module {
} }
createIf(condition: ExpressionRef, ifTrue: ExpressionRef, ifFalse: ExpressionRef = 0): ExpressionRef { createIf(condition: ExpressionRef, ifTrue: ExpressionRef, ifFalse: ExpressionRef = 0): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenIf(this.ref, condition, ifTrue, ifFalse); return _BinaryenIf(this.ref, condition, ifTrue, ifFalse);
} }
createNop(): ExpressionRef { createNop(): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenNop(this.ref); return _BinaryenNop(this.ref);
} }
createReturn(expression: ExpressionRef = 0): ExpressionRef { createReturn(expression: ExpressionRef = 0): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenReturn(this.ref, expression); return _BinaryenReturn(this.ref, expression);
} }
createSelect(ifTrue: ExpressionRef, ifFalse: ExpressionRef, condition: ExpressionRef): ExpressionRef { createSelect(ifTrue: ExpressionRef, ifFalse: ExpressionRef, condition: ExpressionRef): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenSelect(this.ref, condition, ifTrue, ifFalse); return _BinaryenSelect(this.ref, condition, ifTrue, ifFalse);
} }
createSwitch(names: string[], defaultName: string | null, condition: ExpressionRef, value: ExpressionRef = 0): ExpressionRef { createSwitch(names: string[], defaultName: string | null, condition: ExpressionRef, value: ExpressionRef = 0): ExpressionRef {
if (this.noEmit) return 0;
var strs = new Array<usize>(names.length); var strs = new Array<usize>(names.length);
for (var i = 0, k: i32 = names.length; i < k; ++i) for (var i = 0, k: i32 = names.length; i < k; ++i)
strs[i] = allocString(names[i]); strs[i] = allocString(names[i]);
@ -497,7 +455,6 @@ export class Module {
} }
createCall(target: string, operands: ExpressionRef[] | null, returnType: NativeType): ExpressionRef { createCall(target: string, operands: ExpressionRef[] | null, returnType: NativeType): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(target); var cStr = allocString(target);
var cArr = allocI32Array(operands); var cArr = allocI32Array(operands);
try { try {
@ -509,7 +466,6 @@ export class Module {
} }
createCallImport(target: string, operands: ExpressionRef[] | null, returnType: NativeType): ExpressionRef { createCallImport(target: string, operands: ExpressionRef[] | null, returnType: NativeType): ExpressionRef {
if (this.noEmit) return 0;
var cStr = allocString(target); var cStr = allocString(target);
var cArr = allocI32Array(operands); var cArr = allocI32Array(operands);
try { try {
@ -521,14 +477,12 @@ export class Module {
} }
createUnreachable(): ExpressionRef { createUnreachable(): ExpressionRef {
if (this.noEmit) return 0;
return _BinaryenUnreachable(this.ref); return _BinaryenUnreachable(this.ref);
} }
// meta // meta
addGlobal(name: string, type: NativeType, mutable: bool, initializer: ExpressionRef): GlobalRef { addGlobal(name: string, type: NativeType, mutable: bool, initializer: ExpressionRef): GlobalRef {
if (this.noEmit) return 0;
var cStr = allocString(name); var cStr = allocString(name);
try { try {
return _BinaryenAddGlobal(this.ref, cStr, type, mutable ? 1 : 0, initializer); return _BinaryenAddGlobal(this.ref, cStr, type, mutable ? 1 : 0, initializer);
@ -538,7 +492,6 @@ export class Module {
} }
addFunction(name: string, type: FunctionTypeRef, varTypes: NativeType[], body: ExpressionRef): FunctionRef { addFunction(name: string, type: FunctionTypeRef, varTypes: NativeType[], body: ExpressionRef): FunctionRef {
if (this.noEmit) return 0;
var cStr = allocString(name); var cStr = allocString(name);
var cArr = allocI32Array(varTypes); var cArr = allocI32Array(varTypes);
try { try {
@ -559,7 +512,6 @@ export class Module {
} }
addFunctionExport(internalName: string, externalName: string): ExportRef { addFunctionExport(internalName: string, externalName: string): ExportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName); var cStr2 = allocString(externalName);
try { try {
@ -571,7 +523,6 @@ export class Module {
} }
addTableExport(internalName: string, externalName: string): ExportRef { addTableExport(internalName: string, externalName: string): ExportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName); var cStr2 = allocString(externalName);
try { try {
@ -583,7 +534,6 @@ export class Module {
} }
addMemoryExport(internalName: string, externalName: string): ExportRef { addMemoryExport(internalName: string, externalName: string): ExportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName); var cStr2 = allocString(externalName);
try { try {
@ -595,7 +545,6 @@ export class Module {
} }
addGlobalExport(internalName: string, externalName: string): ExportRef { addGlobalExport(internalName: string, externalName: string): ExportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName); var cStr2 = allocString(externalName);
try { try {
@ -607,7 +556,6 @@ export class Module {
} }
removeExport(externalName: string): void { removeExport(externalName: string): void {
if (this.noEmit) return;
var cStr = allocString(externalName); var cStr = allocString(externalName);
try { try {
_BinaryenRemoveExport(this.ref, cStr); _BinaryenRemoveExport(this.ref, cStr);
@ -617,7 +565,6 @@ export class Module {
} }
addFunctionImport(internalName: string, externalModuleName: string, externalBaseName: string, functionType: FunctionTypeRef): ImportRef { addFunctionImport(internalName: string, externalModuleName: string, externalBaseName: string, functionType: FunctionTypeRef): ImportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName); var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName); var cStr3 = allocString(externalBaseName);
@ -631,7 +578,6 @@ export class Module {
} }
addTableImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef { addTableImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName); var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName); var cStr3 = allocString(externalBaseName);
@ -645,7 +591,6 @@ export class Module {
} }
addMemoryImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef { addMemoryImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName); var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName); var cStr3 = allocString(externalBaseName);
@ -659,7 +604,6 @@ export class Module {
} }
addGlobalImport(internalName: string, externalModuleName: string, externalBaseName: string, globalType: NativeType): ImportRef { addGlobalImport(internalName: string, externalModuleName: string, externalBaseName: string, globalType: NativeType): ImportRef {
if (this.noEmit) return 0;
var cStr1 = allocString(internalName); var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName); var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName); var cStr3 = allocString(externalBaseName);
@ -673,7 +617,6 @@ export class Module {
} }
removeImport(internalName: string): void { removeImport(internalName: string): void {
if (this.noEmit) return;
var cStr = allocString(internalName); var cStr = allocString(internalName);
try { try {
_BinaryenRemoveImport(this.ref, cStr); _BinaryenRemoveImport(this.ref, cStr);
@ -683,7 +626,6 @@ export class Module {
} }
setMemory(initial: Index, maximum: Index, segments: MemorySegment[], target: Target, exportName: string | null = null): void { setMemory(initial: Index, maximum: Index, segments: MemorySegment[], target: Target, exportName: string | null = null): void {
if (this.noEmit) return;
var cStr = allocString(exportName); var cStr = allocString(exportName);
var k = segments.length; var k = segments.length;
var segs = new Array<usize>(k); var segs = new Array<usize>(k);
@ -713,7 +655,6 @@ export class Module {
} }
setFunctionTable(funcs: FunctionRef[]): void { setFunctionTable(funcs: FunctionRef[]): void {
if (this.noEmit) return;
var cArr = allocI32Array(funcs); var cArr = allocI32Array(funcs);
try { try {
_BinaryenSetFunctionTable(this.ref, cArr, funcs.length); _BinaryenSetFunctionTable(this.ref, cArr, funcs.length);
@ -723,7 +664,6 @@ export class Module {
} }
setStart(func: FunctionRef): void { setStart(func: FunctionRef): void {
if (this.noEmit) return;
_BinaryenSetStart(this.ref, func); _BinaryenSetStart(this.ref, func);
} }
@ -765,7 +705,6 @@ export class Module {
} }
validate(): bool { validate(): bool {
if (this.noEmit) return false;
return _BinaryenModuleValidate(this.ref) == 1; return _BinaryenModuleValidate(this.ref) == 1;
} }
@ -802,12 +741,13 @@ export class Module {
} }
createRelooper(): Relooper { createRelooper(): Relooper {
return this.noEmit ? Relooper.createStub(this) : Relooper.create(this); return Relooper.create(this);
} }
// currently supports side effect free expressions only // currently supports side effect free expressions only
cloneExpression(expr: ExpressionRef, noSideEffects: bool = false, maxDepth: i32 = i32.MAX_VALUE): ExpressionRef { cloneExpression(expr: ExpressionRef, noSideEffects: bool = false, maxDepth: i32 = i32.MAX_VALUE): ExpressionRef {
if (this.noEmit || maxDepth < 0) return 0; if (maxDepth < 0)
return 0;
var nested1: ExpressionRef, var nested1: ExpressionRef,
nested2: ExpressionRef; nested2: ExpressionRef;
@ -859,43 +799,36 @@ export class Relooper {
module: Module; module: Module;
ref: RelooperRef; ref: RelooperRef;
noEmit: bool;
static create(module: Module): Relooper { static create(module: Module): Relooper {
var relooper = new Relooper(); var relooper = new Relooper();
relooper.module = module; relooper.module = module;
relooper.ref = _RelooperCreate(); relooper.ref = _RelooperCreate();
relooper.noEmit = false; return relooper;
return relooper;
} }
static createStub(module: Module): Relooper { static createStub(module: Module): Relooper {
var relooper = new Relooper(); var relooper = new Relooper();
relooper.module = module; relooper.module = module;
relooper.ref = 0; relooper.ref = 0;
relooper.noEmit = true;
return relooper; return relooper;
} }
private constructor() {} private constructor() {}
addBlock(code: ExpressionRef): RelooperBlockRef { addBlock(code: ExpressionRef): RelooperBlockRef {
if (this.noEmit) return 0;
return _RelooperAddBlock(this.ref, code); return _RelooperAddBlock(this.ref, code);
} }
addBranch(from: RelooperBlockRef, to: RelooperBlockRef, condition: ExpressionRef = 0, code: ExpressionRef = 0): void { addBranch(from: RelooperBlockRef, to: RelooperBlockRef, condition: ExpressionRef = 0, code: ExpressionRef = 0): void {
if (this.noEmit) return;
_RelooperAddBranch(from, to, condition, code); _RelooperAddBranch(from, to, condition, code);
} }
addBlockWithSwitch(code: ExpressionRef, condition: ExpressionRef): RelooperBlockRef { addBlockWithSwitch(code: ExpressionRef, condition: ExpressionRef): RelooperBlockRef {
if (this.noEmit) return 0;
return _RelooperAddBlockWithSwitch(this.ref, code, condition); return _RelooperAddBlockWithSwitch(this.ref, code, condition);
} }
addBranchForSwitch(from: RelooperBlockRef, to: RelooperBlockRef, indexes: i32[], code: ExpressionRef = 0): void { addBranchForSwitch(from: RelooperBlockRef, to: RelooperBlockRef, indexes: i32[], code: ExpressionRef = 0): void {
if (this.noEmit) return;
var cArr = allocI32Array(indexes); var cArr = allocI32Array(indexes);
try { try {
_RelooperAddBranchForSwitch(from, to, cArr, indexes.length, code); _RelooperAddBranchForSwitch(from, to, cArr, indexes.length, code);
@ -905,7 +838,6 @@ export class Relooper {
} }
renderAndDispose(entry: RelooperBlockRef, labelHelper: Index): ExpressionRef { renderAndDispose(entry: RelooperBlockRef, labelHelper: Index): ExpressionRef {
if (this.noEmit) return 0;
return _RelooperRenderAndDispose(this.ref, entry, labelHelper, this.module.ref); return _RelooperRenderAndDispose(this.ref, entry, labelHelper, this.module.ref);
} }
} }