-
Notifications
You must be signed in to change notification settings - Fork 0
/
hccompile.py
executable file
·399 lines (303 loc) · 10.1 KB
/
hccompile.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env python3
import sys
import string
from hcexceptions import HCTypeError, LexerError, HCParseError
from hcast import generate_name
import hrminstr as hrmi
import hcparse2
# Extract a list of all unique blocks from a statement list
def extract_blocks(stmt_list):
if len(stmt_list.stmts) == 0:
return []
nodes_to_check = [stmt_list.stmts[0].block]
blocks = []
names_assigned = 0
while len(nodes_to_check) > 0:
block = nodes_to_check.pop()
while isinstance(block, hrmi.CompoundBlock):
block = block.first_block
if block in blocks:
continue
blocks.append(block)
block.set_label(generate_name(names_assigned))
names_assigned += 1
if block.conditional is not None:
nodes_to_check.append(block.conditional.dest)
if block.next is not None:
nodes_to_check.append(block.next.dest)
return blocks
# Optimise code by tracking what the state of the
# office will be at each stage in the code.
def optimise_state_tracking(blocks, initial_memory):
# First, ensure all state_at_start values are accurate
state = hrmi.OfficeState([hrmi.EmptyHands()])
for mem in initial_memory:
if mem.value is not None:
state.add_constraint(hrmi.VariableHasValue(mem.name, mem.value))
blocks[0].update_state(state)
blocks_to_check = [blocks[0]]
while len(blocks_to_check) > 0:
blk = blocks_to_check.pop()
if blk.state_data_propagated:
continue
state = blk.state_at_start.clone()
for instr in blk.instructions:
instr.simulate_state(state)
# Propagate through both possible paths of the conditional jump
if blk.conditional is not None:
cond_state = state.clone()
blk.conditional.simulate_state_pass(cond_state)
cond_block = blk.conditional.dest
cond_block.update_state(cond_state)
blocks_to_check.append(cond_block)
blk.conditional.simulate_state_fail(state)
# Propagate through the unconditional
if blk.next is not None:
next_block = blk.next.dest
next_block.update_state(state)
blocks_to_check.append(next_block)
blk.state_data_propagated = True
# Make optimisations based on calculated state data
for blk in blocks:
state = blk.state_at_start.clone()
i = 0
while i < len(blk.instructions):
instr = blk.instructions[i]
# Remove the instruction if it's redundant
if instr.state_redundant(state):
del blk.instructions[i]
continue
# Expand pseudo instructions if possible
if isinstance(instr, hrmi.PseudoInstruction):
expanded = instr.attempt_expand(state)
if expanded is not None:
blk.instructions[i:i+1] = expanded
continue
instr.simulate_state(state)
i += 1
cjump = blk.conditional
if cjump is None:
continue
# Check if the conditional jump will always fail or always pass
if cjump.redundant_fails(state):
blk.unlink_conditional()
elif cjump.redundant_passes(state):
blk.next.redirect(cjump.dest)
blk.unlink_conditional()
def memory_map_contains(memory_map, var_name):
for memloc in memory_map:
if memloc.name == var_name:
return True
return False
# Find each time a variable is referenced, and work backwards
# to each place in the code that that variable could be getting
# its value from, marking that in every instruction inbetween,
# that variable is in use.
def record_variable_use(blocks, memory_map):
for blk in blocks:
blk.clear_variable_use()
for blk in blocks:
for i in range(len(blk.instructions)):
instr = blk.instructions[i]
if not instr.reads_variable:
continue
blk.back_propagate_variable_use(i, instr.loc,
memory_map_contains(memory_map, instr.loc))
# Similar to record_variable_use, but tracks when the values stored in the hands
# are needed.
def record_hand_use(blocks):
for blk in blocks:
blk.clear_hand_use()
for blk in blocks:
for i, instr in enumerate(blk.instructions):
if not instr.reads_hands:
continue
blk.back_propagate_hands_use(i)
if blk.conditional is not None:
blk.back_propagate_hands_use(-1)
# Optimise code by tracking where variables are actually
# used, and when their value is last set.
# Any instances of a variable's value being set when
# it isn't going to be used again may be removed.
def optimise_variable_needs(blocks, memory_map):
record_variable_use(blocks, memory_map)
record_hand_use(blocks)
for blk in blocks:
i = 0
while i < len(blk.instructions):
instr = blk.instructions[i]
if instr.var_redundant():
del blk.instructions[i]
else:
i += 1
def rename_variable(blocks, old_name, new_name):
for block in blocks:
for instr in block.instructions:
if instr.needs_variable(old_name):
instr.mark_variable_used(new_name)
if not isinstance(instr, hrmi.AbstractParameterisedInstruction):
continue
if instr.loc == old_name:
instr.loc = new_name
# Class used to track which variables are used alongside which others.
class VariableUseTracker:
__slots__ = [
# Set of sorted tuples of pairs of variables
# which are used simultaneously.
"used",
# Set of each unique variable seen
"unique_vars",
]
def __init__(self):
self.used = set()
self.unique_vars = set()
def _mk_key(self, var_a, var_b):
return tuple(sorted((var_a, var_b)))
def mark_used(self, var_a, var_b):
self.used.add(self._mk_key(var_a, var_b))
def are_used(self, var_a, var_b):
if var_a == var_b:
return True
return self._mk_key(var_a, var_b) in self.used
def add_var(self, var):
self.unique_vars.add(var)
def get_unique(self):
return iter(self.unique_vars)
def __repr__(self):
return type(self).__name__ + "(" + repr(self.used) + ")"
# Merge variables which are never required to hold
# their values simultaneously.
# Variables which may be merged in this way
# will be renamed to share the same name.
def merge_disjoint_variables(blocks, namespace, memory_map):
record_variable_use(blocks, memory_map)
# Create data structure to track which pairs
# of variables are used simultaneously
var_use = VariableUseTracker()
# Iterate through each instruction in the whole program
for block in blocks:
for instr in block.instructions:
# Check if two or more variables are used during this instruction
instr_vars = list(instr.variables_used)
for i in range(len(instr_vars)):
var1 = instr_vars[i]
var_use.add_var(var1)
for j in range(i + 1, len(instr_vars)):
var2 = instr_vars[j]
# If so, mark the variables as unmergable in the array
var_use.mark_used(var1, var2)
# Look through the array for any mergable pairs of variables
for var1 in var_use.get_unique():
if memory_map_contains(memory_map, var1):
continue
for var2 in var_use.get_unique():
if var_use.are_used(var1, var2):
continue
# Rename instances of the first to match the second
rename_variable(blocks, var1, var2)
# Update the table to reflect the change
for var3 in namespace.names:
# Any variables which var1 couldn't merge
# with, var2 now can't merge with either.
if var_use.are_used(var1, var3):
var_use.mark_used(var2, var3)
# var1 can no longer merge with anything,
# because it doesn't exist anymore
var_use.mark_used(var1, var3)
var_use.mark_used(var1, var2)
# Removes blocks which are simply a trivial redirect to another block
def collapse_redundant_blocks(blocks):
redundant_blocks = []
for block in blocks:
# If block has at least one instruction, it's not redundant
if len(block.instructions) > 0:
continue
# If it has a condition jump, it's not redundant
if block.conditional is not None:
continue
# If it has no unconditional jump, we can't reroute it (and it's
# probably the end block, which we don't want to get rid of)
if block.next is None:
continue
# But if it is redundant, redirect blocks
# which jump to it to skip past it
for jmp in [*block.jumps_in]:
jmp.redirect(block.next.dest)
block.next.dest.unregister_jump_in(block.next)
# And add it to the list of redundant blocks,
# so we can remove it from the list later
redundant_blocks.append(block)
for block in redundant_blocks:
blocks.remove(block)
def mark_implicit_jumps(blocks):
for i in range(len(blocks) - 1):
if blocks[i].next is None:
continue
if blocks[i].next.dest is blocks[i + 1]:
blocks[i].next.implicit = True
# Assign named variables to memory locations
def assign_memory(blocks, initial_memory):
if len(blocks) == 0:
return
variables = []
for mem in initial_memory:
if len(variables) <= mem.loc:
variables += [None] * (mem.loc - len(variables) + 1)
variables[mem.loc] = mem.name
def get_addr(name):
nonlocal variables
if name not in variables:
if None in variables:
idx = variables.index(None)
variables[idx] = name
else:
variables.append(name)
return variables.index(name)
for block in blocks:
for inst in block.instructions:
if (isinstance(inst, hrmi.AbstractParameterisedInstruction)
and type(inst.loc) is str):
inst.loc = get_addr(inst.loc)
def main():
import argparse
parser = argparse.ArgumentParser(description="Compile .hc files")
parser.add_argument("input", default=None)
args = parser.parse_args()
tree = None
try:
if args.input is None:
tree = hcparse2.parse_file(sys.stdin)
else:
tree = hcparse2.parse_from_path(args.input)
except (LexerError, HCParseError) as e:
print(e, file=sys.stderr)
return 1
try:
initial_memory_map = tree.get_memory_map()
namespace = tree.get_namespace()
tree.validate_structure(namespace)
tree.create_blocks()
end_block = hrmi.Block()
tree.last_block.assign_next(end_block)
blocks = extract_blocks(tree)
# Ensure end block is at the end, if it's still present
if end_block in blocks:
blocks.remove(end_block)
blocks.append(end_block)
merge_disjoint_variables(blocks, namespace, initial_memory_map)
optimise_state_tracking(blocks, initial_memory_map)
except HCTypeError as e:
print(e, file=sys.stderr)
return 1
optimise_variable_needs(blocks, initial_memory_map)
collapse_redundant_blocks(blocks)
assign_memory(blocks, initial_memory_map)
mark_implicit_jumps(blocks)
print("-- HUMAN RESOURCE MACHINE PROGRAM --\n")
for block in blocks:
asm = block.to_asm()
if len(asm) > 0:
print(asm)
return 0
if __name__ == "__main__":
sys.exit(main())