Add TBAA Builder
This commit is contained in:
parent
425e382368
commit
d491d91dd2
2 changed files with 85 additions and 0 deletions
51
llvm/tbaa.py
Normal file
51
llvm/tbaa.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
from llvm.core import *
|
||||
|
||||
class TBAABuilder(object):
|
||||
'''Simplify creation of TBAA metadata.
|
||||
|
||||
Each TBAABuidler object operates on a module.
|
||||
User can create multiple TBAABuilder on a module
|
||||
'''
|
||||
|
||||
def __init__(self, module, rootid):
|
||||
'''
|
||||
module --- the module to use.
|
||||
root --- string name to identify the TBAA root.
|
||||
'''
|
||||
self.__module = module
|
||||
self.__rootid = rootid
|
||||
self.__rootmd = self.__new_md(rootid)
|
||||
|
||||
@classmethod
|
||||
def new(cls, module, rootid):
|
||||
return cls(module, rootid)
|
||||
|
||||
def get_node(self, name, parent=None, const=False):
|
||||
'''Returns a MetaData object representing a TBAA node.
|
||||
|
||||
Use loadstore_instruction.set_metadata('tbaa', node) to
|
||||
bind a type to a memory.
|
||||
'''
|
||||
parent = parent or self.root
|
||||
const = Constant.int(Type.int(), int(bool(const)))
|
||||
return self.__new_md(name, parent, const)
|
||||
|
||||
@property
|
||||
def module(self):
|
||||
return self.__module
|
||||
|
||||
@property
|
||||
def root(self):
|
||||
return self.__rootmd
|
||||
|
||||
@property
|
||||
def root_name(self):
|
||||
return self.__rootid
|
||||
|
||||
def __new_md(self, *args):
|
||||
contents = list(args)
|
||||
for i, v in enumerate(contents):
|
||||
if isinstance(v, str):
|
||||
contents[i] = MetaDataString.get(self.module, v)
|
||||
return MetaData.get(self.module, contents)
|
||||
|
||||
34
test/tbaa.py
Normal file
34
test/tbaa.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from llvm.core import *
|
||||
from llvm.tbaa import *
|
||||
import unittest
|
||||
|
||||
class TestTBAABuilder(unittest.TestCase):
|
||||
def test_tbaa_builder(self):
|
||||
mod = Module.new('test_tbaa_builder')
|
||||
fty = Type.function(Type.void(), [Type.pointer(Type.float())])
|
||||
foo = mod.add_function(fty, 'foo')
|
||||
bb = foo.append_basic_block('entry')
|
||||
bldr = Builder.new(bb)
|
||||
|
||||
tbaa = TBAABuilder.new(mod, "tbaa.root")
|
||||
float = tbaa.get_node('float', const=False)
|
||||
const_float = tbaa.get_node('const float', float, const=True)
|
||||
|
||||
|
||||
tbaa = TBAABuilder.new(mod, "tbaa.root")
|
||||
old_const_float = const_float
|
||||
del const_float
|
||||
|
||||
const_float = tbaa.get_node('const float', float, const=True)
|
||||
|
||||
self.assertIs(old_const_float, const_float)
|
||||
|
||||
ptr = bldr.load(foo.args[0])
|
||||
ptr.set_metadata('tbaa', const_float)
|
||||
|
||||
|
||||
bldr.ret_void()
|
||||
print mod
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue