-
Notifications
You must be signed in to change notification settings - Fork 138
/
embed.c
947 lines (733 loc) · 22.8 KB
/
embed.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/*
Copyright: 2001-2006 The Perl Foundation. All Rights Reserved.
$Id$
=head1 NAME
src/embed.c - The Parrot embedding interface
=head1 DESCRIPTION
This file implements the Parrot embedding interface.
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "parrot/embed.h"
#include "parrot/oplib/ops.h"
/*
=item C<Parrot_Interp Parrot_new(Parrot_Interp parent)>
Returns a new Parrot interpreter.
The first created interpreter (C<parent> is C<NULL>) is the last one
to get destroyed.
=cut
*/
#ifdef JIT_CAPABLE
#if EXEC_CAPABLE
# include "parrot/exec.h"
#endif /* EXEC_CAPABLE */
#include "parrot/jit.h"
#endif
Parrot_Interp
Parrot_new(Parrot_Interp parent)
{
/* interpreter.c:make_interpreter builds a new Parrot_Interp. */
return make_interpreter(parent, PARROT_NO_FLAGS);
}
extern void Parrot_initialize_core_pmcs(Interp *interp);
/*
=item C<void Parrot_init(Interp *interpreter)>
Initializes the new interpreter. This function only has effect the first
time it is called. Therefore Parrot_init() doesn't have to be called
on an interpreter returned from Parrot_new().
Use this function when you intend to enter the run loop,
which automatically sets the top of stack pointer.
=item C<void Parrot_init_stacktop(Interp *interpreter, void *stack_top)>
Like above. Additionally sets the stack top, so that Parrot objects created
in inner stack frames will be visible during DODs stack walking code.
B<stack_top> should be the address of an automatic variable in the caller's
stack frame. All unanchored Parrot objects (PMCs) must live in inner stack
frames so that they are not destroyed during DOD runs.
Use this function when you call into Parrot before entering a run loop.
=cut
*/
void
Parrot_init(Interp *interpreter)
{
if (!interpreter->world_inited) {
/* global_setup.c:init_world sets up some vtable stuff.
* It must only be called once.
*/
interpreter->world_inited = 1;
init_world(interpreter);
}
}
void
Parrot_init_stacktop(Interp *interpreter, void *stack_top)
{
interpreter->lo_var_ptr = stack_top;
Parrot_init(interpreter);
}
/*
=item C<void Parrot_set_flag(Interp *interpreter, Parrot_Interp_flag flag)>
Sets a flag in the interpreter specified by C<flag>, any of
C<PARROT_BOUNDS_FLAG>, or C<PARROT_PROFILE_FLAG> to enable profiling, and
bounds checking respectively or C<PARROT_THR_TYPE_1>, C<PARROT_THR_TYPE_2>, or
C<PARROT_THR_TYPE_3> to disable thread communication and variable sharing,
disable variable sharing but enable thread communication, or to enable variable
sharing.
=item C<void Parrot_set_debug(Interp *interpreter, UINTVAL flag)>
Set a debug flag: C<PARROT_DEBUG_FLAG>.
=item C<void Parrot_set_trace(Interp *interpreter, UINTVAL flag)>
Set a trace flag: C<PARROT_TRACE_FLAG>
=cut
*/
void
Parrot_set_flag(Interp *interpreter, Parrot_Interp_flag flag)
{
/* These two macros (from interpreter.h) do exactly what they look like. */
Interp_flags_SET(interpreter, flag);
switch (flag) {
case PARROT_BOUNDS_FLAG:
case PARROT_PROFILE_FLAG:
Interp_core_SET(interpreter, PARROT_SLOW_CORE);
break;
default:
break;
}
}
void
Parrot_set_debug(Interp *interpreter, UINTVAL flag)
{
interpreter->debug_flags |= flag;
}
void
Parrot_set_trace(Interp *interpreter, UINTVAL flag)
{
CONTEXT(interpreter->ctx)->trace_flags |= flag;
Interp_core_SET(interpreter, PARROT_SLOW_CORE);
}
/*
=item C<void Parrot_clear_flag(Interp *, Parrot_Interp_flag flag)>
=item C<void Parrot_clear_debug(Interp *, UINTVAL flag)>
=item C<void Parrot_clear_trace(Interp *, UINTVAL flag)>
Clears a flag in the interpreter.
=cut
*/
void
Parrot_clear_flag(Parrot_Interp interpreter, Parrot_Interp_flag flag)
{
Interp_flags_CLEAR(interpreter, flag);
}
void
Parrot_clear_debug(Interp *interpreter, UINTVAL flag)
{
interpreter->debug_flags &= ~flag;
}
void
Parrot_clear_trace(Interp *interpreter, UINTVAL flag)
{
CONTEXT(interpreter->ctx)->trace_flags &= ~flag;
}
/*
=item C<Parrot_Int
Parrot_test_flag(Interp*, Parrot_Interp_flag flag)>
=item C<UINTVAL
Parrot_test_debug(Interp*, UINTVAL flag)>
=item C<UINTVAL
Parrot_test_trace(Interp*, UINTVAL flag)>
Test the interpreter flags specified in C<flag>.
=cut
*/
Parrot_Int
Parrot_test_flag(Interp* interpreter, Parrot_Interp_flag flag)
{
return Interp_flags_TEST(interpreter, flag);
}
UINTVAL
Parrot_test_debug(Interp *interpreter, UINTVAL flag)
{
return interpreter->debug_flags & flag;
}
UINTVAL
Parrot_test_trace(Interp *interpreter, UINTVAL flag)
{
return CONTEXT(interpreter->ctx)->trace_flags & flag;
}
/*
=item C<void
Parrot_set_run_core(Interp *interpreter, Parrot_Run_core_t core)>
Sets the specified run core.
=cut
*/
void
Parrot_set_run_core(Interp *interpreter, Parrot_Run_core_t core)
{
Interp_core_SET(interpreter, core);
}
/*
=item C<void
Parrot_setwarnings(Interp *interpreter, Parrot_warnclass wc)>
Activates the given warnings.
=cut
*/
void
Parrot_setwarnings(Interp *interpreter, Parrot_warnclass wc)
{
/* Activates the given warnings. (Macro from warnings.h.) */
PARROT_WARNINGS_on(interpreter, wc);
}
/*
=item C<struct PackFile *
Parrot_readbc(Interp *interpreter, const char *filename)>
Read in a bytecode, unpack it into a C<PackFile> structure, and do fixups.
=cut
*/
struct PackFile *
Parrot_readbc(Interp *interpreter, const char *fullname)
{
INTVAL program_size, wanted;
char *program_code;
struct PackFile *pf;
FILE * io = NULL;
INTVAL is_mapped = 0;
#ifdef PARROT_HAS_HEADER_SYSMMAN
int fd = -1;
#endif
if (fullname == NULL || strcmp(fullname, "-") == 0) {
/* read from STDIN */
io = stdin;
/* read 1k at a time */
program_size = 0;
}
else {
STRING *fs = string_make(interpreter, fullname,
strlen(fullname), NULL, 0);
if (!Parrot_stat_info_intval(interpreter, fs, STAT_EXISTS)) {
PIO_eprintf(interpreter, "Parrot VM: Can't stat %s, code %i.\n",
fullname, errno);
return NULL;
}
/*
* TODO check for regular file
*/
program_size = Parrot_stat_info_intval(interpreter, fs, STAT_FILESIZE);
#ifndef PARROT_HAS_HEADER_SYSMMAN
io = fopen(fullname, "rb");
if (!io) {
PIO_eprintf(interpreter, "Parrot VM: Can't open %s, code %i.\n",
fullname, errno);
return NULL;
}
#endif /* PARROT_HAS_HEADER_SYSMMAN */
}
#ifdef PARROT_HAS_HEADER_SYSMMAN
again:
#endif
/* if we've opened a file (or stdin) with PIO, read it in */
if (io) {
size_t chunk_size;
char *cursor;
INTVAL read_result;
chunk_size = program_size > 0 ? program_size : 1024;
program_code = (char *)mem_sys_allocate(chunk_size);
wanted = program_size;
program_size = 0;
cursor = (char *)program_code;
while ((read_result =
fread(cursor, 1, chunk_size, io)) > 0) {
program_size += read_result;
if (program_size == wanted)
break;
chunk_size = 1024;
program_code =
mem_sys_realloc(program_code, program_size + chunk_size);
if (!program_code) {
PIO_eprintf(interpreter,
"Parrot VM: Could not reallocate buffer while reading packfile from PIO.\n");
return NULL;
}
cursor = (char *)program_code + program_size;
}
if (read_result < 0) {
PIO_eprintf(interpreter,
"Parrot VM: Problem reading packfile from PIO.\n");
return NULL;
}
fclose(io);
}
else {
/* if we've gotten here, we opted not to use PIO to read the file.
* use mmap */
#ifdef PARROT_HAS_HEADER_SYSMMAN
fd = open(fullname, O_RDONLY | O_BINARY);
if (!fd) {
PIO_eprintf(interpreter, "Parrot VM: Can't open %s, code %i.\n",
fullname, errno);
return NULL;
}
program_code =
mmap(0, program_size, PROT_READ, MAP_SHARED, fd, (off_t)0);
if (program_code == (void *)MAP_FAILED) {
Parrot_warn(interpreter, PARROT_WARNINGS_IO_FLAG,
"Parrot VM: Can't mmap file %s, code %i.\n",
fullname, errno);
/* try again, now with IO reading the file */
io = fopen(fullname, "rb");
if (!io) {
PIO_eprintf(interpreter,
"Parrot VM: Can't open %s, code %i.\n",
fullname, errno);
return NULL;
}
goto again;
}
is_mapped = 1;
#else /* PARROT_HAS_HEADER_SYSMMAN */
PIO_eprintf(interpreter, "Parrot VM: uncaught error occurred reading "
"file or mmap not available.\n");
return NULL;
#endif /* PARROT_HAS_HEADER_SYSMMAN */
}
/* Now that we have the bytecode, let's unpack it. */
pf = PackFile_new(interpreter, is_mapped);
if (!PackFile_unpack
(interpreter, pf, (opcode_t *)program_code, program_size)) {
PIO_eprintf(interpreter, "Parrot VM: Can't unpack packfile %s.\n",
fullname);
return NULL;
}
/*
* Set :main routine
*/
do_sub_pragmas(interpreter, pf->cur_cs, PBC_PBC, NULL);
/*
* JITting and/or prederefing the sub/the bytecode is done
* in switch_to_cs before actual usage of the segment
*/
#ifdef PARROT_HAS_HEADER_SYSMMAN
if (fd >= 0) {
close(fd); /* the man page states, it's ok to close a mmaped file */
}
#else
/* XXX Parrot_exec uses this
mem_sys_free(program_code); */
#endif
return pf;
}
/*
=item C<void
Parrot_loadbc(Interp *interpreter, struct PackFile *pf)>
Loads the C<PackFile> returned by C<Parrot_readbc()>.
=cut
*/
void
Parrot_loadbc(Interp *interpreter, struct PackFile *pf)
{
interpreter->initial_pf = pf;
interpreter->code = pf->cur_cs;
}
/*
=item C<static PMC*
setup_argv(Interp *interpreter, int argc, char ** argv)>
Creates and returns C<ARGS> array PMC.
=cut
*/
static PMC*
setup_argv(Interp *interpreter, int argc, char ** argv)
{
INTVAL i;
PMC *userargv;
if (Interp_debug_TEST(interpreter, PARROT_START_DEBUG_FLAG)) {
PIO_eprintf(interpreter,
"*** Parrot VM: Setting up ARGV array."
" Current argc: %d ***\n",
argc);
}
userargv = pmc_new_noinit(interpreter, enum_class_ResizableStringArray);
/* immediately anchor pmc to root set */
VTABLE_set_pmc_keyed_int(interpreter, interpreter->iglobals,
(INTVAL)IGLOBALS_ARGV_LIST, userargv);
VTABLE_init(interpreter, userargv);
for (i = 0; i < argc; i++) {
/* Run through argv, adding everything to @ARGS. */
STRING *arg = string_make(interpreter, argv[i], strlen(argv[i]),
NULL, PObj_external_FLAG);
if (Interp_debug_TEST(interpreter, PARROT_START_DEBUG_FLAG)) {
PIO_eprintf(interpreter, "\t%vd: %s\n", i, argv[i]);
}
VTABLE_push_string(interpreter, userargv, arg);
}
return userargv;
}
/*
=item C<static int
prof_sort_f(const void *a, const void *b)>
Sort function for profile data. Sorts by time.
=cut
*/
static int
prof_sort_f(const void *a, const void *b)
{
const ProfData *pa = (const ProfData *) a;
const ProfData *pb = (const ProfData *) b;
if (pa->time < pb->time)
return 1;
if (pa->time > pb->time)
return -1;
return 0;
}
/*
=item C<static const char *
op_name(Parrot_Interp interpreter, int k)>
Returns the name of the opcode.
=cut
*/
static const char *
op_name(Parrot_Interp interpreter, int k)
{
switch (k) {
case PARROT_PROF_DOD_p1:
return "DOD_mark_root";
case PARROT_PROF_DOD_p2:
return "DOD_mark_next";
case PARROT_PROF_DOD_cp:
return "DOD_collect_PMC";
case PARROT_PROF_DOD_cb:
return "DOD_collect_buffers";
case PARROT_PROF_GC:
return "GC";
case PARROT_PROF_EXCEPTION:
return "EXCEPTION";
default:
break;
}
return interpreter->op_info_table[k - PARROT_PROF_EXTRA].full_name;
}
/*
=item C<static FLOATVAL
calibrate(Parrot_Interp interpreter)>
With this calibration, reported times of C<parrot -p> almost match those
measured with time C<parrot -b>.
=cut
*/
static FLOATVAL
calibrate(Parrot_Interp interpreter)
{
size_t n = interpreter->op_count;
size_t i;
FLOATVAL start, empty;
opcode_t code[] = { 1 }; /* noop */
opcode_t *pc = code;
if (n < 1000000) /* minimum opcode count for calibration */
n = 1000000;
start = Parrot_floatval_time();
for (empty = 0.0, i = 0; i < n; i++)
pc = (interpreter->op_func_table[*code])(pc, interpreter);
empty += Parrot_floatval_time() - start;
return empty / (FLOATVAL)n;
}
/*
=item C<static void print_profile(int status, void *p)>
Prints out a profile listing.
=cut
*/
static void
print_profile(int status, void *p)
{
Parrot_Interp interpreter = (Parrot_Interp) p;
if (interpreter->profile != NULL) {
UINTVAL j;
int k;
int jit;
UINTVAL op_count = 0;
UINTVAL call_count = 0;
FLOATVAL sum_time = 0.0;
RunProfile *profile = interpreter->profile;
FLOATVAL empty = calibrate(interpreter);
PIO_printf(interpreter, " Code J Name Calls Total/s Avg/ms\n");
for (j = 0; j < interpreter->op_count + PARROT_PROF_EXTRA; j++) {
UINTVAL n = profile->data[j].numcalls;
profile->data[j].op = j;
if (j >= PARROT_PROF_EXTRA) {
profile->data[j].time -= empty * n;
if (profile->data[j].time < 0.0) /* faster than noop */
profile->data[j].time = 0.0;
}
}
qsort(profile->data, interpreter->op_count +
PARROT_PROF_EXTRA,
sizeof(ProfData), prof_sort_f);
for (j = 0; j < interpreter->op_count + PARROT_PROF_EXTRA; j++) {
UINTVAL n = profile->data[j].numcalls;
FLOATVAL t = profile->data[j].time;
if (n > 0) {
op_count++;
call_count += n;
sum_time += t;
k = profile->data[j].op;
jit = '-';
#if JIT_CAPABLE
if (k >= PARROT_PROF_EXTRA &&
op_jit[k - PARROT_PROF_EXTRA].extcall != 1)
jit = 'j';
#endif
PIO_printf(interpreter, " %4d %c %-25s %8vu %10vf %10.6vf\n",
k - PARROT_PROF_EXTRA,
jit,
op_name(interpreter, k),
n,
t,
(FLOATVAL)(t * 1000.0 / (FLOATVAL)n)
);
}
}
PIO_printf(interpreter, " %4vu - %-25s %8vu %10vf %10.6vf\n",
op_count,
"-",
call_count,
sum_time,
(FLOATVAL)(sum_time * 1000.0 / (FLOATVAL)call_count)
);
}
}
/*
=item C<static void print_debug(int status, void *p)>
Prints GC info.
=cut
*/
static void
print_debug(int status, void *p)
{
Parrot_Interp interpreter = (Parrot_Interp) p;
if (Interp_debug_TEST(interpreter, PARROT_MEM_STAT_DEBUG_FLAG)) {
/* Give the souls brave enough to activate debugging an earful
* about GC. */
PIO_eprintf(interpreter, "*** Parrot VM: Dumping GC info ***\n");
PDB_info(interpreter);
}
}
static PMC*
set_current_sub(Interp *interpreter)
{
opcode_t i, ci;
struct PackFile_ByteCode *cur_cs;
struct PackFile_FixupTable *ft;
struct PackFile_ConstTable *ct;
struct Parrot_sub *sub;
PMC *sub_pmc;
opcode_t *code_start;
size_t offs;
/*
* Walk the fixup table. The first Sub-like entry should be our
* entry point with the address at our resume_offset.
*/
cur_cs = interpreter->code;
ft = cur_cs->fixups;
ct = cur_cs->const_table;
for (i = 0; i < ft->fixup_count; i++) {
switch (ft->fixups[i]->type) {
case enum_fixup_sub:
ci = ft->fixups[i]->offset;
sub_pmc = ct->constants[ci]->u.key;
sub = PMC_sub(sub_pmc);
if (sub->seg != cur_cs)
continue;
code_start = (opcode_t*) sub->seg->base.data;
offs = sub->start_offs;
if (offs == interpreter->resume_offset) {
CONTEXT(interpreter->ctx)->current_sub = sub_pmc;
CONTEXT(interpreter->ctx)->current_HLL = sub->HLL_id;
return sub_pmc;
}
break;
}
}
/*
* if we didn't find anything put a dummy PMC into current_sub
*/
sub_pmc = pmc_new(interpreter, enum_class_Sub);
PMC_sub(sub_pmc)->start_offs = 0;
CONTEXT(interpreter->ctx)->current_sub = sub_pmc;
return sub_pmc;
}
/*
=item C<void
Parrot_runcode(Interp *interpreter, int argc, char *argv[])>
Sets up C<ARGV> and runs the ops.
=cut
*/
void
Parrot_runcode(Interp *interpreter, int argc, char *argv[])
{
PMC *userargv, *main_sub;
if (Interp_debug_TEST(interpreter, PARROT_START_DEBUG_FLAG)) {
PIO_eprintf(interpreter,
"*** Parrot VM: Setting stack top. ***\n");
}
/* Debugging mode nonsense. */
if (Interp_debug_TEST(interpreter, PARROT_START_DEBUG_FLAG)) {
if (Interp_flags_TEST(interpreter, PARROT_BOUNDS_FLAG)) {
PIO_eprintf(interpreter,
"*** Parrot VM: Bounds checking enabled. ***\n");
}
if (Interp_trace_TEST(interpreter, PARROT_TRACE_OPS_FLAG)) {
PIO_eprintf(interpreter, "*** Parrot VM: Tracing enabled. ***\n");
}
PIO_eprintf(interpreter, "*** Parrot VM: ");
switch (interpreter->run_core) {
case PARROT_SLOW_CORE:
PIO_eprintf(interpreter, "Slow core");
break;
case PARROT_FAST_CORE:
PIO_eprintf(interpreter, "Fast core");
break;
case PARROT_SWITCH_CORE:
case PARROT_SWITCH_JIT_CORE:
PIO_eprintf(interpreter, "Switch core");
break;
case PARROT_CGP_CORE:
case PARROT_CGP_JIT_CORE:
PIO_eprintf(interpreter, "CGP core");
break;
case PARROT_CGOTO_CORE:
PIO_eprintf(interpreter, "CGoto core");
break;
case PARROT_JIT_CORE:
PIO_eprintf(interpreter, "JIT core");
break;
case PARROT_EXEC_CORE:
PIO_eprintf(interpreter, "EXEC core");
break;
}
PIO_eprintf(interpreter, " ***\n");
}
/* Set up @ARGS (or whatever this language calls it) in userargv. */
userargv = setup_argv(interpreter, argc, argv);
#if EXEC_CAPABLE
/* s. runops_exec interpreter.c */
if (Interp_core_TEST(interpreter, PARROT_EXEC_CORE)) {
extern int Parrot_exec_run;
Parrot_exec_run = 1;
}
#endif
/*
* If any profile information was gathered, print it out
* before exiting, then print debug infos if turned on.
*/
Parrot_on_exit(print_debug, interpreter);
Parrot_on_exit(print_profile, interpreter);
/* Let's kick the tires and light the fires--call interpreter.c:runops. */
main_sub = CONTEXT(interpreter->ctx)->current_sub;
/*
* if no sub was marked being :main, we create a dummy sub with offset 0
*/
if (!main_sub) {
main_sub = set_current_sub(interpreter);
}
CONTEXT(interpreter->ctx)->current_sub = NULL;
CONTEXT(interpreter->ctx)->constants =
interpreter->code->const_table->constants;
Parrot_runops_fromc_args(interpreter, main_sub, "vP", userargv);
}
/*
=item C<void
Parrot_debug(Interp *interpreter, int argc, char **argv)>
Runs the interpreter's bytecode in debugging mode.
=cut
*/
opcode_t *
Parrot_debug(Interp *debugger, opcode_t * pc)
{
PDB_t *pdb;
const char *command;
Interp *interpreter;
pdb = debugger->pdb;
pdb->cur_opcode = pc;
PDB_init(debugger, NULL);
/* disassemble needs this for now */
interpreter = pdb->debugee;
interpreter->pdb = pdb;
debugger->lo_var_ptr = interpreter->lo_var_ptr;
PDB_disassemble(interpreter, NULL);
while (!(pdb->state & PDB_EXIT)) {
PDB_get_command(debugger);
command = pdb->cur_command;
PDB_run_command(debugger, command);
}
return NULL;
}
/*
=item C<void
Parrot_disassemble(Interp *interpreter)>
Disassembles and prints out the interpreter's bytecode.
This is used by the Parrot disassembler.
=cut
*/
void
Parrot_disassemble(Interp *interpreter)
{
PDB_t *pdb;
PDB_line_t *line;
char *c;
pdb = (PDB_t *)mem_sys_allocate_zeroed(sizeof(PDB_t));
interpreter->pdb = pdb;
pdb->cur_opcode = interpreter->code->base.data;
PDB_disassemble(interpreter, NULL);
line = pdb->file->line;
while (line->next) {
/* If it has a label print it */
if (line->label)
PIO_printf(interpreter, "L%li:\t", line->label->number);
c = pdb->file->source + line->source_offset;
while (*c != '\n' && c)
PIO_printf(interpreter, "%c", *(c++));
PIO_printf(interpreter, "\n");
line = line->next;
}
return;
}
/*
=item C<void
Parrot_run_native(Parrot_Interp interpreter, native_func_t func)>
Run the C function C<func> through the program C<[enternative, end]>.
This ensures that the function is run with the same setup as in other
run loops.
This function is used in some of the source tests in F<t/src> which use
the interpreter outside a runloop.
=cut
*/
void
Parrot_run_native(Parrot_Interp interpreter, native_func_t func)
{
static opcode_t program_code[2];
struct PackFile * pf;
program_code[0] = interpreter->op_lib->op_code("enternative", 0);
program_code[1] = 0; /* end */
pf = PackFile_new(interpreter, 0);
pf->cur_cs = (struct PackFile_ByteCode *)
(pf->PackFuncs[PF_BYTEC_SEG].new_seg)(interpreter, pf, "code", 1);
pf->cur_cs->base.data = program_code;
pf->cur_cs->base.size = 2;
Parrot_loadbc(interpreter, pf);
run_native = func;
if (interpreter->code && interpreter->code->const_table) {
CONTEXT(interpreter->ctx)->constants =
interpreter->code->const_table->constants;
}
runops(interpreter, interpreter->resume_offset);
}
/*
=back
=head1 SEE ALSO
F<include/parrot/embed.h> and F<docs/embed.pod>.
=head1 HISTORY
Initial version by Brent Dax on 2002.1.28.
=cut
*/
/*
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/