From bb51249b4c98157eea3dbe693982e4c9c0d0dac7 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 25 May 2021 19:12:16 -0600 Subject: [PATCH] Optimize around single instruction jump statements --- include/optimizer.hpp | 4 ++++ src/6502-c++.cpp | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/optimizer.hpp b/include/optimizer.hpp index be2517e..d4c0faa 100644 --- a/include/optimizer.hpp +++ b/include/optimizer.hpp @@ -30,6 +30,10 @@ constexpr bool is_opcode(const mos6502 &op, const auto... opcodes) { return ((op constexpr bool is_end_of_block(const auto &begin) { + if (begin->text.ends_with("__optimizable") || begin->op.value.ends_with("__optimizable")) { + return false; + } + if (begin->type == ASMLine::Type::Label) { return true; } return is_opcode(*begin, diff --git a/src/6502-c++.cpp b/src/6502-c++.cpp index 38030c1..dce34f9 100644 --- a/src/6502-c++.cpp +++ b/src/6502-c++.cpp @@ -363,7 +363,7 @@ void subtract_16_bit(const Personality &personality, void increment_16_bit(const Personality &personality, std::vector &instructions, int reg) { - std::string skip_high_byte_label = "skip_inc_high_byte_" + std::to_string(instructions.size()); + std::string skip_high_byte_label = "skip_inc_high_byte_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::inc, personality.get_register(reg)); instructions.emplace_back(mos6502::OpCode::bne, Operand(Operand::Type::literal, skip_high_byte_label)); instructions.emplace_back(mos6502::OpCode::inc, personality.get_register(reg + 1)); @@ -573,7 +573,7 @@ void translate_instruction(const Personality &personality, case AVR::OpCode::cpse: { instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); instructions.emplace_back(mos6502::OpCode::cmp, personality.get_register(o2_reg_num)); - std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::beq, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return; @@ -582,7 +582,7 @@ void translate_instruction(const Personality &personality, instructions.emplace_back( mos6502::OpCode::lda, Operand(o2.type, fixup_8bit_literal("$" + std::to_string(1 << (to_int(o2.value)))))); instructions.emplace_back(mos6502::OpCode::bit, personality.get_register(o1_reg_num)); - std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::beq, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return; @@ -591,7 +591,7 @@ void translate_instruction(const Personality &personality, instructions.emplace_back(mos6502::OpCode::lda, Operand(o2.type, fixup_8bit_literal("$" + std::to_string(1 << (to_int(o2.value.c_str())))))); instructions.emplace_back(mos6502::OpCode::bit, personality.get_register(o1_reg_num)); - std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::bne, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return; @@ -602,7 +602,7 @@ void translate_instruction(const Personality &personality, return; } else if (o1.value == ".+2") { // assumes 6502 'borrow' for Carry flag instead of carry, so bcc instead of bcs - std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::bne, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return; @@ -664,7 +664,7 @@ void translate_instruction(const Personality &personality, if (o1.value == ".+2") { // assumes 6502 'borrow' for Carry flag instead of carry, so bcc instead of bcs - std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::bcc, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return; @@ -729,7 +729,7 @@ void translate_instruction(const Personality &personality, case AVR::OpCode::brsh: { if (o1.value == ".+2") { // assumes 6502 'borrow' for Carry flag instead of carry, so bcs instead of bcc - std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::bcs, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return; @@ -772,7 +772,7 @@ void translate_instruction(const Personality &personality, case AVR::OpCode::breq: { if (o1.value == ".+2") { // assumes 6502 'borrow' for Carry flag instead of carry, so bcc instead of bcs - std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()) + "__optimizable"; instructions.emplace_back(mos6502::OpCode::beq, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return;