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