~ubuntu-branches/ubuntu/lucid/gauche-c-wrapper/lucid

« back to all changes in this revision

Viewing changes to libffi/src/x86/unix64.S

  • Committer: Bazaar Package Importer
  • Author(s): NIIBE Yutaka
  • Date: 2008-04-07 09:15:03 UTC
  • Revision ID: james.westby@ubuntu.com-20080407091503-wu0h414koe95kj4i
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -----------------------------------------------------------------------
 
2
   unix64.S - Copyright (c) 2002  Bo Thorsen <bo@suse.de>
 
3
 
 
4
   x86-64 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, 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
   ----------------------------------------------------------------------- */
 
25
 
 
26
#ifdef __x86_64__
 
27
#define LIBFFI_ASM      
 
28
#include <fficonfig.h>
 
29
#include <ffi.h>
 
30
 
 
31
.text
 
32
 
 
33
/* ffi_call_unix64 (void *args, unsigned long bytes, unsigned flags,
 
34
                    void *raddr, void (*fnaddr)());
 
35
 
 
36
   Bit o trickiness here -- ARGS+BYTES is the base of the stack frame
 
37
   for this function.  This has been allocated by ffi_call.  We also
 
38
   deallocate some of the stack that has been alloca'd.  */
 
39
 
 
40
        .align  2
 
41
        .globl  ffi_call_unix64
 
42
        .type   ffi_call_unix64,@function
 
43
 
 
44
ffi_call_unix64:
 
45
.LUW0:
 
46
        movq    (%rsp), %r10            /* Load return address.  */
 
47
        leaq    (%rdi, %rsi), %rax      /* Find local stack base.  */
 
48
        movq    %rdx, (%rax)            /* Save flags.  */
 
49
        movq    %rcx, 8(%rax)           /* Save raddr.  */
 
50
        movq    %rbp, 16(%rax)          /* Save old frame pointer.  */
 
51
        movq    %r10, 24(%rax)          /* Relocate return address.  */
 
52
        movq    %rax, %rbp              /* Finalize local stack frame.  */
 
53
.LUW1:
 
54
        movq    %rdi, %r10              /* Save a copy of the register area. */
 
55
        movq    %r8, %r11               /* Save a copy of the target fn.  */
 
56
        movl    %r9d, %eax              /* Set number of SSE registers.  */
 
57
 
 
58
        /* Load up all argument registers.  */
 
59
        movq    (%r10), %rdi
 
60
        movq    8(%r10), %rsi
 
61
        movq    16(%r10), %rdx
 
62
        movq    24(%r10), %rcx
 
63
        movq    32(%r10), %r8
 
64
        movq    40(%r10), %r9
 
65
        testl   %eax, %eax
 
66
        jnz     .Lload_sse
 
67
.Lret_from_load_sse:
 
68
 
 
69
        /* Deallocate the reg arg area.  */
 
70
        leaq    176(%r10), %rsp
 
71
 
 
72
        /* Call the user function.  */
 
73
        call    *%r11
 
74
 
 
75
        /* Deallocate stack arg area; local stack frame in redzone.  */
 
76
        leaq    24(%rbp), %rsp
 
77
 
 
78
        movq    0(%rbp), %rcx           /* Reload flags.  */
 
79
        movq    8(%rbp), %rdi           /* Reload raddr.  */
 
80
        movq    16(%rbp), %rbp          /* Reload old frame pointer.  */
 
81
.LUW2:
 
82
 
 
83
        /* The first byte of the flags contains the FFI_TYPE.  */
 
84
        movzbl  %cl, %r10d
 
85
        leaq    .Lstore_table(%rip), %r11
 
86
        movslq  (%r11, %r10, 4), %r10
 
87
        addq    %r11, %r10
 
88
        jmp     *%r10
 
89
 
 
90
        .section .rodata
 
91
.Lstore_table:
 
92
        .long   .Lst_void-.Lstore_table         /* FFI_TYPE_VOID */
 
93
        .long   .Lst_sint32-.Lstore_table       /* FFI_TYPE_INT */
 
94
        .long   .Lst_float-.Lstore_table        /* FFI_TYPE_FLOAT */
 
95
        .long   .Lst_double-.Lstore_table       /* FFI_TYPE_DOUBLE */
 
96
        .long   .Lst_ldouble-.Lstore_table      /* FFI_TYPE_LONGDOUBLE */
 
97
        .long   .Lst_uint8-.Lstore_table        /* FFI_TYPE_UINT8 */
 
98
        .long   .Lst_sint8-.Lstore_table        /* FFI_TYPE_SINT8 */
 
99
        .long   .Lst_uint16-.Lstore_table       /* FFI_TYPE_UINT16 */
 
100
        .long   .Lst_sint16-.Lstore_table       /* FFI_TYPE_SINT16 */
 
101
        .long   .Lst_uint32-.Lstore_table       /* FFI_TYPE_UINT32 */
 
102
        .long   .Lst_sint32-.Lstore_table       /* FFI_TYPE_SINT32 */
 
103
        .long   .Lst_int64-.Lstore_table        /* FFI_TYPE_UINT64 */
 
104
        .long   .Lst_int64-.Lstore_table        /* FFI_TYPE_SINT64 */
 
105
        .long   .Lst_struct-.Lstore_table       /* FFI_TYPE_STRUCT */
 
106
        .long   .Lst_int64-.Lstore_table        /* FFI_TYPE_POINTER */
 
107
 
 
108
        .text
 
109
        .align 2
 
110
.Lst_void:
 
111
        ret
 
112
        .align 2
 
113
 
 
114
.Lst_uint8:
 
115
        movzbq  %al, %rax
 
116
        movq    %rax, (%rdi)
 
117
        ret
 
118
        .align 2
 
119
.Lst_sint8:
 
120
        movsbq  %al, %rax
 
121
        movq    %rax, (%rdi)
 
122
        ret
 
123
        .align 2
 
124
.Lst_uint16:
 
125
        movzwq  %ax, %rax
 
126
        movq    %rax, (%rdi)
 
127
        .align 2
 
128
.Lst_sint16:
 
129
        movswq  %ax, %rax
 
130
        movq    %rax, (%rdi)
 
131
        ret
 
132
        .align 2
 
133
.Lst_uint32:
 
134
        movl    %eax, %eax
 
135
        movq    %rax, (%rdi)
 
136
        .align 2
 
137
.Lst_sint32:
 
138
        cltq
 
139
        movq    %rax, (%rdi)
 
140
        ret
 
141
        .align 2
 
142
.Lst_int64:
 
143
        movq    %rax, (%rdi)
 
144
        ret
 
145
 
 
146
        .align 2
 
147
.Lst_float:
 
148
        movss   %xmm0, (%rdi)
 
149
        ret
 
150
        .align 2
 
151
.Lst_double:
 
152
        movsd   %xmm0, (%rdi)
 
153
        ret
 
154
.Lst_ldouble:
 
155
        fstpt   (%rdi)
 
156
        ret
 
157
 
 
158
        .align 2
 
159
.Lst_struct:
 
160
        leaq    -20(%rsp), %rsi         /* Scratch area in redzone.  */
 
161
 
 
162
        /* We have to locate the values now, and since we don't want to
 
163
           write too much data into the user's return value, we spill the
 
164
           value to a 16 byte scratch area first.  Bits 8, 9, and 10
 
165
           control where the values are located.  Only one of the three
 
166
           bits will be set; see ffi_prep_cif_machdep for the pattern.  */
 
167
        movd    %xmm0, %r10
 
168
        movd    %xmm1, %r11
 
169
        testl   $0x100, %ecx
 
170
        cmovnz  %rax, %rdx
 
171
        cmovnz  %r10, %rax
 
172
        testl   $0x200, %ecx
 
173
        cmovnz  %r10, %rdx
 
174
        testl   $0x400, %ecx
 
175
        cmovnz  %r10, %rax
 
176
        cmovnz  %r11, %rdx
 
177
        movq    %rax, (%rsi)
 
178
        movq    %rdx, 8(%rsi)
 
179
 
 
180
        /* Bits 12-31 contain the true size of the structure.  Copy from
 
181
           the scratch area to the true destination.  */
 
182
        shrl    $12, %ecx
 
183
        rep movsb
 
184
        ret
 
185
 
 
186
        /* Many times we can avoid loading any SSE registers at all.
 
187
           It's not worth an indirect jump to load the exact set of
 
188
           SSE registers needed; zero or all is a good compromise.  */
 
189
        .align 2
 
190
.LUW3:
 
191
.Lload_sse:
 
192
        movdqa  48(%r10), %xmm0
 
193
        movdqa  64(%r10), %xmm1
 
194
        movdqa  80(%r10), %xmm2
 
195
        movdqa  96(%r10), %xmm3
 
196
        movdqa  112(%r10), %xmm4
 
197
        movdqa  128(%r10), %xmm5
 
198
        movdqa  144(%r10), %xmm6
 
199
        movdqa  160(%r10), %xmm7
 
200
        jmp     .Lret_from_load_sse
 
201
 
 
202
.LUW4:
 
203
        .size    ffi_call_unix64,.-ffi_call_unix64
 
204
 
 
205
        .align  2
 
206
        .globl ffi_closure_unix64
 
207
        .type   ffi_closure_unix64,@function
 
208
 
 
209
ffi_closure_unix64:
 
210
.LUW5:
 
211
        /* The carry flag is set by the trampoline iff SSE registers
 
212
           are used.  Don't clobber it before the branch instruction.  */
 
213
        leaq    -200(%rsp), %rsp
 
214
.LUW6:
 
215
        movq    %rdi, (%rsp)
 
216
        movq    %rsi, 8(%rsp)
 
217
        movq    %rdx, 16(%rsp)
 
218
        movq    %rcx, 24(%rsp)
 
219
        movq    %r8, 32(%rsp)
 
220
        movq    %r9, 40(%rsp)
 
221
        jc      .Lsave_sse
 
222
.Lret_from_save_sse:
 
223
 
 
224
        movq    %r10, %rdi
 
225
        leaq    176(%rsp), %rsi
 
226
        movq    %rsp, %rdx
 
227
        leaq    208(%rsp), %rcx
 
228
        call    ffi_closure_unix64_inner@PLT
 
229
 
 
230
        /* Deallocate stack frame early; return value is now in redzone.  */
 
231
        addq    $200, %rsp
 
232
.LUW7:
 
233
 
 
234
        /* The first byte of the return value contains the FFI_TYPE.  */
 
235
        movzbl  %al, %r10d
 
236
        leaq    .Lload_table(%rip), %r11
 
237
        movslq  (%r11, %r10, 4), %r10
 
238
        addq    %r11, %r10
 
239
        jmp     *%r10
 
240
 
 
241
        .section .rodata
 
242
.Lload_table:
 
243
        .long   .Lld_void-.Lload_table          /* FFI_TYPE_VOID */
 
244
        .long   .Lld_int32-.Lload_table         /* FFI_TYPE_INT */
 
245
        .long   .Lld_float-.Lload_table         /* FFI_TYPE_FLOAT */
 
246
        .long   .Lld_double-.Lload_table        /* FFI_TYPE_DOUBLE */
 
247
        .long   .Lld_ldouble-.Lload_table       /* FFI_TYPE_LONGDOUBLE */
 
248
        .long   .Lld_int8-.Lload_table          /* FFI_TYPE_UINT8 */
 
249
        .long   .Lld_int8-.Lload_table          /* FFI_TYPE_SINT8 */
 
250
        .long   .Lld_int16-.Lload_table         /* FFI_TYPE_UINT16 */
 
251
        .long   .Lld_int16-.Lload_table         /* FFI_TYPE_SINT16 */
 
252
        .long   .Lld_int32-.Lload_table         /* FFI_TYPE_UINT32 */
 
253
        .long   .Lld_int32-.Lload_table         /* FFI_TYPE_SINT32 */
 
254
        .long   .Lld_int64-.Lload_table         /* FFI_TYPE_UINT64 */
 
255
        .long   .Lld_int64-.Lload_table         /* FFI_TYPE_SINT64 */
 
256
        .long   .Lld_struct-.Lload_table        /* FFI_TYPE_STRUCT */
 
257
        .long   .Lld_int64-.Lload_table         /* FFI_TYPE_POINTER */
 
258
 
 
259
        .text
 
260
        .align 2
 
261
.Lld_void:
 
262
        ret
 
263
 
 
264
        .align 2
 
265
.Lld_int8:
 
266
        movzbl  -24(%rsp), %eax
 
267
        ret
 
268
        .align 2
 
269
.Lld_int16:
 
270
        movzwl  -24(%rsp), %eax
 
271
        ret
 
272
        .align 2
 
273
.Lld_int32:
 
274
        movl    -24(%rsp), %eax
 
275
        ret
 
276
        .align 2
 
277
.Lld_int64:
 
278
        movq    -24(%rsp), %rax
 
279
        ret
 
280
 
 
281
        .align 2
 
282
.Lld_float:
 
283
        movss   -24(%rsp), %xmm0
 
284
        ret
 
285
        .align 2
 
286
.Lld_double:
 
287
        movsd   -24(%rsp), %xmm0
 
288
        ret
 
289
        .align 2
 
290
.Lld_ldouble:
 
291
        fldt    -24(%rsp)
 
292
        ret
 
293
 
 
294
        .align 2
 
295
.Lld_struct:
 
296
        /* There are four possibilities here, %rax/%rdx, %xmm0/%rax,
 
297
           %rax/%xmm0, %xmm0/%xmm1.  We collapse two by always loading
 
298
           both rdx and xmm1 with the second word.  For the remaining,
 
299
           bit 8 set means xmm0 gets the second word, and bit 9 means
 
300
           that rax gets the second word.  */
 
301
        movq    -24(%rsp), %rcx
 
302
        movq    -16(%rsp), %rdx
 
303
        movq    -16(%rsp), %xmm1
 
304
        testl   $0x100, %eax
 
305
        cmovnz  %rdx, %rcx
 
306
        movd    %rcx, %xmm0
 
307
        testl   $0x200, %eax
 
308
        movq    -24(%rsp), %rax
 
309
        cmovnz  %rdx, %rax
 
310
        ret
 
311
 
 
312
        /* See the comment above .Lload_sse; the same logic applies here.  */
 
313
        .align 2
 
314
.LUW8:
 
315
.Lsave_sse:
 
316
        movdqa  %xmm0, 48(%rsp)
 
317
        movdqa  %xmm1, 64(%rsp)
 
318
        movdqa  %xmm2, 80(%rsp)
 
319
        movdqa  %xmm3, 96(%rsp)
 
320
        movdqa  %xmm4, 112(%rsp)
 
321
        movdqa  %xmm5, 128(%rsp)
 
322
        movdqa  %xmm6, 144(%rsp)
 
323
        movdqa  %xmm7, 160(%rsp)
 
324
        jmp     .Lret_from_save_sse
 
325
 
 
326
.LUW9:
 
327
        .size   ffi_closure_unix64,.-ffi_closure_unix64
 
328
 
 
329
        .section        .eh_frame,"a",@progbits
 
330
.Lframe1:
 
331
        .long   .LECIE1-.LSCIE1         /* CIE Length */
 
332
.LSCIE1:
 
333
        .long   0                       /* CIE Identifier Tag */
 
334
        .byte   1                       /* CIE Version */
 
335
        .ascii "zR\0"                   /* CIE Augmentation */
 
336
        .uleb128 1                      /* CIE Code Alignment Factor */
 
337
        .sleb128 -8                     /* CIE Data Alignment Factor */
 
338
        .byte   0x10                    /* CIE RA Column */
 
339
        .uleb128 1                      /* Augmentation size */
 
340
        .byte   0x1b                    /* FDE Encoding (pcrel sdata4) */
 
341
        .byte   0xc                     /* DW_CFA_def_cfa, %rsp offset 8 */
 
342
        .uleb128 7
 
343
        .uleb128 8
 
344
        .byte   0x80+16                 /* DW_CFA_offset, %rip offset 1*-8 */
 
345
        .uleb128 1
 
346
        .align 8
 
347
.LECIE1:
 
348
.LSFDE1:
 
349
        .long   .LEFDE1-.LASFDE1        /* FDE Length */
 
350
.LASFDE1:
 
351
        .long   .LASFDE1-.Lframe1       /* FDE CIE offset */
 
352
        .long   .LUW0-.                 /* FDE initial location */
 
353
        .long   .LUW4-.LUW0             /* FDE address range */
 
354
        .uleb128 0x0                    /* Augmentation size */
 
355
 
 
356
        .byte   0x4                     /* DW_CFA_advance_loc4 */
 
357
        .long   .LUW1-.LUW0
 
358
 
 
359
        /* New stack frame based off rbp.  This is a itty bit of unwind
 
360
           trickery in that the CFA *has* changed.  There is no easy way
 
361
           to describe it correctly on entry to the function.  Fortunately,
 
362
           it doesn't matter too much since at all points we can correctly
 
363
           unwind back to ffi_call.  Note that the location to which we
 
364
           moved the return address is (the new) CFA-8, so from the
 
365
           perspective of the unwind info, it hasn't moved.  */
 
366
        .byte   0xc                     /* DW_CFA_def_cfa, %rbp offset 32 */
 
367
        .uleb128 6
 
368
        .uleb128 32
 
369
        .byte   0x80+6                  /* DW_CFA_offset, %rbp offset 2*-8 */
 
370
        .uleb128 2
 
371
        .byte   0xa                     /* DW_CFA_remember_state */
 
372
 
 
373
        .byte   0x4                     /* DW_CFA_advance_loc4 */
 
374
        .long   .LUW2-.LUW1
 
375
        .byte   0xc                     /* DW_CFA_def_cfa, %rsp offset 8 */
 
376
        .uleb128 7
 
377
        .uleb128 8
 
378
        .byte   0xc0+6                  /* DW_CFA_restore, %rbp */
 
379
 
 
380
        .byte   0x4                     /* DW_CFA_advance_loc4 */
 
381
        .long   .LUW3-.LUW2
 
382
        .byte   0xb                     /* DW_CFA_restore_state */
 
383
 
 
384
        .align 8
 
385
.LEFDE1:
 
386
.LSFDE3:
 
387
        .long   .LEFDE3-.LASFDE3        /* FDE Length */
 
388
.LASFDE3:
 
389
        .long   .LASFDE3-.Lframe1       /* FDE CIE offset */
 
390
        .long   .LUW5-.                 /* FDE initial location */
 
391
        .long   .LUW9-.LUW5             /* FDE address range */
 
392
        .uleb128 0x0                    /* Augmentation size */
 
393
 
 
394
        .byte   0x4                     /* DW_CFA_advance_loc4 */
 
395
        .long   .LUW6-.LUW5
 
396
        .byte   0xe                     /* DW_CFA_def_cfa_offset */
 
397
        .uleb128 208
 
398
        .byte   0xa                     /* DW_CFA_remember_state */
 
399
 
 
400
        .byte   0x4                     /* DW_CFA_advance_loc4 */
 
401
        .long   .LUW7-.LUW6
 
402
        .byte   0xe                     /* DW_CFA_def_cfa_offset */
 
403
        .uleb128 8
 
404
 
 
405
        .byte   0x4                     /* DW_CFA_advance_loc4 */
 
406
        .long   .LUW8-.LUW7
 
407
        .byte   0xb                     /* DW_CFA_restore_state */
 
408
 
 
409
        .align 8
 
410
.LEFDE3:
 
411
 
 
412
#endif /* __x86_64__ */