Mercurial > lcfOS
annotate python/c3/codegenerator.py @ 221:848c4b15fd0b
pointers
author | Windel Bouwman |
---|---|
date | Mon, 08 Jul 2013 22:21:44 +0200 |
parents | 3f6c30a5d234 |
children | c3f1ce8b638f |
rev | line source |
---|---|
155 | 1 import ir |
2 from . import astnodes | |
171 | 3 from .scope import boolType |
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): | |
175 | 20 # Take care of forward declarations: |
217 | 21 for s in pkg.innerScope.Functions: |
204 | 22 f = self.builder.newFunction(s.name) |
175 | 23 self.funcMap[s] = f |
217 | 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): |
170 | 53 if type(code) is astnodes.CompoundStatement: |
221 | 54 for s in code.statements: |
55 self.genCode(s) | |
170 | 56 elif type(code) is astnodes.Assignment: |
57 re = self.genExprCode(code.rval) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
58 # TODO: Handle pointers |
221 | 59 loc = self.genExprCode(code.lval) |
60 # determine location of variable | |
174 | 61 self.builder.addIns(ir.Store(loc, re)) |
170 | 62 elif type(code) is astnodes.IfStatement: |
171 | 63 bbtrue = self.builder.newBB() |
64 bbfalse = self.builder.newBB() | |
65 te = self.builder.newBB() | |
66 self.genCondCode(code.condition, bbtrue, bbfalse) | |
67 self.builder.setBB(bbtrue) | |
170 | 68 self.genCode(code.truestatement) |
171 | 69 self.builder.addIns(ir.Branch(te)) |
70 self.builder.setBB(bbfalse) | |
170 | 71 self.genCode(code.falsestatement) |
171 | 72 self.builder.addIns(ir.Branch(te)) |
73 self.builder.setBB(te) | |
170 | 74 elif type(code) is astnodes.EmptyStatement: |
75 pass | |
76 elif type(code) is astnodes.ReturnStatement: | |
174 | 77 if code.expr: |
78 re = self.genExprCode(code.expr) | |
79 self.builder.addIns(ir.Return(re)) | |
80 else: | |
81 self.builder.addIns(ir.Return()) | |
176 | 82 elif type(code) is astnodes.WhileStatement: |
83 bbdo = self.builder.newBB() | |
84 bbtest = self.builder.newBB() | |
85 te = self.builder.newBB() | |
86 self.builder.addIns(ir.Branch(bbtest)) | |
87 self.builder.setBB(bbtest) | |
88 self.genCondCode(code.condition, bbdo, te) | |
89 self.builder.setBB(bbdo) | |
217 | 90 self.genCode(code.statement) |
176 | 91 self.builder.addIns(ir.Branch(bbtest)) |
92 self.builder.setBB(te) | |
170 | 93 else: |
94 print('Unknown stmt:', code) | |
217 | 95 def genCondCode(self, expr, bbtrue, bbfalse): |
171 | 96 # Implement sequential logical operators |
97 assert expr.typ == boolType | |
98 if type(expr) is astnodes.Binop: | |
99 if expr.op == 'or': | |
100 l2 = self.builder.newBB() | |
101 self.genCondCode(expr.a, bbtrue, l2) | |
102 self.builder.setBB(l2) | |
103 self.genCondCode(expr.b, bbtrue, bbfalse) | |
104 elif expr.op == 'and': | |
105 l2 = self.builder.newBB() | |
106 self.genCondCode(expr.a, l2, bbfalse) | |
107 self.builder.setBB(l2) | |
108 self.genCondCode(expr.b, bbtrue, bbfalse) | |
109 elif expr.op in ['==', '>', '<']: | |
110 ta = self.genExprCode(expr.a) | |
111 tb = self.genExprCode(expr.b) | |
112 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse) | |
113 self.builder.addIns(i) | |
114 else: | |
174 | 115 raise NotImlementedError('Unknown condition {0}'.format(expr)) |
171 | 116 elif type(expr) is astnodes.Literal: |
117 if expr.val: | |
205 | 118 self.builder.addIns(ir.Branch(bbtrue)) |
171 | 119 else: |
205 | 120 self.builder.addIns(ir.Branch(bbfalse)) |
171 | 121 else: |
122 print('Unknown cond', expr) | |
217 | 123 def genExprCode(self, expr): |
170 | 124 if type(expr) is astnodes.Binop: |
125 ra = self.genExprCode(expr.a) | |
126 rb = self.genExprCode(expr.b) | |
221 | 127 ops = ['+', '-', '*', '/', '|', '&'] |
170 | 128 if expr.op in ops: |
221 | 129 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'} |
177 | 130 tmp = self.builder.newTmp(tmpnames[expr.op]) |
170 | 131 op = expr.op |
132 ins = ir.BinaryOperator(tmp, op, ra, rb) | |
171 | 133 self.builder.addIns(ins) |
170 | 134 return tmp |
135 else: | |
171 | 136 print('Unknown {0}'.format(expr)) |
177 | 137 tmp = self.builder.newTmp() |
171 | 138 # TODO |
139 return tmp | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
140 elif type(expr) is astnodes.Unop: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
141 if expr.op == '&': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
142 # Address of operator |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
143 ra = self.genExprCode(expr.a) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
144 tmp = self.builder.newTmp('addr') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
145 return tmp |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
146 #self.builder.addIns(ins) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
147 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
148 print('Unknown {0}'.format(expr)) |
170 | 149 elif type(expr) is astnodes.Constant: |
171 | 150 tmp = self.builder.newTmp() |
151 # TODO | |
152 return tmp | |
170 | 153 elif type(expr) is astnodes.VariableUse: |
171 | 154 tmp = self.builder.newTmp() |
174 | 155 loc = self.varMap[expr.target] |
156 i = ir.Load(loc, tmp) | |
171 | 157 self.builder.addIns(i) |
158 return tmp | |
170 | 159 elif type(expr) is astnodes.Literal: |
171 | 160 tmp = self.builder.newTmp() |
161 ins = ir.ImmLoad(tmp, expr.val) | |
162 self.builder.addIns(ins) | |
158 | 163 return tmp |
175 | 164 elif type(expr) is astnodes.FunctionCall: |
165 tmp = self.builder.newTmp("res") | |
166 args = [] | |
167 for arg in expr.args: | |
168 ar = self.genExprCode(arg) | |
169 args.append(ar) | |
170 fn = self.funcMap[expr.proc] | |
171 ins = ir.Call(fn, args, tmp) | |
172 self.builder.addIns(ins) | |
173 return tmp | |
157 | 174 else: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
175 raise CompilerError('Unknown expr {}'.format(expr)) |
157 | 176 |