annotate python/c3/codegenerator.py @ 262:ed14e077124c

Added conditional branch instructions
author Windel Bouwman
date Fri, 09 Aug 2013 11:30:11 +0200
parents 444b9df2ed99
children 5ec7580976d9
rev   line source
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
1 import logging
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
2 import ir
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
3 from . import astnodes
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
4 from .scope import boolType, intType
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
5 from ppci import CompilerError
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
6 from .typecheck import theType
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
7
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 231
diff changeset
8 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', \
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 231
diff changeset
9 '&':'and', '>>':'shl', '<<':'shr'}
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
10
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
11 class CodeGenerator:
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
12 """ Generates intermediate code from a package """
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
13 def __init__(self):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
14 self.logger = logging.getLogger('c3cgen')
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
15
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
16 def gencode(self, pkg):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
17 assert type(pkg) is astnodes.Package
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
18 self.logger.info('Generating ir-code for {}'.format(pkg.name))
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
19 self.varMap = {} # Maps variables to storage locations.
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
20 self.funcMap = {}
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
21 self.builder = ir.Builder()
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
22 m = ir.Module(pkg.name)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
23 self.builder.setModule(m)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
24 self.genModule(pkg)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
25 return m
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
26
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
27 # inner helpers:
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
28 def genModule(self, pkg):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
29 # Take care of forward declarations:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
30 for s in pkg.innerScope.Functions:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
31 f = self.builder.newFunction(s.name)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
32 self.funcMap[s] = f
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
33 for s in pkg.innerScope:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
34 if type(s) is astnodes.Variable:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
35 v = self.builder.newTmp(s.name)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 177
diff changeset
36 #self.builder.addIns(ir.Alloc(v))
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 177
diff changeset
37 self.varMap[s] = v
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
38 elif type(s) is astnodes.Function:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
39 # TODO: handle arguments
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
40 f = self.funcMap[s]
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
41 self.builder.setFunction(f)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
42 bb = self.builder.newBB()
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
43 f.entry = bb
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
44 self.builder.setBB(bb)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
45 # generate room for locals:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
46
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
47 for sym in s.innerScope:
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
48 #print(sym, sym.isParameter)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
49 # TODO: handle parameters different
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
50 v = self.builder.newTmp(sym.name)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
51 self.builder.addIns(ir.Alloc(v))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
52 self.varMap[sym] = v
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
53
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
54 self.genCode(s.body)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
55 # TODO handle return?
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
56 self.builder.addIns(ir.Return())
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
57 self.builder.setFunction(None)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
58 else:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
59 print(s)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
60
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
61 def genCode(self, code):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
62 assert isinstance(code, astnodes.Statement)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 233
diff changeset
63 self.builder.setLoc(code.loc)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
64 if type(code) is astnodes.CompoundStatement:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
65 for s in code.statements:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
66 self.genCode(s)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
67 elif type(code) is astnodes.Assignment:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
68 re = self.genExprCode(code.rval)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
69 loc = self.genExprCode(code.lval)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
70 self.builder.addIns(ir.Store(loc, re))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
71 elif type(code) is astnodes.ExpressionStatement:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
72 self.genExprCode(code.ex)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
73 elif type(code) is astnodes.IfStatement:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
74 bbtrue = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
75 bbfalse = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
76 te = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
77 self.genCondCode(code.condition, bbtrue, bbfalse)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
78 self.builder.setBB(bbtrue)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
79 self.genCode(code.truestatement)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
80 self.builder.addIns(ir.Branch(te))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
81 self.builder.setBB(bbfalse)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
82 if code.falsestatement:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
83 self.genCode(code.falsestatement)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
84 self.builder.addIns(ir.Branch(te))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
85 self.builder.setBB(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
86 elif type(code) is astnodes.ReturnStatement:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
87 if code.expr:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
88 re = self.genExprCode(code.expr)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
89 self.builder.addIns(ir.Return(re))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
90 else:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
91 self.builder.addIns(ir.Return())
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
92 elif type(code) is astnodes.WhileStatement:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
93 bbdo = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
94 bbtest = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
95 te = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
96 self.builder.addIns(ir.Branch(bbtest))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
97 self.builder.setBB(bbtest)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
98 self.genCondCode(code.condition, bbdo, te)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
99 self.builder.setBB(bbdo)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
100 self.genCode(code.statement)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
101 self.builder.addIns(ir.Branch(bbtest))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
102 self.builder.setBB(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
103 else:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
104 print('Unknown stmt:', code)
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
105 raise NotImplementedError()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
106
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
107 def genCondCode(self, expr, bbtrue, bbfalse):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
108 # Implement sequential logical operators
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
109 assert expr.typ == boolType
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
110 if type(expr) is astnodes.Binop:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
111 if expr.op == 'or':
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
112 l2 = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
113 self.genCondCode(expr.a, bbtrue, l2)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
114 self.builder.setBB(l2)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
115 self.genCondCode(expr.b, bbtrue, bbfalse)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
116 elif expr.op == 'and':
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
117 l2 = self.builder.newBB()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
118 self.genCondCode(expr.a, l2, bbfalse)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
119 self.builder.setBB(l2)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
120 self.genCondCode(expr.b, bbtrue, bbfalse)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
121 elif expr.op in ['==', '>', '<']:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
122 ta = self.genExprCode(expr.a)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
123 tb = self.genExprCode(expr.b)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
124 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
125 self.builder.addIns(i)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
126 else:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
127 raise NotImlementedError('Unknown condition {0}'.format(expr))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
128 elif type(expr) is astnodes.Literal:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
129 if expr.val:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
130 self.builder.addIns(ir.Branch(bbtrue))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
131 else:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
132 self.builder.addIns(ir.Branch(bbfalse))
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
133 else:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
134 print('Unknown cond', expr)
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
135 raise NotImplementedError()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
136
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
137 def cast_to_rvalue(self, expr, loc):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
138 """ Cast lvalue to rvalue if required """
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
139 if hasattr(expr, 'expect_rvalue'):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
140 # Load value from memory location:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
141 tmp = self.builder.newTmp()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
142 i = ir.Load(loc, tmp)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
143 self.builder.addIns(i)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
144 return tmp
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
145 if expr.lvalue:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
146 return loc
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
147
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
148 def genExprCode(self, expr):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
149 assert isinstance(expr, astnodes.Expression)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
150 if type(expr) is astnodes.Binop:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
151 ra = self.genExprCode(expr.a)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
152 rb = self.genExprCode(expr.b)
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 231
diff changeset
153 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
154 if expr.op in ops:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
155 tmp = self.builder.newTmp(tmpnames[expr.op])
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
156 op = expr.op
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
157 ins = ir.BinaryOperator(tmp, op, ra, rb)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
158 self.builder.addIns(ins)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
159 return tmp
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
160 else:
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 232
diff changeset
161 raise NotImplementedError('Unknown {0}'.format(expr))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
162 tmp = self.builder.newTmp()
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
163 # TODO
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
164 return tmp
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
165 elif type(expr) is astnodes.Unop:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
166 ra = self.genExprCode(expr.a)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
167 if expr.op == '&':
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
168 # Address of operator
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
169 tmp = self.builder.newTmp('addr')
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
170 return tmp
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
171 #self.builder.addIns(ins)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
172 else:
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 232
diff changeset
173 raise NotImplementedError('Unknown {0}'.format(expr))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
174 elif type(expr) is astnodes.Constant:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
175 tmp = self.builder.newTmp()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
176 # TODO
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
177 return tmp
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
178 elif type(expr) is astnodes.VariableUse:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
179 loc = self.varMap[expr.target]
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
180 return self.cast_to_rvalue(expr, loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
181 elif type(expr) is astnodes.Deref:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
182 # dereference pointer type:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
183 addr = self.genExprCode(expr.ptr)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
184 tmp = self.builder.newTmp('deref')
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
185 ins = ir.Load(addr, tmp)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
186 self.builder.addIns(ins)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
187 return tmp
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 225
diff changeset
188 elif type(expr) is astnodes.FieldRef:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
189 b = self.genExprCode(expr.base)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
190 offset = self.builder.newTmp('off_' + expr.field)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
191 bt = theType(expr.base.typ)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
192 ofs = bt.fieldOffset(expr.field)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
193 ins = ir.ImmLoad(offset, ofs)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
194 self.builder.addIns(ins)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
195 tmp2 = self.builder.newTmp('adr_' + expr.field)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
196 ins = ir.BinaryOperator(tmp2, '+', b, offset)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
197 self.builder.addIns(ins)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
198 return self.cast_to_rvalue(expr, tmp2)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
199 elif type(expr) is astnodes.Literal:
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 249
diff changeset
200 tmp = self.builder.newTmp('const')
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
201 ins = ir.ImmLoad(tmp, expr.val)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
202 self.builder.addIns(ins)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
203 return tmp
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
204 elif type(expr) is astnodes.TypeCast:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
205 ar = self.genExprCode(expr.a)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
206 tt = theType(expr.to_type)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
207 if isinstance(tt, astnodes.PointerType):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
208 if expr.a.typ is intType:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
209 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
210 elif isinstance(expr.a.typ, astnodes.PointerType):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
211 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
212 else:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
213 raise Exception()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
214 else:
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 232
diff changeset
215 raise NotImplementedError("not implemented")
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
216 elif type(expr) is astnodes.FunctionCall:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
217 tmp = self.builder.newTmp("res")
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
218 args = []
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
219 for arg in expr.args:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
220 ar = self.genExprCode(arg)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
221 args.append(ar)
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
222 fn = self.funcMap[expr.proc]
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
223 ins = ir.Call(fn, args, tmp)
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
224 self.builder.addIns(ins)
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
225 return tmp
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
226 else:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
227 raise NotImplementedError('Unknown expr {}'.format(expr))
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
228