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