Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
995 changes: 240 additions & 755 deletions bin/binaryen.js

Large diffs are not rendered by default.

894 changes: 171 additions & 723 deletions bin/wasm.js

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
return preserveStack(function() {
return Module['_BinaryenBlock'](module, name ? strToStack(name) : 0,
i32sToStack(children), children.length,
typeof type !== 'undefined' ? type : Module['undefined']);
typeof type !== 'undefined' ? type : Module['none']);
});
};
this['if'] = function(condition, ifTrue, ifFalse) {
Expand All @@ -228,11 +228,15 @@
return Module['_BinaryenLoop'](module, strToStack(label), body);
});
};
this['break'] = function(label, condition, value) {
this['break'] = this['br'] = function(label, condition, value) {
return preserveStack(function() {
return Module['_BinaryenBreak'](module, strToStack(label), condition, value);
});
};
this['br_if'] = function(label, condition, value) {
assert(condition);
return this['br'](label, condition, value);
};
this['switch'] = function(names, defaultName, condition, value) {
return preserveStack(function() {
var namei32s = [];
Expand All @@ -248,38 +252,38 @@
return Module['_BinaryenCall'](module, strToStack(name), i32sToStack(operands), operands.length, type);
});
};
this['callImport'] = function(name, operands, type) {
this['callImport'] = this['call_import'] = function(name, operands, type) {
return preserveStack(function() {
return Module['_BinaryenCallImport'](module, strToStack(name), i32sToStack(operands), operands.length, type);
});
};
this['callIndirect'] = function(target, operands, type) {
this['callIndirect'] = this['call_indirect'] = function(target, operands, type) {
return preserveStack(function() {
return Module['_BinaryenCallIndirect'](module, target, i32sToStack(operands), operands.length, strToStack(type));
});
};
this['getLocal'] = function(index, type) {
this['getLocal'] = this['get_local'] = function(index, type) {
return Module['_BinaryenGetLocal'](module, index, type);
};
this['setLocal'] = function(index, value) {
this['setLocal'] = this['set_local'] = this['set_local'] = function(index, value) {
return Module['_BinaryenSetLocal'](module, index, value);
};
this['teeLocal'] = function(index, value) {
this['teeLocal'] = this['tee_local'] = function(index, value) {
return Module['_BinaryenTeeLocal'](module, index, value);
};
this['getGlobal'] = function(name, type) {
this['getGlobal'] = this['get_global'] = function(name, type) {
return Module['_BinaryenGetGlobal'](module, strToStack(name), type);
}
this['setGlobal'] = function(name, value) {
this['setGlobal'] = this['set_global'] = function(name, value) {
return Module['_BinaryenSetGlobal'](module, strToStack(name), value);
}
this['currentMemory'] = function() {
this['currentMemory'] = this['current_memory'] = function() {
return Module['_BinaryenHost'](module, Module['CurrentMemory']);
}
this['growMemory'] = function(value) {
this['growMemory'] = this['grow_memory'] = function(value) {
return Module['_BinaryenHost'](module, Module['GrowMemory'], null, i32sToStack([value]), 1);
}
this['hasFeature'] = function(name) {
this['hasFeature'] = this['has_feature'] = function(name) {
return Module['_BinaryenHost'](module, Module['HasFeature'], strToStack(name));
}

Expand Down Expand Up @@ -1278,4 +1282,4 @@
typeof window !== "undefined" && window ||
this)["Binaryen"] = Module;

})();
})();
10 changes: 5 additions & 5 deletions test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ int main() {
expressions[190] = BinaryenBinary(the_module, 62, expressions[188], expressions[189]);
{
BinaryenExpressionRef children[] = { 0 };
expressions[191] = BinaryenBlock(the_module, NULL, children, 0, BinaryenUndefined());
expressions[191] = BinaryenBlock(the_module, NULL, children, 0, 0);
}
expressions[192] = BinaryenIf(the_module, expressions[7], expressions[8], expressions[9]);
expressions[193] = BinaryenIf(the_module, expressions[10], expressions[11], expressions[0]);
Expand Down Expand Up @@ -1412,17 +1412,17 @@ getExpressionType=3
9.5
{
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] };
expressions[251] = BinaryenBlock(the_module, "the-value", children, 95, BinaryenUndefined());
expressions[251] = BinaryenBlock(the_module, "the-value", children, 95, 0);
}
expressions[252] = BinaryenDrop(the_module, expressions[251]);
{
BinaryenExpressionRef children[] = { expressions[252] };
expressions[253] = BinaryenBlock(the_module, "the-nothing", children, 1, BinaryenUndefined());
expressions[253] = BinaryenBlock(the_module, "the-nothing", children, 1, 0);
}
expressions[254] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
{
BinaryenExpressionRef children[] = { expressions[253], expressions[254] };
expressions[255] = BinaryenBlock(the_module, "the-body", children, 2, BinaryenUndefined());
expressions[255] = BinaryenBlock(the_module, "the-body", children, 2, 0);
}
{
BinaryenType varTypes[] = { 1 };
Expand Down Expand Up @@ -2445,7 +2445,7 @@ getExpressionType=3
expressions[139] = BinaryenReturn(the_module, expressions[138]);
{
BinaryenExpressionRef children[] = { expressions[137], expressions[139] };
expressions[140] = BinaryenBlock(the_module, "the-list", children, 2, BinaryenUndefined());
expressions[140] = BinaryenBlock(the_module, "the-list", children, 2, 0);
}
relooperBlocks[0] = RelooperAddBlock(the_relooper, expressions[140]);
expressions[141] = RelooperRenderAndDispose(the_relooper, relooperBlocks[0], 0, the_module);
Expand Down
75 changes: 75 additions & 0 deletions test/binaryen.js/sieve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Create a module to work on
var module = new Binaryen.Module();

// Set a memory of initially one page, maximum 100 pages
module.setMemory(1, 100);

// Create a function type for i32 (i32) (i.e., return i32, get an i32 param)
var ii = module.addFunctionType('i', Binaryen.i32, [Binaryen.i32]);

var body = module.block(
null,
[
// if the current memory size is too small, grow it
module.if(
module.i32.lt_u(
module.i32.mul(
module.current_memory(),
module.i32.const(65536)
),
module.get_local(0, Binaryen.i32)
),
module.grow_memory(
module.i32.sub(
module.i32.div_u(
module.i32.add(
module.get_local(0, Binaryen.i32),
module.i32.const(65535)
),
module.i32.const(65536)
),
module.current_memory()
)
)
),
// first, clear memory
module.set_local(1, module.i32.const(0)),
module.loop('clear', module.block(null, [
module.i32.store8(0, 1,
module.get_local(1, Binaryen.i32),
module.i32.const(0)
),
module.set_local(1, module.i32.add(
module.get_local(1, Binaryen.i32),
module.i32.const(1)
)),
module.br_if('clear', module.i32.eq(
module.get_local(1),
module.get_local(0)
))
])),
// perform the sieve TODO
// calculate how many primes there are
module.return(module.get_local(0, Binaryen.i32))
],
Binaryen.none
);

// Create the add function
// Note: no additional local variables (that's the [])
module.addFunction('sieve', ii, [Binaryen.i32], body);

// Export the function, so we can call it later (for simplicity we
// export it as the same name as it has internally)
module.addFunctionExport('sieve', 'sieve');

// Print out the text
console.log(module.emitText());

// Optimize the module! This removes the 'return', since the
// output of the add can just fall through
module.optimize();

// Print out the optimized module's text
console.log('optimized:\n\n' + module.emitText());

107 changes: 107 additions & 0 deletions test/binaryen.js/sieve.js.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
(module
(type $i (func (param i32) (result i32)))
(memory $0 1 100)
(export "sieve" (func $sieve))
(func $sieve (; 0 ;) (type $i) (param $0 i32) (result i32)
(local $1 i32)
(if
(i32.lt_u
(i32.mul
(current_memory)
(i32.const 65536)
)
(get_local $0)
)
(grow_memory
(i32.sub
(i32.div_u
(i32.add
(get_local $0)
(i32.const 65535)
)
(i32.const 65536)
)
(current_memory)
)
)
)
(set_local $1
(i32.const 0)
)
(loop $clear
(i32.store8
(get_local $1)
(i32.const 0)
)
(set_local $1
(i32.add
(get_local $1)
(i32.const 1)
)
)
(br_if $clear
(i32.eq
(get_local $1)
(get_local $0)
)
)
)
(return
(get_local $0)
)
)
)

optimized:

(module
(type $i (func (param i32) (result i32)))
(memory $0 1 100)
(export "sieve" (func $sieve))
(func $sieve (; 0 ;) (type $i) (param $0 i32) (result i32)
(local $1 i32)
(if
(i32.lt_u
(i32.mul
(current_memory)
(i32.const 65536)
)
(get_local $0)
)
(grow_memory
(i32.sub
(i32.div_u
(i32.add
(get_local $0)
(i32.const 65535)
)
(i32.const 65536)
)
(current_memory)
)
)
)
(set_local $1
(i32.const 0)
)
(loop $clear
(i32.store8
(get_local $1)
(i32.const 0)
)
(br_if $clear
(i32.eq
(tee_local $1
(i32.add
(get_local $1)
(i32.const 1)
)
)
(get_local $0)
)
)
)
(get_local $0)
)
)