Skip to content

Commit 732068e

Browse files
committed
Add trapMode option to asc; Disable flatten/ssa passes for now
1 parent 6d8de50 commit 732068e

16 files changed

+1096
-1307
lines changed

Diff for: README.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,20 @@ Syntax: asc [options] [file ...]
6969
Examples: asc hello.ts
7070
7171
Options:
72-
-v, --version Prints the compiler's version.
73-
-h, --help Prints this message.
74-
-O, --optimize Optimizes the module.
75-
-c, --validate Validates the module.
76-
-o, --outFile Specifies the output file.
77-
-b, --binaryFile Specifies the binary format output file.
78-
-t, --textFile Specifies the text format output file.
79-
-a, --asmjsFile Specifies the asm.js format output file.
80-
--noTreeShaking Disables tree-shaking.
81-
--noDebug Disables assertions.
72+
-v, --version Prints the compiler's version.
73+
-h, --help Prints this message.
74+
-O, --optimize Optimizes the module.
75+
-c, --validate Validates the module.
76+
-o, --outFile Specifies the output file. Format is determined by file extension.
77+
-b, --binaryFile Specifies the binary format output file (.wasm).
78+
-t, --textFile Specifies the text format output file (.wast).
79+
-a, --asmjsFile Specifies the asm.js format output file (.js).
80+
--noTreeShaking Disables tree-shaking.
81+
--noDebug Disables assertions.
82+
--trapMode Sets the trap mode to use.
83+
none Do not modify trapping operations. This is the default.
84+
clamp Replace trapping operations with clamping semantics.
85+
js Replace trapping operations with JS semantics.
8286
```
8387

8488
Unless a bundle has been built to `dist/`, `asc` runs the TypeScript sources directly via [ts-node](https://www.npmjs.com/package/ts-node). Useful for development.

Diff for: bin/asc.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Object.keys(conf).forEach(key => {
3131

3232
var args = minimist(process.argv.slice(2), opts);
3333
var version = require("../package.json").version;
34+
var indent = 20;
3435
if (isDev) version += "-dev";
3536

3637
if (args.version) {
@@ -48,9 +49,16 @@ if (args.help || args._.length < 1) {
4849
if (option.aliases && option.aliases[0].length === 1)
4950
text += "-" + option.aliases[0] + ", ";
5051
text += "--" + name;
51-
while (text.length < 24)
52+
while (text.length < indent)
5253
text += " ";
53-
options.push(text + option.desc);
54+
if (Array.isArray(option.desc)) {
55+
options.push(text + option.desc[0] + option.desc.slice(1).map(line => {
56+
for (var i = 0; i < indent; ++i)
57+
line = " " + line;
58+
return "\n" + line;
59+
}).join(""));
60+
} else
61+
options.push(text + option.desc);
5462
});
5563
console.log([
5664
"Version " + version,
@@ -135,6 +143,11 @@ if (args.validate)
135143
process.exit(1);
136144
}
137145

146+
if (args.trapMode === "clamp")
147+
module.runPasses([ "trap-mode-clamp" ]);
148+
else if (args.trapMode === "js")
149+
module.runPasses([ "trap-mode-js" ]);
150+
138151
if (args.optimize)
139152
module.optimize();
140153

Diff for: bin/asc.json

+14-4
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020
"aliases": [ "c", "check" ]
2121
},
2222
"outFile": {
23-
"desc": "Specifies the output file.",
23+
"desc": "Specifies the output file. Format is determined by file extension.",
2424
"type": "string",
2525
"aliases": [ "o" ]
2626
},
2727
"binaryFile": {
28-
"desc": "Specifies the binary format output file.",
28+
"desc": "Specifies the binary format output file (.wasm).",
2929
"type": "string",
3030
"aliases": [ "b" ]
3131
},
3232
"textFile": {
33-
"desc": "Specifies the text format output file.",
33+
"desc": "Specifies the text format output file (.wast).",
3434
"type": "string",
3535
"aliases": [ "t" ]
3636
},
3737
"asmjsFile": {
38-
"desc": "Specifies the asm.js format output file.",
38+
"desc": "Specifies the asm.js format output file (.js).",
3939
"type": "string",
4040
"aliases": [ "a" ]
4141
},
@@ -46,5 +46,15 @@
4646
"noDebug": {
4747
"desc": "Disables assertions.",
4848
"type": "boolean"
49+
},
50+
"trapMode": {
51+
"desc": [
52+
"Sets the trap mode to use.",
53+
"none Do not modify trapping operations. This is the default.",
54+
"clamp Replace trapping operations with clamping semantics.",
55+
"js Replace trapping operations with JS semantics."
56+
],
57+
"type": "string",
58+
"default": "none"
4959
}
5060
}

Diff for: src/compiler.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ export class Compiler extends DiagnosticEmitter {
393393
if (getExpressionId(initializer) != ExpressionId.Const) {
394394
initializer = this.precomputeExpressionRef(initializer);
395395
if (getExpressionId(initializer) != ExpressionId.Const) {
396-
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
396+
if (element.isConstant)
397+
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
397398
initializeInStart = true;
398399
}
399400
}
@@ -407,7 +408,8 @@ export class Compiler extends DiagnosticEmitter {
407408
this.module.createGetGlobal(previousValue.internalName, NativeType.I32),
408409
this.module.createI32(1)
409410
);
410-
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, val.declaration.range);
411+
if (element.isConstant)
412+
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, val.declaration.range);
411413
initializeInStart = true;
412414
}
413415
if (initializeInStart) {

Diff for: src/module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ export class Module {
702702

703703
optimize(func: FunctionRef = 0): void {
704704
// see: https://github.com/WebAssembly/binaryen/issues/1331#issuecomment-350328175
705-
this.runPasses([ "flatten", "ssa" ], func);
705+
// this.runPasses([ "flatten", "ssa" ], func);
706706
if (func) {
707707
_BinaryenFunctionOptimize(func, this.ref);
708708
} else {

Diff for: tests/binaryen/export-import.js

-13
This file was deleted.

Diff for: tests/compiler/declare.optimized.wast

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(module
2+
(type $v (func))
3+
(import "env" "external" (func $declare/external))
24
(memory $0 1)
3-
(data (i32.const 4) "\08")
5+
(export "external" (func $declare/external))
46
(export "memory" (memory $0))
57
)

Diff for: tests/compiler/declare.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
declare function external(): void;
22

3-
// "unexpected false: module function exports must be found"
4-
// see: https://github.com/WebAssembly/binaryen/issues/1325
53
export { external };

Diff for: tests/compiler/do.optimized.wast

+8-18
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,37 @@
55
(export "loopDoInDo" (func $do/loopDoInDo))
66
(export "memory" (memory $0))
77
(func $do/loopDo (; 0 ;) (type $iv) (param $0 i32)
8-
(local $1 i32)
9-
(set_local $1
10-
(get_local $0)
11-
)
128
(loop $continue|0
139
(br_if $continue|0
14-
(tee_local $1
10+
(tee_local $0
1511
(i32.sub
16-
(get_local $1)
12+
(get_local $0)
1713
(i32.const 1)
1814
)
1915
)
2016
)
2117
)
2218
)
2319
(func $do/loopDoInDo (; 1 ;) (type $iv) (param $0 i32)
24-
(local $1 i32)
25-
(local $2 i32)
2620
(loop $continue|0
27-
(set_local $1
21+
(set_local $0
2822
(i32.sub
2923
(get_local $0)
3024
(i32.const 1)
3125
)
3226
)
3327
(loop $continue|1
3428
(br_if $continue|1
35-
(tee_local $2
36-
(tee_local $1
37-
(tee_local $0
38-
(i32.sub
39-
(get_local $1)
40-
(i32.const 1)
41-
)
42-
)
29+
(tee_local $0
30+
(i32.sub
31+
(get_local $0)
32+
(i32.const 1)
4333
)
4434
)
4535
)
4636
)
4737
(br_if $continue|0
48-
(get_local $2)
38+
(get_local $0)
4939
)
5040
)
5141
)

Diff for: tests/compiler/game-of-life.optimized.wast

+24-30
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
(get_local $4)
101101
(get_global $game-of-life/w)
102102
)
103-
(tee_local $3
103+
(tee_local $2
104104
(select
105105
(i32.sub
106106
(get_local $1)
@@ -128,7 +128,7 @@
128128
(get_local $4)
129129
(get_global $game-of-life/w)
130130
)
131-
(tee_local $2
131+
(tee_local $3
132132
(select
133133
(i32.const 0)
134134
(i32.add
@@ -150,7 +150,7 @@
150150
(get_local $0)
151151
(get_global $game-of-life/w)
152152
)
153-
(get_local $3)
153+
(get_local $2)
154154
)
155155
)
156156
)
@@ -160,7 +160,7 @@
160160
(get_local $0)
161161
(get_global $game-of-life/w)
162162
)
163-
(get_local $2)
163+
(get_local $3)
164164
)
165165
)
166166
)
@@ -170,7 +170,7 @@
170170
(get_local $5)
171171
(get_global $game-of-life/w)
172172
)
173-
(get_local $3)
173+
(get_local $2)
174174
)
175175
)
176176
)
@@ -190,7 +190,7 @@
190190
(get_local $5)
191191
(get_global $game-of-life/w)
192192
)
193-
(get_local $2)
193+
(get_local $3)
194194
)
195195
)
196196
)
@@ -205,38 +205,32 @@
205205
(get_local $1)
206206
)
207207
)
208-
(block
209-
(if
210-
(i32.eqz
211-
(tee_local $3
212-
(i32.lt_s
213-
(get_local $2)
214-
(i32.const 2)
215-
)
216-
)
217-
)
218-
(set_local $3
219-
(i32.gt_s
208+
(if
209+
(if (result i32)
210+
(tee_local $3
211+
(i32.lt_s
220212
(get_local $2)
221-
(i32.const 3)
213+
(i32.const 2)
222214
)
223215
)
224-
)
225-
(if
226216
(get_local $3)
227-
(i32.store8
217+
(i32.gt_s
218+
(get_local $2)
219+
(i32.const 3)
220+
)
221+
)
222+
(i32.store8
223+
(i32.add
228224
(i32.add
229-
(i32.add
230-
(get_global $game-of-life/s)
231-
(i32.mul
232-
(get_local $0)
233-
(get_global $game-of-life/w)
234-
)
225+
(get_global $game-of-life/s)
226+
(i32.mul
227+
(get_local $0)
228+
(get_global $game-of-life/w)
235229
)
236-
(get_local $1)
237230
)
238-
(i32.const 0)
231+
(get_local $1)
239232
)
233+
(i32.const 0)
240234
)
241235
)
242236
(if

0 commit comments

Comments
 (0)