1 from crossplatform_ir_generation import CIRProgram, CIRInstruction, CIRLabel
3 PUSHING_INSTRUCTIONS_WITHOUT_SIDE_EFFECTS = set(
7 def push_drop_optimization(ir):
13 if isinstance(ir[i], CIRInstruction)\
14 and ir[i].instruction in PUSHING_INSTRUCTIONS_WITHOUT_SIDE_EFFECTS\
16 and isinstance(ir[i + 1], CIRInstruction)\
17 and ir[i + 1].instruction == 'drop':
23 # TODO This can be smarter
24 def unused_pop_optimization(ir):
30 # TODO Having symbols be a string starting with "sym" is a bit hacky
31 if isinstance(entry, CIRInstruction)\
32 and entry.instruction != 'pop'\
33 and isinstance(entry.argument, str)\
34 and entry.argument.startswith('sym'):
35 used_symbols.add(entry.argument)
38 if isinstance(entry, CIRInstruction) and entry.instruction == 'pop' and entry.argument not in used_symbols:
39 yield CIRInstruction(instruction='drop', argument=None)
43 def optimize(cir_program):
44 ir = cir_program.entry_list
46 ir = push_drop_optimization(ir)
47 ir = unused_pop_optimization(ir)
49 return CIRProgram(entry_list=tuple(ir))