diff --git a/llvm/_core.cpp b/llvm/_core.cpp index 99642e8..2453479 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -607,6 +607,27 @@ _wrap_obj2none(LLVMDeleteBasicBlock, LLVMBasicBlockRef) /*===-- MetaData -----------------------------------------------------===*/ _wrap_objlist2obj(LLVMMetaDataGet, LLVMModuleRef, LLVMValueRef, LLVMValueRef) +_wrap_objstrobj2none(LLVMAddNamedMetadataOperand, LLVMModuleRef, LLVMValueRef) + +static PyObject * +_wLLVMGetNamedMetadataOperands(PyObject *self, PyObject *args) +{ + PyObject *obj_module; + const char *name; + + if (!PyArg_ParseTuple(args, "Os", &obj_module, &name)) + return NULL; + + LLVMModuleRef module = (LLVMModuleRef)PyCapsule_GetPointer(obj_module, NULL); + unsigned num_operands = LLVMGetNamedMetadataNumOperands(module, name); + LLVMValueRef *operands = (LLVMValueRef*)malloc(sizeof(LLVMValueRef)*num_operands); + LLVMGetNamedMetadataOperands(module, name, operands); + + PyObject *list = make_list_from_LLVMValueRef_array(operands, num_operands); + free(operands); + + return list; +} /*===-- Instructions -----------------------------------------------------===*/ @@ -1660,6 +1681,8 @@ static PyMethodDef core_methods[] = { /* MetaData */ _method( LLVMMetaDataGet ) + _method( LLVMGetNamedMetadataOperands ) + _method( LLVMAddNamedMetadataOperand ) /* Instructions */ _method( LLVMGetInstructionParent ) diff --git a/llvm/core.py b/llvm/core.py index 215252a..d5b829b 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -1457,6 +1457,15 @@ class MetaData(Value): ptr = _core.LLVMMetaDataGet(module.ptr, vs) return MetaData(ptr) + @staticmethod + def get_named_operands(module, name): + lst = _core.LLVMGetNamedMetadataOperands(module.ptr, name) + return [MetaData(ptr) for ptr in lst] + + @staticmethod + def add_named_operand(module, name, operand): + _core.LLVMAddNamedMetadataOperand(module.ptr, name, operand.ptr) + #===----------------------------------------------------------------------=== # Instruction #===----------------------------------------------------------------------=== diff --git a/llvm/test_llvmpy.py b/llvm/test_llvmpy.py index 6c85d93..5d0d62b 100644 --- a/llvm/test_llvmpy.py +++ b/llvm/test_llvmpy.py @@ -16,7 +16,7 @@ else: from llvm import __version__ from llvm.core import (Module, Type, GlobalVariable, Function, Builder, - Constant) + Constant, MetaData) import llvm.passes as lp import llvm.ee as le @@ -390,6 +390,19 @@ tests.append(TestUses) # --------------------------------------------------------------------------- +class TestMetaData(unittest.TestCase): + # test module metadata + def test_metadata(self): + m = Module.new('a') + metadata = MetaData.get(m, []) # empty metadata node + MetaData.add_named_operand(m, 'foo', metadata) + self.assertEqual(MetaData.get_named_operands(m, 'foo'), [metadata]) + self.assertEqual(MetaData.get_named_operands(m, 'bar'), []) + +tests.append(TestMetaData) + +# --------------------------------------------------------------------------- + def run(verbosity=1): print('llvmpy is installed in: ' + os.path.dirname(__file__)) print('llvmpy version: ' + __version__)