Open Ovid opened 1 month ago
state ops (nextstate, dbstate) are generated for each argument, which is what the debugger uses for stepping:
$ ./perl -Ilib -MO=Concise,f -e 'use v5.40.0; sub f ($x, $y) { print $x, $y }'
main::f:
a <1> leavesub[1 ref] K/REFC,1 ->(end)
- <@> lineseq KP ->a
- <1> ex-argcheck vK/1 ->7
- <@> lineseq vK ->-
1 <;> nextstate(main 6 -e:1) v:us,*,&,$,fea=8 ->2
2 <+> argcheck(2,0) v ->3
3 <;> nextstate(main 4 -e:1) v:us,*,&,$,fea=8 ->4
4 <+> argelem(0)[$x:4,6] v/SV ->5
5 <;> nextstate(main 5 -e:1) v:us,*,&,$,fea=8 ->6
6 <+> argelem(1)[$y:5,6] v/SV ->7
- <;> ex-nextstate(main 6 -e:1) v:us,*,&,$,fea=8 ->-
7 <;> nextstate(main 6 -e:1) v:us,*,&,$,fea=8 ->8
9 <@> print sK ->a
8 <0> padrange[$x:4,6; $y:5,6] /range=2 ->9
- <0> padsv[$x:4,6] s ->-
- <0> padsv[$y:5,6] s ->9
-e syntax OK
These appear to be deliberate, both sigscalarelem and sigslurpsigil in perly.y both add state ops.
@iabyn might know why.
On Tue, Sep 17, 2024 at 06:53:04PM -0700, Tony Cook wrote:
state ops (nextstate, dbstate) are generated for each argument, which is what the debugger uses for stepping:
These appear to be deliberate, both sigscalarelem and sigslurpsigil in perly.y both add state ops.
@iabyn might know why.
The original signature implementation (largely by Zefram) just planted a lot of standard perl ops to process signatures as if the signature handling had actually been written in perl. So for example:
sub f ($a, $b = 1) {}
deparsed as:
sub f { die sprintf("Too many arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @ <= 2; die sprintf("Too few arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @ >= 1; my $a = $[0]; my $b = @ >= 2 ? $_[1] : 1;
In that implementation, each item of parameter processing was separated by a nextstate op, which (among other things) ensured that the taint state was reset after each assignment (so one tainted arg wouldn't taint subsequent parameter values), and line numbers for warnings etc would be correct(ish).
When I added the sigFOO ops, I kept the nextstate ops: mainly in an attempt to preserve current behaviour as much as possible. I always intended that such sigFOO ops would be an intermediate step, and they would all be replaced by a single OP_SIGNATURE op in the peephole optimiser which would (via a mini state machine) do all the sig processing in a single op, with any taint resetting etc done by the OP_SIGNATURE, with no OP_NEXTSTATEs left.
I don't think I ever considered how the debugger would be affected.
-- Standards (n). Battle insignia or tribal totems.
While I was looking at how to implement named params, I had vague thoughts in mind for neatening up the signature things overall; much as Dave suggests there. In the longterm I don't imagine there'll be as many OP_NEXTSTATE (or DBSTATE) ops involved.
In the short-term, I wonder if we could try simply not emitting so many of them. Since conceptually every defaulting expression for optional params does execute like a statement, those ought to be retained, but there's probably no need to have one on the OP_ARGCHECK nor the mandatory params without a defaulting expression.
Failing that, there's likely a spare private bit on the OP_DBSTATE that we can use to signal to the debugger not to count that one, so as to preserve the tree-shaped structure for code that wants to inspect the optree, but allowing the debugger to skip over those.
There was some discussion in IRC covering this earlier:
-d
, we can make the state op non-steppable/breakable by making then nextstate ops instead, rather than adding a private bit
Description
When using the debugger (
perl -d $program
), you can use thes
command to step through lines of code, one at a time. However, this is not true for subroutine signatures. When yous
tep through one of these subs, you have tos
tep through the signature at N+1 times, where N is the number of variables in the signature. These extra steps are not obvious, because nothing visible is happening, and they can be confusing. More than once I've accidentallys
tepped over the first line of a sub/method because I hit "return" too many times on a signatures.This makes using the already cumbersome debugger CLI even more cumbersone.
It does not affect subroutines without signatures, even if they have a prototype (since the latter is compile-time, not run time).
This appears to affect all versions of Perl which allow signatures, whether by default, or when using the experimental signatures feature.
Steps to Reproduce
Run the following program with
perl -d
:Expected behavior
When running the above program with the debugger, when you try to step into the first sub, you immediately get into the body of the sub:
I expect the debugger to never stop on the signature line, or at minimum, to only stop once (because we might need to see the defaults).
However, for all subs with signatures, you must repeat the
s
(or just hit "return" to reuse the last step command) N+1 times for every variable in the signature, even though it's not obvious to the programmer that runtime behavior is occurring here:However, you can verify runtime behavior if you print out the value of those variables after every
s
tep:That being said, I've never felt the need to dump signature variables while they're being individually assigned.
Perl configuration