Skip to content

Commit

Permalink
Don't emit cstack handling code if unused
Browse files Browse the repository at this point in the history
Anything that doesn't capture or won't need to backtrack into a subrule
call will not need a cstack. We already allocate it lazily, but this
change means we don't even generate code to trim it when the null check
guarding it would always come out true. This saves the check and makes
the generated code more compact. This results in a bit over 3KB less
bytecode from compiling the NQP grammar. This change combined with the
previous one to not emit unused cclass setup save around 43KB off the
compiled output of the Perl 6 grammar.
  • Loading branch information
jnthn committed Apr 8, 2018
1 parent 9b75f67 commit d6061c5
Showing 1 changed file with 52 additions and 30 deletions.
82 changes: 52 additions & 30 deletions src/vm/moar/QAST/QASTRegexCompilerMAST.nqp
Expand Up @@ -68,6 +68,9 @@ class QAST::MASTRegexCompiler {
# Jump table.
has @!rxjumps;

# Do we use the cursor stack?
has int $!cstack_used;

method new(:$qastcomp!, :$regalloc!) {
my $obj := nqp::create(self);
$obj.BUILD($qastcomp, $regalloc);
Expand All @@ -84,6 +87,11 @@ class QAST::MASTRegexCompiler {
$!qastcomp.unique($str)
}

method !get_cstack() {
$!cstack_used := 1;
%!reg<cstack>
}

method as_mast($node, :$want) {
# Build the list of (unique) registers we need
my $tgt := $!regalloc.fresh_s();
Expand Down Expand Up @@ -111,8 +119,6 @@ class QAST::MASTRegexCompiler {
my $restartlabel := label();
my $faillabel := label();
my $jumplabel := label();
my $cutlabel := label();
my $cstacklabel := label();

my $self := $*BLOCK.local('self');

Expand Down Expand Up @@ -209,25 +215,34 @@ class QAST::MASTRegexCompiler {
$i0 := $!regalloc.fresh_i();
$itmp := $!regalloc.fresh_i();
my $i18 := $!regalloc.fresh_i();
nqp::push(@ins, $restartlabel);
if $!cstack_used {
nqp::push(@ins, op('getattr_o', $cstack, $cur, $curclass, sval('$!cstack'),
ival(nqp::hintfor($!cursor_type, '$!cstack'))));
}
merge_ins(@ins, [
$restartlabel,
op('getattr_o', $cstack, $cur, $curclass, sval('$!cstack'),
ival(nqp::hintfor($!cursor_type, '$!cstack'))),
$faillabel,
op('isnull', $i0, $bstack),
op('if_i', $i0, $donelabel),
op('elems', $i0, $bstack),
op('gt_i', $i0, $i0, $zero),
op('unless_i', $i0, $donelabel),
op('pop_i', $itmp, $bstack),
op('islist', $i0, $cstack),
op('unless_i', $i0, $cstacklabel),
op('elems', $i0, $cstack),
op('gt_i', $i0, $i0, $zero),
op('unless_i', $i0, $cstacklabel),
op('dec_i', $itmp),
op('atpos_o', $back_cur, $cstack, $itmp),
$cstacklabel,
op('pop_i', $itmp, $bstack)
]);
if $!cstack_used {
my $cstacklabel := label();
merge_ins(@ins, [
op('islist', $i0, $cstack),
op('unless_i', $i0, $cstacklabel),
op('elems', $i0, $cstack),
op('gt_i', $i0, $i0, $zero),
op('unless_i', $i0, $cstacklabel),
op('dec_i', $itmp),
op('atpos_o', $back_cur, $cstack, $itmp),
$cstacklabel
]);
}
merge_ins(@ins, [
op('pop_i', $rep, $bstack),
op('pop_i', $pos, $bstack),
op('pop_i', $itmp, $bstack),
Expand All @@ -237,17 +252,24 @@ class QAST::MASTRegexCompiler {
op('if_i', $i0, $faillabel),
op('eq_i', $i0, $itmp, $zero),
op('if_i', $i0, $faillabel),
]);
if $!cstack_used {
# backtrack the cursor stack
op('isnull', $i0, $cstack),
op('if_i', $i0, $jumplabel),
op('unless_o', $cstack, $jumplabel),
op('elems', $i18, $bstack),
op('le_i', $i0, $i18, $zero),
op('if_i', $i0, $cutlabel),
op('dec_i', $i18),
op('atpos_i', $i18, $bstack, $i18),
$cutlabel,
op('setelemspos', $cstack, $i18),
my $cutlabel := label();
merge_ins(@ins, [
op('isnull', $i0, $cstack),
op('if_i', $i0, $jumplabel),
op('unless_o', $cstack, $jumplabel),
op('elems', $i18, $bstack),
op('le_i', $i0, $i18, $zero),
op('if_i', $i0, $cutlabel),
op('dec_i', $i18),
op('atpos_i', $i18, $bstack, $i18),
$cutlabel,
op('setelemspos', $cstack, $i18),
]);
}
merge_ins(@ins, [
$jumplabel,
op('jumplist', ival(+@!rxjumps), $itmp)
]);
Expand Down Expand Up @@ -1062,7 +1084,7 @@ class QAST::MASTRegexCompiler {
op('findmeth', %!reg<method>, %!reg<cur>, sval('!cursor_capture')),
op('const_s', $s11, sval($node.name)),
call(%!reg<method>, [$Arg::obj, $Arg::obj, $Arg::str],
%!reg<cur>, $p11, $s11, :result(%!reg<cstack>)),
%!reg<cur>, $p11, $s11, :result(self.'!get_cstack'())),
op('goto', $donelabel),
$faillabel,
op('goto', %!reg<fail>),
Expand Down Expand Up @@ -1193,7 +1215,7 @@ class QAST::MASTRegexCompiler {
sval('!cursor_capture')));
nqp::push(@ins, op('const_s', $sname, sval($node.name)));
nqp::push(@ins, call(%!reg<method>, [$Arg::obj, $Arg::obj, $Arg::str],
%!reg<cur>, $p11, $sname, :result(%!reg<cstack>)));
%!reg<cur>, $p11, $sname, :result(self.'!get_cstack'())));
$!regalloc.release_register($sname, $MVM_reg_str);
$captured := 1;

Expand All @@ -1207,7 +1229,7 @@ class QAST::MASTRegexCompiler {
op('push_i', $bstack, $itmp),
op('push_i', $bstack, %!reg<negone>),
op('push_i', $bstack, %!reg<negone>),
op('elems', $itmp, %!reg<cstack>),
op('elems', $itmp, self.'!get_cstack'()),
op('dec_i', $itmp),
op('push_i', $bstack, $itmp)
]);
Expand All @@ -1216,7 +1238,7 @@ class QAST::MASTRegexCompiler {
nqp::push(@ins, op('findmeth', %!reg<method>, %!reg<cur>,
sval('!cursor_push_cstack')));
nqp::push(@ins, call(%!reg<method>, [$Arg::obj, $Arg::obj],
%!reg<cur>, $p11, :result(%!reg<cstack>)));
%!reg<cur>, $p11, :result(self.'!get_cstack'())));
}

my $bstack := %!reg<bstack>;
Expand All @@ -1225,7 +1247,7 @@ class QAST::MASTRegexCompiler {
op('push_i', $bstack, $itmp),
op('push_i', $bstack, %!reg<zero>),
op('push_i', $bstack, %!reg<pos>),
op('elems', $itmp, %!reg<cstack>),
op('elems', $itmp, self.'!get_cstack'()),
op('push_i', $bstack, $itmp)
]);
}
Expand All @@ -1237,7 +1259,7 @@ class QAST::MASTRegexCompiler {
sval('!cursor_capture')));
nqp::push(@ins, op('const_s', $sname, sval($node.name)));
nqp::push(@ins, call(%!reg<method>, [$Arg::obj, $Arg::obj, $Arg::str],
%!reg<cur>, $p11, $sname, :result(%!reg<cstack>)));
%!reg<cur>, $p11, $sname, :result(self.'!get_cstack'())));
$!regalloc.release_register($sname, $MVM_reg_str);
}

Expand Down

0 comments on commit d6061c5

Please sign in to comment.