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

  1. #ifndef _avcall_i386_c                /*-*- C -*-*/
  2. #define _avcall_i386_c "$Id: avcall-i386.c,v 1.1.1.1 1995/09/10 10:58:54 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.   This is free software distributed under the GNU General Public
  8.   Licence described in the file COPYING. Contact the author if
  9.   you don't have this or can't live with it. There is ABSOLUTELY
  10.   NO WARRANTY, explicit or implied, on this software.
  11. **/
  12. /*----------------------------------------------------------------------
  13.   !!! THIS ROUTINE MUST BE COMPILED gcc -O -fno-omit-frame-pointer !!!
  14.  
  15.   Foreign function interface for a Linux i386/486 with gcc.
  16.  
  17.   This calls a C function with an argument list built up using macros
  18.   defined in av_call.h.
  19.  
  20.   i386 Argument Passing Conventions:
  21.  
  22.   All arguments are passed on the stack with word alignment. Doubles take
  23.   two words. Structure args are passed as true structures embedded in the
  24.   argument stack. Float and double returns often come from FPU registers.
  25.  
  26.   To return a structure, the called function copies the value to space
  27.   pointed to by its first argument, and all other arguments are shifted
  28.   down by one. On NeXTstep, however, the called function copies the return
  29.   value to the address supplied in register "%ebx". Gcc without
  30.   -fpcc-struct-return returns <= 4 byte structures as integers.
  31.  
  32.   Compile this routine with gcc -O (or -O2 -fno-omit-frame-pointer or -g -O)
  33.   to get the right register variables. For other compilers use the
  34.   pre-compiled assembler version.
  35.  
  36.   -fomit-frame-pointer is forbidden because when calling structure returning
  37.   functions (the "i = (*l->func)();" line below) the called functions pops
  38.   the return value container pointer from the stack: "ret $4" instead of
  39.   "ret". (See gcc-2.6.3 macro RETURN_POPS_ARGS.) From our point of view, %esp
  40.   gets magically incremented. A workaround would be to push the return value
  41.   container pointer using an __asm__("pushl %0" : : : ...) instruction.
  42.   ----------------------------------------------------------------------*/
  43. #include "avcall.h.in"
  44.  
  45. #define RETURN(TYPE,VAL)    (*(TYPE*)l->raddr = (TYPE)(VAL))
  46.  
  47. int
  48. __builtin_avcall(av_alist* l)
  49. {
  50.   register __avword*    sp    __asm__("sp");    /* C names for registers */
  51. /*register __avword    iret    __asm__("eax"); */
  52.   register __avword    iret2    __asm__("edx");
  53.  
  54.   __avword* argframe = (sp -= __AV_ALIST_WORDS); /* make room for argument list */
  55.   int arglen = l->aptr - l->args;
  56.   __avword i;
  57.  
  58.   for (i = 0; i < arglen; i++)        /* push function args onto stack */
  59.     argframe[i] = l->args[i];
  60.  
  61.   /* struct return address */
  62.   if ((l->flags & __AV_NEXTGCC_STRUCT_RETURN) && (l->rtype == __AVstruct))
  63.     __asm__("movl %0,%%ebx" : : "g" (l->raddr) : "bx" /* %ebx */);
  64.  
  65.   switch (l->rtype)            /* call function */
  66.   {
  67.   case __AVfloat:
  68.     *(float*)l->raddr = (*(float(*)())l->func)();
  69.     return 0;
  70.   case __AVdouble:
  71.     *(double*)l->raddr = (*(double(*)())l->func)();
  72.     return 0;
  73.   default:
  74.     i = (*l->func)();
  75.     break;
  76.   }
  77.   switch (l->rtype)            /* save return value */
  78.   {
  79.   case __AVvoid:                    break;
  80.   case __AVword:    RETURN(__avword,    i);    break;
  81.   case __AVchar:    RETURN(char,        i);    break;
  82.   case __AVschar:    RETURN(signed char,    i);    break;
  83.   case __AVuchar:    RETURN(unsigned char,    i);    break;
  84.   case __AVshort:    RETURN(short,        i);    break;
  85.   case __AVushort:    RETURN(unsigned short,    i);    break;
  86.   case __AVint:        RETURN(int,        i);    break;
  87.   case __AVuint:    RETURN(unsigned int,    i);    break;
  88.   case __AVlong:    RETURN(long,        i);    break;
  89.   case __AVulong:    RETURN(unsigned long,    i);    break;
  90.   case __AVfloat:    /* see above */            break;
  91.   case __AVdouble:    /* see above */            break;
  92.   case __AVvoidp:    RETURN(void*,        i);    break;
  93.   case __AVstruct:
  94.     if (l->flags & __AV_PCC_STRUCT_RETURN)
  95.     { /* pcc struct return convention: need a  *(TYPE*)l->raddr = *(TYPE*)i;  */
  96.       switch (l->rsize)
  97.       {
  98.       case sizeof(char):  RETURN(char,    *(char*)i);    break;
  99.       case sizeof(short): RETURN(short,    *(short*)i);    break;
  100.       case sizeof(int):      RETURN(int,    *(int*)i);    break;
  101.       case sizeof(double):
  102.     ((int*)l->raddr)[0] = ((int*)i)[0];
  103.     ((int*)l->raddr)[1] = ((int*)i)[1];
  104.     break;
  105.       default:
  106.     {
  107.       int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
  108.       while (--n >= 0)
  109.         ((__avword*)l->raddr)[n] = ((__avword*)i)[n];
  110.     }
  111.     break;
  112.       }
  113.     }
  114.     else
  115.     { /* normal struct return convention */
  116.       if (l->flags & __AV_REGISTER_STRUCT_RETURN)
  117.     switch (l->rsize)
  118.     {
  119.     case sizeof(char):  RETURN(char,  i);    break;
  120.     case sizeof(short): RETURN(short, i);    break;
  121.     case sizeof(int):   RETURN(int,   i);    break;
  122.     case 2*sizeof(__avword):
  123.       ((__avword*)l->raddr)[0] = i;
  124.       ((__avword*)l->raddr)[1] = iret2;
  125.       break;
  126.     default:                break;
  127.     }
  128.     }
  129.     break;
  130.   default:                    break;
  131.   }
  132.   return 0;
  133. }
  134.  
  135. #endif /*_avcall_i386_c */
  136.