This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump Data::Dumper version
[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 55 if (PL_op->op_private & OPpOFFBYONE) {
1604cfb0 56 if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF;
93f0bc49
FC
57 }
58 else {
5b6f7443 59 cxix = dopopto_cursub();
93f0bc49 60 if (cxix < 0)
1604cfb0 61 RETPUSHUNDEF;
93f0bc49
FC
62 cx = &cxstack[cxix];
63 }
a0d0e21e 64
93f0bc49 65 switch (cx->blk_gimme) {
54310121 66 case G_ARRAY:
1604cfb0 67 RETPUSHYES;
54310121 68 case G_SCALAR:
1604cfb0 69 RETPUSHNO;
54310121 70 default:
1604cfb0 71 RETPUSHUNDEF;
54310121 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 92 if (PL_op->op_flags & OPf_STACKED) {
1604cfb0
MS
93 dMARK;
94 nargs = SP - MARK;
95 args = ++MARK;
df787a7b
DM
96 }
97 else {
1604cfb0
MS
98 nargs = 1;
99 args = SP;
df787a7b
DM
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)) {
1604cfb0
MS
105 SP = args-1;
106 RETURN;
131b3ad0 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 114 new_re = (eng->op_comp
1604cfb0
MS
115 ? eng->op_comp
116 : &Perl_re_op_compile
117 )(aTHX_ args, nargs, pm->op_code_list, eng, re,
118 &is_bare_re,
dbc200c5 119 (pm->op_pmflags & RXf_PMf_FLAGCOPYMASK),
1604cfb0
MS
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)
1604cfb0
MS
124 ReANY(new_re)->qr_anoncv
125 = (CV*) SvREFCNT_inc(PAD_SV(PL_op->op_targ));
9f141731
DM
126
127 if (is_bare_re) {
1604cfb0
MS
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;
137 const bool was_tainted = TAINT_get;
138 if (pm->op_flags & OPf_STACKED)
139 lhs = args[-1];
140 else if (pm->op_targ)
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
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
1604cfb0
MS
151 }
152 tmp = reg_temp_copy(NULL, new_re);
153 ReREFCNT_dec(new_re);
154 new_re = tmp;
df787a7b 155 }
dbc200c5 156
9f141731 157 if (re != new_re) {
1604cfb0
MS
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) {
1604cfb0 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) {
1604cfb0 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) {
1604cfb0
MS
207 if(old)
208 ReREFCNT_dec(old);
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++) {
1604cfb0
MS
215 const SSize_t saviters = cx->sb_iters;
216 if (cx->sb_iters > cx->sb_maxiters)
217 DIE(aTHX_ "Substitution loop");
a0d0e21e 218
1604cfb0 219 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
447ee134 220
1604cfb0
MS
221 /* See "how taint works" above pp_subst() */
222 sv_catsv_nomg(dstr, POPs);
223 if (UNLIKELY(TAINT_get))
224 cx->sb_rxtainted |= SUBST_TAINT_REPL;
225 if (CxONCE(cx) || s < orig ||
03c83e26 226 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
1604cfb0 227 (s == m), cx->sb_targ, NULL,
d5e7783a 228 (REXEC_IGNOREPOS|REXEC_NOT_FIRST|REXEC_FAIL_ON_UNDERFLOW)))
1604cfb0
MS
229 {
230 SV *targ = cx->sb_targ;
231
232 assert(cx->sb_strend >= s);
233 if(cx->sb_strend > s) {
234 if (DO_UTF8(dstr) && !SvUTF8(targ))
235 sv_catpvn_nomg_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
236 else
237 sv_catpvn_nomg(dstr, s, cx->sb_strend - s);
238 }
239 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
240 cx->sb_rxtainted |= SUBST_TAINT_PAT;
241
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 {
249 SV_CHECK_THINKFIRST_COW_DROP(targ);
250 if (isGV(targ)) Perl_croak_no_modify();
251 SvPV_free(targ);
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
259 PL_tainted = 0;
260 mPUSHi(saviters - 1);
261
262 (void)SvPOK_only_UTF8(targ);
263 }
264
265 /* update the taint state of various variables in
266 * preparation for final exit.
267 * See "how taint works" above pp_subst() */
268 if (TAINTING_get) {
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 */
280 TAINT_set(
284167a5 281 cBOOL(cx->sb_rxtainted &
1604cfb0 282 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
284167a5 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
1604cfb0
MS
302 SvTAINT(TARG);
303 }
304 /* PL_tainted must be correctly set for this mg_set */
305 SvSETMAGIC(TARG);
306 TAINT_NOT;
80d88db0 307
1604cfb0
MS
308 CX_LEAVE_SCOPE(cx);
309 CX_POPSUBST(cx);
5da525e9 310 CX_POP(cx);
80d88db0 311
1604cfb0
MS
312 PERL_ASYNC_CHECK();
313 RETURNOP(pm->op_next);
314 NOT_REACHED; /* NOTREACHED */
315 }
316 cx->sb_iters = saviters;
a0d0e21e 317 }
07bc277f 318 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
1604cfb0
MS
319 m = s;
320 s = orig;
6502e081 321 assert(!RX_SUBOFFSET(rx));
1604cfb0
MS
322 cx->sb_orig = orig = RX_SUBBEG(rx);
323 s = orig + (m - s);
324 cx->sb_strend = s + (cx->sb_strend - m);
a0d0e21e 325 }
07bc277f 326 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
db79b45b 327 if (m > s) {
1604cfb0
MS
328 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
329 sv_catpvn_nomg_utf8_upgrade(dstr, s, m - s, nsv);
330 else
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. */
1604cfb0
MS
335 SV * const sv
336 = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ;
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
1604cfb0
MS
345 if (!(mg = mg_find_mglob(sv))) {
346 mg = sv_magicext_mglob(sv);
347 }
348 MgBYTEPOS_set(mg, sv, SvPVX(sv), m - orig);
084916e3 349 }
988e6e7e 350 if (old != rx)
1604cfb0 351 (void)ReREFCNT_inc(rx);
a3815e44 352 /* update the taint state of 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) {
1604cfb0
MS
356 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
357 cx->sb_rxtainted |= SUBST_TAINT_PAT;
20be6587 358
1604cfb0
MS
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 */
20be6587 364
1604cfb0
MS
365 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
366 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
367 SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT)
368 ? cx->sb_dstr : cx->sb_targ);
369 TAINT_NOT;
20be6587 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
1604cfb0 387 i = 7 + (RX_NPARENS(rx)+1) * 2;
ed252734 388#else
1604cfb0 389 i = 6 + (RX_NPARENS(rx)+1) * 2;
ed252734 390#endif
1604cfb0
MS
391 if (!p)
392 Newx(p, i, UV);
393 else
394 Renew(p, i, UV);
395 *rsp = (void*)p;
c90c0ff4
PP
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 412 for (i = 0; i <= RX_NPARENS(rx); ++i) {
1604cfb0
MS
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 433 if (RX_SAVED_COPY(rx))
1604cfb0 434 SvREFCNT_dec (RX_SAVED_COPY(rx));
bdd9a1b1 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 443 for (i = 0; i <= RX_NPARENS(rx); ++i) {
1604cfb0
MS
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) {
1604cfb0 458 void *tmp = INT2PTR(char*,*p);
6c31ff74 459#ifdef PERL_POISON
db2c6cb3 460#ifdef PERL_ANY_COW
1604cfb0 461 U32 i = 9 + p[1] * 2;
94010e71 462#else
1604cfb0 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
1604cfb0
MS
474 Safefree(tmp);
475 Safefree(p);
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))
1604cfb0 524 SvTAINTED_on(PL_formtarget);
1bd51a4c 525 if (DO_UTF8(PL_formtarget))
1604cfb0 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 (;;) {
1604cfb0
MS
536 DEBUG_f( {
537 const char *name = "???";
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;
557 case FF_0DECIMAL: name = "0DECIMAL"; break;
558 case FF_LINESNGL: name = "LINESNGL"; break;
559 }
560 if (arg >= 0)
561 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
562 else
563 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
564 } );
565 switch (*fpc++) {
566 case FF_LINEMARK: /* start (or end) of a line */
567 linemark = t - SvPVX(PL_formtarget);
568 lines++;
569 gotsome = FALSE;
570 break;
571
572 case FF_LITERAL: /* append <arg> literal chars */
573 to_copy = *fpc++;
574 source = (U8 *)f;
575 f += to_copy;
576 trans = '~';
577 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
578 goto append;
579
580 case FF_SKIP: /* skip <arg> chars in format */
581 f += *fpc++;
582 break;
583
584 case FF_FETCH: /* get next item and set field size to <arg> */
585 arg = *fpc++;
586 f += arg;
587 fieldsize = arg;
588
589 if (MARK < SP)
590 sv = *++MARK;
591 else {
592 sv = &PL_sv_no;
593 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
594 }
595 if (SvTAINTED(sv))
596 SvTAINTED_on(PL_formtarget);
597 break;
598
599 case FF_CHECKNL: /* find max len of item (up to \n) that fits field */
600 {
601 const char *s = item = SvPV_const(sv, len);
602 const char *send = s + len;
9b4bdfd4
DM
603
604 itemsize = 0;
1604cfb0 605 item_is_utf8 = DO_UTF8(sv);
9b4bdfd4
DM
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;
1604cfb0
MS
622 break;
623 }
a0d0e21e 624
1604cfb0
MS
625 case FF_CHECKCHOP: /* like CHECKNL, but up to highest split point */
626 {
627 const char *s = item = SvPV_const(sv, len);
628 const char *send = s + len;
9b4bdfd4
DM
629 I32 size = 0;
630
631 chophere = NULL;
1604cfb0 632 item_is_utf8 = DO_UTF8(sv);
9b4bdfd4
DM
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
1604cfb0
MS
681 break;
682 }
683
684 case FF_SPACE: /* append padding space (diff of field, item size) */
685 arg = fieldsize - itemsize;
686 if (arg) {
687 fieldsize -= arg;
688 while (arg-- > 0)
689 *t++ = ' ';
690 }
691 break;
692
693 case FF_HALFSPACE: /* like FF_SPACE, but only append half as many */
694 arg = fieldsize - itemsize;
695 if (arg) {
696 arg /= 2;
697 fieldsize -= arg;
698 while (arg-- > 0)
699 *t++ = ' ';
700 }
701 break;
702
703 case FF_ITEM: /* append a text item, while blanking ctrl chars */
704 to_copy = itembytes;
705 source = (U8 *)item;
706 trans = 1;
707 goto append;
708
709 case FF_CHOP: /* (for ^*) chop the current item */
710 if (sv != &PL_sv_no) {
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 }
1604cfb0
MS
729 if (chopspace) {
730 while (isSPACE(*s))
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));
1604cfb0
MS
739 SvSETMAGIC(sv);
740 break;
741 }
2165bd23 742 /* FALLTHROUGH */
a0d0e21e 743
1604cfb0
MS
744 case FF_LINESNGL: /* process ^* */
745 chopspace = 0;
c67159e1 746 /* FALLTHROUGH */
4a73dc0b 747
1604cfb0
MS
748 case FF_LINEGLOB: /* process @* */
749 {
750 const bool oneline = fpc[-1] == FF_LINESNGL;
751 const char *s = item = SvPV_const(sv, len);
752 const char *const send = s + len;
753
754 item_is_utf8 = DO_UTF8(sv);
755 chophere = s + len;
756 if (!len)
757 break;
758 trans = 0;
759 gotsome = TRUE;
760 source = (U8 *) s;
761 to_copy = len;
762 while (s < send) {
763 if (*s++ == '\n') {
764 if (oneline) {
765 to_copy = s - item - 1;
766 chophere = s;
767 break;
768 } else {
769 if (s == send) {
770 to_copy--;
771 } else
772 lines++;
773 }
774 }
775 }
776 }
777
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 */
782 {
783 U8 *tmp = NULL;
784 STRLEN grow = 0;
785
786 SvCUR_set(PL_formtarget,
787 t - SvPVX_const(PL_formtarget));
788
789 if (targ_is_utf8 && !item_is_utf8) {
790 source = tmp = bytes_to_utf8(source, &to_copy);
732f08da 791 grow = to_copy;
1604cfb0
MS
792 } else {
793 if (item_is_utf8 && !targ_is_utf8) {
794 U8 *s;
795 /* Upgrade targ to UTF8, and then we reduce it to
796 a problem we have a simple solution for.
797 Don't need get magic. */
798 sv_utf8_upgrade_nomg(PL_formtarget);
799 targ_is_utf8 = TRUE;
800 /* re-calculate linemark */
801 s = (U8*)SvPVX(PL_formtarget);
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;
807 while (linemark--)
808 s += UTF8_SAFE_SKIP(s,
40c725d1 809 (U8 *) SvEND(PL_formtarget));
1604cfb0
MS
810 linemark = s - (U8*)SvPVX(PL_formtarget);
811 }
812 /* Easy. They agree. */
813 assert (item_is_utf8 == targ_is_utf8);
814 }
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);
824
825 Copy(source, t, to_copy, char);
826 if (trans) {
827 /* blank out ~ or control chars, depending on trans.
828 * works on bytes not chars, so relies on not
829 * matching utf8 continuation bytes */
830 U8 *s = (U8*)t;
831 U8 *send = s + to_copy;
832 while (s < send) {
833 const int ch = *s;
834 if (trans == '~' ? (ch == '~') : isCNTRL(ch))
835 *s = ' ';
836 s++;
837 }
838 }
839
840 t += to_copy;
841 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
842 if (tmp)
843 Safefree(tmp);
844 break;
845 }
846
847 case FF_0DECIMAL: /* like FF_DECIMAL but for 0### */
848 arg = *fpc++;
849 fmt = (const char *)
850 ((arg & FORM_NUM_POINT) ? "%#0*.*" NVff : "%0*.*" NVff);
851 goto ff_dec;
852
853 case FF_DECIMAL: /* do @##, ^##, where <arg>=(precision|flags) */
854 arg = *fpc++;
855 fmt = (const char *)
856 ((arg & FORM_NUM_POINT) ? "%#*.*" NVff : "%*.*" NVff);
857 ff_dec:
858 /* If the field is marked with ^ and the value is undefined,
859 blank it out. */
860 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
861 arg = fieldsize;
862 while (arg--)
863 *t++ = ' ';
864 break;
865 }
866 gotsome = TRUE;
867 value = SvNV(sv);
868 /* overflow evidence */
869 if (num_overflow(value, fieldsize, arg)) {
870 arg = fieldsize;
871 while (arg--)
872 *t++ = '#';
873 break;
874 }
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 {
a4eca1d4 884 int len;
065d0f13 885 if (!quadmath_format_valid(fmt))
a4eca1d4 886 Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", fmt);
065d0f13 887 len = quadmath_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value);
a4eca1d4 888 if (len == -1)
065d0f13 889 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", fmt);
a4eca1d4
JH
890 }
891#else
b587c0e8 892 /* we generate fmt ourselves so it is safe */
7347ee54 893 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
e8549682 894 len = my_snprintf(t, max, fmt, (int) fieldsize, (int) arg, value);
7347ee54 895 GCC_DIAG_RESTORE_STMT;
a4eca1d4
JH
896#endif
897 PERL_MY_SNPRINTF_POST_GUARD(len, max);
a2287a13 898 RESTORE_LC_NUMERIC();
1604cfb0
MS
899 }
900 t += fieldsize;
901 break;
902
903 case FF_NEWLINE: /* delete trailing spaces, then append \n */
904 f++;
905 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
906 t++;
907 *t++ = '\n';
908 break;
909
910 case FF_BLANK: /* for arg==0: do '~'; for arg>0 : do '~~' */
911 arg = *fpc++;
912 if (gotsome) {
913 if (arg) { /* repeat until fields exhausted? */
914 fpc--;
915 goto end;
916 }
917 }
918 else {
919 t = SvPVX(PL_formtarget) + linemark;
920 lines--;
921 }
922 break;
923
924 case FF_MORE: /* replace long end of string with '...' */
925 {
926 const char *s = chophere;
927 const char *send = item + len;
928 if (chopspace) {
929 while (isSPACE(*s) && (s < send))
930 s++;
931 }
932 if (s < send) {
933 char *s1;
934 arg = fieldsize - itemsize;
935 if (arg) {
936 fieldsize -= arg;
937 while (arg-- > 0)
938 *t++ = ' ';
939 }
940 s1 = t - 3;
941 if (strBEGINs(s1," ")) {
942 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
943 s1--;
944 }
945 *s1++ = '.';
946 *s1++ = '.';
947 *s1++ = '.';
948 }
949 break;
950 }
951
952 case FF_END: /* tidy up, then return */
953 end:
954 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
955 *t = '\0';
956 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
957 if (targ_is_utf8)
958 SvUTF8_on(PL_formtarget);
959 FmLINES(PL_formtarget) += lines;
960 SP = ORIGMARK;
961 if (fpc[-1] == FF_BLANK)
962 RETURNOP(cLISTOP->op_first);
963 else
964 RETPUSHYES;
965 }
a0d0e21e
LW
966 }
967}
968
10088f56 969/* also used for: pp_mapstart() */
a0d0e21e
LW
970PP(pp_grepstart)
971{
20b7effb 972 dSP;
a0d0e21e
LW
973 SV *src;
974
6cae08a8 975 if (PL_stack_base + TOPMARK == SP) {
1604cfb0
MS
976 (void)POPMARK;
977 if (GIMME_V == G_SCALAR)
978 XPUSHs(&PL_sv_zero);
979 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 980 }
6cae08a8 981 PL_stack_sp = PL_stack_base + TOPMARK + 1;
897d3989
NC
982 Perl_pp_pushmark(aTHX); /* push dst */
983 Perl_pp_pushmark(aTHX); /* push src */
d343c3ef 984 ENTER_with_name("grep"); /* enter outer scope */
a0d0e21e
LW
985
986 SAVETMPS;
ffd49c98 987 SAVE_DEFSV;
d343c3ef 988 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 989 SAVEVPTR(PL_curpm);
a0d0e21e 990
6cae08a8 991 src = PL_stack_base[TOPMARK];
60779a30 992 if (SvPADTMP(src)) {
1604cfb0
MS
993 src = PL_stack_base[TOPMARK] = sv_mortalcopy(src);
994 PL_tmps_floor++;
a0ed822e 995 }
a0d0e21e 996 SvTEMP_off(src);
ffd49c98 997 DEFSV_set(src);
a0d0e21e
LW
998
999 PUTBACK;
533c011a 1000 if (PL_op->op_type == OP_MAPSTART)
1604cfb0 1001 Perl_pp_pushmark(aTHX); /* push top */
533c011a 1002 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
1003}
1004
a0d0e21e
LW
1005PP(pp_mapwhile)
1006{
20b7effb 1007 dSP;
1c23e2bd 1008 const U8 gimme = GIMME_V;
6cae08a8 1009 I32 items = (SP - PL_stack_base) - TOPMARK; /* how many new items */
a0d0e21e
LW
1010 I32 count;
1011 I32 shift;
1012 SV** src;
ac27b0f5 1013 SV** dst;
a0d0e21e 1014
544f3153 1015 /* first, move source pointer to the next item in the source list */
3280af22 1016 ++PL_markstack_ptr[-1];
544f3153
GS
1017
1018 /* if there are new items, push them into the destination list */
4c90a460 1019 if (items && gimme != G_VOID) {
1604cfb0
MS
1020 /* might need to make room back there first */
1021 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1022 /* XXX this implementation is very pessimal because the stack
1023 * is repeatedly extended for every set of items. Is possible
1024 * to do this without any stack extension or copying at all
1025 * by maintaining a separate list over which the map iterates
1026 * (like foreach does). --gsar */
1027
1028 /* everything in the stack after the destination list moves
1029 * towards the end the stack by the amount of room needed */
1030 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1031
1032 /* items to shift up (accounting for the moved source pointer) */
1033 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1034
1035 /* This optimization is by Ben Tilly and it does
1036 * things differently from what Sarathy (gsar)
1037 * is describing. The downside of this optimization is
1038 * that leaves "holes" (uninitialized and hopefully unused areas)
1039 * to the Perl stack, but on the other hand this
1040 * shouldn't be a problem. If Sarathy's idea gets
1041 * implemented, this optimization should become
1042 * irrelevant. --jhi */
18ef8bea
BT
1043 if (shift < count)
1044 shift = count; /* Avoid shifting too often --Ben Tilly */
bfed75c6 1045
1604cfb0
MS
1046 EXTEND(SP,shift);
1047 src = SP;
1048 dst = (SP += shift);
1049 PL_markstack_ptr[-1] += shift;
1050 *PL_markstack_ptr += shift;
1051 while (count--)
1052 *dst-- = *src--;
1053 }
1054 /* copy the new items down to the destination list */
1055 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1056 if (gimme == G_ARRAY) {
1057 /* add returned items to the collection (making mortal copies
1058 * if necessary), then clear the current temps stack frame
1059 * *except* for those items. We do this splicing the items
1060 * into the start of the tmps frame (so some items may be on
1061 * the tmps stack twice), then moving PL_tmps_floor above
1062 * them, then freeing the frame. That way, the only tmps that
1063 * accumulate over iterations are the return values for map.
1064 * We have to do to this way so that everything gets correctly
1065 * freed if we die during the map.
1066 */
1067 I32 tmpsbase;
1068 I32 i = items;
1069 /* make space for the slice */
1070 EXTEND_MORTAL(items);
1071 tmpsbase = PL_tmps_floor + 1;
1072 Move(PL_tmps_stack + tmpsbase,
1073 PL_tmps_stack + tmpsbase + items,
1074 PL_tmps_ix - PL_tmps_floor,
1075 SV*);
1076 PL_tmps_ix += items;
1077
1078 while (i-- > 0) {
1079 SV *sv = POPs;
1080 if (!SvTEMP(sv))
1081 sv = sv_mortalcopy(sv);
1082 *dst-- = sv;
1083 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1084 }
1085 /* clear the stack frame except for the items */
1086 PL_tmps_floor += items;
1087 FREETMPS;
1088 /* FREETMPS may have cleared the TEMP flag on some of the items */
1089 i = items;
1090 while (i-- > 0)
1091 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1092 }
1093 else {
1094 /* scalar context: we don't care about which values map returns
1095 * (we use undef here). And so we certainly don't want to do mortal
1096 * copies of meaningless values. */
1097 while (items-- > 0) {
1098 (void)POPs;
1099 *dst-- = &PL_sv_undef;
1100 }
1101 FREETMPS;
1102 }
a0d0e21e 1103 }
b2a2a901 1104 else {
1604cfb0 1105 FREETMPS;
b2a2a901 1106 }
d343c3ef 1107 LEAVE_with_name("grep_item"); /* exit inner scope */
a0d0e21e
LW
1108
1109 /* All done yet? */
6cae08a8 1110 if (PL_markstack_ptr[-1] > TOPMARK) {
a0d0e21e 1111
1604cfb0
MS
1112 (void)POPMARK; /* pop top */
1113 LEAVE_with_name("grep"); /* exit outer scope */
1114 (void)POPMARK; /* pop src */
1115 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1116 (void)POPMARK; /* pop dst */
1117 SP = PL_stack_base + POPMARK; /* pop original mark */
1118 if (gimme == G_SCALAR) {
1119 dTARGET;
1120 XPUSHi(items);
1121 }
1122 else if (gimme == G_ARRAY)
1123 SP += items;
1124 RETURN;
a0d0e21e
LW
1125 }
1126 else {
1604cfb0 1127 SV *src;
a0d0e21e 1128
1604cfb0
MS
1129 ENTER_with_name("grep_item"); /* enter inner scope */
1130 SAVEVPTR(PL_curpm);
a0d0e21e 1131
1604cfb0
MS
1132 /* set $_ to the new source item */
1133 src = PL_stack_base[PL_markstack_ptr[-1]];
1134 if (SvPADTMP(src)) {
60779a30
DM
1135 src = sv_mortalcopy(src);
1136 }
1604cfb0
MS
1137 SvTEMP_off(src);
1138 DEFSV_set(src);
a0d0e21e 1139
1604cfb0 1140 RETURNOP(cLOGOP->op_other);
a0d0e21e
LW
1141 }
1142}
1143
a0d0e21e
LW
1144/* Range stuff. */
1145
1146PP(pp_range)
1147{
f4c975aa 1148 dTARG;
82334630 1149 if (GIMME_V == G_ARRAY)
1604cfb0 1150 return NORMAL;
f4c975aa
DM
1151 GETTARGET;
1152 if (SvTRUE_NN(targ))
1604cfb0 1153 return cLOGOP->op_other;
538573f7 1154 else
1604cfb0 1155 return NORMAL;
a0d0e21e
LW
1156}
1157
1158PP(pp_flip)
1159{
39644a26 1160 dSP;
a0d0e21e 1161
82334630 1162 if (GIMME_V == G_ARRAY) {
1604cfb0 1163 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1164 }
1165 else {
1604cfb0
MS
1166 dTOPss;
1167 SV * const targ = PAD_SV(PL_op->op_targ);
1168 int flip = 0;
1169
1170 if (PL_op->op_private & OPpFLIP_LINENUM) {
1171 if (GvIO(PL_last_in_gv)) {
1172 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1173 }
1174 else {
1175 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1176 if (gv && GvSV(gv))
1177 flip = SvIV(sv) == SvIV(GvSV(gv));
1178 }
1179 } else {
1180 flip = SvTRUE_NN(sv);
1181 }
1182 if (flip) {
1183 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1184 if (PL_op->op_flags & OPf_SPECIAL) {
1185 sv_setiv(targ, 1);
1186 SETs(targ);
1187 RETURN;
1188 }
1189 else {
1190 sv_setiv(targ, 0);
1191 SP--;
1192 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1193 }
1194 }
9aa3b533 1195 SvPVCLEAR(TARG);
1604cfb0
MS
1196 SETs(targ);
1197 RETURN;
a0d0e21e
LW
1198 }
1199}
1200
8e9bbdb9 1201/* This code tries to decide if "$left .. $right" should use the
d1bc97fe
H
1202 magical string increment, or if the range is numeric. Initially,
1203 an exception was made for *any* string beginning with "0" (see
1204 [#18165], AMS 20021031), but now that is only applied when the
1205 string's length is also >1 - see the rules now documented in
1206 perlop [#133695] */
8e9bbdb9
RGS
1207
1208#define RANGE_IS_NUMERIC(left,right) ( \
1604cfb0
MS
1209 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1210 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1211 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
d1bc97fe
H
1212 looks_like_number(left)) && SvPOKp(left) \
1213 && !(*SvPVX_const(left) == '0' && SvCUR(left)>1 ) )) \
e0ab1c0e 1214 && (!SvOK(right) || looks_like_number(right))))
8e9bbdb9 1215
a0d0e21e
LW
1216PP(pp_flop)
1217{
20b7effb 1218 dSP;
a0d0e21e 1219
82334630 1220 if (GIMME_V == G_ARRAY) {
1604cfb0
MS
1221 dPOPPOPssrl;
1222
1223 SvGETMAGIC(left);
1224 SvGETMAGIC(right);
1225
1226 if (RANGE_IS_NUMERIC(left,right)) {
1227 IV i, j, n;
1228 if ((SvOK(left) && !SvIOK(left) && SvNV_nomg(left) < IV_MIN) ||
1229 (SvOK(right) && (SvIOK(right)
1230 ? SvIsUV(right) && SvUV(right) > IV_MAX
1231 : SvNV_nomg(right) > (NV) IV_MAX)))
1232 DIE(aTHX_ "Range iterator outside integer range");
1233 i = SvIV_nomg(left);
1234 j = SvIV_nomg(right);
1235 if (j >= i) {
b262c4c9
JH
1236 /* Dance carefully around signed max. */
1237 bool overflow = (i <= 0 && j > SSize_t_MAX + i - 1);
1238 if (!overflow) {
1239 n = j - i + 1;
1240 /* The wraparound of signed integers is undefined
1241 * behavior, but here we aim for count >=1, and
1242 * negative count is just wrong. */
a1e27170
TC
1243 if (n < 1
1244#if IVSIZE > Size_t_size
1245 || n > SSize_t_MAX
1246#endif
1247 )
b262c4c9
JH
1248 overflow = TRUE;
1249 }
1250 if (overflow)
1251 Perl_croak(aTHX_ "Out of memory during list extend");
1604cfb0
MS
1252 EXTEND_MORTAL(n);
1253 EXTEND(SP, n);
1254 }
1255 else
1256 n = 0;
1257 while (n--) {
1258 SV * const sv = sv_2mortal(newSViv(i));
1259 PUSHs(sv);
fc01cab4
DM
1260 if (n) /* avoid incrementing above IV_MAX */
1261 i++;
1604cfb0
MS
1262 }
1263 }
1264 else {
1265 STRLEN len, llen;
1266 const char * const lpv = SvPV_nomg_const(left, llen);
1267 const char * const tmps = SvPV_nomg_const(right, len);
1268
1269 SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP);
d6c970c7
AC
1270 if (DO_UTF8(right) && IN_UNI_8_BIT)
1271 len = sv_len_utf8_nomg(right);
1604cfb0
MS
1272 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1273 XPUSHs(sv);
1274 if (strEQ(SvPVX_const(sv),tmps))
1275 break;
1276 sv = sv_2mortal(newSVsv(sv));
1277 sv_inc(sv);
1278 }
1279 }
a0d0e21e
LW
1280 }
1281 else {
1604cfb0
MS
1282 dTOPss;
1283 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1284 int flop = 0;
1285 sv_inc(targ);
1286
1287 if (PL_op->op_private & OPpFLIP_LINENUM) {
1288 if (GvIO(PL_last_in_gv)) {
1289 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1290 }
1291 else {
1292 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1293 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1294 }
1295 }
1296 else {
1297 flop = SvTRUE_NN(sv);
1298 }
1299
1300 if (flop) {
1301 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1302 sv_catpvs(targ, "E0");
1303 }
1304 SETs(targ);
a0d0e21e
LW
1305 }
1306
1307 RETURN;
1308}
1309
1310/* Control. */
1311
27da23d5 1312static const char * const context_name[] = {
515afda2 1313 "pseudo-block",
7896dde7 1314 NULL, /* CXt_WHEN never actually needs "block" */
76753e7f 1315 NULL, /* CXt_BLOCK never actually needs "block" */
7896dde7 1316 NULL, /* CXt_GIVEN never actually needs "block" */
76753e7f 1317 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
76753e7f 1318 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
93661e56
DM
1319 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1320 NULL, /* CXt_LOOP_LIST never actually needs "loop" */
1321 NULL, /* CXt_LOOP_ARY never actually needs "loop" */
515afda2 1322 "subroutine",
76753e7f 1323 "format",
515afda2 1324 "eval",
515afda2 1325 "substitution",
515afda2
NC
1326};
1327
76e3520e 1328STATIC I32
5db1eb8d 1329S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags)
a0d0e21e 1330{
eb578fdb 1331 I32 i;
a0d0e21e 1332
7918f24d
NC
1333 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1334
a0d0e21e 1335 for (i = cxstack_ix; i >= 0; i--) {
1604cfb0
MS
1336 const PERL_CONTEXT * const cx = &cxstack[i];
1337 switch (CxTYPE(cx)) {
a1325b90
PLE
1338 case CXt_EVAL:
1339 if(CxTRY(cx))
1340 continue;
1341 /* FALLTHROUGH */
1604cfb0
MS
1342 case CXt_SUBST:
1343 case CXt_SUB:
1344 case CXt_FORMAT:
1604cfb0
MS
1345 case CXt_NULL:
1346 /* diag_listed_as: Exiting subroutine via %s */
1347 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1348 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1349 if (CxTYPE(cx) == CXt_NULL) /* sort BLOCK */
1350 return -1;
1351 break;
1352 case CXt_LOOP_PLAIN:
1353 case CXt_LOOP_LAZYIV:
1354 case CXt_LOOP_LAZYSV:
1355 case CXt_LOOP_LIST:
1356 case CXt_LOOP_ARY:
1357 {
5db1eb8d
BF
1358 STRLEN cx_label_len = 0;
1359 U32 cx_label_flags = 0;
1604cfb0
MS
1360 const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags);
1361 if (!cx_label || !(
5db1eb8d
BF
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))) )) {
1604cfb0
MS
1372 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1373 (long)i, cx_label));
1374 continue;
1375 }
1376 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1377 return i;
1378 }
1379 }
a0d0e21e
LW
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)
1604cfb0 1401 return G_VOID;
e50aee73 1402
a05700a8
DM
1403 gimme = (cxstack[cxix].blk_gimme & G_WANT);
1404 if (!gimme)
1604cfb0 1405 Perl_croak(aTHX_ "panic: bad gimme: %d\n", gimme);
a05700a8 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 1416 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1604cfb0 1417 return CxLVAL(cxstack + cxix);
78f9721b 1418 else
1604cfb0 1419 return 0;
78f9721b
SM
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))
1604cfb0 1430 return CxLVAL(cxstack + cxix);
777d9014 1431 else
1604cfb0 1432 return 0;
777d9014
FC
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--) {
1604cfb0
MS
1446 const PERL_CONTEXT * const cx = &cxstk[i];
1447 switch (CxTYPE(cx)) {
1448 default:
1449 continue;
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;
a1325b90
PLE
1457 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1458 return i;
1459
1604cfb0 1460 case CXt_EVAL:
a1325b90
PLE
1461 if (CxTRY(cx))
1462 continue;
1463 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1464 return i;
1465
1604cfb0
MS
1466 case CXt_FORMAT:
1467 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1468 return i;
1469 }
a0d0e21e
LW
1470 }
1471 return i;
1472}
1473
76e3520e 1474STATIC I32
cea2e8a9 1475S_dopoptoeval(pTHX_ I32 startingblock)
a0d0e21e
LW
1476{
1477 I32 i;
a0d0e21e 1478 for (i = startingblock; i >= 0; i--) {
1604cfb0
MS
1479 const PERL_CONTEXT *cx = &cxstack[i];
1480 switch (CxTYPE(cx)) {
1481 default:
1482 continue;
1483 case CXt_EVAL:
1484 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1485 return i;
1486 }
a0d0e21e
LW
1487 }
1488 return i;
1489}
1490
76e3520e 1491STATIC I32
cea2e8a9 1492S_dopoptoloop(pTHX_ I32 startingblock)
a0d0e21e
LW
1493{
1494 I32 i;
a0d0e21e 1495 for (i = startingblock; i >= 0; i--) {
1604cfb0
MS
1496 const PERL_CONTEXT * const cx = &cxstack[i];
1497 switch (CxTYPE(cx)) {
a1325b90
PLE
1498 case CXt_EVAL:
1499 if(CxTRY(cx))
1500 continue;
1501 /* FALLTHROUGH */
1604cfb0
MS
1502 case CXt_SUBST:
1503 case CXt_SUB:
1504 case CXt_FORMAT:
1604cfb0
MS
1505 case CXt_NULL:
1506 /* diag_listed_as: Exiting subroutine via %s */
1507 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1508 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1509 if ((CxTYPE(cx)) == CXt_NULL) /* sort BLOCK */
1510 return -1;
1511 break;
1512 case CXt_LOOP_PLAIN:
1513 case CXt_LOOP_LAZYIV:
1514 case CXt_LOOP_LAZYSV:
1515 case CXt_LOOP_LIST:
1516 case CXt_LOOP_ARY:
1517 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1518 return i;
1519 }
a0d0e21e
LW
1520 }
1521 return i;
1522}
1523
7896dde7
Z
1524/* find the next GIVEN or FOR (with implicit $_) loop context block */
1525
1526STATIC I32
1527S_dopoptogivenfor(pTHX_ I32 startingblock)
1528{
1529 I32 i;
1530 for (i = startingblock; i >= 0; i--) {
1604cfb0
MS
1531 const PERL_CONTEXT *cx = &cxstack[i];
1532 switch (CxTYPE(cx)) {
1533 default:
1534 continue;
1535 case CXt_GIVEN:
1536 DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found given at cx=%ld)\n", (long)i));
1537 return i;
1538 case CXt_LOOP_PLAIN:
7896dde7 1539 assert(!(cx->cx_type & CXp_FOR_DEF));
1604cfb0
MS
1540 break;
1541 case CXt_LOOP_LAZYIV:
1542 case CXt_LOOP_LAZYSV:
1543 case CXt_LOOP_LIST:
1544 case CXt_LOOP_ARY:
7896dde7 1545 if (cx->cx_type & CXp_FOR_DEF) {
1604cfb0
MS
1546 DEBUG_l( Perl_deb(aTHX_ "(dopoptogivenfor(): found foreach at cx=%ld)\n", (long)i));
1547 return i;
1548 }
1549 }
7896dde7
Z
1550 }
1551 return i;
1552}
1553
0d863452 1554STATIC I32
7896dde7 1555S_dopoptowhen(pTHX_ I32 startingblock)
0d863452
RH
1556{
1557 I32 i;
1558 for (i = startingblock; i >= 0; i--) {
1604cfb0
MS
1559 const PERL_CONTEXT *cx = &cxstack[i];
1560 switch (CxTYPE(cx)) {
1561 default:
1562 continue;
1563 case CXt_WHEN:
1564 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1565 return i;
1566 }
0d863452
RH
1567 }
1568 return i;
1569}
1570
abaf5d5a 1571/* dounwind(): pop all contexts above (but not including) cxix.
fc6e609e
DM
1572 * Note that it clears the savestack frame associated with each popped
1573 * context entry, but doesn't free any temps.
ed8ff0f3 1574 * It does a cx_popblock() of the last frame that it pops, and leaves
fc6e609e 1575 * cxstack_ix equal to cxix.
abaf5d5a
DM
1576 */
1577
a0d0e21e 1578void
864dbfa3 1579Perl_dounwind(pTHX_ I32 cxix)
a0d0e21e 1580{
f144f1e3 1581 if (!PL_curstackinfo) /* can happen if die during thread cloning */
1604cfb0 1582 return;
f144f1e3 1583
a0d0e21e 1584 while (cxstack_ix > cxix) {
4ebe6e95 1585 PERL_CONTEXT *cx = CX_CUR();
5e691bc6 1586
1604cfb0
MS
1587 CX_DEBUG(cx, "UNWIND");
1588 /* Note: we don't need to restore the base context info till the end. */
76d10131
DM
1589
1590 CX_LEAVE_SCOPE(cx);
1591
1604cfb0
MS
1592 switch (CxTYPE(cx)) {
1593 case CXt_SUBST:
1594 CX_POPSUBST(cx);
7332835e
DM
1595 /* CXt_SUBST is not a block context type, so skip the
1596 * cx_popblock(cx) below */
1597 if (cxstack_ix == cxix + 1) {
1598 cxstack_ix--;
1599 return;
1600 }
1604cfb0
MS
1601 break;
1602 case CXt_SUB:
1603 cx_popsub(cx);
1604 break;
1605 case CXt_EVAL:
1606 cx_popeval(cx);
1607 break;
1608 case CXt_LOOP_PLAIN:
1609 case CXt_LOOP_LAZYIV:
1610 case CXt_LOOP_LAZYSV:
1611 case CXt_LOOP_LIST:
1612 case CXt_LOOP_ARY:
1613 cx_poploop(cx);
1614 break;
1615 case CXt_WHEN:
1616 cx_popwhen(cx);
1617 break;
1618 case CXt_GIVEN:
1619 cx_popgiven(cx);
1620 break;
1621 case CXt_BLOCK:
1622 case CXt_NULL:
f217cb3d 1623 /* these two don't have a POPFOO() */
1604cfb0
MS
1624 break;
1625 case CXt_FORMAT:
1626 cx_popformat(cx);
1627 break;
1628 }
fc6e609e 1629 if (cxstack_ix == cxix + 1) {
ed8ff0f3 1630 cx_popblock(cx);
fc6e609e 1631 }
1604cfb0 1632 cxstack_ix--;
a0d0e21e 1633 }
fc6e609e 1634
a0d0e21e
LW
1635}
1636
5a844595
GS
1637void
1638Perl_qerror(pTHX_ SV *err)
1639{
7918f24d
NC
1640 PERL_ARGS_ASSERT_QERROR;
1641
6b2fb389 1642 if (PL_in_eval) {
1604cfb0
MS
1643 if (PL_in_eval & EVAL_KEEPERR) {
1644 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf,
ecad31f0 1645 SVfARG(err));
1604cfb0
MS
1646 }
1647 else
1648 sv_catsv(ERRSV, err);
6b2fb389 1649 }
5a844595 1650 else if (PL_errors)
1604cfb0 1651 sv_catsv(PL_errors, err);
5a844595 1652 else
1604cfb0 1653 Perl_warn(aTHX_ "%" SVf, SVfARG(err));
13765c85 1654 if (PL_parser)
1604cfb0 1655 ++PL_parser->error_count;
5a844595
GS
1656}
1657
d308a779
DM
1658
1659
06a7bc17
DM
1660/* pop a CXt_EVAL context and in addition, if it was a require then
1661 * based on action:
1662 * 0: do nothing extra;
1663 * 1: undef $INC{$name}; croak "$name did not return a true value";
1664 * 2: delete $INC{$name}; croak "$errsv: Compilation failed in require"
1665 */
d308a779 1666
8a1fd305 1667static void
06a7bc17 1668S_pop_eval_context_maybe_croak(pTHX_ PERL_CONTEXT *cx, SV *errsv, int action)
d308a779 1669{
51787acf 1670 SV *namesv = NULL; /* init to avoid dumb compiler warning */
06a7bc17
DM
1671 bool do_croak;
1672
1673 CX_LEAVE_SCOPE(cx);
1674 do_croak = action && (CxOLD_OP_TYPE(cx) == OP_REQUIRE);
2a1e0dfe
DM
1675 if (do_croak) {
1676 /* keep namesv alive after cx_popeval() */
1677 namesv = cx->blk_eval.old_namesv;
1678 cx->blk_eval.old_namesv = NULL;
1679 sv_2mortal(namesv);
1680 }
06a7bc17
DM
1681 cx_popeval(cx);
1682 cx_popblock(cx);
1683 CX_POP(cx);
1684
1685 if (do_croak) {
1686 const char *fmt;
1687 HV *inc_hv = GvHVn(PL_incgv);
1688 I32 klen = SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv);
1689 const char *key = SvPVX_const(namesv);
d308a779 1690
06a7bc17
DM
1691 if (action == 1) {
1692 (void)hv_delete(inc_hv, key, klen, G_DISCARD);
147e3846 1693 fmt = "%" SVf " did not return a true value";
06a7bc17
DM
1694 errsv = namesv;
1695 }
1696 else {
1697 (void)hv_store(inc_hv, key, klen, &PL_sv_undef, 0);
147e3846 1698 fmt = "%" SVf "Compilation failed in require";
06a7bc17
DM
1699 if (!errsv)
1700 errsv = newSVpvs_flags("Unknown error\n", SVs_TEMP);
1701 }
1702
1703 Perl_croak(aTHX_ fmt, SVfARG(errsv));
1704 }
d308a779
DM
1705}
1706
1707
8c86f023
DM
1708/* die_unwind(): this is the final destination for the various croak()
1709 * functions. If we're in an eval, unwind the context and other stacks
1710 * back to the top-most CXt_EVAL and set $@ to msv; otherwise print msv
1711 * to STDERR and initiate an exit. Note that if the CXt_EVAL popped back
1712 * to is a require the exception will be rethrown, as requires don't
1713 * actually trap exceptions.
1714 */
06a7bc17 1715
bb4c52e0 1716void
c5df3096 1717Perl_die_unwind(pTHX_ SV *msv)
a0d0e21e 1718{
8c86f023 1719 SV *exceptsv = msv;
96d9b9cd 1720 U8 in_eval = PL_in_eval;
c5df3096 1721 PERL_ARGS_ASSERT_DIE_UNWIND;
87582a92 1722
96d9b9cd 1723 if (in_eval) {
1604cfb0 1724 I32 cxix;
a0d0e21e 1725
b66d79a6
DM
1726 /* We need to keep this SV alive through all the stack unwinding
1727 * and FREETMPSing below, while ensuing that it doesn't leak
1728 * if we call out to something which then dies (e.g. sub STORE{die}
1729 * when unlocalising a tied var). So we do a dance with
1730 * mortalising and SAVEFREEing.
1731 */
042abef7
N
1732 if (PL_phase == PERL_PHASE_DESTRUCT) {
1733 exceptsv = sv_mortalcopy(exceptsv);
1734 } else {
1735 exceptsv = sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv));
1736 }
8c86f023 1737
1604cfb0
MS
1738 /*
1739 * Historically, perl used to set ERRSV ($@) early in the die
1740 * process and rely on it not getting clobbered during unwinding.
1741 * That sucked, because it was liable to get clobbered, so the
1742 * setting of ERRSV used to emit the exception from eval{} has
1743 * been moved to much later, after unwinding (see just before
1744 * JMPENV_JUMP below). However, some modules were relying on the
1745 * early setting, by examining $@ during unwinding to use it as
1746 * a flag indicating whether the current unwinding was caused by
1747 * an exception. It was never a reliable flag for that purpose,
1748 * being totally open to false positives even without actual
1749 * clobberage, but was useful enough for production code to
1750 * semantically rely on it.
1751 *
1752 * We'd like to have a proper introspective interface that
1753 * explicitly describes the reason for whatever unwinding
1754 * operations are currently in progress, so that those modules
1755 * work reliably and $@ isn't further overloaded. But we don't
1756 * have one yet. In its absence, as a stopgap measure, ERRSV is
1757 * now *additionally* set here, before unwinding, to serve as the
1758 * (unreliable) flag that it used to.
1759 *
1760 * This behaviour is temporary, and should be removed when a
1761 * proper way to detect exceptional unwinding has been developed.
1762 * As of 2010-12, the authors of modules relying on the hack
1763 * are aware of the issue, because the modules failed on
1764 * perls 5.13.{1..7} which had late setting of $@ without this
1765 * early-setting hack.
1766 */
1767 if (!(in_eval & EVAL_KEEPERR)) {
933e3e63
TC
1768 /* remove any read-only/magic from the SV, so we don't
1769 get infinite recursion when setting ERRSV */
1770 SANE_ERRSV();
1604cfb0 1771 sv_setsv_flags(ERRSV, exceptsv,
8c86f023 1772 (SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL));
933e3e63 1773 }
22a30693 1774
1604cfb0
MS
1775 if (in_eval & EVAL_KEEPERR) {
1776 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %" SVf,
1777 SVfARG(exceptsv));
1778 }
1779
1780 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1781 && PL_curstackinfo->si_prev)
1782 {
1783 dounwind(-1);
1784 POPSTACK;
1785 }
1786
1787 if (cxix >= 0) {
1788 PERL_CONTEXT *cx;
1789 SV **oldsp;
1c23e2bd 1790 U8 gimme;
1604cfb0
MS
1791 JMPENV *restartjmpenv;
1792 OP *restartop;
a0d0e21e 1793
1604cfb0
MS
1794 if (cxix < cxstack_ix)
1795 dounwind(cxix);
a0d0e21e 1796
4ebe6e95 1797 cx = CX_CUR();
f7d0774b 1798 assert(CxTYPE(cx) == CXt_EVAL);
e17c1e7c
DM
1799
1800 /* return false to the caller of eval */
f5ddd604 1801 oldsp = PL_stack_base + cx->blk_oldsp;
f7d0774b 1802 gimme = cx->blk_gimme;
1604cfb0
MS
1803 if (gimme == G_SCALAR)
1804 *++oldsp = &PL_sv_undef;
1805 PL_stack_sp = oldsp;
f7d0774b 1806
1604cfb0
MS
1807 restartjmpenv = cx->blk_eval.cur_top_env;
1808 restartop = cx->blk_eval.retop;
b66d79a6
DM
1809
1810 /* We need a FREETMPS here to avoid late-called destructors
1811 * clobbering $@ *after* we set it below, e.g.
1812 * sub DESTROY { eval { die "X" } }
1813 * eval { my $x = bless []; die $x = 0, "Y" };
1814 * is($@, "Y")
1815 * Here the clearing of the $x ref mortalises the anon array,
1816 * which needs to be freed *before* $& is set to "Y",
1817 * otherwise it gets overwritten with "X".
1818 *
1819 * However, the FREETMPS will clobber exceptsv, so preserve it
1820 * on the savestack for now.
1821 */
1822 SAVEFREESV(SvREFCNT_inc_simple_NN(exceptsv));
1823 FREETMPS;
1824 /* now we're about to pop the savestack, so re-mortalise it */
1825 sv_2mortal(SvREFCNT_inc_simple_NN(exceptsv));
1826
06a7bc17
DM
1827 /* Note that unlike pp_entereval, pp_require isn't supposed to
1828 * trap errors. So if we're a require, after we pop the
1829 * CXt_EVAL that pp_require pushed, rethrow the error with
1830 * croak(exceptsv). This is all handled by the call below when
1831 * action == 2.
1832 */
1833 S_pop_eval_context_maybe_croak(aTHX_ cx, exceptsv, 2);
a0d0e21e 1834
1604cfb0 1835 if (!(in_eval & EVAL_KEEPERR)) {
933e3e63 1836 SANE_ERRSV();
1604cfb0 1837 sv_setsv(ERRSV, exceptsv);
933e3e63 1838 }
1604cfb0
MS
1839 PL_restartjmpenv = restartjmpenv;
1840 PL_restartop = restartop;
1841 JMPENV_JUMP(3);
1842 NOT_REACHED; /* NOTREACHED */
1843 }
a0d0e21e 1844 }
87582a92 1845
96d9b9cd 1846 write_to_stderr(exceptsv);
f86702cc 1847 my_failure_exit();
e5964223 1848 NOT_REACHED; /* NOTREACHED */
a0d0e21e
LW
1849}
1850
1851PP(pp_xor)
1852{
20b7effb 1853 dSP; dPOPTOPssrl;
f4c975aa 1854 if (SvTRUE_NN(left) != SvTRUE_NN(right))
1604cfb0 1855 RETSETYES;
a0d0e21e 1856 else
1604cfb0 1857 RETSETNO;
a0d0e21e
LW
1858}
1859
8dff4fc5 1860/*
dcccc8ff 1861
3f620621 1862=for apidoc_section $CV
dcccc8ff 1863
8dff4fc5
BM
1864=for apidoc caller_cx
1865
72d33970 1866The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
8dff4fc5 1867returned C<PERL_CONTEXT> structure can be interrogated to find all the
72d33970 1868information returned to Perl by C<caller>. Note that XSUBs don't get a
8dff4fc5
BM
1869stack frame, so C<caller_cx(0, NULL)> will return information for the
1870immediately-surrounding Perl code.
1871
1872This function skips over the automatic calls to C<&DB::sub> made on the
72d33970 1873behalf of the debugger. If the stack frame requested was a sub called by
8dff4fc5
BM
1874C<DB::sub>, the return value will be the frame for the call to
1875C<DB::sub>, since that has the correct line number/etc. for the call
72d33970 1876site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
8dff4fc5
BM
1877frame for the sub call itself.
1878
1879=cut
1880*/
1881
1882const PERL_CONTEXT *
1883Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
a0d0e21e 1884{
5b6f7443 1885 I32 cxix = dopopto_cursub();
eb578fdb
KW
1886 const PERL_CONTEXT *cx;
1887 const PERL_CONTEXT *ccstack = cxstack;
901017d6 1888 const PERL_SI *top_si = PL_curstackinfo;
27d41816 1889
a0d0e21e 1890 for (;;) {
1604cfb0
MS
1891 /* we may be in a higher stacklevel, so dig down deeper */
1892 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1893 top_si = top_si->si_prev;
1894 ccstack = top_si->si_cxstack;
1895 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1896 }
1897 if (cxix < 0)
1898 return NULL;
1899 /* caller() should not report the automatic calls to &DB::sub */
1900 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1901 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1902 count++;
1903 if (!count--)
1904 break;
1905 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1906 }
2c375eb9
GS
1907
1908 cx = &ccstack[cxix];
8dff4fc5
BM
1909 if (dbcxp) *dbcxp = cx;
1910
7766f137 1911 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1912 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1604cfb0
MS
1913 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1914 field below is defined for any cx. */
1915 /* caller() should not report the automatic calls to &DB::sub */
1916 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1917 cx = &ccstack[dbcxix];
06a5b730
PP
1918 }
1919
8dff4fc5
BM
1920 return cx;
1921}
1922
1923PP(pp_caller)
1924{
8dff4fc5 1925 dSP;
eb578fdb 1926 const PERL_CONTEXT *cx;
8dff4fc5 1927 const PERL_CONTEXT *dbcx;
1c23e2bd 1928 U8 gimme = GIMME_V;
d527ce7c 1929 const HEK *stash_hek;
8dff4fc5 1930 I32 count = 0;
ce0b554b 1931 bool has_arg = MAXARG && TOPs;
25502127 1932 const COP *lcop;
8dff4fc5 1933
ce0b554b
FC
1934 if (MAXARG) {
1935 if (has_arg)
1604cfb0 1936 count = POPi;
ce0b554b
FC
1937 else (void)POPs;
1938 }
8dff4fc5 1939
ce0b554b 1940 cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx);
8dff4fc5 1941 if (!cx) {
1604cfb0
MS
1942 if (gimme != G_ARRAY) {
1943 EXTEND(SP, 1);
1944 RETPUSHUNDEF;
1945 }
1946 RETURN;
8dff4fc5
BM
1947 }
1948
5e691bc6 1949 CX_DEBUG(cx, "CALLER");
d0279c7c 1950 assert(CopSTASH(cx->blk_oldcop));
e7886211
FC
1951 stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV
1952 ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop))
1953 : NULL;
48ebc325 1954 if (gimme != G_ARRAY) {
27d41816 1955 EXTEND(SP, 1);
1604cfb0
MS
1956 if (!stash_hek)
1957 PUSHs(&PL_sv_undef);
1958 else {
1959 dTARGET;
1960 sv_sethek(TARG, stash_hek);
1961 PUSHs(TARG);
1962 }
1963 RETURN;
a0d0e21e 1964 }
a0d0e21e 1965
b3ca2e83 1966 EXTEND(SP, 11);
27d41816 1967
d527ce7c 1968 if (!stash_hek)
1604cfb0 1969 PUSHs(&PL_sv_undef);
d527ce7c 1970 else {
1604cfb0
MS
1971 dTARGET;
1972 sv_sethek(TARG, stash_hek);
1973 PUSHTARG;
d527ce7c 1974 }
6e449a3a 1975 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
e6dae479 1976 lcop = closest_cop(cx->blk_oldcop, OpSIBLING(cx->blk_oldcop),
1604cfb0 1977 cx->blk_sub.retop, TRUE);
25502127 1978 if (!lcop)
1604cfb0 1979 lcop = cx->blk_oldcop;
e9e9e546 1980 mPUSHu(CopLINE(lcop));
ce0b554b 1981 if (!has_arg)
1604cfb0 1982 RETURN;
7766f137 1983 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1604cfb0
MS
1984 /* So is ccstack[dbcxix]. */
1985 if (CvHASGV(dbcx->blk_sub.cv)) {
1986 PUSHs(cv_name(dbcx->blk_sub.cv, 0, 0));
1987 PUSHs(boolSV(CxHASARGS(cx)));
1988 }
1989 else {
1990 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1991 PUSHs(boolSV(CxHASARGS(cx)));
1992 }
a0d0e21e
LW
1993 }
1994 else {
1604cfb0
MS
1995 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1996 PUSHs(&PL_sv_zero);
a0d0e21e 1997 }
1c23e2bd 1998 gimme = cx->blk_gimme;
54310121 1999 if (gimme == G_VOID)
1604cfb0 2000 PUSHs(&PL_sv_undef);
54310121 2001 else
1604cfb0 2002 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 2003 if (CxTYPE(cx) == CXt_EVAL) {
1604cfb0
MS
2004 /* eval STRING */
2005 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
78beb4ca
TC
2006 SV *cur_text = cx->blk_eval.cur_text;
2007 if (SvCUR(cur_text) >= 2) {
2008 PUSHs(newSVpvn_flags(SvPVX(cur_text), SvCUR(cur_text)-2,
2009 SvUTF8(cur_text)|SVs_TEMP));
2010 }
2011 else {
2012 /* I think this is will always be "", but be sure */
2013 PUSHs(sv_2mortal(newSVsv(cur_text)));
2014 }
2015
1604cfb0
MS
2016 PUSHs(&PL_sv_no);
2017 }
2018 /* require */
2019 else if (cx->blk_eval.old_namesv) {
2020 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
2021 PUSHs(&PL_sv_yes);
2022 }
2023 /* eval BLOCK (try blocks have old_namesv == 0) */
2024 else {
2025 PUSHs(&PL_sv_undef);
2026 PUSHs(&PL_sv_undef);
2027 }
4633a7c4 2028 }
a682de96 2029 else {
1604cfb0
MS
2030 PUSHs(&PL_sv_undef);
2031 PUSHs(&PL_sv_undef);
a682de96 2032 }
bafb2adc 2033 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1604cfb0 2034 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 2035 {
9513529b 2036 /* slot 0 of the pad contains the original @_ */
1604cfb0 2037 AV * const ary = MUTABLE_AV(AvARRAY(MUTABLE_AV(
9513529b
DM
2038 PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[
2039 cx->blk_sub.olddepth+1]))[0]);
1604cfb0 2040 const SSize_t off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 2041
1604cfb0 2042 Perl_init_dbargs(aTHX);
a0d0e21e 2043
1604cfb0
MS
2044 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
2045 av_extend(PL_dbargs, AvFILLp(ary) + off);
f14cf363
TC
2046 if (AvFILLp(ary) + 1 + off)
2047 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1604cfb0 2048 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 2049 }
6e449a3a 2050 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5 2051 {
1604cfb0
MS
2052 SV * mask ;
2053 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 2054
1604cfb0 2055 if (old_warnings == pWARN_NONE)
e476b1b5 2056 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1604cfb0 2057 else if (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0)
f07626ad 2058 mask = &PL_sv_undef ;
ac27b0f5 2059 else if (old_warnings == pWARN_ALL ||
1604cfb0
MS
2060 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
2061 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
2062 }
e476b1b5 2063 else
72dc9ed5 2064 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 2065 mPUSHs(mask);
e476b1b5 2066 }
b3ca2e83 2067
c28fe1ec 2068 PUSHs(cx->blk_oldcop->cop_hints_hash ?
1604cfb0
MS
2069 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
2070 : &PL_sv_undef);
a0d0e21e
LW
2071 RETURN;
2072}
2073
a0d0e21e
LW
2074PP(pp_reset)
2075{
39644a26 2076 dSP;
ca826051
FC
2077 const char * tmps;
2078 STRLEN len = 0;
2372d073
DM
2079 if (MAXARG < 1 || (!TOPs && !POPs)) {
2080 EXTEND(SP, 1);
1604cfb0 2081 tmps = NULL, len = 0;
2372d073 2082 }
ca826051 2083 else
1604cfb0 2084 tmps = SvPVx_const(POPs, len);
ca826051 2085 sv_resetpvn(tmps, len, CopSTASH(PL_curcop));
3280af22 2086 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2087 RETURN;
2088}
2089
dd2155a4
DM
2090/* like pp_nextstate, but used instead when the debugger is active */
2091
a0d0e21e
LW
2092PP(pp_dbstate)
2093{
533c011a 2094 PL_curcop = (COP*)PL_op;
a0d0e21e 2095 TAINT_NOT; /* Each statement is presumed innocent */
4ebe6e95 2096 PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp;
a0d0e21e
LW
2097 FREETMPS;
2098
f410a211
NC
2099 PERL_ASYNC_CHECK();
2100
88df5f01 2101 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1604cfb0 2102 || PL_DBsingle_iv || PL_DBsignal_iv || PL_DBtrace_iv)
a0d0e21e 2103 {
1604cfb0
MS
2104 dSP;
2105 PERL_CONTEXT *cx;
2106 const U8 gimme = G_ARRAY;
2107 GV * const gv = PL_DBgv;
2108 CV * cv = NULL;
432d4561
JL
2109
2110 if (gv && isGV_with_GP(gv))
2111 cv = GvCV(gv);
a0d0e21e 2112
1604cfb0
MS
2113 if (!cv || (!CvROOT(cv) && !CvXSUB(cv)))
2114 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 2115
1604cfb0
MS
2116 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2117 /* don't do recursive DB::DB call */
2118 return NORMAL;
748a9306 2119
1604cfb0 2120 if (CvISXSUB(cv)) {
8ae997c5
DM
2121 ENTER;
2122 SAVEI32(PL_debug);
2123 PL_debug = 0;
2124 SAVESTACK_POS();
8a44b450 2125 SAVETMPS;
1604cfb0
MS
2126 PUSHMARK(SP);
2127 (void)(*CvXSUB(cv))(aTHX_ cv);
2128 FREETMPS;
2129 LEAVE;
2130 return NORMAL;
2131 }
2132 else {
2133 cx = cx_pushblock(CXt_SUB, gimme, SP, PL_savestack_ix);
2134 cx_pushsub(cx, cv, PL_op->op_next, 0);
a73d8813
DM
2135 /* OP_DBSTATE's op_private holds hint bits rather than
2136 * the lvalue-ish flags seen in OP_ENTERSUB. So cancel
2137 * any CxLVAL() flags that have now been mis-calculated */
2138 cx->blk_u16 = 0;
8ae997c5
DM
2139
2140 SAVEI32(PL_debug);
2141 PL_debug = 0;
2142 SAVESTACK_POS();
1604cfb0
MS
2143 CvDEPTH(cv)++;
2144 if (CvDEPTH(cv) >= 2)
2145 pad_push(CvPADLIST(cv), CvDEPTH(cv));
2146 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv));
2147 RETURNOP(CvSTART(cv));
2148 }
a0d0e21e
LW
2149 }
2150 else
1604cfb0 2151 return NORMAL;
a0d0e21e
LW
2152}
2153
0663a8f8 2154
2b9a6457
VP
2155PP(pp_enter)
2156{
1c23e2bd 2157 U8 gimme = GIMME_V;
2b9a6457 2158
d4ce7588
DM
2159 (void)cx_pushblock(CXt_BLOCK, gimme, PL_stack_sp, PL_savestack_ix);
2160 return NORMAL;
2b9a6457
VP
2161}
2162
d4ce7588 2163
2b9a6457
VP
2164PP(pp_leave)
2165{
eb578fdb 2166 PERL_CONTEXT *cx;
f5ddd604 2167 SV **oldsp;
1c23e2bd 2168 U8 gimme;
2b9a6457 2169
4ebe6e95 2170 cx = CX_CUR();
61d3b95a 2171 assert(CxTYPE(cx) == CXt_BLOCK);
4df352a8
DM
2172
2173 if (PL_op->op_flags & OPf_SPECIAL)
c349b9a0 2174 /* fake block should preserve $1 et al; e.g. /(...)/ while ...; */
1604cfb0 2175 cx->blk_oldpm = PL_curpm;
4df352a8 2176
f5ddd604 2177 oldsp = PL_stack_base + cx->blk_oldsp;
61d3b95a 2178 gimme = cx->blk_gimme;
2b9a6457 2179
0663a8f8 2180 if (gimme == G_VOID)
f5ddd604 2181 PL_stack_sp = oldsp;
0663a8f8 2182 else
f5ddd604 2183 leave_adjust_stacks(oldsp, oldsp, gimme,
75bc488d 2184 PL_op->op_private & OPpLVALUE ? 3 : 1);
67f63db7 2185
2f450c1b 2186 CX_LEAVE_SCOPE(cx);
ed8ff0f3 2187 cx_popblock(cx);
5da525e9 2188 CX_POP(cx);
2b9a6457 2189
0663a8f8 2190 return NORMAL;
2b9a6457
VP
2191}
2192
eaa9f768
JH
2193static bool
2194S_outside_integer(pTHX_ SV *sv)
2195{
2196 if (SvOK(sv)) {
2197 const NV nv = SvNV_nomg(sv);
415b66b2
JH
2198 if (Perl_isinfnan(nv))
2199 return TRUE;
eaa9f768
JH
2200#ifdef NV_PRESERVES_UV
2201 if (nv < (NV)IV_MIN || nv > (NV)IV_MAX)
2202 return TRUE;
2203#else
2204 if (nv <= (NV)IV_MIN)
2205 return TRUE;
2206 if ((nv > 0) &&
2207 ((nv > (NV)UV_MAX ||
2208 SvUV_nomg(sv) > (UV)IV_MAX)))
2209 return TRUE;
2210#endif
2211 }
2212 return FALSE;
2213}
2214
a0d0e21e
LW
2215PP(pp_enteriter)
2216{
20b7effb 2217 dSP; dMARK;
eb578fdb 2218 PERL_CONTEXT *cx;
1c23e2bd 2219 const U8 gimme = GIMME_V;
4ad63d70 2220 void *itervarp; /* GV or pad slot of the iteration variable */
f0bb9bf7 2221 SV *itersave; /* the old var in the iterator var slot */
93661e56 2222 U8 cxflags = 0;
a0d0e21e 2223
aafca525 2224 if (PL_op->op_targ) { /* "my" variable */
1604cfb0 2225 itervarp = &PAD_SVl(PL_op->op_targ);
4ad63d70
DM
2226 itersave = *(SV**)itervarp;
2227 assert(itersave);
1604cfb0 2228 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
fdb8b82b
DM
2229 /* the SV currently in the pad slot is never live during
2230 * iteration (the slot is always aliased to one of the items)
2231 * so it's always stale */
1604cfb0
MS
2232 SvPADSTALE_on(itersave);
2233 }
4ad63d70 2234 SvREFCNT_inc_simple_void_NN(itersave);
1604cfb0 2235 cxflags = CXp_FOR_PAD;
54b9620d 2236 }
d39c26a6 2237 else {
1604cfb0
MS
2238 SV * const sv = POPs;
2239 itervarp = (void *)sv;
d4e2b4d6 2240 if (LIKELY(isGV(sv))) { /* symbol table variable */
6d3ca00e
DM
2241 itersave = GvSV(sv);
2242 SvREFCNT_inc_simple_void(itersave);
93661e56 2243 cxflags = CXp_FOR_GV;
7896dde7
Z
2244 if (PL_op->op_private & OPpITER_DEF)
2245 cxflags |= CXp_FOR_DEF;
d4e2b4d6
DM
2246 }
2247 else { /* LV ref: for \$foo (...) */
2248 assert(SvTYPE(sv) == SVt_PVMG);
2249 assert(SvMAGIC(sv));
2250 assert(SvMAGIC(sv)->mg_type == PERL_MAGIC_lvref);
2251 itersave = NULL;
93661e56 2252 cxflags = CXp_FOR_LVREF;
d4e2b4d6 2253 }
d39c26a6 2254 }
7896dde7
Z
2255 /* OPpITER_DEF (implicit $_) should only occur with a GV iter var */
2256 assert((cxflags & CXp_FOR_GV) || !(PL_op->op_private & OPpITER_DEF));
0d863452 2257
75fbe096
DM
2258 /* Note that this context is initially set as CXt_NULL. Further on
2259 * down it's changed to one of the CXt_LOOP_*. Before it's changed,
2260 * there mustn't be anything in the blk_loop substruct that requires
2261 * freeing or undoing, in case we die in the meantime. And vice-versa.
2262 */
ed8ff0f3 2263 cx = cx_pushblock(cxflags, gimme, MARK, PL_savestack_ix);
d1b6bf72 2264 cx_pushloop_for(cx, itervarp, itersave);
2c49879e 2265
533c011a 2266 if (PL_op->op_flags & OPf_STACKED) {
2c49879e
DM
2267 /* OPf_STACKED implies either a single array: for(@), with a
2268 * single AV on the stack, or a range: for (1..5), with 1 and 5 on
2269 * the stack */
1604cfb0
MS
2270 SV *maybe_ary = POPs;
2271 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2c49879e 2272 /* range */
1604cfb0
MS
2273 dPOPss;
2274 SV * const right = maybe_ary;
2275 if (UNLIKELY(cxflags & CXp_FOR_LVREF))
2276 DIE(aTHX_ "Assigned value is not a reference");
2277 SvGETMAGIC(sv);
2278 SvGETMAGIC(right);
2279 if (RANGE_IS_NUMERIC(sv,right)) {
2280 cx->cx_type |= CXt_LOOP_LAZYIV;
2281 if (S_outside_integer(aTHX_ sv) ||
eaa9f768 2282 S_outside_integer(aTHX_ right))
1604cfb0
MS
2283 DIE(aTHX_ "Range iterator outside integer range");
2284 cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv);
2285 cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right);
2286 }
2287 else {
2288 cx->cx_type |= CXt_LOOP_LAZYSV;
2289 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2290 cx->blk_loop.state_u.lazysv.end = right;
2291 SvREFCNT_inc_simple_void_NN(right);
2292 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2293 /* This will do the upgrade to SVt_PV, and warn if the value
2294 is uninitialised. */
2295 (void) SvPV_nolen_const(right);
2296 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2297 to replace !SvOK() with a pointer to "". */
2298 if (!SvOK(right)) {
2299 SvREFCNT_dec(right);
2300 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2301 }
2302 }
2303 }
2304 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2c49879e 2305 /* for (@array) {} */
93661e56 2306 cx->cx_type |= CXt_LOOP_ARY;
1604cfb0
MS
2307 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2308 SvREFCNT_inc_simple_void_NN(maybe_ary);
2309 cx->blk_loop.state_u.ary.ix =
2310 (PL_op->op_private & OPpITER_REVERSED) ?
2311 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2312 -1;
2313 }
8a1f10dd 2314 /* EXTEND(SP, 1) not needed in this branch because we just did POPs */
89ea2908 2315 }
d01136d6 2316 else { /* iterating over items on the stack */
93661e56 2317 cx->cx_type |= CXt_LOOP_LIST;
1bef65a2 2318 cx->blk_oldsp = SP - PL_stack_base;
1604cfb0 2319 cx->blk_loop.state_u.stack.basesp = MARK - PL_stack_base;
93661e56
DM
2320 cx->blk_loop.state_u.stack.ix =
2321 (PL_op->op_private & OPpITER_REVERSED)
2322 ? cx->blk_oldsp + 1
2323 : cx->blk_loop.state_u.stack.basesp;
8a1f10dd
DM
2324 /* pre-extend stack so pp_iter doesn't have to check every time
2325 * it pushes yes/no */
2326 EXTEND(SP, 1);
4633a7c4 2327 }
a0d0e21e
LW
2328
2329 RETURN;
2330}
2331
2332PP(pp_enterloop)
2333{
eb578fdb 2334 PERL_CONTEXT *cx;
1c23e2bd 2335 const U8 gimme = GIMME_V;
a0d0e21e 2336
d4ce7588 2337 cx = cx_pushblock(CXt_LOOP_PLAIN, gimme, PL_stack_sp, PL_savestack_ix);
d1b6bf72 2338 cx_pushloop_plain(cx);
d4ce7588 2339 return NORMAL;
a0d0e21e
LW
2340}
2341
d4ce7588 2342
a0d0e21e
LW
2343PP(pp_leaveloop)
2344{
eb578fdb 2345 PERL_CONTEXT *cx;
1c23e2bd 2346 U8 gimme;
032736ab 2347 SV **base;
f5ddd604 2348 SV **oldsp;
a0d0e21e 2349
4ebe6e95 2350 cx = CX_CUR();
3b719c58 2351 assert(CxTYPE_is_LOOP(cx));
032736ab
DM
2352 oldsp = PL_stack_base + cx->blk_oldsp;
2353 base = CxTYPE(cx) == CXt_LOOP_LIST
1bef65a2 2354 ? PL_stack_base + cx->blk_loop.state_u.stack.basesp
032736ab 2355 : oldsp;
61d3b95a 2356 gimme = cx->blk_gimme;
f86702cc 2357
0663a8f8 2358 if (gimme == G_VOID)
032736ab 2359 PL_stack_sp = base;
0663a8f8 2360 else
032736ab 2361 leave_adjust_stacks(oldsp, base, gimme,
75bc488d 2362 PL_op->op_private & OPpLVALUE ? 3 : 1);
f86702cc 2363
2f450c1b 2364 CX_LEAVE_SCOPE(cx);
d1b6bf72 2365 cx_poploop(cx); /* Stack values are safe: release loop vars ... */
ed8ff0f3 2366 cx_popblock(cx);
5da525e9 2367 CX_POP(cx);
f86702cc 2368
f86702cc 2369 return NORMAL;
a0d0e21e
LW
2370}
2371
31ccb4f5
DM
2372
2373/* This duplicates most of pp_leavesub, but with additional code to handle
2374 * return args in lvalue context. It was forked from pp_leavesub to
2375 * avoid slowing down that function any further.
2376 *
2377 * Any changes made to this function may need to be copied to pp_leavesub
2378 * and vice-versa.
c349b9a0
DM
2379 *
2380 * also tail-called by pp_return
57486a97
DM
2381 */
2382
31ccb4f5 2383PP(pp_leavesublv)
3bdf583b 2384{
1c23e2bd 2385 U8 gimme;
57486a97 2386 PERL_CONTEXT *cx;
799da9d7 2387 SV **oldsp;
5da525e9 2388 OP *retop;
57486a97 2389
4ebe6e95 2390 cx = CX_CUR();
61d3b95a
DM
2391 assert(CxTYPE(cx) == CXt_SUB);
2392
2393 if (CxMULTICALL(cx)) {
1f0ba93b
DM
2394 /* entry zero of a stack is always PL_sv_undef, which
2395 * simplifies converting a '()' return into undef in scalar context */
2396 assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef);
1604cfb0 2397 return 0;
1f0ba93b 2398 }
85ecf147 2399
61d3b95a 2400 gimme = cx->blk_gimme;
799da9d7 2401 oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */
57486a97 2402
799da9d7
DM
2403 if (gimme == G_VOID)
2404 PL_stack_sp = oldsp;
2405 else {
2406 U8 lval = CxLVAL(cx);
2407 bool is_lval = (lval && !(lval & OPpENTERSUB_INARGS));
2408 const char *what = NULL;
2409
2410 if (gimme == G_SCALAR) {
2411 if (is_lval) {
2412 /* check for bad return arg */
2413 if (oldsp < PL_stack_sp) {
2414 SV *sv = *PL_stack_sp;
2415 if ((SvPADTMP(sv) || SvREADONLY(sv))) {
2416 what =
2417 SvREADONLY(sv) ? (sv == &PL_sv_undef) ? "undef"
2418 : "a readonly value" : "a temporary";
2419 }
2420 else goto ok;
2421 }
2422 else {
2423 /* sub:lvalue{} will take us here. */
2424 what = "undef";
2425 }
2426 croak:
2427 Perl_croak(aTHX_
2428 "Can't return %s from lvalue subroutine", what);
2429 }
57486a97 2430
799da9d7 2431 ok:
e02ce34b 2432 leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2);
e80c4acf 2433
799da9d7
DM
2434 if (lval & OPpDEREF) {
2435 /* lval_sub()->{...} and similar */
2436 dSP;
2437 SvGETMAGIC(TOPs);
2438 if (!SvOK(TOPs)) {
2439 TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF);
2440 }
2441 PUTBACK;
2442 }
2443 }
2444 else {
2445 assert(gimme == G_ARRAY);
2446 assert (!(lval & OPpDEREF));
2447
2448 if (is_lval) {
2449 /* scan for bad return args */
2450 SV **p;
2451 for (p = PL_stack_sp; p > oldsp; p--) {
2452 SV *sv = *p;
2453 /* the PL_sv_undef exception is to allow things like
2454 * this to work, where PL_sv_undef acts as 'skip'
2455 * placeholder on the LHS of list assigns:
2456 * sub foo :lvalue { undef }
2457 * ($a, undef, foo(), $b) = 1..4;
2458 */
2459 if (sv != &PL_sv_undef && (SvPADTMP(sv) || SvREADONLY(sv)))
2460 {
2461 /* Might be flattened array after $#array = */
2462 what = SvREADONLY(sv)
2463 ? "a readonly value" : "a temporary";
2464 goto croak;
2465 }
2466 }
2467 }
2468
e02ce34b 2469 leave_adjust_stacks(oldsp, oldsp, gimme, is_lval ? 3 : 2);
799da9d7 2470 }
3bdf583b 2471 }
57486a97 2472
2f450c1b 2473 CX_LEAVE_SCOPE(cx);
a73d8813 2474 cx_popsub(cx); /* Stack values are safe: release CV and @_ ... */
ed8ff0f3 2475 cx_popblock(cx);
5da525e9
DM
2476 retop = cx->blk_sub.retop;
2477 CX_POP(cx);
57486a97 2478
5da525e9 2479 return retop;
3bdf583b
FC
2480}
2481
57486a97 2482
a0d0e21e
LW
2483PP(pp_return)
2484{
20b7effb 2485 dSP; dMARK;
eb578fdb 2486 PERL_CONTEXT *cx;
a1325b90
PLE
2487 I32 cxix = dopopto_cursub();
2488
d40dc6b1
DM
2489 assert(cxstack_ix >= 0);
2490 if (cxix < cxstack_ix) {
2491 if (cxix < 0) {
79646418
DM
2492 if (!( PL_curstackinfo->si_type == PERLSI_SORT
2493 || ( PL_curstackinfo->si_type == PERLSI_MULTICALL
2494 && (cxstack[0].cx_type & CXp_SUB_RE_FAKE))
2495 )
2496 )
3089a108 2497 DIE(aTHX_ "Can't return outside a subroutine");
79646418
DM
2498 /* We must be in:
2499 * a sort block, which is a CXt_NULL not a CXt_SUB;
2500 * or a /(?{...})/ block.
2501 * Handle specially. */
2502 assert(CxTYPE(&cxstack[0]) == CXt_NULL
2503 || ( CxTYPE(&cxstack[0]) == CXt_SUB
2504 && (cxstack[0].cx_type & CXp_SUB_RE_FAKE)));
3089a108
DM
2505 if (cxstack_ix > 0) {
2506 /* See comment below about context popping. Since we know
2507 * we're scalar and not lvalue, we can preserve the return
2508 * value in a simpler fashion than there. */
2509 SV *sv = *SP;
d40dc6b1 2510 assert(cxstack[0].blk_gimme == G_SCALAR);
3089a108
DM
2511 if ( (sp != PL_stack_base)
2512 && !(SvFLAGS(sv) & (SVs_TEMP|SVs_PADTMP))
2513 )
2514 *SP = sv_mortalcopy(sv);
2515 dounwind(0);
d40dc6b1 2516 }
3089a108
DM
2517 /* caller responsible for popping cxstack[0] */
2518 return 0;
d40dc6b1 2519 }
3089a108
DM
2520
2521 /* There are contexts that need popping. Doing this may free the
c349b9a0 2522 * return value(s), so preserve them first: e.g. popping the plain
3089a108
DM
2523 * loop here would free $x:
2524 * sub f { { my $x = 1; return $x } }
2525 * We may also need to shift the args down; for example,
2526 * for (1,2) { return 3,4 }
c349b9a0
DM
2527 * leaves 1,2,3,4 on the stack. Both these actions will be done by
2528 * leave_adjust_stacks(), along with freeing any temps. Note that
2529 * whoever we tail-call (e.g. pp_leaveeval) will also call
2530 * leave_adjust_stacks(); however, the second call is likely to
2531 * just see a bunch of SvTEMPs with a ref count of 1, and so just
2532 * pass them through, rather than copying them again. So this
2533 * isn't as inefficient as it sounds.
3089a108
DM
2534 */
2535 cx = &cxstack[cxix];
3089a108 2536 PUTBACK;
e02ce34b
DM
2537 if (cx->blk_gimme != G_VOID)
2538 leave_adjust_stacks(MARK, PL_stack_base + cx->blk_oldsp,
fc6e609e
DM
2539 cx->blk_gimme,
2540 CxTYPE(cx) == CXt_SUB && CvLVALUE(cx->blk_sub.cv)
2541 ? 3 : 0);
0663a8f8 2542 SPAGAIN;
1604cfb0 2543 dounwind(cxix);
42831ce4 2544 cx = &cxstack[cxix]; /* CX stack may have been realloced */
d40dc6b1 2545 }
3089a108
DM
2546 else {
2547 /* Like in the branch above, we need to handle any extra junk on
2548 * the stack. But because we're not also popping extra contexts, we
2549 * don't have to worry about prematurely freeing args. So we just
2550 * need to do the bare minimum to handle junk, and leave the main
2551 * arg processing in the function we tail call, e.g. pp_leavesub.
2552 * In list context we have to splice out the junk; in scalar
2553 * context we can leave as-is (pp_leavesub will later return the
2554 * top stack element). But for an empty arg list, e.g.
2555 * for (1,2) { return }
2556 * we need to set sp = oldsp so that pp_leavesub knows to push
2557 * &PL_sv_undef onto the stack.
2558 */
3ebe7c5a
DM
2559 SV **oldsp;
2560 cx = &cxstack[cxix];
2561 oldsp = PL_stack_base + cx->blk_oldsp;
2562 if (oldsp != MARK) {
2563 SSize_t nargs = SP - MARK;
2564 if (nargs) {
2565 if (cx->blk_gimme == G_ARRAY) {
2566 /* shift return args to base of call stack frame */
2567 Move(MARK + 1, oldsp + 1, nargs, SV*);
2568 PL_stack_sp = oldsp + nargs;
2569 }
6228a1e1 2570 }
3ebe7c5a
DM
2571 else
2572 PL_stack_sp = oldsp;
13929c4c 2573 }
3089a108 2574 }
617a4f41
DM
2575
2576 /* fall through to a normal exit */
2577 switch (CxTYPE(cx)) {
2578 case CXt_EVAL:
99dbf645 2579 return CxEVALBLOCK(cx)
617a4f41
DM
2580 ? Perl_pp_leavetry(aTHX)
2581 : Perl_pp_leaveeval(aTHX);
2582 case CXt_SUB:
13929c4c 2583 return CvLVALUE(cx->blk_sub.cv)
31ccb4f5 2584 ? Perl_pp_leavesublv(aTHX)
13929c4c 2585 : Perl_pp_leavesub(aTHX);
7766f137 2586 case CXt_FORMAT:
617a4f41 2587 return Perl_pp_leavewrite(aTHX);
a0d0e21e 2588 default:
1604cfb0 2589 DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx));
a0d0e21e 2590 }
a0d0e21e
LW
2591}
2592
42b5eca0 2593/* find the enclosing loop or labelled loop and dounwind() back to it. */
4f443c3d 2594
31705cda 2595static PERL_CONTEXT *
42b5eca0 2596S_unwind_loop(pTHX)
a0d0e21e 2597{
a0d0e21e 2598 I32 cxix;
1f039d60 2599 if (PL_op->op_flags & OPf_SPECIAL) {
1604cfb0
MS
2600 cxix = dopoptoloop(cxstack_ix);
2601 if (cxix < 0)
2602 /* diag_listed_as: Can't "last" outside a loop block */
2603 Perl_croak(aTHX_ "Can't \"%s\" outside a loop block",
42b5eca0 2604 OP_NAME(PL_op));
1f039d60
FC
2605 }
2606 else {
1604cfb0
MS
2607 dSP;
2608 STRLEN label_len;
2609 const char * const label =
2610 PL_op->op_flags & OPf_STACKED
2611 ? SvPV(TOPs,label_len)
2612 : (label_len = strlen(cPVOP->op_pv), cPVOP->op_pv);
2613 const U32 label_flags =
2614 PL_op->op_flags & OPf_STACKED
2615 ? SvUTF8(POPs)
2616 : (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
2617 PUTBACK;
1f039d60 2618 cxix = dopoptolabel(label, label_len, label_flags);
1604cfb0
MS
2619 if (cxix < 0)
2620 /* diag_listed_as: Label not found for "last %s" */
2621 Perl_croak(aTHX_ "Label not found for \"%s %" SVf "\"",
2622 OP_NAME(PL_op),
1f039d60
FC
2623 SVfARG(PL_op->op_flags & OPf_STACKED
2624 && !SvGMAGICAL(TOPp1s)
2625 ? TOPp1s
2626 : newSVpvn_flags(label,
2627 label_len,
2628 label_flags | SVs_TEMP)));
2629 }
2630 if (cxix < cxstack_ix)
1604cfb0 2631 dounwind(cxix);
dc7f00ca 2632 return &cxstack[cxix];
1f039d60
FC
2633}
2634
dc7f00ca 2635
1f039d60
FC
2636PP(pp_last)
2637{
eb578fdb 2638 PERL_CONTEXT *cx;
5da525e9 2639 OP* nextop;
9d4ba2ae 2640
42b5eca0 2641 cx = S_unwind_loop(aTHX);
4df352a8 2642
93661e56 2643 assert(CxTYPE_is_LOOP(cx));
1bef65a2
DM
2644 PL_stack_sp = PL_stack_base
2645 + (CxTYPE(cx) == CXt_LOOP_LIST
2646 ? cx->blk_loop.state_u.stack.basesp
2647 : cx->blk_oldsp
2648 );
a0d0e21e 2649
a1f49e72 2650 TAINT_NOT;
f86702cc
PP
2651
2652 /* Stack values are safe: */
2f450c1b 2653 CX_LEAVE_SCOPE(cx);
d1b6bf72 2654 cx_poploop(cx); /* release loop vars ... */
ed8ff0f3 2655 cx_popblock(cx);
5da525e9
DM
2656 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2657 CX_POP(cx);
a0d0e21e 2658
5da525e9 2659 return nextop;
a0d0e21e
LW
2660}
2661
2662PP(pp_next)
2663{
eb578fdb 2664 PERL_CONTEXT *cx;
a0d0e21e 2665
cd97dc8d
DM
2666 /* if not a bare 'next' in the main scope, search for it */
2667 cx = CX_CUR();
2668 if (!((PL_op->op_flags & OPf_SPECIAL) && CxTYPE_is_LOOP(cx)))
2669 cx = S_unwind_loop(aTHX);
a0d0e21e 2670
ed8ff0f3 2671 cx_topblock(cx);
3a1b2b9e 2672 PL_curcop = cx->blk_oldcop;
47c9d59f 2673 PERL_ASYNC_CHECK();
d57ce4df 2674 return (cx)->blk_loop.my_op->op_nextop;
a0d0e21e
LW
2675}
2676
2677PP(pp_redo)
2678{
42b5eca0 2679 PERL_CONTEXT *cx = S_unwind_loop(aTHX);
dc7f00ca 2680 OP* redo_op = cx->blk_loop.my_op->op_redoop;
a0d0e21e 2681
a034e688 2682 if (redo_op->op_type == OP_ENTER) {
1604cfb0
MS
2683 /* pop one less context to avoid $x being freed in while (my $x..) */
2684 cxstack_ix++;
dc7f00ca 2685 cx = CX_CUR();
1604cfb0
MS
2686 assert(CxTYPE(cx) == CXt_BLOCK);
2687 redo_op = redo_op->op_next;
a034e688
DM
2688 }
2689
936c78b5 2690 FREETMPS;
ef588991 2691 CX_LEAVE_SCOPE(cx);
ed8ff0f3 2692 cx_topblock(cx);
3a1b2b9e 2693 PL_curcop = cx->blk_oldcop;
47c9d59f 2694 PERL_ASYNC_CHECK();
a034e688 2695 return redo_op;
a0d0e21e
LW
2696}
2697
6d90e983 2698#define UNENTERABLE (OP *)1
4bfb5532 2699#define GOTO_DEPTH 64
6d90e983 2700
0824fdcb 2701STATIC OP *
5db1eb8d 2702S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit)
a0d0e21e 2703{
a0d0e21e 2704 OP **ops = opstack;
a1894d81 2705 static const char* const too_deep = "Target of goto is too deeply nested";
a0d0e21e 2706
7918f24d
NC
2707 PERL_ARGS_ASSERT_DOFINDLABEL;
2708
fc36a67e 2709 if (ops >= oplimit)
1604cfb0 2710 Perl_croak(aTHX_ "%s", too_deep);
11343788 2711 if (o->op_type == OP_LEAVE ||
1604cfb0
MS
2712 o->op_type == OP_SCOPE ||
2713 o->op_type == OP_LEAVELOOP ||
2714 o->op_type == OP_LEAVESUB ||
2715 o->op_type == OP_LEAVETRY ||
2716 o->op_type == OP_LEAVEGIVEN)
fc36a67e 2717 {
1604cfb0 2718 *ops++ = cUNOPo->op_first;
fc36a67e 2719 }
4bfb5532
FC
2720 else if (oplimit - opstack < GOTO_DEPTH) {
2721 if (o->op_flags & OPf_KIDS
1604cfb0
MS
2722 && cUNOPo->op_first->op_type == OP_PUSHMARK) {
2723 *ops++ = UNENTERABLE;
4bfb5532
FC
2724 }
2725 else if (o->op_flags & OPf_KIDS && PL_opargs[o->op_type]
1604cfb0
MS
2726 && OP_CLASS(o) != OA_LOGOP
2727 && o->op_type != OP_LINESEQ
2728 && o->op_type != OP_SREFGEN
2729 && o->op_type != OP_ENTEREVAL
2730 && o->op_type != OP_GLOB
2731 && o->op_type != OP_RV2CV) {
2732 OP * const kid = cUNOPo->op_first;
2733 if (OP_GIMME(kid, 0) != G_SCALAR || OpHAS_SIBLING(kid))
2734 *ops++ = UNENTERABLE;
4bfb5532 2735 }
6d90e983
FC
2736 }
2737 if (ops >= oplimit)
1604cfb0 2738 Perl_croak(aTHX_ "%s", too_deep);
c4aa4e48 2739 *ops = 0;
11343788 2740 if (o->op_flags & OPf_KIDS) {
1604cfb0
MS
2741 OP *kid;
2742 OP * const kid1 = cUNOPo->op_first;
2743 /* First try all the kids at this level, since that's likeliest. */
2744 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2745 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
5db1eb8d
BF
2746 STRLEN kid_label_len;
2747 U32 kid_label_flags;
1604cfb0 2748 const char *kid_label = CopLABEL_len_flags(kCOP,
5db1eb8d 2749 &kid_label_len, &kid_label_flags);
1604cfb0 2750 if (kid_label && (
5db1eb8d
BF
2751 ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
2752 (flags & SVf_UTF8)
2753 ? (bytes_cmp_utf8(
2754 (const U8*)kid_label, kid_label_len,
2755 (const U8*)label, len) == 0)
2756 : (bytes_cmp_utf8(
2757 (const U8*)label, len,
2758 (const U8*)kid_label, kid_label_len) == 0)
eade7155
BF
2759 : ( len == kid_label_len && ((kid_label == label)
2760 || memEQ(kid_label, label, len)))))
1604cfb0
MS
2761 return kid;
2762 }
2763 }
2764 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2765 bool first_kid_of_binary = FALSE;
2766 if (kid == PL_lastgotoprobe)
2767 continue;
2768 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2769 if (ops == opstack)
2770 *ops++ = kid;
2771 else if (ops[-1] != UNENTERABLE
2772 && (ops[-1]->op_type == OP_NEXTSTATE ||
2773 ops[-1]->op_type == OP_DBSTATE))
2774 ops[-1] = kid;
2775 else
2776 *ops++ = kid;
2777 }
2778 if (kid == kid1 && ops != opstack && ops[-1] == UNENTERABLE) {
2779 first_kid_of_binary = TRUE;
2780 ops--;
2781 }
2782 if ((o = dofindlabel(kid, label, len, flags, ops, oplimit)))
2783 return o;
2784 if (first_kid_of_binary)
2785 *ops++ = UNENTERABLE;
2786 }
a0d0e21e 2787 }
c4aa4e48 2788 *ops = 0;
a0d0e21e
LW
2789 return 0;
2790}
2791
b1c05ba5 2792
b5377742
FC
2793static void
2794S_check_op_type(pTHX_ OP * const o)
2795{
2796 /* Eventually we may want to stack the needed arguments
2797 * for each op. For now, we punt on the hard ones. */
2798 /* XXX This comment seems to me like wishful thinking. --sprout */
6d90e983 2799 if (o == UNENTERABLE)
1604cfb0 2800 Perl_croak(aTHX_
6d90e983 2801 "Can't \"goto\" into a binary or list expression");
b5377742
FC
2802 if (o->op_type == OP_ENTERITER)
2803 Perl_croak(aTHX_
2804 "Can't \"goto\" into the middle of a foreach loop");
a01f4640
FC
2805 if (o->op_type == OP_ENTERGIVEN)
2806 Perl_croak(aTHX_
2807 "Can't \"goto\" into a \"given\" block");
b5377742
FC
2808}
2809
b1c05ba5
DM
2810/* also used for: pp_dump() */
2811
2812PP(pp_goto)
a0d0e21e 2813{
c91f661c 2814 dSP;
cbbf8932 2815 OP *retop = NULL;
a0d0e21e 2816 I32 ix;
eb578fdb 2817 PERL_CONTEXT *cx;
fc36a67e 2818 OP *enterops[GOTO_DEPTH];
cbbf8932 2819 const char *label = NULL;
5db1eb8d
BF
2820 STRLEN label_len = 0;
2821 U32 label_flags = 0;
bfed75c6 2822 const bool do_dump = (PL_op->op_type == OP_DUMP);
a1894d81 2823 static const char* const must_have_label = "goto must have label";
a0d0e21e 2824
533c011a 2825 if (PL_op->op_flags & OPf_STACKED) {
7d1d69cb
DM
2826 /* goto EXPR or goto &foo */
2827
1604cfb0
MS
2828 SV * const sv = POPs;
2829 SvGETMAGIC(sv);
a0d0e21e 2830
1604cfb0 2831 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
0fa3d31d 2832 /* This egregious kludge implements goto &subroutine */
1604cfb0
MS
2833 I32 cxix;
2834 PERL_CONTEXT *cx;
2835 CV *cv = MUTABLE_CV(SvRV(sv));
2836 AV *arg = GvAV(PL_defgv);
2837
2838 while (!CvROOT(cv) && !CvXSUB(cv)) {
2839 const GV * const gv = CvGV(cv);
2840 if (gv) {
2841 GV *autogv;
2842 SV *tmpstr;
2843 /* autoloaded stub? */
2844 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2845 continue;
2846 autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv),
2847 GvNAMELEN(gv),
c271df94 2848 GvNAMEUTF8(gv) ? SVf_UTF8 : 0);
1604cfb0
MS
2849 if (autogv && (cv = GvCV(autogv)))
2850 continue;
2851 tmpstr = sv_newmortal();
2852 gv_efullname3(tmpstr, gv, NULL);
2853 DIE(aTHX_ "Goto undefined subroutine &%" SVf, SVfARG(tmpstr));
2854 }
2855 DIE(aTHX_ "Goto undefined subroutine");
2856 }
2857
2858 cxix = dopopto_cursub();
d338c0c2
DM
2859 if (cxix < 0) {
2860 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
8da3792e 2861 }
d338c0c2 2862 cx = &cxstack[cxix];
1604cfb0
MS
2863 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2864 if (CxTYPE(cx) == CXt_EVAL) {
2865 if (CxREALEVAL(cx))
2866 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2867 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2868 else
2869 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2870 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2871 }
2872 else if (CxMULTICALL(cx))
2873 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2874
2875 /* First do some returnish stuff. */
2876
2877 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2878 FREETMPS;
2879 if (cxix < cxstack_ix) {
2880 dounwind(cxix);
d338c0c2 2881 }
7e637ba4 2882 cx = CX_CUR();
1604cfb0
MS
2883 cx_topblock(cx);
2884 SPAGAIN;
39de75fd 2885
8ae997c5
DM
2886 /* protect @_ during save stack unwind. */
2887 if (arg)
2888 SvREFCNT_inc_NN(sv_2mortal(MUTABLE_SV(arg)));
2889
1604cfb0 2890 assert(PL_scopestack_ix == cx->blk_oldscopesp);
dfe0f39b 2891 CX_LEAVE_SCOPE(cx);
8ae997c5 2892
1604cfb0 2893 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
a73d8813 2894 /* this is part of cx_popsub_args() */
1604cfb0 2895 AV* av = MUTABLE_AV(PAD_SVl(0));
e2657e18
DM
2896 assert(AvARRAY(MUTABLE_AV(
2897 PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[
2898 CvDEPTH(cx->blk_sub.cv)])) == PL_curpad);
2899
f72bdec3
DM
2900 /* we are going to donate the current @_ from the old sub
2901 * to the new sub. This first part of the donation puts a
2902 * new empty AV in the pad[0] slot of the old sub,
2903 * unless pad[0] and @_ differ (e.g. if the old sub did
2904 * local *_ = []); in which case clear the old pad[0]
2905 * array in the usual way */
1604cfb0 2906 if (av == arg || AvREAL(av))
95b2f486 2907 clear_defarray(av, av == arg);
1604cfb0
MS
2908 else CLEAR_ARGARRAY(av);
2909 }
88c11d84 2910
b1e25d05
DM
2911 /* don't restore PL_comppad here. It won't be needed if the
2912 * sub we're going to is non-XS, but restoring it early then
2913 * croaking (e.g. the "Goto undefined subroutine" below)
2914 * means the CX block gets processed again in dounwind,
2915 * but this time with the wrong PL_comppad */
88c11d84 2916
1604cfb0
MS
2917 /* A destructor called during LEAVE_SCOPE could have undefined
2918 * our precious cv. See bug #99850. */
2919 if (!CvROOT(cv) && !CvXSUB(cv)) {
2920 const GV * const gv = CvGV(cv);
2921 if (gv) {
2922 SV * const tmpstr = sv_newmortal();
2923 gv_efullname3(tmpstr, gv, NULL);
2924 DIE(aTHX_ "Goto undefined subroutine &%" SVf,
2925 SVfARG(tmpstr));
2926 }
2927 DIE(aTHX_ "Goto undefined subroutine");
2928 }
2929
2930 if (CxTYPE(cx) == CXt_SUB) {
2931 CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth;
cd17cc2e
DM
2932 SvREFCNT_dec_NN(cx->blk_sub.cv);
2933 }
2934
1604cfb0
MS
2935 /* Now do some callish stuff. */
2936 if (CvISXSUB(cv)) {
2937 const SSize_t items = arg ? AvFILL(arg) + 1 : 0;
2938 const bool m = arg ? cBOOL(SvRMAGICAL(arg)) : 0;
2939 SV** mark;
049bd5ff 2940
8ae997c5 2941 ENTER;
80774f05
DM
2942 SAVETMPS;
2943 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2944
1604cfb0
MS
2945 /* put GvAV(defgv) back onto stack */
2946 if (items) {
2947 EXTEND(SP, items+1); /* @_ could have been extended. */
2948 }
2949 mark = SP;
2950 if (items) {
2951 SSize_t index;
2952 bool r = cBOOL(AvREAL(arg));
2953 for (index=0; index<items; index++)
2954 {
2955 SV *sv;
2956 if (m) {
2957 SV ** const svp = av_fetch(arg, index, 0);
2958 sv = svp ? *svp : NULL;
2959 }
2960 else sv = AvARRAY(arg)[index];
2961 SP[index+1] = sv
2962 ? r ? SvREFCNT_inc_NN(sv_2mortal(sv)) : sv
2963 : sv_2mortal(newSVavdefelem(arg, index, 1));
2964 }
2965 }
2966 SP += items;
2967 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2968 /* Restore old @_ */
b405d38b 2969 CX_POP_SAVEARRAY(cx);
1604cfb0 2970 }
1fa4e549 2971
1604cfb0 2972 retop = cx->blk_sub.retop;
b1e25d05
DM
2973 PL_comppad = cx->blk_sub.prevcomppad;
2974 PL_curpad = LIKELY(PL_comppad) ? AvARRAY(PL_comppad) : NULL;
8ae997c5 2975
1604cfb0 2976 /* XS subs don't have a CXt_SUB, so pop it;
ed8ff0f3
DM
2977 * this is a cx_popblock(), less all the stuff we already did
2978 * for cx_topblock() earlier */
8ae997c5 2979 PL_curcop = cx->blk_oldcop;
843fe1ca
DM
2980 /* this is cx_popsub, less all the stuff we already did */
2981 PL_curstackinfo->si_cxsubix = cx->blk_sub.old_cxsubix;
2982
5da525e9 2983 CX_POP(cx);
8ae997c5 2984
1604cfb0
MS
2985 /* Push a mark for the start of arglist */
2986 PUSHMARK(mark);
2987 PUTBACK;
2988 (void)(*CvXSUB(cv))(aTHX_ cv);
2989 LEAVE;
2990 goto _return;
2991 }
2992 else {
2993 PADLIST * const padlist = CvPADLIST(cv);
39de75fd 2994
80774f05
DM
2995 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2996
a73d8813 2997 /* partial unrolled cx_pushsub(): */
39de75fd 2998
1604cfb0
MS
2999 cx->blk_sub.cv = cv;
3000 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 3001
1604cfb0 3002 CvDEPTH(cv)++;
2c50b7ed 3003 SvREFCNT_inc_simple_void_NN(cv);
1604cfb0
MS
3004 if (CvDEPTH(cv) > 1) {
3005 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
3006 sub_crush_depth(cv);
3007 pad_push(padlist, CvDEPTH(cv));
3008 }
3009 PL_curcop = cx->blk_oldcop;
3010 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
3011 if (CxHASARGS(cx))
3012 {
f72bdec3
DM
3013 /* second half of donating @_ from the old sub to the
3014 * new sub: abandon the original pad[0] AV in the
3015 * new sub, and replace it with the donated @_.
3016 * pad[0] takes ownership of the extra refcount
3017 * we gave arg earlier */
1604cfb0
MS
3018 if (arg) {
3019 SvREFCNT_dec(PAD_SVl(0));
3020 PAD_SVl(0) = (SV *)arg;
13122036 3021 SvREFCNT_inc_simple_void_NN(arg);
1604cfb0
MS
3022 }
3023
3024 /* GvAV(PL_defgv) might have been modified on scope
3025 exit, so point it at arg again. */
3026 if (arg != GvAV(PL_defgv)) {
3027 AV * const av = GvAV(PL_defgv);
3028 GvAV(PL_defgv) = (AV *)SvREFCNT_inc_simple(arg);
3029 SvREFCNT_dec(av);
3030 }
3031 }
3032
3033 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
3034 Perl_get_db_sub(aTHX_ NULL, cv);
3035 if (PERLDB_GOTO) {
3036 CV * const gotocv = get_cvs("DB::goto", 0);
3037 if (gotocv) {
3038 PUSHMARK( PL_stack_sp );
3039 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
3040 PL_stack_sp--;
3041 }
3042 }
3043 }
3044 retop = CvSTART(cv);
3045 goto putback_return;
3046 }
3047 }
3048 else {
7d1d69cb 3049 /* goto EXPR */
1604cfb0 3050 label = SvPV_nomg_const(sv, label_len);
5db1eb8d 3051 label_flags = SvUTF8(sv);
1604cfb0 3052 }
a0d0e21e 3053 }
2fc690dc 3054 else if (!(PL_op->op_flags & OPf_SPECIAL)) {
7d1d69cb 3055 /* goto LABEL or dump LABEL */
1604cfb0 3056 label = cPVOP->op_pv;
5db1eb8d
BF
3057 label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
3058 label_len = strlen(label);
3059 }
0157ef98 3060 if (!(do_dump || label_len)) DIE(aTHX_ "%s", must_have_label);
a0d0e21e 3061
f410a211
NC
3062 PERL_ASYNC_CHECK();
3063
3532f34a 3064 if (label_len) {
1604cfb0
MS
3065 OP *gotoprobe = NULL;
3066 bool leaving_eval = FALSE;
3067 bool in_block = FALSE;
3068 bool pseudo_block = FALSE;
3069 PERL_CONTEXT *last_eval_cx = NULL;
3070
3071 /* find label */
3072
3073 PL_lastgotoprobe = NULL;
3074 *enterops = 0;
3075 for (ix = cxstack_ix; ix >= 0; ix--) {
3076 cx = &cxstack[ix];
3077 switch (CxTYPE(cx)) {
3078 case CXt_EVAL:
3079 leaving_eval = TRUE;
99dbf645 3080 if (!CxEVALBLOCK(cx)) {
1604cfb0
MS
3081 gotoprobe = (last_eval_cx ?
3082 last_eval_cx->blk_eval.old_eval_root :
3083 PL_eval_root);
3084 last_eval_cx = cx;
3085 break;
9c5794fe
RH
3086 }
3087 /* else fall through */
93661e56
DM
3088 case CXt_LOOP_PLAIN:
3089 case CXt_LOOP_LAZYIV:
3090 case CXt_LOOP_LAZYSV:
3091 case CXt_LOOP_LIST:
3092 case CXt_LOOP_ARY:
1604cfb0
MS
3093 case CXt_GIVEN:
3094 case CXt_WHEN:
3095 gotoprobe = OpSIBLING(cx->blk_oldcop);
3096 break;
3097 case CXt_SUBST:
3098 continue;
3099 case CXt_BLOCK:
3100 if (ix) {
3101 gotoprobe = OpSIBLING(cx->blk_oldcop);
3102 in_block = TRUE;
3103 } else
3104 gotoprobe = PL_main_root;
3105 break;
3106 case CXt_SUB:
3107 gotoprobe = CvROOT(cx->blk_sub.cv);
3108 pseudo_block = cBOOL(CxMULTICALL(cx));
3109 break;
3110 case CXt_FORMAT:
3111 case CXt_NULL:
3112 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
3113 default:
3114 if (ix)
3115 DIE(aTHX_ "panic: goto, type=%u, ix=%ld",
3116 CxTYPE(cx), (long) ix);
3117 gotoprobe = PL_main_root;
3118 break;
3119 }
3120 if (gotoprobe) {
29e61fd9
DM
3121 OP *sibl1, *sibl2;
3122
1604cfb0
MS
3123 retop = dofindlabel(gotoprobe, label, label_len, label_flags,
3124 enterops, enterops + GOTO_DEPTH);
3125 if (retop)
3126 break;
3127 if ( (sibl1 = OpSIBLING(gotoprobe)) &&
3128 sibl1->op_type == OP_UNSTACK &&
3129 (sibl2 = OpSIBLING(sibl1)))
29e61fd9 3130 {
1604cfb0
MS
3131 retop = dofindlabel(sibl2,
3132 label, label_len, label_flags, enterops,
3133 enterops + GOTO_DEPTH);
3134 if (retop)
3135 break;
3136 }
3137 }
3138 if (pseudo_block)
3139 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
3140 PL_lastgotoprobe = gotoprobe;
3141 }
3142 if (!retop)
3143 DIE(aTHX_ "Can't find label %" UTF8f,
3144 UTF8fARG(label_flags, label_len, label));
3145
3146 /* if we're leaving an eval, check before we pop any frames
3b2447bc 3147 that we're not going to punt, otherwise the error
1604cfb0 3148 won't be caught */
3b2447bc 3149
1604cfb0
MS
3150 if (leaving_eval && *enterops && enterops[1]) {
3151 I32 i;
3b2447bc 3152 for (i = 1; enterops[i]; i++)
b5377742 3153 S_check_op_type(aTHX_ enterops[i]);
1604cfb0
MS
3154 }
3155
3156 if (*enterops && enterops[1]) {
3157 I32 i = enterops[1] != UNENTERABLE
3158 && enterops[1]->op_type == OP_ENTER && in_block
3159 ? 2
3160 : 1;
3161 if (enterops[i])
3162 deprecate("\"goto\" to jump into a construct");
3163 }
3164
3165 /* pop unwanted frames */
3166
3167 if (ix < cxstack_ix) {
3168 if (ix < 0)
3169 DIE(aTHX_ "panic: docatch: illegal ix=%ld", (long)ix);
3170 dounwind(ix);
7e637ba4 3171 cx = CX_CUR();
1604cfb0
MS
3172 cx_topblock(cx);
3173 }
3174
3175 /* push wanted frames */
3176
3177 if (*enterops && enterops[1]) {
3178 OP * const oldop = PL_op;
3179 ix = enterops[1] != UNENTERABLE
3180 && enterops[1]->op_type == OP_ENTER && in_block
3181 ? 2
3182 : 1;
3183 for (; enterops[ix]; ix++) {
3184 PL_op = enterops[ix];
3185 S_check_op_type(aTHX_ PL_op);
3186 DEBUG_l( Perl_deb(aTHX_ "pp_goto: Entering %s\n",
3187 OP_NAME(PL_op)));
3188 PL_op->op_ppaddr(aTHX);
3189 }
3190 PL_op = oldop;
3191 }
a0d0e21e
LW
3192 }
3193
2631bbca 3194 if (do_dump) {
a5f75d66 3195#ifdef VMS
1604cfb0 3196 if (!retop) retop = PL_main_start;
a5f75d66 3197#endif
1604cfb0
MS
3198 PL_restartop = retop;
3199 PL_do_undump = TRUE;
a0d0e21e 3200
1604cfb0 3201 my_unexec();
a0d0e21e 3202
1604cfb0
MS
3203 PL_restartop = 0; /* hmm, must be GNU unexec().. */
3204 PL_do_undump = FALSE;
a0d0e21e
LW
3205 }
3206
51eb35b5
DD
3207 putback_return:
3208 PL_stack_sp = sp;
3209 _return:
47c9d59f 3210 PERL_ASYNC_CHECK();
51eb35b5 3211 return retop;
a0d0e21e
LW
3212}
3213
3214PP(pp_exit)
3215{
39644a26 3216 dSP;
a0d0e21e
LW
3217 I32 anum;
3218
3219 if (MAXARG < 1)
1604cfb0 3220 anum = 0;
9d3c658e 3221 else if (!TOPs) {
1604cfb0 3222 anum = 0; (void)POPs;
9d3c658e 3223 }
ff0cee69 3224 else {
1604cfb0 3225 anum = SvIVx(POPs);
d98f61e7 3226#ifdef VMS
1604cfb0
MS
3227 if (anum == 1
3228 && SvTRUE(cop_hints_fetch_pvs(PL_curcop, "vmsish_exit", 0)))
3229 anum = 0;
97124ef6
FC
3230 VMSISH_HUSHED =
3231 VMSISH_HUSHED || (PL_curcop->op_private & OPpHUSH_VMSISH);
ff0cee69
PP
3232#endif
3233 }
cc3604b1 3234 PL_exit_flags |= PERL_EXIT_EXPECTED;
a0d0e21e 3235 my_exit(anum);
3280af22 3236 PUSHs(&PL_sv_undef);
a0d0e21e
LW
3237 RETURN;
3238}
3239
a0d0e21e
LW
3240/* Eval. */
3241
0824fdcb 3242STATIC void
cea2e8a9 3243S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 3244{
504618e9 3245 const char *s = SvPVX_const(sv);
890ce7af 3246 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 3247 I32 line = 1;
a0d0e21e 3248
7918f24d
NC
3249 PERL_ARGS_ASSERT_SAVE_LINES;
3250
a0d0e21e 3251 while (s && s < send) {
1604cfb0
MS
3252 const char *t;
3253 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 3254
1604cfb0
MS
3255 t = (const char *)memchr(s, '\n', send - s);
3256 if (t)
3257 t++;
3258 else
3259 t = send;
a0d0e21e 3260
1604cfb0
MS
3261 sv_setpvn(tmpstr, s, t - s);
3262 av_store(array, line++, tmpstr);
3263 s = t;
a0d0e21e
LW
3264 }
3265}
3266
22f16304
RU
3267/*
3268=for apidoc docatch
3269
3270Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3271
32720 is used as continue inside eval,
3273
32743 is used for a die caught by an inner eval - continue inner loop
3275
75af9d73 3276See F<cop.h>: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
22f16304
RU
3277establish a local jmpenv to handle exception traps.
3278
3279=cut
3280*/
0824fdcb 3281STATIC OP *
d7e3f70f 3282S_docatch(pTHX_ Perl_ppaddr_t firstpp)
1e422769 3283{
6224f72b 3284 int ret;
06b5626a 3285 OP * const oldop = PL_op;
db36c5a1 3286 dJMPENV;
1e422769 3287
54310121 3288 assert(CATCH_GET == TRUE);
8bffa5f8 3289
14dd3ad8 3290 JMPENV_PUSH(ret);
6224f72b 3291 switch (ret) {
312caa8e 3292 case 0:
1604cfb0 3293 PL_op = firstpp(aTHX);
14dd3ad8 3294 redo_body:
1604cfb0
MS
3295 CALLRUNOPS(aTHX);
3296 break;
312caa8e 3297 case 3:
1604cfb0
MS
3298 /* die caught by an inner eval - continue inner loop */
3299 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3300 PL_restartjmpenv = NULL;
3301 PL_op = PL_restartop;
3302 PL_restartop = 0;
3303 goto redo_body;
3304 }
3305 /* FALLTHROUGH */
312caa8e 3306 default:
1604cfb0
MS
3307 JMPENV_POP;
3308 PL_op = oldop;
3309 JMPENV_JUMP(ret);
3310 NOT_REACHED; /* NOTREACHED */
1e422769 3311 }
14dd3ad8 3312 JMPENV_POP;
533c011a 3313 PL_op = oldop;
5f66b61c 3314 return NULL;
1e422769
PP
3315}
3316
a3985cdc
DM
3317
3318/*
3319=for apidoc find_runcv
3320
3321Locate the CV corresponding to the currently executing sub or eval.
796b6530
KW
3322If C<db_seqp> is non_null, skip CVs that are in the DB package and populate
3323C<*db_seqp> with the cop sequence number at the point that the DB:: code was
72d33970
FC
3324entered. (This allows debuggers to eval in the scope of the breakpoint
3325rather than in the scope of the debugger itself.)
a3985cdc
DM
3326
3327=cut
3328*/
3329
3330CV*
d819b83a 3331Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 3332{
db4cf31d 3333 return Perl_find_runcv_where(aTHX_ 0, 0, db_seqp);
70794f7b
FC
3334}
3335
3336/* If this becomes part of the API, it might need a better name. */
3337CV *
db4cf31d 3338Perl_find_runcv_where(pTHX_ U8 cond, IV arg, U32 *db_seqp)
70794f7b 3339{
a3985cdc 3340 PERL_SI *si;
b4b0692a 3341 int level = 0;
a3985cdc 3342
d819b83a 3343 if (db_seqp)
1604cfb0 3344 *db_seqp =
c3923c33
DM
3345 PL_curcop == &PL_compiling
3346 ? PL_cop_seqmax
3347 : PL_curcop->cop_seq;
3348
a3985cdc 3349 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 3350 I32 ix;
1604cfb0
MS
3351 for (ix = si->si_cxix; ix >= 0; ix--) {
3352 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3353 CV *cv = NULL;
3354 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3355 cv = cx->blk_sub.cv;
3356 /* skip DB:: code */
3357 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3358 *db_seqp = cx->blk_oldcop->cop_seq;
3359 continue;
3360 }
a453e28a
DM
3361 if (cx->cx_type & CXp_SUB_RE)
3362 continue;
1604cfb0 3363 }
99dbf645 3364 else if (CxTYPE(cx) == CXt_EVAL && !CxEVALBLOCK(cx))
1604cfb0
MS
3365 cv = cx->blk_eval.cv;
3366 if (cv) {
3367 switch (cond) {
3368 case FIND_RUNCV_padid_eq:
3369 if (!CvPADLIST(cv)
3370 || CvPADLIST(cv)->xpadl_id != (U32)arg)
3371 continue;
3372 return cv;
3373 case FIND_RUNCV_level_eq:
3374 if (level++ != arg) continue;
2165bd23 3375 /* FALLTHROUGH */
1604cfb0
MS
3376 default:
3377 return cv;
3378 }
3379 }
3380 }
a3985cdc 3381 }
db4cf31d 3382 return cond == FIND_RUNCV_padid_eq ? NULL : PL_main_cv;
a3985cdc
DM
3383}
3384
3385
27e90453
DM
3386/* Run yyparse() in a setjmp wrapper. Returns:
3387 * 0: yyparse() successful
3388 * 1: yyparse() failed
3389 * 3: yyparse() died
3390 */
3391STATIC int
28ac2b49 3392S_try_yyparse(pTHX_ int gramtype)
27e90453
DM
3393{
3394 int ret;
3395 dJMPENV;
3396
4ebe6e95 3397 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
27e90453
DM
3398 JMPENV_PUSH(ret);
3399 switch (ret) {
3400 case 0:
1604cfb0
MS
3401 ret = yyparse(gramtype) ? 1 : 0;
3402 break;
27e90453 3403 case 3:
1604cfb0 3404 break;
27e90453 3405 default:
1604cfb0
MS
3406 JMPENV_POP;
3407 JMPENV_JUMP(ret);
3408 NOT_REACHED; /* NOTREACHED */
27e90453
DM
3409 }
3410 JMPENV_POP;
3411 return ret;
3412}
3413
3414
104a8185
DM
3415/* Compile a require/do or an eval ''.
3416 *
a3985cdc 3417 * outside is the lexically enclosing CV (if any) that invoked us.
104a8185
DM
3418 * seq is the current COP scope value.
3419 * hh is the saved hints hash, if any.
3420 *
410be5db 3421 * Returns a bool indicating whether the compile was successful; if so,
104a8185
DM
3422 * PL_eval_start contains the first op of the compiled code; otherwise,
3423 * pushes undef.
3424 *
3425 * This function is called from two places: pp_require and pp_entereval.
3426 * These can be distinguished by whether PL_op is entereval.
7d116edc
FC
3427 */
3428
410be5db 3429STATIC bool
1c23e2bd 3430S_doeval_compile(pTHX_ U8 gimme, CV* outside, U32 seq, HV *hh)
a0d0e21e 3431{
20b7effb 3432 dSP;
46c461b5 3433 OP * const saveop = PL_op;
104a8185 3434 bool clear_hints = saveop->op_type != OP_ENTEREVAL;
f45b078d 3435 COP * const oldcurcop = PL_curcop;
26c9400e 3436 bool in_require = (saveop->op_type == OP_REQUIRE);
27e90453 3437 int yystatus;
676a678a 3438 CV *evalcv;
a0d0e21e 3439
27e90453 3440 PL_in_eval = (in_require
1604cfb0
MS
3441 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3442 : (EVAL_INEVAL |
a1941760
DM
3443 ((PL_op->op_private & OPpEVAL_RE_REPARSING)
3444 ? EVAL_RE_REPARSING : 0)));
a0d0e21e 3445
1ce6579f
PP
3446 PUSHMARK(SP);
3447
676a678a
Z
3448 evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3449 CvEVAL_on(evalcv);
4ebe6e95
DM
3450 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
3451 CX_CUR()->blk_eval.cv = evalcv;
3452 CX_CUR()->blk_gimme = gimme;
2090ab20 3453
676a678a
Z
3454 CvOUTSIDE_SEQ(evalcv) = seq;
3455 CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 3456
dd2155a4 3457 /* set up a scratch pad */
a0d0e21e 3458
eacbb379 3459 CvPADLIST_set(evalcv, pad_new(padnew_SAVE));
cecbe010 3460 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2c05e328 3461
07055b4c 3462
b5bbe64a 3463 SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */
748a9306 3464
a0d0e21e
LW
3465 /* make sure we compile in the right package */
3466
ed094faf 3467 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
1604cfb0
MS
3468 SAVEGENERICSV(PL_curstash);
3469 PL_curstash = (HV *)CopSTASH(PL_curcop);
3470 if (SvTYPE(PL_curstash) != SVt_PVHV) PL_curstash = NULL;
3471 else {
3472 SvREFCNT_inc_simple_void(PL_curstash);
3473 save_item(PL_curstname);
3474 sv_sethek(PL_curstname, HvNAME_HEK(PL_curstash));
3475 }
a0d0e21e 3476 }
3c10abe3 3477 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3280af22
NIS
3478 SAVESPTR(PL_beginav);
3479 PL_beginav = newAV();
3480 SAVEFREESV(PL_beginav);
3c10abe3
AG
3481 SAVESPTR(PL_unitcheckav);
3482 PL_unitcheckav = newAV();
3483 SAVEFREESV(PL_unitcheckav);
a0d0e21e 3484
81d86705 3485
104a8185 3486 ENTER_with_name("evalcomp");
676a678a
Z
3487 SAVESPTR(PL_compcv);
3488 PL_compcv = evalcv;
3489
a0d0e21e
LW
3490 /* try to compile it */
3491
5f66b61c 3492 PL_eval_root = NULL;
3280af22 3493 PL_curcop = &PL_compiling;
26c9400e 3494 if ((saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
1604cfb0 3495 PL_in_eval |= EVAL_KEEPERR;
ab69dbc2 3496 else
1604cfb0 3497 CLEAR_ERRSV();
27e90453 3498
377b5421
DM
3499 SAVEHINTS();
3500 if (clear_hints) {
1604cfb0
MS
3501 PL_hints = HINTS_DEFAULT;
3502 hv_clear(GvHV(PL_hintgv));
9f601cf3 3503 CLEARFEATUREBITS();
377b5421
DM
3504 }
3505 else {
1604cfb0
MS
3506 PL_hints = saveop->op_private & OPpEVAL_COPHH
3507 ? oldcurcop->cop_hints : (U32)saveop->op_targ;
4f3e2518
DM
3508
3509 /* making 'use re eval' not be in scope when compiling the
3510 * qr/mabye_has_runtime_code_block/ ensures that we don't get
3511 * infinite recursion when S_has_runtime_code() gives a false
3512 * positive: the second time round, HINT_RE_EVAL isn't set so we
3513 * don't bother calling S_has_runtime_code() */
3514 if (PL_in_eval & EVAL_RE_REPARSING)
3515 PL_hints &= ~HINT_RE_EVAL;
3516
1604cfb0
MS
3517 if (hh) {
3518 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3519 SvREFCNT_dec(GvHV(PL_hintgv));
3520 GvHV(PL_hintgv) = hh;
9f601cf3 3521 FETCHFEATUREBITSHH(hh);
1604cfb0 3522 }
377b5421
DM
3523 }
3524 SAVECOMPILEWARNINGS();
3525 if (clear_hints) {
1604cfb0
MS
3526 if (PL_dowarn & G_WARN_ALL_ON)
3527 PL_compiling.cop_warnings = pWARN_ALL ;
3528 else if (PL_dowarn & G_WARN_ALL_OFF)
3529 PL_compiling.cop_warnings = pWARN_NONE ;
3530 else
3531 PL_compiling.cop_warnings = pWARN_STD ;
377b5421
DM
3532 }
3533 else {
1604cfb0
MS
3534 PL_compiling.cop_warnings =
3535 DUP_WARNINGS(oldcurcop->cop_warnings);
3536 cophh_free(CopHINTHASH_get(&PL_compiling));
3537 if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) {
3538 /* The label, if present, is the first entry on the chain. So rather
3539 than writing a blank label in front of it (which involves an
3540 allocation), just use the next entry in the chain. */
3541 PL_compiling.cop_hints_hash
3542 = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next);
3543 /* Check the assumption that this removed the label. */
3544 assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3545 }
3546 else
3547 PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash);
377b5421 3548 }
f45b078d 3549
a88d97bf 3550 CALL_BLOCK_HOOKS(bhk_eval, saveop);
52db365a 3551
27e90453
DM
3552 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3553 * so honour CATCH_GET and trap it here if necessary */
3554
fc69996c
DM
3555
3556 /* compile the code */
28ac2b49 3557 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
27e90453
DM
3558
3559 if (yystatus || PL_parser->error_count || !PL_eval_root) {
1604cfb0 3560 PERL_CONTEXT *cx;
d308a779 3561 SV *errsv;
bfed75c6 3562
1604cfb0
MS
3563 PL_op = saveop;
3564 /* note that if yystatus == 3, then the require/eval died during
fc69996c
DM
3565 * compilation, so the EVAL CX block has already been popped, and
3566 * various vars restored */
1604cfb0
MS
3567 if (yystatus != 3) {
3568 if (PL_eval_root) {
3569 op_free(PL_eval_root);
3570 PL_eval_root = NULL;
3571 }
3572 SP = PL_stack_base + POPMARK; /* pop original mark */
4ebe6e95 3573 cx = CX_CUR();
06a7bc17
DM
3574 assert(CxTYPE(cx) == CXt_EVAL);
3575 /* pop the CXt_EVAL, and if was a require, croak */
3576 S_pop_eval_context_maybe_croak(aTHX_ cx, ERRSV, 2);
1604cfb0 3577 }
d308a779 3578
03e81cd3
DM
3579 /* die_unwind() re-croaks when in require, having popped the
3580 * require EVAL context. So we should never catch a require
3581 * exception here */
1604cfb0 3582 assert(!in_require);
03e81cd3 3583
1604cfb0 3584 errsv = ERRSV;
d308a779
DM
3585 if (!*(SvPV_nolen_const(errsv)))
3586 sv_setpvs(errsv, "Compilation error");
3587
1604cfb0
MS
3588 if (gimme != G_ARRAY) PUSHs(&PL_sv_undef);
3589 PUTBACK;
3590 return FALSE;
a0d0e21e 3591 }
fc69996c
DM
3592
3593 /* Compilation successful. Now clean up */
3594
3595 LEAVE_with_name("evalcomp");
104a8185 3596
57843af0 3597 CopLINE_set(&PL_compiling, 0);
104a8185 3598 SAVEFREEOP(PL_eval_root);
8be227ab 3599 cv_forget_slab(evalcv);
0c58d367 3600
a0d0e21e
LW
3601 DEBUG_x(dump_eval());
3602
55497cff 3603 /* Register with debugger: */
26c9400e 3604 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
1604cfb0
MS
3605 CV * const cv = get_cvs("DB::postponed", 0);