-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatch.py
337 lines (301 loc) · 11.3 KB
/
match.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
import sys
import enum
from tokenizer import Tokenizer, Token, SymbolToken, KeywordToken, WhiteSpaceToken, IdentifierToken
from fpc_logging import verbose, logMessage
#--------------------------------------------------------------------#
# Types #
#--------------------------------------------------------------------#
class FunctionType(enum.Enum):
host = 0
device = 1
host_device = 2
device_host = 3
#--------------------------------------------------------------------#
# Match #
#--------------------------------------------------------------------#
class Match:
def __init__(self):
# Ranges of code blocks that have been matched: (b, e)
# b: block begining, e: block end
self.code_range_cache = []
def _matched_block(self, blockRange: tuple) -> bool:
if len(self.code_range_cache) == 0:
self.code_range_cache.append(blockRange)
return False
x, y = blockRange
last_x, last_y = self.code_range_cache[-1:][0]
if x >= last_x and y <= last_y:
return True
else:
# Block has not been seen
self.code_range_cache.append(blockRange)
return False
def _match_keyword(self, token, content):
if isinstance(token, KeywordToken):
if str(token)==content:
return True
return False
def _match_symbol(self, token, content):
if isinstance(token, SymbolToken):
if str(token)==content:
return True
return False
def _match_identifier(self, token, content):
if isinstance(token, IdentifierToken):
if str(token)==content:
return True
return False
def _match_white_space(self, token):
if isinstance(token, WhiteSpaceToken):
return True
return False
## Matches __attribute__((device))
def _match_device_decl(self, buff):
if len(buff) < 6:
return False
if (self._match_keyword(buff[0], '__attribute__') and
self._match_symbol(buff[1], '(') and
self._match_symbol(buff[2], '(') and
self._match_identifier(buff[3], 'device') and
self._match_symbol(buff[4], ')') and
self._match_symbol(buff[5], ')')
):
return 6
return False
## Matches __attribute__((host))
def _match_host_decl(self, buff):
if len(buff) < 6:
return False
if (self._match_keyword(buff[0], '__attribute__') and
self._match_symbol(buff[1], '(') and
self._match_symbol(buff[2], '(') and
self._match_identifier(buff[3], 'host') and
self._match_symbol(buff[4], ')') and
self._match_symbol(buff[5], ')')
):
return 6
return False
## Matches __attribute__((host)) __attribute__((device))
def _match_host_device_decl(self, buff):
h = self._match_host_decl(buff)
if h:
n = 20 # number of whispaces
for i in range(n):
if not self._match_white_space(buff[h+i]):
break
d = self._match_device_decl(buff[h+i:])
if d:
return h+i+d
return False
## Matches __attribute__((device)) __attribute__((host))
def _match_device_host_decl(self, buff):
d = self._match_device_decl(buff)
if d:
n = 20 # number of whispaces
for i in range(n):
if not self._match_white_space(buff[d+i]):
break
h = self._match_host_decl(buff[d+i:])
if h:
return d+i+h
return False
def _match_anything_until(self, buff, untilStr):
for i in range(len(buff)):
if str(buff[i])==untilStr:
return i+1
return False
## Match anything until we see the last '}'
def _match_anything_until_balanced_bracket(self, buff):
openBrackets = 0
for i in range(len(buff)):
if str(buff[i])=='{':
openBrackets += 1
if str(buff[i])=='}':
openBrackets -= 1
if openBrackets == -1:
return i+1
return False
## Match anything until we see a particular token, e.g., ';'
## If see an unwanted char, it doesn't match and return False
def _match_anything_until_except(self, buff, untilStr, exceptChars):
for i in range(len(buff)):
if str(buff[i]) in exceptChars:
return False
if str(buff[i])==untilStr:
return i+1
return False
## Match anything until we see a particular token, e.g., ';',
## or until we see an inbalanaced parethesis ')'.
## If we see an assigment operator (=) we return False
## If we see a curly bracket ({) we return False
def _match_anything_until_or_imbalanced_parenthesis(self, buff, untilStr):
open_parenthesis = 0 # (
open_square_brackets = 0 # [
open_less_than = 0 # <
equal_sign = 0
found_match = False
for i in range(len(buff)):
if str(buff[i])=='<': open_less_than += 1
elif str(buff[i])=='>': open_less_than -= 1
elif str(buff[i])=='[': open_square_brackets += 1
elif str(buff[i])==']': open_square_brackets -= 1
elif str(buff[i])=='(': open_parenthesis += 1
elif str(buff[i])==')':
open_parenthesis -= 1
if open_parenthesis == -1: # found imbalanced parenthesis
#return i+1
found_match = True
break
elif str(buff[i])=='=':
equal_sign += 1
if equal_sign > 1:
return False
elif str(buff[i])==untilStr:
if open_parenthesis == 0:
found_match = True
break
#return i+1
elif str(buff[i])=='{' or str(buff[i])=='}': return False
elif str(buff[i])==',':
if (open_parenthesis==0 and
open_square_brackets==0 and
open_less_than==0):
found_match = True
break
#return i+1
if found_match:
# Check there is at least one arithmetic operator
for k in range(0, i+1):
t = buff[k]
if str(t)=='+' or str(t)=='-' or str(t)=='*' or str(t)=='/':
return i+1
return False
## Get next non-white-space token
def _nextNonEmpty(self, tokensList):
for i in range(len(tokensList)):
if not isinstance(tokensList[i], WhiteSpaceToken):
return i
return None
## Match any of these three device annotations:
## (a) __device__
## (b) __device__ __host__
## (c) __host__ __device__
def _match_any_device_annotation(self, buff):
d = False # device
dh = False # device host
hd = False # host device
func_type = FunctionType.host
dh = self._match_device_host_decl(buff)
if not dh:
dh = self._match_host_device_decl(buff)
if not dh:
d = self._match_device_decl(buff)
if d:
func_type = FunctionType.device
else:
func_type = FunctionType.host_device
else:
func_type = FunctionType.device_host
return d, dh, hd, func_type
## Returns a line number range that defines a device function:
##
## __attribute__((device)) ANY ( ANY ) ANY { ANY }
## __attribute__((device)) __attribute__((host)) ANY ( ANY ) ANY { ANY }
## __attribute__((device)) __attribute__((host)) ANY ( ANY ) ANY { ANY }
##
def match_device_function(self, buff):
linesThatMatched = []
startIndexes = [] # index of __attribute__ tokens
for i in range(len(buff)):
if self._match_keyword(buff[i], '__attribute__'):
startIndexes.append(i)
## Iterate starting from potential function definitions
for i in startIndexes:
if i+1+10 > len(buff): # we need at least 10 token to match
continue
#m1 = self._match_device_decl(buff[i:])
d, d_h, h_d, func_type = self._match_any_device_annotation(buff[i:])
if d or d_h or h_d:
# Get the number of tokens fromn the annotation that matched
if d: m1 = d
elif d_h: m1 = d_h
elif h_d: m1 = h_d
else: return [] # we couldn't match any device function
#print('m1', m1, 'func_type', func_type)
m2 = self._match_anything_until(buff[i+m1:], '(')
if m2:
m3 = self._match_anything_until(buff[i+m1+m2:], ')')
if m3:
m4 = self._match_anything_until(buff[i+m1+m2+m3:], '{')
if m4:
m5 = self._match_anything_until_balanced_bracket(buff[i+m1+m2+m3+m4:])
if m5:
startIndex = i
endIndex = i+m1+m2+m3+m4+m5 - 1 # Important to subtract 1 (because indexes start with zero)
startLine = buff[i].lineNumber()
endLine = buff[endIndex].lineNumber()
if not self._matched_block( (startLine, endLine) ):
if verbose():
print('Not seen block:', (startLine, endLine), '\ncache:', self.code_range_cache)
linesThatMatched.append((startLine, endLine, startIndex, endIndex, func_type))
return linesThatMatched
def _find_indexes_with_assignmets(self, tokensRange):
startIndexes = [] # indexes with assigment operator
for i in range(len(tokensRange)):
if (self._match_symbol(tokensRange[i], '+=') or
self._match_symbol(tokensRange[i], '-=') or
self._match_symbol(tokensRange[i], '*=') or
self._match_symbol(tokensRange[i], '/=')
):
startIndexes.append(i)
elif (self._match_symbol(tokensRange[i], '=')):
if not (self._match_symbol(tokensRange[i-1], '<') or
self._match_symbol(tokensRange[i-1], '>') or
self._match_symbol(tokensRange[i-1], '=')):
startIndexes.append(i)
return startIndexes
## Matches an assigment in a given range of lines:
## ... = x + y ...;
##
## Returns list of tuples (i, j), where with the indexes
## of the begin anf end of the assigment RHS (right hand side)
## for each assigment in the range.
def match_assigment(self, tokensRange):
startIndexes = self._find_indexes_with_assignmets(tokensRange)
ret = []
for i in startIndexes:
## Match until ;
m1 = self._match_anything_until_or_imbalanced_parenthesis(tokensRange[i:], ';')
if m1:
left = self._nextNonEmpty(tokensRange[i+1:])
if verbose(): print('PRE:', tokensRange[i+1+left])
if verbose(): print('POST:', tokensRange[i+m1-1])
ret.append((i+1+left, i+m1-1))
return ret
# Print friendly a bugger of tokens
def printTokens(self, buff):
for i in range(len(buff)):
if verbose(): print('['+str(i)+']:', str(buff[i]))
#--------------------------------------------------------------------#
# Helper #
#--------------------------------------------------------------------#
def printTokenBuffer(buff):
print('*** Buffer ***')
for i in range(len(buff)):
print(i, ':', buff[i].__class__.__name__, buff[i])
#--------------------------------------------------------------------#
# Main #
#--------------------------------------------------------------------#
if __name__ == '__main__':
fileName = sys.argv[1]
t = Tokenizer(fileName)
allTokens = []
for token in t.tokenize():
allTokens.append(token)
print('token', type(token), ':', str(token), 'line:', token.lineNumber())
m = Match()
funcLines = m.match_device_function(allTokens)
print('Lines:', funcLines)
for l in funcLines:
startLine, endLine, startIndex, endIndex, _ = l # unpack lines and indexes
tokenIndexes = m.match_assigment(allTokens[startIndex:endIndex])