175
|
1 from .function import Function
|
171
|
2
|
268
|
3 class Expression:
|
205
|
4 pass
|
|
5
|
239
|
6
|
268
|
7 class Const(Expression):
|
|
8 def __init__(self, value):
|
|
9 self.value = value
|
239
|
10
|
268
|
11 def __repr__(self):
|
|
12 return 'Constant {}'.format(self.value)
|
239
|
13
|
|
14
|
158
|
15 # Function calling:
|
268
|
16 class Call(Expression):
|
|
17 def __init__(self, f, arguments):
|
|
18 self.f = f
|
|
19 assert type(f) is Function
|
|
20 self.arguments = arguments
|
261
|
21
|
|
22 def __repr__(self):
|
268
|
23 args = ','.join([str(arg) for arg in self.arguments])
|
|
24 return '{}({})'.format(self.f.name, args)
|
170
|
25
|
262
|
26
|
171
|
27 # Data operations
|
268
|
28 class Binop(Expression):
|
|
29 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
|
|
30 def __init__(self, value1, operation, value2):
|
|
31 assert operation in Binop.ops
|
261
|
32 self.value1 = value1
|
|
33 self.value2 = value2
|
|
34 self.operation = operation
|
239
|
35
|
230
|
36 def __repr__(self):
|
|
37 a, b = self.value1, self.value2
|
268
|
38 return '({} {} {})'.format(a, self.operation, b)
|
|
39
|
|
40
|
|
41 def Add(a, b):
|
|
42 return Binop(a, '+', b)
|
|
43
|
|
44
|
|
45 class Alloc(Expression):
|
|
46 """ Allocates space on the stack """
|
|
47 def __init__(self):
|
|
48 super().__init__()
|
70
|
49
|
268
|
50 def __repr__(self):
|
|
51 return 'Alloc'
|
|
52
|
|
53
|
|
54 class Temp(Expression):
|
|
55 """ Temporary storage, same as register """
|
|
56 def __init__(self, name):
|
|
57 self.name = name
|
|
58
|
|
59 def __repr__(self):
|
|
60 return 'T_{}_'.format(self.name)
|
239
|
61
|
261
|
62
|
268
|
63 class Mem(Expression):
|
|
64 def __init__(self, e):
|
|
65 self.e = e
|
252
|
66
|
|
67 def __repr__(self):
|
268
|
68 return '[{}]'.format(self.e)
|
|
69
|
|
70
|
|
71 class Statement:
|
|
72 """ Base class for all instructions. """
|
|
73 pass
|
157
|
74
|
261
|
75
|
268
|
76 class Move(Statement):
|
|
77 def __init__(self, dst, src):
|
|
78 self.dst = dst
|
|
79 self.src = src
|
259
|
80
|
252
|
81 def __repr__(self):
|
268
|
82 return '{} = {}'.format(self.dst, self.src)
|
|
83
|
|
84
|
|
85 class Exp(Statement):
|
|
86 def __init__(self, e):
|
|
87 self.e = e
|
|
88
|
252
|
89
|
268
|
90 class Label(Statement):
|
|
91 def __init__(self, name):
|
|
92 self.name = name
|
|
93 self.statements = []
|
|
94
|
|
95 def __repr__(self):
|
|
96 return 'LABEL {}:'.format(self.name)
|
160
|
97
|
261
|
98
|
171
|
99 # Branching:
|
268
|
100 class Jump(Statement):
|
219
|
101 def __init__(self, target):
|
261
|
102 self.target = target
|
268
|
103 self.Targets = [target]
|
261
|
104
|
219
|
105 def __repr__(self):
|
268
|
106 return 'BRANCH {}'.format(self.target.name)
|
170
|
107
|
261
|
108
|
268
|
109 class CJump(Statement):
|
|
110 def __init__(self, a, cond, b, lab_yes, lab_no):
|
|
111 assert cond in ['==', '<', '>', '>=', '<=', '!=']
|
|
112 self.a = a
|
|
113 self.cond = cond
|
|
114 self.b = b
|
|
115 self.lab_yes = lab_yes
|
|
116 self.lab_no = lab_no
|
|
117 self.Targets = [lab_yes, lab_no]
|
261
|
118
|
265
|
119 def __repr__(self):
|
268
|
120 return 'IF {} {} {} THEN {} ELSE {}'.format(self.a, self.cond, self.b, self.lab_yes, self.lab_no)
|
170
|
121
|
261
|
122
|
268
|
123 class Return(Statement):
|
|
124 def __init__(self, value):
|
261
|
125 self.value = value
|
268
|
126 self.Targets = [] # TODO: Do this in another way
|
261
|
127
|
|
128 def __repr__(self):
|
|
129 if self.value:
|
|
130 return 'ret {0}'.format(self.value)
|
|
131 else:
|
|
132 return 'ret'
|
|
133
|
|
134
|