Skip to content

Commit 8b9e991

Browse files
committed
Optimize away control exception handlers in while/unit/repeat loops that don't need them.
1 parent c0fbe4d commit 8b9e991

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

src/NQP/Actions.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ class NQP::Actions is HLL::Actions {
339339
method statement_control:sym<while>($/) {
340340
my $past := xblock_immediate( $<xblock>.ast );
341341
$past.op(~$<sym>);
342+
unless $*CONTROL_USED {
343+
$past.push(QAST::IVal.new( :value(1), :named('nohandler') ));
344+
}
342345
make $past;
343346
}
344347

@@ -353,6 +356,9 @@ class NQP::Actions is HLL::Actions {
353356
$past := QAST::Op.new( $<EXPR>.ast, block_immediate( $<pblock>.ast ),
354357
:op($op), :node($/) );
355358
}
359+
unless $*CONTROL_USED {
360+
$past.push(QAST::IVal.new( :value(1), :named('nohandler') ));
361+
}
356362
make $past;
357363
}
358364

src/NQP/Grammar.pm

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ grammar NQP::Grammar is HLL::Grammar {
2424
NQP::World.new(:handle($source_id)) !!
2525
NQP::World.new(:handle($source_id), :description($file));
2626

27-
my $*SCOPE := '';
28-
my $*MULTINESS := '';
29-
my $*PKGDECL := '';
30-
my $*INVOCANT_OK := 0;
31-
my $*RETURN_USED := 0;
27+
my $*SCOPE := '';
28+
my $*MULTINESS := '';
29+
my $*PKGDECL := '';
30+
my $*INVOCANT_OK := 0;
31+
my $*RETURN_USED := 0;
32+
my $*CONTROL_USED := 0;
3233
my %*HANDLERS;
3334
self.comp_unit;
3435
}
@@ -211,11 +212,13 @@ grammar NQP::Grammar is HLL::Grammar {
211212
}
212213

213214
token statement_control:sym<while> {
215+
:my $*CONTROL_USED := 0;
214216
$<sym>=[while|until] \s :s
215217
<xblock>
216218
}
217219

218220
token statement_control:sym<repeat> {
221+
:my $*CONTROL_USED := 0;
219222
<sym> \s :s
220223
[
221224
| $<wu>=[while|until]\s <xblock>
@@ -730,9 +733,9 @@ grammar NQP::Grammar is HLL::Grammar {
730733

731734
token prefix:sym<return> { <sym> \s <O('%list_prefix')> { $*RETURN_USED := 1 } }
732735
token prefix:sym<make> { <sym> \s <O('%list_prefix')> }
733-
token term:sym<last> { <sym> }
734-
token term:sym<next> { <sym> }
735-
token term:sym<redo> { <sym> }
736+
token term:sym<last> { <sym> { $*CONTROL_USED := 1 } }
737+
token term:sym<next> { <sym> { $*CONTROL_USED := 1 } }
738+
token term:sym<redo> { <sym> { $*CONTROL_USED := 1 } }
736739

737740
method smartmatch($/) {
738741
# swap rhs into invocant position

src/QAST/Operations.nqp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ for ('', 'repeat_') -> $repness {
612612
$ops.push($done_lbl);
613613
$ops.push_pirop('pop_eh');
614614
}
615+
else {
616+
$ops.push($done_lbl);
617+
}
615618

616619
$ops;
617620
});

src/QRegex/Cursor.nqp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ role NQPCursorRole {
182182
if !nqp::isnull($actions) && nqp::can($actions, $name);
183183
}
184184

185+
my @EMPTY := [];
185186
method !protoregex($name) {
186187
# Obtain and run NFA.
187188
my $nfa := self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) });
@@ -195,7 +196,7 @@ role NQPCursorRole {
195196
$rxname := nqp::atpos(@rxfate, nqp::pop_i(@fates));
196197
#nqp::say("invoking $rxname");
197198
$cur := self."$rxname"();
198-
last if nqp::getattr_i($cur, $?CLASS, '$!pos') >= 0;
199+
@fates := @EMPTY if nqp::getattr_i($cur, $?CLASS, '$!pos') >= 0;
199200
}
200201
$cur // self."!cursor_start"();
201202
}

0 commit comments

Comments
 (0)