def rewrite_free_ptr(block): for i, instruction in enumerate(block.get_instructions()): opcode = instruction.opcode if opcode == "MLOAD" and \ instruction.reads[0] == 64: new_instruction = MoveInstruction("MOVE", ["$m"], instruction.writes, instruction.address) block.set_instruction(i, new_instruction) elif opcode == "MSTORE" and \ instruction.reads[0] == 64: new_instruction = MoveInstruction("MOVE", [instruction.reads[1]], ["$m"], instruction.address) block.set_instruction(i, new_instruction)
def __fold_constant(i, instruction, block): opcode = instruction.opcode constants = instruction.get_constants() if not constants: return inputs = [opcode] + constants writes = instruction.writes address = instruction.address if opcode in bin_ops: try: value = execute_binop(inputs) except ZeroDivisionError: return new_instruction = MoveInstruction("MOVE", [value], writes, address) block.set_instruction(i, new_instruction) elif opcode in mono_ops: value = execute_monop(inputs) new_instruction = MoveInstruction("MOVE", [value], writes, address) block.set_instruction(i, new_instruction)
def __rewrite_move(i, instruction, block): opcode = instruction.opcode reads = instruction.reads writes = instruction.writes address = instruction.address if opcode == "DIV" and reads[1] == 1: new_instruction = MoveInstruction("MOVE", [reads[0]], writes, address) block.set_instruction(i, new_instruction) elif opcode == "ADD" and reads[0] == 0: new_instruction = MoveInstruction("MOVE", [reads[1]], writes, address) block.set_instruction(i, new_instruction) elif opcode == "MUL" and reads[0] == 1: new_instruction = MoveInstruction("MOVE", [reads[1]], writes, address) block.set_instruction(i, new_instruction) elif opcode == "AND" and reads[0] == WORD_MASK: new_instruction = MoveInstruction("MOVE", [reads[1]], writes, address) block.set_instruction(i, new_instruction) elif opcode == "SUB" and reads[0] == reads[1]: new_instruction = MoveInstruction("MOVE", [0], writes, address) block.set_instruction(i, new_instruction)
def __remove_address_mask(i, instruction_0, instruction_1, block): if instruction_0.opcode not in {"CALLER", "ADDRESS"} or \ instruction_1.opcode != "AND": return w_0 = instruction_0.writes[0] w_1 = instruction_1.writes[0] reads_1 = instruction_1.reads if reads_1[0] != ADDRESS_MASK or reads_1[1] != w_0: return address = instruction_1.address instruction_1 = MoveInstruction("MOVE", [w_0], [w_1], address) block.set_instruction(i, instruction_1)