-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpp.py
406 lines (362 loc) · 15.2 KB
/
mpp.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/python
"""
MIPS Preprocessor
by Steve Johnson (srj15@case.edu) and Tim Henderson (tah35@case.edu)
This code is licensed under the GNU General Public License v2.0.
For more info, please consult http://www.gnu.org/licenses/gpl-2.0.html
==INCLUDING FILES==
#include your_file.s
==DEFINING MACROS==
#define macro_name [global]
move %1, %2
#end
Put %n in macros to specify where parameters go.
Add 'global' to the #define line if this macro should be accessible from all other files.
==CALLING MACROS==
macro_name a b
==USING MACROS IN MACROS==
You can use macros inside other macros as long as the first is defined above the second.
==REGISTER RENAMING==
Define an alias like this:
@my_alias = $t0
Then you can use them just like a register.
==REPETITIONS==
Put "#repeat n" above a line to repeat that line n times. See kernel_data.s for an example.
==SCOPING==
Put curly braces around your code on their own lines to make all labels and aliases valid only within that scope.
"""
"""
Kernel macros:
number_user_programs $dst
counts user programs
load_user_programs
loads starting addresses for user programs into the appropriate kernel data locations
load_first_program:
starts the program specified in the jistfile
"""
import string, sys, re
global_macros = {}
kernel_macros = {}
max_user_programs = 16
main_count = 0
label_count = 0
main_labels = list()
var_split = re.compile(r'\s*=\s*')
strip_comments = False
first_prog = 0
magic_strings = []
def get_file_text(f1):
#process includes
s = ""
in_lines = []
included = []
for line in f1:
stripped = line.strip()
if stripped.startswith("#include"):
linesplit = stripped.split()
arg = ' '.join(linesplit[1:])
if arg not in included:
included.append(arg)
f3 = open(arg, 'r')
text = get_file_text(f3)
if not strip_comments:
text = '\n###'+arg+'###\n' + text + '\n###end '+arg+'###\n'
f3.close()
in_lines.append(text)
else:
in_lines.append(line)
return ''.join(in_lines)
def make_kernel_macros():
'''creates specialized macros. this is sneaky sneaky business.'''
global kernel_macros
magic_prompt_string = ''
if len(magic_strings):
magic_prompt_string = ' .asciiz "'+''.join(magic_strings) + '"\n'
number_user_programs = ''
if not strip_comments:
number_user_programs += ' '*4 + '#'*16 + ' start number_user_programs ' + '#'*16 + '\n'
number_user_programs += ' '*4 + 'li $s1 &main_count&'
if not strip_comments:
number_user_programs += ' '*4 + '#'*17 + ' end number_user_programs ' + '#'*17 + '\n'
number_user_programs = ''.join(
process_lines(number_user_programs, True, False)
)
load_user_programs = ''
if not strip_comments:
load_user_programs += ' '*4 + '#'*16 + ' start load_user_programs ' + '#'*16 + '\n'
load_user_programs += ' '*4 + '__save_frame\n'
load_user_programs += ' '*4 + 'la $s0 user_program_locations\n'
for i in range(main_count):
load_user_programs += ' '*4 + 'la $s1 &main_labels['+str(i)+']&\n'
load_user_programs += ' '*4 + 'sw $s1 '+str(i*4)+'($s0)\n'
load_user_programs += ' '*4 + '__restore_frame\n'
if not strip_comments:
load_user_programs += ' '*4 + '#'*17 + ' end load_user_programs ' + '#'*17 + '\n'
load_user_programs = ''.join(
process_lines(load_user_programs, True, False)
)
load_first_program = ''
if not strip_comments:
load_first_program += ' '+"#"*16+' start load_first_program '+'#'*16+'\n'
load_first_program += " la $s0 " + "user_program_locations\n"
load_first_program += " lw $s1 " + "%s($s0)\n" % str(first_prog)
load_first_program += " li $s2 " + "%s\n" % str(first_prog)
load_first_program = ''.join(
process_lines(load_first_program, True, False)
)
if not strip_comments:
load_first_program += ' '*4 + '#'*17 + ' end load_first_program ' + '#'*17 + '\n'
kernel_macros.update({
'number_user_programs':number_user_programs,
'load_user_programs':load_user_programs,
'load_first_program':load_first_program,
'magic_prompt_string':magic_prompt_string
})
def post_process_kernel_macro(macro_text):
'''to be called after arg replacement is finished'''
r = re.compile(r'\&.*\&')
exprs = r.findall(macro_text)
for expr in exprs:
exec "rep = " + expr[1:-1] in globals()
macro_text = macro_text.replace(expr, str(rep))
return macro_text
#if specified, rename all the labels so they don't conflict with
#tim&steve's kernel labels
def substitute_labels(s):
global main_count, label_count
line_list = s.split('\n')
replacements = []
label_test = re.compile(r'^([a-zA-Z0-9_]+):( |$)')
label_strip = re.compile(r'__u\d+$')
in_macro = False
for line in line_list:
linestrip = line.strip()
if linestrip.startswith('#define'): in_macro = True
if linestrip.startswith('#end'): in_macro = False
if len(linestrip) > 0 and not in_macro:
r = label_test.match(linestrip)
if r:
label = r.groups()[0]
if label == 'main':
replacements.append((
re.compile(r'\b'+label+r'\b'),
label+'_'+str(main_count)
))
main_labels.append(label+'_'+str(main_count))
main_count += 1
if main_count >= max_user_programs:
raise Exception, "to many user programs added"
else:
replacements.append((
re.compile(r'\b'+label+r'\b'),
label_strip.split(label)[0]+'__u'+str(label_count)
))
label_count += 1
for r, new in replacements:
s = r.sub(new, s)
return s
def rep_line(line, local_macros, use_kernel_macros):
#process macros
global global_macros
out_lines = []
linesplit = line.split()
if len(linesplit) > 0:
mtext = ""
name = linesplit[0]
#See if first keyword is a local or global macro, set mtext if found
if string.lower(name) in global_macros.keys():
mtext = global_macros[name]
if string.lower(name) in local_macros.keys():
mtext = local_macros[name]
if use_kernel_macros and string.lower(name) in kernel_macros.keys():
mtext = kernel_macros[name]
#if keyword is a macro...
if mtext != "":
#if macro has arguments...
if len(linesplit) > 1:
#walk comma-delimited arg list
arg_num = len(linesplit) - 1
arg_list_string = ' '.join(linesplit[1:])
arg_list = [t.strip() for t in arg_list_string.split()]
while arg_num > 0:
#replace expression with argument
mtext = mtext.replace("%"+str(arg_num), arg_list[arg_num-1])
arg_num -= 1
if use_kernel_macros and string.lower(name) in kernel_macros.keys():
mtext = post_process_kernel_macro(mtext)
mtext = substitute_labels(mtext)
#append macro text (possibly transformed) to output
out_lines.append(process_lines(mtext, use_kernel_macros, local_macros))
else:
out_lines.append(line)
else:
out_lines.append(line)
return out_lines
def rep_names(lines, names):
for i, line in enumerate(lines):
rep = False
for name in names.keys():
nameloc = lines[i].find(name)
hashsign = lines[i].find('#')
r = re.compile(r'(\W)'+name+'(\W|$)')
gs = r.search(lines[i])
while gs and nameloc != -1 and (nameloc < hashsign or hashsign == -1):
gs = gs.groups()
if nameloc != -1 and (nameloc < hashsign or hashsign == -1):
rep = True
lines[i] = r.sub(gs[0]+names[name]+gs[1], lines[i], 1)
nameloc = lines[i].find(name)
hashsign = lines[i].find('#')
gs = r.search(lines[i])
if rep and lines[i][-1] != '>':
line = line.lstrip().rstrip()
hashsign = line.find('#')
if hashsign == -1: hashsign = len(line)
lines[i] += ' # ::-> ' + line[:hashsign] + '>'
return lines;
def process_lines(s, kernel, use_kernel_macros, local_macros=dict(), toplevel=False):
global global_macros
in_lines = s.split('\n')
in_macro = False
is_global = False
#out_lines = list()
repetitions = 1
macro_name = ""
scopes = [[]]
varnames = [dict()]
cm_varnames = None #current macro variable names
for line in in_lines:
kw = string.lower(line.strip())
if kw.startswith('#define'):
#start defining macro, get its name and init a list of its lines
if in_macro: print "Macro error."
in_macro = True
linesplit = line.split()
macro_name = string.lower(linesplit[1])
is_global = False
if strip_comments:
start_text = ''
else:
start_text = ' '*4 + '#'*16 + ' start ' + macro_name + ' ' + '#'*16
if len(linesplit) > 2 and string.lower(linesplit[2]) == 'global':
is_global = True
if is_global:
global_macros[macro_name] = [start_text]
else:
local_macros[macro_name] = [start_text]
cm_varnames = dict()
elif kw.startswith('#end'):
#concatenate the lines and stop defining the macro
in_macro = False
if strip_comments:
end_text = ''
else:
end_text = ' '*4 + '#'*17 + ' end ' + macro_name + ' ' + '#'*17
if macro_name in local_macros:
local_macros[macro_name].append(end_text)
local_macros[macro_name] = rep_names(local_macros[macro_name], cm_varnames)
local_macros[macro_name] = "\n".join(local_macros[macro_name])
if macro_name in global_macros:
global_macros[macro_name].append(end_text)
global_macros[macro_name] = rep_names(global_macros[macro_name], cm_varnames)
global_macros[macro_name] = "\n".join(global_macros[macro_name])
cm_varnames = None
elif kw.startswith('#repeat'):
linesplit = line.split()
if len(linesplit) == 2:
repetitions = int(linesplit[1])
elif kw.startswith('{'):
#print '{'
scopes.append(list())
varnames.append(dict())
elif kw.startswith('}'):
#print '}'
l = scopes.pop()
names = varnames.pop()
#print l
lines = substitute_labels('\n'.join(l)).split('\n')
lines = rep_names(lines, names)
scopes[-1].extend(lines)
#print lines
#print scopes[-1][-3:]
elif kw.startswith('@'): #variable name
name = var_split.split(line.lstrip().rstrip())
if len(name) != 2: raise Exception, 'Syntax Error in variable name line = \n' + line
if in_macro:
if cm_varnames.has_key(name[0]):
raise Exception, 'Syntax Error name "%s" already defined in current macro "%s"'%\
(name[0], macro_name)
if name[1] in cm_varnames.values():
raise Exception, 'Syntax Error reg "%s" already named in current macro "%s"'%\
(name[1], macro_name)
cm_varnames[name[0]] = name[1]
if macro_name in local_macros:
local_macros[macro_name].append('#' + line)
elif macro_name in global_macros:
global_macros[macro_name].append('#' + line)
else:
if varnames[-1].has_key(name[0]):
raise Exception, \
'Syntax Error name "%s" already defined in current scope\n line = "%s"'\
% (name[0], line)
if name[1] in varnames[-1].values():
raise Exception, \
'Syntax Error reg "%s" already named in current scope\n line = "%s"'%\
(name[1], line)
varnames[-1][name[0]] = name[1]
scopes[-1].append('#' + line)
else:
if in_macro:
#check for macro-in-macro
if macro_name in local_macros.keys():
local_macros[macro_name].append(line)
#+= rep_line(line, local_macros, use_kernel_macros)
if macro_name in global_macros.keys():
global_macros[macro_name].append(line)
#+= rep_line(line, local_macros, use_kernel_macros)
else:
#check for regular ol' macro
scopes[-1] += rep_line(line, local_macros, use_kernel_macros) * repetitions
repetitions = 1
if len(scopes) == 1 and len(varnames) == 1:
lines = rep_names(scopes[0], varnames[0])
for i, line in enumerate(lines):
atsign = line.find('@')
hashsign = line.find('#')
if toplevel and atsign != -1 and (atsign < hashsign or hashsign == -1):
raise Warning, "Syntax error name unconverted on line = '%s'" % line
if toplevel and line and line[-1] == '>': lines[i] = line[:-1]
lines[i] = ' '*4 + lines[i].lstrip().rstrip()
if strip_comments:
return '\n'.join([l for l in lines if not l.strip().startswith('#') and l.strip() != ''])
else:
return '\n'.join(lines)
else:
raise Exception, "Scoping Error"
def process(path, out, kernel=False, replace_labels=False, use_kernel_macros=False, cstrip=False, first=0, ps=[]):
global global_macros, strip_comments, first_prog, magic_strings #magic!
magic_strings = ps
strip_comments = cstrip
first_prog = first
f1 = open(path, 'r')
s = get_file_text(f1)
if replace_labels:
s = substitute_labels(s)
s = process_lines(s, kernel, use_kernel_macros, toplevel=True)
f1.close()
#write giant string to file
f2 = open(out, 'w')
f2.write(s)
f2.close()
#make_kernel_macros()
#print kernel_macros['load_user_programs']
#print main_labels
#print global_macros
if __name__ == "__main__":
#if called from the cl, process with default args
try:
infile = sys.argv[1]
outfile = sys.argv[2]
except:
raise Exception, "use 'python mpp.py in_file out_file"
process(infile, outfile)