refu-lang / refu

Refu language
21 stars 4 forks source link

[Experiment] Emit also WebAssembly in the backend #34

Open LefterisJP opened 7 years ago

LefterisJP commented 7 years ago

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

axic commented 7 years ago

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: 1) Support namespaces in foreign_import. Wasm has a two-level import system, where each import has a namespace and a symbol.

2) Support a way to specify actual exports. Currently all global functions are exported.

3) Support emitting Webassembly inline assembly (or at least the (unreachable) instruction)

axic commented 7 years ago

For anyone interested:

axic commented 7 years ago

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.

LefterisJP commented 7 years ago

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);