Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 228:7f18ed9b6b7e
Removal of emptystatement class
author | Windel Bouwman |
---|---|
date | Sat, 13 Jul 2013 11:12:24 +0200 |
parents | 82dfe6a32717 |
children | 88a1e0baef65 |
rev | line source |
---|---|
155 | 1 import ir |
2 from . import astnodes | |
222 | 3 from .scope import boolType, intType |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
4 from ppci import CompilerError |
228 | 5 |
170 | 6 class CodeGenerator: |
217 | 7 """ Generates intermediate code from a package """ |
8 def gencode(self, pkg): | |
9 assert type(pkg) is astnodes.Package | |
10 self.varMap = {} # Maps variables to storage locations. | |
11 self.funcMap = {} | |
12 self.builder = ir.Builder() | |
13 m = ir.Module(pkg.name) | |
14 self.builder.setModule(m) | |
15 self.genModule(pkg) | |
16 return m | |
170 | 17 |
217 | 18 # inner helpers: |
19 def genModule(self, pkg): | |
222 | 20 # Take care of forward declarations: |
21 for s in pkg.innerScope.Functions: | |
204 | 22 f = self.builder.newFunction(s.name) |
175 | 23 self.funcMap[s] = f |
222 | 24 for s in pkg.innerScope: |
170 | 25 if type(s) is astnodes.Variable: |
205 | 26 v = self.builder.newVariable(s.name) |
186 | 27 #self.builder.addIns(ir.Alloc(v)) |
28 self.varMap[s] = v | |
170 | 29 elif type(s) is astnodes.Function: |
30 # TODO: handle arguments | |
175 | 31 f = self.funcMap[s] |
204 | 32 self.builder.setFunction(f) |
171 | 33 bb = self.builder.newBB() |
172 | 34 f.entry = bb |
171 | 35 self.builder.setBB(bb) |
174 | 36 # generate room for locals: |
37 | |
217 | 38 for sym in s.innerScope: |
175 | 39 #print(sym, sym.isParameter) |
40 # TODO: handle parameters different | |
174 | 41 v = self.builder.newTmp(sym.name) |
42 self.builder.addIns(ir.Alloc(v)) | |
43 self.varMap[sym] = v | |
44 | |
170 | 45 self.genCode(s.body) |
172 | 46 # TODO handle return? |
171 | 47 self.builder.addIns(ir.Return()) |
204 | 48 self.builder.setFunction(None) |
170 | 49 else: |
50 print(s) | |
158 | 51 |
217 | 52 def genCode(self, code): |
222 | 53 assert isinstance(code, astnodes.Statement) |
54 if type(code) is astnodes.CompoundStatement: | |
221 | 55 for s in code.statements: |
56 self.genCode(s) | |
222 | 57 elif type(code) is astnodes.Assignment: |
228 | 58 re = self.genExprCode(code.rval) |
59 # TODO: Handle pointers | |
60 loc = self.genExprCode(code.lval) | |
61 # determine location of variable | |
62 self.builder.addIns(ir.Store(loc, re)) | |
222 | 63 elif type(code) is astnodes.ExpressionStatement: |
64 self.genExprCode(code.ex) | |
65 elif type(code) is astnodes.IfStatement: | |
228 | 66 bbtrue = self.builder.newBB() |
67 bbfalse = self.builder.newBB() | |
68 te = self.builder.newBB() | |
69 self.genCondCode(code.condition, bbtrue, bbfalse) | |
70 self.builder.setBB(bbtrue) | |
71 self.genCode(code.truestatement) | |
72 self.builder.addIns(ir.Branch(te)) | |
73 self.builder.setBB(bbfalse) | |
74 if code.falsestatement: | |
75 self.genCode(code.falsestatement) | |
76 self.builder.addIns(ir.Branch(te)) | |
77 self.builder.setBB(te) | |
222 | 78 elif type(code) is astnodes.ReturnStatement: |
228 | 79 if code.expr: |
80 re = self.genExprCode(code.expr) | |
81 self.builder.addIns(ir.Return(re)) | |
82 else: | |
83 self.builder.addIns(ir.Return()) | |
222 | 84 elif type(code) is astnodes.WhileStatement: |
228 | 85 bbdo = self.builder.newBB() |
86 bbtest = self.builder.newBB() | |
87 te = self.builder.newBB() | |
88 self.builder.addIns(ir.Branch(bbtest)) | |
89 self.builder.setBB(bbtest) | |
90 self.genCondCode(code.condition, bbdo, te) | |
91 self.builder.setBB(bbdo) | |
92 self.genCode(code.statement) | |
93 self.builder.addIns(ir.Branch(bbtest)) | |
94 self.builder.setBB(te) | |
222 | 95 else: |
228 | 96 print('Unknown stmt:', code) |
217 | 97 def genCondCode(self, expr, bbtrue, bbfalse): |
228 | 98 # Implement sequential logical operators |
99 assert expr.typ == boolType | |
100 if type(expr) is astnodes.Binop: | |
101 if expr.op == 'or': | |
102 l2 = self.builder.newBB() | |
103 self.genCondCode(expr.a, bbtrue, l2) | |
104 self.builder.setBB(l2) | |
105 self.genCondCode(expr.b, bbtrue, bbfalse) | |
106 elif expr.op == 'and': | |
107 l2 = self.builder.newBB() | |
108 self.genCondCode(expr.a, l2, bbfalse) | |
109 self.builder.setBB(l2) | |
110 self.genCondCode(expr.b, bbtrue, bbfalse) | |
111 elif expr.op in ['==', '>', '<']: | |
112 ta = self.genExprCode(expr.a) | |
113 tb = self.genExprCode(expr.b) | |
114 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse) | |
115 self.builder.addIns(i) | |
116 else: | |
117 raise NotImlementedError('Unknown condition {0}'.format(expr)) | |
118 elif type(expr) is astnodes.Literal: | |
119 if expr.val: | |
120 self.builder.addIns(ir.Branch(bbtrue)) | |
121 else: | |
122 self.builder.addIns(ir.Branch(bbfalse)) | |
123 else: | |
124 print('Unknown cond', expr) | |
217 | 125 def genExprCode(self, expr): |
225 | 126 if type(expr) is astnodes.Binop: |
228 | 127 ra = self.genExprCode(expr.a) |
128 rb = self.genExprCode(expr.b) | |
129 ops = ['+', '-', '*', '/', '|', '&'] | |
130 if expr.op in ops: | |
131 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'} | |
132 tmp = self.builder.newTmp(tmpnames[expr.op]) | |
133 op = expr.op | |
134 ins = ir.BinaryOperator(tmp, op, ra, rb) | |
135 self.builder.addIns(ins) | |
136 return tmp | |
137 else: | |
138 print('Unknown {0}'.format(expr)) | |
139 tmp = self.builder.newTmp() | |
140 # TODO | |
141 return tmp | |
225 | 142 elif type(expr) is astnodes.Unop: |
143 ra = self.genExprCode(expr.a) | |
144 if expr.op == '&': | |
145 # Address of operator | |
146 tmp = self.builder.newTmp('addr') | |
147 return tmp | |
148 #self.builder.addIns(ins) | |
149 else: | |
150 raise Exception('Unknown {0}'.format(expr)) | |
151 elif type(expr) is astnodes.Constant: | |
152 tmp = self.builder.newTmp() | |
153 # TODO | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
154 return tmp |
225 | 155 elif type(expr) is astnodes.VariableUse: |
156 tmp = self.builder.newTmp() | |
157 loc = self.varMap[expr.target] | |
158 i = ir.Load(loc, tmp) | |
159 self.builder.addIns(i) | |
160 return tmp | |
161 elif type(expr) is astnodes.Deref: | |
222 | 162 # dereference pointer type: |
225 | 163 addr = self.genExprCode(expr.ptr) |
222 | 164 tmp = self.builder.newTmp('deref') |
225 | 165 ins = ir.Load(addr, tmp) |
222 | 166 self.builder.addIns(ins) |
167 return tmp | |
227 | 168 elif type(expr) is astnodes.FieldRef: |
169 tmp = self.builder.newTmp('struct_mem' + expr.field) | |
170 #ins = ir.BinaryOperator( | |
171 # TODO: add offset? | |
172 return tmp | |
225 | 173 elif type(expr) is astnodes.Literal: |
174 tmp = self.builder.newTmp() | |
175 ins = ir.ImmLoad(tmp, expr.val) | |
176 self.builder.addIns(ins) | |
177 return tmp | |
178 elif type(expr) is astnodes.TypeCast: | |
222 | 179 ar = self.genExprCode(expr.a) |
180 if isinstance(expr.to_type, astnodes.PointerType): | |
181 if expr.a.typ is intType: | |
182 return ar | |
183 elif isinstance(expr.a.typ, astnodes.PointerType): | |
184 return ar | |
185 else: | |
186 raise Exception() | |
225 | 187 elif type(expr) is astnodes.FunctionCall: |
228 | 188 tmp = self.builder.newTmp("res") |
189 args = [] | |
190 for arg in expr.args: | |
191 ar = self.genExprCode(arg) | |
192 args.append(ar) | |
193 fn = self.funcMap[expr.proc] | |
194 ins = ir.Call(fn, args, tmp) | |
195 self.builder.addIns(ins) | |
196 return tmp | |
225 | 197 else: |
228 | 198 raise CompilerError('Unknown expr {}'.format(expr)) |
157 | 199 |