-
Notifications
You must be signed in to change notification settings - Fork 4
/
irvisitor.py
183 lines (152 loc) · 5.31 KB
/
irvisitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import ir
ir.generate_asdl_file()
import _asdl.loma as loma_ir
class IRVisitor:
""" Visitor pattern: we use IRVisitor to traverse loma IR code,
and visit its children.
To use this class, you should inherit IRVisitor, and define
your own visit functions to decide what to do.
By default the class does nothing to the IR code.
"""
def visit_function(self, node):
match node:
case loma_ir.FunctionDef():
self.visit_function_def(node)
case loma_ir.ForwardDiff():
self.visit_forward_diff(node)
case loma_ir.ReverseDiff():
self.visit_reverse_diff(node)
case _:
assert False, f'Visitor error: unhandled func {node}'
def visit_function_def(self, node):
for stmt in node.body:
self.visit_stmt(stmt)
def visit_forward_diff(self, node):
pass
def visit_reverse_diff(self, node):
pass
def visit_stmt(self, node):
match node:
case loma_ir.Return():
self.visit_return(node)
case loma_ir.Declare():
self.visit_declare(node)
case loma_ir.Assign():
self.visit_assign(node)
case loma_ir.IfElse():
self.visit_ifelse(node)
case loma_ir.While():
self.visit_while(node)
case loma_ir.CallStmt():
self.visit_call_stmt(node)
case _:
assert False, f'Visitor error: unhandled statement {node}'
def visit_return(self, node):
self.visit_expr(node.val)
def visit_declare(self, node):
if node.val is not None:
self.visit_expr(node.val)
def visit_assign(self, node):
self.visit_expr(node.val)
def visit_ifelse(self, node):
self.visit_expr(node.cond)
for stmt in node.then_stmts:
self.visit_stmt(stmt)
for stmt in node.else_stmts:
self.visit_stmt(stmt)
def visit_while(self, node):
self.visit_expr(node.cond)
for stmt in node.body:
self.visit_stmt(stmt)
def visit_call_stmt(self, node):
self.visit_expr(node.call)
def visit_expr(self, node):
match node:
case loma_ir.Var():
self.visit_var(node)
case loma_ir.ArrayAccess():
self.visit_array_access(node)
case loma_ir.StructAccess():
self.visit_struct_access(node)
case loma_ir.ConstFloat():
self.visit_const_float(node)
case loma_ir.ConstInt():
self.visit_const_int(node)
case loma_ir.BinaryOp():
self.visit_binary_op(node)
case loma_ir.Call():
self.visit_call(node)
case _:
assert False, f'Visitor error: unhandled expression {node}'
def visit_var(self, node):
pass
def visit_array_access(self, node):
self.visit_expr(node.array)
self.visit_expr(node.index)
def visit_struct_access(self, node):
self.visit_expr(node.struct)
pass
def visit_const_float(self, node):
pass
def visit_const_int(self, node):
pass
def visit_binary_op(self, node):
match node.op:
case loma_ir.Add():
self.visit_add(node)
case loma_ir.Sub():
self.visit_sub(node)
case loma_ir.Mul():
self.visit_mul(node)
case loma_ir.Div():
self.visit_div(node)
case loma_ir.Less():
self.visit_less(node)
case loma_ir.LessEqual():
self.visit_less_equal(node)
case loma_ir.Greater():
self.visit_greater(node)
case loma_ir.GreaterEqual():
self.visit_greater_equal(node)
case loma_ir.Equal():
self.visit_equal(node)
case loma_ir.And():
self.visit_and(node)
case loma_ir.Or():
self.visit_or(node)
def visit_add(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_sub(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_mul(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_div(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_less(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_less_equal(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_greater(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_greater_equal(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_equal(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_and(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_or(self, node):
self.visit_expr(node.left)
self.visit_expr(node.right)
def visit_call(self, node):
for arg in node.args:
self.visit_expr(arg)