Skip to content

Commit b97cea4

Browse files
committed
Implement nqp::chain compilation.
1 parent ce7482c commit b97cea4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/vm/moar/QAST/QASTOperationsMAST.nqp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,59 @@ QAST::MASTOperations.add_core_op('hash', -> $qastcomp, $op {
563563
$hash
564564
});
565565

566+
# Chaining.
567+
QAST::MASTOperations.add_core_op('chain', -> $qastcomp, $op {
568+
# First, we build up the list of nodes in the chain
569+
my @clist;
570+
my $cqast := $op;
571+
while $cqast ~~ QAST::Op && $cqast.op eq 'chain' {
572+
nqp::push(@clist, $cqast);
573+
$cqast := $cqast[0];
574+
}
575+
576+
my @ops;
577+
my $res_reg := $*REGALLOC.fresh_register($MVM_reg_obj);
578+
my $endlabel := MAST::Label.new( :name($qastcomp.unique('chain_end_')) );
579+
580+
$cqast := nqp::pop(@clist);
581+
my $aqast := $cqast[0];
582+
my $acomp := $qastcomp.as_mast($aqast, :want($MVM_reg_obj));
583+
push_ilist(@ops, $acomp);
584+
585+
my $more := 1;
586+
while $more {
587+
my $bqast := $cqast[1];
588+
my $bcomp := $qastcomp.as_mast($bqast, :want($MVM_reg_obj));
589+
push_ilist(@ops, $bcomp);
590+
591+
my $callee := $qastcomp.as_mast(
592+
QAST::Var.new( :name($cqast.name), :scope('lexical') ),
593+
:want($MVM_reg_obj));
594+
push_ilist(@ops, $callee);
595+
nqp::push(@ops, MAST::Call.new(
596+
:target($callee.result_reg),
597+
:flags([$Arg::obj, $Arg::obj]),
598+
:result($res_reg),
599+
$acomp.result_reg, $bcomp.result_reg
600+
));
601+
602+
$*REGALLOC.release_register($callee.result_reg, $MVM_reg_obj);
603+
$*REGALLOC.release_register($acomp.result_reg, $MVM_reg_obj);
604+
605+
if @clist {
606+
push_op(@ops, 'unless_o', $res_reg, $endlabel);
607+
$cqast := nqp::pop(@clist);
608+
$acomp := $bcomp;
609+
}
610+
else {
611+
$more := 0;
612+
}
613+
}
614+
615+
nqp::push(@ops, $endlabel);
616+
MAST::InstructionList.new(@ops, $res_reg, $MVM_reg_obj)
617+
});
618+
566619
# Conditionals.
567620
for <if unless> -> $op_name {
568621
QAST::MASTOperations.add_core_op($op_name, -> $qastcomp, $op {

0 commit comments

Comments
 (0)