Fix CompareInstruction.predicate
This commit is contained in:
parent
5b2b878f24
commit
a0eb03b239
2 changed files with 67 additions and 2 deletions
18
llvm/core.py
18
llvm/core.py
|
|
@ -55,12 +55,18 @@ class Enum(int):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def declare(cls):
|
def declare(cls):
|
||||||
|
declared = cls._declared_ = {}
|
||||||
scope = globals()
|
scope = globals()
|
||||||
for name in filter(lambda s: s.startswith(cls.prefix), dir(cls)):
|
for name in filter(lambda s: s.startswith(cls.prefix), dir(cls)):
|
||||||
n = getattr(cls, name)
|
n = getattr(cls, name)
|
||||||
typ = type(name, (cls,), {})
|
typ = type(name, (cls,), {})
|
||||||
scope[name] = typ(n)
|
obj = typ(n)
|
||||||
|
declared[n] = obj
|
||||||
|
scope[name] = obj
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(cls, num):
|
||||||
|
return cls._declared_[num]
|
||||||
|
|
||||||
# type id (llvm::Type::TypeID)
|
# type id (llvm::Type::TypeID)
|
||||||
class TypeEnum(Enum):
|
class TypeEnum(Enum):
|
||||||
|
|
@ -1872,16 +1878,24 @@ class PHINode(Instruction):
|
||||||
|
|
||||||
|
|
||||||
class SwitchInstruction(Instruction):
|
class SwitchInstruction(Instruction):
|
||||||
|
_type_ = api.llvm.SwitchInst
|
||||||
|
|
||||||
def add_case(self, const, bblk):
|
def add_case(self, const, bblk):
|
||||||
self._ptr.addCase(const._ptr, bblk._ptr)
|
self._ptr.addCase(const._ptr, bblk._ptr)
|
||||||
|
|
||||||
|
|
||||||
class CompareInstruction(Instruction):
|
class CompareInstruction(Instruction):
|
||||||
|
_type_ = api.llvm.CmpInst
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def predicate(self):
|
def predicate(self):
|
||||||
return self._ptr.getPredicate()
|
n = self._ptr.getPredicate()
|
||||||
|
try:
|
||||||
|
return ICMPEnum.get(n)
|
||||||
|
except KeyError:
|
||||||
|
return FCMPEnum.get(n)
|
||||||
|
|
||||||
|
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
# Basic block
|
# Basic block
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
|
|
|
||||||
|
|
@ -1302,6 +1302,57 @@ class TestArgAttr(TestCase):
|
||||||
|
|
||||||
tests.append(TestArgAttr)
|
tests.append(TestArgAttr)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class TestSwitch(TestCase):
|
||||||
|
def test_arg_attr(self):
|
||||||
|
m = Module.new('oifjda')
|
||||||
|
fnty = Type.function(Type.void(), [Type.int()])
|
||||||
|
func = m.add_function(fnty, 'foo')
|
||||||
|
bb = func.append_basic_block('')
|
||||||
|
bbdef = func.append_basic_block('')
|
||||||
|
bbsw1 = func.append_basic_block('')
|
||||||
|
bbsw2 = func.append_basic_block('')
|
||||||
|
bldr = Builder.new(bb)
|
||||||
|
|
||||||
|
swt = bldr.switch(func.args[0], bbdef, n=2)
|
||||||
|
swt.add_case(Constant.int(Type.int(), 0), bbsw1)
|
||||||
|
swt.add_case(Constant.int(Type.int(), 1), bbsw2)
|
||||||
|
|
||||||
|
bldr.position_at_end(bbsw1)
|
||||||
|
bldr.ret_void()
|
||||||
|
|
||||||
|
bldr.position_at_end(bbsw2)
|
||||||
|
bldr.ret_void()
|
||||||
|
|
||||||
|
bldr.position_at_end(bbdef)
|
||||||
|
bldr.ret_void()
|
||||||
|
|
||||||
|
func.verify()
|
||||||
|
|
||||||
|
tests.append(TestSwitch)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class TestCmp(TestCase):
|
||||||
|
def test_arg_attr(self):
|
||||||
|
m = Module.new('oifjda')
|
||||||
|
fnty = Type.function(Type.void(), [Type.int()])
|
||||||
|
func = m.add_function(fnty, 'foo')
|
||||||
|
bb = func.append_basic_block('')
|
||||||
|
bldr = Builder.new(bb)
|
||||||
|
|
||||||
|
cmpinst = bldr.icmp(lc.ICMP_ULE, func.args[0],
|
||||||
|
Constant.int(Type.int(), 123))
|
||||||
|
self.assertTrue(repr(cmpinst.predicate).startswith('ICMP_ULE'))
|
||||||
|
self.assertEqual(cmpinst.predicate, lc.ICMP_ULE)
|
||||||
|
bldr.ret_void()
|
||||||
|
|
||||||
|
func.verify()
|
||||||
|
|
||||||
|
tests.append(TestCmp)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def run(verbosity=1):
|
def run(verbosity=1):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue