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