summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/fiddle/libffi-3.2.1/src/nios2/sysv.S
blob: 75f442bbeeb9bb9d2f5b3cb40b617ecface12263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Low-level libffi support for Altera Nios II.

   Copyright (c) 2013 Mentor Graphics.

   Permission is hereby granted, free of charge, to any person obtaining
   a copy of this software and associated documentation files (the
   ``Software''), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense, and/or sell copies of the Software, and to
   permit persons to whom the Software is furnished to do so, subject to
   the following conditions:
   
   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

/* This function is declared on the C side as 

   extern UINT64 ffi_call_sysv (void (*arghook) (char *, extended_cif *),
                                extended_cif *ecif,
                                unsigned nbytes, 
                                void (*fn) (void));
        
   On input, the arguments appear as
        r4 = arghook
        r5 = ecif
        r6 = nbytes
        r7 = fn
*/

        .section        .text
        .align  2
        .global ffi_call_sysv
        .type   ffi_call_sysv, @function

ffi_call_sysv:
        .cfi_startproc

        /* Create the stack frame, saving r16 so we can use it locally.  */
        addi    sp, sp, -12
        .cfi_def_cfa_offset 12
        stw     ra, 8(sp)
        stw     fp, 4(sp)
        stw     r16, 0(sp)
        .cfi_offset 31, -4
        .cfi_offset 28, -8
        .cfi_offset 16, -12
        mov     fp, sp
        .cfi_def_cfa_register 28
        mov     r16, r7

        /* Adjust the stack pointer to create the argument buffer
           nbytes long.  */
        sub     sp, sp, r6

        /* Call the arghook function.  */
        mov     r2, r4          /* fn */
        mov     r4, sp          /* argbuffer */
        callr   r2              /* r5 already contains ecif */

        /* Pop off the first 16 bytes of the argument buffer on the stack,
           transferring the contents to the argument registers.  */
        ldw     r4, 0(sp)
        ldw     r5, 4(sp)
        ldw     r6, 8(sp)
        ldw     r7, 12(sp)
        addi    sp, sp, 16

        /* Call the user function, which leaves its result in r2 and r3.  */
        callr   r16

        /* Pop off the stack frame.  */
        mov     sp, fp
        ldw     ra, 8(sp)
        ldw     fp, 4(sp)
        ldw     r16, 0(sp)
        addi    sp, sp, 12
        ret
        .cfi_endproc
        .size   ffi_call_sysv, .-ffi_call_sysv


/* Closure trampolines jump here after putting the C helper address
   in r9 and the closure pointer in r10.  The user-supplied arguments
   to the closure are in the normal places, in r4-r7 and on the
   stack.  Push the register arguments on the stack too and then call the
   C helper function to deal with them.  */
   
        .section        .text
        .align  2
        .global ffi_closure_sysv
        .type   ffi_closure_sysv, @function

ffi_closure_sysv:
        .cfi_startproc

        /* Create the stack frame, pushing the register args on the stack
           just below the stack args.  This is the same trick illustrated
           in Figure 7-3 in the Nios II Processor Reference Handbook, used
           for variable arguments and structures passed by value.  */
        addi    sp, sp, -20
        .cfi_def_cfa_offset 20
        stw     ra, 0(sp)
        .cfi_offset 31, -20
        stw     r4, 4(sp)
        .cfi_offset 4, -16
        stw     r5, 8(sp)
        .cfi_offset 5, -12
        stw     r6, 12(sp)
        .cfi_offset 6, -8
        stw     r7, 16(sp)
        .cfi_offset 7, -4

        /* Call the helper.
           r4 = pointer to arguments on stack
           r5 = closure pointer (loaded in r10 by the trampoline)
           r9 = address of helper function (loaded by trampoline) */
        addi    r4, sp, 4
        mov     r5, r10
        callr   r9
        
        /* Pop the stack and return.  */
        ldw     ra, 0(sp)
        addi    sp, sp, 20
        .cfi_def_cfa_offset -20
        ret
        .cfi_endproc
        .size   ffi_closure_sysv, .-ffi_closure_sysv