diff options
author | David Mitchell <davem@iabyn.com> | 2016-09-15 10:59:37 +0100 |
---|---|---|
committer | David Mitchell <davem@iabyn.com> | 2016-10-04 11:18:40 +0100 |
commit | 5012eebe5586df96a1869edfedea1382aa254085 (patch) | |
tree | 1ade02c4dd69a3204fb5db3a1b8588f6854c2946 /ext/B/t | |
parent | 1c5665476f0d7250c7d93f82eab2b7cda1e6937f (diff) | |
download | perl-5012eebe5586df96a1869edfedea1382aa254085.tar.gz |
make OP_SPLIT a PMOP, and eliminate OP_PUSHRE
Most ops that execute a regex, such as match and subst, are of type PMOP.
A PMOP allows the actual regex to be attached directly to that op, due
to its extra fields.
OP_SPLIT is different; it is just a plain LISTOP, but it always has an
OP_PUSHRE as its first child, which *is* a PMOP and which has the regex
attached.
At runtime, pp_pushre()'s only job is to push itself (i.e. the current
PL_op) onto the stack. Later pp_split() pops this to get access to the
regex it wants to execute.
This is a bit unpleasant, because we're pushing an OP* onto the stack,
which is supposed to be an array of SV*'s. As a bit of a hack, on
DEBUGGING builds we push a PVLV with the PL_op address embedded instead,
but this still isn't very satisfactory.
Now that regexes are first-class SVs, we could push a REGEXP onto the
stack rather than PL_op. However, there is an optimisation of @array =
split which eliminates the assign and embeds the array's GV/padix directly
in the PUSHRE op. So split still needs access to that op. But the pushre
op will always be splitop->op_first anyway, so one possibility is to just
skip executing the pushre altogether, and make pp_split just directly
access op_first instead to get the regex and @array info.
But if we're doing that, then why not just go the full hog and make
OP_SPLIT into a PMOP, and eliminate the OP_PUSHRE op entirely: with the
data that was spread across the two ops now combined into just the one
split op.
That is exactly what this commit does.
For a simple compile-time pattern like split(/foo/, $s, 1), the optree
looks like:
before:
<@> split[t2] lK
</> pushre(/"foo"/) s/RTIME
<0> padsv[$s:1,2] s
<$> const(IV 1) s
after:
</> split(/"foo"/)[t2] lK/RTIME
<0> padsv[$s:1,2] s
<$> const[IV 1] s
while for a run-time expression like split(/$pat/, $s, 1),
before:
<@> split[t3] lK
</> pushre() sK/RTIME
<|> regcomp(other->8) sK
<0> padsv[$pat:2,3] s
<0> padsv[$s:1,3] s
<$> const(IV 1)s
after:
</> split()[t3] lK/RTIME
<|> regcomp(other->8) sK
<0> padsv[$pat:2,3] s
<0> padsv[$s:1,3] s
<$> const[IV 1] s
This makes the code faster and simpler.
At the same time, two new private flags have been added for OP_SPLIT -
OPpSPLIT_ASSIGN and OPpSPLIT_LEX - which make it explicit that the
assign op has been optimised away, and if so, whether the array is
lexical.
Also, deparsing of split has been improved, to the extent that
perl TEST -deparse op/split.t
now passes.
Also, a couple of panic messages in pp_split() have been replaced with
asserts().
Diffstat (limited to 'ext/B/t')
-rw-r--r-- | ext/B/t/b.t | 3 | ||||
-rw-r--r-- | ext/B/t/optree_concise.t | 4 | ||||
-rw-r--r-- | ext/B/t/walkoptree.t | 6 |
3 files changed, 6 insertions, 7 deletions
diff --git a/ext/B/t/b.t b/ext/B/t/b.t index 4638c3e577..a5d724912b 100644 --- a/ext/B/t/b.t +++ b/ext/B/t/b.t @@ -298,8 +298,7 @@ is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)"); is(B::class(bless {}, "Wibble::Bibble"), "Bibble", "Testing B::class()"); is(B::cast_I32(3.14), 3, "Testing B::cast_I32()"); -is(B::opnumber("chop"), $] >= 5.015 ? 39 : 38, - "Testing opnumber with opname (chop)"); +is(B::opnumber("chop"), 38, "Testing opnumber with opname (chop)"); { no warnings 'once'; diff --git a/ext/B/t/optree_concise.t b/ext/B/t/optree_concise.t index 12781acdb8..1e2594703f 100644 --- a/ext/B/t/optree_concise.t +++ b/ext/B/t/optree_concise.t @@ -183,13 +183,13 @@ checkOptree ( name => "terse basic", UNOP (0x82b0918) leavesub [1] LISTOP (0x82b08d8) lineseq COP (0x82b0880) nextstate - UNOP (0x82b0860) null [15] + UNOP (0x82b0860) null [14] PADOP (0x82b0840) gvsv GV (0x82a818c) *a EOT_EOT # UNOP (0x8282310) leavesub [1] # LISTOP (0x82822f0) lineseq # COP (0x82822b8) nextstate -# UNOP (0x812fc20) null [15] +# UNOP (0x812fc20) null [14] # SVOP (0x812fc00) gvsv GV (0x814692c) *a EONT_EONT diff --git a/ext/B/t/walkoptree.t b/ext/B/t/walkoptree.t index 3648835b7f..1d42dd5140 100644 --- a/ext/B/t/walkoptree.t +++ b/ext/B/t/walkoptree.t @@ -36,13 +36,13 @@ my $victim = sub { $_[0] =~ s/(a)/ $1/; # PMOP_pmreplroot(cPMOPo) is NULL for this $_[0] =~ s/(b)//; - # This gives an OP_PUSHRE + # This gives an OP_SPLIT split /c/; }; is (B::walkoptree_debug, 0, 'walkoptree_debug() is 0'); B::walkoptree(B::svref_2object($victim)->ROOT, "pie"); -foreach (qw(substcont pushre split leavesub)) { +foreach (qw(substcont split split leavesub)) { is ($seen{$_}, 1, "Our victim had a $_ OP"); } is_deeply ([keys %debug], [], 'walkoptree_debug was not called'); @@ -52,7 +52,7 @@ is (B::walkoptree_debug, 1, 'walkoptree_debug() is 1'); %seen = (); B::walkoptree(B::svref_2object($victim)->ROOT, "pie"); -foreach (qw(substcont pushre split leavesub)) { +foreach (qw(substcont split split leavesub)) { is ($seen{$_}, 1, "Our victim had a $_ OP"); } is_deeply (\%debug, \%seen, 'walkoptree_debug was called correctly'); |