summaryrefslogtreecommitdiff
path: root/src/wasm-emscripten.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Function pointer cast emulation (#1468)Alon Zakai2018-03-131-664/+0
| | | | | | | | | | | This adds a pass that implements "function pointer cast emulation" - allows indirect calls to go through even if the number of arguments or their types is incorrect. That is undefined behavior in C/C++ but in practice somehow works in native archs. It is even relied upon in e.g. Python. Emscripten already has such emulation for asm.js, which also worked for asm2wasm. This implements something like it in binaryen which also allows the wasm backend to use it. As a result, Python should now be portable using the wasm backend. The mechanism used for the emulation is to make all indirect calls use a fixed number of arguments, all of type i64, and a return type of also i64. Thunks are then placed in the table which translate the arguments properly for the target, basically by reinterpreting to i64 and back. As a result, receiving an i64 when an i32 is sent will have the upper bits all zero, and the reverse would truncate the upper bits, etc. (Note that this is different than emscripten's existing emulation, which converts (as signed) to a double. That makes sense for JS where double's can contain all numeric values, but in wasm we have i64s. Also, bitwise conversion may be more like what native archs do anyhow. It is enough for Python.) Also adds validation for a function's type matching the function's actual params and result (surprised we didn't have that before, but we didn't, and there was even a place in the test suite where that was wrong). Also simplifies the build script by moving two cpp files into the wasm/ subdir, so they can be built once and shared between the various tools.
* Let s2wasm emit binary output (#1465)Jacob Gravelle2018-03-081-0/+9
| | | | | | | | | | | | | | | | | | | | * Emit invokeFuncs list as metadata * Refactor s2wasm to use ModuleWriter * Fix wasm-emscripten-finalize metadata output for binary output * Add a flag to emit binary from s2wasm NOTE: I chose to emit text by default, and binary behind a flag. This mismatches with asm2wasm (and the expectations of users of a "2wasm" tool), but doesn't break any existing users of s2wasm. If s2wasm is deprecated in favor of lld, this will be the least disruptive change, and we won't have to live with awkward defaults for too long. * Emit source maps in the binary output of s2wasm * Only emit binary with an outfile specified
* EM_JS binaryen support (#1410)Jacob Gravelle2018-02-261-58/+129
| | | | | | | | | | | * Emit EM_JS metadata * Include s2wasm-style em_js support * Change em_js metadata to be keyed on name * Add testcase for em_js, don't always emit emJsFuncs metadata * Better error handling for unexpectedly-formatted __em_js__ functions
* Generate all s2wasm metadata in binaryen (#1440)Jacob Gravelle2018-02-261-11/+55
| | | | | | | | | * Extract comma-handling logic into a lambda function * Start emitting all metadata from binaryen - handle declares and externs * Add implementedFunctions and exports metadata * Remove EM_ASM calls from declares
* Emscripten addFunction support for Wasm backend (#1395)Heejin Ahn2018-02-071-7/+127
| | | This adds necessary command line options for addFunction support, and generates required jsCall imports and generates jsCall thunk functions.
* First pass at LLD support for Emscripten (#1346)Jacob Gravelle2018-01-221-50/+89
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Skeleton of a beginning of o2wasm, WIP and probably not going to be used * Get building post-cherry-pick * ast->ir, remove commented out code, include a debug module print because linking * Read linking section, print emscripten metadata json * WasmBinaryWriter emits user sections on Module * Remove debugging prints, everything that isn't needed to build metadata * Rename o2wasm to lld-metadata * lld-metadata support for outputting to file * Use tables index instead of function index for initializer functions * Add lld-emscripten tool to add emscripten-runtime functions to wasm modules (built with lld) * Handle EM_ASM in lld-emscripten * Add a list of functions to forcibly export (for initializer functions) * Disable incorrect initializer function reading * Add error printing when parsing .o files in lld-metadata * Remove ';; METADATA: ' prefix from lld-metadata, output is now standalone json * Support em_asm consts that aren't at the start of a segment * Initial test framework for lld-metadata tool * Add em_asm test * Add support for WASM_INIT_FUNCS in the linking section * Remove reloc section parsing because it's unused * lld-emscripten can read and write text * Add test harness for lld-emscripten * Export all functions for now * Add missing lld test output * Add support for reading object files differently Only difference so far is in importing mutable globals being an object file representation for symbols, but invalid wasm. * Update help strings * Update linking tests for stackAlloc fix * Rename lld-emscripten,lld-metadata to wasm-emscripten-finalize,wasm-link-metadata * Add help text to header comments * auto& instead of auto & * Extract LinkType to abi/wasm-object.h * Remove special handling for wasm object file reading, allow mutable globals * Add braces around default switch case * Fix flake8 errors * Handle generating dyncall thunks for imports as well * Use explicit bool for stackPointerGlobal * Use glob patterns for lld file iteration * Use __wasm_call_ctors for all initializer functions
* Optimize out memory and table when possible (#1352)Alon Zakai2018-01-101-1/+3
| | | We can remove the memory/table (itself, or an import if imported) if they are not used. This is pretty minor on a large wasm file, but when reading small wasts it's very noticeable to have an unused memory and table all the time.
* Fix stackAlloc runtime function generation for wasm backend (#1348)Derek Schuff2018-01-041-5/+3
| | | | It was returning the top of the allocated space rather than the bottom. Fix taken from @tbfleming in kripken/emscripten#5974
* Decouple wasm-linker from Emscripten glue (#1293)Jacob Gravelle2017-11-151-80/+90
|
* Expressions should not appear twice in the ast (#1191)Alon Zakai2017-09-181-1/+2
|
* Runtime.stackAlloc should grow down for wasm (#1073)jgravelle-google2017-06-281-8/+4
| | | | | | * Runtime.stackAlloc should grow down for wasm * stackAlloc should align properly; update tests
* S2wasm runtime funcs (#1027)jgravelle-google2017-06-051-14/+127
| | | | | | | | | | | | | | | | * Generate stackSave function in s2wasm * Generate stackAlloc in s2wasm * Generate stackRestore in s2wasm * Update dot_s tests for runtime functions * Add s2wasm check for exporting runtime functions * Fix flake8 for s2wasm.py * Rename wasmBuilder to builder
* Refactor AsmConstWalker to use smaller subfunctions (#923)jgravelle-google2017-02-231-35/+61
| | | | | | * Refactor AsmConstWalker to use smaller subfunctions * Replace cashew::IStrings with Names
* 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>
* Fully handle EM_ASM in s2wasm (#910)jgravelle-google2017-02-231-3/+22
| | | | | | | | | | | | * Fully handle EM_ASM in s2wasm * Iterate with size_ts, remember to erase from importsMap as well * Fix dot_s test EM_ASM signatures * Move Name out to its own file, support/name.h * Move removeImportsWithSubstring out of Module class
* clean up raw pointer import->functionType, make it a Name like everything ↵Alon Zakai2017-02-171-1/+1
| | | | else (#915)
* Generate global imports separately from emscripten glue (#852)jgravelle-google2016-12-011-11/+0
|
* Handle importing globals in s2wasm (#843)jgravelle-google2016-11-301-0/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Handle importing globals in s2wasm * Make importedGlobals a set of Names, make Names hashable * Revert "Make importedGlobals a set of Names, make Names hashable" This reverts commit 1d0ca7a5e3839b15ca60593330979864c9c3ed60. * Refactor relocation parsing to handle expressions directly * PR Feedback - Move comment where it belongs - Add comment about ownership to addRelocation - Remove do-nothing parseImportGlobal * Reword "imported globals" to "imported objects" - Flip isObjectImported to isObjectImplemented, for consistency * Add tests for s2wasm globals. Also implement import relocation expression handling * Simplify globals.s test * Fix memory leak of relocation * Use unique_ptr instead of delete in getRelocatableExpression
* Refactor Import::Kind and Export::Kind into an ExternalKind enum class (#725)Alon Zakai2016-10-031-2/+2
|
* In AsmConstWalker, don't assume a segment exists (#697)jgravelle-google2016-09-141-2/+10
| | | | | | | | | | It's possible to generate an EM_ASM call with empty contents (due to ifdefs, for example), and this gets converted to an empty string. AsmConstWalker assumes that by this point any addresses it is pointing to have a corresponding data section, which is reasonable. However in the case of an empty string, we don't create a data section, but just leave that address uninitialized, i.e. set to 0. In the case of AsmConstWalker, a correct thing to do is to emit the empty string as metadata, which becomes an empty emscripten_asm_v call.
* Add flag to s2wasm to export __growWasmMemory function (#696)jgravelle-google2016-09-091-0/+208
* Add a flag to s2wasm to export grow_memory Binaryen's wasm.js-post.js calls back in to wasm in order to grow the linear memory, via a function that asm2wasm exports called __growWasmMemory. This changes exposes that method through s2wasm when invoked with a flag. * Move AsmConstWalker from wasm-linker to wasm-emscripten * Add test for memory growth in s2wasm * Move makeDynCallThunks into wasm-emscripten module * Move mutation in getTableSegment into a separate method * Move emscripten metadata generation into wasm-emscripten Also make AsmConstWalker internal to the wasm-emscripten module, as it's only used for the metadata pass.