Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 235:ff40407c0240
Fix ALabel to Label
author | Windel Bouwman |
---|---|
date | Mon, 15 Jul 2013 17:20:37 +0200 |
parents | d3dccf12ca88 |
children | e41e4109addd |
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) |
58 if type(code) is astnodes.CompoundStatement: | |
221 | 59 for s in code.statements: |
60 self.genCode(s) | |
222 | 61 elif type(code) is astnodes.Assignment: |
228 | 62 re = self.genExprCode(code.rval) |
63 loc = self.genExprCode(code.lval) | |
64 self.builder.addIns(ir.Store(loc, re)) | |
222 | 65 elif type(code) is astnodes.ExpressionStatement: |
66 self.genExprCode(code.ex) | |
67 elif type(code) is astnodes.IfStatement: | |
228 | 68 bbtrue = self.builder.newBB() |
69 bbfalse = self.builder.newBB() | |
70 te = self.builder.newBB() | |
71 self.genCondCode(code.condition, bbtrue, bbfalse) | |
72 self.builder.setBB(bbtrue) | |
73 self.genCode(code.truestatement) | |
74 self.builder.addIns(ir.Branch(te)) | |
75 self.builder.setBB(bbfalse) | |
76 if code.falsestatement: | |
77 self.genCode(code.falsestatement) | |
78 self.builder.addIns(ir.Branch(te)) | |
79 self.builder.setBB(te) | |
222 | 80 elif type(code) is astnodes.ReturnStatement: |
228 | 81 if code.expr: |
82 re = self.genExprCode(code.expr) | |
83 self.builder.addIns(ir.Return(re)) | |
84 else: | |
85 self.builder.addIns(ir.Return()) | |
222 | 86 elif type(code) is astnodes.WhileStatement: |
228 | 87 bbdo = self.builder.newBB() |
88 bbtest = self.builder.newBB() | |
89 te = self.builder.newBB() | |
90 self.builder.addIns(ir.Branch(bbtest)) | |
91 self.builder.setBB(bbtest) | |
92 self.genCondCode(code.condition, bbdo, te) | |
93 self.builder.setBB(bbdo) | |
94 self.genCode(code.statement) | |
95 self.builder.addIns(ir.Branch(bbtest)) | |
96 self.builder.setBB(te) | |
222 | 97 else: |
228 | 98 print('Unknown stmt:', code) |
230 | 99 |
217 | 100 def genCondCode(self, expr, bbtrue, bbfalse): |
228 | 101 # Implement sequential logical operators |
102 assert expr.typ == boolType | |
103 if type(expr) is astnodes.Binop: | |
104 if expr.op == 'or': | |
105 l2 = self.builder.newBB() | |
106 self.genCondCode(expr.a, bbtrue, l2) | |
107 self.builder.setBB(l2) | |
108 self.genCondCode(expr.b, bbtrue, bbfalse) | |
109 elif expr.op == 'and': | |
110 l2 = self.builder.newBB() | |
111 self.genCondCode(expr.a, l2, bbfalse) | |
112 self.builder.setBB(l2) | |
113 self.genCondCode(expr.b, bbtrue, bbfalse) | |
114 elif expr.op in ['==', '>', '<']: | |
115 ta = self.genExprCode(expr.a) | |
116 tb = self.genExprCode(expr.b) | |
117 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse) | |
118 self.builder.addIns(i) | |
119 else: | |
120 raise NotImlementedError('Unknown condition {0}'.format(expr)) | |
121 elif type(expr) is astnodes.Literal: | |
122 if expr.val: | |
123 self.builder.addIns(ir.Branch(bbtrue)) | |
124 else: | |
125 self.builder.addIns(ir.Branch(bbfalse)) | |
126 else: | |
127 print('Unknown cond', expr) | |
230 | 128 |
129 def cast_to_rvalue(self, expr, loc): | |
130 """ Cast lvalue to rvalue if required """ | |
131 if hasattr(expr, 'expect_rvalue'): | |
132 # Load value from memory location: | |
133 tmp = self.builder.newTmp() | |
134 i = ir.Load(loc, tmp) | |
135 self.builder.addIns(i) | |
136 return tmp | |
137 if expr.lvalue: | |
138 return loc | |
139 | |
217 | 140 def genExprCode(self, expr): |
230 | 141 assert isinstance(expr, astnodes.Expression) |
225 | 142 if type(expr) is astnodes.Binop: |
228 | 143 ra = self.genExprCode(expr.a) |
144 rb = self.genExprCode(expr.b) | |
232 | 145 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>'] |
228 | 146 if expr.op in ops: |
147 tmp = self.builder.newTmp(tmpnames[expr.op]) | |
148 op = expr.op | |
149 ins = ir.BinaryOperator(tmp, op, ra, rb) | |
150 self.builder.addIns(ins) | |
151 return tmp | |
152 else: | |
233 | 153 raise NotImplementedError('Unknown {0}'.format(expr)) |
228 | 154 tmp = self.builder.newTmp() |
155 # TODO | |
156 return tmp | |
225 | 157 elif type(expr) is astnodes.Unop: |
158 ra = self.genExprCode(expr.a) | |
159 if expr.op == '&': | |
160 # Address of operator | |
161 tmp = self.builder.newTmp('addr') | |
162 return tmp | |
163 #self.builder.addIns(ins) | |
164 else: | |
233 | 165 raise NotImplementedError('Unknown {0}'.format(expr)) |
225 | 166 elif type(expr) is astnodes.Constant: |
167 tmp = self.builder.newTmp() | |
168 # TODO | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
169 return tmp |
225 | 170 elif type(expr) is astnodes.VariableUse: |
171 loc = self.varMap[expr.target] | |
230 | 172 return self.cast_to_rvalue(expr, loc) |
225 | 173 elif type(expr) is astnodes.Deref: |
222 | 174 # dereference pointer type: |
225 | 175 addr = self.genExprCode(expr.ptr) |
222 | 176 tmp = self.builder.newTmp('deref') |
225 | 177 ins = ir.Load(addr, tmp) |
222 | 178 self.builder.addIns(ins) |
179 return tmp | |
227 | 180 elif type(expr) is astnodes.FieldRef: |
230 | 181 b = self.genExprCode(expr.base) |
182 offset = self.builder.newTmp('off_' + expr.field) | |
231 | 183 bt = theType(expr.base.typ) |
230 | 184 ofs = bt.fieldOffset(expr.field) |
185 ins = ir.ImmLoad(offset, ofs) | |
186 self.builder.addIns(ins) | |
187 tmp2 = self.builder.newTmp('adr_' + expr.field) | |
188 ins = ir.BinaryOperator(tmp2, '+', b, offset) | |
189 self.builder.addIns(ins) | |
190 return self.cast_to_rvalue(expr, tmp2) | |
225 | 191 elif type(expr) is astnodes.Literal: |
192 tmp = self.builder.newTmp() | |
193 ins = ir.ImmLoad(tmp, expr.val) | |
194 self.builder.addIns(ins) | |
195 return tmp | |
196 elif type(expr) is astnodes.TypeCast: | |
222 | 197 ar = self.genExprCode(expr.a) |
231 | 198 tt = theType(expr.to_type) |
230 | 199 if isinstance(tt, astnodes.PointerType): |
222 | 200 if expr.a.typ is intType: |
201 return ar | |
202 elif isinstance(expr.a.typ, astnodes.PointerType): | |
203 return ar | |
204 else: | |
205 raise Exception() | |
230 | 206 else: |
233 | 207 raise NotImplementedError("not implemented") |
225 | 208 elif type(expr) is astnodes.FunctionCall: |
228 | 209 tmp = self.builder.newTmp("res") |
210 args = [] | |
211 for arg in expr.args: | |
212 ar = self.genExprCode(arg) | |
213 args.append(ar) | |
214 fn = self.funcMap[expr.proc] | |
215 ins = ir.Call(fn, args, tmp) | |
216 self.builder.addIns(ins) | |
217 return tmp | |
225 | 218 else: |
233 | 219 raise NotImplementedError('Unknown expr {}'.format(expr)) |
157 | 220 |