Emscripten emulation layer for llvm-based compiler

17 views
Skip to first unread message

Jacek Sieka

unread,
Aug 7, 2019, 6:07:50 AM8/7/19
to emscripten-discuss
Hi!

I'm experimenting a little with wasm for a llvm-based compiler I'm working on - https://github.com/arnetheduck/nlvm - for the Nim language.

Nim typically is compiled to C and then the C compiler takes over meaning that it's fairly straightforward get it to compile with emcc to wasm instead. In `nlvm`, Nim is compiled straight to llvm IR and consequently, nlvm is able to output wasm code as well.

The next step that I think would make sense to get the Nim code to run in a browser would be to use the emscripten runtime, but from what I can tell, when emcc runs it customizes the runtime for the specific case.

How does that work? IFor example, is there some tooling in emcc that I can give an llvm IR file to (which imports various C functions like clang-compiled C code would), and have emcc output the corresponding html/js/wasm files?

Thanks!

Jacek

Alon Zakai

unread,
Aug 7, 2019, 1:33:40 PM8/7/19
to emscripte...@googlegroups.com
Hi Jacek,

Yes, basically that should work - run

emcc input.bc -o output.js

where input.bc is LLVM IR, and then you'll get a js+wasm combination that you can run in node.js or on the Web. And with -o output.html you'd also get html etc.

Things to be careful of:

 * The LLVM version that generated the IR should be close enough to emscripten's LLVM version. It's best to use the wasm backend (https://v8.dev/blog/emscripten-llvm-wasm#testing) with emscripten - the default there is a very recent build of LLVM from master. If that's too new for you, you can use an older emscripten version perhaps.
 * An alternative to using LLVM IR is to use wasm object files, which are a much more stable format. You can emit those using your compiler's LLVM by telling it to use the wasm backend, and then run emcc on that .o file.
 * Emscripten's libc etc. assume the code was compiled with the emscripten headers. If not, the ABI may be incompatible for things like syscalls.

- Alon


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/e3c2ada5-f2fa-4ccf-8558-ca30fde4e656%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages