1 /* -----------------------------------------------------------------------
2 o32.S - Copyright (c) 1996, 1998 Red Hat, Inc.
4 MIPS Foreign Function Interface
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 ----------------------------------------------------------------------- */
27 #include <fficonfig.h>
30 /* Only build this code if we are compiling for o32 */
32 #if defined(FFI_MIPS_O32)
38 #define SIZEOF_FRAME ( 4 * FFI_SIZEOF_ARG + 2 * FFI_SIZEOF_ARG )
47 SUBU $sp, SIZEOF_FRAME # Frame size
48 REG_S $fp, SIZEOF_FRAME - 2*FFI_SIZEOF_ARG($sp) # Save frame pointer
49 REG_S ra, SIZEOF_FRAME - 1*FFI_SIZEOF_ARG($sp) # Save return address
52 move t9, callback # callback function pointer
53 REG_S flags, SIZEOF_FRAME + 3*FFI_SIZEOF_ARG($fp) # flags
55 # Allocate at least 4 words in the argstack
57 bge bytes, 4 * FFI_SIZEOF_ARG, bigger
58 LI v0, 4 * FFI_SIZEOF_ARG
62 ADDU t0, v0, 2 * FFI_SIZEOF_ARG -1 # make sure it is aligned
63 and v0, t0, -2 * FFI_SIZEOF_ARG # to an 8 byte boundry
66 SUBU $sp, $sp, v0 # move the stack pointer to reflect the
69 ADDU a0, $sp, 4 * FFI_SIZEOF_ARG
70 ADDU a3, $fp, SIZEOF_FRAME + 3*FFI_SIZEOF_ARG
74 REG_L t0, SIZEOF_FRAME + 3*FFI_SIZEOF_ARG($fp) # load the flags word
75 add t2, t0, 0 # and copy it into t2
77 and t0, ((1<<4)-1) # mask out the return type
78 SRL t2, 4 # shift our arg info
80 ADDU $sp, $sp, 4 * FFI_SIZEOF_ARG # adjust $sp to new args
82 bnez t0, pass_d # make it quick for int
83 REG_L a0, 0*FFI_SIZEOF_ARG($sp) # just go ahead and load the
84 REG_L a1, 1*FFI_SIZEOF_ARG($sp) # four regs.
85 REG_L a2, 2*FFI_SIZEOF_ARG($sp)
86 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
90 bne t0, FFI_ARGS_D, pass_f
91 l.d $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
92 REG_L a2, 2*FFI_SIZEOF_ARG($sp) # passing a double
93 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
97 bne t0, FFI_ARGS_F, pass_d_d
98 l.s $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
99 REG_L a1, 1*FFI_SIZEOF_ARG($sp) # passing a float
100 REG_L a2, 2*FFI_SIZEOF_ARG($sp)
101 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
105 bne t0, FFI_ARGS_DD, pass_f_f
106 l.d $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
107 l.d $f14, 2*FFI_SIZEOF_ARG($sp) # passing two doubles
111 bne t0, FFI_ARGS_FF, pass_d_f
112 l.s $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
113 l.s $f14, 1*FFI_SIZEOF_ARG($sp) # passing two floats
114 REG_L a2, 2*FFI_SIZEOF_ARG($sp)
115 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
119 bne t0, FFI_ARGS_DF, pass_f_d
120 l.d $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
121 l.s $f14, 2*FFI_SIZEOF_ARG($sp) # passing double and float
122 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
126 # assume that the only other combination must be float then double
127 # bne t0, FFI_ARGS_F_D, call_it
128 l.s $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
129 l.d $f14, 2*FFI_SIZEOF_ARG($sp) # passing double and float
132 # Load the function pointer
133 REG_L t9, SIZEOF_FRAME + 5*FFI_SIZEOF_ARG($fp)
135 # If the return value pointer is NULL, assume no return value.
136 REG_L t1, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
139 bne t2, FFI_TYPE_INT, retfloat
141 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
146 bne t2, FFI_TYPE_FLOAT, retdouble
148 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
153 bne t2, FFI_TYPE_DOUBLE, noretval
155 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
165 REG_L $fp, SIZEOF_FRAME - 2*FFI_SIZEOF_ARG($sp) # Restore frame pointer
166 REG_L ra, SIZEOF_FRAME - 1*FFI_SIZEOF_ARG($sp) # Restore return address
167 ADDU $sp, SIZEOF_FRAME # Fix stack pointer