Mercurial > lcfOS
annotate python/ppci/c3/analyse.py @ 301:6753763d3bec
merge codegen into ppci package
author | Windel Bouwman |
---|---|
date | Thu, 05 Dec 2013 17:02:38 +0100 |
parents | 158068af716c |
children | 0615b5308710 |
rev | line source |
---|---|
255 | 1 import logging |
163 | 2 from .visitor import Visitor |
3 from .astnodes import * | |
287 | 4 from .scope import * |
5 | |
150 | 6 |
288 | 7 class C3Pass: |
215 | 8 def __init__(self, diag): |
9 self.diag = diag | |
255 | 10 self.logger = logging.getLogger('c3') |
186 | 11 self.ok = True |
288 | 12 self.visitor = Visitor() |
215 | 13 |
14 def error(self, msg, loc=None): | |
15 self.ok = False | |
16 self.diag.error(msg, loc) | |
17 | |
288 | 18 def visit(self, pkg, pre, post): |
19 self.visitor.visit(pkg, pre, post) | |
20 | |
21 | |
22 class AddScope(C3Pass): | |
23 """ Scope is attached to the correct modules. """ | |
24 def addScope(self, pkg): | |
25 self.logger.info('Adding scoping to package {}'.format(pkg.name)) | |
26 # Prepare top level scope and set scope to all objects: | |
27 self.scopeStack = [topScope] | |
28 modScope = Scope(self.CurrentScope) | |
29 self.scopeStack.append(modScope) | |
30 self.visit(pkg, self.enterScope, self.quitScope) | |
31 assert len(self.scopeStack) == 2 | |
32 return self.ok | |
33 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
34 @property |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
35 def CurrentScope(self): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
36 return self.scopeStack[-1] |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
37 |
215 | 38 def addSymbol(self, sym): |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
39 if self.CurrentScope.hasSymbol(sym.name): |
215 | 40 self.error('Redefinition of {0}'.format(sym.name), sym.loc) |
41 else: | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
42 self.CurrentScope.addSymbol(sym) |
215 | 43 |
44 def enterScope(self, sym): | |
45 # Distribute the scope: | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
46 sym.scope = self.CurrentScope |
215 | 47 |
48 # Add symbols to current scope: | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
49 if isinstance(sym, Symbol) or isinstance(sym, DefinedType): |
225 | 50 self.addSymbol(sym) |
215 | 51 |
52 # Create subscope: | |
53 if type(sym) in [Package, Function]: | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
54 newScope = Scope(self.CurrentScope) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
55 self.scopeStack.append(newScope) |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
56 sym.innerScope = self.CurrentScope |
215 | 57 |
58 def quitScope(self, sym): | |
59 # Pop out of scope: | |
60 if type(sym) in [Package, Function]: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
61 self.scopeStack.pop(-1) |
150 | 62 |
288 | 63 |
64 class Analyzer(C3Pass): | |
65 """ | |
66 Context handling is done here. | |
67 Scope is attached to the correct modules. | |
68 This class checks names and references. | |
69 """ | |
70 | |
71 def analyzePackage(self, pkg, packageDict): | |
72 self.ok = True | |
73 # Prepare top level scope and set scope to all objects: | |
74 | |
75 self.logger.info('Resolving imports for package {}'.format(pkg.name)) | |
76 # Handle imports: | |
77 for i in pkg.imports: | |
293 | 78 if i not in packageDict: |
288 | 79 self.error('Cannot import {}'.format(i)) |
80 continue | |
293 | 81 ip = packageDict[i] |
288 | 82 pkg.scope.addSymbol(ip) |
301 | 83 fr = FixRefs(self.diag) |
84 fr.fixRefs(pkg) | |
85 return self.ok and fr.ok | |
288 | 86 |
87 | |
88 class FixRefs(C3Pass): | |
89 def fixRefs(self, pkg): | |
301 | 90 self.ok = True |
300 | 91 self.logger.info('Resolving references for {}'.format(pkg.name)) |
288 | 92 self.visitor.visit(pkg, self.findRefs) |
93 | |
215 | 94 # Reference fixups: |
95 def resolveDesignator(self, d, scope): | |
289 | 96 assert isinstance(d, Designator), type(d) |
97 assert type(scope) is Scope | |
98 if scope.hasSymbol(d.tname): | |
99 s = scope.getSymbol(d.tname) | |
100 if isinstance(d, ImportDesignator): | |
101 if s.innerScope.hasSymbol(d.vname): | |
102 return s.innerScope.getSymbol(d.vname) | |
103 else: | |
300 | 104 s.addRef(None) # TODO: fix this? |
289 | 105 return s |
300 | 106 self.error('Cannot resolve name {0}'.format(d.tname), d.loc) |
215 | 107 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
108 def resolveType(self, t, scope): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
109 if type(t) is PointerType: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
110 t.ptype = self.resolveType(t.ptype, scope) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
111 return t |
226 | 112 elif type(t) is StructureType: |
231 | 113 offset = 0 |
226 | 114 for mem in t.mems: |
231 | 115 mem.offset = offset |
226 | 116 mem.typ = self.resolveType(mem.typ, scope) |
231 | 117 offset += theType(mem.typ).bytesize |
118 t.bytesize = offset | |
226 | 119 return t |
289 | 120 elif isinstance(t, Designator): |
226 | 121 t = self.resolveDesignator(t, scope) |
249 | 122 if t: |
123 return self.resolveType(t, scope) | |
225 | 124 elif isinstance(t, Type): |
125 # Already resolved?? | |
126 return t | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
127 else: |
225 | 128 raise Exception('Error resolving type {} {}'.format(t, type(t))) |
231 | 129 |
215 | 130 def findRefs(self, sym): |
300 | 131 if type(sym) is Constant or isinstance(sym, Variable): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
132 sym.typ = self.resolveType(sym.typ, sym.scope) |
222 | 133 elif type(sym) is TypeCast: |
134 sym.to_type = self.resolveType(sym.to_type, sym.scope) | |
215 | 135 elif type(sym) is VariableUse: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
136 sym.target = self.resolveDesignator(sym.target, sym.scope) |
215 | 137 elif type(sym) is FunctionCall: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
138 varuse = sym.proc |
300 | 139 print(varuse) |
140 if type(varuse) is VariableUse: | |
141 sym.proc = self.resolveDesignator(varuse.target, sym.scope) | |
215 | 142 elif type(sym) is Function: |
143 # Checkup function type: | |
144 ft = sym.typ | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
145 ft.returntype = self.resolveType(ft.returntype, sym.scope) |
288 | 146 ft.parametertypes = [self.resolveType(pt, sym.scope) for pt in |
147 ft.parametertypes] | |
275 | 148 # Mark local variables: |
149 for d in sym.declarations: | |
150 if isinstance(d, Variable): | |
151 d.isLocal = True | |
226 | 152 elif type(sym) is DefinedType: |
153 sym.typ = self.resolveType(sym.typ, sym.scope) | |
215 | 154 |
288 | 155 |
287 | 156 # Type checking: |
157 | |
158 def theType(t): | |
288 | 159 """ Recurse until a 'real' type is found """ |
287 | 160 if type(t) is DefinedType: |
161 return theType(t.typ) | |
162 return t | |
163 | |
288 | 164 |
287 | 165 def equalTypes(a, b): |
288 | 166 """ Compare types a and b for structural equavalence. """ |
287 | 167 # Recurse into named types: |
288 | 168 a, b = theType(a), theType(b) |
287 | 169 |
170 if type(a) is type(b): | |
171 if type(a) is BaseType: | |
172 return a.name == b.name | |
173 elif type(a) is PointerType: | |
174 return equalTypes(a.ptype, b.ptype) | |
175 elif type(a) is StructureType: | |
176 if len(a.mems) != len(b.mems): | |
177 return False | |
288 | 178 return all(equalTypes(am.typ, bm.typ) for am, bm in |
179 zip(a.mems, b.mems)) | |
287 | 180 else: |
288 | 181 raise NotImplementedError( |
182 'Type compare for {} not implemented'.format(type(a))) | |
287 | 183 return False |
184 | |
288 | 185 |
287 | 186 def canCast(fromT, toT): |
187 fromT = theType(fromT) | |
188 toT = theType(toT) | |
189 if isinstance(fromT, PointerType) and isinstance(toT, PointerType): | |
190 return True | |
191 elif fromT is intType and isinstance(toT, PointerType): | |
192 return True | |
193 return False | |
194 | |
288 | 195 |
287 | 196 def expectRval(s): |
197 # TODO: solve this better | |
198 s.expect_rvalue = True | |
199 | |
200 | |
288 | 201 class TypeChecker(C3Pass): |
287 | 202 def checkPackage(self, pkg): |
300 | 203 self.logger.info('Type checking {}'.format(pkg.name)) |
287 | 204 self.ok = True |
288 | 205 self.visit(pkg, None, self.check2) |
287 | 206 return self.ok |
207 | |
208 def check2(self, sym): | |
209 if type(sym) in [IfStatement, WhileStatement]: | |
210 if not equalTypes(sym.condition.typ, boolType): | |
211 msg = 'Condition must be of type {}'.format(boolType) | |
212 self.error(msg, sym.condition.loc) | |
213 elif type(sym) is Assignment: | |
214 l, r = sym.lval, sym.rval | |
215 if not equalTypes(l.typ, r.typ): | |
216 msg = 'Cannot assign {} to {}'.format(r.typ, l.typ) | |
217 self.error(msg, sym.loc) | |
218 if not l.lvalue: | |
219 self.error('No valid lvalue {}'.format(l), l.loc) | |
220 #if sym.rval.lvalue: | |
221 # self.error('Right hand side must be an rvalue', sym.rval.loc) | |
222 expectRval(sym.rval) | |
223 elif type(sym) is ReturnStatement: | |
215 | 224 pass |
287 | 225 elif type(sym) is FunctionCall: |
226 # Check arguments: | |
227 ngiv = len(sym.args) | |
228 ptypes = sym.proc.typ.parametertypes | |
229 nreq = len(ptypes) | |
230 if ngiv != nreq: | |
300 | 231 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) |
287 | 232 else: |
300 | 233 for a, at in zip(sym.args, ptypes): |
234 expectRval(a) | |
235 if not equalTypes(a.typ, at): | |
236 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) | |
287 | 237 # determine return type: |
238 sym.typ = sym.proc.typ.returntype | |
239 elif type(sym) is VariableUse: | |
240 sym.lvalue = True | |
301 | 241 assert isinstance(sym.target, Variable), sym.target |
242 sym.typ = sym.target.typ | |
287 | 243 elif type(sym) is Literal: |
244 sym.lvalue = False | |
245 if type(sym.val) is int: | |
246 sym.typ = intType | |
247 elif type(sym.val) is float: | |
248 sym.typ = doubleType | |
249 elif type(sym.val) is bool: | |
250 sym.typ = boolType | |
251 else: | |
252 raise Exception('Unknown literal type'.format(sym.val)) | |
253 elif type(sym) is Unop: | |
254 if sym.op == '&': | |
255 sym.typ = PointerType(sym.a.typ) | |
256 sym.lvalue = False | |
257 else: | |
258 raise Exception('Unknown unop {0}'.format(sym.op)) | |
259 elif type(sym) is Deref: | |
260 # pointer deref | |
261 sym.lvalue = True | |
262 # check if the to be dereferenced variable is a pointer type: | |
263 ptype = theType(sym.ptr.typ) | |
264 if type(ptype) is PointerType: | |
265 sym.typ = ptype.ptype | |
266 else: | |
267 self.error('Cannot dereference non-pointer type {}'.format(ptype), sym.loc) | |
268 sym.typ = intType | |
269 elif type(sym) is FieldRef: | |
270 basetype = sym.base.typ | |
271 sym.lvalue = sym.base.lvalue | |
272 basetype = theType(basetype) | |
273 if type(basetype) is StructureType: | |
274 if basetype.hasField(sym.field): | |
275 sym.typ = basetype.fieldType(sym.field) | |
276 else: | |
277 self.error('{} does not contain field {}'.format(basetype, sym.field), sym.loc) | |
278 sym.typ = intType | |
279 else: | |
280 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc) | |
281 sym.typ = intType | |
282 elif type(sym) is Binop: | |
283 sym.lvalue = False | |
284 if sym.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']: | |
285 expectRval(sym.a) | |
286 expectRval(sym.b) | |
287 if equalTypes(sym.a.typ, sym.b.typ): | |
300 | 288 if equalTypes(sym.a.typ, intType): |
289 sym.typ = sym.a.typ | |
290 else: | |
291 self.error('Can only add integers', sym.loc) | |
292 sym.typ = intType | |
287 | 293 else: |
300 | 294 # assume void here? TODO: throw exception! |
295 sym.typ = intType | |
296 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) | |
297 elif sym.op in ['>', '<', '==', '<=', '>=', '!=']: | |
287 | 298 expectRval(sym.a) |
299 expectRval(sym.b) | |
300 sym.typ = boolType | |
301 if not equalTypes(sym.a.typ, sym.b.typ): | |
300 | 302 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) |
287 | 303 elif sym.op in ['or', 'and']: |
304 sym.typ = boolType | |
305 if not equalTypes(sym.a.typ, boolType): | |
300 | 306 self.error('Must be {0}'.format(boolType), sym.a.loc) |
287 | 307 if not equalTypes(sym.b.typ, boolType): |
300 | 308 self.error('Must be {0}'.format(boolType), sym.b.loc) |
287 | 309 else: |
310 raise Exception('Unknown binop {0}'.format(sym.op)) | |
311 elif isinstance(sym, Variable): | |
312 # check initial value type: | |
313 # TODO | |
215 | 314 pass |
287 | 315 elif type(sym) is TypeCast: |
316 if canCast(sym.a.typ, sym.to_type): | |
317 sym.typ = sym.to_type | |
318 else: | |
319 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type), sym.loc) | |
320 sym.typ = intType | |
321 elif type(sym) is Constant: | |
322 if not equalTypes(sym.typ, sym.value.typ): | |
323 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) | |
324 elif type(sym) in [CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]: | |
325 pass | |
326 else: | |
327 raise NotImplementedError('Unknown type check {0}'.format(sym)) |