Add methods to set volatile inst.

This commit is contained in:
Siu Kwan Lam 2012-08-02 16:51:42 -07:00
commit 9d068fd582
3 changed files with 70 additions and 4 deletions

View file

@ -596,6 +596,7 @@ _wrap_obj2obj(LLVMInstIsArithmeticShift, LLVMValueRef, int)
_wrap_obj2obj(LLVMInstIsAssociative, LLVMValueRef, int)
_wrap_obj2obj(LLVMInstIsCommutative, LLVMValueRef, int)
_wrap_obj2obj(LLVMInstIsVolatile, LLVMValueRef, int)
_wrap_objint2none(LLVMSetVolatile, LLVMValueRef)
_wrap_obj2obj(LLVMInstGetOpcode, LLVMValueRef, int)
_wrap_obj2str(LLVMInstGetOpcodeName, LLVMValueRef)
@ -1554,6 +1555,7 @@ static PyMethodDef core_methods[] = {
_method( LLVMInstIsAssociative )
_method( LLVMInstIsCommutative )
_method( LLVMInstIsVolatile )
_method( LLVMSetVolatile )
_method( LLVMInstGetOpcode )
_method( LLVMInstGetOpcodeName )

View file

@ -1458,6 +1458,9 @@ class Instruction(User):
"""True if this is a volatile load or store."""
return _core.LLVMInstIsVolatile(self.ptr) != 0
def set_volatile(self, flag):
return _core.LLVMSetVolatile(self.ptr, int(bool(flag)))
@property
def opcode(self):
return _core.LLVMInstGetOpcode(self.ptr)
@ -1827,14 +1830,20 @@ class Builder(object):
check_is_value(ptr)
return _make_value(_core.LLVMBuildFree(self.ptr, ptr.ptr))
def load(self, ptr, name=""):
def load(self, ptr, name="", volatile=False):
check_is_value(ptr)
return _make_value(_core.LLVMBuildLoad(self.ptr, ptr.ptr, name))
inst = _make_value(_core.LLVMBuildLoad(self.ptr, ptr.ptr, name))
if volatile:
inst.set_volatile(volatile)
return inst
def store(self, value, ptr):
def store(self, value, ptr, volatile=False):
check_is_value(value)
check_is_value(ptr)
return _make_value(_core.LLVMBuildStore(self.ptr, value.ptr, ptr.ptr))
inst = _make_value(_core.LLVMBuildStore(self.ptr, value.ptr, ptr.ptr))
if volatile:
inst.set_volatile(volatile)
return inst
def gep(self, ptr, indices, name=""):
check_is_value(ptr)

55
test/volatile.py Normal file
View file

@ -0,0 +1,55 @@
from llvm.core import *
import unittest
class TestVolatile(unittest.TestCase):
def test_volatile(self):
mod = Module.new('mod')
functype = Type.function(Type.void(), [])
func = mod.add_function(functype, name='foo')
bb = func.append_basic_block('entry')
bldr = Builder.new(bb)
ptr = bldr.alloca(Type.int())
# test load inst
val = bldr.load(ptr)
self.assertFalse(val.is_volatile, "default must be non-volatile")
val.set_volatile(True)
self.assertTrue(val.is_volatile, "fail to set volatile")
val.set_volatile(False)
self.assertFalse(val.is_volatile, "fail to unset volatile")
# test store inst
store_inst = bldr.store(val, ptr)
self.assertFalse(store_inst.is_volatile, "default must be non-volatile")
store_inst.set_volatile(True)
self.assertTrue(store_inst.is_volatile, "fail to set volatile")
store_inst.set_volatile(False)
self.assertFalse(store_inst.is_volatile, "fail to unset volatile")
def test_volatile_another(self):
mod = Module.new('mod')
functype = Type.function(Type.void(), [])
func = mod.add_function(functype, name='foo')
bb = func.append_basic_block('entry')
bldr = Builder.new(bb)
ptr = bldr.alloca(Type.int())
# test load inst
val = bldr.load(ptr, volatile=True)
self.assertTrue(val.is_volatile, "volatile kwarg does not work")
val.set_volatile(False)
self.assertFalse(val.is_volatile, "fail to unset volatile")
val.set_volatile(True)
self.assertTrue(val.is_volatile, "fail to set volatile")
# test store inst
store_inst = bldr.store(val, ptr, volatile=True)
self.assertTrue(store_inst.is_volatile, "volatile kwarg does not work")
store_inst.set_volatile(False)
self.assertFalse(store_inst.is_volatile, "fail to unset volatile")
store_inst.set_volatile(True)
self.assertTrue(store_inst.is_volatile, "fail to set volatile")
if __name__ == '__main__':
unittest.main()