Skip to content

Commit

Permalink
ParseXS: generate_output(): change ST(0)= logic
Browse files Browse the repository at this point in the history
Remove the $do_copy_tmp lex var and add a $ST0_already_assigned_to
lex var.

When deciding whether to emit

    ST(0) = ...

that decision should be based on whether the expanded typemap already
includes such an assignment. (Often it doesn't, because various
optimisations change it to 'RETVALSV = ' etc.)

This commit sets a variable which directly states whether this is true,
instead of relying on indirect state.

This commit should provide no change in functionality, but it makes the
code more robust against future changes, by not having to rely on all
branches knowing to set all the correct indirect variable values.
  • Loading branch information
iabyn committed Aug 23, 2024
1 parent a9dcb33 commit 4323ab6
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3907,7 +3907,8 @@ sub generate_output {
my @lines; # lines of code to eventually emit
my $use_RETVALSV = 1;
my $do_mortal = 0;
my $do_copy_tmp = 1;
# the typemap already includes 'ST(0) =' rather than 'RETVALSV = ' etc
my $ST0_already_assigned_to = 0;
my $want_newmortal = 0;

# Evaluate the typemap, expanding any vars like $var and $arg.
Expand Down Expand Up @@ -3995,7 +3996,6 @@ sub generate_output {
# This RE must be tried before next elsif, as is it effectively a
# special-case of the more general /\$arg =/ pattern.

$do_copy_tmp = 0; #$arg will be a ST(X), no SV* RETVAL, no RETVALSV
$use_RETVALSV = 0;
}
else {
Expand Down Expand Up @@ -4034,6 +4034,7 @@ sub generate_output {
}
else {
$evalexpr =~ s/\bRETVALSV\b/$orig_arg/g;
$ST0_already_assigned_to = 1;
}
}

Expand All @@ -4051,11 +4052,8 @@ sub generate_output {

# Emit the final 'ST(0) = RETVAL' or similar, unless ST(0)
# was already assigned to earlier directly by the typemap.
# The $do_copy_tmp condition (always true except for immortals)
# means that this is usually done. But for immortals we only do
# it if extra code has been emitted, i.e. mortalisation or set magic.
push @lines, "\t$orig_arg = RETVAL$sv;\n"
if $do_mortal || $do_setmagic || $do_copy_tmp;
unless $ST0_already_assigned_to;

if ($use_RETVALSV) {
# Add an extra 4-indent
Expand Down

0 comments on commit 4323ab6

Please sign in to comment.