]> git.saurik.com Git - cycript.git/blob - sig/ffi_type.cpp
Replace sig::Primitive with full object hierarchy.
[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 const struct Signature *signature,
35 ffi_type **types,
36 size_t skip = 0,
37 size_t offset = 0
38 ) {
39 _assert(signature->count >= skip);
40 for (size_t index = skip; index != signature->count; ++index)
41 types[index - skip + offset] = signature->elements[index].type->GetFFI(pool);
42 }
43
44 template <>
45 ffi_type *Primitive<bool>::GetFFI(CYPool &pool) const {
46 return &ffi_type_uchar;
47 }
48
49 template <>
50 ffi_type *Primitive<char>::GetFFI(CYPool &pool) const {
51 return &ffi_type_schar;
52 }
53
54 template <>
55 ffi_type *Primitive<float>::GetFFI(CYPool &pool) const {
56 return &ffi_type_float;
57 }
58
59 template <>
60 ffi_type *Primitive<double>::GetFFI(CYPool &pool) const {
61 return &ffi_type_double;
62 }
63
64 template <>
65 ffi_type *Primitive<signed char>::GetFFI(CYPool &pool) const {
66 return &ffi_type_schar;
67 }
68
69 template <>
70 ffi_type *Primitive<signed int>::GetFFI(CYPool &pool) const {
71 return &ffi_type_sint;
72 }
73
74 template <>
75 ffi_type *Primitive<signed long int>::GetFFI(CYPool &pool) const {
76 return &ffi_type_slong;
77 }
78
79 template <>
80 ffi_type *Primitive<signed long long int>::GetFFI(CYPool &pool) const {
81 return &ffi_type_slonglong;
82 }
83
84 template <>
85 ffi_type *Primitive<signed short int>::GetFFI(CYPool &pool) const {
86 return &ffi_type_sshort;
87 }
88
89 template <>
90 ffi_type *Primitive<unsigned char>::GetFFI(CYPool &pool) const {
91 return &ffi_type_uchar;
92 }
93
94 template <>
95 ffi_type *Primitive<unsigned int>::GetFFI(CYPool &pool) const {
96 return &ffi_type_uint;
97 }
98
99 template <>
100 ffi_type *Primitive<unsigned long int>::GetFFI(CYPool &pool) const {
101 return &ffi_type_ulong;
102 }
103
104 template <>
105 ffi_type *Primitive<unsigned long long int>::GetFFI(CYPool &pool) const {
106 return &ffi_type_ulonglong;
107 }
108
109 template <>
110 ffi_type *Primitive<unsigned short int>::GetFFI(CYPool &pool) const {
111 return &ffi_type_ushort;
112 }
113
114 ffi_type *Void::GetFFI(CYPool &pool) const {
115 return &ffi_type_void;
116 }
117
118 ffi_type *Unknown::GetFFI(CYPool &pool) const {
119 _assert(false);
120 }
121
122 ffi_type *String::GetFFI(CYPool &pool) const {
123 return &ffi_type_pointer;
124 }
125
126 ffi_type *Meta::GetFFI(CYPool &pool) const {
127 return &ffi_type_pointer;
128 }
129
130 ffi_type *Selector::GetFFI(CYPool &pool) const {
131 return &ffi_type_pointer;
132 }
133
134 ffi_type *Bits::GetFFI(CYPool &pool) const {
135 /* XXX: we can totally make this work */
136 _assert(false);
137 }
138
139 ffi_type *Pointer::GetFFI(CYPool &pool) const {
140 return &ffi_type_pointer;
141 }
142
143 ffi_type *Array::GetFFI(CYPool &pool) const {
144 // XXX: this is really lame
145 ffi_type *ffi(new(pool) ffi_type());
146 ffi->size = 0;
147 ffi->alignment = 0;
148 ffi->type = FFI_TYPE_STRUCT;
149
150 ffi_type *element(type.GetFFI(pool));
151
152 ffi->elements = new(pool) ffi_type *[size + 1];
153 for (size_t i(0); i != size; ++i)
154 ffi->elements[i] = element;
155 ffi->elements[size] = NULL;
156
157 return ffi;
158 }
159
160 ffi_type *Object::GetFFI(CYPool &pool) const {
161 return &ffi_type_pointer;
162 }
163
164 ffi_type *Aggregate::GetFFI(CYPool &pool) const {
165 // XXX: we can totally make overlap work
166 _assert(!overlap);
167
168 ffi_type *ffi(new(pool) ffi_type());
169 ffi->size = 0;
170 ffi->alignment = 0;
171 ffi->type = FFI_TYPE_STRUCT;
172
173 ffi->elements = new(pool) ffi_type *[signature.count + 1];
174 sig_ffi_types(pool, &signature, ffi->elements);
175 ffi->elements[signature.count] = NULL;
176
177 return ffi;
178 }
179
180 ffi_type *Function::GetFFI(CYPool &pool) const {
181 _assert(false);
182 }
183
184 ffi_type *Block::GetFFI(CYPool &pool) const {
185 return &ffi_type_pointer;
186 }
187
188 void sig_ffi_cif(
189 CYPool &pool,
190 struct Signature *signature,
191 ffi_cif *cif,
192 size_t skip,
193 ffi_type **types,
194 size_t offset
195 ) {
196 if (types == NULL)
197 types = new(pool) ffi_type *[signature->count - 1];
198 ffi_type *type = signature->elements[0].type->GetFFI(pool);
199 sig_ffi_types(pool, signature, types, 1 + skip, offset);
200 ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, signature->count - 1 - skip + offset, type, types);
201 _assert(status == FFI_OK);
202 }
203
204 }