MCPcopy Create free account
hub / github.com/N64Recomp/N64Recomp / recompile_function_impl

Function recompile_function_impl

src/recompilation.cpp:763–873  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

761 print_indent();
762 generator.emit_label(fmt::format("after_{}", link_branch_index));
763 }
764
765 return true;
766}
767
768template <typename GeneratorType>
769bool recompile_function_impl(GeneratorType& generator, const N64Recomp::Context& context, size_t func_index, std::ostream& output_file, std::span<std::vector<uint32_t>> static_funcs_out, bool tag_reference_relocs) {
770 const N64Recomp::Function& func = context.functions[func_index];
771 //fmt::print("Recompiling {}\n", func.name);
772 std::vector<rabbitizer::InstructionCpu> instructions;
773
774 generator.emit_function_start(func.name, func_index);
775
776 if (context.trace_mode) {
777 fmt::print(output_file,
778 " TRACE_ENTRY()\n",
779 func.name);
780 }
781
782 // Skip analysis and recompilation of this function is stubbed.
783 if (!func.stubbed) {
784 // Use a set to sort and deduplicate labels
785 std::set<uint32_t> branch_labels;
786 instructions.reserve(func.words.size());
787
788 auto hook_find = func.function_hooks.find(-1);
789 if (hook_find != func.function_hooks.end()) {
790 fmt::print(output_file, " {}\n", hook_find->second);
791 }
792
793 // First pass, disassemble each instruction and collect branch labels
794 uint32_t vram = func.vram;
795 for (uint32_t word : func.words) {
796 const auto& instr = instructions.emplace_back(byteswap(word), vram);
797
798 // If this is a branch or a direct jump, add it to the local label list
799 if (instr.isBranch() || instr.getUniqueId() == rabbitizer::InstrId::UniqueId::cpu_j) {
800 branch_labels.insert((uint32_t)instr.getBranchVramGeneric());
801 }
802
803 // Advance the vram address by the size of one instruction
804 vram += 4;
805 }
806
807 // Analyze function
808 N64Recomp::FunctionStats stats{};
809 if (!N64Recomp::analyze_function(context, func, instructions, stats)) {
810 fmt::print(stderr, "Failed to analyze {}\n", func.name);
811 output_file.clear();
812 return false;
813 }
814
815 std::unordered_set<uint32_t> jtbl_lw_instructions{};
816 std::unordered_set<uint32_t> jtbl_addu_gp_instructions{};
817
818 // Add jump table labels into function
819 for (const auto& jtbl : stats.jump_tables) {
820 jtbl_lw_instructions.insert(jtbl.lw_vram);

Callers 2

recompile_functionMethod · 0.85

Calls 6

process_instructionFunction · 0.70
byteswapFunction · 0.50
emit_function_startMethod · 0.45
emit_gotoMethod · 0.45
emit_labelMethod · 0.45
emit_function_endMethod · 0.45

Tested by

no test coverage detected