-
Notifications
You must be signed in to change notification settings - Fork 138
/
main.c
445 lines (298 loc) · 10.1 KB
/
main.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
Copyright (C) 2001-2010, Parrot Foundation.
=head1 NAME
src/runcore/main.c - main functions for Parrot runcores
=head1 DESCRIPTION
The runcore API handles running the operations.
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "parrot/runcore_api.h"
#include "parrot/runcore_profiling.h"
#include "parrot/runcore_subprof.h"
#include "parrot/oplib/core_ops.h"
#include "parrot/oplib/ops.h"
#include "main.str"
#include "parrot/dynext.h"
#include "pmc/pmc_parrotlibrary.h"
#include "pmc/pmc_callcontext.h"
/* HEADERIZER HFILE: include/parrot/runcore_api.h */
/* XXX Needs to get done at the same time as the other interpreter files */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
static oplib_init_f get_dynamic_op_lib_init(PARROT_INTERP,
ARGIN(const PMC *lib))
__attribute__nonnull__(2);
#define ASSERT_ARGS_get_dynamic_op_lib_init __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(lib))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=item C<void Parrot_runcore_init(PARROT_INTERP)>
Initializes the runcores.
=cut
*/
void
Parrot_runcore_init(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_runcore_init)
STRING * const default_core = CONST_STRING(interp, "fast");
interp->cores = NULL;
interp->num_cores = 0;
Parrot_runcore_slow_init(interp);
Parrot_runcore_fast_init(interp);
Parrot_runcore_subprof_init(interp);
Parrot_runcore_exec_init(interp);
Parrot_runcore_gc_debug_init(interp);
Parrot_runcore_debugger_init(interp);
Parrot_runcore_profiling_init(interp);
/* set the default runcore */
Parrot_runcore_switch(interp, default_core);
}
/*
=item C<INTVAL Parrot_runcore_register(PARROT_INTERP, Parrot_runcore_t
*coredata)>
Registers a new runcore with Parrot. Returns 1 on success, 0 on failure.
=cut
*/
PARROT_EXPORT
INTVAL
Parrot_runcore_register(PARROT_INTERP, ARGIN(Parrot_runcore_t *coredata))
{
ASSERT_ARGS(Parrot_runcore_register)
size_t i = interp->num_cores++;
interp->cores = mem_gc_realloc_n_typed_zeroed(interp, interp->cores,
interp->num_cores, i, Parrot_runcore_t *);
interp->cores[i] = coredata;
return 1;
}
/*
=item C<void Parrot_runcore_switch(PARROT_INTERP, STRING *name)>
Switches to a named runcore. Throws an exception on an unknown runcore.
=cut
*/
PARROT_EXPORT
void
Parrot_runcore_switch(PARROT_INTERP, ARGIN(STRING *name))
{
ASSERT_ARGS(Parrot_runcore_switch)
size_t num_cores = interp->num_cores;
size_t i;
if (interp->run_core
&& STRING_equal(interp, name, interp->run_core->name))
return;
for (i = 0; i < num_cores; ++i) {
if (STRING_equal(interp, name, interp->cores[i]->name)) {
interp->run_core = interp->cores[i];
return;
}
}
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"Invalid runcore %Ss requested\n", name);
}
/*
=item C<static oplib_init_f get_dynamic_op_lib_init(PARROT_INTERP, const PMC
*lib)>
Returns an dynamic oplib's opcode's library C<op_lib> init function.
C<lib> will be a C<ParrotLibrary> PMC.
=cut
*/
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
static oplib_init_f
get_dynamic_op_lib_init(SHIM_INTERP, ARGIN(const PMC *lib))
{
ASSERT_ARGS(get_dynamic_op_lib_init)
return (oplib_init_f)D2FPTR(
((Parrot_ParrotLibrary_attributes *)PMC_data(lib))->oplib_init);
}
/*
=item C<void prepare_for_run(PARROT_INTERP)>
Prepares to run the interpreter's run core.
=cut
*/
void
prepare_for_run(PARROT_INTERP)
{
ASSERT_ARGS(prepare_for_run)
const runcore_prepare_fn_type prepare_run = interp->run_core->prepare_run;
if (prepare_run)
(*prepare_run)(interp, interp->run_core);
}
/*
=item C<void runops_int(PARROT_INTERP, size_t offset)>
Run Parrot operations of loaded code segment until an end opcode is
reached. Run core is selected depending on the C<Interp_flags>. When a
C<restart> opcode is encountered, a different core may be selected and
evaluation of opcode continues.
=cut
*/
void
runops_int(PARROT_INTERP, size_t offset)
{
ASSERT_ARGS(runops_int)
interp->resume_offset = offset;
interp->resume_flag |= RESUME_RESTART;
while (interp->resume_flag & RESUME_RESTART) {
opcode_t * const pc = (opcode_t *)
interp->code->base.data + interp->resume_offset;
const runcore_runops_fn_type core = interp->run_core->runops;
interp->resume_offset = 0;
interp->resume_flag &= ~(RESUME_RESTART | RESUME_INITIAL);
(*core)(interp, interp->run_core, pc);
/* if we have fallen out with resume and we were running CGOTO, set
* the stacktop again to a sane value, so that restarting the runloop
* is ok. */
if (interp->resume_flag & RESUME_RESTART) {
if ((int)interp->resume_offset < 0)
Parrot_ex_throw_from_c_args(interp, NULL, 1,
"branch_cs: illegal resume offset");
}
}
}
/*
=item C<void Parrot_runcore_destroy(PARROT_INTERP)>
Shuts down the runcores and deallocates any dynops memory.
=cut
*/
void
Parrot_runcore_destroy(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_runcore_destroy)
size_t num_cores = interp->num_cores;
size_t i;
for (i = 0; i < num_cores; ++i) {
Parrot_runcore_t * const core = interp->cores[i];
const runcore_destroy_fn_type destroy = core->destroy;
if (destroy)
(*destroy)(interp, core);
mem_gc_free(interp, core);
}
if (interp->cores)
mem_gc_free(interp, interp->cores);
interp->cores = NULL;
interp->run_core = NULL;
if (interp->all_op_libs)
mem_gc_free(interp, interp->all_op_libs);
interp->all_op_libs = NULL;
}
/*
=back
=head2 Dynamic Loading Functions
=over 4
=item C<void dynop_register(PARROT_INTERP, PMC *lib_pmc)>
Register a dynamic oplib.
=cut
*/
PARROT_EXPORT
void
dynop_register(PARROT_INTERP, ARGIN(PMC *lib_pmc))
{
ASSERT_ARGS(dynop_register)
op_lib_t *lib;
oplib_init_f init_func;
if (!interp->all_op_libs)
interp->all_op_libs = mem_gc_allocate_n_zeroed_typed(interp,
interp->n_libs + 1, op_lib_t*);
else
interp->all_op_libs = mem_gc_realloc_n_typed_zeroed(interp, interp->all_op_libs,
interp->n_libs + 1, interp->n_libs, op_lib_t *);
init_func = get_dynamic_op_lib_init(interp, lib_pmc);
lib = init_func(interp, 1);
interp->all_op_libs[interp->n_libs++] = lib;
/* if we are registering an op_lib variant, called from below the base
* names of this lib and the previous one are the same */
if (interp->n_libs >= 2
&& (STREQ(interp->all_op_libs[interp->n_libs-2]->name, lib->name)))
return;
parrot_hash_oplib(interp, lib);
}
/*
=item C<void parrot_hash_oplib(PARROT_INTERP, op_lib_t *lib)>
Add the ops in C<lib> to the global name => op_info hash.
=cut
*/
void
parrot_hash_oplib(PARROT_INTERP, ARGIN(op_lib_t *lib))
{
ASSERT_ARGS(parrot_hash_oplib)
int i;
DECL_CONST_CAST;
for (i = 0; i < lib->op_count; i++) {
op_info_t *op = &lib->op_info_table[i];
Parrot_hash_put(interp, interp->op_hash, PARROT_const_cast(char *, op->full_name),
(void *)op);
if (!Parrot_hash_exists(interp, interp->op_hash, PARROT_const_cast(char *, op->name)))
Parrot_hash_put(interp, interp->op_hash, PARROT_const_cast(char *, op->name),
(void *)op);
}
}
/*
=item C<void Parrot_runcore_disable_event_checking(PARROT_INTERP)>
Restore old function table.
XXX This is only implemented for the function core at present.
=cut
*/
PARROT_EXPORT
void
Parrot_runcore_disable_event_checking(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_runcore_disable_event_checking)
PackFile_ByteCode *cs = interp->code;
/* restore func table */
PARROT_ASSERT(cs->save_func_table);
cs->op_func_table = cs->save_func_table;
cs->save_func_table = NULL;
}
/*
=item C<void Parrot_runcore_enable_event_checking(PARROT_INTERP)>
Replace func table with one that does event checking for all opcodes.
NOTE: C<enable_event_checking()> is called async by the event handler
thread. All action done from here has to be async safe.
XXX This is only implemented for the function core at present.
=cut
*/
PARROT_EXPORT
void
Parrot_runcore_enable_event_checking(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_runcore_enable_event_checking)
PackFile_ByteCode *cs = interp->code;
/* only save if we're not already event checking */
if (cs->save_func_table == NULL)
cs->save_func_table = cs->op_func_table;
/* ensure event checking table is big enough */
if (interp->evc_func_table_size < cs->op_count) {
size_t i;
op_lib_t *core_lib = get_core_op_lib_init(interp, interp->run_core)(interp, 1);
interp->evc_func_table = interp->evc_func_table ?
mem_gc_realloc_n_typed_zeroed(interp,
interp->evc_func_table, cs->op_count,
interp->evc_func_table_size, op_func_t) :
mem_gc_allocate_n_zeroed_typed(interp,
cs->op_count, op_func_t);
for (i = interp->evc_func_table_size; i < cs->op_count; i++)
interp->evc_func_table[i] =
core_lib->op_func_table[PARROT_OP_check_events__];
interp->evc_func_table_size = cs->op_count;
}
/* put evc table in place */
cs->op_func_table = interp->evc_func_table;
}
/*
=back
=head1 SEE ALSO
F<include/parrot/interpreter.h>, F<src/interp/inter_cb.c>,
F<src/interp/inter_create.c>, F<src/interp/inter_misc.c>, F<src/call/ops.c>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/