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