| 1 | /* pp_hot.c |
| 2 | * |
| 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 |
| 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 | * |
| 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"] |
| 19 | */ |
| 20 | |
| 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 | |
| 34 | #include "EXTERN.h" |
| 35 | #define PERL_IN_PP_HOT_C |
| 36 | #include "perl.h" |
| 37 | |
| 38 | /* Hot code. */ |
| 39 | |
| 40 | PP(pp_const) |
| 41 | { |
| 42 | dSP; |
| 43 | XPUSHs(cSVOP_sv); |
| 44 | RETURN; |
| 45 | } |
| 46 | |
| 47 | PP(pp_nextstate) |
| 48 | { |
| 49 | PL_curcop = (COP*)PL_op; |
| 50 | TAINT_NOT; /* Each statement is presumed innocent */ |
| 51 | PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp; |
| 52 | FREETMPS; |
| 53 | PERL_ASYNC_CHECK(); |
| 54 | return NORMAL; |
| 55 | } |
| 56 | |
| 57 | PP(pp_gvsv) |
| 58 | { |
| 59 | dSP; |
| 60 | EXTEND(SP,1); |
| 61 | if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) |
| 62 | PUSHs(save_scalar(cGVOP_gv)); |
| 63 | else |
| 64 | PUSHs(GvSVn(cGVOP_gv)); |
| 65 | RETURN; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* also used for: pp_lineseq() pp_regcmaybe() pp_scalar() pp_scope() */ |
| 70 | |
| 71 | PP(pp_null) |
| 72 | { |
| 73 | return NORMAL; |
| 74 | } |
| 75 | |
| 76 | /* This is sometimes called directly by pp_coreargs, pp_grepstart and |
| 77 | amagic_call. */ |
| 78 | PP(pp_pushmark) |
| 79 | { |
| 80 | PUSHMARK(PL_stack_sp); |
| 81 | return NORMAL; |
| 82 | } |
| 83 | |
| 84 | PP(pp_stringify) |
| 85 | { |
| 86 | dSP; dTARGET; |
| 87 | SV * const sv = TOPs; |
| 88 | SETs(TARG); |
| 89 | sv_copypv(TARG, sv); |
| 90 | SvSETMAGIC(TARG); |
| 91 | /* no PUTBACK, SETs doesn't inc/dec SP */ |
| 92 | return NORMAL; |
| 93 | } |
| 94 | |
| 95 | PP(pp_gv) |
| 96 | { |
| 97 | dSP; |
| 98 | XPUSHs(MUTABLE_SV(cGVOP_gv)); |
| 99 | RETURN; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* also used for: pp_andassign() */ |
| 104 | |
| 105 | PP(pp_and) |
| 106 | { |
| 107 | PERL_ASYNC_CHECK(); |
| 108 | { |
| 109 | /* SP is not used to remove a variable that is saved across the |
| 110 | sv_2bool_flags call in SvTRUE_NN, if a RISC/CISC or low/high machine |
| 111 | register or load/store vs direct mem ops macro is introduced, this |
| 112 | should be a define block between direct PL_stack_sp and dSP operations, |
| 113 | presently, using PL_stack_sp is bias towards CISC cpus */ |
| 114 | SV * const sv = *PL_stack_sp; |
| 115 | if (!SvTRUE_NN(sv)) |
| 116 | return NORMAL; |
| 117 | else { |
| 118 | if (PL_op->op_type == OP_AND) |
| 119 | --PL_stack_sp; |
| 120 | return cLOGOP->op_other; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | PP(pp_sassign) |
| 126 | { |
| 127 | dSP; |
| 128 | /* sassign keeps its args in the optree traditionally backwards. |
| 129 | So we pop them differently. |
| 130 | */ |
| 131 | SV *left = POPs; SV *right = TOPs; |
| 132 | |
| 133 | if (PL_op->op_private & OPpASSIGN_BACKWARDS) { /* {or,and,dor}assign */ |
| 134 | SV * const temp = left; |
| 135 | left = right; right = temp; |
| 136 | } |
| 137 | assert(TAINTING_get || !TAINT_get); |
| 138 | if (UNLIKELY(TAINT_get) && !SvTAINTED(right)) |
| 139 | TAINT_NOT; |
| 140 | if (UNLIKELY(PL_op->op_private & OPpASSIGN_CV_TO_GV)) { |
| 141 | /* *foo =\&bar */ |
| 142 | SV * const cv = SvRV(right); |
| 143 | const U32 cv_type = SvTYPE(cv); |
| 144 | const bool is_gv = isGV_with_GP(left); |
| 145 | const bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM; |
| 146 | |
| 147 | if (!got_coderef) { |
| 148 | assert(SvROK(cv)); |
| 149 | } |
| 150 | |
| 151 | /* Can do the optimisation if left (LVALUE) is not a typeglob, |
| 152 | right (RVALUE) is a reference to something, and we're in void |
| 153 | context. */ |
| 154 | if (!got_coderef && !is_gv && GIMME_V == G_VOID) { |
| 155 | /* Is the target symbol table currently empty? */ |
| 156 | GV * const gv = gv_fetchsv_nomg(left, GV_NOINIT, SVt_PVGV); |
| 157 | if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) { |
| 158 | /* Good. Create a new proxy constant subroutine in the target. |
| 159 | The gv becomes a(nother) reference to the constant. */ |
| 160 | SV *const value = SvRV(cv); |
| 161 | |
| 162 | SvUPGRADE(MUTABLE_SV(gv), SVt_IV); |
| 163 | SvPCS_IMPORTED_on(gv); |
| 164 | SvRV_set(gv, value); |
| 165 | SvREFCNT_inc_simple_void(value); |
| 166 | SETs(left); |
| 167 | RETURN; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* Need to fix things up. */ |
| 172 | if (!is_gv) { |
| 173 | /* Need to fix GV. */ |
| 174 | left = MUTABLE_SV(gv_fetchsv_nomg(left,GV_ADD, SVt_PVGV)); |
| 175 | } |
| 176 | |
| 177 | if (!got_coderef) { |
| 178 | /* We've been returned a constant rather than a full subroutine, |
| 179 | but they expect a subroutine reference to apply. */ |
| 180 | if (SvROK(cv)) { |
| 181 | ENTER_with_name("sassign_coderef"); |
| 182 | SvREFCNT_inc_void(SvRV(cv)); |
| 183 | /* newCONSTSUB takes a reference count on the passed in SV |
| 184 | from us. We set the name to NULL, otherwise we get into |
| 185 | all sorts of fun as the reference to our new sub is |
| 186 | donated to the GV that we're about to assign to. |
| 187 | */ |
| 188 | SvRV_set(right, MUTABLE_SV(newCONSTSUB(GvSTASH(left), NULL, |
| 189 | SvRV(cv)))); |
| 190 | SvREFCNT_dec_NN(cv); |
| 191 | LEAVE_with_name("sassign_coderef"); |
| 192 | } else { |
| 193 | /* What can happen for the corner case *{"BONK"} = \&{"BONK"}; |
| 194 | is that |
| 195 | First: ops for \&{"BONK"}; return us the constant in the |
| 196 | symbol table |
| 197 | Second: ops for *{"BONK"} cause that symbol table entry |
| 198 | (and our reference to it) to be upgraded from RV |
| 199 | to typeblob) |
| 200 | Thirdly: We get here. cv is actually PVGV now, and its |
| 201 | GvCV() is actually the subroutine we're looking for |
| 202 | |
| 203 | So change the reference so that it points to the subroutine |
| 204 | of that typeglob, as that's what they were after all along. |
| 205 | */ |
| 206 | GV *const upgraded = MUTABLE_GV(cv); |
| 207 | CV *const source = GvCV(upgraded); |
| 208 | |
| 209 | assert(source); |
| 210 | assert(CvFLAGS(source) & CVf_CONST); |
| 211 | |
| 212 | SvREFCNT_inc_simple_void_NN(source); |
| 213 | SvREFCNT_dec_NN(upgraded); |
| 214 | SvRV_set(right, MUTABLE_SV(source)); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | if ( |
| 220 | UNLIKELY(SvTEMP(left)) && !SvSMAGICAL(left) && SvREFCNT(left) == 1 && |
| 221 | (!isGV_with_GP(left) || SvFAKE(left)) && ckWARN(WARN_MISC) |
| 222 | ) |
| 223 | Perl_warner(aTHX_ |
| 224 | packWARN(WARN_MISC), "Useless assignment to a temporary" |
| 225 | ); |
| 226 | SvSetMagicSV(left, right); |
| 227 | SETs(left); |
| 228 | RETURN; |
| 229 | } |
| 230 | |
| 231 | PP(pp_cond_expr) |
| 232 | { |
| 233 | dSP; |
| 234 | SV *sv; |
| 235 | |
| 236 | PERL_ASYNC_CHECK(); |
| 237 | sv = POPs; |
| 238 | RETURNOP(SvTRUE_NN(sv) ? cLOGOP->op_other : cLOGOP->op_next); |
| 239 | } |
| 240 | |
| 241 | PP(pp_unstack) |
| 242 | { |
| 243 | PERL_CONTEXT *cx; |
| 244 | PERL_ASYNC_CHECK(); |
| 245 | TAINT_NOT; /* Each statement is presumed innocent */ |
| 246 | cx = CX_CUR(); |
| 247 | PL_stack_sp = PL_stack_base + cx->blk_oldsp; |
| 248 | FREETMPS; |
| 249 | if (!(PL_op->op_flags & OPf_SPECIAL)) { |
| 250 | assert(CxTYPE(cx) == CXt_BLOCK || CxTYPE_is_LOOP(cx)); |
| 251 | CX_LEAVE_SCOPE(cx); |
| 252 | } |
| 253 | return NORMAL; |
| 254 | } |
| 255 | |
| 256 | PP(pp_concat) |
| 257 | { |
| 258 | dSP; dATARGET; tryAMAGICbin_MG(concat_amg, AMGf_assign); |
| 259 | { |
| 260 | dPOPTOPssrl; |
| 261 | bool lbyte; |
| 262 | STRLEN rlen; |
| 263 | const char *rpv = NULL; |
| 264 | bool rbyte = FALSE; |
| 265 | bool rcopied = FALSE; |
| 266 | |
| 267 | if (TARG == right && right != left) { /* $r = $l.$r */ |
| 268 | rpv = SvPV_nomg_const(right, rlen); |
| 269 | rbyte = !DO_UTF8(right); |
| 270 | right = newSVpvn_flags(rpv, rlen, SVs_TEMP); |
| 271 | rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */ |
| 272 | rcopied = TRUE; |
| 273 | } |
| 274 | |
| 275 | if (TARG != left) { /* not $l .= $r */ |
| 276 | STRLEN llen; |
| 277 | const char* const lpv = SvPV_nomg_const(left, llen); |
| 278 | lbyte = !DO_UTF8(left); |
| 279 | sv_setpvn(TARG, lpv, llen); |
| 280 | if (!lbyte) |
| 281 | SvUTF8_on(TARG); |
| 282 | else |
| 283 | SvUTF8_off(TARG); |
| 284 | } |
| 285 | else { /* $l .= $r and left == TARG */ |
| 286 | if (!SvOK(left)) { |
| 287 | if ((left == right /* $l .= $l */ |
| 288 | || (PL_op->op_private & OPpTARGET_MY)) /* $l = $l . $r */ |
| 289 | && ckWARN(WARN_UNINITIALIZED) |
| 290 | ) |
| 291 | report_uninit(left); |
| 292 | SvPVCLEAR(left); |
| 293 | } |
| 294 | else { |
| 295 | SvPV_force_nomg_nolen(left); |
| 296 | } |
| 297 | lbyte = !DO_UTF8(left); |
| 298 | if (IN_BYTES) |
| 299 | SvUTF8_off(left); |
| 300 | } |
| 301 | |
| 302 | if (!rcopied) { |
| 303 | rpv = SvPV_nomg_const(right, rlen); |
| 304 | rbyte = !DO_UTF8(right); |
| 305 | } |
| 306 | if (lbyte != rbyte) { |
| 307 | if (lbyte) |
| 308 | sv_utf8_upgrade_nomg(TARG); |
| 309 | else { |
| 310 | if (!rcopied) |
| 311 | right = newSVpvn_flags(rpv, rlen, SVs_TEMP); |
| 312 | sv_utf8_upgrade_nomg(right); |
| 313 | rpv = SvPV_nomg_const(right, rlen); |
| 314 | } |
| 315 | } |
| 316 | sv_catpvn_nomg(TARG, rpv, rlen); |
| 317 | |
| 318 | SETTARG; |
| 319 | RETURN; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* push the elements of av onto the stack */ |
| 324 | |
| 325 | STATIC void |
| 326 | S_pushav(pTHX_ AV* const av) |
| 327 | { |
| 328 | dSP; |
| 329 | const SSize_t maxarg = AvFILL(av) + 1; |
| 330 | EXTEND(SP, maxarg); |
| 331 | if (UNLIKELY(SvRMAGICAL(av))) { |
| 332 | PADOFFSET i; |
| 333 | for (i=0; i < (PADOFFSET)maxarg; i++) { |
| 334 | SV ** const svp = av_fetch(av, i, FALSE); |
| 335 | SP[i+1] = svp ? *svp : &PL_sv_undef; |
| 336 | } |
| 337 | } |
| 338 | else { |
| 339 | PADOFFSET i; |
| 340 | for (i=0; i < (PADOFFSET)maxarg; i++) { |
| 341 | SV * const sv = AvARRAY(av)[i]; |
| 342 | SP[i+1] = LIKELY(sv) ? sv : &PL_sv_undef; |
| 343 | } |
| 344 | } |
| 345 | SP += maxarg; |
| 346 | PUTBACK; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /* ($lex1,@lex2,...) or my ($lex1,@lex2,...) */ |
| 351 | |
| 352 | PP(pp_padrange) |
| 353 | { |
| 354 | dSP; |
| 355 | PADOFFSET base = PL_op->op_targ; |
| 356 | int count = (int)(PL_op->op_private) & OPpPADRANGE_COUNTMASK; |
| 357 | if (PL_op->op_flags & OPf_SPECIAL) { |
| 358 | /* fake the RHS of my ($x,$y,..) = @_ */ |
| 359 | PUSHMARK(SP); |
| 360 | S_pushav(aTHX_ GvAVn(PL_defgv)); |
| 361 | SPAGAIN; |
| 362 | } |
| 363 | |
| 364 | /* note, this is only skipped for compile-time-known void cxt */ |
| 365 | if ((PL_op->op_flags & OPf_WANT) != OPf_WANT_VOID) { |
| 366 | int i; |
| 367 | |
| 368 | EXTEND(SP, count); |
| 369 | PUSHMARK(SP); |
| 370 | for (i = 0; i <count; i++) |
| 371 | *++SP = PAD_SV(base+i); |
| 372 | } |
| 373 | if (PL_op->op_private & OPpLVAL_INTRO) { |
| 374 | SV **svp = &(PAD_SVl(base)); |
| 375 | const UV payload = (UV)( |
| 376 | (base << (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)) |
| 377 | | (count << SAVE_TIGHT_SHIFT) |
| 378 | | SAVEt_CLEARPADRANGE); |
| 379 | int i; |
| 380 | |
| 381 | STATIC_ASSERT_STMT(OPpPADRANGE_COUNTMASK + 1 == (1 << OPpPADRANGE_COUNTSHIFT)); |
| 382 | assert((payload >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)) |
| 383 | == (Size_t)base); |
| 384 | { |
| 385 | dSS_ADD; |
| 386 | SS_ADD_UV(payload); |
| 387 | SS_ADD_END(1); |
| 388 | } |
| 389 | |
| 390 | for (i = 0; i <count; i++) |
| 391 | SvPADSTALE_off(*svp++); /* mark lexical as active */ |
| 392 | } |
| 393 | RETURN; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | PP(pp_padsv) |
| 398 | { |
| 399 | dSP; |
| 400 | EXTEND(SP, 1); |
| 401 | { |
| 402 | OP * const op = PL_op; |
| 403 | /* access PL_curpad once */ |
| 404 | SV ** const padentry = &(PAD_SVl(op->op_targ)); |
| 405 | { |
| 406 | dTARG; |
| 407 | TARG = *padentry; |
| 408 | PUSHs(TARG); |
| 409 | PUTBACK; /* no pop/push after this, TOPs ok */ |
| 410 | } |
| 411 | if (op->op_flags & OPf_MOD) { |
| 412 | if (op->op_private & OPpLVAL_INTRO) |
| 413 | if (!(op->op_private & OPpPAD_STATE)) |
| 414 | save_clearsv(padentry); |
| 415 | if (op->op_private & OPpDEREF) { |
| 416 | /* TOPs is equivalent to TARG here. Using TOPs (SP) rather |
| 417 | than TARG reduces the scope of TARG, so it does not |
| 418 | span the call to save_clearsv, resulting in smaller |
| 419 | machine code. */ |
| 420 | TOPs = vivify_ref(TOPs, op->op_private & OPpDEREF); |
| 421 | } |
| 422 | } |
| 423 | return op->op_next; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | PP(pp_readline) |
| 428 | { |
| 429 | dSP; |
| 430 | /* pp_coreargs pushes a NULL to indicate no args passed to |
| 431 | * CORE::readline() */ |
| 432 | if (TOPs) { |
| 433 | SvGETMAGIC(TOPs); |
| 434 | tryAMAGICunTARGETlist(iter_amg, 0); |
| 435 | PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--); |
| 436 | } |
| 437 | else PL_last_in_gv = PL_argvgv, PL_stack_sp--; |
| 438 | if (!isGV_with_GP(PL_last_in_gv)) { |
| 439 | if (SvROK(PL_last_in_gv) && isGV_with_GP(SvRV(PL_last_in_gv))) |
| 440 | PL_last_in_gv = MUTABLE_GV(SvRV(PL_last_in_gv)); |
| 441 | else { |
| 442 | dSP; |
| 443 | XPUSHs(MUTABLE_SV(PL_last_in_gv)); |
| 444 | PUTBACK; |
| 445 | Perl_pp_rv2gv(aTHX); |
| 446 | PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--); |
| 447 | if (PL_last_in_gv == (GV *)&PL_sv_undef) |
| 448 | PL_last_in_gv = NULL; |
| 449 | else |
| 450 | assert(isGV_with_GP(PL_last_in_gv)); |
| 451 | } |
| 452 | } |
| 453 | return do_readline(); |
| 454 | } |
| 455 | |
| 456 | PP(pp_eq) |
| 457 | { |
| 458 | dSP; |
| 459 | SV *left, *right; |
| 460 | |
| 461 | tryAMAGICbin_MG(eq_amg, AMGf_set|AMGf_numeric); |
| 462 | right = POPs; |
| 463 | left = TOPs; |
| 464 | SETs(boolSV( |
| 465 | (SvIOK_notUV(left) && SvIOK_notUV(right)) |
| 466 | ? (SvIVX(left) == SvIVX(right)) |
| 467 | : ( do_ncmp(left, right) == 0) |
| 468 | )); |
| 469 | RETURN; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* also used for: pp_i_preinc() */ |
| 474 | |
| 475 | PP(pp_preinc) |
| 476 | { |
| 477 | SV *sv = *PL_stack_sp; |
| 478 | |
| 479 | if (LIKELY(((sv->sv_flags & |
| 480 | (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV| |
| 481 | SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK)) |
| 482 | == SVf_IOK)) |
| 483 | && SvIVX(sv) != IV_MAX) |
| 484 | { |
| 485 | SvIV_set(sv, SvIVX(sv) + 1); |
| 486 | } |
| 487 | else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_inc */ |
| 488 | sv_inc(sv); |
| 489 | SvSETMAGIC(sv); |
| 490 | return NORMAL; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | /* also used for: pp_i_predec() */ |
| 495 | |
| 496 | PP(pp_predec) |
| 497 | { |
| 498 | SV *sv = *PL_stack_sp; |
| 499 | |
| 500 | if (LIKELY(((sv->sv_flags & |
| 501 | (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV| |
| 502 | SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK)) |
| 503 | == SVf_IOK)) |
| 504 | && SvIVX(sv) != IV_MIN) |
| 505 | { |
| 506 | SvIV_set(sv, SvIVX(sv) - 1); |
| 507 | } |
| 508 | else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_dec */ |
| 509 | sv_dec(sv); |
| 510 | SvSETMAGIC(sv); |
| 511 | return NORMAL; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | /* also used for: pp_orassign() */ |
| 516 | |
| 517 | PP(pp_or) |
| 518 | { |
| 519 | dSP; |
| 520 | SV *sv; |
| 521 | PERL_ASYNC_CHECK(); |
| 522 | sv = TOPs; |
| 523 | if (SvTRUE_NN(sv)) |
| 524 | RETURN; |
| 525 | else { |
| 526 | if (PL_op->op_type == OP_OR) |
| 527 | --SP; |
| 528 | RETURNOP(cLOGOP->op_other); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | |
| 533 | /* also used for: pp_dor() pp_dorassign() */ |
| 534 | |
| 535 | PP(pp_defined) |
| 536 | { |
| 537 | dSP; |
| 538 | SV* sv; |
| 539 | bool defined; |
| 540 | const int op_type = PL_op->op_type; |
| 541 | const bool is_dor = (op_type == OP_DOR || op_type == OP_DORASSIGN); |
| 542 | |
| 543 | if (is_dor) { |
| 544 | PERL_ASYNC_CHECK(); |
| 545 | sv = TOPs; |
| 546 | if (UNLIKELY(!sv || !SvANY(sv))) { |
| 547 | if (op_type == OP_DOR) |
| 548 | --SP; |
| 549 | RETURNOP(cLOGOP->op_other); |
| 550 | } |
| 551 | } |
| 552 | else { |
| 553 | /* OP_DEFINED */ |
| 554 | sv = POPs; |
| 555 | if (UNLIKELY(!sv || !SvANY(sv))) |
| 556 | RETPUSHNO; |
| 557 | } |
| 558 | |
| 559 | defined = FALSE; |
| 560 | switch (SvTYPE(sv)) { |
| 561 | case SVt_PVAV: |
| 562 | if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) |
| 563 | defined = TRUE; |
| 564 | break; |
| 565 | case SVt_PVHV: |
| 566 | if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) |
| 567 | defined = TRUE; |
| 568 | break; |
| 569 | case SVt_PVCV: |
| 570 | if (CvROOT(sv) || CvXSUB(sv)) |
| 571 | defined = TRUE; |
| 572 | break; |
| 573 | default: |
| 574 | SvGETMAGIC(sv); |
| 575 | if (SvOK(sv)) |
| 576 | defined = TRUE; |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | if (is_dor) { |
| 581 | if(defined) |
| 582 | RETURN; |
| 583 | if(op_type == OP_DOR) |
| 584 | --SP; |
| 585 | RETURNOP(cLOGOP->op_other); |
| 586 | } |
| 587 | /* assuming OP_DEFINED */ |
| 588 | if(defined) |
| 589 | RETPUSHYES; |
| 590 | RETPUSHNO; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | |
| 595 | PP(pp_add) |
| 596 | { |
| 597 | dSP; dATARGET; bool useleft; SV *svl, *svr; |
| 598 | |
| 599 | tryAMAGICbin_MG(add_amg, AMGf_assign|AMGf_numeric); |
| 600 | svr = TOPs; |
| 601 | svl = TOPm1s; |
| 602 | |
| 603 | #ifdef PERL_PRESERVE_IVUV |
| 604 | |
| 605 | /* special-case some simple common cases */ |
| 606 | if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) { |
| 607 | IV il, ir; |
| 608 | U32 flags = (svl->sv_flags & svr->sv_flags); |
| 609 | if (flags & SVf_IOK) { |
| 610 | /* both args are simple IVs */ |
| 611 | UV topl, topr; |
| 612 | il = SvIVX(svl); |
| 613 | ir = SvIVX(svr); |
| 614 | do_iv: |
| 615 | topl = ((UV)il) >> (UVSIZE * 8 - 2); |
| 616 | topr = ((UV)ir) >> (UVSIZE * 8 - 2); |
| 617 | |
| 618 | /* if both are in a range that can't under/overflow, do a |
| 619 | * simple integer add: if the top of both numbers |
| 620 | * are 00 or 11, then it's safe */ |
| 621 | if (!( ((topl+1) | (topr+1)) & 2)) { |
| 622 | SP--; |
| 623 | TARGi(il + ir, 0); /* args not GMG, so can't be tainted */ |
| 624 | SETs(TARG); |
| 625 | RETURN; |
| 626 | } |
| 627 | goto generic; |
| 628 | } |
| 629 | else if (flags & SVf_NOK) { |
| 630 | /* both args are NVs */ |
| 631 | NV nl = SvNVX(svl); |
| 632 | NV nr = SvNVX(svr); |
| 633 | |
| 634 | if ( |
| 635 | #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan) |
| 636 | !Perl_isnan(nl) && nl == (NV)(il = (IV)nl) |
| 637 | && !Perl_isnan(nr) && nr == (NV)(ir = (IV)nr) |
| 638 | #else |
| 639 | nl == (NV)(il = (IV)nl) && nr == (NV)(ir = (IV)nr) |
| 640 | #endif |
| 641 | ) |
| 642 | /* nothing was lost by converting to IVs */ |
| 643 | goto do_iv; |
| 644 | SP--; |
| 645 | TARGn(nl + nr, 0); /* args not GMG, so can't be tainted */ |
| 646 | SETs(TARG); |
| 647 | RETURN; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | generic: |
| 652 | |
| 653 | useleft = USE_LEFT(svl); |
| 654 | /* We must see if we can perform the addition with integers if possible, |
| 655 | as the integer code detects overflow while the NV code doesn't. |
| 656 | If either argument hasn't had a numeric conversion yet attempt to get |
| 657 | the IV. It's important to do this now, rather than just assuming that |
| 658 | it's not IOK as a PV of "9223372036854775806" may not take well to NV |
| 659 | addition, and an SV which is NOK, NV=6.0 ought to be coerced to |
| 660 | integer in case the second argument is IV=9223372036854775806 |
| 661 | We can (now) rely on sv_2iv to do the right thing, only setting the |
| 662 | public IOK flag if the value in the NV (or PV) slot is truly integer. |
| 663 | |
| 664 | A side effect is that this also aggressively prefers integer maths over |
| 665 | fp maths for integer values. |
| 666 | |
| 667 | How to detect overflow? |
| 668 | |
| 669 | C 99 section 6.2.6.1 says |
| 670 | |
| 671 | The range of nonnegative values of a signed integer type is a subrange |
| 672 | of the corresponding unsigned integer type, and the representation of |
| 673 | the same value in each type is the same. A computation involving |
| 674 | unsigned operands can never overflow, because a result that cannot be |
| 675 | represented by the resulting unsigned integer type is reduced modulo |
| 676 | the number that is one greater than the largest value that can be |
| 677 | represented by the resulting type. |
| 678 | |
| 679 | (the 9th paragraph) |
| 680 | |
| 681 | which I read as "unsigned ints wrap." |
| 682 | |
| 683 | signed integer overflow seems to be classed as "exception condition" |
| 684 | |
| 685 | If an exceptional condition occurs during the evaluation of an |
| 686 | expression (that is, if the result is not mathematically defined or not |
| 687 | in the range of representable values for its type), the behavior is |
| 688 | undefined. |
| 689 | |
| 690 | (6.5, the 5th paragraph) |
| 691 | |
| 692 | I had assumed that on 2s complement machines signed arithmetic would |
| 693 | wrap, hence coded pp_add and pp_subtract on the assumption that |
| 694 | everything perl builds on would be happy. After much wailing and |
| 695 | gnashing of teeth it would seem that irix64 knows its ANSI spec well, |
| 696 | knows that it doesn't need to, and doesn't. Bah. Anyway, the all- |
| 697 | unsigned code below is actually shorter than the old code. :-) |
| 698 | */ |
| 699 | |
| 700 | if (SvIV_please_nomg(svr)) { |
| 701 | /* Unless the left argument is integer in range we are going to have to |
| 702 | use NV maths. Hence only attempt to coerce the right argument if |
| 703 | we know the left is integer. */ |
| 704 | UV auv = 0; |
| 705 | bool auvok = FALSE; |
| 706 | bool a_valid = 0; |
| 707 | |
| 708 | if (!useleft) { |
| 709 | auv = 0; |
| 710 | a_valid = auvok = 1; |
| 711 | /* left operand is undef, treat as zero. + 0 is identity, |
| 712 | Could SETi or SETu right now, but space optimise by not adding |
| 713 | lots of code to speed up what is probably a rarish case. */ |
| 714 | } else { |
| 715 | /* Left operand is defined, so is it IV? */ |
| 716 | if (SvIV_please_nomg(svl)) { |
| 717 | if ((auvok = SvUOK(svl))) |
| 718 | auv = SvUVX(svl); |
| 719 | else { |
| 720 | const IV aiv = SvIVX(svl); |
| 721 | if (aiv >= 0) { |
| 722 | auv = aiv; |
| 723 | auvok = 1; /* Now acting as a sign flag. */ |
| 724 | } else { |
| 725 | auv = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv); |
| 726 | } |
| 727 | } |
| 728 | a_valid = 1; |
| 729 | } |
| 730 | } |
| 731 | if (a_valid) { |
| 732 | bool result_good = 0; |
| 733 | UV result; |
| 734 | UV buv; |
| 735 | bool buvok = SvUOK(svr); |
| 736 | |
| 737 | if (buvok) |
| 738 | buv = SvUVX(svr); |
| 739 | else { |
| 740 | const IV biv = SvIVX(svr); |
| 741 | if (biv >= 0) { |
| 742 | buv = biv; |
| 743 | buvok = 1; |
| 744 | } else |
| 745 | buv = (biv == IV_MIN) ? (UV)biv : (UV)(-biv); |
| 746 | } |
| 747 | /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve, |
| 748 | else "IV" now, independent of how it came in. |
| 749 | if a, b represents positive, A, B negative, a maps to -A etc |
| 750 | a + b => (a + b) |
| 751 | A + b => -(a - b) |
| 752 | a + B => (a - b) |
| 753 | A + B => -(a + b) |
| 754 | all UV maths. negate result if A negative. |
| 755 | add if signs same, subtract if signs differ. */ |
| 756 | |
| 757 | if (auvok ^ buvok) { |
| 758 | /* Signs differ. */ |
| 759 | if (auv >= buv) { |
| 760 | result = auv - buv; |
| 761 | /* Must get smaller */ |
| 762 | if (result <= auv) |
| 763 | result_good = 1; |
| 764 | } else { |
| 765 | result = buv - auv; |
| 766 | if (result <= buv) { |
| 767 | /* result really should be -(auv-buv). as its negation |
| 768 | of true value, need to swap our result flag */ |
| 769 | auvok = !auvok; |
| 770 | result_good = 1; |
| 771 | } |
| 772 | } |
| 773 | } else { |
| 774 | /* Signs same */ |
| 775 | result = auv + buv; |
| 776 | if (result >= auv) |
| 777 | result_good = 1; |
| 778 | } |
| 779 | if (result_good) { |
| 780 | SP--; |
| 781 | if (auvok) |
| 782 | SETu( result ); |
| 783 | else { |
| 784 | /* Negate result */ |
| 785 | if (result <= (UV)IV_MIN) |
| 786 | SETi(result == (UV)IV_MIN |
| 787 | ? IV_MIN : -(IV)result); |
| 788 | else { |
| 789 | /* result valid, but out of range for IV. */ |
| 790 | SETn( -(NV)result ); |
| 791 | } |
| 792 | } |
| 793 | RETURN; |
| 794 | } /* Overflow, drop through to NVs. */ |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | #else |
| 799 | useleft = USE_LEFT(svl); |
| 800 | #endif |
| 801 | |
| 802 | { |
| 803 | NV value = SvNV_nomg(svr); |
| 804 | (void)POPs; |
| 805 | if (!useleft) { |
| 806 | /* left operand is undef, treat as zero. + 0.0 is identity. */ |
| 807 | SETn(value); |
| 808 | RETURN; |
| 809 | } |
| 810 | SETn( value + SvNV_nomg(svl) ); |
| 811 | RETURN; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | |
| 816 | /* also used for: pp_aelemfast_lex() */ |
| 817 | |
| 818 | PP(pp_aelemfast) |
| 819 | { |
| 820 | dSP; |
| 821 | AV * const av = PL_op->op_type == OP_AELEMFAST_LEX |
| 822 | ? MUTABLE_AV(PAD_SV(PL_op->op_targ)) : GvAVn(cGVOP_gv); |
| 823 | const U32 lval = PL_op->op_flags & OPf_MOD; |
| 824 | const I8 key = (I8)PL_op->op_private; |
| 825 | SV** svp; |
| 826 | SV *sv; |
| 827 | |
| 828 | assert(SvTYPE(av) == SVt_PVAV); |
| 829 | |
| 830 | EXTEND(SP, 1); |
| 831 | |
| 832 | /* inlined av_fetch() for simple cases ... */ |
| 833 | if (!SvRMAGICAL(av) && key >= 0 && key <= AvFILLp(av)) { |
| 834 | sv = AvARRAY(av)[key]; |
| 835 | if (sv) { |
| 836 | PUSHs(sv); |
| 837 | RETURN; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /* ... else do it the hard way */ |
| 842 | svp = av_fetch(av, key, lval); |
| 843 | sv = (svp ? *svp : &PL_sv_undef); |
| 844 | |
| 845 | if (UNLIKELY(!svp && lval)) |
| 846 | DIE(aTHX_ PL_no_aelem, (int)key); |
| 847 | |
| 848 | if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */ |
| 849 | mg_get(sv); |
| 850 | PUSHs(sv); |
| 851 | RETURN; |
| 852 | } |
| 853 | |
| 854 | PP(pp_join) |
| 855 | { |
| 856 | dSP; dMARK; dTARGET; |
| 857 | MARK++; |
| 858 | do_join(TARG, *MARK, MARK, SP); |
| 859 | SP = MARK; |
| 860 | SETs(TARG); |
| 861 | RETURN; |
| 862 | } |
| 863 | |
| 864 | /* Oversized hot code. */ |
| 865 | |
| 866 | /* also used for: pp_say() */ |
| 867 | |
| 868 | PP(pp_print) |
| 869 | { |
| 870 | dSP; dMARK; dORIGMARK; |
| 871 | PerlIO *fp; |
| 872 | MAGIC *mg; |
| 873 | GV * const gv |
| 874 | = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv; |
| 875 | IO *io = GvIO(gv); |
| 876 | |
| 877 | if (io |
| 878 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
| 879 | { |
| 880 | had_magic: |
| 881 | if (MARK == ORIGMARK) { |
| 882 | /* If using default handle then we need to make space to |
| 883 | * pass object as 1st arg, so move other args up ... |
| 884 | */ |
| 885 | MEXTEND(SP, 1); |
| 886 | ++MARK; |
| 887 | Move(MARK, MARK + 1, (SP - MARK) + 1, SV*); |
| 888 | ++SP; |
| 889 | } |
| 890 | return Perl_tied_method(aTHX_ SV_CONST(PRINT), mark - 1, MUTABLE_SV(io), |
| 891 | mg, |
| 892 | (G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK |
| 893 | | (PL_op->op_type == OP_SAY |
| 894 | ? TIED_METHOD_SAY : 0)), sp - mark); |
| 895 | } |
| 896 | if (!io) { |
| 897 | if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv))) |
| 898 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
| 899 | goto had_magic; |
| 900 | report_evil_fh(gv); |
| 901 | SETERRNO(EBADF,RMS_IFI); |
| 902 | goto just_say_no; |
| 903 | } |
| 904 | else if (!(fp = IoOFP(io))) { |
| 905 | if (IoIFP(io)) |
| 906 | report_wrongway_fh(gv, '<'); |
| 907 | else |
| 908 | report_evil_fh(gv); |
| 909 | SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI); |
| 910 | goto just_say_no; |
| 911 | } |
| 912 | else { |
| 913 | SV * const ofs = GvSV(PL_ofsgv); /* $, */ |
| 914 | MARK++; |
| 915 | if (ofs && (SvGMAGICAL(ofs) || SvOK(ofs))) { |
| 916 | while (MARK <= SP) { |
| 917 | if (!do_print(*MARK, fp)) |
| 918 | break; |
| 919 | MARK++; |
| 920 | if (MARK <= SP) { |
| 921 | /* don't use 'ofs' here - it may be invalidated by magic callbacks */ |
| 922 | if (!do_print(GvSV(PL_ofsgv), fp)) { |
| 923 | MARK--; |
| 924 | break; |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | else { |
| 930 | while (MARK <= SP) { |
| 931 | if (!do_print(*MARK, fp)) |
| 932 | break; |
| 933 | MARK++; |
| 934 | } |
| 935 | } |
| 936 | if (MARK <= SP) |
| 937 | goto just_say_no; |
| 938 | else { |
| 939 | if (PL_op->op_type == OP_SAY) { |
| 940 | if (PerlIO_write(fp, "\n", 1) == 0 || PerlIO_error(fp)) |
| 941 | goto just_say_no; |
| 942 | } |
| 943 | else if (PL_ors_sv && SvOK(PL_ors_sv)) |
| 944 | if (!do_print(PL_ors_sv, fp)) /* $\ */ |
| 945 | goto just_say_no; |
| 946 | |
| 947 | if (IoFLAGS(io) & IOf_FLUSH) |
| 948 | if (PerlIO_flush(fp) == EOF) |
| 949 | goto just_say_no; |
| 950 | } |
| 951 | } |
| 952 | SP = ORIGMARK; |
| 953 | XPUSHs(&PL_sv_yes); |
| 954 | RETURN; |
| 955 | |
| 956 | just_say_no: |
| 957 | SP = ORIGMARK; |
| 958 | XPUSHs(&PL_sv_undef); |
| 959 | RETURN; |
| 960 | } |
| 961 | |
| 962 | |
| 963 | /* do the common parts of pp_padhv() and pp_rv2hv() |
| 964 | * It assumes the caller has done EXTEND(SP, 1) or equivalent. |
| 965 | * 'is_keys' indicates the OPpPADHV_ISKEYS/OPpRV2HV_ISKEYS flag is set. |
| 966 | * 'has_targ' indicates that the op has a target - this should |
| 967 | * be a compile-time constant so that the code can constant-folded as |
| 968 | * appropriate |
| 969 | * */ |
| 970 | |
| 971 | PERL_STATIC_INLINE OP* |
| 972 | S_padhv_rv2hv_common(pTHX_ HV *hv, U8 gimme, bool is_keys, bool has_targ) |
| 973 | { |
| 974 | bool tied; |
| 975 | dSP; |
| 976 | |
| 977 | assert(PL_op->op_type == OP_PADHV || PL_op->op_type == OP_RV2HV); |
| 978 | |
| 979 | if (gimme == G_ARRAY) { |
| 980 | hv_pushkv(hv); |
| 981 | return NORMAL; |
| 982 | } |
| 983 | |
| 984 | if (is_keys) |
| 985 | /* 'keys %h' masquerading as '%h': reset iterator */ |
| 986 | (void)hv_iterinit(hv); |
| 987 | |
| 988 | tied = SvRMAGICAL(hv) && mg_find(MUTABLE_SV(hv), PERL_MAGIC_tied); |
| 989 | |
| 990 | if ( ( PL_op->op_private & OPpTRUEBOOL |
| 991 | || ( PL_op->op_private & OPpMAYBE_TRUEBOOL |
| 992 | && block_gimme() == G_VOID) |
| 993 | ) |
| 994 | && !tied |
| 995 | ) |
| 996 | PUSHs(HvUSEDKEYS(hv) ? &PL_sv_yes : &PL_sv_zero); |
| 997 | else if (gimme == G_SCALAR) { |
| 998 | if (is_keys) { |
| 999 | IV i; |
| 1000 | if (tied) { |
| 1001 | i = 0; |
| 1002 | while (hv_iternext(hv)) |
| 1003 | i++; |
| 1004 | } |
| 1005 | else |
| 1006 | i = HvUSEDKEYS(hv); |
| 1007 | if (has_targ) { |
| 1008 | dTARGET; |
| 1009 | PUSHi(i); |
| 1010 | } |
| 1011 | else |
| 1012 | mPUSHi(i); |
| 1013 | } |
| 1014 | else |
| 1015 | PUSHs(Perl_hv_scalar(aTHX_ hv)); |
| 1016 | } |
| 1017 | |
| 1018 | PUTBACK; |
| 1019 | return NORMAL; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | /* This is also called directly by pp_lvavref. */ |
| 1024 | PP(pp_padav) |
| 1025 | { |
| 1026 | dSP; dTARGET; |
| 1027 | U8 gimme; |
| 1028 | assert(SvTYPE(TARG) == SVt_PVAV); |
| 1029 | if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO )) |
| 1030 | if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) )) |
| 1031 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); |
| 1032 | EXTEND(SP, 1); |
| 1033 | |
| 1034 | if (PL_op->op_flags & OPf_REF) { |
| 1035 | PUSHs(TARG); |
| 1036 | RETURN; |
| 1037 | } |
| 1038 | else if (PL_op->op_private & OPpMAYBE_LVSUB) { |
| 1039 | const I32 flags = is_lvalue_sub(); |
| 1040 | if (flags && !(flags & OPpENTERSUB_INARGS)) { |
| 1041 | if (GIMME_V == G_SCALAR) |
| 1042 | /* diag_listed_as: Can't return %s to lvalue scalar context */ |
| 1043 | Perl_croak(aTHX_ "Can't return array to lvalue scalar context"); |
| 1044 | PUSHs(TARG); |
| 1045 | RETURN; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | gimme = GIMME_V; |
| 1050 | if (gimme == G_ARRAY) { |
| 1051 | S_pushav(aTHX_ (AV*)TARG); |
| 1052 | return NORMAL; |
| 1053 | } |
| 1054 | |
| 1055 | if (gimme == G_SCALAR) { |
| 1056 | const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1; |
| 1057 | if (!maxarg) |
| 1058 | PUSHs(&PL_sv_zero); |
| 1059 | else if (PL_op->op_private & OPpTRUEBOOL) |
| 1060 | PUSHs(&PL_sv_yes); |
| 1061 | else |
| 1062 | mPUSHi(maxarg); |
| 1063 | } |
| 1064 | RETURN; |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | PP(pp_padhv) |
| 1069 | { |
| 1070 | dSP; dTARGET; |
| 1071 | U8 gimme; |
| 1072 | |
| 1073 | assert(SvTYPE(TARG) == SVt_PVHV); |
| 1074 | if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO )) |
| 1075 | if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) )) |
| 1076 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); |
| 1077 | |
| 1078 | EXTEND(SP, 1); |
| 1079 | |
| 1080 | if (PL_op->op_flags & OPf_REF) { |
| 1081 | PUSHs(TARG); |
| 1082 | RETURN; |
| 1083 | } |
| 1084 | else if (PL_op->op_private & OPpMAYBE_LVSUB) { |
| 1085 | const I32 flags = is_lvalue_sub(); |
| 1086 | if (flags && !(flags & OPpENTERSUB_INARGS)) { |
| 1087 | if (GIMME_V == G_SCALAR) |
| 1088 | /* diag_listed_as: Can't return %s to lvalue scalar context */ |
| 1089 | Perl_croak(aTHX_ "Can't return hash to lvalue scalar context"); |
| 1090 | PUSHs(TARG); |
| 1091 | RETURN; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | gimme = GIMME_V; |
| 1096 | |
| 1097 | return S_padhv_rv2hv_common(aTHX_ (HV*)TARG, gimme, |
| 1098 | cBOOL(PL_op->op_private & OPpPADHV_ISKEYS), |
| 1099 | 0 /* has_targ*/); |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | /* also used for: pp_rv2hv() */ |
| 1104 | /* also called directly by pp_lvavref */ |
| 1105 | |
| 1106 | PP(pp_rv2av) |
| 1107 | { |
| 1108 | dSP; dTOPss; |
| 1109 | const U8 gimme = GIMME_V; |
| 1110 | static const char an_array[] = "an ARRAY"; |
| 1111 | static const char a_hash[] = "a HASH"; |
| 1112 | const bool is_pp_rv2av = PL_op->op_type == OP_RV2AV |
| 1113 | || PL_op->op_type == OP_LVAVREF; |
| 1114 | const svtype type = is_pp_rv2av ? SVt_PVAV : SVt_PVHV; |
| 1115 | |
| 1116 | SvGETMAGIC(sv); |
| 1117 | if (SvROK(sv)) { |
| 1118 | if (UNLIKELY(SvAMAGIC(sv))) { |
| 1119 | sv = amagic_deref_call(sv, is_pp_rv2av ? to_av_amg : to_hv_amg); |
| 1120 | } |
| 1121 | sv = SvRV(sv); |
| 1122 | if (UNLIKELY(SvTYPE(sv) != type)) |
| 1123 | /* diag_listed_as: Not an ARRAY reference */ |
| 1124 | DIE(aTHX_ "Not %s reference", is_pp_rv2av ? an_array : a_hash); |
| 1125 | else if (UNLIKELY(PL_op->op_flags & OPf_MOD |
| 1126 | && PL_op->op_private & OPpLVAL_INTRO)) |
| 1127 | Perl_croak(aTHX_ "%s", PL_no_localize_ref); |
| 1128 | } |
| 1129 | else if (UNLIKELY(SvTYPE(sv) != type)) { |
| 1130 | GV *gv; |
| 1131 | |
| 1132 | if (!isGV_with_GP(sv)) { |
| 1133 | gv = Perl_softref2xv(aTHX_ sv, is_pp_rv2av ? an_array : a_hash, |
| 1134 | type, &sp); |
| 1135 | if (!gv) |
| 1136 | RETURN; |
| 1137 | } |
| 1138 | else { |
| 1139 | gv = MUTABLE_GV(sv); |
| 1140 | } |
| 1141 | sv = is_pp_rv2av ? MUTABLE_SV(GvAVn(gv)) : MUTABLE_SV(GvHVn(gv)); |
| 1142 | if (PL_op->op_private & OPpLVAL_INTRO) |
| 1143 | sv = is_pp_rv2av ? MUTABLE_SV(save_ary(gv)) : MUTABLE_SV(save_hash(gv)); |
| 1144 | } |
| 1145 | if (PL_op->op_flags & OPf_REF) { |
| 1146 | SETs(sv); |
| 1147 | RETURN; |
| 1148 | } |
| 1149 | else if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) { |
| 1150 | const I32 flags = is_lvalue_sub(); |
| 1151 | if (flags && !(flags & OPpENTERSUB_INARGS)) { |
| 1152 | if (gimme != G_ARRAY) |
| 1153 | goto croak_cant_return; |
| 1154 | SETs(sv); |
| 1155 | RETURN; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | if (is_pp_rv2av) { |
| 1160 | AV *const av = MUTABLE_AV(sv); |
| 1161 | /* The guts of pp_rv2av */ |
| 1162 | if (gimme == G_ARRAY) { |
| 1163 | SP--; |
| 1164 | PUTBACK; |
| 1165 | S_pushav(aTHX_ av); |
| 1166 | SPAGAIN; |
| 1167 | } |
| 1168 | else if (gimme == G_SCALAR) { |
| 1169 | const SSize_t maxarg = AvFILL(av) + 1; |
| 1170 | if (PL_op->op_private & OPpTRUEBOOL) |
| 1171 | SETs(maxarg ? &PL_sv_yes : &PL_sv_zero); |
| 1172 | else { |
| 1173 | dTARGET; |
| 1174 | SETi(maxarg); |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | else { |
| 1179 | SP--; PUTBACK; |
| 1180 | return S_padhv_rv2hv_common(aTHX_ (HV*)sv, gimme, |
| 1181 | cBOOL(PL_op->op_private & OPpRV2HV_ISKEYS), |
| 1182 | 1 /* has_targ*/); |
| 1183 | } |
| 1184 | RETURN; |
| 1185 | |
| 1186 | croak_cant_return: |
| 1187 | Perl_croak(aTHX_ "Can't return %s to lvalue scalar context", |
| 1188 | is_pp_rv2av ? "array" : "hash"); |
| 1189 | RETURN; |
| 1190 | } |
| 1191 | |
| 1192 | STATIC void |
| 1193 | S_do_oddball(pTHX_ SV **oddkey, SV **firstkey) |
| 1194 | { |
| 1195 | PERL_ARGS_ASSERT_DO_ODDBALL; |
| 1196 | |
| 1197 | if (*oddkey) { |
| 1198 | if (ckWARN(WARN_MISC)) { |
| 1199 | const char *err; |
| 1200 | if (oddkey == firstkey && |
| 1201 | SvROK(*oddkey) && |
| 1202 | (SvTYPE(SvRV(*oddkey)) == SVt_PVAV || |
| 1203 | SvTYPE(SvRV(*oddkey)) == SVt_PVHV)) |
| 1204 | { |
| 1205 | err = "Reference found where even-sized list expected"; |
| 1206 | } |
| 1207 | else |
| 1208 | err = "Odd number of elements in hash assignment"; |
| 1209 | Perl_warner(aTHX_ packWARN(WARN_MISC), "%s", err); |
| 1210 | } |
| 1211 | |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | /* Do a mark and sweep with the SVf_BREAK flag to detect elements which |
| 1217 | * are common to both the LHS and RHS of an aassign, and replace them |
| 1218 | * with copies. All these copies are made before the actual list assign is |
| 1219 | * done. |
| 1220 | * |
| 1221 | * For example in ($a,$b) = ($b,$a), assigning the value of the first RHS |
| 1222 | * element ($b) to the first LH element ($a), modifies $a; when the |
| 1223 | * second assignment is done, the second RH element now has the wrong |
| 1224 | * value. So we initially replace the RHS with ($b, mortalcopy($a)). |
| 1225 | * Note that we don't need to make a mortal copy of $b. |
| 1226 | * |
| 1227 | * The algorithm below works by, for every RHS element, mark the |
| 1228 | * corresponding LHS target element with SVf_BREAK. Then if the RHS |
| 1229 | * element is found with SVf_BREAK set, it means it would have been |
| 1230 | * modified, so make a copy. |
| 1231 | * Note that by scanning both LHS and RHS in lockstep, we avoid |
| 1232 | * unnecessary copies (like $b above) compared with a naive |
| 1233 | * "mark all LHS; copy all marked RHS; unmark all LHS". |
| 1234 | * |
| 1235 | * If the LHS element is a 'my' declaration' and has a refcount of 1, then |
| 1236 | * it can't be common and can be skipped. |
| 1237 | * |
| 1238 | * On DEBUGGING builds it takes an extra boolean, fake. If true, it means |
| 1239 | * that we thought we didn't need to call S_aassign_copy_common(), but we |
| 1240 | * have anyway for sanity checking. If we find we need to copy, then panic. |
| 1241 | */ |
| 1242 | |
| 1243 | PERL_STATIC_INLINE void |
| 1244 | S_aassign_copy_common(pTHX_ SV **firstlelem, SV **lastlelem, |
| 1245 | SV **firstrelem, SV **lastrelem |
| 1246 | #ifdef DEBUGGING |
| 1247 | , bool fake |
| 1248 | #endif |
| 1249 | ) |
| 1250 | { |
| 1251 | dVAR; |
| 1252 | SV **relem; |
| 1253 | SV **lelem; |
| 1254 | SSize_t lcount = lastlelem - firstlelem + 1; |
| 1255 | bool marked = FALSE; /* have we marked any LHS with SVf_BREAK ? */ |
| 1256 | bool const do_rc1 = cBOOL(PL_op->op_private & OPpASSIGN_COMMON_RC1); |
| 1257 | bool copy_all = FALSE; |
| 1258 | |
| 1259 | assert(!PL_in_clean_all); /* SVf_BREAK not already in use */ |
| 1260 | assert(firstlelem < lastlelem); /* at least 2 LH elements */ |
| 1261 | assert(firstrelem < lastrelem); /* at least 2 RH elements */ |
| 1262 | |
| 1263 | |
| 1264 | lelem = firstlelem; |
| 1265 | /* we never have to copy the first RH element; it can't be corrupted |
| 1266 | * by assigning something to the corresponding first LH element. |
| 1267 | * So this scan does in a loop: mark LHS[N]; test RHS[N+1] |
| 1268 | */ |
| 1269 | relem = firstrelem + 1; |
| 1270 | |
| 1271 | for (; relem <= lastrelem; relem++) { |
| 1272 | SV *svr; |
| 1273 | |
| 1274 | /* mark next LH element */ |
| 1275 | |
| 1276 | if (--lcount >= 0) { |
| 1277 | SV *svl = *lelem++; |
| 1278 | |
| 1279 | if (UNLIKELY(!svl)) {/* skip AV alias marker */ |
| 1280 | assert (lelem <= lastlelem); |
| 1281 | svl = *lelem++; |
| 1282 | lcount--; |
| 1283 | } |
| 1284 | |
| 1285 | assert(svl); |
| 1286 | if (SvSMAGICAL(svl)) { |
| 1287 | copy_all = TRUE; |
| 1288 | } |
| 1289 | if (SvTYPE(svl) == SVt_PVAV || SvTYPE(svl) == SVt_PVHV) { |
| 1290 | if (!marked) |
| 1291 | return; |
| 1292 | /* this LH element will consume all further args; |
| 1293 | * no need to mark any further LH elements (if any). |
| 1294 | * But we still need to scan any remaining RHS elements; |
| 1295 | * set lcount negative to distinguish from lcount == 0, |
| 1296 | * so the loop condition continues being true |
| 1297 | */ |
| 1298 | lcount = -1; |
| 1299 | lelem--; /* no need to unmark this element */ |
| 1300 | } |
| 1301 | else if (!(do_rc1 && SvREFCNT(svl) == 1) && !SvIMMORTAL(svl)) { |
| 1302 | SvFLAGS(svl) |= SVf_BREAK; |
| 1303 | marked = TRUE; |
| 1304 | } |
| 1305 | else if (!marked) { |
| 1306 | /* don't check RH element if no SVf_BREAK flags set yet */ |
| 1307 | if (!lcount) |
| 1308 | break; |
| 1309 | continue; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | /* see if corresponding RH element needs copying */ |
| 1314 | |
| 1315 | assert(marked); |
| 1316 | svr = *relem; |
| 1317 | assert(svr); |
| 1318 | |
| 1319 | if (UNLIKELY(SvFLAGS(svr) & (SVf_BREAK|SVs_GMG) || copy_all)) { |
| 1320 | U32 brk = (SvFLAGS(svr) & SVf_BREAK); |
| 1321 | |
| 1322 | #ifdef DEBUGGING |
| 1323 | if (fake) { |
| 1324 | /* op_dump(PL_op); */ |
| 1325 | Perl_croak(aTHX_ |
| 1326 | "panic: aassign skipped needed copy of common RH elem %" |
| 1327 | UVuf, (UV)(relem - firstrelem)); |
| 1328 | } |
| 1329 | #endif |
| 1330 | |
| 1331 | TAINT_NOT; /* Each item is independent */ |
| 1332 | |
| 1333 | /* Dear TODO test in t/op/sort.t, I love you. |
| 1334 | (It's relying on a panic, not a "semi-panic" from newSVsv() |
| 1335 | and then an assertion failure below.) */ |
| 1336 | if (UNLIKELY(SvIS_FREED(svr))) { |
| 1337 | Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p", |
| 1338 | (void*)svr); |
| 1339 | } |
| 1340 | /* avoid break flag while copying; otherwise COW etc |
| 1341 | * disabled... */ |
| 1342 | SvFLAGS(svr) &= ~SVf_BREAK; |
| 1343 | /* Not newSVsv(), as it does not allow copy-on-write, |
| 1344 | resulting in wasteful copies. |
| 1345 | Also, we use SV_NOSTEAL in case the SV is used more than |
| 1346 | once, e.g. (...) = (f())[0,0] |
| 1347 | Where the same SV appears twice on the RHS without a ref |
| 1348 | count bump. (Although I suspect that the SV won't be |
| 1349 | stealable here anyway - DAPM). |
| 1350 | */ |
| 1351 | *relem = sv_mortalcopy_flags(svr, |
| 1352 | SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL); |
| 1353 | /* ... but restore afterwards in case it's needed again, |
| 1354 | * e.g. ($a,$b,$c) = (1,$a,$a) |
| 1355 | */ |
| 1356 | SvFLAGS(svr) |= brk; |
| 1357 | } |
| 1358 | |
| 1359 | if (!lcount) |
| 1360 | break; |
| 1361 | } |
| 1362 | |
| 1363 | if (!marked) |
| 1364 | return; |
| 1365 | |
| 1366 | /*unmark LHS */ |
| 1367 | |
| 1368 | while (lelem > firstlelem) { |
| 1369 | SV * const svl = *(--lelem); |
| 1370 | if (svl) |
| 1371 | SvFLAGS(svl) &= ~SVf_BREAK; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | |
| 1377 | PP(pp_aassign) |
| 1378 | { |
| 1379 | dVAR; dSP; |
| 1380 | SV **lastlelem = PL_stack_sp; |
| 1381 | SV **lastrelem = PL_stack_base + POPMARK; |
| 1382 | SV **firstrelem = PL_stack_base + POPMARK + 1; |
| 1383 | SV **firstlelem = lastrelem + 1; |
| 1384 | |
| 1385 | SV **relem; |
| 1386 | SV **lelem; |
| 1387 | U8 gimme; |
| 1388 | /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we |
| 1389 | * only need to save locally, not on the save stack */ |
| 1390 | U16 old_delaymagic = PL_delaymagic; |
| 1391 | #ifdef DEBUGGING |
| 1392 | bool fake = 0; |
| 1393 | #endif |
| 1394 | |
| 1395 | PL_delaymagic = DM_DELAY; /* catch simultaneous items */ |
| 1396 | |
| 1397 | /* If there's a common identifier on both sides we have to take |
| 1398 | * special care that assigning the identifier on the left doesn't |
| 1399 | * clobber a value on the right that's used later in the list. |
| 1400 | */ |
| 1401 | |
| 1402 | /* at least 2 LH and RH elements, or commonality isn't an issue */ |
| 1403 | if (firstlelem < lastlelem && firstrelem < lastrelem) { |
| 1404 | for (relem = firstrelem+1; relem <= lastrelem; relem++) { |
| 1405 | if (SvGMAGICAL(*relem)) |
| 1406 | goto do_scan; |
| 1407 | } |
| 1408 | for (lelem = firstlelem; lelem <= lastlelem; lelem++) { |
| 1409 | if (*lelem && SvSMAGICAL(*lelem)) |
| 1410 | goto do_scan; |
| 1411 | } |
| 1412 | if ( PL_op->op_private & (OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1) ) { |
| 1413 | if (PL_op->op_private & OPpASSIGN_COMMON_RC1) { |
| 1414 | /* skip the scan if all scalars have a ref count of 1 */ |
| 1415 | for (lelem = firstlelem; lelem <= lastlelem; lelem++) { |
| 1416 | SV *sv = *lelem; |
| 1417 | if (!sv || SvREFCNT(sv) == 1) |
| 1418 | continue; |
| 1419 | if (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVAV) |
| 1420 | goto do_scan; |
| 1421 | break; |
| 1422 | } |
| 1423 | } |
| 1424 | else { |
| 1425 | do_scan: |
| 1426 | S_aassign_copy_common(aTHX_ |
| 1427 | firstlelem, lastlelem, firstrelem, lastrelem |
| 1428 | #ifdef DEBUGGING |
| 1429 | , fake |
| 1430 | #endif |
| 1431 | ); |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | #ifdef DEBUGGING |
| 1436 | else { |
| 1437 | /* on debugging builds, do the scan even if we've concluded we |
| 1438 | * don't need to, then panic if we find commonality. Note that the |
| 1439 | * scanner assumes at least 2 elements */ |
| 1440 | if (firstlelem < lastlelem && firstrelem < lastrelem) { |
| 1441 | fake = 1; |
| 1442 | goto do_scan; |
| 1443 | } |
| 1444 | } |
| 1445 | #endif |
| 1446 | |
| 1447 | gimme = GIMME_V; |
| 1448 | relem = firstrelem; |
| 1449 | lelem = firstlelem; |
| 1450 | |
| 1451 | if (relem > lastrelem) |
| 1452 | goto no_relems; |
| 1453 | |
| 1454 | /* first lelem loop while there are still relems */ |
| 1455 | while (LIKELY(lelem <= lastlelem)) { |
| 1456 | bool alias = FALSE; |
| 1457 | SV *lsv = *lelem++; |
| 1458 | |
| 1459 | TAINT_NOT; /* Each item stands on its own, taintwise. */ |
| 1460 | |
| 1461 | assert(relem <= lastrelem); |
| 1462 | if (UNLIKELY(!lsv)) { |
| 1463 | alias = TRUE; |
| 1464 | lsv = *lelem++; |
| 1465 | ASSUME(SvTYPE(lsv) == SVt_PVAV); |
| 1466 | } |
| 1467 | |
| 1468 | switch (SvTYPE(lsv)) { |
| 1469 | case SVt_PVAV: { |
| 1470 | SV **svp; |
| 1471 | SSize_t i; |
| 1472 | SSize_t tmps_base; |
| 1473 | SSize_t nelems = lastrelem - relem + 1; |
| 1474 | AV *ary = MUTABLE_AV(lsv); |
| 1475 | |
| 1476 | /* Assigning to an aggregate is tricky. First there is the |
| 1477 | * issue of commonality, e.g. @a = ($a[0]). Since the |
| 1478 | * stack isn't refcounted, clearing @a prior to storing |
| 1479 | * elements will free $a[0]. Similarly with |
| 1480 | * sub FETCH { $status[$_[1]] } @status = @tied[0,1]; |
| 1481 | * |
| 1482 | * The way to avoid these issues is to make the copy of each |
| 1483 | * SV (and we normally store a *copy* in the array) *before* |
| 1484 | * clearing the array. But this has a problem in that |
| 1485 | * if the code croaks during copying, the not-yet-stored copies |
| 1486 | * could leak. One way to avoid this is to make all the copies |
| 1487 | * mortal, but that's quite expensive. |
| 1488 | * |
| 1489 | * The current solution to these issues is to use a chunk |
| 1490 | * of the tmps stack as a temporary refcounted-stack. SVs |
| 1491 | * will be put on there during processing to avoid leaks, |
| 1492 | * but will be removed again before the end of this block, |
| 1493 | * so free_tmps() is never normally called. Also, the |
| 1494 | * sv_refcnt of the SVs doesn't have to be manipulated, since |
| 1495 | * the ownership of 1 reference count is transferred directly |
| 1496 | * from the tmps stack to the AV when the SV is stored. |
| 1497 | * |
| 1498 | * We disarm slots in the temps stack by storing PL_sv_undef |
| 1499 | * there: it doesn't matter if that SV's refcount is |
| 1500 | * repeatedly decremented during a croak. But usually this is |
| 1501 | * only an interim measure. By the end of this code block |
| 1502 | * we try where possible to not leave any PL_sv_undef's on the |
| 1503 | * tmps stack e.g. by shuffling newer entries down. |
| 1504 | * |
| 1505 | * There is one case where we don't copy: non-magical |
| 1506 | * SvTEMP(sv)'s with a ref count of 1. The only owner of these |
| 1507 | * is on the tmps stack, so its safe to directly steal the SV |
| 1508 | * rather than copying. This is common in things like function |
| 1509 | * returns, map etc, which all return a list of such SVs. |
| 1510 | * |
| 1511 | * Note however something like @a = (f())[0,0], where there is |
| 1512 | * a danger of the same SV being shared: this avoided because |
| 1513 | * when the SV is stored as $a[0], its ref count gets bumped, |
| 1514 | * so the RC==1 test fails and the second element is copied |
| 1515 | * instead. |
| 1516 | * |
| 1517 | * We also use one slot in the tmps stack to hold an extra |
| 1518 | * ref to the array, to ensure it doesn't get prematurely |
| 1519 | * freed. Again, this is removed before the end of this block. |
| 1520 | * |
| 1521 | * Note that OPpASSIGN_COMMON_AGG is used to flag a possible |
| 1522 | * @a = ($a[0]) case, but the current implementation uses the |
| 1523 | * same algorithm regardless, so ignores that flag. (It *is* |
| 1524 | * used in the hash branch below, however). |
| 1525 | */ |
| 1526 | |
| 1527 | /* Reserve slots for ary, plus the elems we're about to copy, |
| 1528 | * then protect ary and temporarily void the remaining slots |
| 1529 | * with &PL_sv_undef */ |
| 1530 | EXTEND_MORTAL(nelems + 1); |
| 1531 | PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(ary); |
| 1532 | tmps_base = PL_tmps_ix + 1; |
| 1533 | for (i = 0; i < nelems; i++) |
| 1534 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; |
| 1535 | PL_tmps_ix += nelems; |
| 1536 | |
| 1537 | /* Make a copy of each RHS elem and save on the tmps_stack |
| 1538 | * (or pass through where we can optimise away the copy) */ |
| 1539 | |
| 1540 | if (UNLIKELY(alias)) { |
| 1541 | U32 lval = (gimme == G_ARRAY) |
| 1542 | ? (PL_op->op_flags & OPf_MOD || LVRET) : 0; |
| 1543 | for (svp = relem; svp <= lastrelem; svp++) { |
| 1544 | SV *rsv = *svp; |
| 1545 | |
| 1546 | SvGETMAGIC(rsv); |
| 1547 | if (!SvROK(rsv)) |
| 1548 | DIE(aTHX_ "Assigned value is not a reference"); |
| 1549 | if (SvTYPE(SvRV(rsv)) > SVt_PVLV) |
| 1550 | /* diag_listed_as: Assigned value is not %s reference */ |
| 1551 | DIE(aTHX_ |
| 1552 | "Assigned value is not a SCALAR reference"); |
| 1553 | if (lval) |
| 1554 | *svp = rsv = sv_mortalcopy(rsv); |
| 1555 | /* XXX else check for weak refs? */ |
| 1556 | rsv = SvREFCNT_inc_NN(SvRV(rsv)); |
| 1557 | assert(tmps_base <= PL_tmps_max); |
| 1558 | PL_tmps_stack[tmps_base++] = rsv; |
| 1559 | } |
| 1560 | } |
| 1561 | else { |
| 1562 | for (svp = relem; svp <= lastrelem; svp++) { |
| 1563 | SV *rsv = *svp; |
| 1564 | |
| 1565 | if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) { |
| 1566 | /* can skip the copy */ |
| 1567 | SvREFCNT_inc_simple_void_NN(rsv); |
| 1568 | SvTEMP_off(rsv); |
| 1569 | } |
| 1570 | else { |
| 1571 | SV *nsv; |
| 1572 | /* do get before newSV, in case it dies and leaks */ |
| 1573 | SvGETMAGIC(rsv); |
| 1574 | nsv = newSV(0); |
| 1575 | /* see comment in S_aassign_copy_common about |
| 1576 | * SV_NOSTEAL */ |
| 1577 | sv_setsv_flags(nsv, rsv, |
| 1578 | (SV_DO_COW_SVSETSV|SV_NOSTEAL)); |
| 1579 | rsv = *svp = nsv; |
| 1580 | } |
| 1581 | |
| 1582 | assert(tmps_base <= PL_tmps_max); |
| 1583 | PL_tmps_stack[tmps_base++] = rsv; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | if (SvRMAGICAL(ary) || AvFILLp(ary) >= 0) /* may be non-empty */ |
| 1588 | av_clear(ary); |
| 1589 | |
| 1590 | /* store in the array, the SVs that are in the tmps stack */ |
| 1591 | |
| 1592 | tmps_base -= nelems; |
| 1593 | |
| 1594 | if (SvMAGICAL(ary) || SvREADONLY(ary) || !AvREAL(ary)) { |
| 1595 | /* for arrays we can't cheat with, use the official API */ |
| 1596 | av_extend(ary, nelems - 1); |
| 1597 | for (i = 0; i < nelems; i++) { |
| 1598 | SV **svp = &(PL_tmps_stack[tmps_base + i]); |
| 1599 | SV *rsv = *svp; |
| 1600 | /* A tied store won't take ownership of rsv, so keep |
| 1601 | * the 1 refcnt on the tmps stack; otherwise disarm |
| 1602 | * the tmps stack entry */ |
| 1603 | if (av_store(ary, i, rsv)) |
| 1604 | *svp = &PL_sv_undef; |
| 1605 | /* av_store() may have added set magic to rsv */; |
| 1606 | SvSETMAGIC(rsv); |
| 1607 | } |
| 1608 | /* disarm ary refcount: see comments below about leak */ |
| 1609 | PL_tmps_stack[tmps_base - 1] = &PL_sv_undef; |
| 1610 | } |
| 1611 | else { |
| 1612 | /* directly access/set the guts of the AV */ |
| 1613 | SSize_t fill = nelems - 1; |
| 1614 | if (fill > AvMAX(ary)) |
| 1615 | av_extend_guts(ary, fill, &AvMAX(ary), &AvALLOC(ary), |
| 1616 | &AvARRAY(ary)); |
| 1617 | AvFILLp(ary) = fill; |
| 1618 | Copy(&(PL_tmps_stack[tmps_base]), AvARRAY(ary), nelems, SV*); |
| 1619 | /* Quietly remove all the SVs from the tmps stack slots, |
| 1620 | * since ary has now taken ownership of the refcnt. |
| 1621 | * Also remove ary: which will now leak if we die before |
| 1622 | * the SvREFCNT_dec_NN(ary) below */ |
| 1623 | if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems)) |
| 1624 | Move(&PL_tmps_stack[tmps_base + nelems], |
| 1625 | &PL_tmps_stack[tmps_base - 1], |
| 1626 | PL_tmps_ix - (tmps_base + nelems) + 1, |
| 1627 | SV*); |
| 1628 | PL_tmps_ix -= (nelems + 1); |
| 1629 | } |
| 1630 | |
| 1631 | if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA)) |
| 1632 | /* its assumed @ISA set magic can't die and leak ary */ |
| 1633 | SvSETMAGIC(MUTABLE_SV(ary)); |
| 1634 | SvREFCNT_dec_NN(ary); |
| 1635 | |
| 1636 | relem = lastrelem + 1; |
| 1637 | goto no_relems; |
| 1638 | } |
| 1639 | |
| 1640 | case SVt_PVHV: { /* normal hash */ |
| 1641 | |
| 1642 | SV **svp; |
| 1643 | bool dirty_tmps; |
| 1644 | SSize_t i; |
| 1645 | SSize_t tmps_base; |
| 1646 | SSize_t nelems = lastrelem - relem + 1; |
| 1647 | HV *hash = MUTABLE_HV(lsv); |
| 1648 | |
| 1649 | if (UNLIKELY(nelems & 1)) { |
| 1650 | do_oddball(lastrelem, relem); |
| 1651 | /* we have firstlelem to reuse, it's not needed any more */ |
| 1652 | *++lastrelem = &PL_sv_undef; |
| 1653 | nelems++; |
| 1654 | } |
| 1655 | |
| 1656 | /* See the SVt_PVAV branch above for a long description of |
| 1657 | * how the following all works. The main difference for hashes |
| 1658 | * is that we treat keys and values separately (and have |
| 1659 | * separate loops for them): as for arrays, values are always |
| 1660 | * copied (except for the SvTEMP optimisation), since they |
| 1661 | * need to be stored in the hash; while keys are only |
| 1662 | * processed where they might get prematurely freed or |
| 1663 | * whatever. */ |
| 1664 | |
| 1665 | /* tmps stack slots: |
| 1666 | * * reserve a slot for the hash keepalive; |
| 1667 | * * reserve slots for the hash values we're about to copy; |
| 1668 | * * preallocate for the keys we'll possibly copy or refcount bump |
| 1669 | * later; |
| 1670 | * then protect hash and temporarily void the remaining |
| 1671 | * value slots with &PL_sv_undef */ |
| 1672 | EXTEND_MORTAL(nelems + 1); |
| 1673 | |
| 1674 | /* convert to number of key/value pairs */ |
| 1675 | nelems >>= 1; |
| 1676 | |
| 1677 | PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(hash); |
| 1678 | tmps_base = PL_tmps_ix + 1; |
| 1679 | for (i = 0; i < nelems; i++) |
| 1680 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; |
| 1681 | PL_tmps_ix += nelems; |
| 1682 | |
| 1683 | /* Make a copy of each RHS hash value and save on the tmps_stack |
| 1684 | * (or pass through where we can optimise away the copy) */ |
| 1685 | |
| 1686 | for (svp = relem + 1; svp <= lastrelem; svp += 2) { |
| 1687 | SV *rsv = *svp; |
| 1688 | |
| 1689 | if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) { |
| 1690 | /* can skip the copy */ |
| 1691 | SvREFCNT_inc_simple_void_NN(rsv); |
| 1692 | SvTEMP_off(rsv); |
| 1693 | } |
| 1694 | else { |
| 1695 | SV *nsv; |
| 1696 | /* do get before newSV, in case it dies and leaks */ |
| 1697 | SvGETMAGIC(rsv); |
| 1698 | nsv = newSV(0); |
| 1699 | /* see comment in S_aassign_copy_common about |
| 1700 | * SV_NOSTEAL */ |
| 1701 | sv_setsv_flags(nsv, rsv, |
| 1702 | (SV_DO_COW_SVSETSV|SV_NOSTEAL)); |
| 1703 | rsv = *svp = nsv; |
| 1704 | } |
| 1705 | |
| 1706 | assert(tmps_base <= PL_tmps_max); |
| 1707 | PL_tmps_stack[tmps_base++] = rsv; |
| 1708 | } |
| 1709 | tmps_base -= nelems; |
| 1710 | |
| 1711 | |
| 1712 | /* possibly protect keys */ |
| 1713 | |
| 1714 | if (UNLIKELY(gimme == G_ARRAY)) { |
| 1715 | /* handle e.g. |
| 1716 | * @a = ((%h = ($$r, 1)), $r = "x"); |
| 1717 | * $_++ for %h = (1,2,3,4); |
| 1718 | */ |
| 1719 | EXTEND_MORTAL(nelems); |
| 1720 | for (svp = relem; svp <= lastrelem; svp += 2) |
| 1721 | *svp = sv_mortalcopy_flags(*svp, |
| 1722 | SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL); |
| 1723 | } |
| 1724 | else if (PL_op->op_private & OPpASSIGN_COMMON_AGG) { |
| 1725 | /* for possible commonality, e.g. |
| 1726 | * %h = ($h{a},1) |
| 1727 | * avoid premature freeing RHS keys by mortalising |
| 1728 | * them. |
| 1729 | * For a magic element, make a copy so that its magic is |
| 1730 | * called *before* the hash is emptied (which may affect |
| 1731 | * a tied value for example). |
| 1732 | * In theory we should check for magic keys in all |
| 1733 | * cases, not just under OPpASSIGN_COMMON_AGG, but in |
| 1734 | * practice, !OPpASSIGN_COMMON_AGG implies only |
| 1735 | * constants or padtmps on the RHS. |
| 1736 | */ |
| 1737 | EXTEND_MORTAL(nelems); |
| 1738 | for (svp = relem; svp <= lastrelem; svp += 2) { |
| 1739 | SV *rsv = *svp; |
| 1740 | if (UNLIKELY(SvGMAGICAL(rsv))) { |
| 1741 | SSize_t n; |
| 1742 | *svp = sv_mortalcopy_flags(*svp, |
| 1743 | SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL); |
| 1744 | /* allow other branch to continue pushing |
| 1745 | * onto tmps stack without checking each time */ |
| 1746 | n = (lastrelem - relem) >> 1; |
| 1747 | EXTEND_MORTAL(n); |
| 1748 | } |
| 1749 | else |
| 1750 | PL_tmps_stack[++PL_tmps_ix] = |
| 1751 | SvREFCNT_inc_simple_NN(rsv); |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | if (SvRMAGICAL(hash) || HvUSEDKEYS(hash)) |
| 1756 | hv_clear(hash); |
| 1757 | |
| 1758 | /* now assign the keys and values to the hash */ |
| 1759 | |
| 1760 | dirty_tmps = FALSE; |
| 1761 | |
| 1762 | if (UNLIKELY(gimme == G_ARRAY)) { |
| 1763 | /* @a = (%h = (...)) etc */ |
| 1764 | SV **svp; |
| 1765 | SV **topelem = relem; |
| 1766 | |
| 1767 | for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) { |
| 1768 | SV *key = *svp++; |
| 1769 | SV *val = *svp; |
| 1770 | /* remove duplicates from list we return */ |
| 1771 | if (!hv_exists_ent(hash, key, 0)) { |
| 1772 | /* copy key back: possibly to an earlier |
| 1773 | * stack location if we encountered dups earlier, |
| 1774 | * The values will be updated later |
| 1775 | */ |
| 1776 | *topelem = key; |
| 1777 | topelem += 2; |
| 1778 | } |
| 1779 | /* A tied store won't take ownership of val, so keep |
| 1780 | * the 1 refcnt on the tmps stack; otherwise disarm |
| 1781 | * the tmps stack entry */ |
| 1782 | if (hv_store_ent(hash, key, val, 0)) |
| 1783 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; |
| 1784 | else |
| 1785 | dirty_tmps = TRUE; |
| 1786 | /* hv_store_ent() may have added set magic to val */; |
| 1787 | SvSETMAGIC(val); |
| 1788 | } |
| 1789 | if (topelem < svp) { |
| 1790 | /* at this point we have removed the duplicate key/value |
| 1791 | * pairs from the stack, but the remaining values may be |
| 1792 | * wrong; i.e. with (a 1 a 2 b 3) on the stack we've removed |
| 1793 | * the (a 2), but the stack now probably contains |
| 1794 | * (a <freed> b 3), because { hv_save(a,1); hv_save(a,2) } |
| 1795 | * obliterates the earlier key. So refresh all values. */ |
| 1796 | lastrelem = topelem - 1; |
| 1797 | while (relem < lastrelem) { |
| 1798 | HE *he; |
| 1799 | he = hv_fetch_ent(hash, *relem++, 0, 0); |
| 1800 | *relem++ = (he ? HeVAL(he) : &PL_sv_undef); |
| 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | else { |
| 1805 | SV **svp; |
| 1806 | for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) { |
| 1807 | SV *key = *svp++; |
| 1808 | SV *val = *svp; |
| 1809 | if (hv_store_ent(hash, key, val, 0)) |
| 1810 | PL_tmps_stack[tmps_base + i] = &PL_sv_undef; |
| 1811 | else |
| 1812 | dirty_tmps = TRUE; |
| 1813 | /* hv_store_ent() may have added set magic to val */; |
| 1814 | SvSETMAGIC(val); |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | if (dirty_tmps) { |
| 1819 | /* there are still some 'live' recounts on the tmps stack |
| 1820 | * - usually caused by storing into a tied hash. So let |
| 1821 | * free_tmps() do the proper but slow job later. |
| 1822 | * Just disarm hash refcount: see comments below about leak |
| 1823 | */ |
| 1824 | PL_tmps_stack[tmps_base - 1] = &PL_sv_undef; |
| 1825 | } |
| 1826 | else { |
| 1827 | /* Quietly remove all the SVs from the tmps stack slots, |
| 1828 | * since hash has now taken ownership of the refcnt. |
| 1829 | * Also remove hash: which will now leak if we die before |
| 1830 | * the SvREFCNT_dec_NN(hash) below */ |
| 1831 | if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems)) |
| 1832 | Move(&PL_tmps_stack[tmps_base + nelems], |
| 1833 | &PL_tmps_stack[tmps_base - 1], |
| 1834 | PL_tmps_ix - (tmps_base + nelems) + 1, |
| 1835 | SV*); |
| 1836 | PL_tmps_ix -= (nelems + 1); |
| 1837 | } |
| 1838 | |
| 1839 | SvREFCNT_dec_NN(hash); |
| 1840 | |
| 1841 | relem = lastrelem + 1; |
| 1842 | goto no_relems; |
| 1843 | } |
| 1844 | |
| 1845 | default: |
| 1846 | if (!SvIMMORTAL(lsv)) { |
| 1847 | SV *ref; |
| 1848 | |
| 1849 | if (UNLIKELY( |
| 1850 | SvTEMP(lsv) && !SvSMAGICAL(lsv) && SvREFCNT(lsv) == 1 && |
| 1851 | (!isGV_with_GP(lsv) || SvFAKE(lsv)) && ckWARN(WARN_MISC) |
| 1852 | )) |
| 1853 | Perl_warner(aTHX_ |
| 1854 | packWARN(WARN_MISC), |
| 1855 | "Useless assignment to a temporary" |
| 1856 | ); |
| 1857 | |
| 1858 | /* avoid freeing $$lsv if it might be needed for further |
| 1859 | * elements, e.g. ($ref, $foo) = (1, $$ref) */ |
| 1860 | if ( SvROK(lsv) |
| 1861 | && ( ((ref = SvRV(lsv)), SvREFCNT(ref)) == 1) |
| 1862 | && lelem <= lastlelem |
| 1863 | ) { |
| 1864 | SSize_t ix; |
| 1865 | SvREFCNT_inc_simple_void_NN(ref); |
| 1866 | /* an unrolled sv_2mortal */ |
| 1867 | ix = ++PL_tmps_ix; |
| 1868 | if (UNLIKELY(ix >= PL_tmps_max)) |
| 1869 | /* speculatively grow enough to cover other |
| 1870 | * possible refs */ |
| 1871 | (void)tmps_grow_p(ix + (lastlelem - lelem)); |
| 1872 | PL_tmps_stack[ix] = ref; |
| 1873 | } |
| 1874 | |
| 1875 | sv_setsv(lsv, *relem); |
| 1876 | *relem = lsv; |
| 1877 | SvSETMAGIC(lsv); |
| 1878 | } |
| 1879 | if (++relem > lastrelem) |
| 1880 | goto no_relems; |
| 1881 | break; |
| 1882 | } /* switch */ |
| 1883 | } /* while */ |
| 1884 | |
| 1885 | |
| 1886 | no_relems: |
| 1887 | |
| 1888 | /* simplified lelem loop for when there are no relems left */ |
| 1889 | while (LIKELY(lelem <= lastlelem)) { |
| 1890 | SV *lsv = *lelem++; |
| 1891 | |
| 1892 | TAINT_NOT; /* Each item stands on its own, taintwise. */ |
| 1893 | |
| 1894 | if (UNLIKELY(!lsv)) { |
| 1895 | lsv = *lelem++; |
| 1896 | ASSUME(SvTYPE(lsv) == SVt_PVAV); |
| 1897 | } |
| 1898 | |
| 1899 | switch (SvTYPE(lsv)) { |
| 1900 | case SVt_PVAV: |
| 1901 | if (SvRMAGICAL(lsv) || AvFILLp((SV*)lsv) >= 0) { |
| 1902 | av_clear((AV*)lsv); |
| 1903 | if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA)) |
| 1904 | SvSETMAGIC(lsv); |
| 1905 | } |
| 1906 | break; |
| 1907 | |
| 1908 | case SVt_PVHV: |
| 1909 | if (SvRMAGICAL(lsv) || HvUSEDKEYS((HV*)lsv)) |
| 1910 | hv_clear((HV*)lsv); |
| 1911 | break; |
| 1912 | |
| 1913 | default: |
| 1914 | if (!SvIMMORTAL(lsv)) { |
| 1915 | sv_set_undef(lsv); |
| 1916 | SvSETMAGIC(lsv); |
| 1917 | *relem++ = lsv; |
| 1918 | } |
| 1919 | break; |
| 1920 | } /* switch */ |
| 1921 | } /* while */ |
| 1922 | |
| 1923 | TAINT_NOT; /* result of list assign isn't tainted */ |
| 1924 | |
| 1925 | if (UNLIKELY(PL_delaymagic & ~DM_DELAY)) { |
| 1926 | /* Will be used to set PL_tainting below */ |
| 1927 | Uid_t tmp_uid = PerlProc_getuid(); |
| 1928 | Uid_t tmp_euid = PerlProc_geteuid(); |
| 1929 | Gid_t tmp_gid = PerlProc_getgid(); |
| 1930 | Gid_t tmp_egid = PerlProc_getegid(); |
| 1931 | |
| 1932 | /* XXX $> et al currently silently ignore failures */ |
| 1933 | if (PL_delaymagic & DM_UID) { |
| 1934 | #ifdef HAS_SETRESUID |
| 1935 | PERL_UNUSED_RESULT( |
| 1936 | setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1, |
| 1937 | (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1, |
| 1938 | (Uid_t)-1)); |
| 1939 | #else |
| 1940 | # ifdef HAS_SETREUID |
| 1941 | PERL_UNUSED_RESULT( |
| 1942 | setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1, |
| 1943 | (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1)); |
| 1944 | # else |
| 1945 | # ifdef HAS_SETRUID |
| 1946 | if ((PL_delaymagic & DM_UID) == DM_RUID) { |
| 1947 | PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid)); |
| 1948 | PL_delaymagic &= ~DM_RUID; |
| 1949 | } |
| 1950 | # endif /* HAS_SETRUID */ |
| 1951 | # ifdef HAS_SETEUID |
| 1952 | if ((PL_delaymagic & DM_UID) == DM_EUID) { |
| 1953 | PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid)); |
| 1954 | PL_delaymagic &= ~DM_EUID; |
| 1955 | } |
| 1956 | # endif /* HAS_SETEUID */ |
| 1957 | if (PL_delaymagic & DM_UID) { |
| 1958 | if (PL_delaymagic_uid != PL_delaymagic_euid) |
| 1959 | DIE(aTHX_ "No setreuid available"); |
| 1960 | PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid)); |
| 1961 | } |
| 1962 | # endif /* HAS_SETREUID */ |
| 1963 | #endif /* HAS_SETRESUID */ |
| 1964 | |
| 1965 | tmp_uid = PerlProc_getuid(); |
| 1966 | tmp_euid = PerlProc_geteuid(); |
| 1967 | } |
| 1968 | /* XXX $> et al currently silently ignore failures */ |
| 1969 | if (PL_delaymagic & DM_GID) { |
| 1970 | #ifdef HAS_SETRESGID |
| 1971 | PERL_UNUSED_RESULT( |
| 1972 | setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1, |
| 1973 | (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1, |
| 1974 | (Gid_t)-1)); |
| 1975 | #else |
| 1976 | # ifdef HAS_SETREGID |
| 1977 | PERL_UNUSED_RESULT( |
| 1978 | setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1, |
| 1979 | (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1)); |
| 1980 | # else |
| 1981 | # ifdef HAS_SETRGID |
| 1982 | if ((PL_delaymagic & DM_GID) == DM_RGID) { |
| 1983 | PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid)); |
| 1984 | PL_delaymagic &= ~DM_RGID; |
| 1985 | } |
| 1986 | # endif /* HAS_SETRGID */ |
| 1987 | # ifdef HAS_SETEGID |
| 1988 | if ((PL_delaymagic & DM_GID) == DM_EGID) { |
| 1989 | PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid)); |
| 1990 | PL_delaymagic &= ~DM_EGID; |
| 1991 | } |
| 1992 | # endif /* HAS_SETEGID */ |
| 1993 | if (PL_delaymagic & DM_GID) { |
| 1994 | if (PL_delaymagic_gid != PL_delaymagic_egid) |
| 1995 | DIE(aTHX_ "No setregid available"); |
| 1996 | PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid)); |
| 1997 | } |
| 1998 | # endif /* HAS_SETREGID */ |
| 1999 | #endif /* HAS_SETRESGID */ |
| 2000 | |
| 2001 | tmp_gid = PerlProc_getgid(); |
| 2002 | tmp_egid = PerlProc_getegid(); |
| 2003 | } |
| 2004 | TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) ); |
| 2005 | #ifdef NO_TAINT_SUPPORT |
| 2006 | PERL_UNUSED_VAR(tmp_uid); |
| 2007 | PERL_UNUSED_VAR(tmp_euid); |
| 2008 | PERL_UNUSED_VAR(tmp_gid); |
| 2009 | PERL_UNUSED_VAR(tmp_egid); |
| 2010 | #endif |
| 2011 | } |
| 2012 | PL_delaymagic = old_delaymagic; |
| 2013 | |
| 2014 | if (gimme == G_VOID) |
| 2015 | SP = firstrelem - 1; |
| 2016 | else if (gimme == G_SCALAR) { |
| 2017 | SP = firstrelem; |
| 2018 | EXTEND(SP,1); |
| 2019 | if (PL_op->op_private & OPpASSIGN_TRUEBOOL) |
| 2020 | SETs((firstlelem - firstrelem) ? &PL_sv_yes : &PL_sv_zero); |
| 2021 | else { |
| 2022 | dTARGET; |
| 2023 | SETi(firstlelem - firstrelem); |
| 2024 | } |
| 2025 | } |
| 2026 | else |
| 2027 | SP = relem - 1; |
| 2028 | |
| 2029 | RETURN; |
| 2030 | } |
| 2031 | |
| 2032 | PP(pp_qr) |
| 2033 | { |
| 2034 | dSP; |
| 2035 | PMOP * const pm = cPMOP; |
| 2036 | REGEXP * rx = PM_GETRE(pm); |
| 2037 | regexp *prog = ReANY(rx); |
| 2038 | SV * const pkg = RXp_ENGINE(prog)->qr_package(aTHX_ (rx)); |
| 2039 | SV * const rv = sv_newmortal(); |
| 2040 | CV **cvp; |
| 2041 | CV *cv; |
| 2042 | |
| 2043 | SvUPGRADE(rv, SVt_IV); |
| 2044 | /* For a subroutine describing itself as "This is a hacky workaround" I'm |
| 2045 | loathe to use it here, but it seems to be the right fix. Or close. |
| 2046 | The key part appears to be that it's essential for pp_qr to return a new |
| 2047 | object (SV), which implies that there needs to be an effective way to |
| 2048 | generate a new SV from the existing SV that is pre-compiled in the |
| 2049 | optree. */ |
| 2050 | SvRV_set(rv, MUTABLE_SV(reg_temp_copy(NULL, rx))); |
| 2051 | SvROK_on(rv); |
| 2052 | |
| 2053 | cvp = &( ReANY((REGEXP *)SvRV(rv))->qr_anoncv); |
| 2054 | if (UNLIKELY((cv = *cvp) && CvCLONE(*cvp))) { |
| 2055 | *cvp = cv_clone(cv); |
| 2056 | SvREFCNT_dec_NN(cv); |
| 2057 | } |
| 2058 | |
| 2059 | if (pkg) { |
| 2060 | HV *const stash = gv_stashsv(pkg, GV_ADD); |
| 2061 | SvREFCNT_dec_NN(pkg); |
| 2062 | (void)sv_bless(rv, stash); |
| 2063 | } |
| 2064 | |
| 2065 | if (UNLIKELY(RXp_ISTAINTED(prog))) { |
| 2066 | SvTAINTED_on(rv); |
| 2067 | SvTAINTED_on(SvRV(rv)); |
| 2068 | } |
| 2069 | XPUSHs(rv); |
| 2070 | RETURN; |
| 2071 | } |
| 2072 | |
| 2073 | PP(pp_match) |
| 2074 | { |
| 2075 | dSP; dTARG; |
| 2076 | PMOP *pm = cPMOP; |
| 2077 | PMOP *dynpm = pm; |
| 2078 | const char *s; |
| 2079 | const char *strend; |
| 2080 | SSize_t curpos = 0; /* initial pos() or current $+[0] */ |
| 2081 | I32 global; |
| 2082 | U8 r_flags = 0; |
| 2083 | const char *truebase; /* Start of string */ |
| 2084 | REGEXP *rx = PM_GETRE(pm); |
| 2085 | regexp *prog = ReANY(rx); |
| 2086 | bool rxtainted; |
| 2087 | const U8 gimme = GIMME_V; |
| 2088 | STRLEN len; |
| 2089 | const I32 oldsave = PL_savestack_ix; |
| 2090 | I32 had_zerolen = 0; |
| 2091 | MAGIC *mg = NULL; |
| 2092 | |
| 2093 | if (PL_op->op_flags & OPf_STACKED) |
| 2094 | TARG = POPs; |
| 2095 | else { |
| 2096 | if (ARGTARG) |
| 2097 | GETTARGET; |
| 2098 | else { |
| 2099 | TARG = DEFSV; |
| 2100 | } |
| 2101 | EXTEND(SP,1); |
| 2102 | } |
| 2103 | |
| 2104 | PUTBACK; /* EVAL blocks need stack_sp. */ |
| 2105 | /* Skip get-magic if this is a qr// clone, because regcomp has |
| 2106 | already done it. */ |
| 2107 | truebase = prog->mother_re |
| 2108 | ? SvPV_nomg_const(TARG, len) |
| 2109 | : SvPV_const(TARG, len); |
| 2110 | if (!truebase) |
| 2111 | DIE(aTHX_ "panic: pp_match"); |
| 2112 | strend = truebase + len; |
| 2113 | rxtainted = (RXp_ISTAINTED(prog) || |
| 2114 | (TAINT_get && (pm->op_pmflags & PMf_RETAINT))); |
| 2115 | TAINT_NOT; |
| 2116 | |
| 2117 | /* We need to know this in case we fail out early - pos() must be reset */ |
| 2118 | global = dynpm->op_pmflags & PMf_GLOBAL; |
| 2119 | |
| 2120 | /* PMdf_USED is set after a ?? matches once */ |
| 2121 | if ( |
| 2122 | #ifdef USE_ITHREADS |
| 2123 | SvREADONLY(PL_regex_pad[pm->op_pmoffset]) |
| 2124 | #else |
| 2125 | pm->op_pmflags & PMf_USED |
| 2126 | #endif |
| 2127 | ) { |
| 2128 | DEBUG_r(PerlIO_printf(Perl_debug_log, "?? already matched once")); |
| 2129 | goto nope; |
| 2130 | } |
| 2131 | |
| 2132 | /* handle the empty pattern */ |
| 2133 | if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) { |
| 2134 | if (PL_curpm == PL_reg_curpm) { |
| 2135 | if (PL_curpm_under) { |
| 2136 | if (PL_curpm_under == PL_reg_curpm) { |
| 2137 | Perl_croak(aTHX_ "Infinite recursion via empty pattern"); |
| 2138 | } else { |
| 2139 | pm = PL_curpm_under; |
| 2140 | } |
| 2141 | } |
| 2142 | } else { |
| 2143 | pm = PL_curpm; |
| 2144 | } |
| 2145 | rx = PM_GETRE(pm); |
| 2146 | prog = ReANY(rx); |
| 2147 | } |
| 2148 | |
| 2149 | if (RXp_MINLEN(prog) >= 0 && (STRLEN)RXp_MINLEN(prog) > len) { |
| 2150 | DEBUG_r(PerlIO_printf(Perl_debug_log, "String shorter than min possible regex match (%" |
| 2151 | UVuf " < %" IVdf ")\n", |
| 2152 | (UV)len, (IV)RXp_MINLEN(prog))); |
| 2153 | goto nope; |
| 2154 | } |
| 2155 | |
| 2156 | /* get pos() if //g */ |
| 2157 | if (global) { |
| 2158 | mg = mg_find_mglob(TARG); |
| 2159 | if (mg && mg->mg_len >= 0) { |
| 2160 | curpos = MgBYTEPOS(mg, TARG, truebase, len); |
| 2161 | /* last time pos() was set, it was zero-length match */ |
| 2162 | if (mg->mg_flags & MGf_MINMATCH) |
| 2163 | had_zerolen = 1; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | #ifdef PERL_SAWAMPERSAND |
| 2168 | if ( RXp_NPARENS(prog) |
| 2169 | || PL_sawampersand |
| 2170 | || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY)) |
| 2171 | || (dynpm->op_pmflags & PMf_KEEPCOPY) |
| 2172 | ) |
| 2173 | #endif |
| 2174 | { |
| 2175 | r_flags |= (REXEC_COPY_STR|REXEC_COPY_SKIP_PRE); |
| 2176 | /* in @a =~ /(.)/g, we iterate multiple times, but copy the buffer |
| 2177 | * only on the first iteration. Therefore we need to copy $' as well |
| 2178 | * as $&, to make the rest of the string available for captures in |
| 2179 | * subsequent iterations */ |
| 2180 | if (! (global && gimme == G_ARRAY)) |
| 2181 | r_flags |= REXEC_COPY_SKIP_POST; |
| 2182 | }; |
| 2183 | #ifdef PERL_SAWAMPERSAND |
| 2184 | if (dynpm->op_pmflags & PMf_KEEPCOPY) |
| 2185 | /* handle KEEPCOPY in pmop but not rx, eg $r=qr/a/; /$r/p */ |
| 2186 | r_flags &= ~(REXEC_COPY_SKIP_PRE|REXEC_COPY_SKIP_POST); |
| 2187 | #endif |
| 2188 | |
| 2189 | s = truebase; |
| 2190 | |
| 2191 | play_it_again: |
| 2192 | if (global) |
| 2193 | s = truebase + curpos; |
| 2194 | |
| 2195 | if (!CALLREGEXEC(rx, (char*)s, (char *)strend, (char*)truebase, |
| 2196 | had_zerolen, TARG, NULL, r_flags)) |
| 2197 | goto nope; |
| 2198 | |
| 2199 | PL_curpm = pm; |
| 2200 | if (dynpm->op_pmflags & PMf_ONCE) |
| 2201 | #ifdef USE_ITHREADS |
| 2202 | SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]); |
| 2203 | #else |
| 2204 | dynpm->op_pmflags |= PMf_USED; |
| 2205 | #endif |
| 2206 | |
| 2207 | if (rxtainted) |
| 2208 | RXp_MATCH_TAINTED_on(prog); |
| 2209 | TAINT_IF(RXp_MATCH_TAINTED(prog)); |
| 2210 | |
| 2211 | /* update pos */ |
| 2212 | |
| 2213 | if (global && (gimme != G_ARRAY || (dynpm->op_pmflags & PMf_CONTINUE))) { |
| 2214 | if (!mg) |
| 2215 | mg = sv_magicext_mglob(TARG); |
| 2216 | MgBYTEPOS_set(mg, TARG, truebase, RXp_OFFS(prog)[0].end); |
| 2217 | if (RXp_ZERO_LEN(prog)) |
| 2218 | mg->mg_flags |= MGf_MINMATCH; |
| 2219 | else |
| 2220 | mg->mg_flags &= ~MGf_MINMATCH; |
| 2221 | } |
| 2222 | |
| 2223 | if ((!RXp_NPARENS(prog) && !global) || gimme != G_ARRAY) { |
| 2224 | LEAVE_SCOPE(oldsave); |
| 2225 | RETPUSHYES; |
| 2226 | } |
| 2227 | |
| 2228 | /* push captures on stack */ |
| 2229 | |
| 2230 | { |
| 2231 | const I32 nparens = RXp_NPARENS(prog); |
| 2232 | I32 i = (global && !nparens) ? 1 : 0; |
| 2233 | |
| 2234 | SPAGAIN; /* EVAL blocks could move the stack. */ |
| 2235 | EXTEND(SP, nparens + i); |
| 2236 | EXTEND_MORTAL(nparens + i); |
| 2237 | for (i = !i; i <= nparens; i++) { |
| 2238 | PUSHs(sv_newmortal()); |
| 2239 | if (LIKELY((RXp_OFFS(prog)[i].start != -1) |
| 2240 | && RXp_OFFS(prog)[i].end != -1 )) |
| 2241 | { |
| 2242 | const I32 len = RXp_OFFS(prog)[i].end - RXp_OFFS(prog)[i].start; |
| 2243 | const char * const s = RXp_OFFS(prog)[i].start + truebase; |
| 2244 | if (UNLIKELY( RXp_OFFS(prog)[i].end < 0 |
| 2245 | || RXp_OFFS(prog)[i].start < 0 |
| 2246 | || len < 0 |
| 2247 | || len > strend - s) |
| 2248 | ) |
| 2249 | DIE(aTHX_ "panic: pp_match start/end pointers, i=%ld, " |
| 2250 | "start=%ld, end=%ld, s=%p, strend=%p, len=%" UVuf, |
| 2251 | (long) i, (long) RXp_OFFS(prog)[i].start, |
| 2252 | (long)RXp_OFFS(prog)[i].end, s, strend, (UV) len); |
| 2253 | sv_setpvn(*SP, s, len); |
| 2254 | if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len)) |
| 2255 | SvUTF8_on(*SP); |
| 2256 | } |
| 2257 | } |
| 2258 | if (global) { |
| 2259 | curpos = (UV)RXp_OFFS(prog)[0].end; |
| 2260 | had_zerolen = RXp_ZERO_LEN(prog); |
| 2261 | PUTBACK; /* EVAL blocks may use stack */ |
| 2262 | r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST; |
| 2263 | goto play_it_again; |
| 2264 | } |
| 2265 | LEAVE_SCOPE(oldsave); |
| 2266 | RETURN; |
| 2267 | } |
| 2268 | NOT_REACHED; /* NOTREACHED */ |
| 2269 | |
| 2270 | nope: |
| 2271 | if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) { |
| 2272 | if (!mg) |
| 2273 | mg = mg_find_mglob(TARG); |
| 2274 | if (mg) |
| 2275 | mg->mg_len = -1; |
| 2276 | } |
| 2277 | LEAVE_SCOPE(oldsave); |
| 2278 | if (gimme == G_ARRAY) |
| 2279 | RETURN; |
| 2280 | RETPUSHNO; |
| 2281 | } |
| 2282 | |
| 2283 | OP * |
| 2284 | Perl_do_readline(pTHX) |
| 2285 | { |
| 2286 | dSP; dTARGETSTACKED; |
| 2287 | SV *sv; |
| 2288 | STRLEN tmplen = 0; |
| 2289 | STRLEN offset; |
| 2290 | PerlIO *fp; |
| 2291 | IO * const io = GvIO(PL_last_in_gv); |
| 2292 | const I32 type = PL_op->op_type; |
| 2293 | const U8 gimme = GIMME_V; |
| 2294 | |
| 2295 | if (io) { |
| 2296 | const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar); |
| 2297 | if (mg) { |
| 2298 | Perl_tied_method(aTHX_ SV_CONST(READLINE), SP, MUTABLE_SV(io), mg, gimme, 0); |
| 2299 | if (gimme == G_SCALAR) { |
| 2300 | SPAGAIN; |
| 2301 | SvSetSV_nosteal(TARG, TOPs); |
| 2302 | SETTARG; |
| 2303 | } |
| 2304 | return NORMAL; |
| 2305 | } |
| 2306 | } |
| 2307 | fp = NULL; |
| 2308 | if (io) { |
| 2309 | fp = IoIFP(io); |
| 2310 | if (!fp) { |
| 2311 | if (IoFLAGS(io) & IOf_ARGV) { |
| 2312 | if (IoFLAGS(io) & IOf_START) { |
| 2313 | IoLINES(io) = 0; |
| 2314 | if (av_tindex(GvAVn(PL_last_in_gv)) < 0) { |
| 2315 | IoFLAGS(io) &= ~IOf_START; |
| 2316 | do_open6(PL_last_in_gv, "-", 1, NULL, NULL, 0); |
| 2317 | SvTAINTED_off(GvSVn(PL_last_in_gv)); /* previous tainting irrelevant */ |
| 2318 | sv_setpvs(GvSVn(PL_last_in_gv), "-"); |
| 2319 | SvSETMAGIC(GvSV(PL_last_in_gv)); |
| 2320 | fp = IoIFP(io); |
| 2321 | goto have_fp; |
| 2322 | } |
| 2323 | } |
| 2324 | fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL); |
| 2325 | if (!fp) { /* Note: fp != IoIFP(io) */ |
| 2326 | (void)do_close(PL_last_in_gv, FALSE); /* now it does*/ |
| 2327 | } |
| 2328 | } |
| 2329 | else if (type == OP_GLOB) |
| 2330 | fp = Perl_start_glob(aTHX_ POPs, io); |
| 2331 | } |
| 2332 | else if (type == OP_GLOB) |
| 2333 | SP--; |
| 2334 | else if (IoTYPE(io) == IoTYPE_WRONLY) { |
| 2335 | report_wrongway_fh(PL_last_in_gv, '>'); |
| 2336 | } |
| 2337 | } |
| 2338 | if (!fp) { |
| 2339 | if ((!io || !(IoFLAGS(io) & IOf_START)) |
| 2340 | && ckWARN(WARN_CLOSED) |
| 2341 | && type != OP_GLOB) |
| 2342 | { |
| 2343 | report_evil_fh(PL_last_in_gv); |
| 2344 | } |
| 2345 | if (gimme == G_SCALAR) { |
| 2346 | /* undef TARG, and push that undefined value */ |
| 2347 | if (type != OP_RCATLINE) { |
| 2348 | sv_set_undef(TARG); |
| 2349 | } |
| 2350 | PUSHTARG; |
| 2351 | } |
| 2352 | RETURN; |
| 2353 | } |
| 2354 | have_fp: |
| 2355 | if (gimme == G_SCALAR) { |
| 2356 | sv = TARG; |
| 2357 | if (type == OP_RCATLINE && SvGMAGICAL(sv)) |
| 2358 | mg_get(sv); |
| 2359 | if (SvROK(sv)) { |
| 2360 | if (type == OP_RCATLINE) |
| 2361 | SvPV_force_nomg_nolen(sv); |
| 2362 | else |
| 2363 | sv_unref(sv); |
| 2364 | } |
| 2365 | else if (isGV_with_GP(sv)) { |
| 2366 | SvPV_force_nomg_nolen(sv); |
| 2367 | } |
| 2368 | SvUPGRADE(sv, SVt_PV); |
| 2369 | tmplen = SvLEN(sv); /* remember if already alloced */ |
| 2370 | if (!tmplen && !SvREADONLY(sv) && !SvIsCOW(sv)) { |
| 2371 | /* try short-buffering it. Please update t/op/readline.t |
| 2372 | * if you change the growth length. |
| 2373 | */ |
| 2374 | Sv_Grow(sv, 80); |
| 2375 | } |
| 2376 | offset = 0; |
| 2377 | if (type == OP_RCATLINE && SvOK(sv)) { |
| 2378 | if (!SvPOK(sv)) { |
| 2379 | SvPV_force_nomg_nolen(sv); |
| 2380 | } |
| 2381 | offset = SvCUR(sv); |
| 2382 | } |
| 2383 | } |
| 2384 | else { |
| 2385 | sv = sv_2mortal(newSV(80)); |
| 2386 | offset = 0; |
| 2387 | } |
| 2388 | |
| 2389 | /* This should not be marked tainted if the fp is marked clean */ |
| 2390 | #define MAYBE_TAINT_LINE(io, sv) \ |
| 2391 | if (!(IoFLAGS(io) & IOf_UNTAINT)) { \ |
| 2392 | TAINT; \ |
| 2393 | SvTAINTED_on(sv); \ |
| 2394 | } |
| 2395 | |
| 2396 | /* delay EOF state for a snarfed empty file */ |
| 2397 | #define SNARF_EOF(gimme,rs,io,sv) \ |
| 2398 | (gimme != G_SCALAR || SvCUR(sv) \ |
| 2399 | || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs)) |
| 2400 | |
| 2401 | for (;;) { |
| 2402 | PUTBACK; |
| 2403 | if (!sv_gets(sv, fp, offset) |
| 2404 | && (type == OP_GLOB |
| 2405 | || SNARF_EOF(gimme, PL_rs, io, sv) |
| 2406 | || PerlIO_error(fp))) |
| 2407 | { |
| 2408 | PerlIO_clearerr(fp); |
| 2409 | if (IoFLAGS(io) & IOf_ARGV) { |
| 2410 | fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL); |
| 2411 | if (fp) |
| 2412 | continue; |
| 2413 | (void)do_close(PL_last_in_gv, FALSE); |
| 2414 | } |
| 2415 | else if (type == OP_GLOB) { |
| 2416 | if (!do_close(PL_last_in_gv, FALSE)) { |
| 2417 | Perl_ck_warner(aTHX_ packWARN(WARN_GLOB), |
| 2418 | "glob failed (child exited with status %d%s)", |
| 2419 | (int)(STATUS_CURRENT >> 8), |
| 2420 | (STATUS_CURRENT & 0x80) ? ", core dumped" : ""); |
| 2421 | } |
| 2422 | } |
| 2423 | if (gimme == G_SCALAR) { |
| 2424 | if (type != OP_RCATLINE) { |
| 2425 | SV_CHECK_THINKFIRST_COW_DROP(TARG); |
| 2426 | SvOK_off(TARG); |
| 2427 | } |
| 2428 | SPAGAIN; |
| 2429 | PUSHTARG; |
| 2430 | } |
| 2431 | MAYBE_TAINT_LINE(io, sv); |
| 2432 | RETURN; |
| 2433 | } |
| 2434 | MAYBE_TAINT_LINE(io, sv); |
| 2435 | IoLINES(io)++; |
| 2436 | IoFLAGS(io) |= IOf_NOLINE; |
| 2437 | SvSETMAGIC(sv); |
| 2438 | SPAGAIN; |
| 2439 | XPUSHs(sv); |
| 2440 | if (type == OP_GLOB) { |
| 2441 | const char *t1; |
| 2442 | Stat_t statbuf; |
| 2443 | |
| 2444 | if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) { |
| 2445 | char * const tmps = SvEND(sv) - 1; |
| 2446 | if (*tmps == *SvPVX_const(PL_rs)) { |
| 2447 | *tmps = '\0'; |
| 2448 | SvCUR_set(sv, SvCUR(sv) - 1); |
| 2449 | } |
| 2450 | } |
| 2451 | for (t1 = SvPVX_const(sv); *t1; t1++) |
| 2452 | #ifdef __VMS |
| 2453 | if (strchr("*%?", *t1)) |
| 2454 | #else |
| 2455 | if (strchr("$&*(){}[]'\";\\|?<>~`", *t1)) |
| 2456 | #endif |
| 2457 | break; |
| 2458 | if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &statbuf) < 0) { |
| 2459 | (void)POPs; /* Unmatched wildcard? Chuck it... */ |
| 2460 | continue; |
| 2461 | } |
| 2462 | } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */ |
| 2463 | if (ckWARN(WARN_UTF8)) { |
| 2464 | const U8 * const s = (const U8*)SvPVX_const(sv) + offset; |
| 2465 | const STRLEN len = SvCUR(sv) - offset; |
| 2466 | const U8 *f; |
| 2467 | |
| 2468 | if (!is_utf8_string_loc(s, len, &f)) |
| 2469 | /* Emulate :encoding(utf8) warning in the same case. */ |
| 2470 | Perl_warner(aTHX_ packWARN(WARN_UTF8), |
| 2471 | "utf8 \"\\x%02X\" does not map to Unicode", |
| 2472 | f < (U8*)SvEND(sv) ? *f : 0); |
| 2473 | } |
| 2474 | } |
| 2475 | if (gimme == G_ARRAY) { |
| 2476 | if (SvLEN(sv) - SvCUR(sv) > 20) { |
| 2477 | SvPV_shrink_to_cur(sv); |
| 2478 | } |
| 2479 | sv = sv_2mortal(newSV(80)); |
| 2480 | continue; |
| 2481 | } |
| 2482 | else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) { |
| 2483 | /* try to reclaim a bit of scalar space (only on 1st alloc) */ |
| 2484 | const STRLEN new_len |
| 2485 | = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */ |
| 2486 | SvPV_renew(sv, new_len); |
| 2487 | } |
| 2488 | RETURN; |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | PP(pp_helem) |
| 2493 | { |
| 2494 | dSP; |
| 2495 | HE* he; |
| 2496 | SV **svp; |
| 2497 | SV * const keysv = POPs; |
| 2498 | HV * const hv = MUTABLE_HV(POPs); |
| 2499 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
| 2500 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; |
| 2501 | SV *sv; |
| 2502 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; |
| 2503 | bool preeminent = TRUE; |
| 2504 | |
| 2505 | if (SvTYPE(hv) != SVt_PVHV) |
| 2506 | RETPUSHUNDEF; |
| 2507 | |
| 2508 | if (localizing) { |
| 2509 | MAGIC *mg; |
| 2510 | HV *stash; |
| 2511 | |
| 2512 | /* If we can determine whether the element exist, |
| 2513 | * Try to preserve the existenceness of a tied hash |
| 2514 | * element by using EXISTS and DELETE if possible. |
| 2515 | * Fallback to FETCH and STORE otherwise. */ |
| 2516 | if (SvCANEXISTDELETE(hv)) |
| 2517 | preeminent = hv_exists_ent(hv, keysv, 0); |
| 2518 | } |
| 2519 | |
| 2520 | he = hv_fetch_ent(hv, keysv, lval && !defer, 0); |
| 2521 | svp = he ? &HeVAL(he) : NULL; |
| 2522 | if (lval) { |
| 2523 | if (!svp || !*svp || *svp == &PL_sv_undef) { |
| 2524 | SV* lv; |
| 2525 | SV* key2; |
| 2526 | if (!defer) { |
| 2527 | DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv)); |
| 2528 | } |
| 2529 | lv = sv_newmortal(); |
| 2530 | sv_upgrade(lv, SVt_PVLV); |
| 2531 | LvTYPE(lv) = 'y'; |
| 2532 | sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0); |
| 2533 | SvREFCNT_dec_NN(key2); /* sv_magic() increments refcount */ |
| 2534 | LvTARG(lv) = SvREFCNT_inc_simple_NN(hv); |
| 2535 | LvTARGLEN(lv) = 1; |
| 2536 | PUSHs(lv); |
| 2537 | RETURN; |
| 2538 | } |
| 2539 | if (localizing) { |
| 2540 | if (HvNAME_get(hv) && isGV(*svp)) |
| 2541 | save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL)); |
| 2542 | else if (preeminent) |
| 2543 | save_helem_flags(hv, keysv, svp, |
| 2544 | (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC); |
| 2545 | else |
| 2546 | SAVEHDELETE(hv, keysv); |
| 2547 | } |
| 2548 | else if (PL_op->op_private & OPpDEREF) { |
| 2549 | PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF)); |
| 2550 | RETURN; |
| 2551 | } |
| 2552 | } |
| 2553 | sv = (svp && *svp ? *svp : &PL_sv_undef); |
| 2554 | /* Originally this did a conditional C<sv = sv_mortalcopy(sv)>; this |
| 2555 | * was to make C<local $tied{foo} = $tied{foo}> possible. |
| 2556 | * However, it seems no longer to be needed for that purpose, and |
| 2557 | * introduced a new bug: stuff like C<while ($hash{taintedval} =~ /.../g> |
| 2558 | * would loop endlessly since the pos magic is getting set on the |
| 2559 | * mortal copy and lost. However, the copy has the effect of |
| 2560 | * triggering the get magic, and losing it altogether made things like |
| 2561 | * c<$tied{foo};> in void context no longer do get magic, which some |
| 2562 | * code relied on. Also, delayed triggering of magic on @+ and friends |
| 2563 | * meant the original regex may be out of scope by now. So as a |
| 2564 | * compromise, do the get magic here. (The MGf_GSKIP flag will stop it |
| 2565 | * being called too many times). */ |
| 2566 | if (!lval && SvRMAGICAL(hv) && SvGMAGICAL(sv)) |
| 2567 | mg_get(sv); |
| 2568 | PUSHs(sv); |
| 2569 | RETURN; |
| 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | /* a stripped-down version of Perl_softref2xv() for use by |
| 2574 | * pp_multideref(), which doesn't use PL_op->op_flags */ |
| 2575 | |
| 2576 | STATIC GV * |
| 2577 | S_softref2xv_lite(pTHX_ SV *const sv, const char *const what, |
| 2578 | const svtype type) |
| 2579 | { |
| 2580 | if (PL_op->op_private & HINT_STRICT_REFS) { |
| 2581 | if (SvOK(sv)) |
| 2582 | Perl_die(aTHX_ PL_no_symref_sv, sv, |
| 2583 | (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what); |
| 2584 | else |
| 2585 | Perl_die(aTHX_ PL_no_usym, what); |
| 2586 | } |
| 2587 | if (!SvOK(sv)) |
| 2588 | Perl_die(aTHX_ PL_no_usym, what); |
| 2589 | return gv_fetchsv_nomg(sv, GV_ADD, type); |
| 2590 | } |
| 2591 | |
| 2592 | |
| 2593 | /* Handle one or more aggregate derefs and array/hash indexings, e.g. |
| 2594 | * $h->{foo} or $a[0]{$key}[$i] or f()->[1] |
| 2595 | * |
| 2596 | * op_aux points to an array of unions of UV / IV / SV* / PADOFFSET. |
| 2597 | * Each of these either contains a set of actions, or an argument, such as |
| 2598 | * an IV to use as an array index, or a lexical var to retrieve. |
| 2599 | * Several actions re stored per UV; we keep shifting new actions off the |
| 2600 | * one UV, and only reload when it becomes zero. |
| 2601 | */ |
| 2602 | |
| 2603 | PP(pp_multideref) |
| 2604 | { |
| 2605 | SV *sv = NULL; /* init to avoid spurious 'may be used uninitialized' */ |
| 2606 | UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux; |
| 2607 | UV actions = items->uv; |
| 2608 | |
| 2609 | assert(actions); |
| 2610 | /* this tells find_uninit_var() where we're up to */ |
| 2611 | PL_multideref_pc = items; |
| 2612 | |
| 2613 | while (1) { |
| 2614 | /* there are three main classes of action; the first retrieve |
| 2615 | * the initial AV or HV from a variable or the stack; the second |
| 2616 | * does the equivalent of an unrolled (/DREFAV, rv2av, aelem), |
| 2617 | * the third an unrolled (/DREFHV, rv2hv, helem). |
| 2618 | */ |
| 2619 | switch (actions & MDEREF_ACTION_MASK) { |
| 2620 | |
| 2621 | case MDEREF_reload: |
| 2622 | actions = (++items)->uv; |
| 2623 | continue; |
| 2624 | |
| 2625 | case MDEREF_AV_padav_aelem: /* $lex[...] */ |
| 2626 | sv = PAD_SVl((++items)->pad_offset); |
| 2627 | goto do_AV_aelem; |
| 2628 | |
| 2629 | case MDEREF_AV_gvav_aelem: /* $pkg[...] */ |
| 2630 | sv = UNOP_AUX_item_sv(++items); |
| 2631 | assert(isGV_with_GP(sv)); |
| 2632 | sv = (SV*)GvAVn((GV*)sv); |
| 2633 | goto do_AV_aelem; |
| 2634 | |
| 2635 | case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */ |
| 2636 | { |
| 2637 | dSP; |
| 2638 | sv = POPs; |
| 2639 | PUTBACK; |
| 2640 | goto do_AV_rv2av_aelem; |
| 2641 | } |
| 2642 | |
| 2643 | case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */ |
| 2644 | sv = UNOP_AUX_item_sv(++items); |
| 2645 | assert(isGV_with_GP(sv)); |
| 2646 | sv = GvSVn((GV*)sv); |
| 2647 | goto do_AV_vivify_rv2av_aelem; |
| 2648 | |
| 2649 | case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */ |
| 2650 | sv = PAD_SVl((++items)->pad_offset); |
| 2651 | /* FALLTHROUGH */ |
| 2652 | |
| 2653 | do_AV_vivify_rv2av_aelem: |
| 2654 | case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */ |
| 2655 | /* this is the OPpDEREF action normally found at the end of |
| 2656 | * ops like aelem, helem, rv2sv */ |
| 2657 | sv = vivify_ref(sv, OPpDEREF_AV); |
| 2658 | /* FALLTHROUGH */ |
| 2659 | |
| 2660 | do_AV_rv2av_aelem: |
| 2661 | /* this is basically a copy of pp_rv2av when it just has the |
| 2662 | * sKR/1 flags */ |
| 2663 | SvGETMAGIC(sv); |
| 2664 | if (LIKELY(SvROK(sv))) { |
| 2665 | if (UNLIKELY(SvAMAGIC(sv))) { |
| 2666 | sv = amagic_deref_call(sv, to_av_amg); |
| 2667 | } |
| 2668 | sv = SvRV(sv); |
| 2669 | if (UNLIKELY(SvTYPE(sv) != SVt_PVAV)) |
| 2670 | DIE(aTHX_ "Not an ARRAY reference"); |
| 2671 | } |
| 2672 | else if (SvTYPE(sv) != SVt_PVAV) { |
| 2673 | if (!isGV_with_GP(sv)) |
| 2674 | sv = (SV*)S_softref2xv_lite(aTHX_ sv, "an ARRAY", SVt_PVAV); |
| 2675 | sv = MUTABLE_SV(GvAVn((GV*)sv)); |
| 2676 | } |
| 2677 | /* FALLTHROUGH */ |
| 2678 | |
| 2679 | do_AV_aelem: |
| 2680 | { |
| 2681 | /* retrieve the key; this may be either a lexical or package |
| 2682 | * var (whose index/ptr is stored as an item) or a signed |
| 2683 | * integer constant stored as an item. |
| 2684 | */ |
| 2685 | SV *elemsv; |
| 2686 | IV elem = 0; /* to shut up stupid compiler warnings */ |
| 2687 | |
| 2688 | |
| 2689 | assert(SvTYPE(sv) == SVt_PVAV); |
| 2690 | |
| 2691 | switch (actions & MDEREF_INDEX_MASK) { |
| 2692 | case MDEREF_INDEX_none: |
| 2693 | goto finish; |
| 2694 | case MDEREF_INDEX_const: |
| 2695 | elem = (++items)->iv; |
| 2696 | break; |
| 2697 | case MDEREF_INDEX_padsv: |
| 2698 | elemsv = PAD_SVl((++items)->pad_offset); |
| 2699 | goto check_elem; |
| 2700 | case MDEREF_INDEX_gvsv: |
| 2701 | elemsv = UNOP_AUX_item_sv(++items); |
| 2702 | assert(isGV_with_GP(elemsv)); |
| 2703 | elemsv = GvSVn((GV*)elemsv); |
| 2704 | check_elem: |
| 2705 | if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv) |
| 2706 | && ckWARN(WARN_MISC))) |
| 2707 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
| 2708 | "Use of reference \"%" SVf "\" as array index", |
| 2709 | SVfARG(elemsv)); |
| 2710 | /* the only time that S_find_uninit_var() needs this |
| 2711 | * is to determine which index value triggered the |
| 2712 | * undef warning. So just update it here. Note that |
| 2713 | * since we don't save and restore this var (e.g. for |
| 2714 | * tie or overload execution), its value will be |
| 2715 | * meaningless apart from just here */ |
| 2716 | PL_multideref_pc = items; |
| 2717 | elem = SvIV(elemsv); |
| 2718 | break; |
| 2719 | } |
| 2720 | |
| 2721 | |
| 2722 | /* this is basically a copy of pp_aelem with OPpDEREF skipped */ |
| 2723 | |
| 2724 | if (!(actions & MDEREF_FLAG_last)) { |
| 2725 | SV** svp = av_fetch((AV*)sv, elem, 1); |
| 2726 | if (!svp || ! (sv=*svp)) |
| 2727 | DIE(aTHX_ PL_no_aelem, elem); |
| 2728 | break; |
| 2729 | } |
| 2730 | |
| 2731 | if (PL_op->op_private & |
| 2732 | (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)) |
| 2733 | { |
| 2734 | if (PL_op->op_private & OPpMULTIDEREF_EXISTS) { |
| 2735 | sv = av_exists((AV*)sv, elem) ? &PL_sv_yes : &PL_sv_no; |
| 2736 | } |
| 2737 | else { |
| 2738 | I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0; |
| 2739 | sv = av_delete((AV*)sv, elem, discard); |
| 2740 | if (discard) |
| 2741 | return NORMAL; |
| 2742 | if (!sv) |
| 2743 | sv = &PL_sv_undef; |
| 2744 | } |
| 2745 | } |
| 2746 | else { |
| 2747 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
| 2748 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; |
| 2749 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; |
| 2750 | bool preeminent = TRUE; |
| 2751 | AV *const av = (AV*)sv; |
| 2752 | SV** svp; |
| 2753 | |
| 2754 | if (UNLIKELY(localizing)) { |
| 2755 | MAGIC *mg; |
| 2756 | HV *stash; |
| 2757 | |
| 2758 | /* If we can determine whether the element exist, |
| 2759 | * Try to preserve the existenceness of a tied array |
| 2760 | * element by using EXISTS and DELETE if possible. |
| 2761 | * Fallback to FETCH and STORE otherwise. */ |
| 2762 | if (SvCANEXISTDELETE(av)) |
| 2763 | preeminent = av_exists(av, elem); |
| 2764 | } |
| 2765 | |
| 2766 | svp = av_fetch(av, elem, lval && !defer); |
| 2767 | |
| 2768 | if (lval) { |
| 2769 | if (!svp || !(sv = *svp)) { |
| 2770 | IV len; |
| 2771 | if (!defer) |
| 2772 | DIE(aTHX_ PL_no_aelem, elem); |
| 2773 | len = av_tindex(av); |
| 2774 | sv = sv_2mortal(newSVavdefelem(av, |
| 2775 | /* Resolve a negative index now, unless it points |
| 2776 | * before the beginning of the array, in which |
| 2777 | * case record it for error reporting in |
| 2778 | * magic_setdefelem. */ |
| 2779 | elem < 0 && len + elem >= 0 |
| 2780 | ? len + elem : elem, 1)); |
| 2781 | } |
| 2782 | else { |
| 2783 | if (UNLIKELY(localizing)) { |
| 2784 | if (preeminent) { |
| 2785 | save_aelem(av, elem, svp); |
| 2786 | sv = *svp; /* may have changed */ |
| 2787 | } |
| 2788 | else |
| 2789 | SAVEADELETE(av, elem); |
| 2790 | } |
| 2791 | } |
| 2792 | } |
| 2793 | else { |
| 2794 | sv = (svp ? *svp : &PL_sv_undef); |
| 2795 | /* see note in pp_helem() */ |
| 2796 | if (SvRMAGICAL(av) && SvGMAGICAL(sv)) |
| 2797 | mg_get(sv); |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | } |
| 2802 | finish: |
| 2803 | { |
| 2804 | dSP; |
| 2805 | XPUSHs(sv); |
| 2806 | RETURN; |
| 2807 | } |
| 2808 | /* NOTREACHED */ |
| 2809 | |
| 2810 | |
| 2811 | |
| 2812 | |
| 2813 | case MDEREF_HV_padhv_helem: /* $lex{...} */ |
| 2814 | sv = PAD_SVl((++items)->pad_offset); |
| 2815 | goto do_HV_helem; |
| 2816 | |
| 2817 | case MDEREF_HV_gvhv_helem: /* $pkg{...} */ |
| 2818 | sv = UNOP_AUX_item_sv(++items); |
| 2819 | assert(isGV_with_GP(sv)); |
| 2820 | sv = (SV*)GvHVn((GV*)sv); |
| 2821 | goto do_HV_helem; |
| 2822 | |
| 2823 | case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */ |
| 2824 | { |
| 2825 | dSP; |
| 2826 | sv = POPs; |
| 2827 | PUTBACK; |
| 2828 | goto do_HV_rv2hv_helem; |
| 2829 | } |
| 2830 | |
| 2831 | case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */ |
| 2832 | sv = UNOP_AUX_item_sv(++items); |
| 2833 | assert(isGV_with_GP(sv)); |
| 2834 | sv = GvSVn((GV*)sv); |
| 2835 | goto do_HV_vivify_rv2hv_helem; |
| 2836 | |
| 2837 | case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */ |
| 2838 | sv = PAD_SVl((++items)->pad_offset); |
| 2839 | /* FALLTHROUGH */ |
| 2840 | |
| 2841 | do_HV_vivify_rv2hv_helem: |
| 2842 | case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */ |
| 2843 | /* this is the OPpDEREF action normally found at the end of |
| 2844 | * ops like aelem, helem, rv2sv */ |
| 2845 | sv = vivify_ref(sv, OPpDEREF_HV); |
| 2846 | /* FALLTHROUGH */ |
| 2847 | |
| 2848 | do_HV_rv2hv_helem: |
| 2849 | /* this is basically a copy of pp_rv2hv when it just has the |
| 2850 | * sKR/1 flags (and pp_rv2hv is aliased to pp_rv2av) */ |
| 2851 | |
| 2852 | SvGETMAGIC(sv); |
| 2853 | if (LIKELY(SvROK(sv))) { |
| 2854 | if (UNLIKELY(SvAMAGIC(sv))) { |
| 2855 | sv = amagic_deref_call(sv, to_hv_amg); |
| 2856 | } |
| 2857 | sv = SvRV(sv); |
| 2858 | if (UNLIKELY(SvTYPE(sv) != SVt_PVHV)) |
| 2859 | DIE(aTHX_ "Not a HASH reference"); |
| 2860 | } |
| 2861 | else if (SvTYPE(sv) != SVt_PVHV) { |
| 2862 | if (!isGV_with_GP(sv)) |
| 2863 | sv = (SV*)S_softref2xv_lite(aTHX_ sv, "a HASH", SVt_PVHV); |
| 2864 | sv = MUTABLE_SV(GvHVn((GV*)sv)); |
| 2865 | } |
| 2866 | /* FALLTHROUGH */ |
| 2867 | |
| 2868 | do_HV_helem: |
| 2869 | { |
| 2870 | /* retrieve the key; this may be either a lexical / package |
| 2871 | * var or a string constant, whose index/ptr is stored as an |
| 2872 | * item |
| 2873 | */ |
| 2874 | SV *keysv = NULL; /* to shut up stupid compiler warnings */ |
| 2875 | |
| 2876 | assert(SvTYPE(sv) == SVt_PVHV); |
| 2877 | |
| 2878 | switch (actions & MDEREF_INDEX_MASK) { |
| 2879 | case MDEREF_INDEX_none: |
| 2880 | goto finish; |
| 2881 | |
| 2882 | case MDEREF_INDEX_const: |
| 2883 | keysv = UNOP_AUX_item_sv(++items); |
| 2884 | break; |
| 2885 | |
| 2886 | case MDEREF_INDEX_padsv: |
| 2887 | keysv = PAD_SVl((++items)->pad_offset); |
| 2888 | break; |
| 2889 | |
| 2890 | case MDEREF_INDEX_gvsv: |
| 2891 | keysv = UNOP_AUX_item_sv(++items); |
| 2892 | keysv = GvSVn((GV*)keysv); |
| 2893 | break; |
| 2894 | } |
| 2895 | |
| 2896 | /* see comment above about setting this var */ |
| 2897 | PL_multideref_pc = items; |
| 2898 | |
| 2899 | |
| 2900 | /* ensure that candidate CONSTs have been HEKified */ |
| 2901 | assert( ((actions & MDEREF_INDEX_MASK) != MDEREF_INDEX_const) |
| 2902 | || SvTYPE(keysv) >= SVt_PVMG |
| 2903 | || !SvOK(keysv) |
| 2904 | || SvROK(keysv) |
| 2905 | || SvIsCOW_shared_hash(keysv)); |
| 2906 | |
| 2907 | /* this is basically a copy of pp_helem with OPpDEREF skipped */ |
| 2908 | |
| 2909 | if (!(actions & MDEREF_FLAG_last)) { |
| 2910 | HE *he = hv_fetch_ent((HV*)sv, keysv, 1, 0); |
| 2911 | if (!he || !(sv=HeVAL(he)) || sv == &PL_sv_undef) |
| 2912 | DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv)); |
| 2913 | break; |
| 2914 | } |
| 2915 | |
| 2916 | if (PL_op->op_private & |
| 2917 | (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)) |
| 2918 | { |
| 2919 | if (PL_op->op_private & OPpMULTIDEREF_EXISTS) { |
| 2920 | sv = hv_exists_ent((HV*)sv, keysv, 0) |
| 2921 | ? &PL_sv_yes : &PL_sv_no; |
| 2922 | } |
| 2923 | else { |
| 2924 | I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0; |
| 2925 | sv = hv_delete_ent((HV*)sv, keysv, discard, 0); |
| 2926 | if (discard) |
| 2927 | return NORMAL; |
| 2928 | if (!sv) |
| 2929 | sv = &PL_sv_undef; |
| 2930 | } |
| 2931 | } |
| 2932 | else { |
| 2933 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
| 2934 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; |
| 2935 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; |
| 2936 | bool preeminent = TRUE; |
| 2937 | SV **svp; |
| 2938 | HV * const hv = (HV*)sv; |
| 2939 | HE* he; |
| 2940 | |
| 2941 | if (UNLIKELY(localizing)) { |
| 2942 | MAGIC *mg; |
| 2943 | HV *stash; |
| 2944 | |
| 2945 | /* If we can determine whether the element exist, |
| 2946 | * Try to preserve the existenceness of a tied hash |
| 2947 | * element by using EXISTS and DELETE if possible. |
| 2948 | * Fallback to FETCH and STORE otherwise. */ |
| 2949 | if (SvCANEXISTDELETE(hv)) |
| 2950 | preeminent = hv_exists_ent(hv, keysv, 0); |
| 2951 | } |
| 2952 | |
| 2953 | he = hv_fetch_ent(hv, keysv, lval && !defer, 0); |
| 2954 | svp = he ? &HeVAL(he) : NULL; |
| 2955 | |
| 2956 | |
| 2957 | if (lval) { |
| 2958 | if (!svp || !(sv = *svp) || sv == &PL_sv_undef) { |
| 2959 | SV* lv; |
| 2960 | SV* key2; |
| 2961 | if (!defer) |
| 2962 | DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv)); |
| 2963 | lv = sv_newmortal(); |
| 2964 | sv_upgrade(lv, SVt_PVLV); |
| 2965 | LvTYPE(lv) = 'y'; |
| 2966 | sv_magic(lv, key2 = newSVsv(keysv), |
| 2967 | PERL_MAGIC_defelem, NULL, 0); |
| 2968 | /* sv_magic() increments refcount */ |
| 2969 | SvREFCNT_dec_NN(key2); |
| 2970 | LvTARG(lv) = SvREFCNT_inc_simple_NN(hv); |
| 2971 | LvTARGLEN(lv) = 1; |
| 2972 | sv = lv; |
| 2973 | } |
| 2974 | else { |
| 2975 | if (localizing) { |
| 2976 | if (HvNAME_get(hv) && isGV(sv)) |
| 2977 | save_gp(MUTABLE_GV(sv), |
| 2978 | !(PL_op->op_flags & OPf_SPECIAL)); |
| 2979 | else if (preeminent) { |
| 2980 | save_helem_flags(hv, keysv, svp, |
| 2981 | (PL_op->op_flags & OPf_SPECIAL) |
| 2982 | ? 0 : SAVEf_SETMAGIC); |
| 2983 | sv = *svp; /* may have changed */ |
| 2984 | } |
| 2985 | else |
| 2986 | SAVEHDELETE(hv, keysv); |
| 2987 | } |
| 2988 | } |
| 2989 | } |
| 2990 | else { |
| 2991 | sv = (svp && *svp ? *svp : &PL_sv_undef); |
| 2992 | /* see note in pp_helem() */ |
| 2993 | if (SvRMAGICAL(hv) && SvGMAGICAL(sv)) |
| 2994 | mg_get(sv); |
| 2995 | } |
| 2996 | } |
| 2997 | goto finish; |
| 2998 | } |
| 2999 | |
| 3000 | } /* switch */ |
| 3001 | |
| 3002 | actions >>= MDEREF_SHIFT; |
| 3003 | } /* while */ |
| 3004 | /* NOTREACHED */ |
| 3005 | } |
| 3006 | |
| 3007 | |
| 3008 | PP(pp_iter) |
| 3009 | { |
| 3010 | PERL_CONTEXT *cx; |
| 3011 | SV *oldsv; |
| 3012 | SV **itersvp; |
| 3013 | |
| 3014 | SV *sv; |
| 3015 | AV *av; |
| 3016 | IV ix; |
| 3017 | IV inc; |
| 3018 | |
| 3019 | cx = CX_CUR(); |
| 3020 | itersvp = CxITERVAR(cx); |
| 3021 | assert(itersvp); |
| 3022 | |
| 3023 | switch (CxTYPE(cx)) { |
| 3024 | |
| 3025 | case CXt_LOOP_LAZYSV: /* string increment */ |
| 3026 | { |
| 3027 | SV* cur = cx->blk_loop.state_u.lazysv.cur; |
| 3028 | SV *end = cx->blk_loop.state_u.lazysv.end; |
| 3029 | /* If the maximum is !SvOK(), pp_enteriter substitutes PL_sv_no. |
| 3030 | It has SvPVX of "" and SvCUR of 0, which is what we want. */ |
| 3031 | STRLEN maxlen = 0; |
| 3032 | const char *max = SvPV_const(end, maxlen); |
| 3033 | if (DO_UTF8(end) && IN_UNI_8_BIT) |
| 3034 | maxlen = sv_len_utf8_nomg(end); |
| 3035 | if (UNLIKELY(SvNIOK(cur) || SvCUR(cur) > maxlen)) |
| 3036 | goto retno; |
| 3037 | |
| 3038 | oldsv = *itersvp; |
| 3039 | /* NB: on the first iteration, oldsv will have a ref count of at |
| 3040 | * least 2 (one extra from blk_loop.itersave), so the GV or pad |
| 3041 | * slot will get localised; on subsequent iterations the RC==1 |
| 3042 | * optimisation may kick in and the SV will be reused. */ |
| 3043 | if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) { |
| 3044 | /* safe to reuse old SV */ |
| 3045 | sv_setsv(oldsv, cur); |
| 3046 | } |
| 3047 | else |
| 3048 | { |
| 3049 | /* we need a fresh SV every time so that loop body sees a |
| 3050 | * completely new SV for closures/references to work as |
| 3051 | * they used to */ |
| 3052 | *itersvp = newSVsv(cur); |
| 3053 | SvREFCNT_dec(oldsv); |
| 3054 | } |
| 3055 | if (strEQ(SvPVX_const(cur), max)) |
| 3056 | sv_setiv(cur, 0); /* terminate next time */ |
| 3057 | else |
| 3058 | sv_inc(cur); |
| 3059 | break; |
| 3060 | } |
| 3061 | |
| 3062 | case CXt_LOOP_LAZYIV: /* integer increment */ |
| 3063 | { |
| 3064 | IV cur = cx->blk_loop.state_u.lazyiv.cur; |
| 3065 | if (UNLIKELY(cur > cx->blk_loop.state_u.lazyiv.end)) |
| 3066 | goto retno; |
| 3067 | |
| 3068 | oldsv = *itersvp; |
| 3069 | /* see NB comment above */ |
| 3070 | if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) { |
| 3071 | /* safe to reuse old SV */ |
| 3072 | |
| 3073 | if ( (SvFLAGS(oldsv) & (SVTYPEMASK|SVf_THINKFIRST|SVf_IVisUV)) |
| 3074 | == SVt_IV) |
| 3075 | { |
| 3076 | /* Cheap SvIOK_only(). |
| 3077 | * Assert that flags which SvIOK_only() would test or |
| 3078 | * clear can't be set, because we're SVt_IV */ |
| 3079 | assert(!(SvFLAGS(oldsv) & |
| 3080 | (SVf_OOK|SVf_UTF8|(SVf_OK & ~(SVf_IOK|SVp_IOK))))); |
| 3081 | SvFLAGS(oldsv) |= (SVf_IOK|SVp_IOK); |
| 3082 | /* SvIV_set() where sv_any points to head */ |
| 3083 | oldsv->sv_u.svu_iv = cur; |
| 3084 | |
| 3085 | } |
| 3086 | else |
| 3087 | sv_setiv(oldsv, cur); |
| 3088 | } |
| 3089 | else |
| 3090 | { |
| 3091 | /* we need a fresh SV every time so that loop body sees a |
| 3092 | * completely new SV for closures/references to work as they |
| 3093 | * used to */ |
| 3094 | *itersvp = newSViv(cur); |
| 3095 | SvREFCNT_dec(oldsv); |
| 3096 | } |
| 3097 | |
| 3098 | if (UNLIKELY(cur == IV_MAX)) { |
| 3099 | /* Handle end of range at IV_MAX */ |
| 3100 | cx->blk_loop.state_u.lazyiv.end = IV_MIN; |
| 3101 | } else |
| 3102 | ++cx->blk_loop.state_u.lazyiv.cur; |
| 3103 | break; |
| 3104 | } |
| 3105 | |
| 3106 | case CXt_LOOP_LIST: /* for (1,2,3) */ |
| 3107 | |
| 3108 | assert(OPpITER_REVERSED == 2); /* so inc becomes -1 or 1 */ |
| 3109 | inc = 1 - (PL_op->op_private & OPpITER_REVERSED); |
| 3110 | ix = (cx->blk_loop.state_u.stack.ix += inc); |
| 3111 | if (UNLIKELY(inc > 0 |
| 3112 | ? ix > cx->blk_oldsp |
| 3113 | : ix <= cx->blk_loop.state_u.stack.basesp) |
| 3114 | ) |
| 3115 | goto retno; |
| 3116 | |
| 3117 | sv = PL_stack_base[ix]; |
| 3118 | av = NULL; |
| 3119 | goto loop_ary_common; |
| 3120 | |
| 3121 | case CXt_LOOP_ARY: /* for (@ary) */ |
| 3122 | |
| 3123 | av = cx->blk_loop.state_u.ary.ary; |
| 3124 | inc = 1 - (PL_op->op_private & OPpITER_REVERSED); |
| 3125 | ix = (cx->blk_loop.state_u.ary.ix += inc); |
| 3126 | if (UNLIKELY(inc > 0 |
| 3127 | ? ix > AvFILL(av) |
| 3128 | : ix < 0) |
| 3129 | ) |
| 3130 | goto retno; |
| 3131 | |
| 3132 | if (UNLIKELY(SvRMAGICAL(av))) { |
| 3133 | SV * const * const svp = av_fetch(av, ix, FALSE); |
| 3134 | sv = svp ? *svp : NULL; |
| 3135 | } |
| 3136 | else { |
| 3137 | sv = AvARRAY(av)[ix]; |
| 3138 | } |
| 3139 | |
| 3140 | loop_ary_common: |
| 3141 | |
| 3142 | if (UNLIKELY(cx->cx_type & CXp_FOR_LVREF)) { |
| 3143 | SvSetMagicSV(*itersvp, sv); |
| 3144 | break; |
| 3145 | } |
| 3146 | |
| 3147 | if (LIKELY(sv)) { |
| 3148 | if (UNLIKELY(SvIS_FREED(sv))) { |
| 3149 | *itersvp = NULL; |
| 3150 | Perl_croak(aTHX_ "Use of freed value in iteration"); |
| 3151 | } |
| 3152 | if (SvPADTMP(sv)) { |
| 3153 | sv = newSVsv(sv); |
| 3154 | } |
| 3155 | else { |
| 3156 | SvTEMP_off(sv); |
| 3157 | SvREFCNT_inc_simple_void_NN(sv); |
| 3158 | } |
| 3159 | } |
| 3160 | else if (av) { |
| 3161 | sv = newSVavdefelem(av, ix, 0); |
| 3162 | } |
| 3163 | else |
| 3164 | sv = &PL_sv_undef; |
| 3165 | |
| 3166 | oldsv = *itersvp; |
| 3167 | *itersvp = sv; |
| 3168 | SvREFCNT_dec(oldsv); |
| 3169 | break; |
| 3170 | |
| 3171 | default: |
| 3172 | DIE(aTHX_ "panic: pp_iter, type=%u", CxTYPE(cx)); |
| 3173 | } |
| 3174 | |
| 3175 | /* Bypass pushing &PL_sv_yes and calling pp_and(); instead |
| 3176 | * jump straight to the AND op's op_other */ |
| 3177 | assert(PL_op->op_next->op_type == OP_AND); |
| 3178 | assert(PL_op->op_next->op_ppaddr == Perl_pp_and); |
| 3179 | return cLOGOPx(PL_op->op_next)->op_other; |
| 3180 | |
| 3181 | retno: |
| 3182 | /* Bypass pushing &PL_sv_no and calling pp_and(); instead |
| 3183 | * jump straight to the AND op's op_next */ |
| 3184 | assert(PL_op->op_next->op_type == OP_AND); |
| 3185 | assert(PL_op->op_next->op_ppaddr == Perl_pp_and); |
| 3186 | /* pp_enteriter should have pre-extended the stack */ |
| 3187 | EXTEND_SKIP(PL_stack_sp, 1); |
| 3188 | /* we only need this for the rare case where the OP_AND isn't |
| 3189 | * in void context, e.g. $x = do { for (..) {...} }; |
| 3190 | * but its cheaper to just push it rather than testing first |
| 3191 | */ |
| 3192 | *++PL_stack_sp = &PL_sv_no; |
| 3193 | return PL_op->op_next->op_next; |
| 3194 | } |
| 3195 | |
| 3196 | |
| 3197 | /* |
| 3198 | A description of how taint works in pattern matching and substitution. |
| 3199 | |
| 3200 | This is all conditional on NO_TAINT_SUPPORT not being defined. Under |
| 3201 | NO_TAINT_SUPPORT, taint-related operations should become no-ops. |
| 3202 | |
| 3203 | While the pattern is being assembled/concatenated and then compiled, |
| 3204 | PL_tainted will get set (via TAINT_set) if any component of the pattern |
| 3205 | is tainted, e.g. /.*$tainted/. At the end of pattern compilation, |
| 3206 | the RXf_TAINTED flag is set on the pattern if PL_tainted is set (via |
| 3207 | TAINT_get). It will also be set if any component of the pattern matches |
| 3208 | based on locale-dependent behavior. |
| 3209 | |
| 3210 | When the pattern is copied, e.g. $r = qr/..../, the SV holding the ref to |
| 3211 | the pattern is marked as tainted. This means that subsequent usage, such |
| 3212 | as /x$r/, will set PL_tainted using TAINT_set, and thus RXf_TAINTED, |
| 3213 | on the new pattern too. |
| 3214 | |
| 3215 | RXf_TAINTED_SEEN is used post-execution by the get magic code |
| 3216 | of $1 et al to indicate whether the returned value should be tainted. |
| 3217 | It is the responsibility of the caller of the pattern (i.e. pp_match, |
| 3218 | pp_subst etc) to set this flag for any other circumstances where $1 needs |
| 3219 | to be tainted. |
| 3220 | |
| 3221 | The taint behaviour of pp_subst (and pp_substcont) is quite complex. |
| 3222 | |
| 3223 | There are three possible sources of taint |
| 3224 | * the source string |
| 3225 | * the pattern (both compile- and run-time, RXf_TAINTED / RXf_TAINTED_SEEN) |
| 3226 | * the replacement string (or expression under /e) |
| 3227 | |
| 3228 | There are four destinations of taint and they are affected by the sources |
| 3229 | according to the rules below: |
| 3230 | |
| 3231 | * the return value (not including /r): |
| 3232 | tainted by the source string and pattern, but only for the |
| 3233 | number-of-iterations case; boolean returns aren't tainted; |
| 3234 | * the modified string (or modified copy under /r): |
| 3235 | tainted by the source string, pattern, and replacement strings; |
| 3236 | * $1 et al: |
| 3237 | tainted by the pattern, and under 'use re "taint"', by the source |
| 3238 | string too; |
| 3239 | * PL_taint - i.e. whether subsequent code (e.g. in a /e block) is tainted: |
| 3240 | should always be unset before executing subsequent code. |
| 3241 | |
| 3242 | The overall action of pp_subst is: |
| 3243 | |
| 3244 | * at the start, set bits in rxtainted indicating the taint status of |
| 3245 | the various sources. |
| 3246 | |
| 3247 | * After each pattern execution, update the SUBST_TAINT_PAT bit in |
| 3248 | rxtainted if RXf_TAINTED_SEEN has been set, to indicate that the |
| 3249 | pattern has subsequently become tainted via locale ops. |
| 3250 | |
| 3251 | * If control is being passed to pp_substcont to execute a /e block, |
| 3252 | save rxtainted in the CXt_SUBST block, for future use by |
| 3253 | pp_substcont. |
| 3254 | |
| 3255 | * Whenever control is being returned to perl code (either by falling |
| 3256 | off the "end" of pp_subst/pp_substcont, or by entering a /e block), |
| 3257 | use the flag bits in rxtainted to make all the appropriate types of |
| 3258 | destination taint visible; e.g. set RXf_TAINTED_SEEN so that $1 |
| 3259 | et al will appear tainted. |
| 3260 | |
| 3261 | pp_match is just a simpler version of the above. |
| 3262 | |
| 3263 | */ |
| 3264 | |
| 3265 | PP(pp_subst) |
| 3266 | { |
| 3267 | dSP; dTARG; |
| 3268 | PMOP *pm = cPMOP; |
| 3269 | PMOP *rpm = pm; |
| 3270 | char *s; |
| 3271 | char *strend; |
| 3272 | const char *c; |
| 3273 | STRLEN clen; |
| 3274 | SSize_t iters = 0; |
| 3275 | SSize_t maxiters; |
| 3276 | bool once; |
| 3277 | U8 rxtainted = 0; /* holds various SUBST_TAINT_* flag bits. |
| 3278 | See "how taint works" above */ |
| 3279 | char *orig; |
| 3280 | U8 r_flags; |
| 3281 | REGEXP *rx = PM_GETRE(pm); |
| 3282 | regexp *prog = ReANY(rx); |
| 3283 | STRLEN len; |
| 3284 | int force_on_match = 0; |
| 3285 | const I32 oldsave = PL_savestack_ix; |
| 3286 | STRLEN slen; |
| 3287 | bool doutf8 = FALSE; /* whether replacement is in utf8 */ |
| 3288 | #ifdef PERL_ANY_COW |
| 3289 | bool was_cow; |
| 3290 | #endif |
| 3291 | SV *nsv = NULL; |
| 3292 | /* known replacement string? */ |
| 3293 | SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL; |
| 3294 | |
| 3295 | PERL_ASYNC_CHECK(); |
| 3296 | |
| 3297 | if (PL_op->op_flags & OPf_STACKED) |
| 3298 | TARG = POPs; |
| 3299 | else { |
| 3300 | if (ARGTARG) |
| 3301 | GETTARGET; |
| 3302 | else { |
| 3303 | TARG = DEFSV; |
| 3304 | } |
| 3305 | EXTEND(SP,1); |
| 3306 | } |
| 3307 | |
| 3308 | SvGETMAGIC(TARG); /* must come before cow check */ |
| 3309 | #ifdef PERL_ANY_COW |
| 3310 | /* note that a string might get converted to COW during matching */ |
| 3311 | was_cow = cBOOL(SvIsCOW(TARG)); |
| 3312 | #endif |
| 3313 | if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) { |
| 3314 | #ifndef PERL_ANY_COW |
| 3315 | if (SvIsCOW(TARG)) |
| 3316 | sv_force_normal_flags(TARG,0); |
| 3317 | #endif |
| 3318 | if ((SvREADONLY(TARG) |
| 3319 | || ( ((SvTYPE(TARG) == SVt_PVGV && isGV_with_GP(TARG)) |
| 3320 | || SvTYPE(TARG) > SVt_PVLV) |
| 3321 | && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))) |
| 3322 | Perl_croak_no_modify(); |
| 3323 | } |
| 3324 | PUTBACK; |
| 3325 | |
| 3326 | orig = SvPV_nomg(TARG, len); |
| 3327 | /* note we don't (yet) force the var into being a string; if we fail |
| 3328 | * to match, we leave as-is; on successful match however, we *will* |
| 3329 | * coerce into a string, then repeat the match */ |
| 3330 | if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV || SvVOK(TARG)) |
| 3331 | force_on_match = 1; |
| 3332 | |
| 3333 | /* only replace once? */ |
| 3334 | once = !(rpm->op_pmflags & PMf_GLOBAL); |
| 3335 | |
| 3336 | /* See "how taint works" above */ |
| 3337 | if (TAINTING_get) { |
| 3338 | rxtainted = ( |
| 3339 | (SvTAINTED(TARG) ? SUBST_TAINT_STR : 0) |
| 3340 | | (RXp_ISTAINTED(prog) ? SUBST_TAINT_PAT : 0) |
| 3341 | | ((pm->op_pmflags & PMf_RETAINT) ? SUBST_TAINT_RETAINT : 0) |
| 3342 | | ((once && !(rpm->op_pmflags & PMf_NONDESTRUCT)) |
| 3343 | ? SUBST_TAINT_BOOLRET : 0)); |
| 3344 | TAINT_NOT; |
| 3345 | } |
| 3346 | |
| 3347 | force_it: |
| 3348 | if (!pm || !orig) |
| 3349 | DIE(aTHX_ "panic: pp_subst, pm=%p, orig=%p", pm, orig); |
| 3350 | |
| 3351 | strend = orig + len; |
| 3352 | slen = DO_UTF8(TARG) ? utf8_length((U8*)orig, (U8*)strend) : len; |
| 3353 | maxiters = 2 * slen + 10; /* We can match twice at each |
| 3354 | position, once with zero-length, |
| 3355 | second time with non-zero. */ |
| 3356 | |
| 3357 | /* handle the empty pattern */ |
| 3358 | if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) { |
| 3359 | if (PL_curpm == PL_reg_curpm) { |
| 3360 | if (PL_curpm_under) { |
| 3361 | if (PL_curpm_under == PL_reg_curpm) { |
| 3362 | Perl_croak(aTHX_ "Infinite recursion via empty pattern"); |
| 3363 | } else { |
| 3364 | pm = PL_curpm_under; |
| 3365 | } |
| 3366 | } |
| 3367 | } else { |
| 3368 | pm = PL_curpm; |
| 3369 | } |
| 3370 | rx = PM_GETRE(pm); |
| 3371 | prog = ReANY(rx); |
| 3372 | } |
| 3373 | |
| 3374 | #ifdef PERL_SAWAMPERSAND |
| 3375 | r_flags = ( RXp_NPARENS(prog) |
| 3376 | || PL_sawampersand |
| 3377 | || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY)) |
| 3378 | || (rpm->op_pmflags & PMf_KEEPCOPY) |
| 3379 | ) |
| 3380 | ? REXEC_COPY_STR |
| 3381 | : 0; |
| 3382 | #else |
| 3383 | r_flags = REXEC_COPY_STR; |
| 3384 | #endif |
| 3385 | |
| 3386 | if (!CALLREGEXEC(rx, orig, strend, orig, 0, TARG, NULL, r_flags)) |
| 3387 | { |
| 3388 | SPAGAIN; |
| 3389 | PUSHs(rpm->op_pmflags & PMf_NONDESTRUCT ? TARG : &PL_sv_no); |
| 3390 | LEAVE_SCOPE(oldsave); |
| 3391 | RETURN; |
| 3392 | } |
| 3393 | PL_curpm = pm; |
| 3394 | |
| 3395 | /* known replacement string? */ |
| 3396 | if (dstr) { |
| 3397 | /* replacement needing upgrading? */ |
| 3398 | if (DO_UTF8(TARG) && !doutf8) { |
| 3399 | nsv = sv_newmortal(); |
| 3400 | SvSetSV(nsv, dstr); |
| 3401 | sv_utf8_upgrade(nsv); |
| 3402 | c = SvPV_const(nsv, clen); |
| 3403 | doutf8 = TRUE; |
| 3404 | } |
| 3405 | else { |
| 3406 | c = SvPV_const(dstr, clen); |
| 3407 | doutf8 = DO_UTF8(dstr); |
| 3408 | } |
| 3409 | |
| 3410 | if (SvTAINTED(dstr)) |
| 3411 | rxtainted |= SUBST_TAINT_REPL; |
| 3412 | } |
| 3413 | else { |
| 3414 | c = NULL; |
| 3415 | doutf8 = FALSE; |
| 3416 | } |
| 3417 | |
| 3418 | /* can do inplace substitution? */ |
| 3419 | if (c |
| 3420 | #ifdef PERL_ANY_COW |
| 3421 | && !was_cow |
| 3422 | #endif |
| 3423 | && (I32)clen <= RXp_MINLENRET(prog) |
| 3424 | && ( once |
| 3425 | || !(r_flags & REXEC_COPY_STR) |
| 3426 | || (!SvGMAGICAL(dstr) && !(RXp_EXTFLAGS(prog) & RXf_EVAL_SEEN)) |
| 3427 | ) |
| 3428 | && !(RXp_EXTFLAGS(prog) & RXf_NO_INPLACE_SUBST) |
| 3429 | && (!doutf8 || SvUTF8(TARG)) |
| 3430 | && !(rpm->op_pmflags & PMf_NONDESTRUCT)) |
| 3431 | { |
| 3432 | |
| 3433 | #ifdef PERL_ANY_COW |
| 3434 | /* string might have got converted to COW since we set was_cow */ |
| 3435 | if (SvIsCOW(TARG)) { |
| 3436 | if (!force_on_match) |
| 3437 | goto have_a_cow; |
| 3438 | assert(SvVOK(TARG)); |
| 3439 | } |
| 3440 | #endif |
| 3441 | if (force_on_match) { |
| 3442 | /* redo the first match, this time with the orig var |
| 3443 | * forced into being a string */ |
| 3444 | force_on_match = 0; |
| 3445 | orig = SvPV_force_nomg(TARG, len); |
| 3446 | goto force_it; |
| 3447 | } |
| 3448 | |
| 3449 | if (once) { |
| 3450 | char *d, *m; |
| 3451 | if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */ |
| 3452 | rxtainted |= SUBST_TAINT_PAT; |
| 3453 | m = orig + RXp_OFFS(prog)[0].start; |
| 3454 | d = orig + RXp_OFFS(prog)[0].end; |
| 3455 | s = orig; |
| 3456 | if (m - s > strend - d) { /* faster to shorten from end */ |
| 3457 | I32 i; |
| 3458 | if (clen) { |
| 3459 | Copy(c, m, clen, char); |
| 3460 | m += clen; |
| 3461 | } |
| 3462 | i = strend - d; |
| 3463 | if (i > 0) { |
| 3464 | Move(d, m, i, char); |
| 3465 | m += i; |
| 3466 | } |
| 3467 | *m = '\0'; |
| 3468 | SvCUR_set(TARG, m - s); |
| 3469 | } |
| 3470 | else { /* faster from front */ |
| 3471 | I32 i = m - s; |
| 3472 | d -= clen; |
| 3473 | if (i > 0) |
| 3474 | Move(s, d - i, i, char); |
| 3475 | sv_chop(TARG, d-i); |
| 3476 | if (clen) |
| 3477 | Copy(c, d, clen, char); |
| 3478 | } |
| 3479 | SPAGAIN; |
| 3480 | PUSHs(&PL_sv_yes); |
| 3481 | } |
| 3482 | else { |
| 3483 | char *d, *m; |
| 3484 | d = s = RXp_OFFS(prog)[0].start + orig; |
| 3485 | do { |
| 3486 | I32 i; |
| 3487 | if (UNLIKELY(iters++ > maxiters)) |
| 3488 | DIE(aTHX_ "Substitution loop"); |
| 3489 | /* run time pattern taint, eg locale */ |
| 3490 | if (UNLIKELY(RXp_MATCH_TAINTED(prog))) |
| 3491 | rxtainted |= SUBST_TAINT_PAT; |
| 3492 | m = RXp_OFFS(prog)[0].start + orig; |
| 3493 | if ((i = m - s)) { |
| 3494 | if (s != d) |
| 3495 | Move(s, d, i, char); |
| 3496 | d += i; |
| 3497 | } |
| 3498 | if (clen) { |
| 3499 | Copy(c, d, clen, char); |
| 3500 | d += clen; |
| 3501 | } |
| 3502 | s = RXp_OFFS(prog)[0].end + orig; |
| 3503 | } while (CALLREGEXEC(rx, s, strend, orig, |
| 3504 | s == m, /* don't match same null twice */ |
| 3505 | TARG, NULL, |
| 3506 | REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW)); |
| 3507 | if (s != d) { |
| 3508 | I32 i = strend - s; |
| 3509 | SvCUR_set(TARG, d - SvPVX_const(TARG) + i); |
| 3510 | Move(s, d, i+1, char); /* include the NUL */ |
| 3511 | } |
| 3512 | SPAGAIN; |
| 3513 | if (PL_op->op_private & OPpTRUEBOOL) |
| 3514 | PUSHs(iters ? &PL_sv_yes : &PL_sv_zero); |
| 3515 | else |
| 3516 | mPUSHi(iters); |
| 3517 | } |
| 3518 | } |
| 3519 | else { |
| 3520 | bool first; |
| 3521 | char *m; |
| 3522 | SV *repl; |
| 3523 | if (force_on_match) { |
| 3524 | /* redo the first match, this time with the orig var |
| 3525 | * forced into being a string */ |
| 3526 | force_on_match = 0; |
| 3527 | if (rpm->op_pmflags & PMf_NONDESTRUCT) { |
| 3528 | /* I feel that it should be possible to avoid this mortal copy |
| 3529 | given that the code below copies into a new destination. |
| 3530 | However, I suspect it isn't worth the complexity of |
| 3531 | unravelling the C<goto force_it> for the small number of |
| 3532 | cases where it would be viable to drop into the copy code. */ |
| 3533 | TARG = sv_2mortal(newSVsv(TARG)); |
| 3534 | } |
| 3535 | orig = SvPV_force_nomg(TARG, len); |
| 3536 | goto force_it; |
| 3537 | } |
| 3538 | #ifdef PERL_ANY_COW |
| 3539 | have_a_cow: |
| 3540 | #endif |
| 3541 | if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */ |
| 3542 | rxtainted |= SUBST_TAINT_PAT; |
| 3543 | repl = dstr; |
| 3544 | s = RXp_OFFS(prog)[0].start + orig; |
| 3545 | dstr = newSVpvn_flags(orig, s-orig, |
| 3546 | SVs_TEMP | (DO_UTF8(TARG) ? SVf_UTF8 : 0)); |
| 3547 | if (!c) { |
| 3548 | PERL_CONTEXT *cx; |
| 3549 | SPAGAIN; |
| 3550 | m = orig; |
| 3551 | /* note that a whole bunch of local vars are saved here for |
| 3552 | * use by pp_substcont: here's a list of them in case you're |
| 3553 | * searching for places in this sub that uses a particular var: |
| 3554 | * iters maxiters r_flags oldsave rxtainted orig dstr targ |
| 3555 | * s m strend rx once */ |
| 3556 | CX_PUSHSUBST(cx); |
| 3557 | RETURNOP(cPMOP->op_pmreplrootu.op_pmreplroot); |
| 3558 | } |
| 3559 | first = TRUE; |
| 3560 | do { |
| 3561 | if (UNLIKELY(iters++ > maxiters)) |
| 3562 | DIE(aTHX_ "Substitution loop"); |
| 3563 | if (UNLIKELY(RXp_MATCH_TAINTED(prog))) |
| 3564 | rxtainted |= SUBST_TAINT_PAT; |
| 3565 | if (RXp_MATCH_COPIED(prog) && RXp_SUBBEG(prog) != orig) { |
| 3566 | char *old_s = s; |
| 3567 | char *old_orig = orig; |
| 3568 | assert(RXp_SUBOFFSET(prog) == 0); |
| 3569 | |
| 3570 | orig = RXp_SUBBEG(prog); |
| 3571 | s = orig + (old_s - old_orig); |
| 3572 | strend = s + (strend - old_s); |
| 3573 | } |
| 3574 | m = RXp_OFFS(prog)[0].start + orig; |
| 3575 | sv_catpvn_nomg_maybeutf8(dstr, s, m - s, DO_UTF8(TARG)); |
| 3576 | s = RXp_OFFS(prog)[0].end + orig; |
| 3577 | if (first) { |
| 3578 | /* replacement already stringified */ |
| 3579 | if (clen) |
| 3580 | sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8); |
| 3581 | first = FALSE; |
| 3582 | } |
| 3583 | else { |
| 3584 | sv_catsv(dstr, repl); |
| 3585 | if (UNLIKELY(SvTAINTED(repl))) |
| 3586 | rxtainted |= SUBST_TAINT_REPL; |
| 3587 | } |
| 3588 | if (once) |
| 3589 | break; |
| 3590 | } while (CALLREGEXEC(rx, s, strend, orig, |
| 3591 | s == m, /* Yields minend of 0 or 1 */ |
| 3592 | TARG, NULL, |
| 3593 | REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW)); |
| 3594 | assert(strend >= s); |
| 3595 | sv_catpvn_nomg_maybeutf8(dstr, s, strend - s, DO_UTF8(TARG)); |
| 3596 | |
| 3597 | if (rpm->op_pmflags & PMf_NONDESTRUCT) { |
| 3598 | /* From here on down we're using the copy, and leaving the original |
| 3599 | untouched. */ |
| 3600 | TARG = dstr; |
| 3601 | SPAGAIN; |
| 3602 | PUSHs(dstr); |
| 3603 | } else { |
| 3604 | #ifdef PERL_ANY_COW |
| 3605 | /* The match may make the string COW. If so, brilliant, because |
| 3606 | that's just saved us one malloc, copy and free - the regexp has |
| 3607 | donated the old buffer, and we malloc an entirely new one, rather |
| 3608 | than the regexp malloc()ing a buffer and copying our original, |
| 3609 | only for us to throw it away here during the substitution. */ |
| 3610 | if (SvIsCOW(TARG)) { |
| 3611 | sv_force_normal_flags(TARG, SV_COW_DROP_PV); |
| 3612 | } else |
| 3613 | #endif |
| 3614 | { |
| 3615 | SvPV_free(TARG); |
| 3616 | } |
| 3617 | SvPV_set(TARG, SvPVX(dstr)); |
| 3618 | SvCUR_set(TARG, SvCUR(dstr)); |
| 3619 | SvLEN_set(TARG, SvLEN(dstr)); |
| 3620 | SvFLAGS(TARG) |= SvUTF8(dstr); |
| 3621 | SvPV_set(dstr, NULL); |
| 3622 | |
| 3623 | SPAGAIN; |
| 3624 | mPUSHi(iters); |
| 3625 | } |
| 3626 | } |
| 3627 | |
| 3628 | if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) { |
| 3629 | (void)SvPOK_only_UTF8(TARG); |
| 3630 | } |
| 3631 | |
| 3632 | /* See "how taint works" above */ |
| 3633 | if (TAINTING_get) { |
| 3634 | if ((rxtainted & SUBST_TAINT_PAT) || |
| 3635 | ((rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) == |
| 3636 | (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) |
| 3637 | ) |
| 3638 | (RXp_MATCH_TAINTED_on(prog)); /* taint $1 et al */ |
| 3639 | |
| 3640 | if (!(rxtainted & SUBST_TAINT_BOOLRET) |
| 3641 | && (rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT)) |
| 3642 | ) |
| 3643 | SvTAINTED_on(TOPs); /* taint return value */ |
| 3644 | else |
| 3645 | SvTAINTED_off(TOPs); /* may have got tainted earlier */ |
| 3646 | |
| 3647 | /* needed for mg_set below */ |
| 3648 | TAINT_set( |
| 3649 | cBOOL(rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)) |
| 3650 | ); |
| 3651 | SvTAINT(TARG); |
| 3652 | } |
| 3653 | SvSETMAGIC(TARG); /* PL_tainted must be correctly set for this mg_set */ |
| 3654 | TAINT_NOT; |
| 3655 | LEAVE_SCOPE(oldsave); |
| 3656 | RETURN; |
| 3657 | } |
| 3658 | |
| 3659 | PP(pp_grepwhile) |
| 3660 | { |
| 3661 | dSP; |
| 3662 | dPOPss; |
| 3663 | |
| 3664 | if (SvTRUE_NN(sv)) |
| 3665 | PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr]; |
| 3666 | ++*PL_markstack_ptr; |
| 3667 | FREETMPS; |
| 3668 | LEAVE_with_name("grep_item"); /* exit inner scope */ |
| 3669 | |
| 3670 | /* All done yet? */ |
| 3671 | if (UNLIKELY(PL_stack_base + *PL_markstack_ptr > SP)) { |
| 3672 | I32 items; |
| 3673 | const U8 gimme = GIMME_V; |
| 3674 | |
| 3675 | LEAVE_with_name("grep"); /* exit outer scope */ |
| 3676 | (void)POPMARK; /* pop src */ |
| 3677 | items = --*PL_markstack_ptr - PL_markstack_ptr[-1]; |
| 3678 | (void)POPMARK; /* pop dst */ |
| 3679 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
| 3680 | if (gimme == G_SCALAR) { |
| 3681 | if (PL_op->op_private & OPpTRUEBOOL) |
| 3682 | PUSHs(items ? &PL_sv_yes : &PL_sv_zero); |
| 3683 | else { |
| 3684 | dTARGET; |
| 3685 | PUSHi(items); |
| 3686 | } |
| 3687 | } |
| 3688 | else if (gimme == G_ARRAY) |
| 3689 | SP += items; |
| 3690 | RETURN; |
| 3691 | } |
| 3692 | else { |
| 3693 | SV *src; |
| 3694 | |
| 3695 | ENTER_with_name("grep_item"); /* enter inner scope */ |
| 3696 | SAVEVPTR(PL_curpm); |
| 3697 | |
| 3698 | src = PL_stack_base[TOPMARK]; |
| 3699 | if (SvPADTMP(src)) { |
| 3700 | src = PL_stack_base[TOPMARK] = sv_mortalcopy(src); |
| 3701 | PL_tmps_floor++; |
| 3702 | } |
| 3703 | SvTEMP_off(src); |
| 3704 | DEFSV_set(src); |
| 3705 | |
| 3706 | RETURNOP(cLOGOP->op_other); |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | /* leave_adjust_stacks(): |
| 3711 | * |
| 3712 | * Process a scope's return args (in the range from_sp+1 .. PL_stack_sp), |
| 3713 | * positioning them at to_sp+1 onwards, and do the equivalent of a |
| 3714 | * FREEMPS and TAINT_NOT. |
| 3715 | * |
| 3716 | * Not intended to be called in void context. |
| 3717 | * |
| 3718 | * When leaving a sub, eval, do{} or other scope, the things that need |
| 3719 | * doing to process the return args are: |
| 3720 | * * in scalar context, only return the last arg (or PL_sv_undef if none); |
| 3721 | * * for the types of return that return copies of their args (such |
| 3722 | * as rvalue sub return), make a mortal copy of every return arg, |
| 3723 | * except where we can optimise the copy away without it being |
| 3724 | * semantically visible; |
| 3725 | * * make sure that the arg isn't prematurely freed; in the case of an |
| 3726 | * arg not copied, this may involve mortalising it. For example, in |
| 3727 | * C<sub f { my $x = ...; $x }>, $x would be freed when we do |
| 3728 | * CX_LEAVE_SCOPE(cx) unless it's protected or copied. |
| 3729 | * |
| 3730 | * What condition to use when deciding whether to pass the arg through |
| 3731 | * or make a copy, is determined by the 'pass' arg; its valid values are: |
| 3732 | * 0: rvalue sub/eval exit |
| 3733 | * 1: other rvalue scope exit |
| 3734 | * 2: :lvalue sub exit in rvalue context |
| 3735 | * 3: :lvalue sub exit in lvalue context and other lvalue scope exits |
| 3736 | * |
| 3737 | * There is a big issue with doing a FREETMPS. We would like to free any |
| 3738 | * temps created by the last statement which the sub executed, rather than |
| 3739 | * leaving them for the caller. In a situation where a sub call isn't |
| 3740 | * soon followed by a nextstate (e.g. nested recursive calls, a la |
| 3741 | * fibonacci()), temps can accumulate, causing memory and performance |
| 3742 | * issues. |
| 3743 | * |
| 3744 | * On the other hand, we don't want to free any TEMPs which are keeping |
| 3745 | * alive any return args that we skipped copying; nor do we wish to undo |
| 3746 | * any mortalising done here. |
| 3747 | * |
| 3748 | * The solution is to split the temps stack frame into two, with a cut |
| 3749 | * point delineating the two halves. We arrange that by the end of this |
| 3750 | * function, all the temps stack frame entries we wish to keep are in the |
| 3751 | * range PL_tmps_floor+1.. tmps_base-1, while the ones to free now are in |
| 3752 | * the range tmps_base .. PL_tmps_ix. During the course of this |
| 3753 | * function, tmps_base starts off as PL_tmps_floor+1, then increases |
| 3754 | * whenever we find or create a temp that we know should be kept. In |
| 3755 | * general the stuff above tmps_base is undecided until we reach the end, |
| 3756 | * and we may need a sort stage for that. |
| 3757 | * |
| 3758 | * To determine whether a TEMP is keeping a return arg alive, every |
| 3759 | * arg that is kept rather than copied and which has the SvTEMP flag |
| 3760 | * set, has the flag temporarily unset, to mark it. At the end we scan |
| 3761 | * the temps stack frame above the cut for entries without SvTEMP and |
| 3762 | * keep them, while turning SvTEMP on again. Note that if we die before |
| 3763 | * the SvTEMPs flags are set again, its safe: at worst, subsequent use of |
| 3764 | * those SVs may be slightly less efficient. |
| 3765 | * |
| 3766 | * In practice various optimisations for some common cases mean we can |
| 3767 | * avoid most of the scanning and swapping about with the temps stack. |
| 3768 | */ |
| 3769 | |
| 3770 | void |
| 3771 | Perl_leave_adjust_stacks(pTHX_ SV **from_sp, SV **to_sp, U8 gimme, int pass) |
| 3772 | { |
| 3773 | dVAR; |
| 3774 | dSP; |
| 3775 | SSize_t tmps_base; /* lowest index into tmps stack that needs freeing now */ |
| 3776 | SSize_t nargs; |
| 3777 | |
| 3778 | PERL_ARGS_ASSERT_LEAVE_ADJUST_STACKS; |
| 3779 | |
| 3780 | TAINT_NOT; |
| 3781 | |
| 3782 | if (gimme == G_ARRAY) { |
| 3783 | nargs = SP - from_sp; |
| 3784 | from_sp++; |
| 3785 | } |
| 3786 | else { |
| 3787 | assert(gimme == G_SCALAR); |
| 3788 | if (UNLIKELY(from_sp >= SP)) { |
| 3789 | /* no return args */ |
| 3790 | assert(from_sp == SP); |
| 3791 | EXTEND(SP, 1); |
| 3792 | *++SP = &PL_sv_undef; |
| 3793 | to_sp = SP; |
| 3794 | nargs = 0; |
| 3795 | } |
| 3796 | else { |
| 3797 | from_sp = SP; |
| 3798 | nargs = 1; |
| 3799 | } |
| 3800 | } |
| 3801 | |
| 3802 | /* common code for G_SCALAR and G_ARRAY */ |
| 3803 | |
| 3804 | tmps_base = PL_tmps_floor + 1; |
| 3805 | |
| 3806 | assert(nargs >= 0); |
| 3807 | if (nargs) { |
| 3808 | /* pointer version of tmps_base. Not safe across temp stack |
| 3809 | * reallocs. */ |
| 3810 | SV **tmps_basep; |
| 3811 | |
| 3812 | EXTEND_MORTAL(nargs); /* one big extend for worst-case scenario */ |
| 3813 | tmps_basep = PL_tmps_stack + tmps_base; |
| 3814 | |
| 3815 | /* process each return arg */ |
| 3816 | |
| 3817 | do { |
| 3818 | SV *sv = *from_sp++; |
| 3819 | |
| 3820 | assert(PL_tmps_ix + nargs < PL_tmps_max); |
| 3821 | #ifdef DEBUGGING |
| 3822 | /* PADTMPs with container set magic shouldn't appear in the |
| 3823 | * wild. This assert is more important for pp_leavesublv(), |
| 3824 | * but by testing for it here, we're more likely to catch |
| 3825 | * bad cases (what with :lvalue subs not being widely |
| 3826 | * deployed). The two issues are that for something like |
| 3827 | * sub :lvalue { $tied{foo} } |
| 3828 | * or |
| 3829 | * sub :lvalue { substr($foo,1,2) } |
| 3830 | * pp_leavesublv() will croak if the sub returns a PADTMP, |
| 3831 | * and currently functions like pp_substr() return a mortal |
| 3832 | * rather than using their PADTMP when returning a PVLV. |
| 3833 | * This is because the PVLV will hold a ref to $foo, |
| 3834 | * so $foo would get delayed in being freed while |
| 3835 | * the PADTMP SV remained in the PAD. |
| 3836 | * So if this assert fails it means either: |
| 3837 | * 1) there is pp code similar to pp_substr that is |
| 3838 | * returning a PADTMP instead of a mortal, and probably |
| 3839 | * needs fixing, or |
| 3840 | * 2) pp_leavesublv is making unwarranted assumptions |
| 3841 | * about always croaking on a PADTMP |
| 3842 | */ |
| 3843 | if (SvPADTMP(sv) && SvSMAGICAL(sv)) { |
| 3844 | MAGIC *mg; |
| 3845 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
| 3846 | assert(PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type)); |
| 3847 | } |
| 3848 | } |
| 3849 | #endif |
| 3850 | |
| 3851 | if ( |
| 3852 | pass == 0 ? (SvTEMP(sv) && !SvMAGICAL(sv) && SvREFCNT(sv) == 1) |
| 3853 | : pass == 1 ? ((SvTEMP(sv) || SvPADTMP(sv)) && !SvMAGICAL(sv) && SvREFCNT(sv) == 1) |
| 3854 | : pass == 2 ? (!SvPADTMP(sv)) |
| 3855 | : 1) |
| 3856 | { |
| 3857 | /* pass through: skip copy for logic or optimisation |
| 3858 | * reasons; instead mortalise it, except that ... */ |
| 3859 | *++to_sp = sv; |
| 3860 | |
| 3861 | if (SvTEMP(sv)) { |
| 3862 | /* ... since this SV is an SvTEMP , we don't need to |
| 3863 | * re-mortalise it; instead we just need to ensure |
| 3864 | * that its existing entry in the temps stack frame |
| 3865 | * ends up below the cut and so avoids being freed |
| 3866 | * this time round. We mark it as needing to be kept |
| 3867 | * by temporarily unsetting SvTEMP; then at the end, |
| 3868 | * we shuffle any !SvTEMP entries on the tmps stack |
| 3869 | * back below the cut. |
| 3870 | * However, there's a significant chance that there's |
| 3871 | * a 1:1 correspondence between the first few (or all) |
| 3872 | * elements in the return args stack frame and those |
| 3873 | * in the temps stack frame; e,g.: |
| 3874 | * sub f { ....; map {...} .... }, |
| 3875 | * or if we're exiting multiple scopes and one of the |
| 3876 | * inner scopes has already made mortal copies of each |
| 3877 | * return arg. |
| 3878 | * |
| 3879 | * If so, this arg sv will correspond to the next item |
| 3880 | * on the tmps stack above the cut, and so can be kept |
| 3881 | * merely by moving the cut boundary up one, rather |
| 3882 | * than messing with SvTEMP. If all args are 1:1 then |
| 3883 | * we can avoid the sorting stage below completely. |
| 3884 | * |
| 3885 | * If there are no items above the cut on the tmps |
| 3886 | * stack, then the SvTEMP must comne from an item |
| 3887 | * below the cut, so there's nothing to do. |
| 3888 | */ |
| 3889 | if (tmps_basep <= &PL_tmps_stack[PL_tmps_ix]) { |
| 3890 | if (sv == *tmps_basep) |
| 3891 | tmps_basep++; |
| 3892 | else |
| 3893 | SvTEMP_off(sv); |
| 3894 | } |
| 3895 | } |
| 3896 | else if (!SvPADTMP(sv)) { |
| 3897 | /* mortalise arg to avoid it being freed during save |
| 3898 | * stack unwinding. Pad tmps don't need mortalising as |
| 3899 | * they're never freed. This is the equivalent of |
| 3900 | * sv_2mortal(SvREFCNT_inc(sv)), except that: |
| 3901 | * * it assumes that the temps stack has already been |
| 3902 | * extended; |
| 3903 | * * it puts the new item at the cut rather than at |
| 3904 | * ++PL_tmps_ix, moving the previous occupant there |
| 3905 | * instead. |
| 3906 | */ |
| 3907 | if (!SvIMMORTAL(sv)) { |
| 3908 | SvREFCNT_inc_simple_void_NN(sv); |
| 3909 | SvTEMP_on(sv); |
| 3910 | /* Note that if there's nothing above the cut, |
| 3911 | * this copies the garbage one slot above |
| 3912 | * PL_tmps_ix onto itself. This is harmless (the |
| 3913 | * stack's already been extended), but might in |
| 3914 | * theory trigger warnings from tools like ASan |
| 3915 | */ |
| 3916 | PL_tmps_stack[++PL_tmps_ix] = *tmps_basep; |
| 3917 | *tmps_basep++ = sv; |
| 3918 | } |
| 3919 | } |
| 3920 | } |
| 3921 | else { |
| 3922 | /* Make a mortal copy of the SV. |
| 3923 | * The following code is the equivalent of sv_mortalcopy() |
| 3924 | * except that: |
| 3925 | * * it assumes the temps stack has already been extended; |
| 3926 | * * it optimises the copying for some simple SV types; |
| 3927 | * * it puts the new item at the cut rather than at |
| 3928 | * ++PL_tmps_ix, moving the previous occupant there |
| 3929 | * instead. |
| 3930 | */ |
| 3931 | SV *newsv = newSV(0); |
| 3932 | |
| 3933 | PL_tmps_stack[++PL_tmps_ix] = *tmps_basep; |
| 3934 | /* put it on the tmps stack early so it gets freed if we die */ |
| 3935 | *tmps_basep++ = newsv; |
| 3936 | *++to_sp = newsv; |
| 3937 | |
| 3938 | if (SvTYPE(sv) <= SVt_IV) { |
| 3939 | /* arg must be one of undef, IV/UV, or RV: skip |
| 3940 | * sv_setsv_flags() and do the copy directly */ |
| 3941 | U32 dstflags; |
| 3942 | U32 srcflags = SvFLAGS(sv); |
| 3943 | |
| 3944 | assert(!SvGMAGICAL(sv)); |
| 3945 | if (srcflags & (SVf_IOK|SVf_ROK)) { |
| 3946 | SET_SVANY_FOR_BODYLESS_IV(newsv); |
| 3947 | |
| 3948 | if (srcflags & SVf_ROK) { |
| 3949 | newsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(sv)); |
| 3950 | /* SV type plus flags */ |
| 3951 | dstflags = (SVt_IV|SVf_ROK|SVs_TEMP); |
| 3952 | } |
| 3953 | else { |
| 3954 | /* both src and dst are <= SVt_IV, so sv_any |
| 3955 | * points to the head; so access the heads |
| 3956 | * directly rather than going via sv_any. |
| 3957 | */ |
| 3958 | assert( &(sv->sv_u.svu_iv) |
| 3959 | == &(((XPVIV*) SvANY(sv))->xiv_iv)); |
| 3960 | assert( &(newsv->sv_u.svu_iv) |
| 3961 | == &(((XPVIV*) SvANY(newsv))->xiv_iv)); |
| 3962 | newsv->sv_u.svu_iv = sv->sv_u.svu_iv; |
| 3963 | /* SV type plus flags */ |
| 3964 | dstflags = (SVt_IV|SVf_IOK|SVp_IOK|SVs_TEMP |
| 3965 | |(srcflags & SVf_IVisUV)); |
| 3966 | } |
| 3967 | } |
| 3968 | else { |
| 3969 | assert(!(srcflags & SVf_OK)); |
| 3970 | dstflags = (SVt_NULL|SVs_TEMP); /* SV type plus flags */ |
| 3971 | } |
| 3972 | SvFLAGS(newsv) = dstflags; |
| 3973 | |
| 3974 | } |
| 3975 | else { |
| 3976 | /* do the full sv_setsv() */ |
| 3977 | SSize_t old_base; |
| 3978 | |
| 3979 | SvTEMP_on(newsv); |
| 3980 | old_base = tmps_basep - PL_tmps_stack; |
| 3981 | SvGETMAGIC(sv); |
| 3982 | sv_setsv_flags(newsv, sv, SV_DO_COW_SVSETSV); |
| 3983 | /* the mg_get or sv_setsv might have created new temps |
| 3984 | * or realloced the tmps stack; regrow and reload */ |
| 3985 | EXTEND_MORTAL(nargs); |
| 3986 | tmps_basep = PL_tmps_stack + old_base; |
| 3987 | TAINT_NOT; /* Each item is independent */ |
| 3988 | } |
| 3989 | |
| 3990 | } |
| 3991 | } while (--nargs); |
| 3992 | |
| 3993 | /* If there are any temps left above the cut, we need to sort |
| 3994 | * them into those to keep and those to free. The only ones to |
| 3995 | * keep are those for which we've temporarily unset SvTEMP. |
| 3996 | * Work inwards from the two ends at tmps_basep .. PL_tmps_ix, |
| 3997 | * swapping pairs as necessary. Stop when we meet in the middle. |
| 3998 | */ |
| 3999 | { |
| 4000 | SV **top = PL_tmps_stack + PL_tmps_ix; |
| 4001 | while (tmps_basep <= top) { |
| 4002 | SV *sv = *top; |
| 4003 | if (SvTEMP(sv)) |
| 4004 | top--; |
| 4005 | else { |
| 4006 | SvTEMP_on(sv); |
| 4007 | *top = *tmps_basep; |
| 4008 | *tmps_basep = sv; |
| 4009 | tmps_basep++; |
| 4010 | } |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | tmps_base = tmps_basep - PL_tmps_stack; |
| 4015 | } |
| 4016 | |
| 4017 | PL_stack_sp = to_sp; |
| 4018 | |
| 4019 | /* unrolled FREETMPS() but using tmps_base-1 rather than PL_tmps_floor */ |
| 4020 | while (PL_tmps_ix >= tmps_base) { |
| 4021 | SV* const sv = PL_tmps_stack[PL_tmps_ix--]; |
| 4022 | #ifdef PERL_POISON |
| 4023 | PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB); |
| 4024 | #endif |
| 4025 | if (LIKELY(sv)) { |
| 4026 | SvTEMP_off(sv); |
| 4027 | SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */ |
| 4028 | } |
| 4029 | } |
| 4030 | } |
| 4031 | |
| 4032 | |
| 4033 | /* also tail-called by pp_return */ |
| 4034 | |
| 4035 | PP(pp_leavesub) |
| 4036 | { |
| 4037 | U8 gimme; |
| 4038 | PERL_CONTEXT *cx; |
| 4039 | SV **oldsp; |
| 4040 | OP *retop; |
| 4041 | |
| 4042 | cx = CX_CUR(); |
| 4043 | assert(CxTYPE(cx) == CXt_SUB); |
| 4044 | |
| 4045 | if (CxMULTICALL(cx)) { |
| 4046 | /* entry zero of a stack is always PL_sv_undef, which |
| 4047 | * simplifies converting a '()' return into undef in scalar context */ |
| 4048 | assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef); |
| 4049 | return 0; |
| 4050 | } |
| 4051 | |
| 4052 | gimme = cx->blk_gimme; |
| 4053 | oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */ |
| 4054 | |
| 4055 | if (gimme == G_VOID) |
| 4056 | PL_stack_sp = oldsp; |
| 4057 | else |
| 4058 | leave_adjust_stacks(oldsp, oldsp, gimme, 0); |
| 4059 | |
| 4060 | CX_LEAVE_SCOPE(cx); |
| 4061 | cx_popsub(cx); /* Stack values are safe: release CV and @_ ... */ |
| 4062 | cx_popblock(cx); |
| 4063 | retop = cx->blk_sub.retop; |
| 4064 | CX_POP(cx); |
| 4065 | |
| 4066 | return retop; |
| 4067 | } |
| 4068 | |
| 4069 | |
| 4070 | /* clear (if possible) or abandon the current @_. If 'abandon' is true, |
| 4071 | * forces an abandon */ |
| 4072 | |
| 4073 | void |
| 4074 | Perl_clear_defarray(pTHX_ AV* av, bool abandon) |
| 4075 | { |
| 4076 | const SSize_t fill = AvFILLp(av); |
| 4077 | |
| 4078 | PERL_ARGS_ASSERT_CLEAR_DEFARRAY; |
| 4079 | |
| 4080 | if (LIKELY(!abandon && SvREFCNT(av) == 1 && !SvMAGICAL(av))) { |
| 4081 | av_clear(av); |
| 4082 | AvREIFY_only(av); |
| 4083 | } |
| 4084 | else { |
| 4085 | AV *newav = newAV(); |
| 4086 | av_extend(newav, fill); |
| 4087 | AvREIFY_only(newav); |
| 4088 | PAD_SVl(0) = MUTABLE_SV(newav); |
| 4089 | SvREFCNT_dec_NN(av); |
| 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | |
| 4094 | PP(pp_entersub) |
| 4095 | { |
| 4096 | dSP; dPOPss; |
| 4097 | GV *gv; |
| 4098 | CV *cv; |
| 4099 | PERL_CONTEXT *cx; |
| 4100 | I32 old_savestack_ix; |
| 4101 | |
| 4102 | if (UNLIKELY(!sv)) |
| 4103 | goto do_die; |
| 4104 | |
| 4105 | /* Locate the CV to call: |
| 4106 | * - most common case: RV->CV: f(), $ref->(): |
| 4107 | * note that if a sub is compiled before its caller is compiled, |
| 4108 | * the stash entry will be a ref to a CV, rather than being a GV. |
| 4109 | * - second most common case: CV: $ref->method() |
| 4110 | */ |
| 4111 | |
| 4112 | /* a non-magic-RV -> CV ? */ |
| 4113 | if (LIKELY( (SvFLAGS(sv) & (SVf_ROK|SVs_GMG)) == SVf_ROK)) { |
| 4114 | cv = MUTABLE_CV(SvRV(sv)); |
| 4115 | if (UNLIKELY(SvOBJECT(cv))) /* might be overloaded */ |
| 4116 | goto do_ref; |
| 4117 | } |
| 4118 | else |
| 4119 | cv = MUTABLE_CV(sv); |
| 4120 | |
| 4121 | /* a CV ? */ |
| 4122 | if (UNLIKELY(SvTYPE(cv) != SVt_PVCV)) { |
| 4123 | /* handle all the weird cases */ |
| 4124 | switch (SvTYPE(sv)) { |
| 4125 | case SVt_PVLV: |
| 4126 | if (!isGV_with_GP(sv)) |
| 4127 | goto do_default; |
| 4128 | /* FALLTHROUGH */ |
| 4129 | case SVt_PVGV: |
| 4130 | cv = GvCVu((const GV *)sv); |
| 4131 | if (UNLIKELY(!cv)) { |
| 4132 | HV *stash; |
| 4133 | cv = sv_2cv(sv, &stash, &gv, 0); |
| 4134 | if (!cv) { |
| 4135 | old_savestack_ix = PL_savestack_ix; |
| 4136 | goto try_autoload; |
| 4137 | } |
| 4138 | } |
| 4139 | break; |
| 4140 | |
| 4141 | default: |
| 4142 | do_default: |
| 4143 | SvGETMAGIC(sv); |
| 4144 | if (SvROK(sv)) { |
| 4145 | do_ref: |
| 4146 | if (UNLIKELY(SvAMAGIC(sv))) { |
| 4147 | sv = amagic_deref_call(sv, to_cv_amg); |
| 4148 | /* Don't SPAGAIN here. */ |
| 4149 | } |
| 4150 | } |
| 4151 | else { |
| 4152 | const char *sym; |
| 4153 | STRLEN len; |
| 4154 | if (UNLIKELY(!SvOK(sv))) |
| 4155 | DIE(aTHX_ PL_no_usym, "a subroutine"); |
| 4156 | |
| 4157 | if (UNLIKELY(sv == &PL_sv_yes)) { /* unfound import, ignore */ |
| 4158 | if (PL_op->op_flags & OPf_STACKED) /* hasargs */ |
| 4159 | SP = PL_stack_base + POPMARK; |
| 4160 | else |
| 4161 | (void)POPMARK; |
| 4162 | if (GIMME_V == G_SCALAR) |
| 4163 | PUSHs(&PL_sv_undef); |
| 4164 | RETURN; |
| 4165 | } |
| 4166 | |
| 4167 | sym = SvPV_nomg_const(sv, len); |
| 4168 | if (PL_op->op_private & HINT_STRICT_REFS) |
| 4169 | DIE(aTHX_ "Can't use string (\"%" SVf32 "\"%s) as a subroutine ref while \"strict refs\" in use", sv, len>32 ? "..." : ""); |
| 4170 | cv = get_cvn_flags(sym, len, GV_ADD|SvUTF8(sv)); |
| 4171 | break; |
| 4172 | } |
| 4173 | cv = MUTABLE_CV(SvRV(sv)); |
| 4174 | if (LIKELY(SvTYPE(cv) == SVt_PVCV)) |
| 4175 | break; |
| 4176 | /* FALLTHROUGH */ |
| 4177 | case SVt_PVHV: |
| 4178 | case SVt_PVAV: |
| 4179 | do_die: |
| 4180 | DIE(aTHX_ "Not a CODE reference"); |
| 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | /* At this point we want to save PL_savestack_ix, either by doing a |
| 4185 | * cx_pushsub(), or for XS, doing an ENTER. But we don't yet know the final |
| 4186 | * CV we will be using (so we don't know whether its XS, so we can't |
| 4187 | * cx_pushsub() or ENTER yet), and determining cv may itself push stuff on |
| 4188 | * the save stack. So remember where we are currently on the save |
| 4189 | * stack, and later update the CX or scopestack entry accordingly. */ |
| 4190 | old_savestack_ix = PL_savestack_ix; |
| 4191 | |
| 4192 | /* these two fields are in a union. If they ever become separate, |
| 4193 | * we have to test for both of them being null below */ |
| 4194 | assert(cv); |
| 4195 | assert((void*)&CvROOT(cv) == (void*)&CvXSUB(cv)); |
| 4196 | while (UNLIKELY(!CvROOT(cv))) { |
| 4197 | GV* autogv; |
| 4198 | SV* sub_name; |
| 4199 | |
| 4200 | /* anonymous or undef'd function leaves us no recourse */ |
| 4201 | if (CvLEXICAL(cv) && CvHASGV(cv)) |
| 4202 | DIE(aTHX_ "Undefined subroutine &%" SVf " called", |
| 4203 | SVfARG(cv_name(cv, NULL, 0))); |
| 4204 | if (CvANON(cv) || !CvHASGV(cv)) { |
| 4205 | DIE(aTHX_ "Undefined subroutine called"); |
| 4206 | } |
| 4207 | |
| 4208 | /* autoloaded stub? */ |
| 4209 | if (cv != GvCV(gv = CvGV(cv))) { |
| 4210 | cv = GvCV(gv); |
| 4211 | } |
| 4212 | /* should call AUTOLOAD now? */ |
| 4213 | else { |
| 4214 | try_autoload: |
| 4215 | autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), |
| 4216 | (GvNAMEUTF8(gv) ? SVf_UTF8 : 0) |
| 4217 | |(PL_op->op_flags & OPf_REF |
| 4218 | ? GV_AUTOLOAD_ISMETHOD |
| 4219 | : 0)); |
| 4220 | cv = autogv ? GvCV(autogv) : NULL; |
| 4221 | } |
| 4222 | if (!cv) { |
| 4223 | sub_name = sv_newmortal(); |
| 4224 | gv_efullname3(sub_name, gv, NULL); |
| 4225 | DIE(aTHX_ "Undefined subroutine &%" SVf " called", SVfARG(sub_name)); |
| 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | /* unrolled "CvCLONE(cv) && ! CvCLONED(cv)" */ |
| 4230 | if (UNLIKELY((CvFLAGS(cv) & (CVf_CLONE|CVf_CLONED)) == CVf_CLONE)) |
| 4231 | DIE(aTHX_ "Closure prototype called"); |
| 4232 | |
| 4233 | if (UNLIKELY((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) |
| 4234 | && !CvNODEBUG(cv))) |
| 4235 | { |
| 4236 | Perl_get_db_sub(aTHX_ &sv, cv); |
| 4237 | if (CvISXSUB(cv)) |
| 4238 | PL_curcopdb = PL_curcop; |
| 4239 | if (CvLVALUE(cv)) { |
| 4240 | /* check for lsub that handles lvalue subroutines */ |
| 4241 | cv = GvCV(gv_fetchpvs("DB::lsub", GV_ADDMULTI, SVt_PVCV)); |
| 4242 | /* if lsub not found then fall back to DB::sub */ |
| 4243 | if (!cv) cv = GvCV(PL_DBsub); |
| 4244 | } else { |
| 4245 | cv = GvCV(PL_DBsub); |
| 4246 | } |
| 4247 | |
| 4248 | if (!cv || (!CvXSUB(cv) && !CvSTART(cv))) |
| 4249 | DIE(aTHX_ "No DB::sub routine defined"); |
| 4250 | } |
| 4251 | |
| 4252 | if (!(CvISXSUB(cv))) { |
| 4253 | /* This path taken at least 75% of the time */ |
| 4254 | dMARK; |
| 4255 | PADLIST *padlist; |
| 4256 | I32 depth; |
| 4257 | bool hasargs; |
| 4258 | U8 gimme; |
| 4259 | |
| 4260 | /* keep PADTMP args alive throughout the call (we need to do this |
| 4261 | * because @_ isn't refcounted). Note that we create the mortals |
| 4262 | * in the caller's tmps frame, so they won't be freed until after |
| 4263 | * we return from the sub. |
| 4264 | */ |
| 4265 | { |
| 4266 | SV **svp = MARK; |
| 4267 | while (svp < SP) { |
| 4268 | SV *sv = *++svp; |
| 4269 | if (!sv) |
| 4270 | continue; |
| 4271 | if (SvPADTMP(sv)) |
| 4272 | *svp = sv = sv_mortalcopy(sv); |
| 4273 | SvTEMP_off(sv); |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | gimme = GIMME_V; |
| 4278 | cx = cx_pushblock(CXt_SUB, gimme, MARK, old_savestack_ix); |
| 4279 | hasargs = cBOOL(PL_op->op_flags & OPf_STACKED); |
| 4280 | cx_pushsub(cx, cv, PL_op->op_next, hasargs); |
| 4281 | |
| 4282 | padlist = CvPADLIST(cv); |
| 4283 | if (UNLIKELY((depth = ++CvDEPTH(cv)) >= 2)) |
| 4284 | pad_push(padlist, depth); |
| 4285 | PAD_SET_CUR_NOSAVE(padlist, depth); |
| 4286 | if (LIKELY(hasargs)) { |
| 4287 | AV *const av = MUTABLE_AV(PAD_SVl(0)); |
| 4288 | SSize_t items; |
| 4289 | AV **defavp; |
| 4290 | |
| 4291 | defavp = &GvAV(PL_defgv); |
| 4292 | cx->blk_sub.savearray = *defavp; |
| 4293 | *defavp = MUTABLE_AV(SvREFCNT_inc_simple_NN(av)); |
| 4294 | |
| 4295 | /* it's the responsibility of whoever leaves a sub to ensure |
| 4296 | * that a clean, empty AV is left in pad[0]. This is normally |
| 4297 | * done by cx_popsub() */ |
| 4298 | assert(!AvREAL(av) && AvFILLp(av) == -1); |
| 4299 | |
| 4300 | items = SP - MARK; |
| 4301 | if (UNLIKELY(items - 1 > AvMAX(av))) { |
| 4302 | SV **ary = AvALLOC(av); |
| 4303 | Renew(ary, items, SV*); |
| 4304 | AvMAX(av) = items - 1; |
| 4305 | AvALLOC(av) = ary; |
| 4306 | AvARRAY(av) = ary; |
| 4307 | } |
| 4308 | |
| 4309 | Copy(MARK+1,AvARRAY(av),items,SV*); |
| 4310 | AvFILLp(av) = items - 1; |
| 4311 | } |
| 4312 | if (UNLIKELY((cx->blk_u16 & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO && |
| 4313 | !CvLVALUE(cv))) |
| 4314 | DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf, |
| 4315 | SVfARG(cv_name(cv, NULL, 0))); |
| 4316 | /* warning must come *after* we fully set up the context |
| 4317 | * stuff so that __WARN__ handlers can safely dounwind() |
| 4318 | * if they want to |
| 4319 | */ |
| 4320 | if (UNLIKELY(depth == PERL_SUB_DEPTH_WARN |
| 4321 | && ckWARN(WARN_RECURSION) |
| 4322 | && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))) |
| 4323 | sub_crush_depth(cv); |
| 4324 | RETURNOP(CvSTART(cv)); |
| 4325 | } |
| 4326 | else { |
| 4327 | SSize_t markix = TOPMARK; |
| 4328 | bool is_scalar; |
| 4329 | |
| 4330 | ENTER; |
| 4331 | /* pretend we did the ENTER earlier */ |
| 4332 | PL_scopestack[PL_scopestack_ix - 1] = old_savestack_ix; |
| 4333 | |
| 4334 | SAVETMPS; |
| 4335 | PUTBACK; |
| 4336 | |
| 4337 | if (UNLIKELY(((PL_op->op_private |
| 4338 | & CX_PUSHSUB_GET_LVALUE_MASK(Perl_is_lvalue_sub) |
| 4339 | ) & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO && |
| 4340 | !CvLVALUE(cv))) |
| 4341 | DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf, |
| 4342 | SVfARG(cv_name(cv, NULL, 0))); |
| 4343 | |
| 4344 | if (UNLIKELY(!(PL_op->op_flags & OPf_STACKED) && GvAV(PL_defgv))) { |
| 4345 | /* Need to copy @_ to stack. Alternative may be to |
| 4346 | * switch stack to @_, and copy return values |
| 4347 | * back. This would allow popping @_ in XSUB, e.g.. XXXX */ |
| 4348 | AV * const av = GvAV(PL_defgv); |
| 4349 | const SSize_t items = AvFILL(av) + 1; |
| 4350 | |
| 4351 | if (items) { |
| 4352 | SSize_t i = 0; |
| 4353 | const bool m = cBOOL(SvRMAGICAL(av)); |
| 4354 | /* Mark is at the end of the stack. */ |
| 4355 | EXTEND(SP, items); |
| 4356 | for (; i < items; ++i) |
| 4357 | { |
| 4358 | SV *sv; |
| 4359 | if (m) { |
| 4360 | SV ** const svp = av_fetch(av, i, 0); |
| 4361 | sv = svp ? *svp : NULL; |
| 4362 | } |
| 4363 | else sv = AvARRAY(av)[i]; |
| 4364 | if (sv) SP[i+1] = sv; |
| 4365 | else { |
| 4366 | SP[i+1] = newSVavdefelem(av, i, 1); |
| 4367 | } |
| 4368 | } |
| 4369 | SP += items; |
| 4370 | PUTBACK ; |
| 4371 | } |
| 4372 | } |
| 4373 | else { |
| 4374 | SV **mark = PL_stack_base + markix; |
| 4375 | SSize_t items = SP - mark; |
| 4376 | while (items--) { |
| 4377 | mark++; |
| 4378 | if (*mark && SvPADTMP(*mark)) { |
| 4379 | *mark = sv_mortalcopy(*mark); |
| 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | /* We assume first XSUB in &DB::sub is the called one. */ |
| 4384 | if (UNLIKELY(PL_curcopdb)) { |
| 4385 | SAVEVPTR(PL_curcop); |
| 4386 | PL_curcop = PL_curcopdb; |
| 4387 | PL_curcopdb = NULL; |
| 4388 | } |
| 4389 | /* Do we need to open block here? XXXX */ |
| 4390 | |
| 4391 | /* calculate gimme here as PL_op might get changed and then not |
| 4392 | * restored until the LEAVE further down */ |
| 4393 | is_scalar = (GIMME_V == G_SCALAR); |
| 4394 | |
| 4395 | /* CvXSUB(cv) must not be NULL because newXS() refuses NULL xsub address */ |
| 4396 | assert(CvXSUB(cv)); |
| 4397 | CvXSUB(cv)(aTHX_ cv); |
| 4398 | |
| 4399 | /* Enforce some sanity in scalar context. */ |
| 4400 | if (is_scalar) { |
| 4401 | SV **svp = PL_stack_base + markix + 1; |
| 4402 | if (svp != PL_stack_sp) { |
| 4403 | *svp = svp > PL_stack_sp ? &PL_sv_undef : *PL_stack_sp; |
| 4404 | PL_stack_sp = svp; |
| 4405 | } |
| 4406 | } |
| 4407 | LEAVE; |
| 4408 | return NORMAL; |
| 4409 | } |
| 4410 | } |
| 4411 | |
| 4412 | void |
| 4413 | Perl_sub_crush_depth(pTHX_ CV *cv) |
| 4414 | { |
| 4415 | PERL_ARGS_ASSERT_SUB_CRUSH_DEPTH; |
| 4416 | |
| 4417 | if (CvANON(cv)) |
| 4418 | Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine"); |
| 4419 | else { |
| 4420 | Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%" SVf "\"", |
| 4421 | SVfARG(cv_name(cv,NULL,0))); |
| 4422 | } |
| 4423 | } |
| 4424 | |
| 4425 | |
| 4426 | |
| 4427 | /* like croak, but report in context of caller */ |
| 4428 | |
| 4429 | void |
| 4430 | Perl_croak_caller(const char *pat, ...) |
| 4431 | { |
| 4432 | dTHX; |
| 4433 | va_list args; |
| 4434 | const PERL_CONTEXT *cx = caller_cx(0, NULL); |
| 4435 | |
| 4436 | /* make error appear at call site */ |
| 4437 | assert(cx); |
| 4438 | PL_curcop = cx->blk_oldcop; |
| 4439 | |
| 4440 | va_start(args, pat); |
| 4441 | vcroak(pat, &args); |
| 4442 | NOT_REACHED; /* NOTREACHED */ |
| 4443 | va_end(args); |
| 4444 | } |
| 4445 | |
| 4446 | |
| 4447 | PP(pp_aelem) |
| 4448 | { |
| 4449 | dSP; |
| 4450 | SV** svp; |
| 4451 | SV* const elemsv = POPs; |
| 4452 | IV elem = SvIV(elemsv); |
| 4453 | AV *const av = MUTABLE_AV(POPs); |
| 4454 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
| 4455 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; |
| 4456 | const bool localizing = PL_op->op_private & OPpLVAL_INTRO; |
| 4457 | bool preeminent = TRUE; |
| 4458 | SV *sv; |
| 4459 | |
| 4460 | if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))) |
| 4461 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
| 4462 | "Use of reference \"%" SVf "\" as array index", |
| 4463 | SVfARG(elemsv)); |
| 4464 | if (UNLIKELY(SvTYPE(av) != SVt_PVAV)) |
| 4465 | RETPUSHUNDEF; |
| 4466 | |
| 4467 | if (UNLIKELY(localizing)) { |
| 4468 | MAGIC *mg; |
| 4469 | HV *stash; |
| 4470 | |
| 4471 | /* If we can determine whether the element exist, |
| 4472 | * Try to preserve the existenceness of a tied array |
| 4473 | * element by using EXISTS and DELETE if possible. |
| 4474 | * Fallback to FETCH and STORE otherwise. */ |
| 4475 | if (SvCANEXISTDELETE(av)) |
| 4476 | preeminent = av_exists(av, elem); |
| 4477 | } |
| 4478 | |
| 4479 | svp = av_fetch(av, elem, lval && !defer); |
| 4480 | if (lval) { |
| 4481 | #ifdef PERL_MALLOC_WRAP |
| 4482 | if (SvUOK(elemsv)) { |
| 4483 | const UV uv = SvUV(elemsv); |
| 4484 | elem = uv > IV_MAX ? IV_MAX : uv; |
| 4485 | } |
| 4486 | else if (SvNOK(elemsv)) |
| 4487 | elem = (IV)SvNV(elemsv); |
| 4488 | if (elem > 0) { |
| 4489 | static const char oom_array_extend[] = |
| 4490 | "Out of memory during array extend"; /* Duplicated in av.c */ |
| 4491 | MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend); |
| 4492 | } |
| 4493 | #endif |
| 4494 | if (!svp || !*svp) { |
| 4495 | IV len; |
| 4496 | if (!defer) |
| 4497 | DIE(aTHX_ PL_no_aelem, elem); |
| 4498 | len = av_tindex(av); |
| 4499 | mPUSHs(newSVavdefelem(av, |
| 4500 | /* Resolve a negative index now, unless it points before the |
| 4501 | beginning of the array, in which case record it for error |
| 4502 | reporting in magic_setdefelem. */ |
| 4503 | elem < 0 && len + elem >= 0 ? len + elem : elem, |
| 4504 | 1)); |
| 4505 | RETURN; |
| 4506 | } |
| 4507 | if (UNLIKELY(localizing)) { |
| 4508 | if (preeminent) |
| 4509 | save_aelem(av, elem, svp); |
| 4510 | else |
| 4511 | SAVEADELETE(av, elem); |
| 4512 | } |
| 4513 | else if (PL_op->op_private & OPpDEREF) { |
| 4514 | PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF)); |
| 4515 | RETURN; |
| 4516 | } |
| 4517 | } |
| 4518 | sv = (svp ? *svp : &PL_sv_undef); |
| 4519 | if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */ |
| 4520 | mg_get(sv); |
| 4521 | PUSHs(sv); |
| 4522 | RETURN; |
| 4523 | } |
| 4524 | |
| 4525 | SV* |
| 4526 | Perl_vivify_ref(pTHX_ SV *sv, U32 to_what) |
| 4527 | { |
| 4528 | PERL_ARGS_ASSERT_VIVIFY_REF; |
| 4529 | |
| 4530 | SvGETMAGIC(sv); |
| 4531 | if (!SvOK(sv)) { |
| 4532 | if (SvREADONLY(sv)) |
| 4533 | Perl_croak_no_modify(); |
| 4534 | prepare_SV_for_RV(sv); |
| 4535 | switch (to_what) { |
| 4536 | case OPpDEREF_SV: |
| 4537 | SvRV_set(sv, newSV(0)); |
| 4538 | break; |
| 4539 | case OPpDEREF_AV: |
| 4540 | SvRV_set(sv, MUTABLE_SV(newAV())); |
| 4541 | break; |
| 4542 | case OPpDEREF_HV: |
| 4543 | SvRV_set(sv, MUTABLE_SV(newHV())); |
| 4544 | break; |
| 4545 | } |
| 4546 | SvROK_on(sv); |
| 4547 | SvSETMAGIC(sv); |
| 4548 | SvGETMAGIC(sv); |
| 4549 | } |
| 4550 | if (SvGMAGICAL(sv)) { |
| 4551 | /* copy the sv without magic to prevent magic from being |
| 4552 | executed twice */ |
| 4553 | SV* msv = sv_newmortal(); |
| 4554 | sv_setsv_nomg(msv, sv); |
| 4555 | return msv; |
| 4556 | } |
| 4557 | return sv; |
| 4558 | } |
| 4559 | |
| 4560 | PERL_STATIC_INLINE HV * |
| 4561 | S_opmethod_stash(pTHX_ SV* meth) |
| 4562 | { |
| 4563 | SV* ob; |
| 4564 | HV* stash; |
| 4565 | |
| 4566 | SV* const sv = PL_stack_base + TOPMARK == PL_stack_sp |
| 4567 | ? (Perl_croak(aTHX_ "Can't call method \"%" SVf "\" without a " |
| 4568 | "package or object reference", SVfARG(meth)), |
| 4569 | (SV *)NULL) |
| 4570 | : *(PL_stack_base + TOPMARK + 1); |
| 4571 | |
| 4572 | PERL_ARGS_ASSERT_OPMETHOD_STASH; |
| 4573 | |
| 4574 | if (UNLIKELY(!sv)) |
| 4575 | undefined: |
| 4576 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on an undefined value", |
| 4577 | SVfARG(meth)); |
| 4578 | |
| 4579 | if (UNLIKELY(SvGMAGICAL(sv))) mg_get(sv); |
| 4580 | else if (SvIsCOW_shared_hash(sv)) { /* MyClass->meth() */ |
| 4581 | stash = gv_stashsv(sv, GV_CACHE_ONLY); |
| 4582 | if (stash) return stash; |
| 4583 | } |
| 4584 | |
| 4585 | if (SvROK(sv)) |
| 4586 | ob = MUTABLE_SV(SvRV(sv)); |
| 4587 | else if (!SvOK(sv)) goto undefined; |
| 4588 | else if (isGV_with_GP(sv)) { |
| 4589 | if (!GvIO(sv)) |
| 4590 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" " |
| 4591 | "without a package or object reference", |
| 4592 | SVfARG(meth)); |
| 4593 | ob = sv; |
| 4594 | if (SvTYPE(ob) == SVt_PVLV && LvTYPE(ob) == 'y') { |
| 4595 | assert(!LvTARGLEN(ob)); |
| 4596 | ob = LvTARG(ob); |
| 4597 | assert(ob); |
| 4598 | } |
| 4599 | *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV(ob)); |
| 4600 | } |
| 4601 | else { |
| 4602 | /* this isn't a reference */ |
| 4603 | GV* iogv; |
| 4604 | STRLEN packlen; |
| 4605 | const char * const packname = SvPV_nomg_const(sv, packlen); |
| 4606 | const U32 packname_utf8 = SvUTF8(sv); |
| 4607 | stash = gv_stashpvn(packname, packlen, packname_utf8 | GV_CACHE_ONLY); |
| 4608 | if (stash) return stash; |
| 4609 | |
| 4610 | if (!(iogv = gv_fetchpvn_flags( |
| 4611 | packname, packlen, packname_utf8, SVt_PVIO |
| 4612 | )) || |
| 4613 | !(ob=MUTABLE_SV(GvIO(iogv)))) |
| 4614 | { |
| 4615 | /* this isn't the name of a filehandle either */ |
| 4616 | if (!packlen) |
| 4617 | { |
| 4618 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" " |
| 4619 | "without a package or object reference", |
| 4620 | SVfARG(meth)); |
| 4621 | } |
| 4622 | /* assume it's a package name */ |
| 4623 | stash = gv_stashpvn(packname, packlen, packname_utf8); |
| 4624 | if (stash) return stash; |
| 4625 | else return MUTABLE_HV(sv); |
| 4626 | } |
| 4627 | /* it _is_ a filehandle name -- replace with a reference */ |
| 4628 | *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV(MUTABLE_SV(iogv))); |
| 4629 | } |
| 4630 | |
| 4631 | /* if we got here, ob should be an object or a glob */ |
| 4632 | if (!ob || !(SvOBJECT(ob) |
| 4633 | || (isGV_with_GP(ob) |
| 4634 | && (ob = MUTABLE_SV(GvIO((const GV *)ob))) |
| 4635 | && SvOBJECT(ob)))) |
| 4636 | { |
| 4637 | Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on unblessed reference", |
| 4638 | SVfARG((SvPOK(meth) && SvPVX(meth) == PL_isa_DOES) |
| 4639 | ? newSVpvs_flags("DOES", SVs_TEMP) |
| 4640 | : meth)); |
| 4641 | } |
| 4642 | |
| 4643 | return SvSTASH(ob); |
| 4644 | } |
| 4645 | |
| 4646 | PP(pp_method) |
| 4647 | { |
| 4648 | dSP; |
| 4649 | GV* gv; |
| 4650 | HV* stash; |
| 4651 | SV* const meth = TOPs; |
| 4652 | |
| 4653 | if (SvROK(meth)) { |
| 4654 | SV* const rmeth = SvRV(meth); |
| 4655 | if (SvTYPE(rmeth) == SVt_PVCV) { |
| 4656 | SETs(rmeth); |
| 4657 | RETURN; |
| 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | stash = opmethod_stash(meth); |
| 4662 | |
| 4663 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK); |
| 4664 | assert(gv); |
| 4665 | |
| 4666 | SETs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); |
| 4667 | RETURN; |
| 4668 | } |
| 4669 | |
| 4670 | #define METHOD_CHECK_CACHE(stash,cache,meth) \ |
| 4671 | const HE* const he = hv_fetch_ent(cache, meth, 0, 0); \ |
| 4672 | if (he) { \ |
| 4673 | gv = MUTABLE_GV(HeVAL(he)); \ |
| 4674 | if (isGV(gv) && GvCV(gv) && (!GvCVGEN(gv) || GvCVGEN(gv) \ |
| 4675 | == (PL_sub_generation + HvMROMETA(stash)->cache_gen))) \ |
| 4676 | { \ |
| 4677 | XPUSHs(MUTABLE_SV(GvCV(gv))); \ |
| 4678 | RETURN; \ |
| 4679 | } \ |
| 4680 | } \ |
| 4681 | |
| 4682 | PP(pp_method_named) |
| 4683 | { |
| 4684 | dSP; |
| 4685 | GV* gv; |
| 4686 | SV* const meth = cMETHOPx_meth(PL_op); |
| 4687 | HV* const stash = opmethod_stash(meth); |
| 4688 | |
| 4689 | if (LIKELY(SvTYPE(stash) == SVt_PVHV)) { |
| 4690 | METHOD_CHECK_CACHE(stash, stash, meth); |
| 4691 | } |
| 4692 | |
| 4693 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK); |
| 4694 | assert(gv); |
| 4695 | |
| 4696 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); |
| 4697 | RETURN; |
| 4698 | } |
| 4699 | |
| 4700 | PP(pp_method_super) |
| 4701 | { |
| 4702 | dSP; |
| 4703 | GV* gv; |
| 4704 | HV* cache; |
| 4705 | SV* const meth = cMETHOPx_meth(PL_op); |
| 4706 | HV* const stash = CopSTASH(PL_curcop); |
| 4707 | /* Actually, SUPER doesn't need real object's (or class') stash at all, |
| 4708 | * as it uses CopSTASH. However, we must ensure that object(class) is |
| 4709 | * correct (this check is done by S_opmethod_stash) */ |
| 4710 | opmethod_stash(meth); |
| 4711 | |
| 4712 | if ((cache = HvMROMETA(stash)->super)) { |
| 4713 | METHOD_CHECK_CACHE(stash, cache, meth); |
| 4714 | } |
| 4715 | |
| 4716 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER); |
| 4717 | assert(gv); |
| 4718 | |
| 4719 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); |
| 4720 | RETURN; |
| 4721 | } |
| 4722 | |
| 4723 | PP(pp_method_redir) |
| 4724 | { |
| 4725 | dSP; |
| 4726 | GV* gv; |
| 4727 | SV* const meth = cMETHOPx_meth(PL_op); |
| 4728 | HV* stash = gv_stashsv(cMETHOPx_rclass(PL_op), 0); |
| 4729 | opmethod_stash(meth); /* not used but needed for error checks */ |
| 4730 | |
| 4731 | if (stash) { METHOD_CHECK_CACHE(stash, stash, meth); } |
| 4732 | else stash = MUTABLE_HV(cMETHOPx_rclass(PL_op)); |
| 4733 | |
| 4734 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK); |
| 4735 | assert(gv); |
| 4736 | |
| 4737 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); |
| 4738 | RETURN; |
| 4739 | } |
| 4740 | |
| 4741 | PP(pp_method_redir_super) |
| 4742 | { |
| 4743 | dSP; |
| 4744 | GV* gv; |
| 4745 | HV* cache; |
| 4746 | SV* const meth = cMETHOPx_meth(PL_op); |
| 4747 | HV* stash = gv_stashsv(cMETHOPx_rclass(PL_op), 0); |
| 4748 | opmethod_stash(meth); /* not used but needed for error checks */ |
| 4749 | |
| 4750 | if (UNLIKELY(!stash)) stash = MUTABLE_HV(cMETHOPx_rclass(PL_op)); |
| 4751 | else if ((cache = HvMROMETA(stash)->super)) { |
| 4752 | METHOD_CHECK_CACHE(stash, cache, meth); |
| 4753 | } |
| 4754 | |
| 4755 | gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER); |
| 4756 | assert(gv); |
| 4757 | |
| 4758 | XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv)); |
| 4759 | RETURN; |
| 4760 | } |
| 4761 | |
| 4762 | /* |
| 4763 | * ex: set ts=8 sts=4 sw=4 et: |
| 4764 | */ |