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))));
409 SV* sv = MUTABLE_SV(cv);
411 if (LIKELY(PL_op->op_flags & OPf_REF)) {
430 if (GIMME_V != G_LIST) {
436 *MARK = &PL_sv_undef;
438 *MARK = refto(*MARK);
442 EXTEND_MORTAL(SP - MARK);
444 *MARK = refto(*MARK);
449 S_refto(pTHX_ SV *sv)
453 PERL_ARGS_ASSERT_REFTO;
455 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
458 if (!(sv = LvTARG(sv)))
461 SvREFCNT_inc_void_NN(sv);
463 else if (SvTYPE(sv) == SVt_PVAV) {
464 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
465 av_reify(MUTABLE_AV(sv));
467 SvREFCNT_inc_void_NN(sv);
469 else if (SvPADTMP(sv)) {
472 else if (UNLIKELY(SvSMAGICAL(sv) && mg_find(sv, PERL_MAGIC_nonelem)))
473 sv_unmagic(SvREFCNT_inc_simple_NN(sv), PERL_MAGIC_nonelem);
476 SvREFCNT_inc_void_NN(sv);
478 rv = newSV_type_mortal(SVt_IV);
479 sv_setrv_noinc(rv, sv);
486 SV * const sv = TOPs;
494 /* op is in boolean context? */
495 if ( (PL_op->op_private & OPpTRUEBOOL)
496 || ( (PL_op->op_private & OPpMAYBE_TRUEBOOL)
497 && block_gimme() == G_VOID))
499 /* refs are always true - unless it's to an object blessed into a
500 * class with a false name, i.e. "0". So we have to check for
501 * that remote possibility. The following is is basically an
502 * unrolled SvTRUE(sv_reftype(rv)) */
503 SV * const rv = SvRV(sv);
505 HV *stash = SvSTASH(rv);
506 HEK *hek = HvNAME_HEK(stash);
508 I32 len = HEK_LEN(hek);
509 /* bail out and do it the hard way? */
512 || (len == 1 && HEK_KEY(hek)[0] == '0')
525 sv_ref(TARG, SvRV(sv), TRUE);
541 stash = CopSTASH(PL_curcop);
542 if (SvTYPE(stash) != SVt_PVHV)
543 Perl_croak(aTHX_ "Attempt to bless into a freed package");
546 SV * const ssv = POPs;
550 if (!ssv) goto curstash;
553 if (!SvAMAGIC(ssv)) {
555 Perl_croak(aTHX_ "Attempt to bless into a reference");
557 /* SvAMAGIC is on here, but it only means potentially overloaded,
558 so after stringification: */
559 ptr = SvPV_nomg_const(ssv,len);
560 /* We need to check the flag again: */
561 if (!SvAMAGIC(ssv)) goto frog;
563 else ptr = SvPV_nomg_const(ssv,len);
565 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
566 "Explicit blessing to '' (assuming package main)");
567 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
570 (void)sv_bless(TOPs, stash);
580 const char * const elem = SvPV_const(sv, len);
581 GV * const gv = MUTABLE_GV(TOPs);
586 /* elem will always be NUL terminated. */
589 if (memEQs(elem, len, "ARRAY"))
591 tmpRef = MUTABLE_SV(GvAV(gv));
592 if (tmpRef && !AvREAL((const AV *)tmpRef)
593 && AvREIFY((const AV *)tmpRef))
594 av_reify(MUTABLE_AV(tmpRef));
598 if (memEQs(elem, len, "CODE"))
599 tmpRef = MUTABLE_SV(GvCVu(gv));
602 if (memEQs(elem, len, "FILEHANDLE")) {
603 tmpRef = MUTABLE_SV(GvIOp(gv));
606 if (memEQs(elem, len, "FORMAT"))
607 tmpRef = MUTABLE_SV(GvFORM(gv));
610 if (memEQs(elem, len, "GLOB"))
611 tmpRef = MUTABLE_SV(gv);
614 if (memEQs(elem, len, "HASH"))
615 tmpRef = MUTABLE_SV(GvHV(gv));
618 if (memEQs(elem, len, "IO"))
619 tmpRef = MUTABLE_SV(GvIOp(gv));
622 if (memEQs(elem, len, "NAME"))
623 sv = newSVhek(GvNAME_HEK(gv));
626 if (memEQs(elem, len, "PACKAGE")) {
627 const HV * const stash = GvSTASH(gv);
628 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
629 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
633 if (memEQs(elem, len, "SCALAR"))
648 /* Pattern matching */
656 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
657 /* Historically, study was skipped in these cases. */
662 /* Make study a no-op. It's no longer useful and its existence
663 complicates matters elsewhere. */
669 /* also used for: pp_transr() */
676 if (PL_op->op_flags & OPf_STACKED)
681 sv = PAD_SV(ARGTARG);
686 if(PL_op->op_type == OP_TRANSR) {
688 const char * const pv = SvPV(sv,len);
689 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
694 Size_t i = do_trans(sv);
700 /* Lvalue operators. */
703 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
709 PERL_ARGS_ASSERT_DO_CHOMP;
711 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
713 if (SvTYPE(sv) == SVt_PVAV) {
715 AV *const av = MUTABLE_AV(sv);
716 const I32 max = AvFILL(av);
718 for (i = 0; i <= max; i++) {
719 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
720 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
721 count += do_chomp(retval, sv, chomping);
725 else if (SvTYPE(sv) == SVt_PVHV) {
726 HV* const hv = MUTABLE_HV(sv);
728 (void)hv_iterinit(hv);
729 while ((entry = hv_iternext(hv)))
730 count += do_chomp(retval, hv_iterval(hv,entry), chomping);
733 else if (SvREADONLY(sv)) {
734 Perl_croak_no_modify();
740 char *temp_buffer = NULL;
745 goto nope_free_nothing;
747 while (len && s[-1] == '\n') {
754 STRLEN rslen, rs_charlen;
755 const char *rsptr = SvPV_const(PL_rs, rslen);
757 rs_charlen = SvUTF8(PL_rs)
761 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
762 /* Assumption is that rs is shorter than the scalar. */
764 /* RS is utf8, scalar is 8 bit. */
766 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
769 /* Cannot downgrade, therefore cannot possibly match.
770 At this point, temp_buffer is not alloced, and
771 is the buffer inside PL_rs, so dont free it.
773 assert (temp_buffer == rsptr);
779 /* RS is 8 bit, scalar is utf8. */
780 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
794 if (memNE(s, rsptr, rslen))
799 SvPV_force_nomg_nolen(sv);
806 Safefree(temp_buffer);
808 SvREFCNT_dec(svrecode);
812 if (len && (!SvPOK(sv) || SvIsCOW(sv)))
813 s = SvPV_force_nomg(sv, len);
816 char * const send = s + len;
817 char * const start = s;
819 while (s > start && UTF8_IS_CONTINUATION(*s))
821 if (is_utf8_string((U8*)s, send - s)) {
822 sv_setpvn(retval, s, send - s);
824 SvCUR_set(sv, s - start);
834 sv_setpvn(retval, s, 1);
848 /* also used for: pp_schomp() */
853 const bool chomping = PL_op->op_type == OP_SCHOMP;
855 const size_t count = do_chomp(TARG, TOPs, chomping);
857 sv_setiv(TARG, count);
863 /* also used for: pp_chomp() */
867 dSP; dMARK; dTARGET; dORIGMARK;
868 const bool chomping = PL_op->op_type == OP_CHOMP;
872 count += do_chomp(TARG, *++MARK, chomping);
874 sv_setiv(TARG, count);
885 if (!PL_op->op_private) {
890 if (PL_op->op_private & OPpTARGET_MY) {
891 SV** const padentry = &PAD_SVl(PL_op->op_targ);
893 EXTEND(SP,1);sp++;PUTBACK;
894 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) == OPpLVAL_INTRO) {
895 save_clearsv(padentry);
907 if (SvTHINKFIRST(sv))
908 sv_force_normal_flags(sv, SV_COW_DROP_PV|SV_IMMEDIATE_UNREF);
910 switch (SvTYPE(sv)) {
914 av_undef(MUTABLE_AV(sv));
917 hv_undef(MUTABLE_HV(sv));
920 if (cv_const_sv((const CV *)sv))
921 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
922 "Constant subroutine %" SVf " undefined",
923 SVfARG(CvANON((const CV *)sv)
924 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
927 ? CvNAME_HEK((CV *)sv)
928 : GvENAME_HEK(CvGV((const CV *)sv))
933 /* let user-undef'd sub keep its identity */
934 cv_undef_flags(MUTABLE_CV(sv), CV_UNDEF_KEEP_NAME);
937 assert(isGV_with_GP(sv));
943 /* undef *Pkg::meth_name ... */
945 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
946 && HvENAME_get(stash);
948 if((stash = GvHV((const GV *)sv))) {
949 if(HvENAME_get(stash))
950 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
954 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
955 gp_free(MUTABLE_GV(sv));
957 GvGP_set(sv, gp_ref(gp));
958 #ifndef PERL_DONT_CREATE_GVSV
959 GvSV(sv) = newSV_type(SVt_NULL);
961 GvLINE(sv) = CopLINE(PL_curcop);
962 GvEGV(sv) = MUTABLE_GV(sv);
966 mro_package_moved(NULL, stash, (const GV *)sv, 0);
968 /* undef *Foo::ISA */
969 if( strEQ(GvNAME((const GV *)sv), "ISA")
970 && (stash = GvSTASH((const GV *)sv))
971 && (method_changed || HvENAME(stash)) )
972 mro_isa_changed_in(stash);
973 else if(method_changed)
974 mro_method_changed_in(
975 GvSTASH((const GV *)sv)
981 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)
982 && !(PL_op->op_private & OPpUNDEF_KEEP_PV)
993 if (PL_op->op_private & OPpTARGET_MY)
1001 /* common "slow" code for pp_postinc and pp_postdec */
1004 S_postincdec_common(pTHX_ SV *sv, SV *targ)
1008 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1011 TARG = sv_newmortal();
1018 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1019 if (inc && !SvOK(TARG))
1026 /* also used for: pp_i_postinc() */
1033 /* special-case sv being a simple integer */
1034 if (LIKELY(((sv->sv_flags &
1035 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1036 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1038 && SvIVX(sv) != IV_MAX)
1041 SvIV_set(sv, iv + 1);
1042 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1047 return S_postincdec_common(aTHX_ sv, TARG);
1051 /* also used for: pp_i_postdec() */
1058 /* special-case sv being a simple integer */
1059 if (LIKELY(((sv->sv_flags &
1060 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1061 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1063 && SvIVX(sv) != IV_MIN)
1066 SvIV_set(sv, iv - 1);
1067 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1072 return S_postincdec_common(aTHX_ sv, TARG);
1076 /* Ordinary operators. */
1080 dSP; dATARGET; SV *svl, *svr;
1081 #ifdef PERL_PRESERVE_IVUV
1084 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1087 #ifdef PERL_PRESERVE_IVUV
1088 /* For integer to integer power, we do the calculation by hand wherever
1089 we're sure it is safe; otherwise we call pow() and try to convert to
1090 integer afterwards. */
1091 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1099 const IV iv = SvIVX(svr);
1103 goto float_it; /* Can't do negative powers this way. */
1107 baseuok = SvUOK(svl);
1109 baseuv = SvUVX(svl);
1111 const IV iv = SvIVX(svl);
1114 baseuok = TRUE; /* effectively it's a UV now */
1116 baseuv = -iv; /* abs, baseuok == false records sign */
1119 /* now we have integer ** positive integer. */
1122 /* foo & (foo - 1) is zero only for a power of 2. */
1123 if (!(baseuv & (baseuv - 1))) {
1124 /* We are raising power-of-2 to a positive integer.
1125 The logic here will work for any base (even non-integer
1126 bases) but it can be less accurate than
1127 pow (base,power) or exp (power * log (base)) when the
1128 intermediate values start to spill out of the mantissa.
1129 With powers of 2 we know this can't happen.
1130 And powers of 2 are the favourite thing for perl
1131 programmers to notice ** not doing what they mean. */
1133 NV base = baseuok ? baseuv : -(NV)baseuv;
1138 while (power >>= 1) {
1146 SvIV_please_nomg(svr);
1149 unsigned int highbit = 8 * sizeof(UV);
1150 unsigned int diff = 8 * sizeof(UV);
1151 while (diff >>= 1) {
1153 if (baseuv >> highbit) {
1157 /* we now have baseuv < 2 ** highbit */
1158 if (power * highbit <= 8 * sizeof(UV)) {
1159 /* result will definitely fit in UV, so use UV math
1160 on same algorithm as above */
1163 const bool odd_power = cBOOL(power & 1);
1167 while (power >>= 1) {
1174 if (baseuok || !odd_power)
1175 /* answer is positive */
1177 else if (result <= (UV)IV_MAX)
1178 /* answer negative, fits in IV */
1179 SETi( -(IV)result );
1180 else if (result == (UV)IV_MIN)
1181 /* 2's complement assumption: special case IV_MIN */
1184 /* answer negative, doesn't fit */
1185 SETn( -(NV)result );
1193 NV right = SvNV_nomg(svr);
1194 NV left = SvNV_nomg(svl);
1197 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1199 We are building perl with long double support and are on an AIX OS
1200 afflicted with a powl() function that wrongly returns NaNQ for any
1201 negative base. This was reported to IBM as PMR #23047-379 on
1202 03/06/2006. The problem exists in at least the following versions
1203 of AIX and the libm fileset, and no doubt others as well:
1205 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1206 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1207 AIX 5.2.0 bos.adt.libm 5.2.0.85
1209 So, until IBM fixes powl(), we provide the following workaround to
1210 handle the problem ourselves. Our logic is as follows: for
1211 negative bases (left), we use fmod(right, 2) to check if the
1212 exponent is an odd or even integer:
1214 - if odd, powl(left, right) == -powl(-left, right)
1215 - if even, powl(left, right) == powl(-left, right)
1217 If the exponent is not an integer, the result is rightly NaNQ, so
1218 we just return that (as NV_NAN).
1222 NV mod2 = Perl_fmod( right, 2.0 );
1223 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1224 SETn( -Perl_pow( -left, right) );
1225 } else if (mod2 == 0.0) { /* even integer */
1226 SETn( Perl_pow( -left, right) );
1227 } else { /* fractional power */
1231 SETn( Perl_pow( left, right) );
1233 #elif IVSIZE == 4 && defined(LONGDOUBLE_DOUBLEDOUBLE) && defined(USE_LONG_DOUBLE)
1235 Under these conditions, if a known libm bug exists, Perl_pow() could return
1236 an incorrect value if the correct value is an integer in the range of around
1237 25 or more bits. The error is always quite small, so we work around it by
1238 rounding to the nearest integer value ... but only if is_int is true.
1239 See https://github.com/Perl/perl5/issues/19625.
1243 SETn( roundl( Perl_pow( left, right) ) );
1245 else SETn( Perl_pow( left, right) );
1248 SETn( Perl_pow( left, right) );
1249 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1251 #ifdef PERL_PRESERVE_IVUV
1253 SvIV_please_nomg(svr);
1261 dSP; dATARGET; SV *svl, *svr;
1262 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1266 #ifdef PERL_PRESERVE_IVUV
1268 /* special-case some simple common cases */
1269 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1271 U32 flags = (svl->sv_flags & svr->sv_flags);
1272 if (flags & SVf_IOK) {
1273 /* both args are simple IVs */
1278 topl = ((UV)il) >> (UVSIZE * 4 - 1);
1279 topr = ((UV)ir) >> (UVSIZE * 4 - 1);
1281 /* if both are in a range that can't under/overflow, do a
1282 * simple integer multiply: if the top halves(*) of both numbers
1283 * are 00...00 or 11...11, then it's safe.
1284 * (*) for 32-bits, the "top half" is the top 17 bits,
1285 * for 64-bits, its 33 bits */
1287 ((topl+1) | (topr+1))
1288 & ( (((UV)1) << (UVSIZE * 4 + 1)) - 2) /* 11..110 */
1291 TARGi(il * ir, 0); /* args not GMG, so can't be tainted */
1297 else if (flags & SVf_NOK) {
1298 /* both args are NVs */
1303 if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
1304 /* nothing was lost by converting to IVs */
1309 # if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
1310 if (Perl_isinf(result)) {
1311 Zero((U8*)&result + 8, 8, U8);
1314 TARGn(result, 0); /* args not GMG, so can't be tainted */
1322 if (SvIV_please_nomg(svr)) {
1323 /* Unless the left argument is integer in range we are going to have to
1324 use NV maths. Hence only attempt to coerce the right argument if
1325 we know the left is integer. */
1326 /* Left operand is defined, so is it IV? */
1327 if (SvIV_please_nomg(svl)) {
1328 bool auvok = SvUOK(svl);
1329 bool buvok = SvUOK(svr);
1330 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1331 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1340 const IV aiv = SvIVX(svl);
1343 auvok = TRUE; /* effectively it's a UV now */
1345 /* abs, auvok == false records sign; Using 0- here and
1346 * later to silence bogus warning from MS VC */
1347 alow = (UV) (0 - (UV) aiv);
1353 const IV biv = SvIVX(svr);
1356 buvok = TRUE; /* effectively it's a UV now */
1358 /* abs, buvok == false records sign */
1359 blow = (UV) (0 - (UV) biv);
1363 /* If this does sign extension on unsigned it's time for plan B */
1364 ahigh = alow >> (4 * sizeof (UV));
1366 bhigh = blow >> (4 * sizeof (UV));
1368 if (ahigh && bhigh) {
1370 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1371 which is overflow. Drop to NVs below. */
1372 } else if (!ahigh && !bhigh) {
1373 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1374 so the unsigned multiply cannot overflow. */
1375 const UV product = alow * blow;
1376 if (auvok == buvok) {
1377 /* -ve * -ve or +ve * +ve gives a +ve result. */
1381 } else if (product <= (UV)IV_MIN) {
1382 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1383 /* -ve result, which could overflow an IV */
1385 /* can't negate IV_MIN, but there are aren't two
1386 * integers such that !ahigh && !bhigh, where the
1387 * product equals 0x800....000 */
1388 assert(product != (UV)IV_MIN);
1389 SETi( -(IV)product );
1391 } /* else drop to NVs below. */
1393 /* One operand is large, 1 small */
1396 /* swap the operands */
1398 bhigh = blow; /* bhigh now the temp var for the swap */
1402 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1403 multiplies can't overflow. shift can, add can, -ve can. */
1404 product_middle = ahigh * blow;
1405 if (!(product_middle & topmask)) {
1406 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1408 product_middle <<= (4 * sizeof (UV));
1409 product_low = alow * blow;
1411 /* as for pp_add, UV + something mustn't get smaller.
1412 IIRC ANSI mandates this wrapping *behaviour* for
1413 unsigned whatever the actual representation*/
1414 product_low += product_middle;
1415 if (product_low >= product_middle) {
1416 /* didn't overflow */
1417 if (auvok == buvok) {
1418 /* -ve * -ve or +ve * +ve gives a +ve result. */
1420 SETu( product_low );
1422 } else if (product_low <= (UV)IV_MIN) {
1423 /* 2s complement assumption again */
1424 /* -ve result, which could overflow an IV */
1426 SETi(product_low == (UV)IV_MIN
1427 ? IV_MIN : -(IV)product_low);
1429 } /* else drop to NVs below. */
1431 } /* product_middle too large */
1432 } /* ahigh && bhigh */
1437 NV right = SvNV_nomg(svr);
1438 NV left = SvNV_nomg(svl);
1439 NV result = left * right;
1442 #if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
1443 if (Perl_isinf(result)) {
1444 Zero((U8*)&result + 8, 8, U8);
1454 dSP; dATARGET; SV *svl, *svr;
1455 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1458 /* Only try to do UV divide first
1459 if ((SLOPPYDIVIDE is true) or
1460 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1462 The assumption is that it is better to use floating point divide
1463 whenever possible, only doing integer divide first if we can't be sure.
1464 If NV_PRESERVES_UV is true then we know at compile time that no UV
1465 can be too large to preserve, so don't need to compile the code to
1466 test the size of UVs. */
1468 #if defined(SLOPPYDIVIDE) || (defined(PERL_PRESERVE_IVUV) && !defined(NV_PRESERVES_UV))
1469 # define PERL_TRY_UV_DIVIDE
1470 /* ensure that 20./5. == 4. */
1473 #ifdef PERL_TRY_UV_DIVIDE
1474 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1475 bool left_non_neg = SvUOK(svl);
1476 bool right_non_neg = SvUOK(svr);
1480 if (right_non_neg) {
1484 const IV biv = SvIVX(svr);
1487 right_non_neg = TRUE; /* effectively it's a UV now */
1493 /* historically undef()/0 gives a "Use of uninitialized value"
1494 warning before dieing, hence this test goes here.
1495 If it were immediately before the second SvIV_please, then
1496 DIE() would be invoked before left was even inspected, so
1497 no inspection would give no warning. */
1499 DIE(aTHX_ "Illegal division by zero");
1505 const IV aiv = SvIVX(svl);
1508 left_non_neg = TRUE; /* effectively it's a UV now */
1517 /* For sloppy divide we always attempt integer division. */
1519 /* Otherwise we only attempt it if either or both operands
1520 would not be preserved by an NV. If both fit in NVs
1521 we fall through to the NV divide code below. However,
1522 as left >= right to ensure integer result here, we know that
1523 we can skip the test on the right operand - right big
1524 enough not to be preserved can't get here unless left is
1527 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1530 /* Integer division can't overflow, but it can be imprecise. */
1532 /* Modern compilers optimize division followed by
1533 * modulo into a single div instruction */
1534 const UV result = left / right;
1535 if (left % right == 0) {
1536 SP--; /* result is valid */
1537 if (left_non_neg == right_non_neg) {
1538 /* signs identical, result is positive. */
1542 /* 2s complement assumption */
1543 if (result <= (UV)IV_MIN)
1544 SETi(result == (UV)IV_MIN ? IV_MIN : -(IV)result);
1546 /* It's exact but too negative for IV. */
1547 SETn( -(NV)result );
1550 } /* tried integer divide but it was not an integer result */
1551 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1552 } /* one operand wasn't SvIOK */
1553 #endif /* PERL_TRY_UV_DIVIDE */
1555 NV right = SvNV_nomg(svr);
1556 NV left = SvNV_nomg(svl);
1557 (void)POPs;(void)POPs;
1558 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1559 if (! Perl_isnan(right) && right == 0.0)
1563 DIE(aTHX_ "Illegal division by zero");
1564 PUSHn( left / right );
1572 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1576 bool left_neg = FALSE;
1577 bool right_neg = FALSE;
1578 bool use_double = FALSE;
1579 bool dright_valid = FALSE;
1582 SV * const svr = TOPs;
1583 SV * const svl = TOPm1s;
1584 if (SvIV_please_nomg(svr)) {
1585 right_neg = !SvUOK(svr);
1589 const IV biv = SvIVX(svr);
1592 right_neg = FALSE; /* effectively it's a UV now */
1594 right = (UV) (0 - (UV) biv);
1599 dright = SvNV_nomg(svr);
1600 right_neg = dright < 0;
1603 if (dright < UV_MAX_P1) {
1604 right = U_V(dright);
1605 dright_valid = TRUE; /* In case we need to use double below. */
1611 /* At this point use_double is only true if right is out of range for
1612 a UV. In range NV has been rounded down to nearest UV and
1613 use_double false. */
1614 if (!use_double && SvIV_please_nomg(svl)) {
1615 left_neg = !SvUOK(svl);
1619 const IV aiv = SvIVX(svl);
1622 left_neg = FALSE; /* effectively it's a UV now */
1624 left = (UV) (0 - (UV) aiv);
1629 dleft = SvNV_nomg(svl);
1630 left_neg = dleft < 0;
1634 /* This should be exactly the 5.6 behaviour - if left and right are
1635 both in range for UV then use U_V() rather than floor. */
1637 if (dleft < UV_MAX_P1) {
1638 /* right was in range, so is dleft, so use UVs not double.
1642 /* left is out of range for UV, right was in range, so promote
1643 right (back) to double. */
1645 /* The +0.5 is used in 5.6 even though it is not strictly
1646 consistent with the implicit +0 floor in the U_V()
1647 inside the #if 1. */
1648 dleft = Perl_floor(dleft + 0.5);
1651 dright = Perl_floor(dright + 0.5);
1662 DIE(aTHX_ "Illegal modulus zero");
1664 dans = Perl_fmod(dleft, dright);
1665 if ((left_neg != right_neg) && dans)
1666 dans = dright - dans;
1669 sv_setnv(TARG, dans);
1675 DIE(aTHX_ "Illegal modulus zero");
1678 if ((left_neg != right_neg) && ans)
1681 /* XXX may warn: unary minus operator applied to unsigned type */
1682 /* could change -foo to be (~foo)+1 instead */
1683 if (ans <= ~((UV)IV_MAX)+1)
1684 sv_setiv(TARG, ~ans+1);
1686 sv_setnv(TARG, -(NV)ans);
1689 sv_setuv(TARG, ans);
1701 bool infnan = FALSE;
1702 const U8 gimme = GIMME_V;
1704 if (gimme == G_LIST && PL_op->op_private & OPpREPEAT_DOLIST) {
1705 /* TODO: think of some way of doing list-repeat overloading ??? */
1710 if (UNLIKELY(PL_op->op_private & OPpREPEAT_DOLIST)) {
1711 /* The parser saw this as a list repeat, and there
1712 are probably several items on the stack. But we're
1713 in scalar/void context, and there's no pp_list to save us
1714 now. So drop the rest of the items -- robin@kitsite.com
1717 if (MARK + 1 < SP) {
1723 ASSUME(MARK + 1 == SP);
1726 MARK[1] = &PL_sv_undef;
1730 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1736 const UV uv = SvUV_nomg(sv);
1738 count = IV_MAX; /* The best we can do? */
1742 count = SvIV_nomg(sv);
1745 else if (SvNOKp(sv)) {
1746 const NV nv = SvNV_nomg(sv);
1747 infnan = Perl_isinfnan(nv);
1748 if (UNLIKELY(infnan)) {
1752 count = -1; /* An arbitrary negative integer */
1758 count = SvIV_nomg(sv);
1761 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
1762 "Non-finite repeat count does nothing");
1763 } else if (count < 0) {
1765 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
1766 "Negative repeat count does nothing");
1769 if (gimme == G_LIST && PL_op->op_private & OPpREPEAT_DOLIST) {
1771 const SSize_t items = SP - MARK;
1772 const U8 mod = PL_op->op_flags & OPf_MOD;
1777 if ( items > SSize_t_MAX / count /* max would overflow */
1778 /* repeatcpy would overflow */
1779 || items > I32_MAX / (I32)sizeof(SV *)
1781 Perl_croak(aTHX_ "%s","Out of memory during list extend");
1782 max = items * count;
1787 if (mod && SvPADTMP(*SP)) {
1788 *SP = sv_mortalcopy(*SP);
1795 repeatcpy((char*)(MARK + items), (char*)MARK,
1796 items * sizeof(const SV *), count - 1);
1799 else if (count <= 0)
1802 else { /* Note: mark already snarfed by pp_list */
1803 SV * const tmpstr = POPs;
1808 sv_setsv_nomg(TARG, tmpstr);
1809 SvPV_force_nomg(TARG, len);
1810 isutf = DO_UTF8(TARG);
1817 if ( len > (MEM_SIZE_MAX-1) / (UV)count /* max would overflow */
1818 || len > (U32)I32_MAX /* repeatcpy would overflow */
1820 Perl_croak(aTHX_ "%s",
1821 "Out of memory during string extend");
1822 max = (UV)count * len + 1;
1825 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1826 SvCUR_set(TARG, SvCUR(TARG) * count);
1828 *SvEND(TARG) = '\0';
1831 (void)SvPOK_only_UTF8(TARG);
1833 (void)SvPOK_only(TARG);
1842 dSP; dATARGET; bool useleft; SV *svl, *svr;
1843 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1847 #ifdef PERL_PRESERVE_IVUV
1849 /* special-case some simple common cases */
1850 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1852 U32 flags = (svl->sv_flags & svr->sv_flags);
1853 if (flags & SVf_IOK) {
1854 /* both args are simple IVs */
1859 topl = ((UV)il) >> (UVSIZE * 8 - 2);
1860 topr = ((UV)ir) >> (UVSIZE * 8 - 2);
1862 /* if both are in a range that can't under/overflow, do a
1863 * simple integer subtract: if the top of both numbers
1864 * are 00 or 11, then it's safe */
1865 if (!( ((topl+1) | (topr+1)) & 2)) {
1867 TARGi(il - ir, 0); /* args not GMG, so can't be tainted */
1873 else if (flags & SVf_NOK) {
1874 /* both args are NVs */
1878 if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
1879 /* nothing was lost by converting to IVs */
1883 TARGn(nl - nr, 0); /* args not GMG, so can't be tainted */
1891 useleft = USE_LEFT(svl);
1892 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1893 "bad things" happen if you rely on signed integers wrapping. */
1894 if (SvIV_please_nomg(svr)) {
1895 /* Unless the left argument is integer in range we are going to have to
1896 use NV maths. Hence only attempt to coerce the right argument if
1897 we know the left is integer. */
1904 a_valid = auvok = 1;
1905 /* left operand is undef, treat as zero. */
1907 /* Left operand is defined, so is it IV? */
1908 if (SvIV_please_nomg(svl)) {
1909 if ((auvok = SvUOK(svl)))
1912 const IV aiv = SvIVX(svl);
1915 auvok = 1; /* Now acting as a sign flag. */
1917 auv = (UV) (0 - (UV) aiv);
1924 bool result_good = 0;
1927 bool buvok = SvUOK(svr);
1932 const IV biv = SvIVX(svr);
1937 buv = (UV) (0 - (UV) biv);
1939 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1940 else "IV" now, independent of how it came in.
1941 if a, b represents positive, A, B negative, a maps to -A etc
1946 all UV maths. negate result if A negative.
1947 subtract if signs same, add if signs differ. */
1949 if (auvok ^ buvok) {
1958 /* Must get smaller */
1963 if (result <= buv) {
1964 /* result really should be -(auv-buv). as its negation
1965 of true value, need to swap our result flag */
1977 if (result <= (UV)IV_MIN)
1978 SETi(result == (UV)IV_MIN
1979 ? IV_MIN : -(IV)result);
1981 /* result valid, but out of range for IV. */
1982 SETn( -(NV)result );
1986 } /* Overflow, drop through to NVs. */
1990 useleft = USE_LEFT(svl);
1993 NV value = SvNV_nomg(svr);
1997 /* left operand is undef, treat as zero - value */
2001 SETn( SvNV_nomg(svl) - value );
2006 #define IV_BITS (IVSIZE * 8)
2008 /* Taking the right operand of bitwise shift operators, returns an int
2009 * indicating the shift amount clipped to the range [-IV_BITS, +IV_BITS].
2012 S_shift_amount(pTHX_ SV *const svr)
2014 const IV iv = SvIV_nomg(svr);
2016 /* Note that [INT_MIN, INT_MAX] cannot be used as the clipping bound;
2017 * INT_MIN will cause overflow in "shift = -shift;" in S_{iv,uv}_shift.
2020 return SvUVX(svr) > IV_BITS ? IV_BITS : (int)SvUVX(svr);
2021 return iv < -IV_BITS ? -IV_BITS : iv > IV_BITS ? IV_BITS : (int)iv;
2024 static UV S_uv_shift(UV uv, int shift, bool left)
2030 if (UNLIKELY(shift >= IV_BITS)) {
2033 return left ? uv << shift : uv >> shift;
2036 static IV S_iv_shift(IV iv, int shift, bool left)
2043 if (UNLIKELY(shift >= IV_BITS)) {
2044 return iv < 0 && !left ? -1 : 0;
2047 /* For left shifts, perl 5 has chosen to treat the value as unsigned for
2048 * the purposes of shifting, then cast back to signed. This is very
2049 * different from Raku:
2051 * $ raku -e 'say -2 +< 5'
2054 * $ ./perl -le 'print -2 << 5'
2055 * 18446744073709551552
2058 return (IV) (((UV) iv) << shift);
2061 /* Here is right shift */
2065 #define UV_LEFT_SHIFT(uv, shift) S_uv_shift(uv, shift, TRUE)
2066 #define UV_RIGHT_SHIFT(uv, shift) S_uv_shift(uv, shift, FALSE)
2067 #define IV_LEFT_SHIFT(iv, shift) S_iv_shift(iv, shift, TRUE)
2068 #define IV_RIGHT_SHIFT(iv, shift) S_iv_shift(iv, shift, FALSE)
2072 dSP; dATARGET; SV *svl, *svr;
2073 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
2077 const int shift = S_shift_amount(aTHX_ svr);
2078 if (PL_op->op_private & OPpUSEINT) {
2079 SETi(IV_LEFT_SHIFT(SvIV_nomg(svl), shift));
2082 SETu(UV_LEFT_SHIFT(SvUV_nomg(svl), shift));
2090 dSP; dATARGET; SV *svl, *svr;
2091 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
2095 const int shift = S_shift_amount(aTHX_ svr);
2096 if (PL_op->op_private & OPpUSEINT) {
2097 SETi(IV_RIGHT_SHIFT(SvIV_nomg(svl), shift));
2100 SETu(UV_RIGHT_SHIFT(SvUV_nomg(svl), shift));
2110 U32 flags_and, flags_or;
2112 tryAMAGICbin_MG(lt_amg, AMGf_numeric);
2115 flags_and = SvFLAGS(left) & SvFLAGS(right);
2116 flags_or = SvFLAGS(left) | SvFLAGS(right);
2119 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2120 ? (SvIVX(left) < SvIVX(right))
2121 : (flags_and & SVf_NOK)
2122 ? (SvNVX(left) < SvNVX(right))
2123 : (do_ncmp(left, right) == -1)
2132 U32 flags_and, flags_or;
2134 tryAMAGICbin_MG(gt_amg, AMGf_numeric);
2137 flags_and = SvFLAGS(left) & SvFLAGS(right);
2138 flags_or = SvFLAGS(left) | SvFLAGS(right);
2141 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2142 ? (SvIVX(left) > SvIVX(right))
2143 : (flags_and & SVf_NOK)
2144 ? (SvNVX(left) > SvNVX(right))
2145 : (do_ncmp(left, right) == 1)
2154 U32 flags_and, flags_or;
2156 tryAMAGICbin_MG(le_amg, AMGf_numeric);
2159 flags_and = SvFLAGS(left) & SvFLAGS(right);
2160 flags_or = SvFLAGS(left) | SvFLAGS(right);
2163 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2164 ? (SvIVX(left) <= SvIVX(right))
2165 : (flags_and & SVf_NOK)
2166 ? (SvNVX(left) <= SvNVX(right))
2167 : (do_ncmp(left, right) <= 0)
2176 U32 flags_and, flags_or;
2178 tryAMAGICbin_MG(ge_amg, AMGf_numeric);
2181 flags_and = SvFLAGS(left) & SvFLAGS(right);
2182 flags_or = SvFLAGS(left) | SvFLAGS(right);
2185 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2186 ? (SvIVX(left) >= SvIVX(right))
2187 : (flags_and & SVf_NOK)
2188 ? (SvNVX(left) >= SvNVX(right))
2189 : ( (do_ncmp(left, right) & 2) == 0)
2198 U32 flags_and, flags_or;
2200 tryAMAGICbin_MG(ne_amg, AMGf_numeric);
2203 flags_and = SvFLAGS(left) & SvFLAGS(right);
2204 flags_or = SvFLAGS(left) | SvFLAGS(right);
2207 ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
2208 ? (SvIVX(left) != SvIVX(right))
2209 : (flags_and & SVf_NOK)
2210 ? (SvNVX(left) != SvNVX(right))
2211 : (do_ncmp(left, right) != 0)
2216 /* compare left and right SVs. Returns:
2220 * 2: left or right was a NaN
2223 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2225 PERL_ARGS_ASSERT_DO_NCMP;
2226 #ifdef PERL_PRESERVE_IVUV
2227 /* Fortunately it seems NaN isn't IOK */
2228 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
2230 const IV leftiv = SvIVX(left);
2231 if (!SvUOK(right)) {
2232 /* ## IV <=> IV ## */
2233 const IV rightiv = SvIVX(right);
2234 return (leftiv > rightiv) - (leftiv < rightiv);
2236 /* ## IV <=> UV ## */
2238 /* As (b) is a UV, it's >=0, so it must be < */
2241 const UV rightuv = SvUVX(right);
2242 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2247 /* ## UV <=> UV ## */
2248 const UV leftuv = SvUVX(left);
2249 const UV rightuv = SvUVX(right);
2250 return (leftuv > rightuv) - (leftuv < rightuv);
2252 /* ## UV <=> IV ## */
2254 const IV rightiv = SvIVX(right);
2256 /* As (a) is a UV, it's >=0, so it cannot be < */
2259 const UV leftuv = SvUVX(left);
2260 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2263 NOT_REACHED; /* NOTREACHED */
2267 NV const rnv = SvNV_nomg(right);
2268 NV const lnv = SvNV_nomg(left);
2270 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2271 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2274 return (lnv > rnv) - (lnv < rnv);
2293 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2296 value = do_ncmp(left, right);
2308 /* also used for: pp_sge() pp_sgt() pp_slt() */
2314 int amg_type = sle_amg;
2318 switch (PL_op->op_type) {
2337 tryAMAGICbin_MG(amg_type, 0);
2341 #ifdef USE_LOCALE_COLLATE
2342 (IN_LC_RUNTIME(LC_COLLATE))
2343 ? sv_cmp_locale_flags(left, right, 0)
2346 sv_cmp_flags(left, right, 0);
2347 SETs(boolSV(cmp * multiplier < rhs));
2355 tryAMAGICbin_MG(seq_amg, 0);
2358 SETs(boolSV(sv_eq_flags(left, right, 0)));
2366 tryAMAGICbin_MG(sne_amg, 0);
2369 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2377 tryAMAGICbin_MG(scmp_amg, 0);
2381 #ifdef USE_LOCALE_COLLATE
2382 (IN_LC_RUNTIME(LC_COLLATE))
2383 ? sv_cmp_locale_flags(left, right, 0)
2386 sv_cmp_flags(left, right, 0);
2395 tryAMAGICbin_MG(band_amg, AMGf_assign);
2398 if (SvNIOKp(left) || SvNIOKp(right)) {
2399 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2400 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2401 if (PL_op->op_private & OPpUSEINT) {
2402 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2406 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2409 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2410 if (right_ro_nonnum) SvNIOK_off(right);
2413 do_vop(PL_op->op_type, TARG, left, right);
2423 tryAMAGICbin_MG(band_amg, AMGf_assign|AMGf_numarg);
2425 dATARGET; dPOPTOPssrl;
2426 if (PL_op->op_private & OPpUSEINT) {
2427 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2431 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2441 tryAMAGICbin_MG(sband_amg, AMGf_assign);
2443 dATARGET; dPOPTOPssrl;
2444 do_vop(OP_BIT_AND, TARG, left, right);
2449 /* also used for: pp_bit_xor() */
2454 const int op_type = PL_op->op_type;
2456 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2459 if (SvNIOKp(left) || SvNIOKp(right)) {
2460 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2461 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2462 if (PL_op->op_private & OPpUSEINT) {
2463 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2464 const IV r = SvIV_nomg(right);
2465 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2469 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2470 const UV r = SvUV_nomg(right);
2471 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2474 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2475 if (right_ro_nonnum) SvNIOK_off(right);
2478 do_vop(op_type, TARG, left, right);
2485 /* also used for: pp_nbit_xor() */
2490 const int op_type = PL_op->op_type;
2492 tryAMAGICbin_MG((op_type == OP_NBIT_OR ? bor_amg : bxor_amg),
2493 AMGf_assign|AMGf_numarg);
2495 dATARGET; dPOPTOPssrl;
2496 if (PL_op->op_private & OPpUSEINT) {
2497 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2498 const IV r = SvIV_nomg(right);
2499 const IV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2503 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2504 const UV r = SvUV_nomg(right);
2505 const UV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2512 /* also used for: pp_sbit_xor() */
2517 const int op_type = PL_op->op_type;
2519 tryAMAGICbin_MG((op_type == OP_SBIT_OR ? sbor_amg : sbxor_amg),
2522 dATARGET; dPOPTOPssrl;
2523 do_vop(op_type == OP_SBIT_OR ? OP_BIT_OR : OP_BIT_XOR, TARG, left,
2529 PERL_STATIC_INLINE bool
2530 S_negate_string(pTHX)
2535 SV * const sv = TOPs;
2536 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2538 s = SvPV_nomg_const(sv, len);
2539 if (isIDFIRST(*s)) {
2540 sv_setpvs(TARG, "-");
2543 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2544 sv_setsv_nomg(TARG, sv);
2545 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2555 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2556 if (S_negate_string(aTHX)) return NORMAL;
2558 SV * const sv = TOPs;
2561 /* It's publicly an integer */
2564 if (SvIVX(sv) == IV_MIN) {
2565 /* 2s complement assumption. */
2566 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) ==
2570 else if (SvUVX(sv) <= IV_MAX) {
2575 else if (SvIVX(sv) != IV_MIN) {
2579 #ifdef PERL_PRESERVE_IVUV
2586 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2587 SETn(-SvNV_nomg(sv));
2588 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2589 goto oops_its_an_int;
2591 SETn(-SvNV_nomg(sv));
2601 tryAMAGICun_MG(not_amg, 0);
2603 *PL_stack_sp = boolSV(!SvTRUE_nomg_NN(sv));
2608 S_scomplement(pTHX_ SV *targ, SV *sv)
2614 sv_copypv_nomg(TARG, sv);
2615 tmps = (U8*)SvPV_nomg(TARG, len);
2618 if (len && ! utf8_to_bytes(tmps, &len)) {
2619 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[PL_op->op_type]);
2621 SvCUR_set(TARG, len);
2629 for ( ; anum && PTR2nat(tmps) % sizeof(long); anum--, tmps++)
2632 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2637 for ( ; anum > 0; anum--, tmps++)
2644 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2648 if (PL_op->op_private & OPpUSEINT) {
2649 const IV i = ~SvIV_nomg(sv);
2653 const UV u = ~SvUV_nomg(sv);
2658 S_scomplement(aTHX_ TARG, sv);
2668 tryAMAGICun_MG(compl_amg, AMGf_numeric|AMGf_numarg);
2671 if (PL_op->op_private & OPpUSEINT) {
2672 const IV i = ~SvIV_nomg(sv);
2676 const UV u = ~SvUV_nomg(sv);
2686 tryAMAGICun_MG(scompl_amg, AMGf_numeric);
2689 S_scomplement(aTHX_ TARG, sv);
2695 /* integer versions of some of the above */
2700 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2703 SETi( (IV)((UV)left * (UV)right) );
2712 tryAMAGICbin_MG(div_amg, AMGf_assign);
2715 IV value = SvIV_nomg(right);
2717 DIE(aTHX_ "Illegal division by zero");
2718 num = SvIV_nomg(left);
2720 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2722 value = (IV)-(UV)num;
2724 value = num / value;
2733 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2737 DIE(aTHX_ "Illegal modulus zero");
2738 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2742 SETi( left % right );
2750 tryAMAGICbin_MG(add_amg, AMGf_assign);
2752 dPOPTOPiirl_ul_nomg;
2753 SETi( (IV)((UV)left + (UV)right) );
2761 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2763 dPOPTOPiirl_ul_nomg;
2764 SETi( (IV)((UV)left - (UV)right) );
2772 tryAMAGICbin_MG(lt_amg, 0);
2775 SETs(boolSV(left < right));
2783 tryAMAGICbin_MG(gt_amg, 0);
2786 SETs(boolSV(left > right));
2794 tryAMAGICbin_MG(le_amg, 0);
2797 SETs(boolSV(left <= right));
2805 tryAMAGICbin_MG(ge_amg, 0);
2808 SETs(boolSV(left >= right));
2816 tryAMAGICbin_MG(eq_amg, 0);
2819 SETs(boolSV(left == right));
2827 tryAMAGICbin_MG(ne_amg, 0);
2830 SETs(boolSV(left != right));
2838 tryAMAGICbin_MG(ncmp_amg, 0);
2845 else if (left < right)
2857 tryAMAGICun_MG(neg_amg, 0);
2858 if (S_negate_string(aTHX)) return NORMAL;
2860 SV * const sv = TOPs;
2861 IV const i = SvIV_nomg(sv);
2867 /* High falutin' math. */
2872 tryAMAGICbin_MG(atan2_amg, 0);
2875 SETn(Perl_atan2(left, right));
2881 /* also used for: pp_cos() pp_exp() pp_log() pp_sqrt() */
2886 int amg_type = fallback_amg;
2887 const char *neg_report = NULL;
2888 const int op_type = PL_op->op_type;
2891 case OP_SIN: amg_type = sin_amg; break;
2892 case OP_COS: amg_type = cos_amg; break;
2893 case OP_EXP: amg_type = exp_amg; break;
2894 case OP_LOG: amg_type = log_amg; neg_report = "log"; break;
2895 case OP_SQRT: amg_type = sqrt_amg; neg_report = "sqrt"; break;
2898 assert(amg_type != fallback_amg);
2900 tryAMAGICun_MG(amg_type, 0);
2902 SV * const arg = TOPs;
2903 const NV value = SvNV_nomg(arg);
2909 if (neg_report) { /* log or sqrt */
2911 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2912 ! Perl_isnan(value) &&
2914 (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)))
2918 SET_NUMERIC_STANDARD();
2919 mesg = Perl_form(aTHX_ "Can't take %s of %" NVgf, neg_report, value);
2922 /* diag_listed_as: Can't take log of %g */
2923 DIE(aTHX_ "%s", mesg);
2928 case OP_SIN: result = Perl_sin(value); break;
2929 case OP_COS: result = Perl_cos(value); break;
2930 case OP_EXP: result = Perl_exp(value); break;
2931 case OP_LOG: result = Perl_log(value); break;
2932 case OP_SQRT: result = Perl_sqrt(value); break;
2939 /* Support Configure command-line overrides for rand() functions.
2940 After 5.005, perhaps we should replace this by Configure support
2941 for drand48(), random(), or rand(). For 5.005, though, maintain
2942 compatibility by calling rand() but allow the user to override it.
2943 See INSTALL for details. --Andy Dougherty 15 July 1998
2945 /* Now it's after 5.005, and Configure supports drand48() and random(),
2946 in addition to rand(). So the overrides should not be needed any more.
2947 --Jarkko Hietaniemi 27 September 1998
2952 if (!PL_srand_called) {
2954 if (PL_srand_override) {
2955 /* env var PERL_RAND_SEED has been set so the user wants
2956 * consistent srand() initialization. */
2957 PERL_SRAND_OVERRIDE_GET(s);
2959 /* Pseudo random initialization from context state and possible
2961 s= (Rand_seed_t)seed();
2963 (void)seedDrand01(s);
2964 PL_srand_called = TRUE;
2976 SV * const sv = POPs;
2982 /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
2983 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2984 if (! Perl_isnan(value) && value == 0.0)
2994 sv_setnv_mg(TARG, value);
3005 if (MAXARG >= 1 && (TOPs || POPs)) {
3012 pv = SvPV(top, len);
3013 flags = grok_number(pv, len, &anum);
3015 if (!(flags & IS_NUMBER_IN_UV)) {
3016 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
3017 "Integer overflow in srand");
3022 if (PL_srand_override) {
3023 /* env var PERL_RAND_SEED has been set so the user wants
3024 * consistent srand() initialization. */
3025 PERL_SRAND_OVERRIDE_GET(anum);
3031 (void)seedDrand01((Rand_seed_t)anum);
3032 PL_srand_called = TRUE;
3036 /* Historically srand always returned true. We can avoid breaking
3038 sv_setpvs(TARG, "0 but true");
3047 tryAMAGICun_MG(int_amg, AMGf_numeric);
3049 SV * const sv = TOPs;
3050 const IV iv = SvIV_nomg(sv);
3051 /* XXX it's arguable that compiler casting to IV might be subtly
3052 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
3053 else preferring IV has introduced a subtle behaviour change bug. OTOH
3054 relying on floating point to be accurate is a bug. */
3059 else if (SvIOK(sv)) {
3061 SETu(SvUV_nomg(sv));
3066 const NV value = SvNV_nomg(sv);
3067 if (UNLIKELY(Perl_isinfnan(value)))
3069 else if (value >= 0.0) {
3070 if (value < (NV)UV_MAX + 0.5) {
3073 SETn(Perl_floor(value));
3077 if (value > (NV)IV_MIN - 0.5) {
3080 SETn(Perl_ceil(value));
3091 tryAMAGICun_MG(abs_amg, AMGf_numeric);
3093 SV * const sv = TOPs;
3094 /* This will cache the NV value if string isn't actually integer */
3095 const IV iv = SvIV_nomg(sv);
3102 else if (SvIOK(sv)) {
3103 /* IVX is precise */
3105 uv = SvUVX(sv); /* force it to be numeric only */
3110 /* "(UV)-(iv + 1) + 1" below is mathematically "-iv", but
3111 transformed so that every subexpression will never trigger
3112 overflows even on 2's complement representation (note that
3113 iv is always < 0 here), and modern compilers could optimize
3114 this to a single negation. */
3115 uv = (UV)-(iv + 1) + 1;
3121 const NV value = SvNV_nomg(sv);
3122 SETn(Perl_fabs(value));
3129 /* also used for: pp_hex() */
3135 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3139 SV* const sv = TOPs;
3141 tmps = (SvPV_const(sv, len));
3143 /* If Unicode, try to downgrade
3144 * If not possible, croak. */
3145 SV* const tsv = sv_2mortal(newSVsv(sv));
3148 (void)sv_utf8_downgrade(tsv, FALSE);
3149 tmps = SvPV_const(tsv, len);
3151 if (PL_op->op_type == OP_HEX)
3154 while (*tmps && len && isSPACE(*tmps))
3158 if (isALPHA_FOLD_EQ(*tmps, 'x')) {
3160 flags |= PERL_SCAN_DISALLOW_PREFIX;
3162 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3164 else if (isALPHA_FOLD_EQ(*tmps, 'b')) {
3166 flags |= PERL_SCAN_DISALLOW_PREFIX;
3167 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3170 if (isALPHA_FOLD_EQ(*tmps, 'o')) {
3173 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3176 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3191 SV * const sv = TOPs;
3193 U32 in_bytes = IN_BYTES;
3194 /* Simplest case shortcut:
3195 * set svflags to just the SVf_POK|SVs_GMG|SVf_UTF8 from the SV,
3196 * with the SVf_UTF8 flag inverted if under 'use bytes' (HINT_BYTES
3199 U32 svflags = (SvFLAGS(sv) ^ (in_bytes << 26)) & (SVf_POK|SVs_GMG|SVf_UTF8);
3201 STATIC_ASSERT_STMT(SVf_UTF8 == (HINT_BYTES << 26));
3204 if (LIKELY(svflags == SVf_POK))
3207 if (svflags & SVs_GMG)
3212 if (!IN_BYTES) { /* reread to avoid using an C auto/register */
3213 if ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == SVf_POK)
3215 if ( SvPOK(sv) && (PL_op->op_private & OPpTRUEBOOL)) {
3216 /* no need to convert from bytes to chars */
3220 len = sv_len_utf8_nomg(sv);
3223 /* unrolled SvPV_nomg_const(sv,len) */
3224 if (SvPOK_nog(sv)) {
3227 if (PL_op->op_private & OPpTRUEBOOL) {
3229 SETs(len ? &PL_sv_yes : &PL_sv_zero);
3234 (void)sv_2pv_flags(sv, &len, 0|SV_CONST_RETURN);
3237 TARGi((IV)(len), 1);
3240 if (!SvPADTMP(TARG)) {
3241 /* OPpTARGET_MY: targ is var in '$lex = length()' */
3246 /* TARG is on stack at this point and is overwriten by SETs.
3247 * This branch is the odd one out, so put TARG by default on
3248 * stack earlier to let local SP go out of liveness sooner */
3251 return NORMAL; /* no putback, SP didn't move in this opcode */
3255 /* Returns false if substring is completely outside original string.
3256 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
3257 always be true for an explicit 0.
3260 Perl_translate_substr_offsets( STRLEN curlen, IV pos1_iv,
3261 bool pos1_is_uv, IV len_iv,
3262 bool len_is_uv, STRLEN *posp,
3268 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
3270 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
3271 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3274 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
3277 if (len_iv || len_is_uv) {
3278 if (!len_is_uv && len_iv < 0) {
3279 pos2_iv = curlen + len_iv;
3281 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3284 } else { /* len_iv >= 0 */
3285 if (!pos1_is_uv && pos1_iv < 0) {
3286 pos2_iv = pos1_iv + len_iv;
3287 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3289 if ((UV)len_iv > curlen-(UV)pos1_iv)
3292 pos2_iv = pos1_iv+len_iv;
3302 if (!pos2_is_uv && pos2_iv < 0) {
3303 if (!pos1_is_uv && pos1_iv < 0)
3307 else if (!pos1_is_uv && pos1_iv < 0)
3310 if ((UV)pos2_iv < (UV)pos1_iv)
3312 if ((UV)pos2_iv > curlen)
3315 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3316 *posp = (STRLEN)( (UV)pos1_iv );
3317 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3334 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3335 const bool rvalue = (GIMME_V != G_VOID);
3338 const char *repl = NULL;
3340 int num_args = PL_op->op_private & 7;
3341 bool repl_need_utf8_upgrade = FALSE;
3345 if(!(repl_sv = POPs)) num_args--;
3347 if ((len_sv = POPs)) {
3348 len_iv = SvIV(len_sv);
3349 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3354 pos1_iv = SvIV(pos_sv);
3355 pos1_is_uv = SvIOK_UV(pos_sv);
3357 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3361 if (lvalue && !repl_sv) {
3363 ret = newSV_type_mortal(SVt_PVLV); /* Not TARG RT#67838 */
3364 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3366 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3368 pos1_is_uv || pos1_iv >= 0
3369 ? (STRLEN)(UV)pos1_iv
3370 : (LvFLAGS(ret) |= LVf_NEG_OFF, (STRLEN)(UV)-pos1_iv);
3372 len_is_uv || len_iv > 0
3373 ? (STRLEN)(UV)len_iv
3374 : (LvFLAGS(ret) |= LVf_NEG_LEN, (STRLEN)(UV)-len_iv);
3376 PUSHs(ret); /* avoid SvSETMAGIC here */
3380 repl = SvPV_const(repl_sv, repl_len);
3383 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3384 "Attempt to use reference as lvalue in substr"
3386 tmps = SvPV_force_nomg(sv, curlen);
3387 if (DO_UTF8(repl_sv) && repl_len) {
3389 /* Upgrade the dest, and recalculate tmps in case the buffer
3390 * got reallocated; curlen may also have been changed */
3391 sv_utf8_upgrade_nomg(sv);
3392 tmps = SvPV_nomg(sv, curlen);
3395 else if (DO_UTF8(sv))
3396 repl_need_utf8_upgrade = TRUE;
3398 else tmps = SvPV_const(sv, curlen);
3400 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3401 if (utf8_curlen == curlen)
3404 curlen = utf8_curlen;
3410 STRLEN pos, len, byte_len, byte_pos;
3412 if (!translate_substr_offsets(
3413 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3417 byte_pos = utf8_curlen
3418 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3423 SvTAINTED_off(TARG); /* decontaminate */
3424 SvUTF8_off(TARG); /* decontaminate */
3425 sv_setpvn(TARG, tmps, byte_len);
3426 #ifdef USE_LOCALE_COLLATE
3427 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3434 SV* repl_sv_copy = NULL;
3436 if (repl_need_utf8_upgrade) {
3437 repl_sv_copy = newSVsv(repl_sv);
3438 sv_utf8_upgrade(repl_sv_copy);
3439 repl = SvPV_const(repl_sv_copy, repl_len);
3443 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3444 SvREFCNT_dec(repl_sv_copy);
3447 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST)
3457 Perl_croak(aTHX_ "substr outside of string");
3458 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3465 const IV size = POPi;
3466 SV* offsetsv = POPs;
3467 SV * const src = POPs;
3468 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3474 /* extract a STRLEN-ranged integer value from offsetsv into offset,
3475 * or flag that its out of range */
3477 IV iv = SvIV(offsetsv);
3479 /* avoid a large UV being wrapped to a negative value */
3480 if (SvIOK_UV(offsetsv) && SvUVX(offsetsv) > (UV)IV_MAX)
3481 errflags = LVf_OUT_OF_RANGE;
3483 errflags = (LVf_NEG_OFF|LVf_OUT_OF_RANGE);
3484 #if PTRSIZE < IVSIZE
3485 else if (iv > Size_t_MAX)
3486 errflags = LVf_OUT_OF_RANGE;
3489 offset = (STRLEN)iv;
3492 retuv = errflags ? 0 : do_vecget(src, offset, size);
3494 if (lvalue) { /* it's an lvalue! */
3495 ret = newSV_type_mortal(SVt_PVLV); /* Not TARG RT#67838 */
3496 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3498 LvTARG(ret) = SvREFCNT_inc_simple(src);
3499 LvTARGOFF(ret) = offset;
3500 LvTARGLEN(ret) = size;
3501 LvFLAGS(ret) = errflags;
3505 SvTAINTED_off(TARG); /* decontaminate */
3509 sv_setuv(ret, retuv);
3517 /* also used for: pp_rindex() */
3530 const char *little_p;
3533 const bool is_index = PL_op->op_type == OP_INDEX;
3534 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3540 big_p = SvPV_const(big, biglen);
3541 little_p = SvPV_const(little, llen);
3543 big_utf8 = DO_UTF8(big);
3544 little_utf8 = DO_UTF8(little);
3545 if (big_utf8 ^ little_utf8) {
3546 /* One needs to be upgraded. */
3548 /* Well, maybe instead we might be able to downgrade the small
3550 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3553 /* If the large string is ISO-8859-1, and it's not possible to
3554 convert the small string to ISO-8859-1, then there is no
3555 way that it could be found anywhere by index. */
3560 /* At this point, pv is a malloc()ed string. So donate it to temp
3561 to ensure it will get free()d */
3562 little = temp = newSV_type(SVt_NULL);
3563 sv_usepvn(temp, pv, llen);
3564 little_p = SvPVX(little);
3566 temp = newSVpvn(little_p, llen);
3568 sv_utf8_upgrade(temp);
3570 little_p = SvPV_const(little, llen);
3573 if (SvGAMAGIC(big)) {
3574 /* Life just becomes a lot easier if I use a temporary here.
3575 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3576 will trigger magic and overloading again, as will fbm_instr()
3578 big = newSVpvn_flags(big_p, biglen,
3579 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3582 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3583 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3584 warn on undef, and we've already triggered a warning with the
3585 SvPV_const some lines above. We can't remove that, as we need to
3586 call some SvPV to trigger overloading early and find out if the
3588 This is all getting too messy. The API isn't quite clean enough,
3589 because data access has side effects.
3591 little = newSVpvn_flags(little_p, llen,
3592 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3593 little_p = SvPVX(little);
3597 offset = is_index ? 0 : biglen;
3599 if (big_utf8 && offset > 0)
3600 offset = sv_pos_u2b_flags(big, offset, 0, SV_CONST_RETURN);
3606 else if (offset > (SSize_t)biglen)
3608 if (!(little_p = is_index
3609 ? fbm_instr((unsigned char*)big_p + offset,
3610 (unsigned char*)big_p + biglen, little, 0)
3611 : rninstr(big_p, big_p + offset,
3612 little_p, little_p + llen)))
3615 retval = little_p - big_p;
3616 if (retval > 1 && big_utf8)
3617 retval = sv_pos_b2u_flags(big, retval, SV_CONST_RETURN);
3622 /* OPpTRUEBOOL indicates an '== -1' has been optimised away */
3623 if (PL_op->op_private & OPpTRUEBOOL) {
3624 SV *result = ((retval != -1) ^ cBOOL(PL_op->op_private & OPpINDEX_BOOLNEG))
3625 ? &PL_sv_yes : &PL_sv_no;
3626 if (PL_op->op_private & OPpTARGET_MY) {
3627 /* $lex = (index() == -1) */
3628 sv_setsv_mg(TARG, result);
3642 dSP; dMARK; dORIGMARK; dTARGET;
3643 SvTAINTED_off(TARG);
3644 do_sprintf(TARG, SP-MARK, MARK+1);
3645 TAINT_IF(SvTAINTED(TARG));
3657 const U8 *s = (U8*)SvPV_const(argsv, len);
3660 ? (len ? utf8n_to_uvchr(s, len, 0, UTF8_ALLOW_ANYUV) : 0)
3674 if (UNLIKELY(SvAMAGIC(top)))
3676 if (UNLIKELY(isinfnansv(top)))
3677 Perl_croak(aTHX_ "Cannot chr %" NVgf, SvNV(top));
3679 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3680 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3682 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3683 && SvNV_nomg(top) < 0.0)))
3685 if (ckWARN(WARN_UTF8)) {
3686 if (SvGMAGICAL(top)) {
3687 SV *top2 = sv_newmortal();
3688 sv_setsv_nomg(top2, top);
3691 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3692 "Invalid negative number (%" SVf ") in chr", SVfARG(top));
3694 value = UNICODE_REPLACEMENT;
3696 value = SvUV_nomg(top);
3700 SvUPGRADE(TARG,SVt_PV);
3702 if (value > 255 && !IN_BYTES) {
3703 SvGROW(TARG, (STRLEN)UVCHR_SKIP(value)+1);
3704 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3705 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3707 (void)SvPOK_only(TARG);
3716 *tmps++ = (char)value;
3718 (void)SvPOK_only(TARG);
3730 const char *tmps = SvPV_const(left, len);
3732 if (DO_UTF8(left)) {
3733 /* If Unicode, try to downgrade.
3734 * If not possible, croak.
3735 * Yes, we made this up. */
3736 SV* const tsv = newSVpvn_flags(tmps, len, SVf_UTF8|SVs_TEMP);
3738 (void)sv_utf8_downgrade(tsv, FALSE);
3739 tmps = SvPV_const(tsv, len);
3741 # ifdef USE_ITHREADS
3743 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3744 /* This should be threadsafe because in ithreads there is only
3745 * one thread per interpreter. If this would not be true,
3746 * we would need a mutex to protect this malloc. */
3747 PL_reentrant_buffer->_crypt_struct_buffer =
3748 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3749 # if defined(__GLIBC__) || defined(__EMX__)
3750 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3751 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3755 # endif /* HAS_CRYPT_R */
3756 # endif /* USE_ITHREADS */
3758 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3765 "The crypt() function is unimplemented due to excessive paranoia.");
3769 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3770 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3773 /* also used for: pp_lcfirst() */
3777 /* Actually is both lcfirst() and ucfirst(). Only the first character
3778 * changes. This means that possibly we can change in-place, ie., just
3779 * take the source and change that one character and store it back, but not
3780 * if read-only etc, or if the length changes */
3784 STRLEN slen; /* slen is the byte length of the whole SV. */
3787 bool inplace; /* ? Convert first char only, in-place */
3788 bool doing_utf8 = FALSE; /* ? using utf8 */
3789 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3790 const int op_type = PL_op->op_type;
3793 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3794 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3795 * stored as UTF-8 at s. */
3796 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3797 * lowercased) character stored in tmpbuf. May be either
3798 * UTF-8 or not, but in either case is the number of bytes */
3799 bool remove_dot_above = FALSE;
3801 s = (const U8*)SvPV_const(source, slen);
3803 /* We may be able to get away with changing only the first character, in
3804 * place, but not if read-only, etc. Later we may discover more reasons to
3805 * not convert in-place. */
3806 inplace = !SvREADONLY(source) && SvPADTMP(source);
3808 #ifdef USE_LOCALE_CTYPE
3810 if (IN_LC_RUNTIME(LC_CTYPE)) {
3811 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
3816 /* First calculate what the changed first character should be. This affects
3817 * whether we can just swap it out, leaving the rest of the string unchanged,
3818 * or even if have to convert the dest to UTF-8 when the source isn't */
3820 if (! slen) { /* If empty */
3821 need = 1; /* still need a trailing NUL */
3825 else if (DO_UTF8(source)) { /* Is the source utf8? */
3829 if (op_type == OP_UCFIRST) {
3830 #ifdef USE_LOCALE_CTYPE
3831 _toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
3833 _toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, 0);
3838 #ifdef USE_LOCALE_CTYPE
3840 _toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
3842 /* In turkic locales, lower casing an 'I' normally yields U+0131,
3843 * LATIN SMALL LETTER DOTLESS I, but not if the grapheme also
3844 * contains a COMBINING DOT ABOVE. Instead it is treated like
3845 * LATIN CAPITAL LETTER I WITH DOT ABOVE lowercased to 'i'. The
3846 * call to lowercase above has handled this. But SpecialCasing.txt
3847 * says we are supposed to remove the COMBINING DOT ABOVE. We can
3848 * tell if we have this situation if I ==> i in a turkic locale. */
3849 if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
3850 && IN_LC_RUNTIME(LC_CTYPE)
3851 && (UNLIKELY(*s == 'I' && tmpbuf[0] == 'i')))
3853 /* Here, we know there was a COMBINING DOT ABOVE. We won't be
3854 * able to handle this in-place. */
3857 /* It seems likely that the DOT will immediately follow the
3858 * 'I'. If so, we can remove it simply by indicating to the
3859 * code below to start copying the source just beyond the DOT.
3860 * We know its length is 2 */
3861 if (LIKELY(memBEGINs(s + 1, s + slen, COMBINING_DOT_ABOVE_UTF8))) {
3864 else { /* But if it doesn't follow immediately, set a flag for
3866 remove_dot_above = TRUE;
3870 PERL_UNUSED_VAR(remove_dot_above);
3872 _toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, 0);
3877 /* we can't do in-place if the length changes. */
3878 if (ulen != tculen) inplace = FALSE;
3879 need = slen + 1 - ulen + tculen;
3881 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3882 * latin1 is treated as caseless. Note that a locale takes
3884 ulen = 1; /* Original character is 1 byte */
3885 tculen = 1; /* Most characters will require one byte, but this will
3886 * need to be overridden for the tricky ones */
3890 #ifdef USE_LOCALE_CTYPE
3892 if (IN_LC_RUNTIME(LC_CTYPE)) {
3893 if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
3894 && ( (op_type == OP_LCFIRST && UNLIKELY(*s == 'I'))
3895 || (op_type == OP_UCFIRST && UNLIKELY(*s == 'i'))))
3897 if (*s == 'I') { /* lcfirst('I') */
3898 tmpbuf[0] = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
3899 tmpbuf[1] = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
3901 else { /* ucfirst('i') */
3902 tmpbuf[0] = UTF8_TWO_BYTE_HI(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
3903 tmpbuf[1] = UTF8_TWO_BYTE_LO(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
3908 convert_source_to_utf8 = TRUE;
3909 need += variant_under_utf8_count(s, s + slen);
3911 else if (op_type == OP_LCFIRST) {
3913 /* For lc, there are no gotchas for UTF-8 locales (other than
3914 * the turkish ones already handled above) */
3915 *tmpbuf = toLOWER_LC(*s);
3917 else { /* ucfirst */
3919 /* But for uc, some characters require special handling */
3920 if (IN_UTF8_CTYPE_LOCALE) {
3924 /* This would be a bug if any locales have upper and title case
3926 *tmpbuf = (U8) toUPPER_LC(*s);
3931 /* Here, not in locale. If not using Unicode rules, is a simple
3932 * lower/upper, depending */
3933 if (! IN_UNI_8_BIT) {
3934 *tmpbuf = (op_type == OP_LCFIRST)
3938 else if (op_type == OP_LCFIRST) {
3939 /* lower case the first letter: no trickiness for any character */
3940 *tmpbuf = toLOWER_LATIN1(*s);
3943 /* Here, is ucfirst non-UTF-8, not in locale (unless that locale is
3944 * non-turkic UTF-8, which we treat as not in locale), and cased
3947 #ifdef USE_LOCALE_CTYPE
3951 title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3953 assert(tculen == 2);
3955 /* If the result is an upper Latin1-range character, it can
3956 * still be represented in one byte, which is its ordinal */
3957 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3958 *tmpbuf = (U8) title_ord;
3962 /* Otherwise it became more than one ASCII character (in
3963 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3964 * beyond Latin1, so the number of bytes changed, so can't
3965 * replace just the first character in place. */
3968 /* If the result won't fit in a byte, the entire result
3969 * will have to be in UTF-8. Allocate enough space for the
3970 * expanded first byte, and if UTF-8, the rest of the input
3971 * string, some or all of which may also expand to two
3972 * bytes, plus the terminating NUL. */
3973 if (title_ord > 255) {
3975 convert_source_to_utf8 = TRUE;
3977 + variant_under_utf8_count(s, s + slen)
3980 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3981 * characters whose title case is above 255 is
3985 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3986 need = slen + 1 + 1;
3990 } /* End of use Unicode (Latin1) semantics */
3991 } /* End of changing the case of the first character */
3993 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3994 * generate the result */
3997 /* We can convert in place. This means we change just the first
3998 * character without disturbing the rest; no need to grow */
4000 s = d = (U8*)SvPV_force_nomg(source, slen);
4006 /* Here, we can't convert in place; we earlier calculated how much
4007 * space we will need, so grow to accommodate that */
4008 SvUPGRADE(dest, SVt_PV);
4009 d = (U8*)SvGROW(dest, need);
4010 (void)SvPOK_only(dest);
4017 if (! convert_source_to_utf8) {
4019 /* Here both source and dest are in UTF-8, but have to create
4020 * the entire output. We initialize the result to be the
4021 * title/lower cased first character, and then append the rest
4023 sv_setpvn(dest, (char*)tmpbuf, tculen);
4026 /* But this boolean being set means we are in a turkic
4027 * locale, and there is a DOT character that needs to be
4028 * removed, and it isn't immediately after the current
4029 * character. Keep concatenating characters to the output
4030 * one at a time, until we find the DOT, which we simply
4032 if (UNLIKELY(remove_dot_above)) {
4034 Size_t this_len = UTF8SKIP(s + ulen);
4036 sv_catpvn(dest, (char*)(s + ulen), this_len);
4039 if (memBEGINs(s + ulen, s + slen, COMBINING_DOT_ABOVE_UTF8)) {
4043 } while (s + ulen < s + slen);
4046 /* The rest of the string can be concatenated unchanged,
4048 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
4052 const U8 *const send = s + slen;
4054 /* Here the dest needs to be in UTF-8, but the source isn't,
4055 * except we earlier UTF-8'd the first character of the source
4056 * into tmpbuf. First put that into dest, and then append the
4057 * rest of the source, converting it to UTF-8 as we go. */
4059 /* Assert tculen is 2 here because the only characters that
4060 * get to this part of the code have 2-byte UTF-8 equivalents */
4061 assert(tculen == 2);
4063 *d++ = *(tmpbuf + 1);
4064 s++; /* We have just processed the 1st char */
4067 append_utf8_from_native_byte(*s, &d);
4072 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4076 else { /* in-place UTF-8. Just overwrite the first character */
4077 Copy(tmpbuf, d, tculen, U8);
4078 SvCUR_set(dest, need - 1);
4082 else { /* Neither source nor dest are, nor need to be UTF-8 */
4084 if (inplace) { /* in-place, only need to change the 1st char */
4087 else { /* Not in-place */
4089 /* Copy the case-changed character(s) from tmpbuf */
4090 Copy(tmpbuf, d, tculen, U8);
4091 d += tculen - 1; /* Code below expects d to point to final
4092 * character stored */
4095 else { /* empty source */
4096 /* See bug #39028: Don't taint if empty */
4100 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
4101 * the destination to retain that flag */
4102 if (DO_UTF8(source))
4105 if (!inplace) { /* Finish the rest of the string, unchanged */
4106 /* This will copy the trailing NUL */
4107 Copy(s + 1, d + 1, slen, U8);
4108 SvCUR_set(dest, need - 1);
4111 #ifdef USE_LOCALE_CTYPE
4112 if (IN_LC_RUNTIME(LC_CTYPE)) {
4117 if (dest != source && SvTAINTED(source))
4135 if ( SvPADTMP(source)
4136 && !SvREADONLY(source) && SvPOK(source)
4139 #ifdef USE_LOCALE_CTYPE
4140 (IN_LC_RUNTIME(LC_CTYPE))
4141 ? ! IN_UTF8_CTYPE_LOCALE
4147 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4148 * make the loop tight, so we overwrite the source with the dest before
4149 * looking at it, and we need to look at the original source
4150 * afterwards. There would also need to be code added to handle
4151 * switching to not in-place in midstream if we run into characters
4152 * that change the length. Since being in locale overrides UNI_8_BIT,
4153 * that latter becomes irrelevant in the above test; instead for
4154 * locale, the size can't normally change, except if the locale is a
4157 s = d = (U8*)SvPV_force_nomg(source, len);
4164 s = (const U8*)SvPV_nomg_const(source, len);
4167 SvUPGRADE(dest, SVt_PV);
4168 d = (U8*)SvGROW(dest, min);
4169 (void)SvPOK_only(dest);
4174 #ifdef USE_LOCALE_CTYPE
4176 if (IN_LC_RUNTIME(LC_CTYPE)) {
4177 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
4182 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4183 to check DO_UTF8 again here. */
4185 if (DO_UTF8(source)) {
4186 const U8 *const send = s + len;
4187 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4189 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
4190 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4191 /* All occurrences of these are to be moved to follow any other marks.
4192 * This is context-dependent. We may not be passed enough context to
4193 * move the iota subscript beyond all of them, but we do the best we can
4194 * with what we're given. The result is always better than if we
4195 * hadn't done this. And, the problem would only arise if we are
4196 * passed a character without all its combining marks, which would be
4197 * the caller's mistake. The information this is based on comes from a
4198 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4199 * itself) and so can't be checked properly to see if it ever gets
4200 * revised. But the likelihood of it changing is remote */
4201 bool in_iota_subscript = FALSE;
4207 if (UNLIKELY(in_iota_subscript)) {
4208 UV cp = utf8_to_uvchr_buf(s, send, NULL);
4210 if (! _invlist_contains_cp(PL_utf8_mark, cp)) {
4212 /* A non-mark. Time to output the iota subscript */
4213 *d++ = UTF8_TWO_BYTE_HI(GREEK_CAPITAL_LETTER_IOTA);
4214 *d++ = UTF8_TWO_BYTE_LO(GREEK_CAPITAL_LETTER_IOTA);
4215 in_iota_subscript = FALSE;
4219 /* Then handle the current character. Get the changed case value
4220 * and copy it to the output buffer */
4223 #ifdef USE_LOCALE_CTYPE
4224 uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
4226 uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, 0);
4228 if (uv == GREEK_CAPITAL_LETTER_IOTA
4229 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4231 in_iota_subscript = TRUE;
4234 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4235 /* If the eventually required minimum size outgrows the
4236 * available space, we need to grow. */
4237 const UV o = d - (U8*)SvPVX_const(dest);
4239 /* If someone uppercases one million U+03B0s we SvGROW()
4240 * one million times. Or we could try guessing how much to
4241 * allocate without allocating too much. But we can't
4242 * really guess without examining the rest of the string.
4243 * Such is life. See corresponding comment in lc code for
4245 d = o + (U8*) SvGROW(dest, min);
4247 Copy(tmpbuf, d, ulen, U8);
4252 if (in_iota_subscript) {
4253 *d++ = UTF8_TWO_BYTE_HI(GREEK_CAPITAL_LETTER_IOTA);
4254 *d++ = UTF8_TWO_BYTE_LO(GREEK_CAPITAL_LETTER_IOTA);
4259 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4261 else { /* Not UTF-8 */
4263 const U8 *const send = s + len;
4265 /* Use locale casing if in locale; regular style if not treating
4266 * latin1 as having case; otherwise the latin1 casing. Do the
4267 * whole thing in a tight loop, for speed, */
4268 #ifdef USE_LOCALE_CTYPE
4269 if (IN_LC_RUNTIME(LC_CTYPE)) {
4270 if (IN_UTF8_CTYPE_LOCALE) {
4273 for (; s < send; d++, s++)
4274 *d = (U8) toUPPER_LC(*s);
4278 if (! IN_UNI_8_BIT) {
4279 for (; s < send; d++, s++) {
4284 #ifdef USE_LOCALE_CTYPE
4287 for (; s < send; d++, s++) {
4290 *d = toUPPER_LATIN1_MOD(*s);
4291 if ( LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)
4293 #ifdef USE_LOCALE_CTYPE
4295 && (LIKELY( ! IN_UTF8_TURKIC_LOCALE
4296 || ! IN_LC_RUNTIME(LC_CTYPE))
4304 /* The mainstream case is the tight loop above. To avoid
4305 * extra tests in that, all three characters that always
4306 * require special handling are mapped by the MOD to the
4307 * one tested just above. Use the source to distinguish
4308 * between those cases */
4310 #if UNICODE_MAJOR_VERSION > 2 \
4311 || (UNICODE_MAJOR_VERSION == 2 && UNICODE_DOT_VERSION >= 1 \
4312 && UNICODE_DOT_DOT_VERSION >= 8)
4313 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4315 /* uc() of this requires 2 characters, but they are
4316 * ASCII. If not enough room, grow the string */
4317 if (SvLEN(dest) < ++min) {
4318 const UV o = d - (U8*)SvPVX_const(dest);
4319 d = o + (U8*) SvGROW(dest, min);
4321 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4322 continue; /* Back to the tight loop; still in ASCII */
4326 /* The other special handling characters have their
4327 * upper cases outside the latin1 range, hence need to be
4328 * in UTF-8, so the whole result needs to be in UTF-8.
4330 * So, here we are somewhere in the middle of processing a
4331 * non-UTF-8 string, and realize that we will have to
4332 * convert the whole thing to UTF-8. What to do? There
4333 * are several possibilities. The simplest to code is to
4334 * convert what we have so far, set a flag, and continue on
4335 * in the loop. The flag would be tested each time through
4336 * the loop, and if set, the next character would be
4337 * converted to UTF-8 and stored. But, I (khw) didn't want
4338 * to slow down the mainstream case at all for this fairly
4339 * rare case, so I didn't want to add a test that didn't
4340 * absolutely have to be there in the loop, besides the
4341 * possibility that it would get too complicated for
4342 * optimizers to deal with. Another possibility is to just
4343 * give up, convert the source to UTF-8, and restart the
4344 * function that way. Another possibility is to convert
4345 * both what has already been processed and what is yet to
4346 * come separately to UTF-8, then jump into the loop that
4347 * handles UTF-8. But the most efficient time-wise of the
4348 * ones I could think of is what follows, and turned out to
4349 * not require much extra code.
4351 * First, calculate the extra space needed for the
4352 * remainder of the source needing to be in UTF-8. Except
4353 * for the 'i' in Turkic locales, in UTF-8 strings, the
4354 * uppercase of a character below 256 occupies the same
4355 * number of bytes as the original. Therefore, the space
4356 * needed is the that number plus the number of characters
4357 * that become two bytes when converted to UTF-8, plus, in
4358 * turkish locales, the number of 'i's. */
4360 extra = send - s + variant_under_utf8_count(s, send);
4362 #ifdef USE_LOCALE_CTYPE
4364 if (UNLIKELY(*s == 'i')) { /* We wouldn't get an 'i' here
4365 unless are in a Turkic
4367 const U8 * s_peek = s;
4372 s_peek = (U8 *) memchr(s_peek + 1, 'i',
4373 send - (s_peek + 1));
4374 } while (s_peek != NULL);
4378 /* Convert what we have so far into UTF-8, telling the
4379 * function that we know it should be converted, and to
4380 * allow extra space for what we haven't processed yet.
4382 * This may cause the string pointer to move, so need to
4383 * save and re-find it. */
4385 len = d - (U8*)SvPVX_const(dest);
4386 SvCUR_set(dest, len);
4387 len = sv_utf8_upgrade_flags_grow(dest,
4388 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4390 + 1 /* trailing NUL */ );
4391 d = (U8*)SvPVX(dest) + len;
4393 /* Now process the remainder of the source, simultaneously
4394 * converting to upper and UTF-8.
4396 * To avoid extra tests in the loop body, and since the
4397 * loop is so simple, split out the rare Turkic case into
4400 #ifdef USE_LOCALE_CTYPE
4401 if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
4402 && UNLIKELY(IN_LC_RUNTIME(LC_CTYPE)))
4404 for (; s < send; s++) {
4406 *d++ = UTF8_TWO_BYTE_HI(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
4407 *d++ = UTF8_TWO_BYTE_LO(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
4410 (void) _to_upper_title_latin1(*s, d, &len, 'S');
4417 for (; s < send; s++) {
4418 (void) _to_upper_title_latin1(*s, d, &len, 'S');
4422 /* Here have processed the whole source; no need to
4423 * continue with the outer loop. Each character has been
4424 * converted to upper case and converted to UTF-8. */
4426 } /* End of processing all latin1-style chars */
4427 } /* End of processing all chars */
4428 } /* End of source is not empty */
4430 if (source != dest) {
4431 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4432 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4434 } /* End of isn't utf8 */
4435 #ifdef USE_LOCALE_CTYPE
4436 if (IN_LC_RUNTIME(LC_CTYPE)) {
4441 if (dest != source && SvTAINTED(source))
4456 bool has_turkic_I = FALSE;
4460 if ( SvPADTMP(source)
4461 && !SvREADONLY(source) && SvPOK(source)
4464 #ifdef USE_LOCALE_CTYPE
4466 && ( LIKELY(! IN_LC_RUNTIME(LC_CTYPE))
4467 || LIKELY(! IN_UTF8_TURKIC_LOCALE))
4473 /* We can convert in place, as, outside of Turkic UTF-8 locales,
4474 * lowercasing anything in the latin1 range (or else DO_UTF8 would have
4475 * been on) doesn't lengthen it. */
4477 s = d = (U8*)SvPV_force_nomg(source, len);
4484 s = (const U8*)SvPV_nomg_const(source, len);
4487 SvUPGRADE(dest, SVt_PV);
4488 d = (U8*)SvGROW(dest, min);
4489 (void)SvPOK_only(dest);
4494 #ifdef USE_LOCALE_CTYPE
4496 if (IN_LC_RUNTIME(LC_CTYPE)) {
4499 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
4501 /* Lowercasing in a Turkic locale can cause non-UTF-8 to need to become
4502 * UTF-8 for the single case of the character 'I' */
4503 if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
4504 && ! DO_UTF8(source)
4505 && (next_I = (U8 *) memchr(s, 'I', len)))
4508 const U8 *const send = s + len;
4513 next_I = (U8 *) memchr(next_I + 1, 'I',
4514 send - (next_I + 1));
4515 } while (next_I != NULL);
4517 /* Except for the 'I', in UTF-8 strings, the lower case of a
4518 * character below 256 occupies the same number of bytes as the
4519 * original. Therefore, the space needed is the original length
4520 * plus I_count plus the number of characters that become two bytes
4521 * when converted to UTF-8 */
4522 sv_utf8_upgrade_flags_grow(dest, 0, len
4524 + variant_under_utf8_count(s, send)
4525 + 1 /* Trailing NUL */ );
4526 d = (U8*)SvPVX(dest);
4527 has_turkic_I = TRUE;
4532 PERL_UNUSED_VAR(has_turkic_I);
4535 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4536 to check DO_UTF8 again here. */
4538 if (DO_UTF8(source)) {
4539 const U8 *const send = s + len;
4540 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4541 bool remove_dot_above = FALSE;
4544 const STRLEN u = UTF8SKIP(s);
4547 #ifdef USE_LOCALE_CTYPE
4549 _toLOWER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
4551 /* If we are in a Turkic locale, we have to do more work. As noted
4552 * in the comments for lcfirst, there is a special case if a 'I'
4553 * is in a grapheme with COMBINING DOT ABOVE UTF8. It turns into a
4554 * 'i', and the DOT must be removed. We check for that situation,
4555 * and set a flag if the DOT is there. Then each time through the
4556 * loop, we have to see if we need to remove the next DOT above,
4557 * and if so, do it. We know that there is a DOT because
4558 * _toLOWER_utf8_flags() wouldn't have returned 'i' unless there
4559 * was one in a proper position. */
4560 if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
4561 && IN_LC_RUNTIME(LC_CTYPE))
4563 if ( UNLIKELY(remove_dot_above)
4564 && memBEGINs(tmpbuf, sizeof(tmpbuf), COMBINING_DOT_ABOVE_UTF8))
4567 remove_dot_above = FALSE;
4570 else if (UNLIKELY(*s == 'I' && tmpbuf[0] == 'i')) {
4571 remove_dot_above = TRUE;
4575 PERL_UNUSED_VAR(remove_dot_above);
4577 _toLOWER_utf8_flags(s, send, tmpbuf, &ulen, 0);
4580 /* Here is where we would do context-sensitive actions for the
4581 * Greek final sigma. See the commit message for 86510fb15 for why
4582 * there isn't any */
4584 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4586 /* If the eventually required minimum size outgrows the
4587 * available space, we need to grow. */
4588 const UV o = d - (U8*)SvPVX_const(dest);
4590 /* If someone lowercases one million U+0130s we SvGROW() one
4591 * million times. Or we could try guessing how much to
4592 * allocate without allocating too much. Such is life.
4593 * Another option would be to grow an extra byte or two more
4594 * each time we need to grow, which would cut down the million
4595 * to 500K, with little waste */
4596 d = o + (U8*) SvGROW(dest, min);
4599 /* Copy the newly lowercased letter to the output buffer we're
4601 Copy(tmpbuf, d, ulen, U8);
4604 } /* End of looping through the source string */
4607 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4608 } else { /* 'source' not utf8 */
4610 const U8 *const send = s + len;
4612 /* Use locale casing if in locale; regular style if not treating
4613 * latin1 as having case; otherwise the latin1 casing. Do the
4614 * whole thing in a tight loop, for speed, */
4615 #ifdef USE_LOCALE_CTYPE
4616 if (IN_LC_RUNTIME(LC_CTYPE)) {
4617 if (LIKELY( ! has_turkic_I)) {
4618 for (; s < send; d++, s++)
4619 *d = toLOWER_LC(*s);
4621 else { /* This is the only case where lc() converts 'dest'
4622 into UTF-8 from a non-UTF-8 'source' */
4623 for (; s < send; s++) {
4625 *d++ = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
4626 *d++ = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
4629 append_utf8_from_native_byte(toLOWER_LATIN1(*s), &d);
4636 if (! IN_UNI_8_BIT) {
4637 for (; s < send; d++, s++) {
4642 for (; s < send; d++, s++) {
4643 *d = toLOWER_LATIN1(*s);
4647 if (source != dest) {
4649 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4652 #ifdef USE_LOCALE_CTYPE
4653 if (IN_LC_RUNTIME(LC_CTYPE)) {
4658 if (dest != source && SvTAINTED(source))
4667 SV * const sv = TOPs;
4669 const char *s = SvPV_const(sv,len);
4671 SvUTF8_off(TARG); /* decontaminate */
4674 SvUPGRADE(TARG, SVt_PV);
4675 SvGROW(TARG, (len * 2) + 1);
4679 STRLEN ulen = UTF8SKIP(s);
4680 bool to_quote = FALSE;
4682 if (UTF8_IS_INVARIANT(*s)) {
4683 if (_isQUOTEMETA(*s)) {
4687 else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, s + len)) {
4689 #ifdef USE_LOCALE_CTYPE
4690 /* In locale, we quote all non-ASCII Latin1 chars.
4691 * Otherwise use the quoting rules */
4693 IN_LC_RUNTIME(LC_CTYPE)
4696 _isQUOTEMETA(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s + 1))))
4701 else if (is_QUOTEMETA_high(s)) {
4716 else if (IN_UNI_8_BIT) {
4718 if (_isQUOTEMETA(*s))
4724 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4725 * including everything above ASCII */
4727 if (!isWORDCHAR_A(*s))
4733 SvCUR_set(TARG, d - SvPVX_const(TARG));
4734 (void)SvPOK_only_UTF8(TARG);
4737 sv_setpvn(TARG, s, len);
4753 U8 tmpbuf[UTF8_MAXBYTES_CASE + 1];
4754 #if UNICODE_MAJOR_VERSION > 3 /* no multifolds in early Unicode */ \
4755 || (UNICODE_MAJOR_VERSION == 3 && ( UNICODE_DOT_VERSION > 0) \
4756 || UNICODE_DOT_DOT_VERSION > 0)
4757 const bool full_folding = TRUE; /* This variable is here so we can easily
4758 move to more generality later */
4760 const bool full_folding = FALSE;
4762 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4763 #ifdef USE_LOCALE_CTYPE
4764 | ( IN_LC_RUNTIME(LC_CTYPE) ? FOLD_FLAGS_LOCALE : 0 )
4768 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4769 * You are welcome(?) -Hugmeir
4777 s = (const U8*)SvPV_nomg_const(source, len);
4779 if (ckWARN(WARN_UNINITIALIZED))
4780 report_uninit(source);
4787 SvUPGRADE(dest, SVt_PV);
4788 d = (U8*)SvGROW(dest, min);
4789 (void)SvPOK_only(dest);
4795 #ifdef USE_LOCALE_CTYPE
4797 if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
4798 CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
4803 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4805 const STRLEN u = UTF8SKIP(s);
4808 _toFOLD_utf8_flags(s, send, tmpbuf, &ulen, flags);
4810 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4811 const UV o = d - (U8*)SvPVX_const(dest);
4812 d = o + (U8*) SvGROW(dest, min);
4815 Copy(tmpbuf, d, ulen, U8);
4820 } /* Unflagged string */
4822 #ifdef USE_LOCALE_CTYPE
4823 if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
4824 if (IN_UTF8_CTYPE_LOCALE) {
4825 goto do_uni_folding;
4827 for (; s < send; d++, s++)
4828 *d = (U8) toFOLD_LC(*s);
4832 if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4833 for (; s < send; d++, s++)
4837 #ifdef USE_LOCALE_CTYPE
4840 /* For ASCII and the Latin-1 range, there's potentially three
4841 * troublesome folds:
4842 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
4843 * casefolding becomes 'ss';
4844 * \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4845 * \x{3BC} (\N{GREEK SMALL LETTER MU})
4846 * I only in Turkic locales, this folds to \x{131}
4847 * \N{LATIN SMALL LETTER DOTLESS I}
4848 * For the rest, the casefold is their lowercase. */
4849 for (; s < send; d++, s++) {
4850 if ( UNLIKELY(*s == MICRO_SIGN)
4851 #ifdef USE_LOCALE_CTYPE
4852 || ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
4853 && UNLIKELY(IN_LC_RUNTIME(LC_CTYPE))
4854 && UNLIKELY(*s == 'I'))
4857 Size_t extra = send - s
4858 + variant_under_utf8_count(s, send);
4860 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
4861 * and 'I' in Turkic locales is \N{LATIN SMALL LETTER
4862 * DOTLESS I} both of which are outside of the latin-1
4863 * range. There's a couple of ways to deal with this -- khw
4864 * discusses them in pp_lc/uc, so go there :) What we do
4865 * here is upgrade what we had already casefolded, then
4866 * enter an inner loop that appends the rest of the
4867 * characters as UTF-8.
4869 * First we calculate the needed size of the upgraded dest
4870 * beyond what's been processed already (the upgrade
4871 * function figures that out). Except for the 'I' in
4872 * Turkic locales, in UTF-8 strings, the fold case of a
4873 * character below 256 occupies the same number of bytes as
4874 * the original (even the Sharp S). Therefore, the space
4875 * needed is the number of bytes remaining plus the number
4876 * of characters that become two bytes when converted to
4877 * UTF-8 plus, in turkish locales, the number of 'I's */
4879 if (UNLIKELY(*s == 'I')) {
4880 const U8 * s_peek = s;
4885 s_peek = (U8 *) memchr(s_peek + 1, 'I',
4886 send - (s_peek + 1));
4887 } while (s_peek != NULL);
4890 /* Growing may move things, so have to save and recalculate
4892 len = d - (U8*)SvPVX_const(dest);
4893 SvCUR_set(dest, len);
4894 len = sv_utf8_upgrade_flags_grow(dest,
4895 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4897 + 1 /* Trailing NUL */ );
4898 d = (U8*)SvPVX(dest) + len;
4901 *d++ = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
4902 *d++ = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
4905 *d++ = UTF8_TWO_BYTE_HI(GREEK_SMALL_LETTER_MU);
4906 *d++ = UTF8_TWO_BYTE_LO(GREEK_SMALL_LETTER_MU);
4910 for (; s < send; s++) {
4912 _to_uni_fold_flags(*s, d, &ulen, flags);
4917 else if ( UNLIKELY(*s == LATIN_SMALL_LETTER_SHARP_S)
4920 /* Under full casefolding, LATIN SMALL LETTER SHARP S
4921 * becomes "ss", which may require growing the SV. */
4922 if (SvLEN(dest) < ++min) {
4923 const UV o = d - (U8*)SvPVX_const(dest);
4924 d = o + (U8*) SvGROW(dest, min);
4929 else { /* Else, the fold is the lower case */
4930 *d = toLOWER_LATIN1(*s);
4936 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4938 #ifdef USE_LOCALE_CTYPE
4939 if (IN_LC_RUNTIME(LC_CTYPE)) {
4944 if (SvTAINTED(source))
4954 dSP; dMARK; dORIGMARK;
4955 AV *const av = MUTABLE_AV(POPs);
4956 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4958 if (SvTYPE(av) == SVt_PVAV) {
4959 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4960 bool can_preserve = FALSE;
4966 can_preserve = SvCANEXISTDELETE(av);
4969 if (lval && localizing) {
4972 for (svp = MARK + 1; svp <= SP; svp++) {
4973 const SSize_t elem = SvIV(*svp);
4977 if (max > AvMAX(av))
4981 while (++MARK <= SP) {
4983 SSize_t elem = SvIV(*MARK);
4984 bool preeminent = TRUE;
4986 if (localizing && can_preserve) {
4987 /* If we can determine whether the element exist,
4988 * Try to preserve the existenceness of a tied array
4989 * element by using EXISTS and DELETE if possible.
4990 * Fallback to FETCH and STORE otherwise. */
4991 preeminent = av_exists(av, elem);
4994 svp = av_fetch(av, elem, lval);
4997 DIE(aTHX_ PL_no_aelem, elem);
5000 save_aelem(av, elem, svp);
5002 SAVEADELETE(av, elem);
5005 *MARK = svp ? *svp : &PL_sv_undef;
5008 if (GIMME_V != G_LIST) {
5010 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5019 AV *const av = MUTABLE_AV(POPs);
5020 I32 lval = (PL_op->op_flags & OPf_MOD);
5021 SSize_t items = SP - MARK;
5023 if (PL_op->op_private & OPpMAYBE_LVSUB) {
5024 const I32 flags = is_lvalue_sub();
5026 if (!(flags & OPpENTERSUB_INARGS))
5027 /* diag_listed_as: Can't modify %s in %s */
5028 Perl_croak(aTHX_ "Can't modify index/value array slice in list assignment");
5035 *(MARK+items*2-1) = *(MARK+items);
5041 while (++MARK <= SP) {
5044 svp = av_fetch(av, SvIV(*MARK), lval);
5046 if (!svp || !*svp || *svp == &PL_sv_undef) {
5047 DIE(aTHX_ PL_no_aelem, SvIV(*MARK));
5049 *MARK = sv_mortalcopy(*MARK);
5051 *++MARK = svp ? *svp : &PL_sv_undef;
5053 if (GIMME_V != G_LIST) {
5054 MARK = SP - items*2;
5055 *++MARK = items > 0 ? *SP : &PL_sv_undef;
5065 AV *array = MUTABLE_AV(POPs);
5066 const U8 gimme = GIMME_V;
5067 IV *iterp = Perl_av_iter_p(aTHX_ array);
5068 const IV current = (*iterp)++;
5070 if (current > av_top_index(array)) {
5072 if (gimme == G_SCALAR)
5080 if (gimme == G_LIST) {
5081 SV **const element = av_fetch(array, current, 0);
5082 PUSHs(element ? *element : &PL_sv_undef);
5087 /* also used for: pp_avalues()*/
5091 AV *array = MUTABLE_AV(POPs);
5092 const U8 gimme = GIMME_V;
5094 *Perl_av_iter_p(aTHX_ array) = 0;
5096 if (gimme == G_SCALAR) {
5098 PUSHi(av_count(array));
5100 else if (gimme == G_LIST) {
5101 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
5102 const I32 flags = is_lvalue_sub();
5103 if (flags && !(flags & OPpENTERSUB_INARGS))
5104 /* diag_listed_as: Can't modify %s in %s */
5106 "Can't modify keys on array in list assignment");
5109 IV n = av_top_index(array);
5114 if ( PL_op->op_type == OP_AKEYS
5115 || ( PL_op->op_type == OP_AVHVSWITCH
5116 && (PL_op->op_private & 3) + OP_AEACH == OP_AKEYS ))
5118 for (i = 0; i <= n; i++) {
5123 for (i = 0; i <= n; i++) {
5124 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
5125 PUSHs(elem ? *elem : &PL_sv_undef);
5133 /* Associative arrays. */
5138 HV * hash = MUTABLE_HV(POPs);
5140 const U8 gimme = GIMME_V;
5142 entry = hv_iternext(hash);
5146 SV* const sv = hv_iterkeysv(entry);
5148 if (gimme == G_LIST) {
5150 val = hv_iterval(hash, entry);
5154 else if (gimme == G_SCALAR)
5161 S_do_delete_local(pTHX)
5164 const U8 gimme = GIMME_V;
5167 const bool sliced = cBOOL(PL_op->op_private & OPpSLICE);
5168 SV **unsliced_keysv = sliced ? NULL : sp--;
5169 SV * const osv = POPs;
5170 SV **mark = sliced ? PL_stack_base + POPMARK : unsliced_keysv-1;
5172 const bool tied = SvRMAGICAL(osv)
5173 && mg_find((const SV *)osv, PERL_MAGIC_tied);
5174 const bool can_preserve = SvCANEXISTDELETE(osv);
5175 const U32 type = SvTYPE(osv);
5176 SV ** const end = sliced ? SP : unsliced_keysv;
5178 if (type == SVt_PVHV) { /* hash element */
5179 HV * const hv = MUTABLE_HV(osv);
5180 while (++MARK <= end) {
5181 SV * const keysv = *MARK;
5183 bool preeminent = TRUE;
5185 preeminent = hv_exists_ent(hv, keysv, 0);
5187 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
5194 sv = hv_delete_ent(hv, keysv, 0, 0);
5196 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5199 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5200 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
5202 *MARK = sv_mortalcopy(sv);
5208 SAVEHDELETE(hv, keysv);
5209 *MARK = &PL_sv_undef;
5213 else if (type == SVt_PVAV) { /* array element */
5214 if (PL_op->op_flags & OPf_SPECIAL) {
5215 AV * const av = MUTABLE_AV(osv);
5216 while (++MARK <= end) {
5217 SSize_t idx = SvIV(*MARK);
5219 bool preeminent = TRUE;
5221 preeminent = av_exists(av, idx);
5223 SV **svp = av_fetch(av, idx, 1);
5230 sv = av_delete(av, idx, 0);
5232 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5235 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
5237 *MARK = sv_mortalcopy(sv);
5243 SAVEADELETE(av, idx);
5244 *MARK = &PL_sv_undef;
5249 DIE(aTHX_ "panic: avhv_delete no longer supported");
5252 DIE(aTHX_ "Not a HASH reference");
5254 if (gimme == G_VOID)
5256 else if (gimme == G_SCALAR) {
5261 *++MARK = &PL_sv_undef;
5265 else if (gimme != G_VOID)
5266 PUSHs(*unsliced_keysv);
5277 if (PL_op->op_private & OPpLVAL_INTRO)
5278 return do_delete_local();
5281 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5283 if (PL_op->op_private & (OPpSLICE|OPpKVSLICE)) {
5285 HV * const hv = MUTABLE_HV(POPs);
5286 const U32 hvtype = SvTYPE(hv);
5288 if (PL_op->op_private & OPpKVSLICE) {
5289 SSize_t items = SP - MARK;
5293 *(MARK+items*2-1) = *(MARK+items);
5300 if (hvtype == SVt_PVHV) { /* hash element */
5301 while ((MARK += (1+skip)) <= SP) {
5302 SV * const sv = hv_delete_ent(hv, *(MARK-skip), discard, 0);
5303 *MARK = sv ? sv : &PL_sv_undef;
5306 else if (hvtype == SVt_PVAV) { /* array element */
5307 if (PL_op->op_flags & OPf_SPECIAL) {
5308 while ((MARK += (1+skip)) <= SP) {
5309 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*(MARK-skip)), discard);
5310 *MARK = sv ? sv : &PL_sv_undef;
5315 DIE(aTHX_ "Not a HASH reference");
5318 else if (gimme == G_SCALAR) {
5323 *++MARK = &PL_sv_undef;
5329 HV * const hv = MUTABLE_HV(POPs);
5331 if (SvTYPE(hv) == SVt_PVHV)
5332 sv = hv_delete_ent(hv, keysv, discard, 0);
5333 else if (SvTYPE(hv) == SVt_PVAV) {
5334 if (PL_op->op_flags & OPf_SPECIAL)
5335 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
5337 DIE(aTHX_ "panic: avhv_delete no longer supported");
5340 DIE(aTHX_ "Not a HASH reference");
5355 if (UNLIKELY( PL_op->op_private & OPpEXISTS_SUB )) {
5357 SV * const sv = POPs;
5358 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
5361 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
5366 hv = MUTABLE_HV(POPs);
5367 if (LIKELY( SvTYPE(hv) == SVt_PVHV )) {
5368 if (hv_exists_ent(hv, tmpsv, 0))
5371 else if (SvTYPE(hv) == SVt_PVAV) {
5372 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
5373 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
5378 DIE(aTHX_ "Not a HASH reference");
5385 dSP; dMARK; dORIGMARK;
5386 HV * const hv = MUTABLE_HV(POPs);
5387 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
5388 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
5389 bool can_preserve = FALSE;
5395 if (SvCANEXISTDELETE(hv))
5396 can_preserve = TRUE;
5399 while (++MARK <= SP) {
5400 SV * const keysv = *MARK;
5403 bool preeminent = TRUE;
5405 if (localizing && can_preserve) {
5406 /* If we can determine whether the element exist,
5407 * try to preserve the existenceness of a tied hash
5408 * element by using EXISTS and DELETE if possible.
5409 * Fallback to FETCH and STORE otherwise. */
5410 preeminent = hv_exists_ent(hv, keysv, 0);
5413 he = hv_fetch_ent(hv, keysv, lval, 0);
5414 svp = he ? &HeVAL(he) : NULL;
5417 if (!svp || !*svp || *svp == &PL_sv_undef) {
5418 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5421 if (HvNAME_get(hv) && isGV_or_RVCV(*svp))
5422 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
5423 else if (preeminent)
5424 save_helem_flags(hv, keysv, svp,
5425 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5427 SAVEHDELETE(hv, keysv);
5430 *MARK = svp && *svp ? *svp : &PL_sv_undef;
5432 if (GIMME_V != G_LIST) {
5434 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5443 HV * const hv = MUTABLE_HV(POPs);
5444 I32 lval = (PL_op->op_flags & OPf_MOD);
5445 SSize_t items = SP - MARK;
5447 if (PL_op->op_private & OPpMAYBE_LVSUB) {
5448 const I32 flags = is_lvalue_sub();
5450 if (!(flags & OPpENTERSUB_INARGS))
5451 /* diag_listed_as: Can't modify %s in %s */
5452 Perl_croak(aTHX_ "Can't modify key/value hash slice in %s assignment",
5453 GIMME_V == G_LIST ? "list" : "scalar");
5460 *(MARK+items*2-1) = *(MARK+items);
5466 while (++MARK <= SP) {
5467 SV * const keysv = *MARK;
5471 he = hv_fetch_ent(hv, keysv, lval, 0);
5472 svp = he ? &HeVAL(he) : NULL;
5475 if (!svp || !*svp || *svp == &PL_sv_undef) {
5476 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5478 *MARK = sv_mortalcopy(*MARK);
5480 *++MARK = svp && *svp ? *svp : &PL_sv_undef;
5482 if (GIMME_V != G_LIST) {
5483 MARK = SP - items*2;
5484 *++MARK = items > 0 ? *SP : &PL_sv_undef;
5490 /* List operators. */
5494 I32 markidx = POPMARK;
5495 if (GIMME_V != G_LIST) {
5496 /* don't initialize mark here, EXTEND() may move the stack */
5499 EXTEND(SP, 1); /* in case no arguments, as in @empty */
5500 mark = PL_stack_base + markidx;
5502 *MARK = *SP; /* unwanted list, return last item */
5504 *MARK = &PL_sv_undef;
5514 SV ** const lastrelem = PL_stack_sp;
5515 SV ** const lastlelem = PL_stack_base + POPMARK;
5516 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5517 SV ** const firstrelem = lastlelem + 1;
5518 const U8 mod = PL_op->op_flags & OPf_MOD;
5520 const I32 max = lastrelem - lastlelem;
5523 if (GIMME_V != G_LIST) {
5524 if (lastlelem < firstlelem) {
5526 *firstlelem = &PL_sv_undef;
5529 I32 ix = SvIV(*lastlelem);
5532 if (ix < 0 || ix >= max)
5533 *firstlelem = &PL_sv_undef;
5535 *firstlelem = firstrelem[ix];
5542 SP = firstlelem - 1;
5546 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
5547 I32 ix = SvIV(*lelem);
5550 if (ix < 0 || ix >= max)
5551 *lelem = &PL_sv_undef;
5553 if (!(*lelem = firstrelem[ix]))
5554 *lelem = &PL_sv_undef;
5555 else if (mod && SvPADTMP(*lelem)) {
5556 *lelem = firstrelem[ix] = sv_mortalcopy(*lelem);
5567 const I32 items = SP - MARK;
5568 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5570 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5571 ? newRV_noinc(av) : av);
5575 /* When an anonlist or anonhash will (1) be empty and (2) return an RV
5576 * pointing to the new AV/HV, the peephole optimizer can swap in this
5577 * simpler function and op_null the originally associated PUSHMARK. */
5581 OP * const op = PL_op;
5583 SV * const sv = MUTABLE_SV( newSV_type(
5584 (op->op_private & OPpEMPTYAVHV_IS_HV) ?
5588 /* Is it an assignment, just a stack push, or both?*/
5589 if (op->op_private & OPpTARGET_MY) {
5590 SV** const padentry = &PAD_SVl(op->op_targ);
5592 /* Since the op_targ is very likely to be an undef SVt_IV from
5593 * a previous iteration, converting it to a live RV can
5594 * typically be special-cased.*/
5595 if (SvTYPE(rv) == SVt_IV && !SvOK(rv)) {
5596 SvFLAGS(rv) = (SVt_IV | SVf_ROK);
5599 sv_setrv_noinc_mg(rv, sv);
5601 if ((op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) == OPpLVAL_INTRO) {
5602 save_clearsv(padentry);
5604 if (GIMME_V == G_VOID) {
5605 RETURN; /* skip extending and pushing */
5608 /* Inlined newRV_noinc */
5609 SV * refsv = newSV_type_mortal(SVt_IV);
5610 SvRV_set(refsv, sv);
5622 dSP; dMARK; dORIGMARK;
5623 HV* const hv = newHV();
5624 SV* const retval = sv_2mortal( PL_op->op_flags & OPf_SPECIAL
5625 ? newRV_noinc(MUTABLE_SV(hv))
5627 /* This isn't quite true for an odd sized list (it's one too few) but it's
5628 not worth the runtime +1 just to optimise for the warning case. */
5629 SSize_t pairs = (SP - MARK) >> 1;
5630 if (pairs > PERL_HASH_DEFAULT_HvMAX) {
5631 hv_ksplit(hv, pairs);
5636 (MARK++, SvGMAGICAL(*MARK) ? sv_mortalcopy(*MARK) : *MARK);
5642 val = newSV_type(SVt_NULL);
5643 sv_setsv_nomg(val, *MARK);
5647 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5648 val = newSV_type(SVt_NULL);
5650 (void)hv_store_ent(hv,key,val,0);
5659 dSP; dMARK; dORIGMARK;
5660 int num_args = (SP - MARK);
5661 AV *ary = MUTABLE_AV(*++MARK);
5670 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5673 return Perl_tied_method(aTHX_ SV_CONST(SPLICE), mark - 1, MUTABLE_SV(ary), mg,
5674 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
5678 if (SvREADONLY(ary))
5679 Perl_croak_no_modify();
5684 offset = i = SvIV(*MARK);
5686 offset += AvFILLp(ary) + 1;
5688 DIE(aTHX_ PL_no_aelem, i);
5690 length = SvIVx(*MARK++);
5692 length += AvFILLp(ary) - offset + 1;
5698 length = AvMAX(ary) + 1; /* close enough to infinity */
5702 length = AvMAX(ary) + 1;
5704 if (offset > AvFILLp(ary) + 1) {
5706 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5707 offset = AvFILLp(ary) + 1;
5709 after = AvFILLp(ary) + 1 - (offset + length);
5710 if (after < 0) { /* not that much array */
5711 length += after; /* offset+length now in array */
5717 /* At this point, MARK .. SP-1 is our new LIST */
5720 diff = newlen - length;
5721 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5724 /* make new elements SVs now: avoid problems if they're from the array */
5725 for (dst = MARK, i = newlen; i; i--) {
5726 SV * const h = *dst;
5727 *dst++ = newSVsv(h);
5730 if (diff < 0) { /* shrinking the area */
5731 SV **tmparyval = NULL;
5733 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5734 Copy(MARK, tmparyval, newlen, SV*);
5737 MARK = ORIGMARK + 1;
5738 if (GIMME_V == G_LIST) { /* copy return vals to stack */
5739 const bool real = cBOOL(AvREAL(ary));
5740 MEXTEND(MARK, length);
5742 EXTEND_MORTAL(length);
5743 for (i = 0, dst = MARK; i < length; i++) {
5744 if ((*dst = AvARRAY(ary)[i+offset])) {
5746 sv_2mortal(*dst); /* free them eventually */
5749 *dst = &PL_sv_undef;
5755 *MARK = AvARRAY(ary)[offset+length-1];
5758 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5759 SvREFCNT_dec(*dst++); /* free them now */
5762 *MARK = &PL_sv_undef;
5764 AvFILLp(ary) += diff;
5766 /* pull up or down? */
5768 if (offset < after) { /* easier to pull up */
5769 if (offset) { /* esp. if nothing to pull */
5770 src = &AvARRAY(ary)[offset-1];
5771 dst = src - diff; /* diff is negative */
5772 for (i = offset; i > 0; i--) /* can't trust Copy */
5776 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5780 if (after) { /* anything to pull down? */
5781 src = AvARRAY(ary) + offset + length;
5782 dst = src + diff; /* diff is negative */
5783 Move(src, dst, after, SV*);
5785 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5786 /* avoid later double free */
5793 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5794 Safefree(tmparyval);
5797 else { /* no, expanding (or same) */
5798 SV** tmparyval = NULL;
5800 Newx(tmparyval, length, SV*); /* so remember deletion */
5801 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5804 if (diff > 0) { /* expanding */
5805 /* push up or down? */
5806 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5810 Move(src, dst, offset, SV*);
5812 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5814 AvFILLp(ary) += diff;
5817 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5818 av_extend(ary, AvFILLp(ary) + diff);
5819 AvFILLp(ary) += diff;
5822 dst = AvARRAY(ary) + AvFILLp(ary);
5824 for (i = after; i; i--) {
5832 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5835 MARK = ORIGMARK + 1;
5836 if (GIMME_V == G_LIST) { /* copy return vals to stack */
5838 const bool real = cBOOL(AvREAL(ary));
5840 EXTEND_MORTAL(length);
5841 for (i = 0, dst = MARK; i < length; i++) {
5842 if ((*dst = tmparyval[i])) {
5844 sv_2mortal(*dst); /* free them eventually */
5846 else *dst = &PL_sv_undef;
5852 else if (length--) {
5853 *MARK = tmparyval[length];
5856 while (length-- > 0)
5857 SvREFCNT_dec(tmparyval[length]);
5860 *MARK = &PL_sv_undef;
5863 *MARK = &PL_sv_undef;
5864 Safefree(tmparyval);
5868 mg_set(MUTABLE_SV(ary));
5876 dSP; dMARK; dORIGMARK; dTARGET;
5877 AV * const ary = MUTABLE_AV(*++MARK);
5878 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5881 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5884 ENTER_with_name("call_PUSH");
5885 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
5886 LEAVE_with_name("call_PUSH");
5887 /* SPAGAIN; not needed: SP is assigned to immediately below */
5890 /* PL_delaymagic is restored by JMPENV_POP on dieing, so we
5891 * only need to save locally, not on the save stack */
5892 U16 old_delaymagic = PL_delaymagic;
5894 if (SvREADONLY(ary) && MARK < SP) Perl_croak_no_modify();
5895 PL_delaymagic = DM_DELAY;
5896 for (++MARK; MARK <= SP; MARK++) {
5898 if (*MARK) SvGETMAGIC(*MARK);
5899 sv = newSV_type(SVt_NULL);
5901 sv_setsv_nomg(sv, *MARK);
5902 av_store(ary, AvFILLp(ary)+1, sv);
5904 if (PL_delaymagic & DM_ARRAY_ISA)
5905 mg_set(MUTABLE_SV(ary));
5906 PL_delaymagic = old_delaymagic;
5909 if (OP_GIMME(PL_op, 0) != G_VOID) {
5910 PUSHi( AvFILL(ary) + 1 );
5915 /* also used for: pp_pop()*/
5919 AV * const av = PL_op->op_flags & OPf_SPECIAL
5920 ? MUTABLE_AV(GvAVn(PL_defgv)) : MUTABLE_AV(POPs);
5921 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5925 (void)sv_2mortal(sv);
5932 dSP; dMARK; dORIGMARK; dTARGET;
5933 AV *ary = MUTABLE_AV(*++MARK);
5934 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5937 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5940 ENTER_with_name("call_UNSHIFT");
5941 call_sv(SV_CONST(UNSHIFT),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
5942 LEAVE_with_name("call_UNSHIFT");
5943 /* SPAGAIN; not needed: SP is assigned to immediately below */
5946 /* PL_delaymagic is restored by JMPENV_POP on dieing, so we
5947 * only need to save locally, not on the save stack */
5948 U16 old_delaymagic = PL_delaymagic;
5951 av_unshift(ary, SP - MARK);
5952 PL_delaymagic = DM_DELAY;
5954 if (!SvMAGICAL(ary)) {
5955 /* The av_unshift above means that many of the checks inside
5956 * av_store are unnecessary. If ary does not have magic attached
5957 * then a simple direct assignment is possible here. */
5959 SV * const sv = newSVsv(*++MARK);
5960 assert( !SvTIED_mg((const SV *)ary, PERL_MAGIC_tied) );
5962 assert( !SvREADONLY(ary) );
5963 assert( AvREAL(ary) || !AvREIFY(ary) );
5964 assert( i <= AvMAX(ary) );
5965 assert( i <= AvFILLp(ary) );
5967 SvREFCNT_dec(AvARRAY(ary)[i]);
5968 AvARRAY(ary)[i] = sv;
5973 SV * const sv = newSVsv(*++MARK);
5974 (void)av_store(ary, i++, sv);
5978 if (PL_delaymagic & DM_ARRAY_ISA)
5979 mg_set(MUTABLE_SV(ary));
5980 PL_delaymagic = old_delaymagic;
5983 if (OP_GIMME(PL_op, 0) != G_VOID) {
5984 PUSHi( AvFILL(ary) + 1 );
5993 if (GIMME_V == G_LIST) {
5994 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5998 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5999 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
6000 av = MUTABLE_AV((*SP));
6001 /* In-place reversing only happens in void context for the array
6002 * assignment. We don't need to push anything on the stack. */
6005 if (SvMAGICAL(av)) {
6007 SV *tmp = sv_newmortal();
6008 /* For SvCANEXISTDELETE */
6011 bool can_preserve = SvCANEXISTDELETE(av);
6013 for (i = 0, j = av_top_index(av); i < j; ++i, --j) {
6017 if (!av_exists(av, i)) {
6018 if (av_exists(av, j)) {
6019 SV *sv = av_delete(av, j, 0);
6020 begin = *av_fetch(av, i, TRUE);
6021 sv_setsv_mg(begin, sv);
6025 else if (!av_exists(av, j)) {
6026 SV *sv = av_delete(av, i, 0);
6027 end = *av_fetch(av, j, TRUE);
6028 sv_setsv_mg(end, sv);
6033 begin = *av_fetch(av, i, TRUE);
6034 end = *av_fetch(av, j, TRUE);
6035 sv_setsv(tmp, begin);
6036 sv_setsv_mg(begin, end);
6037 sv_setsv_mg(end, tmp);
6041 SV **begin = AvARRAY(av);
6044 SV **end = begin + AvFILLp(av);
6046 while (begin < end) {
6047 SV * const tmp = *begin;
6058 SV * const tmp = *MARK;
6062 /* safe as long as stack cannot get extended in the above */
6071 SvUTF8_off(TARG); /* decontaminate */
6072 if (SP - MARK > 1) {
6073 do_join(TARG, &PL_sv_no, MARK, SP);
6076 } else if (SP > MARK) {
6077 sv_setsv(TARG, *SP);
6080 sv_setsv(TARG, DEFSV);
6083 SvSETMAGIC(TARG); /* remove any utf8 length magic */
6085 up = SvPV_force(TARG, len);
6088 if (DO_UTF8(TARG)) { /* first reverse each character */
6089 U8* s = (U8*)SvPVX(TARG);
6090 const U8* send = (U8*)(s + len);
6092 if (UTF8_IS_INVARIANT(*s)) {
6097 if (!utf8_to_uvchr_buf(s, send, 0))
6101 down = (char*)(s - 1);
6102 /* reverse this character */
6104 const char tmp = *up;
6112 down = SvPVX(TARG) + len - 1;
6114 const char tmp = *up;
6118 (void)SvPOK_only_UTF8(TARG);
6127 AV *ary = ( (PL_op->op_private & OPpSPLIT_ASSIGN) /* @a = split */
6128 && (PL_op->op_flags & OPf_STACKED)) /* @{expr} = split */
6129 ? (AV *)POPs : NULL;
6130 IV limit = POPi; /* note, negative is forever */
6131 SV * const sv = POPs;
6133 const char *s = SvPV_const(sv, len);
6134 const bool do_utf8 = DO_UTF8(sv);
6135 const bool in_uni_8_bit = IN_UNI_8_BIT;
6136 const char *strend = s + len;
6142 const STRLEN slen = do_utf8
6143 ? utf8_length((U8*)s, (U8*)strend)
6144 : (STRLEN)(strend - s);
6145 SSize_t maxiters = slen + 10;
6146 I32 trailing_empty = 0;
6148 const IV origlimit = limit;
6151 const U8 gimme = GIMME_V;
6153 I32 oldsave = PL_savestack_ix;
6154 U32 flags = (do_utf8 ? SVf_UTF8 : 0) |
6155 SVs_TEMP; /* Make mortal SVs by default */
6160 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
6161 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
6163 /* handle @ary = split(...) optimisation */
6164 if (PL_op->op_private & OPpSPLIT_ASSIGN) {
6166 if (!(PL_op->op_flags & OPf_STACKED)) {
6167 if (PL_op->op_private & OPpSPLIT_LEX) {
6168 if (PL_op->op_private & OPpLVAL_INTRO)
6169 SAVECLEARSV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
6170 ary = (AV *)PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff);
6175 MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
6177 pm->op_pmreplrootu.op_pmtargetgv;
6179 if (PL_op->op_private & OPpLVAL_INTRO)
6184 /* skip anything pushed by OPpLVAL_INTRO above */
6185 oldsave = PL_savestack_ix;
6188 /* Some defence against stack-not-refcounted bugs */
6189 (void)sv_2mortal(SvREFCNT_inc_simple_NN(ary));
6191 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
6193 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
6195 flags &= ~SVs_TEMP; /* SVs will not be mortal */
6199 base = SP - PL_stack_base;
6201 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
6203 while (s < strend && isSPACE_utf8_safe(s, strend))
6206 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
6207 while (s < strend && isSPACE_LC(*s))
6210 else if (in_uni_8_bit) {
6211 while (s < strend && isSPACE_L1(*s))
6215 while (s < strend && isSPACE(*s))
6220 gimme_scalar = gimme == G_SCALAR && !ary;
6223 limit = maxiters + 2;
6224 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
6227 /* this one uses 'm' and is a negative test */
6229 while (m < strend && ! isSPACE_utf8_safe(m, strend) ) {
6230 const int t = UTF8SKIP(m);
6231 /* isSPACE_utf8_safe returns FALSE for malform utf8 */
6238 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
6240 while (m < strend && !isSPACE_LC(*m))
6243 else if (in_uni_8_bit) {
6244 while (m < strend && !isSPACE_L1(*m))
6247 while (m < strend && !isSPACE(*m))
6260 dstr = newSVpvn_flags(s, m-s, flags);
6264 /* skip the whitespace found last */
6266 s = m + UTF8SKIP(m);
6270 /* this one uses 's' and is a positive test */
6272 while (s < strend && isSPACE_utf8_safe(s, strend) )
6275 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
6277 while (s < strend && isSPACE_LC(*s))
6280 else if (in_uni_8_bit) {
6281 while (s < strend && isSPACE_L1(*s))
6284 while (s < strend && isSPACE(*s))
6289 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
6291 for (m = s; m < strend && *m != '\n'; m++)
6304 dstr = newSVpvn_flags(s, m-s, flags);
6310 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
6311 /* This case boils down to deciding which is the smaller of:
6312 * limit - effectively a number of characters
6313 * slen - which already contains the number of characters in s
6315 * The resulting number is the number of iters (for gimme_scalar)
6316 * or the number of SVs to create (!gimme_scalar). */
6318 /* setting it to -1 will trigger a panic in EXTEND() */
6319 const SSize_t sslen = slen > SSize_t_MAX ? -1 : (SSize_t)slen;
6320 const IV items = limit - 1;
6321 if (sslen < items || items < 0) {
6324 /* Note: The same result is returned if the following block
6325 * is removed, because of the "keep field after final delim?"
6326 * adjustment, but having the following makes the "correct"
6327 * behaviour more apparent. */
6335 if (!gimme_scalar) {
6337 Pre-extend the stack, either the number of bytes or
6338 characters in the string or a limited amount, triggered by:
6339 my ($x, $y) = split //, $str;
6348 dstr = newSVpvn_flags(m, s-m, flags);
6353 dstr = newSVpvn_flags(s, 1, flags);
6360 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
6361 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
6362 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
6363 && !(RX_EXTFLAGS(rx) & RXf_IS_ANCHORED)) {
6364 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
6365 SV * const csv = CALLREG_INTUIT_STRING(rx);
6367 len = RX_MINLENRET(rx);
6368 if (len == 1 && !RX_UTF8(rx) && !tail) {
6369 const char c = *SvPV_nolen_const(csv);
6371 for (m = s; m < strend && *m != c; m++)
6382 dstr = newSVpvn_flags(s, m-s, flags);
6385 /* The rx->minlen is in characters but we want to step
6386 * s ahead by bytes. */
6388 s = (char*)utf8_hop_forward((U8*) m, len, (U8*) strend);
6390 s = m + len; /* Fake \n at the end */
6394 const bool multiline = (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) ? 1 : 0;
6396 while (s < strend && --limit &&
6397 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
6398 csv, multiline ? FBMrf_MULTILINE : 0)) )
6407 dstr = newSVpvn_flags(s, m-s, flags);
6410 /* The rx->minlen is in characters but we want to step
6411 * s ahead by bytes. */
6413 s = (char*)utf8_hop_forward((U8*)m, len, (U8 *) strend);
6415 s = m + len; /* Fake \n at the end */
6420 maxiters += slen * RX_NPARENS(rx);
6421 while (s < strend && --limit)
6425 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1,
6428 if (rex_return == 0)
6430 TAINT_IF(RX_MATCH_TAINTED(rx));
6431 /* we never pass the REXEC_COPY_STR flag, so it should
6432 * never get copied */
6433 assert(!RX_MATCH_COPIED(rx));
6434 m = RX_OFFS(rx)[0].start + orig;
6443 dstr = newSVpvn_flags(s, m-s, flags);
6446 if (RX_NPARENS(rx)) {
6448 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
6449 s = RX_OFFS(rx)[i].start + orig;
6450 m = RX_OFFS(rx)[i].end + orig;
6452 /* japhy (07/27/01) -- the (m && s) test doesn't catch
6453 parens that didn't match -- they should be set to
6454 undef, not the empty string */
6462 if (m >= orig && s >= orig) {
6463 dstr = newSVpvn_flags(s, m-s, flags);
6466 dstr = &PL_sv_undef; /* undef, not "" */
6472 s = RX_OFFS(rx)[0].end + orig;
6476 if (!gimme_scalar) {
6477 iters = (SP - PL_stack_base) - base;
6479 if (iters > maxiters)
6480 DIE(aTHX_ "Split loop");
6482 /* keep field after final delim? */
6483 if (s < strend || (iters && origlimit)) {
6484 if (!gimme_scalar) {
6485 const STRLEN l = strend - s;
6486 dstr = newSVpvn_flags(s, l, flags);
6491 else if (!origlimit) {
6493 iters -= trailing_empty;
6495 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
6496 if (TOPs && !(flags & SVs_TEMP))
6505 LEAVE_SCOPE(oldsave);
6511 if (av_count(ary) > 0)
6517 if (AvMAX(ary) > -1) {
6518 /* don't free mere refs */
6519 Zero(AvARRAY(ary), AvMAX(ary), SV*);
6522 if(AvMAX(ary) < iters)
6523 av_extend(ary,iters);
6526 /* Need to copy the SV*s from the stack into ary */
6527 Copy(SP + 1 - iters, AvARRAY(ary), iters, SV*);
6528 AvFILLp(ary) = iters - 1;
6530 if (SvSMAGICAL(ary)) {
6532 mg_set(MUTABLE_SV(ary));
6536 if (gimme != G_LIST) {
6537 /* SP points to the final SV* pushed to the stack. But the SV* */
6538 /* are not going to be used from the stack. Point SP to below */
6539 /* the first of these SV*. */
6546 av_extend(ary,iters);
6549 ENTER_with_name("call_PUSH");
6550 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
6551 LEAVE_with_name("call_PUSH");
6554 if (gimme == G_LIST) {
6556 /* EXTEND should not be needed - we just popped them */
6557 EXTEND_SKIP(SP, iters);
6558 for (i=0; i < iters; i++) {
6559 SV **svp = av_fetch(ary, i, FALSE);
6560 PUSHs((svp) ? *svp : &PL_sv_undef);
6567 if (gimme != G_LIST) {
6578 SV *const sv = PAD_SVl(PL_op->op_targ);
6580 if (SvPADSTALE(sv)) {
6583 RETURNOP(cLOGOP->op_other);
6585 RETURNOP(cLOGOP->op_next);
6594 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
6595 || SvTYPE(retsv) == SVt_PVCV) {
6596 retsv = refto(retsv);
6603 /* used for: pp_padany(), pp_custom(); plus any system ops
6604 * that aren't implemented on a particular platform */
6606 PP(unimplemented_op)
6608 const Optype op_type = PL_op->op_type;
6609 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
6610 with out of range op numbers - it only "special" cases op_custom.
6611 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
6612 if we get here for a custom op then that means that the custom op didn't
6613 have an implementation. Given that OP_NAME() looks up the custom op
6614 by its op_ppaddr, likely it will return NULL, unless someone (unhelpfully)
6615 registers &Perl_unimplemented_op as the address of their custom op.
6616 NULL doesn't generate a useful error message. "custom" does. */
6617 const char *const name = op_type >= OP_max
6618 ? "[out of range]" : PL_op_name[op_type];
6619 if(OP_IS_SOCKET(op_type))
6620 DIE(aTHX_ PL_no_sock_func, name);
6621 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
6625 S_maybe_unwind_defav(pTHX)
6627 if (CX_CUR()->cx_type & CXp_HASARGS) {
6628 PERL_CONTEXT *cx = CX_CUR();
6630 assert(CxHASARGS(cx));
6632 cx->cx_type &= ~CXp_HASARGS;
6636 /* For sorting out arguments passed to a &CORE:: subroutine */
6640 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
6641 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
6642 AV * const at_ = GvAV(PL_defgv);
6643 SV **svp = at_ ? AvARRAY(at_) : NULL;
6644 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
6645 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
6646 bool seen_question = 0;
6647 const char *err = NULL;
6648 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
6650 /* Count how many args there are first, to get some idea how far to
6651 extend the stack. */
6653 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
6655 if (oa & OA_OPTIONAL) seen_question = 1;
6656 if (!seen_question) minargs++;
6660 if(numargs < minargs) err = "Not enough";
6661 else if(numargs > maxargs) err = "Too many";
6663 /* diag_listed_as: Too many arguments for %s */
6665 "%s arguments for %s", err,
6666 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
6669 /* Reset the stack pointer. Without this, we end up returning our own
6670 arguments in list context, in addition to the values we are supposed
6671 to return. nextstate usually does this on sub entry, but we need
6672 to run the next op with the caller's hints, so we cannot have a
6674 SP = PL_stack_base + CX_CUR()->blk_oldsp;
6676 if(!maxargs) RETURN;
6678 /* We do this here, rather than with a separate pushmark op, as it has
6679 to come in between two things this function does (stack reset and
6680 arg pushing). This seems the easiest way to do it. */
6683 (void)Perl_pp_pushmark(aTHX);
6686 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
6687 PUTBACK; /* The code below can die in various places. */
6689 oa = PL_opargs[opnum] >> OASHIFT;
6690 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
6695 if (!numargs && defgv && whicharg == minargs + 1) {
6698 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
6702 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
6709 if (CvUNIQUE(find_runcv_where(FIND_RUNCV_level_eq,1,NULL)))
6712 S_maybe_unwind_defav(aTHX);
6715 PUSHs((SV *)GvAVn(gv));
6718 if (!svp || !*svp || !SvROK(*svp)
6719 || SvTYPE(SvRV(*svp)) != SVt_PVAV)
6721 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
6722 "Type of arg %d to &CORE::%s must be array reference",
6723 whicharg, PL_op_desc[opnum]
6728 if (!svp || !*svp || !SvROK(*svp)
6729 || ( SvTYPE(SvRV(*svp)) != SVt_PVHV
6730 && ( opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
6731 || SvTYPE(SvRV(*svp)) != SVt_PVAV )))
6733 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
6734 "Type of arg %d to &CORE::%s must be hash%s reference",
6735 whicharg, PL_op_desc[opnum],
6736 opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
6743 if (!numargs) PUSHs(NULL);
6744 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
6745 /* no magic here, as the prototype will have added an extra
6746 refgen and we just want what was there before that */
6749 const bool constr = PL_op->op_private & whicharg;
6751 svp && *svp ? *svp : &PL_sv_undef,
6752 constr, cBOOL(CopHINTS_get(PL_curcop) & HINT_STRICT_REFS),
6758 if (!numargs) goto try_defsv;
6760 const bool wantscalar =
6761 PL_op->op_private & OPpCOREARGS_SCALARMOD;
6762 if (!svp || !*svp || !SvROK(*svp)
6763 /* We have to permit globrefs even for the \$ proto, as
6764 *foo is indistinguishable from ${\*foo}, and the proto-
6765 type permits the latter. */
6766 || SvTYPE(SvRV(*svp)) > (
6767 wantscalar ? SVt_PVLV
6768 : opnum == OP_LOCK || opnum == OP_UNDEF
6774 "Type of arg %d to &CORE::%s must be %s",
6775 whicharg, PL_op_name[opnum],
6777 ? "scalar reference"
6778 : opnum == OP_LOCK || opnum == OP_UNDEF
6779 ? "reference to one of [$@%&*]"
6780 : "reference to one of [$@%*]"
6783 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv) {
6784 /* Undo @_ localisation, so that sub exit does not undo
6785 part of our undeffing. */
6786 S_maybe_unwind_defav(aTHX);
6791 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
6799 /* Implement CORE::keys(),values(),each().
6801 * We won't know until run-time whether the arg is an array or hash,
6804 * pp_keys/pp_values/pp_each
6806 * pp_akeys/pp_avalues/pp_aeach
6808 * as appropriate (or whatever pp function actually implements the OP_FOO
6809 * functionality for each FOO).
6816 (SvTYPE(TOPs) == SVt_PVAV ? OP_AEACH : OP_EACH)
6817 + (PL_op->op_private & OPpAVHVSWITCH_MASK)
6825 if (PL_op->op_private & OPpOFFBYONE) {
6826 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
6828 else cv = find_runcv(NULL);
6829 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
6834 S_localise_aelem_lval(pTHX_ AV * const av, SV * const keysv,
6835 const bool can_preserve)
6837 const SSize_t ix = SvIV(keysv);
6838 if (can_preserve ? av_exists(av, ix) : TRUE) {
6839 SV ** const svp = av_fetch(av, ix, 1);
6841 Perl_croak(aTHX_ PL_no_aelem, ix);
6842 save_aelem(av, ix, svp);
6845 SAVEADELETE(av, ix);
6849 S_localise_helem_lval(pTHX_ HV * const hv, SV * const keysv,
6850 const bool can_preserve)
6852 if (can_preserve ? hv_exists_ent(hv, keysv, 0) : TRUE) {
6853 HE * const he = hv_fetch_ent(hv, keysv, 1, 0);
6854 SV ** const svp = he ? &HeVAL(he) : NULL;
6856 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(keysv));
6857 save_helem_flags(hv, keysv, svp, 0);
6860 SAVEHDELETE(hv, keysv);
6864 S_localise_gv_slot(pTHX_ GV *gv, U8 type)
6866 if (type == OPpLVREF_SV) {
6867 save_pushptrptr(gv, SvREFCNT_inc_simple(GvSV(gv)), SAVEt_GVSV);
6870 else if (type == OPpLVREF_AV)
6871 /* XXX Inefficient, as it creates a new AV, which we are
6872 about to clobber. */
6875 assert(type == OPpLVREF_HV);
6876 /* XXX Likewise inefficient. */
6885 SV * const key = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
6886 SV * const left = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
6888 const char *bad = NULL;
6889 const U8 type = PL_op->op_private & OPpLVREF_TYPE;
6890 if (!SvROK(sv)) DIE(aTHX_ "Assigned value is not a reference");
6893 if (SvTYPE(SvRV(sv)) > SVt_PVLV)
6897 if (SvTYPE(SvRV(sv)) != SVt_PVAV)
6901 if (SvTYPE(SvRV(sv)) != SVt_PVHV)
6905 if (SvTYPE(SvRV(sv)) != SVt_PVCV)
6909 /* diag_listed_as: Assigned value is not %s reference */
6910 DIE(aTHX_ "Assigned value is not a%s reference", bad);
6914 switch (left ? SvTYPE(left) : 0) {
6917 SV * const old = PAD_SV(ARGTARG);
6918 PAD_SETSV(ARGTARG, SvREFCNT_inc_NN(SvRV(sv)));
6920 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
6922 SAVECLEARSV(PAD_SVl(ARGTARG));
6926 if (PL_op->op_private & OPpLVAL_INTRO) {
6927 S_localise_gv_slot(aTHX_ (GV *)left, type);
6929 gv_setref(left, sv);
6934 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6935 S_localise_aelem_lval(aTHX_ (AV *)left, key,
6936 SvCANEXISTDELETE(left));
6938 av_store((AV *)left, SvIV(key), SvREFCNT_inc_simple_NN(SvRV(sv)));
6941 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6943 S_localise_helem_lval(aTHX_ (HV *)left, key,
6944 SvCANEXISTDELETE(left));
6946 (void)hv_store_ent((HV *)left, key, SvREFCNT_inc_simple_NN(SvRV(sv)), 0);
6948 if (PL_op->op_flags & OPf_MOD)
6949 SETs(sv_2mortal(newSVsv(sv)));
6950 /* XXX else can weak references go stale before they are read, e.g.,
6959 SV * const ret = newSV_type_mortal(SVt_PVMG);
6960 SV * const elem = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
6961 SV * const arg = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
6962 MAGIC * const mg = sv_magicext(ret, arg, PERL_MAGIC_lvref,
6963 &PL_vtbl_lvref, (char *)elem,
6964 elem ? HEf_SVKEY : (I32)ARGTARG);
6965 mg->mg_private = PL_op->op_private;
6966 if (PL_op->op_private & OPpLVREF_ITER)
6967 mg->mg_flags |= MGf_PERSIST;
6968 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6974 const bool can_preserve = SvCANEXISTDELETE(arg);
6975 if (SvTYPE(arg) == SVt_PVAV)
6976 S_localise_aelem_lval(aTHX_ (AV *)arg, elem, can_preserve);
6978 S_localise_helem_lval(aTHX_ (HV *)arg, elem, can_preserve);
6982 S_localise_gv_slot(aTHX_ (GV *)arg,
6983 PL_op->op_private & OPpLVREF_TYPE);
6985 else if (!(PL_op->op_private & OPpPAD_STATE))
6986 SAVECLEARSV(PAD_SVl(ARGTARG));
6995 AV * const av = (AV *)POPs;
6996 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
6997 bool can_preserve = FALSE;
6999 if (UNLIKELY(localizing)) {
7004 can_preserve = SvCANEXISTDELETE(av);
7006 if (SvTYPE(av) == SVt_PVAV) {
7009 for (svp = MARK + 1; svp <= SP; svp++) {
7010 const SSize_t elem = SvIV(*svp);
7014 if (max > AvMAX(av))
7019 while (++MARK <= SP) {
7020 SV * const elemsv = *MARK;
7021 if (UNLIKELY(localizing)) {
7022 if (SvTYPE(av) == SVt_PVAV)
7023 S_localise_aelem_lval(aTHX_ av, elemsv, can_preserve);
7025 S_localise_helem_lval(aTHX_ (HV *)av, elemsv, can_preserve);
7027 *MARK = newSV_type_mortal(SVt_PVMG);
7028 sv_magic(*MARK,(SV *)av,PERL_MAGIC_lvref,(char *)elemsv,HEf_SVKEY);
7035 if (PL_op->op_flags & OPf_STACKED)
7036 Perl_pp_rv2av(aTHX);
7038 Perl_pp_padav(aTHX);
7042 SETs(0); /* special alias marker that aassign recognises */
7053 CV* constsub = newCONSTSUB(
7054 SvTYPE(CopSTASH(PL_curcop))==SVt_PVHV ? CopSTASH(PL_curcop) : NULL,
7056 SvREFCNT_inc_simple_NN(sv)
7059 SV* ret_sv = sv_2mortal((SV *)constsub);
7061 /* Prior to Perl 5.38 anonconst ops always fed into srefgen.
7062 5.38 redefined anonconst to create the reference without srefgen.
7063 OPf_REF was added to the op. In case some XS code out there creates
7064 anonconst the old way, we accommodate OPf_REF's absence here.
7066 if (LIKELY(PL_op->op_flags & OPf_REF)) {
7067 ret_sv = refto(ret_sv);
7075 /* process one subroutine argument - typically when the sub has a signature:
7076 * introduce PL_curpad[op_targ] and assign to it the value
7077 * for $: (OPf_STACKED ? *sp : $_[N])
7078 * for @/%: @_[N..$#_]
7080 * It's equivalent to
7083 * my $foo = (value-on-stack)
7085 * my @foo = @_[N..$#_]
7095 AV *defav = GvAV(PL_defgv); /* @_ */
7096 IV ix = PTR2IV(cUNOP_AUXo->op_aux);
7099 /* do 'my $var, @var or %var' action */
7100 padentry = &(PAD_SVl(o->op_targ));
7101 save_clearsv(padentry);
7104 if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_SV) {
7105 if (o->op_flags & OPf_STACKED) {
7112 /* should already have been checked */
7114 #if IVSIZE > PTRSIZE
7115 assert(ix <= SSize_t_MAX);
7118 svp = av_fetch(defav, ix, FALSE);
7119 val = svp ? *svp : &PL_sv_undef;
7124 /* cargo-culted from pp_sassign */
7125 assert(TAINTING_get || !TAINT_get);
7126 if (UNLIKELY(TAINT_get) && !SvTAINTED(val))
7129 SvSetMagicSV(targ, val);
7133 /* must be AV or HV */
7135 assert(!(o->op_flags & OPf_STACKED));
7136 argc = ((IV)AvFILL(defav) + 1) - ix;
7138 /* This is a copy of the relevant parts of pp_aassign().
7140 if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_AV) {
7143 if (AvFILL((AV*)targ) > -1) {
7144 /* target should usually be empty. If we get get
7145 * here, someone's been doing some weird closure tricks.
7146 * Make a copy of all args before clearing the array,
7147 * to avoid the equivalent of @a = ($a[0]) prematurely freeing
7148 * elements. See similar code in pp_aassign.
7150 for (i = 0; i < argc; i++) {
7151 SV **svp = av_fetch(defav, ix + i, FALSE);
7152 SV *newsv = newSVsv_flags(svp ? *svp : &PL_sv_undef,
7153 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
7154 if (!av_store(defav, ix + i, newsv))
7155 SvREFCNT_dec_NN(newsv);
7157 av_clear((AV*)targ);
7163 av_extend((AV*)targ, argc);
7168 SV **svp = av_fetch(defav, ix + i, FALSE);
7169 SV *val = svp ? *svp : &PL_sv_undef;
7170 tmpsv = newSV_type(SVt_NULL);
7171 sv_setsv(tmpsv, val);
7172 av_store((AV*)targ, i++, tmpsv);
7180 assert((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_HV);
7182 if (SvRMAGICAL(targ) || HvUSEDKEYS((HV*)targ)) {
7183 /* see "target should usually be empty" comment above */
7184 for (i = 0; i < argc; i++) {
7185 SV **svp = av_fetch(defav, ix + i, FALSE);
7186 SV *newsv = newSV_type(SVt_NULL);
7187 sv_setsv_flags(newsv,
7188 svp ? *svp : &PL_sv_undef,
7189 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
7190 if (!av_store(defav, ix + i, newsv))
7191 SvREFCNT_dec_NN(newsv);
7193 hv_clear((HV*)targ);
7198 assert(argc % 2 == 0);
7207 svp = av_fetch(defav, ix + i++, FALSE);
7208 key = svp ? *svp : &PL_sv_undef;
7209 svp = av_fetch(defav, ix + i++, FALSE);
7210 val = svp ? *svp : &PL_sv_undef;
7213 if (UNLIKELY(SvGMAGICAL(key)))
7214 key = sv_mortalcopy(key);
7215 tmpsv = newSV_type(SVt_NULL);
7216 sv_setsv(tmpsv, val);
7217 hv_store_ent((HV*)targ, key, tmpsv, 0);
7225 /* Handle a default value for one subroutine argument (typically as part
7226 * of a subroutine signature).
7227 * It's equivalent to
7228 * @_ > op_targ ? $_[op_targ] : result_of(op_other)
7230 * Intended to be used where op_next is an OP_ARGELEM
7232 * We abuse the op_targ field slightly: it's an index into @_ rather than
7238 OP * const o = PL_op;
7239 AV *defav = GvAV(PL_defgv); /* @_ */
7240 IV ix = (IV)o->op_targ;
7243 #if IVSIZE > PTRSIZE
7244 assert(ix <= SSize_t_MAX);
7247 if (AvFILL(defav) >= ix) {
7249 SV **svp = av_fetch(defav, ix, FALSE);
7250 SV *val = svp ? *svp : &PL_sv_undef;
7254 return cLOGOPo->op_other;
7259 S_find_runcv_name(void)
7274 sv = sv_newmortal();
7275 gv_fullname4(sv, gv, NULL, TRUE);
7279 /* Check a sub's arguments - i.e. that it has the correct number of args
7280 * (and anything else we might think of in future). Typically used with
7286 OP * const o = PL_op;
7287 struct op_argcheck_aux *aux = (struct op_argcheck_aux *)cUNOP_AUXo->op_aux;
7288 UV params = aux->params;
7289 UV opt_params = aux->opt_params;
7290 char slurpy = aux->slurpy;
7291 AV *defav = GvAV(PL_defgv); /* @_ */
7295 assert(!SvMAGICAL(defav));
7296 argc = (UV)(AvFILLp(defav) + 1);
7297 too_few = (argc < (params - opt_params));
7299 if (UNLIKELY(too_few || (!slurpy && argc > params)))
7301 /* diag_listed_as: Too few arguments for subroutine '%s' (got %d; expected %d) */
7302 /* diag_listed_as: Too few arguments for subroutine '%s' (got %d; expected at least %d) */
7303 /* diag_listed_as: Too many arguments for subroutine '%s' (got %d; expected %d) */
7304 /* diag_listed_as: Too many arguments for subroutine '%s' (got %d; expected at most %d)*/
7305 Perl_croak_caller("Too %s arguments for subroutine '%" SVf "' (got %" UVuf "; expected %s%" UVuf ")",
7306 too_few ? "few" : "many",
7307 S_find_runcv_name(),
7309 too_few ? (slurpy || opt_params ? "at least " : "") : (opt_params ? "at most " : ""),
7310 too_few ? (params - opt_params) : params);
7312 if (UNLIKELY(slurpy == '%' && argc > params && (argc - params) % 2))
7313 /* diag_listed_as: Odd name/value argument for subroutine '%s' */
7314 Perl_croak_caller("Odd name/value argument for subroutine '%" SVf "'",
7315 S_find_runcv_name());
7328 SETs(boolSV(sv_isa_sv(left, right)));
7337 if (SvTRUE_NN(result)) {
7338 return cLOGOP->op_other;
7358 SV *arg = *PL_stack_sp;
7362 *PL_stack_sp = boolSV(SvIsBOOL(arg));
7368 SV *arg = *PL_stack_sp;
7372 *PL_stack_sp = boolSV(SvWEAKREF(arg));
7402 if(!SvROK(arg) || !SvOBJECT((rv = SvRV(arg)))) {
7407 if((PL_op->op_private & OPpTRUEBOOL) ||
7408 ((PL_op->op_private & OPpMAYBE_TRUEBOOL) && (block_gimme() == G_VOID))) {
7409 /* We only care about the boolean truth, not the specific string value.
7410 * We just have to check for the annoying cornercase of the package
7412 HV *stash = SvSTASH(rv);
7413 HEK *hek = HvNAME_HEK(stash);
7416 I32 len = HEK_LEN(hek);
7417 if(UNLIKELY(len == HEf_SVKEY || (len == 1 && HEK_KEY(hek)[0] == '0')))
7424 SETs(sv_ref(NULL, rv, TRUE));
7439 sv_setuv_mg(TARG, PTR2UV(SvRV(arg)));
7441 sv_setsv(TARG, &PL_sv_undef);
7456 sv_setpv_mg(TARG, sv_reftype(SvRV(arg), FALSE));
7458 sv_setsv(TARG, &PL_sv_undef);
7468 PUSHn(Perl_ceil(POPn));
7476 PUSHn(Perl_floor(POPn));
7482 SV *arg = *PL_stack_sp;
7486 *PL_stack_sp = boolSV(SvTAINTED(arg));
7491 * ex: set ts=8 sts=4 sw=4 et: