From 41953602bf69415c6c5e9d07196e97a4d1875dbd Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sun, 9 Sep 2012 11:57:42 +0200 Subject: [PATCH] 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 --- llvm/_core.cpp | 4 ++++ llvm/core.py | 12 +++++++++++- llvm/test_llvmpy.py | 14 +++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/llvm/_core.cpp b/llvm/_core.cpp index b7de471..dd7f965 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -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 ) diff --git a/llvm/core.py b/llvm/core.py index 40b1c9c..dba7148 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -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): diff --git a/llvm/test_llvmpy.py b/llvm/test_llvmpy.py index fc1b8c8..6c85d93 100644 --- a/llvm/test_llvmpy.py +++ b/llvm/test_llvmpy.py @@ -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])