Live data from Hacker News

A Complete Guide to LLVM for Programming Language Creators

mukulrathi.co.uk

1–10 of 47 posts

Re: A Complete Guide to LLVM for Programming Language Creators

#4

> LLVM IR looks like a more readable form of assembly I think assembly is more readable than the dumped LLVM IR output of a real compiler… Anyway, that was a good overview of how to build simple LLVM IR. :)

The IR is pretty messy but if you can see through the clutter the IR is much easier to read the flow of. There's a reason why we use SSA (although prior to mem2reg running you don't quite have it, but still)

Re: A Complete Guide to LLVM for Programming Language Creators

#5
A question for the LLVM experts here:

In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend.

How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source code to an asssembler? Or should one really just bite the bullet and use C++ to generate the IR? I noticed that the compiler in this tutorial has a frontend in Ocaml and a backend in C++, with the communication between them being done via protobufs.

Re: A Complete Guide to LLVM for Programming Language Creators

#7
As someone who writes a lot of toy languages, I made this scaffolding for a LLVM-based compiler: https://github.com/finiteloop/compiler

It uses Bison and Flex for parsing and lexing unlike this post, but may be a useful starting point for those building their own toy languages.

Re: A Complete Guide to LLVM for Programming Language Creators

#8
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

I would avoid any text, but LLVM has mature bindings in ocaml and Haskell, for example. The textual representation isn't stable IIRC, and it adds a step in between you and your already lumbering backend.

Ultimately the C++ API isn't too difficult to use but LLVM mandates a fairly hardcore level of C++ knowledge to play with it's internals.

Quick Tip: if you're thinking "Holy shit how do I get from [complicated] all the way down to IR instructions" lower the big thing to a something simpler so you can reuse the code to generate IR from that - for example, a foreach loop is expressible as a for loop under the hood, now you only have to be compile for loops. This would usually be done in the AST itself.

Re: A Complete Guide to LLVM for Programming Language Creators

#10
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

Julia has an interesting split here, it does the lowering into SSA from in pure Julia and then has a codegen steps that translates the SSA from into LLVM IR, but for that second step we do use the C++ API. We have very robust bindings to the C-API, but it forever feels just a bit incomplete and less cared for. The C-API is very stable, whereas the C++ API does change quite a bit.
Post reply on HN