From 87b6f874776b253f77f4a1bf4c1844d99a8e544f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20N=C3=BC=C3=9Flein?= Date: Thu, 2 Jul 2009 21:12:49 +0200 Subject: implement three-address-code objects instead of printf --- src/back/generator.py | 10 +++++ src/back/tac.py | 103 ++++++++++++++++++++++++++++---------------------- 2 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 src/back/generator.py (limited to 'src/back') diff --git a/src/back/generator.py b/src/back/generator.py new file mode 100644 index 0000000..851149b --- /dev/null +++ b/src/back/generator.py @@ -0,0 +1,10 @@ +class Register(object): + __shared_state = {} + count = 0 + + def __init__(self): + self.__dict__ = self.__shared_state + + def new(self): + self.count += 1 + return self.count diff --git a/src/back/tac.py b/src/back/tac.py index fc072d9..23ba1db 100644 --- a/src/back/tac.py +++ b/src/back/tac.py @@ -10,60 +10,71 @@ class Op(object): def __repr__(self): return """""" % self -# arg1 arg2 -Op.ADD = Op("ADD") # x + y -> x y -Op.SUB = Op("SUB") # x - y -> x y -Op.MUL = Op("MUL") # x * y -> x y -Op.DIV = Op("DIV") # x / y -> x y -Op.MOD = Op("MOD") # x % y -> x y -Op.AND = Op("AND") # x AND y -> x y -Op.OR = Op("OR") # x OR y -> x y - -Op.NOT = Op("NOT") # !x -> x -Op.MINUS = Op("MINUS") # -x -> x - -Op.ASSIGN = Op("ASSIGN") # x = y -> x y - -Op.JMP = Op("JMP") # "goto" x -> x -Op.BEQ = Op("EQ") # "if x == y" -> x y -Op.BNE = Op("NE") # "if x != y" -> x y -Op.LE = Op("LE") # ... -Op.GE = Op("GE") -Op.LT = Op("LT") -Op.GT = Op("GT") - -Op.PARAM = Op("PARAM") # param x -> x -Op.CALL = Op("CALL") # fun x (y-args..) -> x y -Op.RETURN = Op("RETURN") # return x -> x - -# maybe in the future -#Op.ARRAYGET = Op("ARRAYGET") # x = y[i] -> i -#Op.ARRAYSET = Op("ARRAYSET") # x[i] = y -> y i - - -class TacElem: - +Op.ADD = Op("ADD") # x = x + y +Op.SUB = Op("SUB") # x = x - y +Op.MUL = Op("MUL") # x = x * y +Op.DIV = Op("DIV") # x = x / y +Op.MOD = Op("MOD") # x = x % y +Op.AND = Op("AND") # x = x AND y +Op.OR = Op("OR") # x = x OR y + +Op.NOT = Op("NOT") # x = !x +Op.MINUS = Op("MINUS") # x = -x + +Op.STORE = Op("STORE") # MEM[x] = y +Op.LOAD = Op("LOAD") # y = MEM[x] +Op.MOV = Op("MOV") # x = y + +Op.CMP = Op("CMP") # Z = x == y, N = x < y + +Op.EQ = Op("EQ") # x = Z +Op.NE = Op("NE") # x = !Z +Op.LT = Op("LT") # x = N +Op.LE = Op("LE") # x = Z || N +Op.GE = Op("GE") # x = !N +Op.GT = Op("GT") # x = !Z && !N + +Op.BEQ = Op("BEQ") # if x goto y +Op.JMP = Op("JMP") # goto x + +Op.PARAM = Op("PARAM") # param x +Op.CALL = Op("CALL") # call x return in y +Op.RETURN = Op("RETURN") # return x + +class TAC(object): def __init__(self,op,arg1=None,arg2=None): self.op = op self.arg1 = arg1 self.arg2 = arg2 def __repr__(self): - return "" % (self.op,self.arg1,self.arg2) + if self.arg2: + return "" % (self.op,self.arg1,self.arg2) + return "" % (self.op,self.arg1) -class TacArray: - def __init__(self): - self.liste = [] +class Label(object): + def __init__(self, name): + self.name = name + + def __repr__(self): + return "" % self.name - def append(self,arg): - self.liste.append(arg) - return len(self.liste)-1 +class FunctionLabel(Label): + def __repr__(self): + return "" % self.name + +class TACList(object): + __shared_state = {} + __tac_list = [] - def createList(self): - i = 1 - output = "" - for item in self.liste: - print "%08d | %s\t%s\t%s" % (i,item.op,item.arg1,item.arg2) - i = i+1 + def __init__(self): + self.__dict__ = self.__shared_state + def add(self, tac): + self.__tac_list.append(tac) + def __repr__(self): + res = "" + for i in range(len(self.__tac_list)): + res += "%04d: %s\n" % (i, repr(self.__tac_list[i])) + return res -- cgit v1.2.3