Skip to content

Commit 87d10bb

Browse files
committed
Merge remote-tracking branch 'origin/master' into release
2 parents c46fea4 + 7c5e50e commit 87d10bb

File tree

91 files changed

+12567
-7453
lines changed

Some content is hidden

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

91 files changed

+12567
-7453
lines changed

bin/asc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ try { require("source-map-support").install(); } catch (e) {}
1818

1919
const asc = module.exports = require("../cli/asc.js");
2020
if (/\basc$/.test(process.argv[1])) {
21-
process.exitCode = asc.main(process.argv.slice(2));
21+
asc.ready.then(() => process.exitCode = asc.main(process.argv.slice(2)));
2222
}

cli/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ The API accepts the same options as the CLI but also lets you override stdout an
1717

1818
```js
1919
const asc = require("assemblyscript/cli/asc");
20-
asc.main([
21-
"myModule.ts",
22-
"--binaryFile", "myModule.wasm",
23-
"--optimize",
24-
"--sourceMap",
25-
"--measure"
26-
], {
27-
stdout: process.stdout,
28-
stderr: process.stderr
29-
}, function(err) {
30-
if (err)
31-
throw err;
32-
...
20+
asc.ready.then(() => {
21+
asc.main([
22+
"myModule.ts",
23+
"--binaryFile", "myModule.wasm",
24+
"--optimize",
25+
"--sourceMap",
26+
"--measure"
27+
], {
28+
stdout: process.stdout,
29+
stderr: process.stderr
30+
}, function(err) {
31+
if (err)
32+
throw err;
33+
...
34+
});
3335
});
3436
```
3537

@@ -43,6 +45,9 @@ const options = require("assemblyscript/cli/asc.json");
4345
You can also compile a source string directly, for example in a browser environment:
4446

4547
```js
46-
const { binary, text, stdout, stderr } = asc.compileString(`...`, { optimize: 2 });
48+
const asc = require("assemblyscript/cli/asc");
49+
asc.ready.then(() => {
50+
const { binary, text, stdout, stderr } = asc.compileString(`...`, { optimize: 2 });
51+
});
4752
...
4853
```

cli/asc.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { OptionDescription } from "./util/options";
22
export { OptionDescription };
33

4+
/** Ready promise resolved once/if the compiler is ready. */
5+
export const ready: Promise<void>;
6+
47
/** Whether this is a webpack bundle or not. */
58
export const isBundle: boolean;
69

cli/asc.js

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,39 @@ const mkdirp = require("./util/mkdirp");
2424
const find = require("./util/find");
2525
const EOL = process.platform === "win32" ? "\r\n" : "\n";
2626
const SEP = process.platform === "win32" ? "\\" : "/";
27+
const binaryen = global.Binaryen || (global.Binaryen = require("binaryen"));
2728

28-
// global.Binaryen = require("../lib/binaryen");
29+
// Proxy Binaryen's ready event
30+
Object.defineProperty(exports, "ready", {
31+
get: function() { return binaryen.ready; }
32+
});
2933

3034
// Emscripten adds an `uncaughtException` listener to Binaryen that results in an additional
3135
// useless code fragment on top of an actual error. suppress this:
3236
if (process.removeAllListeners) process.removeAllListeners("uncaughtException");
3337

3438
// Use distribution files if present, otherwise run the sources directly
3539
var assemblyscript, isDev = false;
36-
(() => {
37-
try { // `asc` on the command line
38-
assemblyscript = require("../dist/assemblyscript.js");
39-
} catch (e) {
40-
try { // `asc` on the command line without dist files
41-
require("ts-node").register({
42-
project: path.join(__dirname, "..", "src", "tsconfig.json"),
43-
skipIgnore: true,
44-
compilerOptions: { target: "ES2016" }
45-
});
46-
require("../src/glue/js");
47-
assemblyscript = require("../src");
48-
isDev = true;
49-
} catch (e_ts) {
50-
try { // `require("dist/asc.js")` in explicit browser tests
51-
assemblyscript = eval("require('./assemblyscript')");
52-
} catch (e) {
53-
throw Error(e_ts.stack + "\n---\n" + e.stack);
54-
}
40+
try { // `asc` on the command line
41+
assemblyscript = require("../dist/assemblyscript.js");
42+
} catch (e) {
43+
try { // `asc` on the command line without dist files
44+
require("ts-node").register({
45+
project: path.join(__dirname, "..", "src", "tsconfig.json"),
46+
skipIgnore: true,
47+
compilerOptions: { target: "ES2016" }
48+
});
49+
require("../src/glue/js");
50+
assemblyscript = require("../src");
51+
isDev = true;
52+
} catch (e_ts) {
53+
try { // `require("dist/asc.js")` in explicit browser tests
54+
assemblyscript = eval("require('./assemblyscript')");
55+
} catch (e) {
56+
throw Error(e_ts.stack + "\n---\n" + e.stack);
5557
}
5658
}
57-
})();
59+
}
5860

5961
/** Whether this is a webpack bundle or not. */
6062
exports.isBundle = typeof BUNDLE_VERSION === "string";
@@ -646,12 +648,12 @@ exports.main = function main(argv, options, callback) {
646648
if (optimizeLevel >= 3 || shrinkLevel >= 1) {
647649
add("ssa-nomerge");
648650
}
649-
if (optimizeLevel >= 4) {
651+
if (optimizeLevel >= 3) {
650652
add("flatten");
651653
add("local-cse");
652654
}
653655
if (hasARC) { // differs
654-
if (optimizeLevel < 4) {
656+
if (optimizeLevel < 3) {
655657
add("flatten");
656658
}
657659
add("post-assemblyscript");
@@ -668,9 +670,10 @@ exports.main = function main(argv, options, callback) {
668670
} else {
669671
add("precompute");
670672
}
671-
if (optimizeLevel >= 2 || shrinkLevel >= 2) {
672-
add("code-pushing");
673-
}
673+
// this will be done later
674+
// if (optimizeLevel >= 2 || shrinkLevel >= 2) {
675+
// add("code-pushing");
676+
// }
674677
add("simplify-locals-nostructure");
675678
add("vacuum");
676679
add("reorder-locals");
@@ -701,30 +704,64 @@ exports.main = function main(argv, options, callback) {
701704
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
702705
add("rse");
703706
}
704-
if (hasARC) { // differs
705-
add("post-assemblyscript-finalize");
706-
}
707707
add("vacuum");
708-
709708
// PassRunner::addDefaultGlobalOptimizationPostPasses
710709
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
711710
add("dae-optimizing");
712711
}
713712
if (optimizeLevel >= 2 || shrinkLevel >= 2) {
714713
add("inlining-optimizing");
715714
}
716-
add("duplicate-function-elimination");
715+
// "duplicate-function-elimination" will better done later
716+
// add("duplicate-function-elimination");
717717
add("duplicate-import-elimination");
718718
if (optimizeLevel >= 2 || shrinkLevel >= 2) {
719719
add("simplify-globals-optimizing");
720720
} else {
721721
add("simplify-globals");
722722
}
723-
add("remove-unused-module-elements");
724-
add("memory-packing");
725-
add("directize");
723+
// replace indirect calls with direct, reduce arity and
724+
// inline this calls if possible
725+
add("directize"); // differs
726+
add("dae-optimizing"); // differs
726727
add("inlining-optimizing"); // differs
727-
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
728+
// ARC finalization should be done exactly after inlining for better release/retain reduction
729+
if (hasARC) { // differs
730+
add("post-assemblyscript-finalize");
731+
}
732+
if (optimizeLevel >= 2 || shrinkLevel >= 1) { // differs
733+
add("rse");
734+
add("vacuum");
735+
736+
// rearrange / reduce switch cases again
737+
add("remove-unused-brs");
738+
add("remove-unused-names");
739+
add("merge-blocks");
740+
add("vacuum");
741+
742+
// replace indirect calls with direct and inline if possible again.
743+
add("directize");
744+
add("inlining-optimizing");
745+
// move some code after early return which potentially could reduce computations
746+
// do this after CFG cleanup (originally it was done before)
747+
add("code-pushing");
748+
749+
// this quite expensive so do this only for highest opt level
750+
if (optimizeLevel >= 3) {
751+
add("simplify-locals-nostructure");
752+
add("reorder-locals");
753+
add("vacuum");
754+
}
755+
// finally optimize all remaining peepholes
756+
add("simplify-globals-optimizing");
757+
add("optimize-instructions");
758+
}
759+
// remove unused elements of table and pack / reduce memory
760+
add("duplicate-function-elimination"); // differs
761+
add("remove-unused-nonfunction-module-elements"); // differs
762+
add("memory-packing");
763+
add("remove-unused-module-elements"); // differs
764+
if (optimizeLevel >= 3 || shrinkLevel >= 1) { // differs. was optimizeLevel >= 2
728765
add("generate-stack-ir");
729766
add("optimize-stack-ir");
730767
}

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
2222
},
2323
"dependencies": {
24-
"binaryen": "89.0.0-nightly.20191215",
24+
"binaryen": "90.0.0-nightly.20191231",
2525
"long": "^4.0.0",
2626
"source-map-support": "^0.5.16",
2727
"ts-node": "^6.2.0"

scripts/update-constants.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Updates the Binaryen constants in src/module.ts
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const binaryen = require("binaryen");
6+
7+
const srcfile = path.join(__dirname, "..", "src", "module.ts");
8+
var src = fs.readFileSync(srcfile, "utf8");
9+
10+
binaryen.ready.then(() => {
11+
src = src.replace(/enum (\w+) \{([^}]*)\}/g, function($0) {
12+
return $0.replace(/(\w+)[ ]+=[ ]+([^,;\n]+)/g, function($0, key, val) {
13+
var match = val.match(/\b(_Binaryen\w+)\b/);
14+
if (match) {
15+
let fn = match[1];
16+
let id = binaryen[fn]();
17+
console.log(fn + " = " + id);
18+
return key + " = " + id + " /* " + fn + " */";
19+
}
20+
return $0;
21+
});
22+
});
23+
fs.writeFileSync(srcfile, src, "utf8");
24+
});

src/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,18 @@ Portable compiler sources that compile to both JavaScript using `tsc` and, event
22

33
Architecture
44
------------
5+
56
![](https://assemblyscript.github.io/assemblyscript/media/architecture.svg)
7+
8+
Usage
9+
-----
10+
11+
Note that using the compiler as a library requires awaiting Binaryen ready state, like so:
12+
13+
```js
14+
const binaryen = require("binaryen");
15+
const assemblyscript = require("assemblyscript");
16+
binaryen.ready.then(() => {
17+
// do something with assemblyscript
18+
});
19+
```

src/builtins.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ export namespace BuiltinSymbols {
323323
export const v128_min = "~lib/builtins/v128.min";
324324
export const v128_max = "~lib/builtins/v128.max";
325325
export const v128_dot = "~lib/builtins/v128.dot";
326+
export const v128_avgr = "~lib/builtins/v128.avgr";
326327
export const v128_abs = "~lib/builtins/v128.abs";
327328
export const v128_sqrt = "~lib/builtins/v128.sqrt";
328329
export const v128_eq = "~lib/builtins/v128.eq";
@@ -357,6 +358,7 @@ export namespace BuiltinSymbols {
357358
export const i8x16_min_u = "~lib/builtins/i8x16.min_u";
358359
export const i8x16_max_s = "~lib/builtins/i8x16.max_s";
359360
export const i8x16_max_u = "~lib/builtins/i8x16.max_u";
361+
export const i8x16_avgr_u = "~lib/builtins/i8x16.avgr_u";
360362
export const i8x16_neg = "~lib/builtins/i8x16.neg";
361363
export const i8x16_add_saturate_s = "~lib/builtins/i8x16.add_saturate_s";
362364
export const i8x16_add_saturate_u = "~lib/builtins/i8x16.add_saturate_u";
@@ -391,6 +393,7 @@ export namespace BuiltinSymbols {
391393
export const i16x8_min_u = "~lib/builtins/i16x8.min_u";
392394
export const i16x8_max_s = "~lib/builtins/i16x8.max_s";
393395
export const i16x8_max_u = "~lib/builtins/i16x8.max_u";
396+
export const i16x8_avgr_u = "~lib/builtins/i16x8.avgr_u";
394397
export const i16x8_neg = "~lib/builtins/i16x8.neg";
395398
export const i16x8_add_saturate_s = "~lib/builtins/i16x8.add_saturate_s";
396399
export const i16x8_add_saturate_u = "~lib/builtins/i16x8.add_saturate_u";
@@ -850,7 +853,7 @@ export function compileCall(
850853
}
851854
offset = (<Field>field).memoryOffset;
852855
} else {
853-
offset = classType.currentMemoryOffset;
856+
offset = classType.nextMemoryOffset;
854857
}
855858
if (compiler.options.isWasm64) {
856859
// implicitly wrap if contextual type is a 32-bit integer
@@ -3377,6 +3380,30 @@ export function compileCall(
33773380
);
33783381
return module.unreachable();
33793382
}
3383+
case BuiltinSymbols.v128_avgr: { // avgr<T!>(a: v128, b: v128) -> v128
3384+
if (
3385+
checkFeatureEnabled(Feature.SIMD, reportNode, compiler) |
3386+
checkTypeRequired(typeArguments, reportNode, compiler) |
3387+
checkArgsRequired(operands, 2, reportNode, compiler)
3388+
) {
3389+
compiler.currentType = Type.v128;
3390+
return module.unreachable();
3391+
}
3392+
let type = typeArguments![0];
3393+
let arg0 = compiler.compileExpression(operands[0], Type.v128, Constraints.CONV_IMPLICIT);
3394+
let arg1 = compiler.compileExpression(operands[1], Type.v128, Constraints.CONV_IMPLICIT);
3395+
if (!type.is(TypeFlags.REFERENCE)) {
3396+
switch (type.kind) {
3397+
case TypeKind.U8: return module.binary(BinaryOp.AvgrU8x16, arg0, arg1);
3398+
case TypeKind.U16: return module.binary(BinaryOp.AvgrU16x8, arg0, arg1);
3399+
}
3400+
}
3401+
compiler.error(
3402+
DiagnosticCode.Operation_0_cannot_be_applied_to_type_1,
3403+
reportNode.typeArgumentsRange, "v128.avgr", type.toString()
3404+
);
3405+
return module.unreachable();
3406+
}
33803407
case BuiltinSymbols.v128_eq: { // eq<T!>(a: v128, b: v128) -> v128
33813408
if (
33823409
checkFeatureEnabled(Feature.SIMD, reportNode, compiler) |
@@ -4516,6 +4543,7 @@ function tryDeferASM(
45164543
case BuiltinSymbols.i8x16_min_u: return deferASM(BuiltinSymbols.v128_min, compiler, Type.u8, operands, Type.v128, reportNode);
45174544
case BuiltinSymbols.i8x16_max_s: return deferASM(BuiltinSymbols.v128_max, compiler, Type.i8, operands, Type.v128, reportNode);
45184545
case BuiltinSymbols.i8x16_max_u: return deferASM(BuiltinSymbols.v128_max, compiler, Type.u8, operands, Type.v128, reportNode);
4546+
case BuiltinSymbols.i8x16_avgr_u: return deferASM(BuiltinSymbols.v128_avgr, compiler, Type.u8, operands, Type.v128, reportNode);
45194547
case BuiltinSymbols.i8x16_neg: return deferASM(BuiltinSymbols.v128_neg, compiler, Type.i8, operands, Type.v128, reportNode);
45204548
case BuiltinSymbols.i8x16_add_saturate_s: return deferASM(BuiltinSymbols.v128_add_saturate, compiler, Type.i8, operands, Type.v128, reportNode);
45214549
case BuiltinSymbols.i8x16_add_saturate_u: return deferASM(BuiltinSymbols.v128_add_saturate, compiler, Type.u8, operands, Type.v128, reportNode);
@@ -4550,6 +4578,7 @@ function tryDeferASM(
45504578
case BuiltinSymbols.i16x8_min_u: return deferASM(BuiltinSymbols.v128_min, compiler, Type.u16, operands, Type.v128, reportNode);
45514579
case BuiltinSymbols.i16x8_max_s: return deferASM(BuiltinSymbols.v128_max, compiler, Type.i16, operands, Type.v128, reportNode);
45524580
case BuiltinSymbols.i16x8_max_u: return deferASM(BuiltinSymbols.v128_max, compiler, Type.u16, operands, Type.v128, reportNode);
4581+
case BuiltinSymbols.i16x8_avgr_u: return deferASM(BuiltinSymbols.v128_avgr, compiler, Type.u16, operands, Type.v128, reportNode);
45534582
case BuiltinSymbols.i16x8_neg: return deferASM(BuiltinSymbols.v128_neg, compiler, Type.i16, operands, Type.v128, reportNode);
45544583
case BuiltinSymbols.i16x8_add_saturate_s: return deferASM(BuiltinSymbols.v128_add_saturate, compiler, Type.i16, operands, Type.v128, reportNode);
45554584
case BuiltinSymbols.i16x8_add_saturate_u: return deferASM(BuiltinSymbols.v128_add_saturate, compiler, Type.u16, operands, Type.v128, reportNode);

src/compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ export class Compiler extends DiagnosticEmitter {
15791579
var runtimeHeaderSize = program.runtimeHeaderSize;
15801580
var arrayPrototype = assert(program.arrayPrototype);
15811581
var arrayInstance = assert(this.resolver.resolveClass(arrayPrototype, [ elementType ]));
1582-
var arrayInstanceSize = arrayInstance.currentMemoryOffset;
1582+
var arrayInstanceSize = arrayInstance.nextMemoryOffset;
15831583
var bufferLength = bufferSegment.buffer.length - runtimeHeaderSize;
15841584
var arrayLength = i32(bufferLength / elementType.byteSize);
15851585

@@ -9123,8 +9123,8 @@ export class Compiler extends DiagnosticEmitter {
91239123
this.compileFunction(allocInstance);
91249124
return module.call(allocInstance.internalName, [
91259125
options.isWasm64
9126-
? module.i64(classInstance.currentMemoryOffset)
9127-
: module.i32(classInstance.currentMemoryOffset),
9126+
? module.i64(classInstance.nextMemoryOffset)
9127+
: module.i32(classInstance.nextMemoryOffset),
91289128
module.i32(
91299129
classInstance.hasDecorator(DecoratorFlags.UNMANAGED)
91309130
? 0

0 commit comments

Comments
 (0)