3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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 */
52 if (GIMME_V == G_SCALAR)
63 if (PL_op->op_private & OPpLVAL_INTRO)
64 if (!(PL_op->op_private & OPpPAD_STATE))
65 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
67 if (PL_op->op_flags & OPf_REF) {
71 if (GIMME == G_SCALAR)
72 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
77 if (gimme == G_ARRAY) {
78 const I32 maxarg = AvFILL((AV*)TARG) + 1;
80 if (SvMAGICAL(TARG)) {
82 for (i=0; i < (U32)maxarg; i++) {
83 SV * const * const svp = av_fetch((AV*)TARG, i, FALSE);
84 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
88 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
92 else if (gimme == G_SCALAR) {
93 SV* const sv = sv_newmortal();
94 const I32 maxarg = AvFILL((AV*)TARG) + 1;
107 if (PL_op->op_private & OPpLVAL_INTRO)
108 if (!(PL_op->op_private & OPpPAD_STATE))
109 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
110 if (PL_op->op_flags & OPf_REF)
113 if (GIMME == G_SCALAR)
114 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
118 if (gimme == G_ARRAY) {
121 else if (gimme == G_SCALAR) {
122 SV* const sv = Perl_hv_scalar(aTHX_ (HV*)TARG);
136 tryAMAGICunDEREF(to_gv);
139 if (SvTYPE(sv) == SVt_PVIO) {
140 GV * const gv = (GV*) sv_newmortal();
141 gv_init(gv, 0, "", 0, 0);
142 GvIOp(gv) = (IO *)sv;
143 SvREFCNT_inc_void_NN(sv);
146 else if (SvTYPE(sv) != SVt_PVGV)
147 DIE(aTHX_ "Not a GLOB reference");
150 if (SvTYPE(sv) != SVt_PVGV) {
151 if (SvGMAGICAL(sv)) {
156 if (!SvOK(sv) && sv != &PL_sv_undef) {
157 /* If this is a 'my' scalar and flag is set then vivify
161 Perl_croak(aTHX_ PL_no_modify);
162 if (PL_op->op_private & OPpDEREF) {
164 if (cUNOP->op_targ) {
166 SV * const namesv = PAD_SV(cUNOP->op_targ);
167 const char * const name = SvPV(namesv, len);
169 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
172 const char * const name = CopSTASHPV(PL_curcop);
175 if (SvTYPE(sv) < SVt_RV)
176 sv_upgrade(sv, SVt_RV);
177 else if (SvPVX_const(sv)) {
182 SvRV_set(sv, (SV*)gv);
187 if (PL_op->op_flags & OPf_REF ||
188 PL_op->op_private & HINT_STRICT_REFS)
189 DIE(aTHX_ PL_no_usym, "a symbol");
190 if (ckWARN(WARN_UNINITIALIZED))
194 if ((PL_op->op_flags & OPf_SPECIAL) &&
195 !(PL_op->op_flags & OPf_MOD))
197 SV * const temp = (SV*)gv_fetchsv(sv, 0, SVt_PVGV);
199 && (!is_gv_magical_sv(sv,0)
200 || !(sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV)))) {
206 if (PL_op->op_private & HINT_STRICT_REFS)
207 DIE(aTHX_ PL_no_symref_sv, sv, "a symbol");
208 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
209 == OPpDONT_INIT_GV) {
210 /* We are the target of a coderef assignment. Return
211 the scalar unchanged, and let pp_sasssign deal with
215 sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV);
219 if (PL_op->op_private & OPpLVAL_INTRO)
220 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
225 /* Helper function for pp_rv2sv and pp_rv2av */
227 Perl_softref2xv(pTHX_ SV *const sv, const char *const what, const U32 type,
233 if (PL_op->op_private & HINT_STRICT_REFS) {
235 Perl_die(aTHX_ PL_no_symref_sv, sv, what);
237 Perl_die(aTHX_ PL_no_usym, what);
240 if (PL_op->op_flags & OPf_REF)
241 Perl_die(aTHX_ PL_no_usym, what);
242 if (ckWARN(WARN_UNINITIALIZED))
244 if (type != SVt_PV && GIMME_V == G_ARRAY) {
248 **spp = &PL_sv_undef;
251 if ((PL_op->op_flags & OPf_SPECIAL) &&
252 !(PL_op->op_flags & OPf_MOD))
254 gv = gv_fetchsv(sv, 0, type);
256 && (!is_gv_magical_sv(sv,0)
257 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
259 **spp = &PL_sv_undef;
264 gv = gv_fetchsv(sv, GV_ADD, type);
276 tryAMAGICunDEREF(to_sv);
279 switch (SvTYPE(sv)) {
285 DIE(aTHX_ "Not a SCALAR reference");
292 if (SvTYPE(gv) != SVt_PVGV) {
293 if (SvGMAGICAL(sv)) {
298 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
304 if (PL_op->op_flags & OPf_MOD) {
305 if (PL_op->op_private & OPpLVAL_INTRO) {
306 if (cUNOP->op_first->op_type == OP_NULL)
307 sv = save_scalar((GV*)TOPs);
309 sv = save_scalar(gv);
311 Perl_croak(aTHX_ PL_no_localize_ref);
313 else if (PL_op->op_private & OPpDEREF)
314 vivify_ref(sv, PL_op->op_private & OPpDEREF);
323 AV * const av = (AV*)TOPs;
324 SV ** const sv = Perl_av_arylen_p(aTHX_ (AV*)av);
326 *sv = newSV_type(SVt_PVMG);
327 sv_magic(*sv, (SV*)av, PERL_MAGIC_arylen, NULL, 0);
335 dVAR; dSP; dTARGET; dPOPss;
337 if (PL_op->op_flags & OPf_MOD || LVRET) {
338 if (SvTYPE(TARG) < SVt_PVLV) {
339 sv_upgrade(TARG, SVt_PVLV);
340 sv_magic(TARG, NULL, PERL_MAGIC_pos, NULL, 0);
344 if (LvTARG(TARG) != sv) {
346 SvREFCNT_dec(LvTARG(TARG));
347 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
349 PUSHs(TARG); /* no SvSETMAGIC */
353 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
354 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
355 if (mg && mg->mg_len >= 0) {
359 PUSHi(i + CopARYBASE_get(PL_curcop));
372 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
374 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
377 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
378 /* (But not in defined().) */
380 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
383 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
384 if ((PL_op->op_private & OPpLVAL_INTRO)) {
385 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
388 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
391 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
395 cv = (CV*)&PL_sv_undef;
406 SV *ret = &PL_sv_undef;
408 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
409 const char * s = SvPVX_const(TOPs);
410 if (strnEQ(s, "CORE::", 6)) {
411 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
412 if (code < 0) { /* Overridable. */
413 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
414 int i = 0, n = 0, seen_question = 0, defgv = 0;
416 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
418 if (code == -KEY_chop || code == -KEY_chomp
419 || code == -KEY_exec || code == -KEY_system)
421 if (code == -KEY_mkdir) {
422 ret = sv_2mortal(newSVpvs("_;$"));
425 if (code == -KEY_readpipe) {
426 s = "CORE::backtick";
428 while (i < MAXO) { /* The slow way. */
429 if (strEQ(s + 6, PL_op_name[i])
430 || strEQ(s + 6, PL_op_desc[i]))
436 goto nonesuch; /* Should not happen... */
438 defgv = PL_opargs[i] & OA_DEFGV;
439 oa = PL_opargs[i] >> OASHIFT;
441 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
445 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
446 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
447 /* But globs are already references (kinda) */
448 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
452 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
455 if (defgv && str[n - 1] == '$')
458 ret = sv_2mortal(newSVpvn(str, n - 1));
460 else if (code) /* Non-Overridable */
462 else { /* None such */
464 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
468 cv = sv_2cv(TOPs, &stash, &gv, 0);
470 ret = sv_2mortal(newSVpvn(SvPVX_const(cv), SvCUR(cv)));
479 CV* cv = (CV*)PAD_SV(PL_op->op_targ);
481 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
497 if (GIMME != G_ARRAY) {
501 *MARK = &PL_sv_undef;
502 *MARK = refto(*MARK);
506 EXTEND_MORTAL(SP - MARK);
508 *MARK = refto(*MARK);
513 S_refto(pTHX_ SV *sv)
518 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
521 if (!(sv = LvTARG(sv)))
524 SvREFCNT_inc_void_NN(sv);
526 else if (SvTYPE(sv) == SVt_PVAV) {
527 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
530 SvREFCNT_inc_void_NN(sv);
532 else if (SvPADTMP(sv) && !IS_PADGV(sv))
536 SvREFCNT_inc_void_NN(sv);
539 sv_upgrade(rv, SVt_RV);
549 SV * const sv = POPs;
554 if (!sv || !SvROK(sv))
557 pv = sv_reftype(SvRV(sv),TRUE);
558 PUSHp(pv, strlen(pv));
568 stash = CopSTASH(PL_curcop);
570 SV * const ssv = POPs;
574 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
575 Perl_croak(aTHX_ "Attempt to bless into a reference");
576 ptr = SvPV_const(ssv,len);
577 if (len == 0 && ckWARN(WARN_MISC))
578 Perl_warner(aTHX_ packWARN(WARN_MISC),
579 "Explicit blessing to '' (assuming package main)");
580 stash = gv_stashpvn(ptr, len, GV_ADD);
583 (void)sv_bless(TOPs, stash);
592 const char * const elem = SvPV_nolen_const(sv);
593 GV * const gv = (GV*)POPs;
598 /* elem will always be NUL terminated. */
599 const char * const second_letter = elem + 1;
602 if (strEQ(second_letter, "RRAY"))
603 tmpRef = (SV*)GvAV(gv);
606 if (strEQ(second_letter, "ODE"))
607 tmpRef = (SV*)GvCVu(gv);
610 if (strEQ(second_letter, "ILEHANDLE")) {
611 /* finally deprecated in 5.8.0 */
612 deprecate("*glob{FILEHANDLE}");
613 tmpRef = (SV*)GvIOp(gv);
616 if (strEQ(second_letter, "ORMAT"))
617 tmpRef = (SV*)GvFORM(gv);
620 if (strEQ(second_letter, "LOB"))
624 if (strEQ(second_letter, "ASH"))
625 tmpRef = (SV*)GvHV(gv);
628 if (*second_letter == 'O' && !elem[2])
629 tmpRef = (SV*)GvIOp(gv);
632 if (strEQ(second_letter, "AME"))
633 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
636 if (strEQ(second_letter, "ACKAGE")) {
637 const HV * const stash = GvSTASH(gv);
638 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
639 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
643 if (strEQ(second_letter, "CALAR"))
658 /* Pattern matching */
663 register unsigned char *s;
666 register I32 *sfirst;
670 if (sv == PL_lastscream) {
674 s = (unsigned char*)(SvPV(sv, len));
676 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
677 /* No point in studying a zero length string, and not safe to study
678 anything that doesn't appear to be a simple scalar (and hence might
679 change between now and when the regexp engine runs without our set
680 magic ever running) such as a reference to an object with overloaded
686 SvSCREAM_off(PL_lastscream);
687 SvREFCNT_dec(PL_lastscream);
689 PL_lastscream = SvREFCNT_inc_simple(sv);
691 s = (unsigned char*)(SvPV(sv, len));
695 if (pos > PL_maxscream) {
696 if (PL_maxscream < 0) {
697 PL_maxscream = pos + 80;
698 Newx(PL_screamfirst, 256, I32);
699 Newx(PL_screamnext, PL_maxscream, I32);
702 PL_maxscream = pos + pos / 4;
703 Renew(PL_screamnext, PL_maxscream, I32);
707 sfirst = PL_screamfirst;
708 snext = PL_screamnext;
710 if (!sfirst || !snext)
711 DIE(aTHX_ "do_study: out of memory");
713 for (ch = 256; ch; --ch)
718 register const I32 ch = s[pos];
720 snext[pos] = sfirst[ch] - pos;
727 /* piggyback on m//g magic */
728 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
737 if (PL_op->op_flags & OPf_STACKED)
739 else if (PL_op->op_private & OPpTARGET_MY)
745 TARG = sv_newmortal();
750 /* Lvalue operators. */
762 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
764 do_chop(TARG, *++MARK);
773 SETi(do_chomp(TOPs));
779 dVAR; dSP; dMARK; dTARGET;
780 register I32 count = 0;
783 count += do_chomp(POPs);
793 if (!PL_op->op_private) {
802 SV_CHECK_THINKFIRST_COW_DROP(sv);
804 switch (SvTYPE(sv)) {
814 if (cv_const_sv((CV*)sv) && ckWARN(WARN_MISC))
815 Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
816 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
820 /* let user-undef'd sub keep its identity */
821 GV* const gv = CvGV((CV*)sv);
828 SvSetMagicSV(sv, &PL_sv_undef);
834 if((stash = GvHV((GV*)sv)) && HvNAME_get(stash))
835 mro_isa_changed_in(stash);
836 /* undef *Pkg::meth_name ... */
837 else if(GvCVu((GV*)sv) && (stash = GvSTASH((GV*)sv)) && HvNAME_get(stash))
838 mro_method_changed_in(stash);
842 GvGP(sv) = gp_ref(gp);
844 GvLINE(sv) = CopLINE(PL_curcop);
850 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
865 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
866 DIE(aTHX_ PL_no_modify);
867 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
868 && SvIVX(TOPs) != IV_MIN)
870 SvIV_set(TOPs, SvIVX(TOPs) - 1);
871 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
882 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
883 DIE(aTHX_ PL_no_modify);
884 sv_setsv(TARG, TOPs);
885 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
886 && SvIVX(TOPs) != IV_MAX)
888 SvIV_set(TOPs, SvIVX(TOPs) + 1);
889 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
894 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
904 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
905 DIE(aTHX_ PL_no_modify);
906 sv_setsv(TARG, TOPs);
907 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
908 && SvIVX(TOPs) != IV_MIN)
910 SvIV_set(TOPs, SvIVX(TOPs) - 1);
911 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
920 /* Ordinary operators. */
924 dVAR; dSP; dATARGET; SV *svl, *svr;
925 #ifdef PERL_PRESERVE_IVUV
928 tryAMAGICbin(pow,opASSIGN);
929 svl = sv_2num(TOPm1s);
931 #ifdef PERL_PRESERVE_IVUV
932 /* For integer to integer power, we do the calculation by hand wherever
933 we're sure it is safe; otherwise we call pow() and try to convert to
934 integer afterwards. */
947 const IV iv = SvIVX(svr);
951 goto float_it; /* Can't do negative powers this way. */
955 baseuok = SvUOK(svl);
959 const IV iv = SvIVX(svl);
962 baseuok = TRUE; /* effectively it's a UV now */
964 baseuv = -iv; /* abs, baseuok == false records sign */
967 /* now we have integer ** positive integer. */
970 /* foo & (foo - 1) is zero only for a power of 2. */
971 if (!(baseuv & (baseuv - 1))) {
972 /* We are raising power-of-2 to a positive integer.
973 The logic here will work for any base (even non-integer
974 bases) but it can be less accurate than
975 pow (base,power) or exp (power * log (base)) when the
976 intermediate values start to spill out of the mantissa.
977 With powers of 2 we know this can't happen.
978 And powers of 2 are the favourite thing for perl
979 programmers to notice ** not doing what they mean. */
981 NV base = baseuok ? baseuv : -(NV)baseuv;
986 while (power >>= 1) {
997 register unsigned int highbit = 8 * sizeof(UV);
998 register unsigned int diff = 8 * sizeof(UV);
1001 if (baseuv >> highbit) {
1005 /* we now have baseuv < 2 ** highbit */
1006 if (power * highbit <= 8 * sizeof(UV)) {
1007 /* result will definitely fit in UV, so use UV math
1008 on same algorithm as above */
1009 register UV result = 1;
1010 register UV base = baseuv;
1011 const bool odd_power = (bool)(power & 1);
1015 while (power >>= 1) {
1022 if (baseuok || !odd_power)
1023 /* answer is positive */
1025 else if (result <= (UV)IV_MAX)
1026 /* answer negative, fits in IV */
1027 SETi( -(IV)result );
1028 else if (result == (UV)IV_MIN)
1029 /* 2's complement assumption: special case IV_MIN */
1032 /* answer negative, doesn't fit */
1033 SETn( -(NV)result );
1043 NV right = SvNV(svr);
1044 NV left = SvNV(svl);
1047 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1049 We are building perl with long double support and are on an AIX OS
1050 afflicted with a powl() function that wrongly returns NaNQ for any
1051 negative base. This was reported to IBM as PMR #23047-379 on
1052 03/06/2006. The problem exists in at least the following versions
1053 of AIX and the libm fileset, and no doubt others as well:
1055 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1056 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1057 AIX 5.2.0 bos.adt.libm 5.2.0.85
1059 So, until IBM fixes powl(), we provide the following workaround to
1060 handle the problem ourselves. Our logic is as follows: for
1061 negative bases (left), we use fmod(right, 2) to check if the
1062 exponent is an odd or even integer:
1064 - if odd, powl(left, right) == -powl(-left, right)
1065 - if even, powl(left, right) == powl(-left, right)
1067 If the exponent is not an integer, the result is rightly NaNQ, so
1068 we just return that (as NV_NAN).
1072 NV mod2 = Perl_fmod( right, 2.0 );
1073 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1074 SETn( -Perl_pow( -left, right) );
1075 } else if (mod2 == 0.0) { /* even integer */
1076 SETn( Perl_pow( -left, right) );
1077 } else { /* fractional power */
1081 SETn( Perl_pow( left, right) );
1084 SETn( Perl_pow( left, right) );
1085 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1087 #ifdef PERL_PRESERVE_IVUV
1097 dVAR; dSP; dATARGET; SV *svl, *svr;
1098 tryAMAGICbin(mult,opASSIGN);
1099 svl = sv_2num(TOPm1s);
1100 svr = sv_2num(TOPs);
1101 #ifdef PERL_PRESERVE_IVUV
1104 /* Unless the left argument is integer in range we are going to have to
1105 use NV maths. Hence only attempt to coerce the right argument if
1106 we know the left is integer. */
1107 /* Left operand is defined, so is it IV? */
1110 bool auvok = SvUOK(svl);
1111 bool buvok = SvUOK(svr);
1112 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1113 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1122 const IV aiv = SvIVX(svl);
1125 auvok = TRUE; /* effectively it's a UV now */
1127 alow = -aiv; /* abs, auvok == false records sign */
1133 const IV biv = SvIVX(svr);
1136 buvok = TRUE; /* effectively it's a UV now */
1138 blow = -biv; /* abs, buvok == false records sign */
1142 /* If this does sign extension on unsigned it's time for plan B */
1143 ahigh = alow >> (4 * sizeof (UV));
1145 bhigh = blow >> (4 * sizeof (UV));
1147 if (ahigh && bhigh) {
1149 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1150 which is overflow. Drop to NVs below. */
1151 } else if (!ahigh && !bhigh) {
1152 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1153 so the unsigned multiply cannot overflow. */
1154 const UV product = alow * blow;
1155 if (auvok == buvok) {
1156 /* -ve * -ve or +ve * +ve gives a +ve result. */
1160 } else if (product <= (UV)IV_MIN) {
1161 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1162 /* -ve result, which could overflow an IV */
1164 SETi( -(IV)product );
1166 } /* else drop to NVs below. */
1168 /* One operand is large, 1 small */
1171 /* swap the operands */
1173 bhigh = blow; /* bhigh now the temp var for the swap */
1177 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1178 multiplies can't overflow. shift can, add can, -ve can. */
1179 product_middle = ahigh * blow;
1180 if (!(product_middle & topmask)) {
1181 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1183 product_middle <<= (4 * sizeof (UV));
1184 product_low = alow * blow;
1186 /* as for pp_add, UV + something mustn't get smaller.
1187 IIRC ANSI mandates this wrapping *behaviour* for
1188 unsigned whatever the actual representation*/
1189 product_low += product_middle;
1190 if (product_low >= product_middle) {
1191 /* didn't overflow */
1192 if (auvok == buvok) {
1193 /* -ve * -ve or +ve * +ve gives a +ve result. */
1195 SETu( product_low );
1197 } else if (product_low <= (UV)IV_MIN) {
1198 /* 2s complement assumption again */
1199 /* -ve result, which could overflow an IV */
1201 SETi( -(IV)product_low );
1203 } /* else drop to NVs below. */
1205 } /* product_middle too large */
1206 } /* ahigh && bhigh */
1211 NV right = SvNV(svr);
1212 NV left = SvNV(svl);
1214 SETn( left * right );
1221 dVAR; dSP; dATARGET; SV *svl, *svr;
1222 tryAMAGICbin(div,opASSIGN);
1223 svl = sv_2num(TOPm1s);
1224 svr = sv_2num(TOPs);
1225 /* Only try to do UV divide first
1226 if ((SLOPPYDIVIDE is true) or
1227 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1229 The assumption is that it is better to use floating point divide
1230 whenever possible, only doing integer divide first if we can't be sure.
1231 If NV_PRESERVES_UV is true then we know at compile time that no UV
1232 can be too large to preserve, so don't need to compile the code to
1233 test the size of UVs. */
1236 # define PERL_TRY_UV_DIVIDE
1237 /* ensure that 20./5. == 4. */
1239 # ifdef PERL_PRESERVE_IVUV
1240 # ifndef NV_PRESERVES_UV
1241 # define PERL_TRY_UV_DIVIDE
1246 #ifdef PERL_TRY_UV_DIVIDE
1251 bool left_non_neg = SvUOK(svl);
1252 bool right_non_neg = SvUOK(svr);
1256 if (right_non_neg) {
1260 const IV biv = SvIVX(svr);
1263 right_non_neg = TRUE; /* effectively it's a UV now */
1269 /* historically undef()/0 gives a "Use of uninitialized value"
1270 warning before dieing, hence this test goes here.
1271 If it were immediately before the second SvIV_please, then
1272 DIE() would be invoked before left was even inspected, so
1273 no inpsection would give no warning. */
1275 DIE(aTHX_ "Illegal division by zero");
1281 const IV aiv = SvIVX(svl);
1284 left_non_neg = TRUE; /* effectively it's a UV now */
1293 /* For sloppy divide we always attempt integer division. */
1295 /* Otherwise we only attempt it if either or both operands
1296 would not be preserved by an NV. If both fit in NVs
1297 we fall through to the NV divide code below. However,
1298 as left >= right to ensure integer result here, we know that
1299 we can skip the test on the right operand - right big
1300 enough not to be preserved can't get here unless left is
1303 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1306 /* Integer division can't overflow, but it can be imprecise. */
1307 const UV result = left / right;
1308 if (result * right == left) {
1309 SP--; /* result is valid */
1310 if (left_non_neg == right_non_neg) {
1311 /* signs identical, result is positive. */
1315 /* 2s complement assumption */
1316 if (result <= (UV)IV_MIN)
1317 SETi( -(IV)result );
1319 /* It's exact but too negative for IV. */
1320 SETn( -(NV)result );
1323 } /* tried integer divide but it was not an integer result */
1324 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1325 } /* left wasn't SvIOK */
1326 } /* right wasn't SvIOK */
1327 #endif /* PERL_TRY_UV_DIVIDE */
1329 NV right = SvNV(svr);
1330 NV left = SvNV(svl);
1331 (void)POPs;(void)POPs;
1332 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1333 if (! Perl_isnan(right) && right == 0.0)
1337 DIE(aTHX_ "Illegal division by zero");
1338 PUSHn( left / right );
1345 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1349 bool left_neg = FALSE;
1350 bool right_neg = FALSE;
1351 bool use_double = FALSE;
1352 bool dright_valid = FALSE;
1356 SV * const svr = sv_2num(TOPs);
1359 right_neg = !SvUOK(svr);
1363 const IV biv = SvIVX(svr);
1366 right_neg = FALSE; /* effectively it's a UV now */
1374 right_neg = dright < 0;
1377 if (dright < UV_MAX_P1) {
1378 right = U_V(dright);
1379 dright_valid = TRUE; /* In case we need to use double below. */
1386 /* At this point use_double is only true if right is out of range for
1387 a UV. In range NV has been rounded down to nearest UV and
1388 use_double false. */
1389 svl = sv_2num(TOPs);
1391 if (!use_double && SvIOK(svl)) {
1393 left_neg = !SvUOK(svl);
1397 const IV aiv = SvIVX(svl);
1400 left_neg = FALSE; /* effectively it's a UV now */
1409 left_neg = dleft < 0;
1413 /* This should be exactly the 5.6 behaviour - if left and right are
1414 both in range for UV then use U_V() rather than floor. */
1416 if (dleft < UV_MAX_P1) {
1417 /* right was in range, so is dleft, so use UVs not double.
1421 /* left is out of range for UV, right was in range, so promote
1422 right (back) to double. */
1424 /* The +0.5 is used in 5.6 even though it is not strictly
1425 consistent with the implicit +0 floor in the U_V()
1426 inside the #if 1. */
1427 dleft = Perl_floor(dleft + 0.5);
1430 dright = Perl_floor(dright + 0.5);
1441 DIE(aTHX_ "Illegal modulus zero");
1443 dans = Perl_fmod(dleft, dright);
1444 if ((left_neg != right_neg) && dans)
1445 dans = dright - dans;
1448 sv_setnv(TARG, dans);
1454 DIE(aTHX_ "Illegal modulus zero");
1457 if ((left_neg != right_neg) && ans)
1460 /* XXX may warn: unary minus operator applied to unsigned type */
1461 /* could change -foo to be (~foo)+1 instead */
1462 if (ans <= ~((UV)IV_MAX)+1)
1463 sv_setiv(TARG, ~ans+1);
1465 sv_setnv(TARG, -(NV)ans);
1468 sv_setuv(TARG, ans);
1477 dVAR; dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1484 const UV uv = SvUV(sv);
1486 count = IV_MAX; /* The best we can do? */
1490 const IV iv = SvIV(sv);
1497 else if (SvNOKp(sv)) {
1498 const NV nv = SvNV(sv);
1506 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1508 static const char oom_list_extend[] = "Out of memory during list extend";
1509 const I32 items = SP - MARK;
1510 const I32 max = items * count;
1512 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1513 /* Did the max computation overflow? */
1514 if (items > 0 && max > 0 && (max < items || max < count))
1515 Perl_croak(aTHX_ oom_list_extend);
1520 /* This code was intended to fix 20010809.028:
1523 for (($x =~ /./g) x 2) {
1524 print chop; # "abcdabcd" expected as output.
1527 * but that change (#11635) broke this code:
1529 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1531 * I can't think of a better fix that doesn't introduce
1532 * an efficiency hit by copying the SVs. The stack isn't
1533 * refcounted, and mortalisation obviously doesn't
1534 * Do The Right Thing when the stack has more than
1535 * one pointer to the same mortal value.
1539 *SP = sv_2mortal(newSVsv(*SP));
1549 repeatcpy((char*)(MARK + items), (char*)MARK,
1550 items * sizeof(SV*), count - 1);
1553 else if (count <= 0)
1556 else { /* Note: mark already snarfed by pp_list */
1557 SV * const tmpstr = POPs;
1560 static const char oom_string_extend[] =
1561 "Out of memory during string extend";
1563 SvSetSV(TARG, tmpstr);
1564 SvPV_force(TARG, len);
1565 isutf = DO_UTF8(TARG);
1570 const STRLEN max = (UV)count * len;
1571 if (len > MEM_SIZE_MAX / count)
1572 Perl_croak(aTHX_ oom_string_extend);
1573 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1574 SvGROW(TARG, max + 1);
1575 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1576 SvCUR_set(TARG, SvCUR(TARG) * count);
1578 *SvEND(TARG) = '\0';
1581 (void)SvPOK_only_UTF8(TARG);
1583 (void)SvPOK_only(TARG);
1585 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1586 /* The parser saw this as a list repeat, and there
1587 are probably several items on the stack. But we're
1588 in scalar context, and there's no pp_list to save us
1589 now. So drop the rest of the items -- robin@kitsite.com
1602 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1603 tryAMAGICbin(subtr,opASSIGN);
1604 svl = sv_2num(TOPm1s);
1605 svr = sv_2num(TOPs);
1606 useleft = USE_LEFT(svl);
1607 #ifdef PERL_PRESERVE_IVUV
1608 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1609 "bad things" happen if you rely on signed integers wrapping. */
1612 /* Unless the left argument is integer in range we are going to have to
1613 use NV maths. Hence only attempt to coerce the right argument if
1614 we know the left is integer. */
1615 register UV auv = 0;
1621 a_valid = auvok = 1;
1622 /* left operand is undef, treat as zero. */
1624 /* Left operand is defined, so is it IV? */
1627 if ((auvok = SvUOK(svl)))
1630 register const IV aiv = SvIVX(svl);
1633 auvok = 1; /* Now acting as a sign flag. */
1634 } else { /* 2s complement assumption for IV_MIN */
1642 bool result_good = 0;
1645 bool buvok = SvUOK(svr);
1650 register const IV biv = SvIVX(svr);
1657 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1658 else "IV" now, independent of how it came in.
1659 if a, b represents positive, A, B negative, a maps to -A etc
1664 all UV maths. negate result if A negative.
1665 subtract if signs same, add if signs differ. */
1667 if (auvok ^ buvok) {
1676 /* Must get smaller */
1681 if (result <= buv) {
1682 /* result really should be -(auv-buv). as its negation
1683 of true value, need to swap our result flag */
1695 if (result <= (UV)IV_MIN)
1696 SETi( -(IV)result );
1698 /* result valid, but out of range for IV. */
1699 SETn( -(NV)result );
1703 } /* Overflow, drop through to NVs. */
1708 NV value = SvNV(svr);
1712 /* left operand is undef, treat as zero - value */
1716 SETn( SvNV(svl) - value );
1723 dVAR; dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1725 const IV shift = POPi;
1726 if (PL_op->op_private & HINT_INTEGER) {
1740 dVAR; dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1742 const IV shift = POPi;
1743 if (PL_op->op_private & HINT_INTEGER) {
1757 dVAR; dSP; tryAMAGICbinSET(lt,0);
1758 #ifdef PERL_PRESERVE_IVUV
1761 SvIV_please(TOPm1s);
1762 if (SvIOK(TOPm1s)) {
1763 bool auvok = SvUOK(TOPm1s);
1764 bool buvok = SvUOK(TOPs);
1766 if (!auvok && !buvok) { /* ## IV < IV ## */
1767 const IV aiv = SvIVX(TOPm1s);
1768 const IV biv = SvIVX(TOPs);
1771 SETs(boolSV(aiv < biv));
1774 if (auvok && buvok) { /* ## UV < UV ## */
1775 const UV auv = SvUVX(TOPm1s);
1776 const UV buv = SvUVX(TOPs);
1779 SETs(boolSV(auv < buv));
1782 if (auvok) { /* ## UV < IV ## */
1784 const IV biv = SvIVX(TOPs);
1787 /* As (a) is a UV, it's >=0, so it cannot be < */
1792 SETs(boolSV(auv < (UV)biv));
1795 { /* ## IV < UV ## */
1796 const IV aiv = SvIVX(TOPm1s);
1800 /* As (b) is a UV, it's >=0, so it must be < */
1807 SETs(boolSV((UV)aiv < buv));
1813 #ifndef NV_PRESERVES_UV
1814 #ifdef PERL_PRESERVE_IVUV
1817 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1819 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1824 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1826 if (Perl_isnan(left) || Perl_isnan(right))
1828 SETs(boolSV(left < right));
1831 SETs(boolSV(TOPn < value));
1839 dVAR; dSP; tryAMAGICbinSET(gt,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 const IV aiv = SvIVX(TOPm1s);
1850 const IV biv = SvIVX(TOPs);
1853 SETs(boolSV(aiv > biv));
1856 if (auvok && buvok) { /* ## UV > UV ## */
1857 const UV auv = SvUVX(TOPm1s);
1858 const UV buv = SvUVX(TOPs);
1861 SETs(boolSV(auv > buv));
1864 if (auvok) { /* ## UV > IV ## */
1866 const IV biv = SvIVX(TOPs);
1870 /* As (a) is a UV, it's >=0, so it must be > */
1875 SETs(boolSV(auv > (UV)biv));
1878 { /* ## IV > UV ## */
1879 const IV aiv = SvIVX(TOPm1s);
1883 /* As (b) is a UV, it's >=0, so it cannot be > */
1890 SETs(boolSV((UV)aiv > buv));
1896 #ifndef NV_PRESERVES_UV
1897 #ifdef PERL_PRESERVE_IVUV
1900 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1902 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1907 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1909 if (Perl_isnan(left) || Perl_isnan(right))
1911 SETs(boolSV(left > right));
1914 SETs(boolSV(TOPn > value));
1922 dVAR; dSP; tryAMAGICbinSET(le,0);
1923 #ifdef PERL_PRESERVE_IVUV
1926 SvIV_please(TOPm1s);
1927 if (SvIOK(TOPm1s)) {
1928 bool auvok = SvUOK(TOPm1s);
1929 bool buvok = SvUOK(TOPs);
1931 if (!auvok && !buvok) { /* ## IV <= IV ## */
1932 const IV aiv = SvIVX(TOPm1s);
1933 const IV biv = SvIVX(TOPs);
1936 SETs(boolSV(aiv <= biv));
1939 if (auvok && buvok) { /* ## UV <= UV ## */
1940 UV auv = SvUVX(TOPm1s);
1941 UV buv = SvUVX(TOPs);
1944 SETs(boolSV(auv <= buv));
1947 if (auvok) { /* ## UV <= IV ## */
1949 const IV biv = SvIVX(TOPs);
1953 /* As (a) is a UV, it's >=0, so a cannot be <= */
1958 SETs(boolSV(auv <= (UV)biv));
1961 { /* ## IV <= UV ## */
1962 const IV aiv = SvIVX(TOPm1s);
1966 /* As (b) is a UV, it's >=0, so a must be <= */
1973 SETs(boolSV((UV)aiv <= buv));
1979 #ifndef NV_PRESERVES_UV
1980 #ifdef PERL_PRESERVE_IVUV
1983 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1985 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1990 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1992 if (Perl_isnan(left) || Perl_isnan(right))
1994 SETs(boolSV(left <= right));
1997 SETs(boolSV(TOPn <= value));
2005 dVAR; dSP; tryAMAGICbinSET(ge,0);
2006 #ifdef PERL_PRESERVE_IVUV
2009 SvIV_please(TOPm1s);
2010 if (SvIOK(TOPm1s)) {
2011 bool auvok = SvUOK(TOPm1s);
2012 bool buvok = SvUOK(TOPs);
2014 if (!auvok && !buvok) { /* ## IV >= IV ## */
2015 const IV aiv = SvIVX(TOPm1s);
2016 const IV biv = SvIVX(TOPs);
2019 SETs(boolSV(aiv >= biv));
2022 if (auvok && buvok) { /* ## UV >= UV ## */
2023 const UV auv = SvUVX(TOPm1s);
2024 const UV buv = SvUVX(TOPs);
2027 SETs(boolSV(auv >= buv));
2030 if (auvok) { /* ## UV >= IV ## */
2032 const IV biv = SvIVX(TOPs);
2036 /* As (a) is a UV, it's >=0, so it must be >= */
2041 SETs(boolSV(auv >= (UV)biv));
2044 { /* ## IV >= UV ## */
2045 const IV aiv = SvIVX(TOPm1s);
2049 /* As (b) is a UV, it's >=0, so a cannot be >= */
2056 SETs(boolSV((UV)aiv >= buv));
2062 #ifndef NV_PRESERVES_UV
2063 #ifdef PERL_PRESERVE_IVUV
2066 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2068 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2073 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2075 if (Perl_isnan(left) || Perl_isnan(right))
2077 SETs(boolSV(left >= right));
2080 SETs(boolSV(TOPn >= value));
2088 dVAR; dSP; tryAMAGICbinSET(ne,0);
2089 #ifndef NV_PRESERVES_UV
2090 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2092 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2096 #ifdef PERL_PRESERVE_IVUV
2099 SvIV_please(TOPm1s);
2100 if (SvIOK(TOPm1s)) {
2101 const bool auvok = SvUOK(TOPm1s);
2102 const bool buvok = SvUOK(TOPs);
2104 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2105 /* Casting IV to UV before comparison isn't going to matter
2106 on 2s complement. On 1s complement or sign&magnitude
2107 (if we have any of them) it could make negative zero
2108 differ from normal zero. As I understand it. (Need to
2109 check - is negative zero implementation defined behaviour
2111 const UV buv = SvUVX(POPs);
2112 const UV auv = SvUVX(TOPs);
2114 SETs(boolSV(auv != buv));
2117 { /* ## Mixed IV,UV ## */
2121 /* != is commutative so swap if needed (save code) */
2123 /* swap. top of stack (b) is the iv */
2127 /* As (a) is a UV, it's >0, so it cannot be == */
2136 /* As (b) is a UV, it's >0, so it cannot be == */
2140 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2142 SETs(boolSV((UV)iv != uv));
2149 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2151 if (Perl_isnan(left) || Perl_isnan(right))
2153 SETs(boolSV(left != right));
2156 SETs(boolSV(TOPn != value));
2164 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
2165 #ifndef NV_PRESERVES_UV
2166 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2167 const UV right = PTR2UV(SvRV(POPs));
2168 const UV left = PTR2UV(SvRV(TOPs));
2169 SETi((left > right) - (left < right));
2173 #ifdef PERL_PRESERVE_IVUV
2174 /* Fortunately it seems NaN isn't IOK */
2177 SvIV_please(TOPm1s);
2178 if (SvIOK(TOPm1s)) {
2179 const bool leftuvok = SvUOK(TOPm1s);
2180 const bool rightuvok = SvUOK(TOPs);
2182 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2183 const IV leftiv = SvIVX(TOPm1s);
2184 const IV rightiv = SvIVX(TOPs);
2186 if (leftiv > rightiv)
2188 else if (leftiv < rightiv)
2192 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2193 const UV leftuv = SvUVX(TOPm1s);
2194 const UV rightuv = SvUVX(TOPs);
2196 if (leftuv > rightuv)
2198 else if (leftuv < rightuv)
2202 } else if (leftuvok) { /* ## UV <=> IV ## */
2203 const IV rightiv = SvIVX(TOPs);
2205 /* As (a) is a UV, it's >=0, so it cannot be < */
2208 const UV leftuv = SvUVX(TOPm1s);
2209 if (leftuv > (UV)rightiv) {
2211 } else if (leftuv < (UV)rightiv) {
2217 } else { /* ## IV <=> UV ## */
2218 const IV leftiv = SvIVX(TOPm1s);
2220 /* As (b) is a UV, it's >=0, so it must be < */
2223 const UV rightuv = SvUVX(TOPs);
2224 if ((UV)leftiv > rightuv) {
2226 } else if ((UV)leftiv < rightuv) {
2244 if (Perl_isnan(left) || Perl_isnan(right)) {
2248 value = (left > right) - (left < right);
2252 else if (left < right)
2254 else if (left > right)
2270 int amg_type = sle_amg;
2274 switch (PL_op->op_type) {
2293 tryAMAGICbinSET_var(amg_type,0);
2296 const int cmp = (IN_LOCALE_RUNTIME
2297 ? sv_cmp_locale(left, right)
2298 : sv_cmp(left, right));
2299 SETs(boolSV(cmp * multiplier < rhs));
2306 dVAR; dSP; tryAMAGICbinSET(seq,0);
2309 SETs(boolSV(sv_eq(left, right)));
2316 dVAR; dSP; tryAMAGICbinSET(sne,0);
2319 SETs(boolSV(!sv_eq(left, right)));
2326 dVAR; dSP; dTARGET; tryAMAGICbin(scmp,0);
2329 const int cmp = (IN_LOCALE_RUNTIME
2330 ? sv_cmp_locale(left, right)
2331 : sv_cmp(left, right));
2339 dVAR; dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2344 if (SvNIOKp(left) || SvNIOKp(right)) {
2345 if (PL_op->op_private & HINT_INTEGER) {
2346 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2350 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2355 do_vop(PL_op->op_type, TARG, left, right);
2364 dVAR; dSP; dATARGET;
2365 const int op_type = PL_op->op_type;
2367 tryAMAGICbin_var((op_type == OP_BIT_OR ? bor_amg : bxor_amg), opASSIGN);
2372 if (SvNIOKp(left) || SvNIOKp(right)) {
2373 if (PL_op->op_private & HINT_INTEGER) {
2374 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2375 const IV r = SvIV_nomg(right);
2376 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2380 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2381 const UV r = SvUV_nomg(right);
2382 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2387 do_vop(op_type, TARG, left, right);
2396 dVAR; dSP; dTARGET; tryAMAGICun(neg);
2398 SV * const sv = sv_2num(TOPs);
2399 const int flags = SvFLAGS(sv);
2401 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2402 /* It's publicly an integer, or privately an integer-not-float */
2405 if (SvIVX(sv) == IV_MIN) {
2406 /* 2s complement assumption. */
2407 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2410 else if (SvUVX(sv) <= IV_MAX) {
2415 else if (SvIVX(sv) != IV_MIN) {
2419 #ifdef PERL_PRESERVE_IVUV
2428 else if (SvPOKp(sv)) {
2430 const char * const s = SvPV_const(sv, len);
2431 if (isIDFIRST(*s)) {
2432 sv_setpvn(TARG, "-", 1);
2435 else if (*s == '+' || *s == '-') {
2437 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2439 else if (DO_UTF8(sv)) {
2442 goto oops_its_an_int;
2444 sv_setnv(TARG, -SvNV(sv));
2446 sv_setpvn(TARG, "-", 1);
2453 goto oops_its_an_int;
2454 sv_setnv(TARG, -SvNV(sv));
2466 dVAR; dSP; tryAMAGICunSET(not);
2467 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2473 dVAR; dSP; dTARGET; tryAMAGICun(compl);
2478 if (PL_op->op_private & HINT_INTEGER) {
2479 const IV i = ~SvIV_nomg(sv);
2483 const UV u = ~SvUV_nomg(sv);
2492 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2493 sv_setsv_nomg(TARG, sv);
2494 tmps = (U8*)SvPV_force(TARG, len);
2497 /* Calculate exact length, let's not estimate. */
2502 U8 * const send = tmps + len;
2503 U8 * const origtmps = tmps;
2504 const UV utf8flags = UTF8_ALLOW_ANYUV;
2506 while (tmps < send) {
2507 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2509 targlen += UNISKIP(~c);
2515 /* Now rewind strings and write them. */
2522 Newx(result, targlen + 1, U8);
2524 while (tmps < send) {
2525 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2527 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2530 sv_usepvn_flags(TARG, (char*)result, targlen,
2531 SV_HAS_TRAILING_NUL);
2538 Newx(result, nchar + 1, U8);
2540 while (tmps < send) {
2541 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2546 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2554 register long *tmpl;
2555 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2558 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2563 for ( ; anum > 0; anum--, tmps++)
2572 /* integer versions of some of the above */
2576 dVAR; dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2579 SETi( left * right );
2587 dVAR; dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2591 DIE(aTHX_ "Illegal division by zero");
2594 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2598 value = num / value;
2604 #if defined(__GLIBC__) && IVSIZE == 8
2611 /* This is the vanilla old i_modulo. */
2612 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2616 DIE(aTHX_ "Illegal modulus zero");
2617 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2621 SETi( left % right );
2626 #if defined(__GLIBC__) && IVSIZE == 8
2631 /* This is the i_modulo with the workaround for the _moddi3 bug
2632 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2633 * See below for pp_i_modulo. */
2634 dVAR; dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2638 DIE(aTHX_ "Illegal modulus zero");
2639 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2643 SETi( left % PERL_ABS(right) );
2650 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2654 DIE(aTHX_ "Illegal modulus zero");
2655 /* The assumption is to use hereafter the old vanilla version... */
2657 PL_ppaddr[OP_I_MODULO] =
2659 /* .. but if we have glibc, we might have a buggy _moddi3
2660 * (at least glicb 2.2.5 is known to have this bug), in other
2661 * words our integer modulus with negative quad as the second
2662 * argument might be broken. Test for this and re-patch the
2663 * opcode dispatch table if that is the case, remembering to
2664 * also apply the workaround so that this first round works
2665 * right, too. See [perl #9402] for more information. */
2669 /* Cannot do this check with inlined IV constants since
2670 * that seems to work correctly even with the buggy glibc. */
2672 /* Yikes, we have the bug.
2673 * Patch in the workaround version. */
2675 PL_ppaddr[OP_I_MODULO] =
2676 &Perl_pp_i_modulo_1;
2677 /* Make certain we work right this time, too. */
2678 right = PERL_ABS(right);
2681 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2685 SETi( left % right );
2693 dVAR; dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2696 SETi( left + right );
2703 dVAR; dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2706 SETi( left - right );
2713 dVAR; dSP; tryAMAGICbinSET(lt,0);
2716 SETs(boolSV(left < right));
2723 dVAR; dSP; tryAMAGICbinSET(gt,0);
2726 SETs(boolSV(left > right));
2733 dVAR; dSP; tryAMAGICbinSET(le,0);
2736 SETs(boolSV(left <= right));
2743 dVAR; dSP; tryAMAGICbinSET(ge,0);
2746 SETs(boolSV(left >= right));
2753 dVAR; dSP; tryAMAGICbinSET(eq,0);
2756 SETs(boolSV(left == right));
2763 dVAR; dSP; tryAMAGICbinSET(ne,0);
2766 SETs(boolSV(left != right));
2773 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
2780 else if (left < right)
2791 dVAR; dSP; dTARGET; tryAMAGICun(neg);
2796 /* High falutin' math. */
2800 dVAR; dSP; dTARGET; tryAMAGICbin(atan2,0);
2803 SETn(Perl_atan2(left, right));
2811 int amg_type = sin_amg;
2812 const char *neg_report = NULL;
2813 NV (*func)(NV) = Perl_sin;
2814 const int op_type = PL_op->op_type;
2831 amg_type = sqrt_amg;
2833 neg_report = "sqrt";
2837 tryAMAGICun_var(amg_type);
2839 const NV value = POPn;
2841 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2842 SET_NUMERIC_STANDARD();
2843 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2846 XPUSHn(func(value));
2851 /* Support Configure command-line overrides for rand() functions.
2852 After 5.005, perhaps we should replace this by Configure support
2853 for drand48(), random(), or rand(). For 5.005, though, maintain
2854 compatibility by calling rand() but allow the user to override it.
2855 See INSTALL for details. --Andy Dougherty 15 July 1998
2857 /* Now it's after 5.005, and Configure supports drand48() and random(),
2858 in addition to rand(). So the overrides should not be needed any more.
2859 --Jarkko Hietaniemi 27 September 1998
2862 #ifndef HAS_DRAND48_PROTO
2863 extern double drand48 (void);
2876 if (!PL_srand_called) {
2877 (void)seedDrand01((Rand_seed_t)seed());
2878 PL_srand_called = TRUE;
2888 const UV anum = (MAXARG < 1) ? seed() : POPu;
2889 (void)seedDrand01((Rand_seed_t)anum);
2890 PL_srand_called = TRUE;
2897 dVAR; dSP; dTARGET; tryAMAGICun(int);
2899 SV * const sv = sv_2num(TOPs);
2900 const IV iv = SvIV(sv);
2901 /* XXX it's arguable that compiler casting to IV might be subtly
2902 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2903 else preferring IV has introduced a subtle behaviour change bug. OTOH
2904 relying on floating point to be accurate is a bug. */
2909 else if (SvIOK(sv)) {
2916 const NV value = SvNV(sv);
2918 if (value < (NV)UV_MAX + 0.5) {
2921 SETn(Perl_floor(value));
2925 if (value > (NV)IV_MIN - 0.5) {
2928 SETn(Perl_ceil(value));
2938 dVAR; dSP; dTARGET; tryAMAGICun(abs);
2940 SV * const sv = sv_2num(TOPs);
2941 /* This will cache the NV value if string isn't actually integer */
2942 const IV iv = SvIV(sv);
2947 else if (SvIOK(sv)) {
2948 /* IVX is precise */
2950 SETu(SvUV(sv)); /* force it to be numeric only */
2958 /* 2s complement assumption. Also, not really needed as
2959 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2965 const NV value = SvNV(sv);
2979 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2983 SV* const sv = POPs;
2985 tmps = (SvPV_const(sv, len));
2987 /* If Unicode, try to downgrade
2988 * If not possible, croak. */
2989 SV* const tsv = sv_2mortal(newSVsv(sv));
2992 sv_utf8_downgrade(tsv, FALSE);
2993 tmps = SvPV_const(tsv, len);
2995 if (PL_op->op_type == OP_HEX)
2998 while (*tmps && len && isSPACE(*tmps))
3004 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3006 else if (*tmps == 'b')
3007 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3009 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3011 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3025 SV * const sv = TOPs;
3028 /* For an overloaded scalar, we can't know in advance if it's going to
3029 be UTF-8 or not. Also, we can't call sv_len_utf8 as it likes to
3030 cache the length. Maybe that should be a documented feature of it.
3033 const char *const p = SvPV_const(sv, len);
3036 SETi(utf8_length((U8*)p, (U8*)p + len));
3042 else if (DO_UTF8(sv))
3043 SETi(sv_len_utf8(sv));
3059 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3061 const I32 arybase = CopARYBASE_get(PL_curcop);
3063 const char *repl = NULL;
3065 const int num_args = PL_op->op_private & 7;
3066 bool repl_need_utf8_upgrade = FALSE;
3067 bool repl_is_utf8 = FALSE;
3069 SvTAINTED_off(TARG); /* decontaminate */
3070 SvUTF8_off(TARG); /* decontaminate */
3074 repl = SvPV_const(repl_sv, repl_len);
3075 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3085 sv_utf8_upgrade(sv);
3087 else if (DO_UTF8(sv))
3088 repl_need_utf8_upgrade = TRUE;
3090 tmps = SvPV_const(sv, curlen);
3092 utf8_curlen = sv_len_utf8(sv);
3093 if (utf8_curlen == curlen)
3096 curlen = utf8_curlen;
3101 if (pos >= arybase) {
3119 else if (len >= 0) {
3121 if (rem > (I32)curlen)
3136 Perl_croak(aTHX_ "substr outside of string");
3137 if (ckWARN(WARN_SUBSTR))
3138 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3142 const I32 upos = pos;
3143 const I32 urem = rem;
3145 sv_pos_u2b(sv, &pos, &rem);
3147 /* we either return a PV or an LV. If the TARG hasn't been used
3148 * before, or is of that type, reuse it; otherwise use a mortal
3149 * instead. Note that LVs can have an extended lifetime, so also
3150 * dont reuse if refcount > 1 (bug #20933) */
3151 if (SvTYPE(TARG) > SVt_NULL) {
3152 if ( (SvTYPE(TARG) == SVt_PVLV)
3153 ? (!lvalue || SvREFCNT(TARG) > 1)
3156 TARG = sv_newmortal();
3160 sv_setpvn(TARG, tmps, rem);
3161 #ifdef USE_LOCALE_COLLATE
3162 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3167 SV* repl_sv_copy = NULL;
3169 if (repl_need_utf8_upgrade) {
3170 repl_sv_copy = newSVsv(repl_sv);
3171 sv_utf8_upgrade(repl_sv_copy);
3172 repl = SvPV_const(repl_sv_copy, repl_len);
3173 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3175 sv_insert(sv, pos, rem, repl, repl_len);
3179 SvREFCNT_dec(repl_sv_copy);
3181 else if (lvalue) { /* it's an lvalue! */
3182 if (!SvGMAGICAL(sv)) {
3184 SvPV_force_nolen(sv);
3185 if (ckWARN(WARN_SUBSTR))
3186 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3187 "Attempt to use reference as lvalue in substr");
3189 if (isGV_with_GP(sv))
3190 SvPV_force_nolen(sv);
3191 else if (SvOK(sv)) /* is it defined ? */
3192 (void)SvPOK_only_UTF8(sv);
3194 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3197 if (SvTYPE(TARG) < SVt_PVLV) {
3198 sv_upgrade(TARG, SVt_PVLV);
3199 sv_magic(TARG, NULL, PERL_MAGIC_substr, NULL, 0);
3203 if (LvTARG(TARG) != sv) {
3205 SvREFCNT_dec(LvTARG(TARG));
3206 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
3208 LvTARGOFF(TARG) = upos;
3209 LvTARGLEN(TARG) = urem;
3213 PUSHs(TARG); /* avoid SvSETMAGIC here */
3220 register const IV size = POPi;
3221 register const IV offset = POPi;
3222 register SV * const src = POPs;
3223 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3225 SvTAINTED_off(TARG); /* decontaminate */
3226 if (lvalue) { /* it's an lvalue! */
3227 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3228 TARG = sv_newmortal();
3229 if (SvTYPE(TARG) < SVt_PVLV) {
3230 sv_upgrade(TARG, SVt_PVLV);
3231 sv_magic(TARG, NULL, PERL_MAGIC_vec, NULL, 0);
3234 if (LvTARG(TARG) != src) {
3236 SvREFCNT_dec(LvTARG(TARG));
3237 LvTARG(TARG) = SvREFCNT_inc_simple(src);
3239 LvTARGOFF(TARG) = offset;
3240 LvTARGLEN(TARG) = size;
3243 sv_setuv(TARG, do_vecget(src, offset, size));
3259 const char *little_p;
3260 const I32 arybase = CopARYBASE_get(PL_curcop);
3263 const bool is_index = PL_op->op_type == OP_INDEX;
3266 /* arybase is in characters, like offset, so combine prior to the
3267 UTF-8 to bytes calculation. */
3268 offset = POPi - arybase;
3272 big_p = SvPV_const(big, biglen);
3273 little_p = SvPV_const(little, llen);
3275 big_utf8 = DO_UTF8(big);
3276 little_utf8 = DO_UTF8(little);
3277 if (big_utf8 ^ little_utf8) {
3278 /* One needs to be upgraded. */
3279 if (little_utf8 && !PL_encoding) {
3280 /* Well, maybe instead we might be able to downgrade the small
3282 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3285 /* If the large string is ISO-8859-1, and it's not possible to
3286 convert the small string to ISO-8859-1, then there is no
3287 way that it could be found anywhere by index. */
3292 /* At this point, pv is a malloc()ed string. So donate it to temp
3293 to ensure it will get free()d */
3294 little = temp = newSV(0);
3295 sv_usepvn(temp, pv, llen);
3296 little_p = SvPVX(little);
3299 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3302 sv_recode_to_utf8(temp, PL_encoding);
3304 sv_utf8_upgrade(temp);
3309 big_p = SvPV_const(big, biglen);
3312 little_p = SvPV_const(little, llen);
3316 if (SvGAMAGIC(big)) {
3317 /* Life just becomes a lot easier if I use a temporary here.
3318 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3319 will trigger magic and overloading again, as will fbm_instr()
3321 big = sv_2mortal(newSVpvn(big_p, biglen));
3326 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3327 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3328 warn on undef, and we've already triggered a warning with the
3329 SvPV_const some lines above. We can't remove that, as we need to
3330 call some SvPV to trigger overloading early and find out if the
3332 This is all getting to messy. The API isn't quite clean enough,
3333 because data access has side effects.
3335 little = sv_2mortal(newSVpvn(little_p, llen));
3338 little_p = SvPVX(little);
3342 offset = is_index ? 0 : biglen;
3344 if (big_utf8 && offset > 0)
3345 sv_pos_u2b(big, &offset, 0);
3351 else if (offset > (I32)biglen)
3353 if (!(little_p = is_index
3354 ? fbm_instr((unsigned char*)big_p + offset,
3355 (unsigned char*)big_p + biglen, little, 0)
3356 : rninstr(big_p, big_p + offset,
3357 little_p, little_p + llen)))
3360 retval = little_p - big_p;
3361 if (retval > 0 && big_utf8)
3362 sv_pos_b2u(big, &retval);
3367 PUSHi(retval + arybase);
3373 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3374 if (SvTAINTED(MARK[1]))
3375 TAINT_PROPER("sprintf");
3376 do_sprintf(TARG, SP-MARK, MARK+1);
3377 TAINT_IF(SvTAINTED(TARG));
3389 const U8 *s = (U8*)SvPV_const(argsv, len);
3391 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3392 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3393 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3397 XPUSHu(DO_UTF8(argsv) ?
3398 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3410 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3412 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3414 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3416 (void) POPs; /* Ignore the argument value. */
3417 value = UNICODE_REPLACEMENT;
3423 SvUPGRADE(TARG,SVt_PV);
3425 if (value > 255 && !IN_BYTES) {
3426 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3427 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3428 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3430 (void)SvPOK_only(TARG);
3439 *tmps++ = (char)value;
3441 (void)SvPOK_only(TARG);
3443 if (PL_encoding && !IN_BYTES) {
3444 sv_recode_to_utf8(TARG, PL_encoding);
3446 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3447 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3451 *tmps++ = (char)value;
3467 const char *tmps = SvPV_const(left, len);
3469 if (DO_UTF8(left)) {
3470 /* If Unicode, try to downgrade.
3471 * If not possible, croak.
3472 * Yes, we made this up. */
3473 SV* const tsv = sv_2mortal(newSVsv(left));
3476 sv_utf8_downgrade(tsv, FALSE);
3477 tmps = SvPV_const(tsv, len);
3479 # ifdef USE_ITHREADS
3481 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3482 /* This should be threadsafe because in ithreads there is only
3483 * one thread per interpreter. If this would not be true,
3484 * we would need a mutex to protect this malloc. */
3485 PL_reentrant_buffer->_crypt_struct_buffer =
3486 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3487 #if defined(__GLIBC__) || defined(__EMX__)
3488 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3489 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3490 /* work around glibc-2.2.5 bug */
3491 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3495 # endif /* HAS_CRYPT_R */
3496 # endif /* USE_ITHREADS */
3498 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3500 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3506 "The crypt() function is unimplemented due to excessive paranoia.");
3518 bool inplace = TRUE;
3520 const int op_type = PL_op->op_type;
3523 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3529 s = (const U8*)SvPV_nomg_const(source, slen);
3535 if (slen && DO_UTF8(source) && UTF8_IS_START(*s)) {
3537 utf8_to_uvchr(s, &ulen);
3538 if (op_type == OP_UCFIRST) {
3539 toTITLE_utf8(s, tmpbuf, &tculen);
3541 toLOWER_utf8(s, tmpbuf, &tculen);
3543 /* If the two differ, we definately cannot do inplace. */
3544 inplace = (ulen == tculen);
3545 need = slen + 1 - ulen + tculen;
3551 if (SvPADTMP(source) && !SvREADONLY(source) && inplace && SvTEMP(source)) {
3552 /* We can convert in place. */
3555 s = d = (U8*)SvPV_force_nomg(source, slen);
3561 SvUPGRADE(dest, SVt_PV);
3562 d = (U8*)SvGROW(dest, need);
3563 (void)SvPOK_only(dest);
3572 /* slen is the byte length of the whole SV.
3573 * ulen is the byte length of the original Unicode character
3574 * stored as UTF-8 at s.
3575 * tculen is the byte length of the freshly titlecased (or
3576 * lowercased) Unicode character stored as UTF-8 at tmpbuf.
3577 * We first set the result to be the titlecased (/lowercased)
3578 * character, and then append the rest of the SV data. */
3579 sv_setpvn(dest, (char*)tmpbuf, tculen);
3581 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3585 Copy(tmpbuf, d, tculen, U8);
3586 SvCUR_set(dest, need - 1);
3591 if (IN_LOCALE_RUNTIME) {
3594 *d = (op_type == OP_UCFIRST)
3595 ? toUPPER_LC(*s) : toLOWER_LC(*s);
3598 *d = (op_type == OP_UCFIRST) ? toUPPER(*s) : toLOWER(*s);
3600 /* See bug #39028 */
3608 /* This will copy the trailing NUL */
3609 Copy(s + 1, d + 1, slen, U8);
3610 SvCUR_set(dest, need - 1);
3617 /* There's so much setup/teardown code common between uc and lc, I wonder if
3618 it would be worth merging the two, and just having a switch outside each
3619 of the three tight loops. */
3633 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3634 && SvTEMP(source) && !DO_UTF8(source)) {
3635 /* We can convert in place. */
3638 s = d = (U8*)SvPV_force_nomg(source, len);
3645 /* The old implementation would copy source into TARG at this point.
3646 This had the side effect that if source was undef, TARG was now
3647 an undefined SV with PADTMP set, and they don't warn inside
3648 sv_2pv_flags(). However, we're now getting the PV direct from
3649 source, which doesn't have PADTMP set, so it would warn. Hence the
3653 s = (const U8*)SvPV_nomg_const(source, len);
3660 SvUPGRADE(dest, SVt_PV);
3661 d = (U8*)SvGROW(dest, min);
3662 (void)SvPOK_only(dest);
3667 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3668 to check DO_UTF8 again here. */
3670 if (DO_UTF8(source)) {
3671 const U8 *const send = s + len;
3672 U8 tmpbuf[UTF8_MAXBYTES+1];
3675 const STRLEN u = UTF8SKIP(s);
3678 toUPPER_utf8(s, tmpbuf, &ulen);
3679 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3680 /* If the eventually required minimum size outgrows
3681 * the available space, we need to grow. */
3682 const UV o = d - (U8*)SvPVX_const(dest);
3684 /* If someone uppercases one million U+03B0s we SvGROW() one
3685 * million times. Or we could try guessing how much to
3686 allocate without allocating too much. Such is life. */
3688 d = (U8*)SvPVX(dest) + o;
3690 Copy(tmpbuf, d, ulen, U8);
3696 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3699 const U8 *const send = s + len;
3700 if (IN_LOCALE_RUNTIME) {
3703 for (; s < send; d++, s++)
3704 *d = toUPPER_LC(*s);
3707 for (; s < send; d++, s++)
3711 if (source != dest) {
3713 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3733 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3734 && SvTEMP(source) && !DO_UTF8(source)) {
3735 /* We can convert in place. */
3738 s = d = (U8*)SvPV_force_nomg(source, len);
3745 /* The old implementation would copy source into TARG at this point.
3746 This had the side effect that if source was undef, TARG was now
3747 an undefined SV with PADTMP set, and they don't warn inside
3748 sv_2pv_flags(). However, we're now getting the PV direct from
3749 source, which doesn't have PADTMP set, so it would warn. Hence the
3753 s = (const U8*)SvPV_nomg_const(source, len);
3760 SvUPGRADE(dest, SVt_PV);
3761 d = (U8*)SvGROW(dest, min);
3762 (void)SvPOK_only(dest);
3767 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3768 to check DO_UTF8 again here. */
3770 if (DO_UTF8(source)) {
3771 const U8 *const send = s + len;
3772 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3775 const STRLEN u = UTF8SKIP(s);
3777 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3779 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode U+03A3 */
3780 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3783 * Now if the sigma is NOT followed by
3784 * /$ignorable_sequence$cased_letter/;
3785 * and it IS preceded by /$cased_letter$ignorable_sequence/;
3786 * where $ignorable_sequence is [\x{2010}\x{AD}\p{Mn}]*
3787 * and $cased_letter is [\p{Ll}\p{Lo}\p{Lt}]
3788 * then it should be mapped to 0x03C2,
3789 * (GREEK SMALL LETTER FINAL SIGMA),
3790 * instead of staying 0x03A3.
3791 * "should be": in other words, this is not implemented yet.
3792 * See lib/unicore/SpecialCasing.txt.
3795 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3796 /* If the eventually required minimum size outgrows
3797 * the available space, we need to grow. */
3798 const UV o = d - (U8*)SvPVX_const(dest);
3800 /* If someone lowercases one million U+0130s we SvGROW() one
3801 * million times. Or we could try guessing how much to
3802 allocate without allocating too much. Such is life. */
3804 d = (U8*)SvPVX(dest) + o;
3806 Copy(tmpbuf, d, ulen, U8);
3812 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3815 const U8 *const send = s + len;
3816 if (IN_LOCALE_RUNTIME) {
3819 for (; s < send; d++, s++)
3820 *d = toLOWER_LC(*s);
3823 for (; s < send; d++, s++)
3827 if (source != dest) {
3829 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3839 SV * const sv = TOPs;
3841 register const char *s = SvPV_const(sv,len);
3843 SvUTF8_off(TARG); /* decontaminate */
3846 SvUPGRADE(TARG, SVt_PV);
3847 SvGROW(TARG, (len * 2) + 1);
3851 if (UTF8_IS_CONTINUED(*s)) {
3852 STRLEN ulen = UTF8SKIP(s);
3876 SvCUR_set(TARG, d - SvPVX_const(TARG));
3877 (void)SvPOK_only_UTF8(TARG);
3880 sv_setpvn(TARG, s, len);
3882 if (SvSMAGICAL(TARG))
3891 dVAR; dSP; dMARK; dORIGMARK;
3892 register AV* const av = (AV*)POPs;
3893 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3895 if (SvTYPE(av) == SVt_PVAV) {
3896 const I32 arybase = CopARYBASE_get(PL_curcop);
3897 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3900 for (svp = MARK + 1; svp <= SP; svp++) {
3901 const I32 elem = SvIV(*svp);
3905 if (max > AvMAX(av))
3908 while (++MARK <= SP) {
3910 I32 elem = SvIV(*MARK);
3914 svp = av_fetch(av, elem, lval);
3916 if (!svp || *svp == &PL_sv_undef)
3917 DIE(aTHX_ PL_no_aelem, elem);
3918 if (PL_op->op_private & OPpLVAL_INTRO)
3919 save_aelem(av, elem, svp);
3921 *MARK = svp ? *svp : &PL_sv_undef;
3924 if (GIMME != G_ARRAY) {
3926 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
3932 /* Associative arrays. */
3938 HV * hash = (HV*)POPs;
3940 const I32 gimme = GIMME_V;
3943 /* might clobber stack_sp */
3944 entry = hv_iternext(hash);
3949 SV* const sv = hv_iterkeysv(entry);
3950 PUSHs(sv); /* won't clobber stack_sp */
3951 if (gimme == G_ARRAY) {
3954 /* might clobber stack_sp */
3955 val = hv_iterval(hash, entry);
3960 else if (gimme == G_SCALAR)
3970 const I32 gimme = GIMME_V;
3971 const I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3973 if (PL_op->op_private & OPpSLICE) {
3975 HV * const hv = (HV*)POPs;
3976 const U32 hvtype = SvTYPE(hv);
3977 if (hvtype == SVt_PVHV) { /* hash element */
3978 while (++MARK <= SP) {
3979 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
3980 *MARK = sv ? sv : &PL_sv_undef;
3983 else if (hvtype == SVt_PVAV) { /* array element */
3984 if (PL_op->op_flags & OPf_SPECIAL) {
3985 while (++MARK <= SP) {
3986 SV * const sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3987 *MARK = sv ? sv : &PL_sv_undef;
3992 DIE(aTHX_ "Not a HASH reference");
3995 else if (gimme == G_SCALAR) {
4000 *++MARK = &PL_sv_undef;
4006 HV * const hv = (HV*)POPs;
4008 if (SvTYPE(hv) == SVt_PVHV)
4009 sv = hv_delete_ent(hv, keysv, discard, 0);
4010 else if (SvTYPE(hv) == SVt_PVAV) {
4011 if (PL_op->op_flags & OPf_SPECIAL)
4012 sv = av_delete((AV*)hv, SvIV(keysv), discard);
4014 DIE(aTHX_ "panic: avhv_delete no longer supported");
4017 DIE(aTHX_ "Not a HASH reference");
4033 if (PL_op->op_private & OPpEXISTS_SUB) {
4035 SV * const sv = POPs;
4036 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4039 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4045 if (SvTYPE(hv) == SVt_PVHV) {
4046 if (hv_exists_ent(hv, tmpsv, 0))
4049 else if (SvTYPE(hv) == SVt_PVAV) {
4050 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4051 if (av_exists((AV*)hv, SvIV(tmpsv)))
4056 DIE(aTHX_ "Not a HASH reference");
4063 dVAR; dSP; dMARK; dORIGMARK;
4064 register HV * const hv = (HV*)POPs;
4065 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4066 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4067 bool other_magic = FALSE;
4073 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
4074 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
4075 /* Try to preserve the existenceness of a tied hash
4076 * element by using EXISTS and DELETE if possible.
4077 * Fallback to FETCH and STORE otherwise */
4078 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
4079 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
4080 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
4083 while (++MARK <= SP) {
4084 SV * const keysv = *MARK;
4087 bool preeminent = FALSE;
4090 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
4091 hv_exists_ent(hv, keysv, 0);
4094 he = hv_fetch_ent(hv, keysv, lval, 0);
4095 svp = he ? &HeVAL(he) : NULL;
4098 if (!svp || *svp == &PL_sv_undef) {
4099 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4102 if (HvNAME_get(hv) && isGV(*svp))
4103 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
4106 save_helem(hv, keysv, svp);
4109 const char * const key = SvPV_const(keysv, keylen);
4110 SAVEDELETE(hv, savepvn(key,keylen),
4111 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
4116 *MARK = svp ? *svp : &PL_sv_undef;
4118 if (GIMME != G_ARRAY) {
4120 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4126 /* List operators. */
4131 if (GIMME != G_ARRAY) {
4133 *MARK = *SP; /* unwanted list, return last item */
4135 *MARK = &PL_sv_undef;
4145 SV ** const lastrelem = PL_stack_sp;
4146 SV ** const lastlelem = PL_stack_base + POPMARK;
4147 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4148 register SV ** const firstrelem = lastlelem + 1;
4149 const I32 arybase = CopARYBASE_get(PL_curcop);
4150 I32 is_something_there = FALSE;
4152 register const I32 max = lastrelem - lastlelem;
4153 register SV **lelem;
4155 if (GIMME != G_ARRAY) {
4156 I32 ix = SvIV(*lastlelem);
4161 if (ix < 0 || ix >= max)
4162 *firstlelem = &PL_sv_undef;
4164 *firstlelem = firstrelem[ix];
4170 SP = firstlelem - 1;
4174 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4175 I32 ix = SvIV(*lelem);
4180 if (ix < 0 || ix >= max)
4181 *lelem = &PL_sv_undef;
4183 is_something_there = TRUE;
4184 if (!(*lelem = firstrelem[ix]))
4185 *lelem = &PL_sv_undef;
4188 if (is_something_there)
4191 SP = firstlelem - 1;
4197 dVAR; dSP; dMARK; dORIGMARK;
4198 const I32 items = SP - MARK;
4199 SV * const av = (SV *) av_make(items, MARK+1);
4200 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4201 XPUSHs(sv_2mortal((PL_op->op_flags & OPf_SPECIAL)
4202 ? newRV_noinc(av) : av));
4208 dVAR; dSP; dMARK; dORIGMARK;
4209 HV* const hv = newHV();
4212 SV * const key = *++MARK;
4213 SV * const val = newSV(0);
4215 sv_setsv(val, *++MARK);
4216 else if (ckWARN(WARN_MISC))
4217 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4218 (void)hv_store_ent(hv,key,val,0);
4221 XPUSHs(sv_2mortal((PL_op->op_flags & OPf_SPECIAL)
4222 ? newRV_noinc((SV*) hv) : (SV*)hv));
4228 dVAR; dSP; dMARK; dORIGMARK;
4229 register AV *ary = (AV*)*++MARK;
4233 register I32 offset;
4234 register I32 length;
4238 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4241 *MARK-- = SvTIED_obj((SV*)ary, mg);
4245 call_method("SPLICE",GIMME_V);
4254 offset = i = SvIV(*MARK);
4256 offset += AvFILLp(ary) + 1;
4258 offset -= CopARYBASE_get(PL_curcop);
4260 DIE(aTHX_ PL_no_aelem, i);
4262 length = SvIVx(*MARK++);
4264 length += AvFILLp(ary) - offset + 1;
4270 length = AvMAX(ary) + 1; /* close enough to infinity */
4274 length = AvMAX(ary) + 1;
4276 if (offset > AvFILLp(ary) + 1) {
4277 if (ckWARN(WARN_MISC))
4278 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4279 offset = AvFILLp(ary) + 1;
4281 after = AvFILLp(ary) + 1 - (offset + length);
4282 if (after < 0) { /* not that much array */
4283 length += after; /* offset+length now in array */
4289 /* At this point, MARK .. SP-1 is our new LIST */
4292 diff = newlen - length;
4293 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4296 /* make new elements SVs now: avoid problems if they're from the array */
4297 for (dst = MARK, i = newlen; i; i--) {
4298 SV * const h = *dst;
4299 *dst++ = newSVsv(h);
4302 if (diff < 0) { /* shrinking the area */
4303 SV **tmparyval = NULL;
4305 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4306 Copy(MARK, tmparyval, newlen, SV*);
4309 MARK = ORIGMARK + 1;
4310 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4311 MEXTEND(MARK, length);
4312 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4314 EXTEND_MORTAL(length);
4315 for (i = length, dst = MARK; i; i--) {
4316 sv_2mortal(*dst); /* free them eventualy */
4323 *MARK = AvARRAY(ary)[offset+length-1];
4326 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4327 SvREFCNT_dec(*dst++); /* free them now */
4330 AvFILLp(ary) += diff;
4332 /* pull up or down? */
4334 if (offset < after) { /* easier to pull up */
4335 if (offset) { /* esp. if nothing to pull */
4336 src = &AvARRAY(ary)[offset-1];
4337 dst = src - diff; /* diff is negative */
4338 for (i = offset; i > 0; i--) /* can't trust Copy */
4342 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4346 if (after) { /* anything to pull down? */
4347 src = AvARRAY(ary) + offset + length;
4348 dst = src + diff; /* diff is negative */
4349 Move(src, dst, after, SV*);
4351 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4352 /* avoid later double free */
4356 dst[--i] = &PL_sv_undef;
4359 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4360 Safefree(tmparyval);
4363 else { /* no, expanding (or same) */
4364 SV** tmparyval = NULL;
4366 Newx(tmparyval, length, SV*); /* so remember deletion */
4367 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4370 if (diff > 0) { /* expanding */
4371 /* push up or down? */
4372 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4376 Move(src, dst, offset, SV*);
4378 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
4380 AvFILLp(ary) += diff;
4383 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4384 av_extend(ary, AvFILLp(ary) + diff);
4385 AvFILLp(ary) += diff;
4388 dst = AvARRAY(ary) + AvFILLp(ary);
4390 for (i = after; i; i--) {
4398 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4401 MARK = ORIGMARK + 1;
4402 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4404 Copy(tmparyval, MARK, length, SV*);
4406 EXTEND_MORTAL(length);
4407 for (i = length, dst = MARK; i; i--) {
4408 sv_2mortal(*dst); /* free them eventualy */
4415 else if (length--) {
4416 *MARK = tmparyval[length];
4419 while (length-- > 0)
4420 SvREFCNT_dec(tmparyval[length]);
4424 *MARK = &PL_sv_undef;
4425 Safefree(tmparyval);
4433 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4434 register AV * const ary = (AV*)*++MARK;
4435 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4438 *MARK-- = SvTIED_obj((SV*)ary, mg);
4442 call_method("PUSH",G_SCALAR|G_DISCARD);
4446 PUSHi( AvFILL(ary) + 1 );
4449 PL_delaymagic = DM_DELAY;
4450 for (++MARK; MARK <= SP; MARK++) {
4451 SV * const sv = newSV(0);
4453 sv_setsv(sv, *MARK);
4454 av_store(ary, AvFILLp(ary)+1, sv);
4456 if (PL_delaymagic & DM_ARRAY)
4461 PUSHi( AvFILLp(ary) + 1 );
4470 AV * const av = (AV*)POPs;
4471 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
4475 (void)sv_2mortal(sv);
4482 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4483 register AV *ary = (AV*)*++MARK;
4484 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4487 *MARK-- = SvTIED_obj((SV*)ary, mg);
4491 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4497 av_unshift(ary, SP - MARK);
4499 SV * const sv = newSVsv(*++MARK);
4500 (void)av_store(ary, i++, sv);
4504 PUSHi( AvFILL(ary) + 1 );
4511 SV ** const oldsp = SP;
4513 if (GIMME == G_ARRAY) {
4516 register SV * const tmp = *MARK;
4520 /* safe as long as stack cannot get extended in the above */
4525 register char *down;
4529 PADOFFSET padoff_du;
4531 SvUTF8_off(TARG); /* decontaminate */
4533 do_join(TARG, &PL_sv_no, MARK, SP);
4535 sv_setsv(TARG, (SP > MARK)
4537 : (padoff_du = find_rundefsvoffset(),
4538 (padoff_du == NOT_IN_PAD
4539 || PAD_COMPNAME_FLAGS_isOUR(padoff_du))
4540 ? DEFSV : PAD_SVl(padoff_du)));
4541 up = SvPV_force(TARG, len);
4543 if (DO_UTF8(TARG)) { /* first reverse each character */
4544 U8* s = (U8*)SvPVX(TARG);
4545 const U8* send = (U8*)(s + len);
4547 if (UTF8_IS_INVARIANT(*s)) {
4552 if (!utf8_to_uvchr(s, 0))
4556 down = (char*)(s - 1);
4557 /* reverse this character */
4561 *down-- = (char)tmp;
4567 down = SvPVX(TARG) + len - 1;
4571 *down-- = (char)tmp;
4573 (void)SvPOK_only_UTF8(TARG);
4585 register IV limit = POPi; /* note, negative is forever */
4586 SV * const sv = POPs;
4588 register const char *s = SvPV_const(sv, len);
4589 const bool do_utf8 = DO_UTF8(sv);
4590 const char *strend = s + len;
4592 register REGEXP *rx;
4594 register const char *m;
4596 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
4597 I32 maxiters = slen + 10;
4599 const I32 origlimit = limit;
4602 const I32 gimme = GIMME_V;
4603 const I32 oldsave = PL_savestack_ix;
4604 I32 make_mortal = 1;
4609 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4614 DIE(aTHX_ "panic: pp_split");
4617 TAINT_IF((rx->extflags & RXf_PMf_LOCALE) &&
4618 (rx->extflags & (RXf_WHITE | RXf_SKIPWHITE)));
4620 RX_MATCH_UTF8_set(rx, do_utf8);
4623 if (pm->op_pmreplrootu.op_pmtargetoff) {
4624 ary = GvAVn((GV*)PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
4627 if (pm->op_pmreplrootu.op_pmtargetgv) {
4628 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
4631 else if (gimme != G_ARRAY)
4632 ary = GvAVn(PL_defgv);
4635 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4641 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4643 XPUSHs(SvTIED_obj((SV*)ary, mg));
4650 for (i = AvFILLp(ary); i >= 0; i--)
4651 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4653 /* temporarily switch stacks */
4654 SAVESWITCHSTACK(PL_curstack, ary);
4658 base = SP - PL_stack_base;
4660 if (rx->extflags & RXf_SKIPWHITE) {
4662 while (*s == ' ' || is_utf8_space((U8*)s))
4665 else if (rx->extflags & RXf_PMf_LOCALE) {
4666 while (isSPACE_LC(*s))
4674 if (rx->extflags & PMf_MULTILINE) {
4679 limit = maxiters + 2;
4680 if (rx->extflags & RXf_WHITE) {
4683 /* this one uses 'm' and is a negative test */
4685 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
4686 const int t = UTF8SKIP(m);
4687 /* is_utf8_space returns FALSE for malform utf8 */
4693 } else if (rx->extflags & RXf_PMf_LOCALE) {
4694 while (m < strend && !isSPACE_LC(*m))
4697 while (m < strend && !isSPACE(*m))
4703 dstr = newSVpvn(s, m-s);
4707 (void)SvUTF8_on(dstr);
4710 /* skip the whitespace found last */
4712 s = m + UTF8SKIP(m);
4716 /* this one uses 's' and is a positive test */
4718 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
4720 } else if (rx->extflags & RXf_PMf_LOCALE) {
4721 while (s < strend && isSPACE_LC(*s))
4724 while (s < strend && isSPACE(*s))
4729 else if (rx->extflags & RXf_START_ONLY) {
4731 for (m = s; m < strend && *m != '\n'; m++)
4736 dstr = newSVpvn(s, m-s);
4740 (void)SvUTF8_on(dstr);
4745 else if (rx->extflags & RXf_NULL && !(s >= strend)) {
4747 Pre-extend the stack, either the number of bytes or
4748 characters in the string or a limited amount, triggered by:
4750 my ($x, $y) = split //, $str;
4754 const U32 items = limit - 1;
4762 /* keep track of how many bytes we skip over */
4765 dstr = newSVpvn(m, s-m);
4770 (void)SvUTF8_on(dstr);
4778 dstr = newSVpvn(s, 1);
4792 else if (do_utf8 == ((rx->extflags & RXf_UTF8) != 0) &&
4793 (rx->extflags & RXf_USE_INTUIT) && !rx->nparens
4794 && (rx->extflags & RXf_CHECK_ALL)
4795 && !(rx->extflags & RXf_ANCH)) {
4796 const int tail = (rx->extflags & RXf_INTUIT_TAIL);
4797 SV * const csv = CALLREG_INTUIT_STRING(rx);
4799 len = rx->minlenret;
4800 if (len == 1 && !(rx->extflags & RXf_UTF8) && !tail) {
4801 const char c = *SvPV_nolen_const(csv);
4803 for (m = s; m < strend && *m != c; m++)
4807 dstr = newSVpvn(s, m-s);
4811 (void)SvUTF8_on(dstr);
4813 /* The rx->minlen is in characters but we want to step
4814 * s ahead by bytes. */
4816 s = (char*)utf8_hop((U8*)m, len);
4818 s = m + len; /* Fake \n at the end */
4822 while (s < strend && --limit &&
4823 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4824 csv, multiline ? FBMrf_MULTILINE : 0)) )
4826 dstr = newSVpvn(s, m-s);
4830 (void)SvUTF8_on(dstr);
4832 /* The rx->minlen is in characters but we want to step
4833 * s ahead by bytes. */
4835 s = (char*)utf8_hop((U8*)m, len);
4837 s = m + len; /* Fake \n at the end */
4842 maxiters += slen * rx->nparens;
4843 while (s < strend && --limit)
4847 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
4850 if (rex_return == 0)
4852 TAINT_IF(RX_MATCH_TAINTED(rx));
4853 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4858 strend = s + (strend - m);
4860 m = rx->offs[0].start + orig;
4861 dstr = newSVpvn(s, m-s);
4865 (void)SvUTF8_on(dstr);
4869 for (i = 1; i <= (I32)rx->nparens; i++) {
4870 s = rx->offs[i].start + orig;
4871 m = rx->offs[i].end + orig;
4873 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4874 parens that didn't match -- they should be set to
4875 undef, not the empty string */
4876 if (m >= orig && s >= orig) {
4877 dstr = newSVpvn(s, m-s);
4880 dstr = &PL_sv_undef; /* undef, not "" */
4884 (void)SvUTF8_on(dstr);
4888 s = rx->offs[0].end + orig;
4892 iters = (SP - PL_stack_base) - base;
4893 if (iters > maxiters)
4894 DIE(aTHX_ "Split loop");
4896 /* keep field after final delim? */
4897 if (s < strend || (iters && origlimit)) {
4898 const STRLEN l = strend - s;
4899 dstr = newSVpvn(s, l);
4903 (void)SvUTF8_on(dstr);
4907 else if (!origlimit) {
4908 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
4909 if (TOPs && !make_mortal)
4912 *SP-- = &PL_sv_undef;
4917 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
4921 if (SvSMAGICAL(ary)) {
4926 if (gimme == G_ARRAY) {
4928 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4936 call_method("PUSH",G_SCALAR|G_DISCARD);
4939 if (gimme == G_ARRAY) {
4941 /* EXTEND should not be needed - we just popped them */
4943 for (i=0; i < iters; i++) {
4944 SV **svp = av_fetch(ary, i, FALSE);
4945 PUSHs((svp) ? *svp : &PL_sv_undef);
4952 if (gimme == G_ARRAY)
4964 SV *const sv = PAD_SVl(PL_op->op_targ);
4966 if (SvPADSTALE(sv)) {
4969 RETURNOP(cLOGOP->op_other);
4971 RETURNOP(cLOGOP->op_next);
4981 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4982 || SvTYPE(retsv) == SVt_PVCV) {
4983 retsv = refto(retsv);
4990 PP(unimplemented_op)
4993 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
4999 * c-indentation-style: bsd
5001 * indent-tabs-mode: t
5004 * ex: set ts=8 sts=4 sw=4 noet: