-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.py
More file actions
executable file
·569 lines (516 loc) · 20.3 KB
/
Copy pathsim.py
File metadata and controls
executable file
·569 lines (516 loc) · 20.3 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/python
# This emulator was originally written by Hanhwi jang.
import argparse
import subprocess
import os
import re
import sys
class Inst:
def __init__ (self, text):
re_instruction = re.compile(r"""
^
(?P<op>\w+) # Opcode
(?:
$ | # No operand (e.g., syscall, nop)
\s+(?P<label_1>[A-Za-z_]\w+)$ | # One label operand (e.g., j, jal)
\s+(?P<r1>\$\w+) # The first register operand
(?:
$ | # One register operand (e.g., jr)
,\s+(?P<label_2>[A-Za-z_]\w+)$ | # One register and one label operands (e.g., la, branchz)
,\s+(?P<immed_1>-?\d+) # An immediate operand
(?:
$ | # One register and one immediate operands (e.g., li)
\((?P<r2_1>\$\w+)\)$ # One register and one address operands (e.g., lw, sw)
) |
,\s+(?P<r2_2>\$\w+) # The second register operand
(?:
$ | # Two registers operand (e.g., Move)
,\s+(?P<label_3>[A-Za-z_]\w+)$ |# Two registers and one label operands (e.g., branchu, branch)
,\s+(?P<immed_2>-?\d+)$ | # Two registers and one immediate operands (e.g., addi)
,\s+(?P<r3>\$\w+)$ # Three registers operands (e.g., add)
)
)
)
""", re.VERBOSE)
m = re_instruction.match(text)
if m is not None:
self.op = m.group('op')
self.r1 = m.group('r1')
if m.group('r2_1') is not None:
self.r2 = m.group('r2_1')
else:
self.r2 = m.group('r2_2')
self.r3 = m.group('r3')
if m.group('immed_1') is not None:
self.immed = m.group('immed_1')
else:
self.immed = m.group('immed_2')
if self.immed is not None:
self.immed = int(self.immed)
if m.group('label_1') is not None:
self.label = m.group('label_1')
elif m.group('label_2') is not None:
self.label = m.group('label_2')
else:
self.label = m.group('label_3')
else:
print "ERROR [%s]" % text
def _print(self):
print self.op, self.r1, self.r2, self.r3, self.immed, self.label
class RF:
archRegsName = [ "$zero", "$at", "$v0", "$v1","$a0","$a1" ,"$a2", "$a3",
"$t0" ,"$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
"$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7",
"$t8", "$t9", "$k0", "$k1", "$gp", "$sp", "$fp", "$ra"]
archRegsMap = dict(zip(archRegsName, range(0, len(archRegsName))))
def getReg(self, reg_name):
if reg_name in RF.archRegsMap:
return self.archRegsVal[RF.archRegsMap[reg_name]]
else:
reg_index = int(reg_name[2:])
if reg_index in self.virtRegsStack[-1]:
return self.virtRegsStack[-1][reg_index]
else:
return None
def setReg(self, reg_name, value):
if reg_name in RF.archRegsMap:
if reg_name != "$zero":
self.archRegsVal[RF.archRegsMap[reg_name]] = value
else:
reg_index = int(reg_name[2:])
self.virtRegsStack[-1][reg_index] = value
def push(self):
self.virtRegsStack.append({})
def pop(self):
del self.virtRegsStack[-1]
def __init__ (self):
self.archRegsVal = [None for x in xrange(0, len(RF.archRegsName))]
self.archRegsVal[0] = 0
self.setReg('$sp', 0xc0000000)
self.setReg('$gp', 0xdeadbeef)
self.setReg('$ra', -1)
self.virtRegsStack = [{}]
return
class Machine:
def get_block_idx(self, addr):
return addr / 4
def syscall(self):
if self.rf.getReg("$v0") == 9:
# sbrk
self.rf.setReg("$v0", 0xffff0000)
return
def execute(self):
# Arit2 case
r = 0
dst = None
nextip = self.ip + 1
inst = self.insts[self.ip]
# print "IP", self.ip
# inst._print()
if inst.op == 'abs':
r = abs(self.rf.getReg(inst.r2))
dst = inst.r1
elif inst.op == 'neg':
r = -(self.rf.getReg(inst.r2))
dst = inst.r1
elif inst.op == 'not':
if self.rf.getReg(inst.r2) == 0:
r = 1
else:
r = 0
dst = inst.r1
elif inst.op == 'add':
# Arith3 case
r = (self.rf.getReg(inst.r2) + self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'and':
r = (self.rf.getReg(inst.r2) & self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'mulo':
r = (self.rf.getReg(inst.r2) * self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'div':
r = (self.rf.getReg(inst.r2) / self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'or':
r = (self.rf.getReg(inst.r2) | self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'rem':
r = (self.rf.getReg(inst.r2) % self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'sub':
r = (self.rf.getReg(inst.r2) - self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'xor':
r = (self.rf.getReg(inst.r2) ^ self.rf.getReg(inst.r3))
dst = inst.r1
elif inst.op == 'seq':
if self.rf.getReg(inst.r2) == self.rf.getReg(inst.r3):
r = 1
else:
r = 0
dst = inst.r1
elif inst.op == 'slt':
if self.rf.getReg(inst.r2) < self.rf.getReg(inst.r3):
r = 1
else:
r = 0
r = (self.rf.getReg(inst.r2) < self.rf.getReg(inst.r3))
dst = inst.r1
# Arithi case
elif inst.op == 'addi':
r = (self.rf.getReg(inst.r2) + inst.immed)
dst = inst.r1
elif inst.op == 'andi':
r = (self.rf.getReg(inst.r2) & inst.immed)
dst = inst.r1
elif inst.op == 'orii':
r = (self.rf.getReg(inst.r2) | inst.immed)
dst = inst.r1
elif inst.op == 'xori':
r = (self.rf.getReg(inst.r2) ^ inst.immed)
dst = inst.r1
# Li
elif inst.op == 'li':
r = inst.immed
dst = inst.r1
# La
elif inst.op == 'la':
r = self.inst_label[inst.label]
dst = inst.r1
# Lw
elif inst.op == 'lw':
addr = self.rf.getReg(inst.r2) + inst.immed
r = self.data[self.get_block_idx(addr)]
dst = inst.r1
# Sw
elif inst.op == 'sw':
addr = self.rf.getReg(inst.r2) + inst.immed
self.data[self.get_block_idx(addr)] = self.rf.getReg(inst.r1)
dst = None
# Move
elif inst.op == 'move':
r = self.rf.getReg(inst.r2)
dst = inst.r1
# Branchz
elif inst.op == 'bltz':
if self.rf.getReg(inst.r1) < 0:
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'beqz':
if self.rf.getReg(inst.r1) == 0:
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bnez':
if self.rf.getReg(inst.r1) != 0:
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bgez':
if self.rf.getReg(inst.r1) >= 0:
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bgtz':
if self.rf.getReg(inst.r1) > 0:
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'blez':
if self.rf.getReg(inst.r1) <= 0:
nextip = self.inst_label[inst.label]
dst = None
# Branchu (* Not correctly implemented *)
elif inst.op == 'bltu':
if self.rf.getReg(inst.r1) < self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bgeu':
if self.rf.getReg(inst.r1) >= self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bgtu':
if self.rf.getReg(inst.r1) > self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bleu':
if self.rf.getReg(inst.r1) <= self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'blt':
if self.rf.getReg(inst.r1) < self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
# Branch
elif inst.op == 'beq':
if self.rf.getReg(inst.r1) == self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bne':
if self.rf.getReg(inst.r1) != self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bge':
if self.rf.getReg(inst.r1) >= self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'bgt':
if self.rf.getReg(inst.r1) > self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
elif inst.op == 'ble':
if self.rf.getReg(inst.r1) <= self.rf.getReg(inst.r2):
nextip = self.inst_label[inst.label]
dst = None
pass
elif inst.op == 'j':
nextip = self.inst_label[inst.label]
dst = None
pass
elif inst.op == 'jal':
r = nextip
if inst.label == '_printint':
if (not is_check) and (not is_output):
print "PRINT:", self.rf.getReg("$a0")
else:
self.output_list.append(self.rf.getReg("$a0"))
else:
nextip = self.inst_label[inst.label]
self.rf.push()
dst = "$ra"
elif inst.op == 'jr':
if inst.r1 == '$ra':
self.rf.pop()
nextip = self.rf.getReg(inst.r1)
dst = None
pass
elif inst.op == 'jalr':
if self.rf.getReg(inst.r2) == self.inst_label['_printint']:
if (not is_check) and (not is_output):
print "PRINT:", self.rf.getReg("$a0")
else:
self.output_list.append(self.rf.getReg("$a0"))
else:
r = nextip
nextip = self.rf.getReg(inst.r2)
self.rf.push()
dst = inst.r1
pass
elif inst.op == 'syscall':
self.syscall()
pass
elif inst.op == 'nop':
pass
# update context
self.ip = nextip
if dst is not None:
# inst._print()
# print self.ip
# print dst
# print len(self.regs)
self.rf.setReg(dst, r)
return
def __init__(self):
self.rf = RF()
self.insts = []
self.data = {} # data
# Instruction Pointer
self.ip = 0
self.inst_label = {}
self.data_base = 0
self.static_base = 0x10000000
if is_check or is_output:
self.output_list = []
return
def load(self, fname):
with open(fname, 'rt') as f:
re_label = re.compile('([A-Za-z_.0-9]+):')
is_text = False
max_reg = 0
for line in f:
i_comment_begin = line.find('#')
if i_comment_begin == -1:
# This line does not have a comment; just stripping spaces
l = line.strip()
elif i_comment_begin == 0:
# The whole line is a comment
continue
else:
# Removing comments and stripping spaces
l = (line[0:i_comment_begin]).strip()
if l == "":
continue
elif l == '.data':
text_flag = False
continue
elif l == '.text':
text_flag = True
continue
elif text_flag is False:
# data mode
# label ?
# data ?
# Ignore everything; fun program does not need a data section
continue
else:
# text mode
# label ?
m = re_label.match(l)
if m is not None:
self.inst_label[m.group(1)] = self.ip
else:
i = Inst(l)
self.insts.append(i)
self.ip = self.ip + 1
return
def start(self):
self.ip = self.inst_label['main']
while True:
self.execute ()
if self.ip == -1:
break
else:
pass
if (not is_check) and (not is_output):
print "main function returns %s" % self.rf.getReg('$v0')
return;
else:
self.output_list.append(self.rf.getReg("$v0"))
return self.output_list
#print self.regs
def stat (self):
return
# I've got this function from http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def which(spath, cond):
def cond_check(fpath):
return os.path.isfile(fpath) and os.access(fpath, cond)
fpath, fname = os.path.split(spath)
if fpath:
if cond_check(spath):
return spath
else:
path_candidate = [os.getcwd()] + (os.environ["PATH"].split(os.pathsep))
for path in path_candidate:
path = path.strip('"')
full_path = os.path.join(path, spath)
if cond_check(full_path):
return full_path
return None
def check_sml(sml, sources):
sml_instance = subprocess.Popen([sml, '-m', sources], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(sml_stdout, sml_stderr) = sml_instance.communicate(input="OS.Process.exit(OS.Process.success) ;")
returncode = sml_instance.returncode
if returncode != 0:
print "Error in checking sml\nError code : %d" % returncode
print "output from sml :\n" + sml_stdout
print "error from sml :\n" + sml_stderr
sys.exit(returncode)
pass_test = 0
fail_test = 0
def run_test(filename):
global pass_test
global fail_test
if use_allocated:
assembly_filename = filename + ".s"
else:
assembly_filename = filename + ".noregalloc.s"
stdout_filename = filename + ".stdout"
output_filename = filename + ".out"
if is_check:
if which(output_filename, os.R_OK) is not None:
print "A fun source code \"" + filename + "\" and the answer file \"" + output_filename + "\" is found. Testing.."
else:
print "A fun source code \"" + filename + "\" is found but its answer file \"" + output_filename + "\" does not exist. Skipping.."
return;
else:
print "A fun source code \"" + filename + "\" is found. Running.."
if os.path.isfile(assembly_filename):
os.remove(assembly_filename)
elif os.path.isdir(assembly_filename):
os.removedirs(assembly_filename)
compiler_instance = subprocess.Popen([sml, '-m', sources], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(compiler_stdout, compiler_stderr) = compiler_instance.communicate(input="Compile.compile(\"" + filename + "\"); OS.Process.exit(OS.Process.success) ;")
returncode = compiler_instance.returncode
if returncode != 0:
print "Error in checking sml\nError code : %d" % returncode
print "output from sml :\n" + sml_stdout
print "error from sml :\n" + sml_stderr
return;
if not which(assembly_filename, os.R_OK):
print "Compile failure"
print "output from compiler :\n" + compiler_stdout
print "error from compiler :\n" + compiler_stderr
return;
# Print the compile output
with open(stdout_filename, "wt") as stdout_file:
print >> stdout_file, compiler_stdout
m = Machine();
m.load(assembly_filename)
if is_output:
with open(output_filename, "wt") as output_file:
print >> output_file, m.start()
elif is_check:
with open(output_filename, "rt") as output_file:
re_list = re.compile(r"^\s*\[\s*(?:None|-?\s*\d+)(?:\s*,\s*(?:None|-?\s*\d+))*\s*\]\s*$")
matched_list = None
for line in output_file:
if re_list.match(line) is not None:
matched_list = line.strip()
break
if matched_list is None:
print "The answer file does not have an answer. Skipping.."
return;
ans = eval(matched_list)
cur = m.start()
if ans == cur:
print "PASS"
pass_test = pass_test + 1
else:
print "FAIL"
print "Answer :\t" + ", ".join(map(str,ans)) + "(return value)"
print "Output :\t" + ", ".join(map(str,cur)) + "(return value)"
fail_test = fail_test + 1
else:
m.start()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Emulate a mips processor for a program with virtual registers')
default_sml = "sml"
default_sources = "sources.cm"
parser.add_argument('testfiles', metavar='testfiles', nargs='+', help='a file name or a directory name for the test file')
parser.add_argument("-s", "--sml", metavar='sml', help="A command for invoking sml interpreter. (default : \"" + default_sml + "\")", default=default_sml)
parser.add_argument("-S", "--sources", metavar='sources', help="A path for sources.cm file. (default : \"" + default_sources + "\")", default=default_sources)
group = parser.add_mutually_exclusive_group()
group.add_argument("-c", "--check", help="checks your implementation with answer files", action="store_true")
group.add_argument("-o", "--output", help="generates answer files", action="store_true")
parser.add_argument("-a", "--allocated", help="uses *.s instead of *.noregalloc.s", action="store_true")
args = parser.parse_args()
global sml
sml = which(args.sml, os.X_OK)
if sml is None:
print "The executable file \"" + args.sml + "\" is not found"
sys.exit(-1)
global sources
sources = which(args.sources, os.R_OK)
if sources is None:
print "The readable file \"" + args.sources + "\" is not found"
sys.exit(-1)
check_sml(sml, sources)
global is_check
is_check = args.check
global is_output
is_output = args.output
global use_allocated
use_allocated = args.allocated
re_fun = re.compile('^.*\.fun$')
for testfile in args.testfiles:
abs_testfile = which(testfile, os.R_OK)
if abs_testfile is not None:
run_test(abs_testfile)
elif os.path.isdir(testfile):
for root, dirs, files in os.walk(testfile):
for filename in files:
if re_fun.match(filename):
abs_testfile = which(os.path.join(root, filename), os.R_OK)
if abs_testfile is not None:
run_test(abs_testfile)
else:
print "The testfile \"" + testfile + "\" is not found. Skipping.."
if is_check:
print "%d test(s), %d pass(es), %d fail(s)" % (pass_test + fail_test, pass_test, fail_test)