mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 22:41:27 +00:00
Moved noEmit to compiler frontend; Added I/O and compile time measuring
This commit is contained in:
177
bin/asc.js
177
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"));
|
||||
|
Reference in New Issue
Block a user