-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreg.rb
More file actions
161 lines (130 loc) · 4.03 KB
/
Copy pathreg.rb
File metadata and controls
161 lines (130 loc) · 4.03 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
class Reg
@i_format_array = ["lw", "lb", "lbu", "sw", "sb", "lui", "beq", "bne", "addi"]
@r_format_array = ["add", "sub", "nor", "and", "slt", "sltu", "sll", "srl", "jr"]
@j_format_array = ["j", "jal"]
# ONLY THESE REGISTERS WILL BE ACCEPTED BY THE REGEX
@zero = ["$zero", "$0"]
@at = ["$at"]
@params = ["$a0", "$a1", "$a2", "$a3"]
@return = ["$v0", "$v1"]
@temp = ["$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8", "$t9"]
@save = ["$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7"]
@kernel = ["$k0", "$k1"]
@global = ["$gp"]
@stack = ["$sp"]
@frame = ["$fp"]
@address = ["$ra"]
@rd = @params + @return + @temp + @save + @stack
@rd.map!{ |r| r = "\\" + r}
@rs = @rd + @zero.map{ |r| r = "\\" + r}
# def self.p s, n
# string = "["
# for i in 0...n
# string += "\"$#{s}#{i}\""
# string += ", " if i != n-1
# end
# string += "]"
# puts string
# end
# self.p "k", 2
def self.check command
if command == nil
puts ("Error empty command")
end
op = command.split[0]
if @i_format_array.include? op
self.check_i_format command
elsif @r_format_array.include? op # DONE
self.check_r_format command
elsif @j_format_array.include? op # DONE
self.check_j_format command
else
"Command Not Found: #{command.split[0]}"
end
end
def self.check_i_format command
op = command.split[0]
case op
when "addi"
(command =~ /^(\s*)(#{op})(\s+)(#{@rd.join "|"})(,\s+)(#{@rs.join "|"})(,\s+)([-]?\d+)(\s*)$/) != nil
when "beq", "bne"
(command =~ /^(\s*)(#{op})(\s+)(#{@rs.join "|"})(,\s+)(#{@rs.join "|"})(,\s+)([a-zA-Z]\w*)(\s*)$/) != nil
else
if op == "lui"
(command =~ /^(\s*)(#{op})(\s+)(#{@rd.join "|"})(,\s+)(\d+)(\s*)$/) != nil
elsif ((op == "lw") || (op == "lu") ||(op == "lb") )
(command =~ /^(\s*)(#{op})(\s+)(#{@rd.join "|"})(,\s+)(\d+\((#{@rs.join "|"})\))(\s*)$/) != nil
else
(command =~ /^(\s*)(#{op})(\s+)(#{@rs.join "|"})(,\s+)(\d+\((#{@rs.join "|"})\))(\s*)$/) != nil
end
end
end
def self.check_r_format command
op = command.split[0]
case op
when "sll", "srl"
(command =~ /^(#{op})(\s+)(#{@rd.join "|"})(,\s+)(#{@rs.join "|"})(,\s+)(\d+)(\s*)$/) != nil
when "jr"
(command =~ /^(\s*)(#{op})(\s+)(\$ra)(\s*)$/) != nil
else
(command =~ /^(\s*)(#{op})(\s+)(#{@rd.join "|"})(,\s+)(#{@rs.join "|"})(,\s+)(#{@rs.join "|"})(\s*)$/) != nil
end
end
def self.check_j_format command
(command =~ /^(\s*)(#{command.split[0]})(\s+)([a-zA-Z]\w*)(\s*)$/) != nil
end
# TESTING COMMANDS
# WHEN SELF.RUN, TYPE ANY COMMAND
# IF IT'S VALID, THE CONSOLE PRINTS TRUE AND SPLITS THE COMMAND INTO ARRAY ELEMENTS
# OTHERWISE IT PRINTS FALSE
# EXAMPLE
# add $t5, $zero, $zero
# true: ["add", "$t5", "$zero", "$zero"]
def self.decode command
command.split.map{ |s| s.gsub(",", "")}
end
def self.get_format command
op = command.split[0]
if @i_format_array.include? op
"i"
elsif @r_format_array.include? op # DONE
"r"
elsif @j_format_array.include? op # DONE
"j"
else
"n"
end
end
def self.get_rs
@rs
end
def self.branch_command? command
["bne", "beq"].include? command.split[0]
end
def self.jump_command? command
["j", "jal", "jr"].include? command.split[0]
end
# def self.run
# print "command_> "
# x = gets.chomp
# while x != "q"
# res = self.check(x)
# out = "#{res}"
# out += ": #{self.decode x}" if res == true
# puts out
# print "command_> "
# x = gets.chomp
# end
# end
# self.run
# puts self.check("addi $t1, $t0, -8")
# puts self.check("lbu $t1, $t0, 8")
puts self.check("lbu $0, 0($t0)")
# puts (self.check("lbu $t1, 0($t1)").to_s + "this one")
# puts self.check("jal LOOP")
# puts self.check("jal 3LOOP")
# puts self.check("jr $ra")
# puts self.check("beq $0, $0, LOOP3")
# puts self.check("beq $0, $0, 9")
puts self.check("lui $t0, 16")
end