-
Notifications
You must be signed in to change notification settings - Fork 0
/
unparser.py
230 lines (203 loc) · 8.26 KB
/
unparser.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
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
import re
from commands import *
from registers import *
from memory import *
from resources import Resources
DEBUG = True
class line(Resources):
'''
The line object contains and handles all the processing of the assembly line
code as python code. As such, it holds all the information of a given line.
Attributes:
label (str): Holds the label of the line, if there exists a label, else
it is None by default.
command (str): The command that is being executed in the line. It is a
string so that we can invoke the command using command_dict.
size (int): Holds the size of the command being executed (in bytes).
ie. If command is being executed as a longword, the size
that will be stored is 4. Similarly word has a size of 2.
source (obj): The source is either an instance of the effective address
class, register class, or an immediate (integer) value.
destination (obj): Similar to source, that is, it is an instance of the
effective address class, or register class, but cannot be
an immediate value.
#TODO: need more testing!!!
'''
def __init__(self, l=None, c=None, z=None, s=None, d=None):
self.label = l
self.size = z
self.source = s
self.dest = d
self.command = c
# self.review() - Temporarily disabling auto-review (Add as a feature)
def review(self):
'''
This method executes the line as python code.
Returns: None
#TODO: Change command_dict to actual command object that invokes
both the command_dict as well as its method command.
'''
# execute command:
Command(self.command, self.size, self.source, self.dest)
s = self.get_source()
d = self.get_dest()
# print(self.source, self.dest)
if DEBUG and print("SOURCE: {} , DEST: {}".format(s, d)): pass
class AssemblyFileReader():
'''
Reads through the '.s' file and converts the assembly commands into python
command for later execution.
#NOTE: Still a work in progresses as we might need to change a few elements
when dealing with multiple files.
#TODO: -Fix the scale factor parsing.
-Commenting is not supported yet.
'''
def __init__(self, file_name = None):
self._filename = file_name # file name
self._file = [] # file unparsed
self._line_a = [] # file parsed (assembly)
self._line_p = dict() # file parsed (python)
self._label_dict = dict() # to organize label to a line number
#IDEA: potentially use a dictionary if we have multiple files?
def read_into_list(self, file_name = None):
'''
Invokes various methods to seperate, the label, command, size,
source, and destination into a tuple for further parsing.
The tuple is stored into _line_a (self) and is blueprinted as follows:
_line_a[line number] = (label, command, size, source, dest)
#NOTE: Under this phase of the program, everything stored into the tuple
is still a string type
'''
c = re.compile(r"""
\s* # skip white space
((?P<label>.*)\:)?
\s*
((?P<command>[a-z]+)?(\.(?P<size>[b|w|l]))?
\s*
(?P<source>.+?)? # source group
\s* (, \s* # skip white space before/after comma
(?P<dest>.*)?)?)? # dest group
\s*$ # skip white space until end of line
""", re.VERBOSE)
if file_name != None:
self.file_name = file_name
with open(self._filename) as f:
self._file = f.readlines()
for line in self._file:
line = line.strip().lower()
if line == '':
continue
l = c.search(line)
label = l.group('label')
command = l.group('command')
size = l.group('size')
source = l.group('source')
dest = l.group('dest')
# if DEBUG and print([label, command, size, source, dest]): pass
self._line_a.append((label, command, size, source, dest))
f.close()
self.process_line()
def process_line(self):
'''
This method will take everything stored into the tuple and convert the
strings into their equivalent python objects using the parse functions.
Line[n] is stored into self._line_p.
'''
n = 0 # line number (0 indexed)
for e in self._line_a:
(l, c, z, s, d) = e
if l != None and l not in self._label_dict:
self._label_dict[l] = n
if z in size_dict:
z = size_dict[z]
s = self.parse_source_or_dest(s, z)
d = self.parse_source_or_dest(d, z)
if DEBUG and print(e): pass
self._line_p[n] = line(l, c, z, s, d)
n += 1
def parse_source_or_dest(self, s, z):
'''
This method parses the source and destination strings into python objects
Arguments:
s (str): The string that must be in either source/destination format
which will under go parsing
z (int): The size of the operation, specifically needed for predecrement
and postincrementing registers
Returns
s (obj): The pythonic object of that string.
ie. '%a0' will return the instance of the AddressRegister
class that represents the register a0.
'''
if s is None:
return None
if s.startswith('-'):
v = s[1:]
c = re.compile(r"""
\(
%a(?P<register>\d)
\)
""", re.VERBOSE)
i = int(c.match(v).group('register'))
v = A[i].get()
A[i].set(v - z, 4)
return memory.get(A[i].get())
elif s.endswith('+'):
v = s[:-1]
c = re.compile(r"""
\(
%a(?P<register>\d)
\)
""", re.VERBOSE)
i = int(c.match(v).group('register'))
v = A[i].get() # get value from address register
A[i].set(v + z, 4)
return memory.get(v) # get effective address
elif s.startswith('(') and s.endswith(')'):
v = s[1:-1]
l = len(v.split(','))
if l == 3:
c = re.compile(r"""
\(
(?P<offset>\d),
%a(?P<address>\d),
%d(?P<scale>\d)
\)
""", re.VERBOSE)
offset = int(c.match(s).group('offset'))
i = int(c.match(s).group('address'))
scale = int(c.match(s).group('scale'))
# factor = int(c.match(s).group('factor'))
return memory.get(offset+A[i].get()+scale)#*factor)
elif l == 2:
c = re.compile(r"""
\(
(?P<offset>\d),
%a(?P<address>\d)
\)
""", re.VERBOSE)
offset = int(c.match(s).group('offset'))
i = int(c.match(s).group('address'))
return memory.get(offset+A[i].get())
elif l == 1:
c = re.compile(r"""
\(
%a(?P<address>\d)
\)
""", re.VERBOSE)
i = int(c.match(s).group('address'))
return memory.get(A[i].get())
else:
for t in sd_type_dict:
if s.startswith(t):
v = s.replace(t, '')
if v.startswith('0x'):
v = int(v, 16)
elif v.startswith('0b'):
v = int(v, 2)
elif v.startswith('0o'):
v = int(v, 8)
else:
v = int(v)
return sd_type_dict[t](v)
assembler = AssemblyFileReader('test.s')
assembler.read_into_list()