155
|
1 import ir
|
|
2 from . import astnodes
|
171
|
3 from .scope import boolType
|
|
4
|
170
|
5 class CodeGenerator:
|
217
|
6 """ Generates intermediate code from a package """
|
|
7 def gencode(self, pkg):
|
|
8 assert type(pkg) is astnodes.Package
|
|
9 self.varMap = {} # Maps variables to storage locations.
|
|
10 self.funcMap = {}
|
|
11 self.builder = ir.Builder()
|
|
12 m = ir.Module(pkg.name)
|
|
13 self.builder.setModule(m)
|
|
14 self.genModule(pkg)
|
|
15 return m
|
170
|
16
|
217
|
17 # inner helpers:
|
|
18 def genModule(self, pkg):
|
175
|
19 # Take care of forward declarations:
|
217
|
20 for s in pkg.innerScope.Functions:
|
204
|
21 f = self.builder.newFunction(s.name)
|
175
|
22 self.funcMap[s] = f
|
217
|
23 for s in pkg.innerScope:
|
170
|
24 if type(s) is astnodes.Variable:
|
205
|
25 v = self.builder.newVariable(s.name)
|
186
|
26 #self.builder.addIns(ir.Alloc(v))
|
|
27 self.varMap[s] = v
|
170
|
28 elif type(s) is astnodes.Function:
|
|
29 # TODO: handle arguments
|
175
|
30 f = self.funcMap[s]
|
204
|
31 self.builder.setFunction(f)
|
171
|
32 bb = self.builder.newBB()
|
172
|
33 f.entry = bb
|
171
|
34 self.builder.setBB(bb)
|
174
|
35 # generate room for locals:
|
|
36
|
217
|
37 for sym in s.innerScope:
|
175
|
38 #print(sym, sym.isParameter)
|
|
39 # TODO: handle parameters different
|
174
|
40 v = self.builder.newTmp(sym.name)
|
|
41 self.builder.addIns(ir.Alloc(v))
|
|
42 self.varMap[sym] = v
|
|
43
|
170
|
44 self.genCode(s.body)
|
172
|
45 # TODO handle return?
|
171
|
46 self.builder.addIns(ir.Return())
|
204
|
47 self.builder.setFunction(None)
|
170
|
48 else:
|
|
49 print(s)
|
158
|
50
|
217
|
51 def genCode(self, code):
|
170
|
52 if type(code) is astnodes.CompoundStatement:
|
|
53 for s in code.statements:
|
|
54 self.genCode(s)
|
|
55 elif type(code) is astnodes.Assignment:
|
|
56 re = self.genExprCode(code.rval)
|
174
|
57 loc = self.varMap[code.lval.target]
|
|
58 self.builder.addIns(ir.Store(loc, re))
|
170
|
59 elif type(code) is astnodes.IfStatement:
|
171
|
60 bbtrue = self.builder.newBB()
|
|
61 bbfalse = self.builder.newBB()
|
|
62 te = self.builder.newBB()
|
|
63 self.genCondCode(code.condition, bbtrue, bbfalse)
|
|
64 self.builder.setBB(bbtrue)
|
170
|
65 self.genCode(code.truestatement)
|
171
|
66 self.builder.addIns(ir.Branch(te))
|
|
67 self.builder.setBB(bbfalse)
|
170
|
68 self.genCode(code.falsestatement)
|
171
|
69 self.builder.addIns(ir.Branch(te))
|
|
70 self.builder.setBB(te)
|
170
|
71 elif type(code) is astnodes.FunctionCall:
|
176
|
72 print('TODO')
|
170
|
73 pass
|
|
74 elif type(code) is astnodes.EmptyStatement:
|
|
75 pass
|
|
76 elif type(code) is astnodes.ReturnStatement:
|
174
|
77 if code.expr:
|
|
78 re = self.genExprCode(code.expr)
|
|
79 self.builder.addIns(ir.Return(re))
|
|
80 else:
|
|
81 self.builder.addIns(ir.Return())
|
176
|
82 elif type(code) is astnodes.WhileStatement:
|
|
83 bbdo = self.builder.newBB()
|
|
84 bbtest = self.builder.newBB()
|
|
85 te = self.builder.newBB()
|
|
86 self.builder.addIns(ir.Branch(bbtest))
|
|
87 self.builder.setBB(bbtest)
|
|
88 self.genCondCode(code.condition, bbdo, te)
|
|
89 self.builder.setBB(bbdo)
|
217
|
90 self.genCode(code.statement)
|
176
|
91 self.builder.addIns(ir.Branch(bbtest))
|
|
92 self.builder.setBB(te)
|
170
|
93 else:
|
|
94 print('Unknown stmt:', code)
|
217
|
95 def genCondCode(self, expr, bbtrue, bbfalse):
|
171
|
96 # Implement sequential logical operators
|
|
97 assert expr.typ == boolType
|
|
98 if type(expr) is astnodes.Binop:
|
|
99 if expr.op == 'or':
|
|
100 l2 = self.builder.newBB()
|
|
101 self.genCondCode(expr.a, bbtrue, l2)
|
|
102 self.builder.setBB(l2)
|
|
103 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
104 elif expr.op == 'and':
|
|
105 l2 = self.builder.newBB()
|
|
106 self.genCondCode(expr.a, l2, bbfalse)
|
|
107 self.builder.setBB(l2)
|
|
108 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
109 elif expr.op in ['==', '>', '<']:
|
|
110 ta = self.genExprCode(expr.a)
|
|
111 tb = self.genExprCode(expr.b)
|
|
112 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
|
|
113 self.builder.addIns(i)
|
|
114 else:
|
174
|
115 raise NotImlementedError('Unknown condition {0}'.format(expr))
|
171
|
116 elif type(expr) is astnodes.Literal:
|
|
117 if expr.val:
|
205
|
118 self.builder.addIns(ir.Branch(bbtrue))
|
171
|
119 else:
|
205
|
120 self.builder.addIns(ir.Branch(bbfalse))
|
171
|
121 else:
|
|
122 print('Unknown cond', expr)
|
217
|
123 def genExprCode(self, expr):
|
170
|
124 if type(expr) is astnodes.Binop:
|
|
125 ra = self.genExprCode(expr.a)
|
|
126 rb = self.genExprCode(expr.b)
|
177
|
127 ops = ['+', '-', '*', '/']
|
170
|
128 if expr.op in ops:
|
177
|
129 tmpnames = {'+':'addtmp', '-':'subtmp', '*': 'multmp', '/':'divtmp'}
|
|
130 tmp = self.builder.newTmp(tmpnames[expr.op])
|
170
|
131 op = expr.op
|
|
132 ins = ir.BinaryOperator(tmp, op, ra, rb)
|
171
|
133 self.builder.addIns(ins)
|
170
|
134 return tmp
|
|
135 else:
|
171
|
136 print('Unknown {0}'.format(expr))
|
177
|
137 tmp = self.builder.newTmp()
|
171
|
138 # TODO
|
|
139 return tmp
|
170
|
140 elif type(expr) is astnodes.Constant:
|
171
|
141 tmp = self.builder.newTmp()
|
|
142 # TODO
|
|
143 return tmp
|
170
|
144 elif type(expr) is astnodes.VariableUse:
|
171
|
145 tmp = self.builder.newTmp()
|
174
|
146 loc = self.varMap[expr.target]
|
|
147 i = ir.Load(loc, tmp)
|
171
|
148 self.builder.addIns(i)
|
|
149 return tmp
|
170
|
150 elif type(expr) is astnodes.Literal:
|
171
|
151 tmp = self.builder.newTmp()
|
|
152 ins = ir.ImmLoad(tmp, expr.val)
|
|
153 self.builder.addIns(ins)
|
158
|
154 return tmp
|
175
|
155 elif type(expr) is astnodes.FunctionCall:
|
|
156 tmp = self.builder.newTmp("res")
|
|
157 args = []
|
|
158 for arg in expr.args:
|
|
159 ar = self.genExprCode(arg)
|
|
160 args.append(ar)
|
|
161 fn = self.funcMap[expr.proc]
|
|
162 ins = ir.Call(fn, args, tmp)
|
|
163 self.builder.addIns(ins)
|
|
164 return tmp
|
157
|
165 else:
|
170
|
166 print('Unknown expr:', code)
|
157
|
167
|