2

I thought that you could turn LLVM bytecode into machine code directly using one of it's built-in tools, but according to this SO post, you have to actually provide the front and back-ends. LLVM is really just a tool for applying optimizations:

"LLVM can be used as a compiler framework, where you provide the "front end" (parser and lexer) and the "back end" (code that converts LLVM's representation to actual machine code)."

So my question is this: Is there a framework you can use where you just write a bytecode compiler into that framework's bytecode language, which is then compiled by the framework into the appropriate machine code?

If there is, then it would seem like writing a compiler wouldn't be any more involved than writing a bytecode compiler (basically lexer/parser and translation). I've never written a compiler, but it seems like you would need to generate several different kinds of assembly code (based on different architectures) and then assemble it to make it useful, but if there's some intermediate framework that does this back-end part, then it would take a lot of the sweat out of it all. I thought that LLVM did this, but my current understanding based on the above is that it doesn't.

Sorry if this is a dumb question, I'm just curious about the whole thing and I don't know much about it.

Community
  • 1
  • 1
antimatter
  • 3,240
  • 2
  • 23
  • 34

1 Answers1

4

LLVM already contains a bunch of back-ends for various popular architectures, including x86. So in theory if you want to create a new language you can just write an LLVM front-end for that language and you're done - you got a compiler for numerous architectures.

In practice just writing a front-end might not be enough (e.g. if you need some runtime libraries or memory management), but it should definitely suffice for some languages. In fact, that's one of the principle idea behind systems like gcc and LLVM - that you can just add a front-end and get architecture support for free (or alternatively, just add a back-end and allow numerous languages to be compiled into it for free).

Oak
  • 26,231
  • 8
  • 93
  • 152