annotate python/ppci/c3/codegenerator.py @ 300:158068af716c

yafm
author Windel Bouwman
date Tue, 03 Dec 2013 18:00:22 +0100
parents python/c3/codegenerator.py@a747a45dcd78
children 6753763d3bec
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
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 280
diff changeset
6 from .analyse import theType
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
7
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
8
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
9 class CodeGenerator(ir.Builder):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
10 """
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
11 Generates intermediate (IR) code from a package. The entry function is
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
12 'genModule'. The main task of this part is to rewrite complex control
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
13 structures, such as while and for loops into simple conditional
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
14 jump statements. Also complex conditional statements are simplified.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
15 Such as 'and' and 'or' statements are rewritten in conditional jumps.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
16 And structured datatypes are rewritten.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
17 """
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
18 def __init__(self):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
19 self.logger = logging.getLogger('c3cgen')
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
20
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
21 def gencode(self, pkg):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
22 self.prepare()
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
23 assert type(pkg) is astnodes.Package
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
24 self.logger.info('Generating ir-code for {}'.format(pkg.name))
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
25 self.varMap = {} # Maps variables to storage locations.
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
26 self.funcMap = {}
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
27 self.m = ir.Module(pkg.name)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
28 self.genModule(pkg)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
29 return self.m
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
30
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
31 # inner helpers:
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
32 def genModule(self, pkg):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
33 # Take care of forward declarations:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
34 for s in pkg.innerScope.Functions:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
35 f = self.newFunction(s.name)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
36 self.funcMap[s] = f
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
37 for v in pkg.innerScope.Variables:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
38 self.varMap[v] = self.newTemp()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
39 for s in pkg.innerScope.Functions:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
40 self.genFunction(s)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
41
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
42 def genFunction(self, fn):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
43 # TODO: handle arguments
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
44 f = self.funcMap[fn]
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
45 f.return_value = self.newTemp()
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
46 # TODO reserve room for stack, this can be done at later point?
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
47 self.setFunction(f)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
48 l2 = self.newBlock()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
49 self.emit(ir.Jump(l2))
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
50 self.setBlock(l2)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
51 # generate room for locals:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
52
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
53 for sym in fn.innerScope:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
54 # TODO: handle parameters different
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
55 if sym.isParameter:
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
56 v = ir.Parameter(sym.name)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
57 f.addParameter(v)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
58 elif sym.isLocal:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
59 v = ir.LocalVariable(sym.name)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
60 f.addLocal(v)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
61 else:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
62 #v = self.newTemp()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
63 raise NotImplementedError('{}'.format(sym))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
64 # TODO: make this ssa here??
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
65 self.varMap[sym] = v
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
66
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
67 self.genCode(fn.body)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
68 # Set the default return value to zero:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
69 # TBD: this may not be required?
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
70 self.emit(ir.Move(f.return_value, ir.Const(0)))
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
71 self.emit(ir.Jump(f.epiloog))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
72 self.setFunction(None)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
73
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
74 def genCode(self, code):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
75 assert isinstance(code, astnodes.Statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
76 self.setLoc(code.loc)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
77 if type(code) is astnodes.CompoundStatement:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
78 for s in code.statements:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
79 self.genCode(s)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
80 elif type(code) is astnodes.Assignment:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
81 rval = self.genExprCode(code.rval)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
82 lval = self.genExprCode(code.lval)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
83 self.emit(ir.Move(lval, rval))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
84 elif type(code) is astnodes.ExpressionStatement:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
85 self.emit(ir.Exp(self.genExprCode(code.ex)))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
86 elif type(code) is astnodes.IfStatement:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
87 bbtrue = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
88 bbfalse = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
89 te = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
90 self.genCondCode(code.condition, bbtrue, bbfalse)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
91 self.setBlock(bbtrue)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
92 self.genCode(code.truestatement)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
93 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
94 self.setBlock(bbfalse)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
95 if code.falsestatement:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
96 self.genCode(code.falsestatement)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
97 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
98 self.setBlock(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
99 elif type(code) is astnodes.ReturnStatement:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
100 if code.expr:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
101 re = self.genExprCode(code.expr)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
102 self.emit(ir.Move(self.fn.return_value, re))
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
103 self.emit(ir.Jump(self.fn.epiloog))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
104 b = self.newBlock()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
105 self.setBlock(b)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
106 else:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
107 self.builder.addIns(ir.Return())
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
108 elif type(code) is astnodes.WhileStatement:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
109 bbdo = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
110 bbtest = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
111 te = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
112 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
113 self.setBlock(bbtest)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
114 self.genCondCode(code.condition, bbdo, te)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
115 self.setBlock(bbdo)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
116 self.genCode(code.statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
117 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
118 self.setBlock(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
119 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
120 raise NotImplementedError('Unknown stmt {}'.format(code))
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
121
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
122 def genCondCode(self, expr, bbtrue, bbfalse):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
123 # Implement sequential logical operators
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
124 assert expr.typ == boolType
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
125 if type(expr) is astnodes.Binop:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
126 if expr.op == 'or':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
127 l2 = self.newBlock()
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
128 self.genCondCode(expr.a, bbtrue, l2)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
129 self.setBlock(l2)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
130 self.genCondCode(expr.b, bbtrue, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
131 elif expr.op == 'and':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
132 l2 = self.newBlock()
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
133 self.genCondCode(expr.a, l2, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
134 self.setBlock(l2)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
135 self.genCondCode(expr.b, bbtrue, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
136 elif expr.op in ['==', '>', '<']:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
137 ta = self.genExprCode(expr.a)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
138 tb = self.genExprCode(expr.b)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
139 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
140 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
141 raise NotImplementedError('Unknown condition {}'.format(expr))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
142 elif type(expr) is astnodes.Literal:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
143 if expr.val:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
144 self.emit(ir.Jump(bbtrue))
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
145 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
146 self.emit(ir.Jump(bbfalse))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
147 else:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
148 raise NotImplementedError('Unknown cond {}'.format(expr))
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
149
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
150 def genExprCode(self, expr):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
151 assert isinstance(expr, astnodes.Expression)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
152 if type(expr) is astnodes.Binop and expr.op in ir.Binop.ops:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
153 ra = self.genExprCode(expr.a)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
154 rb = self.genExprCode(expr.b)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
155 return ir.Binop(ra, expr.op, rb)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
156 elif type(expr) is astnodes.Unop and expr.op == '&':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
157 ra = self.genExprCode(expr.a)
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
158 assert type(ra) is ir.Mem
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
159 return ra.e
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
160 elif type(expr) is astnodes.VariableUse:
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
161 # This returns the dereferenced variable.
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
162 if expr.target.isParameter:
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
163 # TODO: now parameters are handled different. Not nice?
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
164 return self.varMap[expr.target]
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
165 else:
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
166 return ir.Mem(self.varMap[expr.target])
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
167 elif type(expr) is astnodes.Deref:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
168 # dereference pointer type:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
169 addr = self.genExprCode(expr.ptr)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
170 return ir.Mem(addr)
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 225
diff changeset
171 elif type(expr) is astnodes.FieldRef:
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
172 base = self.genExprCode(expr.base)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
173 assert type(base) is ir.Mem, type(base)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
174 base = base.e
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
175 bt = theType(expr.base.typ)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
176 offset = ir.Const(bt.fieldOffset(expr.field))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
177 return ir.Mem(ir.Add(base, offset))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
178 elif type(expr) is astnodes.Literal:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
179 return ir.Const(expr.val)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
180 elif type(expr) is astnodes.TypeCast:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
181 # TODO: improve this mess:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
182 ar = self.genExprCode(expr.a)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 230
diff changeset
183 tt = theType(expr.to_type)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
184 if isinstance(tt, astnodes.PointerType):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
185 if expr.a.typ is intType:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
186 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
187 elif isinstance(expr.a.typ, astnodes.PointerType):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
188 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
189 else:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
190 raise Exception()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
191 else:
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 232
diff changeset
192 raise NotImplementedError("not implemented")
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
193 elif type(expr) is astnodes.FunctionCall:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
194 args = [self.genExprCode(e) for e in expr.args]
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
195 fn = self.funcMap[expr.proc]
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
196 return ir.Call(fn, args)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
197 else:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
198 raise NotImplementedError('Unknown expr {}'.format(expr))