llvm.org GIT mirror llvm / 6563c87
ocaml bindings: add getopcode for constant and instruction, and int64_of_const. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141990 91177308-0d34-0410-b5e6-96231b3b80d8 Torok Edwin 7 years ago
5 changed file(s) with 185 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
129129 | True
130130 end
131131
132 module Opcode = struct
133 type t =
134 | Invalid (* not an instruction *)
135 (* Terminator Instructions *)
136 | Ret
137 | Br
138 | Switch
139 | IndirectBr
140 | Invoke
141 | Invalid2
142 | Unreachable
143 (* Standard Binary Operators *)
144 | Add
145 | FAdd
146 | Sub
147 | FSub
148 | Mul
149 | FMul
150 | UDiv
151 | SDiv
152 | FDiv
153 | URem
154 | SRem
155 | FRem
156 (* Logical Operators *)
157 | Shl
158 | LShr
159 | AShr
160 | And
161 | Or
162 | Xor
163 (* Memory Operators *)
164 | Alloca
165 | Load
166 | Store
167 | GetElementPtr
168 (* Cast Operators *)
169 | Trunc
170 | ZExt
171 | SExt
172 | FPToUI
173 | FPToSI
174 | UIToFP
175 | SIToFP
176 | FPTrunc
177 | FPExt
178 | PtrToInt
179 | IntToPtr
180 | BitCast
181 (* Other Operators *)
182 | ICmp
183 | FCmp
184 | PHI
185 | Call
186 | Select
187 | UserOp1
188 | UserOp2
189 | VAArg
190 | ExtractElement
191 | InsertElement
192 | ShuffleVector
193 | ExtractValue
194 | InsertValue
195 | Fence
196 | AtomicCmpXchg
197 | AtomicRMW
198 | Resume
199 | LandingPad
200 | Unwind
201 end
202
132203 exception IoError of string
133204
134205 external register_exns : exn -> unit = "llvm_register_core_exns"
271342 external undef : lltype -> llvalue = "LLVMGetUndef"
272343 external is_null : llvalue -> bool = "llvm_is_null"
273344 external is_undef : llvalue -> bool = "llvm_is_undef"
345 external constexpr_opcode : llvalue -> Opcode.t = "llvm_constexpr_get_opcode"
274346
275347 (*--... Operations on instructions .........................................--*)
276348 external has_metadata : llvalue -> bool = "llvm_has_metadata"
288360 external const_int : lltype -> int -> llvalue = "llvm_const_int"
289361 external const_of_int64 : lltype -> Int64.t -> bool -> llvalue
290362 = "llvm_const_of_int64"
363 external int64_of_const : llvalue -> Int64.t option
364 = "llvm_int64_of_const"
291365 external const_int_of_string : lltype -> string -> int -> llvalue
292366 = "llvm_const_int_of_string"
293367 external const_float : lltype -> float -> llvalue = "llvm_const_float"
705779 external instr_pred : llvalue -> (llbasicblock, llvalue) llrev_pos
706780 = "llvm_instr_pred"
707781
782 external instr_opcode : llvalue -> Opcode.t = "llvm_instr_get_opcode"
708783 external icmp_predicate : llvalue -> Icmp.t option = "llvm_instr_icmp_predicate"
709784
710785 let rec iter_instrs_range f i e =
178178 | True
179179 end
180180
181 (** The opcodes for LLVM instructions and constant expressions. *)
182 module Opcode : sig
183 type t =
184 | Invalid (* not an instruction *)
185 (* Terminator Instructions *)
186 | Ret
187 | Br
188 | Switch
189 | IndirectBr
190 | Invoke
191 | Invalid2
192 | Unreachable
193 (* Standard Binary Operators *)
194 | Add
195 | FAdd
196 | Sub
197 | FSub
198 | Mul
199 | FMul
200 | UDiv
201 | SDiv
202 | FDiv
203 | URem
204 | SRem
205 | FRem
206 (* Logical Operators *)
207 | Shl
208 | LShr
209 | AShr
210 | And
211 | Or
212 | Xor
213 (* Memory Operators *)
214 | Alloca
215 | Load
216 | Store
217 | GetElementPtr
218 (* Cast Operators *)
219 | Trunc
220 | ZExt
221 | SExt
222 | FPToUI
223 | FPToSI
224 | UIToFP
225 | SIToFP
226 | FPTrunc
227 | FPExt
228 | PtrToInt
229 | IntToPtr
230 | BitCast
231 (* Other Operators *)
232 | ICmp
233 | FCmp
234 | PHI
235 | Call
236 | Select
237 | UserOp1
238 | UserOp2
239 | VAArg
240 | ExtractElement
241 | InsertElement
242 | ShuffleVector
243 | ExtractValue
244 | InsertValue
245 | Fence
246 | AtomicCmpXchg
247 | AtomicRMW
248 | Resume
249 | LandingPad
250 | Unwind
251 end
252
181253
182254 (** {6 Iteration} *)
183255
542614 otherwise. Similar to [llvm::isa]. *)
543615 val is_undef : llvalue -> bool
544616
545
617 val constexpr_opcode : llvalue -> Opcode.t
546618 (** {7 Operations on instructions} *)
547619
548620 (** [has_metadata i] returns whether or not the instruction [i] has any
594666 [i]. See the method [llvm::ConstantInt::get]. *)
595667 val const_of_int64 : lltype -> Int64.t -> bool -> llvalue
596668
669 (** [int64_of_const c] returns the int64 value of the [c] constant integer.
670 * None is returned if this is not an integer constant, or bitwidth exceeds 64.
671 * See the method [llvm::ConstantInt::getSExtValue].*)
672 val int64_of_const : llvalue -> Int64.t option
597673
598674 (** [const_int_of_string ty s r] returns the integer constant of type [ty] and
599675 * value [s], with the radix [r]. See the method [llvm::ConstantInt::get]. *)
14381514 [f1,...,fN] are the instructions of basic block [bb]. Tail recursive. *)
14391515 val fold_right_instrs: (llvalue -> 'a -> 'a) -> llbasicblock -> 'a -> 'a
14401516
1517 val instr_opcode : llvalue -> Opcode.t
14411518
14421519 val icmp_predicate : llvalue -> Icmp.t option
14431520
426426 return Val_bool(LLVMIsUndef(Val));
427427 }
428428
429 /* llvalue -> Opcode.t */
430 CAMLprim value llvm_constexpr_get_opcode(LLVMValueRef Val) {
431 return LLVMIsAConstantExpr(Val) ?
432 Val_int(LLVMGetConstOpcode(Val)) : Val_int(0);
433 }
434
429435 /*--... Operations on instructions .........................................--*/
430436
431437 /* llvalue -> bool */
509515 CAMLprim LLVMValueRef llvm_const_of_int64(LLVMTypeRef IntTy, value N,
510516 value SExt) {
511517 return LLVMConstInt(IntTy, Int64_val(N), Bool_val(SExt));
518 }
519
520 /* llvalue -> Int64.t */
521 CAMLprim value llvm_int64_of_const(LLVMValueRef Const)
522 {
523 CAMLparam0();
524 if (LLVMIsAConstantInt(Const) &&
525 LLVMGetIntTypeWidth(LLVMTypeOf(Const)) <= 64) {
526 value Option = alloc(1, 0);
527 Field(Option, 0) = caml_copy_int64(LLVMConstIntGetSExtValue(Const));
528 CAMLreturn(Option);
529 }
530 CAMLreturn(Val_int(0));
512531 }
513532
514533 /* lltype -> string -> int -> llvalue */
10121031 DEFINE_ITERATORS(instr, Instruction, LLVMBasicBlockRef, LLVMValueRef,
10131032 LLVMGetInstructionParent)
10141033
1034 /* llvalue -> Opcode.t */
1035 CAMLprim value llvm_instr_get_opcode(LLVMValueRef Inst) {
1036 LLVMOpcode o = LLVMGetInstructionOpcode(Inst);
1037 assert (o <= LLVMUnwind );
1038 return Val_int(o);
1039 }
10151040
10161041 /* llvalue -> ICmp.t */
10171042 CAMLprim value llvm_instr_icmp_predicate(LLVMValueRef Val) {
788788 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst);
789789 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst);
790790 void LLVMInstructionEraseFromParent(LLVMValueRef Inst);
791 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst);
791792 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst);
792793
793794 /* Operations on call sites */
15901590 return (LLVMIntPredicate)0;
15911591 }
15921592
1593 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1594 if (Instruction *C = dyn_cast(unwrap(Inst)))
1595 return map_to_llvmopcode(C->getOpcode());
1596 return (LLVMOpcode)0;
1597 }
1598
15931599 /*--.. Call and invoke instructions ........................................--*/
15941600
15951601 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {