/
disp.c
1412 lines (1338 loc) · 68.5 KB
/
disp.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"
#define GET_UI16(pc, idx) *((MVMuint16 *)((pc) + (idx)))
MVM_STATIC_INLINE MVMuint64 GET_UI64(const MVMuint8 *pc, MVMint32 idx) {
MVMuint64 retval;
memcpy(&retval, pc + idx, sizeof(retval));
return retval;
}
/* Create op info for a dispatch instruction, so that during specialization we
* can pretend it's not varargs. */
size_t MVM_spesh_disp_dispatch_op_info_size(MVMThreadContext *tc,
const MVMOpInfo *base_info, MVMCallsite *callsite) {
/* In general, ops support up to an operand limit; in the case that there are more,
* we'd overrun the buffer. We thus allocate more. */
MVMuint32 total_ops = base_info->num_operands + callsite->flag_count;
return sizeof(MVMOpInfo) + (total_ops > MVM_MAX_OPERANDS
? total_ops - MVM_MAX_OPERANDS
: 0) * sizeof(MVMuint8);
}
void MVM_spesh_disp_initialize_dispatch_op_info(MVMThreadContext *tc,
const MVMOpInfo *base_info, MVMCallsite *callsite, MVMOpInfo *dispatch_info) {
/* Populate based on the original operation. */
memcpy(dispatch_info, base_info, sizeof(MVMOpInfo));
/* Tweak the operand count and set up new operand info based on the callsite. */
dispatch_info->num_operands += callsite->flag_count;
MVMuint16 operand_index = base_info->num_operands;
MVMuint16 flag_index;
for (flag_index = 0; flag_index < callsite->flag_count; operand_index++, flag_index++) {
MVMCallsiteFlags flag = callsite->arg_flags[flag_index];
if (flag & MVM_CALLSITE_ARG_OBJ) {
dispatch_info->operands[operand_index] = MVM_operand_obj;
}
else if (flag & MVM_CALLSITE_ARG_INT) {
dispatch_info->operands[operand_index] = MVM_operand_int64;
}
else if (flag & MVM_CALLSITE_ARG_NUM) {
dispatch_info->operands[operand_index] = MVM_operand_num64;
}
else if (flag & MVM_CALLSITE_ARG_STR) {
dispatch_info->operands[operand_index] = MVM_operand_str;
}
dispatch_info->operands[operand_index] |= MVM_operand_read_reg;
}
}
/* Returns the callsite argument offset for an opcode. */
MVMCallsite * MVM_spesh_disp_callsite_for_dispatch_op(MVMuint16 opcode, MVMuint8 *args,
MVMCompUnit *cu) {
switch (opcode) {
case MVM_OP_dispatch_v:
case MVM_OP_sp_dispatch_v:
return cu->body.callsites[GET_UI16(args, 4)];
case MVM_OP_dispatch_i:
case MVM_OP_dispatch_n:
case MVM_OP_dispatch_s:
case MVM_OP_dispatch_o:
case MVM_OP_sp_dispatch_i:
case MVM_OP_sp_dispatch_n:
case MVM_OP_sp_dispatch_s:
case MVM_OP_sp_dispatch_o:
return cu->body.callsites[GET_UI16(args, 6)];
case MVM_OP_sp_runbytecode_v:
case MVM_OP_sp_runcfunc_v:
case MVM_OP_sp_runnativecall_v:
return (MVMCallsite *)GET_UI64(args, 2);
case MVM_OP_sp_runbytecode_i:
case MVM_OP_sp_runbytecode_n:
case MVM_OP_sp_runbytecode_s:
case MVM_OP_sp_runbytecode_o:
case MVM_OP_sp_runcfunc_i:
case MVM_OP_sp_runcfunc_n:
case MVM_OP_sp_runcfunc_s:
case MVM_OP_sp_runcfunc_o:
case MVM_OP_sp_runnativecall_i:
case MVM_OP_sp_runnativecall_n:
case MVM_OP_sp_runnativecall_s:
case MVM_OP_sp_runnativecall_o:
return (MVMCallsite *)GET_UI64(args, 4);
default:
MVM_panic(1, "Unknown dispatch op when resolving callsite");
}
}
/* Hit count of an outcome, used for analizing how to optimize the dispatch. */
typedef struct {
MVMuint32 outcome;
MVMuint32 hits;
} OutcomeHitCount;
/* Rewrite a dispatch instruction into an sp_dispatch one, resolving only the
* static frame and inline cache offset. */
static void rewrite_to_sp_dispatch(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshIns *ins,
MVMuint32 bytecode_offset) {
/* Resolve the callsite. */
MVMuint32 callsite_idx = ins->operands[ins->info->opcode == MVM_OP_dispatch_v ? 1 : 2].callsite_idx;
MVMCallsite *callsite = g->sf->body.cu->body.callsites[callsite_idx];
/* Pick the new dispatch instruction and create instruction info for it. */
const MVMOpInfo *new_ins_base_info;
switch (ins->info->opcode) {
case MVM_OP_dispatch_v: new_ins_base_info = MVM_op_get_op(MVM_OP_sp_dispatch_v); break;
case MVM_OP_dispatch_i: new_ins_base_info = MVM_op_get_op(MVM_OP_sp_dispatch_i); break;
case MVM_OP_dispatch_n: new_ins_base_info = MVM_op_get_op(MVM_OP_sp_dispatch_n); break;
case MVM_OP_dispatch_o: new_ins_base_info = MVM_op_get_op(MVM_OP_sp_dispatch_o); break;
case MVM_OP_dispatch_s: new_ins_base_info = MVM_op_get_op(MVM_OP_sp_dispatch_s); break;
default:
MVM_oops(tc, "Unexpected dispatch instruction to rewrite");
}
MVMOpInfo *new_ins_info = MVM_spesh_alloc(tc, g, MVM_spesh_disp_dispatch_op_info_size(
tc, new_ins_base_info, callsite));
MVM_spesh_disp_initialize_dispatch_op_info(tc, new_ins_base_info, callsite, new_ins_info);
ins->info = new_ins_info;
/* Rewrite the operands. */
MVMSpeshOperand *new_operands = MVM_spesh_alloc(tc, g,
new_ins_info->num_operands * sizeof(MVMSpeshOperand));
MVMuint32 target = 0;
MVMuint32 source = 0;
if (new_ins_info->opcode != MVM_OP_sp_dispatch_v)
new_operands[target++] = ins->operands[source++]; /* Result value */
new_operands[target++] = ins->operands[source++]; /* Dispatcher name */
new_operands[target++] = ins->operands[source++]; /* Callsite index */
new_operands[target++].lit_i16 = MVM_spesh_add_spesh_slot_try_reuse(tc, g,
(MVMCollectable *)g->sf);
new_operands[target++].lit_ui32 = MVM_disp_inline_cache_get_slot(tc, g->sf, bytecode_offset);
memcpy(new_operands + target, ins->operands + source,
callsite->flag_count * sizeof(MVMSpeshOperand));
ins->operands = new_operands;
}
/* Get the index of the first normal argument operand to the dispatch
* instruction (after result unless void, dispatcher name, and callsite). */
static MVMuint32 find_disp_op_first_real_arg(MVMThreadContext *tc, MVMSpeshIns *ins) {
if (ins->info->opcode == MVM_OP_dispatch_v)
return 2;
assert(ins->info->opcode == MVM_OP_dispatch_i ||
ins->info->opcode == MVM_OP_dispatch_n ||
ins->info->opcode == MVM_OP_dispatch_s ||
ins->info->opcode == MVM_OP_dispatch_o);
return 3;
}
/* Find an annotation on a dispatch instruction and remove it. */
static MVMSpeshAnn * take_dispatch_annotation(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshIns *ins, MVMint32 kind) {
MVMSpeshAnn *deopt_ann = ins->annotations;
MVMSpeshAnn *prev = NULL;
while (deopt_ann) {
if (deopt_ann->type == kind) {
if (prev)
prev->next = deopt_ann->next;
else
ins->annotations = deopt_ann->next;
deopt_ann->next = NULL;
return deopt_ann;
}
prev = deopt_ann;
deopt_ann = deopt_ann->next;
}
MVM_panic(1, "Spesh: unexpectedly missing annotation on dispatch op");
}
/* Creates an annotation relating a synthetic (added during optimization) deopt
* point back to the original one whose usages will have been recorded in the
* facts. */
static MVMSpeshAnn * create_synthetic_deopt_annotation(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshAnn *in) {
MVMSpeshAnn *ann = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshAnn));
ann->type = MVM_SPESH_ANN_DEOPT_SYNTH;
ann->data.deopt_idx = in->data.deopt_idx;
return ann;
}
/* Takes an instruction that may deopt and an operand that should contain the
* deopt index. Reuse the dispatch instruction deopt annotation if that did
* not happen already, otherwise clone it. */
static void set_deopt(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshIns *ins,
MVMSpeshOperand *index_operand, MVMSpeshAnn *deopt_ann,
MVMuint32 *reused_deopt_ann) {
if (*reused_deopt_ann) {
/* Already reused, clone needed. */
MVMuint32 new_deopt_index = MVM_spesh_graph_add_deopt_annotation(tc, g,
ins, g->deopt_addrs[deopt_ann->data.deopt_idx * 2], deopt_ann->type);
index_operand->lit_ui32 = new_deopt_index;
deopt_ann = create_synthetic_deopt_annotation(tc, g, deopt_ann);
}
else {
/* First usage. */
*reused_deopt_ann = 1;
index_operand->lit_ui32 = deopt_ann->data.deopt_idx;
}
deopt_ann->next = ins->annotations;
ins->annotations = deopt_ann;
}
/* Emit a type, concreteness or object literal guard instruction (when concreteness
* only, comparee is null). */
static MVMSpeshOperand emit_guard(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns **insert_after, MVMuint16 op,
MVMSpeshOperand guard_reg, MVMCollectable *comparee, MVMSpeshAnn *deopt_ann,
MVMuint32 *reused_deopt_ann) {
/* Produce a new version for after the guarding. */
MVMSpeshOperand guarded_reg = MVM_spesh_manipulate_split_version(tc, g,
guard_reg, bb, (*insert_after)->next);
/* Produce the instruction and insert it. */
MVMSpeshIns *guard = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
guard->info = MVM_op_get_op(op);
guard->operands = MVM_spesh_alloc(tc, g, (comparee ? 4 : 3) * sizeof(MVMSpeshOperand));
guard->operands[0] = guarded_reg;
guard->operands[1] = guard_reg;
if (comparee) {
guard->operands[2].lit_i16 = MVM_spesh_add_spesh_slot_try_reuse(tc, g,
comparee);
set_deopt(tc, g, guard, &(guard->operands[3]), deopt_ann, reused_deopt_ann);
}
else {
set_deopt(tc, g, guard, &(guard->operands[2]), deopt_ann, reused_deopt_ann);
}
MVM_spesh_manipulate_insert_ins(tc, bb, *insert_after, guard);
*insert_after = guard;
/* Tweak usages. */
MVM_spesh_get_facts(tc, g, guarded_reg)->writer = guard;
MVM_spesh_usages_add_by_reg(tc, g, guard_reg, guard);
/* Add facts to and return the guarded register. */
MVM_spesh_facts_guard_facts(tc, g, bb, guard);
return guarded_reg;
}
/* Emit a HLL guard instruction. */
static MVMSpeshOperand emit_hll_guard(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns **insert_after, MVMSpeshOperand guard_reg,
MVMHLLConfig *hll, MVMSpeshAnn *deopt_ann, MVMuint32 *reused_deopt_ann) {
/* Produce a new version for after the guarding. */
MVMSpeshOperand guarded_reg = MVM_spesh_manipulate_split_version(tc, g,
guard_reg, bb, (*insert_after)->next);
/* Produce the instruction and insert it. */
MVMSpeshIns *guard = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
guard->info = MVM_op_get_op(MVM_OP_sp_guardhll);
guard->operands = MVM_spesh_alloc(tc, g, 4 * sizeof(MVMSpeshOperand));
guard->operands[0] = guarded_reg;
guard->operands[1] = guard_reg;
guard->operands[2].lit_ui64 = (MVMuint64)hll;
set_deopt(tc, g, guard, &(guard->operands[3]), deopt_ann, reused_deopt_ann);
MVM_spesh_manipulate_insert_ins(tc, bb, *insert_after, guard);
*insert_after = guard;
/* Tweak usages. */
MVM_spesh_get_facts(tc, g, guarded_reg)->writer = guard;
MVM_spesh_usages_add_by_reg(tc, g, guard_reg, guard);
/* Add facts to and return the guarded register. */
MVM_spesh_facts_guard_facts(tc, g, bb, guard);
return guarded_reg;
}
/* Emit a simple binary instruction with a result register and either a
* input register or input constant. */
static void emit_bi_op(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns **insert_after, MVMuint16 op,
MVMSpeshOperand to_reg, MVMSpeshOperand from_reg) {
/* Produce the instruction and insert it. */
MVMSpeshIns *ins = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
ins->info = MVM_op_get_op(op);
ins->operands = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshOperand) * 2);
ins->operands[0] = to_reg;
ins->operands[1] = from_reg;
MVM_spesh_manipulate_insert_ins(tc, bb, *insert_after, ins);
*insert_after = ins;
/* Tweak usages. */
MVM_spesh_get_facts(tc, g, to_reg)->writer = ins;
if ((ins->info->operands[1] & MVM_operand_rw_mask) == MVM_operand_read_reg)
MVM_spesh_usages_add_by_reg(tc, g, from_reg, ins);
}
/* Emit a simple three-ary instruction where the first register is written
* to and the other two operands are registers that are read from. */
static void emit_tri_op(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns **insert_after, MVMuint16 op,
MVMSpeshOperand to_reg, MVMSpeshOperand from_reg, MVMSpeshOperand third_reg) {
/* Produce the instruction and insert it. */
MVMSpeshIns *ins = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
ins->info = MVM_op_get_op(op);
ins->operands = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshOperand) * 3);
ins->operands[0] = to_reg;
ins->operands[1] = from_reg;
ins->operands[2] = third_reg;
MVM_spesh_manipulate_insert_ins(tc, bb, *insert_after, ins);
*insert_after = ins;
/* Tweak usages. */
MVM_spesh_get_facts(tc, g, to_reg)->writer = ins;
MVM_spesh_usages_add_by_reg(tc, g, from_reg, ins);
MVM_spesh_usages_add_by_reg(tc, g, third_reg, ins);
}
/* Emit an instruction to load a value into a spesh slot. */
static void emit_load_spesh_slot(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns **insert_after, MVMSpeshOperand to_reg,
MVMCollectable *value) {
/* Add the instruction. */
MVMSpeshIns *ins = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
ins->info = MVM_op_get_op(MVM_OP_sp_getspeshslot);
ins->operands = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshOperand) * 2);
ins->operands[0] = to_reg;
ins->operands[1].lit_i16 = MVM_spesh_add_spesh_slot_try_reuse(tc, g, value);
MVM_spesh_manipulate_insert_ins(tc, bb, *insert_after, ins);
*insert_after = ins;
/* Set facts. */
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g, to_reg);
facts->writer = ins;
facts->flags |= MVM_SPESH_FACT_KNOWN_VALUE | MVM_SPESH_FACT_KNOWN_TYPE |
(IS_CONCRETE(value) ? MVM_SPESH_FACT_CONCRETE : MVM_SPESH_FACT_TYPEOBJ);
facts->value.o = (MVMObject *)value;
facts->type = STABLE(value)->WHAT;
}
/* Emit an instruction to load an attribute. */
static void emit_load_attribute(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns **insert_after, MVMuint16 op,
MVMSpeshOperand to_reg, MVMSpeshOperand from_reg, MVMuint16 offset) {
/* Produce instruction. */
MVMSpeshIns *ins = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
ins->info = MVM_op_get_op(op);
ins->operands = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshOperand) * 3);
ins->operands[0] = to_reg;
ins->operands[1] = from_reg;
ins->operands[2].lit_i16 = offset;
MVM_spesh_graph_add_comment(tc, g, ins, "emitted from dispatch program attribute load op");
MVM_spesh_manipulate_insert_ins(tc, bb, *insert_after, ins);
*insert_after = ins;
/* Keep usage in sync. */
MVM_spesh_get_facts(tc, g, to_reg)->writer = ins;
MVM_spesh_usages_add_by_reg(tc, g, from_reg, ins);
}
/* Emit a guard that a value is a given literal string. */
static MVMSpeshOperand emit_literal_str_guard(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns **insert_after, MVMSpeshOperand testee,
MVMString *expected, MVMSpeshAnn *deopt_ann, MVMuint32 *reused_deopt_ann) {
/* Load the string literal value from a spesh slot. */
MVMSpeshOperand cmp_str_reg = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_str);
emit_load_spesh_slot(tc, g, bb, insert_after, cmp_str_reg, (MVMCollectable *)expected);
/* Emit the comparison op. */
MVMSpeshOperand op_res_reg = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_int64);
emit_tri_op(tc, g, bb, insert_after, MVM_OP_eq_s, op_res_reg, testee, cmp_str_reg);
/* Emit the guard. */
emit_guard(tc, g, bb, insert_after, MVM_OP_sp_guardnonzero, op_res_reg, NULL,
deopt_ann, reused_deopt_ann);
/* The temporary registers are immediately free for re-use. */
MVM_spesh_manipulate_release_temp_reg(tc, g, cmp_str_reg);
MVM_spesh_manipulate_release_temp_reg(tc, g, op_res_reg);
return testee;
}
static MVMuint16 resumption_op_non_constant(MVMDispProgram *dp, MVMuint16 res_idx) {
/* Count up how many non-constant values the resume init capture will
* involve. */
MVMDispProgramResumption *dpr = &(dp->resumptions[res_idx]);
MVMuint16 non_constant;
if (dpr->init_values) {
MVMuint16 i;
non_constant = 0;
for (i = 0; i < dpr->init_callsite->flag_count; i++) {
switch (dpr->init_values[i].source) {
case MVM_DISP_RESUME_INIT_ARG:
case MVM_DISP_RESUME_INIT_TEMP:
non_constant++;
default:
break;
}
}
}
else {
/* It's based on the initial argument catpure to the dispatch. */
non_constant = dpr->init_callsite->flag_count;
}
return non_constant;
}
size_t MVM_spesh_disp_resumption_op_info_size(MVMThreadContext *tc,
MVMDispProgram *dp, MVMuint16 res_idx) {
MVMuint16 non_constant = resumption_op_non_constant(dp, res_idx);
/* Form the operand info. */
const MVMOpInfo *base_info = MVM_op_get_op(MVM_OP_sp_resumption);
MVMuint16 total_ops = base_info->num_operands + non_constant;
return sizeof(MVMOpInfo) + (total_ops > MVM_MAX_OPERANDS
? total_ops - MVM_MAX_OPERANDS
: 0);
}
/* Form the instruction info for an sp_resumption given the specified dispatch
* program resumption. This is a varargs instruction, thus why we need to
* synthesize the instruction info. */
MVMOpInfo * MVM_spesh_disp_initialize_resumption_op_info(MVMThreadContext *tc,
MVMDispProgram *dp, MVMuint16 res_idx, MVMOpInfo *res_info) {
const MVMOpInfo *base_info = MVM_op_get_op(MVM_OP_sp_resumption);
MVMuint16 non_constant = resumption_op_non_constant(dp, res_idx);
MVMDispProgramResumption *dpr = &(dp->resumptions[res_idx]);
memcpy(res_info, base_info, sizeof(MVMOpInfo));
res_info->num_operands += non_constant;
MVMuint16 operand_index = base_info->num_operands;
MVMuint16 i;
for (i = 0; i < dpr->init_callsite->flag_count; i++) {
MVMint32 include = dpr->init_values
? dpr->init_values[i].source == MVM_DISP_RESUME_INIT_ARG ||
dpr->init_values[i].source == MVM_DISP_RESUME_INIT_TEMP
: 1;
if (include) {
MVMCallsiteFlags flag = dpr->init_callsite->arg_flags[i];
if (flag & MVM_CALLSITE_ARG_OBJ)
res_info->operands[operand_index] = MVM_operand_obj;
else if (flag & MVM_CALLSITE_ARG_INT)
res_info->operands[operand_index] = MVM_operand_int64;
else if (flag & MVM_CALLSITE_ARG_NUM)
res_info->operands[operand_index] = MVM_operand_num64;
else if (flag & MVM_CALLSITE_ARG_STR)
res_info->operands[operand_index] = MVM_operand_str;
res_info->operands[operand_index] |= MVM_operand_read_reg;
operand_index++;
}
}
return res_info;
}
/* Insert an instruction relating to each dispatch resumption init state that
* is wanted by the dispatch program. This makes sure we preserve the various
* registers that are used by the resumption. */
static void insert_resume_inits(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb,
MVMSpeshIns **insert_after, MVMDispProgram *dp, MVMSpeshOperand *orig_args,
MVMSpeshOperand *temporaries, MVMint32 deopt_idx) {
MVMuint16 i;
for (i = 0; i < dp->num_resumptions; i++) {
/* Allocate the instruction. */
MVMDispProgramResumption *dpr = &(dp->resumptions[i]);
MVMSpeshIns *ins = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
MVMOpInfo *res_op_info = MVM_spesh_alloc(tc, g,
MVM_spesh_disp_resumption_op_info_size(tc, dp, i));
MVM_spesh_disp_initialize_resumption_op_info(tc, dp, i, res_op_info);
ins->info = res_op_info;
ins->operands = MVM_spesh_alloc(tc, g, ins->info->num_operands * sizeof(MVMSpeshOperand));
/* Get a register to use for the resumption state, should it be
* required. */
ins->operands[0] = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_obj);
MVM_spesh_get_facts(tc, g, ins->operands[0])->writer = ins;
/* Prepare a spesh resume init record holding data about the dispatch
* resumption and write it into the instruction operands. */
ins->operands[1].lit_ui16 = MVM_VECTOR_ELEMS(g->resume_inits);
MVMSpeshResumeInit init_record = {
.dp = dp,
.deopt_idx = deopt_idx,
.res_idx = i,
.state_register = 0,
.init_registers = NULL,
};
MVM_VECTOR_PUSH(g->resume_inits, init_record);
/* Add all of the non-constant args. */
MVMuint16 j;
MVMuint16 insert_pos = 3;
for (j = 0; j < dpr->init_callsite->flag_count; j++) {
MVMSpeshOperand source;
if (!dpr->init_values) {
source = orig_args[j];
}
else if (dpr->init_values[j].source == MVM_DISP_RESUME_INIT_ARG) {
source = orig_args[dpr->init_values[j].index];
}
else if (dpr->init_values[j].source == MVM_DISP_RESUME_INIT_TEMP) {
source = temporaries[dpr->init_values[j].index];
}
else {
continue; /* Constant */
}
ins->operands[2].lit_ui16++;
ins->operands[insert_pos++] = source;
MVM_spesh_usages_add_by_reg(tc, g, source, ins);
}
/* Insert the instruction. */
MVM_spesh_manipulate_insert_ins(tc, bb, *insert_after, ins);
*insert_after = ins;
}
}
/* Try to translate a dispatch program into a sequence of ops (which will
* be subject to later optimization and potentially JIT compilation). */
static int translate_dispatch_program(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *bb, MVMSpeshIns *ins, MVMDispProgram *dp, MVMSpeshIns **next_ins) {
/* First, validate it is a dispatch program we know how to compile. */
MVMuint32 i;
for (i = 0; i < dp->num_ops; i++) {
switch (dp->ops[i].code) {
case MVMDispOpcodeGuardArgType:
case MVMDispOpcodeGuardArgTypeConc:
case MVMDispOpcodeGuardArgTypeTypeObject:
case MVMDispOpcodeGuardArgConc:
case MVMDispOpcodeGuardArgTypeObject:
case MVMDispOpcodeGuardArgLiteralObj:
case MVMDispOpcodeGuardArgLiteralStr:
case MVMDispOpcodeGuardArgNotLiteralObj:
case MVMDispOpcodeGuardArgHLL:
case MVMDispOpcodeGuardTempType:
case MVMDispOpcodeGuardTempTypeConc:
case MVMDispOpcodeGuardTempTypeTypeObject:
case MVMDispOpcodeGuardTempConc:
case MVMDispOpcodeGuardTempTypeObject:
case MVMDispOpcodeGuardTempLiteralObj:
case MVMDispOpcodeGuardTempLiteralStr:
case MVMDispOpcodeGuardTempNotLiteralObj:
case MVMDispOpcodeGuardTempHLL:
case MVMDispOpcodeLoadCaptureValue:
case MVMDispOpcodeLoadConstantObjOrStr:
case MVMDispOpcodeLoadConstantInt:
case MVMDispOpcodeLoadConstantNum:
case MVMDispOpcodeLoadAttributeObj:
case MVMDispOpcodeLoadAttributeInt:
case MVMDispOpcodeLoadAttributeNum:
case MVMDispOpcodeLoadAttributeStr:
case MVMDispOpcodeUnboxInt:
case MVMDispOpcodeUnboxNum:
case MVMDispOpcodeUnboxStr:
case MVMDispOpcodeLoadHOW:
case MVMDispOpcodeLookup:
case MVMDispOpcodeSet:
case MVMDispOpcodeResultValueObj:
case MVMDispOpcodeResultValueStr:
case MVMDispOpcodeResultValueInt:
case MVMDispOpcodeResultValueNum:
case MVMDispOpcodeUseArgsTail:
case MVMDispOpcodeCopyArgsTail:
case MVMDispOpcodeResultBytecode:
case MVMDispOpcodeResultCFunction:
case MVMDispOpcodeResultForeignCode:
break;
default:
MVM_spesh_graph_add_comment(tc, g, ins, "dispatch not compiled: op %s NYI",
MVM_disp_opcode_to_name(dp->ops[i].code));
return 0;
}
}
/* We'll re-use the deopt annotation on the dispatch instruction for
* the first guard, and then clone it later if needed. */
MVMSpeshAnn *deopt_ann = take_dispatch_annotation(tc, g, ins,
MVM_SPESH_ANN_DEOPT_PRE_INS);
MVMuint32 reused_deopt_ann = 0;
/* Steal other annotations in order to put them onto any runbytecode
* instruction we generate. */
MVMSpeshAnn *deopt_all_ann = take_dispatch_annotation(tc, g, ins,
MVM_SPESH_ANN_DEOPT_ALL_INS);
MVMSpeshAnn *cached_ann = take_dispatch_annotation(tc, g, ins,
MVM_SPESH_ANN_CACHED);
/* Find the arguments that are the input to the dispatch and copy
* them, since we'll mutate this list. */
MVMuint32 first_real_arg = find_disp_op_first_real_arg(tc, ins);
MVMSpeshOperand *orig_args = &ins->operands[first_real_arg];
MVMuint32 num_real_args = ins->info->num_operands - first_real_arg;
size_t args_size = num_real_args * sizeof(MVMSpeshOperand);
MVMSpeshOperand *args = MVM_spesh_alloc(tc, g, args_size);
memcpy(args, orig_args, args_size);
/* Registers holding temporaries, which may be registers that we have
* allocated, or may refer to the input arguments. */
MVMSpeshOperand *temporaries = MVM_spesh_alloc(tc, g,
sizeof(MVMSpeshOperand) * dp->num_temporaries);
/* Registers that we have allocated, and should release after the
* emitting of the dispatch program. (Since the temporaries could get
* reused for something else and another register aliased there, we
* need to keep track of the list of things to release separately
* from the temporaries array above.) */
MVM_VECTOR_DECL(MVMSpeshOperand, allocated_temps);
MVM_VECTOR_INIT(allocated_temps, 0);
/* In the case we emit bytecode, we may also need to delay release of
* the temporaries until after runbytecode optimization. */
MVMint32 delay_temps_release = 0;
/* Visit the ops of the dispatch program and translate them. */
MVMSpeshIns *insert_after = ins;
MVMCallsite *callsite = NULL;
MVMint32 skip_args = -1;
for (i = 0; i < dp->num_ops; i++) {
MVMDispProgramOp *op = &(dp->ops[i]);
switch (op->code) {
case MVMDispOpcodeGuardArgType: {
MVMCollectable *wanted_st = dp->gc_constants[op->arg_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) == (MVMSTable *)wanted_st)
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guard, args[op->arg_guard.arg_idx],
wanted_st, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgTypeConc: {
MVMCollectable *wanted_st = dp->gc_constants[op->arg_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) == (MVMSTable *)wanted_st &&
(facts->flags & MVM_SPESH_FACT_CONCRETE))
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardconc, args[op->arg_guard.arg_idx],
wanted_st, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgTypeTypeObject: {
MVMCollectable *wanted_st = dp->gc_constants[op->arg_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) == (MVMSTable *)wanted_st &&
(facts->flags & MVM_SPESH_FACT_TYPEOBJ))
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardtype, args[op->arg_guard.arg_idx],
wanted_st, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgConc: {
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if (facts->flags & MVM_SPESH_FACT_CONCRETE)
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardjustconc, args[op->arg_guard.arg_idx],
NULL, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgTypeObject: {
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if (facts->flags & MVM_SPESH_FACT_TYPEOBJ)
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardjusttype, args[op->arg_guard.arg_idx],
NULL, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgLiteralObj: {
MVMCollectable *wanted_obj = dp->gc_constants[op->arg_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
facts->value.o == (MVMObject *)wanted_obj)
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardobj, args[op->arg_guard.arg_idx],
wanted_obj, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgLiteralStr: {
MVMString *wanted_str = (MVMString *)dp->gc_constants[op->arg_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
MVM_string_equal(tc, facts->value.s, wanted_str))
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_literal_str_guard(tc, g, bb,
&insert_after, args[op->arg_guard.arg_idx], wanted_str,
deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgNotLiteralObj: {
MVMCollectable *unwanted_obj = dp->gc_constants[op->arg_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if (((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
facts->value.o != (MVMObject *)unwanted_obj) ||
((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) != STABLE(unwanted_obj)))
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardnotobj, args[op->arg_guard.arg_idx],
unwanted_obj, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardArgHLL: {
MVMHLLConfig *wanted_hll = dp->constants[op->arg_guard.checkee].hll;
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
args[op->arg_guard.arg_idx]);
if (((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
STABLE(facts->value.o)->hll_owner == wanted_hll) ||
((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type)->hll_owner == wanted_hll))
MVM_spesh_use_facts(tc, g, facts);
else
args[op->arg_guard.arg_idx] = emit_hll_guard(tc, g, bb,
&insert_after, args[op->arg_guard.arg_idx], wanted_hll,
deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempType: {
MVMCollectable *wanted_st = dp->gc_constants[op->temp_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) == (MVMSTable *)wanted_st)
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guard, temporaries[op->temp_guard.temp],
wanted_st, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempTypeConc: {
MVMCollectable *wanted_st = dp->gc_constants[op->temp_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) == (MVMSTable *)wanted_st &&
(facts->flags & MVM_SPESH_FACT_CONCRETE))
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardconc, temporaries[op->temp_guard.temp],
wanted_st, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempTypeTypeObject: {
MVMCollectable *wanted_st = dp->gc_constants[op->temp_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) == (MVMSTable *)wanted_st &&
(facts->flags & MVM_SPESH_FACT_TYPEOBJ))
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardtype, temporaries[op->temp_guard.temp],
wanted_st, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempConc: {
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if (facts->flags & MVM_SPESH_FACT_CONCRETE)
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardjustconc, temporaries[op->temp_guard.temp],
NULL, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempTypeObject: {
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if (facts->flags & MVM_SPESH_FACT_TYPEOBJ)
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardjusttype, temporaries[op->temp_guard.temp],
NULL, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempLiteralObj: {
MVMCollectable *wanted_obj = dp->gc_constants[op->temp_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
facts->value.o == (MVMObject *)wanted_obj)
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardobj, temporaries[op->temp_guard.temp],
wanted_obj, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempLiteralStr: {
MVMString *wanted_str = (MVMString *)dp->gc_constants[op->temp_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if ((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
MVM_string_equal(tc, facts->value.s, wanted_str))
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_literal_str_guard(tc, g, bb,
&insert_after, temporaries[op->temp_guard.temp],
wanted_str, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempNotLiteralObj: {
MVMCollectable *unwanted_obj = dp->gc_constants[op->temp_guard.checkee];
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if (((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
facts->value.o != (MVMObject *)unwanted_obj) ||
((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type) != STABLE(unwanted_obj)))
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_guard(tc, g, bb, &insert_after,
MVM_OP_sp_guardnotobj, temporaries[op->temp_guard.temp],
unwanted_obj, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeGuardTempHLL: {
MVMHLLConfig *wanted_hll = dp->constants[op->temp_guard.checkee].hll;
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g,
temporaries[op->temp_guard.temp]);
if (((facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) &&
STABLE(facts->value.o)->hll_owner == wanted_hll) ||
((facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) &&
STABLE(facts->type)->hll_owner == wanted_hll))
MVM_spesh_use_facts(tc, g, facts);
else
temporaries[op->temp_guard.temp] = emit_hll_guard(tc, g, bb,
&insert_after, temporaries[op->temp_guard.temp],
wanted_hll, deopt_ann, &reused_deopt_ann);
break;
}
case MVMDispOpcodeLoadCaptureValue:
/* We already have all the capture values in the arg registers
* so just alias. */
temporaries[op->load.temp] = args[op->load.idx];
break;
case MVMDispOpcodeLoadConstantObjOrStr: {
MVMCollectable *value = dp->gc_constants[op->load.idx];
MVMuint16 reg_kind = REPR(value)->ID == MVM_REPR_ID_MVMString
? MVM_reg_str
: MVM_reg_obj;
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, reg_kind);
temporaries[op->load.temp] = temp;
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_load_spesh_slot(tc, g, bb, &insert_after, temp, value);
break;
}
case MVMDispOpcodeLoadConstantInt: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_int64);
temporaries[op->load.temp] = temp;
MVM_VECTOR_PUSH(allocated_temps, temp);
MVMSpeshOperand constint = { .lit_i64 = dp->constants[op->load.idx].i64 };
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_const_i64,
temporaries[op->load.temp], constint);
break;
}
case MVMDispOpcodeLoadConstantNum: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_num64);
temporaries[op->load.temp] = temp;
MVM_VECTOR_PUSH(allocated_temps, temp);
MVMSpeshOperand constnum = { .lit_i64 = dp->constants[op->load.idx].n64 };
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_const_n64,
temporaries[op->load.temp], constnum);
break;
}
case MVMDispOpcodeLoadAttributeObj: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_obj);
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_load_attribute(tc, g, bb, &insert_after, MVM_OP_sp_p6oget_o, temp,
temporaries[op->load.temp], op->load.idx);
temporaries[op->load.temp] = temp;
break;
}
case MVMDispOpcodeLoadAttributeInt: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_int64);
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_load_attribute(tc, g, bb, &insert_after, MVM_OP_sp_p6oget_i, temp,
temporaries[op->load.temp], op->load.idx);
temporaries[op->load.temp] = temp;
break;
}
case MVMDispOpcodeLoadAttributeNum: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_num64);
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_load_attribute(tc, g, bb, &insert_after, MVM_OP_sp_p6oget_n, temp,
temporaries[op->load.temp], op->load.idx);
temporaries[op->load.temp] = temp;
break;
}
case MVMDispOpcodeLoadAttributeStr: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_str);
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_load_attribute(tc, g, bb, &insert_after, MVM_OP_sp_p6oget_s, temp,
temporaries[op->load.temp], op->load.idx);
temporaries[op->load.temp] = temp;
break;
}
case MVMDispOpcodeUnboxInt: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_int64);
temporaries[op->load.temp] = temp;
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_unbox_i,
temporaries[op->load.temp], temporaries[op->load.idx]);
break;
}
case MVMDispOpcodeUnboxNum: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_num64);
temporaries[op->load.temp] = temp;
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_unbox_n,
temporaries[op->load.temp], temporaries[op->load.idx]);
break;
}
case MVMDispOpcodeUnboxStr: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_str);
temporaries[op->load.temp] = temp;
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_unbox_s,
temporaries[op->load.temp], temporaries[op->load.idx]);
break;
}
case MVMDispOpcodeLoadHOW: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_obj);
temporaries[op->load.temp] = temp;
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_gethow,
temporaries[op->load.temp], temporaries[op->load.idx]);
break;
}
case MVMDispOpcodeLookup: {
MVMSpeshOperand temp = MVM_spesh_manipulate_get_temp_reg(tc, g, MVM_reg_str);
MVM_VECTOR_PUSH(allocated_temps, temp);
emit_tri_op(tc, g, bb, &insert_after, MVM_OP_atkey_o, temp,
temporaries[op->load.temp], temporaries[op->load.idx]);
temporaries[op->load.temp] = temp;
break;
}
case MVMDispOpcodeSet:
/* Don't need to turn this into a set for now at least, since
* dispatch programs currently have temporaries as write only. */
temporaries[op->load.temp] = temporaries[op->load.idx];
break;
case MVMDispOpcodeResultValueObj:
/* Emit instruction according to result type. */
switch (ins->info->opcode) {
case MVM_OP_dispatch_v:
break;
case MVM_OP_dispatch_o:
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_set, ins->operands[0],
temporaries[op->res_value.temp]);
MVM_spesh_copy_facts(tc, g, ins->operands[0],
temporaries[op->res_value.temp]);
break;
case MVM_OP_dispatch_i:
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_unbox_i, ins->operands[0],
temporaries[op->res_value.temp]);
break;
case MVM_OP_dispatch_n:
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_unbox_n, ins->operands[0],
temporaries[op->res_value.temp]);
break;
case MVM_OP_dispatch_s:
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_unbox_s, ins->operands[0],
temporaries[op->res_value.temp]);
break;
default:
MVM_oops(tc, "Unexpected dispatch op when translating object result");
}
break;
case MVMDispOpcodeResultValueStr:
switch (ins->info->opcode) {
case MVM_OP_dispatch_v:
break;
case MVM_OP_dispatch_o: {
MVMObject *box_type = g->sf->body.cu->body.hll_config->str_box_type;
MVMSpeshOperand type_temp = MVM_spesh_manipulate_get_temp_reg(tc,
g, MVM_reg_obj);
MVM_VECTOR_PUSH(allocated_temps, type_temp);
emit_load_spesh_slot(tc, g, bb, &insert_after, type_temp,
(MVMCollectable *)box_type);
emit_tri_op(tc, g, bb, &insert_after, MVM_OP_box_s, ins->operands[0],
temporaries[op->res_value.temp], type_temp);
break;
}
case MVM_OP_dispatch_i:
emit_bi_op(tc, g, bb, &insert_after, MVM_OP_coerce_si, ins->operands[0],