/
QASTCompilerMAST.nqp
2207 lines (2040 loc) · 87.5 KB
/
QASTCompilerMAST.nqp
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
# Per-compilation instance of the MAST compiler, holding state and driving the
# compilation.
my class MASTCompilerInstance {
# The HLL that we're compiling.
has $!hll;
# The MAST compilation unit we're compiling it into.
has $!mast_compunit;
# MAST frames lookup hash.
has %!mast_frames;
# The filename we're compiling.
has $!file;
# The serialization context of the code we're compiling, if we have one.
has $!sc;
# The most recent op we tried to compile, for error reporting.
has $!last_op;
# This uses a very simple scheme. Write registers are assumed
# to be write-once, read-once. Therefore, if a QAST control
# structure wants to reuse the intermediate result of an
# expression, it must `set` the result to other registers before
# using the result as an arg to another op.
my class RegAlloc {
has $!frame;
has @!objs;
has @!int64s;
has @!int32s;
has @!int16s;
has @!int8s;
has @!num64s;
has @!num32s;
has @!strs;
has @!uint64s;
has @!uint32s;
has @!uint16s;
has @!uint8s;
has %!released_indexes;
method new($frame) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RegAlloc, '$!frame', $frame);
nqp::bindattr($obj, RegAlloc, '@!objs', []);
nqp::bindattr($obj, RegAlloc, '@!int64s', []);
nqp::bindattr($obj, RegAlloc, '@!int32s', []);
nqp::bindattr($obj, RegAlloc, '@!int16s', []);
nqp::bindattr($obj, RegAlloc, '@!int8s', []);
nqp::bindattr($obj, RegAlloc, '@!num64s', []);
nqp::bindattr($obj, RegAlloc, '@!num32s', []);
nqp::bindattr($obj, RegAlloc, '@!strs', []);
nqp::bindattr($obj, RegAlloc, '@!uint64s', []);
nqp::bindattr($obj, RegAlloc, '@!uint32s', []);
nqp::bindattr($obj, RegAlloc, '@!uint16s', []);
nqp::bindattr($obj, RegAlloc, '@!uint8s', []);
nqp::bindattr($obj, RegAlloc, '%!released_indexes', {});
$obj
}
method fresh_i() { self.fresh_register($MVM_reg_int64) }
method fresh_n() { self.fresh_register($MVM_reg_num64) }
method fresh_s() { self.fresh_register($MVM_reg_str) }
method fresh_o() { self.fresh_register($MVM_reg_obj) }
# QAST::Vars need entirely new MAST::Locals all to themselves,
# so a Local can't be a non-Var for the first half of a block and
# then a Var the second half, but then control returns to the first half
method fresh_register($kind, $new = 0) {
my @arr; my $type;
# set $new to 1 here if you suspect a problem with the allocator,
# or if you suspect a register is being double-released somewhere.
# $new := 1;
if $kind == $MVM_reg_int64 { @arr := @!int64s; $type := int }
elsif $kind == $MVM_reg_num64 { @arr := @!num64s; $type := num }
elsif $kind == $MVM_reg_str { @arr := @!strs; $type := str }
elsif $kind == $MVM_reg_obj { @arr := @!objs; $type := NQPMu }
elsif $kind == $MVM_reg_int32 { @arr := @!int32s; $type := int32 }
elsif $kind == $MVM_reg_int16 { @arr := @!int16s; $type := int16 }
elsif $kind == $MVM_reg_int8 { @arr := @!int8s; $type := int8 }
elsif $kind == $MVM_reg_num32 { @arr := @!num32s; $type := num32 }
elsif $kind == $MVM_reg_uint64 { @arr := @!uint64s; $type := uint64 }
elsif $kind == $MVM_reg_uint32 { @arr := @!uint32s; $type := uint32 }
elsif $kind == $MVM_reg_uint16 { @arr := @!uint16s; $type := uint16 }
elsif $kind == $MVM_reg_uint8 { @arr := @!uint8s; $type := uint8 }
else { nqp::die("unhandled reg kind $kind") }
my $reg;
if nqp::elems(@arr) && !$new {
$reg := nqp::pop(@arr);
nqp::deletekey(%!released_indexes, $reg.index);
}
else {
$reg := MAST::Local.new(:index($!frame.add_local($type)));
}
$reg
}
method release_i($reg) { self.release_register($reg, $MVM_reg_int64) }
method release_n($reg) { self.release_register($reg, $MVM_reg_num64) }
method release_s($reg) { self.release_register($reg, $MVM_reg_str) }
method release_o($reg) { self.release_register($reg, $MVM_reg_obj) }
method release_register($reg, $kind, $force = 0) {
return 1 if $kind == $MVM_reg_void || !$force && $*BLOCK.is_var($reg)
|| nqp::existskey(%!released_indexes, $reg.index);
%!released_indexes{$reg.index} := 1;
return nqp::push(@!int64s, $reg) if $kind == $MVM_reg_int64;
return nqp::push(@!num64s, $reg) if $kind == $MVM_reg_num64;
return nqp::push(@!strs, $reg) if $kind == $MVM_reg_str;
return nqp::push(@!objs, $reg) if $kind == $MVM_reg_obj;
return nqp::push(@!int32s, $reg) if $kind == $MVM_reg_int32;
return nqp::push(@!int16s, $reg) if $kind == $MVM_reg_int16;
return nqp::push(@!int8s, $reg) if $kind == $MVM_reg_int8;
return nqp::push(@!num32s, $reg) if $kind == $MVM_reg_num32;
return nqp::push(@!uint64s, $reg) if $kind == $MVM_reg_uint64;
return nqp::push(@!uint32s, $reg) if $kind == $MVM_reg_uint32;
return nqp::push(@!uint16s, $reg) if $kind == $MVM_reg_uint16;
return nqp::push(@!uint8s, $reg) if $kind == $MVM_reg_uint8;
nqp::die("unhandled reg kind $kind");
}
}
# Holds information about the QAST::Block we're currently compiling.
my class BlockInfo {
has $!qast; # The QAST::Block
has $!outer; # Outer block's BlockInfo
has %!local_names_by_index; # Locals' names by their indexes
has %!locals; # Mapping of local names to locals
has %!local_kinds; # Mapping of local registers to kinds
has %!localrefs; # Mapping of localref names to locals
has %!localref_kinds; # Mapping of localref registers to kinds
has %!lexicals; # Mapping of lexical names to lexicals
has %!lexical_kinds; # Mapping of lexical names to kinds
has %!lexical_params; # Mapping of lexical param names to their initial result reg
has %!lexicalrefs; # Mapping of lexical reference names to lexicals
has %!lexicalref_kinds; # Mapping of lexical reference names to kinds
has int $!param_idx; # Current lexical parameter index
has $!compiler; # The QAST::MASTCompiler
has @!params; # List of QAST::Var param nodes
has $!return_kind; # Kind of return, tracked while emitting
has @!captured_inners; # List of CUIDs of blocks we statically capture
has %!cloned_inners; # Mapping of CUIDs of blocks we clone to register with the clone
has @!contvar_locals; # Locals with a contvar that needs allocating.
method new($qast, $outer, $compiler) {
my $obj := nqp::create(self);
$obj.BUILD($qast, $outer, $compiler);
$obj
}
method BUILD($qast, $outer, $compiler) {
$!qast := $qast;
$!outer := $outer;
$!compiler := $compiler;
%!local_names_by_index := nqp::hash();
%!locals := nqp::hash();
%!local_kinds := nqp::hash();
%!localrefs := nqp::hash();
%!localref_kinds := nqp::hash();
%!lexicals := nqp::hash();
%!lexical_kinds := nqp::hash();
%!lexical_params := nqp::hash();
%!lexicalrefs := nqp::hash();
%!lexicalref_kinds := nqp::hash();
@!params := nqp::list();
@!captured_inners := nqp::list();
%!cloned_inners := nqp::hash();
@!contvar_locals := nqp::list();
}
method add_param($var) {
@!params[+@!params] := $var;
if $var.scope eq 'local' {
self.register_local($var);
}
else {
my $res_kind := self.add_lexical($var);
my $res_reg := $*REGALLOC.fresh_register($res_kind);
%!lexical_params{$var.name} := $res_reg;
[$res_kind, $res_reg]
}
}
method add_lexical($var, :$is_static, :$is_cont, :$is_state) {
my $mf := $*MAST_FRAME;
my $type := $var.returns;
my $kind := $!compiler.type_to_register_kind($type);
my $index := $mf.add_lexical($type, $var.name);
self.register_lexical($var.name, $index, $kind);
if $is_static || $is_cont || $is_state {
my int $flags := $is_static ?? 0 !!
$is_cont ?? 1 !! 2;
my $val := $var.value;
my $sc := nqp::getobjsc($val);
if nqp::isnull($sc) {
nqp::die("Object of type " ~ $val.HOW.name($val) ~
" in QAST::Var value, but not in SC");
}
my int $idx := nqp::scgetobjidx($sc, $val);
my int $sc_idx := $!compiler.mast_compunit.sc_idx($sc);
$mf.add_static_lex_value($index, $flags, $sc_idx, $idx);
}
$kind;
}
method register_lexical(str $name, $index, $kind) {
if nqp::existskey(%!lexicals, $name) || nqp::existskey(%!lexicalrefs, $name) {
nqp::die("Lexical '$name' already declared");
}
my $lex := MAST::Lexical.new( :index($index), :frames_out(0) );
%!lexicals{$name} := $lex;
%!lexical_kinds{$name} := $kind;
$lex;
}
method add_lexicalref($var) {
my $mf := $*MAST_FRAME;
my $type := $var.returns;
my $kind := $!compiler.type_to_register_kind($type);
unless nqp::objprimspec($type) {
nqp::die("lexicalref declaration must have a native type");
}
my $index := $mf.add_lexical(NQPMu, $var.name);
self.register_lexicalref($var.name, $index, $kind);
$MVM_reg_obj
}
method register_lexicalref(str $name, $index, $kind) {
if nqp::existskey(%!lexicals, $name) || nqp::existskey(%!lexicalrefs, $name) {
nqp::die("Lexical '$name' already declared");
}
my $lex := MAST::Lexical.new( :index($index), :frames_out(0) );
%!lexicalrefs{$name} := $lex;
%!lexicalref_kinds{$name} := $kind;
$lex;
}
method register_local($var, :$is_cont) {
my $name := $var.name;
my $temporary := ?$*INSTMT;
if nqp::existskey(%!locals, $name) || nqp::existskey(%!localrefs, $name) ||
$temporary && nqp::existskey(%*STMTTEMPS, $name) {
nqp::die("Local (or localref) '$name' already declared");
}
my $kind := $!compiler.type_to_register_kind($var.returns);
%!local_kinds{$name} := $kind;
# pass a 1 meaning get a Totally New MAST::Local
my $local := $*REGALLOC.fresh_register($kind, !$temporary);
%!locals{$name} := $local;
%!local_names_by_index{$local.index} := $name;
if $temporary {
%*STMTTEMPS{$name} := $local;
}
if $is_cont {
nqp::push(@!contvar_locals, $var);
}
$local;
}
method register_localref($var) {
my $name := $var.name;
my $temporary := ?$*INSTMT;
if nqp::existskey(%!localrefs, $name) || nqp::existskey(%!locals, $name) ||
$temporary && nqp::existskey(%*STMTTEMPS, $name) {
nqp::die("Localref (or local) '$name' already declared");
}
my $kind := $!compiler.type_to_register_kind($var.returns);
%!localref_kinds{$name} := $kind;
# pass a 1 meaning get a Totally New MAST::Local
my $localref := $*REGALLOC.fresh_register($MVM_reg_obj, !$temporary);
%!localrefs{$name} := $localref;
%!local_names_by_index{$localref.index} := $name;
if $temporary {
%*STMTTEMPS{$name} := $localref;
}
$localref;
}
# returns whether a MAST::Local is a variable in this block
method is_var($local) {
nqp::existskey(%!local_names_by_index, $local.index)
}
method return_kind(*@value) {
if @value {
nqp::die("inconsistent immediate block return type")
if ($!qast.blocktype eq 'immediate' || $!qast.blocktype eq 'immediate_static') &&
nqp::defined($!return_kind) && @value[0] != $!return_kind;
$!return_kind := @value[0];
}
$!return_kind
}
method release_temp($name) {
my $local := %!locals{$name};
my $index := $local.index();
my $kind := %!local_kinds{$name};
$*REGALLOC.release_register($local, $kind, 1);
nqp::deletekey(%!local_names_by_index, $index);
nqp::deletekey(%!locals, $name);
nqp::deletekey(%!local_kinds, $name);
}
method qast() { $!qast }
method outer() { $!outer }
method lexical($name) { %!lexicals{$name} }
method lexicals() { %!lexicals }
method lexicalref($name) { %!lexicalrefs{$name} }
method lexicalrefs() { %!lexicalrefs }
method local($name) { %!locals{$name} }
method local_kind($name) { %!local_kinds{$name} }
method localref($name) { %!localrefs{$name} }
method localref_kind($name) { %!localref_kinds{$name} }
method lexical_kind($name) { %!lexical_kinds{$name} }
method lexical_kinds() { %!lexical_kinds }
method lexicalref_kind($name) { %!lexicalref_kinds{$name} }
method lexicalref_kinds() { %!lexicalref_kinds }
method params() { @!params }
method lexical_param($name) { %!lexical_params{$name} }
method resolve_lexical($name) {
my $block := self;
my $out := 0;
while $block {
my $lex := ($block.lexicals()){$name};
return MAST::Lexical.new( :index($lex.index), :frames_out($out) ) if $lex;
$out++;
$block := $block.outer;
}
nqp::die("could not resolve lexical $name");
}
method capture_inner($block) {
nqp::push(@!captured_inners, $block.cuid)
}
method clone_inner($block) {
my $cuid := $block.cuid;
my $already := %!cloned_inners{$cuid};
if $already {
$already
}
else {
my $reg := $*REGALLOC.fresh_register($MVM_reg_obj, 1);
%!cloned_inners{$cuid} := $reg;
%!local_names_by_index{$reg.index} := $cuid;
$reg
}
}
method captured_inners() { @!captured_inners }
method cloned_inners() { %!cloned_inners }
method contvar_locals() { @!contvar_locals }
}
method source_for_node($node) {
my $source := $node.node
?? ~ nqp::escape($node.node.Str)
!! '';
if nqp::chars($source) > 103 {
$source := nqp::substr($source, 0, 100) ~ '...';
}
if nqp::chars($source) {
$source := qq[ (source text: "$source")];
}
$source;
}
my int $serno := 0;
method unique($prefix = '') { $prefix ~ $serno++ }
method mast_compunit() { $!mast_compunit }
method mast_frames() { %!mast_frames }
method sc() { $!sc }
method to_mast($qast, %mast_frames = nqp::hash()) {
# Set up compilation state.
$!hll := '';
$!mast_compunit := MAST::CompUnit.new();
%!mast_frames := %mast_frames;
$!file := nqp::ifnull(nqp::getlexdyn('$?FILES'), "<unknown file>");
$!sc := NQPMu;
# Compile, and evaluate to compilation unit.
self.as_mast($qast);
#CATCH {
# my $err := $!;
# my $source := self.source_for_node($!last_op);
# nqp::die("QAST -> MAST failed while compiling op " ~ $!last_op.op ~ "$source: $err");
#}
$!mast_compunit
}
method coerce($res, $desired) {
my $got := $res.result_kind;
if $got != $desired {
$res.append(self.coercion($res, $desired));
}
$res
}
# Expects that the value in need of coercing has already been
# obtained. Produces instructions to coerce it.
method coercion($res, $desired) {
my $il := nqp::list();
my $got := $res.result_kind;
my $reg := $res.result_reg;
if $got == $desired {
# Nothing to do.
}
elsif $desired == $MVM_reg_void {
$reg := MAST::VOID;
}
elsif $desired == $MVM_reg_obj {
# See if we already have full-width native.
if $got == $MVM_reg_int64 || $got == $MVM_reg_num64 ||
$got == $MVM_reg_str || $got == $MVM_reg_void ||
$got == $MVM_reg_uint64 {
return QAST::MASTOperations.box(self, $!hll, $got, $reg);
}
elsif $got == $MVM_reg_num32 {
my $grow := self.coercion($res, $MVM_reg_num64);
my $box := QAST::MASTOperations.box(self, $!hll, $MVM_reg_num64,
$grow.result_reg);
$il := $grow.instructions;
push_ilist($il, $box);
$reg := $box.result_reg;
}
elsif $got == $MVM_reg_int32 || $got == $MVM_reg_int16 || $got == $MVM_reg_int8 {
my $grow := self.coercion($res, $MVM_reg_int64);
my $box := QAST::MASTOperations.box(self, $!hll, $MVM_reg_int64,
$grow.result_reg);
$il := $grow.instructions;
push_ilist($il, $box);
$reg := $box.result_reg;
}
elsif $got == $MVM_reg_uint32 || $got == $MVM_reg_uint16 || $got == $MVM_reg_uint8 {
my $grow := self.coercion($res, $MVM_reg_uint64);
my $box := QAST::MASTOperations.box(self, $!hll, $MVM_reg_uint64,
$grow.result_reg);
$il := $grow.instructions;
push_ilist($il, $box);
$reg := $box.result_reg;
}
else {
nqp::die("Unknown boxing case; got: " ~ $got);
}
}
elsif $got == $MVM_reg_obj {
# See if we want a full-width native.
if $desired == $MVM_reg_int64 || $desired == $MVM_reg_num64 ||
$desired == $MVM_reg_str || $desired == $MVM_reg_uint64 {
return QAST::MASTOperations.unbox(self, $!hll, $desired, $reg);
}
elsif $desired == $MVM_reg_num32 {
my $unbox := QAST::MASTOperations.unbox(self, $!hll, $MVM_reg_num64, $reg);
my $shrink := self.coercion($unbox, $desired);
$il := $unbox.instructions;
push_ilist($il, $shrink);
$reg := $shrink.result_reg;
}
elsif $desired == $MVM_reg_int32 || $desired == $MVM_reg_int16 || $desired == $MVM_reg_int8 {
my $unbox := QAST::MASTOperations.unbox(self, $!hll, $MVM_reg_int64, $reg);
my $shrink := self.coercion($unbox, $desired);
$il := $unbox.instructions;
push_ilist($il, $shrink);
$reg := $shrink.result_reg;
}
elsif $desired == $MVM_reg_uint32 || $desired == $MVM_reg_uint16 || $desired == $MVM_reg_uint8 {
my $unbox := QAST::MASTOperations.unbox(self, $!hll, $MVM_reg_uint64, $reg);
my $shrink := self.coercion($unbox, $desired);
$il := $unbox.instructions;
push_ilist($il, $shrink);
$reg := $shrink.result_reg;
}
else {
nqp::die("Unknown unboxing case; desired: " ~ $desired);
}
}
else {
my $res_reg := $*REGALLOC.fresh_register($desired);
my $release_type := $got;
if $desired == $MVM_reg_int64 {
if $got == $MVM_reg_num64 {
push_op($il, 'coerce_ni', $res_reg, $reg);
}
elsif $got == $MVM_reg_str {
push_op($il, 'coerce_si', $res_reg, $reg);
}
elsif $got == $MVM_reg_void {
push_op($il, 'const_i64', $res_reg, MAST::IVal.new( :value(0) ));
}
elsif $got == $MVM_reg_int32 {
push_op($il, 'extend_i32', $res_reg, $reg);
}
elsif $got == $MVM_reg_int16 {
push_op($il, 'extend_i16', $res_reg, $reg);
}
elsif $got == $MVM_reg_int8 {
push_op($il, 'extend_i8', $res_reg, $reg);
}
elsif $got == $MVM_reg_uint64 {
push_op($il, 'coerce_ui', $res_reg, $reg);
}
elsif $got == $MVM_reg_uint32 || $got == $MVM_reg_uint16 || $got == $MVM_reg_uint8 {
my $uint64 := self.coercion($res, $MVM_reg_uint64);
$il := $uint64.instructions;
$reg := $uint64.result_reg;
$release_type := $uint64.result_kind;
push_op($il, 'coerce_ui', $res_reg, $reg);
}
else {
nqp::die("Unknown coercion case for int; got: "~$got);
}
}
elsif $desired == $MVM_reg_num64 {
if $got == $MVM_reg_int64 {
push_op($il, 'coerce_in', $res_reg, $reg);
}
elsif $got == $MVM_reg_str {
push_op($il, 'coerce_sn', $res_reg, $reg);
}
elsif $got == $MVM_reg_num32 {
push_op($il, 'extend_n32', $res_reg, $reg);
}
elsif $got == $MVM_reg_void {
push_op($il, 'const_n64', $res_reg, MAST::NVal.new( :value(0) ));
}
else {
nqp::die("Unknown coercion case for num; got: "~$got);
}
}
elsif $desired == $MVM_reg_str {
if $got == $MVM_reg_int64 {
push_op($il, 'coerce_is', $res_reg, $reg);
}
elsif $got == $MVM_reg_num64 {
push_op($il, 'coerce_ns', $res_reg, $reg);
}
elsif $got == $MVM_reg_void {
push_op($il, 'const_s', $res_reg, MAST::SVal.new( :value('') ));
}
else {
nqp::die("Unknown coercion case for str; got: "~$got);
}
}
elsif $desired == $MVM_reg_num32 {
if $got == $MVM_reg_num64 {
push_op($il, 'trunc_n32', $res_reg, $reg);
}
else {
nqp::die("Unknown coercion case for num32; got: "~$got);
}
}
elsif $desired == $MVM_reg_int32 {
if $got == $MVM_reg_int64 {
push_op($il, 'trunc_i32', $res_reg, $reg);
}
else {
nqp::die("Unknown coercion case for int32; got: " ~ $got);
}
}
elsif $desired == $MVM_reg_int16 {
if $got == $MVM_reg_int64 {
push_op($il, 'trunc_i16', $res_reg, $reg);
}
else {
nqp::die("Unknown coercion case for int16; got: " ~ $got);
}
}
elsif $desired == $MVM_reg_int8 {
if $got == $MVM_reg_int64 {
push_op($il, 'trunc_i8', $res_reg, $reg);
}
else {
nqp::die("Unknown coercion case for int8; got: " ~ $got);
}
}
elsif $desired == $MVM_reg_uint64 {
if $got == $MVM_reg_uint32 {
push_op($il, 'extend_u32', $res_reg, $reg);
}
elsif $got == $MVM_reg_uint16 {
push_op($il, 'extend_u16', $res_reg, $reg);
}
elsif $got == $MVM_reg_uint8 {
push_op($il, 'extend_u8', $res_reg, $reg);
}
else {
unless $got == $MVM_reg_int64 {
my $int64 := self.coercion($res, $MVM_reg_int64);
$il := $int64.instructions;
$reg := $int64.result_reg;
$release_type := $int64.result_kind;
}
push_op($il, 'coerce_iu', $res_reg, $reg);
}
}
elsif $desired == $MVM_reg_uint32 {
unless $got == $MVM_reg_uint64 {
my $uint64 := self.coercion($res, $MVM_reg_uint64);
$il := $uint64.instructions;
$reg := $uint64.result_reg;
$release_type := $uint64.result_kind;
}
push_op($il, 'trunc_u32', $res_reg, $reg);
}
elsif $desired == $MVM_reg_uint16 {
unless $got == $MVM_reg_uint64 {
my $uint64 := self.coercion($res, $MVM_reg_uint64);
$il := $uint64.instructions;
$reg := $uint64.result_reg;
$release_type := $uint64.result_kind;
}
push_op($il, 'trunc_u16', $res_reg, $reg);
}
elsif $desired == $MVM_reg_uint8 {
unless $got == $MVM_reg_uint64 {
my $uint64 := self.coercion($res, $MVM_reg_uint64);
$il := $uint64.instructions;
$reg := $uint64.result_reg;
$release_type := $uint64.result_kind;
}
push_op($il, 'trunc_u8', $res_reg, $reg);
}
else {
nqp::die("Coercion from type '$got' to '$desired' NYI");
}
$*REGALLOC.release_register($reg, $release_type);
$reg := $res_reg;
}
MAST::InstructionList.new($il, $reg, $desired)
}
method as_mast($qast, :$want) {
my $*WANT;
if nqp::defined($want) {
$*WANT := $want;
if nqp::istype($qast, QAST::Want) {
self.coerce(self.compile_node(want($qast, $want), :$want), $want)
}
else {
self.coerce(self.compile_node($qast, :$want), $want)
}
}
else {
self.compile_node($qast)
}
}
sub want($node, $type) {
my @possibles := $node.list;
my $best := nqp::atpos(@possibles, 0);
if $type != $MVM_reg_obj {
my $char := $type == $MVM_reg_void ?? 'v' !!
$type == $MVM_reg_int64 ?? 'I' !!
$type == $MVM_reg_num64 ?? 'N' !!
$type == $MVM_reg_str ?? 'S' !!
'X';
my int $i := 1;
my int $n := nqp::elems(@possibles);
while $i < $n {
if nqp::index(nqp::atpos(@possibles, $i), $char) >= 0 {
$best := nqp::atpos(@possibles, $i + 1);
last;
}
$i := $i + 2;
}
}
$best
}
my @return_opnames := [
'return',
'return_i',
'return_i',
'return_i',
'return_i',
'return_n',
'return_n',
'return_s',
'return_o'
];
my @type_initials := [
'', 'i', 'i', 'i', 'i', 'n', 'n', 's', 'o'
];
my @attr_opnames := [
'',
'attr_i',
'attr_i',
'attr_i',
'attr_i',
'attr_n',
'attr_n',
'attr_s',
'attr_o'
];
my @attrref_opnames := [
'',
'getattrref_i',
'getattrref_i',
'getattrref_i',
'getattrref_i',
'getattrref_n',
'getattrref_n',
'getattrref_s',
'', '', '', '', '', '', '', '', '',
# XXX Want a getattrref_u in the end
'getattrref_i',
'getattrref_i',
'getattrref_i',
'getattrref_i'
];
my @kind_to_op_slot := [
0, 0, 0, 0, 0, 1, 1, 2, 3, -1, -1, -1, -1, -1, -1, -1, -1, 4, 4, 4, 4
];
my @param_opnames := [
'param_rp_i',
'param_rp_n',
'param_rp_s',
'param_rp_o',
'param_rp_u',
'param_op_i',
'param_op_n',
'param_op_s',
'param_op_o',
'param_op_u',
'param_rn_i',
'param_rn_n',
'param_rn_s',
'param_rn_o',
'param_rn_u',
'param_on_i',
'param_on_n',
'param_on_s',
'param_on_o',
'param_on_u',
'param_rn2_i',
'param_rn2_n',
'param_rn2_s',
'param_rn2_o',
'param_rn2_u',
'param_on2_i',
'param_on2_n',
'param_on2_s',
'param_on2_o',
'param_on2_u'
];
my @return_types := [ NQPMu, int, int, int, int, num, num, str, NQPMu ];
method register_kind_to_type($kind) { @return_types[$kind] }
proto method compile_node($node, :$want) { * }
multi method compile_node(QAST::CompUnit $cu, :$want) {
# Should have a single child which is the outer block.
if +@($cu) != 1 || !nqp::istype($cu[0], QAST::Block) {
nqp::die("QAST::CompUnit should have one child that is a QAST::Block");
}
# Set HLL and serialization context.
if $cu.hll {
$!hll := $cu.hll;
$!mast_compunit.hll($!hll);
}
if $cu.sc {
$!sc := $cu.sc;
}
# Blocks we've seen while compiling.
my %*BLOCKS_DONE;
# Compile the block; make sure $*BLOCK is clear.
my $*BLOCK;
self.as_mast($cu[0]);
# If we are in compilation mode, or have pre-deserialization or
# post-deserialization tasks, handle those. Overall, the process
# is to desugar this into simpler QAST nodes, then compile those.
my $is_nested := $cu.is_nested;
my $comp_mode := $cu.compilation_mode;
my @pre_des := $cu.pre_deserialize;
my @post_des := $cu.post_deserialize;
if $comp_mode || @pre_des || @post_des {
# Create a block into which we'll install all of the other
# pieces.
my $block := QAST::Block.new( :blocktype('raw'), :name('<dependencies+deserialize>') );
# Add pre-deserialization tasks, each as a QAST::Stmt.
for @pre_des {
$block.push(QAST::Stmt.new($_));
}
# If we need to do deserialization, emit code for that.
if $comp_mode && !$is_nested {
$block.push(self.deserialization_code($cu.sc(), $cu.code_ref_blocks(),
$cu.repo_conflict_resolver()));
}
# Provided we have a serialization context,, pop it off the
# compiling SC stack.
unless $is_nested {
nqp::popcompsc() if $cu.sc;
}
# Add post-deserialization tasks.
for @post_des {
$block.push(QAST::Stmt.new($_));
}
# Compile to MAST and register this block as the deserialization
# handler.
self.as_mast($block);
$!mast_compunit.deserialize_frame(%!mast_frames{$block.cuid});
}
# Compile and include load-time logic, if any.
if nqp::defined($cu.load) {
my $load_block := QAST::Block.new(
:blocktype('raw'), :name('<load>'),
$cu.load
);
self.as_mast($load_block);
$!mast_compunit.load_frame(%!mast_frames{$load_block.cuid});
}
# Compile and include main-time logic, if any, and wrap it up so that we
# pass command line arguments.
if nqp::defined($cu.main) {
my $main_block := QAST::Block.new(
:blocktype('raw'), :name('<entry>'),
QAST::Op.new(
:op('call'),
QAST::Block.new(
:blocktype('declaration'), :name('<main>'),
$cu.main
),
QAST::VM.new( :moarop('clargs'), :flat(1) )
));
self.as_mast($main_block);
$!mast_compunit.main_frame(%!mast_frames{$main_block.cuid});
}
}
# This method is a hook point so that we can override serialization when cross-compiling
method serialize_sc($sc) {
my $sh := nqp::list_s();
my str $serialized := nqp::serialize($sc, $sh);
[$serialized, nqp::null()];
}
method deserialization_code($sc, @code_ref_blocks, $repo_conf_res) {
my $sc_tuple := self.serialize_sc($sc);
my str $serialized := $sc_tuple[0];
my $sh := $sc_tuple[1];
# String heap QAST.
my $sh_ast;
if nqp::islist($sh) {
$sh_ast := QAST::Op.new( :op('list_s') );
my $sh_elems := nqp::elems($sh);
my $i := 0;
while $i < $sh_elems {
$sh_ast.push(nqp::isnull_s(nqp::atpos_s($sh, $i))
?? QAST::Op.new( :op('null_s') )
!! QAST::SVal.new( :value(nqp::atpos_s($sh, $i)) ));
$i := $i + 1;
}
}
else {
$sh_ast := QAST::Op.new( :op('null') );
}
# Code references.
my $cr_ast := QAST::Op.new( :op('list_b'), |@code_ref_blocks );
# Handle repossession conflict resolution code, if any.
if $repo_conf_res {
$repo_conf_res.push(QAST::Var.new( :name('conflicts'), :scope('local') ));
}
else {
$repo_conf_res := QAST::Op.new(
:op('die_s'),
QAST::SVal.new( :value('Repossession conflicts occurred during deserialization') )
);
}
# Overall deserialization QAST.
QAST::Stmts.new(
QAST::Op.new(
:op('bind'),
QAST::Var.new( :name('cur_sc'), :scope('local'), :decl('var') ),
QAST::Op.new( :op('createsc'), QAST::SVal.new( :value(nqp::scgethandle($sc)) ) )
),
QAST::Op.new(
:op('scsetdesc'),
QAST::Var.new( :name('cur_sc'), :scope('local') ),
QAST::SVal.new( :value(nqp::scgetdesc($sc)) )
),
QAST::Op.new(
:op('bind'),
QAST::Var.new( :name('conflicts'), :scope('local'), :decl('var') ),
QAST::Op.new( :op('list') )
),
QAST::Op.new(
:op('deserialize'),
nqp::isnull_s($serialized)
?? QAST::Op.new( :op('null_s') )
!! QAST::SVal.new( :value($serialized) ),
QAST::Var.new( :name('cur_sc'), :scope('local') ),
$sh_ast, # XXX I had to leave this in, otherwise the JS backend doesn't build.
# I would love some help figuring out why!
QAST::Block.new( :blocktype('immediate'), $cr_ast ),
QAST::Var.new( :name('conflicts'), :scope('local') )
),
QAST::Op.new(
:op('if'),
QAST::Op.new(
:op('elems'),
QAST::Var.new( :name('conflicts'), :scope('local') )
),
$repo_conf_res
)
)
}
multi method compile_node(QAST::Block $node, :$want) {
my $cuid := $node.cuid();
my $block;
my $outer;
if %*BLOCKS_DONE{$cuid} -> @already {
$block := @already[0];
$outer := @already[1];
}
else {
my $outer_frame := try $*MAST_FRAME;
# Create an empty frame and add it to the compilation unit.
my $frame := MAST::Frame.new(
:name($node.name),
:cuuid($cuid));
$!mast_compunit.add_frame($frame);
try $outer := $*BLOCK;
$block := BlockInfo.new($node, (nqp::defined($outer) ?? $outer !! NQPMu), self);
%*BLOCKS_DONE{$cuid} := [$block, $outer];
# stash the frame by the block's cuid so other references
# by this block can find it.
%!mast_frames{$cuid} := $frame;
# Set the outer if it exists
$frame.set_outer($outer_frame)
if $outer_frame && $outer_frame ~~ MAST::Frame;
# Set exit handler and thunk flags if needed.
if $node.has_exit_handler {
$frame.has_exit_handler(1);
}
if $node.is_thunk {
$frame.is_thunk(1);
}
# Set code object, if any.
my $code_obj := $node.code_object;
if nqp::isconcrete($code_obj) {
my $sc := nqp::getobjsc($code_obj);
if nqp::isnull($sc) {
nqp::die("Object of type " ~ $code_obj.HOW.name($code_obj) ~
" is QAST::Block code object, but not in SC");
}
my int $idx := nqp::scgetobjidx($sc, $code_obj);
my int $sc_idx := $!mast_compunit.sc_idx($sc);
$frame.set_code_object_idxs($sc_idx, $idx);
}
# Compile all the substatements.
my $ins;
{
my $*BINDVAL := 0;
# Create a register allocator for this frame.
my $*REGALLOC := RegAlloc.new($frame);
# when we enter a QAST::Stmt, the contextual will be cloned, and the locals of
# newly declared QAST::Vars of local scope inside the Stmt will be stashed here,
# so they can be released at the end of the QAST::Stmt in which they were
# declared. Inability to declare duplicate names is still enfoced, and types are
# still enforced.
my %*STMTTEMPS := nqp::hash();