diff options
-rw-r--r-- | bindings/ocaml/llvm/llvm.ml | 2 | ||||
-rw-r--r-- | bindings/ocaml/llvm/llvm.mli | 11 | ||||
-rw-r--r-- | bindings/ocaml/llvm/llvm_ocaml.c | 7 | ||||
-rw-r--r-- | test/Bindings/Ocaml/vmcore.ml | 13 |
4 files changed, 30 insertions, 3 deletions
diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index 85acc5e578..d178dcc001 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -619,6 +619,8 @@ external build_cond_br : llvalue -> llbasicblock -> llbasicblock -> llbuilder -> llvalue = "llvm_build_cond_br" external build_switch : llvalue -> llbasicblock -> int -> llbuilder -> llvalue = "llvm_build_switch" +external add_case : llvalue -> llvalue -> llbasicblock -> unit + = "llvm_add_case" external build_invoke : llvalue -> llvalue array -> llbasicblock -> llbasicblock -> string -> llbuilder -> llvalue = "llvm_build_invoke_bc" "llvm_build_invoke_nat" diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index 5aedefb925..a7d26def21 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -1212,13 +1212,20 @@ external build_br : llbasicblock -> llbuilder -> llvalue = "llvm_build_br" external build_cond_br : llvalue -> llbasicblock -> llbasicblock -> llbuilder -> llvalue = "llvm_build_cond_br" -(** [build_switch case elsebb b] creates an empty +(** [build_switch case elsebb count b] creates an empty [switch %case, %elsebb] - instruction at the position specified by the instruction builder [b]. + instruction at the position specified by the instruction builder [b] with + space reserved for [count] cases. See the method [llvm::LLVMBuilder::CreateSwitch]. *) external build_switch : llvalue -> llbasicblock -> int -> llbuilder -> llvalue = "llvm_build_switch" +(** [add_case sw onval bb] causes switch instruction [sw] to branch to [bb] + when its input matches the constant [onval]. + See the method [llvm::SwitchInst::addCase]. **) +external add_case : llvalue -> llvalue -> llbasicblock -> unit + = "llvm_add_case" + (** [build_invoke fn args tobb unwindbb name b] creates an [%name = invoke %fn(args) to %tobb unwind %unwindbb] instruction at the position specified by the instruction builder [b]. diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index 76ad408c35..94bd374594 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -848,6 +848,13 @@ CAMLprim LLVMValueRef llvm_build_switch(LLVMValueRef Of, return LLVMBuildSwitch(Builder_val(B), Of, Else, Int_val(EstimatedCount)); } +CAMLprim value llvm_add_case(LLVMValueRef Switch, + LLVMValueRef OnVal, + LLVMBasicBlockRef Dest) { + LLVMAddCase(Switch, OnVal, Dest); + return Val_unit; +} + /* llvalue -> llvalue array -> llbasicblock -> llbasicblock -> string -> llbuilder -> llvalue */ CAMLprim LLVMValueRef llvm_build_invoke_nat(LLVMValueRef Fn, value Args, diff --git a/test/Bindings/Ocaml/vmcore.ml b/test/Bindings/Ocaml/vmcore.ml index ffb69701c8..24846b68bb 100644 --- a/test/Bindings/Ocaml/vmcore.ml +++ b/test/Bindings/Ocaml/vmcore.ml @@ -825,7 +825,18 @@ let test_builder () = ignore (build_cond_br cond bb03 bb00 b) end; - (* TODO: Switch *) + group "switch"; begin + (* RUN: grep {switch.*P1.*SwiBlock3} < %t.ll + * RUN: grep {2,.*SwiBlock2} < %t.ll + *) + let bb1 = append_block "SwiBlock1" fn in + let bb2 = append_block "SwiBlock2" fn in + ignore (build_unreachable (builder_at_end bb2)); + let bb3 = append_block "SwiBlock3" fn in + ignore (build_unreachable (builder_at_end bb3)); + let si = build_switch p1 bb3 1 (builder_at_end bb1) in + ignore (add_case si (const_int i32_type 2) bb2) + end; group "invoke"; begin (* RUN: grep {Inst02.*invoke.*P1.*P2} < %t.ll |