annotate python/c3/codegenerator.py @ 172:5a7d37d615ee

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