Basically the only slightly non-trivial step is closure conversion[1] which lifts free (AKA "captured" in C++ terminology) variables from a closure, which lets you encode closures faithfully in a first-order language with structs.
[0] https://github.com/jozefg/pcf/
[1] http://lampwww.epfl.ch/teaching/archive/advanced_compiler/20...
This technique is used by functional language compilers, see Compiling With Continuations for more info on the underlying theory
[0]: https://en.wikipedia.org/wiki/Continuation-passing_style
[1]: https://books.google.com/books/about/Compiling_with_Continua...
This is true, however CPS moves the work from the stack to the heap, so a deep enough recursion would exhaust the heap instead. Using a trampoline[0] in conjunction with CPS ensure constant stack usage for tail-recursive functions.
[0] https://dspace.mit.edu/bitstream/handle/1721.1/5753/AIM-443....
[0] https://www.more-magic.net/posts/internals-gc.html#managing-...
I'm actually compiling to WebAssembly but it's the same principles
My current approach is writing my runtime in c or c++, and put most logic there. And I will try to generate c code first, and compile the c code using clang and learn from the generated IR.
I kinda want to write the runtime also in rust, but I don't know if linking will be a problem.
Generating C seems like a good middle ground. I also considered that. Frankly though code generation is hard regardless because you’re gonna have some abstractions that just don’t lower nicely.
Writing the runtime in Rust might work but ownership might get in the way.
Happy to talk about this more if you want. Shoot me an email
I'm new to llvm IR. it is more assembly than I thought. my language is similar to javascript. I found it's difficult to map it to llvm IR.