/
Compiler.pir
2027 lines (1656 loc) · 52.7 KB
/
Compiler.pir
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
=head1 NAME
PAST::Compiler - PAST Compiler
=head1 DESCRIPTION
PAST::Compiler implements a basic compiler for PAST nodes.
By default PAST::Compiler transforms a PAST tree into POST.
=head2 Signature Flags
Throughout the compiler PAST uses a number of 1-character
"flags" to indicate allowable register types and conversions.
This helps the compiler generate more efficient code and know
what sorts of conversions are allowed (or desired). The
basic flags are:
P,S,I,N PMC, string, int, or num register
s string register or constant
i int register or constant
n num register or constant
r any register result
v void (no result)
* any result type except void
+ PMC, int register, num register, or numeric constant
~ PMC, string register, or string constant
: argument (same as '*'), possibly with :named or :flat
These flags are used to describe signatures and desired return
types for various operations. For example, if an opcode is
specified with a signature of C<I~P*>, then the opcode places
its result in an int register, its first child is coerced into
some sort of string value, its second child is coerced into a
PMC register, and the third and subsequent children can return
any value type.
=cut
.include "cclass.pasm"
.include "except_types.pasm"
.include "interpinfo.pasm"
.namespace [ 'PAST::Compiler' ]
.sub 'onload' :anon :load :init
load_bytecode 'PCT/HLLCompiler.pbc'
.local pmc p6meta, cproto
p6meta = new 'P6metaclass'
cproto = p6meta.'new_class'('PAST::Compiler', 'parent'=>'PCT::HLLCompiler', 'attr'=>'%!symtable')
cproto.'language'('PAST')
$P1 = split ' ', 'post pir evalpmc'
cproto.'stages'($P1)
## %piropsig is a table of common opcode signatures
.local pmc piropsig
piropsig = new 'Hash'
piropsig['isa'] = 'IP~'
piropsig['isfalse'] = 'IP'
piropsig['issame'] = 'IPP'
piropsig['istrue'] = 'IP'
piropsig['n_abs'] = 'PP'
piropsig['n_add'] = 'PP+'
piropsig['n_band'] = 'PPP'
piropsig['n_bnot'] = 'PP'
piropsig['n_bor'] = 'PPP'
piropsig['n_concat'] = 'PP~'
piropsig['n_div'] = 'PP+'
piropsig['n_fdiv'] = 'PP+'
piropsig['n_mod'] = 'PP+'
piropsig['n_mul'] = 'PP+'
piropsig['n_neg'] = 'PP'
piropsig['n_not'] = 'PP'
piropsig['n_shl'] = 'PP+'
piropsig['n_shr'] = 'PP+'
piropsig['n_sub'] = 'PP+'
piropsig['pow'] = 'NN+'
piropsig['print'] = 'v*'
piropsig['set'] = 'PP'
set_global '%piropsig', piropsig
## %valflags specifies when PAST::Val nodes are allowed to
## be used as a constant. The 'e' flag indicates that the
## value must be quoted+escaped in PIR code.
.local pmc valflags
valflags = new 'Hash'
valflags['String'] = 's~*:e'
valflags['Integer'] = 'i+*:'
valflags['Float'] = 'n+*:'
set_global '%valflags', valflags
$P0 = new 'CodeString'
set_global '%!codestring', $P0
$P0 = new 'Integer'
$P0 = 11
set_global '$!serno', $P0
.return ()
.end
=head2 Compiler methods
=over 4
=item to_post(node [, 'option'=>option, ...])
Compile the abstract syntax tree given by C<past> into POST.
=cut
.sub 'to_post' :method
.param pmc past
.param pmc options :slurpy :named
.local pmc symtable
symtable = new 'Hash'
setattribute self, '%!symtable', symtable
.local pmc blockpast
blockpast = get_global '@?BLOCK'
unless null blockpast goto have_blockpast
blockpast = new 'ResizablePMCArray'
set_global '@?BLOCK', blockpast
have_blockpast:
null $P0
set_global '$?SUB', $P0 # see RT#49758
.return self.'as_post'(past, 'rtype'=>'v')
.end
=item escape(str)
Return C<str> as a PIR constant string.
=cut
.sub 'escape' :method
.param string str
$P0 = get_global '%!codestring'
str = $P0.'escape'(str)
.return (str)
.end
=item unique([STR fmt])
Generate a unique number that can be used as an identifier.
If C<fmt> is provided, then it will be used as a prefix to the
unique number.
=cut
.sub 'unique' :method
.param string fmt :optional
.param int has_fmt :opt_flag
if has_fmt goto unique_1
fmt = ''
unique_1:
$P0 = get_global '$!serno'
$S0 = $P0
$S0 = concat fmt, $S0
inc $P0
.return ($S0)
.end
=item uniquereg(rtype)
Generate a unique register based on C<rtype>, where C<rtype>
is one of the signature flags described above.
=cut
.sub 'uniquereg' :method
.param string rtype
unless rtype goto err_nortype
if rtype == 'v' goto reg_void
.local string reg
reg = 'P'
$I0 = index 'Ss~Nn+Ii', rtype
if $I0 < 0 goto reg_psin
reg = substr 'SSSNNNII', $I0, 1
reg_psin:
reg = concat '$', reg
.return self.'unique'(reg)
reg_void:
.return ('')
err_nortype:
self.panic('rtype not set')
.end
=item coerce(post, rtype)
Return a POST tree that coerces the result of C<post> to have a
return value compatible with C<rtype>. C<rtype> can also be
a specific register, in which case the result of C<post> is
forced into that register (with conversions as needed).
=cut
.sub 'coerce' :method
.param pmc post
.param string rtype
unless rtype goto err_nortype
.local string pmctype, result, rrtype
null pmctype
null result
## if rtype is a register, then set result and use the register
## type as rtype
$S0 = substr rtype, 0, 1
unless $S0 == '$' goto have_rtype
result = rtype
rtype = substr result, 1, 1
have_rtype:
## these rtypes allow any return value, so no coercion needed.
$I0 = index 'v*:', rtype
if $I0 >= 0 goto end
## figure out what type of result we already have
.local string source
source = post.'result'()
$S0 = substr source, 0, 1
if $S0 == '$' goto source_reg
if $S0 == '"' goto source_str
if $S0 == '.' goto source_int_or_num
if $S0 == '-' goto source_int_or_num
$I0 = is_cclass .CCLASS_NUMERIC, source, 0
if $I0 goto source_int_or_num
$S0 = substr source, 0, 8
if $S0 == 'unicode:' goto source_str
## assume that whatever is left acts like a PMC
goto source_pmc
source_reg:
## source is some sort of register
## if a register is all we need, we're done
if rtype == 'r' goto end
$S0 = substr source, 1, 1
## if we have the correct register type already, we're done
if $S0 != rtype goto source_reg_1
unless result goto end
goto coerce_reg
source_reg_1:
$S0 = downcase $S0
if $S0 == rtype goto end
## figure it out based on the register type
if $S0 == 's' goto source_str
if rtype == '+' goto end
if $S0 == 'i' goto source_int
if $S0 == 'n' goto source_num
source_pmc:
$I0 = index 'SINsin', rtype
if $I0 < 0 goto end
goto coerce_reg
source_str:
if rtype == '~' goto end
if rtype == 's' goto end
rrtype = 'S'
pmctype = "'String'"
goto coerce_reg
source_int_or_num:
if rtype == '+' goto end
## existence of an 'e' or '.' implies num
$I0 = index source, '.'
if $I0 >= 0 goto source_num
$I0 = index source, 'E'
if $I0 >= 0 goto source_num
source_int:
if rtype == 'i' goto end
rrtype = 'I'
pmctype = "'Integer'"
goto coerce_reg
source_num:
if rtype == 'n' goto end
rrtype = 'N'
pmctype = "'Float'"
coerce_reg:
## okay, we know we have to do a coercion.
## If we just need the value in a register (rtype == 'r'),
## then create result based on the preferred register type (rrtype).
if rtype != 'r' goto coerce_reg_1
result = self.'uniquereg'(rrtype)
coerce_reg_1:
## if we haven't set the result target yet, then generate one
## based on rtype. (The case of rtype == 'r' was handled above.)
if result goto coerce_reg_2
result = self.'uniquereg'(rtype)
coerce_reg_2:
## create a new ops node to hold the coercion, put C<post> in it.
$P0 = get_hll_global ['POST'], 'Ops'
post = $P0.'new'(post, 'result'=>result)
## if we need a new pmc (rtype == 'P' && pmctype defined), create it
if rtype != 'P' goto have_result
unless pmctype goto have_result
post.'push_pirop'('new', result, pmctype)
have_result:
## store the value into the target register
post.'push_pirop'('set', result, source)
end:
.return (post)
err_nortype:
self.panic('rtype not set')
.end
=item post_children(node [, 'signature'=>signature] )
Return the POST representation of evaluating all of C<node>'s
children in sequence. The C<signature> option is a string of
flags as described in "Signature Flags" above. Since we're
just evaluating children nodes, the first character of
C<signature> (return value type) is ignored. Thus a C<signature>
of C<v~P*> says that the first child needs to be something
in string context, the second child should be a PMC, and the
third and subsequent children can be any value they wish.
=cut
.sub 'post_children' :method
.param pmc node
.param pmc options :slurpy :named
.local pmc ops
$P0 = get_hll_global ['POST'], 'Ops'
ops = $P0.'new'('node'=>node)
## get any conversion types
.local string signature
signature = options['signature']
if signature goto have_signature
signature = '**'
have_signature:
.local int sigmax, sigidx
sigmax = length signature
dec sigmax
## if the signature contains a ':', then we're doing
## flagged arguments (:flat, :named)
.local pmc posargs, namedargs
posargs = new 'ResizableStringArray'
null namedargs
$I0 = index signature, ':'
if $I0 < 0 goto nocolon
namedargs = new 'ResizableStringArray'
nocolon:
.local pmc iter
.local string rtype
.local int sigidx
iter = node.'iterator'()
sigidx = 1
rtype = substr signature, sigidx, 1
iter_loop:
unless iter goto iter_end
.local pmc cpast, cpost
cpast = shift iter
cpost = self.'as_post'(cpast, 'rtype'=>rtype)
cpost = self.'coerce'(cpost, rtype)
ops.'push'(cpost)
.local pmc isflat
isflat = cpast.'flat'()
if rtype != ':' goto iter_pos
.local pmc npast, npost
npast = cpast.'named'()
unless npast goto iter_pos
$S0 = cpost
if isflat goto flat_named
npost = self.'as_post'(npast, 'rtype'=>'~')
$S1 = npost
ops.'push'(npost)
concat $S0, ' :named('
concat $S0, $S1
concat $S0, ')'
goto named_done
flat_named:
concat $S0, ' :named :flat'
named_done:
push namedargs, $S0
goto iter_rtype
iter_pos:
if isflat goto flat_pos
push posargs, cpost
goto iter_rtype
flat_pos:
$S0 = cpost
concat $S0, ' :flat'
push posargs, $S0
iter_rtype:
unless sigidx < sigmax goto iter_loop
inc sigidx
rtype = substr signature, sigidx, 1
goto iter_loop
iter_end:
.return (ops, posargs, namedargs)
.end
=back
=head2 Methods on C<PAST::Node> arguments
The methods below are used to transform PAST nodes into their
POST equivalents.
=head3 Defaults
=over 4
=item as_post(node) (General)
Return a POST representation of C<node>. Note that C<post> is
a multimethod based on the type of its first argument, this is
the method that is called when no other methods match.
=item as_post(Any)
This is the "fallback" method for any unrecognized node types.
We use this to throw a more useful exception in case any non-PAST
nodes make it into the tree.
=cut
.sub 'as_post' :method :multi(_, _)
.param pmc node
.param pmc options :slurpy :named
$S0 = typeof node
self.panic("PAST::Compiler can't compile node of type ", $S0)
.end
=item as_post(Undef)
Return an empty POST node that can be used to hold a (PMC) result.
=cut
.sub 'as_post' :method :multi(_, Undef)
.param pmc node
.param pmc options :slurpy :named
.local string result
$P0 = get_hll_global ['POST'], 'Ops'
result = self.'uniquereg'('P')
.return $P0.'new'('result'=>result)
.end
=item as_post(String class)
Generate POST to create a new object of type C<class>. This
is typically invoked by the various vivification methods below
(e.g., in a PAST::Var node to default a variable to a given type).
=cut
.sub 'as_post' :method :multi(_, String)
.param pmc node
.param pmc options :slurpy :named
.local string result
$P0 = get_hll_global ['POST'], 'Op'
result = self.'uniquereg'('P')
$S0 = self.'escape'(node)
.return $P0.'new'(result, $S0, 'pirop'=>'new', 'result'=>result)
.end
=item as_post(PAST::Node node)
Return the POST representation of executing C<node>'s children in
sequence. The result of the final child is used as the result
of this node.
N.B.: This method is also the one that is invoked for converting
nodes of type C<PAST::Stmts>.
=cut
.sub 'as_post' :method :multi(_, PAST::Node)
.param pmc node
.param pmc options :slurpy :named
.local pmc ops
.local string rtype
rtype = options['rtype']
$P0 = node.'list'()
$I0 = elements $P0
$S0 = repeat 'v', $I0
concat $S0, rtype
ops = self.'post_children'(node, 'signature'=>$S0)
$P0 = ops[-1]
ops.'result'($P0)
.return (ops)
.end
=back
=head3 C<PAST::Block>
=over 4
=item as_post(PAST::Block node)
Return the POST representation of a C<PAST::Block>.
=cut
.sub 'as_post' :method :multi(_, ['PAST::Block'])
.param pmc node
.param pmc options :slurpy :named
## add current block node to @?BLOCK
.local pmc blockpast
blockpast = get_global '@?BLOCK'
unshift blockpast, node
.local string name, pirflags, blocktype
.local pmc ns
name = node.'name'()
pirflags = node.'pirflags'()
blocktype = node.'blocktype'()
ns = node.'namespace'()
## handle anonymous blocks
if name goto have_name
name = self.'unique'('_block')
if ns goto have_name
pirflags = concat pirflags, ' :anon'
have_name:
## create a POST::Sub node for this block
.local pmc bpost
$P0 = get_hll_global ['POST'], 'Sub'
bpost = $P0.'new'('node'=>node, 'name'=>name, 'blocktype'=>blocktype, 'namespace'=>ns)
unless pirflags goto pirflags_done
bpost.'pirflags'(pirflags)
pirflags_done:
## determine the outer POST::Sub for the new one
.local pmc outerpost
outerpost = get_global '$?SUB'
$P0 = node.'lexical'()
if $P0 goto outer_block
null $P0
set_global '$?SUB', $P0
goto outer_done
outer_block:
bpost.'outer'(outerpost)
set_global '$?SUB', bpost
outer_done:
## merge the node's symtable with the master
.local pmc outersym, symtable
outersym = getattribute self, '%!symtable'
symtable = outersym
## if the Block doesn't have a symtable, re-use the existing one
$P0 = node.'symtable'()
unless $P0 goto have_symtable
## if the Block has a default ('') entry, use the Block's symtable as-is
symtable = $P0
$I0 = defined symtable['']
if $I0 goto have_symtable
## merge the Block's symtable with outersym
symtable = clone symtable
symtable_merge:
.local pmc iter
iter = new 'Iterator', outersym
symtable_merge_loop:
unless iter goto have_symtable
$S0 = shift iter
$I0 = exists symtable[$S0]
if $I0 goto symtable_merge_loop
$P0 = iter[$S0]
symtable[$S0] = $P0
goto symtable_merge_loop
have_symtable:
setattribute self, '%!symtable', symtable
.local pmc compiler
compiler = node.'compiler'()
if compiler goto children_compiler
## control exception handler
.local pmc ctrlpast, ctrllabel, rethrowlabel
ctrlpast = node.'control'()
unless ctrlpast goto children_past
$P0 = get_hll_global ['POST'], 'Label'
$S0 = self.'unique'('control_')
ctrllabel = $P0.'new'('result'=>$S0)
$S0 = concat $S0, '_rethrow'
rethrowlabel = $P0.'new'('result'=>$S0)
bpost.'push_pirop'('push_eh', ctrllabel)
children_past:
## all children but last are void context, last returns anything
$P0 = node.'list'()
$I0 = elements $P0
$S0 = repeat 'v', $I0
concat $S0, '*'
## convert children to post
.local pmc ops
ops = self.'post_children'(node, 'signature'=>$S0)
bpost.'push'(ops)
## result of last child is return from block
$P0 = ops[-1]
bpost.'push_pirop'('return', $P0)
unless ctrlpast goto sub_done
bpost.'push'(ctrllabel)
bpost.'push_pirop'('.local pmc exception')
bpost.'push_pirop'('.get_results (exception, $S10)')
$I0 = isa ctrlpast, 'PAST::Node'
if $I0 goto control_past
if ctrlpast == 'return_pir' goto control_return
self.panic("Unrecognized control handler '", ctrlpast, "'")
control_return:
## handle 'return' exceptions
$S0 = self.'uniquereg'('P')
bpost.'push_pirop'('getattribute', $S0, 'exception', '"type"')
bpost.'push_pirop'('if_null', $S0, rethrowlabel)
bpost.'push_pirop'('ne', $S0, .CONTROL_RETURN, rethrowlabel)
bpost.'push_pirop'('getattribute', $S0, 'exception', '"payload"')
bpost.'push_pirop'('return', $S0)
bpost.'push'(rethrowlabel)
bpost.'push_pirop'('rethrow', 'exception')
goto sub_done
control_past:
$P0 = self.'as_post'(ctrlpast, 'rtype'=>'*')
bpost.'push'($P0)
goto sub_done
children_compiler:
## set the compiler to use for the POST::Sub node, pass on
## and compiler arguments and add this block's child to it.
bpost.'compiler'(compiler)
$P0 = node.'compiler_args'()
bpost.'compiler_args'($P0)
$P0 = node[0]
bpost.'push'($P0)
sub_done:
## generate any loadinit code for the sub
$I0 = exists node['loadinit']
unless $I0 goto loadinit_done
.local pmc lipast, lipost
lipast = node.'loadinit'()
lipost = self.'as_post'(lipast, 'rtype'=>'v')
$P0 = get_hll_global ['POST'], 'Sub'
lipost = $P0.'new'('outer'=>bpost, 'pirflags'=>':load :init')
lipost.'push_pirop'('.local pmc', 'block')
lipost.'push_pirop'('interpinfo', '$P20', .INTERPINFO_CURRENT_SUB)
lipost.'push_pirop'('callmethod', 'get_outer', '$P20', 'result'=>'block')
bpost.'push'(lipost)
loadinit_done:
## restore previous outer scope and symtable
set_global '$?SUB', outerpost
setattribute self, '%!symtable', outersym
## determine name and namespace
name = self.'escape'(name)
$I0 = defined ns
unless $I0 goto have_ns_key
$P0 = get_global '%!codestring'
ns = $P0.'key'(ns)
have_ns_key:
.local string rtype, result
rtype = options['rtype']
if blocktype == 'immediate' goto block_immediate
if rtype == 'v' goto block_done
result = self.'uniquereg'('P')
$P0 = get_hll_global ['POST'], 'Ops'
bpost = $P0.'new'(bpost, 'node'=>node, 'result'=>result)
if ns goto block_decl_ns
concat $S0, '.const .Sub ', result
concat $S0, ' = '
concat $S0, name
bpost.'push_pirop'($S0)
goto block_done
block_decl_ns:
bpost.'push_pirop'('get_hll_global', result, ns, name)
goto block_done
block_immediate:
result = self.'uniquereg'(rtype)
$P0 = get_hll_global ['POST'], 'Ops'
bpost = $P0.'new'(bpost, 'node'=>node, 'result'=>result)
if ns goto block_immediate_ns
bpost.'push_pirop'('call', name, 'result'=>result)
goto block_done
block_immediate_ns:
$S0 = '$P10'
bpost.'push_pirop'('get_hll_global', $S0, ns, name, 'result'=>$S0)
bpost.'push_pirop'('call', $S0, 'result'=>result)
block_done:
## remove current block from @?BLOCK
$P99 = shift blockpast
.return (bpost)
.end
=back
=head3 C<PAST::Op>
=over 4
=item as_post(PAST::Op node)
Return the POST representation of a C<PAST::Op> node. Normally
this is handled by redispatching to a method corresponding to
the node's "pasttype" attribute.
=cut
.sub 'as_post' :method :multi(_, ['PAST::Op'])
.param pmc node
.param pmc options :slurpy :named
## see if we set first child's lvalue
$I0 = node.'lvalue'()
unless $I0 goto have_lvalue
$P0 = node[0]
if null $P0 goto have_lvalue
$P0.'lvalue'($I0)
have_lvalue:
.local string pasttype
pasttype = node.'pasttype'()
unless pasttype goto post_pirop
$P0 = find_method self, pasttype
.return self.$P0(node, options :flat :named)
post_pirop:
.local pmc pirop
pirop = node.'pirop'()
unless pirop goto post_inline
.return self.'pirop'(node, options :flat :named)
post_inline:
.local pmc inline
inline = node.'inline'()
unless inline goto post_call
.return self.'inline'(node, options :flat :named)
post_call:
.return self.'call'(node, options :flat :named)
.end
=item pirop(PAST::Op node)
Return the POST representation of a C<PAST::Op> node with
a 'pasttype' of 'pirop'.
=cut
.sub 'pirop' :method :multi(_, ['PAST::Op'])
.param pmc node
.param pmc options :slurpy :named
.local string pirop, signature
pirop = node.'pirop'()
## see if pirop is of form "pirop signature"
$I0 = index pirop, ' '
if $I0 < 0 goto pirop_1
$I1 = $I0 + 1
signature = substr pirop, $I1
pirop = substr pirop, 0, $I0
goto have_signature
pirop_1:
$P0 = get_global '%piropsig'
signature = $P0[pirop]
if signature goto have_signature
signature = 'vP'
have_signature:
.local pmc ops
ops = self.'post_children'(node, 'signature'=>signature)
.local pmc arglist
arglist = ops.'list'()
$S0 = substr signature, 0, 1
if $S0 == 'v' goto pirop_void
pirop_reg:
.local string result
result = self.'uniquereg'($S0)
ops.'result'(result)
ops.'push_pirop'(pirop, result, arglist :flat)
.return (ops)
pirop_void:
ops.'push_pirop'(pirop, arglist :flat)
.return (ops)
.end
=item call(PAST::Op node)
Return the POST representation of a C<PAST::Op> node
for calling a sub.
=cut
.sub 'call' :method :multi(_, ['PAST::Op'])
.param pmc node
.param pmc options :slurpy :named
.local string pasttype
pasttype = node.'pasttype'()
if pasttype goto have_pasttype
pasttype = 'call'
have_pasttype:
.local string signature
signature = 'v:'
## for callmethod, the invocant (child) must be a PMC
if pasttype != 'callmethod' goto have_signature
signature = 'vP:'
have_signature:
.local pmc name, ops, posargs, namedargs
name = node.'name'()
if name goto call_by_name
## our first child is the thing to be invoked, so make sure it's a PMC
substr signature, 1, 0, 'P'
(ops, posargs, namedargs) = self.'post_children'(node, 'signature'=>signature)
goto children_done
call_by_name:
(ops, posargs, namedargs) = self.'post_children'(node, 'signature'=>signature)
$I0 = isa name, 'PAST::Node'
if $I0 goto call_by_name_past
$S0 = self.'escape'(name)
unshift posargs, $S0
goto children_done
call_by_name_past:
.local pmc name_post
name_post = self.'as_post'(name, 'rtype'=>'s')
name_post = self.'coerce'(name_post, 's')
ops.'push'(name_post)
unshift posargs, name_post
children_done:
## generate the call itself
.local string result, rtype
rtype = options['rtype']
result = self.'uniquereg'(rtype)
ops.'push_pirop'(pasttype, posargs :flat, namedargs :flat, 'result'=>result)
ops.'result'(result)
.return (ops)
.end
=item callmethod(PAST::Op node)
Return the POST representation of a C<PAST::Op> node
to invoke a method on a PMC.
=cut
.sub 'callmethod' :method :multi(_, ['PAST::Op'])
.param pmc node
.param pmc options :slurpy :named
.return self.'call'(node, options :flat :named)
.end
=item if(PAST::Op node)
=item unless(PAST::Op node)
Return the POST representation of C<PAST::Op> nodes with
a 'pasttype' of if/unless.
=cut
.sub 'if' :method :multi(_,['PAST::Op'])
.param pmc node
.param pmc options :slurpy :named
.local pmc opsclass, ops
opsclass = get_hll_global ['POST'], 'Ops'
ops = opsclass.'new'('node'=>node)
.local string rtype, result
rtype = options['rtype']
result = self.'uniquereg'(rtype)
ops.'result'(result)
.local string pasttype
pasttype = node.'pasttype'()
.local pmc exprpast, thenpast, elsepast, childpast
.local pmc exprpost, thenpost, elsepost, childpost
exprpast = node[0]
thenpast = node[1]
elsepast = node[2]
.local pmc thenlabel, endlabel
$P0 = get_hll_global ['POST'], 'Label'
$S0 = concat pasttype, '_'
$S0 = self.'unique'($S0)
thenlabel = $P0.'new'('result'=>$S0)
$S0 = concat $S0, '_end'
endlabel = $P0.'new'('result'=>$S0)
.local string exprrtype, childrtype
exprrtype = 'r'
if rtype != 'v' goto have_exprrtype
exprrtype = '*'
have_exprrtype:
childrtype = rtype
$I0 = index '*:', rtype
if $I0 < 0 goto have_childrtype
childrtype = 'P'
have_childrtype:
exprpost = self.'as_post'(exprpast, 'rtype'=>exprrtype)
childpast = thenpast
bsr make_childpost
thenpost = childpost
childpast = elsepast
bsr make_childpost
elsepost = childpost
if null elsepost goto no_elsepost
ops.'push'(exprpost)
ops.'push_pirop'(pasttype, exprpost, thenlabel)
if null elsepost goto else_done
ops.'push'(elsepost)
else_done:
ops.'push_pirop'('goto', endlabel)
ops.'push'(thenlabel)
if null thenpost goto then_done
ops.'push'(thenpost)
then_done:
ops.'push'(endlabel)
.return (ops)
no_elsepost:
$S0 = 'if'
unless pasttype == $S0 goto no_elsepost_1
$S0 = 'unless'
no_elsepost_1:
ops.'push'(exprpost)
ops.'push_pirop'($S0, exprpost, endlabel)
if null thenpost goto no_elsepost_2
ops.'push'(thenpost)
no_elsepost_2:
ops.'push'(endlabel)
.return (ops)
make_childpost:
null childpost
$I0 = defined childpast
unless $I0 goto no_childpast
childpost = self.'as_post'(childpast, 'rtype'=>childrtype)
goto childpost_coerce
no_childpast:
if rtype == 'v' goto ret_childpost
childpost = opsclass.'new'('result'=>exprpost)
childpost_coerce:
unless result goto ret_childpost
childpost = self.'coerce'(childpost, result)
ret_childpost:
ret
.end
.sub 'unless' :method :multi(_, ['PAST::Op'])
.param pmc node
.param pmc options :slurpy :named
.return self.'if'(node, options :flat :named)
.end
=item while(PAST::Op node)
=item until(PAST::Op node)
Return the POST representation of a C<while> or C<until> loop.
=cut
.sub 'while' :method :multi(_, ['PAST::Op'])
.param pmc node
.param pmc options :slurpy :named
.local string pasttype
pasttype = node.'pasttype'()
.local pmc ops
$P0 = get_hll_global ['POST'], 'Ops'