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