;Entry:
;R0  = instruction
;R7  = original instruction address + 8
;R14 = re-interpreted instruction address + 8

;i_BL
;----
;sets up R14 with the original return address and adds in flags

; BL xxx

ALIGN   32, 4
.i_BL
 STR     R7, [R13, #-4]!
 MOV     R7, R0, LSL #8			;R7=PC relative offset in top bits
 ADD     R7, R14, R7, ASR #6		;R7=BL destination address in JIT space
 SUB     R4, R7, #jit_code_start	;R4=actual address

 CMP     R4, #jit_appspace_end		;is it within Application space?
 MOVHS   R1, #&13			;NO, ADFF5013
 BHS     JIT_report_unimplemented	; |+ report illegal branch

 #if JIT_predictive_branching == 1
   PREDICT_BRANCH R7			;add to branch prediction table
 #endif
 
 STR     R7, [R13, #-4]!
 LDR     R7, [R13, #4]
 #if compile_RO > 400
   CODELET_BL 9
   TEQ     R1, #0			;reusing a codelet?
   LDREQ   R7, [R13], #8		;YES
   BEQ     _reused_codelet
   ADR     R5, _codelet

   LDMIA   R5!, {R1-R4}
   STMIA   R6!, {R1-R4}


   LDMIA   R5!, {R1-R3}
   STMIA   R6!, {R1-R3}
 #endif
 #if compile_RO == 400
   CODELET_BL 2
   TEQ     R1, #0			;reusing a codelet?
   LDREQ   R7, [R13], #8		;YES
   BEQ     _reused_codelet
   ADR     R5, _codelet
 #endif
 LDR     R7, [R13], #8

 LDMIA   R5, {R1-R2}
					;-------- 3rd instruction --------
 SUB     R5, R7, R6
 SUB     R5, R5, #8 + (1 * 4)		;correct for offset and prefetch
 BIC     R5, R5, #%111111 << 26
 ORR     R2, R2, R5, LSR #2		;B <BL address>
 STMIA   R6, {R1-R2}

 ._reused_codelet
 #if JIT_predictive_branching == 1
B       i_instruction_handled		;continue processing instructions
 #endif
 #if JIT_predictive_branching == 0
B       i_und_exit
 #endif


 ._codelet
 SUB     R14, R14, #jit_code_start	;R1
 #if compile_RO > 400
   STR     R0, _codelet-4		;R2 tmp1
   MRS     R0, CPSR			;R3
   BIC     R0, R0, #%1111 << 8		;R4 clear bits 8-11
   ORR     R0, R0, R0, LSL #20		;R1 IRQ/FIQ
   AND     R0, R0, #&FC000003		;R2 clear unwanted
   ORR     R14, R14, R0			;R3
   LDR     R0, _codelet-4		;R1 tmp1
 #endif
 DCD     B_blank			;R2
					;2 instructions (26bit)
					;9 instructions (32bit)