diff --git a/bin/asc.js b/bin/asc.js index 36fb353d..c437ae56 100644 --- a/bin/asc.js +++ b/bin/asc.js @@ -63,7 +63,7 @@ if (args.help || args._.length < 1) { }); console.log([ "Version " + version, - "Syntax: asc [options] [entryFile ...]", + "Syntax: asc [entryFile ...] [options]", "", "Examples: asc hello.ts", " asc hello.ts -b hello.wasm -t hello.wast", @@ -76,6 +76,21 @@ if (args.help || args._.length < 1) { 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) { var diagnostic; var hasErrors = false; @@ -92,10 +107,18 @@ function checkDiagnostics(parser) { // Include standard library if (!args.noLib) { var stdlibDir = path.join(__dirname, "..", "std", "assembly"); - glob.sync("*.ts", { cwd: stdlibDir }).forEach(file => { - var nextText = fs.readFileSync(path.join(stdlibDir, file), { encoding: "utf8" }); - parser = assemblyscript.parseFile(nextText, "std:" + file, parser, false); - }); + var notIoTime = 0; + readTime += measure(() => { + 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 @@ -105,10 +128,16 @@ args._.forEach(filename => { var entryText; try { - entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" }); + readTime += measure(() => { + entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" }); + }); + ++readCount; } catch (e) { try { - entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" }); + readTime += measure(() => { + entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" }); + }); + ++readCount; entryPath = entryPath + "/index"; } catch (e) { console.error("File '" + entryPath + ".ts' not found."); @@ -120,21 +149,31 @@ args._.forEach(filename => { var nextText; // Load entry text - parser = assemblyscript.parseFile(entryText, entryPath, parser, true); + parseTime += measure(() => { + parser = assemblyscript.parseFile(entryText, entryPath, parser, true); + }); while ((nextPath = parser.nextFile()) != null) { try { - nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" }); + readTime += measure(() => { + nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" }); + }); + ++readCount; } catch (e) { try { - nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" }); + readTime += measure(() => { + nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" }); + }); + ++readCount; nextPath = nextPath + "/index"; } catch (e) { console.error("Imported file '" + nextPath + ".ts' not found."); process.exit(1); } } - assemblyscript.parseFile(nextText, nextPath, parser); + parseTime += measure(() => { + assemblyscript.parseFile(nextText, nextPath, parser); + }); } checkDiagnostics(parser); }); @@ -144,21 +183,29 @@ assemblyscript.setTarget(options, 0); assemblyscript.setNoTreeShaking(options, args.noTreeShaking); assemblyscript.setNoAssert(options, args.noAssert); 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); if (args.validate) - if (!module.validate()) { - module.dispose(); - process.exit(1); - } + validateTime += measure(() => { + if (!module.validate()) { + module.dispose(); + process.exit(1); + } + }); if (args.trapMode === "clamp") - module.runPasses([ "trap-mode-clamp" ]); + optimizeTime += measure(() => { + module.runPasses([ "trap-mode-clamp" ]); + }); else if (args.trapMode === "js") - module.runPasses([ "trap-mode-js" ]); + optimizeTime += measure(() => { + module.runPasses([ "trap-mode-js" ]); + }); else if (args.trapMode !== "allow") { console.log("Unsupported trap mode: " + args.trapMode); process.exit(1); @@ -200,10 +247,6 @@ if (typeof args.optimizeLevel === "number") if (typeof args.shrinkLevel === "number") 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 if (args.runPasses) { if (typeof args.runPasses === "string") @@ -220,39 +263,67 @@ module.setShrinkLevel(shrinkLevel); module.setDebugInfo(debugInfo); if (optimizeLevel >= 0) - module.optimize(); + optimizeTime += measure(() => { + module.optimize(); + }); 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 (/\.wast$/.test(args.outFile) && args.textFile == null) - args.textFile = args.outFile; - else if (/\.js$/.test(args.outFile) && args.asmjsFile == null) - args.asmjsFile = args.outFile; - else if (args.binaryFile == null) - args.binaryFile = args.outFile; -} -if (args.binaryFile != null && args.binaryFile.length) { - fs.writeFileSync(args.binaryFile, module.toBinary()); - hasOutput = true; -} -if (args.textFile != null && args.textFile.length) { - fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" }); - hasOutput = true; -} -if (args.asmjsFile != null && args.asmjsFile.length) { - fs.writeFileSync(args.asmjsFile, module.toAsmjs(), { encoding: "utf8" }); - hasOutput = true; -} -if (!hasOutput) { - if (args.binaryFile === "") - process.stdout.write(Buffer.from(module.toBinary())); - else if (args.asmjsFile === "") - module.printAsmjs(); - else - module.print(); + if (args.outFile != null) { + if (/\.wast$/.test(args.outFile) && args.textFile == null) + args.textFile = args.outFile; + else if (/\.js$/.test(args.outFile) && args.asmjsFile == null) + args.asmjsFile = args.outFile; + else if (args.binaryFile == null) + args.binaryFile = args.outFile; + } + if (args.binaryFile != null && args.binaryFile.length) { + writeTime += measure(() => { + fs.writeFileSync(args.binaryFile, module.toBinary()); + }); + hasOutput = true; + } + if (args.textFile != null && args.textFile.length) { + writeTime += measure(() => { + fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" }); + }); + hasOutput = true; + } + if (args.asmjsFile != null && args.asmjsFile.length) { + writeTime += measure(() => { + fs.writeFileSync(args.asmjsFile, module.toAsmjs(), { encoding: "utf8" }); + }); + hasOutput = true; + } + 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(); + +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")); diff --git a/bin/asc.json b/bin/asc.json index f79a2fe6..8d0dc6d3 100644 --- a/bin/asc.json +++ b/bin/asc.json @@ -13,11 +13,11 @@ "desc": [ "Optimizes the module. Also accepts the optimize level:", "", - " -O Defaults: Equivalent to -O2s", + " -O Uses defaults. Equivalent to -O2s", " -O0 Equivalent to --optimizeLevel 0", " -O1 Equivalent to --optimizeLevel 1", " -O2 Equivalent to --optimizeLevel 2", - " -O2 Equivalent to --optimizeLevel 3", + " -O3 Equivalent to --optimizeLevel 3", " -Oz Equivalent to -O but with --shrinkLevel 2", " -O3s Equivalent to -O3 with --shrinkLevel 1 etc.", "" @@ -39,7 +39,7 @@ "aliases": [ "c", "check" ] }, "outFile": { - "desc": "Specifies the output file. Format is determined by file extension.", + "desc": "Specifies the output file. File extension indicates format.", "type": "string", "aliases": [ "o" ] }, @@ -59,15 +59,19 @@ "aliases": [ "a" ] }, "noTreeShaking": { - "desc": "Disables tree-shaking.", + "desc": "Disables compiler-level tree-shaking.", "type": "boolean" }, "noDebug": { - "desc": "Disables debug information in binaries.", + "desc": "Disables maintaining debug information in binaries.", "type": "boolean" }, "noAssert": { - "desc": "Disables assertions.", + "desc": "Replaces assertions with NOPs.", + "type": "boolean" + }, + "noEmit": { + "desc": "Performs compilation as usual without emitting code.", "type": "boolean" }, "noLib": { @@ -91,7 +95,14 @@ "default": "allow" }, "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" + }, + "measure": { + "desc": "Prints measuring information on I/O and compile times.", + "type": "boolean" } } diff --git a/src/compiler.ts b/src/compiler.ts index 74e371de..a75ff038 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -136,8 +136,6 @@ export enum Target { export class Options { /** WebAssembly target. Defaults to {@link 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. */ noTreeShaking: bool = false; /** If true, replaces assertions with nops. */ @@ -199,7 +197,7 @@ export class Compiler extends DiagnosticEmitter { this.program = program; this.options = options ? options : new Options(); 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 var startFunctionTemplate = new FunctionPrototype(program, "start", "start", null); @@ -315,8 +313,7 @@ export class Compiler extends DiagnosticEmitter { var previousFunction = this.currentFunction; this.currentFunction = this.startFunction; var expr = this.compileStatement(statement); - if (!this.module.noEmit) - this.startFunctionBody.push(expr); + this.startFunctionBody.push(expr); this.currentFunction = previousFunction; break; } @@ -388,7 +385,7 @@ export class Compiler extends DiagnosticEmitter { if (declaration.initializer) { if (!initExpr) initExpr = this.compileExpression(declaration.initializer, global.type); - if (!this.module.noEmit && _BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { + if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { if (!global.isMutable) { initExpr = this.precomputeExpressionRef(initExpr); if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { @@ -407,33 +404,30 @@ export class Compiler extends DiagnosticEmitter { if (initializeInStart) { this.module.addGlobal(internalName, nativeType, true, global.type.toNativeZero(this.module)); var setExpr = this.module.createSetGlobal(internalName, initExpr); - if (!this.module.noEmit) - this.startFunctionBody.push(setExpr); + this.startFunctionBody.push(setExpr); } else { if (!global.isMutable) { - if (!this.module.noEmit) { - var exprType = _BinaryenExpressionGetType(initExpr); - switch (exprType) { + var exprType = _BinaryenExpressionGetType(initExpr); + switch (exprType) { - case NativeType.I32: - global.constantIntegerValue = new I64(_BinaryenConstGetValueI32(initExpr), 0); - break; + case NativeType.I32: + global.constantIntegerValue = new I64(_BinaryenConstGetValueI32(initExpr), 0); + break; - case NativeType.I64: - global.constantIntegerValue = new I64(_BinaryenConstGetValueI64Low(initExpr), _BinaryenConstGetValueI64High(initExpr)); - break; + case NativeType.I64: + global.constantIntegerValue = new I64(_BinaryenConstGetValueI64Low(initExpr), _BinaryenConstGetValueI64High(initExpr)); + break; - case NativeType.F32: - global.constantFloatValue = _BinaryenConstGetValueF32(initExpr); - break; + case NativeType.F32: + global.constantFloatValue = _BinaryenConstGetValueF32(initExpr); + break; - case NativeType.F64: - global.constantFloatValue = _BinaryenConstGetValueF64(initExpr); - break; + case NativeType.F64: + global.constantFloatValue = _BinaryenConstGetValueF64(initExpr); + break; - default: - throw new Error("concrete type expected"); - } + default: + throw new Error("concrete type expected"); } global.hasConstantValue = true; if (!declaration || declaration.isTopLevel) { // might be re-exported @@ -477,7 +471,7 @@ export class Compiler extends DiagnosticEmitter { var initExpr: ExpressionRef; if (valueDeclaration.value) { initExpr = this.compileExpression(valueDeclaration.value, Type.i32); - if (!this.module.noEmit && _BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { + if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { initExpr = this.precomputeExpressionRef(initExpr); if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { if (element.isConstant) @@ -502,17 +496,14 @@ export class Compiler extends DiagnosticEmitter { if (initInStart) { this.module.addGlobal(val.internalName, NativeType.I32, true, this.module.createI32(0)); var setExpr = this.module.createSetGlobal(val.internalName, initExpr); - if (!this.module.noEmit) - this.startFunctionBody.push(setExpr); + this.startFunctionBody.push(setExpr); } else { this.module.addGlobal(val.internalName, NativeType.I32, false, initExpr); - if (!this.module.noEmit) { - if (_BinaryenExpressionGetType(initExpr) == NativeType.I32) { - val.constantValue = _BinaryenConstGetValueI32(initExpr); - val.hasConstantValue = true; - } else - throw new Error("i32 expected"); - } + if (_BinaryenExpressionGetType(initExpr) == NativeType.I32) { + val.constantValue = _BinaryenConstGetValueI32(initExpr); + val.hasConstantValue = true; + } else + throw new Error("i32 expected"); } } else throw new Error("declaration expected"); diff --git a/src/module.ts b/src/module.ts index 8e537aad..23b1c065 100644 --- a/src/module.ts +++ b/src/module.ts @@ -239,7 +239,6 @@ export class Module { ref: ModuleRef; lit: BinaryenLiteral; - noEmit: bool; static MAX_MEMORY_WASM32: Index = 0xffff; @@ -247,7 +246,6 @@ export class Module { var module = new Module(); module.ref = _BinaryenModuleCreate(); module.lit = changetype(allocate_memory(16)); - module.noEmit = false; return module; } @@ -257,27 +255,17 @@ export class Module { var module = new Module(); module.ref = _BinaryenModuleRead(cArr, buffer.length); module.lit = changetype(allocate_memory(16)); - module.noEmit = false; - return module; + return module; } finally { free_memory(changetype(cArr)); } } - static createStub(): Module { - var module = new Module(); - module.ref = 0; - module.lit = changetype(0); - module.noEmit = true; - return module; - } - private constructor() { } // types addFunctionType(name: string, result: NativeType, paramTypes: NativeType[]): FunctionRef { - if (this.noEmit) return 0; var cStr = allocString(name); var cArr = allocI32Array(paramTypes); try { @@ -289,7 +277,6 @@ export class Module { } getFunctionTypeBySignature(result: NativeType, paramTypes: NativeType[]): FunctionTypeRef { - if (this.noEmit) return 0; var cArr = allocI32Array(paramTypes); try { return _BinaryenGetFunctionTypeBySignature(this.ref, result, cArr, paramTypes.length); @@ -301,41 +288,34 @@ export class Module { // expressions createI32(value: i32): ExpressionRef { - if (this.noEmit) return 0; _BinaryenLiteralInt32(this.lit, value); return _BinaryenConst(this.ref, this.lit); } createI64(lo: i32, hi: i32 = 0): ExpressionRef { - if (this.noEmit) return 0; _BinaryenLiteralInt64(this.lit, lo, hi); return _BinaryenConst(this.ref, this.lit); } createF32(value: f32): ExpressionRef { - if (this.noEmit) return 0; _BinaryenLiteralFloat32(this.lit, value); return _BinaryenConst(this.ref, this.lit); } createF64(value: f64): ExpressionRef { - if (this.noEmit) return 0; _BinaryenLiteralFloat64(this.lit, value); return _BinaryenConst(this.ref, this.lit); } createUnary(op: UnaryOp, expr: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenUnary(this.ref, op, expr); } createBinary(op: BinaryOp, left: ExpressionRef, right: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenBinary(this.ref, op, left, right); } createHost(op: HostOp, name: string | null = null, operands: ExpressionRef[] | null = null): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(name); var cArr = allocI32Array(operands); try { @@ -347,17 +327,14 @@ export class Module { } createGetLocal(index: i32, type: NativeType): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenGetLocal(this.ref, index, type); } createTeeLocal(index: i32, value: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenTeeLocal(this.ref, index, value); } createGetGlobal(name: string, type: NativeType): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(name); try { 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 { - if (this.noEmit) return 0; 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 { - if (this.noEmit) return 0; return _BinaryenStore(this.ref, bytes, offset, /* always aligned */ bytes, ptr, value, type); } createAtomicLoad(bytes: Index, ptr: ExpressionRef, type: NativeType, offset: Index = 0): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenAtomicLoad(this.ref, bytes, offset, type, ptr); } 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); } 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); } 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); } createAtomicWait(ptr: ExpressionRef, expected: ExpressionRef, timeout: ExpressionRef, expectedType: NativeType): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenAtomicWait(this.ref, ptr, expected, timeout, expectedType); } createAtomicWake(ptr: ExpressionRef, wakeCount: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenAtomicWake(this.ref, ptr, wakeCount); } // statements createSetLocal(index: Index, value: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenSetLocal(this.ref, index, value); } createSetGlobal(name: string, value: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(name); try { return _BinaryenSetGlobal(this.ref, cStr, value); @@ -424,7 +391,6 @@ export class Module { } createBlock(label: string | null, children: ExpressionRef[], type: NativeType = NativeType.Auto): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(label); var cArr = allocI32Array(children); try { @@ -436,7 +402,6 @@ export class Module { } createBreak(label: string | null, condition: ExpressionRef = 0, value: ExpressionRef = 0): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(label); try { return _BinaryenBreak(this.ref, cStr, condition, value); @@ -446,12 +411,10 @@ export class Module { } createDrop(expression: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenDrop(this.ref, expression); } createLoop(label: string | null, body: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(label); try { return _BinaryenLoop(this.ref, cStr, body); @@ -461,27 +424,22 @@ export class Module { } createIf(condition: ExpressionRef, ifTrue: ExpressionRef, ifFalse: ExpressionRef = 0): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenIf(this.ref, condition, ifTrue, ifFalse); } createNop(): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenNop(this.ref); } createReturn(expression: ExpressionRef = 0): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenReturn(this.ref, expression); } createSelect(ifTrue: ExpressionRef, ifFalse: ExpressionRef, condition: ExpressionRef): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenSelect(this.ref, condition, ifTrue, ifFalse); } createSwitch(names: string[], defaultName: string | null, condition: ExpressionRef, value: ExpressionRef = 0): ExpressionRef { - if (this.noEmit) return 0; var strs = new Array(names.length); for (var i = 0, k: i32 = names.length; i < k; ++i) strs[i] = allocString(names[i]); @@ -497,7 +455,6 @@ export class Module { } createCall(target: string, operands: ExpressionRef[] | null, returnType: NativeType): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(target); var cArr = allocI32Array(operands); try { @@ -509,7 +466,6 @@ export class Module { } createCallImport(target: string, operands: ExpressionRef[] | null, returnType: NativeType): ExpressionRef { - if (this.noEmit) return 0; var cStr = allocString(target); var cArr = allocI32Array(operands); try { @@ -521,14 +477,12 @@ export class Module { } createUnreachable(): ExpressionRef { - if (this.noEmit) return 0; return _BinaryenUnreachable(this.ref); } // meta addGlobal(name: string, type: NativeType, mutable: bool, initializer: ExpressionRef): GlobalRef { - if (this.noEmit) return 0; var cStr = allocString(name); try { 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 { - if (this.noEmit) return 0; var cStr = allocString(name); var cArr = allocI32Array(varTypes); try { @@ -559,7 +512,6 @@ export class Module { } addFunctionExport(internalName: string, externalName: string): ExportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalName); try { @@ -571,7 +523,6 @@ export class Module { } addTableExport(internalName: string, externalName: string): ExportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalName); try { @@ -583,7 +534,6 @@ export class Module { } addMemoryExport(internalName: string, externalName: string): ExportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalName); try { @@ -595,7 +545,6 @@ export class Module { } addGlobalExport(internalName: string, externalName: string): ExportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalName); try { @@ -607,7 +556,6 @@ export class Module { } removeExport(externalName: string): void { - if (this.noEmit) return; var cStr = allocString(externalName); try { _BinaryenRemoveExport(this.ref, cStr); @@ -617,7 +565,6 @@ export class Module { } addFunctionImport(internalName: string, externalModuleName: string, externalBaseName: string, functionType: FunctionTypeRef): ImportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalModuleName); var cStr3 = allocString(externalBaseName); @@ -631,7 +578,6 @@ export class Module { } addTableImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalModuleName); var cStr3 = allocString(externalBaseName); @@ -645,7 +591,6 @@ export class Module { } addMemoryImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalModuleName); var cStr3 = allocString(externalBaseName); @@ -659,7 +604,6 @@ export class Module { } addGlobalImport(internalName: string, externalModuleName: string, externalBaseName: string, globalType: NativeType): ImportRef { - if (this.noEmit) return 0; var cStr1 = allocString(internalName); var cStr2 = allocString(externalModuleName); var cStr3 = allocString(externalBaseName); @@ -673,7 +617,6 @@ export class Module { } removeImport(internalName: string): void { - if (this.noEmit) return; var cStr = allocString(internalName); try { _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 { - if (this.noEmit) return; var cStr = allocString(exportName); var k = segments.length; var segs = new Array(k); @@ -713,7 +655,6 @@ export class Module { } setFunctionTable(funcs: FunctionRef[]): void { - if (this.noEmit) return; var cArr = allocI32Array(funcs); try { _BinaryenSetFunctionTable(this.ref, cArr, funcs.length); @@ -723,7 +664,6 @@ export class Module { } setStart(func: FunctionRef): void { - if (this.noEmit) return; _BinaryenSetStart(this.ref, func); } @@ -765,7 +705,6 @@ export class Module { } validate(): bool { - if (this.noEmit) return false; return _BinaryenModuleValidate(this.ref) == 1; } @@ -802,12 +741,13 @@ export class Module { } createRelooper(): Relooper { - return this.noEmit ? Relooper.createStub(this) : Relooper.create(this); + return Relooper.create(this); } // currently supports side effect free expressions only 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, nested2: ExpressionRef; @@ -859,43 +799,36 @@ export class Relooper { module: Module; ref: RelooperRef; - noEmit: bool; static create(module: Module): Relooper { var relooper = new Relooper(); relooper.module = module; relooper.ref = _RelooperCreate(); - relooper.noEmit = false; - return relooper; + return relooper; } static createStub(module: Module): Relooper { var relooper = new Relooper(); relooper.module = module; relooper.ref = 0; - relooper.noEmit = true; return relooper; } private constructor() {} addBlock(code: ExpressionRef): RelooperBlockRef { - if (this.noEmit) return 0; return _RelooperAddBlock(this.ref, code); } addBranch(from: RelooperBlockRef, to: RelooperBlockRef, condition: ExpressionRef = 0, code: ExpressionRef = 0): void { - if (this.noEmit) return; _RelooperAddBranch(from, to, condition, code); } addBlockWithSwitch(code: ExpressionRef, condition: ExpressionRef): RelooperBlockRef { - if (this.noEmit) return 0; return _RelooperAddBlockWithSwitch(this.ref, code, condition); } addBranchForSwitch(from: RelooperBlockRef, to: RelooperBlockRef, indexes: i32[], code: ExpressionRef = 0): void { - if (this.noEmit) return; var cArr = allocI32Array(indexes); try { _RelooperAddBranchForSwitch(from, to, cArr, indexes.length, code); @@ -905,7 +838,6 @@ export class Relooper { } renderAndDispose(entry: RelooperBlockRef, labelHelper: Index): ExpressionRef { - if (this.noEmit) return 0; return _RelooperRenderAndDispose(this.ref, entry, labelHelper, this.module.ref); } }