Added Module.id property (getter+setter).

Updated test/asm.py to use unittest.
This commit is contained in:
Siu Kwan Lam 2012-07-20 20:15:21 -07:00
commit 9bbeee519b
6 changed files with 57 additions and 12 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
*~
build

View file

@ -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 )

View file

@ -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

View file

@ -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;

View file

@ -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.

View file

@ -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 '<string>'.
m2.id = m.id # Copy the name from `m`
self.assertEqual(str(m2).strip(), asm.strip())
if __name__ == '__main__':
unittest.main()