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

信息检索与抽取

开发平台:

Unix_Linux

  1. #ifndef _avcall_mips_c /*-*- C -*-*/
  2. #define _avcall_mips_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 !!!
  13.   Foreign function interface for an SGI MIPS with gcc/sgi-cc.
  14.   This calls a C function with an argument list built up using macros
  15.   defined in av_call.h.
  16.   SGI MIPS Argument Passing Conventions
  17.   - The entire argument list forms a structure with all the appropriate
  18.     holes & alignments, and space for this is allocated in the stack frame.
  19.   - Shorter integers are promoted to word length (sizeof(int)=sizeof(long)=4).
  20.   - Doubles are 2 words aligned on even boundaries.
  21.   - The first 4 words of the structure are passed in registers $4...$7, stack
  22.     space for these is always allocated. Remaining words are passed on the
  23.     stack.
  24.   - If the first two args are floats or doubles, they are also passed in $f12
  25.     and $f14. But varargs functions will expect them in the integer registers
  26.     and we can't tell whether the function is varargs so we pass them both ways.
  27.   - Structure arguments are copies embedded in the arglist structure.
  28.   - Structure returns are pointers to caller-allocated space passed in as the
  29.     first argument of the list. The function also returns the pointer.
  30.   - Integer/pointer returns are in $2, float/double returns in $f0.
  31.   - Under IRIX 5, the called function expects to see its own address in $25.
  32.   This file needs to be compiled with gcc for the asm extensions, but the
  33.   assembly version of it and the header file seem to work with SGI cc.
  34.   ----------------------------------------------------------------------*/
  35. #include "avcall.h.in"
  36. #define RETURN(TYPE,VAL) (*(TYPE*)l->raddr = (TYPE)(VAL))
  37. #define OFFSETOF(struct,member) ((int)&(((struct*)0)->member))
  38. typedef __avword (*func_pointer)();
  39. register func_pointer t9 __asm__("$25");
  40. int
  41. __builtin_avcall(av_alist* l)
  42. {
  43.   register __avword* sp __asm__("$sp");  /* C names for registers */
  44.   register __avword iret2 __asm__("$3");
  45.   register float fret __asm__("$f0");
  46.   register double dret __asm__("$f0");
  47.   __avword *space = __builtin_alloca(__AV_ALIST_WORDS * sizeof(__avword)); /* big space for child's stack frame */
  48.   __avword *argframe = (__avword*)sp; /* stack offset for argument list is 0 */
  49.   int arglen = l->aptr - l->args;
  50.   __avword i;
  51.   if (l->flags & __AV_FLOAT_1) /* push leading float args */
  52.   {
  53.     __asm__("l.d $f12,%1(%0)" : : "p" (l), "i" OFFSETOF(av_alist,floatarg[0]));
  54.     if (l->flags & __AV_FLOAT_2)
  55.       __asm__("l.d $f14,%1(%0)" : : "p" (l), "i" OFFSETOF(av_alist,floatarg[1]));
  56.   }
  57.   for (i = 4; i < arglen; i++) /* push excess function args */
  58.     argframe[i] = l->args[i];
  59.   i = (*(t9 = l->func))(l->args[0], l->args[1],  /* call function with 1st 4 args */
  60. l->args[2], l->args[3]);
  61.   /* save return value */
  62.   if (l->rtype == __AVvoid) {
  63.   } else
  64.   if (l->rtype == __AVword) {
  65.     RETURN(__avword, i);
  66.   } else
  67.   if (l->rtype == __AVchar) {
  68.     RETURN(char, i);
  69.   } else
  70.   if (l->rtype == __AVschar) {
  71.     RETURN(signed char, i);
  72.   } else
  73.   if (l->rtype == __AVuchar) {
  74.     RETURN(unsigned char, i);
  75.   } else
  76.   if (l->rtype == __AVshort) {
  77.     RETURN(short, i);
  78.   } else
  79.   if (l->rtype == __AVushort) {
  80.     RETURN(unsigned short, i);
  81.   } else
  82.   if (l->rtype == __AVint) {
  83.     RETURN(int, i);
  84.   } else
  85.   if (l->rtype == __AVuint) {
  86.     RETURN(unsigned int, i);
  87.   } else
  88.   if (l->rtype == __AVlong) {
  89.     RETURN(long, i);
  90.   } else
  91.   if (l->rtype == __AVulong) {
  92.     RETURN(unsigned long, i);
  93.   } else
  94.   if (l->rtype == __AVlonglong || l->rtype == __AVulonglong) {
  95.     ((__avword*)l->raddr)[0] = i;
  96.     ((__avword*)l->raddr)[1] = iret2;
  97.   } else
  98.   if (l->rtype == __AVfloat) {
  99.     RETURN(float, fret);
  100.   } else
  101.   if (l->rtype == __AVdouble) {
  102.     RETURN(double, dret);
  103.   } else
  104.   if (l->rtype == __AVvoidp) {
  105.     RETURN(void*, i);
  106.   } else
  107.   if (l->rtype == __AVstruct) {
  108.     if (l->flags & __AV_PCC_STRUCT_RETURN) {
  109.       /* pcc struct return convention: need a  *(TYPE*)l->raddr = *(TYPE*)i;  */
  110.       if (l->rsize == sizeof(char)) {
  111.         RETURN(char, *(char*)i);
  112.       } else
  113.       if (l->rsize == sizeof(short)) {
  114.         RETURN(short, *(short*)i);
  115.       } else
  116.       if (l->rsize == sizeof(int)) {
  117.         RETURN(int, *(int*)i);
  118.       } else
  119.       if (l->rsize == sizeof(double)) {
  120.         ((int*)l->raddr)[0] = ((int*)i)[0];
  121.         ((int*)l->raddr)[1] = ((int*)i)[1];
  122.       } else {
  123.         int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
  124.         while (--n >= 0)
  125.           ((__avword*)l->raddr)[n] = ((__avword*)i)[n];
  126.       }
  127.     } else {
  128.       /* normal struct return convention */
  129.       if (l->flags & __AV_SMALL_STRUCT_RETURN) {
  130.         if (l->rsize == sizeof(char)) {
  131.           RETURN(char, i);
  132.         } else
  133.         if (l->rsize == sizeof(short)) {
  134.           RETURN(short, i);
  135.         } else
  136.         if (l->rsize == sizeof(int)) {
  137.           RETURN(int, i);
  138.         }
  139.       }
  140.     }
  141.   }
  142.   return 0;
  143. }
  144. #endif /*_avcall_mips_c */