From 9eb2a2441d05b6e3f4ae84cfec9904bb72dafd45 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 9 Sep 2014 10:30:10 +0800 Subject: [PATCH] py2llvm: factor repr/same_type/merge for values --- artiq/py2llvm/base_types.py | 25 +++++-------------------- artiq/py2llvm/fractions.py | 10 ---------- artiq/py2llvm/values.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/artiq/py2llvm/base_types.py b/artiq/py2llvm/base_types.py index a922a2d75..f34646cf6 100644 --- a/artiq/py2llvm/base_types.py +++ b/artiq/py2llvm/base_types.py @@ -4,19 +4,9 @@ from artiq.py2llvm.values import VGeneric class VNone(VGeneric): - def __repr__(self): - return "" - def get_llvm_type(self): 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): pass @@ -52,7 +42,8 @@ class VInt(VGeneric): if other.nbits > self.nbits: self.nbits = other.nbits else: - raise TypeError + raise TypeError("Incompatible types: {} and {}" + .format(repr(self), repr(other))) def set_value(self, builder, n): self.set_ssa_value( @@ -160,15 +151,9 @@ class VBool(VInt): def __init__(self): VInt.__init__(self, 1) - def __repr__(self): - return "" - - def same_type(self, other): - return isinstance(other, VBool) - - def merge(self, other): - if not isinstance(other, VBool): - raise TypeError + __repr__ = VGeneric.__repr__ + same_type = VGeneric.same_type + merge = VGeneric.merge def set_const_value(self, builder, b): VInt.set_const_value(self, builder, int(b)) diff --git a/artiq/py2llvm/fractions.py b/artiq/py2llvm/fractions.py index 7bf652c73..498a916e8 100644 --- a/artiq/py2llvm/fractions.py +++ b/artiq/py2llvm/fractions.py @@ -70,16 +70,6 @@ class VFraction(VGeneric): def get_llvm_type(self): return lc.Type.vector(lc.Type.int(64), 2) - def __repr__(self): - return "" - - def same_type(self, other): - return isinstance(other, VFraction) - - def merge(self, other): - if not isinstance(other, VFraction): - raise TypeError - def _nd(self, builder): ssa_value = self.get_ssa_value(builder) a = builder.extract_element( diff --git a/artiq/py2llvm/values.py b/artiq/py2llvm/values.py index 711b0cdb9..e5536181a 100644 --- a/artiq/py2llvm/values.py +++ b/artiq/py2llvm/values.py @@ -7,6 +7,17 @@ class VGeneric: def __init__(self): 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): if isinstance(self._llvm_value, lc.AllocaInstruction): return builder.load(self._llvm_value)