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:
|
|
22 f = self.builder.newFunc(s.name)
|
|
23 self.funcMap[s] = f
|
170
|
24 for s in pkg.scope:
|
|
25 if type(s) is astnodes.Variable:
|
|
26 # TODO
|
|
27 pass
|
|
28 elif type(s) is astnodes.Function:
|
|
29 # TODO: handle arguments
|
175
|
30 f = self.funcMap[s]
|
172
|
31 self.builder.setFunc(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
|
|
37 for sym in s.scope:
|
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())
|
172
|
47 self.builder.setFunc(None)
|
170
|
48 else:
|
|
49 print(s)
|
158
|
50
|
170
|
51 def genCode(self, code):
|
|
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)
|
|
90 self.genCode(code.dostatement)
|
|
91 self.builder.addIns(ir.Branch(bbtest))
|
|
92 self.builder.setBB(te)
|
170
|
93 else:
|
|
94 print('Unknown stmt:', code)
|
171
|
95 def genCondCode(self, expr, bbtrue, bbfalse):
|
|
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:
|
|
118 self.builder.addIns(ir.BranchInstruction(bbtrue))
|
|
119 else:
|
|
120 self.builder.addIns(ir.BranchInstruction(bbfalse))
|
|
121 else:
|
|
122 print('Unknown cond', expr)
|
170
|
123 def genExprCode(self, expr):
|
|
124 if type(expr) is astnodes.Binop:
|
|
125 ra = self.genExprCode(expr.a)
|
|
126 rb = self.genExprCode(expr.b)
|
171
|
127 tmp = self.builder.newTmp()
|
170
|
128 ops = ['+', '-', '*', '/', 'and', 'or']
|
|
129 if expr.op in ops:
|
|
130 op = expr.op
|
|
131 ins = ir.BinaryOperator(tmp, op, ra, rb)
|
171
|
132 self.builder.addIns(ins)
|
170
|
133 return tmp
|
|
134 else:
|
171
|
135 print('Unknown {0}'.format(expr))
|
|
136 # TODO
|
|
137 return tmp
|
170
|
138 elif type(expr) is astnodes.Constant:
|
171
|
139 tmp = self.builder.newTmp()
|
|
140 # TODO
|
|
141 return tmp
|
170
|
142 elif type(expr) is astnodes.VariableUse:
|
171
|
143 tmp = self.builder.newTmp()
|
174
|
144 loc = self.varMap[expr.target]
|
|
145 i = ir.Load(loc, tmp)
|
171
|
146 self.builder.addIns(i)
|
|
147 return tmp
|
170
|
148 elif type(expr) is astnodes.Literal:
|
171
|
149 tmp = self.builder.newTmp()
|
|
150 ins = ir.ImmLoad(tmp, expr.val)
|
|
151 self.builder.addIns(ins)
|
158
|
152 return tmp
|
175
|
153 elif type(expr) is astnodes.FunctionCall:
|
|
154 tmp = self.builder.newTmp("res")
|
|
155 args = []
|
|
156 for arg in expr.args:
|
|
157 ar = self.genExprCode(arg)
|
|
158 args.append(ar)
|
|
159 fn = self.funcMap[expr.proc]
|
|
160 ins = ir.Call(fn, args, tmp)
|
|
161 self.builder.addIns(ins)
|
|
162 return tmp
|
157
|
163 else:
|
170
|
164 print('Unknown expr:', code)
|
157
|
165
|