-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.py
50 lines (38 loc) · 1.53 KB
/
scanner.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
from parse import parse
from utils import get_instruction_format, register_for_name
def parse_r_type(opcode, line):
if opcode in ['add', 'sub', 'mul', 'and', 'or', 'xor', 'slt', 'seq']:
return parse("{} ${}, ${}, ${}", line)
if opcode in ['sll', 'srl', 'sra']:
return parse("{} ${}, ${}, {}", line)
if opcode == 'mv':
return parse("{} ${}, ${}", line)
def parse_i_type(opcode, line):
if opcode in ['sw', 'lw', 'thread_finished']:
return [opcode]
if opcode in ['ldc', 'ldi']:
return parse("{} ${}, {}", line)
if opcode == 'addi':
return parse("{} ${}, ${}, {}", line)
def scan(input):
instructions = []
for line in input:
comment_index = line.find(';')
if comment_index < 0:
comment_index = len(line)
bare_line = line[0:comment_index].replace('?', '').strip()
opcode = bare_line.split(" ")[0]
masked = line[0] == '?'
instruction_format = get_instruction_format(opcode)
instruction_tokens = None
if instruction_format == 'r':
instruction_tokens = parse_r_type(opcode, bare_line)
if instruction_format == 'i':
instruction_tokens = parse_i_type(opcode, bare_line)
if instruction_format == 'nop':
instruction_tokens = ['nop']
if not instruction_tokens:
continue
instruction_tokens = map(register_for_name, instruction_tokens)
instructions.append(list(instruction_tokens) + [masked, line])
return instructions