3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "It's a big house this, and very peculiar. Always a bit more to discover,
13 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
16 /* This file contains general pp ("push/pop") functions that execute the
17 * opcodes that make up a perl program. A typical pp function expects to
18 * find its arguments on the stack, and usually pushes its results onto
19 * the stack, hence the 'pp' terminology. Each OP structure contains
20 * a pointer to the relevant pp_foo() function.
30 /* XXX I can't imagine anyone who doesn't have this actually _needs_
31 it, since pid_t is an integral type.
34 #ifdef NEED_GETPID_PROTO
35 extern Pid_t getpid (void);
39 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
40 * This switches them over to IEEE.
42 #if defined(LIBM_LIB_VERSION)
43 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
46 /* variations on pp_null */
51 if (GIMME_V == G_SCALAR)
67 if (PL_op->op_private & OPpLVAL_INTRO)
68 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
70 if (PL_op->op_flags & OPf_REF) {
74 if (GIMME == G_SCALAR)
75 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
80 if (gimme == G_ARRAY) {
81 const I32 maxarg = AvFILL((AV*)TARG) + 1;
83 if (SvMAGICAL(TARG)) {
85 for (i=0; i < (U32)maxarg; i++) {
86 SV **svp = av_fetch((AV*)TARG, i, FALSE);
87 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
91 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
95 else if (gimme == G_SCALAR) {
96 SV* sv = sv_newmortal();
97 const I32 maxarg = AvFILL((AV*)TARG) + 1;
110 if (PL_op->op_private & OPpLVAL_INTRO)
111 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
112 if (PL_op->op_flags & OPf_REF)
115 if (GIMME == G_SCALAR)
116 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
120 if (gimme == G_ARRAY) {
123 else if (gimme == G_SCALAR) {
124 SV* sv = Perl_hv_scalar(aTHX_ (HV*)TARG);
132 DIE(aTHX_ "NOT IMPL LINE %d",__LINE__);
143 tryAMAGICunDEREF(to_gv);
146 if (SvTYPE(sv) == SVt_PVIO) {
147 GV *gv = (GV*) sv_newmortal();
148 gv_init(gv, 0, "", 0, 0);
149 GvIOp(gv) = (IO *)sv;
150 (void)SvREFCNT_inc(sv);
153 else if (SvTYPE(sv) != SVt_PVGV)
154 DIE(aTHX_ "Not a GLOB reference");
157 if (SvTYPE(sv) != SVt_PVGV) {
161 if (SvGMAGICAL(sv)) {
166 if (!SvOK(sv) && sv != &PL_sv_undef) {
167 /* If this is a 'my' scalar and flag is set then vivify
171 Perl_croak(aTHX_ PL_no_modify);
172 if (PL_op->op_private & OPpDEREF) {
174 if (cUNOP->op_targ) {
176 SV *namesv = PAD_SV(cUNOP->op_targ);
177 const char *name = SvPV(namesv, len);
178 gv = (GV*)NEWSV(0,0);
179 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
182 const char *name = CopSTASHPV(PL_curcop);
185 if (SvTYPE(sv) < SVt_RV)
186 sv_upgrade(sv, SVt_RV);
187 if (SvPVX_const(sv)) {
192 SvRV_set(sv, (SV*)gv);
197 if (PL_op->op_flags & OPf_REF ||
198 PL_op->op_private & HINT_STRICT_REFS)
199 DIE(aTHX_ PL_no_usym, "a symbol");
200 if (ckWARN(WARN_UNINITIALIZED))
205 if ((PL_op->op_flags & OPf_SPECIAL) &&
206 !(PL_op->op_flags & OPf_MOD))
208 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
210 && (!is_gv_magical(sym,len,0)
211 || !(sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV))))
217 if (PL_op->op_private & HINT_STRICT_REFS)
218 DIE(aTHX_ PL_no_symref, sym, "a symbol");
219 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
223 if (PL_op->op_private & OPpLVAL_INTRO)
224 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
236 tryAMAGICunDEREF(to_sv);
239 switch (SvTYPE(sv)) {
243 DIE(aTHX_ "Not a SCALAR reference");
251 if (SvTYPE(gv) != SVt_PVGV) {
252 if (SvGMAGICAL(sv)) {
258 if (PL_op->op_flags & OPf_REF ||
259 PL_op->op_private & HINT_STRICT_REFS)
260 DIE(aTHX_ PL_no_usym, "a SCALAR");
261 if (ckWARN(WARN_UNINITIALIZED))
266 if ((PL_op->op_flags & OPf_SPECIAL) &&
267 !(PL_op->op_flags & OPf_MOD))
269 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
271 && (!is_gv_magical(sym,len,0)
272 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV))))
278 if (PL_op->op_private & HINT_STRICT_REFS)
279 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
280 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
285 if (PL_op->op_flags & OPf_MOD) {
286 if (PL_op->op_private & OPpLVAL_INTRO) {
287 if (cUNOP->op_first->op_type == OP_NULL)
288 sv = save_scalar((GV*)TOPs);
290 sv = save_scalar(gv);
292 Perl_croak(aTHX_ PL_no_localize_ref);
294 else if (PL_op->op_private & OPpDEREF)
295 vivify_ref(sv, PL_op->op_private & OPpDEREF);
305 SV *sv = AvARYLEN(av);
307 AvARYLEN(av) = sv = NEWSV(0,0);
308 sv_upgrade(sv, SVt_IV);
309 sv_magic(sv, (SV*)av, PERL_MAGIC_arylen, Nullch, 0);
317 dSP; dTARGET; dPOPss;
319 if (PL_op->op_flags & OPf_MOD || LVRET) {
320 if (SvTYPE(TARG) < SVt_PVLV) {
321 sv_upgrade(TARG, SVt_PVLV);
322 sv_magic(TARG, Nullsv, PERL_MAGIC_pos, Nullch, 0);
326 if (LvTARG(TARG) != sv) {
328 SvREFCNT_dec(LvTARG(TARG));
329 LvTARG(TARG) = SvREFCNT_inc(sv);
331 PUSHs(TARG); /* no SvSETMAGIC */
337 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
338 mg = mg_find(sv, PERL_MAGIC_regex_global);
339 if (mg && mg->mg_len >= 0) {
343 PUSHi(i + PL_curcop->cop_arybase);
357 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
358 /* (But not in defined().) */
359 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
362 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
363 if ((PL_op->op_private & OPpLVAL_INTRO)) {
364 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
367 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
371 cv = (CV*)&PL_sv_undef;
385 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
386 const char *s = SvPVX_const(TOPs);
387 if (strnEQ(s, "CORE::", 6)) {
388 const int code = keyword(s + 6, SvCUR(TOPs) - 6);
389 if (code < 0) { /* Overridable. */
390 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
391 int i = 0, n = 0, seen_question = 0;
393 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
395 if (code == -KEY_chop || code == -KEY_chomp)
397 while (i < MAXO) { /* The slow way. */
398 if (strEQ(s + 6, PL_op_name[i])
399 || strEQ(s + 6, PL_op_desc[i]))
405 goto nonesuch; /* Should not happen... */
407 oa = PL_opargs[i] >> OASHIFT;
409 if (oa & OA_OPTIONAL && !seen_question) {
413 else if (n && str[0] == ';' && seen_question)
414 goto set; /* XXXX system, exec */
415 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
416 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
417 /* But globs are already references (kinda) */
418 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
422 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
426 ret = sv_2mortal(newSVpvn(str, n - 1));
428 else if (code) /* Non-Overridable */
430 else { /* None such */
432 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
436 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
438 ret = sv_2mortal(newSVpvn(SvPVX_const(cv), SvCUR(cv)));
447 CV* cv = (CV*)PAD_SV(PL_op->op_targ);
449 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
465 if (GIMME != G_ARRAY) {
469 *MARK = &PL_sv_undef;
470 *MARK = refto(*MARK);
474 EXTEND_MORTAL(SP - MARK);
476 *MARK = refto(*MARK);
481 S_refto(pTHX_ SV *sv)
485 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
488 if (!(sv = LvTARG(sv)))
491 (void)SvREFCNT_inc(sv);
493 else if (SvTYPE(sv) == SVt_PVAV) {
494 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
497 (void)SvREFCNT_inc(sv);
499 else if (SvPADTMP(sv) && !IS_PADGV(sv))
503 (void)SvREFCNT_inc(sv);
506 sv_upgrade(rv, SVt_RV);
520 if (sv && SvGMAGICAL(sv))
523 if (!sv || !SvROK(sv))
527 pv = sv_reftype(sv,TRUE);
528 PUSHp(pv, strlen(pv));
538 stash = CopSTASH(PL_curcop);
544 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
545 Perl_croak(aTHX_ "Attempt to bless into a reference");
547 if (ckWARN(WARN_MISC) && len == 0)
548 Perl_warner(aTHX_ packWARN(WARN_MISC),
549 "Explicit blessing to '' (assuming package main)");
550 stash = gv_stashpvn(ptr, len, TRUE);
553 (void)sv_bless(TOPs, stash);
567 elem = SvPV(sv, n_a);
572 /* elem will always be NUL terminated. */
573 const char *elem2 = elem + 1;
576 if (strEQ(elem2, "RRAY"))
577 tmpRef = (SV*)GvAV(gv);
580 if (strEQ(elem2, "ODE"))
581 tmpRef = (SV*)GvCVu(gv);
584 if (strEQ(elem2, "ILEHANDLE")) {
585 /* finally deprecated in 5.8.0 */
586 deprecate("*glob{FILEHANDLE}");
587 tmpRef = (SV*)GvIOp(gv);
590 if (strEQ(elem2, "ORMAT"))
591 tmpRef = (SV*)GvFORM(gv);
594 if (strEQ(elem2, "LOB"))
598 if (strEQ(elem2, "ASH"))
599 tmpRef = (SV*)GvHV(gv);
602 if (*elem2 == 'O' && !elem[2])
603 tmpRef = (SV*)GvIOp(gv);
606 if (strEQ(elem2, "AME"))
607 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
610 if (strEQ(elem2, "ACKAGE")) {
611 const char *name = HvNAME_get(GvSTASH(gv));
612 sv = newSVpv(name ? name : "__ANON__", 0);
616 if (strEQ(elem2, "CALAR"))
631 /* Pattern matching */
636 register unsigned char *s;
639 register I32 *sfirst;
643 if (sv == PL_lastscream) {
649 SvSCREAM_off(PL_lastscream);
650 SvREFCNT_dec(PL_lastscream);
652 PL_lastscream = SvREFCNT_inc(sv);
655 s = (unsigned char*)(SvPV(sv, len));
659 if (pos > PL_maxscream) {
660 if (PL_maxscream < 0) {
661 PL_maxscream = pos + 80;
662 New(301, PL_screamfirst, 256, I32);
663 New(302, PL_screamnext, PL_maxscream, I32);
666 PL_maxscream = pos + pos / 4;
667 Renew(PL_screamnext, PL_maxscream, I32);
671 sfirst = PL_screamfirst;
672 snext = PL_screamnext;
674 if (!sfirst || !snext)
675 DIE(aTHX_ "do_study: out of memory");
677 for (ch = 256; ch; --ch)
684 snext[pos] = sfirst[ch] - pos;
691 /* piggyback on m//g magic */
692 sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0);
701 if (PL_op->op_flags & OPf_STACKED)
707 TARG = sv_newmortal();
712 /* Lvalue operators. */
724 dSP; dMARK; dTARGET; dORIGMARK;
726 do_chop(TARG, *++MARK);
735 SETi(do_chomp(TOPs));
742 register I32 count = 0;
745 count += do_chomp(POPs);
756 if (!sv || !SvANY(sv))
758 switch (SvTYPE(sv)) {
760 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
761 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
765 if (HvARRAY(sv) || SvGMAGICAL(sv)
766 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
770 if (CvROOT(sv) || CvXSUB(sv))
787 if (!PL_op->op_private) {
796 if (SvTHINKFIRST(sv))
799 switch (SvTYPE(sv)) {
809 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
810 Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
811 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
815 /* let user-undef'd sub keep its identity */
816 GV* gv = CvGV((CV*)sv);
823 SvSetMagicSV(sv, &PL_sv_undef);
827 Newz(602, gp, 1, GP);
828 GvGP(sv) = gp_ref(gp);
829 GvSV(sv) = NEWSV(72,0);
830 GvLINE(sv) = CopLINE(PL_curcop);
836 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
838 SvPV_set(sv, Nullch);
851 if (SvTYPE(TOPs) > SVt_PVLV)
852 DIE(aTHX_ PL_no_modify);
853 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
854 && SvIVX(TOPs) != IV_MIN)
856 SvIV_set(TOPs, SvIVX(TOPs) - 1);
857 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
868 if (SvTYPE(TOPs) > SVt_PVLV)
869 DIE(aTHX_ PL_no_modify);
870 sv_setsv(TARG, TOPs);
871 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
872 && SvIVX(TOPs) != IV_MAX)
874 SvIV_set(TOPs, SvIVX(TOPs) + 1);
875 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
880 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
890 if (SvTYPE(TOPs) > SVt_PVLV)
891 DIE(aTHX_ PL_no_modify);
892 sv_setsv(TARG, TOPs);
893 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
894 && SvIVX(TOPs) != IV_MIN)
896 SvIV_set(TOPs, SvIVX(TOPs) - 1);
897 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
906 /* Ordinary operators. */
911 #ifdef PERL_PRESERVE_IVUV
914 tryAMAGICbin(pow,opASSIGN);
915 #ifdef PERL_PRESERVE_IVUV
916 /* For integer to integer power, we do the calculation by hand wherever
917 we're sure it is safe; otherwise we call pow() and try to convert to
918 integer afterwards. */
922 bool baseuok = SvUOK(TOPm1s);
926 baseuv = SvUVX(TOPm1s);
928 IV iv = SvIVX(TOPm1s);
931 baseuok = TRUE; /* effectively it's a UV now */
933 baseuv = -iv; /* abs, baseuok == false records sign */
947 goto float_it; /* Can't do negative powers this way. */
950 /* now we have integer ** positive integer. */
953 /* foo & (foo - 1) is zero only for a power of 2. */
954 if (!(baseuv & (baseuv - 1))) {
955 /* We are raising power-of-2 to a positive integer.
956 The logic here will work for any base (even non-integer
957 bases) but it can be less accurate than
958 pow (base,power) or exp (power * log (base)) when the
959 intermediate values start to spill out of the mantissa.
960 With powers of 2 we know this can't happen.
961 And powers of 2 are the favourite thing for perl
962 programmers to notice ** not doing what they mean. */
964 NV base = baseuok ? baseuv : -(NV)baseuv;
967 for (; power; base *= base, n++) {
968 /* Do I look like I trust gcc with long longs here?
970 UV bit = (UV)1 << (UV)n;
973 /* Only bother to clear the bit if it is set. */
975 /* Avoid squaring base again if we're done. */
976 if (power == 0) break;
984 register unsigned int highbit = 8 * sizeof(UV);
985 register unsigned int lowbit = 0;
986 register unsigned int diff;
987 bool odd_power = (bool)(power & 1);
988 while ((diff = (highbit - lowbit) >> 1)) {
989 if (baseuv & ~((1 << (lowbit + diff)) - 1))
994 /* we now have baseuv < 2 ** highbit */
995 if (power * highbit <= 8 * sizeof(UV)) {
996 /* result will definitely fit in UV, so use UV math
997 on same algorithm as above */
998 register UV result = 1;
999 register UV base = baseuv;
1001 for (; power; base *= base, n++) {
1002 register UV bit = (UV)1 << (UV)n;
1006 if (power == 0) break;
1010 if (baseuok || !odd_power)
1011 /* answer is positive */
1013 else if (result <= (UV)IV_MAX)
1014 /* answer negative, fits in IV */
1015 SETi( -(IV)result );
1016 else if (result == (UV)IV_MIN)
1017 /* 2's complement assumption: special case IV_MIN */
1020 /* answer negative, doesn't fit */
1021 SETn( -(NV)result );
1032 SETn( Perl_pow( left, right) );
1033 #ifdef PERL_PRESERVE_IVUV
1043 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1044 #ifdef PERL_PRESERVE_IVUV
1047 /* Unless the left argument is integer in range we are going to have to
1048 use NV maths. Hence only attempt to coerce the right argument if
1049 we know the left is integer. */
1050 /* Left operand is defined, so is it IV? */
1051 SvIV_please(TOPm1s);
1052 if (SvIOK(TOPm1s)) {
1053 bool auvok = SvUOK(TOPm1s);
1054 bool buvok = SvUOK(TOPs);
1055 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1056 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1063 alow = SvUVX(TOPm1s);
1065 IV aiv = SvIVX(TOPm1s);
1068 auvok = TRUE; /* effectively it's a UV now */
1070 alow = -aiv; /* abs, auvok == false records sign */
1076 IV biv = SvIVX(TOPs);
1079 buvok = TRUE; /* effectively it's a UV now */
1081 blow = -biv; /* abs, buvok == false records sign */
1085 /* If this does sign extension on unsigned it's time for plan B */
1086 ahigh = alow >> (4 * sizeof (UV));
1088 bhigh = blow >> (4 * sizeof (UV));
1090 if (ahigh && bhigh) {
1091 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1092 which is overflow. Drop to NVs below. */
1093 } else if (!ahigh && !bhigh) {
1094 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1095 so the unsigned multiply cannot overflow. */
1096 UV product = alow * blow;
1097 if (auvok == buvok) {
1098 /* -ve * -ve or +ve * +ve gives a +ve result. */
1102 } else if (product <= (UV)IV_MIN) {
1103 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1104 /* -ve result, which could overflow an IV */
1106 SETi( -(IV)product );
1108 } /* else drop to NVs below. */
1110 /* One operand is large, 1 small */
1113 /* swap the operands */
1115 bhigh = blow; /* bhigh now the temp var for the swap */
1119 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1120 multiplies can't overflow. shift can, add can, -ve can. */
1121 product_middle = ahigh * blow;
1122 if (!(product_middle & topmask)) {
1123 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1125 product_middle <<= (4 * sizeof (UV));
1126 product_low = alow * blow;
1128 /* as for pp_add, UV + something mustn't get smaller.
1129 IIRC ANSI mandates this wrapping *behaviour* for
1130 unsigned whatever the actual representation*/
1131 product_low += product_middle;
1132 if (product_low >= product_middle) {
1133 /* didn't overflow */
1134 if (auvok == buvok) {
1135 /* -ve * -ve or +ve * +ve gives a +ve result. */
1137 SETu( product_low );
1139 } else if (product_low <= (UV)IV_MIN) {
1140 /* 2s complement assumption again */
1141 /* -ve result, which could overflow an IV */
1143 SETi( -(IV)product_low );
1145 } /* else drop to NVs below. */
1147 } /* product_middle too large */
1148 } /* ahigh && bhigh */
1149 } /* SvIOK(TOPm1s) */
1154 SETn( left * right );
1161 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1162 /* Only try to do UV divide first
1163 if ((SLOPPYDIVIDE is true) or
1164 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1166 The assumption is that it is better to use floating point divide
1167 whenever possible, only doing integer divide first if we can't be sure.
1168 If NV_PRESERVES_UV is true then we know at compile time that no UV
1169 can be too large to preserve, so don't need to compile the code to
1170 test the size of UVs. */
1173 # define PERL_TRY_UV_DIVIDE
1174 /* ensure that 20./5. == 4. */
1176 # ifdef PERL_PRESERVE_IVUV
1177 # ifndef NV_PRESERVES_UV
1178 # define PERL_TRY_UV_DIVIDE
1183 #ifdef PERL_TRY_UV_DIVIDE
1186 SvIV_please(TOPm1s);
1187 if (SvIOK(TOPm1s)) {
1188 bool left_non_neg = SvUOK(TOPm1s);
1189 bool right_non_neg = SvUOK(TOPs);
1193 if (right_non_neg) {
1194 right = SvUVX(TOPs);
1197 IV biv = SvIVX(TOPs);
1200 right_non_neg = TRUE; /* effectively it's a UV now */
1206 /* historically undef()/0 gives a "Use of uninitialized value"
1207 warning before dieing, hence this test goes here.
1208 If it were immediately before the second SvIV_please, then
1209 DIE() would be invoked before left was even inspected, so
1210 no inpsection would give no warning. */
1212 DIE(aTHX_ "Illegal division by zero");
1215 left = SvUVX(TOPm1s);
1218 IV aiv = SvIVX(TOPm1s);
1221 left_non_neg = TRUE; /* effectively it's a UV now */
1230 /* For sloppy divide we always attempt integer division. */
1232 /* Otherwise we only attempt it if either or both operands
1233 would not be preserved by an NV. If both fit in NVs
1234 we fall through to the NV divide code below. However,
1235 as left >= right to ensure integer result here, we know that
1236 we can skip the test on the right operand - right big
1237 enough not to be preserved can't get here unless left is
1240 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1243 /* Integer division can't overflow, but it can be imprecise. */
1244 UV result = left / right;
1245 if (result * right == left) {
1246 SP--; /* result is valid */
1247 if (left_non_neg == right_non_neg) {
1248 /* signs identical, result is positive. */
1252 /* 2s complement assumption */
1253 if (result <= (UV)IV_MIN)
1254 SETi( -(IV)result );
1256 /* It's exact but too negative for IV. */
1257 SETn( -(NV)result );
1260 } /* tried integer divide but it was not an integer result */
1261 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1262 } /* left wasn't SvIOK */
1263 } /* right wasn't SvIOK */
1264 #endif /* PERL_TRY_UV_DIVIDE */
1268 DIE(aTHX_ "Illegal division by zero");
1269 PUSHn( left / right );
1276 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1280 bool left_neg = FALSE;
1281 bool right_neg = FALSE;
1282 bool use_double = FALSE;
1283 bool dright_valid = FALSE;
1289 right_neg = !SvUOK(TOPs);
1291 right = SvUVX(POPs);
1293 IV biv = SvIVX(POPs);
1296 right_neg = FALSE; /* effectively it's a UV now */
1304 right_neg = dright < 0;
1307 if (dright < UV_MAX_P1) {
1308 right = U_V(dright);
1309 dright_valid = TRUE; /* In case we need to use double below. */
1315 /* At this point use_double is only true if right is out of range for
1316 a UV. In range NV has been rounded down to nearest UV and
1317 use_double false. */
1319 if (!use_double && SvIOK(TOPs)) {
1321 left_neg = !SvUOK(TOPs);
1325 IV aiv = SvIVX(POPs);
1328 left_neg = FALSE; /* effectively it's a UV now */
1337 left_neg = dleft < 0;
1341 /* This should be exactly the 5.6 behaviour - if left and right are
1342 both in range for UV then use U_V() rather than floor. */
1344 if (dleft < UV_MAX_P1) {
1345 /* right was in range, so is dleft, so use UVs not double.
1349 /* left is out of range for UV, right was in range, so promote
1350 right (back) to double. */
1352 /* The +0.5 is used in 5.6 even though it is not strictly
1353 consistent with the implicit +0 floor in the U_V()
1354 inside the #if 1. */
1355 dleft = Perl_floor(dleft + 0.5);
1358 dright = Perl_floor(dright + 0.5);
1368 DIE(aTHX_ "Illegal modulus zero");
1370 dans = Perl_fmod(dleft, dright);
1371 if ((left_neg != right_neg) && dans)
1372 dans = dright - dans;
1375 sv_setnv(TARG, dans);
1381 DIE(aTHX_ "Illegal modulus zero");
1384 if ((left_neg != right_neg) && ans)
1387 /* XXX may warn: unary minus operator applied to unsigned type */
1388 /* could change -foo to be (~foo)+1 instead */
1389 if (ans <= ~((UV)IV_MAX)+1)
1390 sv_setiv(TARG, ~ans+1);
1392 sv_setnv(TARG, -(NV)ans);
1395 sv_setuv(TARG, ans);
1404 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1414 count = IV_MAX; /* The best we can do? */
1425 else if (SvNOKp(sv)) {
1434 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1436 I32 items = SP - MARK;
1438 static const char oom_list_extend[] =
1439 "Out of memory during list extend";
1441 max = items * count;
1442 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1443 /* Did the max computation overflow? */
1444 if (items > 0 && max > 0 && (max < items || max < count))
1445 Perl_croak(aTHX_ oom_list_extend);
1450 /* This code was intended to fix 20010809.028:
1453 for (($x =~ /./g) x 2) {
1454 print chop; # "abcdabcd" expected as output.
1457 * but that change (#11635) broke this code:
1459 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1461 * I can't think of a better fix that doesn't introduce
1462 * an efficiency hit by copying the SVs. The stack isn't
1463 * refcounted, and mortalisation obviously doesn't
1464 * Do The Right Thing when the stack has more than
1465 * one pointer to the same mortal value.
1469 *SP = sv_2mortal(newSVsv(*SP));
1479 repeatcpy((char*)(MARK + items), (char*)MARK,
1480 items * sizeof(SV*), count - 1);
1483 else if (count <= 0)
1486 else { /* Note: mark already snarfed by pp_list */
1490 static const char oom_string_extend[] =
1491 "Out of memory during string extend";
1493 SvSetSV(TARG, tmpstr);
1494 SvPV_force(TARG, len);
1495 isutf = DO_UTF8(TARG);
1500 STRLEN max = (UV)count * len;
1501 if (len > ((MEM_SIZE)~0)/count)
1502 Perl_croak(aTHX_ oom_string_extend);
1503 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1504 SvGROW(TARG, max + 1);
1505 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1506 SvCUR_set(TARG, SvCUR(TARG) * count);
1508 *SvEND(TARG) = '\0';
1511 (void)SvPOK_only_UTF8(TARG);
1513 (void)SvPOK_only(TARG);
1515 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1516 /* The parser saw this as a list repeat, and there
1517 are probably several items on the stack. But we're
1518 in scalar context, and there's no pp_list to save us
1519 now. So drop the rest of the items -- robin@kitsite.com
1532 dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN);
1533 useleft = USE_LEFT(TOPm1s);
1534 #ifdef PERL_PRESERVE_IVUV
1535 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1536 "bad things" happen if you rely on signed integers wrapping. */
1539 /* Unless the left argument is integer in range we are going to have to
1540 use NV maths. Hence only attempt to coerce the right argument if
1541 we know the left is integer. */
1542 register UV auv = 0;
1548 a_valid = auvok = 1;
1549 /* left operand is undef, treat as zero. */
1551 /* Left operand is defined, so is it IV? */
1552 SvIV_please(TOPm1s);
1553 if (SvIOK(TOPm1s)) {
1554 if ((auvok = SvUOK(TOPm1s)))
1555 auv = SvUVX(TOPm1s);
1557 register IV aiv = SvIVX(TOPm1s);
1560 auvok = 1; /* Now acting as a sign flag. */
1561 } else { /* 2s complement assumption for IV_MIN */
1569 bool result_good = 0;
1572 bool buvok = SvUOK(TOPs);
1577 register IV biv = SvIVX(TOPs);
1584 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1585 else "IV" now, independent of how it came in.
1586 if a, b represents positive, A, B negative, a maps to -A etc
1591 all UV maths. negate result if A negative.
1592 subtract if signs same, add if signs differ. */
1594 if (auvok ^ buvok) {
1603 /* Must get smaller */
1608 if (result <= buv) {
1609 /* result really should be -(auv-buv). as its negation
1610 of true value, need to swap our result flag */
1622 if (result <= (UV)IV_MIN)
1623 SETi( -(IV)result );
1625 /* result valid, but out of range for IV. */
1626 SETn( -(NV)result );
1630 } /* Overflow, drop through to NVs. */
1634 useleft = USE_LEFT(TOPm1s);
1638 /* left operand is undef, treat as zero - value */
1642 SETn( TOPn - value );
1649 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1652 if (PL_op->op_private & HINT_INTEGER) {
1666 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1669 if (PL_op->op_private & HINT_INTEGER) {
1683 dSP; tryAMAGICbinSET(lt,0);
1684 #ifdef PERL_PRESERVE_IVUV
1687 SvIV_please(TOPm1s);
1688 if (SvIOK(TOPm1s)) {
1689 bool auvok = SvUOK(TOPm1s);
1690 bool buvok = SvUOK(TOPs);
1692 if (!auvok && !buvok) { /* ## IV < IV ## */
1693 IV aiv = SvIVX(TOPm1s);
1694 IV biv = SvIVX(TOPs);
1697 SETs(boolSV(aiv < biv));
1700 if (auvok && buvok) { /* ## UV < UV ## */
1701 UV auv = SvUVX(TOPm1s);
1702 UV buv = SvUVX(TOPs);
1705 SETs(boolSV(auv < buv));
1708 if (auvok) { /* ## UV < IV ## */
1715 /* As (a) is a UV, it's >=0, so it cannot be < */
1720 SETs(boolSV(auv < (UV)biv));
1723 { /* ## IV < UV ## */
1727 aiv = SvIVX(TOPm1s);
1729 /* As (b) is a UV, it's >=0, so it must be < */
1736 SETs(boolSV((UV)aiv < buv));
1742 #ifndef NV_PRESERVES_UV
1743 #ifdef PERL_PRESERVE_IVUV
1746 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1748 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1754 SETs(boolSV(TOPn < value));
1761 dSP; tryAMAGICbinSET(gt,0);
1762 #ifdef PERL_PRESERVE_IVUV
1765 SvIV_please(TOPm1s);
1766 if (SvIOK(TOPm1s)) {
1767 bool auvok = SvUOK(TOPm1s);
1768 bool buvok = SvUOK(TOPs);
1770 if (!auvok && !buvok) { /* ## IV > IV ## */
1771 IV aiv = SvIVX(TOPm1s);
1772 IV biv = SvIVX(TOPs);
1775 SETs(boolSV(aiv > biv));
1778 if (auvok && buvok) { /* ## UV > UV ## */
1779 UV auv = SvUVX(TOPm1s);
1780 UV buv = SvUVX(TOPs);
1783 SETs(boolSV(auv > buv));
1786 if (auvok) { /* ## UV > IV ## */
1793 /* As (a) is a UV, it's >=0, so it must be > */
1798 SETs(boolSV(auv > (UV)biv));
1801 { /* ## IV > UV ## */
1805 aiv = SvIVX(TOPm1s);
1807 /* As (b) is a UV, it's >=0, so it cannot be > */
1814 SETs(boolSV((UV)aiv > buv));
1820 #ifndef NV_PRESERVES_UV
1821 #ifdef PERL_PRESERVE_IVUV
1824 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1826 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1832 SETs(boolSV(TOPn > value));
1839 dSP; tryAMAGICbinSET(le,0);
1840 #ifdef PERL_PRESERVE_IVUV
1843 SvIV_please(TOPm1s);
1844 if (SvIOK(TOPm1s)) {
1845 bool auvok = SvUOK(TOPm1s);
1846 bool buvok = SvUOK(TOPs);
1848 if (!auvok && !buvok) { /* ## IV <= IV ## */
1849 IV aiv = SvIVX(TOPm1s);
1850 IV biv = SvIVX(TOPs);
1853 SETs(boolSV(aiv <= biv));
1856 if (auvok && buvok) { /* ## UV <= UV ## */
1857 UV auv = SvUVX(TOPm1s);
1858 UV buv = SvUVX(TOPs);
1861 SETs(boolSV(auv <= buv));
1864 if (auvok) { /* ## UV <= IV ## */
1871 /* As (a) is a UV, it's >=0, so a cannot be <= */
1876 SETs(boolSV(auv <= (UV)biv));
1879 { /* ## IV <= UV ## */
1883 aiv = SvIVX(TOPm1s);
1885 /* As (b) is a UV, it's >=0, so a must be <= */
1892 SETs(boolSV((UV)aiv <= buv));
1898 #ifndef NV_PRESERVES_UV
1899 #ifdef PERL_PRESERVE_IVUV
1902 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1904 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1910 SETs(boolSV(TOPn <= value));
1917 dSP; tryAMAGICbinSET(ge,0);
1918 #ifdef PERL_PRESERVE_IVUV
1921 SvIV_please(TOPm1s);
1922 if (SvIOK(TOPm1s)) {
1923 bool auvok = SvUOK(TOPm1s);
1924 bool buvok = SvUOK(TOPs);
1926 if (!auvok && !buvok) { /* ## IV >= IV ## */
1927 IV aiv = SvIVX(TOPm1s);
1928 IV biv = SvIVX(TOPs);
1931 SETs(boolSV(aiv >= biv));
1934 if (auvok && buvok) { /* ## UV >= UV ## */
1935 UV auv = SvUVX(TOPm1s);
1936 UV buv = SvUVX(TOPs);
1939 SETs(boolSV(auv >= buv));
1942 if (auvok) { /* ## UV >= IV ## */
1949 /* As (a) is a UV, it's >=0, so it must be >= */
1954 SETs(boolSV(auv >= (UV)biv));
1957 { /* ## IV >= UV ## */
1961 aiv = SvIVX(TOPm1s);
1963 /* As (b) is a UV, it's >=0, so a cannot be >= */
1970 SETs(boolSV((UV)aiv >= buv));
1976 #ifndef NV_PRESERVES_UV
1977 #ifdef PERL_PRESERVE_IVUV
1980 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1982 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
1988 SETs(boolSV(TOPn >= value));
1995 dSP; tryAMAGICbinSET(ne,0);
1996 #ifndef NV_PRESERVES_UV
1997 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1999 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2003 #ifdef PERL_PRESERVE_IVUV
2006 SvIV_please(TOPm1s);
2007 if (SvIOK(TOPm1s)) {
2008 bool auvok = SvUOK(TOPm1s);
2009 bool buvok = SvUOK(TOPs);
2011 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2012 /* Casting IV to UV before comparison isn't going to matter
2013 on 2s complement. On 1s complement or sign&magnitude
2014 (if we have any of them) it could make negative zero
2015 differ from normal zero. As I understand it. (Need to
2016 check - is negative zero implementation defined behaviour
2018 UV buv = SvUVX(POPs);
2019 UV auv = SvUVX(TOPs);
2021 SETs(boolSV(auv != buv));
2024 { /* ## Mixed IV,UV ## */
2028 /* != is commutative so swap if needed (save code) */
2030 /* swap. top of stack (b) is the iv */
2034 /* As (a) is a UV, it's >0, so it cannot be == */
2043 /* As (b) is a UV, it's >0, so it cannot be == */
2047 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2049 SETs(boolSV((UV)iv != uv));
2057 SETs(boolSV(TOPn != value));
2064 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2065 #ifndef NV_PRESERVES_UV
2066 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2067 UV right = PTR2UV(SvRV(POPs));
2068 UV left = PTR2UV(SvRV(TOPs));
2069 SETi((left > right) - (left < right));
2073 #ifdef PERL_PRESERVE_IVUV
2074 /* Fortunately it seems NaN isn't IOK */
2077 SvIV_please(TOPm1s);
2078 if (SvIOK(TOPm1s)) {
2079 bool leftuvok = SvUOK(TOPm1s);
2080 bool rightuvok = SvUOK(TOPs);
2082 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2083 IV leftiv = SvIVX(TOPm1s);
2084 IV rightiv = SvIVX(TOPs);
2086 if (leftiv > rightiv)
2088 else if (leftiv < rightiv)
2092 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2093 UV leftuv = SvUVX(TOPm1s);
2094 UV rightuv = SvUVX(TOPs);
2096 if (leftuv > rightuv)
2098 else if (leftuv < rightuv)
2102 } else if (leftuvok) { /* ## UV <=> IV ## */
2106 rightiv = SvIVX(TOPs);
2108 /* As (a) is a UV, it's >=0, so it cannot be < */
2111 leftuv = SvUVX(TOPm1s);
2112 if (leftuv > (UV)rightiv) {
2114 } else if (leftuv < (UV)rightiv) {
2120 } else { /* ## IV <=> UV ## */
2124 leftiv = SvIVX(TOPm1s);
2126 /* As (b) is a UV, it's >=0, so it must be < */
2129 rightuv = SvUVX(TOPs);
2130 if ((UV)leftiv > rightuv) {
2132 } else if ((UV)leftiv < rightuv) {
2150 if (Perl_isnan(left) || Perl_isnan(right)) {
2154 value = (left > right) - (left < right);
2158 else if (left < right)
2160 else if (left > right)
2174 dSP; tryAMAGICbinSET(slt,0);
2177 int cmp = (IN_LOCALE_RUNTIME
2178 ? sv_cmp_locale(left, right)
2179 : sv_cmp(left, right));
2180 SETs(boolSV(cmp < 0));
2187 dSP; tryAMAGICbinSET(sgt,0);
2190 int cmp = (IN_LOCALE_RUNTIME
2191 ? sv_cmp_locale(left, right)
2192 : sv_cmp(left, right));
2193 SETs(boolSV(cmp > 0));
2200 dSP; tryAMAGICbinSET(sle,0);
2203 int cmp = (IN_LOCALE_RUNTIME
2204 ? sv_cmp_locale(left, right)
2205 : sv_cmp(left, right));
2206 SETs(boolSV(cmp <= 0));
2213 dSP; tryAMAGICbinSET(sge,0);
2216 int cmp = (IN_LOCALE_RUNTIME
2217 ? sv_cmp_locale(left, right)
2218 : sv_cmp(left, right));
2219 SETs(boolSV(cmp >= 0));
2226 dSP; tryAMAGICbinSET(seq,0);
2229 SETs(boolSV(sv_eq(left, right)));
2236 dSP; tryAMAGICbinSET(sne,0);
2239 SETs(boolSV(!sv_eq(left, right)));
2246 dSP; dTARGET; tryAMAGICbin(scmp,0);
2249 int cmp = (IN_LOCALE_RUNTIME
2250 ? sv_cmp_locale(left, right)
2251 : sv_cmp(left, right));
2259 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2262 if (SvNIOKp(left) || SvNIOKp(right)) {
2263 if (PL_op->op_private & HINT_INTEGER) {
2264 IV i = SvIV(left) & SvIV(right);
2268 UV u = SvUV(left) & SvUV(right);
2273 do_vop(PL_op->op_type, TARG, left, right);
2282 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
2285 if (SvNIOKp(left) || SvNIOKp(right)) {
2286 if (PL_op->op_private & HINT_INTEGER) {
2287 IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
2291 UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
2296 do_vop(PL_op->op_type, TARG, left, right);
2305 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
2308 if (SvNIOKp(left) || SvNIOKp(right)) {
2309 if (PL_op->op_private & HINT_INTEGER) {
2310 IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
2314 UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
2319 do_vop(PL_op->op_type, TARG, left, right);
2328 dSP; dTARGET; tryAMAGICun(neg);
2331 int flags = SvFLAGS(sv);
2334 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2335 /* It's publicly an integer, or privately an integer-not-float */
2338 if (SvIVX(sv) == IV_MIN) {
2339 /* 2s complement assumption. */
2340 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2343 else if (SvUVX(sv) <= IV_MAX) {
2348 else if (SvIVX(sv) != IV_MIN) {
2352 #ifdef PERL_PRESERVE_IVUV
2361 else if (SvPOKp(sv)) {
2363 char *s = SvPV(sv, len);
2364 if (isIDFIRST(*s)) {
2365 sv_setpvn(TARG, "-", 1);
2368 else if (*s == '+' || *s == '-') {
2370 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2372 else if (DO_UTF8(sv)) {
2375 goto oops_its_an_int;
2377 sv_setnv(TARG, -SvNV(sv));
2379 sv_setpvn(TARG, "-", 1);
2386 goto oops_its_an_int;
2387 sv_setnv(TARG, -SvNV(sv));
2399 dSP; tryAMAGICunSET(not);
2400 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2406 dSP; dTARGET; tryAMAGICun(compl);
2410 if (PL_op->op_private & HINT_INTEGER) {
2424 (void)SvPV_nomg(sv,len); /* force check for uninit var */
2426 tmps = (U8*)SvPV_force(TARG, len);
2429 /* Calculate exact length, let's not estimate. */
2438 while (tmps < send) {
2439 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2440 tmps += UTF8SKIP(tmps);
2441 targlen += UNISKIP(~c);
2447 /* Now rewind strings and write them. */
2451 Newz(0, result, targlen + 1, U8);
2452 while (tmps < send) {
2453 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2454 tmps += UTF8SKIP(tmps);
2455 result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY);
2459 sv_setpvn(TARG, (char*)result, targlen);
2463 Newz(0, result, nchar + 1, U8);
2464 while (tmps < send) {
2465 U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY);
2466 tmps += UTF8SKIP(tmps);
2471 sv_setpvn(TARG, (char*)result, nchar);
2480 register long *tmpl;
2481 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2484 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
2489 for ( ; anum > 0; anum--, tmps++)
2498 /* integer versions of some of the above */
2502 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2505 SETi( left * right );
2512 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2516 DIE(aTHX_ "Illegal division by zero");
2517 value = POPi / value;
2526 /* This is the vanilla old i_modulo. */
2527 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2531 DIE(aTHX_ "Illegal modulus zero");
2532 SETi( left % right );
2537 #if defined(__GLIBC__) && IVSIZE == 8
2541 /* This is the i_modulo with the workaround for the _moddi3 bug
2542 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2543 * See below for pp_i_modulo. */
2544 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2548 DIE(aTHX_ "Illegal modulus zero");
2549 SETi( left % PERL_ABS(right) );
2557 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2561 DIE(aTHX_ "Illegal modulus zero");
2562 /* The assumption is to use hereafter the old vanilla version... */
2564 PL_ppaddr[OP_I_MODULO] =
2565 &Perl_pp_i_modulo_0;
2566 /* .. but if we have glibc, we might have a buggy _moddi3
2567 * (at least glicb 2.2.5 is known to have this bug), in other
2568 * words our integer modulus with negative quad as the second
2569 * argument might be broken. Test for this and re-patch the
2570 * opcode dispatch table if that is the case, remembering to
2571 * also apply the workaround so that this first round works
2572 * right, too. See [perl #9402] for more information. */
2573 #if defined(__GLIBC__) && IVSIZE == 8
2577 /* Cannot do this check with inlined IV constants since
2578 * that seems to work correctly even with the buggy glibc. */
2580 /* Yikes, we have the bug.
2581 * Patch in the workaround version. */
2583 PL_ppaddr[OP_I_MODULO] =
2584 &Perl_pp_i_modulo_1;
2585 /* Make certain we work right this time, too. */
2586 right = PERL_ABS(right);
2590 SETi( left % right );
2597 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2600 SETi( left + right );
2607 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2610 SETi( left - right );
2617 dSP; tryAMAGICbinSET(lt,0);
2620 SETs(boolSV(left < right));
2627 dSP; tryAMAGICbinSET(gt,0);
2630 SETs(boolSV(left > right));
2637 dSP; tryAMAGICbinSET(le,0);
2640 SETs(boolSV(left <= right));
2647 dSP; tryAMAGICbinSET(ge,0);
2650 SETs(boolSV(left >= right));
2657 dSP; tryAMAGICbinSET(eq,0);
2660 SETs(boolSV(left == right));
2667 dSP; tryAMAGICbinSET(ne,0);
2670 SETs(boolSV(left != right));
2677 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2684 else if (left < right)
2695 dSP; dTARGET; tryAMAGICun(neg);
2700 /* High falutin' math. */
2704 dSP; dTARGET; tryAMAGICbin(atan2,0);
2707 SETn(Perl_atan2(left, right));
2714 dSP; dTARGET; tryAMAGICun(sin);
2718 value = Perl_sin(value);
2726 dSP; dTARGET; tryAMAGICun(cos);
2730 value = Perl_cos(value);
2736 /* Support Configure command-line overrides for rand() functions.
2737 After 5.005, perhaps we should replace this by Configure support
2738 for drand48(), random(), or rand(). For 5.005, though, maintain
2739 compatibility by calling rand() but allow the user to override it.
2740 See INSTALL for details. --Andy Dougherty 15 July 1998
2742 /* Now it's after 5.005, and Configure supports drand48() and random(),
2743 in addition to rand(). So the overrides should not be needed any more.
2744 --Jarkko Hietaniemi 27 September 1998
2747 #ifndef HAS_DRAND48_PROTO
2748 extern double drand48 (void);
2761 if (!PL_srand_called) {
2762 (void)seedDrand01((Rand_seed_t)seed());
2763 PL_srand_called = TRUE;
2778 (void)seedDrand01((Rand_seed_t)anum);
2779 PL_srand_called = TRUE;
2786 dSP; dTARGET; tryAMAGICun(exp);
2790 value = Perl_exp(value);
2798 dSP; dTARGET; tryAMAGICun(log);
2803 SET_NUMERIC_STANDARD();
2804 DIE(aTHX_ "Can't take log of %"NVgf, value);
2806 value = Perl_log(value);
2814 dSP; dTARGET; tryAMAGICun(sqrt);
2819 SET_NUMERIC_STANDARD();
2820 DIE(aTHX_ "Can't take sqrt of %"NVgf, value);
2822 value = Perl_sqrt(value);
2830 dSP; dTARGET; tryAMAGICun(int);
2833 IV iv = TOPi; /* attempt to convert to IV if possible. */
2834 /* XXX it's arguable that compiler casting to IV might be subtly
2835 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2836 else preferring IV has introduced a subtle behaviour change bug. OTOH
2837 relying on floating point to be accurate is a bug. */
2841 else if (SvIOK(TOPs)) {
2850 if (value < (NV)UV_MAX + 0.5) {
2853 SETn(Perl_floor(value));
2857 if (value > (NV)IV_MIN - 0.5) {
2860 SETn(Perl_ceil(value));
2870 dSP; dTARGET; tryAMAGICun(abs);
2872 /* This will cache the NV value if string isn't actually integer */
2877 else if (SvIOK(TOPs)) {
2878 /* IVX is precise */
2880 SETu(TOPu); /* force it to be numeric only */
2888 /* 2s complement assumption. Also, not really needed as
2889 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2909 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2915 tmps = (SvPVx(sv, len));
2917 /* If Unicode, try to downgrade
2918 * If not possible, croak. */
2919 SV* tsv = sv_2mortal(newSVsv(sv));
2922 sv_utf8_downgrade(tsv, FALSE);
2925 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2926 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2939 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2945 tmps = (SvPVx(sv, len));
2947 /* If Unicode, try to downgrade
2948 * If not possible, croak. */
2949 SV* tsv = sv_2mortal(newSVsv(sv));
2952 sv_utf8_downgrade(tsv, FALSE);
2955 while (*tmps && len && isSPACE(*tmps))
2960 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2961 else if (*tmps == 'b')
2962 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2964 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2966 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2983 SETi(sv_len_utf8(sv));
2999 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3001 const I32 arybase = PL_curcop->cop_arybase;
3003 const char *repl = 0;
3005 int num_args = PL_op->op_private & 7;
3006 bool repl_need_utf8_upgrade = FALSE;
3007 bool repl_is_utf8 = FALSE;
3009 SvTAINTED_off(TARG); /* decontaminate */
3010 SvUTF8_off(TARG); /* decontaminate */
3014 repl = SvPV(repl_sv, repl_len);
3015 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3025 sv_utf8_upgrade(sv);
3027 else if (DO_UTF8(sv))
3028 repl_need_utf8_upgrade = TRUE;
3030 tmps = SvPV(sv, curlen);
3032 utf8_curlen = sv_len_utf8(sv);
3033 if (utf8_curlen == curlen)
3036 curlen = utf8_curlen;
3041 if (pos >= arybase) {
3059 else if (len >= 0) {
3061 if (rem > (I32)curlen)
3076 Perl_croak(aTHX_ "substr outside of string");
3077 if (ckWARN(WARN_SUBSTR))
3078 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3085 sv_pos_u2b(sv, &pos, &rem);
3087 /* we either return a PV or an LV. If the TARG hasn't been used
3088 * before, or is of that type, reuse it; otherwise use a mortal
3089 * instead. Note that LVs can have an extended lifetime, so also
3090 * dont reuse if refcount > 1 (bug #20933) */
3091 if (SvTYPE(TARG) > SVt_NULL) {
3092 if ( (SvTYPE(TARG) == SVt_PVLV)
3093 ? (!lvalue || SvREFCNT(TARG) > 1)
3096 TARG = sv_newmortal();
3100 sv_setpvn(TARG, tmps, rem);
3101 #ifdef USE_LOCALE_COLLATE
3102 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3107 SV* repl_sv_copy = NULL;
3109 if (repl_need_utf8_upgrade) {
3110 repl_sv_copy = newSVsv(repl_sv);
3111 sv_utf8_upgrade(repl_sv_copy);
3112 repl = SvPV(repl_sv_copy, repl_len);
3113 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3115 sv_insert(sv, pos, rem, repl, repl_len);
3119 SvREFCNT_dec(repl_sv_copy);
3121 else if (lvalue) { /* it's an lvalue! */
3122 if (!SvGMAGICAL(sv)) {
3126 if (ckWARN(WARN_SUBSTR))
3127 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3128 "Attempt to use reference as lvalue in substr");
3130 if (SvOK(sv)) /* is it defined ? */
3131 (void)SvPOK_only_UTF8(sv);
3133 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3136 if (SvTYPE(TARG) < SVt_PVLV) {
3137 sv_upgrade(TARG, SVt_PVLV);
3138 sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0);
3144 if (LvTARG(TARG) != sv) {
3146 SvREFCNT_dec(LvTARG(TARG));
3147 LvTARG(TARG) = SvREFCNT_inc(sv);
3149 LvTARGOFF(TARG) = upos;
3150 LvTARGLEN(TARG) = urem;
3154 PUSHs(TARG); /* avoid SvSETMAGIC here */
3161 register IV size = POPi;
3162 register IV offset = POPi;
3163 register SV *src = POPs;
3164 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3166 SvTAINTED_off(TARG); /* decontaminate */
3167 if (lvalue) { /* it's an lvalue! */
3168 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3169 TARG = sv_newmortal();
3170 if (SvTYPE(TARG) < SVt_PVLV) {
3171 sv_upgrade(TARG, SVt_PVLV);
3172 sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0);
3175 if (LvTARG(TARG) != src) {
3177 SvREFCNT_dec(LvTARG(TARG));
3178 LvTARG(TARG) = SvREFCNT_inc(src);
3180 LvTARGOFF(TARG) = offset;
3181 LvTARGLEN(TARG) = size;
3184 sv_setuv(TARG, do_vecget(src, offset, size));
3200 I32 arybase = PL_curcop->cop_arybase;
3207 offset = POPi - arybase;
3210 big_utf8 = DO_UTF8(big);
3211 little_utf8 = DO_UTF8(little);
3212 if (big_utf8 ^ little_utf8) {
3213 /* One needs to be upgraded. */
3214 SV *bytes = little_utf8 ? big : little;
3216 char *p = SvPV(bytes, len);
3218 temp = newSVpvn(p, len);
3221 sv_recode_to_utf8(temp, PL_encoding);
3223 sv_utf8_upgrade(temp);
3232 if (big_utf8 && offset > 0)
3233 sv_pos_u2b(big, &offset, 0);
3234 tmps = SvPV(big, biglen);
3237 else if (offset > (I32)biglen)
3239 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
3240 (unsigned char*)tmps + biglen, little, 0)))
3243 retval = tmps2 - tmps;
3244 if (retval > 0 && big_utf8)
3245 sv_pos_b2u(big, &retval);
3248 PUSHi(retval + arybase);
3264 I32 arybase = PL_curcop->cop_arybase;
3272 big_utf8 = DO_UTF8(big);
3273 little_utf8 = DO_UTF8(little);
3274 if (big_utf8 ^ little_utf8) {
3275 /* One needs to be upgraded. */
3276 SV *bytes = little_utf8 ? big : little;
3278 char *p = SvPV(bytes, len);
3280 temp = newSVpvn(p, len);
3283 sv_recode_to_utf8(temp, PL_encoding);
3285 sv_utf8_upgrade(temp);
3294 tmps2 = SvPV(little, llen);
3295 tmps = SvPV(big, blen);
3300 if (offset > 0 && big_utf8)
3301 sv_pos_u2b(big, &offset, 0);
3302 offset = offset - arybase + llen;
3306 else if (offset > (I32)blen)
3308 if (!(tmps2 = rninstr(tmps, tmps + offset,
3309 tmps2, tmps2 + llen)))
3312 retval = tmps2 - tmps;
3313 if (retval > 0 && big_utf8)
3314 sv_pos_b2u(big, &retval);
3317 PUSHi(retval + arybase);
3323 dSP; dMARK; dORIGMARK; dTARGET;
3324 do_sprintf(TARG, SP-MARK, MARK+1);
3325 TAINT_IF(SvTAINTED(TARG));
3326 if (DO_UTF8(*(MARK+1)))
3338 U8 *s = (U8*)SvPVx(argsv, len);
3341 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3342 tmpsv = sv_2mortal(newSVsv(argsv));
3343 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3347 XPUSHu(DO_UTF8(argsv) ?
3348 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3360 (void)SvUPGRADE(TARG,SVt_PV);
3362 if (value > 255 && !IN_BYTES) {
3363 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3364 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3365 SvCUR_set(TARG, tmps - SvPVX(TARG));
3367 (void)SvPOK_only(TARG);
3376 *tmps++ = (char)value;
3378 (void)SvPOK_only(TARG);
3379 if (PL_encoding && !IN_BYTES) {
3380 sv_recode_to_utf8(TARG, PL_encoding);
3382 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3383 memEQ(tmps, "\xef\xbf\xbd\0", 4)) {
3387 *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value);
3388 *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value);
3404 char *tmps = SvPV(left, len);
3406 if (DO_UTF8(left)) {
3407 /* If Unicode, try to downgrade.
3408 * If not possible, croak.
3409 * Yes, we made this up. */
3410 SV* tsv = sv_2mortal(newSVsv(left));
3413 sv_utf8_downgrade(tsv, FALSE);
3416 # ifdef USE_ITHREADS
3418 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3419 /* This should be threadsafe because in ithreads there is only
3420 * one thread per interpreter. If this would not be true,
3421 * we would need a mutex to protect this malloc. */
3422 PL_reentrant_buffer->_crypt_struct_buffer =
3423 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3424 #if defined(__GLIBC__) || defined(__EMX__)
3425 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3426 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3427 /* work around glibc-2.2.5 bug */
3428 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3432 # endif /* HAS_CRYPT_R */
3433 # endif /* USE_ITHREADS */
3435 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
3437 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
3443 "The crypt() function is unimplemented due to excessive paranoia.");
3456 (s = (U8*)SvPV_nomg(sv, slen)) && slen &&
3457 UTF8_IS_START(*s)) {
3458 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3462 utf8_to_uvchr(s, &ulen);
3463 toTITLE_utf8(s, tmpbuf, &tculen);
3464 utf8_to_uvchr(tmpbuf, 0);
3466 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3468 /* slen is the byte length of the whole SV.
3469 * ulen is the byte length of the original Unicode character
3470 * stored as UTF-8 at s.
3471 * tculen is the byte length of the freshly titlecased
3472 * Unicode character stored as UTF-8 at tmpbuf.
3473 * We first set the result to be the titlecased character,
3474 * and then append the rest of the SV data. */
3475 sv_setpvn(TARG, (char*)tmpbuf, tculen);
3477 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3482 s = (U8*)SvPV_force_nomg(sv, slen);
3483 Copy(tmpbuf, s, tculen, U8);
3487 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3489 SvUTF8_off(TARG); /* decontaminate */
3490 sv_setsv_nomg(TARG, sv);
3494 s = (U8*)SvPV_force_nomg(sv, slen);
3496 if (IN_LOCALE_RUNTIME) {
3499 *s = toUPPER_LC(*s);
3518 (s = (U8*)SvPV_nomg(sv, slen)) && slen &&
3519 UTF8_IS_START(*s)) {
3521 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3525 toLOWER_utf8(s, tmpbuf, &ulen);
3526 uv = utf8_to_uvchr(tmpbuf, 0);
3527 tend = uvchr_to_utf8(tmpbuf, uv);
3529 if (!SvPADTMP(sv) || (STRLEN)(tend - tmpbuf) != ulen || SvREADONLY(sv)) {
3531 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
3533 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3538 s = (U8*)SvPV_force_nomg(sv, slen);
3539 Copy(tmpbuf, s, ulen, U8);
3543 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3545 SvUTF8_off(TARG); /* decontaminate */
3546 sv_setsv_nomg(TARG, sv);
3550 s = (U8*)SvPV_force_nomg(sv, slen);
3552 if (IN_LOCALE_RUNTIME) {
3555 *s = toLOWER_LC(*s);
3578 U8 tmpbuf[UTF8_MAXBYTES+1];
3580 s = (U8*)SvPV_nomg(sv,len);
3582 SvUTF8_off(TARG); /* decontaminate */
3583 sv_setpvn(TARG, "", 0);
3587 STRLEN min = len + 1;
3589 (void)SvUPGRADE(TARG, SVt_PV);
3591 (void)SvPOK_only(TARG);
3592 d = (U8*)SvPVX(TARG);
3595 STRLEN u = UTF8SKIP(s);
3597 toUPPER_utf8(s, tmpbuf, &ulen);
3598 if (ulen > u && (SvLEN(TARG) < (min += ulen - u))) {
3599 /* If the eventually required minimum size outgrows
3600 * the available space, we need to grow. */
3601 UV o = d - (U8*)SvPVX(TARG);
3603 /* If someone uppercases one million U+03B0s we
3604 * SvGROW() one million times. Or we could try
3605 * guessing how much to allocate without allocating
3606 * too much. Such is life. */
3608 d = (U8*)SvPVX(TARG) + o;
3610 Copy(tmpbuf, d, ulen, U8);
3616 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3621 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3623 SvUTF8_off(TARG); /* decontaminate */
3624 sv_setsv_nomg(TARG, sv);
3628 s = (U8*)SvPV_force_nomg(sv, len);
3630 register U8 *send = s + len;
3632 if (IN_LOCALE_RUNTIME) {
3635 for (; s < send; s++)
3636 *s = toUPPER_LC(*s);
3639 for (; s < send; s++)
3661 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3663 s = (U8*)SvPV_nomg(sv,len);
3665 SvUTF8_off(TARG); /* decontaminate */
3666 sv_setpvn(TARG, "", 0);
3670 STRLEN min = len + 1;
3672 (void)SvUPGRADE(TARG, SVt_PV);
3674 (void)SvPOK_only(TARG);
3675 d = (U8*)SvPVX(TARG);
3678 STRLEN u = UTF8SKIP(s);
3679 UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3681 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode U+03A3 */
3682 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3684 * Now if the sigma is NOT followed by
3685 * /$ignorable_sequence$cased_letter/;
3686 * and it IS preceded by
3687 * /$cased_letter$ignorable_sequence/;
3688 * where $ignorable_sequence is
3689 * [\x{2010}\x{AD}\p{Mn}]*
3690 * and $cased_letter is
3691 * [\p{Ll}\p{Lo}\p{Lt}]
3692 * then it should be mapped to 0x03C2,
3693 * (GREEK SMALL LETTER FINAL SIGMA),
3694 * instead of staying 0x03A3.
3695 * "should be": in other words,
3696 * this is not implemented yet.
3697 * See lib/unicore/SpecialCasing.txt.
3700 if (ulen > u && (SvLEN(TARG) < (min += ulen - u))) {
3701 /* If the eventually required minimum size outgrows
3702 * the available space, we need to grow. */
3703 UV o = d - (U8*)SvPVX(TARG);
3705 /* If someone lowercases one million U+0130s we
3706 * SvGROW() one million times. Or we could try
3707 * guessing how much to allocate without allocating.
3708 * too much. Such is life. */
3710 d = (U8*)SvPVX(TARG) + o;
3712 Copy(tmpbuf, d, ulen, U8);
3718 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3723 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3725 SvUTF8_off(TARG); /* decontaminate */
3726 sv_setsv_nomg(TARG, sv);
3731 s = (U8*)SvPV_force_nomg(sv, len);
3733 register U8 *send = s + len;
3735 if (IN_LOCALE_RUNTIME) {
3738 for (; s < send; s++)
3739 *s = toLOWER_LC(*s);
3742 for (; s < send; s++)
3756 register char *s = SvPV(sv,len);
3759 SvUTF8_off(TARG); /* decontaminate */
3761 (void)SvUPGRADE(TARG, SVt_PV);
3762 SvGROW(TARG, (len * 2) + 1);
3766 if (UTF8_IS_CONTINUED(*s)) {
3767 STRLEN ulen = UTF8SKIP(s);
3791 SvCUR_set(TARG, d - SvPVX(TARG));
3792 (void)SvPOK_only_UTF8(TARG);
3795 sv_setpvn(TARG, s, len);
3797 if (SvSMAGICAL(TARG))
3806 dSP; dMARK; dORIGMARK;
3808 register AV* av = (AV*)POPs;
3809 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3810 I32 arybase = PL_curcop->cop_arybase;
3813 if (SvTYPE(av) == SVt_PVAV) {
3814 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3816 for (svp = MARK + 1; svp <= SP; svp++) {
3821 if (max > AvMAX(av))
3824 while (++MARK <= SP) {
3825 elem = SvIVx(*MARK);
3829 svp = av_fetch(av, elem, lval);
3831 if (!svp || *svp == &PL_sv_undef)
3832 DIE(aTHX_ PL_no_aelem, elem);
3833 if (PL_op->op_private & OPpLVAL_INTRO)
3834 save_aelem(av, elem, svp);
3836 *MARK = svp ? *svp : &PL_sv_undef;
3839 if (GIMME != G_ARRAY) {
3841 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
3847 /* Associative arrays. */
3852 HV *hash = (HV*)POPs;
3854 const I32 gimme = GIMME_V;
3855 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
3858 /* might clobber stack_sp */
3859 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
3864 SV* sv = hv_iterkeysv(entry);
3865 PUSHs(sv); /* won't clobber stack_sp */
3866 if (gimme == G_ARRAY) {
3869 /* might clobber stack_sp */
3871 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
3876 else if (gimme == G_SCALAR)
3895 const I32 gimme = GIMME_V;
3896 const I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3900 if (PL_op->op_private & OPpSLICE) {
3904 hvtype = SvTYPE(hv);
3905 if (hvtype == SVt_PVHV) { /* hash element */
3906 while (++MARK <= SP) {
3907 sv = hv_delete_ent(hv, *MARK, discard, 0);
3908 *MARK = sv ? sv : &PL_sv_undef;
3911 else if (hvtype == SVt_PVAV) {
3912 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3913 while (++MARK <= SP) {
3914 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3915 *MARK = sv ? sv : &PL_sv_undef;
3918 else { /* pseudo-hash element */
3919 while (++MARK <= SP) {
3920 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
3921 *MARK = sv ? sv : &PL_sv_undef;
3926 DIE(aTHX_ "Not a HASH reference");
3929 else if (gimme == G_SCALAR) {
3934 *++MARK = &PL_sv_undef;
3941 if (SvTYPE(hv) == SVt_PVHV)
3942 sv = hv_delete_ent(hv, keysv, discard, 0);
3943 else if (SvTYPE(hv) == SVt_PVAV) {
3944 if (PL_op->op_flags & OPf_SPECIAL)
3945 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3947 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
3950 DIE(aTHX_ "Not a HASH reference");
3965 if (PL_op->op_private & OPpEXISTS_SUB) {
3969 cv = sv_2cv(sv, &hv, &gv, FALSE);
3972 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3978 if (SvTYPE(hv) == SVt_PVHV) {
3979 if (hv_exists_ent(hv, tmpsv, 0))
3982 else if (SvTYPE(hv) == SVt_PVAV) {
3983 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3984 if (av_exists((AV*)hv, SvIV(tmpsv)))
3987 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
3991 DIE(aTHX_ "Not a HASH reference");
3998 dSP; dMARK; dORIGMARK;
3999 register HV *hv = (HV*)POPs;
4000 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4001 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
4002 bool localizing = PL_op->op_private & OPpLVAL_INTRO ? TRUE : FALSE;
4003 bool other_magic = FALSE;
4009 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
4010 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
4011 /* Try to preserve the existenceness of a tied hash
4012 * element by using EXISTS and DELETE if possible.
4013 * Fallback to FETCH and STORE otherwise */
4014 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
4015 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
4016 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
4019 if (!realhv && localizing)
4020 DIE(aTHX_ "Can't localize pseudo-hash element");
4022 if (realhv || SvTYPE(hv) == SVt_PVAV) {
4023 while (++MARK <= SP) {
4026 bool preeminent = FALSE;
4029 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
4030 realhv ? hv_exists_ent(hv, keysv, 0)
4031 : avhv_exists_ent((AV*)hv, keysv, 0);
4035 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
4036 svp = he ? &HeVAL(he) : 0;
4039 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
4042 if (!svp || *svp == &PL_sv_undef) {
4043 DIE(aTHX_ PL_no_helem_sv, keysv);
4047 save_helem(hv, keysv, svp);
4050 char *key = SvPV(keysv, keylen);
4051 SAVEDELETE(hv, savepvn(key,keylen), keylen);
4055 *MARK = svp ? *svp : &PL_sv_undef;
4058 if (GIMME != G_ARRAY) {
4060 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4066 /* List operators. */
4071 if (GIMME != G_ARRAY) {
4073 *MARK = *SP; /* unwanted list, return last item */
4075 *MARK = &PL_sv_undef;
4084 SV **lastrelem = PL_stack_sp;
4085 SV **lastlelem = PL_stack_base + POPMARK;
4086 SV **firstlelem = PL_stack_base + POPMARK + 1;
4087 register SV **firstrelem = lastlelem + 1;
4088 I32 arybase = PL_curcop->cop_arybase;
4089 I32 lval = PL_op->op_flags & OPf_MOD;
4090 I32 is_something_there = lval;
4092 register I32 max = lastrelem - lastlelem;
4093 register SV **lelem;
4096 if (GIMME != G_ARRAY) {
4097 ix = SvIVx(*lastlelem);
4102 if (ix < 0 || ix >= max)
4103 *firstlelem = &PL_sv_undef;
4105 *firstlelem = firstrelem[ix];
4111 SP = firstlelem - 1;
4115 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4121 if (ix < 0 || ix >= max)
4122 *lelem = &PL_sv_undef;
4124 is_something_there = TRUE;
4125 if (!(*lelem = firstrelem[ix]))
4126 *lelem = &PL_sv_undef;
4129 if (is_something_there)
4132 SP = firstlelem - 1;
4138 dSP; dMARK; dORIGMARK;
4139 I32 items = SP - MARK;
4140 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
4141 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4148 dSP; dMARK; dORIGMARK;
4149 HV* hv = (HV*)sv_2mortal((SV*)newHV());
4153 SV *val = NEWSV(46, 0);
4155 sv_setsv(val, *++MARK);
4156 else if (ckWARN(WARN_MISC))
4157 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4158 (void)hv_store_ent(hv,key,val,0);
4167 dSP; dMARK; dORIGMARK;
4168 register AV *ary = (AV*)*++MARK;
4172 register I32 offset;
4173 register I32 length;
4180 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4181 *MARK-- = SvTIED_obj((SV*)ary, mg);
4185 call_method("SPLICE",GIMME_V);
4194 offset = i = SvIVx(*MARK);
4196 offset += AvFILLp(ary) + 1;
4198 offset -= PL_curcop->cop_arybase;
4200 DIE(aTHX_ PL_no_aelem, i);
4202 length = SvIVx(*MARK++);
4204 length += AvFILLp(ary) - offset + 1;
4210 length = AvMAX(ary) + 1; /* close enough to infinity */
4214 length = AvMAX(ary) + 1;
4216 if (offset > AvFILLp(ary) + 1) {
4217 if (ckWARN(WARN_MISC))
4218 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4219 offset = AvFILLp(ary) + 1;
4221 after = AvFILLp(ary) + 1 - (offset + length);
4222 if (after < 0) { /* not that much array */
4223 length += after; /* offset+length now in array */
4229 /* At this point, MARK .. SP-1 is our new LIST */
4232 diff = newlen - length;
4233 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4236 /* make new elements SVs now: avoid problems if they're from the array */
4237 for (dst = MARK, i = newlen; i; i--) {
4239 *dst++ = newSVsv(h);
4242 if (diff < 0) { /* shrinking the area */
4244 New(451, tmparyval, newlen, SV*); /* so remember insertion */
4245 Copy(MARK, tmparyval, newlen, SV*);
4248 MARK = ORIGMARK + 1;
4249 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4250 MEXTEND(MARK, length);
4251 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4253 EXTEND_MORTAL(length);
4254 for (i = length, dst = MARK; i; i--) {
4255 sv_2mortal(*dst); /* free them eventualy */
4262 *MARK = AvARRAY(ary)[offset+length-1];
4265 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4266 SvREFCNT_dec(*dst++); /* free them now */
4269 AvFILLp(ary) += diff;
4271 /* pull up or down? */
4273 if (offset < after) { /* easier to pull up */
4274 if (offset) { /* esp. if nothing to pull */
4275 src = &AvARRAY(ary)[offset-1];
4276 dst = src - diff; /* diff is negative */
4277 for (i = offset; i > 0; i--) /* can't trust Copy */
4281 SvPV_set(ary, (char*)(AvARRAY(ary) - diff)); /* diff is negative */
4285 if (after) { /* anything to pull down? */
4286 src = AvARRAY(ary) + offset + length;
4287 dst = src + diff; /* diff is negative */
4288 Move(src, dst, after, SV*);
4290 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4291 /* avoid later double free */
4295 dst[--i] = &PL_sv_undef;
4298 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4299 Safefree(tmparyval);
4302 else { /* no, expanding (or same) */
4304 New(452, tmparyval, length, SV*); /* so remember deletion */
4305 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4308 if (diff > 0) { /* expanding */
4310 /* push up or down? */
4312 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4316 Move(src, dst, offset, SV*);
4318 SvPV_set(ary, (char*)(AvARRAY(ary) - diff));/* diff is positive */
4320 AvFILLp(ary) += diff;
4323 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4324 av_extend(ary, AvFILLp(ary) + diff);
4325 AvFILLp(ary) += diff;
4328 dst = AvARRAY(ary) + AvFILLp(ary);
4330 for (i = after; i; i--) {
4338 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4341 MARK = ORIGMARK + 1;
4342 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4344 Copy(tmparyval, MARK, length, SV*);
4346 EXTEND_MORTAL(length);
4347 for (i = length, dst = MARK; i; i--) {
4348 sv_2mortal(*dst); /* free them eventualy */
4352 Safefree(tmparyval);
4356 else if (length--) {
4357 *MARK = tmparyval[length];
4360 while (length-- > 0)
4361 SvREFCNT_dec(tmparyval[length]);
4363 Safefree(tmparyval);
4366 *MARK = &PL_sv_undef;
4374 dSP; dMARK; dORIGMARK; dTARGET;
4375 register AV *ary = (AV*)*++MARK;
4376 register SV *sv = &PL_sv_undef;
4379 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4380 *MARK-- = SvTIED_obj((SV*)ary, mg);
4384 call_method("PUSH",G_SCALAR|G_DISCARD);
4389 /* Why no pre-extend of ary here ? */
4390 for (++MARK; MARK <= SP; MARK++) {
4393 sv_setsv(sv, *MARK);
4398 PUSHi( AvFILL(ary) + 1 );
4406 SV *sv = av_pop(av);
4408 (void)sv_2mortal(sv);
4417 SV *sv = av_shift(av);
4422 (void)sv_2mortal(sv);
4429 dSP; dMARK; dORIGMARK; dTARGET;
4430 register AV *ary = (AV*)*++MARK;
4435 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4436 *MARK-- = SvTIED_obj((SV*)ary, mg);
4440 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4445 av_unshift(ary, SP - MARK);
4447 sv = newSVsv(*++MARK);
4448 (void)av_store(ary, i++, sv);
4452 PUSHi( AvFILL(ary) + 1 );
4462 if (GIMME == G_ARRAY) {
4469 /* safe as long as stack cannot get extended in the above */
4474 register char *down;
4479 SvUTF8_off(TARG); /* decontaminate */
4481 do_join(TARG, &PL_sv_no, MARK, SP);
4483 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
4484 up = SvPV_force(TARG, len);
4486 if (DO_UTF8(TARG)) { /* first reverse each character */
4487 U8* s = (U8*)SvPVX(TARG);
4488 U8* send = (U8*)(s + len);
4490 if (UTF8_IS_INVARIANT(*s)) {
4495 if (!utf8_to_uvchr(s, 0))
4499 down = (char*)(s - 1);
4500 /* reverse this character */
4504 *down-- = (char)tmp;
4510 down = SvPVX(TARG) + len - 1;
4514 *down-- = (char)tmp;
4516 (void)SvPOK_only_UTF8(TARG);
4528 register IV limit = POPi; /* note, negative is forever */
4531 register char *s = SvPV(sv, len);
4532 bool do_utf8 = DO_UTF8(sv);
4533 char *strend = s + len;
4535 register REGEXP *rx;
4539 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s);
4540 I32 maxiters = slen + 10;
4543 I32 origlimit = limit;
4546 const I32 gimme = GIMME_V;
4547 const I32 oldsave = PL_savestack_ix;
4548 I32 make_mortal = 1;
4549 MAGIC *mg = (MAGIC *) NULL;
4552 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4557 DIE(aTHX_ "panic: pp_split");
4560 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4561 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4563 RX_MATCH_UTF8_set(rx, do_utf8);
4565 if (pm->op_pmreplroot) {
4567 ary = GvAVn((GV*)PAD_SVl(INT2PTR(PADOFFSET, pm->op_pmreplroot)));
4569 ary = GvAVn((GV*)pm->op_pmreplroot);
4572 else if (gimme != G_ARRAY)
4573 #ifdef USE_5005THREADS
4574 ary = (AV*)PAD_SVl(0);
4576 ary = GvAVn(PL_defgv);
4577 #endif /* USE_5005THREADS */
4580 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4586 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4588 XPUSHs(SvTIED_obj((SV*)ary, mg));
4594 for (i = AvFILLp(ary); i >= 0; i--)
4595 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4597 /* temporarily switch stacks */
4598 SAVESWITCHSTACK(PL_curstack, ary);
4602 base = SP - PL_stack_base;
4604 if (pm->op_pmflags & PMf_SKIPWHITE) {
4605 if (pm->op_pmflags & PMf_LOCALE) {
4606 while (isSPACE_LC(*s))
4614 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4615 SAVEINT(PL_multiline);
4616 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4620 limit = maxiters + 2;
4621 if (pm->op_pmflags & PMf_WHITE) {
4624 while (m < strend &&
4625 !((pm->op_pmflags & PMf_LOCALE)
4626 ? isSPACE_LC(*m) : isSPACE(*m)))
4631 dstr = newSVpvn(s, m-s);
4635 (void)SvUTF8_on(dstr);
4639 while (s < strend &&
4640 ((pm->op_pmflags & PMf_LOCALE)
4641 ? isSPACE_LC(*s) : isSPACE(*s)))
4645 else if (rx->precomp[0] == '^' && rx->precomp[1] == '\0') {
4648 for (m = s; m < strend && *m != '\n'; m++) ;
4652 dstr = newSVpvn(s, m-s);
4656 (void)SvUTF8_on(dstr);
4661 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4662 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4663 && (rx->reganch & ROPT_CHECK_ALL)
4664 && !(rx->reganch & ROPT_ANCH)) {
4665 int tail = (rx->reganch & RE_INTUIT_TAIL);
4666 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4669 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4671 char c = *SvPV(csv, n_a);
4674 for (m = s; m < strend && *m != c; m++) ;
4677 dstr = newSVpvn(s, m-s);
4681 (void)SvUTF8_on(dstr);
4683 /* The rx->minlen is in characters but we want to step
4684 * s ahead by bytes. */
4686 s = (char*)utf8_hop((U8*)m, len);
4688 s = m + len; /* Fake \n at the end */
4692 while (s < strend && --limit &&
4693 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4694 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
4696 dstr = newSVpvn(s, m-s);
4700 (void)SvUTF8_on(dstr);
4702 /* The rx->minlen is in characters but we want to step
4703 * s ahead by bytes. */
4705 s = (char*)utf8_hop((U8*)m, len);
4707 s = m + len; /* Fake \n at the end */
4712 maxiters += slen * rx->nparens;
4713 while (s < strend && --limit)
4716 i = CALLREGEXEC(aTHX_ rx, s, strend, orig, 1 , sv, NULL, 0);
4720 TAINT_IF(RX_MATCH_TAINTED(rx));
4721 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4726 strend = s + (strend - m);
4728 m = rx->startp[0] + orig;
4729 dstr = newSVpvn(s, m-s);
4733 (void)SvUTF8_on(dstr);
4736 for (i = 1; i <= (I32)rx->nparens; i++) {
4737 s = rx->startp[i] + orig;
4738 m = rx->endp[i] + orig;
4740 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4741 parens that didn't match -- they should be set to
4742 undef, not the empty string */
4743 if (m >= orig && s >= orig) {
4744 dstr = newSVpvn(s, m-s);
4747 dstr = &PL_sv_undef; /* undef, not "" */
4751 (void)SvUTF8_on(dstr);
4755 s = rx->endp[0] + orig;
4759 iters = (SP - PL_stack_base) - base;
4760 if (iters > maxiters)
4761 DIE(aTHX_ "Split loop");
4763 /* keep field after final delim? */
4764 if (s < strend || (iters && origlimit)) {
4765 STRLEN l = strend - s;
4766 dstr = newSVpvn(s, l);
4770 (void)SvUTF8_on(dstr);
4774 else if (!origlimit) {
4775 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
4776 if (TOPs && !make_mortal)
4779 *SP-- = &PL_sv_undef;
4784 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
4788 if (SvSMAGICAL(ary)) {
4793 if (gimme == G_ARRAY) {
4795 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4803 call_method("PUSH",G_SCALAR|G_DISCARD);
4806 if (gimme == G_ARRAY) {
4807 /* EXTEND should not be needed - we just popped them */
4809 for (i=0; i < iters; i++) {
4810 SV **svp = av_fetch(ary, i, FALSE);
4811 PUSHs((svp) ? *svp : &PL_sv_undef);
4818 if (gimme == G_ARRAY)
4827 #ifdef USE_5005THREADS
4829 Perl_unlock_condpair(pTHX_ void *svv)
4831 MAGIC *mg = mg_find((SV*)svv, PERL_MAGIC_mutex);
4834 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
4835 MUTEX_LOCK(MgMUTEXP(mg));
4836 if (MgOWNER(mg) != thr)
4837 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
4839 COND_SIGNAL(MgOWNERCONDP(mg));
4840 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
4841 PTR2UV(thr), PTR2UV(svv)));
4842 MUTEX_UNLOCK(MgMUTEXP(mg));
4844 #endif /* USE_5005THREADS */
4852 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4853 || SvTYPE(retsv) == SVt_PVCV) {
4854 retsv = refto(retsv);
4862 #ifdef USE_5005THREADS
4865 if (PL_op->op_private & OPpLVAL_INTRO)
4866 PUSHs(*save_threadsv(PL_op->op_targ));
4868 PUSHs(THREADSV(PL_op->op_targ));
4871 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
4872 #endif /* USE_5005THREADS */
4877 * c-indentation-style: bsd
4879 * indent-tabs-mode: t
4882 * ex: set ts=8 sts=4 sw=4 noet: