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