-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
executable file
·325 lines (289 loc) · 9.02 KB
/
compiler.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from exceptions import ModuleExceptions, TranspilerExceptions
from modules import Module
from rich.console import Console
from rich.table import Table
from rich.syntax import Syntax
console = Console()
from rich import print as rprint
import inspect
import pprint
pprint = pprint.PrettyPrinter(indent=2).pprint
class Compiler:
def __init__(
self,
tree,
nextaddr = 0,
variables = {}, #Dictlist(),
functions = {},
arguments = {},
label_count = 0,
bitdata_nextaddr = 0,
namespace = 'main',
bitstart = "0x10000000",
bitdata = "0x10200001",
warnings = 0,
line = 1,
flags = '',
namespaces = {},
filename = ""
) -> None:
self.code = tree
self.actions = {}
self.filename = filename
self.variables: dict = variables
self.functions: dict = functions
self.arguments: dict = arguments
self.flags = []
self.bitstart = bitstart
self.bitend = None
self.bitdata = bitdata
self.nextaddr = nextaddr if nextaddr or (isinstance(nextaddr, int) and nextaddr != 0) else int(bitstart, 0)
self.bitdata_nextaddr = bitdata_nextaddr if bitdata_nextaddr or (isinstance(bitdata_nextaddr, int) and bitdata_nextaddr != 0) else int(bitdata, 0)
self.mode = "standard"
self.labelcounter = label_count
self.namespace = namespace
self.namespaces = namespaces
self._modules = {}
self._current_code = ""
self.warnings = warnings
self.line = line
self.type_bit_length = {
"char":1,
"hex":2,
"int8":2,
"int16":3,
"int32":4,
"int":4,
"list":1,
"list_int":4
}
self.finished = []
self.finished_funcs = []
self.raw_modules = []
if 'o1' in flags:
self.optimisation_level = 1
elif 'o2' in flags:
self.optimisation_level = 2
elif 'o3' in flags:
self.optimisation_level = 3
else:
self.optimisation_level = 1
def run(self, code=None):
if code is None:
code = self.code
else:
code = code
for action in code:
if action[0] == "COMPILER":
if action[1]['KEY'] == 'bitstart':
self.bitstart = int(action[1]['VALUE'][1]['VALUE'], 16)
self.nextaddr = self.bitstart
if action[1]['KEY'] == 'origin':
offset = int(action[1]['VALUE'][1]['VALUE'], 16)
self.flags.insert(0,f"\n; LABEL_OFFSET\nfl labeloffset={offset}\n")
elif action[0] in self.actions:
ret = self.actions[action[0]](action[1], op = self.optimisation_level)
self._current_code = action
if self.actions[action[0]].type == Module.MODULE_TYPES.SPECIAL_ACTION:
self.finished.append(ret)
elif self.actions[action[0]].type == Module.MODULE_TYPES.SPECIAL_ACTION_FUNC:
self.finished_funcs.append(ret)
else:
raise TranspilerExceptions.UnknownActionReference(action[0])
self.line += 1
def new_instance(
self,
tree,
nextaddr = 0,
variables = {}, #Dictlist(),
functions = {},
label_count = 0,
bitdata_nextaddr = 0,
namespace = 'sub_main',
bitstart = None,
bitdata = None,
inherit = False,
namespaces = {}
):
if inherit:
instance = Compiler(
tree,
self.nextaddr,
variables,
functions,
{},
self.labelcounter,
self.bitdata_nextaddr,
namespace,
self.bitstart,
self.bitdata,
self.warnings,
self.line,
self.namespaces,
filename = self.filename
)
else:
instance = Compiler(
tree,
nextaddr,
variables,
functions,
{},
label_count,
bitdata_nextaddr,
namespace,
bitstart,
bitdata,
self.warnings,
self.line,
self.namespaces,
filename = self.filename
)
# Modules and actions need to be recreated with a new scope
instance.populate_modules_actions(
self.raw_modules
)
# instance.actions = self.actions
# instance._modules = self._modules
return instance
def populate_modules_actions(
self,
Modules: list[Module]
) -> None:
self.raw_modules = Modules
for module in Modules:
modobj: Module = module(self)
modobj.type = module.type
if modobj.type == Module.MODULE_TYPES.FUNCTION:
self.functions[module.name] = modobj
else:
self.actions[module.name] = modobj
self._modules[module.name] = modobj
def get_action(
self,
name
) -> Module:
if name in self.actions:
return self.actions[name]
else:
print(f"At {self.line}")
raise TranspilerExceptions.ActionRequiredButNotFound(name, self.actions)
def allocate(
self,
size
) -> hex:
pos = self.nextaddr
self.nextaddr += size
return hex(pos)
def new_label(
self
) -> int:
label = self.labelcounter
self.labelcounter += 1
return label
def sync(
self,
instance,
passthrough: bool = False
):
"""
Synchronize with the instance's values
"""
self.nextaddr = instance.nextaddr
self.bitdata_nextaddr = instance.bitdata_nextaddr
self.labelcounter = instance.labelcounter
self.warnings = instance.warnings
if passthrough:
self.namespaces[instance.namespace] = instance
for func in instance.functions:
func = instance.functions[func]
if isinstance(func, dict):
self.finished_funcs.append(func['source'])
def warn(
self
):
self.warnings += 1
def get_variable(
self,
name: str
) -> dict:
if name in self.variables:
return self.variables[name]
else:
print(f"At {self.line}")
print(self._current_code)
print(self.namespace)
raise TranspilerExceptions.UnkownVar(name, self.variables)
def create_variable(
self,
name: str,
pos: int,
type,
obj,
force = False
) -> None:
if name in self.variables and not force and name != '_':
print(f"At {self.line}")
raise TranspilerExceptions.VarExists(name)
self.variables[name] = {
"pos": pos,
"type": type,
"object": obj
}
def create_function(
self,
name: str,
obj: object,
source: str,
returns: str
) -> None:
self.functions[name] = {
"object": obj,
"source": source,
"returns": returns
}
def create_namespace(
self,
name: str,
obj: object,
source: str
) -> None:
self.namespaces[name] = {
"object": obj,
"source": source
}
def pause(self):
rprint("[yellow bold]Compilation paused. Press enter to continue[/yellow bold]")
input()
def guide(
self
) -> None:
'''
Display a guide block for debugging
'''
print('\n')
table = Table(title=f"Compiler object of {self}")
table.add_column("Module & Type", justify="right", style="cyan")
table.add_column("Source", justify="right", style="green")
table.add_column("Optimised [Bool/Code]", justify="right", style="white")
table.add_column("Valid", justify="right", style="white")
table.add_column("ID", justify="right", style="white")
for module in self._modules:
module: Module = self._modules[module]
source = Syntax(
inspect.getsource(module.proc_tree),
"Python",
padding=1,
line_numbers=True
)
if module.optimise is None:
optimise = "Unoptimised"
else:
optimise = Syntax(
inspect.getsource(module.optimise),
"Python",
padding=1,
line_numbers=True
)
table.add_row(f"{module.name}\n\n[red]{module.type}[/red]", source, optimise, str(module.verify()), hex(id(module)))
console.print(table)