annotate python/ppci/c3/visitor.py @ 362:c05ab629976a

Added CPUID for arm
author Windel Bouwman
date Sat, 15 Mar 2014 10:56:34 +0100
parents 5477e499b039
children 6ae782a085e0
rev   line source
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
1 from .astnodes import *
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
2
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
3
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
4 class Visitor:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
5 """
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
6 Visitor that can visit all nodes in the AST
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
7 and run pre and post functions.
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
8 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
9 def visit(self, node, f_pre=None, f_post=None):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
10 self.f_pre = f_pre
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
11 self.f_post = f_post
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
12 self.do(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
13
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
14 def do(self, node):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
15 # Run pre function:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
16 if self.f_pre:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
17 self.f_pre(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
18
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
19 # Descent into subnodes:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
20 if type(node) is Package:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
21 for decl in node.declarations:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
22 self.do(decl)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
23 elif type(node) is Function:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
24 for s in node.declarations:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
25 self.do(s)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
26 self.do(node.typ)
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
27 if node.body:
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
28 self.do(node.body)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
29 elif type(node) is Compound:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
30 for s in node.statements:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
31 self.do(s)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
32 elif type(node) is If:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
33 self.do(node.condition)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
34 self.do(node.truestatement)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
35 self.do(node.falsestatement)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
36 elif type(node) is While:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
37 self.do(node.condition)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
38 self.do(node.statement)
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
39 elif type(node) is For:
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
40 self.do(node.init)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
41 self.do(node.condition)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
42 self.do(node.final)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
43 self.do(node.statement)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
44 elif type(node) is Assignment:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
45 self.do(node.lval)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
46 self.do(node.rval)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
47 elif type(node) is FunctionCall:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
48 for arg in node.args:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
49 self.do(arg)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
50 self.do(node.proc)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
51 elif type(node) is Return:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
52 self.do(node.expr)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
53 elif type(node) is Binop:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
54 self.do(node.a)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
55 self.do(node.b)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
56 elif type(node) is Unop:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
57 self.do(node.a)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
58 elif type(node) is ExpressionStatement:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
59 self.do(node.ex)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
60 elif type(node) is TypeCast:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
61 self.do(node.a)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
62 self.do(node.to_type)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
63 elif type(node) is Member:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
64 self.do(node.base)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 315
diff changeset
65 elif type(node) is Index:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 315
diff changeset
66 self.do(node.base)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 315
diff changeset
67 self.do(node.i)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
68 elif type(node) is Deref:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
69 self.do(node.ptr)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
70 elif type(node) is Constant:
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 307
diff changeset
71 self.do(node.typ)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
72 self.do(node.value)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
73 elif type(node) is DefinedType:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
74 self.do(node.typ)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
75 elif isinstance(node, Variable):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
76 self.do(node.typ)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
77 elif type(node) is PointerType:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
78 self.do(node.ptype)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
79 elif type(node) is StructureType:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
80 for m in node.mems:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
81 self.do(m.typ)
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 315
diff changeset
82 elif type(node) is ArrayType:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 315
diff changeset
83 self.do(node.element_type)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 315
diff changeset
84 self.do(node.size)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
85 elif type(node) is FunctionType:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
86 for pt in node.parametertypes:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
87 self.do(pt)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
88 self.do(node.returntype)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
89 elif type(node) in [Identifier, Literal, Empty]:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
90 # Those nodes do not have child nodes.
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
91 pass
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
92 else:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
93 raise Exception('Could not visit "{0}"'.format(node))
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
94
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
95 # run post function
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
96 if self.f_post:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
97 self.f_post(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
98
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
99
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
100 class AstPrinter:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
101 """ Prints an AST as text """
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 307
diff changeset
102 def printAst(self, pkg, f):
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 307
diff changeset
103 self.indent = 2
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 307
diff changeset
104 self.f = f
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
105 visitor = Visitor()
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
106 visitor.visit(pkg, self.print1, self.print2)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
107
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
108 def print1(self, node):
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 307
diff changeset
109 print(' ' * self.indent + str(node), file=self.f)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
110 self.indent += 2
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
111
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
112 def print2(self, node):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
113 self.indent -= 2