]> git.saurik.com Git - cycript.git/blob - sig/ffi_type.cpp
Pretend to support Blocks.
[cycript.git] / sig / ffi_type.cpp
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #include "Error.hpp"
41
42 #include "sig/ffi_type.hpp"
43 #include "sig/types.hpp"
44
45 #define ffi_type_slonglong ffi_type_sint64
46 #define ffi_type_ulonglong ffi_type_uint64
47
48 namespace sig {
49
50 void sig_ffi_types(
51 apr_pool_t *pool,
52 ffi_type *(*sig_ffi_type)(apr_pool_t *, struct Type *),
53 struct Signature *signature,
54 ffi_type **types,
55 size_t skip = 0,
56 size_t offset = 0
57 ) {
58 _assert(signature->count >= skip);
59 for (size_t index = skip; index != signature->count; ++index)
60 types[index - skip + offset] = (*sig_ffi_type)(pool, signature->elements[index].type);
61 }
62
63 ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type) {
64 switch (type->primitive) {
65 case typename_P: return &ffi_type_pointer;
66
67 case union_P:
68 /* XXX: we can totally make this work */
69 _assert(false);
70 break;
71
72 case string_P: return &ffi_type_pointer;
73 case selector_P: return &ffi_type_pointer;
74 case block_P: return &ffi_type_pointer;
75 case object_P: return &ffi_type_pointer;
76 case boolean_P: return &ffi_type_uchar;
77 case uchar_P: return &ffi_type_uchar;
78 case uint_P: return &ffi_type_uint;
79 case ulong_P: return &ffi_type_ulong;
80 case ulonglong_P: return &ffi_type_ulonglong;
81 case ushort_P: return &ffi_type_ushort;
82
83 case array_P: {
84 // XXX: this is really lame
85 ffi_type *aggregate(reinterpret_cast<ffi_type *>(apr_palloc(pool, sizeof(ffi_type))));
86 aggregate->size = 0;
87 aggregate->alignment = 0;
88 aggregate->type = FFI_TYPE_STRUCT;
89
90 ffi_type *element(ObjectiveC(pool, type->data.data.type));
91 size_t size(type->data.data.size);
92
93 aggregate->elements = reinterpret_cast<ffi_type **>(apr_palloc(pool, (size + 1) * sizeof(ffi_type *)));
94 for (size_t i(0); i != size; ++i)
95 aggregate->elements[i] = element;
96 aggregate->elements[size] = NULL;
97
98 return aggregate;
99 } break;
100
101 case pointer_P: return &ffi_type_pointer;
102
103 case bit_P:
104 /* XXX: we can totally make this work */
105 _assert(false);
106 break;
107
108 case char_P: return &ffi_type_schar;
109 case double_P: return &ffi_type_double;
110 case float_P: return &ffi_type_float;
111 case int_P: return &ffi_type_sint;
112 case long_P: return &ffi_type_slong;
113 case longlong_P: return &ffi_type_slonglong;
114 case short_P: return &ffi_type_sshort;
115
116 case void_P: return &ffi_type_void;
117
118 case struct_P: {
119 ffi_type *aggregate(reinterpret_cast<ffi_type *>(apr_palloc(pool, sizeof(ffi_type))));
120 aggregate->size = 0;
121 aggregate->alignment = 0;
122 aggregate->type = FFI_TYPE_STRUCT;
123
124 aggregate->elements = reinterpret_cast<ffi_type **>(apr_palloc(pool, (type->data.signature.count + 1) * sizeof(ffi_type *)));
125 sig_ffi_types(pool, &ObjectiveC, &type->data.signature, aggregate->elements);
126 aggregate->elements[type->data.signature.count] = NULL;
127
128 return aggregate;
129 } break;
130
131 default:
132 _assert(false);
133 break;
134 }
135 }
136
137 ffi_type *Java(apr_pool_t *pool, struct Type *type) {
138 switch (type->primitive) {
139 case typename_P: return &ffi_type_pointer;
140 case union_P: return &ffi_type_pointer;
141 case string_P: return &ffi_type_pointer;
142 case selector_P: return &ffi_type_pointer;
143 case block_P: return &ffi_type_pointer;
144 case object_P: return &ffi_type_pointer;
145 case boolean_P: return &ffi_type_uchar;
146 case uchar_P: return &ffi_type_uchar;
147 case uint_P: return &ffi_type_uint;
148 case ulong_P: return &ffi_type_ulong;
149 case ulonglong_P: return &ffi_type_ulonglong;
150 case ushort_P: return &ffi_type_ushort;
151 case array_P: return &ffi_type_pointer;
152 case pointer_P: return &ffi_type_pointer;
153
154 /* XXX: bit type */
155 case bit_P: return &ffi_type_uint;
156
157 case char_P: return &ffi_type_schar;
158 case double_P: return &ffi_type_double;
159 case float_P: return &ffi_type_double;
160 case int_P: return &ffi_type_sint;
161 case long_P: return &ffi_type_slong;
162 case longlong_P: return &ffi_type_slonglong;
163 case short_P: return &ffi_type_sshort;
164 case void_P: return &ffi_type_void;
165 case struct_P: return &ffi_type_pointer;
166
167 default:
168 _assert(false);
169 break;
170 }
171 }
172
173 void sig_ffi_cif(
174 apr_pool_t *pool,
175 ffi_type *(*sig_ffi_type)(apr_pool_t *, struct Type *),
176 struct Signature *signature,
177 ffi_cif *cif,
178 size_t skip,
179 ffi_type **types,
180 size_t offset
181 ) {
182 if (types == NULL)
183 types = reinterpret_cast<ffi_type **>(apr_palloc(pool, (signature->count - 1) * sizeof(ffi_type *)));
184 ffi_type *type = (*sig_ffi_type)(pool, signature->elements[0].type);
185 sig_ffi_types(pool, sig_ffi_type, signature, types, 1 + skip, offset);
186 ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, signature->count - 1 - skip + offset, type, types);
187 _assert(status == FFI_OK);
188 }
189
190 }