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