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