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

  1. #ifndef _avcall_alpha_c                /*-*- C -*-*/
  2. #define _avcall_alpha_c "$Id: avcall-alpha.c,v 1.1.1.1 1995/09/10 10:58:56 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 a DEC Alpha with gcc.
  18.  
  19.   This calls a C function with an argument list built up using macros
  20.   defined in av_call.h.
  21.  
  22.   Alpha Argument Passing Conventions
  23.  
  24.   The first 6 arguments are passed in registers $16-$21 for integers,
  25.   in registers $f16-$f21 for floats. From then on, everything is passed
  26.   on the stack.
  27.  
  28.   Everything on the stack is word-aligned.
  29.  
  30.   Integers and pointers are returned in $0, floats and doubles in $f0.
  31.   To return a structure, the called function copies the value to space
  32.   pointed to by its first argument, and all other arguments are shifted
  33.   down by one.
  34.  
  35.   Compile this routine with gcc for the __asm__ extensions and with
  36.   optimisation on (-O or -O2 or -g -O) so that argframe is set to the
  37.   correct offset.
  38.   ----------------------------------------------------------------------*/
  39. #include "avcall.h.in"
  40.  
  41. #define RETURN(TYPE,VAL)    (*(TYPE*)l->raddr = (TYPE)(VAL))
  42.  
  43. int
  44. __builtin_avcall(av_alist* l)
  45. {
  46.   register __avword*    sp    __asm__("$30");  /* C names for registers */
  47.   register long        arg1    __asm__("$16");
  48.   register long        arg2    __asm__("$17");
  49.   register long        arg3    __asm__("$18");
  50.   register long        arg4    __asm__("$19");
  51.   register long        arg5    __asm__("$20");
  52.   register long        arg6    __asm__("$21");
  53.   register double    fret    __asm__("$f0");
  54.   register double    farg1    __asm__("$f16");
  55.   register double    farg2    __asm__("$f17");
  56.   register double    farg3    __asm__("$f18");
  57.   register double    farg4    __asm__("$f19");
  58.   register double    farg5    __asm__("$f20");
  59.   register double    farg6    __asm__("$f21");
  60. /*register __avword    iret    __asm__("$0"); */
  61.   register __avword    iret2    __asm__("$1");
  62.  
  63.   __avword* argframe = (sp -= __AV_ALIST_WORDS); /* make room for argument list */
  64.   int arglen = ((unsigned long) l->aptr - (unsigned long) l->args) >> 3;
  65.   __avword i, i2;
  66.  
  67.   for (i = 6; i < arglen; i++)        /* push excess function args */
  68.     argframe[i-6] = l->args[i];
  69.  
  70.                     /* call function with 1st 6 args */
  71.   /* we pass the args both in the integer registers and the floating point
  72.      registers, so we don't have to store the argument types. */
  73.   __asm__ __volatile__ ("ldq $16,%0" : : "m" (l->args[0])); /* arg1 = l->args[0]; */
  74.   __asm__ __volatile__ ("ldt $f16,%0" : : "m" (l->args[0])); /* farg1 = *(double*) &l->args[0]; */
  75.   __asm__ __volatile__ ("ldq $17,%0" : : "m" (l->args[1])); /* arg2 = l->args[1]; */
  76.   __asm__ __volatile__ ("ldt $f17,%0" : : "m" (l->args[1])); /* farg2 = *(double*) &l->args[1]; */
  77.   __asm__ __volatile__ ("ldq $18,%0" : : "m" (l->args[2])); /* arg3 = l->args[2]; */
  78.   __asm__ __volatile__ ("ldt $f18,%0" : : "m" (l->args[2])); /* farg3 = *(double*) &l->args[2]; */
  79.   __asm__ __volatile__ ("ldq $19,%0" : : "m" (l->args[3])); /* arg4 = l->args[3]; */
  80.   __asm__ __volatile__ ("ldt $f19,%0" : : "m" (l->args[3])); /* farg4 = *(double*) &l->args[3]; */
  81.   __asm__ __volatile__ ("ldq $20,%0" : : "m" (l->args[4])); /* arg5 = l->args[4]; */
  82.   __asm__ __volatile__ ("ldt $f20,%0" : : "m" (l->args[4])); /* farg5 = *(double*) &l->args[4]; */
  83.   __asm__ __volatile__ ("ldq $21,%0" : : "m" (l->args[5])); /* arg6 = l->args[5]; */
  84.   __asm__ __volatile__ ("ldt $f21,%0" : : "m" (l->args[5])); /* farg6 = *(double*) &l->args[5]; */
  85.   i = (*l->func)();
  86.   i2 = iret2;
  87.   sp += __AV_ALIST_WORDS;    /* remove argument list from the stack */
  88.   /* this is apparently not needed, but better safe than sorry... */
  89.   __asm__ __volatile__ ("" : : :
  90.             /* clobber */ "$16", "$17", "$18", "$19", "$20", "$21",
  91.                      "$f16","$f17","$f18","$f19","$f20","$f21");
  92.  
  93.   switch (l->rtype)            /* save return value */
  94.   {
  95.   case __AVvoid:                    break;
  96.   case __AVword:    RETURN(__avword,    i);    break;
  97.   case __AVchar:    RETURN(char,        i);    break;
  98.   case __AVschar:    RETURN(signed char,    i);    break;
  99.   case __AVuchar:    RETURN(unsigned char,    i);    break;
  100.   case __AVshort:    RETURN(short,        i);    break;
  101.   case __AVushort:    RETURN(unsigned short,    i);    break;
  102.   case __AVint:        RETURN(int,        i);    break;
  103.   case __AVuint:    RETURN(unsigned int,    i);    break;
  104.   case __AVlong:    RETURN(long,        i);    break;
  105.   case __AVulong:    RETURN(unsigned long,    i);    break;
  106.   case __AVfloat:    RETURN(float,        fret);    break;
  107.   case __AVdouble:    RETURN(double,        fret);    break;
  108.   case __AVvoidp:    RETURN(void*,        i);    break;
  109.   case __AVstruct:
  110.     if (l->flags & __AV_PCC_STRUCT_RETURN)
  111.     { /* pcc struct return convention: need a  *(TYPE*)l->raddr = *(TYPE*)i;  */
  112.       switch (l->rsize)
  113.       {
  114.       case sizeof(char):  RETURN(char,    *(char*)i);    break;
  115.       case sizeof(short): RETURN(short,    *(short*)i);    break;
  116.       case sizeof(int):      RETURN(int,    *(int*)i);    break;
  117.       case sizeof(long):  RETURN(long,    *(long*)i);    break;
  118.       default:
  119.     {
  120.       int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
  121.       while (--n >= 0)
  122.         ((__avword*)l->raddr)[n] = ((__avword*)i)[n];
  123.     }
  124.     break;
  125.       }
  126.     }
  127.     else
  128.     { /* normal struct return convention */
  129.       if (l->flags & __AV_REGISTER_STRUCT_RETURN)
  130.     switch (l->rsize)
  131.     {
  132.     case sizeof(char):  RETURN(char,  i);    break;
  133.     case sizeof(short): RETURN(short, i);    break;
  134.     case sizeof(int):   RETURN(int,   i);    break;
  135.     case sizeof(long):  RETURN(long,  i);    break;
  136.     case 2*sizeof(__avword):
  137.       ((__avword*)l->raddr)[0] = i;
  138.       ((__avword*)l->raddr)[1] = i2;
  139.       break;
  140.     default:                break;
  141.     }
  142.     }
  143.     break;
  144.   default:                    break;
  145.   }
  146.   return 0;
  147. }
  148.  
  149. #endif /*_avcall_alpha_c */
  150.