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