This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix typo in comment about Configure modification
[perl5.git] / pp_ctl.c
CommitLineData
a0d0e21e
LW
1/* pp_ctl.c
2 *
1129b882
NC
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
a0d0e21e
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
11/*
4ac71550
TC
12 * Now far ahead the Road has gone,
13 * And I must follow, if I can,
14 * Pursuing it with eager feet,
15 * Until it joins some larger way
16 * Where many paths and errands meet.
17 * And whither then? I cannot say.
18 *
19 * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
a0d0e21e
LW
20 */
21
166f8a29
DM
22/* This file contains control-oriented pp ("push/pop") functions that
23 * execute the opcodes that make up a perl program. A typical pp function
24 * expects to find its arguments on the stack, and usually pushes its
25 * results onto the stack, hence the 'pp' terminology. Each OP structure
26 * contains a pointer to the relevant pp_foo() function.
27 *
28 * Control-oriented means things like pp_enteriter() and pp_next(), which
29 * alter the flow of control of the program.
30 */
31
32
a0d0e21e 33#include "EXTERN.h"
864dbfa3 34#define PERL_IN_PP_CTL_C
a0d0e21e
LW
35#include "perl.h"
36
d7e3f70f
Z
37#define RUN_PP_CATCHABLY(thispp) \
38 STMT_START { if (CATCH_GET) return docatch(thispp); } STMT_END
1e422769 39
94fcd414
NC
40#define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
41
a0d0e21e
LW
42PP(pp_wantarray)
43{
39644a26 44 dSP;
a0d0e21e 45 I32 cxix;
93f0bc49 46 const PERL_CONTEXT *cx;
a0d0e21e
LW
47 EXTEND(SP, 1);
48
93f0bc49
FC
49 if (PL_op->op_private & OPpOFFBYONE) {
50 if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF;
51 }
52 else {
53 cxix = dopoptosub(cxstack_ix);
54 if (cxix < 0)
a0d0e21e 55 RETPUSHUNDEF;
93f0bc49
FC
56 cx = &cxstack[cxix];
57 }
a0d0e21e 58
93f0bc49 59 switch (cx->blk_gimme) {
54310121 60 case G_ARRAY:
a0d0e21e 61 RETPUSHYES;
54310121 62 case G_SCALAR:
a0d0e21e 63 RETPUSHNO;
54310121
PP
64 default:
65 RETPUSHUNDEF;
66 }
a0d0e21e
LW
67}
68
2cd61cdb
IZ
69PP(pp_regcreset)
70{
0b4182de 71 TAINT_NOT;
2cd61cdb
IZ
72 return NORMAL;
73}
74
b3eb6a9b
GS
75PP(pp_regcomp)
76{
39644a26 77 dSP;
eb578fdb 78 PMOP *pm = (PMOP*)cLOGOP->op_other;
9f141731 79 SV **args;
df787a7b 80 int nargs;
84679df5 81 REGEXP *re = NULL;
9f141731
DM
82 REGEXP *new_re;
83 const regexp_engine *eng;
dbc200c5 84 bool is_bare_re= FALSE;
bfed75c6 85
df787a7b
DM
86 if (PL_op->op_flags & OPf_STACKED) {
87 dMARK;
88 nargs = SP - MARK;
89 args = ++MARK;
90 }
91 else {
92 nargs = 1;
93 args = SP;
94 }
95
4b5a0d1c 96 /* prevent recompiling under /o and ithreads. */
3db8f154 97#if defined(USE_ITHREADS)
131b3ad0 98 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
df787a7b 99 SP = args-1;
131b3ad0
DM
100 RETURN;
101 }
513629ba 102#endif
d4b87e75 103
9f141731
DM
104 re = PM_GETRE(pm);
105 assert (re != (REGEXP*) &PL_sv_undef);
106 eng = re ? RX_ENGINE(re) : current_re_engine();
107
3c13cae6
DM
108 new_re = (eng->op_comp
109 ? eng->op_comp
110 : &Perl_re_op_compile
111 )(aTHX_ args, nargs, pm->op_code_list, eng, re,
346d3070 112 &is_bare_re,
dbc200c5 113 (pm->op_pmflags & RXf_PMf_FLAGCOPYMASK),
a5ae69f0
DM
114 pm->op_pmflags |
115 (PL_op->op_flags & OPf_SPECIAL ? PMf_USE_RE_EVAL : 0));
dbc200c5 116
346d3070 117 if (pm->op_pmflags & PMf_HAS_CV)
8d919b0a 118 ReANY(new_re)->qr_anoncv
9fe3265f 119 = (CV*) SvREFCNT_inc(PAD_SV(PL_op->op_targ));
9f141731
DM
120
121 if (is_bare_re) {
122 REGEXP *tmp;
123 /* The match's LHS's get-magic might need to access this op's regexp
124 (e.g. $' =~ /$re/ while foo; see bug 70764). So we must call
125 get-magic now before we replace the regexp. Hopefully this hack can
126 be replaced with the approach described at
127 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03/msg122415.html
128 some day. */
129 if (pm->op_type == OP_MATCH) {
130 SV *lhs;
284167a5 131 const bool was_tainted = TAINT_get;
9f141731
DM
132 if (pm->op_flags & OPf_STACKED)
133 lhs = args[-1];
6ffceeb7 134 else if (pm->op_targ)
9f141731
DM
135 lhs = PAD_SV(pm->op_targ);
136 else lhs = DEFSV;
137 SvGETMAGIC(lhs);
138 /* Restore the previous value of PL_tainted (which may have been
139 modified by get-magic), to avoid incorrectly setting the
284167a5
SM
140 RXf_TAINTED flag with RX_TAINT_on further down. */
141 TAINT_set(was_tainted);
dc6d7f5c 142#ifdef NO_TAINT_SUPPORT
9a9b5ec9
DM
143 PERL_UNUSED_VAR(was_tainted);
144#endif
df787a7b 145 }
9f141731
DM
146 tmp = reg_temp_copy(NULL, new_re);
147 ReREFCNT_dec(new_re);
148 new_re = tmp;
df787a7b 149 }
dbc200c5 150
9f141731
DM
151 if (re != new_re) {
152 ReREFCNT_dec(re);
153 PM_SETRE(pm, new_re);
c277df42 154 }
d4b87e75 155
dbc200c5 156
d48c660d
DM
157 assert(TAINTING_get || !TAINT_get);
158 if (TAINT_get) {
9f141731 159 SvTAINTED_on((SV*)new_re);
284167a5 160 RX_TAINT_on(new_re);
72311751 161 }
72311751 162
5585e758
YO
163 /* handle the empty pattern */
164 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm) {
165 if (PL_curpm == PL_reg_curpm) {
3cb4cde3
TC
166 if (PL_curpm_under && PL_curpm_under == PL_reg_curpm) {
167 Perl_croak(aTHX_ "Infinite recursion via empty pattern");
5585e758 168 }
5585e758
YO
169 }
170 }
171
c737faaf
YO
172#if !defined(USE_ITHREADS)
173 /* can't change the optree at runtime either */
174 /* PMf_KEEP is handled differently under threads to avoid these problems */
a0d0e21e 175 if (pm->op_pmflags & PMf_KEEP) {
533c011a 176 cLOGOP->op_first->op_next = PL_op->op_next;
a0d0e21e 177 }
c737faaf 178#endif
9f141731 179
df787a7b 180 SP = args-1;
a0d0e21e
LW
181 RETURN;
182}
183
9f141731 184
a0d0e21e
LW
185PP(pp_substcont)
186{
39644a26 187 dSP;
4ebe6e95 188 PERL_CONTEXT *cx = CX_CUR();
eb578fdb
KW
189 PMOP * const pm = (PMOP*) cLOGOP->op_other;
190 SV * const dstr = cx->sb_dstr;
191 char *s = cx->sb_s;
192 char *m = cx->sb_m;
a0d0e21e 193 char *orig = cx->sb_orig;
eb578fdb 194 REGEXP * const rx = cx->sb_rx;
c445ea15 195 SV *nsv = NULL;
988e6e7e 196 REGEXP *old = PM_GETRE(pm);
f410a211
NC
197
198 PERL_ASYNC_CHECK();
199
988e6e7e 200 if(old != rx) {
bfed75c6 201 if(old)
988e6e7e 202 ReREFCNT_dec(old);
d6106309 203 PM_SETRE(pm,ReREFCNT_inc(rx));
d8f2cf8a
AB
204 }
205
d9f97599 206 rxres_restore(&cx->sb_rxres, rx);
c90c0ff4 207
a0d0e21e 208 if (cx->sb_iters++) {
3c6ef0a5 209 const SSize_t saviters = cx->sb_iters;
a0d0e21e 210 if (cx->sb_iters > cx->sb_maxiters)
cea2e8a9 211 DIE(aTHX_ "Substitution loop");
a0d0e21e 212
447ee134
DM
213 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
214
ef07e810 215 /* See "how taint works" above pp_subst() */
20be6587
DM
216 if (SvTAINTED(TOPs))
217 cx->sb_rxtainted |= SUBST_TAINT_REPL;
447ee134 218 sv_catsv_nomg(dstr, POPs);
2c296965 219 if (CxONCE(cx) || s < orig ||
03c83e26
DM
220 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
221 (s == m), cx->sb_targ, NULL,
d5e7783a 222 (REXEC_IGNOREPOS|REXEC_NOT_FIRST|REXEC_FAIL_ON_UNDERFLOW)))
a0d0e21e 223 {
8ca8a454 224 SV *targ = cx->sb_targ;
748a9306 225
078c425b
JH
226 assert(cx->sb_strend >= s);
227 if(cx->sb_strend > s) {
228 if (DO_UTF8(dstr) && !SvUTF8(targ))
4bac9ae4 229 sv_catpvn_nomg_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
078c425b 230 else
4bac9ae4 231 sv_catpvn_nomg(dstr, s, cx->sb_strend - s);
078c425b 232 }
20be6587
DM
233 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
234 cx->sb_rxtainted |= SUBST_TAINT_PAT;
9212bbba 235
8ca8a454
NC
236 if (pm->op_pmflags & PMf_NONDESTRUCT) {
237 PUSHs(dstr);
238 /* From here on down we're using the copy, and leaving the
239 original untouched. */
240 targ = dstr;
241 }
242 else {
9e0ea7f3
FC
243 SV_CHECK_THINKFIRST_COW_DROP(targ);
244 if (isGV(targ)) Perl_croak_no_modify();
245 SvPV_free(targ);
8ca8a454
NC
246 SvPV_set(targ, SvPVX(dstr));
247 SvCUR_set(targ, SvCUR(dstr));
248 SvLEN_set(targ, SvLEN(dstr));
249 if (DO_UTF8(dstr))
250 SvUTF8_on(targ);
251 SvPV_set(dstr, NULL);
252
52c47e16 253 PL_tainted = 0;
4f4d7508 254 mPUSHi(saviters - 1);
48c036b1 255
8ca8a454
NC
256 (void)SvPOK_only_UTF8(targ);
257 }
5cd24f17 258
20be6587 259 /* update the taint state of various various variables in
ef07e810
DM
260 * preparation for final exit.
261 * See "how taint works" above pp_subst() */
284167a5 262 if (TAINTING_get) {
20be6587
DM
263 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
264 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
265 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
266 )
267 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
268
269 if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
270 && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
271 )
272 SvTAINTED_on(TOPs); /* taint return value */
273 /* needed for mg_set below */
284167a5
SM
274 TAINT_set(
275 cBOOL(cx->sb_rxtainted &
276 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
277 );
20be6587
DM
278 SvTAINT(TARG);
279 }
280 /* PL_tainted must be correctly set for this mg_set */
281 SvSETMAGIC(TARG);
282 TAINT_NOT;
80d88db0 283
8e7e33ef 284 CX_LEAVE_SCOPE(cx);
b405d38b 285 CX_POPSUBST(cx);
5da525e9 286 CX_POP(cx);
80d88db0 287
47c9d59f 288 PERL_ASYNC_CHECK();
a0d0e21e 289 RETURNOP(pm->op_next);
e5964223 290 NOT_REACHED; /* NOTREACHED */
a0d0e21e 291 }
8e5e9ebe 292 cx->sb_iters = saviters;
a0d0e21e 293 }
07bc277f 294 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
295 m = s;
296 s = orig;
6502e081 297 assert(!RX_SUBOFFSET(rx));
07bc277f 298 cx->sb_orig = orig = RX_SUBBEG(rx);
a0d0e21e
LW
299 s = orig + (m - s);
300 cx->sb_strend = s + (cx->sb_strend - m);
301 }
07bc277f 302 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
db79b45b 303 if (m > s) {
bfed75c6 304 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
4bac9ae4 305 sv_catpvn_nomg_utf8_upgrade(dstr, s, m - s, nsv);
db79b45b 306 else
4bac9ae4 307 sv_catpvn_nomg(dstr, s, m-s);
db79b45b 308 }
07bc277f 309 cx->sb_s = RX_OFFS(rx)[0].end + orig;
084916e3 310 { /* Update the pos() information. */
8ca8a454
NC
311 SV * const sv
312 = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ;
084916e3 313 MAGIC *mg;
a911bb25
DM
314
315 /* the string being matched against may no longer be a string,
316 * e.g. $_=0; s/.../$_++/ge */
317
318 if (!SvPOK(sv))
319 SvPV_force_nomg_nolen(sv);
320
96c2a8ff
FC
321 if (!(mg = mg_find_mglob(sv))) {
322 mg = sv_magicext_mglob(sv);
084916e3 323 }
cda67c99 324 MgBYTEPOS_set(mg, sv, SvPVX(sv), m - orig);
084916e3 325 }
988e6e7e 326 if (old != rx)
d6106309 327 (void)ReREFCNT_inc(rx);
20be6587 328 /* update the taint state of various various variables in preparation
ef07e810
DM
329 * for calling the code block.
330 * See "how taint works" above pp_subst() */
284167a5 331 if (TAINTING_get) {
20be6587
DM
332 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
333 cx->sb_rxtainted |= SUBST_TAINT_PAT;
334
335 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
336 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
337 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
338 )
339 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
340
341 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
342 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
8ca8a454
NC
343 SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT)
344 ? cx->sb_dstr : cx->sb_targ);
20be6587
DM
345 TAINT_NOT;
346 }
d9f97599 347 rxres_save(&cx->sb_rxres, rx);
af9838cc 348 PL_curpm = pm;
29f2e912 349 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
a0d0e21e
LW
350}
351
c90c0ff4 352void
864dbfa3 353Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4
PP
354{
355 UV *p = (UV*)*rsp;
356 U32 i;
7918f24d
NC
357
358 PERL_ARGS_ASSERT_RXRES_SAVE;
96a5add6 359 PERL_UNUSED_CONTEXT;
c90c0ff4 360
07bc277f 361 if (!p || p[1] < RX_NPARENS(rx)) {
db2c6cb3 362#ifdef PERL_ANY_COW
6502e081 363 i = 7 + (RX_NPARENS(rx)+1) * 2;
ed252734 364#else
6502e081 365 i = 6 + (RX_NPARENS(rx)+1) * 2;
ed252734 366#endif
c90c0ff4 367 if (!p)
a02a5408 368 Newx(p, i, UV);
c90c0ff4
PP
369 else
370 Renew(p, i, UV);
371 *rsp = (void*)p;
372 }
373
5eabab15
DM
374 /* what (if anything) to free on croak */
375 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
cf93c79d 376 RX_MATCH_COPIED_off(rx);
6c31ff74 377 *p++ = RX_NPARENS(rx);
c90c0ff4 378
db2c6cb3 379#ifdef PERL_ANY_COW
bdd9a1b1
NC
380 *p++ = PTR2UV(RX_SAVED_COPY(rx));
381 RX_SAVED_COPY(rx) = NULL;
ed252734
NC
382#endif
383
07bc277f
NC
384 *p++ = PTR2UV(RX_SUBBEG(rx));
385 *p++ = (UV)RX_SUBLEN(rx);
6502e081
DM
386 *p++ = (UV)RX_SUBOFFSET(rx);
387 *p++ = (UV)RX_SUBCOFFSET(rx);
07bc277f
NC
388 for (i = 0; i <= RX_NPARENS(rx); ++i) {
389 *p++ = (UV)RX_OFFS(rx)[i].start;
390 *p++ = (UV)RX_OFFS(rx)[i].end;
c90c0ff4
PP
391 }
392}
393
9c105995
NC
394static void
395S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4
PP
396{
397 UV *p = (UV*)*rsp;
398 U32 i;
7918f24d
NC
399
400 PERL_ARGS_ASSERT_RXRES_RESTORE;
96a5add6 401 PERL_UNUSED_CONTEXT;
c90c0ff4 402
ed252734 403 RX_MATCH_COPY_FREE(rx);
cf93c79d 404 RX_MATCH_COPIED_set(rx, *p);
c90c0ff4 405 *p++ = 0;
6c31ff74 406 RX_NPARENS(rx) = *p++;
c90c0ff4 407
db2c6cb3 408#ifdef PERL_ANY_COW
bdd9a1b1
NC
409 if (RX_SAVED_COPY(rx))
410 SvREFCNT_dec (RX_SAVED_COPY(rx));
411 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
ed252734
NC
412 *p++ = 0;
413#endif
414
07bc277f
NC
415 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
416 RX_SUBLEN(rx) = (I32)(*p++);
6502e081
DM
417 RX_SUBOFFSET(rx) = (I32)*p++;
418 RX_SUBCOFFSET(rx) = (I32)*p++;
07bc277f
NC
419 for (i = 0; i <= RX_NPARENS(rx); ++i) {
420 RX_OFFS(rx)[i].start = (I32)(*p++);
421 RX_OFFS(rx)[i].end = (I32)(*p++);
c90c0ff4
PP
422 }
423}
424
9c105995
NC
425static void
426S_rxres_free(pTHX_ void **rsp)
c90c0ff4 427{
44f8325f 428 UV * const p = (UV*)*rsp;
7918f24d
NC
429
430 PERL_ARGS_ASSERT_RXRES_FREE;
96a5add6 431 PERL_UNUSED_CONTEXT;
c90c0ff4
PP
432
433 if (p) {
94010e71 434 void *tmp = INT2PTR(char*,*p);
6c31ff74 435#ifdef PERL_POISON
db2c6cb3 436#ifdef PERL_ANY_COW
6c31ff74 437 U32 i = 9 + p[1] * 2;
94010e71 438#else
6c31ff74 439 U32 i = 8 + p[1] * 2;
94010e71 440#endif
6c31ff74
NC
441#endif
442
db2c6cb3 443#ifdef PERL_ANY_COW
6c31ff74 444 SvREFCNT_dec (INT2PTR(SV*,p[2]));
ed252734 445#endif
6c31ff74
NC
446#ifdef PERL_POISON
447 PoisonFree(p, i, sizeof(UV));
448#endif
449
450 Safefree(tmp);
c90c0ff4 451 Safefree(p);
4608196e 452 *rsp = NULL;
c90c0ff4
PP
453 }
454}
455
a701009a
DM
456#define FORM_NUM_BLANK (1<<30)
457#define FORM_NUM_POINT (1<<29)
458
a0d0e21e
LW
459PP(pp_formline)
460{
20b7effb 461 dSP; dMARK; dORIGMARK;
eb578fdb 462 SV * const tmpForm = *++MARK;
086b26f3 463 SV *formsv; /* contains text of original format */
eb578fdb
KW
464 U32 *fpc; /* format ops program counter */
465 char *t; /* current append position in target string */
086b26f3 466 const char *f; /* current position in format string */
eb578fdb
KW
467 I32 arg;
468 SV *sv = NULL; /* current item */
086b26f3 469 const char *item = NULL;/* string value of current item */
9b4bdfd4
DM
470 I32 itemsize = 0; /* length (chars) of item, possibly truncated */
471 I32 itembytes = 0; /* as itemsize, but length in bytes */
086b26f3
DM
472 I32 fieldsize = 0; /* width of current field */
473 I32 lines = 0; /* number of lines that have been output */
474 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
475 const char *chophere = NULL; /* where to chop current item */
f5ada144 476 STRLEN linemark = 0; /* pos of start of line in output */
65202027 477 NV value;
086b26f3 478 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
9b4bdfd4 479 STRLEN len; /* length of current sv */
732f08da 480 STRLEN linemax; /* estimate of output size in bytes */
1bd51a4c
IH
481 bool item_is_utf8 = FALSE;
482 bool targ_is_utf8 = FALSE;
bd7084a6 483 const char *fmt;
74e0ddf7 484 MAGIC *mg = NULL;
4ff700b9
DM
485 U8 *source; /* source of bytes to append */
486 STRLEN to_copy; /* how may bytes to append */
ea60cfe8 487 char trans; /* what chars to translate */
92ff660b 488 bool copied_form = FALSE; /* have we duplicated the form? */
74e0ddf7 489
3808a683 490 mg = doparseform(tmpForm);
a0d0e21e 491
74e0ddf7 492 fpc = (U32*)mg->mg_ptr;
3808a683
DM
493 /* the actual string the format was compiled from.
494 * with overload etc, this may not match tmpForm */
495 formsv = mg->mg_obj;
496
74e0ddf7 497
3280af22 498 SvPV_force(PL_formtarget, len);
3808a683 499 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
125b9982 500 SvTAINTED_on(PL_formtarget);
1bd51a4c
IH
501 if (DO_UTF8(PL_formtarget))
502 targ_is_utf8 = TRUE;
732f08da
DM
503 /* this is an initial estimate of how much output buffer space
504 * to allocate. It may be exceeded later */
505 linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
506 t = SvGROW(PL_formtarget, len + linemax + 1);
26e935cf 507 /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
a0d0e21e 508 t += len;
3808a683 509 f = SvPV_const(formsv, len);
a0d0e21e
LW
510
511 for (;;) {
512 DEBUG_f( {
bfed75c6 513 const char *name = "???";
a0d0e21e
LW
514 arg = -1;
515 switch (*fpc) {
516 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
517 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
518 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
519 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
520 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
521
522 case FF_CHECKNL: name = "CHECKNL"; break;
523 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
524 case FF_SPACE: name = "SPACE"; break;
525 case FF_HALFSPACE: name = "HALFSPACE"; break;
526 case FF_ITEM: name = "ITEM"; break;
527 case FF_CHOP: name = "CHOP"; break;
528 case FF_LINEGLOB: name = "LINEGLOB"; break;
529 case FF_NEWLINE: name = "NEWLINE"; break;
530 case FF_MORE: name = "MORE"; break;
531 case FF_LINEMARK: name = "LINEMARK"; break;
532 case FF_END: name = "END"; break;
bfed75c6 533 case FF_0DECIMAL: name = "0DECIMAL"; break;
a1b95068 534 case FF_LINESNGL: name = "LINESNGL"; break;
a0d0e21e
LW
535 }
536 if (arg >= 0)
bf49b057 537 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
a0d0e21e 538 else
bf49b057 539 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
5f80b19c 540 } );
a0d0e21e 541 switch (*fpc++) {
4a73dc0b 542 case FF_LINEMARK: /* start (or end) of a line */
f5ada144 543 linemark = t - SvPVX(PL_formtarget);
a0d0e21e
LW
544 lines++;
545 gotsome = FALSE;
546 break;
547
4a73dc0b 548 case FF_LITERAL: /* append <arg> literal chars */
ea60cfe8
DM
549 to_copy = *fpc++;
550 source = (U8 *)f;
551 f += to_copy;
552 trans = '~';
75645721 553 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
ea60cfe8 554 goto append;
a0d0e21e 555
4a73dc0b 556 case FF_SKIP: /* skip <arg> chars in format */
a0d0e21e
LW
557 f += *fpc++;
558 break;
559
4a73dc0b 560 case FF_FETCH: /* get next item and set field size to <arg> */
a0d0e21e
LW
561 arg = *fpc++;
562 f += arg;
563 fieldsize = arg;
564
565 if (MARK < SP)
566 sv = *++MARK;
567 else {
3280af22 568 sv = &PL_sv_no;
a2a5de95 569 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
a0d0e21e 570 }
125b9982
NT
571 if (SvTAINTED(sv))
572 SvTAINTED_on(PL_formtarget);
a0d0e21e
LW
573 break;
574
4a73dc0b 575 case FF_CHECKNL: /* find max len of item (up to \n) that fits field */
5a34cab7 576 {
5a34cab7 577 const char *s = item = SvPV_const(sv, len);
9b4bdfd4
DM
578 const char *send = s + len;
579
580 itemsize = 0;
581 item_is_utf8 = DO_UTF8(sv);
582 while (s < send) {
583 if (!isCNTRL(*s))
584 gotsome = TRUE;
585 else if (*s == '\n')
586 break;
587
588 if (item_is_utf8)
589 s += UTF8SKIP(s);
590 else
591 s++;
592 itemsize++;
593 if (itemsize == fieldsize)
594 break;
595 }
596 itembytes = s - item;
62db6ea5 597 chophere = s;
5a34cab7 598 break;
a0ed51b3 599 }
a0d0e21e 600
4a73dc0b 601 case FF_CHECKCHOP: /* like CHECKNL, but up to highest split point */
5a34cab7
NC
602 {
603 const char *s = item = SvPV_const(sv, len);
9b4bdfd4
DM
604 const char *send = s + len;
605 I32 size = 0;
606
607 chophere = NULL;
608 item_is_utf8 = DO_UTF8(sv);
609 while (s < send) {
610 /* look for a legal split position */
611 if (isSPACE(*s)) {
612 if (*s == '\r') {
613 chophere = s;
614 itemsize = size;
615 break;
616 }
617 if (chopspace) {
618 /* provisional split point */
619 chophere = s;
620 itemsize = size;
621 }
622 /* we delay testing fieldsize until after we've
623 * processed the possible split char directly
624 * following the last field char; so if fieldsize=3
625 * and item="a b cdef", we consume "a b", not "a".
626 * Ditto further down.
627 */
628 if (size == fieldsize)
629 break;
630 }
631 else {
632 if (strchr(PL_chopset, *s)) {
633 /* provisional split point */
634 /* for a non-space split char, we include
635 * the split char; hence the '+1' */
636 chophere = s + 1;
637 itemsize = size;
638 }
639 if (size == fieldsize)
640 break;
641 if (!isCNTRL(*s))
642 gotsome = TRUE;
643 }
644
645 if (item_is_utf8)
646 s += UTF8SKIP(s);
647 else
077dbbf3 648 s++;
9b4bdfd4
DM
649 size++;
650 }
651 if (!chophere || s == send) {
652 chophere = s;
653 itemsize = size;
654 }
655 itembytes = chophere - item;
656
5a34cab7 657 break;
a0d0e21e 658 }
a0d0e21e 659
4a73dc0b 660 case FF_SPACE: /* append padding space (diff of field, item size) */
a0d0e21e
LW
661 arg = fieldsize - itemsize;
662 if (arg) {
663 fieldsize -= arg;
664 while (arg-- > 0)
665 *t++ = ' ';
666 }
667 break;
668
4a73dc0b 669 case FF_HALFSPACE: /* like FF_SPACE, but only append half as many */
a0d0e21e
LW
670 arg = fieldsize - itemsize;
671 if (arg) {
672 arg /= 2;
673 fieldsize -= arg;
674 while (arg-- > 0)
675 *t++ = ' ';
676 }
677 break;
678
4a73dc0b 679 case FF_ITEM: /* append a text item, while blanking ctrl chars */
9b4bdfd4 680 to_copy = itembytes;
8aa7beb6
DM
681 source = (U8 *)item;
682 trans = 1;
8aa7beb6 683 goto append;
a0d0e21e 684
4a73dc0b 685 case FF_CHOP: /* (for ^*) chop the current item */
fb9282c3 686 if (sv != &PL_sv_no) {
5a34cab7 687 const char *s = chophere;
86191aed
TC
688 if (!copied_form &&
689 ((sv == tmpForm || SvSMAGICAL(sv))
690 || (SvGMAGICAL(tmpForm) && !sv_only_taint_gmagic(tmpForm))) ) {
691 /* sv and tmpForm are either the same SV, or magic might allow modification
692 of tmpForm when sv is modified, so copy */
693 SV *newformsv = sv_mortalcopy(formsv);
694 U32 *new_compiled;
695
696 f = SvPV_nolen(newformsv) + (f - SvPV_nolen(formsv));
697 Newx(new_compiled, mg->mg_len / sizeof(U32), U32);
698 memcpy(new_compiled, mg->mg_ptr, mg->mg_len);
699 SAVEFREEPV(new_compiled);
700 fpc = new_compiled + (fpc - (U32*)mg->mg_ptr);
701 formsv = newformsv;
702
92ff660b 703 copied_form = TRUE;
86191aed 704 }
5a34cab7 705 if (chopspace) {
af68e756 706 while (isSPACE(*s))
5a34cab7
NC
707 s++;
708 }
9b4bdfd4
DM
709 if (SvPOKp(sv))
710 sv_chop(sv,s);
711 else
712 /* tied, overloaded or similar strangeness.
713 * Do it the hard way */
714 sv_setpvn(sv, s, len - (s-item));
5a34cab7
NC
715 SvSETMAGIC(sv);
716 break;
a0d0e21e 717 }
a0d0e21e 718
4a73dc0b 719 case FF_LINESNGL: /* process ^* */
a1b95068 720 chopspace = 0;
c67159e1 721 /* FALLTHROUGH */
4a73dc0b
DM
722
723 case FF_LINEGLOB: /* process @* */
5a34cab7 724 {
e32383e2 725 const bool oneline = fpc[-1] == FF_LINESNGL;
5a34cab7 726 const char *s = item = SvPV_const(sv, len);
7440a75b 727 const char *const send = s + len;
7440a75b 728
f3f2f1a3 729 item_is_utf8 = DO_UTF8(sv);
fb9282c3 730 chophere = s + len;
a1137ee5 731 if (!len)
7440a75b 732 break;
ea60cfe8 733 trans = 0;
0d21cefe 734 gotsome = TRUE;
4ff700b9
DM
735 source = (U8 *) s;
736 to_copy = len;
0d21cefe
DM
737 while (s < send) {
738 if (*s++ == '\n') {
739 if (oneline) {
9b4bdfd4 740 to_copy = s - item - 1;
0d21cefe
DM
741 chophere = s;
742 break;
743 } else {
744 if (s == send) {
0d21cefe
DM
745 to_copy--;
746 } else
747 lines++;
1bd51a4c 748 }
a0d0e21e 749 }
0d21cefe 750 }
a2c0032b
DM
751 }
752
ea60cfe8
DM
753 append:
754 /* append to_copy bytes from source to PL_formstring.
755 * item_is_utf8 implies source is utf8.
756 * if trans, translate certain characters during the copy */
a2c0032b
DM
757 {
758 U8 *tmp = NULL;
732f08da 759 STRLEN grow = 0;
0325ce87 760
732f08da
DM
761 SvCUR_set(PL_formtarget,
762 t - SvPVX_const(PL_formtarget));
0325ce87 763
0d21cefe
DM
764 if (targ_is_utf8 && !item_is_utf8) {
765 source = tmp = bytes_to_utf8(source, &to_copy);
732f08da 766 grow = to_copy;
0d21cefe
DM
767 } else {
768 if (item_is_utf8 && !targ_is_utf8) {
f5ada144 769 U8 *s;
0d21cefe 770 /* Upgrade targ to UTF8, and then we reduce it to
0325ce87
DM
771 a problem we have a simple solution for.
772 Don't need get magic. */
0d21cefe 773 sv_utf8_upgrade_nomg(PL_formtarget);
0325ce87 774 targ_is_utf8 = TRUE;
f5ada144
DM
775 /* re-calculate linemark */
776 s = (U8*)SvPVX(PL_formtarget);
732f08da
DM
777 /* the bytes we initially allocated to append the
778 * whole line may have been gobbled up during the
779 * upgrade, so allocate a whole new line's worth
780 * for safety */
781 grow = linemax;
f5ada144
DM
782 while (linemark--)
783 s += UTF8SKIP(s);
784 linemark = s - (U8*)SvPVX(PL_formtarget);
e8e72d41 785 }
0d21cefe
DM
786 /* Easy. They agree. */
787 assert (item_is_utf8 == targ_is_utf8);
788 }
732f08da
DM
789 if (!trans)
790 /* @* and ^* are the only things that can exceed
791 * the linemax, so grow by the output size, plus
792 * a whole new form's worth in case of any further
793 * output */
794 grow = linemax + to_copy;
795 if (grow)
796 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
797 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
0d21cefe
DM
798
799 Copy(source, t, to_copy, char);
ea60cfe8 800 if (trans) {
8aa7beb6
DM
801 /* blank out ~ or control chars, depending on trans.
802 * works on bytes not chars, so relies on not
803 * matching utf8 continuation bytes */
ea60cfe8
DM
804 U8 *s = (U8*)t;
805 U8 *send = s + to_copy;
806 while (s < send) {
8aa7beb6 807 const int ch = *s;
077dbbf3 808 if (trans == '~' ? (ch == '~') : isCNTRL(ch))
ea60cfe8
DM
809 *s = ' ';
810 s++;
811 }
812 }
813
0d21cefe 814 t += to_copy;
732f08da 815 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
a1137ee5 816 if (tmp)
0d21cefe 817 Safefree(tmp);
5a34cab7 818 break;
a0d0e21e 819 }
a0d0e21e 820
4a73dc0b 821 case FF_0DECIMAL: /* like FF_DECIMAL but for 0### */
a0d0e21e 822 arg = *fpc++;
bd7084a6 823 fmt = (const char *)
a029fa42 824 ((arg & FORM_NUM_POINT) ? "%#0*.*" NVff : "%0*.*" NVff);
bd7084a6 825 goto ff_dec;
5d37acd6 826
bd7084a6
DM
827 case FF_DECIMAL: /* do @##, ^##, where <arg>=(precision|flags) */
828 arg = *fpc++;
bd7084a6 829 fmt = (const char *)
a029fa42 830 ((arg & FORM_NUM_POINT) ? "%#*.*" NVff : "%*.*" NVff);
bd7084a6 831 ff_dec:
784707d5
JP
832 /* If the field is marked with ^ and the value is undefined,
833 blank it out. */
a701009a 834 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
784707d5
JP
835 arg = fieldsize;
836 while (arg--)
837 *t++ = ' ';
838 break;
839 }
840 gotsome = TRUE;
841 value = SvNV(sv);
a1b95068 842 /* overflow evidence */
bfed75c6 843 if (num_overflow(value, fieldsize, arg)) {
a1b95068
LW
844 arg = fieldsize;
845 while (arg--)
846 *t++ = '#';
847 break;
848 }
784707d5
JP
849 /* Formats aren't yet marked for locales, so assume "yes". */
850 {
e8549682
JH
851 Size_t max = SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget));
852 int len;
67d796ae
KW
853 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
854 STORE_LC_NUMERIC_SET_TO_NEEDED();
51f14a05 855 arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
a4eca1d4
JH
856#ifdef USE_QUADMATH
857 {
858 const char* qfmt = quadmath_format_single(fmt);
859 int len;
860 if (!qfmt)
861 Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", fmt);
862 len = quadmath_snprintf(t, max, qfmt, (int) fieldsize, (int) arg, value);
863 if (len == -1)
864 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", qfmt);
865 if (qfmt != fmt)
866 Safefree(fmt);
867 }
868#else
b587c0e8
DM
869 /* we generate fmt ourselves so it is safe */
870 GCC_DIAG_IGNORE(-Wformat-nonliteral);
e8549682 871 len = my_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value);
b587c0e8 872 GCC_DIAG_RESTORE;
a4eca1d4
JH
873#endif
874 PERL_MY_SNPRINTF_POST_GUARD(len, max);
a2287a13 875 RESTORE_LC_NUMERIC();
784707d5
JP
876 }
877 t += fieldsize;
878 break;
a1b95068 879
4a73dc0b 880 case FF_NEWLINE: /* delete trailing spaces, then append \n */
a0d0e21e 881 f++;
732f08da 882 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
a0d0e21e
LW
883 t++;
884 *t++ = '\n';
885 break;
886
4a73dc0b 887 case FF_BLANK: /* for arg==0: do '~'; for arg>0 : do '~~' */
a0d0e21e
LW
888 arg = *fpc++;
889 if (gotsome) {
890 if (arg) { /* repeat until fields exhausted? */
11f9eeaf
DM
891 fpc--;
892 goto end;
a0d0e21e
LW
893 }
894 }
895 else {
f5ada144 896 t = SvPVX(PL_formtarget) + linemark;
a0d0e21e
LW
897 lines--;
898 }
899 break;
900
4a73dc0b 901 case FF_MORE: /* replace long end of string with '...' */
5a34cab7
NC
902 {
903 const char *s = chophere;
904 const char *send = item + len;
905 if (chopspace) {
af68e756 906 while (isSPACE(*s) && (s < send))
5a34cab7 907 s++;
a0d0e21e 908 }
5a34cab7
NC
909 if (s < send) {
910 char *s1;
911 arg = fieldsize - itemsize;
912 if (arg) {
913 fieldsize -= arg;
914 while (arg-- > 0)
915 *t++ = ' ';
916 }
917 s1 = t - 3;
918 if (strnEQ(s1," ",3)) {
919 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
920 s1--;
921 }
922 *s1++ = '.';
923 *s1++ = '.';
924 *s1++ = '.';
a0d0e21e 925 }
5a34cab7 926 break;
a0d0e21e 927 }
4a73dc0b
DM
928
929 case FF_END: /* tidy up, then return */
11f9eeaf 930 end:
bf2bec63 931 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
a0d0e21e 932 *t = '\0';
b15aece3 933 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c
IH
934 if (targ_is_utf8)
935 SvUTF8_on(PL_formtarget);
3280af22 936 FmLINES(PL_formtarget) += lines;
a0d0e21e 937 SP = ORIGMARK;
11f9eeaf
DM
938 if (fpc[-1] == FF_BLANK)
939 RETURNOP(cLISTOP->op_first);
940 else
941 RETPUSHYES;
a0d0e21e
LW
942 }
943 }
944}
945
10088f56 946/* also used for: pp_mapstart() */
a0d0e21e
LW
947PP(pp_grepstart)
948{
20b7effb 949 dSP;
a0d0e21e
LW
950 SV *src;
951
6cae08a8 952 if (PL_stack_base + TOPMARK == SP) {
a0d0e21e 953 (void)POPMARK;
54310121 954 if (GIMME_V == G_SCALAR)
725c44f9 955 XPUSHs(&PL_sv_zero);
533c011a 956 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 957 }
6cae08a8 958 PL_stack_sp = PL_stack_base + TOPMARK + 1;
897d3989
NC
959 Perl_pp_pushmark(aTHX); /* push dst */
960 Perl_pp_pushmark(aTHX); /* push src */
d343c3ef 961 ENTER_with_name("grep"); /* enter outer scope */
a0d0e21e
LW
962
963 SAVETMPS;
ffd49c98 964 SAVE_DEFSV;
d343c3ef 965 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 966 SAVEVPTR(PL_curpm);
a0d0e21e 967
6cae08a8 968 src = PL_stack_base[TOPMARK];
60779a30 969 if (SvPADTMP(src)) {
6cae08a8 970 src = PL_stack_base[TOPMARK] = sv_mortalcopy(src);
a0ed822e
FC
971 PL_tmps_floor++;
972 }
a0d0e21e 973 SvTEMP_off(src);
ffd49c98 974 DEFSV_set(src);
a0d0e21e
LW
975
976 PUTBACK;
533c011a 977 if (PL_op->op_type == OP_MAPSTART)
897d3989 978 Perl_pp_pushmark(aTHX); /* push top */
533c011a 979 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
980}
981
a0d0e21e
LW
982PP(pp_mapwhile)
983{
20b7effb 984 dSP;
1c23e2bd 985 const U8 gimme = GIMME_V;
6cae08a8 986 I32 items = (SP - PL_stack_base) - TOPMARK; /* how many new items */
a0d0e21e
LW
987 I32 count;
988 I32 shift;
989 SV** src;
ac27b0f5 990 SV** dst;
a0d0e21e 991
544f3153 992 /* first, move source pointer to the next item in the source list */
3280af22 993 ++PL_markstack_ptr[-1];
544f3153
GS
994
995 /* if there are new items, push them into the destination list */
4c90a460 996 if (items && gimme != G_VOID) {
544f3153
GS
997 /* might need to make room back there first */
998 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
999 /* XXX this implementation is very pessimal because the stack
1000 * is repeatedly extended for every set of items. Is possible
1001 * to do this without any stack extension or copying at all
1002 * by maintaining a separate list over which the map iterates
18ef8bea 1003 * (like foreach does). --gsar */
544f3153
GS
1004
1005 /* everything in the stack after the destination list moves
1006 * towards the end the stack by the amount of room needed */
1007 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1008
1009 /* items to shift up (accounting for the moved source pointer) */
1010 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
18ef8bea
BT
1011
1012 /* This optimization is by Ben Tilly and it does
1013 * things differently from what Sarathy (gsar)
1014 * is describing. The downside of this optimization is
1015 * that leaves "holes" (uninitialized and hopefully unused areas)
1016 * to the Perl stack, but on the other hand this
1017 * shouldn't be a problem. If Sarathy's idea gets
1018 * implemented, this optimization should become
1019 * irrelevant. --jhi */
1020 if (shift < count)
1021 shift = count; /* Avoid shifting too often --Ben Tilly */
bfed75c6 1022
924508f0
GS
1023 EXTEND(SP,shift);
1024 src = SP;
1025 dst = (SP += shift);
3280af22
NIS
1026 PL_markstack_ptr[-1] += shift;
1027 *PL_markstack_ptr += shift;
544f3153 1028 while (count--)
a0d0e21e
LW
1029 *dst-- = *src--;
1030 }
544f3153 1031 /* copy the new items down to the destination list */
ac27b0f5 1032 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
22023b26 1033 if (gimme == G_ARRAY) {
b2a2a901
DM
1034 /* add returned items to the collection (making mortal copies
1035 * if necessary), then clear the current temps stack frame
1036 * *except* for those items. We do this splicing the items
1037 * into the start of the tmps frame (so some items may be on
59d53fd6 1038 * the tmps stack twice), then moving PL_tmps_floor above
b2a2a901
DM
1039 * them, then freeing the frame. That way, the only tmps that
1040 * accumulate over iterations are the return values for map.
1041 * We have to do to this way so that everything gets correctly
1042 * freed if we die during the map.
1043 */
1044 I32 tmpsbase;
1045 I32 i = items;
1046 /* make space for the slice */
1047 EXTEND_MORTAL(items);
1048 tmpsbase = PL_tmps_floor + 1;
1049 Move(PL_tmps_stack + tmpsbase,
1050 PL_tmps_stack + tmpsbase + items,
1051 PL_tmps_ix - PL_tmps_floor,
1052 SV*);
1053 PL_tmps_ix += items;
1054
1055 while (i-- > 0) {
1056 SV *sv = POPs;
1057 if (!SvTEMP(sv))
1058 sv = sv_mortalcopy(sv);
1059 *dst-- = sv;
1060 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1061 }
1062 /* clear the stack frame except for the items */
1063 PL_tmps_floor += items;
1064 FREETMPS;
1065 /* FREETMPS may have cleared the TEMP flag on some of the items */
1066 i = items;
1067 while (i-- > 0)
1068 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
22023b26 1069 }
bfed75c6 1070 else {
22023b26
TP
1071 /* scalar context: we don't care about which values map returns
1072 * (we use undef here). And so we certainly don't want to do mortal
1073 * copies of meaningless values. */
1074 while (items-- > 0) {
b988aa42 1075 (void)POPs;
22023b26
TP
1076 *dst-- = &PL_sv_undef;
1077 }
b2a2a901 1078 FREETMPS;
22023b26 1079 }
a0d0e21e 1080 }
b2a2a901
DM
1081 else {
1082 FREETMPS;
1083 }
d343c3ef 1084 LEAVE_with_name("grep_item"); /* exit inner scope */
a0d0e21e
LW
1085
1086 /* All done yet? */
6cae08a8 1087 if (PL_markstack_ptr[-1] > TOPMARK) {
a0d0e21e
LW
1088
1089 (void)POPMARK; /* pop top */
d343c3ef 1090 LEAVE_with_name("grep"); /* exit outer scope */
a0d0e21e 1091 (void)POPMARK; /* pop src */
3280af22 1092 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1093 (void)POPMARK; /* pop dst */
3280af22 1094 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1095 if (gimme == G_SCALAR) {
7cc47870
RGS
1096 dTARGET;
1097 XPUSHi(items);
a0d0e21e 1098 }
54310121
PP
1099 else if (gimme == G_ARRAY)
1100 SP += items;
a0d0e21e
LW
1101 RETURN;
1102 }
1103 else {
1104 SV *src;
1105
d343c3ef 1106 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1107 SAVEVPTR(PL_curpm);
a0d0e21e 1108
544f3153 1109 /* set $_ to the new source item */
3280af22 1110 src = PL_stack_base[PL_markstack_ptr[-1]];
60779a30 1111 if (SvPADTMP(src)) {
60779a30
DM
1112 src = sv_mortalcopy(src);
1113 }
a0d0e21e 1114 SvTEMP_off(src);
ffd49c98 1115 DEFSV_set(src);
a0d0e21e
LW
1116
1117 RETURNOP(cLOGOP->op_other);
1118 }
1119}
1120
a0d0e21e
LW
1121/* Range stuff. */
1122
1123PP(pp_range)
1124{
f4c975aa 1125 dTARG;
82334630 1126 if (GIMME_V == G_ARRAY)
1a67a97c 1127 return NORMAL;
f4c975aa
DM
1128 GETTARGET;
1129 if (SvTRUE_NN(targ))
1a67a97c 1130 return cLOGOP->op_other;
538573f7 1131 else
1a67a97c 1132 return NORMAL;
a0d0e21e
LW
1133}
1134
1135PP(pp_flip)
1136{
39644a26 1137 dSP;
a0d0e21e 1138
82334630 1139 if (GIMME_V == G_ARRAY) {
1a67a97c 1140 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1141 }
1142 else {
1143 dTOPss;
44f8325f 1144 SV * const targ = PAD_SV(PL_op->op_targ);
bfed75c6 1145 int flip = 0;
790090df 1146
bfed75c6 1147 if (PL_op->op_private & OPpFLIP_LINENUM) {
4e3399f9
YST
1148 if (GvIO(PL_last_in_gv)) {
1149 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1150 }
1151 else {
fafc274c 1152 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
44f8325f
AL
1153 if (gv && GvSV(gv))
1154 flip = SvIV(sv) == SvIV(GvSV(gv));
4e3399f9 1155 }
bfed75c6 1156 } else {
f4c975aa 1157 flip = SvTRUE_NN(sv);
bfed75c6
AL
1158 }
1159 if (flip) {
a0d0e21e 1160 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
533c011a 1161 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1162 sv_setiv(targ, 1);
3e3baf6d 1163 SETs(targ);
a0d0e21e
LW
1164 RETURN;
1165 }
1166 else {
1167 sv_setiv(targ, 0);
924508f0 1168 SP--;
1a67a97c 1169 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1170 }
1171 }
9aa3b533 1172 SvPVCLEAR(TARG);
a0d0e21e
LW
1173 SETs(targ);
1174 RETURN;
1175 }
1176}
1177
8e9bbdb9
RGS
1178/* This code tries to decide if "$left .. $right" should use the
1179 magical string increment, or if the range is numeric (we make
1180 an exception for .."0" [#18165]). AMS 20021031. */
1181
1182#define RANGE_IS_NUMERIC(left,right) ( \
b0e74086
RGS
1183 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1184 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
e0ab1c0e 1185 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
b15aece3 1186 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
e0ab1c0e 1187 && (!SvOK(right) || looks_like_number(right))))
8e9bbdb9 1188
a0d0e21e
LW
1189PP(pp_flop)
1190{
20b7effb 1191 dSP;
a0d0e21e 1192
82334630 1193 if (GIMME_V == G_ARRAY) {
a0d0e21e 1194 dPOPPOPssrl;
86cb7173 1195
5b295bef
RD
1196 SvGETMAGIC(left);
1197 SvGETMAGIC(right);
a0d0e21e 1198
8e9bbdb9 1199 if (RANGE_IS_NUMERIC(left,right)) {
b262c4c9 1200 IV i, j, n;
4d91eccc
FC
1201 if ((SvOK(left) && !SvIOK(left) && SvNV_nomg(left) < IV_MIN) ||
1202 (SvOK(right) && (SvIOK(right)
1203 ? SvIsUV(right) && SvUV(right) > IV_MAX
1204 : SvNV_nomg(right) > IV_MAX)))
d470f89e 1205 DIE(aTHX_ "Range iterator outside integer range");
f52e41ad 1206 i = SvIV_nomg(left);
b262c4c9
JH
1207 j = SvIV_nomg(right);
1208 if (j >= i) {
1209 /* Dance carefully around signed max. */
1210 bool overflow = (i <= 0 && j > SSize_t_MAX + i - 1);
1211 if (!overflow) {
1212 n = j - i + 1;
1213 /* The wraparound of signed integers is undefined
1214 * behavior, but here we aim for count >=1, and
1215 * negative count is just wrong. */
a1e27170
TC
1216 if (n < 1
1217#if IVSIZE > Size_t_size
1218 || n > SSize_t_MAX
1219#endif
1220 )
b262c4c9
JH
1221 overflow = TRUE;
1222 }
1223 if (overflow)
1224 Perl_croak(aTHX_ "Out of memory during list extend");
1225 EXTEND_MORTAL(n);
1226 EXTEND(SP, n);
bbce6d69 1227 }
c1ab3db2 1228 else
b262c4c9
JH
1229 n = 0;
1230 while (n--) {
fc01cab4 1231 SV * const sv = sv_2mortal(newSViv(i));
a0d0e21e 1232 PUSHs(sv);
fc01cab4
DM
1233 if (n) /* avoid incrementing above IV_MAX */
1234 i++;
a0d0e21e
LW
1235 }
1236 }
1237 else {
3c323193
FC
1238 STRLEN len, llen;
1239 const char * const lpv = SvPV_nomg_const(left, llen);
f52e41ad 1240 const char * const tmps = SvPV_nomg_const(right, len);
a0d0e21e 1241
3c323193 1242 SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP);
d6c970c7
AC
1243 if (DO_UTF8(right) && IN_UNI_8_BIT)
1244 len = sv_len_utf8_nomg(right);
89ea2908 1245 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
a0d0e21e 1246 XPUSHs(sv);
b15aece3 1247 if (strEQ(SvPVX_const(sv),tmps))
89ea2908 1248 break;
a0d0e21e
LW
1249 sv = sv_2mortal(newSVsv(sv));
1250 sv_inc(sv);
1251 }
a0d0e21e
LW
1252 }
1253 }
1254 else {
1255 dTOPss;
901017d6 1256 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
4e3399f9 1257 int flop = 0;
a0d0e21e 1258 sv_inc(targ);
4e3399f9
YST
1259
1260 if (PL_op->op_private & OPpFLIP_LINENUM) {
1261 if (GvIO(PL_last_in_gv)) {
1262 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1263 }
1264 else {
fafc274c 1265 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
4e3399f9
YST
1266 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1267 }
1268 }
1269 else {
f4c975aa 1270 flop = SvTRUE_NN(sv);
4e3399f9
YST
1271 }
1272
1273 if (flop) {
a0d0e21e 1274 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
396482e1 1275 sv_catpvs(targ, "E0");
a0d0e21e
LW
1276 }
1277 SETs(targ);
1278 }
1279
1280 RETURN;
1281}
1282
1283/* Control. */
1284
27da23d5 1285static const char * const context_name[] = {
515afda2 1286 "pseudo-block",
f31522f3 1287 NULL, /* CXt_WHEN never actually needs "block" */
76753e7f 1288 NULL, /* CXt_BLOCK never actually needs "block" */
f31522f3 1289 NULL, /* CXt_GIVEN never actually needs "block" */
76753e7f 1290 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
76753e7f 1291 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
93661e56
DM
1292 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1293 NULL, /* CXt_LOOP_LIST never actually needs "loop" */
1294 NULL, /* CXt_LOOP_ARY never actually needs "loop" */
515afda2 1295 "subroutine",
76753e7f 1296 "format",
515afda2 1297 "eval",
515afda2 1298 "substitution",
515afda2
NC
1299};
1300
76e3520e 1301STATIC I32
5db1eb8d 1302S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags)
a0d0e21e 1303{
eb578fdb 1304 I32 i;
a0d0e21e 1305
7918f24d
NC
1306 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1307
a0d0e21e 1308 for (i = cxstack_ix; i >= 0; i--) {
eb578fdb 1309 const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1310 switch (CxTYPE(cx)) {
a0d0e21e 1311 case CXt_SUBST:
a0d0e21e 1312 case CXt_SUB:
7766f137 1313 case CXt_FORMAT:
a0d0e21e 1314 case CXt_EVAL:
0a753a76 1315 case CXt_NULL:
dcbac5bb 1316 /* diag_listed_as: Exiting subroutine via %s */
a2a5de95
NC
1317 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1318 context_name[CxTYPE(cx)], OP_NAME(PL_op));
79646418 1319 if (CxTYPE(cx) == CXt_NULL) /* sort BLOCK */
515afda2
NC
1320 return -1;
1321 break;
93661e56 1322 case CXt_LOOP_PLAIN:
c6fdafd0 1323 case CXt_LOOP_LAZYIV:
d01136d6 1324 case CXt_LOOP_LAZYSV:
93661e56
DM
1325 case CXt_LOOP_LIST:
1326 case CXt_LOOP_ARY:
7e8f1eac 1327 {
5db1eb8d
BF
1328 STRLEN cx_label_len = 0;
1329 U32 cx_label_flags = 0;
1330 const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags);
1331 if (!cx_label || !(
1332 ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
1333 (flags & SVf_UTF8)
1334 ? (bytes_cmp_utf8(
1335 (const U8*)cx_label, cx_label_len,
1336 (const U8*)label, len) == 0)
1337 : (bytes_cmp_utf8(
1338 (const U8*)label, len,
1339 (const U8*)cx_label, cx_label_len) == 0)
eade7155
BF
1340 : (len == cx_label_len && ((cx_label == label)
1341 || memEQ(cx_label, label, len))) )) {
1c98cc53 1342 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
7e8f1eac 1343 (long)i, cx_label));
a0d0e21e
LW
1344 continue;
1345 }
1c98cc53 1346 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
a0d0e21e 1347 return i;
7e8f1eac 1348 }
a0d0e21e
LW
1349 }
1350 }
1351 return i;
1352}
1353
0d863452
RH
1354
1355
1c23e2bd 1356U8
864dbfa3 1357Perl_dowantarray(pTHX)
e50aee73 1358{
1c23e2bd 1359 const U8 gimme = block_gimme();
54310121
PP
1360 return (gimme == G_VOID) ? G_SCALAR : gimme;
1361}
1362
1c23e2bd 1363U8
864dbfa3 1364Perl_block_gimme(pTHX)
54310121 1365{
06b5626a 1366 const I32 cxix = dopoptosub(cxstack_ix);
a05700a8 1367 U8 gimme;
e50aee73 1368 if (cxix < 0)
46fc3d4c 1369 return G_VOID;
e50aee73 1370
a05700a8
DM
1371 gimme = (cxstack[cxix].blk_gimme & G_WANT);
1372 if (!gimme)
1373 Perl_croak(aTHX_ "panic: bad gimme: %d\n", gimme);
1374 return gimme;
e50aee73
AD
1375}
1376
a05700a8 1377
78f9721b
SM
1378I32
1379Perl_is_lvalue_sub(pTHX)
1380{
06b5626a 1381 const I32 cxix = dopoptosub(cxstack_ix);
78f9721b
SM
1382 assert(cxix >= 0); /* We should only be called from inside subs */
1383
bafb2adc
NC
1384 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1385 return CxLVAL(cxstack + cxix);
78f9721b
SM
1386 else
1387 return 0;
1388}
1389
a73d8813 1390/* only used by cx_pushsub() */
777d9014
FC
1391I32
1392Perl_was_lvalue_sub(pTHX)
1393{
777d9014
FC
1394 const I32 cxix = dopoptosub(cxstack_ix-1);
1395 assert(cxix >= 0); /* We should only be called from inside subs */
1396
1397 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1398 return CxLVAL(cxstack + cxix);
1399 else
1400 return 0;
1401}
1402
76e3520e 1403STATIC I32
901017d6 1404S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
2c375eb9 1405{
a0d0e21e 1406 I32 i;
7918f24d
NC
1407
1408 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
81611534
JH
1409#ifndef DEBUGGING
1410 PERL_UNUSED_CONTEXT;
1411#endif
7918f24d 1412
a0d0e21e 1413 for (i = startingblock; i >= 0; i--) {
eb578fdb 1414 const PERL_CONTEXT * const cx = &cxstk[i];
6b35e009 1415 switch (CxTYPE(cx)) {
a0d0e21e
LW
1416 default:
1417 continue;
a0d0e21e 1418 case CXt_SUB:
5fbe8311
DM
1419 /* in sub foo { /(?{...})/ }, foo ends up on the CX stack
1420 * twice; the first for the normal foo() call, and the second
1421 * for a faked up re-entry into the sub to execute the
1422 * code block. Hide this faked entry from the world. */
1423 if (cx->cx_type & CXp_SUB_RE_FAKE)
1424 continue;
c67159e1 1425 /* FALLTHROUGH */
5fbe8311 1426 case CXt_EVAL:
7766f137 1427 case CXt_FORMAT:
1c98cc53 1428 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
a0d0e21e
LW
1429 return i;
1430 }
1431 }
1432 return i;
1433}
1434
76e3520e 1435STATIC I32
cea2e8a9 1436S_dopoptoeval(pTHX_ I32 startingblock)
a0d0e21e
LW
1437{
1438 I32 i;
a0d0e21e 1439 for (i = startingblock; i >= 0; i--) {
eb578fdb 1440 const PERL_CONTEXT *cx = &cxstack[i];
6b35e009 1441 switch (CxTYPE(cx)) {
a0d0e21e
LW
1442 default:
1443 continue;
1444 case CXt_EVAL:
1c98cc53 1445 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
a0d0e21e
LW
1446 return i;
1447 }
1448 }
1449 return i;
1450}
1451
76e3520e 1452STATIC I32
cea2e8a9 1453S_dopoptoloop(pTHX_ I32 startingblock)
a0d0e21e
LW
1454{
1455 I32 i;
a0d0e21e 1456 for (i = startingblock; i >= 0; i--) {
eb578fdb 1457 const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1458 switch (CxTYPE(cx)) {
a0d0e21e 1459 case CXt_SUBST:
a0d0e21e 1460 case CXt_SUB:
7766f137 1461 case CXt_FORMAT:
a0d0e21e 1462 case CXt_EVAL:
0a753a76 1463 case CXt_NULL:
dcbac5bb 1464 /* diag_listed_as: Exiting subroutine via %s */
a2a5de95
NC
1465 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1466 context_name[CxTYPE(cx)], OP_NAME(PL_op));
79646418 1467 if ((CxTYPE(cx)) == CXt_NULL) /* sort BLOCK */
515afda2
NC
1468 return -1;
1469 break;
93661e56 1470 case CXt_LOOP_PLAIN:
c6fdafd0 1471 case CXt_LOOP_LAZYIV:
d01136d6 1472 case CXt_LOOP_LAZYSV:
93661e56
DM
1473 case CXt_LOOP_LIST:
1474 case CXt_LOOP_ARY:
1c98cc53 1475 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
a0d0e21e
LW
1476 return i;
1477 }
1478 }
1479 return i;
1480}
1481
28e0cf42 1482/* find the next GIVEN or FOR (with implicit $_) loop context block */
97f2561e 1483
0d863452 1484STATIC I32
97f2561e 1485S_dopoptogivenfor(pTHX_ I32 startingblock)
0d863452
RH
1486{
1487 I32 i;
1488 for (i = startingblock; i >= 0; i--) {
eb578fdb 1489 const PERL_CONTEXT *cx = &cxstack[i];
0d863452
RH
1490 switch (CxTYPE(cx)) {
1491 default:
1492 continue;
1493 case CXt_GIVEN:
97f2561e 1494 DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found given at cx=%ld)\n", (long)i));
0d863452 1495 return i;
3b719c58 1496 case CXt_LOOP_PLAIN:
93661e56 1497 assert(!(cx->cx_type & CXp_FOR_DEF));
3b719c58 1498 break;
c6fdafd0 1499 case CXt_LOOP_LAZYIV:
d01136d6 1500 case CXt_LOOP_LAZYSV:
93661e56
DM
1501 case CXt_LOOP_LIST:
1502 case CXt_LOOP_ARY:
1503 if (cx->cx_type & CXp_FOR_DEF) {
97f2561e 1504 DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found foreach at cx=%ld)\n", (long)i));
0d863452
RH
1505 return i;
1506 }
1507 }
1508 }
1509 return i;
1510}
1511
1512STATIC I32
1513S_dopoptowhen(pTHX_ I32 startingblock)
1514{
1515 I32 i;
1516 for (i = startingblock; i >= 0; i--) {
eb578fdb 1517 const PERL_CONTEXT *cx = &cxstack[i];
0d863452
RH
1518 switch (CxTYPE(cx)) {
1519 default:
1520 continue;
1521 case CXt_WHEN:
1c98cc53 1522 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
0d863452
RH
1523 return i;
1524 }
1525 }
1526 return i;
1527}
1528
abaf5d5a 1529/* dounwind(): pop all contexts above (but not including) cxix.
fc6e609e
DM
1530 * Note that it clears the savestack frame associated with each popped
1531 * context entry, but doesn't free any temps.
ed8ff0f3 1532 * It does a cx_popblock() of the last frame that it pops, and leaves
fc6e609e 1533 * cxstack_ix equal to cxix.
abaf5d5a
DM
1534 */
1535
a0d0e21e 1536void
864dbfa3 1537Perl_dounwind(pTHX_ I32 cxix)
a0d0e21e 1538{
f144f1e3
DM
1539 if (!PL_curstackinfo) /* can happen if die during thread cloning */
1540 return;
1541
a0d0e21e 1542 while (cxstack_ix > cxix) {
4ebe6e95 1543 PERL_CONTEXT *cx = CX_CUR();
5e691bc6
DM
1544
1545 CX_DEBUG(cx, "UNWIND");
a0d0e21e 1546 /* Note: we don't need to restore the base context info till the end. */
76d10131
DM
1547
1548 CX_LEAVE_SCOPE(cx);
1549
6b35e009 1550 switch (CxTYPE(cx)) {
c90c0ff4 1551 case CXt_SUBST:
b405d38b 1552 CX_POPSUBST(cx);
7332835e
DM
1553 /* CXt_SUBST is not a block context type, so skip the
1554 * cx_popblock(cx) below */
1555 if (cxstack_ix == cxix + 1) {
1556 cxstack_ix--;
1557 return;
1558 }
80d88db0 1559 break;
a0d0e21e 1560 case CXt_SUB:
a73d8813 1561 cx_popsub(cx);
a0d0e21e
LW
1562 break;
1563 case CXt_EVAL:
13febba5 1564 cx_popeval(cx);
03e489bc 1565 break;
93661e56 1566 case CXt_LOOP_PLAIN:
c6fdafd0 1567 case CXt_LOOP_LAZYIV:
d01136d6 1568 case CXt_LOOP_LAZYSV:
93661e56
DM
1569 case CXt_LOOP_LIST:
1570 case CXt_LOOP_ARY:
d1b6bf72 1571 cx_poploop(cx);
a0d0e21e 1572 break;
b95eccd3 1573 case CXt_WHEN:
2a7b7c61 1574 cx_popwhen(cx);
b95eccd3
DM
1575 break;
1576 case CXt_GIVEN:
2a7b7c61 1577 cx_popgiven(cx);
b95eccd3 1578 break;
f217cb3d 1579 case CXt_BLOCK:
0a753a76 1580 case CXt_NULL:
f217cb3d 1581 /* these two don't have a POPFOO() */
a0d0e21e 1582 break;
7766f137 1583 case CXt_FORMAT:
6a7d52cc 1584 cx_popformat(cx);
7766f137 1585 break;
a0d0e21e 1586 }
fc6e609e 1587 if (cxstack_ix == cxix + 1) {
ed8ff0f3 1588 cx_popblock(cx);
fc6e609e 1589 }
c90c0ff4 1590 cxstack_ix--;
a0d0e21e 1591 }
fc6e609e 1592
a0d0e21e
LW
1593}
1594
5a844595
GS
1595void
1596Perl_qerror(pTHX_ SV *err)
1597{
7918f24d
NC
1598 PERL_ARGS_ASSERT_QERROR;
1599
6b2fb389
DM
1600 if (PL_in_eval) {
1601 if (PL_in_eval & EVAL_KEEPERR) {
147e3846 1602 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf,
ecad31f0 1603 SVfARG(err));
6b2fb389
DM
1604 }
1605 else
1606 sv_catsv(ERRSV, err);
1607 }
5a844595
GS
1608 else if (PL_errors)
1609 sv_catsv(PL_errors, err);
1610 else
147e3846 1611 Perl_warn(aTHX_ "%" SVf, SVfARG(err));
13765c85
DM
1612 if (PL_parser)
1613 ++PL_parser->error_count;
5a844595
GS
1614}
1615
d308a779
DM
1616
1617
06a7bc17
DM
1618/* pop a CXt_EVAL context and in addition, if it was a require then
1619 * based on action:
1620 * 0: do nothing extra;
1621 * 1: undef $INC{$name}; croak "$name did not return a true value";
1622 * 2: delete $INC{$name}; croak "$errsv: Compilation failed in require"
1623 */
d308a779 1624
8a1fd305 1625static void
06a7bc17 1626S_pop_eval_context_maybe_croak(pTHX_ PERL_CONTEXT *cx, SV *errsv, int action)
d308a779 1627{
51787acf 1628 SV *namesv = NULL; /* init to avoid dumb compiler warning */
06a7bc17
DM
1629 bool do_croak;
1630
1631 CX_LEAVE_SCOPE(cx);
1632 do_croak = action && (CxOLD_OP_TYPE(cx) == OP_REQUIRE);
2a1e0dfe
DM
1633 if (do_croak) {
1634 /* keep namesv alive after cx_popeval() */
1635 namesv = cx->blk_eval.old_namesv;
1636 cx->blk_eval.old_namesv = NULL;
1637 sv_2mortal(namesv);
1638 }
06a7bc17
DM
1639 cx_popeval(cx);
1640 cx_popblock(cx);
1641 CX_POP(cx);
1642
1643 if (do_croak) {
1644 const char *fmt;
1645 HV *inc_hv = GvHVn(PL_incgv);
1646 I32 klen = SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv);
1647 const char *key = SvPVX_const(namesv);
d308a779 1648
06a7bc17
DM
1649 if (action == 1) {
1650 (void)hv_delete(inc_hv, key, klen, G_DISCARD);
147e3846 1651 fmt = "%" SVf " did not return a true value";
06a7bc17
DM
1652 errsv = namesv;
1653 }
1654 else {
1655 (void)hv_store(inc_hv, key, klen, &PL_sv_undef, 0);
147e3846 1656 fmt = "%" SVf "Compilation failed in require";
06a7bc17
DM
1657 if (!errsv)
1658 errsv = newSVpvs_flags("Unknown error\n", SVs_TEMP);
1659 }
1660
1661 Perl_croak(aTHX_ fmt, SVfARG(errsv));
1662 }
d308a779
DM
1663}
1664
1665
8c86f023
DM
1666/* die_unwind(): this is the final destination for the various croak()
1667 * functions. If we're in an eval, unwind the context and other stacks
1668 * back to the top-most CXt_EVAL and set $@ to msv; otherwise print msv
1669 * to STDERR and initiate an exit. Note that if the CXt_EVAL popped back
1670 * to is a require the exception will be rethrown, as requires don't
1671 * actually trap exceptions.
1672 */
06a7bc17 1673
bb4c52e0 1674void
c5df3096 1675Perl_die_unwind(pTHX_ SV *msv)
a0d0e21e 1676{
8c86f023 1677 SV *exceptsv = msv;
96d9b9cd 1678 U8 in_eval = PL_in_eval;
c5df3096 1679 PERL_ARGS_ASSERT_DIE_UNWIND;
87582a92 1680
96d9b9cd 1681 if (in_eval) {
a0d0e21e 1682 I32 cxix;
a0d0e21e 1683
b66d79a6
DM
1684 /* We need to keep this SV alive through all the stack unwinding
1685 * and FREETMPSing below, while ensuing that it doesn't leak
1686 * if we call out to something which then dies (e.g. sub STORE{die}
1687 * when unlocalising a tied var). So we do a dance with
1688 * mortalising and SAVEFREEing.
1689 */
1690 sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv));
8c86f023 1691
22a30693
Z
1692 /*
1693 * Historically, perl used to set ERRSV ($@) early in the die
1694 * process and rely on it not getting clobbered during unwinding.
1695 * That sucked, because it was liable to get clobbered, so the
1696 * setting of ERRSV used to emit the exception from eval{} has
1697 * been moved to much later, after unwinding (see just before
1698 * JMPENV_JUMP below). However, some modules were relying on the
1699 * early setting, by examining $@ during unwinding to use it as
1700 * a flag indicating whether the current unwinding was caused by
1701 * an exception. It was never a reliable flag for that purpose,
1702 * being totally open to false positives even without actual
1703 * clobberage, but was useful enough for production code to
1704 * semantically rely on it.
1705 *
1706 * We'd like to have a proper introspective interface that
1707 * explicitly describes the reason for whatever unwinding
1708 * operations are currently in progress, so that those modules
1709 * work reliably and $@ isn't further overloaded. But we don't
1710 * have one yet. In its absence, as a stopgap measure, ERRSV is
1711 * now *additionally* set here, before unwinding, to serve as the
1712 * (unreliable) flag that it used to.
1713 *
1714 * This behaviour is temporary, and should be removed when a
1715 * proper way to detect exceptional unwinding has been developed.
1716 * As of 2010-12, the authors of modules relying on the hack
1717 * are aware of the issue, because the modules failed on
1718 * perls 5.13.{1..7} which had late setting of $@ without this
1719 * early-setting hack.
1720 */
8c86f023
DM
1721 if (!(in_eval & EVAL_KEEPERR))
1722 sv_setsv_flags(ERRSV, exceptsv,
1723 (SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL));
22a30693 1724
fc941f37 1725 if (in_eval & EVAL_KEEPERR) {
147e3846 1726 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf,
fc941f37
Z
1727 SVfARG(exceptsv));
1728 }
1729
5a844595
GS
1730 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1731 && PL_curstackinfo->si_prev)
1732 {
bac4b2ad 1733 dounwind(-1);
d3acc0f7 1734 POPSTACK;
bac4b2ad 1735 }
e336de0d 1736
a0d0e21e 1737 if (cxix >= 0) {
eb578fdb 1738 PERL_CONTEXT *cx;
f5ddd604 1739 SV **oldsp;
1c23e2bd 1740 U8 gimme;
8f89e5a9
Z
1741 JMPENV *restartjmpenv;
1742 OP *restartop;
a0d0e21e
LW
1743
1744 if (cxix < cxstack_ix)
1745 dounwind(cxix);
1746
4ebe6e95 1747 cx = CX_CUR();
f7d0774b 1748 assert(CxTYPE(cx) == CXt_EVAL);
e17c1e7c
DM
1749
1750 /* return false to the caller of eval */
f5ddd604 1751 oldsp = PL_stack_base + cx->blk_oldsp;
f7d0774b 1752 gimme = cx->blk_gimme;
f7d0774b 1753 if (gimme == G_SCALAR)
f5ddd604
DM
1754 *++oldsp = &PL_sv_undef;
1755 PL_stack_sp = oldsp;
f7d0774b 1756
7d140242
DM
1757 restartjmpenv = cx->blk_eval.cur_top_env;
1758 restartop = cx->blk_eval.retop;
b66d79a6
DM
1759
1760 /* We need a FREETMPS here to avoid late-called destructors
1761 * clobbering $@ *after* we set it below, e.g.
1762 * sub DESTROY { eval { die "X" } }
1763 * eval { my $x = bless []; die $x = 0, "Y" };
1764 * is($@, "Y")
1765 * Here the clearing of the $x ref mortalises the anon array,
1766 * which needs to be freed *before* $& is set to "Y",
1767 * otherwise it gets overwritten with "X".
1768 *
1769 * However, the FREETMPS will clobber exceptsv, so preserve it
1770 * on the savestack for now.
1771 */
1772 SAVEFREESV(SvREFCNT_inc_simple_NN(exceptsv));
1773 FREETMPS;
1774 /* now we're about to pop the savestack, so re-mortalise it */
1775 sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv));
1776
06a7bc17
DM
1777 /* Note that unlike pp_entereval, pp_require isn't supposed to
1778 * trap errors. So if we're a require, after we pop the
1779 * CXt_EVAL that pp_require pushed, rethrow the error with
1780 * croak(exceptsv). This is all handled by the call below when
1781 * action == 2.
1782 */
1783 S_pop_eval_context_maybe_croak(aTHX_ cx, exceptsv, 2);
a0d0e21e 1784
fc941f37 1785 if (!(in_eval & EVAL_KEEPERR))
96d9b9cd 1786 sv_setsv(ERRSV, exceptsv);
8f89e5a9
Z
1787 PL_restartjmpenv = restartjmpenv;
1788 PL_restartop = restartop;
bb4c52e0 1789 JMPENV_JUMP(3);
e5964223 1790 NOT_REACHED; /* NOTREACHED */
a0d0e21e
LW
1791 }
1792 }
87582a92 1793
96d9b9cd 1794 write_to_stderr(exceptsv);
f86702cc 1795 my_failure_exit();
e5964223 1796 NOT_REACHED; /* NOTREACHED */
a0d0e21e
LW
1797}
1798
1799PP(pp_xor)
1800{
20b7effb 1801 dSP; dPOPTOPssrl;
f4c975aa 1802 if (SvTRUE_NN(left) != SvTRUE_NN(right))
a0d0e21e
LW
1803 RETSETYES;
1804 else
1805 RETSETNO;
1806}
1807
8dff4fc5 1808/*
dcccc8ff
KW
1809
1810=head1 CV Manipulation Functions
1811
8dff4fc5
BM
1812=for apidoc caller_cx
1813
72d33970 1814The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
8dff4fc5 1815returned C<PERL_CONTEXT> structure can be interrogated to find all the
72d33970 1816information returned to Perl by C<caller>. Note that XSUBs don't get a
8dff4fc5
BM
1817stack frame, so C<caller_cx(0, NULL)> will return information for the
1818immediately-surrounding Perl code.
1819
1820This function skips over the automatic calls to C<&DB::sub> made on the
72d33970 1821behalf of the debugger. If the stack frame requested was a sub called by
8dff4fc5
BM
1822C<DB::sub>, the return value will be the frame for the call to
1823C<DB::sub>, since that has the correct line number/etc. for the call
72d33970 1824site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
8dff4fc5
BM
1825frame for the sub call itself.
1826
1827=cut
1828*/
1829
1830const PERL_CONTEXT *
1831Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
a0d0e21e 1832{
eb578fdb
KW
1833 I32 cxix = dopoptosub(cxstack_ix);
1834 const PERL_CONTEXT *cx;
1835 const PERL_CONTEXT *ccstack = cxstack;
901017d6 1836 const PERL_SI *top_si = PL_curstackinfo;
27d41816 1837
a0d0e21e 1838 for (;;) {
2c375eb9
GS
1839 /* we may be in a higher stacklevel, so dig down deeper */
1840 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1841 top_si = top_si->si_prev;
1842 ccstack = top_si->si_cxstack;
1843 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1844 }
8dff4fc5
BM
1845 if (cxix < 0)
1846 return NULL;
f2a7f298 1847 /* caller() should not report the automatic calls to &DB::sub */
1848 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
3280af22 1849 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1850 count++;
1851 if (!count--)
1852 break;
2c375eb9 1853 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1854 }
2c375eb9
GS
1855
1856 cx = &ccstack[cxix];
8dff4fc5
BM
1857 if (dbcxp) *dbcxp = cx;
1858
7766f137 1859 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1860 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
2c375eb9 1861 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1862 field below is defined for any cx. */
f2a7f298 1863 /* caller() should not report the automatic calls to &DB::sub */
1864 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1865 cx = &ccstack[dbcxix];
06a5b730
PP
1866 }
1867
8dff4fc5
BM
1868 return cx;
1869}
1870
1871PP(pp_caller)
1872{
8dff4fc5 1873 dSP;
eb578fdb 1874 const PERL_CONTEXT *cx;
8dff4fc5 1875 const PERL_CONTEXT *dbcx;
1c23e2bd 1876 U8 gimme = GIMME_V;
d527ce7c 1877 const HEK *stash_hek;
8dff4fc5 1878 I32 count = 0;
ce0b554b 1879 bool has_arg = MAXARG && TOPs;
25502127 1880 const COP *lcop;
8dff4fc5 1881
ce0b554b
FC
1882 if (MAXARG) {
1883 if (has_arg)
8dff4fc5 1884 count = POPi;
ce0b554b
FC
1885 else (void)POPs;
1886 }
8dff4fc5 1887
ce0b554b 1888 cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx);
8dff4fc5 1889 if (!cx) {
48ebc325 1890 if (gimme != G_ARRAY) {
8dff4fc5
BM
1891 EXTEND(SP, 1);
1892 RETPUSHUNDEF;
1893 }
1894 RETURN;
1895 }
1896
5e691bc6 1897 CX_DEBUG(cx, "CALLER");
d0279c7c 1898 assert(CopSTASH(cx->blk_oldcop));
e7886211
FC
1899 stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV
1900 ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop))
1901 : NULL;
48ebc325 1902 if (gimme != G_ARRAY) {
27d41816 1903 EXTEND(SP, 1);
d527ce7c 1904 if (!stash_hek)
3280af22 1905 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1906 else {
1907 dTARGET;
d527ce7c 1908 sv_sethek(TARG, stash_hek);
49d8d3a1
MB
1909 PUSHs(TARG);
1910 }
a0d0e21e
LW
1911 RETURN;
1912 }
a0d0e21e 1913
b3ca2e83 1914 EXTEND(SP, 11);
27d41816 1915
d527ce7c 1916 if (!stash_hek)
3280af22 1917 PUSHs(&PL_sv_undef);
d527ce7c
BF
1918 else {
1919 dTARGET;
1920 sv_sethek(TARG, stash_hek);
1921 PUSHTARG;
1922 }
6e449a3a 1923 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
e6dae479 1924 lcop = closest_cop(cx->blk_oldcop, OpSIBLING(cx->blk_oldcop),
25502127
FC
1925 cx->blk_sub.retop, TRUE);
1926 if (!lcop)
1927 lcop = cx->blk_oldcop;
e9e9e546 1928 mPUSHu(CopLINE(lcop));
ce0b554b 1929 if (!has_arg)
a0d0e21e 1930 RETURN;
7766f137
GS
1931 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1932 /* So is ccstack[dbcxix]. */
a5f47741 1933 if (CvHASGV(dbcx->blk_sub.cv)) {
ecf05a58 1934 PUSHs(cv_name(dbcx->blk_sub.cv, 0, 0));
bf38a478 1935 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804
RGS
1936 }
1937 else {
84bafc02 1938 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
bf38a478 1939 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804 1940 }
a0d0e21e
LW
1941 }
1942 else {
84bafc02 1943 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
725c44f9 1944 PUSHs(&PL_sv_zero);
a0d0e21e 1945 }
1c23e2bd 1946 gimme = cx->blk_gimme;
54310121 1947 if (gimme == G_VOID)
3280af22 1948 PUSHs(&PL_sv_undef);
54310121 1949 else
98625aca 1950 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 1951 if (CxTYPE(cx) == CXt_EVAL) {
811a4de9 1952 /* eval STRING */
85a64632 1953 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
78beb4ca
TC
1954 SV *cur_text = cx->blk_eval.cur_text;
1955 if (SvCUR(cur_text) >= 2) {
1956 PUSHs(newSVpvn_flags(SvPVX(cur_text), SvCUR(cur_text)-2,
1957 SvUTF8(cur_text)|SVs_TEMP));
1958 }
1959 else {
1960 /* I think this is will always be "", but be sure */
1961 PUSHs(sv_2mortal(newSVsv(cur_text)));
1962 }
1963
3280af22 1964 PUSHs(&PL_sv_no);
0f79a09d 1965 }
811a4de9 1966 /* require */
0f79a09d 1967 else if (cx->blk_eval.old_namesv) {
6e449a3a 1968 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
3280af22 1969 PUSHs(&PL_sv_yes);
06a5b730 1970 }
811a4de9
GS
1971 /* eval BLOCK (try blocks have old_namesv == 0) */
1972 else {
1973 PUSHs(&PL_sv_undef);
1974 PUSHs(&PL_sv_undef);
1975 }
4633a7c4 1976 }
a682de96
GS
1977 else {
1978 PUSHs(&PL_sv_undef);
1979 PUSHs(&PL_sv_undef);
1980 }
bafb2adc 1981 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
ed094faf 1982 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 1983 {
9513529b
DM
1984 /* slot 0 of the pad contains the original @_ */
1985 AV * const ary = MUTABLE_AV(AvARRAY(MUTABLE_AV(
1986 PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[
1987 cx->blk_sub.olddepth+1]))[0]);
c70927a6 1988 const SSize_t off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 1989
e1a80902 1990 Perl_init_dbargs(aTHX);
a0d0e21e 1991
3280af22
NIS
1992 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1993 av_extend(PL_dbargs, AvFILLp(ary) + off);
f14cf363
TC
1994 if (AvFILLp(ary) + 1 + off)
1995 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
3280af22 1996 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 1997 }
6e449a3a 1998 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5
GS
1999 {
2000 SV * mask ;
72dc9ed5 2001 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 2002
f07626ad 2003 if (old_warnings == pWARN_NONE)
e476b1b5 2004 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
f07626ad
FC
2005 else if (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0)
2006 mask = &PL_sv_undef ;
ac27b0f5 2007 else if (old_warnings == pWARN_ALL ||
75b6c4ca
RGS
2008 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
2009 /* Get the bit mask for $warnings::Bits{all}, because
2010 * it could have been extended by warnings::register */
2011 SV **bits_all;
6673a63c 2012 HV * const bits = get_hv("warnings::Bits", 0);
017a3ce5 2013 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
2014 mask = newSVsv(*bits_all);
2015 }
2016 else {
2017 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
2018 }
2019 }
e476b1b5 2020 else
72dc9ed5 2021 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 2022 mPUSHs(mask);
e476b1b5 2023 }
b3ca2e83 2024
c28fe1ec 2025 PUSHs(cx->blk_oldcop->cop_hints_hash ?
20439bc7 2026 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
b3ca2e83 2027 : &PL_sv_undef);
a0d0e21e
LW
2028 RETURN;
2029}
2030
a0d0e21e
LW
2031PP(pp_reset)
2032{
39644a26 2033 dSP;
ca826051
FC
2034 const char * tmps;
2035 STRLEN len = 0;
2372d073
DM
2036 if (MAXARG < 1 || (!TOPs && !POPs)) {
2037 EXTEND(SP, 1);
ca826051 2038 tmps = NULL, len = 0;
2372d073 2039 }
ca826051
FC
2040 else
2041 tmps = SvPVx_const(POPs, len);
2042 sv_resetpvn(tmps, len, CopSTASH(PL_curcop));
3280af22 2043 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2044 RETURN;
2045}
2046
dd2155a4
DM
2047/* like pp_nextstate, but used instead when the debugger is active */
2048
a0d0e21e
LW
2049PP(pp_dbstate)
2050{
533c011a 2051 PL_curcop = (COP*)PL_op;
a0d0e21e 2052 TAINT_NOT; /* Each statement is presumed innocent */
4ebe6e95 2053 PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp;
a0d0e21e
LW
2054 FREETMPS;
2055
f410a211
NC
2056 PERL_ASYNC_CHECK();
2057
88df5f01 2058 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
a6d69523 2059 || PL_DBsingle_iv || PL_DBsignal_iv || PL_DBtrace_iv)
a0d0e21e 2060 {
39644a26 2061 dSP;
eb578fdb 2062 PERL_CONTEXT *cx;
1c23e2bd 2063 const U8 gimme = G_ARRAY;
0bd48802 2064 GV * const gv = PL_DBgv;
432d4561
JL
2065 CV * cv = NULL;
2066
2067 if (gv && isGV_with_GP(gv))
2068 cv = GvCV(gv);
a0d0e21e 2069
c2cb6f77 2070 if (!cv || (!CvROOT(cv) && !CvXSUB(cv)))
cea2e8a9 2071 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 2072
aea4f609
DM
2073 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2074 /* don't do recursive DB::DB call */
a0d0e21e 2075 return NORMAL;
748a9306 2076
aed2304a 2077 if (CvISXSUB(cv)) {
8ae997c5
DM
2078 ENTER;
2079 SAVEI32(PL_debug);
2080 PL_debug = 0;
2081 SAVESTACK_POS();
8a44b450 2082 SAVETMPS;
c127bd3a
SF
2083 PUSHMARK(SP);
2084 (void)(*CvXSUB(cv))(aTHX_ cv);
c127bd3a 2085 FREETMPS;
a57c6685 2086 LEAVE;
c127bd3a
SF
2087 return NORMAL;
2088 }
2089 else {
ed8ff0f3 2090 cx = cx_pushblock(CXt_SUB, gimme, SP, PL_savestack_ix);
a73d8813
DM
2091 cx_pushsub(cx, cv, PL_op->op_next, 0);
2092 /* OP_DBSTATE's op_private holds hint bits rather than
2093 * the lvalue-ish flags seen in OP_ENTERSUB. So cancel
2094 * any CxLVAL() flags that have now been mis-calculated */
2095 cx->blk_u16 = 0;
8ae997c5
DM
2096
2097 SAVEI32(PL_debug);
2098 PL_debug = 0;
2099 SAVESTACK_POS();
c127bd3a 2100 CvDEPTH(cv)++;
d2af2719 2101 if (CvDEPTH(cv) >= 2)
9d976ff5 2102 pad_push(CvPADLIST(cv), CvDEPTH(cv));
9d976ff5 2103 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv));
c127bd3a
SF
2104 RETURNOP(CvSTART(cv));
2105 }
a0d0e21e
LW
2106 }
2107 else
2108 return NORMAL;
2109}
2110
0663a8f8 2111
2b9a6457
VP
2112PP(pp_enter)
2113{
1c23e2bd 2114 U8 gimme = GIMME_V;
2b9a6457 2115
d4ce7588
DM
2116 (void)cx_pushblock(CXt_BLOCK, gimme, PL_stack_sp, PL_savestack_ix);
2117 return NORMAL;
2b9a6457
VP
2118}
2119
d4ce7588 2120
2b9a6457
VP
2121PP(pp_leave)
2122{
eb578fdb 2123 PERL_CONTEXT *cx;
f5ddd604 2124 SV **oldsp;
1c23e2bd 2125 U8 gimme;
2b9a6457 2126
4ebe6e95 2127 cx = CX_CUR();
61d3b95a 2128 assert(CxTYPE(cx) == CXt_BLOCK);
4df352a8
DM
2129
2130 if (PL_op->op_flags & OPf_SPECIAL)
c349b9a0
DM
2131 /* fake block should preserve $1 et al; e.g. /(...)/ while ...; */
2132 cx->blk_oldpm = PL_curpm;
4df352a8 2133
f5ddd604 2134 oldsp = PL_stack_base + cx->blk_oldsp;
61d3b95a 2135 gimme = cx->blk_gimme;
2b9a6457 2136
0663a8f8 2137 if (gimme == G_VOID)
f5ddd604 2138 PL_stack_sp = oldsp;
0663a8f8 2139 else
f5ddd604 2140 leave_adjust_stacks(oldsp, oldsp, gimme,
75bc488d 2141 PL_op->op_private & OPpLVALUE ? 3 : 1);
67f63db7 2142
2f450c1b 2143 CX_LEAVE_SCOPE(cx);
ed8ff0f3 2144 cx_popblock(cx);
5da525e9 2145 CX_POP(cx);
2b9a6457 2146
0663a8f8 2147 return NORMAL;
2b9a6457
VP
2148}
2149
eaa9f768
JH
2150static bool
2151S_outside_integer(pTHX_ SV *sv)
2152{
2153 if (SvOK(sv)) {
2154 const NV nv = SvNV_nomg(sv);
415b66b2
JH
2155 if (Perl_isinfnan(nv))
2156 return TRUE;
eaa9f768
JH
2157#ifdef NV_PRESERVES_UV
2158 if (nv < (NV)IV_MIN || nv > (NV)IV_MAX)
2159 return TRUE;
2160#else
2161 if (nv <= (NV)IV_MIN)
2162 return TRUE;
2163 if ((nv > 0) &&
2164 ((nv > (NV)UV_MAX ||
2165 SvUV_nomg(sv) > (UV)IV_MAX)))
2166 return TRUE;
2167#endif
2168 }
2169 return FALSE;
2170}
2171
a0d0e21e
LW
2172PP(pp_enteriter)
2173{
20b7effb 2174 dSP; dMARK;
eb578fdb 2175 PERL_CONTEXT *cx;
1c23e2bd 2176 const U8 gimme = GIMME_V;
4ad63d70 2177 void *itervarp; /* GV or pad slot of the iteration variable */
f0bb9bf7 2178 SV *itersave; /* the old var in the iterator var slot */
93661e56 2179 U8 cxflags = 0;
a0d0e21e 2180
aafca525 2181 if (PL_op->op_targ) { /* "my" variable */
4ad63d70
DM
2182 itervarp = &PAD_SVl(PL_op->op_targ);
2183 itersave = *(SV**)itervarp;
2184 assert(itersave);
aafca525 2185 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
fdb8b82b
DM
2186 /* the SV currently in the pad slot is never live during
2187 * iteration (the slot is always aliased to one of the items)
2188 * so it's always stale */
4ad63d70 2189 SvPADSTALE_on(itersave);
14f338dc 2190 }
4ad63d70 2191 SvREFCNT_inc_simple_void_NN(itersave);
93661e56 2192 cxflags = CXp_FOR_PAD;
54b9620d 2193 }
d39c26a6
FC
2194 else {
2195 SV * const sv = POPs;
4ad63d70 2196 itervarp = (void *)sv;
d4e2b4d6 2197 if (LIKELY(isGV(sv))) { /* symbol table variable */
6d3ca00e
DM
2198 itersave = GvSV(sv);
2199 SvREFCNT_inc_simple_void(itersave);
93661e56 2200 cxflags = CXp_FOR_GV;
28e0cf42
DM
2201 if (PL_op->op_private & OPpITER_DEF)
2202 cxflags |= CXp_FOR_DEF;
d4e2b4d6
DM
2203 }
2204 else { /* LV ref: for \$foo (...) */
2205 assert(SvTYPE(sv) == SVt_PVMG);
2206 assert(SvMAGIC(sv));
2207 assert(SvMAGIC(sv)->mg_type == PERL_MAGIC_lvref);
2208 itersave = NULL;
93661e56 2209 cxflags = CXp_FOR_LVREF;
d4e2b4d6 2210 }
d39c26a6 2211 }
28e0cf42
DM
2212 /* OPpITER_DEF (implicit $_) should only occur with a GV iter var */
2213 assert((cxflags & CXp_FOR_GV) || !(PL_op->op_private & OPpITER_DEF));
0d863452 2214
75fbe096
DM
2215 /* Note that this context is initially set as CXt_NULL. Further on
2216 * down it's changed to one of the CXt_LOOP_*. Before it's changed,
2217 * there mustn't be anything in the blk_loop substruct that requires
2218 * freeing or undoing, in case we die in the meantime. And vice-versa.
2219 */
ed8ff0f3 2220 cx = cx_pushblock(cxflags, gimme, MARK, PL_savestack_ix);
d1b6bf72 2221 cx_pushloop_for(cx, itervarp, itersave);
2c49879e 2222
533c011a 2223 if (PL_op->op_flags & OPf_STACKED) {
2c49879e
DM
2224 /* OPf_STACKED implies either a single array: for(@), with a
2225 * single AV on the stack, or a range: for (1..5), with 1 and 5 on
2226 * the stack */
d01136d6
BS
2227 SV *maybe_ary = POPs;
2228 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2c49879e 2229 /* range */
89ea2908 2230 dPOPss;
d01136d6 2231 SV * const right = maybe_ary;
93661e56 2232 if (UNLIKELY(cxflags & CXp_FOR_LVREF))
d39c26a6 2233 DIE(aTHX_ "Assigned value is not a reference");
984a4bea
RD
2234 SvGETMAGIC(sv);
2235 SvGETMAGIC(right);
4fe3f0fa 2236 if (RANGE_IS_NUMERIC(sv,right)) {
c6fdafd0 2237 cx->cx_type |= CXt_LOOP_LAZYIV;
eaa9f768
JH
2238 if (S_outside_integer(aTHX_ sv) ||
2239 S_outside_integer(aTHX_ right))
076d9a11 2240 DIE(aTHX_ "Range iterator outside integer range");
f52e41ad
FC
2241 cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv);
2242 cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right);
89ea2908 2243 }
3f63a782 2244 else {
d01136d6 2245 cx->cx_type |= CXt_LOOP_LAZYSV;
d01136d6
BS
2246 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2247 cx->blk_loop.state_u.lazysv.end = right;
2c49879e 2248 SvREFCNT_inc_simple_void_NN(right);
d01136d6 2249 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
267cc4a8
NC
2250 /* This will do the upgrade to SVt_PV, and warn if the value
2251 is uninitialised. */
10516c54 2252 (void) SvPV_nolen_const(right);
267cc4a8
NC
2253 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2254 to replace !SvOK() with a pointer to "". */
2255 if (!SvOK(right)) {
2256 SvREFCNT_dec(right);
d01136d6 2257 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
267cc4a8 2258 }
3f63a782 2259 }
89ea2908 2260 }
d01136d6 2261 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2c49879e 2262 /* for (@array) {} */
93661e56 2263 cx->cx_type |= CXt_LOOP_ARY;
502c6561 2264 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2c49879e 2265 SvREFCNT_inc_simple_void_NN(maybe_ary);
d01136d6
BS
2266 cx->blk_loop.state_u.ary.ix =
2267 (PL_op->op_private & OPpITER_REVERSED) ?
2268 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2269 -1;
ef3e5ea9 2270 }
8a1f10dd 2271 /* EXTEND(SP, 1) not needed in this branch because we just did POPs */
89ea2908 2272 }
d01136d6 2273 else { /* iterating over items on the stack */
93661e56 2274 cx->cx_type |= CXt_LOOP_LIST;
1bef65a2 2275 cx->blk_oldsp = SP - PL_stack_base;
93661e56
DM
2276 cx->blk_loop.state_u.stack.basesp = MARK - PL_stack_base;
2277 cx->blk_loop.state_u.stack.ix =
2278 (PL_op->op_private & OPpITER_REVERSED)
2279 ? cx->blk_oldsp + 1
2280 : cx->blk_loop.state_u.stack.basesp;
8a1f10dd
DM
2281 /* pre-extend stack so pp_iter doesn't have to check every time
2282 * it pushes yes/no */
2283 EXTEND(SP, 1);
4633a7c4 2284 }
a0d0e21e
LW
2285
2286 RETURN;
2287}
2288
2289PP(pp_enterloop)
2290{
eb578fdb 2291 PERL_CONTEXT *cx;
1c23e2bd 2292 const U8 gimme = GIMME_V;
a0d0e21e 2293
d4ce7588 2294 cx = cx_pushblock(CXt_LOOP_PLAIN, gimme, PL_stack_sp, PL_savestack_ix);
d1b6bf72 2295 cx_pushloop_plain(cx);
d4ce7588 2296 return NORMAL;
a0d0e21e
LW
2297}
2298
d4ce7588 2299
a0d0e21e
LW
2300PP(pp_leaveloop)
2301{
eb578fdb 2302 PERL_CONTEXT *cx;
1c23e2bd 2303 U8 gimme;
032736ab 2304 SV **base;
f5ddd604 2305 SV **oldsp;
a0d0e21e 2306
4ebe6e95 2307 cx = CX_CUR();
3b719c58 2308 assert(CxTYPE_is_LOOP(cx));
032736ab
DM
2309 oldsp = PL_stack_base + cx->blk_oldsp;
2310 base = CxTYPE(cx) == CXt_LOOP_LIST
1bef65a2 2311 ? PL_stack_base + cx->blk_loop.state_u.stack.basesp
032736ab 2312 : oldsp;
61d3b95a 2313 gimme = cx->blk_gimme;
f86702cc 2314
0663a8f8 2315 if (gimme == G_VOID)
032736ab 2316 PL_stack_sp = base;
0663a8f8 2317 else
032736ab 2318 leave_adjust_stacks(oldsp, base, gimme,
75bc488d 2319 PL_op->op_private & OPpLVALUE ? 3 : 1);
f86702cc 2320
2f450c1b 2321 CX_LEAVE_SCOPE(cx);
d1b6bf72 2322 cx_poploop(cx); /* Stack values are safe: release loop vars ... */
ed8ff0f3 2323 cx_popblock(cx);
5da525e9 2324 CX_POP(cx);
f86702cc 2325
f86702cc 2326 return NORMAL;
a0d0e21e
LW
2327}
2328
31ccb4f5
DM
2329
2330/* This duplicates most of pp_leavesub, but with additional code to handle
2331 * return args in lvalue context. It was forked from pp_leavesub to
2332 * avoid slowing down that function any further.
2333 *
2334 * Any changes made to this function may need to be copied to pp_leavesub
2335 * and vice-versa.
c349b9a0
DM
2336 *
2337 * also tail-called by pp_return
57486a97
DM
2338 */
2339
31ccb4f5 2340PP(pp_leavesublv)
3bdf583b 2341{
1c23e2bd 2342 U8 gimme;
57486a97 2343 PERL_CONTEXT *cx;
799da9d7 2344 SV **oldsp;
5da525e9 2345 OP *retop;
57486a97 2346
4ebe6e95 2347 cx = CX_CUR();
61d3b95a
DM
2348 assert(CxTYPE(cx) == CXt_SUB);
2349
2350 if (CxMULTICALL(cx)) {
1f0ba93b
DM
2351 /* entry zero of a stack is always PL_sv_undef, which
2352 * simplifies converting a '()' return into undef in scalar context */
2353 assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef);
85ecf147 2354 return 0;
1f0ba93b 2355 }
85ecf147 2356
61d3b95a 2357 gimme = cx->blk_gimme;
799da9d7 2358 oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */
57486a97 2359
799da9d7
DM
2360 if (gimme == G_VOID)
2361 PL_stack_sp = oldsp;
2362 else {
2363 U8 lval = CxLVAL(cx);
2364 bool is_lval = (lval && !(lval & OPpENTERSUB_INARGS));
2365 const char *what = NULL;
2366
2367 if (gimme == G_SCALAR) {
2368 if (is_lval) {
2369 /* check for bad return arg */
2370 if (oldsp < PL_stack_sp) {
2371 SV *sv = *PL_stack_sp;
2372 if ((SvPADTMP(sv) || SvREADONLY(sv))) {
2373 what =
2374 SvREADONLY(sv) ? (sv == &PL_sv_undef) ? "undef"
2375 : "a readonly value" : "a temporary";
2376 }
2377 else goto ok;
2378 }
2379 else {
2380 /* sub:lvalue{} will take us here. */
2381 what = "undef";
2382 }
2383 croak:
2384 Perl_croak(aTHX_
2385 "Can't return %s from lvalue subroutine", what);
2386 }
57486a97 2387
799da9d7 2388 ok:
e02ce34b 2389 leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2);
e80c4acf 2390
799da9d7
DM
2391 if (lval & OPpDEREF) {
2392 /* lval_sub()->{...} and similar */
2393 dSP;
2394 SvGETMAGIC(TOPs);
2395 if (!SvOK(TOPs)) {
2396 TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF);
2397 }
2398 PUTBACK;
2399 }
2400 }
2401 else {
2402 assert(gimme == G_ARRAY);
2403 assert (!(lval & OPpDEREF));
2404
2405 if (is_lval) {
2406 /* scan for bad return args */
2407 SV **p;
2408 for (p = PL_stack_sp; p > oldsp; p--) {
2409 SV *sv = *p;
2410 /* the PL_sv_undef exception is to allow things like
2411 * this to work, where PL_sv_undef acts as 'skip'
2412 * placeholder on the LHS of list assigns:
2413 * sub foo :lvalue { undef }
2414 * ($a, undef, foo(), $b) = 1..4;
2415 */
2416 if (sv != &PL_sv_undef && (SvPADTMP(sv) || SvREADONLY(sv)))
2417 {
2418 /* Might be flattened array after $#array = */
2419 what = SvREADONLY(sv)
2420 ? "a readonly value" : "a temporary";
2421 goto croak;
2422 }
2423 }
2424 }
2425
e02ce34b 2426 leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2);
799da9d7 2427 }
3bdf583b 2428 }
57486a97 2429
2f450c1b 2430 CX_LEAVE_SCOPE(cx);
a73d8813 2431 cx_popsub(cx); /* Stack values are safe: release CV and @_ ... */
ed8ff0f3 2432 cx_popblock(cx);
5da525e9
DM
2433 retop = cx->blk_sub.retop;
2434 CX_POP(cx);
57486a97 2435
5da525e9 2436 return retop;
3bdf583b
FC
2437}
2438
57486a97 2439
a0d0e21e
LW
2440PP(pp_return)
2441{
20b7effb 2442 dSP; dMARK;
eb578fdb 2443 PERL_CONTEXT *cx;
0bd48802
AL
2444 const I32 cxix = dopoptosub(cxstack_ix);
2445
d40dc6b1
DM
2446 assert(cxstack_ix >= 0);
2447 if (cxix < cxstack_ix) {
2448 if (cxix < 0) {
79646418
DM
2449 if (!( PL_curstackinfo->si_type == PERLSI_SORT
2450 || ( PL_curstackinfo->si_type == PERLSI_MULTICALL
2451 && (cxstack[0].cx_type & CXp_SUB_RE_FAKE))
2452 )
2453 )
3089a108 2454 DIE(aTHX_ "Can't return outside a subroutine");
79646418
DM
2455 /* We must be in:
2456 * a sort block, which is a CXt_NULL not a CXt_SUB;
2457 * or a /(?{...})/ block.
2458 * Handle specially. */
2459 assert(CxTYPE(&cxstack[0]) == CXt_NULL
2460 || ( CxTYPE(&cxstack[0]) == CXt_SUB
2461 && (cxstack[0].cx_type & CXp_SUB_RE_FAKE)));
3089a108
DM
2462 if (cxstack_ix > 0) {
2463 /* See comment below about context popping. Since we know
2464 * we're scalar and not lvalue, we can preserve the return
2465 * value in a simpler fashion than there. */
2466 SV *sv = *SP;
d40dc6b1 2467 assert(cxstack[0].blk_gimme == G_SCALAR);
3089a108
DM
2468 if ( (sp != PL_stack_base)
2469 && !(SvFLAGS(sv) & (SVs_TEMP|SVs_PADTMP))
2470 )
2471 *SP = sv_mortalcopy(sv);
2472 dounwind(0);
d40dc6b1 2473 }
3089a108
DM
2474 /* caller responsible for popping cxstack[0] */
2475 return 0;
d40dc6b1 2476 }
3089a108
DM
2477
2478 /* There are contexts that need popping. Doing this may free the
c349b9a0 2479 * return value(s), so preserve them first: e.g. popping the plain
3089a108
DM
2480 * loop here would free $x:
2481 * sub f { { my $x = 1; return $x } }
2482 * We may also need to shift the args down; for example,
2483 * for (1,2) { return 3,4 }
c349b9a0
DM
2484 * leaves 1,2,3,4 on the stack. Both these actions will be done by
2485 * leave_adjust_stacks(), along with freeing any temps. Note that
2486 * whoever we tail-call (e.g. pp_leaveeval) will also call
2487 * leave_adjust_stacks(); however, the second call is likely to
2488 * just see a bunch of SvTEMPs with a ref count of 1, and so just
2489 * pass them through, rather than copying them again. So this
2490 * isn't as inefficient as it sounds.
3089a108
DM
2491 */
2492 cx = &cxstack[cxix];
3089a108 2493 PUTBACK;
e02ce34b
DM
2494 if (cx->blk_gimme != G_VOID)
2495 leave_adjust_stacks(MARK, PL_stack_base + cx->blk_oldsp,
fc6e609e
DM
2496 cx->blk_gimme,
2497 CxTYPE(cx) == CXt_SUB && CvLVALUE(cx->blk_sub.cv)
2498 ? 3 : 0);
0663a8f8 2499 SPAGAIN;
a0d0e21e 2500 dounwind(cxix);
42831ce4 2501 cx = &cxstack[cxix]; /* CX stack may have been realloced */
d40dc6b1 2502 }
3089a108
DM
2503 else {
2504 /* Like in the branch above, we need to handle any extra junk on
2505 * the stack. But because we're not also popping extra contexts, we
2506 * don't have to worry about prematurely freeing args. So we just
2507 * need to do the bare minimum to handle junk, and leave the main
2508 * arg processing in the function we tail call, e.g. pp_leavesub.
2509 * In list context we have to splice out the junk; in scalar
2510 * context we can leave as-is (pp_leavesub will later return the
2511 * top stack element). But for an empty arg list, e.g.
2512 * for (1,2) { return }
2513 * we need to set sp = oldsp so that pp_leavesub knows to push
2514 * &PL_sv_undef onto the stack.
2515 */
3ebe7c5a
DM
2516 SV **oldsp;
2517 cx = &cxstack[cxix];
2518 oldsp = PL_stack_base + cx->blk_oldsp;
2519 if (oldsp != MARK) {
2520 SSize_t nargs = SP - MARK;
2521 if (nargs) {
2522 if (cx->blk_gimme == G_ARRAY) {
2523 /* shift return args to base of call stack frame */
2524 Move(MARK + 1, oldsp + 1, nargs, SV*);
2525 PL_stack_sp = oldsp + nargs;
2526 }
6228a1e1 2527 }
3ebe7c5a
DM
2528 else
2529 PL_stack_sp = oldsp;
13929c4c 2530 }
3089a108 2531 }
617a4f41
DM
2532
2533 /* fall through to a normal exit */
2534 switch (CxTYPE(cx)) {
2535 case CXt_EVAL:
2536 return CxTRYBLOCK(cx)
2537 ? Perl_pp_leavetry(aTHX)
2538 : Perl_pp_leaveeval(aTHX);
2539 case CXt_SUB:
13929c4c 2540 return CvLVALUE(cx->blk_sub.cv)
31ccb4f5 2541 ? Perl_pp_leavesublv(aTHX)
13929c4c 2542 : Perl_pp_leavesub(aTHX);
7766f137 2543 case CXt_FORMAT:
617a4f41 2544 return Perl_pp_leavewrite(aTHX);
a0d0e21e 2545 default:
5637ef5b 2546 DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx));
a0d0e21e 2547 }
a0d0e21e
LW
2548}
2549
42b5eca0 2550/* find the enclosing loop or labelled loop and dounwind() back to it. */
4f443c3d 2551
31705cda 2552static PERL_CONTEXT *
42b5eca0 2553S_unwind_loop(pTHX)
a0d0e21e 2554{
a0d0e21e 2555 I32 cxix;
1f039d60
FC
2556 if (PL_op->op_flags & OPf_SPECIAL) {
2557 cxix = dopoptoloop(cxstack_ix);
2558 if (cxix < 0)
2559 /* diag_listed_as: Can't "last" outside a loop block */
42b5eca0
DM
2560 Perl_croak(aTHX_ "Can't \"%s\" outside a loop block",
2561 OP_NAME(PL_op));
1f039d60
FC
2562 }
2563 else {
2564 dSP;
2565 STRLEN label_len;
2566 const char * const label =
2567 PL_op->op_flags & OPf_STACKED
2568 ? SvPV(TOPs,label_len)
2569 : (label_len = strlen(cPVOP->op_pv), cPVOP->op_pv);
2570 const U32 label_flags =
2571 PL_op->op_flags & OPf_STACKED
2572 ? SvUTF8(POPs)
2573 : (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
2574 PUTBACK;
2575 cxix = dopoptolabel(label, label_len, label_flags);
2576 if (cxix < 0)
2577 /* diag_listed_as: Label not found for "last %s" */
147e3846 2578 Perl_croak(aTHX_ "Label not found for \"%s %" SVf "\"",
42b5eca0 2579 OP_NAME(PL_op),
1f039d60
FC
2580 SVfARG(PL_op->op_flags & OPf_STACKED
2581 && !SvGMAGICAL(TOPp1s)
2582 ? TOPp1s
2583 : newSVpvn_flags(label,
2584 label_len,
2585 label_flags | SVs_TEMP)));
2586 }
2587 if (cxix < cxstack_ix)
2588 dounwind(cxix);
dc7f00ca 2589 return &cxstack[cxix];
1f039d60
FC
2590}
2591
dc7f00ca 2592
1f039d60
FC
2593PP(pp_last)
2594{
eb578fdb 2595 PERL_CONTEXT *cx;
5da525e9 2596 OP* nextop;
9d4ba2ae 2597
42b5eca0 2598 cx = S_unwind_loop(aTHX);
4df352a8 2599
93661e56 2600 assert(CxTYPE_is_LOOP(cx));
1bef65a2
DM
2601 PL_stack_sp = PL_stack_base
2602 + (CxTYPE(cx) == CXt_LOOP_LIST
2603 ? cx->blk_loop.state_u.stack.basesp
2604 : cx->blk_oldsp
2605 );
a0d0e21e 2606
a1f49e72 2607 TAINT_NOT;
f86702cc
PP
2608
2609 /* Stack values are safe: */
2f450c1b 2610 CX_LEAVE_SCOPE(cx);
d1b6bf72 2611 cx_poploop(cx); /* release loop vars ... */
ed8ff0f3 2612 cx_popblock(cx);
5da525e9
DM
2613 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2614 CX_POP(cx);
a0d0e21e 2615
5da525e9 2616 return nextop;
a0d0e21e
LW
2617}
2618
2619PP(pp_next)
2620{
eb578fdb 2621 PERL_CONTEXT *cx;
a0d0e21e 2622
cd97dc8d
DM
2623 /* if not a bare 'next' in the main scope, search for it */
2624 cx = CX_CUR();
2625 if (!((PL_op->op_flags & OPf_SPECIAL) && CxTYPE_is_LOOP(cx)))
2626 cx = S_unwind_loop(aTHX);
a0d0e21e 2627
ed8ff0f3 2628 cx_topblock(cx);
3a1b2b9e 2629 PL_curcop = cx->blk_oldcop;
47c9d59f 2630 PERL_ASYNC_CHECK();
d57ce4df 2631 return (cx)->blk_loop.my_op->op_nextop;
a0d0e21e
LW
2632}
2633
2634PP(pp_redo)
2635{
42b5eca0 2636 PERL_CONTEXT *cx = S_unwind_loop(aTHX);
dc7f00ca 2637 OP* redo_op = cx->blk_loop.my_op->op_redoop;
a0d0e21e 2638
a034e688
DM
2639 if (redo_op->op_type == OP_ENTER) {
2640 /* pop one less context to avoid $x being freed in while (my $x..) */
2641 cxstack_ix++;
dc7f00ca
DM
2642 cx = CX_CUR();
2643 assert(CxTYPE(cx) == CXt_BLOCK);
a034e688
DM
2644 redo_op = redo_op->op_next;
2645 }
2646
936c78b5 2647 FREETMPS;
ef588991 2648 CX_LEAVE_SCOPE(cx);
ed8ff0f3 2649 cx_topblock(cx);
3a1b2b9e 2650 PL_curcop = cx->blk_oldcop;
47c9d59f 2651 PERL_ASYNC_CHECK();
a034e688 2652 return redo_op;
a0d0e21e
LW
2653}
2654
0824fdcb 2655STATIC OP *
5db1eb8d 2656S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit)
a0d0e21e 2657{
a0d0e21e 2658 OP **ops = opstack;
a1894d81 2659 static const char* const too_deep = "Target of goto is too deeply nested";
a0d0e21e 2660
7918f24d
NC
2661 PERL_ARGS_ASSERT_DOFINDLABEL;
2662
fc36a67e 2663 if (ops >= oplimit)
0157ef98 2664 Perl_croak(aTHX_ "%s", too_deep);
11343788
MB
2665 if (o->op_type == OP_LEAVE ||
2666 o->op_type == OP_SCOPE ||
2667 o->op_type == OP_LEAVELOOP ||
33d34e4c 2668 o->op_type == OP_LEAVESUB ||
11343788 2669 o->op_type == OP_LEAVETRY)
fc36a67e 2670 {
5dc0d613 2671 *ops++ = cUNOPo->op_first;
fc36a67e 2672 if (ops >= oplimit)
0157ef98 2673 Perl_croak(aTHX_ "%s", too_deep);
fc36a67e 2674 }
c4aa4e48 2675 *ops = 0;
11343788 2676 if (o->op_flags & OPf_KIDS) {
aec46f14 2677 OP *kid;
a0d0e21e 2678 /* First try all the kids at this level, since that's likeliest. */
e6dae479 2679 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
7e8f1eac 2680 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
5db1eb8d
BF
2681 STRLEN kid_label_len;
2682 U32 kid_label_flags;
2683 const char *kid_label = CopLABEL_len_flags(kCOP,
2684 &kid_label_len, &kid_label_flags);
2685 if (kid_label && (
2686 ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
2687 (flags & SVf_UTF8)
2688 ? (bytes_cmp_utf8(
2689 (const U8*)kid_label, kid_label_len,
2690 (const U8*)label, len) == 0)
2691 : (bytes_cmp_utf8(
2692 (const U8*)label, len,
2693 (const U8*)kid_label, kid_label_len) == 0)
eade7155
BF
2694 : ( len == kid_label_len && ((kid_label == label)
2695 || memEQ(kid_label, label, len)))))
7e8f1eac
AD
2696 return kid;
2697 }
a0d0e21e 2698 }
e6dae479 2699 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
3280af22 2700 if (kid == PL_lastgotoprobe)
a0d0e21e 2701 continue;
ed8d0fe2
SM
2702 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2703 if (ops == opstack)
2704 *ops++ = kid;
2705 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2706 ops[-1]->op_type == OP_DBSTATE)
2707 ops[-1] = kid;
2708 else
2709 *ops++ = kid;
2710 }
5db1eb8d 2711 if ((o = dofindlabel(kid, label, len, flags, ops, oplimit)))
11343788 2712 return o;
a0d0e21e
LW
2713 }
2714 }
c4aa4e48 2715 *ops = 0;
a0d0e21e
LW
2716 return 0;
2717}
2718
b1c05ba5
DM
2719
2720/* also used for: pp_dump() */
2721
2722PP(pp_goto)
a0d0e21e 2723{
27da23d5 2724 dVAR; dSP;
cbbf8932 2725 OP *retop = NULL;
a0d0e21e 2726 I32 ix;
eb578fdb 2727 PERL_CONTEXT *cx;
fc36a67e
PP
2728#define GOTO_DEPTH 64
2729 OP *enterops[GOTO_DEPTH];
cbbf8932 2730 const char *label = NULL;
5db1eb8d
BF
2731 STRLEN label_len = 0;
2732 U32 label_flags = 0;
bfed75c6 2733 const bool do_dump = (PL_op->op_type == OP_DUMP);
a1894d81 2734 static const char* const must_have_label = "goto must have label";
a0d0e21e 2735
533c011a 2736 if (PL_op->op_flags & OPf_STACKED) {
7d1d69cb
DM
2737 /* goto EXPR or goto &foo */
2738
9d4ba2ae 2739 SV * const sv = POPs;
55b37f1c 2740 SvGETMAGIC(sv);
a0d0e21e 2741
a0d0e21e 2742 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
0fa3d31d 2743 /* This egregious kludge implements goto &subroutine */
a0d0e21e 2744 I32 cxix;
eb578fdb 2745 PERL_CONTEXT *cx;
ea726b52 2746 CV *cv = MUTABLE_CV(SvRV(sv));
049bd5ff 2747 AV *arg = GvAV(PL_defgv);
a0d0e21e 2748
5d52e310 2749 while (!CvROOT(cv) && !CvXSUB(cv)) {
7fc63493 2750 const GV * const gv = CvGV(cv);
e8f7dd13 2751 if (gv) {
7fc63493 2752 GV *autogv;
e8f7dd13
GS
2753 SV *tmpstr;
2754 /* autoloaded stub? */
2755 if (cv != GvCV(gv) && (cv = GvCV(gv)))
5d52e310 2756 continue;
c271df94
BF
2757 autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv),
2758 GvNAMELEN(gv),
2759 GvNAMEUTF8(gv) ? SVf_UTF8 : 0);
e8f7dd13 2760 if (autogv && (cv = GvCV(autogv)))
5d52e310 2761 continue;
e8f7dd13 2762 tmpstr = sv_newmortal();
c445ea15 2763 gv_efullname3(tmpstr, gv, NULL);
147e3846 2764 DIE(aTHX_ "Goto undefined subroutine &%" SVf, SVfARG(tmpstr));
4aa0a1f7 2765 }
cea2e8a9 2766 DIE(aTHX_ "Goto undefined subroutine");
4aa0a1f7
AD
2767 }
2768
a0d0e21e 2769 cxix = dopoptosub(cxstack_ix);
d338c0c2
DM
2770 if (cxix < 0) {
2771 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
8da3792e 2772 }
d338c0c2 2773 cx = &cxstack[cxix];
564abe23 2774 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2d43a17f 2775 if (CxTYPE(cx) == CXt_EVAL) {
c74ace89 2776 if (CxREALEVAL(cx))
00455a92 2777 /* diag_listed_as: Can't goto subroutine from an eval-%s */
c74ace89
DM
2778 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2779 else
00455a92 2780 /* diag_listed_as: Can't goto subroutine from an eval-%s */
c74ace89 2781 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2d43a17f 2782 }
9850bf21
RH
2783 else if (CxMULTICALL(cx))
2784 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
d338c0c2
DM
2785
2786 /* First do some returnish stuff. */
2787
2788 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2789 FREETMPS;
2790 if (cxix < cxstack_ix) {
2791 dounwind(cxix);
2792 }
7e637ba4 2793 cx = CX_CUR();
ed8ff0f3 2794 cx_topblock(cx);
d338c0c2 2795 SPAGAIN;
39de75fd 2796
8ae997c5
DM
2797 /* protect @_ during save stack unwind. */
2798 if (arg)
2799 SvREFCNT_inc_NN(sv_2mortal(MUTABLE_SV(arg)));
2800
2801 assert(PL_scopestack_ix == cx->blk_oldscopesp);
dfe0f39b 2802 CX_LEAVE_SCOPE(cx);
8ae997c5 2803
bafb2adc 2804 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
a73d8813 2805 /* this is part of cx_popsub_args() */
9513529b 2806 AV* av = MUTABLE_AV(PAD_SVl(0));
e2657e18
DM
2807 assert(AvARRAY(MUTABLE_AV(
2808 PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[
2809 CvDEPTH(cx->blk_sub.cv)])) == PL_curpad);
2810
f72bdec3
DM
2811 /* we are going to donate the current @_ from the old sub
2812 * to the new sub. This first part of the donation puts a
2813 * new empty AV in the pad[0] slot of the old sub,
2814 * unless pad[0] and @_ differ (e.g. if the old sub did
2815 * local *_ = []); in which case clear the old pad[0]
2816 * array in the usual way */
95b2f486
DM
2817 if (av == arg || AvREAL(av))
2818 clear_defarray(av, av == arg);
049bd5ff 2819 else CLEAR_ARGARRAY(av);
a0d0e21e 2820 }
88c11d84 2821
b1e25d05
DM
2822 /* don't restore PL_comppad here. It won't be needed if the
2823 * sub we're going to is non-XS, but restoring it early then
2824 * croaking (e.g. the "Goto undefined subroutine" below)
2825 * means the CX block gets processed again in dounwind,
2826 * but this time with the wrong PL_comppad */
88c11d84 2827
1d59c038
FC
2828 /* A destructor called during LEAVE_SCOPE could have undefined
2829 * our precious cv. See bug #99850. */
2830 if (!CvROOT(cv) && !CvXSUB(cv)) {
2831 const GV * const gv = CvGV(cv);
2832 if (gv) {
2833 SV * const tmpstr = sv_newmortal();
2834 gv_efullname3(tmpstr, gv, NULL);
147e3846 2835 DIE(aTHX_ "Goto undefined subroutine &%" SVf,
1d59c038
FC
2836 SVfARG(tmpstr));
2837 }
2838 DIE(aTHX_ "Goto undefined subroutine");
2839 }
2840
cd17cc2e
DM
2841 if (CxTYPE(cx) == CXt_SUB) {
2842 CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth;
2843 SvREFCNT_dec_NN(cx->blk_sub.cv);
2844 }
2845
a0d0e21e 2846 /* Now do some callish stuff. */
aed2304a 2847 if (CvISXSUB(cv)) {
ad39f3a2 2848 const SSize_t items = arg ? AvFILL(arg) + 1 : 0;
cd313eb4 2849 const bool m = arg ? cBOOL(SvRMAGICAL(arg)) : 0;
049bd5ff
FC
2850 SV** mark;
2851
8ae997c5 2852 ENTER;
80774f05
DM
2853 SAVETMPS;
2854 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2855
049bd5ff 2856 /* put GvAV(defgv) back onto stack */
8c9d3376
FC
2857 if (items) {
2858 EXTEND(SP, items+1); /* @_ could have been extended. */
8c9d3376 2859 }
049bd5ff 2860 mark = SP;
ad39f3a2 2861 if (items) {
de935cc9 2862 SSize_t index;
ad39f3a2 2863 bool r = cBOOL(AvREAL(arg));
b1464ded 2864 for (index=0; index<items; index++)
ad39f3a2
FC
2865 {
2866 SV *sv;
2867 if (m) {
2868 SV ** const svp = av_fetch(arg, index, 0);
2869 sv = svp ? *svp : NULL;
dd2a7f90 2870 }
ad39f3a2
FC
2871 else sv = AvARRAY(arg)[index];
2872 SP[index+1] = sv
2873 ? r ? SvREFCNT_inc_NN(sv_2mortal(sv)) : sv
2874 : sv_2mortal(newSVavdefelem(arg, index, 1));
2875 }
049bd5ff 2876 }
ad39f3a2 2877 SP += items;
049bd5ff
FC
2878 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2879 /* Restore old @_ */
b405d38b 2880 CX_POP_SAVEARRAY(cx);
b1464ded 2881 }
1fa4e549 2882
51eb35b5 2883 retop = cx->blk_sub.retop;
b1e25d05
DM
2884 PL_comppad = cx->blk_sub.prevcomppad;
2885 PL_curpad = LIKELY(PL_comppad) ? AvARRAY(PL_comppad) : NULL;
8ae997c5
DM
2886
2887 /* XS subs don't have a CXt_SUB, so pop it;
ed8ff0f3
DM
2888 * this is a cx_popblock(), less all the stuff we already did
2889 * for cx_topblock() earlier */
8ae997c5 2890 PL_curcop = cx->blk_oldcop;
5da525e9 2891 CX_POP(cx);
8ae997c5 2892
b37c2d43
AL
2893 /* Push a mark for the start of arglist */
2894 PUSHMARK(mark);
2895 PUTBACK;
2896 (void)(*CvXSUB(cv))(aTHX_ cv);
a57c6685 2897 LEAVE;
51eb35b5 2898 goto _return;
a0d0e21e
LW
2899 }
2900 else {
b70d5558 2901 PADLIST * const padlist = CvPADLIST(cv);
39de75fd 2902
80774f05
DM
2903 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2904
a73d8813 2905 /* partial unrolled cx_pushsub(): */
39de75fd 2906
a0d0e21e 2907 cx->blk_sub.cv = cv;
1a5b3db4 2908 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 2909
a0d0e21e 2910 CvDEPTH(cv)++;
2c50b7ed
DM
2911 SvREFCNT_inc_simple_void_NN(cv);
2912 if (CvDEPTH(cv) > 1) {
2b9dff67 2913 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
44a8e56a 2914 sub_crush_depth(cv);
26019298 2915 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2916 }
426a09cd 2917 PL_curcop = cx->blk_oldcop;
fd617465 2918 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
bafb2adc 2919 if (CxHASARGS(cx))
6d4ff0d2 2920 {
f72bdec3
DM
2921 /* second half of donating @_ from the old sub to the
2922 * new sub: abandon the original pad[0] AV in the
2923 * new sub, and replace it with the donated @_.
2924 * pad[0] takes ownership of the extra refcount
2925 * we gave arg earlier */
bfa371b6
FC
2926 if (arg) {
2927 SvREFCNT_dec(PAD_SVl(0));
fed4514a 2928 PAD_SVl(0) = (SV *)arg;
13122036 2929 SvREFCNT_inc_simple_void_NN(arg);
bfa371b6 2930 }
049bd5ff
FC
2931
2932 /* GvAV(PL_defgv) might have been modified on scope
f72bdec3 2933 exit, so point it at arg again. */
049bd5ff
FC
2934 if (arg != GvAV(PL_defgv)) {
2935 AV * const av = GvAV(PL_defgv);
2936 GvAV(PL_defgv) = (AV *)SvREFCNT_inc_simple(arg);
2937 SvREFCNT_dec(av);
a0d0e21e
LW
2938 }
2939 }
13122036 2940
491527d0 2941 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
005a8a35 2942 Perl_get_db_sub(aTHX_ NULL, cv);
b37c2d43 2943 if (PERLDB_GOTO) {
b96d8cd9 2944 CV * const gotocv = get_cvs("DB::goto", 0);
b37c2d43
AL
2945 if (gotocv) {
2946 PUSHMARK( PL_stack_sp );
ad64d0ec 2947 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
b37c2d43
AL
2948 PL_stack_sp--;
2949 }
491527d0 2950 }
1ce6579f 2951 }
51eb35b5
DD
2952 retop = CvSTART(cv);
2953 goto putback_return;
a0d0e21e
LW
2954 }
2955 }
1614b0e3 2956 else {
7d1d69cb 2957 /* goto EXPR */
55b37f1c 2958 label = SvPV_nomg_const(sv, label_len);
5db1eb8d 2959 label_flags = SvUTF8(sv);
1614b0e3 2960 }
a0d0e21e 2961 }
2fc690dc 2962 else if (!(PL_op->op_flags & OPf_SPECIAL)) {
7d1d69cb 2963 /* goto LABEL or dump LABEL */
5db1eb8d
BF
2964 label = cPVOP->op_pv;
2965 label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
2966 label_len = strlen(label);
2967 }
0157ef98 2968 if (!(do_dump || label_len)) DIE(aTHX_ "%s", must_have_label);
a0d0e21e 2969
f410a211
NC
2970 PERL_ASYNC_CHECK();
2971
3532f34a 2972 if (label_len) {
cbbf8932 2973 OP *gotoprobe = NULL;
3b2447bc 2974 bool leaving_eval = FALSE;
33d34e4c 2975 bool in_block = FALSE;
3c157b3c 2976 bool pseudo_block = FALSE;
cbbf8932 2977 PERL_CONTEXT *last_eval_cx = NULL;
a0d0e21e
LW
2978
2979 /* find label */
2980
d4c19fe8 2981 PL_lastgotoprobe = NULL;
a0d0e21e
LW
2982 *enterops = 0;
2983 for (ix = cxstack_ix; ix >= 0; ix--) {
2984 cx = &cxstack[ix];
6b35e009 2985 switch (CxTYPE(cx)) {
a0d0e21e 2986 case CXt_EVAL:
3b2447bc 2987 leaving_eval = TRUE;
971ecbe6 2988 if (!CxTRYBLOCK(cx)) {
a4f3a277
RH
2989 gotoprobe = (last_eval_cx ?
2990 last_eval_cx->blk_eval.old_eval_root :
2991 PL_eval_root);
2992 last_eval_cx = cx;
9c5794fe
RH
2993 break;
2994 }
2995 /* else fall through */
93661e56
DM
2996 case CXt_LOOP_PLAIN:
2997 case CXt_LOOP_LAZYIV:
2998 case CXt_LOOP_LAZYSV:
2999 case CXt_LOOP_LIST:
3000 case CXt_LOOP_ARY:
bb5aedc1
VP
3001 case CXt_GIVEN:
3002 case CXt_WHEN:
e6dae479 3003 gotoprobe = OpSIBLING(cx->blk_oldcop);
a0d0e21e
LW
3004 break;
3005 case CXt_SUBST:
3006 continue;
3007 case CXt_BLOCK:
33d34e4c 3008 if (ix) {
e6dae479 3009 gotoprobe = OpSIBLING(cx->blk_oldcop);
33d34e4c
AE
3010 in_block = TRUE;
3011 } else
3280af22 3012 gotoprobe = PL_main_root;
a0d0e21e 3013 break;
b3933176 3014 case CXt_SUB:
3c157b3c
Z
3015 gotoprobe = CvROOT(cx->blk_sub.cv);
3016 pseudo_block = cBOOL(CxMULTICALL(cx));
3017 break;
7766f137 3018 case CXt_FORMAT:
0a753a76 3019 case CXt_NULL:
a651a37d 3020 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
a0d0e21e
LW
3021 default:
3022 if (ix)
5637ef5b
NC
3023 DIE(aTHX_ "panic: goto, type=%u, ix=%ld",
3024 CxTYPE(cx), (long) ix);
3280af22 3025 gotoprobe = PL_main_root;
a0d0e21e
LW
3026 break;
3027 }
2b597662 3028 if (gotoprobe) {
29e61fd9
DM
3029 OP *sibl1, *sibl2;
3030
5db1eb8d 3031 retop = dofindlabel(gotoprobe, label, label_len, label_flags,
2b597662
GS
3032 enterops, enterops + GOTO_DEPTH);
3033 if (retop)
3034 break;
e6dae479 3035 if ( (sibl1 = OpSIBLING(gotoprobe)) &&
29e61fd9 3036 sibl1->op_type == OP_UNSTACK &&
e6dae479 3037 (sibl2 = OpSIBLING(sibl1)))
29e61fd9
DM
3038 {
3039 retop = dofindlabel(sibl2,
5db1eb8d
BF
3040 label, label_len, label_flags, enterops,
3041 enterops + GOTO_DEPTH);
eae48c89
Z
3042 if (retop)
3043 break;
3044 }
2b597662 3045 }
3c157b3c
Z
3046 if (pseudo_block)
3047 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
3280af22 3048 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
3049 }
3050 if (!retop)
147e3846 3051 DIE(aTHX_ "Can't find label %" UTF8f,
b17a0679 3052 UTF8fARG(label_flags, label_len, label));
a0d0e21e 3053
3b2447bc
RH
3054 /* if we're leaving an eval, check before we pop any frames
3055 that we're not going to punt, otherwise the error
3056 won't be caught */
3057
3058 if (leaving_eval && *enterops && enterops[1]) {
3059 I32 i;
3060 for (i = 1; enterops[i]; i++)
3061 if (enterops[i]->op_type == OP_ENTERITER)
3062 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3063 }
3064
b500e03b
GG
3065 if (*enterops && enterops[1]) {
3066 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3067 if (enterops[i])
dc6e8de0 3068 deprecate("\"goto\" to jump into a construct");
b500e03b
GG
3069 }
3070
a0d0e21e
LW
3071 /* pop unwanted frames */
3072
3073 if (ix < cxstack_ix) {
a0d0e21e 3074 if (ix < 0)
5edb7975 3075 DIE(aTHX_ "panic: docatch: illegal ix=%ld", (long)ix);
a0d0e21e 3076 dounwind(ix);
7e637ba4 3077 cx = CX_CUR();
ed8ff0f3 3078 cx_topblock(cx);
a0d0e21e
LW
3079 }
3080
3081 /* push wanted frames */
3082
748a9306 3083 if (*enterops && enterops[1]) {
0bd48802 3084 OP * const oldop = PL_op;
33d34e4c
AE
3085 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3086 for (; enterops[ix]; ix++) {
533c011a 3087 PL_op = enterops[ix];
84902520
TB
3088 /* Eventually we may want to stack the needed arguments
3089 * for each op. For now, we punt on the hard ones. */
533c011a 3090 if (PL_op->op_type == OP_ENTERITER)
894356b3 3091 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
16c91539 3092 PL_op->op_ppaddr(aTHX);
a0d0e21e 3093 }
533c011a 3094 PL_op = oldop;
a0d0e21e
LW
3095 }
3096 }
3097
2631bbca 3098 if (do_dump) {
a5f75d66 3099#ifdef VMS
6b88bc9c 3100 if (!retop) retop = PL_main_start;
a5f75d66 3101#endif
3280af22
NIS
3102 PL_restartop = retop;
3103 PL_do_undump = TRUE;
a0d0e21e
LW
3104
3105 my_unexec();
3106
3280af22
NIS
3107 PL_restartop = 0; /* hmm, must be GNU unexec().. */
3108 PL_do_undump = FALSE;
a0d0e21e
LW
3109 }
3110
51eb35b5
DD
3111 putback_return:
3112 PL_stack_sp = sp;
3113 _return:
47c9d59f 3114 PERL_ASYNC_CHECK();
51eb35b5 3115 return retop;
a0d0e21e
LW
3116}
3117
3118PP(pp_exit)
3119{
39644a26 3120 dSP;
a0d0e21e
LW
3121 I32 anum;
3122
3123 if (MAXARG < 1)
3124 anum = 0;
9d3c658e
FC
3125 else if (!TOPs) {
3126 anum = 0; (void)POPs;
3127 }
ff0cee69 3128 else {
a0d0e21e 3129 anum = SvIVx(POPs);
d98f61e7 3130#ifdef VMS
5450b4d8
FC
3131 if (anum == 1
3132 && SvTRUE(cop_hints_fetch_pvs(PL_curcop, "vmsish_exit", 0)))
ff0cee69 3133 anum = 0;
97124ef6
FC
3134 VMSISH_HUSHED =
3135 VMSISH_HUSHED || (PL_curcop->op_private & OPpHUSH_VMSISH);
ff0cee69
PP
3136#endif
3137 }
cc3604b1 3138 PL_exit_flags |= PERL_EXIT_EXPECTED;
a0d0e21e 3139 my_exit(anum);
3280af22 3140 PUSHs(&PL_sv_undef);
a0d0e21e
LW
3141 RETURN;
3142}
3143
a0d0e21e
LW
3144/* Eval. */
3145
0824fdcb 3146STATIC void
cea2e8a9 3147S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 3148{
504618e9 3149 const char *s = SvPVX_const(sv);
890ce7af 3150 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 3151 I32 line = 1;
a0d0e21e 3152
7918f24d
NC
3153 PERL_ARGS_ASSERT_SAVE_LINES;
3154
a0d0e21e 3155 while (s && s < send) {
f54cb97a 3156 const char *t;
b9f83d2f 3157 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 3158
1d963ff3 3159 t = (const char *)memchr(s, '\n', send - s);
a0d0e21e
LW
3160 if (t)
3161 t++;
3162 else
3163 t = send;
3164
3165 sv_setpvn(tmpstr, s, t - s);
3166 av_store(array, line++, tmpstr);
3167 s = t;
3168 }
3169}
3170
22f16304
RU
3171/*
3172=for apidoc docatch
3173
3174Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3175
31760 is used as continue inside eval,
3177
31783 is used for a die caught by an inner eval - continue inner loop
3179
75af9d73 3180See F<cop.h>: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
22f16304
RU
3181establish a local jmpenv to handle exception traps.
3182
3183=cut
3184*/
0824fdcb 3185STATIC OP *
d7e3f70f 3186S_docatch(pTHX_ Perl_ppaddr_t firstpp)
1e422769 3187{
6224f72b 3188 int ret;
06b5626a 3189 OP * const oldop = PL_op;
db36c5a1 3190 dJMPENV;
1e422769 3191
54310121 3192 assert(CATCH_GET == TRUE);
8bffa5f8 3193
14dd3ad8 3194 JMPENV_PUSH(ret);
6224f72b 3195 switch (ret) {
312caa8e 3196 case 0:
d7e3f70f 3197 PL_op = firstpp(aTHX);
14dd3ad8 3198 redo_body:
85aaa934 3199 CALLRUNOPS(aTHX);
312caa8e
CS
3200 break;
3201 case 3:
8bffa5f8 3202 /* die caught by an inner eval - continue inner loop */
febb3a6d
Z
3203 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3204 PL_restartjmpenv = NULL;
312caa8e
CS
3205 PL_op = PL_restartop;
3206 PL_restartop = 0;
3207 goto redo_body;
3208 }
924ba076 3209 /* FALLTHROUGH */
312caa8e 3210 default:
14dd3ad8 3211 JMPENV_POP;
533c011a 3212 PL_op = oldop;
6224f72b 3213 JMPENV_JUMP(ret);
e5964223 3214 NOT_REACHED; /* NOTREACHED */
1e422769 3215 }
14dd3ad8 3216 JMPENV_POP;
533c011a 3217 PL_op = oldop;
5f66b61c 3218 return NULL;
1e422769
PP
3219}
3220
a3985cdc
DM
3221
3222/*
3223=for apidoc find_runcv
3224
3225Locate the CV corresponding to the currently executing sub or eval.
796b6530
KW
3226If C<db_seqp> is non_null, skip CVs that are in the DB package and populate
3227C<*db_seqp> with the cop sequence number at the point that the DB:: code was
72d33970
FC
3228entered. (This allows debuggers to eval in the scope of the breakpoint
3229rather than in the scope of the debugger itself.)
a3985cdc
DM
3230
3231=cut
3232*/
3233
3234CV*
d819b83a 3235Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 3236{
db4cf31d 3237 return Perl_find_runcv_where(aTHX_ 0, 0, db_seqp);
70794f7b
FC
3238}
3239
3240/* If this becomes part of the API, it might need a better name. */
3241CV *
db4cf31d 3242Perl_find_runcv_where(pTHX_ U8 cond, IV arg, U32 *db_seqp)
70794f7b 3243{
a3985cdc 3244 PERL_SI *si;
b4b0692a 3245 int level = 0;
a3985cdc 3246
d819b83a 3247 if (db_seqp)
c3923c33
DM
3248 *db_seqp =
3249 PL_curcop == &PL_compiling
3250 ? PL_cop_seqmax
3251 : PL_curcop->cop_seq;
3252
a3985cdc 3253 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 3254 I32 ix;
a3985cdc 3255 for (ix = si->si_cxix; ix >= 0; ix--) {
06b5626a 3256 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
70794f7b 3257 CV *cv = NULL;
d819b83a 3258 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
70794f7b 3259 cv = cx->blk_sub.cv;
d819b83a
DM
3260 /* skip DB:: code */
3261 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3262 *db_seqp = cx->blk_oldcop->cop_seq;
3263 continue;
3264 }
a453e28a
DM
3265 if (cx->cx_type & CXp_SUB_RE)
3266 continue;
d819b83a 3267 }
a3985cdc 3268 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
70794f7b
FC
3269 cv = cx->blk_eval.cv;
3270 if (cv) {
3271 switch (cond) {
db4cf31d
FC
3272 case FIND_RUNCV_padid_eq:
3273 if (!CvPADLIST(cv)
b4db5868 3274 || CvPADLIST(cv)->xpadl_id != (U32)arg)
8771da69 3275 continue;
b4b0692a
FC
3276 return cv;
3277 case FIND_RUNCV_level_eq:
db4cf31d 3278 if (level++ != arg) continue;
70794f7b
FC
3279 /* GERONIMO! */
3280 default:
3281 return cv;
3282 }
3283 }
a3985cdc
DM
3284 }
3285 }
db4cf31d 3286 return cond == FIND_RUNCV_padid_eq ? NULL : PL_main_cv;
a3985cdc
DM
3287}
3288
3289
27e90453
DM
3290/* Run yyparse() in a setjmp wrapper. Returns:
3291 * 0: yyparse() successful
3292 * 1: yyparse() failed
3293 * 3: yyparse() died
3294 */
3295STATIC int
28ac2b49 3296S_try_yyparse(pTHX_ int gramtype)
27e90453
DM
3297{
3298 int ret;
3299 dJMPENV;
3300
4ebe6e95 3301 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
27e90453
DM
3302 JMPENV_PUSH(ret);
3303 switch (ret) {
3304 case 0:
28ac2b49 3305 ret = yyparse(gramtype) ? 1 : 0;
27e90453
DM
3306 break;
3307 case 3:
3308 break;
3309 default:
3310 JMPENV_POP;
3311 JMPENV_JUMP(ret);
e5964223 3312 NOT_REACHED; /* NOTREACHED */
27e90453
DM
3313 }
3314 JMPENV_POP;
3315 return ret;
3316}
3317
3318
104a8185
DM
3319/* Compile a require/do or an eval ''.
3320 *
a3985cdc 3321 * outside is the lexically enclosing CV (if any) that invoked us.
104a8185
DM
3322 * seq is the current COP scope value.
3323 * hh is the saved hints hash, if any.
3324 *
410be5db 3325 * Returns a bool indicating whether the compile was successful; if so,
104a8185
DM
3326 * PL_eval_start contains the first op of the compiled code; otherwise,
3327 * pushes undef.
3328 *
3329 * This function is called from two places: pp_require and pp_entereval.
3330 * These can be distinguished by whether PL_op is entereval.
7d116edc
FC
3331 */
3332
410be5db 3333STATIC bool
1c23e2bd 3334S_doeval_compile(pTHX_ U8 gimme, CV* outside, U32 seq, HV *hh)
a0d0e21e 3335{
20b7effb 3336 dSP;
46c461b5 3337 OP * const saveop = PL_op;
104a8185 3338 bool clear_hints = saveop->op_type != OP_ENTEREVAL;
f45b078d 3339 COP * const oldcurcop = PL_curcop;
26c9400e 3340 bool in_require = (saveop->op_type == OP_REQUIRE);
27e90453 3341 int yystatus;
676a678a 3342 CV *evalcv;
a0d0e21e 3343
27e90453 3344 PL_in_eval = (in_require
6dc8a9e4 3345 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
a1941760
DM
3346 : (EVAL_INEVAL |
3347 ((PL_op->op_private & OPpEVAL_RE_REPARSING)
3348 ? EVAL_RE_REPARSING : 0)));
a0d0e21e 3349
1ce6579f
PP
3350 PUSHMARK(SP);
3351
676a678a
Z
3352 evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3353 CvEVAL_on(evalcv);
4ebe6e95
DM
3354 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
3355 CX_CUR()->blk_eval.cv = evalcv;
3356 CX_CUR()->blk_gimme = gimme;
2090ab20 3357
676a678a
Z
3358 CvOUTSIDE_SEQ(evalcv) = seq;
3359 CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 3360
dd2155a4 3361 /* set up a scratch pad */
a0d0e21e 3362
eacbb379 3363 CvPADLIST_set(evalcv, pad_new(padnew_SAVE));
cecbe010 3364 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2c05e328 3365
07055b4c 3366
b5bbe64a 3367 SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */
748a9306 3368
a0d0e21e
LW
3369 /* make sure we compile in the right package */
3370
ed094faf 3371 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
03d9f026 3372 SAVEGENERICSV(PL_curstash);
cb1ad50e
FC
3373 PL_curstash = (HV *)CopSTASH(PL_curcop);
3374 if (SvTYPE(PL_curstash) != SVt_PVHV) PL_curstash = NULL;
04680144
FC
3375 else {
3376 SvREFCNT_inc_simple_void(PL_curstash);
3377 save_item(PL_curstname);
3378 sv_sethek(PL_curstname, HvNAME_HEK(PL_curstash));