/
inter_misc.c
368 lines (290 loc) · 9.92 KB
/
inter_misc.c
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-2009, Parrot Foundation.
$Id$
=head1 NAME
src/interp/inter_misc.c - Parrot Interpreter miscellaneous functions
=head1 DESCRIPTION
NCI function setup, compiler registration, C<interpinfo>, and C<sysinfo> opcodes.
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "inter_misc.str"
#include "../compilers/imcc/imc.h"
#include "parrot/runcore_api.h"
#include "pmc/pmc_callcontext.h"
#include "parrot/has_header.h"
/* HEADERIZER HFILE: include/parrot/interpreter.h */
/*
=item C<void register_nci_method(PARROT_INTERP, const int type, void *func,
const char *name, const char *proto)>
Create an entry in the C<nci_method_table> for the given NCI method of PMC
class C<type>.
=cut
*/
PARROT_EXPORT
void
register_nci_method(PARROT_INTERP, const int type, ARGIN(void *func),
ARGIN(const char *name), ARGIN(const char *proto))
{
ASSERT_ARGS(register_nci_method)
PMC * const method = Parrot_pmc_new(interp, enum_class_NCI);
STRING * const method_name = string_make(interp, name, strlen(name),
NULL, PObj_constant_FLAG|PObj_external_FLAG);
/* create call func */
VTABLE_set_pointer_keyed_str(interp, method,
string_make(interp, proto, strlen(proto), NULL,
PObj_constant_FLAG|PObj_external_FLAG),
func);
/* insert it into namespace */
VTABLE_set_pmc_keyed_str(interp, interp->vtables[type]->_namespace,
method_name, method);
}
/*
=item C<void register_raw_nci_method_in_ns(PARROT_INTERP, const int type, void
*func, STRING *name)>
Create an entry in the C<nci_method_table> for the given raw NCI method
of PMC class C<type>.
=cut
*/
PARROT_EXPORT
void
register_raw_nci_method_in_ns(PARROT_INTERP, const int type, ARGIN(void *func),
ARGIN(STRING *name))
{
ASSERT_ARGS(register_raw_nci_method_in_ns)
PMC * const method = Parrot_pmc_new(interp, enum_class_NCI);
/* setup call func */
VTABLE_set_pointer(interp, method, func);
/* insert it into namespace */
VTABLE_set_pmc_keyed_str(interp, interp->vtables[type]->_namespace,
name, method);
}
/*
=item C<void Parrot_mark_method_writes(PARROT_INTERP, int type, const char
*name)>
Mark the method C<name> on PMC type C<type> as one that modifies the PMC.
=cut
*/
PARROT_EXPORT
void
Parrot_mark_method_writes(PARROT_INTERP, int type, ARGIN(const char *name))
{
ASSERT_ARGS(Parrot_mark_method_writes)
STRING *const str_name = Parrot_str_new_constant(interp, name);
PMC *const pmc_true = Parrot_pmc_new_init_int(interp, enum_class_Integer, 1);
PMC *const method = VTABLE_get_pmc_keyed_str(
interp, interp->vtables[type]->_namespace, str_name);
VTABLE_setprop(interp, method, CONST_STRING(interp, "write"), pmc_true);
}
/*
=item C<void Parrot_compreg(PARROT_INTERP, STRING *type, Parrot_compiler_func_t
func)>
Register a parser/compiler function.
=cut
*/
PARROT_EXPORT
void
Parrot_compreg(PARROT_INTERP, ARGIN(STRING *type), ARGIN(Parrot_compiler_func_t func))
{
ASSERT_ARGS(Parrot_compreg)
PMC * const iglobals = interp->iglobals;
PMC * const nci = Parrot_pmc_new(interp, enum_class_NCI);
STRING * const sc = CONST_STRING(interp, "PJt");
PMC * hash = VTABLE_get_pmc_keyed_int(interp, interp->iglobals,
IGLOBALS_COMPREG_HASH);
if (PMC_IS_NULL(hash)) {
hash = Parrot_pmc_new_noinit(interp, enum_class_Hash);
VTABLE_init(interp, hash);
VTABLE_set_pmc_keyed_int(interp, iglobals,
(INTVAL)IGLOBALS_COMPREG_HASH, hash);
}
VTABLE_set_pmc_keyed_str(interp, hash, type, nci);
/* build native call interface for the C sub in "func" */
VTABLE_set_pointer_keyed_str(interp, nci, sc, (void *)func);
}
/*
=item C<void * Parrot_compile_file(PARROT_INTERP, const char *fullname, STRING
**error)>
Compile code file.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
void *
Parrot_compile_file(PARROT_INTERP, ARGIN(const char *fullname), ARGOUT(STRING **error))
{
ASSERT_ARGS(Parrot_compile_file)
return imcc_compile_file(interp, fullname, error);
}
/*
=item C<INTVAL interpinfo(PARROT_INTERP, INTVAL what)>
C<what> specifies the type of information you want about the interpreter.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL
interpinfo(PARROT_INTERP, INTVAL what)
{
ASSERT_ARGS(interpinfo)
INTVAL ret;
switch (what) {
case TOTAL_MEM_ALLOC:
ret = Parrot_gc_total_memory_allocated(interp);
break;
case GC_MARK_RUNS:
ret = Parrot_gc_count_mark_runs(interp);
break;
case GC_LAZY_MARK_RUNS:
ret = Parrot_gc_count_lazy_mark_runs(interp);
break;
case GC_COLLECT_RUNS:
ret = Parrot_gc_count_collect_runs(interp);
break;
case ACTIVE_PMCS:
ret = Parrot_gc_active_pmcs(interp);
break;
case ACTIVE_BUFFERS:
ret = Parrot_gc_active_sized_buffers(interp);
break;
case TOTAL_PMCS:
ret = Parrot_gc_total_pmcs(interp);
break;
case TOTAL_BUFFERS:
ret = Parrot_gc_total_sized_buffers(interp);
break;
case HEADER_ALLOCS_SINCE_COLLECT:
ret = Parrot_gc_headers_alloc_since_last_collect(interp);
break;
case MEM_ALLOCS_SINCE_COLLECT:
ret = Parrot_gc_mem_alloc_since_last_collect(interp);
break;
case TOTAL_COPIED:
ret = Parrot_gc_total_copied(interp);
break;
case IMPATIENT_PMCS:
ret = Parrot_gc_impatient_pmcs(interp);
break;
case CURRENT_RUNCORE:
{
STRING *name = interp->run_core->name;
if (Parrot_str_equal(interp, name, CONST_STRING(interp, "slow")))
return PARROT_SLOW_CORE;
else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "fast")))
return PARROT_FAST_CORE;
else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "switch")))
return PARROT_EXEC_CORE;
else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "gc_debug")))
return PARROT_GC_DEBUG_CORE;
else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "debugger")))
return PARROT_DEBUGGER_CORE;
else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "profiling")))
return PARROT_PROFILING_CORE;
}
default: /* or a warning only? */
ret = -1;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"illegal argument in interpinfo");
}
return ret;
}
/*
=item C<PMC* interpinfo_p(PARROT_INTERP, INTVAL what)>
C<what> specifies the type of information you want about the
interpreter.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
PMC*
interpinfo_p(PARROT_INTERP, INTVAL what)
{
ASSERT_ARGS(interpinfo_p)
PMC *result;
switch (what) {
case CURRENT_SUB:
result = Parrot_pcc_get_sub(interp, CURRENT_CONTEXT(interp));
break;
case CURRENT_CONT:
result = Parrot_pcc_get_continuation(interp, CURRENT_CONTEXT(interp));
break;
case CURRENT_OBJECT:
result = Parrot_pcc_get_object(interp, CURRENT_CONTEXT(interp));
break;
case CURRENT_LEXPAD:
result = Parrot_pcc_get_lex_pad(interp, CURRENT_CONTEXT(interp));
break;
default: /* or a warning only? */
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"illegal argument in interpinfo");
}
/* Don't send NULL values to P registers */
return result ? result : PMCNULL;
}
/*
=item C<STRING* interpinfo_s(PARROT_INTERP, INTVAL what)>
Takes an interpreter name and an information type as arguments.
Returns corresponding information strings about the interpreter:
the full pathname, executable name, or the file stem,
(or throws an error exception, if the type is not recognised).
Valid types are EXECUTABLE_FULLNAME, EXECUTABLE_BASENAME,
and RUNTIME_PREFIX.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING*
interpinfo_s(PARROT_INTERP, INTVAL what)
{
ASSERT_ARGS(interpinfo_s)
switch (what) {
case EXECUTABLE_FULLNAME: {
PMC * const exe_name = VTABLE_get_pmc_keyed_int(interp, interp->iglobals,
IGLOBALS_EXECUTABLE);
if (PMC_IS_NULL(exe_name))
return CONST_STRING(interp, "");
return VTABLE_get_string(interp, exe_name);
}
case EXECUTABLE_BASENAME: {
PMC * const exe_name = VTABLE_get_pmc_keyed_int(interp,
interp->iglobals, IGLOBALS_EXECUTABLE);
if (PMC_IS_NULL(exe_name))
return CONST_STRING(interp, "");
else {
/* Need to strip back to what follows the final / or \. */
STRING * const fullname = VTABLE_get_string(interp, exe_name);
char * const fullname_c = Parrot_str_to_cstring(interp, fullname);
int pos = strlen(fullname_c) - 1;
STRING *basename;
while (pos > 0
&& fullname_c[pos] != '/'
&& fullname_c[pos] != '\\')
--pos;
if (pos > 0)
++pos;
basename = Parrot_str_new(interp, fullname_c + pos, 0);
Parrot_str_free_cstring(fullname_c);
return basename;
}
}
case RUNTIME_PREFIX:
return Parrot_get_runtime_path(interp);
case GC_SYS_NAME: {
STRING * name = Parrot_gc_sys_name(interp);
return name;
}
default:
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"illegal argument in interpinfo");
}
}
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/