From 9d068fd582659b1650ab37801879d09e8e3428be Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Thu, 2 Aug 2012 16:51:42 -0700 Subject: [PATCH] Add methods to set volatile inst. --- llvm/_core.cpp | 2 ++ llvm/core.py | 17 +++++++++++---- test/volatile.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 test/volatile.py diff --git a/llvm/_core.cpp b/llvm/_core.cpp index 765d106..821537e 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -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 ) diff --git a/llvm/core.py b/llvm/core.py index f4c75bf..f1cade7 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -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) diff --git a/test/volatile.py b/test/volatile.py new file mode 100644 index 0000000..e567a80 --- /dev/null +++ b/test/volatile.py @@ -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()