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

信息检索与抽取

开发平台:

Unix_Linux

  1. #ifndef _avcall_rs6000_c /*-*- C -*-*/
  2. #define _avcall_rs6000_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 IBM RS/6000 with gcc
  14.   This calls a C function with an argument list built up using macros
  15.   defined in av_call.h.
  16.   RS6000 Argument Passing Conventions:
  17.   All arguments, except the first 8 words, are passed on the stack with
  18.   word alignment. Doubles take two words. The first 13 doubles and floats
  19.   are also passed in floating-point-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.
  23.   Differences between AIX and SysV.4 argument passing conventions:
  24.   - AIX: the first 13 doubles and floats are passed in FP registers,
  25.          and when they do, there is still room allocated for them in the
  26.          argument sequence (integer regs or stack).
  27.     SysV.4: the first 8 doubles and floats are passed in FP registers,
  28.          and no room is allocated for them in the argument sequence.
  29.   - AIX: Structures are passed in the argument sequence.
  30.     SysV.4: Structures are passed by reference: only a pointer appears in
  31.          the argument sequence.
  32.   - AIX: Long longs are only word aligned.
  33.     SysV.4: Long longs are two-word aligned, both when passed in registers
  34.          (pairs: 3/4, 5/6, 7/8, 9/10) and when passed on the stack. (Recall
  35.          that the stack is always 8-byte aligned).
  36.   Compile this routine with gcc -O (or -O2 -fno-omit-frame-pointer or -g -O)
  37.   to get the right register variables. For other compilers use the
  38.   pre-compiled assembler version.
  39.   ----------------------------------------------------------------------*/
  40. #include "avcall.h.in"
  41. #ifdef __linux__
  42. #define STACK_OFFSET 2
  43. #else
  44. #define STACK_OFFSET 14
  45. #endif
  46. #define RETURN(TYPE,VAL) (*(TYPE*)l->raddr = (TYPE)(VAL))
  47. register double farg1 __asm__("fr1");
  48. register double farg2 __asm__("fr2");
  49. register double farg3 __asm__("fr3");
  50. register double farg4 __asm__("fr4");
  51. register double farg5 __asm__("fr5");
  52. register double farg6 __asm__("fr6");
  53. register double farg7 __asm__("fr7");
  54. register double farg8 __asm__("fr8");
  55. register double farg9 __asm__("fr9");
  56. register double farg10 __asm__("fr10");
  57. register double farg11 __asm__("fr11");
  58. register double farg12 __asm__("fr12");
  59. register double farg13 __asm__("fr13");
  60. int
  61. __builtin_avcall(av_alist* l)
  62. {
  63.   register __avword* sp __asm__("r1");  /* C names for registers */
  64. /*register __avword iret __asm__("r3"); */
  65.   register __avword iret2 __asm__("r4");
  66.   register float fret __asm__("fr1");
  67.   register double dret __asm__("fr1");
  68.   __avword space[__AV_ALIST_WORDS]; /* space for callee's stack frame */
  69.   __avword* argframe = sp + STACK_OFFSET;/* stack offset for argument list */
  70.   int arglen = l->aptr - l->args;
  71. #ifdef _AIX
  72.   int farglen = 0;
  73. #else
  74.   int farglen = l->faptr - l->fargs;
  75. #endif
  76.   __avword i;
  77.   for (i = (8-farglen); i < arglen; i++) /* push function args onto stack */
  78.     argframe[i-8+farglen] = l->args[i];
  79.   /* pass first 13 floating-point args in registers */
  80.   arglen = l->faptr - l->fargs;
  81.   if (arglen == 0) goto fargs0;
  82.   else if (arglen == 1) goto fargs1;
  83.   else if (arglen == 2) goto fargs2;
  84.   else if (arglen == 3) goto fargs3;
  85.   else if (arglen == 4) goto fargs4;
  86.   else if (arglen == 5) goto fargs5;
  87.   else if (arglen == 6) goto fargs6;
  88.   else if (arglen == 7) goto fargs7;
  89.   else if (arglen == 8) goto fargs8;
  90.   else if (arglen == 9) goto fargs9;
  91.   else if (arglen == 10) goto fargs10;
  92.   else if (arglen == 11) goto fargs11;
  93.   else if (arglen == 12) goto fargs12;
  94.   else if (arglen == 13) goto fargs13;
  95.   fargs13: farg13 = l->fargs[12];
  96.   fargs12: farg12 = l->fargs[11];
  97.   fargs11: farg11 = l->fargs[10];
  98.   fargs10: farg10 = l->fargs[9];
  99.   fargs9: farg9 = l->fargs[8];
  100.   fargs8: farg8 = l->fargs[7];
  101.   fargs7: farg7 = l->fargs[6];
  102.   fargs6: farg6 = l->fargs[5];
  103.   fargs5: farg5 = l->fargs[4];
  104.   fargs4: farg4 = l->fargs[3];
  105.   fargs3: farg3 = l->fargs[2];
  106.   fargs2: farg2 = l->fargs[1];
  107.   fargs1: farg1 = l->fargs[0];
  108.   fargs0: ;
  109. /* call function, pass 8 args in registers */
  110.   i = (*l->func)(l->args[0], l->args[1], l->args[2], l->args[3],
  111.  l->args[4], l->args[5], l->args[6], l->args[7]);
  112.   /* save return value */
  113.   if (l->rtype == __AVvoid) {
  114.   } else
  115.   if (l->rtype == __AVword) {
  116.     RETURN(__avword, i);
  117.   } else
  118.   if (l->rtype == __AVchar) {
  119.     RETURN(char, i);
  120.   } else
  121.   if (l->rtype == __AVschar) {
  122.     RETURN(signed char, i);
  123.   } else
  124.   if (l->rtype == __AVuchar) {
  125.     RETURN(unsigned char, i);
  126.   } else
  127.   if (l->rtype == __AVshort) {
  128.     RETURN(short, i);
  129.   } else
  130.   if (l->rtype == __AVushort) {
  131.     RETURN(unsigned short, i);
  132.   } else
  133.   if (l->rtype == __AVint) {
  134.     RETURN(int, i);
  135.   } else
  136.   if (l->rtype == __AVuint) {
  137.     RETURN(unsigned int, i);
  138.   } else
  139.   if (l->rtype == __AVlong) {
  140.     RETURN(long, i);
  141.   } else
  142.   if (l->rtype == __AVulong) {
  143.     RETURN(unsigned long, i);
  144.   } else
  145.   if (l->rtype == __AVlonglong || l->rtype == __AVulonglong) {
  146.     ((__avword*)l->raddr)[0] = i;
  147.     ((__avword*)l->raddr)[1] = iret2;
  148.   } else
  149.   if (l->rtype == __AVfloat) {
  150.     RETURN(float, fret);
  151.   } else
  152.   if (l->rtype == __AVdouble) {
  153.     RETURN(double, dret);
  154.   } else
  155.   if (l->rtype == __AVvoidp) {
  156.     RETURN(void*, i);
  157.   } else
  158.   if (l->rtype == __AVstruct) {
  159.     if (l->flags & __AV_PCC_STRUCT_RETURN) {
  160.       /* pcc struct return convention: need a  *(TYPE*)l->raddr = *(TYPE*)i;  */
  161.       if (l->rsize == sizeof(char)) {
  162.         RETURN(char, *(char*)i);
  163.       } else
  164.       if (l->rsize == sizeof(short)) {
  165.         RETURN(short, *(short*)i);
  166.       } else
  167.       if (l->rsize == sizeof(int)) {
  168.         RETURN(int, *(int*)i);
  169.       } else
  170.       if (l->rsize == sizeof(double)) {
  171.         ((int*)l->raddr)[0] = ((int*)i)[0];
  172.         ((int*)l->raddr)[1] = ((int*)i)[1];
  173.       } else {
  174.         int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
  175.         while (--n >= 0)
  176.           ((__avword*)l->raddr)[n] = ((__avword*)i)[n];
  177.       }
  178.     } else {
  179.       /* normal struct return convention */
  180.       if (l->flags & __AV_REGISTER_STRUCT_RETURN) {
  181.         if (l->rsize == sizeof(char)) {
  182.           RETURN(char, i);
  183.         } else
  184.         if (l->rsize == sizeof(short)) {
  185.           RETURN(short, i);
  186.         } else
  187.         if (l->rsize == sizeof(int)) {
  188.           RETURN(int, i);
  189.         } else
  190.         if (l->rsize == 2*sizeof(__avword)) {
  191.           ((__avword*)l->raddr)[0] = i;
  192.           ((__avword*)l->raddr)[1] = iret2;
  193.         }
  194.       }
  195.     }
  196.   }
  197.   return 0;
  198. }
  199. #endif /*_avcall_rs6000_c */