3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 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 (void)SvREFCNT_inc(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);
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 * const 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)) {
248 if (PL_op->op_private & HINT_STRICT_REFS) {
250 DIE(aTHX_ PL_no_symref_sv, sv, "a SCALAR");
252 DIE(aTHX_ PL_no_usym, "a SCALAR");
255 if (PL_op->op_flags & OPf_REF)
256 DIE(aTHX_ PL_no_usym, "a SCALAR");
257 if (ckWARN(WARN_UNINITIALIZED))
261 if ((PL_op->op_flags & OPf_SPECIAL) &&
262 !(PL_op->op_flags & OPf_MOD))
264 gv = (GV*)gv_fetchsv(sv, 0, SVt_PV);
266 && (!is_gv_magical_sv(sv, 0)
267 || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV))))
273 gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV);
278 if (PL_op->op_flags & OPf_MOD) {
279 if (PL_op->op_private & OPpLVAL_INTRO) {
280 if (cUNOP->op_first->op_type == OP_NULL)
281 sv = save_scalar((GV*)TOPs);
283 sv = save_scalar(gv);
285 Perl_croak(aTHX_ PL_no_localize_ref);
287 else if (PL_op->op_private & OPpDEREF)
288 vivify_ref(sv, PL_op->op_private & OPpDEREF);
297 AV * const av = (AV*)TOPs;
298 SV ** const sv = Perl_av_arylen_p(aTHX_ (AV*)av);
301 sv_upgrade(*sv, SVt_PVMG);
302 sv_magic(*sv, (SV*)av, PERL_MAGIC_arylen, Nullch, 0);
310 dSP; dTARGET; dPOPss;
312 if (PL_op->op_flags & OPf_MOD || LVRET) {
313 if (SvTYPE(TARG) < SVt_PVLV) {
314 sv_upgrade(TARG, SVt_PVLV);
315 sv_magic(TARG, Nullsv, PERL_MAGIC_pos, Nullch, 0);
319 if (LvTARG(TARG) != sv) {
321 SvREFCNT_dec(LvTARG(TARG));
322 LvTARG(TARG) = SvREFCNT_inc(sv);
324 PUSHs(TARG); /* no SvSETMAGIC */
328 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
329 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
330 if (mg && mg->mg_len >= 0) {
334 PUSHi(i + PL_curcop->cop_arybase);
347 I32 flags = (PL_op->op_flags & OPf_SPECIAL) ? 0
348 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT))
349 == OPpMAY_RETURN_CONSTANT) ? GV_ADD|GV_NOEXPAND : GV_ADD;
350 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
351 /* (But not in defined().) */
353 CV *cv = sv_2cv(TOPs, &stash, &gv, flags);
356 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
357 if ((PL_op->op_private & OPpLVAL_INTRO)) {
358 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
361 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
364 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
368 cv = (CV*)&PL_sv_undef;
382 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
383 const char * const s = SvPVX_const(TOPs);
384 if (strnEQ(s, "CORE::", 6)) {
385 const int code = keyword(s + 6, SvCUR(TOPs) - 6);
386 if (code < 0) { /* Overridable. */
387 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
388 int i = 0, n = 0, seen_question = 0;
390 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
392 if (code == -KEY_chop || code == -KEY_chomp
393 || code == -KEY_exec || code == -KEY_system)
395 while (i < MAXO) { /* The slow way. */
396 if (strEQ(s + 6, PL_op_name[i])
397 || strEQ(s + 6, PL_op_desc[i]))
403 goto nonesuch; /* Should not happen... */
405 oa = PL_opargs[i] >> OASHIFT;
407 if (oa & OA_OPTIONAL && !seen_question) {
411 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
412 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
413 /* But globs are already references (kinda) */
414 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
418 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
422 ret = sv_2mortal(newSVpvn(str, n - 1));
424 else if (code) /* Non-Overridable */
426 else { /* None such */
428 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
432 cv = sv_2cv(TOPs, &stash, &gv, 0);
434 ret = sv_2mortal(newSVpvn(SvPVX_const(cv), SvCUR(cv)));
443 CV* cv = (CV*)PAD_SV(PL_op->op_targ);
445 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
461 if (GIMME != G_ARRAY) {
465 *MARK = &PL_sv_undef;
466 *MARK = refto(*MARK);
470 EXTEND_MORTAL(SP - MARK);
472 *MARK = refto(*MARK);
477 S_refto(pTHX_ SV *sv)
481 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
484 if (!(sv = LvTARG(sv)))
487 (void)SvREFCNT_inc(sv);
489 else if (SvTYPE(sv) == SVt_PVAV) {
490 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
493 (void)SvREFCNT_inc(sv);
495 else if (SvPADTMP(sv) && !IS_PADGV(sv))
499 (void)SvREFCNT_inc(sv);
502 sv_upgrade(rv, SVt_RV);
512 SV * const sv = POPs;
517 if (!sv || !SvROK(sv))
520 pv = sv_reftype(SvRV(sv),TRUE);
521 PUSHp(pv, strlen(pv));
531 stash = CopSTASH(PL_curcop);
533 SV * const ssv = POPs;
537 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
538 Perl_croak(aTHX_ "Attempt to bless into a reference");
539 ptr = SvPV_const(ssv,len);
540 if (len == 0 && ckWARN(WARN_MISC))
541 Perl_warner(aTHX_ packWARN(WARN_MISC),
542 "Explicit blessing to '' (assuming package main)");
543 stash = gv_stashpvn(ptr, len, TRUE);
546 (void)sv_bless(TOPs, stash);
555 const char * const elem = SvPV_nolen_const(sv);
556 GV * const gv = (GV*)POPs;
557 SV * tmpRef = Nullsv;
561 /* elem will always be NUL terminated. */
562 const char * const second_letter = elem + 1;
565 if (strEQ(second_letter, "RRAY"))
566 tmpRef = (SV*)GvAV(gv);
569 if (strEQ(second_letter, "ODE"))
570 tmpRef = (SV*)GvCVu(gv);
573 if (strEQ(second_letter, "ILEHANDLE")) {
574 /* finally deprecated in 5.8.0 */
575 deprecate("*glob{FILEHANDLE}");
576 tmpRef = (SV*)GvIOp(gv);
579 if (strEQ(second_letter, "ORMAT"))
580 tmpRef = (SV*)GvFORM(gv);
583 if (strEQ(second_letter, "LOB"))
587 if (strEQ(second_letter, "ASH"))
588 tmpRef = (SV*)GvHV(gv);
591 if (*second_letter == 'O' && !elem[2])
592 tmpRef = (SV*)GvIOp(gv);
595 if (strEQ(second_letter, "AME"))
596 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
599 if (strEQ(second_letter, "ACKAGE")) {
600 const HEK *hek = HvNAME_HEK(GvSTASH(gv));
601 sv = hek ? newSVhek(hek) : newSVpvn("__ANON__", 8);
605 if (strEQ(second_letter, "CALAR"))
620 /* Pattern matching */
625 register unsigned char *s;
628 register I32 *sfirst;
632 if (sv == PL_lastscream) {
638 SvSCREAM_off(PL_lastscream);
639 SvREFCNT_dec(PL_lastscream);
641 PL_lastscream = SvREFCNT_inc(sv);
644 s = (unsigned char*)(SvPV(sv, len));
648 if (pos > PL_maxscream) {
649 if (PL_maxscream < 0) {
650 PL_maxscream = pos + 80;
651 Newx(PL_screamfirst, 256, I32);
652 Newx(PL_screamnext, PL_maxscream, I32);
655 PL_maxscream = pos + pos / 4;
656 Renew(PL_screamnext, PL_maxscream, I32);
660 sfirst = PL_screamfirst;
661 snext = PL_screamnext;
663 if (!sfirst || !snext)
664 DIE(aTHX_ "do_study: out of memory");
666 for (ch = 256; ch; --ch)
671 register const I32 ch = s[pos];
673 snext[pos] = sfirst[ch] - pos;
680 /* piggyback on m//g magic */
681 sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0);
690 if (PL_op->op_flags & OPf_STACKED)
692 else if (PL_op->op_private & OPpTARGET_MY)
698 TARG = sv_newmortal();
703 /* Lvalue operators. */
715 dSP; dMARK; dTARGET; dORIGMARK;
717 do_chop(TARG, *++MARK);
726 SETi(do_chomp(TOPs));
733 register I32 count = 0;
736 count += do_chomp(POPs);
746 if (!PL_op->op_private) {
755 SV_CHECK_THINKFIRST_COW_DROP(sv);
757 switch (SvTYPE(sv)) {
767 if (cv_const_sv((CV*)sv) && ckWARN(WARN_MISC))
768 Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
769 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
773 /* let user-undef'd sub keep its identity */
774 GV* const gv = CvGV((CV*)sv);
781 SvSetMagicSV(sv, &PL_sv_undef);
786 GvGP(sv) = gp_ref(gp);
787 GvSV(sv) = NEWSV(72,0);
788 GvLINE(sv) = CopLINE(PL_curcop);
794 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
796 SvPV_set(sv, Nullch);
809 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
810 DIE(aTHX_ PL_no_modify);
811 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
812 && SvIVX(TOPs) != IV_MIN)
814 SvIV_set(TOPs, SvIVX(TOPs) - 1);
815 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
826 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
827 DIE(aTHX_ PL_no_modify);
828 sv_setsv(TARG, TOPs);
829 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
830 && SvIVX(TOPs) != IV_MAX)
832 SvIV_set(TOPs, SvIVX(TOPs) + 1);
833 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
838 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
848 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
849 DIE(aTHX_ PL_no_modify);
850 sv_setsv(TARG, TOPs);
851 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
852 && SvIVX(TOPs) != IV_MIN)
854 SvIV_set(TOPs, SvIVX(TOPs) - 1);
855 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
864 /* Ordinary operators. */
869 #ifdef PERL_PRESERVE_IVUV
872 tryAMAGICbin(pow,opASSIGN);
873 #ifdef PERL_PRESERVE_IVUV
874 /* For integer to integer power, we do the calculation by hand wherever
875 we're sure it is safe; otherwise we call pow() and try to convert to
876 integer afterwards. */
889 const IV iv = SvIVX(TOPs);
893 goto float_it; /* Can't do negative powers this way. */
897 baseuok = SvUOK(TOPm1s);
899 baseuv = SvUVX(TOPm1s);
901 const IV iv = SvIVX(TOPm1s);
904 baseuok = TRUE; /* effectively it's a UV now */
906 baseuv = -iv; /* abs, baseuok == false records sign */
909 /* now we have integer ** positive integer. */
912 /* foo & (foo - 1) is zero only for a power of 2. */
913 if (!(baseuv & (baseuv - 1))) {
914 /* We are raising power-of-2 to a positive integer.
915 The logic here will work for any base (even non-integer
916 bases) but it can be less accurate than
917 pow (base,power) or exp (power * log (base)) when the
918 intermediate values start to spill out of the mantissa.
919 With powers of 2 we know this can't happen.
920 And powers of 2 are the favourite thing for perl
921 programmers to notice ** not doing what they mean. */
923 NV base = baseuok ? baseuv : -(NV)baseuv;
928 while (power >>= 1) {
939 register unsigned int highbit = 8 * sizeof(UV);
940 register unsigned int diff = 8 * sizeof(UV);
943 if (baseuv >> highbit) {
947 /* we now have baseuv < 2 ** highbit */
948 if (power * highbit <= 8 * sizeof(UV)) {
949 /* result will definitely fit in UV, so use UV math
950 on same algorithm as above */
951 register UV result = 1;
952 register UV base = baseuv;
953 const bool odd_power = (bool)(power & 1);
957 while (power >>= 1) {
964 if (baseuok || !odd_power)
965 /* answer is positive */
967 else if (result <= (UV)IV_MAX)
968 /* answer negative, fits in IV */
970 else if (result == (UV)IV_MIN)
971 /* 2's complement assumption: special case IV_MIN */
974 /* answer negative, doesn't fit */
986 SETn( Perl_pow( left, right) );
987 #ifdef PERL_PRESERVE_IVUV
997 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
998 #ifdef PERL_PRESERVE_IVUV
1001 /* Unless the left argument is integer in range we are going to have to
1002 use NV maths. Hence only attempt to coerce the right argument if
1003 we know the left is integer. */
1004 /* Left operand is defined, so is it IV? */
1005 SvIV_please(TOPm1s);
1006 if (SvIOK(TOPm1s)) {
1007 bool auvok = SvUOK(TOPm1s);
1008 bool buvok = SvUOK(TOPs);
1009 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1010 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1017 alow = SvUVX(TOPm1s);
1019 const IV aiv = SvIVX(TOPm1s);
1022 auvok = TRUE; /* effectively it's a UV now */
1024 alow = -aiv; /* abs, auvok == false records sign */
1030 const IV biv = SvIVX(TOPs);
1033 buvok = TRUE; /* effectively it's a UV now */
1035 blow = -biv; /* abs, buvok == false records sign */
1039 /* If this does sign extension on unsigned it's time for plan B */
1040 ahigh = alow >> (4 * sizeof (UV));
1042 bhigh = blow >> (4 * sizeof (UV));
1044 if (ahigh && bhigh) {
1045 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1046 which is overflow. Drop to NVs below. */
1047 } else if (!ahigh && !bhigh) {
1048 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1049 so the unsigned multiply cannot overflow. */
1050 UV product = alow * blow;
1051 if (auvok == buvok) {
1052 /* -ve * -ve or +ve * +ve gives a +ve result. */
1056 } else if (product <= (UV)IV_MIN) {
1057 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1058 /* -ve result, which could overflow an IV */
1060 SETi( -(IV)product );
1062 } /* else drop to NVs below. */
1064 /* One operand is large, 1 small */
1067 /* swap the operands */
1069 bhigh = blow; /* bhigh now the temp var for the swap */
1073 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1074 multiplies can't overflow. shift can, add can, -ve can. */
1075 product_middle = ahigh * blow;
1076 if (!(product_middle & topmask)) {
1077 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1079 product_middle <<= (4 * sizeof (UV));
1080 product_low = alow * blow;
1082 /* as for pp_add, UV + something mustn't get smaller.
1083 IIRC ANSI mandates this wrapping *behaviour* for
1084 unsigned whatever the actual representation*/
1085 product_low += product_middle;
1086 if (product_low >= product_middle) {
1087 /* didn't overflow */
1088 if (auvok == buvok) {
1089 /* -ve * -ve or +ve * +ve gives a +ve result. */
1091 SETu( product_low );
1093 } else if (product_low <= (UV)IV_MIN) {
1094 /* 2s complement assumption again */
1095 /* -ve result, which could overflow an IV */
1097 SETi( -(IV)product_low );
1099 } /* else drop to NVs below. */
1101 } /* product_middle too large */
1102 } /* ahigh && bhigh */
1103 } /* SvIOK(TOPm1s) */
1108 SETn( left * right );
1115 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1116 /* Only try to do UV divide first
1117 if ((SLOPPYDIVIDE is true) or
1118 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1120 The assumption is that it is better to use floating point divide
1121 whenever possible, only doing integer divide first if we can't be sure.
1122 If NV_PRESERVES_UV is true then we know at compile time that no UV
1123 can be too large to preserve, so don't need to compile the code to
1124 test the size of UVs. */
1127 # define PERL_TRY_UV_DIVIDE
1128 /* ensure that 20./5. == 4. */
1130 # ifdef PERL_PRESERVE_IVUV
1131 # ifndef NV_PRESERVES_UV
1132 # define PERL_TRY_UV_DIVIDE
1137 #ifdef PERL_TRY_UV_DIVIDE
1140 SvIV_please(TOPm1s);
1141 if (SvIOK(TOPm1s)) {
1142 bool left_non_neg = SvUOK(TOPm1s);
1143 bool right_non_neg = SvUOK(TOPs);
1147 if (right_non_neg) {
1148 right = SvUVX(TOPs);
1151 const IV biv = SvIVX(TOPs);
1154 right_non_neg = TRUE; /* effectively it's a UV now */
1160 /* historically undef()/0 gives a "Use of uninitialized value"
1161 warning before dieing, hence this test goes here.
1162 If it were immediately before the second SvIV_please, then
1163 DIE() would be invoked before left was even inspected, so
1164 no inpsection would give no warning. */
1166 DIE(aTHX_ "Illegal division by zero");
1169 left = SvUVX(TOPm1s);
1172 const IV aiv = SvIVX(TOPm1s);
1175 left_non_neg = TRUE; /* effectively it's a UV now */
1184 /* For sloppy divide we always attempt integer division. */
1186 /* Otherwise we only attempt it if either or both operands
1187 would not be preserved by an NV. If both fit in NVs
1188 we fall through to the NV divide code below. However,
1189 as left >= right to ensure integer result here, we know that
1190 we can skip the test on the right operand - right big
1191 enough not to be preserved can't get here unless left is
1194 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1197 /* Integer division can't overflow, but it can be imprecise. */
1198 const UV result = left / right;
1199 if (result * right == left) {
1200 SP--; /* result is valid */
1201 if (left_non_neg == right_non_neg) {
1202 /* signs identical, result is positive. */
1206 /* 2s complement assumption */
1207 if (result <= (UV)IV_MIN)
1208 SETi( -(IV)result );
1210 /* It's exact but too negative for IV. */
1211 SETn( -(NV)result );
1214 } /* tried integer divide but it was not an integer result */
1215 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1216 } /* left wasn't SvIOK */
1217 } /* right wasn't SvIOK */
1218 #endif /* PERL_TRY_UV_DIVIDE */
1222 DIE(aTHX_ "Illegal division by zero");
1223 PUSHn( left / right );
1230 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1234 bool left_neg = FALSE;
1235 bool right_neg = FALSE;
1236 bool use_double = FALSE;
1237 bool dright_valid = FALSE;
1243 right_neg = !SvUOK(TOPs);
1245 right = SvUVX(POPs);
1247 const IV biv = SvIVX(POPs);
1250 right_neg = FALSE; /* effectively it's a UV now */
1258 right_neg = dright < 0;
1261 if (dright < UV_MAX_P1) {
1262 right = U_V(dright);
1263 dright_valid = TRUE; /* In case we need to use double below. */
1269 /* At this point use_double is only true if right is out of range for
1270 a UV. In range NV has been rounded down to nearest UV and
1271 use_double false. */
1273 if (!use_double && SvIOK(TOPs)) {
1275 left_neg = !SvUOK(TOPs);
1279 const IV aiv = SvIVX(POPs);
1282 left_neg = FALSE; /* effectively it's a UV now */
1291 left_neg = dleft < 0;
1295 /* This should be exactly the 5.6 behaviour - if left and right are
1296 both in range for UV then use U_V() rather than floor. */
1298 if (dleft < UV_MAX_P1) {
1299 /* right was in range, so is dleft, so use UVs not double.
1303 /* left is out of range for UV, right was in range, so promote
1304 right (back) to double. */
1306 /* The +0.5 is used in 5.6 even though it is not strictly
1307 consistent with the implicit +0 floor in the U_V()
1308 inside the #if 1. */
1309 dleft = Perl_floor(dleft + 0.5);
1312 dright = Perl_floor(dright + 0.5);
1322 DIE(aTHX_ "Illegal modulus zero");
1324 dans = Perl_fmod(dleft, dright);
1325 if ((left_neg != right_neg) && dans)
1326 dans = dright - dans;
1329 sv_setnv(TARG, dans);
1335 DIE(aTHX_ "Illegal modulus zero");
1338 if ((left_neg != right_neg) && ans)
1341 /* XXX may warn: unary minus operator applied to unsigned type */
1342 /* could change -foo to be (~foo)+1 instead */
1343 if (ans <= ~((UV)IV_MAX)+1)
1344 sv_setiv(TARG, ~ans+1);
1346 sv_setnv(TARG, -(NV)ans);
1349 sv_setuv(TARG, ans);
1358 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1365 const UV uv = SvUV(sv);
1367 count = IV_MAX; /* The best we can do? */
1371 const IV iv = SvIV(sv);
1378 else if (SvNOKp(sv)) {
1379 const NV nv = SvNV(sv);
1387 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1389 static const char oom_list_extend[] = "Out of memory during list extend";
1390 const I32 items = SP - MARK;
1391 const I32 max = items * count;
1393 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1394 /* Did the max computation overflow? */
1395 if (items > 0 && max > 0 && (max < items || max < count))
1396 Perl_croak(aTHX_ oom_list_extend);
1401 /* This code was intended to fix 20010809.028:
1404 for (($x =~ /./g) x 2) {
1405 print chop; # "abcdabcd" expected as output.
1408 * but that change (#11635) broke this code:
1410 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1412 * I can't think of a better fix that doesn't introduce
1413 * an efficiency hit by copying the SVs. The stack isn't
1414 * refcounted, and mortalisation obviously doesn't
1415 * Do The Right Thing when the stack has more than
1416 * one pointer to the same mortal value.
1420 *SP = sv_2mortal(newSVsv(*SP));
1430 repeatcpy((char*)(MARK + items), (char*)MARK,
1431 items * sizeof(SV*), count - 1);
1434 else if (count <= 0)
1437 else { /* Note: mark already snarfed by pp_list */
1438 SV * const tmpstr = POPs;
1441 static const char oom_string_extend[] =
1442 "Out of memory during string extend";
1444 SvSetSV(TARG, tmpstr);
1445 SvPV_force(TARG, len);
1446 isutf = DO_UTF8(TARG);
1451 STRLEN max = (UV)count * len;
1452 if (len > ((MEM_SIZE)~0)/count)
1453 Perl_croak(aTHX_ oom_string_extend);
1454 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1455 SvGROW(TARG, max + 1);
1456 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1457 SvCUR_set(TARG, SvCUR(TARG) * count);
1459 *SvEND(TARG) = '\0';
1462 (void)SvPOK_only_UTF8(TARG);
1464 (void)SvPOK_only(TARG);
1466 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1467 /* The parser saw this as a list repeat, and there
1468 are probably several items on the stack. But we're
1469 in scalar context, and there's no pp_list to save us
1470 now. So drop the rest of the items -- robin@kitsite.com
1483 dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN);
1484 useleft = USE_LEFT(TOPm1s);
1485 #ifdef PERL_PRESERVE_IVUV
1486 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1487 "bad things" happen if you rely on signed integers wrapping. */
1490 /* Unless the left argument is integer in range we are going to have to
1491 use NV maths. Hence only attempt to coerce the right argument if
1492 we know the left is integer. */
1493 register UV auv = 0;
1499 a_valid = auvok = 1;
1500 /* left operand is undef, treat as zero. */
1502 /* Left operand is defined, so is it IV? */
1503 SvIV_please(TOPm1s);
1504 if (SvIOK(TOPm1s)) {
1505 if ((auvok = SvUOK(TOPm1s)))
1506 auv = SvUVX(TOPm1s);
1508 register const IV aiv = SvIVX(TOPm1s);
1511 auvok = 1; /* Now acting as a sign flag. */
1512 } else { /* 2s complement assumption for IV_MIN */
1520 bool result_good = 0;
1523 bool buvok = SvUOK(TOPs);
1528 register const IV biv = SvIVX(TOPs);
1535 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1536 else "IV" now, independent of how it came in.
1537 if a, b represents positive, A, B negative, a maps to -A etc
1542 all UV maths. negate result if A negative.
1543 subtract if signs same, add if signs differ. */
1545 if (auvok ^ buvok) {
1554 /* Must get smaller */
1559 if (result <= buv) {
1560 /* result really should be -(auv-buv). as its negation
1561 of true value, need to swap our result flag */
1573 if (result <= (UV)IV_MIN)
1574 SETi( -(IV)result );
1576 /* result valid, but out of range for IV. */
1577 SETn( -(NV)result );
1581 } /* Overflow, drop through to NVs. */
1585 useleft = USE_LEFT(TOPm1s);
1589 /* left operand is undef, treat as zero - value */
1593 SETn( TOPn - value );
1600 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1602 const IV shift = POPi;
1603 if (PL_op->op_private & HINT_INTEGER) {
1617 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1619 const IV shift = POPi;
1620 if (PL_op->op_private & HINT_INTEGER) {
1634 dSP; tryAMAGICbinSET(lt,0);
1635 #ifdef PERL_PRESERVE_IVUV
1638 SvIV_please(TOPm1s);
1639 if (SvIOK(TOPm1s)) {
1640 bool auvok = SvUOK(TOPm1s);
1641 bool buvok = SvUOK(TOPs);
1643 if (!auvok && !buvok) { /* ## IV < IV ## */
1644 const IV aiv = SvIVX(TOPm1s);
1645 const IV biv = SvIVX(TOPs);
1648 SETs(boolSV(aiv < biv));
1651 if (auvok && buvok) { /* ## UV < UV ## */
1652 const UV auv = SvUVX(TOPm1s);
1653 const UV buv = SvUVX(TOPs);
1656 SETs(boolSV(auv < buv));
1659 if (auvok) { /* ## UV < IV ## */
1661 const IV biv = SvIVX(TOPs);
1664 /* As (a) is a UV, it's >=0, so it cannot be < */
1669 SETs(boolSV(auv < (UV)biv));
1672 { /* ## IV < UV ## */
1673 const IV aiv = SvIVX(TOPm1s);
1677 /* As (b) is a UV, it's >=0, so it must be < */
1684 SETs(boolSV((UV)aiv < buv));
1690 #ifndef NV_PRESERVES_UV
1691 #ifdef PERL_PRESERVE_IVUV
1694 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1696 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1702 SETs(boolSV(TOPn < value));
1709 dSP; tryAMAGICbinSET(gt,0);
1710 #ifdef PERL_PRESERVE_IVUV
1713 SvIV_please(TOPm1s);
1714 if (SvIOK(TOPm1s)) {
1715 bool auvok = SvUOK(TOPm1s);
1716 bool buvok = SvUOK(TOPs);
1718 if (!auvok && !buvok) { /* ## IV > IV ## */
1719 const IV aiv = SvIVX(TOPm1s);
1720 const IV biv = SvIVX(TOPs);
1723 SETs(boolSV(aiv > biv));
1726 if (auvok && buvok) { /* ## UV > UV ## */
1727 const UV auv = SvUVX(TOPm1s);
1728 const UV buv = SvUVX(TOPs);
1731 SETs(boolSV(auv > buv));
1734 if (auvok) { /* ## UV > IV ## */
1736 const IV biv = SvIVX(TOPs);
1740 /* As (a) is a UV, it's >=0, so it must be > */
1745 SETs(boolSV(auv > (UV)biv));
1748 { /* ## IV > UV ## */
1749 const IV aiv = SvIVX(TOPm1s);
1753 /* As (b) is a UV, it's >=0, so it cannot be > */
1760 SETs(boolSV((UV)aiv > buv));
1766 #ifndef NV_PRESERVES_UV
1767 #ifdef PERL_PRESERVE_IVUV
1770 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1772 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1778 SETs(boolSV(TOPn > value));
1785 dSP; tryAMAGICbinSET(le,0);
1786 #ifdef PERL_PRESERVE_IVUV
1789 SvIV_please(TOPm1s);
1790 if (SvIOK(TOPm1s)) {
1791 bool auvok = SvUOK(TOPm1s);
1792 bool buvok = SvUOK(TOPs);
1794 if (!auvok && !buvok) { /* ## IV <= IV ## */
1795 const IV aiv = SvIVX(TOPm1s);
1796 const IV biv = SvIVX(TOPs);
1799 SETs(boolSV(aiv <= biv));
1802 if (auvok && buvok) { /* ## UV <= UV ## */
1803 UV auv = SvUVX(TOPm1s);
1804 UV buv = SvUVX(TOPs);
1807 SETs(boolSV(auv <= buv));
1810 if (auvok) { /* ## UV <= IV ## */
1812 const IV biv = SvIVX(TOPs);
1816 /* As (a) is a UV, it's >=0, so a cannot be <= */
1821 SETs(boolSV(auv <= (UV)biv));
1824 { /* ## IV <= UV ## */
1825 const IV aiv = SvIVX(TOPm1s);
1829 /* As (b) is a UV, it's >=0, so a must be <= */
1836 SETs(boolSV((UV)aiv <= buv));
1842 #ifndef NV_PRESERVES_UV
1843 #ifdef PERL_PRESERVE_IVUV
1846 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1848 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1854 SETs(boolSV(TOPn <= value));
1861 dSP; tryAMAGICbinSET(ge,0);
1862 #ifdef PERL_PRESERVE_IVUV
1865 SvIV_please(TOPm1s);
1866 if (SvIOK(TOPm1s)) {
1867 bool auvok = SvUOK(TOPm1s);
1868 bool buvok = SvUOK(TOPs);
1870 if (!auvok && !buvok) { /* ## IV >= IV ## */
1871 const IV aiv = SvIVX(TOPm1s);
1872 const IV biv = SvIVX(TOPs);
1875 SETs(boolSV(aiv >= biv));
1878 if (auvok && buvok) { /* ## UV >= UV ## */
1879 const UV auv = SvUVX(TOPm1s);
1880 const UV buv = SvUVX(TOPs);
1883 SETs(boolSV(auv >= buv));
1886 if (auvok) { /* ## UV >= IV ## */
1888 const IV biv = SvIVX(TOPs);
1892 /* As (a) is a UV, it's >=0, so it must be >= */
1897 SETs(boolSV(auv >= (UV)biv));
1900 { /* ## IV >= UV ## */
1901 const IV aiv = SvIVX(TOPm1s);
1905 /* As (b) is a UV, it's >=0, so a cannot be >= */
1912 SETs(boolSV((UV)aiv >= buv));
1918 #ifndef NV_PRESERVES_UV
1919 #ifdef PERL_PRESERVE_IVUV
1922 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1924 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
1930 SETs(boolSV(TOPn >= value));
1937 dSP; tryAMAGICbinSET(ne,0);
1938 #ifndef NV_PRESERVES_UV
1939 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1941 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
1945 #ifdef PERL_PRESERVE_IVUV
1948 SvIV_please(TOPm1s);
1949 if (SvIOK(TOPm1s)) {
1950 const bool auvok = SvUOK(TOPm1s);
1951 const bool buvok = SvUOK(TOPs);
1953 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
1954 /* Casting IV to UV before comparison isn't going to matter
1955 on 2s complement. On 1s complement or sign&magnitude
1956 (if we have any of them) it could make negative zero
1957 differ from normal zero. As I understand it. (Need to
1958 check - is negative zero implementation defined behaviour
1960 const UV buv = SvUVX(POPs);
1961 const UV auv = SvUVX(TOPs);
1963 SETs(boolSV(auv != buv));
1966 { /* ## Mixed IV,UV ## */
1970 /* != is commutative so swap if needed (save code) */
1972 /* swap. top of stack (b) is the iv */
1976 /* As (a) is a UV, it's >0, so it cannot be == */
1985 /* As (b) is a UV, it's >0, so it cannot be == */
1989 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
1991 SETs(boolSV((UV)iv != uv));
1999 SETs(boolSV(TOPn != value));
2006 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2007 #ifndef NV_PRESERVES_UV
2008 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2009 const UV right = PTR2UV(SvRV(POPs));
2010 const UV left = PTR2UV(SvRV(TOPs));
2011 SETi((left > right) - (left < right));
2015 #ifdef PERL_PRESERVE_IVUV
2016 /* Fortunately it seems NaN isn't IOK */
2019 SvIV_please(TOPm1s);
2020 if (SvIOK(TOPm1s)) {
2021 const bool leftuvok = SvUOK(TOPm1s);
2022 const bool rightuvok = SvUOK(TOPs);
2024 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2025 const IV leftiv = SvIVX(TOPm1s);
2026 const IV rightiv = SvIVX(TOPs);
2028 if (leftiv > rightiv)
2030 else if (leftiv < rightiv)
2034 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2035 const UV leftuv = SvUVX(TOPm1s);
2036 const UV rightuv = SvUVX(TOPs);
2038 if (leftuv > rightuv)
2040 else if (leftuv < rightuv)
2044 } else if (leftuvok) { /* ## UV <=> IV ## */
2045 const IV rightiv = SvIVX(TOPs);
2047 /* As (a) is a UV, it's >=0, so it cannot be < */
2050 const UV leftuv = SvUVX(TOPm1s);
2051 if (leftuv > (UV)rightiv) {
2053 } else if (leftuv < (UV)rightiv) {
2059 } else { /* ## IV <=> UV ## */
2060 const IV leftiv = SvIVX(TOPm1s);
2062 /* As (b) is a UV, it's >=0, so it must be < */
2065 const UV rightuv = SvUVX(TOPs);
2066 if ((UV)leftiv > rightuv) {
2068 } else if ((UV)leftiv < rightuv) {
2086 if (Perl_isnan(left) || Perl_isnan(right)) {
2090 value = (left > right) - (left < right);
2094 else if (left < right)
2096 else if (left > right)
2112 int amg_type = sle_amg;
2116 switch (PL_op->op_type) {
2135 tryAMAGICbinSET_var(amg_type,0);
2138 const int cmp = (IN_LOCALE_RUNTIME
2139 ? sv_cmp_locale(left, right)
2140 : sv_cmp(left, right));
2141 SETs(boolSV(cmp * multiplier < rhs));
2148 dSP; tryAMAGICbinSET(seq,0);
2151 SETs(boolSV(sv_eq(left, right)));
2158 dSP; tryAMAGICbinSET(sne,0);
2161 SETs(boolSV(!sv_eq(left, right)));
2168 dSP; dTARGET; tryAMAGICbin(scmp,0);
2171 const int cmp = (IN_LOCALE_RUNTIME
2172 ? sv_cmp_locale(left, right)
2173 : sv_cmp(left, right));
2181 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2186 if (SvNIOKp(left) || SvNIOKp(right)) {
2187 if (PL_op->op_private & HINT_INTEGER) {
2188 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2192 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2197 do_vop(PL_op->op_type, TARG, left, right);
2206 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
2211 if (SvNIOKp(left) || SvNIOKp(right)) {
2212 if (PL_op->op_private & HINT_INTEGER) {
2213 const IV i = (USE_LEFT(left) ? SvIV_nomg(left) : 0) ^ SvIV_nomg(right);
2217 const UV u = (USE_LEFT(left) ? SvUV_nomg(left) : 0) ^ SvUV_nomg(right);
2222 do_vop(PL_op->op_type, TARG, left, right);
2231 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
2236 if (SvNIOKp(left) || SvNIOKp(right)) {
2237 if (PL_op->op_private & HINT_INTEGER) {
2238 const IV i = (USE_LEFT(left) ? SvIV_nomg(left) : 0) | SvIV_nomg(right);
2242 const UV u = (USE_LEFT(left) ? SvUV_nomg(left) : 0) | SvUV_nomg(right);
2247 do_vop(PL_op->op_type, TARG, left, right);
2256 dSP; dTARGET; tryAMAGICun(neg);
2259 const int flags = SvFLAGS(sv);
2261 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2262 /* It's publicly an integer, or privately an integer-not-float */
2265 if (SvIVX(sv) == IV_MIN) {
2266 /* 2s complement assumption. */
2267 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2270 else if (SvUVX(sv) <= IV_MAX) {
2275 else if (SvIVX(sv) != IV_MIN) {
2279 #ifdef PERL_PRESERVE_IVUV
2288 else if (SvPOKp(sv)) {
2290 const char *s = SvPV_const(sv, len);
2291 if (isIDFIRST(*s)) {
2292 sv_setpvn(TARG, "-", 1);
2295 else if (*s == '+' || *s == '-') {
2297 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2299 else if (DO_UTF8(sv)) {
2302 goto oops_its_an_int;
2304 sv_setnv(TARG, -SvNV(sv));
2306 sv_setpvn(TARG, "-", 1);
2313 goto oops_its_an_int;
2314 sv_setnv(TARG, -SvNV(sv));
2326 dSP; tryAMAGICunSET(not);
2327 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2333 dSP; dTARGET; tryAMAGICun(compl);
2338 if (PL_op->op_private & HINT_INTEGER) {
2339 const IV i = ~SvIV_nomg(sv);
2343 const UV u = ~SvUV_nomg(sv);
2352 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2353 sv_setsv_nomg(TARG, sv);
2354 tmps = (U8*)SvPV_force(TARG, len);
2357 /* Calculate exact length, let's not estimate. */
2366 while (tmps < send) {
2367 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2368 tmps += UTF8SKIP(tmps);
2369 targlen += UNISKIP(~c);
2375 /* Now rewind strings and write them. */
2379 Newxz(result, targlen + 1, U8);
2380 while (tmps < send) {
2381 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2382 tmps += UTF8SKIP(tmps);
2383 result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY);
2387 sv_setpvn(TARG, (char*)result, targlen);
2391 Newxz(result, nchar + 1, U8);
2392 while (tmps < send) {
2393 const U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY);
2394 tmps += UTF8SKIP(tmps);
2399 sv_setpvn(TARG, (char*)result, nchar);
2408 register long *tmpl;
2409 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2412 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
2417 for ( ; anum > 0; anum--, tmps++)
2426 /* integer versions of some of the above */
2430 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2433 SETi( left * right );
2440 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2444 DIE(aTHX_ "Illegal division by zero");
2445 value = POPi / value;
2454 /* This is the vanilla old i_modulo. */
2455 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2459 DIE(aTHX_ "Illegal modulus zero");
2460 SETi( left % right );
2465 #if defined(__GLIBC__) && IVSIZE == 8
2469 /* This is the i_modulo with the workaround for the _moddi3 bug
2470 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2471 * See below for pp_i_modulo. */
2472 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2476 DIE(aTHX_ "Illegal modulus zero");
2477 SETi( left % PERL_ABS(right) );
2485 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2489 DIE(aTHX_ "Illegal modulus zero");
2490 /* The assumption is to use hereafter the old vanilla version... */
2492 PL_ppaddr[OP_I_MODULO] =
2494 /* .. but if we have glibc, we might have a buggy _moddi3
2495 * (at least glicb 2.2.5 is known to have this bug), in other
2496 * words our integer modulus with negative quad as the second
2497 * argument might be broken. Test for this and re-patch the
2498 * opcode dispatch table if that is the case, remembering to
2499 * also apply the workaround so that this first round works
2500 * right, too. See [perl #9402] for more information. */
2501 #if defined(__GLIBC__) && IVSIZE == 8
2505 /* Cannot do this check with inlined IV constants since
2506 * that seems to work correctly even with the buggy glibc. */
2508 /* Yikes, we have the bug.
2509 * Patch in the workaround version. */
2511 PL_ppaddr[OP_I_MODULO] =
2512 &Perl_pp_i_modulo_1;
2513 /* Make certain we work right this time, too. */
2514 right = PERL_ABS(right);
2518 SETi( left % right );
2525 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2528 SETi( left + right );
2535 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2538 SETi( left - right );
2545 dSP; tryAMAGICbinSET(lt,0);
2548 SETs(boolSV(left < right));
2555 dSP; tryAMAGICbinSET(gt,0);
2558 SETs(boolSV(left > right));
2565 dSP; tryAMAGICbinSET(le,0);
2568 SETs(boolSV(left <= right));
2575 dSP; tryAMAGICbinSET(ge,0);
2578 SETs(boolSV(left >= right));
2585 dSP; tryAMAGICbinSET(eq,0);
2588 SETs(boolSV(left == right));
2595 dSP; tryAMAGICbinSET(ne,0);
2598 SETs(boolSV(left != right));
2605 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2612 else if (left < right)
2623 dSP; dTARGET; tryAMAGICun(neg);
2628 /* High falutin' math. */
2632 dSP; dTARGET; tryAMAGICbin(atan2,0);
2635 SETn(Perl_atan2(left, right));
2642 dSP; dTARGET; tryAMAGICun(sin);
2644 const NV value = POPn;
2645 XPUSHn(Perl_sin(value));
2652 dSP; dTARGET; tryAMAGICun(cos);
2654 const NV value = POPn;
2655 XPUSHn(Perl_cos(value));
2660 /* Support Configure command-line overrides for rand() functions.
2661 After 5.005, perhaps we should replace this by Configure support
2662 for drand48(), random(), or rand(). For 5.005, though, maintain
2663 compatibility by calling rand() but allow the user to override it.
2664 See INSTALL for details. --Andy Dougherty 15 July 1998
2666 /* Now it's after 5.005, and Configure supports drand48() and random(),
2667 in addition to rand(). So the overrides should not be needed any more.
2668 --Jarkko Hietaniemi 27 September 1998
2671 #ifndef HAS_DRAND48_PROTO
2672 extern double drand48 (void);
2685 if (!PL_srand_called) {
2686 (void)seedDrand01((Rand_seed_t)seed());
2687 PL_srand_called = TRUE;
2697 const UV anum = (MAXARG < 1) ? seed() : POPu;
2698 (void)seedDrand01((Rand_seed_t)anum);
2699 PL_srand_called = TRUE;
2706 dSP; dTARGET; tryAMAGICun(exp);
2710 value = Perl_exp(value);
2718 dSP; dTARGET; tryAMAGICun(log);
2720 const NV value = POPn;
2722 SET_NUMERIC_STANDARD();
2723 DIE(aTHX_ "Can't take log of %"NVgf, value);
2725 XPUSHn(Perl_log(value));
2732 dSP; dTARGET; tryAMAGICun(sqrt);
2734 const NV value = POPn;
2736 SET_NUMERIC_STANDARD();
2737 DIE(aTHX_ "Can't take sqrt of %"NVgf, value);
2739 XPUSHn(Perl_sqrt(value));
2746 dSP; dTARGET; tryAMAGICun(int);
2748 const IV iv = TOPi; /* attempt to convert to IV if possible. */
2749 /* XXX it's arguable that compiler casting to IV might be subtly
2750 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2751 else preferring IV has introduced a subtle behaviour change bug. OTOH
2752 relying on floating point to be accurate is a bug. */
2756 else if (SvIOK(TOPs)) {
2763 const NV value = TOPn;
2765 if (value < (NV)UV_MAX + 0.5) {
2768 SETn(Perl_floor(value));
2772 if (value > (NV)IV_MIN - 0.5) {
2775 SETn(Perl_ceil(value));
2785 dSP; dTARGET; tryAMAGICun(abs);
2787 /* This will cache the NV value if string isn't actually integer */
2792 else if (SvIOK(TOPs)) {
2793 /* IVX is precise */
2795 SETu(TOPu); /* force it to be numeric only */
2803 /* 2s complement assumption. Also, not really needed as
2804 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2810 const NV value = TOPn;
2825 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2829 SV* const sv = POPs;
2831 tmps = (SvPV_const(sv, len));
2833 /* If Unicode, try to downgrade
2834 * If not possible, croak. */
2835 SV* const tsv = sv_2mortal(newSVsv(sv));
2838 sv_utf8_downgrade(tsv, FALSE);
2839 tmps = SvPV_const(tsv, len);
2841 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2842 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2855 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2859 SV* const sv = POPs;
2861 tmps = (SvPV_const(sv, len));
2863 /* If Unicode, try to downgrade
2864 * If not possible, croak. */
2865 SV* const tsv = sv_2mortal(newSVsv(sv));
2868 sv_utf8_downgrade(tsv, FALSE);
2869 tmps = SvPV_const(tsv, len);
2871 while (*tmps && len && isSPACE(*tmps))
2876 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2877 else if (*tmps == 'b')
2878 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2880 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2882 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2896 SV * const sv = TOPs;
2899 SETi(sv_len_utf8(sv));
2915 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2917 const I32 arybase = PL_curcop->cop_arybase;
2919 const char *repl = 0;
2921 const int num_args = PL_op->op_private & 7;
2922 bool repl_need_utf8_upgrade = FALSE;
2923 bool repl_is_utf8 = FALSE;
2925 SvTAINTED_off(TARG); /* decontaminate */
2926 SvUTF8_off(TARG); /* decontaminate */
2930 repl = SvPV_const(repl_sv, repl_len);
2931 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
2941 sv_utf8_upgrade(sv);
2943 else if (DO_UTF8(sv))
2944 repl_need_utf8_upgrade = TRUE;
2946 tmps = SvPV_const(sv, curlen);
2948 utf8_curlen = sv_len_utf8(sv);
2949 if (utf8_curlen == curlen)
2952 curlen = utf8_curlen;
2957 if (pos >= arybase) {
2975 else if (len >= 0) {
2977 if (rem > (I32)curlen)
2992 Perl_croak(aTHX_ "substr outside of string");
2993 if (ckWARN(WARN_SUBSTR))
2994 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
2998 const I32 upos = pos;
2999 const I32 urem = rem;
3001 sv_pos_u2b(sv, &pos, &rem);
3003 /* we either return a PV or an LV. If the TARG hasn't been used
3004 * before, or is of that type, reuse it; otherwise use a mortal
3005 * instead. Note that LVs can have an extended lifetime, so also
3006 * dont reuse if refcount > 1 (bug #20933) */
3007 if (SvTYPE(TARG) > SVt_NULL) {
3008 if ( (SvTYPE(TARG) == SVt_PVLV)
3009 ? (!lvalue || SvREFCNT(TARG) > 1)
3012 TARG = sv_newmortal();
3016 sv_setpvn(TARG, tmps, rem);
3017 #ifdef USE_LOCALE_COLLATE
3018 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3023 SV* repl_sv_copy = NULL;
3025 if (repl_need_utf8_upgrade) {
3026 repl_sv_copy = newSVsv(repl_sv);
3027 sv_utf8_upgrade(repl_sv_copy);
3028 repl = SvPV_const(repl_sv_copy, repl_len);
3029 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3031 sv_insert(sv, pos, rem, repl, repl_len);
3035 SvREFCNT_dec(repl_sv_copy);
3037 else if (lvalue) { /* it's an lvalue! */
3038 if (!SvGMAGICAL(sv)) {
3040 SvPV_force_nolen(sv);
3041 if (ckWARN(WARN_SUBSTR))
3042 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3043 "Attempt to use reference as lvalue in substr");
3045 if (SvOK(sv)) /* is it defined ? */
3046 (void)SvPOK_only_UTF8(sv);
3048 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3051 if (SvTYPE(TARG) < SVt_PVLV) {
3052 sv_upgrade(TARG, SVt_PVLV);
3053 sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0);
3059 if (LvTARG(TARG) != sv) {
3061 SvREFCNT_dec(LvTARG(TARG));
3062 LvTARG(TARG) = SvREFCNT_inc(sv);
3064 LvTARGOFF(TARG) = upos;
3065 LvTARGLEN(TARG) = urem;
3069 PUSHs(TARG); /* avoid SvSETMAGIC here */
3076 register const IV size = POPi;
3077 register const IV offset = POPi;
3078 register SV * const src = POPs;
3079 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3081 SvTAINTED_off(TARG); /* decontaminate */
3082 if (lvalue) { /* it's an lvalue! */
3083 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3084 TARG = sv_newmortal();
3085 if (SvTYPE(TARG) < SVt_PVLV) {
3086 sv_upgrade(TARG, SVt_PVLV);
3087 sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0);
3090 if (LvTARG(TARG) != src) {
3092 SvREFCNT_dec(LvTARG(TARG));
3093 LvTARG(TARG) = SvREFCNT_inc(src);
3095 LvTARGOFF(TARG) = offset;
3096 LvTARGLEN(TARG) = size;
3099 sv_setuv(TARG, do_vecget(src, offset, size));
3115 const I32 arybase = PL_curcop->cop_arybase;
3122 offset = POPi - arybase;
3125 big_utf8 = DO_UTF8(big);
3126 little_utf8 = DO_UTF8(little);
3127 if (big_utf8 ^ little_utf8) {
3128 /* One needs to be upgraded. */
3129 SV * const bytes = little_utf8 ? big : little;
3131 const char * const p = SvPV_const(bytes, len);
3133 temp = newSVpvn(p, len);
3136 sv_recode_to_utf8(temp, PL_encoding);
3138 sv_utf8_upgrade(temp);
3147 if (big_utf8 && offset > 0)
3148 sv_pos_u2b(big, &offset, 0);
3149 tmps = SvPV_const(big, biglen);
3152 else if (offset > (I32)biglen)
3154 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
3155 (unsigned char*)tmps + biglen, little, 0)))
3158 retval = tmps2 - tmps;
3159 if (retval > 0 && big_utf8)
3160 sv_pos_b2u(big, &retval);
3163 PUSHi(retval + arybase);
3179 const I32 arybase = PL_curcop->cop_arybase;
3187 big_utf8 = DO_UTF8(big);
3188 little_utf8 = DO_UTF8(little);
3189 if (big_utf8 ^ little_utf8) {
3190 /* One needs to be upgraded. */
3191 SV * const bytes = little_utf8 ? big : little;
3193 const char *p = SvPV_const(bytes, len);
3195 temp = newSVpvn(p, len);
3198 sv_recode_to_utf8(temp, PL_encoding);
3200 sv_utf8_upgrade(temp);
3209 tmps2 = SvPV_const(little, llen);
3210 tmps = SvPV_const(big, blen);
3215 if (offset > 0 && big_utf8)
3216 sv_pos_u2b(big, &offset, 0);
3217 offset = offset - arybase + llen;
3221 else if (offset > (I32)blen)
3223 if (!(tmps2 = rninstr(tmps, tmps + offset,
3224 tmps2, tmps2 + llen)))
3227 retval = tmps2 - tmps;
3228 if (retval > 0 && big_utf8)
3229 sv_pos_b2u(big, &retval);
3232 PUSHi(retval + arybase);
3238 dSP; dMARK; dORIGMARK; dTARGET;
3239 do_sprintf(TARG, SP-MARK, MARK+1);
3240 TAINT_IF(SvTAINTED(TARG));
3251 const U8 *s = (U8*)SvPV_const(argsv, len);
3254 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3255 tmpsv = sv_2mortal(newSVsv(argsv));
3256 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3260 XPUSHu(DO_UTF8(argsv) ?
3261 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3273 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3275 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3277 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3279 (void) POPs; /* Ignore the argument value. */
3280 value = UNICODE_REPLACEMENT;
3286 SvUPGRADE(TARG,SVt_PV);
3288 if (value > 255 && !IN_BYTES) {
3289 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3290 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3291 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3293 (void)SvPOK_only(TARG);
3302 *tmps++ = (char)value;
3304 (void)SvPOK_only(TARG);
3305 if (PL_encoding && !IN_BYTES) {
3306 sv_recode_to_utf8(TARG, PL_encoding);
3308 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3309 memEQ(tmps, "\xef\xbf\xbd\0", 4)) {
3313 *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value);
3314 *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value);
3329 const char *tmps = SvPV_const(left, len);
3331 if (DO_UTF8(left)) {
3332 /* If Unicode, try to downgrade.
3333 * If not possible, croak.
3334 * Yes, we made this up. */
3335 SV* const tsv = sv_2mortal(newSVsv(left));
3338 sv_utf8_downgrade(tsv, FALSE);
3339 tmps = SvPV_const(tsv, len);
3341 # ifdef USE_ITHREADS
3343 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3344 /* This should be threadsafe because in ithreads there is only
3345 * one thread per interpreter. If this would not be true,
3346 * we would need a mutex to protect this malloc. */
3347 PL_reentrant_buffer->_crypt_struct_buffer =
3348 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3349 #if defined(__GLIBC__) || defined(__EMX__)
3350 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3351 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3352 /* work around glibc-2.2.5 bug */
3353 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3357 # endif /* HAS_CRYPT_R */
3358 # endif /* USE_ITHREADS */
3360 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3362 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3368 "The crypt() function is unimplemented due to excessive paranoia.");
3378 const int op_type = PL_op->op_type;
3382 (s = (const U8*)SvPV_nomg_const(sv, slen)) && slen &&
3383 UTF8_IS_START(*s)) {
3384 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3388 utf8_to_uvchr(s, &ulen);
3389 if (op_type == OP_UCFIRST) {
3390 toTITLE_utf8(s, tmpbuf, &tculen);
3392 toLOWER_utf8(s, tmpbuf, &tculen);
3395 if (!SvPADTMP(sv) || SvREADONLY(sv) || ulen != tculen) {
3397 /* slen is the byte length of the whole SV.
3398 * ulen is the byte length of the original Unicode character
3399 * stored as UTF-8 at s.
3400 * tculen is the byte length of the freshly titlecased (or
3401 * lowercased) Unicode character stored as UTF-8 at tmpbuf.
3402 * We first set the result to be the titlecased (/lowercased)
3403 * character, and then append the rest of the SV data. */
3404 sv_setpvn(TARG, (char*)tmpbuf, tculen);
3406 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3411 s = (U8*)SvPV_force_nomg(sv, slen);
3412 Copy(tmpbuf, s, tculen, U8);
3417 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3419 SvUTF8_off(TARG); /* decontaminate */
3420 sv_setsv_nomg(TARG, sv);
3424 s1 = (U8*)SvPV_force_nomg(sv, slen);
3426 if (IN_LOCALE_RUNTIME) {
3429 *s1 = (op_type == OP_UCFIRST)
3430 ? toUPPER_LC(*s1) : toLOWER_LC(*s1);
3433 *s1 = (op_type == OP_UCFIRST) ? toUPPER(*s1) : toLOWER(*s1);
3453 U8 tmpbuf[UTF8_MAXBYTES+1];
3455 s = (const U8*)SvPV_nomg_const(sv,len);
3457 SvUTF8_off(TARG); /* decontaminate */
3458 sv_setpvn(TARG, "", 0);
3462 STRLEN min = len + 1;
3464 SvUPGRADE(TARG, SVt_PV);
3466 (void)SvPOK_only(TARG);
3467 d = (U8*)SvPVX(TARG);
3470 STRLEN u = UTF8SKIP(s);
3472 toUPPER_utf8(s, tmpbuf, &ulen);
3473 if (ulen > u && (SvLEN(TARG) < (min += ulen - u))) {
3474 /* If the eventually required minimum size outgrows
3475 * the available space, we need to grow. */
3476 const UV o = d - (U8*)SvPVX_const(TARG);
3478 /* If someone uppercases one million U+03B0s we
3479 * SvGROW() one million times. Or we could try
3480 * guessing how much to allocate without allocating
3481 * too much. Such is life. */
3483 d = (U8*)SvPVX(TARG) + o;
3485 Copy(tmpbuf, d, ulen, U8);
3491 SvCUR_set(TARG, d - (U8*)SvPVX_const(TARG));
3497 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3499 SvUTF8_off(TARG); /* decontaminate */
3500 sv_setsv_nomg(TARG, sv);
3504 s = (U8*)SvPV_force_nomg(sv, len);
3506 register const U8 *send = s + len;
3508 if (IN_LOCALE_RUNTIME) {
3511 for (; s < send; s++)
3512 *s = toUPPER_LC(*s);
3515 for (; s < send; s++)
3537 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3539 s = (const U8*)SvPV_nomg_const(sv,len);
3541 SvUTF8_off(TARG); /* decontaminate */
3542 sv_setpvn(TARG, "", 0);
3546 STRLEN min = len + 1;
3548 SvUPGRADE(TARG, SVt_PV);
3550 (void)SvPOK_only(TARG);
3551 d = (U8*)SvPVX(TARG);
3554 const STRLEN u = UTF8SKIP(s);
3555 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3557 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode U+03A3 */
3558 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3560 * Now if the sigma is NOT followed by
3561 * /$ignorable_sequence$cased_letter/;
3562 * and it IS preceded by
3563 * /$cased_letter$ignorable_sequence/;
3564 * where $ignorable_sequence is
3565 * [\x{2010}\x{AD}\p{Mn}]*
3566 * and $cased_letter is
3567 * [\p{Ll}\p{Lo}\p{Lt}]
3568 * then it should be mapped to 0x03C2,
3569 * (GREEK SMALL LETTER FINAL SIGMA),
3570 * instead of staying 0x03A3.
3571 * "should be": in other words,
3572 * this is not implemented yet.
3573 * See lib/unicore/SpecialCasing.txt.
3576 if (ulen > u && (SvLEN(TARG) < (min += ulen - u))) {
3577 /* If the eventually required minimum size outgrows
3578 * the available space, we need to grow. */
3579 const UV o = d - (U8*)SvPVX_const(TARG);
3581 /* If someone lowercases one million U+0130s we
3582 * SvGROW() one million times. Or we could try
3583 * guessing how much to allocate without allocating.
3584 * too much. Such is life. */
3586 d = (U8*)SvPVX(TARG) + o;
3588 Copy(tmpbuf, d, ulen, U8);
3594 SvCUR_set(TARG, d - (U8*)SvPVX_const(TARG));
3600 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3602 SvUTF8_off(TARG); /* decontaminate */
3603 sv_setsv_nomg(TARG, sv);
3608 s = (U8*)SvPV_force_nomg(sv, len);
3610 register const U8 * const send = s + len;
3612 if (IN_LOCALE_RUNTIME) {
3615 for (; s < send; s++)
3616 *s = toLOWER_LC(*s);
3619 for (; s < send; s++)
3631 SV * const sv = TOPs;
3633 register const char *s = SvPV_const(sv,len);
3635 SvUTF8_off(TARG); /* decontaminate */
3638 SvUPGRADE(TARG, SVt_PV);
3639 SvGROW(TARG, (len * 2) + 1);
3643 if (UTF8_IS_CONTINUED(*s)) {
3644 STRLEN ulen = UTF8SKIP(s);
3668 SvCUR_set(TARG, d - SvPVX_const(TARG));
3669 (void)SvPOK_only_UTF8(TARG);
3672 sv_setpvn(TARG, s, len);
3674 if (SvSMAGICAL(TARG))
3683 dSP; dMARK; dORIGMARK;
3684 register AV* const av = (AV*)POPs;
3685 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3687 if (SvTYPE(av) == SVt_PVAV) {
3688 const I32 arybase = PL_curcop->cop_arybase;
3689 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3692 for (svp = MARK + 1; svp <= SP; svp++) {
3693 const I32 elem = SvIVx(*svp);
3697 if (max > AvMAX(av))
3700 while (++MARK <= SP) {
3702 I32 elem = SvIVx(*MARK);
3706 svp = av_fetch(av, elem, lval);
3708 if (!svp || *svp == &PL_sv_undef)
3709 DIE(aTHX_ PL_no_aelem, elem);
3710 if (PL_op->op_private & OPpLVAL_INTRO)
3711 save_aelem(av, elem, svp);
3713 *MARK = svp ? *svp : &PL_sv_undef;
3716 if (GIMME != G_ARRAY) {
3718 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
3724 /* Associative arrays. */
3729 HV * const hash = (HV*)POPs;
3731 const I32 gimme = GIMME_V;
3734 /* might clobber stack_sp */
3735 entry = hv_iternext(hash);
3740 SV* const sv = hv_iterkeysv(entry);
3741 PUSHs(sv); /* won't clobber stack_sp */
3742 if (gimme == G_ARRAY) {
3745 /* might clobber stack_sp */
3746 val = hv_iterval(hash, entry);
3751 else if (gimme == G_SCALAR)
3760 const I32 gimme = GIMME_V;
3761 const I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3763 if (PL_op->op_private & OPpSLICE) {
3765 HV * const hv = (HV*)POPs;
3766 const U32 hvtype = SvTYPE(hv);
3767 if (hvtype == SVt_PVHV) { /* hash element */
3768 while (++MARK <= SP) {
3769 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
3770 *MARK = sv ? sv : &PL_sv_undef;
3773 else if (hvtype == SVt_PVAV) { /* array element */
3774 if (PL_op->op_flags & OPf_SPECIAL) {
3775 while (++MARK <= SP) {
3776 SV * const sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3777 *MARK = sv ? sv : &PL_sv_undef;
3782 DIE(aTHX_ "Not a HASH reference");
3785 else if (gimme == G_SCALAR) {
3790 *++MARK = &PL_sv_undef;
3796 HV * const hv = (HV*)POPs;
3798 if (SvTYPE(hv) == SVt_PVHV)
3799 sv = hv_delete_ent(hv, keysv, discard, 0);
3800 else if (SvTYPE(hv) == SVt_PVAV) {
3801 if (PL_op->op_flags & OPf_SPECIAL)
3802 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3804 DIE(aTHX_ "panic: avhv_delete no longer supported");
3807 DIE(aTHX_ "Not a HASH reference");
3822 if (PL_op->op_private & OPpEXISTS_SUB) {
3824 SV * const sv = POPs;
3825 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
3828 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3834 if (SvTYPE(hv) == SVt_PVHV) {
3835 if (hv_exists_ent(hv, tmpsv, 0))
3838 else if (SvTYPE(hv) == SVt_PVAV) {
3839 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3840 if (av_exists((AV*)hv, SvIV(tmpsv)))
3845 DIE(aTHX_ "Not a HASH reference");
3852 dSP; dMARK; dORIGMARK;
3853 register HV * const hv = (HV*)POPs;
3854 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3855 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
3856 bool other_magic = FALSE;
3862 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
3863 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
3864 /* Try to preserve the existenceness of a tied hash
3865 * element by using EXISTS and DELETE if possible.
3866 * Fallback to FETCH and STORE otherwise */
3867 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
3868 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
3869 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
3872 while (++MARK <= SP) {
3873 SV * const keysv = *MARK;
3876 bool preeminent = FALSE;
3879 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
3880 hv_exists_ent(hv, keysv, 0);
3883 he = hv_fetch_ent(hv, keysv, lval, 0);
3884 svp = he ? &HeVAL(he) : 0;
3887 if (!svp || *svp == &PL_sv_undef) {
3888 DIE(aTHX_ PL_no_helem_sv, keysv);
3892 save_helem(hv, keysv, svp);
3895 const char *key = SvPV_const(keysv, keylen);
3896 SAVEDELETE(hv, savepvn(key,keylen), keylen);
3900 *MARK = svp ? *svp : &PL_sv_undef;
3902 if (GIMME != G_ARRAY) {
3904 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
3910 /* List operators. */
3915 if (GIMME != G_ARRAY) {
3917 *MARK = *SP; /* unwanted list, return last item */
3919 *MARK = &PL_sv_undef;
3928 SV ** const lastrelem = PL_stack_sp;
3929 SV ** const lastlelem = PL_stack_base + POPMARK;
3930 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
3931 register SV ** const firstrelem = lastlelem + 1;
3932 const I32 arybase = PL_curcop->cop_arybase;
3933 I32 is_something_there = PL_op->op_flags & OPf_MOD;
3935 register const I32 max = lastrelem - lastlelem;
3936 register SV **lelem;
3938 if (GIMME != G_ARRAY) {
3939 I32 ix = SvIVx(*lastlelem);
3944 if (ix < 0 || ix >= max)
3945 *firstlelem = &PL_sv_undef;
3947 *firstlelem = firstrelem[ix];
3953 SP = firstlelem - 1;
3957 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
3958 I32 ix = SvIVx(*lelem);
3963 if (ix < 0 || ix >= max)
3964 *lelem = &PL_sv_undef;
3966 is_something_there = TRUE;
3967 if (!(*lelem = firstrelem[ix]))
3968 *lelem = &PL_sv_undef;
3971 if (is_something_there)
3974 SP = firstlelem - 1;
3980 dSP; dMARK; dORIGMARK;
3981 const I32 items = SP - MARK;
3982 SV * const av = sv_2mortal((SV*)av_make(items, MARK+1));
3983 SP = ORIGMARK; /* av_make() might realloc stack_sp */
3990 dSP; dMARK; dORIGMARK;
3991 HV* const hv = (HV*)sv_2mortal((SV*)newHV());
3994 SV * const key = *++MARK;
3995 SV * const val = NEWSV(46, 0);
3997 sv_setsv(val, *++MARK);
3998 else if (ckWARN(WARN_MISC))
3999 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4000 (void)hv_store_ent(hv,key,val,0);
4009 dVAR; dSP; dMARK; dORIGMARK;
4010 register AV *ary = (AV*)*++MARK;
4014 register I32 offset;
4015 register I32 length;
4020 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4023 *MARK-- = SvTIED_obj((SV*)ary, mg);
4027 call_method("SPLICE",GIMME_V);
4036 offset = i = SvIVx(*MARK);
4038 offset += AvFILLp(ary) + 1;
4040 offset -= PL_curcop->cop_arybase;
4042 DIE(aTHX_ PL_no_aelem, i);
4044 length = SvIVx(*MARK++);
4046 length += AvFILLp(ary) - offset + 1;
4052 length = AvMAX(ary) + 1; /* close enough to infinity */
4056 length = AvMAX(ary) + 1;
4058 if (offset > AvFILLp(ary) + 1) {
4059 if (ckWARN(WARN_MISC))
4060 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4061 offset = AvFILLp(ary) + 1;
4063 after = AvFILLp(ary) + 1 - (offset + length);
4064 if (after < 0) { /* not that much array */
4065 length += after; /* offset+length now in array */
4071 /* At this point, MARK .. SP-1 is our new LIST */
4074 diff = newlen - length;
4075 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4078 /* make new elements SVs now: avoid problems if they're from the array */
4079 for (dst = MARK, i = newlen; i; i--) {
4080 SV * const h = *dst;
4081 *dst++ = newSVsv(h);
4084 if (diff < 0) { /* shrinking the area */
4086 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4087 Copy(MARK, tmparyval, newlen, SV*);
4090 MARK = ORIGMARK + 1;
4091 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4092 MEXTEND(MARK, length);
4093 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4095 EXTEND_MORTAL(length);
4096 for (i = length, dst = MARK; i; i--) {
4097 sv_2mortal(*dst); /* free them eventualy */
4104 *MARK = AvARRAY(ary)[offset+length-1];
4107 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4108 SvREFCNT_dec(*dst++); /* free them now */
4111 AvFILLp(ary) += diff;
4113 /* pull up or down? */
4115 if (offset < after) { /* easier to pull up */
4116 if (offset) { /* esp. if nothing to pull */
4117 src = &AvARRAY(ary)[offset-1];
4118 dst = src - diff; /* diff is negative */
4119 for (i = offset; i > 0; i--) /* can't trust Copy */
4123 SvPV_set(ary, (char*)(AvARRAY(ary) - diff)); /* diff is negative */
4127 if (after) { /* anything to pull down? */
4128 src = AvARRAY(ary) + offset + length;
4129 dst = src + diff; /* diff is negative */
4130 Move(src, dst, after, SV*);
4132 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4133 /* avoid later double free */
4137 dst[--i] = &PL_sv_undef;
4140 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4141 Safefree(tmparyval);
4144 else { /* no, expanding (or same) */
4146 Newx(tmparyval, length, SV*); /* so remember deletion */
4147 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4150 if (diff > 0) { /* expanding */
4152 /* push up or down? */
4154 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4158 Move(src, dst, offset, SV*);
4160 SvPV_set(ary, (char*)(AvARRAY(ary) - diff));/* diff is positive */
4162 AvFILLp(ary) += diff;
4165 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4166 av_extend(ary, AvFILLp(ary) + diff);
4167 AvFILLp(ary) += diff;
4170 dst = AvARRAY(ary) + AvFILLp(ary);
4172 for (i = after; i; i--) {
4180 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4183 MARK = ORIGMARK + 1;
4184 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4186 Copy(tmparyval, MARK, length, SV*);
4188 EXTEND_MORTAL(length);
4189 for (i = length, dst = MARK; i; i--) {
4190 sv_2mortal(*dst); /* free them eventualy */
4194 Safefree(tmparyval);
4198 else if (length--) {
4199 *MARK = tmparyval[length];
4202 while (length-- > 0)
4203 SvREFCNT_dec(tmparyval[length]);
4205 Safefree(tmparyval);
4208 *MARK = &PL_sv_undef;
4216 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4217 register AV *ary = (AV*)*++MARK;
4218 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4221 *MARK-- = SvTIED_obj((SV*)ary, mg);
4225 call_method("PUSH",G_SCALAR|G_DISCARD);
4229 PUSHi( AvFILL(ary) + 1 );
4232 for (++MARK; MARK <= SP; MARK++) {
4233 SV * const sv = NEWSV(51, 0);
4235 sv_setsv(sv, *MARK);
4236 av_store(ary, AvFILLp(ary)+1, sv);
4239 PUSHi( AvFILLp(ary) + 1 );
4247 AV * const av = (AV*)POPs;
4248 SV * const sv = av_pop(av);
4250 (void)sv_2mortal(sv);
4258 AV * const av = (AV*)POPs;
4259 SV * const sv = av_shift(av);
4264 (void)sv_2mortal(sv);
4271 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4272 register AV *ary = (AV*)*++MARK;
4273 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4276 *MARK-- = SvTIED_obj((SV*)ary, mg);
4280 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4286 av_unshift(ary, SP - MARK);
4288 SV * const sv = newSVsv(*++MARK);
4289 (void)av_store(ary, i++, sv);
4293 PUSHi( AvFILL(ary) + 1 );
4300 SV ** const oldsp = SP;
4302 if (GIMME == G_ARRAY) {
4305 register SV * const tmp = *MARK;
4309 /* safe as long as stack cannot get extended in the above */
4314 register char *down;
4320 SvUTF8_off(TARG); /* decontaminate */
4322 do_join(TARG, &PL_sv_no, MARK, SP);
4324 sv_setsv(TARG, (SP > MARK)
4326 : (padoff_du = find_rundefsvoffset(),
4327 (padoff_du == NOT_IN_PAD || PAD_COMPNAME_FLAGS(padoff_du) & SVpad_OUR)
4328 ? DEFSV : PAD_SVl(padoff_du)));
4329 up = SvPV_force(TARG, len);
4331 if (DO_UTF8(TARG)) { /* first reverse each character */
4332 U8* s = (U8*)SvPVX(TARG);
4333 const U8* send = (U8*)(s + len);
4335 if (UTF8_IS_INVARIANT(*s)) {
4340 if (!utf8_to_uvchr(s, 0))
4344 down = (char*)(s - 1);
4345 /* reverse this character */
4349 *down-- = (char)tmp;
4355 down = SvPVX(TARG) + len - 1;
4359 *down-- = (char)tmp;
4361 (void)SvPOK_only_UTF8(TARG);
4373 register IV limit = POPi; /* note, negative is forever */
4374 SV * const sv = POPs;
4376 register const char *s = SvPV_const(sv, len);
4377 const bool do_utf8 = DO_UTF8(sv);
4378 const char *strend = s + len;
4380 register REGEXP *rx;
4382 register const char *m;
4384 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s);
4385 I32 maxiters = slen + 10;
4387 const I32 origlimit = limit;
4390 const I32 gimme = GIMME_V;
4391 const I32 oldsave = PL_savestack_ix;
4392 I32 make_mortal = 1;
4394 MAGIC *mg = (MAGIC *) NULL;
4397 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4402 DIE(aTHX_ "panic: pp_split");
4405 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4406 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4408 RX_MATCH_UTF8_set(rx, do_utf8);
4410 if (pm->op_pmreplroot) {
4412 ary = GvAVn((GV*)PAD_SVl(INT2PTR(PADOFFSET, pm->op_pmreplroot)));
4414 ary = GvAVn((GV*)pm->op_pmreplroot);
4417 else if (gimme != G_ARRAY)
4418 ary = GvAVn(PL_defgv);
4421 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4427 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4429 XPUSHs(SvTIED_obj((SV*)ary, mg));
4436 for (i = AvFILLp(ary); i >= 0; i--)
4437 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4439 /* temporarily switch stacks */
4440 SAVESWITCHSTACK(PL_curstack, ary);
4444 base = SP - PL_stack_base;
4446 if (pm->op_pmflags & PMf_SKIPWHITE) {
4447 if (pm->op_pmflags & PMf_LOCALE) {
4448 while (isSPACE_LC(*s))
4456 if (pm->op_pmflags & PMf_MULTILINE) {
4461 limit = maxiters + 2;
4462 if (pm->op_pmflags & PMf_WHITE) {
4465 while (m < strend &&
4466 !((pm->op_pmflags & PMf_LOCALE)
4467 ? isSPACE_LC(*m) : isSPACE(*m)))
4472 dstr = newSVpvn(s, m-s);
4476 (void)SvUTF8_on(dstr);
4480 while (s < strend &&
4481 ((pm->op_pmflags & PMf_LOCALE)
4482 ? isSPACE_LC(*s) : isSPACE(*s)))
4486 else if (rx->precomp[0] == '^' && rx->precomp[1] == '\0') {
4488 for (m = s; m < strend && *m != '\n'; m++)
4493 dstr = newSVpvn(s, m-s);
4497 (void)SvUTF8_on(dstr);
4502 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4503 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4504 && (rx->reganch & ROPT_CHECK_ALL)
4505 && !(rx->reganch & ROPT_ANCH)) {
4506 const int tail = (rx->reganch & RE_INTUIT_TAIL);
4507 SV * const csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4510 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4511 const char c = *SvPV_nolen_const(csv);
4513 for (m = s; m < strend && *m != c; m++)
4517 dstr = newSVpvn(s, m-s);
4521 (void)SvUTF8_on(dstr);
4523 /* The rx->minlen is in characters but we want to step
4524 * s ahead by bytes. */
4526 s = (char*)utf8_hop((U8*)m, len);
4528 s = m + len; /* Fake \n at the end */
4532 while (s < strend && --limit &&
4533 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4534 csv, multiline ? FBMrf_MULTILINE : 0)) )
4536 dstr = newSVpvn(s, m-s);
4540 (void)SvUTF8_on(dstr);
4542 /* The rx->minlen is in characters but we want to step
4543 * s ahead by bytes. */
4545 s = (char*)utf8_hop((U8*)m, len);
4547 s = m + len; /* Fake \n at the end */
4552 maxiters += slen * rx->nparens;
4553 while (s < strend && --limit)
4557 rex_return = CALLREGEXEC(aTHX_ rx, (char*)s, (char*)strend, (char*)orig, 1 ,
4560 if (rex_return == 0)
4562 TAINT_IF(RX_MATCH_TAINTED(rx));
4563 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4568 strend = s + (strend - m);
4570 m = rx->startp[0] + orig;
4571 dstr = newSVpvn(s, m-s);
4575 (void)SvUTF8_on(dstr);
4579 for (i = 1; i <= (I32)rx->nparens; i++) {
4580 s = rx->startp[i] + orig;
4581 m = rx->endp[i] + orig;
4583 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4584 parens that didn't match -- they should be set to
4585 undef, not the empty string */
4586 if (m >= orig && s >= orig) {
4587 dstr = newSVpvn(s, m-s);
4590 dstr = &PL_sv_undef; /* undef, not "" */
4594 (void)SvUTF8_on(dstr);
4598 s = rx->endp[0] + orig;
4602 iters = (SP - PL_stack_base) - base;
4603 if (iters > maxiters)
4604 DIE(aTHX_ "Split loop");
4606 /* keep field after final delim? */
4607 if (s < strend || (iters && origlimit)) {
4608 const STRLEN l = strend - s;
4609 dstr = newSVpvn(s, l);
4613 (void)SvUTF8_on(dstr);
4617 else if (!origlimit) {
4618 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
4619 if (TOPs && !make_mortal)
4622 *SP-- = &PL_sv_undef;
4627 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
4631 if (SvSMAGICAL(ary)) {
4636 if (gimme == G_ARRAY) {
4638 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4646 call_method("PUSH",G_SCALAR|G_DISCARD);
4649 if (gimme == G_ARRAY) {
4651 /* EXTEND should not be needed - we just popped them */
4653 for (i=0; i < iters; i++) {
4654 SV **svp = av_fetch(ary, i, FALSE);
4655 PUSHs((svp) ? *svp : &PL_sv_undef);
4662 if (gimme == G_ARRAY)
4677 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4678 || SvTYPE(retsv) == SVt_PVCV) {
4679 retsv = refto(retsv);
4686 PP(unimplemented_op)
4688 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
4694 * c-indentation-style: bsd
4696 * indent-tabs-mode: t
4699 * ex: set ts=8 sts=4 sw=4 noet: