1
#ifndef _avcall_m68k_amiga_c /*-*- C -*-*/
2
#define _avcall_m68k_amiga_c
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>
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.
13
/*----------------------------------------------------------------------
14
!!! THIS ROUTINE MUST BE COMPILED gcc -O !!!
16
Foreign function interface for a m68k Amiga with gcc.
18
This calls a C function with an argument list built up using macros
21
M68k Argument Passing Conventions:
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.
29
Some specific arguments may be passed in registers.
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"
36
#define RETURN(TYPE,VAL) (*(TYPE*)l->raddr = (TYPE)(VAL))
39
__builtin_avcall(av_alist* l)
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 */
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 */
55
__avword* argframe = sp; /* stack offset for argument list */
56
int arglen = l->aptr - l->args;
59
for (i = 0; i < arglen; i++) /* push function args onto stack */
60
argframe[i] = l->args[i];
63
if (l->rtype == __AVstruct) /* put struct return address into a1 */
64
l->regargs[8+1] = (__avword)(l->raddr);
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.
70
__asm__("moveml %1,%0" /* %1 == #0x7ffc == d2-d7/a0-a6 */
71
: "=m" (regspace[0]) : "n" (__SAVED_REGS));
73
__asm__("movel %0,sp@-" : : "g" (&&return_here)); /* prepare function call */
74
__asm__("movel %0,sp@-" : : "g" (l->func));
76
/* put some arguments into registers */
77
__asm__("moveml %0,#x7fff" /* 0x7fff == d0-d7/a0-a6 */
80
/* no need for a clobber list since we save the registers ourselves */
83
__asm__("rts" : "=d" (iret) : : "d1"); /* call function */
84
return_here: /* function returns here */
86
__asm__("moveml %0,%1" /* %1 == #0x7ffc == d2-d7/a0-a6 */
87
: : "m" (regspace[0]), "n" (__SAVED_REGS) ); /* restore registers */
89
sp += __AV_ALIST_WORDS + __NUM_SAVED_REGS; /* restore stack pointer */
91
/* save return value */
92
if (l->rtype == __AVvoid) {
94
if (l->rtype == __AVword) {
95
RETURN(__avword, iret);
97
if (l->rtype == __AVchar) {
100
if (l->rtype == __AVschar) {
101
RETURN(signed char, iret);
103
if (l->rtype == __AVuchar) {
104
RETURN(unsigned char, iret);
106
if (l->rtype == __AVshort) {
109
if (l->rtype == __AVushort) {
110
RETURN(unsigned short, iret);
112
if (l->rtype == __AVint) {
115
if (l->rtype == __AVuint) {
116
RETURN(unsigned int, iret);
118
if (l->rtype == __AVlong) {
121
if (l->rtype == __AVulong) {
122
RETURN(unsigned long, iret);
124
if (l->rtype == __AVlonglong || l->rtype == __AVulonglong) {
125
((__avword*)l->raddr)[0] = iret;
126
((__avword*)l->raddr)[1] = iret2;
128
if (l->rtype == __AVfloat) {
129
if (l->flags & __AV_SUNCC_FLOAT_RETURN) {
130
RETURN(float, (float)dret);
135
if (l->rtype == __AVdouble) {
136
RETURN(double, dret);
138
if (l->rtype == __AVvoidp) {
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 */
148
if (l->rsize == sizeof(short)) {
152
if (l->rsize == sizeof(int)) {
156
if (l->rsize == 2*sizeof(__avword)) {
157
((__avword*)l->raddr)[0] = iret;
158
((__avword*)l->raddr)[1] = iret2;
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);
167
if (l->rsize == sizeof(short)) {
168
RETURN(short, *(short*)iret);
170
if (l->rsize == sizeof(int)) {
171
RETURN(int, *(int*)iret);
173
if (l->rsize == sizeof(double)) {
174
((int*)l->raddr)[0] = ((int*)iret)[0];
175
((int*)l->raddr)[1] = ((int*)iret)[1];
177
int n = (l->rsize + sizeof(__avword)-1)/sizeof(__avword);
179
((__avword*)l->raddr)[n] = ((__avword*)iret)[n];
182
/* normal struct return convention */
189
#endif /*_avcall_m68k_amiga_c */