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