py2llvm: factor repr/same_type/merge for values

This commit is contained in:
Sebastien Bourdeauducq 2014-09-09 10:30:10 +08:00
parent 60368aa9e2
commit 9eb2a2441d
3 changed files with 16 additions and 30 deletions

View File

@ -4,19 +4,9 @@ from artiq.py2llvm.values import VGeneric
class VNone(VGeneric): class VNone(VGeneric):
def __repr__(self):
return "<VNone>"
def get_llvm_type(self): def get_llvm_type(self):
return lc.Type.void() return lc.Type.void()
def same_type(self, other):
return isinstance(other, VNone)
def merge(self, other):
if not isinstance(other, VNone):
raise TypeError
def alloca(self, builder, name): def alloca(self, builder, name):
pass pass
@ -52,7 +42,8 @@ class VInt(VGeneric):
if other.nbits > self.nbits: if other.nbits > self.nbits:
self.nbits = other.nbits self.nbits = other.nbits
else: else:
raise TypeError raise TypeError("Incompatible types: {} and {}"
.format(repr(self), repr(other)))
def set_value(self, builder, n): def set_value(self, builder, n):
self.set_ssa_value( self.set_ssa_value(
@ -160,15 +151,9 @@ class VBool(VInt):
def __init__(self): def __init__(self):
VInt.__init__(self, 1) VInt.__init__(self, 1)
def __repr__(self): __repr__ = VGeneric.__repr__
return "<VBool>" same_type = VGeneric.same_type
merge = VGeneric.merge
def same_type(self, other):
return isinstance(other, VBool)
def merge(self, other):
if not isinstance(other, VBool):
raise TypeError
def set_const_value(self, builder, b): def set_const_value(self, builder, b):
VInt.set_const_value(self, builder, int(b)) VInt.set_const_value(self, builder, int(b))

View File

@ -70,16 +70,6 @@ class VFraction(VGeneric):
def get_llvm_type(self): def get_llvm_type(self):
return lc.Type.vector(lc.Type.int(64), 2) return lc.Type.vector(lc.Type.int(64), 2)
def __repr__(self):
return "<VFraction>"
def same_type(self, other):
return isinstance(other, VFraction)
def merge(self, other):
if not isinstance(other, VFraction):
raise TypeError
def _nd(self, builder): def _nd(self, builder):
ssa_value = self.get_ssa_value(builder) ssa_value = self.get_ssa_value(builder)
a = builder.extract_element( a = builder.extract_element(

View File

@ -7,6 +7,17 @@ class VGeneric:
def __init__(self): def __init__(self):
self._llvm_value = None self._llvm_value = None
def __repr__(self):
return "<" + self.__class__.__name__ + ">"
def same_type(self, other):
return isinstance(other, self.__class__)
def merge(self, other):
if not self.same_type(other):
raise TypeError("Incompatible types: {} and {}"
.format(repr(self), repr(other)))
def get_ssa_value(self, builder): def get_ssa_value(self, builder):
if isinstance(self._llvm_value, lc.AllocaInstruction): if isinstance(self._llvm_value, lc.AllocaInstruction):
return builder.load(self._llvm_value) return builder.load(self._llvm_value)