This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add core_prototype; make pp_prototype use it
[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
JH
31#include "reentr.h"
32
dfe9444c
AD
33/* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
35 --AD 2/20/1998
36*/
37#ifdef NEED_GETPID_PROTO
38extern Pid_t getpid (void);
8ac85365
NIS
39#endif
40
0630166f
SP
41/*
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
44 */
45#if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
47#endif
48
13017935
SM
49/* variations on pp_null */
50
93a17b20
LW
51PP(pp_stub)
52{
97aff369 53 dVAR;
39644a26 54 dSP;
54310121 55 if (GIMME_V == G_SCALAR)
3280af22 56 XPUSHs(&PL_sv_undef);
93a17b20
LW
57 RETURN;
58}
59
79072805
LW
60/* Pushy stuff. */
61
93a17b20
LW
62PP(pp_padav)
63{
97aff369 64 dVAR; dSP; dTARGET;
13017935 65 I32 gimme;
e190e9b4 66 assert(SvTYPE(TARG) == SVt_PVAV);
533c011a 67 if (PL_op->op_private & OPpLVAL_INTRO)
a5911867
RGS
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
85e6fe83 70 EXTEND(SP, 1);
533c011a 71 if (PL_op->op_flags & OPf_REF) {
85e6fe83 72 PUSHs(TARG);
93a17b20 73 RETURN;
40c94d11
FC
74 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
75 const I32 flags = is_lvalue_sub();
76 if (flags && !(flags & OPpENTERSUB_INARGS)) {
78f9721b
SM
77 if (GIMME == G_SCALAR)
78 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
79 PUSHs(TARG);
80 RETURN;
40c94d11 81 }
85e6fe83 82 }
13017935
SM
83 gimme = GIMME_V;
84 if (gimme == G_ARRAY) {
502c6561 85 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
85e6fe83 86 EXTEND(SP, maxarg);
93965878
NIS
87 if (SvMAGICAL(TARG)) {
88 U32 i;
eb160463 89 for (i=0; i < (U32)maxarg; i++) {
502c6561 90 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
3280af22 91 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
93965878
NIS
92 }
93 }
94 else {
502c6561 95 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
93965878 96 }
85e6fe83
LW
97 SP += maxarg;
98 }
13017935 99 else if (gimme == G_SCALAR) {
1b6737cc 100 SV* const sv = sv_newmortal();
502c6561 101 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
85e6fe83
LW
102 sv_setiv(sv, maxarg);
103 PUSHs(sv);
104 }
105 RETURN;
93a17b20
LW
106}
107
108PP(pp_padhv)
109{
97aff369 110 dVAR; dSP; dTARGET;
54310121 111 I32 gimme;
112
e190e9b4 113 assert(SvTYPE(TARG) == SVt_PVHV);
93a17b20 114 XPUSHs(TARG);
533c011a 115 if (PL_op->op_private & OPpLVAL_INTRO)
a5911867
RGS
116 if (!(PL_op->op_private & OPpPAD_STATE))
117 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
533c011a 118 if (PL_op->op_flags & OPf_REF)
93a17b20 119 RETURN;
40c94d11
FC
120 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
121 const I32 flags = is_lvalue_sub();
122 if (flags && !(flags & OPpENTERSUB_INARGS)) {
78f9721b
SM
123 if (GIMME == G_SCALAR)
124 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
125 RETURN;
40c94d11 126 }
78f9721b 127 }
54310121 128 gimme = GIMME_V;
129 if (gimme == G_ARRAY) {
981b7185 130 RETURNOP(Perl_do_kv(aTHX));
85e6fe83 131 }
54310121 132 else if (gimme == G_SCALAR) {
85fbaab2 133 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
85e6fe83 134 SETs(sv);
85e6fe83 135 }
54310121 136 RETURN;
93a17b20
LW
137}
138
79072805
LW
139/* Translations. */
140
4bdf8368 141static const char S_no_symref_sv[] =
def89bff
NC
142 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
143
79072805
LW
144PP(pp_rv2gv)
145{
97aff369 146 dVAR; dSP; dTOPss;
8ec5e241 147
f64c9ac5 148 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
ed6116ce 149 if (SvROK(sv)) {
a0d0e21e 150 wasref:
93d7320b
DM
151 if (SvAMAGIC(sv)) {
152 sv = amagic_deref_call(sv, to_gv_amg);
153 SPAGAIN;
154 }
ed6116ce 155 sv = SvRV(sv);
b1dadf13 156 if (SvTYPE(sv) == SVt_PVIO) {
159b6efe 157 GV * const gv = MUTABLE_GV(sv_newmortal());
b1dadf13 158 gv_init(gv, 0, "", 0, 0);
a45c7426 159 GvIOp(gv) = MUTABLE_IO(sv);
b37c2d43 160 SvREFCNT_inc_void_NN(sv);
ad64d0ec 161 sv = MUTABLE_SV(gv);
ef54e1a4 162 }
6e592b3a 163 else if (!isGV_with_GP(sv))
cea2e8a9 164 DIE(aTHX_ "Not a GLOB reference");
79072805
LW
165 }
166 else {
6e592b3a 167 if (!isGV_with_GP(sv)) {
afd1915d 168 if (!SvOK(sv) && sv != &PL_sv_undef) {
b13b2135 169 /* If this is a 'my' scalar and flag is set then vivify
853846ea 170 * NI-S 1999/05/07
b13b2135 171 */
ac53db4c 172 if (SvREADONLY(sv))
6ad8f254 173 Perl_croak_no_modify(aTHX);
1d8d4d2a 174 if (PL_op->op_private & OPpDEREF) {
2c8ac474
GS
175 GV *gv;
176 if (cUNOP->op_targ) {
177 STRLEN len;
0bd48802
AL
178 SV * const namesv = PAD_SV(cUNOP->op_targ);
179 const char * const name = SvPV(namesv, len);
159b6efe 180 gv = MUTABLE_GV(newSV(0));
2c8ac474
GS
181 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
182 }
183 else {
0bd48802 184 const char * const name = CopSTASHPV(PL_curcop);
2c8ac474 185 gv = newGVgen(name);
1d8d4d2a 186 }
43230e26 187 prepare_SV_for_RV(sv);
ad64d0ec 188 SvRV_set(sv, MUTABLE_SV(gv));
853846ea 189 SvROK_on(sv);
1d8d4d2a 190 SvSETMAGIC(sv);
853846ea 191 goto wasref;
2c8ac474 192 }
533c011a
NIS
193 if (PL_op->op_flags & OPf_REF ||
194 PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 195 DIE(aTHX_ PL_no_usym, "a symbol");
599cee73 196 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 197 report_uninit(sv);
a0d0e21e
LW
198 RETSETUNDEF;
199 }
35cd451c
GS
200 if ((PL_op->op_flags & OPf_SPECIAL) &&
201 !(PL_op->op_flags & OPf_MOD))
202 {
ad64d0ec 203 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
7a5fd60d
NC
204 if (!temp
205 && (!is_gv_magical_sv(sv,0)
ad64d0ec
NC
206 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
207 SVt_PVGV))))) {
35cd451c 208 RETSETUNDEF;
c9d5ac95 209 }
7a5fd60d 210 sv = temp;
35cd451c
GS
211 }
212 else {
213 if (PL_op->op_private & HINT_STRICT_REFS)
10b53e54 214 DIE(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), "a symbol");
e26df76a
NC
215 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
216 == OPpDONT_INIT_GV) {
217 /* We are the target of a coderef assignment. Return
218 the scalar unchanged, and let pp_sasssign deal with
219 things. */
220 RETURN;
221 }
7ffa7e75
FC
222 {
223 STRLEN len;
224 const char * const nambeg = SvPV_nomg_const(sv, len);
225 sv = MUTABLE_SV(
226 gv_fetchpvn_flags(
227 nambeg, len, GV_ADD | SvUTF8(sv), SVt_PVGV
228 )
229 );
230 }
35cd451c 231 }
2acc3314
FC
232 /* FAKE globs in the symbol table cause weird bugs (#77810) */
233 if (sv) SvFAKE_off(sv);
93a17b20 234 }
79072805 235 }
2acc3314
FC
236 if (sv && SvFAKE(sv)) {
237 SV *newsv = sv_newmortal();
5cf4b255 238 sv_setsv_flags(newsv, sv, 0);
2acc3314 239 SvFAKE_off(newsv);
d8906c05 240 sv = newsv;
2acc3314 241 }
d8906c05
FC
242 if (PL_op->op_private & OPpLVAL_INTRO)
243 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
244 SETs(sv);
79072805
LW
245 RETURN;
246}
247
dc3c76f8
NC
248/* Helper function for pp_rv2sv and pp_rv2av */
249GV *
fe9845cc
RB
250Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
251 const svtype type, SV ***spp)
dc3c76f8
NC
252{
253 dVAR;
254 GV *gv;
255
7918f24d
NC
256 PERL_ARGS_ASSERT_SOFTREF2XV;
257
dc3c76f8
NC
258 if (PL_op->op_private & HINT_STRICT_REFS) {
259 if (SvOK(sv))
10b53e54 260 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
dc3c76f8
NC
261 else
262 Perl_die(aTHX_ PL_no_usym, what);
263 }
264 if (!SvOK(sv)) {
fd1d9b5c
FC
265 if (
266 PL_op->op_flags & OPf_REF &&
267 PL_op->op_next->op_type != OP_BOOLKEYS
268 )
dc3c76f8
NC
269 Perl_die(aTHX_ PL_no_usym, what);
270 if (ckWARN(WARN_UNINITIALIZED))
271 report_uninit(sv);
272 if (type != SVt_PV && GIMME_V == G_ARRAY) {
273 (*spp)--;
274 return NULL;
275 }
276 **spp = &PL_sv_undef;
277 return NULL;
278 }
279 if ((PL_op->op_flags & OPf_SPECIAL) &&
280 !(PL_op->op_flags & OPf_MOD))
281 {
81e3fc25 282 gv = gv_fetchsv(sv, 0, type);
dc3c76f8
NC
283 if (!gv
284 && (!is_gv_magical_sv(sv,0)
81e3fc25 285 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
dc3c76f8
NC
286 {
287 **spp = &PL_sv_undef;
288 return NULL;
289 }
290 }
291 else {
7ffa7e75
FC
292 STRLEN len;
293 const char * const nambeg = SvPV_nomg_const(sv, len);
294 gv = gv_fetchpvn_flags(nambeg, len, GV_ADD | SvUTF8(sv), type);
dc3c76f8
NC
295 }
296 return gv;
297}
298
79072805
LW
299PP(pp_rv2sv)
300{
97aff369 301 dVAR; dSP; dTOPss;
c445ea15 302 GV *gv = NULL;
79072805 303
0824d667
DM
304 if (!(PL_op->op_private & OPpDEREFed))
305 SvGETMAGIC(sv);
ed6116ce 306 if (SvROK(sv)) {
93d7320b
DM
307 if (SvAMAGIC(sv)) {
308 sv = amagic_deref_call(sv, to_sv_amg);
309 SPAGAIN;
310 }
f5284f61 311
ed6116ce 312 sv = SvRV(sv);
79072805
LW
313 switch (SvTYPE(sv)) {
314 case SVt_PVAV:
315 case SVt_PVHV:
316 case SVt_PVCV:
cbae9b9f
YST
317 case SVt_PVFM:
318 case SVt_PVIO:
cea2e8a9 319 DIE(aTHX_ "Not a SCALAR reference");
42d0e0b7 320 default: NOOP;
79072805
LW
321 }
322 }
323 else {
159b6efe 324 gv = MUTABLE_GV(sv);
748a9306 325
6e592b3a 326 if (!isGV_with_GP(gv)) {
dc3c76f8
NC
327 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
328 if (!gv)
329 RETURN;
463ee0b2 330 }
29c711a3 331 sv = GvSVn(gv);
a0d0e21e 332 }
533c011a 333 if (PL_op->op_flags & OPf_MOD) {
82d03984
RGS
334 if (PL_op->op_private & OPpLVAL_INTRO) {
335 if (cUNOP->op_first->op_type == OP_NULL)
159b6efe 336 sv = save_scalar(MUTABLE_GV(TOPs));
82d03984
RGS
337 else if (gv)
338 sv = save_scalar(gv);
339 else
f1f66076 340 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
82d03984 341 }
533c011a
NIS
342 else if (PL_op->op_private & OPpDEREF)
343 vivify_ref(sv, PL_op->op_private & OPpDEREF);
79072805 344 }
a0d0e21e 345 SETs(sv);
79072805
LW
346 RETURN;
347}
348
349PP(pp_av2arylen)
350{
97aff369 351 dVAR; dSP;
502c6561 352 AV * const av = MUTABLE_AV(TOPs);
02d85cc3
EB
353 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
354 if (lvalue) {
355 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
356 if (!*sv) {
357 *sv = newSV_type(SVt_PVMG);
358 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
359 }
360 SETs(*sv);
361 } else {
362 SETs(sv_2mortal(newSViv(
363 AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop)
364 )));
79072805 365 }
79072805
LW
366 RETURN;
367}
368
a0d0e21e
LW
369PP(pp_pos)
370{
2154eca7 371 dVAR; dSP; dPOPss;
8ec5e241 372
78f9721b 373 if (PL_op->op_flags & OPf_MOD || LVRET) {
16eb5365
FC
374 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
375 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
376 LvTYPE(ret) = '.';
377 LvTARG(ret) = SvREFCNT_inc_simple(sv);
2154eca7 378 PUSHs(ret); /* no SvSETMAGIC */
a0d0e21e
LW
379 RETURN;
380 }
381 else {
a0d0e21e 382 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
1b6737cc 383 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
565764a8 384 if (mg && mg->mg_len >= 0) {
2154eca7 385 dTARGET;
a0ed51b3 386 I32 i = mg->mg_len;
7e2040f0 387 if (DO_UTF8(sv))
a0ed51b3 388 sv_pos_b2u(sv, &i);
fc15ae8f 389 PUSHi(i + CopARYBASE_get(PL_curcop));
a0d0e21e
LW
390 RETURN;
391 }
392 }
393 RETPUSHUNDEF;
394 }
395}
396
79072805
LW
397PP(pp_rv2cv)
398{
97aff369 399 dVAR; dSP;
79072805 400 GV *gv;
1eced8f8 401 HV *stash_unused;
c445ea15
AL
402 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
403 ? 0
404 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
405 ? GV_ADD|GV_NOEXPAND
406 : GV_ADD;
4633a7c4
LW
407 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
408 /* (But not in defined().) */
e26df76a 409
1eced8f8 410 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
07055b4c
CS
411 if (cv) {
412 if (CvCLONE(cv))
ad64d0ec 413 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
d32f2495
SC
414 if ((PL_op->op_private & OPpLVAL_INTRO)) {
415 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
416 cv = GvCV(gv);
417 if (!CvLVALUE(cv))
418 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
419 }
07055b4c 420 }
e26df76a 421 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
ea726b52 422 cv = MUTABLE_CV(gv);
e26df76a 423 }
07055b4c 424 else
ea726b52 425 cv = MUTABLE_CV(&PL_sv_undef);
ad64d0ec 426 SETs(MUTABLE_SV(cv));
79072805
LW
427 RETURN;
428}
429
c07a80fd 430PP(pp_prototype)
431{
97aff369 432 dVAR; dSP;
c07a80fd 433 CV *cv;
434 HV *stash;
435 GV *gv;
fabdb6c0 436 SV *ret = &PL_sv_undef;
c07a80fd 437
b6c543e3 438 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
e3f73d4e 439 const char * s = SvPVX_const(TOPs);
b6c543e3 440 if (strnEQ(s, "CORE::", 6)) {
b8c38f0a
FC
441 SV *const sv = core_prototype(NULL, s + 6, SvCUR(TOPs) - 6, 1);
442 if (sv) ret = sv;
443 goto set;
b6c543e3
IZ
444 }
445 }
f2c0649b 446 cv = sv_2cv(TOPs, &stash, &gv, 0);
5f05dabc 447 if (cv && SvPOK(cv))
59cd0e26 448 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
b6c543e3 449 set:
c07a80fd 450 SETs(ret);
451 RETURN;
452}
453
a0d0e21e
LW
454PP(pp_anoncode)
455{
97aff369 456 dVAR; dSP;
ea726b52 457 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
a5f75d66 458 if (CvCLONE(cv))
ad64d0ec 459 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
5f05dabc 460 EXTEND(SP,1);
ad64d0ec 461 PUSHs(MUTABLE_SV(cv));
a0d0e21e
LW
462 RETURN;
463}
464
465PP(pp_srefgen)
79072805 466{
97aff369 467 dVAR; dSP;
71be2cbc 468 *SP = refto(*SP);
79072805 469 RETURN;
8ec5e241 470}
a0d0e21e
LW
471
472PP(pp_refgen)
473{
97aff369 474 dVAR; dSP; dMARK;
a0d0e21e 475 if (GIMME != G_ARRAY) {
5f0b1d4e
GS
476 if (++MARK <= SP)
477 *MARK = *SP;
478 else
3280af22 479 *MARK = &PL_sv_undef;
5f0b1d4e
GS
480 *MARK = refto(*MARK);
481 SP = MARK;
482 RETURN;
a0d0e21e 483 }
bbce6d69 484 EXTEND_MORTAL(SP - MARK);
71be2cbc 485 while (++MARK <= SP)
486 *MARK = refto(*MARK);
a0d0e21e 487 RETURN;
79072805
LW
488}
489
76e3520e 490STATIC SV*
cea2e8a9 491S_refto(pTHX_ SV *sv)
71be2cbc 492{
97aff369 493 dVAR;
71be2cbc 494 SV* rv;
495
7918f24d
NC
496 PERL_ARGS_ASSERT_REFTO;
497
71be2cbc 498 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
499 if (LvTARGLEN(sv))
68dc0745 500 vivify_defelem(sv);
501 if (!(sv = LvTARG(sv)))
3280af22 502 sv = &PL_sv_undef;
0dd88869 503 else
b37c2d43 504 SvREFCNT_inc_void_NN(sv);
71be2cbc 505 }
d8b46c1b 506 else if (SvTYPE(sv) == SVt_PVAV) {
502c6561
NC
507 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
508 av_reify(MUTABLE_AV(sv));
d8b46c1b 509 SvTEMP_off(sv);
b37c2d43 510 SvREFCNT_inc_void_NN(sv);
d8b46c1b 511 }
f2933f5f
DM
512 else if (SvPADTMP(sv) && !IS_PADGV(sv))
513 sv = newSVsv(sv);
71be2cbc 514 else {
515 SvTEMP_off(sv);
b37c2d43 516 SvREFCNT_inc_void_NN(sv);
71be2cbc 517 }
518 rv = sv_newmortal();
4df7f6af 519 sv_upgrade(rv, SVt_IV);
b162af07 520 SvRV_set(rv, sv);
71be2cbc 521 SvROK_on(rv);
522 return rv;
523}
524
79072805
LW
525PP(pp_ref)
526{
97aff369 527 dVAR; dSP; dTARGET;
e1ec3a88 528 const char *pv;
1b6737cc 529 SV * const sv = POPs;
f12c7020 530
5b295bef
RD
531 if (sv)
532 SvGETMAGIC(sv);
f12c7020 533
a0d0e21e 534 if (!sv || !SvROK(sv))
4633a7c4 535 RETPUSHNO;
79072805 536
cba0b539
FR
537 pv = sv_reftype(SvRV(sv),TRUE);
538 PUSHp(pv, strlen(pv));
79072805
LW
539 RETURN;
540}
541
542PP(pp_bless)
543{
97aff369 544 dVAR; dSP;
463ee0b2 545 HV *stash;
79072805 546
463ee0b2 547 if (MAXARG == 1)
11faa288 548 stash = CopSTASH(PL_curcop);
7b8d334a 549 else {
1b6737cc 550 SV * const ssv = POPs;
7b8d334a 551 STRLEN len;
e1ec3a88 552 const char *ptr;
81689caa 553
016a42f3 554 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
81689caa 555 Perl_croak(aTHX_ "Attempt to bless into a reference");
5c144d81 556 ptr = SvPV_const(ssv,len);
a2a5de95
NC
557 if (len == 0)
558 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
559 "Explicit blessing to '' (assuming package main)");
da51bb9b 560 stash = gv_stashpvn(ptr, len, GV_ADD);
7b8d334a 561 }
a0d0e21e 562
5d3fdfeb 563 (void)sv_bless(TOPs, stash);
79072805
LW
564 RETURN;
565}
566
fb73857a 567PP(pp_gelem)
568{
97aff369 569 dVAR; dSP;
b13b2135 570
1b6737cc
AL
571 SV *sv = POPs;
572 const char * const elem = SvPV_nolen_const(sv);
159b6efe 573 GV * const gv = MUTABLE_GV(POPs);
c445ea15 574 SV * tmpRef = NULL;
1b6737cc 575
c445ea15 576 sv = NULL;
c4ba80c3
NC
577 if (elem) {
578 /* elem will always be NUL terminated. */
1b6737cc 579 const char * const second_letter = elem + 1;
c4ba80c3
NC
580 switch (*elem) {
581 case 'A':
1b6737cc 582 if (strEQ(second_letter, "RRAY"))
ad64d0ec 583 tmpRef = MUTABLE_SV(GvAV(gv));
c4ba80c3
NC
584 break;
585 case 'C':
1b6737cc 586 if (strEQ(second_letter, "ODE"))
ad64d0ec 587 tmpRef = MUTABLE_SV(GvCVu(gv));
c4ba80c3
NC
588 break;
589 case 'F':
1b6737cc 590 if (strEQ(second_letter, "ILEHANDLE")) {
c4ba80c3
NC
591 /* finally deprecated in 5.8.0 */
592 deprecate("*glob{FILEHANDLE}");
ad64d0ec 593 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
594 }
595 else
1b6737cc 596 if (strEQ(second_letter, "ORMAT"))
ad64d0ec 597 tmpRef = MUTABLE_SV(GvFORM(gv));
c4ba80c3
NC
598 break;
599 case 'G':
1b6737cc 600 if (strEQ(second_letter, "LOB"))
ad64d0ec 601 tmpRef = MUTABLE_SV(gv);
c4ba80c3
NC
602 break;
603 case 'H':
1b6737cc 604 if (strEQ(second_letter, "ASH"))
ad64d0ec 605 tmpRef = MUTABLE_SV(GvHV(gv));
c4ba80c3
NC
606 break;
607 case 'I':
1b6737cc 608 if (*second_letter == 'O' && !elem[2])
ad64d0ec 609 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
610 break;
611 case 'N':
1b6737cc 612 if (strEQ(second_letter, "AME"))
a663657d 613 sv = newSVhek(GvNAME_HEK(gv));
c4ba80c3
NC
614 break;
615 case 'P':
1b6737cc 616 if (strEQ(second_letter, "ACKAGE")) {
7fa3a4ab
NC
617 const HV * const stash = GvSTASH(gv);
618 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
396482e1 619 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
c4ba80c3
NC
620 }
621 break;
622 case 'S':
1b6737cc 623 if (strEQ(second_letter, "CALAR"))
f9d52e31 624 tmpRef = GvSVn(gv);
c4ba80c3 625 break;
39b99f21 626 }
fb73857a 627 }
76e3520e
GS
628 if (tmpRef)
629 sv = newRV(tmpRef);
fb73857a 630 if (sv)
631 sv_2mortal(sv);
632 else
3280af22 633 sv = &PL_sv_undef;
fb73857a 634 XPUSHs(sv);
635 RETURN;
636}
637
a0d0e21e 638/* Pattern matching */
79072805 639
a0d0e21e 640PP(pp_study)
79072805 641{
97aff369 642 dVAR; dSP; dPOPss;
a0d0e21e 643 register unsigned char *s;
72de20cd 644 char *sfirst_raw;
a0d0e21e 645 STRLEN len;
4185c919 646 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_study) : NULL;
72de20cd
NC
647 U8 quanta;
648 STRLEN size;
4185c919
NC
649
650 if (mg && SvSCREAM(sv))
651 RETPUSHYES;
a0d0e21e 652
a4f4e906 653 s = (unsigned char*)(SvPV(sv, len));
bc9a5256 654 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
a4f4e906
NC
655 /* No point in studying a zero length string, and not safe to study
656 anything that doesn't appear to be a simple scalar (and hence might
657 change between now and when the regexp engine runs without our set
bd473224 658 magic ever running) such as a reference to an object with overloaded
bc9a5256
NC
659 stringification. Also refuse to study an FBM scalar, as this gives
660 more flexibility in SV flag usage. No real-world code would ever
661 end up studying an FBM scalar, so this isn't a real pessimisation.
72de20cd
NC
662 Endemic use of I32 in Perl_screaminstr makes it hard to safely push
663 the study length limit from I32_MAX to U32_MAX - 1.
bc9a5256 664 */
a4f4e906
NC
665 RETPUSHNO;
666 }
667
72de20cd
NC
668 if (len < 0xFF) {
669 quanta = 1;
670 } else if (len < 0xFFFF) {
671 quanta = 2;
672 } else
673 quanta = 4;
a0d0e21e 674
72de20cd
NC
675 size = (256 + len) * quanta;
676 sfirst_raw = (char *)safemalloc(size);
677
678 if (!sfirst_raw)
cea2e8a9 679 DIE(aTHX_ "do_study: out of memory");
a0d0e21e 680
4185c919
NC
681 SvSCREAM_on(sv);
682 if (!mg)
683 mg = sv_magicext(sv, NULL, PERL_MAGIC_study, &PL_vtbl_regexp, NULL, 0);
72de20cd
NC
684 mg->mg_ptr = sfirst_raw;
685 mg->mg_len = size;
686 mg->mg_private = quanta;
687
688 memset(sfirst_raw, ~0, 256 * quanta);
689
690 /* The assumption here is that most studied strings are fairly short, hence
691 the pain of the extra code is worth it, given the memory savings.
692 80 character string, 336 bytes as U8, down from 1344 as U32
693 800 character string, 2112 bytes as U16, down from 4224 as U32
694 */
695
696 if (quanta == 1) {
697 U8 *const sfirst = (U8 *)sfirst_raw;
698 U8 *const snext = sfirst + 256;
699 while (len-- > 0) {
700 const U8 ch = s[len];
701 snext[len] = sfirst[ch];
702 sfirst[ch] = len;
703 }
704 } else if (quanta == 2) {
705 U16 *const sfirst = (U16 *)sfirst_raw;
706 U16 *const snext = sfirst + 256;
707 while (len-- > 0) {
708 const U8 ch = s[len];
709 snext[len] = sfirst[ch];
710 sfirst[ch] = len;
711 }
712 } else {
713 U32 *const sfirst = (U32 *)sfirst_raw;
714 U32 *const snext = sfirst + 256;
715 while (len-- > 0) {
716 const U8 ch = s[len];
717 snext[len] = sfirst[ch];
718 sfirst[ch] = len;
719 }
79072805
LW
720 }
721
1e422769 722 RETPUSHYES;
79072805
LW
723}
724
a0d0e21e 725PP(pp_trans)
79072805 726{
97aff369 727 dVAR; dSP; dTARG;
a0d0e21e
LW
728 SV *sv;
729
533c011a 730 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e 731 sv = POPs;
59f00321
RGS
732 else if (PL_op->op_private & OPpTARGET_MY)
733 sv = GETTARGET;
79072805 734 else {
54b9620d 735 sv = DEFSV;
a0d0e21e 736 EXTEND(SP,1);
79072805 737 }
adbc6bb1 738 TARG = sv_newmortal();
bb16bae8
FC
739 if(PL_op->op_type == OP_TRANSR) {
740 SV * const newsv = newSVsv(sv);
741 do_trans(newsv);
742 mPUSHs(newsv);
743 }
744 else PUSHi(do_trans(sv));
a0d0e21e 745 RETURN;
79072805
LW
746}
747
a0d0e21e 748/* Lvalue operators. */
79072805 749
81745e4e
NC
750static void
751S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
752{
753 dVAR;
754 STRLEN len;
755 char *s;
756
757 PERL_ARGS_ASSERT_DO_CHOMP;
758
759 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
760 return;
761 if (SvTYPE(sv) == SVt_PVAV) {
762 I32 i;
763 AV *const av = MUTABLE_AV(sv);
764 const I32 max = AvFILL(av);
765
766 for (i = 0; i <= max; i++) {
767 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
768 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
769 do_chomp(retval, sv, chomping);
770 }
771 return;
772 }
773 else if (SvTYPE(sv) == SVt_PVHV) {
774 HV* const hv = MUTABLE_HV(sv);
775 HE* entry;
776 (void)hv_iterinit(hv);
777 while ((entry = hv_iternext(hv)))
778 do_chomp(retval, hv_iterval(hv,entry), chomping);
779 return;
780 }
781 else if (SvREADONLY(sv)) {
782 if (SvFAKE(sv)) {
783 /* SV is copy-on-write */
784 sv_force_normal_flags(sv, 0);
785 }
786 if (SvREADONLY(sv))
787 Perl_croak_no_modify(aTHX);
788 }
789
790 if (PL_encoding) {
791 if (!SvUTF8(sv)) {
792 /* XXX, here sv is utf8-ized as a side-effect!
793 If encoding.pm is used properly, almost string-generating
794 operations, including literal strings, chr(), input data, etc.
795 should have been utf8-ized already, right?
796 */
797 sv_recode_to_utf8(sv, PL_encoding);
798 }
799 }
800
801 s = SvPV(sv, len);
802 if (chomping) {
803 char *temp_buffer = NULL;
804 SV *svrecode = NULL;
805
806 if (s && len) {
807 s += --len;
808 if (RsPARA(PL_rs)) {
809 if (*s != '\n')
810 goto nope;
811 ++SvIVX(retval);
812 while (len && s[-1] == '\n') {
813 --len;
814 --s;
815 ++SvIVX(retval);
816 }
817 }
818 else {
819 STRLEN rslen, rs_charlen;
820 const char *rsptr = SvPV_const(PL_rs, rslen);
821
822 rs_charlen = SvUTF8(PL_rs)
823 ? sv_len_utf8(PL_rs)
824 : rslen;
825
826 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
827 /* Assumption is that rs is shorter than the scalar. */
828 if (SvUTF8(PL_rs)) {
829 /* RS is utf8, scalar is 8 bit. */
830 bool is_utf8 = TRUE;
831 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
832 &rslen, &is_utf8);
833 if (is_utf8) {
834 /* Cannot downgrade, therefore cannot possibly match
835 */
836 assert (temp_buffer == rsptr);
837 temp_buffer = NULL;
838 goto nope;
839 }
840 rsptr = temp_buffer;
841 }
842 else if (PL_encoding) {
843 /* RS is 8 bit, encoding.pm is used.
844 * Do not recode PL_rs as a side-effect. */
845 svrecode = newSVpvn(rsptr, rslen);
846 sv_recode_to_utf8(svrecode, PL_encoding);
847 rsptr = SvPV_const(svrecode, rslen);
848 rs_charlen = sv_len_utf8(svrecode);
849 }
850 else {
851 /* RS is 8 bit, scalar is utf8. */
852 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
853 rsptr = temp_buffer;
854 }
855 }
856 if (rslen == 1) {
857 if (*s != *rsptr)
858 goto nope;
859 ++SvIVX(retval);
860 }
861 else {
862 if (len < rslen - 1)
863 goto nope;
864 len -= rslen - 1;
865 s -= rslen - 1;
866 if (memNE(s, rsptr, rslen))
867 goto nope;
868 SvIVX(retval) += rs_charlen;
869 }
870 }
871 s = SvPV_force_nolen(sv);
872 SvCUR_set(sv, len);
873 *SvEND(sv) = '\0';
874 SvNIOK_off(sv);
875 SvSETMAGIC(sv);
876 }
877 nope:
878
879 SvREFCNT_dec(svrecode);
880
881 Safefree(temp_buffer);
882 } else {
883 if (len && !SvPOK(sv))
884 s = SvPV_force_nomg(sv, len);
885 if (DO_UTF8(sv)) {
886 if (s && len) {
887 char * const send = s + len;
888 char * const start = s;
889 s = send - 1;
890 while (s > start && UTF8_IS_CONTINUATION(*s))
891 s--;
892 if (is_utf8_string((U8*)s, send - s)) {
893 sv_setpvn(retval, s, send - s);
894 *s = '\0';
895 SvCUR_set(sv, s - start);
896 SvNIOK_off(sv);
897 SvUTF8_on(retval);
898 }
899 }
900 else
901 sv_setpvs(retval, "");
902 }
903 else if (s && len) {
904 s += --len;
905 sv_setpvn(retval, s, 1);
906 *s = '\0';
907 SvCUR_set(sv, len);
908 SvUTF8_off(sv);
909 SvNIOK_off(sv);
910 }
911 else
912 sv_setpvs(retval, "");
913 SvSETMAGIC(sv);
914 }
915}
916
a0d0e21e
LW
917PP(pp_schop)
918{
97aff369 919 dVAR; dSP; dTARGET;
fa54efae
NC
920 const bool chomping = PL_op->op_type == OP_SCHOMP;
921
922 if (chomping)
923 sv_setiv(TARG, 0);
924 do_chomp(TARG, TOPs, chomping);
a0d0e21e
LW
925 SETTARG;
926 RETURN;
79072805
LW
927}
928
a0d0e21e 929PP(pp_chop)
79072805 930{
97aff369 931 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
fa54efae 932 const bool chomping = PL_op->op_type == OP_CHOMP;
8ec5e241 933
fa54efae
NC
934 if (chomping)
935 sv_setiv(TARG, 0);
20cf1f79 936 while (MARK < SP)
fa54efae 937 do_chomp(TARG, *++MARK, chomping);
20cf1f79
NC
938 SP = ORIGMARK;
939 XPUSHTARG;
a0d0e21e 940 RETURN;
79072805
LW
941}
942
a0d0e21e
LW
943PP(pp_undef)
944{
97aff369 945 dVAR; dSP;
a0d0e21e
LW
946 SV *sv;
947
533c011a 948 if (!PL_op->op_private) {
774d564b 949 EXTEND(SP, 1);
a0d0e21e 950 RETPUSHUNDEF;
774d564b 951 }
79072805 952
a0d0e21e
LW
953 sv = POPs;
954 if (!sv)
955 RETPUSHUNDEF;
85e6fe83 956
765f542d 957 SV_CHECK_THINKFIRST_COW_DROP(sv);
85e6fe83 958
a0d0e21e
LW
959 switch (SvTYPE(sv)) {
960 case SVt_NULL:
961 break;
962 case SVt_PVAV:
502c6561 963 av_undef(MUTABLE_AV(sv));
a0d0e21e
LW
964 break;
965 case SVt_PVHV:
85fbaab2 966 hv_undef(MUTABLE_HV(sv));
a0d0e21e
LW
967 break;
968 case SVt_PVCV:
a2a5de95
NC
969 if (cv_const_sv((const CV *)sv))
970 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
971 CvANON((const CV *)sv) ? "(anonymous)"
972 : GvENAME(CvGV((const CV *)sv)));
5f66b61c 973 /* FALLTHROUGH */
9607fc9c 974 case SVt_PVFM:
6fc92669
GS
975 {
976 /* let user-undef'd sub keep its identity */
ea726b52
NC
977 GV* const gv = CvGV((const CV *)sv);
978 cv_undef(MUTABLE_CV(sv));
b3f91e91 979 CvGV_set(MUTABLE_CV(sv), gv);
6fc92669 980 }
a0d0e21e 981 break;
8e07c86e 982 case SVt_PVGV:
6e592b3a 983 if (SvFAKE(sv)) {
3280af22 984 SvSetMagicSV(sv, &PL_sv_undef);
6e592b3a
BM
985 break;
986 }
987 else if (isGV_with_GP(sv)) {
20408e3c 988 GP *gp;
dd69841b
BB
989 HV *stash;
990
dd69841b 991 /* undef *Pkg::meth_name ... */
e530fb81
FC
992 bool method_changed
993 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
994 && HvENAME_get(stash);
995 /* undef *Foo:: */
996 if((stash = GvHV((const GV *)sv))) {
997 if(HvENAME_get(stash))
998 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
999 else stash = NULL;
1000 }
dd69841b 1001
159b6efe 1002 gp_free(MUTABLE_GV(sv));
a02a5408 1003 Newxz(gp, 1, GP);
c43ae56f 1004 GvGP_set(sv, gp_ref(gp));
561b68a9 1005 GvSV(sv) = newSV(0);
57843af0 1006 GvLINE(sv) = CopLINE(PL_curcop);
159b6efe 1007 GvEGV(sv) = MUTABLE_GV(sv);
20408e3c 1008 GvMULTI_on(sv);
e530fb81
FC
1009
1010 if(stash)
afdbe55d 1011 mro_package_moved(NULL, stash, (const GV *)sv, 0);
e530fb81
FC
1012 stash = NULL;
1013 /* undef *Foo::ISA */
1014 if( strEQ(GvNAME((const GV *)sv), "ISA")
1015 && (stash = GvSTASH((const GV *)sv))
1016 && (method_changed || HvENAME(stash)) )
1017 mro_isa_changed_in(stash);
1018 else if(method_changed)
1019 mro_method_changed_in(
da9043f5 1020 GvSTASH((const GV *)sv)
e530fb81
FC
1021 );
1022
6e592b3a 1023 break;
20408e3c 1024 }
6e592b3a 1025 /* FALL THROUGH */
a0d0e21e 1026 default:
b15aece3 1027 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
8bd4d4c5 1028 SvPV_free(sv);
c445ea15 1029 SvPV_set(sv, NULL);
4633a7c4 1030 SvLEN_set(sv, 0);
a0d0e21e 1031 }
0c34ef67 1032 SvOK_off(sv);
4633a7c4 1033 SvSETMAGIC(sv);
79072805 1034 }
a0d0e21e
LW
1035
1036 RETPUSHUNDEF;
79072805
LW
1037}
1038
a0d0e21e 1039PP(pp_predec)
79072805 1040{
97aff369 1041 dVAR; dSP;
6e592b3a 1042 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
6ad8f254 1043 Perl_croak_no_modify(aTHX);
3510b4a1
NC
1044 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1045 && SvIVX(TOPs) != IV_MIN)
55497cff 1046 {
45977657 1047 SvIV_set(TOPs, SvIVX(TOPs) - 1);
55497cff 1048 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
1049 }
1050 else
1051 sv_dec(TOPs);
a0d0e21e
LW
1052 SvSETMAGIC(TOPs);
1053 return NORMAL;
1054}
79072805 1055
a0d0e21e
LW
1056PP(pp_postinc)
1057{
97aff369 1058 dVAR; dSP; dTARGET;
6e592b3a 1059 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
6ad8f254 1060 Perl_croak_no_modify(aTHX);
7dcb9b98
DM
1061 if (SvROK(TOPs))
1062 TARG = sv_newmortal();
a0d0e21e 1063 sv_setsv(TARG, TOPs);
3510b4a1
NC
1064 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1065 && SvIVX(TOPs) != IV_MAX)
55497cff 1066 {
45977657 1067 SvIV_set(TOPs, SvIVX(TOPs) + 1);
55497cff 1068 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
1069 }
1070 else
6f1401dc 1071 sv_inc_nomg(TOPs);
a0d0e21e 1072 SvSETMAGIC(TOPs);
1e54a23f 1073 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
a0d0e21e
LW
1074 if (!SvOK(TARG))
1075 sv_setiv(TARG, 0);
1076 SETs(TARG);
1077 return NORMAL;
1078}
79072805 1079
a0d0e21e
LW
1080PP(pp_postdec)
1081{
97aff369 1082 dVAR; dSP; dTARGET;
6e592b3a 1083 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
6ad8f254 1084 Perl_croak_no_modify(aTHX);
7dcb9b98
DM
1085 if (SvROK(TOPs))
1086 TARG = sv_newmortal();
a0d0e21e 1087 sv_setsv(TARG, TOPs);
3510b4a1
NC
1088 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1089 && SvIVX(TOPs) != IV_MIN)
55497cff 1090 {
45977657 1091 SvIV_set(TOPs, SvIVX(TOPs) - 1);
55497cff 1092 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
1093 }
1094 else
6f1401dc 1095 sv_dec_nomg(TOPs);
a0d0e21e
LW
1096 SvSETMAGIC(TOPs);
1097 SETs(TARG);
1098 return NORMAL;
1099}
79072805 1100
a0d0e21e
LW
1101/* Ordinary operators. */
1102
1103PP(pp_pow)
1104{
800401ee 1105 dVAR; dSP; dATARGET; SV *svl, *svr;
58d76dfd 1106#ifdef PERL_PRESERVE_IVUV
52a96ae6
HS
1107 bool is_int = 0;
1108#endif
6f1401dc
DM
1109 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1110 svr = TOPs;
1111 svl = TOPm1s;
52a96ae6
HS
1112#ifdef PERL_PRESERVE_IVUV
1113 /* For integer to integer power, we do the calculation by hand wherever
1114 we're sure it is safe; otherwise we call pow() and try to convert to
1115 integer afterwards. */
58d76dfd 1116 {
6f1401dc 1117 SvIV_please_nomg(svr);
800401ee 1118 if (SvIOK(svr)) {
6f1401dc 1119 SvIV_please_nomg(svl);
800401ee 1120 if (SvIOK(svl)) {
900658e3
PF
1121 UV power;
1122 bool baseuok;
1123 UV baseuv;
1124
800401ee
JH
1125 if (SvUOK(svr)) {
1126 power = SvUVX(svr);
900658e3 1127 } else {
800401ee 1128 const IV iv = SvIVX(svr);
900658e3
PF
1129 if (iv >= 0) {
1130 power = iv;
1131 } else {
1132 goto float_it; /* Can't do negative powers this way. */
1133 }
1134 }
1135
800401ee 1136 baseuok = SvUOK(svl);
900658e3 1137 if (baseuok) {
800401ee 1138 baseuv = SvUVX(svl);
900658e3 1139 } else {
800401ee 1140 const IV iv = SvIVX(svl);
900658e3
PF
1141 if (iv >= 0) {
1142 baseuv = iv;
1143 baseuok = TRUE; /* effectively it's a UV now */
1144 } else {
1145 baseuv = -iv; /* abs, baseuok == false records sign */
1146 }
1147 }
52a96ae6
HS
1148 /* now we have integer ** positive integer. */
1149 is_int = 1;
1150
1151 /* foo & (foo - 1) is zero only for a power of 2. */
58d76dfd 1152 if (!(baseuv & (baseuv - 1))) {
52a96ae6 1153 /* We are raising power-of-2 to a positive integer.
58d76dfd
JH
1154 The logic here will work for any base (even non-integer
1155 bases) but it can be less accurate than
1156 pow (base,power) or exp (power * log (base)) when the
1157 intermediate values start to spill out of the mantissa.
1158 With powers of 2 we know this can't happen.
1159 And powers of 2 are the favourite thing for perl
1160 programmers to notice ** not doing what they mean. */
1161 NV result = 1.0;
1162 NV base = baseuok ? baseuv : -(NV)baseuv;
900658e3
PF
1163
1164 if (power & 1) {
1165 result *= base;
1166 }
1167 while (power >>= 1) {
1168 base *= base;
1169 if (power & 1) {
1170 result *= base;
1171 }
1172 }
58d76dfd
JH
1173 SP--;
1174 SETn( result );
6f1401dc 1175 SvIV_please_nomg(svr);
58d76dfd 1176 RETURN;
52a96ae6
HS
1177 } else {
1178 register unsigned int highbit = 8 * sizeof(UV);
900658e3
PF
1179 register unsigned int diff = 8 * sizeof(UV);
1180 while (diff >>= 1) {
1181 highbit -= diff;
1182 if (baseuv >> highbit) {
1183 highbit += diff;
1184 }
52a96ae6
HS
1185 }
1186 /* we now have baseuv < 2 ** highbit */
1187 if (power * highbit <= 8 * sizeof(UV)) {
1188 /* result will definitely fit in UV, so use UV math
1189 on same algorithm as above */
1190 register UV result = 1;
1191 register UV base = baseuv;
f2338a2e 1192 const bool odd_power = cBOOL(power & 1);
900658e3
PF
1193 if (odd_power) {
1194 result *= base;
1195 }
1196 while (power >>= 1) {
1197 base *= base;
1198 if (power & 1) {
52a96ae6 1199 result *= base;
52a96ae6
HS
1200 }
1201 }
1202 SP--;
0615a994 1203 if (baseuok || !odd_power)
52a96ae6
HS
1204 /* answer is positive */
1205 SETu( result );
1206 else if (result <= (UV)IV_MAX)
1207 /* answer negative, fits in IV */
1208 SETi( -(IV)result );
1209 else if (result == (UV)IV_MIN)
1210 /* 2's complement assumption: special case IV_MIN */
1211 SETi( IV_MIN );
1212 else
1213 /* answer negative, doesn't fit */
1214 SETn( -(NV)result );
1215 RETURN;
1216 }
1217 }
1218 }
1219 }
58d76dfd 1220 }
52a96ae6 1221 float_it:
58d76dfd 1222#endif
a0d0e21e 1223 {
6f1401dc
DM
1224 NV right = SvNV_nomg(svr);
1225 NV left = SvNV_nomg(svl);
4efa5a16 1226 (void)POPs;
3aaeb624
JA
1227
1228#if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1229 /*
1230 We are building perl with long double support and are on an AIX OS
1231 afflicted with a powl() function that wrongly returns NaNQ for any
1232 negative base. This was reported to IBM as PMR #23047-379 on
1233 03/06/2006. The problem exists in at least the following versions
1234 of AIX and the libm fileset, and no doubt others as well:
1235
1236 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1237 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1238 AIX 5.2.0 bos.adt.libm 5.2.0.85
1239
1240 So, until IBM fixes powl(), we provide the following workaround to
1241 handle the problem ourselves. Our logic is as follows: for
1242 negative bases (left), we use fmod(right, 2) to check if the
1243 exponent is an odd or even integer:
1244
1245 - if odd, powl(left, right) == -powl(-left, right)
1246 - if even, powl(left, right) == powl(-left, right)
1247
1248 If the exponent is not an integer, the result is rightly NaNQ, so
1249 we just return that (as NV_NAN).
1250 */
1251
1252 if (left < 0.0) {
1253 NV mod2 = Perl_fmod( right, 2.0 );
1254 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1255 SETn( -Perl_pow( -left, right) );
1256 } else if (mod2 == 0.0) { /* even integer */
1257 SETn( Perl_pow( -left, right) );
1258 } else { /* fractional power */
1259 SETn( NV_NAN );
1260 }
1261 } else {
1262 SETn( Perl_pow( left, right) );
1263 }
1264#else
52a96ae6 1265 SETn( Perl_pow( left, right) );
3aaeb624
JA
1266#endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1267
52a96ae6
HS
1268#ifdef PERL_PRESERVE_IVUV
1269 if (is_int)
6f1401dc 1270 SvIV_please_nomg(svr);
52a96ae6
HS
1271#endif
1272 RETURN;
93a17b20 1273 }
a0d0e21e
LW
1274}
1275
1276PP(pp_multiply)
1277{
800401ee 1278 dVAR; dSP; dATARGET; SV *svl, *svr;
6f1401dc
DM
1279 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1280 svr = TOPs;
1281 svl = TOPm1s;
28e5dec8 1282#ifdef PERL_PRESERVE_IVUV
6f1401dc 1283 SvIV_please_nomg(svr);
800401ee 1284 if (SvIOK(svr)) {
28e5dec8
JH
1285 /* Unless the left argument is integer in range we are going to have to
1286 use NV maths. Hence only attempt to coerce the right argument if
1287 we know the left is integer. */
1288 /* Left operand is defined, so is it IV? */
6f1401dc 1289 SvIV_please_nomg(svl);
800401ee
JH
1290 if (SvIOK(svl)) {
1291 bool auvok = SvUOK(svl);
1292 bool buvok = SvUOK(svr);
28e5dec8
JH
1293 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1294 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1295 UV alow;
1296 UV ahigh;
1297 UV blow;
1298 UV bhigh;
1299
1300 if (auvok) {
800401ee 1301 alow = SvUVX(svl);
28e5dec8 1302 } else {
800401ee 1303 const IV aiv = SvIVX(svl);
28e5dec8
JH
1304 if (aiv >= 0) {
1305 alow = aiv;
1306 auvok = TRUE; /* effectively it's a UV now */
1307 } else {
1308 alow = -aiv; /* abs, auvok == false records sign */
1309 }
1310 }
1311 if (buvok) {
800401ee 1312 blow = SvUVX(svr);
28e5dec8 1313 } else {
800401ee 1314 const IV biv = SvIVX(svr);
28e5dec8
JH
1315 if (biv >= 0) {
1316 blow = biv;
1317 buvok = TRUE; /* effectively it's a UV now */
1318 } else {
1319 blow = -biv; /* abs, buvok == false records sign */
1320 }
1321 }
1322
1323 /* If this does sign extension on unsigned it's time for plan B */
1324 ahigh = alow >> (4 * sizeof (UV));
1325 alow &= botmask;
1326 bhigh = blow >> (4 * sizeof (UV));
1327 blow &= botmask;
1328 if (ahigh && bhigh) {
6f207bd3 1329 NOOP;
28e5dec8
JH
1330 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1331 which is overflow. Drop to NVs below. */
1332 } else if (!ahigh && !bhigh) {
1333 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1334 so the unsigned multiply cannot overflow. */
c445ea15 1335 const UV product = alow * blow;
28e5dec8
JH
1336 if (auvok == buvok) {
1337 /* -ve * -ve or +ve * +ve gives a +ve result. */
1338 SP--;
1339 SETu( product );
1340 RETURN;
1341 } else if (product <= (UV)IV_MIN) {
1342 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1343 /* -ve result, which could overflow an IV */
1344 SP--;
25716404 1345 SETi( -(IV)product );
28e5dec8
JH
1346 RETURN;
1347 } /* else drop to NVs below. */
1348 } else {
1349 /* One operand is large, 1 small */
1350 UV product_middle;
1351 if (bhigh) {
1352 /* swap the operands */
1353 ahigh = bhigh;
1354 bhigh = blow; /* bhigh now the temp var for the swap */
1355 blow = alow;
1356 alow = bhigh;
1357 }
1358 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1359 multiplies can't overflow. shift can, add can, -ve can. */
1360 product_middle = ahigh * blow;
1361 if (!(product_middle & topmask)) {
1362 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1363 UV product_low;
1364 product_middle <<= (4 * sizeof (UV));
1365 product_low = alow * blow;
1366
1367 /* as for pp_add, UV + something mustn't get smaller.
1368 IIRC ANSI mandates this wrapping *behaviour* for
1369 unsigned whatever the actual representation*/
1370 product_low += product_middle;
1371 if (product_low >= product_middle) {
1372 /* didn't overflow */
1373 if (auvok == buvok) {
1374 /* -ve * -ve or +ve * +ve gives a +ve result. */
1375 SP--;
1376 SETu( product_low );
1377 RETURN;
1378 } else if (product_low <= (UV)IV_MIN) {
1379 /* 2s complement assumption again */
1380 /* -ve result, which could overflow an IV */
1381 SP--;
25716404 1382 SETi( -(IV)product_low );
28e5dec8
JH
1383 RETURN;
1384 } /* else drop to NVs below. */
1385 }
1386 } /* product_middle too large */
1387 } /* ahigh && bhigh */
800401ee
JH
1388 } /* SvIOK(svl) */
1389 } /* SvIOK(svr) */
28e5dec8 1390#endif
a0d0e21e 1391 {
6f1401dc
DM
1392 NV right = SvNV_nomg(svr);
1393 NV left = SvNV_nomg(svl);
4efa5a16 1394 (void)POPs;
a0d0e21e
LW
1395 SETn( left * right );
1396 RETURN;
79072805 1397 }
a0d0e21e
LW
1398}
1399
1400PP(pp_divide)
1401{
800401ee 1402 dVAR; dSP; dATARGET; SV *svl, *svr;
6f1401dc
DM
1403 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1404 svr = TOPs;
1405 svl = TOPm1s;
5479d192 1406 /* Only try to do UV divide first
68795e93 1407 if ((SLOPPYDIVIDE is true) or
5479d192
NC
1408 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1409 to preserve))
1410 The assumption is that it is better to use floating point divide
1411 whenever possible, only doing integer divide first if we can't be sure.
1412 If NV_PRESERVES_UV is true then we know at compile time that no UV
1413 can be too large to preserve, so don't need to compile the code to
1414 test the size of UVs. */
1415
a0d0e21e 1416#ifdef SLOPPYDIVIDE
5479d192
NC
1417# define PERL_TRY_UV_DIVIDE
1418 /* ensure that 20./5. == 4. */
a0d0e21e 1419#else
5479d192
NC
1420# ifdef PERL_PRESERVE_IVUV
1421# ifndef NV_PRESERVES_UV
1422# define PERL_TRY_UV_DIVIDE
1423# endif
1424# endif
a0d0e21e 1425#endif
5479d192
NC
1426
1427#ifdef PERL_TRY_UV_DIVIDE
6f1401dc 1428 SvIV_please_nomg(svr);
800401ee 1429 if (SvIOK(svr)) {
6f1401dc 1430 SvIV_please_nomg(svl);
800401ee
JH
1431 if (SvIOK(svl)) {
1432 bool left_non_neg = SvUOK(svl);
1433 bool right_non_neg = SvUOK(svr);
5479d192
NC
1434 UV left;
1435 UV right;
1436
1437 if (right_non_neg) {
800401ee 1438 right = SvUVX(svr);
5479d192
NC
1439 }
1440 else {
800401ee 1441 const IV biv = SvIVX(svr);
5479d192
NC
1442 if (biv >= 0) {
1443 right = biv;
1444 right_non_neg = TRUE; /* effectively it's a UV now */
1445 }
1446 else {
1447 right = -biv;
1448 }
1449 }
1450 /* historically undef()/0 gives a "Use of uninitialized value"
1451 warning before dieing, hence this test goes here.
1452 If it were immediately before the second SvIV_please, then
1453 DIE() would be invoked before left was even inspected, so
486ec47a 1454 no inspection would give no warning. */
5479d192
NC
1455 if (right == 0)
1456 DIE(aTHX_ "Illegal division by zero");
1457
1458 if (left_non_neg) {
800401ee 1459 left = SvUVX(svl);
5479d192
NC
1460 }
1461 else {
800401ee 1462 const IV aiv = SvIVX(svl);
5479d192
NC
1463 if (aiv >= 0) {
1464 left = aiv;
1465 left_non_neg = TRUE; /* effectively it's a UV now */
1466 }
1467 else {
1468 left = -aiv;
1469 }
1470 }
1471
1472 if (left >= right
1473#ifdef SLOPPYDIVIDE
1474 /* For sloppy divide we always attempt integer division. */
1475#else
1476 /* Otherwise we only attempt it if either or both operands
1477 would not be preserved by an NV. If both fit in NVs
0c2ee62a
NC
1478 we fall through to the NV divide code below. However,
1479 as left >= right to ensure integer result here, we know that
1480 we can skip the test on the right operand - right big
1481 enough not to be preserved can't get here unless left is
1482 also too big. */
1483
1484 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
5479d192
NC
1485#endif
1486 ) {
1487 /* Integer division can't overflow, but it can be imprecise. */
1b6737cc 1488 const UV result = left / right;
5479d192
NC
1489 if (result * right == left) {
1490 SP--; /* result is valid */
1491 if (left_non_neg == right_non_neg) {
1492 /* signs identical, result is positive. */
1493 SETu( result );
1494 RETURN;
1495 }
1496 /* 2s complement assumption */
1497 if (result <= (UV)IV_MIN)
91f3b821 1498 SETi( -(IV)result );
5479d192
NC
1499 else {
1500 /* It's exact but too negative for IV. */
1501 SETn( -(NV)result );
1502 }
1503 RETURN;
1504 } /* tried integer divide but it was not an integer result */
32fdb065 1505 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
5479d192
NC
1506 } /* left wasn't SvIOK */
1507 } /* right wasn't SvIOK */
1508#endif /* PERL_TRY_UV_DIVIDE */
1509 {
6f1401dc
DM
1510 NV right = SvNV_nomg(svr);
1511 NV left = SvNV_nomg(svl);
4efa5a16 1512 (void)POPs;(void)POPs;
ebc6a117
PD
1513#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1514 if (! Perl_isnan(right) && right == 0.0)
1515#else
5479d192 1516 if (right == 0.0)
ebc6a117 1517#endif
5479d192
NC
1518 DIE(aTHX_ "Illegal division by zero");
1519 PUSHn( left / right );
1520 RETURN;
79072805 1521 }
a0d0e21e
LW
1522}
1523
1524PP(pp_modulo)
1525{
6f1401dc
DM
1526 dVAR; dSP; dATARGET;
1527 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
a0d0e21e 1528 {
9c5ffd7c
JH
1529 UV left = 0;
1530 UV right = 0;
dc656993
JH
1531 bool left_neg = FALSE;
1532 bool right_neg = FALSE;
e2c88acc
NC
1533 bool use_double = FALSE;
1534 bool dright_valid = FALSE;
9c5ffd7c
JH
1535 NV dright = 0.0;
1536 NV dleft = 0.0;
6f1401dc
DM
1537 SV * const svr = TOPs;
1538 SV * const svl = TOPm1s;
1539 SvIV_please_nomg(svr);
800401ee
JH
1540 if (SvIOK(svr)) {
1541 right_neg = !SvUOK(svr);
e2c88acc 1542 if (!right_neg) {
800401ee 1543 right = SvUVX(svr);
e2c88acc 1544 } else {
800401ee 1545 const IV biv = SvIVX(svr);
e2c88acc
NC
1546 if (biv >= 0) {
1547 right = biv;
1548 right_neg = FALSE; /* effectively it's a UV now */
1549 } else {
1550 right = -biv;
1551 }
1552 }
1553 }
1554 else {
6f1401dc 1555 dright = SvNV_nomg(svr);
787eafbd
IZ
1556 right_neg = dright < 0;
1557 if (right_neg)
1558 dright = -dright;
e2c88acc
NC
1559 if (dright < UV_MAX_P1) {
1560 right = U_V(dright);
1561 dright_valid = TRUE; /* In case we need to use double below. */
1562 } else {
1563 use_double = TRUE;
1564 }
787eafbd 1565 }
a0d0e21e 1566
e2c88acc
NC
1567 /* At this point use_double is only true if right is out of range for
1568 a UV. In range NV has been rounded down to nearest UV and
1569 use_double false. */
6f1401dc 1570 SvIV_please_nomg(svl);
800401ee
JH
1571 if (!use_double && SvIOK(svl)) {
1572 if (SvIOK(svl)) {
1573 left_neg = !SvUOK(svl);
e2c88acc 1574 if (!left_neg) {
800401ee 1575 left = SvUVX(svl);
e2c88acc 1576 } else {
800401ee 1577 const IV aiv = SvIVX(svl);
e2c88acc
NC
1578 if (aiv >= 0) {
1579 left = aiv;
1580 left_neg = FALSE; /* effectively it's a UV now */
1581 } else {
1582 left = -aiv;
1583 }
1584 }
1585 }
1586 }
787eafbd 1587 else {
6f1401dc 1588 dleft = SvNV_nomg(svl);
787eafbd
IZ
1589 left_neg = dleft < 0;
1590 if (left_neg)
1591 dleft = -dleft;
68dc0745 1592
e2c88acc
NC
1593 /* This should be exactly the 5.6 behaviour - if left and right are
1594 both in range for UV then use U_V() rather than floor. */
1595 if (!use_double) {
1596 if (dleft < UV_MAX_P1) {
1597 /* right was in range, so is dleft, so use UVs not double.
1598 */
1599 left = U_V(dleft);
1600 }
1601 /* left is out of range for UV, right was in range, so promote
1602 right (back) to double. */
1603 else {
1604 /* The +0.5 is used in 5.6 even though it is not strictly
1605 consistent with the implicit +0 floor in the U_V()
1606 inside the #if 1. */
1607 dleft = Perl_floor(dleft + 0.5);
1608 use_double = TRUE;
1609 if (dright_valid)
1610 dright = Perl_floor(dright + 0.5);
1611 else
1612 dright = right;
1613 }
1614 }
1615 }
6f1401dc 1616 sp -= 2;
787eafbd 1617 if (use_double) {
65202027 1618 NV dans;
787eafbd 1619
787eafbd 1620 if (!dright)
cea2e8a9 1621 DIE(aTHX_ "Illegal modulus zero");
787eafbd 1622
65202027 1623 dans = Perl_fmod(dleft, dright);
787eafbd
IZ
1624 if ((left_neg != right_neg) && dans)
1625 dans = dright - dans;
1626 if (right_neg)
1627 dans = -dans;
1628 sv_setnv(TARG, dans);
1629 }
1630 else {
1631 UV ans;
1632
787eafbd 1633 if (!right)
cea2e8a9 1634 DIE(aTHX_ "Illegal modulus zero");
787eafbd
IZ
1635
1636 ans = left % right;
1637 if ((left_neg != right_neg) && ans)
1638 ans = right - ans;
1639 if (right_neg) {
1640 /* XXX may warn: unary minus operator applied to unsigned type */
1641 /* could change -foo to be (~foo)+1 instead */
1642 if (ans <= ~((UV)IV_MAX)+1)
1643 sv_setiv(TARG, ~ans+1);
1644 else
65202027 1645 sv_setnv(TARG, -(NV)ans);
787eafbd
IZ
1646 }
1647 else
1648 sv_setuv(TARG, ans);
1649 }
1650 PUSHTARG;
1651 RETURN;
79072805 1652 }
a0d0e21e 1653}
79072805 1654
a0d0e21e
LW
1655PP(pp_repeat)
1656{
6f1401dc 1657 dVAR; dSP; dATARGET;
2b573ace 1658 register IV count;
6f1401dc
DM
1659 SV *sv;
1660
1661 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1662 /* TODO: think of some way of doing list-repeat overloading ??? */
1663 sv = POPs;
1664 SvGETMAGIC(sv);
1665 }
1666 else {
1667 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1668 sv = POPs;
1669 }
1670
2b573ace
JH
1671 if (SvIOKp(sv)) {
1672 if (SvUOK(sv)) {
6f1401dc 1673 const UV uv = SvUV_nomg(sv);
2b573ace
JH
1674 if (uv > IV_MAX)
1675 count = IV_MAX; /* The best we can do? */
1676 else
1677 count = uv;
1678 } else {
6f1401dc 1679 const IV iv = SvIV_nomg(sv);
2b573ace
JH
1680 if (iv < 0)
1681 count = 0;
1682 else
1683 count = iv;
1684 }
1685 }
1686 else if (SvNOKp(sv)) {
6f1401dc 1687 const NV nv = SvNV_nomg(sv);
2b573ace
JH
1688 if (nv < 0.0)
1689 count = 0;
1690 else
1691 count = (IV)nv;
1692 }
1693 else
6f1401dc
DM
1694 count = SvIV_nomg(sv);
1695
533c011a 1696 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
a0d0e21e 1697 dMARK;
0bd48802
AL
1698 static const char oom_list_extend[] = "Out of memory during list extend";
1699 const I32 items = SP - MARK;
1700 const I32 max = items * count;
79072805 1701
2b573ace
JH
1702 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1703 /* Did the max computation overflow? */
27d5b266 1704 if (items > 0 && max > 0 && (max < items || max < count))
2b573ace 1705 Perl_croak(aTHX_ oom_list_extend);
a0d0e21e
LW
1706 MEXTEND(MARK, max);
1707 if (count > 1) {
1708 while (SP > MARK) {
976c8a39
JH
1709#if 0
1710 /* This code was intended to fix 20010809.028:
1711
1712 $x = 'abcd';
1713 for (($x =~ /./g) x 2) {
1714 print chop; # "abcdabcd" expected as output.
1715 }
1716
1717 * but that change (#11635) broke this code:
1718
1719 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1720
1721 * I can't think of a better fix that doesn't introduce
1722 * an efficiency hit by copying the SVs. The stack isn't
1723 * refcounted, and mortalisation obviously doesn't
1724 * Do The Right Thing when the stack has more than
1725 * one pointer to the same mortal value.
1726 * .robin.
1727 */
e30acc16
RH
1728 if (*SP) {
1729 *SP = sv_2mortal(newSVsv(*SP));
1730 SvREADONLY_on(*SP);
1731 }
976c8a39
JH
1732#else
1733 if (*SP)
1734 SvTEMP_off((*SP));
1735#endif
a0d0e21e 1736 SP--;
79072805 1737 }
a0d0e21e
LW
1738 MARK++;
1739 repeatcpy((char*)(MARK + items), (char*)MARK,
ad64d0ec 1740 items * sizeof(const SV *), count - 1);
a0d0e21e 1741 SP += max;
79072805 1742 }
a0d0e21e
LW
1743 else if (count <= 0)
1744 SP -= items;
79072805 1745 }
a0d0e21e 1746 else { /* Note: mark already snarfed by pp_list */
0bd48802 1747 SV * const tmpstr = POPs;
a0d0e21e 1748 STRLEN len;
9b877dbb 1749 bool isutf;
2b573ace
JH
1750 static const char oom_string_extend[] =
1751 "Out of memory during string extend";
a0d0e21e 1752
6f1401dc
DM
1753 if (TARG != tmpstr)
1754 sv_setsv_nomg(TARG, tmpstr);
1755 SvPV_force_nomg(TARG, len);
9b877dbb 1756 isutf = DO_UTF8(TARG);
8ebc5c01 1757 if (count != 1) {
1758 if (count < 1)
1759 SvCUR_set(TARG, 0);
1760 else {
c445ea15 1761 const STRLEN max = (UV)count * len;
19a94d75 1762 if (len > MEM_SIZE_MAX / count)
2b573ace
JH
1763 Perl_croak(aTHX_ oom_string_extend);
1764 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
8569b9dc 1765 SvGROW(TARG, max + 1);
a0d0e21e 1766 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
b162af07 1767 SvCUR_set(TARG, SvCUR(TARG) * count);
7a4c00b4 1768 }
a0d0e21e 1769 *SvEND(TARG) = '\0';
a0d0e21e 1770 }
dfcb284a
GS
1771 if (isutf)
1772 (void)SvPOK_only_UTF8(TARG);
1773 else
1774 (void)SvPOK_only(TARG);
b80b6069
RH
1775
1776 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1777 /* The parser saw this as a list repeat, and there
1778 are probably several items on the stack. But we're
1779 in scalar context, and there's no pp_list to save us
1780 now. So drop the rest of the items -- robin@kitsite.com
1781 */
1782 dMARK;
1783 SP = MARK;
1784 }
a0d0e21e 1785 PUSHTARG;
79072805 1786 }
a0d0e21e
LW
1787 RETURN;
1788}
79072805 1789
a0d0e21e
LW
1790PP(pp_subtract)
1791{
800401ee 1792 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
6f1401dc
DM
1793 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1794 svr = TOPs;
1795 svl = TOPm1s;
800401ee 1796 useleft = USE_LEFT(svl);
28e5dec8 1797#ifdef PERL_PRESERVE_IVUV
7dca457a
NC
1798 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1799 "bad things" happen if you rely on signed integers wrapping. */
6f1401dc 1800 SvIV_please_nomg(svr);
800401ee 1801 if (SvIOK(svr)) {
28e5dec8
JH
1802 /* Unless the left argument is integer in range we are going to have to
1803 use NV maths. Hence only attempt to coerce the right argument if
1804 we know the left is integer. */
9c5ffd7c
JH
1805 register UV auv = 0;
1806 bool auvok = FALSE;
7dca457a
NC
1807 bool a_valid = 0;
1808
28e5dec8 1809 if (!useleft) {
7dca457a
NC
1810 auv = 0;
1811 a_valid = auvok = 1;
1812 /* left operand is undef, treat as zero. */
28e5dec8
JH
1813 } else {
1814 /* Left operand is defined, so is it IV? */
6f1401dc 1815 SvIV_please_nomg(svl);
800401ee
JH
1816 if (SvIOK(svl)) {
1817 if ((auvok = SvUOK(svl)))
1818 auv = SvUVX(svl);
7dca457a 1819 else {
800401ee 1820 register const IV aiv = SvIVX(svl);
7dca457a
NC
1821 if (aiv >= 0) {
1822 auv = aiv;
1823 auvok = 1; /* Now acting as a sign flag. */
1824 } else { /* 2s complement assumption for IV_MIN */
1825 auv = (UV)-aiv;
28e5dec8 1826 }
7dca457a
NC
1827 }
1828 a_valid = 1;
1829 }
1830 }
1831 if (a_valid) {
1832 bool result_good = 0;
1833 UV result;
1834 register UV buv;
800401ee 1835 bool buvok = SvUOK(svr);
9041c2e3 1836
7dca457a 1837 if (buvok)
800401ee 1838 buv = SvUVX(svr);
7dca457a 1839 else {
800401ee 1840 register const IV biv = SvIVX(svr);
7dca457a
NC
1841 if (biv >= 0) {
1842 buv = biv;
1843 buvok = 1;
1844 } else
1845 buv = (UV)-biv;
1846 }
1847 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
602f51c4 1848 else "IV" now, independent of how it came in.
7dca457a
NC
1849 if a, b represents positive, A, B negative, a maps to -A etc
1850 a - b => (a - b)
1851 A - b => -(a + b)
1852 a - B => (a + b)
1853 A - B => -(a - b)
1854 all UV maths. negate result if A negative.
1855 subtract if signs same, add if signs differ. */
1856
1857 if (auvok ^ buvok) {
1858 /* Signs differ. */
1859 result = auv + buv;
1860 if (result >= auv)
1861 result_good = 1;
1862 } else {
1863 /* Signs same */
1864 if (auv >= buv) {
1865 result = auv - buv;
1866 /* Must get smaller */
1867 if (result <= auv)
1868 result_good = 1;
1869 } else {
1870 result = buv - auv;
1871 if (result <= buv) {
1872 /* result really should be -(auv-buv). as its negation
1873 of true value, need to swap our result flag */
1874 auvok = !auvok;
1875 result_good = 1;
28e5dec8 1876 }
28e5dec8
JH
1877 }
1878 }
7dca457a
NC
1879 if (result_good) {
1880 SP--;
1881 if (auvok)
1882 SETu( result );
1883 else {
1884 /* Negate result */
1885 if (result <= (UV)IV_MIN)
1886 SETi( -(IV)result );
1887 else {
1888 /* result valid, but out of range for IV. */
1889 SETn( -(NV)result );
1890 }
1891 }
1892 RETURN;
1893 } /* Overflow, drop through to NVs. */
28e5dec8
JH
1894 }
1895 }
1896#endif
a0d0e21e 1897 {
6f1401dc 1898 NV value = SvNV_nomg(svr);
4efa5a16
RD
1899 (void)POPs;
1900
28e5dec8
JH
1901 if (!useleft) {
1902 /* left operand is undef, treat as zero - value */
1903 SETn(-value);
1904 RETURN;
1905 }
6f1401dc 1906 SETn( SvNV_nomg(svl) - value );
28e5dec8 1907 RETURN;
79072805 1908 }
a0d0e21e 1909}
79072805 1910
a0d0e21e
LW
1911PP(pp_left_shift)
1912{
6f1401dc 1913 dVAR; dSP; dATARGET; SV *svl, *svr;
a42d0242 1914 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
6f1401dc
DM
1915 svr = POPs;
1916 svl = TOPs;
a0d0e21e 1917 {
6f1401dc 1918 const IV shift = SvIV_nomg(svr);
d0ba1bd2 1919 if (PL_op->op_private & HINT_INTEGER) {
6f1401dc 1920 const IV i = SvIV_nomg(svl);
972b05a9 1921 SETi(i << shift);
d0ba1bd2
JH
1922 }
1923 else {
6f1401dc 1924 const UV u = SvUV_nomg(svl);
972b05a9 1925 SETu(u << shift);
d0ba1bd2 1926 }
55497cff 1927 RETURN;
79072805 1928 }
a0d0e21e 1929}
79072805 1930
a0d0e21e
LW
1931PP(pp_right_shift)
1932{
6f1401dc 1933 dVAR; dSP; dATARGET; SV *svl, *svr;
a42d0242 1934 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
6f1401dc
DM
1935 svr = POPs;
1936 svl = TOPs;
a0d0e21e 1937 {
6f1401dc 1938 const IV shift = SvIV_nomg(svr);
d0ba1bd2 1939 if (PL_op->op_private & HINT_INTEGER) {
6f1401dc 1940 const IV i = SvIV_nomg(svl);
972b05a9 1941 SETi(i >> shift);
d0ba1bd2
JH
1942 }
1943 else {
6f1401dc 1944 const UV u = SvUV_nomg(svl);
972b05a9 1945 SETu(u >> shift);
d0ba1bd2 1946 }
a0d0e21e 1947 RETURN;
93a17b20 1948 }
79072805
LW
1949}
1950
a0d0e21e 1951PP(pp_lt)
79072805 1952{
6f1401dc 1953 dVAR; dSP;
33efebe6
DM
1954 SV *left, *right;
1955
a42d0242 1956 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
33efebe6
DM
1957 right = POPs;
1958 left = TOPs;
1959 SETs(boolSV(
1960 (SvIOK_notUV(left) && SvIOK_notUV(right))
1961 ? (SvIVX(left) < SvIVX(right))
1962 : (do_ncmp(left, right) == -1)
1963 ));
1964 RETURN;
a0d0e21e 1965}
79072805 1966
a0d0e21e
LW
1967PP(pp_gt)
1968{
6f1401dc 1969 dVAR; dSP;
33efebe6 1970 SV *left, *right;
1b6737cc 1971
33efebe6
DM
1972 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1973 right = POPs;
1974 left = TOPs;
1975 SETs(boolSV(
1976 (SvIOK_notUV(left) && SvIOK_notUV(right))
1977 ? (SvIVX(left) > SvIVX(right))
1978 : (do_ncmp(left, right) == 1)
1979 ));
1980 RETURN;
a0d0e21e
LW
1981}
1982
1983PP(pp_le)
1984{
6f1401dc 1985 dVAR; dSP;
33efebe6 1986 SV *left, *right;
1b6737cc 1987
33efebe6
DM
1988 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1989 right = POPs;
1990 left = TOPs;
1991 SETs(boolSV(
1992 (SvIOK_notUV(left) && SvIOK_notUV(right))
1993 ? (SvIVX(left) <= SvIVX(right))
1994 : (do_ncmp(left, right) <= 0)
1995 ));
1996 RETURN;
a0d0e21e
LW
1997}
1998
1999PP(pp_ge)
2000{
6f1401dc 2001 dVAR; dSP;
33efebe6
DM
2002 SV *left, *right;
2003
2004 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
2005 right = POPs;
2006 left = TOPs;
2007 SETs(boolSV(
2008 (SvIOK_notUV(left) && SvIOK_notUV(right))
2009 ? (SvIVX(left) >= SvIVX(right))
2010 : ( (do_ncmp(left, right) & 2) == 0)
2011 ));
2012 RETURN;
2013}
1b6737cc 2014
33efebe6
DM
2015PP(pp_ne)
2016{
2017 dVAR; dSP;
2018 SV *left, *right;
2019
2020 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2021 right = POPs;
2022 left = TOPs;
2023 SETs(boolSV(
2024 (SvIOK_notUV(left) && SvIOK_notUV(right))
2025 ? (SvIVX(left) != SvIVX(right))
2026 : (do_ncmp(left, right) != 0)
2027 ));
2028 RETURN;
2029}
1b6737cc 2030
33efebe6
DM
2031/* compare left and right SVs. Returns:
2032 * -1: <
2033 * 0: ==
2034 * 1: >
2035 * 2: left or right was a NaN
2036 */
2037I32
2038Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2039{
2040 dVAR;
1b6737cc 2041
33efebe6
DM
2042 PERL_ARGS_ASSERT_DO_NCMP;
2043#ifdef PERL_PRESERVE_IVUV
2044 SvIV_please_nomg(right);
2045 /* Fortunately it seems NaN isn't IOK */
2046 if (SvIOK(right)) {
2047 SvIV_please_nomg(left);
2048 if (SvIOK(left)) {
2049 if (!SvUOK(left)) {
2050 const IV leftiv = SvIVX(left);
2051 if (!SvUOK(right)) {
2052 /* ## IV <=> IV ## */
2053 const IV rightiv = SvIVX(right);
2054 return (leftiv > rightiv) - (leftiv < rightiv);
28e5dec8 2055 }
33efebe6
DM
2056 /* ## IV <=> UV ## */
2057 if (leftiv < 0)
2058 /* As (b) is a UV, it's >=0, so it must be < */
2059 return -1;
2060 {
2061 const UV rightuv = SvUVX(right);
2062 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
28e5dec8 2063 }
28e5dec8 2064 }
79072805 2065
33efebe6
DM
2066 if (SvUOK(right)) {
2067 /* ## UV <=> UV ## */
2068 const UV leftuv = SvUVX(left);
2069 const UV rightuv = SvUVX(right);
2070 return (leftuv > rightuv) - (leftuv < rightuv);
28e5dec8 2071 }
33efebe6
DM
2072 /* ## UV <=> IV ## */
2073 {
2074 const IV rightiv = SvIVX(right);
2075 if (rightiv < 0)
2076 /* As (a) is a UV, it's >=0, so it cannot be < */
2077 return 1;
2078 {
2079 const UV leftuv = SvUVX(left);
2080 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
28e5dec8 2081 }
28e5dec8 2082 }
33efebe6 2083 /* NOTREACHED */
28e5dec8
JH
2084 }
2085 }
2086#endif
a0d0e21e 2087 {
33efebe6
DM
2088 NV const rnv = SvNV_nomg(right);
2089 NV const lnv = SvNV_nomg(left);
2090
cab190d4 2091#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
33efebe6
DM
2092 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2093 return 2;
2094 }
2095 return (lnv > rnv) - (lnv < rnv);
cab190d4 2096#else
33efebe6
DM
2097 if (lnv < rnv)
2098 return -1;
2099 if (lnv > rnv)
2100 return 1;
2101 if (lnv == rnv)
2102 return 0;
2103 return 2;
cab190d4 2104#endif
a0d0e21e 2105 }
79072805
LW
2106}
2107
33efebe6 2108
a0d0e21e 2109PP(pp_ncmp)
79072805 2110{
33efebe6
DM
2111 dVAR; dSP;
2112 SV *left, *right;
2113 I32 value;
a42d0242 2114 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
33efebe6
DM
2115 right = POPs;
2116 left = TOPs;
2117 value = do_ncmp(left, right);
2118 if (value == 2) {
3280af22 2119 SETs(&PL_sv_undef);
79072805 2120 }
33efebe6
DM
2121 else {
2122 dTARGET;
2123 SETi(value);
2124 }
2125 RETURN;
a0d0e21e 2126}
79072805 2127
afd9910b 2128PP(pp_sle)
a0d0e21e 2129{
97aff369 2130 dVAR; dSP;
79072805 2131
afd9910b
NC
2132 int amg_type = sle_amg;
2133 int multiplier = 1;
2134 int rhs = 1;
79072805 2135
afd9910b
NC
2136 switch (PL_op->op_type) {
2137 case OP_SLT:
2138 amg_type = slt_amg;
2139 /* cmp < 0 */
2140 rhs = 0;
2141 break;
2142 case OP_SGT:
2143 amg_type = sgt_amg;
2144 /* cmp > 0 */
2145 multiplier = -1;
2146 rhs = 0;
2147 break;
2148 case OP_SGE:
2149 amg_type = sge_amg;
2150 /* cmp >= 0 */
2151 multiplier = -1;
2152 break;
79072805 2153 }
79072805 2154
6f1401dc 2155 tryAMAGICbin_MG(amg_type, AMGf_set);
a0d0e21e
LW
2156 {
2157 dPOPTOPssrl;
1b6737cc 2158 const int cmp = (IN_LOCALE_RUNTIME
078504b2
FC
2159 ? sv_cmp_locale_flags(left, right, 0)
2160 : sv_cmp_flags(left, right, 0));
afd9910b 2161 SETs(boolSV(cmp * multiplier < rhs));
a0d0e21e
LW
2162 RETURN;
2163 }
2164}
79072805 2165
36477c24 2166PP(pp_seq)
2167{
6f1401dc
DM
2168 dVAR; dSP;
2169 tryAMAGICbin_MG(seq_amg, AMGf_set);
36477c24 2170 {
2171 dPOPTOPssrl;
078504b2 2172 SETs(boolSV(sv_eq_flags(left, right, 0)));
a0d0e21e
LW
2173 RETURN;
2174 }
2175}
79072805 2176
a0d0e21e 2177PP(pp_sne)
79072805 2178{
6f1401dc
DM
2179 dVAR; dSP;
2180 tryAMAGICbin_MG(sne_amg, AMGf_set);
a0d0e21e
LW
2181 {
2182 dPOPTOPssrl;
078504b2 2183 SETs(boolSV(!sv_eq_flags(left, right, 0)));
a0d0e21e 2184 RETURN;
463ee0b2 2185 }
79072805
LW
2186}
2187
a0d0e21e 2188PP(pp_scmp)
79072805 2189{
6f1401dc
DM
2190 dVAR; dSP; dTARGET;
2191 tryAMAGICbin_MG(scmp_amg, 0);
a0d0e21e
LW
2192 {
2193 dPOPTOPssrl;
1b6737cc 2194 const int cmp = (IN_LOCALE_RUNTIME
078504b2
FC
2195 ? sv_cmp_locale_flags(left, right, 0)
2196 : sv_cmp_flags(left, right, 0));
bbce6d69 2197 SETi( cmp );
a0d0e21e
LW
2198 RETURN;
2199 }
2200}
79072805 2201
55497cff 2202PP(pp_bit_and)
2203{
6f1401dc
DM
2204 dVAR; dSP; dATARGET;
2205 tryAMAGICbin_MG(band_amg, AMGf_assign);
a0d0e21e
LW
2206 {
2207 dPOPTOPssrl;
4633a7c4 2208 if (SvNIOKp(left) || SvNIOKp(right)) {
b20c4ee1
FC
2209 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2210 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
d0ba1bd2 2211 if (PL_op->op_private & HINT_INTEGER) {
1b6737cc 2212 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
972b05a9 2213 SETi(i);
d0ba1bd2
JH
2214 }
2215 else {
1b6737cc 2216 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
972b05a9 2217 SETu(u);
d0ba1bd2 2218 }
b20c4ee1
FC
2219 if (left_ro_nonnum) SvNIOK_off(left);
2220 if (right_ro_nonnum) SvNIOK_off(right);
a0d0e21e
LW
2221 }
2222 else {
533c011a 2223 do_vop(PL_op->op_type, TARG, left, right);
a0d0e21e
LW
2224 SETTARG;
2225 }
2226 RETURN;
2227 }
2228}
79072805 2229
a0d0e21e
LW
2230PP(pp_bit_or)
2231{
3658c1f1
NC
2232 dVAR; dSP; dATARGET;
2233 const int op_type = PL_op->op_type;
2234
6f1401dc 2235 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
a0d0e21e
LW
2236 {
2237 dPOPTOPssrl;
4633a7c4 2238 if (SvNIOKp(left) || SvNIOKp(right)) {
b20c4ee1
FC
2239 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2240 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
d0ba1bd2 2241 if (PL_op->op_private & HINT_INTEGER) {
3658c1f1
NC
2242 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2243 const IV r = SvIV_nomg(right);
2244 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2245 SETi(result);
d0ba1bd2
JH
2246 }
2247 else {
3658c1f1
NC
2248 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2249 const UV r = SvUV_nomg(right);
2250 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2251 SETu(result);
d0ba1bd2 2252 }
b20c4ee1
FC
2253 if (left_ro_nonnum) SvNIOK_off(left);
2254 if (right_ro_nonnum) SvNIOK_off(right);
a0d0e21e
LW
2255 }
2256 else {
3658c1f1 2257 do_vop(op_type, TARG, left, right);
a0d0e21e
LW
2258 SETTARG;
2259 }
2260 RETURN;
79072805 2261 }
a0d0e21e 2262}
79072805 2263
a0d0e21e
LW
2264PP(pp_negate)
2265{
6f1401dc
DM
2266 dVAR; dSP; dTARGET;
2267 tryAMAGICun_MG(neg_amg, AMGf_numeric);
a0d0e21e 2268 {
6f1401dc 2269 SV * const sv = TOPs;
1b6737cc 2270 const int flags = SvFLAGS(sv);
a5b92898 2271
886a4465 2272 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
a5b92898
RB
2273 SvIV_please( sv );
2274 }
2275
28e5dec8
JH
2276 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2277 /* It's publicly an integer, or privately an integer-not-float */
2278 oops_its_an_int:
9b0e499b
GS
2279 if (SvIsUV(sv)) {
2280 if (SvIVX(sv) == IV_MIN) {
28e5dec8 2281 /* 2s complement assumption. */
9b0e499b
GS
2282 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2283 RETURN;
2284 }
2285 else if (SvUVX(sv) <= IV_MAX) {
beccb14c 2286 SETi(-SvIVX(sv));
9b0e499b
GS
2287 RETURN;
2288 }
2289 }
2290 else if (SvIVX(sv) != IV_MIN) {
2291 SETi(-SvIVX(sv));
2292 RETURN;
2293 }
28e5dec8
JH
2294#ifdef PERL_PRESERVE_IVUV
2295 else {
2296 SETu((UV)IV_MIN);
2297 RETURN;
2298 }
2299#endif
9b0e499b
GS
2300 }
2301 if (SvNIOKp(sv))
6f1401dc 2302 SETn(-SvNV_nomg(sv));
4633a7c4 2303 else if (SvPOKp(sv)) {
a0d0e21e 2304 STRLEN len;
6f1401dc 2305 const char * const s = SvPV_nomg_const(sv, len);
bbce6d69 2306 if (isIDFIRST(*s)) {
76f68e9b 2307 sv_setpvs(TARG, "-");
a0d0e21e 2308 sv_catsv(TARG, sv);
79072805 2309 }
a0d0e21e 2310 else if (*s == '+' || *s == '-') {
6f1401dc
DM
2311 sv_setsv_nomg(TARG, sv);
2312 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
79072805 2313 }
8eb28a70 2314 else if (DO_UTF8(sv)) {
6f1401dc 2315 SvIV_please_nomg(sv);
8eb28a70
JH
2316 if (SvIOK(sv))
2317 goto oops_its_an_int;
2318 if (SvNOK(sv))
6f1401dc 2319 sv_setnv(TARG, -SvNV_nomg(sv));
8eb28a70 2320 else {
76f68e9b 2321 sv_setpvs(TARG, "-");
8eb28a70
JH
2322 sv_catsv(TARG, sv);
2323 }
834a4ddd 2324 }
28e5dec8 2325 else {
6f1401dc 2326 SvIV_please_nomg(sv);
8eb28a70
JH
2327 if (SvIOK(sv))
2328 goto oops_its_an_int;
6f1401dc 2329 sv_setnv(TARG, -SvNV_nomg(sv));
28e5dec8 2330 }
a0d0e21e 2331 SETTARG;
79072805 2332 }
4633a7c4 2333 else
6f1401dc 2334 SETn(-SvNV_nomg(sv));
79072805 2335 }
a0d0e21e 2336 RETURN;
79072805
LW
2337}
2338
a0d0e21e 2339PP(pp_not)
79072805 2340{
6f1401dc
DM
2341 dVAR; dSP;
2342 tryAMAGICun_MG(not_amg, AMGf_set);
06c841cf 2343 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
a0d0e21e 2344 return NORMAL;
79072805
LW
2345}
2346
a0d0e21e 2347PP(pp_complement)
79072805 2348{
6f1401dc 2349 dVAR; dSP; dTARGET;
a42d0242 2350 tryAMAGICun_MG(compl_amg, AMGf_numeric);
a0d0e21e
LW
2351 {
2352 dTOPss;
4633a7c4 2353 if (SvNIOKp(sv)) {
d0ba1bd2 2354 if (PL_op->op_private & HINT_INTEGER) {
1b6737cc 2355 const IV i = ~SvIV_nomg(sv);
972b05a9 2356 SETi(i);
d0ba1bd2
JH
2357 }
2358 else {
1b6737cc 2359 const UV u = ~SvUV_nomg(sv);
972b05a9 2360 SETu(u);
d0ba1bd2 2361 }
a0d0e21e
LW
2362 }
2363 else {
51723571 2364 register U8 *tmps;
55497cff 2365 register I32 anum;
a0d0e21e
LW
2366 STRLEN len;
2367
10516c54 2368 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
891f9566 2369 sv_setsv_nomg(TARG, sv);
6f1401dc 2370 tmps = (U8*)SvPV_force_nomg(TARG, len);
a0d0e21e 2371 anum = len;
1d68d6cd 2372 if (SvUTF8(TARG)) {
a1ca4561 2373 /* Calculate exact length, let's not estimate. */
1d68d6cd 2374 STRLEN targlen = 0;
ba210ebe 2375 STRLEN l;
a1ca4561
YST
2376 UV nchar = 0;
2377 UV nwide = 0;
01f6e806 2378 U8 * const send = tmps + len;
74d49cd0
TS
2379 U8 * const origtmps = tmps;
2380 const UV utf8flags = UTF8_ALLOW_ANYUV;
1d68d6cd 2381
1d68d6cd 2382 while (tmps < send) {
74d49cd0
TS
2383 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2384 tmps += l;
5bbb0b5a 2385 targlen += UNISKIP(~c);
a1ca4561
YST
2386 nchar++;
2387 if (c > 0xff)
2388 nwide++;
1d68d6cd
SC
2389 }
2390
2391 /* Now rewind strings and write them. */
74d49cd0 2392 tmps = origtmps;
a1ca4561
YST
2393
2394 if (nwide) {
01f6e806
AL
2395 U8 *result;
2396 U8 *p;
2397
74d49cd0 2398 Newx(result, targlen + 1, U8);
01f6e806 2399 p = result;
a1ca4561 2400 while (tmps < send) {
74d49cd0
TS
2401 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2402 tmps += l;
01f6e806 2403 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
a1ca4561 2404 }
01f6e806 2405 *p = '\0';
c1c21316
NC
2406 sv_usepvn_flags(TARG, (char*)result, targlen,
2407 SV_HAS_TRAILING_NUL);
a1ca4561
YST
2408 SvUTF8_on(TARG);
2409 }
2410 else {
01f6e806
AL
2411 U8 *result;
2412 U8 *p;
2413
74d49cd0 2414 Newx(result, nchar + 1, U8);
01f6e806 2415 p = result;
a1ca4561 2416 while (tmps < send) {
74d49cd0
TS
2417 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2418 tmps += l;
01f6e806 2419 *p++ = ~c;
a1ca4561 2420 }
01f6e806 2421 *p = '\0';
c1c21316 2422 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
d0a21e00 2423 SvUTF8_off(TARG);
1d68d6cd 2424 }
ec93b65f 2425 SETTARG;
1d68d6cd
SC
2426 RETURN;
2427 }
a0d0e21e 2428#ifdef LIBERAL
51723571
JH
2429 {
2430 register long *tmpl;
2431 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2432 *tmps = ~*tmps;
2433 tmpl = (long*)tmps;
bb7a0f54 2434 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
51723571
JH
2435 *tmpl = ~*tmpl;
2436 tmps = (U8*)tmpl;
2437 }
a0d0e21e
LW
2438#endif
2439 for ( ; anum > 0; anum--, tmps++)
2440 *tmps = ~*tmps;
ec93b65f 2441 SETTARG;
a0d0e21e
LW
2442 }
2443 RETURN;
2444 }
79072805
LW
2445}
2446
a0d0e21e
LW
2447/* integer versions of some of the above */
2448
a0d0e21e 2449PP(pp_i_multiply)
79072805 2450{
6f1401dc
DM
2451 dVAR; dSP; dATARGET;
2452 tryAMAGICbin_MG(mult_amg, AMGf_assign);
a0d0e21e 2453 {
6f1401dc 2454 dPOPTOPiirl_nomg;
a0d0e21e
LW
2455 SETi( left * right );
2456 RETURN;
2457 }
79072805
LW
2458}
2459
a0d0e21e 2460PP(pp_i_divide)
79072805 2461{
85935d8e 2462 IV num;
6f1401dc
DM
2463 dVAR; dSP; dATARGET;
2464 tryAMAGICbin_MG(div_amg, AMGf_assign);
a0d0e21e 2465 {
6f1401dc 2466 dPOPTOPssrl;
85935d8e 2467 IV value = SvIV_nomg(right);
a0d0e21e 2468 if (value == 0)
ece1bcef 2469 DIE(aTHX_ "Illegal division by zero");
85935d8e 2470 num = SvIV_nomg(left);
a0cec769
YST
2471
2472 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2473 if (value == -1)
2474 value = - num;
2475 else
2476 value = num / value;
6f1401dc 2477 SETi(value);
a0d0e21e
LW
2478 RETURN;
2479 }
79072805
LW
2480}
2481
befad5d1 2482#if defined(__GLIBC__) && IVSIZE == 8
224ec323
JH
2483STATIC
2484PP(pp_i_modulo_0)
befad5d1
NC
2485#else
2486PP(pp_i_modulo)
2487#endif
224ec323
JH
2488{
2489 /* This is the vanilla old i_modulo. */
6f1401dc
DM
2490 dVAR; dSP; dATARGET;
2491 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2492 {
6f1401dc 2493 dPOPTOPiirl_nomg;
224ec323
JH
2494 if (!right)
2495 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2496 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2497 if (right == -1)
2498 SETi( 0 );
2499 else
2500 SETi( left % right );
224ec323
JH
2501 RETURN;
2502 }
2503}
2504
11010fa3 2505#if defined(__GLIBC__) && IVSIZE == 8
224ec323
JH
2506STATIC
2507PP(pp_i_modulo_1)
befad5d1 2508
224ec323 2509{
224ec323 2510 /* This is the i_modulo with the workaround for the _moddi3 bug
fce2b89e 2511 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
224ec323 2512 * See below for pp_i_modulo. */
6f1401dc
DM
2513 dVAR; dSP; dATARGET;
2514 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2515 {
6f1401dc 2516 dPOPTOPiirl_nomg;
224ec323
JH
2517 if (!right)
2518 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2519 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2520 if (right == -1)
2521 SETi( 0 );
2522 else
2523 SETi( left % PERL_ABS(right) );
224ec323
JH
2524 RETURN;
2525 }
224ec323
JH
2526}
2527
a0d0e21e 2528PP(pp_i_modulo)
79072805 2529{
6f1401dc
DM
2530 dVAR; dSP; dATARGET;
2531 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2532 {
6f1401dc 2533 dPOPTOPiirl_nomg;
224ec323
JH
2534 if (!right)
2535 DIE(aTHX_ "Illegal modulus zero");
2536 /* The assumption is to use hereafter the old vanilla version... */
2537 PL_op->op_ppaddr =
2538 PL_ppaddr[OP_I_MODULO] =
1c127fab 2539 Perl_pp_i_modulo_0;
224ec323
JH
2540 /* .. but if we have glibc, we might have a buggy _moddi3
2541 * (at least glicb 2.2.5 is known to have this bug), in other
2542 * words our integer modulus with negative quad as the second
2543 * argument might be broken. Test for this and re-patch the
2544 * opcode dispatch table if that is the case, remembering to
2545 * also apply the workaround so that this first round works
2546 * right, too. See [perl #9402] for more information. */
224ec323
JH
2547 {
2548 IV l = 3;
2549 IV r = -10;
2550 /* Cannot do this check with inlined IV constants since
2551 * that seems to work correctly even with the buggy glibc. */
2552 if (l % r == -3) {
2553 /* Yikes, we have the bug.
2554 * Patch in the workaround version. */
2555 PL_op->op_ppaddr =
2556 PL_ppaddr[OP_I_MODULO] =
2557 &Perl_pp_i_modulo_1;
2558 /* Make certain we work right this time, too. */
32fdb065 2559 right = PERL_ABS(right);
224ec323
JH
2560 }
2561 }
a0cec769
YST
2562 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2563 if (right == -1)
2564 SETi( 0 );
2565 else
2566 SETi( left % right );
224ec323
JH
2567 RETURN;
2568 }
79072805 2569}
befad5d1 2570#endif
79072805 2571
a0d0e21e 2572PP(pp_i_add)
79072805 2573{
6f1401dc
DM
2574 dVAR; dSP; dATARGET;
2575 tryAMAGICbin_MG(add_amg, AMGf_assign);
a0d0e21e 2576 {
6f1401dc 2577 dPOPTOPiirl_ul_nomg;
a0d0e21e
LW
2578 SETi( left + right );
2579 RETURN;
79072805 2580 }
79072805
LW
2581}
2582
a0d0e21e 2583PP(pp_i_subtract)
79072805 2584{
6f1401dc
DM
2585 dVAR; dSP; dATARGET;
2586 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
a0d0e21e 2587 {
6f1401dc 2588 dPOPTOPiirl_ul_nomg;
a0d0e21e
LW
2589 SETi( left - right );
2590 RETURN;
79072805 2591 }
79072805
LW
2592}
2593
a0d0e21e 2594PP(pp_i_lt)
79072805 2595{
6f1401dc
DM
2596 dVAR; dSP;
2597 tryAMAGICbin_MG(lt_amg, AMGf_set);
a0d0e21e 2598 {
96b6b87f 2599 dPOPTOPiirl_nomg;
54310121 2600 SETs(boolSV(left < right));
a0d0e21e
LW
2601 RETURN;
2602 }
79072805
LW
2603}
2604
a0d0e21e 2605PP(pp_i_gt)
79072805 2606{
6f1401dc
DM
2607 dVAR; dSP;
2608 tryAMAGICbin_MG(gt_amg, AMGf_set);
a0d0e21e 2609 {
96b6b87f 2610 dPOPTOPiirl_nomg;
54310121 2611 SETs(boolSV(left > right));
a0d0e21e
LW
2612 RETURN;
2613 }
79072805
LW
2614}
2615
a0d0e21e 2616PP(pp_i_le)
79072805 2617{
6f1401dc
DM
2618 dVAR; dSP;
2619 tryAMAGICbin_MG(le_amg, AMGf_set);
a0d0e21e 2620 {
96b6b87f 2621 dPOPTOPiirl_nomg;
54310121 2622 SETs(boolSV(left <= right));
a0d0e21e 2623 RETURN;
85e6fe83 2624 }
79072805
LW
2625}
2626
a0d0e21e 2627PP(pp_i_ge)
79072805 2628{
6f1401dc
DM
2629 dVAR; dSP;
2630 tryAMAGICbin_MG(ge_amg, AMGf_set);
a0d0e21e 2631 {
96b6b87f 2632 dPOPTOPiirl_nomg;
54310121 2633 SETs(boolSV(left >= right));
a0d0e21e
LW
2634 RETURN;
2635 }
79072805
LW
2636}
2637
a0d0e21e 2638PP(pp_i_eq)
79072805 2639{
6f1401dc
DM
2640 dVAR; dSP;
2641 tryAMAGICbin_MG(eq_amg, AMGf_set);
a0d0e21e 2642 {
96b6b87f 2643 dPOPTOPiirl_nomg;
54310121 2644 SETs(boolSV(left == right));
a0d0e21e
LW
2645 RETURN;
2646 }
79072805
LW
2647}
2648
a0d0e21e 2649PP(pp_i_ne)
79072805 2650{
6f1401dc
DM
2651 dVAR; dSP;
2652 tryAMAGICbin_MG(ne_amg, AMGf_set);
a0d0e21e 2653 {
96b6b87f 2654 dPOPTOPiirl_nomg;
54310121 2655 SETs(boolSV(left != right));
a0d0e21e
LW
2656 RETURN;
2657 }
79072805
LW
2658}
2659
a0d0e21e 2660PP(pp_i_ncmp)
79072805 2661{
6f1401dc
DM
2662 dVAR; dSP; dTARGET;
2663 tryAMAGICbin_MG(ncmp_amg, 0);
a0d0e21e 2664 {
96b6b87f 2665 dPOPTOPiirl_nomg;
a0d0e21e 2666 I32 value;
79072805 2667
a0d0e21e 2668 if (left > right)
79072805 2669 value = 1;
a0d0e21e 2670 else if (left < right)
79072805 2671 value = -1;
a0d0e21e 2672 else
79072805 2673 value = 0;
a0d0e21e
LW
2674 SETi(value);
2675 RETURN;
79072805 2676 }
85e6fe83
LW
2677}
2678
2679PP(pp_i_negate)
2680{
6f1401dc
DM
2681 dVAR; dSP; dTARGET;
2682 tryAMAGICun_MG(neg_amg, 0);
2683 {
2684 SV * const sv = TOPs;
2685 IV const i = SvIV_nomg(sv);
2686 SETi(-i);
2687 RETURN;
2688 }
85e6fe83
LW
2689}
2690
79072805
LW
2691/* High falutin' math. */
2692
2693PP(pp_atan2)
2694{
6f1401dc
DM
2695 dVAR; dSP; dTARGET;
2696 tryAMAGICbin_MG(atan2_amg, 0);
a0d0e21e 2697 {
096c060c 2698 dPOPTOPnnrl_nomg;
a1021d57 2699 SETn(Perl_atan2(left, right));
a0d0e21e
LW
2700 RETURN;
2701 }
79072805
LW
2702}
2703
2704PP(pp_sin)
2705{
71302fe3
NC
2706 dVAR; dSP; dTARGET;
2707 int amg_type = sin_amg;
2708 const char *neg_report = NULL;
bc81784a 2709 NV (*func)(NV) = Perl_sin;
71302fe3
NC
2710 const int op_type = PL_op->op_type;
2711
2712 switch (op_type) {
2713 case OP_COS:
2714 amg_type = cos_amg;
bc81784a 2715 func = Perl_cos;
71302fe3
NC
2716 break;
2717 case OP_EXP:
2718 amg_type = exp_amg;
bc81784a 2719 func = Perl_exp;
71302fe3
NC
2720 break;
2721 case OP_LOG:
2722 amg_type = log_amg;
bc81784a 2723 func = Perl_log;
71302fe3
NC
2724 neg_report = "log";
2725 break;
2726 case OP_SQRT:
2727 amg_type = sqrt_amg;
bc81784a 2728 func = Perl_sqrt;
71302fe3
NC
2729 neg_report = "sqrt";
2730 break;
a0d0e21e 2731 }
79072805 2732
6f1401dc
DM
2733
2734 tryAMAGICun_MG(amg_type, 0);
a0d0e21e 2735 {
6f1401dc
DM
2736 SV * const arg = POPs;
2737 const NV value = SvNV_nomg(arg);
71302fe3
NC
2738 if (neg_report) {
2739 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2740 SET_NUMERIC_STANDARD();
2741 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2742 }
2743 }
2744 XPUSHn(func(value));
a0d0e21e
LW
2745 RETURN;
2746 }
79072805
LW
2747}
2748
56cb0a1c
AD
2749/* Support Configure command-line overrides for rand() functions.
2750 After 5.005, perhaps we should replace this by Configure support
2751 for drand48(), random(), or rand(). For 5.005, though, maintain
2752 compatibility by calling rand() but allow the user to override it.
2753 See INSTALL for details. --Andy Dougherty 15 July 1998
2754*/
85ab1d1d
JH
2755/* Now it's after 5.005, and Configure supports drand48() and random(),
2756 in addition to rand(). So the overrides should not be needed any more.
2757 --Jarkko Hietaniemi 27 September 1998
2758 */
2759
2760#ifndef HAS_DRAND48_PROTO
20ce7b12 2761extern double drand48 (void);
56cb0a1c
AD
2762#endif
2763
79072805
LW
2764PP(pp_rand)
2765{
97aff369 2766 dVAR; dSP; dTARGET;
65202027 2767 NV value;
79072805
LW
2768 if (MAXARG < 1)
2769 value = 1.0;
2770 else
2771 value = POPn;
2772 if (value == 0.0)
2773 value = 1.0;
80252599 2774 if (!PL_srand_called) {
85ab1d1d 2775 (void)seedDrand01((Rand_seed_t)seed());
80252599 2776 PL_srand_called = TRUE;
93dc8474 2777 }
85ab1d1d 2778 value *= Drand01();
79072805
LW
2779 XPUSHn(value);
2780 RETURN;
2781}
2782
2783PP(pp_srand)
2784{
83832992 2785 dVAR; dSP; dTARGET;
0bd48802 2786 const UV anum = (MAXARG < 1) ? seed() : POPu;
85ab1d1d 2787 (void)seedDrand01((Rand_seed_t)anum);
80252599 2788 PL_srand_called = TRUE;
da1010ec
NC
2789 if (anum)
2790 XPUSHu(anum);
2791 else {
2792 /* Historically srand always returned true. We can avoid breaking
2793 that like this: */
2794 sv_setpvs(TARG, "0 but true");
2795 XPUSHTARG;
2796 }
83832992 2797 RETURN;
79072805
LW
2798}
2799
79072805
LW
2800PP(pp_int)
2801{
6f1401dc
DM
2802 dVAR; dSP; dTARGET;
2803 tryAMAGICun_MG(int_amg, AMGf_numeric);
774d564b 2804 {
6f1401dc
DM
2805 SV * const sv = TOPs;
2806 const IV iv = SvIV_nomg(sv);
28e5dec8
JH
2807 /* XXX it's arguable that compiler casting to IV might be subtly
2808 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2809 else preferring IV has introduced a subtle behaviour change bug. OTOH
2810 relying on floating point to be accurate is a bug. */
2811
c781a409 2812 if (!SvOK(sv)) {
922c4365 2813 SETu(0);
c781a409
RD
2814 }
2815 else if (SvIOK(sv)) {
2816 if (SvIsUV(sv))
6f1401dc 2817 SETu(SvUV_nomg(sv));
c781a409 2818 else
28e5dec8 2819 SETi(iv);
c781a409 2820 }
c781a409 2821 else {
6f1401dc 2822 const NV value = SvNV_nomg(sv);
1048ea30 2823 if (value >= 0.0) {
28e5dec8
JH
2824 if (value < (NV)UV_MAX + 0.5) {
2825 SETu(U_V(value));
2826 } else {
059a1014 2827 SETn(Perl_floor(value));
28e5dec8 2828 }
1048ea30 2829 }
28e5dec8
JH
2830 else {
2831 if (value > (NV)IV_MIN - 0.5) {
2832 SETi(I_V(value));
2833 } else {
1bbae031 2834 SETn(Perl_ceil(value));
28e5dec8
JH
2835 }
2836 }
774d564b 2837 }
79072805 2838 }
79072805
LW
2839 RETURN;
2840}
2841
463ee0b2
LW
2842PP(pp_abs)
2843{
6f1401dc
DM
2844 dVAR; dSP; dTARGET;
2845 tryAMAGICun_MG(abs_amg, AMGf_numeric);
a0d0e21e 2846 {
6f1401dc 2847 SV * const sv = TOPs;
28e5dec8 2848 /* This will cache the NV value if string isn't actually integer */
6f1401dc 2849 const IV iv = SvIV_nomg(sv);
a227d84d 2850
800401ee 2851 if (!SvOK(sv)) {
922c4365 2852 SETu(0);
800401ee
JH
2853 }
2854 else if (SvIOK(sv)) {
28e5dec8 2855 /* IVX is precise */
800401ee 2856 if (SvIsUV(sv)) {
6f1401dc 2857 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
28e5dec8
JH
2858 } else {
2859 if (iv >= 0) {
2860 SETi(iv);
2861 } else {
2862 if (iv != IV_MIN) {
2863 SETi(-iv);
2864 } else {
2865 /* 2s complement assumption. Also, not really needed as
2866 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2867 SETu(IV_MIN);
2868 }
a227d84d 2869 }
28e5dec8
JH
2870 }
2871 } else{
6f1401dc 2872 const NV value = SvNV_nomg(sv);
774d564b 2873 if (value < 0.0)
1b6737cc 2874 SETn(-value);
a4474c9e
DD
2875 else
2876 SETn(value);
774d564b 2877 }
a0d0e21e 2878 }
774d564b 2879 RETURN;
463ee0b2
LW
2880}
2881
79072805
LW
2882PP(pp_oct)
2883{
97aff369 2884 dVAR; dSP; dTARGET;
5c144d81 2885 const char *tmps;
53305cf1 2886 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
6f894ead 2887 STRLEN len;
53305cf1
NC
2888 NV result_nv;
2889 UV result_uv;
1b6737cc 2890 SV* const sv = POPs;
79072805 2891
349d4f2f 2892 tmps = (SvPV_const(sv, len));
2bc69dc4
NIS
2893 if (DO_UTF8(sv)) {
2894 /* If Unicode, try to downgrade
2895 * If not possible, croak. */
1b6737cc 2896 SV* const tsv = sv_2mortal(newSVsv(sv));
2bc69dc4
NIS
2897
2898 SvUTF8_on(tsv);
2899 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 2900 tmps = SvPV_const(tsv, len);
2bc69dc4 2901 }
daa2adfd
NC
2902 if (PL_op->op_type == OP_HEX)
2903 goto hex;
2904
6f894ead 2905 while (*tmps && len && isSPACE(*tmps))
53305cf1 2906 tmps++, len--;
9e24b6e2 2907 if (*tmps == '0')
53305cf1 2908 tmps++, len--;
a674e8db 2909 if (*tmps == 'x' || *tmps == 'X') {
daa2adfd 2910 hex:
53305cf1 2911 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
daa2adfd 2912 }
a674e8db 2913 else if (*tmps == 'b' || *tmps == 'B')
53305cf1 2914 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
464e2e8a 2915 else
53305cf1
NC
2916 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2917
2918 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2919 XPUSHn(result_nv);
2920 }
2921 else {
2922 XPUSHu(result_uv);
2923 }
79072805
LW
2924 RETURN;
2925}
2926
2927/* String stuff. */
2928
2929PP(pp_length)
2930{
97aff369 2931 dVAR; dSP; dTARGET;
0bd48802 2932 SV * const sv = TOPs;
a0ed51b3 2933
656266fc 2934 if (SvGAMAGIC(sv)) {
9f621bb0
NC
2935 /* For an overloaded or magic scalar, we can't know in advance if
2936 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2937 it likes to cache the length. Maybe that should be a documented
2938 feature of it.
92331800
NC
2939 */
2940 STRLEN len;
9f621bb0
NC
2941 const char *const p
2942 = sv_2pv_flags(sv, &len,
2943 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
92331800 2944
d88e091f 2945 if (!p) {
9407f9c1
DL
2946 if (!SvPADTMP(TARG)) {
2947 sv_setsv(TARG, &PL_sv_undef);
2948 SETTARG;
2949 }
2950 SETs(&PL_sv_undef);
d88e091f 2951 }
9f621bb0 2952 else if (DO_UTF8(sv)) {
899be101 2953 SETi(utf8_length((U8*)p, (U8*)p + len));
92331800
NC
2954 }
2955 else
2956 SETi(len);
656266fc 2957 } else if (SvOK(sv)) {
9f621bb0
NC
2958 /* Neither magic nor overloaded. */
2959 if (DO_UTF8(sv))
2960 SETi(sv_len_utf8(sv));
2961 else
2962 SETi(sv_len(sv));
656266fc 2963 } else {
9407f9c1
DL
2964 if (!SvPADTMP(TARG)) {
2965 sv_setsv_nomg(TARG, &PL_sv_undef);
2966 SETTARG;
2967 }
2968 SETs(&PL_sv_undef);
92331800 2969 }
79072805
LW
2970 RETURN;
2971}
2972
2973PP(pp_substr)
2974{
97aff369 2975 dVAR; dSP; dTARGET;
79072805 2976 SV *sv;
463ee0b2 2977 STRLEN curlen;
9402d6ed 2978 STRLEN utf8_curlen;
777f7c56
EB
2979 SV * pos_sv;
2980 IV pos1_iv;
2981 int pos1_is_uv;
2982 IV pos2_iv;
2983 int pos2_is_uv;
2984 SV * len_sv;
2985 IV len_iv = 0;
2986 int len_is_uv = 1;
050e6362 2987 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
e1ec3a88 2988 const char *tmps;
777f7c56 2989 const IV arybase = CopARYBASE_get(PL_curcop);
9402d6ed 2990 SV *repl_sv = NULL;
cbbf8932 2991 const char *repl = NULL;
7b8d334a 2992 STRLEN repl_len;
050e6362 2993 const int num_args = PL_op->op_private & 7;
13e30c65 2994 bool repl_need_utf8_upgrade = FALSE;
9402d6ed 2995 bool repl_is_utf8 = FALSE;
79072805 2996
78f9721b
SM
2997 if (num_args > 2) {
2998 if (num_args > 3) {
9402d6ed 2999 repl_sv = POPs;
83003860 3000 repl = SvPV_const(repl_sv, repl_len);
9402d6ed 3001 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
7b8d334a 3002 }
777f7c56
EB
3003 len_sv = POPs;
3004 len_iv = SvIV(len_sv);
3005 len_is_uv = SvIOK_UV(len_sv);
5d82c453 3006 }
777f7c56
EB
3007 pos_sv = POPs;
3008 pos1_iv = SvIV(pos_sv);
3009 pos1_is_uv = SvIOK_UV(pos_sv);
79072805 3010 sv = POPs;
849ca7ee 3011 PUTBACK;
9402d6ed
JH
3012 if (repl_sv) {
3013 if (repl_is_utf8) {
3014 if (!DO_UTF8(sv))
3015 sv_utf8_upgrade(sv);
3016 }
13e30c65
JH
3017 else if (DO_UTF8(sv))
3018 repl_need_utf8_upgrade = TRUE;
9402d6ed 3019 }
5c144d81 3020 tmps = SvPV_const(sv, curlen);
7e2040f0 3021 if (DO_UTF8(sv)) {
9402d6ed
JH
3022 utf8_curlen = sv_len_utf8(sv);
3023 if (utf8_curlen == curlen)
3024 utf8_curlen = 0;
a0ed51b3 3025 else
9402d6ed 3026 curlen = utf8_curlen;
a0ed51b3 3027 }
d1c2b58a 3028 else
9402d6ed 3029 utf8_curlen = 0;
a0ed51b3 3030
777f7c56
EB
3031 if ( (pos1_is_uv && arybase < 0) || (pos1_iv >= arybase) ) { /* pos >= $[ */
3032 UV pos1_uv = pos1_iv-arybase;
3033 /* Overflow can occur when $[ < 0 */
3034 if (arybase < 0 && pos1_uv < (UV)pos1_iv)
1c900557 3035 goto bound_fail;
777f7c56
EB
3036 pos1_iv = pos1_uv;
3037 pos1_is_uv = 1;
3038 }
3039 else if (pos1_is_uv ? (UV)pos1_iv > 0 : pos1_iv > 0) {
1c900557 3040 goto bound_fail; /* $[=3; substr($_,2,...) */
777f7c56
EB
3041 }
3042 else { /* pos < $[ */
3043 if (pos1_iv == 0) { /* $[=1; substr($_,0,...) */
3044 pos1_iv = curlen;
3045 pos1_is_uv = 1;
3046 } else {
3047 if (curlen) {
3048 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3049 pos1_iv += curlen;
3050 }
5d82c453 3051 }
68dc0745 3052 }
777f7c56
EB
3053 if (pos1_is_uv || pos1_iv > 0) {
3054 if ((UV)pos1_iv > curlen)
1c900557 3055 goto bound_fail;
777f7c56
EB
3056 }
3057
3058 if (num_args > 2) {
3059 if (!len_is_uv && len_iv < 0) {
3060 pos2_iv = curlen + len_iv;
3061 if (curlen)
3062 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3063 else
3064 pos2_is_uv = 0;
3065 } else { /* len_iv >= 0 */
3066 if (!pos1_is_uv && pos1_iv < 0) {
3067 pos2_iv = pos1_iv + len_iv;
3068 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3069 } else {
3070 if ((UV)len_iv > curlen-(UV)pos1_iv)
3071 pos2_iv = curlen;
3072 else
3073 pos2_iv = pos1_iv+len_iv;
3074 pos2_is_uv = 1;
3075 }
5d82c453 3076 }
2304df62 3077 }
79072805 3078 else {
777f7c56
EB
3079 pos2_iv = curlen;
3080 pos2_is_uv = 1;
3081 }
3082
3083 if (!pos2_is_uv && pos2_iv < 0) {
3084 if (!pos1_is_uv && pos1_iv < 0)
1c900557 3085 goto bound_fail;
777f7c56
EB
3086 pos2_iv = 0;
3087 }
3088 else if (!pos1_is_uv && pos1_iv < 0)
3089 pos1_iv = 0;
3090
3091 if ((UV)pos2_iv < (UV)pos1_iv)
3092 pos2_iv = pos1_iv;
3093 if ((UV)pos2_iv > curlen)
3094 pos2_iv = curlen;
3095
3096 {
3097 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3098 const STRLEN pos = (STRLEN)( (UV)pos1_iv );
3099 const STRLEN len = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
777f7c56 3100 STRLEN byte_len = len;
d931b1be
NC
3101 STRLEN byte_pos = utf8_curlen
3102 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3103
2154eca7
EB
3104 if (lvalue && !repl) {
3105 SV * ret;
3106
3107 if (!SvGMAGICAL(sv)) {
3108 if (SvROK(sv)) {
3109 SvPV_force_nolen(sv);
3110 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3111 "Attempt to use reference as lvalue in substr");
3112 }
3113 if (isGV_with_GP(sv))
3114 SvPV_force_nolen(sv);
3115 else if (SvOK(sv)) /* is it defined ? */
3116 (void)SvPOK_only_UTF8(sv);
3117 else
3118 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
781e7547 3119 }
2154eca7
EB
3120
3121 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3122 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3123 LvTYPE(ret) = 'x';
3124 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3125 LvTARGOFF(ret) = pos;
3126 LvTARGLEN(ret) = len;
3127
3128 SPAGAIN;
3129 PUSHs(ret); /* avoid SvSETMAGIC here */
3130 RETURN;
781e7547
DM
3131 }
3132
2154eca7
EB
3133 SvTAINTED_off(TARG); /* decontaminate */
3134 SvUTF8_off(TARG); /* decontaminate */
3135
3136 tmps += byte_pos;
777f7c56 3137 sv_setpvn(TARG, tmps, byte_len);
12aa1545 3138#ifdef USE_LOCALE_COLLATE
14befaf4 3139 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
12aa1545 3140#endif
9402d6ed 3141 if (utf8_curlen)
7f66633b 3142 SvUTF8_on(TARG);
2154eca7 3143
f7928d6c 3144 if (repl) {
13e30c65
JH
3145 SV* repl_sv_copy = NULL;
3146
3147 if (repl_need_utf8_upgrade) {
3148 repl_sv_copy = newSVsv(repl_sv);
3149 sv_utf8_upgrade(repl_sv_copy);
349d4f2f 3150 repl = SvPV_const(repl_sv_copy, repl_len);
13e30c65
JH
3151 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3152 }
502d9230
VP
3153 if (!SvOK(sv))
3154 sv_setpvs(sv, "");
777f7c56 3155 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
9402d6ed 3156 if (repl_is_utf8)
f7928d6c 3157 SvUTF8_on(sv);
ef8d46e8 3158 SvREFCNT_dec(repl_sv_copy);
f7928d6c 3159 }
79072805 3160 }
849ca7ee 3161 SPAGAIN;
e27c778f
FC
3162 SvSETMAGIC(TARG);
3163 PUSHs(TARG);
79072805 3164 RETURN;
777f7c56 3165
1c900557 3166bound_fail:
777f7c56
EB
3167 if (lvalue || repl)
3168 Perl_croak(aTHX_ "substr outside of string");
3169 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3170 RETPUSHUNDEF;
79072805
LW
3171}
3172
3173PP(pp_vec)
3174{
2154eca7 3175 dVAR; dSP;
1b6737cc
AL
3176 register const IV size = POPi;
3177 register const IV offset = POPi;
3178 register SV * const src = POPs;
3179 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2154eca7 3180 SV * ret;
a0d0e21e 3181
81e118e0 3182 if (lvalue) { /* it's an lvalue! */
2154eca7
EB
3183 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3184 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3185 LvTYPE(ret) = 'v';
3186 LvTARG(ret) = SvREFCNT_inc_simple(src);
3187 LvTARGOFF(ret) = offset;
3188 LvTARGLEN(ret) = size;
3189 }
3190 else {
3191 dTARGET;
3192 SvTAINTED_off(TARG); /* decontaminate */
3193 ret = TARG;
79072805
LW
3194 }
3195
2154eca7
EB
3196 sv_setuv(ret, do_vecget(src, offset, size));
3197 PUSHs(ret);
79072805
LW
3198 RETURN;
3199}
3200
3201PP(pp_index)
3202{
97aff369 3203 dVAR; dSP; dTARGET;
79072805
LW
3204 SV *big;
3205 SV *little;
c445ea15 3206 SV *temp = NULL;
ad66a58c 3207 STRLEN biglen;
2723d216 3208 STRLEN llen = 0;
79072805
LW
3209 I32 offset;
3210 I32 retval;
73ee8be2
NC
3211 const char *big_p;
3212 const char *little_p;
fc15ae8f 3213 const I32 arybase = CopARYBASE_get(PL_curcop);
2f040f7f
NC
3214 bool big_utf8;
3215 bool little_utf8;
2723d216 3216 const bool is_index = PL_op->op_type == OP_INDEX;
79072805 3217
2723d216
NC
3218 if (MAXARG >= 3) {
3219 /* arybase is in characters, like offset, so combine prior to the
3220 UTF-8 to bytes calculation. */
79072805 3221 offset = POPi - arybase;
2723d216 3222 }
79072805
LW
3223 little = POPs;
3224 big = POPs;
73ee8be2
NC
3225 big_p = SvPV_const(big, biglen);
3226 little_p = SvPV_const(little, llen);
3227
e609e586
NC
3228 big_utf8 = DO_UTF8(big);
3229 little_utf8 = DO_UTF8(little);
3230 if (big_utf8 ^ little_utf8) {
3231 /* One needs to be upgraded. */
2f040f7f
NC
3232 if (little_utf8 && !PL_encoding) {
3233 /* Well, maybe instead we might be able to downgrade the small
3234 string? */
1eced8f8 3235 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
2f040f7f
NC
3236 &little_utf8);
3237 if (little_utf8) {
3238 /* If the large string is ISO-8859-1, and it's not possible to
3239 convert the small string to ISO-8859-1, then there is no
3240 way that it could be found anywhere by index. */
3241 retval = -1;
3242 goto fail;
3243 }
e609e586 3244
2f040f7f
NC
3245 /* At this point, pv is a malloc()ed string. So donate it to temp
3246 to ensure it will get free()d */
3247 little = temp = newSV(0);
73ee8be2
NC
3248 sv_usepvn(temp, pv, llen);
3249 little_p = SvPVX(little);
e609e586 3250 } else {
73ee8be2
NC
3251 temp = little_utf8
3252 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
2f040f7f
NC
3253
3254 if (PL_encoding) {
3255 sv_recode_to_utf8(temp, PL_encoding);
3256 } else {
3257 sv_utf8_upgrade(temp);
3258 }
3259 if (little_utf8) {
3260 big = temp;
3261 big_utf8 = TRUE;
73ee8be2 3262 big_p = SvPV_const(big, biglen);
2f040f7f
NC
3263 } else {
3264 little = temp;
73ee8be2 3265 little_p = SvPV_const(little, llen);
2f040f7f 3266 }
e609e586
NC
3267 }
3268 }
73ee8be2
NC
3269 if (SvGAMAGIC(big)) {
3270 /* Life just becomes a lot easier if I use a temporary here.
3271 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3272 will trigger magic and overloading again, as will fbm_instr()
3273 */
59cd0e26
NC
3274 big = newSVpvn_flags(big_p, biglen,
3275 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3276 big_p = SvPVX(big);
3277 }
e4e44778 3278 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
73ee8be2
NC
3279 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3280 warn on undef, and we've already triggered a warning with the
3281 SvPV_const some lines above. We can't remove that, as we need to
3282 call some SvPV to trigger overloading early and find out if the
3283 string is UTF-8.
3284 This is all getting to messy. The API isn't quite clean enough,
3285 because data access has side effects.
3286 */
59cd0e26
NC
3287 little = newSVpvn_flags(little_p, llen,
3288 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3289 little_p = SvPVX(little);
3290 }
e609e586 3291
79072805 3292 if (MAXARG < 3)
2723d216 3293 offset = is_index ? 0 : biglen;
a0ed51b3 3294 else {
ad66a58c 3295 if (big_utf8 && offset > 0)
a0ed51b3 3296 sv_pos_u2b(big, &offset, 0);
73ee8be2
NC
3297 if (!is_index)
3298 offset += llen;
a0ed51b3 3299 }
79072805
LW
3300 if (offset < 0)
3301 offset = 0;
ad66a58c
NC
3302 else if (offset > (I32)biglen)
3303 offset = biglen;
73ee8be2
NC
3304 if (!(little_p = is_index
3305 ? fbm_instr((unsigned char*)big_p + offset,
3306 (unsigned char*)big_p + biglen, little, 0)
3307 : rninstr(big_p, big_p + offset,
3308 little_p, little_p + llen)))
a0ed51b3 3309 retval = -1;
ad66a58c 3310 else {
73ee8be2 3311 retval = little_p - big_p;
ad66a58c
NC
3312 if (retval > 0 && big_utf8)
3313 sv_pos_b2u(big, &retval);
3314 }
ef8d46e8 3315 SvREFCNT_dec(temp);
2723d216 3316 fail:
a0ed51b3 3317 PUSHi(retval + arybase);
79072805
LW
3318 RETURN;
3319}
3320
3321PP(pp_sprintf)
3322{
97aff369 3323 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3e6bd4bf 3324 SvTAINTED_off(TARG);
79072805 3325 do_sprintf(TARG, SP-MARK, MARK+1);
bbce6d69 3326 TAINT_IF(SvTAINTED(TARG));
79072805
LW
3327 SP = ORIGMARK;
3328 PUSHTARG;
3329 RETURN;
3330}
3331
79072805
LW
3332PP(pp_ord)
3333{
97aff369 3334 dVAR; dSP; dTARGET;
1eced8f8 3335
7df053ec 3336 SV *argsv = POPs;
ba210ebe 3337 STRLEN len;
349d4f2f 3338 const U8 *s = (U8*)SvPV_const(argsv, len);
121910a4 3339
799ef3cb 3340 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
1eced8f8 3341 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
799ef3cb 3342 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
121910a4
JH
3343 argsv = tmpsv;
3344 }
79072805 3345
872c91ae 3346 XPUSHu(DO_UTF8(argsv) ?
89ebb4a3 3347 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
5fc32dea 3348 (UV)(*s & 0xff));
68795e93 3349
79072805
LW
3350 RETURN;
3351}
3352
463ee0b2
LW
3353PP(pp_chr)
3354{
97aff369 3355 dVAR; dSP; dTARGET;
463ee0b2 3356 char *tmps;
8a064bd6
JH
3357 UV value;
3358
3359 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3360 ||
3361 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3362 if (IN_BYTES) {
3363 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3364 } else {
3365 (void) POPs; /* Ignore the argument value. */
3366 value = UNICODE_REPLACEMENT;
3367 }
3368 } else {
3369 value = POPu;
3370 }
463ee0b2 3371
862a34c6 3372 SvUPGRADE(TARG,SVt_PV);
a0ed51b3 3373
0064a8a9 3374 if (value > 255 && !IN_BYTES) {
eb160463 3375 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
62961d2e 3376 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
349d4f2f 3377 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
a0ed51b3
LW
3378 *tmps = '\0';
3379 (void)SvPOK_only(TARG);
aa6ffa16 3380 SvUTF8_on(TARG);
a0ed51b3
LW
3381 XPUSHs(TARG);
3382 RETURN;
3383 }
3384
748a9306 3385 SvGROW(TARG,2);
463ee0b2
LW
3386 SvCUR_set(TARG, 1);
3387 tmps = SvPVX(TARG);
eb160463 3388 *tmps++ = (char)value;
748a9306 3389 *tmps = '\0';
a0d0e21e 3390 (void)SvPOK_only(TARG);
4c5ed6e2 3391
88632417 3392 if (PL_encoding && !IN_BYTES) {
799ef3cb 3393 sv_recode_to_utf8(TARG, PL_encoding);
88632417
JH
3394 tmps = SvPVX(TARG);
3395 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
4c5ed6e2
TS
3396 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3397 SvGROW(TARG, 2);
d5a15ac2 3398 tmps = SvPVX(TARG);
4c5ed6e2
TS
3399 SvCUR_set(TARG, 1);
3400 *tmps++ = (char)value;
88632417 3401 *tmps = '\0';
4c5ed6e2 3402 SvUTF8_off(TARG);
88632417
JH
3403 }
3404 }
4c5ed6e2 3405
463ee0b2
LW
3406 XPUSHs(TARG);
3407 RETURN;
3408}
3409
79072805
LW
3410PP(pp_crypt)
3411{
79072805 3412#ifdef HAS_CRYPT
97aff369 3413 dVAR; dSP; dTARGET;
5f74f29c 3414 dPOPTOPssrl;
85c16d83 3415 STRLEN len;
10516c54 3416 const char *tmps = SvPV_const(left, len);
2bc69dc4 3417
85c16d83 3418 if (DO_UTF8(left)) {
2bc69dc4 3419 /* If Unicode, try to downgrade.
f2791508
JH
3420 * If not possible, croak.
3421 * Yes, we made this up. */
1b6737cc 3422 SV* const tsv = sv_2mortal(newSVsv(left));
2bc69dc4 3423
f2791508 3424 SvUTF8_on(tsv);
2bc69dc4 3425 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 3426 tmps = SvPV_const(tsv, len);
85c16d83 3427 }
05404ffe
JH
3428# ifdef USE_ITHREADS
3429# ifdef HAS_CRYPT_R
3430 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3431 /* This should be threadsafe because in ithreads there is only
3432 * one thread per interpreter. If this would not be true,
3433 * we would need a mutex to protect this malloc. */
3434 PL_reentrant_buffer->_crypt_struct_buffer =
3435 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3436#if defined(__GLIBC__) || defined(__EMX__)
3437 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3438 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3439 /* work around glibc-2.2.5 bug */
3440 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3441 }
05404ffe 3442#endif
6ab58e4d 3443 }
05404ffe
JH
3444# endif /* HAS_CRYPT_R */
3445# endif /* USE_ITHREADS */
5f74f29c 3446# ifdef FCRYPT
83003860 3447 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3448# else
83003860 3449 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3450# endif
ec93b65f 3451 SETTARG;
4808266b 3452 RETURN;
79072805 3453#else
b13b2135 3454 DIE(aTHX_
79072805
LW
3455 "The crypt() function is unimplemented due to excessive paranoia.");
3456#endif
79072805
LW
3457}
3458
00f254e2
KW
3459/* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3460 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3461
00f254e2
KW
3462/* Below are several macros that generate code */
3463/* Generates code to store a unicode codepoint c that is known to occupy
3464 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3465#define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3466 STMT_START { \
3467 *(p) = UTF8_TWO_BYTE_HI(c); \
3468 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3469 } STMT_END
3470
3471/* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3472 * available byte after the two bytes */
3473#define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3474 STMT_START { \
3475 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3476 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3477 } STMT_END
3478
3479/* Generates code to store the upper case of latin1 character l which is known
3480 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3481 * are only two characters that fit this description, and this macro knows
3482 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3483 * bytes */
3484#define STORE_NON_LATIN1_UC(p, l) \
3485STMT_START { \
3486 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3487 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3488 } else { /* Must be the following letter */ \
3489 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3490 } \
3491} STMT_END
3492
3493/* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3494 * after the character stored */
3495#define CAT_NON_LATIN1_UC(p, l) \
3496STMT_START { \
3497 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3498 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3499 } else { \
3500 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3501 } \
3502} STMT_END
3503
3504/* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3505 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3506 * and must require two bytes to store it. Advances p to point to the next
3507 * available position */
3508#define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3509STMT_START { \
3510 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3511 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3512 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3513 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3514 } else {/* else is one of the other two special cases */ \
3515 CAT_NON_LATIN1_UC((p), (l)); \
3516 } \
3517} STMT_END
3518
79072805
LW
3519PP(pp_ucfirst)
3520{
00f254e2
KW
3521 /* Actually is both lcfirst() and ucfirst(). Only the first character
3522 * changes. This means that possibly we can change in-place, ie., just
3523 * take the source and change that one character and store it back, but not
3524 * if read-only etc, or if the length changes */
3525
97aff369 3526 dVAR;
39644a26 3527 dSP;
d54190f6 3528 SV *source = TOPs;
00f254e2 3529 STRLEN slen; /* slen is the byte length of the whole SV. */
d54190f6
NC
3530 STRLEN need;
3531 SV *dest;
00f254e2
KW
3532 bool inplace; /* ? Convert first char only, in-place */
3533 bool doing_utf8 = FALSE; /* ? using utf8 */
3534 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
12e9c124 3535 const int op_type = PL_op->op_type;
d54190f6
NC
3536 const U8 *s;
3537 U8 *d;
3538 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
00f254e2
KW
3539 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3540 * stored as UTF-8 at s. */
3541 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3542 * lowercased) character stored in tmpbuf. May be either
3543 * UTF-8 or not, but in either case is the number of bytes */
d54190f6
NC
3544
3545 SvGETMAGIC(source);
3546 if (SvOK(source)) {
3547 s = (const U8*)SvPV_nomg_const(source, slen);
3548 } else {
0a0ffbce
RGS
3549 if (ckWARN(WARN_UNINITIALIZED))
3550 report_uninit(source);
1eced8f8 3551 s = (const U8*)"";
d54190f6
NC
3552 slen = 0;
3553 }
a0ed51b3 3554
00f254e2
KW
3555 /* We may be able to get away with changing only the first character, in
3556 * place, but not if read-only, etc. Later we may discover more reasons to
3557 * not convert in-place. */
3558 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3559
3560 /* First calculate what the changed first character should be. This affects
3561 * whether we can just swap it out, leaving the rest of the string unchanged,
3562 * or even if have to convert the dest to UTF-8 when the source isn't */
3563
3564 if (! slen) { /* If empty */
3565 need = 1; /* still need a trailing NUL */
3566 }
3567 else if (DO_UTF8(source)) { /* Is the source utf8? */
d54190f6 3568 doing_utf8 = TRUE;
00f254e2
KW
3569
3570/* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3571 * and doesn't allow for the user to specify their own. When code is added to
3572 * detect if there is a user-defined mapping in force here, and if so to use
3573 * that, then the code below can be compiled. The detection would be a good
3574 * thing anyway, as currently the user-defined mappings only work on utf8
3575 * strings, and thus depend on the chosen internal storage method, which is a
3576 * bad thing */
3577#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3578 if (UTF8_IS_INVARIANT(*s)) {
3579
3580 /* An invariant source character is either ASCII or, in EBCDIC, an
3581 * ASCII equivalent or a caseless C1 control. In both these cases,
3582 * the lower and upper cases of any character are also invariants
3583 * (and title case is the same as upper case). So it is safe to
3584 * use the simple case change macros which avoid the overhead of
3585 * the general functions. Note that if perl were to be extended to
3586 * do locale handling in UTF-8 strings, this wouldn't be true in,
3587 * for example, Lithuanian or Turkic. */
3588 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3589 tculen = ulen = 1;
3590 need = slen + 1;
12e9c124 3591 }
00f254e2
KW
3592 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3593 U8 chr;
3594
3595 /* Similarly, if the source character isn't invariant but is in the
3596 * latin1 range (or EBCDIC equivalent thereof), we have the case
3597 * changes compiled into perl, and can avoid the overhead of the
3598 * general functions. In this range, the characters are stored as
3599 * two UTF-8 bytes, and it so happens that any changed-case version
3600 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3601 tculen = ulen = 2;
3602 need = slen + 1;
3603
3604 /* Convert the two source bytes to a single Unicode code point
3605 * value, change case and save for below */
356979f4 3606 chr = TWO_BYTE_UTF8_TO_UNI(*s, *(s+1));
00f254e2
KW
3607 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3608 U8 lower = toLOWER_LATIN1(chr);
3609 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3610 }
3611 else { /* ucfirst */
3612 U8 upper = toUPPER_LATIN1_MOD(chr);
3613
3614 /* Most of the latin1 range characters are well-behaved. Their
3615 * title and upper cases are the same, and are also in the
3616 * latin1 range. The macro above returns their upper (hence
3617 * title) case, and all that need be done is to save the result
3618 * for below. However, several characters are problematic, and
3619 * have to be handled specially. The MOD in the macro name
3620 * above means that these tricky characters all get mapped to
3621 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
3622 * This mapping saves some tests for the majority of the
3623 * characters */
3624
3625 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3626
3627 /* Not tricky. Just save it. */
3628 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
3629 }
3630 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
3631
3632 /* This one is tricky because it is two characters long,
3633 * though the UTF-8 is still two bytes, so the stored
3634 * length doesn't change */
3635 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
3636 *(tmpbuf + 1) = 's';
3637 }
3638 else {
3639
3640 /* The other two have their title and upper cases the same,
3641 * but are tricky because the changed-case characters
3642 * aren't in the latin1 range. They, however, do fit into
3643 * two UTF-8 bytes */
3644 STORE_NON_LATIN1_UC(tmpbuf, chr);
3645 }
3646 }
3647 }
3648 else {
3649#endif /* end of dont want to break user-defined casing */
3650
3651 /* Here, can't short-cut the general case */
3652
3653 utf8_to_uvchr(s, &ulen);
3654 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
3655 else toLOWER_utf8(s, tmpbuf, &tculen);
3656
3657 /* we can't do in-place if the length changes. */
3658 if (ulen != tculen) inplace = FALSE;
3659 need = slen + 1 - ulen + tculen;
3660#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3661 }
3662#endif
d54190f6 3663 }
00f254e2
KW
3664 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3665 * latin1 is treated as caseless. Note that a locale takes
3666 * precedence */
3667 tculen = 1; /* Most characters will require one byte, but this will
3668 * need to be overridden for the tricky ones */
3669 need = slen + 1;
3670
3671 if (op_type == OP_LCFIRST) {
d54190f6 3672
00f254e2
KW