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