summaryrefslogtreecommitdiff
path: root/pp_ctl.c
Commit message (Collapse)AuthorAgeFilesLines
* [perl #128182] Fix crash with require $nonstringFather Chrysostomos2017-07-281-2/+2
| | | | | | | | | | If something other than a plain string (e.g. a reference or typeglob) whose stringified form contains a null character is passed to require() or do(), it crashes, as of v5.19.3-130-gc8028aa, because the code in question that handles the error tries to read fields of the scalar that are only valid if it is a string internally. (cherry picked from commit 08f800f8519574aea9e744ff83230fb93772652b)
* Improve code comments for some ctx stuffDavid Mitchell2016-03-301-11/+18
| | | | | | | | | * in pp_return(), some comments were out of date about how leave_adjust_stacks() is called ; * add a comment to all the functions that pp_return() tail-calls to the effect that they can be tail-called; * make it clearer when/why OPf_SPECIAL is set on OP_LEAVE; * CXt_LOOP_PLAIN can be a while loop as well as a plain block.
* rename and function-ise dtrace macrosDavid Mitchell2016-03-181-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit: 1. Renames the various dtrace probe macros into a consistent and self-documenting pattern, e.g. ENTRY_PROBE => PERL_DTRACE_PROBE_ENTRY RETURN_PROBE => PERL_DTRACE_PROBE_RETURN Since they're supposed to be defined only under PERL_CORE, this shouldn't break anything that's not being naughty. 2. Implement the main body of these macros using a real function. They were formerly defined along the lines of if (PERL_SUB_ENTRY_ENABLED()) PERL_SUB_ENTRY(...); The PERL_SUB_ENTRY() part is a macro generated by the dtrace system, which for example on linux expands to a large bunch of assembly directives. Replace the direct macro with a function wrapper, e.g. if (PERL_SUB_ENTRY_ENABLED()) Perl_dtrace_probe_call(aTHX_ cv, TRUE); This reduces to once the number of times the macro is expanded. The new functions also take simpler args and then process the values they need using intermediate temporary vars to avoid huge macro expansions. For example ENTRY_PROBE(CvNAMED(cv) ? HEK_KEY(CvNAME_HEK(cv)) : GvENAME(CvGV(cv)), CopFILE((const COP *)CvSTART(cv)), CopLINE((const COP *)CvSTART(cv)), CopSTASHPV((const COP *)CvSTART(cv))); is now PERL_DTRACE_PROBE_ENTRY(cv); This reduces the executable size by 1K on -O2 -Dusedtrace builds, and by 45K on -DDEBUGGING -Dusedtrace builds.
* remove dSP from a couple of pp_enter* fnsDavid Mitchell2016-02-031-9/+7
| | | | | | | | These functions don't modify the args stack, so there's no need to dSP; ...; PUTBACK. Also write a negated bit test condition in pp_enterwhen() a bit less clumsily.
* make gimme consistently U8David Mitchell2016-02-031-24/+24
| | | | | | | | | | | | | The value of gimme stored in the context stack is U8. Make all other uses in the main core consistent with this. My primary motivation on this was that the new function cx_pushblock(), which I gave a 'U8 gimme' parameter, was generating warnings where callers were passing I32 gimme vars to it. Rather than play whack-a-mole, it seemed simpler to just uniformly use U8 everywhere. Porting/bench.pl shows a consistent reduction of about 2 instructions on the loop and sub benchmarks, so this change isn't harming performance.
* convert CX_{PUSH|POP}{WHEN|GIVEN} to inline fnsDavid Mitchell2016-02-031-6/+6
| | | | Replace CX_PUSHGIVEN() with cx_pushgiven() etc.
* convert CX_PUSHLOOP*/POPLOOP to inline fnsDavid Mitchell2016-02-031-5/+5
| | | | Replace CX_PUSHLOOP_FOR() with cx_pushfloop_for() etc.
* convert CX_PUSHEVAL/POPEVAL to inline fnsDavid Mitchell2016-02-031-11/+11
| | | | | | Replace CX_PUSHEVAL() with cx_pusheval() etc. No functional changes.
* convert CX_PUSHFORMAT/POPFORMAT to inline fnsDavid Mitchell2016-02-031-1/+1
| | | | | | Replace CX_PUSHFORMAT() with cx_pushformat() etc. No functional changes.
* convert CX_PUSHSUB/POPSUB to inline fnsDavid Mitchell2016-02-031-6/+10
| | | | | | Replace CX_PUSHSUB() with cx_pushsub() etc. No functional changes.
* convert CX_PUSH/POP/TOPBLOCK to inline fnsDavid Mitchell2016-02-031-30/+29
| | | | | | Replace CX_PUSHBLOCK() with cx_pushblock() etc. No functional changes.
* rename PUSHBLOCK,PUSHSUB etc to CX_PUSHBLOCK etcDavid Mitchell2016-02-031-19/+20
| | | | | | | Earlier all the POPFOO macros were renamed to CX_POPFOO to reflect the changed API (like POPBLOCK no longer decremented cxstack_ix). Now rename the PUSH ones for consistency.
* eliminate PUSH/POPBASICBLK macrosDavid Mitchell2016-02-031-6/+2
| | | | | | These are both NOOPs now, and were introduced within this branch as a temporary measure while extra stuff needed doing when pushing or popping a CXt_BOCK (pp_enter/pp_leave).
* pp_enteriter: add comment about setting cxt typeDavid Mitchell2016-02-031-0/+5
|
* PUSHEVAL: make n param an SV rather than a stringDavid Mitchell2016-02-031-3/+3
| | | | | | | | | | Rather than doing cx->blk_eval.old_namesv = (n ? newSVpv(n,0) : NULL); make the caller responsible for creating and passing in the SV. Since only only place (pp_require) passes a non-null value, this saves the other places having to test for nullness.
* PUSHSUB: make retop a parameterDavid Mitchell2016-02-031-2/+1
| | | | | | Rather than doing cx->blk_sub.retop = NULL in PUSHSUB, then relying on the caller to subsequently change it to something more useful, make it an arg to PUSHSUB.
* PUSHEVAL: make retop a parameterDavid Mitchell2016-02-031-10/+6
| | | | | | Rather than doing cx->blk_eval.retop = NULL in PUSHEVAL, then relying on the caller to subsequently change it to something more useful, make it an arg to PUSHEVAL.
* PUSHSUB: don't use implicit argsDavid Mitchell2016-02-031-2/+1
| | | | | Make cv and hasargs explicit parameters of PUSHSUB(), rather than just assuming that there are such vars in scope.
* PUSHBLOCK: don't use implicit argsDavid Mitchell2016-02-031-9/+9
| | | | | Make gimme a parameter of PUSHBLOCK() rather than just assuming that there's a 'gimme' var in scope.
* move PL_savestack_ix saving into PUSHBLOCKDavid Mitchell2016-02-031-13/+9
| | | | | | | | | | | | Currently blku_oldsaveix was being set by the various PUSHFOO macros, except for PUSHSUB and PUSHEVAL which expected their caller to do it manually. Now that all the main context state is stored on the context stack rather than than some on the save stack too, things are a lot simpler, and this messy transitional state can now be rationalised, whereby blku_oldsaveix is now always set by PUSHBLOCK; the exact value being specified by a new arg to PUSHBLOCK.
* eliminate PERL_STACK_OVERFLOW_CHECKDavid Mitchell2016-02-031-3/+1
| | | | | | | | This macro is defined as NOOP on all platforms except for MacOS classic, where it was added as a hook to allow for OSes that have a small CPU stack size. Since pp_entersub et al don't actually use the CPU stack, this hook looks misconceived from the beginning. So remove all uses of it in the core.
* rename POPFOO() to CX_POPFOO()David Mitchell2016-02-031-45/+45
| | | | | | | | | | | | | | | | Rename all the context-popping macros such as POPBLOCK and POPSUB, by giving them a CX_ prefix. (Do TOPBLOCK too). This is principally to deliberately break any existing non-core use of these non-API macros, as their behaviour has changed in this branch. In particular, POPBLOCK(cx) no longer decrements the cxt stack pointer nor sets cx; instead, cx is now expected to already point to the stack frame which POPBLOCK should process. At the same time, giving them a CX_ prefix makes it clearer that these are all part of a family of macros that manipulate the context stack. The PUSHFOO() macros will be renamed in a later commit.
* pp_redo()): reorder some stuffDavid Mitchell2016-02-031-2/+2
| | | | | | It probably doesn't make any difference, but reorder the FREETMPS, CX_LEAVE_SCOPE and TOPBLOCK so it matches the order we do things when leaving a scope.
* optimise bare 'next'David Mitchell2016-02-031-1/+4
| | | | | If a 'next' without a label appears directly in the scope of the current loop, then skip searching the context stack for a suitable LOOP context.
* make S_unwind_loop static againDavid Mitchell2016-02-031-1/+1
| | | | The previous commit but one accidentally removed the 'static' declaration
* S_unwind_loop(): remove opname paramDavid Mitchell2016-02-031-8/+8
| | | | | This is only used for error messages, and can be derived from OP_NAME(PL_op); so for efficiency, don't pass it.
* S_unwind_loop(): return pointer rather than indexDavid Mitchell2016-02-031-12/+9
| | | | | return &cxstack[cxix] rather than cxix, since this is what the caller actually needs.
* dounwind(): do a POPBLOCK for final cx frame.David Mitchell2016-02-031-7/+13
| | | | | | | | | | | | | | | | | | | | | Previously dounwind() relied on the caller to a TOPBLOCK or POPBLOCK following the call to dounwind(). It's debatable who should be responsible. Arguably its more efficient for dounwind() not to do a POPBLOCK, since the caller will probably immediately follow on with POPFOO; POPBLOCK for the next context frame anyway. Logically however, dounwind() should do this, and its not possible for the caller to do so retrospectively, as context frame cxstack_ix + 1 may have been overwritten by the time dounwind returns. Also, the changes in this branch mean that the old PL_tmps_floor is now saved in the context struct rather than on the save stack, so code that does C<dounwind(-1); LEAVE_SCOPE();> will no longer automatically restore PL_tmps_floor. With thiis commit, it will. The change to pp_return reflects that we now need to copy any return args *before* donwind() is called, so that "return $1" will mg_get($1) while the correct (inner) PL_curpm is still in scope.
* pp_break(): don't use TOPBLOCKDavid Mitchell2016-02-031-1/+1
| | | | | | | It appears to be using TOPBLOCK purely for its effect of resetting PL_stack_sp. Since the next op will be pp_leavegiven which will do a POPBLOCK, the other actions of TOPBLOCK are redundant. So just set PL_stack_sp directly.
* only set CXp_FOR_DEF with CXp_FOR_GVDavid Mitchell2016-02-031-4/+5
| | | | | | | | | | C<for (...)> is a special-case of C<for $pkg_var (...)>, so CXp_FOR_DEF should only be set when CXp_FOR_GV also is. So in pp_enteriter(), only test for (PL_op->op_private & OPpITER_DEF) in the GV branch and assert that it's otherwise unset. This is slightly more efficient in the non-GV branches. Also add some more commentary to the CXp_FOR_* flag definitions.
* give POP_SAVEARRAY() macro a cx argDavid Mitchell2016-02-031-1/+1
| | | | rather than just assuming that there's a 'cx' var in scope
* clarify code comment in pp_goto(()David Mitchell2016-02-031-1/+1
|
* fix *_ = "" for 0 .. 1;David Mitchell2016-02-031-6/+2
| | | | | | | | | | | | | | | | | | | | | | | | RT #123994 pp_iter couldn't handle GvSv(gv) being NULL. In that ticket, Tony Cook suggested two possible fixes. First, always instantiate the GvSV slot at the start of pp_iter by using GvSVn rather than GvSV in the CxITERVAR() macro; Second, test for it being null within the two 'range' branches, (for(1..9), for('a'..'z')), and if so create it. One advantage of doing it there is that there's already code for (re)creating the SV if the reference count is != 1. It also means that the list and array cases (for(@a), for(1,3,5)) which always put the next iterated SV into the pad/GvSV slot don't waste time creating and then immediately discarding an SV if GvSV was NULL. I went for the second fix. It also means that's there's no longer any need in pp_enteriter to initially poulate GvSV is it was null, as this will be detected during the first pp_iter() anyway.
* pp_enteriter: use efficient SvREFCNT_inc variantDavid Mitchell2016-02-031-2/+8
| | | | | | replace a couple of SvREFCNT_inc() with SvREFCNT_inc_simple_void_NN(). Also add a couple of code comments.
* provide some basic documentation for Perl_dounwindDavid Mitchell2016-02-031-0/+9
|
* pp_ctl.c: s/newsp/oldsp/gDavid Mitchell2016-02-031-29/+29
| | | | | | | | | | | | There are various pp_leavefoo() functions that have similar code along the lines of SV **newsp; newsp = PL_stack_base + cx->blk_oldsp; Rename all these vars to 'oldsp' to reduce the cognitive dissonance. The name 'newsp' was a hangover from when POPBLOCK used to implicitly set a var called 'newsp.
* replace leave_common() with leave_adjust_stacks()David Mitchell2016-02-031-67/+9
| | | | | | | | | | Make the remaining callers of S_leave_common() use leave_adjust_stacks() instead, then delete this static function. This brings the benefits of freeing TEMPS on all scope exists that has already been introduced on sub exits; uses the optimised code for creating mortal copies; and finally unifies all the different 'process return args on scope exit' implementations into single function.
* make pp_return() use leave_adjust_stacks()David Mitchell2016-02-031-12/+13
| | | | | | | It was using S_leave_common(), but that's shortly to be removed. It also required adding an extra arg to leave_adjust_stacks() to indicate where to shift the return args to. This will also be needed for when we replace the remaining uses of S_leave_common() with leave_adjust_stacks().
* make pp_leavesublv use S_leavesub_adjust_stacks()David Mitchell2016-02-031-93/+68
| | | | | | | | | | | | | | | | | | | | | | | Currently S_leavesub_adjust_stacks() is just used by pp_leavesub. Rename it to Perl_leave_adjust_stacks(), extend its functionality slightly, then make pp_leavesublv() use it too. This means that lvalue sub exit gains the benefit of FREETMPS being done, and (where mortal copying needs doing) the optimised copying code. It also means there is now one less version of the "process args on scope exit" code. pp_leavesublv() still does a scan of its return args looking for things to croak() on, but leaves everything else to leave_adjust_stacks(). leave_adjust_stacks() is intended shortly to be used in place of S_leave_common() too, thus unifying all args-on-scope-exit code. The changes to leave_adjust_stacks() in this commit (apart from the renaming and doc changes) are: * a new arg to indicate what condition to use to decide whether to pass or copy the arg; * a new branch to mortalise and ref count bump an arg
* pp_leavesublv(): document PL_sv_undef exceptionDavid Mitchell2016-02-031-0/+6
|
* pp_leavesublv(): croak on *all* PADTMPsDavid Mitchell2016-02-031-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | pp_leavesublv() generally croaks on returned PADTMPs when called in lvalue context. However, an exception was made in scalar context if the PADTMP had set magic. This was to allow for things like sub :lvalue { $tied{foo} } and sub :lvalue { substr($foo,1,2) } However, it was later found that in places like pp_substr(), when returning a PVLV, it should return a new mortal rather than upgrading its pad target to PVLV, because the PVLV holds a reference to $foo which then gets delayed being freed indefinitely. Since places like pp_susbtr() no longer return lvalue PADTMPs, there's no longer any need to make a special exception in pp_leavesublv(). I've added an assertion to the effect that PADTMPs don't have set container magic, but I've added the assert to pp_leavesub() rather than pp_leavesublv(), since the former is much likely to be used in many weird situations and edge cases that would quickly flush out any false assumptions. If this assumption is wrong and the exception needs to be re-instated in pp_leavesublv(), then presumably it needs adding to the ARRAY context branch too - I'm assuming that its previous absence there was an oversight; i.e. sub foo :lvalue { $tied{foo}, substr($foo,1,2) } foo() = (1,2); should work too.
* extend magic copy test to all scope exit typesDavid Mitchell2016-02-031-2/+4
| | | | | | | | | | | Commit v5.15.6-387-g6f48390 forced leavesub to copy returned items if they were get-magical. Normally rvalue subs are supposed to return a copy of their return args, but that copy is sometimes skipped if leavesub thinks the side-effects will never be visible. Tied elements was an example where the implementation leaked. However, this applies equally well to other leave types, such as do { ....}, so test for get magic in those too.
* Always copy return values when exiting scopeDavid Mitchell2016-02-031-9/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | v5.14.0-642-g3ed94dc fixed certain instances where returning from a sub incorrectly returned the actual value rather than a copy, e.g. sub f { delete $foo{bar} } This was because if the value(s) being returned had SvTEMP set, copying was skipped. That commit added an extra condition to the skip test, SvREFCNT(sv) == 1. However, this applies equally well to other scope exits, for example do { ...; delete $foo{bar} } So this commits adds the RC==1 test to S_leave_common() too, which handles all the non-sub scope exits. As well as adding a test to do.t, it adds an additional test to sub.t, since the original tests, although they *detected* a non-copied return, didn't actually demonstrate a case where it was actually harmful. Note that S_leave_common() also sometimes skips on PADTMPs as well as TEMPs, so this commit as a side-effect also makes it copy PADTMPs unless their RC ==1. But since their RC should in fact almost always be 1 anyway, in practice it makes no difference.
* make TOPBLOCK(cx) not set cxDavid Mitchell2016-02-031-0/+6
| | | | | | | | | | | | | | i.e. replace TOPBLOCK(cx); with cx = CX_CUR(); TOPBLOCK(cx); This is part of general trend of separating out the cx/cxstack_ix setting/manipulation from the save/restore actions
* rename DEBUG_CX() to CX_DEBUG()David Mitchell2016-02-031-2/+3
| | | | | For consistency with the new naming convention for context-stack macros. Also, give it a cx arg rather than using CX_CUR()
* add CX_CUR() macroDavid Mitchell2016-02-031-24/+24
| | | | | | This is simply #define CX_CUR() (&cxstack[cxstack_ix])
* move and rename cx_old_savestack_ixDavid Mitchell2016-02-031-4/+4
| | | | | | | | | | | Earlier on in this branch I temporarily added cx_old_savestack_ix as the first element in the context struct. This commit moves it down 32 bits so it now follows type/gimme/u16 in the sbu and blku unions. This means that type is now back as the first item in the struct. I've also renamed it blku_oldsaveix to be more in keeping with similar field names.
* Eliminate cx->blk_loop.resetspDavid Mitchell2016-02-031-9/+12
| | | | | | | | | | | | | | | Of all the loop types, only CXt_LOOP_LIST, i.e. for (1,2,3) {}, needs to keep anything on the stack, i.e. for all the others, cx->blk_loop.resetsp can always equal cx->blk_oldsp. For CXt_LOOP_LIST, the same value as resetsp is stored as blk_loop.state_u.stack.basesp. So we can eliminate the resetsp field. This is good as the loop substruct is the largest (ITHREADS) or joint largest with eval (non-threaded). Also, we now don't have to store that value for non-CXt_LOOP_LIST loops. The downside is we now have to choose between basesp and oldsp in pp_leaveloop and pp_last, based on CX type.
* pp_iter(): optimise stack handlingDavid Mitchell2016-02-031-0/+4
| | | | | | | | | Make pp_enteriter() do EXTEND(SP,1); then there's no need for pp_iter() to check for space to push PL_sv_yes/no each time round the loop. Since the only stack manipulation in pp_iter is now just pushing a boolean at the end, remove dSP etc and just directly push to PL_stack_sp at the end.
* split CXt_LOOP_FOR into CXt_LOOP_LIST,CXt_LOOP_ARYDavid Mitchell2016-02-031-43/+36
| | | | | | | | | | | | | | | Create a new context type so that "for (1,2,3)" and "for (@ary)" are now two separate types. For the list type, we store the index of the base stack element in the state union rather than having an array pointer. Currently this is just the same as blk_resetsp, but this will shortly allow us to eliminate the resetsp field from the struct block_loop - which is currently the largest sub-struct within the block union. Having two separate types also allows the two cases to be handled directly in the main switch in the hot pp_iter code, rather than having extra conditionals.