From d781e83bd11f801276afc4adb8fef5d9e862dbd3 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Fri, 19 Oct 2012 16:18:24 -0500 Subject: [PATCH] can now clone modules --- llvm/_core.cpp | 2 ++ llvm/core.py | 3 +++ llvm/extra.cpp | 6 ++++++ llvm/extra.h | 7 +++++++ test/clonemodule.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 test/clonemodule.py diff --git a/llvm/_core.cpp b/llvm/_core.cpp index 19dadec..73d6099 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -297,6 +297,7 @@ _wLLVMLinkModules(PyObject *self, PyObject *args) _wrap_objstr2obj(LLVMModuleGetOrInsertNamedMetaData, LLVMModuleRef, LLVMNamedMDRef) _wrap_objstr2obj(LLVMModuleGetNamedMetaData, LLVMModuleRef, LLVMNamedMDRef) +_wrap_obj2obj(LLVMCloneModule, LLVMModuleRef, LLVMModuleRef) /*===----------------------------------------------------------------------===*/ /* Types */ @@ -1577,6 +1578,7 @@ static PyMethodDef core_methods[] = { _method( LLVMLinkModules ) _method( LLVMModuleGetOrInsertNamedMetaData ) _method( LLVMModuleGetNamedMetaData ) + _method( LLVMCloneModule ) /* Types */ diff --git a/llvm/core.py b/llvm/core.py index 8b9a7ae..b9e156c 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -582,6 +582,9 @@ class Module(llvm.Ownable, llvm.Cacheable): ptr = _core.LLVMModuleGetNamedMetaData(self.ptr, name) return NamedMetaData(ptr) + def clone(self): + return Module(_core.LLVMCloneModule(self.ptr)) + #===----------------------------------------------------------------------=== # Types #===----------------------------------------------------------------------=== diff --git a/llvm/extra.cpp b/llvm/extra.cpp index 198fbb9..3c636b6 100644 --- a/llvm/extra.cpp +++ b/llvm/extra.cpp @@ -135,6 +135,12 @@ const CodeGenOpt::Level OptLevelMap[] = { }; } // end anony namespace +LLVMModuleRef LLVMCloneModule(LLVMModuleRef mod) +{ + using namespace llvm; + return wrap(CloneModule(unwrap(mod))); +} + const char * LLVMDumpNamedMDToString(LLVMNamedMDRef nmd) { using namespace llvm; diff --git a/llvm/extra.h b/llvm/extra.h index 32ca193..007f6c0 100644 --- a/llvm/extra.h +++ b/llvm/extra.h @@ -54,6 +54,13 @@ extern "C" { #endif + +/* + * Wraps llvm::CloneModule + */ +LLVMModuleRef LLVMCloneModule(LLVMModuleRef mod); + + /* * Wraps NamedMDNode::print() */ diff --git a/test/clonemodule.py b/test/clonemodule.py new file mode 100644 index 0000000..d762faa --- /dev/null +++ b/test/clonemodule.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +# Import the llvm-py modules. +from llvm import * +from llvm.core import * + +import logging +import unittest + + +class TestCloneModule(unittest.TestCase): + def test_example(self): + my_module = Module.new('my_module') + + ty_int = Type.int() # by default 32 bits + + ty_func = Type.function(ty_int, [ty_int, ty_int]) + + f_sum = my_module.add_function(ty_func, "sum") + + self.assertEqual(str(f_sum).strip(), 'declare i32 @sum(i32, i32)') + + f_sum.args[0].name = "a" + f_sum.args[1].name = "b" + + bb = f_sum.append_basic_block("entry") + + builder = Builder.new(bb) + + tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") + + self.assertEqual(str(tmp).strip(), '%tmp = add i32 %a, %b') + + builder.ret(tmp) + + cloned = my_module.clone() + + self.assertTrue(id(cloned) != id(my_module)) + self.assertTrue(str(cloned) == str(my_module)) + self.assertTrue(cloned == my_module) + + + +if __name__ == '__main__': + unittest.main() +