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