-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
281 lines (236 loc) · 10.9 KB
/
scanner.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
'''
scans the code and extract loop nest information
1. depth
2. loop bounds
3. input sizes
...
...
'''
import re
from costmodel import *
import math
import operator
class Scanner:
def __init__(self, file):
self.file = file
def scan_loop(self):
fp = open(self.file,'r')
d = 0 #number of loops
loop_ids = []
stms = 0 #number of stms
stmt_list = []
STMT_LIST = [] #top level holder of all stmts
stmt_nest = [] #number of stms in each nest
limits= []
stmt_ready = False
rbracks = 0
lbracks = 0
nests = [] #number of loop nests, and their depths. nests[i] = depth of i-th nest
nest = 0 #current loop nest
newNest = True
inside = False
linenumber = 0
num_itrs = [] #number of iterations for each loop level
num_itrs_per_nest = []
symbols = {} #hold #define values
vars = [] #hold variable names
mem = {'ref':0} #global accesses, cache/registry accesses, memory reuse info
ARITH = [] # number of operations for each nest
MEM = [] #memory references for each loop nest
add = 0
sub = 0
mul = 0
div = 0
mod = 0
while True:
line = fp.readline()
if line == '': #EOF
break
linenumber = linenumber + 1 # line number
if line.strip() == '\n': #empty line
continue
if line.strip().startswith('//'):
continue
if '#define' in line: #currently, symbols are only read via #define
l = line.split(' ')
try:
symbols.update({l[1].strip(): int(l[2])})
except ValueError:
continue
if line.strip().startswith('float'):
if '=' in line:
first = (re.split(r'=', line)[0]).strip()
vars.append(first.split()[1].strip())
elif ',' in line: #multiple declarations in the same line
first = re.split(r'float|,|;', line)[1:-1]
for f in range(len(first)):
vars.append(first[f].strip())
elif '[' not in line and ',' not in line:
first = re.split(r'float|;', line)[1]
vars.append(first.strip())
else:
#a line that declares float data. Assume format float A[N][N]
#assume one declaration for a line
first = (re.split(r'\[', line))[0]
#now remove the data type 'float'
vars.append(first.split()[1])
if line.strip().startswith('int') and 'main' not in line:
#int a;
#int a = 1;
if '=' not in line:
vars.append(line.split()[1][:-1])
if 'for' in line:
#extract the loop control logic
inside = True
d = d + 1 # new loop level
if newNest:
nest +=1
nests.append(0)
newNest = False
num_itrs_per_nest.append(0)
stmt_nest.append(0)
STMT_LIST.append([])
ARITH.append({'add':0, 'sub': 0, 'mul': 0, 'div': 0, 'mod': 0 })
MEM.append({'ref':0})
nests[nest-1] += 1
stmt_ready = True
gotStart = False
gotEnd = False
#l = re.split(r'\(|\)', line.strip()) #ideally this should break the line into 3 parts, l[1] has the loop control data
l= re.split('{', line.split('for')[1].strip())[0].strip()[1:-1]
#print l
l1 = l.split(';') #again, 3 pieces
#print l1
idx = l1[0].split('=')[0]
if idx not in loop_ids:
loop_ids.append(idx.strip())
start = l1[0].split('=')[1]
if start.strip() in symbols: #if terminate condition is a defined variable
start = symbols[start.strip()]
elif start.strip() in loop_ids:
start = math.log(max(symbols.iteritems(), key=operator.itemgetter(1))[1]) #a heuristic approximation of the log2 of the largest symbol
elif '?' in start: #tertnary operator
start = '0'
else:
#start bound is most likely an expression including either a symbol or another loop bound
for symbol in symbols: #more complicated end conditions such as N-1, ...
if symbol in re.split(r'\+|-|\*', start.strip()):
start = symbols[symbol]
gotStart = True
break
if not gotStart:
for var in loop_ids:
if var in re.split(r'\+|-|\*', start.strip()):
start = math.log(max(symbols.iteritems(), key=operator.itemgetter(1))[1]) #a heuristic approximation of the log2 of the largest symbol
break
#print vars
#print l1
l2 = re.split(r'<=|>=|<|>|',l1[1])
end = l2[1]
if end.strip() in symbols: #if terminate condition is a defined variable
end = symbols[end.strip()]
elif end.strip() in loop_ids:
end = math.log(max(symbols.iteritems(), key=operator.itemgetter(1))[1]) # a heuristic approximation of the log2 of the largest symbol
else:
for symbol in symbols:
if symbol in re.split(r'\+|-|\*', end.strip()):
end = symbols[symbol]
#print end
gotEnd = True
break
if not gotEnd:
for var in loop_ids:
if var in re.split(r'\+|-|\*', end.strip()):
end = math.log(max(symbols.iteritems(), key=operator.itemgetter(1))[1]) #a heuristic approximation of the log2 of the largest symbol
break
limits.append((idx, start,end))
# TODO: extract loop bounds and infer if they are > 0
step = l1[2]
if '++' in step or '--' in step:
step_size = 1
else:
#i = i + x
rhs = (step.split('=')[1]).strip()
step_size = int(re.split(r'\+|-|\*|/',rhs)[-1])
try:
end = float(end)
except ValueError:
for var in loop_ids:
if var in re.split(r'\+| - |\*', end.strip()):
end = math.log(max(symbols.iteritems(), key=operator.itemgetter(1))[1]) # a heuristic approximation of the log2 of the largest symbol
break
try:
start = float(start)
except ValueError:
for var in loop_ids:
if var in re.split(r'\+|-|\*', start.strip()):
start = math.log(max(symbols.iteritems(), key=operator.itemgetter(1))[1]) # a heuristic approximation of the log2 of the largest symbol
break
print start, end, step_size
it = math.ceil((end - start)/step_size)
x = math.fabs(it)
if x == 0.0:
x = 1 # 0 messes up the multiplication later on
num_itrs.append(x)
num_itrs_per_nest[nest-1] +=x
#continue
if '{' in line:
if inside and d>0:
lbracks = lbracks + 1
stmt_ready = True
continue
if '}' in line:
if inside:
rbracks = rbracks + 1
if lbracks == rbracks: #braces are balanced
stmt_ready = False
inside = False #leaving the loop nest
newNest = True
else:
stmt_ready = True
continue
if line.strip().endswith(';'): #any other line
if stmt_ready:
stms = stms + 1
stmt_ready = False
stmt_list.append([])
stmt_nest[nest-1] +=1
if inside:
(stmt_list[stms-1]).append(line.strip())
STMT_LIST[nest-1].append(line.strip())
#extract computational and memory info
add = add + line.count('+')
sub = sub + line.count('-')
mul = mul + line.count('*')
div = div + line.count('/')
mod = mod + line.count('%')
ARITH[nest-1]['add'] += line.count('+')
ARITH[nest-1]['sub'] += line.count('-')
ARITH[nest-1]['mul'] += line.count('*')
ARITH[nest-1]['div'] += line.count('/')
ARITH[nest-1]['mod'] += line.count('%')
for v in vars:
if v in line:
MEM[nest-1]['ref'] += line.count(v)
continue
#tot_itrs = 1
#for loop in range(len(num_itrs)): #limits = [(idx, start, end)]
# tot_itrs *= num_itrs[loop] # end - start
tot_itrs = 0
for n in range(len(nests)):
tot_itrs+= num_itrs_per_nest[n]
#DEBUG PRINTS
#print symbols
#print limits
print 'Nests=', nests, 'depth=', d, 'nests=', nest
#print num_itrs_per_nest
#print 'iterations='+ str(tot_itrs)
#print stmt_nest
#print vars
#arith = {'add': add, 'sub': sub, 'mul': mul, 'div': div, 'mod': mod}
#print ARITH
#print MEM
c = cost(mem= MEM, arith=ARITH, tot_lines=linenumber, tot_itrs=num_itrs_per_nest, nests=nests, num_nests=len(nests))
#c['memcost'], c['arithcost'] are now lists of length len(nests)
return {"depth":d, 'stms':stms, 'loops':loop_ids, 'lines': linenumber,
'stms_list': STMT_LIST, 'mem':c['memcost'], 'arith': c['arithcost'], 'stmt_nest': stmt_nest, 'loop_nests': nests}