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