3 ms·
Code 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 requir
by hardwaregeek 6y ago
Code 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