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
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.
12 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
19 /* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
31 #include "invlist_inline.h"
33 #include "regcharclass.h"
35 /* variations on pp_null */
40 if (GIMME_V == G_SCALAR)
52 assert(SvTYPE(TARG) == SVt_PVCV);
67 CV * const protocv = PadnamePROTOCV(
68 PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG]
70 assert(SvTYPE(TARG) == SVt_PVCV);
72 if (CvISXSUB(protocv)) { /* constant */
73 /* XXX Should we clone it here? */
74 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
75 to introcv and remove the SvPADSTALE_off. */
76 SAVEPADSVANDMORTALIZE(ARGTARG);
77 PAD_SVl(ARGTARG) = SvREFCNT_inc_simple_NN(protocv);
80 if (CvROOT(protocv)) {
81 assert(CvCLONE(protocv));
82 assert(!CvCLONED(protocv));
84 cv_clone_into(protocv,(CV *)TARG);
85 SAVECLEARSV(PAD_SVl(ARGTARG));
92 /* In some cases this function inspects PL_op. If this function is called
93 for new op types, more bool parameters may need to be added in place of
96 When noinit is true, the absence of a gv will cause a retval of undef.
97 This is unrelated to the cv-to-gv assignment case.
101 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
104 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
107 sv = amagic_deref_call(sv, to_gv_amg);
111 if (SvTYPE(sv) == SVt_PVIO) {
112 GV * const gv = MUTABLE_GV(sv_newmortal());
113 gv_init(gv, 0, "__ANONIO__", 10, 0);
114 GvIOp(gv) = MUTABLE_IO(sv);
115 SvREFCNT_inc_void_NN(sv);
118 else if (!isGV_with_GP(sv)) {
119 Perl_die(aTHX_ "Not a GLOB reference");
123 if (!isGV_with_GP(sv)) {
125 /* If this is a 'my' scalar and flag is set then vivify
128 if (vivify_sv && sv != &PL_sv_undef) {
132 Perl_croak_no_modify();
133 gv = MUTABLE_GV(newSV_type(SVt_NULL));
134 stash = CopSTASH(PL_curcop);
135 if (SvTYPE(stash) != SVt_PVHV) stash = NULL;
136 if (cUNOP->op_targ) {
137 SV * const namesv = PAD_SV(cUNOP->op_targ);
138 gv_init_sv(gv, stash, namesv, 0);
141 gv_init_pv(gv, stash, "__ANONIO__", 0);
143 sv_setrv_noinc_mg(sv, MUTABLE_SV(gv));
146 if (PL_op->op_flags & OPf_REF || strict) {
147 Perl_die(aTHX_ PL_no_usym, "a symbol");
149 if (ckWARN(WARN_UNINITIALIZED))
155 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
156 sv, GV_ADDMG, SVt_PVGV
165 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
169 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
170 == OPpDONT_INIT_GV) {
171 /* We are the target of a coderef assignment. Return
172 the scalar unchanged, and let pp_sasssign deal with
176 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
178 /* FAKE globs in the symbol table cause weird bugs (#77810) */
182 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
183 SV *newsv = sv_mortalcopy_flags(sv, 0);
195 sv, PL_op->op_private & OPpDEREF,
196 PL_op->op_private & HINT_STRICT_REFS,
197 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
198 || PL_op->op_type == OP_READLINE
200 if (PL_op->op_private & OPpLVAL_INTRO)
201 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
206 /* Helper function for pp_rv2sv and pp_rv2av */
208 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
209 const svtype type, SV ***spp)
213 PERL_ARGS_ASSERT_SOFTREF2XV;
215 if (PL_op->op_private & HINT_STRICT_REFS) {
217 Perl_die(aTHX_ PL_no_symref_sv, sv,
218 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
220 Perl_die(aTHX_ PL_no_usym, what);
224 PL_op->op_flags & OPf_REF
226 Perl_die(aTHX_ PL_no_usym, what);
227 if (ckWARN(WARN_UNINITIALIZED))
229 if (type != SVt_PV && GIMME_V == G_LIST) {
233 **spp = &PL_sv_undef;
236 if ((PL_op->op_flags & OPf_SPECIAL) &&
237 !(PL_op->op_flags & OPf_MOD))
239 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
241 **spp = &PL_sv_undef;
246 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
259 sv = amagic_deref_call(sv, to_sv_amg);
263 if (SvTYPE(sv) >= SVt_PVAV)
264 DIE(aTHX_ "Not a SCALAR reference");
269 if (!isGV_with_GP(gv)) {
270 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
276 if (PL_op->op_flags & OPf_MOD) {
277 if (PL_op->op_private & OPpLVAL_INTRO) {
278 if (cUNOP->op_first->op_type == OP_NULL)
279 sv = save_scalar(MUTABLE_GV(TOPs));
281 sv = save_scalar(gv);
283 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
285 else if (PL_op->op_private & OPpDEREF)
286 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
288 SPAGAIN; /* in case chasing soft refs reallocated the stack */
296 AV * const av = MUTABLE_AV(TOPs);
297 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
299 SV ** const svp = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
301 *svp = newSV_type(SVt_PVMG);
302 sv_magic(*svp, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
306 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
315 if (PL_op->op_flags & OPf_MOD || LVRET) {
316 SV * const ret = newSV_type_mortal(SVt_PVLV);/* Not TARG RT#67838 */
317 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
319 LvTARG(ret) = SvREFCNT_inc_simple(sv);
320 SETs(ret); /* no SvSETMAGIC */
323 const MAGIC * const mg = mg_find_mglob(sv);
324 if (mg && mg->mg_len != -1) {
325 STRLEN i = mg->mg_len;
326 if (PL_op->op_private & OPpTRUEBOOL)
327 SETs(i ? &PL_sv_yes : &PL_sv_zero);
330 if (mg->mg_flags & MGf_BYTES && DO_UTF8(sv))
331 i = sv_pos_b2u_flags(sv, i, SV_GMAGIC|SV_CONST_RETURN);
346 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
348 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT))
349 == OPpMAY_RETURN_CONSTANT)
352 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
353 /* (But not in defined().) */
355 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
357 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
358 cv = SvTYPE(SvRV(gv)) == SVt_PVCV
359 ? MUTABLE_CV(SvRV(gv))
363 cv = MUTABLE_CV(&PL_sv_undef);
364 SETs(MUTABLE_SV(cv));
374 SV *ret = &PL_sv_undef;
376 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
377 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
378 const char * s = SvPVX_const(TOPs);
379 if (memBEGINs(s, SvCUR(TOPs), "CORE::")) {
380 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
382 DIE(aTHX_ "Can't find an opnumber for \"%" UTF8f "\"",
383 UTF8fARG(SvFLAGS(TOPs) & SVf_UTF8, SvCUR(TOPs)-6, s+6));
385 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
391 cv = sv_2cv(TOPs, &stash, &gv, 0);
393 ret = newSVpvn_flags(
394 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
404 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
406 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
408 PUSHs(MUTABLE_SV(cv));
422 if (GIMME_V != G_LIST) {
428 *MARK = &PL_sv_undef;
430 *MARK = refto(*MARK);
434 EXTEND_MORTAL(SP - MARK);
436 *MARK = refto(*MARK);
441 S_refto(pTHX_ SV *sv)
445 PERL_ARGS_ASSERT_REFTO;
447 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
450 if (!(sv = LvTARG(sv)))
453 SvREFCNT_inc_void_NN(sv);
455 else if (SvTYPE(sv) == SVt_PVAV) {
456 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
457 av_reify(MUTABLE_AV(sv));
459 SvREFCNT_inc_void_NN(sv);
461 else if (SvPADTMP(sv)) {
464 else if (UNLIKELY(SvSMAGICAL(sv) && mg_find(sv, PERL_MAGIC_nonelem)))
465 sv_unmagic(SvREFCNT_inc_simple_NN(sv), PERL_MAGIC_nonelem);
468 SvREFCNT_inc_void_NN(sv);
470 rv = newSV_type_mortal(SVt_IV);
471 sv_setrv_noinc(rv, sv);
478 SV * const sv = TOPs;
486 /* op is in boolean context? */
487 if ( (PL_op->op_private & OPpTRUEBOOL)
488 || ( (PL_op->op_private & OPpMAYBE_TRUEBOOL)
489 && block_gimme() == G_VOID))
491 /* refs are always true - unless it's to an object blessed into a
492 * class with a false name, i.e. "0". So we have to check for
493 * that remote possibility. The following is is basically an
494 * unrolled SvTRUE(sv_reftype(rv)) */
495 SV * const rv = SvRV(sv);
497 HV *stash = SvSTASH(rv);
498 HEK *hek = HvNAME_HEK(stash);
500 I32 len = HEK_LEN(hek);
501 /* bail out and do it the hard way? */
504 || (len == 1 && HEK_KEY(hek)[0] == '0')
517 sv_ref(TARG, SvRV(sv), TRUE);
533 stash = CopSTASH(PL_curcop);
534 if (SvTYPE(stash) != SVt_PVHV)
535 Perl_croak(aTHX_ "Attempt to bless into a freed package");
538 SV * const ssv = POPs;
542 if (!ssv) goto curstash;
545 if (!SvAMAGIC(ssv)) {
547 Perl_croak(aTHX_ "Attempt to bless into a reference");
549 /* SvAMAGIC is on here, but it only means potentially overloaded,
550 so after stringification: */
551 ptr = SvPV_nomg_const(ssv,len);
552 /* We need to check the flag again: */
553 if (!SvAMAGIC(ssv)) goto frog;
555 else ptr = SvPV_nomg_const(ssv,len);
557 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
558 "Explicit blessing to '' (assuming package main)");
559 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
562 (void)sv_bless(TOPs, stash);
572 const char * const elem = SvPV_const(sv, len);
573 GV * const gv = MUTABLE_GV(TOPs);
578 /* elem will always be NUL terminated. */
581 if (memEQs(elem, len, "ARRAY"))
583 tmpRef = MUTABLE_SV(GvAV(gv));
584 if (tmpRef && !AvREAL((const AV *)tmpRef)
585 && AvREIFY((const AV *)tmpRef))
586 av_reify(MUTABLE_AV(tmpRef));
590 if (memEQs(elem, len, "CODE"))
591 tmpRef = MUTABLE_SV(GvCVu(gv));
594 if (memEQs(elem, len, "FILEHANDLE")) {
595 tmpRef = MUTABLE_SV(GvIOp(gv));
598 if (memEQs(elem, len, "FORMAT"))
599 tmpRef = MUTABLE_SV(GvFORM(gv));
602 if (memEQs(elem, len, "GLOB"))
603 tmpRef = MUTABLE_SV(gv);
606 if (memEQs(elem, len, "HASH"))
607 tmpRef = MUTABLE_SV(GvHV(gv));
610 if (memEQs(elem, len, "IO"))
611 tmpRef = MUTABLE_SV(GvIOp(gv));
614 if (memEQs(elem, len, "NAME"))
615 sv = newSVhek(GvNAME_HEK(gv));
618 if (memEQs(elem, len, "PACKAGE")) {
619 const HV * const stash = GvSTASH(gv);
620 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
621 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
625 if (memEQs(elem, len, "SCALAR"))
640 /* Pattern matching */
648 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
649 /* Historically, study was skipped in these cases. */
654 /* Make study a no-op. It's no longer useful and its existence
655 complicates matters elsewhere. */
661 /* also used for: pp_transr() */
668 if (PL_op->op_flags & OPf_STACKED)
673 sv = PAD_SV(ARGTARG);
678 if(PL_op->op_type == OP_TRANSR) {
680 const char * const pv = SvPV(sv,len);
681 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
686 Size_t i = do_trans(sv);
692 /* Lvalue operators. */
695 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
701 PERL_ARGS_ASSERT_DO_CHOMP;
703 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
705 if (SvTYPE(sv) == SVt_PVAV) {
707 AV *const av = MUTABLE_AV(sv);
708 const I32 max = AvFILL(av);
710 for (i = 0; i <= max; i++) {
711 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
712 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
713 count += do_chomp(retval, sv, chomping);
717 else if (SvTYPE(sv) == SVt_PVHV) {
718 HV* const hv = MUTABLE_HV(sv);
720 (void)hv_iterinit(hv);
721 while ((entry = hv_iternext(hv)))
722 count += do_chomp(retval, hv_iterval(hv,entry), chomping);
725 else if (SvREADONLY(sv)) {
726 Perl_croak_no_modify();
732 char *temp_buffer = NULL;
737 goto nope_free_nothing;
739 while (len && s[-1] == '\n') {
746 STRLEN rslen, rs_charlen;
747 const char *rsptr = SvPV_const(PL_rs, rslen);
749 rs_charlen = SvUTF8(PL_rs)
753 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
754 /* Assumption is that rs is shorter than the scalar. */
756 /* RS is utf8, scalar is 8 bit. */
758 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
761 /* Cannot downgrade, therefore cannot possibly match.
762 At this point, temp_buffer is not alloced, and
763 is the buffer inside PL_rs, so dont free it.
765 assert (temp_buffer == rsptr);
771 /* RS is 8 bit, scalar is utf8. */
772 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
786 if (memNE(s, rsptr, rslen))
791 SvPV_force_nomg_nolen(sv);
798 Safefree(temp_buffer);
800 SvREFCNT_dec(svrecode);
804 if (len && (!SvPOK(sv) || SvIsCOW(sv)))
805 s = SvPV_force_nomg(sv, len);
808 char * const send = s + len;
809 char * const start = s;
811 while (s > start && UTF8_IS_CONTINUATION(*s))
813 if (is_utf8_string((U8*)s, send - s)) {
814 sv_setpvn(retval, s, send - s);
816 SvCUR_set(sv, s - start);
826 sv_setpvn(retval, s, 1);
840 /* also used for: pp_schomp() */
845 const bool chomping = PL_op->op_type == OP_SCHOMP;
847 const size_t count = do_chomp(TARG, TOPs, chomping);
849 sv_setiv(TARG, count);
855 /* also used for: pp_chomp() */
859 dSP; dMARK; dTARGET; dORIGMARK;
860 const bool chomping = PL_op->op_type == OP_CHOMP;
864 count += do_chomp(TARG, *++MARK, chomping);
866 sv_setiv(TARG, count);
877 if (!PL_op->op_private) {
882 if (PL_op->op_private & OPpTARGET_MY) {
883 SV** const padentry = &PAD_SVl(PL_op->op_targ);
885 EXTEND(SP,1);sp++;PUTBACK;
886 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) == OPpLVAL_INTRO) {
887 save_clearsv(padentry);
899 if (SvTHINKFIRST(sv))
900 sv_force_normal_flags(sv, SV_COW_DROP_PV|SV_IMMEDIATE_UNREF);
902 switch (SvTYPE(sv)) {
906 av_undef(MUTABLE_AV(sv));
909 hv_undef(MUTABLE_HV(sv));
912 if (cv_const_sv((const CV *)sv))
913 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
914 "Constant subroutine %" SVf " undefined",
915 SVfARG(CvANON((const CV *)sv)
916 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
919 ? CvNAME_HEK((CV *)sv)
920 : GvENAME_HEK(CvGV((const CV *)sv))
925 /* let user-undef'd sub keep its identity */
926 cv_undef_flags(MUTABLE_CV(sv), CV_UNDEF_KEEP_NAME);
929 assert(isGV_with_GP(sv));
935 /* undef *Pkg::meth_name ... */
937 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
938 && HvENAME_get(stash);
940 if((stash = GvHV((const GV *)sv))) {
941 if(HvENAME_get(stash))
942 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
946 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
947 gp_free(MUTABLE_GV(sv));
949 GvGP_set(sv, gp_ref(gp));
950 #ifndef PERL_DONT_CREATE_GVSV
951 GvSV(sv) = newSV_type(SVt_NULL);
953 GvLINE(sv) = CopLINE(PL_curcop);
954 GvEGV(sv) = MUTABLE_GV(sv);
958 mro_package_moved(NULL, stash, (const GV *)sv, 0);
960 /* undef *Foo::ISA */
961 if( strEQ(GvNAME((const GV *)sv), "ISA")
962 && (stash = GvSTASH((const GV *)sv))
963 && (method_changed || HvENAME(stash)) )
964 mro_isa_changed_in(stash);
965 else if(method_changed)
966 mro_method_changed_in(
967 GvSTASH((const GV *)sv)
973 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)
974 && !(PL_op->op_private & OPpUNDEF_KEEP_PV)
985 if (PL_op->op_private & OPpTARGET_MY)
993 /* common "slow" code for pp_postinc and pp_postdec */
996 S_postincdec_common(pTHX_ SV *sv, SV *targ)
1000 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1003 TARG = sv_newmortal();
1010 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1011 if (inc && !SvOK(TARG))
1018 /* also used for: pp_i_postinc() */
1025 /* special-case sv being a simple integer */
1026 if (LIKELY(((sv->sv_flags &
1027 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1028 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1030 && SvIVX(sv) != IV_MAX)
1033 SvIV_set(sv, iv + 1);
1034 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1039 return S_postincdec_common(aTHX_ sv, TARG);
1043 /* also used for: pp_i_postdec() */
1050 /* special-case sv being a simple integer */
1051 if (LIKELY(((sv->sv_flags &
1052 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1053 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1055 && SvIVX(sv) != IV_MIN)
1058 SvIV_set(sv, iv - 1);
1059 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1064 return S_postincdec_common(aTHX_ sv, TARG);
1068 /* Ordinary operators. */
1072 dSP; dATARGET; SV *svl, *svr;
1073 #ifdef PERL_PRESERVE_IVUV
1076 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1079 #ifdef PERL_PRESERVE_IVUV
1080 /* For integer to integer power, we do the calculation by hand wherever
1081 we're sure it is safe; otherwise we call pow() and try to convert to
1082 integer afterwards. */
1083 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1091 const IV iv = SvIVX(svr);
1095 goto float_it; /* Can't do negative powers this way. */
1099 baseuok = SvUOK(svl);
1101 baseuv = SvUVX(svl);
1103 const IV iv = SvIVX(svl);
1106 baseuok = TRUE; /* effectively it's a UV now */
1108 baseuv = -iv; /* abs, baseuok == false records sign */
1111 /* now we have integer ** positive integer. */
1114 /* foo & (foo - 1) is zero only for a power of 2. */
1115 if (!(baseuv & (baseuv - 1))) {
1116 /* We are raising power-of-2 to a positive integer.
1117 The logic here will work for any base (even non-integer
1118 bases) but it can be less accurate than
1119 pow (base,power) or exp (power * log (base)) when the
1120 intermediate values start to spill out of the mantissa.
1121 With powers of 2 we know this can't happen.
1122 And powers of 2 are the favourite thing for perl
1123 programmers to notice ** not doing what they mean. */
1125 NV base = baseuok ? baseuv : -(NV)baseuv;
1130 while (power >>= 1) {
1138 SvIV_please_nomg(svr);
1141 unsigned int highbit = 8 * sizeof(UV);
1142 unsigned int diff = 8 * sizeof(UV);
1143 while (diff >>= 1) {
1145 if (baseuv >> highbit) {
1149 /* we now have baseuv < 2 ** highbit */
1150 if (power * highbit <= 8 * sizeof(UV)) {
1151 /* result will definitely fit in UV, so use UV math
1152 on same algorithm as above */
1155 const bool odd_power = cBOOL(power & 1);
1159 while (power >>= 1) {
1166 if (baseuok || !odd_power)
1167 /* answer is positive */
1169 else if (result <= (UV)IV_MAX)
1170 /* answer negative, fits in IV */
1171 SETi( -(IV)result );
1172 else if (result == (UV)IV_MIN)
1173 /* 2's complement assumption: special case IV_MIN */
1176 /* answer negative, doesn't fit */
1177 SETn( -(NV)result );
1185 NV right = SvNV_nomg(svr);
1186 NV left = SvNV_nomg(svl);
1189 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1191 We are building perl with long double support and are on an AIX OS
1192 afflicted with a powl() function that wrongly returns NaNQ for any
1193 negative base. This was reported to IBM as PMR #23047-379 on
1194 03/06/2006. The problem exists in at least the following versions
1195 of AIX and the libm fileset, and no doubt others as well:
1197 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1198 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1199 AIX 5.2.0 bos.adt.libm 5.2.0.85
1201 So, until IBM fixes powl(), we provide the following workaround to
1202 handle the problem ourselves. Our logic is as follows: for
1203 negative bases (left), we use fmod(right, 2) to check if the
1204 exponent is an odd or even integer:
1206 - if odd, powl(left, right) == -powl(-left, right)
1207 - if even, powl(left, right) == powl(-left, right)
1209 If the exponent is not an integer, the result is rightly NaNQ, so
1210 we just return that (as NV_NAN).
1214 NV mod2 = Perl_fmod( right, 2.0 );
1215 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1216 SETn( -Perl_pow( -left, right) );
1217 } else if (mod2 == 0.0) { /* even integer */
1218 SETn( Perl_pow( -left, right) );
1219 } else { /* fractional power */
1223 SETn( Perl_pow( left, right) );
1225 #elif IVSIZE == 4 && defined(LONGDOUBLE_DOUBLEDOUBLE) && defined(USE_LONG_DOUBLE)
1227 Under these conditions, if a known libm bug exists, Perl_pow() could return
1228 an incorrect value if the correct value is an integer in the range of around
1229 25 or more bits. The error is always quite small, so we work around it by
1230 rounding to the nearest integer value ... but only if is_int is true.
1231 See https://github.com/Perl/perl5/issues/19625.
1235 SETn( roundl( Perl_pow( left, right) ) );
1237 else SETn( Perl_pow( left, right) );
1240 SETn( Perl_pow( left, right) );
1241 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1243 #ifdef PERL_PRESERVE_IVUV
1245 SvIV_please_nomg(svr);
1253 dSP; dATARGET; SV *svl, *svr;
1254 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1258 #ifdef PERL_PRESERVE_IVUV
1260 /* special-case some simple common cases */
1261 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1263 U32 flags = (svl->sv_flags & svr->sv_flags);
1264 if (flags & SVf_IOK) {
1265 /* both args are simple IVs */
1270 topl = ((UV)il) >> (UVSIZE * 4 - 1);
1271 topr = ((UV)ir) >> (UVSIZE * 4 - 1);
1273 /* if both are in a range that can't under/overflow, do a
1274 * simple integer multiply: if the top halves(*) of both numbers
1275 * are 00...00 or 11...11, then it's safe.
1276 * (*) for 32-bits, the "top half" is the top 17 bits,
1277 * for 64-bits, its 33 bits */
1279 ((topl+1) | (topr+1))
1280 & ( (((UV)1) << (UVSIZE * 4 + 1)) - 2) /* 11..110 */
1283 TARGi(il * ir, 0); /* args not GMG, so can't be tainted */
1289 else if (flags & SVf_NOK) {
1290 /* both args are NVs */
1295 if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
1296 /* nothing was lost by converting to IVs */
1301 # if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
1302 if (Perl_isinf(result)) {
1303 Zero((U8*)&result + 8, 8, U8);
1306 TARGn(result, 0); /* args not GMG, so can't be tainted */
1314 if (SvIV_please_nomg(svr)) {
1315 /* Unless the left argument is integer in range we are going to have to
1316 use NV maths. Hence only attempt to coerce the right argument if
1317 we know the left is integer. */
1318 /* Left operand is defined, so is it IV? */
1319 if (SvIV_please_nomg(svl)) {
1320 bool auvok = SvUOK(svl);
1321 bool buvok = SvUOK(svr);
1322 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1323 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1332 const IV aiv = SvIVX(svl);
1335 auvok = TRUE; /* effectively it's a UV now */
1337 /* abs, auvok == false records sign; Using 0- here and
1338 * later to silence bogus warning from MS VC */
1339 alow = (UV) (0 - (UV) aiv);
1345 const IV biv = SvIVX(svr);
1348 buvok = TRUE; /* effectively it's a UV now */
1350 /* abs, buvok == false records sign */
1351 blow = (UV) (0 - (UV) biv);
1355 /* If this does sign extension on unsigned it's time for plan B */
1356 ahigh = alow >> (4 * sizeof (UV));
1358 bhigh = blow >> (4 * sizeof (UV));
1360 if (ahigh && bhigh) {
1362 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1363 which is overflow. Drop to NVs below. */
1364 } else if (!ahigh && !bhigh) {
1365 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1366 so the unsigned multiply cannot overflow. */
1367 const UV product = alow * blow;
1368 if (auvok == buvok) {
1369 /* -ve * -ve or +ve * +ve gives a +ve result. */
1373 } else if (product <= (UV)IV_MIN) {
1374 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1375 /* -ve result, which could overflow an IV */
1377 /* can't negate IV_MIN, but there are aren't two
1378 * integers such that !ahigh && !bhigh, where the
1379 * product equals 0x800....000 */
1380 assert(product != (UV)IV_MIN);
1381 SETi( -(IV)product );
1383 } /* else drop to NVs below. */
1385 /* One operand is large, 1 small */
1388 /* swap the operands */
1390 bhigh = blow; /* bhigh now the temp var for the swap */
1394 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1395 multiplies can't overflow. shift can, add can, -ve can. */
1396 product_middle = ahigh * blow;
1397 if (!(product_middle & topmask)) {
1398 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1400 product_middle <<= (4 * sizeof (UV));
1401 product_low = alow * blow;
1403 /* as for pp_add, UV + something mustn't get smaller.
1404 IIRC ANSI mandates this wrapping *behaviour* for
1405 unsigned whatever the actual representation*/
1406 product_low += product_middle;
1407 if (product_low >= product_middle) {
1408 /* didn't overflow */
1409 if (auvok == buvok) {
1410 /* -ve * -ve or +ve * +ve gives a +ve result. */
1412 SETu( product_low );
1414 } else if (product_low <= (UV)IV_MIN) {
1415 /* 2s complement assumption again */
1416 /* -ve result, which could overflow an IV */
1418 SETi(product_low == (UV)IV_MIN
1419 ? IV_MIN : -(IV)product_low);
1421 } /* else drop to NVs below. */
1423 } /* product_middle too large */
1424 } /* ahigh && bhigh */
1429 NV right = SvNV_nomg(svr);
1430 NV left = SvNV_nomg(svl);
1431 NV result = left * right;
1434 #if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
1435 if (Perl_isinf(result)) {
1436 Zero((U8*)&result + 8, 8, U8);
1446 dSP; dATARGET; SV *svl, *svr;
1447 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1450 /* Only try to do UV divide first
1451 if ((SLOPPYDIVIDE is true) or
1452 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1454 The assumption is that it is better to use floating point divide
1455 whenever possible, only doing integer divide first if we can't be sure.
1456 If NV_PRESERVES_UV is true then we know at compile time that no UV
1457 can be too large to preserve, so don't need to compile the code to
1458 test the size of UVs. */
1460 #if defined(SLOPPYDIVIDE) || (defined(PERL_PRESERVE_IVUV) && !defined(NV_PRESERVES_UV))
1461 # define PERL_TRY_UV_DIVIDE
1462 /* ensure that 20./5. == 4. */
1465 #ifdef PERL_TRY_UV_DIVIDE
1466 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1467 bool left_non_neg = SvUOK(svl);
1468 bool right_non_neg = SvUOK(svr);
1472 if (right_non_neg) {
1476 const IV biv = SvIVX(svr);
1479 right_non_neg = TRUE; /* effectively it's a UV now */
1485 /* historically undef()/0 gives a "Use of uninitialized value"
1486 warning before dieing, hence this test goes here.
1487 If it were immediately before the second SvIV_please, then
1488 DIE() would be invoked before left was even inspected, so
1489 no inspection would give no warning. */
1491 DIE(aTHX_ "Illegal division by zero");
1497 const IV aiv = SvIVX(svl);
1500 left_non_neg = TRUE; /* effectively it's a UV now */
1509 /* For sloppy divide we always attempt integer division. */
1511 /* Otherwise we only attempt it if either or both operands
1512 would not be preserved by an NV. If both fit in NVs
1513 we fall through to the NV divide code below. However,
1514 as left >= right to ensure integer result here, we know that
1515 we can skip the test on the right operand - right big
1516 enough not to be preserved can't get here unless left is
1519 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1522 /* Integer division can't overflow, but it can be imprecise. */
1524 /* Modern compilers optimize division followed by
1525 * modulo into a single div instruction */
1526 const UV result = left / right;
1527 if (left % right == 0) {
1528 SP--; /* result is valid */
1529 if (left_non_neg == right_non_neg) {
1530 /* signs identical, result is positive. */
1534 /* 2s complement assumption */
1535 if (result <= (UV)IV_MIN)
1536 SETi(result == (UV)IV_MIN ? IV_MIN : -(IV)result);
1538 /* It's exact but too negative for IV. */
1539 SETn( -(NV)result );
1542 } /* tried integer divide but it was not an integer result */
1543 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1544 } /* one operand wasn't SvIOK */
1545 #endif /* PERL_TRY_UV_DIVIDE */
1547 NV right = SvNV_nomg(svr);
1548 NV left = SvNV_nomg(svl);
1549 (void)POPs;(void)POPs;
1550 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1551 if (! Perl_isnan(right) && right == 0.0)
1555 DIE(aTHX_ "Illegal division by zero");
1556 PUSHn( left / right );
1564 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1568 bool left_neg = FALSE;
1569 bool right_neg = FALSE;
1570 bool use_double = FALSE;
1571 bool dright_valid = FALSE;
1574 SV * const svr = TOPs;
1575 SV * const svl = TOPm1s;
1576 if (SvIV_please_nomg(svr)) {
1577 right_neg = !SvUOK(svr);
1581 const IV biv = SvIVX(svr);
1584 right_neg = FALSE; /* effectively it's a UV now */
1586 right = (UV) (0 - (UV) biv);
1591 dright = SvNV_nomg(svr);
1592 right_neg = dright < 0;
1595 if (dright < UV_MAX_P1) {
1596 right = U_V(dright);
1597 dright_valid = TRUE; /* In case we need to use double below. */
1603 /* At this point use_double is only true if right is out of range for
1604 a UV. In range NV has been rounded down to nearest UV and
1605 use_double false. */
1606 if (!use_double && SvIV_please_nomg(svl)) {
1607 left_neg = !SvUOK(svl);
1611 const IV aiv = SvIVX(svl);
1614 left_neg = FALSE; /* effectively it's a UV now */
1616 left = (UV) (0 - (UV) aiv);
1621 dleft = SvNV_nomg(svl);
1622 left_neg = dleft < 0;
1626 /* This should be exactly the 5.6 behaviour - if left and right are
1627 both in range for UV then use U_V() rather than floor. */
1629 if (dleft < UV_MAX_P1) {
1630 /* right was in range, so is dleft, so use UVs not double.
1634 /* left is out of range for UV, right was in range, so promote
1635 right (back) to double. */
1637 /* The +0.5 is used in 5.6 even though it is not strictly
1638 consistent with the implicit +0 floor in the U_V()
1639 inside the #if 1. */
1640 dleft = Perl_floor(dleft + 0.5);
1643 dright = Perl_floor(dright + 0.5);
1654 DIE(aTHX_ "Illegal modulus zero");
1656 dans = Perl_fmod(dleft, dright);
1657 if ((left_neg != right_neg) && dans)
1658 dans = dright - dans;
1661 sv_setnv(TARG, dans);
1667 DIE(aTHX_ "Illegal modulus zero");
1670 if ((left_neg != right_neg) && ans)
1673 /* XXX may warn: unary minus operator applied to unsigned type */
1674 /* could change -foo to be (~foo)+1 instead */
1675 if (ans <= ~((UV)IV_MAX)+1)
1676 sv_setiv(TARG, ~ans+1);
1678 sv_setnv(TARG, -(NV)ans);
1681 sv_setuv(TARG, ans);
1693 bool infnan = FALSE;
1694 const U8 gimme = GIMME_V;
1696 if (gimme == G_LIST && PL_op->op_private & OPpREPEAT_DOLIST) {
1697 /* TODO: think of some way of doing list-repeat overloading ??? */
1702 if (UNLIKELY(PL_op->op_private & OPpREPEAT_DOLIST)) {
1703 /* The parser saw this as a list repeat, and there
1704 are probably several items on the stack. But we're
1705 in scalar/void context, and there's no pp_list to save us
1706 now. So drop the rest of the items -- robin@kitsite.com
1709 if (MARK + 1 < SP) {
1715 ASSUME(MARK + 1 == SP);
1718 MARK[1] = &PL_sv_undef;
1722 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1728 const UV uv = SvUV_nomg(sv);
1730 count = IV_MAX; /* The best we can do? */
1734 count = SvIV_nomg(sv);
1737 else if (SvNOKp(sv)) {
1738 const NV nv = SvNV_nomg(sv);
1739 infnan = Perl_isinfnan(nv);
1740 if (UNLIKELY(infnan)) {
1744 count = -1; /* An arbitrary negative integer */
1750 count = SvIV_nomg(sv);
1753 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
1754 "Non-finite repeat count does nothing");
1755 } else if (count < 0) {
1757 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
1758 "Negative repeat count does nothing");
1761 if (gimme == G_LIST && PL_op->op_private & OPpREPEAT_DOLIST) {
1763 const SSize_t items = SP - MARK;
1764 const U8 mod = PL_op->op_flags & OPf_MOD;
1769 if ( items > SSize_t_MAX / count /* max would overflow */
1770 /* repeatcpy would overflow */
1771 || items > I32_MAX / (I32)sizeof(SV *)
1773 Perl_croak(aTHX_ "%s","Out of memory during list extend");
1774 max = items * count;
1779 if (mod && SvPADTMP(*SP)) {
1780 *SP = sv_mortalcopy(*SP);
1787 repeatcpy((char*)(MARK + items), (char*)MARK,
1788 items * sizeof(const SV *), count - 1);
1791 else if (count <= 0)
1794 else { /* Note: mark already snarfed by pp_list */
1795 SV * const tmpstr = POPs;
1800 sv_setsv_nomg(TARG, tmpstr);
1801 SvPV_force_nomg(TARG, len);
1802 isutf = DO_UTF8(TARG);
1809 if ( len > (MEM_SIZE_MAX-1) / (UV)count /* max would overflow */
1810 || len > (U32)I32_MAX /* repeatcpy would overflow */
1812 Perl_croak(aTHX_ "%s",
1813 "Out of memory during string extend");
1814 max = (UV)count * len + 1;
1817 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1818 SvCUR_set(TARG, SvCUR(TARG) * count);
1820 *SvEND(TARG) = '\0';
1823 (void)SvPOK_only_UTF8(TARG);
1825 (void)SvPOK_only(TARG);
1834 dSP; dATARGET; bool useleft; SV *svl, *svr;
1835 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1839 #ifdef PERL_PRESERVE_IVUV
1841 /* special-case some simple common cases */
1842 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1844 U32 flags = (svl->sv_flags & svr->sv_flags);
1845 if (flags & SVf_IOK) {
1846 /* both args are simple IVs */
1851 topl = ((UV)il) >> (UVSIZE * 8 - 2);
1852 topr = ((UV)ir) >> (UVSIZE * 8 - 2);
1854 /* if both are in a range that can't under/overflow, do a
1855 * simple integer subtract: if the top of both numbers
1856 * are 00 or 11, then it's safe */
1857 if (!( ((topl+1) | (topr+1)) & 2)) {
1859 TARGi(il - ir, 0); /* args not GMG, so can't be tainted */
1865 else if (flags & SVf_NOK) {
1866 /* both args are NVs */
1870 if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
1871 /* nothing was lost by converting to IVs */
1875 TARGn(nl - nr, 0); /* args not GMG, so can't be tainted */
1883 useleft = USE_LEFT(svl);
1884 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1885 "bad things" happen if you rely on signed integers wrapping. */
1886 if (SvIV_please_nomg(svr)) {
1887 /* Unless the left argument is integer in range we are going to have to
1888 use NV maths. Hence only attempt to coerce the right argument if
1889 we know the left is integer. */
1896 a_valid = auvok = 1;
1897 /* left operand is undef, treat as zero. */
1899 /* Left operand is defined, so is it IV? */
1900 if (SvIV_please_nomg(svl)) {
1901 if ((auvok = SvUOK(svl)))
1904 const IV aiv = SvIVX(svl);
1907 auvok = 1; /* Now acting as a sign flag. */
1909 auv = (UV) (0 - (UV) aiv);
1916 bool result_good = 0;
1919 bool buvok = SvUOK(svr);
1924 const IV biv = SvIVX(svr);
1929 buv = (UV) (0 - (UV) biv);
1931 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1932 else "IV" now, independent of how it came in.
1933 if a, b represents positive, A, B negative, a maps to -A etc
1938 all UV maths. negate result if A negative.
1939 subtract if signs same, add if signs differ. */
1941 if (auvok ^ buvok) {
1950 /* Must get smaller */
1955 if (result <= buv) {
1956 /* result really should be -(auv-buv). as its negation
1957 of true value, need to swap our result flag */
1969 if (result <= (UV)IV_MIN)
1970 SETi(result == (UV)IV_MIN
1971 ? IV_MIN : -(IV)result);
1973 /* result valid, but out of range for IV. */
1974 SETn( -(NV)result );
1978 } /* Overflow, drop through to NVs. */
1982 useleft = USE_LEFT(svl);
1985 NV value = SvNV_nomg(svr);
1989 /* left operand is undef, treat as zero - value */
1993 SETn( SvNV_nomg(svl) - value );
1998 #define IV_BITS (IVSIZE * 8)
2000 /* Taking the right operand of bitwise shift operators, returns an int
2001 * indicating the shift amount clipped to the range [-IV_BITS, +IV_BITS].
2004 S_shift_amount(pTHX_ SV *const svr)
2006 const IV iv = SvIV_nomg(svr);
2008 /* Note that [INT_MIN, INT_MAX] cannot be used as the clipping bound;
2009 * INT_MIN will cause overflow in "shift = -shift;" in S_{iv,uv}_shift.
2012 return SvUVX(svr) > IV_BITS ? IV_BITS : (int)SvUVX(svr);
2013 return iv < -IV_BITS ? -IV_BITS : iv > IV_BITS ? IV_BITS : (int)iv;
2016 static UV S_uv_shift(UV uv, int shift, bool left)
2022 if (UNLIKELY(shift >= IV_BITS)) {
2025 return left ? uv << shift : uv >> shift;
2028 static IV S_iv_shift(IV iv, int shift, bool left)
2035 if (UNLIKELY(shift >= IV_BITS)) {
2036 return iv < 0 && !left ? -1 : 0;
2039 /* For left shifts, perl 5 has chosen to treat the value as unsigned for
2040 * the purposes of shifting, then cast back to signed. This is very
2041 * different from Raku:
2043 * $ raku -e 'say -2 +< 5'
2046 * $ ./perl -le 'print -2 << 5'
2047 * 18446744073709551552
2050 return (IV) (((UV) iv) << shift);
2053 /* Here is right shift */
2057 #define UV_LEFT_SHIFT(uv, shift) S_uv_shift(uv, shift, TRUE)
2058 #define UV_RIGHT_SHIFT(uv, shift) S_uv_shift(uv, shift, FALSE)
2059 #define IV_LEFT_SHIFT(iv, shift) S_iv_shift(iv, shift, TRUE)
2060 #define IV_RIGHT_SHIFT(iv, shift) S_iv_shift(iv, shift, FALSE)
2064 dSP; dATARGET; SV *svl, *svr;
2065 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
2069 const int shift = S_shift_amount(aTHX_ svr);
2070 if (PL_op->op_private & OPpUSEINT) {
2071 SETi(IV_LEFT_SHIFT(SvIV_nomg(svl), shift));
2074 SETu(UV_LEFT_SHIFT(SvUV_nomg(svl), shift));
2082 dSP; dATARGET; SV *svl, *svr;
2083 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
2087 const int shift = S_shift_amount(aTHX_ svr);
2088 if (PL_op->op_private & OPpUSEINT) {
2089 SETi(IV_RIGHT_SHIFT(SvIV_nomg(svl), shift));
2092 SETu(UV_RIGHT_SHIFT(SvUV_nomg(svl), shift));
2102 U32 flags_and, flags_or;
2104 tryAMAGICbin_MG(lt_amg, AMGf_numeric);
2107 flags_and = SvFLAGS(left) & SvFLAGS(right);
2108 flags_or = SvFLAGS(left) | SvFLAGS(right);
2111 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2112 ? (SvIVX(left) < SvIVX(right))
2113 : (flags_and & SVf_NOK)
2114 ? (SvNVX(left) < SvNVX(right))
2115 : (do_ncmp(left, right) == -1)
2124 U32 flags_and, flags_or;
2126 tryAMAGICbin_MG(gt_amg, AMGf_numeric);
2129 flags_and = SvFLAGS(left) & SvFLAGS(right);
2130 flags_or = SvFLAGS(left) | SvFLAGS(right);
2133 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2134 ? (SvIVX(left) > SvIVX(right))
2135 : (flags_and & SVf_NOK)
2136 ? (SvNVX(left) > SvNVX(right))
2137 : (do_ncmp(left, right) == 1)
2146 U32 flags_and, flags_or;
2148 tryAMAGICbin_MG(le_amg, AMGf_numeric);
2151 flags_and = SvFLAGS(left) & SvFLAGS(right);
2152 flags_or = SvFLAGS(left) | SvFLAGS(right);
2155 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2156 ? (SvIVX(left) <= SvIVX(right))
2157 : (flags_and & SVf_NOK)
2158 ? (SvNVX(left) <= SvNVX(right))
2159 : (do_ncmp(left, right) <= 0)
2168 U32 flags_and, flags_or;
2170 tryAMAGICbin_MG(ge_amg, AMGf_numeric);
2173 flags_and = SvFLAGS(left) & SvFLAGS(right);
2174 flags_or = SvFLAGS(left) | SvFLAGS(right);
2177 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2178 ? (SvIVX(left) >= SvIVX(right))
2179 : (flags_and & SVf_NOK)
2180 ? (SvNVX(left) >= SvNVX(right))
2181 : ( (do_ncmp(left, right) & 2) == 0)
2190 U32 flags_and, flags_or;
2192 tryAMAGICbin_MG(ne_amg, AMGf_numeric);
2195 flags_and = SvFLAGS(left) & SvFLAGS(right);
2196 flags_or = SvFLAGS(left) | SvFLAGS(right);
2199 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2200 ? (SvIVX(left) != SvIVX(right))
2201 : (flags_and & SVf_NOK)
2202 ? (SvNVX(left) != SvNVX(right))
2203 : (do_ncmp(left, right) != 0)
2208 /* compare left and right SVs. Returns:
2212 * 2: left or right was a NaN
2215 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2217 PERL_ARGS_ASSERT_DO_NCMP;
2218 #ifdef PERL_PRESERVE_IVUV
2219 /* Fortunately it seems NaN isn't IOK */
2220 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
2222 const IV leftiv = SvIVX(left);
2223 if (!SvUOK(right)) {
2224 /* ## IV <=> IV ## */
2225 const IV rightiv = SvIVX(right);
2226 return (leftiv > rightiv) - (leftiv < rightiv);
2228 /* ## IV <=> UV ## */
2230 /* As (b) is a UV, it's >=0, so it must be < */
2233 const UV rightuv = SvUVX(right);
2234 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2239 /* ## UV <=> UV ## */
2240 const UV leftuv = SvUVX(left);
2241 const UV rightuv = SvUVX(right);
2242 return (leftuv > rightuv) - (leftuv < rightuv);
2244 /* ## UV <=> IV ## */
2246 const IV rightiv = SvIVX(right);
2248 /* As (a) is a UV, it's >=0, so it cannot be < */
2251 const UV leftuv = SvUVX(left);
2252 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2255 NOT_REACHED; /* NOTREACHED */
2259 NV const rnv = SvNV_nomg(right);
2260 NV const lnv = SvNV_nomg(left);
2262 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2263 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2266 return (lnv > rnv) - (lnv < rnv);
2285 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2288 value = do_ncmp(left, right);
2300 /* also used for: pp_sge() pp_sgt() pp_slt() */
2306 int amg_type = sle_amg;
2310 switch (PL_op->op_type) {
2329 tryAMAGICbin_MG(amg_type, 0);
2333 #ifdef USE_LOCALE_COLLATE
2334 (IN_LC_RUNTIME(LC_COLLATE))
2335 ? sv_cmp_locale_flags(left, right, 0)
2338 sv_cmp_flags(left, right, 0);
2339 SETs(boolSV(cmp * multiplier < rhs));
2347 tryAMAGICbin_MG(seq_amg, 0);
2350 SETs(boolSV(sv_eq_flags(left, right, 0)));
2358 tryAMAGICbin_MG(sne_amg, 0);
2361 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2369 tryAMAGICbin_MG(scmp_amg, 0);
2373 #ifdef USE_LOCALE_COLLATE
2374 (IN_LC_RUNTIME(LC_COLLATE))
2375 ? sv_cmp_locale_flags(left, right, 0)
2378 sv_cmp_flags(left, right, 0);
2387 tryAMAGICbin_MG(band_amg, AMGf_assign);
2390 if (SvNIOKp(left) || SvNIOKp(right)) {
2391 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2392 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2393 if (PL_op->op_private & OPpUSEINT) {
2394 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2398 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2401 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2402 if (right_ro_nonnum) SvNIOK_off(right);
2405 do_vop(PL_op->op_type, TARG, left, right);
2415 tryAMAGICbin_MG(band_amg, AMGf_assign|AMGf_numarg);
2417 dATARGET; dPOPTOPssrl;
2418 if (PL_op->op_private & OPpUSEINT) {
2419 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2423 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2433 tryAMAGICbin_MG(sband_amg, AMGf_assign);
2435 dATARGET; dPOPTOPssrl;
2436 do_vop(OP_BIT_AND, TARG, left, right);
2441 /* also used for: pp_bit_xor() */
2446 const int op_type = PL_op->op_type;
2448 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2451 if (SvNIOKp(left) || SvNIOKp(right)) {
2452 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2453 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2454 if (PL_op->op_private & OPpUSEINT) {
2455 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2456 const IV r = SvIV_nomg(right);
2457 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2461 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2462 const UV r = SvUV_nomg(right);
2463 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2466 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2467 if (right_ro_nonnum) SvNIOK_off(right);
2470 do_vop(op_type, TARG, left, right);
2477 /* also used for: pp_nbit_xor() */
2482 const int op_type = PL_op->op_type;
2484 tryAMAGICbin_MG((op_type == OP_NBIT_OR ? bor_amg : bxor_amg),
2485 AMGf_assign|AMGf_numarg);
2487 dATARGET; dPOPTOPssrl;
2488 if (PL_op->op_private & OPpUSEINT) {
2489 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2490 const IV r = SvIV_nomg(right);
2491 const IV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2495 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2496 const UV r = SvUV_nomg(right);
2497 const UV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2504 /* also used for: pp_sbit_xor() */
2509 const int op_type = PL_op->op_type;
2511 tryAMAGICbin_MG((op_type == OP_SBIT_OR ? sbor_amg : sbxor_amg),
2514 dATARGET; dPOPTOPssrl;
2515 do_vop(op_type == OP_SBIT_OR ? OP_BIT_OR : OP_BIT_XOR, TARG, left,
2521 PERL_STATIC_INLINE bool
2522 S_negate_string(pTHX)
2527 SV * const sv = TOPs;
2528 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2530 s = SvPV_nomg_const(sv, len);
2531 if (isIDFIRST(*s)) {
2532 sv_setpvs(TARG, "-");
2535 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2536 sv_setsv_nomg(TARG, sv);
2537 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2547 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2548 if (S_negate_string(aTHX)) return NORMAL;
2550 SV * const sv = TOPs;
2553 /* It's publicly an integer */
2556 if (SvIVX(sv) == IV_MIN) {
2557 /* 2s complement assumption. */
2558 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) ==
2562 else if (SvUVX(sv) <= IV_MAX) {
2567 else if (SvIVX(sv) != IV_MIN) {
2571 #ifdef PERL_PRESERVE_IVUV
2578 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2579 SETn(-SvNV_nomg(sv));
2580 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2581 goto oops_its_an_int;
2583 SETn(-SvNV_nomg(sv));
2593 tryAMAGICun_MG(not_amg, 0);
2595 *PL_stack_sp = boolSV(!SvTRUE_nomg_NN(sv));
2600 S_scomplement(pTHX_ SV *targ, SV *sv)
2606 sv_copypv_nomg(TARG, sv);
2607 tmps = (U8*)SvPV_nomg(TARG, len);
2610 if (len && ! utf8_to_bytes(tmps, &len)) {
2611 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[PL_op->op_type]);
2613 SvCUR_set(TARG, len);
2621 for ( ; anum && PTR2nat(tmps) % sizeof(long); anum--, tmps++)
2624 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2629 for ( ; anum > 0; anum--, tmps++)
2636 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2640 if (PL_op->op_private & OPpUSEINT) {
2641 const IV i = ~SvIV_nomg(sv);
2645 const UV u = ~SvUV_nomg(sv);
2650 S_scomplement(aTHX_ TARG, sv);
2660 tryAMAGICun_MG(compl_amg, AMGf_numeric|AMGf_numarg);
2663 if (PL_op->op_private & OPpUSEINT) {
2664 const IV i = ~SvIV_nomg(sv);
2668 const UV u = ~SvUV_nomg(sv);
2678 tryAMAGICun_MG(scompl_amg, AMGf_numeric);
2681 S_scomplement(aTHX_ TARG, sv);
2687 /* integer versions of some of the above */
2692 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2695 SETi( (IV)((UV)left * (UV)right) );
2704 tryAMAGICbin_MG(div_amg, AMGf_assign);
2707 IV value = SvIV_nomg(right);
2709 DIE(aTHX_ "Illegal division by zero");
2710 num = SvIV_nomg(left);
2712 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2714 value = (IV)-(UV)num;
2716 value = num / value;
2725 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2729 DIE(aTHX_ "Illegal modulus zero");
2730 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2734 SETi( left % right );
2742 tryAMAGICbin_MG(add_amg, AMGf_assign);
2744 dPOPTOPiirl_ul_nomg;
2745 SETi( (IV)((UV)left + (UV)right) );
2753 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2755 dPOPTOPiirl_ul_nomg;
2756 SETi( (IV)((UV)left - (UV)right) );
2764 tryAMAGICbin_MG(lt_amg, 0);
2767 SETs(boolSV(left < right));
2775 tryAMAGICbin_MG(gt_amg, 0);
2778 SETs(boolSV(left > right));
2786 tryAMAGICbin_MG(le_amg, 0);
2789 SETs(boolSV(left <= right));
2797 tryAMAGICbin_MG(ge_amg, 0);
2800 SETs(boolSV(left >= right));
2808 tryAMAGICbin_MG(eq_amg, 0);
2811 SETs(boolSV(left == right));
2819 tryAMAGICbin_MG(ne_amg, 0);
2822 SETs(boolSV(left != right));
2830 tryAMAGICbin_MG(ncmp_amg, 0);
2837 else if (left < right)
2849 tryAMAGICun_MG(neg_amg, 0);
2850 if (S_negate_string(aTHX)) return NORMAL;
2852 SV * const sv = TOPs;
2853 IV const i = SvIV_nomg(sv);
2859 /* High falutin' math. */
2864 tryAMAGICbin_MG(atan2_amg, 0);
2867 SETn(Perl_atan2(left, right));
2873 /* also used for: pp_cos() pp_exp() pp_log() pp_sqrt() */
2878 int amg_type = fallback_amg;
2879 const char *neg_report = NULL;
2880 const int op_type = PL_op->op_type;
2883 case OP_SIN: amg_type = sin_amg; break;
2884 case OP_COS: amg_type = cos_amg; break;
2885 case OP_EXP: amg_type = exp_amg; break;
2886 case OP_LOG: amg_type = log_amg; neg_report = "log"; break;
2887 case OP_SQRT: amg_type = sqrt_amg; neg_report = "sqrt"; break;
2890 assert(amg_type != fallback_amg);
2892 tryAMAGICun_MG(amg_type, 0);
2894 SV * const arg = TOPs;
2895 const NV value = SvNV_nomg(arg);
2901 if (neg_report) { /* log or sqrt */
2903 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2904 ! Perl_isnan(value) &&
2906 (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)))
2910 SET_NUMERIC_STANDARD();
2911 mesg = Perl_form(aTHX_ "Can't take %s of %" NVgf, neg_report, value);
2914 /* diag_listed_as: Can't take log of %g */
2915 DIE(aTHX_ "%s", mesg);
2920 case OP_SIN: result = Perl_sin(value); break;
2921 case OP_COS: result = Perl_cos(value); break;
2922 case OP_EXP: result = Perl_exp(value); break;
2923 case OP_LOG: result = Perl_log(value); break;
2924 case OP_SQRT: result = Perl_sqrt(value); break;
2931 /* Support Configure command-line overrides for rand() functions.
2932 After 5.005, perhaps we should replace this by Configure support
2933 for drand48(), random(), or rand(). For 5.005, though, maintain
2934 compatibility by calling rand() but allow the user to override it.
2935 See INSTALL for details. --Andy Dougherty 15 July 1998
2937 /* Now it's after 5.005, and Configure supports drand48() and random(),
2938 in addition to rand(). So the overrides should not be needed any more.
2939 --Jarkko Hietaniemi 27 September 1998
2944 if (!PL_srand_called) {
2946 if (PL_srand_override) {
2947 /* env var PERL_RAND_SEED has been set so the user wants
2948 * consistent srand() initialization. */
2949 PERL_SRAND_OVERRIDE_GET(s);
2951 /* Pseudo random initialization from context state and possible
2953 s= (Rand_seed_t)seed();
2955 (void)seedDrand01(s);
2956 PL_srand_called = TRUE;
2968 SV * const sv = POPs;
2974 /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
2975 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2976 if (! Perl_isnan(value) && value == 0.0)
2986 sv_setnv_mg(TARG, value);
2997 if (MAXARG >= 1 && (TOPs || POPs)) {
3004 pv = SvPV(top, len);
3005 flags = grok_number(pv, len, &anum);
3007 if (!(flags & IS_NUMBER_IN_UV)) {
3008 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
3009 "Integer overflow in srand");
3014 if (PL_srand_override) {
3015 /* env var PERL_RAND_SEED has been set so the user wants
3016 * consistent srand() initialization. */
3017 PERL_SRAND_OVERRIDE_GET(anum);
3023 (void)seedDrand01((Rand_seed_t)anum);
3024 PL_srand_called = TRUE;
3028 /* Historically srand always returned true. We can avoid breaking
3030 sv_setpvs(TARG, "0 but true");
3039 tryAMAGICun_MG(int_amg, AMGf_numeric);
3041 SV * const sv = TOPs;
3042 const IV iv = SvIV_nomg(sv);
3043 /* XXX it's arguable that compiler casting to IV might be subtly
3044 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
3045 else preferring IV has introduced a subtle behaviour change bug. OTOH
3046 relying on floating point to be accurate is a bug. */
3051 else if (SvIOK(sv)) {
3053 SETu(SvUV_nomg(sv));
3058 const NV value = SvNV_nomg(sv);
3059 if (UNLIKELY(Perl_isinfnan(value)))
3061 else if (value >= 0.0) {
3062 if (value < (NV)UV_MAX + 0.5) {
3065 SETn(Perl_floor(value));
3069 if (value > (NV)IV_MIN - 0.5) {
3072 SETn(Perl_ceil(value));
3083 tryAMAGICun_MG(abs_amg, AMGf_numeric);
3085 SV * const sv = TOPs;
3086 /* This will cache the NV value if string isn't actually integer */
3087 const IV iv = SvIV_nomg(sv);
3094 else if (SvIOK(sv)) {
3095 /* IVX is precise */
3097 uv = SvUVX(sv); /* force it to be numeric only */
3102 /* "(UV)-(iv + 1) + 1" below is mathematically "-iv", but
3103 transformed so that every subexpression will never trigger
3104 overflows even on 2's complement representation (note that
3105 iv is always < 0 here), and modern compilers could optimize
3106 this to a single negation. */
3107 uv = (UV)-(iv + 1) + 1;
3113 const NV value = SvNV_nomg(sv);
3114 SETn(Perl_fabs(value));
3121 /* also used for: pp_hex() */
3127 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3131 SV* const sv = TOPs;
3133 tmps = (SvPV_const(sv, len));
3135 /* If Unicode, try to downgrade
3136 * If not possible, croak. */
3137 SV* const tsv = sv_2mortal(newSVsv(sv));
3140 (void)sv_utf8_downgrade(tsv, FALSE);
3141 tmps = SvPV_const(tsv, len);
3143 if (PL_op->op_type == OP_HEX)
3146 while (*tmps && len && isSPACE(*tmps))
3150 if (isALPHA_FOLD_EQ(*tmps, 'x')) {
3152 flags |= PERL_SCAN_DISALLOW_PREFIX;
3154 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3156 else if (isALPHA_FOLD_EQ(*tmps, 'b')) {
3158 flags |= PERL_SCAN_DISALLOW_PREFIX;
3159 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3162 if (isALPHA_FOLD_EQ(*tmps, 'o')) {
3165 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3168 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3183 SV * const sv = TOPs;
3185 U32 in_bytes = IN_BYTES;
3186 /* Simplest case shortcut:
3187 * set svflags to just the SVf_POK|SVs_GMG|SVf_UTF8 from the SV,
3188 * with the SVf_UTF8 flag inverted if under 'use bytes' (HINT_BYTES
3191 U32 svflags = (SvFLAGS(sv) ^ (in_bytes << 26)) & (SVf_POK|SVs_GMG|SVf_UTF8);
3193 STATIC_ASSERT_STMT(SVf_UTF8 == (HINT_BYTES << 26));
3196 if (LIKELY(svflags == SVf_POK))
3199 if (svflags & SVs_GMG)
3204 if (!IN_BYTES) { /* reread to avoid using an C auto/register */
3205 if ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == SVf_POK)
3207 if ( SvPOK(sv) && (PL_op->op_private & OPpTRUEBOOL)) {
3208 /* no need to convert from bytes to chars */
3212 len = sv_len_utf8_nomg(sv);
3215 /* unrolled SvPV_nomg_const(sv,len) */
3216 if (SvPOK_nog(sv)) {
3219 if (PL_op->op_private & OPpTRUEBOOL) {
3221 SETs(len ? &PL_sv_yes : &PL_sv_zero);
3226 (void)sv_2pv_flags(sv, &len, 0|SV_CONST_RETURN);
3229 TARGi((IV)(len), 1);
3232 if (!SvPADTMP(TARG)) {
3233 /* OPpTARGET_MY: targ is var in '$lex = length()' */
3238 /* TARG is on stack at this point and is overwriten by SETs.
3239 * This branch is the odd one out, so put TARG by default on
3240 * stack earlier to let local SP go out of liveness sooner */
3243 return NORMAL; /* no putback, SP didn't move in this opcode */
3247 /* Returns false if substring is completely outside original string.
3248 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
3249 always be true for an explicit 0.
3252 Perl_translate_substr_offsets( STRLEN curlen, IV pos1_iv,
3253 bool pos1_is_uv, IV len_iv,
3254 bool len_is_uv, STRLEN *posp,
3260 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
3262 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
3263 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3266 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
3269 if (len_iv || len_is_uv) {
3270 if (!len_is_uv && len_iv < 0) {
3271 pos2_iv = curlen + len_iv;
3273 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3276 } else { /* len_iv >= 0 */
3277 if (!pos1_is_uv && pos1_iv < 0) {
3278 pos2_iv = pos1_iv + len_iv;
3279 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3281 if ((UV)len_iv > curlen-(UV)pos1_iv)
3284 pos2_iv = pos1_iv+len_iv;
3294 if (!pos2_is_uv && pos2_iv < 0) {
3295 if (!pos1_is_uv && pos1_iv < 0)
3299 else if (!pos1_is_uv && pos1_iv < 0)
3302 if ((UV)pos2_iv < (UV)pos1_iv)
3304 if ((UV)pos2_iv > curlen)
3307 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3308 *posp = (STRLEN)( (UV)pos1_iv );
3309 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3326 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3327 const bool rvalue = (GIMME_V != G_VOID);
3330 const char *repl = NULL;
3332 int num_args = PL_op->op_private & 7;
3333 bool repl_need_utf8_upgrade = FALSE;
3337 if(!(repl_sv = POPs)) num_args--;
3339 if ((len_sv = POPs)) {
3340 len_iv = SvIV(len_sv);
3341 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3346 pos1_iv = SvIV(pos_sv);
3347 pos1_is_uv = SvIOK_UV(pos_sv);
3349 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3353 if (lvalue && !repl_sv) {
3355 ret = newSV_type_mortal(SVt_PVLV); /* Not TARG RT#67838 */
3356 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3358 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3360 pos1_is_uv || pos1_iv >= 0
3361 ? (STRLEN)(UV)pos1_iv
3362 : (LvFLAGS(ret) |= LVf_NEG_OFF, (STRLEN)(UV)-pos1_iv);
3364 len_is_uv || len_iv > 0
3365 ? (STRLEN)(UV)len_iv
3366 : (LvFLAGS(ret) |= LVf_NEG_LEN, (STRLEN)(UV)-len_iv);
3368 PUSHs(ret); /* avoid SvSETMAGIC here */
3372 repl = SvPV_const(repl_sv, repl_len);
3375 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3376 "Attempt to use reference as lvalue in substr"
3378 tmps = SvPV_force_nomg(sv, curlen);
3379 if (DO_UTF8(repl_sv) && repl_len) {
3381 /* Upgrade the dest, and recalculate tmps in case the buffer
3382 * got reallocated; curlen may also have been changed */
3383 sv_utf8_upgrade_nomg(sv);
3384 tmps = SvPV_nomg(sv, curlen);
3387 else if (DO_UTF8(sv))
3388 repl_need_utf8_upgrade = TRUE;
3390 else tmps = SvPV_const(sv, curlen);
3392 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3393 if (utf8_curlen == curlen)
3396 curlen = utf8_curlen;
3402 STRLEN pos, len, byte_len, byte_pos;
3404 if (!translate_substr_offsets(
3405 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3409 byte_pos = utf8_curlen
3410 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3415 SvTAINTED_off(TARG); /* decontaminate */
3416 SvUTF8_off(TARG); /* decontaminate */
3417 sv_setpvn(TARG, tmps, byte_len);
3418 #ifdef USE_LOCALE_COLLATE
3419 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3426 SV* repl_sv_copy = NULL;
3428 if (repl_need_utf8_upgrade) {
3429 repl_sv_copy = newSVsv(repl_sv);
3430 sv_utf8_upgrade(repl_sv_copy);
3431 repl = SvPV_const(repl_sv_copy, repl_len);
3435 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3436 SvREFCNT_dec(repl_sv_copy);
3439 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST)
3449 Perl_croak(aTHX_ "substr outside of string");
3450 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3457 const IV size = POPi;
3458 SV* offsetsv = POPs;
3459 SV * const src = POPs;
3460 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3466 /* extract a STRLEN-ranged integer value from offsetsv into offset,
3467 * or flag that its out of range */
3469 IV iv = SvIV(offsetsv);
3471 /* avoid a large UV being wrapped to a negative value */
3472 if (SvIOK_UV(offsetsv) && SvUVX(offsetsv) > (UV)IV_MAX)
3473 errflags = LVf_OUT_OF_RANGE;
3475 errflags = (LVf_NEG_OFF|LVf_OUT_OF_RANGE);
3476 #if PTRSIZE < IVSIZE
3477 else if (iv > Size_t_MAX)
3478 errflags = LVf_OUT_OF_RANGE;
3481 offset = (STRLEN)iv;
3484 retuv = errflags ? 0 : do_vecget(src, offset, size);
3486 if (lvalue) { /* it's an lvalue! */
3487 ret = newSV_type_mortal(SVt_PVLV); /* Not TARG RT#67838 */
3488 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3490 LvTARG(ret) = SvREFCNT_inc_simple(src);
3491 LvTARGOFF(ret) = offset;
3492 LvTARGLEN(ret) = size;
3493 LvFLAGS(ret) = errflags;
3497 SvTAINTED_off(TARG); /* decontaminate */
3501 sv_setuv(ret, retuv);
3509 /* also used for: pp_rindex() */
3522 const char *little_p;
3525 const bool is_index = PL_op->op_type == OP_INDEX;
3526 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3532 big_p = SvPV_const(big, biglen);
3533 little_p = SvPV_const(little, llen);
3535 big_utf8 = DO_UTF8(big);
3536 little_utf8 = DO_UTF8(little);
3537 if (big_utf8 ^ little_utf8) {
3538 /* One needs to be upgraded. */
3540 /* Well, maybe instead we might be able to downgrade the small
3542 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3545 /* If the large string is ISO-8859-1, and it's not possible to
3546 convert the small string to ISO-8859-1, then there is no
3547 way that it could be found anywhere by index. */
3552 /* At this point, pv is a malloc()ed string. So donate it to temp
3553 to ensure it will get free()d */
3554 little = temp = newSV_type(SVt_NULL);
3555 sv_usepvn(temp, pv, llen);
3556 little_p = SvPVX(little);
3558 temp = newSVpvn(little_p, llen);
3560 sv_utf8_upgrade(temp);
3562 little_p = SvPV_const(little, llen);
3565 if (SvGAMAGIC(big)) {
3566 /* Life just becomes a lot easier if I use a temporary here.
3567 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3568 will trigger magic and overloading again, as will fbm_instr()
3570 big = newSVpvn_flags(big_p, biglen,
3571 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3574 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3575 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3576 warn on undef, and we've already triggered a warning with the
3577 SvPV_const some lines above. We can't remove that, as we need to
3578 call some SvPV to trigger overloading early and find out if the
3580 This is all getting too messy. The API isn't quite clean enough,
3581 because data access has side effects.
3583 little = newSVpvn_flags(little_p, llen,
3584 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3585 little_p = SvPVX(little);
3589 offset = is_index ? 0 : biglen;
3591 if (big_utf8 && offset > 0)
3592 offset = sv_pos_u2b_flags(big, offset, 0, SV_CONST_RETURN);
3598 else if (offset > (SSize_t)biglen)
3600 if (!(little_p = is_index
3601 ? fbm_instr((unsigned char*)big_p + offset,
3602 (unsigned char*)big_p + biglen, little, 0)
3603 : rninstr(big_p, big_p + offset,
3604 little_p, little_p + llen)))
3607 retval = little_p - big_p;
3608 if (retval > 1 && big_utf8)
3609 retval = sv_pos_b2u_flags(big, retval, SV_CONST_RETURN);
3614 /* OPpTRUEBOOL indicates an '== -1' has been optimised away */
3615 if (PL_op->op_private & OPpTRUEBOOL) {
3616 SV *result = ((retval != -1) ^ cBOOL(PL_op->op_private & OPpINDEX_BOOLNEG))
3617 ? &PL_sv_yes : &PL_sv_no;
3618 if (PL_op->op_private & OPpTARGET_MY) {
3619 /* $lex = (index() == -1) */
3620 sv_setsv_mg(TARG, result);
3634 dSP; dMARK; dORIGMARK; dTARGET;
3635 SvTAINTED_off(TARG);
3636 do_sprintf(TARG, SP-MARK, MARK+1);
3637 TAINT_IF(SvTAINTED(TARG));
3649 const U8 *s = (U8*)SvPV_const(argsv, len);
3652 ? (len ? utf8n_to_uvchr(s, len, 0, UTF8_ALLOW_ANYUV) : 0)
3666 if (UNLIKELY(SvAMAGIC(top)))
3668 if (UNLIKELY(isinfnansv(top)))
3669 Perl_croak(aTHX_ "Cannot chr %" NVgf, SvNV(top));
3671 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3672 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3674 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3675 && SvNV_nomg(top) < 0.0)))
3677 if (ckWARN(WARN_UTF8)) {
3678 if (SvGMAGICAL(top)) {
3679 SV *top2 = sv_newmortal();
3680 sv_setsv_nomg(top2, top);
3683 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3684 "Invalid negative number (%" SVf ") in chr", SVfARG(top));
3686 value = UNICODE_REPLACEMENT;
3688 value = SvUV_nomg(top);
3692 SvUPGRADE(TARG,SVt_PV);
3694 if (value > 255 && !IN_BYTES) {
3695 SvGROW(TARG, (STRLEN)UVCHR_SKIP(value)+1);
3696 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3697 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3699 (void)SvPOK_only(TARG);
3708 *tmps++ = (char)value;
3710 (void)SvPOK_only(TARG);
3722 const char *tmps = SvPV_const(left, len);
3724 if (DO_UTF8(left)) {
3725 /* If Unicode, try to downgrade.
3726 * If not possible, croak.
3727 * Yes, we made this up. */
3728 SV* const tsv = newSVpvn_flags(tmps, len, SVf_UTF8|SVs_TEMP);
3730 (void)sv_utf8_downgrade(tsv, FALSE);
3731 tmps = SvPV_const(tsv, len);
3733 # ifdef USE_ITHREADS
3735 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3736 /* This should be threadsafe because in ithreads there is only
3737 * one thread per interpreter. If this would not be true,
3738 * we would need a mutex to protect this malloc. */
3739 PL_reentrant_buffer->_crypt_struct_buffer =
3740 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3741 # if defined(__GLIBC__) || defined(__EMX__)
3742 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3743 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3747 # endif /* HAS_CRYPT_R */
3748 # endif /* USE_ITHREADS */
3750 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3757 "The crypt() function is unimplemented due to excessive paranoia.");
3761 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3762 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3765 /* also used for: pp_lcfirst() */
3769 /* Actually is both lcfirst() and ucfirst(). Only the first character
3770 * changes. This means that possibly we can change in-place, ie., just
3771 * take the source and change that one character and store it back, but not
3772 * if read-only etc, or if the length changes */
3776 STRLEN slen; /* slen is the byte length of the whole SV. */
3779 bool inplace; /* ? Convert first char only, in-place */
3780 bool doing_utf8 = FALSE; /* ? using utf8 */
3781 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3782 const int op_type = PL_op->op_type;
3785 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3786 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3787 * stored as UTF-8 at s. */
3788 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3789 * lowercased) character stored in tmpbuf. May be either
3790 * UTF-8 or not, but in either case is the number of bytes */
3791 bool remove_dot_above = FALSE;
3793 s = (const U8*)SvPV_const(source, slen);
3795 /* We may be able to get away with changing only the first character, in
3796 * place, but not if read-only, etc. Later we may discover more reasons to
3797 * not convert in-place. */
3798 inplace = !SvREADONLY(source) && SvPADTMP(source);
3800 #ifdef USE_LOCALE_CTYPE
3802 if (IN_LC_RUNTIME(LC_CTYPE)) {
3803 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
3808 /* First calculate what the changed first character should be. This affects
3809 * whether we can just swap it out, leaving the rest of the string unchanged,
3810 * or even if have to convert the dest to UTF-8 when the source isn't */
3812 if (! slen) { /* If empty */
3813 need = 1; /* still need a trailing NUL */
3817 else if (DO_UTF8(source)) { /* Is the source utf8? */
3821 if (op_type == OP_UCFIRST) {
3822 #ifdef USE_LOCALE_CTYPE
3823 _toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
3825 _toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, 0);
3830 #ifdef USE_LOCALE_CTYPE
3832 _toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
3834 /* In turkic locales, lower casing an 'I' normally yields U+0131,
3835 * LATIN SMALL LETTER DOTLESS I, but not if the grapheme also
3836 * contains a COMBINING DOT ABOVE. Instead it is treated like
3837 * LATIN CAPITAL LETTER I WITH DOT ABOVE lowercased to 'i'. The
3838 * call to lowercase above has handled this. But SpecialCasing.txt
3839 * says we are supposed to remove the COMBINING DOT ABOVE. We can
3840 * tell if we have this situation if I ==> i in a turkic locale. */
3841 if ( UNLIKELY(PL_in_utf8_turkic_locale)
3842 && IN_LC_RUNTIME(LC_CTYPE)
3843 && (UNLIKELY(*s == 'I' && tmpbuf[0] == 'i')))
3845 /* Here, we know there was a COMBINING DOT ABOVE. We won't be
3846 * able to handle this in-place. */
3849 /* It seems likely that the DOT will immediately follow the
3850 * 'I'. If so, we can remove it simply by indicating to the
3851 * code below to start copying the source just beyond the DOT.
3852 * We know its length is 2 */
3853 if (LIKELY(memBEGINs(s + 1, s + slen, COMBINING_DOT_ABOVE_UTF8))) {
3856 else { /* But if it doesn't follow immediately, set a flag for
3858 remove_dot_above = TRUE;
3862 PERL_UNUSED_VAR(remove_dot_above);
3864 _toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, 0);
3869 /* we can't do in-place if the length changes. */
3870 if (ulen != tculen) inplace = FALSE;
3871 need = slen + 1 - ulen + tculen;
3873 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3874 * latin1 is treated as caseless. Note that a locale takes
3876 ulen = 1; /* Original character is 1 byte */
3877 tculen = 1; /* Most characters will require one byte, but this will
3878 * need to be overridden for the tricky ones */
3882 #ifdef USE_LOCALE_CTYPE
3884 if (IN_LC_RUNTIME(LC_CTYPE)) {
3885 if ( UNLIKELY(PL_in_utf8_turkic_locale)
3886 && ( (op_type == OP_LCFIRST && UNLIKELY(*s == 'I'))
3887 || (op_type == OP_UCFIRST && UNLIKELY(*s == 'i'))))
3889 if (*s == 'I') { /* lcfirst('I') */
3890 tmpbuf[0] = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
3891 tmpbuf[1] = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
3893 else { /* ucfirst('i') */
3894 tmpbuf[0] = UTF8_TWO_BYTE_HI(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
3895 tmpbuf[1] = UTF8_TWO_BYTE_LO(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
3900 convert_source_to_utf8 = TRUE;
3901 need += variant_under_utf8_count(s, s + slen);
3903 else if (op_type == OP_LCFIRST) {
3905 /* For lc, there are no gotchas for UTF-8 locales (other than
3906 * the turkish ones already handled above) */
3907 *tmpbuf = toLOWER_LC(*s);
3909 else { /* ucfirst */
3911 /* But for uc, some characters require special handling */
3912 if (IN_UTF8_CTYPE_LOCALE) {
3916 /* This would be a bug if any locales have upper and title case
3918 *tmpbuf = (U8) toUPPER_LC(*s);
3923 /* Here, not in locale. If not using Unicode rules, is a simple
3924 * lower/upper, depending */
3925 if (! IN_UNI_8_BIT) {
3926 *tmpbuf = (op_type == OP_LCFIRST)
3930 else if (op_type == OP_LCFIRST) {
3931 /* lower case the first letter: no trickiness for any character */
3932 *tmpbuf = toLOWER_LATIN1(*s);
3935 /* Here, is ucfirst non-UTF-8, not in locale (unless that locale is
3936 * non-turkic UTF-8, which we treat as not in locale), and cased
3939 #ifdef USE_LOCALE_CTYPE
3943 title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3945 assert(tculen == 2);
3947 /* If the result is an upper Latin1-range character, it can
3948 * still be represented in one byte, which is its ordinal */
3949 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3950 *tmpbuf = (U8) title_ord;
3954 /* Otherwise it became more than one ASCII character (in
3955 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3956 * beyond Latin1, so the number of bytes changed, so can't
3957 * replace just the first character in place. */
3960 /* If the result won't fit in a byte, the entire result
3961 * will have to be in UTF-8. Allocate enough space for the
3962 * expanded first byte, and if UTF-8, the rest of the input
3963 * string, some or all of which may also expand to two
3964 * bytes, plus the terminating NUL. */
3965 if (title_ord > 255) {
3967 convert_source_to_utf8 = TRUE;
3969 + variant_under_utf8_count(s, s + slen)
3972 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3973 * characters whose title case is above 255 is
3977 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3978 need = slen + 1 + 1;
3982 } /* End of use Unicode (Latin1) semantics */
3983 } /* End of changing the case of the first character */
3985 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3986 * generate the result */
3989 /* We can convert in place. This means we change just the first
3990 * character without disturbing the rest; no need to grow */
3992 s = d = (U8*)SvPV_force_nomg(source, slen);
3998 /* Here, we can't convert in place; we earlier calculated how much
3999 * space we will need, so grow to accommodate that */
4000 SvUPGRADE(dest, SVt_PV);
4001 d = (U8*)SvGROW(dest, need);
4002 (void)SvPOK_only(dest);
4009 if (! convert_source_to_utf8) {
4011 /* Here both source and dest are in UTF-8, but have to create
4012 * the entire output. We initialize the result to be the
4013 * title/lower cased first character, and then append the rest
4015 sv_setpvn(dest, (char*)tmpbuf, tculen);
4018 /* But this boolean being set means we are in a turkic
4019 * locale, and there is a DOT character that needs to be
4020 * removed, and it isn't immediately after the current
4021 * character. Keep concatenating characters to the output
4022 * one at a time, until we find the DOT, which we simply
4024 if (UNLIKELY(remove_dot_above)) {
4026 Size_t this_len = UTF8SKIP(s + ulen);
4028 sv_catpvn(dest, (char*)(s + ulen), this_len);
4031 if (memBEGINs(s + ulen, s + slen, COMBINING_DOT_ABOVE_UTF8)) {
4035 } while (s + ulen < s + slen);
4038 /* The rest of the string can be concatenated unchanged,
4040 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
4044 const U8 *const send = s + slen;
4046 /* Here the dest needs to be in UTF-8, but the source isn't,
4047 * except we earlier UTF-8'd the first character of the source
4048 * into tmpbuf. First put that into dest, and then append the
4049 * rest of the source, converting it to UTF-8 as we go. */
4051 /* Assert tculen is 2 here because the only characters that
4052 * get to this part of the code have 2-byte UTF-8 equivalents */
4053 assert(tculen == 2);
4055 *d++ = *(tmpbuf + 1);
4056 s++; /* We have just processed the 1st char */
4059 append_utf8_from_native_byte(*s, &d);
4064 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4068 else { /* in-place UTF-8. Just overwrite the first character */
4069 Copy(tmpbuf, d, tculen, U8);
4070 SvCUR_set(dest, need - 1);
4074 else { /* Neither source nor dest are, nor need to be UTF-8 */
4076 if (inplace) { /* in-place, only need to change the 1st char */
4079 else { /* Not in-place */
4081 /* Copy the case-changed character(s) from tmpbuf */
4082 Copy(tmpbuf, d, tculen, U8);
4083 d += tculen - 1; /* Code below expects d to point to final
4084 * character stored */
4087 else { /* empty source */
4088 /* See bug #39028: Don't taint if empty */
4092 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
4093 * the destination to retain that flag */
4094 if (DO_UTF8(source))
4097 if (!inplace) { /* Finish the rest of the string, unchanged */
4098 /* This will copy the trailing NUL */
4099 Copy(s + 1, d + 1, slen, U8);
4100 SvCUR_set(dest, need - 1);
4103 #ifdef USE_LOCALE_CTYPE
4104 if (IN_LC_RUNTIME(LC_CTYPE)) {
4109 if (dest != source && SvTAINTED(source))
4127 if ( SvPADTMP(source)
4128 && !SvREADONLY(source) && SvPOK(source)
4131 #ifdef USE_LOCALE_CTYPE
4132 (IN_LC_RUNTIME(LC_CTYPE))
4133 ? ! IN_UTF8_CTYPE_LOCALE
4139 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4140 * make the loop tight, so we overwrite the source with the dest before
4141 * looking at it, and we need to look at the original source
4142 * afterwards. There would also need to be code added to handle
4143 * switching to not in-place in midstream if we run into characters
4144 * that change the length. Since being in locale overrides UNI_8_BIT,
4145 * that latter becomes irrelevant in the above test; instead for
4146 * locale, the size can't normally change, except if the locale is a
4149 s = d = (U8*)SvPV_force_nomg(source, len);
4156 s = (const U8*)SvPV_nomg_const(source, len);
4159 SvUPGRADE(dest, SVt_PV);
4160 d = (U8*)SvGROW(dest, min);
4161 (void)SvPOK_only(dest);
4166 #ifdef USE_LOCALE_CTYPE
4168 if (IN_LC_RUNTIME(LC_CTYPE)) {
4169 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
4174 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4175 to check DO_UTF8 again here. */
4177 if (DO_UTF8(source)) {
4178 const U8 *const send = s + len;
4179 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4181 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
4182 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4183 /* All occurrences of these are to be moved to follow any other marks.
4184 * This is context-dependent. We may not be passed enough context to
4185 * move the iota subscript beyond all of them, but we do the best we can
4186 * with what we're given. The result is always better than if we
4187 * hadn't done this. And, the problem would only arise if we are
4188 * passed a character without all its combining marks, which would be
4189 * the caller's mistake. The information this is based on comes from a
4190 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4191 * itself) and so can't be checked properly to see if it ever gets
4192 * revised. But the likelihood of it changing is remote */
4193 bool in_iota_subscript = FALSE;
4199 if (UNLIKELY(in_iota_subscript)) {
4200 UV cp = utf8_to_uvchr_buf(s, send, NULL);
4202 if (! _invlist_contains_cp(PL_utf8_mark, cp)) {
4204 /* A non-mark. Time to output the iota subscript */
4205 *d++ = UTF8_TWO_BYTE_HI(GREEK_CAPITAL_LETTER_IOTA);
4206 *d++ = UTF8_TWO_BYTE_LO(GREEK_CAPITAL_LETTER_IOTA);
4207 in_iota_subscript = FALSE;
4211 /* Then handle the current character. Get the changed case value
4212 * and copy it to the output buffer */
4215 #ifdef USE_LOCALE_CTYPE
4216 uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
4218 uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, 0);
4220 if (uv == GREEK_CAPITAL_LETTER_IOTA
4221 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4223 in_iota_subscript = TRUE;
4226 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4227 /* If the eventually required minimum size outgrows the
4228 * available space, we need to grow. */
4229 const UV o = d - (U8*)SvPVX_const(dest);
4231 /* If someone uppercases one million U+03B0s we SvGROW()
4232 * one million times. Or we could try guessing how much to
4233 * allocate without allocating too much. But we can't
4234 * really guess without examining the rest of the string.
4235 * Such is life. See corresponding comment in lc code for
4237 d = o + (U8*) SvGROW(dest, min);
4239 Copy(tmpbuf, d, ulen, U8);
4244 if (in_iota_subscript) {
4245 *d++ = UTF8_TWO_BYTE_HI(GREEK_CAPITAL_LETTER_IOTA);
4246 *d++ = UTF8_TWO_BYTE_LO(GREEK_CAPITAL_LETTER_IOTA);
4251 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4253 else { /* Not UTF-8 */
4255 const U8 *const send = s + len;
4257 /* Use locale casing if in locale; regular style if not treating
4258 * latin1 as having case; otherwise the latin1 casing. Do the
4259 * whole thing in a tight loop, for speed, */
4260 #ifdef USE_LOCALE_CTYPE
4261 if (IN_LC_RUNTIME(LC_CTYPE)) {
4262 if (IN_UTF8_CTYPE_LOCALE) {
4265 for (; s < send; d++, s++)
4266 *d = (U8) toUPPER_LC(*s);
4270 if (! IN_UNI_8_BIT) {
4271 for (; s < send; d++, s++) {
4276 #ifdef USE_LOCALE_CTYPE
4279 for (; s < send; d++, s++) {
4282 *d = toUPPER_LATIN1_MOD(*s);
4283 if ( LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)
4285 #ifdef USE_LOCALE_CTYPE
4287 && (LIKELY( ! PL_in_utf8_turkic_locale
4288 || ! IN_LC_RUNTIME(LC_CTYPE))
4296 /* The mainstream case is the tight loop above. To avoid
4297 * extra tests in that, all three characters that always
4298 * require special handling are mapped by the MOD to the
4299 * one tested just above. Use the source to distinguish
4300 * between those cases */
4302 #if UNICODE_MAJOR_VERSION > 2 \
4303 || (UNICODE_MAJOR_VERSION == 2 && UNICODE_DOT_VERSION >= 1 \
4304 && UNICODE_DOT_DOT_VERSION >= 8)
4305 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4307 /* uc() of this requires 2 characters, but they are
4308 * ASCII. If not enough room, grow the string */
4309 if (SvLEN(dest) < ++min) {
4310 const UV o = d - (U8*)SvPVX_const(dest);
4311 d = o + (U8*) SvGROW(dest, min);
4313 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4314 continue; /* Back to the tight loop; still in ASCII */
4318 /* The other special handling characters have their
4319 * upper cases outside the latin1 range, hence need to be
4320 * in UTF-8, so the whole result needs to be in UTF-8.
4322 * So, here we are somewhere in the middle of processing a
4323 * non-UTF-8 string, and realize that we will have to
4324 * convert the whole thing to UTF-8. What to do? There
4325 * are several possibilities. The simplest to code is to
4326 * convert what we have so far, set a flag, and continue on
4327 * in the loop. The flag would be tested each time through
4328 * the loop, and if set, the next character would be
4329 * converted to UTF-8 and stored. But, I (khw) didn't want
4330 * to slow down the mainstream case at all for this fairly
4331 * rare case, so I didn't want to add a test that didn't
4332 * absolutely have to be there in the loop, besides the
4333 * possibility that it would get too complicated for
4334 * optimizers to deal with. Another possibility is to just
4335 * give up, convert the source to UTF-8, and restart the
4336 * function that way. Another possibility is to convert
4337 * both what has already been processed and what is yet to
4338 * come separately to UTF-8, then jump into the loop that
4339 * handles UTF-8. But the most efficient time-wise of the
4340 * ones I could think of is what follows, and turned out to
4341 * not require much extra code.
4343 * First, calculate the extra space needed for the
4344 * remainder of the source needing to be in UTF-8. Except
4345 * for the 'i' in Turkic locales, in UTF-8 strings, the
4346 * uppercase of a character below 256 occupies the same
4347 * number of bytes as the original. Therefore, the space
4348 * needed is the that number plus the number of characters
4349 * that become two bytes when converted to UTF-8, plus, in
4350 * turkish locales, the number of 'i's. */
4352 extra = send - s + variant_under_utf8_count(s, send);
4354 #ifdef USE_LOCALE_CTYPE
4356 if (UNLIKELY(*s == 'i')) { /* We wouldn't get an 'i' here
4357 unless are in a Turkic
4359 const U8 * s_peek = s;
4364 s_peek = (U8 *) memchr(s_peek + 1, 'i',
4365 send - (s_peek + 1));
4366 } while (s_peek != NULL);
4370 /* Convert what we have so far into UTF-8, telling the
4371 * function that we know it should be converted, and to
4372 * allow extra space for what we haven't processed yet.
4374 * This may cause the string pointer to move, so need to
4375 * save and re-find it. */
4377 len = d - (U8*)SvPVX_const(dest);
4378 SvCUR_set(dest, len);
4379 len = sv_utf8_upgrade_flags_grow(dest,
4380 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4382 + 1 /* trailing NUL */ );
4383 d = (U8*)SvPVX(dest) + len;
4385 /* Now process the remainder of the source, simultaneously
4386 * converting to upper and UTF-8.
4388 * To avoid extra tests in the loop body, and since the
4389 * loop is so simple, split out the rare Turkic case into
4392 #ifdef USE_LOCALE_CTYPE
4393 if ( UNLIKELY(PL_in_utf8_turkic_locale)
4394 && UNLIKELY(IN_LC_RUNTIME(LC_CTYPE)))
4396 for (; s < send; s++) {
4398 *d++ = UTF8_TWO_BYTE_HI(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
4399 *d++ = UTF8_TWO_BYTE_LO(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
4402 (void) _to_upper_title_latin1(*s, d, &len, 'S');
4409 for (; s < send; s++) {
4410 (void) _to_upper_title_latin1(*s, d, &len, 'S');
4414 /* Here have processed the whole source; no need to
4415 * continue with the outer loop. Each character has been
4416 * converted to upper case and converted to UTF-8. */
4418 } /* End of processing all latin1-style chars */
4419 } /* End of processing all chars */
4420 } /* End of source is not empty */
4422 if (source != dest) {
4423 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4424 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4426 } /* End of isn't utf8 */
4427 #ifdef USE_LOCALE_CTYPE
4428 if (IN_LC_RUNTIME(LC_CTYPE)) {
4433 if (dest != source && SvTAINTED(source))
4448 bool has_turkic_I = FALSE;
4452 if ( SvPADTMP(source)
4453 && !SvREADONLY(source) && SvPOK(source)
4456 #ifdef USE_LOCALE_CTYPE
4458 && ( LIKELY(! IN_LC_RUNTIME(LC_CTYPE))
4459 || LIKELY(! PL_in_utf8_turkic_locale))
4465 /* We can convert in place, as, outside of Turkic UTF-8 locales,
4466 * lowercasing anything in the latin1 range (or else DO_UTF8 would have
4467 * been on) doesn't lengthen it. */
4469 s = d = (U8*)SvPV_force_nomg(source, len);
4476 s = (const U8*)SvPV_nomg_const(source, len);
4479 SvUPGRADE(dest, SVt_PV);
4480 d = (U8*)SvGROW(dest, min);
4481 (void)SvPOK_only(dest);
4486 #ifdef USE_LOCALE_CTYPE
4488 if (IN_LC_RUNTIME(LC_CTYPE)) {
4491 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
4493 /* Lowercasing in a Turkic locale can cause non-UTF-8 to need to become
4494 * UTF-8 for the single case of the character 'I' */
4495 if ( UNLIKELY(PL_in_utf8_turkic_locale)
4496 && ! DO_UTF8(source)
4497 && (next_I = (U8 *) memchr(s, 'I', len)))
4500 const U8 *const send = s + len;
4505 next_I = (U8 *) memchr(next_I + 1, 'I',
4506 send - (next_I + 1));
4507 } while (next_I != NULL);
4509 /* Except for the 'I', in UTF-8 strings, the lower case of a
4510 * character below 256 occupies the same number of bytes as the
4511 * original. Therefore, the space needed is the original length
4512 * plus I_count plus the number of characters that become two bytes
4513 * when converted to UTF-8 */
4514 sv_utf8_upgrade_flags_grow(dest, 0, len
4516 + variant_under_utf8_count(s, send)
4517 + 1 /* Trailing NUL */ );
4518 d = (U8*)SvPVX(dest);
4519 has_turkic_I = TRUE;
4524 PERL_UNUSED_VAR(has_turkic_I);
4527 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4528 to check DO_UTF8 again here. */
4530 if (DO_UTF8(source)) {
4531 const U8 *const send = s + len;
4532 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4533 bool remove_dot_above = FALSE;
4536 const STRLEN u = UTF8SKIP(s);
4539 #ifdef USE_LOCALE_CTYPE
4541 _toLOWER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
4543 /* If we are in a Turkic locale, we have to do more work. As noted
4544 * in the comments for lcfirst, there is a special case if a 'I'
4545 * is in a grapheme with COMBINING DOT ABOVE UTF8. It turns into a
4546 * 'i', and the DOT must be removed. We check for that situation,
4547 * and set a flag if the DOT is there. Then each time through the
4548 * loop, we have to see if we need to remove the next DOT above,
4549 * and if so, do it. We know that there is a DOT because
4550 * _toLOWER_utf8_flags() wouldn't have returned 'i' unless there
4551 * was one in a proper position. */
4552 if ( UNLIKELY(PL_in_utf8_turkic_locale)
4553 && IN_LC_RUNTIME(LC_CTYPE))
4555 if ( UNLIKELY(remove_dot_above)
4556 && memBEGINs(tmpbuf, sizeof(tmpbuf), COMBINING_DOT_ABOVE_UTF8))
4559 remove_dot_above = FALSE;
4562 else if (UNLIKELY(*s == 'I' && tmpbuf[0] == 'i')) {
4563 remove_dot_above = TRUE;
4567 PERL_UNUSED_VAR(remove_dot_above);
4569 _toLOWER_utf8_flags(s, send, tmpbuf, &ulen, 0);
4572 /* Here is where we would do context-sensitive actions for the
4573 * Greek final sigma. See the commit message for 86510fb15 for why
4574 * there isn't any */
4576 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4578 /* If the eventually required minimum size outgrows the
4579 * available space, we need to grow. */
4580 const UV o = d - (U8*)SvPVX_const(dest);
4582 /* If someone lowercases one million U+0130s we SvGROW() one
4583 * million times. Or we could try guessing how much to
4584 * allocate without allocating too much. Such is life.
4585 * Another option would be to grow an extra byte or two more
4586 * each time we need to grow, which would cut down the million
4587 * to 500K, with little waste */
4588 d = o + (U8*) SvGROW(dest, min);
4591 /* Copy the newly lowercased letter to the output buffer we're
4593 Copy(tmpbuf, d, ulen, U8);
4596 } /* End of looping through the source string */
4599 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4600 } else { /* 'source' not utf8 */
4602 const U8 *const send = s + len;
4604 /* Use locale casing if in locale; regular style if not treating
4605 * latin1 as having case; otherwise the latin1 casing. Do the
4606 * whole thing in a tight loop, for speed, */
4607 #ifdef USE_LOCALE_CTYPE
4608 if (IN_LC_RUNTIME(LC_CTYPE)) {
4609 if (LIKELY( ! has_turkic_I)) {
4610 for (; s < send; d++, s++)
4611 *d = toLOWER_LC(*s);
4613 else { /* This is the only case where lc() converts 'dest'
4614 into UTF-8 from a non-UTF-8 'source' */
4615 for (; s < send; s++) {
4617 *d++ = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
4618 *d++ = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
4621 append_utf8_from_native_byte(toLOWER_LATIN1(*s), &d);
4628 if (! IN_UNI_8_BIT) {
4629 for (; s < send; d++, s++) {
4634 for (; s < send; d++, s++) {
4635 *d = toLOWER_LATIN1(*s);
4639 if (source != dest) {
4641 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4644 #ifdef USE_LOCALE_CTYPE
4645 if (IN_LC_RUNTIME(LC_CTYPE)) {
4650 if (dest != source && SvTAINTED(source))
4659 SV * const sv = TOPs;
4661 const char *s = SvPV_const(sv,len);
4663 SvUTF8_off(TARG); /* decontaminate */
4666 SvUPGRADE(TARG, SVt_PV);
4667 SvGROW(TARG, (len * 2) + 1);
4671 STRLEN ulen = UTF8SKIP(s);
4672 bool to_quote = FALSE;
4674 if (UTF8_IS_INVARIANT(*s)) {
4675 if (_isQUOTEMETA(*s)) {
4679 else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, s + len)) {
4681 #ifdef USE_LOCALE_CTYPE
4682 /* In locale, we quote all non-ASCII Latin1 chars.
4683 * Otherwise use the quoting rules */
4685 IN_LC_RUNTIME(LC_CTYPE)
4688 _isQUOTEMETA(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s + 1))))
4693 else if (is_QUOTEMETA_high(s)) {
4708 else if (IN_UNI_8_BIT) {
4710 if (_isQUOTEMETA(*s))
4716 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4717 * including everything above ASCII */
4719 if (!isWORDCHAR_A(*s))
4725 SvCUR_set(TARG, d - SvPVX_const(TARG));
4726 (void)SvPOK_only_UTF8(TARG);
4729 sv_setpvn(TARG, s, len);
4745 U8 tmpbuf[UTF8_MAXBYTES_CASE + 1];
4746 #if UNICODE_MAJOR_VERSION > 3 /* no multifolds in early Unicode */ \
4747 || (UNICODE_MAJOR_VERSION == 3 && ( UNICODE_DOT_VERSION > 0) \
4748 || UNICODE_DOT_DOT_VERSION > 0)
4749 const bool full_folding = TRUE; /* This variable is here so we can easily
4750 move to more generality later */
4752 const bool full_folding = FALSE;
4754 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4755 #ifdef USE_LOCALE_CTYPE
4756 | ( IN_LC_RUNTIME(LC_CTYPE) ? FOLD_FLAGS_LOCALE : 0 )
4760 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4761 * You are welcome(?) -Hugmeir
4769 s = (const U8*)SvPV_nomg_const(source, len);
4771 if (ckWARN(WARN_UNINITIALIZED))
4772 report_uninit(source);
4779 SvUPGRADE(dest, SVt_PV);
4780 d = (U8*)SvGROW(dest, min);
4781 (void)SvPOK_only(dest);
4787 #ifdef USE_LOCALE_CTYPE
4789 if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
4790 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
4795 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4797 const STRLEN u = UTF8SKIP(s);
4800 _toFOLD_utf8_flags(s, send, tmpbuf, &ulen, flags);
4802 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4803 const UV o = d - (U8*)SvPVX_const(dest);
4804 d = o + (U8*) SvGROW(dest, min);
4807 Copy(tmpbuf, d, ulen, U8);
4812 } /* Unflagged string */
4814 #ifdef USE_LOCALE_CTYPE
4815 if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
4816 if (IN_UTF8_CTYPE_LOCALE) {
4817 goto do_uni_folding;
4819 for (; s < send; d++, s++)
4820 *d = (U8) toFOLD_LC(*s);
4824 if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4825 for (; s < send; d++, s++)
4829 #ifdef USE_LOCALE_CTYPE
4832 /* For ASCII and the Latin-1 range, there's potentially three
4833 * troublesome folds:
4834 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
4835 * casefolding becomes 'ss';
4836 * \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4837 * \x{3BC} (\N{GREEK SMALL LETTER MU})
4838 * I only in Turkic locales, this folds to \x{131}
4839 * \N{LATIN SMALL LETTER DOTLESS I}
4840 * For the rest, the casefold is their lowercase. */
4841 for (; s < send; d++, s++) {
4842 if ( UNLIKELY(*s == MICRO_SIGN)
4843 #ifdef USE_LOCALE_CTYPE
4844 || ( UNLIKELY(PL_in_utf8_turkic_locale)
4845 && UNLIKELY(IN_LC_RUNTIME(LC_CTYPE))
4846 && UNLIKELY(*s == 'I'))
4849 Size_t extra = send - s
4850 + variant_under_utf8_count(s, send);
4852 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
4853 * and 'I' in Turkic locales is \N{LATIN SMALL LETTER
4854 * DOTLESS I} both of which are outside of the latin-1
4855 * range. There's a couple of ways to deal with this -- khw
4856 * discusses them in pp_lc/uc, so go there :) What we do
4857 * here is upgrade what we had already casefolded, then
4858 * enter an inner loop that appends the rest of the
4859 * characters as UTF-8.
4861 * First we calculate the needed size of the upgraded dest
4862 * beyond what's been processed already (the upgrade
4863 * function figures that out). Except for the 'I' in
4864 * Turkic locales, in UTF-8 strings, the fold case of a
4865 * character below 256 occupies the same number of bytes as
4866 * the original (even the Sharp S). Therefore, the space
4867 * needed is the number of bytes remaining plus the number
4868 * of characters that become two bytes when converted to
4869 * UTF-8 plus, in turkish locales, the number of 'I's */
4871 if (UNLIKELY(*s == 'I')) {
4872 const U8 * s_peek = s;
4877 s_peek = (U8 *) memchr(s_peek + 1, 'I',
4878 send - (s_peek + 1));
4879 } while (s_peek != NULL);
4882 /* Growing may move things, so have to save and recalculate
4884 len = d - (U8*)SvPVX_const(dest);
4885 SvCUR_set(dest, len);
4886 len = sv_utf8_upgrade_flags_grow(dest,
4887 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4889 + 1 /* Trailing NUL */ );
4890 d = (U8*)SvPVX(dest) + len;
4893 *d++ = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
4894 *d++ = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
4897 *d++ = UTF8_TWO_BYTE_HI(GREEK_SMALL_LETTER_MU);
4898 *d++ = UTF8_TWO_BYTE_LO(GREEK_SMALL_LETTER_MU);
4902 for (; s < send; s++) {
4904 _to_uni_fold_flags(*s, d, &ulen, flags);
4909 else if ( UNLIKELY(*s == LATIN_SMALL_LETTER_SHARP_S)
4912 /* Under full casefolding, LATIN SMALL LETTER SHARP S
4913 * becomes "ss", which may require growing the SV. */
4914 if (SvLEN(dest) < ++min) {
4915 const UV o = d - (U8*)SvPVX_const(dest);
4916 d = o + (U8*) SvGROW(dest, min);
4921 else { /* Else, the fold is the lower case */
4922 *d = toLOWER_LATIN1(*s);
4928 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4930 #ifdef USE_LOCALE_CTYPE
4931 if (IN_LC_RUNTIME(LC_CTYPE)) {
4936 if (SvTAINTED(source))
4946 dSP; dMARK; dORIGMARK;
4947 AV *const av = MUTABLE_AV(POPs);
4948 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4950 if (SvTYPE(av) == SVt_PVAV) {
4951 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4952 bool can_preserve = FALSE;
4958 can_preserve = SvCANEXISTDELETE(av);
4961 if (lval && localizing) {
4964 for (svp = MARK + 1; svp <= SP; svp++) {
4965 const SSize_t elem = SvIV(*svp);
4969 if (max > AvMAX(av))
4973 while (++MARK <= SP) {
4975 SSize_t elem = SvIV(*MARK);
4976 bool preeminent = TRUE;
4978 if (localizing && can_preserve) {
4979 /* If we can determine whether the element exist,
4980 * Try to preserve the existenceness of a tied array
4981 * element by using EXISTS and DELETE if possible.
4982 * Fallback to FETCH and STORE otherwise. */
4983 preeminent = av_exists(av, elem);
4986 svp = av_fetch(av, elem, lval);
4989 DIE(aTHX_ PL_no_aelem, elem);
4992 save_aelem(av, elem, svp);
4994 SAVEADELETE(av, elem);
4997 *MARK = svp ? *svp : &PL_sv_undef;
5000 if (GIMME_V != G_LIST) {
5002 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5011 AV *const av = MUTABLE_AV(POPs);
5012 I32 lval = (PL_op->op_flags & OPf_MOD);
5013 SSize_t items = SP - MARK;
5015 if (PL_op->op_private & OPpMAYBE_LVSUB) {
5016 const I32 flags = is_lvalue_sub();
5018 if (!(flags & OPpENTERSUB_INARGS))
5019 /* diag_listed_as: Can't modify %s in %s */
5020 Perl_croak(aTHX_ "Can't modify index/value array slice in list assignment");
5027 *(MARK+items*2-1) = *(MARK+items);
5033 while (++MARK <= SP) {
5036 svp = av_fetch(av, SvIV(*MARK), lval);
5038 if (!svp || !*svp || *svp == &PL_sv_undef) {
5039 DIE(aTHX_ PL_no_aelem, SvIV(*MARK));
5041 *MARK = sv_mortalcopy(*MARK);
5043 *++MARK = svp ? *svp : &PL_sv_undef;
5045 if (GIMME_V != G_LIST) {
5046 MARK = SP - items*2;
5047 *++MARK = items > 0 ? *SP : &PL_sv_undef;
5057 AV *array = MUTABLE_AV(POPs);
5058 const U8 gimme = GIMME_V;
5059 IV *iterp = Perl_av_iter_p(aTHX_ array);
5060 const IV current = (*iterp)++;
5062 if (current > av_top_index(array)) {
5064 if (gimme == G_SCALAR)
5072 if (gimme == G_LIST) {
5073 SV **const element = av_fetch(array, current, 0);
5074 PUSHs(element ? *element : &PL_sv_undef);
5079 /* also used for: pp_avalues()*/
5083 AV *array = MUTABLE_AV(POPs);
5084 const U8 gimme = GIMME_V;
5086 *Perl_av_iter_p(aTHX_ array) = 0;
5088 if (gimme == G_SCALAR) {
5090 PUSHi(av_count(array));
5092 else if (gimme == G_LIST) {
5093 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
5094 const I32 flags = is_lvalue_sub();
5095 if (flags && !(flags & OPpENTERSUB_INARGS))
5096 /* diag_listed_as: Can't modify %s in %s */
5098 "Can't modify keys on array in list assignment");
5101 IV n = av_top_index(array);
5106 if ( PL_op->op_type == OP_AKEYS
5107 || ( PL_op->op_type == OP_AVHVSWITCH
5108 && (PL_op->op_private & 3) + OP_AEACH == OP_AKEYS ))
5110 for (i = 0; i <= n; i++) {
5115 for (i = 0; i <= n; i++) {
5116 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
5117 PUSHs(elem ? *elem : &PL_sv_undef);
5125 /* Associative arrays. */
5130 HV * hash = MUTABLE_HV(POPs);
5132 const U8 gimme = GIMME_V;
5134 entry = hv_iternext(hash);
5138 SV* const sv = hv_iterkeysv(entry);
5140 if (gimme == G_LIST) {
5142 val = hv_iterval(hash, entry);
5146 else if (gimme == G_SCALAR)
5153 S_do_delete_local(pTHX)
5156 const U8 gimme = GIMME_V;
5159 const bool sliced = cBOOL(PL_op->op_private & OPpSLICE);
5160 SV **unsliced_keysv = sliced ? NULL : sp--;
5161 SV * const osv = POPs;
5162 SV **mark = sliced ? PL_stack_base + POPMARK : unsliced_keysv-1;
5164 const bool tied = SvRMAGICAL(osv)
5165 && mg_find((const SV *)osv, PERL_MAGIC_tied);
5166 const bool can_preserve = SvCANEXISTDELETE(osv);
5167 const U32 type = SvTYPE(osv);
5168 SV ** const end = sliced ? SP : unsliced_keysv;
5170 if (type == SVt_PVHV) { /* hash element */
5171 HV * const hv = MUTABLE_HV(osv);
5172 while (++MARK <= end) {
5173 SV * const keysv = *MARK;
5175 bool preeminent = TRUE;
5177 preeminent = hv_exists_ent(hv, keysv, 0);
5179 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
5186 sv = hv_delete_ent(hv, keysv, 0, 0);
5188 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5191 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5192 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
5194 *MARK = sv_mortalcopy(sv);
5200 SAVEHDELETE(hv, keysv);
5201 *MARK = &PL_sv_undef;
5205 else if (type == SVt_PVAV) { /* array element */
5206 if (PL_op->op_flags & OPf_SPECIAL) {
5207 AV * const av = MUTABLE_AV(osv);
5208 while (++MARK <= end) {
5209 SSize_t idx = SvIV(*MARK);
5211 bool preeminent = TRUE;
5213 preeminent = av_exists(av, idx);
5215 SV **svp = av_fetch(av, idx, 1);
5222 sv = av_delete(av, idx, 0);
5224 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5227 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
5229 *MARK = sv_mortalcopy(sv);
5235 SAVEADELETE(av, idx);
5236 *MARK = &PL_sv_undef;
5241 DIE(aTHX_ "panic: avhv_delete no longer supported");
5244 DIE(aTHX_ "Not a HASH reference");
5246 if (gimme == G_VOID)
5248 else if (gimme == G_SCALAR) {
5253 *++MARK = &PL_sv_undef;
5257 else if (gimme != G_VOID)
5258 PUSHs(*unsliced_keysv);
5269 if (PL_op->op_private & OPpLVAL_INTRO)
5270 return do_delete_local();
5273 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5275 if (PL_op->op_private & (OPpSLICE|OPpKVSLICE)) {
5277 HV * const hv = MUTABLE_HV(POPs);
5278 const U32 hvtype = SvTYPE(hv);
5280 if (PL_op->op_private & OPpKVSLICE) {
5281 SSize_t items = SP - MARK;
5285 *(MARK+items*2-1) = *(MARK+items);
5292 if (hvtype == SVt_PVHV) { /* hash element */
5293 while ((MARK += (1+skip)) <= SP) {
5294 SV * const sv = hv_delete_ent(hv, *(MARK-skip), discard, 0);
5295 *MARK = sv ? sv : &PL_sv_undef;
5298 else if (hvtype == SVt_PVAV) { /* array element */
5299 if (PL_op->op_flags & OPf_SPECIAL) {
5300 while ((MARK += (1+skip)) <= SP) {
5301 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*(MARK-skip)), discard);
5302 *MARK = sv ? sv : &PL_sv_undef;
5307 DIE(aTHX_ "Not a HASH reference");
5310 else if (gimme == G_SCALAR) {
5315 *++MARK = &PL_sv_undef;
5321 HV * const hv = MUTABLE_HV(POPs);
5323 if (SvTYPE(hv) == SVt_PVHV)
5324 sv = hv_delete_ent(hv, keysv, discard, 0);
5325 else if (SvTYPE(hv) == SVt_PVAV) {
5326 if (PL_op->op_flags & OPf_SPECIAL)
5327 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
5329 DIE(aTHX_ "panic: avhv_delete no longer supported");
5332 DIE(aTHX_ "Not a HASH reference");
5347 if (UNLIKELY( PL_op->op_private & OPpEXISTS_SUB )) {
5349 SV * const sv = POPs;
5350 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
5353 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
5358 hv = MUTABLE_HV(POPs);
5359 if (LIKELY( SvTYPE(hv) == SVt_PVHV )) {
5360 if (hv_exists_ent(hv, tmpsv, 0))
5363 else if (SvTYPE(hv) == SVt_PVAV) {
5364 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
5365 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
5370 DIE(aTHX_ "Not a HASH reference");
5377 dSP; dMARK; dORIGMARK;
5378 HV * const hv = MUTABLE_HV(POPs);
5379 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
5380 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
5381 bool can_preserve = FALSE;
5387 if (SvCANEXISTDELETE(hv))
5388 can_preserve = TRUE;
5391 while (++MARK <= SP) {
5392 SV * const keysv = *MARK;
5395 bool preeminent = TRUE;
5397 if (localizing && can_preserve) {
5398 /* If we can determine whether the element exist,
5399 * try to preserve the existenceness of a tied hash
5400 * element by using EXISTS and DELETE if possible.
5401 * Fallback to FETCH and STORE otherwise. */
5402 preeminent = hv_exists_ent(hv, keysv, 0);
5405 he = hv_fetch_ent(hv, keysv, lval, 0);
5406 svp = he ? &HeVAL(he) : NULL;
5409 if (!svp || !*svp || *svp == &PL_sv_undef) {
5410 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5413 if (HvNAME_get(hv) && isGV_or_RVCV(*svp))
5414 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
5415 else if (preeminent)
5416 save_helem_flags(hv, keysv, svp,
5417 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5419 SAVEHDELETE(hv, keysv);
5422 *MARK = svp && *svp ? *svp : &PL_sv_undef;
5424 if (GIMME_V != G_LIST) {
5426 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5435 HV * const hv = MUTABLE_HV(POPs);
5436 I32 lval = (PL_op->op_flags & OPf_MOD);
5437 SSize_t items = SP - MARK;
5439 if (PL_op->op_private & OPpMAYBE_LVSUB) {
5440 const I32 flags = is_lvalue_sub();
5442 if (!(flags & OPpENTERSUB_INARGS))
5443 /* diag_listed_as: Can't modify %s in %s */
5444 Perl_croak(aTHX_ "Can't modify key/value hash slice in %s assignment",
5445 GIMME_V == G_LIST ? "list" : "scalar");
5452 *(MARK+items*2-1) = *(MARK+items);
5458 while (++MARK <= SP) {
5459 SV * const keysv = *MARK;
5463 he = hv_fetch_ent(hv, keysv, lval, 0);
5464 svp = he ? &HeVAL(he) : NULL;
5467 if (!svp || !*svp || *svp == &PL_sv_undef) {
5468 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5470 *MARK = sv_mortalcopy(*MARK);
5472 *++MARK = svp && *svp ? *svp : &PL_sv_undef;
5474 if (GIMME_V != G_LIST) {
5475 MARK = SP - items*2;
5476 *++MARK = items > 0 ? *SP : &PL_sv_undef;
5482 /* List operators. */
5486 I32 markidx = POPMARK;
5487 if (GIMME_V != G_LIST) {
5488 /* don't initialize mark here, EXTEND() may move the stack */
5491 EXTEND(SP, 1); /* in case no arguments, as in @empty */
5492 mark = PL_stack_base + markidx;
5494 *MARK = *SP; /* unwanted list, return last item */
5496 *MARK = &PL_sv_undef;
5506 SV ** const lastrelem = PL_stack_sp;
5507 SV ** const lastlelem = PL_stack_base + POPMARK;
5508 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5509 SV ** const firstrelem = lastlelem + 1;
5510 const U8 mod = PL_op->op_flags & OPf_MOD;
5512 const I32 max = lastrelem - lastlelem;
5515 if (GIMME_V != G_LIST) {
5516 if (lastlelem < firstlelem) {
5518 *firstlelem = &PL_sv_undef;
5521 I32 ix = SvIV(*lastlelem);
5524 if (ix < 0 || ix >= max)
5525 *firstlelem = &PL_sv_undef;
5527 *firstlelem = firstrelem[ix];
5534 SP = firstlelem - 1;
5538 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
5539 I32 ix = SvIV(*lelem);
5542 if (ix < 0 || ix >= max)
5543 *lelem = &PL_sv_undef;
5545 if (!(*lelem = firstrelem[ix]))
5546 *lelem = &PL_sv_undef;
5547 else if (mod && SvPADTMP(*lelem)) {
5548 *lelem = firstrelem[ix] = sv_mortalcopy(*lelem);
5559 const I32 items = SP - MARK;
5560 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5562 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5563 ? newRV_noinc(av) : av);
5569 dSP; dMARK; dORIGMARK;
5570 HV* const hv = newHV();
5571 SV* const retval = sv_2mortal( PL_op->op_flags & OPf_SPECIAL
5572 ? newRV_noinc(MUTABLE_SV(hv))
5574 /* This isn't quite true for an odd sized list (it's one too few) but it's
5575 not worth the runtime +1 just to optimise for the warning case. */
5576 SSize_t pairs = (SP - MARK) >> 1;
5577 if (pairs > PERL_HASH_DEFAULT_HvMAX) {
5578 hv_ksplit(hv, pairs);
5583 (MARK++, SvGMAGICAL(*MARK) ? sv_mortalcopy(*MARK) : *MARK);
5589 val = newSV_type(SVt_NULL);
5590 sv_setsv_nomg(val, *MARK);
5594 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5595 val = newSV_type(SVt_NULL);
5597 (void)hv_store_ent(hv,key,val,0);
5606 dSP; dMARK; dORIGMARK;
5607 int num_args = (SP - MARK);
5608 AV *ary = MUTABLE_AV(*++MARK);
5617 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5620 return Perl_tied_method(aTHX_ SV_CONST(SPLICE), mark - 1, MUTABLE_SV(ary), mg,
5621 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
5625 if (SvREADONLY(ary))
5626 Perl_croak_no_modify();
5631 offset = i = SvIV(*MARK);
5633 offset += AvFILLp(ary) + 1;
5635 DIE(aTHX_ PL_no_aelem, i);
5637 length = SvIVx(*MARK++);
5639 length += AvFILLp(ary) - offset + 1;
5645 length = AvMAX(ary) + 1; /* close enough to infinity */
5649 length = AvMAX(ary) + 1;
5651 if (offset > AvFILLp(ary) + 1) {
5653 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5654 offset = AvFILLp(ary) + 1;
5656 after = AvFILLp(ary) + 1 - (offset + length);
5657 if (after < 0) { /* not that much array */
5658 length += after; /* offset+length now in array */
5664 /* At this point, MARK .. SP-1 is our new LIST */
5667 diff = newlen - length;
5668 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5671 /* make new elements SVs now: avoid problems if they're from the array */
5672 for (dst = MARK, i = newlen; i; i--) {
5673 SV * const h = *dst;
5674 *dst++ = newSVsv(h);
5677 if (diff < 0) { /* shrinking the area */
5678 SV **tmparyval = NULL;
5680 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5681 Copy(MARK, tmparyval, newlen, SV*);
5684 MARK = ORIGMARK + 1;
5685 if (GIMME_V == G_LIST) { /* copy return vals to stack */
5686 const bool real = cBOOL(AvREAL(ary));
5687 MEXTEND(MARK, length);
5689 EXTEND_MORTAL(length);
5690 for (i = 0, dst = MARK; i < length; i++) {
5691 if ((*dst = AvARRAY(ary)[i+offset])) {
5693 sv_2mortal(*dst); /* free them eventually */
5696 *dst = &PL_sv_undef;
5702 *MARK = AvARRAY(ary)[offset+length-1];
5705 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5706 SvREFCNT_dec(*dst++); /* free them now */
5709 *MARK = &PL_sv_undef;
5711 AvFILLp(ary) += diff;
5713 /* pull up or down? */
5715 if (offset < after) { /* easier to pull up */
5716 if (offset) { /* esp. if nothing to pull */
5717 src = &AvARRAY(ary)[offset-1];
5718 dst = src - diff; /* diff is negative */
5719 for (i = offset; i > 0; i--) /* can't trust Copy */
5723 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5727 if (after) { /* anything to pull down? */
5728 src = AvARRAY(ary) + offset + length;
5729 dst = src + diff; /* diff is negative */
5730 Move(src, dst, after, SV*);
5732 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5733 /* avoid later double free */
5740 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5741 Safefree(tmparyval);
5744 else { /* no, expanding (or same) */
5745 SV** tmparyval = NULL;
5747 Newx(tmparyval, length, SV*); /* so remember deletion */
5748 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5751 if (diff > 0) { /* expanding */
5752 /* push up or down? */
5753 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5757 Move(src, dst, offset, SV*);
5759 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5761 AvFILLp(ary) += diff;
5764 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5765 av_extend(ary, AvFILLp(ary) + diff);
5766 AvFILLp(ary) += diff;
5769 dst = AvARRAY(ary) + AvFILLp(ary);
5771 for (i = after; i; i--) {
5779 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5782 MARK = ORIGMARK + 1;
5783 if (GIMME_V == G_LIST) { /* copy return vals to stack */
5785 const bool real = cBOOL(AvREAL(ary));
5787 EXTEND_MORTAL(length);
5788 for (i = 0, dst = MARK; i < length; i++) {
5789 if ((*dst = tmparyval[i])) {
5791 sv_2mortal(*dst); /* free them eventually */
5793 else *dst = &PL_sv_undef;
5799 else if (length--) {
5800 *MARK = tmparyval[length];
5803 while (length-- > 0)
5804 SvREFCNT_dec(tmparyval[length]);
5807 *MARK = &PL_sv_undef;
5810 *MARK = &PL_sv_undef;
5811 Safefree(tmparyval);
5815 mg_set(MUTABLE_SV(ary));
5823 dSP; dMARK; dORIGMARK; dTARGET;
5824 AV * const ary = MUTABLE_AV(*++MARK);
5825 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5828 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5831 ENTER_with_name("call_PUSH");
5832 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
5833 LEAVE_with_name("call_PUSH");
5834 /* SPAGAIN; not needed: SP is assigned to immediately below */
5837 /* PL_delaymagic is restored by JMPENV_POP on dieing, so we
5838 * only need to save locally, not on the save stack */
5839 U16 old_delaymagic = PL_delaymagic;
5841 if (SvREADONLY(ary) && MARK < SP) Perl_croak_no_modify();
5842 PL_delaymagic = DM_DELAY;
5843 for (++MARK; MARK <= SP; MARK++) {
5845 if (*MARK) SvGETMAGIC(*MARK);
5846 sv = newSV_type(SVt_NULL);
5848 sv_setsv_nomg(sv, *MARK);
5849 av_store(ary, AvFILLp(ary)+1, sv);
5851 if (PL_delaymagic & DM_ARRAY_ISA)
5852 mg_set(MUTABLE_SV(ary));
5853 PL_delaymagic = old_delaymagic;
5856 if (OP_GIMME(PL_op, 0) != G_VOID) {
5857 PUSHi( AvFILL(ary) + 1 );
5862 /* also used for: pp_pop()*/
5866 AV * const av = PL_op->op_flags & OPf_SPECIAL
5867 ? MUTABLE_AV(GvAVn(PL_defgv)) : MUTABLE_AV(POPs);
5868 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5872 (void)sv_2mortal(sv);
5879 dSP; dMARK; dORIGMARK; dTARGET;
5880 AV *ary = MUTABLE_AV(*++MARK);
5881 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5884 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5887 ENTER_with_name("call_UNSHIFT");
5888 call_sv(SV_CONST(UNSHIFT),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
5889 LEAVE_with_name("call_UNSHIFT");
5890 /* SPAGAIN; not needed: SP is assigned to immediately below */
5893 /* PL_delaymagic is restored by JMPENV_POP on dieing, so we
5894 * only need to save locally, not on the save stack */
5895 U16 old_delaymagic = PL_delaymagic;
5898 av_unshift(ary, SP - MARK);
5899 PL_delaymagic = DM_DELAY;
5901 if (!SvMAGICAL(ary)) {
5902 /* The av_unshift above means that many of the checks inside
5903 * av_store are unnecessary. If ary does not have magic attached
5904 * then a simple direct assignment is possible here. */
5906 SV * const sv = newSVsv(*++MARK);
5907 assert( !SvTIED_mg((const SV *)ary, PERL_MAGIC_tied) );
5909 assert( !SvREADONLY(ary) );
5910 assert( AvREAL(ary) || !AvREIFY(ary) );
5911 assert( i <= AvMAX(ary) );
5912 assert( i <= AvFILLp(ary) );
5914 SvREFCNT_dec(AvARRAY(ary)[i]);
5915 AvARRAY(ary)[i] = sv;
5920 SV * const sv = newSVsv(*++MARK);
5921 (void)av_store(ary, i++, sv);
5925 if (PL_delaymagic & DM_ARRAY_ISA)
5926 mg_set(MUTABLE_SV(ary));
5927 PL_delaymagic = old_delaymagic;
5930 if (OP_GIMME(PL_op, 0) != G_VOID) {
5931 PUSHi( AvFILL(ary) + 1 );
5940 if (GIMME_V == G_LIST) {
5941 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5945 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5946 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5947 av = MUTABLE_AV((*SP));
5948 /* In-place reversing only happens in void context for the array
5949 * assignment. We don't need to push anything on the stack. */
5952 if (SvMAGICAL(av)) {
5954 SV *tmp = sv_newmortal();
5955 /* For SvCANEXISTDELETE */
5958 bool can_preserve = SvCANEXISTDELETE(av);
5960 for (i = 0, j = av_top_index(av); i < j; ++i, --j) {
5964 if (!av_exists(av, i)) {
5965 if (av_exists(av, j)) {
5966 SV *sv = av_delete(av, j, 0);
5967 begin = *av_fetch(av, i, TRUE);
5968 sv_setsv_mg(begin, sv);
5972 else if (!av_exists(av, j)) {
5973 SV *sv = av_delete(av, i, 0);
5974 end = *av_fetch(av, j, TRUE);
5975 sv_setsv_mg(end, sv);
5980 begin = *av_fetch(av, i, TRUE);
5981 end = *av_fetch(av, j, TRUE);
5982 sv_setsv(tmp, begin);
5983 sv_setsv_mg(begin, end);
5984 sv_setsv_mg(end, tmp);
5988 SV **begin = AvARRAY(av);
5991 SV **end = begin + AvFILLp(av);
5993 while (begin < end) {
5994 SV * const tmp = *begin;
6005 SV * const tmp = *MARK;
6009 /* safe as long as stack cannot get extended in the above */
6018 SvUTF8_off(TARG); /* decontaminate */
6019 if (SP - MARK > 1) {
6020 do_join(TARG, &PL_sv_no, MARK, SP);
6023 } else if (SP > MARK) {
6024 sv_setsv(TARG, *SP);
6027 sv_setsv(TARG, DEFSV);
6030 SvSETMAGIC(TARG); /* remove any utf8 length magic */
6032 up = SvPV_force(TARG, len);
6035 if (DO_UTF8(TARG)) { /* first reverse each character */
6036 U8* s = (U8*)SvPVX(TARG);
6037 const U8* send = (U8*)(s + len);
6039 if (UTF8_IS_INVARIANT(*s)) {
6044 if (!utf8_to_uvchr_buf(s, send, 0))
6048 down = (char*)(s - 1);
6049 /* reverse this character */
6051 const char tmp = *up;
6059 down = SvPVX(TARG) + len - 1;
6061 const char tmp = *up;
6065 (void)SvPOK_only_UTF8(TARG);
6074 AV *ary = ( (PL_op->op_private & OPpSPLIT_ASSIGN) /* @a = split */
6075 && (PL_op->op_flags & OPf_STACKED)) /* @{expr} = split */
6076 ? (AV *)POPs : NULL;
6077 IV limit = POPi; /* note, negative is forever */
6078 SV * const sv = POPs;
6080 const char *s = SvPV_const(sv, len);
6081 const bool do_utf8 = DO_UTF8(sv);
6082 const bool in_uni_8_bit = IN_UNI_8_BIT;
6083 const char *strend = s + len;
6089 const STRLEN slen = do_utf8
6090 ? utf8_length((U8*)s, (U8*)strend)
6091 : (STRLEN)(strend - s);
6092 SSize_t maxiters = slen + 10;
6093 I32 trailing_empty = 0;
6095 const IV origlimit = limit;
6098 const U8 gimme = GIMME_V;
6100 I32 oldsave = PL_savestack_ix;
6101 U32 flags = (do_utf8 ? SVf_UTF8 : 0) |
6102 SVs_TEMP; /* Make mortal SVs by default */
6107 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
6108 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
6110 /* handle @ary = split(...) optimisation */
6111 if (PL_op->op_private & OPpSPLIT_ASSIGN) {
6113 if (!(PL_op->op_flags & OPf_STACKED)) {
6114 if (PL_op->op_private & OPpSPLIT_LEX) {
6115 if (PL_op->op_private & OPpLVAL_INTRO)
6116 SAVECLEARSV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
6117 ary = (AV *)PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff);
6122 MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
6124 pm->op_pmreplrootu.op_pmtargetgv;
6126 if (PL_op->op_private & OPpLVAL_INTRO)
6131 /* skip anything pushed by OPpLVAL_INTRO above */
6132 oldsave = PL_savestack_ix;
6135 /* Some defence against stack-not-refcounted bugs */
6136 (void)sv_2mortal(SvREFCNT_inc_simple_NN(ary));
6138 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
6140 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
6142 flags &= ~SVs_TEMP; /* SVs will not be mortal */
6146 base = SP - PL_stack_base;
6148 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
6150 while (s < strend && isSPACE_utf8_safe(s, strend))
6153 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
6154 while (s < strend && isSPACE_LC(*s))
6157 else if (in_uni_8_bit) {
6158 while (s < strend && isSPACE_L1(*s))
6162 while (s < strend && isSPACE(*s))
6167 gimme_scalar = gimme == G_SCALAR && !ary;
6170 limit = maxiters + 2;
6171 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
6174 /* this one uses 'm' and is a negative test */
6176 while (m < strend && ! isSPACE_utf8_safe(m, strend) ) {
6177 const int t = UTF8SKIP(m);
6178 /* isSPACE_utf8_safe returns FALSE for malform utf8 */
6185 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
6187 while (m < strend && !isSPACE_LC(*m))
6190 else if (in_uni_8_bit) {
6191 while (m < strend && !isSPACE_L1(*m))
6194 while (m < strend && !isSPACE(*m))
6207 dstr = newSVpvn_flags(s, m-s, flags);
6211 /* skip the whitespace found last */
6213 s = m + UTF8SKIP(m);
6217 /* this one uses 's' and is a positive test */
6219 while (s < strend && isSPACE_utf8_safe(s, strend) )
6222 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
6224 while (s < strend && isSPACE_LC(*s))
6227 else if (in_uni_8_bit) {
6228 while (s < strend && isSPACE_L1(*s))
6231 while (s < strend && isSPACE(*s))
6236 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
6238 for (m = s; m < strend && *m != '\n'; m++)
6251 dstr = newSVpvn_flags(s, m-s, flags);
6257 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
6258 /* This case boils down to deciding which is the smaller of:
6259 * limit - effectively a number of characters
6260 * slen - which already contains the number of characters in s
6262 * The resulting number is the number of iters (for gimme_scalar)
6263 * or the number of SVs to create (!gimme_scalar). */
6265 /* setting it to -1 will trigger a panic in EXTEND() */
6266 const SSize_t sslen = slen > SSize_t_MAX ? -1 : (SSize_t)slen;
6267 const IV items = limit - 1;
6268 if (sslen < items || items < 0) {
6271 /* Note: The same result is returned if the following block
6272 * is removed, because of the "keep field after final delim?"
6273 * adjustment, but having the following makes the "correct"
6274 * behaviour more apparent. */
6282 if (!gimme_scalar) {
6284 Pre-extend the stack, either the number of bytes or
6285 characters in the string or a limited amount, triggered by:
6286 my ($x, $y) = split //, $str;
6295 dstr = newSVpvn_flags(m, s-m, flags);
6300 dstr = newSVpvn_flags(s, 1, flags);
6307 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
6308 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
6309 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
6310 && !(RX_EXTFLAGS(rx) & RXf_IS_ANCHORED)) {
6311 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
6312 SV * const csv = CALLREG_INTUIT_STRING(rx);
6314 len = RX_MINLENRET(rx);
6315 if (len == 1 && !RX_UTF8(rx) && !tail) {
6316 const char c = *SvPV_nolen_const(csv);
6318 for (m = s; m < strend && *m != c; m++)
6329 dstr = newSVpvn_flags(s, m-s, flags);
6332 /* The rx->minlen is in characters but we want to step
6333 * s ahead by bytes. */
6335 s = (char*)utf8_hop_forward((U8*) m, len, (U8*) strend);
6337 s = m + len; /* Fake \n at the end */
6341 const bool multiline = (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) ? 1 : 0;
6343 while (s < strend && --limit &&
6344 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
6345 csv, multiline ? FBMrf_MULTILINE : 0)) )
6354 dstr = newSVpvn_flags(s, m-s, flags);
6357 /* The rx->minlen is in characters but we want to step
6358 * s ahead by bytes. */
6360 s = (char*)utf8_hop_forward((U8*)m, len, (U8 *) strend);
6362 s = m + len; /* Fake \n at the end */
6367 maxiters += slen * RX_NPARENS(rx);
6368 while (s < strend && --limit)
6372 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1,
6375 if (rex_return == 0)
6377 TAINT_IF(RX_MATCH_TAINTED(rx));
6378 /* we never pass the REXEC_COPY_STR flag, so it should
6379 * never get copied */
6380 assert(!RX_MATCH_COPIED(rx));
6381 m = RX_OFFS(rx)[0].start + orig;
6390 dstr = newSVpvn_flags(s, m-s, flags);
6393 if (RX_NPARENS(rx)) {
6395 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
6396 s = RX_OFFS(rx)[i].start + orig;
6397 m = RX_OFFS(rx)[i].end + orig;
6399 /* japhy (07/27/01) -- the (m && s) test doesn't catch
6400 parens that didn't match -- they should be set to
6401 undef, not the empty string */
6409 if (m >= orig && s >= orig) {
6410 dstr = newSVpvn_flags(s, m-s, flags);
6413 dstr = &PL_sv_undef; /* undef, not "" */
6419 s = RX_OFFS(rx)[0].end + orig;
6423 if (!gimme_scalar) {
6424 iters = (SP - PL_stack_base) - base;
6426 if (iters > maxiters)
6427 DIE(aTHX_ "Split loop");
6429 /* keep field after final delim? */
6430 if (s < strend || (iters && origlimit)) {
6431 if (!gimme_scalar) {
6432 const STRLEN l = strend - s;
6433 dstr = newSVpvn_flags(s, l, flags);
6438 else if (!origlimit) {
6440 iters -= trailing_empty;
6442 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
6443 if (TOPs && !(flags & SVs_TEMP))
6452 LEAVE_SCOPE(oldsave);
6458 if (av_count(ary) > 0)
6464 if (AvMAX(ary) > -1) {
6465 /* don't free mere refs */
6466 Zero(AvARRAY(ary), AvMAX(ary), SV*);
6469 if(AvMAX(ary) < iters)
6470 av_extend(ary,iters);
6473 /* Need to copy the SV*s from the stack into ary */
6474 Copy(SP + 1 - iters, AvARRAY(ary), iters, SV*);
6475 AvFILLp(ary) = iters - 1;
6477 if (SvSMAGICAL(ary)) {
6479 mg_set(MUTABLE_SV(ary));
6483 if (gimme != G_LIST) {
6484 /* SP points to the final SV* pushed to the stack. But the SV* */
6485 /* are not going to be used from the stack. Point SP to below */
6486 /* the first of these SV*. */
6493 av_extend(ary,iters);
6496 ENTER_with_name("call_PUSH");
6497 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
6498 LEAVE_with_name("call_PUSH");
6501 if (gimme == G_LIST) {
6503 /* EXTEND should not be needed - we just popped them */
6504 EXTEND_SKIP(SP, iters);
6505 for (i=0; i < iters; i++) {
6506 SV **svp = av_fetch(ary, i, FALSE);
6507 PUSHs((svp) ? *svp : &PL_sv_undef);
6514 if (gimme != G_LIST) {
6525 SV *const sv = PAD_SVl(PL_op->op_targ);
6527 if (SvPADSTALE(sv)) {
6530 RETURNOP(cLOGOP->op_other);
6532 RETURNOP(cLOGOP->op_next);
6541 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
6542 || SvTYPE(retsv) == SVt_PVCV) {
6543 retsv = refto(retsv);
6550 /* used for: pp_padany(), pp_custom(); plus any system ops
6551 * that aren't implemented on a particular platform */
6553 PP(unimplemented_op)
6555 const Optype op_type = PL_op->op_type;
6556 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
6557 with out of range op numbers - it only "special" cases op_custom.
6558 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
6559 if we get here for a custom op then that means that the custom op didn't
6560 have an implementation. Given that OP_NAME() looks up the custom op
6561 by its op_ppaddr, likely it will return NULL, unless someone (unhelpfully)
6562 registers &Perl_unimplemented_op as the address of their custom op.
6563 NULL doesn't generate a useful error message. "custom" does. */
6564 const char *const name = op_type >= OP_max
6565 ? "[out of range]" : PL_op_name[op_type];
6566 if(OP_IS_SOCKET(op_type))
6567 DIE(aTHX_ PL_no_sock_func, name);
6568 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
6572 S_maybe_unwind_defav(pTHX)
6574 if (CX_CUR()->cx_type & CXp_HASARGS) {
6575 PERL_CONTEXT *cx = CX_CUR();
6577 assert(CxHASARGS(cx));
6579 cx->cx_type &= ~CXp_HASARGS;
6583 /* For sorting out arguments passed to a &CORE:: subroutine */
6587 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
6588 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
6589 AV * const at_ = GvAV(PL_defgv);
6590 SV **svp = at_ ? AvARRAY(at_) : NULL;
6591 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
6592 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
6593 bool seen_question = 0;
6594 const char *err = NULL;
6595 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
6597 /* Count how many args there are first, to get some idea how far to
6598 extend the stack. */
6600 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
6602 if (oa & OA_OPTIONAL) seen_question = 1;
6603 if (!seen_question) minargs++;
6607 if(numargs < minargs) err = "Not enough";
6608 else if(numargs > maxargs) err = "Too many";
6610 /* diag_listed_as: Too many arguments for %s */
6612 "%s arguments for %s", err,
6613 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
6616 /* Reset the stack pointer. Without this, we end up returning our own
6617 arguments in list context, in addition to the values we are supposed
6618 to return. nextstate usually does this on sub entry, but we need
6619 to run the next op with the caller's hints, so we cannot have a
6621 SP = PL_stack_base + CX_CUR()->blk_oldsp;
6623 if(!maxargs) RETURN;
6625 /* We do this here, rather than with a separate pushmark op, as it has
6626 to come in between two things this function does (stack reset and
6627 arg pushing). This seems the easiest way to do it. */
6630 (void)Perl_pp_pushmark(aTHX);
6633 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
6634 PUTBACK; /* The code below can die in various places. */
6636 oa = PL_opargs[opnum] >> OASHIFT;
6637 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
6642 if (!numargs && defgv && whicharg == minargs + 1) {
6645 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
6649 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
6656 if (CvUNIQUE(find_runcv_where(FIND_RUNCV_level_eq,1,NULL)))
6659 S_maybe_unwind_defav(aTHX);
6662 PUSHs((SV *)GvAVn(gv));
6665 if (!svp || !*svp || !SvROK(*svp)
6666 || SvTYPE(SvRV(*svp)) != SVt_PVAV)
6668 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
6669 "Type of arg %d to &CORE::%s must be array reference",
6670 whicharg, PL_op_desc[opnum]
6675 if (!svp || !*svp || !SvROK(*svp)
6676 || ( SvTYPE(SvRV(*svp)) != SVt_PVHV
6677 && ( opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
6678 || SvTYPE(SvRV(*svp)) != SVt_PVAV )))
6680 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
6681 "Type of arg %d to &CORE::%s must be hash%s reference",
6682 whicharg, PL_op_desc[opnum],
6683 opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
6690 if (!numargs) PUSHs(NULL);
6691 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
6692 /* no magic here, as the prototype will have added an extra
6693 refgen and we just want what was there before that */
6696 const bool constr = PL_op->op_private & whicharg;
6698 svp && *svp ? *svp : &PL_sv_undef,
6699 constr, cBOOL(CopHINTS_get(PL_curcop) & HINT_STRICT_REFS),
6705 if (!numargs) goto try_defsv;
6707 const bool wantscalar =
6708 PL_op->op_private & OPpCOREARGS_SCALARMOD;
6709 if (!svp || !*svp || !SvROK(*svp)
6710 /* We have to permit globrefs even for the \$ proto, as
6711 *foo is indistinguishable from ${\*foo}, and the proto-
6712 type permits the latter. */
6713 || SvTYPE(SvRV(*svp)) > (
6714 wantscalar ? SVt_PVLV
6715 : opnum == OP_LOCK || opnum == OP_UNDEF
6721 "Type of arg %d to &CORE::%s must be %s",
6722 whicharg, PL_op_name[opnum],
6724 ? "scalar reference"
6725 : opnum == OP_LOCK || opnum == OP_UNDEF
6726 ? "reference to one of [$@%&*]"
6727 : "reference to one of [$@%*]"
6730 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv) {
6731 /* Undo @_ localisation, so that sub exit does not undo
6732 part of our undeffing. */
6733 S_maybe_unwind_defav(aTHX);
6738 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
6746 /* Implement CORE::keys(),values(),each().
6748 * We won't know until run-time whether the arg is an array or hash,
6751 * pp_keys/pp_values/pp_each
6753 * pp_akeys/pp_avalues/pp_aeach
6755 * as appropriate (or whatever pp function actually implements the OP_FOO
6756 * functionality for each FOO).
6763 (SvTYPE(TOPs) == SVt_PVAV ? OP_AEACH : OP_EACH)
6764 + (PL_op->op_private & OPpAVHVSWITCH_MASK)
6772 if (PL_op->op_private & OPpOFFBYONE) {
6773 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
6775 else cv = find_runcv(NULL);
6776 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
6781 S_localise_aelem_lval(pTHX_ AV * const av, SV * const keysv,
6782 const bool can_preserve)
6784 const SSize_t ix = SvIV(keysv);
6785 if (can_preserve ? av_exists(av, ix) : TRUE) {
6786 SV ** const svp = av_fetch(av, ix, 1);
6788 Perl_croak(aTHX_ PL_no_aelem, ix);
6789 save_aelem(av, ix, svp);
6792 SAVEADELETE(av, ix);
6796 S_localise_helem_lval(pTHX_ HV * const hv, SV * const keysv,
6797 const bool can_preserve)
6799 if (can_preserve ? hv_exists_ent(hv, keysv, 0) : TRUE) {
6800 HE * const he = hv_fetch_ent(hv, keysv, 1, 0);
6801 SV ** const svp = he ? &HeVAL(he) : NULL;
6803 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(keysv));
6804 save_helem_flags(hv, keysv, svp, 0);
6807 SAVEHDELETE(hv, keysv);
6811 S_localise_gv_slot(pTHX_ GV *gv, U8 type)
6813 if (type == OPpLVREF_SV) {
6814 save_pushptrptr(gv, SvREFCNT_inc_simple(GvSV(gv)), SAVEt_GVSV);
6817 else if (type == OPpLVREF_AV)
6818 /* XXX Inefficient, as it creates a new AV, which we are
6819 about to clobber. */
6822 assert(type == OPpLVREF_HV);
6823 /* XXX Likewise inefficient. */
6832 SV * const key = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
6833 SV * const left = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
6835 const char *bad = NULL;
6836 const U8 type = PL_op->op_private & OPpLVREF_TYPE;
6837 if (!SvROK(sv)) DIE(aTHX_ "Assigned value is not a reference");
6840 if (SvTYPE(SvRV(sv)) > SVt_PVLV)
6844 if (SvTYPE(SvRV(sv)) != SVt_PVAV)
6848 if (SvTYPE(SvRV(sv)) != SVt_PVHV)
6852 if (SvTYPE(SvRV(sv)) != SVt_PVCV)
6856 /* diag_listed_as: Assigned value is not %s reference */
6857 DIE(aTHX_ "Assigned value is not a%s reference", bad);
6861 switch (left ? SvTYPE(left) : 0) {
6864 SV * const old = PAD_SV(ARGTARG);
6865 PAD_SETSV(ARGTARG, SvREFCNT_inc_NN(SvRV(sv)));
6867 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
6869 SAVECLEARSV(PAD_SVl(ARGTARG));
6873 if (PL_op->op_private & OPpLVAL_INTRO) {
6874 S_localise_gv_slot(aTHX_ (GV *)left, type);
6876 gv_setref(left, sv);
6881 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6882 S_localise_aelem_lval(aTHX_ (AV *)left, key,
6883 SvCANEXISTDELETE(left));
6885 av_store((AV *)left, SvIV(key), SvREFCNT_inc_simple_NN(SvRV(sv)));
6888 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6890 S_localise_helem_lval(aTHX_ (HV *)left, key,
6891 SvCANEXISTDELETE(left));
6893 (void)hv_store_ent((HV *)left, key, SvREFCNT_inc_simple_NN(SvRV(sv)), 0);
6895 if (PL_op->op_flags & OPf_MOD)
6896 SETs(sv_2mortal(newSVsv(sv)));
6897 /* XXX else can weak references go stale before they are read, e.g.,
6906 SV * const ret = newSV_type_mortal(SVt_PVMG);
6907 SV * const elem = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
6908 SV * const arg = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
6909 MAGIC * const mg = sv_magicext(ret, arg, PERL_MAGIC_lvref,
6910 &PL_vtbl_lvref, (char *)elem,
6911 elem ? HEf_SVKEY : (I32)ARGTARG);
6912 mg->mg_private = PL_op->op_private;
6913 if (PL_op->op_private & OPpLVREF_ITER)
6914 mg->mg_flags |= MGf_PERSIST;
6915 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6921 const bool can_preserve = SvCANEXISTDELETE(arg);
6922 if (SvTYPE(arg) == SVt_PVAV)
6923 S_localise_aelem_lval(aTHX_ (AV *)arg, elem, can_preserve);
6925 S_localise_helem_lval(aTHX_ (HV *)arg, elem, can_preserve);
6929 S_localise_gv_slot(aTHX_ (GV *)arg,
6930 PL_op->op_private & OPpLVREF_TYPE);
6932 else if (!(PL_op->op_private & OPpPAD_STATE))
6933 SAVECLEARSV(PAD_SVl(ARGTARG));
6942 AV * const av = (AV *)POPs;
6943 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
6944 bool can_preserve = FALSE;
6946 if (UNLIKELY(localizing)) {
6951 can_preserve = SvCANEXISTDELETE(av);
6953 if (SvTYPE(av) == SVt_PVAV) {
6956 for (svp = MARK + 1; svp <= SP; svp++) {
6957 const SSize_t elem = SvIV(*svp);
6961 if (max > AvMAX(av))
6966 while (++MARK <= SP) {
6967 SV * const elemsv = *MARK;
6968 if (UNLIKELY(localizing)) {
6969 if (SvTYPE(av) == SVt_PVAV)
6970 S_localise_aelem_lval(aTHX_ av, elemsv, can_preserve);
6972 S_localise_helem_lval(aTHX_ (HV *)av, elemsv, can_preserve);
6974 *MARK = newSV_type_mortal(SVt_PVMG);
6975 sv_magic(*MARK,(SV *)av,PERL_MAGIC_lvref,(char *)elemsv,HEf_SVKEY);
6982 if (PL_op->op_flags & OPf_STACKED)
6983 Perl_pp_rv2av(aTHX);
6985 Perl_pp_padav(aTHX);
6989 SETs(0); /* special alias marker that aassign recognises */
6999 SETs(sv_2mortal((SV *)newCONSTSUB(SvTYPE(CopSTASH(PL_curcop))==SVt_PVHV
7000 ? CopSTASH(PL_curcop)
7002 NULL, SvREFCNT_inc_simple_NN(sv))));
7007 /* process one subroutine argument - typically when the sub has a signature:
7008 * introduce PL_curpad[op_targ] and assign to it the value
7009 * for $: (OPf_STACKED ? *sp : $_[N])
7010 * for @/%: @_[N..$#_]
7012 * It's equivalent to
7015 * my $foo = (value-on-stack)
7017 * my @foo = @_[N..$#_]
7027 AV *defav = GvAV(PL_defgv); /* @_ */
7028 IV ix = PTR2IV(cUNOP_AUXo->op_aux);
7031 /* do 'my $var, @var or %var' action */
7032 padentry = &(PAD_SVl(o->op_targ));
7033 save_clearsv(padentry);
7036 if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_SV) {
7037 if (o->op_flags & OPf_STACKED) {
7044 /* should already have been checked */
7046 #if IVSIZE > PTRSIZE
7047 assert(ix <= SSize_t_MAX);
7050 svp = av_fetch(defav, ix, FALSE);
7051 val = svp ? *svp : &PL_sv_undef;
7056 /* cargo-culted from pp_sassign */
7057 assert(TAINTING_get || !TAINT_get);
7058 if (UNLIKELY(TAINT_get) && !SvTAINTED(val))
7061 SvSetMagicSV(targ, val);
7065 /* must be AV or HV */
7067 assert(!(o->op_flags & OPf_STACKED));
7068 argc = ((IV)AvFILL(defav) + 1) - ix;
7070 /* This is a copy of the relevant parts of pp_aassign().
7072 if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_AV) {
7075 if (AvFILL((AV*)targ) > -1) {
7076 /* target should usually be empty. If we get get
7077 * here, someone's been doing some weird closure tricks.
7078 * Make a copy of all args before clearing the array,
7079 * to avoid the equivalent of @a = ($a[0]) prematurely freeing
7080 * elements. See similar code in pp_aassign.
7082 for (i = 0; i < argc; i++) {
7083 SV **svp = av_fetch(defav, ix + i, FALSE);
7084 SV *newsv = newSVsv_flags(svp ? *svp : &PL_sv_undef,
7085 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
7086 if (!av_store(defav, ix + i, newsv))
7087 SvREFCNT_dec_NN(newsv);
7089 av_clear((AV*)targ);
7095 av_extend((AV*)targ, argc);
7100 SV **svp = av_fetch(defav, ix + i, FALSE);
7101 SV *val = svp ? *svp : &PL_sv_undef;
7102 tmpsv = newSV_type(SVt_NULL);
7103 sv_setsv(tmpsv, val);
7104 av_store((AV*)targ, i++, tmpsv);
7112 assert((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_HV);
7114 if (SvRMAGICAL(targ) || HvUSEDKEYS((HV*)targ)) {
7115 /* see "target should usually be empty" comment above */
7116 for (i = 0; i < argc; i++) {
7117 SV **svp = av_fetch(defav, ix + i, FALSE);
7118 SV *newsv = newSV_type(SVt_NULL);
7119 sv_setsv_flags(newsv,
7120 svp ? *svp : &PL_sv_undef,
7121 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
7122 if (!av_store(defav, ix + i, newsv))
7123 SvREFCNT_dec_NN(newsv);
7125 hv_clear((HV*)targ);
7130 assert(argc % 2 == 0);
7139 svp = av_fetch(defav, ix + i++, FALSE);
7140 key = svp ? *svp : &PL_sv_undef;
7141 svp = av_fetch(defav, ix + i++, FALSE);
7142 val = svp ? *svp : &PL_sv_undef;
7145 if (UNLIKELY(SvGMAGICAL(key)))
7146 key = sv_mortalcopy(key);
7147 tmpsv = newSV_type(SVt_NULL);
7148 sv_setsv(tmpsv, val);
7149 hv_store_ent((HV*)targ, key, tmpsv, 0);
7157 /* Handle a default value for one subroutine argument (typically as part
7158 * of a subroutine signature).
7159 * It's equivalent to
7160 * @_ > op_targ ? $_[op_targ] : result_of(op_other)
7162 * Intended to be used where op_next is an OP_ARGELEM
7164 * We abuse the op_targ field slightly: it's an index into @_ rather than
7170 OP * const o = PL_op;
7171 AV *defav = GvAV(PL_defgv); /* @_ */
7172 IV ix = (IV)o->op_targ;
7175 #if IVSIZE > PTRSIZE
7176 assert(ix <= SSize_t_MAX);
7179 if (AvFILL(defav) >= ix) {
7181 SV **svp = av_fetch(defav, ix, FALSE);
7182 SV *val = svp ? *svp : &PL_sv_undef;
7186 return cLOGOPo->op_other;
7191 S_find_runcv_name(void)
7206 sv = sv_newmortal();
7207 gv_fullname4(sv, gv, NULL, TRUE);
7211 /* Check a sub's arguments - i.e. that it has the correct number of args
7212 * (and anything else we might think of in future). Typically used with
7218 OP * const o = PL_op;
7219 struct op_argcheck_aux *aux = (struct op_argcheck_aux *)cUNOP_AUXo->op_aux;
7220 UV params = aux->params;
7221 UV opt_params = aux->opt_params;
7222 char slurpy = aux->slurpy;
7223 AV *defav = GvAV(PL_defgv); /* @_ */
7227 assert(!SvMAGICAL(defav));
7228 argc = (UV)(AvFILLp(defav) + 1);
7229 too_few = (argc < (params - opt_params));
7231 if (UNLIKELY(too_few || (!slurpy && argc > params)))
7233 /* diag_listed_as: Too few arguments for subroutine '%s' (got %d; expected %d) */
7234 /* diag_listed_as: Too few arguments for subroutine '%s' (got %d; expected at least %d) */
7235 /* diag_listed_as: Too many arguments for subroutine '%s' (got %d; expected %d) */
7236 /* diag_listed_as: Too many arguments for subroutine '%s' (got %d; expected at most %d)*/
7237 Perl_croak_caller("Too %s arguments for subroutine '%" SVf "' (got %" UVuf "; expected %s%" UVuf ")",
7238 too_few ? "few" : "many",
7239 S_find_runcv_name(),
7241 too_few ? (slurpy || opt_params ? "at least " : "") : (opt_params ? "at most " : ""),
7242 too_few ? (params - opt_params) : params);
7244 if (UNLIKELY(slurpy == '%' && argc > params && (argc - params) % 2))
7245 /* diag_listed_as: Odd name/value argument for subroutine '%s' */
7246 Perl_croak_caller("Odd name/value argument for subroutine '%" SVf "'",
7247 S_find_runcv_name());
7260 SETs(boolSV(sv_isa_sv(left, right)));
7269 if (SvTRUE_NN(result)) {
7270 return cLOGOP->op_other;
7290 SV *arg = *PL_stack_sp;
7294 *PL_stack_sp = boolSV(SvIsBOOL(arg));
7300 SV *arg = *PL_stack_sp;
7304 *PL_stack_sp = boolSV(SvWEAKREF(arg));
7334 if(!SvROK(arg) || !SvOBJECT((rv = SvRV(arg)))) {
7339 if((PL_op->op_private & OPpTRUEBOOL) ||
7340 ((PL_op->op_private & OPpMAYBE_TRUEBOOL) && (block_gimme() == G_VOID))) {
7341 /* We only care about the boolean truth, not the specific string value.
7342 * We just have to check for the annoying cornercase of the package
7344 HV *stash = SvSTASH(rv);
7345 HEK *hek = HvNAME_HEK(stash);
7348 I32 len = HEK_LEN(hek);
7349 if(UNLIKELY(len == HEf_SVKEY || (len == 1 && HEK_KEY(hek)[0] == '0')))
7356 SETs(sv_ref(NULL, rv, TRUE));
7371 sv_setuv_mg(TARG, PTR2UV(SvRV(arg)));
7373 sv_setsv(TARG, &PL_sv_undef);
7388 sv_setpv_mg(TARG, sv_reftype(SvRV(arg), FALSE));
7390 sv_setsv(TARG, &PL_sv_undef);
7400 PUSHn(Perl_ceil(POPn));
7408 PUSHn(Perl_floor(POPn));
7414 SV *arg = *PL_stack_sp;
7418 *PL_stack_sp = boolSV(SvTAINTED(arg));
7423 * ex: set ts=8 sts=4 sw=4 et: