Add opaque StructType;

Remove test/typehandle.py;
Add test/opaque.py to demonstrate recursive type.
This commit is contained in:
Siu Kwan Lam 2012-07-22 15:14:52 -07:00
commit c9902ab8cf
9 changed files with 171 additions and 80 deletions

40
test/opaque.py Executable file
View file

@ -0,0 +1,40 @@
#!/usr/bin/env python
from llvm.core import *
from llvm import LLVMException
import logging, unittest
class TestOpaque(unittest.TestCase):
def test_opaque(self):
# Create an opaque type
ts = Type.opaque('mystruct')
self.assertIn('type opaque', str(ts))
self.assertTrue(ts.is_opaque)
self.assertTrue(ts.is_identified)
self.assertFalse(ts.is_literal)
logging.debug(ts)
# Create a recursive type
ts.set_body([Type.int(), Type.pointer(ts)])
self.assertEqual(ts.elements[0], Type.int())
self.assertEqual(ts.elements[1], Type.pointer(ts))
self.assertEqual(ts.elements[1].pointee, ts)
self.assertFalse(ts.is_opaque) # is not longer a opaque type
logging.debug(ts)
with self.assertRaises(LLVMException):
# Cannot redefine
ts.set_body([])
def test_opaque_with_no_name(self):
with self.assertRaises(LLVMException):
Type.opaque('')
if __name__ == '__main__':
unittest.main()

View file

@ -67,6 +67,25 @@ class TestModule(unittest.TestCase):
for elty in struct.elements:
self.assertEqual(elty, Type.int())
# rename identified type
struct.name = 'new_name'
self.assertEqual(struct.name, 'new_name')
self.assertIs(m.get_type_named("struct.two.int"), None)
self.assertEqual(got_struct.name, struct.name)
# remove identified type
struct.name = ''
self.assertIs(m.get_type_named("struct.two.int"), None)
self.assertIs(m.get_type_named("new_name"), None)
# another name
struct.name = 'another.name'
self.assertEqual(struct.name, 'another.name')
self.assertEqual(got_struct.name, struct.name)
def testglobal_variable(self):
"""Global variables."""
m = Module.new("test5.1")

View file

@ -3,9 +3,10 @@
from llvm.core import *
from io import StringIO
import unittest
def make_module():
test_module = """
test_module = u"""
define i32 @sum(i32, i32) {
entry:
%2 = add i32 %0, %1
@ -14,15 +15,13 @@ def make_module():
"""
return Module.from_assembly(StringIO(test_module))
class TestAttr(unittest.TestCase):
def test_align(self):
m = make_module()
f = m.get_function_named('sum')
f.args[0].alignment = 16
self.assertIn("align 16", str(f))
self.assertEqual(f.args[0].alignment, 16)
def test_align(m):
f = m.get_function_named('sum')
f.args[0].alignment = 16
assert "align 16" in str(f)
assert f.args[0].alignment == 16
m = make_module()
test_align(m)
if __name__ == '__main__':
unittest.main()

View file

@ -1,19 +0,0 @@
#!/usr/bin/env python
from llvm.core import *
# create a type handle object
th = TypeHandle.new(Type.opaque())
# create the struct with an opaque* instead of self*
ts = Type.struct([ Type.int(), Type.pointer(th.type) ])
# unify the types
th.type.refine(ts)
# create a module, and add a "typedef"
m = Module.new('mod1')
m.add_type_name("struct.node", th.type)
# show what we created
print(m)

View file

@ -2,44 +2,44 @@
from llvm.core import *
m = Module.new('a')
t = Type.int()
ft = Type.function(t, [t, t, t])
f = m.add_function(ft, "func")
b = f.append_basic_block('entry')
bld = Builder.new(b)
tmp1 = bld.add(Constant.int(t, 100), f.args[0], "tmp1")
tmp2 = bld.add(tmp1, f.args[1], "tmp2")
tmp3 = bld.add(tmp1, f.args[2], "tmp3")
bld.ret(tmp3)
import unittest, logging
print("-"*60)
print(m)
print("-"*60)
class TestUses(unittest.TestCase):
def test_uses(self):
m = Module.new('a')
t = Type.int()
ft = Type.function(t, [t, t, t])
f = m.add_function(ft, "func")
b = f.append_basic_block('entry')
bld = Builder.new(b)
tmp1 = bld.add(Constant.int(t, 100), f.args[0], "tmp1")
tmp2 = bld.add(tmp1, f.args[1], "tmp2")
tmp3 = bld.add(tmp1, f.args[2], "tmp3")
bld.ret(tmp3)
print("Testing use count ..", end=' ')
c1 = f.args[0].use_count == 1
c2 = f.args[1].use_count == 1
c3 = f.args[2].use_count == 1
c4 = tmp1.use_count == 2
c5 = tmp2.use_count == 0
c6 = tmp3.use_count == 1
if c1 and c2 and c3 and c4 and c5 and c6:
print("OK")
else:
print("FAIL")
logging.debug("-"*60)
logging.debug(m)
logging.debug("-"*60)
logging.debug("Testing use count ..", end=' ')
self.assertEqual(f.args[0].use_count, 1)
self.assertEqual(f.args[1].use_count, 1)
self.assertEqual(f.args[2].use_count, 1)
self.assertEqual(tmp1.use_count, 2)
self.assertEqual(tmp2.use_count, 0)
self.assertEqual(tmp3.use_count, 1)
logging.debug("Testing uses ..", end=' ')
self.assertIs(f.args[0].uses[0], tmp1)
self.assertEqual(len(f.args[0].uses), 1)
self.assertIs(f.args[1].uses[0], tmp2)
self.assertEqual(len(f.args[1].uses), 1)
self.assertIs(f.args[2].uses[0], tmp3)
self.assertEqual(len(f.args[2].uses), 1)
self.assertEqual(len(tmp1.uses), 2)
self.assertEqual(len(tmp2.uses), 0)
self.assertEqual(len(tmp3.uses), 1)
if __name__ == '__main__':
unittest.main()
print("Testing uses ..", end=' ')
c1 = f.args[0].uses[0] is tmp1
c2 = len(f.args[0].uses) == 1
c3 = f.args[1].uses[0] is tmp2
c4 = len(f.args[1].uses) == 1
c5 = f.args[2].uses[0] is tmp3
c6 = len(f.args[2].uses) == 1
c7 = len(tmp1.uses) == 2
c8 = len(tmp2.uses) == 0
c9 = len(tmp3.uses) == 1
if c1 and c2 and c3 and c4 and c5 and c6 and c7 and c8 and c9:
print("OK")
else:
print("FAIL")