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 /* XXX see also S_pushav in pp_hot.c */
88 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
90 if (SvMAGICAL(TARG)) {
92 for (i=0; i < (U32)maxarg; i++) {
93 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
94 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
98 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
102 else if (gimme == G_SCALAR) {
103 SV* const sv = sv_newmortal();
104 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
105 sv_setiv(sv, maxarg);
116 assert(SvTYPE(TARG) == SVt_PVHV);
118 if (PL_op->op_private & OPpLVAL_INTRO)
119 if (!(PL_op->op_private & OPpPAD_STATE))
120 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
121 if (PL_op->op_flags & OPf_REF)
123 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
124 const I32 flags = is_lvalue_sub();
125 if (flags && !(flags & OPpENTERSUB_INARGS)) {
126 if (GIMME == G_SCALAR)
127 /* diag_listed_as: Can't return %s to lvalue scalar context */
128 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
133 if (gimme == G_ARRAY) {
134 RETURNOP(Perl_do_kv(aTHX));
136 else if ((PL_op->op_private & OPpTRUEBOOL
137 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
138 && block_gimme() == G_VOID ))
139 && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied)))
140 SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
141 else if (gimme == G_SCALAR) {
142 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
151 assert(SvTYPE(TARG) == SVt_PVCV);
159 SvPADSTALE_off(TARG);
167 mg_find(PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG],
169 assert(SvTYPE(TARG) == SVt_PVCV);
172 if (CvISXSUB(mg->mg_obj)) { /* constant */
173 /* XXX Should we clone it here? */
174 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
175 to introcv and remove the SvPADSTALE_off. */
176 SAVEPADSVANDMORTALIZE(ARGTARG);
177 PAD_SVl(ARGTARG) = mg->mg_obj;
180 if (CvROOT(mg->mg_obj)) {
181 assert(CvCLONE(mg->mg_obj));
182 assert(!CvCLONED(mg->mg_obj));
184 cv_clone_into((CV *)mg->mg_obj,(CV *)TARG);
185 SAVECLEARSV(PAD_SVl(ARGTARG));
192 static const char S_no_symref_sv[] =
193 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
195 /* In some cases this function inspects PL_op. If this function is called
196 for new op types, more bool parameters may need to be added in place of
199 When noinit is true, the absence of a gv will cause a retval of undef.
200 This is unrelated to the cv-to-gv assignment case.
204 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
208 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
211 sv = amagic_deref_call(sv, to_gv_amg);
215 if (SvTYPE(sv) == SVt_PVIO) {
216 GV * const gv = MUTABLE_GV(sv_newmortal());
217 gv_init(gv, 0, "__ANONIO__", 10, 0);
218 GvIOp(gv) = MUTABLE_IO(sv);
219 SvREFCNT_inc_void_NN(sv);
222 else if (!isGV_with_GP(sv))
223 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
226 if (!isGV_with_GP(sv)) {
228 /* If this is a 'my' scalar and flag is set then vivify
231 if (vivify_sv && sv != &PL_sv_undef) {
234 Perl_croak_no_modify(aTHX);
235 if (cUNOP->op_targ) {
236 SV * const namesv = PAD_SV(cUNOP->op_targ);
237 gv = MUTABLE_GV(newSV(0));
238 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
241 const char * const name = CopSTASHPV(PL_curcop);
242 gv = newGVgen_flags(name,
243 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
245 prepare_SV_for_RV(sv);
246 SvRV_set(sv, MUTABLE_SV(gv));
251 if (PL_op->op_flags & OPf_REF || strict)
252 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
253 if (ckWARN(WARN_UNINITIALIZED))
259 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
260 sv, GV_ADDMG, SVt_PVGV
270 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
273 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
274 == OPpDONT_INIT_GV) {
275 /* We are the target of a coderef assignment. Return
276 the scalar unchanged, and let pp_sasssign deal with
280 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
282 /* FAKE globs in the symbol table cause weird bugs (#77810) */
286 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
287 SV *newsv = sv_newmortal();
288 sv_setsv_flags(newsv, sv, 0);
300 sv, PL_op->op_private & OPpDEREF,
301 PL_op->op_private & HINT_STRICT_REFS,
302 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
303 || PL_op->op_type == OP_READLINE
305 if (PL_op->op_private & OPpLVAL_INTRO)
306 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
311 /* Helper function for pp_rv2sv and pp_rv2av */
313 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
314 const svtype type, SV ***spp)
319 PERL_ARGS_ASSERT_SOFTREF2XV;
321 if (PL_op->op_private & HINT_STRICT_REFS) {
323 Perl_die(aTHX_ S_no_symref_sv, sv,
324 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
326 Perl_die(aTHX_ PL_no_usym, what);
330 PL_op->op_flags & OPf_REF
332 Perl_die(aTHX_ PL_no_usym, what);
333 if (ckWARN(WARN_UNINITIALIZED))
335 if (type != SVt_PV && GIMME_V == G_ARRAY) {
339 **spp = &PL_sv_undef;
342 if ((PL_op->op_flags & OPf_SPECIAL) &&
343 !(PL_op->op_flags & OPf_MOD))
345 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
347 **spp = &PL_sv_undef;
352 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
365 sv = amagic_deref_call(sv, to_sv_amg);
369 switch (SvTYPE(sv)) {
375 DIE(aTHX_ "Not a SCALAR reference");
382 if (!isGV_with_GP(gv)) {
383 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
389 if (PL_op->op_flags & OPf_MOD) {
390 if (PL_op->op_private & OPpLVAL_INTRO) {
391 if (cUNOP->op_first->op_type == OP_NULL)
392 sv = save_scalar(MUTABLE_GV(TOPs));
394 sv = save_scalar(gv);
396 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
398 else if (PL_op->op_private & OPpDEREF)
399 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
408 AV * const av = MUTABLE_AV(TOPs);
409 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
411 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
413 *sv = newSV_type(SVt_PVMG);
414 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
418 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
427 if (PL_op->op_flags & OPf_MOD || LVRET) {
428 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
429 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
431 LvTARG(ret) = SvREFCNT_inc_simple(sv);
432 PUSHs(ret); /* no SvSETMAGIC */
436 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
437 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
438 if (mg && mg->mg_len >= 0) {
456 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
458 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
461 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
462 /* (But not in defined().) */
464 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
466 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
470 cv = MUTABLE_CV(&PL_sv_undef);
471 SETs(MUTABLE_SV(cv));
481 SV *ret = &PL_sv_undef;
483 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
484 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
485 const char * s = SvPVX_const(TOPs);
486 if (strnEQ(s, "CORE::", 6)) {
487 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
488 if (!code || code == -KEY_CORE)
489 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
490 SVfARG(newSVpvn_flags(
491 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
494 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
500 cv = sv_2cv(TOPs, &stash, &gv, 0);
502 ret = newSVpvn_flags(
503 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
513 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
515 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
517 PUSHs(MUTABLE_SV(cv));
531 if (GIMME != G_ARRAY) {
535 *MARK = &PL_sv_undef;
536 *MARK = refto(*MARK);
540 EXTEND_MORTAL(SP - MARK);
542 *MARK = refto(*MARK);
547 S_refto(pTHX_ SV *sv)
552 PERL_ARGS_ASSERT_REFTO;
554 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
557 if (!(sv = LvTARG(sv)))
560 SvREFCNT_inc_void_NN(sv);
562 else if (SvTYPE(sv) == SVt_PVAV) {
563 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
564 av_reify(MUTABLE_AV(sv));
566 SvREFCNT_inc_void_NN(sv);
568 else if (SvPADTMP(sv) && !IS_PADGV(sv))
572 SvREFCNT_inc_void_NN(sv);
575 sv_upgrade(rv, SVt_IV);
584 SV * const sv = POPs;
589 if (!sv || !SvROK(sv))
592 (void)sv_ref(TARG,SvRV(sv),TRUE);
604 stash = CopSTASH(PL_curcop);
606 SV * const ssv = POPs;
610 if (!ssv) goto curstash;
611 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
612 Perl_croak(aTHX_ "Attempt to bless into a reference");
613 ptr = SvPV_const(ssv,len);
615 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
616 "Explicit blessing to '' (assuming package main)");
617 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
620 (void)sv_bless(TOPs, stash);
630 const char * const elem = SvPV_const(sv, len);
631 GV * const gv = MUTABLE_GV(POPs);
636 /* elem will always be NUL terminated. */
637 const char * const second_letter = elem + 1;
640 if (len == 5 && strEQ(second_letter, "RRAY"))
641 tmpRef = MUTABLE_SV(GvAV(gv));
644 if (len == 4 && strEQ(second_letter, "ODE"))
645 tmpRef = MUTABLE_SV(GvCVu(gv));
648 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
649 /* finally deprecated in 5.8.0 */
650 deprecate("*glob{FILEHANDLE}");
651 tmpRef = MUTABLE_SV(GvIOp(gv));
654 if (len == 6 && strEQ(second_letter, "ORMAT"))
655 tmpRef = MUTABLE_SV(GvFORM(gv));
658 if (len == 4 && strEQ(second_letter, "LOB"))
659 tmpRef = MUTABLE_SV(gv);
662 if (len == 4 && strEQ(second_letter, "ASH"))
663 tmpRef = MUTABLE_SV(GvHV(gv));
666 if (*second_letter == 'O' && !elem[2] && len == 2)
667 tmpRef = MUTABLE_SV(GvIOp(gv));
670 if (len == 4 && strEQ(second_letter, "AME"))
671 sv = newSVhek(GvNAME_HEK(gv));
674 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
675 const HV * const stash = GvSTASH(gv);
676 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
677 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
681 if (len == 6 && strEQ(second_letter, "CALAR"))
696 /* Pattern matching */
704 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
705 /* Historically, study was skipped in these cases. */
709 /* Make study a no-op. It's no longer useful and its existence
710 complicates matters elsewhere. */
719 if (PL_op->op_flags & OPf_STACKED)
721 else if (PL_op->op_private & OPpTARGET_MY)
727 if(PL_op->op_type == OP_TRANSR) {
729 const char * const pv = SvPV(sv,len);
730 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
735 TARG = sv_newmortal();
741 /* Lvalue operators. */
744 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
750 PERL_ARGS_ASSERT_DO_CHOMP;
752 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
754 if (SvTYPE(sv) == SVt_PVAV) {
756 AV *const av = MUTABLE_AV(sv);
757 const I32 max = AvFILL(av);
759 for (i = 0; i <= max; i++) {
760 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
761 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
762 do_chomp(retval, sv, chomping);
766 else if (SvTYPE(sv) == SVt_PVHV) {
767 HV* const hv = MUTABLE_HV(sv);
769 (void)hv_iterinit(hv);
770 while ((entry = hv_iternext(hv)))
771 do_chomp(retval, hv_iterval(hv,entry), chomping);
774 else if (SvREADONLY(sv)) {
776 /* SV is copy-on-write */
777 sv_force_normal_flags(sv, 0);
780 Perl_croak_no_modify(aTHX);
785 /* XXX, here sv is utf8-ized as a side-effect!
786 If encoding.pm is used properly, almost string-generating
787 operations, including literal strings, chr(), input data, etc.
788 should have been utf8-ized already, right?
790 sv_recode_to_utf8(sv, PL_encoding);
796 char *temp_buffer = NULL;
805 while (len && s[-1] == '\n') {
812 STRLEN rslen, rs_charlen;
813 const char *rsptr = SvPV_const(PL_rs, rslen);
815 rs_charlen = SvUTF8(PL_rs)
819 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
820 /* Assumption is that rs is shorter than the scalar. */
822 /* RS is utf8, scalar is 8 bit. */
824 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
827 /* Cannot downgrade, therefore cannot possibly match
829 assert (temp_buffer == rsptr);
835 else if (PL_encoding) {
836 /* RS is 8 bit, encoding.pm is used.
837 * Do not recode PL_rs as a side-effect. */
838 svrecode = newSVpvn(rsptr, rslen);
839 sv_recode_to_utf8(svrecode, PL_encoding);
840 rsptr = SvPV_const(svrecode, rslen);
841 rs_charlen = sv_len_utf8(svrecode);
844 /* RS is 8 bit, scalar is utf8. */
845 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
859 if (memNE(s, rsptr, rslen))
861 SvIVX(retval) += rs_charlen;
864 s = SvPV_force_nomg_nolen(sv);
872 SvREFCNT_dec(svrecode);
874 Safefree(temp_buffer);
876 if (len && !SvPOK(sv))
877 s = SvPV_force_nomg(sv, len);
880 char * const send = s + len;
881 char * const start = s;
883 while (s > start && UTF8_IS_CONTINUATION(*s))
885 if (is_utf8_string((U8*)s, send - s)) {
886 sv_setpvn(retval, s, send - s);
888 SvCUR_set(sv, s - start);
894 sv_setpvs(retval, "");
898 sv_setpvn(retval, s, 1);
905 sv_setpvs(retval, "");
913 const bool chomping = PL_op->op_type == OP_SCHOMP;
917 do_chomp(TARG, TOPs, chomping);
924 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
925 const bool chomping = PL_op->op_type == OP_CHOMP;
930 do_chomp(TARG, *++MARK, chomping);
941 if (!PL_op->op_private) {
950 SV_CHECK_THINKFIRST_COW_DROP(sv);
952 switch (SvTYPE(sv)) {
956 av_undef(MUTABLE_AV(sv));
959 hv_undef(MUTABLE_HV(sv));
962 if (cv_const_sv((const CV *)sv))
963 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
964 "Constant subroutine %"SVf" undefined",
965 SVfARG(CvANON((const CV *)sv)
966 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
967 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
971 /* let user-undef'd sub keep its identity */
972 GV* const gv = CvGV((const CV *)sv);
973 HEK * const hek = CvNAME_HEK((CV *)sv);
974 if (hek) share_hek_hek(hek);
975 cv_undef(MUTABLE_CV(sv));
976 if (gv) CvGV_set(MUTABLE_CV(sv), gv);
978 SvANY((CV *)sv)->xcv_gv_u.xcv_hek = hek;
984 assert(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)
1025 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1041 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1042 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1043 Perl_croak_no_modify(aTHX);
1045 TARG = sv_newmortal();
1046 sv_setsv(TARG, TOPs);
1047 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1048 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1050 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1051 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1055 else sv_dec_nomg(TOPs);
1057 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1058 if (inc && !SvOK(TARG))
1064 /* Ordinary operators. */
1068 dVAR; dSP; dATARGET; SV *svl, *svr;
1069 #ifdef PERL_PRESERVE_IVUV
1072 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1075 #ifdef PERL_PRESERVE_IVUV
1076 /* For integer to integer power, we do the calculation by hand wherever
1077 we're sure it is safe; otherwise we call pow() and try to convert to
1078 integer afterwards. */
1079 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1087 const IV iv = SvIVX(svr);
1091 goto float_it; /* Can't do negative powers this way. */
1095 baseuok = SvUOK(svl);
1097 baseuv = SvUVX(svl);
1099 const IV iv = SvIVX(svl);
1102 baseuok = TRUE; /* effectively it's a UV now */
1104 baseuv = -iv; /* abs, baseuok == false records sign */
1107 /* now we have integer ** positive integer. */
1110 /* foo & (foo - 1) is zero only for a power of 2. */
1111 if (!(baseuv & (baseuv - 1))) {
1112 /* We are raising power-of-2 to a positive integer.
1113 The logic here will work for any base (even non-integer
1114 bases) but it can be less accurate than
1115 pow (base,power) or exp (power * log (base)) when the
1116 intermediate values start to spill out of the mantissa.
1117 With powers of 2 we know this can't happen.
1118 And powers of 2 are the favourite thing for perl
1119 programmers to notice ** not doing what they mean. */
1121 NV base = baseuok ? baseuv : -(NV)baseuv;
1126 while (power >>= 1) {
1134 SvIV_please_nomg(svr);
1137 unsigned int highbit = 8 * sizeof(UV);
1138 unsigned int diff = 8 * sizeof(UV);
1139 while (diff >>= 1) {
1141 if (baseuv >> highbit) {
1145 /* we now have baseuv < 2 ** highbit */
1146 if (power * highbit <= 8 * sizeof(UV)) {
1147 /* result will definitely fit in UV, so use UV math
1148 on same algorithm as above */
1151 const bool odd_power = cBOOL(power & 1);
1155 while (power >>= 1) {
1162 if (baseuok || !odd_power)
1163 /* answer is positive */
1165 else if (result <= (UV)IV_MAX)
1166 /* answer negative, fits in IV */
1167 SETi( -(IV)result );
1168 else if (result == (UV)IV_MIN)
1169 /* 2's complement assumption: special case IV_MIN */
1172 /* answer negative, doesn't fit */
1173 SETn( -(NV)result );
1181 NV right = SvNV_nomg(svr);
1182 NV left = SvNV_nomg(svl);
1185 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1187 We are building perl with long double support and are on an AIX OS
1188 afflicted with a powl() function that wrongly returns NaNQ for any
1189 negative base. This was reported to IBM as PMR #23047-379 on
1190 03/06/2006. The problem exists in at least the following versions
1191 of AIX and the libm fileset, and no doubt others as well:
1193 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1194 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1195 AIX 5.2.0 bos.adt.libm 5.2.0.85
1197 So, until IBM fixes powl(), we provide the following workaround to
1198 handle the problem ourselves. Our logic is as follows: for
1199 negative bases (left), we use fmod(right, 2) to check if the
1200 exponent is an odd or even integer:
1202 - if odd, powl(left, right) == -powl(-left, right)
1203 - if even, powl(left, right) == powl(-left, right)
1205 If the exponent is not an integer, the result is rightly NaNQ, so
1206 we just return that (as NV_NAN).
1210 NV mod2 = Perl_fmod( right, 2.0 );
1211 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1212 SETn( -Perl_pow( -left, right) );
1213 } else if (mod2 == 0.0) { /* even integer */
1214 SETn( Perl_pow( -left, right) );
1215 } else { /* fractional power */
1219 SETn( Perl_pow( left, right) );
1222 SETn( Perl_pow( left, right) );
1223 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1225 #ifdef PERL_PRESERVE_IVUV
1227 SvIV_please_nomg(svr);
1235 dVAR; dSP; dATARGET; SV *svl, *svr;
1236 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1239 #ifdef PERL_PRESERVE_IVUV
1240 if (SvIV_please_nomg(svr)) {
1241 /* Unless the left argument is integer in range we are going to have to
1242 use NV maths. Hence only attempt to coerce the right argument if
1243 we know the left is integer. */
1244 /* Left operand is defined, so is it IV? */
1245 if (SvIV_please_nomg(svl)) {
1246 bool auvok = SvUOK(svl);
1247 bool buvok = SvUOK(svr);
1248 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1249 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1258 const IV aiv = SvIVX(svl);
1261 auvok = TRUE; /* effectively it's a UV now */
1263 alow = -aiv; /* abs, auvok == false records sign */
1269 const IV biv = SvIVX(svr);
1272 buvok = TRUE; /* effectively it's a UV now */
1274 blow = -biv; /* abs, buvok == false records sign */
1278 /* If this does sign extension on unsigned it's time for plan B */
1279 ahigh = alow >> (4 * sizeof (UV));
1281 bhigh = blow >> (4 * sizeof (UV));
1283 if (ahigh && bhigh) {
1285 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1286 which is overflow. Drop to NVs below. */
1287 } else if (!ahigh && !bhigh) {
1288 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1289 so the unsigned multiply cannot overflow. */
1290 const UV product = alow * blow;
1291 if (auvok == buvok) {
1292 /* -ve * -ve or +ve * +ve gives a +ve result. */
1296 } else if (product <= (UV)IV_MIN) {
1297 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1298 /* -ve result, which could overflow an IV */
1300 SETi( -(IV)product );
1302 } /* else drop to NVs below. */
1304 /* One operand is large, 1 small */
1307 /* swap the operands */
1309 bhigh = blow; /* bhigh now the temp var for the swap */
1313 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1314 multiplies can't overflow. shift can, add can, -ve can. */
1315 product_middle = ahigh * blow;
1316 if (!(product_middle & topmask)) {
1317 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1319 product_middle <<= (4 * sizeof (UV));
1320 product_low = alow * blow;
1322 /* as for pp_add, UV + something mustn't get smaller.
1323 IIRC ANSI mandates this wrapping *behaviour* for
1324 unsigned whatever the actual representation*/
1325 product_low += product_middle;
1326 if (product_low >= product_middle) {
1327 /* didn't overflow */
1328 if (auvok == buvok) {
1329 /* -ve * -ve or +ve * +ve gives a +ve result. */
1331 SETu( product_low );
1333 } else if (product_low <= (UV)IV_MIN) {
1334 /* 2s complement assumption again */
1335 /* -ve result, which could overflow an IV */
1337 SETi( -(IV)product_low );
1339 } /* else drop to NVs below. */
1341 } /* product_middle too large */
1342 } /* ahigh && bhigh */
1347 NV right = SvNV_nomg(svr);
1348 NV left = SvNV_nomg(svl);
1350 SETn( left * right );
1357 dVAR; dSP; dATARGET; SV *svl, *svr;
1358 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1361 /* Only try to do UV divide first
1362 if ((SLOPPYDIVIDE is true) or
1363 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1365 The assumption is that it is better to use floating point divide
1366 whenever possible, only doing integer divide first if we can't be sure.
1367 If NV_PRESERVES_UV is true then we know at compile time that no UV
1368 can be too large to preserve, so don't need to compile the code to
1369 test the size of UVs. */
1372 # define PERL_TRY_UV_DIVIDE
1373 /* ensure that 20./5. == 4. */
1375 # ifdef PERL_PRESERVE_IVUV
1376 # ifndef NV_PRESERVES_UV
1377 # define PERL_TRY_UV_DIVIDE
1382 #ifdef PERL_TRY_UV_DIVIDE
1383 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1384 bool left_non_neg = SvUOK(svl);
1385 bool right_non_neg = SvUOK(svr);
1389 if (right_non_neg) {
1393 const IV biv = SvIVX(svr);
1396 right_non_neg = TRUE; /* effectively it's a UV now */
1402 /* historically undef()/0 gives a "Use of uninitialized value"
1403 warning before dieing, hence this test goes here.
1404 If it were immediately before the second SvIV_please, then
1405 DIE() would be invoked before left was even inspected, so
1406 no inspection would give no warning. */
1408 DIE(aTHX_ "Illegal division by zero");
1414 const IV aiv = SvIVX(svl);
1417 left_non_neg = TRUE; /* effectively it's a UV now */
1426 /* For sloppy divide we always attempt integer division. */
1428 /* Otherwise we only attempt it if either or both operands
1429 would not be preserved by an NV. If both fit in NVs
1430 we fall through to the NV divide code below. However,
1431 as left >= right to ensure integer result here, we know that
1432 we can skip the test on the right operand - right big
1433 enough not to be preserved can't get here unless left is
1436 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1439 /* Integer division can't overflow, but it can be imprecise. */
1440 const UV result = left / right;
1441 if (result * right == left) {
1442 SP--; /* result is valid */
1443 if (left_non_neg == right_non_neg) {
1444 /* signs identical, result is positive. */
1448 /* 2s complement assumption */
1449 if (result <= (UV)IV_MIN)
1450 SETi( -(IV)result );
1452 /* It's exact but too negative for IV. */
1453 SETn( -(NV)result );
1456 } /* tried integer divide but it was not an integer result */
1457 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1458 } /* one operand wasn't SvIOK */
1459 #endif /* PERL_TRY_UV_DIVIDE */
1461 NV right = SvNV_nomg(svr);
1462 NV left = SvNV_nomg(svl);
1463 (void)POPs;(void)POPs;
1464 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1465 if (! Perl_isnan(right) && right == 0.0)
1469 DIE(aTHX_ "Illegal division by zero");
1470 PUSHn( left / right );
1477 dVAR; dSP; dATARGET;
1478 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1482 bool left_neg = FALSE;
1483 bool right_neg = FALSE;
1484 bool use_double = FALSE;
1485 bool dright_valid = FALSE;
1488 SV * const svr = TOPs;
1489 SV * const svl = TOPm1s;
1490 if (SvIV_please_nomg(svr)) {
1491 right_neg = !SvUOK(svr);
1495 const IV biv = SvIVX(svr);
1498 right_neg = FALSE; /* effectively it's a UV now */
1505 dright = SvNV_nomg(svr);
1506 right_neg = dright < 0;
1509 if (dright < UV_MAX_P1) {
1510 right = U_V(dright);
1511 dright_valid = TRUE; /* In case we need to use double below. */
1517 /* At this point use_double is only true if right is out of range for
1518 a UV. In range NV has been rounded down to nearest UV and
1519 use_double false. */
1520 if (!use_double && SvIV_please_nomg(svl)) {
1521 left_neg = !SvUOK(svl);
1525 const IV aiv = SvIVX(svl);
1528 left_neg = FALSE; /* effectively it's a UV now */
1535 dleft = SvNV_nomg(svl);
1536 left_neg = dleft < 0;
1540 /* This should be exactly the 5.6 behaviour - if left and right are
1541 both in range for UV then use U_V() rather than floor. */
1543 if (dleft < UV_MAX_P1) {
1544 /* right was in range, so is dleft, so use UVs not double.
1548 /* left is out of range for UV, right was in range, so promote
1549 right (back) to double. */
1551 /* The +0.5 is used in 5.6 even though it is not strictly
1552 consistent with the implicit +0 floor in the U_V()
1553 inside the #if 1. */
1554 dleft = Perl_floor(dleft + 0.5);
1557 dright = Perl_floor(dright + 0.5);
1568 DIE(aTHX_ "Illegal modulus zero");
1570 dans = Perl_fmod(dleft, dright);
1571 if ((left_neg != right_neg) && dans)
1572 dans = dright - dans;
1575 sv_setnv(TARG, dans);
1581 DIE(aTHX_ "Illegal modulus zero");
1584 if ((left_neg != right_neg) && ans)
1587 /* XXX may warn: unary minus operator applied to unsigned type */
1588 /* could change -foo to be (~foo)+1 instead */
1589 if (ans <= ~((UV)IV_MAX)+1)
1590 sv_setiv(TARG, ~ans+1);
1592 sv_setnv(TARG, -(NV)ans);
1595 sv_setuv(TARG, ans);
1604 dVAR; dSP; dATARGET;
1608 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1609 /* TODO: think of some way of doing list-repeat overloading ??? */
1614 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1620 const UV uv = SvUV_nomg(sv);
1622 count = IV_MAX; /* The best we can do? */
1626 const IV iv = SvIV_nomg(sv);
1633 else if (SvNOKp(sv)) {
1634 const NV nv = SvNV_nomg(sv);
1641 count = SvIV_nomg(sv);
1643 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1645 static const char oom_list_extend[] = "Out of memory during list extend";
1646 const I32 items = SP - MARK;
1647 const I32 max = items * count;
1649 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1650 /* Did the max computation overflow? */
1651 if (items > 0 && max > 0 && (max < items || max < count))
1652 Perl_croak(aTHX_ oom_list_extend);
1657 /* This code was intended to fix 20010809.028:
1660 for (($x =~ /./g) x 2) {
1661 print chop; # "abcdabcd" expected as output.
1664 * but that change (#11635) broke this code:
1666 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1668 * I can't think of a better fix that doesn't introduce
1669 * an efficiency hit by copying the SVs. The stack isn't
1670 * refcounted, and mortalisation obviously doesn't
1671 * Do The Right Thing when the stack has more than
1672 * one pointer to the same mortal value.
1676 *SP = sv_2mortal(newSVsv(*SP));
1686 repeatcpy((char*)(MARK + items), (char*)MARK,
1687 items * sizeof(const SV *), count - 1);
1690 else if (count <= 0)
1693 else { /* Note: mark already snarfed by pp_list */
1694 SV * const tmpstr = POPs;
1697 static const char oom_string_extend[] =
1698 "Out of memory during string extend";
1701 sv_setsv_nomg(TARG, tmpstr);
1702 SvPV_force_nomg(TARG, len);
1703 isutf = DO_UTF8(TARG);
1708 const STRLEN max = (UV)count * len;
1709 if (len > MEM_SIZE_MAX / count)
1710 Perl_croak(aTHX_ oom_string_extend);
1711 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1712 SvGROW(TARG, max + 1);
1713 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1714 SvCUR_set(TARG, SvCUR(TARG) * count);
1716 *SvEND(TARG) = '\0';
1719 (void)SvPOK_only_UTF8(TARG);
1721 (void)SvPOK_only(TARG);
1723 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1724 /* The parser saw this as a list repeat, and there
1725 are probably several items on the stack. But we're
1726 in scalar context, and there's no pp_list to save us
1727 now. So drop the rest of the items -- robin@kitsite.com
1739 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1740 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1743 useleft = USE_LEFT(svl);
1744 #ifdef PERL_PRESERVE_IVUV
1745 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1746 "bad things" happen if you rely on signed integers wrapping. */
1747 if (SvIV_please_nomg(svr)) {
1748 /* Unless the left argument is integer in range we are going to have to
1749 use NV maths. Hence only attempt to coerce the right argument if
1750 we know the left is integer. */
1757 a_valid = auvok = 1;
1758 /* left operand is undef, treat as zero. */
1760 /* Left operand is defined, so is it IV? */
1761 if (SvIV_please_nomg(svl)) {
1762 if ((auvok = SvUOK(svl)))
1765 const IV aiv = SvIVX(svl);
1768 auvok = 1; /* Now acting as a sign flag. */
1769 } else { /* 2s complement assumption for IV_MIN */
1777 bool result_good = 0;
1780 bool buvok = SvUOK(svr);
1785 const IV biv = SvIVX(svr);
1792 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1793 else "IV" now, independent of how it came in.
1794 if a, b represents positive, A, B negative, a maps to -A etc
1799 all UV maths. negate result if A negative.
1800 subtract if signs same, add if signs differ. */
1802 if (auvok ^ buvok) {
1811 /* Must get smaller */
1816 if (result <= buv) {
1817 /* result really should be -(auv-buv). as its negation
1818 of true value, need to swap our result flag */
1830 if (result <= (UV)IV_MIN)
1831 SETi( -(IV)result );
1833 /* result valid, but out of range for IV. */
1834 SETn( -(NV)result );
1838 } /* Overflow, drop through to NVs. */
1843 NV value = SvNV_nomg(svr);
1847 /* left operand is undef, treat as zero - value */
1851 SETn( SvNV_nomg(svl) - value );
1858 dVAR; dSP; dATARGET; SV *svl, *svr;
1859 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1863 const IV shift = SvIV_nomg(svr);
1864 if (PL_op->op_private & HINT_INTEGER) {
1865 const IV i = SvIV_nomg(svl);
1869 const UV u = SvUV_nomg(svl);
1878 dVAR; dSP; dATARGET; SV *svl, *svr;
1879 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1883 const IV shift = SvIV_nomg(svr);
1884 if (PL_op->op_private & HINT_INTEGER) {
1885 const IV i = SvIV_nomg(svl);
1889 const UV u = SvUV_nomg(svl);
1901 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1905 (SvIOK_notUV(left) && SvIOK_notUV(right))
1906 ? (SvIVX(left) < SvIVX(right))
1907 : (do_ncmp(left, right) == -1)
1917 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1921 (SvIOK_notUV(left) && SvIOK_notUV(right))
1922 ? (SvIVX(left) > SvIVX(right))
1923 : (do_ncmp(left, right) == 1)
1933 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1937 (SvIOK_notUV(left) && SvIOK_notUV(right))
1938 ? (SvIVX(left) <= SvIVX(right))
1939 : (do_ncmp(left, right) <= 0)
1949 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1953 (SvIOK_notUV(left) && SvIOK_notUV(right))
1954 ? (SvIVX(left) >= SvIVX(right))
1955 : ( (do_ncmp(left, right) & 2) == 0)
1965 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1969 (SvIOK_notUV(left) && SvIOK_notUV(right))
1970 ? (SvIVX(left) != SvIVX(right))
1971 : (do_ncmp(left, right) != 0)
1976 /* compare left and right SVs. Returns:
1980 * 2: left or right was a NaN
1983 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1987 PERL_ARGS_ASSERT_DO_NCMP;
1988 #ifdef PERL_PRESERVE_IVUV
1989 /* Fortunately it seems NaN isn't IOK */
1990 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1992 const IV leftiv = SvIVX(left);
1993 if (!SvUOK(right)) {
1994 /* ## IV <=> IV ## */
1995 const IV rightiv = SvIVX(right);
1996 return (leftiv > rightiv) - (leftiv < rightiv);
1998 /* ## IV <=> UV ## */
2000 /* As (b) is a UV, it's >=0, so it must be < */
2003 const UV rightuv = SvUVX(right);
2004 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2009 /* ## UV <=> UV ## */
2010 const UV leftuv = SvUVX(left);
2011 const UV rightuv = SvUVX(right);
2012 return (leftuv > rightuv) - (leftuv < rightuv);
2014 /* ## UV <=> IV ## */
2016 const IV rightiv = SvIVX(right);
2018 /* As (a) is a UV, it's >=0, so it cannot be < */
2021 const UV leftuv = SvUVX(left);
2022 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2025 assert(0); /* NOTREACHED */
2029 NV const rnv = SvNV_nomg(right);
2030 NV const lnv = SvNV_nomg(left);
2032 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2033 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2036 return (lnv > rnv) - (lnv < rnv);
2055 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2058 value = do_ncmp(left, right);
2073 int amg_type = sle_amg;
2077 switch (PL_op->op_type) {
2096 tryAMAGICbin_MG(amg_type, AMGf_set);
2099 const int cmp = (IN_LOCALE_RUNTIME
2100 ? sv_cmp_locale_flags(left, right, 0)
2101 : sv_cmp_flags(left, right, 0));
2102 SETs(boolSV(cmp * multiplier < rhs));
2110 tryAMAGICbin_MG(seq_amg, AMGf_set);
2113 SETs(boolSV(sv_eq_flags(left, right, 0)));
2121 tryAMAGICbin_MG(sne_amg, AMGf_set);
2124 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2132 tryAMAGICbin_MG(scmp_amg, 0);
2135 const int cmp = (IN_LOCALE_RUNTIME
2136 ? sv_cmp_locale_flags(left, right, 0)
2137 : sv_cmp_flags(left, right, 0));
2145 dVAR; dSP; dATARGET;
2146 tryAMAGICbin_MG(band_amg, AMGf_assign);
2149 if (SvNIOKp(left) || SvNIOKp(right)) {
2150 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2151 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2152 if (PL_op->op_private & HINT_INTEGER) {
2153 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2157 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2160 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2161 if (right_ro_nonnum) SvNIOK_off(right);
2164 do_vop(PL_op->op_type, TARG, left, right);
2173 dVAR; dSP; dATARGET;
2174 const int op_type = PL_op->op_type;
2176 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2179 if (SvNIOKp(left) || SvNIOKp(right)) {
2180 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2181 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2182 if (PL_op->op_private & HINT_INTEGER) {
2183 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2184 const IV r = SvIV_nomg(right);
2185 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2189 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2190 const UV r = SvUV_nomg(right);
2191 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2194 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2195 if (right_ro_nonnum) SvNIOK_off(right);
2198 do_vop(op_type, TARG, left, right);
2205 PERL_STATIC_INLINE bool
2206 S_negate_string(pTHX)
2211 SV * const sv = TOPs;
2212 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2214 s = SvPV_nomg_const(sv, len);
2215 if (isIDFIRST(*s)) {
2216 sv_setpvs(TARG, "-");
2219 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2220 sv_setsv_nomg(TARG, sv);
2221 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2231 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2232 if (S_negate_string(aTHX)) return NORMAL;
2234 SV * const sv = TOPs;
2237 /* It's publicly an integer */
2240 if (SvIVX(sv) == IV_MIN) {
2241 /* 2s complement assumption. */
2242 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2245 else if (SvUVX(sv) <= IV_MAX) {
2250 else if (SvIVX(sv) != IV_MIN) {
2254 #ifdef PERL_PRESERVE_IVUV
2261 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2262 SETn(-SvNV_nomg(sv));
2263 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2264 goto oops_its_an_int;
2266 SETn(-SvNV_nomg(sv));
2274 tryAMAGICun_MG(not_amg, AMGf_set);
2275 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2282 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2286 if (PL_op->op_private & HINT_INTEGER) {
2287 const IV i = ~SvIV_nomg(sv);
2291 const UV u = ~SvUV_nomg(sv);
2300 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2301 sv_setsv_nomg(TARG, sv);
2302 tmps = (U8*)SvPV_force_nomg(TARG, len);
2305 /* Calculate exact length, let's not estimate. */
2310 U8 * const send = tmps + len;
2311 U8 * const origtmps = tmps;
2312 const UV utf8flags = UTF8_ALLOW_ANYUV;
2314 while (tmps < send) {
2315 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2317 targlen += UNISKIP(~c);
2323 /* Now rewind strings and write them. */
2330 Newx(result, targlen + 1, U8);
2332 while (tmps < send) {
2333 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2335 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2338 sv_usepvn_flags(TARG, (char*)result, targlen,
2339 SV_HAS_TRAILING_NUL);
2346 Newx(result, nchar + 1, U8);
2348 while (tmps < send) {
2349 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2354 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2363 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2366 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2371 for ( ; anum > 0; anum--, tmps++)
2379 /* integer versions of some of the above */
2383 dVAR; dSP; dATARGET;
2384 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2387 SETi( left * right );
2395 dVAR; dSP; dATARGET;
2396 tryAMAGICbin_MG(div_amg, AMGf_assign);
2399 IV value = SvIV_nomg(right);
2401 DIE(aTHX_ "Illegal division by zero");
2402 num = SvIV_nomg(left);
2404 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2408 value = num / value;
2414 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2421 /* This is the vanilla old i_modulo. */
2422 dVAR; dSP; dATARGET;
2423 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2427 DIE(aTHX_ "Illegal modulus zero");
2428 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2432 SETi( left % right );
2437 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2442 /* This is the i_modulo with the workaround for the _moddi3 bug
2443 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2444 * See below for pp_i_modulo. */
2445 dVAR; dSP; dATARGET;
2446 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2450 DIE(aTHX_ "Illegal modulus zero");
2451 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2455 SETi( left % PERL_ABS(right) );
2462 dVAR; dSP; dATARGET;
2463 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2467 DIE(aTHX_ "Illegal modulus zero");
2468 /* The assumption is to use hereafter the old vanilla version... */
2470 PL_ppaddr[OP_I_MODULO] =
2472 /* .. but if we have glibc, we might have a buggy _moddi3
2473 * (at least glicb 2.2.5 is known to have this bug), in other
2474 * words our integer modulus with negative quad as the second
2475 * argument might be broken. Test for this and re-patch the
2476 * opcode dispatch table if that is the case, remembering to
2477 * also apply the workaround so that this first round works
2478 * right, too. See [perl #9402] for more information. */
2482 /* Cannot do this check with inlined IV constants since
2483 * that seems to work correctly even with the buggy glibc. */
2485 /* Yikes, we have the bug.
2486 * Patch in the workaround version. */
2488 PL_ppaddr[OP_I_MODULO] =
2489 &Perl_pp_i_modulo_1;
2490 /* Make certain we work right this time, too. */
2491 right = PERL_ABS(right);
2494 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2498 SETi( left % right );
2506 dVAR; dSP; dATARGET;
2507 tryAMAGICbin_MG(add_amg, AMGf_assign);
2509 dPOPTOPiirl_ul_nomg;
2510 SETi( left + right );
2517 dVAR; dSP; dATARGET;
2518 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2520 dPOPTOPiirl_ul_nomg;
2521 SETi( left - right );
2529 tryAMAGICbin_MG(lt_amg, AMGf_set);
2532 SETs(boolSV(left < right));
2540 tryAMAGICbin_MG(gt_amg, AMGf_set);
2543 SETs(boolSV(left > right));
2551 tryAMAGICbin_MG(le_amg, AMGf_set);
2554 SETs(boolSV(left <= right));
2562 tryAMAGICbin_MG(ge_amg, AMGf_set);
2565 SETs(boolSV(left >= right));
2573 tryAMAGICbin_MG(eq_amg, AMGf_set);
2576 SETs(boolSV(left == right));
2584 tryAMAGICbin_MG(ne_amg, AMGf_set);
2587 SETs(boolSV(left != right));
2595 tryAMAGICbin_MG(ncmp_amg, 0);
2602 else if (left < right)
2614 tryAMAGICun_MG(neg_amg, 0);
2615 if (S_negate_string(aTHX)) return NORMAL;
2617 SV * const sv = TOPs;
2618 IV const i = SvIV_nomg(sv);
2624 /* High falutin' math. */
2629 tryAMAGICbin_MG(atan2_amg, 0);
2632 SETn(Perl_atan2(left, right));
2640 int amg_type = sin_amg;
2641 const char *neg_report = NULL;
2642 NV (*func)(NV) = Perl_sin;
2643 const int op_type = PL_op->op_type;
2660 amg_type = sqrt_amg;
2662 neg_report = "sqrt";
2667 tryAMAGICun_MG(amg_type, 0);
2669 SV * const arg = POPs;
2670 const NV value = SvNV_nomg(arg);
2672 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2673 SET_NUMERIC_STANDARD();
2674 /* diag_listed_as: Can't take log of %g */
2675 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2678 XPUSHn(func(value));
2683 /* Support Configure command-line overrides for rand() functions.
2684 After 5.005, perhaps we should replace this by Configure support
2685 for drand48(), random(), or rand(). For 5.005, though, maintain
2686 compatibility by calling rand() but allow the user to override it.
2687 See INSTALL for details. --Andy Dougherty 15 July 1998
2689 /* Now it's after 5.005, and Configure supports drand48() and random(),
2690 in addition to rand(). So the overrides should not be needed any more.
2691 --Jarkko Hietaniemi 27 September 1998
2694 #ifndef HAS_DRAND48_PROTO
2695 extern double drand48 (void);
2705 value = 1.0; (void)POPs;
2711 if (!PL_srand_called) {
2712 (void)seedDrand01((Rand_seed_t)seed());
2713 PL_srand_called = TRUE;
2725 if (MAXARG >= 1 && (TOPs || POPs)) {
2732 pv = SvPV(top, len);
2733 flags = grok_number(pv, len, &anum);
2735 if (!(flags & IS_NUMBER_IN_UV)) {
2736 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2737 "Integer overflow in srand");
2745 (void)seedDrand01((Rand_seed_t)anum);
2746 PL_srand_called = TRUE;
2750 /* Historically srand always returned true. We can avoid breaking
2752 sv_setpvs(TARG, "0 but true");
2761 tryAMAGICun_MG(int_amg, AMGf_numeric);
2763 SV * const sv = TOPs;
2764 const IV iv = SvIV_nomg(sv);
2765 /* XXX it's arguable that compiler casting to IV might be subtly
2766 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2767 else preferring IV has introduced a subtle behaviour change bug. OTOH
2768 relying on floating point to be accurate is a bug. */
2773 else if (SvIOK(sv)) {
2775 SETu(SvUV_nomg(sv));
2780 const NV value = SvNV_nomg(sv);
2782 if (value < (NV)UV_MAX + 0.5) {
2785 SETn(Perl_floor(value));
2789 if (value > (NV)IV_MIN - 0.5) {
2792 SETn(Perl_ceil(value));
2803 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2805 SV * const sv = TOPs;
2806 /* This will cache the NV value if string isn't actually integer */
2807 const IV iv = SvIV_nomg(sv);
2812 else if (SvIOK(sv)) {
2813 /* IVX is precise */
2815 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2823 /* 2s complement assumption. Also, not really needed as
2824 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2830 const NV value = SvNV_nomg(sv);
2844 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2848 SV* const sv = POPs;
2850 tmps = (SvPV_const(sv, len));
2852 /* If Unicode, try to downgrade
2853 * If not possible, croak. */
2854 SV* const tsv = sv_2mortal(newSVsv(sv));
2857 sv_utf8_downgrade(tsv, FALSE);
2858 tmps = SvPV_const(tsv, len);
2860 if (PL_op->op_type == OP_HEX)
2863 while (*tmps && len && isSPACE(*tmps))
2867 if (*tmps == 'x' || *tmps == 'X') {
2869 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2871 else if (*tmps == 'b' || *tmps == 'B')
2872 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2874 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2876 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2890 SV * const sv = TOPs;
2895 SETi(sv_len_utf8_nomg(sv));
2899 (void)SvPV_nomg_const(sv,len);
2903 if (!SvPADTMP(TARG)) {
2904 sv_setsv_nomg(TARG, &PL_sv_undef);
2912 /* Returns false if substring is completely outside original string.
2913 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2914 always be true for an explicit 0.
2917 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2918 bool pos1_is_uv, IV len_iv,
2919 bool len_is_uv, STRLEN *posp,
2925 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2927 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2928 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2931 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2934 if (len_iv || len_is_uv) {
2935 if (!len_is_uv && len_iv < 0) {
2936 pos2_iv = curlen + len_iv;
2938 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2941 } else { /* len_iv >= 0 */
2942 if (!pos1_is_uv && pos1_iv < 0) {
2943 pos2_iv = pos1_iv + len_iv;
2944 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2946 if ((UV)len_iv > curlen-(UV)pos1_iv)
2949 pos2_iv = pos1_iv+len_iv;
2959 if (!pos2_is_uv && pos2_iv < 0) {
2960 if (!pos1_is_uv && pos1_iv < 0)
2964 else if (!pos1_is_uv && pos1_iv < 0)
2967 if ((UV)pos2_iv < (UV)pos1_iv)
2969 if ((UV)pos2_iv > curlen)
2972 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2973 *posp = (STRLEN)( (UV)pos1_iv );
2974 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2991 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2992 const bool rvalue = (GIMME_V != G_VOID);
2995 const char *repl = NULL;
2997 int num_args = PL_op->op_private & 7;
2998 bool repl_need_utf8_upgrade = FALSE;
3002 if(!(repl_sv = POPs)) num_args--;
3004 if ((len_sv = POPs)) {
3005 len_iv = SvIV(len_sv);
3006 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3011 pos1_iv = SvIV(pos_sv);
3012 pos1_is_uv = SvIOK_UV(pos_sv);
3014 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3019 if (lvalue && !repl_sv) {
3021 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3022 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3024 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3026 pos1_is_uv || pos1_iv >= 0
3027 ? (STRLEN)(UV)pos1_iv
3028 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3030 len_is_uv || len_iv > 0
3031 ? (STRLEN)(UV)len_iv
3032 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3035 PUSHs(ret); /* avoid SvSETMAGIC here */
3039 repl = SvPV_const(repl_sv, repl_len);
3042 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3043 "Attempt to use reference as lvalue in substr"
3045 tmps = SvPV_force_nomg(sv, curlen);
3046 if (DO_UTF8(repl_sv) && repl_len) {
3048 sv_utf8_upgrade_nomg(sv);
3052 else if (DO_UTF8(sv))
3053 repl_need_utf8_upgrade = TRUE;
3055 else tmps = SvPV_const(sv, curlen);
3057 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3058 if (utf8_curlen == curlen)
3061 curlen = utf8_curlen;
3067 STRLEN pos, len, byte_len, byte_pos;
3069 if (!translate_substr_offsets(
3070 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3074 byte_pos = utf8_curlen
3075 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3080 SvTAINTED_off(TARG); /* decontaminate */
3081 SvUTF8_off(TARG); /* decontaminate */
3082 sv_setpvn(TARG, tmps, byte_len);
3083 #ifdef USE_LOCALE_COLLATE
3084 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3091 SV* repl_sv_copy = NULL;
3093 if (repl_need_utf8_upgrade) {
3094 repl_sv_copy = newSVsv(repl_sv);
3095 sv_utf8_upgrade(repl_sv_copy);
3096 repl = SvPV_const(repl_sv_copy, repl_len);
3100 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3101 SvREFCNT_dec(repl_sv_copy);
3113 Perl_croak(aTHX_ "substr outside of string");
3114 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3121 const IV size = POPi;
3122 const IV offset = POPi;
3123 SV * const src = POPs;
3124 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3127 if (lvalue) { /* it's an lvalue! */
3128 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3129 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3131 LvTARG(ret) = SvREFCNT_inc_simple(src);
3132 LvTARGOFF(ret) = offset;
3133 LvTARGLEN(ret) = size;
3137 SvTAINTED_off(TARG); /* decontaminate */
3141 sv_setuv(ret, do_vecget(src, offset, size));
3157 const char *little_p;
3160 const bool is_index = PL_op->op_type == OP_INDEX;
3161 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3167 big_p = SvPV_const(big, biglen);
3168 little_p = SvPV_const(little, llen);
3170 big_utf8 = DO_UTF8(big);
3171 little_utf8 = DO_UTF8(little);
3172 if (big_utf8 ^ little_utf8) {
3173 /* One needs to be upgraded. */
3174 if (little_utf8 && !PL_encoding) {
3175 /* Well, maybe instead we might be able to downgrade the small
3177 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3180 /* If the large string is ISO-8859-1, and it's not possible to
3181 convert the small string to ISO-8859-1, then there is no
3182 way that it could be found anywhere by index. */
3187 /* At this point, pv is a malloc()ed string. So donate it to temp
3188 to ensure it will get free()d */
3189 little = temp = newSV(0);
3190 sv_usepvn(temp, pv, llen);
3191 little_p = SvPVX(little);
3194 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3197 sv_recode_to_utf8(temp, PL_encoding);
3199 sv_utf8_upgrade(temp);
3204 big_p = SvPV_const(big, biglen);
3207 little_p = SvPV_const(little, llen);
3211 if (SvGAMAGIC(big)) {
3212 /* Life just becomes a lot easier if I use a temporary here.
3213 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3214 will trigger magic and overloading again, as will fbm_instr()
3216 big = newSVpvn_flags(big_p, biglen,
3217 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3220 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3221 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3222 warn on undef, and we've already triggered a warning with the
3223 SvPV_const some lines above. We can't remove that, as we need to
3224 call some SvPV to trigger overloading early and find out if the
3226 This is all getting to messy. The API isn't quite clean enough,
3227 because data access has side effects.
3229 little = newSVpvn_flags(little_p, llen,
3230 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3231 little_p = SvPVX(little);
3235 offset = is_index ? 0 : biglen;
3237 if (big_utf8 && offset > 0)
3238 sv_pos_u2b(big, &offset, 0);
3244 else if (offset > (I32)biglen)
3246 if (!(little_p = is_index
3247 ? fbm_instr((unsigned char*)big_p + offset,
3248 (unsigned char*)big_p + biglen, little, 0)
3249 : rninstr(big_p, big_p + offset,
3250 little_p, little_p + llen)))
3253 retval = little_p - big_p;
3254 if (retval > 0 && big_utf8)
3255 sv_pos_b2u(big, &retval);
3265 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3266 SvTAINTED_off(TARG);
3267 do_sprintf(TARG, SP-MARK, MARK+1);
3268 TAINT_IF(SvTAINTED(TARG));
3280 const U8 *s = (U8*)SvPV_const(argsv, len);
3282 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3283 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3284 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3288 XPUSHu(DO_UTF8(argsv) ?
3289 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3303 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3304 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3306 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3307 && SvNV_nomg(top) < 0.0))) {
3308 if (ckWARN(WARN_UTF8)) {
3309 if (SvGMAGICAL(top)) {
3310 SV *top2 = sv_newmortal();
3311 sv_setsv_nomg(top2, top);
3314 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3315 "Invalid negative number (%"SVf") in chr", top);
3317 value = UNICODE_REPLACEMENT;
3319 value = SvUV_nomg(top);
3322 SvUPGRADE(TARG,SVt_PV);
3324 if (value > 255 && !IN_BYTES) {
3325 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3326 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3327 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3329 (void)SvPOK_only(TARG);
3338 *tmps++ = (char)value;
3340 (void)SvPOK_only(TARG);
3342 if (PL_encoding && !IN_BYTES) {
3343 sv_recode_to_utf8(TARG, PL_encoding);
3345 if (SvCUR(TARG) == 0
3346 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3347 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3352 *tmps++ = (char)value;
3368 const char *tmps = SvPV_const(left, len);
3370 if (DO_UTF8(left)) {
3371 /* If Unicode, try to downgrade.
3372 * If not possible, croak.
3373 * Yes, we made this up. */
3374 SV* const tsv = sv_2mortal(newSVsv(left));
3377 sv_utf8_downgrade(tsv, FALSE);
3378 tmps = SvPV_const(tsv, len);
3380 # ifdef USE_ITHREADS
3382 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3383 /* This should be threadsafe because in ithreads there is only
3384 * one thread per interpreter. If this would not be true,
3385 * we would need a mutex to protect this malloc. */
3386 PL_reentrant_buffer->_crypt_struct_buffer =
3387 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3388 #if defined(__GLIBC__) || defined(__EMX__)
3389 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3390 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3391 /* work around glibc-2.2.5 bug */
3392 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3396 # endif /* HAS_CRYPT_R */
3397 # endif /* USE_ITHREADS */
3399 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3401 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3407 "The crypt() function is unimplemented due to excessive paranoia.");
3411 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3412 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3414 /* Generates code to store a unicode codepoint c that is known to occupy
3415 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3416 * and p is advanced to point to the next available byte after the two bytes */
3417 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3419 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3420 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3425 /* Actually is both lcfirst() and ucfirst(). Only the first character
3426 * changes. This means that possibly we can change in-place, ie., just
3427 * take the source and change that one character and store it back, but not
3428 * if read-only etc, or if the length changes */
3433 STRLEN slen; /* slen is the byte length of the whole SV. */
3436 bool inplace; /* ? Convert first char only, in-place */
3437 bool doing_utf8 = FALSE; /* ? using utf8 */
3438 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3439 const int op_type = PL_op->op_type;
3442 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3443 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3444 * stored as UTF-8 at s. */
3445 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3446 * lowercased) character stored in tmpbuf. May be either
3447 * UTF-8 or not, but in either case is the number of bytes */
3448 bool tainted = FALSE;
3452 s = (const U8*)SvPV_nomg_const(source, slen);
3454 if (ckWARN(WARN_UNINITIALIZED))
3455 report_uninit(source);
3460 /* We may be able to get away with changing only the first character, in
3461 * place, but not if read-only, etc. Later we may discover more reasons to
3462 * not convert in-place. */
3463 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3465 /* First calculate what the changed first character should be. This affects
3466 * whether we can just swap it out, leaving the rest of the string unchanged,
3467 * or even if have to convert the dest to UTF-8 when the source isn't */
3469 if (! slen) { /* If empty */
3470 need = 1; /* still need a trailing NUL */
3473 else if (DO_UTF8(source)) { /* Is the source utf8? */
3476 if (op_type == OP_UCFIRST) {
3477 _to_utf8_title_flags(s, tmpbuf, &tculen,
3478 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3481 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3482 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3485 /* we can't do in-place if the length changes. */
3486 if (ulen != tculen) inplace = FALSE;
3487 need = slen + 1 - ulen + tculen;
3489 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3490 * latin1 is treated as caseless. Note that a locale takes
3492 ulen = 1; /* Original character is 1 byte */
3493 tculen = 1; /* Most characters will require one byte, but this will
3494 * need to be overridden for the tricky ones */
3497 if (op_type == OP_LCFIRST) {
3499 /* lower case the first letter: no trickiness for any character */
3500 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3501 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3504 else if (IN_LOCALE_RUNTIME) {
3505 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3506 * have upper and title case different
3509 else if (! IN_UNI_8_BIT) {
3510 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3511 * on EBCDIC machines whatever the
3512 * native function does */
3514 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3515 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3517 assert(tculen == 2);
3519 /* If the result is an upper Latin1-range character, it can
3520 * still be represented in one byte, which is its ordinal */
3521 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3522 *tmpbuf = (U8) title_ord;
3526 /* Otherwise it became more than one ASCII character (in
3527 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3528 * beyond Latin1, so the number of bytes changed, so can't
3529 * replace just the first character in place. */
3532 /* If the result won't fit in a byte, the entire result will
3533 * have to be in UTF-8. Assume worst case sizing in
3534 * conversion. (all latin1 characters occupy at most two bytes
3536 if (title_ord > 255) {
3538 convert_source_to_utf8 = TRUE;
3539 need = slen * 2 + 1;
3541 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3542 * (both) characters whose title case is above 255 is
3546 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3547 need = slen + 1 + 1;
3551 } /* End of use Unicode (Latin1) semantics */
3552 } /* End of changing the case of the first character */
3554 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3555 * generate the result */
3558 /* We can convert in place. This means we change just the first
3559 * character without disturbing the rest; no need to grow */
3561 s = d = (U8*)SvPV_force_nomg(source, slen);
3567 /* Here, we can't convert in place; we earlier calculated how much
3568 * space we will need, so grow to accommodate that */
3569 SvUPGRADE(dest, SVt_PV);
3570 d = (U8*)SvGROW(dest, need);
3571 (void)SvPOK_only(dest);
3578 if (! convert_source_to_utf8) {
3580 /* Here both source and dest are in UTF-8, but have to create
3581 * the entire output. We initialize the result to be the
3582 * title/lower cased first character, and then append the rest
3584 sv_setpvn(dest, (char*)tmpbuf, tculen);
3586 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3590 const U8 *const send = s + slen;
3592 /* Here the dest needs to be in UTF-8, but the source isn't,
3593 * except we earlier UTF-8'd the first character of the source
3594 * into tmpbuf. First put that into dest, and then append the
3595 * rest of the source, converting it to UTF-8 as we go. */
3597 /* Assert tculen is 2 here because the only two characters that
3598 * get to this part of the code have 2-byte UTF-8 equivalents */
3600 *d++ = *(tmpbuf + 1);
3601 s++; /* We have just processed the 1st char */
3603 for (; s < send; s++) {
3604 d = uvchr_to_utf8(d, *s);
3607 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3611 else { /* in-place UTF-8. Just overwrite the first character */
3612 Copy(tmpbuf, d, tculen, U8);
3613 SvCUR_set(dest, need - 1);
3621 else { /* Neither source nor dest are in or need to be UTF-8 */
3623 if (IN_LOCALE_RUNTIME) {
3627 if (inplace) { /* in-place, only need to change the 1st char */
3630 else { /* Not in-place */
3632 /* Copy the case-changed character(s) from tmpbuf */
3633 Copy(tmpbuf, d, tculen, U8);
3634 d += tculen - 1; /* Code below expects d to point to final
3635 * character stored */
3638 else { /* empty source */
3639 /* See bug #39028: Don't taint if empty */
3643 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3644 * the destination to retain that flag */
3648 if (!inplace) { /* Finish the rest of the string, unchanged */
3649 /* This will copy the trailing NUL */
3650 Copy(s + 1, d + 1, slen, U8);
3651 SvCUR_set(dest, need - 1);
3654 if (dest != source && SvTAINTED(source))
3660 /* There's so much setup/teardown code common between uc and lc, I wonder if
3661 it would be worth merging the two, and just having a switch outside each
3662 of the three tight loops. There is less and less commonality though */
3676 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3677 && SvTEMP(source) && !DO_UTF8(source)
3678 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3680 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3681 * make the loop tight, so we overwrite the source with the dest before
3682 * looking at it, and we need to look at the original source
3683 * afterwards. There would also need to be code added to handle
3684 * switching to not in-place in midstream if we run into characters
3685 * that change the length.
3688 s = d = (U8*)SvPV_force_nomg(source, len);
3695 /* The old implementation would copy source into TARG at this point.
3696 This had the side effect that if source was undef, TARG was now
3697 an undefined SV with PADTMP set, and they don't warn inside
3698 sv_2pv_flags(). However, we're now getting the PV direct from
3699 source, which doesn't have PADTMP set, so it would warn. Hence the
3703 s = (const U8*)SvPV_nomg_const(source, len);
3705 if (ckWARN(WARN_UNINITIALIZED))
3706 report_uninit(source);
3712 SvUPGRADE(dest, SVt_PV);
3713 d = (U8*)SvGROW(dest, min);
3714 (void)SvPOK_only(dest);
3719 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3720 to check DO_UTF8 again here. */
3722 if (DO_UTF8(source)) {
3723 const U8 *const send = s + len;
3724 U8 tmpbuf[UTF8_MAXBYTES+1];
3725 bool tainted = FALSE;
3727 /* All occurrences of these are to be moved to follow any other marks.
3728 * This is context-dependent. We may not be passed enough context to
3729 * move the iota subscript beyond all of them, but we do the best we can
3730 * with what we're given. The result is always better than if we
3731 * hadn't done this. And, the problem would only arise if we are
3732 * passed a character without all its combining marks, which would be
3733 * the caller's mistake. The information this is based on comes from a
3734 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3735 * itself) and so can't be checked properly to see if it ever gets
3736 * revised. But the likelihood of it changing is remote */
3737 bool in_iota_subscript = FALSE;
3743 if (in_iota_subscript && ! is_utf8_mark(s)) {
3745 /* A non-mark. Time to output the iota subscript */
3746 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3747 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3749 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3750 in_iota_subscript = FALSE;
3753 /* Then handle the current character. Get the changed case value
3754 * and copy it to the output buffer */
3757 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3758 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3759 if (uv == GREEK_CAPITAL_LETTER_IOTA
3760 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3762 in_iota_subscript = TRUE;
3765 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3766 /* If the eventually required minimum size outgrows the
3767 * available space, we need to grow. */
3768 const UV o = d - (U8*)SvPVX_const(dest);
3770 /* If someone uppercases one million U+03B0s we SvGROW()
3771 * one million times. Or we could try guessing how much to
3772 * allocate without allocating too much. Such is life.
3773 * See corresponding comment in lc code for another option
3776 d = (U8*)SvPVX(dest) + o;
3778 Copy(tmpbuf, d, ulen, U8);
3783 if (in_iota_subscript) {
3784 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3789 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3795 else { /* Not UTF-8 */
3797 const U8 *const send = s + len;
3799 /* Use locale casing if in locale; regular style if not treating
3800 * latin1 as having case; otherwise the latin1 casing. Do the
3801 * whole thing in a tight loop, for speed, */
3802 if (IN_LOCALE_RUNTIME) {
3805 for (; s < send; d++, s++)
3806 *d = toUPPER_LC(*s);
3808 else if (! IN_UNI_8_BIT) {
3809 for (; s < send; d++, s++) {
3814 for (; s < send; d++, s++) {
3815 *d = toUPPER_LATIN1_MOD(*s);
3816 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3818 /* The mainstream case is the tight loop above. To avoid
3819 * extra tests in that, all three characters that require
3820 * special handling are mapped by the MOD to the one tested
3822 * Use the source to distinguish between the three cases */
3824 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3826 /* uc() of this requires 2 characters, but they are
3827 * ASCII. If not enough room, grow the string */
3828 if (SvLEN(dest) < ++min) {
3829 const UV o = d - (U8*)SvPVX_const(dest);
3831 d = (U8*)SvPVX(dest) + o;
3833 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3834 continue; /* Back to the tight loop; still in ASCII */
3837 /* The other two special handling characters have their
3838 * upper cases outside the latin1 range, hence need to be
3839 * in UTF-8, so the whole result needs to be in UTF-8. So,
3840 * here we are somewhere in the middle of processing a
3841 * non-UTF-8 string, and realize that we will have to convert
3842 * the whole thing to UTF-8. What to do? There are
3843 * several possibilities. The simplest to code is to
3844 * convert what we have so far, set a flag, and continue on
3845 * in the loop. The flag would be tested each time through
3846 * the loop, and if set, the next character would be
3847 * converted to UTF-8 and stored. But, I (khw) didn't want
3848 * to slow down the mainstream case at all for this fairly
3849 * rare case, so I didn't want to add a test that didn't
3850 * absolutely have to be there in the loop, besides the
3851 * possibility that it would get too complicated for
3852 * optimizers to deal with. Another possibility is to just
3853 * give up, convert the source to UTF-8, and restart the
3854 * function that way. Another possibility is to convert
3855 * both what has already been processed and what is yet to
3856 * come separately to UTF-8, then jump into the loop that
3857 * handles UTF-8. But the most efficient time-wise of the
3858 * ones I could think of is what follows, and turned out to
3859 * not require much extra code. */
3861 /* Convert what we have so far into UTF-8, telling the
3862 * function that we know it should be converted, and to
3863 * allow extra space for what we haven't processed yet.
3864 * Assume the worst case space requirements for converting
3865 * what we haven't processed so far: that it will require
3866 * two bytes for each remaining source character, plus the
3867 * NUL at the end. This may cause the string pointer to
3868 * move, so re-find it. */
3870 len = d - (U8*)SvPVX_const(dest);
3871 SvCUR_set(dest, len);
3872 len = sv_utf8_upgrade_flags_grow(dest,
3873 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3875 d = (U8*)SvPVX(dest) + len;
3877 /* Now process the remainder of the source, converting to
3878 * upper and UTF-8. If a resulting byte is invariant in
3879 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3880 * append it to the output. */
3881 for (; s < send; s++) {
3882 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3886 /* Here have processed the whole source; no need to continue
3887 * with the outer loop. Each character has been converted
3888 * to upper case and converted to UTF-8 */
3891 } /* End of processing all latin1-style chars */
3892 } /* End of processing all chars */
3893 } /* End of source is not empty */
3895 if (source != dest) {
3896 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3897 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3899 } /* End of isn't utf8 */
3900 if (dest != source && SvTAINTED(source))
3919 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3920 && SvTEMP(source) && !DO_UTF8(source)) {
3922 /* We can convert in place, as lowercasing anything in the latin1 range
3923 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3925 s = d = (U8*)SvPV_force_nomg(source, len);
3932 /* The old implementation would copy source into TARG at this point.
3933 This had the side effect that if source was undef, TARG was now
3934 an undefined SV with PADTMP set, and they don't warn inside
3935 sv_2pv_flags(). However, we're now getting the PV direct from
3936 source, which doesn't have PADTMP set, so it would warn. Hence the
3940 s = (const U8*)SvPV_nomg_const(source, len);
3942 if (ckWARN(WARN_UNINITIALIZED))
3943 report_uninit(source);
3949 SvUPGRADE(dest, SVt_PV);
3950 d = (U8*)SvGROW(dest, min);
3951 (void)SvPOK_only(dest);
3956 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3957 to check DO_UTF8 again here. */
3959 if (DO_UTF8(source)) {
3960 const U8 *const send = s + len;
3961 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3962 bool tainted = FALSE;
3965 const STRLEN u = UTF8SKIP(s);
3968 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3969 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3971 /* Here is where we would do context-sensitive actions. See the
3972 * commit message for this comment for why there isn't any */
3974 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3976 /* If the eventually required minimum size outgrows the
3977 * available space, we need to grow. */
3978 const UV o = d - (U8*)SvPVX_const(dest);
3980 /* If someone lowercases one million U+0130s we SvGROW() one
3981 * million times. Or we could try guessing how much to
3982 * allocate without allocating too much. Such is life.
3983 * Another option would be to grow an extra byte or two more
3984 * each time we need to grow, which would cut down the million
3985 * to 500K, with little waste */
3987 d = (U8*)SvPVX(dest) + o;
3990 /* Copy the newly lowercased letter to the output buffer we're
3992 Copy(tmpbuf, d, ulen, U8);
3995 } /* End of looping through the source string */
3998 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4003 } else { /* Not utf8 */
4005 const U8 *const send = s + len;
4007 /* Use locale casing if in locale; regular style if not treating
4008 * latin1 as having case; otherwise the latin1 casing. Do the
4009 * whole thing in a tight loop, for speed, */
4010 if (IN_LOCALE_RUNTIME) {
4013 for (; s < send; d++, s++)
4014 *d = toLOWER_LC(*s);
4016 else if (! IN_UNI_8_BIT) {
4017 for (; s < send; d++, s++) {
4022 for (; s < send; d++, s++) {
4023 *d = toLOWER_LATIN1(*s);
4027 if (source != dest) {
4029 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4032 if (dest != source && SvTAINTED(source))
4041 SV * const sv = TOPs;
4043 const char *s = SvPV_const(sv,len);
4045 SvUTF8_off(TARG); /* decontaminate */
4048 SvUPGRADE(TARG, SVt_PV);
4049 SvGROW(TARG, (len * 2) + 1);
4053 STRLEN ulen = UTF8SKIP(s);
4054 bool to_quote = FALSE;
4056 if (UTF8_IS_INVARIANT(*s)) {
4057 if (_isQUOTEMETA(*s)) {
4061 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4063 /* In locale, we quote all non-ASCII Latin1 chars.
4064 * Otherwise use the quoting rules */
4065 if (IN_LOCALE_RUNTIME
4066 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4071 else if (is_QUOTEMETA_high(s)) {
4086 else if (IN_UNI_8_BIT) {
4088 if (_isQUOTEMETA(*s))
4094 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4095 * including everything above ASCII */
4097 if (!isWORDCHAR_A(*s))
4103 SvCUR_set(TARG, d - SvPVX_const(TARG));
4104 (void)SvPOK_only_UTF8(TARG);
4107 sv_setpvn(TARG, s, len);
4124 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4125 const bool full_folding = TRUE;
4126 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4127 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4129 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4130 * You are welcome(?) -Hugmeir
4138 s = (const U8*)SvPV_nomg_const(source, len);
4140 if (ckWARN(WARN_UNINITIALIZED))
4141 report_uninit(source);
4148 SvUPGRADE(dest, SVt_PV);
4149 d = (U8*)SvGROW(dest, min);
4150 (void)SvPOK_only(dest);
4155 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4156 bool tainted = FALSE;
4158 const STRLEN u = UTF8SKIP(s);
4161 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4163 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4164 const UV o = d - (U8*)SvPVX_const(dest);
4166 d = (U8*)SvPVX(dest) + o;
4169 Copy(tmpbuf, d, ulen, U8);
4178 } /* Unflagged string */
4180 /* For locale, bytes, and nothing, the behavior is supposed to be the
4183 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4186 for (; s < send; d++, s++)
4187 *d = toLOWER_LC(*s);
4189 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4190 for (; s < send; d++, s++)
4194 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4195 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4196 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4197 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4200 for (; s < send; d++, s++) {
4201 if (*s == MICRO_SIGN) {
4202 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4203 * is outside of the latin-1 range. There's a couple of ways to
4204 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4205 * What we do here is upgrade what we had already casefolded,
4206 * then enter an inner loop that appends the rest of the characters
4209 len = d - (U8*)SvPVX_const(dest);
4210 SvCUR_set(dest, len);
4211 len = sv_utf8_upgrade_flags_grow(dest,
4212 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4213 /* The max expansion for latin1
4214 * chars is 1 byte becomes 2 */
4216 d = (U8*)SvPVX(dest) + len;
4218 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4220 for (; s < send; s++) {
4222 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4223 if UNI_IS_INVARIANT(fc) {
4224 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4232 Copy(tmpbuf, d, ulen, U8);
4238 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4239 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4240 * which may require growing the SV.
4242 if (SvLEN(dest) < ++min) {
4243 const UV o = d - (U8*)SvPVX_const(dest);
4245 d = (U8*)SvPVX(dest) + o;
4250 else { /* If it's not one of those two, the fold is their lower case */
4251 *d = toLOWER_LATIN1(*s);
4257 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4259 if (SvTAINTED(source))
4269 dVAR; dSP; dMARK; dORIGMARK;
4270 AV *const av = MUTABLE_AV(POPs);
4271 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4273 if (SvTYPE(av) == SVt_PVAV) {
4274 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4275 bool can_preserve = FALSE;
4281 can_preserve = SvCANEXISTDELETE(av);
4284 if (lval && localizing) {
4287 for (svp = MARK + 1; svp <= SP; svp++) {
4288 const I32 elem = SvIV(*svp);
4292 if (max > AvMAX(av))
4296 while (++MARK <= SP) {
4298 I32 elem = SvIV(*MARK);
4299 bool preeminent = TRUE;
4301 if (localizing && can_preserve) {
4302 /* If we can determine whether the element exist,
4303 * Try to preserve the existenceness of a tied array
4304 * element by using EXISTS and DELETE if possible.
4305 * Fallback to FETCH and STORE otherwise. */
4306 preeminent = av_exists(av, elem);
4309 svp = av_fetch(av, elem, lval);
4311 if (!svp || *svp == &PL_sv_undef)
4312 DIE(aTHX_ PL_no_aelem, elem);
4315 save_aelem(av, elem, svp);
4317 SAVEADELETE(av, elem);
4320 *MARK = svp ? *svp : &PL_sv_undef;
4323 if (GIMME != G_ARRAY) {
4325 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4331 /* Smart dereferencing for keys, values and each */
4343 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4348 "Type of argument to %s must be unblessed hashref or arrayref",
4349 PL_op_desc[PL_op->op_type] );
4352 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4354 "Can't modify %s in %s",
4355 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4358 /* Delegate to correct function for op type */
4360 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4361 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4364 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4372 AV *array = MUTABLE_AV(POPs);
4373 const I32 gimme = GIMME_V;
4374 IV *iterp = Perl_av_iter_p(aTHX_ array);
4375 const IV current = (*iterp)++;
4377 if (current > av_len(array)) {
4379 if (gimme == G_SCALAR)
4387 if (gimme == G_ARRAY) {
4388 SV **const element = av_fetch(array, current, 0);
4389 PUSHs(element ? *element : &PL_sv_undef);
4398 AV *array = MUTABLE_AV(POPs);
4399 const I32 gimme = GIMME_V;
4401 *Perl_av_iter_p(aTHX_ array) = 0;
4403 if (gimme == G_SCALAR) {
4405 PUSHi(av_len(array) + 1);
4407 else if (gimme == G_ARRAY) {
4408 IV n = Perl_av_len(aTHX_ array);
4413 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4414 for (i = 0; i <= n; i++) {
4419 for (i = 0; i <= n; i++) {
4420 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4421 PUSHs(elem ? *elem : &PL_sv_undef);
4428 /* Associative arrays. */
4434 HV * hash = MUTABLE_HV(POPs);
4436 const I32 gimme = GIMME_V;
4439 /* might clobber stack_sp */
4440 entry = hv_iternext(hash);
4445 SV* const sv = hv_iterkeysv(entry);
4446 PUSHs(sv); /* won't clobber stack_sp */
4447 if (gimme == G_ARRAY) {
4450 /* might clobber stack_sp */
4451 val = hv_iterval(hash, entry);
4456 else if (gimme == G_SCALAR)
4463 S_do_delete_local(pTHX)
4467 const I32 gimme = GIMME_V;
4470 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4471 SV *unsliced_keysv = sliced ? NULL : POPs;
4472 SV * const osv = POPs;
4473 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4475 const bool tied = SvRMAGICAL(osv)
4476 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4477 const bool can_preserve = SvCANEXISTDELETE(osv);
4478 const U32 type = SvTYPE(osv);
4479 SV ** const end = sliced ? SP : &unsliced_keysv;
4481 if (type == SVt_PVHV) { /* hash element */
4482 HV * const hv = MUTABLE_HV(osv);
4483 while (++MARK <= end) {
4484 SV * const keysv = *MARK;
4486 bool preeminent = TRUE;
4488 preeminent = hv_exists_ent(hv, keysv, 0);
4490 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4497 sv = hv_delete_ent(hv, keysv, 0, 0);
4498 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4501 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4502 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4504 *MARK = sv_mortalcopy(sv);
4510 SAVEHDELETE(hv, keysv);
4511 *MARK = &PL_sv_undef;
4515 else if (type == SVt_PVAV) { /* array element */
4516 if (PL_op->op_flags & OPf_SPECIAL) {
4517 AV * const av = MUTABLE_AV(osv);
4518 while (++MARK <= end) {
4519 I32 idx = SvIV(*MARK);
4521 bool preeminent = TRUE;
4523 preeminent = av_exists(av, idx);
4525 SV **svp = av_fetch(av, idx, 1);
4532 sv = av_delete(av, idx, 0);
4533 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4536 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4538 *MARK = sv_mortalcopy(sv);
4544 SAVEADELETE(av, idx);
4545 *MARK = &PL_sv_undef;
4550 DIE(aTHX_ "panic: avhv_delete no longer supported");
4553 DIE(aTHX_ "Not a HASH reference");
4555 if (gimme == G_VOID)
4557 else if (gimme == G_SCALAR) {
4562 *++MARK = &PL_sv_undef;
4566 else if (gimme != G_VOID)
4567 PUSHs(unsliced_keysv);
4579 if (PL_op->op_private & OPpLVAL_INTRO)
4580 return do_delete_local();
4583 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4585 if (PL_op->op_private & OPpSLICE) {
4587 HV * const hv = MUTABLE_HV(POPs);
4588 const U32 hvtype = SvTYPE(hv);
4589 if (hvtype == SVt_PVHV) { /* hash element */
4590 while (++MARK <= SP) {
4591 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4592 *MARK = sv ? sv : &PL_sv_undef;
4595 else if (hvtype == SVt_PVAV) { /* array element */
4596 if (PL_op->op_flags & OPf_SPECIAL) {
4597 while (++MARK <= SP) {
4598 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4599 *MARK = sv ? sv : &PL_sv_undef;
4604 DIE(aTHX_ "Not a HASH reference");
4607 else if (gimme == G_SCALAR) {
4612 *++MARK = &PL_sv_undef;
4618 HV * const hv = MUTABLE_HV(POPs);
4620 if (SvTYPE(hv) == SVt_PVHV)
4621 sv = hv_delete_ent(hv, keysv, discard, 0);
4622 else if (SvTYPE(hv) == SVt_PVAV) {
4623 if (PL_op->op_flags & OPf_SPECIAL)
4624 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4626 DIE(aTHX_ "panic: avhv_delete no longer supported");
4629 DIE(aTHX_ "Not a HASH reference");
4645 if (PL_op->op_private & OPpEXISTS_SUB) {
4647 SV * const sv = POPs;
4648 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4651 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4656 hv = MUTABLE_HV(POPs);
4657 if (SvTYPE(hv) == SVt_PVHV) {
4658 if (hv_exists_ent(hv, tmpsv, 0))
4661 else if (SvTYPE(hv) == SVt_PVAV) {
4662 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4663 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4668 DIE(aTHX_ "Not a HASH reference");
4675 dVAR; dSP; dMARK; dORIGMARK;
4676 HV * const hv = MUTABLE_HV(POPs);
4677 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4678 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4679 bool can_preserve = FALSE;
4685 if (SvCANEXISTDELETE(hv))
4686 can_preserve = TRUE;
4689 while (++MARK <= SP) {
4690 SV * const keysv = *MARK;
4693 bool preeminent = TRUE;
4695 if (localizing && can_preserve) {
4696 /* If we can determine whether the element exist,
4697 * try to preserve the existenceness of a tied hash
4698 * element by using EXISTS and DELETE if possible.
4699 * Fallback to FETCH and STORE otherwise. */
4700 preeminent = hv_exists_ent(hv, keysv, 0);
4703 he = hv_fetch_ent(hv, keysv, lval, 0);
4704 svp = he ? &HeVAL(he) : NULL;
4707 if (!svp || !*svp || *svp == &PL_sv_undef) {
4708 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4711 if (HvNAME_get(hv) && isGV(*svp))
4712 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4713 else if (preeminent)
4714 save_helem_flags(hv, keysv, svp,
4715 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4717 SAVEHDELETE(hv, keysv);
4720 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4722 if (GIMME != G_ARRAY) {
4724 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4730 /* List operators. */
4735 if (GIMME != G_ARRAY) {
4737 *MARK = *SP; /* unwanted list, return last item */
4739 *MARK = &PL_sv_undef;
4749 SV ** const lastrelem = PL_stack_sp;
4750 SV ** const lastlelem = PL_stack_base + POPMARK;
4751 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4752 SV ** const firstrelem = lastlelem + 1;
4753 I32 is_something_there = FALSE;
4755 const I32 max = lastrelem - lastlelem;
4758 if (GIMME != G_ARRAY) {
4759 I32 ix = SvIV(*lastlelem);
4762 if (ix < 0 || ix >= max)
4763 *firstlelem = &PL_sv_undef;
4765 *firstlelem = firstrelem[ix];
4771 SP = firstlelem - 1;
4775 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4776 I32 ix = SvIV(*lelem);
4779 if (ix < 0 || ix >= max)
4780 *lelem = &PL_sv_undef;
4782 is_something_there = TRUE;
4783 if (!(*lelem = firstrelem[ix]))
4784 *lelem = &PL_sv_undef;
4787 if (is_something_there)
4790 SP = firstlelem - 1;
4796 dVAR; dSP; dMARK; dORIGMARK;
4797 const I32 items = SP - MARK;
4798 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4799 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4800 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4801 ? newRV_noinc(av) : av);
4807 dVAR; dSP; dMARK; dORIGMARK;
4808 HV* const hv = (HV *)sv_2mortal((SV *)newHV());
4812 (MARK++, SvGMAGICAL(*MARK) ? sv_mortalcopy(*MARK) : *MARK);
4819 sv_setsv(val, *MARK);
4823 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4826 (void)hv_store_ent(hv,key,val,0);
4829 if (PL_op->op_flags & OPf_SPECIAL)
4830 mXPUSHs(newRV_inc(MUTABLE_SV(hv)));
4831 else XPUSHs(MUTABLE_SV(hv));
4836 S_deref_plain_array(pTHX_ AV *ary)
4838 if (SvTYPE(ary) == SVt_PVAV) return ary;
4839 SvGETMAGIC((SV *)ary);
4840 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4841 Perl_die(aTHX_ "Not an ARRAY reference");
4842 else if (SvOBJECT(SvRV(ary)))
4843 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4844 return (AV *)SvRV(ary);
4847 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4848 # define DEREF_PLAIN_ARRAY(ary) \
4851 SvTYPE(aRrRay) == SVt_PVAV \
4853 : S_deref_plain_array(aTHX_ aRrRay); \
4856 # define DEREF_PLAIN_ARRAY(ary) \
4858 PL_Sv = (SV *)(ary), \
4859 SvTYPE(PL_Sv) == SVt_PVAV \
4861 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4867 dVAR; dSP; dMARK; dORIGMARK;
4868 int num_args = (SP - MARK);
4869 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4878 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4881 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4882 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4889 offset = i = SvIV(*MARK);
4891 offset += AvFILLp(ary) + 1;
4893 DIE(aTHX_ PL_no_aelem, i);
4895 length = SvIVx(*MARK++);
4897 length += AvFILLp(ary) - offset + 1;
4903 length = AvMAX(ary) + 1; /* close enough to infinity */
4907 length = AvMAX(ary) + 1;
4909 if (offset > AvFILLp(ary) + 1) {
4911 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4912 offset = AvFILLp(ary) + 1;
4914 after = AvFILLp(ary) + 1 - (offset + length);
4915 if (after < 0) { /* not that much array */
4916 length += after; /* offset+length now in array */
4922 /* At this point, MARK .. SP-1 is our new LIST */
4925 diff = newlen - length;
4926 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4929 /* make new elements SVs now: avoid problems if they're from the array */
4930 for (dst = MARK, i = newlen; i; i--) {
4931 SV * const h = *dst;
4932 *dst++ = newSVsv(h);
4935 if (diff < 0) { /* shrinking the area */
4936 SV **tmparyval = NULL;
4938 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4939 Copy(MARK, tmparyval, newlen, SV*);
4942 MARK = ORIGMARK + 1;
4943 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4944 MEXTEND(MARK, length);
4945 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4947 EXTEND_MORTAL(length);
4948 for (i = length, dst = MARK; i; i--) {
4949 sv_2mortal(*dst); /* free them eventually */
4956 *MARK = AvARRAY(ary)[offset+length-1];
4959 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4960 SvREFCNT_dec(*dst++); /* free them now */
4963 AvFILLp(ary) += diff;
4965 /* pull up or down? */
4967 if (offset < after) { /* easier to pull up */
4968 if (offset) { /* esp. if nothing to pull */
4969 src = &AvARRAY(ary)[offset-1];
4970 dst = src - diff; /* diff is negative */
4971 for (i = offset; i > 0; i--) /* can't trust Copy */
4975 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4979 if (after) { /* anything to pull down? */
4980 src = AvARRAY(ary) + offset + length;
4981 dst = src + diff; /* diff is negative */
4982 Move(src, dst, after, SV*);
4984 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4985 /* avoid later double free */
4989 dst[--i] = &PL_sv_undef;
4992 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4993 Safefree(tmparyval);
4996 else { /* no, expanding (or same) */
4997 SV** tmparyval = NULL;
4999 Newx(tmparyval, length, SV*); /* so remember deletion */
5000 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5003 if (diff > 0) { /* expanding */
5004 /* push up or down? */
5005 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5009 Move(src, dst, offset, SV*);
5011 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5013 AvFILLp(ary) += diff;
5016 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5017 av_extend(ary, AvFILLp(ary) + diff);
5018 AvFILLp(ary) += diff;
5021 dst = AvARRAY(ary) + AvFILLp(ary);
5023 for (i = after; i; i--) {
5031 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5034 MARK = ORIGMARK + 1;
5035 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5037 Copy(tmparyval, MARK, length, SV*);
5039 EXTEND_MORTAL(length);
5040 for (i = length, dst = MARK; i; i--) {
5041 sv_2mortal(*dst); /* free them eventually */
5048 else if (length--) {
5049 *MARK = tmparyval[length];
5052 while (length-- > 0)
5053 SvREFCNT_dec(tmparyval[length]);
5057 *MARK = &PL_sv_undef;
5058 Safefree(tmparyval);
5062 mg_set(MUTABLE_SV(ary));
5070 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5071 AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5072 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5075 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5078 ENTER_with_name("call_PUSH");
5079 call_method("PUSH",G_SCALAR|G_DISCARD);
5080 LEAVE_with_name("call_PUSH");
5084 if (SvREADONLY(ary) && MARK < SP) Perl_croak_no_modify(aTHX);
5085 PL_delaymagic = DM_DELAY;
5086 for (++MARK; MARK <= SP; MARK++) {
5088 if (*MARK) SvGETMAGIC(*MARK);
5091 sv_setsv_nomg(sv, *MARK);
5092 av_store(ary, AvFILLp(ary)+1, sv);
5094 if (PL_delaymagic & DM_ARRAY_ISA)
5095 mg_set(MUTABLE_SV(ary));
5100 if (OP_GIMME(PL_op, 0) != G_VOID) {
5101 PUSHi( AvFILL(ary) + 1 );
5110 AV * const av = PL_op->op_flags & OPf_SPECIAL
5111 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5112 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5116 (void)sv_2mortal(sv);
5123 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5124 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5125 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5128 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5131 ENTER_with_name("call_UNSHIFT");
5132 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5133 LEAVE_with_name("call_UNSHIFT");
5138 av_unshift(ary, SP - MARK);
5140 SV * const sv = newSVsv(*++MARK);
5141 (void)av_store(ary, i++, sv);
5145 if (OP_GIMME(PL_op, 0) != G_VOID) {
5146 PUSHi( AvFILL(ary) + 1 );
5155 if (GIMME == G_ARRAY) {
5156 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5160 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5161 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5162 av = MUTABLE_AV((*SP));
5163 /* In-place reversing only happens in void context for the array
5164 * assignment. We don't need to push anything on the stack. */
5167 if (SvMAGICAL(av)) {
5169 SV *tmp = sv_newmortal();
5170 /* For SvCANEXISTDELETE */
5173 bool can_preserve = SvCANEXISTDELETE(av);
5175 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5179 if (!av_exists(av, i)) {
5180 if (av_exists(av, j)) {
5181 SV *sv = av_delete(av, j, 0);
5182 begin = *av_fetch(av, i, TRUE);
5183 sv_setsv_mg(begin, sv);
5187 else if (!av_exists(av, j)) {
5188 SV *sv = av_delete(av, i, 0);
5189 end = *av_fetch(av, j, TRUE);
5190 sv_setsv_mg(end, sv);
5195 begin = *av_fetch(av, i, TRUE);
5196 end = *av_fetch(av, j, TRUE);
5197 sv_setsv(tmp, begin);
5198 sv_setsv_mg(begin, end);
5199 sv_setsv_mg(end, tmp);
5203 SV **begin = AvARRAY(av);
5206 SV **end = begin + AvFILLp(av);
5208 while (begin < end) {
5209 SV * const tmp = *begin;
5220 SV * const tmp = *MARK;
5224 /* safe as long as stack cannot get extended in the above */
5235 SvUTF8_off(TARG); /* decontaminate */
5237 do_join(TARG, &PL_sv_no, MARK, SP);
5239 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5240 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5241 report_uninit(TARG);
5244 up = SvPV_force(TARG, len);
5246 if (DO_UTF8(TARG)) { /* first reverse each character */
5247 U8* s = (U8*)SvPVX(TARG);
5248 const U8* send = (U8*)(s + len);
5250 if (UTF8_IS_INVARIANT(*s)) {
5255 if (!utf8_to_uvchr_buf(s, send, 0))
5259 down = (char*)(s - 1);
5260 /* reverse this character */
5264 *down-- = (char)tmp;
5270 down = SvPVX(TARG) + len - 1;
5274 *down-- = (char)tmp;
5276 (void)SvPOK_only_UTF8(TARG);
5288 IV limit = POPi; /* note, negative is forever */
5289 SV * const sv = POPs;
5291 const char *s = SvPV_const(sv, len);
5292 const bool do_utf8 = DO_UTF8(sv);
5293 const bool skipwhite = PL_op->op_flags & OPf_SPECIAL;
5294 const char *strend = s + len;
5300 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5301 I32 maxiters = slen + 10;
5302 I32 trailing_empty = 0;
5304 const I32 origlimit = limit;
5307 const I32 gimme = GIMME_V;
5309 const I32 oldsave = PL_savestack_ix;
5310 U32 make_mortal = SVs_TEMP;
5315 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5320 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5323 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5324 (RX_EXTFLAGS(rx) & RXf_WHITE || skipwhite));
5326 RX_MATCH_UTF8_set(rx, do_utf8);
5329 if (pm->op_pmreplrootu.op_pmtargetoff) {
5330 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5333 if (pm->op_pmreplrootu.op_pmtargetgv) {
5334 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5339 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5345 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5347 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5354 for (i = AvFILLp(ary); i >= 0; i--)
5355 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5357 /* temporarily switch stacks */
5358 SAVESWITCHSTACK(PL_curstack, ary);
5362 base = SP - PL_stack_base;
5366 while (*s == ' ' || is_utf8_space((U8*)s))
5369 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5370 while (isSPACE_LC(*s))
5378 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5382 gimme_scalar = gimme == G_SCALAR && !ary;
5385 limit = maxiters + 2;
5386 if (RX_EXTFLAGS(rx) & RXf_WHITE || skipwhite) {
5389 /* this one uses 'm' and is a negative test */
5391 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5392 const int t = UTF8SKIP(m);
5393 /* is_utf8_space returns FALSE for malform utf8 */
5400 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5401 while (m < strend && !isSPACE_LC(*m))
5404 while (m < strend && !isSPACE(*m))
5417 dstr = newSVpvn_flags(s, m-s,
5418 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5422 /* skip the whitespace found last */
5424 s = m + UTF8SKIP(m);
5428 /* this one uses 's' and is a positive test */
5430 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5433 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5434 while (s < strend && isSPACE_LC(*s))
5437 while (s < strend && isSPACE(*s))
5442 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5444 for (m = s; m < strend && *m != '\n'; m++)
5457 dstr = newSVpvn_flags(s, m-s,
5458 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5464 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5466 Pre-extend the stack, either the number of bytes or
5467 characters in the string or a limited amount, triggered by:
5469 my ($x, $y) = split //, $str;
5473 if (!gimme_scalar) {
5474 const U32 items = limit - 1;
5483 /* keep track of how many bytes we skip over */
5493 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5506 dstr = newSVpvn(s, 1);
5522 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5523 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5524 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5525 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5526 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5527 SV * const csv = CALLREG_INTUIT_STRING(rx);
5529 len = RX_MINLENRET(rx);
5530 if (len == 1 && !RX_UTF8(rx) && !tail) {
5531 const char c = *SvPV_nolen_const(csv);
5533 for (m = s; m < strend && *m != c; m++)
5544 dstr = newSVpvn_flags(s, m-s,
5545 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5548 /* The rx->minlen is in characters but we want to step
5549 * s ahead by bytes. */
5551 s = (char*)utf8_hop((U8*)m, len);
5553 s = m + len; /* Fake \n at the end */
5557 while (s < strend && --limit &&
5558 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5559 csv, multiline ? FBMrf_MULTILINE : 0)) )
5568 dstr = newSVpvn_flags(s, m-s,
5569 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5572 /* The rx->minlen is in characters but we want to step
5573 * s ahead by bytes. */
5575 s = (char*)utf8_hop((U8*)m, len);
5577 s = m + len; /* Fake \n at the end */
5582 maxiters += slen * RX_NPARENS(rx);
5583 while (s < strend && --limit)
5587 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5590 if (rex_return == 0)
5592 TAINT_IF(RX_MATCH_TAINTED(rx));
5593 /* we never pass the REXEC_COPY_STR flag, so it should
5594 * never get copied */
5595 assert(!RX_MATCH_COPIED(rx));
5596 m = RX_OFFS(rx)[0].start + orig;
5605 dstr = newSVpvn_flags(s, m-s,
5606 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5609 if (RX_NPARENS(rx)) {
5611 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5612 s = RX_OFFS(rx)[i].start + orig;
5613 m = RX_OFFS(rx)[i].end + orig;
5615 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5616 parens that didn't match -- they should be set to
5617 undef, not the empty string */
5625 if (m >= orig && s >= orig) {
5626 dstr = newSVpvn_flags(s, m-s,
5627 (do_utf8 ? SVf_UTF8 : 0)
5631 dstr = &PL_sv_undef; /* undef, not "" */
5637 s = RX_OFFS(rx)[0].end + orig;
5641 if (!gimme_scalar) {
5642 iters = (SP - PL_stack_base) - base;
5644 if (iters > maxiters)
5645 DIE(aTHX_ "Split loop");
5647 /* keep field after final delim? */
5648 if (s < strend || (iters && origlimit)) {
5649 if (!gimme_scalar) {
5650 const STRLEN l = strend - s;
5651 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5656 else if (!origlimit) {
5658 iters -= trailing_empty;
5660 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5661 if (TOPs && !make_mortal)
5663 *SP-- = &PL_sv_undef;
5670 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5674 if (SvSMAGICAL(ary)) {
5676 mg_set(MUTABLE_SV(ary));
5679 if (gimme == G_ARRAY) {
5681 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5688 ENTER_with_name("call_PUSH");
5689 call_method("PUSH",G_SCALAR|G_DISCARD);
5690 LEAVE_with_name("call_PUSH");
5692 if (gimme == G_ARRAY) {
5694 /* EXTEND should not be needed - we just popped them */
5696 for (i=0; i < iters; i++) {
5697 SV **svp = av_fetch(ary, i, FALSE);
5698 PUSHs((svp) ? *svp : &PL_sv_undef);
5705 if (gimme == G_ARRAY)
5717 SV *const sv = PAD_SVl(PL_op->op_targ);
5719 if (SvPADSTALE(sv)) {
5722 RETURNOP(cLOGOP->op_other);
5724 RETURNOP(cLOGOP->op_next);
5734 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5735 || SvTYPE(retsv) == SVt_PVCV) {
5736 retsv = refto(retsv);
5743 PP(unimplemented_op)
5746 const Optype op_type = PL_op->op_type;
5747 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5748 with out of range op numbers - it only "special" cases op_custom.
5749 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5750 if we get here for a custom op then that means that the custom op didn't
5751 have an implementation. Given that OP_NAME() looks up the custom op
5752 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5753 registers &PL_unimplemented_op as the address of their custom op.
5754 NULL doesn't generate a useful error message. "custom" does. */
5755 const char *const name = op_type >= OP_max
5756 ? "[out of range]" : PL_op_name[PL_op->op_type];
5757 if(OP_IS_SOCKET(op_type))
5758 DIE(aTHX_ PL_no_sock_func, name);
5759 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5762 /* For sorting out arguments passed to a &CORE:: subroutine */
5766 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5767 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5768 AV * const at_ = GvAV(PL_defgv);
5769 SV **svp = at_ ? AvARRAY(at_) : NULL;
5770 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5771 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5772 bool seen_question = 0;
5773 const char *err = NULL;
5774 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5776 /* Count how many args there are first, to get some idea how far to
5777 extend the stack. */
5779 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5781 if (oa & OA_OPTIONAL) seen_question = 1;
5782 if (!seen_question) minargs++;
5786 if(numargs < minargs) err = "Not enough";
5787 else if(numargs > maxargs) err = "Too many";
5789 /* diag_listed_as: Too many arguments for %s */
5791 "%s arguments for %s", err,
5792 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5795 /* Reset the stack pointer. Without this, we end up returning our own
5796 arguments in list context, in addition to the values we are supposed
5797 to return. nextstate usually does this on sub entry, but we need
5798 to run the next op with the caller's hints, so we cannot have a
5800 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5802 if(!maxargs) RETURN;
5804 /* We do this here, rather than with a separate pushmark op, as it has
5805 to come in between two things this function does (stack reset and
5806 arg pushing). This seems the easiest way to do it. */
5809 (void)Perl_pp_pushmark(aTHX);
5812 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5813 PUTBACK; /* The code below can die in various places. */
5815 oa = PL_opargs[opnum] >> OASHIFT;
5816 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5821 if (!numargs && defgv && whicharg == minargs + 1) {
5822 PUSHs(find_rundefsv2(
5823 find_runcv_where(FIND_RUNCV_level_eq, 1, NULL),
5824 cxstack[cxstack_ix].blk_oldcop->cop_seq
5827 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5831 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5836 if (!svp || !*svp || !SvROK(*svp)
5837 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5839 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5840 "Type of arg %d to &CORE::%s must be hash reference",
5841 whicharg, OP_DESC(PL_op->op_next)
5846 if (!numargs) PUSHs(NULL);
5847 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5848 /* no magic here, as the prototype will have added an extra
5849 refgen and we just want what was there before that */
5852 const bool constr = PL_op->op_private & whicharg;
5854 svp && *svp ? *svp : &PL_sv_undef,
5855 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5861 if (!numargs) goto try_defsv;
5863 const bool wantscalar =
5864 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5865 if (!svp || !*svp || !SvROK(*svp)
5866 /* We have to permit globrefs even for the \$ proto, as
5867 *foo is indistinguishable from ${\*foo}, and the proto-
5868 type permits the latter. */
5869 || SvTYPE(SvRV(*svp)) > (
5870 wantscalar ? SVt_PVLV
5871 : opnum == OP_LOCK || opnum == OP_UNDEF
5877 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5878 "Type of arg %d to &CORE::%s must be %s",
5879 whicharg, PL_op_name[opnum],
5881 ? "scalar reference"
5882 : opnum == OP_LOCK || opnum == OP_UNDEF
5883 ? "reference to one of [$@%&*]"
5884 : "reference to one of [$@%*]"
5887 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5888 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5889 /* Undo @_ localisation, so that sub exit does not undo
5890 part of our undeffing. */
5891 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5893 cx->cx_type &= ~ CXp_HASARGS;
5894 assert(!AvREAL(cx->blk_sub.argarray));
5899 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5911 if (PL_op->op_private & OPpOFFBYONE) {
5912 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
5914 else cv = find_runcv(NULL);
5915 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5922 * c-indentation-style: bsd
5924 * indent-tabs-mode: nil
5927 * ex: set ts=8 sts=4 sw=4 et: