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

Method analyze_function

src/analysis.cpp:267–351  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

265 if (instr.modifiesRt()) {
266 reg_states[rt].invalidate();
267 }
268 break;
269 }
270 return true;
271}
272
273bool N64Recomp::analyze_function(const N64Recomp::Context& context, const N64Recomp::Function& func,
274 const std::vector<rabbitizer::InstructionCpu>& instructions, N64Recomp::FunctionStats& stats) {
275 const Section* section = &context.sections[func.section_index];
276 std::optional<uint32_t> got_ram_addr = section->got_ram_addr;
277
278 // Create a state to track each register (r0 won't be used)
279 RegState reg_states[32] {};
280 std::vector<RegState> stack_states{};
281
282 // Look for jump tables
283 // A linear search through the func won't be accurate due to not taking control flow into account, but it'll work for finding jtables
284 for (const auto& instr : instructions) {
285 if (!analyze_instruction(instr, func, stats, reg_states, stack_states, got_ram_addr.has_value())) {
286 return false;
287 }
288 }
289
290 // Calculate absolute addresses for position-independent jump tables
291 if (got_ram_addr.has_value()) {
292 uint32_t got_rom_addr = got_ram_addr.value() + func.rom - func.vram;
293
294 for (size_t i = 0; i < stats.jump_tables.size(); i++) {
295 JumpTable& cur_jtbl = stats.jump_tables[i];
296
297 if (cur_jtbl.got_offset.has_value()) {
298 uint32_t got_word = byteswap(*reinterpret_cast<const uint32_t*>(&context.rom[got_rom_addr + cur_jtbl.got_offset.value()]));
299
300 cur_jtbl.vram += (section->ram_addr + got_word);
301 }
302 }
303 }
304
305 // Sort jump tables by their address
306 std::sort(stats.jump_tables.begin(), stats.jump_tables.end(),
307 [](const JumpTable& a, const JumpTable& b)
308 {
309 return a.vram < b.vram;
310 });
311
312 // Determine jump table sizes
313 for (size_t i = 0; i < stats.jump_tables.size(); i++) {
314 JumpTable& cur_jtbl = stats.jump_tables[i];
315 uint32_t end_address = (uint32_t)-1;
316 uint32_t entry_count = 0;
317 uint32_t vram = cur_jtbl.vram;
318
319 if (i < stats.jump_tables.size() - 1) {
320 end_address = stats.jump_tables[i + 1].vram;
321 }
322
323 // TODO this assumes that the jump table is in the same section as the function itself
324 cur_jtbl.rom = cur_jtbl.vram + func.rom - func.vram;

Callers

nothing calls this directly

Calls 2

analyze_instructionFunction · 0.85
byteswapFunction · 0.50

Tested by

no test coverage detected