-
Notifications
You must be signed in to change notification settings - Fork 4
/
preprocessor.py
378 lines (316 loc) · 15.9 KB
/
preprocessor.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from __future__ import annotations
import collections
import sys
from typing import Dict, Tuple, Iterable, Union, Deque, Set, List, Optional, NoReturn
from flipjump.interpretter.debugging.macro_usage_graph import show_macro_usage_pie_graph
from flipjump.utils.constants import (
MACRO_SEPARATOR_STRING,
STARTING_LABEL_IN_MACROS_STRING,
DEFAULT_MAX_MACRO_RECURSION_DEPTH,
GAP_BETWEEN_PYTHONS_AND_PREPROCESSOR_MACRO_RECURSION_DEPTH,
)
from flipjump.utils.exceptions import FlipJumpPreprocessorException, FlipJumpExprException
from flipjump.assembler.inner_classes.expr import Expr
from flipjump.assembler.inner_classes.ops import (
FlipJump,
WordFlip,
Label,
Segment,
Reserve,
MacroCall,
RepCall,
CodePosition,
Macro,
LastPhaseOp,
MacroName,
NewSegment,
ReserveBits,
Pad,
Padding,
INITIAL_MACRO_NAME,
INITIAL_ARGS,
INITIAL_LABELS_PREFIX,
)
CurrTree = Deque[Union[MacroCall, RepCall]]
OpsQueue = Deque[LastPhaseOp]
LabelsDict = Dict[str, int]
wflip_start_label = '_.wflip_area_start_'
def macro_resolve_error(
curr_tree: CurrTree, msg: str = '', *, orig_exception: Optional[BaseException] = None
) -> NoReturn:
"""
raise a descriptive error (with the macro-expansion trace).
@param curr_tree: the ops in the macro-calling path to arrive in this macro
@param msg: the message to show on error
@param orig_exception: if not None, raise from this base error.
"""
error_str = "Macro Resolve Error" + (f':\n {msg}\n' if msg else '.\n')
if curr_tree:
error_str += 'Macro call trace:\n'
for i, op in enumerate(curr_tree):
error_str += f' {i}) {op.trace_str()}\n'
raise FlipJumpPreprocessorException(error_str) from orig_exception
class PreprocessorData:
"""
maintains the preprocessor "global" data structures, throughout its recursion.
e.g. current address, resulting ops, labels' dictionary, macros' dictionary...
also offer many functions to manipulate its data.
@note should call finish before get_result...().
"""
class _PrepareMacroCall:
def __init__(
self,
curr_tree: CurrTree,
calling_op: Union[MacroCall, RepCall],
macros: Dict[MacroName, Macro],
max_recursion_depth: Optional[int],
):
"""
Validates that the called macro exists, and that the macro depth is ok. Updates the curr_tree variable.
@param curr_tree: the ops in the macro-calling path to arrive in this macro (not including the calling op)
@param calling_op: the current macro call (either of type Macro or RepCall).
@param macros: parser's result; the dictionary from the macro names to the macro declaration
@param max_recursion_depth: The compiler supports macros that recursively uses other macros,
up to the specified recursion depth. If None: no recursion depth restrictions are applied.
"""
self.curr_tree = curr_tree
self.calling_op = calling_op
self.macros = macros
self.max_recursion_depth = max_recursion_depth
def __enter__(self) -> None:
macro_name = self.calling_op.macro_name
if macro_name not in self.macros:
macro_resolve_error(
self.curr_tree,
f"macro {macro_name} is used but isn't defined. " f"In {self.calling_op.code_position}.",
)
self.curr_tree.append(self.calling_op)
if self.max_recursion_depth is not None and len(self.curr_tree) > self.max_recursion_depth:
macro_resolve_error(
self.curr_tree,
"The maximal macro-expansion recursive depth was reached. "
"change the max_recursion_depth variable.",
)
def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore[no-untyped-def]
self.curr_tree.pop()
def __init__(self, memory_width: int, macros: Dict[MacroName, Macro], max_recursion_depth: int):
"""
@param memory_width: the memory-width
@param macros: parser's result; the dictionary from the macro names to the macro declaration
@param max_recursion_depth: The compiler supports macros that recursively uses other macros,
up to the specified recursion depth. If None: no recursion depth restrictions are applied.
"""
self.memory_width = memory_width
self.macros = macros
self.curr_address: int = 0
self.macro_code_size: Dict[str, int] = collections.defaultdict(lambda: 0)
self.curr_tree: CurrTree = collections.deque()
self.curr_segment_index: int = 0
self.labels_code_positions: Dict[str, CodePosition] = {}
self.result_ops: Deque[LastPhaseOp] = collections.deque()
self.labels: Dict[str, int] = {}
self.addresses_with_labels: Set[int] = set()
self.macro_start_labels: List[Tuple[int, str, CodePosition]] = [] # (address, label, code_position)
first_segment: NewSegment = NewSegment(0)
self.last_new_segment: NewSegment = first_segment
self.result_ops.append(first_segment)
self.max_recursion_depth = max_recursion_depth
if max_recursion_depth + GAP_BETWEEN_PYTHONS_AND_PREPROCESSOR_MACRO_RECURSION_DEPTH < sys.getrecursionlimit():
sys.setrecursionlimit(max_recursion_depth + GAP_BETWEEN_PYTHONS_AND_PREPROCESSOR_MACRO_RECURSION_DEPTH)
def patch_last_wflip_address(self) -> None:
self.last_new_segment.wflip_start_address = self.curr_address
def finish(self, show_statistics: bool) -> None:
self.patch_last_wflip_address()
self.insert_macro_start_labels_if_their_address_not_used()
if show_statistics:
show_macro_usage_pie_graph(dict(self.macro_code_size), self.curr_address)
def prepare_macro_call(self, calling_op: Union[MacroCall, RepCall]) -> PreprocessorData._PrepareMacroCall:
return PreprocessorData._PrepareMacroCall(self.curr_tree, calling_op, self.macros, self.max_recursion_depth)
def get_result_ops_and_labels(self) -> Tuple[OpsQueue, LabelsDict]:
return self.result_ops, self.labels
def insert_segment(self, next_segment_start: int) -> None:
self.labels[f'{wflip_start_label}{self.curr_segment_index}'] = self.curr_address
self.curr_segment_index += 1
self.patch_last_wflip_address()
new_segment = NewSegment(next_segment_start)
self.last_new_segment = new_segment
self.result_ops.append(new_segment)
self.curr_address = next_segment_start
def insert_reserve(self, reserved_bits_size: int) -> None:
self.curr_address += reserved_bits_size
self.result_ops.append(ReserveBits(self.curr_address))
def insert_label(self, label: str, code_position: CodePosition, *, address: Optional[int] = None) -> None:
if address is None:
address = self.curr_address
if label in self.labels:
other_position = self.labels_code_positions[label]
macro_resolve_error(
self.curr_tree, f'label declared twice - "{label}" on ' f'{code_position} and {other_position}'
)
self.labels_code_positions[label] = code_position
self.labels[label] = address
self.addresses_with_labels.add(address)
def insert_macro_start_label(self, label: str, code_position: CodePosition) -> None:
"""
@note must be called at the start of the function.
"""
self.macro_start_labels.append((self.curr_address, label, code_position))
def insert_macro_start_labels_if_their_address_not_used(self) -> None:
for address, label, code_position in self.macro_start_labels[::-1]:
if address not in self.addresses_with_labels:
self.insert_label(label, code_position, address=address)
def register_macro_code_size(self, macro_path: str, init_curr_address: int) -> None:
if 1 <= len(self.curr_tree) <= 2:
self.macro_code_size[macro_path] += self.curr_address - init_curr_address
def align_current_address(self, ops_alignment: int) -> None:
op_size = 2 * self.memory_width
ops_to_pad = (-self.curr_address // op_size) % ops_alignment
self.curr_address += ops_to_pad * op_size
self.result_ops.append(Padding(ops_to_pad))
def get_rep_times(op: RepCall, preprocessor_data: PreprocessorData) -> int:
try:
return op.calculate_times(preprocessor_data.labels)
except FlipJumpExprException as e:
macro_resolve_error(
preprocessor_data.curr_tree,
f"Can't evaluate how many times to repeat in " f"'rep {op.macro_name}'. In {op.code_position}.",
orig_exception=e,
)
def get_pad_ops_alignment(op: Pad, preprocessor_data: PreprocessorData) -> int:
try:
return op.calculate_ops_alignment(preprocessor_data.labels)
except FlipJumpExprException as e:
macro_resolve_error(
preprocessor_data.curr_tree,
f"Can't evaluate how much to pad in " f"'pad {op.ops_alignment}'. In {op.code_position}.",
orig_exception=e,
)
def get_next_segment_start(op: Segment, preprocessor_data: PreprocessorData) -> int:
try:
next_segment_start = op.calculate_address(preprocessor_data.labels)
if next_segment_start % preprocessor_data.memory_width != 0:
macro_resolve_error(
preprocessor_data.curr_tree,
f'segment ops must have a w-aligned '
f'(memory-width-aligned) address: '
f'{hex(next_segment_start)}. In {op.code_position}.',
)
return next_segment_start
except FlipJumpExprException as e:
macro_resolve_error(preprocessor_data.curr_tree, f'segment failed. In {op.code_position}.', orig_exception=e)
def get_reserved_bits_size(op: Reserve, preprocessor_data: PreprocessorData) -> int:
try:
reserved_bits_size = op.calculate_reserved_bit_size(preprocessor_data.labels)
if reserved_bits_size % preprocessor_data.memory_width != 0:
macro_resolve_error(
preprocessor_data.curr_tree,
f'reserve ops must have a w-aligned '
f'(memory-width aligned) value: '
f'{hex(reserved_bits_size)}. In {op.code_position}.',
)
return reserved_bits_size
except FlipJumpExprException as e:
macro_resolve_error(preprocessor_data.curr_tree, f'reserve failed. In {op.code_position}.', orig_exception=e)
def get_params_dictionary(
current_macro: Macro, args: Iterable[Expr], namespace: str, labels_prefix: str
) -> Dict[str, Expr]:
"""
generates the dictionary between the labels (params and local-params) defined by the macro, and their Expr-values.
@param current_macro: the current macro
@param args: the macro's arguments (Expressions)
@param namespace: the current namespace
@param labels_prefix: the path to the currently-preprocessed macro
@return: the parameters' dictionary
"""
params_dict: Dict[str, Expr] = dict(zip(current_macro.params, args))
for local_param in current_macro.local_params:
params_dict[local_param] = Expr(f'{labels_prefix}{MACRO_SEPARATOR_STRING}{local_param}')
if namespace:
for k, v in tuple(params_dict.items()):
params_dict[f'{namespace}.{k}'] = v
return params_dict
def resolve_macro_aux(
preprocessor_data: PreprocessorData,
macro_name: MacroName,
args: Iterable[Expr],
labels_prefix: str,
) -> None:
"""
recursively unwind the current macro into a serialized stream of ops and add them to the result_ops-queue.
also add every label's value to the labels-dictionary. both saved in preprocessor_data.
@param preprocessor_data: maintains the preprocessor "global" data structures
@param macro_name: the name of the macro to unwind
@param args: the arguments for the macro to unwind
@param labels_prefix: The prefix for all labels defined in this macro
"""
init_curr_address = preprocessor_data.curr_address
current_macro = preprocessor_data.macros[macro_name]
params_dict = get_params_dictionary(current_macro, args, current_macro.namespace, labels_prefix)
preprocessor_data.insert_macro_start_label(
f'{labels_prefix}{MACRO_SEPARATOR_STRING}{STARTING_LABEL_IN_MACROS_STRING}', current_macro.code_position
)
for op in current_macro.ops:
if isinstance(op, Label):
preprocessor_data.insert_label(op.eval_name(params_dict), op.code_position)
elif isinstance(op, FlipJump) or isinstance(op, WordFlip):
preprocessor_data.curr_address += 2 * preprocessor_data.memory_width
params_dict['$'] = Expr(preprocessor_data.curr_address)
preprocessor_data.result_ops.append(op.eval_new(params_dict))
del params_dict['$']
elif isinstance(op, Pad):
op = op.eval_new(params_dict)
ops_alignment = get_pad_ops_alignment(op, preprocessor_data)
preprocessor_data.align_current_address(ops_alignment)
elif isinstance(op, MacroCall):
op = op.eval_new(params_dict)
next_macro_path = (
f"{labels_prefix}{MACRO_SEPARATOR_STRING}" if labels_prefix else ""
) + f"{op.code_position.short_str()}:{op.macro_name}"
with preprocessor_data.prepare_macro_call(op):
resolve_macro_aux(preprocessor_data, op.macro_name, op.arguments, next_macro_path)
elif isinstance(op, RepCall):
op = op.eval_new(params_dict)
rep_times = get_rep_times(op, preprocessor_data)
if rep_times == 0:
continue
next_macro_path = (
f"{labels_prefix}{MACRO_SEPARATOR_STRING}" if labels_prefix else ""
) + f"{op.code_position.short_str()}:rep{{}}:{op.macro_name}"
with preprocessor_data.prepare_macro_call(op):
for i in range(rep_times):
op.current_index = i
resolve_macro_aux(
preprocessor_data, op.macro_name, op.calculate_arguments(i), next_macro_path.format(i)
)
elif isinstance(op, Segment):
op = op.eval_new(params_dict)
next_segment_start = get_next_segment_start(op, preprocessor_data)
preprocessor_data.insert_segment(next_segment_start)
elif isinstance(op, Reserve):
op = op.eval_new(params_dict)
reserved_bits_size = get_reserved_bits_size(op, preprocessor_data)
preprocessor_data.insert_reserve(reserved_bits_size)
else:
macro_resolve_error(preprocessor_data.curr_tree, f"Can't assemble this opcode - {str(op)}")
preprocessor_data.register_macro_code_size(labels_prefix, init_curr_address)
def resolve_macros(
memory_width: int,
macros: Dict[MacroName, Macro],
*,
show_statistics: bool = False,
max_recursion_depth: int = DEFAULT_MAX_MACRO_RECURSION_DEPTH,
) -> Tuple[OpsQueue, LabelsDict]:
"""
unwind the macro tree to a serialized-queue of ops,
and creates a dictionary from label's name to its address.
@param memory_width: the memory-width
@param macros: parser's result; the dictionary from the macro names to the macro declaration
@param show_statistics: if True then prints the macro-usage statistics
@param max_recursion_depth: The compiler supports macros that recursively uses other macros,
up to the specified recursion depth. If None: no recursion depth restrictions are applied.
@return: tuple of the queue of ops, and the labels' dictionary
"""
preprocessor_data = PreprocessorData(memory_width, macros, max_recursion_depth)
resolve_macro_aux(preprocessor_data, INITIAL_MACRO_NAME, INITIAL_ARGS, INITIAL_LABELS_PREFIX)
preprocessor_data.finish(show_statistics)
return preprocessor_data.get_result_ops_and_labels()