1
- "use strict" ;
2
1
/**
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
4
22
*
5
23
* Uses the low-level API exported from src/index.ts so it works with the compiler compiled to
6
24
* JavaScript as well as the compiler compiled to WebAssembly (eventually). Runs the sources
7
25
* directly through ts-node if distribution files are not present (indicated by a `-dev` version).
8
26
*
9
27
* Can also be packaged as a bundle suitable for in-browser use with the standard library injected
10
28
* in the build step. See dist/asc.js for the bundle and webpack.config.js for building details.
11
- *
12
- * @module cli/asc
13
29
*/
14
30
15
31
// Use "." instead of "/" as cwd in browsers
@@ -231,12 +247,7 @@ exports.main = function main(argv, options, callback) {
231
247
assemblyscript . setSourceMap ( compilerOptions , args . sourceMap != null ) ;
232
248
assemblyscript . setNoUnsafe ( compilerOptions , args . noUnsafe ) ;
233
249
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 ) ;
240
251
241
252
// Add or override aliases if specified
242
253
if ( args . use ) {
@@ -390,7 +401,8 @@ exports.main = function main(argv, options, callback) {
390
401
if ( ( sourceText = readFile ( sourcePath = internalPath + ".ts" , baseDir ) ) == null ) {
391
402
if ( ( sourceText = readFile ( sourcePath = internalPath + "/index.ts" , baseDir ) ) == null ) {
392
403
// 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 ) ;
394
406
}
395
407
}
396
408
@@ -478,13 +490,16 @@ exports.main = function main(argv, options, callback) {
478
490
var internalPath ;
479
491
while ( ( internalPath = assemblyscript . nextFile ( program ) ) != null ) {
480
492
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." ) )
482
494
stats . parseCount ++ ;
483
495
stats . parseTime += measure ( ( ) => {
484
496
assemblyscript . parse ( program , file . sourceText , file . sourcePath , false ) ;
485
497
} ) ;
486
498
}
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
+ }
488
503
}
489
504
490
505
// Include runtime template before entry files so its setup runs first
@@ -570,6 +585,20 @@ exports.main = function main(argv, options, callback) {
570
585
optimizeLevel = Math . min ( Math . max ( optimizeLevel , 0 ) , 3 ) ;
571
586
shrinkLevel = Math . min ( Math . max ( shrinkLevel , 0 ) , 2 ) ;
572
587
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
+
573
602
var module ;
574
603
stats . compileCount ++ ;
575
604
try {
@@ -579,9 +608,10 @@ exports.main = function main(argv, options, callback) {
579
608
} catch ( e ) {
580
609
return callback ( e ) ;
581
610
}
582
- if ( checkDiagnostics ( program , stderr ) ) {
611
+ var numErrors = checkDiagnostics ( program , stderr ) ;
612
+ if ( numErrors ) {
583
613
if ( module ) module . dispose ( ) ;
584
- return callback ( Error ( "Compile error") ) ;
614
+ return callback ( Error ( numErrors + " compile error(s) ") ) ;
585
615
}
586
616
587
617
// Call afterCompile transform hook
@@ -643,6 +673,20 @@ exports.main = function main(argv, options, callback) {
643
673
const passes = [ ] ;
644
674
function add ( pass ) { passes . push ( pass ) ; }
645
675
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
+
646
690
// Optimize the module if requested
647
691
if ( optimizeLevel > 0 || shrinkLevel > 0 ) {
648
692
// Binaryen's default passes with Post-AssemblyScript passes added.
@@ -656,9 +700,15 @@ exports.main = function main(argv, options, callback) {
656
700
add ( "ssa-nomerge" ) ;
657
701
}
658
702
if ( optimizeLevel >= 3 ) {
703
+ add ( "simplify-locals-nostructure" ) ; // differs
704
+ add ( "vacuum" ) ; // differs
705
+ add ( "reorder-locals" ) ; // differs
659
706
add ( "flatten" ) ;
660
707
add ( "local-cse" ) ;
661
708
}
709
+ if ( optimizeLevel >= 2 || shrinkLevel >= 1 ) { // differs
710
+ add ( "rse" ) ;
711
+ }
662
712
if ( hasARC ) { // differs
663
713
if ( optimizeLevel < 3 ) {
664
714
add ( "flatten" ) ;
@@ -668,11 +718,12 @@ exports.main = function main(argv, options, callback) {
668
718
add ( "dce" ) ;
669
719
add ( "remove-unused-brs" ) ;
670
720
add ( "remove-unused-names" ) ;
671
- add ( "optimize-instructions" ) ;
721
+ // add("optimize-instructions"); // differs move 2 lines above
672
722
if ( optimizeLevel >= 2 || shrinkLevel >= 1 ) {
673
723
add ( "pick-load-signs" ) ;
674
724
add ( "simplify-globals-optimizing" ) ; // differs
675
725
}
726
+ add ( "optimize-instructions" ) ; // differs
676
727
if ( optimizeLevel >= 3 || shrinkLevel >= 2 ) {
677
728
add ( "precompute-propagate" ) ;
678
729
} else {
@@ -682,19 +733,25 @@ exports.main = function main(argv, options, callback) {
682
733
// if (optimizeLevel >= 2 || shrinkLevel >= 2) {
683
734
// add("code-pushing");
684
735
// }
736
+ if ( optimizeLevel >= 3 && shrinkLevel <= 1 ) { // differs
737
+ add ( "licm" ) ;
738
+ }
685
739
add ( "simplify-locals-nostructure" ) ;
686
740
add ( "vacuum" ) ;
687
741
add ( "reorder-locals" ) ;
688
742
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
+ // }
692
746
add ( "coalesce-locals" ) ;
693
747
add ( "simplify-locals" ) ;
694
748
add ( "vacuum" ) ;
695
749
add ( "reorder-locals" ) ;
696
750
add ( "coalesce-locals" ) ;
697
751
add ( "reorder-locals" ) ;
752
+ if ( optimizeLevel >= 3 || shrinkLevel >= 1 ) { // differs
753
+ add ( "merge-locals" ) ;
754
+ }
698
755
add ( "vacuum" ) ;
699
756
if ( optimizeLevel >= 3 || shrinkLevel >= 1 ) {
700
757
add ( "code-folding" ) ;
@@ -754,29 +811,26 @@ exports.main = function main(argv, options, callback) {
754
811
add ( "remove-unused-brs" ) ;
755
812
add ( "vacuum" ) ;
756
813
757
- // replace indirect calls with direct and inline if possible again.
758
- add ( "directize" ) ;
759
- add ( "inlining-optimizing" ) ;
760
814
// move some code after early return which potentially could reduce computations
761
815
// do this after CFG cleanup (originally it was done before)
762
816
// moved from (1)
763
817
add ( "code-pushing" ) ;
764
-
765
- // this quite expensive so do this only for highest opt level
766
- add ( "simplify-globals-optimizing" ) ;
767
818
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" ) ;
771
824
add ( "precompute-propagate" ) ;
825
+ add ( "coalesce-locals" ) ;
826
+ add ( "merge-locals" ) ;
772
827
add ( "simplify-locals-nostructure" ) ;
773
828
add ( "vacuum" ) ;
774
-
775
- add ( "reorder-locals" ) ;
776
- } else {
777
- add ( "simplify-globals-optimizing" ) ;
829
+ add ( "inlining-optimizing" ) ;
830
+ add ( "precompute-propagate" ) ;
778
831
}
779
832
add ( "optimize-instructions" ) ;
833
+ add ( "simplify-globals-optimizing" ) ;
780
834
}
781
835
// remove unused elements of table and pack / reduce memory
782
836
add ( "duplicate-function-elimination" ) ; // differs
@@ -1023,17 +1077,17 @@ exports.main = function main(argv, options, callback) {
1023
1077
/** Checks diagnostics emitted so far for errors. */
1024
1078
function checkDiagnostics ( program , stderr ) {
1025
1079
var diagnostic ;
1026
- var hasErrors = false ;
1080
+ var numErrors = 0 ;
1027
1081
while ( ( diagnostic = assemblyscript . nextDiagnostic ( program ) ) != null ) {
1028
1082
if ( stderr ) {
1029
1083
stderr . write (
1030
1084
assemblyscript . formatDiagnostic ( diagnostic , stderr . isTTY , true ) +
1031
1085
EOL + EOL
1032
1086
) ;
1033
1087
}
1034
- if ( assemblyscript . isError ( diagnostic ) ) hasErrors = true ;
1088
+ if ( assemblyscript . isError ( diagnostic ) ) ++ numErrors ;
1035
1089
}
1036
- return hasErrors ;
1090
+ return numErrors ;
1037
1091
}
1038
1092
1039
1093
exports . checkDiagnostics = checkDiagnostics ;
0 commit comments