From d491d91dd27849eac3bd070f2069fe25fa98e398 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Thu, 29 Nov 2012 12:36:54 -0600 Subject: [PATCH] Add TBAA Builder --- llvm/tbaa.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/tbaa.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 llvm/tbaa.py create mode 100644 test/tbaa.py diff --git a/llvm/tbaa.py b/llvm/tbaa.py new file mode 100644 index 0000000..d8e0074 --- /dev/null +++ b/llvm/tbaa.py @@ -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) + diff --git a/test/tbaa.py b/test/tbaa.py new file mode 100644 index 0000000..d775a53 --- /dev/null +++ b/test/tbaa.py @@ -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()