avcall-i386.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:5k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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