/*===-- llvm_ocaml.c - LLVM Ocaml Glue --------------------------*- C++ -*-===*\
|* *|
|* The LLVM Compiler Infrastructure *|
|* *|
|* This file is distributed under the University of Illinois Open Source *|
|* License. See LICENSE.TXT for details. *|
|* *|
|*===----------------------------------------------------------------------===*|
|* *|
|* This file glues LLVM's ocaml interface to its C interface. These functions *|
|* are by and large transparent wrappers to the corresponding C functions. *|
|* *|
|* Note that these functions intentionally take liberties with the CAMLparamX *|
|* macros, since most of the parameters are not GC heap objects. *|
|* *|
\*===----------------------------------------------------------------------===*/
#include "llvm-c/Core.h"
#include "caml/alloc.h"
#include "caml/custom.h"
#include "caml/memory.h"
#include "caml/fail.h"
#include "caml/callback.h"
#include "llvm/Config/config.h"
#include <assert.h>
#include <stdlib.h>
/* Can't use the recommended caml_named_value mechanism for backwards
compatibility reasons. This is largely equivalent. */
static value llvm_ioerror_exn;
CAMLprim value llvm_register_core_exns(value IoError) {
llvm_ioerror_exn = Field(IoError, 0);
register_global_root(&llvm_ioerror_exn);
return Val_unit;
}
static void llvm_raise(value Prototype, char *Message) {
CAMLparam1(Prototype);
CAMLlocal1(CamlMessage);
CamlMessage = copy_string(Message);
LLVMDisposeMessage(Message);
raise_with_arg(Prototype, CamlMessage);
abort(); /* NOTREACHED */
#ifdef CAMLnoreturn
CAMLnoreturn; /* Silences warnings, but is missing in some versions. */
#endif
}
static value alloc_variant(int tag, void *Value) {
value Iter = alloc_small(1, tag);
Field(Iter, 0) = Val_op(Value);
return Iter;
}
/* Macro to convert the C first/next/last/prev idiom to the Ocaml llpos/
llrev_pos idiom. */
#define DEFINE_ITERATORS(camlname, cname, pty, cty, pfun) \
/* llmodule -> ('a, 'b) llpos */ \
CAMLprim value llvm_##camlname##_begin(pty Mom) { \
cty First = LLVMGetFirst##cname(Mom); \
if (First) \
return alloc_variant(1, First); \
return alloc_variant(0, Mom); \
} \
\
/* llvalue -> ('a, 'b) llpos */ \
CAMLprim value llvm_##camlname##_succ(cty Kid) { \
cty Next = LLVMGetNext##cname(Kid); \
if (Next) \
return alloc_variant(1, Next); \
return alloc_variant(0, pfun(Kid)); \
} \
\
/* llmodule -> ('a, 'b) llrev_pos */ \
CAMLprim value llvm_##camlname##_end(pty Mom) { \
cty Last = LLVMGetLast##cname(Mom); \
if (Last) \
return alloc_variant(1, Last); \
return alloc_variant(0, Mom); \
} \
\
/* llvalue -> ('a, 'b) llrev_pos */ \
CAMLprim value llvm_##camlname##_pred(cty Kid) { \
cty Prev = LLVMGetPrevious##cname(Kid); \
if (Prev) \
return alloc_variant(1, Prev); \
return alloc_variant(0, pfun(Kid)); \
}
/*===-- Modules -----------------------------------------------------------===*/
/* string -> llmodule */
CAMLprim LLVMModuleRef llvm_create_module(value ModuleID) {
return LLVMModuleCreateWithName(String_val(ModuleID));
}
/* llmodule -> unit */
CAMLprim value llvm_dispose_module(LLVMModuleRef M) {