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