Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 298 lines (195 sloc) 6.083 kB
c64e58f pmc docs update 3
Michael Scott authored
1 /*
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
2 Copyright (C) 2001-2008, The Perl Foundation.
c64e58f pmc docs update 3
Michael Scott authored
3 $Id$
4
5 =head1 NAME
6
f474cd7 @bschmalhofer [perl #38235] Rename (dyn)?classes to (dyn)?pmc
bschmalhofer authored
7 src/pmc/nci.pmc - Native Call Interface
c64e58f pmc docs update 3
Michael Scott authored
8
9 =head1 DESCRIPTION
10
11 The vtable functions for the native C call functions.
12
13 =head2 Methods
14
15 =over 4
16
17 =cut
18
19 */
2a2c19b Added in code to properly do native calls to library routines loaded via
Dan Sugalski authored
20
21 #include "parrot/parrot.h"
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
22
0c0850c use need_ext flag
Leopold Toetsch authored
23 pmclass NCI need_ext {
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
24 ATTR STRING *signature; /* The signature. */
25 ATTR void *func; /* Function pointer to what we'll call. */
26 ATTR INTVAL arity; /* Cached arity of the NCI. */
2a2c19b Added in code to properly do native calls to library routines loaded via
Dan Sugalski authored
27
c64e58f pmc docs update 3
Michael Scott authored
28 /*
29
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
30 =item C<METHOD get_multisig()>
1415cda add introspection method get_multisig
Leopold Toetsch authored
31
32 Return the MMD signature PMC, if any or a Null PMC.
33
34 =cut
35
36 */
37
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
38 METHOD get_multisig() {
39 PMC *sig = PMC_pmc_val(SELF) ? PMC_pmc_val(SELF) : PMCNULL;
40 RETURN(PMC *sig);
1415cda add introspection method get_multisig
Leopold Toetsch authored
41 }
42
43 /*
44
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
45 =item C<METHOD set_raw_nci_ptr(void *func)>
d1c84dd @tewk [IMCC] added full calling conventions to C PMC methods.
tewk authored
46
47 Sets the specified function pointer and raw flag.
48
49 =cut
50
51 */
52
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
53 METHOD make_raw_nci(PMC *func) {
54 VTABLE_set_pointer(interp, SELF, (void *)func);
d1c84dd @tewk [IMCC] added full calling conventions to C PMC methods.
tewk authored
55 }
56
57 /*
58
c64e58f pmc docs update 3
Michael Scott authored
59 =item C<void init()>
60
61 Initializes the NCI with a C<NULL> function pointer.
62
63 =cut
64
65 */
66
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
67 VTABLE void init() {
c64e58f pmc docs update 3
Michael Scott authored
68 PMC_struct_val(SELF) = NULL;
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
69 PMC_pmc_val(SELF) = PMCNULL;
b080c3b @chromatic [PDD 07] Fix C parenthesis spacing and a compilation warning.
chromatic authored
70 PMC_data(SELF) = mem_allocate_zeroed_typed(Parrot_NCI);
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
71
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
72 /* Mark that we're not a raw NCI. */
d1c84dd @tewk [IMCC] added full calling conventions to C PMC methods.
tewk authored
73 PObj_flag_CLEAR(private2, SELF);
0176661 @chromatic [PMC] Actually clean up memory allocated in the NCI PMC. This fixes …
chromatic authored
74 PObj_active_destroy_SET(SELF);
f7fc86b nci
Leopold Toetsch authored
75 }
76
c64e58f pmc docs update 3
Michael Scott authored
77 /*
78
380c0ba fix NCI related DOD problem
Leopold Toetsch authored
79 =item C<void set_pointer_keyed_str(STRING *key, void *func)>
c64e58f pmc docs update 3
Michael Scott authored
80
380c0ba fix NCI related DOD problem
Leopold Toetsch authored
81 Sets the specified function pointer and signature (C<*key>).
c64e58f pmc docs update 3
Michael Scott authored
82
83 =cut
84
85 */
86
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
87 VTABLE void set_pointer(void *ptr) {
88 PMC_struct_val(SELF) = ptr;
89 PObj_flag_SET(private2, SELF);
90 }
91
92 VTABLE void set_pointer_keyed_str(STRING *key, void *func) {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
93 Parrot_NCI * const nci_info = PARROT_NCI(SELF);
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
94
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
95 /* Store the original function and signature. */
380c0ba fix NCI related DOD problem
Leopold Toetsch authored
96 PMC_struct_val(SELF) = func;
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
97 nci_info->signature = key;
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
98
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
99 /* Arity is length of that string minus one (the return type). */
100 nci_info->arity = string_length(INTERP, key) - 1;
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
101
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
102 /* Build call function. */
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
103 nci_info->func = (PMC *)(build_call_func(INTERP, SELF, key));
65bd9bf eval #1
Leopold Toetsch authored
104 }
11b723d fixed memory leak in jitted i386 nci code; clone nci
Leopold Toetsch authored
105
c64e58f pmc docs update 3
Michael Scott authored
106 /*
107
108 =item C<void destroy()>
109
110 Destroys the NCI, freeing any allocated memory.
111
112 =cut
113
114 */
115
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
116 VTABLE void destroy() {
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
117 if (PMC_data(SELF)) {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
118 Parrot_NCI * const nci_info = PARROT_NCI(SELF);
0176661 @chromatic [PMC] Actually clean up memory allocated in the NCI PMC. This fixes …
chromatic authored
119
120 #if defined(CAN_BUILD_CALL_FRAMES)
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
121 if (nci_info->func)
122 mem_free_executable(nci_info->func);
0176661 @chromatic [PMC] Actually clean up memory allocated in the NCI PMC. This fixes …
chromatic authored
123 #endif
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
124 mem_sys_free(nci_info);
0176661 @chromatic [PMC] Actually clean up memory allocated in the NCI PMC. This fixes …
chromatic authored
125 PMC_data(SELF) = NULL;
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
126 }
11b723d fixed memory leak in jitted i386 nci code; clone nci
Leopold Toetsch authored
127 }
128
c64e58f pmc docs update 3
Michael Scott authored
129 /*
130
131 =item C<PMC *clone()>
132
133 Creates and returns a clone of the NCI.
134
135 =cut
136
137 */
138
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
139 VTABLE PMC *clone() {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
140 Parrot_NCI * const nci_info_self = PARROT_NCI(SELF);
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
141 Parrot_NCI *nci_info_ret;
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
142
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
143 PMC * const ret = pmc_new_noinit(INTERP, SELF->vtable->base_type);
8d6d5d3 #25266
Leopold Toetsch authored
144 PMC_struct_val(ret) = PMC_struct_val(SELF);
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
145 PMC_pmc_val(ret) = PMCNULL;
b080c3b @chromatic [PDD 07] Fix C parenthesis spacing and a compilation warning.
chromatic authored
146 PMC_data(ret) = mem_allocate_zeroed_typed(Parrot_NCI);
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
147 nci_info_ret = PARROT_NCI(ret);
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
148
c64e58f pmc docs update 3
Michael Scott authored
149 /* FIXME if data is malloced (JIT/i386!) then we need
150 * the length of data here, to memcpy it
151 * ManagedStruct or Buffer?
152 */
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
153 nci_info_ret->func = nci_info_self->func;
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
154 nci_info_ret->signature = nci_info_self->signature;
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
155 nci_info_ret->arity = nci_info_self->arity;
156 PObj_get_FLAGS(ret) |= (PObj_get_FLAGS(SELF) & 0x7);
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
157
c64e58f pmc docs update 3
Michael Scott authored
158 return ret;
2a2c19b Added in code to properly do native calls to library routines loaded via
Dan Sugalski authored
159 }
160
c64e58f pmc docs update 3
Michael Scott authored
161 /*
162
163 =item C<INTVAL defined()>
164
165 Returns whether the NCI is defined.
166
167 =cut
168
169 */
170
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
171 VTABLE INTVAL defined() {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
172 Parrot_NCI * const nci_info = PARROT_NCI(SELF);
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
173 return nci_info->func != NULL;
2a2c19b Added in code to properly do native calls to library routines loaded via
Dan Sugalski authored
174 }
175
c64e58f pmc docs update 3
Michael Scott authored
176 /*
177
0ae5d55 @paultcochrane Patch courtesy of Steve Peters <steve at fisharerojo dot org>, his co…
paultcochrane authored
178 =item C<opcode_t *invoke(void *next)>
c64e58f pmc docs update 3
Michael Scott authored
179
8838c7f builtins 1 - first shot
Leopold Toetsch authored
180 Calls the associated C function, returning C<*next>. If
181 the invocant is a class, the PMC arguments are
182 shifted down.
c64e58f pmc docs update 3
Michael Scott authored
183
184 =cut
185
186 */
187
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
188 VTABLE opcode_t *invoke(void *next) {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
189 Parrot_NCI * const nci_info = PARROT_NCI(SELF);
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
190
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
191 typedef INTVAL (*nci_sub_t)(PARROT_INTERP, PMC *);
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
192
40cbe4a @particle #41325 [PATCH] Fill in missing pointer cast in
particle authored
193 nci_sub_t func = PObj_flag_TEST(private2, SELF)
6586a2c @jnthn Implement arity method on NCI PMCs, plus a test for it.
jnthn authored
194 ? (nci_sub_t) D2FPTR(PMC_struct_val(SELF))
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
195 : (nci_sub_t) D2FPTR(nci_info->func);
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
196
92c390d Fix tailcalls to NCI, #38724
Leopold Toetsch authored
197 PMC *cont;
b9b6c03 fix MMD wrapping for __is_equal
Leopold Toetsch authored
198
199 if (!func)
200 real_exception(INTERP, NULL, INVALID_OPERATION,
201 "attempt to call NULL function");
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
202
92c390d Fix tailcalls to NCI, #38724
Leopold Toetsch authored
203 func(INTERP, SELF);
204 cont = INTERP->current_cont;
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
205
8838c7f builtins 1 - first shot
Leopold Toetsch authored
206 /*
92c390d Fix tailcalls to NCI, #38724
Leopold Toetsch authored
207 * If the NCI function was tailcalled, the return result
208 * is already passed back to the caller of this frame
209 * - see Parrot_init_ret_nci(). We therefore invoke the
210 * return continuation here, which gets rid of this frame
211 * and returns the real return address
8838c7f builtins 1 - first shot
Leopold Toetsch authored
212 */
bf41b66 @particle [PDD07]: ws cleanup in src/, src/dynpmc/, and src/pmc/
particle authored
213 if (cont && cont != NEED_CONTINUATION &&
92c390d Fix tailcalls to NCI, #38724
Leopold Toetsch authored
214 (PObj_get_FLAGS(cont) & SUB_FLAG_TAILCALL)) {
3240c6e @chipdude Consistently name interpreter "interp" instead of "interpreter".
chipdude authored
215 cont = CONTEXT(interp->ctx)->current_cont;
92c390d Fix tailcalls to NCI, #38724
Leopold Toetsch authored
216 next = VTABLE_invoke(INTERP, cont, next);
217 }
82c7cf6 @chromatic [PMC] Minor cleanups and tidyings.
chromatic authored
218
e063e49 @chromatic Continuing Parrot/C++ cleanups (Steve Peters, RT #42746).
chromatic authored
219 return (opcode_t *)next;
2a2c19b Added in code to properly do native calls to library routines loaded via
Dan Sugalski authored
220 }
380c0ba fix NCI related DOD problem
Leopold Toetsch authored
221
c64e58f pmc docs update 3
Michael Scott authored
222 /*
223
224 =item C<INTVAL get_integer()>
225
226 Returns the function pointer as an integer.
227
228 =cut
229
230 */
231
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
232 VTABLE INTVAL get_integer() {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
233 Parrot_NCI * const nci_info = PARROT_NCI(SELF);
637246f @jnthn Refactor the NCI PMC to have an underlying structure, in preparation …
jnthn authored
234 return (INTVAL)nci_info->func;
3168931 Forth can now generate code for words on the fly and execute them.
Dan Sugalski authored
235 }
236
f35b03c @rubys Initial support for sort
rubys authored
237 /*
238
239 =item C<INTVAL get_bool()>
240
241 Returns the boolean value of the pointer.
242
243 =cut
244
245 */
246
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
247 VTABLE INTVAL get_bool() {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
248 Parrot_NCI * const nci_info = PARROT_NCI(SELF);
249 return (0 != (INTVAL)nci_info->func);
f35b03c @rubys Initial support for sort
rubys authored
250 }
251
6586a2c @jnthn Implement arity method on NCI PMCs, plus a test for it.
jnthn authored
252 /*
253
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
254 =item C<METHOD arity()>
6586a2c @jnthn Implement arity method on NCI PMCs, plus a test for it.
jnthn authored
255
256 Return the arity of the NCI (the number of arugments).
257
258 =cut
259
260 */
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
261 METHOD arity() {
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
262 Parrot_NCI * const nci_info = PARROT_NCI(SELF);
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
263
264 if (nci_info && nci_info->func) {
265 INTVAL arity = nci_info->arity;
266 RETURN(INTVAL arity);
267 }
62e3049 @petdance consting, and fixed embedded tabs
petdance authored
268 else
269 real_exception(INTERP, NULL, INVALID_OPERATION,
270 "You cannot get the arity of an undefined NCI.");
6586a2c @jnthn Implement arity method on NCI PMCs, plus a test for it.
jnthn authored
271
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
272 RETURN(INTVAL 0);
273 }
2a2c19b Added in code to properly do native calls to library routines loaded via
Dan Sugalski authored
274 }
c64e58f pmc docs update 3
Michael Scott authored
275
276 /*
277
278 =back
279
280 =head1 SEE ALSO
281
282 F<docs/pdds/pdd03_calling_conventions.pod>.
283
284 =head1 HISTORY
285
286 Initial revision by sean 2002/08/04.
287
288 =cut
289
290 */
291
0eb3eb3 added formatting footer and detabbed
Michael Scott authored
292 /*
293 * Local variables:
72575ae @bschmalhofer #40481: [PATCH] C-code coda in pmc files
bschmalhofer authored
294 * c-file-style: "parrot"
0eb3eb3 added formatting footer and detabbed
Michael Scott authored
295 * End:
296 * vim: expandtab shiftwidth=4:
72575ae @bschmalhofer #40481: [PATCH] C-code coda in pmc files
bschmalhofer authored
297 */
Something went wrong with that request. Please try again.