Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* pp_hot.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 | /* | |
12 | * Then he heard Merry change the note, and up went the Horn-cry of Buckland, | |
13 | * shaking the air. | |
14 | * | |
4ac71550 TC |
15 | * Awake! Awake! Fear, Fire, Foes! Awake! |
16 | * Fire, Foes! Awake! | |
17 | * | |
18 | * [p.1007 of _The Lord of the Rings_, VI/viii: "The Scouring of the Shire"] | |
a0d0e21e LW |
19 | */ |
20 | ||
166f8a29 DM |
21 | /* This file contains 'hot' pp ("push/pop") functions that |
22 | * execute the opcodes that make up a perl program. A typical pp function | |
23 | * expects to find its arguments on the stack, and usually pushes its | |
24 | * results onto the stack, hence the 'pp' terminology. Each OP structure | |
25 | * contains a pointer to the relevant pp_foo() function. | |
26 | * | |
27 | * By 'hot', we mean common ops whose execution speed is critical. | |
28 | * By gathering them together into a single file, we encourage | |
29 | * CPU cache hits on hot code. Also it could be taken as a warning not to | |
30 | * change any code in this file unless you're sure it won't affect | |
31 | * performance. | |
32 | */ | |
33 | ||
a0d0e21e | 34 | #include "EXTERN.h" |
864dbfa3 | 35 | #define PERL_IN_PP_HOT_C |
a0d0e21e | 36 | #include "perl.h" |
e0be7821 | 37 | #include "regcomp.h" |
a0d0e21e LW |
38 | |
39 | /* Hot code. */ | |
40 | ||
41 | PP(pp_const) | |
42 | { | |
39644a26 | 43 | dSP; |
996c9baa | 44 | XPUSHs(cSVOP_sv); |
a0d0e21e LW |
45 | RETURN; |
46 | } | |
47 | ||
48 | PP(pp_nextstate) | |
49 | { | |
533c011a | 50 | PL_curcop = (COP*)PL_op; |
a0d0e21e | 51 | TAINT_NOT; /* Each statement is presumed innocent */ |
4ebe6e95 | 52 | PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp; |
a0d0e21e | 53 | FREETMPS; |
f410a211 | 54 | PERL_ASYNC_CHECK(); |
a0d0e21e LW |
55 | return NORMAL; |
56 | } | |
57 | ||
58 | PP(pp_gvsv) | |
59 | { | |
39644a26 | 60 | dSP; |
4b0ceb86 | 61 | assert(SvTYPE(cGVOP_gv) == SVt_PVGV); |
924508f0 | 62 | EXTEND(SP,1); |
5d9574c1 | 63 | if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) |
1604cfb0 | 64 | PUSHs(save_scalar(cGVOP_gv)); |
a0d0e21e | 65 | else |
1604cfb0 | 66 | PUSHs(GvSVn(cGVOP_gv)); |
a0d0e21e LW |
67 | RETURN; |
68 | } | |
69 | ||
b1c05ba5 DM |
70 | |
71 | /* also used for: pp_lineseq() pp_regcmaybe() pp_scalar() pp_scope() */ | |
72 | ||
a0d0e21e LW |
73 | PP(pp_null) |
74 | { | |
75 | return NORMAL; | |
76 | } | |
77 | ||
3dd9d4e4 FC |
78 | /* This is sometimes called directly by pp_coreargs, pp_grepstart and |
79 | amagic_call. */ | |
a0d0e21e LW |
80 | PP(pp_pushmark) |
81 | { | |
3280af22 | 82 | PUSHMARK(PL_stack_sp); |
a0d0e21e LW |
83 | return NORMAL; |
84 | } | |
85 | ||
86 | PP(pp_stringify) | |
87 | { | |
20b7effb | 88 | dSP; dTARGET; |
4cc783ef DD |
89 | SV * const sv = TOPs; |
90 | SETs(TARG); | |
91 | sv_copypv(TARG, sv); | |
92 | SvSETMAGIC(TARG); | |
93 | /* no PUTBACK, SETs doesn't inc/dec SP */ | |
94 | return NORMAL; | |
a0d0e21e LW |
95 | } |
96 | ||
97 | PP(pp_gv) | |
98 | { | |
20b7effb | 99 | dSP; |
4b0ceb86 PE |
100 | /* cGVOP_gv might be a real GV or might be an RV to a CV */ |
101 | assert(SvTYPE(cGVOP_gv) == SVt_PVGV || | |
102 | (SvTYPE(cGVOP_gv) <= SVt_PVMG && SvROK(cGVOP_gv) && SvTYPE(SvRV(cGVOP_gv)) == SVt_PVCV)); | |
ad64d0ec | 103 | XPUSHs(MUTABLE_SV(cGVOP_gv)); |
a0d0e21e LW |
104 | RETURN; |
105 | } | |
106 | ||
b1c05ba5 DM |
107 | |
108 | /* also used for: pp_andassign() */ | |
109 | ||
a0d0e21e LW |
110 | PP(pp_and) |
111 | { | |
f410a211 | 112 | PERL_ASYNC_CHECK(); |
4cc783ef | 113 | { |
1604cfb0 MS |
114 | /* SP is not used to remove a variable that is saved across the |
115 | sv_2bool_flags call in SvTRUE_NN, if a RISC/CISC or low/high machine | |
116 | register or load/store vs direct mem ops macro is introduced, this | |
117 | should be a define block between direct PL_stack_sp and dSP operations, | |
118 | presently, using PL_stack_sp is bias towards CISC cpus */ | |
119 | SV * const sv = *PL_stack_sp; | |
120 | if (!SvTRUE_NN(sv)) | |
121 | return NORMAL; | |
122 | else { | |
123 | if (PL_op->op_type == OP_AND) | |
124 | --PL_stack_sp; | |
125 | return cLOGOP->op_other; | |
126 | } | |
a0d0e21e LW |
127 | } |
128 | } | |
129 | ||
9fdd7fc4 RL |
130 | /* |
131 | * Mashup of simple padsv + sassign OPs | |
132 | * Doesn't support the following lengthy and unlikely sassign case: | |
133 | * (UNLIKELY(PL_op->op_private & OPpASSIGN_CV_TO_GV)) | |
134 | * These cases have a separate optimization, so are not handled here: | |
135 | * (PL_op->op_private & OPpASSIGN_BACKWARDS) {or,and,dor}assign | |
136 | */ | |
137 | ||
138 | PP(pp_padsv_store) | |
139 | { | |
140 | dSP; | |
141 | OP * const op = PL_op; | |
142 | SV** const padentry = &PAD_SVl(op->op_targ); | |
143 | SV* targ = *padentry; /* lvalue to assign into */ | |
144 | SV* const val = TOPs; /* RHS value to assign */ | |
145 | ||
146 | /* !OPf_STACKED is not handled by this OP */ | |
147 | assert(op->op_flags & OPf_STACKED); | |
148 | ||
149 | /* Inlined, simplified pp_padsv here */ | |
150 | if ((op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) == OPpLVAL_INTRO) { | |
151 | save_clearsv(padentry); | |
152 | } | |
153 | ||
154 | /* Inlined, simplified pp_sassign from here */ | |
155 | assert(TAINTING_get || !TAINT_get); | |
156 | if (UNLIKELY(TAINT_get) && !SvTAINTED(val)) | |
157 | TAINT_NOT; | |
158 | ||
159 | if ( | |
160 | UNLIKELY(SvTEMP(targ)) && !SvSMAGICAL(targ) && SvREFCNT(targ) == 1 && | |
161 | (!isGV_with_GP(targ) || SvFAKE(targ)) && ckWARN(WARN_MISC) | |
162 | ) | |
163 | Perl_warner(aTHX_ | |
164 | packWARN(WARN_MISC), "Useless assignment to a temporary" | |
165 | ); | |
166 | SvSetMagicSV(targ, val); | |
167 | ||
168 | SETs(targ); | |
169 | RETURN; | |
170 | } | |
171 | ||
aafefcb9 RL |
172 | /* A mashup of simplified AELEMFAST_LEX + SASSIGN OPs */ |
173 | ||
174 | PP(pp_aelemfastlex_store) | |
175 | { | |
176 | dSP; | |
177 | OP * const op = PL_op; | |
178 | SV* const val = TOPs; /* RHS value to assign */ | |
179 | AV * const av = MUTABLE_AV(PAD_SV(op->op_targ)); | |
180 | const I8 key = (I8)PL_op->op_private; | |
181 | SV * targ = NULL; | |
182 | ||
183 | /* !OPf_STACKED is not handled by this OP */ | |
184 | assert(op->op_flags & OPf_STACKED); | |
185 | ||
186 | /* Inlined, simplified pp_aelemfast here */ | |
187 | assert(SvTYPE(av) == SVt_PVAV); | |
aafefcb9 RL |
188 | |
189 | /* inlined av_fetch() for simple cases ... */ | |
9e99fbb3 | 190 | if (!SvRMAGICAL(av) && key >=0 && key <= AvFILLp(av)) { |
aafefcb9 RL |
191 | targ = AvARRAY(av)[key]; |
192 | } | |
193 | /* ... else do it the hard way */ | |
194 | if (!targ) { | |
195 | SV **svp = av_fetch(av, key, 1); | |
196 | ||
197 | if (svp) | |
198 | targ = *svp; | |
199 | else | |
200 | DIE(aTHX_ PL_no_aelem, (int)key); | |
201 | } | |
202 | ||
203 | /* Inlined, simplified pp_sassign from here */ | |
204 | assert(TAINTING_get || !TAINT_get); | |
205 | if (UNLIKELY(TAINT_get) && !SvTAINTED(val)) | |
206 | TAINT_NOT; | |
207 | ||
9e99fbb3 RL |
208 | /* This assertion is a deviation from pp_sassign, which uses an if() |
209 | * condition to check for "Useless assignment to a temporary" and | |
210 | * warns if the condition is true. Here, the condition should NEVER | |
211 | * be true when the LHS is the result of an array fetch. The | |
212 | * assertion is here as a final check that this remains the case. | |
213 | */ | |
214 | assert(!(SvTEMP(targ) && SvREFCNT(targ) == 1 && !SvSMAGICAL(targ))); | |
215 | ||
aafefcb9 RL |
216 | SvSetMagicSV(targ, val); |
217 | ||
218 | SETs(targ); | |
219 | RETURN; | |
220 | } | |
221 | ||
a0d0e21e LW |
222 | PP(pp_sassign) |
223 | { | |
20b7effb | 224 | dSP; |
3e75a3c4 RU |
225 | /* sassign keeps its args in the optree traditionally backwards. |
226 | So we pop them differently. | |
227 | */ | |
228 | SV *left = POPs; SV *right = TOPs; | |
748a9306 | 229 | |
354eabfa | 230 | if (PL_op->op_private & OPpASSIGN_BACKWARDS) { /* {or,and,dor}assign */ |
1604cfb0 MS |
231 | SV * const temp = left; |
232 | left = right; right = temp; | |
a0d0e21e | 233 | } |
d48c660d DM |
234 | assert(TAINTING_get || !TAINT_get); |
235 | if (UNLIKELY(TAINT_get) && !SvTAINTED(right)) | |
1604cfb0 | 236 | TAINT_NOT; |
5d9574c1 DM |
237 | if (UNLIKELY(PL_op->op_private & OPpASSIGN_CV_TO_GV)) { |
238 | /* *foo =\&bar */ | |
1604cfb0 MS |
239 | SV * const cv = SvRV(right); |
240 | const U32 cv_type = SvTYPE(cv); | |
241 | const bool is_gv = isGV_with_GP(left); | |
242 | const bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM; | |
243 | ||
244 | if (!got_coderef) { | |
245 | assert(SvROK(cv)); | |
246 | } | |
247 | ||
248 | /* Can do the optimisation if left (LVALUE) is not a typeglob, | |
249 | right (RVALUE) is a reference to something, and we're in void | |
250 | context. */ | |
251 | if (!got_coderef && !is_gv && GIMME_V == G_VOID) { | |
252 | /* Is the target symbol table currently empty? */ | |
253 | GV * const gv = gv_fetchsv_nomg(left, GV_NOINIT, SVt_PVGV); | |
254 | if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) { | |
255 | /* Good. Create a new proxy constant subroutine in the target. | |
256 | The gv becomes a(nother) reference to the constant. */ | |
257 | SV *const value = SvRV(cv); | |
258 | ||
259 | SvUPGRADE(MUTABLE_SV(gv), SVt_IV); | |
260 | SvPCS_IMPORTED_on(gv); | |
261 | SvRV_set(gv, value); | |
262 | SvREFCNT_inc_simple_void(value); | |
263 | SETs(left); | |
264 | RETURN; | |
265 | } | |
266 | } | |
267 | ||
268 | /* Need to fix things up. */ | |
269 | if (!is_gv) { | |
270 | /* Need to fix GV. */ | |
271 | left = MUTABLE_SV(gv_fetchsv_nomg(left,GV_ADD, SVt_PVGV)); | |
272 | } | |
273 | ||
274 | if (!got_coderef) { | |
275 | /* We've been returned a constant rather than a full subroutine, | |
276 | but they expect a subroutine reference to apply. */ | |
277 | if (SvROK(cv)) { | |
278 | ENTER_with_name("sassign_coderef"); | |
279 | SvREFCNT_inc_void(SvRV(cv)); | |
280 | /* newCONSTSUB takes a reference count on the passed in SV | |
281 | from us. We set the name to NULL, otherwise we get into | |
282 | all sorts of fun as the reference to our new sub is | |
283 | donated to the GV that we're about to assign to. | |
284 | */ | |
285 | SvRV_set(right, MUTABLE_SV(newCONSTSUB(GvSTASH(left), NULL, | |
286 | SvRV(cv)))); | |
287 | SvREFCNT_dec_NN(cv); | |
288 | LEAVE_with_name("sassign_coderef"); | |
289 | } else { | |
290 | /* What can happen for the corner case *{"BONK"} = \&{"BONK"}; | |
291 | is that | |
292 | First: ops for \&{"BONK"}; return us the constant in the | |
293 | symbol table | |
294 | Second: ops for *{"BONK"} cause that symbol table entry | |
295 | (and our reference to it) to be upgraded from RV | |
296 | to typeblob) | |
297 | Thirdly: We get here. cv is actually PVGV now, and its | |
298 | GvCV() is actually the subroutine we're looking for | |
299 | ||
300 | So change the reference so that it points to the subroutine | |
301 | of that typeglob, as that's what they were after all along. | |
302 | */ | |
303 | GV *const upgraded = MUTABLE_GV(cv); | |
304 | CV *const source = GvCV(upgraded); | |
305 | ||
306 | assert(source); | |
307 | assert(CvFLAGS(source) & CVf_CONST); | |
308 | ||
309 | SvREFCNT_inc_simple_void_NN(source); | |
310 | SvREFCNT_dec_NN(upgraded); | |
311 | SvRV_set(right, MUTABLE_SV(source)); | |
312 | } | |
313 | } | |
53a42478 | 314 | |
e26df76a | 315 | } |
8fe85e3f | 316 | if ( |
5d9574c1 | 317 | UNLIKELY(SvTEMP(left)) && !SvSMAGICAL(left) && SvREFCNT(left) == 1 && |
3e75a3c4 | 318 | (!isGV_with_GP(left) || SvFAKE(left)) && ckWARN(WARN_MISC) |
8fe85e3f | 319 | ) |
1604cfb0 MS |
320 | Perl_warner(aTHX_ |
321 | packWARN(WARN_MISC), "Useless assignment to a temporary" | |
322 | ); | |
3e75a3c4 RU |
323 | SvSetMagicSV(left, right); |
324 | SETs(left); | |
a0d0e21e LW |
325 | RETURN; |
326 | } | |
327 | ||
328 | PP(pp_cond_expr) | |
329 | { | |
20b7effb | 330 | dSP; |
f4c975aa DM |
331 | SV *sv; |
332 | ||
f410a211 | 333 | PERL_ASYNC_CHECK(); |
f4c975aa DM |
334 | sv = POPs; |
335 | RETURNOP(SvTRUE_NN(sv) ? cLOGOP->op_other : cLOGOP->op_next); | |
a0d0e21e LW |
336 | } |
337 | ||
338 | PP(pp_unstack) | |
339 | { | |
f5319de9 | 340 | PERL_CONTEXT *cx; |
8f3964af | 341 | PERL_ASYNC_CHECK(); |
a0d0e21e | 342 | TAINT_NOT; /* Each statement is presumed innocent */ |
4ebe6e95 | 343 | cx = CX_CUR(); |
f5319de9 | 344 | PL_stack_sp = PL_stack_base + cx->blk_oldsp; |
a0d0e21e | 345 | FREETMPS; |
eae48c89 | 346 | if (!(PL_op->op_flags & OPf_SPECIAL)) { |
93661e56 | 347 | assert(CxTYPE(cx) == CXt_BLOCK || CxTYPE_is_LOOP(cx)); |
1604cfb0 | 348 | CX_LEAVE_SCOPE(cx); |
eae48c89 | 349 | } |
a0d0e21e LW |
350 | return NORMAL; |
351 | } | |
352 | ||
16fe3f8a DM |
353 | |
354 | /* The main body of pp_concat, not including the magic/overload and | |
355 | * stack handling. | |
356 | * It does targ = left . right. | |
357 | * Moved into a separate function so that pp_multiconcat() can use it | |
358 | * too. | |
359 | */ | |
360 | ||
361 | PERL_STATIC_INLINE void | |
362 | S_do_concat(pTHX_ SV *left, SV *right, SV *targ, U8 targmy) | |
a0d0e21e | 363 | { |
8d6d96c1 HS |
364 | bool lbyte; |
365 | STRLEN rlen; | |
d4c19fe8 | 366 | const char *rpv = NULL; |
a6b599c7 | 367 | bool rbyte = FALSE; |
a9c4fd4e | 368 | bool rcopied = FALSE; |
8d6d96c1 | 369 | |
6f1401dc | 370 | if (TARG == right && right != left) { /* $r = $l.$r */ |
1604cfb0 MS |
371 | rpv = SvPV_nomg_const(right, rlen); |
372 | rbyte = !DO_UTF8(right); | |
373 | right = newSVpvn_flags(rpv, rlen, SVs_TEMP); | |
374 | rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */ | |
375 | rcopied = TRUE; | |
8d6d96c1 | 376 | } |
7889fe52 | 377 | |
89734059 | 378 | if (TARG != left) { /* not $l .= $r */ |
a9c4fd4e | 379 | STRLEN llen; |
6f1401dc | 380 | const char* const lpv = SvPV_nomg_const(left, llen); |
1604cfb0 MS |
381 | lbyte = !DO_UTF8(left); |
382 | sv_setpvn(TARG, lpv, llen); | |
383 | if (!lbyte) | |
384 | SvUTF8_on(TARG); | |
385 | else | |
386 | SvUTF8_off(TARG); | |
8d6d96c1 | 387 | } |
18ea7bf2 | 388 | else { /* $l .= $r and left == TARG */ |
1604cfb0 | 389 | if (!SvOK(left)) { |
51f69a24 | 390 | if ((left == right /* $l .= $l */ |
16fe3f8a | 391 | || targmy) /* $l = $l . $r */ |
51f69a24 AC |
392 | && ckWARN(WARN_UNINITIALIZED) |
393 | ) | |
394 | report_uninit(left); | |
adf14ec6 | 395 | SvPVCLEAR(left); |
1604cfb0 | 396 | } |
18ea7bf2 S |
397 | else { |
398 | SvPV_force_nomg_nolen(left); | |
399 | } | |
1604cfb0 MS |
400 | lbyte = !DO_UTF8(left); |
401 | if (IN_BYTES) | |
402 | SvUTF8_off(left); | |
8d6d96c1 | 403 | } |
a12c0f56 | 404 | |
c75ab21a | 405 | if (!rcopied) { |
1604cfb0 MS |
406 | rpv = SvPV_nomg_const(right, rlen); |
407 | rbyte = !DO_UTF8(right); | |
c75ab21a | 408 | } |
8d6d96c1 | 409 | if (lbyte != rbyte) { |
1604cfb0 MS |
410 | if (lbyte) |
411 | sv_utf8_upgrade_nomg(TARG); | |
412 | else { | |
413 | if (!rcopied) | |
414 | right = newSVpvn_flags(rpv, rlen, SVs_TEMP); | |
415 | sv_utf8_upgrade_nomg(right); | |
416 | rpv = SvPV_nomg_const(right, rlen); | |
417 | } | |
a0d0e21e | 418 | } |
8d6d96c1 | 419 | sv_catpvn_nomg(TARG, rpv, rlen); |
16fe3f8a DM |
420 | SvSETMAGIC(TARG); |
421 | } | |
422 | ||
43ebc500 | 423 | |
16fe3f8a DM |
424 | PP(pp_concat) |
425 | { | |
426 | dSP; dATARGET; tryAMAGICbin_MG(concat_amg, AMGf_assign); | |
427 | { | |
428 | dPOPTOPssrl; | |
429 | S_do_concat(aTHX_ left, right, targ, PL_op->op_private & OPpTARGET_MY); | |
430 | SETs(TARG); | |
a0d0e21e | 431 | RETURN; |
748a9306 | 432 | } |
a0d0e21e LW |
433 | } |
434 | ||
e839e6ed DM |
435 | |
436 | /* pp_multiconcat() | |
437 | ||
438 | Concatenate one or more args, possibly interleaved with constant string | |
439 | segments. The result may be assigned to, or appended to, a variable or | |
440 | expression. | |
441 | ||
442 | Several op_flags and/or op_private bits indicate what the target is, and | |
443 | whether it's appended to. Valid permutations are: | |
444 | ||
445 | - (PADTMP) = (A.B.C....) | |
446 | OPpTARGET_MY $lex = (A.B.C....) | |
447 | OPpTARGET_MY,OPpLVAL_INTRO my $lex = (A.B.C....) | |
448 | OPpTARGET_MY,OPpMULTICONCAT_APPEND $lex .= (A.B.C....) | |
449 | OPf_STACKED expr = (A.B.C....) | |
450 | OPf_STACKED,OPpMULTICONCAT_APPEND expr .= (A.B.C....) | |
451 | ||
452 | Other combinations like (A.B).(C.D) are not optimised into a multiconcat | |
453 | op, as it's too hard to get the correct ordering of ties, overload etc. | |
454 | ||
455 | In addition: | |
456 | ||
457 | OPpMULTICONCAT_FAKE: not a real concat, instead an optimised | |
458 | sprintf "...%s...". Don't call '.' | |
459 | overloading: only use '""' overloading. | |
460 | ||
55b62dee DM |
461 | OPpMULTICONCAT_STRINGIFY: the RHS was of the form |
462 | "...$a...$b..." rather than | |
e839e6ed DM |
463 | "..." . $a . "..." . $b . "..." |
464 | ||
465 | An OP_MULTICONCAT is of type UNOP_AUX. The fixed slots of the aux array are | |
466 | defined with PERL_MULTICONCAT_IX_FOO constants, where: | |
467 | ||
468 | ||
469 | FOO index description | |
470 | -------- ----- ---------------------------------- | |
471 | NARGS 0 number of arguments | |
472 | PLAIN_PV 1 non-utf8 constant string | |
473 | PLAIN_LEN 2 non-utf8 constant string length | |
474 | UTF8_PV 3 utf8 constant string | |
475 | UTF8_LEN 4 utf8 constant string length | |
476 | LENGTHS 5 first of nargs+1 const segment lengths | |
477 | ||
478 | The idea is that a general string concatenation will have a fixed (known | |
479 | at compile time) number of variable args, interspersed with constant | |
480 | strings, e.g. "a=$a b=$b\n" | |
481 | ||
482 | All the constant string segments "a=", " b=" and "\n" are stored as a | |
483 | single string "a= b=\n", pointed to from the PLAIN_PV/UTF8_PV slot, along | |
484 | with a series of segment lengths: e.g. 2,3,1. In the case where the | |
485 | constant string is plain but has a different utf8 representation, both | |
486 | variants are stored, and two sets of (nargs+1) segments lengths are stored | |
487 | in the slots beginning at PERL_MULTICONCAT_IX_LENGTHS. | |
488 | ||
489 | A segment length of -1 indicates that there is no constant string at that | |
490 | point; this distinguishes between e.g. ($a . $b) and ($a . "" . $b), which | |
491 | have differing overloading behaviour. | |
492 | ||
493 | */ | |
494 | ||
495 | PP(pp_multiconcat) | |
496 | { | |
497 | dSP; | |
498 | SV *targ; /* The SV to be assigned or appended to */ | |
057ba76a | 499 | char *targ_pv; /* where within SvPVX(targ) we're writing to */ |
e839e6ed DM |
500 | STRLEN targ_len; /* SvCUR(targ) */ |
501 | SV **toparg; /* the highest arg position on the stack */ | |
502 | UNOP_AUX_item *aux; /* PL_op->op_aux buffer */ | |
503 | UNOP_AUX_item *const_lens; /* the segment length array part of aux */ | |
504 | const char *const_pv; /* the current segment of the const string buf */ | |
ca84e88e DM |
505 | SSize_t nargs; /* how many args were expected */ |
506 | SSize_t stack_adj; /* how much to adjust SP on return */ | |
057ba76a | 507 | STRLEN grow; /* final size of destination string (targ) */ |
e839e6ed DM |
508 | UV targ_count; /* how many times targ has appeared on the RHS */ |
509 | bool is_append; /* OPpMULTICONCAT_APPEND flag is set */ | |
510 | bool slow_concat; /* args too complex for quick concat */ | |
511 | U32 dst_utf8; /* the result will be utf8 (indicate this with | |
512 | SVf_UTF8 in a U32, rather than using bool, | |
513 | for ease of testing and setting) */ | |
514 | /* for each arg, holds the result of an SvPV() call */ | |
515 | struct multiconcat_svpv { | |
d966075e | 516 | const char *pv; |
e839e6ed DM |
517 | SSize_t len; |
518 | } | |
519 | *targ_chain, /* chain of slots where targ has appeared on RHS */ | |
520 | *svpv_p, /* ptr for looping through svpv_buf */ | |
521 | *svpv_base, /* first slot (may be greater than svpv_buf), */ | |
522 | *svpv_end, /* and slot after highest result so far, of: */ | |
523 | svpv_buf[PERL_MULTICONCAT_MAXARG]; /* buf for storing SvPV() results */ | |
524 | ||
525 | aux = cUNOP_AUXx(PL_op)->op_aux; | |
ca84e88e | 526 | stack_adj = nargs = aux[PERL_MULTICONCAT_IX_NARGS].ssize; |
e839e6ed DM |
527 | is_append = cBOOL(PL_op->op_private & OPpMULTICONCAT_APPEND); |
528 | ||
529 | /* get targ from the stack or pad */ | |
530 | ||
531 | if (PL_op->op_flags & OPf_STACKED) { | |
532 | if (is_append) { | |
533 | /* for 'expr .= ...', expr is the bottom item on the stack */ | |
534 | targ = SP[-nargs]; | |
535 | stack_adj++; | |
536 | } | |
537 | else | |
538 | /* for 'expr = ...', expr is the top item on the stack */ | |
539 | targ = POPs; | |
540 | } | |
541 | else { | |
542 | SV **svp = &(PAD_SVl(PL_op->op_targ)); | |
543 | targ = *svp; | |
544 | if (PL_op->op_private & OPpLVAL_INTRO) { | |
545 | assert(PL_op->op_private & OPpTARGET_MY); | |
546 | save_clearsv(svp); | |
547 | } | |
548 | if (!nargs) | |
549 | /* $lex .= "const" doesn't cause anything to be pushed */ | |
550 | EXTEND(SP,1); | |
551 | } | |
552 | ||
553 | toparg = SP; | |
554 | SP -= (nargs - 1); | |
e839e6ed DM |
555 | grow = 1; /* allow for '\0' at minimum */ |
556 | targ_count = 0; | |
557 | targ_chain = NULL; | |
558 | targ_len = 0; | |
559 | svpv_end = svpv_buf; | |
560 | /* only utf8 variants of the const strings? */ | |
561 | dst_utf8 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv ? 0 : SVf_UTF8; | |
562 | ||
563 | ||
564 | /* -------------------------------------------------------------- | |
565 | * Phase 1: | |
566 | * | |
567 | * stringify (i.e. SvPV()) every arg and store the resultant pv/len/utf8 | |
568 | * triplets in svpv_buf[]. Also increment 'grow' by the args' lengths. | |
569 | * | |
570 | * utf8 is indicated by storing a negative length. | |
571 | * | |
572 | * Where an arg is actually targ, the stringification is deferred: | |
573 | * the length is set to 0, and the slot is added to targ_chain. | |
574 | * | |
af390142 DM |
575 | * If a magic, overloaded, or otherwise weird arg is found, which |
576 | * might have side effects when stringified, the loop is abandoned and | |
577 | * we goto a code block where a more basic 'emulate calling | |
578 | * pp_cpncat() on each arg in turn' is done. | |
e839e6ed DM |
579 | */ |
580 | ||
581 | for (; SP <= toparg; SP++, svpv_end++) { | |
e839e6ed DM |
582 | U32 utf8; |
583 | STRLEN len; | |
584 | SV *sv; | |
585 | ||
586 | assert(svpv_end - svpv_buf < PERL_MULTICONCAT_MAXARG); | |
587 | ||
588 | sv = *SP; | |
e839e6ed DM |
589 | |
590 | /* this if/else chain is arranged so that common/simple cases | |
591 | * take few conditionals */ | |
592 | ||
af390142 DM |
593 | if (LIKELY((SvFLAGS(sv) & (SVs_GMG|SVf_ROK|SVf_POK)) == SVf_POK)) { |
594 | /* common case: sv is a simple non-magical PV */ | |
595 | if (targ == sv) { | |
596 | /* targ appears on RHS. | |
597 | * Delay storing PV pointer; instead, add slot to targ_chain | |
598 | * so it can be populated later, after targ has been grown and | |
599 | * we know its final SvPVX() address. | |
600 | */ | |
601 | targ_on_rhs: | |
602 | svpv_end->len = 0; /* zerojng here means we can skip | |
603 | updating later if targ_len == 0 */ | |
604 | svpv_end->pv = (char*)targ_chain; | |
605 | targ_chain = svpv_end; | |
606 | targ_count++; | |
607 | continue; | |
608 | } | |
609 | ||
e839e6ed | 610 | len = SvCUR(sv); |
af390142 | 611 | svpv_end->pv = SvPVX(sv); |
e839e6ed | 612 | } |
af390142 DM |
613 | else if (UNLIKELY(SvFLAGS(sv) & (SVs_GMG|SVf_ROK))) |
614 | /* may have side effects: tie, overload etc. | |
615 | * Abandon 'stringify everything first' and handle | |
616 | * args in strict order. Note that already-stringified args | |
617 | * will be reprocessed, which is safe because the each first | |
618 | * stringification would have been idempotent. | |
e839e6ed | 619 | */ |
af390142 DM |
620 | goto do_magical; |
621 | else if (SvNIOK(sv)) { | |
622 | if (targ == sv) | |
623 | goto targ_on_rhs; | |
624 | /* stringify general valid scalar */ | |
e839e6ed DM |
625 | svpv_end->pv = sv_2pv_flags(sv, &len, 0); |
626 | } | |
af390142 DM |
627 | else if (!SvOK(sv)) { |
628 | if (ckWARN(WARN_UNINITIALIZED)) | |
629 | /* an undef value in the presence of warnings may trigger | |
630 | * side affects */ | |
631 | goto do_magical; | |
d966075e | 632 | svpv_end->pv = ""; |
af390142 DM |
633 | len = 0; |
634 | } | |
635 | else | |
636 | goto do_magical; /* something weird */ | |
e839e6ed DM |
637 | |
638 | utf8 = (SvFLAGS(sv) & SVf_UTF8); | |
639 | dst_utf8 |= utf8; | |
640 | ASSUME(len < SSize_t_MAX); | |
641 | svpv_end->len = utf8 ? -(SSize_t)len : (SSize_t)len; | |
642 | grow += len; | |
643 | } | |
644 | ||
645 | /* -------------------------------------------------------------- | |
646 | * Phase 2: | |
647 | * | |
648 | * Stringify targ: | |
649 | * | |
650 | * if targ appears on the RHS or is appended to, force stringify it; | |
651 | * otherwise set it to "". Then set targ_len. | |
652 | */ | |
653 | ||
654 | if (is_append) { | |
af390142 DM |
655 | /* abandon quick route if using targ might have side effects */ |
656 | if (UNLIKELY(SvFLAGS(targ) & (SVs_GMG|SVf_ROK))) | |
657 | goto do_magical; | |
e839e6ed DM |
658 | |
659 | if (SvOK(targ)) { | |
660 | U32 targ_utf8; | |
661 | stringify_targ: | |
662 | SvPV_force_nomg_nolen(targ); | |
663 | targ_utf8 = SvFLAGS(targ) & SVf_UTF8; | |
664 | if (UNLIKELY(dst_utf8 & ~targ_utf8)) { | |
665 | if (LIKELY(!IN_BYTES)) | |
666 | sv_utf8_upgrade_nomg(targ); | |
667 | } | |
668 | else | |
669 | dst_utf8 |= targ_utf8; | |
670 | ||
671 | targ_len = SvCUR(targ); | |
672 | grow += targ_len * (targ_count + is_append); | |
673 | goto phase3; | |
674 | } | |
af390142 DM |
675 | else if (ckWARN(WARN_UNINITIALIZED)) |
676 | /* warning might have side effects */ | |
677 | goto do_magical; | |
678 | /* the undef targ will be silently SvPVCLEAR()ed below */ | |
e839e6ed DM |
679 | } |
680 | else if (UNLIKELY(SvTYPE(targ) >= SVt_REGEXP)) { | |
681 | /* Assigning to some weird LHS type. Don't force the LHS to be an | |
682 | * empty string; instead, do things 'long hand' by using the | |
683 | * overload code path, which concats to a TEMP sv and does | |
684 | * sv_catsv() calls rather than COPY()s. This ensures that even | |
685 | * bizarre code like this doesn't break or crash: | |
686 | * *F = *F . *F. | |
687 | * (which makes the 'F' typeglob an alias to the | |
688 | * '*main::F*main::F' typeglob). | |
689 | */ | |
af390142 | 690 | goto do_magical; |
e839e6ed | 691 | } |
af390142 | 692 | else if (targ_chain) |
e839e6ed | 693 | /* targ was found on RHS. |
af390142 DM |
694 | * Force stringify it, using the same code as the append branch |
695 | * above, except that we don't need the magic/overload/undef | |
696 | * checks as these will already have been done in the phase 1 | |
697 | * loop. | |
e839e6ed | 698 | */ |
e839e6ed | 699 | goto stringify_targ; |
e839e6ed DM |
700 | |
701 | /* unrolled SvPVCLEAR() - mostly: no need to grow or set SvCUR() to 0; | |
702 | * those will be done later. */ | |
e839e6ed DM |
703 | SV_CHECK_THINKFIRST_COW_DROP(targ); |
704 | SvUPGRADE(targ, SVt_PV); | |
705 | SvFLAGS(targ) &= ~(SVf_OK|SVf_IVisUV|SVf_UTF8); | |
706 | SvFLAGS(targ) |= (SVf_POK|SVp_POK|dst_utf8); | |
707 | ||
708 | phase3: | |
709 | ||
710 | /* -------------------------------------------------------------- | |
711 | * Phase 3: | |
712 | * | |
057ba76a | 713 | * UTF-8 tweaks and grow targ: |
e839e6ed DM |
714 | * |
715 | * Now that we know the length and utf8-ness of both the targ and | |
057ba76a | 716 | * args, grow targ to the size needed to accumulate all the args, based |
e839e6ed DM |
717 | * on whether targ appears on the RHS, whether we're appending, and |
718 | * whether any non-utf8 args expand in size if converted to utf8. | |
719 | * | |
720 | * For the latter, if dst_utf8 we scan non-utf8 args looking for | |
721 | * variant chars, and adjust the svpv->len value of those args to the | |
722 | * utf8 size and negate it to flag them. At the same time we un-negate | |
723 | * the lens of any utf8 args since after this phase we no longer care | |
724 | * whether an arg is utf8 or not. | |
725 | * | |
726 | * Finally, initialise const_lens and const_pv based on utf8ness. | |
727 | * Note that there are 3 permutations: | |
728 | * | |
729 | * * If the constant string is invariant whether utf8 or not (e.g. "abc"), | |
730 | * then aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN] are the same as | |
731 | * aux[PERL_MULTICONCAT_IX_UTF8_PV/LEN] and there is one set of | |
732 | * segment lengths. | |
733 | * | |
734 | * * If the string is fully utf8, e.g. "\x{100}", then | |
735 | * aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN] == (NULL,0) and there is | |
736 | * one set of segment lengths. | |
737 | * | |
738 | * * If the string has different plain and utf8 representations | |
a3815e44 | 739 | * (e.g. "\x80"), then aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN]] |
e839e6ed DM |
740 | * holds the plain rep, while aux[PERL_MULTICONCAT_IX_UTF8_PV/LEN] |
741 | * holds the utf8 rep, and there are 2 sets of segment lengths, | |
742 | * with the utf8 set following after the plain set. | |
743 | * | |
744 | * On entry to this section the (pv,len) pairs in svpv_buf have the | |
745 | * following meanings: | |
746 | * (pv, len) a plain string | |
747 | * (pv, -len) a utf8 string | |
748 | * (NULL, 0) left-most targ \ linked together R-to-L | |
749 | * (next, 0) other targ / in targ_chain | |
750 | */ | |
751 | ||
752 | /* turn off utf8 handling if 'use bytes' is in scope */ | |
753 | if (UNLIKELY(dst_utf8 && IN_BYTES)) { | |
754 | dst_utf8 = 0; | |
057ba76a | 755 | SvUTF8_off(targ); |
e839e6ed DM |
756 | /* undo all the negative lengths which flag utf8-ness */ |
757 | for (svpv_p = svpv_buf; svpv_p < svpv_end; svpv_p++) { | |
758 | SSize_t len = svpv_p->len; | |
759 | if (len < 0) | |
760 | svpv_p->len = -len; | |
761 | } | |
762 | } | |
763 | ||
764 | /* grow += total of lengths of constant string segments */ | |
765 | { | |
766 | SSize_t len; | |
767 | len = aux[dst_utf8 ? PERL_MULTICONCAT_IX_UTF8_LEN | |
b5bf9f73 | 768 | : PERL_MULTICONCAT_IX_PLAIN_LEN].ssize; |
e839e6ed DM |
769 | slow_concat = cBOOL(len); |
770 | grow += len; | |
771 | } | |
772 | ||
773 | const_lens = aux + PERL_MULTICONCAT_IX_LENGTHS; | |
774 | ||
775 | if (dst_utf8) { | |
776 | const_pv = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv; | |
777 | if ( aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv | |
778 | && const_pv != aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv) | |
779 | /* separate sets of lengths for plain and utf8 */ | |
780 | const_lens += nargs + 1; | |
781 | ||
782 | /* If the result is utf8 but some of the args aren't, | |
783 | * calculate how much extra growth is needed for all the chars | |
784 | * which will expand to two utf8 bytes. | |
785 | * Also, if the growth is non-zero, negate the length to indicate | |
a3815e44 | 786 | * that this is a variant string. Conversely, un-negate the |
e839e6ed DM |
787 | * length on utf8 args (which was only needed to flag non-utf8 |
788 | * args in this loop */ | |
789 | for (svpv_p = svpv_buf; svpv_p < svpv_end; svpv_p++) { | |
7d5ed5d0 | 790 | SSize_t len, extra; |
e839e6ed DM |
791 | |
792 | len = svpv_p->len; | |
793 | if (len <= 0) { | |
794 | svpv_p->len = -len; | |
795 | continue; | |
796 | } | |
797 | ||
7d5ed5d0 KW |
798 | extra = variant_under_utf8_count((U8 *) svpv_p->pv, |
799 | (U8 *) svpv_p->pv + len); | |
e839e6ed DM |
800 | if (UNLIKELY(extra)) { |
801 | grow += extra; | |
802 | /* -ve len indicates special handling */ | |
803 | svpv_p->len = -(len + extra); | |
804 | slow_concat = TRUE; | |
805 | } | |
806 | } | |
807 | } | |
808 | else | |
809 | const_pv = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv; | |
810 | ||
811 | /* unrolled SvGROW(), except don't check for SVf_IsCOW, which should | |
812 | * already have been dropped */ | |
057ba76a DM |
813 | assert(!SvIsCOW(targ)); |
814 | targ_pv = (SvLEN(targ) < (grow) ? sv_grow(targ,grow) : SvPVX(targ)); | |
e839e6ed DM |
815 | |
816 | ||
817 | /* -------------------------------------------------------------- | |
818 | * Phase 4: | |
819 | * | |
057ba76a DM |
820 | * Now that targ has been grown, we know the final address of the targ |
821 | * PVX, if needed. Preserve / move targ contents if appending or if | |
822 | * targ appears on RHS. | |
e839e6ed DM |
823 | * |
824 | * Also update svpv_buf slots in targ_chain. | |
825 | * | |
826 | * Don't bother with any of this if the target length is zero: | |
827 | * targ_len is set to zero unless we're appending or targ appears on | |
828 | * RHS. And even if it is, we can optimise by skipping this chunk of | |
829 | * code for zero targ_len. In the latter case, we don't need to update | |
830 | * the slots in targ_chain with the (zero length) target string, since | |
831 | * we set the len in such slots to 0 earlier, and since the Copy() is | |
832 | * skipped on zero length, it doesn't matter what svpv_p->pv contains. | |
833 | * | |
834 | * On entry to this section the (pv,len) pairs in svpv_buf have the | |
835 | * following meanings: | |
836 | * (pv, len) a pure-plain or utf8 string | |
837 | * (pv, -(len+extra)) a plain string which will expand by 'extra' | |
838 | * bytes when converted to utf8 | |
839 | * (NULL, 0) left-most targ \ linked together R-to-L | |
840 | * (next, 0) other targ / in targ_chain | |
841 | * | |
842 | * On exit, the targ contents will have been moved to the | |
843 | * earliest place they are needed (e.g. $x = "abc$x" will shift them | |
844 | * 3 bytes, while $x .= ... will leave them at the beginning); | |
057ba76a | 845 | * and dst_pv will point to the location within SvPVX(targ) where the |
e839e6ed DM |
846 | * next arg should be copied. |
847 | */ | |
848 | ||
849 | svpv_base = svpv_buf; | |
850 | ||
851 | if (targ_len) { | |
852 | struct multiconcat_svpv *tc_stop; | |
057ba76a | 853 | char *targ_buf = targ_pv; /* ptr to original targ string */ |
e839e6ed | 854 | |
e839e6ed DM |
855 | assert(is_append || targ_count); |
856 | ||
857 | if (is_append) { | |
057ba76a | 858 | targ_pv += targ_len; |
e839e6ed DM |
859 | tc_stop = NULL; |
860 | } | |
861 | else { | |
862 | /* The targ appears on RHS, e.g. '$t = $a . $t . $t'. | |
863 | * Move the current contents of targ to the first | |
864 | * position where it's needed, and use that as the src buffer | |
865 | * for any further uses (such as the second RHS $t above). | |
866 | * In calculating the first position, we need to sum the | |
867 | * lengths of all consts and args before that. | |
868 | */ | |
869 | ||
870 | UNOP_AUX_item *lens = const_lens; | |
871 | /* length of first const string segment */ | |
b5bf9f73 | 872 | STRLEN offset = lens->ssize > 0 ? lens->ssize : 0; |
e839e6ed DM |
873 | |
874 | assert(targ_chain); | |
875 | svpv_p = svpv_base; | |
876 | ||
877 | for (;;) { | |
878 | SSize_t len; | |
879 | if (!svpv_p->pv) | |
880 | break; /* the first targ argument */ | |
881 | /* add lengths of the next arg and const string segment */ | |
882 | len = svpv_p->len; | |
883 | if (len < 0) /* variant args have this */ | |
884 | len = -len; | |
885 | offset += (STRLEN)len; | |
b5bf9f73 | 886 | len = (++lens)->ssize; |
e839e6ed DM |
887 | offset += (len >= 0) ? (STRLEN)len : 0; |
888 | if (!offset) { | |
889 | /* all args and consts so far are empty; update | |
890 | * the start position for the concat later */ | |
891 | svpv_base++; | |
892 | const_lens++; | |
893 | } | |
894 | svpv_p++; | |
895 | assert(svpv_p < svpv_end); | |
896 | } | |
897 | ||
898 | if (offset) { | |
057ba76a DM |
899 | targ_buf += offset; |
900 | Move(targ_pv, targ_buf, targ_len, char); | |
e839e6ed | 901 | /* a negative length implies don't Copy(), but do increment */ |
90b21a3e | 902 | svpv_p->len = -((SSize_t)targ_len); |
e839e6ed DM |
903 | slow_concat = TRUE; |
904 | } | |
905 | else { | |
906 | /* skip the first targ copy */ | |
907 | svpv_base++; | |
908 | const_lens++; | |
057ba76a | 909 | targ_pv += targ_len; |
e839e6ed DM |
910 | } |
911 | ||
912 | /* Don't populate the first targ slot in the loop below; it's | |
913 | * either not used because we advanced svpv_base beyond it, or | |
914 | * we already stored the special -targ_len value in it | |
915 | */ | |
916 | tc_stop = svpv_p; | |
917 | } | |
918 | ||
919 | /* populate slots in svpv_buf representing targ on RHS */ | |
920 | while (targ_chain != tc_stop) { | |
921 | struct multiconcat_svpv *p = targ_chain; | |
922 | targ_chain = (struct multiconcat_svpv *)(p->pv); | |
057ba76a | 923 | p->pv = targ_buf; |
e839e6ed DM |
924 | p->len = (SSize_t)targ_len; |
925 | } | |
926 | } | |
927 | ||
928 | ||
929 | /* -------------------------------------------------------------- | |
930 | * Phase 5: | |
931 | * | |
057ba76a | 932 | * Append all the args in svpv_buf, plus the const strings, to targ. |
e839e6ed DM |
933 | * |
934 | * On entry to this section the (pv,len) pairs in svpv_buf have the | |
935 | * following meanings: | |
936 | * (pv, len) a pure-plain or utf8 string (which may be targ) | |
937 | * (pv, -(len+extra)) a plain string which will expand by 'extra' | |
938 | * bytes when converted to utf8 | |
939 | * (0, -len) left-most targ, whose content has already | |
057ba76a | 940 | * been copied. Just advance targ_pv by len. |
e839e6ed DM |
941 | */ |
942 | ||
943 | /* If there are no constant strings and no special case args | |
944 | * (svpv_p->len < 0), use a simpler, more efficient concat loop | |
945 | */ | |
946 | if (!slow_concat) { | |
947 | for (svpv_p = svpv_base; svpv_p < svpv_end; svpv_p++) { | |
948 | SSize_t len = svpv_p->len; | |
949 | if (!len) | |
950 | continue; | |
057ba76a DM |
951 | Copy(svpv_p->pv, targ_pv, len, char); |
952 | targ_pv += len; | |
e839e6ed DM |
953 | } |
954 | const_lens += (svpv_end - svpv_base + 1); | |
955 | } | |
956 | else { | |
957 | /* Note that we iterate the loop nargs+1 times: to append nargs | |
958 | * arguments and nargs+1 constant strings. For example, "-$a-$b-" | |
959 | */ | |
92ef216d | 960 | svpv_p = svpv_base; |
e839e6ed DM |
961 | |
962 | for (;;) { | |
b5bf9f73 | 963 | SSize_t len = (const_lens++)->ssize; |
e839e6ed DM |
964 | |
965 | /* append next const string segment */ | |
966 | if (len > 0) { | |
057ba76a DM |
967 | Copy(const_pv, targ_pv, len, char); |
968 | targ_pv += len; | |
e839e6ed DM |
969 | const_pv += len; |
970 | } | |
971 | ||
92ef216d | 972 | if (svpv_p == svpv_end) |
e839e6ed DM |
973 | break; |
974 | ||
975 | /* append next arg */ | |
976 | len = svpv_p->len; | |
977 | ||
978 | if (LIKELY(len > 0)) { | |
057ba76a DM |
979 | Copy(svpv_p->pv, targ_pv, len, char); |
980 | targ_pv += len; | |
e839e6ed DM |
981 | } |
982 | else if (UNLIKELY(len < 0)) { | |
983 | /* negative length indicates two special cases */ | |
984 | const char *p = svpv_p->pv; | |
985 | len = -len; | |
986 | if (UNLIKELY(p)) { | |
987 | /* copy plain-but-variant pv to a utf8 targ */ | |
057ba76a | 988 | char * end_pv = targ_pv + len; |
e839e6ed | 989 | assert(dst_utf8); |
057ba76a | 990 | while (targ_pv < end_pv) { |
e839e6ed | 991 | U8 c = (U8) *p++; |
057ba76a | 992 | append_utf8_from_native_byte(c, (U8**)&targ_pv); |
e839e6ed DM |
993 | } |
994 | } | |
995 | else | |
996 | /* arg is already-copied targ */ | |
057ba76a | 997 | targ_pv += len; |
e839e6ed DM |
998 | } |
999 | ||
92ef216d | 1000 | ++svpv_p; |
e839e6ed DM |
1001 | } |
1002 | } | |
1003 | ||
057ba76a DM |
1004 | *targ_pv = '\0'; |
1005 | SvCUR_set(targ, targ_pv - SvPVX(targ)); | |
1006 | assert(grow >= SvCUR(targ) + 1); | |
1007 | assert(SvLEN(targ) >= SvCUR(targ) + 1); | |
e839e6ed DM |
1008 | |
1009 | /* -------------------------------------------------------------- | |
1010 | * Phase 6: | |
1011 | * | |
af390142 | 1012 | * return result |
e839e6ed DM |
1013 | */ |
1014 | ||
af390142 DM |
1015 | SP -= stack_adj; |
1016 | SvTAINT(targ); | |
1017 | SETTARG; | |
1018 | RETURN; | |
e839e6ed | 1019 | |
af390142 DM |
1020 | /* -------------------------------------------------------------- |
1021 | * Phase 7: | |
1022 | * | |
1023 | * We only get here if any of the args (or targ too in the case of | |
1024 | * append) have something which might cause side effects, such | |
1025 | * as magic, overload, or an undef value in the presence of warnings. | |
1026 | * In that case, any earlier attempt to stringify the args will have | |
1027 | * been abandoned, and we come here instead. | |
1028 | * | |
1029 | * Here, we concat each arg in turn the old-fashioned way: essentially | |
1030 | * emulating pp_concat() in a loop. This means that all the weird edge | |
1031 | * cases will be handled correctly, if not necessarily speedily. | |
1032 | * | |
1033 | * Note that some args may already have been stringified - those are | |
1034 | * processed again, which is safe, since only args without side-effects | |
1035 | * were stringified earlier. | |
1036 | */ | |
1037 | ||
1038 | do_magical: | |
1039 | { | |
1040 | SSize_t i, n; | |
1041 | SV *left = NULL; | |
1042 | SV *right; | |
1043 | SV* nexttarg; | |
1044 | bool nextappend; | |
1045 | U32 utf8 = 0; | |
1046 | SV **svp; | |
1047 | const char *cpv = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv; | |
1048 | UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS; | |
55b62dee | 1049 | Size_t arg_count = 0; /* how many args have been processed */ |
af390142 DM |
1050 | |
1051 | if (!cpv) { | |
1052 | cpv = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv; | |
1053 | utf8 = SVf_UTF8; | |
1054 | } | |
1055 | ||
1056 | svp = toparg - nargs + 1; | |
1057 | ||
1058 | /* iterate for: | |
1059 | * nargs arguments, | |
1060 | * plus possible nargs+1 consts, | |
1061 | * plus, if appending, a final targ in an extra last iteration | |
1062 | */ | |
1063 | ||
1064 | n = nargs *2 + 1; | |
55b62dee DM |
1065 | for (i = 0; i <= n; i++) { |
1066 | SSize_t len; | |
1067 | ||
1068 | /* if necessary, stringify the final RHS result in | |
1069 | * something like $targ .= "$a$b$c" - simulating | |
1070 | * pp_stringify | |
1071 | */ | |
1072 | if ( i == n | |
1073 | && (PL_op->op_private &OPpMULTICONCAT_STRINGIFY) | |
1074 | && !(SvPOK(left)) | |
1075 | /* extra conditions for backwards compatibility: | |
1076 | * probably incorrect, but keep the existing behaviour | |
1077 | * for now. The rules are: | |
1078 | * $x = "$ov" single arg: stringify; | |
1079 | * $x = "$ov$y" multiple args: don't stringify, | |
1080 | * $lex = "$ov$y$z" except TARGMY with at least 2 concats | |
1081 | */ | |
1082 | && ( arg_count == 1 | |
1083 | || ( arg_count >= 3 | |
1084 | && !is_append | |
1085 | && (PL_op->op_private & OPpTARGET_MY) | |
1086 | && !(PL_op->op_private & OPpLVAL_INTRO) | |
1087 | ) | |
1088 | ) | |
1089 | ) | |
1090 | { | |
7ea8b04b | 1091 | SV *tmp = newSV_type_mortal(SVt_PV); |
55b62dee DM |
1092 | sv_copypv(tmp, left); |
1093 | SvSETMAGIC(tmp); | |
1094 | left = tmp; | |
1095 | } | |
1096 | ||
1097 | /* do one extra iteration to handle $targ in $targ .= ... */ | |
1098 | if (i == n && !is_append) | |
1099 | break; | |
1100 | ||
af390142 | 1101 | /* get the next arg SV or regen the next const SV */ |
55b62dee | 1102 | len = lens[i >> 1].ssize; |
af390142 DM |
1103 | if (i == n) { |
1104 | /* handle the final targ .= (....) */ | |
1105 | right = left; | |
1106 | left = targ; | |
1107 | } | |
1108 | else if (i & 1) | |
1109 | right = svp[(i >> 1)]; | |
1110 | else if (len < 0) | |
1111 | continue; /* no const in this position */ | |
1112 | else { | |
1113 | right = newSVpvn_flags(cpv, len, (utf8 | SVs_TEMP)); | |
1114 | cpv += len; | |
1115 | } | |
e839e6ed | 1116 | |
55b62dee DM |
1117 | arg_count++; |
1118 | ||
1119 | if (arg_count <= 1) { | |
af390142 DM |
1120 | left = right; |
1121 | continue; /* need at least two SVs to concat together */ | |
1122 | } | |
1123 | ||
55b62dee | 1124 | if (arg_count == 2 && i < n) { |
af390142 DM |
1125 | /* for the first concat, create a mortal acting like the |
1126 | * padtmp from OP_CONST. In later iterations this will | |
1127 | * be appended to */ | |
1128 | nexttarg = sv_newmortal(); | |
1129 | nextappend = FALSE; | |
af390142 DM |
1130 | } |
1131 | else { | |
1132 | nexttarg = left; | |
1133 | nextappend = TRUE; | |
1134 | } | |
1135 | ||
1136 | /* Handle possible overloading. | |
1137 | * This is basically an unrolled | |
1138 | * tryAMAGICbin_MG(concat_amg, AMGf_assign); | |
1139 | * and | |
1140 | * Perl_try_amagic_bin() | |
1141 | * call, but using left and right rather than SP[-1], SP[0], | |
1142 | * and not relying on OPf_STACKED implying .= | |
e839e6ed | 1143 | */ |
e839e6ed | 1144 | |
af390142 DM |
1145 | if ((SvFLAGS(left)|SvFLAGS(right)) & (SVf_ROK|SVs_GMG)) { |
1146 | SvGETMAGIC(left); | |
1147 | if (left != right) | |
1148 | SvGETMAGIC(right); | |
1149 | ||
1150 | if ((SvAMAGIC(left) || SvAMAGIC(right)) | |
1151 | /* sprintf doesn't do concat overloading, | |
1152 | * but allow for $x .= sprintf(...) | |
1153 | */ | |
1154 | && ( !(PL_op->op_private & OPpMULTICONCAT_FAKE) | |
1155 | || i == n) | |
e839e6ed | 1156 | ) |
af390142 DM |
1157 | { |
1158 | SV * const tmpsv = amagic_call(left, right, concat_amg, | |
1159 | (nextappend ? AMGf_assign: 0)); | |
1160 | if (tmpsv) { | |
7554d344 DM |
1161 | /* NB: tryAMAGICbin_MG() includes an OPpTARGET_MY test |
1162 | * here, which isn't needed as any implicit | |
1163 | * assign done under OPpTARGET_MY is done after | |
af390142 DM |
1164 | * this loop */ |
1165 | if (nextappend) { | |
1166 | sv_setsv(left, tmpsv); | |
1167 | SvSETMAGIC(left); | |
e839e6ed | 1168 | } |
af390142 DM |
1169 | else |
1170 | left = tmpsv; | |
1171 | continue; | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | /* if both args are the same magical value, make one a copy */ | |
1176 | if (left == right && SvGMAGICAL(left)) { | |
f7f919a0 | 1177 | SV * targetsv = right; |
af390142 DM |
1178 | /* Print the uninitialized warning now, so it includes the |
1179 | * variable name. */ | |
1180 | if (!SvOK(right)) { | |
1181 | if (ckWARN(WARN_UNINITIALIZED)) | |
1182 | report_uninit(right); | |
f7f919a0 | 1183 | targetsv = &PL_sv_no; |
e839e6ed | 1184 | } |
f7f919a0 | 1185 | left = sv_mortalcopy_flags(targetsv, 0); |
af390142 | 1186 | SvGETMAGIC(right); |
e839e6ed DM |
1187 | } |
1188 | } | |
e839e6ed | 1189 | |
af390142 DM |
1190 | /* nexttarg = left . right */ |
1191 | S_do_concat(aTHX_ left, right, nexttarg, 0); | |
1192 | left = nexttarg; | |
e839e6ed | 1193 | } |
e839e6ed | 1194 | |
af390142 | 1195 | SP = toparg - stack_adj + 1; |
e839e6ed | 1196 | |
4e521aaf DM |
1197 | /* Return the result of all RHS concats, unless this op includes |
1198 | * an assign ($lex = x.y.z or expr = x.y.z), in which case copy | |
1199 | * to target (which will be $lex or expr). | |
af390142 DM |
1200 | * If we are appending, targ will already have been appended to in |
1201 | * the loop */ | |
4e521aaf DM |
1202 | if ( !is_append |
1203 | && ( (PL_op->op_flags & OPf_STACKED) | |
1204 | || (PL_op->op_private & OPpTARGET_MY)) | |
1205 | ) { | |
af390142 DM |
1206 | sv_setsv(targ, left); |
1207 | SvSETMAGIC(targ); | |
1208 | } | |
4e521aaf DM |
1209 | else |
1210 | targ = left; | |
af390142 DM |
1211 | SETs(targ); |
1212 | RETURN; | |
1213 | } | |
e839e6ed DM |
1214 | } |
1215 | ||
1216 | ||
0b5aba47 DM |
1217 | /* push the elements of av onto the stack. |
1218 | * Returns PL_op->op_next to allow tail-call optimisation of its callers */ | |
d5524600 | 1219 | |
0b5aba47 | 1220 | STATIC OP* |
d5524600 DM |
1221 | S_pushav(pTHX_ AV* const av) |
1222 | { | |
1223 | dSP; | |
c70927a6 | 1224 | const SSize_t maxarg = AvFILL(av) + 1; |
d5524600 | 1225 | EXTEND(SP, maxarg); |
5d9574c1 | 1226 | if (UNLIKELY(SvRMAGICAL(av))) { |
c70927a6 FC |
1227 | PADOFFSET i; |
1228 | for (i=0; i < (PADOFFSET)maxarg; i++) { | |
fd77b29b FC |
1229 | SV ** const svp = av_fetch(av, i, FALSE); |
1230 | SP[i+1] = LIKELY(svp) | |
1231 | ? *svp | |
1232 | : UNLIKELY(PL_op->op_flags & OPf_MOD) | |
1f1dcfb5 | 1233 | ? av_nonelem(av,i) |
fd77b29b | 1234 | : &PL_sv_undef; |
d5524600 DM |
1235 | } |
1236 | } | |
1237 | else { | |
c70927a6 FC |
1238 | PADOFFSET i; |
1239 | for (i=0; i < (PADOFFSET)maxarg; i++) { | |
6661956a | 1240 | SV *sv = AvARRAY(av)[i]; |
1604cfb0 | 1241 | SP[i+1] = LIKELY(sv) |
fd77b29b FC |
1242 | ? sv |
1243 | : UNLIKELY(PL_op->op_flags & OPf_MOD) | |
1f1dcfb5 | 1244 | ? av_nonelem(av,i) |
fd77b29b | 1245 | : &PL_sv_undef; |
ce0d59fd | 1246 | } |
d5524600 DM |
1247 | } |
1248 | SP += maxarg; | |
1249 | PUTBACK; | |
0b5aba47 | 1250 | return NORMAL; |
d5524600 DM |
1251 | } |
1252 | ||
1253 | ||
a7fd8ef6 DM |
1254 | /* ($lex1,@lex2,...) or my ($lex1,@lex2,...) */ |
1255 | ||
1256 | PP(pp_padrange) | |
1257 | { | |
20b7effb | 1258 | dSP; |
a7fd8ef6 DM |
1259 | PADOFFSET base = PL_op->op_targ; |
1260 | int count = (int)(PL_op->op_private) & OPpPADRANGE_COUNTMASK; | |
d5524600 DM |
1261 | if (PL_op->op_flags & OPf_SPECIAL) { |
1262 | /* fake the RHS of my ($x,$y,..) = @_ */ | |
1263 | PUSHMARK(SP); | |
0b5aba47 | 1264 | (void)S_pushav(aTHX_ GvAVn(PL_defgv)); |
d5524600 DM |
1265 | SPAGAIN; |
1266 | } | |
1267 | ||
a7fd8ef6 DM |
1268 | /* note, this is only skipped for compile-time-known void cxt */ |
1269 | if ((PL_op->op_flags & OPf_WANT) != OPf_WANT_VOID) { | |
19742f39 AL |
1270 | int i; |
1271 | ||
a7fd8ef6 DM |
1272 | EXTEND(SP, count); |
1273 | PUSHMARK(SP); | |
1274 | for (i = 0; i <count; i++) | |
1275 | *++SP = PAD_SV(base+i); | |
1276 | } | |
1277 | if (PL_op->op_private & OPpLVAL_INTRO) { | |
4e09461c DM |
1278 | SV **svp = &(PAD_SVl(base)); |
1279 | const UV payload = (UV)( | |
1280 | (base << (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)) | |
1281 | | (count << SAVE_TIGHT_SHIFT) | |
1282 | | SAVEt_CLEARPADRANGE); | |
19742f39 AL |
1283 | int i; |
1284 | ||
6d59e610 | 1285 | STATIC_ASSERT_STMT(OPpPADRANGE_COUNTMASK + 1 == (1 << OPpPADRANGE_COUNTSHIFT)); |
d081a355 DM |
1286 | assert((payload >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)) |
1287 | == (Size_t)base); | |
a3444cc5 DM |
1288 | { |
1289 | dSS_ADD; | |
1290 | SS_ADD_UV(payload); | |
1291 | SS_ADD_END(1); | |
1292 | } | |
4e09461c | 1293 | |
a7fd8ef6 | 1294 | for (i = 0; i <count; i++) |
4e09461c | 1295 | SvPADSTALE_off(*svp++); /* mark lexical as active */ |
a7fd8ef6 DM |
1296 | } |
1297 | RETURN; | |
1298 | } | |
1299 | ||
1300 | ||
a0d0e21e LW |
1301 | PP(pp_padsv) |
1302 | { | |
20b7effb | 1303 | dSP; |
6c28b496 DD |
1304 | EXTEND(SP, 1); |
1305 | { | |
1604cfb0 MS |
1306 | OP * const op = PL_op; |
1307 | /* access PL_curpad once */ | |
1308 | SV ** const padentry = &(PAD_SVl(op->op_targ)); | |
1309 | { | |
1310 | dTARG; | |
1311 | TARG = *padentry; | |
1312 | PUSHs(TARG); | |
1313 | PUTBACK; /* no pop/push after this, TOPs ok */ | |
1314 | } | |
1315 | if (op->op_flags & OPf_MOD) { | |
1316 | if (op->op_private & OPpLVAL_INTRO) | |
1317 | if (!(op->op_private & OPpPAD_STATE)) | |
1318 | save_clearsv(padentry); | |
1319 | if (op->op_private & OPpDEREF) { | |
1320 | /* TOPs is equivalent to TARG here. Using TOPs (SP) rather | |
1321 | than TARG reduces the scope of TARG, so it does not | |
1322 | span the call to save_clearsv, resulting in smaller | |
1323 | machine code. */ | |
1324 | TOPs = vivify_ref(TOPs, op->op_private & OPpDEREF); | |
1325 | } | |
1326 | } | |
1327 | return op->op_next; | |
4633a7c4 | 1328 | } |
a0d0e21e LW |
1329 | } |
1330 | ||
1331 | PP(pp_readline) | |
1332 | { | |
30901a8a | 1333 | dSP; |
12dc5f94 DM |
1334 | /* pp_coreargs pushes a NULL to indicate no args passed to |
1335 | * CORE::readline() */ | |
30901a8a | 1336 | if (TOPs) { |
1604cfb0 MS |
1337 | SvGETMAGIC(TOPs); |
1338 | tryAMAGICunTARGETlist(iter_amg, 0); | |
1339 | PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--); | |
30901a8a FC |
1340 | } |
1341 | else PL_last_in_gv = PL_argvgv, PL_stack_sp--; | |
6e592b3a | 1342 | if (!isGV_with_GP(PL_last_in_gv)) { |
1604cfb0 MS |
1343 | if (SvROK(PL_last_in_gv) && isGV_with_GP(SvRV(PL_last_in_gv))) |
1344 | PL_last_in_gv = MUTABLE_GV(SvRV(PL_last_in_gv)); | |
1345 | else { | |
1346 | dSP; | |
1347 | XPUSHs(MUTABLE_SV(PL_last_in_gv)); | |
1348 | PUTBACK; | |
1349 | Perl_pp_rv2gv(aTHX); | |
1350 | PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--); | |
745e740c | 1351 | assert((SV*)PL_last_in_gv == &PL_sv_undef || isGV_with_GP(PL_last_in_gv)); |
1604cfb0 | 1352 | } |
f5284f61 | 1353 | } |
a0d0e21e LW |
1354 | return do_readline(); |
1355 | } | |
1356 | ||
1357 | PP(pp_eq) | |
1358 | { | |
20b7effb | 1359 | dSP; |
33efebe6 | 1360 | SV *left, *right; |
fe9826e3 | 1361 | U32 flags_and, flags_or; |
33efebe6 | 1362 | |
0872de45 | 1363 | tryAMAGICbin_MG(eq_amg, AMGf_numeric); |
33efebe6 DM |
1364 | right = POPs; |
1365 | left = TOPs; | |
fe9826e3 RL |
1366 | flags_and = SvFLAGS(left) & SvFLAGS(right); |
1367 | flags_or = SvFLAGS(left) | SvFLAGS(right); | |
1368 | ||
33efebe6 | 1369 | SETs(boolSV( |
fe9826e3 RL |
1370 | ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) ) |
1371 | ? (SvIVX(left) == SvIVX(right)) | |
1372 | : (flags_and & SVf_NOK) | |
1373 | ? (SvNVX(left) == SvNVX(right)) | |
1374 | : ( do_ncmp(left, right) == 0) | |
33efebe6 DM |
1375 | )); |
1376 | RETURN; | |
a0d0e21e LW |
1377 | } |
1378 | ||
b1c05ba5 | 1379 | |
4c2c3128 | 1380 | /* also used for: pp_i_preinc() */ |
b1c05ba5 | 1381 | |
a0d0e21e LW |
1382 | PP(pp_preinc) |
1383 | { | |
4c2c3128 DM |
1384 | SV *sv = *PL_stack_sp; |
1385 | ||
1386 | if (LIKELY(((sv->sv_flags & | |
1387 | (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV| | |
1388 | SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK)) | |
1389 | == SVf_IOK)) | |
1390 | && SvIVX(sv) != IV_MAX) | |
1391 | { | |
1604cfb0 | 1392 | SvIV_set(sv, SvIVX(sv) + 1); |
4c2c3128 DM |
1393 | } |
1394 | else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_inc */ | |
1604cfb0 | 1395 | sv_inc(sv); |
4c2c3128 DM |
1396 | SvSETMAGIC(sv); |
1397 | return NORMAL; | |
1398 | } | |
1399 | ||
1400 | ||
1401 | /* also used for: pp_i_predec() */ | |
1402 | ||
1403 | PP(pp_predec) | |
1404 | { | |
1405 | SV *sv = *PL_stack_sp; | |
1406 | ||
1407 | if (LIKELY(((sv->sv_flags & | |
1408 | (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV| | |
1409 | SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK)) | |
1410 | == SVf_IOK)) | |
1411 | && SvIVX(sv) != IV_MIN) | |
55497cff | 1412 | { |
1604cfb0 | 1413 | SvIV_set(sv, SvIVX(sv) - 1); |
748a9306 | 1414 | } |
4c2c3128 | 1415 | else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_dec */ |
1604cfb0 | 1416 | sv_dec(sv); |
4c2c3128 | 1417 | SvSETMAGIC(sv); |
a0d0e21e LW |
1418 | return NORMAL; |
1419 | } | |
1420 | ||
b1c05ba5 DM |
1421 | |
1422 | /* also used for: pp_orassign() */ | |
1423 | ||
a0d0e21e LW |
1424 | PP(pp_or) |
1425 | { | |
20b7effb | 1426 | dSP; |
f4c975aa | 1427 | SV *sv; |
f410a211 | 1428 | PERL_ASYNC_CHECK(); |
f4c975aa DM |
1429 | sv = TOPs; |
1430 | if (SvTRUE_NN(sv)) | |
1604cfb0 | 1431 | RETURN; |
a0d0e21e | 1432 | else { |
1604cfb0 | 1433 | if (PL_op->op_type == OP_OR) |
c960fc3b | 1434 | --SP; |
1604cfb0 | 1435 | RETURNOP(cLOGOP->op_other); |
a0d0e21e LW |
1436 | } |
1437 | } | |
1438 | ||
b1c05ba5 DM |
1439 | |
1440 | /* also used for: pp_dor() pp_dorassign() */ | |
1441 | ||
25a55bd7 | 1442 | PP(pp_defined) |
c963b151 | 1443 | { |
20b7effb | 1444 | dSP; |
c243917e RL |
1445 | SV* sv = TOPs; |
1446 | bool defined = FALSE; | |
25a55bd7 | 1447 | const int op_type = PL_op->op_type; |
ea5195b7 | 1448 | const bool is_dor = (op_type == OP_DOR || op_type == OP_DORASSIGN); |
c963b151 | 1449 | |
6136c704 | 1450 | if (is_dor) { |
1604cfb0 | 1451 | PERL_ASYNC_CHECK(); |
5d9574c1 | 1452 | if (UNLIKELY(!sv || !SvANY(sv))) { |
1604cfb0 MS |
1453 | if (op_type == OP_DOR) |
1454 | --SP; | |
25a55bd7 SP |
1455 | RETURNOP(cLOGOP->op_other); |
1456 | } | |
b7c44293 RGS |
1457 | } |
1458 | else { | |
1604cfb0 | 1459 | /* OP_DEFINED */ |
5d9574c1 | 1460 | if (UNLIKELY(!sv || !SvANY(sv))) |
c243917e | 1461 | RETSETNO; |
b7c44293 | 1462 | } |
25a55bd7 | 1463 | |
034242a8 NC |
1464 | /* Historically what followed was a switch on SvTYPE(sv), handling SVt_PVAV, |
1465 | * SVt_PVCV, SVt_PVHV and "default". `defined &sub` is still valid syntax, | |
1466 | * hence we still need the special case PVCV code. But AVs and HVs now | |
1467 | * should never arrive here... */ | |
1468 | #ifdef DEBUGGING | |
1469 | assert(SvTYPE(sv) != SVt_PVAV); | |
1470 | assert(SvTYPE(sv) != SVt_PVHV); | |
1471 | #endif | |
1472 | ||
2517717a | 1473 | if (UNLIKELY(SvTYPE(sv) == SVt_PVCV)) { |
1604cfb0 MS |
1474 | if (CvROOT(sv) || CvXSUB(sv)) |
1475 | defined = TRUE; | |
2517717a NC |
1476 | } |
1477 | else { | |
1604cfb0 MS |
1478 | SvGETMAGIC(sv); |
1479 | if (SvOK(sv)) | |
1480 | defined = TRUE; | |
c963b151 | 1481 | } |
6136c704 AL |
1482 | |
1483 | if (is_dor) { | |
c960fc3b SP |
1484 | if(defined) |
1485 | RETURN; | |
1486 | if(op_type == OP_DOR) | |
1487 | --SP; | |
25a55bd7 | 1488 | RETURNOP(cLOGOP->op_other); |
25a55bd7 | 1489 | } |
d9aa96a4 SP |
1490 | /* assuming OP_DEFINED */ |
1491 | if(defined) | |
c243917e RL |
1492 | RETSETYES; |
1493 | RETSETNO; | |
c963b151 BD |
1494 | } |
1495 | ||
230ee21f DM |
1496 | |
1497 | ||
a0d0e21e LW |
1498 | PP(pp_add) |
1499 | { | |
20b7effb | 1500 | dSP; dATARGET; bool useleft; SV *svl, *svr; |
230ee21f | 1501 | |
6f1401dc DM |
1502 | tryAMAGICbin_MG(add_amg, AMGf_assign|AMGf_numeric); |
1503 | svr = TOPs; | |
1504 | svl = TOPm1s; | |
1505 | ||
28e5dec8 | 1506 | #ifdef PERL_PRESERVE_IVUV |
230ee21f DM |
1507 | |
1508 | /* special-case some simple common cases */ | |
1509 | if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) { | |
1510 | IV il, ir; | |
1511 | U32 flags = (svl->sv_flags & svr->sv_flags); | |
1512 | if (flags & SVf_IOK) { | |
1513 | /* both args are simple IVs */ | |
1514 | UV topl, topr; | |
1515 | il = SvIVX(svl); | |
1516 | ir = SvIVX(svr); | |
1517 | do_iv: | |
1518 | topl = ((UV)il) >> (UVSIZE * 8 - 2); | |
1519 | topr = ((UV)ir) >> (UVSIZE * 8 - 2); | |
1520 | ||
1521 | /* if both are in a range that can't under/overflow, do a | |
1522 | * simple integer add: if the top of both numbers | |
1523 | * are 00 or 11, then it's safe */ | |
1524 | if (!( ((topl+1) | (topr+1)) & 2)) { | |
1525 | SP--; | |
1526 | TARGi(il + ir, 0); /* args not GMG, so can't be tainted */ | |
1527 | SETs(TARG); | |
1528 | RETURN; | |
1529 | } | |
1530 | goto generic; | |
1531 | } | |
1532 | else if (flags & SVf_NOK) { | |
1533 | /* both args are NVs */ | |
1534 | NV nl = SvNVX(svl); | |
1535 | NV nr = SvNVX(svr); | |
1536 | ||
3a019afd | 1537 | if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) { |
230ee21f DM |
1538 | /* nothing was lost by converting to IVs */ |
1539 | goto do_iv; | |
3a019afd | 1540 | } |
230ee21f DM |
1541 | SP--; |
1542 | TARGn(nl + nr, 0); /* args not GMG, so can't be tainted */ | |
1543 | SETs(TARG); | |
1544 | RETURN; | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | generic: | |
1549 | ||
1550 | useleft = USE_LEFT(svl); | |
28e5dec8 JH |
1551 | /* We must see if we can perform the addition with integers if possible, |
1552 | as the integer code detects overflow while the NV code doesn't. | |
1553 | If either argument hasn't had a numeric conversion yet attempt to get | |
1554 | the IV. It's important to do this now, rather than just assuming that | |
1555 | it's not IOK as a PV of "9223372036854775806" may not take well to NV | |
1556 | addition, and an SV which is NOK, NV=6.0 ought to be coerced to | |
1557 | integer in case the second argument is IV=9223372036854775806 | |
1558 | We can (now) rely on sv_2iv to do the right thing, only setting the | |
1559 | public IOK flag if the value in the NV (or PV) slot is truly integer. | |
1560 | ||
1561 | A side effect is that this also aggressively prefers integer maths over | |
7dca457a NC |
1562 | fp maths for integer values. |
1563 | ||
a00b5bd3 | 1564 | How to detect overflow? |
7dca457a NC |
1565 | |
1566 | C 99 section 6.2.6.1 says | |
1567 | ||
1568 | The range of nonnegative values of a signed integer type is a subrange | |
1569 | of the corresponding unsigned integer type, and the representation of | |
1570 | the same value in each type is the same. A computation involving | |
1571 | unsigned operands can never overflow, because a result that cannot be | |
1572 | represented by the resulting unsigned integer type is reduced modulo | |
1573 | the number that is one greater than the largest value that can be | |
1574 | represented by the resulting type. | |
1575 | ||
1576 | (the 9th paragraph) | |
1577 | ||
1578 | which I read as "unsigned ints wrap." | |
1579 | ||
1580 | signed integer overflow seems to be classed as "exception condition" | |
1581 | ||
1582 | If an exceptional condition occurs during the evaluation of an | |
1583 | expression (that is, if the result is not mathematically defined or not | |
1584 | in the range of representable values for its type), the behavior is | |
1585 | undefined. | |
1586 | ||
1587 | (6.5, the 5th paragraph) | |
1588 | ||
1589 | I had assumed that on 2s complement machines signed arithmetic would | |
1590 | wrap, hence coded pp_add and pp_subtract on the assumption that | |
1591 | everything perl builds on would be happy. After much wailing and | |
1592 | gnashing of teeth it would seem that irix64 knows its ANSI spec well, | |
1593 | knows that it doesn't need to, and doesn't. Bah. Anyway, the all- | |
1594 | unsigned code below is actually shorter than the old code. :-) | |
1595 | */ | |
1596 | ||
01f91bf2 | 1597 | if (SvIV_please_nomg(svr)) { |
1604cfb0 MS |
1598 | /* Unless the left argument is integer in range we are going to have to |
1599 | use NV maths. Hence only attempt to coerce the right argument if | |
1600 | we know the left is integer. */ | |
1601 | UV auv = 0; | |
1602 | bool auvok = FALSE; | |
1603 | bool a_valid = 0; | |
1604 | ||
1605 | if (!useleft) { | |
1606 | auv = 0; | |
1607 | a_valid = auvok = 1; | |
1608 | /* left operand is undef, treat as zero. + 0 is identity, | |
1609 | Could SETi or SETu right now, but space optimise by not adding | |
1610 | lots of code to speed up what is probably a rarish case. */ | |
1611 | } else { | |
1612 | /* Left operand is defined, so is it IV? */ | |
1613 | if (SvIV_please_nomg(svl)) { | |
1614 | if ((auvok = SvUOK(svl))) | |
1615 | auv = SvUVX(svl); | |
1616 | else { | |
1617 | const IV aiv = SvIVX(svl); | |
1618 | if (aiv >= 0) { | |
1619 | auv = aiv; | |
1620 | auvok = 1; /* Now acting as a sign flag. */ | |
1621 | } else { | |
9354a41f KW |
1622 | /* Using 0- here and later to silence bogus warning |
1623 | * from MS VC */ | |
1624 | auv = (UV) (0 - (UV) aiv); | |
1604cfb0 MS |
1625 | } |
1626 | } | |
1627 | a_valid = 1; | |
1628 | } | |
1629 | } | |
1630 | if (a_valid) { | |
1631 | bool result_good = 0; | |
1632 | UV result; | |
1633 | UV buv; | |
1634 | bool buvok = SvUOK(svr); | |
1635 | ||
1636 | if (buvok) | |
1637 | buv = SvUVX(svr); | |
1638 | else { | |
1639 | const IV biv = SvIVX(svr); | |
1640 | if (biv >= 0) { | |
1641 | buv = biv; | |
1642 | buvok = 1; | |
1643 | } else | |
9354a41f | 1644 | buv = (UV) (0 - (UV) biv); |
1604cfb0 MS |
1645 | } |
1646 | /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve, | |
1647 | else "IV" now, independent of how it came in. | |
1648 | if a, b represents positive, A, B negative, a maps to -A etc | |
1649 | a + b => (a + b) | |
1650 | A + b => -(a - b) | |
1651 | a + B => (a - b) | |
1652 | A + B => -(a + b) | |
1653 | all UV maths. negate result if A negative. | |
1654 | add if signs same, subtract if signs differ. */ | |
1655 | ||
1656 | if (auvok ^ buvok) { | |
1657 | /* Signs differ. */ | |
1658 | if (auv >= buv) { | |
1659 | result = auv - buv; | |
1660 | /* Must get smaller */ | |
1661 | if (result <= auv) | |
1662 | result_good = 1; | |
1663 | } else { | |
1664 | result = buv - auv; | |
1665 | if (result <= buv) { | |
1666 | /* result really should be -(auv-buv). as its negation | |
1667 | of true value, need to swap our result flag */ | |
1668 | auvok = !auvok; | |
1669 | result_good = 1; | |
1670 | } | |
1671 | } | |
1672 | } else { | |
1673 | /* Signs same */ | |
1674 | result = auv + buv; | |
1675 | if (result >= auv) | |
1676 | result_good = 1; | |
1677 | } | |
1678 | if (result_good) { | |
1679 | SP--; | |
1680 | if (auvok) | |
1681 | SETu( result ); | |
1682 | else { | |
1683 | /* Negate result */ | |
1684 | if (result <= (UV)IV_MIN) | |
53e2bfb7 DM |
1685 | SETi(result == (UV)IV_MIN |
1686 | ? IV_MIN : -(IV)result); | |
1604cfb0 MS |
1687 | else { |
1688 | /* result valid, but out of range for IV. */ | |
1689 | SETn( -(NV)result ); | |
1690 | } | |
1691 | } | |
1692 | RETURN; | |
1693 | } /* Overflow, drop through to NVs. */ | |
1694 | } | |
28e5dec8 | 1695 | } |
230ee21f DM |
1696 | |
1697 | #else | |
1698 | useleft = USE_LEFT(svl); | |
28e5dec8 | 1699 | #endif |
230ee21f | 1700 | |
a0d0e21e | 1701 | { |
1604cfb0 MS |
1702 | NV value = SvNV_nomg(svr); |
1703 | (void)POPs; | |
1704 | if (!useleft) { | |
1705 | /* left operand is undef, treat as zero. + 0.0 is identity. */ | |
1706 | SETn(value); | |
1707 | RETURN; | |
1708 | } | |
1709 | SETn( value + SvNV_nomg(svl) ); | |
1710 | RETURN; | |
a0d0e21e LW |
1711 | } |
1712 | } | |
1713 | ||
b1c05ba5 DM |
1714 | |
1715 | /* also used for: pp_aelemfast_lex() */ | |
1716 | ||
a0d0e21e LW |
1717 | PP(pp_aelemfast) |
1718 | { | |
20b7effb | 1719 | dSP; |
93bad3fd | 1720 | AV * const av = PL_op->op_type == OP_AELEMFAST_LEX |
1604cfb0 | 1721 | ? MUTABLE_AV(PAD_SV(PL_op->op_targ)) : GvAVn(cGVOP_gv); |
a3b680e6 | 1722 | const U32 lval = PL_op->op_flags & OPf_MOD; |
7e169e84 DM |
1723 | const I8 key = (I8)PL_op->op_private; |
1724 | SV** svp; | |
1725 | SV *sv; | |
1726 | ||
1727 | assert(SvTYPE(av) == SVt_PVAV); | |
1728 | ||
f4484b87 DM |
1729 | EXTEND(SP, 1); |
1730 | ||
7e169e84 DM |
1731 | /* inlined av_fetch() for simple cases ... */ |
1732 | if (!SvRMAGICAL(av) && key >= 0 && key <= AvFILLp(av)) { | |
1733 | sv = AvARRAY(av)[key]; | |
9fb994be | 1734 | if (sv) { |
7e169e84 DM |
1735 | PUSHs(sv); |
1736 | RETURN; | |
5af5b232 RL |
1737 | } else if (!lval) { |
1738 | PUSHs(&PL_sv_undef); | |
1739 | RETURN; | |
7e169e84 DM |
1740 | } |
1741 | } | |
1742 | ||
1743 | /* ... else do it the hard way */ | |
1744 | svp = av_fetch(av, key, lval); | |
1745 | sv = (svp ? *svp : &PL_sv_undef); | |
b024352e DM |
1746 | |
1747 | if (UNLIKELY(!svp && lval)) | |
7e169e84 | 1748 | DIE(aTHX_ PL_no_aelem, (int)key); |
b024352e | 1749 | |
39cf747a | 1750 | if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */ |
1604cfb0 | 1751 | mg_get(sv); |
be6c24e0 | 1752 | PUSHs(sv); |
a0d0e21e LW |
1753 | RETURN; |
1754 | } | |
1755 | ||
1756 | PP(pp_join) | |
1757 | { | |
20b7effb | 1758 | dSP; dMARK; dTARGET; |
a0d0e21e LW |
1759 | MARK++; |
1760 | do_join(TARG, *MARK, MARK, SP); | |
1761 | SP = MARK; | |
1762 | SETs(TARG); | |
1763 | RETURN; | |
1764 | } | |
1765 | ||
a0d0e21e LW |
1766 | /* Oversized hot code. */ |
1767 | ||
b1c05ba5 DM |
1768 | /* also used for: pp_say() */ |
1769 | ||
a0d0e21e LW |
1770 | PP(pp_print) |
1771 | { | |
20b7effb | 1772 | dSP; dMARK; dORIGMARK; |
eb578fdb | 1773 | PerlIO *fp; |
236988e4 | 1774 | MAGIC *mg; |
159b6efe | 1775 | GV * const gv |
1604cfb0 | 1776 | = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv; |
9c9f25b8 | 1777 | IO *io = GvIO(gv); |
5b468f54 | 1778 | |
9c9f25b8 | 1779 | if (io |
1604cfb0 | 1780 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
5b468f54 | 1781 | { |
01bb7c6d | 1782 | had_magic: |
1604cfb0 MS |
1783 | if (MARK == ORIGMARK) { |
1784 | /* If using default handle then we need to make space to | |
1785 | * pass object as 1st arg, so move other args up ... | |
1786 | */ | |
1787 | MEXTEND(SP, 1); | |
1788 | ++MARK; | |
1789 | Move(MARK, MARK + 1, (SP - MARK) + 1, SV*); | |
1790 | ++SP; | |
1791 | } | |
1792 | return Perl_tied_method(aTHX_ SV_CONST(PRINT), mark - 1, MUTABLE_SV(io), | |
1793 | mg, | |
1794 | (G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK | |
1795 | | (PL_op->op_type == OP_SAY | |
1796 | ? TIED_METHOD_SAY : 0)), sp - mark); | |
236988e4 | 1797 | } |
9c9f25b8 | 1798 | if (!io) { |
68b590d9 | 1799 | if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv))) |
1604cfb0 | 1800 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
01bb7c6d | 1801 | goto had_magic; |
1604cfb0 MS |
1802 | report_evil_fh(gv); |
1803 | SETERRNO(EBADF,RMS_IFI); | |
1804 | goto just_say_no; | |
a0d0e21e LW |
1805 | } |
1806 | else if (!(fp = IoOFP(io))) { | |
1604cfb0 MS |
1807 | if (IoIFP(io)) |
1808 | report_wrongway_fh(gv, '<'); | |
1809 | else | |
1810 | report_evil_fh(gv); | |
1811 | SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI); | |
1812 | goto just_say_no; | |
a0d0e21e LW |
1813 | } |
1814 | else { | |
1604cfb0 MS |
1815 | SV * const ofs = GvSV(PL_ofsgv); /* $, */ |
1816 | MARK++; | |
1817 | if (ofs && (SvGMAGICAL(ofs) || SvOK(ofs))) { | |
1818 | while (MARK <= SP) { | |
1819 | if (!do_print(*MARK, fp)) | |
1820 | break; | |
1821 | MARK++; | |
1822 | if (MARK <= SP) { | |
1823 | /* don't use 'ofs' here - it may be invalidated by magic callbacks */ | |
1824 | if (!do_print(GvSV(PL_ofsgv), fp)) { | |
1825 | MARK--; | |
1826 | break; | |
1827 | } | |
1828 | } | |
1829 | } | |
1830 | } | |
1831 | else { | |
1832 | while (MARK <= SP) { | |
1833 | if (!do_print(*MARK, fp)) | |
1834 | break; | |
1835 | MARK++; | |
1836 | } | |
1837 | } | |
1838 | if (MARK <= SP) | |
1839 | goto just_say_no; | |
1840 | else { | |
1841 | if (PL_op->op_type == OP_SAY) { | |
1842 | if (PerlIO_write(fp, "\n", 1) == 0 || PerlIO_error(fp)) | |
1843 | goto just_say_no; | |
1844 | } | |
cfc4a7da | 1845 | else if (PL_ors_sv && SvOK(PL_ors_sv)) |
1604cfb0 MS |
1846 | if (!do_print(PL_ors_sv, fp)) /* $\ */ |
1847 | goto just_say_no; | |
a0d0e21e | 1848 | |
1604cfb0 MS |
1849 | if (IoFLAGS(io) & IOf_FLUSH) |
1850 | if (PerlIO_flush(fp) == EOF) | |
1851 | goto just_say_no; | |
1852 | } | |
a0d0e21e LW |
1853 | } |
1854 | SP = ORIGMARK; | |
e52fd6f4 | 1855 | XPUSHs(&PL_sv_yes); |
a0d0e21e LW |
1856 | RETURN; |
1857 | ||
1858 | just_say_no: | |
1859 | SP = ORIGMARK; | |
e52fd6f4 | 1860 | XPUSHs(&PL_sv_undef); |
a0d0e21e LW |
1861 | RETURN; |
1862 | } | |
1863 | ||
b1c05ba5 | 1864 | |
aa36782f DM |
1865 | /* do the common parts of pp_padhv() and pp_rv2hv() |
1866 | * It assumes the caller has done EXTEND(SP, 1) or equivalent. | |
af3b1cba | 1867 | * 'is_keys' indicates the OPpPADHV_ISKEYS/OPpRV2HV_ISKEYS flag is set. |
e84e4286 DM |
1868 | * 'has_targ' indicates that the op has a target - this should |
1869 | * be a compile-time constant so that the code can constant-folded as | |
1870 | * appropriate | |
aa36782f DM |
1871 | * */ |
1872 | ||
1873 | PERL_STATIC_INLINE OP* | |
e84e4286 | 1874 | S_padhv_rv2hv_common(pTHX_ HV *hv, U8 gimme, bool is_keys, bool has_targ) |
aa36782f | 1875 | { |
aa36782f DM |
1876 | dSP; |
1877 | ||
1878 | assert(PL_op->op_type == OP_PADHV || PL_op->op_type == OP_RV2HV); | |
1879 | ||
eb7e169e | 1880 | if (gimme == G_LIST) { |
8dc9003f | 1881 | hv_pushkv(hv, 3); |
af3b1cba | 1882 | return NORMAL; |
aa36782f DM |
1883 | } |
1884 | ||
1885 | if (is_keys) | |
1886 | /* 'keys %h' masquerading as '%h': reset iterator */ | |
1887 | (void)hv_iterinit(hv); | |
1888 | ||
6f2dc9a6 DM |
1889 | if (gimme == G_VOID) |
1890 | return NORMAL; | |
1891 | ||
4f5c8291 YO |
1892 | bool is_bool = ( PL_op->op_private & OPpTRUEBOOL |
1893 | || ( PL_op->op_private & OPpMAYBE_TRUEBOOL | |
1894 | && block_gimme() == G_VOID)); | |
1895 | ||
1896 | MAGIC *is_tied_mg = SvRMAGICAL(hv) | |
1897 | ? mg_find(MUTABLE_SV(hv), PERL_MAGIC_tied) | |
1898 | : NULL; | |
e80717e7 | 1899 | |
4f5c8291 YO |
1900 | IV i = 0; |
1901 | SV *sv = NULL; | |
1902 | if (UNLIKELY(is_tied_mg)) { | |
e80717e7 DM |
1903 | if (is_keys && !is_bool) { |
1904 | i = 0; | |
1905 | while (hv_iternext(hv)) | |
1906 | i++; | |
1907 | goto push_i; | |
1908 | } | |
1909 | else { | |
4f5c8291 | 1910 | sv = magic_scalarpack(hv, is_tied_mg); |
e80717e7 DM |
1911 | goto push_sv; |
1912 | } | |
3cd2c7d4 | 1913 | } |
e80717e7 | 1914 | else { |
00164771 NC |
1915 | #if defined(DYNAMIC_ENV_FETCH) && defined(VMS) |
1916 | /* maybe nothing set up %ENV for iteration yet... | |
1917 | do this always (not just if HvUSEDKEYS(hv) is currently 0) because | |
1918 | we ought to give a *consistent* answer to "how many keys?" | |
1919 | whether we ask this op in scalar context, or get the list of all | |
1920 | keys then check its length, and whether we do either with or without | |
1921 | an %ENV lookup first. prime_env_iter() returns quickly if nothing | |
1922 | needs doing. */ | |
1923 | if (SvRMAGICAL((const SV *)hv) | |
1924 | && mg_find((const SV *)hv, PERL_MAGIC_env)) { | |
1925 | prime_env_iter(); | |
1926 | } | |
1927 | #endif | |
e80717e7 DM |
1928 | i = HvUSEDKEYS(hv); |
1929 | if (is_bool) { | |
1930 | sv = i ? &PL_sv_yes : &PL_sv_zero; | |
1931 | push_sv: | |
1932 | PUSHs(sv); | |
1933 | } | |
1934 | else { | |
1935 | push_i: | |
e84e4286 DM |
1936 | if (has_targ) { |
1937 | dTARGET; | |
1938 | PUSHi(i); | |
1939 | } | |
1940 | else | |
6f2dc9a6 DM |
1941 | if (is_keys) { |
1942 | /* parent op should be an unused OP_KEYS whose targ we can | |
1943 | * use */ | |
1944 | dTARG; | |
1945 | OP *k; | |
1946 | ||
1947 | assert(!OpHAS_SIBLING(PL_op)); | |
1948 | k = PL_op->op_sibparent; | |
1949 | assert(k->op_type == OP_KEYS); | |
1950 | TARG = PAD_SV(k->op_targ); | |
1951 | PUSHi(i); | |
1952 | } | |
1953 | else | |
e84e4286 | 1954 | mPUSHi(i); |
aa36782f | 1955 | } |
aa36782f DM |
1956 | } |
1957 | ||
1958 | PUTBACK; | |
1959 | return NORMAL; | |
1960 | } | |
1961 | ||
1962 | ||
e855b461 DM |
1963 | /* This is also called directly by pp_lvavref. */ |
1964 | PP(pp_padav) | |
1965 | { | |
1966 | dSP; dTARGET; | |
1967 | U8 gimme; | |
1968 | assert(SvTYPE(TARG) == SVt_PVAV); | |
1969 | if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO )) | |
1604cfb0 MS |
1970 | if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) )) |
1971 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); | |
e855b461 DM |
1972 | EXTEND(SP, 1); |
1973 | ||
1974 | if (PL_op->op_flags & OPf_REF) { | |
1604cfb0 MS |
1975 | PUSHs(TARG); |
1976 | RETURN; | |
e855b461 DM |
1977 | } |
1978 | else if (PL_op->op_private & OPpMAYBE_LVSUB) { | |
1979 | const I32 flags = is_lvalue_sub(); | |
1980 | if (flags && !(flags & OPpENTERSUB_INARGS)) { | |
1604cfb0 | 1981 | if (GIMME_V == G_SCALAR) |
e855b461 DM |
1982 | /* diag_listed_as: Can't return %s to lvalue scalar context */ |
1983 | Perl_croak(aTHX_ "Can't return array to lvalue scalar context"); | |
1984 | PUSHs(TARG); | |
1985 | RETURN; | |
1986 | } | |
1987 | } | |
1988 | ||
1989 | gimme = GIMME_V; | |
eb7e169e | 1990 | if (gimme == G_LIST) |
0b5aba47 | 1991 | return S_pushav(aTHX_ (AV*)TARG); |
327c9b9e DM |
1992 | |
1993 | if (gimme == G_SCALAR) { | |
1604cfb0 | 1994 | const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1; |
e855b461 DM |
1995 | if (!maxarg) |
1996 | PUSHs(&PL_sv_zero); | |
1997 | else if (PL_op->op_private & OPpTRUEBOOL) | |
1998 | PUSHs(&PL_sv_yes); | |
1999 | else | |
2000 | mPUSHi(maxarg); | |
2001 | } | |
2002 | RETURN; | |
2003 | } | |
2004 | ||
2005 | ||
2006 | PP(pp_padhv) | |
2007 | { | |
2008 | dSP; dTARGET; | |
2009 | U8 gimme; | |
e855b461 DM |
2010 | |
2011 | assert(SvTYPE(TARG) == SVt_PVHV); | |
e855b461 | 2012 | if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO )) |
1604cfb0 MS |
2013 | if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) )) |
2014 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); | |
e855b461 | 2015 | |
aa36782f DM |
2016 | EXTEND(SP, 1); |
2017 | ||
2018 | if (PL_op->op_flags & OPf_REF) { | |
2019 | PUSHs(TARG); | |
1604cfb0 | 2020 | RETURN; |
aa36782f | 2021 | } |
e855b461 DM |
2022 | else if (PL_op->op_private & OPpMAYBE_LVSUB) { |
2023 | const I32 flags = is_lvalue_sub(); | |
2024 | if (flags && !(flags & OPpENTERSUB_INARGS)) { | |
2025 | if (GIMME_V == G_SCALAR) | |
2026 | /* diag_listed_as: Can't return %s to lvalue scalar context */ | |
2027 | Perl_croak(aTHX_ "Can't return hash to lvalue scalar context"); | |
aa36782f | 2028 | PUSHs(TARG); |
e855b461 DM |
2029 | RETURN; |
2030 | } | |
2031 | } | |
2032 | ||
2033 | gimme = GIMME_V; | |
e855b461 | 2034 | |
aa36782f | 2035 | return S_padhv_rv2hv_common(aTHX_ (HV*)TARG, gimme, |
e84e4286 DM |
2036 | cBOOL(PL_op->op_private & OPpPADHV_ISKEYS), |
2037 | 0 /* has_targ*/); | |
e855b461 DM |
2038 | } |
2039 | ||
2040 | ||
b1c05ba5 | 2041 | /* also used for: pp_rv2hv() */ |
bdaf10a5 | 2042 | /* also called directly by pp_lvavref */ |
b1c05ba5 | 2043 | |
a0d0e21e LW |
2044 | PP(pp_rv2av) |
2045 | { | |
20b7effb | 2046 | dSP; dTOPss; |
1c23e2bd | 2047 | const U8 gimme = GIMME_V; |
13c59d41 MH |
2048 | static const char an_array[] = "an ARRAY"; |
2049 | static const char a_hash[] = "a HASH"; | |
bdaf10a5 | 2050 | const bool is_pp_rv2av = PL_op->op_type == OP_RV2AV |
1604cfb0 | 2051 | || PL_op->op_type == OP_LVAVREF; |
d83b45b8 | 2052 | const svtype type = is_pp_rv2av ? SVt_PVAV : SVt_PVHV; |
a0d0e21e | 2053 | |
9026059d | 2054 | SvGETMAGIC(sv); |
a0d0e21e | 2055 | if (SvROK(sv)) { |
1604cfb0 MS |
2056 | if (UNLIKELY(SvAMAGIC(sv))) { |
2057 | sv = amagic_deref_call(sv, is_pp_rv2av ? to_av_amg : to_hv_amg); | |
2058 | } | |
2059 | sv = SvRV(sv); | |
2060 | if (UNLIKELY(SvTYPE(sv) != type)) | |
2061 | /* diag_listed_as: Not an ARRAY reference */ | |
2062 | DIE(aTHX_ "Not %s reference", is_pp_rv2av ? an_array : a_hash); | |
2063 | else if (UNLIKELY(PL_op->op_flags & OPf_MOD | |
2064 | && PL_op->op_private & OPpLVAL_INTRO)) | |
2065 | Perl_croak(aTHX_ "%s", PL_no_localize_ref); | |
a0d0e21e | 2066 | } |
5d9574c1 | 2067 | else if (UNLIKELY(SvTYPE(sv) != type)) { |
1604cfb0 MS |
2068 | GV *gv; |
2069 | ||
2070 | if (!isGV_with_GP(sv)) { | |
2071 | gv = Perl_softref2xv(aTHX_ sv, is_pp_rv2av ? an_array : a_hash, | |
2072 | type, &sp); | |
2073 | if (!gv) | |
2074 | RETURN; | |
2075 | } | |
2076 | else { | |
2077 | gv = MUTABLE_GV(sv); | |
2078 | } | |
2079 | sv = is_pp_rv2av ? MUTABLE_SV(GvAVn(gv)) : MUTABLE_SV(GvHVn(gv)); | |
2080 | if (PL_op->op_private & OPpLVAL_INTRO) | |
2081 | sv = is_pp_rv2av ? MUTABLE_SV(save_ary(gv)) : MUTABLE_SV(save_hash(gv)); | |
9f527363 FC |
2082 | } |
2083 | if (PL_op->op_flags & OPf_REF) { | |
1604cfb0 MS |
2084 | SETs(sv); |
2085 | RETURN; | |
9f527363 | 2086 | } |
5d9574c1 | 2087 | else if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) { |
1604cfb0 MS |
2088 | const I32 flags = is_lvalue_sub(); |
2089 | if (flags && !(flags & OPpENTERSUB_INARGS)) { | |
eb7e169e | 2090 | if (gimme != G_LIST) |
1604cfb0 MS |
2091 | goto croak_cant_return; |
2092 | SETs(sv); | |
2093 | RETURN; | |
2094 | } | |
a0d0e21e LW |
2095 | } |
2096 | ||
17ab7946 | 2097 | if (is_pp_rv2av) { |
1604cfb0 | 2098 | AV *const av = MUTABLE_AV(sv); |
0b5aba47 | 2099 | |
eb7e169e | 2100 | if (gimme == G_LIST) { |
d5524600 DM |
2101 | SP--; |
2102 | PUTBACK; | |
0b5aba47 | 2103 | return S_pushav(aTHX_ av); |
1604cfb0 | 2104 | } |
0b5aba47 | 2105 | |
1604cfb0 MS |
2106 | if (gimme == G_SCALAR) { |
2107 | const SSize_t maxarg = AvFILL(av) + 1; | |
7be75ccf DM |
2108 | if (PL_op->op_private & OPpTRUEBOOL) |
2109 | SETs(maxarg ? &PL_sv_yes : &PL_sv_zero); | |
2110 | else { | |
2111 | dTARGET; | |
2112 | SETi(maxarg); | |
2113 | } | |
1604cfb0 | 2114 | } |
7be75ccf DM |
2115 | } |
2116 | else { | |
aa36782f DM |
2117 | SP--; PUTBACK; |
2118 | return S_padhv_rv2hv_common(aTHX_ (HV*)sv, gimme, | |
e84e4286 DM |
2119 | cBOOL(PL_op->op_private & OPpRV2HV_ISKEYS), |
2120 | 1 /* has_targ*/); | |
17ab7946 | 2121 | } |
be85d344 | 2122 | RETURN; |
042560a6 NC |
2123 | |
2124 | croak_cant_return: | |
2125 | Perl_croak(aTHX_ "Can't return %s to lvalue scalar context", | |
1604cfb0 | 2126 | is_pp_rv2av ? "array" : "hash"); |
77e217c6 | 2127 | RETURN; |
a0d0e21e LW |
2128 | } |
2129 | ||
10c8fecd | 2130 | STATIC void |
fb8f4cf8 | 2131 | S_do_oddball(pTHX_ SV **oddkey, SV **firstkey) |
10c8fecd | 2132 | { |
7918f24d NC |
2133 | PERL_ARGS_ASSERT_DO_ODDBALL; |
2134 | ||
fb8f4cf8 | 2135 | if (*oddkey) { |
6d822dc4 | 2136 | if (ckWARN(WARN_MISC)) { |
1604cfb0 MS |
2137 | const char *err; |
2138 | if (oddkey == firstkey && | |
2139 | SvROK(*oddkey) && | |
2140 | (SvTYPE(SvRV(*oddkey)) == SVt_PVAV || | |
2141 | SvTYPE(SvRV(*oddkey)) == SVt_PVHV)) | |
2142 | { | |
2143 | err = "Reference found where even-sized list expected"; | |
2144 | } | |
2145 | else | |
2146 | err = "Odd number of elements in hash assignment"; | |
2147 | Perl_warner(aTHX_ packWARN(WARN_MISC), "%s", err); | |
2148 | } | |
6d822dc4 | 2149 | |
10c8fecd GS |
2150 | } |
2151 | } | |
2152 | ||
a5f48505 DM |
2153 | |
2154 | /* Do a mark and sweep with the SVf_BREAK flag to detect elements which | |
2155 | * are common to both the LHS and RHS of an aassign, and replace them | |
2156 | * with copies. All these copies are made before the actual list assign is | |
2157 | * done. | |
2158 | * | |
2159 | * For example in ($a,$b) = ($b,$a), assigning the value of the first RHS | |
2160 | * element ($b) to the first LH element ($a), modifies $a; when the | |
2161 | * second assignment is done, the second RH element now has the wrong | |
2162 | * value. So we initially replace the RHS with ($b, mortalcopy($a)). | |
2163 | * Note that we don't need to make a mortal copy of $b. | |
2164 | * | |
2165 | * The algorithm below works by, for every RHS element, mark the | |
2166 | * corresponding LHS target element with SVf_BREAK. Then if the RHS | |
2167 | * element is found with SVf_BREAK set, it means it would have been | |
2168 | * modified, so make a copy. | |
2169 | * Note that by scanning both LHS and RHS in lockstep, we avoid | |
2170 | * unnecessary copies (like $b above) compared with a naive | |
2171 | * "mark all LHS; copy all marked RHS; unmark all LHS". | |
2172 | * | |
2173 | * If the LHS element is a 'my' declaration' and has a refcount of 1, then | |
2174 | * it can't be common and can be skipped. | |
ebc643ce DM |
2175 | * |
2176 | * On DEBUGGING builds it takes an extra boolean, fake. If true, it means | |
2177 | * that we thought we didn't need to call S_aassign_copy_common(), but we | |
2178 | * have anyway for sanity checking. If we find we need to copy, then panic. | |
a5f48505 DM |
2179 | */ |
2180 | ||
2181 | PERL_STATIC_INLINE void | |
2182 | S_aassign_copy_common(pTHX_ SV **firstlelem, SV **lastlelem, | |
ebc643ce DM |
2183 | SV **firstrelem, SV **lastrelem |
2184 | #ifdef DEBUGGING | |
2185 | , bool fake | |
2186 | #endif | |
2187 | ) | |
a5f48505 | 2188 | { |
a5f48505 DM |
2189 | SV **relem; |
2190 | SV **lelem; | |
2191 | SSize_t lcount = lastlelem - firstlelem + 1; | |
2192 | bool marked = FALSE; /* have we marked any LHS with SVf_BREAK ? */ | |
2193 | bool const do_rc1 = cBOOL(PL_op->op_private & OPpASSIGN_COMMON_RC1); | |
beb08a1e | 2194 | bool copy_all = FALSE; |
a5f48505 DM |
2195 | |
2196 | assert(!PL_in_clean_all); /* SVf_BREAK not already in use */ | |
2197 | assert(firstlelem < lastlelem); /* at least 2 LH elements */ | |
2198 | assert(firstrelem < lastrelem); /* at least 2 RH elements */ | |
2199 | ||
ebc643ce DM |
2200 | |
2201 | lelem = firstlelem; | |
a5f48505 DM |
2202 | /* we never have to copy the first RH element; it can't be corrupted |
2203 | * by assigning something to the corresponding first LH element. | |
2204 | * So this scan does in a loop: mark LHS[N]; test RHS[N+1] | |
2205 | */ | |
ebc643ce | 2206 | relem = firstrelem + 1; |
a5f48505 DM |
2207 | |
2208 | for (; relem <= lastrelem; relem++) { | |
2209 | SV *svr; | |
2210 | ||
2211 | /* mark next LH element */ | |
2212 | ||
2213 | if (--lcount >= 0) { | |
2214 | SV *svl = *lelem++; | |
2215 | ||
2216 | if (UNLIKELY(!svl)) {/* skip AV alias marker */ | |
2217 | assert (lelem <= lastlelem); | |
2218 | svl = *lelem++; | |
2219 | lcount--; | |
2220 | } | |
2221 | ||
2222 | assert(svl); | |
beb08a1e TC |
2223 | if (SvSMAGICAL(svl)) { |
2224 | copy_all = TRUE; | |
2225 | } | |
a5f48505 DM |
2226 | if (SvTYPE(svl) == SVt_PVAV || SvTYPE(svl) == SVt_PVHV) { |
2227 | if (!marked) | |
2228 | return; | |
2229 | /* this LH element will consume all further args; | |
2230 | * no need to mark any further LH elements (if any). | |
2231 | * But we still need to scan any remaining RHS elements; | |
2232 | * set lcount negative to distinguish from lcount == 0, | |
2233 | * so the loop condition continues being true | |
2234 | */ | |
2235 | lcount = -1; | |
2236 | lelem--; /* no need to unmark this element */ | |
2237 | } | |
94a5f659 | 2238 | else if (!(do_rc1 && SvREFCNT(svl) == 1) && !SvIMMORTAL(svl)) { |
a5f48505 DM |
2239 | SvFLAGS(svl) |= SVf_BREAK; |
2240 | marked = TRUE; | |
2241 | } | |
2242 | else if (!marked) { | |
2243 | /* don't check RH element if no SVf_BREAK flags set yet */ | |
2244 | if (!lcount) | |
2245 | break; | |
2246 | continue; | |
2247 | } | |
2248 | } | |
2249 | ||
2250 | /* see if corresponding RH element needs copying */ | |
2251 | ||
2252 | assert(marked); | |
2253 | svr = *relem; | |
2254 | assert(svr); | |
2255 | ||
5c1db569 | 2256 | if (UNLIKELY(SvFLAGS(svr) & (SVf_BREAK|SVs_GMG) || copy_all)) { |
1050723f | 2257 | U32 brk = (SvFLAGS(svr) & SVf_BREAK); |
a5f48505 | 2258 | |
ebc643ce DM |
2259 | #ifdef DEBUGGING |
2260 | if (fake) { | |
9ae0115f | 2261 | /* op_dump(PL_op); */ |
ebc643ce DM |
2262 | Perl_croak(aTHX_ |
2263 | "panic: aassign skipped needed copy of common RH elem %" | |
2264 | UVuf, (UV)(relem - firstrelem)); | |
2265 | } | |
2266 | #endif | |
2267 | ||
a5f48505 DM |
2268 | TAINT_NOT; /* Each item is independent */ |
2269 | ||
2270 | /* Dear TODO test in t/op/sort.t, I love you. | |
2271 | (It's relying on a panic, not a "semi-panic" from newSVsv() | |
2272 | and then an assertion failure below.) */ | |
2273 | if (UNLIKELY(SvIS_FREED(svr))) { | |
2274 | Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p", | |
2275 | (void*)svr); | |
2276 | } | |
2277 | /* avoid break flag while copying; otherwise COW etc | |
2278 | * disabled... */ | |
2279 | SvFLAGS(svr) &= ~SVf_BREAK; | |
2280 | /* Not newSVsv(), as it does not allow copy-on-write, | |
8c1e192f DM |
2281 | resulting in wasteful copies. |
2282 | Also, we use SV_NOSTEAL in case the SV is used more than | |
2283 | once, e.g. (...) = (f())[0,0] | |
2284 | Where the same SV appears twice on the RHS without a ref | |
2285 | count bump. (Although I suspect that the SV won't be | |
2286 | stealable here anyway - DAPM). | |
2287 | */ | |
a5f48505 DM |
2288 | *relem = sv_mortalcopy_flags(svr, |
2289 | SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL); | |
2290 | /* ... but restore afterwards in case it's needed again, | |
2291 | * e.g. ($a,$b,$c) = (1,$a,$a) | |
2292 | */ | |
1050723f | 2293 | SvFLAGS(svr) |= brk; |
a5f48505 DM |
2294 | } |
2295 | ||
2296 | if (!lcount) | |
2297 | break; | |
2298 | } | |
2299 | ||
2300 | if (!marked) | |
2301 | return; | |
2302 | ||
2303 | /*unmark LHS */ | |
2304 | ||
2305 | while (lelem > firstlelem) { | |
2306 | SV * const svl = *(--lelem); | |
2307 | if (svl) | |
2308 | SvFLAGS(svl) &= ~SVf_BREAK; | |
2309 | } | |
2310 | } | |
2311 | ||
2312 | ||
2313 | ||
a0d0e21e LW |
2314 | PP(pp_aassign) |
2315 | { | |
c91f661c | 2316 | dSP; |
3280af22 NIS |
2317 | SV **lastlelem = PL_stack_sp; |
2318 | SV **lastrelem = PL_stack_base + POPMARK; | |
2319 | SV **firstrelem = PL_stack_base + POPMARK + 1; | |
a0d0e21e LW |
2320 | SV **firstlelem = lastrelem + 1; |
2321 | ||
eb578fdb KW |
2322 | SV **relem; |
2323 | SV **lelem; | |
1c23e2bd | 2324 | U8 gimme; |
a8c38aeb | 2325 | /* PL_delaymagic is restored by JMPENV_POP on dieing, so we |
a68090fe DM |
2326 | * only need to save locally, not on the save stack */ |
2327 | U16 old_delaymagic = PL_delaymagic; | |
ebc643ce DM |
2328 | #ifdef DEBUGGING |
2329 | bool fake = 0; | |
2330 | #endif | |
5637b936 | 2331 | |
3280af22 | 2332 | PL_delaymagic = DM_DELAY; /* catch simultaneous items */ |
a0d0e21e LW |
2333 | |
2334 | /* If there's a common identifier on both sides we have to take | |
2335 | * special care that assigning the identifier on the left doesn't | |
2336 | * clobber a value on the right that's used later in the list. | |
2337 | */ | |
acdea6f0 | 2338 | |
beb08a1e TC |
2339 | /* at least 2 LH and RH elements, or commonality isn't an issue */ |
2340 | if (firstlelem < lastlelem && firstrelem < lastrelem) { | |
5c1db569 TC |
2341 | for (relem = firstrelem+1; relem <= lastrelem; relem++) { |
2342 | if (SvGMAGICAL(*relem)) | |
2343 | goto do_scan; | |
2344 | } | |
beb08a1e TC |
2345 | for (lelem = firstlelem; lelem <= lastlelem; lelem++) { |
2346 | if (*lelem && SvSMAGICAL(*lelem)) | |
2347 | goto do_scan; | |
a5f48505 | 2348 | } |
beb08a1e TC |
2349 | if ( PL_op->op_private & (OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1) ) { |
2350 | if (PL_op->op_private & OPpASSIGN_COMMON_RC1) { | |
2351 | /* skip the scan if all scalars have a ref count of 1 */ | |
2352 | for (lelem = firstlelem; lelem <= lastlelem; lelem++) { | |
8b0c3377 | 2353 | SV *sv = *lelem; |
beb08a1e TC |
2354 | if (!sv || SvREFCNT(sv) == 1) |
2355 | continue; | |
2356 | if (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVAV) | |
2357 | goto do_scan; | |
2358 | break; | |
2359 | } | |
2360 | } | |
2361 | else { | |
2362 | do_scan: | |
2363 | S_aassign_copy_common(aTHX_ | |
2364 | firstlelem, lastlelem, firstrelem, lastrelem | |
ebc643ce | 2365 | #ifdef DEBUGGING |
beb08a1e | 2366 | , fake |
ebc643ce | 2367 | #endif |
beb08a1e TC |
2368 | ); |
2369 | } | |
a5f48505 | 2370 | } |
a0d0e21e | 2371 | } |
ebc643ce DM |
2372 | #ifdef DEBUGGING |
2373 | else { | |
2374 | /* on debugging builds, do the scan even if we've concluded we | |
2375 | * don't need to, then panic if we find commonality. Note that the | |
2376 | * scanner assumes at least 2 elements */ | |
2377 | if (firstlelem < lastlelem && firstrelem < lastrelem) { | |
2378 | fake = 1; | |
2379 | goto do_scan; | |
2380 | } | |
2381 | } | |
2382 | #endif | |
a0d0e21e | 2383 | |
a5f48505 | 2384 | gimme = GIMME_V; |
a0d0e21e LW |
2385 | relem = firstrelem; |
2386 | lelem = firstlelem; | |
10c8fecd | 2387 | |
8b0c3377 DM |
2388 | if (relem > lastrelem) |
2389 | goto no_relems; | |
2390 | ||
2391 | /* first lelem loop while there are still relems */ | |
5d9574c1 | 2392 | while (LIKELY(lelem <= lastlelem)) { |
1604cfb0 MS |
2393 | bool alias = FALSE; |
2394 | SV *lsv = *lelem++; | |
8b0c3377 | 2395 | |
c73f612f DM |
2396 | TAINT_NOT; /* Each item stands on its own, taintwise. */ |
2397 | ||
8b0c3377 | 2398 | assert(relem <= lastrelem); |
1604cfb0 MS |
2399 | if (UNLIKELY(!lsv)) { |
2400 | alias = TRUE; | |
2401 | lsv = *lelem++; | |
2402 | ASSUME(SvTYPE(lsv) == SVt_PVAV); | |
2403 | } | |
2404 | ||
2405 | switch (SvTYPE(lsv)) { | |
2406 | case SVt_PVAV: { | |
8b0c3377 DM |
2407 | SV **svp; |
2408 | SSize_t i; | |
2409 | SSize_t tmps_base; | |
2410 | SSize_t nelems = lastrelem - relem + 1; | |
b09ed995 | 2411 | AV *ary = MUTABLE_AV(lsv); |
8b0c3377 DM |
2412 | |
2413 | /* Assigning to an aggregate is tricky. First there is the | |
2414 | * issue of commonality, e.g. @a = ($a[0]). Since the | |
2415 | * stack isn't refcounted, clearing @a prior to storing | |
2416 | * elements will free $a[0]. Similarly with | |
2417 | * sub FETCH { $status[$_[1]] } @status = @tied[0,1]; | |
2418 | * | |
2419 | * The way to avoid these issues is to make the copy of each | |
2420 | * SV (and we normally store a *copy* in the array) *before* | |
2421 | * clearing the array. But this has a problem in that | |
2422 | * if the code croaks during copying, the not-yet-stored copies | |
2423 | * could leak. One way to avoid this is to make all the copies | |
2424 | * mortal, but that's quite expensive. | |
2425 | * | |
2426 | * The current solution to these issues is to use a chunk | |
2427 | * of the tmps stack as a temporary refcounted-stack. SVs | |
2428 | * will be put on there during processing to avoid leaks, | |
2429 | * but will be removed again before the end of this block, | |
2430 | * so free_tmps() is never normally called. Also, the | |
2431 | * sv_refcnt of the SVs doesn't have to be manipulated, since | |
2432 | * the ownership of 1 reference count is transferred directly | |
2433 | * from the tmps stack to the AV when the SV is stored. | |
2434 | * | |
2435 | * We disarm slots in the temps stack by storing PL_sv_undef | |
2436 | * there: it doesn't matter if that SV's refcount is | |
2437 | * repeatedly decremented during a croak. But usually this is | |
2438 | * only an interim measure. By the end of this code block | |
2439 | * we try where possible to not leave any PL_sv_undef's on the | |
2440 | * tmps stack e.g. by shuffling newer entries down. | |
2441 | * | |
2442 | * There is one case where we don't copy: non-magical | |
2443 | * SvTEMP(sv)'s with a ref count of 1. The only owner of these | |
2444 | * is on the tmps stack, so its safe to directly steal the SV | |
2445 | * rather than copying. This is common in things like function | |
2446 | * returns, map etc, which all return a list of such SVs. | |
2447 | * | |
2448 | * Note however something like @a = (f())[0,0], where there is | |
2449 | * a danger of the same SV being shared: this avoided because | |
2450 | * when the SV is stored as $a[0], its ref count gets bumped, | |
2451 | * so the RC==1 test fails and the second element is copied | |
2452 | * instead. | |
2453 | * | |
2454 | * We also use one slot in the tmps stack to hold an extra | |
2455 | * ref to the array, to ensure it doesn't get prematurely | |
2456 | * freed. Again, this is removed before the end of this block. | |
2457 | * | |
2458 | * Note that OPpASSIGN_COMMON_AGG is used to flag a possible | |
2459 | * @a = ($a[0]) case, but the current implementation uses the | |
2460 | * same algorithm regardless, so ignores that flag. (It *is* | |
2461 | * used in the hash branch below, however). | |
2462 | */ | |
2463 | ||
2464 | /* Reserve slots for ary, plus the elems we're about to copy, | |
2465 | * then protect ary and temporarily void the remaining slots | |
2466 | * with &PL_sv_undef */ | |
2467 | EXTEND_MORTAL(nelems + 1); | |
2468 | PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(ary); | |
2469 | tmps_base = PL_tmps_ix + 1; | |
2470 | for (i = 0; i < nelems; i++) | |
2471 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; | |
2472 | PL_tmps_ix += nelems; | |
2473 | ||
2474 | /* Make a copy of each RHS elem and save on the tmps_stack | |
2475 | * (or pass through where we can optimise away the copy) */ | |
2476 | ||
2477 | if (UNLIKELY(alias)) { | |
eb7e169e | 2478 | U32 lval = (gimme == G_LIST) |
8b0c3377 | 2479 | ? (PL_op->op_flags & OPf_MOD || LVRET) : 0; |
a5f48505 | 2480 | for (svp = relem; svp <= lastrelem; svp++) { |
8b0c3377 DM |
2481 | SV *rsv = *svp; |
2482 | ||
2483 | SvGETMAGIC(rsv); | |
2484 | if (!SvROK(rsv)) | |
2485 | DIE(aTHX_ "Assigned value is not a reference"); | |
2486 | if (SvTYPE(SvRV(rsv)) > SVt_PVLV) | |
2487 | /* diag_listed_as: Assigned value is not %s reference */ | |
2488 | DIE(aTHX_ | |
2489 | "Assigned value is not a SCALAR reference"); | |
2490 | if (lval) | |
2491 | *svp = rsv = sv_mortalcopy(rsv); | |
2492 | /* XXX else check for weak refs? */ | |
2493 | rsv = SvREFCNT_inc_NN(SvRV(rsv)); | |
2494 | assert(tmps_base <= PL_tmps_max); | |
2495 | PL_tmps_stack[tmps_base++] = rsv; | |
a5f48505 | 2496 | } |
a5f48505 | 2497 | } |
8b0c3377 DM |
2498 | else { |
2499 | for (svp = relem; svp <= lastrelem; svp++) { | |
2500 | SV *rsv = *svp; | |
a5f48505 | 2501 | |
8b0c3377 DM |
2502 | if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) { |
2503 | /* can skip the copy */ | |
2504 | SvREFCNT_inc_simple_void_NN(rsv); | |
2505 | SvTEMP_off(rsv); | |
2506 | } | |
a5f48505 | 2507 | else { |
8b0c3377 | 2508 | SV *nsv; |
8c1e192f DM |
2509 | /* see comment in S_aassign_copy_common about |
2510 | * SV_NOSTEAL */ | |
f7f919a0 RL |
2511 | nsv = newSVsv_flags(rsv, |
2512 | (SV_DO_COW_SVSETSV|SV_NOSTEAL|SV_GMAGIC)); | |
8b0c3377 | 2513 | rsv = *svp = nsv; |
a5f48505 | 2514 | } |
8b0c3377 DM |
2515 | |
2516 | assert(tmps_base <= PL_tmps_max); | |
2517 | PL_tmps_stack[tmps_base++] = rsv; | |
2518 | } | |
2519 | } | |
2520 | ||
2521 | if (SvRMAGICAL(ary) || AvFILLp(ary) >= 0) /* may be non-empty */ | |
2522 | av_clear(ary); | |
2523 | ||
2524 | /* store in the array, the SVs that are in the tmps stack */ | |
2525 | ||
2526 | tmps_base -= nelems; | |
2527 | ||
80c1439f | 2528 | if (SvMAGICAL(ary) || SvREADONLY(ary) || !AvREAL(ary)) { |
8b0c3377 DM |
2529 | /* for arrays we can't cheat with, use the official API */ |
2530 | av_extend(ary, nelems - 1); | |
2531 | for (i = 0; i < nelems; i++) { | |
2532 | SV **svp = &(PL_tmps_stack[tmps_base + i]); | |
2533 | SV *rsv = *svp; | |
2534 | /* A tied store won't take ownership of rsv, so keep | |
2535 | * the 1 refcnt on the tmps stack; otherwise disarm | |
2536 | * the tmps stack entry */ | |
2537 | if (av_store(ary, i, rsv)) | |
2538 | *svp = &PL_sv_undef; | |
2539 | /* av_store() may have added set magic to rsv */; | |
2540 | SvSETMAGIC(rsv); | |
2541 | } | |
2542 | /* disarm ary refcount: see comments below about leak */ | |
2543 | PL_tmps_stack[tmps_base - 1] = &PL_sv_undef; | |
2544 | } | |
2545 | else { | |
2546 | /* directly access/set the guts of the AV */ | |
2547 | SSize_t fill = nelems - 1; | |
2548 | if (fill > AvMAX(ary)) | |
2549 | av_extend_guts(ary, fill, &AvMAX(ary), &AvALLOC(ary), | |
2550 | &AvARRAY(ary)); | |
2551 | AvFILLp(ary) = fill; | |
2552 | Copy(&(PL_tmps_stack[tmps_base]), AvARRAY(ary), nelems, SV*); | |
2553 | /* Quietly remove all the SVs from the tmps stack slots, | |
2554 | * since ary has now taken ownership of the refcnt. | |
2555 | * Also remove ary: which will now leak if we die before | |
2556 | * the SvREFCNT_dec_NN(ary) below */ | |
2557 | if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems)) | |
2558 | Move(&PL_tmps_stack[tmps_base + nelems], | |
2559 | &PL_tmps_stack[tmps_base - 1], | |
2560 | PL_tmps_ix - (tmps_base + nelems) + 1, | |
2561 | SV*); | |
2562 | PL_tmps_ix -= (nelems + 1); | |
2563 | } | |
2564 | ||
1604cfb0 | 2565 | if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA)) |
8b0c3377 | 2566 | /* its assumed @ISA set magic can't die and leak ary */ |
1604cfb0 | 2567 | SvSETMAGIC(MUTABLE_SV(ary)); |
8b0c3377 DM |
2568 | SvREFCNT_dec_NN(ary); |
2569 | ||
2570 | relem = lastrelem + 1; | |
1604cfb0 | 2571 | goto no_relems; |
a5f48505 DM |
2572 | } |
2573 | ||
1604cfb0 | 2574 | case SVt_PVHV: { /* normal hash */ |
8b0c3377 DM |
2575 | |
2576 | SV **svp; | |
2577 | bool dirty_tmps; | |
2578 | SSize_t i; | |
2579 | SSize_t tmps_base; | |
2580 | SSize_t nelems = lastrelem - relem + 1; | |
b09ed995 | 2581 | HV *hash = MUTABLE_HV(lsv); |
8b0c3377 DM |
2582 | |
2583 | if (UNLIKELY(nelems & 1)) { | |
2584 | do_oddball(lastrelem, relem); | |
2585 | /* we have firstlelem to reuse, it's not needed any more */ | |
2586 | *++lastrelem = &PL_sv_undef; | |
2587 | nelems++; | |
2588 | } | |
2589 | ||
2590 | /* See the SVt_PVAV branch above for a long description of | |
2591 | * how the following all works. The main difference for hashes | |
2592 | * is that we treat keys and values separately (and have | |
2593 | * separate loops for them): as for arrays, values are always | |
2594 | * copied (except for the SvTEMP optimisation), since they | |
2595 | * need to be stored in the hash; while keys are only | |
2596 | * processed where they might get prematurely freed or | |
2597 | * whatever. */ | |
2598 | ||
2599 | /* tmps stack slots: | |
2600 | * * reserve a slot for the hash keepalive; | |
2601 | * * reserve slots for the hash values we're about to copy; | |
2602 | * * preallocate for the keys we'll possibly copy or refcount bump | |
2603 | * later; | |
2604 | * then protect hash and temporarily void the remaining | |
2605 | * value slots with &PL_sv_undef */ | |
2606 | EXTEND_MORTAL(nelems + 1); | |
2607 | ||
2608 | /* convert to number of key/value pairs */ | |
2609 | nelems >>= 1; | |
2610 | ||
2611 | PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(hash); | |
2612 | tmps_base = PL_tmps_ix + 1; | |
2613 | for (i = 0; i < nelems; i++) | |
2614 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; | |
2615 | PL_tmps_ix += nelems; | |
2616 | ||
2617 | /* Make a copy of each RHS hash value and save on the tmps_stack | |
2618 | * (or pass through where we can optimise away the copy) */ | |
2619 | ||
2620 | for (svp = relem + 1; svp <= lastrelem; svp += 2) { | |
2621 | SV *rsv = *svp; | |
2622 | ||
2623 | if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) { | |
2624 | /* can skip the copy */ | |
2625 | SvREFCNT_inc_simple_void_NN(rsv); | |
2626 | SvTEMP_off(rsv); | |
2627 | } | |
2628 | else { | |
2629 | SV *nsv; | |
8b0c3377 DM |
2630 | /* see comment in S_aassign_copy_common about |
2631 | * SV_NOSTEAL */ | |
f7f919a0 RL |
2632 | nsv = newSVsv_flags(rsv, |
2633 | (SV_DO_COW_SVSETSV|SV_NOSTEAL|SV_GMAGIC)); | |
8b0c3377 | 2634 | rsv = *svp = nsv; |
1c4ea384 RZ |
2635 | } |
2636 | ||
8b0c3377 DM |
2637 | assert(tmps_base <= PL_tmps_max); |
2638 | PL_tmps_stack[tmps_base++] = rsv; | |
2639 | } | |
2640 | tmps_base -= nelems; | |
a5f48505 | 2641 | |
a5f48505 | 2642 | |
8b0c3377 DM |
2643 | /* possibly protect keys */ |
2644 | ||
eb7e169e | 2645 | if (UNLIKELY(gimme == G_LIST)) { |
8b0c3377 DM |
2646 | /* handle e.g. |
2647 | * @a = ((%h = ($$r, 1)), $r = "x"); | |
2648 | * $_++ for %h = (1,2,3,4); | |
2649 | */ | |
2650 | EXTEND_MORTAL(nelems); | |
2651 | for (svp = relem; svp <= lastrelem; svp += 2) | |
2652 | *svp = sv_mortalcopy_flags(*svp, | |
2653 | SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL); | |
2654 | } | |
2655 | else if (PL_op->op_private & OPpASSIGN_COMMON_AGG) { | |
2656 | /* for possible commonality, e.g. | |
2657 | * %h = ($h{a},1) | |
2658 | * avoid premature freeing RHS keys by mortalising | |
2659 | * them. | |
2660 | * For a magic element, make a copy so that its magic is | |
2661 | * called *before* the hash is emptied (which may affect | |
2662 | * a tied value for example). | |
2663 | * In theory we should check for magic keys in all | |
2664 | * cases, not just under OPpASSIGN_COMMON_AGG, but in | |
2665 | * practice, !OPpASSIGN_COMMON_AGG implies only | |
2666 | * constants or padtmps on the RHS. | |
2667 | */ | |
2668 | EXTEND_MORTAL(nelems); | |
2669 | for (svp = relem; svp <= lastrelem; svp += 2) { | |
2670 | SV *rsv = *svp; | |
2671 | if (UNLIKELY(SvGMAGICAL(rsv))) { | |
2672 | SSize_t n; | |
a5f48505 DM |
2673 | *svp = sv_mortalcopy_flags(*svp, |
2674 | SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL); | |
8b0c3377 DM |
2675 | /* allow other branch to continue pushing |
2676 | * onto tmps stack without checking each time */ | |
2677 | n = (lastrelem - relem) >> 1; | |
2678 | EXTEND_MORTAL(n); | |
a5f48505 | 2679 | } |
8b0c3377 DM |
2680 | else |
2681 | PL_tmps_stack[++PL_tmps_ix] = | |
2682 | SvREFCNT_inc_simple_NN(rsv); | |
a5f48505 | 2683 | } |
8b0c3377 | 2684 | } |
a5f48505 | 2685 | |
8b0c3377 DM |
2686 | if (SvRMAGICAL(hash) || HvUSEDKEYS(hash)) |
2687 | hv_clear(hash); | |
a5f48505 | 2688 | |
e2737abb NC |
2689 | /* "nelems" was converted to the number of pairs earlier. */ |
2690 | if (nelems > PERL_HASH_DEFAULT_HvMAX) { | |
2691 | hv_ksplit(hash, nelems); | |
2692 | } | |
2693 | ||
8b0c3377 DM |
2694 | /* now assign the keys and values to the hash */ |
2695 | ||
2696 | dirty_tmps = FALSE; | |
2697 | ||
eb7e169e | 2698 | if (UNLIKELY(gimme == G_LIST)) { |
8b0c3377 DM |
2699 | /* @a = (%h = (...)) etc */ |
2700 | SV **svp; | |
2701 | SV **topelem = relem; | |
2702 | ||
2703 | for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) { | |
2704 | SV *key = *svp++; | |
2705 | SV *val = *svp; | |
2706 | /* remove duplicates from list we return */ | |
2707 | if (!hv_exists_ent(hash, key, 0)) { | |
2708 | /* copy key back: possibly to an earlier | |
2709 | * stack location if we encountered dups earlier, | |
2710 | * The values will be updated later | |
2711 | */ | |
2712 | *topelem = key; | |
2713 | topelem += 2; | |
632b9d6f | 2714 | } |
8b0c3377 DM |
2715 | /* A tied store won't take ownership of val, so keep |
2716 | * the 1 refcnt on the tmps stack; otherwise disarm | |
2717 | * the tmps stack entry */ | |
2718 | if (hv_store_ent(hash, key, val, 0)) | |
2719 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; | |
2720 | else | |
2721 | dirty_tmps = TRUE; | |
2722 | /* hv_store_ent() may have added set magic to val */; | |
2723 | SvSETMAGIC(val); | |
2724 | } | |
2725 | if (topelem < svp) { | |
1c4ea384 RZ |
2726 | /* at this point we have removed the duplicate key/value |
2727 | * pairs from the stack, but the remaining values may be | |
2728 | * wrong; i.e. with (a 1 a 2 b 3) on the stack we've removed | |
2729 | * the (a 2), but the stack now probably contains | |
2730 | * (a <freed> b 3), because { hv_save(a,1); hv_save(a,2) } | |
2731 | * obliterates the earlier key. So refresh all values. */ | |
8b0c3377 DM |
2732 | lastrelem = topelem - 1; |
2733 | while (relem < lastrelem) { | |
1c4ea384 RZ |
2734 | HE *he; |
2735 | he = hv_fetch_ent(hash, *relem++, 0, 0); | |
2736 | *relem++ = (he ? HeVAL(he) : &PL_sv_undef); | |
2737 | } | |
2738 | } | |
8b0c3377 DM |
2739 | } |
2740 | else { | |
2741 | SV **svp; | |
2742 | for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) { | |
2743 | SV *key = *svp++; | |
2744 | SV *val = *svp; | |
2745 | if (hv_store_ent(hash, key, val, 0)) | |
2746 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; | |
2747 | else | |
2748 | dirty_tmps = TRUE; | |
2749 | /* hv_store_ent() may have added set magic to val */; | |
2750 | SvSETMAGIC(val); | |
2751 | } | |
2752 | } | |
2753 | ||
2754 | if (dirty_tmps) { | |
2755 | /* there are still some 'live' recounts on the tmps stack | |
2756 | * - usually caused by storing into a tied hash. So let | |
2757 | * free_tmps() do the proper but slow job later. | |
2758 | * Just disarm hash refcount: see comments below about leak | |
2759 | */ | |
2760 | PL_tmps_stack[tmps_base - 1] = &PL_sv_undef; | |
2761 | } | |
2762 | else { | |
2763 | /* Quietly remove all the SVs from the tmps stack slots, | |
2764 | * since hash has now taken ownership of the refcnt. | |
2765 | * Also remove hash: which will now leak if we die before | |
2766 | * the SvREFCNT_dec_NN(hash) below */ | |
2767 | if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems)) | |
2768 | Move(&PL_tmps_stack[tmps_base + nelems], | |
2769 | &PL_tmps_stack[tmps_base - 1], | |
2770 | PL_tmps_ix - (tmps_base + nelems) + 1, | |
2771 | SV*); | |
2772 | PL_tmps_ix -= (nelems + 1); | |
2773 | } | |
2774 | ||
2775 | SvREFCNT_dec_NN(hash); | |
2776 | ||
2777 | relem = lastrelem + 1; | |
1604cfb0 MS |
2778 | goto no_relems; |
2779 | } | |
8b0c3377 | 2780 | |
1604cfb0 MS |
2781 | default: |
2782 | if (!SvIMMORTAL(lsv)) { | |
d24e3eb1 DM |
2783 | SV *ref; |
2784 | ||
8b0c3377 DM |
2785 | if (UNLIKELY( |
2786 | SvTEMP(lsv) && !SvSMAGICAL(lsv) && SvREFCNT(lsv) == 1 && | |
2787 | (!isGV_with_GP(lsv) || SvFAKE(lsv)) && ckWARN(WARN_MISC) | |
2788 | )) | |
2789 | Perl_warner(aTHX_ | |
2790 | packWARN(WARN_MISC), | |
2791 | "Useless assignment to a temporary" | |
2792 | ); | |
d24e3eb1 DM |
2793 | |
2794 | /* avoid freeing $$lsv if it might be needed for further | |
2795 | * elements, e.g. ($ref, $foo) = (1, $$ref) */ | |
2796 | if ( SvROK(lsv) | |
2797 | && ( ((ref = SvRV(lsv)), SvREFCNT(ref)) == 1) | |
2798 | && lelem <= lastlelem | |
2799 | ) { | |
2800 | SSize_t ix; | |
2801 | SvREFCNT_inc_simple_void_NN(ref); | |
2802 | /* an unrolled sv_2mortal */ | |
2803 | ix = ++PL_tmps_ix; | |
2804 | if (UNLIKELY(ix >= PL_tmps_max)) | |
2805 | /* speculatively grow enough to cover other | |
2806 | * possible refs */ | |
67c3640a | 2807 | (void)tmps_grow_p(ix + (lastlelem - lelem)); |
d24e3eb1 DM |
2808 | PL_tmps_stack[ix] = ref; |
2809 | } | |
2810 | ||
8b0c3377 DM |
2811 | sv_setsv(lsv, *relem); |
2812 | *relem = lsv; | |
2813 | SvSETMAGIC(lsv); | |
2814 | } | |
2815 | if (++relem > lastrelem) | |
2816 | goto no_relems; | |
1604cfb0 | 2817 | break; |
8b0c3377 DM |
2818 | } /* switch */ |
2819 | } /* while */ | |
2820 | ||
2821 | ||
2822 | no_relems: | |
2823 | ||
2824 | /* simplified lelem loop for when there are no relems left */ | |
2825 | while (LIKELY(lelem <= lastlelem)) { | |
1604cfb0 | 2826 | SV *lsv = *lelem++; |
c73f612f DM |
2827 | |
2828 | TAINT_NOT; /* Each item stands on its own, taintwise. */ | |
2829 | ||
1604cfb0 MS |
2830 | if (UNLIKELY(!lsv)) { |
2831 | lsv = *lelem++; | |
2832 | ASSUME(SvTYPE(lsv) == SVt_PVAV); | |
2833 | } | |
8b0c3377 | 2834 | |
1604cfb0 MS |
2835 | switch (SvTYPE(lsv)) { |
2836 | case SVt_PVAV: | |
b09ed995 DM |
2837 | if (SvRMAGICAL(lsv) || AvFILLp((SV*)lsv) >= 0) { |
2838 | av_clear((AV*)lsv); | |
8b0c3377 | 2839 | if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA)) |
b09ed995 | 2840 | SvSETMAGIC(lsv); |
8b0c3377 DM |
2841 | } |
2842 | break; | |
2843 | ||
1604cfb0 | 2844 | case SVt_PVHV: |
b09ed995 DM |
2845 | if (SvRMAGICAL(lsv) || HvUSEDKEYS((HV*)lsv)) |
2846 | hv_clear((HV*)lsv); | |
8b0c3377 DM |
2847 | break; |
2848 | ||
1604cfb0 MS |
2849 | default: |
2850 | if (!SvIMMORTAL(lsv)) { | |
e03e82a0 | 2851 | sv_set_undef(lsv); |
8b0c3377 DM |
2852 | SvSETMAGIC(lsv); |
2853 | } | |
282d9dfe | 2854 | *relem++ = lsv; |
1604cfb0 | 2855 | break; |
8b0c3377 DM |
2856 | } /* switch */ |
2857 | } /* while */ | |
2858 | ||
c73f612f DM |
2859 | TAINT_NOT; /* result of list assign isn't tainted */ |
2860 | ||
5d9574c1 | 2861 | if (UNLIKELY(PL_delaymagic & ~DM_DELAY)) { |
1604cfb0 MS |
2862 | /* Will be used to set PL_tainting below */ |
2863 | Uid_t tmp_uid = PerlProc_getuid(); | |
2864 | Uid_t tmp_euid = PerlProc_geteuid(); | |
2865 | Gid_t tmp_gid = PerlProc_getgid(); | |
2866 | Gid_t tmp_egid = PerlProc_getegid(); | |
985213f2 | 2867 | |
b469f1e0 | 2868 | /* XXX $> et al currently silently ignore failures */ |
1604cfb0 | 2869 | if (PL_delaymagic & DM_UID) { |
a0d0e21e | 2870 | #ifdef HAS_SETRESUID |
1604cfb0 | 2871 | PERL_UNUSED_RESULT( |
b469f1e0 JH |
2872 | setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1, |
2873 | (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1, | |
2874 | (Uid_t)-1)); | |
d1a21e44 | 2875 | #elif defined(HAS_SETREUID) |
b469f1e0 JH |
2876 | PERL_UNUSED_RESULT( |
2877 | setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1, | |
2878 | (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1)); | |
d1a21e44 | 2879 | #else |
56febc5e | 2880 | # ifdef HAS_SETRUID |
1604cfb0 MS |
2881 | if ((PL_delaymagic & DM_UID) == DM_RUID) { |
2882 | PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid)); | |
2883 | PL_delaymagic &= ~DM_RUID; | |
2884 | } | |
56febc5e AD |
2885 | # endif /* HAS_SETRUID */ |
2886 | # ifdef HAS_SETEUID | |
1604cfb0 MS |
2887 | if ((PL_delaymagic & DM_UID) == DM_EUID) { |
2888 | PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid)); | |
2889 | PL_delaymagic &= ~DM_EUID; | |
2890 | } | |
56febc5e | 2891 | # endif /* HAS_SETEUID */ |
1604cfb0 MS |
2892 | if (PL_delaymagic & DM_UID) { |
2893 | if (PL_delaymagic_uid != PL_delaymagic_euid) | |
2894 | DIE(aTHX_ "No setreuid available"); | |
2895 | PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid)); | |
2896 | } | |
56febc5e | 2897 | #endif /* HAS_SETRESUID */ |
04783dc7 | 2898 | |
1604cfb0 MS |
2899 | tmp_uid = PerlProc_getuid(); |
2900 | tmp_euid = PerlProc_geteuid(); | |
2901 | } | |
b469f1e0 | 2902 | /* XXX $> et al currently silently ignore failures */ |
1604cfb0 | 2903 | if (PL_delaymagic & DM_GID) { |
a0d0e21e | 2904 | #ifdef HAS_SETRESGID |
1604cfb0 | 2905 | PERL_UNUSED_RESULT( |
b469f1e0 JH |
2906 | setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1, |
2907 | (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1, | |
2908 | (Gid_t)-1)); | |
d1a21e44 | 2909 | #elif defined(HAS_SETREGID) |
1604cfb0 | 2910 | PERL_UNUSED_RESULT( |
b469f1e0 JH |
2911 | setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1, |
2912 | (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1)); | |
d1a21e44 | 2913 | #else |
56febc5e | 2914 | # ifdef HAS_SETRGID |
1604cfb0 MS |
2915 | if ((PL_delaymagic & DM_GID) == DM_RGID) { |
2916 | PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid)); | |
2917 | PL_delaymagic &= ~DM_RGID; | |
2918 | } | |
56febc5e AD |
2919 | # endif /* HAS_SETRGID */ |
2920 | # ifdef HAS_SETEGID | |
1604cfb0 MS |
2921 | if ((PL_delaymagic & DM_GID) == DM_EGID) { |
2922 | PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid)); | |
2923 | PL_delaymagic &= ~DM_EGID; | |
2924 | } | |
56febc5e | 2925 | # endif /* HAS_SETEGID */ |
1604cfb0 MS |
2926 | if (PL_delaymagic & DM_GID) { |
2927 | if (PL_delaymagic_gid != PL_delaymagic_egid) | |
2928 | DIE(aTHX_ "No setregid available"); | |
2929 | PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid)); | |
2930 | } | |
56febc5e | 2931 | #endif /* HAS_SETRESGID */ |
04783dc7 | 2932 | |
1604cfb0 MS |
2933 | tmp_gid = PerlProc_getgid(); |
2934 | tmp_egid = PerlProc_getegid(); | |
2935 | } | |
2936 | TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) ); | |
9a9b5ec9 DM |
2937 | #ifdef NO_TAINT_SUPPORT |
2938 | PERL_UNUSED_VAR(tmp_uid); | |
2939 | PERL_UNUSED_VAR(tmp_euid); | |
2940 | PERL_UNUSED_VAR(tmp_gid); | |
2941 | PERL_UNUSED_VAR(tmp_egid); | |
2942 | #endif | |
a0d0e21e | 2943 | } |
a68090fe | 2944 | PL_delaymagic = old_delaymagic; |
54310121 | 2945 | |
54310121 | 2946 | if (gimme == G_VOID) |
1604cfb0 | 2947 | SP = firstrelem - 1; |
54310121 | 2948 | else if (gimme == G_SCALAR) { |
1604cfb0 | 2949 | SP = firstrelem; |
b09ed995 | 2950 | EXTEND(SP,1); |
7b394f12 DM |
2951 | if (PL_op->op_private & OPpASSIGN_TRUEBOOL) |
2952 | SETs((firstlelem - firstrelem) ? &PL_sv_yes : &PL_sv_zero); | |
2953 | else { | |
2954 | dTARGET; | |
2955 | SETi(firstlelem - firstrelem); | |
2956 | } | |
54310121 | 2957 | } |
b09ed995 DM |
2958 | else |
2959 | SP = relem - 1; | |
08aeb9f7 | 2960 | |
54310121 | 2961 | RETURN; |
a0d0e21e LW |
2962 | } |
2963 | ||
8782bef2 GB |
2964 | PP(pp_qr) |
2965 | { | |
20b7effb | 2966 | dSP; |
eb578fdb | 2967 | PMOP * const pm = cPMOP; |
fe578d7f | 2968 | REGEXP * rx = PM_GETRE(pm); |
196a02af DM |
2969 | regexp *prog = ReANY(rx); |
2970 | SV * const pkg = RXp_ENGINE(prog)->qr_package(aTHX_ (rx)); | |
7ea8b04b | 2971 | SV * const rv = newSV_type_mortal(SVt_IV); |
d63c20f2 DM |
2972 | CV **cvp; |
2973 | CV *cv; | |
288b8c02 NC |
2974 | |
2975 | SvUPGRADE(rv, SVt_IV); | |
c2123ae3 NC |
2976 | /* For a subroutine describing itself as "This is a hacky workaround" I'm |
2977 | loathe to use it here, but it seems to be the right fix. Or close. | |
2978 | The key part appears to be that it's essential for pp_qr to return a new | |
2979 | object (SV), which implies that there needs to be an effective way to | |
2980 | generate a new SV from the existing SV that is pre-compiled in the | |
2981 | optree. */ | |
2982 | SvRV_set(rv, MUTABLE_SV(reg_temp_copy(NULL, rx))); | |
288b8c02 NC |
2983 | SvROK_on(rv); |
2984 | ||
8d919b0a | 2985 | cvp = &( ReANY((REGEXP *)SvRV(rv))->qr_anoncv); |
5d9574c1 | 2986 | if (UNLIKELY((cv = *cvp) && CvCLONE(*cvp))) { |
1604cfb0 MS |
2987 | *cvp = cv_clone(cv); |
2988 | SvREFCNT_dec_NN(cv); | |
d63c20f2 DM |
2989 | } |
2990 | ||
288b8c02 | 2991 | if (pkg) { |
1604cfb0 MS |
2992 | HV *const stash = gv_stashsv(pkg, GV_ADD); |
2993 | SvREFCNT_dec_NN(pkg); | |
2994 | (void)sv_bless(rv, stash); | |
288b8c02 NC |
2995 | } |
2996 | ||
196a02af | 2997 | if (UNLIKELY(RXp_ISTAINTED(prog))) { |
e08e52cf | 2998 | SvTAINTED_on(rv); |
9274aefd DM |
2999 | SvTAINTED_on(SvRV(rv)); |
3000 | } | |
c8c13c22 JB |
3001 | XPUSHs(rv); |
3002 | RETURN; | |
8782bef2 GB |
3003 | } |
3004 | ||
e0be7821 KW |
3005 | STATIC bool |
3006 | S_are_we_in_Debug_EXECUTE_r(pTHX) | |
3007 | { | |
3008 | /* Given a 'use re' is in effect, does it ask for outputting execution | |
3009 | * debug info? | |
3010 | * | |
3011 | * This is separated from the sole place it's called, an inline function, | |
3012 | * because it is the large-ish slow portion of the function */ | |
3013 | ||
3014 | DECLARE_AND_GET_RE_DEBUG_FLAGS_NON_REGEX; | |
3015 | ||
3016 | return cBOOL(RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MASK)); | |
3017 | } | |
3018 | ||
3019 | PERL_STATIC_INLINE bool | |
3020 | S_should_we_output_Debug_r(pTHX_ regexp *prog) | |
3021 | { | |
3022 | PERL_ARGS_ASSERT_SHOULD_WE_OUTPUT_DEBUG_R; | |
3023 | ||
3024 | /* pp_match can output regex debugging info. This function returns a | |
3025 | * boolean as to whether or not it should. | |
3026 | * | |
3027 | * Under -Dr, it should. Any reasonable compiler will optimize this bit of | |
3028 | * code away on non-debugging builds. */ | |
3029 | if (UNLIKELY(DEBUG_r_TEST)) { | |
3030 | return TRUE; | |
3031 | } | |
3032 | ||
3033 | /* If the regex engine is using the non-debugging execution routine, then | |
3034 | * no debugging should be output. Same if the field is NULL that pluggable | |
3035 | * engines are not supposed to fill. */ | |
3036 | if ( LIKELY(prog->engine->exec == &Perl_regexec_flags) | |
3037 | || UNLIKELY(prog->engine->op_comp == NULL)) | |
3038 | { | |
3039 | return FALSE; | |
3040 | } | |
3041 | ||
3042 | /* Otherwise have to check */ | |
3043 | return S_are_we_in_Debug_EXECUTE_r(aTHX); | |
3044 | } | |
3045 | ||
a0d0e21e LW |
3046 | PP(pp_match) |
3047 | { | |
20b7effb | 3048 | dSP; dTARG; |
eb578fdb | 3049 | PMOP *pm = cPMOP; |
d65afb4b | 3050 | PMOP *dynpm = pm; |
eb578fdb | 3051 | const char *s; |
5c144d81 | 3052 | const char *strend; |
99a90e59 | 3053 | SSize_t curpos = 0; /* initial pos() or current $+[0] */ |
a0d0e21e | 3054 | I32 global; |
7fadf4a7 | 3055 | U8 r_flags = 0; |
5c144d81 | 3056 | const char *truebase; /* Start of string */ |
eb578fdb | 3057 | REGEXP *rx = PM_GETRE(pm); |
196a02af | 3058 | regexp *prog = ReANY(rx); |
b3eb6a9b | 3059 | bool rxtainted; |
1c23e2bd | 3060 | const U8 gimme = GIMME_V; |
a0d0e21e | 3061 | STRLEN len; |
a3b680e6 | 3062 | const I32 oldsave = PL_savestack_ix; |
e60df1fa | 3063 | I32 had_zerolen = 0; |
b1422d62 | 3064 | MAGIC *mg = NULL; |
a0d0e21e | 3065 | |
533c011a | 3066 | if (PL_op->op_flags & OPf_STACKED) |
1604cfb0 | 3067 | TARG = POPs; |
a0d0e21e | 3068 | else { |
9399c607 DM |
3069 | if (ARGTARG) |
3070 | GETTARGET; | |
3071 | else { | |
3072 | TARG = DEFSV; | |
3073 | } | |
1604cfb0 | 3074 | EXTEND(SP,1); |
a0d0e21e | 3075 | } |
d9f424b2 | 3076 | |
c277df42 | 3077 | PUTBACK; /* EVAL blocks need stack_sp. */ |
69dc4b30 FC |
3078 | /* Skip get-magic if this is a qr// clone, because regcomp has |
3079 | already done it. */ | |
196a02af | 3080 | truebase = prog->mother_re |
1604cfb0 MS |
3081 | ? SvPV_nomg_const(TARG, len) |
3082 | : SvPV_const(TARG, len); | |
f1d31338 | 3083 | if (!truebase) |
1604cfb0 | 3084 | DIE(aTHX_ "panic: pp_match"); |
f1d31338 | 3085 | strend = truebase + len; |
196a02af | 3086 | rxtainted = (RXp_ISTAINTED(prog) || |
1604cfb0 | 3087 | (TAINT_get && (pm->op_pmflags & PMf_RETAINT))); |
9212bbba | 3088 | TAINT_NOT; |
a0d0e21e | 3089 | |
6c864ec2 | 3090 | /* We need to know this in case we fail out early - pos() must be reset */ |
de0df3c0 MH |
3091 | global = dynpm->op_pmflags & PMf_GLOBAL; |
3092 | ||
d65afb4b | 3093 | /* PMdf_USED is set after a ?? matches once */ |
c737faaf YO |
3094 | if ( |
3095 | #ifdef USE_ITHREADS | |
3096 | SvREADONLY(PL_regex_pad[pm->op_pmoffset]) | |
3097 | #else | |
3098 | pm->op_pmflags & PMf_USED | |
3099 | #endif | |
3100 | ) { | |
e0be7821 KW |
3101 | if (UNLIKELY(should_we_output_Debug_r(prog))) { |
3102 | PerlIO_printf(Perl_debug_log, "?? already matched once"); | |
3103 | } | |
1604cfb0 | 3104 | goto nope; |
a0d0e21e LW |
3105 | } |
3106 | ||
5585e758 | 3107 | /* handle the empty pattern */ |
196a02af | 3108 | if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) { |
5585e758 YO |
3109 | if (PL_curpm == PL_reg_curpm) { |
3110 | if (PL_curpm_under) { | |
3111 | if (PL_curpm_under == PL_reg_curpm) { | |
3112 | Perl_croak(aTHX_ "Infinite recursion via empty pattern"); | |
3113 | } else { | |
3114 | pm = PL_curpm_under; | |
3115 | } | |
3116 | } | |
3117 | } else { | |
3118 | pm = PL_curpm; | |
3119 | } | |
3120 | rx = PM_GETRE(pm); | |
196a02af | 3121 | prog = ReANY(rx); |
a0d0e21e | 3122 | } |
d65afb4b | 3123 | |
196a02af | 3124 | if (RXp_MINLEN(prog) >= 0 && (STRLEN)RXp_MINLEN(prog) > len) { |
e0be7821 KW |
3125 | if (UNLIKELY(should_we_output_Debug_r(prog))) { |
3126 | PerlIO_printf(Perl_debug_log, | |
3f5ee3fa | 3127 | "String shorter than min possible regex match (%zd < %zd)\n", |
e0be7821 KW |
3128 | len, RXp_MINLEN(prog)); |
3129 | } | |
1604cfb0 | 3130 | goto nope; |
e5dc5375 | 3131 | } |
c277df42 | 3132 | |
8ef97b0e | 3133 | /* get pos() if //g */ |
de0df3c0 | 3134 | if (global) { |
b1422d62 | 3135 | mg = mg_find_mglob(TARG); |
8ef97b0e | 3136 | if (mg && mg->mg_len >= 0) { |
25fdce4a | 3137 | curpos = MgBYTEPOS(mg, TARG, truebase, len); |
8ef97b0e DM |
3138 | /* last time pos() was set, it was zero-length match */ |
3139 | if (mg->mg_flags & MGf_MINMATCH) | |
3140 | had_zerolen = 1; | |
3141 | } | |
a0d0e21e | 3142 | } |
8ef97b0e | 3143 | |
6e240d0b | 3144 | #ifdef PERL_SAWAMPERSAND |
196a02af | 3145 | if ( RXp_NPARENS(prog) |
6502e081 | 3146 | || PL_sawampersand |
196a02af | 3147 | || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY)) |
5b0e71e9 | 3148 | || (dynpm->op_pmflags & PMf_KEEPCOPY) |
6e240d0b FC |
3149 | ) |
3150 | #endif | |
3151 | { | |
1604cfb0 | 3152 | r_flags |= (REXEC_COPY_STR|REXEC_COPY_SKIP_PRE); |
6502e081 DM |
3153 | /* in @a =~ /(.)/g, we iterate multiple times, but copy the buffer |
3154 | * only on the first iteration. Therefore we need to copy $' as well | |
3155 | * as $&, to make the rest of the string available for captures in | |
3156 | * subsequent iterations */ | |
eb7e169e | 3157 | if (! (global && gimme == G_LIST)) |
6502e081 DM |
3158 | r_flags |= REXEC_COPY_SKIP_POST; |
3159 | }; | |
5b0e71e9 DM |
3160 | #ifdef PERL_SAWAMPERSAND |
3161 | if (dynpm->op_pmflags & PMf_KEEPCOPY) | |
3162 | /* handle KEEPCOPY in pmop but not rx, eg $r=qr/a/; /$r/p */ | |
3163 | r_flags &= ~(REXEC_COPY_SKIP_PRE|REXEC_COPY_SKIP_POST); | |
3164 | #endif | |
22e551b9 | 3165 | |
f1d31338 DM |
3166 | s = truebase; |
3167 | ||
d7be1480 | 3168 | play_it_again: |
985afbc1 | 3169 | if (global) |
1604cfb0 | 3170 | s = truebase + curpos; |
f722798b | 3171 | |
77da2310 | 3172 | if (!CALLREGEXEC(rx, (char*)s, (char *)strend, (char*)truebase, |
1604cfb0 MS |
3173 | had_zerolen, TARG, NULL, r_flags)) |
3174 | goto nope; | |
77da2310 NC |
3175 | |
3176 | PL_curpm = pm; | |
985afbc1 | 3177 | if (dynpm->op_pmflags & PMf_ONCE) |
c737faaf | 3178 | #ifdef USE_ITHREADS |
1604cfb0 | 3179 | SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]); |
c737faaf | 3180 | #else |
1604cfb0 | 3181 | dynpm->op_pmflags |= PMf_USED; |
c737faaf | 3182 | #endif |
a0d0e21e | 3183 | |
72311751 | 3184 | if (rxtainted) |
1604cfb0 | 3185 | RXp_MATCH_TAINTED_on(prog); |
196a02af | 3186 | TAINT_IF(RXp_MATCH_TAINTED(prog)); |
35c2ccc3 DM |
3187 | |
3188 | /* update pos */ | |
3189 | ||
eb7e169e | 3190 | if (global && (gimme != G_LIST || (dynpm->op_pmflags & PMf_CONTINUE))) { |
b1422d62 | 3191 | if (!mg) |
35c2ccc3 | 3192 | mg = sv_magicext_mglob(TARG); |
3f11a285 | 3193 | MgBYTEPOS_set(mg, TARG, truebase, RXp_OFFS_END(prog,0)); |
196a02af | 3194 | if (RXp_ZERO_LEN(prog)) |
adf51885 DM |
3195 | mg->mg_flags |= MGf_MINMATCH; |
3196 | else | |
3197 | mg->mg_flags &= ~MGf_MINMATCH; | |
35c2ccc3 DM |
3198 | } |
3199 | ||
eb7e169e | 3200 | if ((!RXp_NPARENS(prog) && !global) || gimme != G_LIST) { |
1604cfb0 MS |
3201 | LEAVE_SCOPE(oldsave); |
3202 | RETPUSHYES; | |
bf9dff51 DM |
3203 | } |
3204 | ||
88ab22af DM |
3205 | /* push captures on stack */ |
3206 | ||
bf9dff51 | 3207 | { |
7ede1e31 YO |
3208 | const I32 logical_nparens = RXp_LOGICAL_NPARENS(prog); |
3209 | /* This following statement is *devious* code. If we are in a global | |
3210 | match and the pattern has no parens in it we should return $& | |
3211 | (offset pair 0). So we set logical_paren to 1 when we should return | |
3212 | $&, otherwise we set it to 0. | |
3213 | ||
3214 | This allows us to simply add logical_nparens to logical_paren to | |
3215 | compute the number of elements we are going to return. | |
3216 | ||
3217 | In the loop intit we "not" it with: logical_paren = !logical_paren | |
3218 | which results in it being 0 inside the loop when we want to return | |
3219 | $&, and results in it being 1 when we want to return the parens. | |
3220 | Thus we either loop over 1..logical_nparens, or just over 0. | |
3221 | ||
3222 | This is an elegant way to do this code wise, but is super devious | |
3223 | and potentially confusing. When I first saw this logic I thought | |
3224 | "WTF?". But it makes sense after you poke it a while. | |
3225 | ||
3226 | Frankly I probably would have done it differently, but it works so | |
3227 | I am leaving it. - Yves */ | |
3228 | I32 logical_paren = (global && !logical_nparens) ? 1 : 0; | |
3229 | I32 *l2p = RXp_LOGICAL_TO_PARNO(prog); | |
3230 | /* this is used to step through the physical parens associated | |
3231 | * with a given logical paren. */ | |
3232 | I32 *p2l_next = RXp_PARNO_TO_LOGICAL_NEXT(prog); | |
1604cfb0 MS |
3233 | |
3234 | SPAGAIN; /* EVAL blocks could move the stack. */ | |
7ede1e31 YO |
3235 | EXTEND(SP, logical_nparens + logical_paren); /* devious code ... */ |
3236 | EXTEND_MORTAL(logical_nparens + logical_paren); /* ... see above */ | |
3237 | ||
3238 | /* loop over the logical parens in the pattern. This may not | |
3239 | correspond to the actual paren checked, as branch reset may | |
3240 | mean that there is more than one paren "behind" the logical | |
3241 | parens. Eg, in /(?|(a)|(b))/ there are two parens, but one | |
3242 | logical paren. */ | |
3243 | for (logical_paren = !logical_paren; | |
3244 | logical_paren <= logical_nparens; | |
3245 | logical_paren++) | |
3246 | { | |
3247 | /* now convert the logical_paren to the physical parens which | |
3248 | are "behind" it. If branch reset was not used then | |
3249 | physical_paren and logical_paren are the same as each other | |
3250 | and we will only perform one iteration of the loop */ | |
3251 | I32 phys_paren = l2p ? l2p[logical_paren] : logical_paren; | |
3252 | SSize_t offs_start, offs_end; | |
3253 | /* We check the loop invariants below and break out of the loop | |
3254 | explicitly if our checks fail, so we use while (1) here to | |
3255 | avoid double testing a conditional. */ | |
3256 | while (1) { | |
3257 | /* Check end offset first, as the start might be >=0 even | |
3258 | though the end is -1, so testing the end first helps | |
3259 | use avoid the start check. Really we should be able to | |
3260 | get away with ONLY testing the end, but testing both | |
3261 | doesn't hurt much and preserves sanity. */ | |
3262 | if (((offs_end = RXp_OFFS_END(prog, phys_paren)) != -1) && | |
3263 | ((offs_start = RXp_OFFS_START(prog, phys_paren)) != -1)) | |
3264 | { | |
3265 | const SSize_t len = offs_end - offs_start; | |
3266 | const char * const s = offs_start + truebase; | |
3267 | if ( UNLIKELY( len < 0 || len > strend - s) ) { | |
3268 | DIE(aTHX_ "panic: pp_match start/end pointers, paren=%" I32df ", " | |
3269 | "start=%zd, end=%zd, s=%p, strend=%p, len=%zd", | |
3270 | phys_paren, offs_start, offs_end, s, strend, len); | |
3271 | } | |
3272 | PUSHs(newSVpvn_flags(s, len, | |
3273 | (DO_UTF8(TARG)) | |
3274 | ? SVf_UTF8|SVs_TEMP | |
3275 | : SVs_TEMP) | |
3276 | ); | |
3277 | break; | |
3278 | } else if (!p2l_next || !(phys_paren = p2l_next[phys_paren])) { | |
3279 | /* Either logical_paren and phys_paren are the same and | |
3280 | we won't have a p2l_next, or they aren't the same (and | |
3281 | we do have a p2l_next) but we have exhausted the list | |
3282 | of physical parens associated with this logical paren. | |
3283 | Either way we are done, and we can push undef and break | |
3284 | out of the loop. */ | |
3285 | PUSHs(sv_newmortal()); | |
3286 | break; | |
3287 | } | |
1604cfb0 MS |
3288 | } |
3289 | } | |
3290 | if (global) { | |
3f11a285 | 3291 | curpos = (UV)RXp_OFFS_END(prog,0); |
1604cfb0 MS |
3292 | had_zerolen = RXp_ZERO_LEN(prog); |
3293 | PUTBACK; /* EVAL blocks may use stack */ | |
3294 | r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST; | |
3295 | goto play_it_again; | |
3296 | } | |
3297 | LEAVE_SCOPE(oldsave); | |
3298 | RETURN; | |
a0d0e21e | 3299 | } |
e5964223 | 3300 | NOT_REACHED; /* NOTREACHED */ |
a0d0e21e | 3301 | |
7b52d656 | 3302 | nope: |
d65afb4b | 3303 | if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) { |
b1422d62 DM |
3304 | if (!mg) |
3305 | mg = mg_find_mglob(TARG); | |
3306 | if (mg) | |
3307 | mg->mg_len = -1; | |
a0d0e21e | 3308 | } |
4633a7c4 | 3309 | LEAVE_SCOPE(oldsave); |
eb7e169e | 3310 | if (gimme == G_LIST) |
1604cfb0 | 3311 | RETURN; |
a0d0e21e LW |
3312 | RETPUSHNO; |
3313 | } | |
3314 | ||
3315 | OP * | |
864dbfa3 | 3316 | Perl_do_readline(pTHX) |
a0d0e21e | 3317 | { |
20b7effb | 3318 | dSP; dTARGETSTACKED; |
eb578fdb | 3319 | SV *sv; |
a0d0e21e LW |
3320 | STRLEN tmplen = 0; |
3321 | STRLEN offset; | |
760ac839 | 3322 | PerlIO *fp; |
eb578fdb KW |
3323 | IO * const io = GvIO(PL_last_in_gv); |
3324 | const I32 type = PL_op->op_type; | |
1c23e2bd | 3325 | const U8 gimme = GIMME_V; |
a0d0e21e | 3326 | |
6136c704 | 3327 | if (io) { |
1604cfb0 MS |
3328 | const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar); |
3329 | if (mg) { | |
3330 | Perl_tied_method(aTHX_ SV_CONST(READLINE), SP, MUTABLE_SV(io), mg, gimme, 0); | |
3331 | if (gimme == G_SCALAR) { | |
3332 | SPAGAIN; | |
3333 | SvSetSV_nosteal(TARG, TOPs); | |
3334 | SETTARG; | |
3335 | } | |
3336 | return NORMAL; | |
3337 | } | |
e79b0511 | 3338 | } |
4608196e | 3339 | fp = NULL; |
a0d0e21e | 3340 | if (io) { |
1604cfb0 MS |
3341 | fp = IoIFP(io); |
3342 | if (!fp) { | |
3343 | if (IoFLAGS(io) & IOf_ARGV) { | |
3344 | if (IoFLAGS(io) & IOf_START) { | |
3345 | IoLINES(io) = 0; | |
3346 | if (av_count(GvAVn(PL_last_in_gv)) == 0) { | |
3347 | IoFLAGS(io) &= ~IOf_START; | |
3348 | do_open6(PL_last_in_gv, "-", 1, NULL, NULL, 0); | |
3349 | SvTAINTED_off(GvSVn(PL_last_in_gv)); /* previous tainting irrelevant */ | |
3350 | sv_setpvs(GvSVn(PL_last_in_gv), "-"); | |
3351 | SvSETMAGIC(GvSV(PL_last_in_gv)); | |
3352 | fp = IoIFP(io); | |
3353 | goto have_fp; | |
3354 | } | |
3355 | } | |
3356 | fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL); | |
3357 | if (!fp) { /* Note: fp != IoIFP(io) */ | |
3358 | (void)do_close(PL_last_in_gv, FALSE); /* now it does*/ | |
3359 | } | |
3360 | } | |
3361 | else if (type == OP_GLOB) | |
3362 | fp = Perl_start_glob(aTHX_ POPs, io); | |
3363 | } | |
3364 | else if (type == OP_GLOB) | |
3365 | SP--; | |
3366 | else if (IoTYPE(io) == IoTYPE_WRONLY) { | |
3367 | report_wrongway_fh(PL_last_in_gv, '>'); | |
3368 | } | |
a0d0e21e LW |
3369 | } |
3370 | if (!fp) { | |
1604cfb0 MS |
3371 | if ((!io || !(IoFLAGS(io) & IOf_START)) |
3372 | && ckWARN(WARN_CLOSED) | |
de7dabb6 | 3373 | && type != OP_GLOB) |
1604cfb0 MS |
3374 | { |
3375 | report_evil_fh(PL_last_in_gv); | |
3376 | } | |
3377 | if (gimme == G_SCALAR) { | |
3378 | /* undef TARG, and push that undefined value */ | |
3379 | if (type != OP_RCATLINE) { | |
3380 | sv_set_undef(TARG); | |
3381 | } | |
3382 | PUSHTARG; | |
3383 | } | |
3384 | RETURN; | |
a0d0e21e | 3385 | } |
a2008d6d | 3386 | have_fp: |
54310121 | 3387 | if (gimme == G_SCALAR) { |
1604cfb0 MS |
3388 | sv = TARG; |
3389 | if (type == OP_RCATLINE && SvGMAGICAL(sv)) | |
3390 | mg_get(sv); | |
3391 | if (SvROK(sv)) { | |
3392 | if (type == OP_RCATLINE) | |
3393 | SvPV_force_nomg_nolen(sv); | |
3394 | else | |
3395 | sv_unref(sv); | |
3396 | } | |
3397 | else if (isGV_with_GP(sv)) { | |
3398 | SvPV_force_nomg_nolen(sv); | |
3399 | } | |
3400 | SvUPGRADE(sv, SVt_PV); | |
3401 | tmplen = SvLEN(sv); /* remember if already alloced */ | |
3402 | if (!tmplen && !SvREADONLY(sv) && !SvIsCOW(sv)) { | |
f72e8700 | 3403 | /* try short-buffering it. Please update t/op/readline.t |
1604cfb0 MS |
3404 | * if you change the growth length. |
3405 | */ | |
3406 | Sv_Grow(sv, 80); | |
3407 | } | |
3408 | offset = 0; | |
3409 | if (type == OP_RCATLINE && SvOK(sv)) { | |
3410 | if (!SvPOK(sv)) { | |
3411 | SvPV_force_nomg_nolen(sv); | |
3412 | } | |
3413 | offset = SvCUR(sv); | |
3414 | } | |
a0d0e21e | 3415 | } |
54310121 | 3416 | else { |
1604cfb0 MS |
3417 | sv = sv_2mortal(newSV(80)); |
3418 | offset = 0; | |
54310121 | 3419 | } |
fbad3eb5 | 3420 | |
3887d568 AP |
3421 | /* This should not be marked tainted if the fp is marked clean */ |
3422 | #define MAYBE_TAINT_LINE(io, sv) \ | |
3423 | if (!(IoFLAGS(io) & IOf_UNTAINT)) { \ | |
1604cfb0 MS |
3424 | TAINT; \ |
3425 | SvTAINTED_on(sv); \ | |
3887d568 AP |
3426 | } |
3427 | ||
684bef36 | 3428 | /* delay EOF state for a snarfed empty file */ |
fbad3eb5 | 3429 | #define SNARF_EOF(gimme,rs,io,sv) \ |
684bef36 | 3430 | (gimme != G_SCALAR || SvCUR(sv) \ |
b9fee9ba | 3431 | || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs)) |
fbad3eb5 | 3432 | |
a0d0e21e | 3433 | for (;;) { |
1604cfb0 MS |
3434 | PUTBACK; |
3435 | if (!sv_gets(sv, fp, offset) | |
3436 | && (type == OP_GLOB | |
3437 | || SNARF_EOF(gimme, PL_rs, io, sv) | |
3438 | || PerlIO_error(fp))) | |
3439 | { | |
1604cfb0 MS |
3440 | if (IoFLAGS(io) & IOf_ARGV) { |
3441 | fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL); | |
80c1f1e4 | 3442 | if (fp) { |
1604cfb0 | 3443 | continue; |
80c1f1e4 | 3444 | } |
1604cfb0 MS |
3445 | (void)do_close(PL_last_in_gv, FALSE); |
3446 | } | |
3447 | else if (type == OP_GLOB) { | |
80c1f1e4 TC |
3448 | /* clear any errors here so we only fail on the pclose() |
3449 | failing, which should only happen on the child | |
3450 | failing | |
3451 | */ | |
3452 | PerlIO_clearerr(fp); | |
1604cfb0 MS |
3453 | if (!do_close(PL_last_in_gv, FALSE)) { |
3454 | Perl_ck_warner(aTHX_ packWARN(WARN_GLOB), | |
3455 | "glob failed (child exited with status %d%s)", | |
3456 | (int)(STATUS_CURRENT >> 8), | |
3457 | (STATUS_CURRENT & 0x80) ? ", core dumped" : ""); | |
3458 | } | |
3459 | } | |
3460 | if (gimme == G_SCALAR) { | |
3461 | if (type != OP_RCATLINE) { | |
3462 | SV_CHECK_THINKFIRST_COW_DROP(TARG); | |
3463 | SvOK_off(TARG); | |
3464 | } | |
3465 | SPAGAIN; | |
3466 | PUSHTARG; | |
3467 | } | |
3468 | MAYBE_TAINT_LINE(io, sv); | |
3469 | RETURN; | |
3470 | } | |
3471 | MAYBE_TAINT_LINE(io, sv); | |
3472 | IoLINES(io)++; | |
3473 | IoFLAGS(io) |= IOf_NOLINE; | |
3474 | SvSETMAGIC(sv); | |
3475 | SPAGAIN; | |
3476 | XPUSHs(sv); | |
3477 | if (type == OP_GLOB) { | |
3478 | const char *t1; | |
3479 | Stat_t statbuf; | |
3480 | ||
3481 | if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) { | |
3482 | char * const tmps = SvEND(sv) - 1; | |
3483 | if (*tmps == *SvPVX_const(PL_rs)) { | |
3484 | *tmps = '\0'; | |
3485 | SvCUR_set(sv, SvCUR(sv) - 1); | |
3486 | } | |
3487 | } | |
3488 | for (t1 = SvPVX_const(sv); *t1; t1++) | |
b51c3e77 | 3489 | #ifdef __VMS |
1604cfb0 | 3490 | if (memCHRs("*%?", *t1)) |
b51c3e77 | 3491 | #else |
1604cfb0 | 3492 | if (memCHRs("$&*(){}[]'\";\\|?<>~`", *t1)) |
b51c3e77 | 3493 | #endif |
1604cfb0 MS |
3494 | break; |
3495 | if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &statbuf) < 0) { | |
3496 | (void)POPs; /* Unmatched wildcard? Chuck it... */ | |
3497 | continue; | |
3498 | } | |
3499 | } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */ | |
3500 | if (ckWARN(WARN_UTF8)) { | |
3501 | const U8 * const s = (const U8*)SvPVX_const(sv) + offset; | |
3502 | const STRLEN len = SvCUR(sv) - offset; | |
3503 | const U8 *f; | |
3504 | ||
3505 | if (!is_utf8_string_loc(s, len, &f)) | |
3506 | /* Emulate :encoding(utf8) warning in the same case. */ | |
3507 | Perl_warner(aTHX_ packWARN(WARN_UTF8), | |
3508 | "utf8 \"\\x%02X\" does not map to Unicode", | |
3509 | f < (U8*)SvEND(sv) ? *f : 0); | |
3510 | } | |
3511 | } | |
eb7e169e | 3512 | if (gimme == G_LIST) { |
1604cfb0 MS |
3513 | if (SvLEN(sv) - SvCUR(sv) > 20) { |
3514 | SvPV_shrink_to_cur(sv); | |
3515 | } | |
3516 | sv = sv_2mortal(newSV(80)); | |
3517 | continue; | |
3518 | } | |
3519 | else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) { | |
3520 | /* try to reclaim a bit of scalar space (only on 1st alloc) */ | |
3521 | const STRLEN new_len | |
3522 | = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */ | |
3523 | SvPV_renew(sv, new_len); | |
3524 | } | |
3525 | RETURN; | |
a0d0e21e LW |
3526 | } |
3527 | } | |
3528 | ||
a0d0e21e LW |
3529 | PP(pp_helem) |
3530 | { | |
20b7effb | 3531 | dSP; |
760ac839 | 3532 | HE* he; |
ae77835f | 3533 | SV **svp; |
c445ea15 | 3534 | SV * const keysv = POPs; |
85fbaab2 | 3535 | HV * const hv = MUTABLE_HV(POPs); |
a3b680e6 AL |
3536 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
3537 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; | |
be6c24e0 | 3538 | SV *sv; |
92970b93 | 3539 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; |
d30e492c | 3540 | bool preeminent = TRUE; |
a0d0e21e | 3541 | |
6dfc73ea | 3542 | if (SvTYPE(hv) != SVt_PVHV) |
1604cfb0 | 3543 | RETPUSHUNDEF; |
d4c19fe8 | 3544 | |
92970b93 | 3545 | if (localizing) { |
1604cfb0 MS |
3546 | MAGIC *mg; |
3547 | HV *stash; | |
d30e492c | 3548 | |
d49261a9 | 3549 | /* If we can determine whether the element exists, |
1604cfb0 MS |
3550 | * Try to preserve the existenceness of a tied hash |
3551 | * element by using EXISTS and DELETE if possible. | |
3552 | * Fallback to FETCH and STORE otherwise. */ | |
3553 | if (SvCANEXISTDELETE(hv)) | |
3554 | preeminent = hv_exists_ent(hv, keysv, 0); | |
d4c19fe8 | 3555 | } |
d30e492c | 3556 | |
5f9d7e2b | 3557 | he = hv_fetch_ent(hv, keysv, lval && !defer, 0); |
d4c19fe8 | 3558 | svp = he ? &HeVAL(he) : NULL; |
a0d0e21e | 3559 | if (lval) { |
1604cfb0 MS |
3560 | if (!svp || !*svp || *svp == &PL_sv_undef) { |
3561 | SV* lv; | |
3562 | SV* key2; | |
3563 | if (!defer) { | |
3564 | DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv)); | |
3565 | } | |
7ea8b04b | 3566 | lv = newSV_type_mortal(SVt_PVLV); |
1604cfb0 MS |
3567 | LvTYPE(lv) = 'y'; |
3568 | sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0); | |
3569 | SvREFCNT_dec_NN(key2); /* sv_magic() increments refcount */ | |
3570 | LvTARG(lv) = SvREFCNT_inc_simple_NN(hv); | |
3571 | LvTARGLEN(lv) = 1; | |
3572 | PUSHs(lv); | |
3573 | RETURN; | |
3574 | } | |
3575 | if (localizing) { | |
3576 | if (HvNAME_get(hv) && isGV_or_RVCV(*svp)) | |
3577 | save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL)); | |
3578 | else if (preeminent) | |
3579 | save_helem_flags(hv, keysv, svp, | |
3580 | (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC); | |
3581 | else | |
3582 | SAVEHDELETE(hv, keysv); | |
3583 | } | |
3584 | else if (PL_op->op_private & OPpDEREF) { | |
3585 | PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF)); | |
3586 | RETURN; | |
3587 | } | |
a0d0e21e | 3588 | } |
746f6409 | 3589 | sv = (svp && *svp ? *svp : &PL_sv_undef); |
fd69380d DM |
3590 | /* Originally this did a conditional C<sv = sv_mortalcopy(sv)>; this |
3591 | * was to make C<local $tied{foo} = $tied{foo}> possible. | |
3592 | * However, it seems no longer to be needed for that purpose, and | |
3593 | * introduced a new bug: stuff like C<while ($hash{taintedval} =~ /.../g> | |
3594 | * would loop endlessly since the pos magic is getting set on the | |
3595 | * mortal copy and lost. However, the copy has the effect of | |
3596 | * triggering the get magic, and losing it altogether made things like | |
3597 | * c<$tied{foo};> in void context no longer do get magic, which some | |
3598 | * code relied on. Also, delayed triggering of magic on @+ and friends | |
3599 | * meant the original regex may be out of scope by now. So as a | |
3600 | * compromise, do the get magic here. (The MGf_GSKIP flag will stop it | |
3601 | * being called too many times). */ | |
39cf747a | 3602 | if (!lval && SvRMAGICAL(hv) && SvGMAGICAL(sv)) |
1604cfb0 | 3603 | mg_get(sv); |
be6c24e0 | 3604 | PUSHs(sv); |
a0d0e21e LW |
3605 | RETURN; |
3606 | } | |
3607 | ||
fedf30e1 DM |
3608 | |
3609 | /* a stripped-down version of Perl_softref2xv() for use by | |
3610 | * pp_multideref(), which doesn't use PL_op->op_flags */ | |
3611 | ||
f9db5646 | 3612 | STATIC GV * |
fedf30e1 | 3613 | S_softref2xv_lite(pTHX_ SV *const sv, const char *const what, |
1604cfb0 | 3614 | const svtype type) |
fedf30e1 DM |
3615 | { |
3616 | if (PL_op->op_private & HINT_STRICT_REFS) { | |
1604cfb0 MS |
3617 | if (SvOK(sv)) |
3618 | Perl_die(aTHX_ PL_no_symref_sv, sv, | |
3619 | (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what); | |
3620 | else | |
3621 | Perl_die(aTHX_ PL_no_usym, what); | |
fedf30e1 DM |
3622 | } |
3623 | if (!SvOK(sv)) | |
3624 | Perl_die(aTHX_ PL_no_usym, what); | |
3625 | return gv_fetchsv_nomg(sv, GV_ADD, type); | |
3626 | } | |
3627 | ||
3628 | ||
79815f56 DM |
3629 | /* Handle one or more aggregate derefs and array/hash indexings, e.g. |
3630 | * $h->{foo} or $a[0]{$key}[$i] or f()->[1] | |
fedf30e1 DM |
3631 | * |
3632 | * op_aux points to an array of unions of UV / IV / SV* / PADOFFSET. | |
79815f56 DM |
3633 | * Each of these either contains a set of actions, or an argument, such as |
3634 | * an IV to use as an array index, or a lexical var to retrieve. | |
3635 | * Several actions re stored per UV; we keep shifting new actions off the | |
3636 | * one UV, and only reload when it becomes zero. | |
fedf30e1 DM |
3637 | */ |
3638 | ||
3639 | PP(pp_multideref) | |
3640 | { | |
3641 | SV *sv = NULL; /* init to avoid spurious 'may be used uninitialized' */ | |
3642 | UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux; | |
3643 | UV actions = items->uv; | |
3644 | ||
3645 | assert(actions); | |
3646 | /* this tells find_uninit_var() where we're up to */ | |
3647 | PL_multideref_pc = items; | |
3648 | ||
3649 | while (1) { | |
3650 | /* there are three main classes of action; the first retrieve | |
3651 | * the initial AV or HV from a variable or the stack; the second | |
3652 | * does the equivalent of an unrolled (/DREFAV, rv2av, aelem), | |
3653 | * the third an unrolled (/DREFHV, rv2hv, helem). | |
3654 | */ | |
3655 | switch (actions & MDEREF_ACTION_MASK) { | |
3656 | ||
3657 | case MDEREF_reload: | |
3658 | actions = (++items)->uv; | |
3659 | continue; | |
3660 | ||
3661 | case MDEREF_AV_padav_aelem: /* $lex[...] */ | |
3662 | sv = PAD_SVl((++items)->pad_offset); | |
3663 | goto do_AV_aelem; | |
3664 | ||
3665 | case MDEREF_AV_gvav_aelem: /* $pkg[...] */ | |
3666 | sv = UNOP_AUX_item_sv(++items); | |
3667 | assert(isGV_with_GP(sv)); | |
3668 | sv = (SV*)GvAVn((GV*)sv); | |
3669 | goto do_AV_aelem; | |
3670 | ||
3671 | case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */ | |
3672 | { | |
3673 | dSP; | |
3674 | sv = POPs; | |
3675 | PUTBACK; | |
3676 | goto do_AV_rv2av_aelem; | |
3677 | } | |
3678 | ||
3679 | case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */ | |
3680 | sv = UNOP_AUX_item_sv(++items); | |
3681 | assert(isGV_with_GP(sv)); | |
3682 | sv = GvSVn((GV*)sv); | |
3683 | goto do_AV_vivify_rv2av_aelem; | |
3684 | ||
3685 | case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */ | |
3686 | sv = PAD_SVl((++items)->pad_offset); | |
3687 | /* FALLTHROUGH */ | |
3688 | ||
3689 | do_AV_vivify_rv2av_aelem: | |
3690 | case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */ | |
3691 | /* this is the OPpDEREF action normally found at the end of | |
3692 | * ops like aelem, helem, rv2sv */ | |
3693 | sv = vivify_ref(sv, OPpDEREF_AV); | |
3694 | /* FALLTHROUGH */ | |
3695 | ||
3696 | do_AV_rv2av_aelem: | |
3697 | /* this is basically a copy of pp_rv2av when it just has the | |
3698 | * sKR/1 flags */ | |
3699 | SvGETMAGIC(sv); | |
3700 | if (LIKELY(SvROK(sv))) { | |
3701 | if (UNLIKELY(SvAMAGIC(sv))) { | |
3702 | sv = amagic_deref_call(sv, to_av_amg); | |
3703 | } | |
3704 | sv = SvRV(sv); | |
3705 | if (UNLIKELY(SvTYPE(sv) != SVt_PVAV)) | |
3706 | DIE(aTHX_ "Not an ARRAY reference"); | |
3707 | } | |
3708 | else if (SvTYPE(sv) != SVt_PVAV) { | |
3709 | if (!isGV_with_GP(sv)) | |
3710 | sv = (SV*)S_softref2xv_lite(aTHX_ sv, "an ARRAY", SVt_PVAV); | |
3711 | sv = MUTABLE_SV(GvAVn((GV*)sv)); | |
3712 | } | |
3713 | /* FALLTHROUGH */ | |
3714 | ||
3715 | do_AV_aelem: | |
3716 | { | |
3717 | /* retrieve the key; this may be either a lexical or package | |
3718 | * var (whose index/ptr is stored as an item) or a signed | |
3719 | * integer constant stored as an item. | |
3720 | */ | |
3721 | SV *elemsv; | |
3722 | IV elem = 0; /* to shut up stupid compiler warnings */ | |
3723 | ||
3724 | ||
3725 | assert(SvTYPE(sv) == SVt_PVAV); | |
3726 | ||
3727 | switch (actions & MDEREF_INDEX_MASK) { | |
3728 | case MDEREF_INDEX_none: | |
3729 | goto finish; | |
3730 | case MDEREF_INDEX_const: | |
3731 | elem = (++items)->iv; | |
3732 | break; | |
3733 | case MDEREF_INDEX_padsv: | |
3734 | elemsv = PAD_SVl((++items)->pad_offset); | |
3735 | goto check_elem; | |
3736 | case MDEREF_INDEX_gvsv: | |
3737 | elemsv = UNOP_AUX_item_sv(++items); | |
3738 | assert(isGV_with_GP(elemsv)); | |
3739 | elemsv = GvSVn((GV*)elemsv); | |
3740 | check_elem: | |
3741 | if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv) | |
3742 | && ckWARN(WARN_MISC))) | |
3743 | Perl_warner(aTHX_ packWARN(WARN_MISC), | |
147e3846 | 3744 | "Use of reference \"%" SVf "\" as array index", |
fedf30e1 DM |
3745 | SVfARG(elemsv)); |
3746 | /* the only time that S_find_uninit_var() needs this | |
3747 | * is to determine which index value triggered the | |
3748 | * undef warning. So just update it here. Note that | |
3749 | * since we don't save and restore this var (e.g. for | |
3750 | * tie or overload execution), its value will be | |
3751 | * meaningless apart from just here */ | |
3752 | PL_multideref_pc = items; | |
3753 | elem = SvIV(elemsv); | |
3754 | break; | |
3755 | } | |
3756 | ||
3757 | ||
3758 | /* this is basically a copy of pp_aelem with OPpDEREF skipped */ | |
3759 | ||
3760 | if (!(actions & MDEREF_FLAG_last)) { | |
3761 | SV** svp = av_fetch((AV*)sv, elem, 1); | |
3762 | if (!svp || ! (sv=*svp)) | |
3763 | DIE(aTHX_ PL_no_aelem, elem); | |
3764 | break; | |
3765 | } | |
3766 | ||
3767 | if (PL_op->op_private & | |
3768 | (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)) | |
3769 | { | |
3770 | if (PL_op->op_private & OPpMULTIDEREF_EXISTS) { | |
3771 | sv = av_exists((AV*)sv, elem) ? &PL_sv_yes : &PL_sv_no; | |
3772 | } | |
3773 | else { | |
3774 | I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0; | |
3775 | sv = av_delete((AV*)sv, elem, discard); | |
3776 | if (discard) | |
3777 | return NORMAL; | |
3778 | if (!sv) | |
3779 | sv = &PL_sv_undef; | |
3780 | } | |
3781 | } | |
3782 | else { | |
3783 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; | |
3784 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; | |
3785 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; | |
3786 | bool preeminent = TRUE; | |
3787 | AV *const av = (AV*)sv; | |
3788 | SV** svp; | |
3789 | ||
3790 | if (UNLIKELY(localizing)) { | |
3791 | MAGIC *mg; | |
3792 | HV *stash; | |
3793 | ||
3794 | /* If we can determine whether the element exist, | |
3795 | * Try to preserve the existenceness of a tied array | |
3796 | * element by using EXISTS and DELETE if possible. | |
3797 | * Fallback to FETCH and STORE otherwise. */ | |
3798 | if (SvCANEXISTDELETE(av)) | |
3799 | preeminent = av_exists(av, elem); | |
3800 | } | |
3801 | ||
3802 | svp = av_fetch(av, elem, lval && !defer); | |
3803 | ||
3804 | if (lval) { | |
3805 | if (!svp || !(sv = *svp)) { | |
3806 | IV len; | |
3807 | if (!defer) | |
3808 | DIE(aTHX_ PL_no_aelem, elem); | |
8272d5bd | 3809 | len = av_top_index(av); |
9ef753fe FC |
3810 | /* Resolve a negative index that falls within |
3811 | * the array. Leave it negative it if falls | |
3812 | * outside the array. */ | |
3813 | if (elem < 0 && len + elem >= 0) | |
3814 | elem = len + elem; | |
3815 | if (elem >= 0 && elem <= len) | |
3816 | /* Falls within the array. */ | |
3817 | sv = av_nonelem(av,elem); | |
3818 | else | |
3819 | /* Falls outside the array. If it is neg- | |
3820 | ative, magic_setdefelem will use the | |
3821 | index for error reporting. */ | |
3822 | sv = sv_2mortal(newSVavdefelem(av,elem,1)); | |
fedf30e1 DM |
3823 | } |
3824 | else { | |
3825 | if (UNLIKELY(localizing)) { | |
3826 | if (preeminent) { | |
3827 | save_aelem(av, elem, svp); | |
3828 | sv = *svp; /* may have changed */ | |
3829 | } | |
3830 | else | |
3831 | SAVEADELETE(av, elem); | |
3832 | } | |
3833 | } | |
3834 | } | |
3835 | else { | |
3836 | sv = (svp ? *svp : &PL_sv_undef); | |
3837 | /* see note in pp_helem() */ | |
3838 | if (SvRMAGICAL(av) && SvGMAGICAL(sv)) | |
3839 | mg_get(sv); | |
3840 | } | |
3841 | } | |
3842 | ||
3843 | } | |
3844 | finish: | |
3845 | { | |
3846 | dSP; | |
3847 | XPUSHs(sv); | |
3848 | RETURN; | |
3849 | } | |
3850 | /* NOTREACHED */ | |
3851 | ||
3852 | ||
3853 | ||
3854 | ||
3855 | case MDEREF_HV_padhv_helem: /* $lex{...} */ | |
3856 | sv = PAD_SVl((++items)->pad_offset); | |
3857 | goto do_HV_helem; | |
3858 | ||
3859 | case MDEREF_HV_gvhv_helem: /* $pkg{...} */ | |
3860 | sv = UNOP_AUX_item_sv(++items); | |
3861 | assert(isGV_with_GP(sv)); | |
3862 | sv = (SV*)GvHVn((GV*)sv); | |
3863 | goto do_HV_helem; | |
3864 | ||
3865 | case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */ | |
3866 | { | |
3867 | dSP; | |
3868 | sv = POPs; | |
3869 | PUTBACK; | |
3870 | goto do_HV_rv2hv_helem; | |
3871 | } | |
3872 | ||
3873 | case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */ | |
3874 | sv = UNOP_AUX_item_sv(++items); | |
3875 | assert(isGV_with_GP(sv)); | |
3876 | sv = GvSVn((GV*)sv); | |
3877 | goto do_HV_vivify_rv2hv_helem; | |
3878 | ||
3879 | case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */ | |
3880 | sv = PAD_SVl((++items)->pad_offset); | |
3881 | /* FALLTHROUGH */ | |
3882 | ||
3883 | do_HV_vivify_rv2hv_helem: | |
3884 | case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */ | |
3885 | /* this is the OPpDEREF action normally found at the end of | |
3886 | * ops like aelem, helem, rv2sv */ | |
3887 | sv = vivify_ref(sv, OPpDEREF_HV); | |
3888 | /* FALLTHROUGH */ | |
3889 | ||
3890 | do_HV_rv2hv_helem: | |
3891 | /* this is basically a copy of pp_rv2hv when it just has the | |
3892 | * sKR/1 flags (and pp_rv2hv is aliased to pp_rv2av) */ | |
3893 | ||
3894 | SvGETMAGIC(sv); | |
3895 | if (LIKELY(SvROK(sv))) { | |
3896 | if (UNLIKELY(SvAMAGIC(sv))) { | |
3897 | sv = amagic_deref_call(sv, to_hv_amg); | |
3898 | } | |
3899 | sv = SvRV(sv); | |
3900 | if (UNLIKELY(SvTYPE(sv) != SVt_PVHV)) | |
3901 | DIE(aTHX_ "Not a HASH reference"); | |
3902 | } | |
3903 | else if (SvTYPE(sv) != SVt_PVHV) { | |
3904 | if (!isGV_with_GP(sv)) | |
3905 | sv = (SV*)S_softref2xv_lite(aTHX_ sv, "a HASH", SVt_PVHV); | |
3906 | sv = MUTABLE_SV(GvHVn((GV*)sv)); | |
3907 | } | |
3908 | /* FALLTHROUGH */ | |
3909 | ||
3910 | do_HV_helem: | |
3911 | { | |
3912 | /* retrieve the key; this may be either a lexical / package | |
3913 | * var or a string constant, whose index/ptr is stored as an | |
3914 | * item | |
3915 | */ | |
3916 | SV *keysv = NULL; /* to shut up stupid compiler warnings */ | |
3917 | ||
3918 | assert(SvTYPE(sv) == SVt_PVHV); | |
3919 | ||
3920 | switch (actions & MDEREF_INDEX_MASK) { | |
3921 | case MDEREF_INDEX_none: | |
3922 | goto finish; | |
3923 | ||
3924 | case MDEREF_INDEX_const: | |
3925 | keysv = UNOP_AUX_item_sv(++items); | |
3926 | break; | |
3927 | ||
3928 | case MDEREF_INDEX_padsv: | |
3929 | keysv = PAD_SVl((++items)->pad_offset); | |
3930 | break; | |
3931 | ||
3932 | case MDEREF_INDEX_gvsv: | |
3933 | keysv = UNOP_AUX_item_sv(++items); | |
3934 | keysv = GvSVn((GV*)keysv); | |
3935 | break; | |
3936 | } | |
3937 | ||
3938 | /* see comment above about setting this var */ | |
3939 | PL_multideref_pc = items; | |
3940 | ||
3941 | ||
3942 | /* ensure that candidate CONSTs have been HEKified */ | |
3943 | assert( ((actions & MDEREF_INDEX_MASK) != MDEREF_INDEX_const) | |
3944 | || SvTYPE(keysv) >= SVt_PVMG | |
3945 | || !SvOK(keysv) | |
3946 | || SvROK(keysv) | |
3947 | || SvIsCOW_shared_hash(keysv)); | |
3948 | ||
3949 | /* this is basically a copy of pp_helem with OPpDEREF skipped */ | |
3950 | ||
3951 | if (!(actions & MDEREF_FLAG_last)) { | |
3952 | HE *he = hv_fetch_ent((HV*)sv, keysv, 1, 0); | |
3953 | if (!he || !(sv=HeVAL(he)) || sv == &PL_sv_undef) | |
3954 | DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv)); | |
3955 | break; | |
3956 | } | |
3957 | ||
3958 | if (PL_op->op_private & | |
3959 | (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)) | |
3960 | { | |
3961 | if (PL_op->op_private & OPpMULTIDEREF_EXISTS) { | |
3962 | sv = hv_exists_ent((HV*)sv, keysv, 0) | |
3963 | ? &PL_sv_yes : &PL_sv_no; | |
3964 | } | |
3965 | else { | |
3966 | I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0; | |
3967 | sv = hv_delete_ent((HV*)sv, keysv, discard, 0); | |
3968 | if (discard) | |
3969 | return NORMAL; | |
3970 | if (!sv) | |
3971 | sv = &PL_sv_undef; | |
3972 | } | |
3973 | } | |
3974 | else { | |
3975 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; | |
3976 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; | |
3977 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; | |
3978 | bool preeminent = TRUE; | |
3979 | SV **svp; | |
3980 | HV * const hv = (HV*)sv; | |
3981 | HE* he; | |
3982 | ||
3983 | if (UNLIKELY(localizing)) { | |
3984 | MAGIC *mg; | |
3985 | HV *stash; | |
3986 | ||
3987 | /* If we can determine whether the element exist, | |
3988 | * Try to preserve the existenceness of a tied hash | |
3989 | * element by using EXISTS and DELETE if possible. | |
3990 | * Fallback to FETCH and STORE otherwise. */ | |
3991 | if (SvCANEXISTDELETE(hv)) | |
3992 | preeminent = hv_exists_ent(hv, keysv, 0); | |
3993 | } | |
3994 | ||
3995 | he = hv_fetch_ent(hv, keysv, lval && !defer, 0); | |
3996 | svp = he ? &HeVAL(he) : NULL; | |
3997 | ||
3998 | ||
3999 | if (lval) { | |
4000 | if (!svp || !(sv = *svp) || sv == &PL_sv_undef) { | |
4001 | SV* lv; | |
4002 | SV* key2; | |
4003 | if (!defer) | |
4004 | DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv)); | |
7ea8b04b | 4005 | lv = newSV_type_mortal(SVt_PVLV); |
fedf30e1 DM |
4006 | LvTYPE(lv) = 'y'; |
4007 | sv_magic(lv, key2 = newSVsv(keysv), | |
4008 | PERL_MAGIC_defelem, NULL, 0); | |
4009 | /* sv_magic() increments refcount */ | |
4010 | SvREFCNT_dec_NN(key2); | |
0ad694a7 | 4011 | LvTARG(lv) = SvREFCNT_inc_simple_NN(hv); |
fedf30e1 DM |
4012 | LvTARGLEN(lv) = 1; |
4013 | sv = lv; | |
4014 | } | |
4015 | else { | |
4016 | if (localizing) { | |
a35c9018 | 4017 | if (HvNAME_get(hv) && isGV_or_RVCV(sv)) |
fedf30e1 DM |
4018 | save_gp(MUTABLE_GV(sv), |
4019 | !(PL_op->op_flags & OPf_SPECIAL)); | |
4020 | else if (preeminent) { | |
4021 | save_helem_flags(hv, keysv, svp, | |
4022 | (PL_op->op_flags & OPf_SPECIAL) | |
4023 | ? 0 : SAVEf_SETMAGIC); | |
4024 | sv = *svp; /* may have changed */ | |
4025 | } | |
4026 | else | |
4027 | SAVEHDELETE(hv, keysv); | |
4028 | } | |
4029 | } | |
4030 | } | |
4031 | else { | |
4032 | sv = (svp && *svp ? *svp : &PL_sv_undef); | |
4033 | /* see note in pp_helem() */ | |
4034 | if (SvRMAGICAL(hv) && SvGMAGICAL(sv)) | |
4035 | mg_get(sv); | |
4036 | } | |
4037 | } | |
4038 | goto finish; | |
4039 | } | |
4040 | ||
4041 | } /* switch */ | |
4042 | ||
4043 | actions >>= MDEREF_SHIFT; | |
4044 | } /* while */ | |
4045 | /* NOTREACHED */ | |
4046 | } | |
4047 | ||
4048 | ||
a0d0e21e LW |
4049 | PP(pp_iter) |
4050 | { | |
d9b6ecc1 NC |
4051 | PERL_CONTEXT *cx = CX_CUR(); |
4052 | SV **itersvp = CxITERVAR(cx); | |
4053 | const U8 type = CxTYPE(cx); | |
84f05d57 | 4054 | |
c52d5e02 NC |
4055 | /* Classic "for" syntax iterates one-at-a-time. |
4056 | Many-at-a-time for loops are only for lexicals declared as part of the | |
4057 | for loop, and rely on all the lexicals being in adjacent pad slots. | |
4058 | ||
4059 | Curiously, even if the iterator variable is a lexical, the pad offset is | |
4060 | stored in the targ slot of the ENTERITER op, meaning that targ of this OP | |
4061 | has always been zero. Hence we can use this op's targ to hold "how many" | |
4062 | for many-at-a-time. We actually store C<how_many - 1>, so that for the | |
4063 | case of one-at-a-time we have zero (as before), as this makes all the | |
4064 | logic of the for loop below much simpler, with all the other | |
4065 | one-at-a-time cases just falling out of this "naturally". */ | |
4066 | PADOFFSET how_many = PL_op->op_targ; | |
4067 | PADOFFSET i = 0; | |
4068 | ||
4b5c941e | 4069 | assert(itersvp); |
a48ce6be | 4070 | |
c52d5e02 | 4071 | for (; i <= how_many; ++i ) { |
d9b6ecc1 NC |
4072 | SV *oldsv; |
4073 | SV *sv; | |
4074 | AV *av; | |
4075 | IV ix; | |
4076 | IV inc; | |
4077 | ||
4078 | switch (type) { | |
a48ce6be | 4079 | |
525dc1e4 | 4080 | case CXt_LOOP_LAZYSV: /* string increment */ |
47b96a1e | 4081 | { |
525dc1e4 NC |
4082 | SV* cur = cx->blk_loop.state_u.lazysv.cur; |
4083 | SV *end = cx->blk_loop.state_u.lazysv.end; | |
4084 | /* If the maximum is !SvOK(), pp_enteriter substitutes PL_sv_no. | |
4085 | It has SvPVX of "" and SvCUR of 0, which is what we want. */ | |
4086 | STRLEN maxlen = 0; | |
4087 | const char *max = SvPV_const(end, maxlen); | |
c52d5e02 | 4088 | bool pad_it = FALSE; |
525dc1e4 NC |
4089 | if (DO_UTF8(end) && IN_UNI_8_BIT) |
4090 | maxlen = sv_len_utf8_nomg(end); | |
c52d5e02 NC |
4091 | if (UNLIKELY(SvNIOK(cur) || SvCUR(cur) > maxlen)) { |
4092 | if (LIKELY(!i)) { | |
4093 | goto retno; | |
4094 | } | |
4095 | /* We are looping n-at-a-time and the range isn't a multiple | |
4096 | of n, so we fill the rest of the lexicals with undef. | |
4097 | This only happens on the last iteration of the loop, and | |
4098 | we will have already set up the "terminate next time" | |
4099 | condition earlier in this for loop for this call of the | |
4100 | ITER op when we set up the lexical corresponding to the | |
4101 | last value in the range. Hence we don't goto retno (yet), | |
4102 | and just below we don't repeat the setup for "terminate | |
4103 | next time". */ | |
4104 | pad_it = TRUE; | |
4105 | } | |
525dc1e4 NC |
4106 | |
4107 | oldsv = *itersvp; | |
4108 | /* NB: on the first iteration, oldsv will have a ref count of at | |
4109 | * least 2 (one extra from blk_loop.itersave), so the GV or pad | |
4110 | * slot will get localised; on subsequent iterations the RC==1 | |
4111 | * optimisation may kick in and the SV will be reused. */ | |
c52d5e02 NC |
4112 | if (UNLIKELY(pad_it)) { |
4113 | *itersvp = &PL_sv_undef; | |
4114 | SvREFCNT_dec(oldsv); | |
4115 | } | |
4116 | else if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) { | |
525dc1e4 NC |
4117 | /* safe to reuse old SV */ |
4118 | sv_setsv(oldsv, cur); | |
4119 | } | |
4120 | else { | |
4121 | /* we need a fresh SV every time so that loop body sees a | |
4122 | * completely new SV for closures/references to work as | |
4123 | * they used to */ | |
4124 | *itersvp = newSVsv(cur); | |
4125 | SvREFCNT_dec(oldsv); | |
4126 | } | |
c52d5e02 NC |
4127 | |
4128 | if (UNLIKELY(pad_it)) { | |
4d1a4226 NC |
4129 | /* We're "beyond the end" of the iterator here, filling the |
4130 | extra lexicals with undef, so we mustn't do anything | |
4131 | (further) to the the iterator itself at this point. | |
4132 | (Observe how the other two blocks modify the iterator's | |
4133 | value) */ | |
c52d5e02 NC |
4134 | } |
4135 | else if (strEQ(SvPVX_const(cur), max)) | |
525dc1e4 NC |
4136 | sv_setiv(cur, 0); /* terminate next time */ |
4137 | else | |
4138 | sv_inc(cur); | |
4139 | break; | |
47b96a1e | 4140 | } |
1604cfb0 | 4141 | |
525dc1e4 NC |
4142 | case CXt_LOOP_LAZYIV: /* integer increment */ |
4143 | { | |
4144 | IV cur = cx->blk_loop.state_u.lazyiv.cur; | |
c52d5e02 NC |
4145 | bool pad_it = FALSE; |
4146 | if (UNLIKELY(cur > cx->blk_loop.state_u.lazyiv.end)) { | |
4147 | if (LIKELY(!i)) { | |
4148 | goto retno; | |
4149 | } | |
4150 | pad_it = TRUE; | |
4151 | } | |
525dc1e4 NC |
4152 | |
4153 | oldsv = *itersvp; | |
4154 | /* see NB comment above */ | |
c52d5e02 NC |
4155 | if (UNLIKELY(pad_it)) { |
4156 | *itersvp = &PL_sv_undef; | |
4157 | SvREFCNT_dec(oldsv); | |
4158 | } | |
4159 | else if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) { | |
525dc1e4 NC |
4160 | /* safe to reuse old SV */ |
4161 | ||
4162 | if ( (SvFLAGS(oldsv) & (SVTYPEMASK|SVf_THINKFIRST|SVf_IVisUV)) | |
4163 | == SVt_IV) { | |
4164 | /* Cheap SvIOK_only(). | |
4165 | * Assert that flags which SvIOK_only() would test or | |
4166 | * clear can't be set, because we're SVt_IV */ | |
4167 | assert(!(SvFLAGS(oldsv) & | |
4168 | (SVf_OOK|SVf_UTF8|(SVf_OK & ~(SVf_IOK|SVp_IOK))))); | |
4169 | SvFLAGS(oldsv) |= (SVf_IOK|SVp_IOK); | |
4170 | /* SvIV_set() where sv_any points to head */ | |
4171 | oldsv->sv_u.svu_iv = cur; | |
c52d5e02 | 4172 | |
525dc1e4 NC |
4173 | } |
4174 | else | |
4175 | sv_setiv(oldsv, cur); | |
4176 | } | |
4177 | else { | |
4178 | /* we need a fresh SV every time so that loop body sees a | |
4179 | * completely new SV for closures/references to work as they | |
4180 | * used to */ | |
4181 | *itersvp = newSViv(cur); | |
4182 | SvREFCNT_dec(oldsv); | |
4183 | } | |
a48ce6be | 4184 | |
c52d5e02 NC |
4185 | if (UNLIKELY(pad_it)) { |
4186 | /* We're good (see "We are looping n-at-a-time" comment | |
4187 | above). */ | |
4188 | } | |
4189 | else if (UNLIKELY(cur == IV_MAX)) { | |
525dc1e4 NC |
4190 | /* Handle end of range at IV_MAX */ |
4191 | cx->blk_loop.state_u.lazyiv.end = IV_MIN; | |
4192 | } else | |
4193 | ++cx->blk_loop.state_u.lazyiv.cur; | |
4194 | break; | |
4195 | } | |
93661e56 | 4196 | |
525dc1e4 | 4197 | case CXt_LOOP_LIST: /* for (1,2,3) */ |
93661e56 | 4198 | |
525dc1e4 NC |
4199 | assert(OPpITER_REVERSED == 2); /* so inc becomes -1 or 1 */ |
4200 | inc = (IV)1 - (IV)(PL_op->op_private & OPpITER_REVERSED); | |
4201 | ix = (cx->blk_loop.state_u.stack.ix += inc); | |
4202 | if (UNLIKELY(inc > 0 | |
4203 | ? ix > cx->blk_oldsp | |
4204 | : ix <= cx->blk_loop.state_u.stack.basesp) | |
c52d5e02 NC |
4205 | ) { |
4206 | if (LIKELY(!i)) { | |
4207 | goto retno; | |
4208 | } | |
4209 | ||
4210 | sv = &PL_sv_undef; | |
4211 | } | |
4212 | else { | |
4213 | sv = PL_stack_base[ix]; | |
4214 | } | |
93661e56 | 4215 | |
525dc1e4 NC |
4216 | av = NULL; |
4217 | goto loop_ary_common; | |
93661e56 | 4218 | |
525dc1e4 | 4219 | case CXt_LOOP_ARY: /* for (@ary) */ |
de080daa | 4220 | |
525dc1e4 NC |
4221 | av = cx->blk_loop.state_u.ary.ary; |
4222 | inc = (IV)1 - (IV)(PL_op->op_private & OPpITER_REVERSED); | |
4223 | ix = (cx->blk_loop.state_u.ary.ix += inc); | |
4224 | if (UNLIKELY(inc > 0 | |
4225 | ? ix > AvFILL(av) | |
4226 | : ix < 0) | |
c52d5e02 NC |
4227 | ) { |
4228 | if (LIKELY(!i)) { | |
4229 | goto retno; | |
4230 | } | |
ef3e5ea9 | 4231 | |
c52d5e02 NC |
4232 | sv = &PL_sv_undef; |
4233 | } else if (UNLIKELY(SvRMAGICAL(av))) { | |
525dc1e4 NC |
4234 | SV * const * const svp = av_fetch(av, ix, FALSE); |
4235 | sv = svp ? *svp : NULL; | |
4236 | } | |
4237 | else { | |
4238 | sv = AvARRAY(av)[ix]; | |
4239 | } | |
93661e56 | 4240 | |
525dc1e4 | 4241 | loop_ary_common: |
d39c26a6 | 4242 | |
525dc1e4 NC |
4243 | if (UNLIKELY(cx->cx_type & CXp_FOR_LVREF)) { |
4244 | SvSetMagicSV(*itersvp, sv); | |
4245 | break; | |
f38aa882 | 4246 | } |
525dc1e4 NC |
4247 | |
4248 | if (LIKELY(sv)) { | |
4249 | if (UNLIKELY(SvIS_FREED(sv))) { | |
4250 | *itersvp = NULL; | |
4251 | Perl_croak(aTHX_ "Use of freed value in iteration"); | |
4252 | } | |
4253 | if (SvPADTMP(sv)) { | |
4254 | sv = newSVsv(sv); | |
4255 | } | |
4256 | else { | |
4257 | SvTEMP_off(sv); | |
4258 | SvREFCNT_inc_simple_void_NN(sv); | |
4259 | } | |
60779a30 | 4260 | } |
525dc1e4 NC |
4261 | else if (av) { |
4262 | sv = newSVavdefelem(av, ix, 0); | |
8e079c2a | 4263 | } |
525dc1e4 NC |
4264 | else |
4265 | sv = &PL_sv_undef; | |
a0d0e21e | 4266 | |
525dc1e4 NC |
4267 | oldsv = *itersvp; |
4268 | *itersvp = sv; | |
4269 | SvREFCNT_dec(oldsv); | |
4270 | break; | |
a48ce6be | 4271 | |
525dc1e4 NC |
4272 | default: |
4273 | DIE(aTHX_ "panic: pp_iter, type=%u", CxTYPE(cx)); | |
4274 | } | |
c52d5e02 NC |
4275 | |
4276 | /* Only relevant for a many-at-a-time loop: */ | |
4277 | ++itersvp; | |
a48ce6be | 4278 | } |
8a1f10dd | 4279 | |
f75ab299 | 4280 | /* Try to bypass pushing &PL_sv_yes and calling pp_and(); instead |
7c114860 DM |
4281 | * jump straight to the AND op's op_other */ |
4282 | assert(PL_op->op_next->op_type == OP_AND); | |
f75ab299 AC |
4283 | if (PL_op->op_next->op_ppaddr == Perl_pp_and) { |
4284 | return cLOGOPx(PL_op->op_next)->op_other; | |
4285 | } | |
4286 | else { | |
4287 | /* An XS module has replaced the op_ppaddr, so fall back to the slow, | |
4288 | * obvious way. */ | |
4289 | /* pp_enteriter should have pre-extended the stack */ | |
4290 | EXTEND_SKIP(PL_stack_sp, 1); | |
4291 | *++PL_stack_sp = &PL_sv_yes; | |
4292 | return PL_op->op_next; | |
4293 | } | |
7c114860 DM |
4294 | |
4295 | retno: | |
f75ab299 | 4296 | /* Try to bypass pushing &PL_sv_no and calling pp_and(); instead |
7c114860 DM |
4297 | * jump straight to the AND op's op_next */ |
4298 | assert(PL_op->op_next->op_type == OP_AND); | |
8a1f10dd | 4299 | /* pp_enteriter should have pre-extended the stack */ |
87058c31 | 4300 | EXTEND_SKIP(PL_stack_sp, 1); |
7c114860 DM |
4301 | /* we only need this for the rare case where the OP_AND isn't |
4302 | * in void context, e.g. $x = do { for (..) {...} }; | |
f75ab299 AC |
4303 | * (or for when an XS module has replaced the op_ppaddr) |
4304 | * but it's cheaper to just push it rather than testing first | |
7c114860 DM |
4305 | */ |
4306 | *++PL_stack_sp = &PL_sv_no; | |
f75ab299 AC |
4307 | if (PL_op->op_next->op_ppaddr == Perl_pp_and) { |
4308 | return PL_op->op_next->op_next; | |
4309 | } | |
4310 | else { | |
4311 | /* An XS module has replaced the op_ppaddr, so fall back to the slow, | |
4312 | * obvious way. */ | |
4313 | return PL_op->op_next; | |
4314 | } | |
a0d0e21e LW |
4315 | } |
4316 | ||
7c114860 | 4317 | |
ef07e810 DM |
4318 | /* |
4319 | A description of how taint works in pattern matching and substitution. | |
4320 | ||
cc442aa1 NC |
4321 | This is all conditional on NO_TAINT_SUPPORT remaining undefined (the default). |
4322 | Under NO_TAINT_SUPPORT, taint-related operations should become no-ops. | |
284167a5 | 4323 | |
4e19c54b | 4324 | While the pattern is being assembled/concatenated and then compiled, |
284167a5 S |
4325 | PL_tainted will get set (via TAINT_set) if any component of the pattern |
4326 | is tainted, e.g. /.*$tainted/. At the end of pattern compilation, | |
4327 | the RXf_TAINTED flag is set on the pattern if PL_tainted is set (via | |
1738e041 DM |
4328 | TAINT_get). It will also be set if any component of the pattern matches |
4329 | based on locale-dependent behavior. | |
ef07e810 | 4330 | |
0ab462a6 DM |
4331 | When the pattern is copied, e.g. $r = qr/..../, the SV holding the ref to |
4332 | the pattern is marked as tainted. This means that subsequent usage, such | |
284167a5 S |
4333 | as /x$r/, will set PL_tainted using TAINT_set, and thus RXf_TAINTED, |
4334 | on the new pattern too. | |
ef07e810 | 4335 | |
272d35c9 | 4336 | RXf_TAINTED_SEEN is used post-execution by the get magic code |
ef07e810 DM |
4337 | of $1 et al to indicate whether the returned value should be tainted. |
4338 | It is the responsibility of the caller of the pattern (i.e. pp_match, | |
4339 | pp_subst etc) to set this flag for any other circumstances where $1 needs | |
4340 | to be tainted. | |
4341 | ||
4342 | The taint behaviour of pp_subst (and pp_substcont) is quite complex. | |
4343 | ||
4344 | There are three possible sources of taint | |
4345 | * the source string | |
4346 | * the pattern (both compile- and run-time, RXf_TAINTED / RXf_TAINTED_SEEN) | |
4347 | * the replacement string (or expression under /e) | |
4348 | ||
4349 | There are four destinations of taint and they are affected by the sources | |
4350 | according to the rules below: | |
4351 | ||
4352 | * the return value (not including /r): | |
1604cfb0 MS |
4353 | tainted by the source string and pattern, but only for the |
4354 | number-of-iterations case; boolean returns aren't tainted; | |
ef07e810 | 4355 | * the modified string (or modified copy under /r): |
1604cfb0 | 4356 | tainted by the source string, pattern, and replacement strings; |
ef07e810 | 4357 | * $1 et al: |
1604cfb0 MS |
4358 | tainted by the pattern, and under 'use re "taint"', by the source |
4359 | string too; | |
ef07e810 | 4360 | * PL_taint - i.e. whether subsequent code (e.g. in a /e block) is tainted: |
1604cfb0 | 4361 | should always be unset before executing subsequent code. |
ef07e810 DM |
4362 | |
4363 | The overall action of pp_subst is: | |
4364 | ||
4365 | * at the start, set bits in rxtainted indicating the taint status of | |
1604cfb0 | 4366 | the various sources. |
ef07e810 DM |
4367 | |
4368 | * After each pattern execution, update the SUBST_TAINT_PAT bit in | |
1604cfb0 MS |
4369 | rxtainted if RXf_TAINTED_SEEN has been set, to indicate that the |
4370 | pattern has subsequently become tainted via locale ops. | |
ef07e810 DM |
4371 | |
4372 | * If control is being passed to pp_substcont to execute a /e block, | |
1604cfb0 MS |
4373 | save rxtainted in the CXt_SUBST block, for future use by |
4374 | pp_substcont. | |
ef07e810 DM |
4375 | |
4376 | * Whenever control is being returned to perl code (either by falling | |
1604cfb0 MS |
4377 | off the "end" of pp_subst/pp_substcont, or by entering a /e block), |
4378 | use the flag bits in rxtainted to make all the appropriate types of | |
4379 | destination taint visible; e.g. set RXf_TAINTED_SEEN so that $1 | |
4380 | et al will appear tainted. | |
ef07e810 DM |
4381 | |
4382 | pp_match is just a simpler version of the above. | |
4383 | ||
4384 | */ | |
4385 | ||
a0d0e21e LW |
4386 | PP(pp_subst) |
4387 | { | |
20b7effb | 4388 | dSP; dTARG; |
eb578fdb | 4389 | PMOP *pm = cPMOP; |
a0d0e21e | 4390 | PMOP *rpm = pm; |
eb578fdb | 4391 | char *s; |
a0d0e21e | 4392 | char *strend; |
5c144d81 | 4393 | const char *c; |
a0d0e21e | 4394 | STRLEN clen; |
3c6ef0a5 FC |
4395 | SSize_t iters = 0; |
4396 | SSize_t maxiters; | |
a0d0e21e | 4397 | bool once; |
ef07e810 | 4398 | U8 rxtainted = 0; /* holds various SUBST_TAINT_* flag bits. |
1604cfb0 | 4399 | See "how taint works" above */ |
a0d0e21e | 4400 | char *orig; |
1ed74d04 | 4401 | U8 r_flags; |
eb578fdb | 4402 | REGEXP *rx = PM_GETRE(pm); |
196a02af | 4403 | regexp *prog = ReANY(rx); |
a0d0e21e LW |
4404 | STRLEN len; |
4405 | int force_on_match = 0; | |
0bcc34c2 | 4406 | const I32 oldsave = PL_savestack_ix; |
26a74523 | 4407 | bool doutf8 = FALSE; /* whether replacement is in utf8 */ |
db2c6cb3 | 4408 | #ifdef PERL_ANY_COW |
106d9a13 | 4409 | bool was_cow; |
ed252734 | 4410 | #endif |
a0714e2c | 4411 | SV *nsv = NULL; |
b770e143 | 4412 | /* known replacement string? */ |
eb578fdb | 4413 | SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL; |
a0d0e21e | 4414 | |
f410a211 NC |
4415 | PERL_ASYNC_CHECK(); |
4416 | ||
533c011a | 4417 | if (PL_op->op_flags & OPf_STACKED) |
1604cfb0 | 4418 | TARG = POPs; |
a0d0e21e | 4419 | else { |
9399c607 DM |
4420 | if (ARGTARG) |
4421 | GETTARGET; | |
4422 | else { | |
4423 | TARG = DEFSV; | |
4424 | } | |
1604cfb0 | 4425 | EXTEND(SP,1); |
1c846c1f | 4426 | } |
d9f424b2 | 4427 | |
64534138 | 4428 | SvGETMAGIC(TARG); /* must come before cow check */ |
db2c6cb3 | 4429 | #ifdef PERL_ANY_COW |
106d9a13 DM |
4430 | /* note that a string might get converted to COW during matching */ |
4431 | was_cow = cBOOL(SvIsCOW(TARG)); | |
ed252734 | 4432 | #endif |
d13a5d3b TC |
4433 | if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) { |
4434 | #ifndef PERL_ANY_COW | |
1604cfb0 MS |
4435 | if (SvIsCOW(TARG)) |
4436 | sv_force_normal_flags(TARG,0); | |
d13a5d3b | 4437 | #endif |
1604cfb0 MS |
4438 | if ((SvREADONLY(TARG) |
4439 | || ( ((SvTYPE(TARG) == SVt_PVGV && isGV_with_GP(TARG)) | |
4440 | || SvTYPE(TARG) > SVt_PVLV) | |
4441 | && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))) | |
4442 | Perl_croak_no_modify(); | |
d13a5d3b | 4443 | } |
8ec5e241 NIS |
4444 | PUTBACK; |
4445 | ||
6ac6605d DM |
4446 | orig = SvPV_nomg(TARG, len); |
4447 | /* note we don't (yet) force the var into being a string; if we fail | |
92711104 | 4448 | * to match, we leave as-is; on successful match however, we *will* |
6ac6605d | 4449 | * coerce into a string, then repeat the match */ |
4499db73 | 4450 | if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV || SvVOK(TARG)) |
1604cfb0 | 4451 | force_on_match = 1; |
20be6587 DM |
4452 | |
4453 | /* only replace once? */ | |
4454 | once = !(rpm->op_pmflags & PMf_GLOBAL); | |
4455 | ||
ef07e810 | 4456 | /* See "how taint works" above */ |
284167a5 | 4457 | if (TAINTING_get) { |
1604cfb0 MS |
4458 | rxtainted = ( |
4459 | (SvTAINTED(TARG) ? SUBST_TAINT_STR : 0) | |
4460 | | (RXp_ISTAINTED(prog) ? SUBST_TAINT_PAT : 0) | |
4461 | | ((pm->op_pmflags & PMf_RETAINT) ? SUBST_TAINT_RETAINT : 0) | |
4462 | | (( (once && !(rpm->op_pmflags & PMf_NONDESTRUCT)) | |
5e501dc5 | 4463 | || (PL_op->op_private & OPpTRUEBOOL)) ? SUBST_TAINT_BOOLRET : 0)); |
1604cfb0 | 4464 | TAINT_NOT; |
20be6587 | 4465 | } |
a12c0f56 | 4466 | |
a0d0e21e | 4467 | force_it: |
6ac6605d | 4468 | if (!pm || !orig) |
1604cfb0 | 4469 | DIE(aTHX_ "panic: pp_subst, pm=%p, orig=%p", pm, orig); |
a0d0e21e | 4470 | |
6ac6605d | 4471 | strend = orig + len; |
be76ad45 LM |
4472 | /* We can match twice at each position, once with zero-length, |
4473 | * second time with non-zero. | |
4474 | * Don't handle utf8 specially; we can use length-in-bytes as an | |
4475 | * upper bound on length-in-characters, and avoid the cpu-cost of | |
4476 | * computing a tighter bound. */ | |
4477 | maxiters = 2 * len + 10; | |
a0d0e21e | 4478 | |
794826f4 | 4479 | /* handle the empty pattern */ |
196a02af | 4480 | if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) { |
5585e758 YO |
4481 | if (PL_curpm == PL_reg_curpm) { |
4482 | if (PL_curpm_under) { | |
4483 | if (PL_curpm_under == PL_reg_curpm) { | |
4484 | Perl_croak(aTHX_ "Infinite recursion via empty pattern"); | |
4485 | } else { | |
4486 | pm = PL_curpm_under; | |
4487 | } | |
4488 | } | |
4489 | } else { | |
4490 | pm = PL_curpm; | |
4491 | } | |
4492 | rx = PM_GETRE(pm); | |
196a02af | 4493 | prog = ReANY(rx); |
a0d0e21e | 4494 | } |
6502e081 | 4495 | |
6e240d0b | 4496 | #ifdef PERL_SAWAMPERSAND |
196a02af | 4497 | r_flags = ( RXp_NPARENS(prog) |
6502e081 | 4498 | || PL_sawampersand |
196a02af | 4499 | || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY)) |
5b0e71e9 | 4500 | || (rpm->op_pmflags & PMf_KEEPCOPY) |
6502e081 DM |
4501 | ) |
4502 | ? REXEC_COPY_STR | |
4503 | : 0; | |
6e240d0b FC |
4504 | #else |
4505 | r_flags = REXEC_COPY_STR; | |
4506 | #endif | |
7fba1cd6 | 4507 | |
0395280b | 4508 | if (!CALLREGEXEC(rx, orig, strend, orig, 0, TARG, NULL, r_flags)) |
8b64c330 | 4509 | { |
1604cfb0 MS |
4510 | SPAGAIN; |
4511 | PUSHs(rpm->op_pmflags & PMf_NONDESTRUCT ? TARG : &PL_sv_no); | |
4512 | LEAVE_SCOPE(oldsave); | |
4513 | RETURN; | |
5e79dfb9 | 4514 | } |
1754320d FC |
4515 | PL_curpm = pm; |
4516 | ||
71be2cbc | 4517 | /* known replacement string? */ |
f272994b | 4518 | if (dstr) { |
1604cfb0 MS |
4519 | /* replacement needing upgrading? */ |
4520 | if (DO_UTF8(TARG) && !doutf8) { | |
4521 | nsv = sv_newmortal(); | |
4522 | SvSetSV(nsv, dstr); | |
4523 | sv_utf8_upgrade(nsv); | |
4524 | c = SvPV_const(nsv, clen); | |
4525 | doutf8 = TRUE; | |
4526 | } | |
4527 | else { | |
4528 | c = SvPV_const(dstr, clen); | |
4529 | doutf8 = DO_UTF8(dstr); | |
4530 | } | |
4531 | ||
4532 | if (UNLIKELY(TAINT_get)) | |
4533 | rxtainted |= SUBST_TAINT_REPL; | |
f272994b A |
4534 | } |
4535 | else { | |
1604cfb0 MS |
4536 | c = NULL; |
4537 | doutf8 = FALSE; | |
f272994b A |
4538 | } |
4539 | ||
71be2cbc | 4540 | /* can do inplace substitution? */ |
ed252734 | 4541 | if (c |
db2c6cb3 | 4542 | #ifdef PERL_ANY_COW |
1604cfb0 | 4543 | && !was_cow |
ed252734 | 4544 | #endif |
196a02af | 4545 | && (I32)clen <= RXp_MINLENRET(prog) |
9cefd268 FC |
4546 | && ( once |
4547 | || !(r_flags & REXEC_COPY_STR) | |
196a02af | 4548 | || (!SvGMAGICAL(dstr) && !(RXp_EXTFLAGS(prog) & RXf_EVAL_SEEN)) |
9cefd268 | 4549 | ) |
196a02af | 4550 | && !(RXp_EXTFLAGS(prog) & RXf_NO_INPLACE_SUBST) |
1604cfb0 MS |
4551 | && (!doutf8 || SvUTF8(TARG)) |
4552 | && !(rpm->op_pmflags & PMf_NONDESTRUCT)) | |
8b030b38 | 4553 | { |
ec911639 | 4554 | |
db2c6cb3 | 4555 | #ifdef PERL_ANY_COW |
106d9a13 | 4556 | /* string might have got converted to COW since we set was_cow */ |
1604cfb0 MS |
4557 | if (SvIsCOW(TARG)) { |
4558 | if (!force_on_match) | |
4559 | goto have_a_cow; | |
4560 | assert(SvVOK(TARG)); | |
4561 | } | |
ed252734 | 4562 | #endif |
1604cfb0 | 4563 | if (force_on_match) { |
6ac6605d DM |
4564 | /* redo the first match, this time with the orig var |
4565 | * forced into being a string */ | |
1604cfb0 MS |
4566 | force_on_match = 0; |
4567 | orig = SvPV_force_nomg(TARG, len); | |
4568 | goto force_it; | |
4569 | } | |
39b40493 | 4570 | |
1604cfb0 | 4571 | if (once) { |
c67ab8f2 | 4572 | char *d, *m; |
1604cfb0 MS |
4573 | if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */ |
4574 | rxtainted |= SUBST_TAINT_PAT; | |
3f11a285 YO |
4575 | m = orig + RXp_OFFS_START(prog,0); |
4576 | d = orig + RXp_OFFS_END(prog,0); | |
1604cfb0 MS |
4577 | s = orig; |
4578 | if (m - s > strend - d) { /* faster to shorten from end */ | |
2ec7214c | 4579 | I32 i; |
1604cfb0 MS |
4580 | if (clen) { |
4581 | Copy(c, m, clen, char); | |
4582 | m += clen; | |
4583 | } | |
4584 | i = strend - d; | |
4585 | if (i > 0) { | |
4586 | Move(d, m, i, char); | |
4587 | m += i; | |
4588 | } | |
4589 | *m = '\0'; | |
4590 | SvCUR_set(TARG, m - s); | |
4591 | } | |
4592 | else { /* faster from front */ | |
2ec7214c | 4593 | I32 i = m - s; |
1604cfb0 | 4594 | d -= clen; |
2ec7214c DM |
4595 | if (i > 0) |
4596 | Move(s, d - i, i, char); | |
1604cfb0 MS |
4597 | sv_chop(TARG, d-i); |
4598 | if (clen) | |
4599 | Copy(c, d, clen, char); | |
4600 | } | |
4601 | SPAGAIN; | |
4602 | PUSHs(&PL_sv_yes); | |
4603 | } | |
4604 | else { | |
c67ab8f2 | 4605 | char *d, *m; |
3f11a285 | 4606 | d = s = RXp_OFFS_START(prog,0) + orig; |
1604cfb0 | 4607 | do { |
2b25edcf | 4608 | I32 i; |
1604cfb0 MS |
4609 | if (UNLIKELY(iters++ > maxiters)) |
4610 | DIE(aTHX_ "Substitution loop"); | |
196a02af | 4611 | /* run time pattern taint, eg locale */ |
1604cfb0 MS |
4612 | if (UNLIKELY(RXp_MATCH_TAINTED(prog))) |
4613 | rxtainted |= SUBST_TAINT_PAT; | |
3f11a285 | 4614 | m = RXp_OFFS_START(prog,0) + orig; |
1604cfb0 MS |
4615 | if ((i = m - s)) { |
4616 | if (s != d) | |
4617 | Move(s, d, i, char); | |
4618 | d += i; | |
4619 | } | |
4620 | if (clen) { | |
4621 | Copy(c, d, clen, char); | |
4622 | d += clen; | |
4623 | } | |
3f11a285 | 4624 | s = RXp_OFFS_END(prog,0) + orig; |
1604cfb0 MS |
4625 | } while (CALLREGEXEC(rx, s, strend, orig, |
4626 | s == m, /* don't match same null twice */ | |
4627 | TARG, NULL, | |
d5e7783a | 4628 | REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW)); |
1604cfb0 | 4629 | if (s != d) { |
2b25edcf | 4630 | I32 i = strend - s; |
1604cfb0 MS |
4631 | SvCUR_set(TARG, d - SvPVX_const(TARG) + i); |
4632 | Move(s, d, i+1, char); /* include the NUL */ | |
4633 | } | |
4634 | SPAGAIN; | |
04d59685 | 4635 | assert(iters); |
7b394f12 | 4636 | if (PL_op->op_private & OPpTRUEBOOL) |
04d59685 | 4637 | PUSHs(&PL_sv_yes); |
7b394f12 DM |
4638 | else |
4639 | mPUSHi(iters); | |
1604cfb0 | 4640 | } |
a0d0e21e | 4641 | } |
ff6e92e8 | 4642 | else { |
1604cfb0 | 4643 | bool first; |
c67ab8f2 | 4644 | char *m; |
1604cfb0 MS |
4645 | SV *repl; |
4646 | if (force_on_match) { | |
6ac6605d DM |
4647 | /* redo the first match, this time with the orig var |
4648 | * forced into being a string */ | |
1604cfb0 MS |
4649 | force_on_match = 0; |
4650 | if (rpm->op_pmflags & PMf_NONDESTRUCT) { | |
4651 | /* I feel that it should be possible to avoid this mortal copy | |
4652 | given that the code below copies into a new destination. | |
4653 | However, I suspect it isn't worth the complexity of | |
4654 | unravelling the C<goto force_it> for the small number of | |
4655 | cases where it would be viable to drop into the copy code. */ | |
4656 | TARG = sv_2mortal(newSVsv(TARG)); | |
4657 | } | |
4658 | orig = SvPV_force_nomg(TARG, len); | |
4659 | goto force_it; | |
4660 | } | |
db2c6cb3 | 4661 | #ifdef PERL_ANY_COW |
ed252734 NC |
4662 | have_a_cow: |
4663 | #endif | |
1604cfb0 MS |
4664 | if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */ |
4665 | rxtainted |= SUBST_TAINT_PAT; | |
4666 | repl = dstr; | |
3f11a285 | 4667 | s = RXp_OFFS_START(prog,0) + orig; |
1604cfb0 | 4668 | dstr = newSVpvn_flags(orig, s-orig, |
0395280b | 4669 | SVs_TEMP | (DO_UTF8(TARG) ? SVf_UTF8 : 0)); |
1604cfb0 MS |
4670 | if (!c) { |
4671 | PERL_CONTEXT *cx; | |
4672 | SPAGAIN; | |
0395280b | 4673 | m = orig; |
1604cfb0 MS |
4674 | /* note that a whole bunch of local vars are saved here for |
4675 | * use by pp_substcont: here's a list of them in case you're | |
4676 | * searching for places in this sub that uses a particular var: | |
4677 | * iters maxiters r_flags oldsave rxtainted orig dstr targ | |
4678 | * s m strend rx once */ | |
4679 | CX_PUSHSUBST(cx); | |
4680 | RETURNOP(cPMOP->op_pmreplrootu.op_pmreplroot); | |
4681 | } | |
4682 | first = TRUE; | |
4683 | do { | |
4684 | if (UNLIKELY(iters++ > maxiters)) | |
4685 | DIE(aTHX_ "Substitution loop"); | |
4686 | if (UNLIKELY(RXp_MATCH_TAINTED(prog))) | |
4687 | rxtainted |= SUBST_TAINT_PAT; | |
4688 | if (RXp_MATCH_COPIED(prog) && RXp_SUBBEG(prog) != orig) { | |
4689 | char *old_s = s; | |
4690 | char *old_orig = orig; | |
196a02af | 4691 | assert(RXp_SUBOFFSET(prog) == 0); |
c67ab8f2 | 4692 | |
1604cfb0 MS |
4693 | orig = RXp_SUBBEG(prog); |
4694 | s = orig + (old_s - old_orig); | |
4695 | strend = s + (strend - old_s); | |
4696 | } | |
3f11a285 | 4697 | m = RXp_OFFS_START(prog,0) + orig; |
1604cfb0 | 4698 | sv_catpvn_nomg_maybeutf8(dstr, s, m - s, DO_UTF8(TARG)); |
3f11a285 | 4699 | s = RXp_OFFS_END(prog,0) + orig; |
1604cfb0 MS |
4700 | if (first) { |
4701 | /* replacement already stringified */ | |
4702 | if (clen) | |
4703 | sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8); | |
4704 | first = FALSE; | |
4705 | } | |
4706 | else { | |
4707 | sv_catsv(dstr, repl); | |
4708 | } | |
4709 | if (once) | |
4710 | break; | |
4711 | } while (CALLREGEXEC(rx, s, strend, orig, | |
ff27773b | 4712 | s == m, /* Yields minend of 0 or 1 */ |
1604cfb0 | 4713 | TARG, NULL, |
d5e7783a | 4714 | REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW)); |
aba224f7 | 4715 | assert(strend >= s); |
1604cfb0 MS |
4716 | sv_catpvn_nomg_maybeutf8(dstr, s, strend - s, DO_UTF8(TARG)); |
4717 | ||
4718 | if (rpm->op_pmflags & PMf_NONDESTRUCT) { | |
4719 | /* From here on down we're using the copy, and leaving the original | |
4720 | untouched. */ | |
4721 | TARG = dstr; | |
4722 | SPAGAIN; | |
4723 | PUSHs(dstr); | |
4724 | } else { | |
db2c6cb3 | 4725 | #ifdef PERL_ANY_COW |
1604cfb0 MS |
4726 | /* The match may make the string COW. If so, brilliant, because |
4727 | that's just saved us one malloc, copy and free - the regexp has | |
4728 | donated the old buffer, and we malloc an entirely new one, rather | |
4729 | than the regexp malloc()ing a buffer and copying our original, | |
4730 | only for us to throw it away here during the substitution. */ | |
4731 | if (SvIsCOW(TARG)) { | |
4732 | sv_force_normal_flags(TARG, SV_COW_DROP_PV); | |
4733 | } else | |
ed252734 | 4734 | #endif |
1604cfb0 MS |
4735 | { |
4736 | SvPV_free(TARG); | |
4737 | } | |
4738 | SvPV_set(TARG, SvPVX(dstr)); | |
4739 | SvCUR_set(TARG, SvCUR(dstr)); | |
4740 | SvLEN_set(TARG, SvLEN(dstr)); | |
4741 | SvFLAGS(TARG) |= SvUTF8(dstr); | |
4742 | SvPV_set(dstr, NULL); | |
4743 | ||
4744 | SPAGAIN; | |
c352ee5f | 4745 | if (PL_op->op_private & OPpTRUEBOOL) |
04d59685 | 4746 | PUSHs(&PL_sv_yes); |
c352ee5f DM |
4747 | else |
4748 | mPUSHi(iters); | |
1604cfb0 | 4749 | } |
8ca8a454 NC |
4750 | } |
4751 | ||
4752 | if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) { | |
1604cfb0 | 4753 | (void)SvPOK_only_UTF8(TARG); |
a0d0e21e | 4754 | } |
20be6587 | 4755 | |
ef07e810 | 4756 | /* See "how taint works" above */ |
284167a5 | 4757 | if (TAINTING_get) { |
1604cfb0 MS |
4758 | if ((rxtainted & SUBST_TAINT_PAT) || |
4759 | ((rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) == | |
4760 | (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) | |
4761 | ) | |
4762 | (RXp_MATCH_TAINTED_on(prog)); /* taint $1 et al */ | |
4763 | ||
4764 | if (!(rxtainted & SUBST_TAINT_BOOLRET) | |
4765 | && (rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT)) | |
4766 | ) | |
4767 | SvTAINTED_on(TOPs); /* taint return value */ | |
4768 | else | |
4769 | SvTAINTED_off(TOPs); /* may have got tainted earlier */ | |
4770 | ||
4771 | /* needed for mg_set below */ | |
4772 | TAINT_set( | |
4773 | cBOOL(rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)) | |
284167a5 | 4774 | ); |
1604cfb0 | 4775 | SvTAINT(TARG); |
20be6587 DM |
4776 | } |
4777 | SvSETMAGIC(TARG); /* PL_tainted must be correctly set for this mg_set */ | |
4778 | TAINT_NOT; | |
f1a76097 DM |
4779 | LEAVE_SCOPE(oldsave); |
4780 | RETURN; | |
a0d0e21e LW |
4781 | } |
4782 | ||
4783 | PP(pp_grepwhile) | |
4784 | { | |
20b7effb | 4785 | dSP; |
f4c975aa | 4786 | dPOPss; |
a0d0e21e | 4787 | |
f4c975aa | 4788 | if (SvTRUE_NN(sv)) |
1604cfb0 | 4789 | PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr]; |
3280af22 | 4790 | ++*PL_markstack_ptr; |
b2a2a901 | 4791 | FREETMPS; |
d343c3ef | 4792 | LEAVE_with_name("grep_item"); /* exit inner scope */ |
a0d0e21e LW |
4793 | |
4794 | /* All done yet? */ | |
5d9574c1 | 4795 | if (UNLIKELY(PL_stack_base + *PL_markstack_ptr > SP)) { |
1604cfb0 MS |
4796 | I32 items; |
4797 | const U8 gimme = GIMME_V; | |
4798 | ||
4799 | LEAVE_with_name("grep"); /* exit outer scope */ | |
4800 | (void)POPMARK; /* pop src */ | |
4801 | items = --*PL_markstack_ptr - PL_markstack_ptr[-1]; | |
4802 | (void)POPMARK; /* pop dst */ | |
4803 | SP = PL_stack_base + POPMARK; /* pop original mark */ | |
4804 | if (gimme == G_SCALAR) { | |
7b394f12 | 4805 | if (PL_op->op_private & OPpTRUEBOOL) |
e3ad3bbc | 4806 | PUSHs(items ? &PL_sv_yes : &PL_sv_zero); |
7b394f12 | 4807 | else { |
1604cfb0 MS |
4808 | dTARGET; |
4809 | PUSHi(items); | |
7b394f12 | 4810 | } |
1604cfb0 | 4811 | } |
eb7e169e | 4812 | else if (gimme == G_LIST) |
1604cfb0 MS |
4813 | SP += items; |
4814 | RETURN; | |
a0d0e21e LW |
4815 | } |
4816 | else { | |
1604cfb0 | 4817 | SV *src; |
a0d0e21e | 4818 | |
1604cfb0 MS |
4819 | ENTER_with_name("grep_item"); /* enter inner scope */ |
4820 | SAVEVPTR(PL_curpm); | |
a0d0e21e | 4821 | |
1604cfb0 MS |
4822 | src = PL_stack_base[TOPMARK]; |
4823 | if (SvPADTMP(src)) { | |
4824 | src = PL_stack_base[TOPMARK] = sv_mortalcopy(src); | |
4825 | PL_tmps_floor++; | |
4826 | } | |
4827 | SvTEMP_off(src); | |
4828 | DEFSV_set(src); | |
a0d0e21e | 4829 | |
1604cfb0 | 4830 | RETURNOP(cLOGOP->op_other); |
a0d0e21e LW |
4831 | } |
4832 | } | |
4833 | ||
799da9d7 | 4834 | /* leave_adjust_stacks(): |
f7a874b8 | 4835 | * |
e02ce34b DM |
4836 | * Process a scope's return args (in the range from_sp+1 .. PL_stack_sp), |
4837 | * positioning them at to_sp+1 onwards, and do the equivalent of a | |
4838 | * FREEMPS and TAINT_NOT. | |
4839 | * | |
f7a874b8 DM |
4840 | * Not intended to be called in void context. |
4841 | * | |
799da9d7 DM |
4842 | * When leaving a sub, eval, do{} or other scope, the things that need |
4843 | * doing to process the return args are: | |
f7a874b8 | 4844 | * * in scalar context, only return the last arg (or PL_sv_undef if none); |
799da9d7 DM |
4845 | * * for the types of return that return copies of their args (such |
4846 | * as rvalue sub return), make a mortal copy of every return arg, | |
4847 | * except where we can optimise the copy away without it being | |
4848 | * semantically visible; | |
4849 | * * make sure that the arg isn't prematurely freed; in the case of an | |
4850 | * arg not copied, this may involve mortalising it. For example, in | |
f7a874b8 DM |
4851 | * C<sub f { my $x = ...; $x }>, $x would be freed when we do |
4852 | * CX_LEAVE_SCOPE(cx) unless it's protected or copied. | |
4853 | * | |
799da9d7 DM |
4854 | * What condition to use when deciding whether to pass the arg through |
4855 | * or make a copy, is determined by the 'pass' arg; its valid values are: | |
4856 | * 0: rvalue sub/eval exit | |
4857 | * 1: other rvalue scope exit | |
4858 | * 2: :lvalue sub exit in rvalue context | |
4859 | * 3: :lvalue sub exit in lvalue context and other lvalue scope exits | |
4860 | * | |
f7a874b8 | 4861 | * There is a big issue with doing a FREETMPS. We would like to free any |
799da9d7 | 4862 | * temps created by the last statement which the sub executed, rather than |
f7a874b8 DM |
4863 | * leaving them for the caller. In a situation where a sub call isn't |
4864 | * soon followed by a nextstate (e.g. nested recursive calls, a la | |
4865 | * fibonacci()), temps can accumulate, causing memory and performance | |
4866 | * issues. | |
4867 | * | |
4868 | * On the other hand, we don't want to free any TEMPs which are keeping | |
799da9d7 DM |
4869 | * alive any return args that we skipped copying; nor do we wish to undo |
4870 | * any mortalising done here. | |
f7a874b8 DM |
4871 | * |
4872 | * The solution is to split the temps stack frame into two, with a cut | |
4873 | * point delineating the two halves. We arrange that by the end of this | |
4874 | * function, all the temps stack frame entries we wish to keep are in the | |
799da9d7 | 4875 | * range PL_tmps_floor+1.. tmps_base-1, while the ones to free now are in |
f7a874b8 DM |
4876 | * the range tmps_base .. PL_tmps_ix. During the course of this |
4877 | * function, tmps_base starts off as PL_tmps_floor+1, then increases | |
4878 | * whenever we find or create a temp that we know should be kept. In | |
4879 | * general the stuff above tmps_base is undecided until we reach the end, | |
4880 | * and we may need a sort stage for that. | |
4881 | * | |
4882 | * To determine whether a TEMP is keeping a return arg alive, every | |
4883 | * arg that is kept rather than copied and which has the SvTEMP flag | |
4884 | * set, has the flag temporarily unset, to mark it. At the end we scan | |
799da9d7 | 4885 | * the temps stack frame above the cut for entries without SvTEMP and |
f7a874b8 | 4886 | * keep them, while turning SvTEMP on again. Note that if we die before |
799da9d7 | 4887 | * the SvTEMPs flags are set again, its safe: at worst, subsequent use of |
f7a874b8 DM |
4888 | * those SVs may be slightly less efficient. |
4889 | * | |
4890 | * In practice various optimisations for some common cases mean we can | |
4891 | * avoid most of the scanning and swapping about with the temps stack. | |
4892 | */ | |
4893 | ||
799da9d7 | 4894 | void |
1c23e2bd | 4895 | Perl_leave_adjust_stacks(pTHX_ SV **from_sp, SV **to_sp, U8 gimme, int pass) |
a0d0e21e | 4896 | { |
20b7effb | 4897 | dSP; |
f7a874b8 DM |
4898 | SSize_t tmps_base; /* lowest index into tmps stack that needs freeing now */ |
4899 | SSize_t nargs; | |
4900 | ||
799da9d7 DM |
4901 | PERL_ARGS_ASSERT_LEAVE_ADJUST_STACKS; |
4902 | ||
f7a874b8 DM |
4903 | TAINT_NOT; |
4904 | ||
eb7e169e | 4905 | if (gimme == G_LIST) { |
e02ce34b DM |
4906 | nargs = SP - from_sp; |
4907 | from_sp++; | |
f7a874b8 DM |
4908 | } |
4909 | else { | |
4910 | assert(gimme == G_SCALAR); | |
e02ce34b | 4911 | if (UNLIKELY(from_sp >= SP)) { |
f7a874b8 | 4912 | /* no return args */ |
e02ce34b | 4913 | assert(from_sp == SP); |
f7a874b8 DM |
4914 | EXTEND(SP, 1); |
4915 | *++SP = &PL_sv_undef; | |
f7a874b8 | 4916 | } |
7460e4ea DM |
4917 | from_sp = SP; |
4918 | nargs = 1; | |
f7a874b8 DM |
4919 | } |
4920 | ||
eb7e169e | 4921 | /* common code for G_SCALAR and G_LIST */ |
f7a874b8 DM |
4922 | |
4923 | tmps_base = PL_tmps_floor + 1; | |
4924 | ||
4925 | assert(nargs >= 0); | |
4926 | if (nargs) { | |
4927 | /* pointer version of tmps_base. Not safe across temp stack | |
4928 | * reallocs. */ | |
4929 | SV **tmps_basep; | |
4930 | ||
4931 | EXTEND_MORTAL(nargs); /* one big extend for worst-case scenario */ | |
4932 | tmps_basep = PL_tmps_stack + tmps_base; | |
f7a874b8 DM |
4933 | |
4934 | /* process each return arg */ | |
4935 | ||
4936 | do { | |
4937 | SV *sv = *from_sp++; | |
4938 | ||
4939 | assert(PL_tmps_ix + nargs < PL_tmps_max); | |
3645bb38 DM |
4940 | #ifdef DEBUGGING |
4941 | /* PADTMPs with container set magic shouldn't appear in the | |
4942 | * wild. This assert is more important for pp_leavesublv(), | |
4943 | * but by testing for it here, we're more likely to catch | |
4944 | * bad cases (what with :lvalue subs not being widely | |
4945 | * deployed). The two issues are that for something like | |
4946 | * sub :lvalue { $tied{foo} } | |
4947 | * or | |
4948 | * sub :lvalue { substr($foo,1,2) } | |
4949 | * pp_leavesublv() will croak if the sub returns a PADTMP, | |
4950 | * and currently functions like pp_substr() return a mortal | |
4951 | * rather than using their PADTMP when returning a PVLV. | |
4952 | * This is because the PVLV will hold a ref to $foo, | |
4953 | * so $foo would get delayed in being freed while | |
4954 | * the PADTMP SV remained in the PAD. | |
4955 | * So if this assert fails it means either: | |
4956 | * 1) there is pp code similar to pp_substr that is | |
4957 | * returning a PADTMP instead of a mortal, and probably | |
4958 | * needs fixing, or | |
5d9c1c9a | 4959 | * 2) pp_leavesublv is making unwarranted assumptions |
3645bb38 DM |
4960 | * about always croaking on a PADTMP |
4961 | */ | |
4962 | if (SvPADTMP(sv) && SvSMAGICAL(sv)) { | |
4963 | MAGIC *mg; | |
4964 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { | |
4965 | assert(PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type)); | |
4966 | } | |
4967 | } | |
4968 | #endif | |
f7a874b8 | 4969 | |
799da9d7 DM |
4970 | if ( |
4971 | pass == 0 ? (SvTEMP(sv) && !SvMAGICAL(sv) && SvREFCNT(sv) == 1) | |
4972 | : pass == 1 ? ((SvTEMP(sv) || SvPADTMP(sv)) && !SvMAGICAL(sv) && SvREFCNT(sv) == 1) | |
4973 | : pass == 2 ? (!SvPADTMP(sv)) | |
4974 | : 1) | |
4975 | { | |
4976 | /* pass through: skip copy for logic or optimisation | |
4977 | * reasons; instead mortalise it, except that ... */ | |
e02ce34b | 4978 | *++to_sp = sv; |
f7a874b8 | 4979 | |
799da9d7 DM |
4980 | if (SvTEMP(sv)) { |
4981 | /* ... since this SV is an SvTEMP , we don't need to | |
4982 | * re-mortalise it; instead we just need to ensure | |
4983 | * that its existing entry in the temps stack frame | |
4984 | * ends up below the cut and so avoids being freed | |
4985 | * this time round. We mark it as needing to be kept | |
4986 | * by temporarily unsetting SvTEMP; then at the end, | |
4987 | * we shuffle any !SvTEMP entries on the tmps stack | |
4988 | * back below the cut. | |
4989 | * However, there's a significant chance that there's | |
4990 | * a 1:1 correspondence between the first few (or all) | |
4991 | * elements in the return args stack frame and those | |
4992 | * in the temps stack frame; e,g.: | |
4993 | * sub f { ....; map {...} .... }, | |
4994 | * or if we're exiting multiple scopes and one of the | |
4995 | * inner scopes has already made mortal copies of each | |
4996 | * return arg. | |
4997 | * | |
4998 | * If so, this arg sv will correspond to the next item | |
4999 | * on the tmps stack above the cut, and so can be kept | |
5000 | * merely by moving the cut boundary up one, rather | |
5001 | * than messing with SvTEMP. If all args are 1:1 then | |
5002 | * we can avoid the sorting stage below completely. | |
977d0c81 DM |
5003 | * |
5004 | * If there are no items above the cut on the tmps | |
5005 | * stack, then the SvTEMP must comne from an item | |
5006 | * below the cut, so there's nothing to do. | |
799da9d7 | 5007 | */ |
977d0c81 DM |
5008 | if (tmps_basep <= &PL_tmps_stack[PL_tmps_ix]) { |
5009 | if (sv == *tmps_basep) | |
5010 | tmps_basep++; | |
5011 | else | |
5012 | SvTEMP_off(sv); | |
5013 | } | |
799da9d7 | 5014 | } |
75bc488d | 5015 | else if (!SvPADTMP(sv)) { |
799da9d7 | 5016 | /* mortalise arg to avoid it being freed during save |
75bc488d | 5017 | * stack unwinding. Pad tmps don't need mortalising as |
977d0c81 DM |
5018 | * they're never freed. This is the equivalent of |
5019 | * sv_2mortal(SvREFCNT_inc(sv)), except that: | |
799da9d7 DM |
5020 | * * it assumes that the temps stack has already been |
5021 | * extended; | |
5022 | * * it puts the new item at the cut rather than at | |
5023 | * ++PL_tmps_ix, moving the previous occupant there | |
5024 | * instead. | |
5025 | */ | |
5026 | if (!SvIMMORTAL(sv)) { | |
977d0c81 | 5027 | SvREFCNT_inc_simple_void_NN(sv); |
799da9d7 | 5028 | SvTEMP_on(sv); |
977d0c81 DM |
5029 | /* Note that if there's nothing above the cut, |
5030 | * this copies the garbage one slot above | |
5031 | * PL_tmps_ix onto itself. This is harmless (the | |
5032 | * stack's already been extended), but might in | |
5033 | * theory trigger warnings from tools like ASan | |
5034 | */ | |
799da9d7 DM |
5035 | PL_tmps_stack[++PL_tmps_ix] = *tmps_basep; |
5036 | *tmps_basep++ = sv; | |
5037 | } | |
5038 | } | |
f7a874b8 | 5039 | } |
c56d7fa9 RL |
5040 | else if (SvIMMORTAL(sv)){ |
5041 | /* Since SvIMMORTALs are never freed, even when their | |
5042 | * refcount drops to zero, there's no benefit in | |
5043 | * creating a mortal copy. | |
5044 | */ | |
5045 | *++to_sp = sv; | |
5046 | } | |
f7a874b8 DM |
5047 | else { |
5048 | /* Make a mortal copy of the SV. | |
5049 | * The following code is the equivalent of sv_mortalcopy() | |
5050 | * except that: | |
5051 | * * it assumes the temps stack has already been extended; | |
5052 | * * it optimises the copying for some simple SV types; | |
5053 | * * it puts the new item at the cut rather than at | |
5054 | * ++PL_tmps_ix, moving the previous occupant there | |
5055 | * instead. | |
5056 | */ | |
8fcb2425 | 5057 | SV *newsv = newSV_type(SVt_NULL); |
f7a874b8 DM |
5058 | |
5059 | PL_tmps_stack[++PL_tmps_ix] = *tmps_basep; | |
5060 | /* put it on the tmps stack early so it gets freed if we die */ | |
5061 | *tmps_basep++ = newsv; | |
e02ce34b | 5062 | *++to_sp = newsv; |
f7a874b8 DM |
5063 | |
5064 | if (SvTYPE(sv) <= SVt_IV) { | |
5065 | /* arg must be one of undef, IV/UV, or RV: skip | |
5066 | * sv_setsv_flags() and do the copy directly */ | |
5067 | U32 dstflags; | |
5068 | U32 srcflags = SvFLAGS(sv); | |
5069 | ||
5070 | assert(!SvGMAGICAL(sv)); | |
5071 | if (srcflags & (SVf_IOK|SVf_ROK)) { | |
5072 | SET_SVANY_FOR_BODYLESS_IV(newsv); | |
5073 | ||
5074 | if (srcflags & SVf_ROK) { | |
5075 | newsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(sv)); | |
5076 | /* SV type plus flags */ | |
5077 | dstflags = (SVt_IV|SVf_ROK|SVs_TEMP); | |
5078 | } | |
5079 | else { | |
5080 | /* both src and dst are <= SVt_IV, so sv_any | |
5081 | * points to the head; so access the heads | |
5082 | * directly rather than going via sv_any. | |
5083 | */ | |
5084 | assert( &(sv->sv_u.svu_iv) | |
5085 | == &(((XPVIV*) SvANY(sv))->xiv_iv)); | |
5086 | assert( &(newsv->sv_u.svu_iv) | |
5087 | == &(((XPVIV*) SvANY(newsv))->xiv_iv)); | |
5088 | newsv->sv_u.svu_iv = sv->sv_u.svu_iv; | |
5089 | /* SV type plus flags */ | |
5090 | dstflags = (SVt_IV|SVf_IOK|SVp_IOK|SVs_TEMP | |
5091 | |(srcflags & SVf_IVisUV)); | |
5092 | } | |
5093 | } | |
5094 | else { | |
5095 | assert(!(srcflags & SVf_OK)); | |
5096 | dstflags = (SVt_NULL|SVs_TEMP); /* SV type plus flags */ | |
5097 | } | |
5098 | SvFLAGS(newsv) = dstflags; | |
5099 | ||
5100 | } | |
5101 | else { | |
5102 | /* do the full sv_setsv() */ | |
5103 | SSize_t old_base; | |
5104 | ||
5105 | SvTEMP_on(newsv); | |
5106 | old_base = tmps_basep - PL_tmps_stack; | |
5107 | SvGETMAGIC(sv); | |
5108 | sv_setsv_flags(newsv, sv, SV_DO_COW_SVSETSV); | |
799da9d7 | 5109 | /* the mg_get or sv_setsv might have created new temps |
f7a874b8 DM |
5110 | * or realloced the tmps stack; regrow and reload */ |
5111 | EXTEND_MORTAL(nargs); | |
5112 | tmps_basep = PL_tmps_stack + old_base; | |
5113 | TAINT_NOT; /* Each item is independent */ | |
5114 | } | |
5115 | ||
5116 | } | |
5117 | } while (--nargs); | |
5118 | ||
5119 | /* If there are any temps left above the cut, we need to sort | |
5120 | * them into those to keep and those to free. The only ones to | |
5121 | * keep are those for which we've temporarily unset SvTEMP. | |
5122 | * Work inwards from the two ends at tmps_basep .. PL_tmps_ix, | |
5123 | * swapping pairs as necessary. Stop when we meet in the middle. | |
5124 | */ | |
5125 | { | |
5126 | SV **top = PL_tmps_stack + PL_tmps_ix; | |
5127 | while (tmps_basep <= top) { | |
5128 | SV *sv = *top; | |
5129 | if (SvTEMP(sv)) | |
5130 | top--; | |
5131 | else { | |
5132 | SvTEMP_on(sv); | |
5133 | *top = *tmps_basep; | |
5134 | *tmps_basep = sv; | |
5135 | tmps_basep++; | |
5136 | } | |
5137 | } | |
5138 | } | |
5139 | ||
5140 | tmps_base = tmps_basep - PL_tmps_stack; | |
5141 | } | |
5142 | ||
e02ce34b | 5143 | PL_stack_sp = to_sp; |
f7a874b8 DM |
5144 | |
5145 | /* unrolled FREETMPS() but using tmps_base-1 rather than PL_tmps_floor */ | |
5146 | while (PL_tmps_ix >= tmps_base) { | |
5147 | SV* const sv = PL_tmps_stack[PL_tmps_ix--]; | |
5148 | #ifdef PERL_POISON | |
5149 | PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB); | |
5150 | #endif | |
5151 | if (LIKELY(sv)) { | |
5152 | SvTEMP_off(sv); | |
5153 | SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */ | |
5154 | } | |
5155 | } | |
5156 | } | |
5157 | ||
5158 | ||
c349b9a0 DM |
5159 | /* also tail-called by pp_return */ |
5160 | ||
f7a874b8 DM |
5161 | PP(pp_leavesub) |
5162 | { | |
1c23e2bd | 5163 | U8 gimme; |
eb578fdb | 5164 | PERL_CONTEXT *cx; |
f7a874b8 | 5165 | SV **oldsp; |
5da525e9 | 5166 | OP *retop; |
a0d0e21e | 5167 | |
4ebe6e95 | 5168 | cx = CX_CUR(); |
61d3b95a DM |
5169 | assert(CxTYPE(cx) == CXt_SUB); |
5170 | ||
5171 | if (CxMULTICALL(cx)) { | |
1f0ba93b DM |
5172 | /* entry zero of a stack is always PL_sv_undef, which |
5173 | * simplifies converting a '()' return into undef in scalar context */ | |
5174 | assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef); | |
1604cfb0 | 5175 | return 0; |
1f0ba93b | 5176 | } |
9850bf21 | 5177 | |
61d3b95a | 5178 | gimme = cx->blk_gimme; |
f7a874b8 | 5179 | oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */ |
1c846c1f | 5180 | |
f7a874b8 DM |
5181 | if (gimme == G_VOID) |
5182 | PL_stack_sp = oldsp; | |
5183 | else | |
e02ce34b | 5184 | leave_adjust_stacks(oldsp, oldsp, gimme, 0); |
1c846c1f | 5185 | |
2f450c1b | 5186 | CX_LEAVE_SCOPE(cx); |
a73d8813 | 5187 | cx_popsub(cx); /* Stack values are safe: release CV and @_ ... */ |
ed8ff0f3 | 5188 | cx_popblock(cx); |
5da525e9 DM |
5189 | retop = cx->blk_sub.retop; |
5190 | CX_POP(cx); | |
a0d0e21e | 5191 | |
5da525e9 | 5192 | return retop; |
a0d0e21e LW |
5193 | } |
5194 | ||
6e45d846 DM |
5195 | |
5196 | /* clear (if possible) or abandon the current @_. If 'abandon' is true, | |
5197 | * forces an abandon */ | |
5198 | ||
5199 | void | |
5200 | Perl_clear_defarray(pTHX_ AV* av, bool abandon) | |
5201 | { | |
6e45d846 DM |
5202 | PERL_ARGS_ASSERT_CLEAR_DEFARRAY; |
5203 | ||
656457d0 | 5204 | if (LIKELY(!abandon && SvREFCNT(av) == 1 && !SvMAGICAL(av))) { |
c3d969bf | 5205 | av_clear(av); |
656457d0 DM |
5206 | AvREIFY_only(av); |
5207 | } | |
c3d969bf | 5208 | else { |
d2a9e960 RL |
5209 | const SSize_t size = AvFILLp(av) + 1; |
5210 | /* The ternary gives consistency with av_extend() */ | |
dbf3614d RL |
5211 | AV *newav = newAV_alloc_x(size < PERL_ARRAY_NEW_MIN_KEY ? |
5212 | PERL_ARRAY_NEW_MIN_KEY : size); | |
656457d0 DM |
5213 | AvREIFY_only(newav); |
5214 | PAD_SVl(0) = MUTABLE_SV(newav); | |
c3d969bf | 5215 | SvREFCNT_dec_NN(av); |
c3d969bf | 5216 | } |
6e45d846 DM |
5217 | } |
5218 | ||
5219 | ||
a0d0e21e LW |
5220 | PP(pp_entersub) |
5221 | { | |
20b7effb | 5222 | dSP; dPOPss; |
a0d0e21e | 5223 | GV *gv; |
eb578fdb KW |
5224 | CV *cv; |
5225 | PERL_CONTEXT *cx; | |
8ae997c5 | 5226 | I32 old_savestack_ix; |
a0d0e21e | 5227 | |
f5719c02 | 5228 | if (UNLIKELY(!sv)) |
1604cfb0 | 5229 | goto do_die; |
1ff56747 DM |
5230 | |
5231 | /* Locate the CV to call: | |
5232 | * - most common case: RV->CV: f(), $ref->(): | |
5233 | * note that if a sub is compiled before its caller is compiled, | |
5234 | * the stash entry will be a ref to a CV, rather than being a GV. | |
5235 | * - second most common case: CV: $ref->method() | |
5236 | */ | |
5237 | ||
5238 | /* a non-magic-RV -> CV ? */ | |
5239 | if (LIKELY( (SvFLAGS(sv) & (SVf_ROK|SVs_GMG)) == SVf_ROK)) { | |
5240 | cv = MUTABLE_CV(SvRV(sv)); | |
5241 | if (UNLIKELY(SvOBJECT(cv))) /* might be overloaded */ | |
5242 | goto do_ref; | |
5243 | } | |
5244 | else | |
5245 | cv = MUTABLE_CV(sv); | |
5246 | ||
5247 | /* a CV ? */ | |
5248 | if (UNLIKELY(SvTYPE(cv) != SVt_PVCV)) { | |
5249 | /* handle all the weird cases */ | |
313107ce | 5250 | switch (SvTYPE(sv)) { |
1ff56747 DM |
5251 | case SVt_PVLV: |
5252 | if (!isGV_with_GP(sv)) | |
5253 | goto do_default; | |
5254 | /* FALLTHROUGH */ | |
313107ce | 5255 | case SVt_PVGV: |
1ff56747 DM |
5256 | cv = GvCVu((const GV *)sv); |
5257 | if (UNLIKELY(!cv)) { | |
313107ce DM |
5258 | HV *stash; |
5259 | cv = sv_2cv(sv, &stash, &gv, 0); | |
1ff56747 DM |
5260 | if (!cv) { |
5261 | old_savestack_ix = PL_savestack_ix; | |
5262 | goto try_autoload; | |
5263 | } | |
313107ce DM |
5264 | } |
5265 | break; | |
1ff56747 | 5266 | |
313107ce | 5267 | default: |
1ff56747 | 5268 | do_default: |
313107ce DM |
5269 | SvGETMAGIC(sv); |
5270 | if (SvROK(sv)) { | |
1ff56747 DM |
5271 | do_ref: |
5272 | if (UNLIKELY(SvAMAGIC(sv))) { | |
313107ce DM |
5273 | sv = amagic_deref_call(sv, to_cv_amg); |
5274 | /* Don't SPAGAIN here. */ | |
5275 | } | |
5276 | } | |
5277 | else { | |
5278 | const char *sym; | |
5279 | STRLEN len; | |
1ff56747 | 5280 | if (UNLIKELY(!SvOK(sv))) |
313107ce | 5281 | DIE(aTHX_ PL_no_usym, "a subroutine"); |
1ff56747 | 5282 | |
313107ce DM |
5283 | sym = SvPV_nomg_const(sv, len); |
5284 | if (PL_op->op_private & HINT_STRICT_REFS) | |
5285 | DIE(aTHX_ "Can't use string (\"%" SVf32 "\"%s) as a subroutine ref while \"strict refs\" in use", sv, len>32 ? "..." : ""); | |
5286 | cv = get_cvn_flags(sym, len, GV_ADD|SvUTF8(sv)); | |
5287 | break; | |
5288 | } | |
5289 | cv = MUTABLE_CV(SvRV(sv)); | |
1ff56747 | 5290 | if (LIKELY(SvTYPE(cv) == SVt_PVCV)) |
313107ce | 5291 | break; |
924ba076 | 5292 | /* FALLTHROUGH */ |
313107ce DM |
5293 | case SVt_PVHV: |
5294 | case SVt_PVAV: | |
1ff56747 | 5295 | do_die: |
313107ce | 5296 | DIE(aTHX_ "Not a CODE reference"); |
313107ce | 5297 | } |
f5719c02 | 5298 | } |
a0d0e21e | 5299 | |
8ae997c5 | 5300 | /* At this point we want to save PL_savestack_ix, either by doing a |
a73d8813 | 5301 | * cx_pushsub(), or for XS, doing an ENTER. But we don't yet know the final |
8ae997c5 | 5302 | * CV we will be using (so we don't know whether its XS, so we can't |
a73d8813 | 5303 | * cx_pushsub() or ENTER yet), and determining cv may itself push stuff on |
8ae997c5 DM |
5304 | * the save stack. So remember where we are currently on the save |
5305 | * stack, and later update the CX or scopestack entry accordingly. */ | |
5306 | old_savestack_ix = PL_savestack_ix; | |
a0d0e21e | 5307 | |
f29834c6 DM |
5308 | /* these two fields are in a union. If they ever become separate, |
5309 | * we have to test for both of them being null below */ | |
9a28816a | 5310 | assert(cv); |
f29834c6 DM |
5311 | assert((void*)&CvROOT(cv) == (void*)&CvXSUB(cv)); |
5312 | while (UNLIKELY(!CvROOT(cv))) { | |
1604cfb0 MS |
5313 | GV* autogv; |
5314 | SV* sub_name; | |
5315 | ||
5316 | /* anonymous or undef'd function leaves us no recourse */ | |
5317 | if (CvLEXICAL(cv) && CvHASGV(cv)) | |
5318 | DIE(aTHX_ "Undefined subroutine &%" SVf " called", | |
5319 | SVfARG(cv_name(cv, NULL, 0))); | |
5320 | if (CvANON(cv) || !CvHASGV(cv)) { | |
5321 | DIE(aTHX_ "Undefined subroutine called"); | |
5322 | } | |
5323 | ||
5324 | /* autoloaded stub? */ | |
5325 | if (cv != GvCV(gv = CvGV(cv))) { | |
5326 | cv = GvCV(gv); | |
5327 | } | |
5328 | /* should call AUTOLOAD now? */ | |
5329 | else { | |
7b52d656 | 5330 | try_autoload: |
1604cfb0 | 5331 | autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), |
1de22db2 FC |
5332 | (GvNAMEUTF8(gv) ? SVf_UTF8 : 0) |
5333 | |(PL_op->op_flags & OPf_REF | |
5334 | ? GV_AUTOLOAD_ISMETHOD | |
5335 | : 0)); | |
b4b431d9 | 5336 | cv = autogv ? GvCV(autogv) : NULL; |
1604cfb0 MS |
5337 | } |
5338 | if (!cv) { | |
b4b431d9 DM |
5339 | sub_name = sv_newmortal(); |
5340 | gv_efullname3(sub_name, gv, NULL); | |
147e3846 | 5341 | DIE(aTHX_ "Undefined subroutine &%" SVf " called", SVfARG(sub_name)); |
b4b431d9 | 5342 | } |
a0d0e21e LW |
5343 | } |
5344 | ||
4f25d042 DM |
5345 | /* unrolled "CvCLONE(cv) && ! CvCLONED(cv)" */ |
5346 | if (UNLIKELY((CvFLAGS(cv) & (CVf_CLONE|CVf_CLONED)) == CVf_CLONE)) | |
1604cfb0 | 5347 | DIE(aTHX_ "Closure prototype called"); |
654c6d71 | 5348 | |
f5719c02 DM |
5349 | if (UNLIKELY((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) |
5350 | && !CvNODEBUG(cv))) | |
5351 | { | |
1604cfb0 MS |
5352 | Perl_get_db_sub(aTHX_ &sv, cv); |
5353 | if (CvISXSUB(cv)) | |
5354 | PL_curcopdb = PL_curcop; | |
1ad62f64 BR |
5355 | if (CvLVALUE(cv)) { |
5356 | /* check for lsub that handles lvalue subroutines */ | |
1604cfb0 | 5357 | cv = GvCV(gv_fetchpvs("DB::lsub", GV_ADDMULTI, SVt_PVCV)); |
1ad62f64 | 5358 | /* if lsub not found then fall back to DB::sub */ |
1604cfb0 | 5359 | if (!cv) cv = GvCV(PL_DBsub); |
1ad62f64 BR |
5360 | } else { |
5361 | cv = GvCV(PL_DBsub); | |
5362 | } | |
a9ef256d | 5363 | |
1604cfb0 MS |
5364 | if (!cv || (!CvXSUB(cv) && !CvSTART(cv))) |
5365 | DIE(aTHX_ "No DB::sub routine defined"); | |
67caa1fe | 5366 | } |
a0d0e21e | 5367 | |
aed2304a | 5368 | if (!(CvISXSUB(cv))) { |
1604cfb0 MS |
5369 | /* This path taken at least 75% of the time */ |
5370 | dMARK; | |
5371 | PADLIST *padlist; | |
3689ad62 | 5372 | I32 depth; |
44dd5d49 | 5373 | bool hasargs; |
1c23e2bd | 5374 | U8 gimme; |
f5719c02 | 5375 | |
20448bad DM |
5376 | /* keep PADTMP args alive throughout the call (we need to do this |
5377 | * because @_ isn't refcounted). Note that we create the mortals | |
5378 | * in the caller's tmps frame, so they won't be freed until after | |
5379 | * we return from the sub. | |
5380 | */ | |
1604cfb0 | 5381 | { |
20448bad DM |
5382 | SV **svp = MARK; |
5383 | while (svp < SP) { | |
5384 | SV *sv = *++svp; | |
5385 | if (!sv) | |
5386 | continue; | |
5387 | if (SvPADTMP(sv)) | |
5388 | *svp = sv = sv_mortalcopy(sv); | |
5389 | SvTEMP_off(sv); | |
1604cfb0 | 5390 | } |
20448bad DM |
5391 | } |
5392 | ||
801bbf61 | 5393 | gimme = GIMME_V; |
1604cfb0 | 5394 | cx = cx_pushblock(CXt_SUB, gimme, MARK, old_savestack_ix); |
44dd5d49 | 5395 | hasargs = cBOOL(PL_op->op_flags & OPf_STACKED); |
1604cfb0 MS |
5396 | cx_pushsub(cx, cv, PL_op->op_next, hasargs); |
5397 | ||
5398 | padlist = CvPADLIST(cv); | |
5399 | if (UNLIKELY((depth = ++CvDEPTH(cv)) >= 2)) | |
5400 | pad_push(padlist, depth); | |
5401 | PAD_SET_CUR_NOSAVE(padlist, depth); | |
5402 | if (LIKELY(hasargs)) { | |
5403 | AV *const av = MUTABLE_AV(PAD_SVl(0)); | |
bdf02c57 DM |
5404 | SSize_t items; |
5405 | AV **defavp; | |
5406 | ||
1604cfb0 MS |
5407 | defavp = &GvAV(PL_defgv); |
5408 | cx->blk_sub.savearray = *defavp; | |
5409 | *defavp = MUTABLE_AV(SvREFCNT_inc_simple_NN(av)); | |
a0d0e21e | 5410 | |
72f28af4 DM |
5411 | /* it's the responsibility of whoever leaves a sub to ensure |
5412 | * that a clean, empty AV is left in pad[0]. This is normally | |
a73d8813 | 5413 | * done by cx_popsub() */ |
72f28af4 DM |
5414 | assert(!AvREAL(av) && AvFILLp(av) == -1); |
5415 | ||
5416 | items = SP - MARK; | |
1604cfb0 | 5417 | if (UNLIKELY(items - 1 > AvMAX(av))) { |
77d27ef6 | 5418 | SV **ary = AvALLOC(av); |
77d27ef6 | 5419 | Renew(ary, items, SV*); |
00195859 | 5420 | AvMAX(av) = items - 1; |
77d27ef6 SF |
5421 | AvALLOC(av) = ary; |
5422 | AvARRAY(av) = ary; | |
5423 | } | |
5424 | ||
f14cf363 TC |
5425 | if (items) |
5426 | Copy(MARK+1,AvARRAY(av),items,SV*); | |
1604cfb0 MS |
5427 | AvFILLp(av) = items - 1; |
5428 | } | |
5429 | if (UNLIKELY((cx->blk_u16 & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO && | |
5430 | !CvLVALUE(cv))) | |
147e3846 | 5431 | DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf, |
0f948285 | 5432 | SVfARG(cv_name(cv, NULL, 0))); |
1604cfb0 MS |
5433 | /* warning must come *after* we fully set up the context |
5434 | * stuff so that __WARN__ handlers can safely dounwind() | |
5435 | * if they want to | |
5436 | */ | |
5437 | if (UNLIKELY(depth == PERL_SUB_DEPTH_WARN | |
f5719c02 DM |
5438 | && ckWARN(WARN_RECURSION) |
5439 | && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))) | |
1604cfb0 MS |
5440 | sub_crush_depth(cv); |
5441 | RETURNOP(CvSTART(cv)); | |
a0d0e21e | 5442 | } |
f1025168 | 5443 | else { |
1604cfb0 | 5444 | SSize_t markix = TOPMARK; |
71d19c37 | 5445 | bool is_scalar; |
f1025168 | 5446 | |
8ae997c5 DM |
5447 | ENTER; |
5448 | /* pretend we did the ENTER earlier */ | |
1604cfb0 | 5449 | PL_scopestack[PL_scopestack_ix - 1] = old_savestack_ix; |
8ae997c5 | 5450 | |
1604cfb0 MS |
5451 | SAVETMPS; |
5452 | PUTBACK; | |
f1025168 | 5453 | |
1604cfb0 MS |
5454 | if (UNLIKELY(((PL_op->op_private |
5455 | & CX_PUSHSUB_GET_LVALUE_MASK(Perl_is_lvalue_sub) | |
4587c532 | 5456 | ) & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO && |
1604cfb0 | 5457 | !CvLVALUE(cv))) |
147e3846 | 5458 | DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf, |
0f948285 | 5459 | SVfARG(cv_name(cv, NULL, 0))); |
4587c532 | 5460 | |
1604cfb0 MS |
5461 | if (UNLIKELY(!(PL_op->op_flags & OPf_STACKED) && GvAV(PL_defgv))) { |
5462 | /* Need to copy @_ to stack. Alternative may be to | |
5463 | * switch stack to @_, and copy return values | |
5464 | * back. This would allow popping @_ in XSUB, e.g.. XXXX */ | |
5465 | AV * const av = GvAV(PL_defgv); | |
5466 | const SSize_t items = AvFILL(av) + 1; | |
5467 | ||
5468 | if (items) { | |
5469 | SSize_t i = 0; | |
5470 | const bool m = cBOOL(SvRMAGICAL(av)); | |
5471 | /* Mark is at the end of the stack. */ | |
5472 | EXTEND(SP, items); | |
5473 | for (; i < items; ++i) | |
5474 | { | |
5475 | SV *sv; | |
5476 | if (m) { | |
5477 | SV ** const svp = av_fetch(av, i, 0); | |
5478 | sv = svp ? *svp : NULL; | |
5479 | } | |
5480 | else sv = AvARRAY(av)[i]; | |
5481 | if (sv) SP[i+1] = sv; | |
5482 | else { | |
5483 | SP[i+1] = av_nonelem(av, i); | |
5484 | } | |
5485 | } | |
5486 | SP += items; | |
5487 | PUTBACK ; | |
5488 | } | |
5489 | } | |
5490 | else { | |
5491 | SV **mark = PL_stack_base + markix; | |
5492 | SSize_t items = SP - mark; | |
5493 | while (items--) { | |
5494 | mark++; | |
5495 | if (*mark && SvPADTMP(*mark)) { | |
5496 | *mark = sv_mortalcopy(*mark); | |
60779a30 | 5497 | } |
1604cfb0 MS |
5498 | } |
5499 | } | |
5500 | /* We assume first XSUB in &DB::sub is the called one. */ | |
5501 | if (UNLIKELY(PL_curcopdb)) { | |
5502 | SAVEVPTR(PL_curcop); | |
5503 | PL_curcop = PL_curcopdb; | |
5504 | PL_curcopdb = NULL; | |
5505 | } | |
5506 | /* Do we need to open block here? XXXX */ | |
72df79cf | 5507 | |
71d19c37 DM |
5508 | /* calculate gimme here as PL_op might get changed and then not |
5509 | * restored until the LEAVE further down */ | |
5510 | is_scalar = (GIMME_V == G_SCALAR); | |
5511 | ||
1604cfb0 MS |
5512 | /* CvXSUB(cv) must not be NULL because newXS() refuses NULL xsub address */ |
5513 | assert(CvXSUB(cv)); | |
5514 | CvXSUB(cv)(aTHX_ cv); | |
3a76ca88 | 5515 | |
cfbdacd3 TC |
5516 | #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY |
5517 | /* This duplicates the check done in runops_debug(), but provides more | |
5518 | * information in the common case of the fault being with an XSUB. | |
5519 | * | |
5520 | * It should also catch an XSUB pushing more than it extends | |
5521 | * in scalar context. | |
5522 | */ | |
5523 | if (PL_curstackinfo->si_stack_hwm < PL_stack_sp - PL_stack_base) | |
5524 | Perl_croak_nocontext( | |
5525 | "panic: XSUB %s::%s (%s) failed to extend arg stack: " | |
5526 | "base=%p, sp=%p, hwm=%p\n", | |
5527 | HvNAME(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)), CvFILE(cv), | |
5528 | PL_stack_base, PL_stack_sp, | |
5529 | PL_stack_base + PL_curstackinfo->si_stack_hwm); | |
5530 | #endif | |
1604cfb0 MS |
5531 | /* Enforce some sanity in scalar context. */ |
5532 | if (is_scalar) { | |
89a18b40 DM |
5533 | SV **svp = PL_stack_base + markix + 1; |
5534 | if (svp != PL_stack_sp) { | |
5535 | *svp = svp > PL_stack_sp ? &PL_sv_undef : *PL_stack_sp; | |
5536 | PL_stack_sp = svp; | |
5537 | } | |
1604cfb0 MS |
5538 | } |
5539 | LEAVE; | |
5540 | return NORMAL; | |
f1025168 | 5541 | } |
a0d0e21e LW |
5542 | } |
5543 | ||
44a8e56a | 5544 | void |
864dbfa3 | 5545 | Perl_sub_crush_depth(pTHX_ CV *cv) |
44a8e56a | 5546 | { |
7918f24d NC |
5547 | PERL_ARGS_ASSERT_SUB_CRUSH_DEPTH; |
5548 | ||
44a8e56a | 5549 | if (CvANON(cv)) |
1604cfb0 | 5550 | Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine"); |
44a8e56a | 5551 | else { |
1604cfb0 MS |
5552 | Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%" SVf "\"", |
5553 | SVfARG(cv_name(cv,NULL,0))); | |
44a8e56a | 5554 | } |
5555 | } | |
5556 | ||
4fa06845 DM |
5557 | |
5558 | ||
5559 | /* like croak, but report in context of caller */ | |
5560 | ||
5561 | void | |
5562 | Perl_croak_caller(const char *pat, ...) | |
5563 | { | |
5564 | dTHX; | |
5565 | va_list args; | |
5566 | const PERL_CONTEXT *cx = caller_cx(0, NULL); | |
5567 | ||
5568 | /* make error appear at call site */ | |
5569 | assert(cx); | |
5570 | PL_curcop = cx->blk_oldcop; | |
5571 | ||
5572 | va_start(args, pat); | |
5573 | vcroak(pat, &args); | |
5574 | NOT_REACHED; /* NOTREACHED */ | |
5575 | va_end(args); | |
5576 | } | |
5577 | ||
5578 | ||
a0d0e21e LW |
5579 | PP(pp_aelem) |
5580 | { | |
20b7effb | 5581 | dSP; |
a0d0e21e | 5582 | SV** svp; |
a3b680e6 | 5583 | SV* const elemsv = POPs; |
d804643f | 5584 | IV elem = SvIV(elemsv); |
502c6561 | 5585 | AV *const av = MUTABLE_AV(POPs); |
e1ec3a88 | 5586 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
bbfdc870 | 5587 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; |
4ad10a0b VP |
5588 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; |
5589 | bool preeminent = TRUE; | |
be6c24e0 | 5590 | SV *sv; |
a0d0e21e | 5591 | |
5d9574c1 | 5592 | if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))) |
1604cfb0 MS |
5593 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
5594 | "Use of reference \"%" SVf "\" as array index", | |
5595 | SVfARG(elemsv)); | |
5d9574c1 | 5596 | if (UNLIKELY(SvTYPE(av) != SVt_PVAV)) |
1604cfb0 | 5597 | RETPUSHUNDEF; |
4ad10a0b | 5598 | |
5d9574c1 | 5599 | if (UNLIKELY(localizing)) { |
1604cfb0 MS |
5600 | MAGIC *mg; |
5601 | HV *stash; | |
4ad10a0b | 5602 | |
1604cfb0 MS |
5603 | /* If we can determine whether the element exist, |
5604 | * Try to preserve the existenceness of a tied array | |
5605 | * element by using EXISTS and DELETE if possible. | |
5606 | * Fallback to FETCH and STORE otherwise. */ | |
5607 | if (SvCANEXISTDELETE(av)) | |
5608 | preeminent = av_exists(av, elem); | |
4ad10a0b VP |
5609 | } |
5610 | ||
68dc0745 | 5611 | svp = av_fetch(av, elem, lval && !defer); |
a0d0e21e | 5612 | if (lval) { |
2b573ace | 5613 | #ifdef PERL_MALLOC_WRAP |
1604cfb0 MS |
5614 | if (SvUOK(elemsv)) { |
5615 | const UV uv = SvUV(elemsv); | |
5616 | elem = uv > IV_MAX ? IV_MAX : uv; | |
5617 | } | |
5618 | else if (SvNOK(elemsv)) | |
5619 | elem = (IV)SvNV(elemsv); | |
5620 | if (elem > 0) { | |
5621 | MEM_WRAP_CHECK_s(elem,SV*,"Out of memory during array extend"); | |
5622 | } | |
2b573ace | 5623 | #endif |
1604cfb0 MS |
5624 | if (!svp || !*svp) { |
5625 | IV len; | |
5626 | if (!defer) | |
5627 | DIE(aTHX_ PL_no_aelem, elem); | |
5628 | len = av_top_index(av); | |
5629 | /* Resolve a negative index that falls within the array. Leave | |
5630 | it negative it if falls outside the array. */ | |
5631 | if (elem < 0 && len + elem >= 0) | |
5632 | elem = len + elem; | |
5633 | if (elem >= 0 && elem <= len) | |
5634 | /* Falls within the array. */ | |
5635 | PUSHs(av_nonelem(av,elem)); | |
5636 | else | |
5637 | /* Falls outside the array. If it is negative, | |
5638 | magic_setdefelem will use the index for error reporting. | |
5639 | */ | |
5640 | mPUSHs(newSVavdefelem(av, elem, 1)); | |
5641 | RETURN; | |
5642 | } | |
5643 | if (UNLIKELY(localizing)) { | |
5644 | if (preeminent) | |
5645 | save_aelem(av, elem, svp); | |
5646 | else | |
5647 | SAVEADELETE(av, elem); | |
5648 | } | |
5649 | else if (PL_op->op_private & OPpDEREF) { | |
5650 | PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF)); | |
5651 | RETURN; | |
5652 | } | |
a0d0e21e | 5653 | } |
3280af22 | 5654 | sv = (svp ? *svp : &PL_sv_undef); |
39cf747a | 5655 | if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */ |
1604cfb0 | 5656 | mg_get(sv); |
be6c24e0 | 5657 | PUSHs(sv); |
a0d0e21e LW |
5658 | RETURN; |
5659 | } | |
5660 | ||
9026059d | 5661 | SV* |
864dbfa3 | 5662 | Perl_vivify_ref(pTHX_ SV *sv, U32 to_what) |
02a9e968 | 5663 | { |
7918f24d NC |
5664 | PERL_ARGS_ASSERT_VIVIFY_REF; |
5665 | ||
5b295bef | 5666 | SvGETMAGIC(sv); |
02a9e968 | 5667 | if (!SvOK(sv)) { |
1604cfb0 MS |
5668 | if (SvREADONLY(sv)) |
5669 | Perl_croak_no_modify(); | |
5670 | prepare_SV_for_RV(sv); | |
5671 | switch (to_what) { | |
5672 | case OPpDEREF_SV: | |
8fcb2425 | 5673 | SvRV_set(sv, newSV_type(SVt_NULL)); |
1604cfb0 MS |
5674 | break; |
5675 | case OPpDEREF_AV: | |
5676 | SvRV_set(sv, MUTABLE_SV(newAV())); | |
5677 | break; | |
5678 | case OPpDEREF_HV: | |
5679 | SvRV_set(sv, MUTABLE_SV(newHV())); | |
5680 | break; | |
5681 | } | |
5682 | SvROK_on(sv); | |
5683 | SvSETMAGIC(sv); | |
5684 | SvGETMAGIC(sv); | |
02a9e968 | 5685 | } |
9026059d | 5686 | if (SvGMAGICAL(sv)) { |
1604cfb0 MS |
5687 | /* copy the sv without magic to prevent magic from being |
5688 | executed twice */ | |
5689 | SV* msv = sv_newmortal(); | |
5690 | sv_setsv_nomg(msv, sv); | |
5691 | return msv; | |
9026059d GG |
5692 | } |
5693 | return sv; | |
02a9e968 CS |
5694 | } |
5695 | ||
7d6c333c | 5696 | PERL_STATIC_INLINE HV * |
5697 | S_opmethod_stash(pTHX_ SV* meth) | |
f5d5a27c | 5698 | { |
a0d0e21e | 5699 | SV* ob; |
56304f61 | 5700 | HV* stash; |
b55b14d0 | 5701 | |
d648ffcb | 5702 | SV* const sv = PL_stack_base + TOPMARK == PL_stack_sp |
1604cfb0 MS |
5703 | ? (Perl_croak(aTHX_ "Can't call method \"%" SVf "\" without a " |
5704 | "package or object reference", SVfARG(meth)), | |
5705 | (SV *)NULL) | |
5706 | : *(PL_stack_base + TOPMARK + 1); | |
f5d5a27c | 5707 | |
7d6c333c | 5708 | PERL_ARGS_ASSERT_OPMETHOD_STASH; |
d648ffcb | 5709 | |
5d9574c1 | 5710 | if (UNLIKELY(!sv)) |
7156e69a | 5711 | undefined: |
1604cfb0 MS |
5712 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on an undefined value", |
5713 | SVfARG(meth)); | |
4f1b7578 | 5714 | |
d648ffcb | 5715 | if (UNLIKELY(SvGMAGICAL(sv))) mg_get(sv); |
5716 | else if (SvIsCOW_shared_hash(sv)) { /* MyClass->meth() */ | |
1604cfb0 MS |
5717 | stash = gv_stashsv(sv, GV_CACHE_ONLY); |
5718 | if (stash) return stash; | |
d648ffcb | 5719 | } |
5720 | ||
a0d0e21e | 5721 | if (SvROK(sv)) |
1604cfb0 | 5722 | ob = MUTABLE_SV(SvRV(sv)); |
7156e69a | 5723 | else if (!SvOK(sv)) goto undefined; |
a77c16f7 | 5724 | else if (isGV_with_GP(sv)) { |
1604cfb0 MS |
5725 | if (!GvIO(sv)) |
5726 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" " | |
5727 | "without a package or object reference", | |
5728 | SVfARG(meth)); | |
5729 | ob = sv; | |
5730 | if (SvTYPE(ob) == SVt_PVLV && LvTYPE(ob) == 'y') { | |
5731 | assert(!LvTARGLEN(ob)); | |
5732 | ob = LvTARG(ob); | |
5733 | assert(ob); | |
5734 | } | |
5735 | *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV(ob)); | |
a77c16f7 | 5736 | } |
a0d0e21e | 5737 | else { |
1604cfb0 MS |
5738 | /* this isn't a reference */ |
5739 | GV* iogv; | |
f937af42 | 5740 | STRLEN packlen; |
89269094 | 5741 | const char * const packname = SvPV_nomg_const(sv, packlen); |
d283e876 | 5742 | const U32 packname_utf8 = SvUTF8(sv); |
5743 | stash = gv_stashpvn(packname, packlen, packname_utf8 | GV_CACHE_ONLY); | |
7d6c333c | 5744 | if (stash) return stash; |
081fc587 | 5745 | |
1604cfb0 MS |
5746 | if (!(iogv = gv_fetchpvn_flags( |
5747 | packname, packlen, packname_utf8, SVt_PVIO | |
5748 | )) || | |
5749 | !(ob=MUTABLE_SV(GvIO(iogv)))) | |
5750 | { | |
5751 | /* this isn't the name of a filehandle either */ | |
5752 | if (!packlen) | |
5753 | { | |
5754 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" " | |
5755 | "without a package or object reference", | |
5756 | SVfARG(meth)); | |
5757 | } | |
5758 | /* assume it's a package name */ | |
5759 | stash = gv_stashpvn(packname, packlen, packname_utf8); | |
5760 | if (stash) return stash; | |
5761 | else return MUTABLE_HV(sv); | |
5762 | } | |
5763 | /* it _is_ a filehandle name -- replace with a reference */ | |
5764 | *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV(MUTABLE_SV(iogv))); | |
a0d0e21e LW |
5765 | } |
5766 | ||
1f3ffe4c | 5767 | /* if we got here, ob should be an object or a glob */ |
f0d43078 | 5768 | if (!ob || !(SvOBJECT(ob) |
1604cfb0 MS |
5769 | || (isGV_with_GP(ob) |
5770 | && (ob = MUTABLE_SV(GvIO((const GV *)ob))) | |
5771 | && SvOBJECT(ob)))) | |
f0d43078 | 5772 | { |
1604cfb0 MS |
5773 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on unblessed reference", |
5774 | SVfARG((SvPOK(meth) && SvPVX(meth) == PL_isa_DOES) | |
b375e37b BF |
5775 | ? newSVpvs_flags("DOES", SVs_TEMP) |
5776 | : meth)); | |
f0d43078 | 5777 | } |
a0d0e21e | 5778 | |
7d6c333c | 5779 | return SvSTASH(ob); |
5780 | } | |
5781 | ||
5782 | PP(pp_method) | |
5783 | { | |
5784 | dSP; | |
5785 | GV* gv; | |
5786 | HV* stash; | |
5787 | SV* const meth = TOPs; | |
5788 | ||
5789 | if (SvROK(meth)) { | |
5790 | SV* const rmeth = SvRV(meth); | |
5791 | if (SvTYPE(rmeth) == SVt_PVCV) { | |
5792 | SETs(rmeth); | |
5793 | RETURN; | |
5794 | } | |
5795 | } | |
a0d0e21e | 5796 | |
7d6c333c | 5797 | stash = opmethod_stash(meth); |
af09ea45 | 5798 | |
7d6c333c | 5799 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK); |
5800 | assert(gv); | |
5801 | ||
5802 | SETs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); | |
5803 | RETURN; | |
5804 | } | |
5805 | ||
810bd8b7 | 5806 | #define METHOD_CHECK_CACHE(stash,cache,meth) \ |
5807 | const HE* const he = hv_fetch_ent(cache, meth, 0, 0); \ | |
5808 | if (he) { \ | |
5809 | gv = MUTABLE_GV(HeVAL(he)); \ | |
5810 | if (isGV(gv) && GvCV(gv) && (!GvCVGEN(gv) || GvCVGEN(gv) \ | |
5811 | == (PL_sub_generation + HvMROMETA(stash)->cache_gen))) \ | |
5812 | { \ | |
5813 | XPUSHs(MUTABLE_SV(GvCV(gv))); \ | |
5814 | RETURN; \ | |
5815 | } \ | |
5816 | } \ | |
5817 | ||
7d6c333c | 5818 | PP(pp_method_named) |
5819 | { | |
5820 | dSP; | |
5821 | GV* gv; | |
c47242c2 | 5822 | SV* const meth = cMETHOP_meth; |
7d6c333c | 5823 | HV* const stash = opmethod_stash(meth); |
5824 | ||
5825 | if (LIKELY(SvTYPE(stash) == SVt_PVHV)) { | |
810bd8b7 | 5826 | METHOD_CHECK_CACHE(stash, stash, meth); |
f5d5a27c CS |
5827 | } |
5828 | ||
7d6c333c | 5829 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK); |
256d1bb2 | 5830 | assert(gv); |
9b9d0b15 | 5831 | |
7d6c333c | 5832 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); |
5833 | RETURN; | |
5834 | } | |
5835 | ||
5836 | PP(pp_method_super) | |
5837 | { | |
5838 | dSP; | |
5839 | GV* gv; | |
5840 | HV* cache; | |
c47242c2 | 5841 | SV* const meth = cMETHOP_meth; |
7d6c333c | 5842 | HV* const stash = CopSTASH(PL_curcop); |
5843 | /* Actually, SUPER doesn't need real object's (or class') stash at all, | |
5844 | * as it uses CopSTASH. However, we must ensure that object(class) is | |
5845 | * correct (this check is done by S_opmethod_stash) */ | |
5846 | opmethod_stash(meth); | |
5847 | ||
5848 | if ((cache = HvMROMETA(stash)->super)) { | |
810bd8b7 | 5849 | METHOD_CHECK_CACHE(stash, cache, meth); |
5850 | } | |
5851 | ||
5852 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER); | |
5853 | assert(gv); | |
5854 | ||
5855 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); | |
5856 | RETURN; | |
5857 | } | |
5858 | ||
5859 | PP(pp_method_redir) | |
5860 | { | |
5861 | dSP; | |
5862 | GV* gv; | |
c47242c2 PE |
5863 | SV* const meth = cMETHOP_meth; |
5864 | HV* stash = gv_stashsv(cMETHOP_rclass, 0); | |
810bd8b7 | 5865 | opmethod_stash(meth); /* not used but needed for error checks */ |
5866 | ||
5867 | if (stash) { METHOD_CHECK_CACHE(stash, stash, meth); } | |
c47242c2 | 5868 | else stash = MUTABLE_HV(cMETHOP_rclass); |
810bd8b7 | 5869 | |
5870 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK); | |
5871 | assert(gv); | |
5872 | ||
5873 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); | |
5874 | RETURN; | |
5875 | } | |
5876 | ||
5877 | PP(pp_method_redir_super) | |
5878 | { | |
5879 | dSP; | |
5880 | GV* gv; | |
5881 | HV* cache; | |
c47242c2 PE |
5882 | SV* const meth = cMETHOP_meth; |
5883 | HV* stash = gv_stashsv(cMETHOP_rclass, 0); | |
810bd8b7 | 5884 | opmethod_stash(meth); /* not used but needed for error checks */ |
5885 | ||
c47242c2 | 5886 | if (UNLIKELY(!stash)) stash = MUTABLE_HV(cMETHOP_rclass); |
810bd8b7 | 5887 | else if ((cache = HvMROMETA(stash)->super)) { |
5888 | METHOD_CHECK_CACHE(stash, cache, meth); | |
7d6c333c | 5889 | } |
5890 | ||
5891 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER); | |
5892 | assert(gv); | |
5893 | ||
5894 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); | |
5895 | RETURN; | |
a0d0e21e | 5896 | } |
241d1a3b NC |
5897 | |
5898 | /* | |
14d04a33 | 5899 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 5900 | */ |