From b8e0338da828ff7b37e49ad8ed1c03ae15e6f935 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Wed, 18 Dec 2013 10:43:11 -0600 Subject: [PATCH] Add AllocaInstruction --- llvm/core.py | 33 +++++++++++++++++++++++++++++---- llvm/tests/test_alloca.py | 24 ++++++++++++++++++++++++ llvmpy/capsule.py | 5 +++-- llvmpy/src/Instruction.py | 3 +++ 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 llvm/tests/test_alloca.py diff --git a/llvm/core.py b/llvm/core.py index 10b123a..9fd5282 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -1951,6 +1951,33 @@ class CompareInstruction(Instruction): return FCMPEnum.get(n) +class AllocaInstruction(Instruction): + _type_ = api.llvm.AllocaInst + + @property + def alignment(self): + return self._ptr.getAlignment() + + @alignment.setter + def alignment(self, n): + self._ptr.setAlignment(n) + + @property + def array_size(self): + return self._ptr.getArraySize() + + @array_size.setter + def array_size(self, value): + return self._ptr.setArraySize(value._ptr)._ptr + + @property + def is_array(self): + return self._ptr.isArrayAllocation() + + @property + def is_static(self): + return self._ptr.isStaticAlloca() + #===----------------------------------------------------------------------=== # Basic block #===----------------------------------------------------------------------=== @@ -2010,7 +2037,8 @@ class _ValueFactory(object): VALUE_INSTRUCTION + OPCODE_INVOKE : CallOrInvokeInstruction, VALUE_INSTRUCTION + OPCODE_SWITCH : SwitchInstruction, VALUE_INSTRUCTION + OPCODE_ICMP : CompareInstruction, - VALUE_INSTRUCTION + OPCODE_FCMP : CompareInstruction + VALUE_INSTRUCTION + OPCODE_FCMP : CompareInstruction, + VALUE_INSTRUCTION + OPCODE_ALLOCA : AllocaInstruction, } @classmethod @@ -2218,7 +2246,6 @@ class Builder(llvm.Wrapper): # memory def malloc(self, ty, name=""): - context = api.llvm.getGlobalContext() allocsz = api.llvm.ConstantExpr.getSizeOf(ty._ptr) ity = allocsz.getType() malloc = api.llvm.CallInst.CreateMalloc(self.basic_block._ptr, @@ -2232,7 +2259,6 @@ class Builder(llvm.Wrapper): return _make_value(inst) def malloc_array(self, ty, size, name=""): - context = api.llvm.getGlobalContext() allocsz = api.llvm.ConstantExpr.getSizeOf(ty._ptr) ity = allocsz.getType() malloc = api.llvm.CallInst.CreateMalloc(self.basic_block._ptr, @@ -2246,7 +2272,6 @@ class Builder(llvm.Wrapper): return _make_value(inst) def alloca(self, ty, name=""): - intty = Type.int() return _make_value(self._ptr.CreateAlloca(ty._ptr, None, name)) def alloca_array(self, ty, size, name=""): diff --git a/llvm/tests/test_alloca.py b/llvm/tests/test_alloca.py new file mode 100644 index 0000000..60e3cf5 --- /dev/null +++ b/llvm/tests/test_alloca.py @@ -0,0 +1,24 @@ +import unittest +from llvm.core import Type, Module, Builder, Constant +from .support import TestCase, tests + +class TestAlloca(TestCase): + def test_alloca_alignment(self): + m = Module.new('') + f = m.add_function(Type.function(Type.void(), []), "foo") + b = Builder.new(f.append_basic_block('')) + inst = b.alloca(Type.int(32)) + inst.alignment = 4 + b.ret_void() + m.verify() + + self.assertTrue(inst.is_static) + self.assertFalse(inst.is_array) + self.assertEqual(inst.alignment, 4) + self.assertEqual(str(inst.array_size), 'i32 1') + +tests.append(TestAlloca) + +if __name__ == '__main__': + unittest.main() + diff --git a/llvmpy/capsule.py b/llvmpy/capsule.py index 9de0571..7018ef8 100644 --- a/llvmpy/capsule.py +++ b/llvmpy/capsule.py @@ -84,7 +84,7 @@ class Capsule(object): return cls(self) def __eq__(self, other): - if self.pointer == other.pointer: + if isinstance(other, Capsule) and self.pointer == other.pointer: assert self.name == other.name return True else: @@ -193,7 +193,8 @@ class Wrapper(object): return hash(self._capsule) def __eq__(self, other): - return self._capsule == other._capsule + if isinstance(other, Wrapper): + return self._capsule == other._capsule def __ne__(self, other): return not(self == other) diff --git a/llvmpy/src/Instruction.py b/llvmpy/src/Instruction.py index 2771015..585b7ec 100644 --- a/llvmpy/src/Instruction.py +++ b/llvmpy/src/Instruction.py @@ -346,6 +346,9 @@ class AllocaInst: isStaticAlloca = Method(cast(Bool, bool)) getArraySize = Method(ptr(Value)) getAllocatedType = Method(ptr(Type)) + getAlignment = Method(cast(Unsigned, int)) + setAlignment = Method(Void, cast(int, Unsigned)) + getArraySize = Method(ptr(Value)) @CastInst