ARM Prolog and Epilog

ARM Prolog and Epilog (Compact 7)

3/12/2014

ARM prolog and epilog code segments are required to implement Structured Exception Handling (SEH) for ARM microprocessors. The ARM prolog is a code segment that sets up the stack frame for a routine. The epilog removes the routine frame and returns from the routine.

The ARM prolog has three constituent parts. All parts are immediately contiguous, with no intervening instructions. When the prolog follows this guideline, the Virtual Unwinder can reverse-execute the prolog. The following list shows the three required parts of an ARM prolog.

  1. A sequence of zero or one instructions that push the incoming argument values from R0, R1, R2, and R3 to the argument home locations, and updates R13 to the new stack top.
    This sequence saves all permanent registers in descending order at the top of the stack frame, following any saved argument registers.
  2. A sequence of one or more instructions that set up the frame pointer, if one is to be established.
    The prolog copies stack pointer R13 to R12 before the initial register saves and uses R12 to compute the value of the frame pointer, R11.
  3. A sequence of zero or more instructions that allocate the remaining stack frame space for local variables, compiler-generated temporaries, and the argument build area by subtracting a 4-byte aligned offset from R13.
    If an offset is too wide to represent in the immediate field, the prolog uses the scratch register R12 to hold the offset. In this case, it sets the value in R12 with a different instruction.

The ARM epilog is a contiguous sequence of instructions that does the following:

  • Restores the saved permanent registers
  • Resets the stack pointer to its value on function entry
  • Returns to the function's calling function

The following list shows the guidelines for implementing an epilog:

  • All parts are immediately contiguous, with no intervening instructions.
  • If a frame pointer is set up, the epilog is a single instruction that uses the frame as the base and updates all nonvolatile registers, including the Program Counter and the stack.
    If no frame is set up, the epilog consists of a stack unlink, if needed, followed by an instruction to restore multiple registers or to copy the link register R14 to the program counter.
    If the function establishes a frame pointer (which has the value of R11 for an ARM epilog), the function must not modify the pointer value during the interval between the completion of the prolog's last instruction and the beginning of the execution of the first instruction of the epilog.
    If the function does not establish a frame pointer (which has the value of R13), the function must not modify the stack pointer during the interval between the completion of the prolog's last instruction and the beginning of the execution of the first instruction of the epilog.
  • In a routine that does not modify nonvolatile registers and is not interworking, the epilog contains only a copy of the link register to the program counter.
  • A routine whose last instruction is a branch to another routine can have an empty epilog if it does not modify nonvolatile registers.
  • The address contained in the stack pointer, which always has the value of R13, must never be greater than the lowest address of any unrestored register value in the Register Save Area.
    This prevents the preserved values of the permanent registers from being corrupted by a context switch or any other asynchronous event that might occur during the execution of a prolog or epilog.

ARM Prolog with frame in R11.

MOV    r12, r13        ; Save stack on entry if needed.
STMDB  r13!, {r0-r3}    ; As needed
STMDB  r13!, {r4-r12, r14}  ; As needed
SUB    r11, r12, #16    ; Sets frame past args
<stack link if needed>

ARM Prolog with no frame.

MOV    r12, r13
STMDB  r13!, {r0-r3}        ; As needed
STMDB  r13! {[r4-r12,]|[r13,]r14}  ; As needed
<stack link if needed>
<note: r12 is not used if the stack (r13) is the first register saved>

ARM Epilog with frame in R11.

<no stack unlink>
LDMDB  r11, {r4-r11, r13, r15}

ARM Epilog with no frame.

<stack unlink if needed>
LDMIA  r13, {r4-R11, r13, r15}

ARM Epilog with interworking return.

<stack unlink if needed>
LDMIA   r13, {r4-r11, r13, LR}
BX      LR
Show:
© 2016 Microsoft