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