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