1 /* -----------------------------------------------------------------------
2    ffi.c - Copyright (c) 2012  Alexandre K. I. de Mendonca <alexandre.keunecke@gmail.com>,
3 							   Paulo Pizarro <paulo.pizarro@gmail.com>
4 
5    Blackfin Foreign Function Interface
6 
7    Permission is hereby granted, free of charge, to any person obtaining
8    a copy of this software and associated documentation files (the
9    ``Software''), to deal in the Software without restriction, including
10    without limitation the rights to use, copy, modify, merge, publish,
11    distribute, sublicense, and/or sell copies of the Software, and to
12    permit persons to whom the Software is furnished to do so, subject to
13    the following conditions:
14 
15    The above copyright notice and this permission notice shall be included
16    in all copies or substantial portions of the Software.
17 
18    THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
19    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21    NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25    DEALINGS IN THE SOFTWARE.
26    ----------------------------------------------------------------------- */
27 #include <ffi.h>
28 #include <ffi_common.h>
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 
33 /* Maximum number of GPRs available for argument passing.  */
34 #define MAX_GPRARGS 3
35 
36 /*
37  * Return types
38  */
39 #define FFIBFIN_RET_VOID 0
40 #define FFIBFIN_RET_BYTE 1
41 #define FFIBFIN_RET_HALFWORD 2
42 #define FFIBFIN_RET_INT64 3
43 #define FFIBFIN_RET_INT32 4
44 
45 /*====================================================================*/
46 /*                          PROTOTYPE          *
47  /*====================================================================*/
48 void ffi_prep_args(unsigned char *, extended_cif *);
49 
50 /*====================================================================*/
51 /*                          Externals                                 */
52 /*                          (Assembly)                                */
53 /*====================================================================*/
54 
55 extern void ffi_call_SYSV(unsigned, extended_cif *, void(*)(unsigned char *, extended_cif *), unsigned, void *, void(*fn)(void));
56 
57 /*====================================================================*/
58 /*                          Implementation                            */
59 /*                                                            */
60 /*====================================================================*/
61 
62 
63 /*
64  * This function calculates the return type (size) based on type.
65  */
66 
ffi_prep_cif_machdep(ffi_cif * cif)67 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
68 {
69    /* --------------------------------------*
70     *   Return handling                *
71     * --------------------------------------*/
72    switch (cif->rtype->type) {
73       case FFI_TYPE_VOID:
74          cif->flags = FFIBFIN_RET_VOID;
75          break;
76       case FFI_TYPE_UINT16:
77       case FFI_TYPE_SINT16:
78          cif->flags = FFIBFIN_RET_HALFWORD;
79          break;
80       case FFI_TYPE_UINT8:
81          cif->flags = FFIBFIN_RET_BYTE;
82          break;
83       case FFI_TYPE_INT:
84       case FFI_TYPE_UINT32:
85       case FFI_TYPE_SINT32:
86       case FFI_TYPE_FLOAT:
87       case FFI_TYPE_POINTER:
88       case FFI_TYPE_SINT8:
89          cif->flags = FFIBFIN_RET_INT32;
90          break;
91       case FFI_TYPE_SINT64:
92       case FFI_TYPE_UINT64:
93       case FFI_TYPE_DOUBLE:
94           cif->flags = FFIBFIN_RET_INT64;
95           break;
96       case FFI_TYPE_STRUCT:
97          if (cif->rtype->size <= 4){
98         	 cif->flags = FFIBFIN_RET_INT32;
99          }else if (cif->rtype->size == 8){
100         	 cif->flags = FFIBFIN_RET_INT64;
101          }else{
102         	 //it will return via a hidden pointer in P0
103         	 cif->flags = FFIBFIN_RET_VOID;
104          }
105          break;
106       default:
107          FFI_ASSERT(0);
108          break;
109    }
110    return FFI_OK;
111 }
112 
113 /*
114  * This will prepare the arguments and will call the assembly routine
115  * cif = the call interface
116  * fn = the function to be called
117  * rvalue = the return value
118  * avalue = the arguments
119  */
ffi_call(ffi_cif * cif,void (* fn)(void),void * rvalue,void ** avalue)120 void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
121 {
122    int ret_type = cif->flags;
123    extended_cif ecif;
124    ecif.cif = cif;
125    ecif.avalue = avalue;
126    ecif.rvalue = rvalue;
127 
128    switch (cif->abi) {
129       case FFI_SYSV:
130          ffi_call_SYSV(cif->bytes, &ecif, ffi_prep_args, ret_type, ecif.rvalue, fn);
131          break;
132       default:
133          FFI_ASSERT(0);
134          break;
135    }
136 }
137 
138 
139 /*
140 * This function prepares the parameters (copies them from the ecif to the stack)
141 *  to call the function (ffi_prep_args is called by the assembly routine in file
142 *  sysv.S, which also calls the actual function)
143 */
ffi_prep_args(unsigned char * stack,extended_cif * ecif)144 void ffi_prep_args(unsigned char *stack, extended_cif *ecif)
145 {
146    register unsigned int i = 0;
147    void **p_argv;
148    unsigned char *argp;
149    ffi_type **p_arg;
150    argp = stack;
151    p_argv = ecif->avalue;
152    for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
153         (i != 0);
154         i--, p_arg++) {
155       size_t z;
156       z = (*p_arg)->size;
157       if (z < sizeof(int)) {
158          z = sizeof(int);
159          switch ((*p_arg)->type) {
160             case FFI_TYPE_SINT8: {
161                   signed char v = *(SINT8 *)(* p_argv);
162                   signed int t = v;
163                   *(signed int *) argp = t;
164                }
165                break;
166             case FFI_TYPE_UINT8: {
167                   unsigned char v = *(UINT8 *)(* p_argv);
168                   unsigned int t = v;
169                   *(unsigned int *) argp = t;
170                }
171                break;
172             case FFI_TYPE_SINT16:
173                *(signed int *) argp = (signed int) * (SINT16 *)(* p_argv);
174                break;
175             case FFI_TYPE_UINT16:
176                *(unsigned int *) argp = (unsigned int) * (UINT16 *)(* p_argv);
177                break;
178             case FFI_TYPE_STRUCT:
179                memcpy(argp, *p_argv, (*p_arg)->size);
180                break;
181             default:
182                FFI_ASSERT(0);
183                break;
184          }
185       } else if (z == sizeof(int)) {
186          *(unsigned int *) argp = (unsigned int) * (UINT32 *)(* p_argv);
187       } else {
188          memcpy(argp, *p_argv, z);
189       }
190       p_argv++;
191       argp += z;
192    }
193 }
194 
195 
196 
197