From 68c15476c3e82136a5d5b247082b48671e0e9009 Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Fri, 26 Mar 2021 14:53:38 -0700 Subject: [PATCH] [sledge][llvm] Simplify llvm_global_initializer using ptr_to_option Summary: This diff uses ptr_to_option to convert a nullable C pointer to an OCaml option instead of the redundant implementation in llvm_global_initializer. Upstream Differential Revision: https://reviews.llvm.org/D99391 Reviewed By: ngorogiannis Differential Revision: D27360847 fbshipit-source-id: b5bfcadf7 --- .../llvm/bindings/ocaml/llvm/llvm_ocaml.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c index 11eca907e..a1c201a3e 100644 --- a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c +++ b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c @@ -35,6 +35,16 @@ value llvm_string_of_message(char* Message) { return String; } +CAMLprim value ptr_to_option(void *Ptr) { + CAMLparam0(); + CAMLlocal1(Option); + if (!Ptr) + CAMLreturn(Val_int(0)); + Option = caml_alloc_small(1, 0); + Store_field(Option, 0, (value)Ptr); + CAMLreturn(Option); +} + void llvm_raise(value Prototype, char *Message) { CAMLparam1(Prototype); caml_raise_with_arg(Prototype, llvm_string_of_message(Message)); @@ -1384,14 +1394,7 @@ CAMLprim value llvm_delete_global(LLVMValueRef GlobalVar) { /* llvalue -> llvalue option */ CAMLprim value llvm_global_initializer(LLVMValueRef GlobalVar) { - CAMLparam0(); - LLVMValueRef Init; - if ((Init = LLVMGetInitializer(GlobalVar))) { - value Option = alloc(1, 0); - Field(Option, 0) = (value) Init; - CAMLreturn(Option); - } - CAMLreturn(Val_int(0)); + return ptr_to_option(LLVMGetInitializer(GlobalVar)); } /* llvalue -> llvalue -> unit */