home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / CLISP-2.LHA / CLISP960530-ki.lha / ffcall / avcall / avcall-mips.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  4.8 KB  |  130 lines

  1. #ifndef _avcall_mips_c                /*-*- C -*-*/
  2. #define _avcall_mips_c "$Id: avcall-mips.c,v 1.1.1.1 1995/09/10 10:58:52 marcus Exp $"
  3. /**
  4.   Copyright 1993 Bill Triggs, <bill@robots.oxford.ac.uk>,
  5.   Oxford University Robotics Group, Oxford OX1 3PJ, U.K.
  6.  
  7.   Copyright 1995 Bruno Haible, <haible@ma2s2.mathematik.uni-karlsruhe.de>
  8.  
  9.   This is free software distributed under the GNU General Public
  10.   Licence described in the file COPYING. Contact the author if
  11.   you don't have this or can't live with it. There is ABSOLUTELY
  12.   NO WARRANTY, explicit or implied, on this software.
  13. **/
  14. /*----------------------------------------------------------------------
  15.   !!! THIS ROUTINE MUST BE COMPILED gcc -O !!!
  16.  
  17.   Foreign function interface for an SGI MIPS with gcc/sgi-cc.
  18.  
  19.   This calls a C function with an argument list built up using macros
  20.   defined in av_call.h.
  21.  
  22.   SGI MIPS Argument Passing Conventions
  23.  
  24.   - The entire argument list forms a structure with all the appropriate
  25.     holes & alignments, and space for this is allocated in the stack frame.
  26.   - Shorter integers are promoted to word length (sizeof(int)=sizeof(long)=4).
  27.   - Doubles are 2 words aligned on even boundaries.
  28.   - The first 4 words of the structure are passed in registers $4...$7, stack
  29.     space for these is always allocated. Remaining words are passed on the
  30.     stack.
  31.   - If the first two args are floats or doubles, they are also passed in $f12
  32.     and $f14. But varargs functions will expect them in the integer registers
  33.     and we can't tell whether the function is varargs so we pass them both ways.
  34.   - Structure arguments are copies embedded in the arglist structure.
  35.   - Structure returns are pointers to caller-allocated space passed in as the
  36.     first argument of the list. The function also returns the pointer.
  37.   - Integer/pointer returns are in $2, float/double returns in $f0.
  38.   - Under IRIX 5, the called function expects to see its own address in $25.
  39.  
  40.   This file needs to be compiled with gcc for the asm extensions, but the
  41.   assembly version of it and the header file seem to work with SGI cc.
  42.   ----------------------------------------------------------------------*/
  43. #include "avcall.h.in"
  44.  
  45. #define RETURN(TYPE,VAL)    (*(TYPE*)l->raddr = (TYPE)(VAL))
  46. #define OFFSETOF(struct,member) ((int)&(((struct*)0)->member))
  47.  
  48. typedef int (*func_pointer)();
  49. register func_pointer    t9    __asm__("$25");
  50.  
  51. int
  52. __builtin_avcall(av_alist* l)
  53. {
  54.   register __avword*    sp    __asm__("$sp");  /* C names for registers */
  55.   register float    fret    __asm__("$f0");
  56.   register double    dret    __asm__("$f0");
  57.   __avword *space = alloca(__AV_ALIST_WORDS*sizeof(__avword));
  58.   __avword *argframe = (__avword*)sp;    /* stack offset for argument list is 0 */
  59.   int arglen = l->aptr - l->args;
  60.   __avword i;
  61.  
  62.   if (l->flags & __AV_FLOAT_1)        /* push leading float args */
  63.   {
  64.     __asm__("l.d $f12,%1(%0)" : : "p" (l), "i" OFFSETOF(av_alist,floatarg[0]));
  65.     if (l->flags & __AV_FLOAT_2)
  66.       __asm__("l.d $f14,%1(%0)" : : "p" (l), "i" OFFSETOF(av_alist,floatarg[1]));
  67.   }
  68.  
  69.   for (i = 4; i < arglen; i++)        /* push excess function args */
  70.     argframe[i] = l->args[i];
  71.  
  72.   i = (*(t9 = l->func))(l->args[0], l->args[1],  /* call function with 1st 4 args */
  73.             l->args[2], l->args[3]);
  74.  
  75.   switch (l->rtype)            /* save return value */
  76.   {
  77.   case __AVvoid:                    break;
  78.   case __AVword:    RETURN(__avword,    i);    break;
  79.   case __AVchar:    RETURN(char,        i);    break;
  80.   case __AVschar:    RETURN(signed char,    i);    break;
  81.   case __AVuchar:    RETURN(unsigned char,    i);    break;
  82.   case __AVshort:    RETURN(short,        i);    break;
  83.   case __AVushort:    RETURN(unsigned short,    i);    break;
  84.   case __AVint:        RETURN(int,        i);    break;
  85.   case __AVuint:    RETURN(unsigned int,    i);    break;
  86.   case __AVlong:    RETURN(long,        i);    break;
  87.   case __AVulong:    RETURN(unsigned long,    i);    break;
  88.   case __AVfloat:    RETURN(float,        fret);    break;
  89.   case __AVdouble:    RETURN(double,        dret);    break;
  90.   case __AVvoidp:    RETURN(void*,        i);    break;
  91.   case __AVstruct:
  92.     if (l->flags & __AV_PCC_STRUCT_RETURN)
  93.     { /* pcc struct return convention: need a  *(TYPE*)l->raddr = *(TYPE*)i;  */
  94.       switch (l->rsize)
  95.       {
  96.       case sizeof(char):  RETURN(char,    *(char*)i);    break;
  97.       case sizeof(short): RETURN(short,    *(short*)i);    break;
  98.       case sizeof(int):      RETURN(int,    *(int*)i);    break;
  99.       case sizeof(double):
  100.     ((int*)l->raddr)[0] = ((int*)i)[0];
  101.     ((int*)l->raddr)[1] = ((int*)i)[1];
  102.     break;
  103.       default:
  104.     {
  105.       int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
  106.       while (--n >= 0)
  107.         ((__avword*)l->raddr)[n] = ((__avword*)i)[n];
  108.     }
  109.     break;
  110.       }
  111.     }
  112.     else
  113.     { /* normal struct return convention */
  114.       if (l->flags & __AV_SMALL_STRUCT_RETURN)
  115.     switch (l->rsize)
  116.     {
  117.     case sizeof(char):  RETURN(char,  i);    break;
  118.     case sizeof(short): RETURN(short, i);    break;
  119.     case sizeof(int):   RETURN(int,   i);    break;
  120.     default:                break;
  121.     }
  122.     }
  123.     break;
  124.   default:                    break;
  125.   }
  126.   return 0;
  127. }
  128.  
  129. #endif /*_avcall_mips_c */
  130.