Debian Codegraph @

src/mips/o32.S

raw
1 /* -----------------------------------------------------------------------
2 o32.S - Copyright (c) 1996, 1998, 2005 Red Hat, Inc.
3
4 MIPS Foreign Function Interface
5
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:
13
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
26
27 #define LIBFFI_ASM
28 #include <fficonfig.h>
29 #include <ffi.h>
30
31 /* Only build this code if we are compiling for o32 */
32
33 #if defined(FFI_MIPS_O32)
34
35 #define callback a0
36 #define bytes a2
37 #define flags a3
38
39 #define SIZEOF_FRAME (4 * FFI_SIZEOF_ARG + 2 * FFI_SIZEOF_ARG)
40 #define A3_OFF (SIZEOF_FRAME + 3 * FFI_SIZEOF_ARG)
41 #define FP_OFF (SIZEOF_FRAME - 2 * FFI_SIZEOF_ARG)
42 #define RA_OFF (SIZEOF_FRAME - 1 * FFI_SIZEOF_ARG)
43
44 .abicalls
45 .text
46 .align 2
47 .globl ffi_call_O32
48 .ent ffi_call_O32
49 ffi_call_O32:
50 $LFB0:
51 # Prologue
52 SUBU $sp, SIZEOF_FRAME # Frame size
53 $LCFI00:
54 REG_S $fp, FP_OFF($sp) # Save frame pointer
55 $LCFI01:
56 REG_S ra, RA_OFF($sp) # Save return address
57 $LCFI02:
58 move $fp, $sp
59
60 $LCFI03:
61 move t9, callback # callback function pointer
62 REG_S flags, A3_OFF($fp) # flags
63
64 # Allocate at least 4 words in the argstack
65 LI v0, 4 * FFI_SIZEOF_ARG
66 blt bytes, v0, sixteen
67
68 ADDU v0, bytes, 7 # make sure it is aligned
69 and v0, -8 # to an 8 byte boundry
70
71 sixteen:
72 SUBU $sp, v0 # move the stack pointer to reflect the
73 # arg space
74
75 ADDU a0, $sp, 4 * FFI_SIZEOF_ARG
76
77 jalr t9
78
79 REG_L t0, A3_OFF($fp) # load the flags word
80 SRL t2, t0, 4 # shift our arg info
81 and t0, ((1<<4)-1) # mask out the return type
82
83 ADDU $sp, 4 * FFI_SIZEOF_ARG # adjust $sp to new args
84
85 #ifndef __mips_soft_float
86 bnez t0, pass_d # make it quick for int
87 #endif
88 REG_L a0, 0*FFI_SIZEOF_ARG($sp) # just go ahead and load the
89 REG_L a1, 1*FFI_SIZEOF_ARG($sp) # four regs.
90 REG_L a2, 2*FFI_SIZEOF_ARG($sp)
91 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
92 b call_it
93
94 #ifndef __mips_soft_float
95 pass_d:
96 bne t0, FFI_ARGS_D, pass_f
97 l.d $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
98 REG_L a2, 2*FFI_SIZEOF_ARG($sp) # passing a double
99 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
100 b call_it
101
102 pass_f:
103 bne t0, FFI_ARGS_F, pass_d_d
104 l.s $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
105 REG_L a1, 1*FFI_SIZEOF_ARG($sp) # passing a float
106 REG_L a2, 2*FFI_SIZEOF_ARG($sp)
107 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
108 b call_it
109
110 pass_d_d:
111 bne t0, FFI_ARGS_DD, pass_f_f
112 l.d $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
113 l.d $f14, 2*FFI_SIZEOF_ARG($sp) # passing two doubles
114 b call_it
115
116 pass_f_f:
117 bne t0, FFI_ARGS_FF, pass_d_f
118 l.s $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
119 l.s $f14, 1*FFI_SIZEOF_ARG($sp) # passing two floats
120 REG_L a2, 2*FFI_SIZEOF_ARG($sp)
121 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
122 b call_it
123
124 pass_d_f:
125 bne t0, FFI_ARGS_DF, pass_f_d
126 l.d $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
127 l.s $f14, 2*FFI_SIZEOF_ARG($sp) # passing double and float
128 REG_L a3, 3*FFI_SIZEOF_ARG($sp)
129 b call_it
130
131 pass_f_d:
132 # assume that the only other combination must be float then double
133 # bne t0, FFI_ARGS_F_D, call_it
134 l.s $f12, 0*FFI_SIZEOF_ARG($sp) # load $fp regs from args
135 l.d $f14, 2*FFI_SIZEOF_ARG($sp) # passing double and float
136
137 #endif
138
139 call_it:
140 # Load the static chain pointer
141 REG_L t7, SIZEOF_FRAME + 6*FFI_SIZEOF_ARG($fp)
142
143 # Load the function pointer
144 REG_L t9, SIZEOF_FRAME + 5*FFI_SIZEOF_ARG($fp)
145
146 # If the return value pointer is NULL, assume no return value.
147 REG_L t1, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
148 beqz t1, noretval
149
150 and t1, t2, ((1<<4)-1)
151 bne t1, FFI_TYPE_INT, retlonglong
152 jalr t9
153 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
154 REG_S v0, 0(t0)
155 b epilogue
156
157 retlonglong:
158 # Really any 64-bit int, signed or not.
159 bne t1, FFI_TYPE_UINT64, retfloat
160 jalr t9
161 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
162 REG_S v1, 4(t0)
163 REG_S v0, 0(t0)
164 b epilogue
165
166 retfloat:
167 bne t1, FFI_TYPE_FLOAT, retdouble
168 jalr t9
169 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
170 #ifndef __mips_soft_float
171 s.s $f0, 0(t0)
172 #else
173 REG_S v0, 0(t0)
174 #endif
175 b epilogue
176
177 retdouble:
178 bne t1, FFI_TYPE_DOUBLE, retcomplex
179 jalr t9
180 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
181 #ifndef __mips_soft_float
182 s.d $f0, 0(t0)
183 #else
184 REG_S v1, 4(t0)
185 REG_S v0, 0(t0)
186 #endif
187 b epilogue
188
189 retcomplex:
190 # mask out the complex elements type.
191 # the struct of flags (bits):
192 # 0-1: arg0
193 # 2-3: arg1
194 # 4-7: return type
195 # 8-11: rtype elements type: for complex
196 # Note here: t2 is flags>>4
197 bne t1, FFI_TYPE_COMPLEX, noretval
198 jalr t9
199 REG_L t0, SIZEOF_FRAME + 4*FFI_SIZEOF_ARG($fp)
200 REG_L t1, A3_OFF($fp) # load the flags word
201 SRL t1, t1, 8
202 li t3, 3
203 beq t1, t3, 3f # double
204 li t3, 2
205 beq t1, t3, 2f # float
206 # FIXME: long double
207 slti t3, t1, 5
208 beqz t3, 5f # (u)int8/16/32/64
209 2:
210 #ifndef __mips_soft_float
211 s.s $f0, 0(t0)
212 s.s $f2, 4(t0)
213 #else
214 # FIXME: do nothing can pass all of the testsuite
215 #endif
216 b epilogue
217 3:
218 #ifndef __mips_soft_float
219 s.d $f0, 0(t0)
220 s.d $f2, 8(t0)
221 #else
222 # FIXME: do nothing can pass all of the testsuite
223 #endif
224 b epilogue
225
226 5:
227 REG_S v1, 4(t0)
228 REG_S v0, 0(t0)
229 b epilogue
230
231 noretval:
232 jalr t9
233
234 # Epilogue
235 epilogue:
236 move $sp, $fp
237 REG_L $fp, FP_OFF($sp) # Restore frame pointer
238 REG_L ra, RA_OFF($sp) # Restore return address
239 ADDU $sp, SIZEOF_FRAME # Fix stack pointer
240 j ra
241
242 $LFE0:
243 .end ffi_call_O32
244
245
246 /* ffi_closure_O32. Expects address of the passed-in ffi_closure
247 in t4 ($12). Stores any arguments passed in registers onto the
248 stack, then calls ffi_closure_mips_inner_O32, which
249 then decodes them.
250
251 Stack layout:
252
253 3 - a3 save
254 2 - a2 save
255 1 - a1 save
256 0 - a0 save, original sp
257 -1 - ra save
258 -2 - fp save
259 -3 - $16 (s0) save
260 -4 - cprestore
261 -5 - return value high (v1)
262 -6 - return value low (v0)
263 -7 - f14 (le high, be low)
264 -8 - f14 (le low, be high)
265 -9 - f12 (le high, be low)
266 -10 - f12 (le low, be high)
267 -11 - Called function a5 save
268 -12 - Called function a4 save
269 -13 - Called function a3 save
270 -14 - Called function a2 save
271 -15 - Called function a1 save
272 -16 - Called function a0 save, our sp and fp point here
273 */
274
275 #define SIZEOF_FRAME2 (16 * FFI_SIZEOF_ARG)
276 #define A3_OFF2 (SIZEOF_FRAME2 + 3 * FFI_SIZEOF_ARG)
277 #define A2_OFF2 (SIZEOF_FRAME2 + 2 * FFI_SIZEOF_ARG)
278 #define A1_OFF2 (SIZEOF_FRAME2 + 1 * FFI_SIZEOF_ARG)
279 #define A0_OFF2 (SIZEOF_FRAME2 + 0 * FFI_SIZEOF_ARG)
280 #define RA_OFF2 (SIZEOF_FRAME2 - 1 * FFI_SIZEOF_ARG)
281 #define FP_OFF2 (SIZEOF_FRAME2 - 2 * FFI_SIZEOF_ARG)
282 #define S0_OFF2 (SIZEOF_FRAME2 - 3 * FFI_SIZEOF_ARG)
283 #define GP_OFF2 (SIZEOF_FRAME2 - 4 * FFI_SIZEOF_ARG)
284 #define V1_OFF2 (SIZEOF_FRAME2 - 5 * FFI_SIZEOF_ARG)
285 #define V0_OFF2 (SIZEOF_FRAME2 - 6 * FFI_SIZEOF_ARG)
286 #define FA_1_1_OFF2 (SIZEOF_FRAME2 - 7 * FFI_SIZEOF_ARG)
287 #define FA_1_0_OFF2 (SIZEOF_FRAME2 - 8 * FFI_SIZEOF_ARG)
288 #define FA_0_1_OFF2 (SIZEOF_FRAME2 - 9 * FFI_SIZEOF_ARG)
289 #define FA_0_0_OFF2 (SIZEOF_FRAME2 - 10 * FFI_SIZEOF_ARG)
290 #define CALLED_A5_OFF2 (SIZEOF_FRAME2 - 11 * FFI_SIZEOF_ARG)
291 #define CALLED_A4_OFF2 (SIZEOF_FRAME2 - 12 * FFI_SIZEOF_ARG)
292
293 .text
294
295 .align 2
296 .globl ffi_go_closure_O32
297 .ent ffi_go_closure_O32
298 ffi_go_closure_O32:
299 $LFB1:
300 # Prologue
301 .frame $fp, SIZEOF_FRAME2, ra
302 .set noreorder
303 .cpload t9
304 .set reorder
305 SUBU $sp, SIZEOF_FRAME2
306 .cprestore GP_OFF2
307 $LCFI10:
308
309 REG_S $16, S0_OFF2($sp) # Save s0
310 REG_S $fp, FP_OFF2($sp) # Save frame pointer
311 REG_S ra, RA_OFF2($sp) # Save return address
312 $LCFI11:
313
314 move $fp, $sp
315 $LCFI12:
316
317 REG_S a0, A0_OFF2($fp)
318 REG_S a1, A1_OFF2($fp)
319 REG_S a2, A2_OFF2($fp)
320 REG_S a3, A3_OFF2($fp)
321
322 # Load ABI enum to s0
323 REG_L $16, 4($15) # cif
324 REG_L $16, 0($16) # abi is first member.
325
326 li $13, 1 # FFI_O32
327 bne $16, $13, 1f # Skip fp save if FFI_O32_SOFT_FLOAT
328
329 #ifndef __mips_soft_float
330 # Store all possible float/double registers.
331 s.d $f12, FA_0_0_OFF2($fp)
332 s.d $f14, FA_1_0_OFF2($fp)
333 #endif
334 1:
335 # prepare arguments for ffi_closure_mips_inner_O32
336 REG_L a0, 4($15) # cif
337 REG_L a1, 8($15) # fun
338 move a2, $15 # user_data = go closure
339 addu a3, $fp, V0_OFF2 # rvalue
340
341 addu t9, $fp, A0_OFF2 # ar
342 REG_S t9, CALLED_A4_OFF2($fp)
343
344 addu t9, $fp, FA_0_0_OFF2 #fpr
345 REG_S t9, CALLED_A5_OFF2($fp)
346
347 b $do_closure
348
349 $LFE1:
350 .end ffi_go_closure_O32
351
352 .align 2
353 .globl ffi_closure_O32
354 .ent ffi_closure_O32
355 ffi_closure_O32:
356 $LFB2:
357 # Prologue
358 .frame $fp, SIZEOF_FRAME2, ra
359 .set noreorder
360 .cpload t9
361 .set reorder
362 SUBU $sp, SIZEOF_FRAME2
363 .cprestore GP_OFF2
364 $LCFI20:
365 REG_S $16, S0_OFF2($sp) # Save s0
366 REG_S $fp, FP_OFF2($sp) # Save frame pointer
367 REG_S ra, RA_OFF2($sp) # Save return address
368 $LCFI21:
369 move $fp, $sp
370
371 $LCFI22:
372 # Store all possible argument registers. If there are more than
373 # four arguments, then they are stored above where we put a3.
374 REG_S a0, A0_OFF2($fp)
375 REG_S a1, A1_OFF2($fp)
376 REG_S a2, A2_OFF2($fp)
377 REG_S a3, A3_OFF2($fp)
378
379 # Load ABI enum to s0
380 REG_L $16, 20($12) # cif pointer follows tramp.
381 REG_L $16, 0($16) # abi is first member.
382
383 li $13, 1 # FFI_O32
384 bne $16, $13, 1f # Skip fp save if FFI_O32_SOFT_FLOAT
385
386 #ifndef __mips_soft_float
387 # Store all possible float/double registers.
388 s.d $f12, FA_0_0_OFF2($fp)
389 s.d $f14, FA_1_0_OFF2($fp)
390 #endif
391 1:
392 # prepare arguments for ffi_closure_mips_inner_O32
393 REG_L a0, 20($12) # cif pointer follows tramp.
394 REG_L a1, 24($12) # fun
395 REG_L a2, 28($12) # user_data
396 addu a3, $fp, V0_OFF2 # rvalue
397
398 addu t9, $fp, A0_OFF2 # ar
399 REG_S t9, CALLED_A4_OFF2($fp)
400
401 addu t9, $fp, FA_0_0_OFF2 #fpr
402 REG_S t9, CALLED_A5_OFF2($fp)
403
404 $do_closure:
405 la t9, ffi_closure_mips_inner_O32
406 # Call ffi_closure_mips_inner_O32 to do the work.
407 jalr t9
408
409 # Load the return value into the appropriate register.
410 move $8, $2
411 li $9, FFI_TYPE_VOID
412 beq $8, $9, closure_done
413
414 li $13, 1 # FFI_O32
415 bne $16, $13, 1f # Skip fp restore if FFI_O32_SOFT_FLOAT
416
417 #ifndef __mips_soft_float
418 li $9, FFI_TYPE_FLOAT
419 l.s $f0, V0_OFF2($fp)
420 beq $8, $9, closure_done
421
422 li $9, FFI_TYPE_DOUBLE
423 l.d $f0, V0_OFF2($fp)
424 beq $8, $9, closure_done
425
426 li $9, FFI_TYPE_COMPLEX
427 bne $8, $9, 1f
428
429 li $9, 8
430 l.s $f0, V0_OFF2($fp)
431 l.s $f2, V1_OFF2($fp)
432 beq $3, $9, closure_done
433
434 li $9, 16
435 l.d $f0, V0_OFF2($fp)
436 l.d $f2, (V0_OFF2+8)($fp)
437 beq $3, $9, closure_done
438 #endif
439 1:
440 REG_L $3, V1_OFF2($fp)
441 REG_L $2, V0_OFF2($fp)
442
443 closure_done:
444 # Epilogue
445 move $sp, $fp
446 REG_L $16, S0_OFF2($sp) # Restore s0
447 REG_L $fp, FP_OFF2($sp) # Restore frame pointer
448 REG_L ra, RA_OFF2($sp) # Restore return address
449 ADDU $sp, SIZEOF_FRAME2
450 j ra
451 $LFE2:
452 .end ffi_closure_O32
453
454 /* DWARF-2 unwind info. */
455
456 .section .eh_frame,"a",@progbits
457 $Lframe0:
458 .4byte $LECIE0-$LSCIE0 # Length of Common Information Entry
459 $LSCIE0:
460 .4byte 0x0 # CIE Identifier Tag
461 .byte 0x1 # CIE Version
462 .ascii "zR\0" # CIE Augmentation
463 .uleb128 0x1 # CIE Code Alignment Factor
464 .sleb128 4 # CIE Data Alignment Factor
465 .byte 0x1f # CIE RA Column
466 .uleb128 0x1 # Augmentation size
467 .byte 0x00 # FDE Encoding (absptr)
468 .byte 0xc # DW_CFA_def_cfa
469 .uleb128 0x1d
470 .uleb128 0x0
471 .align 2
472 $LECIE0:
473
474 $LSFDE0:
475 .4byte $LEFDE0-$LASFDE0 # FDE Length
476 $LASFDE0:
477 .4byte $LASFDE0-$Lframe0 # FDE CIE offset
478 .4byte $LFB0 # FDE initial location
479 .4byte $LFE0-$LFB0 # FDE address range
480 .uleb128 0x0 # Augmentation size
481 .byte 0x4 # DW_CFA_advance_loc4
482 .4byte $LCFI00-$LFB0
483 .byte 0xe # DW_CFA_def_cfa_offset
484 .uleb128 0x18
485 .byte 0x4 # DW_CFA_advance_loc4
486 .4byte $LCFI01-$LCFI00
487 .byte 0x11 # DW_CFA_offset_extended_sf
488 .uleb128 0x1e # $fp
489 .sleb128 -2 # SIZEOF_FRAME2 - 2*FFI_SIZEOF_ARG($sp)
490 .byte 0x11 # DW_CFA_offset_extended_sf
491 .uleb128 0x1f # $ra
492 .sleb128 -1 # SIZEOF_FRAME2 - 1*FFI_SIZEOF_ARG($sp)
493 .byte 0x4 # DW_CFA_advance_loc4
494 .4byte $LCFI02-$LCFI01
495 .byte 0xc # DW_CFA_def_cfa
496 .uleb128 0x1e
497 .uleb128 0x18
498 .align 2
499 $LEFDE0:
500
501 $LSFDE1:
502 .4byte $LEFDE1-$LASFDE1 # FDE Length
503 $LASFDE1:
504 .4byte $LASFDE1-$Lframe0 # FDE CIE offset
505 .4byte $LFB1 # FDE initial location
506 .4byte $LFE1-$LFB1 # FDE address range
507 .uleb128 0x0 # Augmentation size
508 .byte 0x4 # DW_CFA_advance_loc4
509 .4byte $LCFI10-$LFB1
510 .byte 0xe # DW_CFA_def_cfa_offset
511 .uleb128 SIZEOF_FRAME2
512 .byte 0x4 # DW_CFA_advance_loc4
513 .4byte $LCFI11-$LCFI10
514 .byte 0x11 # DW_CFA_offset_extended_sf
515 .uleb128 0x10 # $16
516 .sleb128 -3 # SIZEOF_FRAME2 - 3*FFI_SIZEOF_ARG($sp)
517 .byte 0x11 # DW_CFA_offset_extended_sf
518 .uleb128 0x1e # $fp
519 .sleb128 -2 # SIZEOF_FRAME2 - 2*FFI_SIZEOF_ARG($sp)
520 .byte 0x11 # DW_CFA_offset_extended_sf
521 .uleb128 0x1f # $ra
522 .sleb128 -1 # SIZEOF_FRAME2 - 1*FFI_SIZEOF_ARG($sp)
523 .byte 0x4 # DW_CFA_advance_loc4
524 .4byte $LCFI12-$LCFI11
525 .byte 0xc # DW_CFA_def_cfa
526 .uleb128 0x1e
527 .uleb128 SIZEOF_FRAME2
528 .align 2
529 $LEFDE1:
530
531 $LSFDE2:
532 .4byte $LEFDE2-$LASFDE2 # FDE Length
533 $LASFDE2:
534 .4byte $LASFDE2-$Lframe0 # FDE CIE offset
535 .4byte $LFB2 # FDE initial location
536 .4byte $LFE2-$LFB2 # FDE address range
537 .uleb128 0x0 # Augmentation size
538 .byte 0x4 # DW_CFA_advance_loc4
539 .4byte $LCFI20-$LFB2
540 .byte 0xe # DW_CFA_def_cfa_offset
541 .uleb128 SIZEOF_FRAME2
542 .byte 0x4 # DW_CFA_advance_loc4
543 .4byte $LCFI21-$LCFI20
544 .byte 0x11 # DW_CFA_offset_extended_sf
545 .uleb128 0x10 # $16
546 .sleb128 -3 # SIZEOF_FRAME2 - 3*FFI_SIZEOF_ARG($sp)
547 .byte 0x11 # DW_CFA_offset_extended_sf
548 .uleb128 0x1e # $fp
549 .sleb128 -2 # SIZEOF_FRAME2 - 2*FFI_SIZEOF_ARG($sp)
550 .byte 0x11 # DW_CFA_offset_extended_sf
551 .uleb128 0x1f # $ra
552 .sleb128 -1 # SIZEOF_FRAME2 - 1*FFI_SIZEOF_ARG($sp)
553 .byte 0x4 # DW_CFA_advance_loc4
554 .4byte $LCFI22-$LCFI21
555 .byte 0xc # DW_CFA_def_cfa
556 .uleb128 0x1e
557 .uleb128 SIZEOF_FRAME2
558 .align 2
559 $LEFDE2:
560
561 #endif
562
563 #if defined __ELF__ && defined __linux__
564 .section .note.GNU-stack,"",%progbits
565 #endif