3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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 to discover,
13 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
16 /* This file contains general pp ("push/pop") functions that execute the
17 * opcodes that make up a perl program. A typical pp function expects to
18 * find its arguments on the stack, and usually pushes its results onto
19 * the stack, hence the 'pp' terminology. Each OP structure contains
20 * a pointer to the relevant pp_foo() function.
30 /* XXX I can't imagine anyone who doesn't have this actually _needs_
31 it, since pid_t is an integral type.
34 #ifdef NEED_GETPID_PROTO
35 extern Pid_t getpid (void);
39 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
40 * This switches them over to IEEE.
42 #if defined(LIBM_LIB_VERSION)
43 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
46 /* variations on pp_null */
51 if (GIMME_V == G_SCALAR)
62 if (PL_op->op_private & OPpLVAL_INTRO)
63 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
65 if (PL_op->op_flags & OPf_REF) {
69 if (GIMME == G_SCALAR)
70 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
75 if (gimme == G_ARRAY) {
76 const I32 maxarg = AvFILL((AV*)TARG) + 1;
78 if (SvMAGICAL(TARG)) {
80 for (i=0; i < (U32)maxarg; i++) {
81 SV * const * const svp = av_fetch((AV*)TARG, i, FALSE);
82 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
86 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
90 else if (gimme == G_SCALAR) {
91 SV* const sv = sv_newmortal();
92 const I32 maxarg = AvFILL((AV*)TARG) + 1;
105 if (PL_op->op_private & OPpLVAL_INTRO)
106 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
107 if (PL_op->op_flags & OPf_REF)
110 if (GIMME == G_SCALAR)
111 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
115 if (gimme == G_ARRAY) {
118 else if (gimme == G_SCALAR) {
119 SV* const sv = Perl_hv_scalar(aTHX_ (HV*)TARG);
133 tryAMAGICunDEREF(to_gv);
136 if (SvTYPE(sv) == SVt_PVIO) {
137 GV * const gv = (GV*) sv_newmortal();
138 gv_init(gv, 0, "", 0, 0);
139 GvIOp(gv) = (IO *)sv;
140 SvREFCNT_inc_void_NN(sv);
143 else if (SvTYPE(sv) != SVt_PVGV)
144 DIE(aTHX_ "Not a GLOB reference");
147 if (SvTYPE(sv) != SVt_PVGV) {
148 if (SvGMAGICAL(sv)) {
153 if (!SvOK(sv) && sv != &PL_sv_undef) {
154 /* If this is a 'my' scalar and flag is set then vivify
158 Perl_croak(aTHX_ PL_no_modify);
159 if (PL_op->op_private & OPpDEREF) {
161 if (cUNOP->op_targ) {
163 SV * const namesv = PAD_SV(cUNOP->op_targ);
164 const char * const name = SvPV(namesv, len);
165 gv = (GV*)NEWSV(0,0);
166 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
169 const char * const name = CopSTASHPV(PL_curcop);
170 gv = newGVgen((char *)name);
172 if (SvTYPE(sv) < SVt_RV)
173 sv_upgrade(sv, SVt_RV);
174 if (SvPVX_const(sv)) {
179 SvRV_set(sv, (SV*)gv);
184 if (PL_op->op_flags & OPf_REF ||
185 PL_op->op_private & HINT_STRICT_REFS)
186 DIE(aTHX_ PL_no_usym, "a symbol");
187 if (ckWARN(WARN_UNINITIALIZED))
191 if ((PL_op->op_flags & OPf_SPECIAL) &&
192 !(PL_op->op_flags & OPf_MOD))
194 SV * temp = (SV*)gv_fetchsv(sv, 0, SVt_PVGV);
196 && (!is_gv_magical_sv(sv,0)
197 || !(sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV)))) {
203 if (PL_op->op_private & HINT_STRICT_REFS)
204 DIE(aTHX_ PL_no_symref_sv, sv, "a symbol");
205 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
206 == OPpDONT_INIT_GV) {
207 /* We are the target of a coderef assignment. Return
208 the scalar unchanged, and let pp_sasssign deal with
212 sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV);
216 if (PL_op->op_private & OPpLVAL_INTRO)
217 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
229 tryAMAGICunDEREF(to_sv);
232 switch (SvTYPE(sv)) {
236 DIE(aTHX_ "Not a SCALAR reference");
242 if (SvTYPE(gv) != SVt_PVGV) {
243 if (SvGMAGICAL(sv)) {
249 if (PL_op->op_flags & OPf_REF ||
250 PL_op->op_private & HINT_STRICT_REFS)
251 DIE(aTHX_ PL_no_usym, "a SCALAR");
252 if (ckWARN(WARN_UNINITIALIZED))
256 if ((PL_op->op_flags & OPf_SPECIAL) &&
257 !(PL_op->op_flags & OPf_MOD))
259 gv = (GV*)gv_fetchsv(sv, 0, SVt_PV);
261 && (!is_gv_magical_sv(sv, 0)
262 || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV))))
268 if (PL_op->op_private & HINT_STRICT_REFS)
269 DIE(aTHX_ PL_no_symref_sv, sv, "a SCALAR");
270 gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV);
275 if (PL_op->op_flags & OPf_MOD) {
276 if (PL_op->op_private & OPpLVAL_INTRO) {
277 if (cUNOP->op_first->op_type == OP_NULL)
278 sv = save_scalar((GV*)TOPs);
280 sv = save_scalar(gv);
282 Perl_croak(aTHX_ PL_no_localize_ref);
284 else if (PL_op->op_private & OPpDEREF)
285 vivify_ref(sv, PL_op->op_private & OPpDEREF);
294 AV *const av = (AV*)TOPs;
295 SV *sv = AvARYLEN(av);
297 AvARYLEN(av) = sv = NEWSV(0,0);
298 sv_upgrade(sv, SVt_IV);
299 sv_magic(sv, (SV*)av, PERL_MAGIC_arylen, NULL, 0);
307 dSP; dTARGET; dPOPss;
309 if (PL_op->op_flags & OPf_MOD || LVRET) {
310 if (SvTYPE(TARG) < SVt_PVLV) {
311 sv_upgrade(TARG, SVt_PVLV);
312 sv_magic(TARG, NULL, PERL_MAGIC_pos, NULL, 0);
316 if (LvTARG(TARG) != sv) {
318 SvREFCNT_dec(LvTARG(TARG));
319 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
321 PUSHs(TARG); /* no SvSETMAGIC */
325 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
326 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
327 if (mg && mg->mg_len >= 0) {
331 PUSHi(i + PL_curcop->cop_arybase);
344 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
346 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
349 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
350 /* (But not in defined().) */
352 CV *cv = sv_2cv(TOPs, &stash, &gv, flags);
355 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
356 if ((PL_op->op_private & OPpLVAL_INTRO)) {
357 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
360 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
363 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
367 cv = (CV*)&PL_sv_undef;
378 SV *ret = &PL_sv_undef;
380 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
381 const char * const s = SvPVX_const(TOPs);
382 if (strnEQ(s, "CORE::", 6)) {
383 const int code = keyword((char *)s + 6, SvCUR(TOPs) - 6);
384 if (code < 0) { /* Overridable. */
385 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
386 int i = 0, n = 0, seen_question = 0;
388 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
390 if (code == -KEY_chop || code == -KEY_chomp
391 || code == -KEY_exec || code == -KEY_system)
393 while (i < MAXO) { /* The slow way. */
394 if (strEQ(s + 6, PL_op_name[i])
395 || strEQ(s + 6, PL_op_desc[i]))
401 goto nonesuch; /* Should not happen... */
403 oa = PL_opargs[i] >> OASHIFT;
405 if (oa & OA_OPTIONAL && !seen_question) {
409 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
410 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
411 /* But globs are already references (kinda) */
412 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
416 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
420 ret = sv_2mortal(newSVpvn(str, n - 1));
422 else if (code) /* Non-Overridable */
424 else { /* None such */
426 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
430 cv = sv_2cv(TOPs, &stash, &gv, 0);
432 ret = sv_2mortal(newSVpvn(SvPVX_const(cv), SvCUR(cv)));
441 CV* cv = (CV*)PAD_SV(PL_op->op_targ);
443 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
459 if (GIMME != G_ARRAY) {
463 *MARK = &PL_sv_undef;
464 *MARK = refto(*MARK);
468 EXTEND_MORTAL(SP - MARK);
470 *MARK = refto(*MARK);
475 S_refto(pTHX_ SV *sv)
479 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
482 if (!(sv = LvTARG(sv)))
485 SvREFCNT_inc_void_NN(sv);
487 else if (SvTYPE(sv) == SVt_PVAV) {
488 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
491 SvREFCNT_inc_void_NN(sv);
493 else if (SvPADTMP(sv) && !IS_PADGV(sv))
497 SvREFCNT_inc_void_NN(sv);
500 sv_upgrade(rv, SVt_RV);
510 SV * const sv = POPs;
515 if (!sv || !SvROK(sv))
518 pv = sv_reftype(SvRV(sv),TRUE);
519 PUSHp(pv, strlen(pv));
529 stash = CopSTASH(PL_curcop);
531 SV * const ssv = POPs;
535 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
536 Perl_croak(aTHX_ "Attempt to bless into a reference");
537 ptr = SvPV_const(ssv,len);
538 if (len == 0 && ckWARN(WARN_MISC))
539 Perl_warner(aTHX_ packWARN(WARN_MISC),
540 "Explicit blessing to '' (assuming package main)");
541 stash = gv_stashpvn(ptr, len, TRUE);
544 (void)sv_bless(TOPs, stash);
553 const char * const elem = SvPV_nolen_const(sv);
554 GV * const gv = (GV*)POPs;
559 /* elem will always be NUL terminated. */
560 const char * const second_letter = elem + 1;
563 if (strEQ(second_letter, "RRAY"))
564 tmpRef = (SV*)GvAV(gv);
567 if (strEQ(second_letter, "ODE"))
568 tmpRef = (SV*)GvCVu(gv);
571 if (strEQ(second_letter, "ILEHANDLE")) {
572 /* finally deprecated in 5.8.0 */
573 deprecate("*glob{FILEHANDLE}");
574 tmpRef = (SV*)GvIOp(gv);
577 if (strEQ(second_letter, "ORMAT"))
578 tmpRef = (SV*)GvFORM(gv);
581 if (strEQ(second_letter, "LOB"))
585 if (strEQ(second_letter, "ASH"))
586 tmpRef = (SV*)GvHV(gv);
589 if (*second_letter == 'O' && !elem[2])
590 tmpRef = (SV*)GvIOp(gv);
593 if (strEQ(second_letter, "AME"))
594 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
597 if (strEQ(second_letter, "ACKAGE")) {
598 const char *const name = HvNAME_get(GvSTASH(gv));
599 sv = newSVpv(name ? name : "__ANON__", 0);
603 if (strEQ(second_letter, "CALAR"))
618 /* Pattern matching */
623 register unsigned char *s;
626 register I32 *sfirst;
630 if (sv == PL_lastscream) {
636 SvSCREAM_off(PL_lastscream);
637 SvREFCNT_dec(PL_lastscream);
639 PL_lastscream = SvREFCNT_inc(sv);
642 s = (unsigned char*)(SvPV(sv, len));
646 if (pos > PL_maxscream) {
647 if (PL_maxscream < 0) {
648 PL_maxscream = pos + 80;
649 Newx(PL_screamfirst, 256, I32);
650 Newx(PL_screamnext, PL_maxscream, I32);
653 PL_maxscream = pos + pos / 4;
654 Renew(PL_screamnext, PL_maxscream, I32);
658 sfirst = PL_screamfirst;
659 snext = PL_screamnext;
661 if (!sfirst || !snext)
662 DIE(aTHX_ "do_study: out of memory");
664 for (ch = 256; ch; --ch)
669 register const I32 ch = s[pos];
671 snext[pos] = sfirst[ch] - pos;
678 /* piggyback on m//g magic */
679 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
688 if (PL_op->op_flags & OPf_STACKED)
694 TARG = sv_newmortal();
699 /* Lvalue operators. */
711 dSP; dMARK; dTARGET; dORIGMARK;
713 do_chop(TARG, *++MARK);
722 SETi(do_chomp(TOPs));
729 register I32 count = 0;
732 count += do_chomp(POPs);
740 register SV* const sv = POPs;
742 if (!sv || !SvANY(sv))
744 switch (SvTYPE(sv)) {
746 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
747 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
751 if (HvARRAY(sv) || SvGMAGICAL(sv)
752 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
756 if (CvROOT(sv) || CvXSUB(sv))
772 if (!PL_op->op_private) {
781 if (SvTHINKFIRST(sv))
784 switch (SvTYPE(sv)) {
794 if (cv_const_sv((CV*)sv) && ckWARN(WARN_MISC))
795 Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
796 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
800 /* let user-undef'd sub keep its identity */
801 GV* const gv = CvGV((CV*)sv);
808 SvSetMagicSV(sv, &PL_sv_undef);
813 GvGP(sv) = gp_ref(gp);
814 GvSV(sv) = NEWSV(72,0);
815 GvLINE(sv) = CopLINE(PL_curcop);
821 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
836 if (SvTYPE(TOPs) > SVt_PVLV)
837 DIE(aTHX_ PL_no_modify);
838 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
839 && SvIVX(TOPs) != IV_MIN)
841 SvIV_set(TOPs, SvIVX(TOPs) - 1);
842 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
853 if (SvTYPE(TOPs) > SVt_PVLV)
854 DIE(aTHX_ PL_no_modify);
855 sv_setsv(TARG, TOPs);
856 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
857 && SvIVX(TOPs) != IV_MAX)
859 SvIV_set(TOPs, SvIVX(TOPs) + 1);
860 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
865 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
875 if (SvTYPE(TOPs) > SVt_PVLV)
876 DIE(aTHX_ PL_no_modify);
877 sv_setsv(TARG, TOPs);
878 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
879 && SvIVX(TOPs) != IV_MIN)
881 SvIV_set(TOPs, SvIVX(TOPs) - 1);
882 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
891 /* Ordinary operators. */
896 #ifdef PERL_PRESERVE_IVUV
899 tryAMAGICbin(pow,opASSIGN);
900 #ifdef PERL_PRESERVE_IVUV
901 /* For integer to integer power, we do the calculation by hand wherever
902 we're sure it is safe; otherwise we call pow() and try to convert to
903 integer afterwards. */
916 const IV iv = SvIVX(TOPs);
920 goto float_it; /* Can't do negative powers this way. */
924 baseuok = SvUOK(TOPm1s);
926 baseuv = SvUVX(TOPm1s);
928 const IV iv = SvIVX(TOPm1s);
931 baseuok = TRUE; /* effectively it's a UV now */
933 baseuv = -iv; /* abs, baseuok == false records sign */
936 /* now we have integer ** positive integer. */
939 /* foo & (foo - 1) is zero only for a power of 2. */
940 if (!(baseuv & (baseuv - 1))) {
941 /* We are raising power-of-2 to a positive integer.
942 The logic here will work for any base (even non-integer
943 bases) but it can be less accurate than
944 pow (base,power) or exp (power * log (base)) when the
945 intermediate values start to spill out of the mantissa.
946 With powers of 2 we know this can't happen.
947 And powers of 2 are the favourite thing for perl
948 programmers to notice ** not doing what they mean. */
950 NV base = baseuok ? baseuv : -(NV)baseuv;
955 while (power >>= 1) {
966 register unsigned int highbit = 8 * sizeof(UV);
967 register unsigned int diff = 8 * sizeof(UV);
970 if (baseuv >> highbit) {
974 /* we now have baseuv < 2 ** highbit */
975 if (power * highbit <= 8 * sizeof(UV)) {
976 /* result will definitely fit in UV, so use UV math
977 on same algorithm as above */
978 register UV result = 1;
979 register UV base = baseuv;
980 const bool odd_power = (bool)(power & 1);
984 while (power >>= 1) {
991 if (baseuok || !odd_power)
992 /* answer is positive */
994 else if (result <= (UV)IV_MAX)
995 /* answer negative, fits in IV */
997 else if (result == (UV)IV_MIN)
998 /* 2's complement assumption: special case IV_MIN */
1001 /* answer negative, doesn't fit */
1002 SETn( -(NV)result );
1013 SETn( Perl_pow( left, right) );
1014 #ifdef PERL_PRESERVE_IVUV
1024 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1025 #ifdef PERL_PRESERVE_IVUV
1028 /* Unless the left argument is integer in range we are going to have to
1029 use NV maths. Hence only attempt to coerce the right argument if
1030 we know the left is integer. */
1031 /* Left operand is defined, so is it IV? */
1032 SvIV_please(TOPm1s);
1033 if (SvIOK(TOPm1s)) {
1034 bool auvok = SvUOK(TOPm1s);
1035 bool buvok = SvUOK(TOPs);
1036 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1037 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1044 alow = SvUVX(TOPm1s);
1046 const IV aiv = SvIVX(TOPm1s);
1049 auvok = TRUE; /* effectively it's a UV now */
1051 alow = -aiv; /* abs, auvok == false records sign */
1057 const IV biv = SvIVX(TOPs);
1060 buvok = TRUE; /* effectively it's a UV now */
1062 blow = -biv; /* abs, buvok == false records sign */
1066 /* If this does sign extension on unsigned it's time for plan B */
1067 ahigh = alow >> (4 * sizeof (UV));
1069 bhigh = blow >> (4 * sizeof (UV));
1071 if (ahigh && bhigh) {
1072 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1073 which is overflow. Drop to NVs below. */
1074 } else if (!ahigh && !bhigh) {
1075 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1076 so the unsigned multiply cannot overflow. */
1077 const UV product = alow * blow;
1078 if (auvok == buvok) {
1079 /* -ve * -ve or +ve * +ve gives a +ve result. */
1083 } else if (product <= (UV)IV_MIN) {
1084 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1085 /* -ve result, which could overflow an IV */
1087 SETi( -(IV)product );
1089 } /* else drop to NVs below. */
1091 /* One operand is large, 1 small */
1094 /* swap the operands */
1096 bhigh = blow; /* bhigh now the temp var for the swap */
1100 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1101 multiplies can't overflow. shift can, add can, -ve can. */
1102 product_middle = ahigh * blow;
1103 if (!(product_middle & topmask)) {
1104 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1106 product_middle <<= (4 * sizeof (UV));
1107 product_low = alow * blow;
1109 /* as for pp_add, UV + something mustn't get smaller.
1110 IIRC ANSI mandates this wrapping *behaviour* for
1111 unsigned whatever the actual representation*/
1112 product_low += product_middle;
1113 if (product_low >= product_middle) {
1114 /* didn't overflow */
1115 if (auvok == buvok) {
1116 /* -ve * -ve or +ve * +ve gives a +ve result. */
1118 SETu( product_low );
1120 } else if (product_low <= (UV)IV_MIN) {
1121 /* 2s complement assumption again */
1122 /* -ve result, which could overflow an IV */
1124 SETi( -(IV)product_low );
1126 } /* else drop to NVs below. */
1128 } /* product_middle too large */
1129 } /* ahigh && bhigh */
1130 } /* SvIOK(TOPm1s) */
1135 SETn( left * right );
1142 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1143 /* Only try to do UV divide first
1144 if ((SLOPPYDIVIDE is true) or
1145 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1147 The assumption is that it is better to use floating point divide
1148 whenever possible, only doing integer divide first if we can't be sure.
1149 If NV_PRESERVES_UV is true then we know at compile time that no UV
1150 can be too large to preserve, so don't need to compile the code to
1151 test the size of UVs. */
1154 # define PERL_TRY_UV_DIVIDE
1155 /* ensure that 20./5. == 4. */
1157 # ifdef PERL_PRESERVE_IVUV
1158 # ifndef NV_PRESERVES_UV
1159 # define PERL_TRY_UV_DIVIDE
1164 #ifdef PERL_TRY_UV_DIVIDE
1167 SvIV_please(TOPm1s);
1168 if (SvIOK(TOPm1s)) {
1169 bool left_non_neg = SvUOK(TOPm1s);
1170 bool right_non_neg = SvUOK(TOPs);
1174 if (right_non_neg) {
1175 right = SvUVX(TOPs);
1178 const IV biv = SvIVX(TOPs);
1181 right_non_neg = TRUE; /* effectively it's a UV now */
1187 /* historically undef()/0 gives a "Use of uninitialized value"
1188 warning before dieing, hence this test goes here.
1189 If it were immediately before the second SvIV_please, then
1190 DIE() would be invoked before left was even inspected, so
1191 no inpsection would give no warning. */
1193 DIE(aTHX_ "Illegal division by zero");
1196 left = SvUVX(TOPm1s);
1199 const IV aiv = SvIVX(TOPm1s);
1202 left_non_neg = TRUE; /* effectively it's a UV now */
1211 /* For sloppy divide we always attempt integer division. */
1213 /* Otherwise we only attempt it if either or both operands
1214 would not be preserved by an NV. If both fit in NVs
1215 we fall through to the NV divide code below. However,
1216 as left >= right to ensure integer result here, we know that
1217 we can skip the test on the right operand - right big
1218 enough not to be preserved can't get here unless left is
1221 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1224 /* Integer division can't overflow, but it can be imprecise. */
1225 const UV result = left / right;
1226 if (result * right == left) {
1227 SP--; /* result is valid */
1228 if (left_non_neg == right_non_neg) {
1229 /* signs identical, result is positive. */
1233 /* 2s complement assumption */
1234 if (result <= (UV)IV_MIN)
1235 SETi( -(IV)result );
1237 /* It's exact but too negative for IV. */
1238 SETn( -(NV)result );
1241 } /* tried integer divide but it was not an integer result */
1242 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1243 } /* left wasn't SvIOK */
1244 } /* right wasn't SvIOK */
1245 #endif /* PERL_TRY_UV_DIVIDE */
1249 DIE(aTHX_ "Illegal division by zero");
1250 PUSHn( left / right );
1257 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1261 bool left_neg = FALSE;
1262 bool right_neg = FALSE;
1263 bool use_double = FALSE;
1264 bool dright_valid = FALSE;
1270 right_neg = !SvUOK(TOPs);
1272 right = SvUVX(POPs);
1274 const IV biv = SvIVX(POPs);
1277 right_neg = FALSE; /* effectively it's a UV now */
1285 right_neg = dright < 0;
1288 if (dright < UV_MAX_P1) {
1289 right = U_V(dright);
1290 dright_valid = TRUE; /* In case we need to use double below. */
1296 /* At this point use_double is only true if right is out of range for
1297 a UV. In range NV has been rounded down to nearest UV and
1298 use_double false. */
1300 if (!use_double && SvIOK(TOPs)) {
1302 left_neg = !SvUOK(TOPs);
1306 const IV aiv = SvIVX(POPs);
1309 left_neg = FALSE; /* effectively it's a UV now */
1318 left_neg = dleft < 0;
1322 /* This should be exactly the 5.6 behaviour - if left and right are
1323 both in range for UV then use U_V() rather than floor. */
1325 if (dleft < UV_MAX_P1) {
1326 /* right was in range, so is dleft, so use UVs not double.
1330 /* left is out of range for UV, right was in range, so promote
1331 right (back) to double. */
1333 /* The +0.5 is used in 5.6 even though it is not strictly
1334 consistent with the implicit +0 floor in the U_V()
1335 inside the #if 1. */
1336 dleft = Perl_floor(dleft + 0.5);
1339 dright = Perl_floor(dright + 0.5);
1349 DIE(aTHX_ "Illegal modulus zero");
1351 dans = Perl_fmod(dleft, dright);
1352 if ((left_neg != right_neg) && dans)
1353 dans = dright - dans;
1356 sv_setnv(TARG, dans);
1362 DIE(aTHX_ "Illegal modulus zero");
1365 if ((left_neg != right_neg) && ans)
1368 /* XXX may warn: unary minus operator applied to unsigned type */
1369 /* could change -foo to be (~foo)+1 instead */
1370 if (ans <= ~((UV)IV_MAX)+1)
1371 sv_setiv(TARG, ~ans+1);
1373 sv_setnv(TARG, -(NV)ans);
1376 sv_setuv(TARG, ans);
1385 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1392 const UV uv = SvUV(sv);
1394 count = IV_MAX; /* The best we can do? */
1398 const IV iv = SvIV(sv);
1405 else if (SvNOKp(sv)) {
1406 const NV nv = SvNV(sv);
1414 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1416 static const char oom_list_extend[] = "Out of memory during list extend";
1417 const I32 items = SP - MARK;
1418 const I32 max = items * count;
1420 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1421 /* Did the max computation overflow? */
1422 if (items > 0 && max > 0 && (max < items || max < count))
1423 Perl_croak(aTHX_ oom_list_extend);
1428 /* This code was intended to fix 20010809.028:
1431 for (($x =~ /./g) x 2) {
1432 print chop; # "abcdabcd" expected as output.
1435 * but that change (#11635) broke this code:
1437 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1439 * I can't think of a better fix that doesn't introduce
1440 * an efficiency hit by copying the SVs. The stack isn't
1441 * refcounted, and mortalisation obviously doesn't
1442 * Do The Right Thing when the stack has more than
1443 * one pointer to the same mortal value.
1447 *SP = sv_2mortal(newSVsv(*SP));
1457 repeatcpy((char*)(MARK + items), (char*)MARK,
1458 items * sizeof(SV*), count - 1);
1461 else if (count <= 0)
1464 else { /* Note: mark already snarfed by pp_list */
1465 SV * const tmpstr = POPs;
1468 static const char oom_string_extend[] =
1469 "Out of memory during string extend";
1471 SvSetSV(TARG, tmpstr);
1472 SvPV_force(TARG, len);
1473 isutf = DO_UTF8(TARG);
1478 const STRLEN max = (UV)count * len;
1479 if (len > ((MEM_SIZE)~0)/count)
1480 Perl_croak(aTHX_ oom_string_extend);
1481 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1482 SvGROW(TARG, max + 1);
1483 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1484 SvCUR_set(TARG, SvCUR(TARG) * count);
1486 *SvEND(TARG) = '\0';
1489 (void)SvPOK_only_UTF8(TARG);
1491 (void)SvPOK_only(TARG);
1493 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1494 /* The parser saw this as a list repeat, and there
1495 are probably several items on the stack. But we're
1496 in scalar context, and there's no pp_list to save us
1497 now. So drop the rest of the items -- robin@kitsite.com
1510 dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN);
1511 useleft = USE_LEFT(TOPm1s);
1512 #ifdef PERL_PRESERVE_IVUV
1513 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1514 "bad things" happen if you rely on signed integers wrapping. */
1517 /* Unless the left argument is integer in range we are going to have to
1518 use NV maths. Hence only attempt to coerce the right argument if
1519 we know the left is integer. */
1520 register UV auv = 0;
1526 a_valid = auvok = 1;
1527 /* left operand is undef, treat as zero. */
1529 /* Left operand is defined, so is it IV? */
1530 SvIV_please(TOPm1s);
1531 if (SvIOK(TOPm1s)) {
1532 if ((auvok = SvUOK(TOPm1s)))
1533 auv = SvUVX(TOPm1s);
1535 register const IV aiv = SvIVX(TOPm1s);
1538 auvok = 1; /* Now acting as a sign flag. */
1539 } else { /* 2s complement assumption for IV_MIN */
1547 bool result_good = 0;
1550 bool buvok = SvUOK(TOPs);
1555 register const IV biv = SvIVX(TOPs);
1562 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1563 else "IV" now, independent of how it came in.
1564 if a, b represents positive, A, B negative, a maps to -A etc
1569 all UV maths. negate result if A negative.
1570 subtract if signs same, add if signs differ. */
1572 if (auvok ^ buvok) {
1581 /* Must get smaller */
1586 if (result <= buv) {
1587 /* result really should be -(auv-buv). as its negation
1588 of true value, need to swap our result flag */
1600 if (result <= (UV)IV_MIN)
1601 SETi( -(IV)result );
1603 /* result valid, but out of range for IV. */
1604 SETn( -(NV)result );
1608 } /* Overflow, drop through to NVs. */
1612 useleft = USE_LEFT(TOPm1s);
1616 /* left operand is undef, treat as zero - value */
1620 SETn( TOPn - value );
1627 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1629 const IV shift = POPi;
1630 if (PL_op->op_private & HINT_INTEGER) {
1644 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1646 const IV shift = POPi;
1647 if (PL_op->op_private & HINT_INTEGER) {
1661 dSP; tryAMAGICbinSET(lt,0);
1662 #ifdef PERL_PRESERVE_IVUV
1665 SvIV_please(TOPm1s);
1666 if (SvIOK(TOPm1s)) {
1667 bool auvok = SvUOK(TOPm1s);
1668 bool buvok = SvUOK(TOPs);
1670 if (!auvok && !buvok) { /* ## IV < IV ## */
1671 const IV aiv = SvIVX(TOPm1s);
1672 const IV biv = SvIVX(TOPs);
1675 SETs(boolSV(aiv < biv));
1678 if (auvok && buvok) { /* ## UV < UV ## */
1679 const UV auv = SvUVX(TOPm1s);
1680 const UV buv = SvUVX(TOPs);
1683 SETs(boolSV(auv < buv));
1686 if (auvok) { /* ## UV < IV ## */
1688 const IV biv = SvIVX(TOPs);
1691 /* As (a) is a UV, it's >=0, so it cannot be < */
1696 SETs(boolSV(auv < (UV)biv));
1699 { /* ## IV < UV ## */
1700 const IV aiv = SvIVX(TOPm1s);
1704 /* As (b) is a UV, it's >=0, so it must be < */
1711 SETs(boolSV((UV)aiv < buv));
1717 #ifndef NV_PRESERVES_UV
1718 #ifdef PERL_PRESERVE_IVUV
1721 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1723 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1729 SETs(boolSV(TOPn < value));
1736 dSP; tryAMAGICbinSET(gt,0);
1737 #ifdef PERL_PRESERVE_IVUV
1740 SvIV_please(TOPm1s);
1741 if (SvIOK(TOPm1s)) {
1742 bool auvok = SvUOK(TOPm1s);
1743 bool buvok = SvUOK(TOPs);
1745 if (!auvok && !buvok) { /* ## IV > IV ## */
1746 const IV aiv = SvIVX(TOPm1s);
1747 const IV biv = SvIVX(TOPs);
1750 SETs(boolSV(aiv > biv));
1753 if (auvok && buvok) { /* ## UV > UV ## */
1754 const UV auv = SvUVX(TOPm1s);
1755 const UV buv = SvUVX(TOPs);
1758 SETs(boolSV(auv > buv));
1761 if (auvok) { /* ## UV > IV ## */
1763 const IV biv = SvIVX(TOPs);
1767 /* As (a) is a UV, it's >=0, so it must be > */
1772 SETs(boolSV(auv > (UV)biv));
1775 { /* ## IV > UV ## */
1776 const IV aiv = SvIVX(TOPm1s);
1780 /* As (b) is a UV, it's >=0, so it cannot be > */
1787 SETs(boolSV((UV)aiv > buv));
1793 #ifndef NV_PRESERVES_UV
1794 #ifdef PERL_PRESERVE_IVUV
1797 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1799 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1805 SETs(boolSV(TOPn > value));
1812 dSP; tryAMAGICbinSET(le,0);
1813 #ifdef PERL_PRESERVE_IVUV
1816 SvIV_please(TOPm1s);
1817 if (SvIOK(TOPm1s)) {
1818 bool auvok = SvUOK(TOPm1s);
1819 bool buvok = SvUOK(TOPs);
1821 if (!auvok && !buvok) { /* ## IV <= IV ## */
1822 const IV aiv = SvIVX(TOPm1s);
1823 const IV biv = SvIVX(TOPs);
1826 SETs(boolSV(aiv <= biv));
1829 if (auvok && buvok) { /* ## UV <= UV ## */
1830 UV auv = SvUVX(TOPm1s);
1831 UV buv = SvUVX(TOPs);
1834 SETs(boolSV(auv <= buv));
1837 if (auvok) { /* ## UV <= IV ## */
1839 const IV biv = SvIVX(TOPs);
1843 /* As (a) is a UV, it's >=0, so a cannot be <= */
1848 SETs(boolSV(auv <= (UV)biv));
1851 { /* ## IV <= UV ## */
1852 const IV aiv = SvIVX(TOPm1s);
1856 /* As (b) is a UV, it's >=0, so a must be <= */
1863 SETs(boolSV((UV)aiv <= buv));
1869 #ifndef NV_PRESERVES_UV
1870 #ifdef PERL_PRESERVE_IVUV
1873 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1875 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1881 SETs(boolSV(TOPn <= value));
1888 dSP; tryAMAGICbinSET(ge,0);
1889 #ifdef PERL_PRESERVE_IVUV
1892 SvIV_please(TOPm1s);
1893 if (SvIOK(TOPm1s)) {
1894 bool auvok = SvUOK(TOPm1s);
1895 bool buvok = SvUOK(TOPs);
1897 if (!auvok && !buvok) { /* ## IV >= IV ## */
1898 const IV aiv = SvIVX(TOPm1s);
1899 const IV biv = SvIVX(TOPs);
1902 SETs(boolSV(aiv >= biv));
1905 if (auvok && buvok) { /* ## UV >= UV ## */
1906 const UV auv = SvUVX(TOPm1s);
1907 const UV buv = SvUVX(TOPs);
1910 SETs(boolSV(auv >= buv));
1913 if (auvok) { /* ## UV >= IV ## */
1915 const IV biv = SvIVX(TOPs);
1919 /* As (a) is a UV, it's >=0, so it must be >= */
1924 SETs(boolSV(auv >= (UV)biv));
1927 { /* ## IV >= UV ## */
1928 const IV aiv = SvIVX(TOPm1s);
1932 /* As (b) is a UV, it's >=0, so a cannot be >= */
1939 SETs(boolSV((UV)aiv >= buv));
1945 #ifndef NV_PRESERVES_UV
1946 #ifdef PERL_PRESERVE_IVUV
1949 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1951 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
1957 SETs(boolSV(TOPn >= value));
1964 dSP; tryAMAGICbinSET(ne,0);
1965 #ifndef NV_PRESERVES_UV
1966 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1968 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
1972 #ifdef PERL_PRESERVE_IVUV
1975 SvIV_please(TOPm1s);
1976 if (SvIOK(TOPm1s)) {
1977 const bool auvok = SvUOK(TOPm1s);
1978 const bool buvok = SvUOK(TOPs);
1980 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
1981 /* Casting IV to UV before comparison isn't going to matter
1982 on 2s complement. On 1s complement or sign&magnitude
1983 (if we have any of them) it could make negative zero
1984 differ from normal zero. As I understand it. (Need to
1985 check - is negative zero implementation defined behaviour
1987 const UV buv = SvUVX(POPs);
1988 const UV auv = SvUVX(TOPs);
1990 SETs(boolSV(auv != buv));
1993 { /* ## Mixed IV,UV ## */
1997 /* != is commutative so swap if needed (save code) */
1999 /* swap. top of stack (b) is the iv */
2003 /* As (a) is a UV, it's >0, so it cannot be == */
2012 /* As (b) is a UV, it's >0, so it cannot be == */
2016 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2018 SETs(boolSV((UV)iv != uv));
2026 SETs(boolSV(TOPn != value));
2033 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2034 #ifndef NV_PRESERVES_UV
2035 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2036 const UV right = PTR2UV(SvRV(POPs));
2037 const UV left = PTR2UV(SvRV(TOPs));
2038 SETi((left > right) - (left < right));
2042 #ifdef PERL_PRESERVE_IVUV
2043 /* Fortunately it seems NaN isn't IOK */
2046 SvIV_please(TOPm1s);
2047 if (SvIOK(TOPm1s)) {
2048 const bool leftuvok = SvUOK(TOPm1s);
2049 const bool rightuvok = SvUOK(TOPs);
2051 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2052 const IV leftiv = SvIVX(TOPm1s);
2053 const IV rightiv = SvIVX(TOPs);
2055 if (leftiv > rightiv)
2057 else if (leftiv < rightiv)
2061 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2062 const UV leftuv = SvUVX(TOPm1s);
2063 const UV rightuv = SvUVX(TOPs);
2065 if (leftuv > rightuv)
2067 else if (leftuv < rightuv)
2071 } else if (leftuvok) { /* ## UV <=> IV ## */
2072 const IV rightiv = SvIVX(TOPs);
2074 /* As (a) is a UV, it's >=0, so it cannot be < */
2077 const UV leftuv = SvUVX(TOPm1s);
2078 if (leftuv > (UV)rightiv) {
2080 } else if (leftuv < (UV)rightiv) {
2086 } else { /* ## IV <=> UV ## */
2087 const IV leftiv = SvIVX(TOPm1s);
2089 /* As (b) is a UV, it's >=0, so it must be < */
2092 const UV rightuv = SvUVX(TOPs);
2093 if ((UV)leftiv > rightuv) {
2095 } else if ((UV)leftiv < rightuv) {
2113 if (Perl_isnan(left) || Perl_isnan(right)) {
2117 value = (left > right) - (left < right);
2121 else if (left < right)
2123 else if (left > right)
2139 int amg_type = sle_amg;
2143 switch (PL_op->op_type) {
2162 tryAMAGICbinSET_var(amg_type,0);
2165 const int cmp = (IN_LOCALE_RUNTIME
2166 ? sv_cmp_locale(left, right)
2167 : sv_cmp(left, right));
2168 SETs(boolSV(cmp * multiplier < rhs));
2175 dSP; tryAMAGICbinSET(seq,0);
2178 SETs(boolSV(sv_eq(left, right)));
2185 dSP; tryAMAGICbinSET(sne,0);
2188 SETs(boolSV(!sv_eq(left, right)));
2195 dSP; dTARGET; tryAMAGICbin(scmp,0);
2198 const int cmp = (IN_LOCALE_RUNTIME
2199 ? sv_cmp_locale(left, right)
2200 : sv_cmp(left, right));
2208 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2213 if (SvNIOKp(left) || SvNIOKp(right)) {
2214 if (PL_op->op_private & HINT_INTEGER) {
2215 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2219 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2224 do_vop(PL_op->op_type, TARG, left, right);
2233 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
2238 if (SvNIOKp(left) || SvNIOKp(right)) {
2239 if (PL_op->op_private & HINT_INTEGER) {
2240 const IV i = (USE_LEFT(left) ? SvIV_nomg(left) : 0) ^ SvIV_nomg(right);
2244 const UV u = (USE_LEFT(left) ? SvUV_nomg(left) : 0) ^ SvUV_nomg(right);
2249 do_vop(PL_op->op_type, TARG, left, right);
2258 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
2263 if (SvNIOKp(left) || SvNIOKp(right)) {
2264 if (PL_op->op_private & HINT_INTEGER) {
2265 const IV i = (USE_LEFT(left) ? SvIV_nomg(left) : 0) | SvIV_nomg(right);
2269 const UV u = (USE_LEFT(left) ? SvUV_nomg(left) : 0) | SvUV_nomg(right);
2274 do_vop(PL_op->op_type, TARG, left, right);
2283 dSP; dTARGET; tryAMAGICun(neg);
2286 const int flags = SvFLAGS(sv);
2288 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2289 /* It's publicly an integer, or privately an integer-not-float */
2292 if (SvIVX(sv) == IV_MIN) {
2293 /* 2s complement assumption. */
2294 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2297 else if (SvUVX(sv) <= IV_MAX) {
2302 else if (SvIVX(sv) != IV_MIN) {
2306 #ifdef PERL_PRESERVE_IVUV
2315 else if (SvPOKp(sv)) {
2317 const char * const s = SvPV_const(sv, len);
2318 if (isIDFIRST(*s)) {
2319 sv_setpvn(TARG, "-", 1);
2322 else if (*s == '+' || *s == '-') {
2324 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2326 else if (DO_UTF8(sv)) {
2329 goto oops_its_an_int;
2331 sv_setnv(TARG, -SvNV(sv));
2333 sv_setpvn(TARG, "-", 1);
2340 goto oops_its_an_int;
2341 sv_setnv(TARG, -SvNV(sv));
2353 dSP; tryAMAGICunSET(not);
2354 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2360 dSP; dTARGET; tryAMAGICun(compl);
2365 if (PL_op->op_private & HINT_INTEGER) {
2366 const IV i = ~SvIV_nomg(sv);
2370 const UV u = ~SvUV_nomg(sv);
2379 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2380 sv_setsv_nomg(TARG, sv);
2381 tmps = (U8*)SvPV_force(TARG, len);
2384 /* Calculate exact length, let's not estimate. */
2393 while (tmps < send) {
2394 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2395 tmps += UTF8SKIP(tmps);
2396 targlen += UNISKIP(~c);
2402 /* Now rewind strings and write them. */
2406 Newxz(result, targlen + 1, U8);
2407 while (tmps < send) {
2408 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2409 tmps += UTF8SKIP(tmps);
2410 result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY);
2414 sv_setpvn(TARG, (char*)result, targlen);
2418 Newxz(result, nchar + 1, U8);
2419 while (tmps < send) {
2420 const U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY);
2421 tmps += UTF8SKIP(tmps);
2426 sv_setpvn(TARG, (char*)result, nchar);
2435 register long *tmpl;
2436 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2439 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
2444 for ( ; anum > 0; anum--, tmps++)
2453 /* integer versions of some of the above */
2457 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2460 SETi( left * right );
2467 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2471 DIE(aTHX_ "Illegal division by zero");
2472 value = POPi / value;
2481 /* This is the vanilla old i_modulo. */
2482 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2486 DIE(aTHX_ "Illegal modulus zero");
2487 SETi( left % right );
2492 #if defined(__GLIBC__) && IVSIZE == 8
2496 /* This is the i_modulo with the workaround for the _moddi3 bug
2497 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2498 * See below for pp_i_modulo. */
2499 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2503 DIE(aTHX_ "Illegal modulus zero");
2504 SETi( left % PERL_ABS(right) );
2512 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2516 DIE(aTHX_ "Illegal modulus zero");
2517 /* The assumption is to use hereafter the old vanilla version... */
2519 PL_ppaddr[OP_I_MODULO] =
2521 /* .. but if we have glibc, we might have a buggy _moddi3
2522 * (at least glicb 2.2.5 is known to have this bug), in other
2523 * words our integer modulus with negative quad as the second
2524 * argument might be broken. Test for this and re-patch the
2525 * opcode dispatch table if that is the case, remembering to
2526 * also apply the workaround so that this first round works
2527 * right, too. See [perl #9402] for more information. */
2528 #if defined(__GLIBC__) && IVSIZE == 8
2532 /* Cannot do this check with inlined IV constants since
2533 * that seems to work correctly even with the buggy glibc. */
2535 /* Yikes, we have the bug.
2536 * Patch in the workaround version. */
2538 PL_ppaddr[OP_I_MODULO] =
2539 &Perl_pp_i_modulo_1;
2540 /* Make certain we work right this time, too. */
2541 right = PERL_ABS(right);
2545 SETi( left % right );
2552 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2555 SETi( left + right );
2562 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2565 SETi( left - right );
2572 dSP; tryAMAGICbinSET(lt,0);
2575 SETs(boolSV(left < right));
2582 dSP; tryAMAGICbinSET(gt,0);
2585 SETs(boolSV(left > right));
2592 dSP; tryAMAGICbinSET(le,0);
2595 SETs(boolSV(left <= right));
2602 dSP; tryAMAGICbinSET(ge,0);
2605 SETs(boolSV(left >= right));
2612 dSP; tryAMAGICbinSET(eq,0);
2615 SETs(boolSV(left == right));
2622 dSP; tryAMAGICbinSET(ne,0);
2625 SETs(boolSV(left != right));
2632 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2639 else if (left < right)
2650 dSP; dTARGET; tryAMAGICun(neg);
2655 /* High falutin' math. */
2659 dSP; dTARGET; tryAMAGICbin(atan2,0);
2662 SETn(Perl_atan2(left, right));
2670 int amg_type = sin_amg;
2671 const char *neg_report = NULL;
2672 NV (*func)(NV) = &Perl_sin;
2673 const int op_type = PL_op->op_type;
2690 amg_type = sqrt_amg;
2692 neg_report = "sqrt";
2696 tryAMAGICun_var(amg_type);
2698 const NV value = POPn;
2700 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2701 SET_NUMERIC_STANDARD();
2702 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2705 XPUSHn(func(value));
2710 /* Support Configure command-line overrides for rand() functions.
2711 After 5.005, perhaps we should replace this by Configure support
2712 for drand48(), random(), or rand(). For 5.005, though, maintain
2713 compatibility by calling rand() but allow the user to override it.
2714 See INSTALL for details. --Andy Dougherty 15 July 1998
2716 /* Now it's after 5.005, and Configure supports drand48() and random(),
2717 in addition to rand(). So the overrides should not be needed any more.
2718 --Jarkko Hietaniemi 27 September 1998
2721 #ifndef HAS_DRAND48_PROTO
2722 extern double drand48 (void);
2735 if (!PL_srand_called) {
2736 (void)seedDrand01((Rand_seed_t)seed());
2737 PL_srand_called = TRUE;
2747 const UV anum = (MAXARG < 1) ? seed() : POPu;
2748 (void)seedDrand01((Rand_seed_t)anum);
2749 PL_srand_called = TRUE;
2756 dSP; dTARGET; tryAMAGICun(int);
2758 const IV iv = TOPi; /* attempt to convert to IV if possible. */
2759 /* XXX it's arguable that compiler casting to IV might be subtly
2760 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2761 else preferring IV has introduced a subtle behaviour change bug. OTOH
2762 relying on floating point to be accurate is a bug. */
2766 else if (SvIOK(TOPs)) {
2773 const NV value = TOPn;
2775 if (value < (NV)UV_MAX + 0.5) {
2778 SETn(Perl_floor(value));
2782 if (value > (NV)IV_MIN - 0.5) {
2785 SETn(Perl_ceil(value));
2795 dSP; dTARGET; tryAMAGICun(abs);
2797 /* This will cache the NV value if string isn't actually integer */
2802 else if (SvIOK(TOPs)) {
2803 /* IVX is precise */
2805 SETu(TOPu); /* force it to be numeric only */
2813 /* 2s complement assumption. Also, not really needed as
2814 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2820 const NV value = TOPn;
2834 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2838 SV* const sv = POPs;
2840 tmps = (SvPV_const(sv, len));
2842 /* If Unicode, try to downgrade
2843 * If not possible, croak. */
2844 SV* const tsv = sv_2mortal(newSVsv(sv));
2847 sv_utf8_downgrade(tsv, FALSE);
2848 tmps = SvPV_const(tsv, len);
2850 if (PL_op->op_type == OP_HEX)
2853 while (*tmps && len && isSPACE(*tmps))
2859 result_uv = grok_hex ((char *)tmps, &len, &flags, &result_nv);
2861 else if (*tmps == 'b')
2862 result_uv = grok_bin ((char *)tmps, &len, &flags, &result_nv);
2864 result_uv = grok_oct ((char *)tmps, &len, &flags, &result_nv);
2866 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2880 SV * const sv = TOPs;
2883 /* For an overloaded scalar, we can't know in advance if it's going to
2884 be UTF-8 or not. Also, we can't call sv_len_utf8 as it likes to
2885 cache the length. Maybe that should be a documented feature of it.
2888 const char *const p = SvPV_const(sv, len);
2891 SETi(utf8_length((U8*)p, (U8*)p + len));
2897 else if (DO_UTF8(sv))
2898 SETi(sv_len_utf8(sv));
2914 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2916 const I32 arybase = PL_curcop->cop_arybase;
2918 const char *repl = NULL;
2920 const int num_args = PL_op->op_private & 7;
2921 bool repl_need_utf8_upgrade = FALSE;
2922 bool repl_is_utf8 = FALSE;
2924 SvTAINTED_off(TARG); /* decontaminate */
2925 SvUTF8_off(TARG); /* decontaminate */
2929 repl = SvPV_const(repl_sv, repl_len);
2930 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
2940 sv_utf8_upgrade(sv);
2942 else if (DO_UTF8(sv))
2943 repl_need_utf8_upgrade = TRUE;
2945 tmps = SvPV_const(sv, curlen);
2947 utf8_curlen = sv_len_utf8(sv);
2948 if (utf8_curlen == curlen)
2951 curlen = utf8_curlen;
2956 if (pos >= arybase) {
2974 else if (len >= 0) {
2976 if (rem > (I32)curlen)
2991 Perl_croak(aTHX_ "substr outside of string");
2992 if (ckWARN(WARN_SUBSTR))
2993 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
2997 const I32 upos = pos;
2998 const I32 urem = rem;
3000 sv_pos_u2b(sv, &pos, &rem);
3002 /* we either return a PV or an LV. If the TARG hasn't been used
3003 * before, or is of that type, reuse it; otherwise use a mortal
3004 * instead. Note that LVs can have an extended lifetime, so also
3005 * dont reuse if refcount > 1 (bug #20933) */
3006 if (SvTYPE(TARG) > SVt_NULL) {
3007 if ( (SvTYPE(TARG) == SVt_PVLV)
3008 ? (!lvalue || SvREFCNT(TARG) > 1)
3011 TARG = sv_newmortal();
3015 sv_setpvn(TARG, tmps, rem);
3016 #ifdef USE_LOCALE_COLLATE
3017 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3022 SV* repl_sv_copy = NULL;
3024 if (repl_need_utf8_upgrade) {
3025 repl_sv_copy = newSVsv(repl_sv);
3026 sv_utf8_upgrade(repl_sv_copy);
3027 repl = SvPV_const(repl_sv_copy, repl_len);
3028 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3030 sv_insert(sv, pos, rem, (char *)repl, repl_len);
3034 SvREFCNT_dec(repl_sv_copy);
3036 else if (lvalue) { /* it's an lvalue! */
3037 if (!SvGMAGICAL(sv)) {
3039 SvPV_force_nolen(sv);
3040 if (ckWARN(WARN_SUBSTR))
3041 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3042 "Attempt to use reference as lvalue in substr");
3044 if (SvOK(sv)) /* is it defined ? */
3045 (void)SvPOK_only_UTF8(sv);
3047 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3050 if (SvTYPE(TARG) < SVt_PVLV) {
3051 sv_upgrade(TARG, SVt_PVLV);
3052 sv_magic(TARG, NULL, PERL_MAGIC_substr, NULL, 0);
3058 if (LvTARG(TARG) != sv) {
3060 SvREFCNT_dec(LvTARG(TARG));
3061 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
3063 LvTARGOFF(TARG) = upos;
3064 LvTARGLEN(TARG) = urem;
3068 PUSHs(TARG); /* avoid SvSETMAGIC here */
3075 register const IV size = POPi;
3076 register const IV offset = POPi;
3077 register SV * const src = POPs;
3078 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3080 SvTAINTED_off(TARG); /* decontaminate */
3081 if (lvalue) { /* it's an lvalue! */
3082 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3083 TARG = sv_newmortal();
3084 if (SvTYPE(TARG) < SVt_PVLV) {
3085 sv_upgrade(TARG, SVt_PVLV);
3086 sv_magic(TARG, NULL, PERL_MAGIC_vec, NULL, 0);
3089 if (LvTARG(TARG) != src) {
3091 SvREFCNT_dec(LvTARG(TARG));
3092 LvTARG(TARG) = SvREFCNT_inc_simple(src);
3094 LvTARGOFF(TARG) = offset;
3095 LvTARGLEN(TARG) = size;
3098 sv_setuv(TARG, do_vecget(src, offset, size));
3114 const char *little_p;
3115 const I32 arybase = PL_curcop->cop_arybase;
3118 const bool is_index = PL_op->op_type == OP_INDEX;
3121 /* arybase is in characters, like offset, so combine prior to the
3122 UTF-8 to bytes calculation. */
3123 offset = POPi - arybase;
3127 big_p = SvPV_const(big, biglen);
3128 little_p = SvPV_const(little, llen);
3130 big_utf8 = DO_UTF8(big);
3131 little_utf8 = DO_UTF8(little);
3132 if (big_utf8 ^ little_utf8) {
3133 /* One needs to be upgraded. */
3134 if (little_utf8 && !PL_encoding) {
3135 /* Well, maybe instead we might be able to downgrade the small
3137 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3140 /* If the large string is ISO-8859-1, and it's not possible to
3141 convert the small string to ISO-8859-1, then there is no
3142 way that it could be found anywhere by index. */
3147 /* At this point, pv is a malloc()ed string. So donate it to temp
3148 to ensure it will get free()d */
3149 little = temp = newSV(0);
3150 sv_usepvn(temp, pv, llen);
3151 little_p = SvPVX(little);
3154 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3157 sv_recode_to_utf8(temp, PL_encoding);
3159 sv_utf8_upgrade(temp);
3164 big_p = SvPV_const(big, biglen);
3167 little_p = SvPV_const(little, llen);
3171 if (SvGAMAGIC(big)) {
3172 /* Life just becomes a lot easier if I use a temporary here.
3173 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3174 will trigger magic and overloading again, as will fbm_instr()
3176 big = sv_2mortal(newSVpvn(big_p, biglen));
3181 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3182 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3183 warn on undef, and we've already triggered a warning with the
3184 SvPV_const some lines above. We can't remove that, as we need to
3185 call some SvPV to trigger overloading early and find out if the
3187 This is all getting to messy. The API isn't quite clean enough,
3188 because data access has side effects.
3190 little = sv_2mortal(newSVpvn(little_p, llen));
3193 little_p = SvPVX(little);
3197 offset = is_index ? 0 : biglen;
3199 if (big_utf8 && offset > 0)
3200 sv_pos_u2b(big, &offset, 0);
3206 else if (offset > (I32)biglen)
3208 if (!(little_p = is_index
3209 ? fbm_instr((unsigned char*)big_p + offset,
3210 (unsigned char*)big_p + biglen, little, 0)
3211 : rninstr(big_p, big_p + offset,
3212 little_p, little_p + llen)))
3215 retval = little_p - big_p;
3216 if (retval > 0 && big_utf8)
3217 sv_pos_b2u(big, &retval);
3222 PUSHi(retval + arybase);
3228 dSP; dMARK; dORIGMARK; dTARGET;
3229 do_sprintf(TARG, SP-MARK, MARK+1);
3230 TAINT_IF(SvTAINTED(TARG));
3241 const U8 *s = (U8*)SvPV_const(argsv, len);
3244 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3245 tmpsv = sv_2mortal(newSVsv(argsv));
3246 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3250 XPUSHu(DO_UTF8(argsv) ?
3251 utf8n_to_uvchr((U8 *)s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3263 (void)SvUPGRADE(TARG,SVt_PV);
3265 if (value > 255 && !IN_BYTES) {
3266 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3267 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3268 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3270 (void)SvPOK_only(TARG);
3279 *tmps++ = (char)value;
3281 (void)SvPOK_only(TARG);
3282 if (PL_encoding && !IN_BYTES) {
3283 sv_recode_to_utf8(TARG, PL_encoding);
3285 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3286 memEQ(tmps, "\xef\xbf\xbd\0", 4)) {
3290 *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value);
3291 *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value);
3306 const char *tmps = SvPV_const(left, len);
3308 if (DO_UTF8(left)) {
3309 /* If Unicode, try to downgrade.
3310 * If not possible, croak.
3311 * Yes, we made this up. */
3312 SV* const tsv = sv_2mortal(newSVsv(left));
3315 sv_utf8_downgrade(tsv, FALSE);
3316 tmps = SvPV_const(tsv, len);
3318 # ifdef USE_ITHREADS
3320 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3321 /* This should be threadsafe because in ithreads there is only
3322 * one thread per interpreter. If this would not be true,
3323 * we would need a mutex to protect this malloc. */
3324 PL_reentrant_buffer->_crypt_struct_buffer =
3325 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3326 #if defined(__GLIBC__) || defined(__EMX__)
3327 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3328 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3329 /* work around glibc-2.2.5 bug */
3330 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3334 # endif /* HAS_CRYPT_R */
3335 # endif /* USE_ITHREADS */
3337 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3339 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3345 "The crypt() function is unimplemented due to excessive paranoia.");
3356 bool inplace = TRUE;
3358 const int op_type = PL_op->op_type;
3361 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3367 s = (const U8*)SvPV_nomg_const(source, slen);
3373 if (slen && DO_UTF8(source) && UTF8_IS_START(*s)) {
3375 utf8_to_uvchr((U8 *)s, &ulen);
3376 if (op_type == OP_UCFIRST) {
3377 toTITLE_utf8((U8 *)s, tmpbuf, &tculen);
3379 toLOWER_utf8((U8 *)s, tmpbuf, &tculen);
3381 /* If the two differ, we definately cannot do inplace. */
3382 inplace = ulen == tculen;
3383 need = slen + 1 - ulen + tculen;
3389 if (SvPADTMP(source) && !SvREADONLY(source) && inplace) {
3390 /* We can convert in place. */
3393 s = d = (U8*)SvPV_force_nomg(source, slen);
3399 SvUPGRADE(dest, SVt_PV);
3400 d = (U8*)SvGROW(dest, need);
3401 (void)SvPOK_only(dest);
3410 /* slen is the byte length of the whole SV.
3411 * ulen is the byte length of the original Unicode character
3412 * stored as UTF-8 at s.
3413 * tculen is the byte length of the freshly titlecased (or
3414 * lowercased) Unicode character stored as UTF-8 at tmpbuf.
3415 * We first set the result to be the titlecased (/lowercased)
3416 * character, and then append the rest of the SV data. */
3417 sv_setpvn(dest, (char*)tmpbuf, tculen);
3419 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3423 Copy(tmpbuf, d, tculen, U8);
3424 SvCUR_set(dest, need - 1);
3429 if (IN_LOCALE_RUNTIME) {
3432 *d = (op_type == OP_UCFIRST)
3433 ? toUPPER_LC(*s) : toLOWER_LC(*s);
3436 *d = (op_type == OP_UCFIRST) ? toUPPER(*s) : toLOWER(*s);
3438 /* See bug #39028 */
3446 /* This will copy the trailing NUL */
3447 Copy(s + 1, d + 1, slen, U8);
3448 SvCUR_set(dest, need - 1);
3455 /* There's so much setup/teardown code common between uc and lc, I wonder if
3456 it would be worth merging the two, and just having a switch outside each
3457 of the three tight loops. */
3470 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3471 && !DO_UTF8(source)) {
3472 /* We can convert in place. */
3475 s = d = (U8*)SvPV_force_nomg(source, len);
3482 /* The old implementation would copy source into TARG at this point.
3483 This had the side effect that if source was undef, TARG was now
3484 an undefined SV with PADTMP set, and they don't warn inside
3485 sv_2pv_flags(). However, we're now getting the PV direct from
3486 source, which doesn't have PADTMP set, so it would warn. Hence the
3490 s = (const U8*)SvPV_nomg_const(source, len);
3497 SvUPGRADE(dest, SVt_PV);
3498 d = (U8*)SvGROW(dest, min);
3499 (void)SvPOK_only(dest);
3504 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3505 to check DO_UTF8 again here. */
3507 if (DO_UTF8(source)) {
3508 const U8 *const send = s + len;
3509 U8 tmpbuf[UTF8_MAXBYTES+1];
3512 const STRLEN u = UTF8SKIP(s);
3515 toUPPER_utf8((U8 *)s, tmpbuf, &ulen);
3516 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3517 /* If the eventually required minimum size outgrows
3518 * the available space, we need to grow. */
3519 const UV o = d - (U8*)SvPVX_const(dest);
3521 /* If someone uppercases one million U+03B0s we SvGROW() one
3522 * million times. Or we could try guessing how much to
3523 allocate without allocating too much. Such is life. */
3525 d = (U8*)SvPVX(dest) + o;
3527 Copy(tmpbuf, d, ulen, U8);
3533 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3536 const U8 *const send = s + len;
3537 if (IN_LOCALE_RUNTIME) {
3540 for (; s < send; d++, s++)
3541 *d = toUPPER_LC(*s);
3544 for (; s < send; d++, s++)
3548 if (source != dest) {
3550 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3569 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3570 && !DO_UTF8(source)) {
3571 /* We can convert in place. */
3574 s = d = (U8*)SvPV_force_nomg(source, len);
3581 /* The old implementation would copy source into TARG at this point.
3582 This had the side effect that if source was undef, TARG was now
3583 an undefined SV with PADTMP set, and they don't warn inside
3584 sv_2pv_flags(). However, we're now getting the PV direct from
3585 source, which doesn't have PADTMP set, so it would warn. Hence the
3589 s = (const U8*)SvPV_nomg_const(source, len);
3596 SvUPGRADE(dest, SVt_PV);
3597 d = (U8*)SvGROW(dest, min);
3598 (void)SvPOK_only(dest);
3603 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3604 to check DO_UTF8 again here. */
3606 if (DO_UTF8(source)) {
3607 const U8 *const send = s + len;
3608 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3611 const STRLEN u = UTF8SKIP(s);
3613 const UV uv = toLOWER_utf8((U8 *)s, tmpbuf, &ulen);
3615 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode U+03A3 */
3616 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3619 * Now if the sigma is NOT followed by
3620 * /$ignorable_sequence$cased_letter/;
3621 * and it IS preceded by /$cased_letter$ignorable_sequence/;
3622 * where $ignorable_sequence is [\x{2010}\x{AD}\p{Mn}]*
3623 * and $cased_letter is [\p{Ll}\p{Lo}\p{Lt}]
3624 * then it should be mapped to 0x03C2,
3625 * (GREEK SMALL LETTER FINAL SIGMA),
3626 * instead of staying 0x03A3.
3627 * "should be": in other words, this is not implemented yet.
3628 * See lib/unicore/SpecialCasing.txt.
3631 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3632 /* If the eventually required minimum size outgrows
3633 * the available space, we need to grow. */
3634 const UV o = d - (U8*)SvPVX_const(dest);
3636 /* If someone lowercases one million U+0130s we SvGROW() one
3637 * million times. Or we could try guessing how much to
3638 allocate without allocating too much. Such is life. */
3640 d = (U8*)SvPVX(dest) + o;
3642 Copy(tmpbuf, d, ulen, U8);
3648 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3651 const U8 *const send = s + len;
3652 if (IN_LOCALE_RUNTIME) {
3655 for (; s < send; d++, s++)
3656 *d = toLOWER_LC(*s);
3659 for (; s < send; d++, s++)
3663 if (source != dest) {
3665 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3675 SV * const sv = TOPs;
3677 register const char *s = SvPV_const(sv,len);
3679 SvUTF8_off(TARG); /* decontaminate */
3682 (void)SvUPGRADE(TARG, SVt_PV);
3683 SvGROW(TARG, (len * 2) + 1);
3687 if (UTF8_IS_CONTINUED(*s)) {
3688 STRLEN ulen = UTF8SKIP(s);
3712 SvCUR_set(TARG, d - SvPVX_const(TARG));
3713 (void)SvPOK_only_UTF8(TARG);
3716 sv_setpvn(TARG, s, len);
3718 if (SvSMAGICAL(TARG))
3727 dSP; dMARK; dORIGMARK;
3728 register AV* const av = (AV*)POPs;
3729 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3731 if (SvTYPE(av) == SVt_PVAV) {
3732 const I32 arybase = PL_curcop->cop_arybase;
3733 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3736 for (svp = MARK + 1; svp <= SP; svp++) {
3737 const I32 elem = SvIVx(*svp);
3741 if (max > AvMAX(av))
3744 while (++MARK <= SP) {
3746 I32 elem = SvIVx(*MARK);
3750 svp = av_fetch(av, elem, lval);
3752 if (!svp || *svp == &PL_sv_undef)
3753 DIE(aTHX_ PL_no_aelem, elem);
3754 if (PL_op->op_private & OPpLVAL_INTRO)
3755 save_aelem(av, elem, svp);
3757 *MARK = svp ? *svp : &PL_sv_undef;
3760 if (GIMME != G_ARRAY) {
3762 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
3768 /* Associative arrays. */
3773 HV * const hash = (HV*)POPs;
3775 const I32 gimme = GIMME_V;
3776 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
3779 /* might clobber stack_sp */
3780 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
3785 SV* const sv = hv_iterkeysv(entry);
3786 PUSHs(sv); /* won't clobber stack_sp */
3787 if (gimme == G_ARRAY) {
3790 /* might clobber stack_sp */
3792 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
3797 else if (gimme == G_SCALAR)
3806 const I32 gimme = GIMME_V;
3807 const I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3809 if (PL_op->op_private & OPpSLICE) {
3811 HV * const hv = (HV*)POPs;
3812 const U32 hvtype = SvTYPE(hv);
3813 if (hvtype == SVt_PVHV) { /* hash element */
3814 while (++MARK <= SP) {
3815 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
3816 *MARK = sv ? sv : &PL_sv_undef;
3819 else if (hvtype == SVt_PVAV) {
3820 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3821 while (++MARK <= SP) {
3822 SV * const sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3823 *MARK = sv ? sv : &PL_sv_undef;
3826 else { /* pseudo-hash element */
3827 while (++MARK <= SP) {
3828 SV * const sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
3829 *MARK = sv ? sv : &PL_sv_undef;
3834 DIE(aTHX_ "Not a HASH reference");
3837 else if (gimme == G_SCALAR) {
3842 *++MARK = &PL_sv_undef;
3848 HV * const hv = (HV*)POPs;
3850 if (SvTYPE(hv) == SVt_PVHV)
3851 sv = hv_delete_ent(hv, keysv, discard, 0);
3852 else if (SvTYPE(hv) == SVt_PVAV) {
3853 if (PL_op->op_flags & OPf_SPECIAL)
3854 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3856 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
3859 DIE(aTHX_ "Not a HASH reference");
3874 if (PL_op->op_private & OPpEXISTS_SUB) {
3876 SV * const sv = POPs;
3877 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
3880 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3886 if (SvTYPE(hv) == SVt_PVHV) {
3887 if (hv_exists_ent(hv, tmpsv, 0))
3890 else if (SvTYPE(hv) == SVt_PVAV) {
3891 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3892 if (av_exists((AV*)hv, SvIV(tmpsv)))
3895 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
3899 DIE(aTHX_ "Not a HASH reference");
3906 dSP; dMARK; dORIGMARK;
3907 register HV * const hv = (HV*)POPs;
3908 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3909 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
3910 const bool localizing = PL_op->op_private & OPpLVAL_INTRO ? TRUE : FALSE;
3911 bool other_magic = FALSE;
3917 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
3918 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
3919 /* Try to preserve the existenceness of a tied hash
3920 * element by using EXISTS and DELETE if possible.
3921 * Fallback to FETCH and STORE otherwise */
3922 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
3923 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
3924 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
3927 if (!realhv && localizing)
3928 DIE(aTHX_ "Can't localize pseudo-hash element");
3930 if (realhv || SvTYPE(hv) == SVt_PVAV) {
3931 while (++MARK <= SP) {
3934 bool preeminent = FALSE;
3937 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
3938 realhv ? hv_exists_ent(hv, keysv, 0)
3939 : avhv_exists_ent((AV*)hv, keysv, 0);
3943 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
3944 svp = he ? &HeVAL(he) : 0;
3947 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
3950 if (!svp || *svp == &PL_sv_undef) {
3951 DIE(aTHX_ PL_no_helem_sv, keysv);
3955 save_helem(hv, keysv, svp);
3958 const char *key = SvPV_const(keysv, keylen);
3959 SAVEDELETE(hv, savepvn(key,keylen), keylen);
3963 *MARK = svp ? *svp : &PL_sv_undef;
3966 if (GIMME != G_ARRAY) {
3968 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
3974 /* List operators. */
3979 if (GIMME != G_ARRAY) {
3981 *MARK = *SP; /* unwanted list, return last item */
3983 *MARK = &PL_sv_undef;
3992 SV ** const lastrelem = PL_stack_sp;
3993 SV ** const lastlelem = PL_stack_base + POPMARK;
3994 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
3995 register SV ** const firstrelem = lastlelem + 1;
3996 const I32 arybase = PL_curcop->cop_arybase;
3997 I32 is_something_there = PL_op->op_flags & OPf_MOD;
3999 register const I32 max = lastrelem - lastlelem;
4000 register SV **lelem;
4002 if (GIMME != G_ARRAY) {
4003 I32 ix = SvIVx(*lastlelem);
4008 if (ix < 0 || ix >= max)
4009 *firstlelem = &PL_sv_undef;
4011 *firstlelem = firstrelem[ix];
4017 SP = firstlelem - 1;
4021 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4022 I32 ix = SvIVx(*lelem);
4027 if (ix < 0 || ix >= max)
4028 *lelem = &PL_sv_undef;
4030 is_something_there = TRUE;
4031 if (!(*lelem = firstrelem[ix]))
4032 *lelem = &PL_sv_undef;
4035 if (is_something_there)
4038 SP = firstlelem - 1;
4044 dSP; dMARK; dORIGMARK;
4045 const I32 items = SP - MARK;
4046 SV * const av = (SV *) av_make(items, MARK+1);
4047 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4048 XPUSHs(sv_2mortal((PL_op->op_flags & OPf_SPECIAL)
4049 ? newRV_noinc(av) : av));
4055 dSP; dMARK; dORIGMARK;
4056 HV* const hv = newHV();
4059 SV * const key = *++MARK;
4060 SV * const val = NEWSV(46, 0);
4062 sv_setsv(val, *++MARK);
4063 else if (ckWARN(WARN_MISC))
4064 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4065 (void)hv_store_ent(hv,key,val,0);
4068 XPUSHs(sv_2mortal((PL_op->op_flags & OPf_SPECIAL)
4069 ? newRV_noinc((SV*) hv) : (SV*)hv));
4075 dSP; dMARK; dORIGMARK;
4076 register AV *ary = (AV*)*++MARK;
4080 register I32 offset;
4081 register I32 length;
4085 SV **tmparyval = NULL;
4086 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4089 *MARK-- = SvTIED_obj((SV*)ary, mg);
4093 call_method("SPLICE",GIMME_V);
4102 offset = i = SvIVx(*MARK);
4104 offset += AvFILLp(ary) + 1;
4106 offset -= PL_curcop->cop_arybase;
4108 DIE(aTHX_ PL_no_aelem, i);
4110 length = SvIVx(*MARK++);
4112 length += AvFILLp(ary) - offset + 1;
4118 length = AvMAX(ary) + 1; /* close enough to infinity */
4122 length = AvMAX(ary) + 1;
4124 if (offset > AvFILLp(ary) + 1) {
4125 if (ckWARN(WARN_MISC))
4126 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4127 offset = AvFILLp(ary) + 1;
4129 after = AvFILLp(ary) + 1 - (offset + length);
4130 if (after < 0) { /* not that much array */
4131 length += after; /* offset+length now in array */
4137 /* At this point, MARK .. SP-1 is our new LIST */
4140 diff = newlen - length;
4141 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4144 /* make new elements SVs now: avoid problems if they're from the array */
4145 for (dst = MARK, i = newlen; i; i--) {
4146 SV * const h = *dst;
4147 *dst++ = newSVsv(h);
4150 if (diff < 0) { /* shrinking the area */
4152 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4153 Copy(MARK, tmparyval, newlen, SV*);
4156 MARK = ORIGMARK + 1;
4157 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4158 MEXTEND(MARK, length);
4159 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4161 EXTEND_MORTAL(length);
4162 for (i = length, dst = MARK; i; i--) {
4163 sv_2mortal(*dst); /* free them eventualy */
4170 *MARK = AvARRAY(ary)[offset+length-1];
4173 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4174 SvREFCNT_dec(*dst++); /* free them now */
4177 AvFILLp(ary) += diff;
4179 /* pull up or down? */
4181 if (offset < after) { /* easier to pull up */
4182 if (offset) { /* esp. if nothing to pull */
4183 src = &AvARRAY(ary)[offset-1];
4184 dst = src - diff; /* diff is negative */
4185 for (i = offset; i > 0; i--) /* can't trust Copy */
4189 SvPV_set(ary, (char*)(AvARRAY(ary) - diff)); /* diff is negative */
4193 if (after) { /* anything to pull down? */
4194 src = AvARRAY(ary) + offset + length;
4195 dst = src + diff; /* diff is negative */
4196 Move(src, dst, after, SV*);
4198 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4199 /* avoid later double free */
4203 dst[--i] = &PL_sv_undef;
4206 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4207 Safefree(tmparyval);
4210 else { /* no, expanding (or same) */
4212 Newx(tmparyval, length, SV*); /* so remember deletion */
4213 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4216 if (diff > 0) { /* expanding */
4218 /* push up or down? */
4220 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4224 Move(src, dst, offset, SV*);
4226 SvPV_set(ary, (char*)(AvARRAY(ary) - diff));/* diff is positive */
4228 AvFILLp(ary) += diff;
4231 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4232 av_extend(ary, AvFILLp(ary) + diff);
4233 AvFILLp(ary) += diff;
4236 dst = AvARRAY(ary) + AvFILLp(ary);
4238 for (i = after; i; i--) {
4246 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4249 MARK = ORIGMARK + 1;
4250 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4252 Copy(tmparyval, MARK, length, SV*);
4254 EXTEND_MORTAL(length);
4255 for (i = length, dst = MARK; i; i--) {
4256 sv_2mortal(*dst); /* free them eventualy */
4260 Safefree(tmparyval);
4264 else if (length--) {
4265 *MARK = tmparyval[length];
4268 while (length-- > 0)
4269 SvREFCNT_dec(tmparyval[length]);
4271 Safefree(tmparyval);
4274 *MARK = &PL_sv_undef;
4282 dSP; dMARK; dORIGMARK; dTARGET;
4283 register AV *ary = (AV*)*++MARK;
4284 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4287 *MARK-- = SvTIED_obj((SV*)ary, mg);
4291 call_method("PUSH",G_SCALAR|G_DISCARD);
4295 PUSHi( AvFILL(ary) + 1 );
4298 for (++MARK; MARK <= SP; MARK++) {
4299 SV * const sv = NEWSV(51, 0);
4301 sv_setsv(sv, *MARK);
4302 av_store(ary, AvFILLp(ary)+1, sv);
4305 PUSHi( AvFILLp(ary) + 1 );
4313 AV * const av = (AV*)POPs;
4314 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
4319 (void)sv_2mortal(sv);
4326 dSP; dMARK; dORIGMARK; dTARGET;
4327 register AV *ary = (AV*)*++MARK;
4328 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4331 *MARK-- = SvTIED_obj((SV*)ary, mg);
4335 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4341 av_unshift(ary, SP - MARK);
4343 SV * const sv = newSVsv(*++MARK);
4344 (void)av_store(ary, i++, sv);
4348 PUSHi( AvFILL(ary) + 1 );
4355 SV ** const oldsp = SP;
4357 if (GIMME == G_ARRAY) {
4360 register SV * const tmp = *MARK;
4364 /* safe as long as stack cannot get extended in the above */
4369 register char *down;
4374 SvUTF8_off(TARG); /* decontaminate */
4376 do_join(TARG, &PL_sv_no, MARK, SP);
4378 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
4379 up = SvPV_force(TARG, len);
4381 if (DO_UTF8(TARG)) { /* first reverse each character */
4382 U8* s = (U8*)SvPVX(TARG);
4383 const U8* send = (U8*)(s + len);
4385 if (UTF8_IS_INVARIANT(*s)) {
4390 if (!utf8_to_uvchr(s, 0))
4394 down = (char*)(s - 1);
4395 /* reverse this character */
4399 *down-- = (char)tmp;
4405 down = SvPVX(TARG) + len - 1;
4409 *down-- = (char)tmp;
4411 (void)SvPOK_only_UTF8(TARG);
4423 register IV limit = POPi; /* note, negative is forever */
4424 SV * const sv = POPs;
4426 register const char *s = SvPV_const(sv, len);
4427 const bool do_utf8 = DO_UTF8(sv);
4428 const char *strend = s + len;
4430 register REGEXP *rx;
4432 register const char *m;
4434 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s);
4435 I32 maxiters = slen + 10;
4437 const I32 origlimit = limit;
4440 const I32 gimme = GIMME_V;
4441 const I32 oldsave = PL_savestack_ix;
4442 I32 make_mortal = 1;
4446 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4451 DIE(aTHX_ "panic: pp_split");
4454 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4455 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4457 RX_MATCH_UTF8_set(rx, do_utf8);
4459 if (pm->op_pmreplroot) {
4461 ary = GvAVn((GV*)PAD_SVl(INT2PTR(PADOFFSET, pm->op_pmreplroot)));
4463 ary = GvAVn((GV*)pm->op_pmreplroot);
4466 else if (gimme != G_ARRAY)
4467 #ifdef USE_5005THREADS
4468 ary = (AV*)PAD_SVl(0);
4470 ary = GvAVn(PL_defgv);
4471 #endif /* USE_5005THREADS */
4474 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4480 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4482 XPUSHs(SvTIED_obj((SV*)ary, mg));
4489 for (i = AvFILLp(ary); i >= 0; i--)
4490 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4492 /* temporarily switch stacks */
4493 SAVESWITCHSTACK(PL_curstack, ary);
4497 base = SP - PL_stack_base;
4499 if (pm->op_pmflags & PMf_SKIPWHITE) {
4500 if (pm->op_pmflags & PMf_LOCALE) {
4501 while (isSPACE_LC(*s))
4509 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4510 SAVEINT(PL_multiline);
4511 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4515 limit = maxiters + 2;
4516 if (pm->op_pmflags & PMf_WHITE) {
4519 while (m < strend &&
4520 !((pm->op_pmflags & PMf_LOCALE)
4521 ? isSPACE_LC(*m) : isSPACE(*m)))
4526 dstr = newSVpvn(s, m-s);
4530 (void)SvUTF8_on(dstr);
4534 while (s < strend &&
4535 ((pm->op_pmflags & PMf_LOCALE)
4536 ? isSPACE_LC(*s) : isSPACE(*s)))
4540 else if (rx->precomp[0] == '^' && rx->precomp[1] == '\0') {
4542 for (m = s; m < strend && *m != '\n'; m++)
4547 dstr = newSVpvn(s, m-s);
4551 (void)SvUTF8_on(dstr);
4556 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4557 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4558 && (rx->reganch & ROPT_CHECK_ALL)
4559 && !(rx->reganch & ROPT_ANCH)) {
4560 const int tail = (rx->reganch & RE_INTUIT_TAIL);
4561 SV * const csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4564 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4565 const char c = *SvPV_nolen_const(csv);
4567 for (m = s; m < strend && *m != c; m++)
4571 dstr = newSVpvn(s, m-s);
4575 (void)SvUTF8_on(dstr);
4577 /* The rx->minlen is in characters but we want to step
4578 * s ahead by bytes. */
4580 s = (char*)utf8_hop((U8*)m, len);
4582 s = m + len; /* Fake \n at the end */
4586 while (s < strend && --limit &&
4587 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4588 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
4590 dstr = newSVpvn(s, m-s);
4594 (void)SvUTF8_on(dstr);
4596 /* The rx->minlen is in characters but we want to step
4597 * s ahead by bytes. */
4599 s = (char*)utf8_hop((U8*)m, len);
4601 s = m + len; /* Fake \n at the end */
4606 maxiters += slen * rx->nparens;
4607 while (s < strend && --limit)
4611 rex_return = CALLREGEXEC(aTHX_ rx, (char*)s, (char*)strend, (char*)orig, 1 ,
4614 if (rex_return == 0)
4616 TAINT_IF(RX_MATCH_TAINTED(rx));
4617 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4622 strend = s + (strend - m);
4624 m = rx->startp[0] + orig;
4625 dstr = newSVpvn(s, m-s);
4629 (void)SvUTF8_on(dstr);
4633 for (i = 1; i <= (I32)rx->nparens; i++) {
4634 s = rx->startp[i] + orig;
4635 m = rx->endp[i] + orig;
4637 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4638 parens that didn't match -- they should be set to
4639 undef, not the empty string */
4640 if (m >= orig && s >= orig) {
4641 dstr = newSVpvn(s, m-s);
4644 dstr = &PL_sv_undef; /* undef, not "" */
4648 (void)SvUTF8_on(dstr);
4652 s = rx->endp[0] + orig;
4656 iters = (SP - PL_stack_base) - base;
4657 if (iters > maxiters)
4658 DIE(aTHX_ "Split loop");
4660 /* keep field after final delim? */
4661 if (s < strend || (iters && origlimit)) {
4662 const STRLEN l = strend - s;
4663 dstr = newSVpvn(s, l);
4667 (void)SvUTF8_on(dstr);
4671 else if (!origlimit) {
4672 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
4673 if (TOPs && !make_mortal)
4676 *SP-- = &PL_sv_undef;
4681 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
4685 if (SvSMAGICAL(ary)) {
4690 if (gimme == G_ARRAY) {
4692 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4700 call_method("PUSH",G_SCALAR|G_DISCARD);
4703 if (gimme == G_ARRAY) {
4705 /* EXTEND should not be needed - we just popped them */
4707 for (i=0; i < iters; i++) {
4708 SV **svp = av_fetch(ary, i, FALSE);
4709 PUSHs((svp) ? *svp : &PL_sv_undef);
4716 if (gimme == G_ARRAY)
4725 #ifdef USE_5005THREADS
4727 Perl_unlock_condpair(pTHX_ void *svv)
4729 MAGIC *mg = mg_find((SV*)svv, PERL_MAGIC_mutex);
4732 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
4733 MUTEX_LOCK(MgMUTEXP(mg));
4734 if (MgOWNER(mg) != thr)
4735 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
4737 COND_SIGNAL(MgOWNERCONDP(mg));
4738 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
4739 PTR2UV(thr), PTR2UV(svv)));
4740 MUTEX_UNLOCK(MgMUTEXP(mg));
4742 #endif /* USE_5005THREADS */
4750 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4751 || SvTYPE(retsv) == SVt_PVCV) {
4752 retsv = refto(retsv);
4759 PP(unimplemented_op)
4761 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
4767 * c-indentation-style: bsd
4769 * indent-tabs-mode: t
4772 * ex: set ts=8 sts=4 sw=4 noet: