From 0947d0e38ba18b32e8eb2b76009d558bc4b2346e Mon Sep 17 00:00:00 2001 From: Peter Zotov Date: Tue, 28 Oct 2014 19:46:48 +0000 Subject: [PATCH] [OCaml] PR19859: Add Llvm.{fcmp_predicate,float_of_const}. Patch by Gabriel Radanne . git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220815 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/ocaml/llvm/llvm.ml | 3 +++ bindings/ocaml/llvm/llvm.mli | 9 +++++++++ bindings/ocaml/llvm/llvm_ocaml.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index dae5bdab095..0c434afb34a 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -466,6 +466,8 @@ external int64_of_const : llvalue -> Int64.t option external const_int_of_string : lltype -> string -> int -> llvalue = "llvm_const_int_of_string" external const_float : lltype -> float -> llvalue = "llvm_const_float" +external float_of_const : llvalue -> float option + = "llvm_float_of_const" external const_float_of_string : lltype -> string -> llvalue = "llvm_const_float_of_string" @@ -955,6 +957,7 @@ external instr_pred : llvalue -> (llbasicblock, llvalue) llrev_pos external instr_opcode : llvalue -> Opcode.t = "llvm_instr_get_opcode" external icmp_predicate : llvalue -> Icmp.t option = "llvm_instr_icmp_predicate" +external fcmp_predicate : llvalue -> Fcmp.t option = "llvm_instr_fcmp_predicate" external instr_clone : llvalue -> llvalue = "llvm_instr_clone" let rec iter_instrs_range f i e = diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index 8a205ce1efa..10aa706b70e 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -842,6 +842,11 @@ val const_int_of_string : lltype -> string -> int -> llvalue value [n]. See the method [llvm::ConstantFP::get]. *) val const_float : lltype -> float -> llvalue +(** [float_of_const c] returns the float value of the [c] constant float. + None is returned if this is not an float constant. + See the method [llvm::ConstantFP::getDoubleValue].*) +val float_of_const : llvalue -> float option + (** [const_float_of_string ty s] returns the floating point constant of type [ty] and value [n]. See the method [llvm::ConstantFP::get]. *) val const_float_of_string : lltype -> string -> llvalue @@ -1699,6 +1704,10 @@ val instr_opcode : llvalue -> Opcode.t instruction [i]. *) val icmp_predicate : llvalue -> Icmp.t option +(** [fcmp_predicate i] returns the [fcmp.t] corresponding to an [fcmp] + instruction [i]. *) +val fcmp_predicate : llvalue -> Fcmp.t option + (** [inst_clone i] returns a copy of instruction [i], The instruction has no parent, and no name. See the method [llvm::Instruction::clone]. *) diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index 93a9972379c..02107223eb6 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -734,6 +734,22 @@ CAMLprim LLVMValueRef llvm_const_float(LLVMTypeRef RealTy, value N) { return LLVMConstReal(RealTy, Double_val(N)); } + +/* llvalue -> float */ +CAMLprim value llvm_float_of_const(LLVMValueRef Const) +{ + if (LLVMIsAConstantFP(Const)) { + LLVMBool LosesInfo; + double res = LLVMConstRealGetDouble(Const, &LosesInfo); + if (LosesInfo) + return Val_int(0); + value Option = alloc(1, 0); + Field(Option, 0) = caml_copy_double(res); + return Option; + } + return Val_int(0); +} + /* lltype -> string -> llvalue */ CAMLprim LLVMValueRef llvm_const_float_of_string(LLVMTypeRef RealTy, value S) { return LLVMConstRealOfStringAndSize(RealTy, String_val(S), @@ -1358,6 +1374,18 @@ CAMLprim value llvm_instr_icmp_predicate(LLVMValueRef Val) { CAMLreturn(Val_int(0)); } +/* llvalue -> FCmp.t option */ +CAMLprim value llvm_instr_fcmp_predicate(LLVMValueRef Val) { + CAMLparam0(); + int x = LLVMGetFCmpPredicate(Val); + if (x) { + value Option = alloc(1, 0); + Field(Option, 0) = Val_int(x - LLVMRealPredicateFalse); + CAMLreturn(Option); + } + CAMLreturn(Val_int(0)); +} + /* llvalue -> llvalue */ CAMLprim LLVMValueRef llvm_instr_clone(LLVMValueRef Inst) { if (!LLVMIsAInstruction(Inst)) -- 2.34.1