Skip to content

Commit

Permalink
[OCaml] Make Llvm_target.DataLayout.t automatically managed
Browse files Browse the repository at this point in the history
This breaks the API by removing Llvm_target.DataLayout.dispose.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194380 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
whitequark committed Nov 11, 2013
1 parent 9683888 commit 26f3bd8
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 54 deletions.
7 changes: 7 additions & 0 deletions bindings/ocaml/executionengine/executionengine_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,10 @@ CAMLprim value llvm_ee_free_machine_code(LLVMValueRef F,
return Val_unit;
}

extern value llvm_alloc_target_data(LLVMTargetDataRef TargetData);

/* ExecutionEngine.t -> Llvm_target.TargetData.t */
CAMLprim value llvm_ee_get_target_data(LLVMExecutionEngineRef EE) {
LLVMTargetDataRef TD = LLVMGetExecutionEngineTargetData(EE);
return llvm_alloc_target_data(TD);
}
3 changes: 1 addition & 2 deletions bindings/ocaml/executionengine/llvm_executionengine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ module ExecutionEngine = struct
= "llvm_ee_free_machine_code"

external target_data: t -> Llvm_target.DataLayout.t
= "LLVMGetExecutionEngineTargetData"
= "llvm_ee_get_target_data"

(* The following are not bound. Patches are welcome.

get_target_data: t -> lltargetdata
add_global_mapping: llvalue -> llgenericvalue -> t -> unit
clear_all_global_mappings: t -> unit
update_global_mapping: llvalue -> llgenericvalue -> t -> unit
Expand Down
3 changes: 1 addition & 2 deletions bindings/ocaml/target/llvm_target.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ module DataLayout = struct
external add : t -> [<Llvm.PassManager.any] Llvm.PassManager.t -> unit
= "llvm_targetdata_add"
external as_string : t -> string = "llvm_targetdata_as_string"
external dispose : t -> unit = "llvm_targetdata_dispose"
end

external byte_order : DataLayout.t -> Endian.t = "llvm_byte_order"
external pointer_size : DataLayout.t -> int = "llvm_pointer_size"
external intptr_type : DataLayout.t -> Llvm.lltype = "LLVMIntPtrType"
external intptr_type : DataLayout.t -> Llvm.lltype = "llvm_intptr_type"
external size_in_bits : DataLayout.t -> Llvm.lltype -> Int64.t
= "llvm_size_in_bits"
external store_size : DataLayout.t -> Llvm.lltype -> Int64.t = "llvm_store_size"
Expand Down
6 changes: 1 addition & 5 deletions bindings/ocaml/target/llvm_target.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ module DataLayout : sig
(** [as_string td] is the string representation of the target data [td].
See the constructor llvm::DataLayout::DataLayout. *)
external as_string : t -> string = "llvm_targetdata_as_string"

(** Deallocates a DataLayout.
See the destructor llvm::DataLayout::~DataLayout. *)
external dispose : t -> unit = "llvm_targetdata_dispose"
end

(** Returns the byte order of a target, either LLVMBigEndian or
Expand All @@ -51,7 +47,7 @@ external pointer_size : DataLayout.t -> int = "llvm_pointer_size"

(** Returns the integer type that is the same size as a pointer on a target.
See the method llvm::DataLayout::getIntPtrType. *)
external intptr_type : DataLayout.t -> Llvm.lltype = "LLVMIntPtrType"
external intptr_type : DataLayout.t -> Llvm.lltype = "llvm_intptr_type"

(** Computes the size of a type in bits for a target.
See the method llvm::DataLayout::getTypeSizeInBits. *)
Expand Down
92 changes: 58 additions & 34 deletions bindings/ocaml/target/target_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,86 +17,110 @@

#include "llvm-c/Target.h"
#include "caml/alloc.h"
#include "caml/custom.h"

#define TargetData_val(v) (*(LLVMTargetDataRef *)(Data_custom_val(v)))

static void llvm_finalize_target_data(value TargetData) {
LLVMDisposeTargetData(TargetData_val(TargetData));
}

static struct custom_operations llvm_target_data_ops = {
(char *) "LLVMTargetData",
llvm_finalize_target_data,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
#ifdef custom_compare_ext_default
, custom_compare_ext_default
#endif
};

value llvm_alloc_target_data(LLVMTargetDataRef TargetData) {
value V = alloc_custom(&llvm_target_data_ops, sizeof(LLVMTargetDataRef), 0, 1);
TargetData_val(V) = TargetData;
return V;
}

/* string -> DataLayout.t */
CAMLprim LLVMTargetDataRef llvm_targetdata_create(value StringRep) {
return LLVMCreateTargetData(String_val(StringRep));
CAMLprim value llvm_targetdata_create(value StringRep) {
return llvm_alloc_target_data(LLVMCreateTargetData(String_val(StringRep)));
}

/* DataLayout.t -> [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
CAMLprim value llvm_targetdata_add(LLVMTargetDataRef TD, LLVMPassManagerRef PM){
LLVMAddTargetData(TD, PM);
CAMLprim value llvm_targetdata_add(value TD, LLVMPassManagerRef PM){
LLVMAddTargetData(TargetData_val(TD), PM);
return Val_unit;
}

/* DataLayout.t -> string */
CAMLprim value llvm_targetdata_as_string(LLVMTargetDataRef TD) {
char *StringRep = LLVMCopyStringRepOfTargetData(TD);
CAMLprim value llvm_targetdata_as_string(value TD) {
char *StringRep = LLVMCopyStringRepOfTargetData(TargetData_val(TD));
value Copy = copy_string(StringRep);
LLVMDisposeMessage(StringRep);
return Copy;
}

/* DataLayout.t -> unit */
CAMLprim value llvm_targetdata_dispose(LLVMTargetDataRef TD) {
LLVMDisposeTargetData(TD);
return Val_unit;
}

/* DataLayout.t -> Endian.t */
CAMLprim value llvm_byte_order(LLVMTargetDataRef TD) {
return Val_int(LLVMByteOrder(TD));
CAMLprim value llvm_byte_order(value TD) {
return Val_int(LLVMByteOrder(TargetData_val(TD)));
}

/* DataLayout.t -> int */
CAMLprim value llvm_pointer_size(LLVMTargetDataRef TD) {
return Val_int(LLVMPointerSize(TD));
CAMLprim value llvm_pointer_size(value TD) {
return Val_int(LLVMPointerSize(TargetData_val(TD)));
}

/* DataLayout.t -> int -> Llvm.lltype */
CAMLprim LLVMTypeRef llvm_intptr_type(value TD) {
return LLVMIntPtrType(TargetData_val(TD));;
}

/* DataLayout.t -> Llvm.lltype -> Int64.t */
CAMLprim value llvm_size_in_bits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
return caml_copy_int64(LLVMSizeOfTypeInBits(TD, Ty));
CAMLprim value llvm_size_in_bits(value TD, LLVMTypeRef Ty) {
return caml_copy_int64(LLVMSizeOfTypeInBits(TargetData_val(TD), Ty));
}

/* DataLayout.t -> Llvm.lltype -> Int64.t */
CAMLprim value llvm_store_size(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
return caml_copy_int64(LLVMStoreSizeOfType(TD, Ty));
CAMLprim value llvm_store_size(value TD, LLVMTypeRef Ty) {
return caml_copy_int64(LLVMStoreSizeOfType(TargetData_val(TD), Ty));
}

/* DataLayout.t -> Llvm.lltype -> Int64.t */
CAMLprim value llvm_abi_size(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
return caml_copy_int64(LLVMABISizeOfType(TD, Ty));
CAMLprim value llvm_abi_size(value TD, LLVMTypeRef Ty) {
return caml_copy_int64(LLVMABISizeOfType(TargetData_val(TD), Ty));
}

/* DataLayout.t -> Llvm.lltype -> int */
CAMLprim value llvm_abi_align(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
return Val_int(LLVMABIAlignmentOfType(TD, Ty));
CAMLprim value llvm_abi_align(value TD, LLVMTypeRef Ty) {
return Val_int(LLVMABIAlignmentOfType(TargetData_val(TD), Ty));
}

/* DataLayout.t -> Llvm.lltype -> int */
CAMLprim value llvm_stack_align(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
return Val_int(LLVMCallFrameAlignmentOfType(TD, Ty));
CAMLprim value llvm_stack_align(value TD, LLVMTypeRef Ty) {
return Val_int(LLVMCallFrameAlignmentOfType(TargetData_val(TD), Ty));
}

/* DataLayout.t -> Llvm.lltype -> int */
CAMLprim value llvm_preferred_align(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
return Val_int(LLVMPreferredAlignmentOfType(TD, Ty));
CAMLprim value llvm_preferred_align(value TD, LLVMTypeRef Ty) {
return Val_int(LLVMPreferredAlignmentOfType(TargetData_val(TD), Ty));
}

/* DataLayout.t -> Llvm.llvalue -> int */
CAMLprim value llvm_preferred_align_of_global(LLVMTargetDataRef TD,
CAMLprim value llvm_preferred_align_of_global(value TD,
LLVMValueRef GlobalVar) {
return Val_int(LLVMPreferredAlignmentOfGlobal(TD, GlobalVar));
return Val_int(LLVMPreferredAlignmentOfGlobal(TargetData_val(TD), GlobalVar));
}

/* DataLayout.t -> Llvm.lltype -> Int64.t -> int */
CAMLprim value llvm_element_at_offset(LLVMTargetDataRef TD, LLVMTypeRef Ty,
CAMLprim value llvm_element_at_offset(value TD, LLVMTypeRef Ty,
value Offset) {
return Val_int(LLVMElementAtOffset(TD, Ty, Int64_val(Offset)));
return Val_int(LLVMElementAtOffset(TargetData_val(TD), Ty, Int64_val(Offset)));
}

/* DataLayout.t -> Llvm.lltype -> int -> Int64.t */
CAMLprim value llvm_offset_of_element(LLVMTargetDataRef TD, LLVMTypeRef Ty,
CAMLprim value llvm_offset_of_element(value TD, LLVMTypeRef Ty,
value Index) {
return caml_copy_int64(LLVMOffsetOfElement(TD, Ty, Int_val(Index)));
return caml_copy_int64(LLVMOffsetOfElement(TargetData_val(TD), Ty, Int_val(Index)));
}
4 changes: 1 addition & 3 deletions test/Bindings/Ocaml/ipo_opts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ let test_transforms () =
++ add_strip_dead_prototypes
++ add_strip_symbols
++ PassManager.run_module m
++ PassManager.dispose);

DataLayout.dispose td
++ PassManager.dispose)


(*===-- Driver ------------------------------------------------------------===*)
Expand Down
4 changes: 1 addition & 3 deletions test/Bindings/Ocaml/scalar_opts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ let test_transforms () =
++ PassManager.initialize
++ PassManager.run_function fn
++ PassManager.finalize
++ PassManager.dispose);

DataLayout.dispose td
++ PassManager.dispose)


(*===-- Driver ------------------------------------------------------------===*)
Expand Down
3 changes: 1 addition & 2 deletions test/Bindings/Ocaml/target.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ let test_target_data () =
assert_equal (preferred_align td sty) 8;
assert_equal (preferred_align_of_global td (declare_global sty "g" m)) 8;
assert_equal (element_at_offset td sty (Int64.of_int 1)) 0;
assert_equal (offset_of_element td sty 1) (Int64.of_int 4);
DataLayout.dispose td
assert_equal (offset_of_element td sty 1) (Int64.of_int 4)


(*===-- Driver ------------------------------------------------------------===*)
Expand Down
4 changes: 1 addition & 3 deletions test/Bindings/Ocaml/vectorize_opts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ let test_transforms () =
++ add_loop_vectorize
++ add_slp_vectorize
++ PassManager.run_module m
++ PassManager.dispose);

DataLayout.dispose td
++ PassManager.dispose)


(*===-- Driver ------------------------------------------------------------===*)
Expand Down

0 comments on commit 26f3bd8

Please sign in to comment.