llvm.org GIT mirror llvm / dc1ce7b
C and Objective Caml bindings for the various getParent methods of the IR. Based on Erick Tryzelaar's patch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48523 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 12 years ago
6 changed file(s) with 158 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
269269 = "LLVMConstShuffleVector"
270270
271271 (*--... Operations on global variables, functions, and aliases (globals) ...--*)
272 external global_parent : llvalue -> llmodule = "LLVMGetGlobalParent"
272273 external is_declaration : llvalue -> bool = "llvm_is_declaration"
273274 external linkage : llvalue -> Linkage.t = "llvm_linkage"
274275 external set_linkage : Linkage.t -> llvalue -> unit = "llvm_set_linkage"
305306 external lookup_function : string -> llmodule -> llvalue option
306307 = "llvm_lookup_function"
307308 external delete_function : llvalue -> unit = "llvm_delete_function"
308 external params : llvalue -> llvalue array = "llvm_params"
309 external param : llvalue -> int -> llvalue = "llvm_param"
310309 external is_intrinsic : llvalue -> bool = "llvm_is_intrinsic"
311310 external function_call_conv : llvalue -> int = "llvm_function_call_conv"
312311 external set_function_call_conv : int -> llvalue -> unit
316315
317316 (* TODO: param attrs *)
318317
318 (*--... Operations on params ...............................................--*)
319 external params : llvalue -> llvalue array = "llvm_params"
320 external param : llvalue -> int -> llvalue = "llvm_param"
321 external param_parent : llvalue -> llvalue = "LLVMGetParamParent"
322
319323 (*--... Operations on basic blocks .........................................--*)
324 external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
325 external value_is_block : llvalue -> bool = "llvm_value_is_block"
326 external block_of_value : llvalue -> llbasicblock = "LLVMValueAsBasicBlock"
327 external block_parent : llbasicblock -> llvalue = "LLVMGetBasicBlockParent"
320328 external basic_blocks : llvalue -> llbasicblock array = "llvm_basic_blocks"
321329 external entry_block : llvalue -> llbasicblock = "LLVMGetEntryBasicBlock"
322330 external delete_block : llbasicblock -> unit = "llvm_delete_block"
323331 external append_block : string -> llvalue -> llbasicblock = "llvm_append_block"
324332 external insert_block : string -> llbasicblock -> llbasicblock
325333 = "llvm_insert_block"
326 external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
327 external value_is_block : llvalue -> bool = "llvm_value_is_block"
328 external block_of_value : llvalue -> llbasicblock = "LLVMValueAsBasicBlock"
334
335 (*--... Operations on instructions .........................................--*)
336 external instr_parent : llvalue -> llbasicblock = "LLVMGetInstructionParent"
329337
330338 (*--... Operations on call sites ...........................................--*)
331339 external instruction_call_conv: llvalue -> int
340348
341349
342350 (*===-- Instruction builders ----------------------------------------------===*)
343 external builder: unit-> llbuilder
344 = "llvm_builder"
351 external builder: unit-> llbuilder = "llvm_builder"
345352 external builder_before : llvalue -> llbuilder = "llvm_builder_before"
346353 external builder_at_end : llbasicblock -> llbuilder = "llvm_builder_at_end"
347354 external position_before : llvalue -> llbuilder -> unit = "llvm_position_before"
348355 external position_at_end : llbasicblock -> llbuilder -> unit
349356 = "llvm_position_at_end"
357 external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
350358
351359 (*--... Terminators ........................................................--*)
352360 external build_ret_void : llbuilder -> llvalue = "llvm_build_ret_void"
675675
676676 (** {7 Operations on global variables, functions, and aliases (globals)} *)
677677
678 (** [global_parent g] is the enclosing module of the global value [g].
679 See the method [llvm::GlobalValue::getParent]. *)
680 external global_parent : llvalue -> llmodule = "LLVMGetGlobalParent"
681
678682 (** [is_declaration g] returns [true] if the global value [g] is a declaration
679683 only. Returns [false] otherwise.
680684 See the method [llvm::GlobalValue::isDeclaration]. *)
807811 See the method [llvm::Function::eraseFromParent]. *)
808812 external delete_function : llvalue -> unit = "llvm_delete_function"
809813
810 (** [params f] returns the parameters of function [f].
811 See the method [llvm::Function::getArgumentList]. *)
812 external params : llvalue -> llvalue array = "llvm_params"
813
814 (** [param f n] returns the [n]th parameter of function [f].
815 See the method [llvm::Function::getArgumentList]. *)
816 external param : llvalue -> int -> llvalue = "llvm_param"
817
818814 (** [is_intrinsic f] returns true if the function [f] is an intrinsic.
819815 See the method [llvm::Function::isIntrinsic]. *)
820816 external is_intrinsic : llvalue -> bool = "llvm_is_intrinsic"
839835 external set_collector : string option -> llvalue -> unit = "llvm_set_collector"
840836
841837
838 (** {7 Operations on params} *)
839
840 (** [params f] returns the parameters of function [f].
841 See the method [llvm::Function::getArgumentList]. *)
842 external params : llvalue -> llvalue array = "llvm_params"
843
844 (** [param f n] returns the [n]th parameter of function [f].
845 See the method [llvm::Function::getArgumentList]. *)
846 external param : llvalue -> int -> llvalue = "llvm_param"
847
848 (** [param_parent p] returns the parent function that owns the parameter.
849 See the method [llvm::Argument::getParent]. *)
850 external param_parent : llvalue -> llvalue = "LLVMGetParamParent"
851
852
842853 (** {7 Operations on basic blocks} *)
843854
844855 (** [basic_blocks fn] returns the basic blocks of the function [f].
864875 external insert_block : string -> llbasicblock -> llbasicblock
865876 = "llvm_insert_block"
866877
878 (** [block_parent bb] returns the parent function that owns the basic block.
879 See the method [llvm::BasicBlock::getParent]. *)
880 external block_parent : llbasicblock -> llvalue = "LLVMGetBasicBlockParent"
881
867882 (** [value_of_block bb] losslessly casts [bb] to an [llvalue]. *)
868883 external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
869884
874889
875890 (** [block_of_value v] losslessly casts [v] to an [llbasicblock]. *)
876891 external block_of_value : llvalue -> llbasicblock = "LLVMValueAsBasicBlock"
892
893
894 (** {7 Operations on instructions} *)
895
896 (** [instr_parent i] is the enclosing basic block of the instruction [i].
897 See the method [llvm::Instruction::getParent]. *)
898 external instr_parent : llvalue -> llbasicblock = "LLVMGetInstructionParent"
877899
878900
879901 (** {7 Operations on call sites} *)
885907 external instruction_call_conv: llvalue -> int
886908 = "llvm_instruction_call_conv"
887909
888 (** [set_inst_call_conv cc ci] sets the calling convention for the call or
889 invoke instruction [ci] to the integer [cc], which can be one of the values
890 from the module {!CallConv}. See the method [llvm::CallInst::setCallingConv]
910 (** [set_instruction_call_conv cc ci] sets the calling convention for the call
911 or invoke instruction [ci] to the integer [cc], which can be one of the
912 values from the module {!CallConv}.
913 See the method [llvm::CallInst::setCallingConv]
891914 and [llvm::InvokeInst::setCallingConv]. *)
892915 external set_instruction_call_conv: int -> llvalue -> unit
893916 = "llvm_set_instruction_call_conv"
908931
909932 (** {6 Instruction builders} *)
910933
911 (** [builder] creates an instruction builder with no position. It is invalid to
912 use this builder until its position is set with {!position_before} or
934 (** [builder ()] creates an instruction builder with no position. It is invalid
935 to use this builder until its position is set with {!position_before} or
913936 {!position_at_end}. See the constructor for [llvm::LLVMBuilder]. *)
914937 external builder: unit-> llbuilder
915938 = "llvm_builder"
930953 basic block [bb]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
931954 external position_at_end : llbasicblock -> llbuilder -> unit
932955 = "llvm_position_at_end"
956
957 (** [insertion_block b] returns the basic block that the builder [b] is
958 positioned to insert into. Raises [Not_Found] if the instruction builder is
959 uninitialized.
960 See the method [llvm::LLVMBuilder::GetInsertBlock]. *)
961 external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
933962
934963
935964 (** {7 Terminators} *)
760760 return Val_unit;
761761 }
762762
763 /* llbuilder -> llbasicblock */
764 CAMLprim LLVMBasicBlockRef llvm_insertion_block(LLVMBuilderRef B) {
765 LLVMBasicBlockRef InsertBlock = LLVMGetInsertBlock(Builder_val(B));
766 if (!InsertBlock)
767 raise_not_found();
768 return InsertBlock;
769 }
770
763771 /*--... Terminators ........................................................--*/
764772
765773 /* llbuilder -> llvalue */
361361 LLVMValueRef MaskConstant);
362362
363363 /* Operations on global variables, functions, and aliases (globals) */
364 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global);
364365 int LLVMIsDeclaration(LLVMValueRef Global);
365366 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global);
366367 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage);
388389 LLVMTypeRef FunctionTy);
389390 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name);
390391 void LLVMDeleteFunction(LLVMValueRef Fn);
391 unsigned LLVMCountParams(LLVMValueRef Fn);
392 void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params);
393 LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index);
394392 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn);
395393 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn);
396394 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC);
397395 const char *LLVMGetCollector(LLVMValueRef Fn);
398396 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll);
399397
398 /* Operations on parameters */
399 unsigned LLVMCountParams(LLVMValueRef Fn);
400 void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params);
401 LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index);
402 LLVMValueRef LLVMGetParamParent(LLVMValueRef Inst);
403
400404 /* Operations on basic blocks */
401405 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb);
402406 int LLVMValueIsBasicBlock(LLVMValueRef Val);
403407 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val);
408 LLVMValueRef LLVMGetBasicBlockParent(LLVMValueRef V);
404409 unsigned LLVMCountBasicBlocks(LLVMValueRef Fn);
405410 void LLVMGetBasicBlocks(LLVMValueRef Fn, LLVMBasicBlockRef *BasicBlocks);
406411 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn);
409414 const char *Name);
410415 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BB);
411416
417 /* Operations on instructions */
418 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst);
419
412420 /* Operations on call sites */
413421 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC);
414422 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr);
429437 LLVMBuilderRef LLVMCreateBuilder();
430438 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
431439 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
440 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
432441 void LLVMDisposeBuilder(LLVMBuilderRef Builder);
433442
434443 /* Terminators */
214214 return wrap(llvm::OpaqueType::get());
215215 }
216216
217 /* Operations on type handles */
217 /*--.. Operations on type handles ..........................................--*/
218218
219219 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
220220 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
545545
546546 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
547547
548 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
549 return wrap(unwrap(Global)->getParent());
550 }
551
548552 int LLVMIsDeclaration(LLVMValueRef Global) {
549553 return unwrap(Global)->isDeclaration();
550554 }
643647
644648 void LLVMDeleteFunction(LLVMValueRef Fn) {
645649 unwrap(Fn)->eraseFromParent();
646 }
647
648 unsigned LLVMCountParams(LLVMValueRef FnRef) {
649 // This function is strictly redundant to
650 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
651 return unwrap(FnRef)->getArgumentList().size();
652 }
653
654 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
655 Function::arg_iterator AI = unwrap(FnRef)->arg_begin();
656 while (index --> 0)
657 AI++;
658 return wrap(AI);
659 }
660
661 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
662 Function *Fn = unwrap(FnRef);
663 for (Function::arg_iterator I = Fn->arg_begin(),
664 E = Fn->arg_end(); I != E; I++)
665 *ParamRefs++ = wrap(I);
666650 }
667651
668652 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
692676 F->clearCollector();
693677 }
694678
679 /*--.. Operations on parameters ............................................--*/
680
681 unsigned LLVMCountParams(LLVMValueRef FnRef) {
682 // This function is strictly redundant to
683 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
684 return unwrap(FnRef)->getArgumentList().size();
685 }
686
687 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
688 Function *Fn = unwrap(FnRef);
689 for (Function::arg_iterator I = Fn->arg_begin(),
690 E = Fn->arg_end(); I != E; I++)
691 *ParamRefs++ = wrap(I);
692 }
693
694 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
695 Function::arg_iterator AI = unwrap(FnRef)->arg_begin();
696 while (index --> 0)
697 AI++;
698 return wrap(AI);
699 }
700
701 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
702 return wrap(unwrap(V)->getParent());
703 }
704
695705 /*--.. Operations on basic blocks ..........................................--*/
696706
697 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
698 return wrap(static_cast(unwrap(Bb)));
707 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
708 return wrap(static_cast(unwrap(BB)));
699709 }
700710
701711 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
704714
705715 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
706716 return wrap(unwrap(Val));
717 }
718
719 LLVMValueRef LLVMGetBasicBlockParent(LLVMValueRef V) {
720 return wrap(unwrap(V)->getParent());
707721 }
708722
709723 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
733747
734748 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
735749 unwrap(BBRef)->eraseFromParent();
750 }
751
752 /*--.. Operations on instructions ..........................................--*/
753
754 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
755 return wrap(unwrap(Inst)->getParent());
736756 }
737757
738758 /*--.. Call and invoke instructions ........................................--*/
792812 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
793813 BasicBlock *BB = unwrap(Block);
794814 unwrap(Builder)->SetInsertPoint(BB);
815 }
816
817 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
818 return wrap(unwrap(Builder)->GetInsertBlock());
795819 }
796820
797821 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
477477
478478 (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll
479479 *)
480 group "declare";
481 insist (None = lookup_function "Fn1" m);
482 let fn = declare_function "Fn1" ty m in
483 insist (pointer_type ty = type_of fn);
484 insist (is_declaration fn);
485 insist (0 = Array.length (basic_blocks fn));
486 insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
487 insist (fn == declare_function "Fn1" ty m);
488 insist (None <> lookup_function "Fn1" m);
489 insist (match lookup_function "Fn1" m with Some x -> x = fn
490 | None -> false);
480 begin group "declare";
481 insist (None = lookup_function "Fn1" m);
482 let fn = declare_function "Fn1" ty m in
483 insist (pointer_type ty = type_of fn);
484 insist (is_declaration fn);
485 insist (0 = Array.length (basic_blocks fn));
486 insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
487 insist (fn == declare_function "Fn1" ty m);
488 insist (None <> lookup_function "Fn1" m);
489 insist (match lookup_function "Fn1" m with Some x -> x = fn
490 | None -> false);
491 insist (m == global_parent fn)
492 end;
491493
492494 (* RUN: grep -v {Fn2} < %t.ll
493495 *)
591593
592594 let test_builder () =
593595 let (++) x f = f x; x in
596
597 begin group "parent";
598 insist (try
599 ignore (insertion_block (builder ()));
600 false
601 with Not_found ->
602 true);
603
604 let fty = function_type void_type [| i32_type |] in
605 let fn = define_function "BuilderParent" fty m in
606 let bb = entry_block fn in
607 let b = builder_at_end bb in
608 let p = param fn 0 in
609 let sum = build_add p p "sum" b in
610 ignore (build_ret_void b);
611
612 insist (fn = block_parent bb);
613 insist (fn = param_parent p);
614 insist (bb = instr_parent sum);
615 insist (bb = insertion_block b)
616 end;
594617
595618 group "ret void";
596619 begin