-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_handling.py
87 lines (72 loc) · 3.15 KB
/
error_handling.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
from globalss import *
# error handling after .data directive
def error_handling():
WORDS = ['add', 'and', 'or', 'sll', 'slt', 'sra', 'srl', 'sub', 'xor', 'mul', 'div', 'rem','addi', 'andi', 'ori', 'lb', 'lh', 'lw', 'jalr' ,'beq', 'bne', 'bge', 'blt' , 'sb', 'sw', 'sd', 'sh', 'auipc', 'lui' ,'jal']
Words1=['add', 'and', 'or', 'sll', 'slt', 'sra', 'srl', 'sub', 'xor', 'mul', 'div', 'rem']
Words2=['beq', 'bne', 'bge', 'blt']
BracWords = ['sb', 'sw', 'sd', 'sh', 'lb', 'lh', 'lw', 'jalr']
Words3=['addi', 'andi', 'ori']
Words4=['auipc', 'lui']
Words5=['jal']
Labels=[]
Labels = name_label #make labels list before generating .mc file
Labelss = []
for x in Labels:
Labelss.append(x+':')
validArg = ['x0','x1','x2','x3','x4','x5','x6','x7','x8','x9','x10','x11','x12','x13','x14','x15','x16','x17','x18','x19','x20','x21','x22','x23','x24','x25','x26','x27','x28','x29','x30','x31']
bracArg = ['(x0)', '(x1)', '(x2)', '(x3)', '(x4)', '(x5)', '(x6)', '(x7)', '(x8)', '(x9)', '(x10)', '(x11)', '(x12)', '(x13)', '(x14)', '(x15)', '(x16)', '(x17)', '(x18)', '(x19)', '(x20)', '(x21)', '(x22)', '(x23)', '(x24)', '(x25)', '(x26)', '(x27)', '(x28)', '(x29)', '(x30)', '(x31)' ]
# s = "lw x1 0(x2)"
# l = s.split()
# print(l[2] in validArg)
f = open("assembly.txt", "r")
f2=0
for x in f:
lis = x.split()
if len(lis)==0:
continue
ins = lis[0]
if ins== '.data':
f2=1
continue
if ins=='.text':
f2=0
continue
if f2==1:
continue
if ins in Labelss:
continue
if ins not in WORDS:
print(ins + " instruction is not supported")
return 1
# 1 format
if ins in Words1:
if lis[1] not in validArg or lis[2] not in validArg or lis[3] not in validArg:
print(ins + " in incorrect format")
return 1
# 2 format
if ins in Words2:
if lis[1] not in validArg or lis[2] not in validArg or lis[3] not in Labels:
print(ins + " in incorrect format (" + lis[3]+ " not defined)")
return 1
# Brac format
if ins in BracWords:
if lis[1] not in validArg or lis[2] in validArg or lis[2] in bracArg:
print(ins + " in incorrect format (No offset given)")
return 1
# 3 format
if ins in Words3:
if lis[1] not in validArg or lis[2] not in validArg or lis[3] in validArg:
print(ins +" in incorrect format ")
return 1
# 4 format
if ins in Words4:
if lis[1] not in validArg or lis[2] in validArg:
print(ins + " in incorrect format")
return 1
# 5 format
if ins in Words5:
if lis[1] not in validArg or lis[2] not in Labels :
print(ins + " in incorrect format")
return 1
f.close()
return 0