annotate python/c3/codegenerator.py @ 174:3eb06f5fb987

Added memory alloc for locals
author Windel Bouwman
date Fri, 19 Apr 2013 19:22:52 +0200
parents 5a7d37d615ee
children a51b3c956386
rev   line source
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
1 import ir
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
2 from . import astnodes
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
3 from .scope import boolType
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
4
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
5 class CodeGenerator:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
6 """ Generates intermediate code from a package """
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
7 def gencode(self, pkg):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
8 assert type(pkg) is astnodes.Package
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
9 self.varMap = {} # Maps variables to storage locations.
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
10 self.builder = ir.Builder()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
11 m = ir.Module(pkg.name)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
12 self.builder.setModule(m)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
13 self.genModule(pkg)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
14 return m
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
15
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
16 # inner helpers:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
17 def genModule(self, pkg):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
18 for s in pkg.scope:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
19 if type(s) is astnodes.Variable:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
20 # TODO
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
21 pass
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
22 elif type(s) is astnodes.Function:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
23 # TODO: handle arguments
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
24 f = self.builder.newFunc(s.name)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
25 self.builder.setFunc(f)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
26 bb = self.builder.newBB()
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
27 f.entry = bb
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
28 self.builder.setBB(bb)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
29 # generate room for locals:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
30
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
31 for sym in s.scope:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
32 v = self.builder.newTmp(sym.name)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
33 self.builder.addIns(ir.Alloc(v))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
34 self.varMap[sym] = v
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
35
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
36 self.genCode(s.body)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
37 # TODO handle return?
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
38 self.builder.addIns(ir.Return())
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
39 self.builder.setFunc(None)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
40 else:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
41 print(s)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
42
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
43 def genCode(self, code):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
44 if type(code) is astnodes.CompoundStatement:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
45 for s in code.statements:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
46 self.genCode(s)
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
47 elif type(code) is astnodes.Assignment:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
48 re = self.genExprCode(code.rval)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
49 print(code.lval)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
50 loc = self.varMap[code.lval.target]
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
51 self.builder.addIns(ir.Store(loc, re))
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
52 elif type(code) is astnodes.IfStatement:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
53 bbtrue = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
54 bbfalse = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
55 te = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
56 self.genCondCode(code.condition, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
57 self.builder.setBB(bbtrue)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
58 self.genCode(code.truestatement)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
59 self.builder.addIns(ir.Branch(te))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
60 self.builder.setBB(bbfalse)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
61 self.genCode(code.falsestatement)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
62 self.builder.addIns(ir.Branch(te))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
63 self.builder.setBB(te)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
64 elif type(code) is astnodes.FunctionCall:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
65 pass
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
66 elif type(code) is astnodes.EmptyStatement:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
67 pass
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
68 elif type(code) is astnodes.ReturnStatement:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
69 if code.expr:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
70 re = self.genExprCode(code.expr)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
71 self.builder.addIns(ir.Return(re))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
72 else:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
73 self.builder.addIns(ir.Return())
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
74 else:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
75 print('Unknown stmt:', code)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
76 def genCondCode(self, expr, bbtrue, bbfalse):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
77 # Implement sequential logical operators
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
78 assert expr.typ == boolType
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
79 if type(expr) is astnodes.Binop:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
80 if expr.op == 'or':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
81 l2 = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
82 self.genCondCode(expr.a, bbtrue, l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
83 self.builder.setBB(l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
84 self.genCondCode(expr.b, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
85 elif expr.op == 'and':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
86 l2 = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
87 self.genCondCode(expr.a, l2, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
88 self.builder.setBB(l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
89 self.genCondCode(expr.b, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
90 elif expr.op in ['==', '>', '<']:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
91 ta = self.genExprCode(expr.a)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
92 tb = self.genExprCode(expr.b)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
93 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
94 self.builder.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
95 else:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
96 raise NotImlementedError('Unknown condition {0}'.format(expr))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
97 elif type(expr) is astnodes.Literal:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
98 if expr.val:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
99 self.builder.addIns(ir.BranchInstruction(bbtrue))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
100 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
101 self.builder.addIns(ir.BranchInstruction(bbfalse))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
102 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
103 print('Unknown cond', expr)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
104 def genExprCode(self, expr):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
105 if type(expr) is astnodes.Binop:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
106 ra = self.genExprCode(expr.a)
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
107 rb = self.genExprCode(expr.b)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
108 tmp = self.builder.newTmp()
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
109 ops = ['+', '-', '*', '/', 'and', 'or']
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
110 if expr.op in ops:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
111 op = expr.op
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
112 ins = ir.BinaryOperator(tmp, op, ra, rb)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
113 self.builder.addIns(ins)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
114 return tmp
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
115 else:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
116 print('Unknown {0}'.format(expr))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
117 # TODO
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
118 return tmp
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
119 elif type(expr) is astnodes.Constant:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
120 tmp = self.builder.newTmp()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
121 # TODO
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
122 return tmp
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
123 elif type(expr) is astnodes.VariableUse:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
124 tmp = self.builder.newTmp()
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
125 loc = self.varMap[expr.target]
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
126 i = ir.Load(loc, tmp)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
127 self.builder.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
128 return tmp
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
129 elif type(expr) is astnodes.Literal:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
130 tmp = self.builder.newTmp()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
131 ins = ir.ImmLoad(tmp, expr.val)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
132 self.builder.addIns(ins)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
133 return tmp
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
134 else:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
135 print('Unknown expr:', code)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
136