code:
/*
  AngelCode Scripting Library
  Copyright (c) 2003-2020 Andreas Jonsson

  This software is provided 'as-is', without any express or implied
  warranty. In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you
     must not claim that you wrote the original software. If you use
     this software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.

  2. Altered source versions must be plainly marked as such, and
     must not be misrepresented as being the original software.

  3. This notice may not be removed or altered from any source
     distribution.

  The original version of this library can be located at:
  http://www.angelcode.com/angelscript/

  Andreas Jonsson
  andreas@angelcode.com
*/

/*
   Assembly routines for the ARM call convention
   Written by Fredrik Ehnbom in June 2009

   Adapted to GNUC by darktemplar216 in September 2009

   Modified by Lasse Oorni for 8-byte stack alignment in May 2012

   The assembler routines for Linux were written by Carlos Luna in December 2012
*/

#if !defined(AS_MAX_PORTABILITY)

#if defined(__arm__) || defined(__ARM__) || defined(I3D_ARCH_ARM)

#if !defined(__linux__) || defined(__ANDROID__) || defined(ANDROID) || defined(__SOFTFP__) || defined(__ARM_PCS)

/* iOS, Android, Marmalade, and Linux with soft-float ABI goes here */

.global armFunc
.global armFuncR0
.global armFuncR0R1
.global armFuncObjLast
.global armFuncR0ObjLast

/* --------------------------------------------------------------------------------------------*/
armFunc:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r8, #0

    beq     nomoreargs

    /* Load the first 4 arguments into r0-r3 */
    cmp     r7, #4
    ldrge   r0, [r6],#4
    cmp     r7, #2*4
    ldrge   r1, [r6],#4
    cmp     r7, #3*4
    ldrge   r2, [r6],#4
    cmp     r7, #4*4
    ldrge   r3, [r6],#4
    ble     nomoreargs

    /* Load the rest of the arguments onto the stack */
    sub     r7, r7, #4*4      /* skip the 4 registers already loaded into r0-r3 */
    add     r8, r7, #4        /* ensure 8-byte stack alignment */
    bic     r8, r8, #4
    sub     sp, sp, r8
    mov     r12, sp           /* copy size != frame size, so store frame start sp */
stackargsloop:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargsloop
    mov     sp, r12
nomoreargs:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

/* --------------------------------------------------------------------------------------------*/
armFuncObjLast:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r8, #0

    mov     r0, r3          /* objlast. might get overwritten */
    mov     r5, r3          /* objlast to temp reg */

    beq     nomoreargsarmFuncObjLast

    /* Load the first 4 arguments into r0-r3 */
    cmp     r7, #4
    ldrge   r0, [r6],#4
    cmp     r7, #2*4
    ldrge   r1, [r6],#4
    movlt   r1, r5
    cmp     r7, #3*4
    ldrge   r2, [r6],#4
    movlt   r2, r5
    cmp     r7, #4*4
    ldrge   r3, [r6],#4
    movlt   r3, r5
    blt     nomoreargsarmFuncObjLast

    /* Load the rest of the arguments onto the stack */
    sub     r7, r7, #4*4    /* skip the 4 registers already loaded into r0-r3 */
    add     r8, r7, #8      /* account for the objlast pointer, ensure 8-byte stack alignment */
    bic     r8, r8, #4
    str     r5, [sp,#-4]    /* store the objlast on stack, twice in case we adjusted alignment */
    str     r5, [sp,#-8]
    sub     sp, sp, r8      /* adjust frame */
    cmp     r7, #0          /* we may also have come here with no extra params */
    beq     nomoreargsarmFuncObjLast
    mov     r12, sp         /* copy size != frame size, so store frame start sp */
stackargslooparmFuncObjLast:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncObjLast
    mov     sp, r12
nomoreargsarmFuncObjLast:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

/* --------------------------------------------------------------------------------------------*/
armFuncR0ObjLast:
    stmdb   sp!, {r4-r8, lr}
    ldr     r5, [sp,#6*4]   /* objlast to temp reg */

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r8, #0

    mov     r0, r3      /* r0 explicitly set */
    mov     r1, r5      /* objlast.  might get overwritten */

    beq     nomoreargsarmFuncR0ObjLast

    /* Load the first 3 arguments into r1-r3 */
    cmp     r7, #1*4
    ldrge   r1, [r6],#4
    cmp     r7, #2*4
    ldrge   r2, [r6],#4
    movlt   r2, r5
    cmp     r7, #3*4
    ldrge   r3, [r6],#4
    movlt   r3, r5
    blt     nomoreargsarmFuncR0ObjLast

    /* Load the rest of the arguments onto the stack */
    sub     r7, r7, #3*4    /* skip the 3 registers already loaded into r1-r3 */
    add     r8, r7, #8      /* account for the objlast pointer, ensure 8-byte stack alignment */
    bic     r8, r8, #4
    str     r5, [sp,#-4]    /* store the objlast on stack, twice in case we adjusted alignment */
    str     r5, [sp,#-8]
    sub     sp, sp, r8      /* adjust frame */
    cmp     r7, #0          /* we may also have come here with no extra params */
    beq     nomoreargsarmFuncR0ObjLast
    mov     r12, sp         /* copy size != frame size, so store frame start sp */
stackargslooparmFuncR0ObjLast:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncR0ObjLast
    mov     sp, r12
nomoreargsarmFuncR0ObjLast:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

/* --------------------------------------------------------------------------------------------*/
armFuncR0:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r8, #0

    mov     r0, r3  /* r0 explicitly set */

    beq     nomoreargsarmFuncR0

    /* Load the first 3 arguments into r1-r3 */
    cmp     r7, #1*4
    ldrge   r1, [r6],#4
    cmp     r7, #2*4
    ldrge   r2, [r6],#4
    cmp     r7, #3*4
    ldrge   r3, [r6],#4
    ble     nomoreargsarmFuncR0

    /* Load the rest of the arguments onto the stack */
    sub     r7, r7, #3*4    /* skip the 3 registers already loaded into r1-r3 */
    add     r8, r7, #4      /* ensure 8-byte stack alignment */
    bic     r8, r8, #4
    sub     sp, sp, r8
    mov     r12, sp         /* copy size != frame size, so store frame start sp */
stackargslooparmFuncR0:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncR0
    mov     sp, r12
nomoreargsarmFuncR0:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

/* --------------------------------------------------------------------------------------------*/
armFuncR0R1:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r8, #0

    mov     r0, r3          /* r0 explicitly set */
    ldr     r1, [sp, #6*4]  /* r1 explicitly set too */

    beq     nomoreargsarmFuncR0R1

    /* Load the first 2 arguments into r2-r3 */
    cmp     r7, #1*4
    ldrge   r2, [r6],#4
    cmp     r7, #2*4
    ldrge   r3, [r6],#4
    ble     nomoreargsarmFuncR0R1

    /* Load the rest of the arguments onto the stack */
    sub     r7, r7, #2*4    /* skip the 2 registers already loaded into r2-r3 */
    add     r8, r7, #4      /* ensure 8-byte stack alignment */
    bic     r8, r8, #4
    sub     sp, sp, r8
    mov     r12, sp         /* copy size != frame size, so store frame start sp */
stackargslooparmFuncR0R1:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncR0R1
    mov     sp, r12
nomoreargsarmFuncR0R1:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

/* --------------------------------------------------------------------------------------------*/
#elif defined(__linux__) && !defined(__SOFTFP__) && !defined(__ARM_PCS)

/* The Linux with hard-float ABI code goes here */

/* These codes are suitable for armeabi + vfp  / armeabihf */
/* when using armeabi + vfp, please set C_FLAGS -mfloat-abi=softfp -mfpu=vfp */
/* using armeabihf, please set C_FLAGS -mfloat-abi=hard -mfpu=vfpv3-d16 */

/* if you prefer to run in ARM mode, please add -marm to C_FLAGS */
/* while using thumb mode, please add -mthumb -Wa,-mimplicit-it=thumb */

/* SP is a multiple of 8 when control first enters a program.*/
/* This places an obligation on authors of low level OS, RTOS, and runtime library code to align SP at all points */
/* at which control first enters a body of (AAPCS-conforming) code. (please read "ARM IHI 0046B" document)*/

.section .text

    .align 2        /* Align the function code to a 4-byte (2^n) word boundary. */
#if defined(__thumb__) || defined(__thumb2__)
    .thumb
    .syntax unified
#else
    .arm            /* Use ARM instructions instead of Thumb.*/
#endif
    .globl armFunc  /* Make the function globally accessible.*/
armFunc:
    push    {r4-r8, r10, r11, lr}   /* sp must be 8-byte alignment for ABI compliance, so the pushed registers must be even */

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */

    /* Load float and double args into d0-d7 and s0-s15 */
    add       r10, r6, #272 /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov       r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargs

    /* Load the first 4 arguments into r0-r3 */
    cmp     r7, #4
    ldrge   r0, [r6]
    cmp     r7, #8
    ldrge   r1, [r6, #4]
    cmp     r7, #12
    ldrge   r2, [r6, #8]
    cmp     r7, #16
    ldrge   r3, [r6, #12]

stackargs:
    ldr     r5, [r6, #268]  /* Load stack size into r5 */
    movs    r7, r5          /* Load stack size into r7, checking for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargs

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargsloop:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargsloop
    mov     sp, r12

nomoreargs:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    vstmia.64 r10, {d0-d7}   /* Copy contents of registers d0-d7 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

/* --------------------------------------------------------------------------------------------*/
    .align 2        /* Align the function code to a 4-byte (2^n) word boundary. */
#if defined(__thumb__) || defined(__thumb2__)
    .thumb
    .syntax unified
#else
    .arm            /* Use ARM instructions instead of Thumb.*/
#endif
    .globl armFuncObjLast       /* Make the function globally accessible.*/
armFuncObjLast:
    push {r4-r8, r10, r11, lr}  /* We�re storing r11 just to keep the stack aligned to an 8 byte boundary */

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */

    mov     r0, r3          /* objlast. might get overwritten */
    mov     r5, #0          /* This will hold an offset of #4 only if objlast couldn�t be placed into an "r" register */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsFuncObjLast

    mov     r5, r3          /* store objlast in r5 temporarily */

    /* Load the first 4 arguments into r0-r3 */
    cmp     r7, #4
    ldrge   r0, [r6]
    cmp     r7, #8
    ldrge   r1, [r6,#4]
    movlt   r1, r5
    cmp     r7, #12
    ldrge   r2, [r6,#8]
    movlt   r2, r5
    cmp     r7, #16
    ldrge   r3, [r6,#12]
    movlt   r3, r5
    movlt   r5, #0                  /* If objlast got placed into a register, r5 = 0 */
    blt     stackargsFuncObjLast    /* If objlast got placed into a register, go to stackargsFuncObjLast */

    str     r5, [r6, #12]           /* Put objlast in r6 + 12 */
    mov     r5, #4                  /* Set r5 with an offset of #4, so objlast can be loaded into the stack */

stackargsFuncObjLast:
    ldr     r7, [r6, #268]  /* Load stack size into r7 */
    add     r7, r7, r5      /* Add the offset placed in r5 (could be #0 or #4) */
    cmp     r7, #0          /* Check for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncObjLast

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r5      /* r6 = r6 - r5 (r5 can be #0 or #4) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncObjLast:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncObjLast
    mov     sp, r12

nomoreargsarmFuncObjLast:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop   {r4-r8, r10,r11, pc}

/* ------------------------------------------------------------------------------------------- */
    .align 2        /* Align the function code to a 4-byte (2^n) word boundary. */
#if defined(__thumb__) || defined(__thumb2__)
    .thumb
    .syntax unified
#else
    .arm            /* Use ARM instructions instead of Thumb.*/
#endif
    .globl armFuncR0ObjLast     /* Make the function globally accessible.*/
armFuncR0ObjLast:
    push    {r4-r8, r10, r11, lr}

    ldr     r5, [sp,#32]   /* objlast to temp reg */

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */

    mov     r0, r3      /* r0 explicitly set */
    mov     r1, r5      /* objlast.  might get overwritten */
    mov     r5, #0      /* This will hold an offset of #4 or #8 if objlast or one arg couldn�t be placed into an "r" register */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsFuncR0ObjLast

    mov     r5, r1          /* store objlast in r5 temporarily */

    /* Load the first 3 arguments into r1-r3 */
    cmp     r7, #4
    ldrge   r1, [r6]
    cmp     r7, #8
    ldrge   r2, [r6,#4]
    movlt   r2, r5
    cmp     r7, #12
    ldrge   r3, [r6,#8]
    movlt   r3, r5
    movlt   r5, #0                  /* If objlast got placed into a register, r5 = 0 */
    blt     stackargsFuncR0ObjLast  /* If objlast got placed into a register, go to stackargsFuncR0ObjLast */

    cmp     r7, #16                 /* Else if we have one last arg set the offset accordingly and store the arg in the array */
    ldrge   r7, [r6, #12]
    strge   r7, [r6, #8]

    str     r5, [r6, #12]           /* Put objlast in r6 + 12 */
    mov     r5, #0

    movge   r5, #4                  /* Set r5 with an offset of #4 if there�s one last arg that couldn�t be placed in r registers */
    add     r5, r5, #4              /* Set r5 with an offset of + #4, so objlast can be loaded into the stack */

stackargsFuncR0ObjLast:
    ldr     r7, [r6, #268]  /* Load stack size into r7 */
    add     r7, r7, r5      /* Add the offset placed in r5 (could be #0 or #4) */
    cmp     r7, #0          /* Check for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncR0ObjLast

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r5      /* r6 = r6 - r5 (r5 can be #0 or #4) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncR0ObjLast:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncR0ObjLast
    mov     sp, r12

nomoreargsarmFuncR0ObjLast:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

/* ------------------------------------------------------------------------------------------- */
    .align 2        /* Align the function code to a 4-byte (2^n) word boundary. */
#if defined(__thumb__) || defined(__thumb2__)
    .thumb
    .syntax unified
#else
    .arm            /* Use ARM instructions instead of Thumb.*/
#endif
    .globl armFuncR0        /* Make the function globally accessible.*/
armFuncR0:
    push {r4-r8, r10, r11, lr}

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r11, #0 /* This will hold an offset of #4 only if the last arg that should have been placed into an "r" reg needs to go to the stack */
    mov     r0, r3  /* r0 explicitly set */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsarmFuncR0

    /* Load the first 3 arguments into r1-r3 */
    cmp     r7, #4
    ldrge   r1, [r6]
    cmp     r7, #8
    ldrge   r2, [r6, #4]
    cmp     r7, #12
    ldrge   r3, [r6, #8]
    cmp     r7, #16
    movge   r11, #4         /* If there is still one arg to be placed, set the offset in r11 to #4 */

stackargsarmFuncR0:
    ldr     r5, [r6, #268]  /* Load stack size into r5 */
    add     r5, r11         /* Add the offset placed in r11 (could be #0 or #4) */
    movs    r7, r5          /* Load stack size into r7, checking for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncR0

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r11     /* r6 = r6 - r11 (r11 can be #0 or #4) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncR0:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncR0
    mov     sp, r12

nomoreargsarmFuncR0:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

/* ------------------------------------------------------------------------------------------- */
    .align 2        /* Align the function code to a 4-byte (2^n) word boundary. */
#if defined(__thumb__) || defined(__thumb2__)
    .thumb
    .syntax unified
#else
    .arm            /* Use ARM instructions instead of Thumb.*/
#endif
    .globl armFuncR0R1      /* Make the function globally accessible.*/
armFuncR0R1:
    push {r4-r8, r10, r11, lr}

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r11, #0 /* This will hold an offset of #4 or #8 only if the last arg (or last 2 args) that should have been placed into "r" regs need to go to the stack */

    mov     r0, r3          /* r0 explicitly set */
    ldr     r1, [sp, #32]   /* r1 explicitly set too */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r2-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsarmFuncR0R1

    /* Load the first 2 arguments into r2-r3 */
    cmp     r7, #4
    ldrge   r2, [r6]
    cmp     r7, #8
    ldrge   r3, [r6, #4]
    cmp     r7, #12
    movge   r11, #4         /* If there is a third arg to be placed, set the offset in r11 to #4 */
    cmp     r7, #16
    movge   r11, #8         /* If there is a fourth arg to be placed, set the offset in r11 to #8 */
    ldrlt   r7, [r6, #8]    /* Else copy the third arg to the correct place in the array */
    strlt   r7, [r6, #12]

stackargsarmFuncR0R1:
    ldr     r5, [r6, #268]  /* Load stack size into r5 */
    add     r5, r11         /* Add the offset placed in r11 (could be #0 or #4 or #8) */
    movs    r7, r5          /* Load stack size into r7, checking for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncR0R1

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r11     /* r6 = r6 - r11 (r11 can be #0 or #4 or #8) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncR0R1:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncR0R1
    mov     sp, r12

nomoreargsarmFuncR0R1:
#if defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_4__)
    mov    lr, pc   /* older ARM didn't support blx */
    mov    pc, r4
#else
    blx     r4
#endif
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

#endif /* hard float abi */

#endif /* arm */

#if defined(__linux__) && defined(__ELF__)
/* ref: http://hardened.gentoo.org/gnu-stack.xml
   ref: https://wiki.gentoo.org/wiki/Hardened/GNU_stack_quickstart */
.section .note.GNU-stack,"",%progbits
#endif

#endif /* !AS_MAX_PORTABILITY */


code:
;
;  AngelCode Scripting Library
;  Copyright (c) 2003-2014 Andreas Jonsson
;
;  This software is provided 'as-is', without any express or implied
;  warranty. In no event will the authors be held liable for any
;  damages arising from the use of this software.
;
;  Permission is granted to anyone to use this software for any
;  purpose, including commercial applications, and to alter it and
;  redistribute it freely, subject to the following restrictions:
;
;  1. The origin of this software must not be misrepresented; you
;     must not claim that you wrote the original software. If you use
;     this software in a product, an acknowledgment in the product
;     documentation would be appreciated but is not required.
;
;  2. Altered source versions must be plainly marked as such, and
;     must not be misrepresented as being the original software.
;
;  3. This notice may not be removed or altered from any source
;     distribution.
;
;  The original version of this library can be located at:
;  http://www.angelcode.com/angelscript/
;
;  Andreas Jonsson
;  andreas@angelcode.com
;

; Assembly routines for the ARM call convention used for Windows CE
; Written by Fredrik Ehnbom in June 2009

; MSVC currently doesn't support inline assembly for the ARM platform
; so this separate file is needed.

; Compile with Microsoft ARM assembler (armasm)
; http://msdn.microsoft.com/en-us/library/hh873190.aspx

    AREA	|.rdata|, DATA, READONLY
    EXPORT armFunc
    EXPORT armFuncR0
    EXPORT armFuncR0R1
    EXPORT armFuncObjLast
    EXPORT armFuncR0ObjLast

    AREA	|.text|, CODE, ARM, ALIGN=3

    ALIGN   8
armFunc PROC
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  ; arg table
    movs    r7, r1  ; arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  ; function address
    mov     r8, #0

    beq     |nomoreargs|

    ; Load the first 4 arguments into r0-r3
    cmp     r7, #4
    ldrge   r0, [r6],#4
    cmp     r7, #2*4
    ldrge   r1, [r6],#4
    cmp     r7, #3*4
    ldrge   r2, [r6],#4
    cmp     r7, #4*4
    ldrge   r3, [r6],#4
    ble     |nomoreargs|

    ; Load the rest of the arguments onto the stack
    sub     r7, r7, #4*4    ; skip the 4 registers already loaded into r0-r3
    sub     sp, sp, r7
    mov     r8, r7
|stackargsloop|
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     |stackargsloop|
|nomoreargs|
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}
    ENDP

    ALIGN   8
armFuncObjLast PROC
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  ; arg table
    movs    r7, r1  ; arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  ; function address
    mov     r8, #0

    mov     r0, r3          ; objlast. might get overwritten
    str     r3, [sp, #-4]!  ; objlast again.

    beq     |nomoreargs@armFuncObjLast|

    ; Load the first 4 arguments into r0-r3
    cmp     r7, #4
    ldrge   r0, [r6],#4
    cmp     r7, #2*4
    ldrge   r1, [r6],#4
    ldrlt   r1, [sp]
    cmp     r7, #3*4
    ldrge   r2, [r6],#4
    ldrlt   r2, [sp]
    cmp     r7, #4*4
    ldrge   r3, [r6],#4
    ldrlt   r3, [sp]
    ble     |nomoreargs@armFuncObjLast|

    ; Load the rest of the arguments onto the stack
    sub     r7, r7, #4*4    ; skip the 4 registers already loaded into r0-r3
    sub     sp, sp, r7
    mov     r8, r7
|stackargsloop@armFuncObjLast|
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     |stackargsloop@armFuncObjLast|
|nomoreargs@armFuncObjLast|
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    add     sp, sp, #4
    ldmia   sp!, {r4-r8, pc}
    ENDP

    ALIGN   8
armFuncR0ObjLast PROC
    stmdb   sp!, {r4-r8, lr}
    ldr     r7, [sp,#6*4]
    str     r7, [sp,#-4]!

    mov     r6, r0  ; arg table
    movs    r7, r1  ; arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  ; function address
    mov     r8, #0

    mov     r0, r3      ; r0 explicitly set
    ldr     r1, [sp]    ; objlast.  might get overwritten

    beq     |nomoreargs@armFuncR0ObjLast|

    ; Load the first 3 arguments into r1-r3
    cmp     r7, #1*4
    ldrge   r1, [r6],#4
    cmp     r7, #2*4
    ldrge   r2, [r6],#4
    ldrlt   r2, [sp]
    cmp     r7, #3*4
    ldrge   r3, [r6],#4
    ldrlt   r3, [sp]
    ble     |nomoreargs@armFuncR0ObjLast|

    ; Load the rest of the arguments onto the stack
    sub     r7, r7, #3*4    ; skip the 3 registers already loaded into r1-r3
    sub     sp, sp, r7
    mov     r8, r7
|stackargsloop@armFuncR0ObjLast|
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     |stackargsloop@armFuncR0ObjLast|
|nomoreargs@armFuncR0ObjLast|
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    add     sp, sp, #4
    ldmia   sp!, {r4-r8, pc}
    ENDP

    ALIGN   8
armFuncR0 PROC
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  ; arg table
    movs    r7, r1  ; arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  ; function address
    mov     r8, #0

    mov     r0, r3  ; r0 explicitly set

    beq     |nomoreargs@armFuncR0|

    ; Load the first 3 arguments into r1-r3
    cmp     r7, #1*4
    ldrge   r1, [r6],#4
    cmp     r7, #2*4
    ldrge   r2, [r6],#4
    cmp     r7, #3*4
    ldrge   r3, [r6],#4
    ble     |nomoreargs@armFuncR0|

    ; Load the rest of the arguments onto the stack
    sub     r7, r7, #3*4    ; skip the 3 registers already loaded into r1-r3
    sub     sp, sp, r7
    mov     r8, r7
|stackargsloop@armFuncR0|
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     |stackargsloop@armFuncR0|
|nomoreargs@armFuncR0|
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}
    ENDP

    ALIGN   8
armFuncR0R1 PROC
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  ; arg table
    movs    r7, r1  ; arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  ; function address
    mov     r8, #0

    mov     r0, r3          ; r0 explicitly set
    ldr     r1, [sp, #6*4]  ; r1 explicitly set too

    beq     |nomoreargs@armFuncR0R1|

    ; Load the first 2 arguments into r2-r3
    cmp     r7, #1*4
    ldrge   r2, [r6],#4
    cmp     r7, #2*4
    ldrge   r3, [r6],#4
    ble     |nomoreargs@armFuncR0R1|

    ; Load the rest of the arguments onto the stack
    sub     r7, r7, #2*4    ; skip the 2 registers already loaded into r2-r3
    sub     sp, sp, r7
    mov     r8, r7
|stackargsloop@armFuncR0R1|
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     |stackargsloop@armFuncR0R1|
|nomoreargs@armFuncR0R1|
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}
    ENDP

    END

repo_name: Antidote/openrpgeditor

license: mit


code:
/*
  AngelCode Scripting Library
  Copyright (c) 2003-2015 Andreas Jonsson

  This software is provided 'as-is', without any express or implied
  warranty. In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you
     must not claim that you wrote the original software. If you use
     this software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.

  2. Altered source versions must be plainly marked as such, and
     must not be misrepresented as being the original software.

  3. This notice may not be removed or altered from any source
     distribution.

  The original version of this library can be located at:
  http://www.angelcode.com/angelscript/

  Andreas Jonsson
  andreas@angelcode.com
*/

/*
   Assembly routines for the Playstation Vita SNC call convention.

   This code was adapted from as_callfunc_arm_gcc (ARM, Linux hard float) by Brandon Bare on October 2014.
*/

#if !defined(AS_MAX_PORTABILITY)

#ifdef __psp2__

.syntax unified
.cpu cortex-a9
.fpu neon

.section .text.armCallFunc
.balign 2
.thumb
.thumb_func

.align 2

.global armFunc
.global armFuncR0
.global armFuncR0R1
.global armFuncObjLast
.global armFuncR0ObjLast

.type armFunc, %function
.type armFuncR0, %function
.type armFuncR0R1, %function
.type armFuncObjLast, %function
.type armFuncR0ObjLast, %function

/* --------------------------------------------------------------------------------------------*/
armFunc:
    .fnstart

    push    {r4-r8, r10, r11, lr}   /* sp must be 8-byte alignment for ABI compliance, so the pushed registers must be even */

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */

    /* Load float and double args into d0-d7 and s0-s15 */
    add       r10, r6, #272 /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov       r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargs

    /* Load the first 4 arguments into r0-r3 */
    cmp     r7, #4

    it ge
    ldrge   r0, [r6]
    cmp     r7, #8

    it ge
    ldrge   r1, [r6, #4]
    cmp     r7, #12

    it ge
    ldrge   r2, [r6, #8]
    cmp     r7, #16

    it ge
    ldrge   r3, [r6, #12]

stackargs:
    ldr     r5, [r6, #268]  /* Load stack size into r5 */
    movs    r7, r5          /* Load stack size into r7, checking for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargs

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargsloop:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargsloop
    mov     sp, r12

nomoreargs:
    blx     r4
    add     sp, sp, r8
    vstmia.64 r10, {d0-d7}   /* Copy contents of registers d0-d7 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

    .fnend

/* --------------------------------------------------------------------------------------------*/
armFuncObjLast:
    .fnstart

    push {r4-r8, r10, r11, lr}  /* We�re storing r11 just to keep the stack aligned to an 8 byte boundary */

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */

    mov     r0, r3          /* objlast. might get overwritten */
    mov     r5, #0          /* This will hold an offset of #4 only if objlast couldn�t be placed into an "r" register */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsFuncObjLast

    mov     r5, r3          /* store objlast in r5 temporarily */

    /* Load the first 4 arguments into r0-r3 */
    cmp     r7, #4

    it ge
    ldrge   r0, [r6]
    cmp     r7, #8

    it ge
    ldrge   r1, [r6,#4]

    it lt
    movlt   r1, r5
    cmp     r7, #12

    it ge
    ldrge   r2, [r6,#8]

    it lt
    movlt   r2, r5
    cmp     r7, #16

    it ge
    ldrge   r3, [r6,#12]

    ittt lt
    movlt   r3, r5
    movlt   r5, #0                  /* If objlast got placed into a register, r5 = 0 */
    blt     stackargsFuncObjLast    /* If objlast got placed into a register, go to stackargsFuncObjLast */

    str     r5, [r6, #12]           /* Put objlast in r6 + 12 */
    mov     r5, #4                  /* Set r5 with an offset of #4, so objlast can be loaded into the stack */

stackargsFuncObjLast:
    ldr     r7, [r6, #268]  /* Load stack size into r7 */
    add     r7, r7, r5      /* Add the offset placed in r5 (could be #0 or #4) */
    cmp     r7, #0          /* Check for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncObjLast

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r5      /* r6 = r6 - r5 (r5 can be #0 or #4) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncObjLast:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncObjLast
    mov     sp, r12

nomoreargsarmFuncObjLast:
    blx     r4
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop   {r4-r8, r10,r11, pc}

    .fnend

/* --------------------------------------------------------------------------------------------*/
armFuncR0ObjLast:
    .fnstart

    push    {r4-r8, r10, r11, lr}

    ldr     r5, [sp,#32]   /* objlast to temp reg */

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */

    mov     r0, r3      /* r0 explicitly set */
    mov     r1, r5      /* objlast.  might get overwritten */
    mov     r5, #0      /* This will hold an offset of #4 or #8 if objlast or one arg couldn�t be placed into an "r" register */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsFuncR0ObjLast

    mov     r5, r1          /* store objlast in r5 temporarily */

    /* Load the first 3 arguments into r1-r3 */
    cmp     r7, #4

    it ge
    ldrge   r1, [r6]
    cmp     r7, #8

    it ge
    ldrge   r2, [r6,#4]

    it lt
    movlt   r2, r5
    cmp     r7, #12

    it ge
    ldrge   r3, [r6,#8]

    ittt lt
    movlt   r3, r5
    movlt   r5, #0                  /* If objlast got placed into a register, r5 = 0 */
    blt     stackargsFuncR0ObjLast  /* If objlast got placed into a register, go to stackargsFuncR0ObjLast */

    cmp     r7, #16                 /* Else if we have one last arg set the offset accordingly and store the arg in the array */

    itt ge
    ldrge   r7, [r6, #12]
    strge   r7, [r6, #8]

    str     r5, [r6, #12]           /* Put objlast in r6 + 12 */
    mov     r5, #0

    it ge
    movge   r5, #4                  /* Set r5 with an offset of #4 if there�s one last arg that couldn�t be placed in r registers */
    add     r5, r5, #4              /* Set r5 with an offset of + #4, so objlast can be loaded into the stack */

stackargsFuncR0ObjLast:
    ldr     r7, [r6, #268]  /* Load stack size into r7 */
    add     r7, r7, r5      /* Add the offset placed in r5 (could be #0 or #4) */
    cmp     r7, #0          /* Check for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncR0ObjLast

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r5      /* r6 = r6 - r5 (r5 can be #0 or #4) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncR0ObjLast:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncR0ObjLast
    mov     sp, r12

nomoreargsarmFuncR0ObjLast:
    blx     r4
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

    .fnend

/* --------------------------------------------------------------------------------------------*/
armFuncR0:
    .fnstart

    push {r4-r8, r10, r11, lr}

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r11, #0 /* This will hold an offset of #4 only if the last arg that should have been placed into an "r" reg needs to go to the stack */
    mov     r0, r3  /* r0 explicitly set */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r0-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsarmFuncR0

    /* Load the first 3 arguments into r1-r3 */
    cmp     r7, #4

    it ge
    ldrge   r1, [r6]
    cmp     r7, #8

    it ge
    ldrge   r2, [r6, #4]
    cmp     r7, #12

    it ge
    ldrge   r3, [r6, #8]
    cmp     r7, #16

    it ge
    movge   r11, #4         /* If there is still one arg to be placed, set the offset in r11 to #4 */

stackargsarmFuncR0:
    ldr     r5, [r6, #268]  /* Load stack size into r5 */
    add     r5, r11         /* Add the offset placed in r11 (could be #0 or #4) */
    movs    r7, r5          /* Load stack size into r7, checking for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncR0

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r11     /* r6 = r6 - r11 (r11 can be #0 or #4) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncR0:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncR0
    mov     sp, r12

nomoreargsarmFuncR0:
    blx     r4
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

    .fnend

/* --------------------------------------------------------------------------------------------*/
armFuncR0R1:
    .fnstart

    push {r4-r8, r10, r11, lr}

    mov     r6, r0  /* arg table */
    movs    r7, r1  /* arg size (also set the condition code flags so that we detect if there are no arguments) */
    mov     r4, r2  /* function address */
    mov     r11, #0 /* This will hold an offset of #4 or #8 only if the last arg (or last 2 args) that should have been placed into "r" regs need to go to the stack */

    mov     r0, r3          /* r0 explicitly set */
    ldr     r1, [sp, #32]   /* r1 explicitly set too */

    /* Load float and double args into d0-d7 and s0-s15 (r10 holds pointer to first float value) */
    add     r10, r6, #272   /* r10 (r6 + 272) points to the first value for the VFP registers */
    mov     r8, #0
    vldmia.64 r10, {d0-d7}  /* Load contents starting at r10 into registers d0-d7 */

    /* If there are no arguments to set into r2-r3 */
    /* go check if there are arguments for the stack */
    beq     stackargsarmFuncR0R1

    /* Load the first 2 arguments into r2-r3 */
    cmp     r7, #4

    it ge
    ldrge   r2, [r6]
    cmp     r7, #8

    it ge
    ldrge   r3, [r6, #4]
    cmp     r7, #12

    it ge
    movge   r11, #4         /* If there is a third arg to be placed, set the offset in r11 to #4 */

    cmp     r7, #16

    it ge
    movge   r11, #8         /* If there is a fourth arg to be placed, set the offset in r11 to #8 */

    itt lt
    ldrlt   r7, [r6, #8]    /* Else copy the third arg to the correct place in the array */
    strlt   r7, [r6, #12]

stackargsarmFuncR0R1:
    ldr     r5, [r6, #268]  /* Load stack size into r5 */
    add     r5, r11         /* Add the offset placed in r11 (could be #0 or #4 or #8) */
    movs    r7, r5          /* Load stack size into r7, checking for 0 args */

    /* If there are no args for the stack, branch */
    beq     nomoreargsarmFuncR0R1

    /* Load the rest of the arguments onto the stack */
    /* Ensure 8-byte stack alignment */
    mov     r8, sp
    sub     sp, sp, r7
    add     r6, r6, #16     /* Set r6 to point to the first arg to be placed on the stack */

    sub     r12, sp, #8
    sub     r6, r6, r11     /* r6 = r6 - r11 (r11 can be #0 or #4 or #8) */
    bic     r12, r12, #7    /* thumb mode couldn't support "bic  sp, sp, #7" instruction */
    sub     r8, r8, r12
    mov     sp, r12         /* copy size != frame size, so store frame start sp, r12(ip) is not callee saved register */

stackargslooparmFuncR0R1:
    ldr     r5, [r6], #4
    subs    r7, r7, #4
    str     r5, [sp], #4
    bne     stackargslooparmFuncR0R1
    mov     sp, r12

nomoreargsarmFuncR0R1:
    blx     r4
    add     sp, sp, r8
    vstmia.64   r10, {d0-d7}    /* Copy contents of registers d0-d10 to the address stored in r10 */

    pop {r4-r8, r10, r11, pc}

    .fnend

#endif

#endif /* !AS_MAX_PORTABILITY */


code:
/*
  AngelCode Scripting Library
  Copyright (c) 2003-2015 Andreas Jonsson

  This software is provided 'as-is', without any express or implied
  warranty. In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you
     must not claim that you wrote the original software. If you use
     this software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.

  2. Altered source versions must be plainly marked as such, and
     must not be misrepresented as being the original software.

  3. This notice may not be removed or altered from any source
     distribution.

  The original version of this library can be located at:
  http://www.angelcode.com/angelscript/

  Andreas Jonsson
  andreas@angelcode.com
*/

// Assembly routines for the ARM call convention
// Written by Fredrik Ehnbom in June 2009

// Adapted to GNUC by darktemplar216 in September 2009
// Small fixed to work under XCode GCC by Gilad Novik in October 2009

#if !defined(AS_MAX_PORTABILITY)

#if defined(__arm__) || defined(__ARM__)

.align 2
.globl _armFunc
.globl _armFuncR0
.globl _armFuncR0R1
.globl _armFuncObjLast
.globl _armFuncR0ObjLast

_armFunc:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  // arg table
    movs    r7, r1  // arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  // function address
    mov     r8, #0

    beq     nomoreargs

    // Load the first 4 arguments into r0-r3
    cmp     r7, #4
    ldrge   r0, [r6],#4
    cmp     r7, #2*4
    ldrge   r1, [r6],#4
    cmp     r7, #3*4
    ldrge   r2, [r6],#4
    cmp     r7, #4*4
    ldrge   r3, [r6],#4
    ble     nomoreargs

    // Load the rest of the arguments onto the stack
    sub     r7, r7, #4*4    // skip the 4 registers already loaded into r0-r3
    sub     sp, sp, r7
    mov     r8, r7
stackargsloop:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargsloop
nomoreargs:
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

_armFuncObjLast:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  // arg table
    movs    r7, r1  // arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  // function address
    mov     r8, #0

    mov     r0, r3          // objlast. might get overwritten
    str     r3, [sp, #-4]!  // objlast again.

    beq     nomoreargsarmFuncObjLast

    // Load the first 4 arguments into r0-r3
    cmp     r7, #4
    ldrge   r0, [r6],#4
    cmp     r7, #2*4
    ldrge   r1, [r6],#4
    ldrlt   r1, [sp]
    cmp     r7, #3*4
    ldrge   r2, [r6],#4
    ldrlt   r2, [sp]
    cmp     r7, #4*4
    ldrge   r3, [r6],#4
    ldrlt   r3, [sp]
    ble     nomoreargsarmFuncObjLast

    // Load the rest of the arguments onto the stack
    sub     r7, r7, #4*4    // skip the 4 registers already loaded into r0-r3
    sub     sp, sp, r7
    mov     r8, r7
stackargslooparmFuncObjLast:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncObjLast
nomoreargsarmFuncObjLast:
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    add     sp, sp, #4
    ldmia   sp!, {r4-r8, pc}

_armFuncR0ObjLast:
    stmdb   sp!, {r4-r8, lr}
    ldr     r7, [sp,#6*4]
    str     r7, [sp,#-4]!

    mov     r6, r0  // arg table
    movs    r7, r1  // arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  // function address
    mov     r8, #0

    mov     r0, r3      // r0 explicitly set
    ldr     r1, [sp]    // objlast.  might get overwritten

    beq     nomoreargsarmFuncR0ObjLast

    // Load the first 3 arguments into r1-r3
    cmp     r7, #1*4
    ldrge   r1, [r6],#4
    cmp     r7, #2*4
    ldrge   r2, [r6],#4
    ldrlt   r2, [sp]
    cmp     r7, #3*4
    ldrge   r3, [r6],#4
    ldrlt   r3, [sp]
    ble     nomoreargsarmFuncR0ObjLast

    // Load the rest of the arguments onto the stack
    sub     r7, r7, #3*4    // skip the 3 registers already loaded into r1-r3
    sub     sp, sp, r7
    mov     r8, r7
stackargslooparmFuncR0ObjLast:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncR0ObjLast
nomoreargsarmFuncR0ObjLast:
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    add     sp, sp, #4
    ldmia   sp!, {r4-r8, pc}

_armFuncR0:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  // arg table
    movs    r7, r1  // arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  // function address
    mov     r8, #0

    mov     r0, r3  // r0 explicitly set

    beq     nomoreargsarmFuncR0

    // Load the first 3 arguments into r1-r3
    cmp     r7, #1*4
    ldrge   r1, [r6],#4
    cmp     r7, #2*4
    ldrge   r2, [r6],#4
    cmp     r7, #3*4
    ldrge   r3, [r6],#4
    ble     nomoreargsarmFuncR0

    // Load the rest of the arguments onto the stack
    sub     r7, r7, #3*4    // skip the 3 registers already loaded into r1-r3
    sub     sp, sp, r7
    mov     r8, r7
stackargslooparmFuncR0:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncR0
nomoreargsarmFuncR0:
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

_armFuncR0R1:
    stmdb   sp!, {r4-r8, lr}
    mov     r6, r0  // arg table
    movs    r7, r1  // arg size (also set the condition code flags so that we detect if there are no arguments)
    mov     r4, r2  // function address
    mov     r8, #0

    mov     r0, r3          // r0 explicitly set
    ldr     r1, [sp, #6*4]  // r1 explicitly set too

    beq     nomoreargsarmFuncR0R1

    // Load the first 2 arguments into r2-r3
    cmp     r7, #1*4
    ldrge   r2, [r6],#4
    cmp     r7, #2*4
    ldrge   r3, [r6],#4
    ble     nomoreargsarmFuncR0R1

    // Load the rest of the arguments onto the stack
    sub     r7, r7, #2*4    // skip the 2 registers already loaded into r2-r3
    sub     sp, sp, r7
    mov     r8, r7
stackargslooparmFuncR0R1:
    ldr     r5, [r6], #4
    str     r5, [sp], #4
    subs    r7, r7, #4
    bne     stackargslooparmFuncR0R1
nomoreargsarmFuncR0R1:
    sub     sp, sp, r8
    blx     r4
    add     sp, sp, r8
    ldmia   sp!, {r4-r8, pc}

#endif

#endif /* !AS_MAX_PORTABILITY */


code:
//
//  AngelCode Scripting Library
//  Copyright (c) 2025 Andreas Jonsson
//
//  This software is provided 'as-is', without any express or implied
//  warranty. In no event will the authors be held liable for any
//  damages arising from the use of this software.
//
//  Permission is granted to anyone to use this software for any
//  purpose, including commercial applications, and to alter it and
//  redistribute it freely, subject to the following restrictions:
//
//  1. The origin of this software must not be misrepresented// you
//     must not claim that you wrote the original software. If you use
//     this software in a product, an acknowledgment in the product
//     documentation would be appreciated but is not required.
//
//  2. Altered source versions must be plainly marked as such, and
//     must not be misrepresented as being the original software.
//
//  3. This notice may not be removed or altered from any source
//     distribution.
//
//  The original version of this library can be located at:
//  http://www.angelcode.com/angelscript/
//
//  Andreas Jonsson
//  andreas@angelcode.com
//

//
// Implements the E2K calling convention
//
// Author: Denis Drakhnia <numas13@gmail.com>
//

#if !defined(AS_MAX_PORTABILITY)
#ifdef AS_E2K

    .file "as_callfunc_e2k.S"
    .text
    .globl CallFunctionE2K
    .type CallFunctionE2K, #function
    .align 8
CallFunctionE2K:
    {
        setwd       wsz=16, nfx=1, dbl=0
        setbn       rsz=7, rbs=4, rcur=0
        movtd,0     %r0, %ctpr1
        cmpedb,1    %r2, 0, %pred0
        shld,2      1, 37, %g17                 // lsr.vlc
        disp        %ctpr2, 1f
    }
    {
        getsp,0     %r5, %r5                    // allocate space on stack for args or return value
        cmpedb,1    %r6, 0, %pred1              // is value returned in memory?
        ord,2       %r2, %g17, %g16             // lsr
        disp        %ctpr3, 0f
    }
    {
        rwd,0       %g16, %lsr
        ldd,2,sm    %r1, 0x00, %b[14]           // loop prologue
        ldd,3,sm    %r1, 0x08, %b[12]
        ldd,5,sm    %r1, 0x10, %b[10]
    }
    {
        ldd,0,sm    %r1, 0x18, %b[8]
        ldd,2,sm    %r1, 0x20, %b[6]
        ldd,3,sm    %r1, 0x28, %b[4]
        ldd,5,sm    %r1, 0x30, %b[2]
    }
    {
        addd,1      %r5, 0, %g16
        addd,2      %r1, 0x38, %g17
        ct          %ctpr2 ? %pred0             // 1f, do not copy arguments to memory
        nop 1 // wait %lsr
    }
 0:
    {
        alc         alcf=1, alct=1
        abn         abnf=1, abnt=1
        addd,1      %g17, 8, %g17
        ldd,2       %g17, 0, %b[0]
        addd,4      %g16, 8, %g16
        std,5       %b[14], %g16, 0
        ct          %ctpr3 ? #NOT_LOOP_END      // 0b
    }
1:
    {
        setwd       wsz=8, nfx=1, dbl=0
        setbn       rsz=3, rbs=4, rcur=0
        ldd,0       %r1, 0x00, %r8              // load arguments to registers
        ldd,2       %r1, 0x08, %r9
        ldd,3       %r1, 0x10, %r10
        ldd,5       %r1, 0x18, %r11
    }
    {
        ldd,0       %r1, 0x20, %b[4]
        ldd,2       %r1, 0x28, %b[5]
        ldd,3       %r1, 0x30, %b[6]
        ldd,5       %r1, 0x38, %b[7]
        call        %ctpr1, wbs=4               // %r0
    }
    {
        shld,0      1, 37, %g16                 // lsr.vlc
        std,2,sm    %b[0], %r3, 0x00 ? %pred1
        std,5,sm    %b[1], %r3, 0x08 ? %pred1
        return      %ctpr3
    }
    {
        ord,0       %r4, %g16, %g16             // lsr
        std,2,sm    %b[2], %r3, 0x10 ? %pred1
        std,5,sm    %b[3], %r3, 0x18 ? %pred1
        disp        %ctpr1, 0f
    }
    {
        rwd,0       %g16, %lsr
        std,2,sm    %b[4], %r3, 0x20 ? %pred1
        std,5,sm    %b[5], %r3, 0x28 ? %pred1
    }
    {
        std,2,sm    %b[6], %r3, 0x30 ? %pred1
        std,5,sm    %b[7], %r3, 0x38 ? %pred1
    }
    {
        setwd       wsz=16, nfx=1, dbl=0
        setbn       rsz=7, rbs=4, rcur=0
        ldw,0,sm    %r5, 0x00, %r22             // %b[14] loop prologue
        ldw,2,sm    %r5, 0x04, %r20             // %b[12]
        ldw,3,sm    %r5, 0x08, %r18             // %b[10]
        ldw,5,sm    %r5, 0x0c, %r16             // %b[8]
        ct          %ctpr3 ? %pred1             // return
    }
    {
        addd,1      %r5, 0x1c, %r5
        ldw,2,sm    %r5, 0x10, %b[6]
        ldw,3,sm    %r5, 0x14, %b[4]
        ldw,5,sm    %r5, 0x18, %b[2]
#if __iset__ > 5
        nop 3
#else
        nop 1
#endif
    }
0:
    {
        alc         alcf=1, alct=1
        abn         abnf=1, abnt=1
        addd,1      %r3, 4, %r3
        stw,2       %b[14], %r3, 0
        addd,3      %r5, 4, %r5
        ldw,5,sm    %r5, 0, %b[0]
        ct          %ctpr1 ? #NOT_LOOP_END      // 0b
    }
    {
        ct          %ctpr3                      // return
    }
    .size CallFunctionE2K, .-CallFunctionE2K

#endif /* AS_E2K */
#endif /* !AS_MAX_PORTABILITY */


code:
/*
   AngelCode Scripting Library
   Copyright (c) 2025 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/

/*
 * Implements the E2K calling convention
 *
 * Author: Denis Drakhnia <numas13@gmail.com>
 */

#include "as_config.h"

#ifndef AS_MAX_PORTABILITY
#ifdef AS_E2K

#include "as_scriptengine.h"
#include "as_texts.h"
#include "as_context.h"

BEGIN_AS_NAMESPACE

#define E2K_MAX_ARGS 64

struct E2KArguments {
    asDWORD *m_args;
    int m_index = 0;

    E2KArguments(asDWORD *args): m_args(args) {}

    asDWORD *ptr() {
        return &m_args[m_index];
    }

    asDWORD takeDWord() {
        return m_args[m_index++];
    }

    asQWORD takeQWord() {
        asQWORD lo = takeDWord();
        asQWORD hi = takeDWord();
        return (hi << 32) | lo;
    }

    asQWORD take(int count) {
        switch (count) {
        case 1: return takeDWord();
        case 2: return takeQWord();
        default:
            asASSERT(0 && "implement me");
            return 0;
        }
    }
};

struct E2KBuffer {
    int m_count = 0;
    asQWORD m_buffer[E2K_MAX_ARGS];

    const asQWORD *ptr() const {
        return m_buffer;
    }

    int count() const {
        return m_count;
    }

    void align() {
        m_count += m_count & 1;
    }

    void push(asQWORD arg) {
        asASSERT(m_count < E2K_MAX_ARGS);
        m_buffer[m_count++] = arg;
    }

    void push_object(void *parm, int size) {
        int c = (size + 7) / 8;
        asASSERT(m_count + c <= E2K_MAX_ARGS);
        memcpy(&m_buffer[m_count], parm, size);
        m_count += c;
    }
};

extern "C" void CallFunctionE2K(asFUNCTION_t func, const asQWORD *args,
        int argsCount, void *retPointer, int retSize, int stackSize, bool returnInMemory);

void CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr,
    void *obj, asDWORD *args, void *retPointer, asQWORD *retQW, void *secondObject)
{
    asCScriptEngine *engine = context->m_engine;
    asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
    const asCDataType &retType = descr->returnType;
    asFUNCTION_t func = sysFunc->func;
    int callConv = sysFunc->callConv;
    E2KArguments parms(args);
    E2KBuffer buffer;
    bool retComplex = retType.IsObject() && (retType.GetTypeInfo()->flags & COMPLEX_RETURN_MASK);

    if (sysFunc->hostReturnInMemory && retComplex) {
        // The return is made in memory.
        buffer.push((asPWORD) retPointer);
    }

    if (obj) {
        asFUNCTION_t *vftable;
        switch (callConv) {
        case ICC_VIRTUAL_THISCALL:
        case ICC_VIRTUAL_THISCALL_RETURNINMEM:
#ifndef AS_NO_THISCALL_FUNCTOR_METHOD
        case ICC_VIRTUAL_THISCALL_OBJFIRST:
        case ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM:
        case ICC_VIRTUAL_THISCALL_OBJLAST:
        case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
#endif
            vftable = *((asFUNCTION_t**) obj);
            func = vftable[FuncPtrToUInt(asFUNCTION_t(func)) >> 3];
            break;
        }
    }

    switch (callConv) {
    case ICC_CDECL_OBJFIRST:
    case ICC_THISCALL:
    case ICC_VIRTUAL_THISCALL:
    case ICC_CDECL_OBJFIRST_RETURNINMEM:
    case ICC_THISCALL_RETURNINMEM:
    case ICC_VIRTUAL_THISCALL_RETURNINMEM:
#ifndef AS_NO_THISCALL_FUNCTOR_METHOD
    case ICC_THISCALL_OBJLAST:
    case ICC_VIRTUAL_THISCALL_OBJLAST:
    case ICC_THISCALL_OBJLAST_RETURNINMEM:
    case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
#endif
        buffer.push((asPWORD) obj);
        break;
#ifndef AS_NO_THISCALL_FUNCTOR_METHOD
    case ICC_THISCALL_OBJFIRST:
    case ICC_VIRTUAL_THISCALL_OBJFIRST:
    case ICC_THISCALL_OBJFIRST_RETURNINMEM:
    case ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM:
        buffer.push((asPWORD) obj);
        buffer.push((asPWORD) secondObject);
        break;
#endif
    }

    for (int i = 0; i < descr->parameterTypes.GetLength(); ++i) {
        const asCDataType& parmType = descr->parameterTypes[i];

        if (parmType.GetTokenType() == ttQuestion) {
            // The variable args are really two, one pointer and one type id.
            buffer.push(parms.takeQWord());
            buffer.push(parms.takeDWord());
        } else if (parmType.IsPrimitive() || parmType.IsReference() || parmType.IsObjectHandle()) {
            // Primitive types and references are single slot values.
            buffer.push(parms.take(parmType.GetSizeOnStackDWords()));
        } else {
            // An object is being passed by value.
            if ((parmType.GetTypeInfo()->flags & COMPLEX_MASK)) {
                // Copy the address of the object.
                buffer.push(parms.takeQWord());
            } else {
                // Copy the value of the object.
                int size = parmType.GetSizeInMemoryBytes();
                void *parm = *(void **) parms.ptr();
                if (size > 8) {
                    buffer.align();
                    buffer.push_object(parm, size);
                    buffer.align();
                } else {
                    buffer.push_object(parm, size);
                }
                // Skip pointer.
                parms.takeQWord();
                // Delete the original memory.
                engine->CallFree(parm);
            }
        }
    }

    switch (callConv) {
    case ICC_CDECL_OBJLAST:
    case ICC_CDECL_OBJLAST_RETURNINMEM:
        buffer.push((asPWORD) obj);
        break;
#ifndef AS_NO_THISCALL_FUNCTOR_METHOD
    case ICC_THISCALL_OBJLAST:
    case ICC_THISCALL_OBJLAST_RETURNINMEM:
    case ICC_VIRTUAL_THISCALL_OBJLAST:
    case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
        buffer.push((asPWORD) secondObject);
        break;
#endif
    }

    int stackSize = buffer.count() * (sizeof(asQWORD) / sizeof(asDWORD));
    int retSize = sysFunc->hostReturnSize;
    bool returnInMemory = sysFunc->hostReturnInMemory && !retComplex;

    if (returnInMemory) {
        // Returned on the user stack and copied to retPointer.
        retSize = retType.GetSizeInMemoryDWords();
        if (retSize > stackSize) {
            stackSize = retSize;
        }
    } else {
        // Returned in registers and copied to retQW.
        asASSERT(retSize <= RETURN_VALUE_MAX_SIZE);
        retPointer = retQW;
    }

    stackSize = (stackSize * sizeof(asDWORD) + 15) & ~15;
    CallFunctionE2K(func, buffer.ptr(), buffer.count(), retPointer, retSize, -stackSize, returnInMemory);
}

END_AS_NAMESPACE

#endif // AS_E2K
#endif // AS_MAX_PORTABILITY


code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2015 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_callfunc_mips.cpp
//
// These functions handle the actual calling of system functions
//
// This version is MIPS specific and was originally written
// by Manu Evans in April, 2006 for Playstation Portable (PSP)
//
// Support for Linux with MIPS was added by Andreas Jonsson in April, 2015
//

#include "as_config.h"

#ifndef AS_MAX_PORTABILITY
#ifdef AS_MIPS

#include "as_callfunc.h"
#include "as_scriptengine.h"
#include "as_texts.h"
#include "as_tokendef.h"
#include "as_context.h"

#include <stdio.h>
#include <stdlib.h>
#if !defined(AS_ANDROID)
#include <regdef.h>
#endif

BEGIN_AS_NAMESPACE

#if defined(__linux__) && defined(_ABIO32)

// The MIPS ABI used by Linux is implemented here
// (Tested on CI20 MIPS Creator with Debian Linux)
//
// ref: SYSTEM V
//      APPLICATION BINARY INTERFACE
//      MIPS RISC Processor
//      http://math-atlas.sourceforge.net/devel/assembly/mipsabi32.pdf
//
// ref: MIPS Instruction Reference
//      http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html

union SFloatRegs
{
	union { double d0; struct { float f0; asDWORD dummy0; };};
	union { double d1; struct { float f1; asDWORD dummy1; };};
} ;

extern "C" asQWORD mipsFunc(asUINT argSize, asDWORD *argBuffer, void *func, SFloatRegs &floatRegs);
asDWORD GetReturnedFloat();
asQWORD GetReturnedDouble();

asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/, void *secondObject)
{
	asCScriptEngine *engine = context->m_engine;
	asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
	int callConv = sysFunc->callConv;

	asQWORD retQW = 0;

	void *func = (void*)sysFunc->func;
	void **vftable;

	asDWORD argBuffer[128]; // Ought to be big enough
	asASSERT( sysFunc->paramSize < 128 );

	asDWORD argOffset = 0;

	SFloatRegs floatRegs;
	asDWORD floatOffset = 0;

	// If the application function returns the value in memory then
	// the first argument must be the pointer to that memory
	if( sysFunc->hostReturnInMemory )
	{
		asASSERT( retPointer );
		argBuffer[argOffset++] = (asPWORD)retPointer;
	}

	if( callConv == ICC_CDECL_OBJFIRST || callConv == ICC_CDECL_OBJFIRST_RETURNINMEM ||
		callConv == ICC_THISCALL || callConv == ICC_THISCALL_RETURNINMEM ||
		callConv == ICC_VIRTUAL_THISCALL || callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM ||
		callConv == ICC_THISCALL_OBJFIRST || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
		callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM ||
		callConv == ICC_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
		callConv == ICC_THISCALL_OBJLAST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
	{
		// Add the object pointer as the first argument
		argBuffer[argOffset++] = (asPWORD)obj;
	}

	if( callConv == ICC_THISCALL_OBJFIRST || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
		callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM )
	{
		// Add the second object pointer
		argBuffer[argOffset++] = (asPWORD)secondObject;
	}

	int spos = 0;
	for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
	{
		asCDataType &paramType = descr->parameterTypes[n];
		if( paramType.IsObject() && !paramType.IsObjectHandle() && !paramType.IsReference() )
		{
			if( paramType.GetTypeInfo()->flags & COMPLEX_MASK )
			{
				// The object is passed by reference
				argBuffer[argOffset++] = args[spos++];
			}
			else
			{
				// Ensure 8byte alignment for classes that need it
				if( (paramType.GetTypeInfo()->flags & asOBJ_APP_CLASS_ALIGN8) && (argOffset & 1) )
					argOffset++;

				// Copy the object's memory to the buffer
				memcpy(&argBuffer[argOffset], *(void**)(args+spos), paramType.GetSizeInMemoryBytes());
				// Delete the original memory
				engine->CallFree(*(char**)(args+spos));
				spos++;
				argOffset += paramType.GetSizeInMemoryDWords();
			}
		}
		else if( paramType.GetTokenType() == ttQuestion )
		{
			// Copy both pointer and type id
			argBuffer[argOffset++] = args[spos++];
			argBuffer[argOffset++] = args[spos++];
		}
		else
		{
			// The first 2 floats or doubles are loaded into the float registers.
			// Actually this is only done if they are the first arguments to the function,
			// but it doesn't cause any harm to load them into the registers even if they
			// won't be used so we don't need to check if they really are the first args.
			if( floatOffset == 0 )
			{
				if( paramType.GetTokenType() == ttFloat )
					floatRegs.f0 = *reinterpret_cast<float*>(&args[spos]);
				else if( paramType.GetTokenType() == ttDouble )
					floatRegs.d0 = *reinterpret_cast<double*>(&args[spos]);
				floatOffset++;
			}
			else if( floatOffset == 1 )
			{
				if( paramType.GetTokenType() == ttFloat )
					floatRegs.f1 = *reinterpret_cast<float*>(&args[spos]);
				else if( paramType.GetTokenType() == ttDouble )
					floatRegs.d1 = *reinterpret_cast<double*>(&args[spos]);
				floatOffset++;
			}

			// Copy the value directly
			if( paramType.GetSizeOnStackDWords() > 1 )
			{
				// Make sure the argument is 8byte aligned
				if( argOffset & 1 )
					argOffset++;
				*reinterpret_cast<asQWORD*>(&argBuffer[argOffset]) = *reinterpret_cast<asQWORD*>(&args[spos]);
				argOffset += 2;
				spos += 2;
			}
			else
				argBuffer[argOffset++] = args[spos++];
		}
	}

	if( callConv == ICC_CDECL_OBJLAST || callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
	{
		// Add the object pointer as the last argument
		argBuffer[argOffset++] = (asPWORD)obj;
	}

	if( callConv == ICC_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
		callConv == ICC_THISCALL_OBJLAST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
	{
		// Add the second object pointer
		argBuffer[argOffset++] = (asPWORD)secondObject;
	}

	switch( callConv )
	{
	case ICC_CDECL:
	case ICC_CDECL_RETURNINMEM:
	case ICC_STDCALL:
	case ICC_STDCALL_RETURNINMEM:
	case ICC_CDECL_OBJLAST:
	case ICC_CDECL_OBJLAST_RETURNINMEM:
	case ICC_CDECL_OBJFIRST:
	case ICC_CDECL_OBJFIRST_RETURNINMEM:
	case ICC_THISCALL:
	case ICC_THISCALL_RETURNINMEM:
	case ICC_THISCALL_OBJFIRST:
	case ICC_THISCALL_OBJFIRST_RETURNINMEM:
	case ICC_THISCALL_OBJLAST:
	case ICC_THISCALL_OBJLAST_RETURNINMEM:
		retQW = mipsFunc(argOffset*4, argBuffer, func, floatRegs);
		break;

	case ICC_VIRTUAL_THISCALL:
	case ICC_VIRTUAL_THISCALL_RETURNINMEM:
	case ICC_VIRTUAL_THISCALL_OBJFIRST:
	case ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM:
	case ICC_VIRTUAL_THISCALL_OBJLAST:
	case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
		// Get virtual function table from the object pointer
		vftable = *(void***)obj;
		retQW = mipsFunc(argOffset*4, argBuffer, vftable[asPWORD(func)>>2], floatRegs);
		break;
	default:
		context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
	}

	// If the return is a float value we need to get the value from the FP register
	if( sysFunc->hostReturnFloat )
	{
		if( sysFunc->hostReturnSize == 1 )
			*(asDWORD*)&retQW = GetReturnedFloat();
		else
			retQW = GetReturnedDouble();
	}

	return retQW;
}

asDWORD GetReturnedFloat()
{
	asDWORD f;

	asm("swc1 $f0, %0\n" : "=m"(f));

	return f;
}

asQWORD GetReturnedDouble()
{
	asQWORD d = 0;

	asm("sdc1 $f0, %0\n" : "=m"(d));

	return d;
}

// asQWORD mipsFunc(asUINT argSize, asDWORD *argBuffer, void *func, SFloatRegs &floatRegs);
// $2,$3                   $4                $5               $6                $7
asm(
"	.text\n"
//"	.align 2\n"
"	.cfi_startproc\n"
"	.global mipsFunc\n"
"	.ent	mipsFunc\n"
"mipsFunc:\n"
//"	.frame	$fp,64,$31		# vars= 0, regs= 0/0, args= 0, gp= 0\n"
//"	.mask	0x00000000,0\n"
//"	.fmask	0x00000000,0\n"
"	.set	noreorder\n"
"	.set	nomacro\n"

// align the stack frame to 8 bytes
"	addiu	$12, $4, 7\n"		// t4 ($12) = argSize ($4) + 7
"	li		$13, -8\n"			// t5 ($13) = 0xfffffffffffffff8
"	and		$12, $12, $13\n"	// t4 ($12) &= t5 ($13). t4 holds the size of the argument block
// It is required that the caller reserves space for at least 16 bytes even if there are less than 4 arguments
// and add 8 bytes for the return pointer and s0 ($16) backup
"	addiu	$13, $12, 24\n"		// t5 = t4 + 24. t5 ($13) holds the total size of the stack frame (including return pointer)
// save the s0 register (so we can use it to remember where our return pointer is lives)
"	sw		$16, -4($sp)\n"		// store the s0 register (so we can use it to remember how big our stack frame is)
"	.cfi_offset 16, -4\n"
// store the return pointer
"	sw		$31, -8($sp)\n"
"	.cfi_offset 31, -8\n"
// keep original stack pointer
"	move	$16, $sp\n"
"	.cfi_def_cfa_register 16\n"
// push the stack
"	subu	$sp, $sp, $13\n"

// store the argument in temporary registers
"	addiu	$25, $6, 0\n"		// t9 ($25) holds the function pointer (must be t9 for position independent code)
"	addiu	$3, $4, 0\n"		// v1 ($3) holds the size of the argument buffer
"	move	$15, $5\n"			// t7 ($15) holds the pointer to the argBuffer
"	move	$14, $7\n"			// t6 ($14) holds the values for the float registers

// load integer registers
"	lw		$4, 0($15)\n"		// a0 ($4)
"	lw		$5, 4($15)\n"		// a1 ($5)
"	lw		$6, 8($15)\n"		// a2 ($6)
"	lw		$7, 12($15)\n"		// a3 ($7)

// load float registers
"	ldc1	$f12, 8($14)\n"
"	ldc1	$f14, 0($14)\n"

// skip stack parameters if there are 4 or less as they are moved into the registers
"	addi	$14, $3, -16\n"		// The first 4 args were already loaded into registers
"	blez	$14, andCall\n"
"	nop\n"

// push stack parameters
"pushArgs:\n"
"	addi	$3, -4\n"
// load from $15 + stack bytes ($3)
"	addu	$14, $15, $3\n"
"	lw		$14, 0($14)\n"
// store to $sp + stack bytes ($3)
"	addu	$13, $sp, $3\n"
"	sw		$14, 0($13)\n"
// if there are more, loop...
"	bne		$3, $0, pushArgs\n"
"	nop\n"

// and call the function
"andCall:\n"
"	jalr	$25\n"
"	nop\n"

// restore original stack pointer
"	move	$sp, $16\n"
// restore the return pointer
"	lw		$31, -8($sp)\n"
// restore the original value of $16
"	lw		$16, -4($sp)\n"
// and return from the function
"	jr		$31\n"
"	nop\n"

"	.set	macro\n"
"	.set	reorder\n"
"	.end	mipsFunc\n"
"	.cfi_endproc\n"
"	.size	mipsFunc, .-mipsFunc\n"
);

#else // !(defined(__linux__) && defined(_ABIO32))

// The MIPS ABI used by PSP and PS2 is implemented here

#define AS_MIPS_MAX_ARGS 32
#define AS_NUM_REG_FLOATS 8
#define AS_NUM_REG_INTS 8

// The array used to send values to the correct places.
// first 0-8 regular values to load into the a0-a3, t0-t3 registers
// then 0-8 float values to load into the f12-f19 registers
// then (AS_MIPS_MAX_ARGS - 16) values to load onto the stack
// the +1 is for when CallThis (object methods) is used
// extra +1 when returning in memory
extern "C" {
// TODO: This array shouldn't be global. It should be a local array in CallSystemFunctionNative
asDWORD mipsArgs[AS_MIPS_MAX_ARGS + 1 + 1];
}

// Loads all data into the correct places and calls the function.
// intArgSize is the size in bytes for how much data to put in int registers
// floatArgSize is the size in bytes for how much data to put in float registers
// stackArgSize is the size in bytes for how much data to put on the callstack
extern "C" asQWORD mipsFunc(int intArgSize, int floatArgSize, int stackArgSize, asDWORD func);

// puts the arguments in the correct place in the mipsArgs-array. See comments above.
// This could be done better.
inline void splitArgs(const asDWORD *args, int argNum, int &numRegIntArgs, int &numRegFloatArgs, int &numRestArgs, int hostFlags)
{
	int i;

	int argBit = 1;
	for (i = 0; i < argNum; i++)
	{
		if (hostFlags & argBit)
		{
			if (numRegFloatArgs < AS_NUM_REG_FLOATS)
			{
				// put in float register
				mipsArgs[AS_NUM_REG_INTS + numRegFloatArgs] = args[i];
				numRegFloatArgs++;
			}
			else
			{
				// put in stack
				mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + numRestArgs] = args[i];
				numRestArgs++;
			}
		}
		else
		{
			if (numRegIntArgs < AS_NUM_REG_INTS)
			{
				// put in int register
				mipsArgs[numRegIntArgs] = args[i];
				numRegIntArgs++;
			}
			else
			{
				// put in stack
				mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + numRestArgs] = args[i];
				numRestArgs++;
			}
		}
		argBit <<= 1;
	}
}

asQWORD CallCDeclFunction(const asDWORD *args, int argSize, asDWORD func, int flags)
{
	int argNum = argSize >> 2;

	int intArgs = 0;
	int floatArgs = 0;
	int restArgs = 0;

	// put the arguments in the correct places in the mipsArgs array
	if(argNum > 0)
		splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);

	return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
}

// This function is identical to CallCDeclFunction, with the only difference that
// the value in the first parameter is the object
asQWORD CallThisCallFunction(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
{
	int argNum = argSize >> 2;

	int intArgs = 1;
	int floatArgs = 0;
	int restArgs = 0;

	mipsArgs[0] = (asDWORD) obj;

	// put the arguments in the correct places in the mipsArgs array
	if (argNum > 0)
		splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);

	return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
}

// This function is identical to CallCDeclFunction, with the only difference that
// the value in the last parameter is the object
asQWORD CallThisCallFunction_objLast(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
{
	int argNum = argSize >> 2;

	int intArgs = 0;
	int floatArgs = 0;
	int restArgs = 0;

	// put the arguments in the correct places in the mipsArgs array
	if(argNum > 0)
		splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);

	if(intArgs < AS_NUM_REG_INTS)
	{
		mipsArgs[intArgs] = (asDWORD) obj;
		intArgs++;
	}
	else
	{
		mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + restArgs] = (asDWORD) obj;
		restArgs++;
	}

	return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
}

asDWORD GetReturnedFloat()
{
	asDWORD f;

	asm("swc1 $f0, %0\n" : "=m"(f));

	return f;
}

asQWORD GetReturnedDouble()
{
	asQWORD d = 0;

	asm("sdc1 $f0, %0\n" : "=m"(d));

	return d;
}

asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/, void */*secondObject*/)
{
	asCScriptEngine *engine = context->m_engine;
	asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
	int callConv = sysFunc->callConv;

	// TODO: Mips does not yet support THISCALL_OBJFIRST/LAST

	asQWORD retQW = 0;

	void    *func              = (void*)sysFunc->func;
	int      paramSize         = sysFunc->paramSize;
	asDWORD *vftable;

	if( descr->returnType.IsObject() && !descr->returnType.IsReference() && !descr->returnType.IsObjectHandle() )
	{
		mipsArgs[AS_MIPS_MAX_ARGS+1] = (asDWORD) retPointer;
	}

	asASSERT(descr->parameterTypes.GetLength() <= AS_MIPS_MAX_ARGS);

	// mark all float arguments
	int argBit = 1;
	int hostFlags = 0;
	int intArgs = 0;
	for( size_t a = 0; a < descr->parameterTypes.GetLength(); a++ )
	{
		if (descr->parameterTypes[a].IsFloatType())
			hostFlags |= argBit;
		else
			intArgs++;
		argBit <<= 1;
	}

	asDWORD paramBuffer[64];
	if( sysFunc->takesObjByVal )
	{
		paramSize = 0;
		int spos = 0;
		int dpos = 1;
		for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
		{
			if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
			{
#ifdef COMPLEX_OBJS_PASSED_BY_REF
				if( descr->parameterTypes[n].GetTypeInfo()->flags & COMPLEX_MASK )
				{
					paramBuffer[dpos++] = args[spos++];
					paramSize++;
				}
				else
#endif
				{
					// Copy the object's memory to the buffer
					memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
					// Delete the original memory
					engine->CallFree(*(char**)(args+spos));
					spos++;
					dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
					paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
				}
			}
			else
			{
				// Copy the value directly
				paramBuffer[dpos++] = args[spos++];
				if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
					paramBuffer[dpos++] = args[spos++];
				paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
			}
		}
		// Keep a free location at the beginning
		args = &paramBuffer[1];
	}

	switch( callConv )
	{
	case ICC_CDECL:
	case ICC_CDECL_RETURNINMEM:
	case ICC_STDCALL:
	case ICC_STDCALL_RETURNINMEM:
		retQW = CallCDeclFunction(args, paramSize<<2, (asDWORD)func, hostFlags);
		break;
	case ICC_THISCALL:
	case ICC_THISCALL_RETURNINMEM:
		retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
		break;
	case ICC_VIRTUAL_THISCALL:
	case ICC_VIRTUAL_THISCALL_RETURNINMEM:
		// Get virtual function table from the object pointer
		vftable = *(asDWORD**)obj;
		retQW = CallThisCallFunction(obj, args, paramSize<<2, vftable[asDWORD(func)>>2], hostFlags);
		break;
	case ICC_CDECL_OBJLAST:
	case ICC_CDECL_OBJLAST_RETURNINMEM:
		retQW = CallThisCallFunction_objLast(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
		break;
	case ICC_CDECL_OBJFIRST:
	case ICC_CDECL_OBJFIRST_RETURNINMEM:
		retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
		break;
	default:
		context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
	}

	// If the return is a float value we need to get the value from the FP register
	if( sysFunc->hostReturnFloat )
	{
		if( sysFunc->hostReturnSize == 1 )
			*(asDWORD*)&retQW = GetReturnedFloat();
		else
			retQW = GetReturnedDouble();
	}

	return retQW;
}

asm(
"	.text\n"
//"	.align 2\n"
"	.global mipsFunc\n"
"	.ent	mipsFunc\n"
"mipsFunc:\n"
//"	.frame	$fp,64,$31		# vars= 0, regs= 0/0, args= 0, gp= 0\n"
//"	.mask	0x00000000,0\n"
//"	.fmask	0x00000000,0\n"
"	.set	noreorder\n"
"	.set	nomacro\n"
// align the stack frame to 8 bytes
"	addiu	$12, $6, 7\n"
"	li		$13, -8\n"			// 0xfffffffffffffff8
"	and		$12, $12, $13\n"	// t4 holds the size of the argument block
// and add 8 bytes for the return pointer and s0 backup
"	addiu	$13, $12, 8\n"		// t5 holds the total size of the stack frame (including return pointer)
// save the s0 register (so we can use it to remember where our return pointer is lives)
"	sw		$16, -4($sp)\n"		// store the s0 register (so we can use it to remember how big our stack frame is)
// push the stack
"	subu	$sp, $sp, $13\n"
// find the return address, place in s0
"	addu	$16, $sp, $12\n"
// store the return pointer
"	sw		$31, 0($16)\n"

// backup our function params
"	addiu	$2, $7, 0\n"
"	addiu	$3, $6, 0\n"

// get global mipsArgs[] array pointer
//"	lui		$15, %hi(mipsArgs)\n"
//"	addiu	$15, $15, %lo(mipsArgs)\n"
// we'll use the macro instead because SN Systems doesnt like %hi/%lo
".set macro\n"
" la  $15, mipsArgs\n"
".set nomacro\n"
// load register params
"	lw		$4, 0($15)\n"
"	lw		$5, 4($15)\n"
"	lw		$6, 8($15)\n"
"	lw		$7, 12($15)\n"
"	lw		$8, 16($15)\n"
"	lw		$9, 20($15)\n"
"	lw		$10, 24($15)\n"
"	lw		$11, 28($15)\n"

// load float params
"	lwc1	$f12, 32($15)\n"
"	lwc1	$f13, 36($15)\n"
"	lwc1	$f14, 40($15)\n"
"	lwc1	$f15, 44($15)\n"
"	lwc1	$f16, 48($15)\n"
"	lwc1	$f17, 52($15)\n"
"	lwc1	$f18, 56($15)\n"
"	lwc1	$f19, 60($15)\n"

// skip stack paramaters if there are none
"	beq		$3, $0, andCall\n"

// push stack paramaters
"	addiu	$15, $15, 64\n"
"pushArgs:\n"
"	addiu	$3, -4\n"
// load from $15 + stack bytes ($3)
"	addu	$14, $15, $3\n"
"	lw		$14, 0($14)\n"
// store to $sp + stack bytes ($3)
"	addu	$13, $sp, $3\n"
"	sw		$14, 0($13)\n"
// if there are more, loop...
"	bne		$3, $0, pushArgs\n"
"	nop\n"

// and call the function
"andCall:\n"
"	jal		$2\n"
"	nop\n"

// restore the return pointer
"	lw		$31, 0($16)\n"
// pop the stack pointer (remembering the return pointer was 8 bytes below the top)
"	addiu	$sp, $16, 8\n"
// and return from the function
"	jr		$31\n"
// restore the s0 register (in the branch delay slot)
"	lw		$16, -4($sp)\n"
"	.set	macro\n"
"	.set	reorder\n"
"	.end	mipsFunc\n"
"	.size	mipsFunc, .-mipsFunc\n"
);

#endif // PSP and PS2 MIPS ABI

END_AS_NAMESPACE

#endif // AS_MIPS
#endif // AS_MAX_PORTABILITY