2

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

user1479589
  • 315
  • 1
  • 4
  • 16
  • 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 Nov 05 '13 at 23:20
  • 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 Nov 06 '13 at 00:16
  • possible duplicate of [How to make clang compile to llvm IR](http://stackoverflow.com/questions/9148890/how-to-make-clang-compile-to-llvm-ir) – Flow Mar 05 '15 at 15:54

1 Answers1

7

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.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Hi, thanks for the response. Also, are you aware of any higher level API's to interact with the LLVM IR? I am aware of LLVM C++ Api.. and other bindings for it eg: llvmpy and a few java wrappers to.. – user1479589 Nov 06 '13 at 00:44
  • @user1479589: the LLVM C++ API is pretty high-level. As you mentioned, llvmpy (use the up-to-date project - https://github.com/llvmpy/llvmpy) can be useful. What higher-level than these do you need, i.e. for what specific needs are these low-level for you? – Eli Bendersky Nov 06 '13 at 00:49