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

信息检索与抽取

开发平台:

Unix_Linux

  1. #ifndef _avcall_m68k_amiga_c /*-*- C -*-*/
  2. #define _avcall_m68k_amiga_c
  3. /**
  4.   Copyright 1993 Bill Triggs, <Bill.Triggs@inrialpes.fr>
  5.   Copyright 1995-1999 Bruno Haible, <haible@clisp.cons.org>
  6.   Copyright 1997 Jörg Höhle, <Joerg.Hoehle@gmd.de>
  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 !!!
  14.   Foreign function interface for a m68k Amiga with gcc.
  15.   This calls a C function with an argument list built up using macros
  16.   defined in av_call.h.
  17.   M68k Argument Passing Conventions:
  18.   All arguments are passed on the stack with word alignment. Doubles take
  19.   two words. Structure args are passed as true structures embedded in the
  20.   argument stack. To return a structure, the called function copies the
  21.   return value to the address supplied in register "a1". Gcc without
  22.   -fpcc-struct-return returns <= 4 byte structures as integers.
  23.   Some specific arguments may be passed in registers.
  24.   Compile this routine with gcc -O (or -O2 or -g -O) to get the right
  25.   register variables, or use the assembler version.
  26.   ----------------------------------------------------------------------*/
  27. #include "avcall.h.in"
  28. #define RETURN(TYPE,VAL) (*(TYPE*)l->raddr = (TYPE)(VAL))
  29. int
  30. __builtin_avcall(av_alist* l)
  31. {
  32.   register __avword* sp __asm__("sp");  /* C names for registers */
  33.   register __avword* sret __asm__("a1"); /* structure return pointer */
  34.   register __avword iret __asm__("d0");
  35.   register __avword iret2 __asm__("d1");
  36.   register float fret __asm__("d0"); /* d0 */
  37.   register double dret __asm__("d0"); /* d0,d1 */
  38.   #define __NUM_SAVED_REGS (6+7) /* save d2-d7/a0-a6 */
  39.   #define __SAVED_REGS 0x7ffc /* moveml mask for d2-d7/a0-a6 */
  40.   /* space for callee's stack frame and temp space for saving registers */
  41.   sp -= __AV_ALIST_WORDS + __NUM_SAVED_REGS;
  42.   #define regspace (sp+__AV_ALIST_WORDS) /* temp space for saving registers */
  43.   {
  44.     __avword* argframe = sp; /* stack offset for argument list */
  45.     int arglen = l->aptr - l->args;
  46.     int i;
  47.     for (i = 0; i < arglen; i++) /* push function args onto stack */
  48.       argframe[i] = l->args[i];
  49.   }
  50.   if (l->rtype == __AVstruct) /* put struct return address into a1 */
  51.     l->regargs[8+1] = (__avword)(l->raddr);
  52.   /* Save most registers by hand. There is no way to persuade gcc that
  53.    * they are clobbered, no matter how large we make the clobber list of
  54.    * of the asm statements.
  55.    */
  56.   __asm__("moveml %1,%0" /* %1 == #0x7ffc == d2-d7/a0-a6 */
  57.           : "=m" (regspace[0]) : "n" (__SAVED_REGS));
  58.   __asm__("movel %0,sp@-" : : "g" (&&return_here)); /* prepare function call */
  59.   __asm__("movel %0,sp@-" : : "g" (l->func));
  60. /* put some arguments into registers */
  61.   __asm__("moveml %0,#x7fff" /* 0x7fff == d0-d7/a0-a6 */
  62.           :
  63.           : "m" (l->regargs[0])
  64.           /* no need for a clobber list since we save the registers ourselves */
  65.          );
  66.   __asm__("rts" : "=d" (iret) :  : "d1"); /* call function */
  67.   return_here: /* function returns here */
  68.   __asm__("moveml %0,%1" /* %1 == #0x7ffc == d2-d7/a0-a6 */
  69.           : : "m" (regspace[0]), "n" (__SAVED_REGS) ); /* restore registers */
  70.   sp += __AV_ALIST_WORDS + __NUM_SAVED_REGS; /* restore stack pointer */
  71.   /* save return value */
  72.   if (l->rtype == __AVvoid) {
  73.   } else
  74.   if (l->rtype == __AVword) {
  75.     RETURN(__avword, iret);
  76.   } else
  77.   if (l->rtype == __AVchar) {
  78.     RETURN(char, iret);
  79.   } else
  80.   if (l->rtype == __AVschar) {
  81.     RETURN(signed char, iret);
  82.   } else
  83.   if (l->rtype == __AVuchar) {
  84.     RETURN(unsigned char, iret);
  85.   } else
  86.   if (l->rtype == __AVshort) {
  87.     RETURN(short, iret);
  88.   } else
  89.   if (l->rtype == __AVushort) {
  90.     RETURN(unsigned short, iret);
  91.   } else
  92.   if (l->rtype == __AVint) {
  93.     RETURN(int, iret);
  94.   } else
  95.   if (l->rtype == __AVuint) {
  96.     RETURN(unsigned int, iret);
  97.   } else
  98.   if (l->rtype == __AVlong) {
  99.     RETURN(long, iret);
  100.   } else
  101.   if (l->rtype == __AVulong) {
  102.     RETURN(unsigned long, iret);
  103.   } else
  104.   if (l->rtype == __AVlonglong || l->rtype == __AVulonglong) {
  105.     ((__avword*)l->raddr)[0] = iret;
  106.     ((__avword*)l->raddr)[1] = iret2;
  107.   } else
  108.   if (l->rtype == __AVfloat) {
  109.     if (l->flags & __AV_SUNCC_FLOAT_RETURN) {
  110.       RETURN(float, (float)dret);
  111.     } else {
  112.       RETURN(float, fret);
  113.     }
  114.   } else
  115.   if (l->rtype == __AVdouble) {
  116.     RETURN(double, dret);
  117.   } else
  118.   if (l->rtype == __AVvoidp) {
  119.     RETURN(void*, iret);
  120.   } else
  121.   if (l->rtype == __AVstruct) {
  122.     /* NB: On m68k, all structure sizes are divisible by 2. */
  123.     if (l->flags & __AV_REGISTER_STRUCT_RETURN) {
  124.       if (l->rsize == sizeof(char)) { /* can't occur */
  125.         RETURN(char, iret);
  126.         goto done;
  127.       } else
  128.       if (l->rsize == sizeof(short)) {
  129.         RETURN(short, iret);
  130.         goto done;
  131.       } else
  132.       if (l->rsize == sizeof(int)) {
  133.         RETURN(int, iret);
  134.         goto done;
  135.       } else
  136.       if (l->rsize == 2*sizeof(__avword)) {
  137.         ((__avword*)l->raddr)[0] = iret;
  138.         ((__avword*)l->raddr)[1] = iret2;
  139.         goto done;
  140.       }
  141.     }
  142.     if (l->flags & __AV_PCC_STRUCT_RETURN) {
  143.       /* pcc struct return convention: need a  *(TYPE*)l->raddr = *(TYPE*)iret;  */
  144.       if (l->rsize == sizeof(char)) { /* can't occur */
  145.         RETURN(char, *(char*)iret);
  146.       } else
  147.       if (l->rsize == sizeof(short)) {
  148.         RETURN(short, *(short*)iret);
  149.       } else
  150.       if (l->rsize == sizeof(int)) {
  151.         RETURN(int, *(int*)iret);
  152.       } else
  153.       if (l->rsize == sizeof(double)) {
  154.         ((int*)l->raddr)[0] = ((int*)iret)[0];
  155.         ((int*)l->raddr)[1] = ((int*)iret)[1];
  156.       } else {
  157.         int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
  158.         while (--n >= 0)
  159.           ((__avword*)l->raddr)[n] = ((__avword*)iret)[n];
  160.       }
  161.     } else {
  162.       /* normal struct return convention */
  163.     }
  164.     done: ;
  165.   }
  166.   return 0;
  167. }
  168. #endif /*_avcall_m68k_amiga_c */