3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 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
23 /* XXX I can't imagine anyone who doesn't have this actually _needs_
24 it, since pid_t is an integral type.
27 #ifdef NEED_GETPID_PROTO
28 extern Pid_t getpid (void);
31 /* variations on pp_null */
36 if (GIMME_V == G_SCALAR)
52 if (PL_op->op_private & OPpLVAL_INTRO)
53 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
55 if (PL_op->op_flags & OPf_REF) {
59 if (GIMME == G_SCALAR)
60 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
65 if (gimme == G_ARRAY) {
66 I32 maxarg = AvFILL((AV*)TARG) + 1;
68 if (SvMAGICAL(TARG)) {
70 for (i=0; i < (U32)maxarg; i++) {
71 SV **svp = av_fetch((AV*)TARG, i, FALSE);
72 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
76 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
80 else if (gimme == G_SCALAR) {
81 SV* sv = sv_newmortal();
82 I32 maxarg = AvFILL((AV*)TARG) + 1;
95 if (PL_op->op_private & OPpLVAL_INTRO)
96 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
97 if (PL_op->op_flags & OPf_REF)
100 if (GIMME == G_SCALAR)
101 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
105 if (gimme == G_ARRAY) {
108 else if (gimme == G_SCALAR) {
109 SV* sv = sv_newmortal();
110 if (SvRMAGICAL(TARG) && mg_find(TARG, PERL_MAGIC_tied))
111 Perl_croak(aTHX_ "Can't provide tied hash usage; "
112 "use keys(%%hash) to test if empty");
113 if (HvFILL((HV*)TARG))
114 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
115 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
125 DIE(aTHX_ "NOT IMPL LINE %d",__LINE__);
136 tryAMAGICunDEREF(to_gv);
139 if (SvTYPE(sv) == SVt_PVIO) {
140 GV *gv = (GV*) sv_newmortal();
141 gv_init(gv, 0, "", 0, 0);
142 GvIOp(gv) = (IO *)sv;
143 (void)SvREFCNT_inc(sv);
146 else if (SvTYPE(sv) != SVt_PVGV)
147 DIE(aTHX_ "Not a GLOB reference");
150 if (SvTYPE(sv) != SVt_PVGV) {
154 if (SvGMAGICAL(sv)) {
159 if (!SvOK(sv) && sv != &PL_sv_undef) {
160 /* If this is a 'my' scalar and flag is set then vivify
163 if (PL_op->op_private & OPpDEREF) {
166 if (cUNOP->op_targ) {
168 SV *namesv = PAD_SV(cUNOP->op_targ);
169 name = SvPV(namesv, len);
170 gv = (GV*)NEWSV(0,0);
171 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
174 name = CopSTASHPV(PL_curcop);
177 if (SvTYPE(sv) < SVt_RV)
178 sv_upgrade(sv, SVt_RV);
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))
192 if ((PL_op->op_flags & OPf_SPECIAL) &&
193 !(PL_op->op_flags & OPf_MOD))
195 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
197 && (!is_gv_magical(sym,len,0)
198 || !(sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV))))
204 if (PL_op->op_private & HINT_STRICT_REFS)
205 DIE(aTHX_ PL_no_symref, sym, "a symbol");
206 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
210 if (PL_op->op_private & OPpLVAL_INTRO)
211 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
223 tryAMAGICunDEREF(to_sv);
226 switch (SvTYPE(sv)) {
230 DIE(aTHX_ "Not a SCALAR reference");
238 if (SvTYPE(gv) != SVt_PVGV) {
239 if (SvGMAGICAL(sv)) {
245 if (PL_op->op_flags & OPf_REF ||
246 PL_op->op_private & HINT_STRICT_REFS)
247 DIE(aTHX_ PL_no_usym, "a SCALAR");
248 if (ckWARN(WARN_UNINITIALIZED))
253 if ((PL_op->op_flags & OPf_SPECIAL) &&
254 !(PL_op->op_flags & OPf_MOD))
256 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
258 && (!is_gv_magical(sym,len,0)
259 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV))))
265 if (PL_op->op_private & HINT_STRICT_REFS)
266 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
267 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
272 if (PL_op->op_flags & OPf_MOD) {
273 if (PL_op->op_private & OPpLVAL_INTRO) {
274 if (cUNOP->op_first->op_type == OP_NULL)
275 sv = save_scalar((GV*)TOPs);
277 sv = save_scalar(gv);
279 Perl_croak(aTHX_ PL_no_localize_ref);
281 else if (PL_op->op_private & OPpDEREF)
282 vivify_ref(sv, PL_op->op_private & OPpDEREF);
292 SV *sv = AvARYLEN(av);
294 AvARYLEN(av) = sv = NEWSV(0,0);
295 sv_upgrade(sv, SVt_IV);
296 sv_magic(sv, (SV*)av, PERL_MAGIC_arylen, Nullch, 0);
304 dSP; dTARGET; dPOPss;
306 if (PL_op->op_flags & OPf_MOD || LVRET) {
307 if (SvTYPE(TARG) < SVt_PVLV) {
308 sv_upgrade(TARG, SVt_PVLV);
309 sv_magic(TARG, Nullsv, PERL_MAGIC_pos, Nullch, 0);
313 if (LvTARG(TARG) != sv) {
315 SvREFCNT_dec(LvTARG(TARG));
316 LvTARG(TARG) = SvREFCNT_inc(sv);
318 PUSHs(TARG); /* no SvSETMAGIC */
324 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
325 mg = mg_find(sv, PERL_MAGIC_regex_global);
326 if (mg && mg->mg_len >= 0) {
330 PUSHi(i + PL_curcop->cop_arybase);
344 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
345 /* (But not in defined().) */
346 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
349 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
350 if ((PL_op->op_private & OPpLVAL_INTRO)) {
351 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
354 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
358 cv = (CV*)&PL_sv_undef;
372 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
373 char *s = SvPVX(TOPs);
374 if (strnEQ(s, "CORE::", 6)) {
377 code = keyword(s + 6, SvCUR(TOPs) - 6);
378 if (code < 0) { /* Overridable. */
379 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
380 int i = 0, n = 0, seen_question = 0;
382 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
384 if (code == -KEY_chop || code == -KEY_chomp)
386 while (i < MAXO) { /* The slow way. */
387 if (strEQ(s + 6, PL_op_name[i])
388 || strEQ(s + 6, PL_op_desc[i]))
394 goto nonesuch; /* Should not happen... */
396 oa = PL_opargs[i] >> OASHIFT;
398 if (oa & OA_OPTIONAL && !seen_question) {
402 else if (n && str[0] == ';' && seen_question)
403 goto set; /* XXXX system, exec */
404 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
405 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
406 /* But globs are already references (kinda) */
407 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
411 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
415 ret = sv_2mortal(newSVpvn(str, n - 1));
417 else if (code) /* Non-Overridable */
419 else { /* None such */
421 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
425 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
427 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
436 CV* cv = (CV*)PAD_SV(PL_op->op_targ);
438 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
454 if (GIMME != G_ARRAY) {
458 *MARK = &PL_sv_undef;
459 *MARK = refto(*MARK);
463 EXTEND_MORTAL(SP - MARK);
465 *MARK = refto(*MARK);
470 S_refto(pTHX_ SV *sv)
474 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
477 if (!(sv = LvTARG(sv)))
480 (void)SvREFCNT_inc(sv);
482 else if (SvTYPE(sv) == SVt_PVAV) {
483 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
486 (void)SvREFCNT_inc(sv);
488 else if (SvPADTMP(sv) && !IS_PADGV(sv))
492 (void)SvREFCNT_inc(sv);
495 sv_upgrade(rv, SVt_RV);
509 if (sv && SvGMAGICAL(sv))
512 if (!sv || !SvROK(sv))
516 pv = sv_reftype(sv,TRUE);
517 PUSHp(pv, strlen(pv));
527 stash = CopSTASH(PL_curcop);
533 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
534 Perl_croak(aTHX_ "Attempt to bless into a reference");
536 if (ckWARN(WARN_MISC) && len == 0)
537 Perl_warner(aTHX_ packWARN(WARN_MISC),
538 "Explicit blessing to '' (assuming package main)");
539 stash = gv_stashpvn(ptr, len, TRUE);
542 (void)sv_bless(TOPs, stash);
556 elem = SvPV(sv, n_a);
560 switch (elem ? *elem : '\0')
563 if (strEQ(elem, "ARRAY"))
564 tmpRef = (SV*)GvAV(gv);
567 if (strEQ(elem, "CODE"))
568 tmpRef = (SV*)GvCVu(gv);
571 if (strEQ(elem, "FILEHANDLE")) {
572 /* finally deprecated in 5.8.0 */
573 deprecate("*glob{FILEHANDLE}");
574 tmpRef = (SV*)GvIOp(gv);
577 if (strEQ(elem, "FORMAT"))
578 tmpRef = (SV*)GvFORM(gv);
581 if (strEQ(elem, "GLOB"))
585 if (strEQ(elem, "HASH"))
586 tmpRef = (SV*)GvHV(gv);
589 if (strEQ(elem, "IO"))
590 tmpRef = (SV*)GvIOp(gv);
593 if (strEQ(elem, "NAME"))
594 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
597 if (strEQ(elem, "PACKAGE")) {
598 if (HvNAME(GvSTASH(gv)))
599 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
601 sv = newSVpv("__ANON__",0);
605 if (strEQ(elem, "SCALAR"))
619 /* Pattern matching */
624 register unsigned char *s;
627 register I32 *sfirst;
631 if (sv == PL_lastscream) {
637 SvSCREAM_off(PL_lastscream);
638 SvREFCNT_dec(PL_lastscream);
640 PL_lastscream = SvREFCNT_inc(sv);
643 s = (unsigned char*)(SvPV(sv, len));
647 if (pos > PL_maxscream) {
648 if (PL_maxscream < 0) {
649 PL_maxscream = pos + 80;
650 New(301, PL_screamfirst, 256, I32);
651 New(302, PL_screamnext, PL_maxscream, I32);
654 PL_maxscream = pos + pos / 4;
655 Renew(PL_screamnext, PL_maxscream, I32);
659 sfirst = PL_screamfirst;
660 snext = PL_screamnext;
662 if (!sfirst || !snext)
663 DIE(aTHX_ "do_study: out of memory");
665 for (ch = 256; ch; --ch)
672 snext[pos] = sfirst[ch] - pos;
679 /* piggyback on m//g magic */
680 sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0);
689 if (PL_op->op_flags & OPf_STACKED)
695 TARG = sv_newmortal();
700 /* Lvalue operators. */
712 dSP; dMARK; dTARGET; dORIGMARK;
714 do_chop(TARG, *++MARK);
723 SETi(do_chomp(TOPs));
730 register I32 count = 0;
733 count += do_chomp(POPs);
744 if (!sv || !SvANY(sv))
746 switch (SvTYPE(sv)) {
748 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
749 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
753 if (HvARRAY(sv) || SvGMAGICAL(sv)
754 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
758 if (CvROOT(sv) || CvXSUB(sv))
775 if (!PL_op->op_private) {
784 SV_CHECK_THINKFIRST_COW_DROP(sv);
786 switch (SvTYPE(sv)) {
796 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
797 Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
798 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
802 /* let user-undef'd sub keep its identity */
803 GV* gv = CvGV((CV*)sv);
810 SvSetMagicSV(sv, &PL_sv_undef);
814 Newz(602, gp, 1, GP);
815 GvGP(sv) = gp_ref(gp);
816 GvSV(sv) = NEWSV(72,0);
817 GvLINE(sv) = CopLINE(PL_curcop);
823 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
826 SvPV_set(sv, Nullch);
839 if (SvTYPE(TOPs) > SVt_PVLV)
840 DIE(aTHX_ PL_no_modify);
841 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
842 && SvIVX(TOPs) != IV_MIN)
845 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
856 if (SvTYPE(TOPs) > SVt_PVLV)
857 DIE(aTHX_ PL_no_modify);
858 sv_setsv(TARG, TOPs);
859 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
860 && SvIVX(TOPs) != IV_MAX)
863 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
868 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
878 if (SvTYPE(TOPs) > SVt_PVLV)
879 DIE(aTHX_ PL_no_modify);
880 sv_setsv(TARG, TOPs);
881 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
882 && SvIVX(TOPs) != IV_MIN)
885 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
894 /* Ordinary operators. */
899 #ifdef PERL_PRESERVE_IVUV
902 tryAMAGICbin(pow,opASSIGN);
903 #ifdef PERL_PRESERVE_IVUV
904 /* For integer to integer power, we do the calculation by hand wherever
905 we're sure it is safe; otherwise we call pow() and try to convert to
906 integer afterwards. */
910 bool baseuok = SvUOK(TOPm1s);
914 baseuv = SvUVX(TOPm1s);
916 IV iv = SvIVX(TOPm1s);
919 baseuok = TRUE; /* effectively it's a UV now */
921 baseuv = -iv; /* abs, baseuok == false records sign */
935 goto float_it; /* Can't do negative powers this way. */
938 /* now we have integer ** positive integer. */
941 /* foo & (foo - 1) is zero only for a power of 2. */
942 if (!(baseuv & (baseuv - 1))) {
943 /* We are raising power-of-2 to a positive integer.
944 The logic here will work for any base (even non-integer
945 bases) but it can be less accurate than
946 pow (base,power) or exp (power * log (base)) when the
947 intermediate values start to spill out of the mantissa.
948 With powers of 2 we know this can't happen.
949 And powers of 2 are the favourite thing for perl
950 programmers to notice ** not doing what they mean. */
952 NV base = baseuok ? baseuv : -(NV)baseuv;
955 for (; power; base *= base, n++) {
956 /* Do I look like I trust gcc with long longs here?
958 UV bit = (UV)1 << (UV)n;
961 /* Only bother to clear the bit if it is set. */
963 /* Avoid squaring base again if we're done. */
964 if (power == 0) break;
972 register unsigned int highbit = 8 * sizeof(UV);
973 register unsigned int lowbit = 0;
974 register unsigned int diff;
975 while ((diff = (highbit - lowbit) >> 1)) {
976 if (baseuv & ~((1 << (lowbit + diff)) - 1))
981 /* we now have baseuv < 2 ** highbit */
982 if (power * highbit <= 8 * sizeof(UV)) {
983 /* result will definitely fit in UV, so use UV math
984 on same algorithm as above */
985 register UV result = 1;
986 register UV base = baseuv;
988 for (; power; base *= base, n++) {
989 register UV bit = (UV)1 << (UV)n;
993 if (power == 0) break;
997 if (baseuok || !(power & 1))
998 /* answer is positive */
1000 else if (result <= (UV)IV_MAX)
1001 /* answer negative, fits in IV */
1002 SETi( -(IV)result );
1003 else if (result == (UV)IV_MIN)
1004 /* 2's complement assumption: special case IV_MIN */
1007 /* answer negative, doesn't fit */
1008 SETn( -(NV)result );
1019 SETn( Perl_pow( left, right) );
1020 #ifdef PERL_PRESERVE_IVUV
1030 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1031 #ifdef PERL_PRESERVE_IVUV
1034 /* Unless the left argument is integer in range we are going to have to
1035 use NV maths. Hence only attempt to coerce the right argument if
1036 we know the left is integer. */
1037 /* Left operand is defined, so is it IV? */
1038 SvIV_please(TOPm1s);
1039 if (SvIOK(TOPm1s)) {
1040 bool auvok = SvUOK(TOPm1s);
1041 bool buvok = SvUOK(TOPs);
1042 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1043 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1050 alow = SvUVX(TOPm1s);
1052 IV aiv = SvIVX(TOPm1s);
1055 auvok = TRUE; /* effectively it's a UV now */
1057 alow = -aiv; /* abs, auvok == false records sign */
1063 IV biv = SvIVX(TOPs);
1066 buvok = TRUE; /* effectively it's a UV now */
1068 blow = -biv; /* abs, buvok == false records sign */
1072 /* If this does sign extension on unsigned it's time for plan B */
1073 ahigh = alow >> (4 * sizeof (UV));
1075 bhigh = blow >> (4 * sizeof (UV));
1077 if (ahigh && bhigh) {
1078 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1079 which is overflow. Drop to NVs below. */
1080 } else if (!ahigh && !bhigh) {
1081 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1082 so the unsigned multiply cannot overflow. */
1083 UV product = alow * blow;
1084 if (auvok == buvok) {
1085 /* -ve * -ve or +ve * +ve gives a +ve result. */
1089 } else if (product <= (UV)IV_MIN) {
1090 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1091 /* -ve result, which could overflow an IV */
1093 SETi( -(IV)product );
1095 } /* else drop to NVs below. */
1097 /* One operand is large, 1 small */
1100 /* swap the operands */
1102 bhigh = blow; /* bhigh now the temp var for the swap */
1106 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1107 multiplies can't overflow. shift can, add can, -ve can. */
1108 product_middle = ahigh * blow;
1109 if (!(product_middle & topmask)) {
1110 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1112 product_middle <<= (4 * sizeof (UV));
1113 product_low = alow * blow;
1115 /* as for pp_add, UV + something mustn't get smaller.
1116 IIRC ANSI mandates this wrapping *behaviour* for
1117 unsigned whatever the actual representation*/
1118 product_low += product_middle;
1119 if (product_low >= product_middle) {
1120 /* didn't overflow */
1121 if (auvok == buvok) {
1122 /* -ve * -ve or +ve * +ve gives a +ve result. */
1124 SETu( product_low );
1126 } else if (product_low <= (UV)IV_MIN) {
1127 /* 2s complement assumption again */
1128 /* -ve result, which could overflow an IV */
1130 SETi( -(IV)product_low );
1132 } /* else drop to NVs below. */
1134 } /* product_middle too large */
1135 } /* ahigh && bhigh */
1136 } /* SvIOK(TOPm1s) */
1141 SETn( left * right );
1148 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1149 /* Only try to do UV divide first
1150 if ((SLOPPYDIVIDE is true) or
1151 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1153 The assumption is that it is better to use floating point divide
1154 whenever possible, only doing integer divide first if we can't be sure.
1155 If NV_PRESERVES_UV is true then we know at compile time that no UV
1156 can be too large to preserve, so don't need to compile the code to
1157 test the size of UVs. */
1160 # define PERL_TRY_UV_DIVIDE
1161 /* ensure that 20./5. == 4. */
1163 # ifdef PERL_PRESERVE_IVUV
1164 # ifndef NV_PRESERVES_UV
1165 # define PERL_TRY_UV_DIVIDE
1170 #ifdef PERL_TRY_UV_DIVIDE
1173 SvIV_please(TOPm1s);
1174 if (SvIOK(TOPm1s)) {
1175 bool left_non_neg = SvUOK(TOPm1s);
1176 bool right_non_neg = SvUOK(TOPs);
1180 if (right_non_neg) {
1181 right = SvUVX(TOPs);
1184 IV biv = SvIVX(TOPs);
1187 right_non_neg = TRUE; /* effectively it's a UV now */
1193 /* historically undef()/0 gives a "Use of uninitialized value"
1194 warning before dieing, hence this test goes here.
1195 If it were immediately before the second SvIV_please, then
1196 DIE() would be invoked before left was even inspected, so
1197 no inpsection would give no warning. */
1199 DIE(aTHX_ "Illegal division by zero");
1202 left = SvUVX(TOPm1s);
1205 IV aiv = SvIVX(TOPm1s);
1208 left_non_neg = TRUE; /* effectively it's a UV now */
1217 /* For sloppy divide we always attempt integer division. */
1219 /* Otherwise we only attempt it if either or both operands
1220 would not be preserved by an NV. If both fit in NVs
1221 we fall through to the NV divide code below. However,
1222 as left >= right to ensure integer result here, we know that
1223 we can skip the test on the right operand - right big
1224 enough not to be preserved can't get here unless left is
1227 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1230 /* Integer division can't overflow, but it can be imprecise. */
1231 UV result = left / right;
1232 if (result * right == left) {
1233 SP--; /* result is valid */
1234 if (left_non_neg == right_non_neg) {
1235 /* signs identical, result is positive. */
1239 /* 2s complement assumption */
1240 if (result <= (UV)IV_MIN)
1241 SETi( -(IV)result );
1243 /* It's exact but too negative for IV. */
1244 SETn( -(NV)result );
1247 } /* tried integer divide but it was not an integer result */
1248 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1249 } /* left wasn't SvIOK */
1250 } /* right wasn't SvIOK */
1251 #endif /* PERL_TRY_UV_DIVIDE */
1255 DIE(aTHX_ "Illegal division by zero");
1256 PUSHn( left / right );
1263 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1267 bool left_neg = FALSE;
1268 bool right_neg = FALSE;
1269 bool use_double = FALSE;
1270 bool dright_valid = FALSE;
1276 right_neg = !SvUOK(TOPs);
1278 right = SvUVX(POPs);
1280 IV biv = SvIVX(POPs);
1283 right_neg = FALSE; /* effectively it's a UV now */
1291 right_neg = dright < 0;
1294 if (dright < UV_MAX_P1) {
1295 right = U_V(dright);
1296 dright_valid = TRUE; /* In case we need to use double below. */
1302 /* At this point use_double is only true if right is out of range for
1303 a UV. In range NV has been rounded down to nearest UV and
1304 use_double false. */
1306 if (!use_double && SvIOK(TOPs)) {
1308 left_neg = !SvUOK(TOPs);
1312 IV aiv = SvIVX(POPs);
1315 left_neg = FALSE; /* effectively it's a UV now */
1324 left_neg = dleft < 0;
1328 /* This should be exactly the 5.6 behaviour - if left and right are
1329 both in range for UV then use U_V() rather than floor. */
1331 if (dleft < UV_MAX_P1) {
1332 /* right was in range, so is dleft, so use UVs not double.
1336 /* left is out of range for UV, right was in range, so promote
1337 right (back) to double. */
1339 /* The +0.5 is used in 5.6 even though it is not strictly
1340 consistent with the implicit +0 floor in the U_V()
1341 inside the #if 1. */
1342 dleft = Perl_floor(dleft + 0.5);
1345 dright = Perl_floor(dright + 0.5);
1355 DIE(aTHX_ "Illegal modulus zero");
1357 dans = Perl_fmod(dleft, dright);
1358 if ((left_neg != right_neg) && dans)
1359 dans = dright - dans;
1362 sv_setnv(TARG, dans);
1368 DIE(aTHX_ "Illegal modulus zero");
1371 if ((left_neg != right_neg) && ans)
1374 /* XXX may warn: unary minus operator applied to unsigned type */
1375 /* could change -foo to be (~foo)+1 instead */
1376 if (ans <= ~((UV)IV_MAX)+1)
1377 sv_setiv(TARG, ~ans+1);
1379 sv_setnv(TARG, -(NV)ans);
1382 sv_setuv(TARG, ans);
1391 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1393 register IV count = POPi;
1394 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1396 I32 items = SP - MARK;
1399 max = items * count;
1404 /* This code was intended to fix 20010809.028:
1407 for (($x =~ /./g) x 2) {
1408 print chop; # "abcdabcd" expected as output.
1411 * but that change (#11635) broke this code:
1413 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1415 * I can't think of a better fix that doesn't introduce
1416 * an efficiency hit by copying the SVs. The stack isn't
1417 * refcounted, and mortalisation obviously doesn't
1418 * Do The Right Thing when the stack has more than
1419 * one pointer to the same mortal value.
1423 *SP = sv_2mortal(newSVsv(*SP));
1433 repeatcpy((char*)(MARK + items), (char*)MARK,
1434 items * sizeof(SV*), count - 1);
1437 else if (count <= 0)
1440 else { /* Note: mark already snarfed by pp_list */
1445 SvSetSV(TARG, tmpstr);
1446 SvPV_force(TARG, len);
1447 isutf = DO_UTF8(TARG);
1452 SvGROW(TARG, (count * len) + 1);
1453 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1454 SvCUR(TARG) *= count;
1456 *SvEND(TARG) = '\0';
1459 (void)SvPOK_only_UTF8(TARG);
1461 (void)SvPOK_only(TARG);
1463 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1464 /* The parser saw this as a list repeat, and there
1465 are probably several items on the stack. But we're
1466 in scalar context, and there's no pp_list to save us
1467 now. So drop the rest of the items -- robin@kitsite.com
1480 dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN);
1481 useleft = USE_LEFT(TOPm1s);
1482 #ifdef PERL_PRESERVE_IVUV
1483 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1484 "bad things" happen if you rely on signed integers wrapping. */
1487 /* Unless the left argument is integer in range we are going to have to
1488 use NV maths. Hence only attempt to coerce the right argument if
1489 we know the left is integer. */
1490 register UV auv = 0;
1496 a_valid = auvok = 1;
1497 /* left operand is undef, treat as zero. */
1499 /* Left operand is defined, so is it IV? */
1500 SvIV_please(TOPm1s);
1501 if (SvIOK(TOPm1s)) {
1502 if ((auvok = SvUOK(TOPm1s)))
1503 auv = SvUVX(TOPm1s);
1505 register IV aiv = SvIVX(TOPm1s);
1508 auvok = 1; /* Now acting as a sign flag. */
1509 } else { /* 2s complement assumption for IV_MIN */
1517 bool result_good = 0;
1520 bool buvok = SvUOK(TOPs);
1525 register IV biv = SvIVX(TOPs);
1532 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1533 else "IV" now, independent of how it came in.
1534 if a, b represents positive, A, B negative, a maps to -A etc
1539 all UV maths. negate result if A negative.
1540 subtract if signs same, add if signs differ. */
1542 if (auvok ^ buvok) {
1551 /* Must get smaller */
1556 if (result <= buv) {
1557 /* result really should be -(auv-buv). as its negation
1558 of true value, need to swap our result flag */
1570 if (result <= (UV)IV_MIN)
1571 SETi( -(IV)result );
1573 /* result valid, but out of range for IV. */
1574 SETn( -(NV)result );
1578 } /* Overflow, drop through to NVs. */
1582 useleft = USE_LEFT(TOPm1s);
1586 /* left operand is undef, treat as zero - value */
1590 SETn( TOPn - value );
1597 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1600 if (PL_op->op_private & HINT_INTEGER) {
1614 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1617 if (PL_op->op_private & HINT_INTEGER) {
1631 dSP; tryAMAGICbinSET(lt,0);
1632 #ifdef PERL_PRESERVE_IVUV
1635 SvIV_please(TOPm1s);
1636 if (SvIOK(TOPm1s)) {
1637 bool auvok = SvUOK(TOPm1s);
1638 bool buvok = SvUOK(TOPs);
1640 if (!auvok && !buvok) { /* ## IV < IV ## */
1641 IV aiv = SvIVX(TOPm1s);
1642 IV biv = SvIVX(TOPs);
1645 SETs(boolSV(aiv < biv));
1648 if (auvok && buvok) { /* ## UV < UV ## */
1649 UV auv = SvUVX(TOPm1s);
1650 UV buv = SvUVX(TOPs);
1653 SETs(boolSV(auv < buv));
1656 if (auvok) { /* ## UV < IV ## */
1663 /* As (a) is a UV, it's >=0, so it cannot be < */
1668 SETs(boolSV(auv < (UV)biv));
1671 { /* ## IV < UV ## */
1675 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) && SvROK(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 IV aiv = SvIVX(TOPm1s);
1720 IV biv = SvIVX(TOPs);
1723 SETs(boolSV(aiv > biv));
1726 if (auvok && buvok) { /* ## UV > UV ## */
1727 UV auv = SvUVX(TOPm1s);
1728 UV buv = SvUVX(TOPs);
1731 SETs(boolSV(auv > buv));
1734 if (auvok) { /* ## UV > IV ## */
1741 /* As (a) is a UV, it's >=0, so it must be > */
1746 SETs(boolSV(auv > (UV)biv));
1749 { /* ## IV > UV ## */
1753 aiv = SvIVX(TOPm1s);
1755 /* As (b) is a UV, it's >=0, so it cannot be > */
1762 SETs(boolSV((UV)aiv > buv));
1768 #ifndef NV_PRESERVES_UV
1769 #ifdef PERL_PRESERVE_IVUV
1772 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1774 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1780 SETs(boolSV(TOPn > value));
1787 dSP; tryAMAGICbinSET(le,0);
1788 #ifdef PERL_PRESERVE_IVUV
1791 SvIV_please(TOPm1s);
1792 if (SvIOK(TOPm1s)) {
1793 bool auvok = SvUOK(TOPm1s);
1794 bool buvok = SvUOK(TOPs);
1796 if (!auvok && !buvok) { /* ## IV <= IV ## */
1797 IV aiv = SvIVX(TOPm1s);
1798 IV biv = SvIVX(TOPs);
1801 SETs(boolSV(aiv <= biv));
1804 if (auvok && buvok) { /* ## UV <= UV ## */
1805 UV auv = SvUVX(TOPm1s);
1806 UV buv = SvUVX(TOPs);
1809 SETs(boolSV(auv <= buv));
1812 if (auvok) { /* ## UV <= IV ## */
1819 /* As (a) is a UV, it's >=0, so a cannot be <= */
1824 SETs(boolSV(auv <= (UV)biv));
1827 { /* ## IV <= UV ## */
1831 aiv = SvIVX(TOPm1s);
1833 /* As (b) is a UV, it's >=0, so a must be <= */
1840 SETs(boolSV((UV)aiv <= buv));
1846 #ifndef NV_PRESERVES_UV
1847 #ifdef PERL_PRESERVE_IVUV
1850 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1852 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1858 SETs(boolSV(TOPn <= value));
1865 dSP; tryAMAGICbinSET(ge,0);
1866 #ifdef PERL_PRESERVE_IVUV
1869 SvIV_please(TOPm1s);
1870 if (SvIOK(TOPm1s)) {
1871 bool auvok = SvUOK(TOPm1s);
1872 bool buvok = SvUOK(TOPs);
1874 if (!auvok && !buvok) { /* ## IV >= IV ## */
1875 IV aiv = SvIVX(TOPm1s);
1876 IV biv = SvIVX(TOPs);
1879 SETs(boolSV(aiv >= biv));
1882 if (auvok && buvok) { /* ## UV >= UV ## */
1883 UV auv = SvUVX(TOPm1s);
1884 UV buv = SvUVX(TOPs);
1887 SETs(boolSV(auv >= buv));
1890 if (auvok) { /* ## UV >= IV ## */
1897 /* As (a) is a UV, it's >=0, so it must be >= */
1902 SETs(boolSV(auv >= (UV)biv));
1905 { /* ## IV >= UV ## */
1909 aiv = SvIVX(TOPm1s);
1911 /* As (b) is a UV, it's >=0, so a cannot be >= */
1918 SETs(boolSV((UV)aiv >= buv));
1924 #ifndef NV_PRESERVES_UV
1925 #ifdef PERL_PRESERVE_IVUV
1928 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1930 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
1936 SETs(boolSV(TOPn >= value));
1943 dSP; tryAMAGICbinSET(ne,0);
1944 #ifndef NV_PRESERVES_UV
1945 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1947 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
1951 #ifdef PERL_PRESERVE_IVUV
1954 SvIV_please(TOPm1s);
1955 if (SvIOK(TOPm1s)) {
1956 bool auvok = SvUOK(TOPm1s);
1957 bool buvok = SvUOK(TOPs);
1959 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
1960 /* Casting IV to UV before comparison isn't going to matter
1961 on 2s complement. On 1s complement or sign&magnitude
1962 (if we have any of them) it could make negative zero
1963 differ from normal zero. As I understand it. (Need to
1964 check - is negative zero implementation defined behaviour
1966 UV buv = SvUVX(POPs);
1967 UV auv = SvUVX(TOPs);
1969 SETs(boolSV(auv != buv));
1972 { /* ## Mixed IV,UV ## */
1976 /* != is commutative so swap if needed (save code) */
1978 /* swap. top of stack (b) is the iv */
1982 /* As (a) is a UV, it's >0, so it cannot be == */
1991 /* As (b) is a UV, it's >0, so it cannot be == */
1995 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
1997 SETs(boolSV((UV)iv != uv));
2005 SETs(boolSV(TOPn != value));
2012 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2013 #ifndef NV_PRESERVES_UV
2014 if (SvROK(TOPs) && SvROK(TOPm1s)) {
2015 UV right = PTR2UV(SvRV(POPs));
2016 UV left = PTR2UV(SvRV(TOPs));
2017 SETi((left > right) - (left < right));
2021 #ifdef PERL_PRESERVE_IVUV
2022 /* Fortunately it seems NaN isn't IOK */
2025 SvIV_please(TOPm1s);
2026 if (SvIOK(TOPm1s)) {
2027 bool leftuvok = SvUOK(TOPm1s);
2028 bool rightuvok = SvUOK(TOPs);
2030 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2031 IV leftiv = SvIVX(TOPm1s);
2032 IV rightiv = SvIVX(TOPs);
2034 if (leftiv > rightiv)
2036 else if (leftiv < rightiv)
2040 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2041 UV leftuv = SvUVX(TOPm1s);
2042 UV rightuv = SvUVX(TOPs);
2044 if (leftuv > rightuv)
2046 else if (leftuv < rightuv)
2050 } else if (leftuvok) { /* ## UV <=> IV ## */
2054 rightiv = SvIVX(TOPs);
2056 /* As (a) is a UV, it's >=0, so it cannot be < */
2059 leftuv = SvUVX(TOPm1s);
2060 if (leftuv > (UV)rightiv) {
2062 } else if (leftuv < (UV)rightiv) {
2068 } else { /* ## IV <=> UV ## */
2072 leftiv = SvIVX(TOPm1s);
2074 /* As (b) is a UV, it's >=0, so it must be < */
2077 rightuv = SvUVX(TOPs);
2078 if ((UV)leftiv > rightuv) {
2080 } else if ((UV)leftiv < rightuv) {
2098 if (Perl_isnan(left) || Perl_isnan(right)) {
2102 value = (left > right) - (left < right);
2106 else if (left < right)
2108 else if (left > right)
2122 dSP; tryAMAGICbinSET(slt,0);
2125 int cmp = (IN_LOCALE_RUNTIME
2126 ? sv_cmp_locale(left, right)
2127 : sv_cmp(left, right));
2128 SETs(boolSV(cmp < 0));
2135 dSP; tryAMAGICbinSET(sgt,0);
2138 int cmp = (IN_LOCALE_RUNTIME
2139 ? sv_cmp_locale(left, right)
2140 : sv_cmp(left, right));
2141 SETs(boolSV(cmp > 0));
2148 dSP; tryAMAGICbinSET(sle,0);
2151 int cmp = (IN_LOCALE_RUNTIME
2152 ? sv_cmp_locale(left, right)
2153 : sv_cmp(left, right));
2154 SETs(boolSV(cmp <= 0));
2161 dSP; tryAMAGICbinSET(sge,0);
2164 int cmp = (IN_LOCALE_RUNTIME
2165 ? sv_cmp_locale(left, right)
2166 : sv_cmp(left, right));
2167 SETs(boolSV(cmp >= 0));
2174 dSP; tryAMAGICbinSET(seq,0);
2177 SETs(boolSV(sv_eq(left, right)));
2184 dSP; tryAMAGICbinSET(sne,0);
2187 SETs(boolSV(!sv_eq(left, right)));
2194 dSP; dTARGET; tryAMAGICbin(scmp,0);
2197 int cmp = (IN_LOCALE_RUNTIME
2198 ? sv_cmp_locale(left, right)
2199 : sv_cmp(left, right));
2207 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2210 if (SvNIOKp(left) || SvNIOKp(right)) {
2211 if (PL_op->op_private & HINT_INTEGER) {
2212 IV i = SvIV(left) & SvIV(right);
2216 UV u = SvUV(left) & SvUV(right);
2221 do_vop(PL_op->op_type, TARG, left, right);
2230 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
2233 if (SvNIOKp(left) || SvNIOKp(right)) {
2234 if (PL_op->op_private & HINT_INTEGER) {
2235 IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
2239 UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
2244 do_vop(PL_op->op_type, TARG, left, right);
2253 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
2256 if (SvNIOKp(left) || SvNIOKp(right)) {
2257 if (PL_op->op_private & HINT_INTEGER) {
2258 IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
2262 UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
2267 do_vop(PL_op->op_type, TARG, left, right);
2276 dSP; dTARGET; tryAMAGICun(neg);
2279 int flags = SvFLAGS(sv);
2282 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2283 /* It's publicly an integer, or privately an integer-not-float */
2286 if (SvIVX(sv) == IV_MIN) {
2287 /* 2s complement assumption. */
2288 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2291 else if (SvUVX(sv) <= IV_MAX) {
2296 else if (SvIVX(sv) != IV_MIN) {
2300 #ifdef PERL_PRESERVE_IVUV
2309 else if (SvPOKp(sv)) {
2311 char *s = SvPV(sv, len);
2312 if (isIDFIRST(*s)) {
2313 sv_setpvn(TARG, "-", 1);
2316 else if (*s == '+' || *s == '-') {
2318 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2320 else if (DO_UTF8(sv)) {
2323 goto oops_its_an_int;
2325 sv_setnv(TARG, -SvNV(sv));
2327 sv_setpvn(TARG, "-", 1);
2334 goto oops_its_an_int;
2335 sv_setnv(TARG, -SvNV(sv));
2347 dSP; tryAMAGICunSET(not);
2348 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2354 dSP; dTARGET; tryAMAGICun(compl);
2358 if (PL_op->op_private & HINT_INTEGER) {
2373 tmps = (U8*)SvPV_force(TARG, len);
2376 /* Calculate exact length, let's not estimate. */
2385 while (tmps < send) {
2386 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2387 tmps += UTF8SKIP(tmps);
2388 targlen += UNISKIP(~c);
2394 /* Now rewind strings and write them. */
2398 Newz(0, result, targlen + 1, U8);
2399 while (tmps < send) {
2400 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2401 tmps += UTF8SKIP(tmps);
2402 result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY);
2406 sv_setpvn(TARG, (char*)result, targlen);
2410 Newz(0, result, nchar + 1, U8);
2411 while (tmps < send) {
2412 U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY);
2413 tmps += UTF8SKIP(tmps);
2418 sv_setpvn(TARG, (char*)result, nchar);
2426 register long *tmpl;
2427 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2430 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
2435 for ( ; anum > 0; anum--, tmps++)
2444 /* integer versions of some of the above */
2448 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2451 SETi( left * right );
2458 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2462 DIE(aTHX_ "Illegal division by zero");
2463 value = POPi / value;
2472 /* This is the vanilla old i_modulo. */
2473 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2477 DIE(aTHX_ "Illegal modulus zero");
2478 SETi( left % right );
2483 #if defined(__GLIBC__) && IVSIZE == 8
2487 /* This is the i_modulo with the workaround for the _moddi3 bug
2488 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2489 * See below for pp_i_modulo. */
2490 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2494 DIE(aTHX_ "Illegal modulus zero");
2495 SETi( left % PERL_ABS(right) );
2503 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2507 DIE(aTHX_ "Illegal modulus zero");
2508 /* The assumption is to use hereafter the old vanilla version... */
2510 PL_ppaddr[OP_I_MODULO] =
2511 &Perl_pp_i_modulo_0;
2512 /* .. but if we have glibc, we might have a buggy _moddi3
2513 * (at least glicb 2.2.5 is known to have this bug), in other
2514 * words our integer modulus with negative quad as the second
2515 * argument might be broken. Test for this and re-patch the
2516 * opcode dispatch table if that is the case, remembering to
2517 * also apply the workaround so that this first round works
2518 * right, too. See [perl #9402] for more information. */
2519 #if defined(__GLIBC__) && IVSIZE == 8
2523 /* Cannot do this check with inlined IV constants since
2524 * that seems to work correctly even with the buggy glibc. */
2526 /* Yikes, we have the bug.
2527 * Patch in the workaround version. */
2529 PL_ppaddr[OP_I_MODULO] =
2530 &Perl_pp_i_modulo_1;
2531 /* Make certain we work right this time, too. */
2532 right = PERL_ABS(right);
2536 SETi( left % right );
2543 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2546 SETi( left + right );
2553 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2556 SETi( left - right );
2563 dSP; tryAMAGICbinSET(lt,0);
2566 SETs(boolSV(left < right));
2573 dSP; tryAMAGICbinSET(gt,0);
2576 SETs(boolSV(left > right));
2583 dSP; tryAMAGICbinSET(le,0);
2586 SETs(boolSV(left <= right));
2593 dSP; tryAMAGICbinSET(ge,0);
2596 SETs(boolSV(left >= right));
2603 dSP; tryAMAGICbinSET(eq,0);
2606 SETs(boolSV(left == right));
2613 dSP; tryAMAGICbinSET(ne,0);
2616 SETs(boolSV(left != right));
2623 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2630 else if (left < right)
2641 dSP; dTARGET; tryAMAGICun(neg);
2646 /* High falutin' math. */
2650 dSP; dTARGET; tryAMAGICbin(atan2,0);
2653 SETn(Perl_atan2(left, right));
2660 dSP; dTARGET; tryAMAGICun(sin);
2664 value = Perl_sin(value);
2672 dSP; dTARGET; tryAMAGICun(cos);
2676 value = Perl_cos(value);
2682 /* Support Configure command-line overrides for rand() functions.
2683 After 5.005, perhaps we should replace this by Configure support
2684 for drand48(), random(), or rand(). For 5.005, though, maintain
2685 compatibility by calling rand() but allow the user to override it.
2686 See INSTALL for details. --Andy Dougherty 15 July 1998
2688 /* Now it's after 5.005, and Configure supports drand48() and random(),
2689 in addition to rand(). So the overrides should not be needed any more.
2690 --Jarkko Hietaniemi 27 September 1998
2693 #ifndef HAS_DRAND48_PROTO
2694 extern double drand48 (void);
2707 if (!PL_srand_called) {
2708 (void)seedDrand01((Rand_seed_t)seed());
2709 PL_srand_called = TRUE;
2724 (void)seedDrand01((Rand_seed_t)anum);
2725 PL_srand_called = TRUE;
2732 dSP; dTARGET; tryAMAGICun(exp);
2736 value = Perl_exp(value);
2744 dSP; dTARGET; tryAMAGICun(log);
2749 SET_NUMERIC_STANDARD();
2750 DIE(aTHX_ "Can't take log of %"NVgf, value);
2752 value = Perl_log(value);
2760 dSP; dTARGET; tryAMAGICun(sqrt);
2765 SET_NUMERIC_STANDARD();
2766 DIE(aTHX_ "Can't take sqrt of %"NVgf, value);
2768 value = Perl_sqrt(value);
2775 * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
2776 * These need to be revisited when a newer toolchain becomes available.
2778 #if defined(__sparc64__) && defined(__GNUC__)
2779 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
2780 # undef SPARC64_MODF_WORKAROUND
2781 # define SPARC64_MODF_WORKAROUND 1
2785 #if defined(SPARC64_MODF_WORKAROUND)
2787 sparc64_workaround_modf(NV theVal, NV *theIntRes)
2790 ret = Perl_modf(theVal, &res);
2798 dSP; dTARGET; tryAMAGICun(int);
2801 IV iv = TOPi; /* attempt to convert to IV if possible. */
2802 /* XXX it's arguable that compiler casting to IV might be subtly
2803 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2804 else preferring IV has introduced a subtle behaviour change bug. OTOH
2805 relying on floating point to be accurate is a bug. */
2816 if (value < (NV)UV_MAX + 0.5) {
2819 #if defined(SPARC64_MODF_WORKAROUND)
2820 (void)sparc64_workaround_modf(value, &value);
2821 #elif defined(HAS_MODFL_POW32_BUG)
2822 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2823 NV offset = Perl_modf(value, &value);
2824 (void)Perl_modf(offset, &offset);
2827 (void)Perl_modf(value, &value);
2833 if (value > (NV)IV_MIN - 0.5) {
2836 #if defined(SPARC64_MODF_WORKAROUND)
2837 (void)sparc64_workaround_modf(-value, &value);
2838 #elif defined(HAS_MODFL_POW32_BUG)
2839 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2840 NV offset = Perl_modf(-value, &value);
2841 (void)Perl_modf(offset, &offset);
2844 (void)Perl_modf(-value, &value);
2856 dSP; dTARGET; tryAMAGICun(abs);
2858 /* This will cache the NV value if string isn't actually integer */
2862 /* IVX is precise */
2864 SETu(TOPu); /* force it to be numeric only */
2872 /* 2s complement assumption. Also, not really needed as
2873 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2893 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2899 tmps = (SvPVx(sv, len));
2901 /* If Unicode, try to downgrade
2902 * If not possible, croak. */
2903 SV* tsv = sv_2mortal(newSVsv(sv));
2906 sv_utf8_downgrade(tsv, FALSE);
2909 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2910 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2923 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2929 tmps = (SvPVx(sv, len));
2931 /* If Unicode, try to downgrade
2932 * If not possible, croak. */
2933 SV* tsv = sv_2mortal(newSVsv(sv));
2936 sv_utf8_downgrade(tsv, FALSE);
2939 while (*tmps && len && isSPACE(*tmps))
2944 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2945 else if (*tmps == 'b')
2946 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2948 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2950 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2967 SETi(sv_len_utf8(sv));
2983 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2985 I32 arybase = PL_curcop->cop_arybase;
2989 int num_args = PL_op->op_private & 7;
2990 bool repl_need_utf8_upgrade = FALSE;
2991 bool repl_is_utf8 = FALSE;
2993 SvTAINTED_off(TARG); /* decontaminate */
2994 SvUTF8_off(TARG); /* decontaminate */
2998 repl = SvPV(repl_sv, repl_len);
2999 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3009 sv_utf8_upgrade(sv);
3011 else if (DO_UTF8(sv))
3012 repl_need_utf8_upgrade = TRUE;
3014 tmps = SvPV(sv, curlen);
3016 utf8_curlen = sv_len_utf8(sv);
3017 if (utf8_curlen == curlen)
3020 curlen = utf8_curlen;
3025 if (pos >= arybase) {
3043 else if (len >= 0) {
3045 if (rem > (I32)curlen)
3060 Perl_croak(aTHX_ "substr outside of string");
3061 if (ckWARN(WARN_SUBSTR))
3062 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3069 sv_pos_u2b(sv, &pos, &rem);
3071 sv_setpvn(TARG, tmps, rem);
3072 #ifdef USE_LOCALE_COLLATE
3073 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3078 SV* repl_sv_copy = NULL;
3080 if (repl_need_utf8_upgrade) {
3081 repl_sv_copy = newSVsv(repl_sv);
3082 sv_utf8_upgrade(repl_sv_copy);
3083 repl = SvPV(repl_sv_copy, repl_len);
3084 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3086 sv_insert(sv, pos, rem, repl, repl_len);
3090 SvREFCNT_dec(repl_sv_copy);
3092 else if (lvalue) { /* it's an lvalue! */
3093 if (!SvGMAGICAL(sv)) {
3097 if (ckWARN(WARN_SUBSTR))
3098 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3099 "Attempt to use reference as lvalue in substr");
3101 if (SvOK(sv)) /* is it defined ? */
3102 (void)SvPOK_only_UTF8(sv);
3104 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3107 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3108 TARG = sv_newmortal();
3109 if (SvTYPE(TARG) < SVt_PVLV) {
3110 sv_upgrade(TARG, SVt_PVLV);
3111 sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0);
3115 if (LvTARG(TARG) != sv) {
3117 SvREFCNT_dec(LvTARG(TARG));
3118 LvTARG(TARG) = SvREFCNT_inc(sv);
3120 LvTARGOFF(TARG) = upos;
3121 LvTARGLEN(TARG) = urem;
3125 PUSHs(TARG); /* avoid SvSETMAGIC here */
3132 register IV size = POPi;
3133 register IV offset = POPi;
3134 register SV *src = POPs;
3135 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3137 SvTAINTED_off(TARG); /* decontaminate */
3138 if (lvalue) { /* it's an lvalue! */
3139 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3140 TARG = sv_newmortal();
3141 if (SvTYPE(TARG) < SVt_PVLV) {
3142 sv_upgrade(TARG, SVt_PVLV);
3143 sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0);
3146 if (LvTARG(TARG) != src) {
3148 SvREFCNT_dec(LvTARG(TARG));
3149 LvTARG(TARG) = SvREFCNT_inc(src);
3151 LvTARGOFF(TARG) = offset;
3152 LvTARGLEN(TARG) = size;
3155 sv_setuv(TARG, do_vecget(src, offset, size));
3170 I32 arybase = PL_curcop->cop_arybase;
3175 offset = POPi - arybase;
3178 tmps = SvPV(big, biglen);
3179 if (offset > 0 && DO_UTF8(big))
3180 sv_pos_u2b(big, &offset, 0);
3183 else if (offset > (I32)biglen)
3185 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
3186 (unsigned char*)tmps + biglen, little, 0)))
3189 retval = tmps2 - tmps;
3190 if (retval > 0 && DO_UTF8(big))
3191 sv_pos_b2u(big, &retval);
3192 PUSHi(retval + arybase);
3207 I32 arybase = PL_curcop->cop_arybase;
3213 tmps2 = SvPV(little, llen);
3214 tmps = SvPV(big, blen);
3218 if (offset > 0 && DO_UTF8(big))
3219 sv_pos_u2b(big, &offset, 0);
3220 offset = offset - arybase + llen;
3224 else if (offset > (I32)blen)
3226 if (!(tmps2 = rninstr(tmps, tmps + offset,
3227 tmps2, tmps2 + llen)))
3230 retval = tmps2 - tmps;
3231 if (retval > 0 && DO_UTF8(big))
3232 sv_pos_b2u(big, &retval);
3233 PUSHi(retval + arybase);
3239 dSP; dMARK; dORIGMARK; dTARGET;
3240 do_sprintf(TARG, SP-MARK, MARK+1);
3241 TAINT_IF(SvTAINTED(TARG));
3242 if (DO_UTF8(*(MARK+1)))
3254 U8 *s = (U8*)SvPVx(argsv, len);
3257 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3258 tmpsv = sv_2mortal(newSVsv(argsv));
3259 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3263 XPUSHu(DO_UTF8(argsv) ?
3264 utf8n_to_uvchr(s, UTF8_MAXLEN, 0, UTF8_ALLOW_ANYUV) :
3276 (void)SvUPGRADE(TARG,SVt_PV);
3278 if (value > 255 && !IN_BYTES) {
3279 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3280 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3281 SvCUR_set(TARG, tmps - SvPVX(TARG));
3283 (void)SvPOK_only(TARG);
3292 *tmps++ = (char)value;
3294 (void)SvPOK_only(TARG);
3295 if (PL_encoding && !IN_BYTES) {
3296 sv_recode_to_utf8(TARG, PL_encoding);
3298 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3299 memEQ(tmps, "\xef\xbf\xbd\0", 4)) {
3303 *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value);
3304 *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value);
3320 char *tmps = SvPV(left, len);
3322 if (DO_UTF8(left)) {
3323 /* If Unicode, try to downgrade.
3324 * If not possible, croak.
3325 * Yes, we made this up. */
3326 SV* tsv = sv_2mortal(newSVsv(left));
3329 sv_utf8_downgrade(tsv, FALSE);
3332 # ifdef USE_ITHREADS
3334 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3335 /* This should be threadsafe because in ithreads there is only
3336 * one thread per interpreter. If this would not be true,
3337 * we would need a mutex to protect this malloc. */
3338 PL_reentrant_buffer->_crypt_struct_buffer =
3339 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3340 #if defined(__GLIBC__) || defined(__EMX__)
3341 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3342 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3343 /* work around glibc-2.2.5 bug */
3344 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3348 # endif /* HAS_CRYPT_R */
3349 # endif /* USE_ITHREADS */
3351 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
3353 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
3359 "The crypt() function is unimplemented due to excessive paranoia.");
3372 (s = (U8*)SvPV_nomg(sv, slen)) && slen &&
3373 UTF8_IS_START(*s)) {
3374 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3378 utf8_to_uvchr(s, &ulen);
3379 toTITLE_utf8(s, tmpbuf, &tculen);
3380 utf8_to_uvchr(tmpbuf, 0);
3382 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3384 /* slen is the byte length of the whole SV.
3385 * ulen is the byte length of the original Unicode character
3386 * stored as UTF-8 at s.
3387 * tculen is the byte length of the freshly titlecased
3388 * Unicode character stored as UTF-8 at tmpbuf.
3389 * We first set the result to be the titlecased character,
3390 * and then append the rest of the SV data. */
3391 sv_setpvn(TARG, (char*)tmpbuf, tculen);
3393 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3398 s = (U8*)SvPV_force_nomg(sv, slen);
3399 Copy(tmpbuf, s, tculen, U8);
3403 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3405 SvUTF8_off(TARG); /* decontaminate */
3406 sv_setsv_nomg(TARG, sv);
3410 s = (U8*)SvPV_force_nomg(sv, slen);
3412 if (IN_LOCALE_RUNTIME) {
3415 *s = toUPPER_LC(*s);
3434 (s = (U8*)SvPV_nomg(sv, slen)) && slen &&
3435 UTF8_IS_START(*s)) {
3437 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3441 toLOWER_utf8(s, tmpbuf, &ulen);
3442 uv = utf8_to_uvchr(tmpbuf, 0);
3443 tend = uvchr_to_utf8(tmpbuf, uv);
3445 if (!SvPADTMP(sv) || (STRLEN)(tend - tmpbuf) != ulen || SvREADONLY(sv)) {
3447 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
3449 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3454 s = (U8*)SvPV_force_nomg(sv, slen);
3455 Copy(tmpbuf, s, ulen, U8);
3459 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3461 SvUTF8_off(TARG); /* decontaminate */
3462 sv_setsv_nomg(TARG, sv);
3466 s = (U8*)SvPV_force_nomg(sv, slen);
3468 if (IN_LOCALE_RUNTIME) {
3471 *s = toLOWER_LC(*s);
3494 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3496 s = (U8*)SvPV_nomg(sv,len);
3498 SvUTF8_off(TARG); /* decontaminate */
3499 sv_setpvn(TARG, "", 0);
3503 STRLEN nchar = utf8_length(s, s + len);
3505 (void)SvUPGRADE(TARG, SVt_PV);
3506 SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1);
3507 (void)SvPOK_only(TARG);
3508 d = (U8*)SvPVX(TARG);
3511 toUPPER_utf8(s, tmpbuf, &ulen);
3512 Copy(tmpbuf, d, ulen, U8);
3518 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3523 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3525 SvUTF8_off(TARG); /* decontaminate */
3526 sv_setsv_nomg(TARG, sv);
3530 s = (U8*)SvPV_force_nomg(sv, len);
3532 register U8 *send = s + len;
3534 if (IN_LOCALE_RUNTIME) {
3537 for (; s < send; s++)
3538 *s = toUPPER_LC(*s);
3541 for (; s < send; s++)
3563 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3565 s = (U8*)SvPV_nomg(sv,len);
3567 SvUTF8_off(TARG); /* decontaminate */
3568 sv_setpvn(TARG, "", 0);
3572 STRLEN nchar = utf8_length(s, s + len);
3574 (void)SvUPGRADE(TARG, SVt_PV);
3575 SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1);
3576 (void)SvPOK_only(TARG);
3577 d = (U8*)SvPVX(TARG);
3580 UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3581 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode */
3582 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3584 * Now if the sigma is NOT followed by
3585 * /$ignorable_sequence$cased_letter/;
3586 * and it IS preceded by
3587 * /$cased_letter$ignorable_sequence/;
3588 * where $ignorable_sequence is
3589 * [\x{2010}\x{AD}\p{Mn}]*
3590 * and $cased_letter is
3591 * [\p{Ll}\p{Lo}\p{Lt}]
3592 * then it should be mapped to 0x03C2,
3593 * (GREEK SMALL LETTER FINAL SIGMA),
3594 * instead of staying 0x03A3.
3595 * See lib/unicore/SpecCase.txt.
3598 Copy(tmpbuf, d, ulen, U8);
3604 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3609 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3611 SvUTF8_off(TARG); /* decontaminate */
3612 sv_setsv_nomg(TARG, sv);
3617 s = (U8*)SvPV_force_nomg(sv, len);
3619 register U8 *send = s + len;
3621 if (IN_LOCALE_RUNTIME) {
3624 for (; s < send; s++)
3625 *s = toLOWER_LC(*s);
3628 for (; s < send; s++)
3642 register char *s = SvPV(sv,len);
3645 SvUTF8_off(TARG); /* decontaminate */
3647 (void)SvUPGRADE(TARG, SVt_PV);
3648 SvGROW(TARG, (len * 2) + 1);
3652 if (UTF8_IS_CONTINUED(*s)) {
3653 STRLEN ulen = UTF8SKIP(s);
3677 SvCUR_set(TARG, d - SvPVX(TARG));
3678 (void)SvPOK_only_UTF8(TARG);
3681 sv_setpvn(TARG, s, len);
3683 if (SvSMAGICAL(TARG))
3692 dSP; dMARK; dORIGMARK;
3694 register AV* av = (AV*)POPs;
3695 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3696 I32 arybase = PL_curcop->cop_arybase;
3699 if (SvTYPE(av) == SVt_PVAV) {
3700 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3702 for (svp = MARK + 1; svp <= SP; svp++) {
3707 if (max > AvMAX(av))
3710 while (++MARK <= SP) {
3711 elem = SvIVx(*MARK);
3715 svp = av_fetch(av, elem, lval);
3717 if (!svp || *svp == &PL_sv_undef)
3718 DIE(aTHX_ PL_no_aelem, elem);
3719 if (PL_op->op_private & OPpLVAL_INTRO)
3720 save_aelem(av, elem, svp);
3722 *MARK = svp ? *svp : &PL_sv_undef;
3725 if (GIMME != G_ARRAY) {
3733 /* Associative arrays. */
3738 HV *hash = (HV*)POPs;
3740 I32 gimme = GIMME_V;
3743 /* might clobber stack_sp */
3744 entry = hv_iternext(hash);
3749 SV* sv = hv_iterkeysv(entry);
3750 PUSHs(sv); /* won't clobber stack_sp */
3751 if (gimme == G_ARRAY) {
3754 /* might clobber stack_sp */
3755 val = hv_iterval(hash, entry);
3760 else if (gimme == G_SCALAR)
3779 I32 gimme = GIMME_V;
3780 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3784 if (PL_op->op_private & OPpSLICE) {
3788 hvtype = SvTYPE(hv);
3789 if (hvtype == SVt_PVHV) { /* hash element */
3790 while (++MARK <= SP) {
3791 sv = hv_delete_ent(hv, *MARK, discard, 0);
3792 *MARK = sv ? sv : &PL_sv_undef;
3795 else if (hvtype == SVt_PVAV) { /* array element */
3796 if (PL_op->op_flags & OPf_SPECIAL) {
3797 while (++MARK <= SP) {
3798 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3799 *MARK = sv ? sv : &PL_sv_undef;
3804 DIE(aTHX_ "Not a HASH reference");
3807 else if (gimme == G_SCALAR) {
3816 if (SvTYPE(hv) == SVt_PVHV)
3817 sv = hv_delete_ent(hv, keysv, discard, 0);
3818 else if (SvTYPE(hv) == SVt_PVAV) {
3819 if (PL_op->op_flags & OPf_SPECIAL)
3820 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3822 DIE(aTHX_ "panic: avhv_delete no longer supported");
3825 DIE(aTHX_ "Not a HASH reference");
3840 if (PL_op->op_private & OPpEXISTS_SUB) {
3844 cv = sv_2cv(sv, &hv, &gv, FALSE);
3847 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3853 if (SvTYPE(hv) == SVt_PVHV) {
3854 if (hv_exists_ent(hv, tmpsv, 0))
3857 else if (SvTYPE(hv) == SVt_PVAV) {
3858 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3859 if (av_exists((AV*)hv, SvIV(tmpsv)))
3864 DIE(aTHX_ "Not a HASH reference");
3871 dSP; dMARK; dORIGMARK;
3872 register HV *hv = (HV*)POPs;
3873 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3874 bool localizing = PL_op->op_private & OPpLVAL_INTRO ? TRUE : FALSE;
3875 bool other_magic = FALSE;
3881 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
3882 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
3883 /* Try to preserve the existenceness of a tied hash
3884 * element by using EXISTS and DELETE if possible.
3885 * Fallback to FETCH and STORE otherwise */
3886 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
3887 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
3888 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
3891 while (++MARK <= SP) {
3895 bool preeminent = FALSE;
3898 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
3899 hv_exists_ent(hv, keysv, 0);
3902 he = hv_fetch_ent(hv, keysv, lval, 0);
3903 svp = he ? &HeVAL(he) : 0;
3906 if (!svp || *svp == &PL_sv_undef) {
3908 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
3912 save_helem(hv, keysv, svp);
3915 char *key = SvPV(keysv, keylen);
3916 SAVEDELETE(hv, savepvn(key,keylen), keylen);
3920 *MARK = svp ? *svp : &PL_sv_undef;
3922 if (GIMME != G_ARRAY) {
3930 /* List operators. */
3935 if (GIMME != G_ARRAY) {
3937 *MARK = *SP; /* unwanted list, return last item */
3939 *MARK = &PL_sv_undef;
3948 SV **lastrelem = PL_stack_sp;
3949 SV **lastlelem = PL_stack_base + POPMARK;
3950 SV **firstlelem = PL_stack_base + POPMARK + 1;
3951 register SV **firstrelem = lastlelem + 1;
3952 I32 arybase = PL_curcop->cop_arybase;
3953 I32 lval = PL_op->op_flags & OPf_MOD;
3954 I32 is_something_there = lval;
3956 register I32 max = lastrelem - lastlelem;
3957 register SV **lelem;
3960 if (GIMME != G_ARRAY) {
3961 ix = SvIVx(*lastlelem);
3966 if (ix < 0 || ix >= max)
3967 *firstlelem = &PL_sv_undef;
3969 *firstlelem = firstrelem[ix];
3975 SP = firstlelem - 1;
3979 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
3985 if (ix < 0 || ix >= max)
3986 *lelem = &PL_sv_undef;
3988 is_something_there = TRUE;
3989 if (!(*lelem = firstrelem[ix]))
3990 *lelem = &PL_sv_undef;
3993 if (is_something_there)
3996 SP = firstlelem - 1;
4002 dSP; dMARK; dORIGMARK;
4003 I32 items = SP - MARK;
4004 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
4005 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4012 dSP; dMARK; dORIGMARK;
4013 HV* hv = (HV*)sv_2mortal((SV*)newHV());
4017 SV *val = NEWSV(46, 0);
4019 sv_setsv(val, *++MARK);
4020 else if (ckWARN(WARN_MISC))
4021 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4022 (void)hv_store_ent(hv,key,val,0);
4031 dSP; dMARK; dORIGMARK;
4032 register AV *ary = (AV*)*++MARK;
4036 register I32 offset;
4037 register I32 length;
4044 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4045 *MARK-- = SvTIED_obj((SV*)ary, mg);
4049 call_method("SPLICE",GIMME_V);
4058 offset = i = SvIVx(*MARK);
4060 offset += AvFILLp(ary) + 1;
4062 offset -= PL_curcop->cop_arybase;
4064 DIE(aTHX_ PL_no_aelem, i);
4066 length = SvIVx(*MARK++);
4068 length += AvFILLp(ary) - offset + 1;
4074 length = AvMAX(ary) + 1; /* close enough to infinity */
4078 length = AvMAX(ary) + 1;
4080 if (offset > AvFILLp(ary) + 1) {
4081 if (ckWARN(WARN_MISC))
4082 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4083 offset = AvFILLp(ary) + 1;
4085 after = AvFILLp(ary) + 1 - (offset + length);
4086 if (after < 0) { /* not that much array */
4087 length += after; /* offset+length now in array */
4093 /* At this point, MARK .. SP-1 is our new LIST */
4096 diff = newlen - length;
4097 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4100 if (diff < 0) { /* shrinking the area */
4102 New(451, tmparyval, newlen, SV*); /* so remember insertion */
4103 Copy(MARK, tmparyval, newlen, SV*);
4106 MARK = ORIGMARK + 1;
4107 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4108 MEXTEND(MARK, length);
4109 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4111 EXTEND_MORTAL(length);
4112 for (i = length, dst = MARK; i; i--) {
4113 sv_2mortal(*dst); /* free them eventualy */
4120 *MARK = AvARRAY(ary)[offset+length-1];
4123 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4124 SvREFCNT_dec(*dst++); /* free them now */
4127 AvFILLp(ary) += diff;
4129 /* pull up or down? */
4131 if (offset < after) { /* easier to pull up */
4132 if (offset) { /* esp. if nothing to pull */
4133 src = &AvARRAY(ary)[offset-1];
4134 dst = src - diff; /* diff is negative */
4135 for (i = offset; i > 0; i--) /* can't trust Copy */
4139 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
4143 if (after) { /* anything to pull down? */
4144 src = AvARRAY(ary) + offset + length;
4145 dst = src + diff; /* diff is negative */
4146 Move(src, dst, after, SV*);
4148 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4149 /* avoid later double free */
4153 dst[--i] = &PL_sv_undef;
4156 for (src = tmparyval, dst = AvARRAY(ary) + offset;
4158 *dst = NEWSV(46, 0);
4159 sv_setsv(*dst++, *src++);
4161 Safefree(tmparyval);
4164 else { /* no, expanding (or same) */
4166 New(452, tmparyval, length, SV*); /* so remember deletion */
4167 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4170 if (diff > 0) { /* expanding */
4172 /* push up or down? */
4174 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4178 Move(src, dst, offset, SV*);
4180 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
4182 AvFILLp(ary) += diff;
4185 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4186 av_extend(ary, AvFILLp(ary) + diff);
4187 AvFILLp(ary) += diff;
4190 dst = AvARRAY(ary) + AvFILLp(ary);
4192 for (i = after; i; i--) {
4199 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
4200 *dst = NEWSV(46, 0);
4201 sv_setsv(*dst++, *src++);
4203 MARK = ORIGMARK + 1;
4204 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4206 Copy(tmparyval, MARK, length, SV*);
4208 EXTEND_MORTAL(length);
4209 for (i = length, dst = MARK; i; i--) {
4210 sv_2mortal(*dst); /* free them eventualy */
4214 Safefree(tmparyval);
4218 else if (length--) {
4219 *MARK = tmparyval[length];
4222 while (length-- > 0)
4223 SvREFCNT_dec(tmparyval[length]);
4225 Safefree(tmparyval);
4228 *MARK = &PL_sv_undef;
4236 dSP; dMARK; dORIGMARK; dTARGET;
4237 register AV *ary = (AV*)*++MARK;
4238 register SV *sv = &PL_sv_undef;
4241 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4242 *MARK-- = SvTIED_obj((SV*)ary, mg);
4246 call_method("PUSH",G_SCALAR|G_DISCARD);
4251 /* Why no pre-extend of ary here ? */
4252 for (++MARK; MARK <= SP; MARK++) {
4255 sv_setsv(sv, *MARK);
4260 PUSHi( AvFILL(ary) + 1 );
4268 SV *sv = av_pop(av);
4270 (void)sv_2mortal(sv);
4279 SV *sv = av_shift(av);
4284 (void)sv_2mortal(sv);
4291 dSP; dMARK; dORIGMARK; dTARGET;
4292 register AV *ary = (AV*)*++MARK;
4297 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4298 *MARK-- = SvTIED_obj((SV*)ary, mg);
4302 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4307 av_unshift(ary, SP - MARK);
4310 sv_setsv(sv, *++MARK);
4311 (void)av_store(ary, i++, sv);
4315 PUSHi( AvFILL(ary) + 1 );
4325 if (GIMME == G_ARRAY) {
4332 /* safe as long as stack cannot get extended in the above */
4337 register char *down;
4342 SvUTF8_off(TARG); /* decontaminate */
4344 do_join(TARG, &PL_sv_no, MARK, SP);
4346 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
4347 up = SvPV_force(TARG, len);
4349 if (DO_UTF8(TARG)) { /* first reverse each character */
4350 U8* s = (U8*)SvPVX(TARG);
4351 U8* send = (U8*)(s + len);
4353 if (UTF8_IS_INVARIANT(*s)) {
4358 if (!utf8_to_uvchr(s, 0))
4362 down = (char*)(s - 1);
4363 /* reverse this character */
4367 *down-- = (char)tmp;
4373 down = SvPVX(TARG) + len - 1;
4377 *down-- = (char)tmp;
4379 (void)SvPOK_only_UTF8(TARG);
4391 register IV limit = POPi; /* note, negative is forever */
4394 register char *s = SvPV(sv, len);
4395 bool do_utf8 = DO_UTF8(sv);
4396 char *strend = s + len;
4398 register REGEXP *rx;
4402 STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s);
4403 I32 maxiters = slen + 10;
4406 I32 origlimit = limit;
4409 AV *oldstack = PL_curstack;
4410 I32 gimme = GIMME_V;
4411 I32 oldsave = PL_savestack_ix;
4412 I32 make_mortal = 1;
4413 MAGIC *mg = (MAGIC *) NULL;
4416 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4421 DIE(aTHX_ "panic: pp_split");
4424 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4425 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4427 RX_MATCH_UTF8_set(rx, do_utf8);
4429 if (pm->op_pmreplroot) {
4431 ary = GvAVn((GV*)PAD_SVl(INT2PTR(PADOFFSET, pm->op_pmreplroot)));
4433 ary = GvAVn((GV*)pm->op_pmreplroot);
4436 else if (gimme != G_ARRAY)
4437 ary = GvAVn(PL_defgv);
4440 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4446 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4448 XPUSHs(SvTIED_obj((SV*)ary, mg));
4454 for (i = AvFILLp(ary); i >= 0; i--)
4455 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4457 /* temporarily switch stacks */
4458 SWITCHSTACK(PL_curstack, ary);
4459 PL_curstackinfo->si_stack = ary;
4463 base = SP - PL_stack_base;
4465 if (pm->op_pmflags & PMf_SKIPWHITE) {
4466 if (pm->op_pmflags & PMf_LOCALE) {
4467 while (isSPACE_LC(*s))
4475 if ((pm->op_pmflags & PMf_MULTILINE) != PL_multiline) {
4476 SAVEINT(PL_multiline);
4477 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4481 limit = maxiters + 2;
4482 if (pm->op_pmflags & PMf_WHITE) {
4485 while (m < strend &&
4486 !((pm->op_pmflags & PMf_LOCALE)
4487 ? isSPACE_LC(*m) : isSPACE(*m)))
4492 dstr = NEWSV(30, m-s);
4493 sv_setpvn(dstr, s, m-s);
4497 (void)SvUTF8_on(dstr);
4501 while (s < strend &&
4502 ((pm->op_pmflags & PMf_LOCALE)
4503 ? isSPACE_LC(*s) : isSPACE(*s)))
4507 else if (strEQ("^", rx->precomp)) {
4510 for (m = s; m < strend && *m != '\n'; m++) ;
4514 dstr = NEWSV(30, m-s);
4515 sv_setpvn(dstr, s, m-s);
4519 (void)SvUTF8_on(dstr);
4524 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4525 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4526 && (rx->reganch & ROPT_CHECK_ALL)
4527 && !(rx->reganch & ROPT_ANCH)) {
4528 int tail = (rx->reganch & RE_INTUIT_TAIL);
4529 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4532 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4534 char c = *SvPV(csv, n_a);
4537 for (m = s; m < strend && *m != c; m++) ;
4540 dstr = NEWSV(30, m-s);
4541 sv_setpvn(dstr, s, m-s);
4545 (void)SvUTF8_on(dstr);
4547 /* The rx->minlen is in characters but we want to step
4548 * s ahead by bytes. */
4550 s = (char*)utf8_hop((U8*)m, len);
4552 s = m + len; /* Fake \n at the end */
4557 while (s < strend && --limit &&
4558 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4559 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
4562 dstr = NEWSV(31, m-s);
4563 sv_setpvn(dstr, s, m-s);
4567 (void)SvUTF8_on(dstr);
4569 /* The rx->minlen is in characters but we want to step
4570 * s ahead by bytes. */
4572 s = (char*)utf8_hop((U8*)m, len);
4574 s = m + len; /* Fake \n at the end */
4579 maxiters += slen * rx->nparens;
4580 while (s < strend && --limit)
4583 i = CALLREGEXEC(aTHX_ rx, s, strend, orig, 1 , sv, NULL, 0);
4587 TAINT_IF(RX_MATCH_TAINTED(rx));
4588 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4593 strend = s + (strend - m);
4595 m = rx->startp[0] + orig;
4596 dstr = NEWSV(32, m-s);
4597 sv_setpvn(dstr, s, m-s);
4601 (void)SvUTF8_on(dstr);
4604 for (i = 1; i <= (I32)rx->nparens; i++) {
4605 s = rx->startp[i] + orig;
4606 m = rx->endp[i] + orig;
4608 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4609 parens that didn't match -- they should be set to
4610 undef, not the empty string */
4611 if (m >= orig && s >= orig) {
4612 dstr = NEWSV(33, m-s);
4613 sv_setpvn(dstr, s, m-s);
4616 dstr = &PL_sv_undef; /* undef, not "" */
4620 (void)SvUTF8_on(dstr);
4624 s = rx->endp[0] + orig;
4628 LEAVE_SCOPE(oldsave);
4629 iters = (SP - PL_stack_base) - base;
4630 if (iters > maxiters)
4631 DIE(aTHX_ "Split loop");
4633 /* keep field after final delim? */
4634 if (s < strend || (iters && origlimit)) {
4635 STRLEN l = strend - s;
4636 dstr = NEWSV(34, l);
4637 sv_setpvn(dstr, s, l);
4641 (void)SvUTF8_on(dstr);
4645 else if (!origlimit) {
4646 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
4647 if (TOPs && !make_mortal)
4656 SWITCHSTACK(ary, oldstack);
4657 PL_curstackinfo->si_stack = oldstack;
4658 if (SvSMAGICAL(ary)) {
4663 if (gimme == G_ARRAY) {
4665 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4673 call_method("PUSH",G_SCALAR|G_DISCARD);
4676 if (gimme == G_ARRAY) {
4677 /* EXTEND should not be needed - we just popped them */
4679 for (i=0; i < iters; i++) {
4680 SV **svp = av_fetch(ary, i, FALSE);
4681 PUSHs((svp) ? *svp : &PL_sv_undef);
4688 if (gimme == G_ARRAY)
4703 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4704 || SvTYPE(retsv) == SVt_PVCV) {
4705 retsv = refto(retsv);
4713 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");