summaryrefslogtreecommitdiffstats
path: root/src/back/tac.py
blob: a875a7d9cdc9d3d422096b10bd311fe7078a5f5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Op(object):
    __slots__ = ["name"]

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    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.BLE      = Op("LE")     # ...
Op.BGE      = Op("GE")
Op.BLT      = Op("LT")
Op.BGT      = 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:

    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)

class TacArray:
    def __init__(self):
        self.liste = []

    def append(self,arg):
        self.liste.append(arg)
        return len(self.liste)-1

    def createList(self):
        i = 0
        output = ""
        for item in self.liste:
            print "%08d | %s\t%s\t%s" % (i,item.op,item.arg1,item.arg2)