]> granicus.if.org Git - llvm/commitdiff
[OCaml] Handle nullptr in Llvm.global_initializer
authorAditya Kumar <hiraditya@msn.com>
Tue, 1 Oct 2019 03:45:09 +0000 (03:45 +0000)
committerAditya Kumar <hiraditya@msn.com>
Tue, 1 Oct 2019 03:45:09 +0000 (03:45 +0000)
LLVMGetInitializer returns nullptr in case there is no
initializer. There is not much that can be done with nullptr in OCaml,
not even test if it is null. Also, there does not seem to be a C or
OCaml API to test if there is an initializer. So this diff changes
Llvm.global_initializer to return an option.

Differential Revision: https://reviews.llvm.org/D65195
Reviewed by: whitequark
Authored by: kren1

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373299 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/ocaml/llvm/llvm.ml
bindings/ocaml/llvm/llvm.mli
bindings/ocaml/llvm/llvm_ocaml.c

index f3ff600bde4f3bb254204f2d9b228d4b56c1bdb1..587972429f332e6265ee870c8165bb5bee4d6e10 100644 (file)
@@ -710,7 +710,7 @@ external define_qualified_global : string -> llvalue -> int -> llmodule ->
 external lookup_global : string -> llmodule -> llvalue option
                        = "llvm_lookup_global"
 external delete_global : llvalue -> unit = "llvm_delete_global"
-external global_initializer : llvalue -> llvalue = "LLVMGetInitializer"
+external global_initializer : llvalue -> llvalue option = "llvm_global_initializer"
 external set_initializer : llvalue -> llvalue -> unit = "llvm_set_initializer"
 external remove_initializer : llvalue -> unit = "llvm_remove_initializer"
 external is_thread_local : llvalue -> bool = "llvm_is_thread_local"
index 6e145fa44f0458ada607944939457569f662be17..5fedd57971dddac95b80a4f71990abff6f2e643f 100644 (file)
@@ -1454,9 +1454,9 @@ val is_global_constant : llvalue -> bool
     See the method [llvm::GlobalVariable::setConstant]. *)
 val set_global_constant : bool -> llvalue -> unit
 
-(** [global_initializer gv] returns the initializer for the global variable
-    [gv]. See the method [llvm::GlobalVariable::getInitializer]. *)
-val global_initializer : llvalue -> llvalue
+(** [global_initializer gv] If global variable [gv] has an initializer it is returned,
+    otherwise returns [None]. See the method [llvm::GlobalVariable::getInitializer]. *)
+val global_initializer : llvalue -> llvalue option
 
 (** [set_initializer c gv] sets the initializer for the global variable
     [gv] to the constant [c].
index 6af34bd9c172098123c2e7ed1848831a238f9a50..abff3ee04a05a34b04a0191b4594dfa1baa2c528 100644 (file)
@@ -1261,6 +1261,18 @@ CAMLprim value llvm_delete_global(LLVMValueRef GlobalVar) {
   return Val_unit;
 }
 
+/* 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));
+}
+
 /* llvalue -> llvalue -> unit */
 CAMLprim value llvm_set_initializer(LLVMValueRef ConstantVal,
                                     LLVMValueRef GlobalVar) {