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