diff --git a/.gitignore b/.gitignore index b25c15b..bdc5af0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *~ +build diff --git a/llvm/_core.c b/llvm/_core.c index f9e76fe..50ceee6 100644 --- a/llvm/_core.c +++ b/llvm/_core.c @@ -85,6 +85,8 @@ _wLLVMModuleCreateWithName(PyObject *self, PyObject *args) _wrap_obj2str(LLVMGetDataLayout, LLVMModuleRef) _wrap_objstr2none(LLVMSetDataLayout, LLVMModuleRef) +_wrap_obj2str(LLVMGetModuleIdentifier, LLVMModuleRef) +_wrap_objstr2none(LLVMSetModuleIdentifier, LLVMModuleRef) _wrap_obj2str(LLVMGetTarget, LLVMModuleRef) _wrap_objstr2none(LLVMSetTarget, LLVMModuleRef) _wrap_objstr2none(LLVMModuleAddLibrary, LLVMModuleRef) @@ -94,7 +96,7 @@ _wrap_obj2none(LLVMDisposeModule, LLVMModuleRef) _wrap_dumper(LLVMDumpModuleToString, LLVMModuleRef) _wrap_obj2obj(LLVMModuleGetPointerSize, LLVMModuleRef, int) _wrap_objstrobj2obj(LLVMModuleGetOrInsertFunction, LLVMModuleRef, - LLVMTypeRef, LLVMValueRef) + LLVMTypeRef, LLVMValueRef) static PyObject * _wLLVMVerifyModule(PyObject *self, PyObject *args) @@ -1319,7 +1321,9 @@ static PyMethodDef core_methods[] = { /* Modules */ _method( LLVMModuleCreateWithName ) _method( LLVMGetDataLayout ) - _method( LLVMSetDataLayout ) + _method( LLVMSetDataLayout ) + _method( LLVMGetModuleIdentifier ) + _method( LLVMSetModuleIdentifier ) _method( LLVMGetTarget ) _method( LLVMSetTarget ) _method( LLVMModuleAddLibrary ) diff --git a/llvm/core.py b/llvm/core.py index 0992dde..acbb257 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -507,7 +507,14 @@ class Module(llvm.Ownable, llvm.Cacheable): def add_library(self, name): return _core.LLVMModuleAddLibrary(self.ptr, name) - + + def _get_id(self): + return _core.LLVMGetModuleIdentifier(self.ptr) + + def _set_id(self, string): + _core.LLVMSetModuleIdentifier(self.ptr, string) + + id = property(_get_id, _set_id) #===----------------------------------------------------------------------=== # Types diff --git a/llvm/extra.cpp b/llvm/extra.cpp index d7b33e5..bd65e7b 100644 --- a/llvm/extra.cpp +++ b/llvm/extra.cpp @@ -90,6 +90,16 @@ char *do_print(W obj) return strdup(buf.str().c_str()); } +char *LLVMGetModuleIdentifier(LLVMModuleRef module) +{ + return strdup(llvm::unwrap(module)->getModuleIdentifier().c_str()); +} + +void LLVMSetModuleIdentifier(LLVMModuleRef module, const char * name) +{ + llvm::unwrap(module)->setModuleIdentifier(name); +} + char *LLVMDumpModuleToString(LLVMModuleRef module) { std::string s; diff --git a/llvm/extra.h b/llvm/extra.h index 3bdb070..9e9ae1b 100644 --- a/llvm/extra.h +++ b/llvm/extra.h @@ -41,6 +41,17 @@ extern "C" { #endif + +/* + * Wraps llvm::Module::getModuleIdentifier() + */ +char *LLVMGetModuleIdentifier(LLVMModuleRef module); + +/* + * Wraps llvm::Module::setModuleIdentifier() + */ +void LLVMSetModuleIdentifier(LLVMModuleRef module, const char * name); + /* Notes: * - Some returned strings must be disposed of by LLVMDisposeMessage. These are * indicated in the comments. Where it is not indicated, DO NOT call dispose. diff --git a/test/asm.py b/test/asm.py index 9f028bd..dd564ae 100755 --- a/test/asm.py +++ b/test/asm.py @@ -3,15 +3,27 @@ from llvm import * from llvm.core import * -# create a module -m = Module.new('module1') -m.add_global_variable(Type.int(), 'i') +import unittest -# write it's assembly representation to a file -asm = str(m) -print(asm, file=file("/tmp/testasm.ll", "w")) +class TestAsm(unittest.TestCase): + def test_asm(self): + # create a module + m = Module.new('module1') + m.add_global_variable(Type.int(), 'i') -# read it back into a module -m2 = Module.from_assembly(file("/tmp/testasm.ll")) -print(m2) + # write it's assembly representation to a file + asm = str(m) + with open("/tmp/testasm.ll", "w") as fout: + fout.write(asm) + + # read it back into a module + with open("/tmp/testasm.ll") as fin: + m2 = Module.from_assembly(fin) + # The default `m.id` is ''. + m2.id = m.id # Copy the name from `m` + + self.assertEqual(str(m2).strip(), asm.strip()) + +if __name__ == '__main__': + unittest.main()