This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
I_Format.py
108 lines (82 loc) · 3.38 KB
/
I_Format.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import re
from UJU_format import UJU_Format
class MyException(Exception):
pass
mnemonic_I = {
'addi': {'opcode': '0010011', 'funct3': '000', 'type': 'value', 'label': False},
'andi': {'opcode': '0010011', 'funct3': '111', 'type': 'value', 'label': False},
'ori': {'opcode': '0010011', 'funct3': '110', 'type': 'value', 'label': False},
'lb': {'opcode': '0000011', 'funct3': '000', 'type': 'offset', 'label': True},
'ld': {'opcode': '0000011', 'funct3': '011', 'type': 'offset', 'label': True},
'lh': {'opcode': '0000011', 'funct3': '001', 'type': 'offset', 'label': True},
'lw': {'opcode': '0000011', 'funct3': '010', 'type': 'offset', 'label': True},
'jalr': {'opcode': '1100111', 'funct3': '000', 'type': 'both', 'label': True}
}
#addi, andi, ori, lb, ld, lh, lw, jalr
def I_Format(instruction):
return I_Format0(instruction, 0)
def I_Format0(instruction, x):
instruction = instruction.replace(',', ' ')
pattern = re.compile(r'(\w+)\s+(\w+)\s+(\-?\w+)(\s+)?(.+)?')
matches = pattern.search(instruction)
machine_code = []
mnemonic = matches.group(1)
try:
rd = matches.group(2).replace('x', '')
except:
raise MyException("One or more arguements is missing!")
imm_neg = 0
if matches.group(4) != None:
try:
rs = matches.group(3).replace('x', '')
imm = matches.group(5)
except:
raise MyException("One or more arguements is missing!")
try:
int(imm)
except:
raise MyException("Immediate Value must be an Integer!")
elif matches.group(5) == None:
if mnemonic_I[mnemonic]['label'] == False:
raise MyException("One or more arguements is missing!")
rs = rd
addr = matches.group(3)
if addr[0:2] != "0x":
raise MyException("One or more arguements is missing!")
UJU_Format('lui x{} {}'.format(rs, addr[0:6]))
machine_code = I_Format0(mnemonic + ' ' + rs + ' 0(' + rs + ')', x + 1)
else:
rs = matches.group(5)
rs = rs.replace('(', '')
rs = rs.replace(')', '')
rs = rs.replace('x', '')
imm = matches.group(3)
if int(rs) > 31:
raise MyException("register x" + rs + " not defined")
if int(rd) > 31:
raise MyException("register x" + rd + " not defined")
if int(imm) > 2047 or int(imm) < -2048:
raise MyException("Immediate value out of bounds!")
if imm[0] == '-':
imm = imm.replace('-', '')
imm = str(int(imm) - 1)
imm_neg = 1
if(len(machine_code) != 5):
if imm_neg == 0:
machine_code.append(bin(int(imm)).replace("0b", "").rjust(12, '0'))
else:
imm = bin(int(imm)).replace("0b", "").rjust(12, '0')
for i in range(len(imm)):
if imm[i] == '0':
imm = imm[:i] + '1' + imm[i+1:]
else:
imm = imm[:i] + '0' + imm[i+1:]
machine_code.append(imm)
machine_code.append(bin(int(rs)).replace("0b", "").rjust(5, '0'))
machine_code.append(mnemonic_I[mnemonic]['funct3'])
machine_code.append(bin(int(rd)).replace("0b", "").rjust(5, '0'))
machine_code.append(mnemonic_I[mnemonic]['opcode'])
if (x == 0):
machine_hex="{:08x}".format(int(''.join(machine_code),2))
return "0x"+machine_hex
return machine_code