summaryrefslogtreecommitdiff
path: root/bindings
diff options
context:
space:
mode:
authorErick Tryzelaar <idadesub@users.sourceforge.net>2009-08-19 06:40:29 +0000
committerErick Tryzelaar <idadesub@users.sourceforge.net>2009-08-19 06:40:29 +0000
commit5371aa2a1c9a4eeecffdb9ab7b2175732e49475b (patch)
tree110bf139985ba9cca0e87e52950e31a2bdd08e60 /bindings
parent2d320867494351031216d76c5b3e6a2789461881 (diff)
downloadllvm-5371aa2a1c9a4eeecffdb9ab7b2175732e49475b.tar.gz
llvm-5371aa2a1c9a4eeecffdb9ab7b2175732e49475b.tar.bz2
llvm-5371aa2a1c9a4eeecffdb9ab7b2175732e49475b.tar.xz
Allow passing around LLVMContext in ocaml.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79410 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r--bindings/ocaml/bitreader/bitreader_ocaml.c14
-rw-r--r--bindings/ocaml/bitreader/llvm_bitreader.ml6
-rw-r--r--bindings/ocaml/bitreader/llvm_bitreader.mli22
-rw-r--r--bindings/ocaml/llvm/llvm.ml30
-rw-r--r--bindings/ocaml/llvm/llvm.mli94
-rw-r--r--bindings/ocaml/llvm/llvm_ocaml.c63
6 files changed, 148 insertions, 81 deletions
diff --git a/bindings/ocaml/bitreader/bitreader_ocaml.c b/bindings/ocaml/bitreader/bitreader_ocaml.c
index 0fd484f123..5fd9f854d9 100644
--- a/bindings/ocaml/bitreader/bitreader_ocaml.c
+++ b/bindings/ocaml/bitreader/bitreader_ocaml.c
@@ -45,27 +45,29 @@ static void llvm_raise(value Prototype, char *Message) {
/*===-- Modules -----------------------------------------------------------===*/
-/* Llvm.llmemorybuffer -> Llvm.module */
-CAMLprim value llvm_get_module_provider(LLVMMemoryBufferRef MemBuf) {
+/* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
+CAMLprim value llvm_get_module_provider(LLVMContextRef C,
+ LLVMMemoryBufferRef MemBuf) {
CAMLparam0();
CAMLlocal2(Variant, MessageVal);
char *Message;
LLVMModuleProviderRef MP;
- if (LLVMGetBitcodeModuleProvider(MemBuf, &MP, &Message))
+ if (LLVMGetBitcodeModuleProviderInContext(C, MemBuf, &MP, &Message))
llvm_raise(llvm_bitreader_error_exn, Message);
CAMLreturn((value) MemBuf);
}
-/* Llvm.llmemorybuffer -> Llvm.llmodule */
-CAMLprim value llvm_parse_bitcode(LLVMMemoryBufferRef MemBuf) {
+/* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
+CAMLprim value llvm_parse_bitcode(LLVMContextRef C,
+ LLVMMemoryBufferRef MemBuf) {
CAMLparam0();
CAMLlocal2(Variant, MessageVal);
LLVMModuleRef M;
char *Message;
- if (LLVMParseBitcode(MemBuf, &M, &Message))
+ if (LLVMParseBitcodeInContext(C, MemBuf, &M, &Message))
llvm_raise(llvm_bitreader_error_exn, Message);
CAMLreturn((value) M);
diff --git a/bindings/ocaml/bitreader/llvm_bitreader.ml b/bindings/ocaml/bitreader/llvm_bitreader.ml
index 816e156552..88587cbe1e 100644
--- a/bindings/ocaml/bitreader/llvm_bitreader.ml
+++ b/bindings/ocaml/bitreader/llvm_bitreader.ml
@@ -13,7 +13,9 @@ exception Error of string
external register_exns : exn -> unit = "llvm_register_bitreader_exns"
let _ = register_exns (Error "")
-external get_module_provider : Llvm.llmemorybuffer -> Llvm.llmoduleprovider
+external get_module_provider : Llvm.llcontext -> Llvm.llmemorybuffer ->
+ Llvm.llmoduleprovider
= "llvm_get_module_provider"
-external parse_bitcode : Llvm.llmemorybuffer -> Llvm.llmodule
+
+external parse_bitcode : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
= "llvm_parse_bitcode"
diff --git a/bindings/ocaml/bitreader/llvm_bitreader.mli b/bindings/ocaml/bitreader/llvm_bitreader.mli
index 15b389bb83..5648b35fee 100644
--- a/bindings/ocaml/bitreader/llvm_bitreader.mli
+++ b/bindings/ocaml/bitreader/llvm_bitreader.mli
@@ -14,16 +14,18 @@
exception Error of string
-(** [read_bitcode_file path] reads the bitcode for a new module [m] from the
- file at [path]. Returns [Success m] if successful, and [Failure msg]
- otherwise, where [msg] is a description of the error encountered.
- See the function [llvm::getBitcodeModuleProvider]. *)
-external get_module_provider : Llvm.llmemorybuffer -> Llvm.llmoduleprovider
+(** [get_module_provider context mb] reads the bitcode for a new
+ module provider [m] from the memory buffer [mb] in the context [context].
+ Returns [m] if successful, or raises [Error msg] otherwise, where [msg] is a
+ description of the error encountered. See the function
+ [llvm::getBitcodeModuleProvider]. *)
+external get_module_provider : Llvm.llcontext -> Llvm.llmemorybuffer ->
+ Llvm.llmoduleprovider
= "llvm_get_module_provider"
-(** [parse_bitcode mb] parses the bitcode for a new module [m] from the memory
- buffer [mb]. Returns [Success m] if successful, and [Failure msg] otherwise,
- where [msg] is a description of the error encountered.
- See the function [llvm::ParseBitcodeFile]. *)
-external parse_bitcode : Llvm.llmemorybuffer -> Llvm.llmodule
+(** [parse_bitcode context mb] parses the bitcode for a new module [m] from the
+ memory buffer [mb] in the context [context]. Returns [m] if successful, or
+ raises [Error msg] otherwise, where [msg] is a description of the error
+ encountered. See the function [llvm::ParseBitcodeFile]. *)
+external parse_bitcode : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
= "llvm_parse_bitcode"
diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml
index 63c79301b2..74fd1f1e17 100644
--- a/bindings/ocaml/llvm/llvm.ml
+++ b/bindings/ocaml/llvm/llvm.ml
@@ -8,6 +8,7 @@
*===----------------------------------------------------------------------===*)
+type llcontext
type llmodule
type lltype
type lltypehandle
@@ -127,10 +128,13 @@ type ('a, 'b) llrev_pos =
| At_start of 'a
| After of 'b
+(*===-- Contexts ----------------------------------------------------------===*)
+external create_context : unit -> llcontext = "llvm_create_context"
+external dispose_context : unit -> llcontext = "llvm_dispose_context"
+external global_context : unit -> llcontext = "llvm_global_context"
(*===-- Modules -----------------------------------------------------------===*)
-
-external create_module : string -> llmodule = "llvm_create_module"
+external create_module : llcontext -> string -> llmodule = "llvm_create_module"
external dispose_module : llmodule -> unit = "llvm_dispose_module"
external target_triple: llmodule -> string
= "llvm_target_triple"
@@ -147,8 +151,8 @@ external delete_type_name : string -> llmodule -> unit
external dump_module : llmodule -> unit = "llvm_dump_module"
(*===-- Types -------------------------------------------------------------===*)
-
external classify_type : lltype -> TypeKind.t = "llvm_classify_type"
+external type_context : lltype -> llcontext = "llvm_type_context"
(*--... Operations on integer types ........................................--*)
external _i1_type : unit -> lltype = "llvm_i1_type"
@@ -188,8 +192,9 @@ external return_type : lltype -> lltype = "LLVMGetReturnType"
external param_types : lltype -> lltype array = "llvm_param_types"
(*--... Operations on struct types .........................................--*)
-external struct_type : lltype array -> lltype = "llvm_struct_type"
-external packed_struct_type : lltype array -> lltype = "llvm_packed_struct_type"
+external struct_type : llcontext -> lltype array -> lltype = "llvm_struct_type"
+external packed_struct_type : llcontext -> lltype array -> lltype
+ = "llvm_packed_struct_type"
external element_types : lltype -> lltype array = "llvm_element_types"
external is_packed : lltype -> bool = "llvm_is_packed"
@@ -247,8 +252,9 @@ external const_float_of_string : lltype -> string -> llvalue
external const_string : string -> llvalue = "llvm_const_string"
external const_stringz : string -> llvalue = "llvm_const_stringz"
external const_array : lltype -> llvalue array -> llvalue = "llvm_const_array"
-external const_struct : llvalue array -> llvalue = "llvm_const_struct"
-external const_packed_struct : llvalue array -> llvalue
+external const_struct : llcontext -> llvalue array -> llvalue
+ = "llvm_const_struct"
+external const_packed_struct : llcontext -> llvalue array -> llvalue
= "llvm_const_packed_struct"
external const_vector : llvalue array -> llvalue = "llvm_const_vector"
@@ -654,20 +660,20 @@ external incoming : llvalue -> (llvalue * llbasicblock) list = "llvm_incoming"
(*===-- Instruction builders ----------------------------------------------===*)
-external builder : unit -> llbuilder = "llvm_builder"
+external builder : llcontext -> llbuilder = "llvm_builder"
external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
= "llvm_position_builder"
external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
external insert_into_builder : llvalue -> string -> llbuilder -> unit
= "llvm_insert_into_builder"
-let builder_at ip =
- let b = builder () in
+let builder_at context ip =
+ let b = builder context in
position_builder ip b;
b
-let builder_before i = builder_at (Before i)
-let builder_at_end bb = builder_at (At_end bb)
+let builder_before context i = builder_at context (Before i)
+let builder_at_end context bb = builder_at context (At_end bb)
let position_before i = position_builder (Before i)
let position_at_end bb = position_builder (At_end bb)
diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli
index cdcbdae61d..1eaa7fa18a 100644
--- a/bindings/ocaml/llvm/llvm.mli
+++ b/bindings/ocaml/llvm/llvm.mli
@@ -17,6 +17,10 @@
These abstract types correlate directly to the LLVM VMCore classes. *)
+(** The top-level container for all LLVM global data. See the
+ [llvm::LLVMContext] class. *)
+type llcontext
+
(** The top-level container for all other LLVM Intermediate Representation (IR)
objects. See the [llvm::Module] class. *)
type llmodule
@@ -188,12 +192,27 @@ type ('a, 'b) llrev_pos =
exception IoError of string
+(** {6 Contexts} *)
+
+(** [create_context ()] creates a context for storing the "global" state in
+ LLVM. See the constructor [llvm::LLVMContext]. *)
+external create_context : unit -> llcontext = "llvm_create_context"
+
+(** [destroy_context ()] destroys a context. See the destructor
+ [llvm::LLVMContext::~LLVMContext]. *)
+external dispose_context : unit -> llcontext = "llvm_dispose_context"
+
+(** See the function [llvm::getGlobalContext]. *)
+external global_context : unit -> llcontext = "llvm_global_context"
+
+
(** {6 Modules} *)
-(** [create_module id] creates a module with the supplied module ID. Modules are
- not garbage collected; it is mandatory to call {!dispose_module} to free
- memory. See the constructor [llvm::Module::Module]. *)
-external create_module : string -> llmodule = "llvm_create_module"
+(** [create_module context id] creates a module with the supplied module ID in
+ the context [context]. Modules are not garbage collected; it is mandatory
+ to call {!dispose_module} to free memory. See the constructor
+ [llvm::Module::Module]. *)
+external create_module : llcontext -> string -> llmodule = "llvm_create_module"
(** [dispose_module m] destroys a module [m] and all of the IR objects it
contained. All references to subordinate objects are invalidated;
@@ -245,6 +264,10 @@ external dump_module : llmodule -> unit = "llvm_dump_module"
See the method [llvm::Type::getTypeID]. *)
external classify_type : lltype -> TypeKind.t = "llvm_classify_type"
+(** [type_context ty] returns the {!llcontext} corresponding to the type [ty].
+ See the method [llvm::Type::getContext]. *)
+external type_context : lltype -> llcontext = "llvm_type_context"
+
(** [string_of_lltype ty] returns a string describing the type [ty]. *)
val string_of_lltype : lltype -> string
@@ -321,13 +344,17 @@ external param_types : lltype -> lltype array = "llvm_param_types"
(** {7 Operations on struct types} *)
-(** [struct_type tys] returns the structure type containing in the types in the
- array [tys]. See the method [llvm::StructType::get]. *)
-external struct_type : lltype array -> lltype = "llvm_struct_type"
+(** [struct_type context tys] returns the structure type in the context
+ [context] containing in the types in the array [tys]. See the method
+ [llvm::StructType::get]. *)
+external struct_type : llcontext -> lltype array -> lltype
+ = "llvm_struct_type"
-(** [packed_struct_type tys] returns the packed structure type containing in the
- types in the array [tys]. See the method [llvm::StructType::get]. *)
-external packed_struct_type : lltype array -> lltype = "llvm_packed_struct_type"
+(** [packed_struct_type context ys] returns the packed structure type in the
+ context [context] containing in the types in the array [tys]. See the method
+ [llvm::StructType::get]. *)
+external packed_struct_type : llcontext -> lltype array -> lltype
+ = "llvm_packed_struct_type"
(** [element_types sty] returns the constituent types of the struct type [sty].
See the method [llvm::StructType::getElementType]. *)
@@ -504,17 +531,19 @@ external const_stringz : string -> llvalue = "llvm_const_stringz"
See the method [llvm::ConstantArray::get]. *)
external const_array : lltype -> llvalue array -> llvalue = "llvm_const_array"
-(** [const_struct elts] returns the structured constant of type
- [struct_type (Array.map type_of elts)] and containing the values [elts].
- This value can in turn be used as the initializer for a global variable.
- See the method [llvm::ConstantStruct::get]. *)
-external const_struct : llvalue array -> llvalue = "llvm_const_struct"
-
-(** [const_packed_struct elts] returns the structured constant of type
- {!packed_struct_type} [(Array.map type_of elts)] and containing the values
- [elts]. This value can in turn be used as the initializer for a global
- variable. See the method [llvm::ConstantStruct::get]. *)
-external const_packed_struct : llvalue array -> llvalue
+(** [const_struct context elts] returns the structured constant of type
+ [struct_type (Array.map type_of elts)] and containing the values [elts]
+ in the context [context]. This value can in turn be used as the initializer
+ for a global variable. See the method [llvm::ConstantStruct::get]. *)
+external const_struct : llcontext -> llvalue array -> llvalue
+ = "llvm_const_struct"
+
+(** [const_packed_struct context elts] returns the structured constant of
+ type {!packed_struct_type} [(Array.map type_of elts)] and containing the
+ values [elts] in the context [context]. This value can in turn be used as
+ the initializer for a global variable. See the method
+ [llvm::ConstantStruct::get]. *)
+external const_packed_struct : llcontext -> llvalue array -> llvalue
= "llvm_const_packed_struct"
(** [const_vector elts] returns the vector constant of type
@@ -590,7 +619,7 @@ external const_sdiv : llvalue -> llvalue -> llvalue = "LLVMConstSDiv"
(** [const_exact_sdiv c1 c2] returns the constant quotient [c1 / c2] of two
signed integer constants. The result is undefined if the result is rounded
- or overflows. See the method [llvm::ConstantExpr::getExactSDiv]. *)
+ or overflows. See the method [llvm::ConstantExpr::getExactSDiv]. *)
external const_exact_sdiv : llvalue -> llvalue -> llvalue = "LLVMConstExactSDiv"
(** [const_fdiv c1 c2] returns the constant quotient [c1 / c2] of two floating
@@ -757,7 +786,7 @@ external const_intcast : llvalue -> lltype -> llvalue
= "LLVMConstIntCast"
(** [const_fpcast c ty] returns a constant fpext, bitcast, or fptrunc for fp ->
- fp casts of constant [c] to type [ty].
+ fp casts of constant [c] to type [ty].
See the method [llvm::ConstantExpr::getFPCast]. *)
external const_fpcast : llvalue -> lltype -> llvalue
= "LLVMConstFPCast"
@@ -1297,22 +1326,23 @@ external incoming : llvalue -> (llvalue * llbasicblock) list = "llvm_incoming"
(** {6 Instruction builders} *)
-(** [builder ()] creates an instruction builder with no position. It is invalid
- to use this builder until its position is set with {!position_before} or
- {!position_at_end}. See the constructor for [llvm::LLVMBuilder]. *)
-external builder : unit -> llbuilder = "llvm_builder"
+(** [builder context] creates an instruction builder with no position in
+ the context [context]. It is invalid to use this builder until its position
+ is set with {!position_before} or {!position_at_end}. See the constructor
+ for [llvm::LLVMBuilder]. *)
+external builder : llcontext -> llbuilder = "llvm_builder"
(** [builder_at ip] creates an instruction builder positioned at [ip].
See the constructor for [llvm::LLVMBuilder]. *)
-val builder_at : (llbasicblock, llvalue) llpos -> llbuilder
+val builder_at : llcontext -> (llbasicblock, llvalue) llpos -> llbuilder
(** [builder_before ins] creates an instruction builder positioned before the
instruction [isn]. See the constructor for [llvm::LLVMBuilder]. *)
-val builder_before : llvalue -> llbuilder
+val builder_before : llcontext -> llvalue -> llbuilder
(** [builder_at_end bb] creates an instruction builder positioned at the end of
the basic block [bb]. See the constructor for [llvm::LLVMBuilder]. *)
-val builder_at_end : llbasicblock -> llbuilder
+val builder_at_end : llcontext -> llbasicblock -> llbuilder
(** [position_builder ip bb] moves the instruction builder [bb] to the position
[ip].
@@ -1648,7 +1678,7 @@ external build_global_string : string -> string -> llbuilder -> llvalue
(** [build_global_stringptr str name b] creates a series of instructions that
adds a global string pointer at the position specified by the instruction
- builder [b].
+ builder [b].
See the method [llvm::LLVMBuilder::CreateGlobalStringPtr]. *)
external build_global_stringptr : string -> string -> llbuilder -> llvalue
= "llvm_build_global_stringptr"
@@ -1876,7 +1906,7 @@ external build_is_not_null : llvalue -> string -> llbuilder -> llvalue
(** [build_ptrdiff lhs rhs name b] creates a series of instructions that measure
the difference between two pointer values at the position specified by the
- instruction builder [b].
+ instruction builder [b].
See the method [llvm::LLVMBuilder::CreatePtrDiff]. *)
external build_ptrdiff : llvalue -> llvalue -> string -> llbuilder -> llvalue
= "llvm_build_ptrdiff"
diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c
index 69bd0a2fbc..ccb1b1cac4 100644
--- a/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/bindings/ocaml/llvm/llvm_ocaml.c
@@ -92,6 +92,24 @@ static value alloc_variant(int tag, void *Value) {
}
+/*===-- Contexts ----------------------------------------------------------===*/
+
+/* unit -> llcontext */
+CAMLprim LLVMContextRef llvm_create_context(value Unit) {
+ return LLVMContextCreate();
+}
+
+/* llcontext -> unit */
+CAMLprim value llvm_dispose_context(LLVMContextRef C) {
+ LLVMContextDispose(C);
+ return Val_unit;
+}
+
+/* unit -> llcontext */
+CAMLprim LLVMContextRef llvm_global_context(value Unit) {
+ return LLVMGetGlobalContext();
+}
+
/*===-- Modules -----------------------------------------------------------===*/
/* string -> llmodule */
@@ -153,6 +171,11 @@ CAMLprim value llvm_classify_type(LLVMTypeRef Ty) {
return Val_int(LLVMGetTypeKind(Ty));
}
+/* lltype -> llcontext */
+CAMLprim LLVMContextRef llvm_type_context(LLVMTypeRef Ty) {
+ return LLVMGetTypeContext(Ty);
+}
+
/*--... Operations on integer types ........................................--*/
/* unit -> lltype */
@@ -228,16 +251,17 @@ CAMLprim value llvm_param_types(LLVMTypeRef FunTy) {
/*--... Operations on struct types .........................................--*/
-/* lltype array -> lltype */
-CAMLprim LLVMTypeRef llvm_struct_type(value ElementTypes) {
- return LLVMStructType((LLVMTypeRef *) ElementTypes,
- Wosize_val(ElementTypes), 0);
+/* llcontext -> lltype array -> lltype */
+CAMLprim LLVMTypeRef llvm_struct_type(LLVMContextRef C, value ElementTypes) {
+ return LLVMStructTypeInContext(C, (LLVMTypeRef *) ElementTypes,
+ Wosize_val(ElementTypes), 0);
}
-/* lltype array -> lltype */
-CAMLprim LLVMTypeRef llvm_packed_struct_type(value ElementTypes) {
- return LLVMStructType((LLVMTypeRef *) ElementTypes,
- Wosize_val(ElementTypes), 1);
+/* llcontext -> lltype array -> lltype */
+CAMLprim LLVMTypeRef llvm_packed_struct_type(LLVMContextRef C,
+ value ElementTypes) {
+ return LLVMStructTypeInContext(C, (LLVMTypeRef *) ElementTypes,
+ Wosize_val(ElementTypes), 1);
}
/* lltype -> lltype array */
@@ -425,16 +449,17 @@ CAMLprim LLVMValueRef llvm_const_array(LLVMTypeRef ElementTy,
Wosize_val(ElementVals));
}
-/* llvalue array -> llvalue */
-CAMLprim LLVMValueRef llvm_const_struct(value ElementVals) {
- return LLVMConstStruct((LLVMValueRef *) Op_val(ElementVals),
- Wosize_val(ElementVals), 0);
+/* llcontext -> llvalue array -> llvalue */
+CAMLprim LLVMValueRef llvm_const_struct(LLVMContextRef C, value ElementVals) {
+ return LLVMConstStructInContext(C, (LLVMValueRef *) Op_val(ElementVals),
+ Wosize_val(ElementVals), 0);
}
-/* llvalue array -> llvalue */
-CAMLprim LLVMValueRef llvm_const_packed_struct(value ElementVals) {
- return LLVMConstStruct((LLVMValueRef *) Op_val(ElementVals),
- Wosize_val(ElementVals), 1);
+/* llcontext -> llvalue array -> llvalue */
+CAMLprim LLVMValueRef llvm_const_packed_struct(LLVMContextRef C,
+ value ElementVals) {
+ return LLVMConstStructInContext(C, (LLVMValueRef *) Op_val(ElementVals),
+ Wosize_val(ElementVals), 1);
}
/* llvalue array -> llvalue */
@@ -905,9 +930,9 @@ static value alloc_builder(LLVMBuilderRef B) {
return V;
}
-/* unit-> llbuilder */
-CAMLprim value llvm_builder(value Unit) {
- return alloc_builder(LLVMCreateBuilder());
+/* llcontext -> llbuilder */
+CAMLprim value llvm_builder(LLVMContextRef C) {
+ return alloc_builder(LLVMCreateBuilderInContext(C));
}
/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */