This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldiag: Correct diag names
[perl5.git] / pp.c
CommitLineData
a0d0e21e 1/* pp.c
79072805 2 *
1129b882
NC
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
79072805 5 *
a0d0e21e
LW
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.
79072805 8 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
12 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
15 *
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
a0d0e21e 17 */
79072805 18
166f8a29
DM
19/* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
24 */
25
79072805 26#include "EXTERN.h"
864dbfa3 27#define PERL_IN_PP_C
79072805 28#include "perl.h"
77bc9082 29#include "keywords.h"
79072805 30
a4af207c 31#include "reentr.h"
685289b5 32#include "regcharclass.h"
a4af207c 33
dfe9444c
AD
34/* XXX I can't imagine anyone who doesn't have this actually _needs_
35 it, since pid_t is an integral type.
36 --AD 2/20/1998
37*/
38#ifdef NEED_GETPID_PROTO
39extern Pid_t getpid (void);
8ac85365
NIS
40#endif
41
a78bc3c6
KW
42static const STRLEN small_mu_len = sizeof(GREEK_SMALL_LETTER_MU_UTF8) - 1;
43static const STRLEN capital_iota_len = sizeof(GREEK_CAPITAL_LETTER_IOTA_UTF8) - 1;
44
13017935
SM
45/* variations on pp_null */
46
93a17b20
LW
47PP(pp_stub)
48{
39644a26 49 dSP;
54310121 50 if (GIMME_V == G_SCALAR)
3280af22 51 XPUSHs(&PL_sv_undef);
93a17b20
LW
52 RETURN;
53}
54
79072805
LW
55/* Pushy stuff. */
56
a46a7b6e 57
93a17b20 58
ac217057
FC
59PP(pp_padcv)
60{
20b7effb 61 dSP; dTARGET;
97b03d64
FC
62 assert(SvTYPE(TARG) == SVt_PVCV);
63 XPUSHs(TARG);
64 RETURN;
ac217057
FC
65}
66
ecf9c8b7
FC
67PP(pp_introcv)
68{
20b7effb 69 dTARGET;
6d5c2147
FC
70 SvPADSTALE_off(TARG);
71 return NORMAL;
ecf9c8b7
FC
72}
73
13f89586
FC
74PP(pp_clonecv)
75{
20b7effb 76 dTARGET;
0f94cb1f
FC
77 CV * const protocv = PadnamePROTOCV(
78 PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG]
79 );
6d5c2147 80 assert(SvTYPE(TARG) == SVt_PVCV);
0f94cb1f
FC
81 assert(protocv);
82 if (CvISXSUB(protocv)) { /* constant */
6d5c2147 83 /* XXX Should we clone it here? */
6d5c2147
FC
84 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
85 to introcv and remove the SvPADSTALE_off. */
86 SAVEPADSVANDMORTALIZE(ARGTARG);
0f94cb1f 87 PAD_SVl(ARGTARG) = SvREFCNT_inc_simple_NN(protocv);
6d5c2147
FC
88 }
89 else {
0f94cb1f
FC
90 if (CvROOT(protocv)) {
91 assert(CvCLONE(protocv));
92 assert(!CvCLONED(protocv));
6d5c2147 93 }
0f94cb1f 94 cv_clone_into(protocv,(CV *)TARG);
6d5c2147
FC
95 SAVECLEARSV(PAD_SVl(ARGTARG));
96 }
97 return NORMAL;
13f89586
FC
98}
99
79072805
LW
100/* Translations. */
101
6f7909da
FC
102/* In some cases this function inspects PL_op. If this function is called
103 for new op types, more bool parameters may need to be added in place of
104 the checks.
105
106 When noinit is true, the absence of a gv will cause a retval of undef.
107 This is unrelated to the cv-to-gv assignment case.
6f7909da
FC
108*/
109
110static SV *
111S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
112 const bool noinit)
113{
f64c9ac5 114 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
ed6116ce 115 if (SvROK(sv)) {
93d7320b
DM
116 if (SvAMAGIC(sv)) {
117 sv = amagic_deref_call(sv, to_gv_amg);
93d7320b 118 }
e4a1664f 119 wasref:
ed6116ce 120 sv = SvRV(sv);
b1dadf13 121 if (SvTYPE(sv) == SVt_PVIO) {
159b6efe 122 GV * const gv = MUTABLE_GV(sv_newmortal());
885f468a 123 gv_init(gv, 0, "__ANONIO__", 10, 0);
a45c7426 124 GvIOp(gv) = MUTABLE_IO(sv);
b37c2d43 125 SvREFCNT_inc_void_NN(sv);
ad64d0ec 126 sv = MUTABLE_SV(gv);
ef54e1a4 127 }
81d52ecd
JH
128 else if (!isGV_with_GP(sv)) {
129 Perl_die(aTHX_ "Not a GLOB reference");
130 }
79072805
LW
131 }
132 else {
6e592b3a 133 if (!isGV_with_GP(sv)) {
f132ae69 134 if (!SvOK(sv)) {
b13b2135 135 /* If this is a 'my' scalar and flag is set then vivify
853846ea 136 * NI-S 1999/05/07
b13b2135 137 */
f132ae69 138 if (vivify_sv && sv != &PL_sv_undef) {
2c8ac474 139 GV *gv;
ce74145d 140 if (SvREADONLY(sv))
cb077ed2 141 Perl_croak_no_modify();
2c8ac474 142 if (cUNOP->op_targ) {
0bd48802 143 SV * const namesv = PAD_SV(cUNOP->op_targ);
94e7eb6f
FC
144 HV *stash = CopSTASH(PL_curcop);
145 if (SvTYPE(stash) != SVt_PVHV) stash = NULL;
159b6efe 146 gv = MUTABLE_GV(newSV(0));
94e7eb6f 147 gv_init_sv(gv, stash, namesv, 0);
2c8ac474
GS
148 }
149 else {
0bd48802 150 const char * const name = CopSTASHPV(PL_curcop);
6b10071b 151 gv = newGVgen_flags(name,
d14578b8 152 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
7bdb4ff0 153 SvREFCNT_inc_simple_void_NN(gv);
1d8d4d2a 154 }
43230e26 155 prepare_SV_for_RV(sv);
ad64d0ec 156 SvRV_set(sv, MUTABLE_SV(gv));
853846ea 157 SvROK_on(sv);
1d8d4d2a 158 SvSETMAGIC(sv);
853846ea 159 goto wasref;
2c8ac474 160 }
81d52ecd
JH
161 if (PL_op->op_flags & OPf_REF || strict) {
162 Perl_die(aTHX_ PL_no_usym, "a symbol");
163 }
599cee73 164 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 165 report_uninit(sv);
6f7909da 166 return &PL_sv_undef;
a0d0e21e 167 }
6f7909da 168 if (noinit)
35cd451c 169 {
77cb3b01
FC
170 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
171 sv, GV_ADDMG, SVt_PVGV
23496c6e 172 ))))
6f7909da 173 return &PL_sv_undef;
35cd451c
GS
174 }
175 else {
81d52ecd
JH
176 if (strict) {
177 Perl_die(aTHX_
fedf30e1 178 PL_no_symref_sv,
81d52ecd
JH
179 sv,
180 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
181 "a symbol"
182 );
183 }
e26df76a
NC
184 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
185 == OPpDONT_INIT_GV) {
186 /* We are the target of a coderef assignment. Return
187 the scalar unchanged, and let pp_sasssign deal with
188 things. */
6f7909da 189 return sv;
e26df76a 190 }
77cb3b01 191 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
35cd451c 192 }
2acc3314 193 /* FAKE globs in the symbol table cause weird bugs (#77810) */
96293f45 194 SvFAKE_off(sv);
93a17b20 195 }
79072805 196 }
8dc99089 197 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
2acc3314 198 SV *newsv = sv_newmortal();
5cf4b255 199 sv_setsv_flags(newsv, sv, 0);
2acc3314 200 SvFAKE_off(newsv);
d8906c05 201 sv = newsv;
2acc3314 202 }
6f7909da
FC
203 return sv;
204}
205
206PP(pp_rv2gv)
207{
20b7effb 208 dSP; dTOPss;
6f7909da
FC
209
210 sv = S_rv2gv(aTHX_
211 sv, PL_op->op_private & OPpDEREF,
212 PL_op->op_private & HINT_STRICT_REFS,
213 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
214 || PL_op->op_type == OP_READLINE
215 );
d8906c05
FC
216 if (PL_op->op_private & OPpLVAL_INTRO)
217 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
218 SETs(sv);
79072805
LW
219 RETURN;
220}
221
dc3c76f8
NC
222/* Helper function for pp_rv2sv and pp_rv2av */
223GV *
fe9845cc
RB
224Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
225 const svtype type, SV ***spp)
dc3c76f8 226{
dc3c76f8
NC
227 GV *gv;
228
7918f24d
NC
229 PERL_ARGS_ASSERT_SOFTREF2XV;
230
dc3c76f8
NC
231 if (PL_op->op_private & HINT_STRICT_REFS) {
232 if (SvOK(sv))
fedf30e1 233 Perl_die(aTHX_ PL_no_symref_sv, sv,
bf3d870f 234 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
dc3c76f8
NC
235 else
236 Perl_die(aTHX_ PL_no_usym, what);
237 }
238 if (!SvOK(sv)) {
fd1d9b5c 239 if (
c8fe3bdf 240 PL_op->op_flags & OPf_REF
fd1d9b5c 241 )
dc3c76f8
NC
242 Perl_die(aTHX_ PL_no_usym, what);
243 if (ckWARN(WARN_UNINITIALIZED))
244 report_uninit(sv);
245 if (type != SVt_PV && GIMME_V == G_ARRAY) {
246 (*spp)--;
247 return NULL;
248 }
249 **spp = &PL_sv_undef;
250 return NULL;
251 }
252 if ((PL_op->op_flags & OPf_SPECIAL) &&
253 !(PL_op->op_flags & OPf_MOD))
254 {
77cb3b01 255 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
dc3c76f8
NC
256 {
257 **spp = &PL_sv_undef;
258 return NULL;
259 }
260 }
261 else {
77cb3b01 262 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
dc3c76f8
NC
263 }
264 return gv;
265}
266
79072805
LW
267PP(pp_rv2sv)
268{
20b7effb 269 dSP; dTOPss;
c445ea15 270 GV *gv = NULL;
79072805 271
9026059d 272 SvGETMAGIC(sv);
ed6116ce 273 if (SvROK(sv)) {
93d7320b
DM
274 if (SvAMAGIC(sv)) {
275 sv = amagic_deref_call(sv, to_sv_amg);
93d7320b 276 }
f5284f61 277
ed6116ce 278 sv = SvRV(sv);
69f00f67 279 if (SvTYPE(sv) >= SVt_PVAV)
cea2e8a9 280 DIE(aTHX_ "Not a SCALAR reference");
79072805
LW
281 }
282 else {
159b6efe 283 gv = MUTABLE_GV(sv);
748a9306 284
6e592b3a 285 if (!isGV_with_GP(gv)) {
dc3c76f8
NC
286 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
287 if (!gv)
288 RETURN;
463ee0b2 289 }
29c711a3 290 sv = GvSVn(gv);
a0d0e21e 291 }
533c011a 292 if (PL_op->op_flags & OPf_MOD) {
82d03984
RGS
293 if (PL_op->op_private & OPpLVAL_INTRO) {
294 if (cUNOP->op_first->op_type == OP_NULL)
159b6efe 295 sv = save_scalar(MUTABLE_GV(TOPs));
82d03984
RGS
296 else if (gv)
297 sv = save_scalar(gv);
298 else
f1f66076 299 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
82d03984 300 }
533c011a 301 else if (PL_op->op_private & OPpDEREF)
9026059d 302 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
79072805 303 }
655f5b26 304 SPAGAIN; /* in case chasing soft refs reallocated the stack */
a0d0e21e 305 SETs(sv);
79072805
LW
306 RETURN;
307}
308
309PP(pp_av2arylen)
310{
20b7effb 311 dSP;
502c6561 312 AV * const av = MUTABLE_AV(TOPs);
02d85cc3
EB
313 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
314 if (lvalue) {
8160c8f5
DM
315 SV ** const svp = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
316 if (!*svp) {
317 *svp = newSV_type(SVt_PVMG);
318 sv_magic(*svp, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
02d85cc3 319 }
8160c8f5 320 SETs(*svp);
02d85cc3 321 } else {
e1dccc0d 322 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
79072805 323 }
79072805
LW
324 RETURN;
325}
326
a0d0e21e
LW
327PP(pp_pos)
328{
27a8dde8 329 dSP; dTOPss;
8ec5e241 330
78f9721b 331 if (PL_op->op_flags & OPf_MOD || LVRET) {
d14578b8 332 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV));/* Not TARG RT#67838 */
16eb5365
FC
333 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
334 LvTYPE(ret) = '.';
335 LvTARG(ret) = SvREFCNT_inc_simple(sv);
27a8dde8 336 SETs(ret); /* no SvSETMAGIC */
a0d0e21e
LW
337 }
338 else {
96c2a8ff 339 const MAGIC * const mg = mg_find_mglob(sv);
6174b39a 340 if (mg && mg->mg_len != -1) {
6174b39a 341 STRLEN i = mg->mg_len;
7b394f12
DM
342 if (PL_op->op_private & OPpTRUEBOOL)
343 SETs(i ? &PL_sv_yes : &PL_sv_zero);
344 else {
345 dTARGET;
346 if (mg->mg_flags & MGf_BYTES && DO_UTF8(sv))
347 i = sv_pos_b2u_flags(sv, i, SV_GMAGIC|SV_CONST_RETURN);
348 SETu(i);
349 }
27a8dde8 350 return NORMAL;
a0d0e21e 351 }
27a8dde8 352 SETs(&PL_sv_undef);
a0d0e21e 353 }
27a8dde8 354 return NORMAL;
a0d0e21e
LW
355}
356
79072805
LW
357PP(pp_rv2cv)
358{
20b7effb 359 dSP;
79072805 360 GV *gv;
1eced8f8 361 HV *stash_unused;
c445ea15 362 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
9da346da 363 ? GV_ADDMG
d14578b8
KW
364 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT))
365 == OPpMAY_RETURN_CONSTANT)
c445ea15
AL
366 ? GV_ADD|GV_NOEXPAND
367 : GV_ADD;
4633a7c4
LW
368 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
369 /* (But not in defined().) */
e26df76a 370
1eced8f8 371 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
5a20ba3d 372 if (cv) NOOP;
e26df76a 373 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
2eaf799e
FC
374 cv = SvTYPE(SvRV(gv)) == SVt_PVCV
375 ? MUTABLE_CV(SvRV(gv))
376 : MUTABLE_CV(gv);
e26df76a 377 }
07055b4c 378 else
ea726b52 379 cv = MUTABLE_CV(&PL_sv_undef);
ad64d0ec 380 SETs(MUTABLE_SV(cv));
3d79e3ee 381 return NORMAL;
79072805
LW
382}
383
c07a80fd 384PP(pp_prototype)
385{
20b7effb 386 dSP;
c07a80fd 387 CV *cv;
388 HV *stash;
389 GV *gv;
fabdb6c0 390 SV *ret = &PL_sv_undef;
c07a80fd 391
6954f42f 392 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
b6c543e3 393 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
e3f73d4e 394 const char * s = SvPVX_const(TOPs);
b6c543e3 395 if (strnEQ(s, "CORE::", 6)) {
be1b855b 396 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
a96df643 397 if (!code)
147e3846 398 DIE(aTHX_ "Can't find an opnumber for \"%" UTF8f "\"",
b17a0679 399 UTF8fARG(SvFLAGS(TOPs) & SVf_UTF8, SvCUR(TOPs)-6, s+6));
4e338c21 400 {
b66130dd
FC
401 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
402 if (sv) ret = sv;
403 }
b8c38f0a 404 goto set;
b6c543e3
IZ
405 }
406 }
f2c0649b 407 cv = sv_2cv(TOPs, &stash, &gv, 0);
5f05dabc 408 if (cv && SvPOK(cv))
8fa6a409
FC
409 ret = newSVpvn_flags(
410 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
411 );
b6c543e3 412 set:
c07a80fd 413 SETs(ret);
414 RETURN;
415}
416
a0d0e21e
LW
417PP(pp_anoncode)
418{
20b7effb 419 dSP;
ea726b52 420 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
a5f75d66 421 if (CvCLONE(cv))
ad64d0ec 422 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
5f05dabc 423 EXTEND(SP,1);
ad64d0ec 424 PUSHs(MUTABLE_SV(cv));
a0d0e21e
LW
425 RETURN;
426}
427
428PP(pp_srefgen)
79072805 429{
20b7effb 430 dSP;
71be2cbc 431 *SP = refto(*SP);
3ed34c76 432 return NORMAL;
8ec5e241 433}
a0d0e21e
LW
434
435PP(pp_refgen)
436{
20b7effb 437 dSP; dMARK;
82334630 438 if (GIMME_V != G_ARRAY) {
5f0b1d4e
GS
439 if (++MARK <= SP)
440 *MARK = *SP;
441 else
1d51ab6c
FC
442 {
443 MEXTEND(SP, 1);
3280af22 444 *MARK = &PL_sv_undef;
1d51ab6c 445 }
5f0b1d4e
GS
446 *MARK = refto(*MARK);
447 SP = MARK;
448 RETURN;
a0d0e21e 449 }
bbce6d69 450 EXTEND_MORTAL(SP - MARK);
71be2cbc 451 while (++MARK <= SP)
452 *MARK = refto(*MARK);
a0d0e21e 453 RETURN;
79072805
LW
454}
455
76e3520e 456STATIC SV*
cea2e8a9 457S_refto(pTHX_ SV *sv)
71be2cbc 458{
459 SV* rv;
460
7918f24d
NC
461 PERL_ARGS_ASSERT_REFTO;
462
71be2cbc 463 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
464 if (LvTARGLEN(sv))
68dc0745 465 vivify_defelem(sv);
466 if (!(sv = LvTARG(sv)))
3280af22 467 sv = &PL_sv_undef;
0dd88869 468 else
b37c2d43 469 SvREFCNT_inc_void_NN(sv);
71be2cbc 470 }
d8b46c1b 471 else if (SvTYPE(sv) == SVt_PVAV) {
502c6561
NC
472 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
473 av_reify(MUTABLE_AV(sv));
d8b46c1b 474 SvTEMP_off(sv);
b37c2d43 475 SvREFCNT_inc_void_NN(sv);
d8b46c1b 476 }
60779a30 477 else if (SvPADTMP(sv)) {
f2933f5f 478 sv = newSVsv(sv);
60779a30 479 }
71be2cbc 480 else {
481 SvTEMP_off(sv);
b37c2d43 482 SvREFCNT_inc_void_NN(sv);
71be2cbc 483 }
484 rv = sv_newmortal();
4df7f6af 485 sv_upgrade(rv, SVt_IV);
b162af07 486 SvRV_set(rv, sv);
71be2cbc 487 SvROK_on(rv);
488 return rv;
489}
490
79072805
LW
491PP(pp_ref)
492{
3c1e67ac
DD
493 dSP;
494 SV * const sv = TOPs;
f12c7020 495
511ddbdf 496 SvGETMAGIC(sv);
ba75e9a4 497 if (!SvROK(sv)) {
3c1e67ac 498 SETs(&PL_sv_no);
ba75e9a4
DM
499 return NORMAL;
500 }
501
502 /* op is in boolean context? */
503 if ( (PL_op->op_private & OPpTRUEBOOL)
504 || ( (PL_op->op_private & OPpMAYBE_TRUEBOOL)
505 && block_gimme() == G_VOID))
506 {
507 /* refs are always true - unless it's to an object blessed into a
508 * class with a false name, i.e. "0". So we have to check for
509 * that remote possibility. The following is is basically an
510 * unrolled SvTRUE(sv_reftype(rv)) */
511 SV * const rv = SvRV(sv);
512 if (SvOBJECT(rv)) {
513 HV *stash = SvSTASH(rv);
514 HEK *hek = HvNAME_HEK(stash);
515 if (hek) {
516 I32 len = HEK_LEN(hek);
517 /* bail out and do it the hard way? */
518 if (UNLIKELY(
519 len == HEf_SVKEY
520 || (len == 1 && HEK_KEY(hek)[0] == '0')
521 ))
522 goto do_sv_ref;
523 }
524 }
525 SETs(&PL_sv_yes);
526 return NORMAL;
527 }
528
529 do_sv_ref:
530 {
3c1e67ac
DD
531 dTARGET;
532 SETs(TARG);
ba75e9a4 533 sv_ref(TARG, SvRV(sv), TRUE);
a10e04b5 534 SvSETMAGIC(TARG);
ba75e9a4 535 return NORMAL;
3c1e67ac 536 }
79072805 537
79072805
LW
538}
539
ba75e9a4 540
79072805
LW
541PP(pp_bless)
542{
20b7effb 543 dSP;
463ee0b2 544 HV *stash;
79072805 545
463ee0b2 546 if (MAXARG == 1)
dcdfe746 547 {
c2f922f1 548 curstash:
11faa288 549 stash = CopSTASH(PL_curcop);
dcdfe746
FC
550 if (SvTYPE(stash) != SVt_PVHV)
551 Perl_croak(aTHX_ "Attempt to bless into a freed package");
552 }
7b8d334a 553 else {
1b6737cc 554 SV * const ssv = POPs;
7b8d334a 555 STRLEN len;
e1ec3a88 556 const char *ptr;
81689caa 557
c2f922f1 558 if (!ssv) goto curstash;
8d9dd4b9 559 SvGETMAGIC(ssv);
c7ea825d
FC
560 if (SvROK(ssv)) {
561 if (!SvAMAGIC(ssv)) {
562 frog:
81689caa 563 Perl_croak(aTHX_ "Attempt to bless into a reference");
c7ea825d
FC
564 }
565 /* SvAMAGIC is on here, but it only means potentially overloaded,
566 so after stringification: */
567 ptr = SvPV_nomg_const(ssv,len);
568 /* We need to check the flag again: */
569 if (!SvAMAGIC(ssv)) goto frog;
570 }
571 else ptr = SvPV_nomg_const(ssv,len);
a2a5de95
NC
572 if (len == 0)
573 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
574 "Explicit blessing to '' (assuming package main)");
e69c50fe 575 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
7b8d334a 576 }
a0d0e21e 577
5d3fdfeb 578 (void)sv_bless(TOPs, stash);
79072805
LW
579 RETURN;
580}
581
fb73857a 582PP(pp_gelem)
583{
20b7effb 584 dSP;
b13b2135 585
1b6737cc 586 SV *sv = POPs;
a180b31a
BF
587 STRLEN len;
588 const char * const elem = SvPV_const(sv, len);
5695161e 589 GV * const gv = MUTABLE_GV(TOPs);
c445ea15 590 SV * tmpRef = NULL;
1b6737cc 591
c445ea15 592 sv = NULL;
c4ba80c3
NC
593 if (elem) {
594 /* elem will always be NUL terminated. */
c4ba80c3
NC
595 switch (*elem) {
596 case 'A':
500f3e18 597 if (memEQs(elem, len, "ARRAY"))
e14698d8 598 {
ad64d0ec 599 tmpRef = MUTABLE_SV(GvAV(gv));
e14698d8
FC
600 if (tmpRef && !AvREAL((const AV *)tmpRef)
601 && AvREIFY((const AV *)tmpRef))
602 av_reify(MUTABLE_AV(tmpRef));
603 }
c4ba80c3
NC
604 break;
605 case 'C':
500f3e18 606 if (memEQs(elem, len, "CODE"))
ad64d0ec 607 tmpRef = MUTABLE_SV(GvCVu(gv));
c4ba80c3
NC
608 break;
609 case 'F':
500f3e18 610 if (memEQs(elem, len, "FILEHANDLE")) {
ad64d0ec 611 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
612 }
613 else
500f3e18 614 if (memEQs(elem, len, "FORMAT"))
ad64d0ec 615 tmpRef = MUTABLE_SV(GvFORM(gv));
c4ba80c3
NC
616 break;
617 case 'G':
500f3e18 618 if (memEQs(elem, len, "GLOB"))
ad64d0ec 619 tmpRef = MUTABLE_SV(gv);
c4ba80c3
NC
620 break;
621 case 'H':
500f3e18 622 if (memEQs(elem, len, "HASH"))
ad64d0ec 623 tmpRef = MUTABLE_SV(GvHV(gv));
c4ba80c3
NC
624 break;
625 case 'I':
500f3e18 626 if (memEQs(elem, len, "IO"))
ad64d0ec 627 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
628 break;
629 case 'N':
500f3e18 630 if (memEQs(elem, len, "NAME"))
a663657d 631 sv = newSVhek(GvNAME_HEK(gv));
c4ba80c3
NC
632 break;
633 case 'P':
500f3e18 634 if (memEQs(elem, len, "PACKAGE")) {
7fa3a4ab
NC
635 const HV * const stash = GvSTASH(gv);
636 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
396482e1 637 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
c4ba80c3
NC
638 }
639 break;
640 case 'S':
500f3e18 641 if (memEQs(elem, len, "SCALAR"))
f9d52e31 642 tmpRef = GvSVn(gv);
c4ba80c3 643 break;
39b99f21 644 }
fb73857a 645 }
76e3520e
GS
646 if (tmpRef)
647 sv = newRV(tmpRef);
fb73857a 648 if (sv)
649 sv_2mortal(sv);
650 else
3280af22 651 sv = &PL_sv_undef;
5695161e 652 SETs(sv);
fb73857a 653 RETURN;
654}
655
a0d0e21e 656/* Pattern matching */
79072805 657
a0d0e21e 658PP(pp_study)
79072805 659{
add3e777 660 dSP; dTOPss;
a0d0e21e
LW
661 STRLEN len;
662
1fa930f2 663 (void)SvPV(sv, len);
bc9a5256 664 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
32f0ea87 665 /* Historically, study was skipped in these cases. */
add3e777
FC
666 SETs(&PL_sv_no);
667 return NORMAL;
a4f4e906
NC
668 }
669
a58a85fa 670 /* Make study a no-op. It's no longer useful and its existence
32f0ea87 671 complicates matters elsewhere. */
add3e777
FC
672 SETs(&PL_sv_yes);
673 return NORMAL;
79072805
LW
674}
675
b1c05ba5
DM
676
677/* also used for: pp_transr() */
678
a0d0e21e 679PP(pp_trans)
79072805 680{
6442877a 681 dSP;
a0d0e21e
LW
682 SV *sv;
683
533c011a 684 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e 685 sv = POPs;
79072805 686 else {
a0d0e21e 687 EXTEND(SP,1);
f605e527 688 if (ARGTARG)
6442877a 689 sv = PAD_SV(ARGTARG);
f605e527
FC
690 else {
691 sv = DEFSV;
692 }
79072805 693 }
bb16bae8 694 if(PL_op->op_type == OP_TRANSR) {
290797f7
FC
695 STRLEN len;
696 const char * const pv = SvPV(sv,len);
697 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
bb16bae8 698 do_trans(newsv);
290797f7 699 PUSHs(newsv);
bb16bae8 700 }
5bbe7184 701 else {
bcb10b84
VP
702 I32 i = do_trans(sv);
703 mPUSHi(i);
5bbe7184 704 }
a0d0e21e 705 RETURN;
79072805
LW
706}
707
a0d0e21e 708/* Lvalue operators. */
79072805 709
f595e19f 710static size_t
81745e4e
NC
711S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
712{
81745e4e
NC
713 STRLEN len;
714 char *s;
f595e19f 715 size_t count = 0;
81745e4e
NC
716
717 PERL_ARGS_ASSERT_DO_CHOMP;
718
719 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
f595e19f 720 return 0;
81745e4e
NC
721 if (SvTYPE(sv) == SVt_PVAV) {
722 I32 i;
723 AV *const av = MUTABLE_AV(sv);
724 const I32 max = AvFILL(av);
725
726 for (i = 0; i <= max; i++) {
727 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
728 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
f595e19f 729 count += do_chomp(retval, sv, chomping);
81745e4e 730 }
f595e19f 731 return count;
81745e4e
NC
732 }
733 else if (SvTYPE(sv) == SVt_PVHV) {
734 HV* const hv = MUTABLE_HV(sv);
735 HE* entry;
736 (void)hv_iterinit(hv);
737 while ((entry = hv_iternext(hv)))
f595e19f
FC
738 count += do_chomp(retval, hv_iterval(hv,entry), chomping);
739 return count;
81745e4e
NC
740 }
741 else if (SvREADONLY(sv)) {
cb077ed2 742 Perl_croak_no_modify();
81745e4e
NC
743 }
744
81745e4e
NC
745 s = SvPV(sv, len);
746 if (chomping) {
81745e4e 747 if (s && len) {
997c424a
DD
748 char *temp_buffer = NULL;
749 SV *svrecode = NULL;
81745e4e
NC
750 s += --len;
751 if (RsPARA(PL_rs)) {
752 if (*s != '\n')
997c424a 753 goto nope_free_nothing;
f595e19f 754 ++count;
81745e4e
NC
755 while (len && s[-1] == '\n') {
756 --len;
757 --s;
f595e19f 758 ++count;
81745e4e
NC
759 }
760 }
761 else {
762 STRLEN rslen, rs_charlen;
763 const char *rsptr = SvPV_const(PL_rs, rslen);
764
765 rs_charlen = SvUTF8(PL_rs)
766 ? sv_len_utf8(PL_rs)
767 : rslen;
768
769 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
770 /* Assumption is that rs is shorter than the scalar. */
771 if (SvUTF8(PL_rs)) {
772 /* RS is utf8, scalar is 8 bit. */
773 bool is_utf8 = TRUE;
774 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
775 &rslen, &is_utf8);
776 if (is_utf8) {
997c424a
DD
777 /* Cannot downgrade, therefore cannot possibly match.
778 At this point, temp_buffer is not alloced, and
779 is the buffer inside PL_rs, so dont free it.
81745e4e
NC
780 */
781 assert (temp_buffer == rsptr);
997c424a 782 goto nope_free_sv;
81745e4e
NC
783 }
784 rsptr = temp_buffer;
785 }
81745e4e
NC
786 else {
787 /* RS is 8 bit, scalar is utf8. */
788 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
789 rsptr = temp_buffer;
790 }
791 }
792 if (rslen == 1) {
793 if (*s != *rsptr)
997c424a 794 goto nope_free_all;
f595e19f 795 ++count;
81745e4e
NC
796 }
797 else {
798 if (len < rslen - 1)
997c424a 799 goto nope_free_all;
81745e4e
NC
800 len -= rslen - 1;
801 s -= rslen - 1;
802 if (memNE(s, rsptr, rslen))
997c424a 803 goto nope_free_all;
f595e19f 804 count += rs_charlen;
81745e4e
NC
805 }
806 }
3b7ded39 807 SvPV_force_nomg_nolen(sv);
81745e4e
NC
808 SvCUR_set(sv, len);
809 *SvEND(sv) = '\0';
810 SvNIOK_off(sv);
811 SvSETMAGIC(sv);
81745e4e 812
997c424a
DD
813 nope_free_all:
814 Safefree(temp_buffer);
815 nope_free_sv:
816 SvREFCNT_dec(svrecode);
817 nope_free_nothing: ;
818 }
81745e4e 819 } else {
f8c80a8e 820 if (len && (!SvPOK(sv) || SvIsCOW(sv)))
81745e4e
NC
821 s = SvPV_force_nomg(sv, len);
822 if (DO_UTF8(sv)) {
823 if (s && len) {
824 char * const send = s + len;
825 char * const start = s;
826 s = send - 1;
827 while (s > start && UTF8_IS_CONTINUATION(*s))
828 s--;
829 if (is_utf8_string((U8*)s, send - s)) {
830 sv_setpvn(retval, s, send - s);
831 *s = '\0';
832 SvCUR_set(sv, s - start);
833 SvNIOK_off(sv);
834 SvUTF8_on(retval);
835 }
836 }
837 else
500f3e18 838 SvPVCLEAR(retval);
81745e4e
NC
839 }
840 else if (s && len) {
841 s += --len;
842 sv_setpvn(retval, s, 1);
843 *s = '\0';
844 SvCUR_set(sv, len);
845 SvUTF8_off(sv);
846 SvNIOK_off(sv);
847 }
848 else
500f3e18 849 SvPVCLEAR(retval);
81745e4e
NC
850 SvSETMAGIC(sv);
851 }
f595e19f 852 return count;
81745e4e
NC
853}
854
b1c05ba5
DM
855
856/* also used for: pp_schomp() */
857
a0d0e21e
LW
858PP(pp_schop)
859{
20b7effb 860 dSP; dTARGET;
fa54efae
NC
861 const bool chomping = PL_op->op_type == OP_SCHOMP;
862
f595e19f 863 const size_t count = do_chomp(TARG, TOPs, chomping);
fa54efae 864 if (chomping)
f595e19f 865 sv_setiv(TARG, count);
a0d0e21e 866 SETTARG;
ee41d8c7 867 return NORMAL;
79072805
LW
868}
869
b1c05ba5
DM
870
871/* also used for: pp_chomp() */
872
a0d0e21e 873PP(pp_chop)
79072805 874{
20b7effb 875 dSP; dMARK; dTARGET; dORIGMARK;
fa54efae 876 const bool chomping = PL_op->op_type == OP_CHOMP;
f595e19f 877 size_t count = 0;
8ec5e241 878
20cf1f79 879 while (MARK < SP)
f595e19f
FC
880 count += do_chomp(TARG, *++MARK, chomping);
881 if (chomping)
882 sv_setiv(TARG, count);
20cf1f79
NC
883 SP = ORIGMARK;
884 XPUSHTARG;
a0d0e21e 885 RETURN;
79072805
LW
886}
887
a0d0e21e
LW
888PP(pp_undef)
889{
20b7effb 890 dSP;
a0d0e21e
LW
891 SV *sv;
892
533c011a 893 if (!PL_op->op_private) {
774d564b 894 EXTEND(SP, 1);
a0d0e21e 895 RETPUSHUNDEF;
774d564b 896 }
79072805 897
821f14b0 898 sv = TOPs;
a0d0e21e 899 if (!sv)
821f14b0
FC
900 {
901 SETs(&PL_sv_undef);
902 return NORMAL;
903 }
85e6fe83 904
4dda930b
FC
905 if (SvTHINKFIRST(sv))
906 sv_force_normal_flags(sv, SV_COW_DROP_PV|SV_IMMEDIATE_UNREF);
85e6fe83 907
a0d0e21e
LW
908 switch (SvTYPE(sv)) {
909 case SVt_NULL:
910 break;
911 case SVt_PVAV:
60edcf09 912 av_undef(MUTABLE_AV(sv));
a0d0e21e
LW
913 break;
914 case SVt_PVHV:
60edcf09 915 hv_undef(MUTABLE_HV(sv));
a0d0e21e
LW
916 break;
917 case SVt_PVCV:
a2a5de95 918 if (cv_const_sv((const CV *)sv))
714cd18f 919 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
147e3846 920 "Constant subroutine %" SVf " undefined",
714cd18f
BF
921 SVfARG(CvANON((const CV *)sv)
922 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
bdbfc51a
FC
923 : sv_2mortal(newSVhek(
924 CvNAMED(sv)
925 ? CvNAME_HEK((CV *)sv)
926 : GvENAME_HEK(CvGV((const CV *)sv))
927 ))
928 ));
5f66b61c 929 /* FALLTHROUGH */
9607fc9c 930 case SVt_PVFM:
6fc92669 931 /* let user-undef'd sub keep its identity */
b7acb0a3 932 cv_undef_flags(MUTABLE_CV(sv), CV_UNDEF_KEEP_NAME);
a0d0e21e 933 break;
8e07c86e 934 case SVt_PVGV:
bc1df6c2
FC
935 assert(isGV_with_GP(sv));
936 assert(!SvFAKE(sv));
937 {
20408e3c 938 GP *gp;
dd69841b
BB
939 HV *stash;
940
dd69841b 941 /* undef *Pkg::meth_name ... */
e530fb81
FC
942 bool method_changed
943 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
944 && HvENAME_get(stash);
945 /* undef *Foo:: */
946 if((stash = GvHV((const GV *)sv))) {
947 if(HvENAME_get(stash))
948 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
949 else stash = NULL;
950 }
dd69841b 951
795eb8c8 952 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
159b6efe 953 gp_free(MUTABLE_GV(sv));
a02a5408 954 Newxz(gp, 1, GP);
c43ae56f 955 GvGP_set(sv, gp_ref(gp));
2e3295e3 956#ifndef PERL_DONT_CREATE_GVSV
561b68a9 957 GvSV(sv) = newSV(0);
2e3295e3 958#endif
57843af0 959 GvLINE(sv) = CopLINE(PL_curcop);
159b6efe 960 GvEGV(sv) = MUTABLE_GV(sv);
20408e3c 961 GvMULTI_on(sv);
e530fb81
FC
962
963 if(stash)
afdbe55d 964 mro_package_moved(NULL, stash, (const GV *)sv, 0);
e530fb81
FC
965 stash = NULL;
966 /* undef *Foo::ISA */
967 if( strEQ(GvNAME((const GV *)sv), "ISA")
968 && (stash = GvSTASH((const GV *)sv))
969 && (method_changed || HvENAME(stash)) )
970 mro_isa_changed_in(stash);
971 else if(method_changed)
972 mro_method_changed_in(
da9043f5 973 GvSTASH((const GV *)sv)
e530fb81
FC
974 );
975
6e592b3a 976 break;
20408e3c 977 }
a0d0e21e 978 default:
b15aece3 979 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
8bd4d4c5 980 SvPV_free(sv);
c445ea15 981 SvPV_set(sv, NULL);
4633a7c4 982 SvLEN_set(sv, 0);
a0d0e21e 983 }
0c34ef67 984 SvOK_off(sv);
4633a7c4 985 SvSETMAGIC(sv);
79072805 986 }
a0d0e21e 987
821f14b0
FC
988 SETs(&PL_sv_undef);
989 return NORMAL;
79072805
LW
990}
991
b1c05ba5 992
20e96431 993/* common "slow" code for pp_postinc and pp_postdec */
b1c05ba5 994
20e96431
DM
995static OP *
996S_postincdec_common(pTHX_ SV *sv, SV *targ)
a0d0e21e 997{
20e96431 998 dSP;
c22c99bc
FC
999 const bool inc =
1000 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
20e96431
DM
1001
1002 if (SvROK(sv))
7dcb9b98 1003 TARG = sv_newmortal();
20e96431
DM
1004 sv_setsv(TARG, sv);
1005 if (inc)
1006 sv_inc_nomg(sv);
1007 else
1008 sv_dec_nomg(sv);
1009 SvSETMAGIC(sv);
1e54a23f 1010 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
c22c99bc 1011 if (inc && !SvOK(TARG))
a0d0e21e 1012 sv_setiv(TARG, 0);
e87de4ab 1013 SETTARG;
a0d0e21e
LW
1014 return NORMAL;
1015}
79072805 1016
20e96431
DM
1017
1018/* also used for: pp_i_postinc() */
1019
1020PP(pp_postinc)
1021{
1022 dSP; dTARGET;
1023 SV *sv = TOPs;
1024
1025 /* special-case sv being a simple integer */
1026 if (LIKELY(((sv->sv_flags &
1027 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1028 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1029 == SVf_IOK))
1030 && SvIVX(sv) != IV_MAX)
1031 {
1032 IV iv = SvIVX(sv);
1033 SvIV_set(sv, iv + 1);
1034 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1035 SETs(TARG);
1036 return NORMAL;
1037 }
1038
1039 return S_postincdec_common(aTHX_ sv, TARG);
1040}
1041
1042
1043/* also used for: pp_i_postdec() */
1044
1045PP(pp_postdec)
1046{
1047 dSP; dTARGET;
1048 SV *sv = TOPs;
1049
1050 /* special-case sv being a simple integer */
1051 if (LIKELY(((sv->sv_flags &
1052 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1053 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1054 == SVf_IOK))
1055 && SvIVX(sv) != IV_MIN)
1056 {
1057 IV iv = SvIVX(sv);
1058 SvIV_set(sv, iv - 1);
1059 TARGi(iv, 0); /* arg not GMG, so can't be tainted */
1060 SETs(TARG);
1061 return NORMAL;
1062 }
1063
1064 return S_postincdec_common(aTHX_ sv, TARG);
1065}
1066
1067
a0d0e21e
LW
1068/* Ordinary operators. */
1069
1070PP(pp_pow)
1071{
20b7effb 1072 dSP; dATARGET; SV *svl, *svr;
58d76dfd 1073#ifdef PERL_PRESERVE_IVUV
52a96ae6
HS
1074 bool is_int = 0;
1075#endif
6f1401dc
DM
1076 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1077 svr = TOPs;
1078 svl = TOPm1s;
52a96ae6
HS
1079#ifdef PERL_PRESERVE_IVUV
1080 /* For integer to integer power, we do the calculation by hand wherever
1081 we're sure it is safe; otherwise we call pow() and try to convert to
1082 integer afterwards. */
01f91bf2 1083 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
900658e3
PF
1084 UV power;
1085 bool baseuok;
1086 UV baseuv;
1087
800401ee
JH
1088 if (SvUOK(svr)) {
1089 power = SvUVX(svr);
900658e3 1090 } else {
800401ee 1091 const IV iv = SvIVX(svr);
900658e3
PF
1092 if (iv >= 0) {
1093 power = iv;
1094 } else {
1095 goto float_it; /* Can't do negative powers this way. */
1096 }
1097 }
1098
800401ee 1099 baseuok = SvUOK(svl);
900658e3 1100 if (baseuok) {
800401ee 1101 baseuv = SvUVX(svl);
900658e3 1102 } else {
800401ee 1103 const IV iv = SvIVX(svl);
900658e3
PF
1104 if (iv >= 0) {
1105 baseuv = iv;
1106 baseuok = TRUE; /* effectively it's a UV now */
1107 } else {
1108 baseuv = -iv; /* abs, baseuok == false records sign */
1109 }
1110 }
52a96ae6
HS
1111 /* now we have integer ** positive integer. */
1112 is_int = 1;
1113
1114 /* foo & (foo - 1) is zero only for a power of 2. */
58d76dfd 1115 if (!(baseuv & (baseuv - 1))) {
52a96ae6 1116 /* We are raising power-of-2 to a positive integer.
58d76dfd
JH
1117 The logic here will work for any base (even non-integer
1118 bases) but it can be less accurate than
1119 pow (base,power) or exp (power * log (base)) when the
1120 intermediate values start to spill out of the mantissa.
1121 With powers of 2 we know this can't happen.
1122 And powers of 2 are the favourite thing for perl
1123 programmers to notice ** not doing what they mean. */
1124 NV result = 1.0;
1125 NV base = baseuok ? baseuv : -(NV)baseuv;
900658e3
PF
1126
1127 if (power & 1) {
1128 result *= base;
1129 }
1130 while (power >>= 1) {
1131 base *= base;
1132 if (power & 1) {
1133 result *= base;
1134 }
1135 }
58d76dfd
JH
1136 SP--;
1137 SETn( result );
6f1401dc 1138 SvIV_please_nomg(svr);
58d76dfd 1139 RETURN;
52a96ae6 1140 } else {
eb578fdb
KW
1141 unsigned int highbit = 8 * sizeof(UV);
1142 unsigned int diff = 8 * sizeof(UV);
900658e3
PF
1143 while (diff >>= 1) {
1144 highbit -= diff;
1145 if (baseuv >> highbit) {
1146 highbit += diff;
1147 }
52a96ae6
HS
1148 }
1149 /* we now have baseuv < 2 ** highbit */
1150 if (power * highbit <= 8 * sizeof(UV)) {
1151 /* result will definitely fit in UV, so use UV math
1152 on same algorithm as above */
eb578fdb
KW
1153 UV result = 1;
1154 UV base = baseuv;
f2338a2e 1155 const bool odd_power = cBOOL(power & 1);
900658e3
PF
1156 if (odd_power) {
1157 result *= base;
1158 }
1159 while (power >>= 1) {
1160 base *= base;
1161 if (power & 1) {
52a96ae6 1162 result *= base;
52a96ae6
HS
1163 }
1164 }
1165 SP--;
0615a994 1166 if (baseuok || !odd_power)
52a96ae6
HS
1167 /* answer is positive */
1168 SETu( result );
1169 else if (result <= (UV)IV_MAX)
1170 /* answer negative, fits in IV */
1171 SETi( -(IV)result );
1172 else if (result == (UV)IV_MIN)
1173 /* 2's complement assumption: special case IV_MIN */
1174 SETi( IV_MIN );
1175 else
1176 /* answer negative, doesn't fit */
1177 SETn( -(NV)result );
1178 RETURN;
1179 }
1180 }
58d76dfd 1181 }
52a96ae6 1182 float_it:
58d76dfd 1183#endif
a0d0e21e 1184 {
6f1401dc
DM
1185 NV right = SvNV_nomg(svr);
1186 NV left = SvNV_nomg(svl);
4efa5a16 1187 (void)POPs;
3aaeb624
JA
1188
1189#if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1190 /*
1191 We are building perl with long double support and are on an AIX OS
1192 afflicted with a powl() function that wrongly returns NaNQ for any
1193 negative base. This was reported to IBM as PMR #23047-379 on
1194 03/06/2006. The problem exists in at least the following versions
1195 of AIX and the libm fileset, and no doubt others as well:
1196
1197 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1198 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1199 AIX 5.2.0 bos.adt.libm 5.2.0.85
1200
1201 So, until IBM fixes powl(), we provide the following workaround to
1202 handle the problem ourselves. Our logic is as follows: for
1203 negative bases (left), we use fmod(right, 2) to check if the
1204 exponent is an odd or even integer:
1205
1206 - if odd, powl(left, right) == -powl(-left, right)
1207 - if even, powl(left, right) == powl(-left, right)
1208
1209 If the exponent is not an integer, the result is rightly NaNQ, so
1210 we just return that (as NV_NAN).
1211 */
1212
1213 if (left < 0.0) {
1214 NV mod2 = Perl_fmod( right, 2.0 );
1215 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1216 SETn( -Perl_pow( -left, right) );
1217 } else if (mod2 == 0.0) { /* even integer */
1218 SETn( Perl_pow( -left, right) );
1219 } else { /* fractional power */
1220 SETn( NV_NAN );
1221 }
1222 } else {
1223 SETn( Perl_pow( left, right) );
1224 }
1225#else
52a96ae6 1226 SETn( Perl_pow( left, right) );
3aaeb624
JA
1227#endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1228
52a96ae6
HS
1229#ifdef PERL_PRESERVE_IVUV
1230 if (is_int)
6f1401dc 1231 SvIV_please_nomg(svr);
52a96ae6
HS
1232#endif
1233 RETURN;
93a17b20 1234 }
a0d0e21e
LW
1235}
1236
1237PP(pp_multiply)
1238{
20b7effb 1239 dSP; dATARGET; SV *svl, *svr;
6f1401dc
DM
1240 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1241 svr = TOPs;
1242 svl = TOPm1s;
230ee21f 1243
28e5dec8 1244#ifdef PERL_PRESERVE_IVUV
230ee21f
DM
1245
1246 /* special-case some simple common cases */
1247 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1248 IV il, ir;
1249 U32 flags = (svl->sv_flags & svr->sv_flags);
1250 if (flags & SVf_IOK) {
1251 /* both args are simple IVs */
1252 UV topl, topr;
1253 il = SvIVX(svl);
1254 ir = SvIVX(svr);
1255 do_iv:
1256 topl = ((UV)il) >> (UVSIZE * 4 - 1);
1257 topr = ((UV)ir) >> (UVSIZE * 4 - 1);
1258
1259 /* if both are in a range that can't under/overflow, do a
1260 * simple integer multiply: if the top halves(*) of both numbers
1261 * are 00...00 or 11...11, then it's safe.
1262 * (*) for 32-bits, the "top half" is the top 17 bits,
1263 * for 64-bits, its 33 bits */
1264 if (!(
1265 ((topl+1) | (topr+1))
1266 & ( (((UV)1) << (UVSIZE * 4 + 1)) - 2) /* 11..110 */
1267 )) {
1268 SP--;
1269 TARGi(il * ir, 0); /* args not GMG, so can't be tainted */
1270 SETs(TARG);
1271 RETURN;
1272 }
1273 goto generic;
1274 }
1275 else if (flags & SVf_NOK) {
1276 /* both args are NVs */
1277 NV nl = SvNVX(svl);
1278 NV nr = SvNVX(svr);
1279 NV result;
1280
3336af0b
DD
1281 if (
1282#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1283 !Perl_isnan(nl) && nl == (NV)(il = (IV)nl)
1284 && !Perl_isnan(nr) && nr == (NV)(ir = (IV)nr)
1285#else
1286 nl == (NV)(il = (IV)nl) && nr == (NV)(ir = (IV)nr)
1287#endif
1288 )
230ee21f
DM
1289 /* nothing was lost by converting to IVs */
1290 goto do_iv;
1291 SP--;
1292 result = nl * nr;
1f02ab1d 1293# if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
230ee21f
DM
1294 if (Perl_isinf(result)) {
1295 Zero((U8*)&result + 8, 8, U8);
1296 }
1297# endif
1298 TARGn(result, 0); /* args not GMG, so can't be tainted */
1299 SETs(TARG);
1300 RETURN;
1301 }
1302 }
1303
1304 generic:
1305
01f91bf2 1306 if (SvIV_please_nomg(svr)) {
28e5dec8
JH
1307 /* Unless the left argument is integer in range we are going to have to
1308 use NV maths. Hence only attempt to coerce the right argument if
1309 we know the left is integer. */
1310 /* Left operand is defined, so is it IV? */
01f91bf2 1311 if (SvIV_please_nomg(svl)) {
800401ee
JH
1312 bool auvok = SvUOK(svl);
1313 bool buvok = SvUOK(svr);
28e5dec8
JH
1314 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1315 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1316 UV alow;
1317 UV ahigh;
1318 UV blow;
1319 UV bhigh;
1320
1321 if (auvok) {
800401ee 1322 alow = SvUVX(svl);
28e5dec8 1323 } else {
800401ee 1324 const IV aiv = SvIVX(svl);
28e5dec8
JH
1325 if (aiv >= 0) {
1326 alow = aiv;
1327 auvok = TRUE; /* effectively it's a UV now */
1328 } else {
53e2bfb7
DM
1329 /* abs, auvok == false records sign */
1330 alow = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv);
28e5dec8
JH
1331 }
1332 }
1333 if (buvok) {
800401ee 1334 blow = SvUVX(svr);
28e5dec8 1335 } else {
800401ee 1336 const IV biv = SvIVX(svr);
28e5dec8
JH
1337 if (biv >= 0) {
1338 blow = biv;
1339 buvok = TRUE; /* effectively it's a UV now */
1340 } else {
53e2bfb7
DM
1341 /* abs, buvok == false records sign */
1342 blow = (biv == IV_MIN) ? (UV)biv : (UV)(-biv);
28e5dec8
JH
1343 }
1344 }
1345
1346 /* If this does sign extension on unsigned it's time for plan B */
1347 ahigh = alow >> (4 * sizeof (UV));
1348 alow &= botmask;
1349 bhigh = blow >> (4 * sizeof (UV));
1350 blow &= botmask;
1351 if (ahigh && bhigh) {
6f207bd3 1352 NOOP;
28e5dec8
JH
1353 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1354 which is overflow. Drop to NVs below. */
1355 } else if (!ahigh && !bhigh) {
1356 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1357 so the unsigned multiply cannot overflow. */
c445ea15 1358 const UV product = alow * blow;
28e5dec8
JH
1359 if (auvok == buvok) {
1360 /* -ve * -ve or +ve * +ve gives a +ve result. */
1361 SP--;
1362 SETu( product );
1363 RETURN;
1364 } else if (product <= (UV)IV_MIN) {
1365 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1366 /* -ve result, which could overflow an IV */
1367 SP--;
02b08bbc
DM
1368 /* can't negate IV_MIN, but there are aren't two
1369 * integers such that !ahigh && !bhigh, where the
1370 * product equals 0x800....000 */
1371 assert(product != (UV)IV_MIN);
25716404 1372 SETi( -(IV)product );
28e5dec8
JH
1373 RETURN;
1374 } /* else drop to NVs below. */
1375 } else {
1376 /* One operand is large, 1 small */
1377 UV product_middle;
1378 if (bhigh) {
1379 /* swap the operands */
1380 ahigh = bhigh;
1381 bhigh = blow; /* bhigh now the temp var for the swap */
1382 blow = alow;
1383 alow = bhigh;
1384 }
1385 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1386 multiplies can't overflow. shift can, add can, -ve can. */
1387 product_middle = ahigh * blow;
1388 if (!(product_middle & topmask)) {
1389 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1390 UV product_low;
1391 product_middle <<= (4 * sizeof (UV));
1392 product_low = alow * blow;
1393
1394 /* as for pp_add, UV + something mustn't get smaller.
1395 IIRC ANSI mandates this wrapping *behaviour* for
1396 unsigned whatever the actual representation*/
1397 product_low += product_middle;
1398 if (product_low >= product_middle) {
1399 /* didn't overflow */
1400 if (auvok == buvok) {
1401 /* -ve * -ve or +ve * +ve gives a +ve result. */
1402 SP--;
1403 SETu( product_low );
1404 RETURN;
1405 } else if (product_low <= (UV)IV_MIN) {
1406 /* 2s complement assumption again */
1407 /* -ve result, which could overflow an IV */
1408 SP--;
53e2bfb7
DM
1409 SETi(product_low == (UV)IV_MIN
1410 ? IV_MIN : -(IV)product_low);
28e5dec8
JH
1411 RETURN;
1412 } /* else drop to NVs below. */
1413 }
1414 } /* product_middle too large */
1415 } /* ahigh && bhigh */
800401ee
JH
1416 } /* SvIOK(svl) */
1417 } /* SvIOK(svr) */
28e5dec8 1418#endif
a0d0e21e 1419 {
6f1401dc
DM
1420 NV right = SvNV_nomg(svr);
1421 NV left = SvNV_nomg(svl);
230ee21f
DM
1422 NV result = left * right;
1423
4efa5a16 1424 (void)POPs;
1f02ab1d 1425#if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
230ee21f
DM
1426 if (Perl_isinf(result)) {
1427 Zero((U8*)&result + 8, 8, U8);
3ec400f5 1428 }
3ec400f5 1429#endif
230ee21f 1430 SETn(result);
a0d0e21e 1431 RETURN;
79072805 1432 }
a0d0e21e
LW
1433}
1434
1435PP(pp_divide)
1436{
20b7effb 1437 dSP; dATARGET; SV *svl, *svr;
6f1401dc
DM
1438 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1439 svr = TOPs;
1440 svl = TOPm1s;
5479d192 1441 /* Only try to do UV divide first
68795e93 1442 if ((SLOPPYDIVIDE is true) or
5479d192
NC
1443 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1444 to preserve))
1445 The assumption is that it is better to use floating point divide
1446 whenever possible, only doing integer divide first if we can't be sure.
1447 If NV_PRESERVES_UV is true then we know at compile time that no UV
1448 can be too large to preserve, so don't need to compile the code to
1449 test the size of UVs. */
1450
a0d0e21e 1451#ifdef SLOPPYDIVIDE
5479d192
NC
1452# define PERL_TRY_UV_DIVIDE
1453 /* ensure that 20./5. == 4. */
a0d0e21e 1454#else
5479d192
NC
1455# ifdef PERL_PRESERVE_IVUV
1456# ifndef NV_PRESERVES_UV
1457# define PERL_TRY_UV_DIVIDE
1458# endif
1459# endif
a0d0e21e 1460#endif
5479d192
NC
1461
1462#ifdef PERL_TRY_UV_DIVIDE
01f91bf2 1463 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
800401ee
JH
1464 bool left_non_neg = SvUOK(svl);
1465 bool right_non_neg = SvUOK(svr);
5479d192
NC
1466 UV left;
1467 UV right;
1468
1469 if (right_non_neg) {
800401ee 1470 right = SvUVX(svr);
5479d192
NC
1471 }
1472 else {
800401ee 1473 const IV biv = SvIVX(svr);
5479d192
NC
1474 if (biv >= 0) {
1475 right = biv;
1476 right_non_neg = TRUE; /* effectively it's a UV now */
1477 }
1478 else {
02b08bbc 1479 right = (biv == IV_MIN) ? (UV)biv : (UV)(-biv);
5479d192
NC
1480 }
1481 }
1482 /* historically undef()/0 gives a "Use of uninitialized value"
1483 warning before dieing, hence this test goes here.
1484 If it were immediately before the second SvIV_please, then
1485 DIE() would be invoked before left was even inspected, so
486ec47a 1486 no inspection would give no warning. */
5479d192
NC
1487 if (right == 0)
1488 DIE(aTHX_ "Illegal division by zero");
1489
1490 if (left_non_neg) {
800401ee 1491 left = SvUVX(svl);
5479d192
NC
1492 }
1493 else {
800401ee 1494 const IV aiv = SvIVX(svl);
5479d192
NC
1495 if (aiv >= 0) {
1496 left = aiv;
1497 left_non_neg = TRUE; /* effectively it's a UV now */
1498 }
1499 else {
02b08bbc 1500 left = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv);
5479d192
NC
1501 }
1502 }
1503
1504 if (left >= right
1505#ifdef SLOPPYDIVIDE
1506 /* For sloppy divide we always attempt integer division. */
1507#else
1508 /* Otherwise we only attempt it if either or both operands
1509 would not be preserved by an NV. If both fit in NVs
0c2ee62a
NC
1510 we fall through to the NV divide code below. However,
1511 as left >= right to ensure integer result here, we know that
1512 we can skip the test on the right operand - right big
1513 enough not to be preserved can't get here unless left is
1514 also too big. */
1515
1516 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
5479d192
NC
1517#endif
1518 ) {
1519 /* Integer division can't overflow, but it can be imprecise. */
1b6737cc 1520 const UV result = left / right;
5479d192
NC
1521 if (result * right == left) {
1522 SP--; /* result is valid */
1523 if (left_non_neg == right_non_neg) {
1524 /* signs identical, result is positive. */
1525 SETu( result );
1526 RETURN;
1527 }
1528 /* 2s complement assumption */
1529 if (result <= (UV)IV_MIN)
02b08bbc 1530 SETi(result == (UV)IV_MIN ? IV_MIN : -(IV)result);
5479d192
NC
1531 else {
1532 /* It's exact but too negative for IV. */
1533 SETn( -(NV)result );
1534 }
1535 RETURN;
1536 } /* tried integer divide but it was not an integer result */
32fdb065 1537 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
01f91bf2 1538 } /* one operand wasn't SvIOK */
5479d192
NC
1539#endif /* PERL_TRY_UV_DIVIDE */
1540 {
6f1401dc
DM
1541 NV right = SvNV_nomg(svr);
1542 NV left = SvNV_nomg(svl);
4efa5a16 1543 (void)POPs;(void)POPs;
ebc6a117
PD
1544#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1545 if (! Perl_isnan(right) && right == 0.0)
1546#else
659c4b96 1547 if (right == 0.0)
ebc6a117 1548#endif
5479d192
NC
1549 DIE(aTHX_ "Illegal division by zero");
1550 PUSHn( left / right );
1551 RETURN;
79072805 1552 }
a0d0e21e
LW
1553}
1554
1555PP(pp_modulo)
1556{
20b7effb 1557 dSP; dATARGET;
6f1401dc 1558 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
a0d0e21e 1559 {
9c5ffd7c
JH
1560 UV left = 0;
1561 UV right = 0;
dc656993
JH
1562 bool left_neg = FALSE;
1563 bool right_neg = FALSE;
e2c88acc
NC
1564 bool use_double = FALSE;
1565 bool dright_valid = FALSE;
9c5ffd7c
JH
1566 NV dright = 0.0;
1567 NV dleft = 0.0;
6f1401dc
DM
1568 SV * const svr = TOPs;
1569 SV * const svl = TOPm1s;
01f91bf2 1570 if (SvIV_please_nomg(svr)) {
800401ee 1571 right_neg = !SvUOK(svr);
e2c88acc 1572 if (!right_neg) {
800401ee 1573 right = SvUVX(svr);
e2c88acc 1574 } else {
800401ee 1575 const IV biv = SvIVX(svr);
e2c88acc
NC
1576 if (biv >= 0) {
1577 right = biv;
1578 right_neg = FALSE; /* effectively it's a UV now */
1579 } else {
02b08bbc 1580 right = (biv == IV_MIN) ? (UV)biv : (UV)(-biv);
e2c88acc
NC
1581 }
1582 }
1583 }
1584 else {
6f1401dc 1585 dright = SvNV_nomg(svr);
787eafbd
IZ
1586 right_neg = dright < 0;
1587 if (right_neg)
1588 dright = -dright;
e2c88acc
NC
1589 if (dright < UV_MAX_P1) {
1590 right = U_V(dright);
1591 dright_valid = TRUE; /* In case we need to use double below. */
1592 } else {
1593 use_double = TRUE;
1594 }
787eafbd 1595 }
a0d0e21e 1596
e2c88acc
NC
1597 /* At this point use_double is only true if right is out of range for
1598 a UV. In range NV has been rounded down to nearest UV and
1599 use_double false. */
01f91bf2 1600 if (!use_double && SvIV_please_nomg(svl)) {
800401ee 1601 left_neg = !SvUOK(svl);
e2c88acc 1602 if (!left_neg) {
800401ee 1603 left = SvUVX(svl);
e2c88acc 1604 } else {
800401ee 1605 const IV aiv = SvIVX(svl);
e2c88acc
NC
1606 if (aiv >= 0) {
1607 left = aiv;
1608 left_neg = FALSE; /* effectively it's a UV now */
1609 } else {
02b08bbc 1610 left = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv);
e2c88acc
NC
1611 }
1612 }
e2c88acc 1613 }
787eafbd 1614 else {
6f1401dc 1615 dleft = SvNV_nomg(svl);
787eafbd
IZ
1616 left_neg = dleft < 0;
1617 if (left_neg)
1618 dleft = -dleft;
68dc0745 1619
e2c88acc
NC
1620 /* This should be exactly the 5.6 behaviour - if left and right are
1621 both in range for UV then use U_V() rather than floor. */
1622 if (!use_double) {
1623 if (dleft < UV_MAX_P1) {
1624 /* right was in range, so is dleft, so use UVs not double.
1625 */
1626 left = U_V(dleft);
1627 }
1628 /* left is out of range for UV, right was in range, so promote
1629 right (back) to double. */
1630 else {
1631 /* The +0.5 is used in 5.6 even though it is not strictly
1632 consistent with the implicit +0 floor in the U_V()
1633 inside the #if 1. */
1634 dleft = Perl_floor(dleft + 0.5);
1635 use_double = TRUE;
1636 if (dright_valid)
1637 dright = Perl_floor(dright + 0.5);
1638 else
1639 dright = right;
1640 }
1641 }
1642 }
6f1401dc 1643 sp -= 2;
787eafbd 1644 if (use_double) {
65202027 1645 NV dans;
787eafbd 1646
659c4b96 1647 if (!dright)
cea2e8a9 1648 DIE(aTHX_ "Illegal modulus zero");
787eafbd 1649
65202027 1650 dans = Perl_fmod(dleft, dright);
659c4b96 1651 if ((left_neg != right_neg) && dans)
787eafbd
IZ
1652 dans = dright - dans;
1653 if (right_neg)
1654 dans = -dans;
1655 sv_setnv(TARG, dans);
1656 }
1657 else {
1658 UV ans;
1659
787eafbd 1660 if (!right)
cea2e8a9 1661 DIE(aTHX_ "Illegal modulus zero");
787eafbd
IZ
1662
1663 ans = left % right;
1664 if ((left_neg != right_neg) && ans)
1665 ans = right - ans;
1666 if (right_neg) {
1667 /* XXX may warn: unary minus operator applied to unsigned type */
1668 /* could change -foo to be (~foo)+1 instead */
1669 if (ans <= ~((UV)IV_MAX)+1)
1670 sv_setiv(TARG, ~ans+1);
1671 else
65202027 1672 sv_setnv(TARG, -(NV)ans);
787eafbd
IZ
1673 }
1674 else
1675 sv_setuv(TARG, ans);
1676 }
1677 PUSHTARG;
1678 RETURN;
79072805 1679 }
a0d0e21e 1680}
79072805 1681
a0d0e21e
LW
1682PP(pp_repeat)
1683{
20b7effb 1684 dSP; dATARGET;
eb578fdb 1685 IV count;
6f1401dc 1686 SV *sv;
02a7a248 1687 bool infnan = FALSE;
6f1401dc 1688
82334630 1689 if (GIMME_V == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
6f1401dc
DM
1690 /* TODO: think of some way of doing list-repeat overloading ??? */
1691 sv = POPs;
1692 SvGETMAGIC(sv);
1693 }
1694 else {
3a100dab
FC
1695 if (UNLIKELY(PL_op->op_private & OPpREPEAT_DOLIST)) {
1696 /* The parser saw this as a list repeat, and there
1697 are probably several items on the stack. But we're
1698 in scalar/void context, and there's no pp_list to save us
1699 now. So drop the rest of the items -- robin@kitsite.com
1700 */
1701 dMARK;
1702 if (MARK + 1 < SP) {
1703 MARK[1] = TOPm1s;
1704 MARK[2] = TOPs;
1705 }
1706 else {
1707 dTOPss;
1708 ASSUME(MARK + 1 == SP);
1709 XPUSHs(sv);
1710 MARK[1] = &PL_sv_undef;
1711 }
1712 SP = MARK + 2;
1713 }
6f1401dc
DM
1714 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1715 sv = POPs;
1716 }
1717
2b573ace
JH
1718 if (SvIOKp(sv)) {
1719 if (SvUOK(sv)) {
6f1401dc 1720 const UV uv = SvUV_nomg(sv);
2b573ace
JH
1721 if (uv > IV_MAX)
1722 count = IV_MAX; /* The best we can do? */
1723 else
1724 count = uv;
1725 } else {
b3211734 1726 count = SvIV_nomg(sv);
2b573ace
JH
1727 }
1728 }
1729 else if (SvNOKp(sv)) {
02a7a248
JH
1730 const NV nv = SvNV_nomg(sv);
1731 infnan = Perl_isinfnan(nv);
1732 if (UNLIKELY(infnan)) {
1733 count = 0;
1734 } else {
1735 if (nv < 0.0)
1736 count = -1; /* An arbitrary negative integer */
1737 else
1738 count = (IV)nv;
1739 }
2b573ace
JH
1740 }
1741 else
02a7a248 1742 count = SvIV_nomg(sv);
6f1401dc 1743
02a7a248
JH
1744 if (infnan) {
1745 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
1746 "Non-finite repeat count does nothing");
1747 } else if (count < 0) {
b3211734
KW
1748 count = 0;
1749 Perl_ck_warner(aTHX_ packWARN(WARN_NUMERIC),
02a7a248 1750 "Negative repeat count does nothing");
b3211734
KW
1751 }
1752
82334630 1753 if (GIMME_V == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
a0d0e21e 1754 dMARK;
052a7c76 1755 const SSize_t items = SP - MARK;
da9e430b 1756 const U8 mod = PL_op->op_flags & OPf_MOD;
79072805 1757
a0d0e21e 1758 if (count > 1) {
052a7c76 1759 SSize_t max;
b3b27d01 1760
052a7c76
DM
1761 if ( items > SSize_t_MAX / count /* max would overflow */
1762 /* repeatcpy would overflow */
1763 || items > I32_MAX / (I32)sizeof(SV *)
b3b27d01
DM
1764 )
1765 Perl_croak(aTHX_ "%s","Out of memory during list extend");
1766 max = items * count;
1767 MEXTEND(MARK, max);
1768
a0d0e21e 1769 while (SP > MARK) {
60779a30
DM
1770 if (*SP) {
1771 if (mod && SvPADTMP(*SP)) {
da9e430b 1772 *SP = sv_mortalcopy(*SP);
60779a30 1773 }
976c8a39 1774 SvTEMP_off((*SP));
da9e430b 1775 }
a0d0e21e 1776 SP--;
79072805 1777 }
a0d0e21e
LW
1778 MARK++;
1779 repeatcpy((char*)(MARK + items), (char*)MARK,
ad64d0ec 1780 items * sizeof(const SV *), count - 1);
a0d0e21e 1781 SP += max;
79072805 1782 }
a0d0e21e 1783 else if (count <= 0)
052a7c76 1784 SP = MARK;
79072805 1785 }
a0d0e21e 1786 else { /* Note: mark already snarfed by pp_list */
0bd48802 1787 SV * const tmpstr = POPs;
a0d0e21e 1788 STRLEN len;
9b877dbb 1789 bool isutf;
a0d0e21e 1790
6f1401dc
DM
1791 if (TARG != tmpstr)
1792 sv_setsv_nomg(TARG, tmpstr);
1793 SvPV_force_nomg(TARG, len);
9b877dbb 1794 isutf = DO_UTF8(TARG);
8ebc5c01 1795 if (count != 1) {
1796 if (count < 1)
1797 SvCUR_set(TARG, 0);
1798 else {
b3b27d01
DM
1799 STRLEN max;
1800
1801 if ( len > (MEM_SIZE_MAX-1) / (UV)count /* max would overflow */
1802 || len > (U32)I32_MAX /* repeatcpy would overflow */
1803 )
1804 Perl_croak(aTHX_ "%s",
1805 "Out of memory during string extend");
1806 max = (UV)count * len + 1;
1807 SvGROW(TARG, max);
1808
a0d0e21e 1809 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
b162af07 1810 SvCUR_set(TARG, SvCUR(TARG) * count);
7a4c00b4 1811 }
a0d0e21e 1812 *SvEND(TARG) = '\0';
a0d0e21e 1813 }
dfcb284a
GS
1814 if (isutf)
1815 (void)SvPOK_only_UTF8(TARG);
1816 else
1817 (void)SvPOK_only(TARG);
b80b6069 1818
a0d0e21e 1819 PUSHTARG;
79072805 1820 }
a0d0e21e
LW
1821 RETURN;
1822}
79072805 1823
a0d0e21e
LW
1824PP(pp_subtract)
1825{
20b7effb 1826 dSP; dATARGET; bool useleft; SV *svl, *svr;
6f1401dc
DM
1827 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1828 svr = TOPs;
1829 svl = TOPm1s;
230ee21f 1830
28e5dec8 1831#ifdef PERL_PRESERVE_IVUV
230ee21f
DM
1832
1833 /* special-case some simple common cases */
1834 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1835 IV il, ir;
1836 U32 flags = (svl->sv_flags & svr->sv_flags);
1837 if (flags & SVf_IOK) {
1838 /* both args are simple IVs */
1839 UV topl, topr;
1840 il = SvIVX(svl);
1841 ir = SvIVX(svr);
1842 do_iv:
1843 topl = ((UV)il) >> (UVSIZE * 8 - 2);
1844 topr = ((UV)ir) >> (UVSIZE * 8 - 2);
1845
1846 /* if both are in a range that can't under/overflow, do a
1847 * simple integer subtract: if the top of both numbers
1848 * are 00 or 11, then it's safe */
1849 if (!( ((topl+1) | (topr+1)) & 2)) {
1850 SP--;
1851 TARGi(il - ir, 0); /* args not GMG, so can't be tainted */
1852 SETs(TARG);
1853 RETURN;
1854 }
1855 goto generic;
1856 }
1857 else if (flags & SVf_NOK) {
1858 /* both args are NVs */
1859 NV nl = SvNVX(svl);
1860 NV nr = SvNVX(svr);
1861
3336af0b
DD
1862 if (
1863#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1864 !Perl_isnan(nl) && nl == (NV)(il = (IV)nl)
1865 && !Perl_isnan(nr) && nr == (NV)(ir = (IV)nr)
1866#else
1867 nl == (NV)(il = (IV)nl) && nr == (NV)(ir = (IV)nr)
1868#endif
1869 )
230ee21f
DM
1870 /* nothing was lost by converting to IVs */
1871 goto do_iv;
1872 SP--;
1873 TARGn(nl - nr, 0); /* args not GMG, so can't be tainted */
1874 SETs(TARG);
1875 RETURN;
1876 }
1877 }
1878
1879 generic:
1880
1881 useleft = USE_LEFT(svl);
7dca457a
NC
1882 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1883 "bad things" happen if you rely on signed integers wrapping. */
01f91bf2 1884 if (SvIV_please_nomg(svr)) {
28e5dec8
JH
1885 /* Unless the left argument is integer in range we are going to have to
1886 use NV maths. Hence only attempt to coerce the right argument if
1887 we know the left is integer. */
eb578fdb 1888 UV auv = 0;
9c5ffd7c 1889 bool auvok = FALSE;
7dca457a
NC
1890 bool a_valid = 0;
1891
28e5dec8 1892 if (!useleft) {
7dca457a
NC
1893 auv = 0;
1894 a_valid = auvok = 1;
1895 /* left operand is undef, treat as zero. */
28e5dec8
JH
1896 } else {
1897 /* Left operand is defined, so is it IV? */
01f91bf2 1898 if (SvIV_please_nomg(svl)) {
800401ee
JH
1899 if ((auvok = SvUOK(svl)))
1900 auv = SvUVX(svl);
7dca457a 1901 else {
eb578fdb 1902 const IV aiv = SvIVX(svl);
7dca457a
NC
1903 if (aiv >= 0) {
1904 auv = aiv;
1905 auvok = 1; /* Now acting as a sign flag. */
1906 } else { /* 2s complement assumption for IV_MIN */
53e2bfb7 1907 auv = (aiv == IV_MIN) ? (UV)aiv : (UV)-aiv;
28e5dec8 1908 }
7dca457a
NC
1909 }
1910 a_valid = 1;
1911 }
1912 }
1913 if (a_valid) {
1914 bool result_good = 0;
1915 UV result;
eb578fdb 1916 UV buv;
800401ee 1917 bool buvok = SvUOK(svr);
9041c2e3 1918
7dca457a 1919 if (buvok)
800401ee 1920 buv = SvUVX(svr);
7dca457a 1921 else {
eb578fdb 1922 const IV biv = SvIVX(svr);
7dca457a
NC
1923 if (biv >= 0) {
1924 buv = biv;
1925 buvok = 1;
1926 } else
53e2bfb7 1927 buv = (biv == IV_MIN) ? (UV)biv : (UV)-biv;
7dca457a
NC
1928 }
1929 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
602f51c4 1930 else "IV" now, independent of how it came in.
7dca457a
NC
1931 if a, b represents positive, A, B negative, a maps to -A etc
1932 a - b => (a - b)
1933 A - b => -(a + b)
1934 a - B => (a + b)
1935 A - B => -(a - b)
1936 all UV maths. negate result if A negative.
1937 subtract if signs same, add if signs differ. */
1938
1939 if (auvok ^ buvok) {
1940 /* Signs differ. */
1941 result = auv + buv;
1942 if (result >= auv)
1943 result_good = 1;
1944 } else {
1945 /* Signs same */
1946 if (auv >= buv) {
1947 result = auv - buv;
1948 /* Must get smaller */
1949 if (result <= auv)
1950 result_good = 1;
1951 } else {
1952 result = buv - auv;
1953 if (result <= buv) {
1954 /* result really should be -(auv-buv). as its negation
1955 of true value, need to swap our result flag */
1956 auvok = !auvok;
1957 result_good = 1;
28e5dec8 1958 }
28e5dec8
JH
1959 }
1960 }
7dca457a
NC
1961 if (result_good) {
1962 SP--;
1963 if (auvok)
1964 SETu( result );
1965 else {
1966 /* Negate result */
1967 if (result <= (UV)IV_MIN)
53e2bfb7
DM
1968 SETi(result == (UV)IV_MIN
1969 ? IV_MIN : -(IV)result);
7dca457a
NC
1970 else {
1971 /* result valid, but out of range for IV. */
1972 SETn( -(NV)result );
1973 }
1974 }
1975 RETURN;
1976 } /* Overflow, drop through to NVs. */
28e5dec8
JH
1977 }
1978 }
230ee21f
DM
1979#else
1980 useleft = USE_LEFT(svl);
28e5dec8 1981#endif
a0d0e21e 1982 {
6f1401dc 1983 NV value = SvNV_nomg(svr);
4efa5a16
RD
1984 (void)POPs;
1985
28e5dec8
JH
1986 if (!useleft) {
1987 /* left operand is undef, treat as zero - value */
1988 SETn(-value);
1989 RETURN;
1990 }
6f1401dc 1991 SETn( SvNV_nomg(svl) - value );
28e5dec8 1992 RETURN;
79072805 1993 }
a0d0e21e 1994}
79072805 1995
b3498293
JH
1996#define IV_BITS (IVSIZE * 8)
1997
1998static UV S_uv_shift(UV uv, int shift, bool left)
1999{
2000 if (shift < 0) {
2001 shift = -shift;
2002 left = !left;
2003 }
2004 if (shift >= IV_BITS) {
2005 return 0;
2006 }
2007 return left ? uv << shift : uv >> shift;
2008}
2009
2010static IV S_iv_shift(IV iv, int shift, bool left)
2011{
2012 if (shift < 0) {
2013 shift = -shift;
2014 left = !left;
2015 }
2016 if (shift >= IV_BITS) {
b69687e7 2017 return iv < 0 && !left ? -1 : 0;
b3498293
JH
2018 }
2019 return left ? iv << shift : iv >> shift;
2020}
2021
2022#define UV_LEFT_SHIFT(uv, shift) S_uv_shift(uv, shift, TRUE)
2023#define UV_RIGHT_SHIFT(uv, shift) S_uv_shift(uv, shift, FALSE)
2024#define IV_LEFT_SHIFT(iv, shift) S_iv_shift(iv, shift, TRUE)
2025#define IV_RIGHT_SHIFT(iv, shift) S_iv_shift(iv, shift, FALSE)
2026
a0d0e21e
LW
2027PP(pp_left_shift)
2028{
20b7effb 2029 dSP; dATARGET; SV *svl, *svr;
a42d0242 2030 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
6f1401dc
DM
2031 svr = POPs;
2032 svl = TOPs;
a0d0e21e 2033 {
6f1401dc 2034 const IV shift = SvIV_nomg(svr);
d0ba1bd2 2035 if (PL_op->op_private & HINT_INTEGER) {
b3498293 2036 SETi(IV_LEFT_SHIFT(SvIV_nomg(svl), shift));
d0ba1bd2
JH
2037 }
2038 else {
b3498293 2039 SETu(UV_LEFT_SHIFT(SvUV_nomg(svl), shift));
d0ba1bd2 2040 }
55497cff 2041 RETURN;
79072805 2042 }
a0d0e21e 2043}
79072805 2044
a0d0e21e
LW
2045PP(pp_right_shift)
2046{
20b7effb 2047 dSP; dATARGET; SV *svl, *svr;
a42d0242 2048 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
6f1401dc
DM
2049 svr = POPs;
2050 svl = TOPs;
a0d0e21e 2051 {
6f1401dc 2052 const IV shift = SvIV_nomg(svr);
d0ba1bd2 2053 if (PL_op->op_private & HINT_INTEGER) {
b3498293 2054 SETi(IV_RIGHT_SHIFT(SvIV_nomg(svl), shift));
d0ba1bd2
JH
2055 }
2056 else {
b3498293 2057 SETu(UV_RIGHT_SHIFT(SvUV_nomg(svl), shift));
d0ba1bd2 2058 }
a0d0e21e 2059 RETURN;
93a17b20 2060 }
79072805
LW
2061}
2062
a0d0e21e 2063PP(pp_lt)
79072805 2064{
20b7effb 2065 dSP;
33efebe6
DM
2066 SV *left, *right;
2067
a42d0242 2068 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
33efebe6
DM
2069 right = POPs;
2070 left = TOPs;
2071 SETs(boolSV(
2072 (SvIOK_notUV(left) && SvIOK_notUV(right))
2073 ? (SvIVX(left) < SvIVX(right))
2074 : (do_ncmp(left, right) == -1)
2075 ));
2076 RETURN;
a0d0e21e 2077}
79072805 2078
a0d0e21e
LW
2079PP(pp_gt)
2080{
20b7effb 2081 dSP;
33efebe6 2082 SV *left, *right;
1b6737cc 2083
33efebe6
DM
2084 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
2085 right = POPs;
2086 left = TOPs;
2087 SETs(boolSV(
2088 (SvIOK_notUV(left) && SvIOK_notUV(right))
2089 ? (SvIVX(left) > SvIVX(right))
2090 : (do_ncmp(left, right) == 1)
2091 ));
2092 RETURN;
a0d0e21e
LW
2093}
2094
2095PP(pp_le)
2096{
20b7effb 2097 dSP;
33efebe6 2098 SV *left, *right;
1b6737cc 2099
33efebe6
DM
2100 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
2101 right = POPs;
2102 left = TOPs;
2103 SETs(boolSV(
2104 (SvIOK_notUV(left) && SvIOK_notUV(right))
2105 ? (SvIVX(left) <= SvIVX(right))
2106 : (do_ncmp(left, right) <= 0)
2107 ));
2108 RETURN;
a0d0e21e
LW
2109}
2110
2111PP(pp_ge)
2112{
20b7effb 2113 dSP;
33efebe6
DM
2114 SV *left, *right;
2115
2116 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
2117 right = POPs;
2118 left = TOPs;
2119 SETs(boolSV(
2120 (SvIOK_notUV(left) && SvIOK_notUV(right))
2121 ? (SvIVX(left) >= SvIVX(right))
2122 : ( (do_ncmp(left, right) & 2) == 0)
2123 ));
2124 RETURN;
2125}
1b6737cc 2126
33efebe6
DM
2127PP(pp_ne)
2128{
20b7effb 2129 dSP;
33efebe6
DM
2130 SV *left, *right;
2131
2132 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2133 right = POPs;
2134 left = TOPs;
2135 SETs(boolSV(
2136 (SvIOK_notUV(left) && SvIOK_notUV(right))
2137 ? (SvIVX(left) != SvIVX(right))
2138 : (do_ncmp(left, right) != 0)
2139 ));
2140 RETURN;
2141}
1b6737cc 2142
33efebe6
DM
2143/* compare left and right SVs. Returns:
2144 * -1: <
2145 * 0: ==
2146 * 1: >
2147 * 2: left or right was a NaN
2148 */
2149I32
2150Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2151{
33efebe6
DM
2152 PERL_ARGS_ASSERT_DO_NCMP;
2153#ifdef PERL_PRESERVE_IVUV
33efebe6 2154 /* Fortunately it seems NaN isn't IOK */
01f91bf2 2155 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
33efebe6
DM
2156 if (!SvUOK(left)) {
2157 const IV leftiv = SvIVX(left);
2158 if (!SvUOK(right)) {
2159 /* ## IV <=> IV ## */
2160 const IV rightiv = SvIVX(right);
2161 return (leftiv > rightiv) - (leftiv < rightiv);
28e5dec8 2162 }
33efebe6
DM
2163 /* ## IV <=> UV ## */
2164 if (leftiv < 0)
2165 /* As (b) is a UV, it's >=0, so it must be < */
2166 return -1;
2167 {
2168 const UV rightuv = SvUVX(right);
2169 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
28e5dec8 2170 }
28e5dec8 2171 }
79072805 2172
33efebe6
DM
2173 if (SvUOK(right)) {
2174 /* ## UV <=> UV ## */
2175 const UV leftuv = SvUVX(left);
2176 const UV rightuv = SvUVX(right);
2177 return (leftuv > rightuv) - (leftuv < rightuv);
28e5dec8 2178 }
33efebe6
DM
2179 /* ## UV <=> IV ## */
2180 {
2181 const IV rightiv = SvIVX(right);
2182 if (rightiv < 0)
2183 /* As (a) is a UV, it's >=0, so it cannot be < */
2184 return 1;
2185 {
2186 const UV leftuv = SvUVX(left);
2187 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
28e5dec8 2188 }
28e5dec8 2189 }
e5964223 2190 NOT_REACHED; /* NOTREACHED */
28e5dec8
JH
2191 }
2192#endif
a0d0e21e 2193 {
33efebe6
DM
2194 NV const rnv = SvNV_nomg(right);
2195 NV const lnv = SvNV_nomg(left);
2196
cab190d4 2197#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
33efebe6
DM
2198 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2199 return 2;
2200 }
2201 return (lnv > rnv) - (lnv < rnv);
cab190d4 2202#else
33efebe6
DM
2203 if (lnv < rnv)
2204 return -1;
2205 if (lnv > rnv)
2206 return 1;
659c4b96 2207 if (lnv == rnv)
33efebe6
DM
2208 return 0;
2209 return 2;
cab190d4 2210#endif
a0d0e21e 2211 }
79072805
LW
2212}
2213
33efebe6 2214
a0d0e21e 2215PP(pp_ncmp)
79072805 2216{
20b7effb 2217 dSP;
33efebe6
DM
2218 SV *left, *right;
2219 I32 value;
a42d0242 2220 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
33efebe6
DM
2221 right = POPs;
2222 left = TOPs;
2223 value = do_ncmp(left, right);
2224 if (value == 2) {
3280af22 2225 SETs(&PL_sv_undef);
79072805 2226 }
33efebe6
DM
2227 else {
2228 dTARGET;
2229 SETi(value);
2230 }
2231 RETURN;
a0d0e21e 2232}
79072805 2233
b1c05ba5
DM
2234
2235/* also used for: pp_sge() pp_sgt() pp_slt() */
2236
afd9910b 2237PP(pp_sle)
a0d0e21e 2238{
20b7effb 2239 dSP;
79072805 2240
afd9910b
NC
2241 int amg_type = sle_amg;
2242 int multiplier = 1;
2243 int rhs = 1;
79072805 2244
afd9910b
NC
2245 switch (PL_op->op_type) {
2246 case OP_SLT:
2247 amg_type = slt_amg;
2248 /* cmp < 0 */
2249 rhs = 0;
2250 break;
2251 case OP_SGT:
2252 amg_type = sgt_amg;
2253 /* cmp > 0 */
2254 multiplier = -1;
2255 rhs = 0;
2256 break;
2257 case OP_SGE:
2258 amg_type = sge_amg;
2259 /* cmp >= 0 */
2260 multiplier = -1;
2261 break;
79072805 2262 }
79072805 2263
6f1401dc 2264 tryAMAGICbin_MG(amg_type, AMGf_set);
a0d0e21e
LW
2265 {
2266 dPOPTOPssrl;
130c5df3 2267 const int cmp =
5778acb6 2268#ifdef USE_LOCALE_COLLATE
130c5df3
KW
2269 (IN_LC_RUNTIME(LC_COLLATE))
2270 ? sv_cmp_locale_flags(left, right, 0)
2271 :
2272#endif
2273 sv_cmp_flags(left, right, 0);
afd9910b 2274 SETs(boolSV(cmp * multiplier < rhs));
a0d0e21e
LW
2275 RETURN;
2276 }
2277}
79072805 2278
36477c24 2279PP(pp_seq)
2280{
20b7effb 2281 dSP;
6f1401dc 2282 tryAMAGICbin_MG(seq_amg, AMGf_set);
36477c24 2283 {
2284 dPOPTOPssrl;
078504b2 2285 SETs(boolSV(sv_eq_flags(left, right, 0)));
a0d0e21e
LW
2286 RETURN;
2287 }
2288}
79072805 2289
a0d0e21e 2290PP(pp_sne)
79072805 2291{
20b7effb 2292 dSP;
6f1401dc 2293 tryAMAGICbin_MG(sne_amg, AMGf_set);
a0d0e21e
LW
2294 {
2295 dPOPTOPssrl;
078504b2 2296 SETs(boolSV(!sv_eq_flags(left, right, 0)));
a0d0e21e 2297 RETURN;
463ee0b2 2298 }
79072805
LW
2299}
2300
a0d0e21e 2301PP(pp_scmp)
79072805 2302{
20b7effb 2303 dSP; dTARGET;
6f1401dc 2304 tryAMAGICbin_MG(scmp_amg, 0);
a0d0e21e
LW
2305 {
2306 dPOPTOPssrl;
130c5df3 2307 const int cmp =
5778acb6 2308#ifdef USE_LOCALE_COLLATE
130c5df3
KW
2309 (IN_LC_RUNTIME(LC_COLLATE))
2310 ? sv_cmp_locale_flags(left, right, 0)
2311 :
2312#endif
2313 sv_cmp_flags(left, right, 0);
bbce6d69 2314 SETi( cmp );
a0d0e21e
LW
2315 RETURN;
2316 }
2317}
79072805 2318
55497cff 2319PP(pp_bit_and)
2320{
20b7effb 2321 dSP; dATARGET;
6f1401dc 2322 tryAMAGICbin_MG(band_amg, AMGf_assign);
a0d0e21e
LW
2323 {
2324 dPOPTOPssrl;
4633a7c4 2325 if (SvNIOKp(left) || SvNIOKp(right)) {
b20c4ee1
FC
2326 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2327 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
d0ba1bd2 2328 if (PL_op->op_private & HINT_INTEGER) {
1b6737cc 2329 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
972b05a9 2330 SETi(i);
d0ba1bd2
JH
2331 }
2332 else {
1b6737cc 2333 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
972b05a9 2334 SETu(u);
d0ba1bd2 2335 }
5ee80e13 2336 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
b20c4ee1 2337 if (right_ro_nonnum) SvNIOK_off(right);
a0d0e21e
LW
2338 }
2339 else {
533c011a 2340 do_vop(PL_op->op_type, TARG, left, right);
a0d0e21e
LW
2341 SETTARG;
2342 }
2343 RETURN;
2344 }
2345}
79072805 2346
5d01050a
FC
2347PP(pp_nbit_and)
2348{
2349 dSP;
636ac8fc 2350 tryAMAGICbin_MG(band_amg, AMGf_assign|AMGf_numarg);
5d01050a
FC
2351 {
2352 dATARGET; dPOPTOPssrl;
2353 if (PL_op->op_private & HINT_INTEGER) {
2354 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2355 SETi(i);
2356 }
2357 else {
2358 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2359 SETu(u);
2360 }
2361 }
2362 RETURN;
2363}
2364
2365PP(pp_sbit_and)
2366{
2367 dSP;
2368 tryAMAGICbin_MG(sband_amg, AMGf_assign);
2369 {
2370 dATARGET; dPOPTOPssrl;
2371 do_vop(OP_BIT_AND, TARG, left, right);
2372 RETSETTARG;
2373 }
2374}
b1c05ba5
DM
2375
2376/* also used for: pp_bit_xor() */
2377
a0d0e21e
LW
2378PP(pp_bit_or)
2379{
20b7effb 2380 dSP; dATARGET;
3658c1f1
NC
2381 const int op_type = PL_op->op_type;
2382
6f1401dc 2383 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
a0d0e21e
LW
2384 {
2385 dPOPTOPssrl;
4633a7c4 2386 if (SvNIOKp(left) || SvNIOKp(right)) {
b20c4ee1
FC
2387 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2388 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
d0ba1bd2 2389 if (PL_op->op_private & HINT_INTEGER) {
3658c1f1
NC
2390 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2391 const IV r = SvIV_nomg(right);
2392 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2393 SETi(result);
d0ba1bd2
JH
2394 }
2395 else {
3658c1f1
NC
2396 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2397 const UV r = SvUV_nomg(right);
2398 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2399 SETu(result);
d0ba1bd2 2400 }
5ee80e13 2401 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
b20c4ee1 2402 if (right_ro_nonnum) SvNIOK_off(right);
a0d0e21e
LW
2403 }
2404 else {
3658c1f1 2405 do_vop(op_type, TARG, left, right);
a0d0e21e
LW
2406 SETTARG;
2407 }
2408 RETURN;
79072805 2409 }
a0d0e21e 2410}
79072805 2411
5d01050a
FC
2412/* also used for: pp_nbit_xor() */
2413
2414PP(pp_nbit_or)
2415{
2416 dSP;
2417 const int op_type = PL_op->op_type;
2418
2419 tryAMAGICbin_MG((op_type == OP_NBIT_OR ? bor_amg : bxor_amg),
636ac8fc 2420 AMGf_assign|AMGf_numarg);
5d01050a
FC
2421 {
2422 dATARGET; dPOPTOPssrl;
2423 if (PL_op->op_private & HINT_INTEGER) {
2424 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2425 const IV r = SvIV_nomg(right);
2426 const IV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2427 SETi(result);
2428 }
2429 else {
2430 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2431 const UV r = SvUV_nomg(right);
2432 const UV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
2433 SETu(result);
2434 }
2435 }
2436 RETURN;
2437}
2438
2439/* also used for: pp_sbit_xor() */
2440
2441PP(pp_sbit_or)
2442{
2443 dSP;
2444 const int op_type = PL_op->op_type;
2445
2446 tryAMAGICbin_MG((op_type == OP_SBIT_OR ? sbor_amg : sbxor_amg),
2447 AMGf_assign);
2448 {
2449 dATARGET; dPOPTOPssrl;
2450 do_vop(op_type == OP_SBIT_OR ? OP_BIT_OR : OP_BIT_XOR, TARG, left,
2451 right);
2452 RETSETTARG;
2453 }
2454}
2455
1c2b3fd6
FC
2456PERL_STATIC_INLINE bool
2457S_negate_string(pTHX)
2458{
2459 dTARGET; dSP;
2460 STRLEN len;
2461 const char *s;
2462 SV * const sv = TOPs;
2463 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2464 return FALSE;
2465 s = SvPV_nomg_const(sv, len);
2466 if (isIDFIRST(*s)) {
2467 sv_setpvs(TARG, "-");
2468 sv_catsv(TARG, sv);
2469 }
2470 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2471 sv_setsv_nomg(TARG, sv);
2472 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2473 }
2474 else return FALSE;
245d035e 2475 SETTARG;
1c2b3fd6
FC
2476 return TRUE;
2477}
2478
a0d0e21e
LW
2479PP(pp_negate)
2480{
20b7effb 2481 dSP; dTARGET;
6f1401dc 2482 tryAMAGICun_MG(neg_amg, AMGf_numeric);
1c2b3fd6 2483 if (S_negate_string(aTHX)) return NORMAL;
a0d0e21e 2484 {
6f1401dc 2485 SV * const sv = TOPs;
a5b92898 2486
d96ab1b5 2487 if (SvIOK(sv)) {
7dbe3150 2488 /* It's publicly an integer */
28e5dec8 2489 oops_its_an_int:
9b0e499b
GS
2490 if (SvIsUV(sv)) {
2491 if (SvIVX(sv) == IV_MIN) {
28e5dec8 2492 /* 2s complement assumption. */
d14578b8
KW
2493 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) ==
2494 IV_MIN */
245d035e 2495 return NORMAL;
9b0e499b
GS
2496 }
2497 else if (SvUVX(sv) <= IV_MAX) {
beccb14c 2498 SETi(-SvIVX(sv));
245d035e 2499 return NORMAL;
9b0e499b
GS
2500 }
2501 }
2502 else if (SvIVX(sv) != IV_MIN) {
2503 SETi(-SvIVX(sv));
245d035e 2504 return NORMAL;
9b0e499b 2505 }
28e5dec8
JH
2506#ifdef PERL_PRESERVE_IVUV
2507 else {
2508 SETu((UV)IV_MIN);
245d035e 2509 return NORMAL;
28e5dec8
JH
2510 }
2511#endif
9b0e499b 2512 }
8a5decd8 2513 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
6f1401dc 2514 SETn(-SvNV_nomg(sv));
1c2b3fd6 2515 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
8eb28a70 2516 goto oops_its_an_int;
4633a7c4 2517 else
6f1401dc 2518 SETn(-SvNV_nomg(sv));
79072805 2519 }
245d035e 2520 return NORMAL;
79072805
LW
2521}
2522
a0d0e21e 2523PP(pp_not)
79072805 2524{
20b7effb 2525 dSP;
f4c975aa
DM
2526 SV *sv;
2527
6f1401dc 2528 tryAMAGICun_MG(not_amg, AMGf_set);
f4c975aa
DM
2529 sv = *PL_stack_sp;
2530 *PL_stack_sp = boolSV(!SvTRUE_nomg_NN(sv));
a0d0e21e 2531 return NORMAL;
79072805
LW
2532}
2533
5d01050a
FC
2534static void
2535S_scomplement(pTHX_ SV *targ, SV *sv)
79072805 2536{
eb578fdb
KW
2537 U8 *tmps;
2538 I32 anum;
a0d0e21e
LW
2539 STRLEN len;
2540
85b0ee6e
FC
2541 sv_copypv_nomg(TARG, sv);
2542 tmps = (U8*)SvPV_nomg(TARG, len);
08b6664b 2543
1d68d6cd 2544 if (SvUTF8(TARG)) {
08b6664b
KW
2545 if (len && ! utf8_to_bytes(tmps, &len)) {
2546 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[PL_op->op_type]);
2547 }
2548 SvCUR(TARG) = len;
2549 SvUTF8_off(TARG);
2550 }
2551
2552 anum = len;
1d68d6cd 2553
a0d0e21e 2554#ifdef LIBERAL
51723571 2555 {
eb578fdb 2556 long *tmpl;
51723571
JH
2557 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2558 *tmps = ~*tmps;
2559 tmpl = (long*)tmps;
bb7a0f54 2560 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
51723571
JH
2561 *tmpl = ~*tmpl;
2562 tmps = (U8*)tmpl;
2563 }
a0d0e21e
LW
2564#endif
2565 for ( ; anum > 0; anum--, tmps++)
2566 *tmps = ~*tmps;
5d01050a
FC
2567}
2568
2569PP(pp_complement)
2570{
2571 dSP; dTARGET;
2572 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2573 {
2574 dTOPss;
2575 if (SvNIOKp(sv)) {
2576 if (PL_op->op_private & HINT_INTEGER) {
2577 const IV i = ~SvIV_nomg(sv);
2578 SETi(i);
2579 }
2580 else {
2581 const UV u = ~SvUV_nomg(sv);
2582 SETu(u);
2583 }
2584 }
2585 else {
2586 S_scomplement(aTHX_ TARG, sv);
ec93b65f 2587 SETTARG;
a0d0e21e 2588 }
24840750 2589 return NORMAL;
5d01050a
FC
2590 }
2591}
2592
2593PP(pp_ncomplement)
2594{
2595 dSP;
636ac8fc 2596 tryAMAGICun_MG(compl_amg, AMGf_numeric|AMGf_numarg);
5d01050a
FC
2597 {
2598 dTARGET; dTOPss;
2599 if (PL_op->op_private & HINT_INTEGER) {
2600 const IV i = ~SvIV_nomg(sv);
2601 SETi(i);
2602 }
2603 else {
2604 const UV u = ~SvUV_nomg(sv);
2605 SETu(u);
2606 }
2607 }
2608 return NORMAL;
2609}
2610
2611PP(pp_scomplement)
2612{
2613 dSP;
2614 tryAMAGICun_MG(scompl_amg, AMGf_numeric);
2615 {
2616 dTARGET; dTOPss;
2617 S_scomplement(aTHX_ TARG, sv);
2618 SETTARG;
2619 return NORMAL;
a0d0e21e 2620 }
79072805
LW
2621}
2622
a0d0e21e
LW
2623/* integer versions of some of the above */
2624
a0d0e21e 2625PP(pp_i_multiply)
79072805 2626{
20b7effb 2627 dSP; dATARGET;
6f1401dc 2628 tryAMAGICbin_MG(mult_amg, AMGf_assign);
a0d0e21e 2629 {
6f1401dc 2630 dPOPTOPiirl_nomg;
a0d0e21e
LW
2631 SETi( left * right );
2632 RETURN;
2633 }
79072805
LW
2634}
2635
a0d0e21e 2636PP(pp_i_divide)
79072805 2637{
85935d8e 2638 IV num;
20b7effb 2639 dSP; dATARGET;
6f1401dc 2640 tryAMAGICbin_MG(div_amg, AMGf_assign);
a0d0e21e 2641 {
6f1401dc 2642 dPOPTOPssrl;
85935d8e 2643 IV value = SvIV_nomg(right);
a0d0e21e 2644 if (value == 0)
ece1bcef 2645 DIE(aTHX_ "Illegal division by zero");
85935d8e 2646 num = SvIV_nomg(left);
a0cec769
YST
2647
2648 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2649 if (value == -1)
2650 value = - num;
2651 else
2652 value = num / value;
6f1401dc 2653 SETi(value);
a0d0e21e
LW
2654 RETURN;
2655 }
79072805
LW
2656}
2657
befad5d1 2658PP(pp_i_modulo)
224ec323
JH
2659{
2660 /* This is the vanilla old i_modulo. */
20b7effb 2661 dSP; dATARGET;
6f1401dc 2662 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2663 {
6f1401dc 2664 dPOPTOPiirl_nomg;
224ec323
JH
2665 if (!right)
2666 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2667 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2668 if (right == -1)
2669 SETi( 0 );
2670 else
2671 SETi( left % right );
224ec323
JH
2672 RETURN;
2673 }
2674}
2675
0927ade0 2676#if defined(__GLIBC__) && IVSIZE == 8 \
bf3d06aa 2677 && ( __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8))
befad5d1 2678
0927ade0 2679PP(pp_i_modulo_glibc_bugfix)
224ec323 2680{
224ec323 2681 /* This is the i_modulo with the workaround for the _moddi3 bug
fce2b89e 2682 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
224ec323 2683 * See below for pp_i_modulo. */
20b7effb 2684 dSP; dATARGET;
6f1401dc 2685 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2686 {
6f1401dc 2687 dPOPTOPiirl_nomg;
224ec323
JH
2688 if (!right)
2689 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2690 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2691 if (right == -1)
2692 SETi( 0 );
2693 else
2694 SETi( left % PERL_ABS(right) );
224ec323
JH
2695 RETURN;
2696 }
224ec323 2697}
befad5d1 2698#endif
79072805 2699
a0d0e21e 2700PP(pp_i_add)
79072805 2701{
20b7effb 2702 dSP; dATARGET;
6f1401dc 2703 tryAMAGICbin_MG(add_amg, AMGf_assign);
a0d0e21e 2704 {
6f1401dc 2705 dPOPTOPiirl_ul_nomg;
a0d0e21e
LW
2706 SETi( left + right );
2707 RETURN;
79072805 2708 }
79072805
LW
2709}
2710
a0d0e21e 2711PP(pp_i_subtract)
79072805 2712{
20b7effb 2713 dSP; dATARGET;
6f1401dc 2714 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
a0d0e21e 2715 {
6f1401dc 2716 dPOPTOPiirl_ul_nomg;
a0d0e21e
LW
2717 SETi( left - right );
2718 RETURN;
79072805 2719 }
79072805
LW
2720}
2721
a0d0e21e 2722PP(pp_i_lt)
79072805 2723{
20b7effb 2724 dSP;
6f1401dc 2725 tryAMAGICbin_MG(lt_amg, AMGf_set);
a0d0e21e 2726 {
96b6b87f 2727 dPOPTOPiirl_nomg;
54310121 2728 SETs(boolSV(left < right));
a0d0e21e
LW
2729 RETURN;
2730 }
79072805
LW
2731}
2732
a0d0e21e 2733PP(pp_i_gt)
79072805 2734{
20b7effb 2735 dSP;
6f1401dc 2736 tryAMAGICbin_MG(gt_amg, AMGf_set);
a0d0e21e 2737 {
96b6b87f 2738 dPOPTOPiirl_nomg;
54310121 2739 SETs(boolSV(left > right));
a0d0e21e
LW
2740 RETURN;
2741 }
79072805
LW
2742}
2743
a0d0e21e 2744PP(pp_i_le)
79072805 2745{
20b7effb 2746 dSP;
6f1401dc 2747 tryAMAGICbin_MG(le_amg, AMGf_set);
a0d0e21e 2748 {
96b6b87f 2749 dPOPTOPiirl_nomg;
54310121 2750 SETs(boolSV(left <= right));
a0d0e21e 2751 RETURN;
85e6fe83 2752 }
79072805
LW
2753}
2754
a0d0e21e 2755PP(pp_i_ge)
79072805 2756{
20b7effb 2757 dSP;
6f1401dc 2758 tryAMAGICbin_MG(ge_amg, AMGf_set);
a0d0e21e 2759 {
96b6b87f 2760 dPOPTOPiirl_nomg;
54310121 2761 SETs(boolSV(left >= right));
a0d0e21e
LW
2762 RETURN;
2763 }
79072805
LW
2764}
2765
a0d0e21e 2766PP(pp_i_eq)
79072805 2767{
20b7effb 2768 dSP;
6f1401dc 2769 tryAMAGICbin_MG(eq_amg, AMGf_set);
a0d0e21e 2770 {
96b6b87f 2771 dPOPTOPiirl_nomg;
54310121 2772 SETs(boolSV(left == right));
a0d0e21e
LW
2773 RETURN;
2774 }
79072805
LW
2775}
2776
a0d0e21e 2777PP(pp_i_ne)
79072805 2778{
20b7effb 2779 dSP;
6f1401dc 2780 tryAMAGICbin_MG(ne_amg, AMGf_set);
a0d0e21e 2781 {
96b6b87f 2782 dPOPTOPiirl_nomg;
54310121 2783 SETs(boolSV(left != right));
a0d0e21e
LW
2784 RETURN;
2785 }
79072805
LW
2786}
2787
a0d0e21e 2788PP(pp_i_ncmp)
79072805 2789{
20b7effb 2790 dSP; dTARGET;
6f1401dc 2791 tryAMAGICbin_MG(ncmp_amg, 0);
a0d0e21e 2792 {
96b6b87f 2793 dPOPTOPiirl_nomg;
a0d0e21e 2794 I32 value;
79072805 2795
a0d0e21e 2796 if (left > right)
79072805 2797 value = 1;
a0d0e21e 2798 else if (left < right)
79072805 2799 value = -1;
a0d0e21e 2800 else
79072805 2801 value = 0;
a0d0e21e
LW
2802 SETi(value);
2803 RETURN;
79072805 2804 }
85e6fe83
LW
2805}
2806
2807PP(pp_i_negate)
2808{
20b7effb 2809 dSP; dTARGET;
6f1401dc 2810 tryAMAGICun_MG(neg_amg, 0);
1c2b3fd6 2811 if (S_negate_string(aTHX)) return NORMAL;
6f1401dc
DM
2812 {
2813 SV * const sv = TOPs;
2814 IV const i = SvIV_nomg(sv);
2815 SETi(-i);
ae642386 2816 return NORMAL;
6f1401dc 2817 }
85e6fe83
LW
2818}
2819
79072805
LW
2820/* High falutin' math. */
2821
2822PP(pp_atan2)
2823{
20b7effb 2824 dSP; dTARGET;
6f1401dc 2825 tryAMAGICbin_MG(atan2_amg, 0);
a0d0e21e 2826 {
096c060c 2827 dPOPTOPnnrl_nomg;
a1021d57 2828 SETn(Perl_atan2(left, right));
a0d0e21e
LW
2829 RETURN;
2830 }
79072805
LW
2831}
2832
b1c05ba5
DM
2833
2834/* also used for: pp_cos() pp_exp() pp_log() pp_sqrt() */
2835
79072805
LW
2836PP(pp_sin)
2837{
20b7effb 2838 dSP; dTARGET;
af71714e 2839 int amg_type = fallback_amg;
71302fe3 2840 const char *neg_report = NULL;
71302fe3
NC
2841 const int op_type = PL_op->op_type;
2842
2843 switch (op_type) {
af71714e
JH
2844 case OP_SIN: amg_type = sin_amg; break;
2845 case OP_COS: amg_type = cos_amg; break;
2846 case OP_EXP: amg_type = exp_amg; break;
2847 case OP_LOG: amg_type = log_amg; neg_report = "log"; break;
2848 case OP_SQRT: amg_type = sqrt_amg; neg_report = "sqrt"; break;
a0d0e21e 2849 }
79072805 2850
af71714e 2851 assert(amg_type != fallback_amg);
6f1401dc
DM
2852
2853 tryAMAGICun_MG(amg_type, 0);
a0d0e21e 2854 {
8c78ed36 2855 SV * const arg = TOPs;
6f1401dc 2856 const NV value = SvNV_nomg(arg);
a5dc2484 2857#ifdef NV_NAN
f256868e 2858 NV result = NV_NAN;
a5dc2484
JH
2859#else
2860 NV result = 0.0;
2861#endif
af71714e 2862 if (neg_report) { /* log or sqrt */
a3463d96
DD
2863 if (
2864#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2865 ! Perl_isnan(value) &&
2866#endif
2867 (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0))) {
71302fe3 2868 SET_NUMERIC_STANDARD();
dcbac5bb 2869 /* diag_listed_as: Can't take log of %g */
147e3846 2870 DIE(aTHX_ "Can't take %s of %" NVgf, neg_report, value);
71302fe3
NC
2871 }
2872 }
af71714e 2873 switch (op_type) {
f256868e 2874 default:
af71714e
JH
2875 case OP_SIN: result = Perl_sin(value); break;
2876 case OP_COS: result = Perl_cos(value); break;
2877 case OP_EXP: result = Perl_exp(value); break;
2878 case OP_LOG: result = Perl_log(value); break;
2879 case OP_SQRT: result = Perl_sqrt(value); break;
2880 }
8c78ed36
FC
2881 SETn(result);
2882 return NORMAL;
a0d0e21e 2883 }
79072805
LW
2884}
2885
56cb0a1c
AD
2886/* Support Configure command-line overrides for rand() functions.
2887 After 5.005, perhaps we should replace this by Configure support
2888 for drand48(), random(), or rand(). For 5.005, though, maintain
2889 compatibility by calling rand() but allow the user to override it.
2890 See INSTALL for details. --Andy Dougherty 15 July 1998
2891*/
85ab1d1d
JH
2892/* Now it's after 5.005, and Configure supports drand48() and random(),
2893 in addition to rand(). So the overrides should not be needed any more.
2894 --Jarkko Hietaniemi 27 September 1998
2895 */
2896
79072805
LW
2897PP(pp_rand)
2898{
80252599 2899 if (!PL_srand_called) {
85ab1d1d 2900 (void)seedDrand01((Rand_seed_t)seed());
80252599 2901 PL_srand_called = TRUE;
93dc8474 2902 }
fdf4dddd
DD
2903 {
2904 dSP;
2905 NV value;
fdf4dddd
DD
2906
2907 if (MAXARG < 1)
7e9044f9
FC
2908 {
2909 EXTEND(SP, 1);
fdf4dddd 2910 value = 1.0;
7e9044f9 2911 }
fdf4dddd
DD
2912 else {
2913 SV * const sv = POPs;
2914 if(!sv)
2915 value = 1.0;
2916 else
2917 value = SvNV(sv);
2918 }
2919 /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
a3463d96
DD
2920#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2921 if (! Perl_isnan(value) && value == 0.0)
2922#else
659c4b96 2923 if (value == 0.0)
a3463d96 2924#endif
fdf4dddd
DD
2925 value = 1.0;
2926 {
2927 dTARGET;
2928 PUSHs(TARG);
2929 PUTBACK;
2930 value *= Drand01();
2931 sv_setnv_mg(TARG, value);
2932 }
2933 }
2934 return NORMAL;
79072805
LW
2935}
2936
2937PP(pp_srand)
2938{
20b7effb 2939 dSP; dTARGET;
f914a682
JL
2940 UV anum;
2941
0a5f3363 2942 if (MAXARG >= 1 && (TOPs || POPs)) {
f914a682
JL
2943 SV *top;
2944 char *pv;
2945 STRLEN len;
2946 int flags;
2947
2948 top = POPs;
2949 pv = SvPV(top, len);
2950 flags = grok_number(pv, len, &anum);
2951
2952 if (!(flags & IS_NUMBER_IN_UV)) {
2953 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2954 "Integer overflow in srand");
2955 anum = UV_MAX;
2956 }
2957 }
2958 else {
2959 anum = seed();
2960 }
2961
85ab1d1d 2962 (void)seedDrand01((Rand_seed_t)anum);
80252599 2963 PL_srand_called = TRUE;
da1010ec
NC
2964 if (anum)
2965 XPUSHu(anum);
2966 else {
2967 /* Historically srand always returned true. We can avoid breaking
2968 that like this: */
2969 sv_setpvs(TARG, "0 but true");
2970 XPUSHTARG;
2971 }
83832992 2972 RETURN;
79072805
LW
2973}
2974
79072805
LW
2975PP(pp_int)
2976{
20b7effb 2977 dSP; dTARGET;
6f1401dc 2978 tryAMAGICun_MG(int_amg, AMGf_numeric);
774d564b 2979 {
6f1401dc
DM
2980 SV * const sv = TOPs;
2981 const IV iv = SvIV_nomg(sv);
28e5dec8
JH
2982 /* XXX it's arguable that compiler casting to IV might be subtly
2983 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2984 else preferring IV has introduced a subtle behaviour change bug. OTOH
2985 relying on floating point to be accurate is a bug. */
2986
c781a409 2987 if (!SvOK(sv)) {
922c4365 2988 SETu(0);
c781a409
RD
2989 }
2990 else if (SvIOK(sv)) {
2991 if (SvIsUV(sv))
6f1401dc 2992 SETu(SvUV_nomg(sv));
c781a409 2993 else
28e5dec8 2994 SETi(iv);
c781a409 2995 }
c781a409 2996 else {
6f1401dc 2997 const NV value = SvNV_nomg(sv);
b9d05018
FC
2998 if (UNLIKELY(Perl_isinfnan(value)))
2999 SETn(value);
5bf8b78e 3000 else if (value >= 0.0) {
28e5dec8
JH
3001 if (value < (NV)UV_MAX + 0.5) {
3002 SETu(U_V(value));
3003 } else {
059a1014 3004 SETn(Perl_floor(value));
28e5dec8 3005 }
1048ea30 3006 }
28e5dec8
JH
3007 else {
3008 if (value > (NV)IV_MIN - 0.5) {
3009 SETi(I_V(value));
3010 } else {
1bbae031 3011 SETn(Perl_ceil(value));
28e5dec8
JH
3012 }
3013 }
774d564b 3014 }
79072805 3015 }
699e9491 3016 return NORMAL;
79072805
LW
3017}
3018
463ee0b2
LW
3019PP(pp_abs)
3020{
20b7effb 3021 dSP; dTARGET;
6f1401dc 3022 tryAMAGICun_MG(abs_amg, AMGf_numeric);
a0d0e21e 3023 {
6f1401dc 3024 SV * const sv = TOPs;
28e5dec8 3025 /* This will cache the NV value if string isn't actually integer */
6f1401dc 3026 const IV iv = SvIV_nomg(sv);
a227d84d 3027
800401ee 3028 if (!SvOK(sv)) {
922c4365 3029 SETu(0);
800401ee
JH
3030 }
3031 else if (SvIOK(sv)) {
28e5dec8 3032 /* IVX is precise */
800401ee 3033 if (SvIsUV(sv)) {
6f1401dc 3034 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
28e5dec8
JH
3035 } else {
3036 if (iv >= 0) {
3037 SETi(iv);
3038 } else {
3039 if (iv != IV_MIN) {
3040 SETi(-iv);
3041 } else {
3042 /* 2s complement assumption. Also, not really needed as
3043 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
b396d0d8 3044 SETu((UV)IV_MIN);
28e5dec8 3045 }
a227d84d 3046 }
28e5dec8
JH
3047 }
3048 } else{
6f1401dc 3049 const NV value = SvNV_nomg(sv);
774d564b 3050 if (value < 0.0)
1b6737cc 3051 SETn(-value);
a4474c9e
DD
3052 else
3053 SETn(value);
774d564b 3054 }
a0d0e21e 3055 }
067b7929 3056 return NORMAL;
463ee0b2
LW
3057}
3058
b1c05ba5
DM
3059
3060/* also used for: pp_hex() */
3061
79072805
LW
3062PP(pp_oct)
3063{
20b7effb 3064 dSP; dTARGET;
5c144d81 3065 const char *tmps;
53305cf1 3066 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
6f894ead 3067 STRLEN len;
53305cf1
NC
3068 NV result_nv;
3069 UV result_uv;
4e51bcca 3070 SV* const sv = TOPs;
79072805 3071
349d4f2f 3072 tmps = (SvPV_const(sv, len));
2bc69dc4
NIS
3073 if (DO_UTF8(sv)) {
3074 /* If Unicode, try to downgrade
3075 * If not possible, croak. */
1b6737cc 3076 SV* const tsv = sv_2mortal(newSVsv(sv));
2bc69dc4
NIS
3077
3078 SvUTF8_on(tsv);
3079 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 3080 tmps = SvPV_const(tsv, len);
2bc69dc4 3081 }
daa2adfd
NC
3082 if (PL_op->op_type == OP_HEX)
3083 goto hex;
3084
6f894ead 3085 while (*tmps && len && isSPACE(*tmps))
53305cf1 3086 tmps++, len--;
9e24b6e2 3087 if (*tmps == '0')
53305cf1 3088 tmps++, len--;
305b8651 3089 if (isALPHA_FOLD_EQ(*tmps, 'x')) {
daa2adfd 3090 hex:
53305cf1 3091 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
daa2adfd 3092 }
305b8651 3093 else if (isALPHA_FOLD_EQ(*tmps, 'b'))
53305cf1 3094 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
464e2e8a 3095 else
53305cf1
NC
3096 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3097
3098 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
4e51bcca 3099 SETn(result_nv);
53305cf1
NC
3100 }
3101 else {
4e51bcca 3102 SETu(result_uv);
53305cf1 3103 }
4e51bcca 3104 return NORMAL;
79072805
LW
3105}
3106
3107/* String stuff. */
3108
5febd2ff 3109
79072805
LW
3110PP(pp_length)
3111{
20b7effb 3112 dSP; dTARGET;
0bd48802 3113 SV * const sv = TOPs;
a0ed51b3 3114
7776003e 3115 U32 in_bytes = IN_BYTES;
5febd2ff
DM
3116 /* Simplest case shortcut:
3117 * set svflags to just the SVf_POK|SVs_GMG|SVf_UTF8 from the SV,
3118 * with the SVf_UTF8 flag inverted if under 'use bytes' (HINT_BYTES
3119 * set)
3120 */
7776003e 3121 U32 svflags = (SvFLAGS(sv) ^ (in_bytes << 26)) & (SVf_POK|SVs_GMG|SVf_UTF8);
5febd2ff
DM
3122
3123 STATIC_ASSERT_STMT(SVf_UTF8 == (HINT_BYTES << 26));
7776003e
DD
3124 SETs(TARG);
3125
5febd2ff 3126 if (LIKELY(svflags == SVf_POK))
7776003e 3127 goto simple_pv;
5febd2ff
DM
3128
3129 if (svflags & SVs_GMG)
7776003e 3130 mg_get(sv);
5febd2ff 3131
0f43fd57 3132 if (SvOK(sv)) {
5b750817 3133 STRLEN len;
f446eca7
DM
3134 if (!IN_BYTES) { /* reread to avoid using an C auto/register */
3135 if ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == SVf_POK)
3136 goto simple_pv;
7b394f12
DM
3137 if ( SvPOK(sv) && (PL_op->op_private & OPpTRUEBOOL)) {
3138 /* no need to convert from bytes to chars */
3139 len = SvCUR(sv);
3140 goto return_bool;
3141 }
5b750817 3142 len = sv_len_utf8_nomg(sv);
f446eca7 3143 }
5febd2ff 3144 else {
7776003e 3145 /* unrolled SvPV_nomg_const(sv,len) */
5febd2ff
DM
3146 if (SvPOK_nog(sv)) {
3147 simple_pv:
7776003e 3148 len = SvCUR(sv);
7b394f12
DM
3149 if (PL_op->op_private & OPpTRUEBOOL) {
3150 return_bool:
3151 SETs(len ? &PL_sv_yes : &PL_sv_zero);
3152 return NORMAL;
3153 }
5febd2ff
DM
3154 }
3155 else {
7776003e
DD
3156 (void)sv_2pv_flags(sv, &len, 0|SV_CONST_RETURN);
3157 }
0f43fd57 3158 }
5b750817 3159 TARGi((IV)(len), 1);
5febd2ff
DM
3160 }
3161 else {
9407f9c1 3162 if (!SvPADTMP(TARG)) {
5febd2ff 3163 /* OPpTARGET_MY: targ is var in '$lex = length()' */
e03e82a0 3164 sv_set_undef(TARG);
5b750817 3165 SvSETMAGIC(TARG);
5febd2ff
DM
3166 }
3167 else
3168 /* TARG is on stack at this point and is overwriten by SETs.
3169 * This branch is the odd one out, so put TARG by default on
3170 * stack earlier to let local SP go out of liveness sooner */
7776003e 3171 SETs(&PL_sv_undef);
92331800 3172 }
7776003e 3173 return NORMAL; /* no putback, SP didn't move in this opcode */
79072805
LW
3174}
3175
5febd2ff 3176
83f78d1a
FC
3177/* Returns false if substring is completely outside original string.
3178 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
3179 always be true for an explicit 0.
3180*/
3181bool
ddeaf645
DD
3182Perl_translate_substr_offsets( STRLEN curlen, IV pos1_iv,
3183 bool pos1_is_uv, IV len_iv,
3184 bool len_is_uv, STRLEN *posp,
3185 STRLEN *lenp)
83f78d1a
FC
3186{
3187 IV pos2_iv;
3188 int pos2_is_uv;
3189
3190 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
3191
3192 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
3193 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3194 pos1_iv += curlen;
3195 }
3196 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
3197 return FALSE;
3198
3199 if (len_iv || len_is_uv) {
3200 if (!len_is_uv && len_iv < 0) {
3201 pos2_iv = curlen + len_iv;
3202 if (curlen)
3203 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3204 else
3205 pos2_is_uv = 0;
3206 } else { /* len_iv >= 0 */
3207 if (!pos1_is_uv && pos1_iv < 0) {
3208 pos2_iv = pos1_iv + len_iv;
3209 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3210 } else {
3211 if ((UV)len_iv > curlen-(UV)pos1_iv)
3212 pos2_iv = curlen;
3213 else
3214 pos2_iv = pos1_iv+len_iv;
3215 pos2_is_uv = 1;
3216 }
3217 }
3218 }
3219 else {
3220 pos2_iv = curlen;
3221 pos2_is_uv = 1;
3222 }
3223
3224 if (!pos2_is_uv && pos2_iv < 0) {
3225 if (!pos1_is_uv && pos1_iv < 0)
3226 return FALSE;
3227 pos2_iv = 0;
3228 }
3229 else if (!pos1_is_uv && pos1_iv < 0)
3230 pos1_iv = 0;
3231
3232 if ((UV)pos2_iv < (UV)pos1_iv)
3233 pos2_iv = pos1_iv;
3234 if ((UV)pos2_iv > curlen)
3235 pos2_iv = curlen;
3236
3237 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3238 *posp = (STRLEN)( (UV)pos1_iv );
3239 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3240
3241 return TRUE;
3242}
3243
79072805
LW
3244PP(pp_substr)
3245{
20b7effb 3246 dSP; dTARGET;
79072805 3247 SV *sv;
463ee0b2 3248 STRLEN curlen;
9402d6ed 3249 STRLEN utf8_curlen;
777f7c56
EB
3250 SV * pos_sv;
3251 IV pos1_iv;
3252 int pos1_is_uv;
777f7c56
EB
3253 SV * len_sv;
3254 IV len_iv = 0;
83f78d1a 3255 int len_is_uv = 0;
24fcb59f 3256 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
bbddc9e0 3257 const bool rvalue = (GIMME_V != G_VOID);
e1ec3a88 3258 const char *tmps;
9402d6ed 3259 SV *repl_sv = NULL;
cbbf8932 3260 const char *repl = NULL;
7b8d334a 3261 STRLEN repl_len;
7bc95ae1 3262 int num_args = PL_op->op_private & 7;
13e30c65 3263 bool repl_need_utf8_upgrade = FALSE;
79072805 3264
78f9721b
SM
3265 if (num_args > 2) {
3266 if (num_args > 3) {
24fcb59f 3267 if(!(repl_sv = POPs)) num_args--;
7bc95ae1
FC
3268 }
3269 if ((len_sv = POPs)) {
3270 len_iv = SvIV(len_sv);
83f78d1a 3271 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
7b8d334a 3272 }
7bc95ae1 3273 else num_args--;
5d82c453 3274 }
777f7c56
EB
3275 pos_sv = POPs;
3276 pos1_iv = SvIV(pos_sv);
3277 pos1_is_uv = SvIOK_UV(pos_sv);
79072805 3278 sv = POPs;
24fcb59f
FC
3279 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3280 assert(!repl_sv);
3281 repl_sv = POPs;
3282 }
6582db62 3283 if (lvalue && !repl_sv) {
83f78d1a
FC
3284 SV * ret;
3285 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3286 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3287 LvTYPE(ret) = 'x';
3288 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3289 LvTARGOFF(ret) =
3290 pos1_is_uv || pos1_iv >= 0
3291 ? (STRLEN)(UV)pos1_iv
b063b0a8 3292 : (LvFLAGS(ret) |= LVf_NEG_OFF, (STRLEN)(UV)-pos1_iv);
83f78d1a
FC
3293 LvTARGLEN(ret) =
3294 len_is_uv || len_iv > 0
3295 ? (STRLEN)(UV)len_iv
b063b0a8 3296 : (LvFLAGS(ret) |= LVf_NEG_LEN, (STRLEN)(UV)-len_iv);
83f78d1a 3297
83f78d1a
FC
3298 PUSHs(ret); /* avoid SvSETMAGIC here */
3299 RETURN;
a74fb2cd 3300 }
6582db62
FC
3301 if (repl_sv) {
3302 repl = SvPV_const(repl_sv, repl_len);
3303 SvGETMAGIC(sv);
3304 if (SvROK(sv))
3305 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3306 "Attempt to use reference as lvalue in substr"
3307 );
3308 tmps = SvPV_force_nomg(sv, curlen);
3309 if (DO_UTF8(repl_sv) && repl_len) {
3310 if (!DO_UTF8(sv)) {
41b1e858
AC
3311 /* Upgrade the dest, and recalculate tmps in case the buffer
3312 * got reallocated; curlen may also have been changed */
01680ee9 3313 sv_utf8_upgrade_nomg(sv);
41b1e858 3314 tmps = SvPV_nomg(sv, curlen);
6582db62
FC
3315 }
3316 }
3317 else if (DO_UTF8(sv))
3318 repl_need_utf8_upgrade = TRUE;
3319 }
3320 else tmps = SvPV_const(sv, curlen);
7e2040f0 3321 if (DO_UTF8(sv)) {
0d788f38 3322 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
9402d6ed
JH
3323 if (utf8_curlen == curlen)
3324 utf8_curlen = 0;
a0ed51b3 3325 else
9402d6ed 3326 curlen = utf8_curlen;
a0ed51b3 3327 }
d1c2b58a 3328 else
9402d6ed 3329 utf8_curlen = 0;
a0ed51b3 3330
83f78d1a
FC
3331 {
3332 STRLEN pos, len, byte_len, byte_pos;
777f7c56 3333
83f78d1a
FC
3334 if (!translate_substr_offsets(
3335 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3336 )) goto bound_fail;
777f7c56 3337
83f78d1a
FC
3338 byte_len = len;
3339 byte_pos = utf8_curlen
0d788f38 3340 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
d931b1be 3341
2154eca7 3342 tmps += byte_pos;
bbddc9e0
CS
3343
3344 if (rvalue) {
3345 SvTAINTED_off(TARG); /* decontaminate */
3346 SvUTF8_off(TARG); /* decontaminate */
3347 sv_setpvn(TARG, tmps, byte_len);
12aa1545 3348#ifdef USE_LOCALE_COLLATE
bbddc9e0 3349 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
12aa1545 3350#endif
bbddc9e0
CS
3351 if (utf8_curlen)
3352 SvUTF8_on(TARG);
3353 }
2154eca7 3354
f7928d6c 3355 if (repl) {
13e30c65
JH
3356 SV* repl_sv_copy = NULL;
3357
3358 if (repl_need_utf8_upgrade) {
3359 repl_sv_copy = newSVsv(repl_sv);
3360 sv_utf8_upgrade(repl_sv_copy);
349d4f2f 3361 repl = SvPV_const(repl_sv_copy, repl_len);
13e30c65 3362 }
502d9230 3363 if (!SvOK(sv))
500f3e18 3364 SvPVCLEAR(sv);
777f7c56 3365 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
ef8d46e8 3366 SvREFCNT_dec(repl_sv_copy);
f7928d6c 3367 }
79072805 3368 }
6a9665b0
FC
3369 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST)
3370 SP++;
3371 else if (rvalue) {
bbddc9e0
CS
3372 SvSETMAGIC(TARG);
3373 PUSHs(TARG);
3374 }
79072805 3375 RETURN;
777f7c56 3376
7b52d656 3377 bound_fail:
83f78d1a 3378 if (repl)
777f7c56
EB
3379 Perl_croak(aTHX_ "substr outside of string");
3380 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3381 RETPUSHUNDEF;
79072805
LW
3382}
3383
3384PP(pp_vec)
3385{
20b7effb 3386 dSP;
eb578fdb 3387 const IV size = POPi;
d69c4304 3388 SV* offsetsv = POPs;
eb578fdb 3389 SV * const src = POPs;
1b6737cc 3390 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2154eca7 3391 SV * ret;
1b92e694
DM
3392 UV retuv;
3393 STRLEN offset = 0;
3394 char errflags = 0;
d69c4304
DM
3395
3396 /* extract a STRLEN-ranged integer value from offsetsv into offset,
1b92e694 3397 * or flag that its out of range */
d69c4304
DM
3398 {
3399 IV iv = SvIV(offsetsv);
3400
3401 /* avoid a large UV being wrapped to a negative value */
1b92e694 3402 if (SvIOK_UV(offsetsv) && SvUVX(offsetsv) > (UV)IV_MAX)
b063b0a8 3403 errflags = LVf_OUT_OF_RANGE;
1b92e694 3404 else if (iv < 0)
b063b0a8 3405 errflags = (LVf_NEG_OFF|LVf_OUT_OF_RANGE);
d69c4304 3406#if PTRSIZE < IVSIZE
1b92e694 3407 else if (iv > Size_t_MAX)
b063b0a8 3408 errflags = LVf_OUT_OF_RANGE;
d69c4304 3409#endif
1b92e694
DM
3410 else
3411 offset = (STRLEN)iv;
d69c4304
DM
3412 }
3413
1b92e694 3414 retuv = errflags ? 0 : do_vecget(src, offset, size);
a0d0e21e 3415
81e118e0 3416 if (lvalue) { /* it's an lvalue! */
2154eca7
EB
3417 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3418 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3419 LvTYPE(ret) = 'v';
3420 LvTARG(ret) = SvREFCNT_inc_simple(src);
3421 LvTARGOFF(ret) = offset;
3422 LvTARGLEN(ret) = size;
1b92e694 3423 LvFLAGS(ret) = errflags;
2154eca7
EB
3424 }
3425 else {
3426 dTARGET;
3427 SvTAINTED_off(TARG); /* decontaminate */
3428 ret = TARG;
79072805
LW
3429 }
3430
d69c4304 3431 sv_setuv(ret, retuv);
f9e95907
FC
3432 if (!lvalue)
3433 SvSETMAGIC(ret);
2154eca7 3434 PUSHs(ret);
79072805
LW
3435 RETURN;
3436}
3437
b1c05ba5
DM
3438
3439/* also used for: pp_rindex() */
3440
79072805
LW
3441PP(pp_index)
3442{
20b7effb 3443 dSP; dTARGET;
79072805
LW
3444 SV *big;
3445 SV *little;
c445ea15 3446 SV *temp = NULL;
ad66a58c 3447 STRLEN biglen;
2723d216 3448 STRLEN llen = 0;
b464e2b7
TC
3449 SSize_t offset = 0;
3450 SSize_t retval;
73ee8be2
NC
3451 const char *big_p;
3452 const char *little_p;
2f040f7f
NC
3453 bool big_utf8;
3454 bool little_utf8;
2723d216 3455 const bool is_index = PL_op->op_type == OP_INDEX;
d3e26383 3456 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
79072805 3457
e1dccc0d
Z
3458 if (threeargs)
3459 offset = POPi;
79072805
LW
3460 little = POPs;
3461 big = POPs;
73ee8be2
NC
3462 big_p = SvPV_const(big, biglen);
3463 little_p = SvPV_const(little, llen);
3464
e609e586
NC
3465 big_utf8 = DO_UTF8(big);
3466 little_utf8 = DO_UTF8(little);
3467 if (big_utf8 ^ little_utf8) {
3468 /* One needs to be upgraded. */
8df0e7a2 3469 if (little_utf8) {
2f040f7f
NC
3470 /* Well, maybe instead we might be able to downgrade the small
3471 string? */
1eced8f8 3472 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
2f040f7f
NC
3473 &little_utf8);
3474 if (little_utf8) {
3475 /* If the large string is ISO-8859-1, and it's not possible to
3476 convert the small string to ISO-8859-1, then there is no
3477 way that it could be found anywhere by index. */
3478 retval = -1;
7e8d786b 3479 goto push_result;
2f040f7f 3480 }
e609e586 3481
2f040f7f
NC
3482 /* At this point, pv is a malloc()ed string. So donate it to temp
3483 to ensure it will get free()d */
3484 little = temp = newSV(0);
73ee8be2
NC
3485 sv_usepvn(temp, pv, llen);
3486 little_p = SvPVX(little);
e609e586 3487 } else {
20e67ba1 3488 temp = newSVpvn(little_p, llen);
2f040f7f 3489
8df0e7a2 3490 sv_utf8_upgrade(temp);
20e67ba1
FC
3491 little = temp;
3492 little_p = SvPV_const(little, llen);
e609e586
NC
3493 }
3494 }
73ee8be2
NC
3495 if (SvGAMAGIC(big)) {
3496 /* Life just becomes a lot easier if I use a temporary here.
3497 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3498 will trigger magic and overloading again, as will fbm_instr()
3499 */
59cd0e26
NC
3500 big = newSVpvn_flags(big_p, biglen,
3501 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3502 big_p = SvPVX(big);
3503 }
e4e44778 3504 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
73ee8be2
NC
3505 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3506 warn on undef, and we've already triggered a warning with the
3507 SvPV_const some lines above. We can't remove that, as we need to
3508 call some SvPV to trigger overloading early and find out if the
3509 string is UTF-8.
8bd97c0c 3510 This is all getting too messy. The API isn't quite clean enough,
73ee8be2
NC
3511 because data access has side effects.
3512 */
59cd0e26
NC
3513 little = newSVpvn_flags(little_p, llen,
3514 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3515 little_p = SvPVX(little);
3516 }
e609e586 3517
d3e26383 3518 if (!threeargs)
2723d216 3519 offset = is_index ? 0 : biglen;
a0ed51b3 3520 else {
ad66a58c 3521 if (big_utf8 && offset > 0)
b464e2b7 3522 offset = sv_pos_u2b_flags(big, offset, 0, SV_CONST_RETURN);
73ee8be2
NC
3523 if (!is_index)
3524 offset += llen;
a0ed51b3 3525 }
79072805
LW
3526 if (offset < 0)
3527 offset = 0;
b464e2b7 3528 else if (offset > (SSize_t)biglen)
ad66a58c 3529 offset = biglen;
73ee8be2
NC
3530 if (!(little_p = is_index
3531 ? fbm_instr((unsigned char*)big_p + offset,
3532 (unsigned char*)big_p + biglen, little, 0)
3533 : rninstr(big_p, big_p + offset,
3534 little_p, little_p + llen)))
a0ed51b3 3535 retval = -1;
ad66a58c 3536 else {
73ee8be2 3537 retval = little_p - big_p;
15c41403 3538 if (retval > 1 && big_utf8)
b464e2b7 3539 retval = sv_pos_b2u_flags(big, retval, SV_CONST_RETURN);
ad66a58c 3540 }
ef8d46e8 3541 SvREFCNT_dec(temp);
7e8d786b
DM
3542
3543 push_result:
3544 /* OPpTRUEBOOL indicates an '== -1' has been optimised away */
3545 if (PL_op->op_private & OPpTRUEBOOL) {
3546 PUSHs( ((retval != -1) ^ cBOOL(PL_op->op_private & OPpINDEX_BOOLNEG))
3547 ? &PL_sv_yes : &PL_sv_no);
3548 if (PL_op->op_private & OPpTARGET_MY)
3549 /* $lex = (index() == -1) */
3550 sv_setsv(TARG, TOPs);
3551 }
3552 else
3553 PUSHi(retval);
79072805
LW
3554 RETURN;
3555}
3556
3557PP(pp_sprintf)
3558{
20b7effb 3559 dSP; dMARK; dORIGMARK; dTARGET;
3e6bd4bf 3560 SvTAINTED_off(TARG);
79072805 3561 do_sprintf(TARG, SP-MARK, MARK+1);
bbce6d69 3562 TAINT_IF(SvTAINTED(TARG));
79072805
LW
3563 SP = ORIGMARK;
3564 PUSHTARG;
3565 RETURN;
3566}
3567
79072805
LW
3568PP(pp_ord)
3569{
20b7effb 3570 dSP; dTARGET;
1eced8f8 3571
6ba92227 3572 SV *argsv = TOPs;
ba210ebe 3573 STRLEN len;
349d4f2f 3574 const U8 *s = (U8*)SvPV_const(argsv, len);
121910a4 3575
6ba92227 3576 SETu(DO_UTF8(argsv)
aee9b917 3577 ? (len ? utf8n_to_uvchr(s, len, 0, UTF8_ALLOW_ANYUV) : 0)
f3943cf2 3578 : (UV)(*s));
68795e93 3579
6ba92227 3580 return NORMAL;
79072805
LW
3581}
3582
463ee0b2
LW
3583PP(pp_chr)
3584{
20b7effb 3585 dSP; dTARGET;
463ee0b2 3586 char *tmps;
8a064bd6 3587 UV value;
d3261b99 3588 SV *top = TOPs;
8a064bd6 3589
71739502 3590 SvGETMAGIC(top);
9911fc4e
FC
3591 if (UNLIKELY(SvAMAGIC(top)))
3592 top = sv_2num(top);
99f450cc 3593 if (UNLIKELY(isinfnansv(top)))
147e3846 3594 Perl_croak(aTHX_ "Cannot chr %" NVgf, SvNV(top));
1cd88304
JH
3595 else {
3596 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3597 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3598 ||
3599 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
2cc2a5a0
KW
3600 && SvNV_nomg(top) < 0.0)))
3601 {
b3fe8680
FC
3602 if (ckWARN(WARN_UTF8)) {
3603 if (SvGMAGICAL(top)) {
3604 SV *top2 = sv_newmortal();
3605 sv_setsv_nomg(top2, top);
3606 top = top2;
3607 }
1cd88304 3608 Perl_warner(aTHX_ packWARN(WARN_UTF8),
147e3846 3609 "Invalid negative number (%" SVf ") in chr", SVfARG(top));
1cd88304
JH
3610 }
3611 value = UNICODE_REPLACEMENT;
3612 } else {
3613 value = SvUV_nomg(top);
3614 }
8a064bd6 3615 }
463ee0b2 3616
862a34c6 3617 SvUPGRADE(TARG,SVt_PV);
a0ed51b3 3618
0064a8a9 3619 if (value > 255 && !IN_BYTES) {
5f560d8a 3620 SvGROW(TARG, (STRLEN)UVCHR_SKIP(value)+1);
62961d2e 3621 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
349d4f2f 3622 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
a0ed51b3
LW
3623 *tmps = '\0';
3624 (void)SvPOK_only(TARG);
aa6ffa16 3625 SvUTF8_on(TARG);
d3261b99
FC
3626 SETTARG;
3627 return NORMAL;
a0ed51b3
LW
3628 }
3629
748a9306 3630 SvGROW(TARG,2);
463ee0b2
LW
3631 SvCUR_set(TARG, 1);
3632 tmps = SvPVX(TARG);
eb160463 3633 *tmps++ = (char)value;
748a9306 3634 *tmps = '\0';
a0d0e21e 3635 (void)SvPOK_only(TARG);
4c5ed6e2 3636
d3261b99
FC
3637 SETTARG;
3638 return NORMAL;
463ee0b2
LW
3639}
3640
79072805
LW
3641PP(pp_crypt)
3642{
79072805 3643#ifdef HAS_CRYPT
20b7effb 3644 dSP; dTARGET;
5f74f29c 3645 dPOPTOPssrl;
85c16d83 3646 STRLEN len;
10516c54 3647 const char *tmps = SvPV_const(left, len);
2bc69dc4 3648
85c16d83 3649 if (DO_UTF8(left)) {
2bc69dc4 3650 /* If Unicode, try to downgrade.
f2791508
JH
3651 * If not possible, croak.
3652 * Yes, we made this up. */
659fbb76 3653 SV* const tsv = newSVpvn_flags(tmps, len, SVf_UTF8|SVs_TEMP);
2bc69dc4 3654
2bc69dc4 3655 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 3656 tmps = SvPV_const(tsv, len);
85c16d83 3657 }
05404ffe
JH
3658# ifdef USE_ITHREADS
3659# ifdef HAS_CRYPT_R
3660 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3661 /* This should be threadsafe because in ithreads there is only
3662 * one thread per interpreter. If this would not be true,
3663 * we would need a mutex to protect this malloc. */
3664 PL_reentrant_buffer->_crypt_struct_buffer =
3665 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3666#if defined(__GLIBC__) || defined(__EMX__)
3667 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3668 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3669 /* work around glibc-2.2.5 bug */
3670 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3671 }
05404ffe 3672#endif
6ab58e4d 3673 }
05404ffe
JH
3674# endif /* HAS_CRYPT_R */
3675# endif /* USE_ITHREADS */
5f74f29c 3676# ifdef FCRYPT
83003860 3677 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3678# else
83003860 3679 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3680# endif
fbc76eb3 3681 SvUTF8_off(TARG);
ec93b65f 3682 SETTARG;
4808266b 3683 RETURN;
79072805 3684#else
b13b2135 3685 DIE(aTHX_
79072805
LW
3686 "The crypt() function is unimplemented due to excessive paranoia.");
3687#endif
79072805
LW
3688}
3689
00f254e2
KW
3690/* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3691 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3692
b1c05ba5
DM
3693
3694/* also used for: pp_lcfirst() */
3695
79072805
LW
3696PP(pp_ucfirst)
3697{
00f254e2
KW
3698 /* Actually is both lcfirst() and ucfirst(). Only the first character
3699 * changes. This means that possibly we can change in-place, ie., just
3700 * take the source and change that one character and store it back, but not
3701 * if read-only etc, or if the length changes */
3702
39644a26 3703 dSP;
d54190f6 3704 SV *source = TOPs;
00f254e2 3705 STRLEN slen; /* slen is the byte length of the whole SV. */
d54190f6
NC
3706 STRLEN need;
3707 SV *dest;
00f254e2
KW
3708 bool inplace; /* ? Convert first char only, in-place */
3709 bool doing_utf8 = FALSE; /* ? using utf8 */
3710 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
12e9c124 3711 const int op_type = PL_op->op_type;
d54190f6
NC
3712 const U8 *s;
3713 U8 *d;
3714 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
00f254e2
KW
3715 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3716 * stored as UTF-8 at s. */
3717 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3718 * lowercased) character stored in tmpbuf. May be either
3719 * UTF-8 or not, but in either case is the number of bytes */
d54190f6 3720
841a5e18 3721 s = (const U8*)SvPV_const(source, slen);
a0ed51b3 3722
00f254e2
KW
3723 /* We may be able to get away with changing only the first character, in
3724 * place, but not if read-only, etc. Later we may discover more reasons to
3725 * not convert in-place. */
1921e031 3726 inplace = !SvREADONLY(source) && SvPADTMP(source);
00f254e2
KW
3727
3728 /* First calculate what the changed first character should be. This affects
3729 * whether we can just swap it out, leaving the rest of the string unchanged,
3730 * or even if have to convert the dest to UTF-8 when the source isn't */
3731
3732 if (! slen) { /* If empty */
3733 need = 1; /* still need a trailing NUL */
b7576bcb 3734 ulen = 0;
00f254e2
KW
3735 }
3736 else if (DO_UTF8(source)) { /* Is the source utf8? */
d54190f6 3737 doing_utf8 = TRUE;
17e95c9d 3738 ulen = UTF8SKIP(s);
094a2f8c 3739 if (op_type == OP_UCFIRST) {
130c5df3 3740#ifdef USE_LOCALE_CTYPE
a1a5ec35 3741 _toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
130c5df3 3742#else
a1a5ec35 3743 _toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, 0);
130c5df3 3744#endif
094a2f8c
KW
3745 }
3746 else {
130c5df3 3747#ifdef USE_LOCALE_CTYPE
a1a5ec35 3748 _toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
130c5df3 3749#else
a1a5ec35 3750 _toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, 0);
130c5df3 3751#endif
094a2f8c 3752 }
00f254e2 3753
17e95c9d
KW
3754 /* we can't do in-place if the length changes. */
3755 if (ulen != tculen) inplace = FALSE;
3756 need = slen + 1 - ulen + tculen;
d54190f6 3757 }
00f254e2
KW
3758 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3759 * latin1 is treated as caseless. Note that a locale takes
3760 * precedence */
167d19f2 3761 ulen = 1; /* Original character is 1 byte */
00f254e2
KW
3762 tculen = 1; /* Most characters will require one byte, but this will
3763 * need to be overridden for the tricky ones */
3764 need = slen + 1;
3765
3766 if (op_type == OP_LCFIRST) {
d54190f6 3767
00f254e2 3768 /* lower case the first letter: no trickiness for any character */
130c5df3 3769#ifdef USE_LOCALE_CTYPE
780fcc9f
KW
3770 if (IN_LC_RUNTIME(LC_CTYPE)) {
3771 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
3772 *tmpbuf = toLOWER_LC(*s);
3773 }
3774 else
130c5df3 3775#endif
780fcc9f
KW
3776 {
3777 *tmpbuf = (IN_UNI_8_BIT)
3778 ? toLOWER_LATIN1(*s)
3779 : toLOWER(*s);
3780 }
00f254e2 3781 }
130c5df3 3782#ifdef USE_LOCALE_CTYPE
780fcc9f 3783 /* is ucfirst() */
d6ded950 3784 else if (IN_LC_RUNTIME(LC_CTYPE)) {
31f05a37
KW
3785 if (IN_UTF8_CTYPE_LOCALE) {
3786 goto do_uni_rules;
3787 }
3788
780fcc9f 3789 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
31f05a37
KW
3790 *tmpbuf = (U8) toUPPER_LC(*s); /* This would be a bug if any
3791 locales have upper and title case
3792 different */
00f254e2 3793 }
130c5df3 3794#endif
00f254e2
KW
3795 else if (! IN_UNI_8_BIT) {
3796 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3797 * on EBCDIC machines whatever the
3798 * native function does */
3799 }
31f05a37
KW
3800 else {
3801 /* Here, is ucfirst non-UTF-8, not in locale (unless that locale is
3802 * UTF-8, which we treat as not in locale), and cased latin1 */
3803 UV title_ord;
91191cf7 3804#ifdef USE_LOCALE_CTYPE
31f05a37 3805 do_uni_rules:
91191cf7 3806#endif
31f05a37
KW
3807
3808 title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
167d19f2
KW
3809 if (tculen > 1) {
3810 assert(tculen == 2);
3811
3812 /* If the result is an upper Latin1-range character, it can
3813 * still be represented in one byte, which is its ordinal */
3814 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3815 *tmpbuf = (U8) title_ord;
3816 tculen = 1;
00f254e2
KW
3817 }
3818 else {
167d19f2
KW
3819 /* Otherwise it became more than one ASCII character (in
3820 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3821 * beyond Latin1, so the number of bytes changed, so can't
3822 * replace just the first character in place. */
3823 inplace = FALSE;
3824
d14578b8
KW
3825 /* If the result won't fit in a byte, the entire result
3826 * will have to be in UTF-8. Assume worst case sizing in
3827 * conversion. (all latin1 characters occupy at most two
3828 * bytes in utf8) */
167d19f2
KW
3829 if (title_ord > 255) {
3830 doing_utf8 = TRUE;
3831 convert_source_to_utf8 = TRUE;
3832 need = slen * 2 + 1;
3833
3834 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3835 * (both) characters whose title case is above 255 is
3836 * 2. */
3837 ulen = 2;
3838 }
3839 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3840 need = slen + 1 + 1;
3841 }
00f254e2 3842 }
167d19f2 3843 }
00f254e2
KW
3844 } /* End of use Unicode (Latin1) semantics */
3845 } /* End of changing the case of the first character */
3846
3847 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3848 * generate the result */
3849 if (inplace) {
3850
3851 /* We can convert in place. This means we change just the first
3852 * character without disturbing the rest; no need to grow */
d54190f6
NC
3853 dest = source;
3854 s = d = (U8*)SvPV_force_nomg(source, slen);
3855 } else {
3856 dTARGET;
3857
3858 dest = TARG;
3859
00f254e2
KW
3860 /* Here, we can't convert in place; we earlier calculated how much
3861 * space we will need, so grow to accommodate that */
d54190f6 3862 SvUPGRADE(dest, SVt_PV);
3b416f41 3863 d = (U8*)SvGROW(dest, need);
d54190f6
NC
3864 (void)SvPOK_only(dest);
3865
3866 SETs(dest);
d54190f6 3867 }
44bc797b 3868
d54190f6 3869 if (doing_utf8) {
00f254e2
KW
3870 if (! inplace) {
3871 if (! convert_source_to_utf8) {
3872
3873 /* Here both source and dest are in UTF-8, but have to create
3874 * the entire output. We initialize the result to be the
3875 * title/lower cased first character, and then append the rest
3876 * of the string. */
3877 sv_setpvn(dest, (char*)tmpbuf, tculen);
3878 if (slen > ulen) {
3879 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3880 }
3881 }
3882 else {
3883 const U8 *const send = s + slen;
3884
3885 /* Here the dest needs to be in UTF-8, but the source isn't,
3886 * except we earlier UTF-8'd the first character of the source
3887 * into tmpbuf. First put that into dest, and then append the
3888 * rest of the source, converting it to UTF-8 as we go. */
3889
3890 /* Assert tculen is 2 here because the only two characters that
3891 * get to this part of the code have 2-byte UTF-8 equivalents */
3892 *d++ = *tmpbuf;
3893 *d++ = *(tmpbuf + 1);
3894 s++; /* We have just processed the 1st char */
3895
3896 for (; s < send; s++) {
3897 d = uvchr_to_utf8(d, *s);
3898 }
3899 *d = '\0';
3900 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3901 }
d54190f6 3902 SvUTF8_on(dest);
a0ed51b3 3903 }
00f254e2 3904 else { /* in-place UTF-8. Just overwrite the first character */
d54190f6
NC
3905 Copy(tmpbuf, d, tculen, U8);
3906 SvCUR_set(dest, need - 1);
a0ed51b3 3907 }
094a2f8c 3908
a0ed51b3 3909 }
00f254e2
KW
3910 else { /* Neither source nor dest are in or need to be UTF-8 */
3911 if (slen) {
00f254e2
KW
3912 if (inplace) { /* in-place, only need to change the 1st char */
3913 *d = *tmpbuf;
3914 }
3915 else { /* Not in-place */
3916
3917 /* Copy the case-changed character(s) from tmpbuf */
3918 Copy(tmpbuf, d, tculen, U8);
3919 d += tculen - 1; /* Code below expects d to point to final
3920 * character stored */
3921 }
3922 }
3923 else { /* empty source */
3924 /* See bug #39028: Don't taint if empty */
d54190f6
NC
3925 *d = *s;
3926 }
3927
00f254e2
KW
3928 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3929 * the destination to retain that flag */
93e088e8 3930 if (SvUTF8(source) && ! IN_BYTES)
d54190f6
NC
3931 SvUTF8_on(dest);
3932
00f254e2 3933 if (!inplace) { /* Finish the rest of the string, unchanged */
d54190f6
NC
3934 /* This will copy the trailing NUL */
3935 Copy(s + 1, d + 1, slen, U8);
3936 SvCUR_set(dest, need - 1);
bbce6d69 3937 }
bbce6d69 3938 }
130c5df3 3939#ifdef USE_LOCALE_CTYPE
d6ded950 3940 if (IN_LC_RUNTIME(LC_CTYPE)) {
445bf929
KW
3941 TAINT;
3942 SvTAINTED_on(dest);
3943 }
130c5df3 3944#endif
539689e7
FC
3945 if (dest != source && SvTAINTED(source))
3946 SvTAINT(dest);
d54190f6 3947 SvSETMAGIC(dest);
3cb4e04f 3948 return NORMAL;
79072805
LW
3949}
3950
67306194
NC
3951/* There's so much setup/teardown code common between uc and lc, I wonder if
3952 it would be worth merging the two, and just having a switch outside each
00f254e2 3953 of the three tight loops. There is less and less commonality though */
79072805
LW
3954PP(pp_uc)
3955{
39644a26 3956 dSP;
67306194 3957 SV *source = TOPs;
463ee0b2 3958 STRLEN len;
67306194
NC
3959 STRLEN min;
3960 SV *dest;
3961 const U8 *s;
3962 U8 *d;
79072805 3963
67306194
NC
3964 SvGETMAGIC(source);
3965
1921e031 3966 if ( SvPADTMP(source)
5cd5e2d6
FC
3967 && !SvREADONLY(source) && SvPOK(source)
3968 && !DO_UTF8(source)
130c5df3
KW
3969 && (
3970#ifdef USE_LOCALE_CTYPE
3971 (IN_LC_RUNTIME(LC_CTYPE))
31f05a37 3972 ? ! IN_UTF8_CTYPE_LOCALE
130c5df3
KW
3973 :
3974#endif
3975 ! IN_UNI_8_BIT))
31f05a37
KW
3976 {
3977
3978 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3979 * make the loop tight, so we overwrite the source with the dest before
3980 * looking at it, and we need to look at the original source
3981 * afterwards. There would also need to be code added to handle
3982 * switching to not in-place in midstream if we run into characters
3983 * that change the length. Since being in locale overrides UNI_8_BIT,
3984 * that latter becomes irrelevant in the above test; instead for
3985 * locale, the size can't normally change, except if the locale is a
3986 * UTF-8 one */
67306194
NC
3987 dest = source;
3988 s = d = (U8*)SvPV_force_nomg(source, len);
3989 min = len + 1;
3990 } else {
a0ed51b3 3991 dTARGET;
a0ed51b3 3992
67306194 3993 dest = TARG;
128c9517 3994
841a5e18 3995 s = (const U8*)SvPV_nomg_const(source, len);
67306194
NC
3996 min = len + 1;
3997
3998 SvUPGRADE(dest, SVt_PV);
3b416f41 3999 d = (U8*)SvGROW(dest, min);
67306194
NC
4000 (void)SvPOK_only(dest);
4001
4002 SETs(dest);
a0ed51b3 4003 }
31351b04 4004
67306194
NC
4005 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4006 to check DO_UTF8 again here. */
4007
4008 if (DO_UTF8(source)) {
4009 const U8 *const send = s + len;
bfac13d4 4010 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
67306194 4011
4c8a458a
KW
4012 /* All occurrences of these are to be moved to follow any other marks.
4013 * This is context-dependent. We may not be passed enough context to
4014 * move the iota subscript beyond all of them, but we do the best we can
4015 * with what we're given. The result is always better than if we
4016 * hadn't done this. And, the problem would only arise if we are
4017 * passed a character without all its combining marks, which would be
4018 * the caller's mistake. The information this is based on comes from a
4019 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4020 * itself) and so can't be checked properly to see if it ever gets
4021 * revised. But the likelihood of it changing is remote */
00f254e2 4022 bool in_iota_subscript = FALSE;
00f254e2 4023
67306194 4024 while (s < send) {
3e16b0e6
KW
4025 STRLEN u;
4026 STRLEN ulen;
4027 UV uv;
7dbf68d2 4028 if (in_iota_subscript && ! _is_utf8_mark(s)) {
3e16b0e6 4029
00f254e2 4030 /* A non-mark. Time to output the iota subscript */
a78bc3c6
KW
4031 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
4032 d += capital_iota_len;
00f254e2 4033 in_iota_subscript = FALSE;
8e058693 4034 }
00f254e2 4035
8e058693
KW
4036 /* Then handle the current character. Get the changed case value
4037 * and copy it to the output buffer */
00f254e2 4038
8e058693 4039 u = UTF8SKIP(s);
130c5df3 4040#ifdef USE_LOCALE_CTYPE
a1a5ec35 4041 uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
130c5df3 4042#else
a1a5ec35 4043 uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, 0);
130c5df3 4044#endif
a78bc3c6
KW
4045#define GREEK_CAPITAL_LETTER_IOTA 0x0399
4046#define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
8e058693 4047 if (uv == GREEK_CAPITAL_LETTER_IOTA
4b88fb76 4048 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
8e058693
KW
4049 {
4050 in_iota_subscript = TRUE;
4051 }
4052 else {
4053 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4054 /* If the eventually required minimum size outgrows the
4055 * available space, we need to grow. */
4056 const UV o = d - (U8*)SvPVX_const(dest);
4057
4058 /* If someone uppercases one million U+03B0s we SvGROW()
4059 * one million times. Or we could try guessing how much to
4060 * allocate without allocating too much. Such is life.
4061 * See corresponding comment in lc code for another option
4062 * */
10656159 4063 d = o + (U8*) SvGROW(dest, min);
8e058693
KW
4064 }
4065 Copy(tmpbuf, d, ulen, U8);
4066 d += ulen;
4067 }
4068 s += u;
67306194 4069 }
4c8a458a 4070 if (in_iota_subscript) {
a78bc3c6
KW
4071 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
4072 d += capital_iota_len;
4c8a458a 4073 }
67306194
NC
4074 SvUTF8_on(dest);
4075 *d = '\0';
094a2f8c 4076
67306194 4077 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4c8a458a
KW
4078 }
4079 else { /* Not UTF-8 */
67306194
NC
4080 if (len) {
4081 const U8 *const send = s + len;
00f254e2
KW
4082
4083 /* Use locale casing if in locale; regular style if not treating
4084 * latin1 as having case; otherwise the latin1 casing. Do the
4085 * whole thing in a tight loop, for speed, */
130c5df3 4086#ifdef USE_LOCALE_CTYPE
d6ded950 4087 if (IN_LC_RUNTIME(LC_CTYPE)) {
31f05a37
KW
4088 if (IN_UTF8_CTYPE_LOCALE) {
4089 goto do_uni_rules;
4090 }
780fcc9f 4091 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
67306194 4092 for (; s < send; d++, s++)
31f05a37 4093 *d = (U8) toUPPER_LC(*s);
31351b04 4094 }
130c5df3
KW
4095 else
4096#endif
4097 if (! IN_UNI_8_BIT) {
00f254e2 4098 for (; s < send; d++, s++) {
67306194 4099 *d = toUPPER(*s);
00f254e2 4100 }
31351b04 4101 }
00f254e2 4102 else {
91191cf7 4103#ifdef USE_LOCALE_CTYPE
31f05a37 4104 do_uni_rules:
91191cf7 4105#endif
00f254e2
KW
4106 for (; s < send; d++, s++) {
4107 *d = toUPPER_LATIN1_MOD(*s);
d14578b8
KW
4108 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
4109 continue;
4110 }
00f254e2
KW
4111
4112 /* The mainstream case is the tight loop above. To avoid
4113 * extra tests in that, all three characters that require
4114 * special handling are mapped by the MOD to the one tested
4115 * just above.
4116 * Use the source to distinguish between the three cases */
4117
79e064b9
KW
4118#if UNICODE_MAJOR_VERSION > 2 \
4119 || (UNICODE_MAJOR_VERSION == 2 && UNICODE_DOT_VERSION >= 1 \
4120 && UNICODE_DOT_DOT_VERSION >= 8)
00f254e2
KW
4121 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4122
4123 /* uc() of this requires 2 characters, but they are
4124 * ASCII. If not enough room, grow the string */
4125 if (SvLEN(dest) < ++min) {
4126 const UV o = d - (U8*)SvPVX_const(dest);
10656159 4127 d = o + (U8*) SvGROW(dest, min);
00f254e2
KW
4128 }
4129 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4130 continue; /* Back to the tight loop; still in ASCII */
4131 }
79e064b9 4132#endif
00f254e2
KW
4133
4134 /* The other two special handling characters have their
4135 * upper cases outside the latin1 range, hence need to be
4136 * in UTF-8, so the whole result needs to be in UTF-8. So,
4137 * here we are somewhere in the middle of processing a
4138 * non-UTF-8 string, and realize that we will have to convert
4139 * the whole thing to UTF-8. What to do? There are
4140 * several possibilities. The simplest to code is to
4141 * convert what we have so far, set a flag, and continue on
4142 * in the loop. The flag would be tested each time through
4143 * the loop, and if set, the next character would be
4144 * converted to UTF-8 and stored. But, I (khw) didn't want
4145 * to slow down the mainstream case at all for this fairly
4146 * rare case, so I didn't want to add a test that didn't
4147 * absolutely have to be there in the loop, besides the
4148 * possibility that it would get too complicated for
4149 * optimizers to deal with. Another possibility is to just
4150 * give up, convert the source to UTF-8, and restart the
4151 * function that way. Another possibility is to convert
4152 * both what has already been processed and what is yet to
4153 * come separately to UTF-8, then jump into the loop that
4154 * handles UTF-8. But the most efficient time-wise of the
4155 * ones I could think of is what follows, and turned out to
4156 * not require much extra code. */
4157
4158 /* Convert what we have so far into UTF-8, telling the
4159 * function that we know it should be converted, and to
4160 * allow extra space for what we haven't processed yet.
4161 * Assume the worst case space requirements for converting
4162 * what we haven't processed so far: that it will require
4163 * two bytes for each remaining source character, plus the
4164 * NUL at the end. This may cause the string pointer to
4165 * move, so re-find it. */
4166
4167 len = d - (U8*)SvPVX_const(dest);
4168 SvCUR_set(dest, len);
4169 len = sv_utf8_upgrade_flags_grow(dest,
4170 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4171 (send -s) * 2 + 1);
4172 d = (U8*)SvPVX(dest) + len;
4173
00f254e2
KW
4174 /* Now process the remainder of the source, converting to
4175 * upper and UTF-8. If a resulting byte is invariant in
4176 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4177 * append it to the output. */
00f254e2 4178 for (; s < send; s++) {
0ecfbd28
KW
4179 (void) _to_upper_title_latin1(*s, d, &len, 'S');
4180 d += len;
00f254e2
KW
4181 }
4182
4183 /* Here have processed the whole source; no need to continue
4184 * with the outer loop. Each character has been converted
4185 * to upper case and converted to UTF-8 */
4186
4187 break;
4188 } /* End of processing all latin1-style chars */
4189 } /* End of processing all chars */
4190 } /* End of source is not empty */
4191
67306194 4192 if (source != dest) {
00f254e2 4193 *d = '\0'; /* Here d points to 1 after last char, add NUL */
67306194
NC
4194 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4195 }
00f254e2 4196 } /* End of isn't utf8 */
130c5df3 4197#ifdef USE_LOCALE_CTYPE
d6ded950 4198 if (IN_LC_RUNTIME(LC_CTYPE)) {
445bf929
KW
4199 TAINT;
4200 SvTAINTED_on(dest);
4201 }
130c5df3 4202#endif
539689e7
FC
4203 if (dest != source && SvTAINTED(source))
4204 SvTAINT(dest);
67306194 4205 SvSETMAGIC(dest);
3cb4e04f 4206 return NORMAL;
79072805
LW
4207}
4208
4209PP(pp_lc)
4210{
39644a26 4211 dSP;
ec9af7d4 4212 SV *source = TOPs;
463ee0b2 4213 STRLEN len;
ec9af7d4
NC
4214 STRLEN min;
4215 SV *dest;
4216 const U8 *s;
4217 U8 *d;
79072805 4218
ec9af7d4
NC
4219 SvGETMAGIC(source);
4220
1921e031 4221 if ( SvPADTMP(source)
5cd5e2d6
FC
4222 && !SvREADONLY(source) && SvPOK(source)
4223 && !DO_UTF8(source)) {
ec9af7d4 4224
00f254e2
KW
4225 /* We can convert in place, as lowercasing anything in the latin1 range
4226 * (or else DO_UTF8 would have been on) doesn't lengthen it */
ec9af7d4
NC
4227 dest = source;
4228 s = d = (U8*)SvPV_force_nomg(source, len);
4229 min = len + 1;
4230 } else {
a0ed51b3 4231 dTARGET;
a0ed51b3 4232
ec9af7d4
NC
4233 dest = TARG;
4234
841a5e18 4235 s = (const U8*)SvPV_nomg_const(source, len);
ec9af7d4 4236 min = len + 1;
128c9517 4237
ec9af7d4 4238 SvUPGRADE(dest, SVt_PV);
3b416f41 4239 d = (U8*)SvGROW(dest, min);
ec9af7d4
NC
4240 (void)SvPOK_only(dest);
4241
4242 SETs(dest);
4243 }
4244
4245 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4246 to check DO_UTF8 again here. */
4247
4248 if (DO_UTF8(source)) {
4249 const U8 *const send = s + len;
4250 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4251
4252 while (s < send) {
06b5486a
KW
4253 const STRLEN u = UTF8SKIP(s);
4254 STRLEN ulen;
00f254e2 4255
130c5df3 4256#ifdef USE_LOCALE_CTYPE
a1a5ec35 4257 _toLOWER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
130c5df3 4258#else
a1a5ec35 4259 _toLOWER_utf8_flags(s, send, tmpbuf, &ulen, 0);
130c5df3 4260#endif
00f254e2 4261
06b5486a 4262 /* Here is where we would do context-sensitive actions. See the
6006ebd0 4263 * commit message for 86510fb15 for why there isn't any */
00f254e2 4264
06b5486a 4265 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
fdb34c52 4266
06b5486a
KW
4267 /* If the eventually required minimum size outgrows the
4268 * available space, we need to grow. */
4269 const UV o = d - (U8*)SvPVX_const(dest);
fdb34c52 4270
06b5486a
KW
4271 /* If someone lowercases one million U+0130s we SvGROW() one
4272 * million times. Or we could try guessing how much to
4273 * allocate without allocating too much. Such is life.
4274 * Another option would be to grow an extra byte or two more
4275 * each time we need to grow, which would cut down the million
4276 * to 500K, with little waste */
10656159 4277 d = o + (U8*) SvGROW(dest, min);
06b5486a 4278 }
86510fb1 4279
06b5486a
KW
4280 /* Copy the newly lowercased letter to the output buffer we're
4281 * building */
4282 Copy(tmpbuf, d, ulen, U8);
4283 d += ulen;
4284 s += u;
00f254e2 4285 } /* End of looping through the source string */
ec9af7d4
NC
4286 SvUTF8_on(dest);
4287 *d = '\0';
4288 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
00f254e2 4289 } else { /* Not utf8 */
31351b04 4290 if (len) {
ec9af7d4 4291 const U8 *const send = s + len;
00f254e2
KW
4292
4293 /* Use locale casing if in locale; regular style if not treating
4294 * latin1 as having case; otherwise the latin1 casing. Do the
4295 * whole thing in a tight loop, for speed, */
130c5df3 4296#ifdef USE_LOCALE_CTYPE
d6ded950 4297 if (IN_LC_RUNTIME(LC_CTYPE)) {
780fcc9f 4298 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
ec9af7d4
NC
4299 for (; s < send; d++, s++)
4300 *d = toLOWER_LC(*s);
445bf929 4301 }
130c5df3
KW
4302 else
4303#endif
4304 if (! IN_UNI_8_BIT) {
00f254e2 4305 for (; s < send; d++, s++) {
ec9af7d4 4306 *d = toLOWER(*s);
00f254e2
KW
4307 }
4308 }
4309 else {
4310 for (; s < send; d++, s++) {
4311 *d = toLOWER_LATIN1(*s);
4312 }
31351b04 4313 }
bbce6d69 4314 }
ec9af7d4
NC
4315 if (source != dest) {
4316 *d = '\0';
4317 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4318 }
79072805 4319 }
130c5df3 4320#ifdef USE_LOCALE_CTYPE
d6ded950 4321 if (IN_LC_RUNTIME(LC_CTYPE)) {
445bf929
KW
4322 TAINT;
4323 SvTAINTED_on(dest);
4324 }
130c5df3 4325#endif
539689e7
FC
4326 if (dest != source && SvTAINTED(source))
4327 SvTAINT(dest);
ec9af7d4 4328 SvSETMAGIC(dest);
3cb4e04f 4329 return NORMAL;
79072805
LW
4330}
4331
a0d0e21e 4332PP(pp_quotemeta)
79072805 4333{
20b7effb 4334 dSP; dTARGET;
1b6737cc 4335 SV * const sv = TOPs;
a0d0e21e 4336 STRLEN len;
eb578fdb 4337 const char *s = SvPV_const(sv,len);
79072805 4338
7e2040f0 4339 SvUTF8_off(TARG); /* decontaminate */
a0d0e21e 4340 if (len) {
eb578fdb 4341 char *d;
862a34c6 4342 SvUPGRADE(TARG, SVt_PV);
c07a80fd 4343 SvGROW(TARG, (len * 2) + 1);
a0d0e21e 4344 d = SvPVX(TARG);
7e2040f0 4345 if (DO_UTF8(sv)) {
0dd2cdef 4346 while (len) {
29050de5 4347 STRLEN ulen = UTF8SKIP(s);
2e2b2571
KW
4348 bool to_quote = FALSE;
4349
4350 if (UTF8_IS_INVARIANT(*s)) {
4351 if (_isQUOTEMETA(*s)) {
4352 to_quote = TRUE;
4353 }
4354 }
042d9e50 4355 else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, s + len)) {
3fea7d29 4356 if (
130c5df3 4357#ifdef USE_LOCALE_CTYPE
20adcf7c
KW
4358 /* In locale, we quote all non-ASCII Latin1 chars.
4359 * Otherwise use the quoting rules */
3fea7d29
BF
4360
4361 IN_LC_RUNTIME(LC_CTYPE)
4362 ||
4363#endif
a62b247b 4364 _isQUOTEMETA(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s + 1))))
2e2b2571
KW
4365 {
4366 to_quote = TRUE;
4367 }
4368 }
685289b5 4369 else if (is_QUOTEMETA_high(s)) {
2e2b2571
KW
4370 to_quote = TRUE;
4371 }
4372
4373 if (to_quote) {
4374 *d++ = '\\';
4375 }
29050de5
KW
4376 if (ulen > len)
4377 ulen = len;
4378 len -= ulen;
4379 while (ulen--)
4380 *d++ = *s++;
0dd2cdef 4381 }
7e2040f0 4382 SvUTF8_on(TARG);
0dd2cdef 4383 }
2e2b2571
KW
4384 else if (IN_UNI_8_BIT) {
4385 while (len--) {
4386 if (_isQUOTEMETA(*s))
4387 *d++ = '\\';
4388 *d++ = *s++;
4389 }
4390 }
0dd2cdef 4391 else {
2e2b2571
KW
4392 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4393 * including everything above ASCII */
0dd2cdef 4394 while (len--) {
adfec831 4395 if (!isWORDCHAR_A(*s))
0dd2cdef
LW
4396 *d++ = '\\';
4397 *d++ = *s++;
4398 }
79072805 4399 }
a0d0e21e 4400 *d = '\0';
349d4f2f 4401 SvCUR_set(TARG, d - SvPVX_const(TARG));
3aa33fe5 4402 (void)SvPOK_only_UTF8(TARG);
79072805 4403 }
a0d0e21e
LW
4404 else
4405 sv_setpvn(TARG, s, len);
ec93b65f 4406 SETTARG;
cfe40115 4407 return NORMAL;
79072805
LW
4408}
4409
838f2281
BF
4410PP(pp_fc)
4411{
838f2281
BF
4412 dTARGET;
4413 dSP;
4414 SV *source = TOPs;
4415 STRLEN len;
4416 STRLEN min;
4417 SV *dest;
4418 const U8 *s;
4419 const U8 *send;
4420 U8 *d;
bfac13d4 4421 U8 tmpbuf[UTF8_MAXBYTES_CASE + 1];
9b63e895
KW
4422#if UNICODE_MAJOR_VERSION > 3 /* no multifolds in early Unicode */ \
4423 || (UNICODE_MAJOR_VERSION == 3 && ( UNICODE_DOT_VERSION > 0) \
4424 || UNICODE_DOT_DOT_VERSION > 0)
a4b69695
KW
4425 const bool full_folding = TRUE; /* This variable is here so we can easily
4426 move to more generality later */
9b63e895
KW
4427#else
4428 const bool full_folding = FALSE;
4429#endif
838f2281 4430 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
130c5df3
KW
4431#ifdef USE_LOCALE_CTYPE
4432 | ( IN_LC_RUNTIME(LC_CTYPE) ? FOLD_FLAGS_LOCALE : 0 )
4433#endif
4434 ;
838f2281
BF
4435
4436 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4437 * You are welcome(?) -Hugmeir
4438 */
4439
4440 SvGETMAGIC(source);
4441
4442 dest = TARG;
4443
4444 if (SvOK(source)) {
4445 s = (const U8*)SvPV_nomg_const(source, len);
4446 } else {
4447 if (ckWARN(WARN_UNINITIALIZED))
4448 report_uninit(source);
4449 s = (const U8*)"";
4450 len = 0;
4451 }
4452
4453 min = len + 1;
4454
4455 SvUPGRADE(dest, SVt_PV);
4456 d = (U8*)SvGROW(dest, min);
4457 (void)SvPOK_only(dest);
4458
4459 SETs(dest);
4460
4461 send = s + len;
4462 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
838f2281
BF
4463 while (s < send) {
4464 const STRLEN u = UTF8SKIP(s);
4465 STRLEN ulen;
4466
a1a5ec35 4467 _toFOLD_utf8_flags(s, send, tmpbuf, &ulen, flags);
838f2281
BF
4468
4469 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4470 const UV o = d - (U8*)SvPVX_const(dest);
10656159 4471 d = o + (U8*) SvGROW(dest, min);
838f2281
BF
4472 }
4473
4474 Copy(tmpbuf, d, ulen, U8);
4475 d += ulen;
4476 s += u;
4477 }
4478 SvUTF8_on(dest);
838f2281 4479 } /* Unflagged string */
0902dd32 4480 else if (len) {
130c5df3 4481#ifdef USE_LOCALE_CTYPE
d6ded950 4482 if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
31f05a37
KW
4483 if (IN_UTF8_CTYPE_LOCALE) {
4484 goto do_uni_folding;
4485 }
780fcc9f 4486 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
838f2281 4487 for (; s < send; d++, s++)
ea36a843 4488 *d = (U8) toFOLD_LC(*s);
838f2281 4489 }
130c5df3
KW
4490 else
4491#endif
4492 if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
838f2281 4493 for (; s < send; d++, s++)
d22b930b 4494 *d = toFOLD(*s);
838f2281
BF
4495 }
4496 else {
91191cf7 4497#ifdef USE_LOCALE_CTYPE
31f05a37 4498 do_uni_folding:
91191cf7 4499#endif
d14578b8
KW
4500 /* For ASCII and the Latin-1 range, there's only two troublesome
4501 * folds, \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
22e255cb 4502 * casefolding becomes 'ss'; and \x{B5} (\N{MICRO SIGN}), which
d14578b8
KW
4503 * under any fold becomes \x{3BC} (\N{GREEK SMALL LETTER MU}) --
4504 * For the rest, the casefold is their lowercase. */
838f2281
BF
4505 for (; s < send; d++, s++) {
4506 if (*s == MICRO_SIGN) {
d14578b8
KW
4507 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
4508 * which is outside of the latin-1 range. There's a couple
4509 * of ways to deal with this -- khw discusses them in
4510 * pp_lc/uc, so go there :) What we do here is upgrade what
4511 * we had already casefolded, then enter an inner loop that
4512 * appends the rest of the characters as UTF-8. */
838f2281
BF
4513 len = d - (U8*)SvPVX_const(dest);
4514 SvCUR_set(dest, len);
4515 len = sv_utf8_upgrade_flags_grow(dest,
4516 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
ea4d335b
KW
4517 /* The max expansion for latin1
4518 * chars is 1 byte becomes 2 */
4519 (send -s) * 2 + 1);
838f2281
BF
4520 d = (U8*)SvPVX(dest) + len;
4521
a78bc3c6
KW
4522 Copy(GREEK_SMALL_LETTER_MU_UTF8, d, small_mu_len, U8);
4523 d += small_mu_len;
838f2281
BF
4524 s++;
4525 for (; s < send; s++) {
4526 STRLEN ulen;
4527 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
6f2d5cbc 4528 if UVCHR_IS_INVARIANT(fc) {
d14578b8
KW
4529 if (full_folding
4530 && *s == LATIN_SMALL_LETTER_SHARP_S)
4531 {
838f2281
BF
4532 *d++ = 's';
4533 *d++ = 's';
4534 }
4535 else
4536 *d++ = (U8)fc;
4537 }
4538 else {
4539 Copy(tmpbuf, d, ulen, U8);
4540 d += ulen;
4541 }
4542 }
4543 break;
4544 }
4545 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
d14578b8
KW
4546 /* Under full casefolding, LATIN SMALL LETTER SHARP S
4547 * becomes "ss", which may require growing the SV. */
838f2281
BF
4548 if (SvLEN(dest) < ++min) {
4549 const UV o = d - (U8*)SvPVX_const(dest);
10656159 4550 d = o + (U8*) SvGROW(dest, min);
838f2281
BF
4551 }
4552 *(d)++ = 's';
4553 *d = 's';
4554 }
d14578b8
KW
4555 else { /* If it's not one of those two, the fold is their lower
4556 case */
838f2281
BF
4557 *d = toLOWER_LATIN1(*s);
4558 }
4559 }
4560 }
4561 }
4562 *d = '\0';
4563 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4564
130c5df3 4565#ifdef USE_LOCALE_CTYPE
d6ded950 4566 if (IN_LC_RUNTIME(LC_CTYPE)) {
445bf929
KW
4567 TAINT;
4568 SvTAINTED_on(dest);
4569 }
130c5df3 4570#endif
838f2281
BF
4571 if (SvTAINTED(source))
4572 SvTAINT(dest);
4573 SvSETMAGIC(dest);
4574 RETURN;
4575}
4576
a0d0e21e 4577/* Arrays. */
79072805 4578
a0d0e21e 4579PP(pp_aslice)
79072805 4580{
20b7effb 4581 dSP; dMARK; dORIGMARK;
eb578fdb
KW
4582 AV *const av = MUTABLE_AV(POPs);
4583 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
79072805 4584
a0d0e21e 4585 if (SvTYPE(av) == SVt_PVAV) {
4ad10a0b
VP
4586 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4587 bool can_preserve = FALSE;
4588
4589 if (localizing) {
4590 MAGIC *mg;
4591 HV *stash;
4592
4593 can_preserve = SvCANEXISTDELETE(av);
4594 }
4595
4596 if (lval && localizing) {
eb578fdb 4597 SV **svp;
c70927a6 4598 SSize_t max = -1;
924508f0 4599 for (svp = MARK + 1; svp <= SP; svp++) {
c70927a6 4600 const SSize_t elem = SvIV(*svp);
748a9306
LW
4601 if (elem > max)
4602 max = elem;
4603 }
4604 if (max > AvMAX(av))
4605 av_extend(av, max);
4606 }
4ad10a0b 4607
a0d0e21e 4608 while (++MARK <= SP) {
eb578fdb 4609 SV **svp;
c70927a6 4610 SSize_t elem = SvIV(*MARK);
4ad10a0b 4611 bool preeminent = TRUE;
a0d0e21e 4612
4ad10a0b
VP
4613 if (localizing && can_preserve) {
4614 /* If we can determine whether the element exist,
4615 * Try to preserve the existenceness of a tied array
4616 * element by using EXISTS and DELETE if possible.
4617 * Fallback to FETCH and STORE otherwise. */
4618 preeminent = av_exists(av, elem);
4619 }
4620
a0d0e21e
LW
4621 svp = av_fetch(av, elem, lval);
4622 if (lval) {
ce0d59fd 4623 if (!svp || !*svp)
cea2e8a9 4624 DIE(aTHX_ PL_no_aelem, elem);
4ad10a0b
VP
4625 if (localizing) {
4626 if (preeminent)
4627 save_aelem(av, elem, svp);
4628 else
4629 SAVEADELETE(av, elem);
4630 }
79072805 4631 }
3280af22 4632 *MARK = svp ? *svp : &PL_sv_undef;
79072805
LW
4633 }
4634 }
82334630 4635 if (GIMME_V != G_ARRAY) {
a0d0e21e 4636 MARK = ORIGMARK;
04ab2c87 4637 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
a0d0e21e
LW
4638 SP = MARK;
4639 }
79072805
LW
4640 RETURN;
4641}
4642
6dd3e0f2
RZ
4643PP(pp_kvaslice)
4644{
20b7effb 4645 dSP; dMARK;
6dd3e0f2
RZ
4646 AV *const av = MUTABLE_AV(POPs);
4647 I32 lval = (PL_op->op_flags & OPf_MOD);
adad97db 4648 SSize_t items = SP - MARK;
6dd3e0f2
RZ
4649
4650 if (PL_op->op_private & OPpMAYBE_LVSUB) {
4651 const I32 flags = is_lvalue_sub();
4652 if (flags) {
4653 if (!(flags & OPpENTERSUB_INARGS))
7aae0299 4654 /* diag_listed_as: Can't modify %s in %s */
6dd3e0f2
RZ
4655 Perl_croak(aTHX_ "Can't modify index/value array slice in list assignment");
4656 lval = flags;
4657 }
4658 }
4659
4660 MEXTEND(SP,items);
4661 while (items > 1) {
4662 *(MARK+items*2-1) = *(MARK+items);
4663 items--;
4664 }
4665 items = SP-MARK;
4666 SP += items;
4667
4668 while (++MARK <= SP) {
4669 SV **svp;
4670
4671 svp = av_fetch(av, SvIV(*MARK), lval);
4672 if (lval) {
4673 if (!svp || !*svp || *svp == &PL_sv_undef) {
4674 DIE(aTHX_ PL_no_aelem, SvIV(*MARK));
4675 }
4676 *MARK = sv_mortalcopy(*MARK);
4677 }
4678 *++MARK = svp ? *svp : &PL_sv_undef;
4679 }
82334630 4680 if (GIMME_V != G_ARRAY) {
6dd3e0f2
RZ
4681 MARK = SP - items*2;
4682 *++MARK = items > 0 ? *SP : &PL_sv_undef;
4683 SP = MARK;
4684 }
4685 RETURN;
4686}
4687
b1c05ba5 4688
878d132a
NC
4689PP(pp_aeach)
4690{
878d132a 4691 dSP;
502c6561 4692 AV *array = MUTABLE_AV(POPs);
1c23e2bd 4693 const U8 gimme = GIMME_V;
453d94a9 4694 IV *iterp = Perl_av_iter_p(aTHX_ array);
878d132a
NC
4695 const IV current = (*iterp)++;
4696
b9f2b683 4697 if (current > av_tindex(array)) {
878d132a
NC
4698 *iterp = 0;
4699 if (gimme == G_SCALAR)
4700 RETPUSHUNDEF;
4701 else
4702 RETURN;
4703 }
4704
4705 EXTEND(SP, 2);
e1dccc0d 4706 mPUSHi(current);
878d132a
NC
4707 if (gimme == G_ARRAY) {
4708 SV **const element = av_fetch(array, current, 0);
4709 PUSHs(element ? *element : &PL_sv_undef);
4710 }
4711 RETURN;
4712}
4713
b1c05ba5 4714/* also used for: pp_avalues()*/
878d132a
NC
4715PP(pp_akeys)
4716{
878d132a 4717 dSP;
502c6561 4718 AV *array = MUTABLE_AV(POPs);
1c23e2bd 4719 const U8 gimme = GIMME_V;
878d132a
NC
4720
4721 *Perl_av_iter_p(aTHX_ array) = 0;
4722
4723 if (gimme == G_SCALAR) {
4724 dTARGET;
b9f2b683 4725 PUSHi(av_tindex(array) + 1);
878d132a
NC
4726 }
4727 else if (gimme == G_ARRAY) {
738155d2
FC
4728 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
4729 const I32 flags = is_lvalue_sub();
4730 if (flags && !(flags & OPpENTERSUB_INARGS))
4731 /* diag_listed_as: Can't modify %s in %s */
4732 Perl_croak(aTHX_
4733 "Can't modify keys on array in list assignment");
4734 }
4735 {
878d132a 4736 IV n = Perl_av_len(aTHX_ array);
e1dccc0d 4737 IV i;
878d132a
NC
4738
4739 EXTEND(SP, n + 1);
4740
73665bc4
FC
4741 if ( PL_op->op_type == OP_AKEYS
4742 || ( PL_op->op_type == OP_AVHVSWITCH
cd642408 4743 && (PL_op->op_private & 3) + OP_AEACH == OP_AKEYS ))
73665bc4 4744 {
e1dccc0d 4745 for (i = 0; i <= n; i++) {
878d132a
NC
4746 mPUSHi(i);
4747 }
4748 }
4749 else {
4750 for (i = 0; i <= n; i++) {
4751 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4752 PUSHs(elem ? *elem : &PL_sv_undef);
4753 }
4754 }
738155d2 4755 }
878d132a
NC
4756 }
4757 RETURN;
4758}
4759
79072805
LW
4760/* Associative arrays. */
4761
4762PP(pp_each)
4763{
39644a26 4764 dSP;
85fbaab2 4765 HV * hash = MUTABLE_HV(POPs);
c07a80fd 4766 HE *entry;
1c23e2bd 4767 const U8 gimme = GIMME_V;
8ec5e241 4768
6d822dc4 4769 entry = hv_iternext(hash);
79072805 4770
79072805
LW
4771 EXTEND(SP, 2);
4772 if (entry) {
1b6737cc 4773 SV* const sv = hv_iterkeysv(entry);
2b32fed8 4774 PUSHs(sv);
54310121 4775 if (gimme == G_ARRAY) {
59af0135 4776 SV *val;
6d822dc4 4777 val = hv_iterval(hash, entry);
59af0135 4778 PUSHs(val);
79072805 4779 }
79072805 4780 }
54310121 4781 else if (gimme == G_SCALAR)
79072805
LW
4782 RETPUSHUNDEF;
4783
4784 RETURN;
4785}
4786
7332a6c4
VP
4787STATIC OP *
4788S_do_delete_local(pTHX)
79072805 4789{
39644a26 4790 dSP;
1c23e2bd 4791 const U8 gimme = GIMME_V;
7332a6c4
VP
4792 const MAGIC *mg;
4793 HV *stash;
ca3f996a 4794 const bool sliced = !!(PL_op->op_private & OPpSLICE);
626040f7 4795 SV **unsliced_keysv = sliced ? NULL : sp--;
ca3f996a 4796 SV * const osv = POPs;
626040f7 4797 SV **mark = sliced ? PL_stack_base + POPMARK : unsliced_keysv-1;
ca3f996a
FC
4798 dORIGMARK;
4799 const bool tied = SvRMAGICAL(osv)
7332a6c4 4800 && mg_find((const SV *)osv, PERL_MAGIC_tied);
ca3f996a
FC
4801 const bool can_preserve = SvCANEXISTDELETE(osv);
4802 const U32 type = SvTYPE(osv);
626040f7 4803 SV ** const end = sliced ? SP : unsliced_keysv;
ca3f996a
FC
4804
4805 if (type == SVt_PVHV) { /* hash element */
7332a6c4 4806 HV * const hv = MUTABLE_HV(osv);
ca3f996a 4807 while (++MARK <= end) {
7332a6c4
VP
4808 SV * const keysv = *MARK;
4809 SV *sv = NULL;
4810 bool preeminent = TRUE;
4811 if (can_preserve)
4812 preeminent = hv_exists_ent(hv, keysv, 0);
4813 if (tied) {
4814 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4815 if (he)
4816 sv = HeVAL(he);
4817 else
4818 preeminent = FALSE;
4819 }
4820 else {
4821 sv = hv_delete_ent(hv, keysv, 0, 0);
9332b95f
FC
4822 if (preeminent)
4823 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
7332a6c4
VP
4824 }
4825 if (preeminent) {
be6064fd 4826 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
7332a6c4
VP
4827 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4828 if (tied) {
4829 *MARK = sv_mortalcopy(sv);
4830 mg_clear(sv);
4831 } else
4832 *MARK = sv;
4833 }
4834 else {
4835 SAVEHDELETE(hv, keysv);
4836 *MARK = &PL_sv_undef;
4837 }
4838 }
ca3f996a
FC
4839 }
4840 else if (type == SVt_PVAV) { /* array element */
7332a6c4
VP
4841 if (PL_op->op_flags & OPf_SPECIAL) {
4842 AV * const av = MUTABLE_AV(osv);
ca3f996a 4843 while (++MARK <= end) {
c70927a6 4844 SSize_t idx = SvIV(*MARK);
7332a6c4
VP
4845 SV *sv = NULL;
4846 bool preeminent = TRUE;
4847 if (can_preserve)
4848 preeminent = av_exists(av, idx);
4849 if (tied) {
4850 SV **svp = av_fetch(av, idx, 1);
4851 if (svp)
4852 sv = *svp;
4853 else
4854 preeminent = FALSE;
4855 }
4856 else {
4857 sv = av_delete(av, idx, 0);
9332b95f
FC
4858 if (preeminent)
4859 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
7332a6c4
VP
4860 }
4861 if (preeminent) {
4862 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4863 if (tied) {
4864 *MARK = sv_mortalcopy(sv);
4865 mg_clear(sv);
4866 } else
4867 *MARK = sv;
4868 }
4869 else {
4870 SAVEADELETE(av, idx);
4871 *MARK = &PL_sv_undef;
4872 }
4873 }
4874 }
ca3f996a
FC
4875 else
4876 DIE(aTHX_ "panic: avhv_delete no longer supported");
4877 }
4878 else
7332a6c4 4879 DIE(aTHX_ "Not a HASH reference");
ca3f996a 4880 if (sliced) {
7332a6c4
VP
4881 if (gimme == G_VOID)
4882 SP = ORIGMARK;
4883 else if (gimme == G_SCALAR) {
4884 MARK = ORIGMARK;
4885 if (SP > MARK)
4886 *++MARK = *SP;
4887 else
4888 *++MARK = &PL_sv_undef;
4889 SP = MARK;
4890 }
4891 }
ca3f996a 4892 else if (gimme != G_VOID)
626040f7 4893 PUSHs(*unsliced_keysv);
7332a6c4
VP
4894
4895 RETURN;
4896}
4897
4898PP(pp_delete)
4899{
7332a6c4 4900 dSP;
1c23e2bd 4901 U8 gimme;
7332a6c4
VP
4902 I32 discard;
4903
4904 if (PL_op->op_private & OPpLVAL_INTRO)
4905 return do_delete_local();
4906
4907 gimme = GIMME_V;
4908 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5f05dabc 4909
cc0776d6 4910 if (PL_op->op_private & (OPpSLICE|OPpKVSLICE)) {
5f05dabc 4911 dMARK; dORIGMARK;
85fbaab2 4912 HV * const hv = MUTABLE_HV(POPs);
1b6737cc 4913 const U32 hvtype = SvTYPE(hv);
cc0776d6
DIM
4914 int skip = 0;
4915 if (PL_op->op_private & OPpKVSLICE) {
4916 SSize_t items = SP - MARK;
4917
4918 MEXTEND(SP,items);
4919 while (items > 1) {
4920 *(MARK+items*2-1) = *(MARK+items);
4921 items--;
4922 }
4923 items = SP - MARK;
4924 SP += items;
4925 skip = 1;
4926 }
01020589 4927 if (hvtype == SVt_PVHV) { /* hash element */
cc0776d6
DIM
4928 while ((MARK += (1+skip)) <= SP) {
4929 SV * const sv = hv_delete_ent(hv, *(MARK-skip), discard, 0);
01020589
GS
4930 *MARK = sv ? sv : &PL_sv_undef;
4931 }
5f05dabc 4932 }
6d822dc4
MS
4933 else if (hvtype == SVt_PVAV) { /* array element */
4934 if (PL_op->op_flags & OPf_SPECIAL) {
cc0776d6
DIM
4935 while ((MARK += (1+skip)) <= SP) {
4936 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*(MARK-skip)), discard);
6d822dc4
MS
4937 *MARK = sv ? sv : &PL_sv_undef;
4938 }
4939 }
01020589
GS
4940 }
4941 else
4942 DIE(aTHX_ "Not a HASH reference");
54310121 4943 if (discard)
4944 SP = ORIGMARK;
4945 else if (gimme == G_SCALAR) {
5f05dabc 4946 MARK = ORIGMARK;
9111c9c0
DM
4947 if (SP > MARK)
4948 *++MARK = *SP;
4949 else
4950 *++MARK = &PL_sv_undef;
5f05dabc 4951 SP = MARK;
4952 }
4953 }
4954 else {
4955 SV *keysv = POPs;
85fbaab2 4956 HV * const hv = MUTABLE_HV(POPs);
295d248e 4957 SV *sv = NULL;
97fcbf96
MB
4958 if (SvTYPE(hv) == SVt_PVHV)
4959 sv = hv_delete_ent(hv, keysv, discard, 0);
01020589
GS
4960 else if (SvTYPE(hv) == SVt_PVAV) {
4961 if (PL_op->op_flags & OPf_SPECIAL)
502c6561 4962 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
af288a60
HS
4963 else
4964 DIE(aTHX_ "panic: avhv_delete no longer supported");
01020589 4965 }
97fcbf96 4966 else
cea2e8a9 4967 DIE(aTHX_ "Not a HASH reference");
5f05dabc 4968 if (!sv)
3280af22 4969 sv = &PL_sv_undef;
54310121 4970 if (!discard)
4971 PUSHs(sv);
79072805 4972 }
79072805
LW
4973 RETURN;
4974}
4975
a0d0e21e 4976PP(pp_exists)
79072805 4977{
39644a26 4978 dSP;
afebc493
GS
4979 SV *tmpsv;
4980 HV *hv;
4981
c7e88ff3 4982 if (UNLIKELY( PL_op->op_private & OPpEXISTS_SUB )) {
afebc493 4983 GV *gv;
0bd48802 4984 SV * const sv = POPs;
f2c0649b 4985 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
afebc493
GS
4986 if (cv)
4987 RETPUSHYES;
4988 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4989 RETPUSHYES;
4990 RETPUSHNO;
4991 }
4992 tmpsv = POPs;
85fbaab2 4993 hv = MUTABLE_HV(POPs);
c7e88ff3 4994 if (LIKELY( SvTYPE(hv) == SVt_PVHV )) {
ae77835f 4995 if (hv_exists_ent(hv, tmpsv, 0))
c750a3ec 4996 RETPUSHYES;
ef54e1a4
JH
4997 }
4998 else if (SvTYPE(hv) == SVt_PVAV) {
01020589 4999 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
502c6561 5000 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
01020589
GS
5001 RETPUSHYES;
5002 }
ef54e1a4
JH
5003 }
5004 else {
cea2e8a9 5005 DIE(aTHX_ "Not a HASH reference");
a0d0e21e 5006 }
a0d0e21e
LW
5007 RETPUSHNO;
5008}
79072805 5009
a0d0e21e
LW
5010PP(pp_hslice)
5011{
20b7effb 5012 dSP; dMARK; dORIGMARK;
eb578fdb
KW
5013 HV * const hv = MUTABLE_HV(POPs);
5014 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
1b6737cc 5015 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
d30e492c 5016 bool can_preserve = FALSE;
79072805 5017
eb85dfd3
DM
5018 if (localizing) {
5019 MAGIC *mg;
5020 HV *stash;
5021
2c5f48c2 5022 if (SvCANEXISTDELETE(hv))
d30e492c 5023 can_preserve = TRUE;
eb85dfd3
DM
5024 }
5025
6d822dc4 5026 while (++MARK <= SP) {
1b6737cc 5027 SV * const keysv = *MARK;
6d822dc4
MS
5028 SV **svp;
5029 HE *he;
d30e492c
VP
5030 bool preeminent = TRUE;
5031
5032 if (localizing && can_preserve) {
5033 /* If we can determine whether the element exist,
5034 * try to preserve the existenceness of a tied hash
5035 * element by using EXISTS and DELETE if possible.
5036 * Fallback to FETCH and STORE otherwise. */
5037 preeminent = hv_exists_ent(hv, keysv, 0);
6d822dc4 5038 }
eb85dfd3 5039
6d822dc4 5040 he = hv_fetch_ent(hv, keysv, lval, 0);
fe5bfecd 5041 svp = he ? &HeVAL(he) : NULL;
eb85dfd3 5042
6d822dc4 5043 if (lval) {
746f6409 5044 if (!svp || !*svp || *svp == &PL_sv_undef) {
be2597df 5045 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
6d822dc4
MS
5046 }
5047 if (localizing) {
7a2e501a 5048 if (HvNAME_get(hv) && isGV(*svp))
159b6efe 5049 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
47cfc530
VP
5050 else if (preeminent)
5051 save_helem_flags(hv, keysv, svp,
5052 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5053 else
5054 SAVEHDELETE(hv, keysv);
6d822dc4
MS
5055 }
5056 }
746f6409 5057 *MARK = svp && *svp ? *svp : &PL_sv_undef;
79072805 5058 }
82334630 5059 if (GIMME_V != G_ARRAY) {
a0d0e21e 5060 MARK = ORIGMARK;
04ab2c87 5061 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
a0d0e21e 5062 SP = MARK;
79072805 5063 }
a0d0e21e
LW
5064 RETURN;
5065}
5066
5cae3edb
RZ
5067PP(pp_kvhslice)
5068{
20b7effb 5069 dSP; dMARK;
5cae3edb
RZ
5070 HV * const hv = MUTABLE_HV(POPs);
5071 I32 lval = (PL_op->op_flags & OPf_MOD);
adad97db 5072 SSize_t items = SP - MARK;
5cae3edb
RZ
5073
5074 if (PL_op->op_private & OPpMAYBE_LVSUB) {
5075 const I32 flags = is_lvalue_sub();
5076 if (flags) {
5077 if (!(flags & OPpENTERSUB_INARGS))
7aae0299 5078 /* diag_listed_as: Can't modify %s in %s */
cc5f9b8a
FC
5079 Perl_croak(aTHX_ "Can't modify key/value hash slice in %s assignment",
5080 GIMME_V == G_ARRAY ? "list" : "scalar");
5cae3edb
RZ
5081 lval = flags;
5082 }
5083 }
5084
5085 MEXTEND(SP,items);
5086 while (items > 1) {
5087 *(MARK+items*2-1) = *(MARK+items);
5088 items--;
5089 }
5090 items = SP-MARK;
5091 SP += items;
5092
5093 while (++MARK <= SP) {
5094 SV * const keysv = *MARK;
5095 SV **svp;
5096 HE *he;
5097
5098 he = hv_fetch_ent(hv, keysv, lval, 0);
5099 svp = he ? &HeVAL(he) : NULL;
5100
5101 if (lval) {
5102 if (!svp || !*svp || *svp == &PL_sv_undef) {
5103 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5104 }
5105 *MARK = sv_mortalcopy(*MARK);
5106 }
5107 *++MARK = svp && *svp ? *svp : &PL_sv_undef;
5108 }
82334630 5109 if (GIMME_V != G_ARRAY) {
5cae3edb
RZ
5110 MARK = SP - items*2;
5111 *++MARK = items > 0 ? *SP : &PL_sv_undef;
5112 SP = MARK;
5113 }
5114 RETURN;
5115}
5116
a0d0e21e
LW
5117/* List operators. */
5118
5119PP(pp_list)
5120{
4fa715fa 5121 I32 markidx = POPMARK;
82334630 5122 if (GIMME_V != G_ARRAY) {
4fa715fa
DD
5123 SV **mark = PL_stack_base + markidx;
5124 dSP;
b54564c3 5125 EXTEND(SP, 1); /* in case no arguments, as in @empty */
a0d0e21e
LW
5126 if (++MARK <= SP)
5127 *MARK = *SP; /* unwanted list, return last item */
8990e307 5128 else
3280af22 5129 *MARK = &PL_sv_undef;
a0d0e21e 5130 SP = MARK;
4fa715fa 5131 PUTBACK;
79072805 5132 }
4fa715fa 5133 return NORMAL;
79072805
LW
5134}
5135
a0d0e21e 5136PP(pp_lslice)
79072805 5137{
39644a26 5138 dSP;
1b6737cc
AL
5139 SV ** const lastrelem = PL_stack_sp;
5140 SV ** const lastlelem = PL_stack_base + POPMARK;
5141 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
eb578fdb 5142 SV ** const firstrelem = lastlelem + 1;
706a6ebc 5143 const U8 mod = PL_op->op_flags & OPf_MOD;
1b6737cc 5144
eb578fdb
KW
5145 const I32 max = lastrelem - lastlelem;
5146 SV **lelem;
a0d0e21e 5147
82334630 5148 if (GIMME_V != G_ARRAY) {
9e59c36b 5149 if (lastlelem < firstlelem) {
7da51ead 5150 EXTEND(SP, 1);
9e59c36b
TC
5151 *firstlelem = &PL_sv_undef;
5152 }
5153 else {
5154 I32 ix = SvIV(*lastlelem);
5155 if (ix < 0)
5156 ix += max;
5157 if (ix < 0 || ix >= max)
5158 *firstlelem = &PL_sv_undef;
5159 else
5160 *firstlelem = firstrelem[ix];
5161 }
5162 SP = firstlelem;
5163 RETURN;
a0d0e21e
LW
5164 }
5165
5166 if (max == 0) {
5167 SP = firstlelem - 1;
5168 RETURN;
5169 }
5170
5171 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4ea561bc 5172 I32 ix = SvIV(*lelem);
c73bf8e3 5173 if (ix < 0)
a0d0e21e 5174 ix += max;
c73bf8e3
HS
5175 if (ix < 0 || ix >= max)
5176 *lelem = &PL_sv_undef;
5177 else {
c73bf8e3 5178 if (!(*lelem = firstrelem[ix]))
3280af22 5179 *lelem = &PL_sv_undef;
60779a30 5180 else if (mod && SvPADTMP(*lelem)) {
706a6ebc 5181 *lelem = firstrelem[ix] = sv_mortalcopy(*lelem);
60779a30 5182 }
748a9306 5183 }
79072805 5184 }
cbce292e 5185 SP = lastlelem;
79072805
LW
5186 RETURN;
5187}
5188
a0d0e21e
LW
5189PP(pp_anonlist)
5190{
20b7effb 5191 dSP; dMARK;
1b6737cc 5192 const I32 items = SP - MARK;
ad64d0ec 5193 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
31476221 5194 SP = MARK;
6e449a3a
MHM
5195 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5196 ? newRV_noinc(av) : av);
a0d0e21e
LW
5197 RETURN;
5198}
5199
5200PP(pp_anonhash)
79072805 5201{
20b7effb 5202 dSP; dMARK; dORIGMARK;
67e67fd7 5203 HV* const hv = newHV();
8d455b9f 5204 SV* const retval = sv_2mortal( PL_op->op_flags & OPf_SPECIAL
67e67fd7 5205 ? newRV_noinc(MUTABLE_SV(hv))
8d455b9f 5206 : MUTABLE_SV(hv) );
a0d0e21e
LW
5207
5208 while (MARK < SP) {
3ed356df
FC
5209 SV * const key =
5210 (MARK++, SvGMAGICAL(*MARK) ? sv_mortalcopy(*MARK) : *MARK);
5211 SV *val;
a0d0e21e 5212 if (MARK < SP)
3ed356df
FC
5213 {
5214 MARK++;
5215 SvGETMAGIC(*MARK);
5216 val = newSV(0);
d187b712 5217 sv_setsv_nomg(val, *MARK);
3ed356df 5218 }
a2a5de95 5219 else
3ed356df 5220 {
a2a5de95 5221 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
3ed356df
FC
5222 val = newSV(0);
5223 }
f12c7020 5224 (void)hv_store_ent(hv,key,val,0);
79072805 5225 }
a0d0e21e 5226 SP = ORIGMARK;
8d455b9f 5227 XPUSHs(retval);
79072805
LW
5228 RETURN;
5229}
5230
a0d0e21e 5231PP(pp_splice)
79072805 5232{
20b7effb 5233 dSP; dMARK; dORIGMARK;
5cd408a2 5234 int num_args = (SP - MARK);
00576728 5235 AV *ary = MUTABLE_AV(*++MARK);
eb578fdb
KW
5236 SV **src;
5237 SV **dst;
c70927a6
FC
5238 SSize_t i;
5239 SSize_t offset;
5240 SSize_t length;
5241 SSize_t newlen;
5242 SSize_t after;
5243 SSize_t diff;
ad64d0ec 5244 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
93965878 5245
1b6737cc 5246 if (mg) {
3e0cb5de 5247 return Perl_tied_method(aTHX_ SV_CONST(SPLICE), mark - 1, MUTABLE_SV(ary), mg,
af71faff
NC
5248 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
5249 sp - mark);
93965878 5250 }
79072805 5251
a0d0e21e 5252 SP++;
79072805 5253
a0d0e21e 5254 if (++MARK < SP) {
4ea561bc 5255 offset = i = SvIV(*MARK);
a0d0e21e 5256 if (offset < 0)
93965878 5257 offset += AvFILLp(ary) + 1;
84902520 5258 if (offset < 0)
cea2e8a9 5259 DIE(aTHX_ PL_no_aelem, i);
a0d0e21e
LW
5260 if (++MARK < SP) {
5261 length = SvIVx(*MARK++);
48cdf507
GA
5262 if (length < 0) {
5263 length += AvFILLp(ary) - offset + 1;
5264 if (length < 0)
5265 length = 0;
5266 }
79072805
LW
5267 }
5268 else
a0d0e21e 5269 length = AvMAX(ary) + 1; /* close enough to infinity */
79072805 5270 }
a0d0e21e
LW
5271 else {
5272 offset = 0;
5273 length = AvMAX(ary) + 1;
5274 }
8cbc2e3b 5275 if (offset > AvFILLp(ary) + 1) {
5cd408a2
EB
5276 if (num_args > 2)
5277 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
93965878 5278 offset = AvFILLp(ary) + 1;
8cbc2e3b 5279 }
93965878 5280 after = AvFILLp(ary) + 1 - (offset + length);
a0d0e21e
LW
5281 if (after < 0) { /* not that much array */
5282 length += after; /* offset+length now in array */
5283 after = 0;
5284 if (!AvALLOC(ary))
5285 av_extend(ary, 0);
5286 }
5287
5288 /* At this point, MARK .. SP-1 is our new LIST */
5289
5290 newlen = SP - MARK;
5291 diff = newlen - length;
13d7cbc1
GS
5292 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5293 av_reify(ary);
a0d0e21e 5294
50528de0
WL
5295 /* make new elements SVs now: avoid problems if they're from the array */
5296 for (dst = MARK, i = newlen; i; i--) {
1b6737cc 5297 SV * const h = *dst;
f2b990bf 5298 *dst++ = newSVsv(h);
50528de0
WL
5299 }
5300
a0d0e21e 5301 if (diff < 0) { /* shrinking the area */
95b63a38 5302 SV **tmparyval = NULL;
a0d0e21e 5303 if (newlen) {
a02a5408 5304 Newx(tmparyval, newlen, SV*); /* so remember insertion */
a0d0e21e 5305 Copy(MARK, tmparyval, newlen, SV*);
79072805 5306 }
a0d0e21e
LW
5307
5308 MARK = ORIGMARK + 1;
82334630 5309 if (GIMME_V == G_ARRAY) { /* copy return vals to stack */
31c61add 5310 const bool real = cBOOL(AvREAL(ary));
a0d0e21e 5311 MEXTEND(MARK, length);
31c61add 5312 if (real)
bbce6d69 5313 EXTEND_MORTAL(length);
31c61add
FC
5314 for (i = 0, dst = MARK; i < length; i++) {
5315 if ((*dst = AvARRAY(ary)[i+offset])) {
5316 if (real)
486ec47a 5317 sv_2mortal(*dst); /* free them eventually */
36477c24 5318 }
31c61add
FC
5319 else
5320 *dst = &PL_sv_undef;
5321 dst++;
a0d0e21e
LW
5322 }
5323 MARK += length - 1;
79072805 5324 }
a0d0e21e
LW
5325 else {
5326 *MARK = AvARRAY(ary)[offset+length-1];
5327 if (AvREAL(ary)) {
d689ffdd 5328 sv_2mortal(*MARK);
a0d0e21e
LW
5329 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5330 SvREFCNT_dec(*dst++); /* free them now */
79072805 5331 }
92b69f65
FC
5332 if (!*MARK)
5333 *MARK = &PL_sv_undef;
a0d0e21e 5334 }
93965878 5335 AvFILLp(ary) += diff;
a0d0e21e
LW
5336
5337 /* pull up or down? */
5338
5339 if (offset < after) { /* easier to pull up */
5340 if (offset) { /* esp. if nothing to pull */
5341 src = &AvARRAY(ary)[offset-1];
5342 dst = src - diff; /* diff is negative */
5343 for (i = offset; i > 0; i--) /* can't trust Copy */
5344 *dst-- = *src--;
79072805 5345 }
a0d0e21e 5346 dst = AvARRAY(ary);
9c6bc640 5347 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
a0d0e21e
LW
5348 AvMAX(ary) += diff;
5349 }
5350 else {
5351 if (after) { /* anything to pull down? */
5352 src = AvARRAY(ary) + offset + length;
5353 dst = src + diff; /* diff is negative */
5354 Move(src, dst, after, SV*);
79072805 5355 }
93965878 5356 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
a0d0e21e
LW
5357 /* avoid later double free */
5358 }
5359 i = -diff;
5360 while (i)
ce0d59fd 5361 dst[--i] = NULL;
a0d0e21e
LW
5362
5363 if (newlen) {
50528de0 5364 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
a0d0e21e
LW
5365 Safefree(tmparyval);
5366 }
5367 }
5368 else { /* no, expanding (or same) */
d3961450 5369 SV** tmparyval = NULL;
a0d0e21e 5370 if (length) {
a02a5408 5371 Newx(tmparyval, length, SV*); /* so remember deletion */
a0d0e21e
LW
5372 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5373 }
5374
5375 if (diff > 0) { /* expanding */
a0d0e21e 5376 /* push up or down? */
a0d0e21e
LW
5377 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5378 if (offset) {
5379 src = AvARRAY(ary);
5380 dst = src - diff;
5381 Move(src, dst, offset, SV*);
79072805 5382 }
9c6bc640 5383 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
a0d0e21e 5384 AvMAX(ary) += diff;
93965878 5385 AvFILLp(ary) += diff;
79072805
LW
5386 }
5387 else {
93965878
NIS
5388 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5389 av_extend(ary, AvFILLp(ary) + diff);
5390 AvFILLp(ary) += diff;
a0d0e21e
LW
5391
5392 if (after) {
93965878 5393 dst = AvARRAY(ary) + AvFILLp(ary);
a0d0e21e
LW
5394 src = dst - diff;
5395 for (i = after; i; i--) {
5396 *dst-- = *src--;
5397 }
79072805
LW
5398 }
5399 }
a0d0e21e
LW
5400 }
5401
50528de0
WL
5402 if (newlen) {
5403 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
a0d0e21e 5404 }
50528de0 5405
a0d0e21e 5406 MARK = ORIGMARK + 1;
82334630 5407 if (GIMME_V == G_ARRAY) { /* copy return vals to stack */
a0d0e21e 5408 if (length) {
31c61add
FC
5409 const bool real = cBOOL(AvREAL(ary));
5410 if (real)
bbce6d69 5411 EXTEND_MORTAL(length);
31c61add
FC
5412 for (i = 0, dst = MARK; i < length; i++) {
5413 if ((*dst = tmparyval[i])) {
5414 if (real)
486ec47a 5415 sv_2mortal(*dst); /* free them eventually */
36477c24 5416 }
31c61add
FC
5417 else *dst = &PL_sv_undef;
5418 dst++;
79072805
LW
5419 }
5420 }
a0d0e21e
LW
5421 MARK += length - 1;
5422 }
5423 else if (length--) {
5424 *MARK = tmparyval[length];
5425 if (AvREAL(ary)) {
d689ffdd 5426 sv_2mortal(*MARK);
a0d0e21e
LW
5427 while (length-- > 0)
5428 SvREFCNT_dec(tmparyval[length]);
79072805 5429 }
92b69f65
FC
5430 if (!*MARK)
5431 *MARK = &PL_sv_undef;
79072805 5432 }
a0d0e21e 5433 else
3280af22 5434 *MARK = &PL_sv_undef;
d3961450 5435 Safefree(tmparyval);
79072805 5436 }
474af990
FR
5437
5438 if (SvMAGICAL(ary))
5439 mg_set(MUTABLE_SV(ary));
5440
a0d0e21e 5441 SP = MARK;
79072805
LW
5442 RETURN;
5443}
5444
a0d0e21e 5445PP(pp_push)
79072805 5446{
20b7effb 5447 dSP; dMARK; dORIGMARK; dTARGET;
00576728 5448 AV * const ary = MUTABLE_AV(*++MARK);
ad64d0ec 5449 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
79072805 5450
1b6737cc 5451 if (mg) {
ad64d0ec 5452 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
93965878
NIS
5453 PUSHMARK(MARK);
5454 PUTBACK;
d343c3ef 5455 ENTER_with_name("call_PUSH");
3e0cb5de 5456 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
d343c3ef 5457 LEAVE_with_name("call_PUSH");
01072573 5458 /* SPAGAIN; not needed: SP is assigned to immediately below */
93965878 5459 }
a60c0954 5460 else {
a68090fe
DM
5461 /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we
5462 * only need to save locally, not on the save stack */
5463 U16 old_delaymagic = PL_delaymagic;
5464
cb077ed2 5465 if (SvREADONLY(ary) && MARK < SP) Perl_croak_no_modify();
89c14e2e 5466 PL_delaymagic = DM_DELAY;
a60c0954 5467 for (++MARK; MARK <= SP; MARK++) {
3ed356df
FC
5468 SV *sv;
5469 if (*MARK) SvGETMAGIC(*MARK);
5470 sv = newSV(0);
a60c0954 5471 if (*MARK)
3ed356df 5472 sv_setsv_nomg(sv, *MARK);
0a75904b 5473 av_store(ary, AvFILLp(ary)+1, sv);
a60c0954 5474 }
354b0578 5475 if (PL_delaymagic & DM_ARRAY_ISA)
ad64d0ec 5476 mg_set(MUTABLE_SV(ary));
a68090fe 5477 PL_delaymagic = old_delaymagic;
6eeabd23
VP
5478 }
5479 SP = ORIGMARK;
5480 if (OP_GIMME(PL_op, 0) != G_VOID) {
5481 PUSHi( AvFILL(ary) + 1 );
79072805 5482 }
79072805
LW
5483 RETURN;
5484}
5485
b1c05ba5 5486/* also used for: pp_pop()*/
a0d0e21e 5487PP(pp_shift)
79072805 5488{
39644a26 5489 dSP;
538f5756 5490 AV * const av = PL_op->op_flags & OPf_SPECIAL
94f9945d 5491 ? MUTABLE_AV(GvAVn(PL_defgv)) : MUTABLE_AV(POPs);
789b4bc9 5492 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
79072805 5493 EXTEND(SP, 1);
c2b4a044 5494 assert (sv);
d689ffdd 5495 if (AvREAL(av))
a0d0e21e
LW
5496 (void)sv_2mortal(sv);
5497 PUSHs(sv);
79072805 5498 RETURN;
79072805
LW
5499}
5500
a0d0e21e 5501PP(pp_unshift)
79072805 5502{
20b7effb 5503 dSP; dMARK; dORIGMARK; dTARGET;
00576728 5504 AV *ary = MUTABLE_AV(*++MARK);
ad64d0ec 5505 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
93965878 5506
1b6737cc 5507 if (mg) {
ad64d0ec 5508 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
7fd66d9d 5509 PUSHMARK(MARK);
93965878 5510 PUTBACK;
d343c3ef 5511 ENTER_with_name("call_UNSHIFT");
36925d9e 5512 call_sv(SV_CONST(UNSHIFT),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
d343c3ef 5513 LEAVE_with_name("call_UNSHIFT");
01072573 5514 /* SPAGAIN; not needed: SP is assigned to immediately below */
93965878 5515 }
a60c0954 5516 else {
a68090fe
DM
5517 /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we
5518 * only need to save locally, not on the save stack */
5519 U16 old_delaymagic = PL_delaymagic;
c70927a6 5520 SSize_t i = 0;
a68090fe 5521
a60c0954 5522 av_unshift(ary, SP - MARK);
39539141 5523 PL_delaymagic = DM_DELAY;
a60c0954 5524 while (MARK < SP) {
1b6737cc 5525 SV * const sv = newSVsv(*++MARK);
a60c0954
NIS
5526 (void)av_store(ary, i++, sv);
5527 }
39539141
DIM
5528 if (PL_delaymagic & DM_ARRAY_ISA)
5529 mg_set(MUTABLE_SV(ary));
a68090fe 5530 PL_delaymagic = old_delaymagic;
79072805 5531 }
a0d0e21e 5532 SP = ORIGMARK;
6eeabd23 5533 if (OP_GIMME(PL_op, 0) != G_VOID) {
5658d0a9
LR
5534 PUSHi( AvFILL(ary) + 1 );
5535 }
79072805 5536 RETURN;
79072805
LW
5537}
5538
a0d0e21e 5539PP(pp_reverse)
79072805 5540{
20b7effb 5541 dSP; dMARK;
79072805 5542
82334630 5543 if (GIMME_V == G_ARRAY) {
484c818f
VP
5544 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5545 AV *av;
5546
5547 /* See pp_sort() */
5548 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5549 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5550 av = MUTABLE_AV((*SP));
5551 /* In-place reversing only happens in void context for the array
5552 * assignment. We don't need to push anything on the stack. */
5553 SP = MARK;
5554
5555 if (SvMAGICAL(av)) {
c70927a6 5556 SSize_t i, j;
eb578fdb 5557 SV *tmp = sv_newmortal();
484c818f
VP
5558 /* For SvCANEXISTDELETE */
5559 HV *stash;
5560 const MAGIC *mg;
5561 bool can_preserve = SvCANEXISTDELETE(av);
5562
b9f2b683 5563 for (i = 0, j = av_tindex(av); i < j; ++i, --j) {
eb578fdb 5564 SV *begin, *end;
484c818f
VP
5565
5566 if (can_preserve) {
5567 if (!av_exists(av, i)) {
5568 if (av_exists(av, j)) {
eb578fdb 5569 SV *sv = av_delete(av, j, 0);
484c818f
VP
5570 begin = *av_fetch(av, i, TRUE);
5571 sv_setsv_mg(begin, sv);
5572 }
5573 continue;
5574 }
5575 else if (!av_exists(av, j)) {
eb578fdb 5576 SV *sv = av_delete(av, i, 0);
484c818f
VP
5577 end = *av_fetch(av, j, TRUE);
5578 sv_setsv_mg(end, sv);
5579 continue;
5580 }
5581 }
5582
5583 begin = *av_fetch(av, i, TRUE);
5584 end = *av_fetch(av, j, TRUE);
5585 sv_setsv(tmp, begin);
5586 sv_setsv_mg(begin, end);
5587 sv_setsv_mg(end, tmp);
5588 }
5589 }
5590 else {
5591 SV **begin = AvARRAY(av);
484c818f 5592
95a26d8e
VP
5593 if (begin) {
5594 SV **end = begin + AvFILLp(av);
5595
5596 while (begin < end) {
eb578fdb 5597 SV * const tmp = *begin;
95a26d8e
VP
5598 *begin++ = *end;
5599 *end-- = tmp;
5600 }
484c818f
VP
5601 }
5602 }
5603 }
5604 else {
5605 SV **oldsp = SP;
5606 MARK++;
5607 while (MARK < SP) {
eb578fdb 5608 SV * const tmp = *MARK;
484c818f
VP
5609 *MARK++ = *SP;
5610 *SP-- = tmp;
5611 }
5612 /* safe as long as stack cannot get extended in the above */
5613 SP = oldsp;
a0d0e21e 5614 }
79072805
LW
5615 }
5616 else {
eb578fdb 5617 char *up;
a0d0e21e
LW
5618 dTARGET;
5619 STRLEN len;
79072805 5620
7e2040f0 5621 SvUTF8_off(TARG); /* decontaminate */
a0d0e21e 5622 if (SP - MARK > 1)
3280af22 5623 do_join(TARG, &PL_sv_no, MARK, SP);
d5d91c1e
DM
5624 else if (SP > MARK)
5625 sv_setsv(TARG, *SP);
5626 else {
5627 sv_setsv(TARG, DEFSV);
5628 EXTEND(SP, 1);
1e21d011
B
5629 }
5630
a0d0e21e
LW
5631 up = SvPV_force(TARG, len);
5632 if (len > 1) {
19742f39 5633 char *down;
7e2040f0 5634 if (DO_UTF8(TARG)) { /* first reverse each character */
dfe13c55 5635 U8* s = (U8*)SvPVX(TARG);
349d4f2f 5636 const U8* send = (U8*)(s + len);
a0ed51b3 5637 while (s < send) {
d742c382 5638 if (UTF8_IS_INVARIANT(*s)) {
a0ed51b3
LW
5639 s++;
5640 continue;
5641 }
5642 else {
4b88fb76 5643 if (!utf8_to_uvchr_buf(s, send, 0))
a0dbb045 5644 break;
dfe13c55 5645 up = (char*)s;
a0ed51b3 5646 s += UTF8SKIP(s);
dfe13c55 5647 down = (char*)(s - 1);
a0dbb045 5648 /* reverse this character */
a0ed51b3 5649 while (down > up) {
19742f39 5650 const char tmp = *up;
a0ed51b3 5651 *up++ = *down;
19742f39 5652 *down-- = tmp;
a0ed51b3
LW
5653 }
5654 }
5655 }
5656 up = SvPVX(TARG);
5657 }
a0d0e21e
LW
5658 down = SvPVX(TARG) + len - 1;
5659 while (down > up) {
19742f39 5660 const char tmp = *up;
a0d0e21e 5661 *up++ = *down;
19742f39 5662 *down-- = tmp;
a0d0e21e 5663 }
3aa33fe5 5664 (void)SvPOK_only_UTF8(TARG);
79072805 5665 }
a0d0e21e
LW
5666 SP = MARK + 1;
5667 SETTARG;
79072805 5668 }
a0d0e21e 5669 RETURN;
79072805
LW
5670}
5671
a0d0e21e 5672PP(pp_split)
79072805 5673{
20b7effb 5674 dSP; dTARG;
692044df
DM
5675 AV *ary = ( (PL_op->op_private & OPpSPLIT_ASSIGN) /* @a = split */
5676 && (PL_op->op_flags & OPf_STACKED)) /* @{expr} = split */
5012eebe 5677 ? (AV *)POPs : NULL;
eb578fdb 5678 IV limit = POPi; /* note, negative is forever */
1b6737cc 5679 SV * const sv = POPs;
a0d0e21e 5680 STRLEN len;
eb578fdb 5681 const char *s = SvPV_const(sv, len);
1b6737cc 5682 const bool do_utf8 = DO_UTF8(sv);
20ae58f7 5683 const bool in_uni_8_bit = IN_UNI_8_BIT;
727b7506 5684 const char *strend = s + len;
5012eebe 5685 PMOP *pm = cPMOPx(PL_op);
eb578fdb
KW
5686 REGEXP *rx;
5687 SV *dstr;
5688 const char *m;
c70927a6 5689 SSize_t iters = 0;
d14578b8
KW
5690 const STRLEN slen = do_utf8
5691 ? utf8_length((U8*)s, (U8*)strend)
5692 : (STRLEN)(strend - s);
c70927a6 5693 SSize_t maxiters = slen + 10;
c1a7495a 5694 I32 trailing_empty = 0;
727b7506 5695 const char *orig;
052a7c76 5696 const IV origlimit = limit;
a0d0e21e
LW
5697 I32 realarray = 0;
5698 I32 base;
1c23e2bd 5699 const U8 gimme = GIMME_V;
941446f6 5700 bool gimme_scalar;
692044df 5701 I32 oldsave = PL_savestack_ix;
437d3b4e 5702 U32 make_mortal = SVs_TEMP;
7fba1cd6 5703 bool multiline = 0;
b37c2d43 5704 MAGIC *mg = NULL;
79072805 5705
aaa362c4 5706 rx = PM_GETRE(pm);
bbce6d69 5707
a62b1201 5708 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
dbc200c5 5709 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
bbce6d69 5710
692044df 5711 /* handle @ary = split(...) optimisation */
5012eebe
DM
5712 if (PL_op->op_private & OPpSPLIT_ASSIGN) {
5713 if (!(PL_op->op_flags & OPf_STACKED)) {
692044df
DM
5714 if (PL_op->op_private & OPpSPLIT_LEX) {
5715 if (PL_op->op_private & OPpLVAL_INTRO)
5716 SAVECLEARSV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
5012eebe 5717 ary = (AV *)PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff);
692044df 5718 }
5012eebe
DM
5719 else {
5720 GV *gv =
971a9dd3 5721#ifdef USE_ITHREADS
5012eebe 5722 MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
971a9dd3 5723#else
5012eebe 5724 pm->op_pmreplrootu.op_pmtargetgv;
20e98b0f 5725#endif
692044df
DM
5726 if (PL_op->op_private & OPpLVAL_INTRO)
5727 ary = save_ary(gv);
5728 else
5729 ary = GvAVn(gv);
5012eebe 5730 }
692044df
DM
5731 /* skip anything pushed by OPpLVAL_INTRO above */
5732 oldsave = PL_savestack_ix;
5012eebe
DM
5733 }
5734
a0d0e21e 5735 realarray = 1;
8ec5e241 5736 PUTBACK;
a0d0e21e 5737 av_extend(ary,0);
821956c5 5738 (void)sv_2mortal(SvREFCNT_inc_simple_NN(sv));
a0d0e21e 5739 av_clear(ary);
8ec5e241 5740 SPAGAIN;
ad64d0ec 5741 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
8ec5e241 5742 PUSHMARK(SP);
ad64d0ec 5743 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
8ec5e241
NIS
5744 }
5745 else {
1c0b011c 5746 if (!AvREAL(ary)) {
1b6737cc 5747 I32 i;
1c0b011c 5748 AvREAL_on(ary);
abff13bb 5749 AvREIFY_off(ary);
1c0b011c 5750 for (i = AvFILLp(ary); i >= 0; i--)
d14578b8 5751 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
1c0b011c
NIS
5752 }
5753 /* temporarily switch stacks */
8b7059b1 5754 SAVESWITCHSTACK(PL_curstack, ary);
8ec5e241 5755 make_mortal = 0;
1c0b011c 5756 }
79072805 5757 }
5012eebe 5758
3280af22 5759 base = SP - PL_stack_base;
a0d0e21e 5760 orig = s;
dbc200c5 5761 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
613f191e 5762 if (do_utf8) {
d720149d 5763 while (s < strend && isSPACE_utf8_safe(s, strend))
613f191e
TS
5764 s += UTF8SKIP(s);
5765 }
a62b1201 5766 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
d720149d 5767 while (s < strend && isSPACE_LC(*s))
bbce6d69 5768 s++;
5769 }
20ae58f7
AC
5770 else if (in_uni_8_bit) {
5771 while (s < strend && isSPACE_L1(*s))
5772 s++;
5773 }
bbce6d69 5774 else {
d720149d 5775 while (s < strend && isSPACE(*s))
bbce6d69 5776 s++;
5777 }
a0d0e21e 5778 }
73134a2e 5779 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
7fba1cd6 5780 multiline = 1;
c07a80fd 5781 }
5782
941446f6
FC
5783 gimme_scalar = gimme == G_SCALAR && !ary;
5784
a0d0e21e
LW
5785 if (!limit)
5786 limit = maxiters + 2;
dbc200c5 5787 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
a0d0e21e 5788 while (--limit) {
bbce6d69 5789 m = s;
8727f688
YO
5790 /* this one uses 'm' and is a negative test */
5791 if (do_utf8) {
7a207065 5792 while (m < strend && ! isSPACE_utf8_safe(m, strend) ) {
613f191e 5793 const int t = UTF8SKIP(m);
7a207065 5794 /* isSPACE_utf8_safe returns FALSE for malform utf8 */
613f191e
TS
5795 if (strend - m < t)
5796 m = strend;
5797 else
5798 m += t;
5799 }
a62b1201 5800 }
d14578b8
KW
5801 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
5802 {
8727f688
YO
5803 while (m < strend && !isSPACE_LC(*m))
5804 ++m;
20ae58f7
AC
5805 }
5806 else if (in_uni_8_bit) {
5807 while (m < strend && !isSPACE_L1(*m))
5808 ++m;
8727f688
YO
5809 } else {
5810 while (m < strend && !isSPACE(*m))
5811 ++m;
5812 }
a0d0e21e
LW
5813 if (m >= strend)
5814 break;
bbce6d69 5815
c1a7495a
BB
5816 if (gimme_scalar) {
5817 iters++;
5818 if (m-s == 0)
5819 trailing_empty++;
5820 else
5821 trailing_empty = 0;
5822 } else {
5823 dstr = newSVpvn_flags(s, m-s,
5824 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5825 XPUSHs(dstr);
5826 }
bbce6d69 5827
613f191e
TS
5828 /* skip the whitespace found last */
5829 if (do_utf8)
5830 s = m + UTF8SKIP(m);
5831 else
5832 s = m + 1;
5833
8727f688
YO
5834 /* this one uses 's' and is a positive test */
5835 if (do_utf8) {
7a207065 5836 while (s < strend && isSPACE_utf8_safe(s, strend) )
8727f688 5837 s += UTF8SKIP(s);
a62b1201 5838 }
d14578b8
KW
5839 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
5840 {
8727f688
YO
5841 while (s < strend && isSPACE_LC(*s))
5842 ++s;
20ae58f7
AC
5843 }
5844 else if (in_uni_8_bit) {
5845 while (s < strend && isSPACE_L1(*s))
5846 ++s;
8727f688
YO
5847 } else {
5848 while (s < strend && isSPACE(*s))
5849 ++s;
5850 }
79072805
LW
5851 }
5852 }
07bc277f 5853 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
a0d0e21e 5854 while (--limit) {
a6e20a40
AL
5855 for (m = s; m < strend && *m != '\n'; m++)
5856 ;
a0d0e21e
LW
5857 m++;
5858 if (m >= strend)
5859 break;
c1a7495a
BB
5860
5861 if (gimme_scalar) {
5862 iters++;
5863 if (m-s == 0)
5864 trailing_empty++;
5865 else
5866 trailing_empty = 0;
5867 } else {
5868 dstr = newSVpvn_flags(s, m-s,
5869 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5870 XPUSHs(dstr);
5871 }
a0d0e21e
LW
5872 s = m;
5873 }
5874 }
07bc277f 5875 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
640f820d
AB
5876 /*
5877 Pre-extend the stack, either the number of bytes or
5878 characters in the string or a limited amount, triggered by:
5879
5880 my ($x, $y) = split //, $str;
5881 or
5882 split //, $str, $i;
5883 */
c1a7495a 5884 if (!gimme_scalar) {
052a7c76
DM
5885 const IV items = limit - 1;
5886 /* setting it to -1 will trigger a panic in EXTEND() */
5887 const SSize_t sslen = slen > SSize_t_MAX ? -1 : (SSize_t)slen;
5888 if (items >=0 && items < sslen)
c1a7495a
BB
5889 EXTEND(SP, items);
5890 else
052a7c76 5891 EXTEND(SP, sslen);
c1a7495a 5892 }
640f820d 5893
e9515b0f
AB
5894 if (do_utf8) {
5895 while (--limit) {
5896 /* keep track of how many bytes we skip over */
5897 m = s;
640f820d 5898 s += UTF8SKIP(s);
c1a7495a
BB
5899 if (gimme_scalar) {
5900 iters++;
5901 if (s-m == 0)
5902 trailing_empty++;
5903 else
5904 trailing_empty = 0;
5905 } else {
5906 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
640f820d 5907
c1a7495a
BB
5908 PUSHs(dstr);
5909 }
640f820d 5910
e9515b0f
AB
5911 if (s >= strend)
5912 break;
5913 }
5914 } else {
5915 while (--limit) {
c1a7495a
BB
5916 if (gimme_scalar) {
5917 iters++;
5918 } else {
5919 dstr = newSVpvn(s, 1);
e9515b0f 5920
e9515b0f 5921
c1a7495a
BB
5922 if (make_mortal)
5923 sv_2mortal(dstr);
640f820d 5924
c1a7495a
BB
5925 PUSHs(dstr);
5926 }
5927
5928 s++;
e9515b0f
AB
5929
5930 if (s >= strend)
5931 break;
5932 }
640f820d
AB
5933 }
5934 }
3c8556c3 5935 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
07bc277f
NC
5936 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5937 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
8e1490ee 5938 && !(RX_EXTFLAGS(rx) & RXf_IS_ANCHORED)) {
07bc277f 5939 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
f9f4320a 5940 SV * const csv = CALLREG_INTUIT_STRING(rx);
cf93c79d 5941
07bc277f 5942 len = RX_MINLENRET(rx);
3c8556c3 5943 if (len == 1 && !RX_UTF8(rx) && !tail) {
1b6737cc 5944 const char c = *SvPV_nolen_const(csv);
a0d0e21e 5945 while (--limit) {
a6e20a40
AL
5946 for (m = s; m < strend && *m != c; m++)
5947 ;
a0d0e21e
LW
5948 if (m >= strend)
5949 break;
c1a7495a
BB
5950 if (gimme_scalar) {
5951 iters++;
5952 if (m-s == 0)
5953 trailing_empty++;
5954 else
5955 trailing_empty = 0;
5956 } else {
5957 dstr = newSVpvn_flags(s, m-s,
d14578b8 5958 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
c1a7495a
BB
5959 XPUSHs(dstr);
5960 }
93f04dac
JH
5961 /* The rx->minlen is in characters but we want to step
5962 * s ahead by bytes. */
1aa99e6b
IH
5963 if (do_utf8)
5964 s = (char*)utf8_hop((U8*)m, len);
5965 else
5966 s = m + len; /* Fake \n at the end */
a0d0e21e
LW
5967 }
5968 }
5969 else {
a0d0e21e 5970 while (s < strend && --limit &&
f722798b 5971 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
7fba1cd6 5972 csv, multiline ? FBMrf_MULTILINE : 0)) )
a0d0e21e 5973 {
c1a7495a
BB
5974 if (gimme_scalar) {
5975 iters++;
5976 if (m-s == 0)
5977 trailing_empty++;
5978 else
5979 trailing_empty = 0;
5980 } else {
5981 dstr = newSVpvn_flags(s, m-s,
d14578b8 5982 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
c1a7495a
BB
5983 XPUSHs(dstr);
5984 }
93f04dac
JH
5985 /* The rx->minlen is in characters but we want to step
5986 * s ahead by bytes. */
1aa99e6b
IH
5987 if (do_utf8)
5988 s = (char*)utf8_hop((U8*)m, len);
5989 else
5990 s = m + len; /* Fake \n at the end */
a0d0e21e 5991 }
463ee0b2 5992 }
463ee0b2 5993 }
a0d0e21e 5994 else {
07bc277f 5995 maxiters += slen * RX_NPARENS(rx);
080c2dec 5996 while (s < strend && --limit)
bbce6d69 5997 {
1b6737cc 5998 I32 rex_return;
080c2dec 5999 PUTBACK;
d14578b8 6000 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1,
c33e64f0 6001 sv, NULL, 0);
080c2dec 6002 SPAGAIN;
1b6737cc 6003 if (rex_return == 0)
080c2dec 6004 break;
d9f97599 6005 TAINT_IF(RX_MATCH_TAINTED(rx));
6502e081
DM
6006 /* we never pass the REXEC_COPY_STR flag, so it should
6007 * never get copied */
6008 assert(!RX_MATCH_COPIED(rx));
07bc277f 6009 m = RX_OFFS(rx)[0].start + orig;
c1a7495a
BB
6010
6011 if (gimme_scalar) {
6012 iters++;
6013 if (m-s == 0)
6014 trailing_empty++;
6015 else
6016 trailing_empty = 0;
6017 } else {
6018 dstr = newSVpvn_flags(s, m-s,
6019 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6020 XPUSHs(dstr);
6021 }
07bc277f 6022 if (RX_NPARENS(rx)) {
1b6737cc 6023 I32 i;
07bc277f
NC
6024 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
6025 s = RX_OFFS(rx)[i].start + orig;
6026 m = RX_OFFS(rx)[i].end + orig;
6de67870
JP
6027
6028 /* japhy (07/27/01) -- the (m && s) test doesn't catch
6029 parens that didn't match -- they should be set to
6030 undef, not the empty string */
c1a7495a
BB
6031 if (gimme_scalar) {
6032 iters++;
6033 if (m-s == 0)
6034 trailing_empty++;
6035 else
6036 trailing_empty = 0;
6037 } else {
6038 if (m >= orig && s >= orig) {
6039 dstr = newSVpvn_flags(s, m-s,
6040 (do_utf8 ? SVf_UTF8 : 0)
6041 | make_mortal);
6042 }
6043 else
6044 dstr = &PL_sv_undef; /* undef, not "" */
6045 XPUSHs(dstr);
748a9306 6046 }
c1a7495a 6047
a0d0e21e
LW
6048 }
6049 }
07bc277f 6050 s = RX_OFFS(rx)[0].end + orig;
a0d0e21e 6051 }
79072805 6052 }
8ec5e241 6053
c1a7495a
BB
6054 if (!gimme_scalar) {
6055 iters = (SP - PL_stack_base) - base;
6056 }
a0d0e21e 6057 if (iters > maxiters)
cea2e8a9 6058 DIE(aTHX_ "Split loop");
8ec5e241 6059
a0d0e21e
LW
6060 /* keep field after final delim? */
6061 if (s < strend || (iters && origlimit)) {
c1a7495a
BB
6062 if (!gimme_scalar) {
6063 const STRLEN l = strend - s;
6064 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6065 XPUSHs(dstr);
6066 }
a0d0e21e 6067 iters++;
79072805 6068 }
a0d0e21e 6069 else if (!origlimit) {
c1a7495a
BB
6070 if (gimme_scalar) {
6071 iters -= trailing_empty;
6072 } else {
6073 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
6074 if (TOPs && !make_mortal)
6075 sv_2mortal(TOPs);
71ca73e5 6076 *SP-- = NULL;
c1a7495a
BB
6077 iters--;
6078 }
89900bd3 6079 }
a0d0e21e 6080 }
8ec5e241 6081
8b7059b1
DM
6082 PUTBACK;
6083 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
6084 SPAGAIN;
a0d0e21e 6085 if (realarray) {
8ec5e241 6086 if (!mg) {
1c0b011c
NIS
6087 if (SvSMAGICAL(ary)) {
6088 PUTBACK;
ad64d0ec 6089 mg_set(MUTABLE_SV(ary));
1c0b011c
NIS
6090 SPAGAIN;
6091 }
6092 if (gimme == G_ARRAY) {
6093 EXTEND(SP, iters);
6094 Copy(AvARRAY(ary), SP + 1, iters, SV*);
6095 SP += iters;
6096 RETURN;
6097 }
8ec5e241 6098 }
1c0b011c 6099 else {
fb73857a 6100 PUTBACK;
d343c3ef 6101 ENTER_with_name("call_PUSH");
36925d9e 6102 call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
d343c3ef 6103 LEAVE_with_name("call_PUSH");
fb73857a 6104 SPAGAIN;
8ec5e241 6105 if (gimme == G_ARRAY) {
c70927a6 6106 SSize_t i;
8ec5e241
NIS
6107 /* EXTEND should not be needed - we just popped them */
6108 EXTEND(SP, iters);
6109 for (i=0; i < iters; i++) {
6110 SV **svp = av_fetch(ary, i, FALSE);
3280af22 6111 PUSHs((svp) ? *svp : &PL_sv_undef);
8ec5e241 6112 }
1c0b011c
NIS
6113 RETURN;
6114 }
a0d0e21e
LW
6115 }
6116 }
6117 else {
6118 if (gimme == G_ARRAY)
6119 RETURN;
6120 }
7f18b612
YST
6121
6122 GETTARGET;
02c161ef 6123 XPUSHi(iters);
7f18b612 6124 RETURN;
79072805 6125}
85e6fe83 6126
c5917253
NC
6127PP(pp_once)
6128{
6129 dSP;
6130 SV *const sv = PAD_SVl(PL_op->op_targ);
6131
6132 if (SvPADSTALE(sv)) {
6133 /* First time. */
6134 SvPADSTALE_off(sv);
6135 RETURNOP(cLOGOP->op_other);
6136 }
6137 RETURNOP(cLOGOP->op_next);
6138}
6139
c0329465
MB
6140PP(pp_lock)
6141{
39644a26 6142 dSP;
c0329465 6143 dTOPss;
e55aaa0e 6144 SV *retsv = sv;
68795e93 6145 SvLOCK(sv);
f79aa60b
FC
6146 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
6147 || SvTYPE(retsv) == SVt_PVCV) {
e55aaa0e
MB
6148 retsv = refto(retsv);
6149 }
6150 SETs(retsv);
c0329465
MB
6151 RETURN;
6152}
a863c7d1 6153
65bca31a 6154
10088f56 6155/* used for: pp_padany(), pp_custom(); plus any system ops
b1c05ba5
DM
6156 * that aren't implemented on a particular platform */
6157
65bca31a
NC
6158PP(unimplemented_op)
6159{
361ed549
NC
6160 const Optype op_type = PL_op->op_type;
6161 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
6162 with out of range op numbers - it only "special" cases op_custom.
6163 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
6164 if we get here for a custom op then that means that the custom op didn't
6165 have an implementation. Given that OP_NAME() looks up the custom op
6166 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
6167 registers &PL_unimplemented_op as the address of their custom op.
6168 NULL doesn't generate a useful error message. "custom" does. */
6169 const char *const name = op_type >= OP_max
6170 ? "[out of range]" : PL_op_name[PL_op->op_type];
7627e6d0
NC
6171 if(OP_IS_SOCKET(op_type))
6172 DIE(aTHX_ PL_no_sock_func, name);
361ed549 6173 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
65bca31a
NC
6174}
6175
bea284c8
FC
6176static void
6177S_maybe_unwind_defav(pTHX)
6178{
6179 if (CX_CUR()->cx_type & CXp_HASARGS) {
6180 PERL_CONTEXT *cx = CX_CUR();
6181
6182 assert(CxHASARGS(cx));
6183 cx_popsub_args(cx);
6184 cx->cx_type &= ~CXp_HASARGS;
6185 }
6186}
6187
deb8a388
FC
6188/* For sorting out arguments passed to a &CORE:: subroutine */
6189PP(pp_coreargs)
6190{
6191 dSP;
7fa5bd9b 6192 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
498a02d8 6193 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
7fa5bd9b 6194 AV * const at_ = GvAV(PL_defgv);
0e80230d
FC
6195 SV **svp = at_ ? AvARRAY(at_) : NULL;
6196 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
7fa5bd9b 6197 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
46e00a91 6198 bool seen_question = 0;
7fa5bd9b 6199 const char *err = NULL;
3e6568b4 6200 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
7fa5bd9b 6201
46e00a91
FC
6202 /* Count how many args there are first, to get some idea how far to
6203 extend the stack. */
7fa5bd9b 6204 while (oa) {
bf0571fd 6205 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
7fa5bd9b 6206 maxargs++;
46e00a91
FC
6207 if (oa & OA_OPTIONAL) seen_question = 1;
6208 if (!seen_question) minargs++;
7fa5bd9b
FC
6209 oa >>= 4;
6210 }
6211
6212 if(numargs < minargs) err = "Not enough";
6213 else if(numargs > maxargs) err = "Too many";
6214 if (err)
6215 /* diag_listed_as: Too many arguments for %s */
6216 Perl_croak(aTHX_
6217 "%s arguments for %s", err,
2a90c7c6 6218 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
7fa5bd9b
FC
6219 );
6220
6221 /* Reset the stack pointer. Without this, we end up returning our own
6222 arguments in list context, in addition to the values we are supposed
6223 to return. nextstate usually does this on sub entry, but we need
e1fa07e3 6224 to run the next op with the caller's hints, so we cannot have a
7fa5bd9b 6225 nextstate. */
4ebe6e95 6226 SP = PL_stack_base + CX_CUR()->blk_oldsp;
7fa5bd9b 6227
46e00a91
FC
6228 if(!maxargs) RETURN;
6229
bf0571fd
FC
6230 /* We do this here, rather than with a separate pushmark op, as it has
6231 to come in between two things this function does (stack reset and
6232 arg pushing). This seems the easiest way to do it. */
3e6568b4 6233 if (pushmark) {
bf0571fd
FC
6234 PUTBACK;
6235 (void)Perl_pp_pushmark(aTHX);
6236 }
6237
6238 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
c931b036 6239 PUTBACK; /* The code below can die in various places. */
46e00a91
FC
6240
6241 oa = PL_opargs[opnum] >> OASHIFT;
3e6568b4 6242 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
c931b036 6243 whicharg++;
46e00a91
FC
6244 switch (oa & 7) {
6245 case OA_SCALAR:
1efec5ed 6246 try_defsv:
d6d78e19 6247 if (!numargs && defgv && whicharg == minargs + 1) {
195eefec 6248 PUSHs(DEFSV);
d6d78e19
FC
6249 }
6250 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
46e00a91 6251 break;
bf0571fd
FC
6252 case OA_LIST:
6253 while (numargs--) {
6254 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
6255 svp++;
6256 }
6257 RETURN;
bea284c8
FC
6258 case OA_AVREF:
6259 if (!numargs) {
6260 GV *gv;
6261 if (CvUNIQUE(find_runcv_where(FIND_RUNCV_level_eq,1,NULL)))
6262 gv = PL_argvgv;
6263 else {
6264 S_maybe_unwind_defav(aTHX);
6265 gv = PL_defgv;
6266 }
6267 PUSHs((SV *)GvAVn(gv));
6268 break;
6269 }
6270 if (!svp || !*svp || !SvROK(*svp)
6271 || SvTYPE(SvRV(*svp)) != SVt_PVAV)
6272 DIE(aTHX_
6273 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
6274 "Type of arg %d to &CORE::%s must be array reference",
6275 whicharg, PL_op_desc[opnum]
6276 );
6277 PUSHs(SvRV(*svp));
6278 break;
19c481f4
FC
6279 case OA_HVREF:
6280 if (!svp || !*svp || !SvROK(*svp)
73665bc4
FC
6281 || ( SvTYPE(SvRV(*svp)) != SVt_PVHV
6282 && ( opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
6283 || SvTYPE(SvRV(*svp)) != SVt_PVAV )))
19c481f4
FC
6284 DIE(aTHX_
6285 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
73665bc4
FC
6286 "Type of arg %d to &CORE::%s must be hash%s reference",
6287 whicharg, PL_op_desc[opnum],
6288 opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
6289 ? ""
6290 : " or array"
19c481f4
FC
6291 );
6292 PUSHs(SvRV(*svp));
6293 break;
c931b036 6294 case OA_FILEREF:
30901a8a
FC
6295 if (!numargs) PUSHs(NULL);
6296 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
c931b036
FC
6297 /* no magic here, as the prototype will have added an extra
6298 refgen and we just want what was there before that */
6299 PUSHs(SvRV(*svp));
6300 else {
6301 const bool constr = PL_op->op_private & whicharg;
6302 PUSHs(S_rv2gv(aTHX_
6303 svp && *svp ? *svp : &PL_sv_undef,
b54f893d 6304 constr, cBOOL(CopHINTS_get(PL_curcop) & HINT_STRICT_REFS),
c931b036
FC
6305 !constr
6306 ));
6307 }
6308 break;
c72a5629 6309 case OA_SCALARREF:
1efec5ed
FC
6310 if (!numargs) goto try_defsv;
6311 else {
17008668
FC
6312 const bool wantscalar =
6313 PL_op->op_private & OPpCOREARGS_SCALARMOD;
c72a5629 6314 if (!svp || !*svp || !SvROK(*svp)
17008668
FC
6315 /* We have to permit globrefs even for the \$ proto, as
6316 *foo is indistinguishable from ${\*foo}, and the proto-
6317 type permits the latter. */
6318 || SvTYPE(SvRV(*svp)) > (
efe889ae 6319 wantscalar ? SVt_PVLV
46bef06f
FC
6320 : opnum == OP_LOCK || opnum == OP_UNDEF
6321 ? SVt_PVCV
efe889ae 6322 : SVt_PVHV
17008668 6323 )
c72a5629
FC
6324 )
6325 DIE(aTHX_
17008668 6326 "Type of arg %d to &CORE::%s must be %s",
46bef06f 6327 whicharg, PL_op_name[opnum],
17008668
FC
6328 wantscalar
6329 ? "scalar reference"
46bef06f 6330 : opnum == OP_LOCK || opnum == OP_UNDEF
efe889ae
FC
6331 ? "reference to one of [$@%&*]"
6332 : "reference to one of [$@%*]"
c72a5629
FC
6333 );
6334 PUSHs(SvRV(*svp));
bea284c8 6335 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv) {
88bb468b 6336 /* Undo @_ localisation, so that sub exit does not undo
04e686b8 6337 part of our undeffing. */
bea284c8 6338 S_maybe_unwind_defav(aTHX);
88bb468b 6339 }
17008668 6340 }
1efec5ed 6341 break;
46e00a91 6342 default:
46e00a91
FC
6343 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
6344 }
6345 oa = oa >> 4;
6346 }
6347
deb8a388
FC
6348 RETURN;
6349}
6350
a2232057
DM
6351/* Implement CORE::keys(),values(),each().
6352 *
6353 * We won't know until run-time whether the arg is an array or hash,
6354 * so this op calls
6355 *
6356 * pp_keys/pp_values/pp_each
6357 * or
6358 * pp_akeys/pp_avalues/pp_aeach
6359 *
6360 * as appropriate (or whatever pp function actually implements the OP_FOO
6361 * functionality for each FOO).
6362 */
6363
88101882
FC
6364PP(pp_avhvswitch)
6365{
a73158aa 6366 dVAR; dSP;
73665bc4
FC
6367 return PL_ppaddr[
6368 (SvTYPE(TOPs) == SVt_PVAV ? OP_AEACH : OP_EACH)
e1e26374 6369 + (PL_op->op_private & OPpAVHVSWITCH_MASK)
73665bc4 6370 ](aTHX);
88101882
FC
6371}
6372
84ed0108
FC
6373PP(pp_runcv)
6374{
6375 dSP;
6376 CV *cv;
6377 if (PL_op->op_private & OPpOFFBYONE) {
db4cf31d 6378 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
84ed0108
FC
6379 }
6380 else cv = find_runcv(NULL);
e157a82b 6381 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
84ed0108
FC
6382 RETURN;
6383}
6384
05a34802 6385static void
2331e434 6386S_localise_aelem_lval(pTHX_ AV * const av, SV * const keysv,
05a34802
FC
6387 const bool can_preserve)
6388{
2331e434 6389 const SSize_t ix = SvIV(keysv);
05a34802
FC
6390 if (can_preserve ? av_exists(av, ix) : TRUE) {
6391 SV ** const svp = av_fetch(av, ix, 1);
6392 if (!svp || !*svp)
6393 Perl_croak(aTHX_ PL_no_aelem, ix);
6394 save_aelem(av, ix, svp);
6395 }
6396 else
6397 SAVEADELETE(av, ix);
6398}
6399
5f94141d
FC
6400static void
6401S_localise_helem_lval(pTHX_ HV * const hv, SV * const keysv,
6402 const bool can_preserve)
6403{
6404 if (can_preserve ? hv_exists_ent(hv, keysv, 0) : TRUE) {
6405 HE * const he = hv_fetch_ent(hv, keysv, 1, 0);
6406 SV ** const svp = he ? &HeVAL(he) : NULL;
6407 if (!svp || !*svp)
6408 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(keysv));
6409 save_helem_flags(hv, keysv, svp, 0);
6410 }
6411 else
6412 SAVEHDELETE(hv, keysv);
6413}
6414
9782ce69
FC
6415static void
6416S_localise_gv_slot(pTHX_ GV *gv, U8 type)
6417{
6418 if (type == OPpLVREF_SV) {
6419 save_pushptrptr(gv, SvREFCNT_inc_simple(GvSV(gv)), SAVEt_GVSV);
6420 GvSV(gv) = 0;
6421 }
6422 else if (type == OPpLVREF_AV)
6423 /* XXX Inefficient, as it creates a new AV, which we are
6424 about to clobber. */
6425 save_ary(gv);
6426 else {
6427 assert(type == OPpLVREF_HV);
6428 /* XXX Likewise inefficient. */
6429 save_hash(gv);
6430 }
6431}
6432
6433
254da51f
FC
6434PP(pp_refassign)
6435{
4fec8804 6436 dSP;
6102323a 6437 SV * const key = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
d8a875d9 6438 SV * const left = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
4fec8804 6439 dTOPss;
3f114923 6440 const char *bad = NULL;
ac0da85a 6441 const U8 type = PL_op->op_private & OPpLVREF_TYPE;
4fec8804 6442 if (!SvROK(sv)) DIE(aTHX_ "Assigned value is not a reference");
ac0da85a 6443 switch (type) {
3f114923
FC
6444 case OPpLVREF_SV:
6445 if (SvTYPE(SvRV(sv)) > SVt_PVLV)
6446 bad = " SCALAR";
6447 break;
6448 case OPpLVREF_AV:
6449 if (SvTYPE(SvRV(sv)) != SVt_PVAV)
6450 bad = "n ARRAY";
6451 break;
6452 case OPpLVREF_HV:
6453 if (SvTYPE(SvRV(sv)) != SVt_PVHV)
6454 bad = " HASH";
6455 break;
6456 case OPpLVREF_CV:
6457 if (SvTYPE(SvRV(sv)) != SVt_PVCV)
6458 bad = " CODE";
6459 }
6460 if (bad)
1f8155a2 6461 /* diag_listed_as: Assigned value is not %s reference */
3f114923 6462 DIE(aTHX_ "Assigned value is not a%s reference", bad);
b943805e
JH
6463 {
6464 MAGIC *mg;
6465 HV *stash;
d8a875d9
FC
6466 switch (left ? SvTYPE(left) : 0) {
6467 case 0:
cf5d2d91
FC
6468 {
6469 SV * const old = PAD_SV(ARGTARG);
d8a875d9 6470 PAD_SETSV(ARGTARG, SvREFCNT_inc_NN(SvRV(sv)));
cf5d2d91 6471 SvREFCNT_dec(old);
3ad7d304
FC
6472 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
6473 == OPpLVAL_INTRO)
fc048fcf 6474 SAVECLEARSV(PAD_SVl(ARGTARG));
d8a875d9 6475 break;
cf5d2d91 6476 }
d8a875d9 6477 case SVt_PVGV:
2a57afb1 6478 if (PL_op->op_private & OPpLVAL_INTRO) {
9782ce69 6479 S_localise_gv_slot(aTHX_ (GV *)left, type);
2a57afb1 6480 }
d8a875d9
FC
6481 gv_setref(left, sv);
6482 SvSETMAGIC(left);
6102323a
FC
6483 break;
6484 case SVt_PVAV:
69a23520 6485 assert(key);
40d2b828 6486 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
2331e434 6487 S_localise_aelem_lval(aTHX_ (AV *)left, key,
05a34802 6488 SvCANEXISTDELETE(left));
40d2b828 6489 }
6102323a
FC
6490 av_store((AV *)left, SvIV(key), SvREFCNT_inc_simple_NN(SvRV(sv)));
6491 break;
5f94141d 6492 case SVt_PVHV:
69a23520
JH
6493 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
6494 assert(key);
5f94141d
FC
6495 S_localise_helem_lval(aTHX_ (HV *)left, key,
6496 SvCANEXISTDELETE(left));
69a23520 6497 }
7fcb36d5 6498 (void)hv_store_ent((HV *)left, key, SvREFCNT_inc_simple_NN(SvRV(sv)), 0);
d8a875d9 6499 }
4fec8804
FC
6500 if (PL_op->op_flags & OPf_MOD)
6501 SETs(sv_2mortal(newSVsv(sv)));
6502 /* XXX else can weak references go stale before they are read, e.g.,
6503 in leavesub? */
6504 RETURN;
b943805e 6505 }
254da51f
FC
6506}
6507
4c5bab50
FC
6508PP(pp_lvref)
6509{
26a50d99
FC
6510 dSP;
6511 SV * const ret = sv_2mortal(newSV_type(SVt_PVMG));
6102323a 6512 SV * const elem = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
2a57afb1 6513 SV * const arg = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
9782ce69
FC
6514 MAGIC * const mg = sv_magicext(ret, arg, PERL_MAGIC_lvref,
6515 &PL_vtbl_lvref, (char *)elem,
23270f96 6516 elem ? HEf_SVKEY : (I32)ARGTARG);
9782ce69 6517 mg->mg_private = PL_op->op_private;
d39c26a6
FC
6518 if (PL_op->op_private & OPpLVREF_ITER)
6519 mg->mg_flags |= MGf_PERSIST;
9846cd95 6520 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
40d2b828 6521 if (elem) {
38bb0011
JH
6522 MAGIC *mg;
6523 HV *stash;
6524 assert(arg);
6525 {
6526 const bool can_preserve = SvCANEXISTDELETE(arg);
6527 if (SvTYPE(arg) == SVt_PVAV)
6528 S_localise_aelem_lval(aTHX_ (AV *)arg, elem, can_preserve);
6529 else
6530 S_localise_helem_lval(aTHX_ (HV *)arg, elem, can_preserve);
6531 }
40d2b828
FC
6532 }
6533 else if (arg) {
9782ce69
FC
6534 S_localise_gv_slot(aTHX_ (GV *)arg,
6535 PL_op->op_private & OPpLVREF_TYPE);
2a57afb1 6536 }
3ad7d304 6537 else if (!(PL_op->op_private & OPpPAD_STATE))
c146a62a 6538 SAVECLEARSV(PAD_SVl(ARGTARG));
1199b01a 6539 }
c146a62a
FC
6540 XPUSHs(ret);
6541 RETURN;
4c5bab50 6542}
84ed0108 6543
16b99412
FC
6544PP(pp_lvrefslice)
6545{
a95dad8a 6546 dSP; dMARK;
0ca7b7f7
FC
6547 AV * const av = (AV *)POPs;
6548 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
6549 bool can_preserve = FALSE;
6550
9846cd95 6551 if (UNLIKELY(localizing)) {
0ca7b7f7
FC
6552 MAGIC *mg;
6553 HV *stash;
6554 SV **svp;
6555
6556 can_preserve = SvCANEXISTDELETE(av);
6557
6558 if (SvTYPE(av) == SVt_PVAV) {
6559 SSize_t max = -1;
6560
6561 for (svp = MARK + 1; svp <= SP; svp++) {
6562 const SSize_t elem = SvIV(*svp);
6563 if (elem > max)
6564 max = elem;
6565 }
6566 if (max > AvMAX(av))
6567 av_extend(av, max);
6568 }
6569 }
6570
6571 while (++MARK <= SP) {
6572 SV * const elemsv = *MARK;
5f94141d 6573 if (SvTYPE(av) == SVt_PVAV)
2331e434 6574 S_localise_aelem_lval(aTHX_ av, elemsv, can_preserve);
5f94141d
FC
6575 else
6576 S_localise_helem_lval(aTHX_ (HV *)av, elemsv, can_preserve);
0ca7b7f7
FC
6577 *MARK = sv_2mortal(newSV_type(SVt_PVMG));
6578 sv_magic(*MARK,(SV *)av,PERL_MAGIC_lvref,(char *)elemsv,HEf_SVKEY);
6579 }
6580 RETURN;
16b99412
FC
6581}
6582
2882b3ff
FC
6583PP(pp_lvavref)
6584{
bdaf10a5
FC
6585 if (PL_op->op_flags & OPf_STACKED)
6586 Perl_pp_rv2av(aTHX);
6587 else
6588 Perl_pp_padav(aTHX);
6589 {
6590 dSP;
6591 dTOPss;
6592 SETs(0); /* special alias marker that aassign recognises */
6593 XPUSHs(sv);
6594 RETURN;
6595 }
2882b3ff
FC
6596}
6597
b77472f9
FC
6598PP(pp_anonconst)
6599{
6600 dSP;
6601 dTOPss;
6602 SETs(sv_2mortal((SV *)newCONSTSUB(SvTYPE(CopSTASH(PL_curcop))==SVt_PVHV
6603 ? CopSTASH(PL_curcop)
6604 : NULL,
6605 NULL, SvREFCNT_inc_simple_NN(sv))));
6606 RETURN;
6607}
6608
4fa06845
DM
6609
6610/* process one subroutine argument - typically when the sub has a signature:
6611 * introduce PL_curpad[op_targ] and assign to it the value
6612 * for $: (OPf_STACKED ? *sp : $_[N])
6613 * for @/%: @_[N..$#_]
6614 *
6615 * It's equivalent to
6616 * my $foo = $_[N];
6617 * or
6618 * my $foo = (value-on-stack)
6619 * or
6620 * my @foo = @_[N..$#_]
6621 * etc
4fa06845
DM
6622 */
6623
6624PP(pp_argelem)
6625{
6626 dTARG;
6627 SV *val;
6628 SV ** padentry;
6629 OP *o = PL_op;
6630 AV *defav = GvAV(PL_defgv); /* @_ */
6daeaaa3 6631 IV ix = PTR2IV(cUNOP_AUXo->op_aux);
4fa06845 6632 IV argc;
4fa06845
DM
6633
6634 /* do 'my $var, @var or %var' action */
6635 padentry = &(PAD_SVl(o->op_targ));
6636 save_clearsv(padentry);
6637 targ = *padentry;
6638
6639 if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_SV) {
6640 if (o->op_flags & OPf_STACKED) {
6641 dSP;
6642 val = POPs;
6643 PUTBACK;
6644 }
6645 else {
f6ca42c7 6646 SV **svp;
4fa06845 6647 /* should already have been checked */
f6ca42c7 6648 assert(ix >= 0);
6daeaaa3
DM
6649#if IVSIZE > PTRSIZE
6650 assert(ix <= SSize_t_MAX);
6651#endif
f6ca42c7
DM
6652
6653 svp = av_fetch(defav, ix, FALSE);
6654 val = svp ? *svp : &PL_sv_undef;
4fa06845
DM
6655 }
6656
6657 /* $var = $val */
6658
6659 /* cargo-culted from pp_sassign */
6660 assert(TAINTING_get || !TAINT_get);
6661 if (UNLIKELY(TAINT_get) && !SvTAINTED(val))
6662 TAINT_NOT;
6663
f6ca42c7 6664 SvSetMagicSV(targ, val);
4fa06845
DM
6665 return o->op_next;
6666 }
6667
6668 /* must be AV or HV */
6669
6670 assert(!(o->op_flags & OPf_STACKED));
f6ca42c7 6671 argc = ((IV)AvFILL(defav) + 1) - ix;
4fa06845
DM
6672
6673 /* This is a copy of the relevant parts of pp_aassign().
4fa06845
DM
6674 */
6675 if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_AV) {
f6ca42c7
DM
6676 IV i;
6677
6678 if (AvFILL((AV*)targ) > -1) {
6679 /* target should usually be empty. If we get get
6680 * here, someone's been doing some weird closure tricks.
6681 * Make a copy of all args before clearing the array,
6682 * to avoid the equivalent of @a = ($a[0]) prematurely freeing
6683 * elements. See similar code in pp_aassign.
6684 */
6685 for (i = 0; i < argc; i++) {
6686 SV **svp = av_fetch(defav, ix + i, FALSE);
6687 SV *newsv = newSV(0);
6688 sv_setsv_flags(newsv,
6689 svp ? *svp : &PL_sv_undef,
6690 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
6691 if (!av_store(defav, ix + i, newsv))
6692 SvREFCNT_dec_NN(newsv);
6693 }
6694 av_clear((AV*)targ);
6695 }
6696
6697 if (argc <= 0)
6698 return o->op_next;
4fa06845 6699
4fa06845
DM
6700 av_extend((AV*)targ, argc);
6701
f6ca42c7 6702 i = 0;
4fa06845
DM
6703 while (argc--) {
6704 SV *tmpsv;
f6ca42c7
DM
6705 SV **svp = av_fetch(defav, ix + i, FALSE);
6706 SV *val = svp ? *svp : &PL_sv_undef;
4fa06845 6707 tmpsv = newSV(0);
f6ca42c7 6708 sv_setsv(tmpsv, val);
4fa06845
DM
6709 av_store((AV*)targ, i++, tmpsv);
6710 TAINT_NOT;
6711 }
6712
6713 }
6714 else {
f6ca42c7
DM
6715 IV i;
6716
4fa06845
DM
6717 assert((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_HV);
6718
f6ca42c7
DM
6719 if (SvRMAGICAL(targ) || HvUSEDKEYS((HV*)targ)) {
6720 /* see "target should usually be empty" comment above */
6721 for (i = 0; i < argc; i++) {
6722 SV **svp = av_fetch(defav, ix + i, FALSE);
6723 SV *newsv = newSV(0);
6724 sv_setsv_flags(newsv,
6725 svp ? *svp : &PL_sv_undef,
6726 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
6727 if (!av_store(defav, ix + i, newsv))
6728 SvREFCNT_dec_NN(newsv);
6729 }
6730 hv_clear((HV*)targ);
6731 }
6732
6733 if (argc <= 0)
6734 return o->op_next;
4fa06845 6735 assert(argc % 2 == 0);
4fa06845 6736
f6ca42c7 6737 i = 0;
4fa06845
DM
6738 while (argc) {
6739 SV *tmpsv;
f6ca42c7
DM
6740 SV **svp;
6741 SV *key;
6742 SV *val;
6743
6744 svp = av_fetch(defav, ix + i++, FALSE);
6745 key = svp ? *svp : &PL_sv_undef;
6746 svp = av_fetch(defav, ix + i++, FALSE);
6747 val = svp ? *svp : &PL_sv_undef;
4fa06845 6748
4fa06845
DM
6749 argc -= 2;
6750 if (UNLIKELY(SvGMAGICAL(key)))
6751 key = sv_mortalcopy(key);
6752 tmpsv = newSV(0);
6753 sv_setsv(tmpsv, val);
6754 hv_store_ent((HV*)targ, key, tmpsv, 0);
6755 TAINT_NOT;
6756 }
6757 }
6758
6759 return o->op_next;
6760}
6761
6762/* Handle a default value for one subroutine argument (typically as part
6763 * of a subroutine signature).
6764 * It's equivalent to
6765 * @_ > op_targ ? $_[op_targ] : result_of(op_other)
6766 *
6767 * Intended to be used where op_next is an OP_ARGELEM
6768 *
6769 * We abuse the op_targ field slightly: it's an index into @_ rather than
6770 * into PL_curpad.
6771 */
6772
6773PP(pp_argdefelem)
6774{
6775 OP * const o = PL_op;
6776 AV *defav = GvAV(PL_defgv); /* @_ */
6daeaaa3 6777 IV ix = (IV)o->op_targ;
4fa06845 6778
f6ca42c7 6779 assert(ix >= 0);
6daeaaa3
DM
6780#if IVSIZE > PTRSIZE
6781 assert(ix <= SSize_t_MAX);
6782#endif
f6ca42c7
DM
6783
6784 if (AvFILL(defav) >= ix) {
4fa06845 6785 dSP;
f6ca42c7
DM
6786 SV **svp = av_fetch(defav, ix, FALSE);
6787 SV *val = svp ? *svp : &PL_sv_undef;
6788 XPUSHs(val);
4fa06845
DM
6789 RETURN;
6790 }
6791 return cLOGOPo->op_other;
6792}
6793
6794
ac7609e4
AC
6795static SV *
6796S_find_runcv_name(void)
6797{
6798 dTHX;
6799 CV *cv;
6800 GV *gv;
6801 SV *sv;
6802
6803 cv = find_runcv(0);
6804 if (!cv)
6805 return &PL_sv_no;
6806
6807 gv = CvGV(cv);
6808 if (!gv)
6809 return &PL_sv_no;
6810
6811 sv = sv_2mortal(newSV(0));
6812 gv_fullname4(sv, gv, NULL, TRUE);
6813 return sv;
6814}
4fa06845
DM
6815
6816/* Check a a subs arguments - i.e. that it has the correct number of args
6817 * (and anything else we might think of in future). Typically used with
6818 * signatured subs.
6819 */
6820
6821PP(pp_argcheck)
6822{
6823 OP * const o = PL_op;
6824 UNOP_AUX_item *aux = cUNOP_AUXo->op_aux;
6daeaaa3
DM
6825 IV params = aux[0].iv;
6826 IV opt_params = aux[1].iv;
4fa06845
DM
6827 char slurpy = (char)(aux[2].iv);
6828 AV *defav = GvAV(PL_defgv); /* @_ */
6daeaaa3 6829 IV argc;
4fa06845
DM
6830 bool too_few;
6831
6832 assert(!SvMAGICAL(defav));
6daeaaa3 6833 argc = (AvFILLp(defav) + 1);
4fa06845
DM
6834 too_few = (argc < (params - opt_params));
6835
6836 if (UNLIKELY(too_few || (!slurpy && argc > params)))
ac7609e4
AC
6837 /* diag_listed_as: Too few arguments for subroutine '%s' */
6838 /* diag_listed_as: Too many arguments for subroutine '%s' */
6839 Perl_croak_caller("Too %s arguments for subroutine '%" SVf "'",
6840 too_few ? "few" : "many", S_find_runcv_name());
4fa06845
DM
6841
6842 if (UNLIKELY(slurpy == '%' && argc > params && (argc - params) % 2))
ac7609e4
AC
6843 /* diag_listed_as: Odd name/value argument for subroutine '%s' */
6844 Perl_croak_caller("Odd name/value argument for subroutine '%" SVf "'",
6845 S_find_runcv_name());
4fa06845
DM
6846
6847 return NORMAL;
6848}
6849
e609e586 6850/*
14d04a33 6851 * ex: set ts=8 sts=4 sw=4 et:
37442d52 6852 */