diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.h b/llvm/bindings/ocaml/llvm/llvm_ocaml.h --- a/llvm/bindings/ocaml/llvm/llvm_ocaml.h +++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.h @@ -33,6 +33,10 @@ value caml_alloc_some(value); #endif +/* to_val and from_val assume the pointer, which comes from LLVM and points + outside the OCaml heap, is at least 2-bit aligned. to_val sets the low bit + so that OCaml treats the pointer as an unboxed value. from_val does the + inverse. */ value to_val(void *ptr); void *from_val(value v); diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c --- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c +++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c @@ -33,21 +33,13 @@ #endif value to_val(void *ptr) { - if ((((value)ptr) & 1) == 0) { - return ((value)ptr) + 1; - } else { - value v = caml_alloc(1, Abstract_tag); - *((void **)Data_abstract_val(v)) = ptr; - return v; - } + assert((((value)ptr) & 1) == 0); + return ((value)ptr) | 1; } void *from_val(value v) { - if (Is_long(v)) { - return (void *)(v - 1); - } else { - return *((void **)Data_abstract_val(v)); - } + assert(Is_long(v)); + return (void *)(v ^ 1); } value llvm_string_of_message(char *Message) { @@ -665,7 +657,7 @@ CAMLparam1(StructTy); CAMLlocal1(Tys); unsigned Length = LLVMCountStructElementTypes(Type_val(StructTy)); - LLVMTypeRef *Temp = alloc_temp(sizeof(LLVMTypeRef) * Length); + LLVMTypeRef *Temp = alloc_temp(Length); LLVMGetStructElementTypes(Type_val(StructTy), Temp); Tys = caml_alloc_tuple(Length); for (unsigned I = 0; I < Length; ++I) { diff --git a/llvm/test/Bindings/OCaml/debuginfo.ml b/llvm/test/Bindings/OCaml/debuginfo.ml --- a/llvm/test/Bindings/OCaml/debuginfo.ml +++ b/llvm/test/Bindings/OCaml/debuginfo.ml @@ -290,7 +290,7 @@ *) let arg0 = (Llvm.params f).(0) in let arg_var = Llvm_debuginfo.dibuild_create_parameter_variable dibuilder ~scope:fun_di - ~name:"my_arg" ~argno:0 ~file:file_di ~line:10 ~ty + ~name:"my_arg" ~argno:1 ~file:file_di ~line:10 ~ty ~always_preserve:false flags_zero in let argdi = Llvm_debuginfo.dibuild_insert_declare_before dibuilder ~storage:arg0