annotate python/c3/codegenerator.py @ 171:3eb9b9e2958d

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