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 @@ -56,6 +56,18 @@ CAMLreturn(String); } +CAMLprim value cstr_to_string_option(const char *CStr, mlsize_t Len) { + CAMLparam0(); + CAMLlocal2 (Option, String); + if (!CStr) + CAMLreturn(Val_int(0)); + String = caml_alloc_string(Len); + memcpy((char *)String_val(String), CStr, Len); + Option = caml_alloc_small(1, 0); + Store_field(Option, 0, (value)String); + CAMLreturn(Option); +} + void llvm_raise(value Prototype, char *Message) { CAMLparam1(Prototype); caml_raise_with_arg(Prototype, llvm_string_of_message(Message)); @@ -529,17 +541,11 @@ } /* lltype -> string option */ -CAMLprim value llvm_struct_name(LLVMTypeRef Ty) -{ - CAMLparam0(); - CAMLlocal1(result); - const char *C = LLVMGetStructName(Ty); - if (C) { - result = caml_alloc_small(1, 0); - Store_field(result, 0, caml_copy_string(C)); - CAMLreturn(result); - } - CAMLreturn(Val_int(0)); +CAMLprim value llvm_struct_name(LLVMTypeRef Ty) { + const char * CStr = LLVMGetStructName(Ty); + if (!CStr) + return Val_int(0); + return cstr_to_string_option (CStr, strlen(CStr)); } /* lltype -> lltype array */ @@ -877,19 +883,8 @@ /* llvalue -> string option */ CAMLprim value llvm_get_mdstring(LLVMValueRef V) { - CAMLparam0(); - CAMLlocal2(Option, Str); - const char *S; unsigned Len; - - if ((S = LLVMGetMDString(V, &Len))) { - Str = caml_alloc_string(Len); - memcpy((char *)String_val(Str), S, Len); - Option = alloc(1,0); - Store_field(Option, 0, Str); - CAMLreturn(Option); - } - CAMLreturn(Val_int(0)); + return cstr_to_string_option(LLVMGetMDString(V, &Len), Len); } CAMLprim value llvm_get_mdnode_operands(LLVMValueRef V) { @@ -1045,21 +1040,11 @@ /* llvalue -> string option */ CAMLprim value llvm_string_of_const(LLVMValueRef Const) { - const char *S; size_t Len; - CAMLparam0(); - CAMLlocal2(Option, Str); - if(LLVMIsAConstantDataSequential(Const) && LLVMIsConstantString(Const)) { - S = LLVMGetAsString(Const, &Len); - Str = caml_alloc_string(Len); - memcpy((char *)String_val(Str), S, Len); - - Option = alloc(1, 0); - Field(Option, 0) = Str; - CAMLreturn(Option); + return cstr_to_string_option(LLVMGetAsString(Const, &Len), Len); } else { - CAMLreturn(Val_int(0)); + return Val_int(0); } }