Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 222:c3f1ce8b638f
Fixup of parser
author | Windel Bouwman |
---|---|
date | Tue, 09 Jul 2013 17:36:31 +0200 |
parents | 848c4b15fd0b |
children | 1c7364bd74c7 |
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): |
170 | 127 if type(expr) is astnodes.Binop: |
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 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
143 elif type(expr) is astnodes.Unop: |
222 | 144 ra = self.genExprCode(expr.a) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
145 if expr.op == '&': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
146 # Address of operator |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
147 tmp = self.builder.newTmp('addr') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
148 return tmp |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
149 #self.builder.addIns(ins) |
222 | 150 elif expr.op == '*': |
151 # dereference pointer type: | |
152 tmp = self.builder.newTmp('deref') | |
153 ins = ir.Load(ra, tmp) | |
154 self.builder.addIns(ins) | |
155 return tmp | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
156 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
157 print('Unknown {0}'.format(expr)) |
170 | 158 elif type(expr) is astnodes.Constant: |
171 | 159 tmp = self.builder.newTmp() |
160 # TODO | |
161 return tmp | |
170 | 162 elif type(expr) is astnodes.VariableUse: |
171 | 163 tmp = self.builder.newTmp() |
174 | 164 loc = self.varMap[expr.target] |
165 i = ir.Load(loc, tmp) | |
171 | 166 self.builder.addIns(i) |
167 return tmp | |
170 | 168 elif type(expr) is astnodes.Literal: |
171 | 169 tmp = self.builder.newTmp() |
170 ins = ir.ImmLoad(tmp, expr.val) | |
171 self.builder.addIns(ins) | |
158 | 172 return tmp |
222 | 173 elif type(expr) is astnodes.TypeCast: |
174 ar = self.genExprCode(expr.a) | |
175 if isinstance(expr.to_type, astnodes.PointerType): | |
176 if expr.a.typ is intType: | |
177 return ar | |
178 elif isinstance(expr.a.typ, astnodes.PointerType): | |
179 return ar | |
180 else: | |
181 raise Exception() | |
175 | 182 elif type(expr) is astnodes.FunctionCall: |
183 tmp = self.builder.newTmp("res") | |
184 args = [] | |
185 for arg in expr.args: | |
186 ar = self.genExprCode(arg) | |
187 args.append(ar) | |
188 fn = self.funcMap[expr.proc] | |
189 ins = ir.Call(fn, args, tmp) | |
190 self.builder.addIns(ins) | |
191 return tmp | |
157 | 192 else: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
193 raise CompilerError('Unknown expr {}'.format(expr)) |
157 | 194 |