refu
refu copied to clipboard
[Experiment] Emit also WebAssembly in the backend
Think on the benefits of adding emitting web assembly from Refu. Think if it makes sense to also emit WebAssembly code for web development.
It would be quite easy to do since LLVM can directly translate to Web Assembly
We've tried this and the LLVM IR output generated using the --output-backend
can be compiled using llc
to WebAssembly. If there would be an output, say --output-llvm-ir
, which doesn't include the debug lines and only outputs a single, linked IR, that could be compiled to WebAssembly without manual changes.
A few language features could help a lot in making this better:
-
Support namespaces in
foreign_import
. Wasm has a two-level import system, where each import has a namespace and a symbol. -
Support a way to specify actual exports. Currently all global functions are exported.
-
Support emitting Webassembly inline assembly (or at least the
(unreachable)
instruction)
For anyone interested:
- this sample Refu code: https://gist.github.com/LefterisJP/f3ce8a2a1f7b69d7f58ecd1fe4b40122
- results in this LLVM IR: https://gist.github.com/LefterisJP/a01ac0c6e67e6dd623b6cfa5013ab0cc
- and this WebAssembly output: https://gist.github.com/axic/660089577bd269e1f0e9665474fb2b89
There's a --llvm-ir
option now to display the IR on the standard output. Perhaps it should be changed to emit a file out.
That is going to be easy, LLVM has a PrintToFile method, existing even in 3.7.
/**
* Print a representation of a module to a file. The ErrorMessage needs to be
* disposed with LLVMDisposeMessage. Returns 0 on success, 1 otherwise.
*
* @see Module::print()
*/
LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
char **ErrorMessage);