Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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