4 ms·
I'm also implementing my own language using rust. I got stuck at code generation. I'm new to llvm IR. it is more assembly than I thought. my language is simil
by billconan 6y ago
I'm also implementing my own language using rust. I got stuck at code generation.
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.
- hardwaregeek 6y agoCode generation is pretty hard. I'd start by generating arithmetic from your AST. Then focus on getting a simple function to compile. Don't worry if this requires a lot of hard coding. Once you get to the harder concepts like closures, consider making a secondary IR that simplifies code gen. I have a typed AST representation that desugars various concepts like first class functions, destructuring, etc. I'm actually compiling to WebAssembly but it's the same principles
- billconan 6y agoI also want to generate webassembly. I compared the option of generating wasm directly with using llvm. The wasm spec is hard to read without enough examples. 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.
- hardwaregeek 6y agoAgreed that the WebAssembly spec is a pain to read. I mostly used this outdated doc: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md https://github.com/WebAssembly/design/blob/master/BinaryEnco... 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
- siraben 6y agoIf your langauge is functional (has closures), I would recommend checking out[0] which is a full end-to-end tutorial on compiling a functional language to C and could be adapted to LLVM, using Haskell as the implementation language. 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/ https://github.com/jozefg/pcf/ [1] http://lampwww.epfl.ch/teaching/archive/advanced_compiler/2007/resources/slides/act-2007-05-closure-conversion.pdf http://lampwww.epfl.ch/teaching/archive/advanced_compiler/20...
- caditinpiscinam 6y agoAny tips on implementing tail-call optimization when compiling to C? (or llvm)?
- ratww 6y agoThe easy way is by using goto.
- siraben 6y agoThat is a good question. Although tail-call is easy to describe and understand I've haven't implemented it myself. I'd also appreciate pointers to resources.
- siraben 6y agoOh, I completely forgot about[0], which talks about TCO in the imperative context (but applies to functional languages as well)! [0] https://dspace.mit.edu/bitstream/handle/1721.1/5753/AIM-443.pdf?sequence=2&isAllowed=y https://dspace.mit.edu/bitstream/handle/1721.1/5753/AIM-443....
- Joker_vD 6y agoSee also [0], which talks about how Chicken SCHEME (which compiles to portable C) does it. [0] https://www.more-magic.net/posts/internals-gc.html#managing-data-on-the-stack https://www.more-magic.net/posts/internals-gc.html#managing-...
- txdv 6y agoYou might want generate assembly yourself, even if it is in the most inefficient way. I found myself that LLVM has a very high barrier to use.