annotate python/ppci/c3/codegenerator.py @ 307:e609d5296ee9

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