summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/fiddle/libffi-3.2.1/src/cris/sysv.S
blob: 79abaee4d959ce87cad13857a68ddb63fc3a95e7 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -----------------------------------------------------------------------
   sysv.S - Copyright (c) 2004 Simon Posnjak
            Copyright (c) 2005 Axis Communications AB

   CRIS Foreign Function Interface

   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 SIMON POSNJAK 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.
   ----------------------------------------------------------------------- */

#define LIBFFI_ASM
#include <ffi.h>
#define CONCAT(x,y) x ## y
#define XCONCAT(x,y) CONCAT (x, y)
#define L(x) XCONCAT (__USER_LABEL_PREFIX__, x)

        .text

        ;; OK, when we get called we should have this (according to
        ;; AXIS ETRAX 100LX Programmer's Manual chapter 6.3).
        ;;
        ;; R10:  ffi_prep_args (func. pointer)
        ;; R11:  &ecif
        ;; R12:  cif->bytes
        ;; R13:  fig->flags
        ;; sp+0: ecif.rvalue
        ;; sp+4: fn (function pointer to the function that we need to call)

        .globl  L(ffi_call_SYSV)
        .type   L(ffi_call_SYSV),@function
        .hidden L(ffi_call_SYSV)

L(ffi_call_SYSV):
        ;; Save the regs to the stack.
        push $srp
        ;; Used for stack pointer saving.
        push $r6
        ;; Used for function address pointer.
        push $r7
        ;; Used for stack pointer saving.
        push $r8
        ;; We save fig->flags to stack we will need them after we
        ;; call The Function.
        push $r13

        ;; Saving current stack pointer.
        move.d $sp,$r8
        move.d $sp,$r6

        ;; Move address of ffi_prep_args to r13.
        move.d $r10,$r13

        ;; Make room on the stack for the args of fn.
        sub.d  $r12,$sp

        ;; Function void ffi_prep_args(char *stack, extended_cif *ecif) parameters are:
        ;;      r10 <-- stack pointer
        ;;      r11 <-- &ecif (already there)
        move.d $sp,$r10

        ;; Call the function.
        jsr $r13

        ;; Save the size of the structures which are passed on stack.
        move.d $r10,$r7

        ;; Move first four args in to r10..r13.
        move.d [$sp+0],$r10
        move.d [$sp+4],$r11
        move.d [$sp+8],$r12
        move.d [$sp+12],$r13

        ;; Adjust the stack and check if any parameters are given on stack.
        addq 16,$sp
        sub.d $r7,$r6
        cmp.d $sp,$r6

        bpl go_on
        nop

go_on_no_params_on_stack:
        move.d $r6,$sp

go_on:
        ;; Discover if we need to put rval address in to r9.
        move.d [$r8+0],$r7
        cmpq FFI_TYPE_STRUCT,$r7
        bne call_now
        nop

        ;; Move rval address to $r9.
        move.d [$r8+20],$r9

call_now:
        ;; Move address of The Function in to r7.
        move.d [$r8+24],$r7

        ;; Call The Function.
        jsr $r7

        ;; Reset stack.
        move.d $r8,$sp

        ;; Load rval type (fig->flags) in to r13.
        pop $r13

        ;; Detect rval type.
        cmpq FFI_TYPE_VOID,$r13
        beq epilogue

        cmpq FFI_TYPE_STRUCT,$r13
        beq epilogue

        cmpq FFI_TYPE_DOUBLE,$r13
        beq return_double_or_longlong

        cmpq FFI_TYPE_UINT64,$r13
        beq return_double_or_longlong

        cmpq FFI_TYPE_SINT64,$r13
        beq return_double_or_longlong
        nop

        ;; Just return the 32 bit value.
        ba return
        nop

return_double_or_longlong:
        ;; Load half of the rval to r10 and the other half to r11.
        move.d [$sp+16],$r13
        move.d $r10,[$r13]
        addq 4,$r13
        move.d $r11,[$r13]
        ba epilogue
        nop

return:
        ;; Load the rval to r10.
        move.d [$sp+16],$r13
        move.d $r10,[$r13]

epilogue:
        pop $r8
        pop $r7
        pop $r6
        Jump [$sp+]

        .size   ffi_call_SYSV,.-ffi_call_SYSV

/* Save R10..R13 into an array, somewhat like varargs.  Copy the next
   argument too, to simplify handling of any straddling parameter.
   Save R9 and SP after those.  Jump to function handling the rest.
   Since this is a template, copied and the main function filled in by
   the user.  */

        .globl  L(ffi_cris_trampoline_template)
        .type   L(ffi_cris_trampoline_template),@function
        .hidden L(ffi_cris_trampoline_template)

L(ffi_cris_trampoline_template):
0:
        /* The value we get for "PC" is right after the prefix instruction,
           two bytes from the beginning, i.e. 0b+2. */
        move.d $r10,[$pc+2f-(0b+2)]
        move.d $pc,$r10
1:
        addq 2f-1b+4,$r10
        move.d $r11,[$r10+]
        move.d $r12,[$r10+]
        move.d $r13,[$r10+]
        move.d [$sp],$r11
        move.d $r11,[$r10+]
        move.d $r9,[$r10+]
        move.d $sp,[$r10+]
        subq FFI_CRIS_TRAMPOLINE_DATA_PART_SIZE,$r10
        move.d 0,$r11
3:
        jump 0
2:
        .size   ffi_cris_trampoline_template,.-0b

/* This macro create a constant usable as "extern const int \name" in
   C from within libffi, when \name has no prefix decoration.  */

        .macro const name,value
        .globl  \name
        .type   \name,@object
        .hidden \name
\name:
        .dword  \value
        .size   \name,4
        .endm

/* Constants for offsets within the trampoline.  We could do this with
   just symbols, avoiding memory contents and memory accesses, but the
   C usage code would look a bit stranger.  */

        const L(ffi_cris_trampoline_fn_offset),2b-4-0b
        const L(ffi_cris_trampoline_closure_offset),3b-4-0b