2
votes

I am looking to generate LLVM-IR code from C code and was wondering how well is the IR generation for functions in:

stdio.h, string.h, stdlib.h and generally the standard memory based functions such as malloc, calloc, since I have not been able to find most of the common functions in:

http://llvm.org/docs/LangRef.html and was wondering about the limitations of this representation and whether I might be required to add my own intrinsics just to deal with standard/most popular c functions.

I am looking to change the code at runtime, so was wondering which kind of approach will give me the most flexibility eg: Manipulate the code at AST level instead.

Thanks

1
I wrote a compiler for a C-like language and targeted the LLVM assembly language that you reference. It took a little getting used and there were some nuances but I was happy with the choice. You can compile the stdlib with clang and then link that in with your code. I found it useful to compile C with clang to llvm assembly and then look at that for information about how things are done in llvm assembly. But manipulating code at runtime, JIT compilation, is whole different story. - Charlie Burns
Hi, I am actually thinking of manipulating the LLVM-IR using LLVM Mutate to represent small code changes that I wish to do eg: changing the parameters to standard library functions etc.. - user1479589
possible duplicate of How to make clang compile to llvm IR - Flow

1 Answers

7
votes

Emitting LLVM IR from C is exactly what the industrial-strength compiler Clang does. I suggest running Clang on small snippets of C code with -emit-llvm (details in this document: http://clang.llvm.org/get_started.html) and observing the resulting IR.

You can even do this in your browser: http://ellcc.org/demo/index.cgi

That will allow you to see how builtins like memcpy are handled and any other similar doubts.

Note that neither LLVM nor Clang carry a full C library with them, but they can be used to compile an existing one. newlib is a popular portable C library designed specifically for being built on various new platforms. PNaCl, for example, uses it to build C/C++ code into portable executables - it compiles newlib with the user's code together into a single LLVM IR module.