llvm.org GIT mirror llvm / 4733be3
Objective Caml bindings for basic block, function, global, and arg iterators. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48711 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 12 years ago
6 changed file(s) with 605 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
100100
101101 external register_exns : exn -> unit = "llvm_register_core_exns"
102102 let _ = register_exns (IoError "")
103
104 type ('a, 'b) llpos =
105 | At_end of 'a
106 | Before of 'b
107
108 type ('a, 'b) llrev_pos =
109 | At_start of 'a
110 | After of 'b
103111
104112
105113 (*===-- Modules -----------------------------------------------------------===*)
297305 external remove_initializer : llvalue -> unit = "llvm_remove_initializer"
298306 external is_thread_local : llvalue -> bool = "llvm_is_thread_local"
299307 external set_thread_local : bool -> llvalue -> unit = "llvm_set_thread_local"
308 external global_begin : llmodule -> (llmodule, llvalue) llpos
309 = "llvm_global_begin"
310 external global_succ : llvalue -> (llmodule, llvalue) llpos
311 = "llvm_global_succ"
312 external global_end : llmodule -> (llmodule, llvalue) llrev_pos
313 = "llvm_global_end"
314 external global_pred : llvalue -> (llmodule, llvalue) llrev_pos
315 = "llvm_global_pred"
316
317 let rec iter_global_range f i e =
318 if i = e then () else
319 match i with
320 | At_end _ -> raise (Invalid_argument "Invalid global variable range.")
321 | Before bb ->
322 f bb;
323 iter_global_range f (global_succ bb) e
324
325 let iter_globals f m =
326 iter_global_range f (global_begin m) (At_end m)
327
328 let rec fold_left_global_range f init i e =
329 if i = e then init else
330 match i with
331 | At_end _ -> raise (Invalid_argument "Invalid global variable range.")
332 | Before bb -> fold_left_global_range f (f init bb) (global_succ bb) e
333
334 let fold_left_globals f init m =
335 fold_left_global_range f init (global_begin m) (At_end m)
336
337 let rec rev_iter_global_range f i e =
338 if i = e then () else
339 match i with
340 | At_start _ -> raise (Invalid_argument "Invalid global variable range.")
341 | After bb ->
342 f bb;
343 rev_iter_global_range f (global_pred bb) e
344
345 let rev_iter_globals f m =
346 rev_iter_global_range f (global_end m) (At_start m)
347
348 let rec fold_right_global_range f i e init =
349 if i = e then init else
350 match i with
351 | At_start _ -> raise (Invalid_argument "Invalid global variable range.")
352 | After bb -> fold_right_global_range f (global_pred bb) e (f bb init)
353
354 let fold_right_globals f m init =
355 fold_right_global_range f (global_end m) (At_start m) init
300356
301357 (*--... Operations on functions ............................................--*)
302358 external declare_function : string -> lltype -> llmodule -> llvalue
312368 = "llvm_set_function_call_conv"
313369 external collector : llvalue -> string option = "llvm_collector"
314370 external set_collector : string option -> llvalue -> unit = "llvm_set_collector"
371 external function_begin : llmodule -> (llmodule, llvalue) llpos
372 = "llvm_function_begin"
373 external function_succ : llvalue -> (llmodule, llvalue) llpos
374 = "llvm_function_succ"
375 external function_end : llmodule -> (llmodule, llvalue) llrev_pos
376 = "llvm_function_end"
377 external function_pred : llvalue -> (llmodule, llvalue) llrev_pos
378 = "llvm_function_pred"
379
380 let rec iter_function_range f i e =
381 if i = e then () else
382 match i with
383 | At_end _ -> raise (Invalid_argument "Invalid function range.")
384 | Before fn ->
385 f fn;
386 iter_function_range f (function_succ fn) e
387
388 let iter_functions f m =
389 iter_function_range f (function_begin m) (At_end m)
390
391 let rec fold_left_function_range f init i e =
392 if i = e then init else
393 match i with
394 | At_end _ -> raise (Invalid_argument "Invalid function range.")
395 | Before fn -> fold_left_function_range f (f init fn) (function_succ fn) e
396
397 let fold_left_functions f init m =
398 fold_left_function_range f init (function_begin m) (At_end m)
399
400 let rec rev_iter_function_range f i e =
401 if i = e then () else
402 match i with
403 | At_start _ -> raise (Invalid_argument "Invalid function range.")
404 | After fn ->
405 f fn;
406 rev_iter_function_range f (function_pred fn) e
407
408 let rev_iter_functions f m =
409 rev_iter_function_range f (function_end m) (At_start m)
410
411 let rec fold_right_function_range f i e init =
412 if i = e then init else
413 match i with
414 | At_start _ -> raise (Invalid_argument "Invalid function range.")
415 | After fn -> fold_right_function_range f (function_pred fn) e (f fn init)
416
417 let fold_right_functions f m init =
418 fold_right_function_range f (function_end m) (At_start m) init
315419
316420 (* TODO: param attrs *)
317421
319423 external params : llvalue -> llvalue array = "llvm_params"
320424 external param : llvalue -> int -> llvalue = "llvm_param"
321425 external param_parent : llvalue -> llvalue = "LLVMGetParamParent"
426 external param_begin : llvalue -> (llvalue, llvalue) llpos = "llvm_param_begin"
427 external param_succ : llvalue -> (llvalue, llvalue) llpos = "llvm_param_succ"
428 external param_end : llvalue -> (llvalue, llvalue) llrev_pos = "llvm_param_end"
429 external param_pred : llvalue -> (llvalue, llvalue) llrev_pos ="llvm_param_pred"
430
431 let rec iter_param_range f i e =
432 if i = e then () else
433 match i with
434 | At_end _ -> raise (Invalid_argument "Invalid parameter range.")
435 | Before p ->
436 f p;
437 iter_param_range f (param_succ p) e
438
439 let iter_params f fn =
440 iter_param_range f (param_begin fn) (At_end fn)
441
442 let rec fold_left_param_range f init i e =
443 if i = e then init else
444 match i with
445 | At_end _ -> raise (Invalid_argument "Invalid parameter range.")
446 | Before p -> fold_left_param_range f (f init p) (param_succ p) e
447
448 let fold_left_params f init fn =
449 fold_left_param_range f init (param_begin fn) (At_end fn)
450
451 let rec rev_iter_param_range f i e =
452 if i = e then () else
453 match i with
454 | At_start _ -> raise (Invalid_argument "Invalid parameter range.")
455 | After p ->
456 f p;
457 rev_iter_param_range f (param_pred p) e
458
459 let rev_iter_params f fn =
460 rev_iter_param_range f (param_end fn) (At_start fn)
461
462 let rec fold_right_param_range f init i e =
463 if i = e then init else
464 match i with
465 | At_start _ -> raise (Invalid_argument "Invalid parameter range.")
466 | After p -> fold_right_param_range f (f p init) (param_pred p) e
467
468 let fold_right_params f fn init =
469 fold_right_param_range f init (param_end fn) (At_start fn)
322470
323471 (*--... Operations on basic blocks .........................................--*)
324472 external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
331479 external append_block : string -> llvalue -> llbasicblock = "llvm_append_block"
332480 external insert_block : string -> llbasicblock -> llbasicblock
333481 = "llvm_insert_block"
482 external block_begin : llvalue -> (llvalue, llbasicblock) llpos
483 = "llvm_block_begin"
484 external block_succ : llbasicblock -> (llvalue, llbasicblock) llpos
485 = "llvm_block_succ"
486 external block_end : llvalue -> (llvalue, llbasicblock) llrev_pos
487 = "llvm_block_end"
488 external block_pred : llbasicblock -> (llvalue, llbasicblock) llrev_pos
489 = "llvm_block_pred"
490
491 let rec iter_block_range f i e =
492 if i = e then () else
493 match i with
494 | At_end _ -> raise (Invalid_argument "Invalid block range.")
495 | Before bb ->
496 f bb;
497 iter_block_range f (block_succ bb) e
498
499 let iter_blocks f fn =
500 iter_block_range f (block_begin fn) (At_end fn)
501
502 let rec fold_left_block_range f init i e =
503 if i = e then init else
504 match i with
505 | At_end _ -> raise (Invalid_argument "Invalid block range.")
506 | Before bb -> fold_left_block_range f (f init bb) (block_succ bb) e
507
508 let fold_left_blocks f init fn =
509 fold_left_block_range f init (block_begin fn) (At_end fn)
510
511 let rec rev_iter_block_range f i e =
512 if i = e then () else
513 match i with
514 | At_start _ -> raise (Invalid_argument "Invalid block range.")
515 | After bb ->
516 f bb;
517 rev_iter_block_range f (block_pred bb) e
518
519 let rev_iter_blocks f fn =
520 rev_iter_block_range f (block_end fn) (At_start fn)
521
522 let rec fold_right_block_range f init i e =
523 if i = e then init else
524 match i with
525 | At_start _ -> raise (Invalid_argument "Invalid block range.")
526 | After bb -> fold_right_block_range f (f bb init) (block_pred bb) e
527
528 let fold_right_blocks f fn init =
529 fold_right_block_range f init (block_end fn) (At_start fn)
334530
335531 (*--... Operations on instructions .........................................--*)
336532 external instr_parent : llvalue -> llbasicblock = "LLVMGetInstructionParent"
148148 end
149149
150150
151 (** {6 Iteration} *)
152
153 (** [Before b] and [At_end a] specify positions from the start of the ['b] list
154 of [a]. [llpos] is used to specify positions in and for reverse iteration
155 through the various value lists maintained by the LLVM IR. *)
156 type ('a, 'b) llpos =
157 | At_end of 'a
158 | Before of 'b
159
160 (** [After b] and [At_start a] specify positions from the end of the ['b] list
161 of [a]. [llrev_pos] is used for reverse iteration through the various value
162 lists maintained by the LLVM IR. *)
163 type ('a, 'b) llrev_pos =
164 | At_start of 'a
165 | After of 'b
166
167
151168 (** {6 Exceptions} *)
152169
153170 exception IoError of string
744761 See the method [llvm::GlobalVariable::eraseFromParent]. *)
745762 external delete_global : llvalue -> unit = "llvm_delete_global"
746763
764 (** [global_begin m] returns the first position in the global variable list of
765 the module [m]. [global_begin] and [global_succ] can be used to iterate
766 over the global list in order.
767 See the method [llvm::Module::global_begin]. *)
768 external global_begin : llmodule -> (llmodule, llvalue) llpos
769 = "llvm_global_begin"
770
771 (** [global_succ gv] returns the global variable list position succeeding
772 [Before gv].
773 See the method [llvm::Module::global_iterator::operator++]. *)
774 external global_succ : llvalue -> (llmodule, llvalue) llpos
775 = "llvm_global_succ"
776
777 (** [iter_globals f m] applies function [f] to each of the global variables of
778 module [m] in order. Tail recursive. *)
779 val iter_globals : (llvalue -> unit) -> llmodule -> unit
780
781 (** [fold_left_globals f init m] is [f (... (f init g1) ...) gN] where
782 [g1,...,gN] are the global variables of module [m]. Tail recursive. *)
783 val fold_left_globals : ('a -> llvalue -> 'a) -> 'a -> llmodule -> 'a
784
785 (** [global_end m] returns the last position in the global variable list of the
786 module [m]. [global_end] and [global_pred] can be used to iterate over the
787 global list in reverse.
788 See the method [llvm::Module::global_end]. *)
789 external global_end : llmodule -> (llmodule, llvalue) llrev_pos
790 = "llvm_global_end"
791
792 (** [global_pred gv] returns the global variable list position preceding
793 [After gv].
794 See the method [llvm::Module::global_iterator::operator--]. *)
795 external global_pred : llvalue -> (llmodule, llvalue) llrev_pos
796 = "llvm_global_pred"
797
798 (** [rev_iter_globals f m] applies function [f] to each of the global variables
799 of module [m] in reverse order. Tail recursive. *)
800 val rev_iter_globals : (llvalue -> unit) -> llmodule -> unit
801
802 (** [fold_right_globals f m init] is [f g1 (... (f gN init) ...)] where
803 [g1,...,gN] are the global variables of module [m]. Tail recursive. *)
804 val fold_right_globals : (llvalue -> 'a -> 'a) -> llmodule -> 'a -> 'a
805
747806 (** [is_global_constant gv] returns [true] if the global variabile [gv] is a
748807 constant. Returns [false] otherwise.
749808 See the method [llvm::GlobalVariable::isConstant]. *)
811870 See the method [llvm::Function::eraseFromParent]. *)
812871 external delete_function : llvalue -> unit = "llvm_delete_function"
813872
873 (** [function_begin m] returns the first position in the function list of the
874 module [m]. [function_begin] and [function_succ] can be used to iterate over
875 the function list in order.
876 See the method [llvm::Module::begin]. *)
877 external function_begin : llmodule -> (llmodule, llvalue) llpos
878 = "llvm_function_begin"
879
880 (** [function_succ gv] returns the function list position succeeding
881 [Before gv].
882 See the method [llvm::Module::iterator::operator++]. *)
883 external function_succ : llvalue -> (llmodule, llvalue) llpos
884 = "llvm_function_succ"
885
886 (** [iter_functions f m] applies function [f] to each of the functions of module
887 [m] in order. Tail recursive. *)
888 val iter_functions : (llvalue -> unit) -> llmodule -> unit
889
890 (** [fold_left_function f init m] is [f (... (f init f1) ...) fN] where
891 [f1,...,fN] are the functions of module [m]. Tail recursive. *)
892 val fold_left_functions : ('a -> llvalue -> 'a) -> 'a -> llmodule -> 'a
893
894 (** [function_end m] returns the last position in the function list of
895 the module [m]. [function_end] and [function_pred] can be used to iterate
896 over the function list in reverse.
897 See the method [llvm::Module::end]. *)
898 external function_end : llmodule -> (llmodule, llvalue) llrev_pos
899 = "llvm_function_end"
900
901 (** [function_pred gv] returns the function list position preceding [After gv].
902 See the method [llvm::Module::iterator::operator--]. *)
903 external function_pred : llvalue -> (llmodule, llvalue) llrev_pos
904 = "llvm_function_pred"
905
906 (** [rev_iter_functions f fn] applies function [f] to each of the functions of
907 module [m] in reverse order. Tail recursive. *)
908 val rev_iter_functions : (llvalue -> unit) -> llmodule -> unit
909
910 (** [fold_right_functions f m init] is [f (... (f init fN) ...) f1] where
911 [f1,...,fN] are the functions of module [m]. Tail recursive. *)
912 val fold_right_functions : (llvalue -> 'a -> 'a) -> llmodule -> 'a -> 'a
913
814914 (** [is_intrinsic f] returns true if the function [f] is an intrinsic.
815915 See the method [llvm::Function::isIntrinsic]. *)
816916 external is_intrinsic : llvalue -> bool = "llvm_is_intrinsic"
849949 See the method [llvm::Argument::getParent]. *)
850950 external param_parent : llvalue -> llvalue = "LLVMGetParamParent"
851951
952 (** [param_begin f] returns the first position in the parameter list of the
953 function [f]. [param_begin] and [param_succ] can be used to iterate over
954 the parameter list in order.
955 See the method [llvm::Function::arg_begin]. *)
956 external param_begin : llvalue -> (llvalue, llvalue) llpos = "llvm_param_begin"
957
958 (** [param_succ bb] returns the parameter list position succeeding
959 [Before bb].
960 See the method [llvm::Function::arg_iterator::operator++]. *)
961 external param_succ : llvalue -> (llvalue, llvalue) llpos = "llvm_param_succ"
962
963 (** [iter_params f fn] applies function [f] to each of the parameters
964 of function [fn] in order. Tail recursive. *)
965 val iter_params : (llvalue -> unit) -> llvalue -> unit
966
967 (** [fold_left_params f init fn] is [f (... (f init b1) ...) bN] where
968 [b1,...,bN] are the parameters of function [fn]. Tail recursive. *)
969 val fold_left_params : ('a -> llvalue -> 'a) -> 'a -> llvalue -> 'a
970
971 (** [param_end f] returns the last position in the parameter list of
972 the function [f]. [param_end] and [param_pred] can be used to iterate
973 over the parameter list in reverse.
974 See the method [llvm::Function::arg_end]. *)
975 external param_end : llvalue -> (llvalue, llvalue) llrev_pos = "llvm_param_end"
976
977 (** [param_pred gv] returns the function list position preceding [After gv].
978 See the method [llvm::Function::arg_iterator::operator--]. *)
979 external param_pred : llvalue -> (llvalue, llvalue) llrev_pos
980 = "llvm_param_pred"
981
982 (** [rev_iter_params f fn] applies function [f] to each of the parameters
983 of function [fn] in reverse order. Tail recursive. *)
984 val rev_iter_params : (llvalue -> unit) -> llvalue -> unit
985
986 (** [fold_right_params f fn init] is [f (... (f init bN) ...) b1] where
987 [b1,...,bN] are the parameters of function [fn]. Tail recursive. *)
988 val fold_right_params : (llvalue -> 'a -> 'a) -> llvalue -> 'a -> 'a
989
852990
853991 (** {7 Operations on basic blocks} *)
854992
8781016 (** [block_parent bb] returns the parent function that owns the basic block.
8791017 See the method [llvm::BasicBlock::getParent]. *)
8801018 external block_parent : llbasicblock -> llvalue = "LLVMGetBasicBlockParent"
1019
1020 (** [block_begin f] returns the first position in the basic block list of the
1021 function [f]. [block_begin] and [block_succ] can be used to iterate over
1022 the basic block list in order.
1023 See the method [llvm::Function::begin]. *)
1024 external block_begin : llvalue -> (llvalue, llbasicblock) llpos
1025 = "llvm_block_begin"
1026
1027 (** [block_succ bb] returns the basic block list position succeeding
1028 [Before bb].
1029 See the method [llvm::Function::iterator::operator++]. *)
1030 external block_succ : llbasicblock -> (llvalue, llbasicblock) llpos
1031 = "llvm_block_succ"
1032
1033 (** [iter_blocks f fn] applies function [f] to each of the basic blocks
1034 of function [fn] in order. Tail recursive. *)
1035 val iter_blocks : (llbasicblock -> unit) -> llvalue -> unit
1036
1037 (** [fold_left_blocks f init fn] is [f (... (f init b1) ...) bN] where
1038 [b1,...,bN] are the basic blocks of function [fn]. Tail recursive. *)
1039 val fold_left_blocks : ('a -> llbasicblock -> 'a) -> 'a -> llvalue -> 'a
1040
1041 (** [block_end f] returns the last position in the basic block list of
1042 the function [f]. [block_end] and [block_pred] can be used to iterate
1043 over the basic block list in reverse.
1044 See the method [llvm::Function::end]. *)
1045 external block_end : llvalue -> (llvalue, llbasicblock) llrev_pos
1046 = "llvm_block_end"
1047
1048 (** [block_pred gv] returns the function list position preceding [After gv].
1049 See the method [llvm::Function::iterator::operator--]. *)
1050 external block_pred : llbasicblock -> (llvalue, llbasicblock) llrev_pos
1051 = "llvm_block_pred"
1052
1053 (** [rev_iter_blocks f fn] applies function [f] to each of the basic blocks
1054 of function [fn] in reverse order. Tail recursive. *)
1055 val rev_iter_blocks : (llbasicblock -> unit) -> llvalue -> unit
1056
1057 (** [fold_right_blocks f fn init] is [f (... (f init bN) ...) b1] where
1058 [b1,...,bN] are the basic blocks of function [fn]. Tail recursive. *)
1059 val fold_right_blocks : (llbasicblock -> 'a -> 'a) -> llvalue -> 'a -> 'a
8811060
8821061 (** [value_of_block bb] losslessly casts [bb] to an [llvalue]. *)
8831062 external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
4949 #endif
5050 }
5151
52 static value alloc_variant(int tag, void *Value) {
53 value Iter = alloc_small(1, tag);
54 Field(Iter, 0) = Val_op(Value);
55 return Iter;
56 }
57
58 /* Macro to convert the C first/next/last/prev idiom to the Ocaml llpos/
59 llrev_pos idiom. */
60 #define DEFINE_ITERATORS(camlname, cname, pty, cty, pfun) \
61 /* llmodule -> ('a, 'b) llpos */ \
62 CAMLprim value llvm_##camlname##_begin(pty Mom) { \
63 cty First = LLVMGetFirst##cname(Mom); \
64 if (First) \
65 return alloc_variant(1, First); \
66 return alloc_variant(0, Mom); \
67 } \
68 \
69 /* llvalue -> ('a, 'b) llpos */ \
70 CAMLprim value llvm_##camlname##_succ(cty Kid) { \
71 cty Next = LLVMGetNext##cname(Kid); \
72 if (Next) \
73 return alloc_variant(1, Next); \
74 return alloc_variant(0, pfun(Kid)) ; \
75 } \
76 \
77 /* llmodule -> ('a, 'b) llrev_pos */ \
78 CAMLprim value llvm_##camlname##_end(pty Mom) { \
79 cty Last = LLVMGetLast##cname(Mom); \
80 if (Last) \
81 return alloc_variant(1, Last); \
82 return alloc_variant(0, Mom); \
83 } \
84 \
85 /* llvalue -> ('a, 'b) llrev_pos */ \
86 CAMLprim value llvm_##camlname##_pred(cty Kid) { \
87 cty Prev = LLVMGetPrevious##cname(Kid); \
88 if (Prev) \
89 return alloc_variant(1, Prev); \
90 return alloc_variant(0, pfun(Kid)); \
91 }
92
5293
5394 /*===-- Modules -----------------------------------------------------------===*/
5495
462503 }
463504
464505 /*--... Operations on global variables .....................................--*/
506
507 DEFINE_ITERATORS(global, Global, LLVMModuleRef, LLVMValueRef,
508 LLVMGetGlobalParent)
465509
466510 /* lltype -> string -> llmodule -> llvalue */
467511 CAMLprim LLVMValueRef llvm_declare_global(LLVMTypeRef Ty, value Name,
540584
541585 /*--... Operations on functions ............................................--*/
542586
587 DEFINE_ITERATORS(function, Function, LLVMModuleRef, LLVMValueRef,
588 LLVMGetGlobalParent)
589
543590 /* string -> lltype -> llmodule -> llvalue */
544591 CAMLprim LLVMValueRef llvm_declare_function(value Name, LLVMTypeRef Ty,
545592 LLVMModuleRef M) {
576623 CAMLprim value llvm_delete_function(LLVMValueRef Fn) {
577624 LLVMDeleteFunction(Fn);
578625 return Val_unit;
579 }
580
581 /* llvalue -> int -> llvalue */
582 CAMLprim LLVMValueRef llvm_param(LLVMValueRef Fn, value Index) {
583 return LLVMGetParam(Fn, Int_val(Index));
584 }
585
586 /* llvalue -> int -> llvalue */
587 CAMLprim value llvm_params(LLVMValueRef Fn, value Index) {
588 value Params = alloc(LLVMCountParams(Fn), 0);
589 LLVMGetParams(Fn, (LLVMValueRef *) Op_val(Params));
590 return Params;
591626 }
592627
593628 /* llvalue -> bool */
629664 return Val_unit;
630665 }
631666
667 /*--... Operations on parameters ...........................................--*/
668
669 DEFINE_ITERATORS(param, Param, LLVMValueRef, LLVMValueRef, LLVMGetParamParent)
670
671 /* llvalue -> int -> llvalue */
672 CAMLprim LLVMValueRef llvm_param(LLVMValueRef Fn, value Index) {
673 return LLVMGetParam(Fn, Int_val(Index));
674 }
675
676 /* llvalue -> int -> llvalue */
677 CAMLprim value llvm_params(LLVMValueRef Fn, value Index) {
678 value Params = alloc(LLVMCountParams(Fn), 0);
679 LLVMGetParams(Fn, (LLVMValueRef *) Op_val(Params));
680 return Params;
681 }
682
632683 /*--... Operations on basic blocks .........................................--*/
684
685 DEFINE_ITERATORS(
686 block, BasicBlock, LLVMValueRef, LLVMBasicBlockRef, LLVMGetBasicBlockParent)
633687
634688 /* llvalue -> llbasicblock array */
635689 CAMLprim value llvm_basic_blocks(LLVMValueRef Fn) {
375375 /* Operations on global variables */
376376 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name);
377377 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name);
378 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M);
379 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M);
380 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar);
381 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar);
378382 void LLVMDeleteGlobal(LLVMValueRef GlobalVar);
379383 int LLVMHasInitializer(LLVMValueRef GlobalVar);
380384 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar);
404408 void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params);
405409 LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index);
406410 LLVMValueRef LLVMGetParamParent(LLVMValueRef Inst);
411 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn);
412 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn);
413 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg);
414 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg);
407415
408416 /* Operations on basic blocks */
409 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb);
417 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB);
410418 int LLVMValueIsBasicBlock(LLVMValueRef Val);
411419 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val);
412 LLVMValueRef LLVMGetBasicBlockParent(LLVMValueRef V);
420 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB);
413421 unsigned LLVMCountBasicBlocks(LLVMValueRef Fn);
414422 void LLVMGetBasicBlocks(LLVMValueRef Fn, LLVMBasicBlockRef *BasicBlocks);
415423 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn);
627627 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
628628 GlobalVariable *GV = unwrap(GlobalVar);
629629 Module::global_iterator I = GV;
630 if (I == GV->getParent()->global_end())
630 if (I == GV->getParent()->global_begin())
631631 return 0;
632632 return wrap(--I);
633633 }
704704 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
705705 Function *Func = unwrap(Fn);
706706 Module::iterator I = Func;
707 if (I == Func->getParent()->end())
707 if (I == Func->getParent()->begin())
708708 return 0;
709709 return wrap(--I);
710710 }
766766 return wrap(unwrap(V)->getParent());
767767 }
768768
769 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
770 Function *Func = unwrap(Fn);
771 Function::arg_iterator I = Func->arg_begin();
772 if (I == Func->arg_end())
773 return 0;
774 return wrap(I);
775 }
776
777 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
778 Function *Func = unwrap(Fn);
779 Function::arg_iterator I = Func->arg_end();
780 if (I == Func->arg_begin())
781 return 0;
782 return wrap(--I);
783 }
784
785 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
786 Argument *A = unwrap(Arg);
787 Function::arg_iterator I = A;
788 if (++I == A->getParent()->arg_end())
789 return 0;
790 return wrap(I);
791 }
792
793 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
794 Argument *A = unwrap(Arg);
795 Function::arg_iterator I = A;
796 if (I == A->getParent()->arg_begin())
797 return 0;
798 return wrap(--I);
799 }
800
769801 /*--.. Operations on basic blocks ..........................................--*/
770802
771803 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
780812 return wrap(unwrap(Val));
781813 }
782814
783 LLVMValueRef LLVMGetBasicBlockParent(LLVMValueRef V) {
784 return wrap(unwrap(V)->getParent());
815 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
816 return wrap(unwrap(BB)->getParent());
785817 }
786818
787819 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
466466 let g = define_global "ConstGlobalVar" fourty_two32 m in
467467 insist (not (is_global_constant g));
468468 set_global_constant true g;
469 insist (is_global_constant g)
469 insist (is_global_constant g);
470
471 begin group "iteration";
472 let m = create_module "temp" in
473
474 insist (At_end m = global_begin m);
475 insist (At_start m = global_end m);
476
477 let g1 = declare_global i32_type "One" m in
478 let g2 = declare_global i32_type "Two" m in
479
480 insist (Before g1 = global_begin m);
481 insist (Before g2 = global_succ g1);
482 insist (At_end m = global_succ g2);
483
484 insist (After g2 = global_end m);
485 insist (After g1 = global_pred g2);
486 insist (At_start m = global_pred g1);
487
488 let lf s x = s ^ "->" ^ value_name x in
489 insist ("->One->Two" = fold_left_globals lf "" m);
490
491 let rf x s = value_name x ^ "<-" ^ s in
492 insist ("One<-Two<-" = fold_right_globals rf m "");
493
494 dispose_module m
495 end
470496
471497
472498 (*===-- Functions ---------------------------------------------------------===*)
539565 insist (None = collector fn);
540566 set_collector (Some "shadowstack") fn;
541567 ignore (build_unreachable (builder_at_end (entry_block fn)));
568 end;
569
570 begin group "iteration";
571 let m = create_module "temp" in
572
573 insist (At_end m = function_begin m);
574 insist (At_start m = function_end m);
575
576 let f1 = define_function "One" ty m in
577 let f2 = define_function "Two" ty m in
578
579 insist (Before f1 = function_begin m);
580 insist (Before f2 = function_succ f1);
581 insist (At_end m = function_succ f2);
582
583 insist (After f2 = function_end m);
584 insist (After f1 = function_pred f2);
585 insist (At_start m = function_pred f1);
586
587 let lf s x = s ^ "->" ^ value_name x in
588 insist ("->One->Two" = fold_left_functions lf "" m);
589
590 let rf x s = value_name x ^ "<-" ^ s in
591 insist ("One<-Two<-" = fold_right_functions rf m "");
592
593 dispose_module m
594 end
595
596
597 (*===-- Params ------------------------------------------------------------===*)
598
599 let test_params () =
600 begin group "iteration";
601 let m = create_module "temp" in
602
603 let vf = define_function "void" (function_type void_type [| |]) m in
604
605 insist (At_end vf = param_begin vf);
606 insist (At_start vf = param_end vf);
607
608 let ty = function_type void_type [| i32_type; i32_type |] in
609 let f = define_function "f" ty m in
610 let p1 = param f 0 in
611 let p2 = param f 1 in
612 set_value_name "One" p1;
613 set_value_name "Two" p2;
614
615 insist (Before p1 = param_begin f);
616 insist (Before p2 = param_succ p1);
617 insist (At_end f = param_succ p2);
618
619 insist (After p2 = param_end f);
620 insist (After p1 = param_pred p2);
621 insist (At_start f = param_pred p1);
622
623 let lf s x = s ^ "->" ^ value_name x in
624 insist ("->One->Two" = fold_left_params lf "" f);
625
626 let rf x s = value_name x ^ "<-" ^ s in
627 insist ("One<-Two<-" = fold_right_params rf f "");
628
629 dispose_module m
542630 end
543631
544632
586674 ignore (build_unreachable (builder_at_end bb));
587675 insist (bb = block_of_value (value_of_block bb));
588676 insist (value_is_block (value_of_block bb));
589 insist (not (value_is_block (const_null i32_type)))
677 insist (not (value_is_block (const_null i32_type)));
678
679 begin group "iteration";
680 let m = create_module "temp" in
681 let f = declare_function "Temp" (function_type i32_type [| |]) m in
682
683 insist (At_end f = block_begin f);
684 insist (At_start f = block_end f);
685
686 let b1 = append_block "One" f in
687 let b2 = append_block "Two" f in
688
689 insist (Before b1 = block_begin f);
690 insist (Before b2 = block_succ b1);
691 insist (At_end f = block_succ b2);
692
693 insist (After b2 = block_end f);
694 insist (After b1 = block_pred b2);
695 insist (At_start f = block_pred b1);
696
697 let lf s x = s ^ "->" ^ value_name (value_of_block x) in
698 insist ("->One->Two" = fold_left_blocks lf "" f);
699
700 let rf x s = value_name (value_of_block x) ^ "<-" ^ s in
701 insist ("One<-Two<-" = fold_right_blocks rf f "");
702
703 dispose_module m
704 end
590705
591706
592707 (*===-- Builder -----------------------------------------------------------===*)
9061021 suite "global values" test_global_values;
9071022 suite "global variables" test_global_variables;
9081023 suite "functions" test_functions;
1024 suite "params" test_params;
9091025 suite "basic blocks" test_basic_blocks;
9101026 suite "builder" test_builder;
9111027 suite "module provider" test_module_provider;