llvm.org GIT mirror llvm / 3dd1674
ocaml bindings: introduce classify_value git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141991 91177308-0d34-0410-b5e6-96231b3b80d8 Torok Edwin 7 years ago
4 changed file(s) with 126 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
200200 | Unwind
201201 end
202202
203 module ValueKind = struct
204 type t =
205 | NullValue
206 | Argument
207 | BasicBlock
208 | InlineAsm
209 | MDNode
210 | MDString
211 | BlockAddress
212 | ConstantAggregateZero
213 | ConstantArray
214 | ConstantExpr
215 | ConstantFP
216 | ConstantInt
217 | ConstantPointerNull
218 | ConstantStruct
219 | ConstantVector
220 | Function
221 | GlobalAlias
222 | GlobalVariable
223 | UndefValue
224 | Instruction of Opcode.t
225 end
226
203227 exception IoError of string
204228
205229 external register_exns : exn -> unit = "llvm_register_core_exns"
289313 external void_type : llcontext -> lltype = "llvm_void_type"
290314 external label_type : llcontext -> lltype = "llvm_label_type"
291315
316 external classify_value : llvalue -> ValueKind.t = "llvm_classify_value"
292317 (*===-- Values ------------------------------------------------------------===*)
293318 external type_of : llvalue -> lltype = "llvm_type_of"
294319 external value_name : llvalue -> string = "llvm_value_name"
250250 | Unwind
251251 end
252252
253 (** The kind of an [llvalue], the result of [classify_value v].
254 * See the various [LLVMIsA*] functions. *)
255 module ValueKind : sig
256 type t =
257 | NullValue
258 | Argument
259 | BasicBlock
260 | InlineAsm
261 | MDNode
262 | MDString
263 | BlockAddress
264 | ConstantAggregateZero
265 | ConstantArray
266 | ConstantExpr
267 | ConstantFP
268 | ConstantInt
269 | ConstantPointerNull
270 | ConstantStruct
271 | ConstantVector
272 | Function
273 | GlobalAlias
274 | GlobalVariable
275 | UndefValue
276 | Instruction of Opcode.t
277 end
253278
254279 (** {6 Iteration} *)
255280
516541 (** [type_of v] returns the type of the value [v].
517542 See the method [llvm::Value::getType]. *)
518543 val type_of : llvalue -> lltype
544
545 val classify_value : llvalue -> ValueKind.t
519546
520547 (** [value_name v] returns the name of the value [v]. For global values, this is
521548 the symbol name. For instructions and basic blocks, it is the SSA register
374374 return LLVMTypeOf(Val);
375375 }
376376
377 /* keep in sync with ValueKind.t */
378 enum ValueKind {
379 NullValue=0,
380 Argument,
381 BasicBlock,
382 InlineAsm,
383 MDNode,
384 MDString,
385 BlockAddress,
386 ConstantAggregateZero,
387 ConstantArray,
388 ConstantExpr,
389 ConstantFP,
390 ConstantInt,
391 ConstantPointerNull,
392 ConstantStruct,
393 ConstantVector,
394 Function,
395 GlobalAlias,
396 GlobalVariable,
397 UndefValue,
398 Instruction
399 };
400
401 /* llvalue -> ValueKind.t */
402 #define DEFINE_CASE(Val, Kind) \
403 do {if (LLVMIsA##Kind(Val)) CAMLreturn(Val_int(Kind));} while(0)
404
405 CAMLprim value llvm_classify_value(LLVMValueRef Val) {
406 CAMLparam0();
407 if (!Val)
408 CAMLreturn(Val_int(NullValue));
409 if (LLVMIsAConstant(Val)) {
410 DEFINE_CASE(Val, BlockAddress);
411 DEFINE_CASE(Val, ConstantAggregateZero);
412 DEFINE_CASE(Val, ConstantArray);
413 DEFINE_CASE(Val, ConstantExpr);
414 DEFINE_CASE(Val, ConstantFP);
415 DEFINE_CASE(Val, ConstantInt);
416 DEFINE_CASE(Val, ConstantPointerNull);
417 DEFINE_CASE(Val, ConstantStruct);
418 DEFINE_CASE(Val, ConstantVector);
419 }
420 if (LLVMIsAInstruction(Val)) {
421 CAMLlocal1(result);
422 result = caml_alloc_small(1, 0);
423 Store_field(result, 0, Val_int(LLVMGetInstructionOpcode(Val)));
424 CAMLreturn(result);
425 }
426 if (LLVMIsAGlobalValue(Val)) {
427 DEFINE_CASE(Val, Function);
428 DEFINE_CASE(Val, GlobalAlias);
429 DEFINE_CASE(Val, GlobalVariable);
430 }
431 DEFINE_CASE(Val, Argument);
432 DEFINE_CASE(Val, BasicBlock);
433 DEFINE_CASE(Val, InlineAsm);
434 DEFINE_CASE(Val, MDNode);
435 DEFINE_CASE(Val, MDString);
436 DEFINE_CASE(Val, UndefValue);
437 failwith("Unknown Value class");
438 }
439
377440 /* llvalue -> string */
378441 CAMLprim value llvm_value_name(LLVMValueRef Val) {
379442 return copy_string(LLVMGetValueName(Val));
10331096
10341097 /* llvalue -> Opcode.t */
10351098 CAMLprim value llvm_instr_get_opcode(LLVMValueRef Inst) {
1036 LLVMOpcode o = LLVMGetInstructionOpcode(Inst);
1099 LLVMOpcode o;
1100 if (!LLVMIsAInstruction(Inst))
1101 failwith("Not an instruction");
1102 o = LLVMGetInstructionOpcode(Inst);
10371103 assert (o <= LLVMUnwind );
10381104 return Val_int(o);
10391105 }
448448 macro(Argument) \
449449 macro(BasicBlock) \
450450 macro(InlineAsm) \
451 macro(MDNode) \
452 macro(MDString) \
451453 macro(User) \
452454 macro(Constant) \
455 macro(BlockAddress) \
453456 macro(ConstantAggregateZero) \
454457 macro(ConstantArray) \
455458 macro(ConstantExpr) \
469472 macro(IntrinsicInst) \
470473 macro(DbgInfoIntrinsic) \
471474 macro(DbgDeclareInst) \
475 macro(EHExceptionInst) \
472476 macro(EHSelectorInst) \
473477 macro(MemIntrinsic) \
474478 macro(MemCpyInst) \
475479 macro(MemMoveInst) \
476480 macro(MemSetInst) \
477481 macro(CmpInst) \
478 macro(FCmpInst) \
479 macro(ICmpInst) \
482 macro(FCmpInst) \
483 macro(ICmpInst) \
480484 macro(ExtractElementInst) \
481485 macro(GetElementPtrInst) \
482486 macro(InsertElementInst) \
488492 macro(StoreInst) \
489493 macro(TerminatorInst) \
490494 macro(BranchInst) \
495 macro(IndirectBrInst) \
491496 macro(InvokeInst) \
492497 macro(ReturnInst) \
493498 macro(SwitchInst) \