Add AllocaInstruction

This commit is contained in:
Siu Kwan Lam 2013-12-18 10:43:11 -06:00
commit b8e0338da8
4 changed files with 59 additions and 6 deletions

View file

@ -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=""):

24
llvm/tests/test_alloca.py Normal file
View file

@ -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()

View file

@ -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)

View file

@ -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