	.arm
	.text
	.global	__cs_enter_context
	.type	__cs_enter_context, %function

__cs_enter_context:
        /* Parameter List (in order)
        * r0 = new stack pointer
        * r1 = first parameter to callback
        * r2 = callback function pointer
        * r3 = new stack limit (not used on most platforms)
        *
        * Return
        * r0 = continuation that returned control back to the caller (null if fallthrough)
        *
        * Variables
        * r4 = temporary for the old stack pointer */

        /* ========== Save State ========== */
        /* store programm counter for later return */
        push {lr}
        /* store callee saved registers */
        push {r4-r12,lr}
        /* ========== Save State ========== */

        /* Perform change to new stack */
        /* Keep old stack as second parameter to our callback function. */
        mov r4, sp
        /* Make sure that stack start is properly aligned. */
        and r0, r0, #-16
        /* Switch to new stack pointer. */
        mov sp, r0

        /* Perform actual function call, this will now be on the new stack */
        /* r0 = first parametor to callback (continuation) */
        /* r1 = second parameter to callback (arbetary pointer) */
        mov r0, r4
        blx r2

        /* Restore state of returned continuation. */
        /* To do so we first reset the stack pointer (which we get returned in r0). */
        /* After that we execute our standard restore procedere to pop the state from the stack. */
        mov sp, r0

        /* ========== Restore State ========== */
        /* restore callee saved registers */
        pop {r4-r12,lr}
        /* ========== Restore State ========== */

        /* Just return back from the call. */
        /* This is the end of a fiber, so we have no continuation. */
        eor r0, r0, r0
        pop {pc}
