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);
418 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
420 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
424 cv = MUTABLE_CV(&PL_sv_undef);
425 SETs(MUTABLE_SV(cv));
435 SV *ret = &PL_sv_undef;
437 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
438 const char * s = SvPVX_const(TOPs);
439 if (strnEQ(s, "CORE::", 6)) {
440 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
441 if (!code || code == -KEY_CORE)
442 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
443 if (code < 0) { /* Overridable. */
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 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_study) : NULL;
658 if (mg && SvSCREAM(sv))
661 s = (unsigned char*)(SvPV(sv, len));
662 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
663 /* No point in studying a zero length string, and not safe to study
664 anything that doesn't appear to be a simple scalar (and hence might
665 change between now and when the regexp engine runs without our set
666 magic ever running) such as a reference to an object with overloaded
667 stringification. Also refuse to study an FBM scalar, as this gives
668 more flexibility in SV flag usage. No real-world code would ever
669 end up studying an FBM scalar, so this isn't a real pessimisation.
670 Endemic use of I32 in Perl_screaminstr makes it hard to safely push
671 the study length limit from I32_MAX to U32_MAX - 1.
676 /* Make study a no-op. It's no longer useful and its existence
677 complicates matters elsewhere. This is a low-impact band-aid.
678 The relevant code will be neatly removed in a future release. */
683 } else if (len < 0xFFFF) {
688 size = (256 + len) * quanta;
689 sfirst_raw = (char *)safemalloc(size);
692 DIE(aTHX_ "do_study: out of memory");
696 mg = sv_magicext(sv, NULL, PERL_MAGIC_study, &PL_vtbl_regexp, NULL, 0);
697 mg->mg_ptr = sfirst_raw;
699 mg->mg_private = quanta;
701 memset(sfirst_raw, ~0, 256 * quanta);
703 /* The assumption here is that most studied strings are fairly short, hence
704 the pain of the extra code is worth it, given the memory savings.
705 80 character string, 336 bytes as U8, down from 1344 as U32
706 800 character string, 2112 bytes as U16, down from 4224 as U32
710 U8 *const sfirst = (U8 *)sfirst_raw;
711 U8 *const snext = sfirst + 256;
713 const U8 ch = s[len];
714 snext[len] = sfirst[ch];
717 } else if (quanta == 2) {
718 U16 *const sfirst = (U16 *)sfirst_raw;
719 U16 *const snext = sfirst + 256;
721 const U8 ch = s[len];
722 snext[len] = sfirst[ch];
726 U32 *const sfirst = (U32 *)sfirst_raw;
727 U32 *const snext = sfirst + 256;
729 const U8 ch = s[len];
730 snext[len] = sfirst[ch];
743 if (PL_op->op_flags & OPf_STACKED)
745 else if (PL_op->op_private & OPpTARGET_MY)
751 TARG = sv_newmortal();
752 if(PL_op->op_type == OP_TRANSR) {
754 const char * const pv = SvPV(sv,len);
755 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
759 else PUSHi(do_trans(sv));
763 /* Lvalue operators. */
766 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
772 PERL_ARGS_ASSERT_DO_CHOMP;
774 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
776 if (SvTYPE(sv) == SVt_PVAV) {
778 AV *const av = MUTABLE_AV(sv);
779 const I32 max = AvFILL(av);
781 for (i = 0; i <= max; i++) {
782 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
783 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
784 do_chomp(retval, sv, chomping);
788 else if (SvTYPE(sv) == SVt_PVHV) {
789 HV* const hv = MUTABLE_HV(sv);
791 (void)hv_iterinit(hv);
792 while ((entry = hv_iternext(hv)))
793 do_chomp(retval, hv_iterval(hv,entry), chomping);
796 else if (SvREADONLY(sv)) {
798 /* SV is copy-on-write */
799 sv_force_normal_flags(sv, 0);
802 Perl_croak_no_modify(aTHX);
807 /* XXX, here sv is utf8-ized as a side-effect!
808 If encoding.pm is used properly, almost string-generating
809 operations, including literal strings, chr(), input data, etc.
810 should have been utf8-ized already, right?
812 sv_recode_to_utf8(sv, PL_encoding);
818 char *temp_buffer = NULL;
827 while (len && s[-1] == '\n') {
834 STRLEN rslen, rs_charlen;
835 const char *rsptr = SvPV_const(PL_rs, rslen);
837 rs_charlen = SvUTF8(PL_rs)
841 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
842 /* Assumption is that rs is shorter than the scalar. */
844 /* RS is utf8, scalar is 8 bit. */
846 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
849 /* Cannot downgrade, therefore cannot possibly match
851 assert (temp_buffer == rsptr);
857 else if (PL_encoding) {
858 /* RS is 8 bit, encoding.pm is used.
859 * Do not recode PL_rs as a side-effect. */
860 svrecode = newSVpvn(rsptr, rslen);
861 sv_recode_to_utf8(svrecode, PL_encoding);
862 rsptr = SvPV_const(svrecode, rslen);
863 rs_charlen = sv_len_utf8(svrecode);
866 /* RS is 8 bit, scalar is utf8. */
867 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
881 if (memNE(s, rsptr, rslen))
883 SvIVX(retval) += rs_charlen;
886 s = SvPV_force_nomg_nolen(sv);
894 SvREFCNT_dec(svrecode);
896 Safefree(temp_buffer);
898 if (len && !SvPOK(sv))
899 s = SvPV_force_nomg(sv, len);
902 char * const send = s + len;
903 char * const start = s;
905 while (s > start && UTF8_IS_CONTINUATION(*s))
907 if (is_utf8_string((U8*)s, send - s)) {
908 sv_setpvn(retval, s, send - s);
910 SvCUR_set(sv, s - start);
916 sv_setpvs(retval, "");
920 sv_setpvn(retval, s, 1);
927 sv_setpvs(retval, "");
935 const bool chomping = PL_op->op_type == OP_SCHOMP;
939 do_chomp(TARG, TOPs, chomping);
946 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
947 const bool chomping = PL_op->op_type == OP_CHOMP;
952 do_chomp(TARG, *++MARK, chomping);
963 if (!PL_op->op_private) {
972 SV_CHECK_THINKFIRST_COW_DROP(sv);
974 switch (SvTYPE(sv)) {
978 av_undef(MUTABLE_AV(sv));
981 hv_undef(MUTABLE_HV(sv));
984 if (cv_const_sv((const CV *)sv))
985 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
986 "Constant subroutine %"SVf" undefined",
987 SVfARG(CvANON((const CV *)sv)
988 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
989 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
993 /* let user-undef'd sub keep its identity */
994 GV* const gv = CvGV((const CV *)sv);
995 cv_undef(MUTABLE_CV(sv));
996 CvGV_set(MUTABLE_CV(sv), gv);
1001 SvSetMagicSV(sv, &PL_sv_undef);
1004 else if (isGV_with_GP(sv)) {
1008 /* undef *Pkg::meth_name ... */
1010 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1011 && HvENAME_get(stash);
1013 if((stash = GvHV((const GV *)sv))) {
1014 if(HvENAME_get(stash))
1015 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1019 gp_free(MUTABLE_GV(sv));
1021 GvGP_set(sv, gp_ref(gp));
1022 GvSV(sv) = newSV(0);
1023 GvLINE(sv) = CopLINE(PL_curcop);
1024 GvEGV(sv) = MUTABLE_GV(sv);
1028 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1030 /* undef *Foo::ISA */
1031 if( strEQ(GvNAME((const GV *)sv), "ISA")
1032 && (stash = GvSTASH((const GV *)sv))
1033 && (method_changed || HvENAME(stash)) )
1034 mro_isa_changed_in(stash);
1035 else if(method_changed)
1036 mro_method_changed_in(
1037 GvSTASH((const GV *)sv)
1044 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1060 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1061 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1062 Perl_croak_no_modify(aTHX);
1064 TARG = sv_newmortal();
1065 sv_setsv(TARG, TOPs);
1066 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1067 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1069 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1070 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1074 else sv_dec_nomg(TOPs);
1076 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1077 if (inc && !SvOK(TARG))
1083 /* Ordinary operators. */
1087 dVAR; dSP; dATARGET; SV *svl, *svr;
1088 #ifdef PERL_PRESERVE_IVUV
1091 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1094 #ifdef PERL_PRESERVE_IVUV
1095 /* For integer to integer power, we do the calculation by hand wherever
1096 we're sure it is safe; otherwise we call pow() and try to convert to
1097 integer afterwards. */
1099 SvIV_please_nomg(svr);
1101 SvIV_please_nomg(svl);
1110 const IV iv = SvIVX(svr);
1114 goto float_it; /* Can't do negative powers this way. */
1118 baseuok = SvUOK(svl);
1120 baseuv = SvUVX(svl);
1122 const IV iv = SvIVX(svl);
1125 baseuok = TRUE; /* effectively it's a UV now */
1127 baseuv = -iv; /* abs, baseuok == false records sign */
1130 /* now we have integer ** positive integer. */
1133 /* foo & (foo - 1) is zero only for a power of 2. */
1134 if (!(baseuv & (baseuv - 1))) {
1135 /* We are raising power-of-2 to a positive integer.
1136 The logic here will work for any base (even non-integer
1137 bases) but it can be less accurate than
1138 pow (base,power) or exp (power * log (base)) when the
1139 intermediate values start to spill out of the mantissa.
1140 With powers of 2 we know this can't happen.
1141 And powers of 2 are the favourite thing for perl
1142 programmers to notice ** not doing what they mean. */
1144 NV base = baseuok ? baseuv : -(NV)baseuv;
1149 while (power >>= 1) {
1157 SvIV_please_nomg(svr);
1160 register unsigned int highbit = 8 * sizeof(UV);
1161 register unsigned int diff = 8 * sizeof(UV);
1162 while (diff >>= 1) {
1164 if (baseuv >> highbit) {
1168 /* we now have baseuv < 2 ** highbit */
1169 if (power * highbit <= 8 * sizeof(UV)) {
1170 /* result will definitely fit in UV, so use UV math
1171 on same algorithm as above */
1172 register UV result = 1;
1173 register UV base = baseuv;
1174 const bool odd_power = cBOOL(power & 1);
1178 while (power >>= 1) {
1185 if (baseuok || !odd_power)
1186 /* answer is positive */
1188 else if (result <= (UV)IV_MAX)
1189 /* answer negative, fits in IV */
1190 SETi( -(IV)result );
1191 else if (result == (UV)IV_MIN)
1192 /* 2's complement assumption: special case IV_MIN */
1195 /* answer negative, doesn't fit */
1196 SETn( -(NV)result );
1206 NV right = SvNV_nomg(svr);
1207 NV left = SvNV_nomg(svl);
1210 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1212 We are building perl with long double support and are on an AIX OS
1213 afflicted with a powl() function that wrongly returns NaNQ for any
1214 negative base. This was reported to IBM as PMR #23047-379 on
1215 03/06/2006. The problem exists in at least the following versions
1216 of AIX and the libm fileset, and no doubt others as well:
1218 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1219 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1220 AIX 5.2.0 bos.adt.libm 5.2.0.85
1222 So, until IBM fixes powl(), we provide the following workaround to
1223 handle the problem ourselves. Our logic is as follows: for
1224 negative bases (left), we use fmod(right, 2) to check if the
1225 exponent is an odd or even integer:
1227 - if odd, powl(left, right) == -powl(-left, right)
1228 - if even, powl(left, right) == powl(-left, right)
1230 If the exponent is not an integer, the result is rightly NaNQ, so
1231 we just return that (as NV_NAN).
1235 NV mod2 = Perl_fmod( right, 2.0 );
1236 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1237 SETn( -Perl_pow( -left, right) );
1238 } else if (mod2 == 0.0) { /* even integer */
1239 SETn( Perl_pow( -left, right) );
1240 } else { /* fractional power */
1244 SETn( Perl_pow( left, right) );
1247 SETn( Perl_pow( left, right) );
1248 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1250 #ifdef PERL_PRESERVE_IVUV
1252 SvIV_please_nomg(svr);
1260 dVAR; dSP; dATARGET; SV *svl, *svr;
1261 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1264 #ifdef PERL_PRESERVE_IVUV
1265 SvIV_please_nomg(svr);
1267 /* Unless the left argument is integer in range we are going to have to
1268 use NV maths. Hence only attempt to coerce the right argument if
1269 we know the left is integer. */
1270 /* Left operand is defined, so is it IV? */
1271 SvIV_please_nomg(svl);
1273 bool auvok = SvUOK(svl);
1274 bool buvok = SvUOK(svr);
1275 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1276 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1285 const IV aiv = SvIVX(svl);
1288 auvok = TRUE; /* effectively it's a UV now */
1290 alow = -aiv; /* abs, auvok == false records sign */
1296 const IV biv = SvIVX(svr);
1299 buvok = TRUE; /* effectively it's a UV now */
1301 blow = -biv; /* abs, buvok == false records sign */
1305 /* If this does sign extension on unsigned it's time for plan B */
1306 ahigh = alow >> (4 * sizeof (UV));
1308 bhigh = blow >> (4 * sizeof (UV));
1310 if (ahigh && bhigh) {
1312 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1313 which is overflow. Drop to NVs below. */
1314 } else if (!ahigh && !bhigh) {
1315 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1316 so the unsigned multiply cannot overflow. */
1317 const UV product = alow * blow;
1318 if (auvok == buvok) {
1319 /* -ve * -ve or +ve * +ve gives a +ve result. */
1323 } else if (product <= (UV)IV_MIN) {
1324 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1325 /* -ve result, which could overflow an IV */
1327 SETi( -(IV)product );
1329 } /* else drop to NVs below. */
1331 /* One operand is large, 1 small */
1334 /* swap the operands */
1336 bhigh = blow; /* bhigh now the temp var for the swap */
1340 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1341 multiplies can't overflow. shift can, add can, -ve can. */
1342 product_middle = ahigh * blow;
1343 if (!(product_middle & topmask)) {
1344 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1346 product_middle <<= (4 * sizeof (UV));
1347 product_low = alow * blow;
1349 /* as for pp_add, UV + something mustn't get smaller.
1350 IIRC ANSI mandates this wrapping *behaviour* for
1351 unsigned whatever the actual representation*/
1352 product_low += product_middle;
1353 if (product_low >= product_middle) {
1354 /* didn't overflow */
1355 if (auvok == buvok) {
1356 /* -ve * -ve or +ve * +ve gives a +ve result. */
1358 SETu( product_low );
1360 } else if (product_low <= (UV)IV_MIN) {
1361 /* 2s complement assumption again */
1362 /* -ve result, which could overflow an IV */
1364 SETi( -(IV)product_low );
1366 } /* else drop to NVs below. */
1368 } /* product_middle too large */
1369 } /* ahigh && bhigh */
1374 NV right = SvNV_nomg(svr);
1375 NV left = SvNV_nomg(svl);
1377 SETn( left * right );
1384 dVAR; dSP; dATARGET; SV *svl, *svr;
1385 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1388 /* Only try to do UV divide first
1389 if ((SLOPPYDIVIDE is true) or
1390 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1392 The assumption is that it is better to use floating point divide
1393 whenever possible, only doing integer divide first if we can't be sure.
1394 If NV_PRESERVES_UV is true then we know at compile time that no UV
1395 can be too large to preserve, so don't need to compile the code to
1396 test the size of UVs. */
1399 # define PERL_TRY_UV_DIVIDE
1400 /* ensure that 20./5. == 4. */
1402 # ifdef PERL_PRESERVE_IVUV
1403 # ifndef NV_PRESERVES_UV
1404 # define PERL_TRY_UV_DIVIDE
1409 #ifdef PERL_TRY_UV_DIVIDE
1410 SvIV_please_nomg(svr);
1412 SvIV_please_nomg(svl);
1414 bool left_non_neg = SvUOK(svl);
1415 bool right_non_neg = SvUOK(svr);
1419 if (right_non_neg) {
1423 const IV biv = SvIVX(svr);
1426 right_non_neg = TRUE; /* effectively it's a UV now */
1432 /* historically undef()/0 gives a "Use of uninitialized value"
1433 warning before dieing, hence this test goes here.
1434 If it were immediately before the second SvIV_please, then
1435 DIE() would be invoked before left was even inspected, so
1436 no inspection would give no warning. */
1438 DIE(aTHX_ "Illegal division by zero");
1444 const IV aiv = SvIVX(svl);
1447 left_non_neg = TRUE; /* effectively it's a UV now */
1456 /* For sloppy divide we always attempt integer division. */
1458 /* Otherwise we only attempt it if either or both operands
1459 would not be preserved by an NV. If both fit in NVs
1460 we fall through to the NV divide code below. However,
1461 as left >= right to ensure integer result here, we know that
1462 we can skip the test on the right operand - right big
1463 enough not to be preserved can't get here unless left is
1466 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1469 /* Integer division can't overflow, but it can be imprecise. */
1470 const UV result = left / right;
1471 if (result * right == left) {
1472 SP--; /* result is valid */
1473 if (left_non_neg == right_non_neg) {
1474 /* signs identical, result is positive. */
1478 /* 2s complement assumption */
1479 if (result <= (UV)IV_MIN)
1480 SETi( -(IV)result );
1482 /* It's exact but too negative for IV. */
1483 SETn( -(NV)result );
1486 } /* tried integer divide but it was not an integer result */
1487 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1488 } /* left wasn't SvIOK */
1489 } /* right wasn't SvIOK */
1490 #endif /* PERL_TRY_UV_DIVIDE */
1492 NV right = SvNV_nomg(svr);
1493 NV left = SvNV_nomg(svl);
1494 (void)POPs;(void)POPs;
1495 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1496 if (! Perl_isnan(right) && right == 0.0)
1500 DIE(aTHX_ "Illegal division by zero");
1501 PUSHn( left / right );
1508 dVAR; dSP; dATARGET;
1509 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1513 bool left_neg = FALSE;
1514 bool right_neg = FALSE;
1515 bool use_double = FALSE;
1516 bool dright_valid = FALSE;
1519 SV * const svr = TOPs;
1520 SV * const svl = TOPm1s;
1521 SvIV_please_nomg(svr);
1523 right_neg = !SvUOK(svr);
1527 const IV biv = SvIVX(svr);
1530 right_neg = FALSE; /* effectively it's a UV now */
1537 dright = SvNV_nomg(svr);
1538 right_neg = dright < 0;
1541 if (dright < UV_MAX_P1) {
1542 right = U_V(dright);
1543 dright_valid = TRUE; /* In case we need to use double below. */
1549 /* At this point use_double is only true if right is out of range for
1550 a UV. In range NV has been rounded down to nearest UV and
1551 use_double false. */
1552 SvIV_please_nomg(svl);
1553 if (!use_double && SvIOK(svl)) {
1555 left_neg = !SvUOK(svl);
1559 const IV aiv = SvIVX(svl);
1562 left_neg = FALSE; /* effectively it's a UV now */
1570 dleft = SvNV_nomg(svl);
1571 left_neg = dleft < 0;
1575 /* This should be exactly the 5.6 behaviour - if left and right are
1576 both in range for UV then use U_V() rather than floor. */
1578 if (dleft < UV_MAX_P1) {
1579 /* right was in range, so is dleft, so use UVs not double.
1583 /* left is out of range for UV, right was in range, so promote
1584 right (back) to double. */
1586 /* The +0.5 is used in 5.6 even though it is not strictly
1587 consistent with the implicit +0 floor in the U_V()
1588 inside the #if 1. */
1589 dleft = Perl_floor(dleft + 0.5);
1592 dright = Perl_floor(dright + 0.5);
1603 DIE(aTHX_ "Illegal modulus zero");
1605 dans = Perl_fmod(dleft, dright);
1606 if ((left_neg != right_neg) && dans)
1607 dans = dright - dans;
1610 sv_setnv(TARG, dans);
1616 DIE(aTHX_ "Illegal modulus zero");
1619 if ((left_neg != right_neg) && ans)
1622 /* XXX may warn: unary minus operator applied to unsigned type */
1623 /* could change -foo to be (~foo)+1 instead */
1624 if (ans <= ~((UV)IV_MAX)+1)
1625 sv_setiv(TARG, ~ans+1);
1627 sv_setnv(TARG, -(NV)ans);
1630 sv_setuv(TARG, ans);
1639 dVAR; dSP; dATARGET;
1643 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1644 /* TODO: think of some way of doing list-repeat overloading ??? */
1649 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1655 const UV uv = SvUV_nomg(sv);
1657 count = IV_MAX; /* The best we can do? */
1661 const IV iv = SvIV_nomg(sv);
1668 else if (SvNOKp(sv)) {
1669 const NV nv = SvNV_nomg(sv);
1676 count = SvIV_nomg(sv);
1678 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1680 static const char oom_list_extend[] = "Out of memory during list extend";
1681 const I32 items = SP - MARK;
1682 const I32 max = items * count;
1684 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1685 /* Did the max computation overflow? */
1686 if (items > 0 && max > 0 && (max < items || max < count))
1687 Perl_croak(aTHX_ oom_list_extend);
1692 /* This code was intended to fix 20010809.028:
1695 for (($x =~ /./g) x 2) {
1696 print chop; # "abcdabcd" expected as output.
1699 * but that change (#11635) broke this code:
1701 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1703 * I can't think of a better fix that doesn't introduce
1704 * an efficiency hit by copying the SVs. The stack isn't
1705 * refcounted, and mortalisation obviously doesn't
1706 * Do The Right Thing when the stack has more than
1707 * one pointer to the same mortal value.
1711 *SP = sv_2mortal(newSVsv(*SP));
1721 repeatcpy((char*)(MARK + items), (char*)MARK,
1722 items * sizeof(const SV *), count - 1);
1725 else if (count <= 0)
1728 else { /* Note: mark already snarfed by pp_list */
1729 SV * const tmpstr = POPs;
1732 static const char oom_string_extend[] =
1733 "Out of memory during string extend";
1736 sv_setsv_nomg(TARG, tmpstr);
1737 SvPV_force_nomg(TARG, len);
1738 isutf = DO_UTF8(TARG);
1743 const STRLEN max = (UV)count * len;
1744 if (len > MEM_SIZE_MAX / count)
1745 Perl_croak(aTHX_ oom_string_extend);
1746 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1747 SvGROW(TARG, max + 1);
1748 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1749 SvCUR_set(TARG, SvCUR(TARG) * count);
1751 *SvEND(TARG) = '\0';
1754 (void)SvPOK_only_UTF8(TARG);
1756 (void)SvPOK_only(TARG);
1758 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1759 /* The parser saw this as a list repeat, and there
1760 are probably several items on the stack. But we're
1761 in scalar context, and there's no pp_list to save us
1762 now. So drop the rest of the items -- robin@kitsite.com
1774 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1775 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1778 useleft = USE_LEFT(svl);
1779 #ifdef PERL_PRESERVE_IVUV
1780 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1781 "bad things" happen if you rely on signed integers wrapping. */
1782 SvIV_please_nomg(svr);
1784 /* Unless the left argument is integer in range we are going to have to
1785 use NV maths. Hence only attempt to coerce the right argument if
1786 we know the left is integer. */
1787 register UV auv = 0;
1793 a_valid = auvok = 1;
1794 /* left operand is undef, treat as zero. */
1796 /* Left operand is defined, so is it IV? */
1797 SvIV_please_nomg(svl);
1799 if ((auvok = SvUOK(svl)))
1802 register const IV aiv = SvIVX(svl);
1805 auvok = 1; /* Now acting as a sign flag. */
1806 } else { /* 2s complement assumption for IV_MIN */
1814 bool result_good = 0;
1817 bool buvok = SvUOK(svr);
1822 register const IV biv = SvIVX(svr);
1829 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1830 else "IV" now, independent of how it came in.
1831 if a, b represents positive, A, B negative, a maps to -A etc
1836 all UV maths. negate result if A negative.
1837 subtract if signs same, add if signs differ. */
1839 if (auvok ^ buvok) {
1848 /* Must get smaller */
1853 if (result <= buv) {
1854 /* result really should be -(auv-buv). as its negation
1855 of true value, need to swap our result flag */
1867 if (result <= (UV)IV_MIN)
1868 SETi( -(IV)result );
1870 /* result valid, but out of range for IV. */
1871 SETn( -(NV)result );
1875 } /* Overflow, drop through to NVs. */
1880 NV value = SvNV_nomg(svr);
1884 /* left operand is undef, treat as zero - value */
1888 SETn( SvNV_nomg(svl) - value );
1895 dVAR; dSP; dATARGET; SV *svl, *svr;
1896 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1900 const IV shift = SvIV_nomg(svr);
1901 if (PL_op->op_private & HINT_INTEGER) {
1902 const IV i = SvIV_nomg(svl);
1906 const UV u = SvUV_nomg(svl);
1915 dVAR; dSP; dATARGET; SV *svl, *svr;
1916 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1920 const IV shift = SvIV_nomg(svr);
1921 if (PL_op->op_private & HINT_INTEGER) {
1922 const IV i = SvIV_nomg(svl);
1926 const UV u = SvUV_nomg(svl);
1938 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1942 (SvIOK_notUV(left) && SvIOK_notUV(right))
1943 ? (SvIVX(left) < SvIVX(right))
1944 : (do_ncmp(left, right) == -1)
1954 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1958 (SvIOK_notUV(left) && SvIOK_notUV(right))
1959 ? (SvIVX(left) > SvIVX(right))
1960 : (do_ncmp(left, right) == 1)
1970 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1974 (SvIOK_notUV(left) && SvIOK_notUV(right))
1975 ? (SvIVX(left) <= SvIVX(right))
1976 : (do_ncmp(left, right) <= 0)
1986 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1990 (SvIOK_notUV(left) && SvIOK_notUV(right))
1991 ? (SvIVX(left) >= SvIVX(right))
1992 : ( (do_ncmp(left, right) & 2) == 0)
2002 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2006 (SvIOK_notUV(left) && SvIOK_notUV(right))
2007 ? (SvIVX(left) != SvIVX(right))
2008 : (do_ncmp(left, right) != 0)
2013 /* compare left and right SVs. Returns:
2017 * 2: left or right was a NaN
2020 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2024 PERL_ARGS_ASSERT_DO_NCMP;
2025 #ifdef PERL_PRESERVE_IVUV
2026 SvIV_please_nomg(right);
2027 /* Fortunately it seems NaN isn't IOK */
2029 SvIV_please_nomg(left);
2032 const IV leftiv = SvIVX(left);
2033 if (!SvUOK(right)) {
2034 /* ## IV <=> IV ## */
2035 const IV rightiv = SvIVX(right);
2036 return (leftiv > rightiv) - (leftiv < rightiv);
2038 /* ## IV <=> UV ## */
2040 /* As (b) is a UV, it's >=0, so it must be < */
2043 const UV rightuv = SvUVX(right);
2044 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2049 /* ## UV <=> UV ## */
2050 const UV leftuv = SvUVX(left);
2051 const UV rightuv = SvUVX(right);
2052 return (leftuv > rightuv) - (leftuv < rightuv);
2054 /* ## UV <=> IV ## */
2056 const IV rightiv = SvIVX(right);
2058 /* As (a) is a UV, it's >=0, so it cannot be < */
2061 const UV leftuv = SvUVX(left);
2062 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2070 NV const rnv = SvNV_nomg(right);
2071 NV const lnv = SvNV_nomg(left);
2073 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2074 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2077 return (lnv > rnv) - (lnv < rnv);
2096 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2099 value = do_ncmp(left, right);
2114 int amg_type = sle_amg;
2118 switch (PL_op->op_type) {
2137 tryAMAGICbin_MG(amg_type, AMGf_set);
2140 const int cmp = (IN_LOCALE_RUNTIME
2141 ? sv_cmp_locale_flags(left, right, 0)
2142 : sv_cmp_flags(left, right, 0));
2143 SETs(boolSV(cmp * multiplier < rhs));
2151 tryAMAGICbin_MG(seq_amg, AMGf_set);
2154 SETs(boolSV(sv_eq_flags(left, right, 0)));
2162 tryAMAGICbin_MG(sne_amg, AMGf_set);
2165 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2173 tryAMAGICbin_MG(scmp_amg, 0);
2176 const int cmp = (IN_LOCALE_RUNTIME
2177 ? sv_cmp_locale_flags(left, right, 0)
2178 : sv_cmp_flags(left, right, 0));
2186 dVAR; dSP; dATARGET;
2187 tryAMAGICbin_MG(band_amg, AMGf_assign);
2190 if (SvNIOKp(left) || SvNIOKp(right)) {
2191 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2192 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2193 if (PL_op->op_private & HINT_INTEGER) {
2194 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2198 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2201 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2202 if (right_ro_nonnum) SvNIOK_off(right);
2205 do_vop(PL_op->op_type, TARG, left, right);
2214 dVAR; dSP; dATARGET;
2215 const int op_type = PL_op->op_type;
2217 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2220 if (SvNIOKp(left) || SvNIOKp(right)) {
2221 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2222 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2223 if (PL_op->op_private & HINT_INTEGER) {
2224 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2225 const IV r = SvIV_nomg(right);
2226 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2230 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2231 const UV r = SvUV_nomg(right);
2232 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2235 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2236 if (right_ro_nonnum) SvNIOK_off(right);
2239 do_vop(op_type, TARG, left, right);
2249 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2251 SV * const sv = TOPs;
2252 const int flags = SvFLAGS(sv);
2254 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
2258 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2259 /* It's publicly an integer, or privately an integer-not-float */
2262 if (SvIVX(sv) == IV_MIN) {
2263 /* 2s complement assumption. */
2264 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2267 else if (SvUVX(sv) <= IV_MAX) {
2272 else if (SvIVX(sv) != IV_MIN) {
2276 #ifdef PERL_PRESERVE_IVUV
2284 SETn(-SvNV_nomg(sv));
2285 else if (SvPOKp(sv)) {
2287 const char * const s = SvPV_nomg_const(sv, len);
2288 if (isIDFIRST(*s)) {
2289 sv_setpvs(TARG, "-");
2292 else if (*s == '+' || *s == '-') {
2293 sv_setsv_nomg(TARG, sv);
2294 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2296 else if (DO_UTF8(sv)) {
2297 SvIV_please_nomg(sv);
2299 goto oops_its_an_int;
2301 sv_setnv(TARG, -SvNV_nomg(sv));
2303 sv_setpvs(TARG, "-");
2308 SvIV_please_nomg(sv);
2310 goto oops_its_an_int;
2311 sv_setnv(TARG, -SvNV_nomg(sv));
2316 SETn(-SvNV_nomg(sv));
2324 tryAMAGICun_MG(not_amg, AMGf_set);
2325 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2332 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2336 if (PL_op->op_private & HINT_INTEGER) {
2337 const IV i = ~SvIV_nomg(sv);
2341 const UV u = ~SvUV_nomg(sv);
2350 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2351 sv_setsv_nomg(TARG, sv);
2352 tmps = (U8*)SvPV_force_nomg(TARG, len);
2355 /* Calculate exact length, let's not estimate. */
2360 U8 * const send = tmps + len;
2361 U8 * const origtmps = tmps;
2362 const UV utf8flags = UTF8_ALLOW_ANYUV;
2364 while (tmps < send) {
2365 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2367 targlen += UNISKIP(~c);
2373 /* Now rewind strings and write them. */
2380 Newx(result, targlen + 1, U8);
2382 while (tmps < send) {
2383 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2385 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2388 sv_usepvn_flags(TARG, (char*)result, targlen,
2389 SV_HAS_TRAILING_NUL);
2396 Newx(result, nchar + 1, U8);
2398 while (tmps < send) {
2399 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2404 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2412 register long *tmpl;
2413 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2416 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2421 for ( ; anum > 0; anum--, tmps++)
2429 /* integer versions of some of the above */
2433 dVAR; dSP; dATARGET;
2434 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2437 SETi( left * right );
2445 dVAR; dSP; dATARGET;
2446 tryAMAGICbin_MG(div_amg, AMGf_assign);
2449 IV value = SvIV_nomg(right);
2451 DIE(aTHX_ "Illegal division by zero");
2452 num = SvIV_nomg(left);
2454 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2458 value = num / value;
2464 #if defined(__GLIBC__) && IVSIZE == 8
2471 /* This is the vanilla old i_modulo. */
2472 dVAR; dSP; dATARGET;
2473 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2477 DIE(aTHX_ "Illegal modulus zero");
2478 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2482 SETi( left % right );
2487 #if defined(__GLIBC__) && IVSIZE == 8
2492 /* This is the i_modulo with the workaround for the _moddi3 bug
2493 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2494 * See below for pp_i_modulo. */
2495 dVAR; dSP; dATARGET;
2496 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2500 DIE(aTHX_ "Illegal modulus zero");
2501 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2505 SETi( left % PERL_ABS(right) );
2512 dVAR; dSP; dATARGET;
2513 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2517 DIE(aTHX_ "Illegal modulus zero");
2518 /* The assumption is to use hereafter the old vanilla version... */
2520 PL_ppaddr[OP_I_MODULO] =
2522 /* .. but if we have glibc, we might have a buggy _moddi3
2523 * (at least glicb 2.2.5 is known to have this bug), in other
2524 * words our integer modulus with negative quad as the second
2525 * argument might be broken. Test for this and re-patch the
2526 * opcode dispatch table if that is the case, remembering to
2527 * also apply the workaround so that this first round works
2528 * right, too. See [perl #9402] for more information. */
2532 /* Cannot do this check with inlined IV constants since
2533 * that seems to work correctly even with the buggy glibc. */
2535 /* Yikes, we have the bug.
2536 * Patch in the workaround version. */
2538 PL_ppaddr[OP_I_MODULO] =
2539 &Perl_pp_i_modulo_1;
2540 /* Make certain we work right this time, too. */
2541 right = PERL_ABS(right);
2544 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2548 SETi( left % right );
2556 dVAR; dSP; dATARGET;
2557 tryAMAGICbin_MG(add_amg, AMGf_assign);
2559 dPOPTOPiirl_ul_nomg;
2560 SETi( left + right );
2567 dVAR; dSP; dATARGET;
2568 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2570 dPOPTOPiirl_ul_nomg;
2571 SETi( left - right );
2579 tryAMAGICbin_MG(lt_amg, AMGf_set);
2582 SETs(boolSV(left < right));
2590 tryAMAGICbin_MG(gt_amg, AMGf_set);
2593 SETs(boolSV(left > right));
2601 tryAMAGICbin_MG(le_amg, AMGf_set);
2604 SETs(boolSV(left <= right));
2612 tryAMAGICbin_MG(ge_amg, AMGf_set);
2615 SETs(boolSV(left >= right));
2623 tryAMAGICbin_MG(eq_amg, AMGf_set);
2626 SETs(boolSV(left == right));
2634 tryAMAGICbin_MG(ne_amg, AMGf_set);
2637 SETs(boolSV(left != right));
2645 tryAMAGICbin_MG(ncmp_amg, 0);
2652 else if (left < right)
2664 tryAMAGICun_MG(neg_amg, 0);
2666 SV * const sv = TOPs;
2667 IV const i = SvIV_nomg(sv);
2673 /* High falutin' math. */
2678 tryAMAGICbin_MG(atan2_amg, 0);
2681 SETn(Perl_atan2(left, right));
2689 int amg_type = sin_amg;
2690 const char *neg_report = NULL;
2691 NV (*func)(NV) = Perl_sin;
2692 const int op_type = PL_op->op_type;
2709 amg_type = sqrt_amg;
2711 neg_report = "sqrt";
2716 tryAMAGICun_MG(amg_type, 0);
2718 SV * const arg = POPs;
2719 const NV value = SvNV_nomg(arg);
2721 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2722 SET_NUMERIC_STANDARD();
2723 /* diag_listed_as: Can't take log of %g */
2724 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2727 XPUSHn(func(value));
2732 /* Support Configure command-line overrides for rand() functions.
2733 After 5.005, perhaps we should replace this by Configure support
2734 for drand48(), random(), or rand(). For 5.005, though, maintain
2735 compatibility by calling rand() but allow the user to override it.
2736 See INSTALL for details. --Andy Dougherty 15 July 1998
2738 /* Now it's after 5.005, and Configure supports drand48() and random(),
2739 in addition to rand(). So the overrides should not be needed any more.
2740 --Jarkko Hietaniemi 27 September 1998
2743 #ifndef HAS_DRAND48_PROTO
2744 extern double drand48 (void);
2754 value = 1.0; (void)POPs;
2760 if (!PL_srand_called) {
2761 (void)seedDrand01((Rand_seed_t)seed());
2762 PL_srand_called = TRUE;
2772 const UV anum = (MAXARG < 1 || (!TOPs && !POPs)) ? seed() : POPu;
2773 (void)seedDrand01((Rand_seed_t)anum);
2774 PL_srand_called = TRUE;
2778 /* Historically srand always returned true. We can avoid breaking
2780 sv_setpvs(TARG, "0 but true");
2789 tryAMAGICun_MG(int_amg, AMGf_numeric);
2791 SV * const sv = TOPs;
2792 const IV iv = SvIV_nomg(sv);
2793 /* XXX it's arguable that compiler casting to IV might be subtly
2794 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2795 else preferring IV has introduced a subtle behaviour change bug. OTOH
2796 relying on floating point to be accurate is a bug. */
2801 else if (SvIOK(sv)) {
2803 SETu(SvUV_nomg(sv));
2808 const NV value = SvNV_nomg(sv);
2810 if (value < (NV)UV_MAX + 0.5) {
2813 SETn(Perl_floor(value));
2817 if (value > (NV)IV_MIN - 0.5) {
2820 SETn(Perl_ceil(value));
2831 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2833 SV * const sv = TOPs;
2834 /* This will cache the NV value if string isn't actually integer */
2835 const IV iv = SvIV_nomg(sv);
2840 else if (SvIOK(sv)) {
2841 /* IVX is precise */
2843 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2851 /* 2s complement assumption. Also, not really needed as
2852 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2858 const NV value = SvNV_nomg(sv);
2872 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2876 SV* const sv = POPs;
2878 tmps = (SvPV_const(sv, len));
2880 /* If Unicode, try to downgrade
2881 * If not possible, croak. */
2882 SV* const tsv = sv_2mortal(newSVsv(sv));
2885 sv_utf8_downgrade(tsv, FALSE);
2886 tmps = SvPV_const(tsv, len);
2888 if (PL_op->op_type == OP_HEX)
2891 while (*tmps && len && isSPACE(*tmps))
2895 if (*tmps == 'x' || *tmps == 'X') {
2897 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2899 else if (*tmps == 'b' || *tmps == 'B')
2900 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2902 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2904 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2918 SV * const sv = TOPs;
2920 if (SvGAMAGIC(sv)) {
2921 /* For an overloaded or magic scalar, we can't know in advance if
2922 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2923 it likes to cache the length. Maybe that should be a documented
2928 = sv_2pv_flags(sv, &len,
2929 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2932 if (!SvPADTMP(TARG)) {
2933 sv_setsv(TARG, &PL_sv_undef);
2938 else if (DO_UTF8(sv)) {
2939 SETi(utf8_length((U8*)p, (U8*)p + len));
2943 } else if (SvOK(sv)) {
2944 /* Neither magic nor overloaded. */
2946 SETi(sv_len_utf8(sv));
2950 if (!SvPADTMP(TARG)) {
2951 sv_setsv_nomg(TARG, &PL_sv_undef);
2959 /* Returns false if substring is completely outside original string.
2960 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2961 always be true for an explicit 0.
2964 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2965 bool pos1_is_uv, IV len_iv,
2966 bool len_is_uv, STRLEN *posp,
2972 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2974 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2975 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2978 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2981 if (len_iv || len_is_uv) {
2982 if (!len_is_uv && len_iv < 0) {
2983 pos2_iv = curlen + len_iv;
2985 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2988 } else { /* len_iv >= 0 */
2989 if (!pos1_is_uv && pos1_iv < 0) {
2990 pos2_iv = pos1_iv + len_iv;
2991 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2993 if ((UV)len_iv > curlen-(UV)pos1_iv)
2996 pos2_iv = pos1_iv+len_iv;
3006 if (!pos2_is_uv && pos2_iv < 0) {
3007 if (!pos1_is_uv && pos1_iv < 0)
3011 else if (!pos1_is_uv && pos1_iv < 0)
3014 if ((UV)pos2_iv < (UV)pos1_iv)
3016 if ((UV)pos2_iv > curlen)
3019 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3020 *posp = (STRLEN)( (UV)pos1_iv );
3021 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3038 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3039 const bool rvalue = (GIMME_V != G_VOID);
3042 const char *repl = NULL;
3044 int num_args = PL_op->op_private & 7;
3045 bool repl_need_utf8_upgrade = FALSE;
3046 bool repl_is_utf8 = FALSE;
3050 if(!(repl_sv = POPs)) num_args--;
3052 if ((len_sv = POPs)) {
3053 len_iv = SvIV(len_sv);
3054 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3059 pos1_iv = SvIV(pos_sv);
3060 pos1_is_uv = SvIOK_UV(pos_sv);
3062 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3068 repl = SvPV_const(repl_sv, repl_len);
3069 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
3072 sv_utf8_upgrade(sv);
3074 else if (DO_UTF8(sv))
3075 repl_need_utf8_upgrade = TRUE;
3079 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3080 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3082 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3084 pos1_is_uv || pos1_iv >= 0
3085 ? (STRLEN)(UV)pos1_iv
3086 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3088 len_is_uv || len_iv > 0
3089 ? (STRLEN)(UV)len_iv
3090 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3093 PUSHs(ret); /* avoid SvSETMAGIC here */
3096 tmps = SvPV_const(sv, curlen);
3098 utf8_curlen = sv_len_utf8(sv);
3099 if (utf8_curlen == curlen)
3102 curlen = utf8_curlen;
3108 STRLEN pos, len, byte_len, byte_pos;
3110 if (!translate_substr_offsets(
3111 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3115 byte_pos = utf8_curlen
3116 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3121 SvTAINTED_off(TARG); /* decontaminate */
3122 SvUTF8_off(TARG); /* decontaminate */
3123 sv_setpvn(TARG, tmps, byte_len);
3124 #ifdef USE_LOCALE_COLLATE
3125 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3132 SV* repl_sv_copy = NULL;
3134 if (repl_need_utf8_upgrade) {
3135 repl_sv_copy = newSVsv(repl_sv);
3136 sv_utf8_upgrade(repl_sv_copy);
3137 repl = SvPV_const(repl_sv_copy, repl_len);
3138 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3141 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3142 "Attempt to use reference as lvalue in substr"
3146 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3149 SvREFCNT_dec(repl_sv_copy);
3161 Perl_croak(aTHX_ "substr outside of string");
3162 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3169 register const IV size = POPi;
3170 register const IV offset = POPi;
3171 register SV * const src = POPs;
3172 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3175 if (lvalue) { /* it's an lvalue! */
3176 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3177 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3179 LvTARG(ret) = SvREFCNT_inc_simple(src);
3180 LvTARGOFF(ret) = offset;
3181 LvTARGLEN(ret) = size;
3185 SvTAINTED_off(TARG); /* decontaminate */
3189 sv_setuv(ret, do_vecget(src, offset, size));
3205 const char *little_p;
3208 const bool is_index = PL_op->op_type == OP_INDEX;
3209 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3215 big_p = SvPV_const(big, biglen);
3216 little_p = SvPV_const(little, llen);
3218 big_utf8 = DO_UTF8(big);
3219 little_utf8 = DO_UTF8(little);
3220 if (big_utf8 ^ little_utf8) {
3221 /* One needs to be upgraded. */
3222 if (little_utf8 && !PL_encoding) {
3223 /* Well, maybe instead we might be able to downgrade the small
3225 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3228 /* If the large string is ISO-8859-1, and it's not possible to
3229 convert the small string to ISO-8859-1, then there is no
3230 way that it could be found anywhere by index. */
3235 /* At this point, pv is a malloc()ed string. So donate it to temp
3236 to ensure it will get free()d */
3237 little = temp = newSV(0);
3238 sv_usepvn(temp, pv, llen);
3239 little_p = SvPVX(little);
3242 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3245 sv_recode_to_utf8(temp, PL_encoding);
3247 sv_utf8_upgrade(temp);
3252 big_p = SvPV_const(big, biglen);
3255 little_p = SvPV_const(little, llen);
3259 if (SvGAMAGIC(big)) {
3260 /* Life just becomes a lot easier if I use a temporary here.
3261 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3262 will trigger magic and overloading again, as will fbm_instr()
3264 big = newSVpvn_flags(big_p, biglen,
3265 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3268 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3269 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3270 warn on undef, and we've already triggered a warning with the
3271 SvPV_const some lines above. We can't remove that, as we need to
3272 call some SvPV to trigger overloading early and find out if the
3274 This is all getting to messy. The API isn't quite clean enough,
3275 because data access has side effects.
3277 little = newSVpvn_flags(little_p, llen,
3278 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3279 little_p = SvPVX(little);
3283 offset = is_index ? 0 : biglen;
3285 if (big_utf8 && offset > 0)
3286 sv_pos_u2b(big, &offset, 0);
3292 else if (offset > (I32)biglen)
3294 if (!(little_p = is_index
3295 ? fbm_instr((unsigned char*)big_p + offset,
3296 (unsigned char*)big_p + biglen, little, 0)
3297 : rninstr(big_p, big_p + offset,
3298 little_p, little_p + llen)))
3301 retval = little_p - big_p;
3302 if (retval > 0 && big_utf8)
3303 sv_pos_b2u(big, &retval);
3313 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3314 SvTAINTED_off(TARG);
3315 do_sprintf(TARG, SP-MARK, MARK+1);
3316 TAINT_IF(SvTAINTED(TARG));
3328 const U8 *s = (U8*)SvPV_const(argsv, len);
3330 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3331 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3332 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3336 XPUSHu(DO_UTF8(argsv) ?
3337 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3349 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3351 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3353 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3355 (void) POPs; /* Ignore the argument value. */
3356 value = UNICODE_REPLACEMENT;
3362 SvUPGRADE(TARG,SVt_PV);
3364 if (value > 255 && !IN_BYTES) {
3365 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3366 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3367 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3369 (void)SvPOK_only(TARG);
3378 *tmps++ = (char)value;
3380 (void)SvPOK_only(TARG);
3382 if (PL_encoding && !IN_BYTES) {
3383 sv_recode_to_utf8(TARG, PL_encoding);
3385 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3386 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3390 *tmps++ = (char)value;
3406 const char *tmps = SvPV_const(left, len);
3408 if (DO_UTF8(left)) {
3409 /* If Unicode, try to downgrade.
3410 * If not possible, croak.
3411 * Yes, we made this up. */
3412 SV* const tsv = sv_2mortal(newSVsv(left));
3415 sv_utf8_downgrade(tsv, FALSE);
3416 tmps = SvPV_const(tsv, len);
3418 # ifdef USE_ITHREADS
3420 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3421 /* This should be threadsafe because in ithreads there is only
3422 * one thread per interpreter. If this would not be true,
3423 * we would need a mutex to protect this malloc. */
3424 PL_reentrant_buffer->_crypt_struct_buffer =
3425 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3426 #if defined(__GLIBC__) || defined(__EMX__)
3427 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3428 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3429 /* work around glibc-2.2.5 bug */
3430 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3434 # endif /* HAS_CRYPT_R */
3435 # endif /* USE_ITHREADS */
3437 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3439 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3445 "The crypt() function is unimplemented due to excessive paranoia.");
3449 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3450 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3452 /* Generates code to store a unicode codepoint c that is known to occupy
3453 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3454 * and p is advanced to point to the next available byte after the two bytes */
3455 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3457 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3458 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3463 /* Actually is both lcfirst() and ucfirst(). Only the first character
3464 * changes. This means that possibly we can change in-place, ie., just
3465 * take the source and change that one character and store it back, but not
3466 * if read-only etc, or if the length changes */
3471 STRLEN slen; /* slen is the byte length of the whole SV. */
3474 bool inplace; /* ? Convert first char only, in-place */
3475 bool doing_utf8 = FALSE; /* ? using utf8 */
3476 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3477 const int op_type = PL_op->op_type;
3480 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3481 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3482 * stored as UTF-8 at s. */
3483 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3484 * lowercased) character stored in tmpbuf. May be either
3485 * UTF-8 or not, but in either case is the number of bytes */
3486 bool tainted = FALSE;
3490 s = (const U8*)SvPV_nomg_const(source, slen);
3492 if (ckWARN(WARN_UNINITIALIZED))
3493 report_uninit(source);
3498 /* We may be able to get away with changing only the first character, in
3499 * place, but not if read-only, etc. Later we may discover more reasons to
3500 * not convert in-place. */
3501 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3503 /* First calculate what the changed first character should be. This affects
3504 * whether we can just swap it out, leaving the rest of the string unchanged,
3505 * or even if have to convert the dest to UTF-8 when the source isn't */
3507 if (! slen) { /* If empty */
3508 need = 1; /* still need a trailing NUL */
3511 else if (DO_UTF8(source)) { /* Is the source utf8? */
3514 if (op_type == OP_UCFIRST) {
3515 _to_utf8_title_flags(s, tmpbuf, &tculen,
3516 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3519 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3520 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3523 /* we can't do in-place if the length changes. */
3524 if (ulen != tculen) inplace = FALSE;
3525 need = slen + 1 - ulen + tculen;
3527 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3528 * latin1 is treated as caseless. Note that a locale takes
3530 ulen = 1; /* Original character is 1 byte */
3531 tculen = 1; /* Most characters will require one byte, but this will
3532 * need to be overridden for the tricky ones */
3535 if (op_type == OP_LCFIRST) {
3537 /* lower case the first letter: no trickiness for any character */
3538 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3539 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3542 else if (IN_LOCALE_RUNTIME) {
3543 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3544 * have upper and title case different
3547 else if (! IN_UNI_8_BIT) {
3548 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3549 * on EBCDIC machines whatever the
3550 * native function does */
3552 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3553 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3555 assert(tculen == 2);
3557 /* If the result is an upper Latin1-range character, it can
3558 * still be represented in one byte, which is its ordinal */
3559 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3560 *tmpbuf = (U8) title_ord;
3564 /* Otherwise it became more than one ASCII character (in
3565 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3566 * beyond Latin1, so the number of bytes changed, so can't
3567 * replace just the first character in place. */
3570 /* If the result won't fit in a byte, the entire result will
3571 * have to be in UTF-8. Assume worst case sizing in
3572 * conversion. (all latin1 characters occupy at most two bytes
3574 if (title_ord > 255) {
3576 convert_source_to_utf8 = TRUE;
3577 need = slen * 2 + 1;
3579 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3580 * (both) characters whose title case is above 255 is
3584 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3585 need = slen + 1 + 1;
3589 } /* End of use Unicode (Latin1) semantics */
3590 } /* End of changing the case of the first character */
3592 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3593 * generate the result */
3596 /* We can convert in place. This means we change just the first
3597 * character without disturbing the rest; no need to grow */
3599 s = d = (U8*)SvPV_force_nomg(source, slen);
3605 /* Here, we can't convert in place; we earlier calculated how much
3606 * space we will need, so grow to accommodate that */
3607 SvUPGRADE(dest, SVt_PV);
3608 d = (U8*)SvGROW(dest, need);
3609 (void)SvPOK_only(dest);
3616 if (! convert_source_to_utf8) {
3618 /* Here both source and dest are in UTF-8, but have to create
3619 * the entire output. We initialize the result to be the
3620 * title/lower cased first character, and then append the rest
3622 sv_setpvn(dest, (char*)tmpbuf, tculen);
3624 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3628 const U8 *const send = s + slen;
3630 /* Here the dest needs to be in UTF-8, but the source isn't,
3631 * except we earlier UTF-8'd the first character of the source
3632 * into tmpbuf. First put that into dest, and then append the
3633 * rest of the source, converting it to UTF-8 as we go. */
3635 /* Assert tculen is 2 here because the only two characters that
3636 * get to this part of the code have 2-byte UTF-8 equivalents */
3638 *d++ = *(tmpbuf + 1);
3639 s++; /* We have just processed the 1st char */
3641 for (; s < send; s++) {
3642 d = uvchr_to_utf8(d, *s);
3645 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3649 else { /* in-place UTF-8. Just overwrite the first character */
3650 Copy(tmpbuf, d, tculen, U8);
3651 SvCUR_set(dest, need - 1);
3659 else { /* Neither source nor dest are in or need to be UTF-8 */
3661 if (IN_LOCALE_RUNTIME) {
3665 if (inplace) { /* in-place, only need to change the 1st char */
3668 else { /* Not in-place */
3670 /* Copy the case-changed character(s) from tmpbuf */
3671 Copy(tmpbuf, d, tculen, U8);
3672 d += tculen - 1; /* Code below expects d to point to final
3673 * character stored */
3676 else { /* empty source */
3677 /* See bug #39028: Don't taint if empty */
3681 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3682 * the destination to retain that flag */
3686 if (!inplace) { /* Finish the rest of the string, unchanged */
3687 /* This will copy the trailing NUL */
3688 Copy(s + 1, d + 1, slen, U8);
3689 SvCUR_set(dest, need - 1);
3692 if (dest != source && SvTAINTED(source))
3698 /* There's so much setup/teardown code common between uc and lc, I wonder if
3699 it would be worth merging the two, and just having a switch outside each
3700 of the three tight loops. There is less and less commonality though */
3714 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3715 && SvTEMP(source) && !DO_UTF8(source)
3716 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3718 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3719 * make the loop tight, so we overwrite the source with the dest before
3720 * looking at it, and we need to look at the original source
3721 * afterwards. There would also need to be code added to handle
3722 * switching to not in-place in midstream if we run into characters
3723 * that change the length.
3726 s = d = (U8*)SvPV_force_nomg(source, len);
3733 /* The old implementation would copy source into TARG at this point.
3734 This had the side effect that if source was undef, TARG was now
3735 an undefined SV with PADTMP set, and they don't warn inside
3736 sv_2pv_flags(). However, we're now getting the PV direct from
3737 source, which doesn't have PADTMP set, so it would warn. Hence the
3741 s = (const U8*)SvPV_nomg_const(source, len);
3743 if (ckWARN(WARN_UNINITIALIZED))
3744 report_uninit(source);
3750 SvUPGRADE(dest, SVt_PV);
3751 d = (U8*)SvGROW(dest, min);
3752 (void)SvPOK_only(dest);
3757 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3758 to check DO_UTF8 again here. */
3760 if (DO_UTF8(source)) {
3761 const U8 *const send = s + len;
3762 U8 tmpbuf[UTF8_MAXBYTES+1];
3763 bool tainted = FALSE;
3765 /* All occurrences of these are to be moved to follow any other marks.
3766 * This is context-dependent. We may not be passed enough context to
3767 * move the iota subscript beyond all of them, but we do the best we can
3768 * with what we're given. The result is always better than if we
3769 * hadn't done this. And, the problem would only arise if we are
3770 * passed a character without all its combining marks, which would be
3771 * the caller's mistake. The information this is based on comes from a
3772 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3773 * itself) and so can't be checked properly to see if it ever gets
3774 * revised. But the likelihood of it changing is remote */
3775 bool in_iota_subscript = FALSE;
3781 if (in_iota_subscript && ! is_utf8_mark(s)) {
3783 /* A non-mark. Time to output the iota subscript */
3784 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3785 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3787 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3788 in_iota_subscript = FALSE;
3791 /* Then handle the current character. Get the changed case value
3792 * and copy it to the output buffer */
3795 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3796 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3797 if (uv == GREEK_CAPITAL_LETTER_IOTA
3798 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3800 in_iota_subscript = TRUE;
3803 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3804 /* If the eventually required minimum size outgrows the
3805 * available space, we need to grow. */
3806 const UV o = d - (U8*)SvPVX_const(dest);
3808 /* If someone uppercases one million U+03B0s we SvGROW()
3809 * one million times. Or we could try guessing how much to
3810 * allocate without allocating too much. Such is life.
3811 * See corresponding comment in lc code for another option
3814 d = (U8*)SvPVX(dest) + o;
3816 Copy(tmpbuf, d, ulen, U8);
3821 if (in_iota_subscript) {
3822 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3827 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3833 else { /* Not UTF-8 */
3835 const U8 *const send = s + len;
3837 /* Use locale casing if in locale; regular style if not treating
3838 * latin1 as having case; otherwise the latin1 casing. Do the
3839 * whole thing in a tight loop, for speed, */
3840 if (IN_LOCALE_RUNTIME) {
3843 for (; s < send; d++, s++)
3844 *d = toUPPER_LC(*s);
3846 else if (! IN_UNI_8_BIT) {
3847 for (; s < send; d++, s++) {
3852 for (; s < send; d++, s++) {
3853 *d = toUPPER_LATIN1_MOD(*s);
3854 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3856 /* The mainstream case is the tight loop above. To avoid
3857 * extra tests in that, all three characters that require
3858 * special handling are mapped by the MOD to the one tested
3860 * Use the source to distinguish between the three cases */
3862 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3864 /* uc() of this requires 2 characters, but they are
3865 * ASCII. If not enough room, grow the string */
3866 if (SvLEN(dest) < ++min) {
3867 const UV o = d - (U8*)SvPVX_const(dest);
3869 d = (U8*)SvPVX(dest) + o;
3871 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3872 continue; /* Back to the tight loop; still in ASCII */
3875 /* The other two special handling characters have their
3876 * upper cases outside the latin1 range, hence need to be
3877 * in UTF-8, so the whole result needs to be in UTF-8. So,
3878 * here we are somewhere in the middle of processing a
3879 * non-UTF-8 string, and realize that we will have to convert
3880 * the whole thing to UTF-8. What to do? There are
3881 * several possibilities. The simplest to code is to
3882 * convert what we have so far, set a flag, and continue on
3883 * in the loop. The flag would be tested each time through
3884 * the loop, and if set, the next character would be
3885 * converted to UTF-8 and stored. But, I (khw) didn't want
3886 * to slow down the mainstream case at all for this fairly
3887 * rare case, so I didn't want to add a test that didn't
3888 * absolutely have to be there in the loop, besides the
3889 * possibility that it would get too complicated for
3890 * optimizers to deal with. Another possibility is to just
3891 * give up, convert the source to UTF-8, and restart the
3892 * function that way. Another possibility is to convert
3893 * both what has already been processed and what is yet to
3894 * come separately to UTF-8, then jump into the loop that
3895 * handles UTF-8. But the most efficient time-wise of the
3896 * ones I could think of is what follows, and turned out to
3897 * not require much extra code. */
3899 /* Convert what we have so far into UTF-8, telling the
3900 * function that we know it should be converted, and to
3901 * allow extra space for what we haven't processed yet.
3902 * Assume the worst case space requirements for converting
3903 * what we haven't processed so far: that it will require
3904 * two bytes for each remaining source character, plus the
3905 * NUL at the end. This may cause the string pointer to
3906 * move, so re-find it. */
3908 len = d - (U8*)SvPVX_const(dest);
3909 SvCUR_set(dest, len);
3910 len = sv_utf8_upgrade_flags_grow(dest,
3911 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3913 d = (U8*)SvPVX(dest) + len;
3915 /* Now process the remainder of the source, converting to
3916 * upper and UTF-8. If a resulting byte is invariant in
3917 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3918 * append it to the output. */
3919 for (; s < send; s++) {
3920 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3924 /* Here have processed the whole source; no need to continue
3925 * with the outer loop. Each character has been converted
3926 * to upper case and converted to UTF-8 */
3929 } /* End of processing all latin1-style chars */
3930 } /* End of processing all chars */
3931 } /* End of source is not empty */
3933 if (source != dest) {
3934 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3935 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3937 } /* End of isn't utf8 */
3938 if (dest != source && SvTAINTED(source))
3957 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3958 && SvTEMP(source) && !DO_UTF8(source)) {
3960 /* We can convert in place, as lowercasing anything in the latin1 range
3961 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3963 s = d = (U8*)SvPV_force_nomg(source, len);
3970 /* The old implementation would copy source into TARG at this point.
3971 This had the side effect that if source was undef, TARG was now
3972 an undefined SV with PADTMP set, and they don't warn inside
3973 sv_2pv_flags(). However, we're now getting the PV direct from
3974 source, which doesn't have PADTMP set, so it would warn. Hence the
3978 s = (const U8*)SvPV_nomg_const(source, len);
3980 if (ckWARN(WARN_UNINITIALIZED))
3981 report_uninit(source);
3987 SvUPGRADE(dest, SVt_PV);
3988 d = (U8*)SvGROW(dest, min);
3989 (void)SvPOK_only(dest);
3994 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3995 to check DO_UTF8 again here. */
3997 if (DO_UTF8(source)) {
3998 const U8 *const send = s + len;
3999 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4000 bool tainted = FALSE;
4003 const STRLEN u = UTF8SKIP(s);
4006 _to_utf8_lower_flags(s, tmpbuf, &ulen,
4007 cBOOL(IN_LOCALE_RUNTIME), &tainted);
4009 /* Here is where we would do context-sensitive actions. See the
4010 * commit message for this comment for why there isn't any */
4012 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4014 /* If the eventually required minimum size outgrows the
4015 * available space, we need to grow. */
4016 const UV o = d - (U8*)SvPVX_const(dest);
4018 /* If someone lowercases one million U+0130s we SvGROW() one
4019 * million times. Or we could try guessing how much to
4020 * allocate without allocating too much. Such is life.
4021 * Another option would be to grow an extra byte or two more
4022 * each time we need to grow, which would cut down the million
4023 * to 500K, with little waste */
4025 d = (U8*)SvPVX(dest) + o;
4028 /* Copy the newly lowercased letter to the output buffer we're
4030 Copy(tmpbuf, d, ulen, U8);
4033 } /* End of looping through the source string */
4036 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4041 } else { /* Not utf8 */
4043 const U8 *const send = s + len;
4045 /* Use locale casing if in locale; regular style if not treating
4046 * latin1 as having case; otherwise the latin1 casing. Do the
4047 * whole thing in a tight loop, for speed, */
4048 if (IN_LOCALE_RUNTIME) {
4051 for (; s < send; d++, s++)
4052 *d = toLOWER_LC(*s);
4054 else if (! IN_UNI_8_BIT) {
4055 for (; s < send; d++, s++) {
4060 for (; s < send; d++, s++) {
4061 *d = toLOWER_LATIN1(*s);
4065 if (source != dest) {
4067 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4070 if (dest != source && SvTAINTED(source))
4079 SV * const sv = TOPs;
4081 register const char *s = SvPV_const(sv,len);
4083 SvUTF8_off(TARG); /* decontaminate */
4086 SvUPGRADE(TARG, SVt_PV);
4087 SvGROW(TARG, (len * 2) + 1);
4091 STRLEN ulen = UTF8SKIP(s);
4092 bool to_quote = FALSE;
4094 if (UTF8_IS_INVARIANT(*s)) {
4095 if (_isQUOTEMETA(*s)) {
4099 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4101 /* In locale, we quote all non-ASCII Latin1 chars.
4102 * Otherwise use the quoting rules */
4103 if (IN_LOCALE_RUNTIME
4104 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4109 else if (_is_utf8_quotemeta((U8 *) s)) {
4124 else if (IN_UNI_8_BIT) {
4126 if (_isQUOTEMETA(*s))
4132 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4133 * including everything above ASCII */
4135 if (!isWORDCHAR_A(*s))
4141 SvCUR_set(TARG, d - SvPVX_const(TARG));
4142 (void)SvPOK_only_UTF8(TARG);
4145 sv_setpvn(TARG, s, len);
4162 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4163 const bool full_folding = TRUE;
4164 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4165 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4167 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4168 * You are welcome(?) -Hugmeir
4176 s = (const U8*)SvPV_nomg_const(source, len);
4178 if (ckWARN(WARN_UNINITIALIZED))
4179 report_uninit(source);
4186 SvUPGRADE(dest, SVt_PV);
4187 d = (U8*)SvGROW(dest, min);
4188 (void)SvPOK_only(dest);
4193 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4194 bool tainted = FALSE;
4196 const STRLEN u = UTF8SKIP(s);
4199 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4201 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4202 const UV o = d - (U8*)SvPVX_const(dest);
4204 d = (U8*)SvPVX(dest) + o;
4207 Copy(tmpbuf, d, ulen, U8);
4216 } /* Unflagged string */
4218 /* For locale, bytes, and nothing, the behavior is supposed to be the
4221 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4224 for (; s < send; d++, s++)
4225 *d = toLOWER_LC(*s);
4227 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4228 for (; s < send; d++, s++)
4232 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4233 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4234 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4235 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4238 for (; s < send; d++, s++) {
4239 if (*s == MICRO_SIGN) {
4240 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4241 * is outside of the latin-1 range. There's a couple of ways to
4242 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4243 * What we do here is upgrade what we had already casefolded,
4244 * then enter an inner loop that appends the rest of the characters
4247 len = d - (U8*)SvPVX_const(dest);
4248 SvCUR_set(dest, len);
4249 len = sv_utf8_upgrade_flags_grow(dest,
4250 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4251 /* The max expansion for latin1
4252 * chars is 1 byte becomes 2 */
4254 d = (U8*)SvPVX(dest) + len;
4256 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4258 for (; s < send; s++) {
4260 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4261 if UNI_IS_INVARIANT(fc) {
4262 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4270 Copy(tmpbuf, d, ulen, U8);
4276 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4277 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4278 * which may require growing the SV.
4280 if (SvLEN(dest) < ++min) {
4281 const UV o = d - (U8*)SvPVX_const(dest);
4283 d = (U8*)SvPVX(dest) + o;
4288 else { /* If it's not one of those two, the fold is their lower case */
4289 *d = toLOWER_LATIN1(*s);
4295 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4297 if (SvTAINTED(source))
4307 dVAR; dSP; dMARK; dORIGMARK;
4308 register AV *const av = MUTABLE_AV(POPs);
4309 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4311 if (SvTYPE(av) == SVt_PVAV) {
4312 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4313 bool can_preserve = FALSE;
4319 can_preserve = SvCANEXISTDELETE(av);
4322 if (lval && localizing) {
4325 for (svp = MARK + 1; svp <= SP; svp++) {
4326 const I32 elem = SvIV(*svp);
4330 if (max > AvMAX(av))
4334 while (++MARK <= SP) {
4336 I32 elem = SvIV(*MARK);
4337 bool preeminent = TRUE;
4339 if (localizing && can_preserve) {
4340 /* If we can determine whether the element exist,
4341 * Try to preserve the existenceness of a tied array
4342 * element by using EXISTS and DELETE if possible.
4343 * Fallback to FETCH and STORE otherwise. */
4344 preeminent = av_exists(av, elem);
4347 svp = av_fetch(av, elem, lval);
4349 if (!svp || *svp == &PL_sv_undef)
4350 DIE(aTHX_ PL_no_aelem, elem);
4353 save_aelem(av, elem, svp);
4355 SAVEADELETE(av, elem);
4358 *MARK = svp ? *svp : &PL_sv_undef;
4361 if (GIMME != G_ARRAY) {
4363 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4369 /* Smart dereferencing for keys, values and each */
4381 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4386 "Type of argument to %s must be unblessed hashref or arrayref",
4387 PL_op_desc[PL_op->op_type] );
4390 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4392 "Can't modify %s in %s",
4393 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4396 /* Delegate to correct function for op type */
4398 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4399 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4402 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4410 AV *array = MUTABLE_AV(POPs);
4411 const I32 gimme = GIMME_V;
4412 IV *iterp = Perl_av_iter_p(aTHX_ array);
4413 const IV current = (*iterp)++;
4415 if (current > av_len(array)) {
4417 if (gimme == G_SCALAR)
4425 if (gimme == G_ARRAY) {
4426 SV **const element = av_fetch(array, current, 0);
4427 PUSHs(element ? *element : &PL_sv_undef);
4436 AV *array = MUTABLE_AV(POPs);
4437 const I32 gimme = GIMME_V;
4439 *Perl_av_iter_p(aTHX_ array) = 0;
4441 if (gimme == G_SCALAR) {
4443 PUSHi(av_len(array) + 1);
4445 else if (gimme == G_ARRAY) {
4446 IV n = Perl_av_len(aTHX_ array);
4451 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4452 for (i = 0; i <= n; i++) {
4457 for (i = 0; i <= n; i++) {
4458 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4459 PUSHs(elem ? *elem : &PL_sv_undef);
4466 /* Associative arrays. */
4472 HV * hash = MUTABLE_HV(POPs);
4474 const I32 gimme = GIMME_V;
4477 /* might clobber stack_sp */
4478 entry = hv_iternext(hash);
4483 SV* const sv = hv_iterkeysv(entry);
4484 PUSHs(sv); /* won't clobber stack_sp */
4485 if (gimme == G_ARRAY) {
4488 /* might clobber stack_sp */
4489 val = hv_iterval(hash, entry);
4494 else if (gimme == G_SCALAR)
4501 S_do_delete_local(pTHX)
4505 const I32 gimme = GIMME_V;
4509 if (PL_op->op_private & OPpSLICE) {
4511 SV * const osv = POPs;
4512 const bool tied = SvRMAGICAL(osv)
4513 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4514 const bool can_preserve = SvCANEXISTDELETE(osv)
4515 || mg_find((const SV *)osv, PERL_MAGIC_env);
4516 const U32 type = SvTYPE(osv);
4517 if (type == SVt_PVHV) { /* hash element */
4518 HV * const hv = MUTABLE_HV(osv);
4519 while (++MARK <= SP) {
4520 SV * const keysv = *MARK;
4522 bool preeminent = TRUE;
4524 preeminent = hv_exists_ent(hv, keysv, 0);
4526 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4533 sv = hv_delete_ent(hv, keysv, 0, 0);
4534 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4537 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4539 *MARK = sv_mortalcopy(sv);
4545 SAVEHDELETE(hv, keysv);
4546 *MARK = &PL_sv_undef;
4550 else if (type == SVt_PVAV) { /* array element */
4551 if (PL_op->op_flags & OPf_SPECIAL) {
4552 AV * const av = MUTABLE_AV(osv);
4553 while (++MARK <= SP) {
4554 I32 idx = SvIV(*MARK);
4556 bool preeminent = TRUE;
4558 preeminent = av_exists(av, idx);
4560 SV **svp = av_fetch(av, idx, 1);
4567 sv = av_delete(av, idx, 0);
4568 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4571 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4573 *MARK = sv_mortalcopy(sv);
4579 SAVEADELETE(av, idx);
4580 *MARK = &PL_sv_undef;
4586 DIE(aTHX_ "Not a HASH reference");
4587 if (gimme == G_VOID)
4589 else if (gimme == G_SCALAR) {
4594 *++MARK = &PL_sv_undef;
4599 SV * const keysv = POPs;
4600 SV * const osv = POPs;
4601 const bool tied = SvRMAGICAL(osv)
4602 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4603 const bool can_preserve = SvCANEXISTDELETE(osv)
4604 || mg_find((const SV *)osv, PERL_MAGIC_env);
4605 const U32 type = SvTYPE(osv);
4607 if (type == SVt_PVHV) {
4608 HV * const hv = MUTABLE_HV(osv);
4609 bool preeminent = TRUE;
4611 preeminent = hv_exists_ent(hv, keysv, 0);
4613 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4620 sv = hv_delete_ent(hv, keysv, 0, 0);
4621 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4624 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4626 SV *nsv = sv_mortalcopy(sv);
4632 SAVEHDELETE(hv, keysv);
4634 else if (type == SVt_PVAV) {
4635 if (PL_op->op_flags & OPf_SPECIAL) {
4636 AV * const av = MUTABLE_AV(osv);
4637 I32 idx = SvIV(keysv);
4638 bool preeminent = TRUE;
4640 preeminent = av_exists(av, idx);
4642 SV **svp = av_fetch(av, idx, 1);
4649 sv = av_delete(av, idx, 0);
4650 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4653 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4655 SV *nsv = sv_mortalcopy(sv);
4661 SAVEADELETE(av, idx);
4664 DIE(aTHX_ "panic: avhv_delete no longer supported");
4667 DIE(aTHX_ "Not a HASH reference");
4670 if (gimme != G_VOID)
4684 if (PL_op->op_private & OPpLVAL_INTRO)
4685 return do_delete_local();
4688 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4690 if (PL_op->op_private & OPpSLICE) {
4692 HV * const hv = MUTABLE_HV(POPs);
4693 const U32 hvtype = SvTYPE(hv);
4694 if (hvtype == SVt_PVHV) { /* hash element */
4695 while (++MARK <= SP) {
4696 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4697 *MARK = sv ? sv : &PL_sv_undef;
4700 else if (hvtype == SVt_PVAV) { /* array element */
4701 if (PL_op->op_flags & OPf_SPECIAL) {
4702 while (++MARK <= SP) {
4703 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4704 *MARK = sv ? sv : &PL_sv_undef;
4709 DIE(aTHX_ "Not a HASH reference");
4712 else if (gimme == G_SCALAR) {
4717 *++MARK = &PL_sv_undef;
4723 HV * const hv = MUTABLE_HV(POPs);
4725 if (SvTYPE(hv) == SVt_PVHV)
4726 sv = hv_delete_ent(hv, keysv, discard, 0);
4727 else if (SvTYPE(hv) == SVt_PVAV) {
4728 if (PL_op->op_flags & OPf_SPECIAL)
4729 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4731 DIE(aTHX_ "panic: avhv_delete no longer supported");
4734 DIE(aTHX_ "Not a HASH reference");
4750 if (PL_op->op_private & OPpEXISTS_SUB) {
4752 SV * const sv = POPs;
4753 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4756 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4761 hv = MUTABLE_HV(POPs);
4762 if (SvTYPE(hv) == SVt_PVHV) {
4763 if (hv_exists_ent(hv, tmpsv, 0))
4766 else if (SvTYPE(hv) == SVt_PVAV) {
4767 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4768 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4773 DIE(aTHX_ "Not a HASH reference");
4780 dVAR; dSP; dMARK; dORIGMARK;
4781 register HV * const hv = MUTABLE_HV(POPs);
4782 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4783 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4784 bool can_preserve = FALSE;
4790 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4791 can_preserve = TRUE;
4794 while (++MARK <= SP) {
4795 SV * const keysv = *MARK;
4798 bool preeminent = TRUE;
4800 if (localizing && can_preserve) {
4801 /* If we can determine whether the element exist,
4802 * try to preserve the existenceness of a tied hash
4803 * element by using EXISTS and DELETE if possible.
4804 * Fallback to FETCH and STORE otherwise. */
4805 preeminent = hv_exists_ent(hv, keysv, 0);
4808 he = hv_fetch_ent(hv, keysv, lval, 0);
4809 svp = he ? &HeVAL(he) : NULL;
4812 if (!svp || !*svp || *svp == &PL_sv_undef) {
4813 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4816 if (HvNAME_get(hv) && isGV(*svp))
4817 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4818 else if (preeminent)
4819 save_helem_flags(hv, keysv, svp,
4820 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4822 SAVEHDELETE(hv, keysv);
4825 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4827 if (GIMME != G_ARRAY) {
4829 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4835 /* List operators. */
4840 if (GIMME != G_ARRAY) {
4842 *MARK = *SP; /* unwanted list, return last item */
4844 *MARK = &PL_sv_undef;
4854 SV ** const lastrelem = PL_stack_sp;
4855 SV ** const lastlelem = PL_stack_base + POPMARK;
4856 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4857 register SV ** const firstrelem = lastlelem + 1;
4858 I32 is_something_there = FALSE;
4860 register const I32 max = lastrelem - lastlelem;
4861 register SV **lelem;
4863 if (GIMME != G_ARRAY) {
4864 I32 ix = SvIV(*lastlelem);
4867 if (ix < 0 || ix >= max)
4868 *firstlelem = &PL_sv_undef;
4870 *firstlelem = firstrelem[ix];
4876 SP = firstlelem - 1;
4880 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4881 I32 ix = SvIV(*lelem);
4884 if (ix < 0 || ix >= max)
4885 *lelem = &PL_sv_undef;
4887 is_something_there = TRUE;
4888 if (!(*lelem = firstrelem[ix]))
4889 *lelem = &PL_sv_undef;
4892 if (is_something_there)
4895 SP = firstlelem - 1;
4901 dVAR; dSP; dMARK; dORIGMARK;
4902 const I32 items = SP - MARK;
4903 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4904 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4905 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4906 ? newRV_noinc(av) : av);
4912 dVAR; dSP; dMARK; dORIGMARK;
4913 HV* const hv = newHV();
4916 SV * const key = *++MARK;
4917 SV * const val = newSV(0);
4919 sv_setsv(val, *++MARK);
4921 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4922 (void)hv_store_ent(hv,key,val,0);
4925 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4926 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4931 S_deref_plain_array(pTHX_ AV *ary)
4933 if (SvTYPE(ary) == SVt_PVAV) return ary;
4934 SvGETMAGIC((SV *)ary);
4935 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4936 Perl_die(aTHX_ "Not an ARRAY reference");
4937 else if (SvOBJECT(SvRV(ary)))
4938 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4939 return (AV *)SvRV(ary);
4942 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4943 # define DEREF_PLAIN_ARRAY(ary) \
4946 SvTYPE(aRrRay) == SVt_PVAV \
4948 : S_deref_plain_array(aTHX_ aRrRay); \
4951 # define DEREF_PLAIN_ARRAY(ary) \
4953 PL_Sv = (SV *)(ary), \
4954 SvTYPE(PL_Sv) == SVt_PVAV \
4956 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4962 dVAR; dSP; dMARK; dORIGMARK;
4963 int num_args = (SP - MARK);
4964 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4968 register I32 offset;
4969 register I32 length;
4973 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4976 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4977 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4984 offset = i = SvIV(*MARK);
4986 offset += AvFILLp(ary) + 1;
4988 DIE(aTHX_ PL_no_aelem, i);
4990 length = SvIVx(*MARK++);
4992 length += AvFILLp(ary) - offset + 1;
4998 length = AvMAX(ary) + 1; /* close enough to infinity */
5002 length = AvMAX(ary) + 1;
5004 if (offset > AvFILLp(ary) + 1) {
5006 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5007 offset = AvFILLp(ary) + 1;
5009 after = AvFILLp(ary) + 1 - (offset + length);
5010 if (after < 0) { /* not that much array */
5011 length += after; /* offset+length now in array */
5017 /* At this point, MARK .. SP-1 is our new LIST */
5020 diff = newlen - length;
5021 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5024 /* make new elements SVs now: avoid problems if they're from the array */
5025 for (dst = MARK, i = newlen; i; i--) {
5026 SV * const h = *dst;
5027 *dst++ = newSVsv(h);
5030 if (diff < 0) { /* shrinking the area */
5031 SV **tmparyval = NULL;
5033 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5034 Copy(MARK, tmparyval, newlen, SV*);
5037 MARK = ORIGMARK + 1;
5038 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5039 MEXTEND(MARK, length);
5040 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5042 EXTEND_MORTAL(length);
5043 for (i = length, dst = MARK; i; i--) {
5044 sv_2mortal(*dst); /* free them eventually */
5051 *MARK = AvARRAY(ary)[offset+length-1];
5054 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5055 SvREFCNT_dec(*dst++); /* free them now */
5058 AvFILLp(ary) += diff;
5060 /* pull up or down? */
5062 if (offset < after) { /* easier to pull up */
5063 if (offset) { /* esp. if nothing to pull */
5064 src = &AvARRAY(ary)[offset-1];
5065 dst = src - diff; /* diff is negative */
5066 for (i = offset; i > 0; i--) /* can't trust Copy */
5070 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5074 if (after) { /* anything to pull down? */
5075 src = AvARRAY(ary) + offset + length;
5076 dst = src + diff; /* diff is negative */
5077 Move(src, dst, after, SV*);
5079 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5080 /* avoid later double free */
5084 dst[--i] = &PL_sv_undef;
5087 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5088 Safefree(tmparyval);
5091 else { /* no, expanding (or same) */
5092 SV** tmparyval = NULL;
5094 Newx(tmparyval, length, SV*); /* so remember deletion */
5095 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5098 if (diff > 0) { /* expanding */
5099 /* push up or down? */
5100 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5104 Move(src, dst, offset, SV*);
5106 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5108 AvFILLp(ary) += diff;
5111 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5112 av_extend(ary, AvFILLp(ary) + diff);
5113 AvFILLp(ary) += diff;
5116 dst = AvARRAY(ary) + AvFILLp(ary);
5118 for (i = after; i; i--) {
5126 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5129 MARK = ORIGMARK + 1;
5130 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5132 Copy(tmparyval, MARK, length, SV*);
5134 EXTEND_MORTAL(length);
5135 for (i = length, dst = MARK; i; i--) {
5136 sv_2mortal(*dst); /* free them eventually */
5143 else if (length--) {
5144 *MARK = tmparyval[length];
5147 while (length-- > 0)
5148 SvREFCNT_dec(tmparyval[length]);
5152 *MARK = &PL_sv_undef;
5153 Safefree(tmparyval);
5157 mg_set(MUTABLE_SV(ary));
5165 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5166 register AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5167 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5170 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5173 ENTER_with_name("call_PUSH");
5174 call_method("PUSH",G_SCALAR|G_DISCARD);
5175 LEAVE_with_name("call_PUSH");
5179 PL_delaymagic = DM_DELAY;
5180 for (++MARK; MARK <= SP; MARK++) {
5181 SV * const sv = newSV(0);
5183 sv_setsv(sv, *MARK);
5184 av_store(ary, AvFILLp(ary)+1, sv);
5186 if (PL_delaymagic & DM_ARRAY_ISA)
5187 mg_set(MUTABLE_SV(ary));
5192 if (OP_GIMME(PL_op, 0) != G_VOID) {
5193 PUSHi( AvFILL(ary) + 1 );
5202 AV * const av = PL_op->op_flags & OPf_SPECIAL
5203 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5204 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5208 (void)sv_2mortal(sv);
5215 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5216 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5217 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5220 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5223 ENTER_with_name("call_UNSHIFT");
5224 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5225 LEAVE_with_name("call_UNSHIFT");
5230 av_unshift(ary, SP - MARK);
5232 SV * const sv = newSVsv(*++MARK);
5233 (void)av_store(ary, i++, sv);
5237 if (OP_GIMME(PL_op, 0) != G_VOID) {
5238 PUSHi( AvFILL(ary) + 1 );
5247 if (GIMME == G_ARRAY) {
5248 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5252 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5253 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5254 av = MUTABLE_AV((*SP));
5255 /* In-place reversing only happens in void context for the array
5256 * assignment. We don't need to push anything on the stack. */
5259 if (SvMAGICAL(av)) {
5261 register SV *tmp = sv_newmortal();
5262 /* For SvCANEXISTDELETE */
5265 bool can_preserve = SvCANEXISTDELETE(av);
5267 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5268 register SV *begin, *end;
5271 if (!av_exists(av, i)) {
5272 if (av_exists(av, j)) {
5273 register SV *sv = av_delete(av, j, 0);
5274 begin = *av_fetch(av, i, TRUE);
5275 sv_setsv_mg(begin, sv);
5279 else if (!av_exists(av, j)) {
5280 register SV *sv = av_delete(av, i, 0);
5281 end = *av_fetch(av, j, TRUE);
5282 sv_setsv_mg(end, sv);
5287 begin = *av_fetch(av, i, TRUE);
5288 end = *av_fetch(av, j, TRUE);
5289 sv_setsv(tmp, begin);
5290 sv_setsv_mg(begin, end);
5291 sv_setsv_mg(end, tmp);
5295 SV **begin = AvARRAY(av);
5298 SV **end = begin + AvFILLp(av);
5300 while (begin < end) {
5301 register SV * const tmp = *begin;
5312 register SV * const tmp = *MARK;
5316 /* safe as long as stack cannot get extended in the above */
5322 register char *down;
5327 SvUTF8_off(TARG); /* decontaminate */
5329 do_join(TARG, &PL_sv_no, MARK, SP);
5331 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5332 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5333 report_uninit(TARG);
5336 up = SvPV_force(TARG, len);
5338 if (DO_UTF8(TARG)) { /* first reverse each character */
5339 U8* s = (U8*)SvPVX(TARG);
5340 const U8* send = (U8*)(s + len);
5342 if (UTF8_IS_INVARIANT(*s)) {
5347 if (!utf8_to_uvchr(s, 0))
5351 down = (char*)(s - 1);
5352 /* reverse this character */
5356 *down-- = (char)tmp;
5362 down = SvPVX(TARG) + len - 1;
5366 *down-- = (char)tmp;
5368 (void)SvPOK_only_UTF8(TARG);
5380 register IV limit = POPi; /* note, negative is forever */
5381 SV * const sv = POPs;
5383 register const char *s = SvPV_const(sv, len);
5384 const bool do_utf8 = DO_UTF8(sv);
5385 const char *strend = s + len;
5387 register REGEXP *rx;
5389 register const char *m;
5391 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5392 I32 maxiters = slen + 10;
5393 I32 trailing_empty = 0;
5395 const I32 origlimit = limit;
5398 const I32 gimme = GIMME_V;
5400 const I32 oldsave = PL_savestack_ix;
5401 U32 make_mortal = SVs_TEMP;
5406 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5411 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5414 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5415 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5417 RX_MATCH_UTF8_set(rx, do_utf8);
5420 if (pm->op_pmreplrootu.op_pmtargetoff) {
5421 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5424 if (pm->op_pmreplrootu.op_pmtargetgv) {
5425 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5430 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5436 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5438 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5445 for (i = AvFILLp(ary); i >= 0; i--)
5446 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5448 /* temporarily switch stacks */
5449 SAVESWITCHSTACK(PL_curstack, ary);
5453 base = SP - PL_stack_base;
5455 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5457 while (*s == ' ' || is_utf8_space((U8*)s))
5460 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5461 while (isSPACE_LC(*s))
5469 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5473 gimme_scalar = gimme == G_SCALAR && !ary;
5476 limit = maxiters + 2;
5477 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5480 /* this one uses 'm' and is a negative test */
5482 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5483 const int t = UTF8SKIP(m);
5484 /* is_utf8_space returns FALSE for malform utf8 */
5491 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5492 while (m < strend && !isSPACE_LC(*m))
5495 while (m < strend && !isSPACE(*m))
5508 dstr = newSVpvn_flags(s, m-s,
5509 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5513 /* skip the whitespace found last */
5515 s = m + UTF8SKIP(m);
5519 /* this one uses 's' and is a positive test */
5521 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5524 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5525 while (s < strend && isSPACE_LC(*s))
5528 while (s < strend && isSPACE(*s))
5533 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5535 for (m = s; m < strend && *m != '\n'; m++)
5548 dstr = newSVpvn_flags(s, m-s,
5549 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5555 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5557 Pre-extend the stack, either the number of bytes or
5558 characters in the string or a limited amount, triggered by:
5560 my ($x, $y) = split //, $str;
5564 if (!gimme_scalar) {
5565 const U32 items = limit - 1;
5574 /* keep track of how many bytes we skip over */
5584 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5597 dstr = newSVpvn(s, 1);
5613 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5614 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5615 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5616 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5617 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5618 SV * const csv = CALLREG_INTUIT_STRING(rx);
5620 len = RX_MINLENRET(rx);
5621 if (len == 1 && !RX_UTF8(rx) && !tail) {
5622 const char c = *SvPV_nolen_const(csv);
5624 for (m = s; m < strend && *m != c; m++)
5635 dstr = newSVpvn_flags(s, m-s,
5636 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5639 /* The rx->minlen is in characters but we want to step
5640 * s ahead by bytes. */
5642 s = (char*)utf8_hop((U8*)m, len);
5644 s = m + len; /* Fake \n at the end */
5648 while (s < strend && --limit &&
5649 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5650 csv, multiline ? FBMrf_MULTILINE : 0)) )
5659 dstr = newSVpvn_flags(s, m-s,
5660 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5663 /* The rx->minlen is in characters but we want to step
5664 * s ahead by bytes. */
5666 s = (char*)utf8_hop((U8*)m, len);
5668 s = m + len; /* Fake \n at the end */
5673 maxiters += slen * RX_NPARENS(rx);
5674 while (s < strend && --limit)
5678 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5679 sv, NULL, SvSCREAM(sv) ? REXEC_SCREAM : 0);
5681 if (rex_return == 0)
5683 TAINT_IF(RX_MATCH_TAINTED(rx));
5684 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5687 orig = RX_SUBBEG(rx);
5689 strend = s + (strend - m);
5691 m = RX_OFFS(rx)[0].start + orig;
5700 dstr = newSVpvn_flags(s, m-s,
5701 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5704 if (RX_NPARENS(rx)) {
5706 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5707 s = RX_OFFS(rx)[i].start + orig;
5708 m = RX_OFFS(rx)[i].end + orig;
5710 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5711 parens that didn't match -- they should be set to
5712 undef, not the empty string */
5720 if (m >= orig && s >= orig) {
5721 dstr = newSVpvn_flags(s, m-s,
5722 (do_utf8 ? SVf_UTF8 : 0)
5726 dstr = &PL_sv_undef; /* undef, not "" */
5732 s = RX_OFFS(rx)[0].end + orig;
5736 if (!gimme_scalar) {
5737 iters = (SP - PL_stack_base) - base;
5739 if (iters > maxiters)
5740 DIE(aTHX_ "Split loop");
5742 /* keep field after final delim? */
5743 if (s < strend || (iters && origlimit)) {
5744 if (!gimme_scalar) {
5745 const STRLEN l = strend - s;
5746 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5751 else if (!origlimit) {
5753 iters -= trailing_empty;
5755 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5756 if (TOPs && !make_mortal)
5758 *SP-- = &PL_sv_undef;
5765 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5769 if (SvSMAGICAL(ary)) {
5771 mg_set(MUTABLE_SV(ary));
5774 if (gimme == G_ARRAY) {
5776 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5783 ENTER_with_name("call_PUSH");
5784 call_method("PUSH",G_SCALAR|G_DISCARD);
5785 LEAVE_with_name("call_PUSH");
5787 if (gimme == G_ARRAY) {
5789 /* EXTEND should not be needed - we just popped them */
5791 for (i=0; i < iters; i++) {
5792 SV **svp = av_fetch(ary, i, FALSE);
5793 PUSHs((svp) ? *svp : &PL_sv_undef);
5800 if (gimme == G_ARRAY)
5812 SV *const sv = PAD_SVl(PL_op->op_targ);
5814 if (SvPADSTALE(sv)) {
5817 RETURNOP(cLOGOP->op_other);
5819 RETURNOP(cLOGOP->op_next);
5829 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5830 || SvTYPE(retsv) == SVt_PVCV) {
5831 retsv = refto(retsv);
5838 PP(unimplemented_op)
5841 const Optype op_type = PL_op->op_type;
5842 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5843 with out of range op numbers - it only "special" cases op_custom.
5844 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5845 if we get here for a custom op then that means that the custom op didn't
5846 have an implementation. Given that OP_NAME() looks up the custom op
5847 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5848 registers &PL_unimplemented_op as the address of their custom op.
5849 NULL doesn't generate a useful error message. "custom" does. */
5850 const char *const name = op_type >= OP_max
5851 ? "[out of range]" : PL_op_name[PL_op->op_type];
5852 if(OP_IS_SOCKET(op_type))
5853 DIE(aTHX_ PL_no_sock_func, name);
5854 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5861 HV * const hv = (HV*)POPs;
5863 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
5865 if (SvRMAGICAL(hv)) {
5866 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5868 XPUSHs(magic_scalarpack(hv, mg));
5873 XPUSHs(boolSV(HvUSEDKEYS(hv) != 0));
5877 /* For sorting out arguments passed to a &CORE:: subroutine */
5881 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5882 int defgv = PL_opargs[opnum] & OA_DEFGV, whicharg = 0;
5883 AV * const at_ = GvAV(PL_defgv);
5884 SV **svp = AvARRAY(at_);
5885 I32 minargs = 0, maxargs = 0, numargs = AvFILLp(at_)+1;
5886 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5887 bool seen_question = 0;
5888 const char *err = NULL;
5889 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5891 /* Count how many args there are first, to get some idea how far to
5892 extend the stack. */
5894 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5896 if (oa & OA_OPTIONAL) seen_question = 1;
5897 if (!seen_question) minargs++;
5901 if(numargs < minargs) err = "Not enough";
5902 else if(numargs > maxargs) err = "Too many";
5904 /* diag_listed_as: Too many arguments for %s */
5906 "%s arguments for %s", err,
5907 opnum ? OP_DESC(PL_op->op_next) : SvPV_nolen_const(cSVOP_sv)
5910 /* Reset the stack pointer. Without this, we end up returning our own
5911 arguments in list context, in addition to the values we are supposed
5912 to return. nextstate usually does this on sub entry, but we need
5913 to run the next op with the caller's hints, so we cannot have a
5915 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5917 if(!maxargs) RETURN;
5919 /* We do this here, rather than with a separate pushmark op, as it has
5920 to come in between two things this function does (stack reset and
5921 arg pushing). This seems the easiest way to do it. */
5924 (void)Perl_pp_pushmark(aTHX);
5927 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5928 PUTBACK; /* The code below can die in various places. */
5930 oa = PL_opargs[opnum] >> OASHIFT;
5931 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5935 if (!numargs && defgv && whicharg == minargs + 1) {
5936 PERL_SI * const oldsi = PL_curstackinfo;
5937 I32 const oldcxix = oldsi->si_cxix;
5939 if (oldcxix) oldsi->si_cxix--;
5940 else PL_curstackinfo = oldsi->si_prev;
5941 caller = find_runcv(NULL);
5942 PL_curstackinfo = oldsi;
5943 oldsi->si_cxix = oldcxix;
5944 PUSHs(find_rundefsv2(
5945 caller,cxstack[cxstack_ix].blk_oldcop->cop_seq
5948 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5952 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5957 if (!svp || !*svp || !SvROK(*svp)
5958 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5960 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5961 "Type of arg %d to &CORE::%s must be hash reference",
5962 whicharg, OP_DESC(PL_op->op_next)
5967 if (!numargs) PUSHs(NULL);
5968 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5969 /* no magic here, as the prototype will have added an extra
5970 refgen and we just want what was there before that */
5973 const bool constr = PL_op->op_private & whicharg;
5975 svp && *svp ? *svp : &PL_sv_undef,
5976 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5983 const bool wantscalar =
5984 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5985 if (!svp || !*svp || !SvROK(*svp)
5986 /* We have to permit globrefs even for the \$ proto, as
5987 *foo is indistinguishable from ${\*foo}, and the proto-
5988 type permits the latter. */
5989 || SvTYPE(SvRV(*svp)) > (
5990 wantscalar ? SVt_PVLV
5991 : opnum == OP_LOCK ? SVt_PVCV
5996 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5997 "Type of arg %d to &CORE::%s must be %s",
5998 whicharg, OP_DESC(PL_op->op_next),
6000 ? "scalar reference"
6002 ? "reference to one of [$@%&*]"
6003 : "reference to one of [$@%*]"
6009 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
6021 if (PL_op->op_private & OPpOFFBYONE) {
6022 PERL_SI * const oldsi = PL_curstackinfo;
6023 I32 const oldcxix = oldsi->si_cxix;
6024 if (oldcxix) oldsi->si_cxix--;
6025 else PL_curstackinfo = oldsi->si_prev;
6026 cv = find_runcv(NULL);
6027 PL_curstackinfo = oldsi;
6028 oldsi->si_cxix = oldcxix;
6030 else cv = find_runcv(NULL);
6031 XPUSHs(CvUNIQUE(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
6038 * c-indentation-style: bsd
6040 * indent-tabs-mode: t
6043 * ex: set ts=8 sts=4 sw=4 noet: