Add NamedMetaData
This commit is contained in:
parent
d1fefe5a69
commit
e0d1b94053
6 changed files with 138 additions and 0 deletions
|
|
@ -295,6 +295,9 @@ _wLLVMLinkModules(PyObject *self, PyObject *args)
|
||||||
LLVMPY_CATCH_ALL
|
LLVMPY_CATCH_ALL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_wrap_objstr2obj(LLVMModuleGetOrInsertNamedMetaData, LLVMModuleRef, LLVMNamedMDRef)
|
||||||
|
_wrap_objstr2obj(LLVMModuleGetNamedMetaData, LLVMModuleRef, LLVMNamedMDRef)
|
||||||
|
|
||||||
/*===----------------------------------------------------------------------===*/
|
/*===----------------------------------------------------------------------===*/
|
||||||
/* Types */
|
/* Types */
|
||||||
/*===----------------------------------------------------------------------===*/
|
/*===----------------------------------------------------------------------===*/
|
||||||
|
|
@ -703,6 +706,14 @@ _wLLVMGetNamedMetadataOperands(PyObject *self, PyObject *args)
|
||||||
LLVMPY_CATCH_ALL
|
LLVMPY_CATCH_ALL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*===-- NamedMetaData -----------------------------------------------------===*/
|
||||||
|
|
||||||
|
_wrap_obj2str( LLVMNamedMetaDataGetName, LLVMNamedMDRef )
|
||||||
|
_wrap_objobj2none( LLVMNamedMetaDataAddOperand, LLVMNamedMDRef, LLVMValueRef )
|
||||||
|
_wrap_obj2none( LLVMEraseNamedMetaData, LLVMNamedMDRef )
|
||||||
|
_wrap_dumper(LLVMDumpNamedMDToString, LLVMNamedMDRef)
|
||||||
|
|
||||||
/*===-- Instructions -----------------------------------------------------===*/
|
/*===-- Instructions -----------------------------------------------------===*/
|
||||||
|
|
||||||
_wrap_obj2obj(LLVMGetInstructionParent, LLVMValueRef, LLVMBasicBlockRef)
|
_wrap_obj2obj(LLVMGetInstructionParent, LLVMValueRef, LLVMBasicBlockRef)
|
||||||
|
|
@ -1564,6 +1575,8 @@ static PyMethodDef core_methods[] = {
|
||||||
_method( LLVMModuleGetPointerSize )
|
_method( LLVMModuleGetPointerSize )
|
||||||
_method( LLVMModuleGetOrInsertFunction )
|
_method( LLVMModuleGetOrInsertFunction )
|
||||||
_method( LLVMLinkModules )
|
_method( LLVMLinkModules )
|
||||||
|
_method( LLVMModuleGetOrInsertNamedMetaData )
|
||||||
|
_method( LLVMModuleGetNamedMetaData )
|
||||||
|
|
||||||
/* Types */
|
/* Types */
|
||||||
|
|
||||||
|
|
@ -1788,6 +1801,12 @@ static PyMethodDef core_methods[] = {
|
||||||
_method( LLVMMetaDataGetNumOperands )
|
_method( LLVMMetaDataGetNumOperands )
|
||||||
_method( LLVMMetaDataStringGet )
|
_method( LLVMMetaDataStringGet )
|
||||||
|
|
||||||
|
/* NamedMetaData */
|
||||||
|
_method( LLVMNamedMetaDataGetName )
|
||||||
|
_method( LLVMNamedMetaDataAddOperand )
|
||||||
|
_method( LLVMEraseNamedMetaData )
|
||||||
|
_method( LLVMDumpNamedMDToString )
|
||||||
|
|
||||||
/* Instructions */
|
/* Instructions */
|
||||||
_method( LLVMGetInstructionParent )
|
_method( LLVMGetInstructionParent )
|
||||||
_method( LLVMGetFirstInstruction )
|
_method( LLVMGetFirstInstruction )
|
||||||
|
|
|
||||||
33
llvm/core.py
33
llvm/core.py
|
|
@ -574,6 +574,14 @@ class Module(llvm.Ownable, llvm.Cacheable):
|
||||||
else:
|
else:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def get_or_insert_named_metadata(self, name):
|
||||||
|
ptr = _core.LLVMModuleGetOrInsertNamedMetaData(self.ptr, name)
|
||||||
|
return NamedMetaData(ptr)
|
||||||
|
|
||||||
|
def get_named_metadata(self, name):
|
||||||
|
ptr = _core.LLVMModuleGetNamedMetaData(self.ptr, name)
|
||||||
|
return NamedMetaData(ptr)
|
||||||
|
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
# Types
|
# Types
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
|
|
@ -1542,6 +1550,31 @@ class MetaDataString(Value):
|
||||||
'''Same as MDString::getString'''
|
'''Same as MDString::getString'''
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
class NamedMetaData(llvm.Cacheable):
|
||||||
|
@staticmethod
|
||||||
|
def get_or_insert(mod, name):
|
||||||
|
return mod.get_or_insert_named_metadata(name)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get(mod, name):
|
||||||
|
return mod.get_named_metadata(name)
|
||||||
|
|
||||||
|
def __init__(self, ptr):
|
||||||
|
self.ptr = ptr
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
_core.LLVMEraseNamedMetaData(self.ptr)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return _core.LLVMNamedMetaDataGetName(self.ptr)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return _core.LLVMDumpNamedMDToString(self.ptr)
|
||||||
|
|
||||||
|
def add(self, operand):
|
||||||
|
_core.LLVMNamedMetaDataAddOperand(self.ptr, operand.ptr)
|
||||||
|
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
# Instruction
|
# Instruction
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,7 @@
|
||||||
#include "llvm/Linker.h"
|
#include "llvm/Linker.h"
|
||||||
#include "llvm/Support/SourceMgr.h"
|
#include "llvm/Support/SourceMgr.h"
|
||||||
|
|
||||||
|
|
||||||
// LLVM-C includes
|
// LLVM-C includes
|
||||||
#include "llvm-c/Core.h"
|
#include "llvm-c/Core.h"
|
||||||
#include "llvm-c/ExecutionEngine.h"
|
#include "llvm-c/ExecutionEngine.h"
|
||||||
|
|
@ -86,6 +87,7 @@
|
||||||
namespace llvm{
|
namespace llvm{
|
||||||
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(EngineBuilder, LLVMEngineBuilderRef)
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(EngineBuilder, LLVMEngineBuilderRef)
|
||||||
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
|
||||||
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(NamedMDNode, LLVMNamedMDRef)
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T **unwrap(LLVMTypeRef *Tys, unsigned Length) {
|
inline T **unwrap(LLVMTypeRef *Tys, unsigned Length) {
|
||||||
|
|
@ -133,6 +135,44 @@ const CodeGenOpt::Level OptLevelMap[] = {
|
||||||
};
|
};
|
||||||
} // end anony namespace
|
} // end anony namespace
|
||||||
|
|
||||||
|
const char * LLVMDumpNamedMDToString(LLVMNamedMDRef nmd)
|
||||||
|
{
|
||||||
|
using namespace llvm;
|
||||||
|
std::string s;
|
||||||
|
llvm::raw_string_ostream buf(s);
|
||||||
|
unwrap(nmd)->print(buf, NULL);
|
||||||
|
return strdup(buf.str().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
const char * LLVMNamedMetaDataGetName(LLVMNamedMDRef nmd)
|
||||||
|
{
|
||||||
|
using namespace llvm;
|
||||||
|
return unwrap(nmd)->getName().data();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LLVMNamedMetaDataAddOperand(LLVMNamedMDRef nmd, LLVMValueRef md)
|
||||||
|
{
|
||||||
|
using namespace llvm;
|
||||||
|
unwrap(nmd)->addOperand(unwrap<MDNode>(md));
|
||||||
|
}
|
||||||
|
|
||||||
|
void LLVMEraseNamedMetaData(LLVMNamedMDRef nmd)
|
||||||
|
{
|
||||||
|
using namespace llvm;
|
||||||
|
unwrap(nmd)->eraseFromParent();
|
||||||
|
}
|
||||||
|
|
||||||
|
LLVMNamedMDRef LLVMModuleGetOrInsertNamedMetaData(LLVMModuleRef mod, const char *name)
|
||||||
|
{
|
||||||
|
using namespace llvm;
|
||||||
|
return wrap(unwrap(mod)->getOrInsertNamedMetadata(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
LLVMNamedMDRef LLVMModuleGetNamedMetaData(LLVMModuleRef mod, const char *name)
|
||||||
|
{
|
||||||
|
using namespace llvm;
|
||||||
|
return wrap(unwrap(mod)->getNamedMetadata(name));
|
||||||
|
}
|
||||||
|
|
||||||
void LLVMInstSetMetaData(LLVMValueRef instref, const char* mdkind,
|
void LLVMInstSetMetaData(LLVMValueRef instref, const char* mdkind,
|
||||||
LLVMValueRef metaref)
|
LLVMValueRef metaref)
|
||||||
|
|
|
||||||
31
llvm/extra.h
31
llvm/extra.h
|
|
@ -47,12 +47,43 @@
|
||||||
|
|
||||||
#include "llvm-c/Transforms/PassManagerBuilder.h"
|
#include "llvm-c/Transforms/PassManagerBuilder.h"
|
||||||
|
|
||||||
|
|
||||||
#include "llvm_c_extra.h"
|
#include "llvm_c_extra.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wraps NamedMDNode::print()
|
||||||
|
*/
|
||||||
|
const char * LLVMDumpNamedMDToString(LLVMNamedMDRef nmd);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wraps NamedMDNode::getName()
|
||||||
|
*/
|
||||||
|
const char * LLVMNamedMetaDataGetName(LLVMNamedMDRef nmd);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wraps NamedMDNode::addOperand()
|
||||||
|
*/
|
||||||
|
void LLVMNamedMetaDataAddOperand(LLVMNamedMDRef nmd, LLVMValueRef md);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wraps NamedMDNode::eraseFromParent()
|
||||||
|
*/
|
||||||
|
void LLVMEraseNamedMetaData(LLVMNamedMDRef nmd);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wraps Module::getOrInsertNamedMetadata
|
||||||
|
*/
|
||||||
|
LLVMNamedMDRef LLVMModuleGetOrInsertNamedMetaData(LLVMModuleRef mod, const char *name);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wraps Module::getNamedMetadata
|
||||||
|
*/
|
||||||
|
LLVMNamedMDRef LLVMModuleGetNamedMetaData(LLVMModuleRef mod, const char *name);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Wraps Instruction::setMetadata()
|
* Wraps Instruction::setMetadata()
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ inline ref wrap(const ty *P) { \
|
||||||
|
|
||||||
typedef struct LLVMOpaqueEngineBuilder *LLVMEngineBuilderRef;
|
typedef struct LLVMOpaqueEngineBuilder *LLVMEngineBuilderRef;
|
||||||
typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
|
typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
|
||||||
|
typedef struct LLVMOpaqueNamedMD *LLVMNamedMDRef;
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
||||||
|
|
@ -952,6 +952,20 @@ tests.append(TestVolatile)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class TestNamedMetaData(unittest.TestCase):
|
||||||
|
def test_named_md(self):
|
||||||
|
m = Module.new('test_named_md')
|
||||||
|
nmd = m.get_or_insert_named_metadata('something')
|
||||||
|
md = MetaData.get(m, [Constant.int(Type.int(), 0xbeef)])
|
||||||
|
nmd.add(md)
|
||||||
|
self.assertTrue(str(nmd).startswith('!something'))
|
||||||
|
ir = str(m)
|
||||||
|
self.assertTrue('!something' in ir)
|
||||||
|
|
||||||
|
tests.append(TestNamedMetaData)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def run(verbosity=1):
|
def run(verbosity=1):
|
||||||
print('llvmpy is installed in: ' + os.path.dirname(__file__))
|
print('llvmpy is installed in: ' + os.path.dirname(__file__))
|
||||||
print('llvmpy version: ' + llvm.__version__)
|
print('llvmpy version: ' + llvm.__version__)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue