annotate python/ppci/c3/codegenerator.py @ 316:56e6ff84f646

Fixed burn led demo
author Windel Bouwman
date Sat, 21 Dec 2013 13:13:26 +0100
parents 084cccaa5deb
children 6f4753202b9a
rev   line source
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
1 import logging
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
2 from .. import ir
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
3 from .. import irutils
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
4 from . import astnodes as ast
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
5
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
6
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
7 class SemanticError(Exception):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
8 """ Error thrown when a semantic issue is observed """
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
9 def __init__(self, msg, loc):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
10 super().__init__()
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
11 self.msg = msg
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
12 self.loc = loc
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
13
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
14
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
15 class CodeGenerator(irutils.Builder):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
16 """
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
17 Generates intermediate (IR) code from a package. The entry function is
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
18 'genModule'. The main task of this part is to rewrite complex control
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
19 structures, such as while and for loops into simple conditional
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
20 jump statements. Also complex conditional statements are simplified.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
21 Such as 'and' and 'or' statements are rewritten in conditional jumps.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
22 And structured datatypes are rewritten.
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
23
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
24 Type checking is done in one run with code generation.
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
25 """
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
26 def __init__(self, diag):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
27 self.logger = logging.getLogger('c3cgen')
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
28 self.diag = diag
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
29
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
30 def gencode(self, pkg):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
31 """ Generate code for a single module """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
32 self.prepare()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
33 assert type(pkg) is ast.Package
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
34 self.pkg = pkg
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
35 self.intType = pkg.scope['int']
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
36 self.boolType = pkg.scope['bool']
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
37 self.logger.info('Generating ir-code for {}'.format(pkg.name), extra={'c3_ast':pkg})
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
38 self.varMap = {} # Maps variables to storage locations.
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
39 self.funcMap = {}
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
40 self.m = ir.Module(pkg.name)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
41 try:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
42 for s in pkg.innerScope.Functions:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
43 f = self.newFunction(s.name)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
44 self.funcMap[s] = f
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
45 for v in pkg.innerScope.Variables:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
46 self.varMap[v] = self.newTemp()
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
47 for s in pkg.innerScope.Functions:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
48 self.gen_function(s)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
49 except SemanticError as e:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
50 self.error(e.msg, e.loc)
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
51 if self.pkg.ok:
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
52 return self.m
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
53
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
54 def error(self, msg, loc=None):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
55 self.pkg.ok = False
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
56 self.diag.error(msg, loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
57
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
58 def gen_function(self, fn):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
59 # TODO: handle arguments
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
60 f = self.funcMap[fn]
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
61 f.return_value = self.newTemp()
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
62 self.setFunction(f)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
63 l2 = self.newBlock()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
64 self.emit(ir.Jump(l2))
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
65 self.setBlock(l2)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
66 # generate room for locals:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
67
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
68 for sym in fn.innerScope:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
69 self.the_type(sym.typ)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
70 if sym.isParameter:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
71 p = ir.Parameter(sym.name)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
72 variable = ir.LocalVariable(sym.name + '_copy')
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
73 f.addParameter(p)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
74 f.addLocal(variable)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
75 # Move parameter into local copy:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
76 self.emit(ir.Move(ir.Mem(variable), p))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
77 elif sym.isLocal:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
78 variable = ir.LocalVariable(sym.name)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
79 f.addLocal(variable)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
80 elif isinstance(sym, ast.Variable):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
81 variable = ir.LocalVariable(sym.name)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
82 f.addLocal(variable)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
83 else:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
84 raise NotImplementedError('{}'.format(sym))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
85 self.varMap[sym] = variable
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
86
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
87 self.genCode(fn.body)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
88 self.emit(ir.Move(f.return_value, ir.Const(0)))
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
89 self.emit(ir.Jump(f.epiloog))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
90 self.setFunction(None)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
91
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
92 def genCode(self, code):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
93 """ Wrapper around gen_stmt to catch errors """
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
94 try:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
95 self.gen_stmt(code)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
96 except SemanticError as e:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
97 self.error(e.msg, e.loc)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
98
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
99 def gen_stmt(self, code):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
100 """ Generate code for a statement """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
101 assert isinstance(code, ast.Statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
102 self.setLoc(code.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
103 if type(code) is ast.Compound:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
104 for s in code.statements:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
105 self.genCode(s)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
106 elif type(code) is ast.Empty:
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
107 pass
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
108 elif type(code) is ast.Assignment:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
109 lval = self.genExprCode(code.lval)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
110 rval = self.genExprCode(code.rval)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
111 if not self.equalTypes(code.lval.typ, code.rval.typ):
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
112 msg = 'Cannot assign {} to {}'.format(code.rval.typ, code.lval.typ)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
113 raise SemanticError(msg, code.loc)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
114 if not code.lval.lvalue:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
115 raise SemanticError('No valid lvalue {}'.format(code.lval), code.lval.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
116 self.emit(ir.Move(lval, rval))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
117 elif type(code) is ast.ExpressionStatement:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
118 self.emit(ir.Exp(self.genExprCode(code.ex)))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
119 elif type(code) is ast.If:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
120 bbtrue = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
121 bbfalse = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
122 te = self.newBlock()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
123 self.gen_cond_code(code.condition, bbtrue, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
124 self.setBlock(bbtrue)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
125 self.genCode(code.truestatement)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
126 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
127 self.setBlock(bbfalse)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
128 self.genCode(code.falsestatement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
129 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
130 self.setBlock(te)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
131 elif type(code) is ast.Return:
303
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
132 re = self.genExprCode(code.expr)
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
133 self.emit(ir.Move(self.fn.return_value, re))
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
134 self.emit(ir.Jump(self.fn.epiloog))
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
135 b = self.newBlock()
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
136 self.setBlock(b)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
137 elif type(code) is ast.While:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
138 bbdo = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
139 bbtest = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
140 te = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
141 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
142 self.setBlock(bbtest)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
143 self.gen_cond_code(code.condition, bbdo, te)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
144 self.setBlock(bbdo)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
145 self.genCode(code.statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
146 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
147 self.setBlock(te)
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
148 elif type(code) is ast.For:
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
149 bbdo = self.newBlock()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
150 bbtest = self.newBlock()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
151 te = self.newBlock()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
152 self.genCode(code.init)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
153 self.emit(ir.Jump(bbtest))
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
154 self.setBlock(bbtest)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
155 self.gen_cond_code(code.condition, bbdo, te)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
156 self.setBlock(bbdo)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
157 self.genCode(code.statement)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
158 self.emit(ir.Jump(bbtest))
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
159 self.setBlock(te)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
160 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
161 raise NotImplementedError('Unknown stmt {}'.format(code))
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
162
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
163 def gen_cond_code(self, expr, bbtrue, bbfalse):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
164 """ Generate conditional logic.
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
165 Implement sequential logical operators. """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
166 if type(expr) is ast.Binop:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
167 if expr.op == 'or':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
168 l2 = self.newBlock()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
169 self.gen_cond_code(expr.a, bbtrue, l2)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
170 if not self.equalTypes(expr.a.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
171 raise SemanticError('Must be boolean', expr.a.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
172 self.setBlock(l2)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
173 self.gen_cond_code(expr.b, bbtrue, bbfalse)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
174 if not self.equalTypes(expr.b.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
175 raise SemanticError('Must be boolean', expr.b.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
176 elif expr.op == 'and':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
177 l2 = self.newBlock()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
178 self.gen_cond_code(expr.a, l2, bbfalse)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
179 if not self.equalTypes(expr.a.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
180 self.error('Must be boolean', expr.a.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
181 self.setBlock(l2)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
182 self.gen_cond_code(expr.b, bbtrue, bbfalse)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
183 if not self.equalTypes(expr.b.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
184 raise SemanticError('Must be boolean', expr.b.loc)
305
0615b5308710 Updated docs
Windel Bouwman
parents: 303
diff changeset
185 elif expr.op in ['==', '>', '<', '!=', '<=', '>=']:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
186 ta = self.genExprCode(expr.a)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
187 tb = self.genExprCode(expr.b)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
188 if not self.equalTypes(expr.a.typ, expr.b.typ):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
189 raise SemanticError('Types unequal {} != {}'
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
190 .format(expr.a.typ, expr.b.typ), expr.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
191 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
192 else:
311
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 308
diff changeset
193 raise SemanticError('non-bool: {}'.format(expr.op), expr.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
194 expr.typ = self.boolType
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
195 elif type(expr) is ast.Literal:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
196 self.genExprCode(expr)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
197 if expr.val:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
198 self.emit(ir.Jump(bbtrue))
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
199 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
200 self.emit(ir.Jump(bbfalse))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
201 else:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
202 raise NotImplementedError('Unknown cond {}'.format(expr))
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
203 if not self.equalTypes(expr.typ, self.boolType):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
204 self.error('Condition must be boolean', expr.loc)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
205
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
206 def genExprCode(self, expr):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
207 """ Generate code for an expression. Return the generated ir-value """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
208 assert isinstance(expr, ast.Expression)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
209 if type(expr) is ast.Binop:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
210 expr.lvalue = False
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
211 if expr.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
212 ra = self.genExprCode(expr.a)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
213 rb = self.genExprCode(expr.b)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
214 if self.equalTypes(expr.a.typ, self.intType) and \
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
215 self.equalTypes(expr.b.typ, self.intType):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
216 expr.typ = expr.a.typ
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
217 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
218 raise SemanticError('Can only add integers', expr.loc)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
219 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
220 raise NotImplementedError("Cannot use equality as expressions")
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
221 return ir.Binop(ra, expr.op, rb)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
222 elif type(expr) is ast.Unop:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
223 if expr.op == '&':
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
224 ra = self.genExprCode(expr.a)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
225 expr.typ = ast.PointerType(expr.a.typ)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
226 if not expr.a.lvalue:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
227 raise SemanticError('No valid lvalue', expr.a.loc)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
228 expr.lvalue = False
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
229 assert type(ra) is ir.Mem
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
230 return ra.e
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
231 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
232 raise NotImplementedError('Unknown unop {0}'.format(expr.op))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
233 elif type(expr) is ast.Identifier:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
234 # Generate code for this identifier.
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
235 tg = self.resolveSymbol(expr)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
236 expr.kind = type(tg)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
237 expr.typ = tg.typ
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
238 # This returns the dereferenced variable.
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
239 if isinstance(tg, ast.Variable):
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
240 expr.lvalue = True
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
241 return ir.Mem(self.varMap[tg])
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
242 elif isinstance(tg, ast.Constant):
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
243 c_val = self.genExprCode(tg.value)
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
244 return self.evalConst(c_val)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
245 else:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
246 raise NotImplementedError(str(tg))
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
247 elif type(expr) is ast.Deref:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
248 # dereference pointer type:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
249 addr = self.genExprCode(expr.ptr)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
250 ptr_typ = self.the_type(expr.ptr.typ)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
251 expr.lvalue = True
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
252 if type(ptr_typ) is ast.PointerType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
253 expr.typ = ptr_typ.ptype
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
254 return ir.Mem(addr)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
255 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
256 raise SemanticError('Cannot deref non-pointer', expr.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
257 elif type(expr) is ast.Member:
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
258 base = self.genExprCode(expr.base)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
259 expr.lvalue = expr.base.lvalue
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
260 basetype = self.the_type(expr.base.typ)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
261 if type(basetype) is ast.StructureType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
262 if basetype.hasField(expr.field):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
263 expr.typ = basetype.fieldType(expr.field)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
264 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
265 raise SemanticError('{} does not contain field {}'
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
266 .format(basetype, expr.field), expr.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
267 else:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
268 raise SemanticError('Cannot select {} of non-structure type {}'
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
269 .format(expr.field, basetype), expr.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
270
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
271 assert type(base) is ir.Mem, type(base)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
272 bt = self.the_type(expr.base.typ)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
273 offset = ir.Const(bt.fieldOffset(expr.field))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
274 return ir.Mem(ir.Add(base.e, offset))
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
275 elif type(expr) is ast.Literal:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
276 expr.lvalue = False
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
277 typemap = {int: 'int', float: 'double', bool: 'bool', str:'string'}
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
278 if type(expr.val) in typemap:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
279 expr.typ = self.pkg.scope[typemap[type(expr.val)]]
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
280 else:
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 311
diff changeset
281 raise SemanticError('Unknown literal type {}'.format(expr.val), expr.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
282 return ir.Const(expr.val)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
283 elif type(expr) is ast.TypeCast:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
284 return self.gen_type_cast(expr)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
285 elif type(expr) is ast.FunctionCall:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
286 return self.gen_function_call(expr)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
287 else:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
288 raise NotImplementedError('Unknown expr {}'.format(expr))
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
289
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
290 def gen_type_cast(self, expr):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
291 """ Generate code for type casting """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
292 ar = self.genExprCode(expr.a)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
293 from_type = self.the_type(expr.a.typ)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
294 to_type = self.the_type(expr.to_type)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
295 if isinstance(from_type, ast.PointerType) and isinstance(to_type, ast.PointerType):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
296 expr.typ = expr.to_type
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
297 return ar
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
298 elif type(from_type) is ast.BaseType and from_type.name == 'int' and \
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
299 isinstance(to_type, ast.PointerType):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
300 expr.typ = expr.to_type
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
301 return ar
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
302 else:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
303 raise SemanticError('Cannot cast {} to {}'
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
304 .format(from_type, to_type), expr.loc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
305
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
306 def gen_function_call(self, expr):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
307 """ Generate code for a function call """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
308 # Evaluate the arguments:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
309 args = [self.genExprCode(e) for e in expr.args]
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
310 # Check arguments:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
311 tg = self.resolveSymbol(expr.proc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
312 if type(tg) is not ast.Function:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
313 raise SemanticError('cannot call {}'.format(tg))
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
314 ftyp = tg.typ
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
315 fname = tg.name
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
316 ptypes = ftyp.parametertypes
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
317 if len(expr.args) != len(ptypes):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
318 raise SemanticError('{} requires {} arguments, {} given'
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
319 .format(fname, len(ptypes), len(expr.args)), expr.loc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
320 for arg, at in zip(expr.args, ptypes):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
321 if not self.equalTypes(arg.typ, at):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
322 raise SemanticError('Got {}, expected {}'
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
323 .format(arg.typ, at), arg.loc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
324 # determine return type:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
325 expr.typ = ftyp.returntype
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
326 return ir.Call(fname, args)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
327
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
328 def evalConst(self, c):
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
329 if isinstance(c, ir.Const):
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
330 return c
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
331 else:
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
332 raise SemanticError('Cannot evaluate constant {}'.format(c))
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
333
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
334 def resolveSymbol(self, sym):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
335 if type(sym) is ast.Member:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
336 base = self.resolveSymbol(sym.base)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
337 if type(base) is not ast.Package:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
338 raise SemanticError('Base is not a package', sym.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
339 scope = base.innerScope
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
340 name = sym.field
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
341 elif type(sym) is ast.Identifier:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
342 scope = sym.scope
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
343 name = sym.target
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
344 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
345 raise NotImplementedError(str(sym))
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
346 if name in scope:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
347 s = scope[name]
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
348 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
349 raise SemanticError('{} undefined'.format(name), sym.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
350 assert isinstance(s, ast.Symbol)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
351 return s
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
352
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
353 def size_of(self, t):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
354 """ Determine the byte size of a type """
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
355 t = self.the_type(t)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
356 if type(t) is ast.BaseType:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
357 return t.bytesize
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
358 elif type(t) is ast.StructureType:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
359 return sum(self.size_of(mem.typ) for mem in t.mems)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
360 else:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
361 raise NotImplementedError(str(t))
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
362
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
363 def the_type(self, t):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
364 """ Recurse until a 'real' type is found """
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
365 if type(t) is ast.DefinedType:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
366 t = self.the_type(t.typ)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
367 elif type(t) in [ast.Identifier, ast.Member]:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
368 t = self.the_type(self.resolveSymbol(t))
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
369 elif type(t) is ast.StructureType:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
370 # Setup offsets of fields. Is this the right place?:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
371 offset = 0
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
372 for mem in t.mems:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
373 mem.offset = offset
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
374 offset = offset + self.size_of(mem.typ)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
375 elif isinstance(t, ast.Type):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
376 pass
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
377 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
378 raise NotImplementedError(str(t))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
379 assert isinstance(t, ast.Type)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
380 return t
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
381
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
382 def equalTypes(self, a, b):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
383 """ Compare types a and b for structural equavalence. """
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
384 # Recurse into named types:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
385 a = self.the_type(a)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
386 b = self.the_type(b)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
387
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
388 if type(a) is type(b):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
389 if type(a) is ast.BaseType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
390 return a.name == b.name
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
391 elif type(a) is ast.PointerType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
392 return self.equalTypes(a.ptype, b.ptype)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
393 elif type(a) is ast.StructureType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
394 if len(a.mems) != len(b.mems):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
395 return False
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
396 return all(self.equalTypes(am.typ, bm.typ) for am, bm in
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
397 zip(a.mems, b.mems))
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
398 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
399 raise NotImplementedError('{} not implemented'.format(type(a)))
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
400 return False