This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add a Configure test to see if NV 0.0 is stored as all bits zero, and
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
241d1a3b 4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
79072805
LW
5 *
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.
8 *
a0d0e21e 9 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
645c22ef
DM
10 *
11 *
5e045b90
AMS
12 * This file contains the code that creates, manipulates and destroys
13 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
14 * structure of an SV, so their creation and destruction is handled
15 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
16 * level functions (eg. substr, split, join) for each of the types are
17 * in the pp*.c files.
79072805
LW
18 */
19
20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_SV_C
79072805 22#include "perl.h"
d2f185dc 23#include "regcomp.h"
79072805 24
51371543 25#define FCALL *f
2c5424a7 26
2f8ed50e
OS
27#ifdef __Lynx__
28/* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
30#endif
31
e23c8137
JH
32#ifdef PERL_UTF8_CACHE_ASSERT
33/* The cache element 0 is the Unicode offset;
34 * the cache element 1 is the byte offset of the element 0;
35 * the cache element 2 is the Unicode length of the substring;
36 * the cache element 3 is the byte length of the substring;
37 * The checking of the substring side would be good
38 * but substr() has enough code paths to make my head spin;
39 * if adding more checks watch out for the following tests:
40 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
41 * lib/utf8.t lib/Unicode/Collate/t/index.t
42 * --jhi
43 */
44#define ASSERT_UTF8_CACHE(cache) \
45 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
46#else
47#define ASSERT_UTF8_CACHE(cache) NOOP
48#endif
49
f8c7b90f 50#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 51#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
607fa7f2 52#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
b5ccf5f2 53/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
765f542d 54 on-write. */
765f542d 55#endif
645c22ef
DM
56
57/* ============================================================================
58
59=head1 Allocation and deallocation of SVs.
60
5e045b90
AMS
61An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
62av, hv...) contains type and reference count information, as well as a
63pointer to the body (struct xrv, xpv, xpviv...), which contains fields
64specific to each type.
65
4977e971
NC
66Normally, this allocation is done using arenas, which by default are
67approximately 4K chunks of memory parcelled up into N heads or bodies. The
68first slot in each arena is reserved, and is used to hold a link to the next
69arena. In the case of heads, the unused first slot also contains some flags
70and a note of the number of slots. Snaked through each arena chain is a
5e045b90 71linked list of free items; when this becomes empty, an extra arena is
4977e971 72allocated and divided up into N items which are threaded into the free list.
645c22ef
DM
73
74The following global variables are associated with arenas:
75
76 PL_sv_arenaroot pointer to list of SV arenas
77 PL_sv_root pointer to list of free SV structures
78
79 PL_foo_arenaroot pointer to list of foo arenas,
80 PL_foo_root pointer to list of free foo bodies
81 ... for foo in xiv, xnv, xrv, xpv etc.
82
83Note that some of the larger and more rarely used body types (eg xpvio)
84are not allocated using arenas, but are instead just malloc()/free()ed as
85required. Also, if PURIFY is defined, arenas are abandoned altogether,
86with all items individually malloc()ed. In addition, a few SV heads are
87not allocated from an arena, but are instead directly created as static
4977e971
NC
88or auto variables, eg PL_sv_undef. The size of arenas can be changed from
89the default by setting PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
90
91The SV arena serves the secondary purpose of allowing still-live SVs
92to be located and destroyed during final cleanup.
93
94At the lowest level, the macros new_SV() and del_SV() grab and free
95an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96to return the SV to the free list with error checking.) new_SV() calls
97more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98SVs in the free list have their SvTYPE field set to all ones.
99
100Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101that allocate and return individual body types. Normally these are mapped
ff276b08
RG
102to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103instead mapped directly to malloc()/free() if PURIFY is defined. The
645c22ef
DM
104new/del functions remove from, or add to, the appropriate PL_foo_root
105list, and call more_xiv() etc to add a new arena if the list is empty.
106
ff276b08 107At the time of very final cleanup, sv_free_arenas() is called from
645c22ef
DM
108perl_destruct() to physically free all the arenas allocated since the
109start of the interpreter. Note that this also clears PL_he_arenaroot,
110which is otherwise dealt with in hv.c.
111
112Manipulation of any of the PL_*root pointers is protected by enclosing
113LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114if threads are enabled.
115
116The function visit() scans the SV arenas list, and calls a specified
117function for each SV it finds which is still live - ie which has an SvTYPE
118other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119following functions (specified as [function that calls visit()] / [function
120called by visit() for each SV]):
121
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
124
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
131 below.
132
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
142
143=head2 Summary
144
145Private API to rest of sv.c
146
147 new_SV(), del_SV(),
148
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
151 etc
152
153Public API:
154
8cf8f3d1 155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef
DM
156
157
158=cut
159
160============================================================================ */
161
162
51371543 163
4561caa4
CS
164/*
165 * "A time to plant, and a time to uproot what was planted..."
166 */
167
cac9b346 168
fd0854ff
DM
169#ifdef DEBUG_LEAKING_SCALARS
170# ifdef NETWARE
171# define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
172# else
173# define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
174# endif
175#else
176# define FREE_SV_DEBUG_FILE(sv)
177#endif
178
053fc874
GS
179#define plant_SV(p) \
180 STMT_START { \
fd0854ff 181 FREE_SV_DEBUG_FILE(p); \
053fc874
GS
182 SvANY(p) = (void *)PL_sv_root; \
183 SvFLAGS(p) = SVTYPEMASK; \
184 PL_sv_root = (p); \
185 --PL_sv_count; \
186 } STMT_END
a0d0e21e 187
fba3b22e 188/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
189#define uproot_SV(p) \
190 STMT_START { \
191 (p) = PL_sv_root; \
192 PL_sv_root = (SV*)SvANY(p); \
193 ++PL_sv_count; \
194 } STMT_END
195
645c22ef 196
cac9b346
NC
197/* make some more SVs by adding another arena */
198
199/* sv_mutex must be held while calling more_sv() */
200STATIC SV*
201S_more_sv(pTHX)
202{
203 SV* sv;
204
205 if (PL_nice_chunk) {
206 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
207 PL_nice_chunk = Nullch;
208 PL_nice_chunk_size = 0;
209 }
210 else {
211 char *chunk; /* must use New here to match call to */
2e7ed132
NC
212 New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
213 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
cac9b346
NC
214 }
215 uproot_SV(sv);
216 return sv;
217}
218
645c22ef
DM
219/* new_SV(): return a new, empty SV head */
220
eba0f806
DM
221#ifdef DEBUG_LEAKING_SCALARS
222/* provide a real function for a debugger to play with */
223STATIC SV*
224S_new_SV(pTHX)
225{
226 SV* sv;
227
228 LOCK_SV_MUTEX;
229 if (PL_sv_root)
230 uproot_SV(sv);
231 else
cac9b346 232 sv = S_more_sv(aTHX);
eba0f806
DM
233 UNLOCK_SV_MUTEX;
234 SvANY(sv) = 0;
235 SvREFCNT(sv) = 1;
236 SvFLAGS(sv) = 0;
fd0854ff
DM
237 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
238 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
239 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
240 sv->sv_debug_inpad = 0;
241 sv->sv_debug_cloned = 0;
242# ifdef NETWARE
243 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
244# else
245 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
246# endif
247
eba0f806
DM
248 return sv;
249}
250# define new_SV(p) (p)=S_new_SV(aTHX)
251
252#else
253# define new_SV(p) \
053fc874
GS
254 STMT_START { \
255 LOCK_SV_MUTEX; \
256 if (PL_sv_root) \
257 uproot_SV(p); \
258 else \
cac9b346 259 (p) = S_more_sv(aTHX); \
053fc874
GS
260 UNLOCK_SV_MUTEX; \
261 SvANY(p) = 0; \
262 SvREFCNT(p) = 1; \
263 SvFLAGS(p) = 0; \
264 } STMT_END
eba0f806 265#endif
463ee0b2 266
645c22ef
DM
267
268/* del_SV(): return an empty SV head to the free list */
269
a0d0e21e 270#ifdef DEBUGGING
4561caa4 271
053fc874
GS
272#define del_SV(p) \
273 STMT_START { \
274 LOCK_SV_MUTEX; \
aea4f609 275 if (DEBUG_D_TEST) \
053fc874
GS
276 del_sv(p); \
277 else \
278 plant_SV(p); \
279 UNLOCK_SV_MUTEX; \
280 } STMT_END
a0d0e21e 281
76e3520e 282STATIC void
cea2e8a9 283S_del_sv(pTHX_ SV *p)
463ee0b2 284{
aea4f609 285 if (DEBUG_D_TEST) {
4633a7c4 286 SV* sva;
a3b680e6 287 bool ok = 0;
3280af22 288 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
a3b680e6
AL
289 SV *sv = sva + 1;
290 SV *svend = &sva[SvREFCNT(sva)];
c0ff570e 291 if (p >= sv && p < svend) {
a0d0e21e 292 ok = 1;
c0ff570e
NC
293 break;
294 }
a0d0e21e
LW
295 }
296 if (!ok) {
0453d815 297 if (ckWARN_d(WARN_INTERNAL))
9014280d 298 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
299 "Attempt to free non-arena SV: 0x%"UVxf
300 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
301 return;
302 }
303 }
4561caa4 304 plant_SV(p);
463ee0b2 305}
a0d0e21e 306
4561caa4
CS
307#else /* ! DEBUGGING */
308
309#define del_SV(p) plant_SV(p)
310
311#endif /* DEBUGGING */
463ee0b2 312
645c22ef
DM
313
314/*
ccfc67b7
JH
315=head1 SV Manipulation Functions
316
645c22ef
DM
317=for apidoc sv_add_arena
318
319Given a chunk of memory, link it to the head of the list of arenas,
320and split it into a list of free SVs.
321
322=cut
323*/
324
4633a7c4 325void
864dbfa3 326Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 327{
4633a7c4 328 SV* sva = (SV*)ptr;
463ee0b2
LW
329 register SV* sv;
330 register SV* svend;
4633a7c4
LW
331
332 /* The first SV in an arena isn't an SV. */
3280af22 333 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
334 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
335 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
336
3280af22
NIS
337 PL_sv_arenaroot = sva;
338 PL_sv_root = sva + 1;
4633a7c4
LW
339
340 svend = &sva[SvREFCNT(sva) - 1];
341 sv = sva + 1;
463ee0b2 342 while (sv < svend) {
a0d0e21e 343 SvANY(sv) = (void *)(SV*)(sv + 1);
03e36789 344#ifdef DEBUGGING
978b032e 345 SvREFCNT(sv) = 0;
03e36789
NC
346#endif
347 /* Must always set typemask because it's awlays checked in on cleanup
348 when the arenas are walked looking for objects. */
8990e307 349 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
350 sv++;
351 }
352 SvANY(sv) = 0;
03e36789
NC
353#ifdef DEBUGGING
354 SvREFCNT(sv) = 0;
355#endif
4633a7c4
LW
356 SvFLAGS(sv) = SVTYPEMASK;
357}
358
055972dc
DM
359/* visit(): call the named function for each non-free SV in the arenas
360 * whose flags field matches the flags/mask args. */
645c22ef 361
5226ed68 362STATIC I32
055972dc 363S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 364{
4633a7c4 365 SV* sva;
5226ed68 366 I32 visited = 0;
8990e307 367
3280af22 368 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
a3b680e6
AL
369 register SV * const svend = &sva[SvREFCNT(sva)];
370 register SV* sv;
4561caa4 371 for (sv = sva + 1; sv < svend; ++sv) {
055972dc
DM
372 if (SvTYPE(sv) != SVTYPEMASK
373 && (sv->sv_flags & mask) == flags
374 && SvREFCNT(sv))
375 {
acfe0abc 376 (FCALL)(aTHX_ sv);
5226ed68
JH
377 ++visited;
378 }
8990e307
LW
379 }
380 }
5226ed68 381 return visited;
8990e307
LW
382}
383
758a08c3
JH
384#ifdef DEBUGGING
385
645c22ef
DM
386/* called by sv_report_used() for each live SV */
387
388static void
acfe0abc 389do_report_used(pTHX_ SV *sv)
645c22ef
DM
390{
391 if (SvTYPE(sv) != SVTYPEMASK) {
392 PerlIO_printf(Perl_debug_log, "****\n");
393 sv_dump(sv);
394 }
395}
758a08c3 396#endif
645c22ef
DM
397
398/*
399=for apidoc sv_report_used
400
401Dump the contents of all SVs not yet freed. (Debugging aid).
402
403=cut
404*/
405
8990e307 406void
864dbfa3 407Perl_sv_report_used(pTHX)
4561caa4 408{
ff270d3a 409#ifdef DEBUGGING
055972dc 410 visit(do_report_used, 0, 0);
ff270d3a 411#endif
4561caa4
CS
412}
413
645c22ef
DM
414/* called by sv_clean_objs() for each live SV */
415
416static void
acfe0abc 417do_clean_objs(pTHX_ SV *sv)
645c22ef
DM
418{
419 SV* rv;
420
421 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
422 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
423 if (SvWEAKREF(sv)) {
424 sv_del_backref(sv);
425 SvWEAKREF_off(sv);
b162af07 426 SvRV_set(sv, NULL);
645c22ef
DM
427 } else {
428 SvROK_off(sv);
b162af07 429 SvRV_set(sv, NULL);
645c22ef
DM
430 SvREFCNT_dec(rv);
431 }
432 }
433
434 /* XXX Might want to check arrays, etc. */
435}
436
437/* called by sv_clean_objs() for each live SV */
438
439#ifndef DISABLE_DESTRUCTOR_KLUDGE
440static void
acfe0abc 441do_clean_named_objs(pTHX_ SV *sv)
645c22ef
DM
442{
443 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
444 if ( SvOBJECT(GvSV(sv)) ||
445 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
446 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
447 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
448 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
449 {
450 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 451 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
452 SvREFCNT_dec(sv);
453 }
454 }
455}
456#endif
457
458/*
459=for apidoc sv_clean_objs
460
461Attempt to destroy all objects not yet freed
462
463=cut
464*/
465
4561caa4 466void
864dbfa3 467Perl_sv_clean_objs(pTHX)
4561caa4 468{
3280af22 469 PL_in_clean_objs = TRUE;
055972dc 470 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 471#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 472 /* some barnacles may yet remain, clinging to typeglobs */
055972dc 473 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 474#endif
3280af22 475 PL_in_clean_objs = FALSE;
4561caa4
CS
476}
477
645c22ef
DM
478/* called by sv_clean_all() for each live SV */
479
480static void
acfe0abc 481do_clean_all(pTHX_ SV *sv)
645c22ef
DM
482{
483 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
484 SvFLAGS(sv) |= SVf_BREAK;
0e705b3b
DM
485 if (PL_comppad == (AV*)sv) {
486 PL_comppad = Nullav;
487 PL_curpad = Null(SV**);
488 }
645c22ef
DM
489 SvREFCNT_dec(sv);
490}
491
492/*
493=for apidoc sv_clean_all
494
495Decrement the refcnt of each remaining SV, possibly triggering a
496cleanup. This function may have to be called multiple times to free
ff276b08 497SVs which are in complex self-referential hierarchies.
645c22ef
DM
498
499=cut
500*/
501
5226ed68 502I32
864dbfa3 503Perl_sv_clean_all(pTHX)
8990e307 504{
5226ed68 505 I32 cleaned;
3280af22 506 PL_in_clean_all = TRUE;
055972dc 507 cleaned = visit(do_clean_all, 0,0);
3280af22 508 PL_in_clean_all = FALSE;
5226ed68 509 return cleaned;
8990e307 510}
463ee0b2 511
645c22ef
DM
512/*
513=for apidoc sv_free_arenas
514
515Deallocate the memory used by all arenas. Note that all the individual SV
516heads and bodies within the arenas must already have been freed.
517
518=cut
519*/
520
4633a7c4 521void
864dbfa3 522Perl_sv_free_arenas(pTHX)
4633a7c4
LW
523{
524 SV* sva;
525 SV* svanext;
7b2c381c 526 void *arena, *arenanext;
8b4f5e17
JC
527 int i;
528 void **arenaroots[] = {
529 (void**) &PL_xnv_arenaroot,
530 (void**) &PL_xpv_arenaroot,
531 (void**) &PL_xpviv_arenaroot,
532 (void**) &PL_xpvnv_arenaroot,
533 (void**) &PL_xpvcv_arenaroot,
534 (void**) &PL_xpvav_arenaroot,
535 (void**) &PL_xpvhv_arenaroot,
536 (void**) &PL_xpvmg_arenaroot,
537 (void**) &PL_xpvgv_arenaroot,
538 (void**) &PL_xpvlv_arenaroot,
539 (void**) &PL_xpvbm_arenaroot,
6f51351d
NC
540 (void**) &PL_he_arenaroot,
541#if defined(USE_ITHREADS)
542 (void**) &PL_pte_arenaroot,
543#endif
8b4f5e17
JC
544 (void**) 0
545 };
546 void **roots[] = {
547 (void**) &PL_xnv_root,
548 (void**) &PL_xpv_root,
549 (void**) &PL_xpviv_root,
550 (void**) &PL_xpvnv_root,
551 (void**) &PL_xpvcv_root,
552 (void**) &PL_xpvav_root,
553 (void**) &PL_xpvhv_root,
554 (void**) &PL_xpvmg_root,
555 (void**) &PL_xpvgv_root,
556 (void**) &PL_xpvlv_root,
557 (void**) &PL_xpvbm_root,
6f51351d
NC
558 (void**) &PL_he_root,
559#if defined(USE_ITHREADS)
560 (void**) &PL_pte_root,
561#endif
8b4f5e17
JC
562 (void**) 0
563 };
4633a7c4
LW
564
565 /* Free arenas here, but be careful about fake ones. (We assume
566 contiguity of the fake ones with the corresponding real ones.) */
567
3280af22 568 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
569 svanext = (SV*) SvANY(sva);
570 while (svanext && SvFAKE(svanext))
571 svanext = (SV*) SvANY(svanext);
572
573 if (!SvFAKE(sva))
1df70142 574 Safefree(sva);
4633a7c4 575 }
8b4f5e17
JC
576
577 assert(sizeof(arenaroots) == sizeof(roots));
5f05dabc 578
8b4f5e17 579 for (i=0; arenaroots[i]; i++) {
727879eb 580
8b4f5e17
JC
581 arena = *arenaroots[i];
582 for (; arena; arena = arenanext) {
583 arenanext = *(void **)arena;
584 Safefree(arena);
585 }
586 *arenaroots[i] = 0;
587 *roots[i] = 0;
612f20c3 588 }
612f20c3 589
3280af22
NIS
590 if (PL_nice_chunk)
591 Safefree(PL_nice_chunk);
592 PL_nice_chunk = Nullch;
593 PL_nice_chunk_size = 0;
594 PL_sv_arenaroot = 0;
595 PL_sv_root = 0;
4633a7c4
LW
596}
597
29489e7c
DM
598/* ---------------------------------------------------------------------
599 *
600 * support functions for report_uninit()
601 */
602
603/* the maxiumum size of array or hash where we will scan looking
604 * for the undefined element that triggered the warning */
605
606#define FUV_MAX_SEARCH_SIZE 1000
607
608/* Look for an entry in the hash whose value has the same SV as val;
609 * If so, return a mortal copy of the key. */
610
611STATIC SV*
612S_find_hash_subscript(pTHX_ HV *hv, SV* val)
613{
27da23d5 614 dVAR;
29489e7c 615 register HE **array;
29489e7c
DM
616 I32 i;
617
618 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
619 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
620 return Nullsv;
621
622 array = HvARRAY(hv);
623
624 for (i=HvMAX(hv); i>0; i--) {
f54cb97a 625 register HE *entry;
29489e7c
DM
626 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
627 if (HeVAL(entry) != val)
628 continue;
629 if ( HeVAL(entry) == &PL_sv_undef ||
630 HeVAL(entry) == &PL_sv_placeholder)
631 continue;
632 if (!HeKEY(entry))
633 return Nullsv;
634 if (HeKLEN(entry) == HEf_SVKEY)
635 return sv_mortalcopy(HeKEY_sv(entry));
636 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
637 }
638 }
639 return Nullsv;
640}
641
642/* Look for an entry in the array whose value has the same SV as val;
643 * If so, return the index, otherwise return -1. */
644
645STATIC I32
646S_find_array_subscript(pTHX_ AV *av, SV* val)
647{
648 SV** svp;
649 I32 i;
650 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
651 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
652 return -1;
653
654 svp = AvARRAY(av);
655 for (i=AvFILLp(av); i>=0; i--) {
656 if (svp[i] == val && svp[i] != &PL_sv_undef)
657 return i;
658 }
659 return -1;
660}
661
662/* S_varname(): return the name of a variable, optionally with a subscript.
663 * If gv is non-zero, use the name of that global, along with gvtype (one
664 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
665 * targ. Depending on the value of the subscript_type flag, return:
666 */
667
668#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
669#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
670#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
671#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
672
673STATIC SV*
bfed75c6 674S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
29489e7c
DM
675 SV* keyname, I32 aindex, int subscript_type)
676{
677 AV *av;
a3b680e6 678 SV *sv;
29489e7c 679
a3b680e6 680 SV * const name = sv_newmortal();
29489e7c
DM
681 if (gv) {
682
683 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
684 * XXX get rid of all this if gv_fullnameX() ever supports this
685 * directly */
686
bfed75c6 687 const char *p;
29489e7c
DM
688 HV *hv = GvSTASH(gv);
689 sv_setpv(name, gvtype);
690 if (!hv)
691 p = "???";
bfcb3514 692 else if (!(p=HvNAME_get(hv)))
29489e7c 693 p = "__ANON__";
29489e7c
DM
694 if (strNE(p, "main")) {
695 sv_catpv(name,p);
696 sv_catpvn(name,"::", 2);
697 }
698 if (GvNAMELEN(gv)>= 1 &&
699 ((unsigned int)*GvNAME(gv)) <= 26)
700 { /* handle $^FOO */
701 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
702 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
703 }
704 else
705 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
706 }
707 else {
708 U32 u;
709 CV *cv = find_runcv(&u);
710 if (!cv || !CvPADLIST(cv))
711 return Nullsv;;
712 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
713 sv = *av_fetch(av, targ, FALSE);
714 /* SvLEN in a pad name is not to be trusted */
f9926b10 715 sv_setpv(name, SvPV_nolen_const(sv));
29489e7c
DM
716 }
717
718 if (subscript_type == FUV_SUBSCRIPT_HASH) {
719 *SvPVX(name) = '$';
720 sv = NEWSV(0,0);
721 Perl_sv_catpvf(aTHX_ name, "{%s}",
3f7c398e 722 pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
29489e7c
DM
723 SvREFCNT_dec(sv);
724 }
725 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
726 *SvPVX(name) = '$';
265a12b8 727 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
29489e7c
DM
728 }
729 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
730 sv_insert(name, 0, 0, "within ", 7);
731
732 return name;
733}
734
735
736/*
737=for apidoc find_uninit_var
738
739Find the name of the undefined variable (if any) that caused the operator o
740to issue a "Use of uninitialized value" warning.
741If match is true, only return a name if it's value matches uninit_sv.
742So roughly speaking, if a unary operator (such as OP_COS) generates a
743warning, then following the direct child of the op may yield an
744OP_PADSV or OP_GV that gives the name of the undefined variable. On the
745other hand, with OP_ADD there are two branches to follow, so we only print
746the variable name if we get an exact match.
747
748The name is returned as a mortal SV.
749
750Assumes that PL_op is the op that originally triggered the error, and that
751PL_comppad/PL_curpad points to the currently executing pad.
752
753=cut
754*/
755
756STATIC SV *
757S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
758{
27da23d5 759 dVAR;
29489e7c
DM
760 SV *sv;
761 AV *av;
762 SV **svp;
763 GV *gv;
764 OP *o, *o2, *kid;
765
766 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
767 uninit_sv == &PL_sv_placeholder)))
768 return Nullsv;
769
770 switch (obase->op_type) {
771
772 case OP_RV2AV:
773 case OP_RV2HV:
774 case OP_PADAV:
775 case OP_PADHV:
776 {
f54cb97a
AL
777 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
778 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
112dcc46
RGS
779 I32 index = 0;
780 SV *keysv = Nullsv;
29489e7c
DM
781 int subscript_type = FUV_SUBSCRIPT_WITHIN;
782
783 if (pad) { /* @lex, %lex */
784 sv = PAD_SVl(obase->op_targ);
785 gv = Nullgv;
786 }
787 else {
788 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
789 /* @global, %global */
790 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
791 if (!gv)
792 break;
793 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
794 }
795 else /* @{expr}, %{expr} */
796 return find_uninit_var(cUNOPx(obase)->op_first,
797 uninit_sv, match);
798 }
799
800 /* attempt to find a match within the aggregate */
801 if (hash) {
802 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
803 if (keysv)
804 subscript_type = FUV_SUBSCRIPT_HASH;
805 }
806 else {
807 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
808 if (index >= 0)
809 subscript_type = FUV_SUBSCRIPT_ARRAY;
810 }
811
812 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
813 break;
814
815 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
816 keysv, index, subscript_type);
817 }
818
819 case OP_PADSV:
820 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
821 break;
822 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
823 Nullsv, 0, FUV_SUBSCRIPT_NONE);
824
825 case OP_GVSV:
826 gv = cGVOPx_gv(obase);
827 if (!gv || (match && GvSV(gv) != uninit_sv))
828 break;
829 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
830
831 case OP_AELEMFAST:
832 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
833 if (match) {
834 av = (AV*)PAD_SV(obase->op_targ);
835 if (!av || SvRMAGICAL(av))
836 break;
837 svp = av_fetch(av, (I32)obase->op_private, FALSE);
838 if (!svp || *svp != uninit_sv)
839 break;
840 }
841 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
842 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
843 }
844 else {
845 gv = cGVOPx_gv(obase);
846 if (!gv)
847 break;
848 if (match) {
849 av = GvAV(gv);
850 if (!av || SvRMAGICAL(av))
851 break;
852 svp = av_fetch(av, (I32)obase->op_private, FALSE);
853 if (!svp || *svp != uninit_sv)
854 break;
855 }
856 return S_varname(aTHX_ gv, "$", 0,
857 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
858 }
859 break;
860
861 case OP_EXISTS:
862 o = cUNOPx(obase)->op_first;
863 if (!o || o->op_type != OP_NULL ||
864 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
865 break;
866 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
867
868 case OP_AELEM:
869 case OP_HELEM:
870 if (PL_op == obase)
871 /* $a[uninit_expr] or $h{uninit_expr} */
872 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
873
874 gv = Nullgv;
875 o = cBINOPx(obase)->op_first;
876 kid = cBINOPx(obase)->op_last;
877
878 /* get the av or hv, and optionally the gv */
879 sv = Nullsv;
880 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
881 sv = PAD_SV(o->op_targ);
882 }
883 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
884 && cUNOPo->op_first->op_type == OP_GV)
885 {
886 gv = cGVOPx_gv(cUNOPo->op_first);
887 if (!gv)
888 break;
889 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
890 }
891 if (!sv)
892 break;
893
894 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
895 /* index is constant */
896 if (match) {
897 if (SvMAGICAL(sv))
898 break;
899 if (obase->op_type == OP_HELEM) {
900 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
901 if (!he || HeVAL(he) != uninit_sv)
902 break;
903 }
904 else {
905 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
906 if (!svp || *svp != uninit_sv)
907 break;
908 }
909 }
910 if (obase->op_type == OP_HELEM)
911 return S_varname(aTHX_ gv, "%", o->op_targ,
912 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
913 else
914 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
915 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
916 ;
917 }
918 else {
919 /* index is an expression;
920 * attempt to find a match within the aggregate */
921 if (obase->op_type == OP_HELEM) {
922 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
923 if (keysv)
924 return S_varname(aTHX_ gv, "%", o->op_targ,
925 keysv, 0, FUV_SUBSCRIPT_HASH);
926 }
927 else {
f54cb97a 928 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
29489e7c 929 if (index >= 0)
f54cb97a 930 return S_varname(aTHX_ gv, "@", o->op_targ,
29489e7c
DM
931 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
932 }
933 if (match)
934 break;
935 return S_varname(aTHX_ gv,
936 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
937 ? "@" : "%",
938 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
939 }
940
941 break;
942
943 case OP_AASSIGN:
944 /* only examine RHS */
945 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
946
947 case OP_OPEN:
948 o = cUNOPx(obase)->op_first;
949 if (o->op_type == OP_PUSHMARK)
950 o = o->op_sibling;
951
952 if (!o->op_sibling) {
953 /* one-arg version of open is highly magical */
954
955 if (o->op_type == OP_GV) { /* open FOO; */
956 gv = cGVOPx_gv(o);
957 if (match && GvSV(gv) != uninit_sv)
958 break;
7a5fa8a2 959 return S_varname(aTHX_ gv, "$", 0,
29489e7c
DM
960 Nullsv, 0, FUV_SUBSCRIPT_NONE);
961 }
962 /* other possibilities not handled are:
963 * open $x; or open my $x; should return '${*$x}'
964 * open expr; should return '$'.expr ideally
965 */
966 break;
967 }
968 goto do_op;
969
970 /* ops where $_ may be an implicit arg */
971 case OP_TRANS:
972 case OP_SUBST:
973 case OP_MATCH:
974 if ( !(obase->op_flags & OPf_STACKED)) {
975 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
976 ? PAD_SVl(obase->op_targ)
977 : DEFSV))
978 {
979 sv = sv_newmortal();
616d8c9c 980 sv_setpvn(sv, "$_", 2);
29489e7c
DM
981 return sv;
982 }
983 }
984 goto do_op;
985
986 case OP_PRTF:
987 case OP_PRINT:
988 /* skip filehandle as it can't produce 'undef' warning */
989 o = cUNOPx(obase)->op_first;
990 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
991 o = o->op_sibling->op_sibling;
992 goto do_op2;
993
994
e21bd382 995 case OP_RV2SV:
29489e7c
DM
996 case OP_CUSTOM:
997 case OP_ENTERSUB:
998 match = 1; /* XS or custom code could trigger random warnings */
999 goto do_op;
1000
1001 case OP_SCHOMP:
1002 case OP_CHOMP:
1003 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1004 return sv_2mortal(newSVpv("${$/}", 0));
1005 /* FALL THROUGH */
1006
1007 default:
1008 do_op:
1009 if (!(obase->op_flags & OPf_KIDS))
1010 break;
1011 o = cUNOPx(obase)->op_first;
1012
1013 do_op2:
1014 if (!o)
1015 break;
1016
1017 /* if all except one arg are constant, or have no side-effects,
1018 * or are optimized away, then it's unambiguous */
1019 o2 = Nullop;
1020 for (kid=o; kid; kid = kid->op_sibling) {
1021 if (kid &&
1022 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1023 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1024 || (kid->op_type == OP_PUSHMARK)
1025 )
1026 )
1027 continue;
1028 if (o2) { /* more than one found */
1029 o2 = Nullop;
1030 break;
1031 }
1032 o2 = kid;
1033 }
1034 if (o2)
1035 return find_uninit_var(o2, uninit_sv, match);
1036
1037 /* scan all args */
1038 while (o) {
1039 sv = find_uninit_var(o, uninit_sv, 1);
1040 if (sv)
1041 return sv;
1042 o = o->op_sibling;
1043 }
1044 break;
1045 }
1046 return Nullsv;
1047}
1048
1049
645c22ef
DM
1050/*
1051=for apidoc report_uninit
1052
1053Print appropriate "Use of uninitialized variable" warning
1054
1055=cut
1056*/
1057
1d7c1841 1058void
29489e7c
DM
1059Perl_report_uninit(pTHX_ SV* uninit_sv)
1060{
1061 if (PL_op) {
112dcc46 1062 SV* varname = Nullsv;
29489e7c
DM
1063 if (uninit_sv) {
1064 varname = find_uninit_var(PL_op, uninit_sv,0);
1065 if (varname)
1066 sv_insert(varname, 0, 0, " ", 1);
1067 }
9014280d 1068 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
93524f2b 1069 varname ? SvPV_nolen_const(varname) : "",
29489e7c
DM
1070 " in ", OP_DESC(PL_op));
1071 }
1d7c1841 1072 else
29489e7c
DM
1073 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1074 "", "", "");
1d7c1841
GS
1075}
1076
de042e1d 1077STATIC void *
e3bbdc67 1078S_more_bodies (pTHX_ void **arena_root, void **root, size_t size)
cac9b346 1079{
e3bbdc67
NC
1080 char *start;
1081 const char *end;
1082 size_t count = PERL_ARENA_SIZE/size;
1083 New(0, start, count*size, char);
1084 *((void **) start) = *arena_root;
1085 *arena_root = (void *)start;
cac9b346 1086
e3bbdc67 1087 end = start + (count-1) * size;
cac9b346 1088
e3bbdc67
NC
1089 /* The initial slot is used to link the arenas together, so it isn't to be
1090 linked into the list of ready-to-use bodies. */
cac9b346 1091
e3bbdc67 1092 start += size;
cac9b346 1093
e3bbdc67 1094 *root = (void *)start;
cac9b346 1095
e3bbdc67
NC
1096 while (start < end) {
1097 char *next = start + size;
1098 *(void**) start = (void *)next;
1099 start = next;
cac9b346 1100 }
e3bbdc67 1101 *(void **)start = 0;
de042e1d
NC
1102
1103 return *root;
cac9b346
NC
1104}
1105
aeb18a1e 1106/* grab a new thing from the free list, allocating more if necessary */
645c22ef 1107
aeb18a1e 1108STATIC void *
dd690478 1109S_new_body(pTHX_ void **arena_root, void **root, size_t size)
932e9ff9 1110{
aeb18a1e 1111 void *xpv;
932e9ff9 1112 LOCK_SV_MUTEX;
aeb18a1e
NC
1113 xpv = *root ? *root : S_more_bodies(aTHX_ arena_root, root, size);
1114 *root = *(void**)xpv;
932e9ff9 1115 UNLOCK_SV_MUTEX;
dd690478 1116 return xpv;
932e9ff9
VB
1117}
1118
aeb18a1e 1119/* return a thing to the free list */
645c22ef 1120
932e9ff9 1121STATIC void
dd690478 1122S_del_body(pTHX_ void *thing, void **root)
932e9ff9
VB
1123{
1124 LOCK_SV_MUTEX;
dd690478
NC
1125 *(void **)thing = *root;
1126 *root = (void*)thing;
932e9ff9
VB
1127 UNLOCK_SV_MUTEX;
1128}
1129
aeb18a1e
NC
1130/* Conventionally we simply malloc() a big block of memory, then divide it
1131 up into lots of the thing that we're allocating.
645c22ef 1132
aeb18a1e
NC
1133 This macro will expand to call to S_new_body. So for XPVBM (with ithreads),
1134 it would become
932e9ff9 1135
aeb18a1e
NC
1136 S_new_body(my_perl, (void**)&(my_perl->Ixpvbm_arenaroot),
1137 (void**)&(my_perl->Ixpvbm_root), sizeof(XPVBM), 0)
1138*/
645c22ef 1139
aeb18a1e
NC
1140#define new_body(TYPE,lctype) \
1141 S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1142 (void**)&PL_ ## lctype ## _root, \
dd690478
NC
1143 sizeof(TYPE))
1144
1145#define del_body(p,TYPE,lctype) \
1146 S_del_body(aTHX_ (void*)p, (void**)&PL_ ## lctype ## _root)
aeb18a1e
NC
1147
1148/* But for some types, we cheat. The type starts with some members that are
1149 never accessed. So we allocate the substructure, starting at the first used
1150 member, then adjust the pointer back in memory by the size of the bit not
1151 allocated, so it's as if we allocated the full structure.
1152 (But things will all go boom if you write to the part that is "not there",
1153 because you'll be overwriting the last members of the preceding structure
1154 in memory.)
1155
1156 We calculate the correction using the STRUCT_OFFSET macro. For example, if
1157 xpv_allocated is the same structure as XPV then the two OFFSETs sum to zero,
1158 and the pointer is unchanged. If the allocated structure is smaller (no
1159 initial NV actually allocated) then the net effect is to subtract the size
1160 of the NV from the pointer, to return a new pointer as if an initial NV were
1161 actually allocated.
1162
1163 This is the same trick as was used for NV and IV bodies. Ironically it
1164 doesn't need to be used for NV bodies any more, because NV is now at the
1165 start of the structure. IV bodies don't need it either, because they are
1166 no longer allocated. */
1167
1168#define new_body_allocated(TYPE,lctype,member) \
dd690478
NC
1169 (void*)((char*)S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1170 (void**)&PL_ ## lctype ## _root, \
1171 sizeof(lctype ## _allocated)) - \
1172 STRUCT_OFFSET(TYPE, member) \
1173 + STRUCT_OFFSET(lctype ## _allocated, member))
aeb18a1e
NC
1174
1175
aeb18a1e 1176#define del_body_allocated(p,TYPE,lctype,member) \
dd690478
NC
1177 S_del_body(aTHX_ (void*)((char*)p + STRUCT_OFFSET(TYPE, member) \
1178 - STRUCT_OFFSET(lctype ## _allocated, member)), \
1179 (void**)&PL_ ## lctype ## _root)
932e9ff9 1180
7bab3ede
MB
1181#define my_safemalloc(s) (void*)safemalloc(s)
1182#define my_safefree(p) safefree((char*)p)
463ee0b2 1183
d33b2eba 1184#ifdef PURIFY
463ee0b2 1185
d33b2eba
GS
1186#define new_XNV() my_safemalloc(sizeof(XPVNV))
1187#define del_XNV(p) my_safefree(p)
463ee0b2 1188
d33b2eba
GS
1189#define new_XPV() my_safemalloc(sizeof(XPV))
1190#define del_XPV(p) my_safefree(p)
9b94d1dd 1191
d33b2eba
GS
1192#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1193#define del_XPVIV(p) my_safefree(p)
932e9ff9 1194
d33b2eba
GS
1195#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1196#define del_XPVNV(p) my_safefree(p)
932e9ff9 1197
d33b2eba
GS
1198#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1199#define del_XPVCV(p) my_safefree(p)
932e9ff9 1200
d33b2eba
GS
1201#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1202#define del_XPVAV(p) my_safefree(p)
1203
1204#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1205#define del_XPVHV(p) my_safefree(p)
1c846c1f 1206
d33b2eba
GS
1207#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1208#define del_XPVMG(p) my_safefree(p)
1209
727879eb
NC
1210#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1211#define del_XPVGV(p) my_safefree(p)
1212
d33b2eba
GS
1213#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1214#define del_XPVLV(p) my_safefree(p)
1215
1216#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1217#define del_XPVBM(p) my_safefree(p)
1218
1219#else /* !PURIFY */
1220
aeb18a1e
NC
1221#define new_XNV() new_body(NV, xnv)
1222#define del_XNV(p) del_body(p, NV, xnv)
9b94d1dd 1223
aeb18a1e
NC
1224#define new_XPV() new_body_allocated(XPV, xpv, xpv_cur)
1225#define del_XPV(p) del_body_allocated(p, XPV, xpv, xpv_cur)
d33b2eba 1226
aeb18a1e
NC
1227#define new_XPVIV() new_body_allocated(XPVIV, xpviv, xpv_cur)
1228#define del_XPVIV(p) del_body_allocated(p, XPVIV, xpviv, xpv_cur)
d33b2eba 1229
aeb18a1e
NC
1230#define new_XPVNV() new_body(XPVNV, xpvnv)
1231#define del_XPVNV(p) del_body(p, XPVNV, xpvnv)
d33b2eba 1232
aeb18a1e
NC
1233#define new_XPVCV() new_body(XPVCV, xpvcv)
1234#define del_XPVCV(p) del_body(p, XPVCV, xpvcv)
d33b2eba 1235
aeb18a1e
NC
1236#define new_XPVAV() new_body_allocated(XPVAV, xpvav, xav_fill)
1237#define del_XPVAV(p) del_body_allocated(p, XPVAV, xpvav, xav_fill)
d33b2eba 1238
aeb18a1e
NC
1239#define new_XPVHV() new_body_allocated(XPVHV, xpvhv, xhv_fill)
1240#define del_XPVHV(p) del_body_allocated(p, XPVHV, xpvhv, xhv_fill)
1c846c1f 1241
aeb18a1e
NC
1242#define new_XPVMG() new_body(XPVMG, xpvmg)
1243#define del_XPVMG(p) del_body(p, XPVMG, xpvmg)
d33b2eba 1244
aeb18a1e
NC
1245#define new_XPVGV() new_body(XPVGV, xpvgv)
1246#define del_XPVGV(p) del_body(p, XPVGV, xpvgv)
727879eb 1247
aeb18a1e
NC
1248#define new_XPVLV() new_body(XPVLV, xpvlv)
1249#define del_XPVLV(p) del_body(p, XPVLV, xpvlv)
d33b2eba 1250
aeb18a1e
NC
1251#define new_XPVBM() new_body(XPVBM, xpvbm)
1252#define del_XPVBM(p) del_body(p, XPVBM, xpvbm)
d33b2eba
GS
1253
1254#endif /* PURIFY */
9b94d1dd 1255
d33b2eba
GS
1256#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1257#define del_XPVFM(p) my_safefree(p)
1c846c1f 1258
d33b2eba
GS
1259#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1260#define del_XPVIO(p) my_safefree(p)
8990e307 1261
954c1994
GS
1262/*
1263=for apidoc sv_upgrade
1264
ff276b08 1265Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1266SV, then copies across as much information as possible from the old body.
ff276b08 1267You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1268
1269=cut
1270*/
1271
63f97190 1272void
864dbfa3 1273Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1274{
9a085840 1275 void** old_body_arena;
878cc751 1276 size_t old_body_offset;
4cbc76b1 1277 size_t old_body_length; /* Well, the length to copy. */
878cc751 1278 void* old_body;
4cbc76b1 1279 bool zero_nv = TRUE;
403d36eb
NC
1280 void* new_body;
1281 size_t new_body_length;
1282 size_t new_body_offset;
1283 void** new_body_arena;
1284 void** new_body_arenaroot;
4cbc76b1 1285 U32 old_type = SvTYPE(sv);
79072805 1286
765f542d
NC
1287 if (mt != SVt_PV && SvIsCOW(sv)) {
1288 sv_force_normal_flags(sv, 0);
f130fd45
NIS
1289 }
1290
79072805 1291 if (SvTYPE(sv) == mt)
63f97190 1292 return;
79072805 1293
f5282e15 1294 if (SvTYPE(sv) > mt)
921edb34
RGS
1295 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1296 (int)SvTYPE(sv), (int)mt);
f5282e15 1297
d2e56290 1298
878cc751
NC
1299 old_body = SvANY(sv);
1300 old_body_arena = 0;
1301 old_body_offset = 0;
4cbc76b1 1302 old_body_length = 0;
403d36eb
NC
1303 new_body_offset = 0;
1304 new_body_length = ~0;
1305
1306 /* Copying structures onto other structures that have been neatly zeroed
1307 has a subtle gotcha. Consider XPVMG
1308
1309 +------+------+------+------+------+-------+-------+
1310 | NV | CUR | LEN | IV | MAGIC | STASH |
1311 +------+------+------+------+------+-------+-------+
1312 0 4 8 12 16 20 24 28
1313
1314 where NVs are aligned to 8 bytes, so that sizeof that structure is
1315 actually 32 bytes long, with 4 bytes of padding at the end:
1316
1317 +------+------+------+------+------+-------+-------+------+
1318 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1319 +------+------+------+------+------+-------+-------+------+
1320 0 4 8 12 16 20 24 28 32
1321
1322 so what happens if you allocate memory for this structure:
1323
1324 +------+------+------+------+------+-------+-------+------+------+...
1325 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1326 +------+------+------+------+------+-------+-------+------+------+...
1327 0 4 8 12 16 20 24 28 32 36
1328
1329 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1330 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1331 started out as zero once, but it's quite possible that it isn't. So now,
1332 rather than a nicely zeroed GP, you have it pointing somewhere random.
1333 Bugs ensue.
1334
1335 (In fact, GP ends up pointing at a previous GP structure, because the
1336 principle cause of the padding in XPVMG getting garbage is a copy of
1337 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob)
1338
1339 So we are careful and work out the size of used parts of all the
1340 structures. */
878cc751 1341
79072805
LW
1342 switch (SvTYPE(sv)) {
1343 case SVt_NULL:
79072805 1344 break;
79072805 1345 case SVt_IV:
ed6116ce 1346 if (mt == SVt_NV)
463ee0b2 1347 mt = SVt_PVNV;
ed6116ce
LW
1348 else if (mt < SVt_PVIV)
1349 mt = SVt_PVIV;
4cbc76b1
NC
1350 old_body_offset = STRUCT_OFFSET(XPVIV, xiv_iv);
1351 old_body_length = sizeof(IV);
79072805
LW
1352 break;
1353 case SVt_NV:
9a085840 1354 old_body_arena = (void **) &PL_xnv_root;
4cbc76b1
NC
1355 old_body_length = sizeof(NV);
1356 zero_nv = FALSE;
878cc751 1357
ed6116ce 1358 if (mt < SVt_PVNV)
79072805
LW
1359 mt = SVt_PVNV;
1360 break;
ed6116ce 1361 case SVt_RV:
ed6116ce 1362 break;
79072805 1363 case SVt_PV:
9a085840 1364 old_body_arena = (void **) &PL_xpv_root;
878cc751
NC
1365 old_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1366 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
403d36eb
NC
1367 old_body_length = STRUCT_OFFSET(XPV, xpv_len)
1368 + sizeof (((XPV*)SvANY(sv))->xpv_len)
1369 - old_body_offset;
748a9306
LW
1370 if (mt <= SVt_IV)
1371 mt = SVt_PVIV;
1372 else if (mt == SVt_NV)
1373 mt = SVt_PVNV;
79072805
LW
1374 break;
1375 case SVt_PVIV:
9a085840 1376 old_body_arena = (void **) &PL_xpviv_root;
878cc751
NC
1377 old_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1378 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
403d36eb
NC
1379 old_body_length = STRUCT_OFFSET(XPVIV, xiv_u)
1380 + sizeof (((XPVIV*)SvANY(sv))->xiv_u)
1381 - old_body_offset;
79072805
LW
1382 break;
1383 case SVt_PVNV:
9a085840 1384 old_body_arena = (void **) &PL_xpvnv_root;
403d36eb
NC
1385 old_body_length = STRUCT_OFFSET(XPVNV, xiv_u)
1386 + sizeof (((XPVNV*)SvANY(sv))->xiv_u);
4cbc76b1 1387 zero_nv = FALSE;
79072805
LW
1388 break;
1389 case SVt_PVMG:
0ec50a73
NC
1390 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1391 there's no way that it can be safely upgraded, because perl.c
1392 expects to Safefree(SvANY(PL_mess_sv)) */
1393 assert(sv != PL_mess_sv);
bce8f412
NC
1394 /* This flag bit is used to mean other things in other scalar types.
1395 Given that it only has meaning inside the pad, it shouldn't be set
1396 on anything that can get upgraded. */
1397 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
9a085840 1398 old_body_arena = (void **) &PL_xpvmg_root;
403d36eb
NC
1399 old_body_length = STRUCT_OFFSET(XPVMG, xmg_stash)
1400 + sizeof (((XPVMG*)SvANY(sv))->xmg_stash);
4cbc76b1 1401 zero_nv = FALSE;
79072805
LW
1402 break;
1403 default:
cea2e8a9 1404 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1405 }
1406
ffb05e06
NC
1407 SvFLAGS(sv) &= ~SVTYPEMASK;
1408 SvFLAGS(sv) |= mt;
1409
79072805
LW
1410 switch (mt) {
1411 case SVt_NULL:
cea2e8a9 1412 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805 1413 case SVt_IV:
4cbc76b1 1414 assert(old_type == SVt_NULL);
339049b0 1415 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
403d36eb 1416 SvIV_set(sv, 0);
79072805
LW
1417 break;
1418 case SVt_NV:
4cbc76b1 1419 assert(old_type == SVt_NULL);
79072805 1420 SvANY(sv) = new_XNV();
403d36eb 1421 SvNV_set(sv, 0);
79072805 1422 break;
ed6116ce 1423 case SVt_RV:
4cbc76b1 1424 assert(old_type == SVt_NULL);
339049b0 1425 SvANY(sv) = &sv->sv_u.svu_rv;
403d36eb 1426 SvRV_set(sv, 0);
ed6116ce 1427 break;
79072805
LW
1428 case SVt_PVHV:
1429 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1430 HvFILL(sv) = 0;
1431 HvMAX(sv) = 0;
8aacddc1 1432 HvTOTALKEYS(sv) = 0;
bd4b1eb5 1433
2068cd4d
NC
1434 goto hv_av_common;
1435
1436 case SVt_PVAV:
1437 SvANY(sv) = new_XPVAV();
1438 AvMAX(sv) = -1;
1439 AvFILLp(sv) = -1;
1440 AvALLOC(sv) = 0;
1441 AvREAL_only(sv);
1442
1443 hv_av_common:
1444 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1445 The target created by newSVrv also is, and it can have magic.
1446 However, it never has SvPVX set.
1447 */
1448 if (old_type >= SVt_RV) {
1449 assert(SvPVX_const(sv) == 0);
8bd4d4c5 1450 }
2068cd4d
NC
1451
1452 /* Could put this in the else clause below, as PVMG must have SvPVX
1453 0 already (the assertion above) */
bd4b1eb5 1454 SvPV_set(sv, (char*)0);
2068cd4d
NC
1455
1456 if (old_type >= SVt_PVMG) {
1457 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_magic);
1458 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1459 } else {
1460 SvMAGIC_set(sv, 0);
1461 SvSTASH_set(sv, 0);
1462 }
79072805 1463 break;
bd4b1eb5
NC
1464
1465 case SVt_PVIO:
403d36eb
NC
1466 new_body = new_XPVIO();
1467 new_body_length = sizeof(XPVIO);
1468 goto zero;
bd4b1eb5 1469 case SVt_PVFM:
403d36eb
NC
1470 new_body = new_XPVFM();
1471 new_body_length = sizeof(XPVFM);
1472 goto zero;
1473
bd4b1eb5 1474 case SVt_PVBM:
403d36eb
NC
1475 new_body_length = sizeof(XPVBM);
1476 new_body_arena = (void **) &PL_xpvbm_root;
1477 new_body_arenaroot = (void **) &PL_xpvbm_arenaroot;
1478 goto new_body;
bd4b1eb5 1479 case SVt_PVGV:
403d36eb
NC
1480 new_body_length = sizeof(XPVGV);
1481 new_body_arena = (void **) &PL_xpvgv_root;
1482 new_body_arenaroot = (void **) &PL_xpvgv_arenaroot;
1483 goto new_body;
79072805 1484 case SVt_PVCV:
403d36eb
NC
1485 new_body_length = sizeof(XPVCV);
1486 new_body_arena = (void **) &PL_xpvcv_root;
1487 new_body_arenaroot = (void **) &PL_xpvcv_arenaroot;
1488 goto new_body;
bd4b1eb5 1489 case SVt_PVLV:
403d36eb
NC
1490 new_body_length = sizeof(XPVLV);
1491 new_body_arena = (void **) &PL_xpvlv_root;
1492 new_body_arenaroot = (void **) &PL_xpvlv_arenaroot;
1493 goto new_body;
1494 case SVt_PVMG:
1495 new_body_length = sizeof(XPVMG);
1496 new_body_arena = (void **) &PL_xpvmg_root;
1497 new_body_arenaroot = (void **) &PL_xpvmg_arenaroot;
1498 goto new_body;
1499 case SVt_PVNV:
1500 new_body_length = sizeof(XPVNV);
1501 new_body_arena = (void **) &PL_xpvnv_root;
1502 new_body_arenaroot = (void **) &PL_xpvnv_arenaroot;
1503 goto new_body;
1504 case SVt_PVIV:
1505 new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1506 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
1507 new_body_length = sizeof(XPVIV) - new_body_offset;
1508 new_body_arena = (void **) &PL_xpviv_root;
1509 new_body_arenaroot = (void **) &PL_xpviv_arenaroot;
1510 /* XXX Is this still needed? Was it ever needed? Surely as there is
1511 no route from NV to PVIV, NOK can never be true */
1512 if (SvNIOK(sv))
1513 (void)SvIOK_on(sv);
1514 SvNOK_off(sv);
1515 goto new_body_no_NV;
1516 case SVt_PV:
1517 new_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1518 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
1519 new_body_length = sizeof(XPV) - new_body_offset;
1520 new_body_arena = (void **) &PL_xpv_root;
1521 new_body_arenaroot = (void **) &PL_xpv_arenaroot;
1522 new_body_no_NV:
1523 /* PV and PVIV don't have an NV slot. */
1524 zero_nv = FALSE;
1525
1526 {
1527 new_body:
1528 assert(new_body_length);
1529#ifndef PURIFY
dd690478 1530 /* This points to the start of the allocated area. */
403d36eb 1531 new_body = S_new_body(aTHX_ new_body_arenaroot, new_body_arena,
dd690478 1532 new_body_length);
403d36eb
NC
1533#else
1534 /* We always allocated the full length item with PURIFY */
1535 new_body_length += new_body_offset;
1536 new_body_offset = 0;
1537 new_body = my_safemalloc(new_body_length);
1538
1539#endif
1540 zero:
dd690478
NC
1541 Zero(new_body, new_body_length, char);
1542 new_body = ((char *)new_body) - new_body_offset;
403d36eb
NC
1543 SvANY(sv) = new_body;
1544
1545 if (old_body_length) {
1546 Copy((char *)old_body + old_body_offset,
1547 (char *)new_body + old_body_offset,
1548 old_body_length, char);
1549 }
1550
1551 /* FIXME - add a Configure test to determine if NV 0.0 is actually
1552 all bits zero. If it is, we can skip this initialisation. */
1553 if (zero_nv)
1554 SvNV_set(sv, 0);
1555
1556 if (mt == SVt_PVIO)
1557 IoPAGE_LEN(sv) = 60;
1558 if (old_type < SVt_RV)
1559 SvPV_set(sv, 0);
bd4b1eb5 1560 }
8990e307 1561 break;
403d36eb
NC
1562 default:
1563 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", mt);
8990e307 1564 }
878cc751
NC
1565
1566
1567 if (old_body_arena) {
1568#ifdef PURIFY
ee6954bb 1569 my_safefree(old_body);
878cc751 1570#else
dd690478
NC
1571 S_del_body(aTHX_ (void*)((char*)old_body + old_body_offset),
1572 old_body_arena);
878cc751 1573#endif
2068cd4d 1574 }
79072805
LW
1575}
1576
645c22ef
DM
1577/*
1578=for apidoc sv_backoff
1579
1580Remove any string offset. You should normally use the C<SvOOK_off> macro
1581wrapper instead.
1582
1583=cut
1584*/
1585
79072805 1586int
864dbfa3 1587Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1588{
1589 assert(SvOOK(sv));
b79f7545
NC
1590 assert(SvTYPE(sv) != SVt_PVHV);
1591 assert(SvTYPE(sv) != SVt_PVAV);
463ee0b2 1592 if (SvIVX(sv)) {
3f7c398e 1593 const char *s = SvPVX_const(sv);
b162af07 1594 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
f880fe2f 1595 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
79072805 1596 SvIV_set(sv, 0);
463ee0b2 1597 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1598 }
1599 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1600 return 0;
79072805
LW
1601}
1602
954c1994
GS
1603/*
1604=for apidoc sv_grow
1605
645c22ef
DM
1606Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1607upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1608Use the C<SvGROW> wrapper instead.
954c1994
GS
1609
1610=cut
1611*/
1612
79072805 1613char *
864dbfa3 1614Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1615{
1616 register char *s;
1617
55497cff 1618#ifdef HAS_64K_LIMIT
79072805 1619 if (newlen >= 0x10000) {
1d7c1841
GS
1620 PerlIO_printf(Perl_debug_log,
1621 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1622 my_exit(1);
1623 }
55497cff 1624#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1625 if (SvROK(sv))
1626 sv_unref(sv);
79072805
LW
1627 if (SvTYPE(sv) < SVt_PV) {
1628 sv_upgrade(sv, SVt_PV);
93524f2b 1629 s = SvPVX_mutable(sv);
79072805
LW
1630 }
1631 else if (SvOOK(sv)) { /* pv is offset? */
1632 sv_backoff(sv);
93524f2b 1633 s = SvPVX_mutable(sv);
79072805
LW
1634 if (newlen > SvLEN(sv))
1635 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1636#ifdef HAS_64K_LIMIT
1637 if (newlen >= 0x10000)
1638 newlen = 0xFFFF;
1639#endif
79072805 1640 }
bc44a8a2 1641 else
4d84ee25 1642 s = SvPVX_mutable(sv);
54f0641b 1643
79072805 1644 if (newlen > SvLEN(sv)) { /* need more room? */
7a9b70e9 1645 newlen = PERL_STRLEN_ROUNDUP(newlen);
8d6dde3e 1646 if (SvLEN(sv) && s) {
7bab3ede 1647#ifdef MYMALLOC
93524f2b 1648 const STRLEN l = malloced_size((void*)SvPVX_const(sv));
8d6dde3e
IZ
1649 if (newlen <= l) {
1650 SvLEN_set(sv, l);
1651 return s;
1652 } else
c70c8a0a 1653#endif
1936d2a7 1654 s = saferealloc(s, newlen);
8d6dde3e 1655 }
bfed75c6 1656 else {
1936d2a7 1657 s = safemalloc(newlen);
3f7c398e
SP
1658 if (SvPVX_const(sv) && SvCUR(sv)) {
1659 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 1660 }
4e83176d 1661 }
79072805 1662 SvPV_set(sv, s);
e1ec3a88 1663 SvLEN_set(sv, newlen);
79072805
LW
1664 }
1665 return s;
1666}
1667
954c1994
GS
1668/*
1669=for apidoc sv_setiv
1670
645c22ef
DM
1671Copies an integer into the given SV, upgrading first if necessary.
1672Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
1673
1674=cut
1675*/
1676
79072805 1677void
864dbfa3 1678Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1679{
765f542d 1680 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2
LW
1681 switch (SvTYPE(sv)) {
1682 case SVt_NULL:
79072805 1683 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1684 break;
1685 case SVt_NV:
1686 sv_upgrade(sv, SVt_PVNV);
1687 break;
ed6116ce 1688 case SVt_RV:
463ee0b2 1689 case SVt_PV:
79072805 1690 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1691 break;
a0d0e21e
LW
1692
1693 case SVt_PVGV:
a0d0e21e
LW
1694 case SVt_PVAV:
1695 case SVt_PVHV:
1696 case SVt_PVCV:
1697 case SVt_PVFM:
1698 case SVt_PVIO:
411caa50 1699 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 1700 OP_DESC(PL_op));
463ee0b2 1701 }
a0d0e21e 1702 (void)SvIOK_only(sv); /* validate number */
45977657 1703 SvIV_set(sv, i);
463ee0b2 1704 SvTAINT(sv);
79072805
LW
1705}
1706
954c1994
GS
1707/*
1708=for apidoc sv_setiv_mg
1709
1710Like C<sv_setiv>, but also handles 'set' magic.
1711
1712=cut
1713*/
1714
79072805 1715void
864dbfa3 1716Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1717{
1718 sv_setiv(sv,i);
1719 SvSETMAGIC(sv);
1720}
1721
954c1994
GS
1722/*
1723=for apidoc sv_setuv
1724
645c22ef
DM
1725Copies an unsigned integer into the given SV, upgrading first if necessary.
1726Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
1727
1728=cut
1729*/
1730
ef50df4b 1731void
864dbfa3 1732Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1733{
55ada374
NC
1734 /* With these two if statements:
1735 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1736
55ada374
NC
1737 without
1738 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1739
55ada374
NC
1740 If you wish to remove them, please benchmark to see what the effect is
1741 */
28e5dec8
JH
1742 if (u <= (UV)IV_MAX) {
1743 sv_setiv(sv, (IV)u);
1744 return;
1745 }
25da4f38
IZ
1746 sv_setiv(sv, 0);
1747 SvIsUV_on(sv);
607fa7f2 1748 SvUV_set(sv, u);
55497cff
PP
1749}
1750
954c1994
GS
1751/*
1752=for apidoc sv_setuv_mg
1753
1754Like C<sv_setuv>, but also handles 'set' magic.
1755
1756=cut
1757*/
1758
55497cff 1759void
864dbfa3 1760Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 1761{
55ada374
NC
1762 /* With these two if statements:
1763 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1764
55ada374
NC
1765 without
1766 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1767
55ada374
NC
1768 If you wish to remove them, please benchmark to see what the effect is
1769 */
28e5dec8
JH
1770 if (u <= (UV)IV_MAX) {
1771 sv_setiv(sv, (IV)u);
1772 } else {
1773 sv_setiv(sv, 0);
1774 SvIsUV_on(sv);
1775 sv_setuv(sv,u);
1776 }
ef50df4b
GS
1777 SvSETMAGIC(sv);
1778}
1779
954c1994
GS
1780/*
1781=for apidoc sv_setnv
1782
645c22ef
DM
1783Copies a double into the given SV, upgrading first if necessary.
1784Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1785
1786=cut
1787*/
1788
ef50df4b 1789void
65202027 1790Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1791{
765f542d 1792 SV_CHECK_THINKFIRST_COW_DROP(sv);
a0d0e21e
LW
1793 switch (SvTYPE(sv)) {
1794 case SVt_NULL:
1795 case SVt_IV:
79072805 1796 sv_upgrade(sv, SVt_NV);
a0d0e21e 1797 break;
a0d0e21e
LW
1798 case SVt_RV:
1799 case SVt_PV:
1800 case SVt_PVIV:
79072805 1801 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 1802 break;
827b7e14 1803
a0d0e21e 1804 case SVt_PVGV:
a0d0e21e
LW
1805 case SVt_PVAV:
1806 case SVt_PVHV:
1807 case SVt_PVCV:
1808 case SVt_PVFM:
1809 case SVt_PVIO:
411caa50 1810 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 1811 OP_NAME(PL_op));
79072805 1812 }
9d6ce603 1813 SvNV_set(sv, num);
a0d0e21e 1814 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1815 SvTAINT(sv);
79072805
LW
1816}
1817
954c1994
GS
1818/*
1819=for apidoc sv_setnv_mg
1820
1821Like C<sv_setnv>, but also handles 'set' magic.
1822
1823=cut
1824*/
1825
ef50df4b 1826void
65202027 1827Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
1828{
1829 sv_setnv(sv,num);
1830 SvSETMAGIC(sv);
1831}
1832
645c22ef
DM
1833/* Print an "isn't numeric" warning, using a cleaned-up,
1834 * printable version of the offending string
1835 */
1836
76e3520e 1837STATIC void
cea2e8a9 1838S_not_a_number(pTHX_ SV *sv)
a0d0e21e 1839{
94463019
JH
1840 SV *dsv;
1841 char tmpbuf[64];
1842 char *pv;
1843
1844 if (DO_UTF8(sv)) {
1845 dsv = sv_2mortal(newSVpv("", 0));
1846 pv = sv_uni_display(dsv, sv, 10, 0);
1847 } else {
1848 char *d = tmpbuf;
1849 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1850 /* each *s can expand to 4 chars + "...\0",
1851 i.e. need room for 8 chars */
ecdeb87c 1852
e62f0680
NC
1853 const char *s, *end;
1854 for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit;
1855 s++) {
94463019
JH
1856 int ch = *s & 0xFF;
1857 if (ch & 128 && !isPRINT_LC(ch)) {
1858 *d++ = 'M';
1859 *d++ = '-';
1860 ch &= 127;
1861 }
1862 if (ch == '\n') {
1863 *d++ = '\\';
1864 *d++ = 'n';
1865 }
1866 else if (ch == '\r') {
1867 *d++ = '\\';
1868 *d++ = 'r';
1869 }
1870 else if (ch == '\f') {
1871 *d++ = '\\';
1872 *d++ = 'f';
1873 }
1874 else if (ch == '\\') {
1875 *d++ = '\\';
1876 *d++ = '\\';
1877 }
1878 else if (ch == '\0') {
1879 *d++ = '\\';
1880 *d++ = '0';
1881 }
1882 else if (isPRINT_LC(ch))
1883 *d++ = ch;
1884 else {
1885 *d++ = '^';
1886 *d++ = toCTRL(ch);
1887 }
1888 }
1889 if (s < end) {
1890 *d++ = '.';
1891 *d++ = '.';
1892 *d++ = '.';
1893 }
1894 *d = '\0';
1895 pv = tmpbuf;
a0d0e21e 1896 }
a0d0e21e 1897
533c011a 1898 if (PL_op)
9014280d 1899 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
1900 "Argument \"%s\" isn't numeric in %s", pv,
1901 OP_DESC(PL_op));
a0d0e21e 1902 else
9014280d 1903 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 1904 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
1905}
1906
c2988b20
NC
1907/*
1908=for apidoc looks_like_number
1909
645c22ef
DM
1910Test if the content of an SV looks like a number (or is a number).
1911C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1912non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
1913
1914=cut
1915*/
1916
1917I32
1918Perl_looks_like_number(pTHX_ SV *sv)
1919{
a3b680e6 1920 register const char *sbegin;
c2988b20
NC
1921 STRLEN len;
1922
1923 if (SvPOK(sv)) {
3f7c398e 1924 sbegin = SvPVX_const(sv);
c2988b20
NC
1925 len = SvCUR(sv);
1926 }
1927 else if (SvPOKp(sv))
83003860 1928 sbegin = SvPV_const(sv, len);
c2988b20 1929 else
e0ab1c0e 1930 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
1931 return grok_number(sbegin, len, NULL);
1932}
25da4f38
IZ
1933
1934/* Actually, ISO C leaves conversion of UV to IV undefined, but
1935 until proven guilty, assume that things are not that bad... */
1936
645c22ef
DM
1937/*
1938 NV_PRESERVES_UV:
1939
1940 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1941 an IV (an assumption perl has been based on to date) it becomes necessary
1942 to remove the assumption that the NV always carries enough precision to
1943 recreate the IV whenever needed, and that the NV is the canonical form.
1944 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1945 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1946 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1947 1) to distinguish between IV/UV/NV slots that have cached a valid
1948 conversion where precision was lost and IV/UV/NV slots that have a
1949 valid conversion which has lost no precision
645c22ef 1950 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1951 would lose precision, the precise conversion (or differently
1952 imprecise conversion) is also performed and cached, to prevent
1953 requests for different numeric formats on the same SV causing
1954 lossy conversion chains. (lossless conversion chains are perfectly
1955 acceptable (still))
1956
1957
1958 flags are used:
1959 SvIOKp is true if the IV slot contains a valid value
1960 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1961 SvNOKp is true if the NV slot contains a valid value
1962 SvNOK is true only if the NV value is accurate
1963
1964 so
645c22ef 1965 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1966 IV(or UV) would lose accuracy over a direct conversion from PV to
1967 IV(or UV). If it would, cache both conversions, return NV, but mark
1968 SV as IOK NOKp (ie not NOK).
1969
645c22ef 1970 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1971 NV would lose accuracy over a direct conversion from PV to NV. If it
1972 would, cache both conversions, flag similarly.
1973
1974 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1975 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1976 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1977 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1978 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1979
645c22ef
DM
1980 The benefit of this is that operations such as pp_add know that if
1981 SvIOK is true for both left and right operands, then integer addition
1982 can be used instead of floating point (for cases where the result won't
1983 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1984 loss of precision compared with integer addition.
1985
1986 * making IV and NV equal status should make maths accurate on 64 bit
1987 platforms
1988 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1989 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1990 looking for SvIOK and checking for overflow will not outweigh the
1991 fp to integer speedup)
1992 * will slow down integer operations (callers of SvIV) on "inaccurate"
1993 values, as the change from SvIOK to SvIOKp will cause a call into
1994 sv_2iv each time rather than a macro access direct to the IV slot
1995 * should speed up number->string conversion on integers as IV is
645c22ef 1996 favoured when IV and NV are equally accurate
28e5dec8
JH
1997
1998 ####################################################################
645c22ef
DM
1999 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2000 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2001 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
2002 ####################################################################
2003
645c22ef 2004 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
2005 performance ratio.
2006*/
2007
2008#ifndef NV_PRESERVES_UV
645c22ef
DM
2009# define IS_NUMBER_UNDERFLOW_IV 1
2010# define IS_NUMBER_UNDERFLOW_UV 2
2011# define IS_NUMBER_IV_AND_UV 2
2012# define IS_NUMBER_OVERFLOW_IV 4
2013# define IS_NUMBER_OVERFLOW_UV 5
2014
2015/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2016
2017/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2018STATIC int
645c22ef 2019S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2020{
3f7c398e 2021 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
28e5dec8
JH
2022 if (SvNVX(sv) < (NV)IV_MIN) {
2023 (void)SvIOKp_on(sv);
2024 (void)SvNOK_on(sv);
45977657 2025 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2026 return IS_NUMBER_UNDERFLOW_IV;
2027 }
2028 if (SvNVX(sv) > (NV)UV_MAX) {
2029 (void)SvIOKp_on(sv);
2030 (void)SvNOK_on(sv);
2031 SvIsUV_on(sv);
607fa7f2 2032 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2033 return IS_NUMBER_OVERFLOW_UV;
2034 }
c2988b20
NC
2035 (void)SvIOKp_on(sv);
2036 (void)SvNOK_on(sv);
2037 /* Can't use strtol etc to convert this string. (See truth table in
2038 sv_2iv */
2039 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 2040 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2041 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2042 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2043 } else {
2044 /* Integer is imprecise. NOK, IOKp */
2045 }
2046 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2047 }
2048 SvIsUV_on(sv);
607fa7f2 2049 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2050 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2051 if (SvUVX(sv) == UV_MAX) {
2052 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2053 possibly be preserved by NV. Hence, it must be overflow.
2054 NOK, IOKp */
2055 return IS_NUMBER_OVERFLOW_UV;
2056 }
2057 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2058 } else {
2059 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2060 }
c2988b20 2061 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2062}
645c22ef
DM
2063#endif /* !NV_PRESERVES_UV*/
2064
891f9566
YST
2065/* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2066 * this function provided for binary compatibility only
2067 */
2068
2069IV
2070Perl_sv_2iv(pTHX_ register SV *sv)
2071{
2072 return sv_2iv_flags(sv, SV_GMAGIC);
2073}
2074
645c22ef 2075/*
891f9566 2076=for apidoc sv_2iv_flags
645c22ef 2077
891f9566
YST
2078Return the integer value of an SV, doing any necessary string
2079conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2080Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
645c22ef
DM
2081
2082=cut
2083*/
28e5dec8 2084
a0d0e21e 2085IV
891f9566 2086Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
79072805
LW
2087{
2088 if (!sv)
2089 return 0;
8990e307 2090 if (SvGMAGICAL(sv)) {
891f9566
YST
2091 if (flags & SV_GMAGIC)
2092 mg_get(sv);
463ee0b2
LW
2093 if (SvIOKp(sv))
2094 return SvIVX(sv);
748a9306 2095 if (SvNOKp(sv)) {
25da4f38 2096 return I_V(SvNVX(sv));
748a9306 2097 }
36477c24
PP
2098 if (SvPOKp(sv) && SvLEN(sv))
2099 return asIV(sv);
3fe9a6f1 2100 if (!SvROK(sv)) {
d008e5eb 2101 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2102 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2103 report_uninit(sv);
c6ee37c5 2104 }
36477c24 2105 return 0;
3fe9a6f1 2106 }
463ee0b2 2107 }
ed6116ce 2108 if (SvTHINKFIRST(sv)) {
a0d0e21e 2109 if (SvROK(sv)) {
a0d0e21e 2110 SV* tmpstr;
1554e226 2111 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2112 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2113 return SvIV(tmpstr);
56431972 2114 return PTR2IV(SvRV(sv));
a0d0e21e 2115 }
765f542d
NC
2116 if (SvIsCOW(sv)) {
2117 sv_force_normal_flags(sv, 0);
47deb5e7 2118 }
0336b60e 2119 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2120 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2121 report_uninit(sv);
ed6116ce
LW
2122 return 0;
2123 }
79072805 2124 }
25da4f38
IZ
2125 if (SvIOKp(sv)) {
2126 if (SvIsUV(sv)) {
2127 return (IV)(SvUVX(sv));
2128 }
2129 else {
2130 return SvIVX(sv);
2131 }
463ee0b2 2132 }
748a9306 2133 if (SvNOKp(sv)) {
28e5dec8
JH
2134 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2135 * without also getting a cached IV/UV from it at the same time
2136 * (ie PV->NV conversion should detect loss of accuracy and cache
2137 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2138
2139 if (SvTYPE(sv) == SVt_NV)
2140 sv_upgrade(sv, SVt_PVNV);
2141
28e5dec8
JH
2142 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2143 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2144 certainly cast into the IV range at IV_MAX, whereas the correct
2145 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2146 cases go to UV */
2147 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2148 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2149 if (SvNVX(sv) == (NV) SvIVX(sv)
2150#ifndef NV_PRESERVES_UV
2151 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2152 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2153 /* Don't flag it as "accurately an integer" if the number
2154 came from a (by definition imprecise) NV operation, and
2155 we're outside the range of NV integer precision */
2156#endif
2157 ) {
2158 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2159 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2160 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2161 PTR2UV(sv),
2162 SvNVX(sv),
2163 SvIVX(sv)));
2164
2165 } else {
2166 /* IV not precise. No need to convert from PV, as NV
2167 conversion would already have cached IV if it detected
2168 that PV->IV would be better than PV->NV->IV
2169 flags already correct - don't set public IOK. */
2170 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2171 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2172 PTR2UV(sv),
2173 SvNVX(sv),
2174 SvIVX(sv)));
2175 }
2176 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2177 but the cast (NV)IV_MIN rounds to a the value less (more
2178 negative) than IV_MIN which happens to be equal to SvNVX ??
2179 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2180 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2181 (NV)UVX == NVX are both true, but the values differ. :-(
2182 Hopefully for 2s complement IV_MIN is something like
2183 0x8000000000000000 which will be exact. NWC */
d460ef45 2184 }
25da4f38 2185 else {
607fa7f2 2186 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2187 if (
2188 (SvNVX(sv) == (NV) SvUVX(sv))
2189#ifndef NV_PRESERVES_UV
2190 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2191 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2192 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2193 /* Don't flag it as "accurately an integer" if the number
2194 came from a (by definition imprecise) NV operation, and
2195 we're outside the range of NV integer precision */
2196#endif
2197 )
2198 SvIOK_on(sv);
25da4f38
IZ
2199 SvIsUV_on(sv);
2200 ret_iv_max:
1c846c1f 2201 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2202 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2203 PTR2UV(sv),
57def98f
JH
2204 SvUVX(sv),
2205 SvUVX(sv)));
25da4f38
IZ
2206 return (IV)SvUVX(sv);
2207 }
748a9306
LW
2208 }
2209 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2210 UV value;
504618e9 2211 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2212 /* We want to avoid a possible problem when we cache an IV which
2213 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2214 the same as the direct translation of the initial string
2215 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2216 be careful to ensure that the value with the .456 is around if the
2217 NV value is requested in the future).
1c846c1f 2218
25da4f38
IZ
2219 This means that if we cache such an IV, we need to cache the
2220 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2221 cache the NV if we are sure it's not needed.
25da4f38 2222 */
16b7a9a4 2223
c2988b20
NC
2224 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2225 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2226 == IS_NUMBER_IN_UV) {
5e045b90 2227 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2228 if (SvTYPE(sv) < SVt_PVIV)
2229 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2230 (void)SvIOK_on(sv);
c2988b20
NC
2231 } else if (SvTYPE(sv) < SVt_PVNV)
2232 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2233
c2988b20
NC
2234 /* If NV preserves UV then we only use the UV value if we know that
2235 we aren't going to call atof() below. If NVs don't preserve UVs
2236 then the value returned may have more precision than atof() will
2237 return, even though value isn't perfectly accurate. */
2238 if ((numtype & (IS_NUMBER_IN_UV
2239#ifdef NV_PRESERVES_UV
2240 | IS_NUMBER_NOT_INT
2241#endif
2242 )) == IS_NUMBER_IN_UV) {
2243 /* This won't turn off the public IOK flag if it was set above */
2244 (void)SvIOKp_on(sv);
2245
2246 if (!(numtype & IS_NUMBER_NEG)) {
2247 /* positive */;
2248 if (value <= (UV)IV_MAX) {
45977657 2249 SvIV_set(sv, (IV)value);
c2988b20 2250 } else {
607fa7f2 2251 SvUV_set(sv, value);
c2988b20
NC
2252 SvIsUV_on(sv);
2253 }
2254 } else {
2255 /* 2s complement assumption */
2256 if (value <= (UV)IV_MIN) {
45977657 2257 SvIV_set(sv, -(IV)value);
c2988b20
NC
2258 } else {
2259 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2260 I'm assuming it will be rare. */
c2988b20
NC
2261 if (SvTYPE(sv) < SVt_PVNV)
2262 sv_upgrade(sv, SVt_PVNV);
2263 SvNOK_on(sv);
2264 SvIOK_off(sv);
2265 SvIOKp_on(sv);
9d6ce603 2266 SvNV_set(sv, -(NV)value);
45977657 2267 SvIV_set(sv, IV_MIN);
c2988b20
NC
2268 }
2269 }
2270 }
2271 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2272 will be in the previous block to set the IV slot, and the next
2273 block to set the NV slot. So no else here. */
2274
2275 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2276 != IS_NUMBER_IN_UV) {
2277 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 2278 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2279
c2988b20
NC
2280 if (! numtype && ckWARN(WARN_NUMERIC))
2281 not_a_number(sv);
28e5dec8 2282
65202027 2283#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2284 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2285 PTR2UV(sv), SvNVX(sv)));
65202027 2286#else
1779d84d 2287 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2288 PTR2UV(sv), SvNVX(sv)));
65202027 2289#endif
28e5dec8
JH
2290
2291
2292#ifdef NV_PRESERVES_UV
c2988b20
NC
2293 (void)SvIOKp_on(sv);
2294 (void)SvNOK_on(sv);
2295 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2296 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2297 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2298 SvIOK_on(sv);
28e5dec8 2299 } else {
c2988b20
NC
2300 /* Integer is imprecise. NOK, IOKp */
2301 }
2302 /* UV will not work better than IV */
2303 } else {
2304 if (SvNVX(sv) > (NV)UV_MAX) {
2305 SvIsUV_on(sv);
2306 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2307 SvUV_set(sv, UV_MAX);
c2988b20
NC
2308 SvIsUV_on(sv);
2309 } else {
607fa7f2 2310 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2311 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2312 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2313 SvIOK_on(sv);
28e5dec8
JH
2314 SvIsUV_on(sv);
2315 } else {
c2988b20
NC
2316 /* Integer is imprecise. NOK, IOKp, is UV */
2317 SvIsUV_on(sv);
28e5dec8 2318 }
28e5dec8 2319 }
c2988b20
NC
2320 goto ret_iv_max;
2321 }
28e5dec8 2322#else /* NV_PRESERVES_UV */
c2988b20
NC
2323 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2324 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2325 /* The IV slot will have been set from value returned by
2326 grok_number above. The NV slot has just been set using
2327 Atof. */
560b0c46 2328 SvNOK_on(sv);
c2988b20
NC
2329 assert (SvIOKp(sv));
2330 } else {
2331 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2332 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2333 /* Small enough to preserve all bits. */
2334 (void)SvIOKp_on(sv);
2335 SvNOK_on(sv);
45977657 2336 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2337 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2338 SvIOK_on(sv);
2339 /* Assumption: first non-preserved integer is < IV_MAX,
2340 this NV is in the preserved range, therefore: */
2341 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2342 < (UV)IV_MAX)) {
32fdb065 2343 Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
c2988b20
NC
2344 }
2345 } else {
2346 /* IN_UV NOT_INT
2347 0 0 already failed to read UV.
2348 0 1 already failed to read UV.
2349 1 0 you won't get here in this case. IV/UV
2350 slot set, public IOK, Atof() unneeded.
2351 1 1 already read UV.
2352 so there's no point in sv_2iuv_non_preserve() attempting
2353 to use atol, strtol, strtoul etc. */
2354 if (sv_2iuv_non_preserve (sv, numtype)
2355 >= IS_NUMBER_OVERFLOW_IV)
2356 goto ret_iv_max;
2357 }
2358 }
28e5dec8 2359#endif /* NV_PRESERVES_UV */
25da4f38 2360 }
28e5dec8 2361 } else {
599cee73 2362 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2363 report_uninit(sv);
25da4f38
IZ
2364 if (SvTYPE(sv) < SVt_IV)
2365 /* Typically the caller expects that sv_any is not NULL now. */
2366 sv_upgrade(sv, SVt_IV);
a0d0e21e 2367 return 0;
79072805 2368 }
1d7c1841
GS
2369 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2370 PTR2UV(sv),SvIVX(sv)));
25da4f38 2371 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2372}
2373
891f9566
YST
2374/* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2375 * this function provided for binary compatibility only
2376 */
2377
2378UV
2379Perl_sv_2uv(pTHX_ register SV *sv)
2380{
2381 return sv_2uv_flags(sv, SV_GMAGIC);
2382}
2383
645c22ef 2384/*
891f9566 2385=for apidoc sv_2uv_flags
645c22ef
DM
2386
2387Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2388conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2389Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2390
2391=cut
2392*/
2393
ff68c719 2394UV
891f9566 2395Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719
PP
2396{
2397 if (!sv)
2398 return 0;
2399 if (SvGMAGICAL(sv)) {
891f9566
YST
2400 if (flags & SV_GMAGIC)
2401 mg_get(sv);
ff68c719
PP
2402 if (SvIOKp(sv))
2403 return SvUVX(sv);
2404 if (SvNOKp(sv))
2405 return U_V(SvNVX(sv));
36477c24
PP
2406 if (SvPOKp(sv) && SvLEN(sv))
2407 return asUV(sv);
3fe9a6f1 2408 if (!SvROK(sv)) {
d008e5eb 2409 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2410 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2411 report_uninit(sv);
c6ee37c5 2412 }
36477c24 2413 return 0;
3fe9a6f1 2414 }
ff68c719
PP
2415 }
2416 if (SvTHINKFIRST(sv)) {
2417 if (SvROK(sv)) {
ff68c719 2418 SV* tmpstr;
1554e226 2419 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2420 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2421 return SvUV(tmpstr);
56431972 2422 return PTR2UV(SvRV(sv));
ff68c719 2423 }
765f542d
NC
2424 if (SvIsCOW(sv)) {
2425 sv_force_normal_flags(sv, 0);
8a818333 2426 }
0336b60e 2427 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2428 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2429 report_uninit(sv);
ff68c719
PP
2430 return 0;
2431 }
2432 }
25da4f38
IZ
2433 if (SvIOKp(sv)) {
2434 if (SvIsUV(sv)) {
2435 return SvUVX(sv);
2436 }
2437 else {
2438 return (UV)SvIVX(sv);
2439 }
ff68c719
PP
2440 }
2441 if (SvNOKp(sv)) {
28e5dec8
JH
2442 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2443 * without also getting a cached IV/UV from it at the same time
2444 * (ie PV->NV conversion should detect loss of accuracy and cache
2445 * IV or UV at same time to avoid this. */
2446 /* IV-over-UV optimisation - choose to cache IV if possible */
2447
25da4f38
IZ
2448 if (SvTYPE(sv) == SVt_NV)
2449 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2450
2451 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2452 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2453 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2454 if (SvNVX(sv) == (NV) SvIVX(sv)
2455#ifndef NV_PRESERVES_UV
2456 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2457 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2458 /* Don't flag it as "accurately an integer" if the number
2459 came from a (by definition imprecise) NV operation, and
2460 we're outside the range of NV integer precision */
2461#endif
2462 ) {
2463 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2464 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2465 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2466 PTR2UV(sv),
2467 SvNVX(sv),
2468 SvIVX(sv)));
2469
2470 } else {
2471 /* IV not precise. No need to convert from PV, as NV
2472 conversion would already have cached IV if it detected
2473 that PV->IV would be better than PV->NV->IV
2474 flags already correct - don't set public IOK. */
2475 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2476 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2477 PTR2UV(sv),
2478 SvNVX(sv),
2479 SvIVX(sv)));
2480 }
2481 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2482 but the cast (NV)IV_MIN rounds to a the value less (more
2483 negative) than IV_MIN which happens to be equal to SvNVX ??
2484 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2485 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2486 (NV)UVX == NVX are both true, but the values differ. :-(
2487 Hopefully for 2s complement IV_MIN is something like
2488 0x8000000000000000 which will be exact. NWC */
d460ef45 2489 }
28e5dec8 2490 else {
607fa7f2 2491 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2492 if (
2493 (SvNVX(sv) == (NV) SvUVX(sv))
2494#ifndef NV_PRESERVES_UV
2495 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2496 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2497 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2498 /* Don't flag it as "accurately an integer" if the number
2499 came from a (by definition imprecise) NV operation, and
2500 we're outside the range of NV integer precision */
2501#endif
2502 )
2503 SvIOK_on(sv);
2504 SvIsUV_on(sv);
1c846c1f 2505 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2506 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2507 PTR2UV(sv),
28e5dec8
JH
2508 SvUVX(sv),
2509 SvUVX(sv)));
25da4f38 2510 }
ff68c719
PP
2511 }
2512 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2513 UV value;
504618e9 2514 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2515
2516 /* We want to avoid a possible problem when we cache a UV which
2517 may be later translated to an NV, and the resulting NV is not
2518 the translation of the initial data.
1c846c1f 2519
25da4f38
IZ
2520 This means that if we cache such a UV, we need to cache the
2521 NV as well. Moreover, we trade speed for space, and do not
2522 cache the NV if not needed.
2523 */
16b7a9a4 2524
c2988b20
NC
2525 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2526 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2527 == IS_NUMBER_IN_UV) {
5e045b90 2528 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 2529 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2530 sv_upgrade(sv, SVt_PVIV);
2531 (void)SvIOK_on(sv);
c2988b20
NC
2532 } else if (SvTYPE(sv) < SVt_PVNV)
2533 sv_upgrade(sv, SVt_PVNV);
d460ef45 2534
c2988b20
NC
2535 /* If NV preserves UV then we only use the UV value if we know that
2536 we aren't going to call atof() below. If NVs don't preserve UVs
2537 then the value returned may have more precision than atof() will
2538 return, even though it isn't accurate. */
2539 if ((numtype & (IS_NUMBER_IN_UV
2540#ifdef NV_PRESERVES_UV
2541 | IS_NUMBER_NOT_INT
2542#endif
2543 )) == IS_NUMBER_IN_UV) {
2544 /* This won't turn off the public IOK flag if it was set above */
2545 (void)SvIOKp_on(sv);
2546
2547 if (!(numtype & IS_NUMBER_NEG)) {
2548 /* positive */;
2549 if (value <= (UV)IV_MAX) {
45977657 2550 SvIV_set(sv, (IV)value);
28e5dec8
JH
2551 } else {
2552 /* it didn't overflow, and it was positive. */
607fa7f2 2553 SvUV_set(sv, value);
28e5dec8
JH
2554 SvIsUV_on(sv);
2555 }
c2988b20
NC
2556 } else {
2557 /* 2s complement assumption */
2558 if (value <= (UV)IV_MIN) {
45977657 2559 SvIV_set(sv, -(IV)value);
c2988b20
NC
2560 } else {
2561 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2562 I'm assuming it will be rare. */
c2988b20
NC
2563 if (SvTYPE(sv) < SVt_PVNV)
2564 sv_upgrade(sv, SVt_PVNV);
2565 SvNOK_on(sv);
2566 SvIOK_off(sv);
2567 SvIOKp_on(sv);
9d6ce603 2568 SvNV_set(sv, -(NV)value);
45977657 2569 SvIV_set(sv, IV_MIN);
c2988b20
NC
2570 }
2571 }
2572 }
2573
2574 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2575 != IS_NUMBER_IN_UV) {
2576 /* It wasn't an integer, or it overflowed the UV. */
3f7c398e 2577 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2578
c2988b20 2579 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
2580 not_a_number(sv);
2581
2582#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2583 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2584 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2585#else
1779d84d 2586 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 2587 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
2588#endif
2589
2590#ifdef NV_PRESERVES_UV
c2988b20
NC
2591 (void)SvIOKp_on(sv);
2592 (void)SvNOK_on(sv);
2593 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2594 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2595 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2596 SvIOK_on(sv);
2597 } else {
2598 /* Integer is imprecise. NOK, IOKp */
2599 }
2600 /* UV will not work better than IV */
2601 } else {
2602 if (SvNVX(sv) > (NV)UV_MAX) {
2603 SvIsUV_on(sv);
2604 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2605 SvUV_set(sv, UV_MAX);
c2988b20
NC
2606 SvIsUV_on(sv);
2607 } else {
607fa7f2 2608 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2609 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2610 NV preservse UV so can do correct comparison. */
2611 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2612 SvIOK_on(sv);
2613 SvIsUV_on(sv);
2614 } else {
2615 /* Integer is imprecise. NOK, IOKp, is UV */
2616 SvIsUV_on(sv);
2617 }
2618 }
2619 }
28e5dec8 2620#else /* NV_PRESERVES_UV */
c2988b20
NC
2621 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2622 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2623 /* The UV slot will have been set from value returned by
2624 grok_number above. The NV slot has just been set using
2625 Atof. */
560b0c46 2626 SvNOK_on(sv);
c2988b20
NC
2627 assert (SvIOKp(sv));
2628 } else {
2629 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2630 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2631 /* Small enough to preserve all bits. */
2632 (void)SvIOKp_on(sv);
2633 SvNOK_on(sv);
45977657 2634 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2635 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2636 SvIOK_on(sv);
2637 /* Assumption: first non-preserved integer is < IV_MAX,
2638 this NV is in the preserved range, therefore: */
2639 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2640 < (UV)IV_MAX)) {
32fdb065 2641 Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
c2988b20
NC
2642 }
2643 } else
2644 sv_2iuv_non_preserve (sv, numtype);
2645 }
28e5dec8 2646#endif /* NV_PRESERVES_UV */
f7bbb42a 2647 }
ff68c719
PP
2648 }
2649 else {
d008e5eb 2650 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2651 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2652 report_uninit(sv);
c6ee37c5 2653 }
25da4f38
IZ
2654 if (SvTYPE(sv) < SVt_IV)
2655 /* Typically the caller expects that sv_any is not NULL now. */
2656 sv_upgrade(sv, SVt_IV);
ff68c719
PP
2657 return 0;
2658 }
25da4f38 2659
1d7c1841
GS
2660 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2661 PTR2UV(sv),SvUVX(sv)));
25da4f38 2662 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
2663}
2664
645c22ef
DM
2665/*
2666=for apidoc sv_2nv
2667
2668Return the num value of an SV, doing any necessary string or integer
2669conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2670macros.
2671
2672=cut
2673*/
2674
65202027 2675NV
864dbfa3 2676Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
2677{
2678 if (!sv)
2679 return 0.0;
8990e307 2680 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2681 mg_get(sv);
2682 if (SvNOKp(sv))
2683 return SvNVX(sv);
a0d0e21e 2684 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2685 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
504618e9 2686 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 2687 not_a_number(sv);
3f7c398e 2688 return Atof(SvPVX_const(sv));
a0d0e21e 2689 }
25da4f38 2690 if (SvIOKp(sv)) {
1c846c1f 2691 if (SvIsUV(sv))
65202027 2692 return (NV)SvUVX(sv);
25da4f38 2693 else
65202027 2694 return (NV)SvIVX(sv);
25da4f38 2695 }
16d20bd9 2696 if (!SvROK(sv)) {
d008e5eb 2697 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2698 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2699 report_uninit(sv);
c6ee37c5 2700 }
66a1b24b 2701 return (NV)0;
16d20bd9 2702 }
463ee0b2 2703 }
ed6116ce 2704 if (SvTHINKFIRST(sv)) {
a0d0e21e 2705 if (SvROK(sv)) {
a0d0e21e 2706 SV* tmpstr;
1554e226 2707 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2708 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2709 return SvNV(tmpstr);
56431972 2710 return PTR2NV(SvRV(sv));
a0d0e21e 2711 }
765f542d
NC
2712 if (SvIsCOW(sv)) {
2713 sv_force_normal_flags(sv, 0);
8a818333 2714 }
0336b60e 2715 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2716 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2717 report_uninit(sv);
ed6116ce
LW
2718 return 0.0;
2719 }
79072805
LW
2720 }
2721 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
2722 if (SvTYPE(sv) == SVt_IV)
2723 sv_upgrade(sv, SVt_PVNV);
2724 else
2725 sv_upgrade(sv, SVt_NV);
906f284f 2726#ifdef USE_LONG_DOUBLE
097ee67d 2727 DEBUG_c({
f93f4e46 2728 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2729 PerlIO_printf(Perl_debug_log,
2730 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2731 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2732 RESTORE_NUMERIC_LOCAL();
2733 });
65202027 2734#else
572bbb43 2735 DEBUG_c({
f93f4e46 2736 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2737 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2738 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2739 RESTORE_NUMERIC_LOCAL();
2740 });
572bbb43 2741#endif
79072805
LW
2742 }
2743 else if (SvTYPE(sv) < SVt_PVNV)
2744 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2745 if (SvNOKp(sv)) {
2746 return SvNVX(sv);
61604483 2747 }
59d8ce62 2748 if (SvIOKp(sv)) {
9d6ce603 2749 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
2750#ifdef NV_PRESERVES_UV
2751 SvNOK_on(sv);
2752#else
2753 /* Only set the public NV OK flag if this NV preserves the IV */
2754 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2755 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2756 : (SvIVX(sv) == I_V(SvNVX(sv))))
2757 SvNOK_on(sv);
2758 else
2759 SvNOKp_on(sv);
2760#endif
93a17b20 2761 }
748a9306 2762 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2763 UV value;
3f7c398e 2764 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20 2765 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 2766 not_a_number(sv);
28e5dec8 2767#ifdef NV_PRESERVES_UV
c2988b20
NC
2768 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2769 == IS_NUMBER_IN_UV) {
5e045b90 2770 /* It's definitely an integer */
9d6ce603 2771 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 2772 } else
3f7c398e 2773 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2774 SvNOK_on(sv);
2775#else
3f7c398e 2776 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2777 /* Only set the public NV OK flag if this NV preserves the value in
2778 the PV at least as well as an IV/UV would.
2779 Not sure how to do this 100% reliably. */
2780 /* if that shift count is out of range then Configure's test is
2781 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2782 UV_BITS */
2783 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2784 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2785 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2786 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2787 /* Can't use strtol etc to convert this string, so don't try.
2788 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2789 SvNOK_on(sv);
2790 } else {
2791 /* value has been set. It may not be precise. */
2792 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2793 /* 2s complement assumption for (UV)IV_MIN */
2794 SvNOK_on(sv); /* Integer is too negative. */
2795 } else {
2796 SvNOKp_on(sv);
2797 SvIOKp_on(sv);
6fa402ec 2798
c2988b20 2799 if (numtype & IS_NUMBER_NEG) {
45977657 2800 SvIV_set(sv, -(IV)value);
c2988b20 2801 } else if (value <= (UV)IV_MAX) {
45977657 2802 SvIV_set(sv, (IV)value);
c2988b20 2803 } else {
607fa7f2 2804 SvUV_set(sv, value);
c2988b20
NC
2805 SvIsUV_on(sv);
2806 }
2807
2808 if (numtype & IS_NUMBER_NOT_INT) {
2809 /* I believe that even if the original PV had decimals,
2810 they are lost beyond the limit of the FP precision.
2811 However, neither is canonical, so both only get p
2812 flags. NWC, 2000/11/25 */
2813 /* Both already have p flags, so do nothing */
2814 } else {
66a1b24b 2815 const NV nv = SvNVX(sv);
c2988b20
NC
2816 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2817 if (SvIVX(sv) == I_V(nv)) {
2818 SvNOK_on(sv);
2819 SvIOK_on(sv);
2820 } else {
2821 SvIOK_on(sv);
2822 /* It had no "." so it must be integer. */
2823 }
2824 } else {
2825 /* between IV_MAX and NV(UV_MAX).
2826 Could be slightly > UV_MAX */
6fa402ec 2827
c2988b20
NC
2828 if (numtype & IS_NUMBER_NOT_INT) {
2829 /* UV and NV both imprecise. */
2830 } else {
66a1b24b 2831 const UV nv_as_uv = U_V(nv);
c2988b20
NC
2832
2833 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2834 SvNOK_on(sv);
2835 SvIOK_on(sv);
2836 } else {
2837 SvIOK_on(sv);
2838 }
2839 }
2840 }
2841 }
2842 }
2843 }
28e5dec8 2844#endif /* NV_PRESERVES_UV */
93a17b20 2845 }
79072805 2846 else {
599cee73 2847 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2848 report_uninit(sv);
25da4f38
IZ
2849 if (SvTYPE(sv) < SVt_NV)
2850 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
2851 /* XXX Ilya implies that this is a bug in callers that assume this
2852 and ideally should be fixed. */
25da4f38 2853 sv_upgrade(sv, SVt_NV);
a0d0e21e 2854 return 0.0;
79072805 2855 }
572bbb43 2856#if defined(USE_LONG_DOUBLE)
097ee67d 2857 DEBUG_c({
f93f4e46 2858 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2859 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2860 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2861 RESTORE_NUMERIC_LOCAL();
2862 });
65202027 2863#else
572bbb43 2864 DEBUG_c({
f93f4e46 2865 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2866 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 2867 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2868 RESTORE_NUMERIC_LOCAL();
2869 });
572bbb43 2870#endif
463ee0b2 2871 return SvNVX(sv);
79072805
LW
2872}
2873
645c22ef
DM
2874/* asIV(): extract an integer from the string value of an SV.
2875 * Caller must validate PVX */
2876
76e3520e 2877STATIC IV
cea2e8a9 2878S_asIV(pTHX_ SV *sv)
36477c24 2879{
c2988b20 2880 UV value;
66a1b24b 2881 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20
NC
2882
2883 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2884 == IS_NUMBER_IN_UV) {
645c22ef 2885 /* It's definitely an integer */
c2988b20
NC
2886 if (numtype & IS_NUMBER_NEG) {
2887 if (value < (UV)IV_MIN)
2888 return -(IV)value;
2889 } else {
2890 if (value < (UV)IV_MAX)
2891 return (IV)value;
2892 }
2893 }
d008e5eb 2894 if (!numtype) {
d008e5eb
GS
2895 if (ckWARN(WARN_NUMERIC))
2896 not_a_number(sv);
2897 }
3f7c398e 2898 return I_V(Atof(SvPVX_const(sv)));
36477c24
PP
2899}
2900
645c22ef
DM
2901/* asUV(): extract an unsigned integer from the string value of an SV
2902 * Caller must validate PVX */
2903
76e3520e 2904STATIC UV
cea2e8a9 2905S_asUV(pTHX_ SV *sv)
36477c24 2906{
c2988b20 2907 UV value;
504618e9 2908 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
36477c24 2909
c2988b20
NC
2910 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2911 == IS_NUMBER_IN_UV) {
645c22ef 2912 /* It's definitely an integer */
6fa402ec 2913 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
2914 return value;
2915 }
d008e5eb 2916 if (!numtype) {
d008e5eb
GS
2917 if (ckWARN(WARN_NUMERIC))
2918 not_a_number(sv);
2919 }
3f7c398e 2920 return U_V(Atof(SvPVX_const(sv)));
36477c24
PP
2921}
2922
645c22ef
DM
2923/*
2924=for apidoc sv_2pv_nolen
2925
2926Like C<sv_2pv()>, but doesn't return the length too. You should usually
2927use the macro wrapper C<SvPV_nolen(sv)> instead.
2928=cut
2929*/
2930
79072805 2931char *
864dbfa3 2932Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d 2933{
dafda6d1 2934 return sv_2pv(sv, 0);
1fa8b10d
JD
2935}
2936
645c22ef
DM
2937/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2938 * UV as a string towards the end of buf, and return pointers to start and
2939 * end of it.
2940 *
2941 * We assume that buf is at least TYPE_CHARS(UV) long.
2942 */
2943
864dbfa3 2944static char *
25da4f38
IZ
2945uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2946{
25da4f38
IZ
2947 char *ptr = buf + TYPE_CHARS(UV);
2948 char *ebuf = ptr;
2949 int sign;
25da4f38
IZ
2950
2951 if (is_uv)
2952 sign = 0;
2953 else if (iv >= 0) {
2954 uv = iv;
2955 sign = 0;
2956 } else {
2957 uv = -iv;
2958 sign = 1;
2959 }
2960 do {
eb160463 2961 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2962 } while (uv /= 10);
2963 if (sign)
2964 *--ptr = '-';
2965 *peob = ebuf;
2966 return ptr;
2967}
2968
09540bc3
JH
2969/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2970 * this function provided for binary compatibility only
2971 */
2972
2973char *
2974Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2975{
2976 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2977}
2978
645c22ef
DM
2979/*
2980=for apidoc sv_2pv_flags
2981
ff276b08 2982Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
2983If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2984if necessary.
2985Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2986usually end up here too.
2987
2988=cut
2989*/
2990
8d6d96c1
HS
2991char *
2992Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2993{
79072805
LW
2994 register char *s;
2995 int olderrno;
cb50f42d 2996 SV *tsv, *origsv;
25da4f38
IZ
2997 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2998 char *tmpbuf = tbuf;
79072805 2999
463ee0b2 3000 if (!sv) {
cdb061a3
NC
3001 if (lp)
3002 *lp = 0;
73d840c0 3003 return (char *)"";
463ee0b2 3004 }
8990e307 3005 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
3006 if (flags & SV_GMAGIC)
3007 mg_get(sv);
463ee0b2 3008 if (SvPOKp(sv)) {
cdb061a3
NC
3009 if (lp)
3010 *lp = SvCUR(sv);
10516c54
NC
3011 if (flags & SV_MUTABLE_RETURN)
3012 return SvPVX_mutable(sv);
4d84ee25
NC
3013 if (flags & SV_CONST_RETURN)
3014 return (char *)SvPVX_const(sv);
463ee0b2
LW
3015 return SvPVX(sv);
3016 }
cf2093f6 3017 if (SvIOKp(sv)) {
1c846c1f 3018 if (SvIsUV(sv))
57def98f 3019 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 3020 else
57def98f 3021 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 3022 tsv = Nullsv;
a0d0e21e 3023 goto tokensave;
463ee0b2
LW
3024 }
3025 if (SvNOKp(sv)) {
2d4389e4 3026 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 3027 tsv = Nullsv;
a0d0e21e 3028 goto tokensave;
463ee0b2 3029 }
16d20bd9 3030 if (!SvROK(sv)) {
d008e5eb 3031 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3032 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3033 report_uninit(sv);
c6ee37c5 3034 }
cdb061a3
NC
3035 if (lp)
3036 *lp = 0;
73d840c0 3037 return (char *)"";
16d20bd9 3038 }
463ee0b2 3039 }
ed6116ce
LW
3040 if (SvTHINKFIRST(sv)) {
3041 if (SvROK(sv)) {
a0d0e21e 3042 SV* tmpstr;
e1ec3a88 3043 register const char *typestr;
1554e226 3044 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3045 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
50adf7d2
NC
3046 /* Unwrap this: */
3047 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */
3048
3049 char *pv;
3050 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
3051 if (flags & SV_CONST_RETURN) {
3052 pv = (char *) SvPVX_const(tmpstr);
3053 } else {
3054 pv = (flags & SV_MUTABLE_RETURN)
3055 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
3056 }
3057 if (lp)
3058 *lp = SvCUR(tmpstr);
3059 } else {
3060 pv = sv_2pv_flags(tmpstr, lp, flags);
3061 }
446eaa42
YST
3062 if (SvUTF8(tmpstr))
3063 SvUTF8_on(sv);
3064 else
3065 SvUTF8_off(sv);
3066 return pv;
3067 }
cb50f42d 3068 origsv = sv;
ed6116ce
LW
3069 sv = (SV*)SvRV(sv);
3070 if (!sv)
e1ec3a88 3071 typestr = "NULLREF";
ed6116ce 3072 else {
f9277f47
IZ
3073 MAGIC *mg;
3074
ed6116ce 3075 switch (SvTYPE(sv)) {
f9277f47
IZ
3076 case SVt_PVMG:
3077 if ( ((SvFLAGS(sv) &
1c846c1f 3078 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
faf82a0b 3079 == (SVs_OBJECT|SVs_SMG))
14befaf4 3080 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
e1ec3a88 3081 const regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3082
2cd61cdb 3083 if (!mg->mg_ptr) {
e1ec3a88 3084 const char *fptr = "msix";
8782bef2
GB
3085 char reflags[6];
3086 char ch;
3087 int left = 0;
3088 int right = 4;
ff385a1b 3089 char need_newline = 0;
eb160463 3090 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3091
155aba94 3092 while((ch = *fptr++)) {
8782bef2
GB
3093 if(reganch & 1) {
3094 reflags[left++] = ch;
3095 }
3096 else {
3097 reflags[right--] = ch;
3098 }
3099 reganch >>= 1;
3100 }
3101 if(left != 4) {
3102 reflags[left] = '-';
3103 left = 5;
3104 }
3105
3106 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3107 /*
3108 * If /x was used, we have to worry about a regex
3109 * ending with a comment later being embedded
3110 * within another regex. If so, we don't want this
3111 * regex's "commentization" to leak out to the
3112 * right part of the enclosing regex, we must cap
3113 * it with a newline.
3114 *
3115 * So, if /x was used, we scan backwards from the
3116 * end of the regex. If we find a '#' before we
3117 * find a newline, we need to add a newline
3118 * ourself. If we find a '\n' first (or if we
3119 * don't find '#' or '\n'), we don't need to add
3120 * anything. -jfriedl
3121 */
3122 if (PMf_EXTENDED & re->reganch)
3123 {
e1ec3a88 3124 const char *endptr = re->precomp + re->prelen;
ff385a1b
JF
3125 while (endptr >= re->precomp)
3126 {
e1ec3a88 3127 const char c = *(endptr--);
ff385a1b
JF
3128 if (c == '\n')
3129 break; /* don't need another */
3130 if (c == '#') {
3131 /* we end while in a comment, so we
3132 need a newline */
3133 mg->mg_len++; /* save space for it */
3134 need_newline = 1; /* note to add it */
ab01544f 3135 break;
ff385a1b
JF
3136 }
3137 }
3138 }
3139
8782bef2
GB
3140 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3141 Copy("(?", mg->mg_ptr, 2, char);
3142 Copy(reflags, mg->mg_ptr+2, left, char);
3143 Copy(":", mg->mg_ptr+left+2, 1, char);
3144 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3145 if (need_newline)
3146 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3147 mg->mg_ptr[mg->mg_len - 1] = ')';
3148 mg->mg_ptr[mg->mg_len] = 0;
3149 }
3280af22 3150 PL_reginterp_cnt += re->program[0].next_off;
cb50f42d
YST
3151
3152 if (re->reganch & ROPT_UTF8)
3153 SvUTF8_on(origsv);
3154 else
3155 SvUTF8_off(origsv);
cdb061a3
NC
3156 if (lp)
3157 *lp = mg->mg_len;
1bd3ad17 3158 return mg->mg_ptr;
f9277f47
IZ
3159 }
3160 /* Fall through */
ed6116ce
LW
3161 case SVt_NULL:
3162 case SVt_IV:
3163 case SVt_NV:
3164 case SVt_RV:
3165 case SVt_PV:
3166 case SVt_PVIV:
3167 case SVt_PVNV:
e1ec3a88
AL
3168 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3169 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
be65207d
DM
3170 /* tied lvalues should appear to be
3171 * scalars for backwards compatitbility */
3172 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3173 ? "SCALAR" : "LVALUE"; break;
e1ec3a88
AL
3174 case SVt_PVAV: typestr = "ARRAY"; break;
3175 case SVt_PVHV: typestr = "HASH"; break;
3176 case SVt_PVCV: typestr = "CODE"; break;
3177 case SVt_PVGV: typestr = "GLOB"; break;
3178 case SVt_PVFM: typestr = "FORMAT"; break;
3179 case SVt_PVIO: typestr = "IO"; break;
3180 default: typestr = "UNKNOWN"; break;
ed6116ce 3181 }
46fc3d4c 3182 tsv = NEWSV(0,0);
a5cb6b62 3183 if (SvOBJECT(sv)) {
bfcb3514 3184 const char *name = HvNAME_get(SvSTASH(sv));
a5cb6b62 3185 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
e1ec3a88 3186 name ? name : "__ANON__" , typestr, PTR2UV(sv));
a5cb6b62 3187 }
ed6116ce 3188 else
e1ec3a88 3189 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
a0d0e21e 3190 goto tokensaveref;
463ee0b2 3191 }
cdb061a3
NC
3192 if (lp)
3193 *lp = strlen(typestr);
73d840c0 3194 return (char *)typestr;
79072805 3195 }
0336b60e 3196 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3197 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3198 report_uninit(sv);
cdb061a3
NC
3199 if (lp)
3200 *lp = 0;
73d840c0 3201 return (char *)"";
79072805 3202 }
79072805 3203 }
28e5dec8
JH
3204 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3205 /* I'm assuming that if both IV and NV are equally valid then
3206 converting the IV is going to be more efficient */
e1ec3a88
AL
3207 const U32 isIOK = SvIOK(sv);
3208 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3209 char buf[TYPE_CHARS(UV)];
3210 char *ebuf, *ptr;
3211
3212 if (SvTYPE(sv) < SVt_PVIV)
3213 sv_upgrade(sv, SVt_PVIV);
3214 if (isUIOK)
3215 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3216 else
3217 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
5902b6a9
NC
3218 /* inlined from sv_setpvn */
3219 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
4d84ee25 3220 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
28e5dec8
JH
3221 SvCUR_set(sv, ebuf - ptr);
3222 s = SvEND(sv);
3223 *s = '\0';
3224 if (isIOK)
3225 SvIOK_on(sv);
3226 else
3227 SvIOKp_on(sv);
3228 if (isUIOK)
3229 SvIsUV_on(sv);
3230 }
3231 else if (SvNOKp(sv)) {
79072805
LW
3232 if (SvTYPE(sv) < SVt_PVNV)
3233 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3234 /* The +20 is pure guesswork. Configure test needed. --jhi */
5902b6a9 3235 s = SvGROW_mutable(sv, NV_DIG + 20);
79072805 3236 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3237#ifdef apollo
463ee0b2 3238 if (SvNVX(sv) == 0.0)
79072805
LW
3239 (void)strcpy(s,"0");
3240 else
3241#endif /*apollo*/
bbce6d69 3242 {
2d4389e4 3243 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3244 }
79072805 3245 errno = olderrno;
a0d0e21e
LW
3246#ifdef FIXNEGATIVEZERO
3247 if (*s == '-' && s[1] == '0' && !s[2])
3248 strcpy(s,"0");
3249#endif
79072805
LW
3250 while (*s) s++;
3251#ifdef hcx
3252 if (s[-1] == '.')
46fc3d4c 3253 *--s = '\0';
79072805
LW
3254#endif
3255 }
79072805 3256 else {
0336b60e
IZ
3257 if (ckWARN(WARN_UNINITIALIZED)
3258 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3259 report_uninit(sv);
cdb061a3 3260 if (lp)
a0d0e21e 3261 *lp = 0;
25da4f38
IZ
3262 if (SvTYPE(sv) < SVt_PV)
3263 /* Typically the caller expects that sv_any is not NULL now. */
3264 sv_upgrade(sv, SVt_PV);
73d840c0 3265 return (char *)"";
79072805 3266 }
cdb061a3
NC
3267 {
3268 STRLEN len = s - SvPVX_const(sv);
3269 if (lp)
3270 *lp = len;
3271 SvCUR_set(sv, len);
3272 }
79072805 3273 SvPOK_on(sv);
1d7c1841 3274 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 3275 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
3276 if (flags & SV_CONST_RETURN)
3277 return (char *)SvPVX_const(sv);
10516c54
NC
3278 if (flags & SV_MUTABLE_RETURN)
3279 return SvPVX_mutable(sv);
463ee0b2 3280 return SvPVX(sv);
a0d0e21e
LW
3281
3282 tokensave:
3283 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3284 /* Sneaky stuff here */
3285
3286 tokensaveref:
46fc3d4c 3287 if (!tsv)
96827780 3288 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3289 sv_2mortal(tsv);
cdb061a3
NC
3290 if (lp)
3291 *lp = SvCUR(tsv);
46fc3d4c 3292 return SvPVX(tsv);
a0d0e21e
LW
3293 }
3294 else {
27da23d5 3295 dVAR;
a0d0e21e 3296 STRLEN len;
73d840c0 3297 const char *t;
46fc3d4c
PP
3298
3299 if (tsv) {
3300 sv_2mortal(tsv);
3f7c398e 3301 t = SvPVX_const(tsv);
46fc3d4c
PP
3302 len = SvCUR(tsv);
3303 }
3304 else {
96827780
MB
3305 t = tmpbuf;
3306 len = strlen(tmpbuf);
46fc3d4c 3307 }
a0d0e21e 3308#ifdef FIXNEGATIVEZERO
46fc3d4c
PP
3309 if (len == 2 && t[0] == '-' && t[1] == '0') {
3310 t = "0";
3311 len = 1;
3312 }
a0d0e21e 3313#endif
862a34c6 3314 SvUPGRADE(sv, SVt_PV);
cdb061a3
NC
3315 if (lp)
3316 *lp = len;
5902b6a9 3317 s = SvGROW_mutable(sv, len + 1);
a0d0e21e 3318 SvCUR_set(sv, len);
6bf554b4 3319 SvPOKp_on(sv);
e90e2364 3320 return strcpy(s, t);
a0d0e21e 3321 }
463ee0b2
LW
3322}
3323
645c22ef 3324/*
6050d10e
JP
3325=for apidoc sv_copypv
3326
3327Copies a stringified representation of the source SV into the
3328destination SV. Automatically performs any necessary mg_get and
54f0641b 3329coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3330UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3331sv_2pv[_flags] but operates directly on an SV instead of just the
3332string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3333would lose the UTF-8'ness of the PV.
3334
3335=cut
3336*/
3337
3338void
3339Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3340{
446eaa42 3341 STRLEN len;
4d84ee25
NC
3342 const char *s;
3343 s = SvPV_const(ssv,len);
cb50f42d 3344 sv_setpvn(dsv,s,len);
446eaa42 3345 if (SvUTF8(ssv))
cb50f42d 3346 SvUTF8_on(dsv);
446eaa42 3347 else
cb50f42d 3348 SvUTF8_off(dsv);
6050d10e
JP
3349}
3350
3351/*
645c22ef
DM
3352=for apidoc sv_2pvbyte_nolen
3353
3354Return a pointer to the byte-encoded representation of the SV.
1e54db1a 3355May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3356
3357Usually accessed via the C<SvPVbyte_nolen> macro.
3358
3359=cut
3360*/
3361
7340a771
GS
3362char *
3363Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3364{
dafda6d1 3365 return sv_2pvbyte(sv, 0);
7340a771
GS
3366}
3367
645c22ef
DM
3368/*
3369=for apidoc sv_2pvbyte
3370
3371Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3372to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3373side-effect.
3374
3375Usually accessed via the C<SvPVbyte> macro.
3376
3377=cut
3378*/
3379
7340a771
GS
3380char *
3381Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3382{
0875d2fe 3383 sv_utf8_downgrade(sv,0);
97972285 3384 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771
GS
3385}
3386
645c22ef
DM
3387/*
3388=for apidoc sv_2pvutf8_nolen
3389
1e54db1a
JH
3390Return a pointer to the UTF-8-encoded representation of the SV.
3391May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3392
3393Usually accessed via the C<SvPVutf8_nolen> macro.
3394
3395=cut
3396*/
3397
7340a771
GS
3398char *
3399Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3400{
dafda6d1 3401 return sv_2pvutf8(sv, 0);
7340a771
GS
3402}
3403
645c22ef
DM
3404/*
3405=for apidoc sv_2pvutf8
3406
1e54db1a
JH
3407Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3408to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3409
3410Usually accessed via the C<SvPVutf8> macro.
3411
3412=cut
3413*/
3414
7340a771
GS
3415char *
3416Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3417{
560a288e 3418 sv_utf8_upgrade(sv);
7d59b7e4 3419 return SvPV(sv,*lp);
7340a771 3420}
1c846c1f 3421
645c22ef
DM
3422/*
3423=for apidoc sv_2bool
3424
3425This function is only called on magical items, and is only used by
8cf8f3d1 3426sv_true() or its macro equivalent.
645c22ef
DM
3427
3428=cut
3429*/
3430
463ee0b2 3431bool
864dbfa3 3432Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3433{
8990e307 3434 if (SvGMAGICAL(sv))
463ee0b2
LW
3435 mg_get(sv);
3436
a0d0e21e
LW
3437 if (!SvOK(sv))
3438 return 0;
3439 if (SvROK(sv)) {
a0d0e21e 3440 SV* tmpsv;
1554e226 3441 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3442 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3443 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3444 return SvRV(sv) != 0;
3445 }
463ee0b2 3446 if (SvPOKp(sv)) {
11343788
MB
3447 register XPV* Xpvtmp;
3448 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
339049b0 3449 (*sv->sv_u.svu_pv > '0' ||
11343788 3450 Xpvtmp->xpv_cur > 1 ||
339049b0 3451 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
463ee0b2
LW
3452 return 1;
3453 else
3454 return 0;
3455 }
3456 else {
3457 if (SvIOKp(sv))
3458 return SvIVX(sv) != 0;
3459 else {
3460 if (SvNOKp(sv))
3461 return SvNVX(sv) != 0.0;
3462 else
3463 return FALSE;
3464 }
3465 }
79072805
LW
3466}
3467
09540bc3
JH
3468/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3469 * this function provided for binary compatibility only
3470 */
3471
3472
3473STRLEN
3474Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3475{
3476 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3477}
3478
c461cf8f
JH
3479/*
3480=for apidoc sv_utf8_upgrade
3481
78ea37eb 3482Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3483Forces the SV to string form if it is not already.
4411f3b6
NIS
3484Always sets the SvUTF8 flag to avoid future validity checks even
3485if all the bytes have hibit clear.
c461cf8f 3486
13a6c0e0
JH
3487This is not as a general purpose byte encoding to Unicode interface:
3488use the Encode extension for that.
3489
8d6d96c1
HS
3490=for apidoc sv_utf8_upgrade_flags
3491
78ea37eb 3492Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3493Forces the SV to string form if it is not already.
8d6d96c1
HS
3494Always sets the SvUTF8 flag to avoid future validity checks even
3495if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3496will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3497C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3498
13a6c0e0
JH
3499This is not as a general purpose byte encoding to Unicode interface:
3500use the Encode extension for that.
3501
8d6d96c1
HS
3502=cut
3503*/
3504
3505STRLEN
3506Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3507{
808c356f
RGS
3508 if (sv == &PL_sv_undef)
3509 return 0;
e0e62c2a
NIS
3510 if (!SvPOK(sv)) {
3511 STRLEN len = 0;
d52b7888
NC
3512 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3513 (void) sv_2pv_flags(sv,&len, flags);
3514 if (SvUTF8(sv))
3515 return len;
3516 } else {
3517 (void) SvPV_force(sv,len);
3518 }
e0e62c2a 3519 }
4411f3b6 3520
f5cee72b 3521 if (SvUTF8(sv)) {
5fec3b1d 3522 return SvCUR(sv);
f5cee72b 3523 }
5fec3b1d 3524
765f542d
NC
3525 if (SvIsCOW(sv)) {
3526 sv_force_normal_flags(sv, 0);
db42d148
NIS
3527 }
3528
88632417 3529 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 3530 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 3531 else { /* Assume Latin-1/EBCDIC */
c4e7c712
NC
3532 /* This function could be much more efficient if we
3533 * had a FLAG in SVs to signal if there are any hibit
3534 * chars in the PV. Given that there isn't such a flag
3535 * make the loop as fast as possible. */
93524f2b
NC
3536 const U8 *s = (U8 *) SvPVX_const(sv);
3537 const U8 *e = (U8 *) SvEND(sv);
3538 const U8 *t = s;
c4e7c712
NC
3539 int hibit = 0;
3540
3541 while (t < e) {
3542 U8 ch = *t++;
3543 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3544 break;
3545 }
3546 if (hibit) {
3547 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
1e2ebb21 3548 U8 *recoded = bytes_to_utf8((U8*)s, &len);
c4e7c712
NC
3549
3550 SvPV_free(sv); /* No longer using what was there before. */
3551
1e2ebb21 3552 SvPV_set(sv, (char*)recoded);
c4e7c712
NC
3553 SvCUR_set(sv, len - 1);
3554 SvLEN_set(sv, len); /* No longer know the real size. */
3555 }
3556 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3557 SvUTF8_on(sv);
560a288e 3558 }
4411f3b6 3559 return SvCUR(sv);
560a288e
GS
3560}
3561
c461cf8f
JH
3562/*
3563=for apidoc sv_utf8_downgrade
3564
78ea37eb
ST
3565Attempts to convert the PV of an SV from characters to bytes.
3566If the PV contains a character beyond byte, this conversion will fail;
3567in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3568true, croaks.
3569
13a6c0e0
JH
3570This is not as a general purpose Unicode to byte encoding interface:
3571use the Encode extension for that.
3572
c461cf8f
JH
3573=cut
3574*/
3575
560a288e
GS
3576bool
3577Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3578{
78ea37eb 3579 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 3580 if (SvCUR(sv)) {
03cfe0ae 3581 U8 *s;
652088fc 3582 STRLEN len;
fa301091 3583
765f542d
NC
3584 if (SvIsCOW(sv)) {
3585 sv_force_normal_flags(sv, 0);
3586 }
03cfe0ae
NIS
3587 s = (U8 *) SvPV(sv, len);
3588 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3589 if (fail_ok)
3590 return FALSE;
3591 else {
3592 if (PL_op)
3593 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3594 OP_DESC(PL_op));
fa301091
JH
3595 else
3596 Perl_croak(aTHX_ "Wide character");
3597 }
4b3603a4 3598 }
b162af07 3599 SvCUR_set(sv, len);
67e989fb 3600 }
560a288e 3601 }
ffebcc3e 3602 SvUTF8_off(sv);
560a288e
GS
3603 return TRUE;
3604}
3605
c461cf8f
JH
3606/*
3607=for apidoc sv_utf8_encode
3608
78ea37eb
ST
3609Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3610flag off so that it looks like octets again.
c461cf8f
JH
3611
3612=cut
3613*/
3614
560a288e
GS
3615void
3616Perl_sv_utf8_encode(pTHX_ register SV *sv)
3617{
4411f3b6 3618 (void) sv_utf8_upgrade(sv);
4c94c214
NC
3619 if (SvIsCOW(sv)) {
3620 sv_force_normal_flags(sv, 0);
3621 }
3622 if (SvREADONLY(sv)) {
3623 Perl_croak(aTHX_ PL_no_modify);
3624 }
560a288e
GS
3625 SvUTF8_off(sv);
3626}
3627
4411f3b6
NIS
3628/*
3629=for apidoc sv_utf8_decode
3630
78ea37eb
ST
3631If the PV of the SV is an octet sequence in UTF-8
3632and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3633so that it looks like a character. If the PV contains only single-byte
3634characters, the C<SvUTF8> flag stays being off.
3635Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
3636
3637=cut
3638*/
3639
560a288e
GS
3640bool
3641Perl_sv_utf8_decode(pTHX_ register SV *sv)
3642{
78ea37eb 3643 if (SvPOKp(sv)) {
93524f2b
NC
3644 const U8 *c;
3645 const U8 *e;
9cbac4c7 3646
645c22ef
DM
3647 /* The octets may have got themselves encoded - get them back as
3648 * bytes
3649 */
3650 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3651 return FALSE;
3652
3653 /* it is actually just a matter of turning the utf8 flag on, but
3654 * we want to make sure everything inside is valid utf8 first.
3655 */
93524f2b 3656 c = (const U8 *) SvPVX_const(sv);
63cd0674 3657 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3658 return FALSE;
93524f2b 3659 e = (const U8 *) SvEND(sv);
511c2ff0 3660 while (c < e) {
c4d5f83a
NIS
3661 U8 ch = *c++;
3662 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3663 SvUTF8_on(sv);
3664 break;
3665 }
560a288e 3666 }
560a288e
GS
3667 }
3668 return TRUE;
3669}
3670
09540bc3
JH
3671/* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3672 * this function provided for binary compatibility only
3673 */
3674
3675void
3676Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3677{
3678 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3679}
3680
954c1994
GS
3681/*
3682=for apidoc sv_setsv
3683
645c22ef
DM
3684Copies the contents of the source SV C<ssv> into the destination SV
3685C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3686function if the source SV needs to be reused. Does not handle 'set' magic.
3687Loosely speaking, it performs a copy-by-value, obliterating any previous
3688content of the destination.
3689
3690You probably want to use one of the assortment of wrappers, such as
3691C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3692C<SvSetMagicSV_nosteal>.
3693
8d6d96c1
HS
3694=for apidoc sv_setsv_flags
3695
645c22ef
DM
3696Copies the contents of the source SV C<ssv> into the destination SV
3697C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3698function if the source SV needs to be reused. Does not handle 'set' magic.
3699Loosely speaking, it performs a copy-by-value, obliterating any previous
3700content of the destination.
3701If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
5fcdf167
NC
3702C<ssv> if appropriate, else not. If the C<flags> parameter has the
3703C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3704and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
3705
3706You probably want to use one of the assortment of wrappers, such as
3707C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3708C<SvSetMagicSV_nosteal>.
3709
3710This is the primary function for copying scalars, and most other
3711copy-ish functions and macros use this underneath.
8d6d96c1
HS
3712
3713=cut
3714*/
3715
3716void
3717Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3718{
8990e307
LW
3719 register U32 sflags;
3720 register int dtype;
3721 register int stype;
463ee0b2 3722
79072805
LW
3723 if (sstr == dstr)
3724 return;
765f542d 3725 SV_CHECK_THINKFIRST_COW_DROP(dstr);
79072805 3726 if (!sstr)
3280af22 3727 sstr = &PL_sv_undef;
8990e307
LW
3728 stype = SvTYPE(sstr);
3729 dtype = SvTYPE(dstr);
79072805 3730
a0d0e21e 3731 SvAMAGIC_off(dstr);
7a5fa8a2 3732 if ( SvVOK(dstr) )
ece467f9
JP
3733 {
3734 /* need to nuke the magic */
3735 mg_free(dstr);
3736 SvRMAGICAL_off(dstr);
3737 }
9e7bc3e8 3738
463ee0b2 3739 /* There's a lot of redundancy below but we're going for speed here */
79072805 3740
8990e307 3741 switch (stype) {
79072805 3742 case SVt_NULL:
aece5585 3743 undef_sstr:
20408e3c
GS
3744 if (dtype != SVt_PVGV) {
3745 (void)SvOK_off(dstr);
3746 return;
3747 }
3748 break;
463ee0b2 3749 case SVt_IV:
aece5585
GA
3750 if (SvIOK(sstr)) {
3751 switch (dtype) {
3752 case SVt_NULL:
8990e307 3753 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3754 break;
3755 case SVt_NV:
8990e307 3756 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3757 break;
3758 case SVt_RV:
3759 case SVt_PV:
a0d0e21e 3760 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3761 break;
3762 }
3763 (void)SvIOK_only(dstr);
45977657 3764 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
3765 if (SvIsUV(sstr))
3766 SvIsUV_on(dstr);
27c9684d
AP
3767 if (SvTAINTED(sstr))
3768 SvTAINT(dstr);
aece5585 3769 return;
8990e307 3770 }
aece5585
GA
3771 goto undef_sstr;
3772
463ee0b2 3773 case SVt_NV:
aece5585
GA
3774 if (SvNOK(sstr)) {
3775 switch (dtype) {
3776 case SVt_NULL:
3777 case SVt_IV:
8990e307 3778 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3779 break;
3780 case SVt_RV:
3781 case SVt_PV:
3782 case SVt_PVIV: