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

  1. #ifndef _avcall_m68k_c                /*-*- C -*-*/
  2. #define _avcall_m68k_c "$Id: avcall-m68k.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 a m68k Sun3 with gcc/sun-cc.
  18.  
  19.   This calls a C function with an argument list built up using macros
  20.   defined in av_call.h.
  21.  
  22.   M68k Argument Passing Conventions:
  23.  
  24.   All arguments are passed on the stack with word alignment. Doubles take
  25.   two words. Structure args are passed as true structures embedded in the
  26.   argument stack. To return a structure, the called function copies the
  27.   return value to the address supplied in register "a1". Gcc without
  28.   -fpcc-struct-return returns <= 4 byte structures as integers.
  29.  
  30.   Compile this routine with gcc -O (or -O2 or -g -O) to get the right
  31.   register variables, or use the assembler version.
  32.   ----------------------------------------------------------------------*/
  33. #include "avcall.h.in"
  34.  
  35. #define RETURN(TYPE,VAL)    (*(TYPE*)l->raddr = (TYPE)(VAL))
  36.  
  37. int
  38. __builtin_avcall(av_alist* l)
  39. {
  40.   register __avword*    sp    __asm__("sp");  /* C names for registers */
  41.   register __avword*    sret    __asm__("a1");    /* structure return pointer */
  42. /*register __avword    iret    __asm__("d0"); */
  43.   register __avword    iret2    __asm__("d1");
  44.   register float    fret    __asm__("d0");    /* d0 */
  45.   register double    dret    __asm__("d0");    /* d0,d1 */
  46.  
  47.   __avword* argframe = (sp -= __AV_ALIST_WORDS); /* make room for argument list */
  48.   int arglen = l->aptr - l->args;
  49.   __avword i;
  50.  
  51.   for (i = 0; i < arglen; i++)        /* push function args onto stack */
  52.     argframe[i] = l->args[i];
  53.  
  54.   if (l->rtype == __AVstruct)        /* push struct return address */
  55.     __asm__("movel %0,a1" : : "g" (l->raddr));
  56.  
  57.   i = (*l->func)();            /* call function */
  58.  
  59.   switch (l->rtype)            /* save return value */
  60.   {
  61.   case __AVvoid:                    break;
  62.   case __AVword:    RETURN(__avword,    i);    break;
  63.   case __AVchar:    RETURN(char,        i);    break;
  64.   case __AVschar:    RETURN(signed char,    i);    break;
  65.   case __AVuchar:    RETURN(unsigned char,    i);    break;
  66.   case __AVshort:    RETURN(short,        i);    break;
  67.   case __AVushort:    RETURN(unsigned short,    i);    break;
  68.   case __AVint:        RETURN(int,        i);    break;
  69.   case __AVuint:    RETURN(unsigned int,    i);    break;
  70.   case __AVlong:    RETURN(long,        i);    break;
  71.   case __AVulong:    RETURN(unsigned long,    i);    break;
  72.   case __AVfloat:
  73.     if (l->flags & __AV_SUNCC_FLOAT_RETURN)
  74.       RETURN(float, (float)dret);
  75.     else
  76.       RETURN(float, fret);
  77.     break;
  78.   case __AVdouble:    RETURN(double,        dret);    break;
  79.   case __AVvoidp:    RETURN(void*,        i);    break;
  80.   case __AVstruct:
  81.     if (l->flags & __AV_PCC_STRUCT_RETURN)
  82.     { /* pcc struct return convention: need a  *(TYPE*)l->raddr = *(TYPE*)i;  */
  83.       switch (l->rsize)
  84.       {
  85.       case sizeof(char):  RETURN(char,    *(char*)i);    break;
  86.       case sizeof(short): RETURN(short,    *(short*)i);    break;
  87.       case sizeof(int):      RETURN(int,    *(int*)i);    break;
  88.       case sizeof(double):
  89.     ((int*)l->raddr)[0] = ((int*)i)[0];
  90.     ((int*)l->raddr)[1] = ((int*)i)[1];
  91.     break;
  92.       default:
  93.     {
  94.       int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
  95.       while (--n >= 0)
  96.         ((__avword*)l->raddr)[n] = ((__avword*)i)[n];
  97.     }
  98.     break;
  99.       }
  100.     }
  101.     else
  102.     { /* normal struct return convention */
  103.       if (l->flags & __AV_REGISTER_STRUCT_RETURN)
  104.     switch (l->rsize)
  105.     {
  106.     case sizeof(char):  RETURN(char,  i);    break;
  107.     case sizeof(short): RETURN(short, i);    break;
  108.     case sizeof(int):   RETURN(int,   i);    break;
  109.     case 2*sizeof(__avword):
  110.       ((__avword*)l->raddr)[0] = i;
  111.       ((__avword*)l->raddr)[1] = iret2;
  112.       break;
  113.     default:                break;
  114.     }
  115.     }
  116.     break;
  117.   default:                    break;
  118.   }
  119.   sp += __AV_ALIST_WORDS;
  120.   return 0;
  121. }
  122.  
  123. #endif /*_avcall_m68k_c */
  124.