Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler output language - LLVM IR vs C

For writing a compiler, what are the advantages and disadvantages of using LLVM IR vs C for a target language? I know both are used, and I imagine that the final machine code would be similar if I were to use clang to compile the C. So what are other things to consider?

like image 216
Dan Avatar asked Apr 22 '12 02:04

Dan


2 Answers

I've used LLVM IR for a few compiler back ends and have worked with compilers that use C as a back end. One thing that I found that gave the LLVM IR an advantage is that it is typed. It is hard to make completely ill-formed output without getting errors from the LLVM libraries.

It is also easier to keep a close correlation between the source code and the IR for debugging, in my opinion.

Plus, you get all the cool LLVM command line tools to analyse and process the IR your front end emits.

like image 132
Richard Pennington Avatar answered Oct 21 '22 12:10

Richard Pennington


Architectures and OSes for which there is no CLANG obviously, or for which it is in an experimental state.

C is more widely accepted, but LLVM IR allows you to spoon feed the LLVM engine. Not all paths to IR are equal.

like image 33
Marco van de Voort Avatar answered Oct 21 '22 11:10

Marco van de Voort