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:
|
|
72 pass
|
|
73 elif type(code) is astnodes.EmptyStatement:
|
|
74 pass
|
|
75 elif type(code) is astnodes.ReturnStatement:
|
174
|
76 if code.expr:
|
|
77 re = self.genExprCode(code.expr)
|
|
78 self.builder.addIns(ir.Return(re))
|
|
79 else:
|
|
80 self.builder.addIns(ir.Return())
|
170
|
81 else:
|
|
82 print('Unknown stmt:', code)
|
171
|
83 def genCondCode(self, expr, bbtrue, bbfalse):
|
|
84 # Implement sequential logical operators
|
|
85 assert expr.typ == boolType
|
|
86 if type(expr) is astnodes.Binop:
|
|
87 if expr.op == 'or':
|
|
88 l2 = self.builder.newBB()
|
|
89 self.genCondCode(expr.a, bbtrue, l2)
|
|
90 self.builder.setBB(l2)
|
|
91 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
92 elif expr.op == 'and':
|
|
93 l2 = self.builder.newBB()
|
|
94 self.genCondCode(expr.a, l2, bbfalse)
|
|
95 self.builder.setBB(l2)
|
|
96 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
97 elif expr.op in ['==', '>', '<']:
|
|
98 ta = self.genExprCode(expr.a)
|
|
99 tb = self.genExprCode(expr.b)
|
|
100 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
|
|
101 self.builder.addIns(i)
|
|
102 else:
|
174
|
103 raise NotImlementedError('Unknown condition {0}'.format(expr))
|
171
|
104 elif type(expr) is astnodes.Literal:
|
|
105 if expr.val:
|
|
106 self.builder.addIns(ir.BranchInstruction(bbtrue))
|
|
107 else:
|
|
108 self.builder.addIns(ir.BranchInstruction(bbfalse))
|
|
109 else:
|
|
110 print('Unknown cond', expr)
|
170
|
111 def genExprCode(self, expr):
|
|
112 if type(expr) is astnodes.Binop:
|
|
113 ra = self.genExprCode(expr.a)
|
|
114 rb = self.genExprCode(expr.b)
|
171
|
115 tmp = self.builder.newTmp()
|
170
|
116 ops = ['+', '-', '*', '/', 'and', 'or']
|
|
117 if expr.op in ops:
|
|
118 op = expr.op
|
|
119 ins = ir.BinaryOperator(tmp, op, ra, rb)
|
171
|
120 self.builder.addIns(ins)
|
170
|
121 return tmp
|
|
122 else:
|
171
|
123 print('Unknown {0}'.format(expr))
|
|
124 # TODO
|
|
125 return tmp
|
170
|
126 elif type(expr) is astnodes.Constant:
|
171
|
127 tmp = self.builder.newTmp()
|
|
128 # TODO
|
|
129 return tmp
|
170
|
130 elif type(expr) is astnodes.VariableUse:
|
171
|
131 tmp = self.builder.newTmp()
|
174
|
132 loc = self.varMap[expr.target]
|
|
133 i = ir.Load(loc, tmp)
|
171
|
134 self.builder.addIns(i)
|
|
135 return tmp
|
170
|
136 elif type(expr) is astnodes.Literal:
|
171
|
137 tmp = self.builder.newTmp()
|
|
138 ins = ir.ImmLoad(tmp, expr.val)
|
|
139 self.builder.addIns(ins)
|
158
|
140 return tmp
|
175
|
141 elif type(expr) is astnodes.FunctionCall:
|
|
142 tmp = self.builder.newTmp("res")
|
|
143 args = []
|
|
144 for arg in expr.args:
|
|
145 ar = self.genExprCode(arg)
|
|
146 args.append(ar)
|
|
147 fn = self.funcMap[expr.proc]
|
|
148 ins = ir.Call(fn, args, tmp)
|
|
149 self.builder.addIns(ins)
|
|
150 return tmp
|
157
|
151 else:
|
170
|
152 print('Unknown expr:', code)
|
157
|
153
|