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.
33 /* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
37 #ifdef NEED_GETPID_PROTO
38 extern Pid_t getpid (void);
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
45 #if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
49 /* variations on pp_null */
55 if (GIMME_V == G_SCALAR)
66 assert(SvTYPE(TARG) == SVt_PVAV);
67 if (PL_op->op_private & OPpLVAL_INTRO)
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
71 if (PL_op->op_flags & OPf_REF) {
74 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
75 const I32 flags = is_lvalue_sub();
76 if (flags && !(flags & OPpENTERSUB_INARGS)) {
77 if (GIMME == G_SCALAR)
78 /* diag_listed_as: Can't return %s to lvalue scalar context */
79 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
85 if (gimme == G_ARRAY) {
86 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
88 if (SvMAGICAL(TARG)) {
90 for (i=0; i < (U32)maxarg; i++) {
91 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
92 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
96 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
100 else if (gimme == G_SCALAR) {
101 SV* const sv = sv_newmortal();
102 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
103 sv_setiv(sv, maxarg);
114 assert(SvTYPE(TARG) == SVt_PVHV);
116 if (PL_op->op_private & OPpLVAL_INTRO)
117 if (!(PL_op->op_private & OPpPAD_STATE))
118 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
119 if (PL_op->op_flags & OPf_REF)
121 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
122 const I32 flags = is_lvalue_sub();
123 if (flags && !(flags & OPpENTERSUB_INARGS)) {
124 if (GIMME == G_SCALAR)
125 /* diag_listed_as: Can't return %s to lvalue scalar context */
126 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
131 if (gimme == G_ARRAY) {
132 RETURNOP(Perl_do_kv(aTHX));
134 else if (gimme == G_SCALAR) {
135 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
143 static const char S_no_symref_sv[] =
144 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
146 /* In some cases this function inspects PL_op. If this function is called
147 for new op types, more bool parameters may need to be added in place of
150 When noinit is true, the absence of a gv will cause a retval of undef.
151 This is unrelated to the cv-to-gv assignment case.
155 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
159 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
162 sv = amagic_deref_call(sv, to_gv_amg);
166 if (SvTYPE(sv) == SVt_PVIO) {
167 GV * const gv = MUTABLE_GV(sv_newmortal());
168 gv_init(gv, 0, "__ANONIO__", 10, 0);
169 GvIOp(gv) = MUTABLE_IO(sv);
170 SvREFCNT_inc_void_NN(sv);
173 else if (!isGV_with_GP(sv))
174 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
177 if (!isGV_with_GP(sv)) {
179 /* If this is a 'my' scalar and flag is set then vivify
182 if (vivify_sv && sv != &PL_sv_undef) {
185 Perl_croak_no_modify(aTHX);
186 if (cUNOP->op_targ) {
187 SV * const namesv = PAD_SV(cUNOP->op_targ);
188 gv = MUTABLE_GV(newSV(0));
189 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
192 const char * const name = CopSTASHPV(PL_curcop);
193 gv = newGVgen_flags(name,
194 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
196 prepare_SV_for_RV(sv);
197 SvRV_set(sv, MUTABLE_SV(gv));
202 if (PL_op->op_flags & OPf_REF || strict)
203 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
204 if (ckWARN(WARN_UNINITIALIZED))
210 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
211 sv, GV_ADDMG, SVt_PVGV
221 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
224 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
225 == OPpDONT_INIT_GV) {
226 /* We are the target of a coderef assignment. Return
227 the scalar unchanged, and let pp_sasssign deal with
231 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
233 /* FAKE globs in the symbol table cause weird bugs (#77810) */
237 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
238 SV *newsv = sv_newmortal();
239 sv_setsv_flags(newsv, sv, 0);
251 sv, PL_op->op_private & OPpDEREF,
252 PL_op->op_private & HINT_STRICT_REFS,
253 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
254 || PL_op->op_type == OP_READLINE
256 if (PL_op->op_private & OPpLVAL_INTRO)
257 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
262 /* Helper function for pp_rv2sv and pp_rv2av */
264 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
265 const svtype type, SV ***spp)
270 PERL_ARGS_ASSERT_SOFTREF2XV;
272 if (PL_op->op_private & HINT_STRICT_REFS) {
274 Perl_die(aTHX_ S_no_symref_sv, sv,
275 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
277 Perl_die(aTHX_ PL_no_usym, what);
281 PL_op->op_flags & OPf_REF &&
282 PL_op->op_next->op_type != OP_BOOLKEYS
284 Perl_die(aTHX_ PL_no_usym, what);
285 if (ckWARN(WARN_UNINITIALIZED))
287 if (type != SVt_PV && GIMME_V == G_ARRAY) {
291 **spp = &PL_sv_undef;
294 if ((PL_op->op_flags & OPf_SPECIAL) &&
295 !(PL_op->op_flags & OPf_MOD))
297 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
299 **spp = &PL_sv_undef;
304 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
317 sv = amagic_deref_call(sv, to_sv_amg);
321 switch (SvTYPE(sv)) {
327 DIE(aTHX_ "Not a SCALAR reference");
334 if (!isGV_with_GP(gv)) {
335 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
341 if (PL_op->op_flags & OPf_MOD) {
342 if (PL_op->op_private & OPpLVAL_INTRO) {
343 if (cUNOP->op_first->op_type == OP_NULL)
344 sv = save_scalar(MUTABLE_GV(TOPs));
346 sv = save_scalar(gv);
348 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
350 else if (PL_op->op_private & OPpDEREF)
351 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
360 AV * const av = MUTABLE_AV(TOPs);
361 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
363 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
365 *sv = newSV_type(SVt_PVMG);
366 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
370 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
379 if (PL_op->op_flags & OPf_MOD || LVRET) {
380 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
381 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
383 LvTARG(ret) = SvREFCNT_inc_simple(sv);
384 PUSHs(ret); /* no SvSETMAGIC */
388 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
389 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
390 if (mg && mg->mg_len >= 0) {
408 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
410 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
413 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
414 /* (But not in defined().) */
416 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
418 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
422 cv = MUTABLE_CV(&PL_sv_undef);
423 SETs(MUTABLE_SV(cv));
433 SV *ret = &PL_sv_undef;
435 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
436 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
437 const char * s = SvPVX_const(TOPs);
438 if (strnEQ(s, "CORE::", 6)) {
439 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
440 if (!code || code == -KEY_CORE)
441 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
442 SVfARG(newSVpvn_flags(
443 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
446 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
452 cv = sv_2cv(TOPs, &stash, &gv, 0);
454 ret = newSVpvn_flags(
455 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
465 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
467 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
469 PUSHs(MUTABLE_SV(cv));
483 if (GIMME != G_ARRAY) {
487 *MARK = &PL_sv_undef;
488 *MARK = refto(*MARK);
492 EXTEND_MORTAL(SP - MARK);
494 *MARK = refto(*MARK);
499 S_refto(pTHX_ SV *sv)
504 PERL_ARGS_ASSERT_REFTO;
506 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
509 if (!(sv = LvTARG(sv)))
512 SvREFCNT_inc_void_NN(sv);
514 else if (SvTYPE(sv) == SVt_PVAV) {
515 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
516 av_reify(MUTABLE_AV(sv));
518 SvREFCNT_inc_void_NN(sv);
520 else if (SvPADTMP(sv) && !IS_PADGV(sv))
524 SvREFCNT_inc_void_NN(sv);
527 sv_upgrade(rv, SVt_IV);
536 SV * const sv = POPs;
541 if (!sv || !SvROK(sv))
544 (void)sv_ref(TARG,SvRV(sv),TRUE);
556 stash = CopSTASH(PL_curcop);
558 SV * const ssv = POPs;
562 if (!ssv) goto curstash;
563 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
564 Perl_croak(aTHX_ "Attempt to bless into a reference");
565 ptr = SvPV_const(ssv,len);
567 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
568 "Explicit blessing to '' (assuming package main)");
569 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
572 (void)sv_bless(TOPs, stash);
582 const char * const elem = SvPV_const(sv, len);
583 GV * const gv = MUTABLE_GV(POPs);
588 /* elem will always be NUL terminated. */
589 const char * const second_letter = elem + 1;
592 if (len == 5 && strEQ(second_letter, "RRAY"))
593 tmpRef = MUTABLE_SV(GvAV(gv));
596 if (len == 4 && strEQ(second_letter, "ODE"))
597 tmpRef = MUTABLE_SV(GvCVu(gv));
600 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
601 /* finally deprecated in 5.8.0 */
602 deprecate("*glob{FILEHANDLE}");
603 tmpRef = MUTABLE_SV(GvIOp(gv));
606 if (len == 6 && strEQ(second_letter, "ORMAT"))
607 tmpRef = MUTABLE_SV(GvFORM(gv));
610 if (len == 4 && strEQ(second_letter, "LOB"))
611 tmpRef = MUTABLE_SV(gv);
614 if (len == 4 && strEQ(second_letter, "ASH"))
615 tmpRef = MUTABLE_SV(GvHV(gv));
618 if (*second_letter == 'O' && !elem[2] && len == 2)
619 tmpRef = MUTABLE_SV(GvIOp(gv));
622 if (len == 4 && strEQ(second_letter, "AME"))
623 sv = newSVhek(GvNAME_HEK(gv));
626 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
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 (len == 6 && strEQ(second_letter, "CALAR"))
648 /* Pattern matching */
653 register unsigned char *s;
656 s = (unsigned char*)(SvPV(sv, len));
657 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
658 /* 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. */
672 if (PL_op->op_flags & OPf_STACKED)
674 else if (PL_op->op_private & OPpTARGET_MY)
680 TARG = sv_newmortal();
681 if(PL_op->op_type == OP_TRANSR) {
683 const char * const pv = SvPV(sv,len);
684 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
688 else PUSHi(do_trans(sv));
692 /* Lvalue operators. */
695 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
701 PERL_ARGS_ASSERT_DO_CHOMP;
703 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
705 if (SvTYPE(sv) == SVt_PVAV) {
707 AV *const av = MUTABLE_AV(sv);
708 const I32 max = AvFILL(av);
710 for (i = 0; i <= max; i++) {
711 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
712 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
713 do_chomp(retval, sv, chomping);
717 else if (SvTYPE(sv) == SVt_PVHV) {
718 HV* const hv = MUTABLE_HV(sv);
720 (void)hv_iterinit(hv);
721 while ((entry = hv_iternext(hv)))
722 do_chomp(retval, hv_iterval(hv,entry), chomping);
725 else if (SvREADONLY(sv)) {
727 /* SV is copy-on-write */
728 sv_force_normal_flags(sv, 0);
731 Perl_croak_no_modify(aTHX);
736 /* XXX, here sv is utf8-ized as a side-effect!
737 If encoding.pm is used properly, almost string-generating
738 operations, including literal strings, chr(), input data, etc.
739 should have been utf8-ized already, right?
741 sv_recode_to_utf8(sv, PL_encoding);
747 char *temp_buffer = NULL;
756 while (len && s[-1] == '\n') {
763 STRLEN rslen, rs_charlen;
764 const char *rsptr = SvPV_const(PL_rs, rslen);
766 rs_charlen = SvUTF8(PL_rs)
770 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
771 /* Assumption is that rs is shorter than the scalar. */
773 /* RS is utf8, scalar is 8 bit. */
775 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
778 /* Cannot downgrade, therefore cannot possibly match
780 assert (temp_buffer == rsptr);
786 else if (PL_encoding) {
787 /* RS is 8 bit, encoding.pm is used.
788 * Do not recode PL_rs as a side-effect. */
789 svrecode = newSVpvn(rsptr, rslen);
790 sv_recode_to_utf8(svrecode, PL_encoding);
791 rsptr = SvPV_const(svrecode, rslen);
792 rs_charlen = sv_len_utf8(svrecode);
795 /* RS is 8 bit, scalar is utf8. */
796 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
810 if (memNE(s, rsptr, rslen))
812 SvIVX(retval) += rs_charlen;
815 s = SvPV_force_nomg_nolen(sv);
823 SvREFCNT_dec(svrecode);
825 Safefree(temp_buffer);
827 if (len && !SvPOK(sv))
828 s = SvPV_force_nomg(sv, len);
831 char * const send = s + len;
832 char * const start = s;
834 while (s > start && UTF8_IS_CONTINUATION(*s))
836 if (is_utf8_string((U8*)s, send - s)) {
837 sv_setpvn(retval, s, send - s);
839 SvCUR_set(sv, s - start);
845 sv_setpvs(retval, "");
849 sv_setpvn(retval, s, 1);
856 sv_setpvs(retval, "");
864 const bool chomping = PL_op->op_type == OP_SCHOMP;
868 do_chomp(TARG, TOPs, chomping);
875 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
876 const bool chomping = PL_op->op_type == OP_CHOMP;
881 do_chomp(TARG, *++MARK, chomping);
892 if (!PL_op->op_private) {
901 SV_CHECK_THINKFIRST_COW_DROP(sv);
903 switch (SvTYPE(sv)) {
907 av_undef(MUTABLE_AV(sv));
910 hv_undef(MUTABLE_HV(sv));
913 if (cv_const_sv((const CV *)sv))
914 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
915 "Constant subroutine %"SVf" undefined",
916 SVfARG(CvANON((const CV *)sv)
917 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
918 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
922 /* let user-undef'd sub keep its identity */
923 GV* const gv = CvGV((const CV *)sv);
924 cv_undef(MUTABLE_CV(sv));
925 CvGV_set(MUTABLE_CV(sv), gv);
930 SvSetMagicSV(sv, &PL_sv_undef);
933 else if (isGV_with_GP(sv)) {
937 /* undef *Pkg::meth_name ... */
939 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
940 && HvENAME_get(stash);
942 if((stash = GvHV((const GV *)sv))) {
943 if(HvENAME_get(stash))
944 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
948 gp_free(MUTABLE_GV(sv));
950 GvGP_set(sv, gp_ref(gp));
952 GvLINE(sv) = CopLINE(PL_curcop);
953 GvEGV(sv) = MUTABLE_GV(sv);
957 mro_package_moved(NULL, stash, (const GV *)sv, 0);
959 /* undef *Foo::ISA */
960 if( strEQ(GvNAME((const GV *)sv), "ISA")
961 && (stash = GvSTASH((const GV *)sv))
962 && (method_changed || HvENAME(stash)) )
963 mro_isa_changed_in(stash);
964 else if(method_changed)
965 mro_method_changed_in(
966 GvSTASH((const GV *)sv)
973 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
989 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
990 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
991 Perl_croak_no_modify(aTHX);
993 TARG = sv_newmortal();
994 sv_setsv(TARG, TOPs);
995 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
996 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
998 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
999 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1003 else sv_dec_nomg(TOPs);
1005 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1006 if (inc && !SvOK(TARG))
1012 /* Ordinary operators. */
1016 dVAR; dSP; dATARGET; SV *svl, *svr;
1017 #ifdef PERL_PRESERVE_IVUV
1020 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1023 #ifdef PERL_PRESERVE_IVUV
1024 /* For integer to integer power, we do the calculation by hand wherever
1025 we're sure it is safe; otherwise we call pow() and try to convert to
1026 integer afterwards. */
1027 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1035 const IV iv = SvIVX(svr);
1039 goto float_it; /* Can't do negative powers this way. */
1043 baseuok = SvUOK(svl);
1045 baseuv = SvUVX(svl);
1047 const IV iv = SvIVX(svl);
1050 baseuok = TRUE; /* effectively it's a UV now */
1052 baseuv = -iv; /* abs, baseuok == false records sign */
1055 /* now we have integer ** positive integer. */
1058 /* foo & (foo - 1) is zero only for a power of 2. */
1059 if (!(baseuv & (baseuv - 1))) {
1060 /* We are raising power-of-2 to a positive integer.
1061 The logic here will work for any base (even non-integer
1062 bases) but it can be less accurate than
1063 pow (base,power) or exp (power * log (base)) when the
1064 intermediate values start to spill out of the mantissa.
1065 With powers of 2 we know this can't happen.
1066 And powers of 2 are the favourite thing for perl
1067 programmers to notice ** not doing what they mean. */
1069 NV base = baseuok ? baseuv : -(NV)baseuv;
1074 while (power >>= 1) {
1082 SvIV_please_nomg(svr);
1085 register unsigned int highbit = 8 * sizeof(UV);
1086 register unsigned int diff = 8 * sizeof(UV);
1087 while (diff >>= 1) {
1089 if (baseuv >> highbit) {
1093 /* we now have baseuv < 2 ** highbit */
1094 if (power * highbit <= 8 * sizeof(UV)) {
1095 /* result will definitely fit in UV, so use UV math
1096 on same algorithm as above */
1097 register UV result = 1;
1098 register UV base = baseuv;
1099 const bool odd_power = cBOOL(power & 1);
1103 while (power >>= 1) {
1110 if (baseuok || !odd_power)
1111 /* answer is positive */
1113 else if (result <= (UV)IV_MAX)
1114 /* answer negative, fits in IV */
1115 SETi( -(IV)result );
1116 else if (result == (UV)IV_MIN)
1117 /* 2's complement assumption: special case IV_MIN */
1120 /* answer negative, doesn't fit */
1121 SETn( -(NV)result );
1129 NV right = SvNV_nomg(svr);
1130 NV left = SvNV_nomg(svl);
1133 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1135 We are building perl with long double support and are on an AIX OS
1136 afflicted with a powl() function that wrongly returns NaNQ for any
1137 negative base. This was reported to IBM as PMR #23047-379 on
1138 03/06/2006. The problem exists in at least the following versions
1139 of AIX and the libm fileset, and no doubt others as well:
1141 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1142 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1143 AIX 5.2.0 bos.adt.libm 5.2.0.85
1145 So, until IBM fixes powl(), we provide the following workaround to
1146 handle the problem ourselves. Our logic is as follows: for
1147 negative bases (left), we use fmod(right, 2) to check if the
1148 exponent is an odd or even integer:
1150 - if odd, powl(left, right) == -powl(-left, right)
1151 - if even, powl(left, right) == powl(-left, right)
1153 If the exponent is not an integer, the result is rightly NaNQ, so
1154 we just return that (as NV_NAN).
1158 NV mod2 = Perl_fmod( right, 2.0 );
1159 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1160 SETn( -Perl_pow( -left, right) );
1161 } else if (mod2 == 0.0) { /* even integer */
1162 SETn( Perl_pow( -left, right) );
1163 } else { /* fractional power */
1167 SETn( Perl_pow( left, right) );
1170 SETn( Perl_pow( left, right) );
1171 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1173 #ifdef PERL_PRESERVE_IVUV
1175 SvIV_please_nomg(svr);
1183 dVAR; dSP; dATARGET; SV *svl, *svr;
1184 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1187 #ifdef PERL_PRESERVE_IVUV
1188 if (SvIV_please_nomg(svr)) {
1189 /* Unless the left argument is integer in range we are going to have to
1190 use NV maths. Hence only attempt to coerce the right argument if
1191 we know the left is integer. */
1192 /* Left operand is defined, so is it IV? */
1193 if (SvIV_please_nomg(svl)) {
1194 bool auvok = SvUOK(svl);
1195 bool buvok = SvUOK(svr);
1196 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1197 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1206 const IV aiv = SvIVX(svl);
1209 auvok = TRUE; /* effectively it's a UV now */
1211 alow = -aiv; /* abs, auvok == false records sign */
1217 const IV biv = SvIVX(svr);
1220 buvok = TRUE; /* effectively it's a UV now */
1222 blow = -biv; /* abs, buvok == false records sign */
1226 /* If this does sign extension on unsigned it's time for plan B */
1227 ahigh = alow >> (4 * sizeof (UV));
1229 bhigh = blow >> (4 * sizeof (UV));
1231 if (ahigh && bhigh) {
1233 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1234 which is overflow. Drop to NVs below. */
1235 } else if (!ahigh && !bhigh) {
1236 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1237 so the unsigned multiply cannot overflow. */
1238 const UV product = alow * blow;
1239 if (auvok == buvok) {
1240 /* -ve * -ve or +ve * +ve gives a +ve result. */
1244 } else if (product <= (UV)IV_MIN) {
1245 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1246 /* -ve result, which could overflow an IV */
1248 SETi( -(IV)product );
1250 } /* else drop to NVs below. */
1252 /* One operand is large, 1 small */
1255 /* swap the operands */
1257 bhigh = blow; /* bhigh now the temp var for the swap */
1261 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1262 multiplies can't overflow. shift can, add can, -ve can. */
1263 product_middle = ahigh * blow;
1264 if (!(product_middle & topmask)) {
1265 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1267 product_middle <<= (4 * sizeof (UV));
1268 product_low = alow * blow;
1270 /* as for pp_add, UV + something mustn't get smaller.
1271 IIRC ANSI mandates this wrapping *behaviour* for
1272 unsigned whatever the actual representation*/
1273 product_low += product_middle;
1274 if (product_low >= product_middle) {
1275 /* didn't overflow */
1276 if (auvok == buvok) {
1277 /* -ve * -ve or +ve * +ve gives a +ve result. */
1279 SETu( product_low );
1281 } else if (product_low <= (UV)IV_MIN) {
1282 /* 2s complement assumption again */
1283 /* -ve result, which could overflow an IV */
1285 SETi( -(IV)product_low );
1287 } /* else drop to NVs below. */
1289 } /* product_middle too large */
1290 } /* ahigh && bhigh */
1295 NV right = SvNV_nomg(svr);
1296 NV left = SvNV_nomg(svl);
1298 SETn( left * right );
1305 dVAR; dSP; dATARGET; SV *svl, *svr;
1306 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1309 /* Only try to do UV divide first
1310 if ((SLOPPYDIVIDE is true) or
1311 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1313 The assumption is that it is better to use floating point divide
1314 whenever possible, only doing integer divide first if we can't be sure.
1315 If NV_PRESERVES_UV is true then we know at compile time that no UV
1316 can be too large to preserve, so don't need to compile the code to
1317 test the size of UVs. */
1320 # define PERL_TRY_UV_DIVIDE
1321 /* ensure that 20./5. == 4. */
1323 # ifdef PERL_PRESERVE_IVUV
1324 # ifndef NV_PRESERVES_UV
1325 # define PERL_TRY_UV_DIVIDE
1330 #ifdef PERL_TRY_UV_DIVIDE
1331 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1332 bool left_non_neg = SvUOK(svl);
1333 bool right_non_neg = SvUOK(svr);
1337 if (right_non_neg) {
1341 const IV biv = SvIVX(svr);
1344 right_non_neg = TRUE; /* effectively it's a UV now */
1350 /* historically undef()/0 gives a "Use of uninitialized value"
1351 warning before dieing, hence this test goes here.
1352 If it were immediately before the second SvIV_please, then
1353 DIE() would be invoked before left was even inspected, so
1354 no inspection would give no warning. */
1356 DIE(aTHX_ "Illegal division by zero");
1362 const IV aiv = SvIVX(svl);
1365 left_non_neg = TRUE; /* effectively it's a UV now */
1374 /* For sloppy divide we always attempt integer division. */
1376 /* Otherwise we only attempt it if either or both operands
1377 would not be preserved by an NV. If both fit in NVs
1378 we fall through to the NV divide code below. However,
1379 as left >= right to ensure integer result here, we know that
1380 we can skip the test on the right operand - right big
1381 enough not to be preserved can't get here unless left is
1384 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1387 /* Integer division can't overflow, but it can be imprecise. */
1388 const UV result = left / right;
1389 if (result * right == left) {
1390 SP--; /* result is valid */
1391 if (left_non_neg == right_non_neg) {
1392 /* signs identical, result is positive. */
1396 /* 2s complement assumption */
1397 if (result <= (UV)IV_MIN)
1398 SETi( -(IV)result );
1400 /* It's exact but too negative for IV. */
1401 SETn( -(NV)result );
1404 } /* tried integer divide but it was not an integer result */
1405 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1406 } /* one operand wasn't SvIOK */
1407 #endif /* PERL_TRY_UV_DIVIDE */
1409 NV right = SvNV_nomg(svr);
1410 NV left = SvNV_nomg(svl);
1411 (void)POPs;(void)POPs;
1412 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1413 if (! Perl_isnan(right) && right == 0.0)
1417 DIE(aTHX_ "Illegal division by zero");
1418 PUSHn( left / right );
1425 dVAR; dSP; dATARGET;
1426 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1430 bool left_neg = FALSE;
1431 bool right_neg = FALSE;
1432 bool use_double = FALSE;
1433 bool dright_valid = FALSE;
1436 SV * const svr = TOPs;
1437 SV * const svl = TOPm1s;
1438 if (SvIV_please_nomg(svr)) {
1439 right_neg = !SvUOK(svr);
1443 const IV biv = SvIVX(svr);
1446 right_neg = FALSE; /* effectively it's a UV now */
1453 dright = SvNV_nomg(svr);
1454 right_neg = dright < 0;
1457 if (dright < UV_MAX_P1) {
1458 right = U_V(dright);
1459 dright_valid = TRUE; /* In case we need to use double below. */
1465 /* At this point use_double is only true if right is out of range for
1466 a UV. In range NV has been rounded down to nearest UV and
1467 use_double false. */
1468 if (!use_double && SvIV_please_nomg(svl)) {
1469 left_neg = !SvUOK(svl);
1473 const IV aiv = SvIVX(svl);
1476 left_neg = FALSE; /* effectively it's a UV now */
1483 dleft = SvNV_nomg(svl);
1484 left_neg = dleft < 0;
1488 /* This should be exactly the 5.6 behaviour - if left and right are
1489 both in range for UV then use U_V() rather than floor. */
1491 if (dleft < UV_MAX_P1) {
1492 /* right was in range, so is dleft, so use UVs not double.
1496 /* left is out of range for UV, right was in range, so promote
1497 right (back) to double. */
1499 /* The +0.5 is used in 5.6 even though it is not strictly
1500 consistent with the implicit +0 floor in the U_V()
1501 inside the #if 1. */
1502 dleft = Perl_floor(dleft + 0.5);
1505 dright = Perl_floor(dright + 0.5);
1516 DIE(aTHX_ "Illegal modulus zero");
1518 dans = Perl_fmod(dleft, dright);
1519 if ((left_neg != right_neg) && dans)
1520 dans = dright - dans;
1523 sv_setnv(TARG, dans);
1529 DIE(aTHX_ "Illegal modulus zero");
1532 if ((left_neg != right_neg) && ans)
1535 /* XXX may warn: unary minus operator applied to unsigned type */
1536 /* could change -foo to be (~foo)+1 instead */
1537 if (ans <= ~((UV)IV_MAX)+1)
1538 sv_setiv(TARG, ~ans+1);
1540 sv_setnv(TARG, -(NV)ans);
1543 sv_setuv(TARG, ans);
1552 dVAR; dSP; dATARGET;
1556 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1557 /* TODO: think of some way of doing list-repeat overloading ??? */
1562 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1568 const UV uv = SvUV_nomg(sv);
1570 count = IV_MAX; /* The best we can do? */
1574 const IV iv = SvIV_nomg(sv);
1581 else if (SvNOKp(sv)) {
1582 const NV nv = SvNV_nomg(sv);
1589 count = SvIV_nomg(sv);
1591 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1593 static const char oom_list_extend[] = "Out of memory during list extend";
1594 const I32 items = SP - MARK;
1595 const I32 max = items * count;
1597 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1598 /* Did the max computation overflow? */
1599 if (items > 0 && max > 0 && (max < items || max < count))
1600 Perl_croak(aTHX_ oom_list_extend);
1605 /* This code was intended to fix 20010809.028:
1608 for (($x =~ /./g) x 2) {
1609 print chop; # "abcdabcd" expected as output.
1612 * but that change (#11635) broke this code:
1614 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1616 * I can't think of a better fix that doesn't introduce
1617 * an efficiency hit by copying the SVs. The stack isn't
1618 * refcounted, and mortalisation obviously doesn't
1619 * Do The Right Thing when the stack has more than
1620 * one pointer to the same mortal value.
1624 *SP = sv_2mortal(newSVsv(*SP));
1634 repeatcpy((char*)(MARK + items), (char*)MARK,
1635 items * sizeof(const SV *), count - 1);
1638 else if (count <= 0)
1641 else { /* Note: mark already snarfed by pp_list */
1642 SV * const tmpstr = POPs;
1645 static const char oom_string_extend[] =
1646 "Out of memory during string extend";
1649 sv_setsv_nomg(TARG, tmpstr);
1650 SvPV_force_nomg(TARG, len);
1651 isutf = DO_UTF8(TARG);
1656 const STRLEN max = (UV)count * len;
1657 if (len > MEM_SIZE_MAX / count)
1658 Perl_croak(aTHX_ oom_string_extend);
1659 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1660 SvGROW(TARG, max + 1);
1661 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1662 SvCUR_set(TARG, SvCUR(TARG) * count);
1664 *SvEND(TARG) = '\0';
1667 (void)SvPOK_only_UTF8(TARG);
1669 (void)SvPOK_only(TARG);
1671 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1672 /* The parser saw this as a list repeat, and there
1673 are probably several items on the stack. But we're
1674 in scalar context, and there's no pp_list to save us
1675 now. So drop the rest of the items -- robin@kitsite.com
1687 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1688 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1691 useleft = USE_LEFT(svl);
1692 #ifdef PERL_PRESERVE_IVUV
1693 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1694 "bad things" happen if you rely on signed integers wrapping. */
1695 if (SvIV_please_nomg(svr)) {
1696 /* Unless the left argument is integer in range we are going to have to
1697 use NV maths. Hence only attempt to coerce the right argument if
1698 we know the left is integer. */
1699 register UV auv = 0;
1705 a_valid = auvok = 1;
1706 /* left operand is undef, treat as zero. */
1708 /* Left operand is defined, so is it IV? */
1709 if (SvIV_please_nomg(svl)) {
1710 if ((auvok = SvUOK(svl)))
1713 register const IV aiv = SvIVX(svl);
1716 auvok = 1; /* Now acting as a sign flag. */
1717 } else { /* 2s complement assumption for IV_MIN */
1725 bool result_good = 0;
1728 bool buvok = SvUOK(svr);
1733 register const IV biv = SvIVX(svr);
1740 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1741 else "IV" now, independent of how it came in.
1742 if a, b represents positive, A, B negative, a maps to -A etc
1747 all UV maths. negate result if A negative.
1748 subtract if signs same, add if signs differ. */
1750 if (auvok ^ buvok) {
1759 /* Must get smaller */
1764 if (result <= buv) {
1765 /* result really should be -(auv-buv). as its negation
1766 of true value, need to swap our result flag */
1778 if (result <= (UV)IV_MIN)
1779 SETi( -(IV)result );
1781 /* result valid, but out of range for IV. */
1782 SETn( -(NV)result );
1786 } /* Overflow, drop through to NVs. */
1791 NV value = SvNV_nomg(svr);
1795 /* left operand is undef, treat as zero - value */
1799 SETn( SvNV_nomg(svl) - value );
1806 dVAR; dSP; dATARGET; SV *svl, *svr;
1807 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1811 const IV shift = SvIV_nomg(svr);
1812 if (PL_op->op_private & HINT_INTEGER) {
1813 const IV i = SvIV_nomg(svl);
1817 const UV u = SvUV_nomg(svl);
1826 dVAR; dSP; dATARGET; SV *svl, *svr;
1827 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1831 const IV shift = SvIV_nomg(svr);
1832 if (PL_op->op_private & HINT_INTEGER) {
1833 const IV i = SvIV_nomg(svl);
1837 const UV u = SvUV_nomg(svl);
1849 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1853 (SvIOK_notUV(left) && SvIOK_notUV(right))
1854 ? (SvIVX(left) < SvIVX(right))
1855 : (do_ncmp(left, right) == -1)
1865 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1869 (SvIOK_notUV(left) && SvIOK_notUV(right))
1870 ? (SvIVX(left) > SvIVX(right))
1871 : (do_ncmp(left, right) == 1)
1881 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1885 (SvIOK_notUV(left) && SvIOK_notUV(right))
1886 ? (SvIVX(left) <= SvIVX(right))
1887 : (do_ncmp(left, right) <= 0)
1897 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1901 (SvIOK_notUV(left) && SvIOK_notUV(right))
1902 ? (SvIVX(left) >= SvIVX(right))
1903 : ( (do_ncmp(left, right) & 2) == 0)
1913 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1917 (SvIOK_notUV(left) && SvIOK_notUV(right))
1918 ? (SvIVX(left) != SvIVX(right))
1919 : (do_ncmp(left, right) != 0)
1924 /* compare left and right SVs. Returns:
1928 * 2: left or right was a NaN
1931 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1935 PERL_ARGS_ASSERT_DO_NCMP;
1936 #ifdef PERL_PRESERVE_IVUV
1937 /* Fortunately it seems NaN isn't IOK */
1938 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1940 const IV leftiv = SvIVX(left);
1941 if (!SvUOK(right)) {
1942 /* ## IV <=> IV ## */
1943 const IV rightiv = SvIVX(right);
1944 return (leftiv > rightiv) - (leftiv < rightiv);
1946 /* ## IV <=> UV ## */
1948 /* As (b) is a UV, it's >=0, so it must be < */
1951 const UV rightuv = SvUVX(right);
1952 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
1957 /* ## UV <=> UV ## */
1958 const UV leftuv = SvUVX(left);
1959 const UV rightuv = SvUVX(right);
1960 return (leftuv > rightuv) - (leftuv < rightuv);
1962 /* ## UV <=> IV ## */
1964 const IV rightiv = SvIVX(right);
1966 /* As (a) is a UV, it's >=0, so it cannot be < */
1969 const UV leftuv = SvUVX(left);
1970 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
1973 assert(0); /* NOTREACHED */
1977 NV const rnv = SvNV_nomg(right);
1978 NV const lnv = SvNV_nomg(left);
1980 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1981 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
1984 return (lnv > rnv) - (lnv < rnv);
2003 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2006 value = do_ncmp(left, right);
2021 int amg_type = sle_amg;
2025 switch (PL_op->op_type) {
2044 tryAMAGICbin_MG(amg_type, AMGf_set);
2047 const int cmp = (IN_LOCALE_RUNTIME
2048 ? sv_cmp_locale_flags(left, right, 0)
2049 : sv_cmp_flags(left, right, 0));
2050 SETs(boolSV(cmp * multiplier < rhs));
2058 tryAMAGICbin_MG(seq_amg, AMGf_set);
2061 SETs(boolSV(sv_eq_flags(left, right, 0)));
2069 tryAMAGICbin_MG(sne_amg, AMGf_set);
2072 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2080 tryAMAGICbin_MG(scmp_amg, 0);
2083 const int cmp = (IN_LOCALE_RUNTIME
2084 ? sv_cmp_locale_flags(left, right, 0)
2085 : sv_cmp_flags(left, right, 0));
2093 dVAR; dSP; dATARGET;
2094 tryAMAGICbin_MG(band_amg, AMGf_assign);
2097 if (SvNIOKp(left) || SvNIOKp(right)) {
2098 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2099 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2100 if (PL_op->op_private & HINT_INTEGER) {
2101 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2105 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2108 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2109 if (right_ro_nonnum) SvNIOK_off(right);
2112 do_vop(PL_op->op_type, TARG, left, right);
2121 dVAR; dSP; dATARGET;
2122 const int op_type = PL_op->op_type;
2124 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2127 if (SvNIOKp(left) || SvNIOKp(right)) {
2128 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2129 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2130 if (PL_op->op_private & HINT_INTEGER) {
2131 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2132 const IV r = SvIV_nomg(right);
2133 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2137 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2138 const UV r = SvUV_nomg(right);
2139 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2142 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2143 if (right_ro_nonnum) SvNIOK_off(right);
2146 do_vop(op_type, TARG, left, right);
2156 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2158 SV * const sv = TOPs;
2160 if (SvIOK(sv) || (SvGMAGICAL(sv) && SvIOKp(sv))) {
2161 /* It's publicly an integer */
2164 if (SvIVX(sv) == IV_MIN) {
2165 /* 2s complement assumption. */
2166 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2169 else if (SvUVX(sv) <= IV_MAX) {
2174 else if (SvIVX(sv) != IV_MIN) {
2178 #ifdef PERL_PRESERVE_IVUV
2185 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2186 SETn(-SvNV_nomg(sv));
2187 else if (SvPOKp(sv)) {
2189 const char * const s = SvPV_nomg_const(sv, len);
2190 if (isIDFIRST(*s)) {
2191 sv_setpvs(TARG, "-");
2194 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2195 sv_setsv_nomg(TARG, sv);
2196 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2198 else if (SvIV_please_nomg(sv))
2199 goto oops_its_an_int;
2201 sv_setnv(TARG, -SvNV_nomg(sv));
2205 SETn(-SvNV_nomg(sv));
2213 tryAMAGICun_MG(not_amg, AMGf_set);
2214 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2221 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2225 if (PL_op->op_private & HINT_INTEGER) {
2226 const IV i = ~SvIV_nomg(sv);
2230 const UV u = ~SvUV_nomg(sv);
2239 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2240 sv_setsv_nomg(TARG, sv);
2241 tmps = (U8*)SvPV_force_nomg(TARG, len);
2244 /* Calculate exact length, let's not estimate. */
2249 U8 * const send = tmps + len;
2250 U8 * const origtmps = tmps;
2251 const UV utf8flags = UTF8_ALLOW_ANYUV;
2253 while (tmps < send) {
2254 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2256 targlen += UNISKIP(~c);
2262 /* Now rewind strings and write them. */
2269 Newx(result, targlen + 1, U8);
2271 while (tmps < send) {
2272 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2274 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2277 sv_usepvn_flags(TARG, (char*)result, targlen,
2278 SV_HAS_TRAILING_NUL);
2285 Newx(result, nchar + 1, U8);
2287 while (tmps < send) {
2288 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2293 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2301 register long *tmpl;
2302 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2305 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2310 for ( ; anum > 0; anum--, tmps++)
2318 /* integer versions of some of the above */
2322 dVAR; dSP; dATARGET;
2323 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2326 SETi( left * right );
2334 dVAR; dSP; dATARGET;
2335 tryAMAGICbin_MG(div_amg, AMGf_assign);
2338 IV value = SvIV_nomg(right);
2340 DIE(aTHX_ "Illegal division by zero");
2341 num = SvIV_nomg(left);
2343 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2347 value = num / value;
2353 #if defined(__GLIBC__) && IVSIZE == 8
2360 /* This is the vanilla old i_modulo. */
2361 dVAR; dSP; dATARGET;
2362 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2366 DIE(aTHX_ "Illegal modulus zero");
2367 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2371 SETi( left % right );
2376 #if defined(__GLIBC__) && IVSIZE == 8
2381 /* This is the i_modulo with the workaround for the _moddi3 bug
2382 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2383 * See below for pp_i_modulo. */
2384 dVAR; dSP; dATARGET;
2385 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2389 DIE(aTHX_ "Illegal modulus zero");
2390 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2394 SETi( left % PERL_ABS(right) );
2401 dVAR; dSP; dATARGET;
2402 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2406 DIE(aTHX_ "Illegal modulus zero");
2407 /* The assumption is to use hereafter the old vanilla version... */
2409 PL_ppaddr[OP_I_MODULO] =
2411 /* .. but if we have glibc, we might have a buggy _moddi3
2412 * (at least glicb 2.2.5 is known to have this bug), in other
2413 * words our integer modulus with negative quad as the second
2414 * argument might be broken. Test for this and re-patch the
2415 * opcode dispatch table if that is the case, remembering to
2416 * also apply the workaround so that this first round works
2417 * right, too. See [perl #9402] for more information. */
2421 /* Cannot do this check with inlined IV constants since
2422 * that seems to work correctly even with the buggy glibc. */
2424 /* Yikes, we have the bug.
2425 * Patch in the workaround version. */
2427 PL_ppaddr[OP_I_MODULO] =
2428 &Perl_pp_i_modulo_1;
2429 /* Make certain we work right this time, too. */
2430 right = PERL_ABS(right);
2433 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2437 SETi( left % right );
2445 dVAR; dSP; dATARGET;
2446 tryAMAGICbin_MG(add_amg, AMGf_assign);
2448 dPOPTOPiirl_ul_nomg;
2449 SETi( left + right );
2456 dVAR; dSP; dATARGET;
2457 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2459 dPOPTOPiirl_ul_nomg;
2460 SETi( left - right );
2468 tryAMAGICbin_MG(lt_amg, AMGf_set);
2471 SETs(boolSV(left < right));
2479 tryAMAGICbin_MG(gt_amg, AMGf_set);
2482 SETs(boolSV(left > right));
2490 tryAMAGICbin_MG(le_amg, AMGf_set);
2493 SETs(boolSV(left <= right));
2501 tryAMAGICbin_MG(ge_amg, AMGf_set);
2504 SETs(boolSV(left >= right));
2512 tryAMAGICbin_MG(eq_amg, AMGf_set);
2515 SETs(boolSV(left == right));
2523 tryAMAGICbin_MG(ne_amg, AMGf_set);
2526 SETs(boolSV(left != right));
2534 tryAMAGICbin_MG(ncmp_amg, 0);
2541 else if (left < right)
2553 tryAMAGICun_MG(neg_amg, 0);
2555 SV * const sv = TOPs;
2556 IV const i = SvIV_nomg(sv);
2562 /* High falutin' math. */
2567 tryAMAGICbin_MG(atan2_amg, 0);
2570 SETn(Perl_atan2(left, right));
2578 int amg_type = sin_amg;
2579 const char *neg_report = NULL;
2580 NV (*func)(NV) = Perl_sin;
2581 const int op_type = PL_op->op_type;
2598 amg_type = sqrt_amg;
2600 neg_report = "sqrt";
2605 tryAMAGICun_MG(amg_type, 0);
2607 SV * const arg = POPs;
2608 const NV value = SvNV_nomg(arg);
2610 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2611 SET_NUMERIC_STANDARD();
2612 /* diag_listed_as: Can't take log of %g */
2613 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2616 XPUSHn(func(value));
2621 /* Support Configure command-line overrides for rand() functions.
2622 After 5.005, perhaps we should replace this by Configure support
2623 for drand48(), random(), or rand(). For 5.005, though, maintain
2624 compatibility by calling rand() but allow the user to override it.
2625 See INSTALL for details. --Andy Dougherty 15 July 1998
2627 /* Now it's after 5.005, and Configure supports drand48() and random(),
2628 in addition to rand(). So the overrides should not be needed any more.
2629 --Jarkko Hietaniemi 27 September 1998
2632 #ifndef HAS_DRAND48_PROTO
2633 extern double drand48 (void);
2643 value = 1.0; (void)POPs;
2649 if (!PL_srand_called) {
2650 (void)seedDrand01((Rand_seed_t)seed());
2651 PL_srand_called = TRUE;
2663 if (MAXARG >= 1 && (TOPs || POPs)) {
2670 pv = SvPV(top, len);
2671 flags = grok_number(pv, len, &anum);
2673 if (!(flags & IS_NUMBER_IN_UV)) {
2674 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2675 "Integer overflow in srand");
2683 (void)seedDrand01((Rand_seed_t)anum);
2684 PL_srand_called = TRUE;
2688 /* Historically srand always returned true. We can avoid breaking
2690 sv_setpvs(TARG, "0 but true");
2699 tryAMAGICun_MG(int_amg, AMGf_numeric);
2701 SV * const sv = TOPs;
2702 const IV iv = SvIV_nomg(sv);
2703 /* XXX it's arguable that compiler casting to IV might be subtly
2704 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2705 else preferring IV has introduced a subtle behaviour change bug. OTOH
2706 relying on floating point to be accurate is a bug. */
2711 else if (SvIOK(sv)) {
2713 SETu(SvUV_nomg(sv));
2718 const NV value = SvNV_nomg(sv);
2720 if (value < (NV)UV_MAX + 0.5) {
2723 SETn(Perl_floor(value));
2727 if (value > (NV)IV_MIN - 0.5) {
2730 SETn(Perl_ceil(value));
2741 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2743 SV * const sv = TOPs;
2744 /* This will cache the NV value if string isn't actually integer */
2745 const IV iv = SvIV_nomg(sv);
2750 else if (SvIOK(sv)) {
2751 /* IVX is precise */
2753 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2761 /* 2s complement assumption. Also, not really needed as
2762 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2768 const NV value = SvNV_nomg(sv);
2782 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2786 SV* const sv = POPs;
2788 tmps = (SvPV_const(sv, len));
2790 /* If Unicode, try to downgrade
2791 * If not possible, croak. */
2792 SV* const tsv = sv_2mortal(newSVsv(sv));
2795 sv_utf8_downgrade(tsv, FALSE);
2796 tmps = SvPV_const(tsv, len);
2798 if (PL_op->op_type == OP_HEX)
2801 while (*tmps && len && isSPACE(*tmps))
2805 if (*tmps == 'x' || *tmps == 'X') {
2807 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2809 else if (*tmps == 'b' || *tmps == 'B')
2810 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2812 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2814 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2828 SV * const sv = TOPs;
2830 if (SvGAMAGIC(sv)) {
2831 /* For an overloaded or magic scalar, we can't know in advance if
2832 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2833 it likes to cache the length. Maybe that should be a documented
2838 = sv_2pv_flags(sv, &len,
2839 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2842 if (!SvPADTMP(TARG)) {
2843 sv_setsv(TARG, &PL_sv_undef);
2848 else if (DO_UTF8(sv)) {
2849 SETi(utf8_length((U8*)p, (U8*)p + len));
2853 } else if (SvOK(sv)) {
2854 /* Neither magic nor overloaded. */
2856 SETi(sv_len_utf8(sv));
2860 if (!SvPADTMP(TARG)) {
2861 sv_setsv_nomg(TARG, &PL_sv_undef);
2869 /* Returns false if substring is completely outside original string.
2870 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2871 always be true for an explicit 0.
2874 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2875 bool pos1_is_uv, IV len_iv,
2876 bool len_is_uv, STRLEN *posp,
2882 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2884 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2885 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2888 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2891 if (len_iv || len_is_uv) {
2892 if (!len_is_uv && len_iv < 0) {
2893 pos2_iv = curlen + len_iv;
2895 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2898 } else { /* len_iv >= 0 */
2899 if (!pos1_is_uv && pos1_iv < 0) {
2900 pos2_iv = pos1_iv + len_iv;
2901 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2903 if ((UV)len_iv > curlen-(UV)pos1_iv)
2906 pos2_iv = pos1_iv+len_iv;
2916 if (!pos2_is_uv && pos2_iv < 0) {
2917 if (!pos1_is_uv && pos1_iv < 0)
2921 else if (!pos1_is_uv && pos1_iv < 0)
2924 if ((UV)pos2_iv < (UV)pos1_iv)
2926 if ((UV)pos2_iv > curlen)
2929 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2930 *posp = (STRLEN)( (UV)pos1_iv );
2931 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2948 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2949 const bool rvalue = (GIMME_V != G_VOID);
2952 const char *repl = NULL;
2954 int num_args = PL_op->op_private & 7;
2955 bool repl_need_utf8_upgrade = FALSE;
2956 bool repl_is_utf8 = FALSE;
2960 if(!(repl_sv = POPs)) num_args--;
2962 if ((len_sv = POPs)) {
2963 len_iv = SvIV(len_sv);
2964 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
2969 pos1_iv = SvIV(pos_sv);
2970 pos1_is_uv = SvIOK_UV(pos_sv);
2972 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
2978 repl = SvPV_const(repl_sv, repl_len);
2979 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
2982 sv_utf8_upgrade(sv);
2984 else if (DO_UTF8(sv))
2985 repl_need_utf8_upgrade = TRUE;
2989 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
2990 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
2992 LvTARG(ret) = SvREFCNT_inc_simple(sv);
2994 pos1_is_uv || pos1_iv >= 0
2995 ? (STRLEN)(UV)pos1_iv
2996 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
2998 len_is_uv || len_iv > 0
2999 ? (STRLEN)(UV)len_iv
3000 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3003 PUSHs(ret); /* avoid SvSETMAGIC here */
3006 tmps = SvPV_const(sv, curlen);
3008 utf8_curlen = sv_len_utf8(sv);
3009 if (utf8_curlen == curlen)
3012 curlen = utf8_curlen;
3018 STRLEN pos, len, byte_len, byte_pos;
3020 if (!translate_substr_offsets(
3021 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3025 byte_pos = utf8_curlen
3026 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3031 SvTAINTED_off(TARG); /* decontaminate */
3032 SvUTF8_off(TARG); /* decontaminate */
3033 sv_setpvn(TARG, tmps, byte_len);
3034 #ifdef USE_LOCALE_COLLATE
3035 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3042 SV* repl_sv_copy = NULL;
3044 if (repl_need_utf8_upgrade) {
3045 repl_sv_copy = newSVsv(repl_sv);
3046 sv_utf8_upgrade(repl_sv_copy);
3047 repl = SvPV_const(repl_sv_copy, repl_len);
3048 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3051 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3052 "Attempt to use reference as lvalue in substr"
3056 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3059 SvREFCNT_dec(repl_sv_copy);
3071 Perl_croak(aTHX_ "substr outside of string");
3072 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3079 register const IV size = POPi;
3080 register const IV offset = POPi;
3081 register SV * const src = POPs;
3082 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3085 if (lvalue) { /* it's an lvalue! */
3086 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3087 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3089 LvTARG(ret) = SvREFCNT_inc_simple(src);
3090 LvTARGOFF(ret) = offset;
3091 LvTARGLEN(ret) = size;
3095 SvTAINTED_off(TARG); /* decontaminate */
3099 sv_setuv(ret, do_vecget(src, offset, size));
3115 const char *little_p;
3118 const bool is_index = PL_op->op_type == OP_INDEX;
3119 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3125 big_p = SvPV_const(big, biglen);
3126 little_p = SvPV_const(little, llen);
3128 big_utf8 = DO_UTF8(big);
3129 little_utf8 = DO_UTF8(little);
3130 if (big_utf8 ^ little_utf8) {
3131 /* One needs to be upgraded. */
3132 if (little_utf8 && !PL_encoding) {
3133 /* Well, maybe instead we might be able to downgrade the small
3135 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3138 /* If the large string is ISO-8859-1, and it's not possible to
3139 convert the small string to ISO-8859-1, then there is no
3140 way that it could be found anywhere by index. */
3145 /* At this point, pv is a malloc()ed string. So donate it to temp
3146 to ensure it will get free()d */
3147 little = temp = newSV(0);
3148 sv_usepvn(temp, pv, llen);
3149 little_p = SvPVX(little);
3152 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3155 sv_recode_to_utf8(temp, PL_encoding);
3157 sv_utf8_upgrade(temp);
3162 big_p = SvPV_const(big, biglen);
3165 little_p = SvPV_const(little, llen);
3169 if (SvGAMAGIC(big)) {
3170 /* Life just becomes a lot easier if I use a temporary here.
3171 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3172 will trigger magic and overloading again, as will fbm_instr()
3174 big = newSVpvn_flags(big_p, biglen,
3175 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3178 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3179 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3180 warn on undef, and we've already triggered a warning with the
3181 SvPV_const some lines above. We can't remove that, as we need to
3182 call some SvPV to trigger overloading early and find out if the
3184 This is all getting to messy. The API isn't quite clean enough,
3185 because data access has side effects.
3187 little = newSVpvn_flags(little_p, llen,
3188 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3189 little_p = SvPVX(little);
3193 offset = is_index ? 0 : biglen;
3195 if (big_utf8 && offset > 0)
3196 sv_pos_u2b(big, &offset, 0);
3202 else if (offset > (I32)biglen)
3204 if (!(little_p = is_index
3205 ? fbm_instr((unsigned char*)big_p + offset,
3206 (unsigned char*)big_p + biglen, little, 0)
3207 : rninstr(big_p, big_p + offset,
3208 little_p, little_p + llen)))
3211 retval = little_p - big_p;
3212 if (retval > 0 && big_utf8)
3213 sv_pos_b2u(big, &retval);
3223 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3224 SvTAINTED_off(TARG);
3225 do_sprintf(TARG, SP-MARK, MARK+1);
3226 TAINT_IF(SvTAINTED(TARG));
3238 const U8 *s = (U8*)SvPV_const(argsv, len);
3240 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3241 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3242 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3246 XPUSHu(DO_UTF8(argsv) ?
3247 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3259 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3261 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3263 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3266 Perl_ck_warner(aTHX_ packWARN(WARN_UTF8), "Invalid negative number (%"SVf") in chr", top);
3267 value = UNICODE_REPLACEMENT;
3273 SvUPGRADE(TARG,SVt_PV);
3275 if (value > 255 && !IN_BYTES) {
3276 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3277 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3278 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3280 (void)SvPOK_only(TARG);
3289 *tmps++ = (char)value;
3291 (void)SvPOK_only(TARG);
3293 if (PL_encoding && !IN_BYTES) {
3294 sv_recode_to_utf8(TARG, PL_encoding);
3296 if (SvCUR(TARG) == 0
3297 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3298 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3303 *tmps++ = (char)value;
3319 const char *tmps = SvPV_const(left, len);
3321 if (DO_UTF8(left)) {
3322 /* If Unicode, try to downgrade.
3323 * If not possible, croak.
3324 * Yes, we made this up. */
3325 SV* const tsv = sv_2mortal(newSVsv(left));
3328 sv_utf8_downgrade(tsv, FALSE);
3329 tmps = SvPV_const(tsv, len);
3331 # ifdef USE_ITHREADS
3333 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3334 /* This should be threadsafe because in ithreads there is only
3335 * one thread per interpreter. If this would not be true,
3336 * we would need a mutex to protect this malloc. */
3337 PL_reentrant_buffer->_crypt_struct_buffer =
3338 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3339 #if defined(__GLIBC__) || defined(__EMX__)
3340 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3341 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3342 /* work around glibc-2.2.5 bug */
3343 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3347 # endif /* HAS_CRYPT_R */
3348 # endif /* USE_ITHREADS */
3350 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3352 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3358 "The crypt() function is unimplemented due to excessive paranoia.");
3362 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3363 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3365 /* Generates code to store a unicode codepoint c that is known to occupy
3366 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3367 * and p is advanced to point to the next available byte after the two bytes */
3368 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3370 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3371 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3376 /* Actually is both lcfirst() and ucfirst(). Only the first character
3377 * changes. This means that possibly we can change in-place, ie., just
3378 * take the source and change that one character and store it back, but not
3379 * if read-only etc, or if the length changes */
3384 STRLEN slen; /* slen is the byte length of the whole SV. */
3387 bool inplace; /* ? Convert first char only, in-place */
3388 bool doing_utf8 = FALSE; /* ? using utf8 */
3389 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3390 const int op_type = PL_op->op_type;
3393 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3394 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3395 * stored as UTF-8 at s. */
3396 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3397 * lowercased) character stored in tmpbuf. May be either
3398 * UTF-8 or not, but in either case is the number of bytes */
3399 bool tainted = FALSE;
3403 s = (const U8*)SvPV_nomg_const(source, slen);
3405 if (ckWARN(WARN_UNINITIALIZED))
3406 report_uninit(source);
3411 /* We may be able to get away with changing only the first character, in
3412 * place, but not if read-only, etc. Later we may discover more reasons to
3413 * not convert in-place. */
3414 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3416 /* First calculate what the changed first character should be. This affects
3417 * whether we can just swap it out, leaving the rest of the string unchanged,
3418 * or even if have to convert the dest to UTF-8 when the source isn't */
3420 if (! slen) { /* If empty */
3421 need = 1; /* still need a trailing NUL */
3424 else if (DO_UTF8(source)) { /* Is the source utf8? */
3427 if (op_type == OP_UCFIRST) {
3428 _to_utf8_title_flags(s, tmpbuf, &tculen,
3429 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3432 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3433 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3436 /* we can't do in-place if the length changes. */
3437 if (ulen != tculen) inplace = FALSE;
3438 need = slen + 1 - ulen + tculen;
3440 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3441 * latin1 is treated as caseless. Note that a locale takes
3443 ulen = 1; /* Original character is 1 byte */
3444 tculen = 1; /* Most characters will require one byte, but this will
3445 * need to be overridden for the tricky ones */
3448 if (op_type == OP_LCFIRST) {
3450 /* lower case the first letter: no trickiness for any character */
3451 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3452 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3455 else if (IN_LOCALE_RUNTIME) {
3456 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3457 * have upper and title case different
3460 else if (! IN_UNI_8_BIT) {
3461 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3462 * on EBCDIC machines whatever the
3463 * native function does */
3465 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3466 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3468 assert(tculen == 2);
3470 /* If the result is an upper Latin1-range character, it can
3471 * still be represented in one byte, which is its ordinal */
3472 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3473 *tmpbuf = (U8) title_ord;
3477 /* Otherwise it became more than one ASCII character (in
3478 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3479 * beyond Latin1, so the number of bytes changed, so can't
3480 * replace just the first character in place. */
3483 /* If the result won't fit in a byte, the entire result will
3484 * have to be in UTF-8. Assume worst case sizing in
3485 * conversion. (all latin1 characters occupy at most two bytes
3487 if (title_ord > 255) {
3489 convert_source_to_utf8 = TRUE;
3490 need = slen * 2 + 1;
3492 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3493 * (both) characters whose title case is above 255 is
3497 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3498 need = slen + 1 + 1;
3502 } /* End of use Unicode (Latin1) semantics */
3503 } /* End of changing the case of the first character */
3505 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3506 * generate the result */
3509 /* We can convert in place. This means we change just the first
3510 * character without disturbing the rest; no need to grow */
3512 s = d = (U8*)SvPV_force_nomg(source, slen);
3518 /* Here, we can't convert in place; we earlier calculated how much
3519 * space we will need, so grow to accommodate that */
3520 SvUPGRADE(dest, SVt_PV);
3521 d = (U8*)SvGROW(dest, need);
3522 (void)SvPOK_only(dest);
3529 if (! convert_source_to_utf8) {
3531 /* Here both source and dest are in UTF-8, but have to create
3532 * the entire output. We initialize the result to be the
3533 * title/lower cased first character, and then append the rest
3535 sv_setpvn(dest, (char*)tmpbuf, tculen);
3537 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3541 const U8 *const send = s + slen;
3543 /* Here the dest needs to be in UTF-8, but the source isn't,
3544 * except we earlier UTF-8'd the first character of the source
3545 * into tmpbuf. First put that into dest, and then append the
3546 * rest of the source, converting it to UTF-8 as we go. */
3548 /* Assert tculen is 2 here because the only two characters that
3549 * get to this part of the code have 2-byte UTF-8 equivalents */
3551 *d++ = *(tmpbuf + 1);
3552 s++; /* We have just processed the 1st char */
3554 for (; s < send; s++) {
3555 d = uvchr_to_utf8(d, *s);
3558 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3562 else { /* in-place UTF-8. Just overwrite the first character */
3563 Copy(tmpbuf, d, tculen, U8);
3564 SvCUR_set(dest, need - 1);
3572 else { /* Neither source nor dest are in or need to be UTF-8 */
3574 if (IN_LOCALE_RUNTIME) {
3578 if (inplace) { /* in-place, only need to change the 1st char */
3581 else { /* Not in-place */
3583 /* Copy the case-changed character(s) from tmpbuf */
3584 Copy(tmpbuf, d, tculen, U8);
3585 d += tculen - 1; /* Code below expects d to point to final
3586 * character stored */
3589 else { /* empty source */
3590 /* See bug #39028: Don't taint if empty */
3594 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3595 * the destination to retain that flag */
3599 if (!inplace) { /* Finish the rest of the string, unchanged */
3600 /* This will copy the trailing NUL */
3601 Copy(s + 1, d + 1, slen, U8);
3602 SvCUR_set(dest, need - 1);
3605 if (dest != source && SvTAINTED(source))
3611 /* There's so much setup/teardown code common between uc and lc, I wonder if
3612 it would be worth merging the two, and just having a switch outside each
3613 of the three tight loops. There is less and less commonality though */
3627 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3628 && SvTEMP(source) && !DO_UTF8(source)
3629 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3631 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3632 * make the loop tight, so we overwrite the source with the dest before
3633 * looking at it, and we need to look at the original source
3634 * afterwards. There would also need to be code added to handle
3635 * switching to not in-place in midstream if we run into characters
3636 * that change the length.
3639 s = d = (U8*)SvPV_force_nomg(source, len);
3646 /* The old implementation would copy source into TARG at this point.
3647 This had the side effect that if source was undef, TARG was now
3648 an undefined SV with PADTMP set, and they don't warn inside
3649 sv_2pv_flags(). However, we're now getting the PV direct from
3650 source, which doesn't have PADTMP set, so it would warn. Hence the
3654 s = (const U8*)SvPV_nomg_const(source, len);
3656 if (ckWARN(WARN_UNINITIALIZED))
3657 report_uninit(source);
3663 SvUPGRADE(dest, SVt_PV);
3664 d = (U8*)SvGROW(dest, min);
3665 (void)SvPOK_only(dest);
3670 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3671 to check DO_UTF8 again here. */
3673 if (DO_UTF8(source)) {
3674 const U8 *const send = s + len;
3675 U8 tmpbuf[UTF8_MAXBYTES+1];
3676 bool tainted = FALSE;
3678 /* All occurrences of these are to be moved to follow any other marks.
3679 * This is context-dependent. We may not be passed enough context to
3680 * move the iota subscript beyond all of them, but we do the best we can
3681 * with what we're given. The result is always better than if we
3682 * hadn't done this. And, the problem would only arise if we are
3683 * passed a character without all its combining marks, which would be
3684 * the caller's mistake. The information this is based on comes from a
3685 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3686 * itself) and so can't be checked properly to see if it ever gets
3687 * revised. But the likelihood of it changing is remote */
3688 bool in_iota_subscript = FALSE;
3694 if (in_iota_subscript && ! is_utf8_mark(s)) {
3696 /* A non-mark. Time to output the iota subscript */
3697 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3698 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3700 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3701 in_iota_subscript = FALSE;
3704 /* Then handle the current character. Get the changed case value
3705 * and copy it to the output buffer */
3708 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3709 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3710 if (uv == GREEK_CAPITAL_LETTER_IOTA
3711 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3713 in_iota_subscript = TRUE;
3716 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3717 /* If the eventually required minimum size outgrows the
3718 * available space, we need to grow. */
3719 const UV o = d - (U8*)SvPVX_const(dest);
3721 /* If someone uppercases one million U+03B0s we SvGROW()
3722 * one million times. Or we could try guessing how much to
3723 * allocate without allocating too much. Such is life.
3724 * See corresponding comment in lc code for another option
3727 d = (U8*)SvPVX(dest) + o;
3729 Copy(tmpbuf, d, ulen, U8);
3734 if (in_iota_subscript) {
3735 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3740 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3746 else { /* Not UTF-8 */
3748 const U8 *const send = s + len;
3750 /* Use locale casing if in locale; regular style if not treating
3751 * latin1 as having case; otherwise the latin1 casing. Do the
3752 * whole thing in a tight loop, for speed, */
3753 if (IN_LOCALE_RUNTIME) {
3756 for (; s < send; d++, s++)
3757 *d = toUPPER_LC(*s);
3759 else if (! IN_UNI_8_BIT) {
3760 for (; s < send; d++, s++) {
3765 for (; s < send; d++, s++) {
3766 *d = toUPPER_LATIN1_MOD(*s);
3767 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3769 /* The mainstream case is the tight loop above. To avoid
3770 * extra tests in that, all three characters that require
3771 * special handling are mapped by the MOD to the one tested
3773 * Use the source to distinguish between the three cases */
3775 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3777 /* uc() of this requires 2 characters, but they are
3778 * ASCII. If not enough room, grow the string */
3779 if (SvLEN(dest) < ++min) {
3780 const UV o = d - (U8*)SvPVX_const(dest);
3782 d = (U8*)SvPVX(dest) + o;
3784 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3785 continue; /* Back to the tight loop; still in ASCII */
3788 /* The other two special handling characters have their
3789 * upper cases outside the latin1 range, hence need to be
3790 * in UTF-8, so the whole result needs to be in UTF-8. So,
3791 * here we are somewhere in the middle of processing a
3792 * non-UTF-8 string, and realize that we will have to convert
3793 * the whole thing to UTF-8. What to do? There are
3794 * several possibilities. The simplest to code is to
3795 * convert what we have so far, set a flag, and continue on
3796 * in the loop. The flag would be tested each time through
3797 * the loop, and if set, the next character would be
3798 * converted to UTF-8 and stored. But, I (khw) didn't want
3799 * to slow down the mainstream case at all for this fairly
3800 * rare case, so I didn't want to add a test that didn't
3801 * absolutely have to be there in the loop, besides the
3802 * possibility that it would get too complicated for
3803 * optimizers to deal with. Another possibility is to just
3804 * give up, convert the source to UTF-8, and restart the
3805 * function that way. Another possibility is to convert
3806 * both what has already been processed and what is yet to
3807 * come separately to UTF-8, then jump into the loop that
3808 * handles UTF-8. But the most efficient time-wise of the
3809 * ones I could think of is what follows, and turned out to
3810 * not require much extra code. */
3812 /* Convert what we have so far into UTF-8, telling the
3813 * function that we know it should be converted, and to
3814 * allow extra space for what we haven't processed yet.
3815 * Assume the worst case space requirements for converting
3816 * what we haven't processed so far: that it will require
3817 * two bytes for each remaining source character, plus the
3818 * NUL at the end. This may cause the string pointer to
3819 * move, so re-find it. */
3821 len = d - (U8*)SvPVX_const(dest);
3822 SvCUR_set(dest, len);
3823 len = sv_utf8_upgrade_flags_grow(dest,
3824 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3826 d = (U8*)SvPVX(dest) + len;
3828 /* Now process the remainder of the source, converting to
3829 * upper and UTF-8. If a resulting byte is invariant in
3830 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3831 * append it to the output. */
3832 for (; s < send; s++) {
3833 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3837 /* Here have processed the whole source; no need to continue
3838 * with the outer loop. Each character has been converted
3839 * to upper case and converted to UTF-8 */
3842 } /* End of processing all latin1-style chars */
3843 } /* End of processing all chars */
3844 } /* End of source is not empty */
3846 if (source != dest) {
3847 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3848 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3850 } /* End of isn't utf8 */
3851 if (dest != source && SvTAINTED(source))
3870 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3871 && SvTEMP(source) && !DO_UTF8(source)) {
3873 /* We can convert in place, as lowercasing anything in the latin1 range
3874 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3876 s = d = (U8*)SvPV_force_nomg(source, len);
3883 /* The old implementation would copy source into TARG at this point.
3884 This had the side effect that if source was undef, TARG was now
3885 an undefined SV with PADTMP set, and they don't warn inside
3886 sv_2pv_flags(). However, we're now getting the PV direct from
3887 source, which doesn't have PADTMP set, so it would warn. Hence the
3891 s = (const U8*)SvPV_nomg_const(source, len);
3893 if (ckWARN(WARN_UNINITIALIZED))
3894 report_uninit(source);
3900 SvUPGRADE(dest, SVt_PV);
3901 d = (U8*)SvGROW(dest, min);
3902 (void)SvPOK_only(dest);
3907 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3908 to check DO_UTF8 again here. */
3910 if (DO_UTF8(source)) {
3911 const U8 *const send = s + len;
3912 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3913 bool tainted = FALSE;
3916 const STRLEN u = UTF8SKIP(s);
3919 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3920 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3922 /* Here is where we would do context-sensitive actions. See the
3923 * commit message for this comment for why there isn't any */
3925 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3927 /* If the eventually required minimum size outgrows the
3928 * available space, we need to grow. */
3929 const UV o = d - (U8*)SvPVX_const(dest);
3931 /* If someone lowercases one million U+0130s we SvGROW() one
3932 * million times. Or we could try guessing how much to
3933 * allocate without allocating too much. Such is life.
3934 * Another option would be to grow an extra byte or two more
3935 * each time we need to grow, which would cut down the million
3936 * to 500K, with little waste */
3938 d = (U8*)SvPVX(dest) + o;
3941 /* Copy the newly lowercased letter to the output buffer we're
3943 Copy(tmpbuf, d, ulen, U8);
3946 } /* End of looping through the source string */
3949 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3954 } else { /* Not utf8 */
3956 const U8 *const send = s + len;
3958 /* Use locale casing if in locale; regular style if not treating
3959 * latin1 as having case; otherwise the latin1 casing. Do the
3960 * whole thing in a tight loop, for speed, */
3961 if (IN_LOCALE_RUNTIME) {
3964 for (; s < send; d++, s++)
3965 *d = toLOWER_LC(*s);
3967 else if (! IN_UNI_8_BIT) {
3968 for (; s < send; d++, s++) {
3973 for (; s < send; d++, s++) {
3974 *d = toLOWER_LATIN1(*s);
3978 if (source != dest) {
3980 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3983 if (dest != source && SvTAINTED(source))
3992 SV * const sv = TOPs;
3994 register const char *s = SvPV_const(sv,len);
3996 SvUTF8_off(TARG); /* decontaminate */
3999 SvUPGRADE(TARG, SVt_PV);
4000 SvGROW(TARG, (len * 2) + 1);
4004 STRLEN ulen = UTF8SKIP(s);
4005 bool to_quote = FALSE;
4007 if (UTF8_IS_INVARIANT(*s)) {
4008 if (_isQUOTEMETA(*s)) {
4012 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4014 /* In locale, we quote all non-ASCII Latin1 chars.
4015 * Otherwise use the quoting rules */
4016 if (IN_LOCALE_RUNTIME
4017 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4022 else if (_is_utf8_quotemeta((U8 *) s)) {
4037 else if (IN_UNI_8_BIT) {
4039 if (_isQUOTEMETA(*s))
4045 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4046 * including everything above ASCII */
4048 if (!isWORDCHAR_A(*s))
4054 SvCUR_set(TARG, d - SvPVX_const(TARG));
4055 (void)SvPOK_only_UTF8(TARG);
4058 sv_setpvn(TARG, s, len);
4075 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4076 const bool full_folding = TRUE;
4077 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4078 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4080 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4081 * You are welcome(?) -Hugmeir
4089 s = (const U8*)SvPV_nomg_const(source, len);
4091 if (ckWARN(WARN_UNINITIALIZED))
4092 report_uninit(source);
4099 SvUPGRADE(dest, SVt_PV);
4100 d = (U8*)SvGROW(dest, min);
4101 (void)SvPOK_only(dest);
4106 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4107 bool tainted = FALSE;
4109 const STRLEN u = UTF8SKIP(s);
4112 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4114 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4115 const UV o = d - (U8*)SvPVX_const(dest);
4117 d = (U8*)SvPVX(dest) + o;
4120 Copy(tmpbuf, d, ulen, U8);
4129 } /* Unflagged string */
4131 /* For locale, bytes, and nothing, the behavior is supposed to be the
4134 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4137 for (; s < send; d++, s++)
4138 *d = toLOWER_LC(*s);
4140 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4141 for (; s < send; d++, s++)
4145 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4146 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4147 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4148 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4151 for (; s < send; d++, s++) {
4152 if (*s == MICRO_SIGN) {
4153 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4154 * is outside of the latin-1 range. There's a couple of ways to
4155 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4156 * What we do here is upgrade what we had already casefolded,
4157 * then enter an inner loop that appends the rest of the characters
4160 len = d - (U8*)SvPVX_const(dest);
4161 SvCUR_set(dest, len);
4162 len = sv_utf8_upgrade_flags_grow(dest,
4163 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4164 /* The max expansion for latin1
4165 * chars is 1 byte becomes 2 */
4167 d = (U8*)SvPVX(dest) + len;
4169 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4171 for (; s < send; s++) {
4173 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4174 if UNI_IS_INVARIANT(fc) {
4175 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4183 Copy(tmpbuf, d, ulen, U8);
4189 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4190 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4191 * which may require growing the SV.
4193 if (SvLEN(dest) < ++min) {
4194 const UV o = d - (U8*)SvPVX_const(dest);
4196 d = (U8*)SvPVX(dest) + o;
4201 else { /* If it's not one of those two, the fold is their lower case */
4202 *d = toLOWER_LATIN1(*s);
4208 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4210 if (SvTAINTED(source))
4220 dVAR; dSP; dMARK; dORIGMARK;
4221 register AV *const av = MUTABLE_AV(POPs);
4222 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4224 if (SvTYPE(av) == SVt_PVAV) {
4225 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4226 bool can_preserve = FALSE;
4232 can_preserve = SvCANEXISTDELETE(av);
4235 if (lval && localizing) {
4238 for (svp = MARK + 1; svp <= SP; svp++) {
4239 const I32 elem = SvIV(*svp);
4243 if (max > AvMAX(av))
4247 while (++MARK <= SP) {
4249 I32 elem = SvIV(*MARK);
4250 bool preeminent = TRUE;
4252 if (localizing && can_preserve) {
4253 /* If we can determine whether the element exist,
4254 * Try to preserve the existenceness of a tied array
4255 * element by using EXISTS and DELETE if possible.
4256 * Fallback to FETCH and STORE otherwise. */
4257 preeminent = av_exists(av, elem);
4260 svp = av_fetch(av, elem, lval);
4262 if (!svp || *svp == &PL_sv_undef)
4263 DIE(aTHX_ PL_no_aelem, elem);
4266 save_aelem(av, elem, svp);
4268 SAVEADELETE(av, elem);
4271 *MARK = svp ? *svp : &PL_sv_undef;
4274 if (GIMME != G_ARRAY) {
4276 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4282 /* Smart dereferencing for keys, values and each */
4294 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4299 "Type of argument to %s must be unblessed hashref or arrayref",
4300 PL_op_desc[PL_op->op_type] );
4303 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4305 "Can't modify %s in %s",
4306 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4309 /* Delegate to correct function for op type */
4311 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4312 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4315 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4323 AV *array = MUTABLE_AV(POPs);
4324 const I32 gimme = GIMME_V;
4325 IV *iterp = Perl_av_iter_p(aTHX_ array);
4326 const IV current = (*iterp)++;
4328 if (current > av_len(array)) {
4330 if (gimme == G_SCALAR)
4338 if (gimme == G_ARRAY) {
4339 SV **const element = av_fetch(array, current, 0);
4340 PUSHs(element ? *element : &PL_sv_undef);
4349 AV *array = MUTABLE_AV(POPs);
4350 const I32 gimme = GIMME_V;
4352 *Perl_av_iter_p(aTHX_ array) = 0;
4354 if (gimme == G_SCALAR) {
4356 PUSHi(av_len(array) + 1);
4358 else if (gimme == G_ARRAY) {
4359 IV n = Perl_av_len(aTHX_ array);
4364 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4365 for (i = 0; i <= n; i++) {
4370 for (i = 0; i <= n; i++) {
4371 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4372 PUSHs(elem ? *elem : &PL_sv_undef);
4379 /* Associative arrays. */
4385 HV * hash = MUTABLE_HV(POPs);
4387 const I32 gimme = GIMME_V;
4390 /* might clobber stack_sp */
4391 entry = hv_iternext(hash);
4396 SV* const sv = hv_iterkeysv(entry);
4397 PUSHs(sv); /* won't clobber stack_sp */
4398 if (gimme == G_ARRAY) {
4401 /* might clobber stack_sp */
4402 val = hv_iterval(hash, entry);
4407 else if (gimme == G_SCALAR)
4414 S_do_delete_local(pTHX)
4418 const I32 gimme = GIMME_V;
4422 if (PL_op->op_private & OPpSLICE) {
4424 SV * const osv = POPs;
4425 const bool tied = SvRMAGICAL(osv)
4426 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4427 const bool can_preserve = SvCANEXISTDELETE(osv);
4428 const U32 type = SvTYPE(osv);
4429 if (type == SVt_PVHV) { /* hash element */
4430 HV * const hv = MUTABLE_HV(osv);
4431 while (++MARK <= SP) {
4432 SV * const keysv = *MARK;
4434 bool preeminent = TRUE;
4436 preeminent = hv_exists_ent(hv, keysv, 0);
4438 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4445 sv = hv_delete_ent(hv, keysv, 0, 0);
4446 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4449 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4451 *MARK = sv_mortalcopy(sv);
4457 SAVEHDELETE(hv, keysv);
4458 *MARK = &PL_sv_undef;
4462 else if (type == SVt_PVAV) { /* array element */
4463 if (PL_op->op_flags & OPf_SPECIAL) {
4464 AV * const av = MUTABLE_AV(osv);
4465 while (++MARK <= SP) {
4466 I32 idx = SvIV(*MARK);
4468 bool preeminent = TRUE;
4470 preeminent = av_exists(av, idx);
4472 SV **svp = av_fetch(av, idx, 1);
4479 sv = av_delete(av, idx, 0);
4480 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4483 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4485 *MARK = sv_mortalcopy(sv);
4491 SAVEADELETE(av, idx);
4492 *MARK = &PL_sv_undef;
4498 DIE(aTHX_ "Not a HASH reference");
4499 if (gimme == G_VOID)
4501 else if (gimme == G_SCALAR) {
4506 *++MARK = &PL_sv_undef;
4511 SV * const keysv = POPs;
4512 SV * const osv = POPs;
4513 const bool tied = SvRMAGICAL(osv)
4514 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4515 const bool can_preserve = SvCANEXISTDELETE(osv);
4516 const U32 type = SvTYPE(osv);
4518 if (type == SVt_PVHV) {
4519 HV * const hv = MUTABLE_HV(osv);
4520 bool preeminent = TRUE;
4522 preeminent = hv_exists_ent(hv, keysv, 0);
4524 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4531 sv = hv_delete_ent(hv, keysv, 0, 0);
4532 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4535 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4537 SV *nsv = sv_mortalcopy(sv);
4543 SAVEHDELETE(hv, keysv);
4545 else if (type == SVt_PVAV) {
4546 if (PL_op->op_flags & OPf_SPECIAL) {
4547 AV * const av = MUTABLE_AV(osv);
4548 I32 idx = SvIV(keysv);
4549 bool preeminent = TRUE;
4551 preeminent = av_exists(av, idx);
4553 SV **svp = av_fetch(av, idx, 1);
4560 sv = av_delete(av, idx, 0);
4561 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4564 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4566 SV *nsv = sv_mortalcopy(sv);
4572 SAVEADELETE(av, idx);
4575 DIE(aTHX_ "panic: avhv_delete no longer supported");
4578 DIE(aTHX_ "Not a HASH reference");
4581 if (gimme != G_VOID)
4595 if (PL_op->op_private & OPpLVAL_INTRO)
4596 return do_delete_local();
4599 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4601 if (PL_op->op_private & OPpSLICE) {
4603 HV * const hv = MUTABLE_HV(POPs);
4604 const U32 hvtype = SvTYPE(hv);
4605 if (hvtype == SVt_PVHV) { /* hash element */
4606 while (++MARK <= SP) {
4607 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4608 *MARK = sv ? sv : &PL_sv_undef;
4611 else if (hvtype == SVt_PVAV) { /* array element */
4612 if (PL_op->op_flags & OPf_SPECIAL) {
4613 while (++MARK <= SP) {
4614 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4615 *MARK = sv ? sv : &PL_sv_undef;
4620 DIE(aTHX_ "Not a HASH reference");
4623 else if (gimme == G_SCALAR) {
4628 *++MARK = &PL_sv_undef;
4634 HV * const hv = MUTABLE_HV(POPs);
4636 if (SvTYPE(hv) == SVt_PVHV)
4637 sv = hv_delete_ent(hv, keysv, discard, 0);
4638 else if (SvTYPE(hv) == SVt_PVAV) {
4639 if (PL_op->op_flags & OPf_SPECIAL)
4640 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4642 DIE(aTHX_ "panic: avhv_delete no longer supported");
4645 DIE(aTHX_ "Not a HASH reference");
4661 if (PL_op->op_private & OPpEXISTS_SUB) {
4663 SV * const sv = POPs;
4664 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4667 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4672 hv = MUTABLE_HV(POPs);
4673 if (SvTYPE(hv) == SVt_PVHV) {
4674 if (hv_exists_ent(hv, tmpsv, 0))
4677 else if (SvTYPE(hv) == SVt_PVAV) {
4678 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4679 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4684 DIE(aTHX_ "Not a HASH reference");
4691 dVAR; dSP; dMARK; dORIGMARK;
4692 register HV * const hv = MUTABLE_HV(POPs);
4693 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4694 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4695 bool can_preserve = FALSE;
4701 if (SvCANEXISTDELETE(hv))
4702 can_preserve = TRUE;
4705 while (++MARK <= SP) {
4706 SV * const keysv = *MARK;
4709 bool preeminent = TRUE;
4711 if (localizing && can_preserve) {
4712 /* If we can determine whether the element exist,
4713 * try to preserve the existenceness of a tied hash
4714 * element by using EXISTS and DELETE if possible.
4715 * Fallback to FETCH and STORE otherwise. */
4716 preeminent = hv_exists_ent(hv, keysv, 0);
4719 he = hv_fetch_ent(hv, keysv, lval, 0);
4720 svp = he ? &HeVAL(he) : NULL;
4723 if (!svp || !*svp || *svp == &PL_sv_undef) {
4724 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4727 if (HvNAME_get(hv) && isGV(*svp))
4728 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4729 else if (preeminent)
4730 save_helem_flags(hv, keysv, svp,
4731 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4733 SAVEHDELETE(hv, keysv);
4736 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4738 if (GIMME != G_ARRAY) {
4740 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4746 /* List operators. */
4751 if (GIMME != G_ARRAY) {
4753 *MARK = *SP; /* unwanted list, return last item */
4755 *MARK = &PL_sv_undef;
4765 SV ** const lastrelem = PL_stack_sp;
4766 SV ** const lastlelem = PL_stack_base + POPMARK;
4767 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4768 register SV ** const firstrelem = lastlelem + 1;
4769 I32 is_something_there = FALSE;
4771 register const I32 max = lastrelem - lastlelem;
4772 register SV **lelem;
4774 if (GIMME != G_ARRAY) {
4775 I32 ix = SvIV(*lastlelem);
4778 if (ix < 0 || ix >= max)
4779 *firstlelem = &PL_sv_undef;
4781 *firstlelem = firstrelem[ix];
4787 SP = firstlelem - 1;
4791 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4792 I32 ix = SvIV(*lelem);
4795 if (ix < 0 || ix >= max)
4796 *lelem = &PL_sv_undef;
4798 is_something_there = TRUE;
4799 if (!(*lelem = firstrelem[ix]))
4800 *lelem = &PL_sv_undef;
4803 if (is_something_there)
4806 SP = firstlelem - 1;
4812 dVAR; dSP; dMARK; dORIGMARK;
4813 const I32 items = SP - MARK;
4814 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4815 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4816 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4817 ? newRV_noinc(av) : av);
4823 dVAR; dSP; dMARK; dORIGMARK;
4824 HV* const hv = newHV();
4827 SV * const key = *++MARK;
4828 SV * const val = newSV(0);
4830 sv_setsv(val, *++MARK);
4832 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4833 (void)hv_store_ent(hv,key,val,0);
4836 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4837 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4842 S_deref_plain_array(pTHX_ AV *ary)
4844 if (SvTYPE(ary) == SVt_PVAV) return ary;
4845 SvGETMAGIC((SV *)ary);
4846 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4847 Perl_die(aTHX_ "Not an ARRAY reference");
4848 else if (SvOBJECT(SvRV(ary)))
4849 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4850 return (AV *)SvRV(ary);
4853 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4854 # define DEREF_PLAIN_ARRAY(ary) \
4857 SvTYPE(aRrRay) == SVt_PVAV \
4859 : S_deref_plain_array(aTHX_ aRrRay); \
4862 # define DEREF_PLAIN_ARRAY(ary) \
4864 PL_Sv = (SV *)(ary), \
4865 SvTYPE(PL_Sv) == SVt_PVAV \
4867 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4873 dVAR; dSP; dMARK; dORIGMARK;
4874 int num_args = (SP - MARK);
4875 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4879 register I32 offset;
4880 register I32 length;
4884 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4887 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4888 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4895 offset = i = SvIV(*MARK);
4897 offset += AvFILLp(ary) + 1;
4899 DIE(aTHX_ PL_no_aelem, i);
4901 length = SvIVx(*MARK++);
4903 length += AvFILLp(ary) - offset + 1;
4909 length = AvMAX(ary) + 1; /* close enough to infinity */
4913 length = AvMAX(ary) + 1;
4915 if (offset > AvFILLp(ary) + 1) {
4917 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4918 offset = AvFILLp(ary) + 1;
4920 after = AvFILLp(ary) + 1 - (offset + length);
4921 if (after < 0) { /* not that much array */
4922 length += after; /* offset+length now in array */
4928 /* At this point, MARK .. SP-1 is our new LIST */
4931 diff = newlen - length;
4932 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4935 /* make new elements SVs now: avoid problems if they're from the array */
4936 for (dst = MARK, i = newlen; i; i--) {
4937 SV * const h = *dst;
4938 *dst++ = newSVsv(h);
4941 if (diff < 0) { /* shrinking the area */
4942 SV **tmparyval = NULL;
4944 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4945 Copy(MARK, tmparyval, newlen, SV*);
4948 MARK = ORIGMARK + 1;
4949 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4950 MEXTEND(MARK, length);
4951 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4953 EXTEND_MORTAL(length);
4954 for (i = length, dst = MARK; i; i--) {
4955 sv_2mortal(*dst); /* free them eventually */
4962 *MARK = AvARRAY(ary)[offset+length-1];
4965 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4966 SvREFCNT_dec(*dst++); /* free them now */
4969 AvFILLp(ary) += diff;
4971 /* pull up or down? */
4973 if (offset < after) { /* easier to pull up */
4974 if (offset) { /* esp. if nothing to pull */
4975 src = &AvARRAY(ary)[offset-1];
4976 dst = src - diff; /* diff is negative */
4977 for (i = offset; i > 0; i--) /* can't trust Copy */
4981 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4985 if (after) { /* anything to pull down? */
4986 src = AvARRAY(ary) + offset + length;
4987 dst = src + diff; /* diff is negative */
4988 Move(src, dst, after, SV*);
4990 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4991 /* avoid later double free */
4995 dst[--i] = &PL_sv_undef;
4998 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4999 Safefree(tmparyval);
5002 else { /* no, expanding (or same) */
5003 SV** tmparyval = NULL;
5005 Newx(tmparyval, length, SV*); /* so remember deletion */
5006 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5009 if (diff > 0) { /* expanding */
5010 /* push up or down? */
5011 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5015 Move(src, dst, offset, SV*);
5017 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5019 AvFILLp(ary) += diff;
5022 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5023 av_extend(ary, AvFILLp(ary) + diff);
5024 AvFILLp(ary) += diff;
5027 dst = AvARRAY(ary) + AvFILLp(ary);
5029 for (i = after; i; i--) {
5037 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5040 MARK = ORIGMARK + 1;
5041 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5043 Copy(tmparyval, MARK, length, SV*);
5045 EXTEND_MORTAL(length);
5046 for (i = length, dst = MARK; i; i--) {
5047 sv_2mortal(*dst); /* free them eventually */
5054 else if (length--) {
5055 *MARK = tmparyval[length];
5058 while (length-- > 0)
5059 SvREFCNT_dec(tmparyval[length]);
5063 *MARK = &PL_sv_undef;
5064 Safefree(tmparyval);
5068 mg_set(MUTABLE_SV(ary));
5076 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5077 register AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5078 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5081 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5084 ENTER_with_name("call_PUSH");
5085 call_method("PUSH",G_SCALAR|G_DISCARD);
5086 LEAVE_with_name("call_PUSH");
5090 PL_delaymagic = DM_DELAY;
5091 for (++MARK; MARK <= SP; MARK++) {
5092 SV * const sv = newSV(0);
5094 sv_setsv(sv, *MARK);
5095 av_store(ary, AvFILLp(ary)+1, sv);
5097 if (PL_delaymagic & DM_ARRAY_ISA)
5098 mg_set(MUTABLE_SV(ary));
5103 if (OP_GIMME(PL_op, 0) != G_VOID) {
5104 PUSHi( AvFILL(ary) + 1 );
5113 AV * const av = PL_op->op_flags & OPf_SPECIAL
5114 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5115 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5119 (void)sv_2mortal(sv);
5126 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5127 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5128 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5131 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5134 ENTER_with_name("call_UNSHIFT");
5135 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5136 LEAVE_with_name("call_UNSHIFT");
5141 av_unshift(ary, SP - MARK);
5143 SV * const sv = newSVsv(*++MARK);
5144 (void)av_store(ary, i++, sv);
5148 if (OP_GIMME(PL_op, 0) != G_VOID) {
5149 PUSHi( AvFILL(ary) + 1 );
5158 if (GIMME == G_ARRAY) {
5159 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5163 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5164 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5165 av = MUTABLE_AV((*SP));
5166 /* In-place reversing only happens in void context for the array
5167 * assignment. We don't need to push anything on the stack. */
5170 if (SvMAGICAL(av)) {
5172 register SV *tmp = sv_newmortal();
5173 /* For SvCANEXISTDELETE */
5176 bool can_preserve = SvCANEXISTDELETE(av);
5178 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5179 register SV *begin, *end;
5182 if (!av_exists(av, i)) {
5183 if (av_exists(av, j)) {
5184 register SV *sv = av_delete(av, j, 0);
5185 begin = *av_fetch(av, i, TRUE);
5186 sv_setsv_mg(begin, sv);
5190 else if (!av_exists(av, j)) {
5191 register SV *sv = av_delete(av, i, 0);
5192 end = *av_fetch(av, j, TRUE);
5193 sv_setsv_mg(end, sv);
5198 begin = *av_fetch(av, i, TRUE);
5199 end = *av_fetch(av, j, TRUE);
5200 sv_setsv(tmp, begin);
5201 sv_setsv_mg(begin, end);
5202 sv_setsv_mg(end, tmp);
5206 SV **begin = AvARRAY(av);
5209 SV **end = begin + AvFILLp(av);
5211 while (begin < end) {
5212 register SV * const tmp = *begin;
5223 register SV * const tmp = *MARK;
5227 /* safe as long as stack cannot get extended in the above */
5233 register char *down;
5238 SvUTF8_off(TARG); /* decontaminate */
5240 do_join(TARG, &PL_sv_no, MARK, SP);
5242 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5243 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5244 report_uninit(TARG);
5247 up = SvPV_force(TARG, len);
5249 if (DO_UTF8(TARG)) { /* first reverse each character */
5250 U8* s = (U8*)SvPVX(TARG);
5251 const U8* send = (U8*)(s + len);
5253 if (UTF8_IS_INVARIANT(*s)) {
5258 if (!utf8_to_uvchr_buf(s, send, 0))
5262 down = (char*)(s - 1);
5263 /* reverse this character */
5267 *down-- = (char)tmp;
5273 down = SvPVX(TARG) + len - 1;
5277 *down-- = (char)tmp;
5279 (void)SvPOK_only_UTF8(TARG);
5291 register IV limit = POPi; /* note, negative is forever */
5292 SV * const sv = POPs;
5294 register const char *s = SvPV_const(sv, len);
5295 const bool do_utf8 = DO_UTF8(sv);
5296 const char *strend = s + len;
5298 register REGEXP *rx;
5300 register const char *m;
5302 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5303 I32 maxiters = slen + 10;
5304 I32 trailing_empty = 0;
5306 const I32 origlimit = limit;
5309 const I32 gimme = GIMME_V;
5311 const I32 oldsave = PL_savestack_ix;
5312 U32 make_mortal = SVs_TEMP;
5317 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5322 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5325 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5326 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5328 RX_MATCH_UTF8_set(rx, do_utf8);
5331 if (pm->op_pmreplrootu.op_pmtargetoff) {
5332 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5335 if (pm->op_pmreplrootu.op_pmtargetgv) {
5336 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5341 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5347 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5349 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5356 for (i = AvFILLp(ary); i >= 0; i--)
5357 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5359 /* temporarily switch stacks */
5360 SAVESWITCHSTACK(PL_curstack, ary);
5364 base = SP - PL_stack_base;
5366 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5368 while (*s == ' ' || is_utf8_space((U8*)s))
5371 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5372 while (isSPACE_LC(*s))
5380 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5384 gimme_scalar = gimme == G_SCALAR && !ary;
5387 limit = maxiters + 2;
5388 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5391 /* this one uses 'm' and is a negative test */
5393 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5394 const int t = UTF8SKIP(m);
5395 /* is_utf8_space returns FALSE for malform utf8 */
5402 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5403 while (m < strend && !isSPACE_LC(*m))
5406 while (m < strend && !isSPACE(*m))
5419 dstr = newSVpvn_flags(s, m-s,
5420 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5424 /* skip the whitespace found last */
5426 s = m + UTF8SKIP(m);
5430 /* this one uses 's' and is a positive test */
5432 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5435 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5436 while (s < strend && isSPACE_LC(*s))
5439 while (s < strend && isSPACE(*s))
5444 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5446 for (m = s; m < strend && *m != '\n'; m++)
5459 dstr = newSVpvn_flags(s, m-s,
5460 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5466 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5468 Pre-extend the stack, either the number of bytes or
5469 characters in the string or a limited amount, triggered by:
5471 my ($x, $y) = split //, $str;
5475 if (!gimme_scalar) {
5476 const U32 items = limit - 1;
5485 /* keep track of how many bytes we skip over */
5495 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5508 dstr = newSVpvn(s, 1);
5524 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5525 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5526 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5527 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5528 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5529 SV * const csv = CALLREG_INTUIT_STRING(rx);
5531 len = RX_MINLENRET(rx);
5532 if (len == 1 && !RX_UTF8(rx) && !tail) {
5533 const char c = *SvPV_nolen_const(csv);
5535 for (m = s; m < strend && *m != c; m++)
5546 dstr = newSVpvn_flags(s, m-s,
5547 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5550 /* The rx->minlen is in characters but we want to step
5551 * s ahead by bytes. */
5553 s = (char*)utf8_hop((U8*)m, len);
5555 s = m + len; /* Fake \n at the end */
5559 while (s < strend && --limit &&
5560 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5561 csv, multiline ? FBMrf_MULTILINE : 0)) )
5570 dstr = newSVpvn_flags(s, m-s,
5571 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5574 /* The rx->minlen is in characters but we want to step
5575 * s ahead by bytes. */
5577 s = (char*)utf8_hop((U8*)m, len);
5579 s = m + len; /* Fake \n at the end */
5584 maxiters += slen * RX_NPARENS(rx);
5585 while (s < strend && --limit)
5589 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5592 if (rex_return == 0)
5594 TAINT_IF(RX_MATCH_TAINTED(rx));
5595 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5598 orig = RX_SUBBEG(rx);
5600 strend = s + (strend - m);
5602 m = RX_OFFS(rx)[0].start + orig;
5611 dstr = newSVpvn_flags(s, m-s,
5612 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5615 if (RX_NPARENS(rx)) {
5617 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5618 s = RX_OFFS(rx)[i].start + orig;
5619 m = RX_OFFS(rx)[i].end + orig;
5621 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5622 parens that didn't match -- they should be set to
5623 undef, not the empty string */
5631 if (m >= orig && s >= orig) {
5632 dstr = newSVpvn_flags(s, m-s,
5633 (do_utf8 ? SVf_UTF8 : 0)
5637 dstr = &PL_sv_undef; /* undef, not "" */
5643 s = RX_OFFS(rx)[0].end + orig;
5647 if (!gimme_scalar) {
5648 iters = (SP - PL_stack_base) - base;
5650 if (iters > maxiters)
5651 DIE(aTHX_ "Split loop");
5653 /* keep field after final delim? */
5654 if (s < strend || (iters && origlimit)) {
5655 if (!gimme_scalar) {
5656 const STRLEN l = strend - s;
5657 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5662 else if (!origlimit) {
5664 iters -= trailing_empty;
5666 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5667 if (TOPs && !make_mortal)
5669 *SP-- = &PL_sv_undef;
5676 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5680 if (SvSMAGICAL(ary)) {
5682 mg_set(MUTABLE_SV(ary));
5685 if (gimme == G_ARRAY) {
5687 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5694 ENTER_with_name("call_PUSH");
5695 call_method("PUSH",G_SCALAR|G_DISCARD);
5696 LEAVE_with_name("call_PUSH");
5698 if (gimme == G_ARRAY) {
5700 /* EXTEND should not be needed - we just popped them */
5702 for (i=0; i < iters; i++) {
5703 SV **svp = av_fetch(ary, i, FALSE);
5704 PUSHs((svp) ? *svp : &PL_sv_undef);
5711 if (gimme == G_ARRAY)
5723 SV *const sv = PAD_SVl(PL_op->op_targ);
5725 if (SvPADSTALE(sv)) {
5728 RETURNOP(cLOGOP->op_other);
5730 RETURNOP(cLOGOP->op_next);
5740 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5741 || SvTYPE(retsv) == SVt_PVCV) {
5742 retsv = refto(retsv);
5749 PP(unimplemented_op)
5752 const Optype op_type = PL_op->op_type;
5753 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5754 with out of range op numbers - it only "special" cases op_custom.
5755 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5756 if we get here for a custom op then that means that the custom op didn't
5757 have an implementation. Given that OP_NAME() looks up the custom op
5758 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5759 registers &PL_unimplemented_op as the address of their custom op.
5760 NULL doesn't generate a useful error message. "custom" does. */
5761 const char *const name = op_type >= OP_max
5762 ? "[out of range]" : PL_op_name[PL_op->op_type];
5763 if(OP_IS_SOCKET(op_type))
5764 DIE(aTHX_ PL_no_sock_func, name);
5765 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5772 HV * const hv = (HV*)POPs;
5774 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
5776 if (SvRMAGICAL(hv)) {
5777 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5779 XPUSHs(magic_scalarpack(hv, mg));
5784 XPUSHs(boolSV(HvUSEDKEYS(hv) != 0));
5788 /* For sorting out arguments passed to a &CORE:: subroutine */
5792 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5793 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5794 AV * const at_ = GvAV(PL_defgv);
5795 SV **svp = at_ ? AvARRAY(at_) : NULL;
5796 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5797 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5798 bool seen_question = 0;
5799 const char *err = NULL;
5800 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5802 /* Count how many args there are first, to get some idea how far to
5803 extend the stack. */
5805 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5807 if (oa & OA_OPTIONAL) seen_question = 1;
5808 if (!seen_question) minargs++;
5812 if(numargs < minargs) err = "Not enough";
5813 else if(numargs > maxargs) err = "Too many";
5815 /* diag_listed_as: Too many arguments for %s */
5817 "%s arguments for %s", err,
5818 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5821 /* Reset the stack pointer. Without this, we end up returning our own
5822 arguments in list context, in addition to the values we are supposed
5823 to return. nextstate usually does this on sub entry, but we need
5824 to run the next op with the caller's hints, so we cannot have a
5826 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5828 if(!maxargs) RETURN;
5830 /* We do this here, rather than with a separate pushmark op, as it has
5831 to come in between two things this function does (stack reset and
5832 arg pushing). This seems the easiest way to do it. */
5835 (void)Perl_pp_pushmark(aTHX);
5838 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5839 PUTBACK; /* The code below can die in various places. */
5841 oa = PL_opargs[opnum] >> OASHIFT;
5842 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5847 if (!numargs && defgv && whicharg == minargs + 1) {
5848 PERL_SI * const oldsi = PL_curstackinfo;
5849 I32 const oldcxix = oldsi->si_cxix;
5851 if (oldcxix) oldsi->si_cxix--;
5852 else PL_curstackinfo = oldsi->si_prev;
5853 caller = find_runcv(NULL);
5854 PL_curstackinfo = oldsi;
5855 oldsi->si_cxix = oldcxix;
5856 PUSHs(find_rundefsv2(
5857 caller,cxstack[cxstack_ix].blk_oldcop->cop_seq
5860 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5864 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5869 if (!svp || !*svp || !SvROK(*svp)
5870 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5872 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5873 "Type of arg %d to &CORE::%s must be hash reference",
5874 whicharg, OP_DESC(PL_op->op_next)
5879 if (!numargs) PUSHs(NULL);
5880 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5881 /* no magic here, as the prototype will have added an extra
5882 refgen and we just want what was there before that */
5885 const bool constr = PL_op->op_private & whicharg;
5887 svp && *svp ? *svp : &PL_sv_undef,
5888 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5894 if (!numargs) goto try_defsv;
5896 const bool wantscalar =
5897 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5898 if (!svp || !*svp || !SvROK(*svp)
5899 /* We have to permit globrefs even for the \$ proto, as
5900 *foo is indistinguishable from ${\*foo}, and the proto-
5901 type permits the latter. */
5902 || SvTYPE(SvRV(*svp)) > (
5903 wantscalar ? SVt_PVLV
5904 : opnum == OP_LOCK || opnum == OP_UNDEF
5910 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5911 "Type of arg %d to &CORE::%s must be %s",
5912 whicharg, PL_op_name[opnum],
5914 ? "scalar reference"
5915 : opnum == OP_LOCK || opnum == OP_UNDEF
5916 ? "reference to one of [$@%&*]"
5917 : "reference to one of [$@%*]"
5920 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5921 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5922 /* Undo @_ localisation, so that sub exit does not undo
5923 part of our undeffing. */
5924 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5926 cx->cx_type &= ~ CXp_HASARGS;
5927 assert(!AvREAL(cx->blk_sub.argarray));
5932 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5944 if (PL_op->op_private & OPpOFFBYONE) {
5945 PERL_SI * const oldsi = PL_curstackinfo;
5946 I32 const oldcxix = oldsi->si_cxix;
5947 if (oldcxix) oldsi->si_cxix--;
5948 else PL_curstackinfo = oldsi->si_prev;
5949 cv = find_runcv(NULL);
5950 PL_curstackinfo = oldsi;
5951 oldsi->si_cxix = oldcxix;
5953 else cv = find_runcv(NULL);
5954 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5961 * c-indentation-style: bsd
5963 * indent-tabs-mode: nil
5966 * ex: set ts=8 sts=4 sw=4 et: