-
Notifications
You must be signed in to change notification settings - Fork 138
/
nci.pmc
368 lines (253 loc) · 8.74 KB
/
nci.pmc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Copyright (C) 2001-2014, Parrot Foundation.
=head1 NAME
src/pmc/nci.pmc - NCI PMC
=head1 DESCRIPTION
The vtable functions for the Native Call Interface, to call
native C functions.
=head2 Methods
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_IGNORABLE_RESULT
static nci_thunk_t /*@alt void@*/
build_func(PARROT_INTERP,
ARGIN(PMC *obj),
ARGMOD(Parrot_NCI_attributes *nci))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(*nci);
#define ASSERT_ARGS_build_func __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(obj) \
, PARROT_ASSERT_ARG(nci))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=item C<static nci_thunk_t build_func(PARROT_INTERP, PMC *obj,
Parrot_NCI_attributes *nci)>
Actually build the NCI thunk.
=cut
*/
PARROT_IGNORABLE_RESULT
static nci_thunk_t
build_func(PARROT_INTERP, ARGIN(PMC *obj), ARGMOD(Parrot_NCI_attributes *nci))
{
ASSERT_ARGS(build_func)
Parrot_nci_sig_to_pcc(interp, nci->signature,
&nci->pcc_params_signature,
&nci->pcc_return_signature);
/* Arity is length of the signature minus one (the return type). */
nci->arity = VTABLE_elements(interp, nci->signature) - 1;
/* Build call function. */
nci->fb_info = build_call_func(interp, nci->signature);
nci->func = F2DPTR(VTABLE_get_pointer(interp, nci->fb_info));
PARROT_GC_WRITE_BARRIER(interp, obj);
return (nci_thunk_t)nci->func;
}
pmclass NCI auto_attrs provides invokable {
/* NCI thunk handling attributes */
ATTR PMC *signature; /* parsed signature */
ATTR void *func; /* function pointer to call */
ATTR PMC *fb_info; /* frame-builder info */
ATTR void *orig_func; /* pointer to wrapped function */
/* Parrot Sub-ish attributes */
ATTR STRING *pcc_params_signature;
ATTR STRING *pcc_return_signature;
ATTR INTVAL arity;
/* MMD fields */
ATTR STRING *long_signature;
ATTR PMC *multi_sig;
/*
=item C<METHOD get_multisig()>
Return the MMD signature PMC, if any or C<PMCNULL>.
=cut
*/
METHOD get_multisig() :no_wb {
PMC *sig;
GET_ATTR_multi_sig(INTERP, SELF, sig);
if (PMC_IS_NULL(sig))
sig = PMCNULL;
RETURN(PMC *sig);
}
/*
=item C<void init()>
Initializes the NCI with a C<NULL> function pointer.
=cut
*/
VTABLE void init() {
PObj_custom_mark_SET(SELF);
}
VTABLE void *get_pointer() :no_wb {
UNUSED(INTERP)
return PARROT_NCI(SELF)->orig_func;
}
/*
=item C<void set_pmc_keyed(PMC *key, PMC *p)>
=item C<void set_pmc_keyed_str(STRING *key, PMC *p)>
Call the equivalent C<set_pointer*> function.
=cut
*/
VTABLE void set_pmc_keyed(PMC *key, PMC *p) :manual_wb {
STATICSELF.set_pointer_keyed(key, VTABLE_get_pointer(INTERP, p));
}
VTABLE void set_pmc_keyed_str(STRING *key, PMC *p) :manual_wb {
STATICSELF.set_pointer_keyed_str(key, VTABLE_get_pointer(INTERP, p));
}
/*
=item C<void set_pointer_keyed(PMC *key, void *func)>
Sets the specified function pointer and signature (C<*key>).
=item C<void set_pointer_keyed_str(STRING *key, void *func)>
Sets the specified function pointer and siganture as described in the string C<key>.
=cut
*/
VTABLE void set_pointer_keyed(PMC *key, void *func) {
/* Store the original function and signature. */
SET_ATTR_orig_func(INTERP, SELF, func);
SET_ATTR_signature(INTERP, SELF, key);
}
VTABLE void set_pointer_keyed_str(STRING *key, void *func) :manual_wb {
SELF.set_pointer_keyed(Parrot_nci_parse_signature(INTERP, key), func);
}
/*
=item C<void mark()>
Mark any referenced strings and PMCs.
=cut
*/
VTABLE void mark() :no_wb {
Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
if (nci_info) {
Parrot_gc_mark_PMC_alive(interp, nci_info->signature);
Parrot_gc_mark_PMC_alive(interp, nci_info->fb_info);
Parrot_gc_mark_PMC_alive(interp, nci_info->multi_sig);
Parrot_gc_mark_STRING_alive(interp, nci_info->long_signature);
Parrot_gc_mark_STRING_alive(interp, nci_info->pcc_params_signature);
Parrot_gc_mark_STRING_alive(interp, nci_info->pcc_return_signature);
}
}
/*
=item C<PMC *clone()>
Creates and returns a clone of the NCI.
=cut
*/
VTABLE PMC *clone() :no_wb {
Parrot_NCI_attributes * const nci_info_self = PARROT_NCI(SELF);
PMC * const ret = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
Parrot_NCI_attributes * const nci_info_ret = PARROT_NCI(ret);
/* FIXME if data is malloced (JIT/i386!) then we need
* the length of data here, to memcpy it. ManagedStruct or Buffer?
*/
nci_info_ret->func = nci_info_self->func;
nci_info_ret->fb_info = nci_info_self->fb_info;
nci_info_ret->orig_func = nci_info_self->orig_func;
nci_info_ret->signature = nci_info_self->signature;
nci_info_ret->pcc_params_signature = nci_info_self->pcc_params_signature;
nci_info_ret->pcc_return_signature = nci_info_self->pcc_params_signature;
nci_info_ret->arity = nci_info_self->arity;
PObj_get_FLAGS(ret) = PObj_get_FLAGS(SELF);
RETURN(PMC *ret);
}
/*
=item C<INTVAL defined()>
Returns whether the NCI is defined.
=cut
*/
VTABLE INTVAL defined() :no_wb {
const Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
UNUSED(INTERP)
return nci_info->orig_func != NULL;
}
/*
=item C<opcode_t *invoke(void *next)>
Calls the associated C function, returning C<*next>. If the invocant is a
class, the PMC arguments are shifted down.
=cut
*/
VTABLE opcode_t *invoke(void *next) :no_wb {
Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
nci_thunk_t func;
PMC *fb_info;
PMC *cont;
func = (nci_thunk_t)D2FPTR(nci_info->func);
if (!func) {
/* build the thunk only when necessary */
func = build_func(interp, SELF, nci_info);
if (!func)
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_UNEXPECTED_NULL,
"attempt to call NULL function");
}
GET_ATTR_fb_info(INTERP, SELF, fb_info);
cont = INTERP->current_cont;
func(INTERP, SELF, fb_info);
/*
* If the NCI function was tailcalled, the return result
* is already passed back to the caller of this frame
* - see Parrot_init_ret_nci(). We therefore invoke the
* return continuation here, which gets rid of this frame
* and returns the real return address
*/
if (!PMC_IS_NULL(cont)
&& (PObj_get_FLAGS(cont) & SUB_FLAG_TAILCALL)) {
cont = Parrot_pcc_get_continuation(interp, CURRENT_CONTEXT(interp));
next = VTABLE_invoke(INTERP, cont, next);
}
return (opcode_t *)next;
}
/*
=item C<INTVAL get_integer()>
Returns the function pointer as an integer.
=cut
*/
VTABLE INTVAL get_integer() :no_wb {
Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
if (!nci_info->func)
build_func(INTERP, SELF, nci_info);
return PTR2INTVAL(nci_info->func);
}
/*
=item C<INTVAL get_bool()>
Returns the boolean value of the pointer.
=cut
*/
VTABLE INTVAL get_bool() :no_wb {
const Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
UNUSED(INTERP)
return !!nci_info->orig_func;
}
/*
=item C<METHOD arity()>
Return the arity of the NCI (the number of arguments).
=cut
*/
METHOD arity() :no_wb {
Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
if (nci_info) {
if (!nci_info->func)
build_func(INTERP, SELF, nci_info);
if (nci_info->func) {
const INTVAL i = nci_info->arity;
RETURN(INTVAL i);
}
}
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_INVALID_OPERATION,
"You cannot get the arity of an undefined NCI");
}
}
/*
=back
=head1 SEE ALSO
F<docs/pdds/pdd03_calling_conventions.pod>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/