Hacker Timesnew | past | comments | ask | show | jobs | submitlogin
Cmm_of_wasm: From WebAssembly to Native Code via the OCaml Back End (simonjf.com)
49 points by octosphere on Dec 10, 2018 | hide | past | favorite | 8 comments


So we could hypothetically convert nginx to WASM with wasmer, then convert it back to native code with cmm_of_wasm? We've come full circle.


As an adjacent dead comment points out, you'd use Emscripten to "convert nginx to WASM" (i.e. "compile"). Wasmer is a runtime.

But there are actually use cases to what you're talking about. Essentially instead of nginx -> LLVM -> machine code you're replacing it with nginx -> WASM -> cmm_of_wasm -> machine code. There are tons of LLVM opts already, but if there weren't, it might be reasonable to use WASM as IR and using cmm_of_wasm's opts to make it faster than a naive C compiler (but of course none of the popular compilers are naive anyways). Also, of course this won't work today because cmm_of_wasm doesn't rebuild the syscalls from emscripten-compiled-WASM (yet). But if you're writing your own language today, ignoring system interaction for a sec, targeting WASM even for native might not be a bad idea as backends are coming w/ optimizations.


You use emscripten to convert nginx c code to wasm


what is it about OCaml that makes it popular as a first stage in implementing new languages and/or compilers?

also...

> and eventually we hope to hook into OCaml’s excellent garbage collector

isnt WASM gc-free? why would the output need explicit GC interaction?


WASM currently does not have a GC but that's precisely why one is needed. Languages that target WASM need to implement their own GC but there's already a proposal to integrate a GC implementation into WASM[1].

[1]: https://github.com/WebAssembly/gc/blob/master/proposals/gc/O...


As Wikipedia says of ml (meta language) languages in general:

"Its types and pattern matching make it well-suited and commonly used to operate on other formal languages, such as in compiler writing, automated theorem proving and formal verification."

https://en.m.wikipedia.org/wiki/ML_(programming_language)


Algebraic data structures are really nice for representing ASTs and intermediate languages. And a compiler lends itself very well to functional programming, because what you ultimately want is just a function that takes the input code and produces the output code.


It's an extremely high-level language that compiles to extremely low-level fast code (except GC).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: