Delayed and non-delayed branches
On SuperH the instruction after a delayed branch, its delay slot, is executed before the branch takes effect. In
jsr @r1 followed by mov #4,r4, the mov runs first, so the callee sees r4 = 4, and the call returns to the
instruction after the delay slot: PR is set to the address of the branch plus 4. The branch target is counted from the branch
itself (PC + 4), not from the slot.
The delayed instructions are bra, bsr, braf, bsrf, jmp, jsr, rts, rte, and (SH-2 and later) the delayed
conditional branches bt/s and bf/s. bt and bf are the non-delayed forms: they have no delay slot. The delay slot of
bt/s and bf/s is executed whether or not the branch is taken, unlike a MIPS branch likely. trapa and the exceptions have no delay slot.
The explorer marks the instruction in a delay slot ("delay slot") so that code reads in the order the processor runs it, and writes "The next instruction is the delay slot" in the note of the branch.
What may not be in a delay slot
Some instructions in a delay slot raise a slot illegal instruction exception (vector offset VBR + 0x100, exception code 0x1A0 on the SH-4 and SH-4A). The lists are in the manuals:
- SH-3, SH-3E, SH3-DSP (Software Manual Rev. 4.00, description of
Delay_Slotand ofLDC):BF,BT,BRA,BSR,JMP,JSR,RTS,RTE,TRAPA,BF/S,BT/S,BRAF,BSRF; and “if theLDC Rm,SRinstruction orLDC.L @Rm+,SRinstruction is located in a delayed slot … it is acknowledged as an illegal slot instruction”. - SH-4 (ST/Hitachi SH-4 core manual and the Renesas SH-4 manual) and SH-4A (Software Manual Rev. 1.50, “Slot Illegal Instruction Exception”): the instructions that modify PC:
JMP,JSR,BRA,BRAF,BSR,BSRF,RTS,RTE,BT,BF,BT/S,BF/S,TRAPA,LDC Rm,SR,LDC.L @Rm+,SRand, on the SH-4A,ICBIandPREFI. Also: a PC-relative MOV andMOVAin a delay slot; a privileged instruction (LDC,STC,RTE,LDTLB,SLEEP, except theLDC/STCforms that access GBR) decoded in user mode in a delay slot; and the undefined instruction0xFFFD(“Decoding of an undefined instruction in a delay slot”, with delayed branch instructionsJMP JSR BRA BRAF BSR BSRF RTS RTE BT/S BF/S).
The explorer marks every one of these that is a table instruction with the flag slot illegal: the branches, trapa, the PC-relative mov and mova, ldc Rm,SR and ldc.l @Rm+,SR on the SH-3 and later (not on the SH-1 and SH-2: the lists of those chips were not read), and icbi and prefi on the SH-4A. It does not mark a privileged instruction in a slot in user mode (it does not know the mode), nor 0xFFFD (an undefined word is illegal anyway). A word that is not an instruction at all, in a delay slot, is an ordinary illegal instruction. An instruction following a conditional delayed branch that is not taken is still a delay slot instruction (SH-4A manual).
Things to know when reading
- The usual compiler idiom is to fill the slot with a useful instruction (
rtsthen the lastmov.l @r15+,r14), or withnop. - A branch whose slot is another delayed branch is a program error; the disassembly shows both and marks the second illegal.
- The explorer decodes every 2-byte aligned address on its own: whether a given word is "in a slot" depends on the word just before it being a delayed branch.
Not described
Known, not yet covered
- Reference: the SH-2A has instructions without delay slot (
jsr/n,rts/n,rtv/n); not decoded.
Unknown
- Reference: the exact list of slot-illegal instructions per chip (see above).