ocaml/C bindings: getmdstring, add num_op, get_op should work on metadata too

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141286 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Torok Edwin 2011-10-06 12:13:11 +00:00
parent c44943ed4f
commit 4f661ab0fb
5 changed files with 76 additions and 2 deletions

View File

@ -281,6 +281,8 @@ external clear_metadata : llvalue -> int -> unit = "llvm_clear_metadata"
(*--... Operations on metadata .......,.....................................--*)
external mdstring : llcontext -> string -> llvalue = "llvm_mdstring"
external mdnode : llcontext -> llvalue array -> llvalue = "llvm_mdnode"
external get_mdstring : llvalue -> string option = "llvm_get_mdstring"
external get_named_metadata : llmodule -> string -> llvalue array = "llvm_get_namedmd"
(*--... Operations on scalar constants .....................................--*)
external const_int : lltype -> int -> llvalue = "llvm_const_int"

View File

@ -571,6 +571,14 @@ val mdstring : llcontext -> string -> llvalue
See the method [llvm::MDNode::get]. *)
val mdnode : llcontext -> llvalue array -> llvalue
(** [get_mdstring v] returns the MDString.
* See the method [llvm::MDString::getString] *)
val get_mdstring : llvalue -> string option
(** [get_named_metadata m name] return all the MDNodes belonging to the named
* metadata (if any).
* See the method [llvm::NamedMDNode::getOperand]. *)
val get_named_metadata : llmodule -> string -> llvalue array
(** {7 Operations on scalar constants} *)

View File

@ -24,6 +24,7 @@
#include "llvm/Config/config.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/* Can't use the recommended caml_named_value mechanism for backwards
@ -468,6 +469,32 @@ CAMLprim LLVMValueRef llvm_mdnode(LLVMContextRef C, value ElementVals) {
Wosize_val(ElementVals));
}
/* llvalue -> string option */
CAMLprim value llvm_get_mdstring(LLVMValueRef V) {
CAMLparam0();
const char *S;
unsigned Len;
if ((S = LLVMGetMDString(V, &Len))) {
CAMLlocal2(Option, Str);
Str = caml_alloc_string(Len);
memcpy(String_val(Str), S, Len);
Option = alloc(1,0);
Store_field(Option, 0, Str);
CAMLreturn(Option);
}
CAMLreturn(Val_int(0));
}
CAMLprim value llvm_get_namedmd(LLVMModuleRef M, value name)
{
CAMLparam1(name);
CAMLlocal1(Nodes);
Nodes = alloc(LLVMGetNamedMetadataNumOperands(M, String_val(name)), 0);
LLVMGetNamedMetadataOperands(M, String_val(name), (LLVMValueRef *) Nodes);
CAMLreturn(Nodes);
}
/*--... Operations on scalar constants .....................................--*/
/* lltype -> int -> llvalue */

View File

@ -550,6 +550,11 @@ LLVMValueRef LLVMMDString(const char *Str, unsigned SLen);
LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
unsigned Count);
LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count);
const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len);
int LLVMGetMDNodeNumOperands(LLVMValueRef V);
LLVMValueRef *LLVMGetMDNodeOperand(LLVMValueRef V, unsigned i);
unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name);
void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest);
/* Operations on scalar constants */
LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,

View File

@ -456,7 +456,10 @@ LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
/*--.. Operations on Users .................................................--*/
LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
return wrap(unwrap<User>(Val)->getOperand(Index));
Value *V = unwrap(Val);
if (MDNode *MD = dyn_cast<MDNode>(V))
return wrap(MD->getOperand(Index));
return wrap(cast<User>(V)->getOperand(Index));
}
void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
@ -464,7 +467,10 @@ void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
}
int LLVMGetNumOperands(LLVMValueRef Val) {
return unwrap<User>(Val)->getNumOperands();
Value *V = unwrap(Val);
if (MDNode *MD = dyn_cast<MDNode>(V))
return MD->getNumOperands();
return cast<User>(V)->getNumOperands();
}
/*--.. Operations on constants of any type .................................--*/
@ -521,6 +527,32 @@ LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
}
const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
*Len = S->getString().size();
return S->getString().data();
}
*Len = 0;
return 0;
}
unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
{
if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
return N->getNumOperands();
}
return 0;
}
void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
{
NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
if (!N)
return;
for (unsigned i=0;i<N->getNumOperands();i++)
Dest[i] = wrap(N->getOperand(i));
}
/*--.. Operations on scalar constants ......................................--*/
LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,