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 (SvPOK(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, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
276 Perl_die(aTHX_ PL_no_usym, what);
280 PL_op->op_flags & OPf_REF &&
281 PL_op->op_next->op_type != OP_BOOLKEYS
283 Perl_die(aTHX_ PL_no_usym, what);
284 if (ckWARN(WARN_UNINITIALIZED))
286 if (type != SVt_PV && GIMME_V == G_ARRAY) {
290 **spp = &PL_sv_undef;
293 if ((PL_op->op_flags & OPf_SPECIAL) &&
294 !(PL_op->op_flags & OPf_MOD))
296 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
298 **spp = &PL_sv_undef;
303 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
316 sv = amagic_deref_call(sv, to_sv_amg);
320 switch (SvTYPE(sv)) {
326 DIE(aTHX_ "Not a SCALAR reference");
333 if (!isGV_with_GP(gv)) {
334 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
340 if (PL_op->op_flags & OPf_MOD) {
341 if (PL_op->op_private & OPpLVAL_INTRO) {
342 if (cUNOP->op_first->op_type == OP_NULL)
343 sv = save_scalar(MUTABLE_GV(TOPs));
345 sv = save_scalar(gv);
347 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
349 else if (PL_op->op_private & OPpDEREF)
350 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
359 AV * const av = MUTABLE_AV(TOPs);
360 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
362 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
364 *sv = newSV_type(SVt_PVMG);
365 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
369 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
378 if (PL_op->op_flags & OPf_MOD || LVRET) {
379 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
380 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
382 LvTARG(ret) = SvREFCNT_inc_simple(sv);
383 PUSHs(ret); /* no SvSETMAGIC */
387 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
388 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
389 if (mg && mg->mg_len >= 0) {
407 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
409 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
412 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
413 /* (But not in defined().) */
415 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
417 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
421 cv = MUTABLE_CV(&PL_sv_undef);
422 SETs(MUTABLE_SV(cv));
432 SV *ret = &PL_sv_undef;
434 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
435 const char * s = SvPVX_const(TOPs);
436 if (strnEQ(s, "CORE::", 6)) {
437 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
438 if (!code || code == -KEY_CORE)
439 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
440 SVfARG(newSVpvn_flags(
441 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
444 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
450 cv = sv_2cv(TOPs, &stash, &gv, 0);
452 ret = newSVpvn_flags(
453 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
463 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
465 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
467 PUSHs(MUTABLE_SV(cv));
481 if (GIMME != G_ARRAY) {
485 *MARK = &PL_sv_undef;
486 *MARK = refto(*MARK);
490 EXTEND_MORTAL(SP - MARK);
492 *MARK = refto(*MARK);
497 S_refto(pTHX_ SV *sv)
502 PERL_ARGS_ASSERT_REFTO;
504 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
507 if (!(sv = LvTARG(sv)))
510 SvREFCNT_inc_void_NN(sv);
512 else if (SvTYPE(sv) == SVt_PVAV) {
513 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
514 av_reify(MUTABLE_AV(sv));
516 SvREFCNT_inc_void_NN(sv);
518 else if (SvPADTMP(sv) && !IS_PADGV(sv))
522 SvREFCNT_inc_void_NN(sv);
525 sv_upgrade(rv, SVt_IV);
534 SV * const sv = POPs;
539 if (!sv || !SvROK(sv))
542 (void)sv_ref(TARG,SvRV(sv),TRUE);
554 stash = CopSTASH(PL_curcop);
556 SV * const ssv = POPs;
560 if (!ssv) goto curstash;
561 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
562 Perl_croak(aTHX_ "Attempt to bless into a reference");
563 ptr = SvPV_const(ssv,len);
565 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
566 "Explicit blessing to '' (assuming package main)");
567 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
570 (void)sv_bless(TOPs, stash);
580 const char * const elem = SvPV_const(sv, len);
581 GV * const gv = MUTABLE_GV(POPs);
586 /* elem will always be NUL terminated. */
587 const char * const second_letter = elem + 1;
590 if (len == 5 && strEQ(second_letter, "RRAY"))
591 tmpRef = MUTABLE_SV(GvAV(gv));
594 if (len == 4 && strEQ(second_letter, "ODE"))
595 tmpRef = MUTABLE_SV(GvCVu(gv));
598 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
599 /* finally deprecated in 5.8.0 */
600 deprecate("*glob{FILEHANDLE}");
601 tmpRef = MUTABLE_SV(GvIOp(gv));
604 if (len == 6 && strEQ(second_letter, "ORMAT"))
605 tmpRef = MUTABLE_SV(GvFORM(gv));
608 if (len == 4 && strEQ(second_letter, "LOB"))
609 tmpRef = MUTABLE_SV(gv);
612 if (len == 4 && strEQ(second_letter, "ASH"))
613 tmpRef = MUTABLE_SV(GvHV(gv));
616 if (*second_letter == 'O' && !elem[2] && len == 2)
617 tmpRef = MUTABLE_SV(GvIOp(gv));
620 if (len == 4 && strEQ(second_letter, "AME"))
621 sv = newSVhek(GvNAME_HEK(gv));
624 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
625 const HV * const stash = GvSTASH(gv);
626 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
627 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
631 if (len == 6 && strEQ(second_letter, "CALAR"))
646 /* Pattern matching */
651 register unsigned char *s;
654 s = (unsigned char*)(SvPV(sv, len));
655 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
656 /* Historically, study was skipped in these cases. */
660 /* Make study a no-op. It's no longer useful and its existence
661 complicates matters elsewhere. */
670 if (PL_op->op_flags & OPf_STACKED)
672 else if (PL_op->op_private & OPpTARGET_MY)
678 TARG = sv_newmortal();
679 if(PL_op->op_type == OP_TRANSR) {
681 const char * const pv = SvPV(sv,len);
682 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
686 else PUSHi(do_trans(sv));
690 /* Lvalue operators. */
693 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
699 PERL_ARGS_ASSERT_DO_CHOMP;
701 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
703 if (SvTYPE(sv) == SVt_PVAV) {
705 AV *const av = MUTABLE_AV(sv);
706 const I32 max = AvFILL(av);
708 for (i = 0; i <= max; i++) {
709 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
710 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
711 do_chomp(retval, sv, chomping);
715 else if (SvTYPE(sv) == SVt_PVHV) {
716 HV* const hv = MUTABLE_HV(sv);
718 (void)hv_iterinit(hv);
719 while ((entry = hv_iternext(hv)))
720 do_chomp(retval, hv_iterval(hv,entry), chomping);
723 else if (SvREADONLY(sv)) {
725 /* SV is copy-on-write */
726 sv_force_normal_flags(sv, 0);
729 Perl_croak_no_modify(aTHX);
734 /* XXX, here sv is utf8-ized as a side-effect!
735 If encoding.pm is used properly, almost string-generating
736 operations, including literal strings, chr(), input data, etc.
737 should have been utf8-ized already, right?
739 sv_recode_to_utf8(sv, PL_encoding);
745 char *temp_buffer = NULL;
754 while (len && s[-1] == '\n') {
761 STRLEN rslen, rs_charlen;
762 const char *rsptr = SvPV_const(PL_rs, rslen);
764 rs_charlen = SvUTF8(PL_rs)
768 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
769 /* Assumption is that rs is shorter than the scalar. */
771 /* RS is utf8, scalar is 8 bit. */
773 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
776 /* Cannot downgrade, therefore cannot possibly match
778 assert (temp_buffer == rsptr);
784 else if (PL_encoding) {
785 /* RS is 8 bit, encoding.pm is used.
786 * Do not recode PL_rs as a side-effect. */
787 svrecode = newSVpvn(rsptr, rslen);
788 sv_recode_to_utf8(svrecode, PL_encoding);
789 rsptr = SvPV_const(svrecode, rslen);
790 rs_charlen = sv_len_utf8(svrecode);
793 /* RS is 8 bit, scalar is utf8. */
794 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
808 if (memNE(s, rsptr, rslen))
810 SvIVX(retval) += rs_charlen;
813 s = SvPV_force_nomg_nolen(sv);
821 SvREFCNT_dec(svrecode);
823 Safefree(temp_buffer);
825 if (len && !SvPOK(sv))
826 s = SvPV_force_nomg(sv, len);
829 char * const send = s + len;
830 char * const start = s;
832 while (s > start && UTF8_IS_CONTINUATION(*s))
834 if (is_utf8_string((U8*)s, send - s)) {
835 sv_setpvn(retval, s, send - s);
837 SvCUR_set(sv, s - start);
843 sv_setpvs(retval, "");
847 sv_setpvn(retval, s, 1);
854 sv_setpvs(retval, "");
862 const bool chomping = PL_op->op_type == OP_SCHOMP;
866 do_chomp(TARG, TOPs, chomping);
873 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
874 const bool chomping = PL_op->op_type == OP_CHOMP;
879 do_chomp(TARG, *++MARK, chomping);
890 if (!PL_op->op_private) {
899 SV_CHECK_THINKFIRST_COW_DROP(sv);
901 switch (SvTYPE(sv)) {
905 av_undef(MUTABLE_AV(sv));
908 hv_undef(MUTABLE_HV(sv));
911 if (cv_const_sv((const CV *)sv))
912 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
913 "Constant subroutine %"SVf" undefined",
914 SVfARG(CvANON((const CV *)sv)
915 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
916 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
920 /* let user-undef'd sub keep its identity */
921 GV* const gv = CvGV((const CV *)sv);
922 cv_undef(MUTABLE_CV(sv));
923 CvGV_set(MUTABLE_CV(sv), gv);
928 SvSetMagicSV(sv, &PL_sv_undef);
931 else if (isGV_with_GP(sv)) {
935 /* undef *Pkg::meth_name ... */
937 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
938 && HvENAME_get(stash);
940 if((stash = GvHV((const GV *)sv))) {
941 if(HvENAME_get(stash))
942 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
946 gp_free(MUTABLE_GV(sv));
948 GvGP_set(sv, gp_ref(gp));
950 GvLINE(sv) = CopLINE(PL_curcop);
951 GvEGV(sv) = MUTABLE_GV(sv);
955 mro_package_moved(NULL, stash, (const GV *)sv, 0);
957 /* undef *Foo::ISA */
958 if( strEQ(GvNAME((const GV *)sv), "ISA")
959 && (stash = GvSTASH((const GV *)sv))
960 && (method_changed || HvENAME(stash)) )
961 mro_isa_changed_in(stash);
962 else if(method_changed)
963 mro_method_changed_in(
964 GvSTASH((const GV *)sv)
971 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
987 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
988 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
989 Perl_croak_no_modify(aTHX);
991 TARG = sv_newmortal();
992 sv_setsv(TARG, TOPs);
993 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
994 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
996 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
997 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1001 else sv_dec_nomg(TOPs);
1003 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1004 if (inc && !SvOK(TARG))
1010 /* Ordinary operators. */
1014 dVAR; dSP; dATARGET; SV *svl, *svr;
1015 #ifdef PERL_PRESERVE_IVUV
1018 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1021 #ifdef PERL_PRESERVE_IVUV
1022 /* For integer to integer power, we do the calculation by hand wherever
1023 we're sure it is safe; otherwise we call pow() and try to convert to
1024 integer afterwards. */
1026 SvIV_please_nomg(svr);
1028 SvIV_please_nomg(svl);
1037 const IV iv = SvIVX(svr);
1041 goto float_it; /* Can't do negative powers this way. */
1045 baseuok = SvUOK(svl);
1047 baseuv = SvUVX(svl);
1049 const IV iv = SvIVX(svl);
1052 baseuok = TRUE; /* effectively it's a UV now */
1054 baseuv = -iv; /* abs, baseuok == false records sign */
1057 /* now we have integer ** positive integer. */
1060 /* foo & (foo - 1) is zero only for a power of 2. */
1061 if (!(baseuv & (baseuv - 1))) {
1062 /* We are raising power-of-2 to a positive integer.
1063 The logic here will work for any base (even non-integer
1064 bases) but it can be less accurate than
1065 pow (base,power) or exp (power * log (base)) when the
1066 intermediate values start to spill out of the mantissa.
1067 With powers of 2 we know this can't happen.
1068 And powers of 2 are the favourite thing for perl
1069 programmers to notice ** not doing what they mean. */
1071 NV base = baseuok ? baseuv : -(NV)baseuv;
1076 while (power >>= 1) {
1084 SvIV_please_nomg(svr);
1087 register unsigned int highbit = 8 * sizeof(UV);
1088 register unsigned int diff = 8 * sizeof(UV);
1089 while (diff >>= 1) {
1091 if (baseuv >> highbit) {
1095 /* we now have baseuv < 2 ** highbit */
1096 if (power * highbit <= 8 * sizeof(UV)) {
1097 /* result will definitely fit in UV, so use UV math
1098 on same algorithm as above */
1099 register UV result = 1;
1100 register UV base = baseuv;
1101 const bool odd_power = cBOOL(power & 1);
1105 while (power >>= 1) {
1112 if (baseuok || !odd_power)
1113 /* answer is positive */
1115 else if (result <= (UV)IV_MAX)
1116 /* answer negative, fits in IV */
1117 SETi( -(IV)result );
1118 else if (result == (UV)IV_MIN)
1119 /* 2's complement assumption: special case IV_MIN */
1122 /* answer negative, doesn't fit */
1123 SETn( -(NV)result );
1133 NV right = SvNV_nomg(svr);
1134 NV left = SvNV_nomg(svl);
1137 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1139 We are building perl with long double support and are on an AIX OS
1140 afflicted with a powl() function that wrongly returns NaNQ for any
1141 negative base. This was reported to IBM as PMR #23047-379 on
1142 03/06/2006. The problem exists in at least the following versions
1143 of AIX and the libm fileset, and no doubt others as well:
1145 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1146 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1147 AIX 5.2.0 bos.adt.libm 5.2.0.85
1149 So, until IBM fixes powl(), we provide the following workaround to
1150 handle the problem ourselves. Our logic is as follows: for
1151 negative bases (left), we use fmod(right, 2) to check if the
1152 exponent is an odd or even integer:
1154 - if odd, powl(left, right) == -powl(-left, right)
1155 - if even, powl(left, right) == powl(-left, right)
1157 If the exponent is not an integer, the result is rightly NaNQ, so
1158 we just return that (as NV_NAN).
1162 NV mod2 = Perl_fmod( right, 2.0 );
1163 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1164 SETn( -Perl_pow( -left, right) );
1165 } else if (mod2 == 0.0) { /* even integer */
1166 SETn( Perl_pow( -left, right) );
1167 } else { /* fractional power */
1171 SETn( Perl_pow( left, right) );
1174 SETn( Perl_pow( left, right) );
1175 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1177 #ifdef PERL_PRESERVE_IVUV
1179 SvIV_please_nomg(svr);
1187 dVAR; dSP; dATARGET; SV *svl, *svr;
1188 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1191 #ifdef PERL_PRESERVE_IVUV
1192 SvIV_please_nomg(svr);
1194 /* Unless the left argument is integer in range we are going to have to
1195 use NV maths. Hence only attempt to coerce the right argument if
1196 we know the left is integer. */
1197 /* Left operand is defined, so is it IV? */
1198 SvIV_please_nomg(svl);
1200 bool auvok = SvUOK(svl);
1201 bool buvok = SvUOK(svr);
1202 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1203 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1212 const IV aiv = SvIVX(svl);
1215 auvok = TRUE; /* effectively it's a UV now */
1217 alow = -aiv; /* abs, auvok == false records sign */
1223 const IV biv = SvIVX(svr);
1226 buvok = TRUE; /* effectively it's a UV now */
1228 blow = -biv; /* abs, buvok == false records sign */
1232 /* If this does sign extension on unsigned it's time for plan B */
1233 ahigh = alow >> (4 * sizeof (UV));
1235 bhigh = blow >> (4 * sizeof (UV));
1237 if (ahigh && bhigh) {
1239 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1240 which is overflow. Drop to NVs below. */
1241 } else if (!ahigh && !bhigh) {
1242 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1243 so the unsigned multiply cannot overflow. */
1244 const UV product = alow * blow;
1245 if (auvok == buvok) {
1246 /* -ve * -ve or +ve * +ve gives a +ve result. */
1250 } else if (product <= (UV)IV_MIN) {
1251 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1252 /* -ve result, which could overflow an IV */
1254 SETi( -(IV)product );
1256 } /* else drop to NVs below. */
1258 /* One operand is large, 1 small */
1261 /* swap the operands */
1263 bhigh = blow; /* bhigh now the temp var for the swap */
1267 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1268 multiplies can't overflow. shift can, add can, -ve can. */
1269 product_middle = ahigh * blow;
1270 if (!(product_middle & topmask)) {
1271 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1273 product_middle <<= (4 * sizeof (UV));
1274 product_low = alow * blow;
1276 /* as for pp_add, UV + something mustn't get smaller.
1277 IIRC ANSI mandates this wrapping *behaviour* for
1278 unsigned whatever the actual representation*/
1279 product_low += product_middle;
1280 if (product_low >= product_middle) {
1281 /* didn't overflow */
1282 if (auvok == buvok) {
1283 /* -ve * -ve or +ve * +ve gives a +ve result. */
1285 SETu( product_low );
1287 } else if (product_low <= (UV)IV_MIN) {
1288 /* 2s complement assumption again */
1289 /* -ve result, which could overflow an IV */
1291 SETi( -(IV)product_low );
1293 } /* else drop to NVs below. */
1295 } /* product_middle too large */
1296 } /* ahigh && bhigh */
1301 NV right = SvNV_nomg(svr);
1302 NV left = SvNV_nomg(svl);
1304 SETn( left * right );
1311 dVAR; dSP; dATARGET; SV *svl, *svr;
1312 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1315 /* Only try to do UV divide first
1316 if ((SLOPPYDIVIDE is true) or
1317 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1319 The assumption is that it is better to use floating point divide
1320 whenever possible, only doing integer divide first if we can't be sure.
1321 If NV_PRESERVES_UV is true then we know at compile time that no UV
1322 can be too large to preserve, so don't need to compile the code to
1323 test the size of UVs. */
1326 # define PERL_TRY_UV_DIVIDE
1327 /* ensure that 20./5. == 4. */
1329 # ifdef PERL_PRESERVE_IVUV
1330 # ifndef NV_PRESERVES_UV
1331 # define PERL_TRY_UV_DIVIDE
1336 #ifdef PERL_TRY_UV_DIVIDE
1337 SvIV_please_nomg(svr);
1339 SvIV_please_nomg(svl);
1341 bool left_non_neg = SvUOK(svl);
1342 bool right_non_neg = SvUOK(svr);
1346 if (right_non_neg) {
1350 const IV biv = SvIVX(svr);
1353 right_non_neg = TRUE; /* effectively it's a UV now */
1359 /* historically undef()/0 gives a "Use of uninitialized value"
1360 warning before dieing, hence this test goes here.
1361 If it were immediately before the second SvIV_please, then
1362 DIE() would be invoked before left was even inspected, so
1363 no inspection would give no warning. */
1365 DIE(aTHX_ "Illegal division by zero");
1371 const IV aiv = SvIVX(svl);
1374 left_non_neg = TRUE; /* effectively it's a UV now */
1383 /* For sloppy divide we always attempt integer division. */
1385 /* Otherwise we only attempt it if either or both operands
1386 would not be preserved by an NV. If both fit in NVs
1387 we fall through to the NV divide code below. However,
1388 as left >= right to ensure integer result here, we know that
1389 we can skip the test on the right operand - right big
1390 enough not to be preserved can't get here unless left is
1393 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1396 /* Integer division can't overflow, but it can be imprecise. */
1397 const UV result = left / right;
1398 if (result * right == left) {
1399 SP--; /* result is valid */
1400 if (left_non_neg == right_non_neg) {
1401 /* signs identical, result is positive. */
1405 /* 2s complement assumption */
1406 if (result <= (UV)IV_MIN)
1407 SETi( -(IV)result );
1409 /* It's exact but too negative for IV. */
1410 SETn( -(NV)result );
1413 } /* tried integer divide but it was not an integer result */
1414 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1415 } /* left wasn't SvIOK */
1416 } /* right wasn't SvIOK */
1417 #endif /* PERL_TRY_UV_DIVIDE */
1419 NV right = SvNV_nomg(svr);
1420 NV left = SvNV_nomg(svl);
1421 (void)POPs;(void)POPs;
1422 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1423 if (! Perl_isnan(right) && right == 0.0)
1427 DIE(aTHX_ "Illegal division by zero");
1428 PUSHn( left / right );
1435 dVAR; dSP; dATARGET;
1436 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1440 bool left_neg = FALSE;
1441 bool right_neg = FALSE;
1442 bool use_double = FALSE;
1443 bool dright_valid = FALSE;
1446 SV * const svr = TOPs;
1447 SV * const svl = TOPm1s;
1448 SvIV_please_nomg(svr);
1450 right_neg = !SvUOK(svr);
1454 const IV biv = SvIVX(svr);
1457 right_neg = FALSE; /* effectively it's a UV now */
1464 dright = SvNV_nomg(svr);
1465 right_neg = dright < 0;
1468 if (dright < UV_MAX_P1) {
1469 right = U_V(dright);
1470 dright_valid = TRUE; /* In case we need to use double below. */
1476 /* At this point use_double is only true if right is out of range for
1477 a UV. In range NV has been rounded down to nearest UV and
1478 use_double false. */
1479 SvIV_please_nomg(svl);
1480 if (!use_double && SvIOK(svl)) {
1482 left_neg = !SvUOK(svl);
1486 const IV aiv = SvIVX(svl);
1489 left_neg = FALSE; /* effectively it's a UV now */
1497 dleft = SvNV_nomg(svl);
1498 left_neg = dleft < 0;
1502 /* This should be exactly the 5.6 behaviour - if left and right are
1503 both in range for UV then use U_V() rather than floor. */
1505 if (dleft < UV_MAX_P1) {
1506 /* right was in range, so is dleft, so use UVs not double.
1510 /* left is out of range for UV, right was in range, so promote
1511 right (back) to double. */
1513 /* The +0.5 is used in 5.6 even though it is not strictly
1514 consistent with the implicit +0 floor in the U_V()
1515 inside the #if 1. */
1516 dleft = Perl_floor(dleft + 0.5);
1519 dright = Perl_floor(dright + 0.5);
1530 DIE(aTHX_ "Illegal modulus zero");
1532 dans = Perl_fmod(dleft, dright);
1533 if ((left_neg != right_neg) && dans)
1534 dans = dright - dans;
1537 sv_setnv(TARG, dans);
1543 DIE(aTHX_ "Illegal modulus zero");
1546 if ((left_neg != right_neg) && ans)
1549 /* XXX may warn: unary minus operator applied to unsigned type */
1550 /* could change -foo to be (~foo)+1 instead */
1551 if (ans <= ~((UV)IV_MAX)+1)
1552 sv_setiv(TARG, ~ans+1);
1554 sv_setnv(TARG, -(NV)ans);
1557 sv_setuv(TARG, ans);
1566 dVAR; dSP; dATARGET;
1570 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1571 /* TODO: think of some way of doing list-repeat overloading ??? */
1576 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1582 const UV uv = SvUV_nomg(sv);
1584 count = IV_MAX; /* The best we can do? */
1588 const IV iv = SvIV_nomg(sv);
1595 else if (SvNOKp(sv)) {
1596 const NV nv = SvNV_nomg(sv);
1603 count = SvIV_nomg(sv);
1605 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1607 static const char oom_list_extend[] = "Out of memory during list extend";
1608 const I32 items = SP - MARK;
1609 const I32 max = items * count;
1611 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1612 /* Did the max computation overflow? */
1613 if (items > 0 && max > 0 && (max < items || max < count))
1614 Perl_croak(aTHX_ oom_list_extend);
1619 /* This code was intended to fix 20010809.028:
1622 for (($x =~ /./g) x 2) {
1623 print chop; # "abcdabcd" expected as output.
1626 * but that change (#11635) broke this code:
1628 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1630 * I can't think of a better fix that doesn't introduce
1631 * an efficiency hit by copying the SVs. The stack isn't
1632 * refcounted, and mortalisation obviously doesn't
1633 * Do The Right Thing when the stack has more than
1634 * one pointer to the same mortal value.
1638 *SP = sv_2mortal(newSVsv(*SP));
1648 repeatcpy((char*)(MARK + items), (char*)MARK,
1649 items * sizeof(const SV *), count - 1);
1652 else if (count <= 0)
1655 else { /* Note: mark already snarfed by pp_list */
1656 SV * const tmpstr = POPs;
1659 static const char oom_string_extend[] =
1660 "Out of memory during string extend";
1663 sv_setsv_nomg(TARG, tmpstr);
1664 SvPV_force_nomg(TARG, len);
1665 isutf = DO_UTF8(TARG);
1670 const STRLEN max = (UV)count * len;
1671 if (len > MEM_SIZE_MAX / count)
1672 Perl_croak(aTHX_ oom_string_extend);
1673 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1674 SvGROW(TARG, max + 1);
1675 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1676 SvCUR_set(TARG, SvCUR(TARG) * count);
1678 *SvEND(TARG) = '\0';
1681 (void)SvPOK_only_UTF8(TARG);
1683 (void)SvPOK_only(TARG);
1685 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1686 /* The parser saw this as a list repeat, and there
1687 are probably several items on the stack. But we're
1688 in scalar context, and there's no pp_list to save us
1689 now. So drop the rest of the items -- robin@kitsite.com
1701 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1702 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1705 useleft = USE_LEFT(svl);
1706 #ifdef PERL_PRESERVE_IVUV
1707 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1708 "bad things" happen if you rely on signed integers wrapping. */
1709 SvIV_please_nomg(svr);
1711 /* Unless the left argument is integer in range we are going to have to
1712 use NV maths. Hence only attempt to coerce the right argument if
1713 we know the left is integer. */
1714 register UV auv = 0;
1720 a_valid = auvok = 1;
1721 /* left operand is undef, treat as zero. */
1723 /* Left operand is defined, so is it IV? */
1724 SvIV_please_nomg(svl);
1726 if ((auvok = SvUOK(svl)))
1729 register const IV aiv = SvIVX(svl);
1732 auvok = 1; /* Now acting as a sign flag. */
1733 } else { /* 2s complement assumption for IV_MIN */
1741 bool result_good = 0;
1744 bool buvok = SvUOK(svr);
1749 register const IV biv = SvIVX(svr);
1756 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1757 else "IV" now, independent of how it came in.
1758 if a, b represents positive, A, B negative, a maps to -A etc
1763 all UV maths. negate result if A negative.
1764 subtract if signs same, add if signs differ. */
1766 if (auvok ^ buvok) {
1775 /* Must get smaller */
1780 if (result <= buv) {
1781 /* result really should be -(auv-buv). as its negation
1782 of true value, need to swap our result flag */
1794 if (result <= (UV)IV_MIN)
1795 SETi( -(IV)result );
1797 /* result valid, but out of range for IV. */
1798 SETn( -(NV)result );
1802 } /* Overflow, drop through to NVs. */
1807 NV value = SvNV_nomg(svr);
1811 /* left operand is undef, treat as zero - value */
1815 SETn( SvNV_nomg(svl) - value );
1822 dVAR; dSP; dATARGET; SV *svl, *svr;
1823 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1827 const IV shift = SvIV_nomg(svr);
1828 if (PL_op->op_private & HINT_INTEGER) {
1829 const IV i = SvIV_nomg(svl);
1833 const UV u = SvUV_nomg(svl);
1842 dVAR; dSP; dATARGET; SV *svl, *svr;
1843 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1847 const IV shift = SvIV_nomg(svr);
1848 if (PL_op->op_private & HINT_INTEGER) {
1849 const IV i = SvIV_nomg(svl);
1853 const UV u = SvUV_nomg(svl);
1865 tryAMAGICbin_MG(lt_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(gt_amg, AMGf_set|AMGf_numeric);
1885 (SvIOK_notUV(left) && SvIOK_notUV(right))
1886 ? (SvIVX(left) > SvIVX(right))
1887 : (do_ncmp(left, right) == 1)
1897 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1901 (SvIOK_notUV(left) && SvIOK_notUV(right))
1902 ? (SvIVX(left) <= SvIVX(right))
1903 : (do_ncmp(left, right) <= 0)
1913 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1917 (SvIOK_notUV(left) && SvIOK_notUV(right))
1918 ? (SvIVX(left) >= SvIVX(right))
1919 : ( (do_ncmp(left, right) & 2) == 0)
1929 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1933 (SvIOK_notUV(left) && SvIOK_notUV(right))
1934 ? (SvIVX(left) != SvIVX(right))
1935 : (do_ncmp(left, right) != 0)
1940 /* compare left and right SVs. Returns:
1944 * 2: left or right was a NaN
1947 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1951 PERL_ARGS_ASSERT_DO_NCMP;
1952 #ifdef PERL_PRESERVE_IVUV
1953 SvIV_please_nomg(right);
1954 /* Fortunately it seems NaN isn't IOK */
1956 SvIV_please_nomg(left);
1959 const IV leftiv = SvIVX(left);
1960 if (!SvUOK(right)) {
1961 /* ## IV <=> IV ## */
1962 const IV rightiv = SvIVX(right);
1963 return (leftiv > rightiv) - (leftiv < rightiv);
1965 /* ## IV <=> UV ## */
1967 /* As (b) is a UV, it's >=0, so it must be < */
1970 const UV rightuv = SvUVX(right);
1971 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
1976 /* ## UV <=> UV ## */
1977 const UV leftuv = SvUVX(left);
1978 const UV rightuv = SvUVX(right);
1979 return (leftuv > rightuv) - (leftuv < rightuv);
1981 /* ## UV <=> IV ## */
1983 const IV rightiv = SvIVX(right);
1985 /* As (a) is a UV, it's >=0, so it cannot be < */
1988 const UV leftuv = SvUVX(left);
1989 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
1997 NV const rnv = SvNV_nomg(right);
1998 NV const lnv = SvNV_nomg(left);
2000 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2001 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2004 return (lnv > rnv) - (lnv < rnv);
2023 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2026 value = do_ncmp(left, right);
2041 int amg_type = sle_amg;
2045 switch (PL_op->op_type) {
2064 tryAMAGICbin_MG(amg_type, AMGf_set);
2067 const int cmp = (IN_LOCALE_RUNTIME
2068 ? sv_cmp_locale_flags(left, right, 0)
2069 : sv_cmp_flags(left, right, 0));
2070 SETs(boolSV(cmp * multiplier < rhs));
2078 tryAMAGICbin_MG(seq_amg, AMGf_set);
2081 SETs(boolSV(sv_eq_flags(left, right, 0)));
2089 tryAMAGICbin_MG(sne_amg, AMGf_set);
2092 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2100 tryAMAGICbin_MG(scmp_amg, 0);
2103 const int cmp = (IN_LOCALE_RUNTIME
2104 ? sv_cmp_locale_flags(left, right, 0)
2105 : sv_cmp_flags(left, right, 0));
2113 dVAR; dSP; dATARGET;
2114 tryAMAGICbin_MG(band_amg, AMGf_assign);
2117 if (SvNIOKp(left) || SvNIOKp(right)) {
2118 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2119 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2120 if (PL_op->op_private & HINT_INTEGER) {
2121 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2125 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2128 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2129 if (right_ro_nonnum) SvNIOK_off(right);
2132 do_vop(PL_op->op_type, TARG, left, right);
2141 dVAR; dSP; dATARGET;
2142 const int op_type = PL_op->op_type;
2144 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2147 if (SvNIOKp(left) || SvNIOKp(right)) {
2148 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2149 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2150 if (PL_op->op_private & HINT_INTEGER) {
2151 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2152 const IV r = SvIV_nomg(right);
2153 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2157 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2158 const UV r = SvUV_nomg(right);
2159 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2162 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2163 if (right_ro_nonnum) SvNIOK_off(right);
2166 do_vop(op_type, TARG, left, right);
2176 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2178 SV * const sv = TOPs;
2179 const int flags = SvFLAGS(sv);
2181 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
2185 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2186 /* It's publicly an integer, or privately an integer-not-float */
2189 if (SvIVX(sv) == IV_MIN) {
2190 /* 2s complement assumption. */
2191 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2194 else if (SvUVX(sv) <= IV_MAX) {
2199 else if (SvIVX(sv) != IV_MIN) {
2203 #ifdef PERL_PRESERVE_IVUV
2211 SETn(-SvNV_nomg(sv));
2212 else if (SvPOKp(sv)) {
2214 const char * const s = SvPV_nomg_const(sv, len);
2215 if (isIDFIRST(*s)) {
2216 sv_setpvs(TARG, "-");
2219 else if (*s == '+' || *s == '-') {
2220 sv_setsv_nomg(TARG, sv);
2221 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2223 else if (DO_UTF8(sv)) {
2224 SvIV_please_nomg(sv);
2226 goto oops_its_an_int;
2228 sv_setnv(TARG, -SvNV_nomg(sv));
2230 sv_setpvs(TARG, "-");
2235 SvIV_please_nomg(sv);
2237 goto oops_its_an_int;
2238 sv_setnv(TARG, -SvNV_nomg(sv));
2243 SETn(-SvNV_nomg(sv));
2251 tryAMAGICun_MG(not_amg, AMGf_set);
2252 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2259 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2263 if (PL_op->op_private & HINT_INTEGER) {
2264 const IV i = ~SvIV_nomg(sv);
2268 const UV u = ~SvUV_nomg(sv);
2277 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2278 sv_setsv_nomg(TARG, sv);
2279 tmps = (U8*)SvPV_force_nomg(TARG, len);
2282 /* Calculate exact length, let's not estimate. */
2287 U8 * const send = tmps + len;
2288 U8 * const origtmps = tmps;
2289 const UV utf8flags = UTF8_ALLOW_ANYUV;
2291 while (tmps < send) {
2292 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2294 targlen += UNISKIP(~c);
2300 /* Now rewind strings and write them. */
2307 Newx(result, targlen + 1, U8);
2309 while (tmps < send) {
2310 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2312 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2315 sv_usepvn_flags(TARG, (char*)result, targlen,
2316 SV_HAS_TRAILING_NUL);
2323 Newx(result, nchar + 1, U8);
2325 while (tmps < send) {
2326 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2331 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2339 register long *tmpl;
2340 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2343 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2348 for ( ; anum > 0; anum--, tmps++)
2356 /* integer versions of some of the above */
2360 dVAR; dSP; dATARGET;
2361 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2364 SETi( left * right );
2372 dVAR; dSP; dATARGET;
2373 tryAMAGICbin_MG(div_amg, AMGf_assign);
2376 IV value = SvIV_nomg(right);
2378 DIE(aTHX_ "Illegal division by zero");
2379 num = SvIV_nomg(left);
2381 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2385 value = num / value;
2391 #if defined(__GLIBC__) && IVSIZE == 8
2398 /* This is the vanilla old i_modulo. */
2399 dVAR; dSP; dATARGET;
2400 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2404 DIE(aTHX_ "Illegal modulus zero");
2405 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2409 SETi( left % right );
2414 #if defined(__GLIBC__) && IVSIZE == 8
2419 /* This is the i_modulo with the workaround for the _moddi3 bug
2420 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2421 * See below for pp_i_modulo. */
2422 dVAR; dSP; dATARGET;
2423 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2427 DIE(aTHX_ "Illegal modulus zero");
2428 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2432 SETi( left % PERL_ABS(right) );
2439 dVAR; dSP; dATARGET;
2440 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2444 DIE(aTHX_ "Illegal modulus zero");
2445 /* The assumption is to use hereafter the old vanilla version... */
2447 PL_ppaddr[OP_I_MODULO] =
2449 /* .. but if we have glibc, we might have a buggy _moddi3
2450 * (at least glicb 2.2.5 is known to have this bug), in other
2451 * words our integer modulus with negative quad as the second
2452 * argument might be broken. Test for this and re-patch the
2453 * opcode dispatch table if that is the case, remembering to
2454 * also apply the workaround so that this first round works
2455 * right, too. See [perl #9402] for more information. */
2459 /* Cannot do this check with inlined IV constants since
2460 * that seems to work correctly even with the buggy glibc. */
2462 /* Yikes, we have the bug.
2463 * Patch in the workaround version. */
2465 PL_ppaddr[OP_I_MODULO] =
2466 &Perl_pp_i_modulo_1;
2467 /* Make certain we work right this time, too. */
2468 right = PERL_ABS(right);
2471 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2475 SETi( left % right );
2483 dVAR; dSP; dATARGET;
2484 tryAMAGICbin_MG(add_amg, AMGf_assign);
2486 dPOPTOPiirl_ul_nomg;
2487 SETi( left + right );
2494 dVAR; dSP; dATARGET;
2495 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2497 dPOPTOPiirl_ul_nomg;
2498 SETi( left - right );
2506 tryAMAGICbin_MG(lt_amg, AMGf_set);
2509 SETs(boolSV(left < right));
2517 tryAMAGICbin_MG(gt_amg, AMGf_set);
2520 SETs(boolSV(left > right));
2528 tryAMAGICbin_MG(le_amg, AMGf_set);
2531 SETs(boolSV(left <= right));
2539 tryAMAGICbin_MG(ge_amg, AMGf_set);
2542 SETs(boolSV(left >= right));
2550 tryAMAGICbin_MG(eq_amg, AMGf_set);
2553 SETs(boolSV(left == right));
2561 tryAMAGICbin_MG(ne_amg, AMGf_set);
2564 SETs(boolSV(left != right));
2572 tryAMAGICbin_MG(ncmp_amg, 0);
2579 else if (left < right)
2591 tryAMAGICun_MG(neg_amg, 0);
2593 SV * const sv = TOPs;
2594 IV const i = SvIV_nomg(sv);
2600 /* High falutin' math. */
2605 tryAMAGICbin_MG(atan2_amg, 0);
2608 SETn(Perl_atan2(left, right));
2616 int amg_type = sin_amg;
2617 const char *neg_report = NULL;
2618 NV (*func)(NV) = Perl_sin;
2619 const int op_type = PL_op->op_type;
2636 amg_type = sqrt_amg;
2638 neg_report = "sqrt";
2643 tryAMAGICun_MG(amg_type, 0);
2645 SV * const arg = POPs;
2646 const NV value = SvNV_nomg(arg);
2648 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2649 SET_NUMERIC_STANDARD();
2650 /* diag_listed_as: Can't take log of %g */
2651 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2654 XPUSHn(func(value));
2659 /* Support Configure command-line overrides for rand() functions.
2660 After 5.005, perhaps we should replace this by Configure support
2661 for drand48(), random(), or rand(). For 5.005, though, maintain
2662 compatibility by calling rand() but allow the user to override it.
2663 See INSTALL for details. --Andy Dougherty 15 July 1998
2665 /* Now it's after 5.005, and Configure supports drand48() and random(),
2666 in addition to rand(). So the overrides should not be needed any more.
2667 --Jarkko Hietaniemi 27 September 1998
2670 #ifndef HAS_DRAND48_PROTO
2671 extern double drand48 (void);
2681 value = 1.0; (void)POPs;
2687 if (!PL_srand_called) {
2688 (void)seedDrand01((Rand_seed_t)seed());
2689 PL_srand_called = TRUE;
2699 const UV anum = (MAXARG < 1 || (!TOPs && !POPs)) ? seed() : POPu;
2700 (void)seedDrand01((Rand_seed_t)anum);
2701 PL_srand_called = TRUE;
2705 /* Historically srand always returned true. We can avoid breaking
2707 sv_setpvs(TARG, "0 but true");
2716 tryAMAGICun_MG(int_amg, AMGf_numeric);
2718 SV * const sv = TOPs;
2719 const IV iv = SvIV_nomg(sv);
2720 /* XXX it's arguable that compiler casting to IV might be subtly
2721 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2722 else preferring IV has introduced a subtle behaviour change bug. OTOH
2723 relying on floating point to be accurate is a bug. */
2728 else if (SvIOK(sv)) {
2730 SETu(SvUV_nomg(sv));
2735 const NV value = SvNV_nomg(sv);
2737 if (value < (NV)UV_MAX + 0.5) {
2740 SETn(Perl_floor(value));
2744 if (value > (NV)IV_MIN - 0.5) {
2747 SETn(Perl_ceil(value));
2758 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2760 SV * const sv = TOPs;
2761 /* This will cache the NV value if string isn't actually integer */
2762 const IV iv = SvIV_nomg(sv);
2767 else if (SvIOK(sv)) {
2768 /* IVX is precise */
2770 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2778 /* 2s complement assumption. Also, not really needed as
2779 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2785 const NV value = SvNV_nomg(sv);
2799 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2803 SV* const sv = POPs;
2805 tmps = (SvPV_const(sv, len));
2807 /* If Unicode, try to downgrade
2808 * If not possible, croak. */
2809 SV* const tsv = sv_2mortal(newSVsv(sv));
2812 sv_utf8_downgrade(tsv, FALSE);
2813 tmps = SvPV_const(tsv, len);
2815 if (PL_op->op_type == OP_HEX)
2818 while (*tmps && len && isSPACE(*tmps))
2822 if (*tmps == 'x' || *tmps == 'X') {
2824 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2826 else if (*tmps == 'b' || *tmps == 'B')
2827 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2829 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2831 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2845 SV * const sv = TOPs;
2847 if (SvGAMAGIC(sv)) {
2848 /* For an overloaded or magic scalar, we can't know in advance if
2849 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2850 it likes to cache the length. Maybe that should be a documented
2855 = sv_2pv_flags(sv, &len,
2856 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2859 if (!SvPADTMP(TARG)) {
2860 sv_setsv(TARG, &PL_sv_undef);
2865 else if (DO_UTF8(sv)) {
2866 SETi(utf8_length((U8*)p, (U8*)p + len));
2870 } else if (SvOK(sv)) {
2871 /* Neither magic nor overloaded. */
2873 SETi(sv_len_utf8(sv));
2877 if (!SvPADTMP(TARG)) {
2878 sv_setsv_nomg(TARG, &PL_sv_undef);
2886 /* Returns false if substring is completely outside original string.
2887 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2888 always be true for an explicit 0.
2891 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2892 bool pos1_is_uv, IV len_iv,
2893 bool len_is_uv, STRLEN *posp,
2899 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2901 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2902 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2905 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2908 if (len_iv || len_is_uv) {
2909 if (!len_is_uv && len_iv < 0) {
2910 pos2_iv = curlen + len_iv;
2912 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2915 } else { /* len_iv >= 0 */
2916 if (!pos1_is_uv && pos1_iv < 0) {
2917 pos2_iv = pos1_iv + len_iv;
2918 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2920 if ((UV)len_iv > curlen-(UV)pos1_iv)
2923 pos2_iv = pos1_iv+len_iv;
2933 if (!pos2_is_uv && pos2_iv < 0) {
2934 if (!pos1_is_uv && pos1_iv < 0)
2938 else if (!pos1_is_uv && pos1_iv < 0)
2941 if ((UV)pos2_iv < (UV)pos1_iv)
2943 if ((UV)pos2_iv > curlen)
2946 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2947 *posp = (STRLEN)( (UV)pos1_iv );
2948 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2965 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2966 const bool rvalue = (GIMME_V != G_VOID);
2969 const char *repl = NULL;
2971 int num_args = PL_op->op_private & 7;
2972 bool repl_need_utf8_upgrade = FALSE;
2973 bool repl_is_utf8 = FALSE;
2977 if(!(repl_sv = POPs)) num_args--;
2979 if ((len_sv = POPs)) {
2980 len_iv = SvIV(len_sv);
2981 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
2986 pos1_iv = SvIV(pos_sv);
2987 pos1_is_uv = SvIOK_UV(pos_sv);
2989 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
2995 repl = SvPV_const(repl_sv, repl_len);
2996 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
2999 sv_utf8_upgrade(sv);
3001 else if (DO_UTF8(sv))
3002 repl_need_utf8_upgrade = TRUE;
3006 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3007 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3009 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3011 pos1_is_uv || pos1_iv >= 0
3012 ? (STRLEN)(UV)pos1_iv
3013 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3015 len_is_uv || len_iv > 0
3016 ? (STRLEN)(UV)len_iv
3017 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3020 PUSHs(ret); /* avoid SvSETMAGIC here */
3023 tmps = SvPV_const(sv, curlen);
3025 utf8_curlen = sv_len_utf8(sv);
3026 if (utf8_curlen == curlen)
3029 curlen = utf8_curlen;
3035 STRLEN pos, len, byte_len, byte_pos;
3037 if (!translate_substr_offsets(
3038 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3042 byte_pos = utf8_curlen
3043 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3048 SvTAINTED_off(TARG); /* decontaminate */
3049 SvUTF8_off(TARG); /* decontaminate */
3050 sv_setpvn(TARG, tmps, byte_len);
3051 #ifdef USE_LOCALE_COLLATE
3052 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3059 SV* repl_sv_copy = NULL;
3061 if (repl_need_utf8_upgrade) {
3062 repl_sv_copy = newSVsv(repl_sv);
3063 sv_utf8_upgrade(repl_sv_copy);
3064 repl = SvPV_const(repl_sv_copy, repl_len);
3065 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3068 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3069 "Attempt to use reference as lvalue in substr"
3073 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3076 SvREFCNT_dec(repl_sv_copy);
3088 Perl_croak(aTHX_ "substr outside of string");
3089 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3096 register const IV size = POPi;
3097 register const IV offset = POPi;
3098 register SV * const src = POPs;
3099 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3102 if (lvalue) { /* it's an lvalue! */
3103 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3104 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3106 LvTARG(ret) = SvREFCNT_inc_simple(src);
3107 LvTARGOFF(ret) = offset;
3108 LvTARGLEN(ret) = size;
3112 SvTAINTED_off(TARG); /* decontaminate */
3116 sv_setuv(ret, do_vecget(src, offset, size));
3132 const char *little_p;
3135 const bool is_index = PL_op->op_type == OP_INDEX;
3136 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3142 big_p = SvPV_const(big, biglen);
3143 little_p = SvPV_const(little, llen);
3145 big_utf8 = DO_UTF8(big);
3146 little_utf8 = DO_UTF8(little);
3147 if (big_utf8 ^ little_utf8) {
3148 /* One needs to be upgraded. */
3149 if (little_utf8 && !PL_encoding) {
3150 /* Well, maybe instead we might be able to downgrade the small
3152 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3155 /* If the large string is ISO-8859-1, and it's not possible to
3156 convert the small string to ISO-8859-1, then there is no
3157 way that it could be found anywhere by index. */
3162 /* At this point, pv is a malloc()ed string. So donate it to temp
3163 to ensure it will get free()d */
3164 little = temp = newSV(0);
3165 sv_usepvn(temp, pv, llen);
3166 little_p = SvPVX(little);
3169 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3172 sv_recode_to_utf8(temp, PL_encoding);
3174 sv_utf8_upgrade(temp);
3179 big_p = SvPV_const(big, biglen);
3182 little_p = SvPV_const(little, llen);
3186 if (SvGAMAGIC(big)) {
3187 /* Life just becomes a lot easier if I use a temporary here.
3188 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3189 will trigger magic and overloading again, as will fbm_instr()
3191 big = newSVpvn_flags(big_p, biglen,
3192 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3195 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3196 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3197 warn on undef, and we've already triggered a warning with the
3198 SvPV_const some lines above. We can't remove that, as we need to
3199 call some SvPV to trigger overloading early and find out if the
3201 This is all getting to messy. The API isn't quite clean enough,
3202 because data access has side effects.
3204 little = newSVpvn_flags(little_p, llen,
3205 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3206 little_p = SvPVX(little);
3210 offset = is_index ? 0 : biglen;
3212 if (big_utf8 && offset > 0)
3213 sv_pos_u2b(big, &offset, 0);
3219 else if (offset > (I32)biglen)
3221 if (!(little_p = is_index
3222 ? fbm_instr((unsigned char*)big_p + offset,
3223 (unsigned char*)big_p + biglen, little, 0)
3224 : rninstr(big_p, big_p + offset,
3225 little_p, little_p + llen)))
3228 retval = little_p - big_p;
3229 if (retval > 0 && big_utf8)
3230 sv_pos_b2u(big, &retval);
3240 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3241 SvTAINTED_off(TARG);
3242 do_sprintf(TARG, SP-MARK, MARK+1);
3243 TAINT_IF(SvTAINTED(TARG));
3255 const U8 *s = (U8*)SvPV_const(argsv, len);
3257 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3258 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3259 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3263 XPUSHu(DO_UTF8(argsv) ?
3264 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3276 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3278 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3280 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3282 (void) POPs; /* Ignore the argument value. */
3283 value = UNICODE_REPLACEMENT;
3289 SvUPGRADE(TARG,SVt_PV);
3291 if (value > 255 && !IN_BYTES) {
3292 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3293 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3294 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3296 (void)SvPOK_only(TARG);
3305 *tmps++ = (char)value;
3307 (void)SvPOK_only(TARG);
3309 if (PL_encoding && !IN_BYTES) {
3310 sv_recode_to_utf8(TARG, PL_encoding);
3312 if (SvCUR(TARG) == 0
3313 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3314 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3319 *tmps++ = (char)value;
3335 const char *tmps = SvPV_const(left, len);
3337 if (DO_UTF8(left)) {
3338 /* If Unicode, try to downgrade.
3339 * If not possible, croak.
3340 * Yes, we made this up. */
3341 SV* const tsv = sv_2mortal(newSVsv(left));
3344 sv_utf8_downgrade(tsv, FALSE);
3345 tmps = SvPV_const(tsv, len);
3347 # ifdef USE_ITHREADS
3349 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3350 /* This should be threadsafe because in ithreads there is only
3351 * one thread per interpreter. If this would not be true,
3352 * we would need a mutex to protect this malloc. */
3353 PL_reentrant_buffer->_crypt_struct_buffer =
3354 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3355 #if defined(__GLIBC__) || defined(__EMX__)
3356 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3357 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3358 /* work around glibc-2.2.5 bug */
3359 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3363 # endif /* HAS_CRYPT_R */
3364 # endif /* USE_ITHREADS */
3366 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3368 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3374 "The crypt() function is unimplemented due to excessive paranoia.");
3378 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3379 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3381 /* Generates code to store a unicode codepoint c that is known to occupy
3382 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3383 * and p is advanced to point to the next available byte after the two bytes */
3384 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3386 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3387 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3392 /* Actually is both lcfirst() and ucfirst(). Only the first character
3393 * changes. This means that possibly we can change in-place, ie., just
3394 * take the source and change that one character and store it back, but not
3395 * if read-only etc, or if the length changes */
3400 STRLEN slen; /* slen is the byte length of the whole SV. */
3403 bool inplace; /* ? Convert first char only, in-place */
3404 bool doing_utf8 = FALSE; /* ? using utf8 */
3405 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3406 const int op_type = PL_op->op_type;
3409 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3410 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3411 * stored as UTF-8 at s. */
3412 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3413 * lowercased) character stored in tmpbuf. May be either
3414 * UTF-8 or not, but in either case is the number of bytes */
3415 bool tainted = FALSE;
3419 s = (const U8*)SvPV_nomg_const(source, slen);
3421 if (ckWARN(WARN_UNINITIALIZED))
3422 report_uninit(source);
3427 /* We may be able to get away with changing only the first character, in
3428 * place, but not if read-only, etc. Later we may discover more reasons to
3429 * not convert in-place. */
3430 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3432 /* First calculate what the changed first character should be. This affects
3433 * whether we can just swap it out, leaving the rest of the string unchanged,
3434 * or even if have to convert the dest to UTF-8 when the source isn't */
3436 if (! slen) { /* If empty */
3437 need = 1; /* still need a trailing NUL */
3440 else if (DO_UTF8(source)) { /* Is the source utf8? */
3443 if (op_type == OP_UCFIRST) {
3444 _to_utf8_title_flags(s, tmpbuf, &tculen,
3445 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3448 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3449 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3452 /* we can't do in-place if the length changes. */
3453 if (ulen != tculen) inplace = FALSE;
3454 need = slen + 1 - ulen + tculen;
3456 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3457 * latin1 is treated as caseless. Note that a locale takes
3459 ulen = 1; /* Original character is 1 byte */
3460 tculen = 1; /* Most characters will require one byte, but this will
3461 * need to be overridden for the tricky ones */
3464 if (op_type == OP_LCFIRST) {
3466 /* lower case the first letter: no trickiness for any character */
3467 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3468 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3471 else if (IN_LOCALE_RUNTIME) {
3472 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3473 * have upper and title case different
3476 else if (! IN_UNI_8_BIT) {
3477 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3478 * on EBCDIC machines whatever the
3479 * native function does */
3481 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3482 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3484 assert(tculen == 2);
3486 /* If the result is an upper Latin1-range character, it can
3487 * still be represented in one byte, which is its ordinal */
3488 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3489 *tmpbuf = (U8) title_ord;
3493 /* Otherwise it became more than one ASCII character (in
3494 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3495 * beyond Latin1, so the number of bytes changed, so can't
3496 * replace just the first character in place. */
3499 /* If the result won't fit in a byte, the entire result will
3500 * have to be in UTF-8. Assume worst case sizing in
3501 * conversion. (all latin1 characters occupy at most two bytes
3503 if (title_ord > 255) {
3505 convert_source_to_utf8 = TRUE;
3506 need = slen * 2 + 1;
3508 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3509 * (both) characters whose title case is above 255 is
3513 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3514 need = slen + 1 + 1;
3518 } /* End of use Unicode (Latin1) semantics */
3519 } /* End of changing the case of the first character */
3521 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3522 * generate the result */
3525 /* We can convert in place. This means we change just the first
3526 * character without disturbing the rest; no need to grow */
3528 s = d = (U8*)SvPV_force_nomg(source, slen);
3534 /* Here, we can't convert in place; we earlier calculated how much
3535 * space we will need, so grow to accommodate that */
3536 SvUPGRADE(dest, SVt_PV);
3537 d = (U8*)SvGROW(dest, need);
3538 (void)SvPOK_only(dest);
3545 if (! convert_source_to_utf8) {
3547 /* Here both source and dest are in UTF-8, but have to create
3548 * the entire output. We initialize the result to be the
3549 * title/lower cased first character, and then append the rest
3551 sv_setpvn(dest, (char*)tmpbuf, tculen);
3553 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3557 const U8 *const send = s + slen;
3559 /* Here the dest needs to be in UTF-8, but the source isn't,
3560 * except we earlier UTF-8'd the first character of the source
3561 * into tmpbuf. First put that into dest, and then append the
3562 * rest of the source, converting it to UTF-8 as we go. */
3564 /* Assert tculen is 2 here because the only two characters that
3565 * get to this part of the code have 2-byte UTF-8 equivalents */
3567 *d++ = *(tmpbuf + 1);
3568 s++; /* We have just processed the 1st char */
3570 for (; s < send; s++) {
3571 d = uvchr_to_utf8(d, *s);
3574 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3578 else { /* in-place UTF-8. Just overwrite the first character */
3579 Copy(tmpbuf, d, tculen, U8);
3580 SvCUR_set(dest, need - 1);
3588 else { /* Neither source nor dest are in or need to be UTF-8 */
3590 if (IN_LOCALE_RUNTIME) {
3594 if (inplace) { /* in-place, only need to change the 1st char */
3597 else { /* Not in-place */
3599 /* Copy the case-changed character(s) from tmpbuf */
3600 Copy(tmpbuf, d, tculen, U8);
3601 d += tculen - 1; /* Code below expects d to point to final
3602 * character stored */
3605 else { /* empty source */
3606 /* See bug #39028: Don't taint if empty */
3610 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3611 * the destination to retain that flag */
3615 if (!inplace) { /* Finish the rest of the string, unchanged */
3616 /* This will copy the trailing NUL */
3617 Copy(s + 1, d + 1, slen, U8);
3618 SvCUR_set(dest, need - 1);
3621 if (dest != source && SvTAINTED(source))
3627 /* There's so much setup/teardown code common between uc and lc, I wonder if
3628 it would be worth merging the two, and just having a switch outside each
3629 of the three tight loops. There is less and less commonality though */
3643 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3644 && SvTEMP(source) && !DO_UTF8(source)
3645 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3647 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3648 * make the loop tight, so we overwrite the source with the dest before
3649 * looking at it, and we need to look at the original source
3650 * afterwards. There would also need to be code added to handle
3651 * switching to not in-place in midstream if we run into characters
3652 * that change the length.
3655 s = d = (U8*)SvPV_force_nomg(source, len);
3662 /* The old implementation would copy source into TARG at this point.
3663 This had the side effect that if source was undef, TARG was now
3664 an undefined SV with PADTMP set, and they don't warn inside
3665 sv_2pv_flags(). However, we're now getting the PV direct from
3666 source, which doesn't have PADTMP set, so it would warn. Hence the
3670 s = (const U8*)SvPV_nomg_const(source, len);
3672 if (ckWARN(WARN_UNINITIALIZED))
3673 report_uninit(source);
3679 SvUPGRADE(dest, SVt_PV);
3680 d = (U8*)SvGROW(dest, min);
3681 (void)SvPOK_only(dest);
3686 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3687 to check DO_UTF8 again here. */
3689 if (DO_UTF8(source)) {
3690 const U8 *const send = s + len;
3691 U8 tmpbuf[UTF8_MAXBYTES+1];
3692 bool tainted = FALSE;
3694 /* All occurrences of these are to be moved to follow any other marks.
3695 * This is context-dependent. We may not be passed enough context to
3696 * move the iota subscript beyond all of them, but we do the best we can
3697 * with what we're given. The result is always better than if we
3698 * hadn't done this. And, the problem would only arise if we are
3699 * passed a character without all its combining marks, which would be
3700 * the caller's mistake. The information this is based on comes from a
3701 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3702 * itself) and so can't be checked properly to see if it ever gets
3703 * revised. But the likelihood of it changing is remote */
3704 bool in_iota_subscript = FALSE;
3710 if (in_iota_subscript && ! is_utf8_mark(s)) {
3712 /* A non-mark. Time to output the iota subscript */
3713 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3714 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3716 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3717 in_iota_subscript = FALSE;
3720 /* Then handle the current character. Get the changed case value
3721 * and copy it to the output buffer */
3724 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3725 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3726 if (uv == GREEK_CAPITAL_LETTER_IOTA
3727 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3729 in_iota_subscript = TRUE;
3732 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3733 /* If the eventually required minimum size outgrows the
3734 * available space, we need to grow. */
3735 const UV o = d - (U8*)SvPVX_const(dest);
3737 /* If someone uppercases one million U+03B0s we SvGROW()
3738 * one million times. Or we could try guessing how much to
3739 * allocate without allocating too much. Such is life.
3740 * See corresponding comment in lc code for another option
3743 d = (U8*)SvPVX(dest) + o;
3745 Copy(tmpbuf, d, ulen, U8);
3750 if (in_iota_subscript) {
3751 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3756 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3762 else { /* Not UTF-8 */
3764 const U8 *const send = s + len;
3766 /* Use locale casing if in locale; regular style if not treating
3767 * latin1 as having case; otherwise the latin1 casing. Do the
3768 * whole thing in a tight loop, for speed, */
3769 if (IN_LOCALE_RUNTIME) {
3772 for (; s < send; d++, s++)
3773 *d = toUPPER_LC(*s);
3775 else if (! IN_UNI_8_BIT) {
3776 for (; s < send; d++, s++) {
3781 for (; s < send; d++, s++) {
3782 *d = toUPPER_LATIN1_MOD(*s);
3783 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3785 /* The mainstream case is the tight loop above. To avoid
3786 * extra tests in that, all three characters that require
3787 * special handling are mapped by the MOD to the one tested
3789 * Use the source to distinguish between the three cases */
3791 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3793 /* uc() of this requires 2 characters, but they are
3794 * ASCII. If not enough room, grow the string */
3795 if (SvLEN(dest) < ++min) {
3796 const UV o = d - (U8*)SvPVX_const(dest);
3798 d = (U8*)SvPVX(dest) + o;
3800 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3801 continue; /* Back to the tight loop; still in ASCII */
3804 /* The other two special handling characters have their
3805 * upper cases outside the latin1 range, hence need to be
3806 * in UTF-8, so the whole result needs to be in UTF-8. So,
3807 * here we are somewhere in the middle of processing a
3808 * non-UTF-8 string, and realize that we will have to convert
3809 * the whole thing to UTF-8. What to do? There are
3810 * several possibilities. The simplest to code is to
3811 * convert what we have so far, set a flag, and continue on
3812 * in the loop. The flag would be tested each time through
3813 * the loop, and if set, the next character would be
3814 * converted to UTF-8 and stored. But, I (khw) didn't want
3815 * to slow down the mainstream case at all for this fairly
3816 * rare case, so I didn't want to add a test that didn't
3817 * absolutely have to be there in the loop, besides the
3818 * possibility that it would get too complicated for
3819 * optimizers to deal with. Another possibility is to just
3820 * give up, convert the source to UTF-8, and restart the
3821 * function that way. Another possibility is to convert
3822 * both what has already been processed and what is yet to
3823 * come separately to UTF-8, then jump into the loop that
3824 * handles UTF-8. But the most efficient time-wise of the
3825 * ones I could think of is what follows, and turned out to
3826 * not require much extra code. */
3828 /* Convert what we have so far into UTF-8, telling the
3829 * function that we know it should be converted, and to
3830 * allow extra space for what we haven't processed yet.
3831 * Assume the worst case space requirements for converting
3832 * what we haven't processed so far: that it will require
3833 * two bytes for each remaining source character, plus the
3834 * NUL at the end. This may cause the string pointer to
3835 * move, so re-find it. */
3837 len = d - (U8*)SvPVX_const(dest);
3838 SvCUR_set(dest, len);
3839 len = sv_utf8_upgrade_flags_grow(dest,
3840 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3842 d = (U8*)SvPVX(dest) + len;
3844 /* Now process the remainder of the source, converting to
3845 * upper and UTF-8. If a resulting byte is invariant in
3846 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3847 * append it to the output. */
3848 for (; s < send; s++) {
3849 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3853 /* Here have processed the whole source; no need to continue
3854 * with the outer loop. Each character has been converted
3855 * to upper case and converted to UTF-8 */
3858 } /* End of processing all latin1-style chars */
3859 } /* End of processing all chars */
3860 } /* End of source is not empty */
3862 if (source != dest) {
3863 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3864 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3866 } /* End of isn't utf8 */
3867 if (dest != source && SvTAINTED(source))
3886 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3887 && SvTEMP(source) && !DO_UTF8(source)) {
3889 /* We can convert in place, as lowercasing anything in the latin1 range
3890 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3892 s = d = (U8*)SvPV_force_nomg(source, len);
3899 /* The old implementation would copy source into TARG at this point.
3900 This had the side effect that if source was undef, TARG was now
3901 an undefined SV with PADTMP set, and they don't warn inside
3902 sv_2pv_flags(). However, we're now getting the PV direct from
3903 source, which doesn't have PADTMP set, so it would warn. Hence the
3907 s = (const U8*)SvPV_nomg_const(source, len);
3909 if (ckWARN(WARN_UNINITIALIZED))
3910 report_uninit(source);
3916 SvUPGRADE(dest, SVt_PV);
3917 d = (U8*)SvGROW(dest, min);
3918 (void)SvPOK_only(dest);
3923 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3924 to check DO_UTF8 again here. */
3926 if (DO_UTF8(source)) {
3927 const U8 *const send = s + len;
3928 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3929 bool tainted = FALSE;
3932 const STRLEN u = UTF8SKIP(s);
3935 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3936 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3938 /* Here is where we would do context-sensitive actions. See the
3939 * commit message for this comment for why there isn't any */
3941 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3943 /* If the eventually required minimum size outgrows the
3944 * available space, we need to grow. */
3945 const UV o = d - (U8*)SvPVX_const(dest);
3947 /* If someone lowercases one million U+0130s we SvGROW() one
3948 * million times. Or we could try guessing how much to
3949 * allocate without allocating too much. Such is life.
3950 * Another option would be to grow an extra byte or two more
3951 * each time we need to grow, which would cut down the million
3952 * to 500K, with little waste */
3954 d = (U8*)SvPVX(dest) + o;
3957 /* Copy the newly lowercased letter to the output buffer we're
3959 Copy(tmpbuf, d, ulen, U8);
3962 } /* End of looping through the source string */
3965 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3970 } else { /* Not utf8 */
3972 const U8 *const send = s + len;
3974 /* Use locale casing if in locale; regular style if not treating
3975 * latin1 as having case; otherwise the latin1 casing. Do the
3976 * whole thing in a tight loop, for speed, */
3977 if (IN_LOCALE_RUNTIME) {
3980 for (; s < send; d++, s++)
3981 *d = toLOWER_LC(*s);
3983 else if (! IN_UNI_8_BIT) {
3984 for (; s < send; d++, s++) {
3989 for (; s < send; d++, s++) {
3990 *d = toLOWER_LATIN1(*s);
3994 if (source != dest) {
3996 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3999 if (dest != source && SvTAINTED(source))
4008 SV * const sv = TOPs;
4010 register const char *s = SvPV_const(sv,len);
4012 SvUTF8_off(TARG); /* decontaminate */
4015 SvUPGRADE(TARG, SVt_PV);
4016 SvGROW(TARG, (len * 2) + 1);
4020 STRLEN ulen = UTF8SKIP(s);
4021 bool to_quote = FALSE;
4023 if (UTF8_IS_INVARIANT(*s)) {
4024 if (_isQUOTEMETA(*s)) {
4028 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4030 /* In locale, we quote all non-ASCII Latin1 chars.
4031 * Otherwise use the quoting rules */
4032 if (IN_LOCALE_RUNTIME
4033 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4038 else if (_is_utf8_quotemeta((U8 *) s)) {
4053 else if (IN_UNI_8_BIT) {
4055 if (_isQUOTEMETA(*s))
4061 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4062 * including everything above ASCII */
4064 if (!isWORDCHAR_A(*s))
4070 SvCUR_set(TARG, d - SvPVX_const(TARG));
4071 (void)SvPOK_only_UTF8(TARG);
4074 sv_setpvn(TARG, s, len);
4091 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4092 const bool full_folding = TRUE;
4093 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4094 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4096 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4097 * You are welcome(?) -Hugmeir
4105 s = (const U8*)SvPV_nomg_const(source, len);
4107 if (ckWARN(WARN_UNINITIALIZED))
4108 report_uninit(source);
4115 SvUPGRADE(dest, SVt_PV);
4116 d = (U8*)SvGROW(dest, min);
4117 (void)SvPOK_only(dest);
4122 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4123 bool tainted = FALSE;
4125 const STRLEN u = UTF8SKIP(s);
4128 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4130 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4131 const UV o = d - (U8*)SvPVX_const(dest);
4133 d = (U8*)SvPVX(dest) + o;
4136 Copy(tmpbuf, d, ulen, U8);
4145 } /* Unflagged string */
4147 /* For locale, bytes, and nothing, the behavior is supposed to be the
4150 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4153 for (; s < send; d++, s++)
4154 *d = toLOWER_LC(*s);
4156 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4157 for (; s < send; d++, s++)
4161 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4162 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4163 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4164 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4167 for (; s < send; d++, s++) {
4168 if (*s == MICRO_SIGN) {
4169 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4170 * is outside of the latin-1 range. There's a couple of ways to
4171 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4172 * What we do here is upgrade what we had already casefolded,
4173 * then enter an inner loop that appends the rest of the characters
4176 len = d - (U8*)SvPVX_const(dest);
4177 SvCUR_set(dest, len);
4178 len = sv_utf8_upgrade_flags_grow(dest,
4179 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4180 /* The max expansion for latin1
4181 * chars is 1 byte becomes 2 */
4183 d = (U8*)SvPVX(dest) + len;
4185 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4187 for (; s < send; s++) {
4189 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4190 if UNI_IS_INVARIANT(fc) {
4191 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4199 Copy(tmpbuf, d, ulen, U8);
4205 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4206 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4207 * which may require growing the SV.
4209 if (SvLEN(dest) < ++min) {
4210 const UV o = d - (U8*)SvPVX_const(dest);
4212 d = (U8*)SvPVX(dest) + o;
4217 else { /* If it's not one of those two, the fold is their lower case */
4218 *d = toLOWER_LATIN1(*s);
4224 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4226 if (SvTAINTED(source))
4236 dVAR; dSP; dMARK; dORIGMARK;
4237 register AV *const av = MUTABLE_AV(POPs);
4238 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4240 if (SvTYPE(av) == SVt_PVAV) {
4241 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4242 bool can_preserve = FALSE;
4248 can_preserve = SvCANEXISTDELETE(av);
4251 if (lval && localizing) {
4254 for (svp = MARK + 1; svp <= SP; svp++) {
4255 const I32 elem = SvIV(*svp);
4259 if (max > AvMAX(av))
4263 while (++MARK <= SP) {
4265 I32 elem = SvIV(*MARK);
4266 bool preeminent = TRUE;
4268 if (localizing && can_preserve) {
4269 /* If we can determine whether the element exist,
4270 * Try to preserve the existenceness of a tied array
4271 * element by using EXISTS and DELETE if possible.
4272 * Fallback to FETCH and STORE otherwise. */
4273 preeminent = av_exists(av, elem);
4276 svp = av_fetch(av, elem, lval);
4278 if (!svp || *svp == &PL_sv_undef)
4279 DIE(aTHX_ PL_no_aelem, elem);
4282 save_aelem(av, elem, svp);
4284 SAVEADELETE(av, elem);
4287 *MARK = svp ? *svp : &PL_sv_undef;
4290 if (GIMME != G_ARRAY) {
4292 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4298 /* Smart dereferencing for keys, values and each */
4310 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4315 "Type of argument to %s must be unblessed hashref or arrayref",
4316 PL_op_desc[PL_op->op_type] );
4319 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4321 "Can't modify %s in %s",
4322 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4325 /* Delegate to correct function for op type */
4327 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4328 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4331 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4339 AV *array = MUTABLE_AV(POPs);
4340 const I32 gimme = GIMME_V;
4341 IV *iterp = Perl_av_iter_p(aTHX_ array);
4342 const IV current = (*iterp)++;
4344 if (current > av_len(array)) {
4346 if (gimme == G_SCALAR)
4354 if (gimme == G_ARRAY) {
4355 SV **const element = av_fetch(array, current, 0);
4356 PUSHs(element ? *element : &PL_sv_undef);
4365 AV *array = MUTABLE_AV(POPs);
4366 const I32 gimme = GIMME_V;
4368 *Perl_av_iter_p(aTHX_ array) = 0;
4370 if (gimme == G_SCALAR) {
4372 PUSHi(av_len(array) + 1);
4374 else if (gimme == G_ARRAY) {
4375 IV n = Perl_av_len(aTHX_ array);
4380 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4381 for (i = 0; i <= n; i++) {
4386 for (i = 0; i <= n; i++) {
4387 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4388 PUSHs(elem ? *elem : &PL_sv_undef);
4395 /* Associative arrays. */
4401 HV * hash = MUTABLE_HV(POPs);
4403 const I32 gimme = GIMME_V;
4406 /* might clobber stack_sp */
4407 entry = hv_iternext(hash);
4412 SV* const sv = hv_iterkeysv(entry);
4413 PUSHs(sv); /* won't clobber stack_sp */
4414 if (gimme == G_ARRAY) {
4417 /* might clobber stack_sp */
4418 val = hv_iterval(hash, entry);
4423 else if (gimme == G_SCALAR)
4430 S_do_delete_local(pTHX)
4434 const I32 gimme = GIMME_V;
4438 if (PL_op->op_private & OPpSLICE) {
4440 SV * const osv = POPs;
4441 const bool tied = SvRMAGICAL(osv)
4442 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4443 const bool can_preserve = SvCANEXISTDELETE(osv)
4444 || mg_find((const SV *)osv, PERL_MAGIC_env);
4445 const U32 type = SvTYPE(osv);
4446 if (type == SVt_PVHV) { /* hash element */
4447 HV * const hv = MUTABLE_HV(osv);
4448 while (++MARK <= SP) {
4449 SV * const keysv = *MARK;
4451 bool preeminent = TRUE;
4453 preeminent = hv_exists_ent(hv, keysv, 0);
4455 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4462 sv = hv_delete_ent(hv, keysv, 0, 0);
4463 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4466 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4468 *MARK = sv_mortalcopy(sv);
4474 SAVEHDELETE(hv, keysv);
4475 *MARK = &PL_sv_undef;
4479 else if (type == SVt_PVAV) { /* array element */
4480 if (PL_op->op_flags & OPf_SPECIAL) {
4481 AV * const av = MUTABLE_AV(osv);
4482 while (++MARK <= SP) {
4483 I32 idx = SvIV(*MARK);
4485 bool preeminent = TRUE;
4487 preeminent = av_exists(av, idx);
4489 SV **svp = av_fetch(av, idx, 1);
4496 sv = av_delete(av, idx, 0);
4497 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4500 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4502 *MARK = sv_mortalcopy(sv);
4508 SAVEADELETE(av, idx);
4509 *MARK = &PL_sv_undef;
4515 DIE(aTHX_ "Not a HASH reference");
4516 if (gimme == G_VOID)
4518 else if (gimme == G_SCALAR) {
4523 *++MARK = &PL_sv_undef;
4528 SV * const keysv = POPs;
4529 SV * const osv = POPs;
4530 const bool tied = SvRMAGICAL(osv)
4531 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4532 const bool can_preserve = SvCANEXISTDELETE(osv)
4533 || mg_find((const SV *)osv, PERL_MAGIC_env);
4534 const U32 type = SvTYPE(osv);
4536 if (type == SVt_PVHV) {
4537 HV * const hv = MUTABLE_HV(osv);
4538 bool preeminent = TRUE;
4540 preeminent = hv_exists_ent(hv, keysv, 0);
4542 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4549 sv = hv_delete_ent(hv, keysv, 0, 0);
4550 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4553 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4555 SV *nsv = sv_mortalcopy(sv);
4561 SAVEHDELETE(hv, keysv);
4563 else if (type == SVt_PVAV) {
4564 if (PL_op->op_flags & OPf_SPECIAL) {
4565 AV * const av = MUTABLE_AV(osv);
4566 I32 idx = SvIV(keysv);
4567 bool preeminent = TRUE;
4569 preeminent = av_exists(av, idx);
4571 SV **svp = av_fetch(av, idx, 1);
4578 sv = av_delete(av, idx, 0);
4579 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4582 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4584 SV *nsv = sv_mortalcopy(sv);
4590 SAVEADELETE(av, idx);
4593 DIE(aTHX_ "panic: avhv_delete no longer supported");
4596 DIE(aTHX_ "Not a HASH reference");
4599 if (gimme != G_VOID)
4613 if (PL_op->op_private & OPpLVAL_INTRO)
4614 return do_delete_local();
4617 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4619 if (PL_op->op_private & OPpSLICE) {
4621 HV * const hv = MUTABLE_HV(POPs);
4622 const U32 hvtype = SvTYPE(hv);
4623 if (hvtype == SVt_PVHV) { /* hash element */
4624 while (++MARK <= SP) {
4625 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4626 *MARK = sv ? sv : &PL_sv_undef;
4629 else if (hvtype == SVt_PVAV) { /* array element */
4630 if (PL_op->op_flags & OPf_SPECIAL) {
4631 while (++MARK <= SP) {
4632 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4633 *MARK = sv ? sv : &PL_sv_undef;
4638 DIE(aTHX_ "Not a HASH reference");
4641 else if (gimme == G_SCALAR) {
4646 *++MARK = &PL_sv_undef;
4652 HV * const hv = MUTABLE_HV(POPs);
4654 if (SvTYPE(hv) == SVt_PVHV)
4655 sv = hv_delete_ent(hv, keysv, discard, 0);
4656 else if (SvTYPE(hv) == SVt_PVAV) {
4657 if (PL_op->op_flags & OPf_SPECIAL)
4658 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4660 DIE(aTHX_ "panic: avhv_delete no longer supported");
4663 DIE(aTHX_ "Not a HASH reference");
4679 if (PL_op->op_private & OPpEXISTS_SUB) {
4681 SV * const sv = POPs;
4682 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4685 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4690 hv = MUTABLE_HV(POPs);
4691 if (SvTYPE(hv) == SVt_PVHV) {
4692 if (hv_exists_ent(hv, tmpsv, 0))
4695 else if (SvTYPE(hv) == SVt_PVAV) {
4696 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4697 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4702 DIE(aTHX_ "Not a HASH reference");
4709 dVAR; dSP; dMARK; dORIGMARK;
4710 register HV * const hv = MUTABLE_HV(POPs);
4711 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4712 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4713 bool can_preserve = FALSE;
4719 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4720 can_preserve = TRUE;
4723 while (++MARK <= SP) {
4724 SV * const keysv = *MARK;
4727 bool preeminent = TRUE;
4729 if (localizing && can_preserve) {
4730 /* If we can determine whether the element exist,
4731 * try to preserve the existenceness of a tied hash
4732 * element by using EXISTS and DELETE if possible.
4733 * Fallback to FETCH and STORE otherwise. */
4734 preeminent = hv_exists_ent(hv, keysv, 0);
4737 he = hv_fetch_ent(hv, keysv, lval, 0);
4738 svp = he ? &HeVAL(he) : NULL;
4741 if (!svp || !*svp || *svp == &PL_sv_undef) {
4742 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4745 if (HvNAME_get(hv) && isGV(*svp))
4746 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4747 else if (preeminent)
4748 save_helem_flags(hv, keysv, svp,
4749 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4751 SAVEHDELETE(hv, keysv);
4754 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4756 if (GIMME != G_ARRAY) {
4758 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4764 /* List operators. */
4769 if (GIMME != G_ARRAY) {
4771 *MARK = *SP; /* unwanted list, return last item */
4773 *MARK = &PL_sv_undef;
4783 SV ** const lastrelem = PL_stack_sp;
4784 SV ** const lastlelem = PL_stack_base + POPMARK;
4785 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4786 register SV ** const firstrelem = lastlelem + 1;
4787 I32 is_something_there = FALSE;
4789 register const I32 max = lastrelem - lastlelem;
4790 register SV **lelem;
4792 if (GIMME != G_ARRAY) {
4793 I32 ix = SvIV(*lastlelem);
4796 if (ix < 0 || ix >= max)
4797 *firstlelem = &PL_sv_undef;
4799 *firstlelem = firstrelem[ix];
4805 SP = firstlelem - 1;
4809 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4810 I32 ix = SvIV(*lelem);
4813 if (ix < 0 || ix >= max)
4814 *lelem = &PL_sv_undef;
4816 is_something_there = TRUE;
4817 if (!(*lelem = firstrelem[ix]))
4818 *lelem = &PL_sv_undef;
4821 if (is_something_there)
4824 SP = firstlelem - 1;
4830 dVAR; dSP; dMARK; dORIGMARK;
4831 const I32 items = SP - MARK;
4832 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4833 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4834 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4835 ? newRV_noinc(av) : av);
4841 dVAR; dSP; dMARK; dORIGMARK;
4842 HV* const hv = newHV();
4845 SV * const key = *++MARK;
4846 SV * const val = newSV(0);
4848 sv_setsv(val, *++MARK);
4850 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4851 (void)hv_store_ent(hv,key,val,0);
4854 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4855 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4860 S_deref_plain_array(pTHX_ AV *ary)
4862 if (SvTYPE(ary) == SVt_PVAV) return ary;
4863 SvGETMAGIC((SV *)ary);
4864 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4865 Perl_die(aTHX_ "Not an ARRAY reference");
4866 else if (SvOBJECT(SvRV(ary)))
4867 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4868 return (AV *)SvRV(ary);
4871 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4872 # define DEREF_PLAIN_ARRAY(ary) \
4875 SvTYPE(aRrRay) == SVt_PVAV \
4877 : S_deref_plain_array(aTHX_ aRrRay); \
4880 # define DEREF_PLAIN_ARRAY(ary) \
4882 PL_Sv = (SV *)(ary), \
4883 SvTYPE(PL_Sv) == SVt_PVAV \
4885 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4891 dVAR; dSP; dMARK; dORIGMARK;
4892 int num_args = (SP - MARK);
4893 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4897 register I32 offset;
4898 register I32 length;
4902 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4905 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4906 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4913 offset = i = SvIV(*MARK);
4915 offset += AvFILLp(ary) + 1;
4917 DIE(aTHX_ PL_no_aelem, i);
4919 length = SvIVx(*MARK++);
4921 length += AvFILLp(ary) - offset + 1;
4927 length = AvMAX(ary) + 1; /* close enough to infinity */
4931 length = AvMAX(ary) + 1;
4933 if (offset > AvFILLp(ary) + 1) {
4935 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4936 offset = AvFILLp(ary) + 1;
4938 after = AvFILLp(ary) + 1 - (offset + length);
4939 if (after < 0) { /* not that much array */
4940 length += after; /* offset+length now in array */
4946 /* At this point, MARK .. SP-1 is our new LIST */
4949 diff = newlen - length;
4950 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4953 /* make new elements SVs now: avoid problems if they're from the array */
4954 for (dst = MARK, i = newlen; i; i--) {
4955 SV * const h = *dst;
4956 *dst++ = newSVsv(h);
4959 if (diff < 0) { /* shrinking the area */
4960 SV **tmparyval = NULL;
4962 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4963 Copy(MARK, tmparyval, newlen, SV*);
4966 MARK = ORIGMARK + 1;
4967 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4968 MEXTEND(MARK, length);
4969 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4971 EXTEND_MORTAL(length);
4972 for (i = length, dst = MARK; i; i--) {
4973 sv_2mortal(*dst); /* free them eventually */
4980 *MARK = AvARRAY(ary)[offset+length-1];
4983 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4984 SvREFCNT_dec(*dst++); /* free them now */
4987 AvFILLp(ary) += diff;
4989 /* pull up or down? */
4991 if (offset < after) { /* easier to pull up */
4992 if (offset) { /* esp. if nothing to pull */
4993 src = &AvARRAY(ary)[offset-1];
4994 dst = src - diff; /* diff is negative */
4995 for (i = offset; i > 0; i--) /* can't trust Copy */
4999 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5003 if (after) { /* anything to pull down? */
5004 src = AvARRAY(ary) + offset + length;
5005 dst = src + diff; /* diff is negative */
5006 Move(src, dst, after, SV*);
5008 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5009 /* avoid later double free */
5013 dst[--i] = &PL_sv_undef;
5016 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5017 Safefree(tmparyval);
5020 else { /* no, expanding (or same) */
5021 SV** tmparyval = NULL;
5023 Newx(tmparyval, length, SV*); /* so remember deletion */
5024 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5027 if (diff > 0) { /* expanding */
5028 /* push up or down? */
5029 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5033 Move(src, dst, offset, SV*);
5035 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5037 AvFILLp(ary) += diff;
5040 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5041 av_extend(ary, AvFILLp(ary) + diff);
5042 AvFILLp(ary) += diff;
5045 dst = AvARRAY(ary) + AvFILLp(ary);
5047 for (i = after; i; i--) {
5055 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5058 MARK = ORIGMARK + 1;
5059 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5061 Copy(tmparyval, MARK, length, SV*);
5063 EXTEND_MORTAL(length);
5064 for (i = length, dst = MARK; i; i--) {
5065 sv_2mortal(*dst); /* free them eventually */
5072 else if (length--) {
5073 *MARK = tmparyval[length];
5076 while (length-- > 0)
5077 SvREFCNT_dec(tmparyval[length]);
5081 *MARK = &PL_sv_undef;
5082 Safefree(tmparyval);
5086 mg_set(MUTABLE_SV(ary));
5094 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5095 register AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5096 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5099 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5102 ENTER_with_name("call_PUSH");
5103 call_method("PUSH",G_SCALAR|G_DISCARD);
5104 LEAVE_with_name("call_PUSH");
5108 PL_delaymagic = DM_DELAY;
5109 for (++MARK; MARK <= SP; MARK++) {
5110 SV * const sv = newSV(0);
5112 sv_setsv(sv, *MARK);
5113 av_store(ary, AvFILLp(ary)+1, sv);
5115 if (PL_delaymagic & DM_ARRAY_ISA)
5116 mg_set(MUTABLE_SV(ary));
5121 if (OP_GIMME(PL_op, 0) != G_VOID) {
5122 PUSHi( AvFILL(ary) + 1 );
5131 AV * const av = PL_op->op_flags & OPf_SPECIAL
5132 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5133 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5137 (void)sv_2mortal(sv);
5144 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5145 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5146 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5149 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5152 ENTER_with_name("call_UNSHIFT");
5153 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5154 LEAVE_with_name("call_UNSHIFT");
5159 av_unshift(ary, SP - MARK);
5161 SV * const sv = newSVsv(*++MARK);
5162 (void)av_store(ary, i++, sv);
5166 if (OP_GIMME(PL_op, 0) != G_VOID) {
5167 PUSHi( AvFILL(ary) + 1 );
5176 if (GIMME == G_ARRAY) {
5177 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5181 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5182 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5183 av = MUTABLE_AV((*SP));
5184 /* In-place reversing only happens in void context for the array
5185 * assignment. We don't need to push anything on the stack. */
5188 if (SvMAGICAL(av)) {
5190 register SV *tmp = sv_newmortal();
5191 /* For SvCANEXISTDELETE */
5194 bool can_preserve = SvCANEXISTDELETE(av);
5196 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5197 register SV *begin, *end;
5200 if (!av_exists(av, i)) {
5201 if (av_exists(av, j)) {
5202 register SV *sv = av_delete(av, j, 0);
5203 begin = *av_fetch(av, i, TRUE);
5204 sv_setsv_mg(begin, sv);
5208 else if (!av_exists(av, j)) {
5209 register SV *sv = av_delete(av, i, 0);
5210 end = *av_fetch(av, j, TRUE);
5211 sv_setsv_mg(end, sv);
5216 begin = *av_fetch(av, i, TRUE);
5217 end = *av_fetch(av, j, TRUE);
5218 sv_setsv(tmp, begin);
5219 sv_setsv_mg(begin, end);
5220 sv_setsv_mg(end, tmp);
5224 SV **begin = AvARRAY(av);
5227 SV **end = begin + AvFILLp(av);
5229 while (begin < end) {
5230 register SV * const tmp = *begin;
5241 register SV * const tmp = *MARK;
5245 /* safe as long as stack cannot get extended in the above */
5251 register char *down;
5256 SvUTF8_off(TARG); /* decontaminate */
5258 do_join(TARG, &PL_sv_no, MARK, SP);
5260 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5261 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5262 report_uninit(TARG);
5265 up = SvPV_force(TARG, len);
5267 if (DO_UTF8(TARG)) { /* first reverse each character */
5268 U8* s = (U8*)SvPVX(TARG);
5269 const U8* send = (U8*)(s + len);
5271 if (UTF8_IS_INVARIANT(*s)) {
5276 if (!utf8_to_uvchr_buf(s, send, 0))
5280 down = (char*)(s - 1);
5281 /* reverse this character */
5285 *down-- = (char)tmp;
5291 down = SvPVX(TARG) + len - 1;
5295 *down-- = (char)tmp;
5297 (void)SvPOK_only_UTF8(TARG);
5309 register IV limit = POPi; /* note, negative is forever */
5310 SV * const sv = POPs;
5312 register const char *s = SvPV_const(sv, len);
5313 const bool do_utf8 = DO_UTF8(sv);
5314 const char *strend = s + len;
5316 register REGEXP *rx;
5318 register const char *m;
5320 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5321 I32 maxiters = slen + 10;
5322 I32 trailing_empty = 0;
5324 const I32 origlimit = limit;
5327 const I32 gimme = GIMME_V;
5329 const I32 oldsave = PL_savestack_ix;
5330 U32 make_mortal = SVs_TEMP;
5335 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5340 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5343 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5344 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5346 RX_MATCH_UTF8_set(rx, do_utf8);
5349 if (pm->op_pmreplrootu.op_pmtargetoff) {
5350 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5353 if (pm->op_pmreplrootu.op_pmtargetgv) {
5354 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5359 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5365 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5367 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5374 for (i = AvFILLp(ary); i >= 0; i--)
5375 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5377 /* temporarily switch stacks */
5378 SAVESWITCHSTACK(PL_curstack, ary);
5382 base = SP - PL_stack_base;
5384 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5386 while (*s == ' ' || is_utf8_space((U8*)s))
5389 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5390 while (isSPACE_LC(*s))
5398 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5402 gimme_scalar = gimme == G_SCALAR && !ary;
5405 limit = maxiters + 2;
5406 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5409 /* this one uses 'm' and is a negative test */
5411 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5412 const int t = UTF8SKIP(m);
5413 /* is_utf8_space returns FALSE for malform utf8 */
5420 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5421 while (m < strend && !isSPACE_LC(*m))
5424 while (m < strend && !isSPACE(*m))
5437 dstr = newSVpvn_flags(s, m-s,
5438 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5442 /* skip the whitespace found last */
5444 s = m + UTF8SKIP(m);
5448 /* this one uses 's' and is a positive test */
5450 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5453 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5454 while (s < strend && isSPACE_LC(*s))
5457 while (s < strend && isSPACE(*s))
5462 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5464 for (m = s; m < strend && *m != '\n'; m++)
5477 dstr = newSVpvn_flags(s, m-s,
5478 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5484 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5486 Pre-extend the stack, either the number of bytes or
5487 characters in the string or a limited amount, triggered by:
5489 my ($x, $y) = split //, $str;
5493 if (!gimme_scalar) {
5494 const U32 items = limit - 1;
5503 /* keep track of how many bytes we skip over */
5513 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5526 dstr = newSVpvn(s, 1);
5542 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5543 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5544 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5545 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5546 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5547 SV * const csv = CALLREG_INTUIT_STRING(rx);
5549 len = RX_MINLENRET(rx);
5550 if (len == 1 && !RX_UTF8(rx) && !tail) {
5551 const char c = *SvPV_nolen_const(csv);
5553 for (m = s; m < strend && *m != c; m++)
5564 dstr = newSVpvn_flags(s, m-s,
5565 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5568 /* The rx->minlen is in characters but we want to step
5569 * s ahead by bytes. */
5571 s = (char*)utf8_hop((U8*)m, len);
5573 s = m + len; /* Fake \n at the end */
5577 while (s < strend && --limit &&
5578 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5579 csv, multiline ? FBMrf_MULTILINE : 0)) )
5588 dstr = newSVpvn_flags(s, m-s,
5589 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5592 /* The rx->minlen is in characters but we want to step
5593 * s ahead by bytes. */
5595 s = (char*)utf8_hop((U8*)m, len);
5597 s = m + len; /* Fake \n at the end */
5602 maxiters += slen * RX_NPARENS(rx);
5603 while (s < strend && --limit)
5607 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5610 if (rex_return == 0)
5612 TAINT_IF(RX_MATCH_TAINTED(rx));
5613 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5616 orig = RX_SUBBEG(rx);
5618 strend = s + (strend - m);
5620 m = RX_OFFS(rx)[0].start + orig;
5629 dstr = newSVpvn_flags(s, m-s,
5630 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5633 if (RX_NPARENS(rx)) {
5635 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5636 s = RX_OFFS(rx)[i].start + orig;
5637 m = RX_OFFS(rx)[i].end + orig;
5639 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5640 parens that didn't match -- they should be set to
5641 undef, not the empty string */
5649 if (m >= orig && s >= orig) {
5650 dstr = newSVpvn_flags(s, m-s,
5651 (do_utf8 ? SVf_UTF8 : 0)
5655 dstr = &PL_sv_undef; /* undef, not "" */
5661 s = RX_OFFS(rx)[0].end + orig;
5665 if (!gimme_scalar) {
5666 iters = (SP - PL_stack_base) - base;
5668 if (iters > maxiters)
5669 DIE(aTHX_ "Split loop");
5671 /* keep field after final delim? */
5672 if (s < strend || (iters && origlimit)) {
5673 if (!gimme_scalar) {
5674 const STRLEN l = strend - s;
5675 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5680 else if (!origlimit) {
5682 iters -= trailing_empty;
5684 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5685 if (TOPs && !make_mortal)
5687 *SP-- = &PL_sv_undef;
5694 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5698 if (SvSMAGICAL(ary)) {
5700 mg_set(MUTABLE_SV(ary));
5703 if (gimme == G_ARRAY) {
5705 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5712 ENTER_with_name("call_PUSH");
5713 call_method("PUSH",G_SCALAR|G_DISCARD);
5714 LEAVE_with_name("call_PUSH");
5716 if (gimme == G_ARRAY) {
5718 /* EXTEND should not be needed - we just popped them */
5720 for (i=0; i < iters; i++) {
5721 SV **svp = av_fetch(ary, i, FALSE);
5722 PUSHs((svp) ? *svp : &PL_sv_undef);
5729 if (gimme == G_ARRAY)
5741 SV *const sv = PAD_SVl(PL_op->op_targ);
5743 if (SvPADSTALE(sv)) {
5746 RETURNOP(cLOGOP->op_other);
5748 RETURNOP(cLOGOP->op_next);
5758 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5759 || SvTYPE(retsv) == SVt_PVCV) {
5760 retsv = refto(retsv);
5767 PP(unimplemented_op)
5770 const Optype op_type = PL_op->op_type;
5771 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5772 with out of range op numbers - it only "special" cases op_custom.
5773 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5774 if we get here for a custom op then that means that the custom op didn't
5775 have an implementation. Given that OP_NAME() looks up the custom op
5776 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5777 registers &PL_unimplemented_op as the address of their custom op.
5778 NULL doesn't generate a useful error message. "custom" does. */
5779 const char *const name = op_type >= OP_max
5780 ? "[out of range]" : PL_op_name[PL_op->op_type];
5781 if(OP_IS_SOCKET(op_type))
5782 DIE(aTHX_ PL_no_sock_func, name);
5783 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5790 HV * const hv = (HV*)POPs;
5792 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
5794 if (SvRMAGICAL(hv)) {
5795 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5797 XPUSHs(magic_scalarpack(hv, mg));
5802 XPUSHs(boolSV(HvUSEDKEYS(hv) != 0));
5806 /* For sorting out arguments passed to a &CORE:: subroutine */
5810 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5811 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5812 AV * const at_ = GvAV(PL_defgv);
5813 SV **svp = at_ ? AvARRAY(at_) : NULL;
5814 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5815 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5816 bool seen_question = 0;
5817 const char *err = NULL;
5818 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5820 /* Count how many args there are first, to get some idea how far to
5821 extend the stack. */
5823 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5825 if (oa & OA_OPTIONAL) seen_question = 1;
5826 if (!seen_question) minargs++;
5830 if(numargs < minargs) err = "Not enough";
5831 else if(numargs > maxargs) err = "Too many";
5833 /* diag_listed_as: Too many arguments for %s */
5835 "%s arguments for %s", err,
5836 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5839 /* Reset the stack pointer. Without this, we end up returning our own
5840 arguments in list context, in addition to the values we are supposed
5841 to return. nextstate usually does this on sub entry, but we need
5842 to run the next op with the caller's hints, so we cannot have a
5844 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5846 if(!maxargs) RETURN;
5848 /* We do this here, rather than with a separate pushmark op, as it has
5849 to come in between two things this function does (stack reset and
5850 arg pushing). This seems the easiest way to do it. */
5853 (void)Perl_pp_pushmark(aTHX);
5856 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5857 PUTBACK; /* The code below can die in various places. */
5859 oa = PL_opargs[opnum] >> OASHIFT;
5860 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5865 if (!numargs && defgv && whicharg == minargs + 1) {
5866 PERL_SI * const oldsi = PL_curstackinfo;
5867 I32 const oldcxix = oldsi->si_cxix;
5869 if (oldcxix) oldsi->si_cxix--;
5870 else PL_curstackinfo = oldsi->si_prev;
5871 caller = find_runcv(NULL);
5872 PL_curstackinfo = oldsi;
5873 oldsi->si_cxix = oldcxix;
5874 PUSHs(find_rundefsv2(
5875 caller,cxstack[cxstack_ix].blk_oldcop->cop_seq
5878 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5882 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5887 if (!svp || !*svp || !SvROK(*svp)
5888 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5890 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5891 "Type of arg %d to &CORE::%s must be hash reference",
5892 whicharg, OP_DESC(PL_op->op_next)
5897 if (!numargs) PUSHs(NULL);
5898 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5899 /* no magic here, as the prototype will have added an extra
5900 refgen and we just want what was there before that */
5903 const bool constr = PL_op->op_private & whicharg;
5905 svp && *svp ? *svp : &PL_sv_undef,
5906 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5912 if (!numargs) goto try_defsv;
5914 const bool wantscalar =
5915 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5916 if (!svp || !*svp || !SvROK(*svp)
5917 /* We have to permit globrefs even for the \$ proto, as
5918 *foo is indistinguishable from ${\*foo}, and the proto-
5919 type permits the latter. */
5920 || SvTYPE(SvRV(*svp)) > (
5921 wantscalar ? SVt_PVLV
5922 : opnum == OP_LOCK || opnum == OP_UNDEF
5928 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5929 "Type of arg %d to &CORE::%s must be %s",
5930 whicharg, PL_op_name[opnum],
5932 ? "scalar reference"
5933 : opnum == OP_LOCK || opnum == OP_UNDEF
5934 ? "reference to one of [$@%&*]"
5935 : "reference to one of [$@%*]"
5938 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5939 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5940 /* Undo @_ localisation, so that sub exit does not undo
5941 part of our undeffing. */
5942 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5944 cx->cx_type &= ~ CXp_HASARGS;
5945 assert(!AvREAL(cx->blk_sub.argarray));
5950 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5962 if (PL_op->op_private & OPpOFFBYONE) {
5963 PERL_SI * const oldsi = PL_curstackinfo;
5964 I32 const oldcxix = oldsi->si_cxix;
5965 if (oldcxix) oldsi->si_cxix--;
5966 else PL_curstackinfo = oldsi->si_prev;
5967 cv = find_runcv(NULL);
5968 PL_curstackinfo = oldsi;
5969 oldsi->si_cxix = oldcxix;
5971 else cv = find_runcv(NULL);
5972 XPUSHs(CvUNIQUE(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5979 * c-indentation-style: bsd
5981 * indent-tabs-mode: nil
5984 * ex: set ts=8 sts=4 sw=4 et: