annotate python/ppci/c3/codegenerator.py @ 393:6ae782a085e0

Added init program
author Windel Bouwman
date Sat, 17 May 2014 21:17:40 +0200
parents 2ec730e45ea1
children 988f3fb861e4
rev   line source
255
7416c923a02a Added more logging
Windel Bouwman
parents: 252
diff changeset
1 import logging
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
2 import struct
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
3 from .. import ir
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
4 from .. import irutils
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
5 from . import astnodes as ast
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
6
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
7
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
8 class SemanticError(Exception):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
9 """ Error thrown when a semantic issue is observed """
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
10 def __init__(self, msg, loc):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
11 super().__init__()
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
12 self.msg = msg
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
13 self.loc = loc
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
14
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
15
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
16 class CodeGenerator(irutils.Builder):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
17 """
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
18 Generates intermediate (IR) code from a package. The entry function is
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
19 'genModule'. The main task of this part is to rewrite complex control
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
20 structures, such as while and for loops into simple conditional
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
21 jump statements. Also complex conditional statements are simplified.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
22 Such as 'and' and 'or' statements are rewritten in conditional jumps.
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
23 And structured datatypes are rewritten.
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
24
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
25 Type checking is done in one run with code generation.
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
26 """
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
27 def __init__(self, diag):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
28 self.logger = logging.getLogger('c3cgen')
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
29 self.diag = diag
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
30
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
31 def gencode(self, pkg):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
32 """ Generate code for a single module """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
33 self.prepare()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
34 assert type(pkg) is ast.Package
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
35 self.pkg = pkg
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
36 self.intType = pkg.scope['int']
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
37 self.boolType = pkg.scope['bool']
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
38 self.pointerSize = 4
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
39 self.logger.debug('Generating ir-code for {}'.format(pkg.name),
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
40 extra={'c3_ast': pkg})
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
41 self.varMap = {} # Maps variables to storage locations.
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
42 self.m = ir.Module(pkg.name)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
43 try:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
44 for typ in pkg.Types:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
45 self.check_type(typ)
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
46 # Only generate function if function contains a body:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
47 real_functions = list(filter(
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
48 lambda f: f.body, pkg.innerScope.Functions))
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
49 for v in pkg.innerScope.Variables:
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
50 v2 = ir.GlobalVariable(v.name)
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
51 self.varMap[v] = v2
363
396e5cefba13 Removed debuginfo instruction
Windel Bouwman
parents: 362
diff changeset
52 if not v.isLocal:
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
53 self.m.add_variable(v2)
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
54 for s in real_functions:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
55 self.gen_function(s)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
56 except SemanticError as e:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
57 self.error(e.msg, e.loc)
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
58 if self.pkg.ok:
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
59 return self.m
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
60
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
61 def error(self, msg, loc=None):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
62 self.pkg.ok = False
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
63 self.diag.error(msg, loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
64
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
65 def gen_function(self, fn):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
66 # TODO: handle arguments
363
396e5cefba13 Removed debuginfo instruction
Windel Bouwman
parents: 362
diff changeset
67 f = self.newFunction(fn.name)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
68 f.return_value = self.newTemp()
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
69 self.setFunction(f)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
70 l2 = self.newBlock()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
71 self.emit(ir.Jump(l2))
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
72 self.setBlock(l2)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
73 # generate room for locals:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
74
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
75 for sym in fn.innerScope:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
76 self.check_type(sym.typ)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
77 if sym.isParameter:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
78 p = ir.Parameter(sym.name)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
79 variable = ir.LocalVariable(sym.name + '_copy')
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
80 f.addParameter(p)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
81 f.addLocal(variable)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
82 # Move parameter into local copy:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
83 self.emit(ir.Move(ir.Mem(variable), p))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
84 elif sym.isLocal:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
85 variable = ir.LocalVariable(sym.name)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
86 f.addLocal(variable)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
87 elif isinstance(sym, ast.Variable):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
88 variable = ir.LocalVariable(sym.name)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
89 f.addLocal(variable)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
90 else:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
91 raise NotImplementedError('{}'.format(sym))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
92 self.varMap[sym] = variable
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
93
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
94 self.genCode(fn.body)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
95 self.emit(ir.Move(f.return_value, ir.Const(0)))
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
96 self.emit(ir.Jump(f.epiloog))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
97 self.setFunction(None)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
98
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 205
diff changeset
99 def genCode(self, code):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
100 """ Wrapper around gen_stmt to catch errors """
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
101 try:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
102 self.gen_stmt(code)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
103 except SemanticError as e:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
104 self.error(e.msg, e.loc)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
105
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
106 def gen_stmt(self, code):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
107 """ Generate code for a statement """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
108 assert isinstance(code, ast.Statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
109 self.setLoc(code.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
110 if type(code) is ast.Compound:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
111 for s in code.statements:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
112 self.genCode(s)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
113 elif type(code) is ast.Empty:
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
114 pass
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
115 elif type(code) is ast.Assignment:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
116 lval = self.gen_expr_code(code.lval)
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
117 rval = self.gen_expr_code(code.rval)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
118 if not self.equal_types(code.lval.typ, code.rval.typ):
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
119 raise SemanticError('Cannot assign {} to {}'
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
120 .format(code.rval.typ, code.lval.typ),
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
121 code.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
122 if not code.lval.lvalue:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
123 raise SemanticError('No valid lvalue {}'.format(code.lval),
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
124 code.lval.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
125 self.emit(ir.Move(lval, rval))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
126 elif type(code) is ast.ExpressionStatement:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
127 self.emit(ir.Exp(self.gen_expr_code(code.ex)))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
128 elif type(code) is ast.If:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
129 bbtrue = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
130 bbfalse = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
131 te = self.newBlock()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
132 self.gen_cond_code(code.condition, bbtrue, bbfalse)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
133 self.setBlock(bbtrue)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
134 self.genCode(code.truestatement)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
135 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
136 self.setBlock(bbfalse)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
137 self.genCode(code.falsestatement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
138 self.emit(ir.Jump(te))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
139 self.setBlock(te)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
140 elif type(code) is ast.Return:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
141 re = self.gen_expr_code(code.expr)
303
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
142 self.emit(ir.Move(self.fn.return_value, re))
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
143 self.emit(ir.Jump(self.fn.epiloog))
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
144 b = self.newBlock()
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
145 self.setBlock(b)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
146 elif type(code) is ast.While:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
147 bbdo = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
148 bbtest = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
149 te = self.newBlock()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
150 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
151 self.setBlock(bbtest)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
152 self.gen_cond_code(code.condition, bbdo, te)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
153 self.setBlock(bbdo)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
154 self.genCode(code.statement)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
155 self.emit(ir.Jump(bbtest))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
156 self.setBlock(te)
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
157 elif type(code) is ast.For:
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
158 bbdo = self.newBlock()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
159 bbtest = self.newBlock()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
160 te = self.newBlock()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
161 self.genCode(code.init)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
162 self.emit(ir.Jump(bbtest))
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
163 self.setBlock(bbtest)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
164 self.gen_cond_code(code.condition, bbdo, te)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
165 self.setBlock(bbdo)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
166 self.genCode(code.statement)
360
42343d189e14 Bugfix in for loop
Windel Bouwman
parents: 354
diff changeset
167 self.genCode(code.final)
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
168 self.emit(ir.Jump(bbtest))
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
169 self.setBlock(te)
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
170 elif type(code) is ast.Switch:
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
171 raise NotImplementedError('Unknown stmt {}'.format(code))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
172 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
173 raise NotImplementedError('Unknown stmt {}'.format(code))
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
174
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
175 def gen_cond_code(self, expr, bbtrue, bbfalse):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
176 """ Generate conditional logic.
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
177 Implement sequential logical operators. """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
178 if type(expr) is ast.Binop:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
179 if expr.op == 'or':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
180 l2 = self.newBlock()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
181 self.gen_cond_code(expr.a, bbtrue, l2)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
182 if not self.equal_types(expr.a.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
183 raise SemanticError('Must be boolean', expr.a.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
184 self.setBlock(l2)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
185 self.gen_cond_code(expr.b, bbtrue, bbfalse)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
186 if not self.equal_types(expr.b.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
187 raise SemanticError('Must be boolean', expr.b.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
188 elif expr.op == 'and':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
189 l2 = self.newBlock()
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
190 self.gen_cond_code(expr.a, l2, bbfalse)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
191 if not self.equal_types(expr.a.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
192 self.error('Must be boolean', expr.a.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
193 self.setBlock(l2)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
194 self.gen_cond_code(expr.b, bbtrue, bbfalse)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
195 if not self.equal_types(expr.b.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
196 raise SemanticError('Must be boolean', expr.b.loc)
305
0615b5308710 Updated docs
Windel Bouwman
parents: 303
diff changeset
197 elif expr.op in ['==', '>', '<', '!=', '<=', '>=']:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
198 ta = self.gen_expr_code(expr.a)
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
199 tb = self.gen_expr_code(expr.b)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
200 if not self.equal_types(expr.a.typ, expr.b.typ):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
201 raise SemanticError('Types unequal {} != {}'
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
202 .format(expr.a.typ, expr.b.typ),
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
203 expr.loc)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
204 self.emit(ir.CJump(ta, expr.op, tb, bbtrue, bbfalse))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
205 else:
311
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 308
diff changeset
206 raise SemanticError('non-bool: {}'.format(expr.op), expr.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
207 expr.typ = self.boolType
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
208 elif type(expr) is ast.Literal:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
209 self.gen_expr_code(expr)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
210 if expr.val:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
211 self.emit(ir.Jump(bbtrue))
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
212 else:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
213 self.emit(ir.Jump(bbfalse))
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
214 else:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
215 raise NotImplementedError('Unknown cond {}'.format(expr))
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
216
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
217 # Check that the condition is a boolean value:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
218 if not self.equal_types(expr.typ, self.boolType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
219 self.error('Condition must be boolean', expr.loc)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
220
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
221 def gen_expr_code(self, expr):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
222 """ Generate code for an expression. Return the generated ir-value """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
223 assert isinstance(expr, ast.Expression)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
224 if type(expr) is ast.Binop:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
225 expr.lvalue = False
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
226 if expr.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
227 ra = self.gen_expr_code(expr.a)
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
228 rb = self.gen_expr_code(expr.b)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
229 if self.equal_types(expr.a.typ, self.intType) and \
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
230 self.equal_types(expr.b.typ, self.intType):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
231 expr.typ = expr.a.typ
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
232 elif self.equal_types(expr.b.typ, self.intType) and \
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
233 type(expr.a.typ) is ast.PointerType:
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
234 # Special case for pointer arithmatic TODO: coerce!
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
235 expr.typ = expr.a.typ
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
236 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
237 raise SemanticError('Can only add integers', expr.loc)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
238 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
239 raise NotImplementedError("Cannot use equality as expressions")
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
240 return ir.Binop(ra, expr.op, rb)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
241 elif type(expr) is ast.Unop:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
242 if expr.op == '&':
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
243 ra = self.gen_expr_code(expr.a)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
244 expr.typ = ast.PointerType(expr.a.typ)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
245 if not expr.a.lvalue:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
246 raise SemanticError('No valid lvalue', expr.a.loc)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
247 expr.lvalue = False
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
248 assert type(ra) is ir.Mem
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
249 return ra.e
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
250 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
251 raise NotImplementedError('Unknown unop {0}'.format(expr.op))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
252 elif type(expr) is ast.Identifier:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
253 # Generate code for this identifier.
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
254 tg = self.resolveSymbol(expr)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
255 expr.kind = type(tg)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
256 expr.typ = tg.typ
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
257 # This returns the dereferenced variable.
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
258 if isinstance(tg, ast.Variable):
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
259 expr.lvalue = True
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
260 return ir.Mem(self.varMap[tg])
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
261 elif isinstance(tg, ast.Constant):
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
262 c_val = self.gen_expr_code(tg.value)
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
263 return self.evalConst(c_val)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
264 else:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
265 raise NotImplementedError(str(tg))
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
266 elif type(expr) is ast.Deref:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
267 # dereference pointer type:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
268 addr = self.gen_expr_code(expr.ptr)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
269 ptr_typ = self.the_type(expr.ptr.typ)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
270 expr.lvalue = True
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
271 if type(ptr_typ) is ast.PointerType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
272 expr.typ = ptr_typ.ptype
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
273 return ir.Mem(addr)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
274 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
275 raise SemanticError('Cannot deref non-pointer', expr.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
276 elif type(expr) is ast.Member:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
277 base = self.gen_expr_code(expr.base)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
278 expr.lvalue = expr.base.lvalue
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
279 basetype = self.the_type(expr.base.typ)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
280 if type(basetype) is ast.StructureType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
281 if basetype.hasField(expr.field):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
282 expr.typ = basetype.fieldType(expr.field)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
283 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
284 raise SemanticError('{} does not contain field {}'
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
285 .format(basetype, expr.field),
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
286 expr.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
287 else:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
288 raise SemanticError('Cannot select {} of non-structure type {}'
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
289 .format(expr.field, basetype), expr.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
290
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 275
diff changeset
291 assert type(base) is ir.Mem, type(base)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
292 bt = self.the_type(expr.base.typ)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 261
diff changeset
293 offset = ir.Const(bt.fieldOffset(expr.field))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
294 return ir.Mem(ir.Add(base.e, offset))
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
295 elif type(expr) is ast.Index:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
296 """ Array indexing """
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
297 base = self.gen_expr_code(expr.base)
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
298 idx = self.gen_expr_code(expr.i)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
299 base_typ = self.the_type(expr.base.typ)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
300 if not isinstance(base_typ, ast.ArrayType):
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
301 raise SemanticError('Cannot index non-array type {}'
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
302 .format(base_typ),
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
303 expr.base.loc)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
304 idx_type = self.the_type(expr.i.typ)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
305 if not self.equal_types(idx_type, self.intType):
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
306 raise SemanticError('Index must be int not {}'
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
307 .format(idx_type), expr.i.loc)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
308 assert type(base) is ir.Mem
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
309 element_type = self.the_type(base_typ.element_type)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
310 element_size = self.size_of(element_type)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
311 expr.typ = base_typ.element_type
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
312 expr.lvalue = True
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
313
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
314 return ir.Mem(ir.Add(base.e, ir.Mul(idx, ir.Const(element_size))))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
315 elif type(expr) is ast.Literal:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
316 expr.lvalue = False
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
317 typemap = {int: 'int',
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
318 float: 'double',
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
319 bool: 'bool',
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
320 str: 'string'}
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
321 if type(expr.val) in typemap:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
322 expr.typ = self.pkg.scope[typemap[type(expr.val)]]
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
323 else:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
324 raise SemanticError('Unknown literal type {}'
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
325 .format(expr.val), expr.loc)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
326 # Construct correct const value:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
327 if type(expr.val) is str:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
328 cval = self.pack_string(expr.val)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
329 return ir.Addr(ir.Const(cval))
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
330 else:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
331 return ir.Const(expr.val)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
332 elif type(expr) is ast.TypeCast:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
333 return self.gen_type_cast(expr)
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
334 elif type(expr) is ast.Sizeof:
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
335 # The type of this expression is int:
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
336 expr.typ = self.intType
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
337 self.check_type(expr.query_typ)
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
338 type_size = self.size_of(expr.query_typ)
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
339 return ir.Const(type_size)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
340 elif type(expr) is ast.FunctionCall:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
341 return self.gen_function_call(expr)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
342 else:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 255
diff changeset
343 raise NotImplementedError('Unknown expr {}'.format(expr))
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
344
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
345 def pack_string(self, txt):
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
346 """ Pack a string using 4 bytes length followed by text data """
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
347 length = struct.pack('<I', len(txt))
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
348 data = txt.encode('ascii')
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
349 return length + data
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
350
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
351 def gen_type_cast(self, expr):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
352 """ Generate code for type casting """
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
353 ar = self.gen_expr_code(expr.a)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
354 from_type = self.the_type(expr.a.typ)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
355 to_type = self.the_type(expr.to_type)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
356 if isinstance(from_type, ast.PointerType) and \
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
357 isinstance(to_type, ast.PointerType):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
358 expr.typ = expr.to_type
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
359 return ar
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
360 elif self.equal_types(self.intType, from_type) and \
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
361 isinstance(to_type, ast.PointerType):
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
362 expr.typ = expr.to_type
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
363 return ar
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
364 elif self.equal_types(self.intType, to_type) \
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
365 and isinstance(from_type, ast.PointerType):
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
366 expr.typ = expr.to_type
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
367 return ar
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
368 elif type(from_type) is ast.BaseType and from_type.name == 'byte' and \
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
369 type(to_type) is ast.BaseType and to_type.name == 'int':
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
370 expr.typ = expr.to_type
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
371 return ar
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
372 else:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
373 raise SemanticError('Cannot cast {} to {}'
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
374 .format(from_type, to_type), expr.loc)
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 336
diff changeset
375
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
376 def gen_function_call(self, expr):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
377 """ Generate code for a function call """
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
378 # Evaluate the arguments:
393
6ae782a085e0 Added init program
Windel Bouwman
parents: 389
diff changeset
379 args = [self.gen_expr_code(e) for e in expr.args]
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
380 # Check arguments:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
381 tg = self.resolveSymbol(expr.proc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
382 if type(tg) is not ast.Function:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
383 raise SemanticError('cannot call {}'.format(tg))
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
384 ftyp = tg.typ
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 334
diff changeset
385 fname = tg.package.name + '_' + tg.name
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
386 ptypes = ftyp.parametertypes
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
387 if len(expr.args) != len(ptypes):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
388 raise SemanticError('{} requires {} arguments, {} given'
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
389 .format(fname, len(ptypes), len(expr.args)),
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
390 expr.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
391 for arg, at in zip(expr.args, ptypes):
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
392 if not self.equal_types(arg.typ, at):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
393 raise SemanticError('Got {}, expected {}'
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
394 .format(arg.typ, at), arg.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
395 # determine return type:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
396 expr.typ = ftyp.returntype
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
397 return ir.Call(fname, args)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
398
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
399 def evalConst(self, c):
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
400 if isinstance(c, ir.Const):
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
401 return c
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
402 else:
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
403 raise SemanticError('Cannot evaluate constant {}'.format(c))
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 312
diff changeset
404
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
405 def resolveSymbol(self, sym):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
406 if type(sym) is ast.Member:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
407 base = self.resolveSymbol(sym.base)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
408 if type(base) is not ast.Package:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
409 raise SemanticError('Base is not a package', sym.loc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
410 scope = base.innerScope
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
411 name = sym.field
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
412 elif type(sym) is ast.Identifier:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
413 scope = sym.scope
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
414 name = sym.target
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
415 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
416 raise NotImplementedError(str(sym))
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
417 if name in scope:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
418 s = scope[name]
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
419 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
420 raise SemanticError('{} undefined'.format(name), sym.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
421 assert isinstance(s, ast.Symbol)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
422 return s
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
423
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
424 def size_of(self, t):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
425 """ Determine the byte size of a type """
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
426 t = self.the_type(t)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
427 if type(t) is ast.BaseType:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
428 return t.bytesize
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
429 elif type(t) is ast.StructureType:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
430 return sum(self.size_of(mem.typ) for mem in t.mems)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
431 elif type(t) is ast.ArrayType:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
432 return t.size * self.size_of(t.element_type)
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
433 elif type(t) is ast.PointerType:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
434 return self.pointerSize
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
435 else:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
436 raise NotImplementedError(str(t))
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
437
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
438 def the_type(self, t, reveil_defined=True):
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
439 """ Recurse until a 'real' type is found
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
440 When reveil_defined is True, defined types are resolved to
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
441 their backing types.
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
442 """
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
443 if type(t) is ast.DefinedType:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
444 if reveil_defined:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
445 t = self.the_type(t.typ)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
446 elif type(t) in [ast.Identifier, ast.Member]:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
447 t = self.the_type(self.resolveSymbol(t), reveil_defined)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
448 elif isinstance(t, ast.Type):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
449 pass
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
450 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
451 raise NotImplementedError(str(t))
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
452 assert isinstance(t, ast.Type)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
453 return t
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
454
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
455 def equal_types(self, a, b, byname=False):
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
456 """ Compare types a and b for structural equavalence.
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
457 if byname is True stop on defined types.
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
458 """
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
459 # Recurse into named types:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
460 a = self.the_type(a, not byname)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
461 b = self.the_type(b, not byname)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
462
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
463 # Check types for sanity:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
464 self.check_type(a)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
465 self.check_type(b)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
466
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
467 # Do structural equivalence check:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
468 if type(a) is type(b):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
469 if type(a) is ast.BaseType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
470 return a.name == b.name
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
471 elif type(a) is ast.PointerType:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
472 # If a pointed type is detected, stop structural
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
473 # equivalence:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
474 return self.equal_types(a.ptype, b.ptype, byname=True)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
475 elif type(a) is ast.StructureType:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
476 if len(a.mems) != len(b.mems):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
477 return False
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
478 return all(self.equal_types(am.typ, bm.typ) for am, bm in
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
479 zip(a.mems, b.mems))
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
480 elif type(a) is ast.ArrayType:
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
481 return self.equal_types(a.element_type, b.element_type)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
482 elif type(a) is ast.DefinedType:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
483 # Try by name in case of defined types:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
484 return a.name == b.name
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
485 else:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
486 raise NotImplementedError('{} not implemented'.format(type(a)))
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
487 return False
389
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
488
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
489 def check_type(self, t, first=True, byname=False):
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
490 """ Determine struct offsets and check for recursiveness by using
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
491 mark and sweep algorithm.
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
492 The calling function could call this function with first set
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
493 to clear the marks.
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
494 """
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
495
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
496 # Reset the mark and sweep:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
497 if first:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
498 self.got_types = set()
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
499
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
500 # Resolve the type:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
501 t = self.the_type(t, not byname)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
502
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
503 # Check for recursion:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
504 if t in self.got_types:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
505 raise SemanticError('Recursive data type {}'.format(t), None)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
506
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
507 if type(t) is ast.BaseType:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
508 pass
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
509 elif type(t) is ast.PointerType:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
510 # If a pointed type is detected, stop structural
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
511 # equivalence:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
512 self.check_type(t.ptype, first=False, byname=True)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
513 elif type(t) is ast.StructureType:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
514 self.got_types.add(t)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
515 # Setup offsets of fields. Is this the right place?:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
516 offset = 0
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
517 for struct_member in t.mems:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
518 self.check_type(struct_member.typ, first=False)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
519 struct_member.offset = offset
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
520 offset = offset + self.size_of(struct_member.typ)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
521 elif type(t) is ast.ArrayType:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
522 self.check_type(t.element_type, first=False)
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
523 elif type(t) is ast.DefinedType:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
524 pass
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
525 else:
2ec730e45ea1 Added check for recursive struct
Windel Bouwman
parents: 364
diff changeset
526 raise NotImplementedError('{} not implemented'.format(type(t)))