summaryrefslogtreecommitdiffstats
path: root/src/back/tac.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/back/tac.py')
-rw-r--r--src/back/tac.py103
1 files changed, 57 insertions, 46 deletions
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