Skip to content

How to convert imported variables? #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
weilianglin opened this issue Nov 24, 2015 · 6 comments
Closed

How to convert imported variables? #6

weilianglin opened this issue Nov 24, 2015 · 6 comments

Comments

@weilianglin
Copy link

From the output of asm2wasm, I find it just simply delete the imported variables statements, such as "var i = env.STACKTOP | 0; ". Without the support of imported variables, can this tool support all asm.js?

@kripken
Copy link
Member

kripken commented Nov 24, 2015

This is an issue that happens because WebAssembly does not have global variables. The idea is that people use locations in memory to replace them, but that requires interaction with the compiler, so it knows where it can place them.

For emscripten, we just reserve 8-1000 for such "mapped globals" as we call them, and we know that will be enough room.

We could make this work for other asm.js code too. The mapping shows up in mappedGlobals, see https://github.com/WebAssembly/binaryen/blob/master/src/wasm-js.cpp#L199 for how it is used. But asm2wasm needs to be told where it can place them, with nextGlobal and maxGlobal https://github.com/WebAssembly/binaryen/blob/master/src/asm2wasm.h#L275 which by default have the 8-1000 range that works for emscripten.

@weilianglin
Copy link
Author

I see. So before running WASM code, load_mapped_globals() should be called to copy imported variables to the correct locations and inside WASM, global variable access is interpreted to load/store of linear memory.

@kripken
Copy link
Member

kripken commented Nov 24, 2015

Yes, exactly.

Out of curiosity, are you trying to convert asm.js from a non-emscripten source?

@weilianglin
Copy link
Author

No, I also convert asm.js generated by EmScripten to v8-native binary format. Since v8-native still supports Global variable, I need to consider how to initialize the imported variables to the global variables. My current solution is to import variables by FFI function call, like
Module.asmLibraryArg.getSTACKTOP = function getSTACKTOP() { return STACKTOP; };

Then modify the asm.js code to call this initialization before running _main function.

@kripken
Copy link
Member

kripken commented Nov 24, 2015

I see. Yes, something like that could work too.

@tlively
Copy link
Member

tlively commented Dec 13, 2024

Closing because asm2wasm no longer exists!

@tlively tlively closed this as completed Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants