News for hackers. Stuff that matters.
Re:Making a programming language using Rust by billconan on Sunday November 08, 2020 @08:56PM (#25030720)
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.

Re:Making a programming language using Rust by siraben on Sunday November 08, 2020 @09:26PM (#25030860)
If 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/

[1] http://lampwww.epfl.ch/teaching/archive/advanced_compiler/20...

Re:Making a programming language using Rust by caditinpiscinam on Sunday November 08, 2020 @10:05PM (#25031077)
Any tips on implementing tail-call optimization when compiling to C? (or llvm)?
Re:Making a programming language using Rust by sinistersnare on Monday November 09, 2020 @12:04AM (#25031630)
If you translate your language to use Continuation Passing Style [0], then you get tail-calls for every function, which effectively obviates the stack.

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...

Re:Making a programming language using Rust by siraben on Monday November 09, 2020 @04:38AM (#25033042)
> then you get tail-calls for every function, which effectively obviates the stack.

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://en.wikipedia.org/wiki/Trampoline_(computing)

Re:Making a programming language using Rust by siraben on Sunday November 08, 2020 @10:26PM (#25031184)
That 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.
Re:Making a programming language using Rust by siraben on Sunday November 08, 2020 @10:29PM (#25031195)
Oh, 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....

Re:Making a programming language using Rust by Joker_vD on Monday November 09, 2020 @06:25AM (#25033632)
See 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-...

Re:Making a programming language using Rust by ratww on Sunday November 08, 2020 @10:19PM (#25031148)
The easy way is by using goto.
Re:Making a programming language using Rust by hardwaregeek on Sunday November 08, 2020 @09:24PM (#25030846)
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

Re:Making a programming language using Rust by billconan on Sunday November 08, 2020 @11:12PM (#25031388)
I 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.

Re:Making a programming language using Rust by hardwaregeek on Monday November 09, 2020 @10:06AM (#25035492)
Agreed that the WebAssembly spec is a pain to read. I mostly used this outdated doc: 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

Re:Making a programming language using Rust by txdv on Monday November 09, 2020 @04:58AM (#25033162)
You 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.