-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmips_checker.pl
executable file
·242 lines (195 loc) · 5.65 KB
/
mips_checker.pl
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
#! /usr/bin/env perl
# MIPS INSTRUCTION CHECKER
#
# Searches through STDIN to verify that the file is entirely made up of valid
# instructions (and not pseudo-instruction). It reads each line, removes
# comments, collapses whitespace, removes labels, discards blank lines
# (including lines that *became* blank when everything else was removed), and
# then verifies that what is left is a valid instruction.
#
# There are several formats that must be recognized:
# R-format (3 operand)
# 2 register and integer (addi, sll, etc.)
# BEQ / BNE
# J / JAL
# JR
# LA
# SYSCALL
# .data / .text
# .globl
# .word / .half / .byte
# .asciiz / .ascii
# CONFIG OPTIONS
#
# As the semester progresses, we will turn on support for new instructions,
# which are not allowed earlier in the semester.
$allow_unsigned_addsub = 1;
$allow_muldiv = 0;
$allow_lui = 0;
$allow_jr = 1;
$allow_mfhilo = 0;
$allow_var_shift = 0;
$allow_bit_count = 0;
$line_no = 0;
foreach $line (<STDIN>)
{
$line_no++;
# get rid of any comment that might exist. The s/foo/bar/g regular
# expression form is for substitution; in this case, we replace
# anything that looks like a comment with an empty string.
$line =~ s/#.*//g;
# remove leading and trailing whitespace, includine newlines.
# Thanks to
# https://perlmaven.com/trim
# for the elegant regexp.
$line =~ s/^\s+|\s+$//g;
# collapse all whitespace down to single spaces.
$line =~ s/\s+/ /g;
# remove labels from the front of any line.
$line =~ s/^\s*[a-zA-Z0-9_]+\s*:\s*//;
# accept blank lines
if ($line eq "")
{
next;
}
# accept lines which are simple assembler directives
if ($line =~ m/^[.](text|data)/)
{
next;
}
if ($line =~ m/^[.]globl\s+[a-zA-Z0-9_]+$/)
{
next;
}
if ($line =~ m/^[.](word|half|byte)\s+(-?[0-9]+|0x[0-9a-fA-F]+|[a-zA-Z0-9_]+|'([^']|\\.)')$/)
{
next;
}
# UPDATE: MARS doesn't require a space before the leading double-quote
# in an .asciiz line. So I made it optional.
if ($line =~ m/^[.]asciiz? ?".*"$/)
{
next;
}
# This pattern matches all R-format instructions (except for shift)
if ($line =~ m/^([a-z]+) \$[a-z0-9]+ ?, ?\$[a-z0-9]+ ?, ?\$[a-z0-9]+$/)
{
# is the instruction one of the ones we recognize?
$inst = $1;
if ($inst =~ m/^(add|sub|slt|and|or|nor|xor)$/)
{
next;
}
if ($allow_unsigned_addsub && $inst =~ m/^(addu|subu)$/)
{
next;
}
if ($allow_muldiv && $inst eq "mul")
{
next;
}
if ($allow_var_shift && $inst =~ m/^(srav|srlv|sllv)$/)
{
next;
}
}
# recognize all of the conditional branch instructions
if ($line =~ m/^(beq|bne) \$[a-z0-9]+ ?, ?\$[a-z0-9]+ ?, ?[a-zA-Z0-9_]+$/)
{
next;
}
# recognize all of the I-format (immediate value) instructions, PLUS
# the shift instructions.
if ($line =~ m/^(addiu?|slti|andi|ori|xori|sll|srl|sra) \$[a-z0-9]+ ?, ?\$[a-z0-9]+ ?, ?(-?[0-9]{1,5}|0x[0-9a-fA-F]{1,4}|'([^']|\\.)')/)
{
next;
}
# recognize the LA instruction here - since it's a real oddball
if ($line =~ m/^la \$[a-z0-9]+ ?, ?[a-zA-Z0-9_]+$/)
{
next;
}
# so is lui, if we're hoenst!
if ($allow_lui && $line =~ m/^lui \$[a-z0-9]+ ?, ?(-?[0-9]{1,5}|0x[0-9a-fA-F]{1,4})/)
{
next;
}
# we've added support for mult/div; we'll handle mfhi/mflo later,
# along with jr
#
# Update: also support clo,clz
if ($allow_muldiv && $line =~ m/^([a-z]+) \$[a-z0-9]+ ?, ?\$[a-z0-9]+/)
{
# is the instruction one of the ones we recognize?
$inst = $1;
if ($allow_muldiv && $inst =~ m/mul|div/)
{
next;
}
if ($allow_bit_count && $inst =~ m/cl(o|z)/)
{
next;
}
}
# recognize all load/store instructions.
if ($line =~ m/^[ls][whb] \$[a-z0-9]+ ?, ?(-?[0-9]+|0x[0-9a-fA-F]+) ?\( ?\$[a-z0-9]+ ?\)$/)
{
next;
}
# recognize all of the j-format instructions
if ($line =~ m/^(j|jal) [a-zA-Z0-9_]+$/)
{
next;
}
# recognize JR - and also MFHI,MFLO - all of which only take a single
# register parameter.
if ($line =~ m/^([a-z]+) \$[a-z0-9]+$/)
{
$inst = $1;
if ($allow_jr && $inst eq "jr")
{
next;
}
if ($allow_mfhilo && $inst =~ m/^(mfhi|mflo)$/)
{
next;
}
}
# recognize syscall - the only non-parameterized instruction
if ($line eq "syscall")
{
next;
}
print "ERROR: UNRECOGNIZED CODE AT LINE $line_no: '$line'\n";
# handle very common mixed-up formats:
# LW <la-params>
# LA <lw-params>
# LW <missing offset>
# ADD <ADDI-type-args>
# ADDI <ADD-type-args>
if ($line =~ m/^[ls][whb] \$[a-z0-9]+ ?, ?[a-zA-Z0-9_]+$/)
{
print " (You used a load/store instruction, but gave the proper parameters for an LA instruction.)\n";
print " LA - load the address of a label into a register\n";
print " LW - read a value from memory\n";
}
if ($line =~ m/^la \$[a-z0-9]+ ?, ?(-?[0-9]+|0x[0-9a-fA-F]+) ?\( ?\$[a-z0-9]+ ?\)$/)
{
print " (You used the LA instruction, but gave the proper parameters for an load/store instruction.)\n";
print " LA - load the address of a label into a register\n";
print " LW - read a value from memory\n";
}
if ($line =~ m/^[ls][whb] ?(-?[0-9]+|0x[0-9a-fA-F]+) ?\( ?\$[a-z0-9]+ ?\)$/)
{
print " (All load/store operations require an offset, as well as the base register.)\n";
print " EXAMPLE: lw \$t0, 0(\$t3)\n";
}
if ($line =~ m/^add \$[a-z0-9]+ ?, ?\$[a-z0-9]+ ?, ?(-?[0-9]{1,5}|0x[0-9a-fA-F]{1,4}|'([^']|\\.)')/)
{
print " (You used the ADD instruction, but gave the proper parameters for an ADDI.)\n";
}
if ($line =~ m/^addi \$[a-z0-9]+ ?, ?\$[a-z0-9]+ ?, ?\$[a-z0-9]+$/)
{
print " (You used the ADDI instruction, but gave the proper parameters for an ADD.)\n";
}
}