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.
|
171
|
10 self.builder = ir.Builder()
|
|
11 m = ir.Module(pkg.name)
|
|
12 self.builder.setModule(m)
|
170
|
13 self.genModule(pkg)
|
171
|
14 return m
|
170
|
15
|
|
16 # inner helpers:
|
|
17 def genModule(self, pkg):
|
|
18 for s in pkg.scope:
|
|
19 if type(s) is astnodes.Variable:
|
|
20 # TODO
|
|
21 pass
|
|
22 elif type(s) is astnodes.Function:
|
|
23 # TODO: handle arguments
|
172
|
24 f = self.builder.newFunc(s.name)
|
|
25 self.builder.setFunc(f)
|
171
|
26 bb = self.builder.newBB()
|
172
|
27 f.entry = bb
|
171
|
28 self.builder.setBB(bb)
|
174
|
29 # generate room for locals:
|
|
30
|
|
31 for sym in s.scope:
|
|
32 v = self.builder.newTmp(sym.name)
|
|
33 self.builder.addIns(ir.Alloc(v))
|
|
34 self.varMap[sym] = v
|
|
35
|
170
|
36 self.genCode(s.body)
|
172
|
37 # TODO handle return?
|
171
|
38 self.builder.addIns(ir.Return())
|
172
|
39 self.builder.setFunc(None)
|
170
|
40 else:
|
|
41 print(s)
|
158
|
42
|
170
|
43 def genCode(self, code):
|
|
44 if type(code) is astnodes.CompoundStatement:
|
|
45 for s in code.statements:
|
|
46 self.genCode(s)
|
|
47 elif type(code) is astnodes.Assignment:
|
|
48 re = self.genExprCode(code.rval)
|
174
|
49 print(code.lval)
|
|
50 loc = self.varMap[code.lval.target]
|
|
51 self.builder.addIns(ir.Store(loc, re))
|
170
|
52 elif type(code) is astnodes.IfStatement:
|
171
|
53 bbtrue = self.builder.newBB()
|
|
54 bbfalse = self.builder.newBB()
|
|
55 te = self.builder.newBB()
|
|
56 self.genCondCode(code.condition, bbtrue, bbfalse)
|
|
57 self.builder.setBB(bbtrue)
|
170
|
58 self.genCode(code.truestatement)
|
171
|
59 self.builder.addIns(ir.Branch(te))
|
|
60 self.builder.setBB(bbfalse)
|
170
|
61 self.genCode(code.falsestatement)
|
171
|
62 self.builder.addIns(ir.Branch(te))
|
|
63 self.builder.setBB(te)
|
170
|
64 elif type(code) is astnodes.FunctionCall:
|
|
65 pass
|
|
66 elif type(code) is astnodes.EmptyStatement:
|
|
67 pass
|
|
68 elif type(code) is astnodes.ReturnStatement:
|
174
|
69 if code.expr:
|
|
70 re = self.genExprCode(code.expr)
|
|
71 self.builder.addIns(ir.Return(re))
|
|
72 else:
|
|
73 self.builder.addIns(ir.Return())
|
170
|
74 else:
|
|
75 print('Unknown stmt:', code)
|
171
|
76 def genCondCode(self, expr, bbtrue, bbfalse):
|
|
77 # Implement sequential logical operators
|
|
78 assert expr.typ == boolType
|
|
79 if type(expr) is astnodes.Binop:
|
|
80 if expr.op == 'or':
|
|
81 l2 = self.builder.newBB()
|
|
82 self.genCondCode(expr.a, bbtrue, l2)
|
|
83 self.builder.setBB(l2)
|
|
84 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
85 elif expr.op == 'and':
|
|
86 l2 = self.builder.newBB()
|
|
87 self.genCondCode(expr.a, l2, bbfalse)
|
|
88 self.builder.setBB(l2)
|
|
89 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
90 elif expr.op in ['==', '>', '<']:
|
|
91 ta = self.genExprCode(expr.a)
|
|
92 tb = self.genExprCode(expr.b)
|
|
93 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
|
|
94 self.builder.addIns(i)
|
|
95 else:
|
174
|
96 raise NotImlementedError('Unknown condition {0}'.format(expr))
|
171
|
97 elif type(expr) is astnodes.Literal:
|
|
98 if expr.val:
|
|
99 self.builder.addIns(ir.BranchInstruction(bbtrue))
|
|
100 else:
|
|
101 self.builder.addIns(ir.BranchInstruction(bbfalse))
|
|
102 else:
|
|
103 print('Unknown cond', expr)
|
170
|
104 def genExprCode(self, expr):
|
|
105 if type(expr) is astnodes.Binop:
|
|
106 ra = self.genExprCode(expr.a)
|
|
107 rb = self.genExprCode(expr.b)
|
171
|
108 tmp = self.builder.newTmp()
|
170
|
109 ops = ['+', '-', '*', '/', 'and', 'or']
|
|
110 if expr.op in ops:
|
|
111 op = expr.op
|
|
112 ins = ir.BinaryOperator(tmp, op, ra, rb)
|
171
|
113 self.builder.addIns(ins)
|
170
|
114 return tmp
|
|
115 else:
|
171
|
116 print('Unknown {0}'.format(expr))
|
|
117 # TODO
|
|
118 return tmp
|
170
|
119 elif type(expr) is astnodes.Constant:
|
171
|
120 tmp = self.builder.newTmp()
|
|
121 # TODO
|
|
122 return tmp
|
170
|
123 elif type(expr) is astnodes.VariableUse:
|
171
|
124 tmp = self.builder.newTmp()
|
174
|
125 loc = self.varMap[expr.target]
|
|
126 i = ir.Load(loc, tmp)
|
171
|
127 self.builder.addIns(i)
|
|
128 return tmp
|
170
|
129 elif type(expr) is astnodes.Literal:
|
171
|
130 tmp = self.builder.newTmp()
|
|
131 ins = ir.ImmLoad(tmp, expr.val)
|
|
132 self.builder.addIns(ins)
|
158
|
133 return tmp
|
157
|
134 else:
|
170
|
135 print('Unknown expr:', code)
|
157
|
136
|