[LLVMdev] C Compiler written in OCaml, Pointers Wanted

Reid Kleckner rnk at mit.edu
Tue Feb 23 19:56:33 PST 2010

On Tue, Feb 23, 2010 at 10:37 PM, Nyx <mcheva at cs.mcgill.ca> wrote:
> Hello,
> For a course project, I am required to write a compiler for some language of
> my choice, and this compiler has to be implemented in a functional language.
> I have chosen create a *JIT* compiler for C source, and to implement my
> compiler in OCaml using LLVM for the back-end. I have experience using LLVM
> in C++ (I wrote a MATLAB JIT compiler not long ago), however, I am a bit
> puzzled as to how to go about some things, and would appreciate some
> pointers:


Couldn't resist.  :-D

> 1. When writing my MATLAB JIT in C++, I created bindings to native C++
> functions to serve as my runtime library. This hardly seems practical in
> OCaml. I would ideally want to write my runtime library in C (this will
> contain functions such as malloc, free, puts, strlen, etc.), and link it
> with the code I compile somehow. Is there any way for LLVM to link with code
> in pre-compiled C object files? Please note that this is for a JIT compiler,
> I need to be able to do this at run-time, I will not be generating an
> executable file.

Is writing the runtime part of the assignment?  In any case, to get
yourself up and running, if you have declarations of the runtime
functions in your module with external linkage, the JIT will dlsym
them if it can't find a definition for them in your module.  So if you
have malloc, strlen, etc. linked into your binary (which I'm guessing
you would, since I'm sure LLVM links in libc), it should be able to
call those.

> 2. One thing I don't know how to go about is memory allocation. As I just
> said, this will be a C *JIT* compiler. This means that my running compiled
> code will have to co-exist with OCaml. How do I go about implementing malloc
> in this context? Does LLVM provide some memory allocation implementation
> that will work with a JIT?

I would guess that since you can call LLVM from OCaml, you can use any
memory allocation strategy that you might normally use in a C/C++
program.  ie, mmap some address space and go nuts.  :)

> 3. Do the OCaml LLVM bindings even allow using LLVM in JIT mode?

No idea.


More information about the llvm-dev mailing list