-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyteassembler.py
162 lines (129 loc) · 4.81 KB
/
byteassembler.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
"""
The Byteassembler takes a human-readable form of Bytecode and assembles it into actual bytecode.
opcodes.txt contains the list of opcodes and their arguments.
"""
import sys
import os.path
from bytecompiler import MathHelper
def test():
if not len(sys.argv) > 1:
print("Please enter a file!")
return
if not (os.path.exists(sys.argv[1]) and os.path.isfile(sys.argv[1])):
print("Not a file!")
return
f = open(sys.argv[1], "r")
d = f.read()
f.close()
print(assemble(d).hex())
def assemble(code):
a = Assembler()
a.add_data(code)
return a.assemble()
class Assembler:
def __init__(self):
f = open("opcodes.txt", "r")
o = f.read()
f.close()
o = o.split("\n")[10:-1]
self.data = ""
self.labels = {}
self.opcodes = {}
for l in o:
l = l.split(" ")
self.opcodes[l[1]] = ((int(l[0], 16), l[2:]))
def add_data(self, data):
self.data += data
def assemble(self):
code = b""
self.data = self.data.split("\n")
lb = dict()
for ln, l in enumerate(self.data):
l = self._remove_ws(l)
if l == "":
continue
if l.endswith(":"):
if l.count(" ") == 0:
l = l[:-1]
if l in self.labels:
print("The label '" + l + "' has already been defined.")
return
self.labels[l] = len(code)
if l in lb:
for e in lb[l]:
n = e[1]
x = e[0]
code = code[:x] + MathHelper.ifsign((len(code) - (x - 1)), n).to_bytes(n, "big") + code[x+n:]
del lb[l]
continue
else:
print("Labels can't have spaces!")
return
l = l.split(" ")
op = self.opcodes[l[0]]
code += op[0].to_bytes(1, "big")
a = 1
for x in op[1]:
if x == "local":
code += int(l[a]).to_bytes(1, "big")
elif x == "const2" or x == "const":
code += int(l[a]).to_bytes((1 if x == "const" else 2), "big")
elif x == "byte":
n = 1
code += MathHelper.ifsign(int(l[a]), n).to_bytes(n, "big")
elif x == "short":
n = 2
code += MathHelper.ifsign(int(l[a]), n).to_bytes(n, "big")
elif x == "tbranch" or x == "fbranch":
n = (2 if x == "tbranch" else 4)
try:
code += MathHelper.ifsign(int(l[a]), n).to_bytes(n, "big")
continue
except ValueError:
pass
if not l[a] in self.labels:
code += bytes(n)
if l[a] in lb:
lb[l[a]] = [*lb[l[a]], (len(code) - n, n)]
else:
lb[l[a]] = [(len(code) - n, n)]
continue
f = self.labels[l[a]]
code += MathHelper.ifsign(-((len(code) - 1) - f), n).to_bytes(n, "big")
elif x == "atype":
if l[a] == "boolean":
code += b"\x04"
elif l[a] == "char":
code += b"\x05"
elif l[a] == "float":
code += b"\x06"
elif l[a] == "double":
code += b"\x07"
elif l[a] == "byte":
code += b"\x08"
elif l[a] == "short":
code += b"\x09"
elif l[a] == "int":
code += b"\x0a"
elif l[a] == "long":
code += b"\x0b"
else:
print("Invalid array type!")
return
elif x == "special":
print("Special cases aren't allowed yet.")
return
a += 1
for x in lb.keys():
print("ERROR: Label '" + x + "' does not exist!")
return code
def _remove_ws(self, l): #Remove whitespace
i = 0
for c in l:
if c != " " and c != "\t":
break
else:
i += 1
return l[i:]
if __name__ == "__main__":
test()