-
Notifications
You must be signed in to change notification settings - Fork 171
/
frame.c
1812 lines (1668 loc) · 76.5 KB
/
frame.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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "moar.h"
/* This allows the dynlex cache to be disabled when bug hunting, if needed. */
#define MVM_DYNLEX_CACHE_ENABLED 1
/* Check spesh candidate pre-selections match the guards. */
#define MVM_SPESH_CHECK_PRESELECTION 0
/* Computes the initial work area for a frame or a specialization of a frame. */
MVMRegister * MVM_frame_initial_work(MVMThreadContext *tc, MVMuint16 *local_types,
MVMuint16 num_locals) {
MVMuint16 i;
MVMRegister *work_initial = MVM_calloc(num_locals, sizeof(MVMRegister));
for (i = 0; i < num_locals; i++)
if (local_types[i] == MVM_reg_obj)
work_initial[i].o = tc->instance->VMNull;
return work_initial;
}
/* Takes a static frame and does various one-off calculations about what
* space it shall need. Also triggers bytecode verification of the frame's
* bytecode. Assumes we are holding the CU's deserialize frame mutex (at
* the time of writing, this is only called from instrumentation_level_barrier). */
static void prepare_and_verify_static_frame(MVMThreadContext *tc, MVMStaticFrame *static_frame) {
MVMStaticFrameBody *static_frame_body = &static_frame->body;
MVMCompUnit *cu = static_frame_body->cu;
/* Ensure the frame is fully deserialized. */
if (!static_frame_body->fully_deserialized) {
MVMROOT(tc, static_frame, {
MVM_bytecode_finish_frame(tc, cu, static_frame, 0);
});
}
/* If we never invoked this compilation unit before, and we have spesh
* enabled, we might either have no spesh log or a nearly full one. This
* will cause problems with gathering data to OSR hot loops. */
if (!cu->body.invoked) {
cu->body.invoked = 1;
if (tc->instance->spesh_enabled)
MVM_spesh_log_new_compunit(tc);
}
/* Work size is number of locals/registers plus size of the maximum
* call site argument list. */
static_frame_body->work_size = sizeof(MVMRegister) *
(static_frame_body->num_locals + static_frame_body->cu->body.max_callsite_size);
/* Validate the bytecode. */
MVMROOT(tc, static_frame, {
MVM_validate_static_frame(tc, static_frame);
});
/* Compute work area initial state that we can memcpy into place each
* time. */
if (static_frame_body->num_locals)
static_frame_body->work_initial = MVM_frame_initial_work(tc,
static_frame_body->local_types,
static_frame_body->num_locals);
/* Check if we have any state var lexicals. */
if (static_frame_body->static_env_flags) {
MVMuint8 *flags = static_frame_body->static_env_flags;
MVMint64 numlex = static_frame_body->num_lexicals;
MVMint64 i;
for (i = 0; i < numlex; i++)
if (flags[i] == 2) {
static_frame_body->has_state_vars = 1;
break;
}
}
/* Allocate the frame's spesh data structure; do it in gen2, both for
* the sake of not triggering GC here to avoid a deadlock risk, but
* also because then it can be assigned into the gen2 static frame
* without causing it to become an inter-gen root. */
MVM_gc_allocate_gen2_default_set(tc);
MVM_ASSIGN_REF(tc, &(static_frame->common.header), static_frame_body->spesh, /* no GC error */
MVM_repr_alloc_init(tc, tc->instance->StaticFrameSpesh));
MVM_gc_allocate_gen2_default_clear(tc);
}
/* When we don't match the current instrumentation level, we hit this. It may
* simply be that we never invoked the frame, in which case we prepare and
* verify it. It may also be because we need to instrument the code for
* profiling. */
static void instrumentation_level_barrier(MVMThreadContext *tc, MVMStaticFrame *static_frame) {
MVMCompUnit *cu = static_frame->body.cu;
MVMROOT2(tc, static_frame, cu, {
/* Obtain mutex, so we don't end up with instrumentation races. */
MVM_reentrantmutex_lock(tc, (MVMReentrantMutex *)cu->body.deserialize_frame_mutex);
/* Re-check instrumentation level in case of races. */
if (static_frame->body.instrumentation_level != tc->instance->instrumentation_level) {
/* Prepare and verify if needed. */
if (static_frame->body.instrumentation_level == 0)
prepare_and_verify_static_frame(tc, static_frame);
/* Add profiling instrumentation if needed. */
if (tc->instance->profiling)
MVM_profile_instrument(tc, static_frame);
else if (tc->instance->cross_thread_write_logging)
MVM_cross_thread_write_instrument(tc, static_frame);
else if (tc->instance->coverage_logging)
MVM_line_coverage_instrument(tc, static_frame);
else if (tc->instance->debugserver)
MVM_breakpoint_instrument(tc, static_frame);
else
/* XXX uninstrumenting is currently turned off, due to multithreading
* woes. If you add an instrumentation that has to be "turned off"
* again at some point, a solution for this problem must be found. */
MVM_profile_ensure_uninstrumented(tc, static_frame);
/* Set up inline cache for the frame. */
MVM_disp_inline_cache_setup(tc, static_frame);
/* Mark frame as being at the current instrumentation level. */
MVM_barrier();
static_frame->body.instrumentation_level = tc->instance->instrumentation_level;
}
/* Release the lock. */
MVM_reentrantmutex_unlock(tc, (MVMReentrantMutex *)cu->body.deserialize_frame_mutex);
});
}
/* Called when the GC destroys a frame. Since the frame may have been alive as
* part of a continuation that was taken but never invoked, we should check
* things normally cleaned up on return don't need cleaning up also. */
void MVM_frame_destroy(MVMThreadContext *tc, MVMFrame *frame) {
MVM_args_proc_cleanup(tc, &frame->params);
if (frame->env && !MVM_FRAME_IS_ON_CALLSTACK(tc, frame))
MVM_fixed_size_free(tc, tc->instance->fsa, frame->allocd_env, frame->env);
if (frame->extra) {
MVMFrameExtra *e = frame->extra;
MVM_fixed_size_free(tc, tc->instance->fsa, sizeof(MVMFrameExtra), e);
}
}
/* Creates a frame for usage as a context only, possibly forcing all of the
* static lexicals to be deserialized if it's used for auto-close purposes.
* Since we're not creating it to run bytecode, just for the purpose of a
* serialized closure, we don't create any call stack record for it. */
static MVMFrame * create_context_only(MVMThreadContext *tc, MVMStaticFrame *static_frame,
MVMObject *code_ref, MVMint32 autoclose) {
MVMFrame *frame;
MVMROOT2(tc, static_frame, code_ref, {
/* Ensure the frame is fully deserialized. */
if (!static_frame->body.fully_deserialized) {
MVM_reentrantmutex_lock(tc,
(MVMReentrantMutex *)static_frame->body.cu->body.deserialize_frame_mutex);
if (!static_frame->body.fully_deserialized)
MVM_bytecode_finish_frame(tc, static_frame->body.cu, static_frame, 0);
MVM_reentrantmutex_unlock(tc,
(MVMReentrantMutex *)static_frame->body.cu->body.deserialize_frame_mutex);
}
frame = MVM_gc_allocate_frame(tc);
});
/* Set static frame and code ref. */
MVM_ASSIGN_REF(tc, &(frame->header), frame->static_info, static_frame);
MVM_ASSIGN_REF(tc, &(frame->header), frame->code_ref, code_ref);
/* Allocate space for lexicals, copying the default lexical environment
* into place and, if we're auto-closing, making sure anything we'd clone
* is vivified to prevent the clone (which is what creates the correct
* BEGIN/INIT semantics). */
if (static_frame->body.env_size) {
frame->env = MVM_fixed_size_alloc_zeroed(tc, tc->instance->fsa, static_frame->body.env_size);
frame->allocd_env = static_frame->body.env_size;
if (autoclose) {
MVMROOT2(tc, frame, static_frame, {
MVMuint16 i;
MVMuint16 num_lexicals = static_frame->body.num_lexicals;
for (i = 0; i < num_lexicals; i++) {
if (!static_frame->body.static_env[i].o && static_frame->body.static_env_flags[i] == 1) {
MVMuint32 scid;
MVMuint32 objid;
if (MVM_bytecode_find_static_lexical_scref(tc, static_frame->body.cu,
static_frame, i, &scid, &objid)) {
MVMObject *resolved;
MVMSerializationContext *sc = MVM_sc_get_sc(tc, static_frame->body.cu, scid);
if (sc == NULL)
MVM_exception_throw_adhoc(tc,
"SC not yet resolved; lookup failed");
resolved = MVM_sc_get_object(tc, sc, objid);
MVM_ASSIGN_REF(tc, &(static_frame->common.header),
static_frame->body.static_env[i].o,
resolved);
}
}
}
});
}
memcpy(frame->env, static_frame->body.static_env, static_frame->body.env_size);
}
return frame;
}
/* Creates a frame that is suitable for deserializing a context into. Starts
* with a ref count of 1 due to being held by an SC. */
MVMFrame * MVM_frame_create_context_only(MVMThreadContext *tc, MVMStaticFrame *static_frame,
MVMObject *code_ref) {
return create_context_only(tc, static_frame, code_ref, 0);
}
/* Provides auto-close functionality, for the handful of cases where we have
* not ever been in the outer frame of something we're invoking. In this case,
* we fake up a frame based on the static lexical environment. */
static MVMFrame * autoclose(MVMThreadContext *tc, MVMStaticFrame *needed) {
MVMFrame *result;
/* First, see if we can find one on the call stack; return it if so. */
MVMFrame *candidate = tc->cur_frame;
while (candidate) {
if (candidate->static_info->body.bytecode == needed->body.bytecode)
return candidate;
candidate = candidate->caller;
}
/* If not, fake up a frame See if it also needs an outer. */
MVMROOT(tc, needed, {
result = create_context_only(tc, needed, (MVMObject *)needed->body.static_code, 1);
});
if (needed->body.outer) {
/* See if the static code object has an outer. */
MVMCode *outer_code = needed->body.outer->body.static_code;
if (outer_code->body.outer &&
outer_code->body.outer->static_info->body.bytecode == needed->body.bytecode) {
/* Yes, just take it. */
MVM_ASSIGN_REF(tc, &(result->header), result->outer, outer_code->body.outer);
}
else {
/* Otherwise, recursively auto-close. */
MVMROOT(tc, result, {
MVMFrame *ac = autoclose(tc, needed->body.outer);
MVM_ASSIGN_REF(tc, &(result->header), result->outer, ac);
});
}
}
return result;
}
/* Obtains memory for a frame that we are about to enter and run bytecode in. Prefers
* the callstack by default, but can put the frame onto the heap if it tends to be
* promoted there anyway. Returns a pointer to the frame wherever in memory it ends
* up living. */
static MVMFrame * allocate_frame(MVMThreadContext *tc, MVMStaticFrame *static_frame,
MVMSpeshCandidate *spesh_cand, MVMint32 heap) {
MVMFrame *frame;
MVMint32 num_locals;
MVMStaticFrameBody *static_frame_body;
MVMJitCode *jitcode;
MVMint32 work_size = spesh_cand ? spesh_cand->body.work_size : static_frame->body.work_size;
MVMint32 env_size = spesh_cand ? spesh_cand->body.env_size : static_frame->body.env_size;
if (heap) {
/* Allocate frame on the heap. The callstack record includes space
* for the work registers and ->work will have been set up already. */
MVMROOT2(tc, static_frame, spesh_cand, {
if (tc->cur_frame)
MVM_frame_force_to_heap(tc, tc->cur_frame);
frame = MVM_callstack_allocate_heap_frame(tc, work_size)->frame;
});
/* If we have an environment, that needs allocating separately for
* heap-based frames. */
if (env_size) {
frame->env = MVM_fixed_size_alloc_zeroed(tc, tc->instance->fsa, env_size);
frame->allocd_env = env_size;
}
}
else {
/* Allocate the frame on the call stack. The callstack record includes
* space for both the work registers and the environment, and both the
* ->work and ->env pointers will have been set up already, but we do
* need to clear the environment. */
MVMCallStackFrame *record = MVM_callstack_allocate_frame(tc, work_size, env_size);
frame = &(record->frame);
memset(frame->env, 0, env_size);
}
/* Set up work area. */
static_frame_body = &(static_frame->body);
jitcode = spesh_cand ? spesh_cand->body.jitcode : NULL;
num_locals = jitcode && jitcode->local_types ? jitcode->num_locals :
(spesh_cand ? spesh_cand->body.num_locals : static_frame_body->num_locals);
if (work_size) {
if (spesh_cand) {
/* Zero frame memory. Spesh makes sure we have VMNull setup in
* the places we need it. */
memset(frame->work, 0, work_size);
}
else {
/* Copy frame template with VMNulls in to place. */
memcpy(frame->work, static_frame_body->work_initial,
sizeof(MVMRegister) * static_frame_body->num_locals);
}
/* Calculate args buffer position. */
frame->args = frame->work + num_locals;
}
/* Set static frame and caller before we let this frame escape and the GC
* see it. */
frame->static_info = static_frame;
frame->caller = tc->cur_frame;
return frame;
}
/* Set up a deopt frame. */
void MVM_frame_setup_deopt(MVMThreadContext *tc, MVMFrame *frame, MVMStaticFrame *static_frame,
MVMCode *code_ref) {
/* Initialize various frame properties. */
frame->static_info = static_frame;
frame->code_ref = (MVMObject *)code_ref;
frame->outer = code_ref->body.outer;
frame->spesh_cand = NULL;
frame->spesh_correlation_id = 0;
frame->args = frame->work + static_frame->body.num_locals;
}
/* Sets up storage for state variables. We do this after tc->cur_frame became
* the current frame, to make sure these new objects will certainly get marked
* if GC is triggered along the way. */
static void setup_state_vars(MVMThreadContext *tc, MVMStaticFrame *static_frame) {
/* Drag everything out of static_frame_body before we start,
* as GC action may invalidate it. */
MVMFrame *frame = tc->cur_frame;
MVMRegister *env = static_frame->body.static_env;
MVMuint8 *flags = static_frame->body.static_env_flags;
MVMint64 numlex = static_frame->body.num_lexicals;
MVMRegister *state = NULL;
MVMint64 state_act = 0; /* 0 = none so far, 1 = first time, 2 = later */
MVMint64 i;
MVMROOT(tc, frame, {
for (i = 0; i < numlex; i++) {
if (flags[i] == 2) {
redo_state:
switch (state_act) {
case 0:
if (MVM_UNLIKELY(!frame->code_ref))
MVM_exception_throw_adhoc(tc,
"Frame must have code-ref to have state variables");
state = ((MVMCode *)frame->code_ref)->body.state_vars;
if (state) {
/* Already have state vars; pull them from this. */
state_act = 2;
}
else {
/* Allocate storage for state vars. */
state = (MVMRegister *)MVM_calloc(1, frame->static_info->body.env_size);
((MVMCode *)frame->code_ref)->body.state_vars = state;
state_act = 1;
/* Note that this frame should run state init code. */
frame->flags |= MVM_FRAME_FLAG_STATE_INIT;
}
goto redo_state;
case 1: {
MVMObject *cloned = MVM_repr_clone(tc, env[i].o);
frame->env[i].o = cloned;
MVM_ASSIGN_REF(tc, &(frame->code_ref->header), state[i].o, cloned);
break;
}
case 2:
frame->env[i].o = state[i].o;
break;
}
}
}
});
}
/* Produces an error on outer frame mis-match. */
static void report_outer_conflict(MVMThreadContext *tc, MVMStaticFrame *static_frame,
MVMFrame *outer) {
char *frame_cuuid = MVM_string_utf8_encode_C_string(tc, static_frame->body.cuuid);
char *frame_name;
char *outer_cuuid = MVM_string_utf8_encode_C_string(tc, outer->static_info->body.cuuid);
char *outer_name;
char *frame_outer_cuuid = MVM_string_utf8_encode_C_string(tc,
static_frame->body.outer
? static_frame->body.outer->body.cuuid
: tc->instance->str_consts.empty);
char *frame_outer_name;
char *waste[7] = { frame_cuuid, outer_cuuid, frame_outer_cuuid, NULL, NULL, NULL, NULL };
int waste_counter = 3;
if (static_frame->body.name) {
frame_name = MVM_string_utf8_encode_C_string(tc, static_frame->body.name);
waste[waste_counter++] = frame_name;
}
else {
frame_name = "<anonymous static frame>";
}
if (outer->static_info->body.name) {
outer_name = MVM_string_utf8_encode_C_string(tc, outer->static_info->body.name);
waste[waste_counter++] = outer_name;
}
else {
outer_name = "<anonymous static frame>";
}
if (static_frame->body.outer && static_frame->body.outer->body.name) {
frame_outer_name = MVM_string_utf8_encode_C_string(tc, static_frame->body.outer->body.name);
waste[waste_counter++] = frame_outer_name;
}
else {
frame_outer_name = "<anonymous static frame>";
}
MVM_exception_throw_adhoc_free(tc, waste,
"When invoking %s '%s', provided outer frame %p (%s '%s') does not match expected static frame %p (%s '%s')",
frame_cuuid,
frame_name,
outer->static_info,
outer_cuuid,
outer_name,
static_frame->body.outer,
frame_outer_cuuid,
frame_outer_name);
}
/* Dispatches execution to the specified code object with the specified args. */
void MVM_frame_dispatch(MVMThreadContext *tc, MVMCode *code, MVMArgs args, MVMint32 spesh_cand) {
MVMFrame *frame;
MVMuint8 *chosen_bytecode;
MVMStaticFrameSpesh *spesh;
/* If the frame was never invoked before, or never before at the current
* instrumentation level, we need to trigger the instrumentation level
* barrier. */
MVMStaticFrame *static_frame = code->body.sf;
if (MVM_UNLIKELY(static_frame->body.instrumentation_level != tc->instance->instrumentation_level)) {
MVMROOT2(tc, static_frame, code, {
instrumentation_level_barrier(tc, static_frame);
});
}
/* Ensure we have an outer if needed. This is done ahead of allocating the
* new frame, since an autoclose will force the callstack on to the heap. */
MVMFrame *outer = code->body.outer;
if (outer) {
/* We were provided with an outer frame. Ensure that it is based on the
* correct static frame (compare on bytecode address to cope with
* nqp::freshcoderef). */
if (MVM_UNLIKELY(static_frame->body.outer == 0 || outer->static_info->body.orig_bytecode != static_frame->body.outer->body.orig_bytecode))
report_outer_conflict(tc, static_frame, outer);
}
else if (static_frame->body.static_code) {
MVMCode *static_code = static_frame->body.static_code;
if (static_code->body.outer) {
/* We're lacking an outer, but our static code object may have one.
* This comes up in the case of cloned protoregexes, for example. */
outer = static_code->body.outer;
}
else if (static_frame->body.outer) {
/* Auto-close, and cache it in the static frame. */
MVMROOT3(tc, static_frame, code, static_code, {
MVM_frame_force_to_heap(tc, tc->cur_frame);
outer = autoclose(tc, static_frame->body.outer);
MVM_ASSIGN_REF(tc, &(static_code->common.header),
static_code->body.outer, outer);
});
}
else {
outer = NULL;
}
}
/* See if any specializations apply. */
spesh = static_frame->body.spesh;
if (spesh_cand < 0) {
spesh_cand = MVM_spesh_arg_guard_run(tc, spesh->body.spesh_arg_guard,
args, NULL);
}
#if MVM_SPESH_CHECK_PRESELECTION
else {
MVMint32 certain = -1;
MVMint32 correct = MVM_spesh_arg_guard_run(tc, spesh->body.spesh_arg_guard,
args, &certain);
if (spesh_cand != correct && spesh_cand != certain) {
fprintf(stderr, "Inconsistent spesh preselection of '%s' (%s): got %d, not %d\n",
MVM_string_utf8_encode_C_string(tc, static_frame->body.name),
MVM_string_utf8_encode_C_string(tc, static_frame->body.cuuid),
spesh_cand, correct);
MVM_dump_backtrace(tc);
}
}
#endif
if (spesh_cand >= 0) {
MVMSpeshCandidate *chosen_cand = spesh->body.spesh_candidates[spesh_cand];
if (static_frame->body.allocate_on_heap) {
MVMROOT4(tc, static_frame, code, outer, chosen_cand, {
frame = allocate_frame(tc, static_frame, chosen_cand, 1);
});
}
else {
frame = allocate_frame(tc, static_frame, chosen_cand, 0);
frame->spesh_correlation_id = 0;
}
frame->code_ref = (MVMObject *)code;
frame->outer = outer;
if (chosen_cand->body.jitcode) {
chosen_bytecode = chosen_cand->body.jitcode->bytecode;
frame->jit_entry_label = chosen_cand->body.jitcode->labels[0];
}
else {
chosen_bytecode = chosen_cand->body.bytecode;
}
frame->effective_spesh_slots = chosen_cand->body.spesh_slots;
frame->spesh_cand = chosen_cand;
/* Initialize argument processing. */
MVM_args_proc_setup(tc, &(frame->params), args);
}
else {
MVMint32 on_heap = static_frame->body.allocate_on_heap;
if (on_heap) {
MVMROOT3(tc, static_frame, code, outer, {
frame = allocate_frame(tc, static_frame, NULL, 1);
});
}
else {
frame = allocate_frame(tc, static_frame, NULL, 0);
frame->spesh_cand = NULL;
frame->effective_spesh_slots = NULL;
frame->spesh_correlation_id = 0;
}
frame->code_ref = (MVMObject *)code;
frame->outer = outer;
chosen_bytecode = static_frame->body.bytecode;
/* Initialize argument processing. Do this before the GC might process the frame. */
MVM_args_proc_setup(tc, &(frame->params), args);
/* If we should be spesh logging, set the correlation ID. */
if (tc->instance->spesh_enabled && tc->spesh_log && static_frame->body.bytecode_size < MVM_SPESH_MAX_BYTECODE_SIZE) {
if (spesh->body.spesh_entries_recorded++ < MVM_SPESH_LOG_LOGGED_ENOUGH) {
MVMint32 id = ++tc->spesh_cid;
frame->spesh_correlation_id = id;
MVMROOT3(tc, static_frame, code, outer, {
if (on_heap) {
MVMROOT(tc, frame, {
MVM_spesh_log_entry(tc, id, static_frame, args);
});
}
else {
MVMROOT2(tc, frame->caller, frame->static_info, {
MVM_spesh_log_entry(tc, id, static_frame, args);
});
}
});
}
}
}
MVM_jit_code_trampoline(tc);
/* Update interpreter and thread context, so next execution will use this
* frame. */
tc->cur_frame = frame;
*(tc->interp_cur_op) = chosen_bytecode;
*(tc->interp_bytecode_start) = chosen_bytecode;
*(tc->interp_reg_base) = frame->work;
*(tc->interp_cu) = static_frame->body.cu;
if (static_frame->body.has_state_vars)
setup_state_vars(tc, static_frame);
}
/* Dispatches to a frame with zero args. Convenience for various entrypoint
* style locations. */
void MVM_frame_dispatch_zero_args(MVMThreadContext *tc, MVMCode *code) {
MVMArgs args = {
.callsite = MVM_callsite_get_common(tc, MVM_CALLSITE_ID_ZERO_ARITY),
.source = NULL,
.map = NULL
};
MVM_frame_dispatch(tc, code, args, -1);
}
/* Dispatches to a frame with args set up by C code. Also sets the expected
* return type and destination for the return value. */
void MVM_frame_dispatch_from_c(MVMThreadContext *tc, MVMCode *code,
MVMCallStackArgsFromC *args_record, MVMRegister *return_value,
MVMReturnType return_type) {
MVMFrame *cur_frame = tc->cur_frame;
cur_frame->return_value = return_value;
cur_frame->return_type = return_type;
cur_frame->return_address = *(tc->interp_cur_op);
MVM_frame_dispatch(tc, code, args_record->args, -1);
}
/* Moves the specified frame from the stack and on to the heap. Must only
* be called if the frame is not already there. Use MVM_frame_force_to_heap
* when not sure. */
MVMFrame * MVM_frame_move_to_heap(MVMThreadContext *tc, MVMFrame *frame) {
/* To keep things simple, we'll promote all non-promoted frames on the call
* stack. We walk the call stack to find them. */
MVMFrame *cur_to_promote = NULL;
MVMFrame *new_cur_frame = NULL;
MVMFrame *update_caller = NULL;
MVMFrame *result = NULL;
MVMCallStackIterator iter;
MVM_callstack_iter_frame_init(tc, &iter, tc->stack_top);
MVM_CHECK_CALLER_CHAIN(tc, cur_to_promote);
MVMROOT4(tc, new_cur_frame, update_caller, cur_to_promote, result, {
while (MVM_callstack_iter_move_next(tc, &iter)) {
/* Check this isn't already a heap or promoted frame; if it is, we're
* done. */
MVMCallStackRecord *record = MVM_callstack_iter_current(tc, &iter);
if (MVM_callstack_kind_ignoring_deopt(record) != MVM_CALLSTACK_RECORD_FRAME)
break;
MVMCallStackFrame *unpromoted_record = (MVMCallStackFrame *)record;
cur_to_promote = &(unpromoted_record->frame);
/* Move any lexical environment to the heap, as it may now
* out-live the callstack entry. */
MVMuint16 env_size = cur_to_promote->allocd_env;
if (env_size) {
MVMRegister *heap_env = MVM_fixed_size_alloc(tc,
tc->instance->fsa, env_size);
memcpy(heap_env, cur_to_promote->env, env_size);
cur_to_promote->env = heap_env;
}
else {
/* Stack frames may set up the env pointer even if it's to
* an empty area (avoids branches); ensure it is nulled out
* so we don't try to do a bogus free later. */
cur_to_promote->env = NULL;
}
/* Clear any dynamic lexical cache entry, as it may point into an
* environment that gets moved to the heap. */
MVMFrameExtra *e = cur_to_promote->extra;
if (e)
e->dynlex_cache_name = NULL;
/* Allocate a heap frame. */
/* frame is safe from the GC as we wouldn't be here if it wasn't on the stack */
MVMFrame *promoted = MVM_gc_allocate_frame(tc);
/* Bump heap promotion counter, to encourage allocating this kind
* of frame directly on the heap in the future. If the frame was
* entered at least 50 times, and over 80% of the entries lead to
* an eventual heap promotion, them we'll mark it to be allocated
* right away on the heap. Note that entries is only bumped when
* spesh logging is taking place, so we only bump the number of
* heap promotions in that case too. */
MVMStaticFrame *sf = cur_to_promote->static_info;
if (!sf->body.allocate_on_heap && cur_to_promote->spesh_correlation_id) {
MVMuint32 promos = sf->body.spesh->body.num_heap_promotions++;
MVMuint32 entries = sf->body.spesh->body.spesh_entries_recorded;
if (entries > 50 && promos > (4 * entries) / 5)
sf->body.allocate_on_heap = 1;
}
/* Copy current frame's body to it. */
memcpy(
(char *)promoted + sizeof(MVMCollectable),
(char *)cur_to_promote + sizeof(MVMCollectable),
sizeof(MVMFrame) - sizeof(MVMCollectable));
/* Update stack record to indicate the promotion, and make it
* reference the heap frame. */
if (record->kind == MVM_CALLSTACK_RECORD_DEOPT_FRAME)
record->orig_kind = MVM_CALLSTACK_RECORD_PROMOTED_FRAME;
else
record->kind = MVM_CALLSTACK_RECORD_PROMOTED_FRAME;
((MVMCallStackPromotedFrame *)record)->frame = promoted;
/* Update caller of previously promoted frame, if any. This is the
* only reference that might point to a non-heap frame. */
if (update_caller) {
MVM_ASSIGN_REF(tc, &(update_caller->header),
update_caller->caller, promoted);
}
/* If we're the first time through the lopo, then we're instead
* replacing the current stack top. Note we do it at the end,
* so that the GC can still walk unpromoted frames if it runs
* in this loop. */
else {
new_cur_frame = promoted;
}
/* If the frame we're promoting was in the active handlers list,
* update the address there. */
if (tc->active_handlers) {
MVMActiveHandler *ah = tc->active_handlers;
while (ah) {
if (ah->frame == cur_to_promote)
ah->frame = promoted;
ah = ah->next_handler;
}
}
/* If we're replacing the frame we were asked to promote, that will
* become our result. */
if (cur_to_promote == frame)
result = promoted;
/* Check if there's a caller, or if we reached the end of the
* chain. */
if (cur_to_promote->caller) {
if (MVM_FRAME_IS_ON_CALLSTACK(tc, cur_to_promote->caller)) {
/* Clear caller in promoted frame, to avoid a heap -> stack
* reference if we GC during this loop. */
promoted->caller = NULL;
update_caller = promoted;
}
else {
if (cur_to_promote == tc->thread_entry_frame)
tc->thread_entry_frame = promoted;
MVM_gc_write_barrier(tc, (MVMCollectable*)promoted, (MVMCollectable*)promoted->caller);
}
}
else {
/* End of caller chain; check if we promoted the entry
* frame */
if (cur_to_promote == tc->thread_entry_frame)
tc->thread_entry_frame = promoted;
}
}
});
MVM_CHECK_CALLER_CHAIN(tc, new_cur_frame);
/* All is promoted. Update thread's current frame pointer. */
tc->cur_frame = new_cur_frame;
/* Hand back new location of promoted frame. */
if (!result)
MVM_panic(1, "Failed to find frame to promote on call stack");
return result;
}
/* This function is to be used by the debugserver if a thread is currently
* blocked. */
MVMFrame * MVM_frame_debugserver_move_to_heap(MVMThreadContext *debug_tc,
MVMThreadContext *owner, MVMFrame *frame) {
/* To keep things simple, we'll promote all non-promoted frames on the call
* stack. We walk the call stack to find them. */
MVMFrame *cur_to_promote = NULL;
MVMFrame *new_cur_frame = NULL;
MVMFrame *update_caller = NULL;
MVMFrame *result = NULL;
MVMCallStackIterator iter;
MVM_callstack_iter_frame_init(owner, &iter, owner->stack_top);
MVM_CHECK_CALLER_CHAIN(owner, cur_to_promote);
MVMROOT4(debug_tc, new_cur_frame, update_caller, cur_to_promote, result, {
while (MVM_callstack_iter_move_next(owner, &iter)) {
/* Check this isn't already a heap or promoted frame; if it is, we're
* done. */
MVMCallStackRecord *record = MVM_callstack_iter_current(owner, &iter);
if (MVM_callstack_kind_ignoring_deopt(record) != MVM_CALLSTACK_RECORD_FRAME)
break;
MVMCallStackFrame *unpromoted_record = (MVMCallStackFrame *)record;
cur_to_promote = &(unpromoted_record->frame);
/* Allocate a heap frame. */
/* frame is safe from the GC as we wouldn't be here if it wasn't on the stack */
MVMFrame *promoted = MVM_gc_allocate_frame(debug_tc);
/* Copy current frame's body to it. */
memcpy(
(char *)promoted + sizeof(MVMCollectable),
(char *)cur_to_promote + sizeof(MVMCollectable),
sizeof(MVMFrame) - sizeof(MVMCollectable));
/* Update stack record to indicate the promotion, and make it
* reference the heap frame. */
if (record->kind == MVM_CALLSTACK_RECORD_DEOPT_FRAME)
record->orig_kind = MVM_CALLSTACK_RECORD_PROMOTED_FRAME;
else
record->kind = MVM_CALLSTACK_RECORD_PROMOTED_FRAME;
((MVMCallStackPromotedFrame *)record)->frame = promoted;
/* Update caller of previously promoted frame, if any. This is the
* only reference that might point to a non-heap frame. */
if (update_caller) {
MVM_ASSIGN_REF(debug_tc, &(update_caller->header),
update_caller->caller, promoted);
}
/* If we're the first time through the lopo, then we're instead
* replacing the current stack top. Note we do it at the end,
* so that the GC can still walk unpromoted frames if it runs
* in this loop. */
else {
new_cur_frame = promoted;
}
/* If the frame we're promoting was in the active handlers list,
* update the address there. */
if (owner->active_handlers) {
MVMActiveHandler *ah = owner->active_handlers;
while (ah) {
if (ah->frame == cur_to_promote)
ah->frame = promoted;
ah = ah->next_handler;
}
}
/* If we're replacing the frame we were asked to promote, that will
* become our result. */
if (cur_to_promote == frame)
result = promoted;
/* Check if there's a caller, or if we reached the end of the
* chain. */
if (cur_to_promote->caller) {
if (MVM_FRAME_IS_ON_CALLSTACK(owner, cur_to_promote->caller)) {
/* Clear caller in promoted frame, to avoid a heap -> stack
* reference if we GC during this loop. */
promoted->caller = NULL;
update_caller = promoted;
}
else {
if (cur_to_promote == owner->thread_entry_frame)
owner->thread_entry_frame = promoted;
MVM_gc_write_barrier(debug_tc, (MVMCollectable*)promoted, (MVMCollectable*)promoted->caller);
}
}
else {
/* End of caller chain; check if we promoted the entry
* frame */
if (cur_to_promote == owner->thread_entry_frame)
owner->thread_entry_frame = promoted;
}
}
});
MVM_CHECK_CALLER_CHAIN(owner, new_cur_frame);
/* All is promoted. Update thread's current frame pointer. */
owner->cur_frame = new_cur_frame;
/* Hand back new location of promoted frame. */
if (!result)
MVM_panic(1, "Failed to find frame to promote on foreign thread's call stack");
return result;
}
/* Removes a single frame, as part of a return or unwind. Done after any exit
* handler has already been run. */
static MVMuint64 remove_one_frame(MVMThreadContext *tc, MVMuint8 unwind) {
MVMFrame *returner = tc->cur_frame;
MVMuint32 need_caller;
/* Clear up any extra frame data. */
if (returner->extra) {
MVMFrameExtra *e = returner->extra;
need_caller = e->caller_info_needed;
/* Preserve the extras if the frame has been used in a ctx operation
* and marked with caller info. */
if (!(e->caller_deopt_idx || e->caller_jit_position)) {
MVM_fixed_size_free_at_safepoint(tc, tc->instance->fsa, sizeof(MVMFrameExtra), e);
returner->extra = NULL;
}
}
else {
need_caller = 0;
}
/* Clean up any allocations for argument working area. */
MVM_args_proc_cleanup(tc, &returner->params);
/* NULL out ->work, to indicate the frame is no longer in dynamic scope.
* This is used by the GC to avoid marking stuff (this is needed for
* safety as otherwise we'd read freed memory), as well as by exceptions to
* ensure the target of an exception throw is indeed still in dynamic
* scope. */
returner->work = NULL;
/* Unwind call stack entries. From this, we find out the caller. This may
* actually *not* be the caller in the frame, because of lazy deopt. Also
* it may invoke something else, in which case we go no further and just
* return to the runloop. */
MVMuint32 thunked = 0;
MVMFrame *caller;
if (MVM_FRAME_IS_ON_CALLSTACK(tc, returner)) {
caller = MVM_callstack_unwind_frame(tc, unwind, &thunked);
}
else {
MVMROOT(tc, returner, {
caller = MVM_callstack_unwind_frame(tc, unwind, &thunked);
});
if (!need_caller)
returner->caller = NULL;
}
if (thunked)
return 1;
/* Switch back to the caller frame if there is one. */
if (caller && (returner != tc->thread_entry_frame || tc->nested_interpreter)) {
*(tc->interp_cur_op) = caller->return_address;
*(tc->interp_bytecode_start) = MVM_frame_effective_bytecode(caller);
*(tc->interp_reg_base) = caller->work;
*(tc->interp_cu) = caller->static_info->body.cu;
/* Handle any special return hooks. */
if (caller->extra) {
MVMFrameExtra *e = caller->extra;
if (e->special_return || e->special_unwind) {
MVMSpecialReturn sr = e->special_return;
MVMSpecialReturn su = e->special_unwind;
void *srd = e->special_return_data;
e->special_return = NULL;
e->special_unwind = NULL;
e->special_return_data = NULL;
e->mark_special_return_data = NULL;
if (unwind && su)
su(tc, srd);
else if (!unwind && sr)
sr(tc, srd);
/* The special_return or special_unwind handler may schedule a
finalizer call for the current runloop. If we are already in
the top most call frame of the runloop, we need to replace
the thread_entry_frame with the finalizer call. Otherwise we
won't run the special code for ending the runloop when the
finalizer returns. */
if (returner == tc->thread_entry_frame && tc->cur_frame != caller)
tc->thread_entry_frame = tc->cur_frame;
}
}
if (returner == tc->thread_entry_frame && tc->cur_frame == caller) {
tc->cur_frame = NULL;
return 0;
}
else {
/* We're either somewhere in a nested call or already up in the top
most frame but still need to run some finalizer, so keep the
runloop running */
return 1;
}
}
else {
tc->cur_frame = NULL;
return 0;
}
}
/* Attempt to return from the current frame. Returns non-zero if we can,
* and zero if there is nowhere to return to (which would signal the exit
* of the interpreter). */
static void remove_after_handler(MVMThreadContext *tc, void *sr_data) {
remove_one_frame(tc, 0);
}
MVMuint64 MVM_frame_try_return(MVMThreadContext *tc) {
MVMFrame *cur_frame = tc->cur_frame;
if (cur_frame->static_info->body.has_exit_handler &&
!(cur_frame->flags & MVM_FRAME_FLAG_EXIT_HAND_RUN)) {
/* Set us up to run exit handler, and make it so we'll really exit the
* frame when that has been done. */
if (tc->cur_frame == tc->thread_entry_frame)
MVM_exception_throw_adhoc(tc, "Thread entry point frame cannot have an exit handler");
MVMFrame *caller = cur_frame->caller;
if (!caller)
MVM_exception_throw_adhoc(tc, "Entry point frame cannot have an exit handler");
MVMHLLConfig *hll = MVM_hll_current(tc);
MVMObject *result;
if (caller->return_type == MVM_RETURN_OBJ) {
result = caller->return_value->o;
if (!result)
result = tc->instance->VMNull;
}
else {
MVMROOT(tc, cur_frame, {
switch (caller->return_type) {
case MVM_RETURN_INT:
result = MVM_repr_box_int(tc, hll->int_box_type, caller->return_value->i64);
break;
case MVM_RETURN_NUM:
result = MVM_repr_box_num(tc, hll->num_box_type, caller->return_value->n64);
break;
case MVM_RETURN_STR:
result = MVM_repr_box_str(tc, hll->str_box_type, caller->return_value->s);
break;
case MVM_RETURN_VOID:
result = cur_frame->extra && cur_frame->extra->exit_handler_result
? cur_frame->extra->exit_handler_result
: tc->instance->VMNull;
break;
default:
result = tc->instance->VMNull;
}
});