[LLVMdev] Calling OCaml functions from LLVM

Jianzhou Zhao jianzhou at seas.upenn.edu
Tue Feb 23 05:21:35 PST 2010


Hi,

I was trying how to call OCaml code from a LLVM pass. First, I tested
how to call OCaml from
simple C++ code, which works. But when I was calling OCaml from LLVM, I have
the following problems:

1) A linkage error.
To call OCaml from C++, we need to link to libcamlrun.a, which defines
the interfaces
between C and OCaml. But libcamlrun.a also has a main function. So ld complains
it conflicts with the main function in opt.cpp. I did not have this
error in simple C++
and OCaml code. I might screw something up. It reports errors when
compiled both
as shared libraries and as archives.

2) Initializing OCaml runtime
OCaml requires to initialize its runtime before using OCaml code in C++ via
a function caml_startup. Where is the best place to call call_startup?
In opt.cpp
or in my pass.

3) Makefile Support
Current Makefile has the default compiling setting for *.c *.ml, and linking
options. But I need to compile the *.ml and *.c that have mixed C and OCaml
with special compilation options and linking arguments.

Does anyone have any idea and an example about how to link OCaml from LLVM?

Thanks in advance
Jianzhou



More information about the llvm-dev mailing list