25

I'm bootstrapping a programming language compiler on top of LLVM. Currently I'm mostly done writing a compiler for a subset of C which is self-compiling. When I'm finished with that, I'll bootstrap my language away from C, maintaining self-compilation as I go.

Since the compiler is self-compiling, any features of C that I use I will have to implement. So it's a constant balance: if I use too many features I will have to implement more than I want to, but if I don't implement enough features it will be difficult to write code.

One such feature is the LLVM bindings. Generating LLVM intermediate representation without the LLVM C bindings is difficult. However, if I us the LLVM bindings, I have to implement them again when I branch away from C.

I'm having some difficulty here, so I a looking for alternative solutions. Any ideas?

Imagist
  • 18,086
  • 12
  • 58
  • 77
  • What are you writing your complier in? C? You are using LLVM for machine code compilation then? – Adam Goode Jan 05 '10 at 20:57
  • My compiler is written in the subset of C that it compiles. Sorry, I didn't explain that well. My current plan is to write out LLVM intermediate representation to a file and then use LLVM to compile it, but that's complicated, and I'm looking for a cleaner solution. – Imagist Jan 05 '10 at 21:05
  • LLVM is a great choice for the back end, by the way. I've had great luck with it. – Richard Pennington Jan 05 '10 at 21:28

3 Answers3

10

You could use the LLVM C bindings, but that requires your language understand enough C to do that.

Another alternative is to write out LLVM assembly language (a text file) and use llvm-as to turn that into bitcode.


Edit:

I re-read you question, I think you already understand the llvm-as vs. binding stuff.

Your language will probably want to be able to bind to C anyway for support libraries, etc. Use the C bindings for now and write your own bindings when you get further along.

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
5

A Strategy for using ANTLR + StringTemplate + LLVM

HTH

plan9assembler
  • 2,862
  • 1
  • 24
  • 13
1

At some point, you're probably going to want to provide an API for wrapping C libraries as extension modules. LLVM may already support this (I know the parrot vm does). Why not use whatever extension system you use to wrap LLVM's own API? They may already support that, too. :)

tangentstorm
  • 7,183
  • 2
  • 29
  • 38