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 static const STRLEN small_mu_len = sizeof(GREEK_SMALL_LETTER_MU_UTF8) - 1;
51 static const STRLEN capital_iota_len = sizeof(GREEK_CAPITAL_LETTER_IOTA_UTF8) - 1;
53 /* variations on pp_null */
58 if (GIMME_V == G_SCALAR)
65 /* This is also called directly by pp_lvavref. */
70 assert(SvTYPE(TARG) == SVt_PVAV);
71 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
72 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
73 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
75 if (PL_op->op_flags & OPf_REF) {
78 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
79 const I32 flags = is_lvalue_sub();
80 if (flags && !(flags & OPpENTERSUB_INARGS)) {
81 if (GIMME_V == G_SCALAR)
82 /* diag_listed_as: Can't return %s to lvalue scalar context */
83 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
89 if (gimme == G_ARRAY) {
90 /* XXX see also S_pushav in pp_hot.c */
91 const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
93 if (SvMAGICAL(TARG)) {
95 for (i=0; i < maxarg; i++) {
96 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
97 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
102 for (i=0; i < maxarg; i++) {
103 SV * const sv = AvARRAY((const AV *)TARG)[i];
104 SP[i+1] = sv ? sv : &PL_sv_undef;
109 else if (gimme == G_SCALAR) {
110 SV* const sv = sv_newmortal();
111 const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
112 sv_setiv(sv, maxarg);
123 assert(SvTYPE(TARG) == SVt_PVHV);
125 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
126 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
127 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
128 if (PL_op->op_flags & OPf_REF)
130 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
131 const I32 flags = is_lvalue_sub();
132 if (flags && !(flags & OPpENTERSUB_INARGS)) {
133 if (GIMME_V == G_SCALAR)
134 /* diag_listed_as: Can't return %s to lvalue scalar context */
135 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
140 if (gimme == G_ARRAY) {
141 RETURNOP(Perl_do_kv(aTHX));
143 else if ((PL_op->op_private & OPpTRUEBOOL
144 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
145 && block_gimme() == G_VOID ))
146 && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied)))
147 SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
148 else if (gimme == G_SCALAR) {
149 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
158 assert(SvTYPE(TARG) == SVt_PVCV);
166 SvPADSTALE_off(TARG);
173 CV * const protocv = PadnamePROTOCV(
174 PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG]
176 assert(SvTYPE(TARG) == SVt_PVCV);
178 if (CvISXSUB(protocv)) { /* constant */
179 /* XXX Should we clone it here? */
180 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
181 to introcv and remove the SvPADSTALE_off. */
182 SAVEPADSVANDMORTALIZE(ARGTARG);
183 PAD_SVl(ARGTARG) = SvREFCNT_inc_simple_NN(protocv);
186 if (CvROOT(protocv)) {
187 assert(CvCLONE(protocv));
188 assert(!CvCLONED(protocv));
190 cv_clone_into(protocv,(CV *)TARG);
191 SAVECLEARSV(PAD_SVl(ARGTARG));
198 /* In some cases this function inspects PL_op. If this function is called
199 for new op types, more bool parameters may need to be added in place of
202 When noinit is true, the absence of a gv will cause a retval of undef.
203 This is unrelated to the cv-to-gv assignment case.
207 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
210 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
213 sv = amagic_deref_call(sv, to_gv_amg);
217 if (SvTYPE(sv) == SVt_PVIO) {
218 GV * const gv = MUTABLE_GV(sv_newmortal());
219 gv_init(gv, 0, "__ANONIO__", 10, 0);
220 GvIOp(gv) = MUTABLE_IO(sv);
221 SvREFCNT_inc_void_NN(sv);
224 else if (!isGV_with_GP(sv)) {
225 Perl_die(aTHX_ "Not a GLOB reference");
229 if (!isGV_with_GP(sv)) {
231 /* If this is a 'my' scalar and flag is set then vivify
234 if (vivify_sv && sv != &PL_sv_undef) {
237 Perl_croak_no_modify();
238 if (cUNOP->op_targ) {
239 SV * const namesv = PAD_SV(cUNOP->op_targ);
240 HV *stash = CopSTASH(PL_curcop);
241 if (SvTYPE(stash) != SVt_PVHV) stash = NULL;
242 gv = MUTABLE_GV(newSV(0));
243 gv_init_sv(gv, stash, namesv, 0);
246 const char * const name = CopSTASHPV(PL_curcop);
247 gv = newGVgen_flags(name,
248 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
249 SvREFCNT_inc_simple_void_NN(gv);
251 prepare_SV_for_RV(sv);
252 SvRV_set(sv, MUTABLE_SV(gv));
257 if (PL_op->op_flags & OPf_REF || strict) {
258 Perl_die(aTHX_ PL_no_usym, "a symbol");
260 if (ckWARN(WARN_UNINITIALIZED))
266 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
267 sv, GV_ADDMG, SVt_PVGV
276 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
280 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
281 == OPpDONT_INIT_GV) {
282 /* We are the target of a coderef assignment. Return
283 the scalar unchanged, and let pp_sasssign deal with
287 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
289 /* FAKE globs in the symbol table cause weird bugs (#77810) */
293 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
294 SV *newsv = sv_newmortal();
295 sv_setsv_flags(newsv, sv, 0);
307 sv, PL_op->op_private & OPpDEREF,
308 PL_op->op_private & HINT_STRICT_REFS,
309 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
310 || PL_op->op_type == OP_READLINE
312 if (PL_op->op_private & OPpLVAL_INTRO)
313 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
318 /* Helper function for pp_rv2sv and pp_rv2av */
320 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
321 const svtype type, SV ***spp)
325 PERL_ARGS_ASSERT_SOFTREF2XV;
327 if (PL_op->op_private & HINT_STRICT_REFS) {
329 Perl_die(aTHX_ PL_no_symref_sv, sv,
330 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
332 Perl_die(aTHX_ PL_no_usym, what);
336 PL_op->op_flags & OPf_REF
338 Perl_die(aTHX_ PL_no_usym, what);
339 if (ckWARN(WARN_UNINITIALIZED))
341 if (type != SVt_PV && GIMME_V == G_ARRAY) {
345 **spp = &PL_sv_undef;
348 if ((PL_op->op_flags & OPf_SPECIAL) &&
349 !(PL_op->op_flags & OPf_MOD))
351 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
353 **spp = &PL_sv_undef;
358 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
371 sv = amagic_deref_call(sv, to_sv_amg);
375 if (SvTYPE(sv) >= SVt_PVAV)
376 DIE(aTHX_ "Not a SCALAR reference");
381 if (!isGV_with_GP(gv)) {
382 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
388 if (PL_op->op_flags & OPf_MOD) {
389 if (PL_op->op_private & OPpLVAL_INTRO) {
390 if (cUNOP->op_first->op_type == OP_NULL)
391 sv = save_scalar(MUTABLE_GV(TOPs));
393 sv = save_scalar(gv);
395 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
397 else if (PL_op->op_private & OPpDEREF)
398 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
407 AV * const av = MUTABLE_AV(TOPs);
408 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
410 SV ** const svp = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
412 *svp = newSV_type(SVt_PVMG);
413 sv_magic(*svp, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
417 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
426 if (PL_op->op_flags & OPf_MOD || LVRET) {
427 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV));/* Not TARG RT#67838 */
428 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
430 LvTARG(ret) = SvREFCNT_inc_simple(sv);
431 SETs(ret); /* no SvSETMAGIC */
434 const MAGIC * const mg = mg_find_mglob(sv);
435 if (mg && mg->mg_len != -1) {
437 STRLEN i = mg->mg_len;
438 if (mg->mg_flags & MGf_BYTES && DO_UTF8(sv))
439 i = sv_pos_b2u_flags(sv, i, SV_GMAGIC|SV_CONST_RETURN);
453 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
455 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT))
456 == OPpMAY_RETURN_CONSTANT)
459 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
460 /* (But not in defined().) */
462 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
464 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
465 cv = SvTYPE(SvRV(gv)) == SVt_PVCV
466 ? MUTABLE_CV(SvRV(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);
489 DIE(aTHX_ "Can't find an opnumber for \"%"UTF8f"\"",
490 UTF8fARG(SvFLAGS(TOPs) & SVf_UTF8, SvCUR(TOPs)-6, s+6));
492 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
498 cv = sv_2cv(TOPs, &stash, &gv, 0);
500 ret = newSVpvn_flags(
501 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
511 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
513 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
515 PUSHs(MUTABLE_SV(cv));
529 if (GIMME_V != G_ARRAY) {
535 *MARK = &PL_sv_undef;
537 *MARK = refto(*MARK);
541 EXTEND_MORTAL(SP - MARK);
543 *MARK = refto(*MARK);
548 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)) {
573 SvREFCNT_inc_void_NN(sv);
576 sv_upgrade(rv, SVt_IV);
585 SV * const sv = TOPs;
593 /* use the return value that is in a register, its the same as TARG */
594 TARG = sv_ref(TARG,SvRV(sv),TRUE);
609 stash = CopSTASH(PL_curcop);
610 if (SvTYPE(stash) != SVt_PVHV)
611 Perl_croak(aTHX_ "Attempt to bless into a freed package");
614 SV * const ssv = POPs;
618 if (!ssv) goto curstash;
621 if (!SvAMAGIC(ssv)) {
623 Perl_croak(aTHX_ "Attempt to bless into a reference");
625 /* SvAMAGIC is on here, but it only means potentially overloaded,
626 so after stringification: */
627 ptr = SvPV_nomg_const(ssv,len);
628 /* We need to check the flag again: */
629 if (!SvAMAGIC(ssv)) goto frog;
631 else ptr = SvPV_nomg_const(ssv,len);
633 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
634 "Explicit blessing to '' (assuming package main)");
635 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
638 (void)sv_bless(TOPs, stash);
648 const char * const elem = SvPV_const(sv, len);
649 GV * const gv = MUTABLE_GV(TOPs);
654 /* elem will always be NUL terminated. */
655 const char * const second_letter = elem + 1;
658 if (len == 5 && strEQ(second_letter, "RRAY"))
660 tmpRef = MUTABLE_SV(GvAV(gv));
661 if (tmpRef && !AvREAL((const AV *)tmpRef)
662 && AvREIFY((const AV *)tmpRef))
663 av_reify(MUTABLE_AV(tmpRef));
667 if (len == 4 && strEQ(second_letter, "ODE"))
668 tmpRef = MUTABLE_SV(GvCVu(gv));
671 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
672 /* finally deprecated in 5.8.0 */
673 deprecate("*glob{FILEHANDLE}");
674 tmpRef = MUTABLE_SV(GvIOp(gv));
677 if (len == 6 && strEQ(second_letter, "ORMAT"))
678 tmpRef = MUTABLE_SV(GvFORM(gv));
681 if (len == 4 && strEQ(second_letter, "LOB"))
682 tmpRef = MUTABLE_SV(gv);
685 if (len == 4 && strEQ(second_letter, "ASH"))
686 tmpRef = MUTABLE_SV(GvHV(gv));
689 if (*second_letter == 'O' && !elem[2] && len == 2)
690 tmpRef = MUTABLE_SV(GvIOp(gv));
693 if (len == 4 && strEQ(second_letter, "AME"))
694 sv = newSVhek(GvNAME_HEK(gv));
697 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
698 const HV * const stash = GvSTASH(gv);
699 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
700 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
704 if (len == 6 && strEQ(second_letter, "CALAR"))
719 /* Pattern matching */
727 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
728 /* Historically, study was skipped in these cases. */
733 /* Make study a no-op. It's no longer useful and its existence
734 complicates matters elsewhere. */
740 /* also used for: pp_transr() */
747 if (PL_op->op_flags & OPf_STACKED)
752 sv = PAD_SV(ARGTARG);
757 if(PL_op->op_type == OP_TRANSR) {
759 const char * const pv = SvPV(sv,len);
760 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
765 I32 i = do_trans(sv);
771 /* Lvalue operators. */
774 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
780 PERL_ARGS_ASSERT_DO_CHOMP;
782 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
784 if (SvTYPE(sv) == SVt_PVAV) {
786 AV *const av = MUTABLE_AV(sv);
787 const I32 max = AvFILL(av);
789 for (i = 0; i <= max; i++) {
790 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
791 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
792 count += do_chomp(retval, sv, chomping);
796 else if (SvTYPE(sv) == SVt_PVHV) {
797 HV* const hv = MUTABLE_HV(sv);
799 (void)hv_iterinit(hv);
800 while ((entry = hv_iternext(hv)))
801 count += do_chomp(retval, hv_iterval(hv,entry), chomping);
804 else if (SvREADONLY(sv)) {
805 Perl_croak_no_modify();
810 /* XXX, here sv is utf8-ized as a side-effect!
811 If encoding.pm is used properly, almost string-generating
812 operations, including literal strings, chr(), input data, etc.
813 should have been utf8-ized already, right?
815 sv_recode_to_utf8(sv, _get_encoding());
822 char *temp_buffer = NULL;
827 goto nope_free_nothing;
829 while (len && s[-1] == '\n') {
836 STRLEN rslen, rs_charlen;
837 const char *rsptr = SvPV_const(PL_rs, rslen);
839 rs_charlen = SvUTF8(PL_rs)
843 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
844 /* Assumption is that rs is shorter than the scalar. */
846 /* RS is utf8, scalar is 8 bit. */
848 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
851 /* Cannot downgrade, therefore cannot possibly match.
852 At this point, temp_buffer is not alloced, and
853 is the buffer inside PL_rs, so dont free it.
855 assert (temp_buffer == rsptr);
860 else if (IN_ENCODING) {
861 /* RS is 8 bit, encoding.pm is used.
862 * Do not recode PL_rs as a side-effect. */
863 svrecode = newSVpvn(rsptr, rslen);
864 sv_recode_to_utf8(svrecode, _get_encoding());
865 rsptr = SvPV_const(svrecode, rslen);
866 rs_charlen = sv_len_utf8(svrecode);
869 /* RS is 8 bit, scalar is utf8. */
870 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
884 if (memNE(s, rsptr, rslen))
889 SvPV_force_nomg_nolen(sv);
896 Safefree(temp_buffer);
898 SvREFCNT_dec(svrecode);
902 if (len && (!SvPOK(sv) || SvIsCOW(sv)))
903 s = SvPV_force_nomg(sv, len);
906 char * const send = s + len;
907 char * const start = s;
909 while (s > start && UTF8_IS_CONTINUATION(*s))
911 if (is_utf8_string((U8*)s, send - s)) {
912 sv_setpvn(retval, s, send - s);
914 SvCUR_set(sv, s - start);
920 sv_setpvs(retval, "");
924 sv_setpvn(retval, s, 1);
931 sv_setpvs(retval, "");
938 /* also used for: pp_schomp() */
943 const bool chomping = PL_op->op_type == OP_SCHOMP;
945 const size_t count = do_chomp(TARG, TOPs, chomping);
947 sv_setiv(TARG, count);
953 /* also used for: pp_chomp() */
957 dSP; dMARK; dTARGET; dORIGMARK;
958 const bool chomping = PL_op->op_type == OP_CHOMP;
962 count += do_chomp(TARG, *++MARK, chomping);
964 sv_setiv(TARG, count);
975 if (!PL_op->op_private) {
987 if (SvTHINKFIRST(sv))
988 sv_force_normal_flags(sv, SV_COW_DROP_PV|SV_IMMEDIATE_UNREF);
990 switch (SvTYPE(sv)) {
994 av_undef(MUTABLE_AV(sv));
997 hv_undef(MUTABLE_HV(sv));
1000 if (cv_const_sv((const CV *)sv))
1001 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
1002 "Constant subroutine %"SVf" undefined",
1003 SVfARG(CvANON((const CV *)sv)
1004 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
1005 : sv_2mortal(newSVhek(
1007 ? CvNAME_HEK((CV *)sv)
1008 : GvENAME_HEK(CvGV((const CV *)sv))
1013 /* let user-undef'd sub keep its identity */
1014 cv_undef_flags(MUTABLE_CV(sv), CV_UNDEF_KEEP_NAME);
1017 assert(isGV_with_GP(sv));
1018 assert(!SvFAKE(sv));
1023 /* undef *Pkg::meth_name ... */
1025 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1026 && HvENAME_get(stash);
1028 if((stash = GvHV((const GV *)sv))) {
1029 if(HvENAME_get(stash))
1030 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1034 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
1035 gp_free(MUTABLE_GV(sv));
1037 GvGP_set(sv, gp_ref(gp));
1038 #ifndef PERL_DONT_CREATE_GVSV
1039 GvSV(sv) = newSV(0);
1041 GvLINE(sv) = CopLINE(PL_curcop);
1042 GvEGV(sv) = MUTABLE_GV(sv);
1046 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1048 /* undef *Foo::ISA */
1049 if( strEQ(GvNAME((const GV *)sv), "ISA")
1050 && (stash = GvSTASH((const GV *)sv))
1051 && (method_changed || HvENAME(stash)) )
1052 mro_isa_changed_in(stash);
1053 else if(method_changed)
1054 mro_method_changed_in(
1055 GvSTASH((const GV *)sv)
1061 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1075 /* common "slow" code for pp_postinc and pp_postdec */
1078 S_postincdec_common(pTHX_ SV *sv, SV *targ)
1082 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1085 TARG = sv_newmortal();
1092 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1093 if (inc && !SvOK(TARG))
1100 /* also used for: pp_i_postinc() */
1107 /* special-case sv being a simple integer */
1108 if (LIKELY(((sv->sv_flags &
1109 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1110 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1112 && SvIVX(sv) != IV_MAX)
1115 SvIV_set(sv, iv + 1);
1116 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1121 return S_postincdec_common(aTHX_ sv, TARG);
1125 /* also used for: pp_i_postdec() */
1132 /* special-case sv being a simple integer */
1133 if (LIKELY(((sv->sv_flags &
1134 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1135 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1137 && SvIVX(sv) != IV_MIN)
1140 SvIV_set(sv, iv - 1);
1141 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1146 return S_postincdec_common(aTHX_ sv, TARG);
1150 /* Ordinary operators. */
1154 dSP; dATARGET; SV *svl, *svr;
1155 #ifdef PERL_PRESERVE_IVUV
1158 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1161 #ifdef PERL_PRESERVE_IVUV
1162 /* For integer to integer power, we do the calculation by hand wherever
1163 we're sure it is safe; otherwise we call pow() and try to convert to
1164 integer afterwards. */
1165 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1173 const IV iv = SvIVX(svr);
1177 goto float_it; /* Can't do negative powers this way. */
1181 baseuok = SvUOK(svl);
1183 baseuv = SvUVX(svl);
1185 const IV iv = SvIVX(svl);
1188 baseuok = TRUE; /* effectively it's a UV now */
1190 baseuv = -iv; /* abs, baseuok == false records sign */
1193 /* now we have integer ** positive integer. */
1196 /* foo & (foo - 1) is zero only for a power of 2. */
1197 if (!(baseuv & (baseuv - 1))) {
1198 /* We are raising power-of-2 to a positive integer.
1199 The logic here will work for any base (even non-integer
1200 bases) but it can be less accurate than
1201 pow (base,power) or exp (power * log (base)) when the
1202 intermediate values start to spill out of the mantissa.
1203 With powers of 2 we know this can't happen.
1204 And powers of 2 are the favourite thing for perl
1205 programmers to notice ** not doing what they mean. */
1207 NV base = baseuok ? baseuv : -(NV)baseuv;
1212 while (power >>= 1) {
1220 SvIV_please_nomg(svr);
1223 unsigned int highbit = 8 * sizeof(UV);
1224 unsigned int diff = 8 * sizeof(UV);
1225 while (diff >>= 1) {
1227 if (baseuv >> highbit) {
1231 /* we now have baseuv < 2 ** highbit */
1232 if (power * highbit <= 8 * sizeof(UV)) {
1233 /* result will definitely fit in UV, so use UV math
1234 on same algorithm as above */
1237 const bool odd_power = cBOOL(power & 1);
1241 while (power >>= 1) {
1248 if (baseuok || !odd_power)
1249 /* answer is positive */
1251 else if (result <= (UV)IV_MAX)
1252 /* answer negative, fits in IV */
1253 SETi( -(IV)result );
1254 else if (result == (UV)IV_MIN)
1255 /* 2's complement assumption: special case IV_MIN */
1258 /* answer negative, doesn't fit */
1259 SETn( -(NV)result );
1267 NV right = SvNV_nomg(svr);
1268 NV left = SvNV_nomg(svl);
1271 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1273 We are building perl with long double support and are on an AIX OS
1274 afflicted with a powl() function that wrongly returns NaNQ for any
1275 negative base. This was reported to IBM as PMR #23047-379 on
1276 03/06/2006. The problem exists in at least the following versions
1277 of AIX and the libm fileset, and no doubt others as well:
1279 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1280 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1281 AIX 5.2.0 bos.adt.libm 5.2.0.85
1283 So, until IBM fixes powl(), we provide the following workaround to
1284 handle the problem ourselves. Our logic is as follows: for
1285 negative bases (left), we use fmod(right, 2) to check if the
1286 exponent is an odd or even integer:
1288 - if odd, powl(left, right) == -powl(-left, right)
1289 - if even, powl(left, right) == powl(-left, right)
1291 If the exponent is not an integer, the result is rightly NaNQ, so
1292 we just return that (as NV_NAN).
1296 NV mod2 = Perl_fmod( right, 2.0 );
1297 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1298 SETn( -Perl_pow( -left, right) );
1299 } else if (mod2 == 0.0) { /* even integer */
1300 SETn( Perl_pow( -left, right) );
1301 } else { /* fractional power */
1305 SETn( Perl_pow( left, right) );
1308 SETn( Perl_pow( left, right) );
1309 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1311 #ifdef PERL_PRESERVE_IVUV
1313 SvIV_please_nomg(svr);
1321 dSP; dATARGET; SV *svl, *svr;
1322 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1326 #ifdef PERL_PRESERVE_IVUV
1328 /* special-case some simple common cases */
1329 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1331 U32 flags = (svl->sv_flags & svr->sv_flags);
1332 if (flags & SVf_IOK) {
1333 /* both args are simple IVs */
1338 topl = ((UV)il) >> (UVSIZE * 4 - 1);
1339 topr = ((UV)ir) >> (UVSIZE * 4 - 1);
1341 /* if both are in a range that can't under/overflow, do a
1342 * simple integer multiply: if the top halves(*) of both numbers
1343 * are 00...00 or 11...11, then it's safe.
1344 * (*) for 32-bits, the "top half" is the top 17 bits,
1345 * for 64-bits, its 33 bits */
1347 ((topl+1) | (topr+1))
1348 & ( (((UV)1) << (UVSIZE * 4 + 1)) - 2) /* 11..110 */
1351 TARGi(il * ir, 0); /* args not GMG, so can't be tainted */
1357 else if (flags & SVf_NOK) {
1358 /* both args are NVs */
1365 if (nl == (NV)il && nr == (NV)ir)
1366 /* nothing was lost by converting to IVs */
1370 # if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BIG_ENDIAN && NVSIZE == 16
1371 if (Perl_isinf(result)) {
1372 Zero((U8*)&result + 8, 8, U8);
1375 TARGn(result, 0); /* args not GMG, so can't be tainted */
1383 if (SvIV_please_nomg(svr)) {
1384 /* Unless the left argument is integer in range we are going to have to
1385 use NV maths. Hence only attempt to coerce the right argument if
1386 we know the left is integer. */
1387 /* Left operand is defined, so is it IV? */
1388 if (SvIV_please_nomg(svl)) {
1389 bool auvok = SvUOK(svl);
1390 bool buvok = SvUOK(svr);
1391 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1392 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1401 const IV aiv = SvIVX(svl);
1404 auvok = TRUE; /* effectively it's a UV now */
1406 /* abs, auvok == false records sign */
1407 alow = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv);
1413 const IV biv = SvIVX(svr);
1416 buvok = TRUE; /* effectively it's a UV now */
1418 /* abs, buvok == false records sign */
1419 blow = (biv == IV_MIN) ? (UV)biv : (UV)(-biv);
1423 /* If this does sign extension on unsigned it's time for plan B */
1424 ahigh = alow >> (4 * sizeof (UV));
1426 bhigh = blow >> (4 * sizeof (UV));
1428 if (ahigh && bhigh) {
1430 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1431 which is overflow. Drop to NVs below. */
1432 } else if (!ahigh && !bhigh) {
1433 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1434 so the unsigned multiply cannot overflow. */
1435 const UV product = alow * blow;
1436 if (auvok == buvok) {
1437 /* -ve * -ve or +ve * +ve gives a +ve result. */
1441 } else if (product <= (UV)IV_MIN) {
1442 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1443 /* -ve result, which could overflow an IV */
1445 /* can't negate IV_MIN, but there are aren't two
1446 * integers such that !ahigh && !bhigh, where the
1447 * product equals 0x800....000 */
1448 assert(product != (UV)IV_MIN);
1449 SETi( -(IV)product );
1451 } /* else drop to NVs below. */
1453 /* One operand is large, 1 small */
1456 /* swap the operands */
1458 bhigh = blow; /* bhigh now the temp var for the swap */
1462 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1463 multiplies can't overflow. shift can, add can, -ve can. */
1464 product_middle = ahigh * blow;
1465 if (!(product_middle & topmask)) {
1466 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1468 product_middle <<= (4 * sizeof (UV));
1469 product_low = alow * blow;
1471 /* as for pp_add, UV + something mustn't get smaller.
1472 IIRC ANSI mandates this wrapping *behaviour* for
1473 unsigned whatever the actual representation*/
1474 product_low += product_middle;
1475 if (product_low >= product_middle) {
1476 /* didn't overflow */
1477 if (auvok == buvok) {
1478 /* -ve * -ve or +ve * +ve gives a +ve result. */
1480 SETu( product_low );
1482 } else if (product_low <= (UV)IV_MIN) {
1483 /* 2s complement assumption again */
1484 /* -ve result, which could overflow an IV */
1486 SETi(product_low == (UV)IV_MIN
1487 ? IV_MIN : -(IV)product_low);
1489 } /* else drop to NVs below. */
1491 } /* product_middle too large */
1492 } /* ahigh && bhigh */
1497 NV right = SvNV_nomg(svr);
1498 NV left = SvNV_nomg(svl);
1499 NV result = left * right;
1502 #if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BIG_ENDIAN && NVSIZE == 16
1503 if (Perl_isinf(result)) {
1504 Zero((U8*)&result + 8, 8, U8);
1514 dSP; dATARGET; SV *svl, *svr;
1515 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1518 /* Only try to do UV divide first
1519 if ((SLOPPYDIVIDE is true) or
1520 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1522 The assumption is that it is better to use floating point divide
1523 whenever possible, only doing integer divide first if we can't be sure.
1524 If NV_PRESERVES_UV is true then we know at compile time that no UV
1525 can be too large to preserve, so don't need to compile the code to
1526 test the size of UVs. */
1529 # define PERL_TRY_UV_DIVIDE
1530 /* ensure that 20./5. == 4. */
1532 # ifdef PERL_PRESERVE_IVUV
1533 # ifndef NV_PRESERVES_UV
1534 # define PERL_TRY_UV_DIVIDE
1539 #ifdef PERL_TRY_UV_DIVIDE
1540 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1541 bool left_non_neg = SvUOK(svl);
1542 bool right_non_neg = SvUOK(svr);
1546 if (right_non_neg) {
1550 const IV biv = SvIVX(svr);
1553 right_non_neg = TRUE; /* effectively it's a UV now */
1556 right = (biv == IV_MIN) ? (UV)biv : (UV)(-biv);
1559 /* historically undef()/0 gives a "Use of uninitialized value"
1560 warning before dieing, hence this test goes here.
1561 If it were immediately before the second SvIV_please, then
1562 DIE() would be invoked before left was even inspected, so
1563 no inspection would give no warning. */
1565 DIE(aTHX_ "Illegal division by zero");
1571 const IV aiv = SvIVX(svl);
1574 left_non_neg = TRUE; /* effectively it's a UV now */
1577 left = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv);
1583 /* For sloppy divide we always attempt integer division. */
1585 /* Otherwise we only attempt it if either or both operands
1586 would not be preserved by an NV. If both fit in NVs
1587 we fall through to the NV divide code below. However,
1588 as left >= right to ensure integer result here, we know that
1589 we can skip the test on the right operand - right big
1590 enough not to be preserved can't get here unless left is
1593 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1596 /* Integer division can't overflow, but it can be imprecise. */
1597 const UV result = left / right;
1598 if (result * right == left) {
1599 SP--; /* result is valid */
1600 if (left_non_neg == right_non_neg) {
1601 /* signs identical, result is positive. */
1605 /* 2s complement assumption */
1606 if (result <= (UV)IV_MIN)
1607 SETi(result == (UV)IV_MIN ? IV_MIN : -(IV)result);
1609 /* It's exact but too negative for IV. */
1610 SETn( -(NV)result );
1613 } /* tried integer divide but it was not an integer result */
1614 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1615 } /* one operand wasn't SvIOK */
1616 #endif /* PERL_TRY_UV_DIVIDE */
1618 NV right = SvNV_nomg(svr);
1619 NV left = SvNV_nomg(svl);
1620 (void)POPs;(void)POPs;
1621 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1622 if (! Perl_isnan(right) && right == 0.0)
1626 DIE(aTHX_ "Illegal division by zero");
1627 PUSHn( left / right );
1635 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1639 bool left_neg = FALSE;
1640 bool right_neg = FALSE;
1641 bool use_double = FALSE;
1642 bool dright_valid = FALSE;
1645 SV * const svr = TOPs;
1646 SV * const svl = TOPm1s;
1647 if (SvIV_please_nomg(svr)) {
1648 right_neg = !SvUOK(svr);
1652 const IV biv = SvIVX(svr);
1655 right_neg = FALSE; /* effectively it's a UV now */
1657 right = (biv == IV_MIN) ? (UV)biv : (UV)(-biv);
1662 dright = SvNV_nomg(svr);
1663 right_neg = dright < 0;
1666 if (dright < UV_MAX_P1) {
1667 right = U_V(dright);
1668 dright_valid = TRUE; /* In case we need to use double below. */
1674 /* At this point use_double is only true if right is out of range for
1675 a UV. In range NV has been rounded down to nearest UV and
1676 use_double false. */
1677 if (!use_double && SvIV_please_nomg(svl)) {
1678 left_neg = !SvUOK(svl);
1682 const IV aiv = SvIVX(svl);
1685 left_neg = FALSE; /* effectively it's a UV now */
1687 left = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv);
1692 dleft = SvNV_nomg(svl);
1693 left_neg = dleft < 0;
1697 /* This should be exactly the 5.6 behaviour - if left and right are
1698 both in range for UV then use U_V() rather than floor. */
1700 if (dleft < UV_MAX_P1) {
1701 /* right was in range, so is dleft, so use UVs not double.
1705 /* left is out of range for UV, right was in range, so promote
1706 right (back) to double. */
1708 /* The +0.5 is used in 5.6 even though it is not strictly
1709 consistent with the implicit +0 floor in the U_V()
1710 inside the #if 1. */
1711 dleft = Perl_floor(dleft + 0.5);
1714 dright = Perl_floor(dright + 0.5);
1725 DIE(aTHX_ "Illegal modulus zero");
1727 dans = Perl_fmod(dleft, dright);
1728 if ((left_neg != right_neg) && dans)
1729 dans = dright - dans;
1732 sv_setnv(TARG, dans);
1738 DIE(aTHX_ "Illegal modulus zero");
1741 if ((left_neg != right_neg) && ans)
1744 /* XXX may warn: unary minus operator applied to unsigned type */
1745 /* could change -foo to be (~foo)+1 instead */
1746 if (ans <= ~((UV)IV_MAX)+1)
1747 sv_setiv(TARG, ~ans+1);
1749 sv_setnv(TARG, -(NV)ans);
1752 sv_setuv(TARG, ans);
1764 bool infnan = FALSE;
1766 if (GIMME_V == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1767 /* TODO: think of some way of doing list-repeat overloading ??? */
1772 if (UNLIKELY(PL_op->op_private & OPpREPEAT_DOLIST)) {
1773 /* The parser saw this as a list repeat, and there
1774 are probably several items on the stack. But we're
1775 in scalar/void context, and there's no pp_list to save us
1776 now. So drop the rest of the items -- robin@kitsite.com
1779 if (MARK + 1 < SP) {
1785 ASSUME(MARK + 1 == SP);
1787 MARK[1] = &PL_sv_undef;
1791 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1797 const UV uv = SvUV_nomg(sv);
1799 count = IV_MAX; /* The best we can do? */
1803 count = SvIV_nomg(sv);
1806 else if (SvNOKp(sv)) {
1807 const NV nv = SvNV_nomg(sv);
1808 infnan = Perl_isinfnan(nv);
1809 if (UNLIKELY(infnan)) {
1813 count = -1; /* An arbitrary negative integer */
1819 count = SvIV_nomg(sv);
1822 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
1823 "Non-finite repeat count does nothing");
1824 } else if (count < 0) {
1826 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
1827 "Negative repeat count does nothing");
1830 if (GIMME_V == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1832 const SSize_t items = SP - MARK;
1833 const U8 mod = PL_op->op_flags & OPf_MOD;
1838 if ( items > SSize_t_MAX / count /* max would overflow */
1839 /* repeatcpy would overflow */
1840 || items > I32_MAX / (I32)sizeof(SV *)
1842 Perl_croak(aTHX_ "%s","Out of memory during list extend");
1843 max = items * count;
1848 if (mod && SvPADTMP(*SP)) {
1849 *SP = sv_mortalcopy(*SP);
1856 repeatcpy((char*)(MARK + items), (char*)MARK,
1857 items * sizeof(const SV *), count - 1);
1860 else if (count <= 0)
1863 else { /* Note: mark already snarfed by pp_list */
1864 SV * const tmpstr = POPs;
1869 sv_setsv_nomg(TARG, tmpstr);
1870 SvPV_force_nomg(TARG, len);
1871 isutf = DO_UTF8(TARG);
1878 if ( len > (MEM_SIZE_MAX-1) / (UV)count /* max would overflow */
1879 || len > (U32)I32_MAX /* repeatcpy would overflow */
1881 Perl_croak(aTHX_ "%s",
1882 "Out of memory during string extend");
1883 max = (UV)count * len + 1;
1886 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1887 SvCUR_set(TARG, SvCUR(TARG) * count);
1889 *SvEND(TARG) = '\0';
1892 (void)SvPOK_only_UTF8(TARG);
1894 (void)SvPOK_only(TARG);
1903 dSP; dATARGET; bool useleft; SV *svl, *svr;
1904 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1908 #ifdef PERL_PRESERVE_IVUV
1910 /* special-case some simple common cases */
1911 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1913 U32 flags = (svl->sv_flags & svr->sv_flags);
1914 if (flags & SVf_IOK) {
1915 /* both args are simple IVs */
1920 topl = ((UV)il) >> (UVSIZE * 8 - 2);
1921 topr = ((UV)ir) >> (UVSIZE * 8 - 2);
1923 /* if both are in a range that can't under/overflow, do a
1924 * simple integer subtract: if the top of both numbers
1925 * are 00 or 11, then it's safe */
1926 if (!( ((topl+1) | (topr+1)) & 2)) {
1928 TARGi(il - ir, 0); /* args not GMG, so can't be tainted */
1934 else if (flags & SVf_NOK) {
1935 /* both args are NVs */
1941 if (nl == (NV)il && nr == (NV)ir)
1942 /* nothing was lost by converting to IVs */
1945 TARGn(nl - nr, 0); /* args not GMG, so can't be tainted */
1953 useleft = USE_LEFT(svl);
1954 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1955 "bad things" happen if you rely on signed integers wrapping. */
1956 if (SvIV_please_nomg(svr)) {
1957 /* Unless the left argument is integer in range we are going to have to
1958 use NV maths. Hence only attempt to coerce the right argument if
1959 we know the left is integer. */
1966 a_valid = auvok = 1;
1967 /* left operand is undef, treat as zero. */
1969 /* Left operand is defined, so is it IV? */
1970 if (SvIV_please_nomg(svl)) {
1971 if ((auvok = SvUOK(svl)))
1974 const IV aiv = SvIVX(svl);
1977 auvok = 1; /* Now acting as a sign flag. */
1978 } else { /* 2s complement assumption for IV_MIN */
1979 auv = (aiv == IV_MIN) ? (UV)aiv : (UV)-aiv;
1986 bool result_good = 0;
1989 bool buvok = SvUOK(svr);
1994 const IV biv = SvIVX(svr);
1999 buv = (biv == IV_MIN) ? (UV)biv : (UV)-biv;
2001 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
2002 else "IV" now, independent of how it came in.
2003 if a, b represents positive, A, B negative, a maps to -A etc
2008 all UV maths. negate result if A negative.
2009 subtract if signs same, add if signs differ. */
2011 if (auvok ^ buvok) {
2020 /* Must get smaller */
2025 if (result <= buv) {
2026 /* result really should be -(auv-buv). as its negation
2027 of true value, need to swap our result flag */
2039 if (result <= (UV)IV_MIN)
2040 SETi(result == (UV)IV_MIN
2041 ? IV_MIN : -(IV)result);
2043 /* result valid, but out of range for IV. */
2044 SETn( -(NV)result );
2048 } /* Overflow, drop through to NVs. */
2052 useleft = USE_LEFT(svl);
2055 NV value = SvNV_nomg(svr);
2059 /* left operand is undef, treat as zero - value */
2063 SETn( SvNV_nomg(svl) - value );
2068 #define IV_BITS (IVSIZE * 8)
2070 static UV S_uv_shift(UV uv, int shift, bool left)
2076 if (shift >= IV_BITS) {
2079 return left ? uv << shift : uv >> shift;
2082 static IV S_iv_shift(IV iv, int shift, bool left)
2088 if (shift >= IV_BITS) {
2089 return iv < 0 && !left ? -1 : 0;
2091 return left ? iv << shift : iv >> shift;
2094 #define UV_LEFT_SHIFT(uv, shift) S_uv_shift(uv, shift, TRUE)
2095 #define UV_RIGHT_SHIFT(uv, shift) S_uv_shift(uv, shift, FALSE)
2096 #define IV_LEFT_SHIFT(iv, shift) S_iv_shift(iv, shift, TRUE)
2097 #define IV_RIGHT_SHIFT(iv, shift) S_iv_shift(iv, shift, FALSE)
2101 dSP; dATARGET; SV *svl, *svr;
2102 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
2106 const IV shift = SvIV_nomg(svr);
2107 if (PL_op->op_private & HINT_INTEGER) {
2108 SETi(IV_LEFT_SHIFT(SvIV_nomg(svl), shift));
2111 SETu(UV_LEFT_SHIFT(SvUV_nomg(svl), shift));
2119 dSP; dATARGET; SV *svl, *svr;
2120 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
2124 const IV shift = SvIV_nomg(svr);
2125 if (PL_op->op_private & HINT_INTEGER) {
2126 SETi(IV_RIGHT_SHIFT(SvIV_nomg(svl), shift));
2129 SETu(UV_RIGHT_SHIFT(SvUV_nomg(svl), shift));
2140 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
2144 (SvIOK_notUV(left) && SvIOK_notUV(right))
2145 ? (SvIVX(left) < SvIVX(right))
2146 : (do_ncmp(left, right) == -1)
2156 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
2160 (SvIOK_notUV(left) && SvIOK_notUV(right))
2161 ? (SvIVX(left) > SvIVX(right))
2162 : (do_ncmp(left, right) == 1)
2172 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
2176 (SvIOK_notUV(left) && SvIOK_notUV(right))
2177 ? (SvIVX(left) <= SvIVX(right))
2178 : (do_ncmp(left, right) <= 0)
2188 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
2192 (SvIOK_notUV(left) && SvIOK_notUV(right))
2193 ? (SvIVX(left) >= SvIVX(right))
2194 : ( (do_ncmp(left, right) & 2) == 0)
2204 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2208 (SvIOK_notUV(left) && SvIOK_notUV(right))
2209 ? (SvIVX(left) != SvIVX(right))
2210 : (do_ncmp(left, right) != 0)
2215 /* compare left and right SVs. Returns:
2219 * 2: left or right was a NaN
2222 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2224 PERL_ARGS_ASSERT_DO_NCMP;
2225 #ifdef PERL_PRESERVE_IVUV
2226 /* Fortunately it seems NaN isn't IOK */
2227 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
2229 const IV leftiv = SvIVX(left);
2230 if (!SvUOK(right)) {
2231 /* ## IV <=> IV ## */
2232 const IV rightiv = SvIVX(right);
2233 return (leftiv > rightiv) - (leftiv < rightiv);
2235 /* ## IV <=> UV ## */
2237 /* As (b) is a UV, it's >=0, so it must be < */
2240 const UV rightuv = SvUVX(right);
2241 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2246 /* ## UV <=> UV ## */
2247 const UV leftuv = SvUVX(left);
2248 const UV rightuv = SvUVX(right);
2249 return (leftuv > rightuv) - (leftuv < rightuv);
2251 /* ## UV <=> IV ## */
2253 const IV rightiv = SvIVX(right);
2255 /* As (a) is a UV, it's >=0, so it cannot be < */
2258 const UV leftuv = SvUVX(left);
2259 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2262 NOT_REACHED; /* NOTREACHED */
2266 NV const rnv = SvNV_nomg(right);
2267 NV const lnv = SvNV_nomg(left);
2269 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2270 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2273 return (lnv > rnv) - (lnv < rnv);
2292 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2295 value = do_ncmp(left, right);
2307 /* also used for: pp_sge() pp_sgt() pp_slt() */
2313 int amg_type = sle_amg;
2317 switch (PL_op->op_type) {
2336 tryAMAGICbin_MG(amg_type, AMGf_set);
2340 #ifdef USE_LOCALE_COLLATE
2341 (IN_LC_RUNTIME(LC_COLLATE))
2342 ? sv_cmp_locale_flags(left, right, 0)
2345 sv_cmp_flags(left, right, 0);
2346 SETs(boolSV(cmp * multiplier < rhs));
2354 tryAMAGICbin_MG(seq_amg, AMGf_set);
2357 SETs(boolSV(sv_eq_flags(left, right, 0)));
2365 tryAMAGICbin_MG(sne_amg, AMGf_set);
2368 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2376 tryAMAGICbin_MG(scmp_amg, 0);
2380 #ifdef USE_LOCALE_COLLATE
2381 (IN_LC_RUNTIME(LC_COLLATE))
2382 ? sv_cmp_locale_flags(left, right, 0)
2385 sv_cmp_flags(left, right, 0);
2394 tryAMAGICbin_MG(band_amg, AMGf_assign);
2397 if (SvNIOKp(left) || SvNIOKp(right)) {
2398 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2399 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2400 if (PL_op->op_private & HINT_INTEGER) {
2401 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2405 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2408 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2409 if (right_ro_nonnum) SvNIOK_off(right);
2412 do_vop(PL_op->op_type, TARG, left, right);
2422 tryAMAGICbin_MG(band_amg, AMGf_assign|AMGf_numarg);
2424 dATARGET; dPOPTOPssrl;
2425 if (PL_op->op_private & HINT_INTEGER) {
2426 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2430 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2440 tryAMAGICbin_MG(sband_amg, AMGf_assign);
2442 dATARGET; dPOPTOPssrl;
2443 do_vop(OP_BIT_AND, TARG, left, right);
2448 /* also used for: pp_bit_xor() */
2453 const int op_type = PL_op->op_type;
2455 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2458 if (SvNIOKp(left) || SvNIOKp(right)) {
2459 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2460 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2461 if (PL_op->op_private & HINT_INTEGER) {
2462 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2463 const IV r = SvIV_nomg(right);
2464 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2468 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2469 const UV r = SvUV_nomg(right);
2470 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2473 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2474 if (right_ro_nonnum) SvNIOK_off(right);
2477 do_vop(op_type, TARG, left, right);
2484 /* also used for: pp_nbit_xor() */
2489 const int op_type = PL_op->op_type;
2491 tryAMAGICbin_MG((op_type == OP_NBIT_OR ? bor_amg : bxor_amg),
2492 AMGf_assign|AMGf_numarg);
2494 dATARGET; dPOPTOPssrl;
2495 if (PL_op->op_private & HINT_INTEGER) {
2496 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2497 const IV r = SvIV_nomg(right);
2498 const IV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2502 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2503 const UV r = SvUV_nomg(right);
2504 const UV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2511 /* also used for: pp_sbit_xor() */
2516 const int op_type = PL_op->op_type;
2518 tryAMAGICbin_MG((op_type == OP_SBIT_OR ? sbor_amg : sbxor_amg),
2521 dATARGET; dPOPTOPssrl;
2522 do_vop(op_type == OP_SBIT_OR ? OP_BIT_OR : OP_BIT_XOR, TARG, left,
2528 PERL_STATIC_INLINE bool
2529 S_negate_string(pTHX)
2534 SV * const sv = TOPs;
2535 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2537 s = SvPV_nomg_const(sv, len);
2538 if (isIDFIRST(*s)) {
2539 sv_setpvs(TARG, "-");
2542 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2543 sv_setsv_nomg(TARG, sv);
2544 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2554 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2555 if (S_negate_string(aTHX)) return NORMAL;
2557 SV * const sv = TOPs;
2560 /* It's publicly an integer */
2563 if (SvIVX(sv) == IV_MIN) {
2564 /* 2s complement assumption. */
2565 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) ==
2569 else if (SvUVX(sv) <= IV_MAX) {
2574 else if (SvIVX(sv) != IV_MIN) {
2578 #ifdef PERL_PRESERVE_IVUV
2585 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2586 SETn(-SvNV_nomg(sv));
2587 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2588 goto oops_its_an_int;
2590 SETn(-SvNV_nomg(sv));
2598 tryAMAGICun_MG(not_amg, AMGf_set);
2599 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2604 S_scomplement(pTHX_ SV *targ, SV *sv)
2610 sv_copypv_nomg(TARG, sv);
2611 tmps = (U8*)SvPV_nomg(TARG, len);
2614 /* Calculate exact length, let's not estimate. */
2619 U8 * const send = tmps + len;
2620 U8 * const origtmps = tmps;
2621 const UV utf8flags = UTF8_ALLOW_ANYUV;
2623 while (tmps < send) {
2624 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2626 targlen += UVCHR_SKIP(~c);
2632 /* Now rewind strings and write them. */
2639 Newx(result, targlen + 1, U8);
2641 while (tmps < send) {
2642 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2644 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2647 sv_usepvn_flags(TARG, (char*)result, targlen,
2648 SV_HAS_TRAILING_NUL);
2655 Newx(result, nchar + 1, U8);
2657 while (tmps < send) {
2658 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2663 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2671 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2674 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2679 for ( ; anum > 0; anum--, tmps++)
2686 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2690 if (PL_op->op_private & HINT_INTEGER) {
2691 const IV i = ~SvIV_nomg(sv);
2695 const UV u = ~SvUV_nomg(sv);
2700 S_scomplement(aTHX_ TARG, sv);
2710 tryAMAGICun_MG(compl_amg, AMGf_numeric|AMGf_numarg);
2713 if (PL_op->op_private & HINT_INTEGER) {
2714 const IV i = ~SvIV_nomg(sv);
2718 const UV u = ~SvUV_nomg(sv);
2728 tryAMAGICun_MG(scompl_amg, AMGf_numeric);
2731 S_scomplement(aTHX_ TARG, sv);
2737 /* integer versions of some of the above */
2742 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2745 SETi( left * right );
2754 tryAMAGICbin_MG(div_amg, AMGf_assign);
2757 IV value = SvIV_nomg(right);
2759 DIE(aTHX_ "Illegal division by zero");
2760 num = SvIV_nomg(left);
2762 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2766 value = num / value;
2772 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS) \
2773 && ( __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8))
2780 /* This is the vanilla old i_modulo. */
2782 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2786 DIE(aTHX_ "Illegal modulus zero");
2787 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2791 SETi( left % right );
2796 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS) \
2797 && ( __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8))
2802 /* This is the i_modulo with the workaround for the _moddi3 bug
2803 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2804 * See below for pp_i_modulo. */
2806 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2810 DIE(aTHX_ "Illegal modulus zero");
2811 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2815 SETi( left % PERL_ABS(right) );
2822 dVAR; dSP; dATARGET;
2823 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2827 DIE(aTHX_ "Illegal modulus zero");
2828 /* The assumption is to use hereafter the old vanilla version... */
2830 PL_ppaddr[OP_I_MODULO] =
2832 /* .. but if we have glibc, we might have a buggy _moddi3
2833 * (at least glibc 2.2.5 is known to have this bug), in other
2834 * words our integer modulus with negative quad as the second
2835 * argument might be broken. Test for this and re-patch the
2836 * opcode dispatch table if that is the case, remembering to
2837 * also apply the workaround so that this first round works
2838 * right, too. See [perl #9402] for more information. */
2842 /* Cannot do this check with inlined IV constants since
2843 * that seems to work correctly even with the buggy glibc. */
2845 /* Yikes, we have the bug.
2846 * Patch in the workaround version. */
2848 PL_ppaddr[OP_I_MODULO] =
2849 &Perl_pp_i_modulo_1;
2850 /* Make certain we work right this time, too. */
2851 right = PERL_ABS(right);
2854 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2858 SETi( left % right );
2867 tryAMAGICbin_MG(add_amg, AMGf_assign);
2869 dPOPTOPiirl_ul_nomg;
2870 SETi( left + right );
2878 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2880 dPOPTOPiirl_ul_nomg;
2881 SETi( left - right );
2889 tryAMAGICbin_MG(lt_amg, AMGf_set);
2892 SETs(boolSV(left < right));
2900 tryAMAGICbin_MG(gt_amg, AMGf_set);
2903 SETs(boolSV(left > right));
2911 tryAMAGICbin_MG(le_amg, AMGf_set);
2914 SETs(boolSV(left <= right));
2922 tryAMAGICbin_MG(ge_amg, AMGf_set);
2925 SETs(boolSV(left >= right));
2933 tryAMAGICbin_MG(eq_amg, AMGf_set);
2936 SETs(boolSV(left == right));
2944 tryAMAGICbin_MG(ne_amg, AMGf_set);
2947 SETs(boolSV(left != right));
2955 tryAMAGICbin_MG(ncmp_amg, 0);
2962 else if (left < right)
2974 tryAMAGICun_MG(neg_amg, 0);
2975 if (S_negate_string(aTHX)) return NORMAL;
2977 SV * const sv = TOPs;
2978 IV const i = SvIV_nomg(sv);
2984 /* High falutin' math. */
2989 tryAMAGICbin_MG(atan2_amg, 0);
2992 SETn(Perl_atan2(left, right));
2998 /* also used for: pp_cos() pp_exp() pp_log() pp_sqrt() */
3003 int amg_type = fallback_amg;
3004 const char *neg_report = NULL;
3005 const int op_type = PL_op->op_type;
3008 case OP_SIN: amg_type = sin_amg; break;
3009 case OP_COS: amg_type = cos_amg; break;
3010 case OP_EXP: amg_type = exp_amg; break;
3011 case OP_LOG: amg_type = log_amg; neg_report = "log"; break;
3012 case OP_SQRT: amg_type = sqrt_amg; neg_report = "sqrt"; break;
3015 assert(amg_type != fallback_amg);
3017 tryAMAGICun_MG(amg_type, 0);
3019 SV * const arg = TOPs;
3020 const NV value = SvNV_nomg(arg);
3022 if (neg_report) { /* log or sqrt */
3024 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3025 ! Perl_isnan(value) &&
3027 (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0))) {
3028 SET_NUMERIC_STANDARD();
3029 /* diag_listed_as: Can't take log of %g */
3030 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
3035 case OP_SIN: result = Perl_sin(value); break;
3036 case OP_COS: result = Perl_cos(value); break;
3037 case OP_EXP: result = Perl_exp(value); break;
3038 case OP_LOG: result = Perl_log(value); break;
3039 case OP_SQRT: result = Perl_sqrt(value); break;
3046 /* Support Configure command-line overrides for rand() functions.
3047 After 5.005, perhaps we should replace this by Configure support
3048 for drand48(), random(), or rand(). For 5.005, though, maintain
3049 compatibility by calling rand() but allow the user to override it.
3050 See INSTALL for details. --Andy Dougherty 15 July 1998
3052 /* Now it's after 5.005, and Configure supports drand48() and random(),
3053 in addition to rand(). So the overrides should not be needed any more.
3054 --Jarkko Hietaniemi 27 September 1998
3059 if (!PL_srand_called) {
3060 (void)seedDrand01((Rand_seed_t)seed());
3061 PL_srand_called = TRUE;
3073 SV * const sv = POPs;
3079 /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
3080 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3081 if (! Perl_isnan(value) && value == 0.0)
3091 sv_setnv_mg(TARG, value);
3102 if (MAXARG >= 1 && (TOPs || POPs)) {
3109 pv = SvPV(top, len);
3110 flags = grok_number(pv, len, &anum);
3112 if (!(flags & IS_NUMBER_IN_UV)) {
3113 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
3114 "Integer overflow in srand");
3122 (void)seedDrand01((Rand_seed_t)anum);
3123 PL_srand_called = TRUE;
3127 /* Historically srand always returned true. We can avoid breaking
3129 sv_setpvs(TARG, "0 but true");
3138 tryAMAGICun_MG(int_amg, AMGf_numeric);
3140 SV * const sv = TOPs;
3141 const IV iv = SvIV_nomg(sv);
3142 /* XXX it's arguable that compiler casting to IV might be subtly
3143 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
3144 else preferring IV has introduced a subtle behaviour change bug. OTOH
3145 relying on floating point to be accurate is a bug. */
3150 else if (SvIOK(sv)) {
3152 SETu(SvUV_nomg(sv));
3157 const NV value = SvNV_nomg(sv);
3158 if (UNLIKELY(Perl_isinfnan(value)))
3160 else if (value >= 0.0) {
3161 if (value < (NV)UV_MAX + 0.5) {
3164 SETn(Perl_floor(value));
3168 if (value > (NV)IV_MIN - 0.5) {
3171 SETn(Perl_ceil(value));
3182 tryAMAGICun_MG(abs_amg, AMGf_numeric);
3184 SV * const sv = TOPs;
3185 /* This will cache the NV value if string isn't actually integer */
3186 const IV iv = SvIV_nomg(sv);
3191 else if (SvIOK(sv)) {
3192 /* IVX is precise */
3194 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
3202 /* 2s complement assumption. Also, not really needed as
3203 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
3209 const NV value = SvNV_nomg(sv);
3220 /* also used for: pp_hex() */
3226 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3230 SV* const sv = TOPs;
3232 tmps = (SvPV_const(sv, len));
3234 /* If Unicode, try to downgrade
3235 * If not possible, croak. */
3236 SV* const tsv = sv_2mortal(newSVsv(sv));
3239 sv_utf8_downgrade(tsv, FALSE);
3240 tmps = SvPV_const(tsv, len);
3242 if (PL_op->op_type == OP_HEX)
3245 while (*tmps && len && isSPACE(*tmps))
3249 if (isALPHA_FOLD_EQ(*tmps, 'x')) {
3251 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3253 else if (isALPHA_FOLD_EQ(*tmps, 'b'))
3254 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3256 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3258 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3272 SV * const sv = TOPs;
3274 U32 in_bytes = IN_BYTES;
3275 /* simplest case shortcut */
3276 /* turn off SVf_UTF8 in tmp flags if HINT_BYTES on*/
3277 U32 svflags = (SvFLAGS(sv) ^ (in_bytes << 26)) & (SVf_POK|SVs_GMG|SVf_UTF8);
3278 STATIC_ASSERT_STMT(HINT_BYTES == 0x00000008 && SVf_UTF8 == 0x20000000 && (SVf_UTF8 == HINT_BYTES << 26));
3281 if(LIKELY(svflags == SVf_POK))
3283 if(svflags & SVs_GMG)
3286 if (!IN_BYTES) /* reread to avoid using an C auto/register */
3287 sv_setiv(TARG, (IV)sv_len_utf8_nomg(sv));
3291 /* unrolled SvPV_nomg_const(sv,len) */
3296 (void)sv_2pv_flags(sv, &len, 0|SV_CONST_RETURN);
3298 sv_setiv(TARG, (IV)(len));
3301 if (!SvPADTMP(TARG)) {
3302 sv_setsv_nomg(TARG, &PL_sv_undef);
3303 } else { /* TARG is on stack at this point and is overwriten by SETs.
3304 This branch is the odd one out, so put TARG by default on
3305 stack earlier to let local SP go out of liveness sooner */
3312 return NORMAL; /* no putback, SP didn't move in this opcode */
3315 /* Returns false if substring is completely outside original string.
3316 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
3317 always be true for an explicit 0.
3320 Perl_translate_substr_offsets( STRLEN curlen, IV pos1_iv,
3321 bool pos1_is_uv, IV len_iv,
3322 bool len_is_uv, STRLEN *posp,
3328 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
3330 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
3331 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3334 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
3337 if (len_iv || len_is_uv) {
3338 if (!len_is_uv && len_iv < 0) {
3339 pos2_iv = curlen + len_iv;
3341 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3344 } else { /* len_iv >= 0 */
3345 if (!pos1_is_uv && pos1_iv < 0) {
3346 pos2_iv = pos1_iv + len_iv;
3347 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3349 if ((UV)len_iv > curlen-(UV)pos1_iv)
3352 pos2_iv = pos1_iv+len_iv;
3362 if (!pos2_is_uv && pos2_iv < 0) {
3363 if (!pos1_is_uv && pos1_iv < 0)
3367 else if (!pos1_is_uv && pos1_iv < 0)
3370 if ((UV)pos2_iv < (UV)pos1_iv)
3372 if ((UV)pos2_iv > curlen)
3375 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3376 *posp = (STRLEN)( (UV)pos1_iv );
3377 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3394 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3395 const bool rvalue = (GIMME_V != G_VOID);
3398 const char *repl = NULL;
3400 int num_args = PL_op->op_private & 7;
3401 bool repl_need_utf8_upgrade = FALSE;
3405 if(!(repl_sv = POPs)) num_args--;
3407 if ((len_sv = POPs)) {
3408 len_iv = SvIV(len_sv);
3409 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3414 pos1_iv = SvIV(pos_sv);
3415 pos1_is_uv = SvIOK_UV(pos_sv);
3417 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3421 if (lvalue && !repl_sv) {
3423 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3424 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3426 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3428 pos1_is_uv || pos1_iv >= 0
3429 ? (STRLEN)(UV)pos1_iv
3430 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3432 len_is_uv || len_iv > 0
3433 ? (STRLEN)(UV)len_iv
3434 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3436 PUSHs(ret); /* avoid SvSETMAGIC here */
3440 repl = SvPV_const(repl_sv, repl_len);
3443 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3444 "Attempt to use reference as lvalue in substr"
3446 tmps = SvPV_force_nomg(sv, curlen);
3447 if (DO_UTF8(repl_sv) && repl_len) {
3449 sv_utf8_upgrade_nomg(sv);
3453 else if (DO_UTF8(sv))
3454 repl_need_utf8_upgrade = TRUE;
3456 else tmps = SvPV_const(sv, curlen);
3458 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3459 if (utf8_curlen == curlen)
3462 curlen = utf8_curlen;
3468 STRLEN pos, len, byte_len, byte_pos;
3470 if (!translate_substr_offsets(
3471 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3475 byte_pos = utf8_curlen
3476 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3481 SvTAINTED_off(TARG); /* decontaminate */
3482 SvUTF8_off(TARG); /* decontaminate */
3483 sv_setpvn(TARG, tmps, byte_len);
3484 #ifdef USE_LOCALE_COLLATE
3485 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3492 SV* repl_sv_copy = NULL;
3494 if (repl_need_utf8_upgrade) {
3495 repl_sv_copy = newSVsv(repl_sv);
3496 sv_utf8_upgrade(repl_sv_copy);
3497 repl = SvPV_const(repl_sv_copy, repl_len);
3501 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3502 SvREFCNT_dec(repl_sv_copy);
3505 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST)
3515 Perl_croak(aTHX_ "substr outside of string");
3516 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3523 const IV size = POPi;
3524 const IV offset = POPi;
3525 SV * const src = POPs;
3526 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3529 if (lvalue) { /* it's an lvalue! */
3530 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3531 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3533 LvTARG(ret) = SvREFCNT_inc_simple(src);
3534 LvTARGOFF(ret) = offset;
3535 LvTARGLEN(ret) = size;
3539 SvTAINTED_off(TARG); /* decontaminate */
3543 sv_setuv(ret, do_vecget(src, offset, size));
3551 /* also used for: pp_rindex() */
3564 const char *little_p;
3567 const bool is_index = PL_op->op_type == OP_INDEX;
3568 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3574 big_p = SvPV_const(big, biglen);
3575 little_p = SvPV_const(little, llen);
3577 big_utf8 = DO_UTF8(big);
3578 little_utf8 = DO_UTF8(little);
3579 if (big_utf8 ^ little_utf8) {
3580 /* One needs to be upgraded. */
3581 if (little_utf8 && !IN_ENCODING) {
3582 /* Well, maybe instead we might be able to downgrade the small
3584 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3587 /* If the large string is ISO-8859-1, and it's not possible to
3588 convert the small string to ISO-8859-1, then there is no
3589 way that it could be found anywhere by index. */
3594 /* At this point, pv is a malloc()ed string. So donate it to temp
3595 to ensure it will get free()d */
3596 little = temp = newSV(0);
3597 sv_usepvn(temp, pv, llen);
3598 little_p = SvPVX(little);
3601 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3604 sv_recode_to_utf8(temp, _get_encoding());
3606 sv_utf8_upgrade(temp);
3611 big_p = SvPV_const(big, biglen);
3614 little_p = SvPV_const(little, llen);
3618 if (SvGAMAGIC(big)) {
3619 /* Life just becomes a lot easier if I use a temporary here.
3620 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3621 will trigger magic and overloading again, as will fbm_instr()
3623 big = newSVpvn_flags(big_p, biglen,
3624 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3627 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3628 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3629 warn on undef, and we've already triggered a warning with the
3630 SvPV_const some lines above. We can't remove that, as we need to
3631 call some SvPV to trigger overloading early and find out if the
3633 This is all getting too messy. The API isn't quite clean enough,
3634 because data access has side effects.
3636 little = newSVpvn_flags(little_p, llen,
3637 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3638 little_p = SvPVX(little);
3642 offset = is_index ? 0 : biglen;
3644 if (big_utf8 && offset > 0)
3645 offset = sv_pos_u2b_flags(big, offset, 0, SV_CONST_RETURN);
3651 else if (offset > (SSize_t)biglen)
3653 if (!(little_p = is_index
3654 ? fbm_instr((unsigned char*)big_p + offset,
3655 (unsigned char*)big_p + biglen, little, 0)
3656 : rninstr(big_p, big_p + offset,
3657 little_p, little_p + llen)))
3660 retval = little_p - big_p;
3661 if (retval > 1 && big_utf8)
3662 retval = sv_pos_b2u_flags(big, retval, SV_CONST_RETURN);
3672 dSP; dMARK; dORIGMARK; dTARGET;
3673 SvTAINTED_off(TARG);
3674 do_sprintf(TARG, SP-MARK, MARK+1);
3675 TAINT_IF(SvTAINTED(TARG));
3687 const U8 *s = (U8*)SvPV_const(argsv, len);
3689 if (IN_ENCODING && SvPOK(argsv) && !DO_UTF8(argsv)) {
3690 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3691 s = (U8*)sv_recode_to_utf8(tmpsv, _get_encoding());
3692 len = UTF8SKIP(s); /* Should be well-formed; so this is its length */
3697 ? utf8n_to_uvchr(s, len, 0, UTF8_ALLOW_ANYUV)
3711 if (UNLIKELY(SvAMAGIC(top)))
3713 if (UNLIKELY(isinfnansv(top)))
3714 Perl_croak(aTHX_ "Cannot chr %"NVgf, SvNV(top));
3716 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3717 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3719 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3720 && SvNV_nomg(top) < 0.0)))
3722 if (ckWARN(WARN_UTF8)) {
3723 if (SvGMAGICAL(top)) {
3724 SV *top2 = sv_newmortal();
3725 sv_setsv_nomg(top2, top);
3728 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3729 "Invalid negative number (%"SVf") in chr", SVfARG(top));
3731 value = UNICODE_REPLACEMENT;
3733 value = SvUV_nomg(top);
3737 SvUPGRADE(TARG,SVt_PV);
3739 if (value > 255 && !IN_BYTES) {
3740 SvGROW(TARG, (STRLEN)UVCHR_SKIP(value)+1);
3741 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3742 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3744 (void)SvPOK_only(TARG);
3753 *tmps++ = (char)value;
3755 (void)SvPOK_only(TARG);
3757 if (IN_ENCODING && !IN_BYTES) {
3758 sv_recode_to_utf8(TARG, _get_encoding());
3760 if (SvCUR(TARG) == 0
3761 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3762 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3767 *tmps++ = (char)value;
3783 const char *tmps = SvPV_const(left, len);
3785 if (DO_UTF8(left)) {
3786 /* If Unicode, try to downgrade.
3787 * If not possible, croak.
3788 * Yes, we made this up. */
3789 SV* const tsv = newSVpvn_flags(tmps, len, SVf_UTF8|SVs_TEMP);
3791 sv_utf8_downgrade(tsv, FALSE);
3792 tmps = SvPV_const(tsv, len);
3794 # ifdef USE_ITHREADS
3796 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3797 /* This should be threadsafe because in ithreads there is only
3798 * one thread per interpreter. If this would not be true,
3799 * we would need a mutex to protect this malloc. */
3800 PL_reentrant_buffer->_crypt_struct_buffer =
3801 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3802 #if defined(__GLIBC__) || defined(__EMX__)
3803 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3804 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3805 /* work around glibc-2.2.5 bug */
3806 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3810 # endif /* HAS_CRYPT_R */
3811 # endif /* USE_ITHREADS */
3813 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3815 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3822 "The crypt() function is unimplemented due to excessive paranoia.");
3826 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3827 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3830 /* also used for: pp_lcfirst() */
3834 /* Actually is both lcfirst() and ucfirst(). Only the first character
3835 * changes. This means that possibly we can change in-place, ie., just
3836 * take the source and change that one character and store it back, but not
3837 * if read-only etc, or if the length changes */
3841 STRLEN slen; /* slen is the byte length of the whole SV. */
3844 bool inplace; /* ? Convert first char only, in-place */
3845 bool doing_utf8 = FALSE; /* ? using utf8 */
3846 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3847 const int op_type = PL_op->op_type;
3850 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3851 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3852 * stored as UTF-8 at s. */
3853 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3854 * lowercased) character stored in tmpbuf. May be either
3855 * UTF-8 or not, but in either case is the number of bytes */
3857 s = (const U8*)SvPV_const(source, slen);
3859 /* We may be able to get away with changing only the first character, in
3860 * place, but not if read-only, etc. Later we may discover more reasons to
3861 * not convert in-place. */
3862 inplace = !SvREADONLY(source)
3863 && ( SvPADTMP(source)
3864 || ( SvTEMP(source) && !SvSMAGICAL(source)
3865 && SvREFCNT(source) == 1));
3867 /* First calculate what the changed first character should be. This affects
3868 * whether we can just swap it out, leaving the rest of the string unchanged,
3869 * or even if have to convert the dest to UTF-8 when the source isn't */
3871 if (! slen) { /* If empty */
3872 need = 1; /* still need a trailing NUL */
3875 else if (DO_UTF8(source)) { /* Is the source utf8? */
3878 if (op_type == OP_UCFIRST) {
3879 #ifdef USE_LOCALE_CTYPE
3880 _to_utf8_title_flags(s, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
3882 _to_utf8_title_flags(s, tmpbuf, &tculen, 0);
3886 #ifdef USE_LOCALE_CTYPE
3887 _to_utf8_lower_flags(s, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
3889 _to_utf8_lower_flags(s, tmpbuf, &tculen, 0);
3893 /* we can't do in-place if the length changes. */
3894 if (ulen != tculen) inplace = FALSE;
3895 need = slen + 1 - ulen + tculen;
3897 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3898 * latin1 is treated as caseless. Note that a locale takes
3900 ulen = 1; /* Original character is 1 byte */
3901 tculen = 1; /* Most characters will require one byte, but this will
3902 * need to be overridden for the tricky ones */
3905 if (op_type == OP_LCFIRST) {
3907 /* lower case the first letter: no trickiness for any character */
3908 #ifdef USE_LOCALE_CTYPE
3909 if (IN_LC_RUNTIME(LC_CTYPE)) {
3910 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
3911 *tmpbuf = toLOWER_LC(*s);
3916 *tmpbuf = (IN_UNI_8_BIT)
3917 ? toLOWER_LATIN1(*s)
3921 #ifdef USE_LOCALE_CTYPE
3923 else if (IN_LC_RUNTIME(LC_CTYPE)) {
3924 if (IN_UTF8_CTYPE_LOCALE) {
3928 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
3929 *tmpbuf = (U8) toUPPER_LC(*s); /* This would be a bug if any
3930 locales have upper and title case
3934 else if (! IN_UNI_8_BIT) {
3935 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3936 * on EBCDIC machines whatever the
3937 * native function does */
3940 /* Here, is ucfirst non-UTF-8, not in locale (unless that locale is
3941 * UTF-8, which we treat as not in locale), and cased latin1 */
3943 #ifdef USE_LOCALE_CTYPE
3947 title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3949 assert(tculen == 2);
3951 /* If the result is an upper Latin1-range character, it can
3952 * still be represented in one byte, which is its ordinal */
3953 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3954 *tmpbuf = (U8) title_ord;
3958 /* Otherwise it became more than one ASCII character (in
3959 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3960 * beyond Latin1, so the number of bytes changed, so can't
3961 * replace just the first character in place. */
3964 /* If the result won't fit in a byte, the entire result
3965 * will have to be in UTF-8. Assume worst case sizing in
3966 * conversion. (all latin1 characters occupy at most two
3968 if (title_ord > 255) {
3970 convert_source_to_utf8 = TRUE;
3971 need = slen * 2 + 1;
3973 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3974 * (both) characters whose title case is above 255 is
3978 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3979 need = slen + 1 + 1;
3983 } /* End of use Unicode (Latin1) semantics */
3984 } /* End of changing the case of the first character */
3986 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3987 * generate the result */
3990 /* We can convert in place. This means we change just the first
3991 * character without disturbing the rest; no need to grow */
3993 s = d = (U8*)SvPV_force_nomg(source, slen);
3999 /* Here, we can't convert in place; we earlier calculated how much
4000 * space we will need, so grow to accommodate that */
4001 SvUPGRADE(dest, SVt_PV);
4002 d = (U8*)SvGROW(dest, need);
4003 (void)SvPOK_only(dest);
4010 if (! convert_source_to_utf8) {
4012 /* Here both source and dest are in UTF-8, but have to create
4013 * the entire output. We initialize the result to be the
4014 * title/lower cased first character, and then append the rest
4016 sv_setpvn(dest, (char*)tmpbuf, tculen);
4018 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
4022 const U8 *const send = s + slen;
4024 /* Here the dest needs to be in UTF-8, but the source isn't,
4025 * except we earlier UTF-8'd the first character of the source
4026 * into tmpbuf. First put that into dest, and then append the
4027 * rest of the source, converting it to UTF-8 as we go. */
4029 /* Assert tculen is 2 here because the only two characters that
4030 * get to this part of the code have 2-byte UTF-8 equivalents */
4032 *d++ = *(tmpbuf + 1);
4033 s++; /* We have just processed the 1st char */
4035 for (; s < send; s++) {
4036 d = uvchr_to_utf8(d, *s);
4039 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4043 else { /* in-place UTF-8. Just overwrite the first character */
4044 Copy(tmpbuf, d, tculen, U8);
4045 SvCUR_set(dest, need - 1);
4049 else { /* Neither source nor dest are in or need to be UTF-8 */
4051 if (inplace) { /* in-place, only need to change the 1st char */
4054 else { /* Not in-place */
4056 /* Copy the case-changed character(s) from tmpbuf */
4057 Copy(tmpbuf, d, tculen, U8);
4058 d += tculen - 1; /* Code below expects d to point to final
4059 * character stored */
4062 else { /* empty source */
4063 /* See bug #39028: Don't taint if empty */
4067 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
4068 * the destination to retain that flag */
4069 if (SvUTF8(source) && ! IN_BYTES)
4072 if (!inplace) { /* Finish the rest of the string, unchanged */
4073 /* This will copy the trailing NUL */
4074 Copy(s + 1, d + 1, slen, U8);
4075 SvCUR_set(dest, need - 1);
4078 #ifdef USE_LOCALE_CTYPE
4079 if (IN_LC_RUNTIME(LC_CTYPE)) {
4084 if (dest != source && SvTAINTED(source))
4090 /* There's so much setup/teardown code common between uc and lc, I wonder if
4091 it would be worth merging the two, and just having a switch outside each
4092 of the three tight loops. There is less and less commonality though */
4105 if ((SvPADTMP(source)
4107 (SvTEMP(source) && !SvSMAGICAL(source) && SvREFCNT(source) == 1))
4108 && !SvREADONLY(source) && SvPOK(source)
4111 #ifdef USE_LOCALE_CTYPE
4112 (IN_LC_RUNTIME(LC_CTYPE))
4113 ? ! IN_UTF8_CTYPE_LOCALE
4119 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4120 * make the loop tight, so we overwrite the source with the dest before
4121 * looking at it, and we need to look at the original source
4122 * afterwards. There would also need to be code added to handle
4123 * switching to not in-place in midstream if we run into characters
4124 * that change the length. Since being in locale overrides UNI_8_BIT,
4125 * that latter becomes irrelevant in the above test; instead for
4126 * locale, the size can't normally change, except if the locale is a
4129 s = d = (U8*)SvPV_force_nomg(source, len);
4136 s = (const U8*)SvPV_nomg_const(source, len);
4139 SvUPGRADE(dest, SVt_PV);
4140 d = (U8*)SvGROW(dest, min);
4141 (void)SvPOK_only(dest);
4146 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4147 to check DO_UTF8 again here. */
4149 if (DO_UTF8(source)) {
4150 const U8 *const send = s + len;
4151 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4153 /* All occurrences of these are to be moved to follow any other marks.
4154 * This is context-dependent. We may not be passed enough context to
4155 * move the iota subscript beyond all of them, but we do the best we can
4156 * with what we're given. The result is always better than if we
4157 * hadn't done this. And, the problem would only arise if we are
4158 * passed a character without all its combining marks, which would be
4159 * the caller's mistake. The information this is based on comes from a
4160 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4161 * itself) and so can't be checked properly to see if it ever gets
4162 * revised. But the likelihood of it changing is remote */
4163 bool in_iota_subscript = FALSE;
4169 if (in_iota_subscript && ! _is_utf8_mark(s)) {
4171 /* A non-mark. Time to output the iota subscript */
4172 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
4173 d += capital_iota_len;
4174 in_iota_subscript = FALSE;
4177 /* Then handle the current character. Get the changed case value
4178 * and copy it to the output buffer */
4181 #ifdef USE_LOCALE_CTYPE
4182 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
4184 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen, 0);
4186 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
4187 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4188 if (uv == GREEK_CAPITAL_LETTER_IOTA
4189 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4191 in_iota_subscript = TRUE;
4194 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4195 /* If the eventually required minimum size outgrows the
4196 * available space, we need to grow. */
4197 const UV o = d - (U8*)SvPVX_const(dest);
4199 /* If someone uppercases one million U+03B0s we SvGROW()
4200 * one million times. Or we could try guessing how much to
4201 * allocate without allocating too much. Such is life.
4202 * See corresponding comment in lc code for another option
4205 d = (U8*)SvPVX(dest) + o;
4207 Copy(tmpbuf, d, ulen, U8);
4212 if (in_iota_subscript) {
4213 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
4214 d += capital_iota_len;
4219 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4221 else { /* Not UTF-8 */
4223 const U8 *const send = s + len;
4225 /* Use locale casing if in locale; regular style if not treating
4226 * latin1 as having case; otherwise the latin1 casing. Do the
4227 * whole thing in a tight loop, for speed, */
4228 #ifdef USE_LOCALE_CTYPE
4229 if (IN_LC_RUNTIME(LC_CTYPE)) {
4230 if (IN_UTF8_CTYPE_LOCALE) {
4233 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
4234 for (; s < send; d++, s++)
4235 *d = (U8) toUPPER_LC(*s);
4239 if (! IN_UNI_8_BIT) {
4240 for (; s < send; d++, s++) {
4245 #ifdef USE_LOCALE_CTYPE
4248 for (; s < send; d++, s++) {
4249 *d = toUPPER_LATIN1_MOD(*s);
4250 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
4254 /* The mainstream case is the tight loop above. To avoid
4255 * extra tests in that, all three characters that require
4256 * special handling are mapped by the MOD to the one tested
4258 * Use the source to distinguish between the three cases */
4260 #if UNICODE_MAJOR_VERSION > 2 \
4261 || (UNICODE_MAJOR_VERSION == 2 && UNICODE_DOT_VERSION >= 1 \
4262 && UNICODE_DOT_DOT_VERSION >= 8)
4263 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4265 /* uc() of this requires 2 characters, but they are
4266 * ASCII. If not enough room, grow the string */
4267 if (SvLEN(dest) < ++min) {
4268 const UV o = d - (U8*)SvPVX_const(dest);
4270 d = (U8*)SvPVX(dest) + o;
4272 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4273 continue; /* Back to the tight loop; still in ASCII */
4277 /* The other two special handling characters have their
4278 * upper cases outside the latin1 range, hence need to be
4279 * in UTF-8, so the whole result needs to be in UTF-8. So,
4280 * here we are somewhere in the middle of processing a
4281 * non-UTF-8 string, and realize that we will have to convert
4282 * the whole thing to UTF-8. What to do? There are
4283 * several possibilities. The simplest to code is to
4284 * convert what we have so far, set a flag, and continue on
4285 * in the loop. The flag would be tested each time through
4286 * the loop, and if set, the next character would be
4287 * converted to UTF-8 and stored. But, I (khw) didn't want
4288 * to slow down the mainstream case at all for this fairly
4289 * rare case, so I didn't want to add a test that didn't
4290 * absolutely have to be there in the loop, besides the
4291 * possibility that it would get too complicated for
4292 * optimizers to deal with. Another possibility is to just
4293 * give up, convert the source to UTF-8, and restart the
4294 * function that way. Another possibility is to convert
4295 * both what has already been processed and what is yet to
4296 * come separately to UTF-8, then jump into the loop that
4297 * handles UTF-8. But the most efficient time-wise of the
4298 * ones I could think of is what follows, and turned out to
4299 * not require much extra code. */
4301 /* Convert what we have so far into UTF-8, telling the
4302 * function that we know it should be converted, and to
4303 * allow extra space for what we haven't processed yet.
4304 * Assume the worst case space requirements for converting
4305 * what we haven't processed so far: that it will require
4306 * two bytes for each remaining source character, plus the
4307 * NUL at the end. This may cause the string pointer to
4308 * move, so re-find it. */
4310 len = d - (U8*)SvPVX_const(dest);
4311 SvCUR_set(dest, len);
4312 len = sv_utf8_upgrade_flags_grow(dest,
4313 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4315 d = (U8*)SvPVX(dest) + len;
4317 /* Now process the remainder of the source, converting to
4318 * upper and UTF-8. If a resulting byte is invariant in
4319 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4320 * append it to the output. */
4321 for (; s < send; s++) {
4322 (void) _to_upper_title_latin1(*s, d, &len, 'S');
4326 /* Here have processed the whole source; no need to continue
4327 * with the outer loop. Each character has been converted
4328 * to upper case and converted to UTF-8 */
4331 } /* End of processing all latin1-style chars */
4332 } /* End of processing all chars */
4333 } /* End of source is not empty */
4335 if (source != dest) {
4336 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4337 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4339 } /* End of isn't utf8 */
4340 #ifdef USE_LOCALE_CTYPE
4341 if (IN_LC_RUNTIME(LC_CTYPE)) {
4346 if (dest != source && SvTAINTED(source))
4364 if ( ( SvPADTMP(source)
4365 || ( SvTEMP(source) && !SvSMAGICAL(source)
4366 && SvREFCNT(source) == 1 )
4368 && !SvREADONLY(source) && SvPOK(source)
4369 && !DO_UTF8(source)) {
4371 /* We can convert in place, as lowercasing anything in the latin1 range
4372 * (or else DO_UTF8 would have been on) doesn't lengthen it */
4374 s = d = (U8*)SvPV_force_nomg(source, len);
4381 s = (const U8*)SvPV_nomg_const(source, len);
4384 SvUPGRADE(dest, SVt_PV);
4385 d = (U8*)SvGROW(dest, min);
4386 (void)SvPOK_only(dest);
4391 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4392 to check DO_UTF8 again here. */
4394 if (DO_UTF8(source)) {
4395 const U8 *const send = s + len;
4396 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4399 const STRLEN u = UTF8SKIP(s);
4402 #ifdef USE_LOCALE_CTYPE
4403 _to_utf8_lower_flags(s, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
4405 _to_utf8_lower_flags(s, tmpbuf, &ulen, 0);
4408 /* Here is where we would do context-sensitive actions. See the
4409 * commit message for 86510fb15 for why there isn't any */
4411 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4413 /* If the eventually required minimum size outgrows the
4414 * available space, we need to grow. */
4415 const UV o = d - (U8*)SvPVX_const(dest);
4417 /* If someone lowercases one million U+0130s we SvGROW() one
4418 * million times. Or we could try guessing how much to
4419 * allocate without allocating too much. Such is life.
4420 * Another option would be to grow an extra byte or two more
4421 * each time we need to grow, which would cut down the million
4422 * to 500K, with little waste */
4424 d = (U8*)SvPVX(dest) + o;
4427 /* Copy the newly lowercased letter to the output buffer we're
4429 Copy(tmpbuf, d, ulen, U8);
4432 } /* End of looping through the source string */
4435 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4436 } else { /* Not utf8 */
4438 const U8 *const send = s + len;
4440 /* Use locale casing if in locale; regular style if not treating
4441 * latin1 as having case; otherwise the latin1 casing. Do the
4442 * whole thing in a tight loop, for speed, */
4443 #ifdef USE_LOCALE_CTYPE
4444 if (IN_LC_RUNTIME(LC_CTYPE)) {
4445 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
4446 for (; s < send; d++, s++)
4447 *d = toLOWER_LC(*s);
4451 if (! IN_UNI_8_BIT) {
4452 for (; s < send; d++, s++) {
4457 for (; s < send; d++, s++) {
4458 *d = toLOWER_LATIN1(*s);
4462 if (source != dest) {
4464 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4467 #ifdef USE_LOCALE_CTYPE
4468 if (IN_LC_RUNTIME(LC_CTYPE)) {
4473 if (dest != source && SvTAINTED(source))
4482 SV * const sv = TOPs;
4484 const char *s = SvPV_const(sv,len);
4486 SvUTF8_off(TARG); /* decontaminate */
4489 SvUPGRADE(TARG, SVt_PV);
4490 SvGROW(TARG, (len * 2) + 1);
4494 STRLEN ulen = UTF8SKIP(s);
4495 bool to_quote = FALSE;
4497 if (UTF8_IS_INVARIANT(*s)) {
4498 if (_isQUOTEMETA(*s)) {
4502 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4504 #ifdef USE_LOCALE_CTYPE
4505 /* In locale, we quote all non-ASCII Latin1 chars.
4506 * Otherwise use the quoting rules */
4508 IN_LC_RUNTIME(LC_CTYPE)
4511 _isQUOTEMETA(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s + 1))))
4516 else if (is_QUOTEMETA_high(s)) {
4531 else if (IN_UNI_8_BIT) {
4533 if (_isQUOTEMETA(*s))
4539 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4540 * including everything above ASCII */
4542 if (!isWORDCHAR_A(*s))
4548 SvCUR_set(TARG, d - SvPVX_const(TARG));
4549 (void)SvPOK_only_UTF8(TARG);
4552 sv_setpvn(TARG, s, len);
4568 U8 tmpbuf[UTF8_MAXBYTES_CASE + 1];
4569 #if UNICODE_MAJOR_VERSION > 3 /* no multifolds in early Unicode */ \
4570 || (UNICODE_MAJOR_VERSION == 3 && ( UNICODE_DOT_VERSION > 0) \
4571 || UNICODE_DOT_DOT_VERSION > 0)
4572 const bool full_folding = TRUE; /* This variable is here so we can easily
4573 move to more generality later */
4575 const bool full_folding = FALSE;
4577 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4578 #ifdef USE_LOCALE_CTYPE
4579 | ( IN_LC_RUNTIME(LC_CTYPE) ? FOLD_FLAGS_LOCALE : 0 )
4583 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4584 * You are welcome(?) -Hugmeir
4592 s = (const U8*)SvPV_nomg_const(source, len);
4594 if (ckWARN(WARN_UNINITIALIZED))
4595 report_uninit(source);
4602 SvUPGRADE(dest, SVt_PV);
4603 d = (U8*)SvGROW(dest, min);
4604 (void)SvPOK_only(dest);
4609 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4611 const STRLEN u = UTF8SKIP(s);
4614 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags);
4616 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4617 const UV o = d - (U8*)SvPVX_const(dest);
4619 d = (U8*)SvPVX(dest) + o;
4622 Copy(tmpbuf, d, ulen, U8);
4627 } /* Unflagged string */
4629 #ifdef USE_LOCALE_CTYPE
4630 if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
4631 if (IN_UTF8_CTYPE_LOCALE) {
4632 goto do_uni_folding;
4634 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
4635 for (; s < send; d++, s++)
4636 *d = (U8) toFOLD_LC(*s);
4640 if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4641 for (; s < send; d++, s++)
4645 #ifdef USE_LOCALE_CTYPE
4648 /* For ASCII and the Latin-1 range, there's only two troublesome
4649 * folds, \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
4650 * casefolding becomes 'ss'; and \x{B5} (\N{MICRO SIGN}), which
4651 * under any fold becomes \x{3BC} (\N{GREEK SMALL LETTER MU}) --
4652 * For the rest, the casefold is their lowercase. */
4653 for (; s < send; d++, s++) {
4654 if (*s == MICRO_SIGN) {
4655 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
4656 * which is outside of the latin-1 range. There's a couple
4657 * of ways to deal with this -- khw discusses them in
4658 * pp_lc/uc, so go there :) What we do here is upgrade what
4659 * we had already casefolded, then enter an inner loop that
4660 * appends the rest of the characters as UTF-8. */
4661 len = d - (U8*)SvPVX_const(dest);
4662 SvCUR_set(dest, len);
4663 len = sv_utf8_upgrade_flags_grow(dest,
4664 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4665 /* The max expansion for latin1
4666 * chars is 1 byte becomes 2 */
4668 d = (U8*)SvPVX(dest) + len;
4670 Copy(GREEK_SMALL_LETTER_MU_UTF8, d, small_mu_len, U8);
4673 for (; s < send; s++) {
4675 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4676 if UVCHR_IS_INVARIANT(fc) {
4678 && *s == LATIN_SMALL_LETTER_SHARP_S)
4687 Copy(tmpbuf, d, ulen, U8);
4693 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4694 /* Under full casefolding, LATIN SMALL LETTER SHARP S
4695 * becomes "ss", which may require growing the SV. */
4696 if (SvLEN(dest) < ++min) {
4697 const UV o = d - (U8*)SvPVX_const(dest);
4699 d = (U8*)SvPVX(dest) + o;
4704 else { /* If it's not one of those two, the fold is their lower
4706 *d = toLOWER_LATIN1(*s);
4712 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4714 #ifdef USE_LOCALE_CTYPE
4715 if (IN_LC_RUNTIME(LC_CTYPE)) {
4720 if (SvTAINTED(source))
4730 dSP; dMARK; dORIGMARK;
4731 AV *const av = MUTABLE_AV(POPs);
4732 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4734 if (SvTYPE(av) == SVt_PVAV) {
4735 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4736 bool can_preserve = FALSE;
4742 can_preserve = SvCANEXISTDELETE(av);
4745 if (lval && localizing) {
4748 for (svp = MARK + 1; svp <= SP; svp++) {
4749 const SSize_t elem = SvIV(*svp);
4753 if (max > AvMAX(av))
4757 while (++MARK <= SP) {
4759 SSize_t elem = SvIV(*MARK);
4760 bool preeminent = TRUE;
4762 if (localizing && can_preserve) {
4763 /* If we can determine whether the element exist,
4764 * Try to preserve the existenceness of a tied array
4765 * element by using EXISTS and DELETE if possible.
4766 * Fallback to FETCH and STORE otherwise. */
4767 preeminent = av_exists(av, elem);
4770 svp = av_fetch(av, elem, lval);
4773 DIE(aTHX_ PL_no_aelem, elem);
4776 save_aelem(av, elem, svp);
4778 SAVEADELETE(av, elem);
4781 *MARK = svp ? *svp : &PL_sv_undef;
4784 if (GIMME_V != G_ARRAY) {
4786 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4795 AV *const av = MUTABLE_AV(POPs);
4796 I32 lval = (PL_op->op_flags & OPf_MOD);
4797 SSize_t items = SP - MARK;
4799 if (PL_op->op_private & OPpMAYBE_LVSUB) {
4800 const I32 flags = is_lvalue_sub();
4802 if (!(flags & OPpENTERSUB_INARGS))
4803 /* diag_listed_as: Can't modify %s in %s */
4804 Perl_croak(aTHX_ "Can't modify index/value array slice in list assignment");
4811 *(MARK+items*2-1) = *(MARK+items);
4817 while (++MARK <= SP) {
4820 svp = av_fetch(av, SvIV(*MARK), lval);
4822 if (!svp || !*svp || *svp == &PL_sv_undef) {
4823 DIE(aTHX_ PL_no_aelem, SvIV(*MARK));
4825 *MARK = sv_mortalcopy(*MARK);
4827 *++MARK = svp ? *svp : &PL_sv_undef;
4829 if (GIMME_V != G_ARRAY) {
4830 MARK = SP - items*2;
4831 *++MARK = items > 0 ? *SP : &PL_sv_undef;
4841 AV *array = MUTABLE_AV(POPs);
4842 const I32 gimme = GIMME_V;
4843 IV *iterp = Perl_av_iter_p(aTHX_ array);
4844 const IV current = (*iterp)++;
4846 if (current > av_tindex(array)) {
4848 if (gimme == G_SCALAR)
4856 if (gimme == G_ARRAY) {
4857 SV **const element = av_fetch(array, current, 0);
4858 PUSHs(element ? *element : &PL_sv_undef);
4863 /* also used for: pp_avalues()*/
4867 AV *array = MUTABLE_AV(POPs);
4868 const I32 gimme = GIMME_V;
4870 *Perl_av_iter_p(aTHX_ array) = 0;
4872 if (gimme == G_SCALAR) {
4874 PUSHi(av_tindex(array) + 1);
4876 else if (gimme == G_ARRAY) {
4877 IV n = Perl_av_len(aTHX_ array);
4882 if (PL_op->op_type == OP_AKEYS) {
4883 for (i = 0; i <= n; i++) {
4888 for (i = 0; i <= n; i++) {
4889 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4890 PUSHs(elem ? *elem : &PL_sv_undef);
4897 /* Associative arrays. */
4902 HV * hash = MUTABLE_HV(POPs);
4904 const I32 gimme = GIMME_V;
4906 entry = hv_iternext(hash);
4910 SV* const sv = hv_iterkeysv(entry);
4912 if (gimme == G_ARRAY) {
4914 val = hv_iterval(hash, entry);
4918 else if (gimme == G_SCALAR)
4925 S_do_delete_local(pTHX)
4928 const I32 gimme = GIMME_V;
4931 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4932 SV **unsliced_keysv = sliced ? NULL : sp--;
4933 SV * const osv = POPs;
4934 SV **mark = sliced ? PL_stack_base + POPMARK : unsliced_keysv-1;
4936 const bool tied = SvRMAGICAL(osv)
4937 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4938 const bool can_preserve = SvCANEXISTDELETE(osv);
4939 const U32 type = SvTYPE(osv);
4940 SV ** const end = sliced ? SP : unsliced_keysv;
4942 if (type == SVt_PVHV) { /* hash element */
4943 HV * const hv = MUTABLE_HV(osv);
4944 while (++MARK <= end) {
4945 SV * const keysv = *MARK;
4947 bool preeminent = TRUE;
4949 preeminent = hv_exists_ent(hv, keysv, 0);
4951 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4958 sv = hv_delete_ent(hv, keysv, 0, 0);
4960 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4963 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4964 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4966 *MARK = sv_mortalcopy(sv);
4972 SAVEHDELETE(hv, keysv);
4973 *MARK = &PL_sv_undef;
4977 else if (type == SVt_PVAV) { /* array element */
4978 if (PL_op->op_flags & OPf_SPECIAL) {
4979 AV * const av = MUTABLE_AV(osv);
4980 while (++MARK <= end) {
4981 SSize_t idx = SvIV(*MARK);
4983 bool preeminent = TRUE;
4985 preeminent = av_exists(av, idx);
4987 SV **svp = av_fetch(av, idx, 1);
4994 sv = av_delete(av, idx, 0);
4996 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4999 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
5001 *MARK = sv_mortalcopy(sv);
5007 SAVEADELETE(av, idx);
5008 *MARK = &PL_sv_undef;
5013 DIE(aTHX_ "panic: avhv_delete no longer supported");
5016 DIE(aTHX_ "Not a HASH reference");
5018 if (gimme == G_VOID)
5020 else if (gimme == G_SCALAR) {
5025 *++MARK = &PL_sv_undef;
5029 else if (gimme != G_VOID)
5030 PUSHs(*unsliced_keysv);
5041 if (PL_op->op_private & OPpLVAL_INTRO)
5042 return do_delete_local();
5045 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5047 if (PL_op->op_private & OPpSLICE) {
5049 HV * const hv = MUTABLE_HV(POPs);
5050 const U32 hvtype = SvTYPE(hv);
5051 if (hvtype == SVt_PVHV) { /* hash element */
5052 while (++MARK <= SP) {
5053 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
5054 *MARK = sv ? sv : &PL_sv_undef;
5057 else if (hvtype == SVt_PVAV) { /* array element */
5058 if (PL_op->op_flags & OPf_SPECIAL) {
5059 while (++MARK <= SP) {
5060 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
5061 *MARK = sv ? sv : &PL_sv_undef;
5066 DIE(aTHX_ "Not a HASH reference");
5069 else if (gimme == G_SCALAR) {
5074 *++MARK = &PL_sv_undef;
5080 HV * const hv = MUTABLE_HV(POPs);
5082 if (SvTYPE(hv) == SVt_PVHV)
5083 sv = hv_delete_ent(hv, keysv, discard, 0);
5084 else if (SvTYPE(hv) == SVt_PVAV) {
5085 if (PL_op->op_flags & OPf_SPECIAL)
5086 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
5088 DIE(aTHX_ "panic: avhv_delete no longer supported");
5091 DIE(aTHX_ "Not a HASH reference");
5106 if (UNLIKELY( PL_op->op_private & OPpEXISTS_SUB )) {
5108 SV * const sv = POPs;
5109 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
5112 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
5117 hv = MUTABLE_HV(POPs);
5118 if (LIKELY( SvTYPE(hv) == SVt_PVHV )) {
5119 if (hv_exists_ent(hv, tmpsv, 0))
5122 else if (SvTYPE(hv) == SVt_PVAV) {
5123 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
5124 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
5129 DIE(aTHX_ "Not a HASH reference");
5136 dSP; dMARK; dORIGMARK;
5137 HV * const hv = MUTABLE_HV(POPs);
5138 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
5139 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
5140 bool can_preserve = FALSE;
5146 if (SvCANEXISTDELETE(hv))
5147 can_preserve = TRUE;
5150 while (++MARK <= SP) {
5151 SV * const keysv = *MARK;
5154 bool preeminent = TRUE;
5156 if (localizing && can_preserve) {
5157 /* If we can determine whether the element exist,
5158 * try to preserve the existenceness of a tied hash
5159 * element by using EXISTS and DELETE if possible.
5160 * Fallback to FETCH and STORE otherwise. */
5161 preeminent = hv_exists_ent(hv, keysv, 0);
5164 he = hv_fetch_ent(hv, keysv, lval, 0);
5165 svp = he ? &HeVAL(he) : NULL;
5168 if (!svp || !*svp || *svp == &PL_sv_undef) {
5169 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5172 if (HvNAME_get(hv) && isGV(*svp))
5173 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
5174 else if (preeminent)
5175 save_helem_flags(hv, keysv, svp,
5176 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5178 SAVEHDELETE(hv, keysv);
5181 *MARK = svp && *svp ? *svp : &PL_sv_undef;
5183 if (GIMME_V != G_ARRAY) {
5185 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5194 HV * const hv = MUTABLE_HV(POPs);
5195 I32 lval = (PL_op->op_flags & OPf_MOD);
5196 SSize_t items = SP - MARK;
5198 if (PL_op->op_private & OPpMAYBE_LVSUB) {
5199 const I32 flags = is_lvalue_sub();
5201 if (!(flags & OPpENTERSUB_INARGS))
5202 /* diag_listed_as: Can't modify %s in %s */
5203 Perl_croak(aTHX_ "Can't modify key/value hash slice in list assignment");
5210 *(MARK+items*2-1) = *(MARK+items);
5216 while (++MARK <= SP) {
5217 SV * const keysv = *MARK;
5221 he = hv_fetch_ent(hv, keysv, lval, 0);
5222 svp = he ? &HeVAL(he) : NULL;
5225 if (!svp || !*svp || *svp == &PL_sv_undef) {
5226 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5228 *MARK = sv_mortalcopy(*MARK);
5230 *++MARK = svp && *svp ? *svp : &PL_sv_undef;
5232 if (GIMME_V != G_ARRAY) {
5233 MARK = SP - items*2;
5234 *++MARK = items > 0 ? *SP : &PL_sv_undef;
5240 /* List operators. */
5244 I32 markidx = POPMARK;
5245 if (GIMME_V != G_ARRAY) {
5246 SV **mark = PL_stack_base + markidx;
5249 *MARK = *SP; /* unwanted list, return last item */
5251 *MARK = &PL_sv_undef;
5261 SV ** const lastrelem = PL_stack_sp;
5262 SV ** const lastlelem = PL_stack_base + POPMARK;
5263 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5264 SV ** const firstrelem = lastlelem + 1;
5265 const U8 mod = PL_op->op_flags & OPf_MOD;
5267 const I32 max = lastrelem - lastlelem;
5270 if (GIMME_V != G_ARRAY) {
5271 if (lastlelem < firstlelem) {
5272 *firstlelem = &PL_sv_undef;
5275 I32 ix = SvIV(*lastlelem);
5278 if (ix < 0 || ix >= max)
5279 *firstlelem = &PL_sv_undef;
5281 *firstlelem = firstrelem[ix];
5288 SP = firstlelem - 1;
5292 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
5293 I32 ix = SvIV(*lelem);
5296 if (ix < 0 || ix >= max)
5297 *lelem = &PL_sv_undef;
5299 if (!(*lelem = firstrelem[ix]))
5300 *lelem = &PL_sv_undef;
5301 else if (mod && SvPADTMP(*lelem)) {
5302 *lelem = firstrelem[ix] = sv_mortalcopy(*lelem);
5313 const I32 items = SP - MARK;
5314 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5316 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5317 ? newRV_noinc(av) : av);
5323 dSP; dMARK; dORIGMARK;
5324 HV* const hv = newHV();
5325 SV* const retval = sv_2mortal( PL_op->op_flags & OPf_SPECIAL
5326 ? newRV_noinc(MUTABLE_SV(hv))
5331 (MARK++, SvGMAGICAL(*MARK) ? sv_mortalcopy(*MARK) : *MARK);
5338 sv_setsv_nomg(val, *MARK);
5342 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5345 (void)hv_store_ent(hv,key,val,0);
5353 S_deref_plain_array(pTHX_ AV *ary)
5355 if (SvTYPE(ary) == SVt_PVAV) return ary;
5356 SvGETMAGIC((SV *)ary);
5357 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
5358 Perl_die(aTHX_ "Not an ARRAY reference");
5359 else if (SvOBJECT(SvRV(ary)))
5360 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
5361 return (AV *)SvRV(ary);
5364 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
5365 # define DEREF_PLAIN_ARRAY(ary) \
5368 SvTYPE(aRrRay) == SVt_PVAV \
5370 : S_deref_plain_array(aTHX_ aRrRay); \
5373 # define DEREF_PLAIN_ARRAY(ary) \
5375 PL_Sv = (SV *)(ary), \
5376 SvTYPE(PL_Sv) == SVt_PVAV \
5378 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
5384 dSP; dMARK; dORIGMARK;
5385 int num_args = (SP - MARK);
5386 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5395 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5398 return Perl_tied_method(aTHX_ SV_CONST(SPLICE), mark - 1, MUTABLE_SV(ary), mg,
5399 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
5406 offset = i = SvIV(*MARK);
5408 offset += AvFILLp(ary) + 1;
5410 DIE(aTHX_ PL_no_aelem, i);
5412 length = SvIVx(*MARK++);
5414 length += AvFILLp(ary) - offset + 1;
5420 length = AvMAX(ary) + 1; /* close enough to infinity */
5424 length = AvMAX(ary) + 1;
5426 if (offset > AvFILLp(ary) + 1) {
5428 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5429 offset = AvFILLp(ary) + 1;
5431 after = AvFILLp(ary) + 1 - (offset + length);
5432 if (after < 0) { /* not that much array */
5433 length += after; /* offset+length now in array */
5439 /* At this point, MARK .. SP-1 is our new LIST */
5442 diff = newlen - length;
5443 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5446 /* make new elements SVs now: avoid problems if they're from the array */
5447 for (dst = MARK, i = newlen; i; i--) {
5448 SV * const h = *dst;
5449 *dst++ = newSVsv(h);
5452 if (diff < 0) { /* shrinking the area */
5453 SV **tmparyval = NULL;
5455 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5456 Copy(MARK, tmparyval, newlen, SV*);
5459 MARK = ORIGMARK + 1;
5460 if (GIMME_V == G_ARRAY) { /* copy return vals to stack */
5461 const bool real = cBOOL(AvREAL(ary));
5462 MEXTEND(MARK, length);
5464 EXTEND_MORTAL(length);
5465 for (i = 0, dst = MARK; i < length; i++) {
5466 if ((*dst = AvARRAY(ary)[i+offset])) {
5468 sv_2mortal(*dst); /* free them eventually */
5471 *dst = &PL_sv_undef;
5477 *MARK = AvARRAY(ary)[offset+length-1];
5480 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5481 SvREFCNT_dec(*dst++); /* free them now */
5484 AvFILLp(ary) += diff;
5486 /* pull up or down? */
5488 if (offset < after) { /* easier to pull up */
5489 if (offset) { /* esp. if nothing to pull */
5490 src = &AvARRAY(ary)[offset-1];
5491 dst = src - diff; /* diff is negative */
5492 for (i = offset; i > 0; i--) /* can't trust Copy */
5496 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5500 if (after) { /* anything to pull down? */
5501 src = AvARRAY(ary) + offset + length;
5502 dst = src + diff; /* diff is negative */
5503 Move(src, dst, after, SV*);
5505 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5506 /* avoid later double free */
5513 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5514 Safefree(tmparyval);
5517 else { /* no, expanding (or same) */
5518 SV** tmparyval = NULL;
5520 Newx(tmparyval, length, SV*); /* so remember deletion */
5521 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5524 if (diff > 0) { /* expanding */
5525 /* push up or down? */
5526 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5530 Move(src, dst, offset, SV*);
5532 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5534 AvFILLp(ary) += diff;
5537 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5538 av_extend(ary, AvFILLp(ary) + diff);
5539 AvFILLp(ary) += diff;
5542 dst = AvARRAY(ary) + AvFILLp(ary);
5544 for (i = after; i; i--) {
5552 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5555 MARK = ORIGMARK + 1;
5556 if (GIMME_V == G_ARRAY) { /* copy return vals to stack */
5558 const bool real = cBOOL(AvREAL(ary));
5560 EXTEND_MORTAL(length);
5561 for (i = 0, dst = MARK; i < length; i++) {
5562 if ((*dst = tmparyval[i])) {
5564 sv_2mortal(*dst); /* free them eventually */
5566 else *dst = &PL_sv_undef;
5572 else if (length--) {
5573 *MARK = tmparyval[length];
5576 while (length-- > 0)
5577 SvREFCNT_dec(tmparyval[length]);
5581 *MARK = &PL_sv_undef;
5582 Safefree(tmparyval);
5586 mg_set(MUTABLE_SV(ary));
5594 dSP; dMARK; dORIGMARK; dTARGET;
5595 AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5596 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5599 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5602 ENTER_with_name("call_PUSH");
5603 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
5604 LEAVE_with_name("call_PUSH");
5605 /* SPAGAIN; not needed: SP is assigned to immediately below */
5608 /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we
5609 * only need to save locally, not on the save stack */
5610 U16 old_delaymagic = PL_delaymagic;
5612 if (SvREADONLY(ary) && MARK < SP) Perl_croak_no_modify();
5613 PL_delaymagic = DM_DELAY;
5614 for (++MARK; MARK <= SP; MARK++) {
5616 if (*MARK) SvGETMAGIC(*MARK);
5619 sv_setsv_nomg(sv, *MARK);
5620 av_store(ary, AvFILLp(ary)+1, sv);
5622 if (PL_delaymagic & DM_ARRAY_ISA)
5623 mg_set(MUTABLE_SV(ary));
5624 PL_delaymagic = old_delaymagic;
5627 if (OP_GIMME(PL_op, 0) != G_VOID) {
5628 PUSHi( AvFILL(ary) + 1 );
5633 /* also used for: pp_pop()*/
5637 AV * const av = PL_op->op_flags & OPf_SPECIAL
5638 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5639 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5643 (void)sv_2mortal(sv);
5650 dSP; dMARK; dORIGMARK; dTARGET;
5651 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5652 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5655 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5658 ENTER_with_name("call_UNSHIFT");
5659 call_sv(SV_CONST(UNSHIFT),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
5660 LEAVE_with_name("call_UNSHIFT");
5661 /* SPAGAIN; not needed: SP is assigned to immediately below */
5664 /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we
5665 * only need to save locally, not on the save stack */
5666 U16 old_delaymagic = PL_delaymagic;
5669 av_unshift(ary, SP - MARK);
5670 PL_delaymagic = DM_DELAY;
5672 SV * const sv = newSVsv(*++MARK);
5673 (void)av_store(ary, i++, sv);
5675 if (PL_delaymagic & DM_ARRAY_ISA)
5676 mg_set(MUTABLE_SV(ary));
5677 PL_delaymagic = old_delaymagic;
5680 if (OP_GIMME(PL_op, 0) != G_VOID) {
5681 PUSHi( AvFILL(ary) + 1 );
5690 if (GIMME_V == G_ARRAY) {
5691 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5695 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5696 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5697 av = MUTABLE_AV((*SP));
5698 /* In-place reversing only happens in void context for the array
5699 * assignment. We don't need to push anything on the stack. */
5702 if (SvMAGICAL(av)) {
5704 SV *tmp = sv_newmortal();
5705 /* For SvCANEXISTDELETE */
5708 bool can_preserve = SvCANEXISTDELETE(av);
5710 for (i = 0, j = av_tindex(av); i < j; ++i, --j) {
5714 if (!av_exists(av, i)) {
5715 if (av_exists(av, j)) {
5716 SV *sv = av_delete(av, j, 0);
5717 begin = *av_fetch(av, i, TRUE);
5718 sv_setsv_mg(begin, sv);
5722 else if (!av_exists(av, j)) {
5723 SV *sv = av_delete(av, i, 0);
5724 end = *av_fetch(av, j, TRUE);
5725 sv_setsv_mg(end, sv);
5730 begin = *av_fetch(av, i, TRUE);
5731 end = *av_fetch(av, j, TRUE);
5732 sv_setsv(tmp, begin);
5733 sv_setsv_mg(begin, end);
5734 sv_setsv_mg(end, tmp);
5738 SV **begin = AvARRAY(av);
5741 SV **end = begin + AvFILLp(av);
5743 while (begin < end) {
5744 SV * const tmp = *begin;
5755 SV * const tmp = *MARK;
5759 /* safe as long as stack cannot get extended in the above */
5770 SvUTF8_off(TARG); /* decontaminate */
5772 do_join(TARG, &PL_sv_no, MARK, SP);
5774 sv_setsv(TARG, SP > MARK ? *SP : DEFSV);
5777 up = SvPV_force(TARG, len);
5779 if (DO_UTF8(TARG)) { /* first reverse each character */
5780 U8* s = (U8*)SvPVX(TARG);
5781 const U8* send = (U8*)(s + len);
5783 if (UTF8_IS_INVARIANT(*s)) {
5788 if (!utf8_to_uvchr_buf(s, send, 0))
5792 down = (char*)(s - 1);
5793 /* reverse this character */
5797 *down-- = (char)tmp;
5803 down = SvPVX(TARG) + len - 1;
5807 *down-- = (char)tmp;
5809 (void)SvPOK_only_UTF8(TARG);
5820 AV *ary = PL_op->op_flags & OPf_STACKED ? (AV *)POPs : NULL;
5821 IV limit = POPi; /* note, negative is forever */
5822 SV * const sv = POPs;
5824 const char *s = SvPV_const(sv, len);
5825 const bool do_utf8 = DO_UTF8(sv);
5826 const char *strend = s + len;
5832 const STRLEN slen = do_utf8
5833 ? utf8_length((U8*)s, (U8*)strend)
5834 : (STRLEN)(strend - s);
5835 SSize_t maxiters = slen + 10;
5836 I32 trailing_empty = 0;
5838 const IV origlimit = limit;
5841 const I32 gimme = GIMME_V;
5843 const I32 oldsave = PL_savestack_ix;
5844 U32 make_mortal = SVs_TEMP;
5849 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5854 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5857 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5858 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5861 if (pm->op_pmreplrootu.op_pmtargetoff) {
5862 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5866 if (pm->op_pmreplrootu.op_pmtargetgv) {
5867 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5871 else if (pm->op_targ)
5872 ary = (AV *)PAD_SVl(pm->op_targ);
5878 (void)sv_2mortal(SvREFCNT_inc_simple_NN(sv));
5881 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5883 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5890 for (i = AvFILLp(ary); i >= 0; i--)
5891 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5893 /* temporarily switch stacks */
5894 SAVESWITCHSTACK(PL_curstack, ary);
5898 base = SP - PL_stack_base;
5900 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5902 while (isSPACE_utf8(s))
5905 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5906 while (isSPACE_LC(*s))
5914 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5918 gimme_scalar = gimme == G_SCALAR && !ary;
5921 limit = maxiters + 2;
5922 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5925 /* this one uses 'm' and is a negative test */
5927 while (m < strend && ! isSPACE_utf8(m) ) {
5928 const int t = UTF8SKIP(m);
5929 /* isSPACE_utf8 returns FALSE for malform utf8 */
5936 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
5938 while (m < strend && !isSPACE_LC(*m))
5941 while (m < strend && !isSPACE(*m))
5954 dstr = newSVpvn_flags(s, m-s,
5955 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5959 /* skip the whitespace found last */
5961 s = m + UTF8SKIP(m);
5965 /* this one uses 's' and is a positive test */
5967 while (s < strend && isSPACE_utf8(s) )
5970 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
5972 while (s < strend && isSPACE_LC(*s))
5975 while (s < strend && isSPACE(*s))
5980 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5982 for (m = s; m < strend && *m != '\n'; m++)
5995 dstr = newSVpvn_flags(s, m-s,
5996 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6002 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
6004 Pre-extend the stack, either the number of bytes or
6005 characters in the string or a limited amount, triggered by:
6007 my ($x, $y) = split //, $str;
6011 if (!gimme_scalar) {
6012 const IV items = limit - 1;
6013 /* setting it to -1 will trigger a panic in EXTEND() */
6014 const SSize_t sslen = slen > SSize_t_MAX ? -1 : (SSize_t)slen;
6015 if (items >=0 && items < sslen)
6023 /* keep track of how many bytes we skip over */
6033 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
6046 dstr = newSVpvn(s, 1);
6062 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
6063 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
6064 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
6065 && !(RX_EXTFLAGS(rx) & RXf_IS_ANCHORED)) {
6066 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
6067 SV * const csv = CALLREG_INTUIT_STRING(rx);
6069 len = RX_MINLENRET(rx);
6070 if (len == 1 && !RX_UTF8(rx) && !tail) {
6071 const char c = *SvPV_nolen_const(csv);
6073 for (m = s; m < strend && *m != c; m++)
6084 dstr = newSVpvn_flags(s, m-s,
6085 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6088 /* The rx->minlen is in characters but we want to step
6089 * s ahead by bytes. */
6091 s = (char*)utf8_hop((U8*)m, len);
6093 s = m + len; /* Fake \n at the end */
6097 while (s < strend && --limit &&
6098 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
6099 csv, multiline ? FBMrf_MULTILINE : 0)) )
6108 dstr = newSVpvn_flags(s, m-s,
6109 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6112 /* The rx->minlen is in characters but we want to step
6113 * s ahead by bytes. */
6115 s = (char*)utf8_hop((U8*)m, len);
6117 s = m + len; /* Fake \n at the end */
6122 maxiters += slen * RX_NPARENS(rx);
6123 while (s < strend && --limit)
6127 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1,
6130 if (rex_return == 0)
6132 TAINT_IF(RX_MATCH_TAINTED(rx));
6133 /* we never pass the REXEC_COPY_STR flag, so it should
6134 * never get copied */
6135 assert(!RX_MATCH_COPIED(rx));
6136 m = RX_OFFS(rx)[0].start + orig;
6145 dstr = newSVpvn_flags(s, m-s,
6146 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6149 if (RX_NPARENS(rx)) {
6151 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
6152 s = RX_OFFS(rx)[i].start + orig;
6153 m = RX_OFFS(rx)[i].end + orig;
6155 /* japhy (07/27/01) -- the (m && s) test doesn't catch
6156 parens that didn't match -- they should be set to
6157 undef, not the empty string */
6165 if (m >= orig && s >= orig) {
6166 dstr = newSVpvn_flags(s, m-s,
6167 (do_utf8 ? SVf_UTF8 : 0)
6171 dstr = &PL_sv_undef; /* undef, not "" */
6177 s = RX_OFFS(rx)[0].end + orig;
6181 if (!gimme_scalar) {
6182 iters = (SP - PL_stack_base) - base;
6184 if (iters > maxiters)
6185 DIE(aTHX_ "Split loop");
6187 /* keep field after final delim? */
6188 if (s < strend || (iters && origlimit)) {
6189 if (!gimme_scalar) {
6190 const STRLEN l = strend - s;
6191 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6196 else if (!origlimit) {
6198 iters -= trailing_empty;
6200 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
6201 if (TOPs && !make_mortal)
6203 *SP-- = &PL_sv_undef;
6210 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
6214 if (SvSMAGICAL(ary)) {
6216 mg_set(MUTABLE_SV(ary));
6219 if (gimme == G_ARRAY) {
6221 Copy(AvARRAY(ary), SP + 1, iters, SV*);
6228 ENTER_with_name("call_PUSH");
6229 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
6230 LEAVE_with_name("call_PUSH");
6232 if (gimme == G_ARRAY) {
6234 /* EXTEND should not be needed - we just popped them */
6236 for (i=0; i < iters; i++) {
6237 SV **svp = av_fetch(ary, i, FALSE);
6238 PUSHs((svp) ? *svp : &PL_sv_undef);
6245 if (gimme == G_ARRAY)
6257 SV *const sv = PAD_SVl(PL_op->op_targ);
6259 if (SvPADSTALE(sv)) {
6262 RETURNOP(cLOGOP->op_other);
6264 RETURNOP(cLOGOP->op_next);
6273 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
6274 || SvTYPE(retsv) == SVt_PVCV) {
6275 retsv = refto(retsv);
6282 /* used for: pp_padany(), pp_mapstart(), pp_custom(); plus any system ops
6283 * that aren't implemented on a particular platform */
6285 PP(unimplemented_op)
6287 const Optype op_type = PL_op->op_type;
6288 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
6289 with out of range op numbers - it only "special" cases op_custom.
6290 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
6291 if we get here for a custom op then that means that the custom op didn't
6292 have an implementation. Given that OP_NAME() looks up the custom op
6293 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
6294 registers &PL_unimplemented_op as the address of their custom op.
6295 NULL doesn't generate a useful error message. "custom" does. */
6296 const char *const name = op_type >= OP_max
6297 ? "[out of range]" : PL_op_name[PL_op->op_type];
6298 if(OP_IS_SOCKET(op_type))
6299 DIE(aTHX_ PL_no_sock_func, name);
6300 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
6303 /* For sorting out arguments passed to a &CORE:: subroutine */
6307 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
6308 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
6309 AV * const at_ = GvAV(PL_defgv);
6310 SV **svp = at_ ? AvARRAY(at_) : NULL;
6311 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
6312 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
6313 bool seen_question = 0;
6314 const char *err = NULL;
6315 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
6317 /* Count how many args there are first, to get some idea how far to
6318 extend the stack. */
6320 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
6322 if (oa & OA_OPTIONAL) seen_question = 1;
6323 if (!seen_question) minargs++;
6327 if(numargs < minargs) err = "Not enough";
6328 else if(numargs > maxargs) err = "Too many";
6330 /* diag_listed_as: Too many arguments for %s */
6332 "%s arguments for %s", err,
6333 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
6336 /* Reset the stack pointer. Without this, we end up returning our own
6337 arguments in list context, in addition to the values we are supposed
6338 to return. nextstate usually does this on sub entry, but we need
6339 to run the next op with the caller's hints, so we cannot have a
6341 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
6343 if(!maxargs) RETURN;
6345 /* We do this here, rather than with a separate pushmark op, as it has
6346 to come in between two things this function does (stack reset and
6347 arg pushing). This seems the easiest way to do it. */
6350 (void)Perl_pp_pushmark(aTHX);
6353 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
6354 PUTBACK; /* The code below can die in various places. */
6356 oa = PL_opargs[opnum] >> OASHIFT;
6357 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
6362 if (!numargs && defgv && whicharg == minargs + 1) {
6365 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
6369 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
6374 if (!svp || !*svp || !SvROK(*svp)
6375 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
6377 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
6378 "Type of arg %d to &CORE::%s must be hash reference",
6379 whicharg, OP_DESC(PL_op->op_next)
6384 if (!numargs) PUSHs(NULL);
6385 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
6386 /* no magic here, as the prototype will have added an extra
6387 refgen and we just want what was there before that */
6390 const bool constr = PL_op->op_private & whicharg;
6392 svp && *svp ? *svp : &PL_sv_undef,
6393 constr, cBOOL(CopHINTS_get(PL_curcop) & HINT_STRICT_REFS),
6399 if (!numargs) goto try_defsv;
6401 const bool wantscalar =
6402 PL_op->op_private & OPpCOREARGS_SCALARMOD;
6403 if (!svp || !*svp || !SvROK(*svp)
6404 /* We have to permit globrefs even for the \$ proto, as
6405 *foo is indistinguishable from ${\*foo}, and the proto-
6406 type permits the latter. */
6407 || SvTYPE(SvRV(*svp)) > (
6408 wantscalar ? SVt_PVLV
6409 : opnum == OP_LOCK || opnum == OP_UNDEF
6415 "Type of arg %d to &CORE::%s must be %s",
6416 whicharg, PL_op_name[opnum],
6418 ? "scalar reference"
6419 : opnum == OP_LOCK || opnum == OP_UNDEF
6420 ? "reference to one of [$@%&*]"
6421 : "reference to one of [$@%*]"
6424 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
6425 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
6426 /* Undo @_ localisation, so that sub exit does not undo
6427 part of our undeffing. */
6428 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
6430 cx->cx_type &= ~ CXp_HASARGS;
6431 assert(!AvREAL(cx->blk_sub.argarray));
6436 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
6448 if (PL_op->op_private & OPpOFFBYONE) {
6449 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
6451 else cv = find_runcv(NULL);
6452 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
6457 S_localise_aelem_lval(pTHX_ AV * const av, SV * const keysv,
6458 const bool can_preserve)
6460 const SSize_t ix = SvIV(keysv);
6461 if (can_preserve ? av_exists(av, ix) : TRUE) {
6462 SV ** const svp = av_fetch(av, ix, 1);
6464 Perl_croak(aTHX_ PL_no_aelem, ix);
6465 save_aelem(av, ix, svp);
6468 SAVEADELETE(av, ix);
6472 S_localise_helem_lval(pTHX_ HV * const hv, SV * const keysv,
6473 const bool can_preserve)
6475 if (can_preserve ? hv_exists_ent(hv, keysv, 0) : TRUE) {
6476 HE * const he = hv_fetch_ent(hv, keysv, 1, 0);
6477 SV ** const svp = he ? &HeVAL(he) : NULL;
6479 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(keysv));
6480 save_helem_flags(hv, keysv, svp, 0);
6483 SAVEHDELETE(hv, keysv);
6487 S_localise_gv_slot(pTHX_ GV *gv, U8 type)
6489 if (type == OPpLVREF_SV) {
6490 save_pushptrptr(gv, SvREFCNT_inc_simple(GvSV(gv)), SAVEt_GVSV);
6493 else if (type == OPpLVREF_AV)
6494 /* XXX Inefficient, as it creates a new AV, which we are
6495 about to clobber. */
6498 assert(type == OPpLVREF_HV);
6499 /* XXX Likewise inefficient. */
6508 SV * const key = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
6509 SV * const left = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
6511 const char *bad = NULL;
6512 const U8 type = PL_op->op_private & OPpLVREF_TYPE;
6513 if (!SvROK(sv)) DIE(aTHX_ "Assigned value is not a reference");
6516 if (SvTYPE(SvRV(sv)) > SVt_PVLV)
6520 if (SvTYPE(SvRV(sv)) != SVt_PVAV)
6524 if (SvTYPE(SvRV(sv)) != SVt_PVHV)
6528 if (SvTYPE(SvRV(sv)) != SVt_PVCV)
6532 /* diag_listed_as: Assigned value is not %s reference */
6533 DIE(aTHX_ "Assigned value is not a%s reference", bad);
6537 switch (left ? SvTYPE(left) : 0) {
6540 SV * const old = PAD_SV(ARGTARG);
6541 PAD_SETSV(ARGTARG, SvREFCNT_inc_NN(SvRV(sv)));
6543 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
6545 SAVECLEARSV(PAD_SVl(ARGTARG));
6549 if (PL_op->op_private & OPpLVAL_INTRO) {
6550 S_localise_gv_slot(aTHX_ (GV *)left, type);
6552 gv_setref(left, sv);
6557 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6558 S_localise_aelem_lval(aTHX_ (AV *)left, key,
6559 SvCANEXISTDELETE(left));
6561 av_store((AV *)left, SvIV(key), SvREFCNT_inc_simple_NN(SvRV(sv)));
6564 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6566 S_localise_helem_lval(aTHX_ (HV *)left, key,
6567 SvCANEXISTDELETE(left));
6569 (void)hv_store_ent((HV *)left, key, SvREFCNT_inc_simple_NN(SvRV(sv)), 0);
6571 if (PL_op->op_flags & OPf_MOD)
6572 SETs(sv_2mortal(newSVsv(sv)));
6573 /* XXX else can weak references go stale before they are read, e.g.,
6582 SV * const ret = sv_2mortal(newSV_type(SVt_PVMG));
6583 SV * const elem = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
6584 SV * const arg = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
6585 MAGIC * const mg = sv_magicext(ret, arg, PERL_MAGIC_lvref,
6586 &PL_vtbl_lvref, (char *)elem,
6587 elem ? HEf_SVKEY : (I32)ARGTARG);
6588 mg->mg_private = PL_op->op_private;
6589 if (PL_op->op_private & OPpLVREF_ITER)
6590 mg->mg_flags |= MGf_PERSIST;
6591 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6597 const bool can_preserve = SvCANEXISTDELETE(arg);
6598 if (SvTYPE(arg) == SVt_PVAV)
6599 S_localise_aelem_lval(aTHX_ (AV *)arg, elem, can_preserve);
6601 S_localise_helem_lval(aTHX_ (HV *)arg, elem, can_preserve);
6605 S_localise_gv_slot(aTHX_ (GV *)arg,
6606 PL_op->op_private & OPpLVREF_TYPE);
6608 else if (!(PL_op->op_private & OPpPAD_STATE))
6609 SAVECLEARSV(PAD_SVl(ARGTARG));
6618 AV * const av = (AV *)POPs;
6619 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
6620 bool can_preserve = FALSE;
6622 if (UNLIKELY(localizing)) {
6627 can_preserve = SvCANEXISTDELETE(av);
6629 if (SvTYPE(av) == SVt_PVAV) {
6632 for (svp = MARK + 1; svp <= SP; svp++) {
6633 const SSize_t elem = SvIV(*svp);
6637 if (max > AvMAX(av))
6642 while (++MARK <= SP) {
6643 SV * const elemsv = *MARK;
6644 if (SvTYPE(av) == SVt_PVAV)
6645 S_localise_aelem_lval(aTHX_ av, elemsv, can_preserve);
6647 S_localise_helem_lval(aTHX_ (HV *)av, elemsv, can_preserve);
6648 *MARK = sv_2mortal(newSV_type(SVt_PVMG));
6649 sv_magic(*MARK,(SV *)av,PERL_MAGIC_lvref,(char *)elemsv,HEf_SVKEY);
6656 if (PL_op->op_flags & OPf_STACKED)
6657 Perl_pp_rv2av(aTHX);
6659 Perl_pp_padav(aTHX);
6663 SETs(0); /* special alias marker that aassign recognises */
6673 SETs(sv_2mortal((SV *)newCONSTSUB(SvTYPE(CopSTASH(PL_curcop))==SVt_PVHV
6674 ? CopSTASH(PL_curcop)
6676 NULL, SvREFCNT_inc_simple_NN(sv))));
6681 * ex: set ts=8 sts=4 sw=4 et: