3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
19 /* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
33 /* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
37 #ifdef NEED_GETPID_PROTO
38 extern Pid_t getpid (void);
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
45 #if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
49 /* variations on pp_null */
55 if (GIMME_V == G_SCALAR)
66 assert(SvTYPE(TARG) == SVt_PVAV);
67 if (PL_op->op_private & OPpLVAL_INTRO)
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
71 if (PL_op->op_flags & OPf_REF) {
74 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
75 const I32 flags = is_lvalue_sub();
76 if (flags && !(flags & OPpENTERSUB_INARGS)) {
77 if (GIMME == G_SCALAR)
78 /* diag_listed_as: Can't return %s to lvalue scalar context */
79 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
85 if (gimme == G_ARRAY) {
86 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
88 if (SvMAGICAL(TARG)) {
90 for (i=0; i < (U32)maxarg; i++) {
91 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
92 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
96 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
100 else if (gimme == G_SCALAR) {
101 SV* const sv = sv_newmortal();
102 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
103 sv_setiv(sv, maxarg);
114 assert(SvTYPE(TARG) == SVt_PVHV);
116 if (PL_op->op_private & OPpLVAL_INTRO)
117 if (!(PL_op->op_private & OPpPAD_STATE))
118 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
119 if (PL_op->op_flags & OPf_REF)
121 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
122 const I32 flags = is_lvalue_sub();
123 if (flags && !(flags & OPpENTERSUB_INARGS)) {
124 if (GIMME == G_SCALAR)
125 /* diag_listed_as: Can't return %s to lvalue scalar context */
126 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
131 if (gimme == G_ARRAY) {
132 RETURNOP(Perl_do_kv(aTHX));
134 else if (gimme == G_SCALAR) {
135 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
143 static const char S_no_symref_sv[] =
144 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
146 /* In some cases this function inspects PL_op. If this function is called
147 for new op types, more bool parameters may need to be added in place of
150 When noinit is true, the absence of a gv will cause a retval of undef.
151 This is unrelated to the cv-to-gv assignment case.
155 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
159 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
162 sv = amagic_deref_call(sv, to_gv_amg);
166 if (SvTYPE(sv) == SVt_PVIO) {
167 GV * const gv = MUTABLE_GV(sv_newmortal());
168 gv_init(gv, 0, "__ANONIO__", 10, 0);
169 GvIOp(gv) = MUTABLE_IO(sv);
170 SvREFCNT_inc_void_NN(sv);
173 else if (!isGV_with_GP(sv))
174 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
177 if (!isGV_with_GP(sv)) {
179 /* If this is a 'my' scalar and flag is set then vivify
182 if (vivify_sv && sv != &PL_sv_undef) {
185 Perl_croak_no_modify(aTHX);
186 if (cUNOP->op_targ) {
187 SV * const namesv = PAD_SV(cUNOP->op_targ);
188 gv = MUTABLE_GV(newSV(0));
189 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
192 const char * const name = CopSTASHPV(PL_curcop);
193 gv = newGVgen_flags(name,
194 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
196 prepare_SV_for_RV(sv);
197 SvRV_set(sv, MUTABLE_SV(gv));
202 if (PL_op->op_flags & OPf_REF || strict)
203 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
204 if (ckWARN(WARN_UNINITIALIZED))
210 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
211 sv, GV_ADDMG, SVt_PVGV
221 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
224 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
225 == OPpDONT_INIT_GV) {
226 /* We are the target of a coderef assignment. Return
227 the scalar unchanged, and let pp_sasssign deal with
231 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
233 /* FAKE globs in the symbol table cause weird bugs (#77810) */
237 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
238 SV *newsv = sv_newmortal();
239 sv_setsv_flags(newsv, sv, 0);
251 sv, PL_op->op_private & OPpDEREF,
252 PL_op->op_private & HINT_STRICT_REFS,
253 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
254 || PL_op->op_type == OP_READLINE
256 if (PL_op->op_private & OPpLVAL_INTRO)
257 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
262 /* Helper function for pp_rv2sv and pp_rv2av */
264 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
265 const svtype type, SV ***spp)
270 PERL_ARGS_ASSERT_SOFTREF2XV;
272 if (PL_op->op_private & HINT_STRICT_REFS) {
274 Perl_die(aTHX_ S_no_symref_sv, sv,
275 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
277 Perl_die(aTHX_ PL_no_usym, what);
281 PL_op->op_flags & OPf_REF &&
282 PL_op->op_next->op_type != OP_BOOLKEYS
284 Perl_die(aTHX_ PL_no_usym, what);
285 if (ckWARN(WARN_UNINITIALIZED))
287 if (type != SVt_PV && GIMME_V == G_ARRAY) {
291 **spp = &PL_sv_undef;
294 if ((PL_op->op_flags & OPf_SPECIAL) &&
295 !(PL_op->op_flags & OPf_MOD))
297 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
299 **spp = &PL_sv_undef;
304 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
317 sv = amagic_deref_call(sv, to_sv_amg);
321 switch (SvTYPE(sv)) {
327 DIE(aTHX_ "Not a SCALAR reference");
334 if (!isGV_with_GP(gv)) {
335 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
341 if (PL_op->op_flags & OPf_MOD) {
342 if (PL_op->op_private & OPpLVAL_INTRO) {
343 if (cUNOP->op_first->op_type == OP_NULL)
344 sv = save_scalar(MUTABLE_GV(TOPs));
346 sv = save_scalar(gv);
348 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
350 else if (PL_op->op_private & OPpDEREF)
351 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
360 AV * const av = MUTABLE_AV(TOPs);
361 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
363 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
365 *sv = newSV_type(SVt_PVMG);
366 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
370 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
379 if (PL_op->op_flags & OPf_MOD || LVRET) {
380 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
381 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
383 LvTARG(ret) = SvREFCNT_inc_simple(sv);
384 PUSHs(ret); /* no SvSETMAGIC */
388 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
389 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
390 if (mg && mg->mg_len >= 0) {
408 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
410 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
413 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
414 /* (But not in defined().) */
416 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
418 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
422 cv = MUTABLE_CV(&PL_sv_undef);
423 SETs(MUTABLE_SV(cv));
433 SV *ret = &PL_sv_undef;
435 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
436 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
437 const char * s = SvPVX_const(TOPs);
438 if (strnEQ(s, "CORE::", 6)) {
439 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
440 if (!code || code == -KEY_CORE)
441 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
442 SVfARG(newSVpvn_flags(
443 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
446 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
452 cv = sv_2cv(TOPs, &stash, &gv, 0);
454 ret = newSVpvn_flags(
455 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
465 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
467 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
469 PUSHs(MUTABLE_SV(cv));
483 if (GIMME != G_ARRAY) {
487 *MARK = &PL_sv_undef;
488 *MARK = refto(*MARK);
492 EXTEND_MORTAL(SP - MARK);
494 *MARK = refto(*MARK);
499 S_refto(pTHX_ SV *sv)
504 PERL_ARGS_ASSERT_REFTO;
506 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
509 if (!(sv = LvTARG(sv)))
512 SvREFCNT_inc_void_NN(sv);
514 else if (SvTYPE(sv) == SVt_PVAV) {
515 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
516 av_reify(MUTABLE_AV(sv));
518 SvREFCNT_inc_void_NN(sv);
520 else if (SvPADTMP(sv) && !IS_PADGV(sv))
524 SvREFCNT_inc_void_NN(sv);
527 sv_upgrade(rv, SVt_IV);
536 SV * const sv = POPs;
541 if (!sv || !SvROK(sv))
544 (void)sv_ref(TARG,SvRV(sv),TRUE);
556 stash = CopSTASH(PL_curcop);
558 SV * const ssv = POPs;
562 if (!ssv) goto curstash;
563 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
564 Perl_croak(aTHX_ "Attempt to bless into a reference");
565 ptr = SvPV_const(ssv,len);
567 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
568 "Explicit blessing to '' (assuming package main)");
569 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
572 (void)sv_bless(TOPs, stash);
582 const char * const elem = SvPV_const(sv, len);
583 GV * const gv = MUTABLE_GV(POPs);
588 /* elem will always be NUL terminated. */
589 const char * const second_letter = elem + 1;
592 if (len == 5 && strEQ(second_letter, "RRAY"))
593 tmpRef = MUTABLE_SV(GvAV(gv));
596 if (len == 4 && strEQ(second_letter, "ODE"))
597 tmpRef = MUTABLE_SV(GvCVu(gv));
600 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
601 /* finally deprecated in 5.8.0 */
602 deprecate("*glob{FILEHANDLE}");
603 tmpRef = MUTABLE_SV(GvIOp(gv));
606 if (len == 6 && strEQ(second_letter, "ORMAT"))
607 tmpRef = MUTABLE_SV(GvFORM(gv));
610 if (len == 4 && strEQ(second_letter, "LOB"))
611 tmpRef = MUTABLE_SV(gv);
614 if (len == 4 && strEQ(second_letter, "ASH"))
615 tmpRef = MUTABLE_SV(GvHV(gv));
618 if (*second_letter == 'O' && !elem[2] && len == 2)
619 tmpRef = MUTABLE_SV(GvIOp(gv));
622 if (len == 4 && strEQ(second_letter, "AME"))
623 sv = newSVhek(GvNAME_HEK(gv));
626 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
627 const HV * const stash = GvSTASH(gv);
628 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
629 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
633 if (len == 6 && strEQ(second_letter, "CALAR"))
648 /* Pattern matching */
656 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
657 /* Historically, study was skipped in these cases. */
661 /* Make study a no-op. It's no longer useful and its existence
662 complicates matters elsewhere. */
671 if (PL_op->op_flags & OPf_STACKED)
673 else if (PL_op->op_private & OPpTARGET_MY)
679 if(PL_op->op_type == OP_TRANSR) {
681 const char * const pv = SvPV(sv,len);
682 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
687 TARG = sv_newmortal();
693 /* Lvalue operators. */
696 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
702 PERL_ARGS_ASSERT_DO_CHOMP;
704 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
706 if (SvTYPE(sv) == SVt_PVAV) {
708 AV *const av = MUTABLE_AV(sv);
709 const I32 max = AvFILL(av);
711 for (i = 0; i <= max; i++) {
712 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
713 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
714 do_chomp(retval, sv, chomping);
718 else if (SvTYPE(sv) == SVt_PVHV) {
719 HV* const hv = MUTABLE_HV(sv);
721 (void)hv_iterinit(hv);
722 while ((entry = hv_iternext(hv)))
723 do_chomp(retval, hv_iterval(hv,entry), chomping);
726 else if (SvREADONLY(sv)) {
728 /* SV is copy-on-write */
729 sv_force_normal_flags(sv, 0);
732 Perl_croak_no_modify(aTHX);
737 /* XXX, here sv is utf8-ized as a side-effect!
738 If encoding.pm is used properly, almost string-generating
739 operations, including literal strings, chr(), input data, etc.
740 should have been utf8-ized already, right?
742 sv_recode_to_utf8(sv, PL_encoding);
748 char *temp_buffer = NULL;
757 while (len && s[-1] == '\n') {
764 STRLEN rslen, rs_charlen;
765 const char *rsptr = SvPV_const(PL_rs, rslen);
767 rs_charlen = SvUTF8(PL_rs)
771 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
772 /* Assumption is that rs is shorter than the scalar. */
774 /* RS is utf8, scalar is 8 bit. */
776 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
779 /* Cannot downgrade, therefore cannot possibly match
781 assert (temp_buffer == rsptr);
787 else if (PL_encoding) {
788 /* RS is 8 bit, encoding.pm is used.
789 * Do not recode PL_rs as a side-effect. */
790 svrecode = newSVpvn(rsptr, rslen);
791 sv_recode_to_utf8(svrecode, PL_encoding);
792 rsptr = SvPV_const(svrecode, rslen);
793 rs_charlen = sv_len_utf8(svrecode);
796 /* RS is 8 bit, scalar is utf8. */
797 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
811 if (memNE(s, rsptr, rslen))
813 SvIVX(retval) += rs_charlen;
816 s = SvPV_force_nomg_nolen(sv);
824 SvREFCNT_dec(svrecode);
826 Safefree(temp_buffer);
828 if (len && !SvPOK(sv))
829 s = SvPV_force_nomg(sv, len);
832 char * const send = s + len;
833 char * const start = s;
835 while (s > start && UTF8_IS_CONTINUATION(*s))
837 if (is_utf8_string((U8*)s, send - s)) {
838 sv_setpvn(retval, s, send - s);
840 SvCUR_set(sv, s - start);
846 sv_setpvs(retval, "");
850 sv_setpvn(retval, s, 1);
857 sv_setpvs(retval, "");
865 const bool chomping = PL_op->op_type == OP_SCHOMP;
869 do_chomp(TARG, TOPs, chomping);
876 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
877 const bool chomping = PL_op->op_type == OP_CHOMP;
882 do_chomp(TARG, *++MARK, chomping);
893 if (!PL_op->op_private) {
902 SV_CHECK_THINKFIRST_COW_DROP(sv);
904 switch (SvTYPE(sv)) {
908 av_undef(MUTABLE_AV(sv));
911 hv_undef(MUTABLE_HV(sv));
914 if (cv_const_sv((const CV *)sv))
915 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
916 "Constant subroutine %"SVf" undefined",
917 SVfARG(CvANON((const CV *)sv)
918 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
919 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
923 /* let user-undef'd sub keep its identity */
924 GV* const gv = CvGV((const CV *)sv);
925 cv_undef(MUTABLE_CV(sv));
926 CvGV_set(MUTABLE_CV(sv), gv);
931 SvSetMagicSV(sv, &PL_sv_undef);
934 else if (isGV_with_GP(sv)) {
938 /* undef *Pkg::meth_name ... */
940 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
941 && HvENAME_get(stash);
943 if((stash = GvHV((const GV *)sv))) {
944 if(HvENAME_get(stash))
945 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
949 gp_free(MUTABLE_GV(sv));
951 GvGP_set(sv, gp_ref(gp));
953 GvLINE(sv) = CopLINE(PL_curcop);
954 GvEGV(sv) = MUTABLE_GV(sv);
958 mro_package_moved(NULL, stash, (const GV *)sv, 0);
960 /* undef *Foo::ISA */
961 if( strEQ(GvNAME((const GV *)sv), "ISA")
962 && (stash = GvSTASH((const GV *)sv))
963 && (method_changed || HvENAME(stash)) )
964 mro_isa_changed_in(stash);
965 else if(method_changed)
966 mro_method_changed_in(
967 GvSTASH((const GV *)sv)
974 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
990 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
991 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
992 Perl_croak_no_modify(aTHX);
994 TARG = sv_newmortal();
995 sv_setsv(TARG, TOPs);
996 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
997 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
999 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1000 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1004 else sv_dec_nomg(TOPs);
1006 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1007 if (inc && !SvOK(TARG))
1013 /* Ordinary operators. */
1017 dVAR; dSP; dATARGET; SV *svl, *svr;
1018 #ifdef PERL_PRESERVE_IVUV
1021 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1024 #ifdef PERL_PRESERVE_IVUV
1025 /* For integer to integer power, we do the calculation by hand wherever
1026 we're sure it is safe; otherwise we call pow() and try to convert to
1027 integer afterwards. */
1028 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1036 const IV iv = SvIVX(svr);
1040 goto float_it; /* Can't do negative powers this way. */
1044 baseuok = SvUOK(svl);
1046 baseuv = SvUVX(svl);
1048 const IV iv = SvIVX(svl);
1051 baseuok = TRUE; /* effectively it's a UV now */
1053 baseuv = -iv; /* abs, baseuok == false records sign */
1056 /* now we have integer ** positive integer. */
1059 /* foo & (foo - 1) is zero only for a power of 2. */
1060 if (!(baseuv & (baseuv - 1))) {
1061 /* We are raising power-of-2 to a positive integer.
1062 The logic here will work for any base (even non-integer
1063 bases) but it can be less accurate than
1064 pow (base,power) or exp (power * log (base)) when the
1065 intermediate values start to spill out of the mantissa.
1066 With powers of 2 we know this can't happen.
1067 And powers of 2 are the favourite thing for perl
1068 programmers to notice ** not doing what they mean. */
1070 NV base = baseuok ? baseuv : -(NV)baseuv;
1075 while (power >>= 1) {
1083 SvIV_please_nomg(svr);
1086 register unsigned int highbit = 8 * sizeof(UV);
1087 register unsigned int diff = 8 * sizeof(UV);
1088 while (diff >>= 1) {
1090 if (baseuv >> highbit) {
1094 /* we now have baseuv < 2 ** highbit */
1095 if (power * highbit <= 8 * sizeof(UV)) {
1096 /* result will definitely fit in UV, so use UV math
1097 on same algorithm as above */
1098 register UV result = 1;
1099 register UV base = baseuv;
1100 const bool odd_power = cBOOL(power & 1);
1104 while (power >>= 1) {
1111 if (baseuok || !odd_power)
1112 /* answer is positive */
1114 else if (result <= (UV)IV_MAX)
1115 /* answer negative, fits in IV */
1116 SETi( -(IV)result );
1117 else if (result == (UV)IV_MIN)
1118 /* 2's complement assumption: special case IV_MIN */
1121 /* answer negative, doesn't fit */
1122 SETn( -(NV)result );
1130 NV right = SvNV_nomg(svr);
1131 NV left = SvNV_nomg(svl);
1134 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1136 We are building perl with long double support and are on an AIX OS
1137 afflicted with a powl() function that wrongly returns NaNQ for any
1138 negative base. This was reported to IBM as PMR #23047-379 on
1139 03/06/2006. The problem exists in at least the following versions
1140 of AIX and the libm fileset, and no doubt others as well:
1142 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1143 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1144 AIX 5.2.0 bos.adt.libm 5.2.0.85
1146 So, until IBM fixes powl(), we provide the following workaround to
1147 handle the problem ourselves. Our logic is as follows: for
1148 negative bases (left), we use fmod(right, 2) to check if the
1149 exponent is an odd or even integer:
1151 - if odd, powl(left, right) == -powl(-left, right)
1152 - if even, powl(left, right) == powl(-left, right)
1154 If the exponent is not an integer, the result is rightly NaNQ, so
1155 we just return that (as NV_NAN).
1159 NV mod2 = Perl_fmod( right, 2.0 );
1160 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1161 SETn( -Perl_pow( -left, right) );
1162 } else if (mod2 == 0.0) { /* even integer */
1163 SETn( Perl_pow( -left, right) );
1164 } else { /* fractional power */
1168 SETn( Perl_pow( left, right) );
1171 SETn( Perl_pow( left, right) );
1172 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1174 #ifdef PERL_PRESERVE_IVUV
1176 SvIV_please_nomg(svr);
1184 dVAR; dSP; dATARGET; SV *svl, *svr;
1185 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1188 #ifdef PERL_PRESERVE_IVUV
1189 if (SvIV_please_nomg(svr)) {
1190 /* Unless the left argument is integer in range we are going to have to
1191 use NV maths. Hence only attempt to coerce the right argument if
1192 we know the left is integer. */
1193 /* Left operand is defined, so is it IV? */
1194 if (SvIV_please_nomg(svl)) {
1195 bool auvok = SvUOK(svl);
1196 bool buvok = SvUOK(svr);
1197 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1198 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1207 const IV aiv = SvIVX(svl);
1210 auvok = TRUE; /* effectively it's a UV now */
1212 alow = -aiv; /* abs, auvok == false records sign */
1218 const IV biv = SvIVX(svr);
1221 buvok = TRUE; /* effectively it's a UV now */
1223 blow = -biv; /* abs, buvok == false records sign */
1227 /* If this does sign extension on unsigned it's time for plan B */
1228 ahigh = alow >> (4 * sizeof (UV));
1230 bhigh = blow >> (4 * sizeof (UV));
1232 if (ahigh && bhigh) {
1234 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1235 which is overflow. Drop to NVs below. */
1236 } else if (!ahigh && !bhigh) {
1237 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1238 so the unsigned multiply cannot overflow. */
1239 const UV product = alow * blow;
1240 if (auvok == buvok) {
1241 /* -ve * -ve or +ve * +ve gives a +ve result. */
1245 } else if (product <= (UV)IV_MIN) {
1246 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1247 /* -ve result, which could overflow an IV */
1249 SETi( -(IV)product );
1251 } /* else drop to NVs below. */
1253 /* One operand is large, 1 small */
1256 /* swap the operands */
1258 bhigh = blow; /* bhigh now the temp var for the swap */
1262 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1263 multiplies can't overflow. shift can, add can, -ve can. */
1264 product_middle = ahigh * blow;
1265 if (!(product_middle & topmask)) {
1266 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1268 product_middle <<= (4 * sizeof (UV));
1269 product_low = alow * blow;
1271 /* as for pp_add, UV + something mustn't get smaller.
1272 IIRC ANSI mandates this wrapping *behaviour* for
1273 unsigned whatever the actual representation*/
1274 product_low += product_middle;
1275 if (product_low >= product_middle) {
1276 /* didn't overflow */
1277 if (auvok == buvok) {
1278 /* -ve * -ve or +ve * +ve gives a +ve result. */
1280 SETu( product_low );
1282 } else if (product_low <= (UV)IV_MIN) {
1283 /* 2s complement assumption again */
1284 /* -ve result, which could overflow an IV */
1286 SETi( -(IV)product_low );
1288 } /* else drop to NVs below. */
1290 } /* product_middle too large */
1291 } /* ahigh && bhigh */
1296 NV right = SvNV_nomg(svr);
1297 NV left = SvNV_nomg(svl);
1299 SETn( left * right );
1306 dVAR; dSP; dATARGET; SV *svl, *svr;
1307 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1310 /* Only try to do UV divide first
1311 if ((SLOPPYDIVIDE is true) or
1312 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1314 The assumption is that it is better to use floating point divide
1315 whenever possible, only doing integer divide first if we can't be sure.
1316 If NV_PRESERVES_UV is true then we know at compile time that no UV
1317 can be too large to preserve, so don't need to compile the code to
1318 test the size of UVs. */
1321 # define PERL_TRY_UV_DIVIDE
1322 /* ensure that 20./5. == 4. */
1324 # ifdef PERL_PRESERVE_IVUV
1325 # ifndef NV_PRESERVES_UV
1326 # define PERL_TRY_UV_DIVIDE
1331 #ifdef PERL_TRY_UV_DIVIDE
1332 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1333 bool left_non_neg = SvUOK(svl);
1334 bool right_non_neg = SvUOK(svr);
1338 if (right_non_neg) {
1342 const IV biv = SvIVX(svr);
1345 right_non_neg = TRUE; /* effectively it's a UV now */
1351 /* historically undef()/0 gives a "Use of uninitialized value"
1352 warning before dieing, hence this test goes here.
1353 If it were immediately before the second SvIV_please, then
1354 DIE() would be invoked before left was even inspected, so
1355 no inspection would give no warning. */
1357 DIE(aTHX_ "Illegal division by zero");
1363 const IV aiv = SvIVX(svl);
1366 left_non_neg = TRUE; /* effectively it's a UV now */
1375 /* For sloppy divide we always attempt integer division. */
1377 /* Otherwise we only attempt it if either or both operands
1378 would not be preserved by an NV. If both fit in NVs
1379 we fall through to the NV divide code below. However,
1380 as left >= right to ensure integer result here, we know that
1381 we can skip the test on the right operand - right big
1382 enough not to be preserved can't get here unless left is
1385 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1388 /* Integer division can't overflow, but it can be imprecise. */
1389 const UV result = left / right;
1390 if (result * right == left) {
1391 SP--; /* result is valid */
1392 if (left_non_neg == right_non_neg) {
1393 /* signs identical, result is positive. */
1397 /* 2s complement assumption */
1398 if (result <= (UV)IV_MIN)
1399 SETi( -(IV)result );
1401 /* It's exact but too negative for IV. */
1402 SETn( -(NV)result );
1405 } /* tried integer divide but it was not an integer result */
1406 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1407 } /* one operand wasn't SvIOK */
1408 #endif /* PERL_TRY_UV_DIVIDE */
1410 NV right = SvNV_nomg(svr);
1411 NV left = SvNV_nomg(svl);
1412 (void)POPs;(void)POPs;
1413 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1414 if (! Perl_isnan(right) && right == 0.0)
1418 DIE(aTHX_ "Illegal division by zero");
1419 PUSHn( left / right );
1426 dVAR; dSP; dATARGET;
1427 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1431 bool left_neg = FALSE;
1432 bool right_neg = FALSE;
1433 bool use_double = FALSE;
1434 bool dright_valid = FALSE;
1437 SV * const svr = TOPs;
1438 SV * const svl = TOPm1s;
1439 if (SvIV_please_nomg(svr)) {
1440 right_neg = !SvUOK(svr);
1444 const IV biv = SvIVX(svr);
1447 right_neg = FALSE; /* effectively it's a UV now */
1454 dright = SvNV_nomg(svr);
1455 right_neg = dright < 0;
1458 if (dright < UV_MAX_P1) {
1459 right = U_V(dright);
1460 dright_valid = TRUE; /* In case we need to use double below. */
1466 /* At this point use_double is only true if right is out of range for
1467 a UV. In range NV has been rounded down to nearest UV and
1468 use_double false. */
1469 if (!use_double && SvIV_please_nomg(svl)) {
1470 left_neg = !SvUOK(svl);
1474 const IV aiv = SvIVX(svl);
1477 left_neg = FALSE; /* effectively it's a UV now */
1484 dleft = SvNV_nomg(svl);
1485 left_neg = dleft < 0;
1489 /* This should be exactly the 5.6 behaviour - if left and right are
1490 both in range for UV then use U_V() rather than floor. */
1492 if (dleft < UV_MAX_P1) {
1493 /* right was in range, so is dleft, so use UVs not double.
1497 /* left is out of range for UV, right was in range, so promote
1498 right (back) to double. */
1500 /* The +0.5 is used in 5.6 even though it is not strictly
1501 consistent with the implicit +0 floor in the U_V()
1502 inside the #if 1. */
1503 dleft = Perl_floor(dleft + 0.5);
1506 dright = Perl_floor(dright + 0.5);
1517 DIE(aTHX_ "Illegal modulus zero");
1519 dans = Perl_fmod(dleft, dright);
1520 if ((left_neg != right_neg) && dans)
1521 dans = dright - dans;
1524 sv_setnv(TARG, dans);
1530 DIE(aTHX_ "Illegal modulus zero");
1533 if ((left_neg != right_neg) && ans)
1536 /* XXX may warn: unary minus operator applied to unsigned type */
1537 /* could change -foo to be (~foo)+1 instead */
1538 if (ans <= ~((UV)IV_MAX)+1)
1539 sv_setiv(TARG, ~ans+1);
1541 sv_setnv(TARG, -(NV)ans);
1544 sv_setuv(TARG, ans);
1553 dVAR; dSP; dATARGET;
1557 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1558 /* TODO: think of some way of doing list-repeat overloading ??? */
1563 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1569 const UV uv = SvUV_nomg(sv);
1571 count = IV_MAX; /* The best we can do? */
1575 const IV iv = SvIV_nomg(sv);
1582 else if (SvNOKp(sv)) {
1583 const NV nv = SvNV_nomg(sv);
1590 count = SvIV_nomg(sv);
1592 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1594 static const char oom_list_extend[] = "Out of memory during list extend";
1595 const I32 items = SP - MARK;
1596 const I32 max = items * count;
1598 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1599 /* Did the max computation overflow? */
1600 if (items > 0 && max > 0 && (max < items || max < count))
1601 Perl_croak(aTHX_ oom_list_extend);
1606 /* This code was intended to fix 20010809.028:
1609 for (($x =~ /./g) x 2) {
1610 print chop; # "abcdabcd" expected as output.
1613 * but that change (#11635) broke this code:
1615 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1617 * I can't think of a better fix that doesn't introduce
1618 * an efficiency hit by copying the SVs. The stack isn't
1619 * refcounted, and mortalisation obviously doesn't
1620 * Do The Right Thing when the stack has more than
1621 * one pointer to the same mortal value.
1625 *SP = sv_2mortal(newSVsv(*SP));
1635 repeatcpy((char*)(MARK + items), (char*)MARK,
1636 items * sizeof(const SV *), count - 1);
1639 else if (count <= 0)
1642 else { /* Note: mark already snarfed by pp_list */
1643 SV * const tmpstr = POPs;
1646 static const char oom_string_extend[] =
1647 "Out of memory during string extend";
1650 sv_setsv_nomg(TARG, tmpstr);
1651 SvPV_force_nomg(TARG, len);
1652 isutf = DO_UTF8(TARG);
1657 const STRLEN max = (UV)count * len;
1658 if (len > MEM_SIZE_MAX / count)
1659 Perl_croak(aTHX_ oom_string_extend);
1660 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1661 SvGROW(TARG, max + 1);
1662 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1663 SvCUR_set(TARG, SvCUR(TARG) * count);
1665 *SvEND(TARG) = '\0';
1668 (void)SvPOK_only_UTF8(TARG);
1670 (void)SvPOK_only(TARG);
1672 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1673 /* The parser saw this as a list repeat, and there
1674 are probably several items on the stack. But we're
1675 in scalar context, and there's no pp_list to save us
1676 now. So drop the rest of the items -- robin@kitsite.com
1688 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1689 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1692 useleft = USE_LEFT(svl);
1693 #ifdef PERL_PRESERVE_IVUV
1694 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1695 "bad things" happen if you rely on signed integers wrapping. */
1696 if (SvIV_please_nomg(svr)) {
1697 /* Unless the left argument is integer in range we are going to have to
1698 use NV maths. Hence only attempt to coerce the right argument if
1699 we know the left is integer. */
1700 register UV auv = 0;
1706 a_valid = auvok = 1;
1707 /* left operand is undef, treat as zero. */
1709 /* Left operand is defined, so is it IV? */
1710 if (SvIV_please_nomg(svl)) {
1711 if ((auvok = SvUOK(svl)))
1714 register const IV aiv = SvIVX(svl);
1717 auvok = 1; /* Now acting as a sign flag. */
1718 } else { /* 2s complement assumption for IV_MIN */
1726 bool result_good = 0;
1729 bool buvok = SvUOK(svr);
1734 register const IV biv = SvIVX(svr);
1741 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1742 else "IV" now, independent of how it came in.
1743 if a, b represents positive, A, B negative, a maps to -A etc
1748 all UV maths. negate result if A negative.
1749 subtract if signs same, add if signs differ. */
1751 if (auvok ^ buvok) {
1760 /* Must get smaller */
1765 if (result <= buv) {
1766 /* result really should be -(auv-buv). as its negation
1767 of true value, need to swap our result flag */
1779 if (result <= (UV)IV_MIN)
1780 SETi( -(IV)result );
1782 /* result valid, but out of range for IV. */
1783 SETn( -(NV)result );
1787 } /* Overflow, drop through to NVs. */
1792 NV value = SvNV_nomg(svr);
1796 /* left operand is undef, treat as zero - value */
1800 SETn( SvNV_nomg(svl) - value );
1807 dVAR; dSP; dATARGET; SV *svl, *svr;
1808 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1812 const IV shift = SvIV_nomg(svr);
1813 if (PL_op->op_private & HINT_INTEGER) {
1814 const IV i = SvIV_nomg(svl);
1818 const UV u = SvUV_nomg(svl);
1827 dVAR; dSP; dATARGET; SV *svl, *svr;
1828 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1832 const IV shift = SvIV_nomg(svr);
1833 if (PL_op->op_private & HINT_INTEGER) {
1834 const IV i = SvIV_nomg(svl);
1838 const UV u = SvUV_nomg(svl);
1850 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1854 (SvIOK_notUV(left) && SvIOK_notUV(right))
1855 ? (SvIVX(left) < SvIVX(right))
1856 : (do_ncmp(left, right) == -1)
1866 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1870 (SvIOK_notUV(left) && SvIOK_notUV(right))
1871 ? (SvIVX(left) > SvIVX(right))
1872 : (do_ncmp(left, right) == 1)
1882 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1886 (SvIOK_notUV(left) && SvIOK_notUV(right))
1887 ? (SvIVX(left) <= SvIVX(right))
1888 : (do_ncmp(left, right) <= 0)
1898 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1902 (SvIOK_notUV(left) && SvIOK_notUV(right))
1903 ? (SvIVX(left) >= SvIVX(right))
1904 : ( (do_ncmp(left, right) & 2) == 0)
1914 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1918 (SvIOK_notUV(left) && SvIOK_notUV(right))
1919 ? (SvIVX(left) != SvIVX(right))
1920 : (do_ncmp(left, right) != 0)
1925 /* compare left and right SVs. Returns:
1929 * 2: left or right was a NaN
1932 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1936 PERL_ARGS_ASSERT_DO_NCMP;
1937 #ifdef PERL_PRESERVE_IVUV
1938 /* Fortunately it seems NaN isn't IOK */
1939 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1941 const IV leftiv = SvIVX(left);
1942 if (!SvUOK(right)) {
1943 /* ## IV <=> IV ## */
1944 const IV rightiv = SvIVX(right);
1945 return (leftiv > rightiv) - (leftiv < rightiv);
1947 /* ## IV <=> UV ## */
1949 /* As (b) is a UV, it's >=0, so it must be < */
1952 const UV rightuv = SvUVX(right);
1953 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
1958 /* ## UV <=> UV ## */
1959 const UV leftuv = SvUVX(left);
1960 const UV rightuv = SvUVX(right);
1961 return (leftuv > rightuv) - (leftuv < rightuv);
1963 /* ## UV <=> IV ## */
1965 const IV rightiv = SvIVX(right);
1967 /* As (a) is a UV, it's >=0, so it cannot be < */
1970 const UV leftuv = SvUVX(left);
1971 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
1974 assert(0); /* NOTREACHED */
1978 NV const rnv = SvNV_nomg(right);
1979 NV const lnv = SvNV_nomg(left);
1981 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1982 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
1985 return (lnv > rnv) - (lnv < rnv);
2004 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2007 value = do_ncmp(left, right);
2022 int amg_type = sle_amg;
2026 switch (PL_op->op_type) {
2045 tryAMAGICbin_MG(amg_type, AMGf_set);
2048 const int cmp = (IN_LOCALE_RUNTIME
2049 ? sv_cmp_locale_flags(left, right, 0)
2050 : sv_cmp_flags(left, right, 0));
2051 SETs(boolSV(cmp * multiplier < rhs));
2059 tryAMAGICbin_MG(seq_amg, AMGf_set);
2062 SETs(boolSV(sv_eq_flags(left, right, 0)));
2070 tryAMAGICbin_MG(sne_amg, AMGf_set);
2073 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2081 tryAMAGICbin_MG(scmp_amg, 0);
2084 const int cmp = (IN_LOCALE_RUNTIME
2085 ? sv_cmp_locale_flags(left, right, 0)
2086 : sv_cmp_flags(left, right, 0));
2094 dVAR; dSP; dATARGET;
2095 tryAMAGICbin_MG(band_amg, AMGf_assign);
2098 if (SvNIOKp(left) || SvNIOKp(right)) {
2099 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2100 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2101 if (PL_op->op_private & HINT_INTEGER) {
2102 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2106 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2109 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2110 if (right_ro_nonnum) SvNIOK_off(right);
2113 do_vop(PL_op->op_type, TARG, left, right);
2122 dVAR; dSP; dATARGET;
2123 const int op_type = PL_op->op_type;
2125 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2128 if (SvNIOKp(left) || SvNIOKp(right)) {
2129 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2130 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2131 if (PL_op->op_private & HINT_INTEGER) {
2132 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2133 const IV r = SvIV_nomg(right);
2134 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2138 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2139 const UV r = SvUV_nomg(right);
2140 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2143 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2144 if (right_ro_nonnum) SvNIOK_off(right);
2147 do_vop(op_type, TARG, left, right);
2154 PERL_STATIC_INLINE bool
2155 S_negate_string(pTHX)
2160 SV * const sv = TOPs;
2161 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2163 s = SvPV_nomg_const(sv, len);
2164 if (isIDFIRST(*s)) {
2165 sv_setpvs(TARG, "-");
2168 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2169 sv_setsv_nomg(TARG, sv);
2170 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2180 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2181 if (S_negate_string(aTHX)) return NORMAL;
2183 SV * const sv = TOPs;
2186 /* It's publicly an integer */
2189 if (SvIVX(sv) == IV_MIN) {
2190 /* 2s complement assumption. */
2191 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2194 else if (SvUVX(sv) <= IV_MAX) {
2199 else if (SvIVX(sv) != IV_MIN) {
2203 #ifdef PERL_PRESERVE_IVUV
2210 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2211 SETn(-SvNV_nomg(sv));
2212 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2213 goto oops_its_an_int;
2215 SETn(-SvNV_nomg(sv));
2223 tryAMAGICun_MG(not_amg, AMGf_set);
2224 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2231 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2235 if (PL_op->op_private & HINT_INTEGER) {
2236 const IV i = ~SvIV_nomg(sv);
2240 const UV u = ~SvUV_nomg(sv);
2249 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2250 sv_setsv_nomg(TARG, sv);
2251 tmps = (U8*)SvPV_force_nomg(TARG, len);
2254 /* Calculate exact length, let's not estimate. */
2259 U8 * const send = tmps + len;
2260 U8 * const origtmps = tmps;
2261 const UV utf8flags = UTF8_ALLOW_ANYUV;
2263 while (tmps < send) {
2264 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2266 targlen += UNISKIP(~c);
2272 /* Now rewind strings and write them. */
2279 Newx(result, targlen + 1, U8);
2281 while (tmps < send) {
2282 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2284 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2287 sv_usepvn_flags(TARG, (char*)result, targlen,
2288 SV_HAS_TRAILING_NUL);
2295 Newx(result, nchar + 1, U8);
2297 while (tmps < send) {
2298 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2303 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2311 register long *tmpl;
2312 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2315 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2320 for ( ; anum > 0; anum--, tmps++)
2328 /* integer versions of some of the above */
2332 dVAR; dSP; dATARGET;
2333 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2336 SETi( left * right );
2344 dVAR; dSP; dATARGET;
2345 tryAMAGICbin_MG(div_amg, AMGf_assign);
2348 IV value = SvIV_nomg(right);
2350 DIE(aTHX_ "Illegal division by zero");
2351 num = SvIV_nomg(left);
2353 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2357 value = num / value;
2363 #if defined(__GLIBC__) && IVSIZE == 8
2370 /* This is the vanilla old i_modulo. */
2371 dVAR; dSP; dATARGET;
2372 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2376 DIE(aTHX_ "Illegal modulus zero");
2377 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2381 SETi( left % right );
2386 #if defined(__GLIBC__) && IVSIZE == 8
2391 /* This is the i_modulo with the workaround for the _moddi3 bug
2392 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2393 * See below for pp_i_modulo. */
2394 dVAR; dSP; dATARGET;
2395 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2399 DIE(aTHX_ "Illegal modulus zero");
2400 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2404 SETi( left % PERL_ABS(right) );
2411 dVAR; dSP; dATARGET;
2412 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2416 DIE(aTHX_ "Illegal modulus zero");
2417 /* The assumption is to use hereafter the old vanilla version... */
2419 PL_ppaddr[OP_I_MODULO] =
2421 /* .. but if we have glibc, we might have a buggy _moddi3
2422 * (at least glicb 2.2.5 is known to have this bug), in other
2423 * words our integer modulus with negative quad as the second
2424 * argument might be broken. Test for this and re-patch the
2425 * opcode dispatch table if that is the case, remembering to
2426 * also apply the workaround so that this first round works
2427 * right, too. See [perl #9402] for more information. */
2431 /* Cannot do this check with inlined IV constants since
2432 * that seems to work correctly even with the buggy glibc. */
2434 /* Yikes, we have the bug.
2435 * Patch in the workaround version. */
2437 PL_ppaddr[OP_I_MODULO] =
2438 &Perl_pp_i_modulo_1;
2439 /* Make certain we work right this time, too. */
2440 right = PERL_ABS(right);
2443 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2447 SETi( left % right );
2455 dVAR; dSP; dATARGET;
2456 tryAMAGICbin_MG(add_amg, AMGf_assign);
2458 dPOPTOPiirl_ul_nomg;
2459 SETi( left + right );
2466 dVAR; dSP; dATARGET;
2467 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2469 dPOPTOPiirl_ul_nomg;
2470 SETi( left - right );
2478 tryAMAGICbin_MG(lt_amg, AMGf_set);
2481 SETs(boolSV(left < right));
2489 tryAMAGICbin_MG(gt_amg, AMGf_set);
2492 SETs(boolSV(left > right));
2500 tryAMAGICbin_MG(le_amg, AMGf_set);
2503 SETs(boolSV(left <= right));
2511 tryAMAGICbin_MG(ge_amg, AMGf_set);
2514 SETs(boolSV(left >= right));
2522 tryAMAGICbin_MG(eq_amg, AMGf_set);
2525 SETs(boolSV(left == right));
2533 tryAMAGICbin_MG(ne_amg, AMGf_set);
2536 SETs(boolSV(left != right));
2544 tryAMAGICbin_MG(ncmp_amg, 0);
2551 else if (left < right)
2563 tryAMAGICun_MG(neg_amg, 0);
2564 if (S_negate_string(aTHX)) return NORMAL;
2566 SV * const sv = TOPs;
2567 IV const i = SvIV_nomg(sv);
2573 /* High falutin' math. */
2578 tryAMAGICbin_MG(atan2_amg, 0);
2581 SETn(Perl_atan2(left, right));
2589 int amg_type = sin_amg;
2590 const char *neg_report = NULL;
2591 NV (*func)(NV) = Perl_sin;
2592 const int op_type = PL_op->op_type;
2609 amg_type = sqrt_amg;
2611 neg_report = "sqrt";
2616 tryAMAGICun_MG(amg_type, 0);
2618 SV * const arg = POPs;
2619 const NV value = SvNV_nomg(arg);
2621 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2622 SET_NUMERIC_STANDARD();
2623 /* diag_listed_as: Can't take log of %g */
2624 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2627 XPUSHn(func(value));
2632 /* Support Configure command-line overrides for rand() functions.
2633 After 5.005, perhaps we should replace this by Configure support
2634 for drand48(), random(), or rand(). For 5.005, though, maintain
2635 compatibility by calling rand() but allow the user to override it.
2636 See INSTALL for details. --Andy Dougherty 15 July 1998
2638 /* Now it's after 5.005, and Configure supports drand48() and random(),
2639 in addition to rand(). So the overrides should not be needed any more.
2640 --Jarkko Hietaniemi 27 September 1998
2643 #ifndef HAS_DRAND48_PROTO
2644 extern double drand48 (void);
2654 value = 1.0; (void)POPs;
2660 if (!PL_srand_called) {
2661 (void)seedDrand01((Rand_seed_t)seed());
2662 PL_srand_called = TRUE;
2674 if (MAXARG >= 1 && (TOPs || POPs)) {
2681 pv = SvPV(top, len);
2682 flags = grok_number(pv, len, &anum);
2684 if (!(flags & IS_NUMBER_IN_UV)) {
2685 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2686 "Integer overflow in srand");
2694 (void)seedDrand01((Rand_seed_t)anum);
2695 PL_srand_called = TRUE;
2699 /* Historically srand always returned true. We can avoid breaking
2701 sv_setpvs(TARG, "0 but true");
2710 tryAMAGICun_MG(int_amg, AMGf_numeric);
2712 SV * const sv = TOPs;
2713 const IV iv = SvIV_nomg(sv);
2714 /* XXX it's arguable that compiler casting to IV might be subtly
2715 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2716 else preferring IV has introduced a subtle behaviour change bug. OTOH
2717 relying on floating point to be accurate is a bug. */
2722 else if (SvIOK(sv)) {
2724 SETu(SvUV_nomg(sv));
2729 const NV value = SvNV_nomg(sv);
2731 if (value < (NV)UV_MAX + 0.5) {
2734 SETn(Perl_floor(value));
2738 if (value > (NV)IV_MIN - 0.5) {
2741 SETn(Perl_ceil(value));
2752 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2754 SV * const sv = TOPs;
2755 /* This will cache the NV value if string isn't actually integer */
2756 const IV iv = SvIV_nomg(sv);
2761 else if (SvIOK(sv)) {
2762 /* IVX is precise */
2764 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2772 /* 2s complement assumption. Also, not really needed as
2773 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2779 const NV value = SvNV_nomg(sv);
2793 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2797 SV* const sv = POPs;
2799 tmps = (SvPV_const(sv, len));
2801 /* If Unicode, try to downgrade
2802 * If not possible, croak. */
2803 SV* const tsv = sv_2mortal(newSVsv(sv));
2806 sv_utf8_downgrade(tsv, FALSE);
2807 tmps = SvPV_const(tsv, len);
2809 if (PL_op->op_type == OP_HEX)
2812 while (*tmps && len && isSPACE(*tmps))
2816 if (*tmps == 'x' || *tmps == 'X') {
2818 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2820 else if (*tmps == 'b' || *tmps == 'B')
2821 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2823 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2825 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2839 SV * const sv = TOPs;
2841 if (SvGAMAGIC(sv)) {
2842 /* For an overloaded or magic scalar, we can't know in advance if
2843 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2844 it likes to cache the length. Maybe that should be a documented
2849 = sv_2pv_flags(sv, &len,
2850 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2853 if (!SvPADTMP(TARG)) {
2854 sv_setsv(TARG, &PL_sv_undef);
2859 else if (DO_UTF8(sv)) {
2860 SETi(utf8_length((U8*)p, (U8*)p + len));
2864 } else if (SvOK(sv)) {
2865 /* Neither magic nor overloaded. */
2867 SETi(sv_len_utf8(sv));
2871 if (!SvPADTMP(TARG)) {
2872 sv_setsv_nomg(TARG, &PL_sv_undef);
2880 /* Returns false if substring is completely outside original string.
2881 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2882 always be true for an explicit 0.
2885 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2886 bool pos1_is_uv, IV len_iv,
2887 bool len_is_uv, STRLEN *posp,
2893 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2895 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2896 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2899 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2902 if (len_iv || len_is_uv) {
2903 if (!len_is_uv && len_iv < 0) {
2904 pos2_iv = curlen + len_iv;
2906 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2909 } else { /* len_iv >= 0 */
2910 if (!pos1_is_uv && pos1_iv < 0) {
2911 pos2_iv = pos1_iv + len_iv;
2912 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2914 if ((UV)len_iv > curlen-(UV)pos1_iv)
2917 pos2_iv = pos1_iv+len_iv;
2927 if (!pos2_is_uv && pos2_iv < 0) {
2928 if (!pos1_is_uv && pos1_iv < 0)
2932 else if (!pos1_is_uv && pos1_iv < 0)
2935 if ((UV)pos2_iv < (UV)pos1_iv)
2937 if ((UV)pos2_iv > curlen)
2940 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2941 *posp = (STRLEN)( (UV)pos1_iv );
2942 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2959 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2960 const bool rvalue = (GIMME_V != G_VOID);
2963 const char *repl = NULL;
2965 int num_args = PL_op->op_private & 7;
2966 bool repl_need_utf8_upgrade = FALSE;
2967 bool repl_is_utf8 = FALSE;
2971 if(!(repl_sv = POPs)) num_args--;
2973 if ((len_sv = POPs)) {
2974 len_iv = SvIV(len_sv);
2975 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
2980 pos1_iv = SvIV(pos_sv);
2981 pos1_is_uv = SvIOK_UV(pos_sv);
2983 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
2989 repl = SvPV_const(repl_sv, repl_len);
2990 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
2993 sv_utf8_upgrade(sv);
2995 else if (DO_UTF8(sv))
2996 repl_need_utf8_upgrade = TRUE;
3000 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3001 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3003 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3005 pos1_is_uv || pos1_iv >= 0
3006 ? (STRLEN)(UV)pos1_iv
3007 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3009 len_is_uv || len_iv > 0
3010 ? (STRLEN)(UV)len_iv
3011 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3014 PUSHs(ret); /* avoid SvSETMAGIC here */
3017 tmps = SvPV_const(sv, curlen);
3019 utf8_curlen = sv_len_utf8(sv);
3020 if (utf8_curlen == curlen)
3023 curlen = utf8_curlen;
3029 STRLEN pos, len, byte_len, byte_pos;
3031 if (!translate_substr_offsets(
3032 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3036 byte_pos = utf8_curlen
3037 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3042 SvTAINTED_off(TARG); /* decontaminate */
3043 SvUTF8_off(TARG); /* decontaminate */
3044 sv_setpvn(TARG, tmps, byte_len);
3045 #ifdef USE_LOCALE_COLLATE
3046 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3053 SV* repl_sv_copy = NULL;
3055 if (repl_need_utf8_upgrade) {
3056 repl_sv_copy = newSVsv(repl_sv);
3057 sv_utf8_upgrade(repl_sv_copy);
3058 repl = SvPV_const(repl_sv_copy, repl_len);
3059 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3062 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3063 "Attempt to use reference as lvalue in substr"
3067 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3070 SvREFCNT_dec(repl_sv_copy);
3082 Perl_croak(aTHX_ "substr outside of string");
3083 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3090 register const IV size = POPi;
3091 register const IV offset = POPi;
3092 register SV * const src = POPs;
3093 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3096 if (lvalue) { /* it's an lvalue! */
3097 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3098 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3100 LvTARG(ret) = SvREFCNT_inc_simple(src);
3101 LvTARGOFF(ret) = offset;
3102 LvTARGLEN(ret) = size;
3106 SvTAINTED_off(TARG); /* decontaminate */
3110 sv_setuv(ret, do_vecget(src, offset, size));
3126 const char *little_p;
3129 const bool is_index = PL_op->op_type == OP_INDEX;
3130 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3136 big_p = SvPV_const(big, biglen);
3137 little_p = SvPV_const(little, llen);
3139 big_utf8 = DO_UTF8(big);
3140 little_utf8 = DO_UTF8(little);
3141 if (big_utf8 ^ little_utf8) {
3142 /* One needs to be upgraded. */
3143 if (little_utf8 && !PL_encoding) {
3144 /* Well, maybe instead we might be able to downgrade the small
3146 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3149 /* If the large string is ISO-8859-1, and it's not possible to
3150 convert the small string to ISO-8859-1, then there is no
3151 way that it could be found anywhere by index. */
3156 /* At this point, pv is a malloc()ed string. So donate it to temp
3157 to ensure it will get free()d */
3158 little = temp = newSV(0);
3159 sv_usepvn(temp, pv, llen);
3160 little_p = SvPVX(little);
3163 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3166 sv_recode_to_utf8(temp, PL_encoding);
3168 sv_utf8_upgrade(temp);
3173 big_p = SvPV_const(big, biglen);
3176 little_p = SvPV_const(little, llen);
3180 if (SvGAMAGIC(big)) {
3181 /* Life just becomes a lot easier if I use a temporary here.
3182 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3183 will trigger magic and overloading again, as will fbm_instr()
3185 big = newSVpvn_flags(big_p, biglen,
3186 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3189 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3190 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3191 warn on undef, and we've already triggered a warning with the
3192 SvPV_const some lines above. We can't remove that, as we need to
3193 call some SvPV to trigger overloading early and find out if the
3195 This is all getting to messy. The API isn't quite clean enough,
3196 because data access has side effects.
3198 little = newSVpvn_flags(little_p, llen,
3199 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3200 little_p = SvPVX(little);
3204 offset = is_index ? 0 : biglen;
3206 if (big_utf8 && offset > 0)
3207 sv_pos_u2b(big, &offset, 0);
3213 else if (offset > (I32)biglen)
3215 if (!(little_p = is_index
3216 ? fbm_instr((unsigned char*)big_p + offset,
3217 (unsigned char*)big_p + biglen, little, 0)
3218 : rninstr(big_p, big_p + offset,
3219 little_p, little_p + llen)))
3222 retval = little_p - big_p;
3223 if (retval > 0 && big_utf8)
3224 sv_pos_b2u(big, &retval);
3234 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3235 SvTAINTED_off(TARG);
3236 do_sprintf(TARG, SP-MARK, MARK+1);
3237 TAINT_IF(SvTAINTED(TARG));
3249 const U8 *s = (U8*)SvPV_const(argsv, len);
3251 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3252 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3253 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3257 XPUSHu(DO_UTF8(argsv) ?
3258 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3272 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3273 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3275 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3276 && SvNV_nomg(top) < 0.0))) {
3277 if (ckWARN(WARN_UTF8)) {
3278 if (SvGMAGICAL(top)) {
3279 SV *top2 = sv_newmortal();
3280 sv_setsv_nomg(top2, top);
3283 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3284 "Invalid negative number (%"SVf") in chr", top);
3286 value = UNICODE_REPLACEMENT;
3288 value = SvUV_nomg(top);
3291 SvUPGRADE(TARG,SVt_PV);
3293 if (value > 255 && !IN_BYTES) {
3294 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3295 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3296 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3298 (void)SvPOK_only(TARG);
3307 *tmps++ = (char)value;
3309 (void)SvPOK_only(TARG);
3311 if (PL_encoding && !IN_BYTES) {
3312 sv_recode_to_utf8(TARG, PL_encoding);
3314 if (SvCUR(TARG) == 0
3315 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3316 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3321 *tmps++ = (char)value;
3337 const char *tmps = SvPV_const(left, len);
3339 if (DO_UTF8(left)) {
3340 /* If Unicode, try to downgrade.
3341 * If not possible, croak.
3342 * Yes, we made this up. */
3343 SV* const tsv = sv_2mortal(newSVsv(left));
3346 sv_utf8_downgrade(tsv, FALSE);
3347 tmps = SvPV_const(tsv, len);
3349 # ifdef USE_ITHREADS
3351 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3352 /* This should be threadsafe because in ithreads there is only
3353 * one thread per interpreter. If this would not be true,
3354 * we would need a mutex to protect this malloc. */
3355 PL_reentrant_buffer->_crypt_struct_buffer =
3356 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3357 #if defined(__GLIBC__) || defined(__EMX__)
3358 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3359 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3360 /* work around glibc-2.2.5 bug */
3361 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3365 # endif /* HAS_CRYPT_R */
3366 # endif /* USE_ITHREADS */
3368 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3370 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3376 "The crypt() function is unimplemented due to excessive paranoia.");
3380 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3381 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3383 /* Generates code to store a unicode codepoint c that is known to occupy
3384 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3385 * and p is advanced to point to the next available byte after the two bytes */
3386 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3388 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3389 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3394 /* Actually is both lcfirst() and ucfirst(). Only the first character
3395 * changes. This means that possibly we can change in-place, ie., just
3396 * take the source and change that one character and store it back, but not
3397 * if read-only etc, or if the length changes */
3402 STRLEN slen; /* slen is the byte length of the whole SV. */
3405 bool inplace; /* ? Convert first char only, in-place */
3406 bool doing_utf8 = FALSE; /* ? using utf8 */
3407 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3408 const int op_type = PL_op->op_type;
3411 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3412 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3413 * stored as UTF-8 at s. */
3414 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3415 * lowercased) character stored in tmpbuf. May be either
3416 * UTF-8 or not, but in either case is the number of bytes */
3417 bool tainted = FALSE;
3421 s = (const U8*)SvPV_nomg_const(source, slen);
3423 if (ckWARN(WARN_UNINITIALIZED))
3424 report_uninit(source);
3429 /* We may be able to get away with changing only the first character, in
3430 * place, but not if read-only, etc. Later we may discover more reasons to
3431 * not convert in-place. */
3432 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3434 /* First calculate what the changed first character should be. This affects
3435 * whether we can just swap it out, leaving the rest of the string unchanged,
3436 * or even if have to convert the dest to UTF-8 when the source isn't */
3438 if (! slen) { /* If empty */
3439 need = 1; /* still need a trailing NUL */
3442 else if (DO_UTF8(source)) { /* Is the source utf8? */
3445 if (op_type == OP_UCFIRST) {
3446 _to_utf8_title_flags(s, tmpbuf, &tculen,
3447 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3450 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3451 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3454 /* we can't do in-place if the length changes. */
3455 if (ulen != tculen) inplace = FALSE;
3456 need = slen + 1 - ulen + tculen;
3458 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3459 * latin1 is treated as caseless. Note that a locale takes
3461 ulen = 1; /* Original character is 1 byte */
3462 tculen = 1; /* Most characters will require one byte, but this will
3463 * need to be overridden for the tricky ones */
3466 if (op_type == OP_LCFIRST) {
3468 /* lower case the first letter: no trickiness for any character */
3469 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3470 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3473 else if (IN_LOCALE_RUNTIME) {
3474 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3475 * have upper and title case different
3478 else if (! IN_UNI_8_BIT) {
3479 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3480 * on EBCDIC machines whatever the
3481 * native function does */
3483 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3484 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3486 assert(tculen == 2);
3488 /* If the result is an upper Latin1-range character, it can
3489 * still be represented in one byte, which is its ordinal */
3490 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3491 *tmpbuf = (U8) title_ord;
3495 /* Otherwise it became more than one ASCII character (in
3496 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3497 * beyond Latin1, so the number of bytes changed, so can't
3498 * replace just the first character in place. */
3501 /* If the result won't fit in a byte, the entire result will
3502 * have to be in UTF-8. Assume worst case sizing in
3503 * conversion. (all latin1 characters occupy at most two bytes
3505 if (title_ord > 255) {
3507 convert_source_to_utf8 = TRUE;
3508 need = slen * 2 + 1;
3510 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3511 * (both) characters whose title case is above 255 is
3515 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3516 need = slen + 1 + 1;
3520 } /* End of use Unicode (Latin1) semantics */
3521 } /* End of changing the case of the first character */
3523 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3524 * generate the result */
3527 /* We can convert in place. This means we change just the first
3528 * character without disturbing the rest; no need to grow */
3530 s = d = (U8*)SvPV_force_nomg(source, slen);
3536 /* Here, we can't convert in place; we earlier calculated how much
3537 * space we will need, so grow to accommodate that */
3538 SvUPGRADE(dest, SVt_PV);
3539 d = (U8*)SvGROW(dest, need);
3540 (void)SvPOK_only(dest);
3547 if (! convert_source_to_utf8) {
3549 /* Here both source and dest are in UTF-8, but have to create
3550 * the entire output. We initialize the result to be the
3551 * title/lower cased first character, and then append the rest
3553 sv_setpvn(dest, (char*)tmpbuf, tculen);
3555 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3559 const U8 *const send = s + slen;
3561 /* Here the dest needs to be in UTF-8, but the source isn't,
3562 * except we earlier UTF-8'd the first character of the source
3563 * into tmpbuf. First put that into dest, and then append the
3564 * rest of the source, converting it to UTF-8 as we go. */
3566 /* Assert tculen is 2 here because the only two characters that
3567 * get to this part of the code have 2-byte UTF-8 equivalents */
3569 *d++ = *(tmpbuf + 1);
3570 s++; /* We have just processed the 1st char */
3572 for (; s < send; s++) {
3573 d = uvchr_to_utf8(d, *s);
3576 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3580 else { /* in-place UTF-8. Just overwrite the first character */
3581 Copy(tmpbuf, d, tculen, U8);
3582 SvCUR_set(dest, need - 1);
3590 else { /* Neither source nor dest are in or need to be UTF-8 */
3592 if (IN_LOCALE_RUNTIME) {
3596 if (inplace) { /* in-place, only need to change the 1st char */
3599 else { /* Not in-place */
3601 /* Copy the case-changed character(s) from tmpbuf */
3602 Copy(tmpbuf, d, tculen, U8);
3603 d += tculen - 1; /* Code below expects d to point to final
3604 * character stored */
3607 else { /* empty source */
3608 /* See bug #39028: Don't taint if empty */
3612 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3613 * the destination to retain that flag */
3617 if (!inplace) { /* Finish the rest of the string, unchanged */
3618 /* This will copy the trailing NUL */
3619 Copy(s + 1, d + 1, slen, U8);
3620 SvCUR_set(dest, need - 1);
3623 if (dest != source && SvTAINTED(source))
3629 /* There's so much setup/teardown code common between uc and lc, I wonder if
3630 it would be worth merging the two, and just having a switch outside each
3631 of the three tight loops. There is less and less commonality though */
3645 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3646 && SvTEMP(source) && !DO_UTF8(source)
3647 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3649 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3650 * make the loop tight, so we overwrite the source with the dest before
3651 * looking at it, and we need to look at the original source
3652 * afterwards. There would also need to be code added to handle
3653 * switching to not in-place in midstream if we run into characters
3654 * that change the length.
3657 s = d = (U8*)SvPV_force_nomg(source, len);
3664 /* The old implementation would copy source into TARG at this point.
3665 This had the side effect that if source was undef, TARG was now
3666 an undefined SV with PADTMP set, and they don't warn inside
3667 sv_2pv_flags(). However, we're now getting the PV direct from
3668 source, which doesn't have PADTMP set, so it would warn. Hence the
3672 s = (const U8*)SvPV_nomg_const(source, len);
3674 if (ckWARN(WARN_UNINITIALIZED))
3675 report_uninit(source);
3681 SvUPGRADE(dest, SVt_PV);
3682 d = (U8*)SvGROW(dest, min);
3683 (void)SvPOK_only(dest);
3688 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3689 to check DO_UTF8 again here. */
3691 if (DO_UTF8(source)) {
3692 const U8 *const send = s + len;
3693 U8 tmpbuf[UTF8_MAXBYTES+1];
3694 bool tainted = FALSE;
3696 /* All occurrences of these are to be moved to follow any other marks.
3697 * This is context-dependent. We may not be passed enough context to
3698 * move the iota subscript beyond all of them, but we do the best we can
3699 * with what we're given. The result is always better than if we
3700 * hadn't done this. And, the problem would only arise if we are
3701 * passed a character without all its combining marks, which would be
3702 * the caller's mistake. The information this is based on comes from a
3703 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3704 * itself) and so can't be checked properly to see if it ever gets
3705 * revised. But the likelihood of it changing is remote */
3706 bool in_iota_subscript = FALSE;
3712 if (in_iota_subscript && ! is_utf8_mark(s)) {
3714 /* A non-mark. Time to output the iota subscript */
3715 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3716 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3718 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3719 in_iota_subscript = FALSE;
3722 /* Then handle the current character. Get the changed case value
3723 * and copy it to the output buffer */
3726 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3727 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3728 if (uv == GREEK_CAPITAL_LETTER_IOTA
3729 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3731 in_iota_subscript = TRUE;
3734 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3735 /* If the eventually required minimum size outgrows the
3736 * available space, we need to grow. */
3737 const UV o = d - (U8*)SvPVX_const(dest);
3739 /* If someone uppercases one million U+03B0s we SvGROW()
3740 * one million times. Or we could try guessing how much to
3741 * allocate without allocating too much. Such is life.
3742 * See corresponding comment in lc code for another option
3745 d = (U8*)SvPVX(dest) + o;
3747 Copy(tmpbuf, d, ulen, U8);
3752 if (in_iota_subscript) {
3753 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3758 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3764 else { /* Not UTF-8 */
3766 const U8 *const send = s + len;
3768 /* Use locale casing if in locale; regular style if not treating
3769 * latin1 as having case; otherwise the latin1 casing. Do the
3770 * whole thing in a tight loop, for speed, */
3771 if (IN_LOCALE_RUNTIME) {
3774 for (; s < send; d++, s++)
3775 *d = toUPPER_LC(*s);
3777 else if (! IN_UNI_8_BIT) {
3778 for (; s < send; d++, s++) {
3783 for (; s < send; d++, s++) {
3784 *d = toUPPER_LATIN1_MOD(*s);
3785 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3787 /* The mainstream case is the tight loop above. To avoid
3788 * extra tests in that, all three characters that require
3789 * special handling are mapped by the MOD to the one tested
3791 * Use the source to distinguish between the three cases */
3793 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3795 /* uc() of this requires 2 characters, but they are
3796 * ASCII. If not enough room, grow the string */
3797 if (SvLEN(dest) < ++min) {
3798 const UV o = d - (U8*)SvPVX_const(dest);
3800 d = (U8*)SvPVX(dest) + o;
3802 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3803 continue; /* Back to the tight loop; still in ASCII */
3806 /* The other two special handling characters have their
3807 * upper cases outside the latin1 range, hence need to be
3808 * in UTF-8, so the whole result needs to be in UTF-8. So,
3809 * here we are somewhere in the middle of processing a
3810 * non-UTF-8 string, and realize that we will have to convert
3811 * the whole thing to UTF-8. What to do? There are
3812 * several possibilities. The simplest to code is to
3813 * convert what we have so far, set a flag, and continue on
3814 * in the loop. The flag would be tested each time through
3815 * the loop, and if set, the next character would be
3816 * converted to UTF-8 and stored. But, I (khw) didn't want
3817 * to slow down the mainstream case at all for this fairly
3818 * rare case, so I didn't want to add a test that didn't
3819 * absolutely have to be there in the loop, besides the
3820 * possibility that it would get too complicated for
3821 * optimizers to deal with. Another possibility is to just
3822 * give up, convert the source to UTF-8, and restart the
3823 * function that way. Another possibility is to convert
3824 * both what has already been processed and what is yet to
3825 * come separately to UTF-8, then jump into the loop that
3826 * handles UTF-8. But the most efficient time-wise of the
3827 * ones I could think of is what follows, and turned out to
3828 * not require much extra code. */
3830 /* Convert what we have so far into UTF-8, telling the
3831 * function that we know it should be converted, and to
3832 * allow extra space for what we haven't processed yet.
3833 * Assume the worst case space requirements for converting
3834 * what we haven't processed so far: that it will require
3835 * two bytes for each remaining source character, plus the
3836 * NUL at the end. This may cause the string pointer to
3837 * move, so re-find it. */
3839 len = d - (U8*)SvPVX_const(dest);
3840 SvCUR_set(dest, len);
3841 len = sv_utf8_upgrade_flags_grow(dest,
3842 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3844 d = (U8*)SvPVX(dest) + len;
3846 /* Now process the remainder of the source, converting to
3847 * upper and UTF-8. If a resulting byte is invariant in
3848 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3849 * append it to the output. */
3850 for (; s < send; s++) {
3851 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3855 /* Here have processed the whole source; no need to continue
3856 * with the outer loop. Each character has been converted
3857 * to upper case and converted to UTF-8 */
3860 } /* End of processing all latin1-style chars */
3861 } /* End of processing all chars */
3862 } /* End of source is not empty */
3864 if (source != dest) {
3865 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3866 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3868 } /* End of isn't utf8 */
3869 if (dest != source && SvTAINTED(source))
3888 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3889 && SvTEMP(source) && !DO_UTF8(source)) {
3891 /* We can convert in place, as lowercasing anything in the latin1 range
3892 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3894 s = d = (U8*)SvPV_force_nomg(source, len);
3901 /* The old implementation would copy source into TARG at this point.
3902 This had the side effect that if source was undef, TARG was now
3903 an undefined SV with PADTMP set, and they don't warn inside
3904 sv_2pv_flags(). However, we're now getting the PV direct from
3905 source, which doesn't have PADTMP set, so it would warn. Hence the
3909 s = (const U8*)SvPV_nomg_const(source, len);
3911 if (ckWARN(WARN_UNINITIALIZED))
3912 report_uninit(source);
3918 SvUPGRADE(dest, SVt_PV);
3919 d = (U8*)SvGROW(dest, min);
3920 (void)SvPOK_only(dest);
3925 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3926 to check DO_UTF8 again here. */
3928 if (DO_UTF8(source)) {
3929 const U8 *const send = s + len;
3930 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3931 bool tainted = FALSE;
3934 const STRLEN u = UTF8SKIP(s);
3937 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3938 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3940 /* Here is where we would do context-sensitive actions. See the
3941 * commit message for this comment for why there isn't any */
3943 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3945 /* If the eventually required minimum size outgrows the
3946 * available space, we need to grow. */
3947 const UV o = d - (U8*)SvPVX_const(dest);
3949 /* If someone lowercases one million U+0130s we SvGROW() one
3950 * million times. Or we could try guessing how much to
3951 * allocate without allocating too much. Such is life.
3952 * Another option would be to grow an extra byte or two more
3953 * each time we need to grow, which would cut down the million
3954 * to 500K, with little waste */
3956 d = (U8*)SvPVX(dest) + o;
3959 /* Copy the newly lowercased letter to the output buffer we're
3961 Copy(tmpbuf, d, ulen, U8);
3964 } /* End of looping through the source string */
3967 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3972 } else { /* Not utf8 */
3974 const U8 *const send = s + len;
3976 /* Use locale casing if in locale; regular style if not treating
3977 * latin1 as having case; otherwise the latin1 casing. Do the
3978 * whole thing in a tight loop, for speed, */
3979 if (IN_LOCALE_RUNTIME) {
3982 for (; s < send; d++, s++)
3983 *d = toLOWER_LC(*s);
3985 else if (! IN_UNI_8_BIT) {
3986 for (; s < send; d++, s++) {
3991 for (; s < send; d++, s++) {
3992 *d = toLOWER_LATIN1(*s);
3996 if (source != dest) {
3998 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4001 if (dest != source && SvTAINTED(source))
4010 SV * const sv = TOPs;
4012 register const char *s = SvPV_const(sv,len);
4014 SvUTF8_off(TARG); /* decontaminate */
4017 SvUPGRADE(TARG, SVt_PV);
4018 SvGROW(TARG, (len * 2) + 1);
4022 STRLEN ulen = UTF8SKIP(s);
4023 bool to_quote = FALSE;
4025 if (UTF8_IS_INVARIANT(*s)) {
4026 if (_isQUOTEMETA(*s)) {
4030 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4032 /* In locale, we quote all non-ASCII Latin1 chars.
4033 * Otherwise use the quoting rules */
4034 if (IN_LOCALE_RUNTIME
4035 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4040 else if (_is_utf8_quotemeta((U8 *) s)) {
4055 else if (IN_UNI_8_BIT) {
4057 if (_isQUOTEMETA(*s))
4063 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4064 * including everything above ASCII */
4066 if (!isWORDCHAR_A(*s))
4072 SvCUR_set(TARG, d - SvPVX_const(TARG));
4073 (void)SvPOK_only_UTF8(TARG);
4076 sv_setpvn(TARG, s, len);
4093 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4094 const bool full_folding = TRUE;
4095 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4096 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4098 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4099 * You are welcome(?) -Hugmeir
4107 s = (const U8*)SvPV_nomg_const(source, len);
4109 if (ckWARN(WARN_UNINITIALIZED))
4110 report_uninit(source);
4117 SvUPGRADE(dest, SVt_PV);
4118 d = (U8*)SvGROW(dest, min);
4119 (void)SvPOK_only(dest);
4124 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4125 bool tainted = FALSE;
4127 const STRLEN u = UTF8SKIP(s);
4130 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4132 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4133 const UV o = d - (U8*)SvPVX_const(dest);
4135 d = (U8*)SvPVX(dest) + o;
4138 Copy(tmpbuf, d, ulen, U8);
4147 } /* Unflagged string */
4149 /* For locale, bytes, and nothing, the behavior is supposed to be the
4152 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4155 for (; s < send; d++, s++)
4156 *d = toLOWER_LC(*s);
4158 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4159 for (; s < send; d++, s++)
4163 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4164 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4165 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4166 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4169 for (; s < send; d++, s++) {
4170 if (*s == MICRO_SIGN) {
4171 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4172 * is outside of the latin-1 range. There's a couple of ways to
4173 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4174 * What we do here is upgrade what we had already casefolded,
4175 * then enter an inner loop that appends the rest of the characters
4178 len = d - (U8*)SvPVX_const(dest);
4179 SvCUR_set(dest, len);
4180 len = sv_utf8_upgrade_flags_grow(dest,
4181 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4182 /* The max expansion for latin1
4183 * chars is 1 byte becomes 2 */
4185 d = (U8*)SvPVX(dest) + len;
4187 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4189 for (; s < send; s++) {
4191 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4192 if UNI_IS_INVARIANT(fc) {
4193 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4201 Copy(tmpbuf, d, ulen, U8);
4207 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4208 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4209 * which may require growing the SV.
4211 if (SvLEN(dest) < ++min) {
4212 const UV o = d - (U8*)SvPVX_const(dest);
4214 d = (U8*)SvPVX(dest) + o;
4219 else { /* If it's not one of those two, the fold is their lower case */
4220 *d = toLOWER_LATIN1(*s);
4226 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4228 if (SvTAINTED(source))
4238 dVAR; dSP; dMARK; dORIGMARK;
4239 register AV *const av = MUTABLE_AV(POPs);
4240 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4242 if (SvTYPE(av) == SVt_PVAV) {
4243 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4244 bool can_preserve = FALSE;
4250 can_preserve = SvCANEXISTDELETE(av);
4253 if (lval && localizing) {
4256 for (svp = MARK + 1; svp <= SP; svp++) {
4257 const I32 elem = SvIV(*svp);
4261 if (max > AvMAX(av))
4265 while (++MARK <= SP) {
4267 I32 elem = SvIV(*MARK);
4268 bool preeminent = TRUE;
4270 if (localizing && can_preserve) {
4271 /* If we can determine whether the element exist,
4272 * Try to preserve the existenceness of a tied array
4273 * element by using EXISTS and DELETE if possible.
4274 * Fallback to FETCH and STORE otherwise. */
4275 preeminent = av_exists(av, elem);
4278 svp = av_fetch(av, elem, lval);
4280 if (!svp || *svp == &PL_sv_undef)
4281 DIE(aTHX_ PL_no_aelem, elem);
4284 save_aelem(av, elem, svp);
4286 SAVEADELETE(av, elem);
4289 *MARK = svp ? *svp : &PL_sv_undef;
4292 if (GIMME != G_ARRAY) {
4294 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4300 /* Smart dereferencing for keys, values and each */
4312 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4317 "Type of argument to %s must be unblessed hashref or arrayref",
4318 PL_op_desc[PL_op->op_type] );
4321 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4323 "Can't modify %s in %s",
4324 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4327 /* Delegate to correct function for op type */
4329 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4330 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4333 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4341 AV *array = MUTABLE_AV(POPs);
4342 const I32 gimme = GIMME_V;
4343 IV *iterp = Perl_av_iter_p(aTHX_ array);
4344 const IV current = (*iterp)++;
4346 if (current > av_len(array)) {
4348 if (gimme == G_SCALAR)
4356 if (gimme == G_ARRAY) {
4357 SV **const element = av_fetch(array, current, 0);
4358 PUSHs(element ? *element : &PL_sv_undef);
4367 AV *array = MUTABLE_AV(POPs);
4368 const I32 gimme = GIMME_V;
4370 *Perl_av_iter_p(aTHX_ array) = 0;
4372 if (gimme == G_SCALAR) {
4374 PUSHi(av_len(array) + 1);
4376 else if (gimme == G_ARRAY) {
4377 IV n = Perl_av_len(aTHX_ array);
4382 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4383 for (i = 0; i <= n; i++) {
4388 for (i = 0; i <= n; i++) {
4389 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4390 PUSHs(elem ? *elem : &PL_sv_undef);
4397 /* Associative arrays. */
4403 HV * hash = MUTABLE_HV(POPs);
4405 const I32 gimme = GIMME_V;
4408 /* might clobber stack_sp */
4409 entry = hv_iternext(hash);
4414 SV* const sv = hv_iterkeysv(entry);
4415 PUSHs(sv); /* won't clobber stack_sp */
4416 if (gimme == G_ARRAY) {
4419 /* might clobber stack_sp */
4420 val = hv_iterval(hash, entry);
4425 else if (gimme == G_SCALAR)
4432 S_do_delete_local(pTHX)
4436 const I32 gimme = GIMME_V;
4439 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4440 SV *unsliced_keysv = sliced ? NULL : POPs;
4441 SV * const osv = POPs;
4442 register SV **mark =
4443 sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4445 const bool tied = SvRMAGICAL(osv)
4446 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4447 const bool can_preserve = SvCANEXISTDELETE(osv);
4448 const U32 type = SvTYPE(osv);
4449 SV ** const end = sliced ? SP : &unsliced_keysv;
4451 if (type == SVt_PVHV) { /* hash element */
4452 HV * const hv = MUTABLE_HV(osv);
4453 while (++MARK <= end) {
4454 SV * const keysv = *MARK;
4456 bool preeminent = TRUE;
4458 preeminent = hv_exists_ent(hv, keysv, 0);
4460 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4467 sv = hv_delete_ent(hv, keysv, 0, 0);
4468 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4471 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4472 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4474 *MARK = sv_mortalcopy(sv);
4480 SAVEHDELETE(hv, keysv);
4481 *MARK = &PL_sv_undef;
4485 else if (type == SVt_PVAV) { /* array element */
4486 if (PL_op->op_flags & OPf_SPECIAL) {
4487 AV * const av = MUTABLE_AV(osv);
4488 while (++MARK <= end) {
4489 I32 idx = SvIV(*MARK);
4491 bool preeminent = TRUE;
4493 preeminent = av_exists(av, idx);
4495 SV **svp = av_fetch(av, idx, 1);
4502 sv = av_delete(av, idx, 0);
4503 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4506 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4508 *MARK = sv_mortalcopy(sv);
4514 SAVEADELETE(av, idx);
4515 *MARK = &PL_sv_undef;
4520 DIE(aTHX_ "panic: avhv_delete no longer supported");
4523 DIE(aTHX_ "Not a HASH reference");
4525 if (gimme == G_VOID)
4527 else if (gimme == G_SCALAR) {
4532 *++MARK = &PL_sv_undef;
4536 else if (gimme != G_VOID)
4537 PUSHs(unsliced_keysv);
4549 if (PL_op->op_private & OPpLVAL_INTRO)
4550 return do_delete_local();
4553 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4555 if (PL_op->op_private & OPpSLICE) {
4557 HV * const hv = MUTABLE_HV(POPs);
4558 const U32 hvtype = SvTYPE(hv);
4559 if (hvtype == SVt_PVHV) { /* hash element */
4560 while (++MARK <= SP) {
4561 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4562 *MARK = sv ? sv : &PL_sv_undef;
4565 else if (hvtype == SVt_PVAV) { /* array element */
4566 if (PL_op->op_flags & OPf_SPECIAL) {
4567 while (++MARK <= SP) {
4568 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4569 *MARK = sv ? sv : &PL_sv_undef;
4574 DIE(aTHX_ "Not a HASH reference");
4577 else if (gimme == G_SCALAR) {
4582 *++MARK = &PL_sv_undef;
4588 HV * const hv = MUTABLE_HV(POPs);
4590 if (SvTYPE(hv) == SVt_PVHV)
4591 sv = hv_delete_ent(hv, keysv, discard, 0);
4592 else if (SvTYPE(hv) == SVt_PVAV) {
4593 if (PL_op->op_flags & OPf_SPECIAL)
4594 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4596 DIE(aTHX_ "panic: avhv_delete no longer supported");
4599 DIE(aTHX_ "Not a HASH reference");
4615 if (PL_op->op_private & OPpEXISTS_SUB) {
4617 SV * const sv = POPs;
4618 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4621 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4626 hv = MUTABLE_HV(POPs);
4627 if (SvTYPE(hv) == SVt_PVHV) {
4628 if (hv_exists_ent(hv, tmpsv, 0))
4631 else if (SvTYPE(hv) == SVt_PVAV) {
4632 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4633 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4638 DIE(aTHX_ "Not a HASH reference");
4645 dVAR; dSP; dMARK; dORIGMARK;
4646 register HV * const hv = MUTABLE_HV(POPs);
4647 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4648 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4649 bool can_preserve = FALSE;
4655 if (SvCANEXISTDELETE(hv))
4656 can_preserve = TRUE;
4659 while (++MARK <= SP) {
4660 SV * const keysv = *MARK;
4663 bool preeminent = TRUE;
4665 if (localizing && can_preserve) {
4666 /* If we can determine whether the element exist,
4667 * try to preserve the existenceness of a tied hash
4668 * element by using EXISTS and DELETE if possible.
4669 * Fallback to FETCH and STORE otherwise. */
4670 preeminent = hv_exists_ent(hv, keysv, 0);
4673 he = hv_fetch_ent(hv, keysv, lval, 0);
4674 svp = he ? &HeVAL(he) : NULL;
4677 if (!svp || !*svp || *svp == &PL_sv_undef) {
4678 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4681 if (HvNAME_get(hv) && isGV(*svp))
4682 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4683 else if (preeminent)
4684 save_helem_flags(hv, keysv, svp,
4685 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4687 SAVEHDELETE(hv, keysv);
4690 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4692 if (GIMME != G_ARRAY) {
4694 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4700 /* List operators. */
4705 if (GIMME != G_ARRAY) {
4707 *MARK = *SP; /* unwanted list, return last item */
4709 *MARK = &PL_sv_undef;
4719 SV ** const lastrelem = PL_stack_sp;
4720 SV ** const lastlelem = PL_stack_base + POPMARK;
4721 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4722 register SV ** const firstrelem = lastlelem + 1;
4723 I32 is_something_there = FALSE;
4725 register const I32 max = lastrelem - lastlelem;
4726 register SV **lelem;
4728 if (GIMME != G_ARRAY) {
4729 I32 ix = SvIV(*lastlelem);
4732 if (ix < 0 || ix >= max)
4733 *firstlelem = &PL_sv_undef;
4735 *firstlelem = firstrelem[ix];
4741 SP = firstlelem - 1;
4745 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4746 I32 ix = SvIV(*lelem);
4749 if (ix < 0 || ix >= max)
4750 *lelem = &PL_sv_undef;
4752 is_something_there = TRUE;
4753 if (!(*lelem = firstrelem[ix]))
4754 *lelem = &PL_sv_undef;
4757 if (is_something_there)
4760 SP = firstlelem - 1;
4766 dVAR; dSP; dMARK; dORIGMARK;
4767 const I32 items = SP - MARK;
4768 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4769 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4770 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4771 ? newRV_noinc(av) : av);
4777 dVAR; dSP; dMARK; dORIGMARK;
4778 HV* const hv = newHV();
4781 SV * const key = *++MARK;
4782 SV * const val = newSV(0);
4784 sv_setsv(val, *++MARK);
4786 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4787 (void)hv_store_ent(hv,key,val,0);
4790 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4791 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4796 S_deref_plain_array(pTHX_ AV *ary)
4798 if (SvTYPE(ary) == SVt_PVAV) return ary;
4799 SvGETMAGIC((SV *)ary);
4800 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4801 Perl_die(aTHX_ "Not an ARRAY reference");
4802 else if (SvOBJECT(SvRV(ary)))
4803 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4804 return (AV *)SvRV(ary);
4807 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4808 # define DEREF_PLAIN_ARRAY(ary) \
4811 SvTYPE(aRrRay) == SVt_PVAV \
4813 : S_deref_plain_array(aTHX_ aRrRay); \
4816 # define DEREF_PLAIN_ARRAY(ary) \
4818 PL_Sv = (SV *)(ary), \
4819 SvTYPE(PL_Sv) == SVt_PVAV \
4821 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4827 dVAR; dSP; dMARK; dORIGMARK;
4828 int num_args = (SP - MARK);
4829 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4833 register I32 offset;
4834 register I32 length;
4838 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4841 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4842 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4849 offset = i = SvIV(*MARK);
4851 offset += AvFILLp(ary) + 1;
4853 DIE(aTHX_ PL_no_aelem, i);
4855 length = SvIVx(*MARK++);
4857 length += AvFILLp(ary) - offset + 1;
4863 length = AvMAX(ary) + 1; /* close enough to infinity */
4867 length = AvMAX(ary) + 1;
4869 if (offset > AvFILLp(ary) + 1) {
4871 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4872 offset = AvFILLp(ary) + 1;
4874 after = AvFILLp(ary) + 1 - (offset + length);
4875 if (after < 0) { /* not that much array */
4876 length += after; /* offset+length now in array */
4882 /* At this point, MARK .. SP-1 is our new LIST */
4885 diff = newlen - length;
4886 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4889 /* make new elements SVs now: avoid problems if they're from the array */
4890 for (dst = MARK, i = newlen; i; i--) {
4891 SV * const h = *dst;
4892 *dst++ = newSVsv(h);
4895 if (diff < 0) { /* shrinking the area */
4896 SV **tmparyval = NULL;
4898 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4899 Copy(MARK, tmparyval, newlen, SV*);
4902 MARK = ORIGMARK + 1;
4903 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4904 MEXTEND(MARK, length);
4905 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4907 EXTEND_MORTAL(length);
4908 for (i = length, dst = MARK; i; i--) {
4909 sv_2mortal(*dst); /* free them eventually */
4916 *MARK = AvARRAY(ary)[offset+length-1];
4919 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4920 SvREFCNT_dec(*dst++); /* free them now */
4923 AvFILLp(ary) += diff;
4925 /* pull up or down? */
4927 if (offset < after) { /* easier to pull up */
4928 if (offset) { /* esp. if nothing to pull */
4929 src = &AvARRAY(ary)[offset-1];
4930 dst = src - diff; /* diff is negative */
4931 for (i = offset; i > 0; i--) /* can't trust Copy */
4935 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4939 if (after) { /* anything to pull down? */
4940 src = AvARRAY(ary) + offset + length;
4941 dst = src + diff; /* diff is negative */
4942 Move(src, dst, after, SV*);
4944 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4945 /* avoid later double free */
4949 dst[--i] = &PL_sv_undef;
4952 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4953 Safefree(tmparyval);
4956 else { /* no, expanding (or same) */
4957 SV** tmparyval = NULL;
4959 Newx(tmparyval, length, SV*); /* so remember deletion */
4960 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4963 if (diff > 0) { /* expanding */
4964 /* push up or down? */
4965 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4969 Move(src, dst, offset, SV*);
4971 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
4973 AvFILLp(ary) += diff;
4976 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4977 av_extend(ary, AvFILLp(ary) + diff);
4978 AvFILLp(ary) += diff;
4981 dst = AvARRAY(ary) + AvFILLp(ary);
4983 for (i = after; i; i--) {
4991 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4994 MARK = ORIGMARK + 1;
4995 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4997 Copy(tmparyval, MARK, length, SV*);
4999 EXTEND_MORTAL(length);
5000 for (i = length, dst = MARK; i; i--) {
5001 sv_2mortal(*dst); /* free them eventually */
5008 else if (length--) {
5009 *MARK = tmparyval[length];
5012 while (length-- > 0)
5013 SvREFCNT_dec(tmparyval[length]);
5017 *MARK = &PL_sv_undef;
5018 Safefree(tmparyval);
5022 mg_set(MUTABLE_SV(ary));
5030 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5031 register AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5032 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5035 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5038 ENTER_with_name("call_PUSH");
5039 call_method("PUSH",G_SCALAR|G_DISCARD);
5040 LEAVE_with_name("call_PUSH");
5044 PL_delaymagic = DM_DELAY;
5045 for (++MARK; MARK <= SP; MARK++) {
5046 SV * const sv = newSV(0);
5048 sv_setsv(sv, *MARK);
5049 av_store(ary, AvFILLp(ary)+1, sv);
5051 if (PL_delaymagic & DM_ARRAY_ISA)
5052 mg_set(MUTABLE_SV(ary));
5057 if (OP_GIMME(PL_op, 0) != G_VOID) {
5058 PUSHi( AvFILL(ary) + 1 );
5067 AV * const av = PL_op->op_flags & OPf_SPECIAL
5068 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5069 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5073 (void)sv_2mortal(sv);
5080 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5081 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5082 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5085 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5088 ENTER_with_name("call_UNSHIFT");
5089 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5090 LEAVE_with_name("call_UNSHIFT");
5095 av_unshift(ary, SP - MARK);
5097 SV * const sv = newSVsv(*++MARK);
5098 (void)av_store(ary, i++, sv);
5102 if (OP_GIMME(PL_op, 0) != G_VOID) {
5103 PUSHi( AvFILL(ary) + 1 );
5112 if (GIMME == G_ARRAY) {
5113 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5117 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5118 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5119 av = MUTABLE_AV((*SP));
5120 /* In-place reversing only happens in void context for the array
5121 * assignment. We don't need to push anything on the stack. */
5124 if (SvMAGICAL(av)) {
5126 register SV *tmp = sv_newmortal();
5127 /* For SvCANEXISTDELETE */
5130 bool can_preserve = SvCANEXISTDELETE(av);
5132 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5133 register SV *begin, *end;
5136 if (!av_exists(av, i)) {
5137 if (av_exists(av, j)) {
5138 register SV *sv = av_delete(av, j, 0);
5139 begin = *av_fetch(av, i, TRUE);
5140 sv_setsv_mg(begin, sv);
5144 else if (!av_exists(av, j)) {
5145 register SV *sv = av_delete(av, i, 0);
5146 end = *av_fetch(av, j, TRUE);
5147 sv_setsv_mg(end, sv);
5152 begin = *av_fetch(av, i, TRUE);
5153 end = *av_fetch(av, j, TRUE);
5154 sv_setsv(tmp, begin);
5155 sv_setsv_mg(begin, end);
5156 sv_setsv_mg(end, tmp);
5160 SV **begin = AvARRAY(av);
5163 SV **end = begin + AvFILLp(av);
5165 while (begin < end) {
5166 register SV * const tmp = *begin;
5177 register SV * const tmp = *MARK;
5181 /* safe as long as stack cannot get extended in the above */
5187 register char *down;
5192 SvUTF8_off(TARG); /* decontaminate */
5194 do_join(TARG, &PL_sv_no, MARK, SP);
5196 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5197 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5198 report_uninit(TARG);
5201 up = SvPV_force(TARG, len);
5203 if (DO_UTF8(TARG)) { /* first reverse each character */
5204 U8* s = (U8*)SvPVX(TARG);
5205 const U8* send = (U8*)(s + len);
5207 if (UTF8_IS_INVARIANT(*s)) {
5212 if (!utf8_to_uvchr_buf(s, send, 0))
5216 down = (char*)(s - 1);
5217 /* reverse this character */
5221 *down-- = (char)tmp;
5227 down = SvPVX(TARG) + len - 1;
5231 *down-- = (char)tmp;
5233 (void)SvPOK_only_UTF8(TARG);
5245 register IV limit = POPi; /* note, negative is forever */
5246 SV * const sv = POPs;
5248 register const char *s = SvPV_const(sv, len);
5249 const bool do_utf8 = DO_UTF8(sv);
5250 const char *strend = s + len;
5252 register REGEXP *rx;
5254 register const char *m;
5256 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5257 I32 maxiters = slen + 10;
5258 I32 trailing_empty = 0;
5260 const I32 origlimit = limit;
5263 const I32 gimme = GIMME_V;
5265 const I32 oldsave = PL_savestack_ix;
5266 U32 make_mortal = SVs_TEMP;
5271 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5276 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5279 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5280 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5282 RX_MATCH_UTF8_set(rx, do_utf8);
5285 if (pm->op_pmreplrootu.op_pmtargetoff) {
5286 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5289 if (pm->op_pmreplrootu.op_pmtargetgv) {
5290 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5295 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5301 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5303 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5310 for (i = AvFILLp(ary); i >= 0; i--)
5311 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5313 /* temporarily switch stacks */
5314 SAVESWITCHSTACK(PL_curstack, ary);
5318 base = SP - PL_stack_base;
5320 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5322 while (*s == ' ' || is_utf8_space((U8*)s))
5325 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5326 while (isSPACE_LC(*s))
5334 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5338 gimme_scalar = gimme == G_SCALAR && !ary;
5341 limit = maxiters + 2;
5342 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5345 /* this one uses 'm' and is a negative test */
5347 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5348 const int t = UTF8SKIP(m);
5349 /* is_utf8_space returns FALSE for malform utf8 */
5356 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5357 while (m < strend && !isSPACE_LC(*m))
5360 while (m < strend && !isSPACE(*m))
5373 dstr = newSVpvn_flags(s, m-s,
5374 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5378 /* skip the whitespace found last */
5380 s = m + UTF8SKIP(m);
5384 /* this one uses 's' and is a positive test */
5386 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5389 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5390 while (s < strend && isSPACE_LC(*s))
5393 while (s < strend && isSPACE(*s))
5398 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5400 for (m = s; m < strend && *m != '\n'; m++)
5413 dstr = newSVpvn_flags(s, m-s,
5414 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5420 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5422 Pre-extend the stack, either the number of bytes or
5423 characters in the string or a limited amount, triggered by:
5425 my ($x, $y) = split //, $str;
5429 if (!gimme_scalar) {
5430 const U32 items = limit - 1;
5439 /* keep track of how many bytes we skip over */
5449 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5462 dstr = newSVpvn(s, 1);
5478 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5479 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5480 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5481 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5482 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5483 SV * const csv = CALLREG_INTUIT_STRING(rx);
5485 len = RX_MINLENRET(rx);
5486 if (len == 1 && !RX_UTF8(rx) && !tail) {
5487 const char c = *SvPV_nolen_const(csv);
5489 for (m = s; m < strend && *m != c; m++)
5500 dstr = newSVpvn_flags(s, m-s,
5501 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5504 /* The rx->minlen is in characters but we want to step
5505 * s ahead by bytes. */
5507 s = (char*)utf8_hop((U8*)m, len);
5509 s = m + len; /* Fake \n at the end */
5513 while (s < strend && --limit &&
5514 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5515 csv, multiline ? FBMrf_MULTILINE : 0)) )
5524 dstr = newSVpvn_flags(s, m-s,
5525 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5528 /* The rx->minlen is in characters but we want to step
5529 * s ahead by bytes. */
5531 s = (char*)utf8_hop((U8*)m, len);
5533 s = m + len; /* Fake \n at the end */
5538 maxiters += slen * RX_NPARENS(rx);
5539 while (s < strend && --limit)
5543 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5546 if (rex_return == 0)
5548 TAINT_IF(RX_MATCH_TAINTED(rx));
5549 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5552 orig = RX_SUBBEG(rx);
5554 strend = s + (strend - m);
5556 m = RX_OFFS(rx)[0].start + orig;
5565 dstr = newSVpvn_flags(s, m-s,
5566 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5569 if (RX_NPARENS(rx)) {
5571 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5572 s = RX_OFFS(rx)[i].start + orig;
5573 m = RX_OFFS(rx)[i].end + orig;
5575 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5576 parens that didn't match -- they should be set to
5577 undef, not the empty string */
5585 if (m >= orig && s >= orig) {
5586 dstr = newSVpvn_flags(s, m-s,
5587 (do_utf8 ? SVf_UTF8 : 0)
5591 dstr = &PL_sv_undef; /* undef, not "" */
5597 s = RX_OFFS(rx)[0].end + orig;
5601 if (!gimme_scalar) {
5602 iters = (SP - PL_stack_base) - base;
5604 if (iters > maxiters)
5605 DIE(aTHX_ "Split loop");
5607 /* keep field after final delim? */
5608 if (s < strend || (iters && origlimit)) {
5609 if (!gimme_scalar) {
5610 const STRLEN l = strend - s;
5611 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5616 else if (!origlimit) {
5618 iters -= trailing_empty;
5620 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5621 if (TOPs && !make_mortal)
5623 *SP-- = &PL_sv_undef;
5630 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5634 if (SvSMAGICAL(ary)) {
5636 mg_set(MUTABLE_SV(ary));
5639 if (gimme == G_ARRAY) {
5641 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5648 ENTER_with_name("call_PUSH");
5649 call_method("PUSH",G_SCALAR|G_DISCARD);
5650 LEAVE_with_name("call_PUSH");
5652 if (gimme == G_ARRAY) {
5654 /* EXTEND should not be needed - we just popped them */
5656 for (i=0; i < iters; i++) {
5657 SV **svp = av_fetch(ary, i, FALSE);
5658 PUSHs((svp) ? *svp : &PL_sv_undef);
5665 if (gimme == G_ARRAY)
5677 SV *const sv = PAD_SVl(PL_op->op_targ);
5679 if (SvPADSTALE(sv)) {
5682 RETURNOP(cLOGOP->op_other);
5684 RETURNOP(cLOGOP->op_next);
5694 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5695 || SvTYPE(retsv) == SVt_PVCV) {
5696 retsv = refto(retsv);
5703 PP(unimplemented_op)
5706 const Optype op_type = PL_op->op_type;
5707 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5708 with out of range op numbers - it only "special" cases op_custom.
5709 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5710 if we get here for a custom op then that means that the custom op didn't
5711 have an implementation. Given that OP_NAME() looks up the custom op
5712 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5713 registers &PL_unimplemented_op as the address of their custom op.
5714 NULL doesn't generate a useful error message. "custom" does. */
5715 const char *const name = op_type >= OP_max
5716 ? "[out of range]" : PL_op_name[PL_op->op_type];
5717 if(OP_IS_SOCKET(op_type))
5718 DIE(aTHX_ PL_no_sock_func, name);
5719 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5726 HV * const hv = (HV*)POPs;
5728 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
5730 if (SvRMAGICAL(hv)) {
5731 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5733 XPUSHs(magic_scalarpack(hv, mg));
5738 XPUSHs(boolSV(HvUSEDKEYS(hv) != 0));
5742 /* For sorting out arguments passed to a &CORE:: subroutine */
5746 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5747 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5748 AV * const at_ = GvAV(PL_defgv);
5749 SV **svp = at_ ? AvARRAY(at_) : NULL;
5750 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5751 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5752 bool seen_question = 0;
5753 const char *err = NULL;
5754 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5756 /* Count how many args there are first, to get some idea how far to
5757 extend the stack. */
5759 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5761 if (oa & OA_OPTIONAL) seen_question = 1;
5762 if (!seen_question) minargs++;
5766 if(numargs < minargs) err = "Not enough";
5767 else if(numargs > maxargs) err = "Too many";
5769 /* diag_listed_as: Too many arguments for %s */
5771 "%s arguments for %s", err,
5772 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5775 /* Reset the stack pointer. Without this, we end up returning our own
5776 arguments in list context, in addition to the values we are supposed
5777 to return. nextstate usually does this on sub entry, but we need
5778 to run the next op with the caller's hints, so we cannot have a
5780 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5782 if(!maxargs) RETURN;
5784 /* We do this here, rather than with a separate pushmark op, as it has
5785 to come in between two things this function does (stack reset and
5786 arg pushing). This seems the easiest way to do it. */
5789 (void)Perl_pp_pushmark(aTHX);
5792 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5793 PUTBACK; /* The code below can die in various places. */
5795 oa = PL_opargs[opnum] >> OASHIFT;
5796 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5801 if (!numargs && defgv && whicharg == minargs + 1) {
5802 PUSHs(find_rundefsv2(
5803 find_runcv_where(FIND_RUNCV_level_eq, (void *)1, NULL),
5804 cxstack[cxstack_ix].blk_oldcop->cop_seq
5807 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5811 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5816 if (!svp || !*svp || !SvROK(*svp)
5817 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5819 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5820 "Type of arg %d to &CORE::%s must be hash reference",
5821 whicharg, OP_DESC(PL_op->op_next)
5826 if (!numargs) PUSHs(NULL);
5827 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5828 /* no magic here, as the prototype will have added an extra
5829 refgen and we just want what was there before that */
5832 const bool constr = PL_op->op_private & whicharg;
5834 svp && *svp ? *svp : &PL_sv_undef,
5835 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5841 if (!numargs) goto try_defsv;
5843 const bool wantscalar =
5844 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5845 if (!svp || !*svp || !SvROK(*svp)
5846 /* We have to permit globrefs even for the \$ proto, as
5847 *foo is indistinguishable from ${\*foo}, and the proto-
5848 type permits the latter. */
5849 || SvTYPE(SvRV(*svp)) > (
5850 wantscalar ? SVt_PVLV
5851 : opnum == OP_LOCK || opnum == OP_UNDEF
5857 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5858 "Type of arg %d to &CORE::%s must be %s",
5859 whicharg, PL_op_name[opnum],
5861 ? "scalar reference"
5862 : opnum == OP_LOCK || opnum == OP_UNDEF
5863 ? "reference to one of [$@%&*]"
5864 : "reference to one of [$@%*]"
5867 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5868 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5869 /* Undo @_ localisation, so that sub exit does not undo
5870 part of our undeffing. */
5871 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5873 cx->cx_type &= ~ CXp_HASARGS;
5874 assert(!AvREAL(cx->blk_sub.argarray));
5879 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5891 if (PL_op->op_private & OPpOFFBYONE) {
5892 cv = find_runcv_where(FIND_RUNCV_level_eq, (void *)1, NULL);
5894 else cv = find_runcv(NULL);
5895 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5902 * c-indentation-style: bsd
5904 * indent-tabs-mode: nil
5907 * ex: set ts=8 sts=4 sw=4 et: