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.
32 #include "regcharclass.h"
34 /* XXX I can't imagine anyone who doesn't have this actually _needs_
35 it, since pid_t is an integral type.
38 #ifdef NEED_GETPID_PROTO
39 extern Pid_t getpid (void);
43 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
44 * This switches them over to IEEE.
46 #if defined(LIBM_LIB_VERSION)
47 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
50 /* variations on pp_null */
56 if (GIMME_V == G_SCALAR)
67 assert(SvTYPE(TARG) == SVt_PVAV);
68 if (PL_op->op_private & OPpLVAL_INTRO)
69 if (!(PL_op->op_private & OPpPAD_STATE))
70 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
72 if (PL_op->op_flags & OPf_REF) {
75 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
76 const I32 flags = is_lvalue_sub();
77 if (flags && !(flags & OPpENTERSUB_INARGS)) {
78 if (GIMME == G_SCALAR)
79 /* diag_listed_as: Can't return %s to lvalue scalar context */
80 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
86 if (gimme == G_ARRAY) {
87 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
89 if (SvMAGICAL(TARG)) {
91 for (i=0; i < (U32)maxarg; i++) {
92 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
93 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
97 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
101 else if (gimme == G_SCALAR) {
102 SV* const sv = sv_newmortal();
103 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
104 sv_setiv(sv, maxarg);
115 assert(SvTYPE(TARG) == SVt_PVHV);
117 if (PL_op->op_private & OPpLVAL_INTRO)
118 if (!(PL_op->op_private & OPpPAD_STATE))
119 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
120 if (PL_op->op_flags & OPf_REF)
122 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
123 const I32 flags = is_lvalue_sub();
124 if (flags && !(flags & OPpENTERSUB_INARGS)) {
125 if (GIMME == G_SCALAR)
126 /* diag_listed_as: Can't return %s to lvalue scalar context */
127 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
132 if (gimme == G_ARRAY) {
133 RETURNOP(Perl_do_kv(aTHX));
135 else if ((PL_op->op_private & OPpTRUEBOOL
136 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
137 && block_gimme() == G_VOID ))
138 && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied)))
139 SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
140 else if (gimme == G_SCALAR) {
141 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
150 assert(SvTYPE(TARG) == SVt_PVCV);
158 SvPADSTALE_off(TARG);
166 mg_find(AvARRAY(PL_comppad_name)[ARGTARG], PERL_MAGIC_proto);
167 assert(SvTYPE(TARG) == SVt_PVCV);
170 if (CvISXSUB(mg->mg_obj)) { /* constant */
171 /* XXX Should we clone it here? */
172 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
173 to introcv and remove the SvPADSTALE_off. */
174 SAVEPADSVANDMORTALIZE(ARGTARG);
175 PAD_SVl(ARGTARG) = mg->mg_obj;
178 if (CvROOT(mg->mg_obj)) {
179 assert(CvCLONE(mg->mg_obj));
180 assert(!CvCLONED(mg->mg_obj));
182 cv_clone_into((CV *)mg->mg_obj,(CV *)TARG);
183 SAVECLEARSV(PAD_SVl(ARGTARG));
190 static const char S_no_symref_sv[] =
191 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
193 /* In some cases this function inspects PL_op. If this function is called
194 for new op types, more bool parameters may need to be added in place of
197 When noinit is true, the absence of a gv will cause a retval of undef.
198 This is unrelated to the cv-to-gv assignment case.
202 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
206 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
209 sv = amagic_deref_call(sv, to_gv_amg);
213 if (SvTYPE(sv) == SVt_PVIO) {
214 GV * const gv = MUTABLE_GV(sv_newmortal());
215 gv_init(gv, 0, "__ANONIO__", 10, 0);
216 GvIOp(gv) = MUTABLE_IO(sv);
217 SvREFCNT_inc_void_NN(sv);
220 else if (!isGV_with_GP(sv))
221 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
224 if (!isGV_with_GP(sv)) {
226 /* If this is a 'my' scalar and flag is set then vivify
229 if (vivify_sv && sv != &PL_sv_undef) {
232 Perl_croak_no_modify(aTHX);
233 if (cUNOP->op_targ) {
234 SV * const namesv = PAD_SV(cUNOP->op_targ);
235 gv = MUTABLE_GV(newSV(0));
236 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
239 const char * const name = CopSTASHPV(PL_curcop);
240 gv = newGVgen_flags(name,
241 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
243 prepare_SV_for_RV(sv);
244 SvRV_set(sv, MUTABLE_SV(gv));
249 if (PL_op->op_flags & OPf_REF || strict)
250 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
251 if (ckWARN(WARN_UNINITIALIZED))
257 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
258 sv, GV_ADDMG, SVt_PVGV
268 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
271 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
272 == OPpDONT_INIT_GV) {
273 /* We are the target of a coderef assignment. Return
274 the scalar unchanged, and let pp_sasssign deal with
278 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
280 /* FAKE globs in the symbol table cause weird bugs (#77810) */
284 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
285 SV *newsv = sv_newmortal();
286 sv_setsv_flags(newsv, sv, 0);
298 sv, PL_op->op_private & OPpDEREF,
299 PL_op->op_private & HINT_STRICT_REFS,
300 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
301 || PL_op->op_type == OP_READLINE
303 if (PL_op->op_private & OPpLVAL_INTRO)
304 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
309 /* Helper function for pp_rv2sv and pp_rv2av */
311 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
312 const svtype type, SV ***spp)
317 PERL_ARGS_ASSERT_SOFTREF2XV;
319 if (PL_op->op_private & HINT_STRICT_REFS) {
321 Perl_die(aTHX_ S_no_symref_sv, sv,
322 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
324 Perl_die(aTHX_ PL_no_usym, what);
328 PL_op->op_flags & OPf_REF
330 Perl_die(aTHX_ PL_no_usym, what);
331 if (ckWARN(WARN_UNINITIALIZED))
333 if (type != SVt_PV && GIMME_V == G_ARRAY) {
337 **spp = &PL_sv_undef;
340 if ((PL_op->op_flags & OPf_SPECIAL) &&
341 !(PL_op->op_flags & OPf_MOD))
343 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
345 **spp = &PL_sv_undef;
350 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
363 sv = amagic_deref_call(sv, to_sv_amg);
367 switch (SvTYPE(sv)) {
373 DIE(aTHX_ "Not a SCALAR reference");
380 if (!isGV_with_GP(gv)) {
381 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
387 if (PL_op->op_flags & OPf_MOD) {
388 if (PL_op->op_private & OPpLVAL_INTRO) {
389 if (cUNOP->op_first->op_type == OP_NULL)
390 sv = save_scalar(MUTABLE_GV(TOPs));
392 sv = save_scalar(gv);
394 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
396 else if (PL_op->op_private & OPpDEREF)
397 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
406 AV * const av = MUTABLE_AV(TOPs);
407 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
409 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
411 *sv = newSV_type(SVt_PVMG);
412 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
416 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
425 if (PL_op->op_flags & OPf_MOD || LVRET) {
426 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
427 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
429 LvTARG(ret) = SvREFCNT_inc_simple(sv);
430 PUSHs(ret); /* no SvSETMAGIC */
434 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
435 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
436 if (mg && mg->mg_len >= 0) {
454 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
456 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
459 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
460 /* (But not in defined().) */
462 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
464 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
468 cv = MUTABLE_CV(&PL_sv_undef);
469 SETs(MUTABLE_SV(cv));
479 SV *ret = &PL_sv_undef;
481 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
482 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
483 const char * s = SvPVX_const(TOPs);
484 if (strnEQ(s, "CORE::", 6)) {
485 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
486 if (!code || code == -KEY_CORE)
487 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
488 SVfARG(newSVpvn_flags(
489 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
492 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
498 cv = sv_2cv(TOPs, &stash, &gv, 0);
500 ret = newSVpvn_flags(
501 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
511 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
513 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
515 PUSHs(MUTABLE_SV(cv));
529 if (GIMME != G_ARRAY) {
533 *MARK = &PL_sv_undef;
534 *MARK = refto(*MARK);
538 EXTEND_MORTAL(SP - MARK);
540 *MARK = refto(*MARK);
545 S_refto(pTHX_ SV *sv)
550 PERL_ARGS_ASSERT_REFTO;
552 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
555 if (!(sv = LvTARG(sv)))
558 SvREFCNT_inc_void_NN(sv);
560 else if (SvTYPE(sv) == SVt_PVAV) {
561 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
562 av_reify(MUTABLE_AV(sv));
564 SvREFCNT_inc_void_NN(sv);
566 else if (SvPADTMP(sv) && !IS_PADGV(sv))
570 SvREFCNT_inc_void_NN(sv);
573 sv_upgrade(rv, SVt_IV);
582 SV * const sv = POPs;
587 if (!sv || !SvROK(sv))
590 (void)sv_ref(TARG,SvRV(sv),TRUE);
602 stash = CopSTASH(PL_curcop);
604 SV * const ssv = POPs;
608 if (!ssv) goto curstash;
609 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
610 Perl_croak(aTHX_ "Attempt to bless into a reference");
611 ptr = SvPV_const(ssv,len);
613 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
614 "Explicit blessing to '' (assuming package main)");
615 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
618 (void)sv_bless(TOPs, stash);
628 const char * const elem = SvPV_const(sv, len);
629 GV * const gv = MUTABLE_GV(POPs);
634 /* elem will always be NUL terminated. */
635 const char * const second_letter = elem + 1;
638 if (len == 5 && strEQ(second_letter, "RRAY"))
639 tmpRef = MUTABLE_SV(GvAV(gv));
642 if (len == 4 && strEQ(second_letter, "ODE"))
643 tmpRef = MUTABLE_SV(GvCVu(gv));
646 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
647 /* finally deprecated in 5.8.0 */
648 deprecate("*glob{FILEHANDLE}");
649 tmpRef = MUTABLE_SV(GvIOp(gv));
652 if (len == 6 && strEQ(second_letter, "ORMAT"))
653 tmpRef = MUTABLE_SV(GvFORM(gv));
656 if (len == 4 && strEQ(second_letter, "LOB"))
657 tmpRef = MUTABLE_SV(gv);
660 if (len == 4 && strEQ(second_letter, "ASH"))
661 tmpRef = MUTABLE_SV(GvHV(gv));
664 if (*second_letter == 'O' && !elem[2] && len == 2)
665 tmpRef = MUTABLE_SV(GvIOp(gv));
668 if (len == 4 && strEQ(second_letter, "AME"))
669 sv = newSVhek(GvNAME_HEK(gv));
672 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
673 const HV * const stash = GvSTASH(gv);
674 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
675 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
679 if (len == 6 && strEQ(second_letter, "CALAR"))
694 /* Pattern matching */
702 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
703 /* Historically, study was skipped in these cases. */
707 /* Make study a no-op. It's no longer useful and its existence
708 complicates matters elsewhere. */
717 if (PL_op->op_flags & OPf_STACKED)
719 else if (PL_op->op_private & OPpTARGET_MY)
725 if(PL_op->op_type == OP_TRANSR) {
727 const char * const pv = SvPV(sv,len);
728 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
733 TARG = sv_newmortal();
739 /* Lvalue operators. */
742 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
748 PERL_ARGS_ASSERT_DO_CHOMP;
750 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
752 if (SvTYPE(sv) == SVt_PVAV) {
754 AV *const av = MUTABLE_AV(sv);
755 const I32 max = AvFILL(av);
757 for (i = 0; i <= max; i++) {
758 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
759 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
760 do_chomp(retval, sv, chomping);
764 else if (SvTYPE(sv) == SVt_PVHV) {
765 HV* const hv = MUTABLE_HV(sv);
767 (void)hv_iterinit(hv);
768 while ((entry = hv_iternext(hv)))
769 do_chomp(retval, hv_iterval(hv,entry), chomping);
772 else if (SvREADONLY(sv)) {
774 /* SV is copy-on-write */
775 sv_force_normal_flags(sv, 0);
778 Perl_croak_no_modify(aTHX);
783 /* XXX, here sv is utf8-ized as a side-effect!
784 If encoding.pm is used properly, almost string-generating
785 operations, including literal strings, chr(), input data, etc.
786 should have been utf8-ized already, right?
788 sv_recode_to_utf8(sv, PL_encoding);
794 char *temp_buffer = NULL;
803 while (len && s[-1] == '\n') {
810 STRLEN rslen, rs_charlen;
811 const char *rsptr = SvPV_const(PL_rs, rslen);
813 rs_charlen = SvUTF8(PL_rs)
817 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
818 /* Assumption is that rs is shorter than the scalar. */
820 /* RS is utf8, scalar is 8 bit. */
822 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
825 /* Cannot downgrade, therefore cannot possibly match
827 assert (temp_buffer == rsptr);
833 else if (PL_encoding) {
834 /* RS is 8 bit, encoding.pm is used.
835 * Do not recode PL_rs as a side-effect. */
836 svrecode = newSVpvn(rsptr, rslen);
837 sv_recode_to_utf8(svrecode, PL_encoding);
838 rsptr = SvPV_const(svrecode, rslen);
839 rs_charlen = sv_len_utf8(svrecode);
842 /* RS is 8 bit, scalar is utf8. */
843 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
857 if (memNE(s, rsptr, rslen))
859 SvIVX(retval) += rs_charlen;
862 s = SvPV_force_nomg_nolen(sv);
870 SvREFCNT_dec(svrecode);
872 Safefree(temp_buffer);
874 if (len && !SvPOK(sv))
875 s = SvPV_force_nomg(sv, len);
878 char * const send = s + len;
879 char * const start = s;
881 while (s > start && UTF8_IS_CONTINUATION(*s))
883 if (is_utf8_string((U8*)s, send - s)) {
884 sv_setpvn(retval, s, send - s);
886 SvCUR_set(sv, s - start);
892 sv_setpvs(retval, "");
896 sv_setpvn(retval, s, 1);
903 sv_setpvs(retval, "");
911 const bool chomping = PL_op->op_type == OP_SCHOMP;
915 do_chomp(TARG, TOPs, chomping);
922 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
923 const bool chomping = PL_op->op_type == OP_CHOMP;
928 do_chomp(TARG, *++MARK, chomping);
939 if (!PL_op->op_private) {
948 SV_CHECK_THINKFIRST_COW_DROP(sv);
950 switch (SvTYPE(sv)) {
954 av_undef(MUTABLE_AV(sv));
957 hv_undef(MUTABLE_HV(sv));
960 if (cv_const_sv((const CV *)sv))
961 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
962 "Constant subroutine %"SVf" undefined",
963 SVfARG(CvANON((const CV *)sv)
964 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
965 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
969 /* let user-undef'd sub keep its identity */
970 GV* const gv = CvGV((const CV *)sv);
971 HEK * const hek = CvNAME_HEK((CV *)sv);
972 if (hek) share_hek_hek(hek);
973 cv_undef(MUTABLE_CV(sv));
974 if (gv) CvGV_set(MUTABLE_CV(sv), gv);
976 SvANY((CV *)sv)->xcv_gv_u.xcv_hek = hek;
983 SvSetMagicSV(sv, &PL_sv_undef);
986 else if (isGV_with_GP(sv)) {
990 /* undef *Pkg::meth_name ... */
992 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
993 && HvENAME_get(stash);
995 if((stash = GvHV((const GV *)sv))) {
996 if(HvENAME_get(stash))
997 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1001 gp_free(MUTABLE_GV(sv));
1003 GvGP_set(sv, gp_ref(gp));
1004 GvSV(sv) = newSV(0);
1005 GvLINE(sv) = CopLINE(PL_curcop);
1006 GvEGV(sv) = MUTABLE_GV(sv);
1010 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1012 /* undef *Foo::ISA */
1013 if( strEQ(GvNAME((const GV *)sv), "ISA")
1014 && (stash = GvSTASH((const GV *)sv))
1015 && (method_changed || HvENAME(stash)) )
1016 mro_isa_changed_in(stash);
1017 else if(method_changed)
1018 mro_method_changed_in(
1019 GvSTASH((const GV *)sv)
1026 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1042 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1043 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1044 Perl_croak_no_modify(aTHX);
1046 TARG = sv_newmortal();
1047 sv_setsv(TARG, TOPs);
1048 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1049 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1051 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1052 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1056 else sv_dec_nomg(TOPs);
1058 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1059 if (inc && !SvOK(TARG))
1065 /* Ordinary operators. */
1069 dVAR; dSP; dATARGET; SV *svl, *svr;
1070 #ifdef PERL_PRESERVE_IVUV
1073 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1076 #ifdef PERL_PRESERVE_IVUV
1077 /* For integer to integer power, we do the calculation by hand wherever
1078 we're sure it is safe; otherwise we call pow() and try to convert to
1079 integer afterwards. */
1080 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1088 const IV iv = SvIVX(svr);
1092 goto float_it; /* Can't do negative powers this way. */
1096 baseuok = SvUOK(svl);
1098 baseuv = SvUVX(svl);
1100 const IV iv = SvIVX(svl);
1103 baseuok = TRUE; /* effectively it's a UV now */
1105 baseuv = -iv; /* abs, baseuok == false records sign */
1108 /* now we have integer ** positive integer. */
1111 /* foo & (foo - 1) is zero only for a power of 2. */
1112 if (!(baseuv & (baseuv - 1))) {
1113 /* We are raising power-of-2 to a positive integer.
1114 The logic here will work for any base (even non-integer
1115 bases) but it can be less accurate than
1116 pow (base,power) or exp (power * log (base)) when the
1117 intermediate values start to spill out of the mantissa.
1118 With powers of 2 we know this can't happen.
1119 And powers of 2 are the favourite thing for perl
1120 programmers to notice ** not doing what they mean. */
1122 NV base = baseuok ? baseuv : -(NV)baseuv;
1127 while (power >>= 1) {
1135 SvIV_please_nomg(svr);
1138 unsigned int highbit = 8 * sizeof(UV);
1139 unsigned int diff = 8 * sizeof(UV);
1140 while (diff >>= 1) {
1142 if (baseuv >> highbit) {
1146 /* we now have baseuv < 2 ** highbit */
1147 if (power * highbit <= 8 * sizeof(UV)) {
1148 /* result will definitely fit in UV, so use UV math
1149 on same algorithm as above */
1152 const bool odd_power = cBOOL(power & 1);
1156 while (power >>= 1) {
1163 if (baseuok || !odd_power)
1164 /* answer is positive */
1166 else if (result <= (UV)IV_MAX)
1167 /* answer negative, fits in IV */
1168 SETi( -(IV)result );
1169 else if (result == (UV)IV_MIN)
1170 /* 2's complement assumption: special case IV_MIN */
1173 /* answer negative, doesn't fit */
1174 SETn( -(NV)result );
1182 NV right = SvNV_nomg(svr);
1183 NV left = SvNV_nomg(svl);
1186 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1188 We are building perl with long double support and are on an AIX OS
1189 afflicted with a powl() function that wrongly returns NaNQ for any
1190 negative base. This was reported to IBM as PMR #23047-379 on
1191 03/06/2006. The problem exists in at least the following versions
1192 of AIX and the libm fileset, and no doubt others as well:
1194 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1195 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1196 AIX 5.2.0 bos.adt.libm 5.2.0.85
1198 So, until IBM fixes powl(), we provide the following workaround to
1199 handle the problem ourselves. Our logic is as follows: for
1200 negative bases (left), we use fmod(right, 2) to check if the
1201 exponent is an odd or even integer:
1203 - if odd, powl(left, right) == -powl(-left, right)
1204 - if even, powl(left, right) == powl(-left, right)
1206 If the exponent is not an integer, the result is rightly NaNQ, so
1207 we just return that (as NV_NAN).
1211 NV mod2 = Perl_fmod( right, 2.0 );
1212 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1213 SETn( -Perl_pow( -left, right) );
1214 } else if (mod2 == 0.0) { /* even integer */
1215 SETn( Perl_pow( -left, right) );
1216 } else { /* fractional power */
1220 SETn( Perl_pow( left, right) );
1223 SETn( Perl_pow( left, right) );
1224 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1226 #ifdef PERL_PRESERVE_IVUV
1228 SvIV_please_nomg(svr);
1236 dVAR; dSP; dATARGET; SV *svl, *svr;
1237 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1240 #ifdef PERL_PRESERVE_IVUV
1241 if (SvIV_please_nomg(svr)) {
1242 /* Unless the left argument is integer in range we are going to have to
1243 use NV maths. Hence only attempt to coerce the right argument if
1244 we know the left is integer. */
1245 /* Left operand is defined, so is it IV? */
1246 if (SvIV_please_nomg(svl)) {
1247 bool auvok = SvUOK(svl);
1248 bool buvok = SvUOK(svr);
1249 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1250 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1259 const IV aiv = SvIVX(svl);
1262 auvok = TRUE; /* effectively it's a UV now */
1264 alow = -aiv; /* abs, auvok == false records sign */
1270 const IV biv = SvIVX(svr);
1273 buvok = TRUE; /* effectively it's a UV now */
1275 blow = -biv; /* abs, buvok == false records sign */
1279 /* If this does sign extension on unsigned it's time for plan B */
1280 ahigh = alow >> (4 * sizeof (UV));
1282 bhigh = blow >> (4 * sizeof (UV));
1284 if (ahigh && bhigh) {
1286 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1287 which is overflow. Drop to NVs below. */
1288 } else if (!ahigh && !bhigh) {
1289 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1290 so the unsigned multiply cannot overflow. */
1291 const UV product = alow * blow;
1292 if (auvok == buvok) {
1293 /* -ve * -ve or +ve * +ve gives a +ve result. */
1297 } else if (product <= (UV)IV_MIN) {
1298 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1299 /* -ve result, which could overflow an IV */
1301 SETi( -(IV)product );
1303 } /* else drop to NVs below. */
1305 /* One operand is large, 1 small */
1308 /* swap the operands */
1310 bhigh = blow; /* bhigh now the temp var for the swap */
1314 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1315 multiplies can't overflow. shift can, add can, -ve can. */
1316 product_middle = ahigh * blow;
1317 if (!(product_middle & topmask)) {
1318 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1320 product_middle <<= (4 * sizeof (UV));
1321 product_low = alow * blow;
1323 /* as for pp_add, UV + something mustn't get smaller.
1324 IIRC ANSI mandates this wrapping *behaviour* for
1325 unsigned whatever the actual representation*/
1326 product_low += product_middle;
1327 if (product_low >= product_middle) {
1328 /* didn't overflow */
1329 if (auvok == buvok) {
1330 /* -ve * -ve or +ve * +ve gives a +ve result. */
1332 SETu( product_low );
1334 } else if (product_low <= (UV)IV_MIN) {
1335 /* 2s complement assumption again */
1336 /* -ve result, which could overflow an IV */
1338 SETi( -(IV)product_low );
1340 } /* else drop to NVs below. */
1342 } /* product_middle too large */
1343 } /* ahigh && bhigh */
1348 NV right = SvNV_nomg(svr);
1349 NV left = SvNV_nomg(svl);
1351 SETn( left * right );
1358 dVAR; dSP; dATARGET; SV *svl, *svr;
1359 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1362 /* Only try to do UV divide first
1363 if ((SLOPPYDIVIDE is true) or
1364 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1366 The assumption is that it is better to use floating point divide
1367 whenever possible, only doing integer divide first if we can't be sure.
1368 If NV_PRESERVES_UV is true then we know at compile time that no UV
1369 can be too large to preserve, so don't need to compile the code to
1370 test the size of UVs. */
1373 # define PERL_TRY_UV_DIVIDE
1374 /* ensure that 20./5. == 4. */
1376 # ifdef PERL_PRESERVE_IVUV
1377 # ifndef NV_PRESERVES_UV
1378 # define PERL_TRY_UV_DIVIDE
1383 #ifdef PERL_TRY_UV_DIVIDE
1384 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1385 bool left_non_neg = SvUOK(svl);
1386 bool right_non_neg = SvUOK(svr);
1390 if (right_non_neg) {
1394 const IV biv = SvIVX(svr);
1397 right_non_neg = TRUE; /* effectively it's a UV now */
1403 /* historically undef()/0 gives a "Use of uninitialized value"
1404 warning before dieing, hence this test goes here.
1405 If it were immediately before the second SvIV_please, then
1406 DIE() would be invoked before left was even inspected, so
1407 no inspection would give no warning. */
1409 DIE(aTHX_ "Illegal division by zero");
1415 const IV aiv = SvIVX(svl);
1418 left_non_neg = TRUE; /* effectively it's a UV now */
1427 /* For sloppy divide we always attempt integer division. */
1429 /* Otherwise we only attempt it if either or both operands
1430 would not be preserved by an NV. If both fit in NVs
1431 we fall through to the NV divide code below. However,
1432 as left >= right to ensure integer result here, we know that
1433 we can skip the test on the right operand - right big
1434 enough not to be preserved can't get here unless left is
1437 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1440 /* Integer division can't overflow, but it can be imprecise. */
1441 const UV result = left / right;
1442 if (result * right == left) {
1443 SP--; /* result is valid */
1444 if (left_non_neg == right_non_neg) {
1445 /* signs identical, result is positive. */
1449 /* 2s complement assumption */
1450 if (result <= (UV)IV_MIN)
1451 SETi( -(IV)result );
1453 /* It's exact but too negative for IV. */
1454 SETn( -(NV)result );
1457 } /* tried integer divide but it was not an integer result */
1458 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1459 } /* one operand wasn't SvIOK */
1460 #endif /* PERL_TRY_UV_DIVIDE */
1462 NV right = SvNV_nomg(svr);
1463 NV left = SvNV_nomg(svl);
1464 (void)POPs;(void)POPs;
1465 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1466 if (! Perl_isnan(right) && right == 0.0)
1470 DIE(aTHX_ "Illegal division by zero");
1471 PUSHn( left / right );
1478 dVAR; dSP; dATARGET;
1479 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1483 bool left_neg = FALSE;
1484 bool right_neg = FALSE;
1485 bool use_double = FALSE;
1486 bool dright_valid = FALSE;
1489 SV * const svr = TOPs;
1490 SV * const svl = TOPm1s;
1491 if (SvIV_please_nomg(svr)) {
1492 right_neg = !SvUOK(svr);
1496 const IV biv = SvIVX(svr);
1499 right_neg = FALSE; /* effectively it's a UV now */
1506 dright = SvNV_nomg(svr);
1507 right_neg = dright < 0;
1510 if (dright < UV_MAX_P1) {
1511 right = U_V(dright);
1512 dright_valid = TRUE; /* In case we need to use double below. */
1518 /* At this point use_double is only true if right is out of range for
1519 a UV. In range NV has been rounded down to nearest UV and
1520 use_double false. */
1521 if (!use_double && SvIV_please_nomg(svl)) {
1522 left_neg = !SvUOK(svl);
1526 const IV aiv = SvIVX(svl);
1529 left_neg = FALSE; /* effectively it's a UV now */
1536 dleft = SvNV_nomg(svl);
1537 left_neg = dleft < 0;
1541 /* This should be exactly the 5.6 behaviour - if left and right are
1542 both in range for UV then use U_V() rather than floor. */
1544 if (dleft < UV_MAX_P1) {
1545 /* right was in range, so is dleft, so use UVs not double.
1549 /* left is out of range for UV, right was in range, so promote
1550 right (back) to double. */
1552 /* The +0.5 is used in 5.6 even though it is not strictly
1553 consistent with the implicit +0 floor in the U_V()
1554 inside the #if 1. */
1555 dleft = Perl_floor(dleft + 0.5);
1558 dright = Perl_floor(dright + 0.5);
1569 DIE(aTHX_ "Illegal modulus zero");
1571 dans = Perl_fmod(dleft, dright);
1572 if ((left_neg != right_neg) && dans)
1573 dans = dright - dans;
1576 sv_setnv(TARG, dans);
1582 DIE(aTHX_ "Illegal modulus zero");
1585 if ((left_neg != right_neg) && ans)
1588 /* XXX may warn: unary minus operator applied to unsigned type */
1589 /* could change -foo to be (~foo)+1 instead */
1590 if (ans <= ~((UV)IV_MAX)+1)
1591 sv_setiv(TARG, ~ans+1);
1593 sv_setnv(TARG, -(NV)ans);
1596 sv_setuv(TARG, ans);
1605 dVAR; dSP; dATARGET;
1609 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1610 /* TODO: think of some way of doing list-repeat overloading ??? */
1615 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1621 const UV uv = SvUV_nomg(sv);
1623 count = IV_MAX; /* The best we can do? */
1627 const IV iv = SvIV_nomg(sv);
1634 else if (SvNOKp(sv)) {
1635 const NV nv = SvNV_nomg(sv);
1642 count = SvIV_nomg(sv);
1644 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1646 static const char oom_list_extend[] = "Out of memory during list extend";
1647 const I32 items = SP - MARK;
1648 const I32 max = items * count;
1650 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1651 /* Did the max computation overflow? */
1652 if (items > 0 && max > 0 && (max < items || max < count))
1653 Perl_croak(aTHX_ oom_list_extend);
1658 /* This code was intended to fix 20010809.028:
1661 for (($x =~ /./g) x 2) {
1662 print chop; # "abcdabcd" expected as output.
1665 * but that change (#11635) broke this code:
1667 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1669 * I can't think of a better fix that doesn't introduce
1670 * an efficiency hit by copying the SVs. The stack isn't
1671 * refcounted, and mortalisation obviously doesn't
1672 * Do The Right Thing when the stack has more than
1673 * one pointer to the same mortal value.
1677 *SP = sv_2mortal(newSVsv(*SP));
1687 repeatcpy((char*)(MARK + items), (char*)MARK,
1688 items * sizeof(const SV *), count - 1);
1691 else if (count <= 0)
1694 else { /* Note: mark already snarfed by pp_list */
1695 SV * const tmpstr = POPs;
1698 static const char oom_string_extend[] =
1699 "Out of memory during string extend";
1702 sv_setsv_nomg(TARG, tmpstr);
1703 SvPV_force_nomg(TARG, len);
1704 isutf = DO_UTF8(TARG);
1709 const STRLEN max = (UV)count * len;
1710 if (len > MEM_SIZE_MAX / count)
1711 Perl_croak(aTHX_ oom_string_extend);
1712 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1713 SvGROW(TARG, max + 1);
1714 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1715 SvCUR_set(TARG, SvCUR(TARG) * count);
1717 *SvEND(TARG) = '\0';
1720 (void)SvPOK_only_UTF8(TARG);
1722 (void)SvPOK_only(TARG);
1724 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1725 /* The parser saw this as a list repeat, and there
1726 are probably several items on the stack. But we're
1727 in scalar context, and there's no pp_list to save us
1728 now. So drop the rest of the items -- robin@kitsite.com
1740 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1741 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1744 useleft = USE_LEFT(svl);
1745 #ifdef PERL_PRESERVE_IVUV
1746 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1747 "bad things" happen if you rely on signed integers wrapping. */
1748 if (SvIV_please_nomg(svr)) {
1749 /* Unless the left argument is integer in range we are going to have to
1750 use NV maths. Hence only attempt to coerce the right argument if
1751 we know the left is integer. */
1758 a_valid = auvok = 1;
1759 /* left operand is undef, treat as zero. */
1761 /* Left operand is defined, so is it IV? */
1762 if (SvIV_please_nomg(svl)) {
1763 if ((auvok = SvUOK(svl)))
1766 const IV aiv = SvIVX(svl);
1769 auvok = 1; /* Now acting as a sign flag. */
1770 } else { /* 2s complement assumption for IV_MIN */
1778 bool result_good = 0;
1781 bool buvok = SvUOK(svr);
1786 const IV biv = SvIVX(svr);
1793 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1794 else "IV" now, independent of how it came in.
1795 if a, b represents positive, A, B negative, a maps to -A etc
1800 all UV maths. negate result if A negative.
1801 subtract if signs same, add if signs differ. */
1803 if (auvok ^ buvok) {
1812 /* Must get smaller */
1817 if (result <= buv) {
1818 /* result really should be -(auv-buv). as its negation
1819 of true value, need to swap our result flag */
1831 if (result <= (UV)IV_MIN)
1832 SETi( -(IV)result );
1834 /* result valid, but out of range for IV. */
1835 SETn( -(NV)result );
1839 } /* Overflow, drop through to NVs. */
1844 NV value = SvNV_nomg(svr);
1848 /* left operand is undef, treat as zero - value */
1852 SETn( SvNV_nomg(svl) - value );
1859 dVAR; dSP; dATARGET; SV *svl, *svr;
1860 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1864 const IV shift = SvIV_nomg(svr);
1865 if (PL_op->op_private & HINT_INTEGER) {
1866 const IV i = SvIV_nomg(svl);
1870 const UV u = SvUV_nomg(svl);
1879 dVAR; dSP; dATARGET; SV *svl, *svr;
1880 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1884 const IV shift = SvIV_nomg(svr);
1885 if (PL_op->op_private & HINT_INTEGER) {
1886 const IV i = SvIV_nomg(svl);
1890 const UV u = SvUV_nomg(svl);
1902 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1906 (SvIOK_notUV(left) && SvIOK_notUV(right))
1907 ? (SvIVX(left) < SvIVX(right))
1908 : (do_ncmp(left, right) == -1)
1918 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1922 (SvIOK_notUV(left) && SvIOK_notUV(right))
1923 ? (SvIVX(left) > SvIVX(right))
1924 : (do_ncmp(left, right) == 1)
1934 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1938 (SvIOK_notUV(left) && SvIOK_notUV(right))
1939 ? (SvIVX(left) <= SvIVX(right))
1940 : (do_ncmp(left, right) <= 0)
1950 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1954 (SvIOK_notUV(left) && SvIOK_notUV(right))
1955 ? (SvIVX(left) >= SvIVX(right))
1956 : ( (do_ncmp(left, right) & 2) == 0)
1966 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1970 (SvIOK_notUV(left) && SvIOK_notUV(right))
1971 ? (SvIVX(left) != SvIVX(right))
1972 : (do_ncmp(left, right) != 0)
1977 /* compare left and right SVs. Returns:
1981 * 2: left or right was a NaN
1984 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1988 PERL_ARGS_ASSERT_DO_NCMP;
1989 #ifdef PERL_PRESERVE_IVUV
1990 /* Fortunately it seems NaN isn't IOK */
1991 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1993 const IV leftiv = SvIVX(left);
1994 if (!SvUOK(right)) {
1995 /* ## IV <=> IV ## */
1996 const IV rightiv = SvIVX(right);
1997 return (leftiv > rightiv) - (leftiv < rightiv);
1999 /* ## IV <=> UV ## */
2001 /* As (b) is a UV, it's >=0, so it must be < */
2004 const UV rightuv = SvUVX(right);
2005 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2010 /* ## UV <=> UV ## */
2011 const UV leftuv = SvUVX(left);
2012 const UV rightuv = SvUVX(right);
2013 return (leftuv > rightuv) - (leftuv < rightuv);
2015 /* ## UV <=> IV ## */
2017 const IV rightiv = SvIVX(right);
2019 /* As (a) is a UV, it's >=0, so it cannot be < */
2022 const UV leftuv = SvUVX(left);
2023 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2026 assert(0); /* NOTREACHED */
2030 NV const rnv = SvNV_nomg(right);
2031 NV const lnv = SvNV_nomg(left);
2033 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2034 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2037 return (lnv > rnv) - (lnv < rnv);
2056 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2059 value = do_ncmp(left, right);
2074 int amg_type = sle_amg;
2078 switch (PL_op->op_type) {
2097 tryAMAGICbin_MG(amg_type, AMGf_set);
2100 const int cmp = (IN_LOCALE_RUNTIME
2101 ? sv_cmp_locale_flags(left, right, 0)
2102 : sv_cmp_flags(left, right, 0));
2103 SETs(boolSV(cmp * multiplier < rhs));
2111 tryAMAGICbin_MG(seq_amg, AMGf_set);
2114 SETs(boolSV(sv_eq_flags(left, right, 0)));
2122 tryAMAGICbin_MG(sne_amg, AMGf_set);
2125 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2133 tryAMAGICbin_MG(scmp_amg, 0);
2136 const int cmp = (IN_LOCALE_RUNTIME
2137 ? sv_cmp_locale_flags(left, right, 0)
2138 : sv_cmp_flags(left, right, 0));
2146 dVAR; dSP; dATARGET;
2147 tryAMAGICbin_MG(band_amg, AMGf_assign);
2150 if (SvNIOKp(left) || SvNIOKp(right)) {
2151 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2152 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2153 if (PL_op->op_private & HINT_INTEGER) {
2154 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2158 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2161 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2162 if (right_ro_nonnum) SvNIOK_off(right);
2165 do_vop(PL_op->op_type, TARG, left, right);
2174 dVAR; dSP; dATARGET;
2175 const int op_type = PL_op->op_type;
2177 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2180 if (SvNIOKp(left) || SvNIOKp(right)) {
2181 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2182 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2183 if (PL_op->op_private & HINT_INTEGER) {
2184 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2185 const IV r = SvIV_nomg(right);
2186 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2190 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2191 const UV r = SvUV_nomg(right);
2192 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2195 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2196 if (right_ro_nonnum) SvNIOK_off(right);
2199 do_vop(op_type, TARG, left, right);
2206 PERL_STATIC_INLINE bool
2207 S_negate_string(pTHX)
2212 SV * const sv = TOPs;
2213 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2215 s = SvPV_nomg_const(sv, len);
2216 if (isIDFIRST(*s)) {
2217 sv_setpvs(TARG, "-");
2220 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2221 sv_setsv_nomg(TARG, sv);
2222 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2232 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2233 if (S_negate_string(aTHX)) return NORMAL;
2235 SV * const sv = TOPs;
2238 /* It's publicly an integer */
2241 if (SvIVX(sv) == IV_MIN) {
2242 /* 2s complement assumption. */
2243 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2246 else if (SvUVX(sv) <= IV_MAX) {
2251 else if (SvIVX(sv) != IV_MIN) {
2255 #ifdef PERL_PRESERVE_IVUV
2262 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2263 SETn(-SvNV_nomg(sv));
2264 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2265 goto oops_its_an_int;
2267 SETn(-SvNV_nomg(sv));
2275 tryAMAGICun_MG(not_amg, AMGf_set);
2276 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2283 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2287 if (PL_op->op_private & HINT_INTEGER) {
2288 const IV i = ~SvIV_nomg(sv);
2292 const UV u = ~SvUV_nomg(sv);
2301 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2302 sv_setsv_nomg(TARG, sv);
2303 tmps = (U8*)SvPV_force_nomg(TARG, len);
2306 /* Calculate exact length, let's not estimate. */
2311 U8 * const send = tmps + len;
2312 U8 * const origtmps = tmps;
2313 const UV utf8flags = UTF8_ALLOW_ANYUV;
2315 while (tmps < send) {
2316 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2318 targlen += UNISKIP(~c);
2324 /* Now rewind strings and write them. */
2331 Newx(result, targlen + 1, U8);
2333 while (tmps < send) {
2334 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2336 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2339 sv_usepvn_flags(TARG, (char*)result, targlen,
2340 SV_HAS_TRAILING_NUL);
2347 Newx(result, nchar + 1, U8);
2349 while (tmps < send) {
2350 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2355 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2364 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2367 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2372 for ( ; anum > 0; anum--, tmps++)
2380 /* integer versions of some of the above */
2384 dVAR; dSP; dATARGET;
2385 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2388 SETi( left * right );
2396 dVAR; dSP; dATARGET;
2397 tryAMAGICbin_MG(div_amg, AMGf_assign);
2400 IV value = SvIV_nomg(right);
2402 DIE(aTHX_ "Illegal division by zero");
2403 num = SvIV_nomg(left);
2405 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2409 value = num / value;
2415 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2422 /* This is the vanilla old i_modulo. */
2423 dVAR; dSP; dATARGET;
2424 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2428 DIE(aTHX_ "Illegal modulus zero");
2429 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2433 SETi( left % right );
2438 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2443 /* This is the i_modulo with the workaround for the _moddi3 bug
2444 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2445 * See below for pp_i_modulo. */
2446 dVAR; dSP; dATARGET;
2447 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2451 DIE(aTHX_ "Illegal modulus zero");
2452 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2456 SETi( left % PERL_ABS(right) );
2463 dVAR; dSP; dATARGET;
2464 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2468 DIE(aTHX_ "Illegal modulus zero");
2469 /* The assumption is to use hereafter the old vanilla version... */
2471 PL_ppaddr[OP_I_MODULO] =
2473 /* .. but if we have glibc, we might have a buggy _moddi3
2474 * (at least glicb 2.2.5 is known to have this bug), in other
2475 * words our integer modulus with negative quad as the second
2476 * argument might be broken. Test for this and re-patch the
2477 * opcode dispatch table if that is the case, remembering to
2478 * also apply the workaround so that this first round works
2479 * right, too. See [perl #9402] for more information. */
2483 /* Cannot do this check with inlined IV constants since
2484 * that seems to work correctly even with the buggy glibc. */
2486 /* Yikes, we have the bug.
2487 * Patch in the workaround version. */
2489 PL_ppaddr[OP_I_MODULO] =
2490 &Perl_pp_i_modulo_1;
2491 /* Make certain we work right this time, too. */
2492 right = PERL_ABS(right);
2495 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2499 SETi( left % right );
2507 dVAR; dSP; dATARGET;
2508 tryAMAGICbin_MG(add_amg, AMGf_assign);
2510 dPOPTOPiirl_ul_nomg;
2511 SETi( left + right );
2518 dVAR; dSP; dATARGET;
2519 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2521 dPOPTOPiirl_ul_nomg;
2522 SETi( left - right );
2530 tryAMAGICbin_MG(lt_amg, AMGf_set);
2533 SETs(boolSV(left < right));
2541 tryAMAGICbin_MG(gt_amg, AMGf_set);
2544 SETs(boolSV(left > right));
2552 tryAMAGICbin_MG(le_amg, AMGf_set);
2555 SETs(boolSV(left <= right));
2563 tryAMAGICbin_MG(ge_amg, AMGf_set);
2566 SETs(boolSV(left >= right));
2574 tryAMAGICbin_MG(eq_amg, AMGf_set);
2577 SETs(boolSV(left == right));
2585 tryAMAGICbin_MG(ne_amg, AMGf_set);
2588 SETs(boolSV(left != right));
2596 tryAMAGICbin_MG(ncmp_amg, 0);
2603 else if (left < right)
2615 tryAMAGICun_MG(neg_amg, 0);
2616 if (S_negate_string(aTHX)) return NORMAL;
2618 SV * const sv = TOPs;
2619 IV const i = SvIV_nomg(sv);
2625 /* High falutin' math. */
2630 tryAMAGICbin_MG(atan2_amg, 0);
2633 SETn(Perl_atan2(left, right));
2641 int amg_type = sin_amg;
2642 const char *neg_report = NULL;
2643 NV (*func)(NV) = Perl_sin;
2644 const int op_type = PL_op->op_type;
2661 amg_type = sqrt_amg;
2663 neg_report = "sqrt";
2668 tryAMAGICun_MG(amg_type, 0);
2670 SV * const arg = POPs;
2671 const NV value = SvNV_nomg(arg);
2673 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2674 SET_NUMERIC_STANDARD();
2675 /* diag_listed_as: Can't take log of %g */
2676 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2679 XPUSHn(func(value));
2684 /* Support Configure command-line overrides for rand() functions.
2685 After 5.005, perhaps we should replace this by Configure support
2686 for drand48(), random(), or rand(). For 5.005, though, maintain
2687 compatibility by calling rand() but allow the user to override it.
2688 See INSTALL for details. --Andy Dougherty 15 July 1998
2690 /* Now it's after 5.005, and Configure supports drand48() and random(),
2691 in addition to rand(). So the overrides should not be needed any more.
2692 --Jarkko Hietaniemi 27 September 1998
2695 #ifndef HAS_DRAND48_PROTO
2696 extern double drand48 (void);
2706 value = 1.0; (void)POPs;
2712 if (!PL_srand_called) {
2713 (void)seedDrand01((Rand_seed_t)seed());
2714 PL_srand_called = TRUE;
2726 if (MAXARG >= 1 && (TOPs || POPs)) {
2733 pv = SvPV(top, len);
2734 flags = grok_number(pv, len, &anum);
2736 if (!(flags & IS_NUMBER_IN_UV)) {
2737 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2738 "Integer overflow in srand");
2746 (void)seedDrand01((Rand_seed_t)anum);
2747 PL_srand_called = TRUE;
2751 /* Historically srand always returned true. We can avoid breaking
2753 sv_setpvs(TARG, "0 but true");
2762 tryAMAGICun_MG(int_amg, AMGf_numeric);
2764 SV * const sv = TOPs;
2765 const IV iv = SvIV_nomg(sv);
2766 /* XXX it's arguable that compiler casting to IV might be subtly
2767 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2768 else preferring IV has introduced a subtle behaviour change bug. OTOH
2769 relying on floating point to be accurate is a bug. */
2774 else if (SvIOK(sv)) {
2776 SETu(SvUV_nomg(sv));
2781 const NV value = SvNV_nomg(sv);
2783 if (value < (NV)UV_MAX + 0.5) {
2786 SETn(Perl_floor(value));
2790 if (value > (NV)IV_MIN - 0.5) {
2793 SETn(Perl_ceil(value));
2804 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2806 SV * const sv = TOPs;
2807 /* This will cache the NV value if string isn't actually integer */
2808 const IV iv = SvIV_nomg(sv);
2813 else if (SvIOK(sv)) {
2814 /* IVX is precise */
2816 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2824 /* 2s complement assumption. Also, not really needed as
2825 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2831 const NV value = SvNV_nomg(sv);
2845 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2849 SV* const sv = POPs;
2851 tmps = (SvPV_const(sv, len));
2853 /* If Unicode, try to downgrade
2854 * If not possible, croak. */
2855 SV* const tsv = sv_2mortal(newSVsv(sv));
2858 sv_utf8_downgrade(tsv, FALSE);
2859 tmps = SvPV_const(tsv, len);
2861 if (PL_op->op_type == OP_HEX)
2864 while (*tmps && len && isSPACE(*tmps))
2868 if (*tmps == 'x' || *tmps == 'X') {
2870 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2872 else if (*tmps == 'b' || *tmps == 'B')
2873 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2875 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2877 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2891 SV * const sv = TOPs;
2893 if (SvGAMAGIC(sv)) {
2894 /* For an overloaded or magic scalar, we can't know in advance if
2895 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2896 it likes to cache the length. Maybe that should be a documented
2901 = sv_2pv_flags(sv, &len,
2902 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2905 if (!SvPADTMP(TARG)) {
2906 sv_setsv(TARG, &PL_sv_undef);
2911 else if (DO_UTF8(sv)) {
2912 SETi(utf8_length((U8*)p, (U8*)p + len));
2916 } else if (SvOK(sv)) {
2917 /* Neither magic nor overloaded. */
2919 SETi(sv_len_utf8(sv));
2923 if (!SvPADTMP(TARG)) {
2924 sv_setsv_nomg(TARG, &PL_sv_undef);
2932 /* Returns false if substring is completely outside original string.
2933 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2934 always be true for an explicit 0.
2937 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2938 bool pos1_is_uv, IV len_iv,
2939 bool len_is_uv, STRLEN *posp,
2945 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2947 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2948 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2951 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2954 if (len_iv || len_is_uv) {
2955 if (!len_is_uv && len_iv < 0) {
2956 pos2_iv = curlen + len_iv;
2958 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2961 } else { /* len_iv >= 0 */
2962 if (!pos1_is_uv && pos1_iv < 0) {
2963 pos2_iv = pos1_iv + len_iv;
2964 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2966 if ((UV)len_iv > curlen-(UV)pos1_iv)
2969 pos2_iv = pos1_iv+len_iv;
2979 if (!pos2_is_uv && pos2_iv < 0) {
2980 if (!pos1_is_uv && pos1_iv < 0)
2984 else if (!pos1_is_uv && pos1_iv < 0)
2987 if ((UV)pos2_iv < (UV)pos1_iv)
2989 if ((UV)pos2_iv > curlen)
2992 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2993 *posp = (STRLEN)( (UV)pos1_iv );
2994 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3011 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3012 const bool rvalue = (GIMME_V != G_VOID);
3015 const char *repl = NULL;
3017 int num_args = PL_op->op_private & 7;
3018 bool repl_need_utf8_upgrade = FALSE;
3019 bool repl_is_utf8 = FALSE;
3023 if(!(repl_sv = POPs)) num_args--;
3025 if ((len_sv = POPs)) {
3026 len_iv = SvIV(len_sv);
3027 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3032 pos1_iv = SvIV(pos_sv);
3033 pos1_is_uv = SvIOK_UV(pos_sv);
3035 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3041 repl = SvPV_const(repl_sv, repl_len);
3042 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
3045 sv_utf8_upgrade(sv);
3047 else if (DO_UTF8(sv))
3048 repl_need_utf8_upgrade = TRUE;
3052 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3053 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3055 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3057 pos1_is_uv || pos1_iv >= 0
3058 ? (STRLEN)(UV)pos1_iv
3059 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3061 len_is_uv || len_iv > 0
3062 ? (STRLEN)(UV)len_iv
3063 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3066 PUSHs(ret); /* avoid SvSETMAGIC here */
3069 tmps = SvPV_const(sv, curlen);
3071 utf8_curlen = sv_len_utf8_nomg(sv);
3072 if (utf8_curlen == curlen)
3075 curlen = utf8_curlen;
3081 STRLEN pos, len, byte_len, byte_pos;
3083 if (!translate_substr_offsets(
3084 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3088 byte_pos = utf8_curlen
3089 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3094 SvTAINTED_off(TARG); /* decontaminate */
3095 SvUTF8_off(TARG); /* decontaminate */
3096 sv_setpvn(TARG, tmps, byte_len);
3097 #ifdef USE_LOCALE_COLLATE
3098 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3105 SV* repl_sv_copy = NULL;
3107 if (repl_need_utf8_upgrade) {
3108 repl_sv_copy = newSVsv(repl_sv);
3109 sv_utf8_upgrade(repl_sv_copy);
3110 repl = SvPV_const(repl_sv_copy, repl_len);
3111 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3114 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3115 "Attempt to use reference as lvalue in substr"
3119 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3122 SvREFCNT_dec(repl_sv_copy);
3134 Perl_croak(aTHX_ "substr outside of string");
3135 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3142 const IV size = POPi;
3143 const IV offset = POPi;
3144 SV * const src = POPs;
3145 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3148 if (lvalue) { /* it's an lvalue! */
3149 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3150 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3152 LvTARG(ret) = SvREFCNT_inc_simple(src);
3153 LvTARGOFF(ret) = offset;
3154 LvTARGLEN(ret) = size;
3158 SvTAINTED_off(TARG); /* decontaminate */
3162 sv_setuv(ret, do_vecget(src, offset, size));
3178 const char *little_p;
3181 const bool is_index = PL_op->op_type == OP_INDEX;
3182 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3188 big_p = SvPV_const(big, biglen);
3189 little_p = SvPV_const(little, llen);
3191 big_utf8 = DO_UTF8(big);
3192 little_utf8 = DO_UTF8(little);
3193 if (big_utf8 ^ little_utf8) {
3194 /* One needs to be upgraded. */
3195 if (little_utf8 && !PL_encoding) {
3196 /* Well, maybe instead we might be able to downgrade the small
3198 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3201 /* If the large string is ISO-8859-1, and it's not possible to
3202 convert the small string to ISO-8859-1, then there is no
3203 way that it could be found anywhere by index. */
3208 /* At this point, pv is a malloc()ed string. So donate it to temp
3209 to ensure it will get free()d */
3210 little = temp = newSV(0);
3211 sv_usepvn(temp, pv, llen);
3212 little_p = SvPVX(little);
3215 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3218 sv_recode_to_utf8(temp, PL_encoding);
3220 sv_utf8_upgrade(temp);
3225 big_p = SvPV_const(big, biglen);
3228 little_p = SvPV_const(little, llen);
3232 if (SvGAMAGIC(big)) {
3233 /* Life just becomes a lot easier if I use a temporary here.
3234 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3235 will trigger magic and overloading again, as will fbm_instr()
3237 big = newSVpvn_flags(big_p, biglen,
3238 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3241 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3242 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3243 warn on undef, and we've already triggered a warning with the
3244 SvPV_const some lines above. We can't remove that, as we need to
3245 call some SvPV to trigger overloading early and find out if the
3247 This is all getting to messy. The API isn't quite clean enough,
3248 because data access has side effects.
3250 little = newSVpvn_flags(little_p, llen,
3251 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3252 little_p = SvPVX(little);
3256 offset = is_index ? 0 : biglen;
3258 if (big_utf8 && offset > 0)
3259 sv_pos_u2b(big, &offset, 0);
3265 else if (offset > (I32)biglen)
3267 if (!(little_p = is_index
3268 ? fbm_instr((unsigned char*)big_p + offset,
3269 (unsigned char*)big_p + biglen, little, 0)
3270 : rninstr(big_p, big_p + offset,
3271 little_p, little_p + llen)))
3274 retval = little_p - big_p;
3275 if (retval > 0 && big_utf8)
3276 sv_pos_b2u(big, &retval);
3286 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3287 SvTAINTED_off(TARG);
3288 do_sprintf(TARG, SP-MARK, MARK+1);
3289 TAINT_IF(SvTAINTED(TARG));
3301 const U8 *s = (U8*)SvPV_const(argsv, len);
3303 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3304 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3305 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3309 XPUSHu(DO_UTF8(argsv) ?
3310 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3324 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3325 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3327 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3328 && SvNV_nomg(top) < 0.0))) {
3329 if (ckWARN(WARN_UTF8)) {
3330 if (SvGMAGICAL(top)) {
3331 SV *top2 = sv_newmortal();
3332 sv_setsv_nomg(top2, top);
3335 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3336 "Invalid negative number (%"SVf") in chr", top);
3338 value = UNICODE_REPLACEMENT;
3340 value = SvUV_nomg(top);
3343 SvUPGRADE(TARG,SVt_PV);
3345 if (value > 255 && !IN_BYTES) {
3346 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3347 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3348 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3350 (void)SvPOK_only(TARG);
3359 *tmps++ = (char)value;
3361 (void)SvPOK_only(TARG);
3363 if (PL_encoding && !IN_BYTES) {
3364 sv_recode_to_utf8(TARG, PL_encoding);
3366 if (SvCUR(TARG) == 0
3367 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3368 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3373 *tmps++ = (char)value;
3389 const char *tmps = SvPV_const(left, len);
3391 if (DO_UTF8(left)) {
3392 /* If Unicode, try to downgrade.
3393 * If not possible, croak.
3394 * Yes, we made this up. */
3395 SV* const tsv = sv_2mortal(newSVsv(left));
3398 sv_utf8_downgrade(tsv, FALSE);
3399 tmps = SvPV_const(tsv, len);
3401 # ifdef USE_ITHREADS
3403 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3404 /* This should be threadsafe because in ithreads there is only
3405 * one thread per interpreter. If this would not be true,
3406 * we would need a mutex to protect this malloc. */
3407 PL_reentrant_buffer->_crypt_struct_buffer =
3408 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3409 #if defined(__GLIBC__) || defined(__EMX__)
3410 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3411 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3412 /* work around glibc-2.2.5 bug */
3413 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3417 # endif /* HAS_CRYPT_R */
3418 # endif /* USE_ITHREADS */
3420 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3422 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3428 "The crypt() function is unimplemented due to excessive paranoia.");
3432 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3433 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3435 /* Generates code to store a unicode codepoint c that is known to occupy
3436 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3437 * and p is advanced to point to the next available byte after the two bytes */
3438 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3440 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3441 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3446 /* Actually is both lcfirst() and ucfirst(). Only the first character
3447 * changes. This means that possibly we can change in-place, ie., just
3448 * take the source and change that one character and store it back, but not
3449 * if read-only etc, or if the length changes */
3454 STRLEN slen; /* slen is the byte length of the whole SV. */
3457 bool inplace; /* ? Convert first char only, in-place */
3458 bool doing_utf8 = FALSE; /* ? using utf8 */
3459 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3460 const int op_type = PL_op->op_type;
3463 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3464 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3465 * stored as UTF-8 at s. */
3466 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3467 * lowercased) character stored in tmpbuf. May be either
3468 * UTF-8 or not, but in either case is the number of bytes */
3469 bool tainted = FALSE;
3473 s = (const U8*)SvPV_nomg_const(source, slen);
3475 if (ckWARN(WARN_UNINITIALIZED))
3476 report_uninit(source);
3481 /* We may be able to get away with changing only the first character, in
3482 * place, but not if read-only, etc. Later we may discover more reasons to
3483 * not convert in-place. */
3484 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3486 /* First calculate what the changed first character should be. This affects
3487 * whether we can just swap it out, leaving the rest of the string unchanged,
3488 * or even if have to convert the dest to UTF-8 when the source isn't */
3490 if (! slen) { /* If empty */
3491 need = 1; /* still need a trailing NUL */
3494 else if (DO_UTF8(source)) { /* Is the source utf8? */
3497 if (op_type == OP_UCFIRST) {
3498 _to_utf8_title_flags(s, tmpbuf, &tculen,
3499 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3502 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3503 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3506 /* we can't do in-place if the length changes. */
3507 if (ulen != tculen) inplace = FALSE;
3508 need = slen + 1 - ulen + tculen;
3510 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3511 * latin1 is treated as caseless. Note that a locale takes
3513 ulen = 1; /* Original character is 1 byte */
3514 tculen = 1; /* Most characters will require one byte, but this will
3515 * need to be overridden for the tricky ones */
3518 if (op_type == OP_LCFIRST) {
3520 /* lower case the first letter: no trickiness for any character */
3521 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3522 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3525 else if (IN_LOCALE_RUNTIME) {
3526 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3527 * have upper and title case different
3530 else if (! IN_UNI_8_BIT) {
3531 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3532 * on EBCDIC machines whatever the
3533 * native function does */
3535 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3536 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3538 assert(tculen == 2);
3540 /* If the result is an upper Latin1-range character, it can
3541 * still be represented in one byte, which is its ordinal */
3542 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3543 *tmpbuf = (U8) title_ord;
3547 /* Otherwise it became more than one ASCII character (in
3548 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3549 * beyond Latin1, so the number of bytes changed, so can't
3550 * replace just the first character in place. */
3553 /* If the result won't fit in a byte, the entire result will
3554 * have to be in UTF-8. Assume worst case sizing in
3555 * conversion. (all latin1 characters occupy at most two bytes
3557 if (title_ord > 255) {
3559 convert_source_to_utf8 = TRUE;
3560 need = slen * 2 + 1;
3562 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3563 * (both) characters whose title case is above 255 is
3567 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3568 need = slen + 1 + 1;
3572 } /* End of use Unicode (Latin1) semantics */
3573 } /* End of changing the case of the first character */
3575 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3576 * generate the result */
3579 /* We can convert in place. This means we change just the first
3580 * character without disturbing the rest; no need to grow */
3582 s = d = (U8*)SvPV_force_nomg(source, slen);
3588 /* Here, we can't convert in place; we earlier calculated how much
3589 * space we will need, so grow to accommodate that */
3590 SvUPGRADE(dest, SVt_PV);
3591 d = (U8*)SvGROW(dest, need);
3592 (void)SvPOK_only(dest);
3599 if (! convert_source_to_utf8) {
3601 /* Here both source and dest are in UTF-8, but have to create
3602 * the entire output. We initialize the result to be the
3603 * title/lower cased first character, and then append the rest
3605 sv_setpvn(dest, (char*)tmpbuf, tculen);
3607 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3611 const U8 *const send = s + slen;
3613 /* Here the dest needs to be in UTF-8, but the source isn't,
3614 * except we earlier UTF-8'd the first character of the source
3615 * into tmpbuf. First put that into dest, and then append the
3616 * rest of the source, converting it to UTF-8 as we go. */
3618 /* Assert tculen is 2 here because the only two characters that
3619 * get to this part of the code have 2-byte UTF-8 equivalents */
3621 *d++ = *(tmpbuf + 1);
3622 s++; /* We have just processed the 1st char */
3624 for (; s < send; s++) {
3625 d = uvchr_to_utf8(d, *s);
3628 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3632 else { /* in-place UTF-8. Just overwrite the first character */
3633 Copy(tmpbuf, d, tculen, U8);
3634 SvCUR_set(dest, need - 1);
3642 else { /* Neither source nor dest are in or need to be UTF-8 */
3644 if (IN_LOCALE_RUNTIME) {
3648 if (inplace) { /* in-place, only need to change the 1st char */
3651 else { /* Not in-place */
3653 /* Copy the case-changed character(s) from tmpbuf */
3654 Copy(tmpbuf, d, tculen, U8);
3655 d += tculen - 1; /* Code below expects d to point to final
3656 * character stored */
3659 else { /* empty source */
3660 /* See bug #39028: Don't taint if empty */
3664 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3665 * the destination to retain that flag */
3669 if (!inplace) { /* Finish the rest of the string, unchanged */
3670 /* This will copy the trailing NUL */
3671 Copy(s + 1, d + 1, slen, U8);
3672 SvCUR_set(dest, need - 1);
3675 if (dest != source && SvTAINTED(source))
3681 /* There's so much setup/teardown code common between uc and lc, I wonder if
3682 it would be worth merging the two, and just having a switch outside each
3683 of the three tight loops. There is less and less commonality though */
3697 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3698 && SvTEMP(source) && !DO_UTF8(source)
3699 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3701 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3702 * make the loop tight, so we overwrite the source with the dest before
3703 * looking at it, and we need to look at the original source
3704 * afterwards. There would also need to be code added to handle
3705 * switching to not in-place in midstream if we run into characters
3706 * that change the length.
3709 s = d = (U8*)SvPV_force_nomg(source, len);
3716 /* The old implementation would copy source into TARG at this point.
3717 This had the side effect that if source was undef, TARG was now
3718 an undefined SV with PADTMP set, and they don't warn inside
3719 sv_2pv_flags(). However, we're now getting the PV direct from
3720 source, which doesn't have PADTMP set, so it would warn. Hence the
3724 s = (const U8*)SvPV_nomg_const(source, len);
3726 if (ckWARN(WARN_UNINITIALIZED))
3727 report_uninit(source);
3733 SvUPGRADE(dest, SVt_PV);
3734 d = (U8*)SvGROW(dest, min);
3735 (void)SvPOK_only(dest);
3740 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3741 to check DO_UTF8 again here. */
3743 if (DO_UTF8(source)) {
3744 const U8 *const send = s + len;
3745 U8 tmpbuf[UTF8_MAXBYTES+1];
3746 bool tainted = FALSE;
3748 /* All occurrences of these are to be moved to follow any other marks.
3749 * This is context-dependent. We may not be passed enough context to
3750 * move the iota subscript beyond all of them, but we do the best we can
3751 * with what we're given. The result is always better than if we
3752 * hadn't done this. And, the problem would only arise if we are
3753 * passed a character without all its combining marks, which would be
3754 * the caller's mistake. The information this is based on comes from a
3755 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3756 * itself) and so can't be checked properly to see if it ever gets
3757 * revised. But the likelihood of it changing is remote */
3758 bool in_iota_subscript = FALSE;
3764 if (in_iota_subscript && ! is_utf8_mark(s)) {
3766 /* A non-mark. Time to output the iota subscript */
3767 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3768 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3770 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3771 in_iota_subscript = FALSE;
3774 /* Then handle the current character. Get the changed case value
3775 * and copy it to the output buffer */
3778 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3779 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3780 if (uv == GREEK_CAPITAL_LETTER_IOTA
3781 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3783 in_iota_subscript = TRUE;
3786 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3787 /* If the eventually required minimum size outgrows the
3788 * available space, we need to grow. */
3789 const UV o = d - (U8*)SvPVX_const(dest);
3791 /* If someone uppercases one million U+03B0s we SvGROW()
3792 * one million times. Or we could try guessing how much to
3793 * allocate without allocating too much. Such is life.
3794 * See corresponding comment in lc code for another option
3797 d = (U8*)SvPVX(dest) + o;
3799 Copy(tmpbuf, d, ulen, U8);
3804 if (in_iota_subscript) {
3805 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3810 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3816 else { /* Not UTF-8 */
3818 const U8 *const send = s + len;
3820 /* Use locale casing if in locale; regular style if not treating
3821 * latin1 as having case; otherwise the latin1 casing. Do the
3822 * whole thing in a tight loop, for speed, */
3823 if (IN_LOCALE_RUNTIME) {
3826 for (; s < send; d++, s++)
3827 *d = toUPPER_LC(*s);
3829 else if (! IN_UNI_8_BIT) {
3830 for (; s < send; d++, s++) {
3835 for (; s < send; d++, s++) {
3836 *d = toUPPER_LATIN1_MOD(*s);
3837 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3839 /* The mainstream case is the tight loop above. To avoid
3840 * extra tests in that, all three characters that require
3841 * special handling are mapped by the MOD to the one tested
3843 * Use the source to distinguish between the three cases */
3845 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3847 /* uc() of this requires 2 characters, but they are
3848 * ASCII. If not enough room, grow the string */
3849 if (SvLEN(dest) < ++min) {
3850 const UV o = d - (U8*)SvPVX_const(dest);
3852 d = (U8*)SvPVX(dest) + o;
3854 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3855 continue; /* Back to the tight loop; still in ASCII */
3858 /* The other two special handling characters have their
3859 * upper cases outside the latin1 range, hence need to be
3860 * in UTF-8, so the whole result needs to be in UTF-8. So,
3861 * here we are somewhere in the middle of processing a
3862 * non-UTF-8 string, and realize that we will have to convert
3863 * the whole thing to UTF-8. What to do? There are
3864 * several possibilities. The simplest to code is to
3865 * convert what we have so far, set a flag, and continue on
3866 * in the loop. The flag would be tested each time through
3867 * the loop, and if set, the next character would be
3868 * converted to UTF-8 and stored. But, I (khw) didn't want
3869 * to slow down the mainstream case at all for this fairly
3870 * rare case, so I didn't want to add a test that didn't
3871 * absolutely have to be there in the loop, besides the
3872 * possibility that it would get too complicated for
3873 * optimizers to deal with. Another possibility is to just
3874 * give up, convert the source to UTF-8, and restart the
3875 * function that way. Another possibility is to convert
3876 * both what has already been processed and what is yet to
3877 * come separately to UTF-8, then jump into the loop that
3878 * handles UTF-8. But the most efficient time-wise of the
3879 * ones I could think of is what follows, and turned out to
3880 * not require much extra code. */
3882 /* Convert what we have so far into UTF-8, telling the
3883 * function that we know it should be converted, and to
3884 * allow extra space for what we haven't processed yet.
3885 * Assume the worst case space requirements for converting
3886 * what we haven't processed so far: that it will require
3887 * two bytes for each remaining source character, plus the
3888 * NUL at the end. This may cause the string pointer to
3889 * move, so re-find it. */
3891 len = d - (U8*)SvPVX_const(dest);
3892 SvCUR_set(dest, len);
3893 len = sv_utf8_upgrade_flags_grow(dest,
3894 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3896 d = (U8*)SvPVX(dest) + len;
3898 /* Now process the remainder of the source, converting to
3899 * upper and UTF-8. If a resulting byte is invariant in
3900 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3901 * append it to the output. */
3902 for (; s < send; s++) {
3903 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3907 /* Here have processed the whole source; no need to continue
3908 * with the outer loop. Each character has been converted
3909 * to upper case and converted to UTF-8 */
3912 } /* End of processing all latin1-style chars */
3913 } /* End of processing all chars */
3914 } /* End of source is not empty */
3916 if (source != dest) {
3917 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3918 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3920 } /* End of isn't utf8 */
3921 if (dest != source && SvTAINTED(source))
3940 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3941 && SvTEMP(source) && !DO_UTF8(source)) {
3943 /* We can convert in place, as lowercasing anything in the latin1 range
3944 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3946 s = d = (U8*)SvPV_force_nomg(source, len);
3953 /* The old implementation would copy source into TARG at this point.
3954 This had the side effect that if source was undef, TARG was now
3955 an undefined SV with PADTMP set, and they don't warn inside
3956 sv_2pv_flags(). However, we're now getting the PV direct from
3957 source, which doesn't have PADTMP set, so it would warn. Hence the
3961 s = (const U8*)SvPV_nomg_const(source, len);
3963 if (ckWARN(WARN_UNINITIALIZED))
3964 report_uninit(source);
3970 SvUPGRADE(dest, SVt_PV);
3971 d = (U8*)SvGROW(dest, min);
3972 (void)SvPOK_only(dest);
3977 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3978 to check DO_UTF8 again here. */
3980 if (DO_UTF8(source)) {
3981 const U8 *const send = s + len;
3982 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3983 bool tainted = FALSE;
3986 const STRLEN u = UTF8SKIP(s);
3989 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3990 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3992 /* Here is where we would do context-sensitive actions. See the
3993 * commit message for this comment for why there isn't any */
3995 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3997 /* If the eventually required minimum size outgrows the
3998 * available space, we need to grow. */
3999 const UV o = d - (U8*)SvPVX_const(dest);
4001 /* If someone lowercases one million U+0130s we SvGROW() one
4002 * million times. Or we could try guessing how much to
4003 * allocate without allocating too much. Such is life.
4004 * Another option would be to grow an extra byte or two more
4005 * each time we need to grow, which would cut down the million
4006 * to 500K, with little waste */
4008 d = (U8*)SvPVX(dest) + o;
4011 /* Copy the newly lowercased letter to the output buffer we're
4013 Copy(tmpbuf, d, ulen, U8);
4016 } /* End of looping through the source string */
4019 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4024 } else { /* Not utf8 */
4026 const U8 *const send = s + len;
4028 /* Use locale casing if in locale; regular style if not treating
4029 * latin1 as having case; otherwise the latin1 casing. Do the
4030 * whole thing in a tight loop, for speed, */
4031 if (IN_LOCALE_RUNTIME) {
4034 for (; s < send; d++, s++)
4035 *d = toLOWER_LC(*s);
4037 else if (! IN_UNI_8_BIT) {
4038 for (; s < send; d++, s++) {
4043 for (; s < send; d++, s++) {
4044 *d = toLOWER_LATIN1(*s);
4048 if (source != dest) {
4050 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4053 if (dest != source && SvTAINTED(source))
4062 SV * const sv = TOPs;
4064 const char *s = SvPV_const(sv,len);
4066 SvUTF8_off(TARG); /* decontaminate */
4069 SvUPGRADE(TARG, SVt_PV);
4070 SvGROW(TARG, (len * 2) + 1);
4074 STRLEN ulen = UTF8SKIP(s);
4075 bool to_quote = FALSE;
4077 if (UTF8_IS_INVARIANT(*s)) {
4078 if (_isQUOTEMETA(*s)) {
4082 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4084 /* In locale, we quote all non-ASCII Latin1 chars.
4085 * Otherwise use the quoting rules */
4086 if (IN_LOCALE_RUNTIME
4087 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4092 else if (is_QUOTEMETA_high(s)) {
4107 else if (IN_UNI_8_BIT) {
4109 if (_isQUOTEMETA(*s))
4115 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4116 * including everything above ASCII */
4118 if (!isWORDCHAR_A(*s))
4124 SvCUR_set(TARG, d - SvPVX_const(TARG));
4125 (void)SvPOK_only_UTF8(TARG);
4128 sv_setpvn(TARG, s, len);
4145 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4146 const bool full_folding = TRUE;
4147 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4148 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4150 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4151 * You are welcome(?) -Hugmeir
4159 s = (const U8*)SvPV_nomg_const(source, len);
4161 if (ckWARN(WARN_UNINITIALIZED))
4162 report_uninit(source);
4169 SvUPGRADE(dest, SVt_PV);
4170 d = (U8*)SvGROW(dest, min);
4171 (void)SvPOK_only(dest);
4176 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4177 bool tainted = FALSE;
4179 const STRLEN u = UTF8SKIP(s);
4182 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4184 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4185 const UV o = d - (U8*)SvPVX_const(dest);
4187 d = (U8*)SvPVX(dest) + o;
4190 Copy(tmpbuf, d, ulen, U8);
4199 } /* Unflagged string */
4201 /* For locale, bytes, and nothing, the behavior is supposed to be the
4204 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4207 for (; s < send; d++, s++)
4208 *d = toLOWER_LC(*s);
4210 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4211 for (; s < send; d++, s++)
4215 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4216 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4217 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4218 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4221 for (; s < send; d++, s++) {
4222 if (*s == MICRO_SIGN) {
4223 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4224 * is outside of the latin-1 range. There's a couple of ways to
4225 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4226 * What we do here is upgrade what we had already casefolded,
4227 * then enter an inner loop that appends the rest of the characters
4230 len = d - (U8*)SvPVX_const(dest);
4231 SvCUR_set(dest, len);
4232 len = sv_utf8_upgrade_flags_grow(dest,
4233 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4234 /* The max expansion for latin1
4235 * chars is 1 byte becomes 2 */
4237 d = (U8*)SvPVX(dest) + len;
4239 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4241 for (; s < send; s++) {
4243 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4244 if UNI_IS_INVARIANT(fc) {
4245 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4253 Copy(tmpbuf, d, ulen, U8);
4259 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4260 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4261 * which may require growing the SV.
4263 if (SvLEN(dest) < ++min) {
4264 const UV o = d - (U8*)SvPVX_const(dest);
4266 d = (U8*)SvPVX(dest) + o;
4271 else { /* If it's not one of those two, the fold is their lower case */
4272 *d = toLOWER_LATIN1(*s);
4278 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4280 if (SvTAINTED(source))
4290 dVAR; dSP; dMARK; dORIGMARK;
4291 AV *const av = MUTABLE_AV(POPs);
4292 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4294 if (SvTYPE(av) == SVt_PVAV) {
4295 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4296 bool can_preserve = FALSE;
4302 can_preserve = SvCANEXISTDELETE(av);
4305 if (lval && localizing) {
4308 for (svp = MARK + 1; svp <= SP; svp++) {
4309 const I32 elem = SvIV(*svp);
4313 if (max > AvMAX(av))
4317 while (++MARK <= SP) {
4319 I32 elem = SvIV(*MARK);
4320 bool preeminent = TRUE;
4322 if (localizing && can_preserve) {
4323 /* If we can determine whether the element exist,
4324 * Try to preserve the existenceness of a tied array
4325 * element by using EXISTS and DELETE if possible.
4326 * Fallback to FETCH and STORE otherwise. */
4327 preeminent = av_exists(av, elem);
4330 svp = av_fetch(av, elem, lval);
4332 if (!svp || *svp == &PL_sv_undef)
4333 DIE(aTHX_ PL_no_aelem, elem);
4336 save_aelem(av, elem, svp);
4338 SAVEADELETE(av, elem);
4341 *MARK = svp ? *svp : &PL_sv_undef;
4344 if (GIMME != G_ARRAY) {
4346 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4352 /* Smart dereferencing for keys, values and each */
4364 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4369 "Type of argument to %s must be unblessed hashref or arrayref",
4370 PL_op_desc[PL_op->op_type] );
4373 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4375 "Can't modify %s in %s",
4376 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4379 /* Delegate to correct function for op type */
4381 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4382 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4385 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4393 AV *array = MUTABLE_AV(POPs);
4394 const I32 gimme = GIMME_V;
4395 IV *iterp = Perl_av_iter_p(aTHX_ array);
4396 const IV current = (*iterp)++;
4398 if (current > av_len(array)) {
4400 if (gimme == G_SCALAR)
4408 if (gimme == G_ARRAY) {
4409 SV **const element = av_fetch(array, current, 0);
4410 PUSHs(element ? *element : &PL_sv_undef);
4419 AV *array = MUTABLE_AV(POPs);
4420 const I32 gimme = GIMME_V;
4422 *Perl_av_iter_p(aTHX_ array) = 0;
4424 if (gimme == G_SCALAR) {
4426 PUSHi(av_len(array) + 1);
4428 else if (gimme == G_ARRAY) {
4429 IV n = Perl_av_len(aTHX_ array);
4434 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4435 for (i = 0; i <= n; i++) {
4440 for (i = 0; i <= n; i++) {
4441 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4442 PUSHs(elem ? *elem : &PL_sv_undef);
4449 /* Associative arrays. */
4455 HV * hash = MUTABLE_HV(POPs);
4457 const I32 gimme = GIMME_V;
4460 /* might clobber stack_sp */
4461 entry = hv_iternext(hash);
4466 SV* const sv = hv_iterkeysv(entry);
4467 PUSHs(sv); /* won't clobber stack_sp */
4468 if (gimme == G_ARRAY) {
4471 /* might clobber stack_sp */
4472 val = hv_iterval(hash, entry);
4477 else if (gimme == G_SCALAR)
4484 S_do_delete_local(pTHX)
4488 const I32 gimme = GIMME_V;
4491 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4492 SV *unsliced_keysv = sliced ? NULL : POPs;
4493 SV * const osv = POPs;
4494 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4496 const bool tied = SvRMAGICAL(osv)
4497 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4498 const bool can_preserve = SvCANEXISTDELETE(osv);
4499 const U32 type = SvTYPE(osv);
4500 SV ** const end = sliced ? SP : &unsliced_keysv;
4502 if (type == SVt_PVHV) { /* hash element */
4503 HV * const hv = MUTABLE_HV(osv);
4504 while (++MARK <= end) {
4505 SV * const keysv = *MARK;
4507 bool preeminent = TRUE;
4509 preeminent = hv_exists_ent(hv, keysv, 0);
4511 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4518 sv = hv_delete_ent(hv, keysv, 0, 0);
4519 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4522 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4523 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4525 *MARK = sv_mortalcopy(sv);
4531 SAVEHDELETE(hv, keysv);
4532 *MARK = &PL_sv_undef;
4536 else if (type == SVt_PVAV) { /* array element */
4537 if (PL_op->op_flags & OPf_SPECIAL) {
4538 AV * const av = MUTABLE_AV(osv);
4539 while (++MARK <= end) {
4540 I32 idx = SvIV(*MARK);
4542 bool preeminent = TRUE;
4544 preeminent = av_exists(av, idx);
4546 SV **svp = av_fetch(av, idx, 1);
4553 sv = av_delete(av, idx, 0);
4554 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4557 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4559 *MARK = sv_mortalcopy(sv);
4565 SAVEADELETE(av, idx);
4566 *MARK = &PL_sv_undef;
4571 DIE(aTHX_ "panic: avhv_delete no longer supported");
4574 DIE(aTHX_ "Not a HASH reference");
4576 if (gimme == G_VOID)
4578 else if (gimme == G_SCALAR) {
4583 *++MARK = &PL_sv_undef;
4587 else if (gimme != G_VOID)
4588 PUSHs(unsliced_keysv);
4600 if (PL_op->op_private & OPpLVAL_INTRO)
4601 return do_delete_local();
4604 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4606 if (PL_op->op_private & OPpSLICE) {
4608 HV * const hv = MUTABLE_HV(POPs);
4609 const U32 hvtype = SvTYPE(hv);
4610 if (hvtype == SVt_PVHV) { /* hash element */
4611 while (++MARK <= SP) {
4612 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4613 *MARK = sv ? sv : &PL_sv_undef;
4616 else if (hvtype == SVt_PVAV) { /* array element */
4617 if (PL_op->op_flags & OPf_SPECIAL) {
4618 while (++MARK <= SP) {
4619 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4620 *MARK = sv ? sv : &PL_sv_undef;
4625 DIE(aTHX_ "Not a HASH reference");
4628 else if (gimme == G_SCALAR) {
4633 *++MARK = &PL_sv_undef;
4639 HV * const hv = MUTABLE_HV(POPs);
4641 if (SvTYPE(hv) == SVt_PVHV)
4642 sv = hv_delete_ent(hv, keysv, discard, 0);
4643 else if (SvTYPE(hv) == SVt_PVAV) {
4644 if (PL_op->op_flags & OPf_SPECIAL)
4645 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4647 DIE(aTHX_ "panic: avhv_delete no longer supported");
4650 DIE(aTHX_ "Not a HASH reference");
4666 if (PL_op->op_private & OPpEXISTS_SUB) {
4668 SV * const sv = POPs;
4669 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4672 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4677 hv = MUTABLE_HV(POPs);
4678 if (SvTYPE(hv) == SVt_PVHV) {
4679 if (hv_exists_ent(hv, tmpsv, 0))
4682 else if (SvTYPE(hv) == SVt_PVAV) {
4683 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4684 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4689 DIE(aTHX_ "Not a HASH reference");
4696 dVAR; dSP; dMARK; dORIGMARK;
4697 HV * const hv = MUTABLE_HV(POPs);
4698 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4699 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4700 bool can_preserve = FALSE;
4706 if (SvCANEXISTDELETE(hv))
4707 can_preserve = TRUE;
4710 while (++MARK <= SP) {
4711 SV * const keysv = *MARK;
4714 bool preeminent = TRUE;
4716 if (localizing && can_preserve) {
4717 /* If we can determine whether the element exist,
4718 * try to preserve the existenceness of a tied hash
4719 * element by using EXISTS and DELETE if possible.
4720 * Fallback to FETCH and STORE otherwise. */
4721 preeminent = hv_exists_ent(hv, keysv, 0);
4724 he = hv_fetch_ent(hv, keysv, lval, 0);
4725 svp = he ? &HeVAL(he) : NULL;
4728 if (!svp || !*svp || *svp == &PL_sv_undef) {
4729 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4732 if (HvNAME_get(hv) && isGV(*svp))
4733 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4734 else if (preeminent)
4735 save_helem_flags(hv, keysv, svp,
4736 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4738 SAVEHDELETE(hv, keysv);
4741 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4743 if (GIMME != G_ARRAY) {
4745 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4751 /* List operators. */
4756 if (GIMME != G_ARRAY) {
4758 *MARK = *SP; /* unwanted list, return last item */
4760 *MARK = &PL_sv_undef;
4770 SV ** const lastrelem = PL_stack_sp;
4771 SV ** const lastlelem = PL_stack_base + POPMARK;
4772 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4773 SV ** const firstrelem = lastlelem + 1;
4774 I32 is_something_there = FALSE;
4776 const I32 max = lastrelem - lastlelem;
4779 if (GIMME != G_ARRAY) {
4780 I32 ix = SvIV(*lastlelem);
4783 if (ix < 0 || ix >= max)
4784 *firstlelem = &PL_sv_undef;
4786 *firstlelem = firstrelem[ix];
4792 SP = firstlelem - 1;
4796 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4797 I32 ix = SvIV(*lelem);
4800 if (ix < 0 || ix >= max)
4801 *lelem = &PL_sv_undef;
4803 is_something_there = TRUE;
4804 if (!(*lelem = firstrelem[ix]))
4805 *lelem = &PL_sv_undef;
4808 if (is_something_there)
4811 SP = firstlelem - 1;
4817 dVAR; dSP; dMARK; dORIGMARK;
4818 const I32 items = SP - MARK;
4819 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4820 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4821 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4822 ? newRV_noinc(av) : av);
4828 dVAR; dSP; dMARK; dORIGMARK;
4829 HV* const hv = newHV();
4832 SV * const key = *++MARK;
4833 SV * const val = newSV(0);
4835 sv_setsv(val, *++MARK);
4837 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4838 (void)hv_store_ent(hv,key,val,0);
4841 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4842 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4847 S_deref_plain_array(pTHX_ AV *ary)
4849 if (SvTYPE(ary) == SVt_PVAV) return ary;
4850 SvGETMAGIC((SV *)ary);
4851 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4852 Perl_die(aTHX_ "Not an ARRAY reference");
4853 else if (SvOBJECT(SvRV(ary)))
4854 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4855 return (AV *)SvRV(ary);
4858 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4859 # define DEREF_PLAIN_ARRAY(ary) \
4862 SvTYPE(aRrRay) == SVt_PVAV \
4864 : S_deref_plain_array(aTHX_ aRrRay); \
4867 # define DEREF_PLAIN_ARRAY(ary) \
4869 PL_Sv = (SV *)(ary), \
4870 SvTYPE(PL_Sv) == SVt_PVAV \
4872 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4878 dVAR; dSP; dMARK; dORIGMARK;
4879 int num_args = (SP - MARK);
4880 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4889 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4892 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4893 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4900 offset = i = SvIV(*MARK);
4902 offset += AvFILLp(ary) + 1;
4904 DIE(aTHX_ PL_no_aelem, i);
4906 length = SvIVx(*MARK++);
4908 length += AvFILLp(ary) - offset + 1;
4914 length = AvMAX(ary) + 1; /* close enough to infinity */
4918 length = AvMAX(ary) + 1;
4920 if (offset > AvFILLp(ary) + 1) {
4922 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4923 offset = AvFILLp(ary) + 1;
4925 after = AvFILLp(ary) + 1 - (offset + length);
4926 if (after < 0) { /* not that much array */
4927 length += after; /* offset+length now in array */
4933 /* At this point, MARK .. SP-1 is our new LIST */
4936 diff = newlen - length;
4937 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4940 /* make new elements SVs now: avoid problems if they're from the array */
4941 for (dst = MARK, i = newlen; i; i--) {
4942 SV * const h = *dst;
4943 *dst++ = newSVsv(h);
4946 if (diff < 0) { /* shrinking the area */
4947 SV **tmparyval = NULL;
4949 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4950 Copy(MARK, tmparyval, newlen, SV*);
4953 MARK = ORIGMARK + 1;
4954 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4955 MEXTEND(MARK, length);
4956 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4958 EXTEND_MORTAL(length);
4959 for (i = length, dst = MARK; i; i--) {
4960 sv_2mortal(*dst); /* free them eventually */
4967 *MARK = AvARRAY(ary)[offset+length-1];
4970 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4971 SvREFCNT_dec(*dst++); /* free them now */
4974 AvFILLp(ary) += diff;
4976 /* pull up or down? */
4978 if (offset < after) { /* easier to pull up */
4979 if (offset) { /* esp. if nothing to pull */
4980 src = &AvARRAY(ary)[offset-1];
4981 dst = src - diff; /* diff is negative */
4982 for (i = offset; i > 0; i--) /* can't trust Copy */
4986 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4990 if (after) { /* anything to pull down? */
4991 src = AvARRAY(ary) + offset + length;
4992 dst = src + diff; /* diff is negative */
4993 Move(src, dst, after, SV*);
4995 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4996 /* avoid later double free */
5000 dst[--i] = &PL_sv_undef;
5003 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5004 Safefree(tmparyval);
5007 else { /* no, expanding (or same) */
5008 SV** tmparyval = NULL;
5010 Newx(tmparyval, length, SV*); /* so remember deletion */
5011 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5014 if (diff > 0) { /* expanding */
5015 /* push up or down? */
5016 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5020 Move(src, dst, offset, SV*);
5022 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5024 AvFILLp(ary) += diff;
5027 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5028 av_extend(ary, AvFILLp(ary) + diff);
5029 AvFILLp(ary) += diff;
5032 dst = AvARRAY(ary) + AvFILLp(ary);
5034 for (i = after; i; i--) {
5042 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5045 MARK = ORIGMARK + 1;
5046 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5048 Copy(tmparyval, MARK, length, SV*);
5050 EXTEND_MORTAL(length);
5051 for (i = length, dst = MARK; i; i--) {
5052 sv_2mortal(*dst); /* free them eventually */
5059 else if (length--) {
5060 *MARK = tmparyval[length];
5063 while (length-- > 0)
5064 SvREFCNT_dec(tmparyval[length]);
5068 *MARK = &PL_sv_undef;
5069 Safefree(tmparyval);
5073 mg_set(MUTABLE_SV(ary));
5081 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5082 AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5083 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5086 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5089 ENTER_with_name("call_PUSH");
5090 call_method("PUSH",G_SCALAR|G_DISCARD);
5091 LEAVE_with_name("call_PUSH");
5095 PL_delaymagic = DM_DELAY;
5096 for (++MARK; MARK <= SP; MARK++) {
5097 SV * const sv = newSV(0);
5099 sv_setsv(sv, *MARK);
5100 av_store(ary, AvFILLp(ary)+1, sv);
5102 if (PL_delaymagic & DM_ARRAY_ISA)
5103 mg_set(MUTABLE_SV(ary));
5108 if (OP_GIMME(PL_op, 0) != G_VOID) {
5109 PUSHi( AvFILL(ary) + 1 );
5118 AV * const av = PL_op->op_flags & OPf_SPECIAL
5119 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5120 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5124 (void)sv_2mortal(sv);
5131 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5132 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5133 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5136 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5139 ENTER_with_name("call_UNSHIFT");
5140 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5141 LEAVE_with_name("call_UNSHIFT");
5146 av_unshift(ary, SP - MARK);
5148 SV * const sv = newSVsv(*++MARK);
5149 (void)av_store(ary, i++, sv);
5153 if (OP_GIMME(PL_op, 0) != G_VOID) {
5154 PUSHi( AvFILL(ary) + 1 );
5163 if (GIMME == G_ARRAY) {
5164 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5168 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5169 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5170 av = MUTABLE_AV((*SP));
5171 /* In-place reversing only happens in void context for the array
5172 * assignment. We don't need to push anything on the stack. */
5175 if (SvMAGICAL(av)) {
5177 SV *tmp = sv_newmortal();
5178 /* For SvCANEXISTDELETE */
5181 bool can_preserve = SvCANEXISTDELETE(av);
5183 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5187 if (!av_exists(av, i)) {
5188 if (av_exists(av, j)) {
5189 SV *sv = av_delete(av, j, 0);
5190 begin = *av_fetch(av, i, TRUE);
5191 sv_setsv_mg(begin, sv);
5195 else if (!av_exists(av, j)) {
5196 SV *sv = av_delete(av, i, 0);
5197 end = *av_fetch(av, j, TRUE);
5198 sv_setsv_mg(end, sv);
5203 begin = *av_fetch(av, i, TRUE);
5204 end = *av_fetch(av, j, TRUE);
5205 sv_setsv(tmp, begin);
5206 sv_setsv_mg(begin, end);
5207 sv_setsv_mg(end, tmp);
5211 SV **begin = AvARRAY(av);
5214 SV **end = begin + AvFILLp(av);
5216 while (begin < end) {
5217 SV * const tmp = *begin;
5228 SV * const tmp = *MARK;
5232 /* safe as long as stack cannot get extended in the above */
5243 SvUTF8_off(TARG); /* decontaminate */
5245 do_join(TARG, &PL_sv_no, MARK, SP);
5247 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5248 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5249 report_uninit(TARG);
5252 up = SvPV_force(TARG, len);
5254 if (DO_UTF8(TARG)) { /* first reverse each character */
5255 U8* s = (U8*)SvPVX(TARG);
5256 const U8* send = (U8*)(s + len);
5258 if (UTF8_IS_INVARIANT(*s)) {
5263 if (!utf8_to_uvchr_buf(s, send, 0))
5267 down = (char*)(s - 1);
5268 /* reverse this character */
5272 *down-- = (char)tmp;
5278 down = SvPVX(TARG) + len - 1;
5282 *down-- = (char)tmp;
5284 (void)SvPOK_only_UTF8(TARG);
5296 IV limit = POPi; /* note, negative is forever */
5297 SV * const sv = POPs;
5299 const char *s = SvPV_const(sv, len);
5300 const bool do_utf8 = DO_UTF8(sv);
5301 const char *strend = s + len;
5307 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5308 I32 maxiters = slen + 10;
5309 I32 trailing_empty = 0;
5311 const I32 origlimit = limit;
5314 const I32 gimme = GIMME_V;
5316 const I32 oldsave = PL_savestack_ix;
5317 U32 make_mortal = SVs_TEMP;
5322 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5327 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5330 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5331 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5333 RX_MATCH_UTF8_set(rx, do_utf8);
5336 if (pm->op_pmreplrootu.op_pmtargetoff) {
5337 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5340 if (pm->op_pmreplrootu.op_pmtargetgv) {
5341 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5346 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5352 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5354 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5361 for (i = AvFILLp(ary); i >= 0; i--)
5362 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5364 /* temporarily switch stacks */
5365 SAVESWITCHSTACK(PL_curstack, ary);
5369 base = SP - PL_stack_base;
5371 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5373 while (*s == ' ' || is_utf8_space((U8*)s))
5376 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5377 while (isSPACE_LC(*s))
5385 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5389 gimme_scalar = gimme == G_SCALAR && !ary;
5392 limit = maxiters + 2;
5393 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5396 /* this one uses 'm' and is a negative test */
5398 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5399 const int t = UTF8SKIP(m);
5400 /* is_utf8_space returns FALSE for malform utf8 */
5407 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5408 while (m < strend && !isSPACE_LC(*m))
5411 while (m < strend && !isSPACE(*m))
5424 dstr = newSVpvn_flags(s, m-s,
5425 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5429 /* skip the whitespace found last */
5431 s = m + UTF8SKIP(m);
5435 /* this one uses 's' and is a positive test */
5437 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5440 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5441 while (s < strend && isSPACE_LC(*s))
5444 while (s < strend && isSPACE(*s))
5449 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5451 for (m = s; m < strend && *m != '\n'; m++)
5464 dstr = newSVpvn_flags(s, m-s,
5465 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5471 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5473 Pre-extend the stack, either the number of bytes or
5474 characters in the string or a limited amount, triggered by:
5476 my ($x, $y) = split //, $str;
5480 if (!gimme_scalar) {
5481 const U32 items = limit - 1;
5490 /* keep track of how many bytes we skip over */
5500 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5513 dstr = newSVpvn(s, 1);
5529 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5530 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5531 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5532 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5533 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5534 SV * const csv = CALLREG_INTUIT_STRING(rx);
5536 len = RX_MINLENRET(rx);
5537 if (len == 1 && !RX_UTF8(rx) && !tail) {
5538 const char c = *SvPV_nolen_const(csv);
5540 for (m = s; m < strend && *m != c; m++)
5551 dstr = newSVpvn_flags(s, m-s,
5552 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5555 /* The rx->minlen is in characters but we want to step
5556 * s ahead by bytes. */
5558 s = (char*)utf8_hop((U8*)m, len);
5560 s = m + len; /* Fake \n at the end */
5564 while (s < strend && --limit &&
5565 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5566 csv, multiline ? FBMrf_MULTILINE : 0)) )
5575 dstr = newSVpvn_flags(s, m-s,
5576 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5579 /* The rx->minlen is in characters but we want to step
5580 * s ahead by bytes. */
5582 s = (char*)utf8_hop((U8*)m, len);
5584 s = m + len; /* Fake \n at the end */
5589 maxiters += slen * RX_NPARENS(rx);
5590 while (s < strend && --limit)
5594 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5597 if (rex_return == 0)
5599 TAINT_IF(RX_MATCH_TAINTED(rx));
5600 /* we never pass the REXEC_COPY_STR flag, so it should
5601 * never get copied */
5602 assert(!RX_MATCH_COPIED(rx));
5603 m = RX_OFFS(rx)[0].start + orig;
5612 dstr = newSVpvn_flags(s, m-s,
5613 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5616 if (RX_NPARENS(rx)) {
5618 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5619 s = RX_OFFS(rx)[i].start + orig;
5620 m = RX_OFFS(rx)[i].end + orig;
5622 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5623 parens that didn't match -- they should be set to
5624 undef, not the empty string */
5632 if (m >= orig && s >= orig) {
5633 dstr = newSVpvn_flags(s, m-s,
5634 (do_utf8 ? SVf_UTF8 : 0)
5638 dstr = &PL_sv_undef; /* undef, not "" */
5644 s = RX_OFFS(rx)[0].end + orig;
5648 if (!gimme_scalar) {
5649 iters = (SP - PL_stack_base) - base;
5651 if (iters > maxiters)
5652 DIE(aTHX_ "Split loop");
5654 /* keep field after final delim? */
5655 if (s < strend || (iters && origlimit)) {
5656 if (!gimme_scalar) {
5657 const STRLEN l = strend - s;
5658 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5663 else if (!origlimit) {
5665 iters -= trailing_empty;
5667 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5668 if (TOPs && !make_mortal)
5670 *SP-- = &PL_sv_undef;
5677 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5681 if (SvSMAGICAL(ary)) {
5683 mg_set(MUTABLE_SV(ary));
5686 if (gimme == G_ARRAY) {
5688 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5695 ENTER_with_name("call_PUSH");
5696 call_method("PUSH",G_SCALAR|G_DISCARD);
5697 LEAVE_with_name("call_PUSH");
5699 if (gimme == G_ARRAY) {
5701 /* EXTEND should not be needed - we just popped them */
5703 for (i=0; i < iters; i++) {
5704 SV **svp = av_fetch(ary, i, FALSE);
5705 PUSHs((svp) ? *svp : &PL_sv_undef);
5712 if (gimme == G_ARRAY)
5724 SV *const sv = PAD_SVl(PL_op->op_targ);
5726 if (SvPADSTALE(sv)) {
5729 RETURNOP(cLOGOP->op_other);
5731 RETURNOP(cLOGOP->op_next);
5741 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5742 || SvTYPE(retsv) == SVt_PVCV) {
5743 retsv = refto(retsv);
5750 PP(unimplemented_op)
5753 const Optype op_type = PL_op->op_type;
5754 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5755 with out of range op numbers - it only "special" cases op_custom.
5756 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5757 if we get here for a custom op then that means that the custom op didn't
5758 have an implementation. Given that OP_NAME() looks up the custom op
5759 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5760 registers &PL_unimplemented_op as the address of their custom op.
5761 NULL doesn't generate a useful error message. "custom" does. */
5762 const char *const name = op_type >= OP_max
5763 ? "[out of range]" : PL_op_name[PL_op->op_type];
5764 if(OP_IS_SOCKET(op_type))
5765 DIE(aTHX_ PL_no_sock_func, name);
5766 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5769 /* For sorting out arguments passed to a &CORE:: subroutine */
5773 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5774 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5775 AV * const at_ = GvAV(PL_defgv);
5776 SV **svp = at_ ? AvARRAY(at_) : NULL;
5777 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5778 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5779 bool seen_question = 0;
5780 const char *err = NULL;
5781 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5783 /* Count how many args there are first, to get some idea how far to
5784 extend the stack. */
5786 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5788 if (oa & OA_OPTIONAL) seen_question = 1;
5789 if (!seen_question) minargs++;
5793 if(numargs < minargs) err = "Not enough";
5794 else if(numargs > maxargs) err = "Too many";
5796 /* diag_listed_as: Too many arguments for %s */
5798 "%s arguments for %s", err,
5799 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5802 /* Reset the stack pointer. Without this, we end up returning our own
5803 arguments in list context, in addition to the values we are supposed
5804 to return. nextstate usually does this on sub entry, but we need
5805 to run the next op with the caller's hints, so we cannot have a
5807 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5809 if(!maxargs) RETURN;
5811 /* We do this here, rather than with a separate pushmark op, as it has
5812 to come in between two things this function does (stack reset and
5813 arg pushing). This seems the easiest way to do it. */
5816 (void)Perl_pp_pushmark(aTHX);
5819 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5820 PUTBACK; /* The code below can die in various places. */
5822 oa = PL_opargs[opnum] >> OASHIFT;
5823 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5828 if (!numargs && defgv && whicharg == minargs + 1) {
5829 PUSHs(find_rundefsv2(
5830 find_runcv_where(FIND_RUNCV_level_eq, 1, NULL),
5831 cxstack[cxstack_ix].blk_oldcop->cop_seq
5834 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5838 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5843 if (!svp || !*svp || !SvROK(*svp)
5844 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5846 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5847 "Type of arg %d to &CORE::%s must be hash reference",
5848 whicharg, OP_DESC(PL_op->op_next)
5853 if (!numargs) PUSHs(NULL);
5854 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5855 /* no magic here, as the prototype will have added an extra
5856 refgen and we just want what was there before that */
5859 const bool constr = PL_op->op_private & whicharg;
5861 svp && *svp ? *svp : &PL_sv_undef,
5862 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5868 if (!numargs) goto try_defsv;
5870 const bool wantscalar =
5871 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5872 if (!svp || !*svp || !SvROK(*svp)
5873 /* We have to permit globrefs even for the \$ proto, as
5874 *foo is indistinguishable from ${\*foo}, and the proto-
5875 type permits the latter. */
5876 || SvTYPE(SvRV(*svp)) > (
5877 wantscalar ? SVt_PVLV
5878 : opnum == OP_LOCK || opnum == OP_UNDEF
5884 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5885 "Type of arg %d to &CORE::%s must be %s",
5886 whicharg, PL_op_name[opnum],
5888 ? "scalar reference"
5889 : opnum == OP_LOCK || opnum == OP_UNDEF
5890 ? "reference to one of [$@%&*]"
5891 : "reference to one of [$@%*]"
5894 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5895 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5896 /* Undo @_ localisation, so that sub exit does not undo
5897 part of our undeffing. */
5898 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5900 cx->cx_type &= ~ CXp_HASARGS;
5901 assert(!AvREAL(cx->blk_sub.argarray));
5906 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5918 if (PL_op->op_private & OPpOFFBYONE) {
5919 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
5921 else cv = find_runcv(NULL);
5922 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5929 * c-indentation-style: bsd
5931 * indent-tabs-mode: nil
5934 * ex: set ts=8 sts=4 sw=4 et: