Skip to content

Commit

Permalink
For blocks containing regexes, don't lower $_
Browse files Browse the repository at this point in the history
Since we need to look it up when doing `/foo/` in sink or boolean
contexts. Unfortunately, the current semantics of that rely on some
action at a distance (find the nearest defined `$_`, and update the `$/`
in that block); these probably want revisiting. I did attempt to try,
when we closure-clone a regex, to copy the appropriate `$_` into it;
this mostly works, but the case of `not /foo/` depends on it to skip
over the `not` frame, and there is another test which even further
relies on the current behavior. So for now, just don't optimize those
cases.
  • Loading branch information
jnthn committed Jan 4, 2019
1 parent 31c8fda commit 17bf741
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Perl6/Optimizer.nqp
Expand Up @@ -31,6 +31,7 @@ my class Symbols {
has $!Block;
has $!PseudoStash;
has $!Routine;
has $!Regex;
has $!Nil;
has $!Failure;
has $!Seq;
Expand Down Expand Up @@ -59,6 +60,7 @@ my class Symbols {
$!Block := self.find_lexical('Block');
$!PseudoStash := self.find_lexical('PseudoStash');
$!Routine := self.find_lexical('Routine');
$!Regex := self.find_lexical('Regex');
$!Nil := self.find_lexical('Nil');
$!Failure := self.find_lexical('Failure');
$!Seq := self.find_lexical('Seq');
Expand Down Expand Up @@ -101,6 +103,7 @@ my class Symbols {
method Mu() { $!Mu }
method Any() { $!Any }
method Block() { $!Block }
method Regex() { $!Regex }
method PseudoStash() { $!PseudoStash }
method Nil() { $!Nil }
method Failure() { $!Failure }
Expand Down Expand Up @@ -438,6 +441,9 @@ my class BlockVarOptimizer {
# If lowering is, for some reason, poisoned.
has int $!poisoned;

# If topic lowering is, for some reason, poisoned.
has int $!topic_poisoned;

# If p6bindsig is used.
has int $!uses_bindsig;

Expand Down Expand Up @@ -490,6 +496,8 @@ my class BlockVarOptimizer {

method poison_lowering() { $!poisoned := 1; }

method poison_topic_lowering() { $!topic_poisoned := 1; }

method uses_bindsig() { $!uses_bindsig := 1; }

method entering_handle_handler() { $!in_handle_handler++; }
Expand Down Expand Up @@ -674,7 +682,7 @@ my class BlockVarOptimizer {
next unless $sigil eq '$' || $sigil eq '@' || $sigil eq '%';
next unless nqp::chars($name) >= 2 &&
(nqp::iscclass(nqp::const::CCLASS_ALPHABETIC, $name, 1) ||
$can_lower_topic && nqp::eqat($name, '_', 1));
$can_lower_topic && !$!topic_poisoned && nqp::eqat($name, '_', 1));
}

# Also must not lexicalref it.
Expand Down Expand Up @@ -2825,6 +2833,9 @@ class Perl6::Optimizer {
if $visit.value =:= $!symbols.PseudoStash {
self.poison_var_lowering();
}
elsif nqp::istype($visit.value, $!symbols.Regex) {
@!block_var_stack[@!block_var_stack - 1].poison_topic_lowering();
}
}
elsif nqp::istype($visit, QAST::ParamTypeCheck) {
self.optimize-param-typecheck: $visit;
Expand Down

0 comments on commit 17bf741

Please sign in to comment.