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
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
1 import ir
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
2 from . import astnodes
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
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
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
5
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
6 class CodeGenerator:
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
7 """ Generates intermediate code from a package """
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
8 def gencode(self, pkg):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
9 assert type(pkg) is astnodes.Package
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
10 self.varMap = {} # Maps variables to storage locations.
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
11 self.funcMap = {}
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
12 self.builder = ir.Builder()
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
13 m = ir.Module(pkg.name)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
14 self.builder.setModule(m)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
15 self.genModule(pkg)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
16 return m
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
17
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
18 # inner helpers:
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
19 def genModule(self, pkg):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
20 # Take care of forward declarations:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
21 for s in pkg.innerScope.Functions:
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
22 f = self.builder.newFunction(s.name)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
23 self.funcMap[s] = f
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
24 for s in pkg.innerScope:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
25 if type(s) is astnodes.Variable:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
26 v = self.builder.newVariable(s.name)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 177
diff changeset
27 #self.builder.addIns(ir.Alloc(v))
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 177
diff changeset
28 self.varMap[s] = v
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
29 elif type(s) is astnodes.Function:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
30 # TODO: handle arguments
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
31 f = self.funcMap[s]
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
32 self.builder.setFunction(f)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
33 bb = self.builder.newBB()
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
34 f.entry = bb
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
35 self.builder.setBB(bb)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
36 # generate room for locals:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
37
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
38 for sym in s.innerScope:
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
39 #print(sym, sym.isParameter)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
40 # TODO: handle parameters different
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
41 v = self.builder.newTmp(sym.name)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
42 self.builder.addIns(ir.Alloc(v))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
43 self.varMap[sym] = v
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
44
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
45 self.genCode(s.body)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
46 # TODO handle return?
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
47 self.builder.addIns(ir.Return())
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
48 self.builder.setFunction(None)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
49 else:
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
50 print(s)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
51
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
52 def genCode(self, code):
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
53 assert isinstance(code, astnodes.Statement)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
54 if type(code) is astnodes.CompoundStatement:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
55 for s in code.statements:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
56 self.genCode(s)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
57 elif type(code) is astnodes.Assignment:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
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
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
60 loc = self.genExprCode(code.lval)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
61 # determine location of variable
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
62 self.builder.addIns(ir.Store(loc, re))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
63 elif type(code) is astnodes.ExpressionStatement:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
64 self.genExprCode(code.ex)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
65 elif type(code) is astnodes.IfStatement:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
66 bbtrue = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
67 bbfalse = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
68 te = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
69 self.genCondCode(code.condition, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
70 self.builder.setBB(bbtrue)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
71 self.genCode(code.truestatement)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
72 self.builder.addIns(ir.Branch(te))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
73 self.builder.setBB(bbfalse)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
74 self.genCode(code.falsestatement)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
75 self.builder.addIns(ir.Branch(te))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
76 self.builder.setBB(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
77 elif type(code) is astnodes.EmptyStatement:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
78 pass
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
79 elif type(code) is astnodes.ReturnStatement:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
80 if code.expr:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
81 re = self.genExprCode(code.expr)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
82 self.builder.addIns(ir.Return(re))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
83 else:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
84 self.builder.addIns(ir.Return())
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
85 elif type(code) is astnodes.WhileStatement:
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
86 bbdo = self.builder.newBB()
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
87 bbtest = self.builder.newBB()
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
88 te = self.builder.newBB()
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
89 self.builder.addIns(ir.Branch(bbtest))
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
90 self.builder.setBB(bbtest)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
91 self.genCondCode(code.condition, bbdo, te)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
92 self.builder.setBB(bbdo)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
93 self.genCode(code.statement)
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
94 self.builder.addIns(ir.Branch(bbtest))
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
95 self.builder.setBB(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
96 else:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
97 print('Unknown stmt:', code)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
98 def genCondCode(self, expr, bbtrue, bbfalse):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
99 # Implement sequential logical operators
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
100 assert expr.typ == boolType
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
101 if type(expr) is astnodes.Binop:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
102 if expr.op == 'or':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
103 l2 = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
104 self.genCondCode(expr.a, bbtrue, l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
105 self.builder.setBB(l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
106 self.genCondCode(expr.b, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
107 elif expr.op == 'and':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
108 l2 = self.builder.newBB()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
109 self.genCondCode(expr.a, l2, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
110 self.builder.setBB(l2)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
111 self.genCondCode(expr.b, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
112 elif expr.op in ['==', '>', '<']:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
113 ta = self.genExprCode(expr.a)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
114 tb = self.genExprCode(expr.b)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
115 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
116 self.builder.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
117 else:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
118 raise NotImlementedError('Unknown condition {0}'.format(expr))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
119 elif type(expr) is astnodes.Literal:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
120 if expr.val:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
121 self.builder.addIns(ir.Branch(bbtrue))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
122 else:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
123 self.builder.addIns(ir.Branch(bbfalse))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
124 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
125 print('Unknown cond', expr)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
126 def genExprCode(self, expr):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
127 if type(expr) is astnodes.Binop:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
128 ra = self.genExprCode(expr.a)
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
129 rb = self.genExprCode(expr.b)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
130 ops = ['+', '-', '*', '/', '|', '&']
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
131 if expr.op in ops:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
132 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'}
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
133 tmp = self.builder.newTmp(tmpnames[expr.op])
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
134 op = expr.op
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
135 ins = ir.BinaryOperator(tmp, op, ra, rb)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
136 self.builder.addIns(ins)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
137 return tmp
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
138 else:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
139 print('Unknown {0}'.format(expr))
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
140 tmp = self.builder.newTmp()
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
141 # TODO
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
142 return tmp
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
143 elif type(expr) is astnodes.Unop:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
144 ra = self.genExprCode(expr.a)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
145 if expr.op == '&':
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
146 # Address of operator
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
147 tmp = self.builder.newTmp('addr')
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
148 return tmp
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
149 #self.builder.addIns(ins)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
150 else:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
151 raise Exception('Unknown {0}'.format(expr))
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
152 elif type(expr) is astnodes.Constant:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
153 tmp = self.builder.newTmp()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
154 # TODO
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
155 return tmp
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
156 elif type(expr) is astnodes.VariableUse:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
157 tmp = self.builder.newTmp()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
158 loc = self.varMap[expr.target]
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
159 i = ir.Load(loc, tmp)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
160 self.builder.addIns(i)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
161 return tmp
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
162 elif type(expr) is astnodes.Deref:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
163 # dereference pointer type:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
164 addr = self.genExprCode(expr.ptr)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
165 tmp = self.builder.newTmp('deref')
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
166 ins = ir.Load(addr, tmp)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
167 self.builder.addIns(ins)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
168 return tmp
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
169 elif type(expr) is astnodes.Literal:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
170 tmp = self.builder.newTmp()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
171 ins = ir.ImmLoad(tmp, expr.val)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
172 self.builder.addIns(ins)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
173 return tmp
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
174 elif type(expr) is astnodes.TypeCast:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
175 ar = self.genExprCode(expr.a)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
176 if isinstance(expr.to_type, astnodes.PointerType):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
177 if expr.a.typ is intType:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
178 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
179 elif isinstance(expr.a.typ, astnodes.PointerType):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
180 return ar
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
181 else:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
182 raise Exception()
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
183 elif type(expr) is astnodes.FunctionCall:
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
184 tmp = self.builder.newTmp("res")
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
185 args = []
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
186 for arg in expr.args:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
187 ar = self.genExprCode(arg)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
188 args.append(ar)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
189 fn = self.funcMap[expr.proc]
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
190 ins = ir.Call(fn, args, tmp)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
191 self.builder.addIns(ins)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
192 return tmp
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
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
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
195