JIT Branches - nonarkitten/monkeymonkeyjit GitHub Wiki

Generally, any control-flow operation should exit the block because we cannot gaurantee address validity of the jump target. This is especially tricky with the 68000 and it's wide variety of addressing modes that can be used with JSR and JMP opcodes. For the purposes of this version of monkeymonkey JIT, only two branching coditions will NOT exit the compiler:

  1. A reverse Bcc or DBcc to a point between the current entry point and the branch is presumed to be a loop. This will decrement our global cycle counter by the extra number of cycles to take the branch and re-execute the code between the branch target address and the branch. There is no exit on a cycle count underflow -- this will make our emulator slightly not cycle PRECISE even if it's still cycle exact.

  2. A forward branch that does not cross any unconditional opcodes such as JSR, JMP, RTS or RTD but can land on them and is within the range of the JIT's maximum block length size (TBD). This is commonly used as either exception-handling code, jump tables or to jump forward to the loop conditional (zero-time exit).

Under no circumstance can JSR, JMP, RTS or RTD even be included in the JIT code. Any Bcc or DBcc that does not meet the criteria will also terminate the JIT compiler.

Branch Cycle Counting

When cycle counting, we exclude, by default, any code that MIGHT be branched over. That is, the JIT block will only count the cycles of instructions that are KNOWN to execute.

Any forward branch is immediately follwed by a SUB[Q] opcode to deduct the number of cycles that it would have skipped if the branch had instead been taken. Since this SUB[Q] opcode is not executed if the branch is taken, the JIT block tally must not also include this cycle time.

Any reverse branch is immediately preceeded by a SUB[Q] opcode to deduct the number of cycles executed between the branch target address and the branch itself. Because this code must have been executedd at least once, the SUB[Q] will tally the cycle count and this should also be omitted from the final cycle count.