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