Add ways to get the value of a ConstantInt

- property z_ext_value gets the zero-extended value using LLVMConstIntGetZExtValue
- property s_ext_value gets the sign-extended value using LLVMConstIntGetSExtValue
- add test case for both
This commit is contained in:
Wladimir J. van der Laan 2012-09-09 11:57:42 +02:00
commit 41953602bf
3 changed files with 28 additions and 2 deletions

View file

@ -461,6 +461,8 @@ _wLLVMConstReal(PyObject *self, PyObject *args)
}
_wrap_objstr2obj(LLVMConstRealOfString, LLVMTypeRef, LLVMValueRef)
_wrap_obj2obj(LLVMConstIntGetZExtValue, LLVMValueRef, llvmwrap_ull)
_wrap_obj2obj(LLVMConstIntGetSExtValue, LLVMValueRef, llvmwrap_ll)
/* Operations on composite constants */
@ -1580,6 +1582,8 @@ static PyMethodDef core_methods[] = {
_method( LLVMConstInt )
_method( LLVMConstReal )
_method( LLVMConstRealOfString )
_method( LLVMConstIntGetZExtValue )
_method( LLVMConstIntGetSExtValue )
/* Operations on composite constants */
_method( LLVMConstString )

View file

@ -1193,7 +1193,17 @@ class ConstantDataVector(Constant):
pass
class ConstantInt(Constant):
pass
@property
def z_ext_value(self):
'''Obtain the zero extended value for an integer constant value.'''
# Warning: assertion failure when value does not fit in 64 bits
return _core.LLVMConstIntGetZExtValue(self.ptr)
@property
def s_ext_value(self):
'''Obtain the sign extended value for an integer constant value.'''
# Warning: assertion failure when value does not fit in 64 bits
return _core.LLVMConstIntGetSExtValue(self.ptr)
class ConstantFP(Constant):

View file

@ -37,7 +37,9 @@ entry:
%tmp1 = call i32 @prod(i32 %0, i32 %1)
%tmp2 = add i32 %tmp1, %2
%tmp3 = add i32 %tmp2, 1
ret i32 %tmp3
%tmp4 = add i32 %tmp3, -1
%tmp5 = add i64 -81985529216486895, 12297829382473034410
ret i32 %tmp4
}
"""
def test_operands(self):
@ -49,10 +51,20 @@ entry:
# test operands
i1 = test_func.basic_blocks[0].instructions[0]
i2 = test_func.basic_blocks[0].instructions[1]
i3 = test_func.basic_blocks[0].instructions[2]
i4 = test_func.basic_blocks[0].instructions[3]
i5 = test_func.basic_blocks[0].instructions[4]
self.assertEqual(i1.operand_count, 3)
self.assertEqual(i2.operand_count, 2)
self.assertEqual(i3.operands[1].z_ext_value, 1)
self.assertEqual(i3.operands[1].s_ext_value, 1)
self.assertEqual(i4.operands[1].z_ext_value, 0xffffffff)
self.assertEqual(i4.operands[1].s_ext_value, -1)
self.assertEqual(i5.operands[0].s_ext_value, -81985529216486895)
self.assertEqual(i5.operands[1].z_ext_value, 12297829382473034410)
self.assert_(i1.operands[-1] is prod)
self.assert_(i1.operands[0] is test_func.args[0])
self.assert_(i1.operands[1] is test_func.args[1])