Skip to content

Commit 9141f42

Browse files
committed
Merge branch 'master' into eqeqeq
2 parents 3de338d + 88cc73b commit 9141f42

File tree

329 files changed

+112803
-90138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

329 files changed

+112803
-90138
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,20 @@ jobs:
141141
cd lib/loader
142142
npm run asbuild
143143
npm run test
144+
test-bootstrap:
145+
name: "Test self-compilation on node: node"
146+
runs-on: ubuntu-latest
147+
needs: check
148+
steps:
149+
- uses: actions/[email protected]
150+
- uses: dcodeIO/[email protected]
151+
with:
152+
node-version: node
153+
- name: Install dependencies
154+
run: npm ci --no-audit
155+
- name: Clean distribution files
156+
run: npm run clean
157+
- name: Test self-compilation
158+
run: |
159+
npm run asbuild
160+
npm run astest

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ under the licensing terms detailed in LICENSE:
1919
* jhwgh1968 <[email protected]>
2020
* Jeffrey Charles <[email protected]>
2121
* Vladimir Tikhonov <[email protected]>
22+
* Duncan Uszkay <[email protected]>
2223

2324
Portions of this software are derived from third-party works licensed under
2425
the following terms:

cli/asc.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Definitions for asc.
3+
* @license Apache-2.0
4+
*/
5+
16
import { OptionDescription } from "./util/options";
27
export { OptionDescription };
38

cli/asc.js

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
"use strict";
21
/**
3-
* Compiler frontend for node.js
2+
* @license
3+
* Copyright 2020 Daniel Wirtz / The AssemblyScript Authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
/**
21+
* @fileoverview Compiler frontend for node.js
422
*
523
* Uses the low-level API exported from src/index.ts so it works with the compiler compiled to
624
* JavaScript as well as the compiler compiled to WebAssembly (eventually). Runs the sources
725
* directly through ts-node if distribution files are not present (indicated by a `-dev` version).
826
*
927
* Can also be packaged as a bundle suitable for in-browser use with the standard library injected
1028
* in the build step. See dist/asc.js for the bundle and webpack.config.js for building details.
11-
*
12-
* @module cli/asc
1329
*/
1430

1531
// Use "." instead of "/" as cwd in browsers
@@ -231,12 +247,7 @@ exports.main = function main(argv, options, callback) {
231247
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);
232248
assemblyscript.setNoUnsafe(compilerOptions, args.noUnsafe);
233249
assemblyscript.setPedantic(compilerOptions, args.pedantic);
234-
235-
// Initialize default aliases
236-
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
237-
assemblyscript.setGlobalAlias(compilerOptions, "Mathf", "NativeMathf");
238-
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/builtins/abort");
239-
assemblyscript.setGlobalAlias(compilerOptions, "trace", "~lib/builtins/trace");
250+
assemblyscript.setLowMemoryLimit(compilerOptions, args.lowMemoryLimit >>> 0);
240251

241252
// Add or override aliases if specified
242253
if (args.use) {
@@ -390,7 +401,8 @@ exports.main = function main(argv, options, callback) {
390401
if ((sourceText = readFile(sourcePath = internalPath + ".ts", baseDir)) == null) {
391402
if ((sourceText = readFile(sourcePath = internalPath + "/index.ts", baseDir)) == null) {
392403
// portable d.ts: uses the .js file next to it in JS or becomes an import in Wasm
393-
sourceText = readFile(sourcePath = internalPath + ".d.ts", baseDir);
404+
sourcePath = internalPath + ".ts";
405+
sourceText = readFile(internalPath + ".d.ts", baseDir);
394406
}
395407
}
396408

@@ -478,13 +490,16 @@ exports.main = function main(argv, options, callback) {
478490
var internalPath;
479491
while ((internalPath = assemblyscript.nextFile(program)) != null) {
480492
let file = getFile(internalPath, assemblyscript.getDependee(program, internalPath));
481-
if (!file) return callback(Error("Import file '" + internalPath + ".ts' not found."))
493+
if (!file) return callback(Error("Import '" + internalPath + "' not found."))
482494
stats.parseCount++;
483495
stats.parseTime += measure(() => {
484496
assemblyscript.parse(program, file.sourceText, file.sourcePath, false);
485497
});
486498
}
487-
if (checkDiagnostics(program, stderr)) return callback(Error("Parse error"));
499+
var numErrors = checkDiagnostics(program, stderr);
500+
if (numErrors) {
501+
return callback(Error(numErrors + " parse error(s)"));
502+
}
488503
}
489504

490505
// Include runtime template before entry files so its setup runs first
@@ -570,6 +585,20 @@ exports.main = function main(argv, options, callback) {
570585
optimizeLevel = Math.min(Math.max(optimizeLevel, 0), 3);
571586
shrinkLevel = Math.min(Math.max(shrinkLevel, 0), 2);
572587

588+
try {
589+
stats.compileTime += measure(() => {
590+
assemblyscript.initializeProgram(program, compilerOptions);
591+
});
592+
} catch(e) {
593+
return callback(e);
594+
}
595+
596+
// Call afterInitialize transform hook
597+
{
598+
let error = applyTransform("afterInitialize", program);
599+
if (error) return callback(error);
600+
}
601+
573602
var module;
574603
stats.compileCount++;
575604
try {
@@ -579,9 +608,10 @@ exports.main = function main(argv, options, callback) {
579608
} catch (e) {
580609
return callback(e);
581610
}
582-
if (checkDiagnostics(program, stderr)) {
611+
var numErrors = checkDiagnostics(program, stderr);
612+
if (numErrors) {
583613
if (module) module.dispose();
584-
return callback(Error("Compile error"));
614+
return callback(Error(numErrors + " compile error(s)"));
585615
}
586616

587617
// Call afterCompile transform hook
@@ -643,6 +673,20 @@ exports.main = function main(argv, options, callback) {
643673
const passes = [];
644674
function add(pass) { passes.push(pass); }
645675

676+
if (optimizeLevel >= 2 && shrinkLevel === 0) {
677+
// tweak inlining options when speed more preferable than size
678+
module.setAlwaysInlineMaxSize(12);
679+
module.setFlexibleInlineMaxSize(70);
680+
module.setOneCallerInlineMaxSize(200);
681+
} else {
682+
// tweak inlining options when size matters
683+
optimizeLevel === 0 && shrinkLevel >= 0
684+
? module.setAlwaysInlineMaxSize(2)
685+
: module.setAlwaysInlineMaxSize(4); // default: 2
686+
module.setFlexibleInlineMaxSize(65); // default: 20
687+
module.setOneCallerInlineMaxSize(80); // default: 15
688+
}
689+
646690
// Optimize the module if requested
647691
if (optimizeLevel > 0 || shrinkLevel > 0) {
648692
// Binaryen's default passes with Post-AssemblyScript passes added.
@@ -656,9 +700,15 @@ exports.main = function main(argv, options, callback) {
656700
add("ssa-nomerge");
657701
}
658702
if (optimizeLevel >= 3) {
703+
add("simplify-locals-nostructure"); // differs
704+
add("vacuum"); // differs
705+
add("reorder-locals"); // differs
659706
add("flatten");
660707
add("local-cse");
661708
}
709+
if (optimizeLevel >= 2 || shrinkLevel >= 1) { // differs
710+
add("rse");
711+
}
662712
if (hasARC) { // differs
663713
if (optimizeLevel < 3) {
664714
add("flatten");
@@ -668,11 +718,12 @@ exports.main = function main(argv, options, callback) {
668718
add("dce");
669719
add("remove-unused-brs");
670720
add("remove-unused-names");
671-
add("optimize-instructions");
721+
// add("optimize-instructions"); // differs move 2 lines above
672722
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
673723
add("pick-load-signs");
674724
add("simplify-globals-optimizing"); // differs
675725
}
726+
add("optimize-instructions"); // differs
676727
if (optimizeLevel >= 3 || shrinkLevel >= 2) {
677728
add("precompute-propagate");
678729
} else {
@@ -682,19 +733,25 @@ exports.main = function main(argv, options, callback) {
682733
// if (optimizeLevel >= 2 || shrinkLevel >= 2) {
683734
// add("code-pushing");
684735
// }
736+
if (optimizeLevel >= 3 && shrinkLevel <= 1) { // differs
737+
add("licm");
738+
}
685739
add("simplify-locals-nostructure");
686740
add("vacuum");
687741
add("reorder-locals");
688742
add("remove-unused-brs");
689-
if (optimizeLevel >= 3 || shrinkLevel >= 2) {
690-
add("merge-locals");
691-
}
743+
// if (optimizeLevel >= 3 || shrinkLevel >= 2) { // do it later
744+
// add("merge-locals");
745+
// }
692746
add("coalesce-locals");
693747
add("simplify-locals");
694748
add("vacuum");
695749
add("reorder-locals");
696750
add("coalesce-locals");
697751
add("reorder-locals");
752+
if (optimizeLevel >= 3 || shrinkLevel >= 1) { // differs
753+
add("merge-locals");
754+
}
698755
add("vacuum");
699756
if (optimizeLevel >= 3 || shrinkLevel >= 1) {
700757
add("code-folding");
@@ -754,29 +811,26 @@ exports.main = function main(argv, options, callback) {
754811
add("remove-unused-brs");
755812
add("vacuum");
756813

757-
// replace indirect calls with direct and inline if possible again.
758-
add("directize");
759-
add("inlining-optimizing");
760814
// move some code after early return which potentially could reduce computations
761815
// do this after CFG cleanup (originally it was done before)
762816
// moved from (1)
763817
add("code-pushing");
764-
765-
// this quite expensive so do this only for highest opt level
766-
add("simplify-globals-optimizing");
767818
if (optimizeLevel >= 3) {
768-
add("simplify-locals-nostructure");
769-
add("vacuum");
770-
819+
// this quite expensive so do this only for highest opt level
820+
add("simplify-globals");
821+
// replace indirect calls with direct and inline if possible again.
822+
add("directize");
823+
add("dae-optimizing");
771824
add("precompute-propagate");
825+
add("coalesce-locals");
826+
add("merge-locals");
772827
add("simplify-locals-nostructure");
773828
add("vacuum");
774-
775-
add("reorder-locals");
776-
} else {
777-
add("simplify-globals-optimizing");
829+
add("inlining-optimizing");
830+
add("precompute-propagate");
778831
}
779832
add("optimize-instructions");
833+
add("simplify-globals-optimizing");
780834
}
781835
// remove unused elements of table and pack / reduce memory
782836
add("duplicate-function-elimination"); // differs
@@ -1023,17 +1077,17 @@ exports.main = function main(argv, options, callback) {
10231077
/** Checks diagnostics emitted so far for errors. */
10241078
function checkDiagnostics(program, stderr) {
10251079
var diagnostic;
1026-
var hasErrors = false;
1080+
var numErrors = 0;
10271081
while ((diagnostic = assemblyscript.nextDiagnostic(program)) != null) {
10281082
if (stderr) {
10291083
stderr.write(
10301084
assemblyscript.formatDiagnostic(diagnostic, stderr.isTTY, true) +
10311085
EOL + EOL
10321086
);
10331087
}
1034-
if (assemblyscript.isError(diagnostic)) hasErrors = true;
1088+
if (assemblyscript.isError(diagnostic)) ++numErrors;
10351089
}
1036-
return hasErrors;
1090+
return numErrors;
10371091
}
10381092

10391093
exports.checkDiagnostics = checkDiagnostics;

cli/asc.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@
167167
"TODO_doesNothingYet": [
168168
" nontrapping-f2i Non-trapping float to integer ops.",
169169
" exception-handling Exception handling.",
170-
" tail-calls Tail call operations."
170+
" tail-calls Tail call operations.",
171+
" multi-value Multi value types."
171172
],
172173
"type": "S"
173174
},
@@ -191,6 +192,12 @@
191192
"type": "S",
192193
"alias": "u"
193194
},
195+
"lowMemoryLimit": {
196+
"category": "Features",
197+
"description": "Enforces very low (<64k) memory constraints.",
198+
"default": 0,
199+
"type": "i"
200+
},
194201

195202
"memoryBase": {
196203
"category": "Linking",

cli/transform.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* Definitions for custom compiler transforms that can be applied with the `--transform` option.
3-
* @module cli/transform
4-
*//***/
2+
* @fileoverview Compiler transform interface definitions.
3+
* @license Apache-2.0
4+
*/
55

66
import { Program, Parser, Module } from "..";
77
import { OutputStream } from "./asc";
@@ -35,6 +35,9 @@ export abstract class Transform {
3535
/** Called when parsing is complete, before a program is instantiated from the AST. */
3636
afterParse?(parser: Parser): void;
3737

38+
/** Called after the program is instantiated. */
39+
afterInitialize?(program: Program): void;
40+
3841
/** Called when compilation is complete, before the module is being validated. */
3942
afterCompile?(module: Module): void;
4043
}

cli/transform.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
/**
2+
* @fileoverview Compiler transform interface.
3+
* @license Apache-2.0
4+
*/
5+
16
// becomes replaced with the actual base by asc
27
exports.Transform = function Transform() {};

cli/util/colors.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Terminal colors utility definitions.
3+
* @license Apache-2.0
4+
*/
5+
16
interface Colors {
27
/** Whether terminal colors are supported. */
38
supported: boolean;

cli/util/colors.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Terminal colors utility.
3+
* @license Apache-2.0
4+
*/
5+
16
var proc = typeof process !== "undefined" && process || {};
27
var isCI = proc.env && "CI" in proc.env; // doesn't work when bundled because 'process' is a mock
38

cli/util/find.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview File finding utility definitions.
3+
* @license Apache-2.0
4+
*/
5+
16
export function files(dirname: string, filter?: ((name: string) => bool) | RegExp): string[];
27
export const TS: RegExp;
38
export const TS_EXCEPT_DTS: RegExp;

0 commit comments

Comments
 (0)