-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_ast.py
336 lines (254 loc) · 8.12 KB
/
c_ast.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- coding: utf-8 -*-
import sys
import logging
class ASTNode(object):
attr_names = ()
def __init__(self):
self.node_name = "ASTNode"
def show(self, buf=sys.stdout, offset=0):
buf.write(' '*offset + self.__class__.__name__+ ': ')
if self.attr_names:
nvlist = [(n, getattr(self,n)) for n in self.attr_names]
attrstr = ', '.join('%s=%s' % nv for nv in nvlist)
buf.write(attrstr)
buf.write('\n')
for child in self.children():
child.show(offset = offset + 2)
def children(self):
raise NotImplementedError
class AST(ASTNode):
def __init__(self, trans_unit):
self.l = [trans_unit]
def children(self):
return self.l
class ArgumentList(ASTNode):
def __init__(self, argument=None):
if argument is None:
self.l = []
elif type(argument) in frozenset([VariableSymbol, Const]):
self.l = [argument]
else:
logging.error('Initial with error type')
def children(self):
return self.l
class FuncDecl(ASTNode):
attr_names = ('return_type', 'storage')
def __init__(self, return_type, function_name, param_list, storage='extern'):
super(FuncDecl, self).__init__()
self.storage = storage
self.return_type = return_type
self.function_name = function_name
self.param_list = param_list
def children(self):
return [self.function_name, self.param_list]
class FuncDef(ASTNode):
attr_names = ('return_type', 'storage')
def __init__(self, return_type, function_name, param_list, body, storage='extern'):
self.node_name = "FuncDef"
super(FuncDef, self).__init__()
self.return_type = return_type
self.function_name = function_name
self.param_list = param_list
self.body = body
self.storage = storage
def children(self):
return [self.function_name, self.param_list, self.body]
class DeclarationList(ASTNode):
decl_types = ["TypeDecl", "ArrayDecl", "FuncDecl"]
def __init__(self, decl=None):
self.node_name = "DeclarationList"
if decl is None:
self.l = []
elif decl.__class__.__name__ in self.decl_types:
self.l = [decl]
else:
logging.error('Initial with error type,decl: {}'.format(decl))
def add_declaration(self, d):
self.l.append(d)
def __add__(self, rhs):
if rhs.__class__.__name__ in self.decl_types:
self.add_declaration(rhs)
elif type(rhs) is DeclarationList:
self.l += rhs.l
return self
def children(self):
return self.l
class StmtList(ASTNode):
def __init__(self, stmt=None):
self.node_name = "StmtList"
if stmt is None:
self.l = []
elif issubclass(stmt.__class__, Statement):
self.l = [stmt]
else:
logging.error('Initial with error type: {0}'.format(stmt.__class__))
def add_stmt(self, s):
self.l.append(s)
def __add__(self, rhs):
if issubclass(rhs.__class__, Statement):
self.add_stmt(rhs)
elif type(rhs) is StmtList:
self.l += rhs.l
return self
def children(self):
return self.l
# Decl:
# name: the variable being declared
# quals: list of qualifiers (const, volatile)
# funcspec: list function specifiers (i.e. inline in C99)
# storage: list of storage specifiers (static, auto, extern, register, etc.)
# type: declaration type (probably nested with all the modifiers)
# init: initialization value, or None
# bitsize: bit field size, or None
class TypeDecl(ASTNode):
'''Declaration: storage type name init
'''
attr_names = ('_type', 'storage')
def __init__(self, _type, _id, init=None, storage='auto'):
self.storage = storage
self._id = _id
self._type = _type
self.init = init
def children(self):
if self.init:
return [self._id, self.init]
return [self._id]
def __add__(self, rhs):
decls = DeclarationList(self)
return decls + rhs
class ArrayDecl(ASTNode):
attr_names = ('_type', 'length')
def __init__(self, _type, _id, length, init=None, storage='auto'):
self.storage = storage
self._id = _id
self._type = _type
self.length = length
self.init = init
def children(self):
if self.init:
return [self._id, self.init]
return [self._id]
def __add__(self, rhs):
decls = DeclarationList(self)
return decls + rhs
class Statement(ASTNode):
def __init__(self):
pass
def __add__(self, rhs):
stmts = StmtList(self)
return stmts + rhs
def children(self):
return []
class DeclStmt(Statement):
''' DeclStmt: Decl SEIM '''
def __init__(self, decl):
self.decl = decl
def children(self):
return [self.decl]
class FuncCall(Statement):
def __init__(self, func_name, argument_list):
self.func_name = func_name
self.argument_list = argument_list
def children(self):
return [self.func_name, self.argument_list]
class IfStmt(Statement):
def __init__(self, cond, then, _else=None):
self.cond = cond
self.then = then
self._else = _else
def children(self):
if self._else is None:
return [self.cond, self.then]
return [self.cond, self.then, self._else]
class WhileStmt(Statement):
def __init__(self, expr, body):
self.cond_expr = expr
self.body = body
self.node_name = "ForStat"
def children(self):
return [self.cond_expr, self.body]
class ReturnStmt(Statement):
def __init__(self, expr=None):
self.expr = expr
def children(self):
if self.expr is None:
return []
return [self.expr]
class Assignment(Statement):
def __init__(self, cast_expr, rhs):
self.cast_expr = cast_expr
self.rhs = rhs
def children(self):
return [self.cast_expr, self.rhs]
class BinaryOp(ASTNode):
attr_names = ('op',)
def __init__(self, lhs, op , rhs):
self.node_name = "BinaryOp"
self.lhs = lhs
self.op = op
self.rhs = rhs
def is_logicalOp(self):
return self.op == '&&' or self.op == "||"
def is_compareOp(self):
return self.op in [">", "<", ">=", "<=", "==", "!="]
def children(self):
return [self.lhs, self.rhs]
class UnaryOp(ASTNode):
attr_names = ('op',)
def __init__(self, op, expr):
self.node_name = "UnaryOp"
self.op = op
self.expr = expr
def children(self):
return [self.expr]
class BreakStmt(Statement):
pass
class ContinueStmt(Statement):
pass
class Symbol(ASTNode):
attr_names = ('name', )
def __init__(self, name):
self.node_name = "Symbol"
self.name = name
self._type = 0
def children(self):
return []
class MethodSymbol(Symbol):
attr_names = ('name',)
def __init__(self, name):
super(MethodSymbol, self).__init__(name)
self.node_name = "MethodSymbol"
class VariableSymbol(Symbol):
attr_names = ('name',)
def __init__(self, name):
super(VariableSymbol, self).__init__(name)
self.node_name = "VariableSymbol"
class Const(ASTNode):
attr_names = ('_type', 'val',)
def __init__(self, _type, val):
self.node_name = "const"
self.val = val
self._type = _type
def children(self):
return []
class Label(ASTNode):
attr_names = ('name', )
def __init__(self, _id):
self._id = _id
self.name = 'L' + str(_id)
def children(self):
return []
class ABSJMP(ASTNode):
attr_names = ('_id', )
def __init__(self, _id):
self._id = _id
def children(self):
return []
class CMPJMP(ASTNode):
attr_names = ('id1', 'id2')
def __init__(self, expr, id1, id2):
self.expr = expr
self.id1 = id1
self.id2 = id2
def children(self):
return [self.expr]