~ubuntu-branches/debian/squeeze/ffcall/squeeze

« back to all changes in this revision

Viewing changes to avcall/avcall-m68k-amiga.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2010-06-26 15:29:30 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100626152930-c09y01gk3szcnykn
Tags: 1.10+cvs20100619-2
Ship to unstable

Show diffs side-by-side

added added

removed removed

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