Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 225:1c7364bd74c7
Fixed pointer deref
author | Windel Bouwman |
---|---|
date | Thu, 11 Jul 2013 07:42:30 +0200 |
parents | c3f1ce8b638f |
children | 82dfe6a32717 |
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 |
171 | 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: |
170 | 58 re = self.genExprCode(code.rval) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
59 # TODO: Handle pointers |
221 | 60 loc = self.genExprCode(code.lval) |
61 # determine location of variable | |
174 | 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: | |
171 | 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) | |
170 | 71 self.genCode(code.truestatement) |
171 | 72 self.builder.addIns(ir.Branch(te)) |
73 self.builder.setBB(bbfalse) | |
170 | 74 self.genCode(code.falsestatement) |
171 | 75 self.builder.addIns(ir.Branch(te)) |
76 self.builder.setBB(te) | |
222 | 77 elif type(code) is astnodes.EmptyStatement: |
170 | 78 pass |
222 | 79 elif type(code) is astnodes.ReturnStatement: |
174 | 80 if code.expr: |
81 re = self.genExprCode(code.expr) | |
82 self.builder.addIns(ir.Return(re)) | |
83 else: | |
84 self.builder.addIns(ir.Return()) | |
222 | 85 elif type(code) is astnodes.WhileStatement: |
176 | 86 bbdo = self.builder.newBB() |
87 bbtest = self.builder.newBB() | |
88 te = self.builder.newBB() | |
89 self.builder.addIns(ir.Branch(bbtest)) | |
90 self.builder.setBB(bbtest) | |
91 self.genCondCode(code.condition, bbdo, te) | |
92 self.builder.setBB(bbdo) | |
217 | 93 self.genCode(code.statement) |
176 | 94 self.builder.addIns(ir.Branch(bbtest)) |
95 self.builder.setBB(te) | |
222 | 96 else: |
170 | 97 print('Unknown stmt:', code) |
217 | 98 def genCondCode(self, expr, bbtrue, bbfalse): |
171 | 99 # Implement sequential logical operators |
100 assert expr.typ == boolType | |
101 if type(expr) is astnodes.Binop: | |
102 if expr.op == 'or': | |
103 l2 = self.builder.newBB() | |
104 self.genCondCode(expr.a, bbtrue, l2) | |
105 self.builder.setBB(l2) | |
106 self.genCondCode(expr.b, bbtrue, bbfalse) | |
107 elif expr.op == 'and': | |
108 l2 = self.builder.newBB() | |
109 self.genCondCode(expr.a, l2, bbfalse) | |
110 self.builder.setBB(l2) | |
111 self.genCondCode(expr.b, bbtrue, bbfalse) | |
112 elif expr.op in ['==', '>', '<']: | |
113 ta = self.genExprCode(expr.a) | |
114 tb = self.genExprCode(expr.b) | |
115 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse) | |
116 self.builder.addIns(i) | |
117 else: | |
174 | 118 raise NotImlementedError('Unknown condition {0}'.format(expr)) |
171 | 119 elif type(expr) is astnodes.Literal: |
120 if expr.val: | |
205 | 121 self.builder.addIns(ir.Branch(bbtrue)) |
171 | 122 else: |
205 | 123 self.builder.addIns(ir.Branch(bbfalse)) |
171 | 124 else: |
125 print('Unknown cond', expr) | |
217 | 126 def genExprCode(self, expr): |
225 | 127 if type(expr) is astnodes.Binop: |
170 | 128 ra = self.genExprCode(expr.a) |
129 rb = self.genExprCode(expr.b) | |
221 | 130 ops = ['+', '-', '*', '/', '|', '&'] |
170 | 131 if expr.op in ops: |
221 | 132 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'} |
177 | 133 tmp = self.builder.newTmp(tmpnames[expr.op]) |
170 | 134 op = expr.op |
135 ins = ir.BinaryOperator(tmp, op, ra, rb) | |
171 | 136 self.builder.addIns(ins) |
170 | 137 return tmp |
138 else: | |
171 | 139 print('Unknown {0}'.format(expr)) |
177 | 140 tmp = self.builder.newTmp() |
171 | 141 # TODO |
142 return tmp | |
225 | 143 elif type(expr) is astnodes.Unop: |
144 ra = self.genExprCode(expr.a) | |
145 if expr.op == '&': | |
146 # Address of operator | |
147 tmp = self.builder.newTmp('addr') | |
148 return tmp | |
149 #self.builder.addIns(ins) | |
150 else: | |
151 raise Exception('Unknown {0}'.format(expr)) | |
152 elif type(expr) is astnodes.Constant: | |
153 tmp = self.builder.newTmp() | |
154 # TODO | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
155 return tmp |
225 | 156 elif type(expr) is astnodes.VariableUse: |
157 tmp = self.builder.newTmp() | |
158 loc = self.varMap[expr.target] | |
159 i = ir.Load(loc, tmp) | |
160 self.builder.addIns(i) | |
161 return tmp | |
162 elif type(expr) is astnodes.Deref: | |
222 | 163 # dereference pointer type: |
225 | 164 addr = self.genExprCode(expr.ptr) |
222 | 165 tmp = self.builder.newTmp('deref') |
225 | 166 ins = ir.Load(addr, tmp) |
222 | 167 self.builder.addIns(ins) |
168 return tmp | |
225 | 169 elif type(expr) is astnodes.Literal: |
170 tmp = self.builder.newTmp() | |
171 ins = ir.ImmLoad(tmp, expr.val) | |
172 self.builder.addIns(ins) | |
173 return tmp | |
174 elif type(expr) is astnodes.TypeCast: | |
222 | 175 ar = self.genExprCode(expr.a) |
176 if isinstance(expr.to_type, astnodes.PointerType): | |
177 if expr.a.typ is intType: | |
178 return ar | |
179 elif isinstance(expr.a.typ, astnodes.PointerType): | |
180 return ar | |
181 else: | |
182 raise Exception() | |
225 | 183 elif type(expr) is astnodes.FunctionCall: |
175 | 184 tmp = self.builder.newTmp("res") |
185 args = [] | |
186 for arg in expr.args: | |
187 ar = self.genExprCode(arg) | |
188 args.append(ar) | |
189 fn = self.funcMap[expr.proc] | |
190 ins = ir.Call(fn, args, tmp) | |
191 self.builder.addIns(ins) | |
192 return tmp | |
225 | 193 else: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
194 raise CompilerError('Unknown expr {}'.format(expr)) |
157 | 195 |