diff options
author | Andreas Nüßlein <nutz@unfoog.de> | 2009-07-02 21:12:49 +0200 |
---|---|---|
committer | Benedikt Böhm <bb@xnull.de> | 2009-07-02 21:12:49 +0200 |
commit | 87b6f874776b253f77f4a1bf4c1844d99a8e544f (patch) | |
tree | 533f15f399da8969f044885851fc97027533d07a /src/back | |
parent | 8c9fb246794f7cb94689c348f104b29433416f4e (diff) | |
download | swppy-87b6f874776b253f77f4a1bf4c1844d99a8e544f.tar.gz swppy-87b6f874776b253f77f4a1bf4c1844d99a8e544f.tar.xz swppy-87b6f874776b253f77f4a1bf4c1844d99a8e544f.zip |
implement three-address-code objects instead of printf
Diffstat (limited to 'src/back')
-rw-r--r-- | src/back/generator.py | 10 | ||||
-rw-r--r-- | src/back/tac.py | 103 |
2 files changed, 67 insertions, 46 deletions
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 """<Operator: "%s">""" % 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 "<TacElem, Op: %s, Arg1: %s, Arg2: %s>" % (self.op,self.arg1,self.arg2) + if self.arg2: + return "<TAC, op: %s, arg1: %s, arg2: %s>" % (self.op,self.arg1,self.arg2) + return "<TAC, op: %s, arg1: %s>" % (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 "<Label, name: %s>" % self.name - def append(self,arg): - self.liste.append(arg) - return len(self.liste)-1 +class FunctionLabel(Label): + def __repr__(self): + return "<FunctionLabel, name: %s>" % 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 |