diff options
author | Torok Edwin <edwintorok@gmail.com> | 2011-10-06 12:13:11 +0000 |
---|---|---|
committer | Torok Edwin <edwintorok@gmail.com> | 2011-10-06 12:13:11 +0000 |
commit | 4f661ab0fb87f97c4a12912249c3c366df882102 (patch) | |
tree | 673ddc9c55602ec3fcd07f6c1687718620e8b002 | |
parent | c44943ed4f4f2d44b6668e6b51eb355f8310660c (diff) | |
download | external_llvm-4f661ab0fb87f97c4a12912249c3c366df882102.zip external_llvm-4f661ab0fb87f97c4a12912249c3c366df882102.tar.gz external_llvm-4f661ab0fb87f97c4a12912249c3c366df882102.tar.bz2 |
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
-rw-r--r-- | bindings/ocaml/llvm/llvm.ml | 2 | ||||
-rw-r--r-- | bindings/ocaml/llvm/llvm.mli | 8 | ||||
-rw-r--r-- | bindings/ocaml/llvm/llvm_ocaml.c | 27 | ||||
-rw-r--r-- | include/llvm-c/Core.h | 5 | ||||
-rw-r--r-- | lib/VMCore/Core.cpp | 36 |
5 files changed, 76 insertions, 2 deletions
diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index e46ea04..f68ab8d 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -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" diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index c070170..b4b9622 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -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} *) diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index 28f0977..14bdbdd 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -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 */ diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 4569109..3d374b0 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -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, diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index 8560de4..a21ec7a 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -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, |