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;
2734 * This is really just a quick hack which grabs various garbage
2735 * values. It really should be a real hash algorithm which
2736 * spreads the effect of every input bit onto every output bit,
2737 * if someone who knows about such things would bother to write it.
2738 * Might be a good idea to add that function to CORE as well.
2739 * No numbers below come from careful analysis or anything here,
2740 * except they are primes and SEED_C1 > 1E6 to get a full-width
2741 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
2742 * probably be bigger too.
2745 # define SEED_C1 1000003
2746 #define SEED_C4 73819
2748 # define SEED_C1 25747
2749 #define SEED_C4 20639
2753 #define SEED_C5 26107
2755 #ifndef PERL_NO_DEV_RANDOM
2760 # include <starlet.h>
2761 /* when[] = (low 32 bits, high 32 bits) of time since epoch
2762 * in 100-ns units, typically incremented ever 10 ms. */
2763 unsigned int when[2];
2765 # ifdef HAS_GETTIMEOFDAY
2766 struct timeval when;
2772 /* This test is an escape hatch, this symbol isn't set by Configure. */
2773 #ifndef PERL_NO_DEV_RANDOM
2774 #ifndef PERL_RANDOM_DEVICE
2775 /* /dev/random isn't used by default because reads from it will block
2776 * if there isn't enough entropy available. You can compile with
2777 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
2778 * is enough real entropy to fill the seed. */
2779 # define PERL_RANDOM_DEVICE "/dev/urandom"
2781 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
2783 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
2792 _ckvmssts(sys$gettim(when));
2793 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
2795 # ifdef HAS_GETTIMEOFDAY
2796 PerlProc_gettimeofday(&when,NULL);
2797 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
2800 u = (U32)SEED_C1 * when;
2803 u += SEED_C3 * (U32)PerlProc_getpid();
2804 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
2805 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
2806 u += SEED_C5 * (U32)PTR2UV(&when);
2813 dSP; dTARGET; tryAMAGICun(exp);
2817 value = Perl_exp(value);
2825 dSP; dTARGET; tryAMAGICun(log);
2830 SET_NUMERIC_STANDARD();
2831 DIE(aTHX_ "Can't take log of %"NVgf, value);
2833 value = Perl_log(value);
2841 dSP; dTARGET; tryAMAGICun(sqrt);
2846 SET_NUMERIC_STANDARD();
2847 DIE(aTHX_ "Can't take sqrt of %"NVgf, value);
2849 value = Perl_sqrt(value);
2856 * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
2857 * These need to be revisited when a newer toolchain becomes available.
2859 #if defined(__sparc64__) && defined(__GNUC__)
2860 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
2861 # undef SPARC64_MODF_WORKAROUND
2862 # define SPARC64_MODF_WORKAROUND 1
2866 #if defined(SPARC64_MODF_WORKAROUND)
2868 sparc64_workaround_modf(NV theVal, NV *theIntRes)
2871 ret = Perl_modf(theVal, &res);
2879 dSP; dTARGET; tryAMAGICun(int);
2882 IV iv = TOPi; /* attempt to convert to IV if possible. */
2883 /* XXX it's arguable that compiler casting to IV might be subtly
2884 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2885 else preferring IV has introduced a subtle behaviour change bug. OTOH
2886 relying on floating point to be accurate is a bug. */
2897 if (value < (NV)UV_MAX + 0.5) {
2900 #if defined(SPARC64_MODF_WORKAROUND)
2901 (void)sparc64_workaround_modf(value, &value);
2902 #elif defined(HAS_MODFL_POW32_BUG)
2903 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2904 NV offset = Perl_modf(value, &value);
2905 (void)Perl_modf(offset, &offset);
2908 (void)Perl_modf(value, &value);
2914 if (value > (NV)IV_MIN - 0.5) {
2917 #if defined(SPARC64_MODF_WORKAROUND)
2918 (void)sparc64_workaround_modf(-value, &value);
2919 #elif defined(HAS_MODFL_POW32_BUG)
2920 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2921 NV offset = Perl_modf(-value, &value);
2922 (void)Perl_modf(offset, &offset);
2925 (void)Perl_modf(-value, &value);
2937 dSP; dTARGET; tryAMAGICun(abs);
2939 /* This will cache the NV value if string isn't actually integer */
2943 /* IVX is precise */
2945 SETu(TOPu); /* force it to be numeric only */
2953 /* 2s complement assumption. Also, not really needed as
2954 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2974 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2980 tmps = (SvPVx(sv, len));
2982 /* If Unicode, try to downgrade
2983 * If not possible, croak. */
2984 SV* tsv = sv_2mortal(newSVsv(sv));
2987 sv_utf8_downgrade(tsv, FALSE);
2990 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2991 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3004 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3010 tmps = (SvPVx(sv, len));
3012 /* If Unicode, try to downgrade
3013 * If not possible, croak. */
3014 SV* tsv = sv_2mortal(newSVsv(sv));
3017 sv_utf8_downgrade(tsv, FALSE);
3020 while (*tmps && len && isSPACE(*tmps))
3025 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3026 else if (*tmps == 'b')
3027 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3029 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3031 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3048 SETi(sv_len_utf8(sv));
3064 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3066 I32 arybase = PL_curcop->cop_arybase;
3070 int num_args = PL_op->op_private & 7;
3071 bool repl_need_utf8_upgrade = FALSE;
3072 bool repl_is_utf8 = FALSE;
3074 SvTAINTED_off(TARG); /* decontaminate */
3075 SvUTF8_off(TARG); /* decontaminate */
3079 repl = SvPV(repl_sv, repl_len);
3080 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3090 sv_utf8_upgrade(sv);
3092 else if (DO_UTF8(sv))
3093 repl_need_utf8_upgrade = TRUE;
3095 tmps = SvPV(sv, curlen);
3097 utf8_curlen = sv_len_utf8(sv);
3098 if (utf8_curlen == curlen)
3101 curlen = utf8_curlen;
3106 if (pos >= arybase) {
3124 else if (len >= 0) {
3126 if (rem > (I32)curlen)
3141 Perl_croak(aTHX_ "substr outside of string");
3142 if (ckWARN(WARN_SUBSTR))
3143 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3150 sv_pos_u2b(sv, &pos, &rem);
3152 sv_setpvn(TARG, tmps, rem);
3153 #ifdef USE_LOCALE_COLLATE
3154 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3159 SV* repl_sv_copy = NULL;
3161 if (repl_need_utf8_upgrade) {
3162 repl_sv_copy = newSVsv(repl_sv);
3163 sv_utf8_upgrade(repl_sv_copy);
3164 repl = SvPV(repl_sv_copy, repl_len);
3165 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3167 sv_insert(sv, pos, rem, repl, repl_len);
3171 SvREFCNT_dec(repl_sv_copy);
3173 else if (lvalue) { /* it's an lvalue! */
3174 if (!SvGMAGICAL(sv)) {
3178 if (ckWARN(WARN_SUBSTR))
3179 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3180 "Attempt to use reference as lvalue in substr");
3182 if (SvOK(sv)) /* is it defined ? */
3183 (void)SvPOK_only_UTF8(sv);
3185 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3188 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3189 TARG = sv_newmortal();
3190 if (SvTYPE(TARG) < SVt_PVLV) {
3191 sv_upgrade(TARG, SVt_PVLV);
3192 sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0);
3196 if (LvTARG(TARG) != sv) {
3198 SvREFCNT_dec(LvTARG(TARG));
3199 LvTARG(TARG) = SvREFCNT_inc(sv);
3201 LvTARGOFF(TARG) = upos;
3202 LvTARGLEN(TARG) = urem;
3206 PUSHs(TARG); /* avoid SvSETMAGIC here */
3213 register IV size = POPi;
3214 register IV offset = POPi;
3215 register SV *src = POPs;
3216 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3218 SvTAINTED_off(TARG); /* decontaminate */
3219 if (lvalue) { /* it's an lvalue! */
3220 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3221 TARG = sv_newmortal();
3222 if (SvTYPE(TARG) < SVt_PVLV) {
3223 sv_upgrade(TARG, SVt_PVLV);
3224 sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0);
3227 if (LvTARG(TARG) != src) {
3229 SvREFCNT_dec(LvTARG(TARG));
3230 LvTARG(TARG) = SvREFCNT_inc(src);
3232 LvTARGOFF(TARG) = offset;
3233 LvTARGLEN(TARG) = size;
3236 sv_setuv(TARG, do_vecget(src, offset, size));
3251 I32 arybase = PL_curcop->cop_arybase;
3256 offset = POPi - arybase;
3259 tmps = SvPV(big, biglen);
3260 if (offset > 0 && DO_UTF8(big))
3261 sv_pos_u2b(big, &offset, 0);
3264 else if (offset > (I32)biglen)
3266 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
3267 (unsigned char*)tmps + biglen, little, 0)))
3270 retval = tmps2 - tmps;
3271 if (retval > 0 && DO_UTF8(big))
3272 sv_pos_b2u(big, &retval);
3273 PUSHi(retval + arybase);
3288 I32 arybase = PL_curcop->cop_arybase;
3294 tmps2 = SvPV(little, llen);
3295 tmps = SvPV(big, blen);
3299 if (offset > 0 && DO_UTF8(big))
3300 sv_pos_u2b(big, &offset, 0);
3301 offset = offset - arybase + llen;
3305 else if (offset > (I32)blen)
3307 if (!(tmps2 = rninstr(tmps, tmps + offset,
3308 tmps2, tmps2 + llen)))
3311 retval = tmps2 - tmps;
3312 if (retval > 0 && DO_UTF8(big))
3313 sv_pos_b2u(big, &retval);
3314 PUSHi(retval + arybase);
3320 dSP; dMARK; dORIGMARK; dTARGET;
3321 do_sprintf(TARG, SP-MARK, MARK+1);
3322 TAINT_IF(SvTAINTED(TARG));
3323 if (DO_UTF8(*(MARK+1)))
3335 U8 *s = (U8*)SvPVx(argsv, len);
3338 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3339 tmpsv = sv_2mortal(newSVsv(argsv));
3340 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3344 XPUSHu(DO_UTF8(argsv) ?
3345 utf8n_to_uvchr(s, UTF8_MAXLEN, 0, UTF8_ALLOW_ANYUV) :
3357 (void)SvUPGRADE(TARG,SVt_PV);
3359 if (value > 255 && !IN_BYTES) {
3360 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3361 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3362 SvCUR_set(TARG, tmps - SvPVX(TARG));
3364 (void)SvPOK_only(TARG);
3373 *tmps++ = (char)value;
3375 (void)SvPOK_only(TARG);
3376 if (PL_encoding && !IN_BYTES) {
3377 sv_recode_to_utf8(TARG, PL_encoding);
3379 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3380 memEQ(tmps, "\xef\xbf\xbd\0", 4)) {
3384 *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value);
3385 *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value);
3401 char *tmps = SvPV(left, len);
3403 if (DO_UTF8(left)) {
3404 /* If Unicode, try to downgrade.
3405 * If not possible, croak.
3406 * Yes, we made this up. */
3407 SV* tsv = sv_2mortal(newSVsv(left));
3410 sv_utf8_downgrade(tsv, FALSE);
3413 # ifdef USE_ITHREADS
3415 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3416 /* This should be threadsafe because in ithreads there is only
3417 * one thread per interpreter. If this would not be true,
3418 * we would need a mutex to protect this malloc. */
3419 PL_reentrant_buffer->_crypt_struct_buffer =
3420 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3421 #if defined(__GLIBC__) || defined(__EMX__)
3422 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3423 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3424 /* work around glibc-2.2.5 bug */
3425 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3429 # endif /* HAS_CRYPT_R */
3430 # endif /* USE_ITHREADS */
3432 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
3434 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
3440 "The crypt() function is unimplemented due to excessive paranoia.");
3453 (s = (U8*)SvPV_nomg(sv, slen)) && slen &&
3454 UTF8_IS_START(*s)) {
3455 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3459 utf8_to_uvchr(s, &ulen);
3460 toTITLE_utf8(s, tmpbuf, &tculen);
3461 utf8_to_uvchr(tmpbuf, 0);
3463 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3465 /* slen is the byte length of the whole SV.
3466 * ulen is the byte length of the original Unicode character
3467 * stored as UTF-8 at s.
3468 * tculen is the byte length of the freshly titlecased
3469 * Unicode character stored as UTF-8 at tmpbuf.
3470 * We first set the result to be the titlecased character,
3471 * and then append the rest of the SV data. */
3472 sv_setpvn(TARG, (char*)tmpbuf, tculen);
3474 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3479 s = (U8*)SvPV_force_nomg(sv, slen);
3480 Copy(tmpbuf, s, tculen, U8);
3484 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3486 SvUTF8_off(TARG); /* decontaminate */
3487 sv_setsv_nomg(TARG, sv);
3491 s = (U8*)SvPV_force_nomg(sv, slen);
3493 if (IN_LOCALE_RUNTIME) {
3496 *s = toUPPER_LC(*s);
3515 (s = (U8*)SvPV_nomg(sv, slen)) && slen &&
3516 UTF8_IS_START(*s)) {
3518 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3522 toLOWER_utf8(s, tmpbuf, &ulen);
3523 uv = utf8_to_uvchr(tmpbuf, 0);
3524 tend = uvchr_to_utf8(tmpbuf, uv);
3526 if (!SvPADTMP(sv) || (STRLEN)(tend - tmpbuf) != ulen || SvREADONLY(sv)) {
3528 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
3530 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3535 s = (U8*)SvPV_force_nomg(sv, slen);
3536 Copy(tmpbuf, s, ulen, U8);
3540 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3542 SvUTF8_off(TARG); /* decontaminate */
3543 sv_setsv_nomg(TARG, sv);
3547 s = (U8*)SvPV_force_nomg(sv, slen);
3549 if (IN_LOCALE_RUNTIME) {
3552 *s = toLOWER_LC(*s);
3575 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3577 s = (U8*)SvPV_nomg(sv,len);
3579 SvUTF8_off(TARG); /* decontaminate */
3580 sv_setpvn(TARG, "", 0);
3584 STRLEN nchar = utf8_length(s, s + len);
3586 (void)SvUPGRADE(TARG, SVt_PV);
3587 SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1);
3588 (void)SvPOK_only(TARG);
3589 d = (U8*)SvPVX(TARG);
3592 toUPPER_utf8(s, tmpbuf, &ulen);
3593 Copy(tmpbuf, d, ulen, U8);
3599 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3604 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3606 SvUTF8_off(TARG); /* decontaminate */
3607 sv_setsv_nomg(TARG, sv);
3611 s = (U8*)SvPV_force_nomg(sv, len);
3613 register U8 *send = s + len;
3615 if (IN_LOCALE_RUNTIME) {
3618 for (; s < send; s++)
3619 *s = toUPPER_LC(*s);
3622 for (; s < send; s++)
3644 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3646 s = (U8*)SvPV_nomg(sv,len);
3648 SvUTF8_off(TARG); /* decontaminate */
3649 sv_setpvn(TARG, "", 0);
3653 STRLEN nchar = utf8_length(s, s + len);
3655 (void)SvUPGRADE(TARG, SVt_PV);
3656 SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1);
3657 (void)SvPOK_only(TARG);
3658 d = (U8*)SvPVX(TARG);
3661 UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3662 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode */
3663 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3665 * Now if the sigma is NOT followed by
3666 * /$ignorable_sequence$cased_letter/;
3667 * and it IS preceded by
3668 * /$cased_letter$ignorable_sequence/;
3669 * where $ignorable_sequence is
3670 * [\x{2010}\x{AD}\p{Mn}]*
3671 * and $cased_letter is
3672 * [\p{Ll}\p{Lo}\p{Lt}]
3673 * then it should be mapped to 0x03C2,
3674 * (GREEK SMALL LETTER FINAL SIGMA),
3675 * instead of staying 0x03A3.
3676 * See lib/unicore/SpecCase.txt.
3679 Copy(tmpbuf, d, ulen, U8);
3685 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3690 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3692 SvUTF8_off(TARG); /* decontaminate */
3693 sv_setsv_nomg(TARG, sv);
3698 s = (U8*)SvPV_force_nomg(sv, len);
3700 register U8 *send = s + len;
3702 if (IN_LOCALE_RUNTIME) {
3705 for (; s < send; s++)
3706 *s = toLOWER_LC(*s);
3709 for (; s < send; s++)
3723 register char *s = SvPV(sv,len);
3726 SvUTF8_off(TARG); /* decontaminate */
3728 (void)SvUPGRADE(TARG, SVt_PV);
3729 SvGROW(TARG, (len * 2) + 1);
3733 if (UTF8_IS_CONTINUED(*s)) {
3734 STRLEN ulen = UTF8SKIP(s);
3758 SvCUR_set(TARG, d - SvPVX(TARG));
3759 (void)SvPOK_only_UTF8(TARG);
3762 sv_setpvn(TARG, s, len);
3764 if (SvSMAGICAL(TARG))
3773 dSP; dMARK; dORIGMARK;
3775 register AV* av = (AV*)POPs;
3776 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3777 I32 arybase = PL_curcop->cop_arybase;
3780 if (SvTYPE(av) == SVt_PVAV) {
3781 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3783 for (svp = MARK + 1; svp <= SP; svp++) {
3788 if (max > AvMAX(av))
3791 while (++MARK <= SP) {
3792 elem = SvIVx(*MARK);
3796 svp = av_fetch(av, elem, lval);
3798 if (!svp || *svp == &PL_sv_undef)
3799 DIE(aTHX_ PL_no_aelem, elem);
3800 if (PL_op->op_private & OPpLVAL_INTRO)
3801 save_aelem(av, elem, svp);
3803 *MARK = svp ? *svp : &PL_sv_undef;
3806 if (GIMME != G_ARRAY) {
3814 /* Associative arrays. */
3819 HV *hash = (HV*)POPs;
3821 I32 gimme = GIMME_V;
3824 /* might clobber stack_sp */
3825 entry = hv_iternext(hash);
3830 SV* sv = hv_iterkeysv(entry);
3831 PUSHs(sv); /* won't clobber stack_sp */
3832 if (gimme == G_ARRAY) {
3835 /* might clobber stack_sp */
3836 val = hv_iterval(hash, entry);
3841 else if (gimme == G_SCALAR)
3860 I32 gimme = GIMME_V;
3861 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3865 if (PL_op->op_private & OPpSLICE) {
3869 hvtype = SvTYPE(hv);
3870 if (hvtype == SVt_PVHV) { /* hash element */
3871 while (++MARK <= SP) {
3872 sv = hv_delete_ent(hv, *MARK, discard, 0);
3873 *MARK = sv ? sv : &PL_sv_undef;
3876 else if (hvtype == SVt_PVAV) { /* array element */
3877 if (PL_op->op_flags & OPf_SPECIAL) {
3878 while (++MARK <= SP) {
3879 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3880 *MARK = sv ? sv : &PL_sv_undef;
3885 DIE(aTHX_ "Not a HASH reference");
3888 else if (gimme == G_SCALAR) {
3897 if (SvTYPE(hv) == SVt_PVHV)
3898 sv = hv_delete_ent(hv, keysv, discard, 0);
3899 else if (SvTYPE(hv) == SVt_PVAV) {
3900 if (PL_op->op_flags & OPf_SPECIAL)
3901 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3903 DIE(aTHX_ "panic: avhv_delete no longer supported");
3906 DIE(aTHX_ "Not a HASH reference");
3921 if (PL_op->op_private & OPpEXISTS_SUB) {
3925 cv = sv_2cv(sv, &hv, &gv, FALSE);
3928 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3934 if (SvTYPE(hv) == SVt_PVHV) {
3935 if (hv_exists_ent(hv, tmpsv, 0))
3938 else if (SvTYPE(hv) == SVt_PVAV) {
3939 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3940 if (av_exists((AV*)hv, SvIV(tmpsv)))
3945 DIE(aTHX_ "Not a HASH reference");
3952 dSP; dMARK; dORIGMARK;
3953 register HV *hv = (HV*)POPs;
3954 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3955 bool localizing = PL_op->op_private & OPpLVAL_INTRO ? TRUE : FALSE;
3956 bool other_magic = FALSE;
3962 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
3963 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
3964 /* Try to preserve the existenceness of a tied hash
3965 * element by using EXISTS and DELETE if possible.
3966 * Fallback to FETCH and STORE otherwise */
3967 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
3968 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
3969 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
3972 while (++MARK <= SP) {
3976 bool preeminent = FALSE;
3979 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
3980 hv_exists_ent(hv, keysv, 0);
3983 he = hv_fetch_ent(hv, keysv, lval, 0);
3984 svp = he ? &HeVAL(he) : 0;
3987 if (!svp || *svp == &PL_sv_undef) {
3989 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
3993 save_helem(hv, keysv, svp);
3996 char *key = SvPV(keysv, keylen);
3997 SAVEDELETE(hv, savepvn(key,keylen), keylen);
4001 *MARK = svp ? *svp : &PL_sv_undef;
4003 if (GIMME != G_ARRAY) {
4011 /* List operators. */
4016 if (GIMME != G_ARRAY) {
4018 *MARK = *SP; /* unwanted list, return last item */
4020 *MARK = &PL_sv_undef;
4029 SV **lastrelem = PL_stack_sp;
4030 SV **lastlelem = PL_stack_base + POPMARK;
4031 SV **firstlelem = PL_stack_base + POPMARK + 1;
4032 register SV **firstrelem = lastlelem + 1;
4033 I32 arybase = PL_curcop->cop_arybase;
4034 I32 lval = PL_op->op_flags & OPf_MOD;
4035 I32 is_something_there = lval;
4037 register I32 max = lastrelem - lastlelem;
4038 register SV **lelem;
4041 if (GIMME != G_ARRAY) {
4042 ix = SvIVx(*lastlelem);
4047 if (ix < 0 || ix >= max)
4048 *firstlelem = &PL_sv_undef;
4050 *firstlelem = firstrelem[ix];
4056 SP = firstlelem - 1;
4060 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4066 if (ix < 0 || ix >= max)
4067 *lelem = &PL_sv_undef;
4069 is_something_there = TRUE;
4070 if (!(*lelem = firstrelem[ix]))
4071 *lelem = &PL_sv_undef;
4074 if (is_something_there)
4077 SP = firstlelem - 1;
4083 dSP; dMARK; dORIGMARK;
4084 I32 items = SP - MARK;
4085 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
4086 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4093 dSP; dMARK; dORIGMARK;
4094 HV* hv = (HV*)sv_2mortal((SV*)newHV());
4098 SV *val = NEWSV(46, 0);
4100 sv_setsv(val, *++MARK);
4101 else if (ckWARN(WARN_MISC))
4102 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4103 (void)hv_store_ent(hv,key,val,0);
4112 dSP; dMARK; dORIGMARK;
4113 register AV *ary = (AV*)*++MARK;
4117 register I32 offset;
4118 register I32 length;
4125 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4126 *MARK-- = SvTIED_obj((SV*)ary, mg);
4130 call_method("SPLICE",GIMME_V);
4139 offset = i = SvIVx(*MARK);
4141 offset += AvFILLp(ary) + 1;
4143 offset -= PL_curcop->cop_arybase;
4145 DIE(aTHX_ PL_no_aelem, i);
4147 length = SvIVx(*MARK++);
4149 length += AvFILLp(ary) - offset + 1;
4155 length = AvMAX(ary) + 1; /* close enough to infinity */
4159 length = AvMAX(ary) + 1;
4161 if (offset > AvFILLp(ary) + 1) {
4162 if (ckWARN(WARN_MISC))
4163 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4164 offset = AvFILLp(ary) + 1;
4166 after = AvFILLp(ary) + 1 - (offset + length);
4167 if (after < 0) { /* not that much array */
4168 length += after; /* offset+length now in array */
4174 /* At this point, MARK .. SP-1 is our new LIST */
4177 diff = newlen - length;
4178 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4181 if (diff < 0) { /* shrinking the area */
4183 New(451, tmparyval, newlen, SV*); /* so remember insertion */
4184 Copy(MARK, tmparyval, newlen, SV*);
4187 MARK = ORIGMARK + 1;
4188 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4189 MEXTEND(MARK, length);
4190 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4192 EXTEND_MORTAL(length);
4193 for (i = length, dst = MARK; i; i--) {
4194 sv_2mortal(*dst); /* free them eventualy */
4201 *MARK = AvARRAY(ary)[offset+length-1];
4204 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4205 SvREFCNT_dec(*dst++); /* free them now */
4208 AvFILLp(ary) += diff;
4210 /* pull up or down? */
4212 if (offset < after) { /* easier to pull up */
4213 if (offset) { /* esp. if nothing to pull */
4214 src = &AvARRAY(ary)[offset-1];
4215 dst = src - diff; /* diff is negative */
4216 for (i = offset; i > 0; i--) /* can't trust Copy */
4220 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
4224 if (after) { /* anything to pull down? */
4225 src = AvARRAY(ary) + offset + length;
4226 dst = src + diff; /* diff is negative */
4227 Move(src, dst, after, SV*);
4229 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4230 /* avoid later double free */
4234 dst[--i] = &PL_sv_undef;
4237 for (src = tmparyval, dst = AvARRAY(ary) + offset;
4239 *dst = NEWSV(46, 0);
4240 sv_setsv(*dst++, *src++);
4242 Safefree(tmparyval);
4245 else { /* no, expanding (or same) */
4247 New(452, tmparyval, length, SV*); /* so remember deletion */
4248 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4251 if (diff > 0) { /* expanding */
4253 /* push up or down? */
4255 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4259 Move(src, dst, offset, SV*);
4261 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
4263 AvFILLp(ary) += diff;
4266 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4267 av_extend(ary, AvFILLp(ary) + diff);
4268 AvFILLp(ary) += diff;
4271 dst = AvARRAY(ary) + AvFILLp(ary);
4273 for (i = after; i; i--) {
4280 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
4281 *dst = NEWSV(46, 0);
4282 sv_setsv(*dst++, *src++);
4284 MARK = ORIGMARK + 1;
4285 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4287 Copy(tmparyval, MARK, length, SV*);
4289 EXTEND_MORTAL(length);
4290 for (i = length, dst = MARK; i; i--) {
4291 sv_2mortal(*dst); /* free them eventualy */
4295 Safefree(tmparyval);
4299 else if (length--) {
4300 *MARK = tmparyval[length];
4303 while (length-- > 0)
4304 SvREFCNT_dec(tmparyval[length]);
4306 Safefree(tmparyval);
4309 *MARK = &PL_sv_undef;
4317 dSP; dMARK; dORIGMARK; dTARGET;
4318 register AV *ary = (AV*)*++MARK;
4319 register SV *sv = &PL_sv_undef;
4322 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4323 *MARK-- = SvTIED_obj((SV*)ary, mg);
4327 call_method("PUSH",G_SCALAR|G_DISCARD);
4332 /* Why no pre-extend of ary here ? */
4333 for (++MARK; MARK <= SP; MARK++) {
4336 sv_setsv(sv, *MARK);
4341 PUSHi( AvFILL(ary) + 1 );
4349 SV *sv = av_pop(av);
4351 (void)sv_2mortal(sv);
4360 SV *sv = av_shift(av);
4365 (void)sv_2mortal(sv);
4372 dSP; dMARK; dORIGMARK; dTARGET;
4373 register AV *ary = (AV*)*++MARK;
4378 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4379 *MARK-- = SvTIED_obj((SV*)ary, mg);
4383 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4388 av_unshift(ary, SP - MARK);
4391 sv_setsv(sv, *++MARK);
4392 (void)av_store(ary, i++, sv);
4396 PUSHi( AvFILL(ary) + 1 );
4406 if (GIMME == G_ARRAY) {
4413 /* safe as long as stack cannot get extended in the above */
4418 register char *down;
4423 SvUTF8_off(TARG); /* decontaminate */
4425 do_join(TARG, &PL_sv_no, MARK, SP);
4427 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
4428 up = SvPV_force(TARG, len);
4430 if (DO_UTF8(TARG)) { /* first reverse each character */
4431 U8* s = (U8*)SvPVX(TARG);
4432 U8* send = (U8*)(s + len);
4434 if (UTF8_IS_INVARIANT(*s)) {
4439 if (!utf8_to_uvchr(s, 0))
4443 down = (char*)(s - 1);
4444 /* reverse this character */
4448 *down-- = (char)tmp;
4454 down = SvPVX(TARG) + len - 1;
4458 *down-- = (char)tmp;
4460 (void)SvPOK_only_UTF8(TARG);
4472 register IV limit = POPi; /* note, negative is forever */
4475 register char *s = SvPV(sv, len);
4476 bool do_utf8 = DO_UTF8(sv);
4477 char *strend = s + len;
4479 register REGEXP *rx;
4483 STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s);
4484 I32 maxiters = slen + 10;
4487 I32 origlimit = limit;
4490 AV *oldstack = PL_curstack;
4491 I32 gimme = GIMME_V;
4492 I32 oldsave = PL_savestack_ix;
4493 I32 make_mortal = 1;
4494 MAGIC *mg = (MAGIC *) NULL;
4497 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4502 DIE(aTHX_ "panic: pp_split");
4505 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4506 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4508 RX_MATCH_UTF8_set(rx, do_utf8);
4510 if (pm->op_pmreplroot) {
4512 ary = GvAVn((GV*)PAD_SVl(INT2PTR(PADOFFSET, pm->op_pmreplroot)));
4514 ary = GvAVn((GV*)pm->op_pmreplroot);
4517 else if (gimme != G_ARRAY)
4518 ary = GvAVn(PL_defgv);
4521 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4527 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4529 XPUSHs(SvTIED_obj((SV*)ary, mg));
4535 for (i = AvFILLp(ary); i >= 0; i--)
4536 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4538 /* temporarily switch stacks */
4539 SWITCHSTACK(PL_curstack, ary);
4540 PL_curstackinfo->si_stack = ary;
4544 base = SP - PL_stack_base;
4546 if (pm->op_pmflags & PMf_SKIPWHITE) {
4547 if (pm->op_pmflags & PMf_LOCALE) {
4548 while (isSPACE_LC(*s))
4556 if ((pm->op_pmflags & PMf_MULTILINE) != PL_multiline) {
4557 SAVEINT(PL_multiline);
4558 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4562 limit = maxiters + 2;
4563 if (pm->op_pmflags & PMf_WHITE) {
4566 while (m < strend &&
4567 !((pm->op_pmflags & PMf_LOCALE)
4568 ? isSPACE_LC(*m) : isSPACE(*m)))
4573 dstr = NEWSV(30, m-s);
4574 sv_setpvn(dstr, s, m-s);
4578 (void)SvUTF8_on(dstr);
4582 while (s < strend &&
4583 ((pm->op_pmflags & PMf_LOCALE)
4584 ? isSPACE_LC(*s) : isSPACE(*s)))
4588 else if (strEQ("^", rx->precomp)) {
4591 for (m = s; m < strend && *m != '\n'; m++) ;
4595 dstr = NEWSV(30, m-s);
4596 sv_setpvn(dstr, s, m-s);
4600 (void)SvUTF8_on(dstr);
4605 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4606 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4607 && (rx->reganch & ROPT_CHECK_ALL)
4608 && !(rx->reganch & ROPT_ANCH)) {
4609 int tail = (rx->reganch & RE_INTUIT_TAIL);
4610 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4613 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4615 char c = *SvPV(csv, n_a);
4618 for (m = s; m < strend && *m != c; m++) ;
4621 dstr = NEWSV(30, m-s);
4622 sv_setpvn(dstr, s, m-s);
4626 (void)SvUTF8_on(dstr);
4628 /* The rx->minlen is in characters but we want to step
4629 * s ahead by bytes. */
4631 s = (char*)utf8_hop((U8*)m, len);
4633 s = m + len; /* Fake \n at the end */
4638 while (s < strend && --limit &&
4639 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4640 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
4643 dstr = NEWSV(31, m-s);
4644 sv_setpvn(dstr, s, m-s);
4648 (void)SvUTF8_on(dstr);
4650 /* The rx->minlen is in characters but we want to step
4651 * s ahead by bytes. */
4653 s = (char*)utf8_hop((U8*)m, len);
4655 s = m + len; /* Fake \n at the end */
4660 maxiters += slen * rx->nparens;
4661 while (s < strend && --limit
4662 /* && (!rx->check_substr
4663 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
4665 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
4666 1 /* minend */, sv, NULL, 0))
4668 TAINT_IF(RX_MATCH_TAINTED(rx));
4669 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4674 strend = s + (strend - m);
4676 m = rx->startp[0] + orig;
4677 dstr = NEWSV(32, m-s);
4678 sv_setpvn(dstr, s, m-s);
4682 (void)SvUTF8_on(dstr);
4685 for (i = 1; i <= (I32)rx->nparens; i++) {
4686 s = rx->startp[i] + orig;
4687 m = rx->endp[i] + orig;
4689 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4690 parens that didn't match -- they should be set to
4691 undef, not the empty string */
4692 if (m >= orig && s >= orig) {
4693 dstr = NEWSV(33, m-s);
4694 sv_setpvn(dstr, s, m-s);
4697 dstr = &PL_sv_undef; /* undef, not "" */
4701 (void)SvUTF8_on(dstr);
4705 s = rx->endp[0] + orig;
4710 LEAVE_SCOPE(oldsave);
4711 iters = (SP - PL_stack_base) - base;
4712 if (iters > maxiters)
4713 DIE(aTHX_ "Split loop");
4715 /* keep field after final delim? */
4716 if (s < strend || (iters && origlimit)) {
4717 STRLEN l = strend - s;
4718 dstr = NEWSV(34, l);
4719 sv_setpvn(dstr, s, l);
4723 (void)SvUTF8_on(dstr);
4727 else if (!origlimit) {
4728 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
4729 if (TOPs && !make_mortal)
4738 SWITCHSTACK(ary, oldstack);
4739 PL_curstackinfo->si_stack = oldstack;
4740 if (SvSMAGICAL(ary)) {
4745 if (gimme == G_ARRAY) {
4747 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4755 call_method("PUSH",G_SCALAR|G_DISCARD);
4758 if (gimme == G_ARRAY) {
4759 /* EXTEND should not be needed - we just popped them */
4761 for (i=0; i < iters; i++) {
4762 SV **svp = av_fetch(ary, i, FALSE);
4763 PUSHs((svp) ? *svp : &PL_sv_undef);
4770 if (gimme == G_ARRAY)
4785 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4786 || SvTYPE(retsv) == SVt_PVCV) {
4787 retsv = refto(retsv);
4795 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");