summaryrefslogtreecommitdiff
path: root/src/wasm2asm.h
Commit message (Collapse)AuthorAgeFilesLines
* Rename `wasm2asm` to `wasm2js`, emit ESM by default (#1642)Alex Crichton2018-08-301-2322/+0
| | | | | | | | | | | | | | | | | | | | | | | | * Rename the `wasm2asm` tool to `wasm2js` This commit performs a relatively simple rename of the `wasm2asm` tool to `wasm2js`. The functionality of the tool doesn't change just yet but it's intended that we'll start generating an ES module instead of just an `asm.js` function soon. * wasm2js: Support `*.wasm` input files Previously `wasm2js` only supported `*.wast` files but to make it a bit easier to use in tooling pipelines this commit adds support for reading in a `*.wasm` file directly. Determining which parser to use depends on the input filename, where the binary parser is used with `*.wasm` files and the wast parser is used for all other files. * wasm2js: Emit ESM imports/exports by default This commit alters the default behavior of `wasm2js` to emit an ESM by default, either importing items from the environment or exporting. Items like initialization of memory are also handled here.
* wasm2asm: Fix and enable a large number of spec tests (#1558)Alex Crichton2018-05-291-198/+364
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Import `abort` from the environment * Add passing spec tests * Bind the abort function * wasm2asm: Fix name collisions Currently function names and local names can collide in namespaces, causing buggy results when a function intends to call another function but ends up using a local value as the target! This fix was required to enable the `fac` spec test * wasm2asm: Get multiple modules in one file working The spec tests seem to have multiple modules defined in some tests and the invocations all use the most recently defined module. This commit updates the `--allow-asserts` mode of wasm2asm to work with this mode of tests, enabling us to enable more spec tests for wasm2asm. * wasm2asm: Enable the float_literals spec test This needed to be modified to account for how JS engines don't work with NaN bits the same way, but it's otherwise largely the same test. Additionally it turns out that asm.js doesn't accept either `Infinity` or `NaN` ambient globals so they needed to get imported through the `global` variable rather than defined as literals in code * wasm2asm: Fix function pointer invocations This commit fixes invocations of functions through function pointers as previously the table names on lookup and definition were mismatched. Both tables now go through signature-based namification rather than athe name of the type itself. Overall this enables a slew of spec tests * wasm2asm: Enable the left-to-right spec test There were two small bugs in the order of evaluation of operators with wasm2asm. The `select` instruction would sometimes evaluate the condition first when it was supposed to be last. Similarly a `call_indirect` instruction would evaluate the function pointer first when it was supposed to be evaluated last. The `select` instruction case was a relatively small fix but the one for `call_indirect` was a bit more pessimized to generate some temporaries. Hopefully if this becomes up a problem it can be tightened up. * wasm2asm: Fix signed load promotions of 64-bit ints This commit enables the `endianness` spec test which revealed a bug in 64-bit loads from smaller sizes which were signed. Previously the upper bits of the 64-bit number were all set to zero but the fix was for signed loads to have all the upper bits match the highest bit of the low 32 bits that we load. * wasm2asm: Enable the `stack` spec test Internally the spec test uses a mixture of the s-expression syntax and the wat syntax, so this is copied over into the `wasm2asm` folder after going through `wat2wasm` to ensure it's consistent for binaryen. * wasm2asm: Fix unaligned loads/stores of floats Replace these operations in `RemoveNonJSOps` by using reinterpretation to translate floats to integers and then use the existing code for unaligned loads/stores of integers. * wasm2asm: Fix a tricky grow_memory codegen bug This commit fixes a tricky codegen bug found in the `grow_memory` instruction. Specifically if you stored the result of `grow_memory` immediately into memory it would look like: HEAP32[..] = __wasm_grow_memory(..); Here though it looks like JS evaluates the destination *before* the grow function is called, but the grow function will invalidate the destination! Furthermore this is actually generalizable to all function calls: HEAP32[..] = foo(..); Because any function could transitively call `grow_memory`. This commit fixes the issue by ensuring that store instructions are always considered statements, unconditionally evaluating the value into a temporary and then storing that into the destination. While a bit of a pessmimization for now it should hopefully fix the bug here. * wasm2asm: Handle offsets in tables This commit fixes initializing tables whose elements have an initial offset. This should hopefully help fix some more Rust code which has all function pointers offset by default! * Update tests * Tweak * location on types * Rename entries of NameScope and document fromName * Comment on lowercase names * Update compiled JS * Update js test output expectation * Rename NameScope::Global to NameScope::Top * Switch to `enum class` * Switch to `Fatal()` * Add TODO for when asm.js is no longer generated
* wasm2asm: Finish i64 lowering operations (#1563)Alex Crichton2018-05-251-21/+32
| | | | | | | | | | | | | | | | | * wasm2asm: Finish i64 lowering operations This commit finishes out lowering i64 operations to JS with implementations of division and remainder for JS. The primary change here is to have these compiled from Rust to wasm and then have them "linked in" via intrinsics. The `RemoveNonJSOps` pass has been updated to include some of what `I64ToI32Lowering` was previously doing, basically replacing some instructions with calls to intrinsics. The intrinsics are now all tracked in one location. Hopefully the intrinsics don't need to be regenerated too much, but for posterity the source currently [lives in a gist][gist], although I suspect that gist won't continue to compile and work as-is for all of time. [gist]: https://gist.github.com/alexcrichton/e7ea67bcdd17ce4b6254e66f77165690
* wasm2asm: Finish f32/f64 operations (#1554)Alex Crichton2018-05-191-191/+29
|
* wasm2asm: Implement float<->int conversions (#1550)Alex Crichton2018-05-161-18/+234
| | | | | | | | | This commit lifts the same conversion strategy that `emcc` takes to convert between floats point numbers and integers, and it should implement all the various matrices of i32/u32/i64/u64 to f32/f64 Some refactoring was performed in the i64->i32 pass to allow for temporary variables to get allocated which have types other than i32, but otherwise this contains a pretty direct translation of `emcc`'s operations to `wasm2asm`.
* wasm2asm: Implement f32/f64.copysign (#1551)Alex Crichton2018-05-151-0/+1
| | | | | | This commit implements the `copysign` instruction for the wasm2asm binary. The implementation here is a new pass which wholesale replaces `copysign` instructions with the equivalent bit ops and reinterpretation instructions. It's intended that this matches Emscripten's implementation of lowering here.
* wasm2asm: Add math aliases for floor, ceil and sqrt (#1549)Daniel Wirtz2018-05-141-0/+3
|
* wasm2asm: Implement reinterpretation instructions (#1547)Alex Crichton2018-05-131-0/+24
| | | | | | | | | | | | | As mentioned in #1458 a naive implementation of these instructions is to round trip the value through address 0 in linear memory. Also pointed out in #1458 this isn't necessarily valid for all languages. For now, though, languages like Rust, C, and C++ would likely be horribly broken if valid data could be stored at low addresses, so this commit goes ahead and adds an implementation of the reinterpretation instructions by traveling data through address 0. This will likely need an update if a language comes a long which can validly store data in the first 8 bytes of linear memory, but it seems like that won't happen in the near future. Closes #1458
* reorder locals in wasm2asm (#1482)Nathan Froyd2018-03-221-0/+1
| | | | | | | The documentation for the simplify-locals pass suggests running reorder-locals after it to clean up unnecessary locals. wasm2asm wasn't doing this, which meant that generated code had a number of unused variables. A good minimizer will probably clean that up, but let's go ahead and clean it up in wasm2asm itself.
* create Math_{min,max} variables for wasm2asm-generated JS (#1476)Nathan Froyd2018-03-161-0/+2
| | | | | | | | | We were using Math_{min,max} in wasm2asm-generated files without declaring said functions. This decision created problems for tests, because Math_min (resp. max) would first be used on f32s, thus returning f32, and then validation would fail when it was used on f64s. The resulting changes make wasm2asm tests pass with MOZJS asm.js validation, which moves #1443 forward.
* eliminate multiple return statements for wasm2asm functions (#1448)Nathan Froyd2018-03-011-2/+3
| | | | This change eliminates one issue that prevents asm.js validation of the generated code, see #1443.
* Improve name mangling of asm.js identifiers (#1433)Daniel Wirtz2018-02-211-20/+14
| | | | | | Also refactors mangling to its own file so it can be reused by generators and consumers, i.e., where it is important to know that an import must be named 'switch_' where it otherwise would be 'switch'. * Update tests and JS dist files
* wasm2asm fixes (#1436)Alon Zakai2018-02-201-12/+0
| | | | | | * don't look for asm.js compilation message if almost asm * fix wasm2asm f32 operations
* Add global initializers to wasm2asm (#1434)Daniel Wirtz2018-02-201-0/+38
| | | | Adds support for (constant i32/f32/f64) global variable initializers, which were previously ignored though get_global/set_global are supported.
* better handling of float ops in wasm2asm (#1427)Nathan Froyd2018-02-151-101/+176
| | | | | | | | | | | | | | | | | | | | * explicitly handle binary float operations in processFunctionBody We weren't handling them before, but it wasn't obvious. Make the (non-) handling of them explicit in the code. We'll add handlers for them shortly. * add handling for simple binary float operations min, max, and copysign will require more sophisticated handling. * add handling for float comparisons * move float min/max handling to the correct place It was previously grouped with the i32 ops. * handle float promotion and demotion
* Update wasm2asm to generate "almost asm" if grow_memory is used (#1340)Kris Selden2018-02-061-6/+222
| | | | | | | | | | * Allow wasm2asm to generate "almost asm" If grow_memory, current_memory or export memory is used then generate "almost asm" with memory growth support. * Log reason for "almost asm" to stderr
* Rename WasmType => Type (#1398)Alon Zakai2018-02-021-11/+11
| | | | * rename WasmType to Type. it's in the wasm:: namespace anyhow, and without Wasm- it fits in better alongside Index, Address, Expression, Module, etc.
* notation change: AST => IR (#1245)Alon Zakai2017-10-241-1/+1
| | | The IR is indeed a tree, but not an "abstract syntax tree" since there is no language for which it is the syntax (except in the most trivial and meaningless sense).
* use simplify-locals in wasm2asm, so the output is not horribly verbose with ↵Alon Zakai2017-10-201-0/+1
| | | | los of extra inefficient variables. this is more similar to the output we had before the flatten rewrite (#1229)
* Flattening rewrite (#1201)Alon Zakai2017-10-031-1/+1
| | | | | | | | | | | | | | | | | | | | Rename flatten-control-flow to flatten, which now flattens everything, not just control flow, so e.g. (i32.add (call $x) (call $y) ) ==> (block (set_local $temp_x (call $x)) (set_local $temp_y (call $y)) (i32.add (get_local $x) (get_local $y) ) ) This uses more locals than before, but is much simpler and avoids a bunch of corner cases and fuzz bugs the old one hit. We can optimize later if necessary.
* wasm2asm fixes for coercion on rotl/rotr/ctz/popcnt calls, and refactor the ↵Alon Zakai2017-09-231-13/+26
| | | | name of constants to include the WASM_ prefix so as not to collide with constants for the unprefixed values (#1199)
* i64 to i32 lowering for wasm2asm (#1134)Thomas Lively2017-09-011-108/+226
|
* Fix uninitialized members in Wasm2AsmBuilder::Flags (#1152)Sam Clegg2017-08-291-2/+2
|
* Initial asm.js output for binaryen-c / binaryen.js (#1136)Daniel Wirtz2017-08-241-5/+5
| | | | * Added BinaryenModulePrintAsmjs (using wasm2asm) + Module#emitAsmjs JS binding
* wasm2asm test generation (#1124)Thomas Lively2017-08-161-51/+229
| | | | | | | | | | | | | | | | | * Translate assert_return invokes to asm * Translate assert_trap tests to JS * Enable wasm2asm tests * Fix wasm2asm translation of store * Update ubuntu nodejs in Travis * Free JSPrinter buffer * Use unique_ptr for Functions to prevent leaks * Add tests for assert translation
* wasm2asm i32 arithmetic support (#1120)Thomas Lively2017-08-071-26/+217
| | | * Rotations, popcnt, ctz, etc
* Get wasm2asm building again (#1107)Thomas Lively2017-08-021-102/+233
| | | | | | | | | | | | | | | | | | * Get wasm2asm building again Updates CMakeLists.txt to have wasm2asm built by default, updates wasm2asm.h to account for recent interface changes, and restores JSPrinter functionality. * Implement splice for array values * Clean up wasm2asm testing * Print semicolons after statements in blocks * Cleanups and semicolons for condition arms * Prettify semicolon emission
* Default Walker subclasses to using Visitor<SubType> (#921)jgravelle-google2017-02-231-1/+1
| | | | Most module walkers use PostWalker<T, Visitor<T>>, let that pattern be expressed as simply PostWalker<T>
* Harmonize the internal opcodes with the binary format (#433)Alon Zakai2016-05-031-3/+3
| | | | | | * harmonize the internal opcodes with the binary format, so they clearly parallel, and also this helps us avoid needing the type to disambiguate * comment on GetLocal in C API
* allocate only expressions in arenas - functions, imports, exports, function ↵Alon Zakai2016-04-271-5/+5
| | | | types, can more simply be held by unique_ptrs on the owning module. this avoids need to coordinate arena allocation for their elements, and only the far more plentiful expression nodes are a perf factor anyhow
* Split construction, scanning, and building phases of S2WasmBuilder (#400)Derek Schuff2016-04-271-1/+1
| | | | | | | | | | | Instead of doing all of the S2Wasm work in the constructor, split construction, scanning (to determine implemented functions) and building of the wasm module. This allows the linker to get the symbol information (e.g. implemented functions) without having to build an entire module (which will be useful for archives) and to allow the linker to link a new object into the existing one by building the wasm module in place on the existing module.
* add an ArenaVector for internal array allocations in expression nodesAlon Zakai2016-04-261-23/+31
|
* create a UnifiedExpressionVisitor for passes that want a single visitor ↵Alon Zakai2016-04-181-1/+1
| | | | function, to avoid confusion with having both visit* and visitExpression in a single pass (#357)
* index locals, so that get_local and set_local have just an index, and local ↵Alon Zakai2016-04-181-30/+31
| | | | names are kept on the Function object (#354)
* rename function locals, to params and vars, which together are all the ↵Alon Zakai2016-04-141-3/+3
| | | | locals. preparation for #336 (#349)
* De-recurse traversals (#333)Alon Zakai2016-04-111-3/+9
| | | | | | | | | | | | * refactor core walking to not recurse * add a simplify-locals test * reuse parent's non-branchey scan logic in SimpleExecutionWalker, reduce code duplication * update wasm.js * rename things following comments
* add apis for accessing module elementsAlon Zakai2016-04-041-1/+1
|
* br_table, no more casesAlon Zakai2016-03-071-25/+13
|
* Fix selectJF Bastien2016-02-051-11/+11
| | | | | | | | | The ordering changed in: https://github.com/WebAssembly/spec/pull/221 Which changed the spec tests, breaking sexpr-wasm because it pulls in the spec tests. This was then fixed: https://github.com/WebAssembly/sexpr-wasm-prototype/commit/23dc368148fc7827a603e3853f5a40287eb9effe Which in turn breaks when binaryen feeds sexpr-wasm .wast files with the old select operand ordering. Note that this PR has new failures when running the torture tests in binaryen-shell: the order of evaluation is correct in binaryen-shell but isn't emitted properly by LLVM in the .s files. This will require another patch to fix LLVM.
* save int bits in Literals, only bitcast to float/double on explicit requestAlon Zakai2016-02-021-4/+4
|
* Make Literal's ctors explicitJF Bastien2016-02-021-1/+1
| | | | This tends to avoid silly mistakes, which I'm worried about after adding an explicit ctor with the enum WasmType parameter. See 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices' rule #40 'avoid providing implicit conversion'.
* wasm2asm: use support/command-line.hJF Bastien2016-01-201-2/+7
|
* improve wasm2asm switch emitting, and add testcaseAlon Zakai2016-01-201-4/+9
|
* Some cleanup.Michael Bebenita2016-01-141-1/+1
|
* Use LLVM style static polymorphism for WasmVisitors.Michael Bebenita2016-01-131-36/+36
|
* Add Travis builds with sanitizersJF Bastien2016-01-101-1/+3
| | | | | | | | | | | | | | | | This triggers 5 independent build / test runs: - clang, no sanitizer; - clang, UB sanitizer; - clang, address sanitizer (disabled for now); - clang, thread sanitizer (disabled for now); - GCC. Enabling UBSan led to these changes: - Fix a bunch of undefined behavior throughout the code base. - Fix some tests that relied on that undefined behavior. - Make some of the tests easier to debug by printing their command line. - Add ubsan blacklist to work around libstdc++ bug. - Example testcase also needs sanitizer because libsupport.a uses it.
* Fix / uniformize include guardsJF Bastien2015-12-221-0/+5
|
* Fix warnings found by GCCJF Bastien2015-12-221-6/+6
| | | | | | My previous patch addressed all LLVM warnings, this one addresses all the GCC ones as well (mostly signed / unsigned mix). The patch also turns on -Wall -Werror.
* unaligned loads and stores in wasm2asmAlon Zakai2015-12-211-2/+85
|
* support load/store offsets in wasm2asmAlon Zakai2015-12-211-0/+6
|