Skip to content

Commit 22f1ce8

Browse files
authored
binaryen.js improvements (#1324)
* binaryen.js improvements: block default value is none, not undefined, and add text-format style aliases for things like getLocal (so you can write get_local as in the text format)
1 parent 0571fe3 commit 22f1ce8

File tree

6 files changed

+615
-1496
lines changed

6 files changed

+615
-1496
lines changed

bin/binaryen.js

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

bin/wasm.js

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

src/js/binaryen.js-post.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
return preserveStack(function() {
218218
return Module['_BinaryenBlock'](module, name ? strToStack(name) : 0,
219219
i32sToStack(children), children.length,
220-
typeof type !== 'undefined' ? type : Module['undefined']);
220+
typeof type !== 'undefined' ? type : Module['none']);
221221
});
222222
};
223223
this['if'] = function(condition, ifTrue, ifFalse) {
@@ -228,11 +228,15 @@
228228
return Module['_BinaryenLoop'](module, strToStack(label), body);
229229
});
230230
};
231-
this['break'] = function(label, condition, value) {
231+
this['break'] = this['br'] = function(label, condition, value) {
232232
return preserveStack(function() {
233233
return Module['_BinaryenBreak'](module, strToStack(label), condition, value);
234234
});
235235
};
236+
this['br_if'] = function(label, condition, value) {
237+
assert(condition);
238+
return this['br'](label, condition, value);
239+
};
236240
this['switch'] = function(names, defaultName, condition, value) {
237241
return preserveStack(function() {
238242
var namei32s = [];
@@ -248,38 +252,38 @@
248252
return Module['_BinaryenCall'](module, strToStack(name), i32sToStack(operands), operands.length, type);
249253
});
250254
};
251-
this['callImport'] = function(name, operands, type) {
255+
this['callImport'] = this['call_import'] = function(name, operands, type) {
252256
return preserveStack(function() {
253257
return Module['_BinaryenCallImport'](module, strToStack(name), i32sToStack(operands), operands.length, type);
254258
});
255259
};
256-
this['callIndirect'] = function(target, operands, type) {
260+
this['callIndirect'] = this['call_indirect'] = function(target, operands, type) {
257261
return preserveStack(function() {
258262
return Module['_BinaryenCallIndirect'](module, target, i32sToStack(operands), operands.length, strToStack(type));
259263
});
260264
};
261-
this['getLocal'] = function(index, type) {
265+
this['getLocal'] = this['get_local'] = function(index, type) {
262266
return Module['_BinaryenGetLocal'](module, index, type);
263267
};
264-
this['setLocal'] = function(index, value) {
268+
this['setLocal'] = this['set_local'] = this['set_local'] = function(index, value) {
265269
return Module['_BinaryenSetLocal'](module, index, value);
266270
};
267-
this['teeLocal'] = function(index, value) {
271+
this['teeLocal'] = this['tee_local'] = function(index, value) {
268272
return Module['_BinaryenTeeLocal'](module, index, value);
269273
};
270-
this['getGlobal'] = function(name, type) {
274+
this['getGlobal'] = this['get_global'] = function(name, type) {
271275
return Module['_BinaryenGetGlobal'](module, strToStack(name), type);
272276
}
273-
this['setGlobal'] = function(name, value) {
277+
this['setGlobal'] = this['set_global'] = function(name, value) {
274278
return Module['_BinaryenSetGlobal'](module, strToStack(name), value);
275279
}
276-
this['currentMemory'] = function() {
280+
this['currentMemory'] = this['current_memory'] = function() {
277281
return Module['_BinaryenHost'](module, Module['CurrentMemory']);
278282
}
279-
this['growMemory'] = function(value) {
283+
this['growMemory'] = this['grow_memory'] = function(value) {
280284
return Module['_BinaryenHost'](module, Module['GrowMemory'], null, i32sToStack([value]), 1);
281285
}
282-
this['hasFeature'] = function(name) {
286+
this['hasFeature'] = this['has_feature'] = function(name) {
283287
return Module['_BinaryenHost'](module, Module['HasFeature'], strToStack(name));
284288
}
285289

@@ -1278,4 +1282,4 @@
12781282
typeof window !== "undefined" && window ||
12791283
this)["Binaryen"] = Module;
12801284

1281-
})();
1285+
})();

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ int main() {
13161316
expressions[190] = BinaryenBinary(the_module, 62, expressions[188], expressions[189]);
13171317
{
13181318
BinaryenExpressionRef children[] = { 0 };
1319-
expressions[191] = BinaryenBlock(the_module, NULL, children, 0, BinaryenUndefined());
1319+
expressions[191] = BinaryenBlock(the_module, NULL, children, 0, 0);
13201320
}
13211321
expressions[192] = BinaryenIf(the_module, expressions[7], expressions[8], expressions[9]);
13221322
expressions[193] = BinaryenIf(the_module, expressions[10], expressions[11], expressions[0]);
@@ -1412,17 +1412,17 @@ getExpressionType=3
14121412
9.5
14131413
{
14141414
BinaryenExpressionRef children[] = { expressions[24], expressions[26], expressions[28], expressions[30], expressions[32], expressions[34], expressions[36], expressions[38], expressions[40], expressions[42], expressions[44], expressions[46], expressions[48], expressions[50], expressions[52], expressions[54], expressions[56], expressions[58], expressions[60], expressions[62], expressions[64], expressions[66], expressions[68], expressions[70], expressions[72], expressions[74], expressions[76], expressions[78], expressions[80], expressions[82], expressions[84], expressions[86], expressions[88], expressions[90], expressions[92], expressions[94], expressions[97], expressions[100], expressions[103], expressions[106], expressions[109], expressions[112], expressions[115], expressions[118], expressions[121], expressions[124], expressions[127], expressions[130], expressions[133], expressions[136], expressions[139], expressions[142], expressions[145], expressions[148], expressions[151], expressions[154], expressions[157], expressions[160], expressions[163], expressions[166], expressions[169], expressions[172], expressions[175], expressions[178], expressions[181], expressions[184], expressions[187], expressions[190], expressions[191], expressions[192], expressions[193], expressions[195], expressions[197], expressions[198], expressions[200], expressions[202], expressions[203], expressions[204], expressions[206], expressions[212], expressions[217], expressions[224], expressions[226], expressions[228], expressions[231], expressions[233], expressions[235], expressions[237], expressions[239], expressions[240], expressions[241], expressions[242], expressions[244], expressions[245], expressions[246] };
1415-
expressions[251] = BinaryenBlock(the_module, "the-value", children, 95, BinaryenUndefined());
1415+
expressions[251] = BinaryenBlock(the_module, "the-value", children, 95, 0);
14161416
}
14171417
expressions[252] = BinaryenDrop(the_module, expressions[251]);
14181418
{
14191419
BinaryenExpressionRef children[] = { expressions[252] };
1420-
expressions[253] = BinaryenBlock(the_module, "the-nothing", children, 1, BinaryenUndefined());
1420+
expressions[253] = BinaryenBlock(the_module, "the-nothing", children, 1, 0);
14211421
}
14221422
expressions[254] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
14231423
{
14241424
BinaryenExpressionRef children[] = { expressions[253], expressions[254] };
1425-
expressions[255] = BinaryenBlock(the_module, "the-body", children, 2, BinaryenUndefined());
1425+
expressions[255] = BinaryenBlock(the_module, "the-body", children, 2, 0);
14261426
}
14271427
{
14281428
BinaryenType varTypes[] = { 1 };
@@ -2445,7 +2445,7 @@ getExpressionType=3
24452445
expressions[139] = BinaryenReturn(the_module, expressions[138]);
24462446
{
24472447
BinaryenExpressionRef children[] = { expressions[137], expressions[139] };
2448-
expressions[140] = BinaryenBlock(the_module, "the-list", children, 2, BinaryenUndefined());
2448+
expressions[140] = BinaryenBlock(the_module, "the-list", children, 2, 0);
24492449
}
24502450
relooperBlocks[0] = RelooperAddBlock(the_relooper, expressions[140]);
24512451
expressions[141] = RelooperRenderAndDispose(the_relooper, relooperBlocks[0], 0, the_module);

test/binaryen.js/sieve.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Create a module to work on
2+
var module = new Binaryen.Module();
3+
4+
// Set a memory of initially one page, maximum 100 pages
5+
module.setMemory(1, 100);
6+
7+
// Create a function type for i32 (i32) (i.e., return i32, get an i32 param)
8+
var ii = module.addFunctionType('i', Binaryen.i32, [Binaryen.i32]);
9+
10+
var body = module.block(
11+
null,
12+
[
13+
// if the current memory size is too small, grow it
14+
module.if(
15+
module.i32.lt_u(
16+
module.i32.mul(
17+
module.current_memory(),
18+
module.i32.const(65536)
19+
),
20+
module.get_local(0, Binaryen.i32)
21+
),
22+
module.grow_memory(
23+
module.i32.sub(
24+
module.i32.div_u(
25+
module.i32.add(
26+
module.get_local(0, Binaryen.i32),
27+
module.i32.const(65535)
28+
),
29+
module.i32.const(65536)
30+
),
31+
module.current_memory()
32+
)
33+
)
34+
),
35+
// first, clear memory
36+
module.set_local(1, module.i32.const(0)),
37+
module.loop('clear', module.block(null, [
38+
module.i32.store8(0, 1,
39+
module.get_local(1, Binaryen.i32),
40+
module.i32.const(0)
41+
),
42+
module.set_local(1, module.i32.add(
43+
module.get_local(1, Binaryen.i32),
44+
module.i32.const(1)
45+
)),
46+
module.br_if('clear', module.i32.eq(
47+
module.get_local(1),
48+
module.get_local(0)
49+
))
50+
])),
51+
// perform the sieve TODO
52+
// calculate how many primes there are
53+
module.return(module.get_local(0, Binaryen.i32))
54+
],
55+
Binaryen.none
56+
);
57+
58+
// Create the add function
59+
// Note: no additional local variables (that's the [])
60+
module.addFunction('sieve', ii, [Binaryen.i32], body);
61+
62+
// Export the function, so we can call it later (for simplicity we
63+
// export it as the same name as it has internally)
64+
module.addFunctionExport('sieve', 'sieve');
65+
66+
// Print out the text
67+
console.log(module.emitText());
68+
69+
// Optimize the module! This removes the 'return', since the
70+
// output of the add can just fall through
71+
module.optimize();
72+
73+
// Print out the optimized module's text
74+
console.log('optimized:\n\n' + module.emitText());
75+

test/binaryen.js/sieve.js.txt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
(module
2+
(type $i (func (param i32) (result i32)))
3+
(memory $0 1 100)
4+
(export "sieve" (func $sieve))
5+
(func $sieve (; 0 ;) (type $i) (param $0 i32) (result i32)
6+
(local $1 i32)
7+
(if
8+
(i32.lt_u
9+
(i32.mul
10+
(current_memory)
11+
(i32.const 65536)
12+
)
13+
(get_local $0)
14+
)
15+
(grow_memory
16+
(i32.sub
17+
(i32.div_u
18+
(i32.add
19+
(get_local $0)
20+
(i32.const 65535)
21+
)
22+
(i32.const 65536)
23+
)
24+
(current_memory)
25+
)
26+
)
27+
)
28+
(set_local $1
29+
(i32.const 0)
30+
)
31+
(loop $clear
32+
(i32.store8
33+
(get_local $1)
34+
(i32.const 0)
35+
)
36+
(set_local $1
37+
(i32.add
38+
(get_local $1)
39+
(i32.const 1)
40+
)
41+
)
42+
(br_if $clear
43+
(i32.eq
44+
(get_local $1)
45+
(get_local $0)
46+
)
47+
)
48+
)
49+
(return
50+
(get_local $0)
51+
)
52+
)
53+
)
54+
55+
optimized:
56+
57+
(module
58+
(type $i (func (param i32) (result i32)))
59+
(memory $0 1 100)
60+
(export "sieve" (func $sieve))
61+
(func $sieve (; 0 ;) (type $i) (param $0 i32) (result i32)
62+
(local $1 i32)
63+
(if
64+
(i32.lt_u
65+
(i32.mul
66+
(current_memory)
67+
(i32.const 65536)
68+
)
69+
(get_local $0)
70+
)
71+
(grow_memory
72+
(i32.sub
73+
(i32.div_u
74+
(i32.add
75+
(get_local $0)
76+
(i32.const 65535)
77+
)
78+
(i32.const 65536)
79+
)
80+
(current_memory)
81+
)
82+
)
83+
)
84+
(set_local $1
85+
(i32.const 0)
86+
)
87+
(loop $clear
88+
(i32.store8
89+
(get_local $1)
90+
(i32.const 0)
91+
)
92+
(br_if $clear
93+
(i32.eq
94+
(tee_local $1
95+
(i32.add
96+
(get_local $1)
97+
(i32.const 1)
98+
)
99+
)
100+
(get_local $0)
101+
)
102+
)
103+
)
104+
(get_local $0)
105+
)
106+
)
107+

0 commit comments

Comments
 (0)