]> git.saurik.com Git - cycript.git/blob - sig/ffi_type.cpp
Put the filename in _trace to make it more useful.
[cycript.git] / sig / ffi_type.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "Error.hpp"
23
24 #include "sig/ffi_type.hpp"
25 #include "sig/types.hpp"
26
27 #define ffi_type_slonglong ffi_type_sint64
28 #define ffi_type_ulonglong ffi_type_uint64
29
30 namespace sig {
31
32 void sig_ffi_types(
33 CYPool &pool,
34 ffi_type *(*sig_ffi_type)(CYPool &, struct Type *),
35 struct Signature *signature,
36 ffi_type **types,
37 size_t skip = 0,
38 size_t offset = 0
39 ) {
40 _assert(signature->count >= skip);
41 for (size_t index = skip; index != signature->count; ++index)
42 types[index - skip + offset] = (*sig_ffi_type)(pool, signature->elements[index].type);
43 }
44
45 ffi_type *ObjectiveC(CYPool &pool, struct Type *type) {
46 switch (type->primitive) {
47 case typename_P: return &ffi_type_pointer;
48
49 case union_P:
50 /* XXX: we can totally make this work */
51 _assert(false);
52 break;
53
54 case string_P: return &ffi_type_pointer;
55 case selector_P: return &ffi_type_pointer;
56 case block_P: return &ffi_type_pointer;
57 case object_P: return &ffi_type_pointer;
58 case boolean_P: return &ffi_type_uchar;
59 case uchar_P: return &ffi_type_uchar;
60 case uint_P: return &ffi_type_uint;
61 case ulong_P: return &ffi_type_ulong;
62 case ulonglong_P: return &ffi_type_ulonglong;
63 case ushort_P: return &ffi_type_ushort;
64
65 case array_P: {
66 // XXX: this is really lame
67 ffi_type *aggregate(new(pool) ffi_type());
68 aggregate->size = 0;
69 aggregate->alignment = 0;
70 aggregate->type = FFI_TYPE_STRUCT;
71
72 ffi_type *element(ObjectiveC(pool, type->data.data.type));
73 size_t size(type->data.data.size);
74
75 aggregate->elements = new(pool) ffi_type *[size + 1];
76 for (size_t i(0); i != size; ++i)
77 aggregate->elements[i] = element;
78 aggregate->elements[size] = NULL;
79
80 return aggregate;
81 } break;
82
83 case pointer_P: return &ffi_type_pointer;
84
85 case bit_P:
86 /* XXX: we can totally make this work */
87 _assert(false);
88 break;
89
90 case char_P: return &ffi_type_schar;
91 case double_P: return &ffi_type_double;
92 case float_P: return &ffi_type_float;
93 case int_P: return &ffi_type_sint;
94 case long_P: return &ffi_type_slong;
95 case longlong_P: return &ffi_type_slonglong;
96 case short_P: return &ffi_type_sshort;
97
98 case void_P: return &ffi_type_void;
99
100 case struct_P: {
101 ffi_type *aggregate(new(pool) ffi_type());
102 aggregate->size = 0;
103 aggregate->alignment = 0;
104 aggregate->type = FFI_TYPE_STRUCT;
105
106 aggregate->elements = new(pool) ffi_type *[type->data.signature.count + 1];
107 sig_ffi_types(pool, &ObjectiveC, &type->data.signature, aggregate->elements);
108 aggregate->elements[type->data.signature.count] = NULL;
109
110 return aggregate;
111 } break;
112
113 default:
114 _assert(false);
115 break;
116 }
117 }
118
119 ffi_type *Java(CYPool &pool, struct Type *type) {
120 switch (type->primitive) {
121 case typename_P: return &ffi_type_pointer;
122 case union_P: _assert(false); break;
123 case string_P: return &ffi_type_pointer;
124 case selector_P: return &ffi_type_pointer;
125 case block_P: return &ffi_type_pointer;
126 case object_P: return &ffi_type_pointer;
127 case boolean_P: return &ffi_type_uchar;
128 case uchar_P: return &ffi_type_uchar;
129 case uint_P: return &ffi_type_uint;
130 case ulong_P: return &ffi_type_ulong;
131 case ulonglong_P: return &ffi_type_ulonglong;
132 case ushort_P: return &ffi_type_ushort;
133 case array_P: return &ffi_type_pointer;
134 case pointer_P: return &ffi_type_pointer;
135 case bit_P: _assert(false); break;
136 case char_P: return &ffi_type_schar;
137 case double_P: return &ffi_type_double;
138 case float_P: return &ffi_type_double;
139 case int_P: return &ffi_type_sint;
140 case long_P: return &ffi_type_slong;
141 case longlong_P: return &ffi_type_slonglong;
142 case short_P: return &ffi_type_sshort;
143 case void_P: return &ffi_type_void;
144 case struct_P: return &ffi_type_pointer;
145
146 default:
147 _assert(false);
148 break;
149 }
150 }
151
152 void sig_ffi_cif(
153 CYPool &pool,
154 ffi_type *(*sig_ffi_type)(CYPool &, struct Type *),
155 struct Signature *signature,
156 ffi_cif *cif,
157 size_t skip,
158 ffi_type **types,
159 size_t offset
160 ) {
161 if (types == NULL)
162 types = new(pool) ffi_type *[signature->count - 1];
163 ffi_type *type = (*sig_ffi_type)(pool, signature->elements[0].type);
164 sig_ffi_types(pool, sig_ffi_type, signature, types, 1 + skip, offset);
165 ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, signature->count - 1 - skip + offset, type, types);
166 _assert(status == FFI_OK);
167 }
168
169 }