This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / sv.c
1 /*    sv.c
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall
5  *    and others
6  *
7  *    You may distribute under the terms of either the GNU General Public
8  *    License or the Artistic License, as specified in the README file.
9  *
10  */
11
12 /*
13  * 'I wonder what the Entish is for "yes" and "no",' he thought.
14  *                                                      --Pippin
15  *
16  *     [p.480 of _The Lord of the Rings_, III/iv: "Treebeard"]
17  */
18
19 /*
20  *
21  *
22  * This file contains the code that creates, manipulates and destroys
23  * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
24  * structure of an SV, so their creation and destruction is handled
25  * here; higher-level functions are in av.c, hv.c, and so on. Opcode
26  * level functions (eg. substr, split, join) for each of the types are
27  * in the pp*.c files.
28  */
29
30 #include "EXTERN.h"
31 #define PERL_IN_SV_C
32 #include "perl.h"
33 #include "regcomp.h"
34 #ifdef __VMS
35 # include <rms.h>
36 #endif
37
38 #ifdef __Lynx__
39 /* Missing proto on LynxOS */
40   char *gconvert(double, int, int,  char *);
41 #endif
42
43 #ifdef USE_QUADMATH
44 #  define SNPRINTF_G(nv, buffer, size, ndig) \
45     quadmath_snprintf(buffer, size, "%.*Qg", (int)ndig, (NV)(nv))
46 #else
47 #  define SNPRINTF_G(nv, buffer, size, ndig) \
48     PERL_UNUSED_RESULT(Gconvert((NV)(nv), (int)ndig, 0, buffer))
49 #endif
50
51 #ifndef SV_COW_THRESHOLD
52 #    define SV_COW_THRESHOLD                    0   /* COW iff len > K */
53 #endif
54 #ifndef SV_COWBUF_THRESHOLD
55 #    define SV_COWBUF_THRESHOLD                 1250 /* COW iff len > K */
56 #endif
57 #ifndef SV_COW_MAX_WASTE_THRESHOLD
58 #    define SV_COW_MAX_WASTE_THRESHOLD          80   /* COW iff (len - cur) < K */
59 #endif
60 #ifndef SV_COWBUF_WASTE_THRESHOLD
61 #    define SV_COWBUF_WASTE_THRESHOLD           80   /* COW iff (len - cur) < K */
62 #endif
63 #ifndef SV_COW_MAX_WASTE_FACTOR_THRESHOLD
64 #    define SV_COW_MAX_WASTE_FACTOR_THRESHOLD   2    /* COW iff len < (cur * K) */
65 #endif
66 #ifndef SV_COWBUF_WASTE_FACTOR_THRESHOLD
67 #    define SV_COWBUF_WASTE_FACTOR_THRESHOLD    2    /* COW iff len < (cur * K) */
68 #endif
69 /* Work around compiler warnings about unsigned >= THRESHOLD when thres-
70    hold is 0. */
71 #if SV_COW_THRESHOLD
72 # define GE_COW_THRESHOLD(cur) ((cur) >= SV_COW_THRESHOLD)
73 #else
74 # define GE_COW_THRESHOLD(cur) 1
75 #endif
76 #if SV_COWBUF_THRESHOLD
77 # define GE_COWBUF_THRESHOLD(cur) ((cur) >= SV_COWBUF_THRESHOLD)
78 #else
79 # define GE_COWBUF_THRESHOLD(cur) 1
80 #endif
81 #if SV_COW_MAX_WASTE_THRESHOLD
82 # define GE_COW_MAX_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COW_MAX_WASTE_THRESHOLD)
83 #else
84 # define GE_COW_MAX_WASTE_THRESHOLD(cur,len) 1
85 #endif
86 #if SV_COWBUF_WASTE_THRESHOLD
87 # define GE_COWBUF_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COWBUF_WASTE_THRESHOLD)
88 #else
89 # define GE_COWBUF_WASTE_THRESHOLD(cur,len) 1
90 #endif
91 #if SV_COW_MAX_WASTE_FACTOR_THRESHOLD
92 # define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COW_MAX_WASTE_FACTOR_THRESHOLD * (cur))
93 #else
94 # define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) 1
95 #endif
96 #if SV_COWBUF_WASTE_FACTOR_THRESHOLD
97 # define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COWBUF_WASTE_FACTOR_THRESHOLD * (cur))
98 #else
99 # define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) 1
100 #endif
101
102 #define CHECK_COW_THRESHOLD(cur,len) (\
103     GE_COW_THRESHOLD((cur)) && \
104     GE_COW_MAX_WASTE_THRESHOLD((cur),(len)) && \
105     GE_COW_MAX_WASTE_FACTOR_THRESHOLD((cur),(len)) \
106 )
107 #define CHECK_COWBUF_THRESHOLD(cur,len) (\
108     GE_COWBUF_THRESHOLD((cur)) && \
109     GE_COWBUF_WASTE_THRESHOLD((cur),(len)) && \
110     GE_COWBUF_WASTE_FACTOR_THRESHOLD((cur),(len)) \
111 )
112
113 #ifdef PERL_UTF8_CACHE_ASSERT
114 /* if adding more checks watch out for the following tests:
115  *   t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
116  *   lib/utf8.t lib/Unicode/Collate/t/index.t
117  * --jhi
118  */
119 #   define ASSERT_UTF8_CACHE(cache) \
120     STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
121                               assert((cache)[2] <= (cache)[3]); \
122                               assert((cache)[3] <= (cache)[1]);} \
123                               } STMT_END
124 #else
125 #   define ASSERT_UTF8_CACHE(cache) NOOP
126 #endif
127
128 static const char S_destroy[] = "DESTROY";
129 #define S_destroy_len (sizeof(S_destroy)-1)
130
131 /* ============================================================================
132
133 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
134 sv, av, hv...) contains type and reference count information, and for
135 many types, a pointer to the body (struct xrv, xpv, xpviv...), which
136 contains fields specific to each type.  Some types store all they need
137 in the head, so don't have a body.
138
139 In all but the most memory-paranoid configurations (ex: PURIFY), heads
140 and bodies are allocated out of arenas, which by default are
141 approximately 4K chunks of memory parcelled up into N heads or bodies.
142 Sv-bodies are allocated by their sv-type, guaranteeing size
143 consistency needed to allocate safely from arrays.
144
145 For SV-heads, the first slot in each arena is reserved, and holds a
146 link to the next arena, some flags, and a note of the number of slots.
147 Snaked through each arena chain is a linked list of free items; when
148 this becomes empty, an extra arena is allocated and divided up into N
149 items which are threaded into the free list.
150
151 SV-bodies are similar, but they use arena-sets by default, which
152 separate the link and info from the arena itself, and reclaim the 1st
153 slot in the arena.  SV-bodies are further described later.
154
155 The following global variables are associated with arenas:
156
157  PL_sv_arenaroot     pointer to list of SV arenas
158  PL_sv_root          pointer to list of free SV structures
159
160  PL_body_arenas      head of linked-list of body arenas
161  PL_body_roots[]     array of pointers to list of free bodies of svtype
162                      arrays are indexed by the svtype needed
163
164 A few special SV heads are not allocated from an arena, but are
165 instead directly created in the interpreter structure, eg PL_sv_undef.
166 The size of arenas can be changed from the default by setting
167 PERL_ARENA_SIZE appropriately at compile time.
168
169 The SV arena serves the secondary purpose of allowing still-live SVs
170 to be located and destroyed during final cleanup.
171
172 At the lowest level, the macros new_SV() and del_SV() grab and free
173 an SV head.  (If debugging with -DD, del_SV() calls the function S_del_sv()
174 to return the SV to the free list with error checking.) new_SV() calls
175 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
176 SVs in the free list have their SvTYPE field set to all ones.
177
178 At the time of very final cleanup, sv_free_arenas() is called from
179 perl_destruct() to physically free all the arenas allocated since the
180 start of the interpreter.
181
182 The internal function visit() scans the SV arenas list, and calls a specified
183 function for each SV it finds which is still live, I<i.e.> which has an SvTYPE
184 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
185 following functions (specified as [function that calls visit()] / [function
186 called by visit() for each SV]):
187
188     sv_report_used() / do_report_used()
189                         dump all remaining SVs (debugging aid)
190
191     sv_clean_objs() / do_clean_objs(),do_clean_named_objs(),
192                       do_clean_named_io_objs(),do_curse()
193                         Attempt to free all objects pointed to by RVs,
194                         try to do the same for all objects indir-
195                         ectly referenced by typeglobs too, and
196                         then do a final sweep, cursing any
197                         objects that remain.  Called once from
198                         perl_destruct(), prior to calling sv_clean_all()
199                         below.
200
201     sv_clean_all() / do_clean_all()
202                         SvREFCNT_dec(sv) each remaining SV, possibly
203                         triggering an sv_free(). It also sets the
204                         SVf_BREAK flag on the SV to indicate that the
205                         refcnt has been artificially lowered, and thus
206                         stopping sv_free() from giving spurious warnings
207                         about SVs which unexpectedly have a refcnt
208                         of zero.  called repeatedly from perl_destruct()
209                         until there are no SVs left.
210
211 =head2 Arena allocator API Summary
212
213 Private API to rest of sv.c
214
215     new_SV(),  del_SV(),
216
217     new_XPVNV(), del_body()
218     etc
219
220 Public API:
221
222     sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
223
224 =cut
225
226  * ========================================================================= */
227
228 /*
229  * "A time to plant, and a time to uproot what was planted..."
230  */
231
232 #ifdef DEBUG_LEAKING_SCALARS
233 #  define FREE_SV_DEBUG_FILE(sv) STMT_START { \
234         if ((sv)->sv_debug_file) PerlMemShared_free((sv)->sv_debug_file); \
235     } STMT_END
236 #  define DEBUG_SV_SERIAL(sv)                                               \
237     DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) del_SV\n",    \
238             PTR2UV(sv), (long)(sv)->sv_debug_serial))
239 #else
240 #  define FREE_SV_DEBUG_FILE(sv)
241 #  define DEBUG_SV_SERIAL(sv)   NOOP
242 #endif
243
244 /* Mark an SV head as unused, and add to free list.
245  *
246  * If SVf_BREAK is set, skip adding it to the free list, as this SV had
247  * its refcount artificially decremented during global destruction, so
248  * there may be dangling pointers to it. The last thing we want in that
249  * case is for it to be reused. */
250
251 #define plant_SV(p) \
252     STMT_START {                                        \
253         const U32 old_flags = SvFLAGS(p);                       \
254         MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__);  \
255         DEBUG_SV_SERIAL(p);                             \
256         FREE_SV_DEBUG_FILE(p);                          \
257         POISON_SV_HEAD(p);                              \
258         SvFLAGS(p) = SVTYPEMASK;                        \
259         if (!(old_flags & SVf_BREAK)) {         \
260             SvARENA_CHAIN_SET(p, PL_sv_root);   \
261             PL_sv_root = (p);                           \
262         }                                               \
263         --PL_sv_count;                                  \
264     } STMT_END
265
266
267 /* make some more SVs by adding another arena */
268
269 SV*
270 Perl_more_sv(pTHX)
271 {
272     SV* sv;
273     char *chunk;                /* must use New here to match call to */
274     Newx(chunk,PERL_ARENA_SIZE,char);  /* Safefree() in sv_free_arenas() */
275     sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
276     uproot_SV(sv);
277     return sv;
278 }
279
280 /* del_SV(): return an empty SV head to the free list */
281
282 #ifdef DEBUGGING
283
284 #define del_SV(p) \
285     STMT_START {                                        \
286         if (DEBUG_D_TEST)                               \
287             del_sv(p);                                  \
288         else                                            \
289             plant_SV(p);                                \
290     } STMT_END
291
292 STATIC void
293 S_del_sv(pTHX_ SV *p)
294 {
295     PERL_ARGS_ASSERT_DEL_SV;
296
297     if (DEBUG_D_TEST) {
298         SV* sva;
299         bool ok = 0;
300         for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
301             const SV * const sv = sva + 1;
302             const SV * const svend = &sva[SvREFCNT(sva)];
303             if (p >= sv && p < svend) {
304                 ok = 1;
305                 break;
306             }
307         }
308         if (!ok) {
309             Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
310                              "Attempt to free non-arena SV: 0x%" UVxf
311                              pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
312             return;
313         }
314     }
315     plant_SV(p);
316 }
317
318 #else /* ! DEBUGGING */
319
320 #define del_SV(p)   plant_SV(p)
321
322 #endif /* DEBUGGING */
323
324
325 /*
326 =for apidoc_section $SV
327
328 =for apidoc sv_add_arena
329
330 Given a chunk of memory, link it to the head of the list of arenas,
331 and split it into a list of free SVs.
332
333 =cut
334 */
335
336 static void
337 S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
338 {
339     SV *const sva = MUTABLE_SV(ptr);
340     SV* sv;
341     SV* svend;
342
343     PERL_ARGS_ASSERT_SV_ADD_ARENA;
344
345     /* The first SV in an arena isn't an SV. */
346     SvANY(sva) = (void *) PL_sv_arenaroot;              /* ptr to next arena */
347     SvREFCNT(sva) = size / sizeof(SV);          /* number of SV slots */
348     SvFLAGS(sva) = flags;                       /* FAKE if not to be freed */
349
350     PL_sv_arenaroot = sva;
351     PL_sv_root = sva + 1;
352
353     svend = &sva[SvREFCNT(sva) - 1];
354     sv = sva + 1;
355     while (sv < svend) {
356         SvARENA_CHAIN_SET(sv, (sv + 1));
357 #ifdef DEBUGGING
358         SvREFCNT(sv) = 0;
359 #endif
360         /* Must always set typemask because it's always checked in on cleanup
361            when the arenas are walked looking for objects.  */
362         SvFLAGS(sv) = SVTYPEMASK;
363         sv++;
364     }
365     SvARENA_CHAIN_SET(sv, 0);
366 #ifdef DEBUGGING
367     SvREFCNT(sv) = 0;
368 #endif
369     SvFLAGS(sv) = SVTYPEMASK;
370 }
371
372 /* visit(): call the named function for each non-free SV in the arenas
373  * whose flags field matches the flags/mask args. */
374
375 STATIC I32
376 S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
377 {
378     SV* sva;
379     I32 visited = 0;
380
381     PERL_ARGS_ASSERT_VISIT;
382
383     for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
384         const SV * const svend = &sva[SvREFCNT(sva)];
385         SV* sv;
386         for (sv = sva + 1; sv < svend; ++sv) {
387             if (SvTYPE(sv) != (svtype)SVTYPEMASK
388                     && (sv->sv_flags & mask) == flags
389                     && SvREFCNT(sv))
390             {
391                 (*f)(aTHX_ sv);
392                 ++visited;
393             }
394         }
395     }
396     return visited;
397 }
398
399 #ifdef DEBUGGING
400
401 /* called by sv_report_used() for each live SV */
402
403 static void
404 do_report_used(pTHX_ SV *const sv)
405 {
406     if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
407         PerlIO_printf(Perl_debug_log, "****\n");
408         sv_dump(sv);
409     }
410 }
411 #endif
412
413 /*
414 =for apidoc sv_report_used
415
416 Dump the contents of all SVs not yet freed (debugging aid).
417
418 =cut
419 */
420
421 void
422 Perl_sv_report_used(pTHX)
423 {
424 #ifdef DEBUGGING
425     visit(do_report_used, 0, 0);
426 #else
427     PERL_UNUSED_CONTEXT;
428 #endif
429 }
430
431 /* called by sv_clean_objs() for each live SV */
432
433 static void
434 do_clean_objs(pTHX_ SV *const ref)
435 {
436     assert (SvROK(ref));
437     {
438         SV * const target = SvRV(ref);
439         if (SvOBJECT(target)) {
440             DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
441             if (SvWEAKREF(ref)) {
442                 sv_del_backref(target, ref);
443                 SvWEAKREF_off(ref);
444                 SvRV_set(ref, NULL);
445             } else {
446                 SvROK_off(ref);
447                 SvRV_set(ref, NULL);
448                 SvREFCNT_dec_NN(target);
449             }
450         }
451     }
452 }
453
454
455 /* clear any slots in a GV which hold objects - except IO;
456  * called by sv_clean_objs() for each live GV */
457
458 static void
459 do_clean_named_objs(pTHX_ SV *const sv)
460 {
461     SV *obj;
462     assert(SvTYPE(sv) == SVt_PVGV);
463     assert(isGV_with_GP(sv));
464     if (!GvGP(sv))
465         return;
466
467     /* freeing GP entries may indirectly free the current GV;
468      * hold onto it while we mess with the GP slots */
469     SvREFCNT_inc(sv);
470
471     if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
472         DEBUG_D((PerlIO_printf(Perl_debug_log,
473                 "Cleaning named glob SV object:\n "), sv_dump(obj)));
474         GvSV(sv) = NULL;
475         SvREFCNT_dec_NN(obj);
476     }
477     if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
478         DEBUG_D((PerlIO_printf(Perl_debug_log,
479                 "Cleaning named glob AV object:\n "), sv_dump(obj)));
480         GvAV(sv) = NULL;
481         SvREFCNT_dec_NN(obj);
482     }
483     if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
484         DEBUG_D((PerlIO_printf(Perl_debug_log,
485                 "Cleaning named glob HV object:\n "), sv_dump(obj)));
486         GvHV(sv) = NULL;
487         SvREFCNT_dec_NN(obj);
488     }
489     if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
490         DEBUG_D((PerlIO_printf(Perl_debug_log,
491                 "Cleaning named glob CV object:\n "), sv_dump(obj)));
492         GvCV_set(sv, NULL);
493         SvREFCNT_dec_NN(obj);
494     }
495     SvREFCNT_dec_NN(sv); /* undo the inc above */
496 }
497
498 /* clear any IO slots in a GV which hold objects (except stderr, defout);
499  * called by sv_clean_objs() for each live GV */
500
501 static void
502 do_clean_named_io_objs(pTHX_ SV *const sv)
503 {
504     SV *obj;
505     assert(SvTYPE(sv) == SVt_PVGV);
506     assert(isGV_with_GP(sv));
507     if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
508         return;
509
510     SvREFCNT_inc(sv);
511     if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
512         DEBUG_D((PerlIO_printf(Perl_debug_log,
513                 "Cleaning named glob IO object:\n "), sv_dump(obj)));
514         GvIOp(sv) = NULL;
515         SvREFCNT_dec_NN(obj);
516     }
517     SvREFCNT_dec_NN(sv); /* undo the inc above */
518 }
519
520 /* Void wrapper to pass to visit() */
521 static void
522 do_curse(pTHX_ SV * const sv) {
523     if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
524      || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
525         return;
526     (void)curse(sv, 0);
527 }
528
529 /*
530 =for apidoc sv_clean_objs
531
532 Attempt to destroy all objects not yet freed.
533
534 =cut
535 */
536
537 void
538 Perl_sv_clean_objs(pTHX)
539 {
540     GV *olddef, *olderr;
541     PL_in_clean_objs = TRUE;
542     visit(do_clean_objs, SVf_ROK, SVf_ROK);
543     /* Some barnacles may yet remain, clinging to typeglobs.
544      * Run the non-IO destructors first: they may want to output
545      * error messages, close files etc */
546     visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
547     visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
548     /* And if there are some very tenacious barnacles clinging to arrays,
549        closures, or what have you.... */
550     visit(do_curse, SVs_OBJECT, SVs_OBJECT);
551     olddef = PL_defoutgv;
552     PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
553     if (olddef && isGV_with_GP(olddef))
554         do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
555     olderr = PL_stderrgv;
556     PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
557     if (olderr && isGV_with_GP(olderr))
558         do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
559     SvREFCNT_dec(olddef);
560     PL_in_clean_objs = FALSE;
561 }
562
563 /* called by sv_clean_all() for each live SV */
564
565 static void
566 do_clean_all(pTHX_ SV *const sv)
567 {
568     if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
569         /* don't clean pid table and strtab */
570         return;
571     }
572     DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%" UVxf "\n", PTR2UV(sv)) ));
573     SvFLAGS(sv) |= SVf_BREAK;
574     SvREFCNT_dec_NN(sv);
575 }
576
577 /*
578 =for apidoc sv_clean_all
579
580 Decrement the refcnt of each remaining SV, possibly triggering a
581 cleanup.  This function may have to be called multiple times to free
582 SVs which are in complex self-referential hierarchies.
583
584 =cut
585 */
586
587 I32
588 Perl_sv_clean_all(pTHX)
589 {
590     I32 cleaned;
591     PL_in_clean_all = TRUE;
592     cleaned = visit(do_clean_all, 0,0);
593     return cleaned;
594 }
595
596 /*
597   ARENASETS: a meta-arena implementation which separates arena-info
598   into struct arena_set, which contains an array of struct
599   arena_descs, each holding info for a single arena.  By separating
600   the meta-info from the arena, we recover the 1st slot, formerly
601   borrowed for list management.  The arena_set is about the size of an
602   arena, avoiding the needless malloc overhead of a naive linked-list.
603
604   The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
605   memory in the last arena-set (1/2 on average).  In trade, we get
606   back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
607   smaller types).  The recovery of the wasted space allows use of
608   small arenas for large, rare body types, by changing array* fields
609   in body_details_by_type[] below.
610 */
611 struct arena_desc {
612     char       *arena;          /* the raw storage, allocated aligned */
613     size_t      size;           /* its size ~4k typ */
614     svtype      utype;          /* bodytype stored in arena */
615 };
616
617 struct arena_set;
618
619 /* Get the maximum number of elements in set[] such that struct arena_set
620    will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
621    therefore likely to be 1 aligned memory page.  */
622
623 #define ARENAS_PER_SET  ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
624                           - 2 * sizeof(int)) / sizeof (struct arena_desc))
625
626 struct arena_set {
627     struct arena_set* next;
628     unsigned int   set_size;    /* ie ARENAS_PER_SET */
629     unsigned int   curr;        /* index of next available arena-desc */
630     struct arena_desc set[ARENAS_PER_SET];
631 };
632
633 /*
634 =for apidoc sv_free_arenas
635
636 Deallocate the memory used by all arenas.  Note that all the individual SV
637 heads and bodies within the arenas must already have been freed.
638
639 =cut
640
641 */
642 void
643 Perl_sv_free_arenas(pTHX)
644 {
645     SV* sva;
646     SV* svanext;
647     unsigned int i;
648
649     /* Free arenas here, but be careful about fake ones.  (We assume
650        contiguity of the fake ones with the corresponding real ones.) */
651
652     for (sva = PL_sv_arenaroot; sva; sva = svanext) {
653         svanext = MUTABLE_SV(SvANY(sva));
654         while (svanext && SvFAKE(svanext))
655             svanext = MUTABLE_SV(SvANY(svanext));
656
657         if (!SvFAKE(sva))
658             Safefree(sva);
659     }
660
661     {
662         struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
663
664         while (aroot) {
665             struct arena_set *current = aroot;
666             i = aroot->curr;
667             while (i--) {
668                 assert(aroot->set[i].arena);
669                 Safefree(aroot->set[i].arena);
670             }
671             aroot = aroot->next;
672             Safefree(current);
673         }
674     }
675     PL_body_arenas = 0;
676
677     i = PERL_ARENA_ROOTS_SIZE;
678     while (i--)
679         PL_body_roots[i] = 0;
680
681     PL_sv_arenaroot = 0;
682     PL_sv_root = 0;
683 }
684
685 /*
686   Historically, here were mid-level routines that manage the
687   allocation of bodies out of the various arenas. Some of these
688   routines and related definitions remain here, but otherse were
689   moved into sv_inline.h to facilitate inlining of newSV_type().
690
691   There are 4 kinds of arenas:
692
693   1. SV-head arenas, which are discussed and handled above
694   2. regular body arenas
695   3. arenas for reduced-size bodies
696   4. Hash-Entry arenas
697
698   Arena types 2 & 3 are chained by body-type off an array of
699   arena-root pointers, which is indexed by svtype.  Some of the
700   larger/less used body types are malloced singly, since a large
701   unused block of them is wasteful.  Also, several svtypes dont have
702   bodies; the data fits into the sv-head itself.  The arena-root
703   pointer thus has a few unused root-pointers (which may be hijacked
704   later for arena type 4)
705
706   3 differs from 2 as an optimization; some body types have several
707   unused fields in the front of the structure (which are kept in-place
708   for consistency).  These bodies can be allocated in smaller chunks,
709   because the leading fields arent accessed.  Pointers to such bodies
710   are decremented to point at the unused 'ghost' memory, knowing that
711   the pointers are used with offsets to the real memory.
712
713 Allocation of SV-bodies is similar to SV-heads, differing as follows;
714 the allocation mechanism is used for many body types, so is somewhat
715 more complicated, it uses arena-sets, and has no need for still-live
716 SV detection.
717
718 At the outermost level, (new|del)_X*V macros return bodies of the
719 appropriate type.  These macros call either (new|del)_body_type or
720 (new|del)_body_allocated macro pairs, depending on specifics of the
721 type.  Most body types use the former pair, the latter pair is used to
722 allocate body types with "ghost fields".
723
724 "ghost fields" are fields that are unused in certain types, and
725 consequently don't need to actually exist.  They are declared because
726 they're part of a "base type", which allows use of functions as
727 methods.  The simplest examples are AVs and HVs, 2 aggregate types
728 which don't use the fields which support SCALAR semantics.
729
730 For these types, the arenas are carved up into appropriately sized
731 chunks, we thus avoid wasted memory for those unaccessed members.
732 When bodies are allocated, we adjust the pointer back in memory by the
733 size of the part not allocated, so it's as if we allocated the full
734 structure.  (But things will all go boom if you write to the part that
735 is "not there", because you'll be overwriting the last members of the
736 preceding structure in memory.)
737
738 We calculate the correction using the STRUCT_OFFSET macro on the first
739 member present.  If the allocated structure is smaller (no initial NV
740 actually allocated) then the net effect is to subtract the size of the NV
741 from the pointer, to return a new pointer as if an initial NV were actually
742 allocated.  (We were using structures named *_allocated for this, but
743 this turned out to be a subtle bug, because a structure without an NV
744 could have a lower alignment constraint, but the compiler is allowed to
745 optimised accesses based on the alignment constraint of the actual pointer
746 to the full structure, for example, using a single 64 bit load instruction
747 because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
748
749 This is the same trick as was used for NV and IV bodies.  Ironically it
750 doesn't need to be used for NV bodies any more, because NV is now at
751 the start of the structure.  IV bodies, and also in some builds NV bodies,
752 don't need it either, because they are no longer allocated.
753
754 In turn, the new_body_* allocators call S_new_body(), which invokes
755 new_body_from_arena macro, which takes a lock, and takes a body off the
756 linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
757 necessary to refresh an empty list.  Then the lock is released, and
758 the body is returned.
759
760 Perl_more_bodies allocates a new arena, and carves it up into an array of N
761 bodies, which it strings into a linked list.  It looks up arena-size
762 and body-size from the body_details table described below, thus
763 supporting the multiple body-types.
764
765 If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
766 the (new|del)_X*V macros are mapped directly to malloc/free.
767
768 For each sv-type, struct body_details bodies_by_type[] carries
769 parameters which control these aspects of SV handling:
770
771 Arena_size determines whether arenas are used for this body type, and if
772 so, how big they are.  PURIFY or PERL_ARENA_SIZE=0 set this field to
773 zero, forcing individual mallocs and frees.
774
775 Body_size determines how big a body is, and therefore how many fit into
776 each arena.  Offset carries the body-pointer adjustment needed for
777 "ghost fields", and is used in *_allocated macros.
778
779 But its main purpose is to parameterize info needed in
780 Perl_sv_upgrade().  The info here dramatically simplifies the function
781 vs the implementation in 5.8.8, making it table-driven.  All fields
782 are used for this, except for arena_size.
783
784 For the sv-types that have no bodies, arenas are not used, so those
785 PL_body_roots[sv_type] are unused, and can be overloaded.  In
786 something of a special case, SVt_NULL is borrowed for HE arenas;
787 PL_body_roots[HE_ARENA_ROOT_IX=SVt_NULL] is filled by S_more_he, but the
788 bodies_by_type[SVt_NULL] slot is not used, as the table is not
789 available in hv.c. Similarly SVt_IV is re-used for HVAUX_ARENA_ROOT_IX.
790
791 */
792
793 /* return a thing to the free list */
794
795 #define del_body(thing, root)                           \
796     STMT_START {                                        \
797         void ** const thing_copy = (void **)thing;      \
798         *thing_copy = *root;                            \
799         *root = (void*)thing_copy;                      \
800     } STMT_END
801
802
803 void *
804 Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
805                   const size_t arena_size)
806 {
807     void ** const root = &PL_body_roots[sv_type];
808     struct arena_desc *adesc;
809     struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
810     unsigned int curr;
811     char *start;
812     const char *end;
813     const size_t good_arena_size = Perl_malloc_good_size(arena_size);
814 #if defined(DEBUGGING)
815     static bool done_sanity_check;
816
817     if (!done_sanity_check) {
818         unsigned int i = SVt_LAST;
819
820         done_sanity_check = TRUE;
821
822         while (i--)
823             assert (bodies_by_type[i].type == i);
824     }
825 #endif
826
827     assert(arena_size);
828
829     /* may need new arena-set to hold new arena */
830     if (!aroot || aroot->curr >= aroot->set_size) {
831         struct arena_set *newroot;
832         Newxz(newroot, 1, struct arena_set);
833         newroot->set_size = ARENAS_PER_SET;
834         newroot->next = aroot;
835         aroot = newroot;
836         PL_body_arenas = (void *) newroot;
837         DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
838     }
839
840     /* ok, now have arena-set with at least 1 empty/available arena-desc */
841     curr = aroot->curr++;
842     adesc = &(aroot->set[curr]);
843     assert(!adesc->arena);
844
845     Newx(adesc->arena, good_arena_size, char);
846     adesc->size = good_arena_size;
847     adesc->utype = sv_type;
848     DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %" UVuf "\n",
849                           curr, (void*)adesc->arena, (UV)good_arena_size));
850
851     start = (char *) adesc->arena;
852
853     /* Get the address of the byte after the end of the last body we can fit.
854        Remember, this is integer division:  */
855     end = start + good_arena_size / body_size * body_size;
856
857     /* computed count doesn't reflect the 1st slot reservation */
858 #if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
859     DEBUG_m(PerlIO_printf(Perl_debug_log,
860                           "arena %p end %p arena-size %d (from %d) type %d "
861                           "size %d ct %d\n",
862                           (void*)start, (void*)end, (int)good_arena_size,
863                           (int)arena_size, sv_type, (int)body_size,
864                           (int)good_arena_size / (int)body_size));
865 #else
866     DEBUG_m(PerlIO_printf(Perl_debug_log,
867                           "arena %p end %p arena-size %d type %d size %d ct %d\n",
868                           (void*)start, (void*)end,
869                           (int)arena_size, sv_type, (int)body_size,
870                           (int)good_arena_size / (int)body_size));
871 #endif
872     *root = (void *)start;
873
874     while (1) {
875         /* Where the next body would start:  */
876         char * const next = start + body_size;
877
878         if (next >= end) {
879             /* This is the last body:  */
880             assert(next == end);
881
882             *(void **)start = 0;
883             return *root;
884         }
885
886         *(void**) start = (void *)next;
887         start = next;
888     }
889 }
890
891 /*
892 =for apidoc sv_upgrade
893
894 Upgrade an SV to a more complex form.  Generally adds a new body type to the
895 SV, then copies across as much information as possible from the old body.
896 It croaks if the SV is already in a more complex form than requested.  You
897 generally want to use the C<SvUPGRADE> macro wrapper, which checks the type
898 before calling C<sv_upgrade>, and hence does not croak.  See also
899 C<L</svtype>>.
900
901 =cut
902 */
903
904 void
905 Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
906 {
907     void*       old_body;
908     void*       new_body;
909     const svtype old_type = SvTYPE(sv);
910     const struct body_details *new_type_details;
911     const struct body_details *old_type_details
912         = bodies_by_type + old_type;
913     SV *referent = NULL;
914
915     PERL_ARGS_ASSERT_SV_UPGRADE;
916
917     if (old_type == new_type)
918         return;
919
920     /* This clause was purposefully added ahead of the early return above to
921        the shared string hackery for (sort {$a <=> $b} keys %hash), with the
922        inference by Nick I-S that it would fix other troublesome cases. See
923        changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
924
925        Given that shared hash key scalars are no longer PVIV, but PV, there is
926        no longer need to unshare so as to free up the IVX slot for its proper
927        purpose. So it's safe to move the early return earlier.  */
928
929     if (new_type > SVt_PVMG && SvIsCOW(sv)) {
930         sv_force_normal_flags(sv, 0);
931     }
932
933     old_body = SvANY(sv);
934
935     /* Copying structures onto other structures that have been neatly zeroed
936        has a subtle gotcha. Consider XPVMG
937
938        +------+------+------+------+------+-------+-------+
939        |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH |
940        +------+------+------+------+------+-------+-------+
941        0      4      8     12     16     20      24      28
942
943        where NVs are aligned to 8 bytes, so that sizeof that structure is
944        actually 32 bytes long, with 4 bytes of padding at the end:
945
946        +------+------+------+------+------+-------+-------+------+
947        |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH | ???  |
948        +------+------+------+------+------+-------+-------+------+
949        0      4      8     12     16     20      24      28     32
950
951        so what happens if you allocate memory for this structure:
952
953        +------+------+------+------+------+-------+-------+------+------+...
954        |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH |  GP  | NAME |
955        +------+------+------+------+------+-------+-------+------+------+...
956        0      4      8     12     16     20      24      28     32     36
957
958        zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
959        expect, because you copy the area marked ??? onto GP. Now, ??? may have
960        started out as zero once, but it's quite possible that it isn't. So now,
961        rather than a nicely zeroed GP, you have it pointing somewhere random.
962        Bugs ensue.
963
964        (In fact, GP ends up pointing at a previous GP structure, because the
965        principle cause of the padding in XPVMG getting garbage is a copy of
966        sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
967        this happens to be moot because XPVGV has been re-ordered, with GP
968        no longer after STASH)
969
970        So we are careful and work out the size of used parts of all the
971        structures.  */
972
973     switch (old_type) {
974     case SVt_NULL:
975         break;
976     case SVt_IV:
977         if (SvROK(sv)) {
978             referent = SvRV(sv);
979             old_type_details = &fake_rv;
980             if (new_type == SVt_NV)
981                 new_type = SVt_PVNV;
982         } else {
983             if (new_type < SVt_PVIV) {
984                 new_type = (new_type == SVt_NV)
985                     ? SVt_PVNV : SVt_PVIV;
986             }
987         }
988         break;
989     case SVt_NV:
990         if (new_type < SVt_PVNV) {
991             new_type = SVt_PVNV;
992         }
993         break;
994     case SVt_PV:
995         assert(new_type > SVt_PV);
996         STATIC_ASSERT_STMT(SVt_IV < SVt_PV);
997         STATIC_ASSERT_STMT(SVt_NV < SVt_PV);
998         break;
999     case SVt_PVIV:
1000         break;
1001     case SVt_PVNV:
1002         break;
1003     case SVt_PVMG:
1004         /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1005            there's no way that it can be safely upgraded, because perl.c
1006            expects to Safefree(SvANY(PL_mess_sv))  */
1007         assert(sv != PL_mess_sv);
1008         break;
1009     default:
1010         if (UNLIKELY(old_type_details->cant_upgrade))
1011             Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1012                        sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
1013     }
1014
1015     if (UNLIKELY(old_type > new_type))
1016         Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1017                 (int)old_type, (int)new_type);
1018
1019     new_type_details = bodies_by_type + new_type;
1020
1021     SvFLAGS(sv) &= ~SVTYPEMASK;
1022     SvFLAGS(sv) |= new_type;
1023
1024     /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1025        the return statements above will have triggered.  */
1026     assert (new_type != SVt_NULL);
1027     switch (new_type) {
1028     case SVt_IV:
1029         assert(old_type == SVt_NULL);
1030         SET_SVANY_FOR_BODYLESS_IV(sv);
1031         SvIV_set(sv, 0);
1032         return;
1033     case SVt_NV:
1034         assert(old_type == SVt_NULL);
1035 #if NVSIZE <= IVSIZE
1036         SET_SVANY_FOR_BODYLESS_NV(sv);
1037 #else
1038         SvANY(sv) = new_XNV();
1039 #endif
1040         SvNV_set(sv, 0);
1041         return;
1042     case SVt_PVHV:
1043     case SVt_PVAV:
1044         assert(new_type_details->body_size);
1045
1046 #ifndef PURIFY
1047         assert(new_type_details->arena);
1048         assert(new_type_details->arena_size);
1049         /* This points to the start of the allocated area.  */
1050         new_body = S_new_body(aTHX_ new_type);
1051         /* xpvav and xpvhv have no offset, so no need to adjust new_body */
1052         assert(!(new_type_details->offset));
1053 #else
1054         /* We always allocated the full length item with PURIFY. To do this
1055            we fake things so that arena is false for all 16 types..  */
1056         new_body = new_NOARENAZ(new_type_details);
1057 #endif
1058         SvANY(sv) = new_body;
1059         if (new_type == SVt_PVAV) {
1060             *((XPVAV*) SvANY(sv)) = (XPVAV) {
1061                 .xmg_stash = NULL, .xmg_u = {.xmg_magic = NULL},
1062                 .xav_fill = -1, .xav_max = -1, .xav_alloc = 0
1063                 };
1064
1065             AvREAL_only(sv);
1066         } else {
1067             *((XPVHV*) SvANY(sv)) = (XPVHV) {
1068                 .xmg_stash = NULL, .xmg_u = {.xmg_magic = NULL},
1069                 .xhv_keys = 0,
1070                 /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1071                 .xhv_max = PERL_HASH_DEFAULT_HvMAX
1072                 };
1073
1074             assert(!SvOK(sv));
1075             SvOK_off(sv);
1076 #ifndef NODEFAULT_SHAREKEYS
1077             HvSHAREKEYS_on(sv);         /* key-sharing on by default */
1078 #endif
1079         }
1080
1081         /* SVt_NULL isn't the only thing upgraded to AV or HV.
1082            The target created by newSVrv also is, and it can have magic.
1083            However, it never has SvPVX set.
1084         */
1085         if (old_type == SVt_IV) {
1086             assert(!SvROK(sv));
1087         } else if (old_type >= SVt_PV) {
1088             assert(SvPVX_const(sv) == 0);
1089         }
1090
1091         if (old_type >= SVt_PVMG) {
1092             SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
1093             SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1094         } else {
1095             sv->sv_u.svu_array = NULL; /* or svu_hash  */
1096         }
1097         break;
1098
1099     case SVt_PVIV:
1100         /* XXX Is this still needed?  Was it ever needed?   Surely as there is
1101            no route from NV to PVIV, NOK can never be true  */
1102         assert(!SvNOKp(sv));
1103         assert(!SvNOK(sv));
1104         /* FALLTHROUGH */
1105     case SVt_PVIO:
1106     case SVt_PVFM:
1107     case SVt_PVGV:
1108     case SVt_PVCV:
1109     case SVt_PVLV:
1110     case SVt_INVLIST:
1111     case SVt_REGEXP:
1112     case SVt_PVMG:
1113     case SVt_PVNV:
1114     case SVt_PV:
1115
1116         assert(new_type_details->body_size);
1117         /* We always allocated the full length item with PURIFY. To do this
1118            we fake things so that arena is false for all 16 types..  */
1119 #ifndef PURIFY
1120         if(new_type_details->arena) {
1121             /* This points to the start of the allocated area.  */
1122             new_body = S_new_body(aTHX_ new_type);
1123             Zero(new_body, new_type_details->body_size, char);
1124             new_body = ((char *)new_body) - new_type_details->offset;
1125         } else
1126 #endif
1127         {
1128             new_body = new_NOARENAZ(new_type_details);
1129         }
1130         SvANY(sv) = new_body;
1131
1132         if (old_type_details->copy) {
1133             /* There is now the potential for an upgrade from something without
1134                an offset (PVNV or PVMG) to something with one (PVCV, PVFM)  */
1135             int offset = old_type_details->offset;
1136             int length = old_type_details->copy;
1137
1138             if (new_type_details->offset > old_type_details->offset) {
1139                 const int difference
1140                     = new_type_details->offset - old_type_details->offset;
1141                 offset += difference;
1142                 length -= difference;
1143             }
1144             assert (length >= 0);
1145
1146             Copy((char *)old_body + offset, (char *)new_body + offset, length,
1147                  char);
1148         }
1149
1150 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1151         /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
1152          * correct 0.0 for us.  Otherwise, if the old body didn't have an
1153          * NV slot, but the new one does, then we need to initialise the
1154          * freshly created NV slot with whatever the correct bit pattern is
1155          * for 0.0  */
1156         if (old_type_details->zero_nv && !new_type_details->zero_nv
1157             && !isGV_with_GP(sv))
1158             SvNV_set(sv, 0);
1159 #endif
1160
1161         if (UNLIKELY(new_type == SVt_PVIO)) {
1162             IO * const io = MUTABLE_IO(sv);
1163             GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
1164
1165             SvOBJECT_on(io);
1166             /* Clear the stashcache because a new IO could overrule a package
1167                name */
1168             DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
1169             hv_clear(PL_stashcache);
1170
1171             SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
1172             IoPAGE_LEN(sv) = 60;
1173         }
1174         if (old_type < SVt_PV) {
1175             /* referent will be NULL unless the old type was SVt_IV emulating
1176                SVt_RV */
1177             sv->sv_u.svu_rv = referent;
1178         }
1179         break;
1180     default:
1181         Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1182                    (unsigned long)new_type);
1183     }
1184
1185     /* if this is zero, this is a body-less SVt_NULL, SVt_IV/SVt_RV,
1186        and sometimes SVt_NV */
1187     if (old_type_details->body_size) {
1188 #ifdef PURIFY
1189         safefree(old_body);
1190 #else
1191         /* Note that there is an assumption that all bodies of types that
1192            can be upgraded came from arenas. Only the more complex non-
1193            upgradable types are allowed to be directly malloc()ed.  */
1194         assert(old_type_details->arena);
1195         del_body((void*)((char*)old_body + old_type_details->offset),
1196                  &PL_body_roots[old_type]);
1197 #endif
1198     }
1199 }
1200
1201 struct xpvhv_aux*
1202 Perl_hv_auxalloc(pTHX_ HV *hv) {
1203     const struct body_details *old_type_details = bodies_by_type + SVt_PVHV;
1204     void *old_body;
1205     void *new_body;
1206
1207     PERL_ARGS_ASSERT_HV_AUXALLOC;
1208     assert(SvTYPE(hv) == SVt_PVHV);
1209     assert(!HvHasAUX(hv));
1210
1211 #ifdef PURIFY
1212     new_body = new_NOARENAZ(&fake_hv_with_aux);
1213 #else
1214     new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
1215 #endif
1216
1217     old_body = SvANY(hv);
1218
1219     Copy((char *)old_body + old_type_details->offset,
1220          (char *)new_body + fake_hv_with_aux.offset,
1221          old_type_details->copy,
1222          char);
1223
1224 #ifdef PURIFY
1225     safefree(old_body);
1226 #else
1227     assert(old_type_details->arena);
1228     del_body((void*)((char*)old_body + old_type_details->offset),
1229              &PL_body_roots[SVt_PVHV]);
1230 #endif
1231
1232     SvANY(hv) = (XPVHV *) new_body;
1233     SvFLAGS(hv) |= SVphv_HasAUX;
1234     return HvAUX(hv);
1235 }
1236
1237 /*
1238 =for apidoc sv_backoff
1239
1240 Remove any string offset.  You should normally use the C<SvOOK_off> macro
1241 wrapper instead.
1242
1243 =cut
1244 */
1245
1246 /* prior to 5.000 stable, this function returned the new OOK-less SvFLAGS
1247    prior to 5.23.4 this function always returned 0
1248 */
1249
1250 void
1251 Perl_sv_backoff(SV *const sv)
1252 {
1253     STRLEN delta;
1254     const char * const s = SvPVX_const(sv);
1255
1256     PERL_ARGS_ASSERT_SV_BACKOFF;
1257
1258     assert(SvOOK(sv));
1259     assert(SvTYPE(sv) != SVt_PVHV);
1260     assert(SvTYPE(sv) != SVt_PVAV);
1261
1262     SvOOK_offset(sv, delta);
1263
1264     SvLEN_set(sv, SvLEN(sv) + delta);
1265     SvPV_set(sv, SvPVX(sv) - delta);
1266     SvFLAGS(sv) &= ~SVf_OOK;
1267     Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1268     return;
1269 }
1270
1271
1272 /* forward declaration */
1273 static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
1274
1275
1276 /*
1277 =for apidoc sv_grow
1278
1279 Expands the character buffer in the SV.  If necessary, uses C<sv_unref> and
1280 upgrades the SV to C<SVt_PV>.  Returns a pointer to the character buffer.
1281 Use the C<SvGROW> wrapper instead.
1282
1283 =cut
1284 */
1285
1286
1287 char *
1288 Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
1289 {
1290     char *s;
1291
1292     PERL_ARGS_ASSERT_SV_GROW;
1293
1294     if (SvROK(sv))
1295         sv_unref(sv);
1296     if (SvTYPE(sv) < SVt_PV) {
1297         sv_upgrade(sv, SVt_PV);
1298         s = SvPVX_mutable(sv);
1299     }
1300     else if (SvOOK(sv)) {       /* pv is offset? */
1301         sv_backoff(sv);
1302         s = SvPVX_mutable(sv);
1303         if (newlen > SvLEN(sv))
1304             newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1305     }
1306     else
1307     {
1308         if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
1309         s = SvPVX_mutable(sv);
1310     }
1311
1312 #ifdef PERL_COPY_ON_WRITE
1313     /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1314      * to store the COW count. So in general, allocate one more byte than
1315      * asked for, to make it likely this byte is always spare: and thus
1316      * make more strings COW-able.
1317      *
1318      * Only increment if the allocation isn't MEM_SIZE_MAX,
1319      * otherwise it will wrap to 0.
1320      */
1321     if ( newlen != MEM_SIZE_MAX )
1322         newlen++;
1323 #endif
1324
1325 #if defined(PERL_USE_MALLOC_SIZE) && defined(Perl_safesysmalloc_size)
1326 #define PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1327 #endif
1328
1329     if (newlen > SvLEN(sv)) {           /* need more room? */
1330         STRLEN minlen = SvCUR(sv);
1331         minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10;
1332         if (newlen < minlen)
1333             newlen = minlen;
1334 #ifndef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1335
1336         /* Don't round up on the first allocation, as odds are pretty good that
1337          * the initial request is accurate as to what is really needed */
1338         if (SvLEN(sv)) {
1339             STRLEN rounded = PERL_STRLEN_ROUNDUP(newlen);
1340             if (rounded > newlen)
1341                 newlen = rounded;
1342         }
1343 #endif
1344         if (SvLEN(sv) && s) {
1345             s = (char*)saferealloc(s, newlen);
1346         }
1347         else {
1348             s = (char*)safemalloc(newlen);
1349             if (SvPVX_const(sv) && SvCUR(sv)) {
1350                 Move(SvPVX_const(sv), s, SvCUR(sv), char);
1351             }
1352         }
1353         SvPV_set(sv, s);
1354 #ifdef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1355         /* Do this here, do it once, do it right, and then we will never get
1356            called back into sv_grow() unless there really is some growing
1357            needed.  */
1358         SvLEN_set(sv, Perl_safesysmalloc_size(s));
1359 #else
1360         SvLEN_set(sv, newlen);
1361 #endif
1362     }
1363     return s;
1364 }
1365
1366 /*
1367 =for apidoc sv_grow_fresh
1368
1369 A cut-down version of sv_grow intended only for when sv is a freshly-minted
1370 SVt_PV, SVt_PVIV, SVt_PVNV, or SVt_PVMG. i.e. sv has the default flags, has
1371 never been any other type, and does not have an existing string. Basically,
1372 just assigns a char buffer and returns a pointer to it.
1373
1374 =cut
1375 */
1376
1377
1378 char *
1379 Perl_sv_grow_fresh(pTHX_ SV *const sv, STRLEN newlen)
1380 {
1381     char *s;
1382
1383     PERL_ARGS_ASSERT_SV_GROW_FRESH;
1384
1385     assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
1386     assert(!SvROK(sv));
1387     assert(!SvOOK(sv));
1388     assert(!SvIsCOW(sv));
1389     assert(!SvLEN(sv));
1390     assert(!SvCUR(sv));
1391
1392 #ifdef PERL_COPY_ON_WRITE
1393     /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1394      * to store the COW count. So in general, allocate one more byte than
1395      * asked for, to make it likely this byte is always spare: and thus
1396      * make more strings COW-able.
1397      *
1398      * Only increment if the allocation isn't MEM_SIZE_MAX,
1399      * otherwise it will wrap to 0.
1400      */
1401     if ( newlen != MEM_SIZE_MAX )
1402         newlen++;
1403 #endif
1404
1405     /* 10 is a longstanding, hardcoded minimum length in sv_grow. */
1406     /* Just doing the same here for consistency. */
1407     if (newlen < 10)
1408         newlen = 10;
1409
1410     s = (char*)safemalloc(newlen);
1411     SvPV_set(sv, s);
1412
1413     /* No PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC here, since many strings */
1414     /* will never be grown once set. Let the real sv_grow worry about that. */
1415     SvLEN_set(sv, newlen);
1416     return s;
1417 }
1418
1419 /*
1420 =for apidoc sv_setiv
1421 =for apidoc_item sv_setiv_mg
1422
1423 These copy an integer into the given SV, upgrading first if necessary.
1424
1425 They differ only in that C<sv_setiv_mg> handles 'set' magic; C<sv_setiv> does
1426 not.
1427
1428 =cut
1429 */
1430
1431 void
1432 Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
1433 {
1434     PERL_ARGS_ASSERT_SV_SETIV;
1435
1436     SV_CHECK_THINKFIRST_COW_DROP(sv);
1437     switch (SvTYPE(sv)) {
1438     case SVt_NULL:
1439     case SVt_NV:
1440         sv_upgrade(sv, SVt_IV);
1441         break;
1442     case SVt_PV:
1443         sv_upgrade(sv, SVt_PVIV);
1444         break;
1445
1446     case SVt_PVGV:
1447         if (!isGV_with_GP(sv))
1448             break;
1449         /* FALLTHROUGH */
1450     case SVt_PVAV:
1451     case SVt_PVHV:
1452     case SVt_PVCV:
1453     case SVt_PVFM:
1454     case SVt_PVIO:
1455         /* diag_listed_as: Can't coerce %s to %s in %s */
1456         Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1457                    OP_DESC(PL_op));
1458         NOT_REACHED; /* NOTREACHED */
1459         break;
1460     default: NOOP;
1461     }
1462     (void)SvIOK_only(sv);                       /* validate number */
1463     SvIV_set(sv, i);
1464     SvTAINT(sv);
1465 }
1466
1467 void
1468 Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
1469 {
1470     PERL_ARGS_ASSERT_SV_SETIV_MG;
1471
1472     sv_setiv(sv,i);
1473     SvSETMAGIC(sv);
1474 }
1475
1476 /*
1477 =for apidoc sv_setuv
1478 =for apidoc_item sv_setuv_mg
1479
1480 These copy an unsigned integer into the given SV, upgrading first if necessary.
1481
1482
1483 They differ only in that C<sv_setuv_mg> handles 'set' magic; C<sv_setuv> does
1484 not.
1485
1486 =cut
1487 */
1488
1489 void
1490 Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
1491 {
1492     PERL_ARGS_ASSERT_SV_SETUV;
1493
1494     /* With the if statement to ensure that integers are stored as IVs whenever
1495        possible:
1496        u=1.49  s=0.52  cu=72.49  cs=10.64  scripts=270  tests=20865
1497
1498        without
1499        u=1.35  s=0.47  cu=73.45  cs=11.43  scripts=270  tests=20865
1500
1501        If you wish to remove the following if statement, so that this routine
1502        (and its callers) always return UVs, please benchmark to see what the
1503        effect is. Modern CPUs may be different. Or may not :-)
1504     */
1505     if (u <= (UV)IV_MAX) {
1506        sv_setiv(sv, (IV)u);
1507        return;
1508     }
1509     sv_setiv(sv, 0);
1510     SvIsUV_on(sv);
1511     SvUV_set(sv, u);
1512 }
1513
1514 void
1515 Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
1516 {
1517     PERL_ARGS_ASSERT_SV_SETUV_MG;
1518
1519     sv_setuv(sv,u);
1520     SvSETMAGIC(sv);
1521 }
1522
1523 /*
1524 =for apidoc sv_setnv
1525 =for apidoc_item sv_setnv_mg
1526
1527 These copy a double into the given SV, upgrading first if necessary.
1528
1529 They differ only in that C<sv_setnv_mg> handles 'set' magic; C<sv_setnv> does
1530 not.
1531
1532 =cut
1533 */
1534
1535 void
1536 Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
1537 {
1538     PERL_ARGS_ASSERT_SV_SETNV;
1539
1540     SV_CHECK_THINKFIRST_COW_DROP(sv);
1541     switch (SvTYPE(sv)) {
1542     case SVt_NULL:
1543     case SVt_IV:
1544         sv_upgrade(sv, SVt_NV);
1545         break;
1546     case SVt_PV:
1547     case SVt_PVIV:
1548         sv_upgrade(sv, SVt_PVNV);
1549         break;
1550
1551     case SVt_PVGV:
1552         if (!isGV_with_GP(sv))
1553             break;
1554         /* FALLTHROUGH */
1555     case SVt_PVAV:
1556     case SVt_PVHV:
1557     case SVt_PVCV:
1558     case SVt_PVFM:
1559     case SVt_PVIO:
1560         /* diag_listed_as: Can't coerce %s to %s in %s */
1561         Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1562                    OP_DESC(PL_op));
1563         NOT_REACHED; /* NOTREACHED */
1564         break;
1565     default: NOOP;
1566     }
1567     SvNV_set(sv, num);
1568     (void)SvNOK_only(sv);                       /* validate number */
1569     SvTAINT(sv);
1570 }
1571
1572 void
1573 Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
1574 {
1575     PERL_ARGS_ASSERT_SV_SETNV_MG;
1576
1577     sv_setnv(sv,num);
1578     SvSETMAGIC(sv);
1579 }
1580
1581 /*
1582 =for apidoc sv_setrv_noinc
1583 =for apidoc_item sv_setrv_noinc_mg
1584
1585 Copies an SV pointer into the given SV as an SV reference, upgrading it if
1586 necessary. After this, C<SvRV(sv)> is equal to I<ref>. This does not adjust
1587 the reference count of I<ref>. The reference I<ref> must not be NULL.
1588
1589 C<sv_setrv_noinc_mg> will invoke 'set' magic on the SV; C<sv_setrv_noinc> will
1590 not.
1591
1592 =cut
1593 */
1594
1595 void
1596 Perl_sv_setrv_noinc(pTHX_ SV *const sv, SV *const ref)
1597 {
1598     PERL_ARGS_ASSERT_SV_SETRV_NOINC;
1599
1600     SV_CHECK_THINKFIRST_COW_DROP(sv);
1601     prepare_SV_for_RV(sv);
1602
1603     SvOK_off(sv);
1604     SvRV_set(sv, ref);
1605     SvROK_on(sv);
1606 }
1607
1608 void
1609 Perl_sv_setrv_noinc_mg(pTHX_ SV *const sv, SV *const ref)
1610 {
1611     PERL_ARGS_ASSERT_SV_SETRV_NOINC_MG;
1612
1613     sv_setrv_noinc(sv, ref);
1614     SvSETMAGIC(sv);
1615 }
1616
1617 /*
1618 =for apidoc sv_setrv_inc
1619 =for apidoc_item sv_setrv_inc_mg
1620
1621 As C<sv_setrv_noinc> but increments the reference count of I<ref>.
1622
1623 C<sv_setrv_inc_mg> will invoke 'set' magic on the SV; C<sv_setrv_inc> will
1624 not.
1625
1626 =cut
1627 */
1628
1629 void
1630 Perl_sv_setrv_inc(pTHX_ SV *const sv, SV *const ref)
1631 {
1632     PERL_ARGS_ASSERT_SV_SETRV_INC;
1633
1634     sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1635 }
1636
1637 void
1638 Perl_sv_setrv_inc_mg(pTHX_ SV *const sv, SV *const ref)
1639 {
1640     PERL_ARGS_ASSERT_SV_SETRV_INC_MG;
1641
1642     sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1643     SvSETMAGIC(sv);
1644 }
1645
1646 /* Return a cleaned-up, printable version of sv, for non-numeric, or
1647  * not incrementable warning display.
1648  * Originally part of S_not_a_number().
1649  * The return value may be != tmpbuf.
1650  */
1651
1652 STATIC const char *
1653 S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
1654     const char *pv;
1655
1656      PERL_ARGS_ASSERT_SV_DISPLAY;
1657
1658      if (DO_UTF8(sv)) {
1659           SV *dsv = newSVpvs_flags("", SVs_TEMP);
1660           pv = sv_uni_display(dsv, sv, 32, UNI_DISPLAY_ISPRINT);
1661      } else {
1662           char *d = tmpbuf;
1663           const char * const limit = tmpbuf + tmpbuf_size - 8;
1664           /* each *s can expand to 4 chars + "...\0",
1665              i.e. need room for 8 chars */
1666
1667           const char *s = SvPVX_const(sv);
1668           const char * const end = s + SvCUR(sv);
1669           for ( ; s < end && d < limit; s++ ) {
1670                int ch = (U8) *s;
1671                if (! isASCII(ch) && !isPRINT_LC(ch)) {
1672                     *d++ = 'M';
1673                     *d++ = '-';
1674
1675                     /* Map to ASCII "equivalent" of Latin1 */
1676                     ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
1677                }
1678                if (ch == '\n') {
1679                     *d++ = '\\';
1680                     *d++ = 'n';
1681                }
1682                else if (ch == '\r') {
1683                     *d++ = '\\';
1684                     *d++ = 'r';
1685                }
1686                else if (ch == '\f') {
1687                     *d++ = '\\';
1688                     *d++ = 'f';
1689                }
1690                else if (ch == '\\') {
1691                     *d++ = '\\';
1692                     *d++ = '\\';
1693                }
1694                else if (ch == '\0') {
1695                     *d++ = '\\';
1696                     *d++ = '0';
1697                }
1698                else if (isPRINT_LC(ch))
1699                     *d++ = ch;
1700                else {
1701                     *d++ = '^';
1702                     *d++ = toCTRL(ch);
1703                }
1704           }
1705           if (s < end) {
1706                *d++ = '.';
1707                *d++ = '.';
1708                *d++ = '.';
1709           }
1710           *d = '\0';
1711           pv = tmpbuf;
1712     }
1713
1714     return pv;
1715 }
1716
1717 /* Print an "isn't numeric" warning, using a cleaned-up,
1718  * printable version of the offending string
1719  */
1720
1721 STATIC void
1722 S_not_a_number(pTHX_ SV *const sv)
1723 {
1724      char tmpbuf[64];
1725      const char *pv;
1726
1727      PERL_ARGS_ASSERT_NOT_A_NUMBER;
1728
1729      pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1730
1731     if (PL_op)
1732         Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1733                     /* diag_listed_as: Argument "%s" isn't numeric%s */
1734                     "Argument \"%s\" isn't numeric in %s", pv,
1735                     OP_DESC(PL_op));
1736     else
1737         Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1738                     /* diag_listed_as: Argument "%s" isn't numeric%s */
1739                     "Argument \"%s\" isn't numeric", pv);
1740 }
1741
1742 STATIC void
1743 S_not_incrementable(pTHX_ SV *const sv) {
1744      char tmpbuf[64];
1745      const char *pv;
1746
1747      PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
1748
1749      pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1750
1751      Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1752                  "Argument \"%s\" treated as 0 in increment (++)", pv);
1753 }
1754
1755 /*
1756 =for apidoc looks_like_number
1757
1758 Test if the content of an SV looks like a number (or is a number).
1759 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1760 non-numeric warning), even if your C<atof()> doesn't grok them.  Get-magic is
1761 ignored.
1762
1763 =cut
1764 */
1765
1766 I32
1767 Perl_looks_like_number(pTHX_ SV *const sv)
1768 {
1769     const char *sbegin;
1770     STRLEN len;
1771     int numtype;
1772
1773     PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1774
1775     if (SvPOK(sv) || SvPOKp(sv)) {
1776         sbegin = SvPV_nomg_const(sv, len);
1777     }
1778     else
1779         return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1780     numtype = grok_number(sbegin, len, NULL);
1781     return ((numtype & IS_NUMBER_TRAILING)) ? 0 : numtype;
1782 }
1783
1784 STATIC bool
1785 S_glob_2number(pTHX_ GV * const gv)
1786 {
1787     PERL_ARGS_ASSERT_GLOB_2NUMBER;
1788
1789     /* We know that all GVs stringify to something that is not-a-number,
1790         so no need to test that.  */
1791     if (ckWARN(WARN_NUMERIC))
1792     {
1793         SV *const buffer = sv_newmortal();
1794         gv_efullname3(buffer, gv, "*");
1795         not_a_number(buffer);
1796     }
1797     /* We just want something true to return, so that S_sv_2iuv_common
1798         can tail call us and return true.  */
1799     return TRUE;
1800 }
1801
1802 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1803    until proven guilty, assume that things are not that bad... */
1804
1805 /*
1806    NV_PRESERVES_UV:
1807
1808    As 64 bit platforms often have an NV that doesn't preserve all bits of
1809    an IV (an assumption perl has been based on to date) it becomes necessary
1810    to remove the assumption that the NV always carries enough precision to
1811    recreate the IV whenever needed, and that the NV is the canonical form.
1812    Instead, IV/UV and NV need to be given equal rights. So as to not lose
1813    precision as a side effect of conversion (which would lead to insanity
1814    and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1815    1) to distinguish between IV/UV/NV slots that have a valid conversion cached
1816       where precision was lost, and IV/UV/NV slots that have a valid conversion
1817       which has lost no precision
1818    2) to ensure that if a numeric conversion to one form is requested that
1819       would lose precision, the precise conversion (or differently
1820       imprecise conversion) is also performed and cached, to prevent
1821       requests for different numeric formats on the same SV causing
1822       lossy conversion chains. (lossless conversion chains are perfectly
1823       acceptable (still))
1824
1825
1826    flags are used:
1827    SvIOKp is true if the IV slot contains a valid value
1828    SvIOK  is true only if the IV value is accurate (UV if SvIOK_UV true)
1829    SvNOKp is true if the NV slot contains a valid value
1830    SvNOK  is true only if the NV value is accurate
1831
1832    so
1833    while converting from PV to NV, check to see if converting that NV to an
1834    IV(or UV) would lose accuracy over a direct conversion from PV to
1835    IV(or UV). If it would, cache both conversions, return NV, but mark
1836    SV as IOK NOKp (ie not NOK).
1837
1838    While converting from PV to IV, check to see if converting that IV to an
1839    NV would lose accuracy over a direct conversion from PV to NV. If it
1840    would, cache both conversions, flag similarly.
1841
1842    Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1843    correctly because if IV & NV were set NV *always* overruled.
1844    Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1845    changes - now IV and NV together means that the two are interchangeable:
1846    SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1847
1848    The benefit of this is that operations such as pp_add know that if
1849    SvIOK is true for both left and right operands, then integer addition
1850    can be used instead of floating point (for cases where the result won't
1851    overflow). Before, floating point was always used, which could lead to
1852    loss of precision compared with integer addition.
1853
1854    * making IV and NV equal status should make maths accurate on 64 bit
1855      platforms
1856    * may speed up maths somewhat if pp_add and friends start to use
1857      integers when possible instead of fp. (Hopefully the overhead in
1858      looking for SvIOK and checking for overflow will not outweigh the
1859      fp to integer speedup)
1860    * will slow down integer operations (callers of SvIV) on "inaccurate"
1861      values, as the change from SvIOK to SvIOKp will cause a call into
1862      sv_2iv each time rather than a macro access direct to the IV slot
1863    * should speed up number->string conversion on integers as IV is
1864      favoured when IV and NV are equally accurate
1865
1866    ####################################################################
1867    You had better be using SvIOK_notUV if you want an IV for arithmetic:
1868    SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1869    On the other hand, SvUOK is true iff UV.
1870    ####################################################################
1871
1872    Your mileage will vary depending your CPU's relative fp to integer
1873    performance ratio.
1874 */
1875
1876 #ifndef NV_PRESERVES_UV
1877 #  define IS_NUMBER_UNDERFLOW_IV 1
1878 #  define IS_NUMBER_UNDERFLOW_UV 2
1879 #  define IS_NUMBER_IV_AND_UV    2
1880 #  define IS_NUMBER_OVERFLOW_IV  4
1881 #  define IS_NUMBER_OVERFLOW_UV  5
1882
1883 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
1884
1885 /* For sv_2nv these three cases are "SvNOK and don't bother casting"  */
1886 STATIC int
1887 S_sv_2iuv_non_preserve(pTHX_ SV *const sv
1888 #  ifdef DEBUGGING
1889                        , I32 numtype
1890 #  endif
1891                        )
1892 {
1893     PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
1894     PERL_UNUSED_CONTEXT;
1895
1896     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));
1897     if (SvNVX(sv) < (NV)IV_MIN) {
1898         (void)SvIOKp_on(sv);
1899         (void)SvNOK_on(sv);
1900         SvIV_set(sv, IV_MIN);
1901         return IS_NUMBER_UNDERFLOW_IV;
1902     }
1903     if (SvNVX(sv) > (NV)UV_MAX) {
1904         (void)SvIOKp_on(sv);
1905         (void)SvNOK_on(sv);
1906         SvIsUV_on(sv);
1907         SvUV_set(sv, UV_MAX);
1908         return IS_NUMBER_OVERFLOW_UV;
1909     }
1910     (void)SvIOKp_on(sv);
1911     (void)SvNOK_on(sv);
1912     /* Can't use strtol etc to convert this string.  (See truth table in
1913        sv_2iv  */
1914     if (SvNVX(sv) < IV_MAX_P1) {
1915         SvIV_set(sv, I_V(SvNVX(sv)));
1916         if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1917             SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1918         } else {
1919             /* Integer is imprecise. NOK, IOKp */
1920         }
1921         return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1922     }
1923     SvIsUV_on(sv);
1924     SvUV_set(sv, U_V(SvNVX(sv)));
1925     if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1926         if (SvUVX(sv) == UV_MAX) {
1927             /* As we know that NVs don't preserve UVs, UV_MAX cannot
1928                possibly be preserved by NV. Hence, it must be overflow.
1929                NOK, IOKp */
1930             return IS_NUMBER_OVERFLOW_UV;
1931         }
1932         SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1933     } else {
1934         /* Integer is imprecise. NOK, IOKp */
1935     }
1936     return IS_NUMBER_OVERFLOW_IV;
1937 }
1938 #endif /* !NV_PRESERVES_UV*/
1939
1940 /* If numtype is infnan, set the NV of the sv accordingly.
1941  * If numtype is anything else, try setting the NV using Atof(PV). */
1942 static void
1943 S_sv_setnv(pTHX_ SV* sv, int numtype)
1944 {
1945     bool pok = cBOOL(SvPOK(sv));
1946     bool nok = FALSE;
1947 #ifdef NV_INF
1948     if ((numtype & IS_NUMBER_INFINITY)) {
1949         SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF);
1950         nok = TRUE;
1951     } else
1952 #endif
1953 #ifdef NV_NAN
1954     if ((numtype & IS_NUMBER_NAN)) {
1955         SvNV_set(sv, NV_NAN);
1956         nok = TRUE;
1957     } else
1958 #endif
1959     if (pok) {
1960         SvNV_set(sv, Atof(SvPVX_const(sv)));
1961         /* Purposefully no true nok here, since we don't want to blow
1962          * away the possible IOK/UV of an existing sv. */
1963     }
1964     if (nok) {
1965         SvNOK_only(sv); /* No IV or UV please, this is pure infnan. */
1966         if (pok)
1967             SvPOK_on(sv); /* PV is okay, though. */
1968     }
1969 }
1970
1971 STATIC bool
1972 S_sv_2iuv_common(pTHX_ SV *const sv)
1973 {
1974     PERL_ARGS_ASSERT_SV_2IUV_COMMON;
1975
1976     if (SvNOKp(sv)) {
1977         /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
1978          * without also getting a cached IV/UV from it at the same time
1979          * (ie PV->NV conversion should detect loss of accuracy and cache
1980          * IV or UV at same time to avoid this. */
1981         /* IV-over-UV optimisation - choose to cache IV if possible */
1982
1983         if (SvTYPE(sv) == SVt_NV)
1984             sv_upgrade(sv, SVt_PVNV);
1985
1986     got_nv:
1987         (void)SvIOKp_on(sv);    /* Must do this first, to clear any SvOOK */
1988         /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
1989            certainly cast into the IV range at IV_MAX, whereas the correct
1990            answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
1991            cases go to UV */
1992 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1993         if (Perl_isnan(SvNVX(sv))) {
1994             SvUV_set(sv, 0);
1995             SvIsUV_on(sv);
1996             return FALSE;
1997         }
1998 #endif
1999         if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2000             SvIV_set(sv, I_V(SvNVX(sv)));
2001             if (SvNVX(sv) == (NV) SvIVX(sv)
2002 #ifndef NV_PRESERVES_UV
2003                 && SvIVX(sv) != IV_MIN /* avoid negating IV_MIN below */
2004                 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2005                     (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2006                 /* Don't flag it as "accurately an integer" if the number
2007                    came from a (by definition imprecise) NV operation, and
2008                    we're outside the range of NV integer precision */
2009 #endif
2010                 ) {
2011                 if (SvNOK(sv))
2012                     SvIOK_on(sv);  /* Can this go wrong with rounding? NWC */
2013                 else {
2014                     /* scalar has trailing garbage, eg "42a" */
2015                 }
2016                 DEBUG_c(PerlIO_printf(Perl_debug_log,
2017                                       "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (precise)\n",
2018                                       PTR2UV(sv),
2019                                       SvNVX(sv),
2020                                       SvIVX(sv)));
2021
2022             } else {
2023                 /* IV not precise.  No need to convert from PV, as NV
2024                    conversion would already have cached IV if it detected
2025                    that PV->IV would be better than PV->NV->IV
2026                    flags already correct - don't set public IOK.  */
2027                 DEBUG_c(PerlIO_printf(Perl_debug_log,
2028                                       "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (imprecise)\n",
2029                                       PTR2UV(sv),
2030                                       SvNVX(sv),
2031                                       SvIVX(sv)));
2032             }
2033             /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2034                but the cast (NV)IV_MIN rounds to a the value less (more
2035                negative) than IV_MIN which happens to be equal to SvNVX ??
2036                Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2037                NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2038                (NV)UVX == NVX are both true, but the values differ. :-(
2039                Hopefully for 2s complement IV_MIN is something like
2040                0x8000000000000000 which will be exact. NWC */
2041         }
2042         else {
2043             SvUV_set(sv, U_V(SvNVX(sv)));
2044             if (
2045                 (SvNVX(sv) == (NV) SvUVX(sv))
2046 #ifndef  NV_PRESERVES_UV
2047                 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2048                 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2049                 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2050                 /* Don't flag it as "accurately an integer" if the number
2051                    came from a (by definition imprecise) NV operation, and
2052                    we're outside the range of NV integer precision */
2053 #endif
2054                 && SvNOK(sv)
2055                 )
2056                 SvIOK_on(sv);
2057             SvIsUV_on(sv);
2058             DEBUG_c(PerlIO_printf(Perl_debug_log,
2059                                   "0x%" UVxf " 2iv(%" UVuf " => %" IVdf ") (as unsigned)\n",
2060                                   PTR2UV(sv),
2061                                   SvUVX(sv),
2062                                   SvUVX(sv)));
2063         }
2064     }
2065     else if (SvPOKp(sv)) {
2066         UV value;
2067         int numtype;
2068         const char *s = SvPVX_const(sv);
2069         const STRLEN cur = SvCUR(sv);
2070
2071         /* short-cut for a single digit string like "1" */
2072
2073         if (cur == 1) {
2074             char c = *s;
2075             if (isDIGIT(c)) {
2076                 if (SvTYPE(sv) < SVt_PVIV)
2077                     sv_upgrade(sv, SVt_PVIV);
2078                 (void)SvIOK_on(sv);
2079                 SvIV_set(sv, (IV)(c - '0'));
2080                 return FALSE;
2081             }
2082         }
2083
2084         numtype = grok_number(s, cur, &value);
2085         /* We want to avoid a possible problem when we cache an IV/ a UV which
2086            may be later translated to an NV, and the resulting NV is not
2087            the same as the direct translation of the initial string
2088            (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2089            be careful to ensure that the value with the .456 is around if the
2090            NV value is requested in the future).
2091
2092            This means that if we cache such an IV/a UV, we need to cache the
2093            NV as well.  Moreover, we trade speed for space, and do not
2094            cache the NV if we are sure it's not needed.
2095          */
2096
2097         /* SVt_PVNV is one higher than SVt_PVIV, hence this order  */
2098         if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2099              == IS_NUMBER_IN_UV) {
2100             /* It's definitely an integer, only upgrade to PVIV */
2101             if (SvTYPE(sv) < SVt_PVIV)
2102                 sv_upgrade(sv, SVt_PVIV);
2103             (void)SvIOK_on(sv);
2104         } else if (SvTYPE(sv) < SVt_PVNV)
2105             sv_upgrade(sv, SVt_PVNV);
2106
2107         if ((numtype & (IS_NUMBER_INFINITY | IS_NUMBER_NAN))) {
2108             if (ckWARN(WARN_NUMERIC) && ((numtype & IS_NUMBER_TRAILING)))
2109                 not_a_number(sv);
2110             S_sv_setnv(aTHX_ sv, numtype);
2111             goto got_nv;        /* Fill IV/UV slot and set IOKp */
2112         }
2113
2114         /* If NVs preserve UVs then we only use the UV value if we know that
2115            we aren't going to call atof() below. If NVs don't preserve UVs
2116            then the value returned may have more precision than atof() will
2117            return, even though value isn't perfectly accurate.  */
2118         if ((numtype & (IS_NUMBER_IN_UV
2119 #ifdef NV_PRESERVES_UV
2120                         | IS_NUMBER_NOT_INT
2121 #endif
2122             )) == IS_NUMBER_IN_UV) {
2123             /* This won't turn off the public IOK flag if it was set above  */
2124             (void)SvIOKp_on(sv);
2125
2126             if (!(numtype & IS_NUMBER_NEG)) {
2127                 /* positive */;
2128                 if (value <= (UV)IV_MAX) {
2129                     SvIV_set(sv, (IV)value);
2130                 } else {
2131                     /* it didn't overflow, and it was positive. */
2132                     SvUV_set(sv, value);
2133                     SvIsUV_on(sv);
2134                 }
2135             } else {
2136                 /* 2s complement assumption  */
2137                 if (value <= (UV)IV_MIN) {
2138                     SvIV_set(sv, value == (UV)IV_MIN
2139                                     ? IV_MIN : -(IV)value);
2140                 } else {
2141                     /* Too negative for an IV.  This is a double upgrade, but
2142                        I'm assuming it will be rare.  */
2143                     if (SvTYPE(sv) < SVt_PVNV)
2144                         sv_upgrade(sv, SVt_PVNV);
2145                     SvNOK_on(sv);
2146                     SvIOK_off(sv);
2147                     SvIOKp_on(sv);
2148                     SvNV_set(sv, -(NV)value);
2149                     SvIV_set(sv, IV_MIN);
2150                 }
2151             }
2152         }
2153         /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2154            will be in the previous block to set the IV slot, and the next
2155            block to set the NV slot.  So no else here.  */
2156
2157         if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2158             != IS_NUMBER_IN_UV) {
2159             /* It wasn't an (integer that doesn't overflow the UV). */
2160             S_sv_setnv(aTHX_ sv, numtype);
2161
2162             if (! numtype && ckWARN(WARN_NUMERIC))
2163                 not_a_number(sv);
2164
2165             DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" NVgf ")\n",
2166                                   PTR2UV(sv), SvNVX(sv)));
2167
2168 #ifdef NV_PRESERVES_UV
2169             SvNOKp_on(sv);
2170             if (numtype)
2171                 SvNOK_on(sv);
2172             goto got_nv;        /* Fill IV/UV slot and set IOKp, maybe IOK */
2173 #else /* NV_PRESERVES_UV */
2174             if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2175                 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2176                 /* The IV/UV slot will have been set from value returned by
2177                    grok_number above.  The NV slot has just been set using
2178                    Atof.  */
2179                 SvNOK_on(sv);
2180                 assert (SvIOKp(sv));
2181             } else {
2182                 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2183                     U_V(Perl_fabs(SvNVX(sv)))) {
2184                     /* Small enough to preserve all bits. */
2185                     (void)SvIOKp_on(sv);
2186                     SvNOK_on(sv);
2187                     SvIV_set(sv, I_V(SvNVX(sv)));
2188                     if ((NV)(SvIVX(sv)) == SvNVX(sv))
2189                         SvIOK_on(sv);
2190                     /* There had been runtime checking for
2191                        "U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX" here to ensure
2192                        that this NV is in the preserved range, but this should
2193                        be always true if the following assertion is true: */
2194                     STATIC_ASSERT_STMT(((UV)1 << NV_PRESERVES_UV_BITS) <=
2195                                        (UV)IV_MAX);
2196                 } else {
2197                     /* IN_UV NOT_INT
2198                          0      0       already failed to read UV.
2199                          0      1       already failed to read UV.
2200                          1      0       you won't get here in this case. IV/UV
2201                                         slot set, public IOK, Atof() unneeded.
2202                          1      1       already read UV.
2203                        so there's no point in sv_2iuv_non_preserve() attempting
2204                        to use atol, strtol, strtoul etc.  */
2205 #  ifdef DEBUGGING
2206                     sv_2iuv_non_preserve (sv, numtype);
2207 #  else
2208                     sv_2iuv_non_preserve (sv);
2209 #  endif
2210                 }
2211             }
2212         /* It might be more code efficient to go through the entire logic above
2213            and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2214            gets complex and potentially buggy, so more programmer efficient
2215            to do it this way, by turning off the public flags:  */
2216         if (!numtype)
2217             SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2218 #endif /* NV_PRESERVES_UV */
2219         }
2220     }
2221     else {
2222         if (isGV_with_GP(sv))
2223             return glob_2number(MUTABLE_GV(sv));
2224
2225         if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2226                 report_uninit(sv);
2227         if (SvTYPE(sv) < SVt_IV)
2228             /* Typically the caller expects that sv_any is not NULL now.  */
2229             sv_upgrade(sv, SVt_IV);
2230         /* Return 0 from the caller.  */
2231         return TRUE;
2232     }
2233     return FALSE;
2234 }
2235
2236 /*
2237 =for apidoc sv_2iv_flags
2238
2239 Return the integer value of an SV, doing any necessary string
2240 conversion.  If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2241 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2242
2243 =cut
2244 */
2245
2246 IV
2247 Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
2248 {
2249     PERL_ARGS_ASSERT_SV_2IV_FLAGS;
2250
2251     assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2252          && SvTYPE(sv) != SVt_PVFM);
2253
2254     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2255         mg_get(sv);
2256
2257     if (SvROK(sv)) {
2258         if (SvAMAGIC(sv)) {
2259             SV * tmpstr;
2260             if (flags & SV_SKIP_OVERLOAD)
2261                 return 0;
2262             tmpstr = AMG_CALLunary(sv, numer_amg);
2263             if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2264                 return SvIV(tmpstr);
2265             }
2266         }
2267         return PTR2IV(SvRV(sv));
2268     }
2269
2270     if (SvVALID(sv) || isREGEXP(sv)) {
2271         /* FBMs use the space for SvIVX and SvNVX for other purposes, so
2272            must not let them cache IVs.
2273            In practice they are extremely unlikely to actually get anywhere
2274            accessible by user Perl code - the only way that I'm aware of is when
2275            a constant subroutine which is used as the second argument to index.
2276
2277            Regexps have no SvIVX and SvNVX fields.
2278         */
2279         assert(SvPOKp(sv));
2280         {
2281             UV value;
2282             const char * const ptr =
2283                 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2284             const int numtype
2285                 = grok_number(ptr, SvCUR(sv), &value);
2286
2287             if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2288                 == IS_NUMBER_IN_UV) {
2289                 /* It's definitely an integer */
2290                 if (numtype & IS_NUMBER_NEG) {
2291                     if (value < (UV)IV_MIN)
2292                         return -(IV)value;
2293                 } else {
2294                     if (value < (UV)IV_MAX)
2295                         return (IV)value;
2296                 }
2297             }
2298
2299             /* Quite wrong but no good choices. */
2300             if ((numtype & IS_NUMBER_INFINITY)) {
2301                 return (numtype & IS_NUMBER_NEG) ? IV_MIN : IV_MAX;
2302             } else if ((numtype & IS_NUMBER_NAN)) {
2303                 return 0; /* So wrong. */
2304             }
2305
2306             if (!numtype) {
2307                 if (ckWARN(WARN_NUMERIC))
2308                     not_a_number(sv);
2309             }
2310             return I_V(Atof(ptr));
2311         }
2312     }
2313
2314     if (SvTHINKFIRST(sv)) {
2315         if (SvREADONLY(sv) && !SvOK(sv)) {
2316             if (ckWARN(WARN_UNINITIALIZED))
2317                 report_uninit(sv);
2318             return 0;
2319         }
2320     }
2321
2322     if (!SvIOKp(sv)) {
2323         if (S_sv_2iuv_common(aTHX_ sv))
2324             return 0;
2325     }
2326
2327     DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" IVdf ")\n",
2328         PTR2UV(sv),SvIVX(sv)));
2329     return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2330 }
2331
2332 /*
2333 =for apidoc sv_2uv_flags
2334
2335 Return the unsigned integer value of an SV, doing any necessary string
2336 conversion.  If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2337 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2338
2339 =for apidoc Amnh||SV_GMAGIC
2340
2341 =cut
2342 */
2343
2344 UV
2345 Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
2346 {
2347     PERL_ARGS_ASSERT_SV_2UV_FLAGS;
2348
2349     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2350         mg_get(sv);
2351
2352     if (SvROK(sv)) {
2353         if (SvAMAGIC(sv)) {
2354             SV *tmpstr;
2355             if (flags & SV_SKIP_OVERLOAD)
2356                 return 0;
2357             tmpstr = AMG_CALLunary(sv, numer_amg);
2358             if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2359                 return SvUV(tmpstr);
2360             }
2361         }
2362         return PTR2UV(SvRV(sv));
2363     }
2364
2365     if (SvVALID(sv) || isREGEXP(sv)) {
2366         /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2367            the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2368            Regexps have no SvIVX and SvNVX fields. */
2369         assert(SvPOKp(sv));
2370         {
2371             UV value;
2372             const char * const ptr =
2373                 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2374             const int numtype
2375                 = grok_number(ptr, SvCUR(sv), &value);
2376
2377             if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2378                 == IS_NUMBER_IN_UV) {
2379                 /* It's definitely an integer */
2380                 if (!(numtype & IS_NUMBER_NEG))
2381                     return value;
2382             }
2383
2384             /* Quite wrong but no good choices. */
2385             if ((numtype & IS_NUMBER_INFINITY)) {
2386                 return UV_MAX; /* So wrong. */
2387             } else if ((numtype & IS_NUMBER_NAN)) {
2388                 return 0; /* So wrong. */
2389             }
2390
2391             if (!numtype) {
2392                 if (ckWARN(WARN_NUMERIC))
2393                     not_a_number(sv);
2394             }
2395             return U_V(Atof(ptr));
2396         }
2397     }
2398
2399     if (SvTHINKFIRST(sv)) {
2400         if (SvREADONLY(sv) && !SvOK(sv)) {
2401             if (ckWARN(WARN_UNINITIALIZED))
2402                 report_uninit(sv);
2403             return 0;
2404         }
2405     }
2406
2407     if (!SvIOKp(sv)) {
2408         if (S_sv_2iuv_common(aTHX_ sv))
2409             return 0;
2410     }
2411
2412     DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2uv(%" UVuf ")\n",
2413                           PTR2UV(sv),SvUVX(sv)));
2414     return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2415 }
2416
2417 /*
2418 =for apidoc sv_2nv_flags
2419
2420 Return the num value of an SV, doing any necessary string or integer
2421 conversion.  If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2422 Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
2423
2424 =cut
2425 */
2426
2427 NV
2428 Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
2429 {
2430     PERL_ARGS_ASSERT_SV_2NV_FLAGS;
2431
2432     assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2433          && SvTYPE(sv) != SVt_PVFM);
2434     if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
2435         /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2436            the same flag bit as SVf_IVisUV, so must not let them cache NVs.
2437            Regexps have no SvIVX and SvNVX fields.  */
2438         const char *ptr;
2439         if (flags & SV_GMAGIC)
2440             mg_get(sv);
2441         if (SvNOKp(sv))
2442             return SvNVX(sv);
2443         if (SvPOKp(sv) && !SvIOKp(sv)) {
2444             ptr = SvPVX_const(sv);
2445             if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
2446                 !grok_number(ptr, SvCUR(sv), NULL))
2447                 not_a_number(sv);
2448             return Atof(ptr);
2449         }
2450         if (SvIOKp(sv)) {
2451             if (SvIsUV(sv))
2452                 return (NV)SvUVX(sv);
2453             else
2454                 return (NV)SvIVX(sv);
2455         }
2456         if (SvROK(sv)) {
2457             goto return_rok;
2458         }
2459         assert(SvTYPE(sv) >= SVt_PVMG);
2460         /* This falls through to the report_uninit near the end of the
2461            function. */
2462     } else if (SvTHINKFIRST(sv)) {
2463         if (SvROK(sv)) {
2464         return_rok:
2465             if (SvAMAGIC(sv)) {
2466                 SV *tmpstr;
2467                 if (flags & SV_SKIP_OVERLOAD)
2468                     return 0;
2469                 tmpstr = AMG_CALLunary(sv, numer_amg);
2470                 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2471                     return SvNV(tmpstr);
2472                 }
2473             }
2474             return PTR2NV(SvRV(sv));
2475         }
2476         if (SvREADONLY(sv) && !SvOK(sv)) {
2477             if (ckWARN(WARN_UNINITIALIZED))
2478                 report_uninit(sv);
2479             return 0.0;
2480         }
2481     }
2482     if (SvTYPE(sv) < SVt_NV) {
2483         /* The logic to use SVt_PVNV if necessary is in sv_upgrade.  */
2484         sv_upgrade(sv, SVt_NV);
2485         CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2486         DEBUG_c({
2487             DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2488             STORE_LC_NUMERIC_SET_STANDARD();
2489             PerlIO_printf(Perl_debug_log,
2490                           "0x%" UVxf " num(%" NVgf ")\n",
2491                           PTR2UV(sv), SvNVX(sv));
2492             RESTORE_LC_NUMERIC();
2493         });
2494         CLANG_DIAG_RESTORE_STMT;
2495
2496     }
2497     else if (SvTYPE(sv) < SVt_PVNV)
2498         sv_upgrade(sv, SVt_PVNV);
2499     if (SvNOKp(sv)) {
2500         return SvNVX(sv);
2501     }
2502     if (SvIOKp(sv)) {
2503         SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2504 #ifdef NV_PRESERVES_UV
2505         if (SvIOK(sv))
2506             SvNOK_on(sv);
2507         else
2508             SvNOKp_on(sv);
2509 #else
2510         /* Only set the public NV OK flag if this NV preserves the IV  */
2511         /* Check it's not 0xFFFFFFFFFFFFFFFF */
2512         if (SvIOK(sv) &&
2513             SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2514                        : (SvIVX(sv) == I_V(SvNVX(sv))))
2515             SvNOK_on(sv);
2516         else
2517             SvNOKp_on(sv);
2518 #endif
2519     }
2520     else if (SvPOKp(sv)) {
2521         UV value;
2522         const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2523         if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
2524             not_a_number(sv);
2525 #ifdef NV_PRESERVES_UV
2526         if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2527             == IS_NUMBER_IN_UV) {
2528             /* It's definitely an integer */
2529             SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2530         } else {
2531             S_sv_setnv(aTHX_ sv, numtype);
2532         }
2533         if (numtype)
2534             SvNOK_on(sv);
2535         else
2536             SvNOKp_on(sv);
2537 #else
2538         SvNV_set(sv, Atof(SvPVX_const(sv)));
2539         /* Only set the public NV OK flag if this NV preserves the value in
2540            the PV at least as well as an IV/UV would.
2541            Not sure how to do this 100% reliably. */
2542         /* if that shift count is out of range then Configure's test is
2543            wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2544            UV_BITS */
2545         if (((UV)1 << NV_PRESERVES_UV_BITS) > U_V(Perl_fabs(SvNVX(sv)))) {
2546             SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2547         } else if (!(numtype & IS_NUMBER_IN_UV)) {
2548             /* Can't use strtol etc to convert this string, so don't try.
2549                sv_2iv and sv_2uv will use the NV to convert, not the PV.  */
2550             SvNOK_on(sv);
2551         } else {
2552             /* value has been set.  It may not be precise.  */
2553             if ((numtype & IS_NUMBER_NEG) && (value >= (UV)IV_MIN)) {
2554                 /* 2s complement assumption for (UV)IV_MIN  */
2555                 SvNOK_on(sv); /* Integer is too negative.  */
2556             } else {
2557                 SvNOKp_on(sv);
2558                 SvIOKp_on(sv);
2559
2560                 if (numtype & IS_NUMBER_NEG) {
2561                     /* -IV_MIN is undefined, but we should never reach
2562                      * this point with both IS_NUMBER_NEG and value ==
2563                      * (UV)IV_MIN */
2564                     assert(value != (UV)IV_MIN);
2565                     SvIV_set(sv, -(IV)value);
2566                 } else if (value <= (UV)IV_MAX) {
2567                     SvIV_set(sv, (IV)value);
2568                 } else {
2569                     SvUV_set(sv, value);
2570                     SvIsUV_on(sv);
2571                 }
2572
2573                 if (numtype & IS_NUMBER_NOT_INT) {
2574                     /* I believe that even if the original PV had decimals,
2575                        they are lost beyond the limit of the FP precision.
2576                        However, neither is canonical, so both only get p
2577                        flags.  NWC, 2000/11/25 */
2578                     /* Both already have p flags, so do nothing */
2579                 } else {
2580                     const NV nv = SvNVX(sv);
2581                     /* XXX should this spot have NAN_COMPARE_BROKEN, too? */
2582                     if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2583                         if (SvIVX(sv) == I_V(nv)) {
2584                             SvNOK_on(sv);
2585                         } else {
2586                             /* It had no "." so it must be integer.  */
2587                         }
2588                         SvIOK_on(sv);
2589                     } else {
2590                         /* between IV_MAX and NV(UV_MAX).
2591                            Could be slightly > UV_MAX */
2592
2593                         if (numtype & IS_NUMBER_NOT_INT) {
2594                             /* UV and NV both imprecise.  */
2595                         } else {
2596                             const UV nv_as_uv = U_V(nv);
2597
2598                             if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2599                                 SvNOK_on(sv);
2600                             }
2601                             SvIOK_on(sv);
2602                         }
2603                     }
2604                 }
2605             }
2606         }
2607         /* It might be more code efficient to go through the entire logic above
2608            and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2609            gets complex and potentially buggy, so more programmer efficient
2610            to do it this way, by turning off the public flags:  */
2611         if (!numtype)
2612             SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2613 #endif /* NV_PRESERVES_UV */
2614     }
2615     else {
2616         if (isGV_with_GP(sv)) {
2617             glob_2number(MUTABLE_GV(sv));
2618             return 0.0;
2619         }
2620
2621         if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2622             report_uninit(sv);
2623         assert (SvTYPE(sv) >= SVt_NV);
2624         /* Typically the caller expects that sv_any is not NULL now.  */
2625         /* XXX Ilya implies that this is a bug in callers that assume this
2626            and ideally should be fixed.  */
2627         return 0.0;
2628     }
2629     CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2630     DEBUG_c({
2631         DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2632         STORE_LC_NUMERIC_SET_STANDARD();
2633         PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2nv(%" NVgf ")\n",
2634                       PTR2UV(sv), SvNVX(sv));
2635         RESTORE_LC_NUMERIC();
2636     });
2637     CLANG_DIAG_RESTORE_STMT;
2638     return SvNVX(sv);
2639 }
2640
2641 /*
2642 =for apidoc sv_2num
2643
2644 Return an SV with the numeric value of the source SV, doing any necessary
2645 reference or overload conversion.  The caller is expected to have handled
2646 get-magic already.
2647
2648 =cut
2649 */
2650
2651 SV *
2652 Perl_sv_2num(pTHX_ SV *const sv)
2653 {
2654     PERL_ARGS_ASSERT_SV_2NUM;
2655
2656     if (!SvROK(sv))
2657         return sv;
2658     if (SvAMAGIC(sv)) {
2659         SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
2660         TAINT_IF(tmpsv && SvTAINTED(tmpsv));
2661         if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2662             return sv_2num(tmpsv);
2663     }
2664     return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2665 }
2666
2667 /* int2str_table: lookup table containing string representations of all
2668  * two digit numbers. For example, int2str_table.arr[0] is "00" and
2669  * int2str_table.arr[12*2] is "12".
2670  *
2671  * We are going to read two bytes at a time, so we have to ensure that
2672  * the array is aligned to a 2 byte boundary. That's why it was made a
2673  * union with a dummy U16 member. */
2674 static const union {
2675     char arr[200];
2676     U16 dummy;
2677 } int2str_table = {{
2678     '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6',
2679     '0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
2680     '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',
2681     '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7',
2682     '2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
2683     '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1',
2684     '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8',
2685     '4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5',
2686     '5', '6', '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2',
2687     '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
2688     '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6',
2689     '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3',
2690     '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0',
2691     '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7',
2692     '9', '8', '9', '9'
2693 }};
2694
2695 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2696  * UV as a string towards the end of buf, and return pointers to start and
2697  * end of it.
2698  *
2699  * We assume that buf is at least TYPE_CHARS(UV) long.
2700  */
2701
2702 PERL_STATIC_INLINE char *
2703 S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
2704 {
2705     char *ptr = buf + TYPE_CHARS(UV);
2706     char * const ebuf = ptr;
2707     int sign;
2708     U16 *word_ptr, *word_table;
2709
2710     PERL_ARGS_ASSERT_UIV_2BUF;
2711
2712     /* ptr has to be properly aligned, because we will cast it to U16* */
2713     assert(PTR2nat(ptr) % 2 == 0);
2714     /* we are going to read/write two bytes at a time */
2715     word_ptr = (U16*)ptr;
2716     word_table = (U16*)int2str_table.arr;
2717
2718     if (UNLIKELY(is_uv))
2719         sign = 0;
2720     else if (iv >= 0) {
2721         uv = iv;
2722         sign = 0;
2723     } else {
2724         /* Using 0- here to silence bogus warning from MS VC */
2725         uv = (UV) (0 - (UV) iv);
2726         sign = 1;
2727     }
2728
2729     while (uv > 99) {
2730         *--word_ptr = word_table[uv % 100];
2731         uv /= 100;
2732     }
2733     ptr = (char*)word_ptr;
2734
2735     if (uv < 10)
2736         *--ptr = (char)uv + '0';
2737     else {
2738         *--word_ptr = word_table[uv];
2739         ptr = (char*)word_ptr;
2740     }
2741
2742     if (sign)
2743         *--ptr = '-';
2744
2745     *peob = ebuf;
2746     return ptr;
2747 }
2748
2749 /* Helper for sv_2pv_flags and sv_vcatpvfn_flags.  If the NV is an
2750  * infinity or a not-a-number, writes the appropriate strings to the
2751  * buffer, including a zero byte.  On success returns the written length,
2752  * excluding the zero byte, on failure (not an infinity, not a nan)
2753  * returns zero, assert-fails on maxlen being too short.
2754  *
2755  * XXX for "Inf", "-Inf", and "NaN", we could have three read-only
2756  * shared string constants we point to, instead of generating a new
2757  * string for each instance. */
2758 STATIC size_t
2759 S_infnan_2pv(NV nv, char* buffer, size_t maxlen, char plus) {
2760     char* s = buffer;
2761     assert(maxlen >= 4);
2762     if (Perl_isinf(nv)) {
2763         if (nv < 0) {
2764             if (maxlen < 5) /* "-Inf\0"  */
2765                 return 0;
2766             *s++ = '-';
2767         } else if (plus) {
2768             *s++ = '+';
2769         }
2770         *s++ = 'I';
2771         *s++ = 'n';
2772         *s++ = 'f';
2773     }
2774     else if (Perl_isnan(nv)) {
2775         *s++ = 'N';
2776         *s++ = 'a';
2777         *s++ = 'N';
2778         /* XXX optionally output the payload mantissa bits as
2779          * "(unsigned)" (to match the nan("...") C99 function,
2780          * or maybe as "(0xhhh...)"  would make more sense...
2781          * provide a format string so that the user can decide?
2782          * NOTE: would affect the maxlen and assert() logic.*/
2783     }
2784     else {
2785       return 0;
2786     }
2787     assert((s == buffer + 3) || (s == buffer + 4));
2788     *s = 0;
2789     return s - buffer;
2790 }
2791
2792 /*
2793 =for apidoc      sv_2pv
2794 =for apidoc_item sv_2pv_flags
2795
2796 These implement the various forms of the L<perlapi/C<SvPV>> macros.
2797 The macros are the preferred interface.
2798
2799 These return a pointer to the string value of an SV (coercing it to a string if
2800 necessary), and set C<*lp> to its length in bytes.
2801
2802 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
2803 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
2804 C<SV_GMAGIC>.
2805
2806 =for apidoc Amnh||SV_GMAGIC
2807
2808 =cut
2809 */
2810
2811 char *
2812 Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
2813 {
2814     char *s;
2815
2816     PERL_ARGS_ASSERT_SV_2PV_FLAGS;
2817
2818     assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2819          && SvTYPE(sv) != SVt_PVFM);
2820     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2821         mg_get(sv);
2822     if (SvROK(sv)) {
2823         if (SvAMAGIC(sv)) {
2824             SV *tmpstr;
2825             if (flags & SV_SKIP_OVERLOAD)
2826                 return NULL;
2827             tmpstr = AMG_CALLunary(sv, string_amg);
2828             TAINT_IF(tmpstr && SvTAINTED(tmpstr));
2829             if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2830                 /* Unwrap this:  */
2831                 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2832                  */
2833
2834                 char *pv;
2835                 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2836                     if (flags & SV_CONST_RETURN) {
2837                         pv = (char *) SvPVX_const(tmpstr);
2838                     } else {
2839                         pv = (flags & SV_MUTABLE_RETURN)
2840                             ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2841                     }
2842                     if (lp)
2843                         *lp = SvCUR(tmpstr);
2844                 } else {
2845                     pv = sv_2pv_flags(tmpstr, lp, flags);
2846                 }
2847                 if (SvUTF8(tmpstr))
2848                     SvUTF8_on(sv);
2849                 else
2850                     SvUTF8_off(sv);
2851                 return pv;
2852             }
2853         }
2854         {
2855             STRLEN len;
2856             char *retval;
2857             char *buffer;
2858             SV *const referent = SvRV(sv);
2859
2860             if (!referent) {
2861                 len = 7;
2862                 retval = buffer = savepvn("NULLREF", len);
2863             } else if (SvTYPE(referent) == SVt_REGEXP &&
2864                        (!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
2865                         amagic_is_enabled(string_amg))) {
2866                 REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
2867
2868                 assert(re);
2869
2870                 /* If the regex is UTF-8 we want the containing scalar to
2871                    have an UTF-8 flag too */
2872                 if (RX_UTF8(re))
2873                     SvUTF8_on(sv);
2874                 else
2875                     SvUTF8_off(sv);
2876
2877                 if (lp)
2878                     *lp = RX_WRAPLEN(re);
2879
2880                 return RX_WRAPPED(re);
2881             } else {
2882                 const char *const typestring = sv_reftype(referent, 0);
2883                 const STRLEN typelen = strlen(typestring);
2884                 UV addr = PTR2UV(referent);
2885                 const char *stashname = NULL;
2886                 STRLEN stashnamelen = 0; /* hush, gcc */
2887                 const char *buffer_end;
2888
2889                 if (SvOBJECT(referent)) {
2890                     const HEK *const name = HvNAME_HEK(SvSTASH(referent));
2891
2892                     if (name) {
2893                         stashname = HEK_KEY(name);
2894                         stashnamelen = HEK_LEN(name);
2895
2896                         if (HEK_UTF8(name)) {
2897                             SvUTF8_on(sv);
2898                         } else {
2899                             SvUTF8_off(sv);
2900                         }
2901                     } else {
2902                         stashname = "__ANON__";
2903                         stashnamelen = 8;
2904                     }
2905                     len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
2906                         + 2 * sizeof(UV) + 2 /* )\0 */;
2907                 } else {
2908                     len = typelen + 3 /* (0x */
2909                         + 2 * sizeof(UV) + 2 /* )\0 */;
2910                 }
2911
2912                 Newx(buffer, len, char);
2913                 buffer_end = retval = buffer + len;
2914
2915                 /* Working backwards  */
2916                 *--retval = '\0';
2917                 *--retval = ')';
2918                 do {
2919                     *--retval = PL_hexdigit[addr & 15];
2920                 } while (addr >>= 4);
2921                 *--retval = 'x';
2922                 *--retval = '0';
2923                 *--retval = '(';
2924
2925                 retval -= typelen;
2926                 memcpy(retval, typestring, typelen);
2927
2928                 if (stashname) {
2929                     *--retval = '=';
2930                     retval -= stashnamelen;
2931                     memcpy(retval, stashname, stashnamelen);
2932                 }
2933                 /* retval may not necessarily have reached the start of the
2934                    buffer here.  */
2935                 assert (retval >= buffer);
2936
2937                 len = buffer_end - retval - 1; /* -1 for that \0  */
2938             }
2939             if (lp)
2940                 *lp = len;
2941             SAVEFREEPV(buffer);
2942             return retval;
2943         }
2944     }
2945
2946     if (SvPOKp(sv)) {
2947         if (lp)
2948             *lp = SvCUR(sv);
2949         if (flags & SV_MUTABLE_RETURN)
2950             return SvPVX_mutable(sv);
2951         if (flags & SV_CONST_RETURN)
2952             return (char *)SvPVX_const(sv);
2953         return SvPVX(sv);
2954     }
2955
2956     if (SvIOK(sv)) {
2957         /* I'm assuming that if both IV and NV are equally valid then
2958            converting the IV is going to be more efficient */
2959         const U32 isUIOK = SvIsUV(sv);
2960         /* The purpose of this union is to ensure that arr is aligned on
2961            a 2 byte boundary, because that is what uiv_2buf() requires */
2962         union {
2963             char arr[TYPE_CHARS(UV)];
2964             U16 dummy;
2965         } buf;
2966         char *ebuf, *ptr;
2967         STRLEN len;
2968
2969         if (SvTYPE(sv) < SVt_PVIV)
2970             sv_upgrade(sv, SVt_PVIV);
2971         ptr = uiv_2buf(buf.arr, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
2972         len = ebuf - ptr;
2973         /* inlined from sv_setpvn */
2974         s = SvGROW_mutable(sv, len + 1);
2975         Move(ptr, s, len, char);
2976         s += len;
2977         *s = '\0';
2978         /* We used to call SvPOK_on(). Whilst this is fine for (most) Perl code,
2979            it means that after this stringification is cached, there is no way
2980            to distinguish between values originally assigned as $a = 42; and
2981            $a = "42"; (or results of string operators vs numeric operators)
2982            where the value has subsequently been used in the other sense
2983            and had a value cached.
2984            This (somewhat) hack means that we retain the cached stringification,
2985            but don't set SVf_POK. Hence if a value is SVf_IOK|SVf_POK then it
2986            originated as "42", whereas if it's SVf_IOK then it originated as 42.
2987            (ignore SVp_IOK and SVp_POK)
2988            The SvPV macros are now updated to recognise this specific case
2989            (and that there isn't overloading or magic that could alter the
2990            cached value) and so return the cached value immediately without
2991            re-entering this function, getting back here to this block of code,
2992            and repeating the same conversion. */
2993         SvPOKp_on(sv);
2994     }
2995     else if (SvNOK(sv)) {
2996         if (SvTYPE(sv) < SVt_PVNV)
2997             sv_upgrade(sv, SVt_PVNV);
2998         if (SvNVX(sv) == 0.0
2999 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3000             && !Perl_isnan(SvNVX(sv))
3001 #endif
3002         ) {
3003             s = SvGROW_mutable(sv, 2);
3004             *s++ = '0';
3005             *s = '\0';
3006         } else {
3007             STRLEN len;
3008             STRLEN size = 5; /* "-Inf\0" */
3009
3010             s = SvGROW_mutable(sv, size);
3011             len = S_infnan_2pv(SvNVX(sv), s, size, 0);
3012             if (len > 0) {
3013                 s += len;
3014                 SvPOKp_on(sv);
3015             }
3016             else {
3017                 /* some Xenix systems wipe out errno here */
3018                 dSAVE_ERRNO;
3019
3020                 size =
3021                     1 + /* sign */
3022                     1 + /* "." */
3023                     NV_DIG +
3024                     1 + /* "e" */
3025                     1 + /* sign */
3026                     5 + /* exponent digits */
3027                     1 + /* \0 */
3028                     2; /* paranoia */
3029
3030                 s = SvGROW_mutable(sv, size);
3031 #ifndef USE_LOCALE_NUMERIC
3032                 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3033
3034                 SvPOKp_on(sv);
3035 #else
3036                 {
3037                     bool local_radix;
3038                     DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3039                     STORE_LC_NUMERIC_SET_TO_NEEDED();
3040
3041                     local_radix = NOT_IN_NUMERIC_STANDARD_;
3042                     if (local_radix && SvCUR(PL_numeric_radix_sv) > 1) {
3043                         size += SvCUR(PL_numeric_radix_sv) - 1;
3044                         s = SvGROW_mutable(sv, size);
3045                     }
3046
3047                     SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3048
3049                     /* If the radix character is UTF-8, and actually is in the
3050                      * output, turn on the UTF-8 flag for the scalar */
3051                     if (   local_radix
3052                         && SvUTF8(PL_numeric_radix_sv)
3053                         && instr(s, SvPVX_const(PL_numeric_radix_sv)))
3054                     {
3055                         SvUTF8_on(sv);
3056                     }
3057
3058                     RESTORE_LC_NUMERIC();
3059                 }
3060
3061                 /* We don't call SvPOK_on(), because it may come to
3062                  * pass that the locale changes so that the
3063                  * stringification we just did is no longer correct.  We
3064                  * will have to re-stringify every time it is needed */
3065 #endif
3066                 RESTORE_ERRNO;
3067             }
3068             while (*s) s++;
3069         }
3070     }
3071     else if (isGV_with_GP(sv)) {
3072         GV *const gv = MUTABLE_GV(sv);
3073         SV *const buffer = sv_newmortal();
3074
3075         gv_efullname3(buffer, gv, "*");
3076
3077         assert(SvPOK(buffer));
3078         if (SvUTF8(buffer))
3079             SvUTF8_on(sv);
3080         else
3081             SvUTF8_off(sv);
3082         if (lp)
3083             *lp = SvCUR(buffer);
3084         return SvPVX(buffer);
3085     }
3086     else {
3087         if (lp)
3088             *lp = 0;
3089         if (flags & SV_UNDEF_RETURNS_NULL)
3090             return NULL;
3091         if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
3092             report_uninit(sv);
3093         /* Typically the caller expects that sv_any is not NULL now.  */
3094         if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
3095             sv_upgrade(sv, SVt_PV);
3096         return (char *)"";
3097     }
3098
3099     {
3100         const STRLEN len = s - SvPVX_const(sv);
3101         if (lp)
3102             *lp = len;
3103         SvCUR_set(sv, len);
3104     }
3105     DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
3106                           PTR2UV(sv),SvPVX_const(sv)));
3107     if (flags & SV_CONST_RETURN)
3108         return (char *)SvPVX_const(sv);
3109     if (flags & SV_MUTABLE_RETURN)
3110         return SvPVX_mutable(sv);
3111     return SvPVX(sv);
3112 }
3113
3114 /*
3115 =for apidoc sv_copypv
3116 =for apidoc_item sv_copypv_flags
3117 =for apidoc_item sv_copypv_nomg
3118
3119 These copy a stringified representation of the source SV into the
3120 destination SV.  They automatically perform coercion of numeric values into
3121 strings.  Guaranteed to preserve the C<UTF8> flag even from overloaded objects.
3122 Similar in nature to C<sv_2pv[_flags]> but they operate directly on an SV
3123 instead of just the string.  Mostly they use L</C<sv_2pv_flags>> to
3124 do the work, except when that would lose the UTF-8'ness of the PV.
3125
3126 The three forms differ only in whether or not they perform 'get magic' on
3127 C<sv>.  C<sv_copypv_nomg> skips 'get magic'; C<sv_copypv> performs it; and
3128 C<sv_copypv_flags> either performs it (if the C<SV_GMAGIC> bit is set in
3129 C<flags>) or doesn't (if that bit is cleared).
3130
3131 =cut
3132 */
3133
3134 void
3135 Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
3136 {
3137     STRLEN len;
3138     const char *s;
3139
3140     PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
3141
3142     s = SvPV_flags_const(ssv,len,(flags & SV_GMAGIC));
3143     sv_setpvn(dsv,s,len);
3144     if (SvUTF8(ssv))
3145         SvUTF8_on(dsv);
3146     else
3147         SvUTF8_off(dsv);
3148 }
3149
3150 /*
3151 =for apidoc      sv_2pvbyte
3152 =for apidoc_item sv_2pvbyte_flags
3153
3154 These implement the various forms of the L<perlapi/C<SvPVbyte>> macros.
3155 The macros are the preferred interface.
3156
3157 These return a pointer to the byte-encoded representation of the SV, and set
3158 C<*lp> to its length.  If the SV is marked as being encoded as UTF-8, it will
3159 be downgraded, if possible, to a byte string.  If the SV cannot be downgraded,
3160 they croak.
3161
3162 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
3163 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
3164 C<SV_GMAGIC>.
3165
3166 =for apidoc Amnh||SV_GMAGIC
3167
3168 =cut
3169 */
3170
3171 char *
3172 Perl_sv_2pvbyte_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3173 {
3174     PERL_ARGS_ASSERT_SV_2PVBYTE_FLAGS;
3175
3176     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3177         mg_get(sv);
3178     if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3179      || isGV_with_GP(sv) || SvROK(sv)) {
3180         SV *sv2 = sv_newmortal();
3181         sv_copypv_nomg(sv2,sv);
3182         sv = sv2;
3183     }
3184     sv_utf8_downgrade_nomg(sv,0);
3185     return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3186 }
3187
3188 /*
3189 =for apidoc      sv_2pvutf8
3190 =for apidoc_item sv_2pvutf8_flags
3191
3192 These implement the various forms of the L<perlapi/C<SvPVutf8>> macros.
3193 The macros are the preferred interface.
3194
3195 These return a pointer to the UTF-8-encoded representation of the SV, and set
3196 C<*lp> to its length in bytes.  They may cause the SV to be upgraded to UTF-8
3197 as a side-effect.
3198
3199 The forms differ in that plain C<sv_2pvutf8> always processes 'get' magic; and
3200 C<sv_2pvutf8_flags> processes 'get' magic if and only if C<flags> contains
3201 C<SV_GMAGIC>.
3202
3203 =cut
3204 */
3205
3206 char *
3207 Perl_sv_2pvutf8_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3208 {
3209     PERL_ARGS_ASSERT_SV_2PVUTF8_FLAGS;
3210
3211     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3212         mg_get(sv);
3213     if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3214      || isGV_with_GP(sv) || SvROK(sv)) {
3215         SV *sv2 = sv_newmortal();
3216         sv_copypv_nomg(sv2,sv);
3217         sv = sv2;
3218     }
3219     sv_utf8_upgrade_nomg(sv);
3220     return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3221 }
3222
3223
3224 /*
3225 =for apidoc sv_2bool
3226
3227 This macro is only used by C<sv_true()> or its macro equivalent, and only if
3228 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>.
3229 It calls C<sv_2bool_flags> with the C<SV_GMAGIC> flag.
3230
3231 =for apidoc sv_2bool_flags
3232
3233 This function is only used by C<sv_true()> and friends,  and only if
3234 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>.  If the flags
3235 contain C<SV_GMAGIC>, then it does an C<mg_get()> first.
3236
3237
3238 =cut
3239 */
3240
3241 bool
3242 Perl_sv_2bool_flags(pTHX_ SV *sv, I32 flags)
3243 {
3244     PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
3245
3246     restart:
3247     if(flags & SV_GMAGIC) SvGETMAGIC(sv);
3248
3249     if (!SvOK(sv))
3250         return 0;
3251     if (SvROK(sv)) {
3252         if (SvAMAGIC(sv)) {
3253             SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
3254             if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) {
3255                 bool svb;
3256                 sv = tmpsv;
3257                 if(SvGMAGICAL(sv)) {
3258                     flags = SV_GMAGIC;
3259                     goto restart; /* call sv_2bool */
3260                 }
3261                 /* expanded SvTRUE_common(sv, (flags = 0, goto restart)) */
3262                 else if(!SvOK(sv)) {
3263                     svb = 0;
3264                 }
3265                 else if(SvPOK(sv)) {
3266                     svb = SvPVXtrue(sv);
3267                 }
3268                 else if((SvFLAGS(sv) & (SVf_IOK|SVf_NOK))) {
3269                     svb = (SvIOK(sv) && SvIVX(sv) != 0)
3270                         || (SvNOK(sv) && SvNVX(sv) != 0.0);
3271                 }
3272                 else {
3273                     flags = 0;
3274                     goto restart; /* call sv_2bool_nomg */
3275                 }
3276                 return cBOOL(svb);
3277             }
3278         }
3279         assert(SvRV(sv));
3280         return TRUE;
3281     }
3282     if (isREGEXP(sv))
3283         return
3284           RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
3285
3286     if (SvNOK(sv) && !SvPOK(sv))
3287         return SvNVX(sv) != 0.0;
3288
3289     return SvTRUE_common(sv, 0);
3290 }
3291
3292 /*
3293 =for apidoc sv_utf8_upgrade
3294 =for apidoc_item sv_utf8_upgrade_flags
3295 =for apidoc_item sv_utf8_upgrade_flags_grow
3296 =for apidoc_item sv_utf8_upgrade_nomg
3297
3298 These convert the PV of an SV to its UTF-8-encoded form.
3299 The SV is forced to string form if it is not already.
3300 They always set the C<SvUTF8> flag to avoid future validity checks even if the
3301 whole string is the same in UTF-8 as not.
3302 They return the number of bytes in the converted string
3303
3304 The forms differ in just two ways.  The main difference is whether or not they
3305 perform 'get magic' on C<sv>.  C<sv_utf8_upgrade_nomg> skips 'get magic';
3306 C<sv_utf8_upgrade> performs it; and C<sv_utf8_upgrade_flags> and
3307 C<sv_utf8_upgrade_flags_grow> either perform it (if the C<SV_GMAGIC> bit is set
3308 in C<flags>) or don't (if that bit is cleared).
3309
3310 The other difference is that C<sv_utf8_upgrade_flags_grow> has an additional
3311 parameter, C<extra>, which allows the caller to specify an amount of space to
3312 be reserved as spare beyond what is needed for the actual conversion.  This is
3313 used when the caller knows it will soon be needing yet more space, and it is
3314 more efficient to request space from the system in a single call.
3315 This form is otherwise identical to C<sv_utf8_upgrade_flags>.
3316
3317 These are not a general purpose byte encoding to Unicode interface: use the
3318 Encode extension for that.
3319
3320 The C<SV_FORCE_UTF8_UPGRADE> flag is now ignored.
3321
3322 =for apidoc Amnh||SV_GMAGIC|
3323 =for apidoc Amnh||SV_FORCE_UTF8_UPGRADE|
3324
3325 =cut
3326
3327 If the routine itself changes the string, it adds a trailing C<NUL>.  Such a
3328 C<NUL> isn't guaranteed due to having other routines do the work in some input
3329 cases, or if the input is already flagged as being in utf8.
3330
3331 */
3332
3333 STRLEN
3334 Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
3335 {
3336     PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
3337
3338     if (sv == &PL_sv_undef)
3339         return 0;
3340     if (!SvPOK_nog(sv)) {
3341         STRLEN len = 0;
3342         if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3343             (void) sv_2pv_flags(sv,&len, flags);
3344             if (SvUTF8(sv)) {
3345                 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3346                 return len;
3347             }
3348         } else {
3349             (void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
3350         }
3351     }
3352
3353     /* SVt_REGEXP's shouldn't be upgraded to UTF8 - they're already
3354      * compiled and individual nodes will remain non-utf8 even if the
3355      * stringified version of the pattern gets upgraded. Whether the
3356      * PVX of a REGEXP should be grown or we should just croak, I don't
3357      * know - DAPM */
3358     if (SvUTF8(sv) || isREGEXP(sv)) {
3359         if (extra) SvGROW(sv, SvCUR(sv) + extra);
3360         return SvCUR(sv);
3361     }
3362
3363     if (SvIsCOW(sv)) {
3364         S_sv_uncow(aTHX_ sv, 0);
3365     }
3366
3367     if (SvCUR(sv) == 0) {
3368         if (extra) SvGROW(sv, extra + 1); /* Make sure is room for a trailing
3369                                              byte */
3370     } else { /* Assume Latin-1/EBCDIC */
3371         /* This function could be much more efficient if we
3372          * had a FLAG in SVs to signal if there are any variant
3373          * chars in the PV.  Given that there isn't such a flag
3374          * make the loop as fast as possible. */
3375         U8 * s = (U8 *) SvPVX_const(sv);
3376         U8 *t = s;
3377
3378         if (is_utf8_invariant_string_loc(s, SvCUR(sv), (const U8 **) &t)) {
3379
3380             /* utf8 conversion not needed because all are invariants.  Mark
3381              * as UTF-8 even if no variant - saves scanning loop */
3382             SvUTF8_on(sv);
3383             if (extra) SvGROW(sv, SvCUR(sv) + extra);
3384             return SvCUR(sv);
3385         }
3386
3387         /* Here, there is at least one variant (t points to the first one), so
3388          * the string should be converted to utf8.  Everything from 's' to
3389          * 't - 1' will occupy only 1 byte each on output.
3390          *
3391          * Note that the incoming SV may not have a trailing '\0', as certain
3392          * code in pp_formline can send us partially built SVs.
3393          *
3394          * There are two main ways to convert.  One is to create a new string
3395          * and go through the input starting from the beginning, appending each
3396          * converted value onto the new string as we go along.  Going this
3397          * route, it's probably best to initially allocate enough space in the
3398          * string rather than possibly running out of space and having to
3399          * reallocate and then copy what we've done so far.  Since everything
3400          * from 's' to 't - 1' is invariant, the destination can be initialized
3401          * with these using a fast memory copy.  To be sure to allocate enough
3402          * space, one could use the worst case scenario, where every remaining
3403          * byte expands to two under UTF-8, or one could parse it and count
3404          * exactly how many do expand.
3405          *
3406          * The other way is to unconditionally parse the remainder of the
3407          * string to figure out exactly how big the expanded string will be,
3408          * growing if needed.  Then start at the end of the string and place
3409          * the character there at the end of the unfilled space in the expanded
3410          * one, working backwards until reaching 't'.
3411          *
3412          * The problem with assuming the worst case scenario is that for very
3413          * long strings, we could allocate much more memory than actually
3414          * needed, which can create performance problems.  If we have to parse
3415          * anyway, the second method is the winner as it may avoid an extra
3416          * copy.  The code used to use the first method under some
3417          * circumstances, but now that there is faster variant counting on
3418          * ASCII platforms, the second method is used exclusively, eliminating
3419          * some code that no longer has to be maintained. */
3420
3421         {
3422             /* Count the total number of variants there are.  We can start
3423              * just beyond the first one, which is known to be at 't' */
3424             const Size_t invariant_length = t - s;
3425             U8 * e = (U8 *) SvEND(sv);
3426
3427             /* The length of the left overs, plus 1. */
3428             const Size_t remaining_length_p1 = e - t;
3429
3430             /* We expand by 1 for the variant at 't' and one for each remaining
3431              * variant (we start looking at 't+1') */
3432             Size_t expansion = 1 + variant_under_utf8_count(t + 1, e);
3433
3434             /* +1 = trailing NUL */
3435             Size_t need = SvCUR(sv) + expansion + extra + 1;
3436             U8 * d;
3437
3438             /* Grow if needed */
3439             if (SvLEN(sv) < need) {
3440                 t = invariant_length + (U8*) SvGROW(sv, need);
3441                 e = t + remaining_length_p1;
3442             }
3443             SvCUR_set(sv, invariant_length + remaining_length_p1 + expansion);
3444
3445             /* Set the NUL at the end */
3446             d = (U8 *) SvEND(sv);
3447             *d-- = '\0';
3448
3449             /* Having decremented d, it points to the position to put the
3450              * very last byte of the expanded string.  Go backwards through
3451              * the string, copying and expanding as we go, stopping when we
3452              * get to the part that is invariant the rest of the way down */
3453
3454             e--;
3455             while (e >= t) {
3456                 if (NATIVE_BYTE_IS_INVARIANT(*e)) {
3457                     *d-- = *e;
3458                 } else {
3459                     *d-- = UTF8_EIGHT_BIT_LO(*e);
3460                     *d-- = UTF8_EIGHT_BIT_HI(*e);
3461                 }
3462                 e--;
3463             }
3464
3465             if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3466                 /* Update pos. We do it at the end rather than during
3467                  * the upgrade, to avoid slowing down the common case
3468                  * (upgrade without pos).
3469                  * pos can be stored as either bytes or characters.  Since
3470                  * this was previously a byte string we can just turn off
3471                  * the bytes flag. */
3472                 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3473                 if (mg) {
3474                     mg->mg_flags &= ~MGf_BYTES;
3475                 }
3476                 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3477                     magic_setutf8(sv,mg); /* clear UTF8 cache */
3478             }
3479         }
3480     }
3481
3482     SvUTF8_on(sv);
3483     return SvCUR(sv);
3484 }
3485
3486 /*
3487 =for apidoc sv_utf8_downgrade
3488 =for apidoc_item sv_utf8_downgrade_flags
3489 =for apidoc_item sv_utf8_downgrade_nomg
3490
3491 These attempt to convert the PV of an SV from characters to bytes.  If the PV
3492 contains a character that cannot fit in a byte, this conversion will fail; in
3493 this case, C<FALSE> is returned if C<fail_ok> is true; otherwise they croak.
3494
3495 They are not a general purpose Unicode to byte encoding interface:
3496 use the C<Encode> extension for that.
3497
3498 They differ only in that:
3499
3500 C<sv_utf8_downgrade> processes 'get' magic on C<sv>.
3501
3502 C<sv_utf8_downgrade_nomg> does not.
3503
3504 C<sv_utf8_downgrade_flags> has an additional C<flags> parameter in which you can specify
3505 C<SV_GMAGIC> to process 'get' magic, or leave it cleared to not process 'get' magic.
3506
3507 =cut
3508 */
3509
3510 bool
3511 Perl_sv_utf8_downgrade_flags(pTHX_ SV *const sv, const bool fail_ok, const U32 flags)
3512 {
3513     PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE_FLAGS;
3514
3515     if (SvPOKp(sv) && SvUTF8(sv)) {
3516         if (SvCUR(sv)) {
3517             U8 *s;
3518             STRLEN len;
3519             U32 mg_flags = flags & SV_GMAGIC;
3520
3521             if (SvIsCOW(sv)) {
3522                 S_sv_uncow(aTHX_ sv, 0);
3523             }
3524             if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3525                 /* update pos */
3526                 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3527                 if (mg && mg->mg_len > 0 && mg->mg_flags & MGf_BYTES) {
3528                         mg->mg_len = sv_pos_b2u_flags(sv, mg->mg_len,
3529                                                 mg_flags|SV_CONST_RETURN);
3530                         mg_flags = 0; /* sv_pos_b2u does get magic */
3531                 }
3532                 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3533                     magic_setutf8(sv,mg); /* clear UTF8 cache */
3534
3535             }
3536             s = (U8 *) SvPV_flags(sv, len, mg_flags);
3537
3538             if (!utf8_to_bytes(s, &len)) {
3539                 if (fail_ok)
3540                     return FALSE;
3541                 else {
3542                     if (PL_op)
3543                         Perl_croak(aTHX_ "Wide character in %s",
3544                                    OP_DESC(PL_op));
3545                     else
3546                         Perl_croak(aTHX_ "Wide character");
3547                 }
3548             }
3549             SvCUR_set(sv, len);
3550         }
3551     }
3552     SvUTF8_off(sv);
3553     return TRUE;
3554 }
3555
3556 /*
3557 =for apidoc sv_utf8_encode
3558
3559 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3560 flag off so that it looks like octets again.
3561
3562 =cut
3563 */
3564
3565 void
3566 Perl_sv_utf8_encode(pTHX_ SV *const sv)
3567 {
3568     PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
3569
3570     if (SvREADONLY(sv)) {
3571         sv_force_normal_flags(sv, 0);
3572     }
3573     (void) sv_utf8_upgrade(sv);
3574     SvUTF8_off(sv);
3575 }
3576
3577 /*
3578 =for apidoc sv_utf8_decode
3579
3580 If the PV of the SV is an octet sequence in Perl's extended UTF-8
3581 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3582 so that it looks like a character.  If the PV contains only single-byte
3583 characters, the C<SvUTF8> flag stays off.
3584 Scans PV for validity and returns FALSE if the PV is invalid UTF-8.
3585
3586 =cut
3587 */
3588
3589 bool
3590 Perl_sv_utf8_decode(pTHX_ SV *const sv)
3591 {
3592     PERL_ARGS_ASSERT_SV_UTF8_DECODE;
3593
3594     if (SvPOKp(sv)) {
3595         const U8 *start, *c, *first_variant;
3596
3597         /* The octets may have got themselves encoded - get them back as
3598          * bytes
3599          */
3600         if (!sv_utf8_downgrade(sv, TRUE))
3601             return FALSE;
3602
3603         /* it is actually just a matter of turning the utf8 flag on, but
3604          * we want to make sure everything inside is valid utf8 first.
3605          */
3606         c = start = (const U8 *) SvPVX_const(sv);
3607         if (! is_utf8_invariant_string_loc(c, SvCUR(sv), &first_variant)) {
3608             if (!is_utf8_string(first_variant, SvCUR(sv) - (first_variant -c)))
3609                 return FALSE;
3610             SvUTF8_on(sv);
3611         }
3612         if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3613             /* XXX Is this dead code?  XS_utf8_decode calls SvSETMAGIC
3614                    after this, clearing pos.  Does anything on CPAN
3615                    need this? */
3616             /* adjust pos to the start of a UTF8 char sequence */
3617             MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3618             if (mg) {
3619                 I32 pos = mg->mg_len;
3620                 if (pos > 0) {
3621                     for (c = start + pos; c > start; c--) {
3622                         if (UTF8_IS_START(*c))
3623                             break;
3624                     }
3625                     mg->mg_len  = c - start;
3626                 }
3627             }
3628             if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3629                 magic_setutf8(sv,mg); /* clear UTF8 cache */
3630         }
3631     }
3632     return TRUE;
3633 }
3634
3635 /*
3636 =for apidoc sv_setsv
3637 =for apidoc_item sv_setsv_flags
3638 =for apidoc_item sv_setsv_mg
3639 =for apidoc_item sv_setsv_nomg
3640
3641 These copy the contents of the source SV C<ssv> into the destination SV C<dsv>.
3642 C<ssv> may be destroyed if it is mortal, so don't use these functions if
3643 the source SV needs to be reused.
3644 Loosely speaking, they perform a copy-by-value, obliterating any previous
3645 content of the destination.
3646
3647 They differ only in that:
3648
3649 C<sv_setsv> calls 'get' magic on C<ssv>, but skips 'set' magic on C<dsv>.
3650
3651 C<sv_setsv_mg> calls both 'get' magic on C<ssv> and 'set' magic on C<dsv>.
3652
3653 C<sv_setsv_nomg> skips all magic.
3654
3655 C<sv_setsv_flags> has a C<flags> parameter which you can use to specify any
3656 combination of magic handling, and also you can specify C<SV_NOSTEAL> so that
3657 the buffers of temps will not be stolen.
3658
3659 You probably want to instead use one of the assortment of wrappers, such as
3660 C<L</SvSetSV>>, C<L</SvSetSV_nosteal>>, C<L</SvSetMagicSV>> and
3661 C<L</SvSetMagicSV_nosteal>>.
3662
3663 C<sv_setsv_flags> is the primary function for copying scalars, and most other
3664 copy-ish functions and macros use it underneath.
3665
3666 =for apidoc Amnh||SV_NOSTEAL
3667
3668 =cut
3669 */
3670
3671 static void
3672 S_glob_assign_glob(pTHX_ SV *const dsv, SV *const ssv, const int dtype)
3673 {
3674     I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */
3675     HV *old_stash = NULL;
3676
3677     PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
3678
3679     if (dtype != SVt_PVGV && !isGV_with_GP(dsv)) {
3680         const char * const name = GvNAME(ssv);
3681         const STRLEN len = GvNAMELEN(ssv);
3682         {
3683             if (dtype >= SVt_PV) {
3684                 SvPV_free(dsv);
3685                 SvPV_set(dsv, 0);
3686                 SvLEN_set(dsv, 0);
3687                 SvCUR_set(dsv, 0);
3688             }
3689             SvUPGRADE(dsv, SVt_PVGV);
3690             (void)SvOK_off(dsv);
3691             isGV_with_GP_on(dsv);
3692         }
3693         GvSTASH(dsv) = GvSTASH(ssv);
3694         if (GvSTASH(dsv))
3695             Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
3696         gv_name_set(MUTABLE_GV(dsv), name, len,
3697                         GV_ADD | (GvNAMEUTF8(ssv) ? SVf_UTF8 : 0 ));
3698         SvFAKE_on(dsv); /* can coerce to non-glob */
3699     }
3700
3701     if(GvGP(MUTABLE_GV(ssv))) {
3702         /* If source has method cache entry, clear it */
3703         if(GvCVGEN(ssv)) {
3704             SvREFCNT_dec(GvCV(ssv));
3705             GvCV_set(ssv, NULL);
3706             GvCVGEN(ssv) = 0;
3707         }
3708         /* If source has a real method, then a method is
3709            going to change */
3710         else if(
3711          GvCV((const GV *)ssv) && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3712         ) {
3713             mro_changes = 1;
3714         }
3715     }
3716
3717     /* If dest already had a real method, that's a change as well */
3718     if(
3719         !mro_changes && GvGP(MUTABLE_GV(dsv)) && GvCVu((const GV *)dsv)
3720      && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3721     ) {
3722         mro_changes = 1;
3723     }
3724
3725     /* We don't need to check the name of the destination if it was not a
3726        glob to begin with. */
3727     if(dtype == SVt_PVGV) {
3728         const char * const name = GvNAME((const GV *)dsv);
3729         const STRLEN len = GvNAMELEN(dsv);
3730         if(memEQs(name, len, "ISA")
3731          /* The stash may have been detached from the symbol table, so
3732             check its name. */
3733          && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3734         )
3735             mro_changes = 2;
3736         else {
3737             if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
3738              || (len == 1 && name[0] == ':')) {
3739                 mro_changes = 3;
3740
3741                 /* Set aside the old stash, so we can reset isa caches on
3742                    its subclasses. */
3743                 if((old_stash = GvHV(dsv)))
3744                     /* Make sure we do not lose it early. */
3745                     SvREFCNT_inc_simple_void_NN(
3746                      sv_2mortal((SV *)old_stash)
3747                     );
3748             }
3749         }
3750
3751         SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
3752     }
3753
3754     /* freeing dsv's GP might free ssv (e.g. *x = $x),
3755      * so temporarily protect it */
3756     ENTER;
3757     SAVEFREESV(SvREFCNT_inc_simple_NN(ssv));
3758     gp_free(MUTABLE_GV(dsv));
3759     GvINTRO_off(dsv);           /* one-shot flag */
3760     GvGP_set(dsv, gp_ref(GvGP(ssv)));
3761     LEAVE;
3762
3763     if (SvTAINTED(ssv))
3764         SvTAINT(dsv);
3765     if (GvIMPORTED(dsv) != GVf_IMPORTED
3766         && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
3767         {
3768             GvIMPORTED_on(dsv);
3769         }
3770     GvMULTI_on(dsv);
3771     if(mro_changes == 2) {
3772       if (GvAV((const GV *)ssv)) {
3773         MAGIC *mg;
3774         SV * const sref = (SV *)GvAV((const GV *)dsv);
3775         if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
3776             if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3777                 AV * const ary = newAV_alloc_x(2);
3778                 av_push_simple(ary, mg->mg_obj); /* takes the refcount */
3779                 av_push_simple(ary, SvREFCNT_inc_simple_NN(dsv));
3780                 mg->mg_obj = (SV *)ary;
3781             } else {
3782                 av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dsv));
3783             }
3784         }
3785         else sv_magic(sref, dsv, PERL_MAGIC_isa, NULL, 0);
3786       }
3787       mro_isa_changed_in(GvSTASH(dsv));
3788     }
3789     else if(mro_changes == 3) {
3790         HV * const stash = GvHV(dsv);
3791         if(old_stash ? (HV *)HvENAME_get(old_stash) : stash)
3792             mro_package_moved(
3793                 stash, old_stash,
3794                 (GV *)dsv, 0
3795             );
3796     }
3797     else if(mro_changes) mro_method_changed_in(GvSTASH(dsv));
3798     if (GvIO(dsv) && dtype == SVt_PVGV) {
3799         DEBUG_o(Perl_deb(aTHX_
3800                         "glob_assign_glob clearing PL_stashcache\n"));
3801         /* It's a cache. It will rebuild itself quite happily.
3802            It's a lot of effort to work out exactly which key (or keys)
3803            might be invalidated by the creation of the this file handle.
3804          */
3805         hv_clear(PL_stashcache);
3806     }
3807     return;
3808 }
3809
3810 void
3811 Perl_gv_setref(pTHX_ SV *const dsv, SV *const ssv)
3812 {
3813     SV * const sref = SvRV(ssv);
3814     SV *dref;
3815     const int intro = GvINTRO(dsv);
3816     SV **location;
3817     U8 import_flag = 0;
3818     const U32 stype = SvTYPE(sref);
3819
3820     PERL_ARGS_ASSERT_GV_SETREF;
3821
3822     if (intro) {
3823         GvINTRO_off(dsv);       /* one-shot flag */
3824         GvLINE(dsv) = CopLINE(PL_curcop);
3825         GvEGV(dsv) = MUTABLE_GV(dsv);
3826     }
3827     GvMULTI_on(dsv);
3828     switch (stype) {
3829     case SVt_PVCV:
3830         location = (SV **) &(GvGP(dsv)->gp_cv); /* XXX bypassing GvCV_set */
3831         import_flag = GVf_IMPORTED_CV;
3832         goto common;
3833     case SVt_PVHV:
3834         location = (SV **) &GvHV(dsv);
3835         import_flag = GVf_IMPORTED_HV;
3836         goto common;
3837     case SVt_PVAV:
3838         location = (SV **) &GvAV(dsv);
3839         import_flag = GVf_IMPORTED_AV;
3840         goto common;
3841     case SVt_PVIO:
3842         location = (SV **) &GvIOp(dsv);
3843         goto common;
3844     case SVt_PVFM:
3845         location = (SV **) &GvFORM(dsv);
3846         goto common;
3847     default:
3848         location = &GvSV(dsv);
3849         import_flag = GVf_IMPORTED_SV;
3850     common:
3851         if (intro) {
3852             if (stype == SVt_PVCV) {
3853                 /*if (GvCVGEN(dsv) && (GvCV(dsv) != (const CV *)sref || GvCVGEN(dsv))) {*/
3854                 if (GvCVGEN(dsv)) {
3855                     SvREFCNT_dec(GvCV(dsv));
3856                     GvCV_set(dsv, NULL);
3857                     GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3858                 }
3859             }
3860             /* SAVEt_GVSLOT takes more room on the savestack and has more
3861                overhead in leave_scope than SAVEt_GENERIC_SV.  But for CVs
3862                leave_scope needs access to the GV so it can reset method
3863                caches.  We must use SAVEt_GVSLOT whenever the type is
3864                SVt_PVCV, even if the stash is anonymous, as the stash may
3865                gain a name somehow before leave_scope. */
3866             if (stype == SVt_PVCV) {
3867                 /* There is no save_pushptrptrptr.  Creating it for this
3868                    one call site would be overkill.  So inline the ss add
3869                    routines here. */
3870                 dSS_ADD;
3871                 SS_ADD_PTR(dsv);
3872                 SS_ADD_PTR(location);
3873                 SS_ADD_PTR(SvREFCNT_inc(*location));
3874                 SS_ADD_UV(SAVEt_GVSLOT);
3875                 SS_ADD_END(4);
3876             }
3877             else SAVEGENERICSV(*location);
3878         }
3879         dref = *location;
3880         if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dsv))) {
3881             CV* const cv = MUTABLE_CV(*location);
3882             if (cv) {
3883                 if (!GvCVGEN((const GV *)dsv) &&
3884                     (CvROOT(cv) || CvXSUB(cv)) &&
3885                     /* redundant check that avoids creating the extra SV
3886                        most of the time: */
3887                     (CvCONST(cv) || (ckWARN(WARN_REDEFINE) && !intro)))
3888                     {
3889                         SV * const new_const_sv =
3890                             CvCONST((const CV *)sref)
3891                                  ? cv_const_sv((const CV *)sref)
3892                                  : NULL;
3893                         HV * const stash = GvSTASH((const GV *)dsv);
3894                         report_redefined_cv(
3895                            sv_2mortal(
3896                              stash
3897                                ? Perl_newSVpvf(aTHX_
3898                                     "%" HEKf "::%" HEKf,
3899                                     HEKfARG(HvNAME_HEK(stash)),
3900                                     HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3901                                : Perl_newSVpvf(aTHX_
3902                                     "%" HEKf,
3903                                     HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3904                            ),
3905                            cv,
3906                            CvCONST((const CV *)sref) ? &new_const_sv : NULL
3907                         );
3908                     }
3909                 if (!intro)
3910                     cv_ckproto_len_flags(cv, (const GV *)dsv,
3911                                    SvPOK(sref) ? CvPROTO(sref) : NULL,
3912                                    SvPOK(sref) ? CvPROTOLEN(sref) : 0,
3913                                    SvPOK(sref) ? SvUTF8(sref) : 0);
3914             }
3915             GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3916             GvASSUMECV_on(dsv);
3917             if(GvSTASH(dsv)) { /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
3918                 if (intro && GvREFCNT(dsv) > 1) {
3919                     /* temporary remove extra savestack's ref */
3920                     --GvREFCNT(dsv);
3921                     gv_method_changed(dsv);
3922                     ++GvREFCNT(dsv);
3923                 }
3924                 else gv_method_changed(dsv);
3925             }
3926         }
3927         *location = SvREFCNT_inc_simple_NN(sref);
3928         if (import_flag && !(GvFLAGS(dsv) & import_flag)
3929             && CopSTASH_ne(PL_curcop, GvSTASH(dsv))) {
3930             GvFLAGS(dsv) |= import_flag;
3931         }
3932
3933         if (stype == SVt_PVHV) {
3934             const char * const name = GvNAME((GV*)dsv);
3935             const STRLEN len = GvNAMELEN(dsv);
3936             if (
3937                 (
3938                    (len > 1 && name[len-2] == ':' && name[len-1] == ':')
3939                 || (len == 1 && name[0] == ':')
3940                 )
3941              && (!dref || HvENAME_get(dref))
3942             ) {
3943                 mro_package_moved(
3944                     (HV *)sref, (HV *)dref,
3945                     (GV *)dsv, 0
3946                 );
3947             }
3948         }
3949         else if (
3950             stype == SVt_PVAV && sref != dref
3951          && memEQs(GvNAME((GV*)dsv), GvNAMELEN((GV*)dsv), "ISA")
3952          /* The stash may have been detached from the symbol table, so
3953             check its name before doing anything. */
3954          && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3955         ) {
3956             MAGIC *mg;
3957             MAGIC * const omg = dref && SvSMAGICAL(dref)
3958                                  ? mg_find(dref, PERL_MAGIC_isa)
3959                                  : NULL;
3960             if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
3961                 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3962                     AV * const ary = newAV_alloc_xz(4);
3963                     av_push_simple(ary, mg->mg_obj); /* takes the refcount */
3964                     mg->mg_obj = (SV *)ary;
3965                 }
3966                 if (omg) {
3967                     if (SvTYPE(omg->mg_obj) == SVt_PVAV) {
3968                         SV **svp = AvARRAY((AV *)omg->mg_obj);
3969                         I32 items = AvFILLp((AV *)omg->mg_obj) + 1;
3970                         while (items--)
3971                             av_push(
3972                              (AV *)mg->mg_obj,
3973                              SvREFCNT_inc_simple_NN(*svp++)
3974                             );
3975                     }
3976                     else
3977                         av_push(
3978                          (AV *)mg->mg_obj,
3979                          SvREFCNT_inc_simple_NN(omg->mg_obj)
3980                         );
3981                 }
3982                 else
3983                     av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dsv));
3984             }
3985             else
3986             {
3987                 SSize_t i;
3988                 sv_magic(
3989                  sref, omg ? omg->mg_obj : dsv, PERL_MAGIC_isa, NULL, 0
3990                 );
3991                 for (i = 0; i <= AvFILL(sref); ++i) {
3992                     SV **elem = av_fetch ((AV*)sref, i, 0);
3993                     if (elem) {
3994                         sv_magic(
3995                           *elem, sref, PERL_MAGIC_isaelem, NULL, i
3996                         );
3997                     }
3998                 }
3999                 mg = mg_find(sref, PERL_MAGIC_isa);
4000             }
4001             /* Since the *ISA assignment could have affected more than
4002                one stash, don't call mro_isa_changed_in directly, but let
4003                magic_clearisa do it for us, as it already has the logic for
4004                dealing with globs vs arrays of globs. */
4005             assert(mg);
4006             Perl_magic_clearisa(aTHX_ NULL, mg);
4007         }
4008         else if (stype == SVt_PVIO) {
4009             DEBUG_o(Perl_deb(aTHX_ "gv_setref clearing PL_stashcache\n"));
4010             /* It's a cache. It will rebuild itself quite happily.
4011                It's a lot of effort to work out exactly which key (or keys)
4012                might be invalidated by the creation of the this file handle.
4013             */
4014             hv_clear(PL_stashcache);
4015         }
4016         break;
4017     }
4018     if (!intro) SvREFCNT_dec(dref);
4019     if (SvTAINTED(ssv))
4020         SvTAINT(dsv);
4021     return;
4022 }
4023
4024
4025
4026
4027 #ifdef PERL_DEBUG_READONLY_COW
4028 # include <sys/mman.h>
4029
4030 # ifndef PERL_MEMORY_DEBUG_HEADER_SIZE
4031 #  define PERL_MEMORY_DEBUG_HEADER_SIZE 0
4032 # endif
4033
4034 void
4035 Perl_sv_buf_to_ro(pTHX_ SV *sv)
4036 {
4037     struct perl_memory_debug_header * const header =
4038         (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4039     const MEM_SIZE len = header->size;
4040     PERL_ARGS_ASSERT_SV_BUF_TO_RO;
4041 # ifdef PERL_TRACK_MEMPOOL
4042     if (!header->readonly) header->readonly = 1;
4043 # endif
4044     if (mprotect(header, len, PROT_READ))
4045         Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
4046                          header, len, errno);
4047 }
4048
4049 static void
4050 S_sv_buf_to_rw(pTHX_ SV *sv)
4051 {
4052     struct perl_memory_debug_header * const header =
4053         (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4054     const MEM_SIZE len = header->size;
4055     PERL_ARGS_ASSERT_SV_BUF_TO_RW;
4056     if (mprotect(header, len, PROT_READ|PROT_WRITE))
4057         Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
4058                          header, len, errno);
4059 # ifdef PERL_TRACK_MEMPOOL
4060     header->readonly = 0;
4061 # endif
4062 }
4063
4064 #else
4065 # define sv_buf_to_ro(sv)       NOOP
4066 # define sv_buf_to_rw(sv)       NOOP
4067 #endif
4068
4069 void
4070 Perl_sv_setsv_flags(pTHX_ SV *dsv, SV* ssv, const I32 flags)
4071 {
4072     U32 sflags;
4073     int dtype;
4074     svtype stype;
4075     unsigned int both_type;
4076
4077     PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
4078
4079     if (UNLIKELY( ssv == dsv ))
4080         return;
4081
4082     if (UNLIKELY( !ssv ))
4083         ssv = &PL_sv_undef;
4084
4085     stype = SvTYPE(ssv);
4086     dtype = SvTYPE(dsv);
4087     both_type = (stype | dtype);
4088
4089     /* with these values, we can check that both SVs are NULL/IV (and not
4090      * freed) just by testing the or'ed types */
4091     STATIC_ASSERT_STMT(SVt_NULL == 0);
4092     STATIC_ASSERT_STMT(SVt_IV   == 1);
4093     if (both_type <= 1) {
4094         /* both src and dst are UNDEF/IV/RV, so we can do a lot of
4095          * special-casing */
4096         U32 sflags;
4097         U32 new_dflags;
4098         SV *old_rv = NULL;
4099
4100         /* minimal subset of SV_CHECK_THINKFIRST_COW_DROP(dsv) */
4101         if (SvREADONLY(dsv))
4102             Perl_croak_no_modify();
4103         if (SvROK(dsv)) {
4104             if (SvWEAKREF(dsv))
4105                 sv_unref_flags(dsv, 0);
4106             else
4107                 old_rv = SvRV(dsv);
4108         }
4109
4110         assert(!SvGMAGICAL(ssv));
4111         assert(!SvGMAGICAL(dsv));
4112
4113         sflags = SvFLAGS(ssv);
4114         if (sflags & (SVf_IOK|SVf_ROK)) {
4115             SET_SVANY_FOR_BODYLESS_IV(dsv);
4116             new_dflags = SVt_IV;
4117
4118             if (sflags & SVf_ROK) {
4119                 dsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(ssv));
4120                 new_dflags |= SVf_ROK;
4121             }
4122             else {
4123                 /* both src and dst are <= SVt_IV, so sv_any points to the
4124                  * head; so access the head directly
4125                  */
4126                 assert(    &(ssv->sv_u.svu_iv)
4127                         == &(((XPVIV*) SvANY(ssv))->xiv_iv));
4128                 assert(    &(dsv->sv_u.svu_iv)
4129                         == &(((XPVIV*) SvANY(dsv))->xiv_iv));
4130                 dsv->sv_u.svu_iv = ssv->sv_u.svu_iv;
4131                 new_dflags |= (SVf_IOK|SVp_IOK|(sflags & SVf_IVisUV));
4132             }
4133         }
4134         else {
4135             new_dflags = dtype; /* turn off everything except the type */
4136         }
4137         SvFLAGS(dsv) = new_dflags;
4138         SvREFCNT_dec(old_rv);
4139
4140         return;
4141     }
4142
4143     if (UNLIKELY(both_type == SVTYPEMASK)) {
4144         if (SvIS_FREED(dsv)) {
4145             Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
4146                        " to a freed scalar %p", SVfARG(ssv), (void *)dsv);
4147         }
4148         if (SvIS_FREED(ssv)) {
4149             Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
4150                        (void*)ssv, (void*)dsv);
4151         }
4152     }
4153
4154
4155
4156     SV_CHECK_THINKFIRST_COW_DROP(dsv);
4157     dtype = SvTYPE(dsv); /* THINKFIRST may have changed type */
4158
4159     /* There's a lot of redundancy below but we're going for speed here */
4160
4161     switch (stype) {
4162     case SVt_NULL:
4163       undef_sstr:
4164         if (LIKELY( dtype != SVt_PVGV && dtype != SVt_PVLV )) {
4165             (void)SvOK_off(dsv);
4166             return;
4167         }
4168         break;
4169     case SVt_IV:
4170         if (SvIOK(ssv)) {
4171             switch (dtype) {
4172             case SVt_NULL:
4173                 /* For performance, we inline promoting to type SVt_IV. */
4174                 /* We're starting from SVt_NULL, so provided that define is
4175                  * actual 0, we don't have to unset any SV type flags
4176                  * to promote to SVt_IV. */
4177                 STATIC_ASSERT_STMT(SVt_NULL == 0);
4178                 SET_SVANY_FOR_BODYLESS_IV(dsv);
4179                 SvFLAGS(dsv) |= SVt_IV;
4180                 break;
4181             case SVt_NV:
4182             case SVt_PV:
4183                 sv_upgrade(dsv, SVt_PVIV);
4184                 break;
4185             case SVt_PVGV:
4186             case SVt_PVLV:
4187                 goto end_of_first_switch;
4188             }
4189             (void)SvIOK_only(dsv);
4190             SvIV_set(dsv,  SvIVX(ssv));
4191             if (SvIsUV(ssv))
4192                 SvIsUV_on(dsv);
4193             /* SvTAINTED can only be true if the SV has taint magic, which in
4194                turn means that the SV type is PVMG (or greater). This is the
4195                case statement for SVt_IV, so this cannot be true (whatever gcov
4196                may say).  */
4197             assert(!SvTAINTED(ssv));
4198             return;
4199         }
4200         if (!SvROK(ssv))
4201             goto undef_sstr;
4202         if (dtype < SVt_PV && dtype != SVt_IV)
4203             sv_upgrade(dsv, SVt_IV);
4204         break;
4205
4206     case SVt_NV:
4207         if (LIKELY( SvNOK(ssv) )) {
4208             switch (dtype) {
4209             case SVt_NULL:
4210             case SVt_IV:
4211                 sv_upgrade(dsv, SVt_NV);
4212                 break;
4213             case SVt_PV:
4214             case SVt_PVIV:
4215                 sv_upgrade(dsv, SVt_PVNV);
4216                 break;
4217             case SVt_PVGV:
4218             case SVt_PVLV:
4219                 goto end_of_first_switch;
4220             }
4221             SvNV_set(dsv, SvNVX(ssv));
4222             (void)SvNOK_only(dsv);
4223             /* SvTAINTED can only be true if the SV has taint magic, which in
4224                turn means that the SV type is PVMG (or greater). This is the
4225                case statement for SVt_NV, so this cannot be true (whatever gcov
4226                may say).  */
4227             assert(!SvTAINTED(ssv));
4228             return;
4229         }
4230         goto undef_sstr;
4231
4232     case SVt_PV:
4233         if (dtype < SVt_PV)
4234             sv_upgrade(dsv, SVt_PV);
4235         break;
4236     case SVt_PVIV:
4237         if (dtype < SVt_PVIV)
4238             sv_upgrade(dsv, SVt_PVIV);
4239         break;
4240     case SVt_PVNV:
4241         if (dtype < SVt_PVNV)
4242             sv_upgrade(dsv, SVt_PVNV);
4243         break;
4244
4245     case SVt_INVLIST:
4246         invlist_clone(ssv, dsv);
4247         break;
4248     default:
4249         {
4250         const char * const type = sv_reftype(ssv,0);
4251         if (PL_op)
4252             /* diag_listed_as: Bizarre copy of %s */
4253             Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
4254         else
4255             Perl_croak(aTHX_ "Bizarre copy of %s", type);
4256         }
4257         NOT_REACHED; /* NOTREACHED */
4258
4259     case SVt_REGEXP:
4260       upgregexp:
4261         if (dtype < SVt_REGEXP)
4262             sv_upgrade(dsv, SVt_REGEXP);
4263         break;
4264
4265     case SVt_PVLV:
4266     case SVt_PVGV:
4267     case SVt_PVMG:
4268         if (SvGMAGICAL(ssv) && (flags & SV_GMAGIC)) {
4269             mg_get(ssv);
4270             if (SvTYPE(ssv) != stype)
4271                 stype = SvTYPE(ssv);
4272         }
4273         if (isGV_with_GP(ssv) && dtype <= SVt_PVLV) {
4274                     glob_assign_glob(dsv, ssv, dtype);
4275                     return;
4276         }
4277         if (stype == SVt_PVLV)
4278         {
4279             if (isREGEXP(ssv)) goto upgregexp;
4280             SvUPGRADE(dsv, SVt_PVNV);
4281         }
4282         else
4283             SvUPGRADE(dsv, (svtype)stype);
4284     }
4285  end_of_first_switch:
4286
4287     /* dsv may have been upgraded.  */
4288     dtype = SvTYPE(dsv);
4289     sflags = SvFLAGS(ssv);
4290
4291     if (UNLIKELY( dtype == SVt_PVCV )) {
4292         /* Assigning to a subroutine sets the prototype.  */
4293         if (SvOK(ssv)) {
4294             STRLEN len;
4295             const char *const ptr = SvPV_const(ssv, len);
4296
4297             SvGROW(dsv, len + 1);
4298             Copy(ptr, SvPVX(dsv), len + 1, char);
4299             SvCUR_set(dsv, len);
4300             SvPOK_only(dsv);
4301             SvFLAGS(dsv) |= sflags & SVf_UTF8;
4302             CvAUTOLOAD_off(dsv);
4303         } else {
4304             SvOK_off(dsv);
4305         }
4306     }
4307     else if (UNLIKELY(dtype == SVt_PVAV || dtype == SVt_PVHV
4308              || dtype == SVt_PVFM))
4309     {
4310         const char * const type = sv_reftype(dsv,0);
4311         if (PL_op)
4312             /* diag_listed_as: Cannot copy to %s */
4313             Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op));
4314         else
4315             Perl_croak(aTHX_ "Cannot copy to %s", type);
4316     } else if (sflags & SVf_ROK) {
4317         if (isGV_with_GP(dsv)
4318             && SvTYPE(SvRV(ssv)) == SVt_PVGV && isGV_with_GP(SvRV(ssv))) {
4319             ssv = SvRV(ssv);
4320             if (ssv == dsv) {
4321                 if (GvIMPORTED(dsv) != GVf_IMPORTED
4322                     && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
4323                 {
4324                     GvIMPORTED_on(dsv);
4325                 }
4326                 GvMULTI_on(dsv);
4327                 return;
4328             }
4329             glob_assign_glob(dsv, ssv, dtype);
4330             return;
4331         }
4332
4333         if (dtype >= SVt_PV) {
4334             if (isGV_with_GP(dsv)) {
4335                 gv_setref(dsv, ssv);
4336                 return;
4337             }
4338             if (SvPVX_const(dsv)) {
4339                 SvPV_free(dsv);
4340                 SvLEN_set(dsv, 0);
4341                 SvCUR_set(dsv, 0);
4342             }
4343         }
4344         (void)SvOK_off(dsv);
4345         SvRV_set(dsv, SvREFCNT_inc(SvRV(ssv)));
4346         SvFLAGS(dsv) |= sflags & SVf_ROK;
4347         assert(!(sflags & SVp_NOK));
4348         assert(!(sflags & SVp_IOK));
4349         assert(!(sflags & SVf_NOK));
4350         assert(!(sflags & SVf_IOK));
4351     }
4352     else if (isGV_with_GP(dsv)) {
4353         if (!(sflags & SVf_OK)) {
4354             Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4355                            "Undefined value assigned to typeglob");
4356         }
4357         else {
4358             GV *gv = gv_fetchsv_nomg(ssv, GV_ADD, SVt_PVGV);
4359             if (dsv != (const SV *)gv) {
4360                 const char * const name = GvNAME((const GV *)dsv);
4361                 const STRLEN len = GvNAMELEN(dsv);
4362                 HV *old_stash = NULL;
4363                 bool reset_isa = FALSE;
4364                 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
4365                  || (len == 1 && name[0] == ':')) {
4366                     /* Set aside the old stash, so we can reset isa caches
4367                        on its subclasses. */
4368                     if((old_stash = GvHV(dsv))) {
4369                         /* Make sure we do not lose it early. */
4370                         SvREFCNT_inc_simple_void_NN(
4371                          sv_2mortal((SV *)old_stash)
4372                         );
4373                     }
4374                     reset_isa = TRUE;
4375                 }
4376
4377                 if (GvGP(dsv)) {
4378                     SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
4379                     gp_free(MUTABLE_GV(dsv));
4380                 }
4381                 GvGP_set(dsv, gp_ref(GvGP(gv)));
4382
4383                 if (reset_isa) {
4384                     HV * const stash = GvHV(dsv);
4385                     if(
4386                         old_stash ? (HV *)HvENAME_get(old_stash) : stash
4387                     )
4388                         mro_package_moved(
4389                          stash, old_stash,
4390                          (GV *)dsv, 0
4391                         );
4392                 }
4393             }
4394         }
4395     }
4396     else if ((dtype == SVt_REGEXP || dtype == SVt_PVLV)
4397           && (stype == SVt_REGEXP || isREGEXP(ssv))) {
4398         reg_temp_copy((REGEXP*)dsv, (REGEXP*)ssv);
4399     }
4400     else if (sflags & SVp_POK) {
4401         const STRLEN cur = SvCUR(ssv);
4402         const STRLEN len = SvLEN(ssv);
4403
4404         /*
4405          * We have three basic ways to copy the string:
4406          *
4407          *  1. Swipe
4408          *  2. Copy-on-write
4409          *  3. Actual copy
4410          *
4411          * Which we choose is based on various factors.  The following
4412          * things are listed in order of speed, fastest to slowest:
4413          *  - Swipe
4414          *  - Copying a short string
4415          *  - Copy-on-write bookkeeping
4416          *  - malloc
4417          *  - Copying a long string
4418          *
4419          * We swipe the string (steal the string buffer) if the SV on the
4420          * rhs is about to be freed anyway (TEMP and refcnt==1).  This is a
4421          * big win on long strings.  It should be a win on short strings if
4422          * SvPVX_const(dsv) has to be allocated.  If not, it should not
4423          * slow things down, as SvPVX_const(ssv) would have been freed
4424          * soon anyway.
4425          *
4426          * We also steal the buffer from a PADTMP (operator target) if it
4427          * is â€˜long enough’.  For short strings, a swipe does not help
4428          * here, as it causes more malloc calls the next time the target
4429          * is used.  Benchmarks show that even if SvPVX_const(dsv) has to
4430          * be allocated it is still not worth swiping PADTMPs for short
4431          * strings, as the savings here are small.
4432          *
4433          * If swiping is not an option, then we see whether it is
4434          * worth using copy-on-write.  If the lhs already has a buf-
4435          * fer big enough and the string is short, we skip it and fall back
4436          * to method 3, since memcpy is faster for short strings than the
4437          * later bookkeeping overhead that copy-on-write entails.
4438
4439          * If the rhs is not a copy-on-write string yet, then we also
4440          * consider whether the buffer is too large relative to the string
4441          * it holds.  Some operations such as readline allocate a large
4442          * buffer in the expectation of reusing it.  But turning such into
4443          * a COW buffer is counter-productive because it increases memory
4444          * usage by making readline allocate a new large buffer the sec-
4445          * ond time round.  So, if the buffer is too large, again, we use
4446          * method 3 (copy).
4447          *
4448          * Finally, if there is no buffer on the left, or the buffer is too
4449          * small, then we use copy-on-write and make both SVs share the
4450          * string buffer.
4451          *
4452          */
4453
4454         /* Whichever path we take through the next code, we want this true,
4455            and doing it now facilitates the COW check.  */
4456         (void)SvPOK_only(dsv);
4457
4458         if (
4459                  (              /* Either ... */
4460                                 /* slated for free anyway (and not COW)? */
4461                     (sflags & (SVs_TEMP|SVf_IsCOW)) == SVs_TEMP
4462                                 /* or a swipable TARG */
4463                  || ((sflags &
4464                            (SVs_PADTMP|SVf_READONLY|SVf_PROTECT|SVf_IsCOW))
4465                        == SVs_PADTMP
4466                                 /* whose buffer is worth stealing */
4467                      && CHECK_COWBUF_THRESHOLD(cur,len)
4468                     )
4469                  ) &&
4470                  !(sflags & SVf_OOK) &&   /* and not involved in OOK hack? */
4471                  (!(flags & SV_NOSTEAL)) &&
4472                                         /* and we're allowed to steal temps */
4473                  SvREFCNT(ssv) == 1 &&   /* and no other references to it? */
4474                  len)             /* and really is a string */
4475         {       /* Passes the swipe test.  */
4476             if (SvPVX_const(dsv))       /* we know that dtype >= SVt_PV */
4477                 SvPV_free(dsv);
4478             SvPV_set(dsv, SvPVX_mutable(ssv));
4479             SvLEN_set(dsv, SvLEN(ssv));
4480             SvCUR_set(dsv, SvCUR(ssv));
4481
4482             SvTEMP_off(dsv);
4483             (void)SvOK_off(ssv);        /* NOTE: nukes most SvFLAGS on ssv */
4484             SvPV_set(ssv, NULL);
4485             SvLEN_set(ssv, 0);
4486             SvCUR_set(ssv, 0);
4487             SvTEMP_off(ssv);
4488         }
4489         /* We must check for SvIsCOW_static() even without
4490          * SV_COW_SHARED_HASH_KEYS being set or else we'll break SvIsBOOL()
4491          */
4492         else if (SvIsCOW_static(ssv)) {
4493             if (SvPVX_const(dsv)) {     /* we know that dtype >= SVt_PV */
4494                 SvPV_free(dsv);
4495             }
4496             SvPV_set(dsv, SvPVX(ssv));
4497             SvLEN_set(dsv, 0);
4498             SvCUR_set(dsv, cur);
4499             SvFLAGS(dsv) |= (SVf_IsCOW|SVppv_STATIC);
4500         }
4501         else if (flags & SV_COW_SHARED_HASH_KEYS
4502               &&
4503 #ifdef PERL_COPY_ON_WRITE
4504                  (sflags & SVf_IsCOW
4505                    ? (!len ||
4506                        (  (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4507                           /* If this is a regular (non-hek) COW, only so
4508                              many COW "copies" are possible. */
4509                        && CowREFCNT(ssv) != SV_COW_REFCNT_MAX  ))
4510                    : (  (sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4511                      && !(SvFLAGS(dsv) & SVf_BREAK)
4512                      && CHECK_COW_THRESHOLD(cur,len) && cur+1 < len
4513                      && (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4514                     ))
4515 #else
4516                  sflags & SVf_IsCOW
4517               && !(SvFLAGS(dsv) & SVf_BREAK)
4518 #endif
4519             ) {
4520             /* Either it's a shared hash key, or it's suitable for
4521                copy-on-write.  */
4522 #ifdef DEBUGGING
4523             if (DEBUG_C_TEST) {
4524                 PerlIO_printf(Perl_debug_log, "Copy on write: ssv --> dsv\n");
4525                 sv_dump(ssv);
4526                 sv_dump(dsv);
4527             }
4528 #endif
4529 #ifdef PERL_ANY_COW
4530             if (!(sflags & SVf_IsCOW)) {
4531                     SvIsCOW_on(ssv);
4532                     CowREFCNT(ssv) = 0;
4533             }
4534 #endif
4535             if (SvPVX_const(dsv)) {     /* we know that dtype >= SVt_PV */
4536                 SvPV_free(dsv);
4537             }
4538
4539 #ifdef PERL_ANY_COW
4540             if (len) {
4541                     if (sflags & SVf_IsCOW) {
4542                         sv_buf_to_rw(ssv);
4543                     }
4544                     CowREFCNT(ssv)++;
4545                     SvPV_set(dsv, SvPVX_mutable(ssv));
4546                     sv_buf_to_ro(ssv);
4547             } else
4548 #endif
4549             {
4550                     /* SvIsCOW_shared_hash */
4551                     DEBUG_C(PerlIO_printf(Perl_debug_log,
4552                                           "Copy on write: Sharing hash\n"));
4553
4554                     assert (SvTYPE(dsv) >= SVt_PV);
4555                     SvPV_set(dsv,
4556                              HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)))));
4557             }
4558             SvLEN_set(dsv, len);
4559             SvCUR_set(dsv, cur);
4560             SvIsCOW_on(dsv);
4561         } else {
4562             /* Failed the swipe test, and we cannot do copy-on-write either.
4563                Have to copy the string.  */
4564             SvGROW(dsv, cur + 1);       /* inlined from sv_setpvn */
4565             Move(SvPVX_const(ssv),SvPVX(dsv),cur,char);
4566             SvCUR_set(dsv, cur);
4567             *SvEND(dsv) = '\0';
4568         }
4569         if (sflags & SVp_NOK) {
4570             SvNV_set(dsv, SvNVX(ssv));
4571             if ((sflags & SVf_NOK) && !(sflags & SVf_POK)) {
4572                 /* Source was SVf_NOK|SVp_NOK|SVp_POK but not SVf_POK, meaning
4573                    a value set as floating point and later stringified, where
4574                   the value happens to be one of the few that we know aren't
4575                   affected by the numeric locale, hence we can cache the
4576                   stringification. Currently that's  +Inf, -Inf and NaN, but
4577                   conceivably we might extend this to -9 .. +9 (excluding -0).
4578                   So mark destination the same: */
4579                 SvFLAGS(dsv) &= ~SVf_POK;
4580             }
4581         }
4582         if (sflags & SVp_IOK) {
4583             SvIV_set(dsv, SvIVX(ssv));
4584             if (sflags & SVf_IVisUV)
4585                 SvIsUV_on(dsv);
4586             if ((sflags & SVf_IOK) && !(sflags & SVf_POK)) {
4587                 /* Source was SVf_IOK|SVp_IOK|SVp_POK but not SVf_POK, meaning
4588                    a value set as an integer and later stringified. So mark
4589                    destination the same: */
4590                 SvFLAGS(dsv) &= ~SVf_POK;
4591             }
4592         }
4593         SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4594         {
4595             const MAGIC * const smg = SvVSTRING_mg(ssv);
4596             if (smg) {
4597                 sv_magic(dsv, NULL, PERL_MAGIC_vstring,
4598                          smg->mg_ptr, smg->mg_len);
4599                 SvRMAGICAL_on(dsv);
4600             }
4601         }
4602     }
4603     else if (sflags & (SVp_IOK|SVp_NOK)) {
4604         (void)SvOK_off(dsv);
4605         SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
4606         if (sflags & SVp_IOK) {
4607             /* XXXX Do we want to set IsUV for IV(ROK)?  Be extra safe... */
4608             SvIV_set(dsv, SvIVX(ssv));
4609         }
4610         if (sflags & SVp_NOK) {
4611             SvNV_set(dsv, SvNVX(ssv));
4612         }
4613     }
4614     else {
4615         if (isGV_with_GP(ssv)) {
4616             gv_efullname3(dsv, MUTABLE_GV(ssv), "*");
4617         }
4618         else
4619             (void)SvOK_off(dsv);
4620     }
4621     if (SvTAINTED(ssv))
4622         SvTAINT(dsv);
4623 }
4624
4625
4626 /*
4627 =for apidoc sv_set_undef
4628
4629 Equivalent to C<sv_setsv(sv, &PL_sv_undef)>, but more efficient.
4630 Doesn't handle set magic.
4631
4632 The perl equivalent is C<$sv = undef;>. Note that it doesn't free any string
4633 buffer, unlike C<undef $sv>.
4634
4635 Introduced in perl 5.25.12.
4636
4637 =cut
4638 */
4639
4640 void
4641 Perl_sv_set_undef(pTHX_ SV *sv)
4642 {
4643     U32 type = SvTYPE(sv);
4644
4645     PERL_ARGS_ASSERT_SV_SET_UNDEF;
4646
4647     /* shortcut, NULL, IV, RV */
4648
4649     if (type <= SVt_IV) {
4650         assert(!SvGMAGICAL(sv));
4651         if (SvREADONLY(sv)) {
4652             /* does undeffing PL_sv_undef count as modifying a read-only
4653              * variable? Some XS code does this */
4654             if (sv == &PL_sv_undef)
4655                 return;
4656             Perl_croak_no_modify();
4657         }
4658
4659         if (SvROK(sv)) {
4660             if (SvWEAKREF(sv))
4661                 sv_unref_flags(sv, 0);
4662             else {
4663                 SV *rv = SvRV(sv);
4664                 SvFLAGS(sv) = type; /* quickly turn off all flags */
4665                 SvREFCNT_dec_NN(rv);
4666                 return;
4667             }
4668         }
4669         SvFLAGS(sv) = type; /* quickly turn off all flags */
4670         return;
4671     }
4672
4673     if (SvIS_FREED(sv))
4674         Perl_croak(aTHX_ "panic: attempt to undefine a freed scalar %p",
4675             (void *)sv);
4676
4677     SV_CHECK_THINKFIRST_COW_DROP(sv);
4678
4679     if (isGV_with_GP(sv))
4680         Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4681                        "Undefined value assigned to typeglob");
4682     else
4683         SvOK_off(sv);
4684 }
4685
4686 /*
4687 =for apidoc sv_set_true
4688
4689 Equivalent to C<sv_setsv(sv, &PL_sv_yes)>, but may be made more
4690 efficient in the future. Doesn't handle set magic.
4691
4692 The perl equivalent is C<$sv = !0;>.
4693
4694 Introduced in perl 5.35.11.
4695
4696 =cut
4697 */
4698
4699 void
4700 Perl_sv_set_true(pTHX_ SV *sv)
4701 {
4702     PERL_ARGS_ASSERT_SV_SET_TRUE;
4703     sv_setsv(sv, &PL_sv_yes);
4704 }
4705
4706 /*
4707 =for apidoc sv_set_false
4708
4709 Equivalent to C<sv_setsv(sv, &PL_sv_no)>, but may be made more
4710 efficient in the future. Doesn't handle set magic.
4711
4712 The perl equivalent is C<$sv = !1;>.
4713
4714 Introduced in perl 5.35.11.
4715
4716 =cut
4717 */
4718
4719 void
4720 Perl_sv_set_false(pTHX_ SV *sv)
4721 {
4722     PERL_ARGS_ASSERT_SV_SET_FALSE;
4723     sv_setsv(sv, &PL_sv_no);
4724 }
4725
4726 /*
4727 =for apidoc sv_set_bool
4728
4729 Equivalent to C<sv_setsv(sv, bool_val ? &Pl_sv_yes : &PL_sv_no)>, but
4730 may be made more efficient in the future. Doesn't handle set magic.
4731
4732 The perl equivalent is C<$sv = !!$expr;>.
4733
4734 Introduced in perl 5.35.11.
4735
4736 =cut
4737 */
4738
4739 void
4740 Perl_sv_set_bool(pTHX_ SV *sv, const bool bool_val)
4741 {
4742     PERL_ARGS_ASSERT_SV_SET_BOOL;
4743     sv_setsv(sv, bool_val ? &PL_sv_yes : &PL_sv_no);
4744 }
4745
4746
4747 void
4748 Perl_sv_setsv_mg(pTHX_ SV *const dsv, SV *const ssv)
4749 {
4750     PERL_ARGS_ASSERT_SV_SETSV_MG;
4751
4752     sv_setsv(dsv,ssv);
4753     SvSETMAGIC(dsv);
4754 }
4755
4756 #ifdef PERL_ANY_COW
4757 #  define SVt_COW SVt_PV
4758 SV *
4759 Perl_sv_setsv_cow(pTHX_ SV *dsv, SV *ssv)
4760 {
4761     STRLEN cur = SvCUR(ssv);
4762     STRLEN len = SvLEN(ssv);
4763     char *new_pv;
4764     U32 new_flags = (SVt_COW|SVf_POK|SVp_POK|SVf_IsCOW);
4765 #if defined(PERL_DEBUG_READONLY_COW) && defined(PERL_COPY_ON_WRITE)
4766     const bool already = cBOOL(SvIsCOW(ssv));
4767 #endif
4768
4769     PERL_ARGS_ASSERT_SV_SETSV_COW;
4770 #ifdef DEBUGGING
4771     if (DEBUG_C_TEST) {
4772         PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4773                       (void*)ssv, (void*)dsv);
4774         sv_dump(ssv);
4775         if (dsv)
4776                     sv_dump(dsv);
4777     }
4778 #endif
4779     if (dsv) {
4780         if (SvTHINKFIRST(dsv))
4781             sv_force_normal_flags(dsv, SV_COW_DROP_PV);
4782         else if (SvPVX_const(dsv))
4783             Safefree(SvPVX_mutable(dsv));
4784     }
4785     else
4786         new_SV(dsv);
4787     SvUPGRADE(dsv, SVt_COW);
4788
4789     assert (SvPOK(ssv));
4790     assert (SvPOKp(ssv));
4791
4792     if (SvIsCOW(ssv)) {
4793         if (SvIsCOW_shared_hash(ssv)) {
4794             /* source is a COW shared hash key.  */
4795             DEBUG_C(PerlIO_printf(Perl_debug_log,
4796                                   "Fast copy on write: Sharing hash\n"));
4797             new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv))));
4798             goto common_exit;
4799         }
4800         else if (SvIsCOW_static(ssv)) {
4801             /* source is static constant; preserve this */
4802             new_pv = SvPVX(ssv);
4803             new_flags |= SVppv_STATIC;
4804             goto common_exit;
4805         }
4806         assert(SvCUR(ssv)+1 < SvLEN(ssv));
4807         assert(CowREFCNT(ssv) < SV_COW_REFCNT_MAX);
4808     } else {
4809         assert ((SvFLAGS(ssv) & CAN_COW_MASK) == CAN_COW_FLAGS);
4810         SvUPGRADE(ssv, SVt_COW);
4811         SvIsCOW_on(ssv);
4812         DEBUG_C(PerlIO_printf(Perl_debug_log,
4813                               "Fast copy on write: Converting ssv to COW\n"));
4814         CowREFCNT(ssv) = 0;
4815     }
4816 #  ifdef PERL_DEBUG_READONLY_COW
4817     if (already) sv_buf_to_rw(ssv);
4818 #  endif
4819     CowREFCNT(ssv)++;
4820     new_pv = SvPVX_mutable(ssv);
4821     sv_buf_to_ro(ssv);
4822
4823   common_exit:
4824     SvPV_set(dsv, new_pv);
4825     SvFLAGS(dsv) = new_flags;
4826     if (SvUTF8(ssv))
4827         SvUTF8_on(dsv);
4828     SvLEN_set(dsv, len);
4829     SvCUR_set(dsv, cur);
4830 #ifdef DEBUGGING
4831     if (DEBUG_C_TEST)
4832                 sv_dump(dsv);
4833 #endif
4834     return dsv;
4835 }
4836 #endif
4837
4838 /*
4839 =for apidoc sv_setpv_bufsize
4840
4841 Sets the SV to be a string of cur bytes length, with at least
4842 len bytes available. Ensures that there is a null byte at SvEND.
4843 Returns a char * pointer to the SvPV buffer.
4844
4845 =cut
4846 */
4847
4848 char *
4849 Perl_sv_setpv_bufsize(pTHX_ SV *const sv, const STRLEN cur, const STRLEN len)
4850 {
4851     char *pv;
4852
4853     PERL_ARGS_ASSERT_SV_SETPV_BUFSIZE;
4854
4855     SV_CHECK_THINKFIRST_COW_DROP(sv);
4856     SvUPGRADE(sv, SVt_PV);
4857     pv = SvGROW(sv, len + 1);
4858     SvCUR_set(sv, cur);
4859     *(SvEND(sv))= '\0';
4860     (void)SvPOK_only_UTF8(sv);                /* validate pointer */
4861
4862     SvTAINT(sv);
4863     if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4864     return pv;
4865 }
4866
4867 /*
4868 =for apidoc            sv_setpv
4869 =for apidoc_item       sv_setpv_mg
4870 =for apidoc_item       sv_setpvn
4871 =for apidoc_item       sv_setpvn_fresh
4872 =for apidoc_item       sv_setpvn_mg
4873 =for apidoc_item |void|sv_setpvs|SV* sv|"literal string"
4874 =for apidoc_item |void|sv_setpvs_mg|SV* sv|"literal string"
4875
4876 These copy a string into the SV C<sv>, making sure it is C<L</SvPOK_only>>.
4877
4878 In the C<pvs> forms, the string must be a C literal string, enclosed in double
4879 quotes.
4880
4881 In the C<pvn> forms, the first byte of the string is pointed to by C<ptr>, and
4882 C<len> indicates the number of bytes to be copied, potentially including
4883 embedded C<NUL> characters.
4884
4885 In the plain C<pv> forms, C<ptr> points to a NUL-terminated C string.  That is,
4886 it points to the first byte of the string, and the copy proceeds up through the
4887 first enountered C<NUL> byte.
4888
4889 In the forms that take a C<ptr> argument, if it is NULL, the SV will become
4890 undefined.
4891
4892 The UTF-8 flag is not changed by these functions.  A terminating NUL byte is
4893 guaranteed in the result.
4894
4895 The C<_mg> forms handle 'set' magic; the other forms skip all magic.
4896
4897 C<sv_setpvn_fresh> is a cut-down alternative to C<sv_setpvn>, intended ONLY
4898 to be used with a fresh sv that has been upgraded to a SVt_PV, SVt_PVIV,
4899 SVt_PVNV, or SVt_PVMG.
4900
4901 =cut
4902 */
4903
4904 void
4905 Perl_sv_setpvn(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4906 {
4907     char *dptr;
4908
4909     PERL_ARGS_ASSERT_SV_SETPVN;
4910
4911     SV_CHECK_THINKFIRST_COW_DROP(sv);
4912     if (isGV_with_GP(sv))
4913         Perl_croak_no_modify();
4914     if (!ptr) {
4915         (void)SvOK_off(sv);
4916         return;
4917     }
4918     else {
4919         /* len is STRLEN which is unsigned, need to copy to signed */
4920         const IV iv = len;
4921         if (iv < 0)
4922             Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen %"
4923                        IVdf, iv);
4924     }
4925     SvUPGRADE(sv, SVt_PV);
4926
4927     dptr = SvGROW(sv, len + 1);
4928     Move(ptr,dptr,len,char);
4929     dptr[len] = '\0';
4930     SvCUR_set(sv, len);
4931     (void)SvPOK_only_UTF8(sv);          /* validate pointer */
4932     SvTAINT(sv);
4933     if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4934 }
4935
4936 void
4937 Perl_sv_setpvn_mg(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4938 {
4939     PERL_ARGS_ASSERT_SV_SETPVN_MG;
4940
4941     sv_setpvn(sv,ptr,len);
4942     SvSETMAGIC(sv);
4943 }
4944
4945 void
4946 Perl_sv_setpvn_fresh(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4947 {
4948     char *dptr;
4949
4950     PERL_ARGS_ASSERT_SV_SETPVN_FRESH;
4951     assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
4952     assert(!SvTHINKFIRST(sv));
4953     assert(!isGV_with_GP(sv));
4954
4955     if (ptr) {
4956         const IV iv = len;
4957         /* len is STRLEN which is unsigned, need to copy to signed */
4958         if (iv < 0)
4959             Perl_croak(aTHX_ "panic: sv_setpvn_fresh called with negative strlen %"
4960                        IVdf, iv);
4961
4962         dptr = sv_grow_fresh(sv, len + 1);
4963         Move(ptr,dptr,len,char);
4964         dptr[len] = '\0';
4965         SvCUR_set(sv, len);
4966         SvPOK_on(sv);
4967         SvTAINT(sv);
4968     }
4969 }
4970
4971 void
4972 Perl_sv_setpv(pTHX_ SV *const sv, const char *const ptr)
4973 {
4974     STRLEN len;
4975
4976     PERL_ARGS_ASSERT_SV_SETPV;
4977
4978     SV_CHECK_THINKFIRST_COW_DROP(sv);
4979     if (!ptr) {
4980         (void)SvOK_off(sv);
4981         return;
4982     }
4983     len = strlen(ptr);
4984     SvUPGRADE(sv, SVt_PV);
4985
4986     SvGROW(sv, len + 1);
4987     Move(ptr,SvPVX(sv),len+1,char);
4988     SvCUR_set(sv, len);
4989     (void)SvPOK_only_UTF8(sv);          /* validate pointer */
4990     SvTAINT(sv);
4991     if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4992 }
4993
4994 void
4995 Perl_sv_setpv_mg(pTHX_ SV *const sv, const char *const ptr)
4996 {
4997     PERL_ARGS_ASSERT_SV_SETPV_MG;
4998
4999     sv_setpv(sv,ptr);
5000     SvSETMAGIC(sv);
5001 }
5002
5003 void
5004 Perl_sv_sethek(pTHX_ SV *const sv, const HEK *const hek)
5005 {
5006     PERL_ARGS_ASSERT_SV_SETHEK;
5007
5008     if (!hek) {
5009         return;
5010     }
5011
5012     if (HEK_LEN(hek) == HEf_SVKEY) {
5013         sv_setsv(sv, *(SV**)HEK_KEY(hek));
5014         return;
5015     } else {
5016         const int flags = HEK_FLAGS(hek);
5017         if (flags & HVhek_WASUTF8) {
5018             STRLEN utf8_len = HEK_LEN(hek);
5019             char *as_utf8 = (char *)bytes_to_utf8((U8*)HEK_KEY(hek), &utf8_len);
5020             sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
5021             SvUTF8_on(sv);
5022             return;
5023         } else if (flags & HVhek_NOTSHARED) {
5024             sv_setpvn(sv, HEK_KEY(hek), HEK_LEN(hek));
5025             if (HEK_UTF8(hek))
5026                 SvUTF8_on(sv);
5027             else SvUTF8_off(sv);
5028             return;
5029         }
5030         {
5031             SV_CHECK_THINKFIRST_COW_DROP(sv);
5032             SvUPGRADE(sv, SVt_PV);
5033             SvPV_free(sv);
5034             SvPV_set(sv,(char *)HEK_KEY(share_hek_hek(hek)));
5035             SvCUR_set(sv, HEK_LEN(hek));
5036             SvLEN_set(sv, 0);
5037             SvIsCOW_on(sv);
5038             SvPOK_on(sv);
5039             if (HEK_UTF8(hek))
5040                 SvUTF8_on(sv);
5041             else SvUTF8_off(sv);
5042             return;
5043         }
5044     }
5045 }
5046
5047
5048 /*
5049 =for apidoc      sv_usepvn
5050 =for apidoc_item sv_usepvn_flags
5051 =for apidoc_item sv_usepvn_mg
5052
5053 These tell an SV to use C<ptr> for its string value.  Normally SVs have
5054 their string stored inside the SV, but these tell the SV to use an
5055 external string instead.
5056
5057 C<ptr> should point to memory that was allocated
5058 by L</C<Newx>>.  It must be
5059 the start of a C<Newx>-ed block of memory, and not a pointer to the
5060 middle of it (beware of L<C<OOK>|perlguts/Offsets> and copy-on-write),
5061 and not be from a non-C<Newx> memory allocator like C<malloc>.  The
5062 string length, C<len>, must be supplied.  By default this function
5063 will L</C<Renew>> (i.e. realloc, move) the memory pointed to by C<ptr>,
5064 so that the pointer should not be freed or used by the programmer after giving
5065 it to C<sv_usepvn>, and neither should any pointers from "behind" that pointer
5066 (I<e.g.>, S<C<ptr> + 1>) be used.
5067
5068 In the C<sv_usepvn_flags> form, if S<C<flags & SV_SMAGIC>> is true,
5069 C<SvSETMAGIC> is called before returning.
5070 And if S<C<flags & SV_HAS_TRAILING_NUL>> is true, then C<ptr[len]> must be
5071 C<NUL>, and the realloc will be skipped (I<i.e.>, the buffer is actually at
5072 least 1 byte longer than C<len>, and already meets the requirements for storing
5073 in C<SvPVX>).
5074
5075 C<sv_usepvn> is merely C<sv_usepvn_flags> with C<flags> set to 0, so 'set'
5076 magic is skipped.
5077
5078 C<sv_usepvn_mg> is merely C<sv_usepvn_flags> with C<flags> set to C<SV_SMAGIC>,
5079 so 'set' magic is performed.
5080
5081 =for apidoc Amnh||SV_SMAGIC
5082 =for apidoc Amnh||SV_HAS_TRAILING_NUL
5083
5084 =cut
5085 */
5086
5087 void
5088 Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags)
5089 {
5090     STRLEN allocate;
5091
5092     PERL_ARGS_ASSERT_SV_USEPVN_FLAGS;
5093
5094     SV_CHECK_THINKFIRST_COW_DROP(sv);
5095     SvUPGRADE(sv, SVt_PV);
5096     if (!ptr) {
5097         (void)SvOK_off(sv);
5098         if (flags & SV_SMAGIC)
5099             SvSETMAGIC(sv);
5100         return;
5101     }
5102     if (SvPVX_const(sv))
5103         SvPV_free(sv);
5104
5105 #ifdef DEBUGGING
5106     if (flags & SV_HAS_TRAILING_NUL)
5107         assert(ptr[len] == '\0');
5108 #endif
5109
5110     allocate = (flags & SV_HAS_TRAILING_NUL)
5111         ? len + 1 :
5112 #ifdef Perl_safesysmalloc_size
5113         len + 1;
5114 #else
5115         PERL_STRLEN_ROUNDUP(len + 1);
5116 #endif
5117     if (flags & SV_HAS_TRAILING_NUL) {
5118         /* It's long enough - do nothing.
5119            Specifically Perl_newCONSTSUB is relying on this.  */
5120     } else {
5121 #ifdef DEBUGGING
5122         /* Force a move to shake out bugs in callers.  */
5123         char *new_ptr = (char*)safemalloc(allocate);
5124         Copy(ptr, new_ptr, len, char);
5125         PoisonFree(ptr,len,char);
5126         Safefree(ptr);
5127         ptr = new_ptr;
5128 #else
5129         ptr = (char*) saferealloc (ptr, allocate);
5130 #endif
5131     }
5132 #ifdef Perl_safesysmalloc_size
5133     SvLEN_set(sv, Perl_safesysmalloc_size(ptr));
5134 #else
5135     SvLEN_set(sv, allocate);
5136 #endif
5137     SvCUR_set(sv, len);
5138     SvPV_set(sv, ptr);
5139     if (!(flags & SV_HAS_TRAILING_NUL)) {
5140         ptr[len] = '\0';
5141     }
5142     (void)SvPOK_only_UTF8(sv);          /* validate pointer */
5143     SvTAINT(sv);
5144     if (flags & SV_SMAGIC)
5145         SvSETMAGIC(sv);
5146 }
5147
5148
5149 static void
5150 S_sv_uncow(pTHX_ SV * const sv, const U32 flags)
5151 {
5152     assert(SvIsCOW(sv));
5153     {
5154 #ifdef PERL_ANY_COW
5155         const char * const pvx = SvPVX_const(sv);
5156         const STRLEN len = SvLEN(sv);
5157         const STRLEN cur = SvCUR(sv);
5158         const bool was_shared_hek = SvIsCOW_shared_hash(sv);
5159
5160 #ifdef DEBUGGING
5161         if (DEBUG_C_TEST) {
5162                 PerlIO_printf(Perl_debug_log,
5163                               "Copy on write: Force normal %ld\n",
5164                               (long) flags);
5165                 sv_dump(sv);
5166         }
5167 #endif
5168         SvIsCOW_off(sv);
5169 # ifdef PERL_COPY_ON_WRITE
5170         if (len) {
5171             /* Must do this first, since the CowREFCNT uses SvPVX and
5172             we need to write to CowREFCNT, or de-RO the whole buffer if we are
5173             the only owner left of the buffer. */
5174             sv_buf_to_rw(sv); /* NOOP if RO-ing not supported */
5175             {
5176                 U8 cowrefcnt = CowREFCNT(sv);
5177                 if(cowrefcnt != 0) {
5178                     cowrefcnt--;
5179                     CowREFCNT(sv) = cowrefcnt;
5180                     sv_buf_to_ro(sv);
5181                     goto copy_over;
5182                 }
5183             }
5184             /* Else we are the only owner of the buffer. */
5185         }
5186         else
5187 # endif
5188         {
5189             /* This SV doesn't own the buffer, so need to Newx() a new one:  */
5190             copy_over:
5191             SvPV_set(sv, NULL);
5192             SvCUR_set(sv, 0);
5193             SvLEN_set(sv, 0);
5194             if (flags & SV_COW_DROP_PV) {
5195                 /* OK, so we don't need to copy our buffer.  */
5196                 SvPOK_off(sv);
5197             } else {
5198                 SvGROW(sv, cur + 1);
5199                 Move(pvx,SvPVX(sv),cur,char);
5200                 SvCUR_set(sv, cur);
5201                 *SvEND(sv) = '\0';
5202             }
5203             if (was_shared_hek) {
5204                         unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5205             }
5206 #ifdef DEBUGGING
5207             if (DEBUG_C_TEST)
5208                 sv_dump(sv);
5209 #endif
5210         }
5211 #else
5212             const char * const pvx = SvPVX_const(sv);
5213             const STRLEN len = SvCUR(sv);
5214             SvIsCOW_off(sv);
5215             SvPV_set(sv, NULL);
5216             SvLEN_set(sv, 0);
5217             if (flags & SV_COW_DROP_PV) {
5218                 /* OK, so we don't need to copy our buffer.  */
5219                 SvPOK_off(sv);
5220             } else {
5221                 SvGROW(sv, len + 1);
5222                 Move(pvx,SvPVX(sv),len,char);
5223                 *SvEND(sv) = '\0';
5224             }
5225             unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5226 #endif
5227     }
5228 }
5229
5230
5231 /*
5232 =for apidoc sv_force_normal_flags
5233
5234 Undo various types of fakery on an SV, where fakery means
5235 "more than" a string: if the PV is a shared string, make
5236 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5237 an C<xpvmg>; if we're a copy-on-write scalar, this is the on-write time when
5238 we do the copy, and is also used locally; if this is a
5239 vstring, drop the vstring magic.  If C<SV_COW_DROP_PV> is set
5240 then a copy-on-write scalar drops its PV buffer (if any) and becomes
5241 C<SvPOK_off> rather than making a copy.  (Used where this
5242 scalar is about to be set to some other value.)  In addition,
5243 the C<flags> parameter gets passed to C<sv_unref_flags()>
5244 when unreffing.  C<sv_force_normal> calls this function
5245 with flags set to 0.
5246
5247 This function is expected to be used to signal to perl that this SV is
5248 about to be written to, and any extra book-keeping needs to be taken care
5249 of.  Hence, it croaks on read-only values.
5250
5251 =for apidoc Amnh||SV_COW_DROP_PV
5252
5253 =cut
5254 */
5255
5256 void
5257 Perl_sv_force_normal_flags(pTHX_ SV *const sv, const U32 flags)
5258 {
5259     PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS;
5260
5261     if (SvREADONLY(sv))
5262         Perl_croak_no_modify();
5263     else if (SvIsCOW(sv) && LIKELY(SvTYPE(sv) != SVt_PVHV))
5264         S_sv_uncow(aTHX_ sv, flags);
5265     if (SvROK(sv))
5266         sv_unref_flags(sv, flags);
5267     else if (SvFAKE(sv) && isGV_with_GP(sv))
5268         sv_unglob(sv, flags);
5269     else if (SvFAKE(sv) && isREGEXP(sv)) {
5270         /* Need to downgrade the REGEXP to a simple(r) scalar. This is analogous
5271            to sv_unglob. We only need it here, so inline it.  */
5272         const bool islv = SvTYPE(sv) == SVt_PVLV;
5273         const svtype new_type =
5274           islv ? SVt_NULL : SvMAGIC(sv) || SvSTASH(sv) ? SVt_PVMG : SVt_PV;
5275         SV *const temp = newSV_type(new_type);
5276         regexp *old_rx_body;
5277
5278         if (new_type == SVt_PVMG) {
5279             SvMAGIC_set(temp, SvMAGIC(sv));
5280             SvMAGIC_set(sv, NULL);
5281             SvSTASH_set(temp, SvSTASH(sv));
5282             SvSTASH_set(sv, NULL);
5283         }
5284         if (!islv)
5285             SvCUR_set(temp, SvCUR(sv));
5286         /* Remember that SvPVX is in the head, not the body. */
5287         assert(ReANY((REGEXP *)sv)->mother_re);
5288
5289         if (islv) {
5290             /* LV-as-regex has sv->sv_any pointing to an XPVLV body,
5291              * whose xpvlenu_rx field points to the regex body */
5292             XPV *xpv = (XPV*)(SvANY(sv));
5293             old_rx_body = xpv->xpv_len_u.xpvlenu_rx;
5294             xpv->xpv_len_u.xpvlenu_rx = NULL;
5295         }
5296         else
5297             old_rx_body = ReANY((REGEXP *)sv);
5298
5299         /* Their buffer is already owned by someone else. */
5300         if (flags & SV_COW_DROP_PV) {
5301             /* SvLEN is already 0.  For SVt_REGEXP, we have a brand new
5302                zeroed body.  For SVt_PVLV, we zeroed it above (len field
5303                a union with xpvlenu_rx) */
5304             assert(!SvLEN(islv ? sv : temp));
5305             sv->sv_u.svu_pv = 0;
5306         }
5307         else {
5308             sv->sv_u.svu_pv = savepvn(RX_WRAPPED((REGEXP *)sv), SvCUR(sv));
5309             SvLEN_set(islv ? sv : temp, SvCUR(sv)+1);
5310             SvPOK_on(sv);
5311         }
5312
5313         /* Now swap the rest of the bodies. */
5314
5315         SvFAKE_off(sv);
5316         if (!islv) {
5317             SvFLAGS(sv) &= ~SVTYPEMASK;
5318             SvFLAGS(sv) |= new_type;
5319             SvANY(sv) = SvANY(temp);
5320         }
5321
5322         SvFLAGS(temp) &= ~(SVTYPEMASK);
5323         SvFLAGS(temp) |= SVt_REGEXP|SVf_FAKE;
5324         SvANY(temp) = old_rx_body;
5325
5326         /* temp is now rebuilt as a correctly structured SVt_REGEXP, so this
5327          * will trigger a call to sv_clear() which will correctly free the
5328          * body. */
5329         SvREFCNT_dec_NN(temp);
5330     }
5331     else if (SvVOK(sv)) sv_unmagic(sv, PERL_MAGIC_vstring);
5332 }
5333
5334 /*
5335 =for apidoc sv_chop
5336
5337 Efficient removal of characters from the beginning of the string buffer.
5338 C<SvPOK(sv)>, or at least C<SvPOKp(sv)>, must be true and C<ptr> must be a
5339 pointer to somewhere inside the string buffer.  C<ptr> becomes the first
5340 character of the adjusted string.  Uses the C<OOK> hack.  On return, only
5341 C<SvPOK(sv)> and C<SvPOKp(sv)> among the C<OK> flags will be true.
5342
5343 Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
5344 refer to the same chunk of data.
5345
5346 The unfortunate similarity of this function's name to that of Perl's C<chop>
5347 operator is strictly coincidental.  This function works from the left;
5348 C<chop> works from the right.
5349
5350 =cut
5351 */
5352
5353 void
5354 Perl_sv_chop(pTHX_ SV *const sv, const char *const ptr)
5355 {
5356     STRLEN delta;
5357     STRLEN old_delta;
5358     U8 *p;
5359 #ifdef DEBUGGING
5360     const U8 *evacp;
5361     STRLEN evacn;
5362 #endif
5363     STRLEN max_delta;
5364
5365     PERL_ARGS_ASSERT_SV_CHOP;
5366
5367     if (!ptr || !SvPOKp(sv))
5368         return;
5369     delta = ptr - SvPVX_const(sv);
5370     if (!delta) {
5371         /* Nothing to do.  */
5372         return;
5373     }
5374     max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv);
5375     if (delta > max_delta)
5376         Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p",
5377                    ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta);
5378     /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), so don't use ptr any more */
5379     SV_CHECK_THINKFIRST(sv);
5380     SvPOK_only_UTF8(sv);
5381
5382     if (!SvOOK(sv)) {
5383         if (!SvLEN(sv)) { /* make copy of shared string */
5384             const char *pvx = SvPVX_const(sv);
5385             const STRLEN len = SvCUR(sv);
5386             SvGROW(sv, len + 1);
5387             Move(pvx,SvPVX(sv),len,char);
5388             *SvEND(sv) = '\0';
5389         }
5390         SvOOK_on(sv);
5391         old_delta = 0;
5392     } else {
5393         SvOOK_offset(sv, old_delta);
5394     }
5395     SvLEN_set(sv, SvLEN(sv) - delta);
5396     SvCUR_set(sv, SvCUR(sv) - delta);
5397     SvPV_set(sv, SvPVX(sv) + delta);
5398
5399     p = (U8 *)SvPVX_const(sv);
5400
5401 #ifdef DEBUGGING
5402     /* how many bytes were evacuated?  we will fill them with sentinel
5403        bytes, except for the part holding the new offset of course. */
5404     evacn = delta;
5405     if (old_delta)
5406         evacn += (old_delta < 0x100 ? 1 : 1 + sizeof(STRLEN));
5407     assert(evacn);
5408     assert(evacn <= delta + old_delta);
5409     evacp = p - evacn;
5410 #endif
5411
5412     /* This sets 'delta' to the accumulated value of all deltas so far */
5413     delta += old_delta;
5414     assert(delta);
5415
5416     /* If 'delta' fits in a byte, store it just prior to the new beginning of
5417      * the string; otherwise store a 0 byte there and store 'delta' just prior
5418      * to that, using as many bytes as a STRLEN occupies.  Thus it overwrites a
5419      * portion of the chopped part of the string */
5420     if (delta < 0x100) {
5421         *--p = (U8) delta;
5422     } else {
5423         *--p = 0;
5424         p -= sizeof(STRLEN);
5425         Copy((U8*)&delta, p, sizeof(STRLEN), U8);
5426     }
5427
5428 #ifdef DEBUGGING
5429     /* Fill the preceding buffer with sentinals to verify that no-one is
5430        using it.  */
5431     while (p > evacp) {
5432         --p;
5433         *p = (U8)PTR2UV(p);
5434     }
5435 #endif
5436 }
5437
5438 /*
5439 =for apidoc sv_catpvn
5440 =for apidoc_item sv_catpvn_flags
5441 =for apidoc_item sv_catpvn_mg
5442 =for apidoc_item sv_catpvn_nomg
5443
5444 These concatenate the C<len> bytes of the string beginning at C<ptr> onto the
5445 end of the string which is in C<dsv>.  The caller must make sure C<ptr>
5446 contains at least C<len> bytes.
5447
5448 For all but C<sv_catpvn_flags>, the string appended is assumed to be valid
5449 UTF-8 if the SV has the UTF-8 status set, and a string of bytes otherwise.
5450
5451 They differ in that:
5452
5453 C<sv_catpvn_mg> performs both 'get' and 'set' magic on C<dsv>.
5454
5455 C<sv_catpvn> performs only 'get' magic.
5456
5457 C<sv_catpvn_nomg> skips all magic.
5458
5459 C<sv_catpvn_flags> has an extra C<flags> parameter which allows you to specify
5460 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>) and
5461 to also override the UTF-8 handling.  By supplying the C<SV_CATBYTES> flag, the
5462 appended string is interpreted as plain bytes; by supplying instead the
5463 C<SV_CATUTF8> flag, it will be interpreted as UTF-8, and the C<dsv> will be
5464 upgraded to UTF-8 if necessary.
5465
5466 C<sv_catpvn>, C<sv_catpvn_mg>, and C<sv_catpvn_nomg> are implemented
5467 in terms of C<sv_catpvn_flags>.
5468
5469 =for apidoc Amnh||SV_CATUTF8
5470 =for apidoc Amnh||SV_CATBYTES
5471
5472 =cut
5473 */
5474
5475 void
5476 Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, const I32 flags)
5477 {
5478     STRLEN dlen;
5479     const char * const dstr = SvPV_force_flags(dsv, dlen, flags);
5480
5481     PERL_ARGS_ASSERT_SV_CATPVN_FLAGS;
5482     assert((flags & (SV_CATBYTES|SV_CATUTF8)) != (SV_CATBYTES|SV_CATUTF8));
5483
5484     if (!(flags & SV_CATBYTES) || !SvUTF8(dsv)) {
5485       if (flags & SV_CATUTF8 && !SvUTF8(dsv)) {
5486          sv_utf8_upgrade_flags_grow(dsv, 0, slen + 1);
5487          dlen = SvCUR(dsv);
5488       }
5489       else SvGROW(dsv, dlen + slen + 3);
5490       if (sstr == dstr)
5491         sstr = SvPVX_const(dsv);
5492       Move(sstr, SvPVX(dsv) + dlen, slen, char);
5493       SvCUR_set(dsv, SvCUR(dsv) + slen);
5494     }
5495     else {
5496         /* We inline bytes_to_utf8, to avoid an extra malloc. */
5497         const char * const send = sstr + slen;
5498         U8 *d;
5499
5500         /* Something this code does not account for, which I think is
5501            impossible; it would require the same pv to be treated as
5502            bytes *and* utf8, which would indicate a bug elsewhere. */
5503         assert(sstr != dstr);
5504
5505         SvGROW(dsv, dlen + slen * 2 + 3);
5506         d = (U8 *)SvPVX(dsv) + dlen;
5507
5508         while (sstr < send) {
5509             append_utf8_from_native_byte(*sstr, &d);
5510             sstr++;
5511         }
5512         SvCUR_set(dsv, d-(const U8 *)SvPVX(dsv));
5513     }
5514     *SvEND(dsv) = '\0';
5515     (void)SvPOK_only_UTF8(dsv);         /* validate pointer */
5516     SvTAINT(dsv);
5517     if (flags & SV_SMAGIC)
5518         SvSETMAGIC(dsv);
5519 }
5520
5521 /*
5522 =for apidoc sv_catsv
5523 =for apidoc_item sv_catsv_flags
5524 =for apidoc_item sv_catsv_mg
5525 =for apidoc_item sv_catsv_nomg
5526
5527 These concatenate the string from SV C<sstr> onto the end of the string in SV
5528 C<dsv>.  If C<sstr> is null, these are no-ops; otherwise only C<dsv> is
5529 modified.
5530
5531 They differ only in what magic they perform:
5532
5533 C<sv_catsv_mg> performs 'get' magic on both SVs before the copy, and 'set' magic
5534 on C<dsv> afterwards.
5535
5536 C<sv_catsv> performs just 'get' magic, on both SVs.
5537
5538 C<sv_catsv_nomg> skips all magic.
5539
5540 C<sv_catsv_flags> has an extra C<flags> parameter which allows you to use
5541 C<SV_GMAGIC> and/or C<SV_SMAGIC> to specify any combination of magic handling
5542 (although either both or neither SV will have 'get' magic applied to it.)
5543
5544 C<sv_catsv>, C<sv_catsv_mg>, and C<sv_catsv_nomg> are implemented
5545 in terms of C<sv_catsv_flags>.
5546
5547 =cut */
5548
5549 void
5550 Perl_sv_catsv_flags(pTHX_ SV *const dsv, SV *const sstr, const I32 flags)
5551 {
5552     PERL_ARGS_ASSERT_SV_CATSV_FLAGS;
5553
5554     if (sstr) {
5555         STRLEN slen;
5556         const char *spv = SvPV_flags_const(sstr, slen, flags);
5557         if (flags & SV_GMAGIC)
5558                 SvGETMAGIC(dsv);
5559         sv_catpvn_flags(dsv, spv, slen,
5560                             DO_UTF8(sstr) ? SV_CATUTF8 : SV_CATBYTES);
5561         if (flags & SV_SMAGIC)
5562                 SvSETMAGIC(dsv);
5563     }
5564 }
5565
5566 /*
5567 =for apidoc sv_catpv
5568 =for apidoc_item sv_catpv_flags
5569 =for apidoc_item sv_catpv_mg
5570 =for apidoc_item sv_catpv_nomg
5571
5572 These concatenate the C<NUL>-terminated string C<sstr> onto the end of the
5573 string which is in the SV.
5574 If the SV has the UTF-8 status set, then the bytes appended should be
5575 valid UTF-8.
5576
5577 They differ only in how they handle magic:
5578
5579 C<sv_catpv_mg> performs both 'get' and 'set' magic.
5580
5581 C<sv_catpv> performs only 'get' magic.
5582
5583 C<sv_catpv_nomg> skips all magic.
5584
5585 C<sv_catpv_flags> has an extra C<flags> parameter which allows you to specify
5586 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>), and
5587 to also override the UTF-8 handling.  By supplying the C<SV_CATUTF8> flag, the
5588 appended string is forced to be interpreted as UTF-8; by supplying instead the
5589 C<SV_CATBYTES> flag, it will be interpreted as just bytes.  Either the SV or
5590 the string appended will be upgraded to UTF-8 if necessary.
5591
5592 =cut
5593 */
5594
5595 void
5596 Perl_sv_catpv(pTHX_ SV *const dsv, const char *sstr)
5597 {
5598     STRLEN len;
5599     STRLEN tlen;
5600     char *junk;
5601
5602     PERL_ARGS_ASSERT_SV_CATPV;
5603
5604     if (!sstr)
5605         return;
5606     junk = SvPV_force(dsv, tlen);
5607     len = strlen(sstr);
5608     SvGROW(dsv, tlen + len + 1);
5609     if (sstr == junk)
5610         sstr = SvPVX_const(dsv);
5611     Move(sstr,SvPVX(dsv)+tlen,len+1,char);
5612     SvCUR_set(dsv, SvCUR(dsv) + len);
5613     (void)SvPOK_only_UTF8(dsv);         /* validate pointer */
5614     SvTAINT(dsv);
5615 }
5616
5617 void
5618 Perl_sv_catpv_flags(pTHX_ SV *dsv, const char *sstr, const I32 flags)
5619 {
5620     PERL_ARGS_ASSERT_SV_CATPV_FLAGS;
5621     sv_catpvn_flags(dsv, sstr, strlen(sstr), flags);
5622 }
5623
5624 void
5625 Perl_sv_catpv_mg(pTHX_ SV *const dsv, const char *const sstr)
5626 {
5627     PERL_ARGS_ASSERT_SV_CATPV_MG;
5628
5629     sv_catpv(dsv,sstr);
5630     SvSETMAGIC(dsv);
5631 }
5632
5633 /*
5634 =for apidoc newSV
5635
5636 Creates a new SV.  A non-zero C<len> parameter indicates the number of
5637 bytes of preallocated string space the SV should have.  An extra byte for a
5638 trailing C<NUL> is also reserved.  (C<SvPOK> is not set for the SV even if string
5639 space is allocated.)  The reference count for the new SV is set to 1.
5640
5641 In 5.9.3, C<newSV()> replaces the older C<NEWSV()> API, and drops the first
5642 parameter, I<x>, a debug aid which allowed callers to identify themselves.
5643 This aid has been superseded by a new build option, C<PERL_MEM_LOG> (see
5644 L<perlhacktips/PERL_MEM_LOG>).  The older API is still there for use in XS
5645 modules supporting older perls.
5646
5647 =cut
5648 */
5649
5650 SV *
5651 Perl_newSV(pTHX_ const STRLEN len)
5652 {
5653     SV *sv;
5654
5655     if (!len)
5656         new_SV(sv);
5657     else {
5658         sv = newSV_type(SVt_PV);
5659         sv_grow_fresh(sv, len + 1);
5660     }
5661     return sv;
5662 }
5663 /*
5664 =for apidoc sv_magicext
5665
5666 Adds magic to an SV, upgrading it if necessary.  Applies the
5667 supplied C<vtable> and returns a pointer to the magic added.
5668
5669 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5670 In particular, you can add magic to C<SvREADONLY> SVs, and add more than
5671 one instance of the same C<how>.
5672
5673 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5674 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5675 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5676 to contain an SV* and is stored as-is with its C<REFCNT> incremented.
5677
5678 (This is now used as a subroutine by C<sv_magic>.)
5679
5680 =cut
5681 */
5682 MAGIC *
5683 Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how,
5684                 const MGVTBL *const vtable, const char *const name, const I32 namlen)
5685 {
5686     MAGIC* mg;
5687
5688     PERL_ARGS_ASSERT_SV_MAGICEXT;
5689
5690     SvUPGRADE(sv, SVt_PVMG);
5691     Newxz(mg, 1, MAGIC);
5692     mg->mg_moremagic = SvMAGIC(sv);
5693     SvMAGIC_set(sv, mg);
5694
5695     /* Sometimes a magic contains a reference loop, where the sv and
5696        object refer to each other.  To prevent a reference loop that
5697        would prevent such objects being freed, we look for such loops
5698        and if we find one we avoid incrementing the object refcount.
5699
5700        Note we cannot do this to avoid self-tie loops as intervening RV must
5701        have its REFCNT incremented to keep it in existence.
5702
5703     */
5704     if (!obj || obj == sv ||
5705         how == PERL_MAGIC_arylen ||
5706         how == PERL_MAGIC_regdata ||
5707         how == PERL_MAGIC_regdatum ||
5708         how == PERL_MAGIC_symtab ||
5709         (SvTYPE(obj) == SVt_PVGV &&
5710             (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv
5711              || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv
5712              || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv)))
5713     {
5714         mg->mg_obj = obj;
5715     }
5716     else {
5717         mg->mg_obj = SvREFCNT_inc_simple(obj);
5718         mg->mg_flags |= MGf_REFCOUNTED;
5719     }
5720
5721     /* Normal self-ties simply pass a null object, and instead of
5722        using mg_obj directly, use the SvTIED_obj macro to produce a
5723        new RV as needed.  For glob "self-ties", we are tieing the PVIO
5724        with an RV obj pointing to the glob containing the PVIO.  In
5725        this case, to avoid a reference loop, we need to weaken the
5726        reference.
5727     */
5728
5729     if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5730         obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv)
5731     {
5732       sv_rvweaken(obj);
5733     }
5734
5735     mg->mg_type = how;
5736     mg->mg_len = namlen;
5737     if (name) {
5738         if (namlen > 0)
5739             mg->mg_ptr = savepvn(name, namlen);
5740         else if (namlen == HEf_SVKEY) {
5741             /* Yes, this is casting away const. This is only for the case of
5742                HEf_SVKEY. I think we need to document this aberation of the
5743                constness of the API, rather than making name non-const, as
5744                that change propagating outwards a long way.  */
5745             mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name);
5746         } else
5747             mg->mg_ptr = (char *) name;
5748     }
5749     mg->mg_virtual = (MGVTBL *) vtable;
5750
5751     mg_magical(sv);
5752     return mg;
5753 }
5754
5755 MAGIC *
5756 Perl_sv_magicext_mglob(pTHX_ SV *sv)
5757 {
5758     PERL_ARGS_ASSERT_SV_MAGICEXT_MGLOB;
5759     if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
5760         /* This sv is only a delegate.  //g magic must be attached to
5761            its target. */
5762         vivify_defelem(sv);
5763         sv = LvTARG(sv);
5764     }
5765     return sv_magicext(sv, NULL, PERL_MAGIC_regex_global,
5766                        &PL_vtbl_mglob, 0, 0);
5767 }
5768
5769 /*
5770 =for apidoc sv_magic
5771
5772 Adds magic to an SV.  First upgrades C<sv> to type C<SVt_PVMG> if
5773 necessary, then adds a new magic item of type C<how> to the head of the
5774 magic list.
5775
5776 See C<L</sv_magicext>> (which C<sv_magic> now calls) for a description of the
5777 handling of the C<name> and C<namlen> arguments.
5778
5779 You need to use C<sv_magicext> to add magic to C<SvREADONLY> SVs and also
5780 to add more than one instance of the same C<how>.
5781
5782 =cut
5783 */
5784
5785 void
5786 Perl_sv_magic(pTHX_ SV *const sv, SV *const obj, const int how,
5787              const char *const name, const I32 namlen)
5788 {
5789     const MGVTBL *vtable;
5790     MAGIC* mg;
5791     unsigned int flags;
5792     unsigned int vtable_index;
5793
5794     PERL_ARGS_ASSERT_SV_MAGIC;
5795
5796     if (how < 0 || (unsigned)how >= C_ARRAY_LENGTH(PL_magic_data)
5797         || ((flags = PL_magic_data[how]),
5798             (vtable_index = flags & PERL_MAGIC_VTABLE_MASK)
5799             > magic_vtable_max))
5800         Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5801
5802     /* PERL_MAGIC_ext is reserved for use by extensions not perl internals.
5803        Useful for attaching extension internal data to perl vars.
5804        Note that multiple extensions may clash if magical scalars
5805        etc holding private data from one are passed to another. */
5806
5807     vtable = (vtable_index == magic_vtable_max)
5808         ? NULL : PL_magic_vtables + vtable_index;
5809
5810     if (SvREADONLY(sv)) {
5811         if (
5812             !PERL_MAGIC_TYPE_READONLY_ACCEPTABLE(how)
5813            )
5814         {
5815             Perl_croak_no_modify();
5816         }
5817     }
5818     if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5819         if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5820             /* sv_magic() refuses to add a magic of the same 'how' as an
5821                existing one
5822              */
5823             if (how == PERL_MAGIC_taint)
5824                 mg->mg_len |= 1;
5825             return;
5826         }
5827     }
5828
5829     /* Rest of work is done else where */
5830     mg = sv_magicext(sv,obj,how,vtable,name,namlen);
5831
5832     switch (how) {
5833     case PERL_MAGIC_taint:
5834         mg->mg_len = 1;
5835         break;
5836     case PERL_MAGIC_ext:
5837     case PERL_MAGIC_dbfile:
5838         SvRMAGICAL_on(sv);
5839         break;
5840     }
5841 }
5842
5843 static int
5844 S_sv_unmagicext_flags(pTHX_ SV *const sv, const int type, MGVTBL *vtbl, const U32 flags)
5845 {
5846     MAGIC* mg;
5847     MAGIC** mgp;
5848
5849     assert(flags <= 1);
5850
5851     if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5852         return 0;
5853     mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic);
5854     for (mg = *mgp; mg; mg = *mgp) {
5855         const MGVTBL* const virt = mg->mg_virtual;
5856         if (mg->mg_type == type && (!flags || virt == vtbl)) {
5857             *mgp = mg->mg_moremagic;
5858             if (virt && virt->svt_free)
5859                 virt->svt_free(aTHX_ sv, mg);
5860             if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5861                 if (mg->mg_len > 0)
5862                     Safefree(mg->mg_ptr);
5863                 else if (mg->mg_len == HEf_SVKEY)
5864                     SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
5865                 else if (mg->mg_type == PERL_MAGIC_utf8)
5866                     Safefree(mg->mg_ptr);
5867             }
5868             if (mg->mg_flags & MGf_REFCOUNTED)
5869                 SvREFCNT_dec(mg->mg_obj);
5870             Safefree(mg);
5871         }
5872         else
5873             mgp = &mg->mg_moremagic;
5874     }
5875     if (SvMAGIC(sv)) {
5876         if (SvMAGICAL(sv))      /* if we're under save_magic, wait for restore_magic; */
5877             mg_magical(sv);     /*    else fix the flags now */
5878     }
5879     else
5880         SvMAGICAL_off(sv);
5881
5882     return 0;
5883 }
5884
5885 /*
5886 =for apidoc sv_unmagic
5887
5888 Removes all magic of type C<type> from an SV.
5889
5890 =cut
5891 */
5892
5893 int
5894 Perl_sv_unmagic(pTHX_ SV *const sv, const int type)
5895 {
5896     PERL_ARGS_ASSERT_SV_UNMAGIC;
5897     return S_sv_unmagicext_flags(aTHX_ sv, type, NULL, 0);
5898 }
5899
5900 /*
5901 =for apidoc sv_unmagicext
5902
5903 Removes all magic of type C<type> with the specified C<vtbl> from an SV.
5904
5905 =cut
5906 */
5907
5908 int
5909 Perl_sv_unmagicext(pTHX_ SV *const sv, const int type, MGVTBL *vtbl)
5910 {
5911     PERL_ARGS_ASSERT_SV_UNMAGICEXT;
5912     return S_sv_unmagicext_flags(aTHX_ sv, type, vtbl, 1);
5913 }
5914
5915 /*
5916 =for apidoc sv_rvweaken
5917
5918 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5919 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5920 push a back-reference to this RV onto the array of backreferences
5921 associated with that magic.  If the RV is magical, set magic will be
5922 called after the RV is cleared.  Silently ignores C<undef> and warns
5923 on already-weak references.
5924
5925 =cut
5926 */
5927
5928 SV *
5929 Perl_sv_rvweaken(pTHX_ SV *const sv)
5930 {
5931     SV *tsv;
5932
5933     PERL_ARGS_ASSERT_SV_RVWEAKEN;
5934
5935     if (!SvOK(sv))  /* let undefs pass */
5936         return sv;
5937     if (!SvROK(sv))
5938         Perl_croak(aTHX_ "Can't weaken a nonreference");
5939     else if (SvWEAKREF(sv)) {
5940         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
5941         return sv;
5942     }
5943     else if (SvREADONLY(sv)) croak_no_modify();
5944     tsv = SvRV(sv);
5945     Perl_sv_add_backref(aTHX_ tsv, sv);
5946     SvWEAKREF_on(sv);
5947     SvREFCNT_dec_NN(tsv);
5948     return sv;
5949 }
5950
5951 /*
5952 =for apidoc sv_rvunweaken
5953
5954 Unweaken a reference: Clear the C<SvWEAKREF> flag on this RV; remove
5955 the backreference to this RV from the array of backreferences
5956 associated with the target SV, increment the refcount of the target.
5957 Silently ignores C<undef> and warns on non-weak references.
5958
5959 =cut
5960 */
5961
5962 SV *
5963 Perl_sv_rvunweaken(pTHX_ SV *const sv)
5964 {
5965     SV *tsv;
5966
5967     PERL_ARGS_ASSERT_SV_RVUNWEAKEN;
5968
5969     if (!SvOK(sv)) /* let undefs pass */
5970         return sv;
5971     if (!SvROK(sv))
5972         Perl_croak(aTHX_ "Can't unweaken a nonreference");
5973     else if (!SvWEAKREF(sv)) {
5974         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is not weak");
5975         return sv;
5976     }
5977     else if (SvREADONLY(sv)) croak_no_modify();
5978
5979     tsv = SvRV(sv);
5980     SvWEAKREF_off(sv);
5981     SvROK_on(sv);
5982     SvREFCNT_inc_NN(tsv);
5983     Perl_sv_del_backref(aTHX_ tsv, sv);
5984     return sv;
5985 }
5986
5987 /*
5988 =for apidoc sv_get_backrefs
5989
5990 If C<sv> is the target of a weak reference then it returns the back
5991 references structure associated with the sv; otherwise return C<NULL>.
5992
5993 When returning a non-null result the type of the return is relevant. If it
5994 is an AV then the elements of the AV are the weak reference RVs which
5995 point at this item. If it is any other type then the item itself is the
5996 weak reference.
5997
5998 See also C<Perl_sv_add_backref()>, C<Perl_sv_del_backref()>,
5999 C<Perl_sv_kill_backrefs()>
6000
6001 =cut
6002 */
6003
6004 SV *
6005 Perl_sv_get_backrefs(SV *const sv)
6006 {
6007     SV *backrefs= NULL;
6008
6009     PERL_ARGS_ASSERT_SV_GET_BACKREFS;
6010
6011     /* find slot to store array or singleton backref */
6012
6013     if (SvTYPE(sv) == SVt_PVHV) {
6014         if (HvHasAUX(sv)) {
6015             struct xpvhv_aux * const iter = HvAUX((HV *)sv);
6016             backrefs = (SV *)iter->xhv_backreferences;
6017         }
6018     } else if (SvMAGICAL(sv)) {
6019         MAGIC *mg = mg_find(sv, PERL_MAGIC_backref);
6020         if (mg)
6021             backrefs = mg->mg_obj;
6022     }
6023     return backrefs;
6024 }
6025
6026 /* Give tsv backref magic if it hasn't already got it, then push a
6027  * back-reference to sv onto the array associated with the backref magic.
6028  *
6029  * As an optimisation, if there's only one backref and it's not an AV,
6030  * store it directly in the HvAUX or mg_obj slot, avoiding the need to
6031  * allocate an AV. (Whether the slot holds an AV tells us whether this is
6032  * active.)
6033  */
6034
6035 /* A discussion about the backreferences array and its refcount:
6036  *
6037  * The AV holding the backreferences is pointed to either as the mg_obj of
6038  * PERL_MAGIC_backref, or in the specific case of a HV, from the
6039  * xhv_backreferences field. The array is created with a refcount
6040  * of 2. This means that if during global destruction the array gets
6041  * picked on before its parent to have its refcount decremented by the
6042  * random zapper, it won't actually be freed, meaning it's still there for
6043  * when its parent gets freed.
6044  *
6045  * When the parent SV is freed, the extra ref is killed by
6046  * Perl_sv_kill_backrefs.  The other ref is killed, in the case of magic,
6047  * by mg_free() / MGf_REFCOUNTED, or for a hash, by Perl_hv_kill_backrefs.
6048  *
6049  * When a single backref SV is stored directly, it is not reference
6050  * counted.
6051  */
6052
6053 void
6054 Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv)
6055 {
6056     SV **svp;
6057     AV *av = NULL;
6058     MAGIC *mg = NULL;
6059
6060     PERL_ARGS_ASSERT_SV_ADD_BACKREF;
6061
6062     /* find slot to store array or singleton backref */
6063
6064     if (SvTYPE(tsv) == SVt_PVHV) {
6065         svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6066     } else {
6067         if (SvMAGICAL(tsv))
6068             mg = mg_find(tsv, PERL_MAGIC_backref);
6069         if (!mg)
6070             mg = sv_magicext(tsv, NULL, PERL_MAGIC_backref, &PL_vtbl_backref, NULL, 0);
6071         svp = &(mg->mg_obj);
6072     }
6073
6074     /* create or retrieve the array */
6075
6076     if (   (!*svp && SvTYPE(sv) == SVt_PVAV)
6077         || (*svp && SvTYPE(*svp) != SVt_PVAV)
6078     ) {
6079         /* create array */
6080         if (mg)
6081             mg->mg_flags |= MGf_REFCOUNTED;
6082         av = newAV();
6083         AvREAL_off(av);
6084         SvREFCNT_inc_simple_void_NN(av);
6085         /* av now has a refcnt of 2; see discussion above */
6086         av_extend(av, *svp ? 2 : 1);
6087         if (*svp) {
6088             /* move single existing backref to the array */
6089             AvARRAY(av)[++AvFILLp(av)] = *svp; /* av_push() */
6090         }
6091         *svp = (SV*)av;
6092     }
6093     else {
6094         av = MUTABLE_AV(*svp);
6095         if (!av) {
6096             /* optimisation: store single backref directly in HvAUX or mg_obj */
6097             *svp = sv;
6098             return;
6099         }
6100         assert(SvTYPE(av) == SVt_PVAV);
6101         if (AvFILLp(av) >= AvMAX(av)) {
6102             av_extend(av, AvFILLp(av)+1);
6103         }
6104     }
6105     /* push new backref */
6106     AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
6107 }
6108
6109 /* delete a back-reference to ourselves from the backref magic associated
6110  * with the SV we point to.
6111  */
6112
6113 void
6114 Perl_sv_del_backref(pTHX_ SV *const tsv, SV *const sv)
6115 {
6116     SV **svp = NULL;
6117
6118     PERL_ARGS_ASSERT_SV_DEL_BACKREF;
6119
6120     if (SvTYPE(tsv) == SVt_PVHV) {
6121         if (HvHasAUX(tsv))
6122             svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6123     }
6124     else if (SvIS_FREED(tsv) && PL_phase == PERL_PHASE_DESTRUCT) {
6125         /* It's possible for the the last (strong) reference to tsv to have
6126            become freed *before* the last thing holding a weak reference.
6127            If both survive longer than the backreferences array, then when
6128            the referent's reference count drops to 0 and it is freed, it's
6129            not able to chase the backreferences, so they aren't NULLed.
6130
6131            For example, a CV holds a weak reference to its stash. If both the
6132            CV and the stash survive longer than the backreferences array,
6133            and the CV gets picked for the SvBREAK() treatment first,
6134            *and* it turns out that the stash is only being kept alive because
6135            of an our variable in the pad of the CV, then midway during CV
6136            destruction the stash gets freed, but CvSTASH() isn't set to NULL.
6137            It ends up pointing to the freed HV. Hence it's chased in here, and
6138            if this block wasn't here, it would hit the !svp panic just below.
6139
6140            I don't believe that "better" destruction ordering is going to help
6141            here - during global destruction there's always going to be the
6142            chance that something goes out of order. We've tried to make it
6143            foolproof before, and it only resulted in evolutionary pressure on
6144            fools. Which made us look foolish for our hubris. :-(
6145         */
6146         return;
6147     }
6148     else {
6149         MAGIC *const mg
6150             = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
6151         svp =  mg ? &(mg->mg_obj) : NULL;
6152     }
6153
6154     if (!svp)
6155         Perl_croak(aTHX_ "panic: del_backref, svp=0");
6156     if (!*svp) {
6157         /* It's possible that sv is being freed recursively part way through the
6158            freeing of tsv. If this happens, the backreferences array of tsv has
6159            already been freed, and so svp will be NULL. If this is the case,
6160            we should not panic. Instead, nothing needs doing, so return.  */
6161         if (PL_phase == PERL_PHASE_DESTRUCT && SvREFCNT(tsv) == 0)
6162             return;
6163         Perl_croak(aTHX_ "panic: del_backref, *svp=%p phase=%s refcnt=%" UVuf,
6164                    (void*)*svp, PL_phase_names[PL_phase], (UV)SvREFCNT(tsv));
6165     }
6166
6167     if (SvTYPE(*svp) == SVt_PVAV) {
6168 #ifdef DEBUGGING
6169         int count = 1;
6170 #endif
6171         AV * const av = (AV*)*svp;
6172         SSize_t fill;
6173         assert(!SvIS_FREED(av));
6174         fill = AvFILLp(av);
6175         assert(fill > -1);
6176         svp = AvARRAY(av);
6177         /* for an SV with N weak references to it, if all those
6178          * weak refs are deleted, then sv_del_backref will be called
6179          * N times and O(N^2) compares will be done within the backref
6180          * array. To ameliorate this potential slowness, we:
6181          * 1) make sure this code is as tight as possible;
6182          * 2) when looking for SV, look for it at both the head and tail of the
6183          *    array first before searching the rest, since some create/destroy
6184          *    patterns will cause the backrefs to be freed in order.
6185          */
6186         if (*svp == sv) {
6187             AvARRAY(av)++;
6188             AvMAX(av)--;
6189         }
6190         else {
6191             SV **p = &svp[fill];
6192             SV *const topsv = *p;
6193             if (topsv != sv) {
6194 #ifdef DEBUGGING
6195                 count = 0;
6196 #endif
6197                 while (--p > svp) {
6198                     if (*p == sv) {
6199                         /* We weren't the last entry.
6200                            An unordered list has this property that you
6201                            can take the last element off the end to fill
6202                            the hole, and it's still an unordered list :-)
6203                         */
6204                         *p = topsv;
6205 #ifdef DEBUGGING
6206                         count++;
6207 #else
6208                         break; /* should only be one */
6209 #endif
6210                     }
6211                 }
6212             }
6213         }
6214         assert(count ==1);
6215         AvFILLp(av) = fill-1;
6216     }
6217     else if (SvIS_FREED(*svp) && PL_phase == PERL_PHASE_DESTRUCT) {
6218         /* freed AV; skip */
6219     }
6220     else {
6221         /* optimisation: only a single backref, stored directly */
6222         if (*svp != sv)
6223             Perl_croak(aTHX_ "panic: del_backref, *svp=%p, sv=%p",
6224                        (void*)*svp, (void*)sv);
6225         *svp = NULL;
6226     }
6227
6228 }
6229
6230 void
6231 Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av)
6232 {
6233     SV **svp;
6234     SV **last;
6235     bool is_array;
6236
6237     PERL_ARGS_ASSERT_SV_KILL_BACKREFS;
6238
6239     if (!av)
6240         return;
6241
6242     /* after multiple passes through Perl_sv_clean_all() for a thingy
6243      * that has badly leaked, the backref array may have gotten freed,
6244      * since we only protect it against 1 round of cleanup */
6245     if (SvIS_FREED(av)) {
6246         if (PL_in_clean_all) /* All is fair */
6247             return;
6248         Perl_croak(aTHX_
6249                    "panic: magic_killbackrefs (freed backref AV/SV)");
6250     }
6251
6252
6253     is_array = (SvTYPE(av) == SVt_PVAV);
6254     if (is_array) {
6255         assert(!SvIS_FREED(av));
6256         svp = AvARRAY(av);
6257         if (svp)
6258             last = svp + AvFILLp(av);
6259     }
6260     else {
6261         /* optimisation: only a single backref, stored directly */
6262         svp = (SV**)&av;
6263         last = svp;
6264     }
6265
6266     if (svp) {
6267         while (svp <= last) {
6268             if (*svp) {
6269                 SV *const referrer = *svp;
6270                 if (SvWEAKREF(referrer)) {
6271                     /* XXX Should we check that it hasn't changed? */
6272                     assert(SvROK(referrer));
6273                     SvRV_set(referrer, 0);
6274                     SvOK_off(referrer);
6275                     SvWEAKREF_off(referrer);
6276                     SvSETMAGIC(referrer);
6277                 } else if (SvTYPE(referrer) == SVt_PVGV ||
6278                            SvTYPE(referrer) == SVt_PVLV) {
6279                     assert(SvTYPE(sv) == SVt_PVHV); /* stash backref */
6280                     /* You lookin' at me?  */
6281                     assert(GvSTASH(referrer));
6282                     assert(GvSTASH(referrer) == (const HV *)sv);
6283                     GvSTASH(referrer) = 0;
6284                 } else if (SvTYPE(referrer) == SVt_PVCV ||
6285                            SvTYPE(referrer) == SVt_PVFM) {
6286                     if (SvTYPE(sv) == SVt_PVHV) { /* stash backref */
6287                         /* You lookin' at me?  */
6288                         assert(CvSTASH(referrer));
6289                         assert(CvSTASH(referrer) == (const HV *)sv);
6290                         SvANY(MUTABLE_CV(referrer))->xcv_stash = 0;
6291                     }
6292                     else {
6293                         assert(SvTYPE(sv) == SVt_PVGV);
6294                         /* You lookin' at me?  */
6295                         assert(CvGV(referrer));
6296                         assert(CvGV(referrer) == (const GV *)sv);
6297                         anonymise_cv_maybe(MUTABLE_GV(sv),
6298                                                 MUTABLE_CV(referrer));
6299                     }
6300
6301                 } else {
6302                     Perl_croak(aTHX_
6303                                "panic: magic_killbackrefs (flags=%" UVxf ")",
6304                                (UV)SvFLAGS(referrer));
6305                 }
6306
6307                 if (is_array)
6308                     *svp = NULL;
6309             }
6310             svp++;
6311         }
6312     }
6313     if (is_array) {
6314         AvFILLp(av) = -1;
6315         SvREFCNT_dec_NN(av); /* remove extra count added by sv_add_backref() */
6316     }
6317     return;
6318 }
6319
6320 /*
6321 =for apidoc sv_insert
6322
6323 Inserts and/or replaces a string at the specified offset/length within the SV.
6324 Similar to the Perl C<substr()> function, with C<littlelen> bytes starting at
6325 C<little> replacing C<len> bytes of the string in C<bigstr> starting at
6326 C<offset>.  Handles get magic.
6327
6328 =for apidoc sv_insert_flags
6329
6330 Same as C<sv_insert>, but the extra C<flags> are passed to the
6331 C<SvPV_force_flags> that applies to C<bigstr>.
6332
6333 =cut
6334 */
6335
6336 void
6337 Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *little, const STRLEN littlelen, const U32 flags)
6338 {
6339     char *big;
6340     char *mid;
6341     char *midend;
6342     char *bigend;
6343     SSize_t i;          /* better be sizeof(STRLEN) or bad things happen */
6344     STRLEN curlen;
6345
6346     PERL_ARGS_ASSERT_SV_INSERT_FLAGS;
6347
6348     SvPV_force_flags(bigstr, curlen, flags);
6349     (void)SvPOK_only_UTF8(bigstr);
6350
6351     if (little >= SvPVX(bigstr) &&
6352         little < SvPVX(bigstr) + (SvLEN(bigstr) ? SvLEN(bigstr) : SvCUR(bigstr))) {
6353         /* little is a pointer to within bigstr, since we can reallocate bigstr,
6354            or little...little+littlelen might overlap offset...offset+len we make a copy
6355         */
6356         little = savepvn(little, littlelen);
6357         SAVEFREEPV(little);
6358     }
6359
6360     if (offset + len > curlen) {
6361         SvGROW(bigstr, offset+len+1);
6362         Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6363         SvCUR_set(bigstr, offset+len);
6364     }
6365
6366     SvTAINT(bigstr);
6367     i = littlelen - len;
6368     if (i > 0) {                        /* string might grow */
6369         big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
6370         mid = big + offset + len;
6371         midend = bigend = big + SvCUR(bigstr);
6372         bigend += i;
6373         *bigend = '\0';
6374         while (midend > mid)            /* shove everything down */
6375             *--bigend = *--midend;
6376         Move(little,big+offset,littlelen,char);
6377         SvCUR_set(bigstr, SvCUR(bigstr) + i);
6378         SvSETMAGIC(bigstr);
6379         return;
6380     }
6381     else if (i == 0) {
6382         Move(little,SvPVX(bigstr)+offset,len,char);
6383         SvSETMAGIC(bigstr);
6384         return;
6385     }
6386
6387     big = SvPVX(bigstr);
6388     mid = big + offset;
6389     midend = mid + len;
6390     bigend = big + SvCUR(bigstr);
6391
6392     if (midend > bigend)
6393         Perl_croak(aTHX_ "panic: sv_insert, midend=%p, bigend=%p",
6394                    midend, bigend);
6395
6396     if (mid - big > bigend - midend) {  /* faster to shorten from end */
6397         if (littlelen) {
6398             Move(little, mid, littlelen,char);
6399             mid += littlelen;
6400         }
6401         i = bigend - midend;
6402         if (i > 0) {
6403             Move(midend, mid, i,char);
6404             mid += i;
6405         }
6406         *mid = '\0';
6407         SvCUR_set(bigstr, mid - big);
6408     }
6409     else if ((i = mid - big)) { /* faster from front */
6410         midend -= littlelen;
6411         mid = midend;
6412         Move(big, midend - i, i, char);
6413         sv_chop(bigstr,midend-i);
6414         if (littlelen)
6415             Move(little, mid, littlelen,char);
6416     }
6417     else if (littlelen) {
6418         midend -= littlelen;
6419         sv_chop(bigstr,midend);
6420         Move(little,midend,littlelen,char);
6421     }
6422     else {
6423         sv_chop(bigstr,midend);
6424     }
6425     SvSETMAGIC(bigstr);
6426 }
6427
6428 /*
6429 =for apidoc sv_replace
6430
6431 Make the first argument a copy of the second, then delete the original.
6432 The target SV physically takes over ownership of the body of the source SV
6433 and inherits its flags; however, the target keeps any magic it owns,
6434 and any magic in the source is discarded.
6435 Note that this is a rather specialist SV copying operation; most of the
6436 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
6437
6438 =cut
6439 */
6440
6441 void
6442 Perl_sv_replace(pTHX_ SV *const sv, SV *const nsv)
6443 {
6444     const U32 refcnt = SvREFCNT(sv);
6445
6446     PERL_ARGS_ASSERT_SV_REPLACE;
6447
6448     SV_CHECK_THINKFIRST_COW_DROP(sv);
6449     if (SvREFCNT(nsv) != 1) {
6450         Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()"
6451                    " (%" UVuf " != 1)", (UV) SvREFCNT(nsv));
6452     }
6453     if (SvMAGICAL(sv)) {
6454         if (SvMAGICAL(nsv))
6455             mg_free(nsv);
6456         else
6457             sv_upgrade(nsv, SVt_PVMG);
6458         SvMAGIC_set(nsv, SvMAGIC(sv));
6459         SvFLAGS(nsv) |= SvMAGICAL(sv);
6460         SvMAGICAL_off(sv);
6461         SvMAGIC_set(sv, NULL);
6462     }
6463     SvREFCNT(sv) = 0;
6464     sv_clear(sv);
6465     assert(!SvREFCNT(sv));
6466 #ifdef DEBUG_LEAKING_SCALARS
6467     sv->sv_flags  = nsv->sv_flags;
6468     sv->sv_any    = nsv->sv_any;
6469     sv->sv_refcnt = nsv->sv_refcnt;
6470     sv->sv_u      = nsv->sv_u;
6471 #else
6472     StructCopy(nsv,sv,SV);
6473 #endif
6474     if(SvTYPE(sv) == SVt_IV) {
6475         SET_SVANY_FOR_BODYLESS_IV(sv);
6476     }
6477
6478
6479     SvREFCNT(sv) = refcnt;
6480     SvFLAGS(nsv) |= SVTYPEMASK;         /* Mark as freed */
6481     SvREFCNT(nsv) = 0;
6482     del_SV(nsv);
6483 }
6484
6485 /* We're about to free a GV which has a CV that refers back to us.
6486  * If that CV will outlive us, make it anonymous (i.e. fix up its CvGV
6487  * field) */
6488
6489 STATIC void
6490 S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv)
6491 {
6492     SV *gvname;
6493     GV *anongv;
6494
6495     PERL_ARGS_ASSERT_ANONYMISE_CV_MAYBE;
6496
6497     /* be assertive! */
6498     assert(SvREFCNT(gv) == 0);
6499     assert(isGV(gv) && isGV_with_GP(gv));
6500     assert(GvGP(gv));
6501     assert(!CvANON(cv));
6502     assert(CvGV(cv) == gv);
6503     assert(!CvNAMED(cv));
6504
6505     /* will the CV shortly be freed by gp_free() ? */
6506     if (GvCV(gv) == cv && GvGP(gv)->gp_refcnt < 2 && SvREFCNT(cv) < 2) {
6507         SvANY(cv)->xcv_gv_u.xcv_gv = NULL;
6508         return;
6509     }
6510
6511     /* if not, anonymise: */
6512     gvname = (GvSTASH(gv) && HvNAME(GvSTASH(gv)) && HvENAME(GvSTASH(gv)))
6513                     ? newSVhek(HvENAME_HEK(GvSTASH(gv)))
6514                     : newSVpvn_flags( "__ANON__", 8, 0 );
6515     sv_catpvs(gvname, "::__ANON__");
6516     anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV);
6517     SvREFCNT_dec_NN(gvname);
6518
6519     CvANON_on(cv);
6520     CvCVGV_RC_on(cv);
6521     SvANY(cv)->xcv_gv_u.xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv));
6522 }
6523
6524
6525 /*
6526 =for apidoc sv_clear
6527
6528 Clear an SV: call any destructors, free up any memory used by the body,
6529 and free the body itself.  The SV's head is I<not> freed, although
6530 its type is set to all 1's so that it won't inadvertently be assumed
6531 to be live during global destruction etc.
6532 This function should only be called when C<REFCNT> is zero.  Most of the time
6533 you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
6534 instead.
6535
6536 =cut
6537 */
6538
6539 void
6540 Perl_sv_clear(pTHX_ SV *const orig_sv)
6541 {
6542     SV* iter_sv = NULL;
6543     SV* next_sv = NULL;
6544     SV *sv = orig_sv;
6545     STRLEN hash_index = 0; /* initialise to make Coverity et al happy.
6546                               Not strictly necessary */
6547
6548     PERL_ARGS_ASSERT_SV_CLEAR;
6549
6550     /* within this loop, sv is the SV currently being freed, and
6551      * iter_sv is the most recent AV or whatever that's being iterated
6552      * over to provide more SVs */
6553
6554     while (sv) {
6555         U32 type = SvTYPE(sv);
6556         HV *stash;
6557
6558         assert(SvREFCNT(sv) == 0);
6559         assert(SvTYPE(sv) != (svtype)SVTYPEMASK);
6560
6561         if (type <= SVt_IV) {
6562             /* Historically this check on type was needed so that the code to
6563              * free bodies wasn't reached for these types, because the arena
6564              * slots were re-used for HEs and pointer table entries. The
6565              * metadata table `bodies_by_type` had the information for the sizes
6566              * for HEs and PTEs, hence the code here had to have a special-case
6567              * check to ensure that the "regular" body freeing code wasn't
6568              * reached, and get confused by the "lies" in `bodies_by_type`.
6569              *
6570              * However, it hasn't actually been needed for that reason since
6571              * Aug 2010 (commit 829cd18aa7f45221), because `bodies_by_type` was
6572              * changed to always hold the accurate metadata for the SV types.
6573              * This was possible because PTEs were no longer allocated from the
6574              * "SVt_IV" arena, and the code to allocate HEs from the "SVt_NULL"
6575              * arena is entirely in hv.c, so doesn't access the table.
6576              *
6577              * Some sort of check is still needed to handle SVt_IVs - pure RVs
6578              * need to take one code path which is common with RVs stored in
6579              * SVt_PV (or larger), but pure IVs mustn't take the "PV but not RV"
6580              * path, as SvPVX() doesn't point to valid memory.
6581              *
6582              * Hence this code is still the most efficient way to handle this.
6583              */
6584
6585             if (SvROK(sv))
6586                 goto free_rv;
6587             SvFLAGS(sv) &= SVf_BREAK;
6588             SvFLAGS(sv) |= SVTYPEMASK;
6589             goto free_head;
6590         }
6591
6592         /* objs are always >= MG, but pad names use the SVs_OBJECT flag
6593            for another purpose  */
6594         assert(!SvOBJECT(sv) || type >= SVt_PVMG);
6595
6596         if (type >= SVt_PVMG) {
6597             if (SvOBJECT(sv)) {
6598                 if (!curse(sv, 1)) goto get_next_sv;
6599                 type = SvTYPE(sv); /* destructor may have changed it */
6600             }
6601             /* Free back-references before magic, in case the magic calls
6602              * Perl code that has weak references to sv. */
6603             if (type == SVt_PVHV) {
6604                 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
6605                 if (SvMAGIC(sv))
6606                     mg_free(sv);
6607             }
6608             else if (SvMAGIC(sv)) {
6609                 /* Free back-references before other types of magic. */
6610                 sv_unmagic(sv, PERL_MAGIC_backref);
6611                 mg_free(sv);
6612             }
6613             SvMAGICAL_off(sv);
6614         }
6615         switch (type) {
6616             /* case SVt_INVLIST: */
6617         case SVt_PVIO:
6618             if (IoIFP(sv) &&
6619                 IoIFP(sv) != PerlIO_stdin() &&
6620                 IoIFP(sv) != PerlIO_stdout() &&
6621                 IoIFP(sv) != PerlIO_stderr() &&
6622                 !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6623             {
6624                 io_close(MUTABLE_IO(sv), NULL, FALSE,
6625                          (IoTYPE(sv) == IoTYPE_WRONLY ||
6626                           IoTYPE(sv) == IoTYPE_RDWR   ||
6627                           IoTYPE(sv) == IoTYPE_APPEND));
6628             }
6629             if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6630                 PerlDir_close(IoDIRP(sv));
6631             IoDIRP(sv) = (DIR*)NULL;
6632             Safefree(IoTOP_NAME(sv));
6633             Safefree(IoFMT_NAME(sv));
6634             Safefree(IoBOTTOM_NAME(sv));
6635             if ((const GV *)sv == PL_statgv)
6636                 PL_statgv = NULL;
6637             goto freescalar;
6638         case SVt_REGEXP:
6639             /* FIXME for plugins */
6640             pregfree2((REGEXP*) sv);
6641             goto freescalar;
6642         case SVt_PVCV:
6643         case SVt_PVFM:
6644             cv_undef(MUTABLE_CV(sv));
6645             /* If we're in a stash, we don't own a reference to it.
6646              * However it does have a back reference to us, which needs to
6647              * be cleared.  */
6648             if ((stash = CvSTASH(sv)))
6649                 sv_del_backref(MUTABLE_SV(stash), sv);
6650             goto freescalar;
6651         case SVt_PVHV:
6652             if (HvTOTALKEYS((HV*)sv) > 0) {
6653                 const HEK *hek;
6654                 /* this statement should match the one at the beginning of
6655                  * hv_undef_flags() */
6656                 if (   PL_phase != PERL_PHASE_DESTRUCT
6657                     && (hek = HvNAME_HEK((HV*)sv)))
6658                 {
6659                     if (PL_stashcache) {
6660                         DEBUG_o(Perl_deb(aTHX_
6661                             "sv_clear clearing PL_stashcache for '%" HEKf
6662                             "'\n",
6663                              HEKfARG(hek)));
6664                         (void)hv_deletehek(PL_stashcache,
6665                                            hek, G_DISCARD);
6666                     }
6667                     hv_name_set((HV*)sv, NULL, 0, 0);
6668                 }
6669
6670                 /* save old iter_sv in unused SvSTASH field */
6671                 assert(!SvOBJECT(sv));
6672                 SvSTASH(sv) = (HV*)iter_sv;
6673                 iter_sv = sv;
6674
6675                 /* save old hash_index in unused SvMAGIC field */
6676                 assert(!SvMAGICAL(sv));
6677                 assert(!SvMAGIC(sv));
6678                 ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index = hash_index;
6679                 hash_index = 0;
6680
6681                 next_sv = Perl_hfree_next_entry(aTHX_ (HV*)sv, &hash_index);
6682                 goto get_next_sv; /* process this new sv */
6683             }
6684             /* free empty hash */
6685             Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6686             assert(!HvARRAY((HV*)sv));
6687             break;
6688         case SVt_PVAV:
6689             {
6690                 AV* av = MUTABLE_AV(sv);
6691                 if (PL_comppad == av) {
6692                     PL_comppad = NULL;
6693                     PL_curpad = NULL;
6694                 }
6695                 if (AvREAL(av) && AvFILLp(av) > -1) {
6696                     next_sv = AvARRAY(av)[AvFILLp(av)--];
6697                     /* save old iter_sv in top-most slot of AV,
6698                      * and pray that it doesn't get wiped in the meantime */
6699                     AvARRAY(av)[AvMAX(av)] = iter_sv;
6700                     iter_sv = sv;
6701                     goto get_next_sv; /* process this new sv */
6702                 }
6703                 Safefree(AvALLOC(av));
6704             }
6705
6706             break;
6707         case SVt_PVLV:
6708             if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
6709                 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
6710                 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
6711                 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
6712             }
6713             else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV**  */
6714                 SvREFCNT_dec(LvTARG(sv));
6715             if (isREGEXP(sv)) {
6716                 /* This PVLV has had a REGEXP assigned to it - the memory
6717                  * normally used to store SvLEN instead points to a regex body.
6718                  * Retrieving the pointer to the regex body from the correct
6719                  * location is normally abstracted by ReANY(), which handles
6720                  * both SVt_PVLV and SVt_REGEXP
6721                  *
6722                  * This code is unwinding the storage specific to SVt_PVLV.
6723                  * We get the body pointer directly from the union, free it,
6724                  * then set SvLEN to whatever value was in the now-freed regex
6725                  * body. The PVX buffer is shared by multiple re's and only
6726                  * freed once, by the re whose SvLEN is non-null.
6727                  *
6728                  * Perl_sv_force_normal_flags() also has code to free this
6729                  * hidden body - it swaps the body into a temporary SV it has
6730                  * just allocated, then frees that SV. That causes execution
6731                  * to reach the SVt_REGEXP: case about 60 lines earlier in this
6732                  * function.
6733                  *
6734                  * See Perl_reg_temp_copy() for the code that sets up this
6735                  * REGEXP body referenced by the PVLV. */
6736                 struct regexp *r = ((XPV*)SvANY(sv))->xpv_len_u.xpvlenu_rx;
6737                 STRLEN len = r->xpv_len;
6738                 pregfree2((REGEXP*) sv);
6739                 del_body_by_type(r, SVt_REGEXP);
6740                 SvLEN_set((sv), len);
6741                 goto freescalar;
6742             }
6743             /* FALLTHROUGH */
6744         case SVt_PVGV:
6745             if (isGV_with_GP(sv)) {
6746                 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
6747                    && HvENAME_get(stash))
6748                     mro_method_changed_in(stash);
6749                 gp_free(MUTABLE_GV(sv));
6750                 if (GvNAME_HEK(sv))
6751                     unshare_hek(GvNAME_HEK(sv));
6752                 /* If we're in a stash, we don't own a reference to it.
6753                  * However it does have a back reference to us, which
6754                  * needs to be cleared.  */
6755                 if ((stash = GvSTASH(sv)))
6756                         sv_del_backref(MUTABLE_SV(stash), sv);
6757             }
6758             /* FIXME. There are probably more unreferenced pointers to SVs
6759              * in the interpreter struct that we should check and tidy in
6760              * a similar fashion to this:  */
6761             /* See also S_sv_unglob, which does the same thing. */
6762             if ((const GV *)sv == PL_last_in_gv)
6763                 PL_last_in_gv = NULL;
6764             else if ((const GV *)sv == PL_statgv)
6765                 PL_statgv = NULL;
6766             else if ((const GV *)sv == PL_stderrgv)
6767                 PL_stderrgv = NULL;
6768             /* FALLTHROUGH */
6769         case SVt_PVMG:
6770         case SVt_PVNV:
6771         case SVt_PVIV:
6772         case SVt_INVLIST:
6773         case SVt_PV:
6774           freescalar:
6775             /* Don't bother with SvOOK_off(sv); as we're only going to
6776              * free it.  */
6777             if (SvOOK(sv)) {
6778                 STRLEN offset;
6779                 SvOOK_offset(sv, offset);
6780                 SvPV_set(sv, SvPVX_mutable(sv) - offset);
6781                 /* Don't even bother with turning off the OOK flag.  */
6782             }
6783             if (SvROK(sv)) {
6784             free_rv:
6785                 {
6786                     SV * const target = SvRV(sv);
6787                     if (SvWEAKREF(sv))
6788                         sv_del_backref(target, sv);
6789                     else
6790                         next_sv = target;
6791                 }
6792             }
6793 #ifdef PERL_ANY_COW
6794             else if (SvPVX_const(sv)
6795                      && !(SvTYPE(sv) == SVt_PVIO
6796                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6797             {
6798                 if (SvIsCOW(sv)) {
6799 #ifdef DEBUGGING
6800                     if (DEBUG_C_TEST) {
6801                         PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
6802                         sv_dump(sv);
6803                     }
6804 #endif
6805                     if (SvIsCOW_static(sv)) {
6806                         SvLEN_set(sv, 0);
6807                     }
6808                     else if (SvIsCOW_shared_hash(sv)) {
6809                         unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6810                     }
6811                     else {
6812                         if (CowREFCNT(sv)) {
6813                             sv_buf_to_rw(sv);
6814                             CowREFCNT(sv)--;
6815                             sv_buf_to_ro(sv);
6816                             SvLEN_set(sv, 0);
6817                         }
6818                     }
6819                 }
6820                 if (SvLEN(sv)) {
6821                     Safefree(SvPVX_mutable(sv));
6822                 }
6823             }
6824 #else
6825             else if (SvPVX_const(sv) && SvLEN(sv)
6826                      && !(SvTYPE(sv) == SVt_PVIO
6827                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6828                 Safefree(SvPVX_mutable(sv));
6829             else if (SvPVX_const(sv) && SvIsCOW(sv)) {
6830                 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6831             }
6832 #endif
6833             break;
6834         case SVt_NV:
6835             break;
6836         }
6837
6838       free_body:
6839
6840         {
6841             U32 arena_index;
6842             const struct body_details *sv_type_details;
6843
6844             if (type == SVt_PVHV && HvHasAUX(sv)) {
6845                 arena_index = HVAUX_ARENA_ROOT_IX;
6846                 sv_type_details = &fake_hv_with_aux;
6847             }
6848             else {
6849                 arena_index = type;
6850                 sv_type_details = bodies_by_type + arena_index;
6851             }
6852
6853             SvFLAGS(sv) &= SVf_BREAK;
6854             SvFLAGS(sv) |= SVTYPEMASK;
6855
6856             if (sv_type_details->arena) {
6857                 del_body(((char *)SvANY(sv) + sv_type_details->offset),
6858                          &PL_body_roots[arena_index]);
6859             }
6860             else if (sv_type_details->body_size) {
6861                 safefree(SvANY(sv));
6862             }
6863         }
6864
6865       free_head:
6866         /* caller is responsible for freeing the head of the original sv */
6867         if (sv != orig_sv && !SvREFCNT(sv))
6868             del_SV(sv);
6869
6870         /* grab and free next sv, if any */
6871       get_next_sv:
6872         while (1) {
6873             sv = NULL;
6874             if (next_sv) {
6875                 sv = next_sv;
6876                 next_sv = NULL;
6877             }
6878             else if (!iter_sv) {
6879                 break;
6880             } else if (SvTYPE(iter_sv) == SVt_PVAV) {
6881                 AV *const av = (AV*)iter_sv;
6882                 if (AvFILLp(av) > -1) {
6883                     sv = AvARRAY(av)[AvFILLp(av)--];
6884                 }
6885                 else { /* no more elements of current AV to free */
6886                     sv = iter_sv;
6887                     type = SvTYPE(sv);
6888                     /* restore previous value, squirrelled away */
6889                     iter_sv = AvARRAY(av)[AvMAX(av)];
6890                     Safefree(AvALLOC(av));
6891                     goto free_body;
6892                 }
6893             } else if (SvTYPE(iter_sv) == SVt_PVHV) {
6894                 sv = Perl_hfree_next_entry(aTHX_ (HV*)iter_sv, &hash_index);
6895                 if (!sv && !HvTOTALKEYS((HV *)iter_sv)) {
6896                     /* no more elements of current HV to free */
6897                     sv = iter_sv;
6898                     type = SvTYPE(sv);
6899                     /* Restore previous values of iter_sv and hash_index,
6900                      * squirrelled away */
6901                     assert(!SvOBJECT(sv));
6902                     iter_sv = (SV*)SvSTASH(sv);
6903                     assert(!SvMAGICAL(sv));
6904                     hash_index = ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index;
6905 #ifdef DEBUGGING
6906                     /* perl -DA does not like rubbish in SvMAGIC. */
6907                     SvMAGIC_set(sv, 0);
6908 #endif
6909
6910                     /* free any remaining detritus from the hash struct */
6911                     Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6912                     assert(!HvARRAY((HV*)sv));
6913                     goto free_body;
6914                 }
6915             }
6916
6917             /* unrolled SvREFCNT_dec and sv_free2 follows: */
6918
6919             if (!sv)
6920                 continue;
6921             if (!SvREFCNT(sv)) {
6922                 sv_free(sv);
6923                 continue;
6924             }
6925             if (--(SvREFCNT(sv)))
6926                 continue;
6927 #ifdef DEBUGGING
6928             if (SvTEMP(sv)) {
6929                 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
6930                          "Attempt to free temp prematurely: SV 0x%" UVxf
6931                          pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6932                 continue;
6933             }
6934 #endif
6935             if (SvIMMORTAL(sv)) {
6936                 /* make sure SvREFCNT(sv)==0 happens very seldom */
6937                 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
6938                 continue;
6939             }
6940             break;
6941         } /* while 1 */
6942
6943     } /* while sv */
6944 }
6945
6946 /* This routine curses the sv itself, not the object referenced by sv. So
6947    sv does not have to be ROK. */
6948
6949 static bool
6950 S_curse(pTHX_ SV * const sv, const bool check_refcnt) {
6951     PERL_ARGS_ASSERT_CURSE;
6952     assert(SvOBJECT(sv));
6953
6954     if (PL_defstash &&  /* Still have a symbol table? */
6955         SvDESTROYABLE(sv))
6956     {
6957         dSP;
6958         HV* stash;
6959         do {
6960           stash = SvSTASH(sv);
6961           assert(SvTYPE(stash) == SVt_PVHV);
6962           if (HvNAME(stash)) {
6963             CV* destructor = NULL;
6964             struct mro_meta *meta;
6965
6966             assert (HvHasAUX(stash));
6967
6968             DEBUG_o( Perl_deb(aTHX_ "Looking for DESTROY method for %s\n",
6969                          HvNAME(stash)) );
6970
6971             /* don't make this an initialization above the assert, since it needs
6972                an AUX structure */
6973             meta = HvMROMETA(stash);
6974             if (meta->destroy_gen && meta->destroy_gen == PL_sub_generation) {
6975                 destructor = meta->destroy;
6976                 DEBUG_o( Perl_deb(aTHX_ "Using cached DESTROY method %p for %s\n",
6977                              (void *)destructor, HvNAME(stash)) );
6978             }
6979             else {
6980                 bool autoload = FALSE;
6981                 GV *gv =
6982                     gv_fetchmeth_pvn(stash, S_destroy, S_destroy_len, -1, 0);
6983                 if (gv)
6984                     destructor = GvCV(gv);
6985                 if (!destructor) {
6986                     gv = gv_autoload_pvn(stash, S_destroy, S_destroy_len,
6987                                          GV_AUTOLOAD_ISMETHOD);
6988                     if (gv)
6989                         destructor = GvCV(gv);
6990                     if (destructor)
6991                         autoload = TRUE;
6992                 }
6993                 /* we don't cache AUTOLOAD for DESTROY, since this code
6994                    would then need to set $__PACKAGE__::AUTOLOAD, or the
6995                    equivalent for XS AUTOLOADs */
6996                 if (!autoload) {
6997                     meta->destroy_gen = PL_sub_generation;
6998                     meta->destroy = destructor;
6999
7000                     DEBUG_o( Perl_deb(aTHX_ "Set cached DESTROY method %p for %s\n",
7001                                       (void *)destructor, HvNAME(stash)) );
7002                 }
7003                 else {
7004                     DEBUG_o( Perl_deb(aTHX_ "Not caching AUTOLOAD for DESTROY method for %s\n",
7005                                       HvNAME(stash)) );
7006                 }
7007             }
7008             assert(!destructor || SvTYPE(destructor) == SVt_PVCV);
7009             if (destructor
7010                 /* A constant subroutine can have no side effects, so
7011                    don't bother calling it.  */
7012                 && !CvCONST(destructor)
7013                 /* Don't bother calling an empty destructor or one that
7014                    returns immediately. */
7015                 && (CvISXSUB(destructor)
7016                 || (CvSTART(destructor)
7017                     && (CvSTART(destructor)->op_next->op_type
7018                                         != OP_LEAVESUB)
7019                     && (CvSTART(destructor)->op_next->op_type
7020                                         != OP_PUSHMARK
7021                         || CvSTART(destructor)->op_next->op_next->op_type
7022                                         != OP_RETURN
7023                        )
7024                    ))
7025                )
7026             {
7027                 SV* const tmpref = newRV(sv);
7028                 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
7029                 ENTER;
7030                 PUSHSTACKi(PERLSI_DESTROY);
7031                 EXTEND(SP, 2);
7032                 PUSHMARK(SP);
7033                 PUSHs(tmpref);
7034                 PUTBACK;
7035                 call_sv(MUTABLE_SV(destructor),
7036                             G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7037                 POPSTACK;
7038                 SPAGAIN;
7039                 LEAVE;
7040                 if(SvREFCNT(tmpref) < 2) {
7041                     /* tmpref is not kept alive! */
7042                     SvREFCNT(sv)--;
7043                     SvRV_set(tmpref, NULL);
7044                     SvROK_off(tmpref);
7045                 }
7046                 SvREFCNT_dec_NN(tmpref);
7047             }
7048           }
7049         } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
7050
7051
7052         if (check_refcnt && SvREFCNT(sv)) {
7053             if (PL_in_clean_objs)
7054                 Perl_croak(aTHX_
7055                   "DESTROY created new reference to dead object '%" HEKf "'",
7056                    HEKfARG(HvNAME_HEK(stash)));
7057             /* DESTROY gave object new lease on life */
7058             return FALSE;
7059         }
7060     }
7061
7062     if (SvOBJECT(sv)) {
7063         HV * const stash = SvSTASH(sv);
7064         /* Curse before freeing the stash, as freeing the stash could cause
7065            a recursive call into S_curse. */
7066         SvOBJECT_off(sv);       /* Curse the object. */
7067         SvSTASH_set(sv,0);      /* SvREFCNT_dec may try to read this */
7068         SvREFCNT_dec(stash); /* possibly of changed persuasion */
7069     }
7070     return TRUE;
7071 }
7072
7073 /*
7074 =for apidoc sv_newref
7075
7076 Increment an SV's reference count.  Use the C<SvREFCNT_inc()> wrapper
7077 instead.
7078
7079 =cut
7080 */
7081
7082 SV *
7083 Perl_sv_newref(pTHX_ SV *const sv)
7084 {
7085     PERL_UNUSED_CONTEXT;
7086     if (sv)
7087         (SvREFCNT(sv))++;
7088     return sv;
7089 }
7090
7091 /*
7092 =for apidoc sv_free
7093
7094 Decrement an SV's reference count, and if it drops to zero, call
7095 C<sv_clear> to invoke destructors and free up any memory used by
7096 the body; finally, deallocating the SV's head itself.
7097 Normally called via a wrapper macro C<SvREFCNT_dec>.
7098
7099 =cut
7100 */
7101
7102 void
7103 Perl_sv_free(pTHX_ SV *const sv)
7104 {
7105     SvREFCNT_dec(sv);
7106 }
7107
7108
7109 /* Private helper function for SvREFCNT_dec().
7110  * Called with rc set to original SvREFCNT(sv), where rc == 0 or 1 */
7111
7112 void
7113 Perl_sv_free2(pTHX_ SV *const sv, const U32 rc)
7114 {
7115
7116     PERL_ARGS_ASSERT_SV_FREE2;
7117
7118     if (LIKELY( rc == 1 )) {
7119         /* normal case */
7120         SvREFCNT(sv) = 0;
7121
7122 #ifdef DEBUGGING
7123         if (SvTEMP(sv)) {
7124             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7125                              "Attempt to free temp prematurely: SV 0x%" UVxf
7126                              pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7127             return;
7128         }
7129 #endif
7130         if (SvIMMORTAL(sv)) {
7131             /* make sure SvREFCNT(sv)==0 happens very seldom */
7132             SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7133             return;
7134         }
7135         sv_clear(sv);
7136         if (! SvREFCNT(sv)) /* may have have been resurrected */
7137             del_SV(sv);
7138         return;
7139     }
7140
7141     /* handle exceptional cases */
7142
7143     assert(rc == 0);
7144
7145     if (SvFLAGS(sv) & SVf_BREAK)
7146         /* this SV's refcnt has been artificially decremented to
7147          * trigger cleanup */
7148         return;
7149     if (PL_in_clean_all) /* All is fair */
7150         return;
7151     if (SvIMMORTAL(sv)) {
7152         /* make sure SvREFCNT(sv)==0 happens very seldom */
7153         SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7154         return;
7155     }
7156     if (ckWARN_d(WARN_INTERNAL)) {
7157 #ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
7158         Perl_dump_sv_child(aTHX_ sv);
7159 #else
7160     #ifdef DEBUG_LEAKING_SCALARS
7161         sv_dump(sv);
7162     #endif
7163 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7164         if (PL_warnhook == PERL_WARNHOOK_FATAL
7165             || ckDEAD(packWARN(WARN_INTERNAL))) {
7166             /* Don't let Perl_warner cause us to escape our fate:  */
7167             abort();
7168         }
7169 #endif
7170         /* This may not return:  */
7171         Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
7172                     "Attempt to free unreferenced scalar: SV 0x%" UVxf
7173                     pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7174 #endif
7175     }
7176 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7177     abort();
7178 #endif
7179
7180 }
7181
7182
7183 /*
7184 =for apidoc sv_len
7185
7186 Returns the length of the string in the SV.  Handles magic and type
7187 coercion and sets the UTF8 flag appropriately.  See also C<L</SvCUR>>, which
7188 gives raw access to the C<xpv_cur> slot.
7189
7190 =cut
7191 */
7192
7193 STRLEN
7194 Perl_sv_len(pTHX_ SV *const sv)
7195 {
7196     STRLEN len;
7197
7198     if (!sv)
7199         return 0;
7200
7201     (void)SvPV_const(sv, len);
7202     return len;
7203 }
7204
7205 /*
7206 =for apidoc sv_len_utf8
7207 =for apidoc_item sv_len_utf8_nomg
7208
7209 These return the number of characters in the string in an SV, counting wide
7210 UTF-8 bytes as a single character.  Both handle type coercion.
7211 They differ only in that C<sv_len_utf8> performs 'get' magic;
7212 C<sv_len_utf8_nomg> skips any magic.
7213
7214 =cut
7215 */
7216
7217 /*
7218  * The length is cached in PERL_MAGIC_utf8, in the mg_len field.  Also the
7219  * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below.
7220  * (Note that the mg_len is not the length of the mg_ptr field.
7221  * This allows the cache to store the character length of the string without
7222  * needing to malloc() extra storage to attach to the mg_ptr.)
7223  *
7224  */
7225
7226 STRLEN
7227 Perl_sv_len_utf8(pTHX_ SV *const sv)
7228 {
7229     if (!sv)
7230         return 0;
7231
7232     SvGETMAGIC(sv);
7233     return sv_len_utf8_nomg(sv);
7234 }
7235
7236 STRLEN
7237 Perl_sv_len_utf8_nomg(pTHX_ SV * const sv)
7238 {
7239     STRLEN len;
7240     const U8 *s = (U8*)SvPV_nomg_const(sv, len);
7241
7242     PERL_ARGS_ASSERT_SV_LEN_UTF8_NOMG;
7243
7244     if (PL_utf8cache && SvUTF8(sv)) {
7245             STRLEN ulen;
7246             MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
7247
7248             if (mg && (mg->mg_len != -1 || mg->mg_ptr)) {
7249                 if (mg->mg_len != -1)
7250                     ulen = mg->mg_len;
7251                 else {
7252                     /* We can use the offset cache for a headstart.
7253                        The longer value is stored in the first pair.  */
7254                     STRLEN *cache = (STRLEN *) mg->mg_ptr;
7255
7256                     ulen = cache[0] + Perl_utf8_length(aTHX_ s + cache[1],
7257                                                        s + len);
7258                 }
7259
7260                 if (PL_utf8cache < 0) {
7261                     const STRLEN real = Perl_utf8_length(aTHX_ s, s + len);
7262                     assert_uft8_cache_coherent("sv_len_utf8", ulen, real, sv);
7263                 }
7264             }
7265             else {
7266                 ulen = Perl_utf8_length(aTHX_ s, s + len);
7267                 utf8_mg_len_cache_update(sv, &mg, ulen);
7268             }
7269             return ulen;
7270     }
7271     return SvUTF8(sv) ? Perl_utf8_length(aTHX_ s, s + len) : len;
7272 }
7273
7274 /* Walk forwards to find the byte corresponding to the passed in UTF-8
7275    offset.  */
7276 static STRLEN
7277 S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send,
7278                       STRLEN *const uoffset_p, bool *const at_end,
7279                       bool* canonical_position)
7280 {
7281     const U8 *s = start;
7282     STRLEN uoffset = *uoffset_p;
7283
7284     PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS;
7285
7286     while (s < send && uoffset) {
7287         --uoffset;
7288         s += UTF8SKIP(s);
7289     }
7290     if (s == send) {
7291         *at_end = TRUE;
7292     }
7293     else if (s > send) {
7294         *at_end = TRUE;
7295         /* This is the existing behaviour. Possibly it should be a croak, as
7296            it's actually a bounds error  */
7297         s = send;
7298     }
7299     /* If the unicode position is beyond the end, we return the end but
7300        shouldn't cache that position */
7301     *canonical_position = (uoffset == 0);
7302     *uoffset_p -= uoffset;
7303     return s - start;
7304 }
7305
7306 /* Given the length of the string in both bytes and UTF-8 characters, decide
7307    whether to walk forwards or backwards to find the byte corresponding to
7308    the passed in UTF-8 offset.  */
7309 static STRLEN
7310 S_sv_pos_u2b_midway(const U8 *const start, const U8 *send,
7311                     STRLEN uoffset, const STRLEN uend)
7312 {
7313     STRLEN backw = uend - uoffset;
7314
7315     PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY;
7316
7317     if (uoffset < 2 * backw) {
7318         /* The assumption is that going forwards is twice the speed of going
7319            forward (that's where the 2 * backw comes from).
7320            (The real figure of course depends on the UTF-8 data.)  */
7321         const U8 *s = start;
7322
7323         while (s < send && uoffset--)
7324             s += UTF8SKIP(s);
7325         assert (s <= send);
7326         if (s > send)
7327             s = send;
7328         return s - start;
7329     }
7330
7331     while (backw--) {
7332         send--;
7333         while (UTF8_IS_CONTINUATION(*send))
7334             send--;
7335     }
7336     return send - start;
7337 }
7338
7339 /* For the string representation of the given scalar, find the byte
7340    corresponding to the passed in UTF-8 offset.  uoffset0 and boffset0
7341    give another position in the string, *before* the sought offset, which
7342    (which is always true, as 0, 0 is a valid pair of positions), which should
7343    help reduce the amount of linear searching.
7344    If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
7345    will be used to reduce the amount of linear searching. The cache will be
7346    created if necessary, and the found value offered to it for update.  */
7347 static STRLEN
7348 S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
7349                     const U8 *const send, STRLEN uoffset,
7350                     STRLEN uoffset0, STRLEN boffset0)
7351 {
7352     STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy.  */
7353     bool found = FALSE;
7354     bool at_end = FALSE;
7355     bool canonical_position = FALSE;
7356
7357     PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
7358
7359     assert (uoffset >= uoffset0);
7360
7361     if (!uoffset)
7362         return 0;
7363
7364     if (!SvREADONLY(sv) && !SvGMAGICAL(sv) && SvPOK(sv)
7365         && PL_utf8cache
7366         && (*mgp || (SvTYPE(sv) >= SVt_PVMG &&
7367                      (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) {
7368         if ((*mgp)->mg_ptr) {
7369             STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
7370             if (cache[0] == uoffset) {
7371                 /* An exact match. */
7372                 return cache[1];
7373             }
7374             if (cache[2] == uoffset) {
7375                 /* An exact match. */
7376                 return cache[3];
7377             }
7378
7379             if (cache[0] < uoffset) {
7380                 /* The cache already knows part of the way.   */
7381                 if (cache[0] > uoffset0) {
7382                     /* The cache knows more than the passed in pair  */
7383                     uoffset0 = cache[0];
7384                     boffset0 = cache[1];
7385                 }
7386                 if ((*mgp)->mg_len != -1) {
7387                     /* And we know the end too.  */
7388                     boffset = boffset0
7389                         + sv_pos_u2b_midway(start + boffset0, send,
7390                                               uoffset - uoffset0,
7391                                               (*mgp)->mg_len - uoffset0);
7392                 } else {
7393                     uoffset -= uoffset0;
7394                     boffset = boffset0
7395                         + sv_pos_u2b_forwards(start + boffset0,
7396                                               send, &uoffset, &at_end,
7397                                               &canonical_position);
7398                     uoffset += uoffset0;
7399                 }
7400             }
7401             else if (cache[2] < uoffset) {
7402                 /* We're between the two cache entries.  */
7403                 if (cache[2] > uoffset0) {
7404                     /* and the cache knows more than the passed in pair  */
7405                     uoffset0 = cache[2];
7406                     boffset0 = cache[3];
7407                 }
7408
7409                 boffset = boffset0
7410                     + sv_pos_u2b_midway(start + boffset0,
7411                                           start + cache[1],
7412                                           uoffset - uoffset0,
7413                                           cache[0] - uoffset0);
7414             } else {
7415                 boffset = boffset0
7416                     + sv_pos_u2b_midway(start + boffset0,
7417                                           start + cache[3],
7418                                           uoffset - uoffset0,
7419                                           cache[2] - uoffset0);
7420             }
7421             found = TRUE;
7422         }
7423         else if ((*mgp)->mg_len != -1) {
7424             /* If we can take advantage of a passed in offset, do so.  */
7425             /* In fact, offset0 is either 0, or less than offset, so don't
7426                need to worry about the other possibility.  */
7427             boffset = boffset0
7428                 + sv_pos_u2b_midway(start + boffset0, send,
7429                                       uoffset - uoffset0,
7430                                       (*mgp)->mg_len - uoffset0);
7431             found = TRUE;
7432         }
7433     }
7434
7435     if (!found || PL_utf8cache < 0) {
7436         STRLEN real_boffset;
7437         uoffset -= uoffset0;
7438         real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0,
7439                                                       send, &uoffset, &at_end,
7440                                                       &canonical_position);
7441         uoffset += uoffset0;
7442
7443         if (found && PL_utf8cache < 0)
7444             assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset,
7445                                        real_boffset, sv);
7446         boffset = real_boffset;
7447     }
7448
7449     if (PL_utf8cache && canonical_position && !SvGMAGICAL(sv) && SvPOK(sv)) {
7450         if (at_end)
7451             utf8_mg_len_cache_update(sv, mgp, uoffset);
7452         else
7453             utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
7454     }
7455     return boffset;
7456 }
7457
7458
7459 /*
7460 =for apidoc sv_pos_u2b_flags
7461
7462 Converts the offset from a count of UTF-8 chars from
7463 the start of the string, to a count of the equivalent number of bytes; if
7464 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7465 C<offset>, rather than from the start
7466 of the string.  Handles type coercion.
7467 C<flags> is passed to C<SvPV_flags>, and usually should be
7468 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7469
7470 =cut
7471 */
7472
7473 /*
7474  * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential
7475  * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7476  * byte offsets.  See also the comments of S_utf8_mg_pos_cache_update().
7477  *
7478  */
7479
7480 STRLEN
7481 Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp,
7482                       U32 flags)
7483 {
7484     const U8 *start;
7485     STRLEN len;
7486     STRLEN boffset;
7487
7488     PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS;
7489
7490     start = (U8*)SvPV_flags(sv, len, flags);
7491     if (len) {
7492         const U8 * const send = start + len;
7493         MAGIC *mg = NULL;
7494         boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0);
7495
7496         if (lenp
7497             && *lenp /* don't bother doing work for 0, as its bytes equivalent
7498                         is 0, and *lenp is already set to that.  */) {
7499             /* Convert the relative offset to absolute.  */
7500             const STRLEN uoffset2 = uoffset + *lenp;
7501             const STRLEN boffset2
7502                 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
7503                                       uoffset, boffset) - boffset;
7504
7505             *lenp = boffset2;
7506         }
7507     } else {
7508         if (lenp)
7509             *lenp = 0;
7510         boffset = 0;
7511     }
7512
7513     return boffset;
7514 }
7515
7516 /*
7517 =for apidoc sv_pos_u2b
7518
7519 Converts the value pointed to by C<offsetp> from a count of UTF-8 chars from
7520 the start of the string, to a count of the equivalent number of bytes; if
7521 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7522 the offset, rather than from the start of the string.  Handles magic and
7523 type coercion.
7524
7525 Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer
7526 than 2Gb.
7527
7528 =cut
7529 */
7530
7531 /*
7532  * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
7533  * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7534  * byte offsets.  See also the comments of S_utf8_mg_pos_cache_update().
7535  *
7536  */
7537
7538 /* This function is subject to size and sign problems */
7539
7540 void
7541 Perl_sv_pos_u2b(pTHX_ SV *const sv, I32 *const offsetp, I32 *const lenp)
7542 {
7543     PERL_ARGS_ASSERT_SV_POS_U2B;
7544
7545     if (lenp) {
7546         STRLEN ulen = (STRLEN)*lenp;
7547         *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen,
7548                                          SV_GMAGIC|SV_CONST_RETURN);
7549         *lenp = (I32)ulen;
7550     } else {
7551         *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL,
7552                                          SV_GMAGIC|SV_CONST_RETURN);
7553     }
7554 }
7555
7556 static void
7557 S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp,
7558                            const STRLEN ulen)
7559 {
7560     PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE;
7561     if (SvREADONLY(sv) || SvGMAGICAL(sv) || !SvPOK(sv))
7562         return;
7563
7564     if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7565                   !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7566         *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
7567     }
7568     assert(*mgp);
7569
7570     (*mgp)->mg_len = ulen;
7571 }
7572
7573 /* Create and update the UTF8 magic offset cache, with the proffered utf8/
7574    byte length pairing. The (byte) length of the total SV is passed in too,
7575    as blen, because for some (more esoteric) SVs, the call to SvPV_const()
7576    may not have updated SvCUR, so we can't rely on reading it directly.
7577
7578    The proffered utf8/byte length pairing isn't used if the cache already has
7579    two pairs, and swapping either for the proffered pair would increase the
7580    RMS of the intervals between known byte offsets.
7581
7582    The cache itself consists of 4 STRLEN values
7583    0: larger UTF-8 offset
7584    1: corresponding byte offset
7585    2: smaller UTF-8 offset
7586    3: corresponding byte offset
7587
7588    Unused cache pairs have the value 0, 0.
7589    Keeping the cache "backwards" means that the invariant of
7590    cache[0] >= cache[2] is maintained even with empty slots, which means that
7591    the code that uses it doesn't need to worry if only 1 entry has actually
7592    been set to non-zero.  It also makes the "position beyond the end of the
7593    cache" logic much simpler, as the first slot is always the one to start
7594    from.
7595 */
7596 static void
7597 S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
7598                            const STRLEN utf8, const STRLEN blen)
7599 {
7600     STRLEN *cache;
7601
7602     PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
7603
7604     if (SvREADONLY(sv))
7605         return;
7606
7607     if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7608                   !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7609         *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
7610                            0);
7611         (*mgp)->mg_len = -1;
7612     }
7613     assert(*mgp);
7614
7615     if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
7616         Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7617         (*mgp)->mg_ptr = (char *) cache;
7618     }
7619     assert(cache);
7620
7621     if (PL_utf8cache < 0 && SvPOKp(sv)) {
7622         /* SvPOKp() because, if sv is a reference, then SvPVX() is actually
7623            a pointer.  Note that we no longer cache utf8 offsets on refer-
7624            ences, but this check is still a good idea, for robustness.  */
7625         const U8 *start = (const U8 *) SvPVX_const(sv);
7626         const STRLEN realutf8 = utf8_length(start, start + byte);
7627
7628         assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8,
7629                                    sv);
7630     }
7631
7632     /* Cache is held with the later position first, to simplify the code
7633        that deals with unbounded ends.  */
7634
7635     ASSERT_UTF8_CACHE(cache);
7636     if (cache[1] == 0) {
7637         /* Cache is totally empty  */
7638         cache[0] = utf8;
7639         cache[1] = byte;
7640     } else if (cache[3] == 0) {
7641         if (byte > cache[1]) {
7642             /* New one is larger, so goes first.  */
7643             cache[2] = cache[0];
7644             cache[3] = cache[1];
7645             cache[0] = utf8;
7646             cache[1] = byte;
7647         } else {
7648             cache[2] = utf8;
7649             cache[3] = byte;
7650         }
7651     } else {
7652 /* float casts necessary? XXX */
7653 #define THREEWAY_SQUARE(a,b,c,d) \
7654             ((float)((d) - (c))) * ((float)((d) - (c))) \
7655             + ((float)((c) - (b))) * ((float)((c) - (b))) \
7656                + ((float)((b) - (a))) * ((float)((b) - (a)))
7657
7658         /* Cache has 2 slots in use, and we know three potential pairs.
7659            Keep the two that give the lowest RMS distance. Do the
7660            calculation in bytes simply because we always know the byte
7661            length.  squareroot has the same ordering as the positive value,
7662            so don't bother with the actual square root.  */
7663         if (byte > cache[1]) {
7664             /* New position is after the existing pair of pairs.  */
7665             const float keep_earlier
7666                 = THREEWAY_SQUARE(0, cache[3], byte, blen);
7667             const float keep_later
7668                 = THREEWAY_SQUARE(0, cache[1], byte, blen);
7669
7670             if (keep_later < keep_earlier) {
7671                 cache[2] = cache[0];
7672                 cache[3] = cache[1];
7673             }
7674             cache[0] = utf8;
7675             cache[1] = byte;
7676         }
7677         else {
7678             const float keep_later = THREEWAY_SQUARE(0, byte, cache[1], blen);
7679             float b, c, keep_earlier;
7680             if (byte > cache[3]) {
7681                 /* New position is between the existing pair of pairs.  */
7682                 b = (float)cache[3];
7683                 c = (float)byte;
7684             } else {
7685                 /* New position is before the existing pair of pairs.  */
7686                 b = (float)byte;
7687                 c = (float)cache[3];
7688             }
7689             keep_earlier = THREEWAY_SQUARE(0, b, c, blen);
7690             if (byte > cache[3]) {
7691                 if (keep_later < keep_earlier) {
7692                     cache[2] = utf8;
7693                     cache[3] = byte;
7694                 }
7695                 else {
7696                     cache[0] = utf8;
7697                     cache[1] = byte;
7698                 }
7699             }
7700             else {
7701                 if (! (keep_later < keep_earlier)) {
7702                     cache[0] = cache[2];
7703                     cache[1] = cache[3];
7704                 }
7705                 cache[2] = utf8;
7706                 cache[3] = byte;
7707             }
7708         }
7709     }
7710     ASSERT_UTF8_CACHE(cache);
7711 }
7712
7713 /* We already know all of the way, now we may be able to walk back.  The same
7714    assumption is made as in S_sv_pos_u2b_midway(), namely that walking
7715    backward is half the speed of walking forward. */
7716 static STRLEN
7717 S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
7718                     const U8 *end, STRLEN endu)
7719 {
7720     const STRLEN forw = target - s;
7721     STRLEN backw = end - target;
7722
7723     PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
7724
7725     if (forw < 2 * backw) {
7726         return utf8_length(s, target);
7727     }
7728
7729     while (end > target) {
7730         end--;
7731         while (UTF8_IS_CONTINUATION(*end)) {
7732             end--;
7733         }
7734         endu--;
7735     }
7736     return endu;
7737 }
7738
7739 /*
7740 =for apidoc sv_pos_b2u_flags
7741
7742 Converts C<offset> from a count of bytes from the start of the string, to
7743 a count of the equivalent number of UTF-8 chars.  Handles type coercion.
7744 C<flags> is passed to C<SvPV_flags>, and usually should be
7745 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7746
7747 =cut
7748 */
7749
7750 /*
7751  * sv_pos_b2u_flags() uses, like sv_pos_u2b_flags(), the mg_ptr of the
7752  * potential PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8
7753  * and byte offsets.
7754  *
7755  */
7756 STRLEN
7757 Perl_sv_pos_b2u_flags(pTHX_ SV *const sv, STRLEN const offset, U32 flags)
7758 {
7759     const U8* s;
7760     STRLEN len = 0; /* Actually always set, but let's keep gcc happy.  */
7761     STRLEN blen;
7762     MAGIC* mg = NULL;
7763     const U8* send;
7764     bool found = FALSE;
7765
7766     PERL_ARGS_ASSERT_SV_POS_B2U_FLAGS;
7767
7768     s = (const U8*)SvPV_flags(sv, blen, flags);
7769
7770     if (blen < offset)
7771         Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset, blen=%" UVuf
7772                    ", byte=%" UVuf, (UV)blen, (UV)offset);
7773
7774     send = s + offset;
7775
7776     if (!SvREADONLY(sv)
7777         && PL_utf8cache
7778         && SvTYPE(sv) >= SVt_PVMG
7779         && (mg = mg_find(sv, PERL_MAGIC_utf8)))
7780     {
7781         if (mg->mg_ptr) {
7782             STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7783             if (cache[1] == offset) {
7784                 /* An exact match. */
7785                 return cache[0];
7786             }
7787             if (cache[3] == offset) {
7788                 /* An exact match. */
7789                 return cache[2];
7790             }
7791
7792             if (cache[1] < offset) {
7793                 /* We already know part of the way. */
7794                 if (mg->mg_len != -1) {
7795                     /* Actually, we know the end too.  */
7796                     len = cache[0]
7797                         + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
7798                                               s + blen, mg->mg_len - cache[0]);
7799                 } else {
7800                     len = cache[0] + utf8_length(s + cache[1], send);
7801                 }
7802             }
7803             else if (cache[3] < offset) {
7804                 /* We're between the two cached pairs, so we do the calculation
7805                    offset by the byte/utf-8 positions for the earlier pair,
7806                    then add the utf-8 characters from the string start to
7807                    there.  */
7808                 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
7809                                           s + cache[1], cache[0] - cache[2])
7810                     + cache[2];
7811
7812             }
7813             else { /* cache[3] > offset */
7814                 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
7815                                           cache[2]);
7816
7817             }
7818             ASSERT_UTF8_CACHE(cache);
7819             found = TRUE;
7820         } else if (mg->mg_len != -1) {
7821             len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
7822             found = TRUE;
7823         }
7824     }
7825     if (!found || PL_utf8cache < 0) {
7826         const STRLEN real_len = utf8_length(s, send);
7827
7828         if (found && PL_utf8cache < 0)
7829             assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv);
7830         len = real_len;
7831     }
7832
7833     if (PL_utf8cache) {
7834         if (blen == offset)
7835             utf8_mg_len_cache_update(sv, &mg, len);
7836         else
7837             utf8_mg_pos_cache_update(sv, &mg, offset, len, blen);
7838     }
7839
7840     return len;
7841 }
7842
7843 /*
7844 =for apidoc sv_pos_b2u
7845
7846 Converts the value pointed to by C<offsetp> from a count of bytes from the
7847 start of the string, to a count of the equivalent number of UTF-8 chars.
7848 Handles magic and type coercion.
7849
7850 Use C<sv_pos_b2u_flags> in preference, which correctly handles strings
7851 longer than 2Gb.
7852
7853 =cut
7854 */
7855
7856 /*
7857  * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
7858  * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7859  * byte offsets.
7860  *
7861  */
7862 void
7863 Perl_sv_pos_b2u(pTHX_ SV *const sv, I32 *const offsetp)
7864 {
7865     PERL_ARGS_ASSERT_SV_POS_B2U;
7866
7867     if (!sv)
7868         return;
7869
7870     *offsetp = (I32)sv_pos_b2u_flags(sv, (STRLEN)*offsetp,
7871                                      SV_GMAGIC|SV_CONST_RETURN);
7872 }
7873
7874 static void
7875 S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache,
7876                              STRLEN real, SV *const sv)
7877 {
7878     PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT;
7879
7880     /* As this is debugging only code, save space by keeping this test here,
7881        rather than inlining it in all the callers.  */
7882     if (from_cache == real)
7883         return;
7884
7885     /* Need to turn the assertions off otherwise we may recurse infinitely
7886        while printing error messages.  */
7887     SAVEI8(PL_utf8cache);
7888     PL_utf8cache = 0;
7889     Perl_croak(aTHX_ "panic: %s cache %" UVuf " real %" UVuf " for %" SVf,
7890                func, (UV) from_cache, (UV) real, SVfARG(sv));
7891 }
7892
7893 /*
7894 =for apidoc sv_eq
7895
7896 Returns a boolean indicating whether the strings in the two SVs are
7897 identical.  Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
7898 coerce its args to strings if necessary.
7899
7900 This function does not handle operator overloading. For a version that does,
7901 see instead C<sv_streq>.
7902
7903 =for apidoc sv_eq_flags
7904
7905 Returns a boolean indicating whether the strings in the two SVs are
7906 identical.  Is UTF-8 and S<C<'use bytes'>> aware and coerces its args to strings
7907 if necessary.  If the flags has the C<SV_GMAGIC> bit set, it handles get-magic, too.
7908
7909 This function does not handle operator overloading. For a version that does,
7910 see instead C<sv_streq_flags>.
7911
7912 =cut
7913 */
7914
7915 I32
7916 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
7917 {
7918     const char *pv1;
7919     STRLEN cur1;
7920     const char *pv2;
7921     STRLEN cur2;
7922
7923     if (!sv1) {
7924         pv1 = "";
7925         cur1 = 0;
7926     }
7927     else {
7928         /* if pv1 and pv2 are the same, second SvPV_const call may
7929          * invalidate pv1 (if we are handling magic), so we may need to
7930          * make a copy */
7931         if (sv1 == sv2 && flags & SV_GMAGIC
7932          && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
7933             pv1 = SvPV_const(sv1, cur1);
7934             sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
7935         }
7936         pv1 = SvPV_flags_const(sv1, cur1, flags);
7937     }
7938
7939     if (!sv2){
7940         pv2 = "";
7941         cur2 = 0;
7942     }
7943     else
7944         pv2 = SvPV_flags_const(sv2, cur2, flags);
7945
7946     if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
7947         /* Differing utf8ness.  */
7948         if (SvUTF8(sv1)) {
7949                   /* sv1 is the UTF-8 one  */
7950                   return bytes_cmp_utf8((const U8*)pv2, cur2,
7951                                         (const U8*)pv1, cur1) == 0;
7952         }
7953         else {
7954                   /* sv2 is the UTF-8 one  */
7955                   return bytes_cmp_utf8((const U8*)pv1, cur1,
7956                                         (const U8*)pv2, cur2) == 0;
7957         }
7958     }
7959
7960     if (cur1 == cur2)
7961         return (pv1 == pv2) || memEQ(pv1, pv2, cur1);
7962     else
7963         return 0;
7964 }
7965
7966 /*
7967 =for apidoc sv_streq_flags
7968
7969 Returns a boolean indicating whether the strings in the two SVs are
7970 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
7971 get-magic too. Will coerce its args to strings if necessary. Treats
7972 C<NULL> as undef. Correctly handles the UTF8 flag.
7973
7974 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
7975 C<eq> overloading will be made. If such overloading does not exist or the
7976 flag is set, then regular string comparison will be used instead.
7977
7978 =for apidoc sv_streq
7979
7980 A convenient shortcut for calling C<sv_streq_flags> with the C<SV_GMAGIC>
7981 flag. This function basically behaves like the Perl code C<$sv1 eq $sv2>.
7982
7983 =cut
7984 */
7985
7986 bool
7987 Perl_sv_streq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
7988 {
7989     PERL_ARGS_ASSERT_SV_STREQ_FLAGS;
7990
7991     if(flags & SV_GMAGIC) {
7992         if(sv1)
7993             SvGETMAGIC(sv1);
7994         if(sv2)
7995             SvGETMAGIC(sv2);
7996     }
7997
7998     /* Treat NULL as undef */
7999     if(!sv1)
8000         sv1 = &PL_sv_undef;
8001     if(!sv2)
8002         sv2 = &PL_sv_undef;
8003
8004     if(!(flags & SV_SKIP_OVERLOAD) &&
8005             (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8006         SV *ret = amagic_call(sv1, sv2, seq_amg, 0);
8007         if(ret)
8008             return SvTRUE(ret);
8009     }
8010
8011     return sv_eq_flags(sv1, sv2, 0);
8012 }
8013
8014 /*
8015 =for apidoc sv_numeq_flags
8016
8017 Returns a boolean indicating whether the numbers in the two SVs are
8018 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8019 get-magic too. Will coerce its args to numbers if necessary. Treats
8020 C<NULL> as undef.
8021
8022 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8023 C<==> overloading will be made. If such overloading does not exist or the
8024 flag is set, then regular numerical comparison will be used instead.
8025
8026 =for apidoc sv_numeq
8027
8028 A convenient shortcut for calling C<sv_numeq_flags> with the C<SV_GMAGIC>
8029 flag. This function basically behaves like the Perl code C<$sv1 == $sv2>.
8030
8031 =cut
8032 */
8033
8034 bool
8035 Perl_sv_numeq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8036 {
8037     PERL_ARGS_ASSERT_SV_NUMEQ_FLAGS;
8038
8039     if(flags & SV_GMAGIC) {
8040         if(sv1)
8041             SvGETMAGIC(sv1);
8042         if(sv2)
8043             SvGETMAGIC(sv2);
8044     }
8045
8046     /* Treat NULL as undef */
8047     if(!sv1)
8048         sv1 = &PL_sv_undef;
8049     if(!sv2)
8050         sv2 = &PL_sv_undef;
8051
8052     if(!(flags & SV_SKIP_OVERLOAD) &&
8053             (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8054         SV *ret = amagic_call(sv1, sv2, eq_amg, 0);
8055         if(ret)
8056             return SvTRUE(ret);
8057     }
8058
8059     return do_ncmp(sv1, sv2) == 0;
8060 }
8061
8062 /*
8063 =for apidoc sv_cmp
8064
8065 Compares the strings in two SVs.  Returns -1, 0, or 1 indicating whether the
8066 string in C<sv1> is less than, equal to, or greater than the string in
8067 C<sv2>.  Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
8068 coerce its args to strings if necessary.  See also C<L</sv_cmp_locale>>.
8069
8070 =for apidoc sv_cmp_flags
8071
8072 Compares the strings in two SVs.  Returns -1, 0, or 1 indicating whether the
8073 string in C<sv1> is less than, equal to, or greater than the string in
8074 C<sv2>.  Is UTF-8 and S<C<'use bytes'>> aware and will coerce its args to strings
8075 if necessary.  If the flags has the C<SV_GMAGIC> bit set, it handles get magic.  See
8076 also C<L</sv_cmp_locale_flags>>.
8077
8078 =cut
8079 */
8080
8081 I32
8082 Perl_sv_cmp(pTHX_ SV *const sv1, SV *const sv2)
8083 {
8084     return sv_cmp_flags(sv1, sv2, SV_GMAGIC);
8085 }
8086
8087 I32
8088 Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2,
8089                   const U32 flags)
8090 {
8091     STRLEN cur1, cur2;
8092     const char *pv1, *pv2;
8093     I32  cmp;
8094     SV *svrecode = NULL;
8095
8096     if (!sv1) {
8097         pv1 = "";
8098         cur1 = 0;
8099     }
8100     else
8101         pv1 = SvPV_flags_const(sv1, cur1, flags);
8102
8103     if (!sv2) {
8104         pv2 = "";
8105         cur2 = 0;
8106     }
8107     else
8108         pv2 = SvPV_flags_const(sv2, cur2, flags);
8109
8110     if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8111         /* Differing utf8ness.  */
8112         if (SvUTF8(sv1)) {
8113                 const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2,
8114                                                    (const U8*)pv1, cur1);
8115                 return retval ? retval < 0 ? -1 : +1 : 0;
8116         }
8117         else {
8118                 const int retval = bytes_cmp_utf8((const U8*)pv1, cur1,
8119                                                   (const U8*)pv2, cur2);
8120                 return retval ? retval < 0 ? -1 : +1 : 0;
8121         }
8122     }
8123
8124     /* Here, if both are non-NULL, then they have the same UTF8ness. */
8125
8126     if (!cur1) {
8127         cmp = cur2 ? -1 : 0;
8128     } else if (!cur2) {
8129         cmp = 1;
8130     } else {
8131         STRLEN shortest_len = cur1 < cur2 ? cur1 : cur2;
8132
8133 #ifdef EBCDIC
8134         if (! DO_UTF8(sv1)) {
8135 #endif
8136             const I32 retval = memcmp((const void*)pv1,
8137                                       (const void*)pv2,
8138                                       shortest_len);
8139             if (retval) {
8140                 cmp = retval < 0 ? -1 : 1;
8141             } else if (cur1 == cur2) {
8142                 cmp = 0;
8143             } else {
8144                 cmp = cur1 < cur2 ? -1 : 1;
8145             }
8146 #ifdef EBCDIC
8147         }
8148         else {  /* Both are to be treated as UTF-EBCDIC */
8149
8150             /* EBCDIC UTF-8 is complicated by the fact that it is based on I8
8151              * which remaps code points 0-255.  We therefore generally have to
8152              * unmap back to the original values to get an accurate comparison.
8153              * But we don't have to do that for UTF-8 invariants, as by
8154              * definition, they aren't remapped, nor do we have to do it for
8155              * above-latin1 code points, as they also aren't remapped.  (This
8156              * code also works on ASCII platforms, but the memcmp() above is
8157              * much faster). */
8158
8159             const char *e = pv1 + shortest_len;
8160
8161             /* Find the first bytes that differ between the two strings */
8162             while (pv1 < e && *pv1 == *pv2) {
8163                 pv1++;
8164                 pv2++;
8165             }
8166
8167
8168             if (pv1 == e) { /* Are the same all the way to the end */
8169                 if (cur1 == cur2) {
8170                     cmp = 0;
8171                 } else {
8172                     cmp = cur1 < cur2 ? -1 : 1;
8173                 }
8174             }
8175             else   /* Here *pv1 and *pv2 are not equal, but all bytes earlier
8176                     * in the strings were.  The current bytes may or may not be
8177                     * at the beginning of a character.  But neither or both are
8178                     * (or else earlier bytes would have been different).  And
8179                     * if we are in the middle of a character, the two
8180                     * characters are comprised of the same number of bytes
8181                     * (because in this case the start bytes are the same, and
8182                     * the start bytes encode the character's length). */
8183                  if (UTF8_IS_INVARIANT(*pv1))
8184             {
8185                 /* If both are invariants; can just compare directly */
8186                 if (UTF8_IS_INVARIANT(*pv2)) {
8187                     cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8188                 }
8189                 else   /* Since *pv1 is invariant, it is the whole character,
8190                           which means it is at the beginning of a character.
8191                           That means pv2 is also at the beginning of a
8192                           character (see earlier comment).  Since it isn't
8193                           invariant, it must be a start byte.  If it starts a
8194                           character whose code point is above 255, that
8195                           character is greater than any single-byte char, which
8196                           *pv1 is */
8197                       if (UTF8_IS_ABOVE_LATIN1_START(*pv2))
8198                 {
8199                     cmp = -1;
8200                 }
8201                 else {
8202                     /* Here, pv2 points to a character composed of 2 bytes
8203                      * whose code point is < 256.  Get its code point and
8204                      * compare with *pv1 */
8205                     cmp = ((U8) *pv1 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8206                            ?  -1
8207                            : 1;
8208                 }
8209             }
8210             else   /* The code point starting at pv1 isn't a single byte */
8211                  if (UTF8_IS_INVARIANT(*pv2))
8212             {
8213                 /* But here, the code point starting at *pv2 is a single byte,
8214                  * and so *pv1 must begin a character, hence is a start byte.
8215                  * If that character is above 255, it is larger than any
8216                  * single-byte char, which *pv2 is */
8217                 if (UTF8_IS_ABOVE_LATIN1_START(*pv1)) {
8218                     cmp = 1;
8219                 }
8220                 else {
8221                     /* Here, pv1 points to a character composed of 2 bytes
8222                      * whose code point is < 256.  Get its code point and
8223                      * compare with the single byte character *pv2 */
8224                     cmp = (EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1)) < (U8) *pv2)
8225                           ?  -1
8226                           : 1;
8227                 }
8228             }
8229             else   /* Here, we've ruled out either *pv1 and *pv2 being
8230                       invariant.  That means both are part of variants, but not
8231                       necessarily at the start of a character */
8232                  if (   UTF8_IS_ABOVE_LATIN1_START(*pv1)
8233                      || UTF8_IS_ABOVE_LATIN1_START(*pv2))
8234             {
8235                 /* Here, at least one is the start of a character, which means
8236                  * the other is also a start byte.  And the code point of at
8237                  * least one of the characters is above 255.  It is a
8238                  * characteristic of UTF-EBCDIC that all start bytes for
8239                  * above-latin1 code points are well behaved as far as code
8240                  * point comparisons go, and all are larger than all other
8241                  * start bytes, so the comparison with those is also well
8242                  * behaved */
8243                 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8244             }
8245             else {
8246                 /* Here both *pv1 and *pv2 are part of variant characters.
8247                  * They could be both continuations, or both start characters.
8248                  * (One or both could even be an illegal start character (for
8249                  * an overlong) which for the purposes of sorting we treat as
8250                  * legal. */
8251                 if (UTF8_IS_CONTINUATION(*pv1)) {
8252
8253                     /* If they are continuations for code points above 255,
8254                      * then comparing the current byte is sufficient, as there
8255                      * is no remapping of these and so the comparison is
8256                      * well-behaved.   We determine if they are such
8257                      * continuations by looking at the preceding byte.  It
8258                      * could be a start byte, from which we can tell if it is
8259                      * for an above 255 code point.  Or it could be a
8260                      * continuation, which means the character occupies at
8261                      * least 3 bytes, so must be above 255.  */
8262                     if (   UTF8_IS_CONTINUATION(*(pv2 - 1))
8263                         || UTF8_IS_ABOVE_LATIN1_START(*(pv2 -1)))
8264                     {
8265                         cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8266                         goto cmp_done;
8267                     }
8268
8269                     /* Here, the continuations are for code points below 256;
8270                      * back up one to get to the start byte */
8271                     pv1--;
8272                     pv2--;
8273                 }
8274
8275                 /* We need to get the actual native code point of each of these
8276                  * variants in order to compare them */
8277                 cmp =  (  EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1))
8278                         < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8279                         ? -1
8280                         : 1;
8281             }
8282         }
8283       cmp_done: ;
8284 #endif
8285     }
8286
8287     SvREFCNT_dec(svrecode);
8288
8289     return cmp;
8290 }
8291
8292 /*
8293 =for apidoc sv_cmp_locale
8294
8295 Compares the strings in two SVs in a locale-aware manner.  Is UTF-8 and
8296 S<C<'use bytes'>> aware, handles get magic, and will coerce its args to strings
8297 if necessary.  See also C<L</sv_cmp>>.
8298
8299 =for apidoc sv_cmp_locale_flags
8300
8301 Compares the strings in two SVs in a locale-aware manner.  Is UTF-8 and
8302 S<C<'use bytes'>> aware and will coerce its args to strings if necessary.  If
8303 the flags contain C<SV_GMAGIC>, it handles get magic.  See also
8304 C<L</sv_cmp_flags>>.
8305
8306 =cut
8307 */
8308
8309 I32
8310 Perl_sv_cmp_locale(pTHX_ SV *const sv1, SV *const sv2)
8311 {
8312     return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC);
8313 }
8314
8315 I32
8316 Perl_sv_cmp_locale_flags(pTHX_ SV *const sv1, SV *const sv2,
8317                          const U32 flags)
8318 {
8319 #ifdef USE_LOCALE_COLLATE
8320
8321     char *pv1, *pv2;
8322     STRLEN len1, len2;
8323     I32 retval;
8324
8325     if (PL_collation_standard)
8326         goto raw_compare;
8327
8328     len1 = len2 = 0;
8329
8330     /* Revert to using raw compare if both operands exist, but either one
8331      * doesn't transform properly for collation */
8332     if (sv1 && sv2) {
8333         pv1 = sv_collxfrm_flags(sv1, &len1, flags);
8334         if (! pv1) {
8335             goto raw_compare;
8336         }
8337         pv2 = sv_collxfrm_flags(sv2, &len2, flags);
8338         if (! pv2) {
8339             goto raw_compare;
8340         }
8341     }
8342     else {
8343         pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL;
8344         pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL;
8345     }
8346
8347     if (!pv1 || !len1) {
8348         if (pv2 && len2)
8349             return -1;
8350         else
8351             goto raw_compare;
8352     }
8353     else {
8354         if (!pv2 || !len2)
8355             return 1;
8356     }
8357
8358     retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
8359
8360     if (retval)
8361         return retval < 0 ? -1 : 1;
8362
8363     /*
8364      * When the result of collation is equality, that doesn't mean
8365      * that there are no differences -- some locales exclude some
8366      * characters from consideration.  So to avoid false equalities,
8367      * we use the raw string as a tiebreaker.
8368      */
8369
8370   raw_compare:
8371     /* FALLTHROUGH */
8372
8373 #else
8374     PERL_UNUSED_ARG(flags);
8375 #endif /* USE_LOCALE_COLLATE */
8376
8377     return sv_cmp(sv1, sv2);
8378 }
8379
8380
8381 #ifdef USE_LOCALE_COLLATE
8382
8383 /*
8384 =for apidoc sv_collxfrm
8385
8386 This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag.  See
8387 C<L</sv_collxfrm_flags>>.
8388
8389 =for apidoc sv_collxfrm_flags
8390
8391 Add Collate Transform magic to an SV if it doesn't already have it.  If the
8392 flags contain C<SV_GMAGIC>, it handles get-magic.
8393
8394 Any scalar variable may carry C<PERL_MAGIC_collxfrm> magic that contains the
8395 scalar data of the variable, but transformed to such a format that a normal
8396 memory comparison can be used to compare the data according to the locale
8397 settings.
8398
8399 =cut
8400 */
8401
8402 char *
8403 Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
8404 {
8405     MAGIC *mg;
8406
8407     PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS;
8408
8409     mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
8410
8411     /* If we don't have collation magic on 'sv', or the locale has changed
8412      * since the last time we calculated it, get it and save it now */
8413     if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
8414         const char *s;
8415         char *xf;
8416         STRLEN len, xlen;
8417
8418         /* Free the old space */
8419         if (mg)
8420             Safefree(mg->mg_ptr);
8421
8422         s = SvPV_flags_const(sv, len, flags);
8423         if ((xf = _mem_collxfrm(s, len, &xlen, cBOOL(SvUTF8(sv))))) {
8424             if (! mg) {
8425                 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
8426                                  0, 0);
8427                 assert(mg);
8428             }
8429             mg->mg_ptr = xf;
8430             mg->mg_len = xlen;
8431         }
8432         else {
8433             if (mg) {
8434                 mg->mg_ptr = NULL;
8435                 mg->mg_len = -1;
8436             }
8437         }
8438     }
8439
8440     if (mg && mg->mg_ptr) {
8441         *nxp = mg->mg_len;
8442         return mg->mg_ptr + sizeof(PL_collation_ix);
8443     }
8444     else {
8445         *nxp = 0;
8446         return NULL;
8447     }
8448 }
8449
8450 #endif /* USE_LOCALE_COLLATE */
8451
8452 static char *
8453 S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8454 {
8455     SV * const tsv = newSV_type(SVt_NULL);
8456     ENTER;
8457     SAVEFREESV(tsv);
8458     sv_gets(tsv, fp, 0);
8459     sv_utf8_upgrade_nomg(tsv);
8460     SvCUR_set(sv,append);
8461     sv_catsv(sv,tsv);
8462     LEAVE;
8463     return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8464 }
8465
8466 static char *
8467 S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8468 {
8469     SSize_t bytesread;
8470     const STRLEN recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
8471       /* Grab the size of the record we're getting */
8472     char *buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
8473
8474     /* Go yank in */
8475 #ifdef __VMS
8476     int fd;
8477     Stat_t st;
8478
8479     /* With a true, record-oriented file on VMS, we need to use read directly
8480      * to ensure that we respect RMS record boundaries.  The user is responsible
8481      * for providing a PL_rs value that corresponds to the FAB$W_MRS (maximum
8482      * record size) field.  N.B. This is likely to produce invalid results on
8483      * varying-width character data when a record ends mid-character.
8484      */
8485     fd = PerlIO_fileno(fp);
8486     if (fd != -1
8487         && PerlLIO_fstat(fd, &st) == 0
8488         && (st.st_fab_rfm == FAB$C_VAR
8489             || st.st_fab_rfm == FAB$C_VFC
8490             || st.st_fab_rfm == FAB$C_FIX)) {
8491
8492         bytesread = PerlLIO_read(fd, buffer, recsize);
8493     }
8494     else /* in-memory file from PerlIO::Scalar
8495           * or not a record-oriented file
8496           */
8497 #endif
8498     {
8499         bytesread = PerlIO_read(fp, buffer, recsize);
8500
8501         /* At this point, the logic in sv_get() means that sv will
8502            be treated as utf-8 if the handle is utf8.
8503         */
8504         if (PerlIO_isutf8(fp) && bytesread > 0) {
8505             char *bend = buffer + bytesread;
8506             char *bufp = buffer;
8507             size_t charcount = 0;
8508             bool charstart = TRUE;
8509             STRLEN skip = 0;
8510
8511             while (charcount < recsize) {
8512                 /* count accumulated characters */
8513                 while (bufp < bend) {
8514                     if (charstart) {
8515                         skip = UTF8SKIP(bufp);
8516                     }
8517                     if (bufp + skip > bend) {
8518                         /* partial at the end */
8519                         charstart = FALSE;
8520                         break;
8521                     }
8522                     else {
8523                         ++charcount;
8524                         bufp += skip;
8525                         charstart = TRUE;
8526                     }
8527                 }
8528
8529                 if (charcount < recsize) {
8530                     STRLEN readsize;
8531                     STRLEN bufp_offset = bufp - buffer;
8532                     SSize_t morebytesread;
8533
8534                     /* originally I read enough to fill any incomplete
8535                        character and the first byte of the next
8536                        character if needed, but if there's many
8537                        multi-byte encoded characters we're going to be
8538                        making a read call for every character beyond
8539                        the original read size.
8540
8541                        So instead, read the rest of the character if
8542                        any, and enough bytes to match at least the
8543                        start bytes for each character we're going to
8544                        read.
8545                     */
8546                     if (charstart)
8547                         readsize = recsize - charcount;
8548                     else
8549                         readsize = skip - (bend - bufp) + recsize - charcount - 1;
8550                     buffer = SvGROW(sv, append + bytesread + readsize + 1) + append;
8551                     bend = buffer + bytesread;
8552                     morebytesread = PerlIO_read(fp, bend, readsize);
8553                     if (morebytesread <= 0) {
8554                         /* we're done, if we still have incomplete
8555                            characters the check code in sv_gets() will
8556                            warn about them.
8557
8558                            I'd originally considered doing
8559                            PerlIO_ungetc() on all but the lead
8560                            character of the incomplete character, but
8561                            read() doesn't do that, so I don't.
8562                         */
8563                         break;
8564                     }
8565
8566                     /* prepare to scan some more */
8567                     bytesread += morebytesread;
8568                     bend = buffer + bytesread;
8569                     bufp = buffer + bufp_offset;
8570                 }
8571             }
8572         }
8573     }
8574
8575     if (bytesread < 0)
8576         bytesread = 0;
8577     SvCUR_set(sv, bytesread + append);
8578     buffer[bytesread] = '\0';
8579     return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8580 }
8581
8582 /*
8583 =for apidoc sv_gets
8584
8585 Get a line from the filehandle and store it into the SV, optionally
8586 appending to the currently-stored string.  If C<append> is not 0, the
8587 line is appended to the SV instead of overwriting it.  C<append> should
8588 be set to the byte offset that the appended string should start at
8589 in the SV (typically, C<SvCUR(sv)> is a suitable choice).
8590
8591 =cut
8592 */
8593
8594 char *
8595 Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8596 {
8597     const char *rsptr;
8598     STRLEN rslen;
8599     STDCHAR rslast;
8600     STDCHAR *bp;
8601     SSize_t cnt;
8602     int i = 0;
8603     int rspara = 0;
8604
8605     PERL_ARGS_ASSERT_SV_GETS;
8606
8607     if (SvTHINKFIRST(sv))
8608         sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
8609     /* XXX. If you make this PVIV, then copy on write can copy scalars read
8610        from <>.
8611        However, perlbench says it's slower, because the existing swipe code
8612        is faster than copy on write.
8613        Swings and roundabouts.  */
8614     SvUPGRADE(sv, SVt_PV);
8615
8616     if (append) {
8617         /* line is going to be appended to the existing buffer in the sv */
8618         if (PerlIO_isutf8(fp)) {
8619             if (!SvUTF8(sv)) {
8620                 sv_utf8_upgrade_nomg(sv);
8621                 sv_pos_u2b(sv,&append,0);
8622             }
8623         } else if (SvUTF8(sv)) {
8624             return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append);
8625         }
8626     }
8627
8628     SvPOK_only(sv);
8629     if (!append) {
8630         /* not appending - "clear" the string by setting SvCUR to 0,
8631          * the pv is still avaiable. */
8632         SvCUR_set(sv,0);
8633     }
8634     if (PerlIO_isutf8(fp))
8635         SvUTF8_on(sv);
8636
8637     if (IN_PERL_COMPILETIME) {
8638         /* we always read code in line mode */
8639         rsptr = "\n";
8640         rslen = 1;
8641     }
8642     else if (RsSNARF(PL_rs)) {
8643         /* If it is a regular disk file use size from stat() as estimate
8644            of amount we are going to read -- may result in mallocing
8645            more memory than we really need if the layers below reduce
8646            the size we read (e.g. CRLF or a gzip layer).
8647          */
8648         Stat_t st;
8649         int fd = PerlIO_fileno(fp);
8650         if (fd >= 0 && (PerlLIO_fstat(fd, &st) == 0) && S_ISREG(st.st_mode))  {
8651             const Off_t offset = PerlIO_tell(fp);
8652             if (offset != (Off_t) -1 && st.st_size + append > offset) {
8653 #ifdef PERL_COPY_ON_WRITE
8654                 /* Add an extra byte for the sake of copy-on-write's
8655                  * buffer reference count. */
8656                 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
8657 #else
8658                 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
8659 #endif
8660             }
8661         }
8662         rsptr = NULL;
8663         rslen = 0;
8664     }
8665     else if (RsRECORD(PL_rs)) {
8666         return S_sv_gets_read_record(aTHX_ sv, fp, append);
8667     }
8668     else if (RsPARA(PL_rs)) {
8669         rsptr = "\n\n";
8670         rslen = 2;
8671         rspara = 1;
8672     }
8673     else {
8674         /* Get $/ i.e. PL_rs into same encoding as stream wants */
8675         if (PerlIO_isutf8(fp)) {
8676             rsptr = SvPVutf8(PL_rs, rslen);
8677         }
8678         else {
8679             if (SvUTF8(PL_rs)) {
8680                 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
8681                     Perl_croak(aTHX_ "Wide character in $/");
8682                 }
8683             }
8684             /* extract the raw pointer to the record separator */
8685             rsptr = SvPV_const(PL_rs, rslen);
8686         }
8687     }
8688
8689     /* rslast is the last character in the record separator
8690      * note we don't use rslast except when rslen is true, so the
8691      * null assign is a placeholder. */
8692     rslast = rslen ? rsptr[rslen - 1] : '\0';
8693
8694     if (rspara) {        /* have to do this both before and after */
8695                          /* to make sure file boundaries work right */
8696         while (1) {
8697             if (PerlIO_eof(fp))
8698                 return 0;
8699             i = PerlIO_getc(fp);
8700             if (i != '\n') {
8701                 if (i == -1)
8702                     return 0;
8703                 PerlIO_ungetc(fp,i);
8704                 break;
8705             }
8706         }
8707     }
8708
8709     /* See if we know enough about I/O mechanism to cheat it ! */
8710
8711     /* This used to be #ifdef test - it is made run-time test for ease
8712        of abstracting out stdio interface. One call should be cheap
8713        enough here - and may even be a macro allowing compile
8714        time optimization.
8715      */
8716
8717     if (PerlIO_fast_gets(fp)) {
8718     /*
8719      * We can do buffer based IO operations on this filehandle.
8720      *
8721      * This means we can bypass a lot of subcalls and process
8722      * the buffer directly, it also means we know the upper bound
8723      * on the amount of data we might read of the current buffer
8724      * into our sv. Knowing this allows us to preallocate the pv
8725      * to be able to hold that maximum, which allows us to simplify
8726      * a lot of logic. */
8727
8728     /*
8729      * We're going to steal some values from the stdio struct
8730      * and put EVERYTHING in the innermost loop into registers.
8731      */
8732     STDCHAR *ptr;       /* pointer into fp's read-ahead buffer */
8733     STRLEN bpx;         /* length of the data in the target sv
8734                            used to fix pointers after a SvGROW */
8735     I32 shortbuffered;  /* If the pv buffer is shorter than the amount
8736                            of data left in the read-ahead buffer.
8737                            If 0 then the pv buffer can hold the full
8738                            amount left, otherwise this is the amount it
8739                            can hold. */
8740
8741     /* Here is some breathtakingly efficient cheating */
8742
8743     /* When you read the following logic resist the urge to think
8744      * of record separators that are 1 byte long. They are an
8745      * uninteresting special (simple) case.
8746      *
8747      * Instead think of record separators which are at least 2 bytes
8748      * long, and keep in mind that we need to deal with such
8749      * separators when they cross a read-ahead buffer boundary.
8750      *
8751      * Also consider that we need to gracefully deal with separators
8752      * that may be longer than a single read ahead buffer.
8753      *
8754      * Lastly do not forget we want to copy the delimiter as well. We
8755      * are copying all data in the file _up_to_and_including_ the separator
8756      * itself.
8757      *
8758      * Now that you have all that in mind here is what is happening below:
8759      *
8760      * 1. When we first enter the loop we do some memory book keeping to see
8761      * how much free space there is in the target SV. (This sub assumes that
8762      * it is operating on the same SV most of the time via $_ and that it is
8763      * going to be able to reuse the same pv buffer each call.) If there is
8764      * "enough" room then we set "shortbuffered" to how much space there is
8765      * and start reading forward.
8766      *
8767      * 2. When we scan forward we copy from the read-ahead buffer to the target
8768      * SV's pv buffer. While we go we watch for the end of the read-ahead buffer,
8769      * and the end of the of pv, as well as for the "rslast", which is the last
8770      * char of the separator.
8771      *
8772      * 3. When scanning forward if we see rslast then we jump backwards in *pv*
8773      * (which has a "complete" record up to the point we saw rslast) and check
8774      * it to see if it matches the separator. If it does we are done. If it doesn't
8775      * we continue on with the scan/copy.
8776      *
8777      * 4. If we run out of read-ahead buffer (cnt goes to 0) then we have to get
8778      * the IO system to read the next buffer. We do this by doing a getc(), which
8779      * returns a single char read (or EOF), and prefills the buffer, and also
8780      * allows us to find out how full the buffer is.  We use this information to
8781      * SvGROW() the sv to the size remaining in the buffer, after which we copy
8782      * the returned single char into the target sv, and then go back into scan
8783      * forward mode.
8784      *
8785      * 5. If we run out of write-buffer then we SvGROW() it by the size of the
8786      * remaining space in the read-buffer.
8787      *
8788      * Note that this code despite its twisty-turny nature is pretty darn slick.
8789      * It manages single byte separators, multi-byte cross boundary separators,
8790      * and cross-read-buffer separators cleanly and efficiently at the cost
8791      * of potentially greatly overallocating the target SV.
8792      *
8793      * Yves
8794      */
8795
8796
8797     /* get the number of bytes remaining in the read-ahead buffer
8798      * on first call on a given fp this will return 0.*/
8799     cnt = PerlIO_get_cnt(fp);
8800
8801     /* make sure we have the room */
8802     if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
8803         /* Not room for all of it
8804            if we are looking for a separator and room for some
8805          */
8806         if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
8807             /* just process what we have room for */
8808             shortbuffered = cnt - SvLEN(sv) + append + 1;
8809             cnt -= shortbuffered;
8810         }
8811         else {
8812             /* ensure that the target sv has enough room to hold
8813              * the rest of the read-ahead buffer */
8814             shortbuffered = 0;
8815             /* remember that cnt can be negative */
8816             SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
8817         }
8818     }
8819     else {
8820         /* we have enough room to hold the full buffer, lets scream */
8821         shortbuffered = 0;
8822     }
8823
8824     /* extract the pointer to sv's string buffer, offset by append as necessary */
8825     bp = (STDCHAR*)SvPVX_const(sv) + append;  /* move these two too to registers */
8826     /* extract the point to the read-ahead buffer */
8827     ptr = (STDCHAR*)PerlIO_get_ptr(fp);
8828
8829     /* some trace debug output */
8830     DEBUG_P(PerlIO_printf(Perl_debug_log,
8831         "Screamer: entering, ptr=%" UVuf ", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
8832     DEBUG_P(PerlIO_printf(Perl_debug_log,
8833         "Screamer: entering: PerlIO * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%"
8834          UVuf "\n",
8835                PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8836                PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
8837
8838     for (;;) {
8839       screamer:
8840         /* if there is stuff left in the read-ahead buffer */
8841         if (cnt > 0) {
8842             /* if there is a separator */
8843             if (rslen) {
8844                 /* find next rslast */
8845                 STDCHAR *p;
8846
8847                 /* shortcut common case of blank line */
8848                 cnt--;
8849                 if ((*bp++ = *ptr++) == rslast)
8850                     goto thats_all_folks;
8851
8852                 p = (STDCHAR *)memchr(ptr, rslast, cnt);
8853                 if (p) {
8854                     SSize_t got = p - ptr + 1;
8855                     Copy(ptr, bp, got, STDCHAR);
8856                     ptr += got;
8857                     bp  += got;
8858                     cnt -= got;
8859                     goto thats_all_folks;
8860                 }
8861                 Copy(ptr, bp, cnt, STDCHAR);
8862                 ptr += cnt;
8863                 bp  += cnt;
8864                 cnt = 0;
8865             }
8866             else {
8867                 /* no separator, slurp the full buffer */
8868                 Copy(ptr, bp, cnt, char);            /* this     |  eat */
8869                 bp += cnt;                           /* screams  |  dust */
8870                 ptr += cnt;                          /* louder   |  sed :-) */
8871                 cnt = 0;
8872                 assert (!shortbuffered);
8873                 goto cannot_be_shortbuffered;
8874             }
8875         }
8876
8877         if (shortbuffered) {            /* oh well, must extend */
8878             /* we didnt have enough room to fit the line into the target buffer
8879              * so we must extend the target buffer and keep going */
8880             cnt = shortbuffered;
8881             shortbuffered = 0;
8882             bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8883             SvCUR_set(sv, bpx);
8884             /* extned the target sv's buffer so it can hold the full read-ahead buffer */
8885             SvGROW(sv, SvLEN(sv) + append + cnt + 2);
8886             bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8887             continue;
8888         }
8889
8890     cannot_be_shortbuffered:
8891         /* we need to refill the read-ahead buffer if possible */
8892
8893         DEBUG_P(PerlIO_printf(Perl_debug_log,
8894                              "Screamer: going to getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8895                               PTR2UV(ptr),(IV)cnt));
8896         PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
8897
8898         DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8899            "Screamer: pre: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8900             PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8901             PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8902
8903         /*
8904             call PerlIO_getc() to let it prefill the lookahead buffer
8905
8906             This used to call 'filbuf' in stdio form, but as that behaves like
8907             getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
8908             another abstraction.
8909
8910             Note we have to deal with the char in 'i' if we are not at EOF
8911         */
8912         bpx = bp - (STDCHAR*)SvPVX_const(sv);
8913         /* signals might be called here, possibly modifying sv */
8914         i   = PerlIO_getc(fp);          /* get more characters */
8915         bp = (STDCHAR*)SvPVX_const(sv) + bpx;
8916
8917         DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8918            "Screamer: post: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8919             PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8920             PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8921
8922         /* find out how much is left in the read-ahead buffer, and rextract its pointer */
8923         cnt = PerlIO_get_cnt(fp);
8924         ptr = (STDCHAR*)PerlIO_get_ptr(fp);     /* reregisterize cnt and ptr */
8925         DEBUG_P(PerlIO_printf(Perl_debug_log,
8926             "Screamer: after getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8927             PTR2UV(ptr),(IV)cnt));
8928
8929         if (i == EOF)                   /* all done for ever? */
8930             goto thats_really_all_folks;
8931
8932         /* make sure we have enough space in the target sv */
8933         bpx = bp - (STDCHAR*)SvPVX_const(sv);   /* box up before relocation */
8934         SvCUR_set(sv, bpx);
8935         SvGROW(sv, bpx + cnt + 2);
8936         bp = (STDCHAR*)SvPVX_const(sv) + bpx;   /* unbox after relocation */
8937
8938         /* copy of the char we got from getc() */
8939         *bp++ = (STDCHAR)i;             /* store character from PerlIO_getc */
8940
8941         /* make sure we deal with the i being the last character of a separator */
8942         if (rslen && (STDCHAR)i == rslast)  /* all done for now? */
8943             goto thats_all_folks;
8944     }
8945
8946   thats_all_folks:
8947     /* check if we have actually found the separator - only really applies
8948      * when rslen > 1 */
8949     if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
8950           memNE((char*)bp - rslen, rsptr, rslen))
8951         goto screamer;                          /* go back to the fray */
8952   thats_really_all_folks:
8953     if (shortbuffered)
8954         cnt += shortbuffered;
8955     DEBUG_P(PerlIO_printf(Perl_debug_log,
8956          "Screamer: quitting, ptr=%" UVuf ", cnt=%" IVdf "\n",PTR2UV(ptr),(IV)cnt));
8957     PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt);  /* put these back or we're in trouble */
8958     DEBUG_P(PerlIO_printf(Perl_debug_log,
8959         "Screamer: end: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf
8960         "\n",
8961         PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8962         PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8963     *bp = '\0';
8964     SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv));      /* set length */
8965     DEBUG_P(PerlIO_printf(Perl_debug_log,
8966         "Screamer: done, len=%ld, string=|%.*s|\n",
8967         (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
8968     }
8969    else
8970     {
8971        /*The big, slow, and stupid way. */
8972         STDCHAR buf[8192];
8973
8974       screamer2:
8975         if (rslen) {
8976             const STDCHAR * const bpe = buf + sizeof(buf);
8977             bp = buf;
8978             while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
8979                 ; /* keep reading */
8980             cnt = bp - buf;
8981         }
8982         else {
8983             cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
8984             /* Accommodate broken VAXC compiler, which applies U8 cast to
8985              * both args of ?: operator, causing EOF to change into 255
8986              */
8987             if (cnt > 0)
8988                  i = (U8)buf[cnt - 1];
8989             else
8990                  i = EOF;
8991         }
8992
8993         if (cnt < 0)
8994             cnt = 0;  /* we do need to re-set the sv even when cnt <= 0 */
8995         if (append)
8996             sv_catpvn_nomg(sv, (char *) buf, cnt);
8997         else
8998             sv_setpvn(sv, (char *) buf, cnt);   /* "nomg" is implied */
8999
9000         if (i != EOF &&                 /* joy */
9001             (!rslen ||
9002              SvCUR(sv) < rslen ||
9003              memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
9004         {
9005             append = -1;
9006             /*
9007              * If we're reading from a TTY and we get a short read,
9008              * indicating that the user hit his EOF character, we need
9009              * to notice it now, because if we try to read from the TTY
9010              * again, the EOF condition will disappear.
9011              *
9012              * The comparison of cnt to sizeof(buf) is an optimization
9013              * that prevents unnecessary calls to feof().
9014              *
9015              * - jik 9/25/96
9016              */
9017             if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
9018                 goto screamer2;
9019         }
9020
9021     }
9022
9023     if (rspara) {               /* have to do this both before and after */
9024         while (i != EOF) {      /* to make sure file boundaries work right */
9025             i = PerlIO_getc(fp);
9026             if (i != '\n') {
9027                 PerlIO_ungetc(fp,i);
9028                 break;
9029             }
9030         }
9031     }
9032
9033     return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
9034 }
9035
9036 /*
9037 =for apidoc sv_inc
9038 =for apidoc_item sv_inc_nomg
9039
9040 These auto-increment the value in the SV, doing string to numeric conversion
9041 if necessary.  They both handle operator overloading.
9042
9043 They differ only in that C<sv_inc> performs 'get' magic; C<sv_inc_nomg> skips
9044 any magic.
9045
9046 =cut
9047 */
9048
9049 void
9050 Perl_sv_inc(pTHX_ SV *const sv)
9051 {
9052     if (!sv)
9053         return;
9054     SvGETMAGIC(sv);
9055     sv_inc_nomg(sv);
9056 }
9057
9058 void
9059 Perl_sv_inc_nomg(pTHX_ SV *const sv)
9060 {
9061     char *d;
9062     int flags;
9063
9064     if (!sv)
9065         return;
9066     if (SvTHINKFIRST(sv)) {
9067         if (SvREADONLY(sv)) {
9068                 Perl_croak_no_modify();
9069         }
9070         if (SvROK(sv)) {
9071             IV i;
9072             if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg))
9073                 return;
9074             i = PTR2IV(SvRV(sv));
9075             sv_unref(sv);
9076             sv_setiv(sv, i);
9077         }
9078         else sv_force_normal_flags(sv, 0);
9079     }
9080     flags = SvFLAGS(sv);
9081     if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
9082         /* It's (privately or publicly) a float, but not tested as an
9083            integer, so test it to see. */
9084         (void) SvIV(sv);
9085         flags = SvFLAGS(sv);
9086     }
9087     if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9088         /* It's publicly an integer, or privately an integer-not-float */
9089 #ifdef PERL_PRESERVE_IVUV
9090       oops_its_int:
9091 #endif
9092         if (SvIsUV(sv)) {
9093             if (SvUVX(sv) == UV_MAX)
9094                 sv_setnv(sv, UV_MAX_P1);
9095             else {
9096                 (void)SvIOK_only_UV(sv);
9097                 SvUV_set(sv, SvUVX(sv) + 1);
9098             }
9099         } else {
9100             if (SvIVX(sv) == IV_MAX)
9101                 sv_setuv(sv, (UV)IV_MAX + 1);
9102             else {
9103                 (void)SvIOK_only(sv);
9104                 SvIV_set(sv, SvIVX(sv) + 1);
9105             }
9106         }
9107         return;
9108     }
9109     if (flags & SVp_NOK) {
9110         const NV was = SvNVX(sv);
9111         if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9112             /* If NVX was NaN, the following comparisons return always false */
9113             UNLIKELY(was >= NV_OVERFLOWS_INTEGERS_AT ||
9114                      was < -NV_OVERFLOWS_INTEGERS_AT) &&
9115 #if defined(NAN_COMPARE_BROKEN)
9116             LIKELY(!Perl_isinfnan(was))
9117 #else
9118             LIKELY(!Perl_isinf(was))
9119 #endif
9120             ) {
9121             /* diag_listed_as: Lost precision when %s %f by 1 */
9122             Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9123                            "Lost precision when incrementing %" NVff " by 1",
9124                            was);
9125         }
9126         (void)SvNOK_only(sv);
9127         SvNV_set(sv, was + 1.0);
9128         return;
9129     }
9130
9131     /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9132     if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9133         Perl_croak_no_modify();
9134
9135     if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
9136         if ((flags & SVTYPEMASK) < SVt_PVIV)
9137             sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
9138         (void)SvIOK_only(sv);
9139         SvIV_set(sv, 1);
9140         return;
9141     }
9142     d = SvPVX(sv);
9143     while (isALPHA(*d)) d++;
9144     while (isDIGIT(*d)) d++;
9145     if (d < SvEND(sv)) {
9146         const int numtype = grok_number_flags(SvPVX_const(sv), SvCUR(sv), NULL, PERL_SCAN_TRAILING);
9147 #ifdef PERL_PRESERVE_IVUV
9148         /* Got to punt this as an integer if needs be, but we don't issue
9149            warnings. Probably ought to make the sv_iv_please() that does
9150            the conversion if possible, and silently.  */
9151         if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9152             /* Need to try really hard to see if it's an integer.
9153                9.22337203685478e+18 is an integer.
9154                but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9155                so $a="9.22337203685478e+18"; $a+0; $a++
9156                needs to be the same as $a="9.22337203685478e+18"; $a++
9157                or we go insane. */
9158
9159             (void) sv_2iv(sv);
9160             if (SvIOK(sv))
9161                 goto oops_its_int;
9162
9163             /* sv_2iv *should* have made this an NV */
9164             if (flags & SVp_NOK) {
9165                 (void)SvNOK_only(sv);
9166                 SvNV_set(sv, SvNVX(sv) + 1.0);
9167                 return;
9168             }
9169             /* I don't think we can get here. Maybe I should assert this
9170                And if we do get here I suspect that sv_setnv will croak. NWC
9171                Fall through. */
9172             DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9173                                   SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9174         }
9175 #endif /* PERL_PRESERVE_IVUV */
9176         if (!numtype && ckWARN(WARN_NUMERIC))
9177             not_incrementable(sv);
9178         sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
9179         return;
9180     }
9181     d--;
9182     while (d >= SvPVX_const(sv)) {
9183         if (isDIGIT(*d)) {
9184             if (++*d <= '9')
9185                 return;
9186             *(d--) = '0';
9187         }
9188         else {
9189 #ifdef EBCDIC
9190             /* MKS: The original code here died if letters weren't consecutive.
9191              * at least it didn't have to worry about non-C locales.  The
9192              * new code assumes that ('z'-'a')==('Z'-'A'), letters are
9193              * arranged in order (although not consecutively) and that only
9194              * [A-Za-z] are accepted by isALPHA in the C locale.
9195              */
9196             if (isALPHA_FOLD_NE(*d, 'z')) {
9197                 do { ++*d; } while (!isALPHA(*d));
9198                 return;
9199             }
9200             *(d--) -= 'z' - 'a';
9201 #else
9202             ++*d;
9203             if (isALPHA(*d))
9204                 return;
9205             *(d--) -= 'z' - 'a' + 1;
9206 #endif
9207         }
9208     }
9209     /* oh,oh, the number grew */
9210     SvGROW(sv, SvCUR(sv) + 2);
9211     SvCUR_set(sv, SvCUR(sv) + 1);
9212     for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
9213         *d = d[-1];
9214     if (isDIGIT(d[1]))
9215         *d = '1';
9216     else
9217         *d = d[1];
9218 }
9219
9220 /*
9221 =for apidoc sv_dec
9222 =for apidoc_item sv_dec_nomg
9223
9224 These auto-decrement the value in the SV, doing string to numeric conversion
9225 if necessary.  They both handle operator overloading.
9226
9227 They differ only in that:
9228
9229 C<sv_dec> handles 'get' magic; C<sv_dec_nomg> skips 'get' magic.
9230
9231 =cut
9232 */
9233
9234 void
9235 Perl_sv_dec(pTHX_ SV *const sv)
9236 {
9237     if (!sv)
9238         return;
9239     SvGETMAGIC(sv);
9240     sv_dec_nomg(sv);
9241 }
9242
9243 void
9244 Perl_sv_dec_nomg(pTHX_ SV *const sv)
9245 {
9246     int flags;
9247
9248     if (!sv)
9249         return;
9250     if (SvTHINKFIRST(sv)) {
9251         if (SvREADONLY(sv)) {
9252                 Perl_croak_no_modify();
9253         }
9254         if (SvROK(sv)) {
9255             IV i;
9256             if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg))
9257                 return;
9258             i = PTR2IV(SvRV(sv));
9259             sv_unref(sv);
9260             sv_setiv(sv, i);
9261         }
9262         else sv_force_normal_flags(sv, 0);
9263     }
9264     /* Unlike sv_inc we don't have to worry about string-never-numbers
9265        and keeping them magic. But we mustn't warn on punting */
9266     flags = SvFLAGS(sv);
9267     if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9268         /* It's publicly an integer, or privately an integer-not-float */
9269 #ifdef PERL_PRESERVE_IVUV
9270       oops_its_int:
9271 #endif
9272         if (SvIsUV(sv)) {
9273             if (SvUVX(sv) == 0) {
9274                 (void)SvIOK_only(sv);
9275                 SvIV_set(sv, -1);
9276             }
9277             else {
9278                 (void)SvIOK_only_UV(sv);
9279                 SvUV_set(sv, SvUVX(sv) - 1);
9280             }
9281         } else {
9282             if (SvIVX(sv) == IV_MIN) {
9283                 sv_setnv(sv, (NV)IV_MIN);
9284                 goto oops_its_num;
9285             }
9286             else {
9287                 (void)SvIOK_only(sv);
9288                 SvIV_set(sv, SvIVX(sv) - 1);
9289             }
9290         }
9291         return;
9292     }
9293     if (flags & SVp_NOK) {
9294     oops_its_num:
9295         {
9296             const NV was = SvNVX(sv);
9297             if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9298                 /* If NVX was NaN, these comparisons return always false */
9299                 UNLIKELY(was <= -NV_OVERFLOWS_INTEGERS_AT ||
9300                          was > NV_OVERFLOWS_INTEGERS_AT) &&
9301 #if defined(NAN_COMPARE_BROKEN)
9302                 LIKELY(!Perl_isinfnan(was))
9303 #else
9304                 LIKELY(!Perl_isinf(was))
9305 #endif
9306                 ) {
9307                 /* diag_listed_as: Lost precision when %s %f by 1 */
9308                 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9309                                "Lost precision when decrementing %" NVff " by 1",
9310                                was);
9311             }
9312             (void)SvNOK_only(sv);
9313             SvNV_set(sv, was - 1.0);
9314             return;
9315         }
9316     }
9317
9318     /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9319     if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9320         Perl_croak_no_modify();
9321
9322     if (!(flags & SVp_POK)) {
9323         if ((flags & SVTYPEMASK) < SVt_PVIV)
9324             sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
9325         SvIV_set(sv, -1);
9326         (void)SvIOK_only(sv);
9327         return;
9328     }
9329 #ifdef PERL_PRESERVE_IVUV
9330     {
9331         const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
9332         if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9333             /* Need to try really hard to see if it's an integer.
9334                9.22337203685478e+18 is an integer.
9335                but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9336                so $a="9.22337203685478e+18"; $a+0; $a--
9337                needs to be the same as $a="9.22337203685478e+18"; $a--
9338                or we go insane. */
9339
9340             (void) sv_2iv(sv);
9341             if (SvIOK(sv))
9342                 goto oops_its_int;
9343
9344             /* sv_2iv *should* have made this an NV */
9345             if (flags & SVp_NOK) {
9346                 (void)SvNOK_only(sv);
9347                 SvNV_set(sv, SvNVX(sv) - 1.0);
9348                 return;
9349             }
9350             /* I don't think we can get here. Maybe I should assert this
9351                And if we do get here I suspect that sv_setnv will croak. NWC
9352                Fall through. */
9353             DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9354                                   SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9355         }
9356     }
9357 #endif /* PERL_PRESERVE_IVUV */
9358     sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0);   /* punt */
9359 }
9360
9361 /* this define is used to eliminate a chunk of duplicated but shared logic
9362  * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be
9363  * used anywhere but here - yves
9364  */
9365 #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \
9366     STMT_START {      \
9367         SSize_t ix = ++PL_tmps_ix;              \
9368         if (UNLIKELY(ix >= PL_tmps_max))        \
9369             ix = tmps_grow_p(ix);                       \
9370         PL_tmps_stack[ix] = (AnSv); \
9371     } STMT_END
9372
9373 /*
9374 =for apidoc sv_mortalcopy
9375
9376 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
9377 The new SV is marked as mortal.  It will be destroyed "soon", either by an
9378 explicit call to C<FREETMPS>, or by an implicit call at places such as
9379 statement boundaries.  See also C<L</sv_newmortal>> and C<L</sv_2mortal>>.
9380
9381 =for apidoc sv_mortalcopy_flags
9382
9383 Like C<sv_mortalcopy>, but the extra C<flags> are passed to the
9384 C<sv_setsv_flags>.
9385
9386 =cut
9387 */
9388
9389 /* Make a string that will exist for the duration of the expression
9390  * evaluation.  Actually, it may have to last longer than that, but
9391  * hopefully we won't free it until it has been assigned to a
9392  * permanent location. */
9393
9394 SV *
9395 Perl_sv_mortalcopy_flags(pTHX_ SV *const oldstr, U32 flags)
9396 {
9397     SV *sv;
9398
9399     if (flags & SV_GMAGIC)
9400         SvGETMAGIC(oldstr); /* before new_SV, in case it dies */
9401     new_SV(sv);
9402     sv_setsv_flags(sv,oldstr,flags & ~SV_GMAGIC);
9403     PUSH_EXTEND_MORTAL__SV_C(sv);
9404     SvTEMP_on(sv);
9405     return sv;
9406 }
9407
9408 /*
9409 =for apidoc sv_newmortal
9410
9411 Creates a new null SV which is mortal.  The reference count of the SV is
9412 set to 1.  It will be destroyed "soon", either by an explicit call to
9413 C<FREETMPS>, or by an implicit call at places such as statement boundaries.
9414 See also C<L</sv_mortalcopy>> and C<L</sv_2mortal>>.
9415
9416 =cut
9417 */
9418
9419 SV *
9420 Perl_sv_newmortal(pTHX)
9421 {
9422     SV *sv;
9423
9424     new_SV(sv);
9425     SvFLAGS(sv) = SVs_TEMP;
9426     PUSH_EXTEND_MORTAL__SV_C(sv);
9427     return sv;
9428 }
9429
9430
9431 /*
9432 =for apidoc newSVpvn_flags
9433
9434 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9435 characters) into it.  The reference count for the
9436 SV is set to 1.  Note that if C<len> is zero, Perl will create a zero length
9437 string.  You are responsible for ensuring that the source string is at least
9438 C<len> bytes long.  If the C<s> argument is NULL the new SV will be undefined.
9439 Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
9440 If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before
9441 returning.  If C<SVf_UTF8> is set, C<s>
9442 is considered to be in UTF-8 and the
9443 C<SVf_UTF8> flag will be set on the new SV.
9444 C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
9445
9446     #define newSVpvn_utf8(s, len, u)                    \
9447         newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
9448
9449 =for apidoc Amnh||SVs_TEMP
9450
9451 =cut
9452 */
9453
9454 SV *
9455 Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
9456 {
9457     SV *sv;
9458
9459     /* All the flags we don't support must be zero.
9460        And we're new code so I'm going to assert this from the start.  */
9461     assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
9462     sv = newSV_type(SVt_PV);
9463     sv_setpvn_fresh(sv,s,len);
9464
9465     /* This code used to do a sv_2mortal(), however we now unroll the call to
9466      * sv_2mortal() and do what it does ourselves here.  Since we have asserted
9467      * that flags can only have the SVf_UTF8 and/or SVs_TEMP flags set above we
9468      * can use it to enable the sv flags directly (bypassing SvTEMP_on), which
9469      * in turn means we dont need to mask out the SVf_UTF8 flag below, which
9470      * means that we eliminate quite a few steps than it looks - Yves
9471      * (explaining patch by gfx) */
9472
9473     SvFLAGS(sv) |= flags;
9474
9475     if(flags & SVs_TEMP){
9476         PUSH_EXTEND_MORTAL__SV_C(sv);
9477     }
9478
9479     return sv;
9480 }
9481
9482 /*
9483 =for apidoc sv_2mortal
9484
9485 Marks an existing SV as mortal.  The SV will be destroyed "soon", either
9486 by an explicit call to C<FREETMPS>, or by an implicit call at places such as
9487 statement boundaries.  C<SvTEMP()> is turned on which means that the SV's
9488 string buffer can be "stolen" if this SV is copied.  See also
9489 C<L</sv_newmortal>> and C<L</sv_mortalcopy>>.
9490
9491 =cut
9492 */
9493
9494 SV *
9495 Perl_sv_2mortal(pTHX_ SV *const sv)
9496 {
9497     if (!sv)
9498         return sv;
9499     if (SvIMMORTAL(sv))
9500         return sv;
9501     PUSH_EXTEND_MORTAL__SV_C(sv);
9502     SvTEMP_on(sv);
9503     return sv;
9504 }
9505
9506 /*
9507 =for apidoc newSVpv
9508
9509 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9510 characters) into it.  The reference count for the
9511 SV is set to 1.  If C<len> is zero, Perl will compute the length using
9512 C<strlen()>, (which means if you use this option, that C<s> can't have embedded
9513 C<NUL> characters and has to have a terminating C<NUL> byte).
9514
9515 This function can cause reliability issues if you are likely to pass in
9516 empty strings that are not null terminated, because it will run
9517 strlen on the string and potentially run past valid memory.
9518
9519 Using L</newSVpvn> is a safer alternative for non C<NUL> terminated strings.
9520 For string literals use L</newSVpvs> instead.  This function will work fine for
9521 C<NUL> terminated strings, but if you want to avoid the if statement on whether
9522 to call C<strlen> use C<newSVpvn> instead (calling C<strlen> yourself).
9523
9524 =cut
9525 */
9526
9527 SV *
9528 Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
9529 {
9530     SV *sv = newSV_type(SVt_PV);
9531     sv_setpvn_fresh(sv, s, len || s == NULL ? len : strlen(s));
9532     return sv;
9533 }
9534
9535 /*
9536 =for apidoc newSVpvn
9537
9538 Creates a new SV and copies a string into it, which may contain C<NUL> characters
9539 (C<\0>) and other binary data.  The reference count for the SV is set to 1.
9540 Note that if C<len> is zero, Perl will create a zero length (Perl) string.  You
9541 are responsible for ensuring that the source buffer is at least
9542 C<len> bytes long.  If the C<buffer> argument is NULL the new SV will be
9543 undefined.
9544
9545 =cut
9546 */
9547
9548 SV *
9549 Perl_newSVpvn(pTHX_ const char *const buffer, const STRLEN len)
9550 {
9551     SV *sv = newSV_type(SVt_PV);
9552     sv_setpvn_fresh(sv,buffer,len);
9553     return sv;
9554 }
9555
9556 /*
9557 =for apidoc newSVhek_mortal
9558
9559 Creates a new mortal SV from the hash key structure.  It will generate
9560 scalars that point to the shared string table where possible.  Returns
9561 a new (undefined) SV if C<hek> is NULL.
9562
9563 This is more efficient than using sv_2mortal(newSVhek( ... ))
9564
9565 =cut
9566 */
9567
9568 SV *
9569 Perl_newSVhek_mortal(pTHX_ const HEK *const hek)
9570 {
9571     SV * const sv = newSVhek(hek);
9572     assert(sv);
9573     assert(!SvIMMORTAL(sv));
9574
9575     PUSH_EXTEND_MORTAL__SV_C(sv);
9576     SvTEMP_on(sv);
9577     return sv;
9578 }
9579
9580 /*
9581 =for apidoc newSVhek
9582
9583 Creates a new SV from the hash key structure.  It will generate scalars that
9584 point to the shared string table where possible.  Returns a new (undefined)
9585 SV if C<hek> is NULL.
9586
9587 =cut
9588 */
9589
9590 SV *
9591 Perl_newSVhek(pTHX_ const HEK *const hek)
9592 {
9593     if (!hek) {
9594         SV *sv;
9595
9596         new_SV(sv);
9597         return sv;
9598     }
9599
9600     if (HEK_LEN(hek) == HEf_SVKEY) {
9601         return newSVsv(*(SV**)HEK_KEY(hek));
9602     } else {
9603         const int flags = HEK_FLAGS(hek);
9604         if (flags & HVhek_WASUTF8) {
9605             /* Trouble :-)
9606                Andreas would like keys he put in as utf8 to come back as utf8
9607             */
9608             STRLEN utf8_len = HEK_LEN(hek);
9609             SV * const sv = newSV_type(SVt_PV);
9610             char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
9611             /* bytes_to_utf8() allocates a new string, which we can repurpose: */
9612             sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
9613             SvUTF8_on (sv);
9614             return sv;
9615         } else if (flags & HVhek_NOTSHARED) {
9616             /* A hash that isn't using shared hash keys has to have
9617                the flag in every key so that we know not to try to call
9618                share_hek_hek on it.  */
9619
9620             SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
9621             if (HEK_UTF8(hek))
9622                 SvUTF8_on (sv);
9623             return sv;
9624         }
9625         /* This will be overwhelminly the most common case.  */
9626         {
9627             /* Inline most of newSVpvn_share(), because share_hek_hek() is far
9628                more efficient than sharepvn().  */
9629             SV *sv = newSV_type(SVt_PV);
9630
9631             SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
9632             SvCUR_set(sv, HEK_LEN(hek));
9633             SvLEN_set(sv, 0);
9634             SvIsCOW_on(sv);
9635             SvPOK_on(sv);
9636             if (HEK_UTF8(hek))
9637                 SvUTF8_on(sv);
9638             return sv;
9639         }
9640     }
9641 }
9642
9643 /*
9644 =for apidoc newSVpvn_share
9645
9646 Creates a new SV with its C<SvPVX_const> pointing to a shared string in the string
9647 table.  If the string does not already exist in the table, it is
9648 created first.  Turns on the C<SvIsCOW> flag (or C<READONLY>
9649 and C<FAKE> in 5.16 and earlier).  If the C<hash> parameter
9650 is non-zero, that value is used; otherwise the hash is computed.
9651 The string's hash can later be retrieved from the SV
9652 with the C<L</SvSHARED_HASH>> macro.  The idea here is
9653 that as the string table is used for shared hash keys these strings will have
9654 C<SvPVX_const == HeKEY> and hash lookup will avoid string compare.
9655
9656 =cut
9657 */
9658
9659 SV *
9660 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
9661 {
9662     SV *sv;
9663     bool is_utf8 = FALSE;
9664     const char *const orig_src = src;
9665
9666     if (len < 0) {
9667         STRLEN tmplen = -len;
9668         is_utf8 = TRUE;
9669         /* See the note in hv.c:hv_fetch() --jhi */
9670         src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9671         len = tmplen;
9672     }
9673     if (!hash)
9674         PERL_HASH(hash, src, len);
9675     sv = newSV_type(SVt_PV);
9676     /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
9677        changes here, update it there too.  */
9678     SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
9679     SvCUR_set(sv, len);
9680     SvLEN_set(sv, 0);
9681     SvIsCOW_on(sv);
9682     SvPOK_on(sv);
9683     if (is_utf8)
9684         SvUTF8_on(sv);
9685     if (src != orig_src)
9686         Safefree(src);
9687     return sv;
9688 }
9689
9690 /*
9691 =for apidoc newSVpv_share
9692
9693 Like C<newSVpvn_share>, but takes a C<NUL>-terminated string instead of a
9694 string/length pair.
9695
9696 =cut
9697 */
9698
9699 SV *
9700 Perl_newSVpv_share(pTHX_ const char *src, U32 hash)
9701 {
9702     return newSVpvn_share(src, strlen(src), hash);
9703 }
9704
9705 #if defined(MULTIPLICITY)
9706
9707 /* pTHX_ magic can't cope with varargs, so this is a no-context
9708  * version of the main function, (which may itself be aliased to us).
9709  * Don't access this version directly.
9710  */
9711
9712 SV *
9713 Perl_newSVpvf_nocontext(const char *const pat, ...)
9714 {
9715     dTHX;
9716     SV *sv;
9717     va_list args;
9718
9719     PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
9720
9721     va_start(args, pat);
9722     sv = vnewSVpvf(pat, &args);
9723     va_end(args);
9724     return sv;
9725 }
9726 #endif
9727
9728 /*
9729 =for apidoc newSVpvf
9730
9731 Creates a new SV and initializes it with the string formatted like
9732 C<sv_catpvf>.
9733
9734 =for apidoc newSVpvf_nocontext
9735 Like C<L</newSVpvf>> but does not take a thread context (C<aTHX>) parameter,
9736 so is used in situations where the caller doesn't already have the thread
9737 context.
9738
9739 =for apidoc vnewSVpvf
9740 Like C<L</newSVpvf>> but the arguments are an encapsulated argument list.
9741
9742 =cut
9743 */
9744
9745 SV *
9746 Perl_newSVpvf(pTHX_ const char *const pat, ...)
9747 {
9748     SV *sv;
9749     va_list args;
9750
9751     PERL_ARGS_ASSERT_NEWSVPVF;
9752
9753     va_start(args, pat);
9754     sv = vnewSVpvf(pat, &args);
9755     va_end(args);
9756     return sv;
9757 }
9758
9759 /* backend for newSVpvf() and newSVpvf_nocontext() */
9760
9761 SV *
9762 Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
9763 {
9764     SV *sv;
9765
9766     PERL_ARGS_ASSERT_VNEWSVPVF;
9767
9768     new_SV(sv);
9769     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
9770     return sv;
9771 }
9772
9773 /*
9774 =for apidoc newSVnv
9775
9776 Creates a new SV and copies a floating point value into it.
9777 The reference count for the SV is set to 1.
9778
9779 =cut
9780 */
9781
9782 SV *
9783 Perl_newSVnv(pTHX_ const NV n)
9784 {
9785     SV *sv = newSV_type(SVt_NV);
9786     (void)SvNOK_on(sv);
9787
9788     SvNV_set(sv, n);
9789     SvTAINT(sv);
9790
9791     return sv;
9792 }
9793
9794 /*
9795 =for apidoc newSViv
9796
9797 Creates a new SV and copies an integer into it.  The reference count for the
9798 SV is set to 1.
9799
9800 =cut
9801 */
9802
9803 SV *
9804 Perl_newSViv(pTHX_ const IV i)
9805 {
9806     SV *sv = newSV_type(SVt_IV);
9807     (void)SvIOK_on(sv);
9808
9809     SvIV_set(sv, i);
9810     SvTAINT(sv);
9811
9812     return sv;
9813 }
9814
9815 /*
9816 =for apidoc newSVuv
9817
9818 Creates a new SV and copies an unsigned integer into it.
9819 The reference count for the SV is set to 1.
9820
9821 =cut
9822 */
9823
9824 SV *
9825 Perl_newSVuv(pTHX_ const UV u)
9826 {
9827     SV *sv;
9828
9829     /* Inlining ONLY the small relevant subset of sv_setuv here
9830      * for performance. Makes a significant difference. */
9831
9832     /* Using ivs is more efficient than using uvs - see sv_setuv */
9833     if (u <= (UV)IV_MAX) {
9834         return newSViv((IV)u);
9835     }
9836
9837     new_SV(sv);
9838
9839     /* We're starting from SVt_FIRST, so provided that's
9840      * actual 0, we don't have to unset any SV type flags
9841      * to promote to SVt_IV. */
9842     STATIC_ASSERT_STMT(SVt_FIRST == 0);
9843
9844     SET_SVANY_FOR_BODYLESS_IV(sv);
9845     SvFLAGS(sv) |= SVt_IV;
9846     (void)SvIOK_on(sv);
9847     (void)SvIsUV_on(sv);
9848
9849     SvUV_set(sv, u);
9850     SvTAINT(sv);
9851
9852     return sv;
9853 }
9854
9855 /*
9856 =for apidoc newSVbool
9857
9858 Creates a new SV boolean.
9859
9860 =cut
9861 */
9862
9863 SV *
9864 Perl_newSVbool(pTHX_ bool bool_val)
9865 {
9866     PERL_ARGS_ASSERT_NEWSVBOOL;
9867     SV *sv = newSVsv(bool_val ? &PL_sv_yes : &PL_sv_no);
9868
9869     return sv;
9870 }
9871
9872 /*
9873 =for apidoc newSV_true
9874
9875 Creates a new SV that is a boolean true.
9876
9877 =cut
9878 */
9879 SV *
9880 Perl_newSV_true(pTHX)
9881 {
9882     PERL_ARGS_ASSERT_NEWSV_TRUE;
9883     SV *sv = newSVsv(&PL_sv_yes);
9884
9885     return sv;
9886 }
9887
9888 /*
9889 =for apidoc newSV_false
9890
9891 Creates a new SV that is a boolean false.
9892
9893 =cut
9894 */
9895
9896 SV *
9897 Perl_newSV_false(pTHX)
9898 {
9899     PERL_ARGS_ASSERT_NEWSV_FALSE;
9900     SV *sv = newSVsv(&PL_sv_no);
9901
9902     return sv;
9903 }
9904
9905 /* newRV_inc is the official function name to use now.
9906  * newRV_inc is in fact #defined to newRV in sv.h
9907  */
9908
9909 SV *
9910 Perl_newRV(pTHX_ SV *const sv)
9911 {
9912     PERL_ARGS_ASSERT_NEWRV;
9913
9914     return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
9915 }
9916
9917 /*
9918 =for apidoc newSVsv
9919 =for apidoc_item newSVsv_flags
9920 =for apidoc_item newSVsv_nomg
9921
9922 These create a new SV which is an exact duplicate of the original SV
9923 (using C<sv_setsv>.)
9924
9925 They differ only in that C<newSVsv> performs 'get' magic; C<newSVsv_nomg> skips
9926 any magic; and C<newSVsv_flags> allows you to explicitly set a C<flags>
9927 parameter.
9928
9929 =cut
9930 */
9931
9932 SV *
9933 Perl_newSVsv_flags(pTHX_ SV *const old, I32 flags)
9934 {
9935     SV *sv;
9936
9937     if (!old)
9938         return NULL;
9939     if (SvTYPE(old) == (svtype)SVTYPEMASK) {
9940         Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
9941         return NULL;
9942     }
9943     /* Do this here, otherwise we leak the new SV if this croaks. */
9944     if (flags & SV_GMAGIC)
9945         SvGETMAGIC(old);
9946     new_SV(sv);
9947     sv_setsv_flags(sv, old, flags & ~SV_GMAGIC);
9948     return sv;
9949 }
9950
9951 /*
9952 =for apidoc sv_reset
9953
9954 Underlying implementation for the C<reset> Perl function.
9955 Note that the perl-level function is vaguely deprecated.
9956
9957 =cut
9958 */
9959
9960 void
9961 Perl_sv_reset(pTHX_ const char *s, HV *const stash)
9962 {
9963     PERL_ARGS_ASSERT_SV_RESET;
9964
9965     sv_resetpvn(*s ? s : NULL, strlen(s), stash);
9966 }
9967
9968 void
9969 Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
9970 {
9971     char todo[PERL_UCHAR_MAX+1];
9972     const char *send;
9973
9974     if (!stash || SvTYPE(stash) != SVt_PVHV)
9975         return;
9976
9977     if (!s) {           /* reset ?? searches */
9978         MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
9979         if (mg && mg->mg_len) {
9980             const U32 count = mg->mg_len / sizeof(PMOP**);
9981             PMOP **pmp = (PMOP**) mg->mg_ptr;
9982             PMOP *const *const end = pmp + count;
9983
9984             while (pmp < end) {
9985 #ifdef USE_ITHREADS
9986                 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
9987 #else
9988                 (*pmp)->op_pmflags &= ~PMf_USED;
9989 #endif
9990                 ++pmp;
9991             }
9992         }
9993         return;
9994     }
9995
9996     /* reset variables */
9997
9998     if (!HvTOTALKEYS(stash))
9999         return;
10000
10001     Zero(todo, 256, char);
10002     send = s + len;
10003     while (s < send) {
10004         I32 max;
10005         I32 i = (unsigned char)*s;
10006         if (s[1] == '-') {
10007             s += 2;
10008         }
10009         max = (unsigned char)*s++;
10010         for ( ; i <= max; i++) {
10011             todo[i] = 1;
10012         }
10013         for (i = 0; i <= (I32) HvMAX(stash); i++) {
10014             HE *entry;
10015             for (entry = HvARRAY(stash)[i];
10016                  entry;
10017                  entry = HeNEXT(entry))
10018             {
10019                 GV *gv;
10020                 SV *sv;
10021
10022                 if (!todo[(U8)*HeKEY(entry)])
10023                     continue;
10024                 gv = MUTABLE_GV(HeVAL(entry));
10025                 if (!isGV(gv))
10026                     continue;
10027                 sv = GvSV(gv);
10028                 if (sv && !SvREADONLY(sv)) {
10029                     SV_CHECK_THINKFIRST_COW_DROP(sv);
10030                     if (!isGV(sv)) SvOK_off(sv);
10031                 }
10032                 if (GvAV(gv)) {
10033                     av_clear(GvAV(gv));
10034                 }
10035                 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
10036                     hv_clear(GvHV(gv));
10037                 }
10038             }
10039         }
10040     }
10041 }
10042
10043 /*
10044 =for apidoc sv_2io
10045
10046 Using various gambits, try to get an IO from an SV: the IO slot if its a
10047 GV; or the recursive result if we're an RV; or the IO slot of the symbol
10048 named after the PV if we're a string.
10049
10050 'Get' magic is ignored on the C<sv> passed in, but will be called on
10051 C<SvRV(sv)> if C<sv> is an RV.
10052
10053 =cut
10054 */
10055
10056 IO*
10057 Perl_sv_2io(pTHX_ SV *const sv)
10058 {
10059     IO* io;
10060     GV* gv;
10061
10062     PERL_ARGS_ASSERT_SV_2IO;
10063
10064     switch (SvTYPE(sv)) {
10065     case SVt_PVIO:
10066         io = MUTABLE_IO(sv);
10067         break;
10068     case SVt_PVGV:
10069     case SVt_PVLV:
10070         if (isGV_with_GP(sv)) {
10071             gv = MUTABLE_GV(sv);
10072             io = GvIO(gv);
10073             if (!io)
10074                 Perl_croak(aTHX_ "Bad filehandle: %" HEKf,
10075                                     HEKfARG(GvNAME_HEK(gv)));
10076             break;
10077         }
10078         /* FALLTHROUGH */
10079     default:
10080         if (!SvOK(sv))
10081             Perl_croak(aTHX_ PL_no_usym, "filehandle");
10082         if (SvROK(sv)) {
10083             SvGETMAGIC(SvRV(sv));
10084             return sv_2io(SvRV(sv));
10085         }
10086         gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
10087         if (gv)
10088             io = GvIO(gv);
10089         else
10090             io = 0;
10091         if (!io) {
10092             SV *newsv = sv;
10093             if (SvGMAGICAL(sv)) {
10094                 newsv = sv_newmortal();
10095                 sv_setsv_nomg(newsv, sv);
10096             }
10097             Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv));
10098         }
10099         break;
10100     }
10101     return io;
10102 }
10103
10104 /*
10105 =for apidoc sv_2cv
10106
10107 Using various gambits, try to get a CV from an SV; in addition, try if
10108 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
10109 The flags in C<lref> are passed to C<gv_fetchsv>.
10110
10111 =cut
10112 */
10113
10114 CV *
10115 Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
10116 {
10117     GV *gv = NULL;
10118     CV *cv = NULL;
10119
10120     PERL_ARGS_ASSERT_SV_2CV;
10121
10122     if (!sv) {
10123         *st = NULL;
10124         *gvp = NULL;
10125         return NULL;
10126     }
10127     switch (SvTYPE(sv)) {
10128     case SVt_PVCV:
10129         *st = CvSTASH(sv);
10130         *gvp = NULL;
10131         return MUTABLE_CV(sv);
10132     case SVt_PVHV:
10133     case SVt_PVAV:
10134         *st = NULL;
10135         *gvp = NULL;
10136         return NULL;
10137     default:
10138         SvGETMAGIC(sv);
10139         if (SvROK(sv)) {
10140             if (SvAMAGIC(sv))
10141                 sv = amagic_deref_call(sv, to_cv_amg);
10142
10143             sv = SvRV(sv);
10144             if (SvTYPE(sv) == SVt_PVCV) {
10145                 cv = MUTABLE_CV(sv);
10146                 *gvp = NULL;
10147                 *st = CvSTASH(cv);
10148                 return cv;
10149             }
10150             else if(SvGETMAGIC(sv), isGV_with_GP(sv))
10151                 gv = MUTABLE_GV(sv);
10152             else
10153                 Perl_croak(aTHX_ "Not a subroutine reference");
10154         }
10155         else if (isGV_with_GP(sv)) {
10156             gv = MUTABLE_GV(sv);
10157         }
10158         else {
10159             gv = gv_fetchsv_nomg(sv, lref, SVt_PVCV);
10160         }
10161         *gvp = gv;
10162         if (!gv) {
10163             *st = NULL;
10164             return NULL;
10165         }
10166         /* Some flags to gv_fetchsv mean don't really create the GV  */
10167         if (!isGV_with_GP(gv)) {
10168             *st = NULL;
10169             return NULL;
10170         }
10171         *st = GvESTASH(gv);
10172         if (lref & ~GV_ADDMG && !GvCVu(gv)) {
10173             /* XXX this is probably not what they think they're getting.
10174              * It has the same effect as "sub name;", i.e. just a forward
10175              * declaration! */
10176             newSTUB(gv,0);
10177         }
10178         return GvCVu(gv);
10179     }
10180 }
10181
10182 /*
10183 =for apidoc sv_true
10184
10185 Returns true if the SV has a true value by Perl's rules.
10186 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
10187 instead use an in-line version.
10188
10189 =cut
10190 */
10191
10192 I32
10193 Perl_sv_true(pTHX_ SV *const sv)
10194 {
10195     if (!sv)
10196         return 0;
10197     if (SvPOK(sv)) {
10198         const XPV* const tXpv = (XPV*)SvANY(sv);
10199         if (tXpv &&
10200                 (tXpv->xpv_cur > 1 ||
10201                 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
10202             return 1;
10203         else
10204             return 0;
10205     }
10206     else {
10207         if (SvIOK(sv))
10208             return SvIVX(sv) != 0;
10209         else {
10210             if (SvNOK(sv))
10211                 return SvNVX(sv) != 0.0;
10212             else
10213                 return sv_2bool(sv);
10214         }
10215     }
10216 }
10217
10218 /*
10219 =for apidoc sv_pvn_force
10220
10221 Get a sensible string out of the SV somehow.
10222 A private implementation of the C<SvPV_force> macro for compilers which
10223 can't cope with complex macro expressions.  Always use the macro instead.
10224
10225 =for apidoc sv_pvn_force_flags
10226
10227 Get a sensible string out of the SV somehow.
10228 If C<flags> has the C<SV_GMAGIC> bit set, will C<L</mg_get>> on C<sv> if
10229 appropriate, else not.  C<sv_pvn_force> and C<sv_pvn_force_nomg> are
10230 implemented in terms of this function.
10231 You normally want to use the various wrapper macros instead: see
10232 C<L</SvPV_force>> and C<L</SvPV_force_nomg>>.
10233
10234 =cut
10235 */
10236
10237 char *
10238 Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
10239 {
10240     PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
10241
10242     if (flags & SV_GMAGIC) SvGETMAGIC(sv);
10243     if (SvTHINKFIRST(sv) && (!SvROK(sv) || SvREADONLY(sv)))
10244         sv_force_normal_flags(sv, 0);
10245
10246     if (SvPOK(sv)) {
10247         if (lp)
10248             *lp = SvCUR(sv);
10249     }
10250     else {
10251         char *s;
10252         STRLEN len;
10253
10254         if (SvTYPE(sv) > SVt_PVLV
10255             || isGV_with_GP(sv))
10256             /* diag_listed_as: Can't coerce %s to %s in %s */
10257             Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
10258                 OP_DESC(PL_op));
10259         s = sv_2pv_flags(sv, &len, flags &~ SV_GMAGIC);
10260         if (!s) {
10261           s = (char *)"";
10262         }
10263         if (lp)
10264             *lp = len;
10265
10266         if (SvTYPE(sv) < SVt_PV ||
10267             s != SvPVX_const(sv)) {     /* Almost, but not quite, sv_setpvn() */
10268             if (SvROK(sv))
10269                 sv_unref(sv);
10270             SvUPGRADE(sv, SVt_PV);              /* Never FALSE */
10271             SvGROW(sv, len + 1);
10272             Move(s,SvPVX(sv),len,char);
10273             SvCUR_set(sv, len);
10274             SvPVX(sv)[len] = '\0';
10275         }
10276         if (!SvPOK(sv)) {
10277             SvPOK_on(sv);               /* validate pointer */
10278             SvTAINT(sv);
10279             DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
10280                                   PTR2UV(sv),SvPVX_const(sv)));
10281         }
10282     }
10283     (void)SvPOK_only_UTF8(sv);
10284     return SvPVX_mutable(sv);
10285 }
10286
10287 /*
10288 =for apidoc sv_pvbyten_force
10289
10290 The backend for the C<SvPVbytex_force> macro.  Always use the macro
10291 instead.  If the SV cannot be downgraded from UTF-8, this croaks.
10292
10293 =cut
10294 */
10295
10296 char *
10297 Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
10298 {
10299     PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
10300
10301     sv_pvn_force(sv,lp);
10302     (void)sv_utf8_downgrade(sv,0);
10303     *lp = SvCUR(sv);
10304     return SvPVX(sv);
10305 }
10306
10307 /*
10308 =for apidoc sv_pvutf8n_force
10309
10310 The backend for the C<SvPVutf8x_force> macro.  Always use the macro
10311 instead.
10312
10313 =cut
10314 */
10315
10316 char *
10317 Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
10318 {
10319     PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
10320
10321     sv_pvn_force(sv,0);
10322     sv_utf8_upgrade_nomg(sv);
10323     *lp = SvCUR(sv);
10324     return SvPVX(sv);
10325 }
10326
10327 /*
10328 =for apidoc sv_reftype
10329
10330 Returns a string describing what the SV is a reference to.
10331
10332 If ob is true and the SV is blessed, the string is the class name,
10333 otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10334
10335 =cut
10336 */
10337
10338 const char *
10339 Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
10340 {
10341     PERL_ARGS_ASSERT_SV_REFTYPE;
10342     if (ob && SvOBJECT(sv)) {
10343         return SvPV_nolen_const(sv_ref(NULL, sv, ob));
10344     }
10345     else {
10346         /* WARNING - There is code, for instance in mg.c, that assumes that
10347          * the only reason that sv_reftype(sv,0) would return a string starting
10348          * with 'L' or 'S' is that it is a LVALUE or a SCALAR.
10349          * Yes this a dodgy way to do type checking, but it saves practically reimplementing
10350          * this routine inside other subs, and it saves time.
10351          * Do not change this assumption without searching for "dodgy type check" in
10352          * the code.
10353          * - Yves */
10354         switch (SvTYPE(sv)) {
10355         case SVt_NULL:
10356         case SVt_IV:
10357         case SVt_NV:
10358         case SVt_PV:
10359         case SVt_PVIV:
10360         case SVt_PVNV:
10361         case SVt_PVMG:
10362                                 if (SvVOK(sv))
10363                                     return "VSTRING";
10364                                 if (SvROK(sv))
10365                                     return "REF";
10366                                 else
10367                                     return "SCALAR";
10368
10369         case SVt_PVLV:          return (char *)  (SvROK(sv) ? "REF"
10370                                 /* tied lvalues should appear to be
10371                                  * scalars for backwards compatibility */
10372                                 : (isALPHA_FOLD_EQ(LvTYPE(sv), 't'))
10373                                     ? "SCALAR" : "LVALUE");
10374         case SVt_PVAV:          return "ARRAY";
10375         case SVt_PVHV:          return "HASH";
10376         case SVt_PVCV:          return "CODE";
10377         case SVt_PVGV:          return (char *) (isGV_with_GP(sv)
10378                                     ? "GLOB" : "SCALAR");
10379         case SVt_PVFM:          return "FORMAT";
10380         case SVt_PVIO:          return "IO";
10381         case SVt_INVLIST:       return "INVLIST";
10382         case SVt_REGEXP:        return "REGEXP";
10383         default:                return "UNKNOWN";
10384         }
10385     }
10386 }
10387
10388 /*
10389 =for apidoc sv_ref
10390
10391 Returns a SV describing what the SV passed in is a reference to.
10392
10393 dst can be a SV to be set to the description or NULL, in which case a
10394 mortal SV is returned.
10395
10396 If ob is true and the SV is blessed, the description is the class
10397 name, otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10398
10399 =cut
10400 */
10401
10402 SV *
10403 Perl_sv_ref(pTHX_ SV *dst, const SV *const sv, const int ob)
10404 {
10405     PERL_ARGS_ASSERT_SV_REF;
10406
10407     if (!dst)
10408         dst = sv_newmortal();
10409
10410     if (ob && SvOBJECT(sv)) {
10411         HvNAME_get(SvSTASH(sv))
10412                     ? sv_sethek(dst, HvNAME_HEK(SvSTASH(sv)))
10413                     : sv_setpvs(dst, "__ANON__");
10414     }
10415     else {
10416         const char * reftype = sv_reftype(sv, 0);
10417         sv_setpv(dst, reftype);
10418     }
10419     return dst;
10420 }
10421
10422 /*
10423 =for apidoc sv_isobject
10424
10425 Returns a boolean indicating whether the SV is an RV pointing to a blessed
10426 object.  If the SV is not an RV, or if the object is not blessed, then this
10427 will return false.
10428
10429 =cut
10430 */
10431
10432 int
10433 Perl_sv_isobject(pTHX_ SV *sv)
10434 {
10435     if (!sv)
10436         return 0;
10437     SvGETMAGIC(sv);
10438     if (!SvROK(sv))
10439         return 0;
10440     sv = SvRV(sv);
10441     if (!SvOBJECT(sv))
10442         return 0;
10443     return 1;
10444 }
10445
10446 /*
10447 =for apidoc sv_isa
10448
10449 Returns a boolean indicating whether the SV is blessed into the specified
10450 class.
10451
10452 This does not check for subtypes or method overloading. Use C<sv_isa_sv> to
10453 verify an inheritance relationship in the same way as the C<isa> operator by
10454 respecting any C<isa()> method overloading; or C<sv_derived_from_sv> to test
10455 directly on the actual object type.
10456
10457 =cut
10458 */
10459
10460 int
10461 Perl_sv_isa(pTHX_ SV *sv, const char *const name)
10462 {
10463     const char *hvname;
10464
10465     PERL_ARGS_ASSERT_SV_ISA;
10466
10467     if (!sv)
10468         return 0;
10469     SvGETMAGIC(sv);
10470     if (!SvROK(sv))
10471         return 0;
10472     sv = SvRV(sv);
10473     if (!SvOBJECT(sv))
10474         return 0;
10475     hvname = HvNAME_get(SvSTASH(sv));
10476     if (!hvname)
10477         return 0;
10478
10479     return strEQ(hvname, name);
10480 }
10481
10482 /*
10483 =for apidoc newSVrv
10484
10485 Creates a new SV for the existing RV, C<rv>, to point to.  If C<rv> is not an
10486 RV then it will be upgraded to one.  If C<classname> is non-null then the new
10487 SV will be blessed in the specified package.  The new SV is returned and its
10488 reference count is 1.  The reference count 1 is owned by C<rv>. See also
10489 newRV_inc() and newRV_noinc() for creating a new RV properly.
10490
10491 =cut
10492 */
10493
10494 SV*
10495 Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
10496 {
10497     SV *sv;
10498
10499     PERL_ARGS_ASSERT_NEWSVRV;
10500
10501     new_SV(sv);
10502
10503     SV_CHECK_THINKFIRST_COW_DROP(rv);
10504
10505     if (UNLIKELY( SvTYPE(rv) >= SVt_PVMG )) {
10506         const U32 refcnt = SvREFCNT(rv);
10507         SvREFCNT(rv) = 0;
10508         sv_clear(rv);
10509         SvFLAGS(rv) = 0;
10510         SvREFCNT(rv) = refcnt;
10511
10512         sv_upgrade(rv, SVt_IV);
10513     } else if (SvROK(rv)) {
10514         SvREFCNT_dec(SvRV(rv));
10515     } else {
10516         prepare_SV_for_RV(rv);
10517     }
10518
10519     SvOK_off(rv);
10520     SvRV_set(rv, sv);
10521     SvROK_on(rv);
10522
10523     if (classname) {
10524         HV* const stash = gv_stashpv(classname, GV_ADD);
10525         (void)sv_bless(rv, stash);
10526     }
10527     return sv;
10528 }
10529
10530 SV *
10531 Perl_newSVavdefelem(pTHX_ AV *av, SSize_t ix, bool extendible)
10532 {
10533     SV * const lv = newSV_type(SVt_PVLV);
10534     PERL_ARGS_ASSERT_NEWSVAVDEFELEM;
10535     LvTYPE(lv) = 'y';
10536     sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
10537     LvTARG(lv) = SvREFCNT_inc_simple_NN(av);
10538     LvSTARGOFF(lv) = ix;
10539     LvTARGLEN(lv) = extendible ? 1 : (STRLEN)UV_MAX;
10540     return lv;
10541 }
10542
10543 /*
10544 =for apidoc sv_setref_pv
10545
10546 Copies a pointer into a new SV, optionally blessing the SV.  The C<rv>
10547 argument will be upgraded to an RV.  That RV will be modified to point to
10548 the new SV.  If the C<pv> argument is C<NULL>, then C<PL_sv_undef> will be placed
10549 into the SV.  The C<classname> argument indicates the package for the
10550 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10551 will have a reference count of 1, and the RV will be returned.
10552
10553 Do not use with other Perl types such as HV, AV, SV, CV, because those
10554 objects will become corrupted by the pointer copy process.
10555
10556 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
10557
10558 =cut
10559 */
10560
10561 SV*
10562 Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
10563 {
10564     PERL_ARGS_ASSERT_SV_SETREF_PV;
10565
10566     if (!pv) {
10567         sv_set_undef(rv);
10568         SvSETMAGIC(rv);
10569     }
10570     else
10571         sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
10572     return rv;
10573 }
10574
10575 /*
10576 =for apidoc sv_setref_iv
10577
10578 Copies an integer into a new SV, optionally blessing the SV.  The C<rv>
10579 argument will be upgraded to an RV.  That RV will be modified to point to
10580 the new SV.  The C<classname> argument indicates the package for the
10581 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10582 will have a reference count of 1, and the RV will be returned.
10583
10584 =cut
10585 */
10586
10587 SV*
10588 Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
10589 {
10590     PERL_ARGS_ASSERT_SV_SETREF_IV;
10591
10592     sv_setiv(newSVrv(rv,classname), iv);
10593     return rv;
10594 }
10595
10596 /*
10597 =for apidoc sv_setref_uv
10598
10599 Copies an unsigned integer into a new SV, optionally blessing the SV.  The C<rv>
10600 argument will be upgraded to an RV.  That RV will be modified to point to
10601 the new SV.  The C<classname> argument indicates the package for the
10602 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10603 will have a reference count of 1, and the RV will be returned.
10604
10605 =cut
10606 */
10607
10608 SV*
10609 Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
10610 {
10611     PERL_ARGS_ASSERT_SV_SETREF_UV;
10612
10613     sv_setuv(newSVrv(rv,classname), uv);
10614     return rv;
10615 }
10616
10617 /*
10618 =for apidoc sv_setref_nv
10619
10620 Copies a double into a new SV, optionally blessing the SV.  The C<rv>
10621 argument will be upgraded to an RV.  That RV will be modified to point to
10622 the new SV.  The C<classname> argument indicates the package for the
10623 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10624 will have a reference count of 1, and the RV will be returned.
10625
10626 =cut
10627 */
10628
10629 SV*
10630 Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
10631 {
10632     PERL_ARGS_ASSERT_SV_SETREF_NV;
10633
10634     sv_setnv(newSVrv(rv,classname), nv);
10635     return rv;
10636 }
10637
10638 /*
10639 =for apidoc sv_setref_pvn
10640
10641 Copies a string into a new SV, optionally blessing the SV.  The length of the
10642 string must be specified with C<n>.  The C<rv> argument will be upgraded to
10643 an RV.  That RV will be modified to point to the new SV.  The C<classname>
10644 argument indicates the package for the blessing.  Set C<classname> to
10645 C<NULL> to avoid the blessing.  The new SV will have a reference count
10646 of 1, and the RV will be returned.
10647
10648 Note that C<sv_setref_pv> copies the pointer while this copies the string.
10649
10650 =cut
10651 */
10652
10653 SV*
10654 Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
10655                    const char *const pv, const STRLEN n)
10656 {
10657     PERL_ARGS_ASSERT_SV_SETREF_PVN;
10658
10659     sv_setpvn(newSVrv(rv,classname), pv, n);
10660     return rv;
10661 }
10662
10663 /*
10664 =for apidoc sv_bless
10665
10666 Blesses an SV into a specified package.  The SV must be an RV.  The package
10667 must be designated by its stash (see C<L</gv_stashpv>>).  The reference count
10668 of the SV is unaffected.
10669
10670 =cut
10671 */
10672
10673 SV*
10674 Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
10675 {
10676     SV *tmpRef;
10677     HV *oldstash = NULL;
10678
10679     PERL_ARGS_ASSERT_SV_BLESS;
10680
10681     SvGETMAGIC(sv);
10682     if (!SvROK(sv))
10683         Perl_croak(aTHX_ "Can't bless non-reference value");
10684     tmpRef = SvRV(sv);
10685     if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY|SVf_PROTECT)) {
10686         if (SvREADONLY(tmpRef))
10687             Perl_croak_no_modify();
10688         if (SvOBJECT(tmpRef)) {
10689             oldstash = SvSTASH(tmpRef);
10690         }
10691     }
10692     SvOBJECT_on(tmpRef);
10693     SvUPGRADE(tmpRef, SVt_PVMG);
10694     SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
10695     SvREFCNT_dec(oldstash);
10696
10697     if(SvSMAGICAL(tmpRef))
10698         if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
10699             mg_set(tmpRef);
10700
10701
10702
10703     return sv;
10704 }
10705
10706 /* Downgrades a PVGV to a PVMG. If it's actually a PVLV, we leave the type
10707  * as it is after unglobbing it.
10708  */
10709
10710 PERL_STATIC_INLINE void
10711 S_sv_unglob(pTHX_ SV *const sv, U32 flags)
10712 {
10713     void *xpvmg;
10714     HV *stash;
10715     SV * const temp = flags & SV_COW_DROP_PV ? NULL : sv_newmortal();
10716
10717     PERL_ARGS_ASSERT_SV_UNGLOB;
10718
10719     assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV);
10720     SvFAKE_off(sv);
10721     if (!(flags & SV_COW_DROP_PV))
10722         gv_efullname3(temp, MUTABLE_GV(sv), "*");
10723
10724     SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
10725     if (GvGP(sv)) {
10726         if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
10727            && HvNAME_get(stash))
10728             mro_method_changed_in(stash);
10729         gp_free(MUTABLE_GV(sv));
10730     }
10731     if (GvSTASH(sv)) {
10732         sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
10733         GvSTASH(sv) = NULL;
10734     }
10735     GvMULTI_off(sv);
10736     if (GvNAME_HEK(sv)) {
10737         unshare_hek(GvNAME_HEK(sv));
10738     }
10739     isGV_with_GP_off(sv);
10740
10741     if(SvTYPE(sv) == SVt_PVGV) {
10742         /* need to keep SvANY(sv) in the right arena */
10743         xpvmg = new_XPVMG();
10744         StructCopy(SvANY(sv), xpvmg, XPVMG);
10745         del_body_by_type(SvANY(sv), SVt_PVGV);
10746         SvANY(sv) = xpvmg;
10747
10748         SvFLAGS(sv) &= ~SVTYPEMASK;
10749         SvFLAGS(sv) |= SVt_PVMG;
10750     }
10751
10752     /* Intentionally not calling any local SET magic, as this isn't so much a
10753        set operation as merely an internal storage change.  */
10754     if (flags & SV_COW_DROP_PV) SvOK_off(sv);
10755     else sv_setsv_flags(sv, temp, 0);
10756
10757     if ((const GV *)sv == PL_last_in_gv)
10758         PL_last_in_gv = NULL;
10759     else if ((const GV *)sv == PL_statgv)
10760         PL_statgv = NULL;
10761 }
10762
10763 /*
10764 =for apidoc sv_unref_flags
10765
10766 Unsets the RV status of the SV, and decrements the reference count of
10767 whatever was being referenced by the RV.  This can almost be thought of
10768 as a reversal of C<newSVrv>.  The C<cflags> argument can contain
10769 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
10770 (otherwise the decrementing is conditional on the reference count being
10771 different from one or the reference being a readonly SV).
10772 See C<L</SvROK_off>>.
10773
10774 =for apidoc Amnh||SV_IMMEDIATE_UNREF
10775
10776 =cut
10777 */
10778
10779 void
10780 Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
10781 {
10782     SV* const target = SvRV(ref);
10783
10784     PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
10785
10786     if (SvWEAKREF(ref)) {
10787         sv_del_backref(target, ref);
10788         SvWEAKREF_off(ref);
10789         SvRV_set(ref, NULL);
10790         return;
10791     }
10792     SvRV_set(ref, NULL);
10793     SvROK_off(ref);
10794     /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
10795        assigned to as BEGIN {$a = \"Foo"} will fail.  */
10796     if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
10797         SvREFCNT_dec_NN(target);
10798     else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
10799         sv_2mortal(target);     /* Schedule for freeing later */
10800 }
10801
10802 /*
10803 =for apidoc sv_untaint
10804
10805 Untaint an SV.  Use C<SvTAINTED_off> instead.
10806
10807 =cut
10808 */
10809
10810 void
10811 Perl_sv_untaint(pTHX_ SV *const sv)
10812 {
10813     PERL_ARGS_ASSERT_SV_UNTAINT;
10814     PERL_UNUSED_CONTEXT;
10815
10816     if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10817         MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10818         if (mg)
10819             mg->mg_len &= ~1;
10820     }
10821 }
10822
10823 /*
10824 =for apidoc sv_tainted
10825
10826 Test an SV for taintedness.  Use C<SvTAINTED> instead.
10827
10828 =cut
10829 */
10830
10831 bool
10832 Perl_sv_tainted(pTHX_ SV *const sv)
10833 {
10834     PERL_ARGS_ASSERT_SV_TAINTED;
10835     PERL_UNUSED_CONTEXT;
10836
10837     if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10838         const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10839         if (mg && (mg->mg_len & 1) )
10840             return TRUE;
10841     }
10842     return FALSE;
10843 }
10844
10845 #if defined(MULTIPLICITY)
10846
10847 /* pTHX_ magic can't cope with varargs, so this is a no-context
10848  * version of the main function, (which may itself be aliased to us).
10849  * Don't access this version directly.
10850  */
10851
10852 void
10853 Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
10854 {
10855     dTHX;
10856     va_list args;
10857
10858     PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
10859
10860     va_start(args, pat);
10861     sv_vsetpvf(sv, pat, &args);
10862     va_end(args);
10863 }
10864
10865 /* pTHX_ magic can't cope with varargs, so this is a no-context
10866  * version of the main function, (which may itself be aliased to us).
10867  * Don't access this version directly.
10868  */
10869
10870 void
10871 Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10872 {
10873     dTHX;
10874     va_list args;
10875
10876     PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
10877
10878     va_start(args, pat);
10879     sv_vsetpvf_mg(sv, pat, &args);
10880     va_end(args);
10881 }
10882 #endif
10883
10884 /*
10885 =for apidoc      sv_setpvf
10886 =for apidoc_item sv_setpvf_mg
10887 =for apidoc_item sv_setpvf_mg_nocontext
10888 =for apidoc_item sv_setpvf_nocontext
10889
10890 These work like C<L</sv_catpvf>> but copy the text into the SV instead of
10891 appending it.
10892
10893 The differences between these are:
10894
10895 C<sv_setpvf_mg> and C<sv_setpvf_mg_nocontext> perform 'set' magic; C<sv_setpvf>
10896 and C<sv_setpvf_nocontext> skip all magic.
10897
10898 C<sv_setpvf_nocontext> and C<sv_setpvf_mg_nocontext> do not take a thread
10899 context (C<aTHX>) parameter, so are used in situations where the caller
10900 doesn't already have the thread context.
10901
10902 =cut
10903 */
10904
10905 void
10906 Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
10907 {
10908     va_list args;
10909
10910     PERL_ARGS_ASSERT_SV_SETPVF;
10911
10912     va_start(args, pat);
10913     sv_vsetpvf(sv, pat, &args);
10914     va_end(args);
10915 }
10916
10917 /*
10918 =for apidoc sv_vsetpvf
10919 =for apidoc_item sv_vsetpvf_mg
10920
10921 These work like C<L</sv_vcatpvf>> but copy the text into the SV instead of
10922 appending it.
10923
10924 They differ only in that C<sv_vsetpvf_mg> performs 'set' magic;
10925 C<sv_vsetpvf> skips all magic.
10926
10927 They are usually used via their frontends, C<L</sv_setpvf>> and
10928 C<L</sv_setpvf_mg>>.
10929
10930 =cut
10931 */
10932
10933 void
10934 Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10935 {
10936     PERL_ARGS_ASSERT_SV_VSETPVF;
10937
10938     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10939 }
10940
10941 void
10942 Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
10943 {
10944     va_list args;
10945
10946     PERL_ARGS_ASSERT_SV_SETPVF_MG;
10947
10948     va_start(args, pat);
10949     sv_vsetpvf_mg(sv, pat, &args);
10950     va_end(args);
10951 }
10952
10953 void
10954 Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10955 {
10956     PERL_ARGS_ASSERT_SV_VSETPVF_MG;
10957
10958     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10959     SvSETMAGIC(sv);
10960 }
10961
10962 #if defined(MULTIPLICITY)
10963
10964 /* pTHX_ magic can't cope with varargs, so this is a no-context
10965  * version of the main function, (which may itself be aliased to us).
10966  * Don't access this version directly.
10967  */
10968
10969 void
10970 Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
10971 {
10972     dTHX;
10973     va_list args;
10974
10975     PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
10976
10977     va_start(args, pat);
10978     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10979     va_end(args);
10980 }
10981
10982 /* pTHX_ magic can't cope with varargs, so this is a no-context
10983  * version of the main function, (which may itself be aliased to us).
10984  * Don't access this version directly.
10985  */
10986
10987 void
10988 Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10989 {
10990     dTHX;
10991     va_list args;
10992
10993     PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
10994
10995     va_start(args, pat);
10996     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10997     SvSETMAGIC(sv);
10998     va_end(args);
10999 }
11000 #endif
11001
11002 /*
11003 =for apidoc sv_catpvf
11004 =for apidoc_item sv_catpvf_mg
11005 =for apidoc_item sv_catpvf_mg_nocontext
11006 =for apidoc_item sv_catpvf_nocontext
11007
11008 These process their arguments like C<sprintf>, and append the formatted
11009 output to an SV.  As with C<sv_vcatpvfn>, argument reordering is not supporte
11010 when called with a non-null C-style variable argument list.
11011
11012 If the appended data contains "wide" characters
11013 (including, but not limited to, SVs with a UTF-8 PV formatted with C<%s>,
11014 and characters >255 formatted with C<%c>), the original SV might get
11015 upgraded to UTF-8.
11016
11017 If the original SV was UTF-8, the pattern should be
11018 valid UTF-8; if the original SV was bytes, the pattern should be too.
11019
11020 All perform 'get' magic, but only C<sv_catpvf_mg> and C<sv_catpvf_mg_nocontext>
11021 perform 'set' magic.
11022
11023 C<sv_catpvf_nocontext> and C<sv_catpvf_mg_nocontext> do not take a thread
11024 context (C<aTHX>) parameter, so are used in situations where the caller
11025 doesn't already have the thread context.
11026
11027 =cut
11028 */
11029
11030 void
11031 Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
11032 {
11033     va_list args;
11034
11035     PERL_ARGS_ASSERT_SV_CATPVF;
11036
11037     va_start(args, pat);
11038     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11039     va_end(args);
11040 }
11041
11042 /*
11043 =for apidoc sv_vcatpvf
11044 =for apidoc_item sv_vcatpvf_mg
11045
11046 These process their arguments like C<sv_vcatpvfn> called with a non-null
11047 C-style variable argument list, and append the formatted output to C<sv>.
11048
11049 They differ only in that C<sv_vcatpvf_mg> performs 'set' magic;
11050 C<sv_vcatpvf> skips 'set' magic.
11051
11052 Both perform 'get' magic.
11053
11054 They are usually accessed via their frontends C<L</sv_catpvf>> and
11055 C<L</sv_catpvf_mg>>.
11056
11057 =cut
11058 */
11059
11060 void
11061 Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11062 {
11063     PERL_ARGS_ASSERT_SV_VCATPVF;
11064
11065     sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11066 }
11067
11068 void
11069 Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11070 {
11071     va_list args;
11072
11073     PERL_ARGS_ASSERT_SV_CATPVF_MG;
11074
11075     va_start(args, pat);
11076     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11077     SvSETMAGIC(sv);
11078     va_end(args);
11079 }
11080
11081 void
11082 Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11083 {
11084     PERL_ARGS_ASSERT_SV_VCATPVF_MG;
11085
11086     sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11087     SvSETMAGIC(sv);
11088 }
11089
11090 /*
11091 =for apidoc sv_vsetpvfn
11092
11093 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
11094 appending it.
11095
11096 Usually used via one of its frontends L</C<sv_vsetpvf>> and
11097 L</C<sv_vsetpvf_mg>>.
11098
11099 =cut
11100 */
11101
11102 void
11103 Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11104                  va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11105 {
11106     PERL_ARGS_ASSERT_SV_VSETPVFN;
11107
11108     SvPVCLEAR(sv);
11109     sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, 0);
11110 }
11111
11112
11113 /* simplified inline Perl_sv_catpvn_nomg() when you know the SV's SvPOK */
11114
11115 PERL_STATIC_INLINE void
11116 S_sv_catpvn_simple(pTHX_ SV *const sv, const char* const buf, const STRLEN len)
11117 {
11118     STRLEN const need = len + SvCUR(sv) + 1;
11119     char *end;
11120
11121     /* can't wrap as both len and SvCUR() are allocated in
11122      * memory and together can't consume all the address space
11123      */
11124     assert(need > len);
11125
11126     assert(SvPOK(sv));
11127     SvGROW(sv, need);
11128     end = SvEND(sv);
11129     Copy(buf, end, len, char);
11130     end += len;
11131     *end = '\0';
11132     SvCUR_set(sv, need - 1);
11133 }
11134
11135
11136 /*
11137  * Warn of missing argument to sprintf. The value used in place of such
11138  * arguments should be &PL_sv_no; an undefined value would yield
11139  * inappropriate "use of uninit" warnings [perl #71000].
11140  */
11141 STATIC void
11142 S_warn_vcatpvfn_missing_argument(pTHX) {
11143     if (ckWARN(WARN_MISSING)) {
11144         Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s",
11145                 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11146     }
11147 }
11148
11149
11150 static void
11151 S_croak_overflow()
11152 {
11153     dTHX;
11154     Perl_croak(aTHX_ "Integer overflow in format string for %s",
11155                     (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn"));
11156 }
11157
11158
11159 /* Given an int i from the next arg (if args is true) or an sv from an arg
11160  * (if args is false), try to extract a STRLEN-ranged value from the arg,
11161  * with overflow checking.
11162  * Sets *neg to true if the value was negative (untouched otherwise.
11163  * Returns the absolute value.
11164  * As an extra margin of safety, it croaks if the returned value would
11165  * exceed the maximum value of a STRLEN / 4.
11166  */
11167
11168 static STRLEN
11169 S_sprintf_arg_num_val(pTHX_ va_list *const args, int i, SV *sv, bool *neg)
11170 {
11171     IV iv;
11172
11173     if (args) {
11174         iv = i;
11175         goto do_iv;
11176     }
11177
11178     if (!sv)
11179         return 0;
11180
11181     SvGETMAGIC(sv);
11182
11183     if (UNLIKELY(SvIsUV(sv))) {
11184         UV uv = SvUV_nomg(sv);
11185         if (uv > IV_MAX)
11186             S_croak_overflow();
11187         iv = uv;
11188     }
11189     else {
11190         iv = SvIV_nomg(sv);
11191       do_iv:
11192         if (iv < 0) {
11193             if (iv < -IV_MAX)
11194                 S_croak_overflow();
11195             iv = -iv;
11196             *neg = TRUE;
11197         }
11198     }
11199
11200     if (iv > (IV)(((STRLEN)~0) / 4))
11201         S_croak_overflow();
11202
11203     return (STRLEN)iv;
11204 }
11205
11206 /* Read in and return a number. Updates *pattern to point to the char
11207  * following the number. Expects the first char to 1..9.
11208  * Croaks if the number exceeds 1/4 of the maximum value of STRLEN.
11209  * This is a belt-and-braces safety measure to complement any
11210  * overflow/wrap checks done in the main body of sv_vcatpvfn_flags.
11211  * It means that e.g. on a 32-bit system the width/precision can't be more
11212  * than 1G, which seems reasonable.
11213  */
11214
11215 STATIC STRLEN
11216 S_expect_number(pTHX_ const char **const pattern)
11217 {
11218     STRLEN var;
11219
11220     PERL_ARGS_ASSERT_EXPECT_NUMBER;
11221
11222     assert(inRANGE(**pattern, '1', '9'));
11223
11224     var = *(*pattern)++ - '0';
11225     while (isDIGIT(**pattern)) {
11226         /* if var * 10 + 9 would exceed 1/4 max strlen, croak */
11227         if (var > ((((STRLEN)~0) / 4 - 9) / 10))
11228             S_croak_overflow();
11229         var = var * 10 + (*(*pattern)++ - '0');
11230     }
11231     return var;
11232 }
11233
11234 /* Implement a fast "%.0f": given a pointer to the end of a buffer (caller
11235  * ensures it's big enough), back fill it with the rounded integer part of
11236  * nv. Returns ptr to start of string, and sets *len to its length.
11237  * Returns NULL if not convertible.
11238  */
11239
11240 STATIC char *
11241 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
11242 {
11243     const int neg = nv < 0;
11244     UV uv;
11245
11246     PERL_ARGS_ASSERT_F0CONVERT;
11247
11248     assert(!Perl_isinfnan(nv));
11249     if (neg)
11250         nv = -nv;
11251     if (nv != 0.0 && nv < (NV) UV_MAX) {
11252         char *p = endbuf;
11253         uv = (UV)nv;
11254         if (uv != nv) {
11255             nv += 0.5;
11256             uv = (UV)nv;
11257             if (uv & 1 && uv == nv)
11258                 uv--;                   /* Round to even */
11259         }
11260         do {
11261             const unsigned dig = uv % 10;
11262             *--p = '0' + dig;
11263         } while (uv /= 10);
11264         if (neg)
11265             *--p = '-';
11266         *len = endbuf - p;
11267         return p;
11268     }
11269     return NULL;
11270 }
11271
11272
11273 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
11274
11275 void
11276 Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11277                  va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11278 {
11279     PERL_ARGS_ASSERT_SV_VCATPVFN;
11280
11281     sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, SV_GMAGIC|SV_SMAGIC);
11282 }
11283
11284
11285 /* For the vcatpvfn code, we need a long double target in case
11286  * HAS_LONG_DOUBLE, even without USE_LONG_DOUBLE, so that we can printf
11287  * with long double formats, even without NV being long double.  But we
11288  * call the target 'fv' instead of 'nv', since most of the time it is not
11289  * (most compilers these days recognize "long double", even if only as a
11290  * synonym for "double").
11291 */
11292 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE && \
11293         defined(PERL_PRIgldbl) && !defined(USE_QUADMATH)
11294 #  define VCATPVFN_FV_GF PERL_PRIgldbl
11295 #  if defined(__VMS) && defined(__ia64) && defined(__IEEE_FLOAT)
11296        /* Work around breakage in OTS$CVT_FLOAT_T_X */
11297 #    define VCATPVFN_NV_TO_FV(nv,fv)                    \
11298             STMT_START {                                \
11299                 double _dv = nv;                        \
11300                 fv = Perl_isnan(_dv) ? LDBL_QNAN : _dv; \
11301             } STMT_END
11302 #  else
11303 #    define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11304 #  endif
11305    typedef long double vcatpvfn_long_double_t;
11306 #else
11307 #  define VCATPVFN_FV_GF NVgf
11308 #  define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11309    typedef NV vcatpvfn_long_double_t;
11310 #endif
11311
11312 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11313 /* The first double can be as large as 2**1023, or '1' x '0' x 1023.
11314  * The second double can be as small as 2**-1074, or '0' x 1073 . '1'.
11315  * The sum of them can be '1' . '0' x 2096 . '1', with implied radix point
11316  * after the first 1023 zero bits.
11317  *
11318  * XXX The 2098 is quite large (262.25 bytes) and therefore some sort
11319  * of dynamically growing buffer might be better, start at just 16 bytes
11320  * (for example) and grow only when necessary.  Or maybe just by looking
11321  * at the exponents of the two doubles? */
11322 #  define DOUBLEDOUBLE_MAXBITS 2098
11323 #endif
11324
11325 /* vhex will contain the values (0..15) of the hex digits ("nybbles"
11326  * of 4 bits); 1 for the implicit 1, and the mantissa bits, four bits
11327  * per xdigit.  For the double-double case, this can be rather many.
11328  * The non-double-double-long-double overshoots since all bits of NV
11329  * are not mantissa bits, there are also exponent bits. */
11330 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11331 #  define VHEX_SIZE (3+DOUBLEDOUBLE_MAXBITS/4)
11332 #else
11333 #  define VHEX_SIZE (1+(NVSIZE * 8)/4)
11334 #endif
11335
11336 /* If we do not have a known long double format, (including not using
11337  * long doubles, or long doubles being equal to doubles) then we will
11338  * fall back to the ldexp/frexp route, with which we can retrieve at
11339  * most as many bits as our widest unsigned integer type is.  We try
11340  * to get a 64-bit unsigned integer even if we are not using a 64-bit UV.
11341  *
11342  * (If you want to test the case of UVSIZE == 4, NVSIZE == 8,
11343  *  set the MANTISSATYPE to int and the MANTISSASIZE to 4.)
11344  */
11345 #if defined(HAS_QUAD) && defined(Uquad_t)
11346 #  define MANTISSATYPE Uquad_t
11347 #  define MANTISSASIZE 8
11348 #else
11349 #  define MANTISSATYPE UV
11350 #  define MANTISSASIZE UVSIZE
11351 #endif
11352
11353 #if defined(DOUBLE_LITTLE_ENDIAN) || defined(LONGDOUBLE_LITTLE_ENDIAN)
11354 #  define HEXTRACT_LITTLE_ENDIAN
11355 #elif defined(DOUBLE_BIG_ENDIAN) || defined(LONGDOUBLE_BIG_ENDIAN)
11356 #  define HEXTRACT_BIG_ENDIAN
11357 #else
11358 #  define HEXTRACT_MIX_ENDIAN
11359 #endif
11360
11361 /* S_hextract() is a helper for S_format_hexfp, for extracting
11362  * the hexadecimal values (for %a/%A).  The nv is the NV where the value
11363  * are being extracted from (either directly from the long double in-memory
11364  * presentation, or from the uquad computed via frexp+ldexp).  frexp also
11365  * is used to update the exponent.  The subnormal is set to true
11366  * for IEEE 754 subnormals/denormals (including the x86 80-bit format).
11367  * The vhex is the pointer to the beginning of the output buffer of VHEX_SIZE.
11368  *
11369  * The tricky part is that S_hextract() needs to be called twice:
11370  * the first time with vend as NULL, and the second time with vend as
11371  * the pointer returned by the first call.  What happens is that on
11372  * the first round the output size is computed, and the intended
11373  * extraction sanity checked.  On the second round the actual output
11374  * (the extraction of the hexadecimal values) takes place.
11375  * Sanity failures cause fatal failures during both rounds. */
11376 STATIC U8*
11377 S_hextract(pTHX_ const NV nv, int* exponent, bool *subnormal,
11378            U8* vhex, U8* vend)
11379 {
11380     U8* v = vhex;
11381     int ix;
11382     int ixmin = 0, ixmax = 0;
11383
11384     /* XXX Inf/NaN are not handled here, since it is
11385      * assumed they are to be output as "Inf" and "NaN". */
11386
11387     /* These macros are just to reduce typos, they have multiple
11388      * repetitions below, but usually only one (or sometimes two)
11389      * of them is really being used. */
11390     /* HEXTRACT_OUTPUT() extracts the high nybble first. */
11391 #define HEXTRACT_OUTPUT_HI(ix) (*v++ = nvp[ix] >> 4)
11392 #define HEXTRACT_OUTPUT_LO(ix) (*v++ = nvp[ix] & 0xF)
11393 #define HEXTRACT_OUTPUT(ix) \
11394     STMT_START { \
11395       HEXTRACT_OUTPUT_HI(ix); HEXTRACT_OUTPUT_LO(ix); \
11396    } STMT_END
11397 #define HEXTRACT_COUNT(ix, c) \
11398     STMT_START { \
11399       v += c; if (ix < ixmin) ixmin = ix; else if (ix > ixmax) ixmax = ix; \
11400    } STMT_END
11401 #define HEXTRACT_BYTE(ix) \
11402     STMT_START { \
11403       if (vend) HEXTRACT_OUTPUT(ix); else HEXTRACT_COUNT(ix, 2); \
11404    } STMT_END
11405 #define HEXTRACT_LO_NYBBLE(ix) \
11406     STMT_START { \
11407       if (vend) HEXTRACT_OUTPUT_LO(ix); else HEXTRACT_COUNT(ix, 1); \
11408    } STMT_END
11409     /* HEXTRACT_TOP_NYBBLE is just convenience disguise,
11410      * to make it look less odd when the top bits of a NV
11411      * are extracted using HEXTRACT_LO_NYBBLE: the highest
11412      * order bits can be in the "low nybble" of a byte. */
11413 #define HEXTRACT_TOP_NYBBLE(ix) HEXTRACT_LO_NYBBLE(ix)
11414 #define HEXTRACT_BYTES_LE(a, b) \
11415     for (ix = a; ix >= b; ix--) { HEXTRACT_BYTE(ix); }
11416 #define HEXTRACT_BYTES_BE(a, b) \
11417     for (ix = a; ix <= b; ix++) { HEXTRACT_BYTE(ix); }
11418 #define HEXTRACT_GET_SUBNORMAL(nv) *subnormal = Perl_fp_class_denorm(nv)
11419 #define HEXTRACT_IMPLICIT_BIT(nv) \
11420     STMT_START { \
11421         if (!*subnormal) { \
11422             if (vend) *v++ = ((nv) == 0.0) ? 0 : 1; else v++; \
11423         } \
11424    } STMT_END
11425
11426 /* Most formats do.  Those which don't should undef this.
11427  *
11428  * But also note that IEEE 754 subnormals do not have it, or,
11429  * expressed alternatively, their implicit bit is zero. */
11430 #define HEXTRACT_HAS_IMPLICIT_BIT
11431
11432 /* Many formats do.  Those which don't should undef this. */
11433 #define HEXTRACT_HAS_TOP_NYBBLE
11434
11435     /* HEXTRACTSIZE is the maximum number of xdigits. */
11436 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
11437 #  define HEXTRACTSIZE (2+DOUBLEDOUBLE_MAXBITS/4)
11438 #else
11439 #  define HEXTRACTSIZE 2 * NVSIZE
11440 #endif
11441
11442     const U8* vmaxend = vhex + HEXTRACTSIZE;
11443
11444     assert(HEXTRACTSIZE <= VHEX_SIZE);
11445
11446     PERL_UNUSED_VAR(ix); /* might happen */
11447     (void)Perl_frexp(PERL_ABS(nv), exponent);
11448     *subnormal = FALSE;
11449     if (vend && (vend <= vhex || vend > vmaxend)) {
11450         /* diag_listed_as: Hexadecimal float: internal error (%s) */
11451         Perl_croak(aTHX_ "Hexadecimal float: internal error (entry)");
11452     }
11453     {
11454         /* First check if using long doubles. */
11455 #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE)
11456 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
11457         /* Used in e.g. VMS and HP-UX IA-64, e.g. -0.1L:
11458          * 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb bf */
11459         /* The bytes 13..0 are the mantissa/fraction,
11460          * the 15,14 are the sign+exponent. */
11461         const U8* nvp = (const U8*)(&nv);
11462         HEXTRACT_GET_SUBNORMAL(nv);
11463         HEXTRACT_IMPLICIT_BIT(nv);
11464 #    undef HEXTRACT_HAS_TOP_NYBBLE
11465         HEXTRACT_BYTES_LE(13, 0);
11466 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
11467         /* Used in e.g. Solaris Sparc and HP-UX PA-RISC, e.g. -0.1L:
11468          * bf fb 99 99 99 99 99 99 99 99 99 99 99 99 99 9a */
11469         /* The bytes 2..15 are the mantissa/fraction,
11470          * the 0,1 are the sign+exponent. */
11471         const U8* nvp = (const U8*)(&nv);
11472         HEXTRACT_GET_SUBNORMAL(nv);
11473         HEXTRACT_IMPLICIT_BIT(nv);
11474 #    undef HEXTRACT_HAS_TOP_NYBBLE
11475         HEXTRACT_BYTES_BE(2, 15);
11476 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
11477         /* x86 80-bit "extended precision", 64 bits of mantissa / fraction /
11478          * significand, 15 bits of exponent, 1 bit of sign.  No implicit bit.
11479          * NVSIZE can be either 12 (ILP32, Solaris x86) or 16 (LP64, Linux
11480          * and OS X), meaning that 2 or 6 bytes are empty padding. */
11481         /* The bytes 0..1 are the sign+exponent,
11482          * the bytes 2..9 are the mantissa/fraction. */
11483         const U8* nvp = (const U8*)(&nv);
11484 #    undef HEXTRACT_HAS_IMPLICIT_BIT
11485 #    undef HEXTRACT_HAS_TOP_NYBBLE
11486         HEXTRACT_GET_SUBNORMAL(nv);
11487         HEXTRACT_BYTES_LE(7, 0);
11488 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
11489         /* Does this format ever happen? (Wikipedia says the Motorola
11490          * 6888x math coprocessors used format _like_ this but padded
11491          * to 96 bits with 16 unused bits between the exponent and the
11492          * mantissa.) */
11493         const U8* nvp = (const U8*)(&nv);
11494 #    undef HEXTRACT_HAS_IMPLICIT_BIT
11495 #    undef HEXTRACT_HAS_TOP_NYBBLE
11496         HEXTRACT_GET_SUBNORMAL(nv);
11497         HEXTRACT_BYTES_BE(0, 7);
11498 #  else
11499 #    define HEXTRACT_FALLBACK
11500         /* Double-double format: two doubles next to each other.
11501          * The first double is the high-order one, exactly like
11502          * it would be for a "lone" double.  The second double
11503          * is shifted down using the exponent so that that there
11504          * are no common bits.  The tricky part is that the value
11505          * of the double-double is the SUM of the two doubles and
11506          * the second one can be also NEGATIVE.
11507          *
11508          * Because of this tricky construction the bytewise extraction we
11509          * use for the other long double formats doesn't work, we must
11510          * extract the values bit by bit.
11511          *
11512          * The little-endian double-double is used .. somewhere?
11513          *
11514          * The big endian double-double is used in e.g. PPC/Power (AIX)
11515          * and MIPS (SGI).
11516          *
11517          * The mantissa bits are in two separate stretches, e.g. for -0.1L:
11518          * 9a 99 99 99 99 99 59 bc 9a 99 99 99 99 99 b9 3f (LE)
11519          * 3f b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE)
11520          */
11521 #  endif
11522 #else /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) */
11523         /* Using normal doubles, not long doubles.
11524          *
11525          * We generate 4-bit xdigits (nybble/nibble) instead of 8-bit
11526          * bytes, since we might need to handle printf precision, and
11527          * also need to insert the radix. */
11528 #  if NVSIZE == 8
11529 #    ifdef HEXTRACT_LITTLE_ENDIAN
11530         /* 0 1 2 3 4 5 6 7 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11531         const U8* nvp = (const U8*)(&nv);
11532         HEXTRACT_GET_SUBNORMAL(nv);
11533         HEXTRACT_IMPLICIT_BIT(nv);
11534         HEXTRACT_TOP_NYBBLE(6);
11535         HEXTRACT_BYTES_LE(5, 0);
11536 #    elif defined(HEXTRACT_BIG_ENDIAN)
11537         /* 7 6 5 4 3 2 1 0 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11538         const U8* nvp = (const U8*)(&nv);
11539         HEXTRACT_GET_SUBNORMAL(nv);
11540         HEXTRACT_IMPLICIT_BIT(nv);
11541         HEXTRACT_TOP_NYBBLE(1);
11542         HEXTRACT_BYTES_BE(2, 7);
11543 #    elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
11544         /* 4 5 6 7 0 1 2 3 (MSB = 7, LSB = 0, 6:7 = nybble:exponent:sign) */
11545         const U8* nvp = (const U8*)(&nv);
11546         HEXTRACT_GET_SUBNORMAL(nv);
11547         HEXTRACT_IMPLICIT_BIT(nv);
11548         HEXTRACT_TOP_NYBBLE(2); /* 6 */
11549         HEXTRACT_BYTE(1); /* 5 */
11550         HEXTRACT_BYTE(0); /* 4 */
11551         HEXTRACT_BYTE(7); /* 3 */
11552         HEXTRACT_BYTE(6); /* 2 */
11553         HEXTRACT_BYTE(5); /* 1 */
11554         HEXTRACT_BYTE(4); /* 0 */
11555 #    elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
11556         /* 3 2 1 0 7 6 5 4 (MSB = 7, LSB = 0, 7:6 = sign:exponent:nybble) */
11557         const U8* nvp = (const U8*)(&nv);
11558         HEXTRACT_GET_SUBNORMAL(nv);
11559         HEXTRACT_IMPLICIT_BIT(nv);
11560         HEXTRACT_TOP_NYBBLE(5); /* 6 */
11561         HEXTRACT_BYTE(6); /* 5 */
11562         HEXTRACT_BYTE(7); /* 4 */
11563         HEXTRACT_BYTE(0); /* 3 */
11564         HEXTRACT_BYTE(1); /* 2 */
11565         HEXTRACT_BYTE(2); /* 1 */
11566         HEXTRACT_BYTE(3); /* 0 */
11567 #    else
11568 #      define HEXTRACT_FALLBACK
11569 #    endif
11570 #  else
11571 #    define HEXTRACT_FALLBACK
11572 #  endif
11573 #endif /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) #else */
11574
11575 #ifdef HEXTRACT_FALLBACK
11576         HEXTRACT_GET_SUBNORMAL(nv);
11577 #  undef HEXTRACT_HAS_TOP_NYBBLE /* Meaningless, but consistent. */
11578         /* The fallback is used for the double-double format, and
11579          * for unknown long double formats, and for unknown double
11580          * formats, or in general unknown NV formats. */
11581         if (nv == (NV)0.0) {
11582             if (vend)
11583                 *v++ = 0;
11584             else
11585                 v++;
11586             *exponent = 0;
11587         }
11588         else {
11589             NV d = nv < 0 ? -nv : nv;
11590             NV e = (NV)1.0;
11591             U8 ha = 0x0; /* hexvalue accumulator */
11592             U8 hd = 0x8; /* hexvalue digit */
11593
11594             /* Shift d and e (and update exponent) so that e <= d < 2*e,
11595              * this is essentially manual frexp(). Multiplying by 0.5 and
11596              * doubling should be lossless in binary floating point. */
11597
11598             *exponent = 1;
11599
11600             while (e > d) {
11601                 e *= (NV)0.5;
11602                 (*exponent)--;
11603             }
11604             /* Now d >= e */
11605
11606             while (d >= e + e) {
11607                 e += e;
11608                 (*exponent)++;
11609             }
11610             /* Now e <= d < 2*e */
11611
11612             /* First extract the leading hexdigit (the implicit bit). */
11613             if (d >= e) {
11614                 d -= e;
11615                 if (vend)
11616                     *v++ = 1;
11617                 else
11618                     v++;
11619             }
11620             else {
11621                 if (vend)
11622                     *v++ = 0;
11623                 else
11624                     v++;
11625             }
11626             e *= (NV)0.5;
11627
11628             /* Then extract the remaining hexdigits. */
11629             while (d > (NV)0.0) {
11630                 if (d >= e) {
11631                     ha |= hd;
11632                     d -= e;
11633                 }
11634                 if (hd == 1) {
11635                     /* Output or count in groups of four bits,
11636                      * that is, when the hexdigit is down to one. */
11637                     if (vend)
11638                         *v++ = ha;
11639                     else
11640                         v++;
11641                     /* Reset the hexvalue. */
11642                     ha = 0x0;
11643                     hd = 0x8;
11644                 }
11645                 else
11646                     hd >>= 1;
11647                 e *= (NV)0.5;
11648             }
11649
11650             /* Flush possible pending hexvalue. */
11651             if (ha) {
11652                 if (vend)
11653                     *v++ = ha;
11654                 else
11655                     v++;
11656             }
11657         }
11658 #endif
11659     }
11660     /* Croak for various reasons: if the output pointer escaped the
11661      * output buffer, if the extraction index escaped the extraction
11662      * buffer, or if the ending output pointer didn't match the
11663      * previously computed value. */
11664     if (v <= vhex || v - vhex >= VHEX_SIZE ||
11665         /* For double-double the ixmin and ixmax stay at zero,
11666          * which is convenient since the HEXTRACTSIZE is tricky
11667          * for double-double. */
11668         ixmin < 0 || ixmax >= NVSIZE ||
11669         (vend && v != vend)) {
11670         /* diag_listed_as: Hexadecimal float: internal error (%s) */
11671         Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11672     }
11673     return v;
11674 }
11675
11676
11677 /* S_format_hexfp(): helper function for Perl_sv_vcatpvfn_flags().
11678  *
11679  * Processes the %a/%A hexadecimal floating-point format, since the
11680  * built-in snprintf()s which are used for most of the f/p formats, don't
11681  * universally handle %a/%A.
11682  * Populates buf of length bufsize, and returns the length of the created
11683  * string.
11684  * The rest of the args have the same meaning as the local vars of the
11685  * same name within Perl_sv_vcatpvfn_flags().
11686  *
11687  * The caller's determination of IN_LC(LC_NUMERIC), passed as in_lc_numeric,
11688  * is used to ensure we do the right thing when we need to access the locale's
11689  * numeric radix.
11690  *
11691  * It requires the caller to make buf large enough.
11692  */
11693
11694 static STRLEN
11695 S_format_hexfp(pTHX_ char * const buf, const STRLEN bufsize, const char c,
11696                     const NV nv, const vcatpvfn_long_double_t fv,
11697                     bool has_precis, STRLEN precis, STRLEN width,
11698                     bool alt, char plus, bool left, bool fill, bool in_lc_numeric)
11699 {
11700     /* Hexadecimal floating point. */
11701     char* p = buf;
11702     U8 vhex[VHEX_SIZE];
11703     U8* v = vhex; /* working pointer to vhex */
11704     U8* vend; /* pointer to one beyond last digit of vhex */
11705     U8* vfnz = NULL; /* first non-zero */
11706     U8* vlnz = NULL; /* last non-zero */
11707     U8* v0 = NULL; /* first output */
11708     const bool lower = (c == 'a');
11709     /* At output the values of vhex (up to vend) will
11710      * be mapped through the xdig to get the actual
11711      * human-readable xdigits. */
11712     const char* xdig = PL_hexdigit;
11713     STRLEN zerotail = 0; /* how many extra zeros to append */
11714     int exponent = 0; /* exponent of the floating point input */
11715     bool hexradix = FALSE; /* should we output the radix */
11716     bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
11717     bool negative = FALSE;
11718     STRLEN elen;
11719
11720     /* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
11721      *
11722      * For example with denormals, (assuming the vanilla
11723      * 64-bit double): the exponent is zero. 1xp-1074 is
11724      * the smallest denormal and the smallest double, it
11725      * could be output also as 0x0.0000000000001p-1022 to
11726      * match its internal structure. */
11727
11728     vend = S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, NULL);
11729     S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, vend);
11730
11731 #if NVSIZE > DOUBLESIZE
11732 #  ifdef HEXTRACT_HAS_IMPLICIT_BIT
11733     /* In this case there is an implicit bit,
11734      * and therefore the exponent is shifted by one. */
11735     exponent--;
11736 #  elif defined(NV_X86_80_BIT)
11737     if (subnormal) {
11738         /* The subnormals of the x86-80 have a base exponent of -16382,
11739          * (while the physical exponent bits are zero) but the frexp()
11740          * returned the scientific-style floating exponent.  We want
11741          * to map the last one as:
11742          * -16831..-16384 -> -16382 (the last normal is 0x1p-16382)
11743          * -16835..-16388 -> -16384
11744          * since we want to keep the first hexdigit
11745          * as one of the [8421]. */
11746         exponent = -4 * ( (exponent + 1) / -4) - 2;
11747     } else {
11748         exponent -= 4;
11749     }
11750     /* TBD: other non-implicit-bit platforms than the x86-80. */
11751 #  endif
11752 #endif
11753
11754     negative = fv < 0 || Perl_signbit(nv);
11755     if (negative)
11756         *p++ = '-';
11757     else if (plus)
11758         *p++ = plus;
11759     *p++ = '0';
11760     if (lower) {
11761         *p++ = 'x';
11762     }
11763     else {
11764         *p++ = 'X';
11765         xdig += 16; /* Use uppercase hex. */
11766     }
11767
11768     /* Find the first non-zero xdigit. */
11769     for (v = vhex; v < vend; v++) {
11770         if (*v) {
11771             vfnz = v;
11772             break;
11773         }
11774     }
11775
11776     if (vfnz) {
11777         /* Find the last non-zero xdigit. */
11778         for (v = vend - 1; v >= vhex; v--) {
11779             if (*v) {
11780                 vlnz = v;
11781                 break;
11782             }
11783         }
11784
11785 #if NVSIZE == DOUBLESIZE
11786         if (fv != 0.0)
11787             exponent--;
11788 #endif
11789
11790         if (subnormal) {
11791 #ifndef NV_X86_80_BIT
11792           if (vfnz[0] > 1) {
11793             /* IEEE 754 subnormals (but not the x86 80-bit):
11794              * we want "normalize" the subnormal,
11795              * so we need to right shift the hex nybbles
11796              * so that the output of the subnormal starts
11797              * from the first true bit.  (Another, equally
11798              * valid, policy would be to dump the subnormal
11799              * nybbles as-is, to display the "physical" layout.) */
11800             int i, n;
11801             U8 *vshr;
11802             /* Find the ceil(log2(v[0])) of
11803              * the top non-zero nybble. */
11804             for (i = vfnz[0], n = 0; i > 1; i >>= 1, n++) { }
11805             assert(n < 4);
11806             assert(vlnz);
11807             vlnz[1] = 0;
11808             for (vshr = vlnz; vshr >= vfnz; vshr--) {
11809               vshr[1] |= (vshr[0] & (0xF >> (4 - n))) << (4 - n);
11810               vshr[0] >>= n;
11811             }
11812             if (vlnz[1]) {
11813               vlnz++;
11814             }
11815           }
11816 #endif
11817           v0 = vfnz;
11818         } else {
11819           v0 = vhex;
11820         }
11821
11822         if (has_precis) {
11823             U8* ve = (subnormal ? vlnz + 1 : vend);
11824             SSize_t vn = ve - v0;
11825             assert(vn >= 1);
11826             if (precis < (Size_t)(vn - 1)) {
11827                 bool overflow = FALSE;
11828                 if (v0[precis + 1] < 0x8) {
11829                     /* Round down, nothing to do. */
11830                 } else if (v0[precis + 1] > 0x8) {
11831                     /* Round up. */
11832                     v0[precis]++;
11833                     overflow = v0[precis] > 0xF;
11834                     v0[precis] &= 0xF;
11835                 } else { /* v0[precis] == 0x8 */
11836                     /* Half-point: round towards the one
11837                      * with the even least-significant digit:
11838                      * 08 -> 0  88 -> 8
11839                      * 18 -> 2  98 -> a
11840                      * 28 -> 2  a8 -> a
11841                      * 38 -> 4  b8 -> c
11842                      * 48 -> 4  c8 -> c
11843                      * 58 -> 6  d8 -> e
11844                      * 68 -> 6  e8 -> e
11845                      * 78 -> 8  f8 -> 10 */
11846                     if ((v0[precis] & 0x1)) {
11847                         v0[precis]++;
11848                     }
11849                     overflow = v0[precis] > 0xF;
11850                     v0[precis] &= 0xF;
11851                 }
11852
11853                 if (overflow) {
11854                     for (v = v0 + precis - 1; v >= v0; v--) {
11855                         (*v)++;
11856                         overflow = *v > 0xF;
11857                         (*v) &= 0xF;
11858                         if (!overflow) {
11859                             break;
11860                         }
11861                     }
11862                     if (v == v0 - 1 && overflow) {
11863                         /* If the overflow goes all the
11864                          * way to the front, we need to
11865                          * insert 0x1 in front, and adjust
11866                          * the exponent. */
11867                         Move(v0, v0 + 1, vn - 1, char);
11868                         *v0 = 0x1;
11869                         exponent += 4;
11870                     }
11871                 }
11872
11873                 /* The new effective "last non zero". */
11874                 vlnz = v0 + precis;
11875             }
11876             else {
11877                 zerotail =
11878                   subnormal ? precis - vn + 1 :
11879                   precis - (vlnz - vhex);
11880             }
11881         }
11882
11883         v = v0;
11884         *p++ = xdig[*v++];
11885
11886         /* If there are non-zero xdigits, the radix
11887          * is output after the first one. */
11888         if (vfnz < vlnz) {
11889           hexradix = TRUE;
11890         }
11891     }
11892     else {
11893         *p++ = '0';
11894         exponent = 0;
11895         zerotail = has_precis ? precis : 0;
11896     }
11897
11898     /* The radix is always output if precis, or if alt. */
11899     if ((has_precis && precis > 0) || alt) {
11900       hexradix = TRUE;
11901     }
11902
11903     if (hexradix) {
11904 #ifndef USE_LOCALE_NUMERIC
11905         PERL_UNUSED_ARG(in_lc_numeric);
11906
11907         *p++ = '.';
11908 #else
11909         if (in_lc_numeric) {
11910             STRLEN n;
11911             WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
11912                 const char* r = SvPV(PL_numeric_radix_sv, n);
11913                 Copy(r, p, n, char);
11914             });
11915             p += n;
11916         }
11917         else {
11918             *p++ = '.';
11919         }
11920 #endif
11921     }
11922
11923     if (vlnz) {
11924         while (v <= vlnz)
11925             *p++ = xdig[*v++];
11926     }
11927
11928     if (zerotail > 0) {
11929       while (zerotail--) {
11930         *p++ = '0';
11931       }
11932     }
11933
11934     elen = p - buf;
11935
11936     /* sanity checks */
11937     if (elen >= bufsize || width >= bufsize)
11938         /* diag_listed_as: Hexadecimal float: internal error (%s) */
11939         Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11940
11941     elen += my_snprintf(p, bufsize - elen,
11942                         "%c%+d", lower ? 'p' : 'P',
11943                         exponent);
11944
11945     if (elen < width) {
11946         STRLEN gap = (STRLEN)(width - elen);
11947         if (left) {
11948             /* Pad the back with spaces. */
11949             memset(buf + elen, ' ', gap);
11950         }
11951         else if (fill) {
11952             /* Insert the zeros after the "0x" and the
11953              * the potential sign, but before the digits,
11954              * otherwise we end up with "0000xH.HHH...",
11955              * when we want "0x000H.HHH..."  */
11956             STRLEN nzero = gap;
11957             char* zerox = buf + 2;
11958             STRLEN nmove = elen - 2;
11959             if (negative || plus) {
11960                 zerox++;
11961                 nmove--;
11962             }
11963             Move(zerox, zerox + nzero, nmove, char);
11964             memset(zerox, fill ? '0' : ' ', nzero);
11965         }
11966         else {
11967             /* Move it to the right. */
11968             Move(buf, buf + gap,
11969                  elen, char);
11970             /* Pad the front with spaces. */
11971             memset(buf, ' ', gap);
11972         }
11973         elen = width;
11974     }
11975     return elen;
11976 }
11977
11978 /*
11979 =for apidoc sv_vcatpvfn
11980 =for apidoc_item sv_vcatpvfn_flags
11981
11982 These process their arguments like C<L<vsprintf(3)>> and append the formatted output
11983 to an SV.  They use an array of SVs if the C-style variable argument list is
11984 missing (C<NULL>). Argument reordering (using format specifiers like C<%2$d> or
11985 C<%*2$d>) is supported only when using an array of SVs; using a C-style
11986 C<va_list> argument list with a format string that uses argument reordering
11987 will yield an exception.
11988
11989 When running with taint checks enabled, they indicate via C<maybe_tainted> if
11990 results are untrustworthy (often due to the use of locales).
11991
11992 They assume that C<pat> has the same utf8-ness as C<sv>.  It's the caller's
11993 responsibility to ensure that this is so.
11994
11995 They differ in that C<sv_vcatpvfn_flags> has a C<flags> parameter in which you
11996 can set or clear the C<SV_GMAGIC> and/or S<SV_SMAGIC> flags, to specify which
11997 magic to handle or not handle; whereas plain C<sv_vcatpvfn> always specifies
11998 both 'get' and 'set' magic.
11999
12000 They are usually used via one of the frontends L</C<sv_vcatpvf>> and
12001 L</C<sv_vcatpvf_mg>>.
12002
12003 =cut
12004 */
12005
12006
12007 void
12008 Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
12009                        va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted,
12010                        const U32 flags)
12011 {
12012     const char *fmtstart; /* character following the current '%' */
12013     const char *q;        /* current position within format */
12014     const char *patend;
12015     STRLEN origlen;
12016     Size_t svix = 0;
12017     static const char nullstr[] = "(null)";
12018     bool has_utf8 = DO_UTF8(sv);    /* has the result utf8? */
12019     const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
12020     /* Times 4: a decimal digit takes more than 3 binary digits.
12021      * NV_DIG: mantissa takes that many decimal digits.
12022      * Plus 32: Playing safe. */
12023     char ebuf[IV_DIG * 4 + NV_DIG + 32];
12024     bool no_redundant_warning = FALSE; /* did we use any explicit format parameter index? */
12025 #ifdef USE_LOCALE_NUMERIC
12026     bool have_in_lc_numeric = FALSE;
12027 #endif
12028     /* we never change this unless USE_LOCALE_NUMERIC */
12029     bool in_lc_numeric = FALSE;
12030     SV *tmp_sv = NULL;
12031
12032     PERL_ARGS_ASSERT_SV_VCATPVFN_FLAGS;
12033     PERL_UNUSED_ARG(maybe_tainted);
12034
12035     if (flags & SV_GMAGIC)
12036         SvGETMAGIC(sv);
12037
12038     /* no matter what, this is a string now */
12039     (void)SvPV_force_nomg(sv, origlen);
12040
12041     /* the code that scans for flags etc following a % relies on
12042      * a '\0' being present to avoid falling off the end. Ideally that
12043      * should be fixed */
12044     assert(pat[patlen] == '\0');
12045
12046
12047     /* Special-case "", "%s", "%-p" (SVf - see below) and "%.0f".
12048      * In each case, if there isn't the correct number of args, instead
12049      * fall through to the main code to handle the issuing of any
12050      * warnings etc.
12051      */
12052
12053     if (patlen == 0 && (args || sv_count == 0))
12054         return;
12055
12056     if (patlen <= 4 && pat[0] == '%' && (args || sv_count == 1)) {
12057
12058         /* "%s" */
12059         if (patlen == 2 && pat[1] == 's') {
12060             if (args) {
12061                 const char * const s = va_arg(*args, char*);
12062                 sv_catpv_nomg(sv, s ? s : nullstr);
12063             }
12064             else {
12065                 /* we want get magic on the source but not the target.
12066                  * sv_catsv can't do that, though */
12067                 SvGETMAGIC(*svargs);
12068                 sv_catsv_nomg(sv, *svargs);
12069             }
12070             return;
12071         }
12072
12073         /* "%-p" */
12074         if (args) {
12075             if (patlen == 3  && pat[1] == '-' && pat[2] == 'p') {
12076                 SV *asv = MUTABLE_SV(va_arg(*args, void*));
12077                 sv_catsv_nomg(sv, asv);
12078                 return;
12079             }
12080         }
12081 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
12082         /* special-case "%.0f" */
12083         else if (   patlen == 4
12084                  && pat[1] == '.' && pat[2] == '0' && pat[3] == 'f')
12085         {
12086             const NV nv = SvNV(*svargs);
12087             if (LIKELY(!Perl_isinfnan(nv))) {
12088                 STRLEN l;
12089                 char *p;
12090
12091                 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
12092                     sv_catpvn_nomg(sv, p, l);
12093                     return;
12094                 }
12095             }
12096         }
12097 #endif /* !USE_LONG_DOUBLE */
12098     }
12099
12100
12101     patend = (char*)pat + patlen;
12102     for (fmtstart = pat; fmtstart < patend; fmtstart = q) {
12103         char intsize     = 0;         /* size qualifier in "%hi..." etc */
12104         bool alt         = FALSE;     /* has      "%#..."    */
12105         bool left        = FALSE;     /* has      "%-..."    */
12106         bool fill        = FALSE;     /* has      "%0..."    */
12107         char plus        = 0;         /* has      "%+..."    */
12108         STRLEN width     = 0;         /* value of "%NNN..."  */
12109         bool has_precis  = FALSE;     /* has      "%.NNN..." */
12110         STRLEN precis    = 0;         /* value of "%.NNN..." */
12111         int base         = 0;         /* base to print in, e.g. 8 for %o */
12112         UV uv            = 0;         /* the value to print of int-ish args */
12113
12114         bool vectorize   = FALSE;     /* has      "%v..."    */
12115         bool vec_utf8    = FALSE;     /* SvUTF8(vec arg)     */
12116         const U8 *vecstr = NULL;      /* SvPVX(vec arg)      */
12117         STRLEN veclen    = 0;         /* SvCUR(vec arg)      */
12118         const char *dotstr = NULL;    /* separator string for %v */
12119         STRLEN dotstrlen;             /* length of separator string for %v */
12120
12121         Size_t efix      = 0;         /* explicit format parameter index */
12122         const Size_t osvix  = svix;   /* original index in case of bad fmt */
12123
12124         SV *argsv        = NULL;
12125         bool is_utf8     = FALSE;     /* is this item utf8?   */
12126         bool arg_missing = FALSE;     /* give "Missing argument" warning */
12127         char esignbuf[4];             /* holds sign prefix, e.g. "-0x" */
12128         STRLEN esignlen  = 0;         /* length of e.g. "-0x" */
12129         STRLEN zeros     = 0;         /* how many '0' to prepend */
12130
12131         const char *eptr = NULL;      /* the address of the element string */
12132         STRLEN elen      = 0;         /* the length  of the element string */
12133
12134         char c;                       /* the actual format ('d', s' etc) */
12135
12136         bool escape_it   = FALSE;     /* if this is a string should we quote and escape it? */
12137
12138
12139         /* echo everything up to the next format specification */
12140         for (q = fmtstart; q < patend && *q != '%'; ++q)
12141             {};
12142
12143         if (q > fmtstart) {
12144             if (has_utf8 && !pat_utf8) {
12145                 /* upgrade and copy the bytes of fmtstart..q-1 to utf8 on
12146                  * the fly */
12147                 const char *p;
12148                 char *dst;
12149                 STRLEN need = SvCUR(sv) + (q - fmtstart) + 1;
12150
12151                 for (p = fmtstart; p < q; p++)
12152                     if (!NATIVE_BYTE_IS_INVARIANT(*p))
12153                         need++;
12154                 SvGROW(sv, need);
12155
12156                 dst = SvEND(sv);
12157                 for (p = fmtstart; p < q; p++)
12158                     append_utf8_from_native_byte((U8)*p, (U8**)&dst);
12159                 *dst = '\0';
12160                 SvCUR_set(sv, need - 1);
12161             }
12162             else
12163                 S_sv_catpvn_simple(aTHX_ sv, fmtstart, q - fmtstart);
12164         }
12165         if (q++ >= patend)
12166             break;
12167
12168         fmtstart = q; /* fmtstart is char following the '%' */
12169
12170 /*
12171     We allow format specification elements in this order:
12172         \d+\$              explicit format parameter index
12173         [-+ 0#]+           flags
12174         v|\*(\d+\$)?v      vector with optional (optionally specified) arg
12175         0                  flag (as above): repeated to allow "v02"
12176         \d+|\*(\d+\$)?     width using optional (optionally specified) arg
12177         \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
12178         [hlqLV]            size
12179     [%bcdefginopsuxDFOUX] format (mandatory)
12180 */
12181
12182         if (inRANGE(*q, '1', '9')) {
12183             width = expect_number(&q);
12184             if (*q == '$') {
12185                 if (args)
12186                     Perl_croak_nocontext(
12187                         "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12188                 ++q;
12189                 efix = (Size_t)width;
12190                 width = 0;
12191                 no_redundant_warning = TRUE;
12192             } else {
12193                 goto gotwidth;
12194             }
12195         }
12196
12197         /* FLAGS */
12198
12199         while (*q) {
12200             switch (*q) {
12201             case ' ':
12202             case '+':
12203                 if (plus == '+' && *q == ' ') /* '+' over ' ' */
12204                     q++;
12205                 else
12206                     plus = *q++;
12207                 continue;
12208
12209             case '-':
12210                 left = TRUE;
12211                 q++;
12212                 continue;
12213
12214             case '0':
12215                 fill = TRUE;
12216                 q++;
12217                 continue;
12218
12219             case '#':
12220                 alt = TRUE;
12221                 q++;
12222                 continue;
12223
12224             default:
12225                 break;
12226             }
12227             break;
12228         }
12229
12230       /* at this point we can expect one of:
12231        *
12232        *  123  an explicit width
12233        *  *    width taken from next arg
12234        *  *12$ width taken from 12th arg
12235        *       or no width
12236        *
12237        * But any width specification may be preceded by a v, in one of its
12238        * forms:
12239        *        v
12240        *        *v
12241        *        *12$v
12242        * So an asterisk may be either a width specifier or a vector
12243        * separator arg specifier, and we don't know which initially
12244        */
12245
12246       tryasterisk:
12247         if (*q == '*') {
12248             STRLEN ix; /* explicit width/vector separator index */
12249             q++;
12250             if (inRANGE(*q, '1', '9')) {
12251                 ix = expect_number(&q);
12252                 if (*q++ == '$') {
12253                     if (args)
12254                         Perl_croak_nocontext(
12255                             "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12256                     no_redundant_warning = TRUE;
12257                 } else
12258                     goto unknown;
12259             }
12260             else
12261                 ix = 0;
12262
12263             if (*q == 'v') {
12264                 SV *vecsv;
12265                 /* The asterisk was for  *v, *NNN$v: vectorizing, but not
12266                  * with the default "." */
12267                 q++;
12268                 if (vectorize)
12269                     goto unknown;
12270                 if (args)
12271                     vecsv = va_arg(*args, SV*);
12272                 else {
12273                     ix = ix ? ix - 1 : svix++;
12274                     vecsv = ix < sv_count ? svargs[ix]
12275                                        : (arg_missing = TRUE, &PL_sv_no);
12276                 }
12277                 dotstr = SvPV_const(vecsv, dotstrlen);
12278                 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
12279                    bad with tied or overloaded values that return UTF8.  */
12280                 if (DO_UTF8(vecsv))
12281                     is_utf8 = TRUE;
12282                 else if (has_utf8) {
12283                     vecsv = sv_mortalcopy(vecsv);
12284                     sv_utf8_upgrade(vecsv);
12285                     dotstr = SvPV_const(vecsv, dotstrlen);
12286                     is_utf8 = TRUE;
12287                 }
12288                 vectorize = TRUE;
12289                 goto tryasterisk;
12290             }
12291
12292             /* the asterisk specified a width */
12293             {
12294                 int i = 0;
12295                 SV *width_sv = NULL;
12296                 if (args)
12297                     i = va_arg(*args, int);
12298                 else {
12299                     ix = ix ? ix - 1 : svix++;
12300                     width_sv = (ix < sv_count) ? svargs[ix]
12301                                       : (arg_missing = TRUE, (SV*)NULL);
12302                 }
12303                 width = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &left);
12304             }
12305         }
12306         else if (*q == 'v') {
12307             q++;
12308             if (vectorize)
12309                 goto unknown;
12310             vectorize = TRUE;
12311             dotstr = ".";
12312             dotstrlen = 1;
12313             goto tryasterisk;
12314
12315         }
12316         else {
12317         /* explicit width? */
12318             if(*q == '0') {
12319                 fill = TRUE;
12320                 q++;
12321             }
12322             if (inRANGE(*q, '1', '9'))
12323                 width = expect_number(&q);
12324         }
12325
12326       gotwidth:
12327
12328         /* PRECISION */
12329
12330         if (*q == '.') {
12331             q++;
12332             if (*q == '*') {
12333                 STRLEN ix; /* explicit precision index */
12334                 q++;
12335                 if (inRANGE(*q, '1', '9')) {
12336                     ix = expect_number(&q);
12337                     if (*q++ == '$') {
12338                         if (args)
12339                             Perl_croak_nocontext(
12340                                 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12341                         no_redundant_warning = TRUE;
12342                     } else
12343                         goto unknown;
12344                 }
12345                 else
12346                     ix = 0;
12347
12348                 {
12349                     int i = 0;
12350                     SV *width_sv = NULL;
12351                     bool neg = FALSE;
12352
12353                     if (args)
12354                         i = va_arg(*args, int);
12355                     else {
12356                         ix = ix ? ix - 1 : svix++;
12357                         width_sv = (ix < sv_count) ? svargs[ix]
12358                                           : (arg_missing = TRUE, (SV*)NULL);
12359                     }
12360                     precis = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &neg);
12361                     has_precis = !neg;
12362                     /* ignore negative precision */
12363                     if (!has_precis)
12364                         precis = 0;
12365                 }
12366             }
12367             else {
12368                 /* although it doesn't seem documented, this code has long
12369                  * behaved so that:
12370                  *   no digits following the '.' is treated like '.0'
12371                  *   the number may be preceded by any number of zeroes,
12372                  *      e.g. "%.0001f", which is the same as "%.1f"
12373                  * so I've kept that behaviour. DAPM May 2017
12374                  */
12375                 while (*q == '0')
12376                     q++;
12377                 precis = inRANGE(*q, '1', '9') ? expect_number(&q) : 0;
12378                 has_precis = TRUE;
12379             }
12380         }
12381
12382         /* SIZE */
12383
12384         switch (*q) {
12385 #ifdef WIN32
12386         case 'I':                       /* Ix, I32x, and I64x */
12387 #  ifdef USE_64_BIT_INT
12388             if (q[1] == '6' && q[2] == '4') {
12389                 q += 3;
12390                 intsize = 'q';
12391                 break;
12392             }
12393 #  endif
12394             if (q[1] == '3' && q[2] == '2') {
12395                 q += 3;
12396                 break;
12397             }
12398 #  ifdef USE_64_BIT_INT
12399             intsize = 'q';
12400 #  endif
12401             q++;
12402             break;
12403 #endif
12404 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12405     (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12406         case 'L':                       /* Ld */
12407             /* FALLTHROUGH */
12408 #  if IVSIZE >= 8
12409         case 'q':                       /* qd */
12410 #  endif
12411             intsize = 'q';
12412             q++;
12413             break;
12414 #endif
12415         case 'l':
12416             ++q;
12417 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12418     (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12419             if (*q == 'l') {    /* lld, llf */
12420                 intsize = 'q';
12421                 ++q;
12422             }
12423             else
12424 #endif
12425                 intsize = 'l';
12426             break;
12427         case 'h':
12428             if (*++q == 'h') {  /* hhd, hhu */
12429                 intsize = 'c';
12430                 ++q;
12431             }
12432             else
12433                 intsize = 'h';
12434             break;
12435 #ifdef USE_QUADMATH
12436         case 'Q':
12437 #endif
12438         case 'V':
12439         case 'z':
12440         case 't':
12441         case 'j':
12442             intsize = *q++;
12443             break;
12444         }
12445
12446         /* CONVERSION */
12447
12448         c = *q++; /* c now holds the conversion type */
12449
12450         /* '%' doesn't have an arg, so skip arg processing */
12451         if (c == '%') {
12452             eptr = q - 1;
12453             elen = 1;
12454             if (vectorize)
12455                 goto unknown;
12456             goto string;
12457         }
12458
12459         if (vectorize && !memCHRs("BbDdiOouUXx", c))
12460             goto unknown;
12461
12462         /* get next arg (individual branches do their own va_arg()
12463          * handling for the args case) */
12464
12465         if (!args) {
12466             efix = efix ? efix - 1 : svix++;
12467             argsv = efix < sv_count ? svargs[efix]
12468                                  : (arg_missing = TRUE, &PL_sv_no);
12469         }
12470
12471
12472         switch (c) {
12473
12474             /* STRINGS */
12475
12476         case 's':
12477             if (args) {
12478                 eptr = va_arg(*args, char*);
12479                 if (eptr)
12480                     if (has_precis)
12481                         elen = my_strnlen(eptr, precis);
12482                     else
12483                         elen = strlen(eptr);
12484                 else {
12485                     eptr = (char *)nullstr;
12486                     elen = sizeof nullstr - 1;
12487                 }
12488             }
12489             else {
12490                 eptr = SvPV_const(argsv, elen);
12491                 if (DO_UTF8(argsv)) {
12492                     STRLEN old_precis = precis;
12493                     if (has_precis && precis < elen) {
12494                         STRLEN ulen = sv_or_pv_len_utf8(argsv, eptr, elen);
12495                         STRLEN p = precis > ulen ? ulen : precis;
12496                         precis = sv_or_pv_pos_u2b(argsv, eptr, p, 0);
12497                                                         /* sticks at end */
12498                     }
12499                     if (width) { /* fudge width (can't fudge elen) */
12500                         if (has_precis && precis < elen)
12501                             width += precis - old_precis;
12502                         else
12503                             width +=
12504                                 elen - sv_or_pv_len_utf8(argsv,eptr,elen);
12505                     }
12506                     is_utf8 = TRUE;
12507                 }
12508             }
12509
12510         string:
12511             if (escape_it) {
12512                 U32 flags = PERL_PV_PRETTY_QUOTEDPREFIX;
12513                 if (is_utf8)
12514                     flags |= PERL_PV_ESCAPE_UNI;
12515
12516                 if (!tmp_sv) {
12517                     /* "blah"... where blah might be made up
12518                      * of characters like \x{1234} */
12519                     tmp_sv = newSV(1 + (PERL_QUOTEDPREFIX_LEN * 8) + 1 + 3);
12520                     sv_2mortal(tmp_sv);
12521                 }
12522                 pv_pretty(tmp_sv, eptr, elen, PERL_QUOTEDPREFIX_LEN,
12523                             NULL, NULL, flags);
12524                 eptr = SvPV_const(tmp_sv, elen);
12525             }
12526             if (has_precis && precis < elen)
12527                 elen = precis;
12528             break;
12529
12530             /* INTEGERS */
12531
12532         case 'p':
12533
12534             /* BEGIN NOTE
12535              *
12536              * We want to extend the C level sprintf format API with
12537              * custom formats for specific types (eg SV*) and behavior.
12538              * However some C compilers are "sprintf aware" and will
12539              * throw compile time exceptions when an illegal sprintf is
12540              * encountered, so we can't just add new format letters.
12541              *
12542              * However it turns out the length argument to the %p format
12543              * is more or less useless (the size of a pointer does not
12544              * change over time) and is not really used in the C level
12545              * code. Accordingly we can map our special behavior to
12546              * specific "length" options to the %p format. We hide these
12547              * mappings behind defines anyway, so nobody needs to know
12548              * that HEKf is actually %2p. This keeps the C compiler
12549              * happy while allowing us to add new formats.
12550              *
12551              * Note the existing logic for which number is used for what
12552              * is torturous. All negative values are used for SVf, and
12553              * non-negative values have arbitrary meanings with no
12554              * structure to them. This may change in the future.
12555              *
12556              * NEVER use the raw %p values directly. Always use the define
12557              * as the underlying mapping may change in the future.
12558              *
12559              * END NOTE
12560              *
12561              * %p extensions:
12562              *
12563              * "%...p" is normally treated like "%...x", except that the
12564              * number to print is the SV's address (or a pointer address
12565              * for C-ish sprintf).
12566              *
12567              * However, the C-ish sprintf variant allows a few special
12568              * extensions. These are currently:
12569              *
12570              * %-p       (SVf)  Like %s, but gets the string from an SV*
12571              *                  arg rather than a char* arg. Use C<SVfARG()>
12572              *                  to set up the argument properly.
12573              *                  (This was previously %_).
12574              *
12575              * %-<num>p         Ditto but like %.<num>s (i.e. num is max
12576              *                  width), there is no escaped and quoted version
12577              *                  of this.
12578              *
12579              * %1p       (PVf_QUOTEDPREFIX). Like raw %s, but it is escaped
12580              *                  and quoted.
12581              *
12582              * %5p       (SVf_QUOTEDPREFIX) Like SVf, but length restricted,
12583              *                  escaped and quoted with pv_pretty. Intended
12584              *                  for error messages.
12585              *
12586              * %2p       (HEKf) Like %s, but using the key string in a HEK
12587              * %7p       (HEKf_QUOTEDPREFIX) ... but escaped and quoted.
12588              *
12589              * %3p       (HEKf256) Ditto but like %.256s
12590              * %8p       (HEKf256_QUOTEDPREFIX) ... but escaped and quoted
12591              *
12592              * %d%lu%4p  (UTF8f) A utf8 string. Consumes 3 args:
12593              *                       (cBOOL(utf8), len, string_buf).
12594              *                   It's handled by the "case 'd'" branch
12595              *                   rather than here.
12596              * %d%lu%9p  (UTF8f_QUOTEDPREFIX) .. but escaped and quoted.
12597              *
12598              *
12599              * %<num>p   where num is > 9: reserved for future
12600              *           extensions. Warns, but then is treated as a
12601              *           general %p (print hex address) format.
12602              *
12603              * NOTE: If you add a new magic %p value you will
12604              * need to update F<t/porting/diag.t> to be aware of it
12605              * on top of adding the various defines and etc. Do not
12606              * forget to add it to F<pod/perlguts.pod> as well.
12607              */
12608
12609             if (   args
12610                 && !intsize
12611                 && !fill
12612                 && !plus
12613                 && !has_precis
12614                     /* not %*p or %*1$p - any width was explicit */
12615                 && q[-2] != '*'
12616                 && q[-2] != '$'
12617             ) {
12618                 if (left || width == 5) {                /* %-p (SVf), %-NNNp, %5p */
12619                     if (left && width) {
12620                         precis = width;
12621                         has_precis = TRUE;
12622                     } else if (width == 5) {
12623                         escape_it = TRUE;
12624                     }
12625                     argsv = MUTABLE_SV(va_arg(*args, void*));
12626                     eptr = SvPV_const(argsv, elen);
12627                     if (DO_UTF8(argsv))
12628                         is_utf8 = TRUE;
12629                     width = 0;
12630                     goto string;
12631                 }
12632                 else if (width == 2 || width == 3 ||
12633                          width == 7 || width == 8)
12634                 {        /* HEKf, HEKf256, HEKf_QUOTEDPREFIX, HEKf256_QUOTEDPREFIX */
12635                     HEK * const hek = va_arg(*args, HEK *);
12636                     eptr = HEK_KEY(hek);
12637                     elen = HEK_LEN(hek);
12638                     if (HEK_UTF8(hek))
12639                         is_utf8 = TRUE;
12640                     if (width == 3) {
12641                         precis = 256;
12642                         has_precis = TRUE;
12643                     }
12644                     if (width > 5)
12645                         escape_it = TRUE;
12646                     width = 0;
12647                     goto string;
12648                 }
12649                 else if (width == 1) {
12650                     eptr = va_arg(*args,char *);
12651                     elen = strlen(eptr);
12652                     escape_it = TRUE;
12653                     width = 0;
12654                     goto string;
12655                 }
12656                 else if (width) {
12657                     /* note width=4 or width=9 is handled under %d */
12658                     Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
12659                          "internal %%<num>p might conflict with future printf extensions");
12660                 }
12661             }
12662
12663             /* treat as normal %...p */
12664
12665             uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
12666             base = 16;
12667             c = 'x';    /* in case the format string contains '#' */
12668             goto do_integer;
12669
12670         case 'c':
12671             /* Ignore any size specifiers, since they're not documented as
12672              * being allowed for %c (ideally we should warn on e.g. '%hc').
12673              * Setting a default intsize, along with a positive
12674              * (which signals unsigned) base, causes, for C-ish use, the
12675              * va_arg to be interpreted as an unsigned int, when it's
12676              * actually signed, which will convert -ve values to high +ve
12677              * values. Note that unlike the libc %c, values > 255 will
12678              * convert to high unicode points rather than being truncated
12679              * to 8 bits. For perlish use, it will do SvUV(argsv), which
12680              * will again convert -ve args to high -ve values.
12681              */
12682             intsize = 0;
12683             base = 1; /* special value that indicates we're doing a 'c' */
12684             goto get_int_arg_val;
12685
12686         case 'D':
12687 #ifdef IV_IS_QUAD
12688             intsize = 'q';
12689 #else
12690             intsize = 'l';
12691 #endif
12692             base = -10;
12693             goto get_int_arg_val;
12694
12695         case 'd':
12696             /* probably just a plain %d, but it might be the start of the
12697              * special UTF8f format, which usually looks something like
12698              * "%d%lu%4p" (the lu may vary by platform) or
12699              * "%d%lu%9p" for an escaped version.
12700              */
12701             assert((UTF8f)[0] == 'd');
12702             assert((UTF8f)[1] == '%');
12703
12704              if (   args              /* UTF8f only valid for C-ish sprintf */
12705                  && q == fmtstart + 1 /* plain %d, not %....d */
12706                  && patend >= fmtstart + sizeof(UTF8f) - 1 /* long enough */
12707                  && *q == '%'
12708                  && strnEQ(q + 1, (UTF8f) + 2, sizeof(UTF8f) - 5)
12709                  && q[sizeof(UTF8f)-3] == 'p'
12710                  && (q[sizeof(UTF8f)-4] == '4' ||
12711                      q[sizeof(UTF8f)-4] == '9'))
12712             {
12713                 /* The argument has already gone through cBOOL, so the cast
12714                    is safe. */
12715                 if (q[sizeof(UTF8f)-4] == '9')
12716                     escape_it = TRUE;
12717                 is_utf8 = (bool)va_arg(*args, int);
12718                 elen = va_arg(*args, UV);
12719                 /* if utf8 length is larger than 0x7ffff..., then it might
12720                  * have been a signed value that wrapped */
12721                 if (elen  > ((~(STRLEN)0) >> 1)) {
12722                     assert(0); /* in DEBUGGING build we want to crash */
12723                     elen = 0; /* otherwise we want to treat this as an empty string */
12724                 }
12725                 eptr = va_arg(*args, char *);
12726                 q += sizeof(UTF8f) - 2;
12727                 goto string;
12728             }
12729
12730             /* FALLTHROUGH */
12731         case 'i':
12732             base = -10;
12733             goto get_int_arg_val;
12734
12735         case 'U':
12736 #ifdef IV_IS_QUAD
12737             intsize = 'q';
12738 #else
12739             intsize = 'l';
12740 #endif
12741             /* FALLTHROUGH */
12742         case 'u':
12743             base = 10;
12744             goto get_int_arg_val;
12745
12746         case 'B':
12747         case 'b':
12748             base = 2;
12749             goto get_int_arg_val;
12750
12751         case 'O':
12752 #ifdef IV_IS_QUAD
12753             intsize = 'q';
12754 #else
12755             intsize = 'l';
12756 #endif
12757             /* FALLTHROUGH */
12758         case 'o':
12759             base = 8;
12760             goto get_int_arg_val;
12761
12762         case 'X':
12763         case 'x':
12764             base = 16;
12765
12766           get_int_arg_val:
12767
12768             if (vectorize) {
12769                 STRLEN ulen;
12770                 SV *vecsv;
12771
12772                 if (base < 0) {
12773                     base = -base;
12774                     if (plus)
12775                          esignbuf[esignlen++] = plus;
12776                 }
12777
12778                 /* initialise the vector string to iterate over */
12779
12780                 vecsv = args ? va_arg(*args, SV*) : argsv;
12781
12782                 /* if this is a version object, we need to convert
12783                  * back into v-string notation and then let the
12784                  * vectorize happen normally
12785                  */
12786                 if (sv_isobject(vecsv) && sv_derived_from(vecsv, "version")) {
12787                     if ( hv_existss(MUTABLE_HV(SvRV(vecsv)), "alpha") ) {
12788                         Perl_ck_warner_d(aTHX_ packWARN(WARN_PRINTF),
12789                         "vector argument not supported with alpha versions");
12790                         vecsv = &PL_sv_no;
12791                     }
12792                     else {
12793                         vecstr = (U8*)SvPV_const(vecsv,veclen);
12794                         vecsv = sv_newmortal();
12795                         scan_vstring((char *)vecstr, (char *)vecstr + veclen,
12796                                      vecsv);
12797                     }
12798                 }
12799                 vecstr = (U8*)SvPV_const(vecsv, veclen);
12800                 vec_utf8 = DO_UTF8(vecsv);
12801
12802               /* This is the re-entry point for when we're iterating
12803                * over the individual characters of a vector arg */
12804               vector:
12805                 if (!veclen)
12806                     goto done_valid_conversion;
12807                 if (vec_utf8)
12808                     uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
12809                                         UTF8_ALLOW_ANYUV);
12810                 else {
12811                     uv = *vecstr;
12812                     ulen = 1;
12813                 }
12814                 vecstr += ulen;
12815                 veclen -= ulen;
12816             }
12817             else {
12818                 /* test arg for inf/nan. This can trigger an unwanted
12819                  * 'str' overload, so manually force 'num' overload first
12820                  * if necessary */
12821                 if (argsv) {
12822                     SvGETMAGIC(argsv);
12823                     if (UNLIKELY(SvAMAGIC(argsv)))
12824                         argsv = sv_2num(argsv);
12825                     if (UNLIKELY(isinfnansv(argsv)))
12826                         goto handle_infnan_argsv;
12827                 }
12828
12829                 if (base < 0) {
12830                     /* signed int type */
12831                     IV iv;
12832                     base = -base;
12833                     if (args) {
12834                         switch (intsize) {
12835                         case 'c':  iv = (char)va_arg(*args, int);  break;
12836                         case 'h':  iv = (short)va_arg(*args, int); break;
12837                         case 'l':  iv = va_arg(*args, long);       break;
12838                         case 'V':  iv = va_arg(*args, IV);         break;
12839                         case 'z':  iv = va_arg(*args, SSize_t);    break;
12840 #ifdef HAS_PTRDIFF_T
12841                         case 't':  iv = va_arg(*args, ptrdiff_t);  break;
12842 #endif
12843                         default:   iv = va_arg(*args, int);        break;
12844                         case 'j':  iv = (IV) va_arg(*args, PERL_INTMAX_T); break;
12845                         case 'q':
12846 #if IVSIZE >= 8
12847                                    iv = va_arg(*args, Quad_t);     break;
12848 #else
12849                                    goto unknown;
12850 #endif
12851                         }
12852                     }
12853                     else {
12854                         /* assign to tiv then cast to iv to work around
12855                          * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12856                         IV tiv = SvIV_nomg(argsv);
12857                         switch (intsize) {
12858                         case 'c':  iv = (char)tiv;   break;
12859                         case 'h':  iv = (short)tiv;  break;
12860                         case 'l':  iv = (long)tiv;   break;
12861                         case 'V':
12862                         default:   iv = tiv;         break;
12863                         case 'q':
12864 #if IVSIZE >= 8
12865                                    iv = (Quad_t)tiv; break;
12866 #else
12867                                    goto unknown;
12868 #endif
12869                         }
12870                     }
12871
12872                     /* now convert iv to uv */
12873                     if (iv >= 0) {
12874                         uv = iv;
12875                         if (plus)
12876                             esignbuf[esignlen++] = plus;
12877                     }
12878                     else {
12879                         /* Using 0- here to silence bogus warning from MS VC */
12880                         uv = (UV) (0 - (UV) iv);
12881                         esignbuf[esignlen++] = '-';
12882                     }
12883                 }
12884                 else {
12885                     /* unsigned int type */
12886                     if (args) {
12887                         switch (intsize) {
12888                         case 'c': uv = (unsigned char)va_arg(*args, unsigned);
12889                                   break;
12890                         case 'h': uv = (unsigned short)va_arg(*args, unsigned);
12891                                   break;
12892                         case 'l': uv = va_arg(*args, unsigned long); break;
12893                         case 'V': uv = va_arg(*args, UV);            break;
12894                         case 'z': uv = va_arg(*args, Size_t);        break;
12895 #ifdef HAS_PTRDIFF_T
12896                                   /* will sign extend, but there is no
12897                                    * uptrdiff_t, so oh well */
12898                         case 't': uv = va_arg(*args, ptrdiff_t);     break;
12899 #endif
12900                         case 'j': uv = (UV) va_arg(*args, PERL_UINTMAX_T); break;
12901                         default:  uv = va_arg(*args, unsigned);      break;
12902                         case 'q':
12903 #if IVSIZE >= 8
12904                                   uv = va_arg(*args, Uquad_t);       break;
12905 #else
12906                                   goto unknown;
12907 #endif
12908                         }
12909                     }
12910                     else {
12911                         /* assign to tiv then cast to iv to work around
12912                          * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12913                         UV tuv = SvUV_nomg(argsv);
12914                         switch (intsize) {
12915                         case 'c': uv = (unsigned char)tuv;  break;
12916                         case 'h': uv = (unsigned short)tuv; break;
12917                         case 'l': uv = (unsigned long)tuv;  break;
12918                         case 'V':
12919                         default:  uv = tuv;                 break;
12920                         case 'q':
12921 #if IVSIZE >= 8
12922                                   uv = (Uquad_t)tuv;        break;
12923 #else
12924                                   goto unknown;
12925 #endif
12926                         }
12927                     }
12928                 }
12929             }
12930
12931         do_integer:
12932             {
12933                 char *ptr = ebuf + sizeof ebuf;
12934                 unsigned dig;
12935                 zeros = 0;
12936
12937                 switch (base) {
12938                 case 16:
12939                     {
12940                     const char * const p =
12941                             (c == 'X') ? PL_hexdigit + 16 : PL_hexdigit;
12942
12943                         do {
12944                             dig = uv & 15;
12945                             *--ptr = p[dig];
12946                         } while (uv >>= 4);
12947                         if (alt && *ptr != '0') {
12948                             esignbuf[esignlen++] = '0';
12949                             esignbuf[esignlen++] = c;  /* 'x' or 'X' */
12950                         }
12951                         break;
12952                     }
12953                 case 8:
12954                     do {
12955                         dig = uv & 7;
12956                         *--ptr = '0' + dig;
12957                     } while (uv >>= 3);
12958                     if (alt && *ptr != '0')
12959                         *--ptr = '0';
12960                     break;
12961                 case 2:
12962                     do {
12963                         dig = uv & 1;
12964                         *--ptr = '0' + dig;
12965                     } while (uv >>= 1);
12966                     if (alt && *ptr != '0') {
12967                         esignbuf[esignlen++] = '0';
12968                         esignbuf[esignlen++] = c; /* 'b' or 'B' */
12969                     }
12970                     break;
12971
12972                 case 1:
12973                     /* special-case: base 1 indicates a 'c' format:
12974                      * we use the common code for extracting a uv,
12975                      * but handle that value differently here than
12976                      * all the other int types */
12977                     if ((uv > 255 ||
12978                          (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
12979                         && !IN_BYTES)
12980                     {
12981                         STATIC_ASSERT_STMT(sizeof(ebuf) >= UTF8_MAXBYTES + 1);
12982                         eptr = ebuf;
12983                         elen = uvchr_to_utf8((U8*)eptr, uv) - (U8*)ebuf;
12984                         is_utf8 = TRUE;
12985                     }
12986                     else {
12987                         eptr = ebuf;
12988                         ebuf[0] = (char)uv;
12989                         elen = 1;
12990                     }
12991                     goto string;
12992
12993                 default:                /* it had better be ten or less */
12994                     do {
12995                         dig = uv % base;
12996                         *--ptr = '0' + dig;
12997                     } while (uv /= base);
12998                     break;
12999                 }
13000                 elen = (ebuf + sizeof ebuf) - ptr;
13001                 eptr = ptr;
13002                 if (has_precis) {
13003                     if (precis > elen)
13004                         zeros = precis - elen;
13005                     else if (precis == 0 && elen == 1 && *eptr == '0'
13006                              && !(base == 8 && alt)) /* "%#.0o" prints "0" */
13007                         elen = 0;
13008
13009                     /* a precision nullifies the 0 flag. */
13010                     fill = FALSE;
13011                 }
13012             }
13013             break;
13014
13015             /* FLOATING POINT */
13016
13017         case 'F':
13018             c = 'f';            /* maybe %F isn't supported here */
13019             /* FALLTHROUGH */
13020         case 'e': case 'E':
13021         case 'f':
13022         case 'g': case 'G':
13023         case 'a': case 'A':
13024
13025         {
13026             STRLEN float_need; /* what PL_efloatsize needs to become */
13027             bool hexfp;        /* hexadecimal floating point? */
13028
13029             vcatpvfn_long_double_t fv;
13030             NV                     nv;
13031
13032             /* This is evil, but floating point is even more evil */
13033
13034             /* for SV-style calling, we can only get NV
13035                for C-style calling, we assume %f is double;
13036                for simplicity we allow any of %Lf, %llf, %qf for long double
13037             */
13038             switch (intsize) {
13039 #if defined(USE_QUADMATH)
13040             case 'Q':
13041                 break;
13042 #endif
13043             case 'V':
13044 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13045                 intsize = 'q';
13046 #endif
13047                 break;
13048 /* [perl #20339] - we should accept and ignore %lf rather than die */
13049             case 'l':
13050                 /* FALLTHROUGH */
13051             default:
13052 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13053                 intsize = args ? 0 : 'q';
13054 #endif
13055                 break;
13056             case 'q':
13057 #if defined(HAS_LONG_DOUBLE)
13058                 break;
13059 #else
13060                 /* FALLTHROUGH */
13061 #endif
13062             case 'c':
13063             case 'h':
13064             case 'z':
13065             case 't':
13066             case 'j':
13067                 goto unknown;
13068             }
13069
13070             /* Now we need (long double) if intsize == 'q', else (double). */
13071             if (args) {
13072                 /* Note: do not pull NVs off the va_list with va_arg()
13073                  * (pull doubles instead) because if you have a build
13074                  * with long doubles, you would always be pulling long
13075                  * doubles, which would badly break anyone using only
13076                  * doubles (i.e. the majority of builds). In other
13077                  * words, you cannot mix doubles and long doubles.
13078                  * The only case where you can pull off long doubles
13079                  * is when the format specifier explicitly asks so with
13080                  * e.g. "%Lg". */
13081 #ifdef USE_QUADMATH
13082                 nv = intsize == 'Q' ? va_arg(*args, NV) :
13083                     intsize == 'q' ? va_arg(*args, long double) :
13084                     va_arg(*args, double);
13085                 fv = nv;
13086 #elif LONG_DOUBLESIZE > DOUBLESIZE
13087                 if (intsize == 'q') {
13088                     fv = va_arg(*args, long double);
13089                     nv = fv;
13090                 } else {
13091                     nv = va_arg(*args, double);
13092                     VCATPVFN_NV_TO_FV(nv, fv);
13093                 }
13094 #else
13095                 nv = va_arg(*args, double);
13096                 fv = nv;
13097 #endif
13098             }
13099             else
13100             {
13101                 SvGETMAGIC(argsv);
13102                 /* we jump here if an int-ish format encountered an
13103                  * infinite/Nan argsv. After setting nv/fv, it falls
13104                  * into the isinfnan block which follows */
13105               handle_infnan_argsv:
13106                 nv = SvNV_nomg(argsv);
13107                 VCATPVFN_NV_TO_FV(nv, fv);
13108             }
13109
13110             if (Perl_isinfnan(nv)) {
13111                 if (c == 'c')
13112                     Perl_croak(aTHX_ "Cannot printf %" NVgf " with '%c'",
13113                                nv, (int)c);
13114
13115                 elen = S_infnan_2pv(nv, ebuf, sizeof(ebuf), plus);
13116                 assert(elen);
13117                 eptr = ebuf;
13118                 zeros     = 0;
13119                 esignlen  = 0;
13120                 dotstrlen = 0;
13121                 break;
13122             }
13123
13124             /* special-case "%.0f" */
13125             if (   c == 'f'
13126                 && !precis
13127                 && has_precis
13128                 && !(width || left || plus || alt)
13129                 && !fill
13130                 && intsize != 'q'
13131                 && ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
13132             )
13133                 goto float_concat;
13134
13135             /* Determine the buffer size needed for the various
13136              * floating-point formats.
13137              *
13138              * The basic possibilities are:
13139              *
13140              *               <---P--->
13141              *    %f 1111111.123456789
13142              *    %e       1.111111123e+06
13143              *    %a     0x1.0f4471f9bp+20
13144              *    %g        1111111.12
13145              *    %g        1.11111112e+15
13146              *
13147              * where P is the value of the precision in the format, or 6
13148              * if not specified. Note the two possible output formats of
13149              * %g; in both cases the number of significant digits is <=
13150              * precision.
13151              *
13152              * For most of the format types the maximum buffer size needed
13153              * is precision, plus: any leading 1 or 0x1, the radix
13154              * point, and an exponent.  The difficult one is %f: for a
13155              * large positive exponent it can have many leading digits,
13156              * which needs to be calculated specially. Also %a is slightly
13157              * different in that in the absence of a specified precision,
13158              * it uses as many digits as necessary to distinguish
13159              * different values.
13160              *
13161              * First, here are the constant bits. For ease of calculation
13162              * we over-estimate the needed buffer size, for example by
13163              * assuming all formats have an exponent and a leading 0x1.
13164              *
13165              * Also for production use, add a little extra overhead for
13166              * safety's sake. Under debugging don't, as it means we're
13167              * more likely to quickly spot issues during development.
13168              */
13169
13170             float_need =     1  /* possible unary minus */
13171                           +  4  /* "0x1" plus very unlikely carry */
13172                           +  1  /* default radix point '.' */
13173                           +  2  /* "e-", "p+" etc */
13174                           +  6  /* exponent: up to 16383 (quad fp) */
13175 #ifndef DEBUGGING
13176                           + 20  /* safety net */
13177 #endif
13178                           +  1; /* \0 */
13179
13180
13181             /* determine the radix point len, e.g. length(".") in "1.2" */
13182 #ifdef USE_LOCALE_NUMERIC
13183             /* note that we may either explicitly use PL_numeric_radix_sv
13184              * below, or implicitly, via an snprintf() variant.
13185              * Note also things like ps_AF.utf8 which has
13186              * "\N{ARABIC DECIMAL SEPARATOR} as a radix point */
13187             if (! have_in_lc_numeric) {
13188                 in_lc_numeric = IN_LC(LC_NUMERIC);
13189                 have_in_lc_numeric = TRUE;
13190             }
13191
13192             if (in_lc_numeric) {
13193                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
13194                     /* this can't wrap unless PL_numeric_radix_sv is a string
13195                      * consuming virtually all the 32-bit or 64-bit address
13196                      * space
13197                      */
13198                     float_need += (SvCUR(PL_numeric_radix_sv) - 1);
13199
13200                     /* floating-point formats only get utf8 if the radix point
13201                      * is utf8. All other characters in the string are < 128
13202                      * and so can be safely appended to both a non-utf8 and utf8
13203                      * string as-is.
13204                      * Note that this will convert the output to utf8 even if
13205                      * the radix point didn't get output.
13206                      */
13207                     if (SvUTF8(PL_numeric_radix_sv) && !has_utf8) {
13208                         sv_utf8_upgrade(sv);
13209                         has_utf8 = TRUE;
13210                     }
13211                 });
13212             }
13213 #endif
13214
13215             hexfp = FALSE;
13216
13217             if (isALPHA_FOLD_EQ(c, 'f')) {
13218                 /* Determine how many digits before the radix point
13219                  * might be emitted.  frexp() (or frexpl) has some
13220                  * unspecified behaviour for nan/inf/-inf, so lucky we've
13221                  * already handled them above */
13222                 STRLEN digits;
13223                 int i = PERL_INT_MIN;
13224                 (void)Perl_frexp((NV)fv, &i);
13225                 if (i == PERL_INT_MIN)
13226                     Perl_die(aTHX_ "panic: frexp: %" VCATPVFN_FV_GF, fv);
13227
13228                 if (i > 0) {
13229                     digits = BIT_DIGITS(i);
13230                     /* this can't overflow. 'digits' will only be a few
13231                      * thousand even for the largest floating-point types.
13232                      * And up until now float_need is just some small
13233                      * constants plus radix len, which can't be in
13234                      * overflow territory unless the radix SV is consuming
13235                      * over 1/2 the address space */
13236                     assert(float_need < ((STRLEN)~0) - digits);
13237                     float_need += digits;
13238                 }
13239             }
13240             else if (UNLIKELY(isALPHA_FOLD_EQ(c, 'a'))) {
13241                 hexfp = TRUE;
13242                 if (!has_precis) {
13243                     /* %a in the absence of precision may print as many
13244                      * digits as needed to represent the entire mantissa
13245                      * bit pattern.
13246                      * This estimate seriously overshoots in most cases,
13247                      * but better the undershooting.  Firstly, all bytes
13248                      * of the NV are not mantissa, some of them are
13249                      * exponent.  Secondly, for the reasonably common
13250                      * long doubles case, the "80-bit extended", two
13251                      * or six bytes of the NV are unused. Also, we'll
13252                      * still pick up an extra +6 from the default
13253                      * precision calculation below. */
13254                     STRLEN digits =
13255 #ifdef LONGDOUBLE_DOUBLEDOUBLE
13256                         /* For the "double double", we need more.
13257                          * Since each double has their own exponent, the
13258                          * doubles may float (haha) rather far from each
13259                          * other, and the number of required bits is much
13260                          * larger, up to total of DOUBLEDOUBLE_MAXBITS bits.
13261                          * See the definition of DOUBLEDOUBLE_MAXBITS.
13262                          *
13263                          * Need 2 hexdigits for each byte. */
13264                         (DOUBLEDOUBLE_MAXBITS/8 + 1) * 2;
13265 #else
13266                         NVSIZE * 2; /* 2 hexdigits for each byte */
13267 #endif
13268                     /* see "this can't overflow" comment above */
13269                     assert(float_need < ((STRLEN)~0) - digits);
13270                     float_need += digits;
13271                 }
13272             }
13273             /* special-case "%.<number>g" if it will fit in ebuf */
13274             else if (c == 'g'
13275                 && precis   /* See earlier comment about buggy Gconvert
13276                                when digits, aka precis, is 0  */
13277                 && has_precis
13278                 /* check that "%.<number>g" formatting will fit in ebuf  */
13279                 && sizeof(ebuf) - float_need > precis
13280                 /* sizeof(ebuf) - float_need will have wrapped if float_need > sizeof(ebuf).     *
13281                  * Therefore we should check that float_need < sizeof(ebuf). Normally, we would  *
13282                  * have run this check first, but that triggers incorrect -Wformat-overflow      *
13283                  * compilation warnings with some versions of gcc if Gconvert invokes sprintf(). *
13284                  * ( See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89161 )                   *
13285                  * So, instead, we check it next:                                                */
13286                 && float_need < sizeof(ebuf)
13287                 && !(width || left || plus || alt)
13288                 && !fill
13289                 && intsize != 'q'
13290             ) {
13291                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13292                     SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
13293                 );
13294                 elen = strlen(ebuf);
13295                 eptr = ebuf;
13296                 goto float_concat;
13297             }
13298
13299
13300             {
13301                 STRLEN pr = has_precis ? precis : 6; /* known default */
13302                 /* this probably can't wrap, since precis is limited
13303                  * to 1/4 address space size, but better safe than sorry
13304                  */
13305                 if (float_need >= ((STRLEN)~0) - pr)
13306                     croak_memory_wrap();
13307                 float_need += pr;
13308             }
13309
13310             if (float_need < width)
13311                 float_need = width;
13312
13313             if (float_need > INT_MAX) {
13314                 /* snprintf() returns an int, and we use that return value,
13315                    so die horribly if the expected size is too large for int
13316                 */
13317                 Perl_croak(aTHX_ "Numeric format result too large");
13318             }
13319
13320             if (PL_efloatsize <= float_need) {
13321                 /* PL_efloatbuf should be at least 1 greater than
13322                  * float_need to allow a trailing \0 to be returned by
13323                  * snprintf().  If we need to grow, overgrow for the
13324                  * benefit of future generations */
13325                 const STRLEN extra = 0x20;
13326                 if (float_need >= ((STRLEN)~0) - extra)
13327                     croak_memory_wrap();
13328                 float_need += extra;
13329                 Safefree(PL_efloatbuf);
13330                 PL_efloatsize = float_need;
13331                 Newx(PL_efloatbuf, PL_efloatsize, char);
13332                 PL_efloatbuf[0] = '\0';
13333             }
13334
13335             if (UNLIKELY(hexfp)) {
13336                 elen = S_format_hexfp(aTHX_ PL_efloatbuf, PL_efloatsize, c,
13337                                 nv, fv, has_precis, precis, width,
13338                                 alt, plus, left, fill, in_lc_numeric);
13339             }
13340             else {
13341                 char *ptr = ebuf + sizeof ebuf;
13342                 *--ptr = '\0';
13343                 *--ptr = c;
13344 #if defined(USE_QUADMATH)
13345                 /* always use Q here.  my_snprint() throws an exception if we
13346                    fallthrough to the double/long double code, even when the
13347                    format is correct, presumably to avoid any accidentally
13348                    missing Q.
13349                 */
13350                 *--ptr = 'Q';
13351                 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
13352 #elif defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
13353                 /* Note that this is HAS_LONG_DOUBLE and PERL_PRIfldbl,
13354                  * not USE_LONG_DOUBLE and NVff.  In other words,
13355                  * this needs to work without USE_LONG_DOUBLE. */
13356                 if (intsize == 'q') {
13357                     /* Copy the one or more characters in a long double
13358                      * format before the 'base' ([efgEFG]) character to
13359                      * the format string. */
13360                     static char const ldblf[] = PERL_PRIfldbl;
13361                     char const *p = ldblf + sizeof(ldblf) - 3;
13362                     while (p >= ldblf) { *--ptr = *p--; }
13363                 }
13364 #endif
13365                 if (has_precis) {
13366                     base = precis;
13367                     do { *--ptr = '0' + (base % 10); } while (base /= 10);
13368                     *--ptr = '.';
13369                 }
13370                 if (width) {
13371                     base = width;
13372                     do { *--ptr = '0' + (base % 10); } while (base /= 10);
13373                 }
13374                 if (fill)
13375                     *--ptr = '0';
13376                 if (left)
13377                     *--ptr = '-';
13378                 if (plus)
13379                     *--ptr = plus;
13380                 if (alt)
13381                     *--ptr = '#';
13382                 *--ptr = '%';
13383
13384                 /* No taint.  Otherwise we are in the strange situation
13385                  * where printf() taints but print($float) doesn't.
13386                  * --jhi */
13387
13388                 /* hopefully the above makes ptr a very constrained format
13389                  * that is safe to use, even though it's not literal */
13390                 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
13391 #ifdef USE_QUADMATH
13392                 {
13393                     if (!quadmath_format_valid(ptr))
13394                         Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
13395                     WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13396                         elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
13397                                                  ptr, nv);
13398                     );
13399                     if ((IV)elen == -1) {
13400                         Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", ptr);
13401                     }
13402                 }
13403 #elif defined(HAS_LONG_DOUBLE)
13404                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13405                     elen = ((intsize == 'q')
13406                             ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13407                             : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv))
13408                 );
13409 #else
13410                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13411                     elen = my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13412                 );
13413 #endif
13414                 GCC_DIAG_RESTORE_STMT;
13415             }
13416
13417             eptr = PL_efloatbuf;
13418
13419           float_concat:
13420
13421             /* Since floating-point formats do their own formatting and
13422              * padding, we skip the main block of code at the end of this
13423              * loop which handles appending eptr to sv, and do our own
13424              * stripped-down version */
13425
13426             assert(!zeros);
13427             assert(!esignlen);
13428             assert(elen);
13429             assert(elen >= width);
13430
13431             S_sv_catpvn_simple(aTHX_ sv, eptr, elen);
13432
13433             goto done_valid_conversion;
13434         }
13435
13436             /* SPECIAL */
13437
13438         case 'n':
13439             {
13440                 STRLEN len;
13441                 /* XXX ideally we should warn if any flags etc have been
13442                  * set, e.g. "%-4.5n" */
13443                 /* XXX if sv was originally non-utf8 with a char in the
13444                  * range 0x80-0xff, then if it got upgraded, we should
13445                  * calculate char len rather than byte len here */
13446                 len = SvCUR(sv) - origlen;
13447                 if (args) {
13448                     int i = (len > PERL_INT_MAX) ? PERL_INT_MAX : (int)len;
13449
13450                     switch (intsize) {
13451                     case 'c':  *(va_arg(*args, char*))      = i; break;
13452                     case 'h':  *(va_arg(*args, short*))     = i; break;
13453                     default:   *(va_arg(*args, int*))       = i; break;
13454                     case 'l':  *(va_arg(*args, long*))      = i; break;
13455                     case 'V':  *(va_arg(*args, IV*))        = i; break;
13456                     case 'z':  *(va_arg(*args, SSize_t*))   = i; break;
13457 #ifdef HAS_PTRDIFF_T
13458                     case 't':  *(va_arg(*args, ptrdiff_t*)) = i; break;
13459 #endif
13460                     case 'j':  *(va_arg(*args, PERL_INTMAX_T*)) = i; break;
13461                     case 'q':
13462 #if IVSIZE >= 8
13463                                *(va_arg(*args, Quad_t*))    = i; break;
13464 #else
13465                                goto unknown;
13466 #endif
13467                     }
13468                 }
13469                 else {
13470                     if (arg_missing)
13471                         Perl_croak_nocontext(
13472                             "Missing argument for %%n in %s",
13473                                 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13474                     sv_setuv_mg(argsv, has_utf8
13475                         ? (UV)utf8_length((U8*)SvPVX(sv), (U8*)SvEND(sv))
13476                         : (UV)len);
13477                 }
13478                 goto done_valid_conversion;
13479             }
13480
13481             /* UNKNOWN */
13482
13483         default:
13484       unknown:
13485             if (!args
13486                 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
13487                 && ckWARN(WARN_PRINTF))
13488             {
13489                 SV * const msg = sv_newmortal();
13490                 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
13491                           (PL_op->op_type == OP_PRTF) ? "" : "s");
13492                 if (fmtstart < patend) {
13493                     const char * const fmtend = q < patend ? q : patend;
13494                     const char * f;
13495                     sv_catpvs(msg, "\"%");
13496                     for (f = fmtstart; f < fmtend; f++) {
13497                         if (isPRINT(*f)) {
13498                             sv_catpvn_nomg(msg, f, 1);
13499                         } else {
13500                             Perl_sv_catpvf(aTHX_ msg, "\\%03o", (U8) *f);
13501                         }
13502                     }
13503                     sv_catpvs(msg, "\"");
13504                 } else {
13505                     sv_catpvs(msg, "end of string");
13506                 }
13507                 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%" SVf, SVfARG(msg)); /* yes, this is reentrant */
13508             }
13509
13510             /* mangled format: output the '%', then continue from the
13511              * character following that */
13512             sv_catpvn_nomg(sv, fmtstart-1, 1);
13513             q = fmtstart;
13514             svix = osvix;
13515             /* Any "redundant arg" warning from now onwards will probably
13516              * just be misleading, so don't bother. */
13517             no_redundant_warning = TRUE;
13518             continue;   /* not "break" */
13519         }
13520
13521         if (is_utf8 != has_utf8) {
13522             if (is_utf8) {
13523                 if (SvCUR(sv))
13524                     sv_utf8_upgrade(sv);
13525             }
13526             else {
13527                 const STRLEN old_elen = elen;
13528                 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
13529                 sv_utf8_upgrade(nsv);
13530                 eptr = SvPVX_const(nsv);
13531                 elen = SvCUR(nsv);
13532
13533                 if (width) { /* fudge width (can't fudge elen) */
13534                     width += elen - old_elen;
13535                 }
13536                 is_utf8 = TRUE;
13537             }
13538         }
13539
13540
13541         /* append esignbuf, filler, zeros, eptr and dotstr to sv */
13542
13543         {
13544             STRLEN need, have, gap;
13545             STRLEN i;
13546             char *s;
13547
13548             /* signed value that's wrapped? */
13549             assert(elen  <= ((~(STRLEN)0) >> 1));
13550
13551             /* if zeros is non-zero, then it represents filler between
13552              * elen and precis. So adding elen and zeros together will
13553              * always be <= precis, and the addition can never wrap */
13554             assert(!zeros || (precis > elen && precis - elen == zeros));
13555             have = elen + zeros;
13556
13557             if (have >= (((STRLEN)~0) - esignlen))
13558                 croak_memory_wrap();
13559             have += esignlen;
13560
13561             need = (have > width ? have : width);
13562             gap = need - have;
13563
13564             if (need >= (((STRLEN)~0) - (SvCUR(sv) + 1)))
13565                 croak_memory_wrap();
13566             need += (SvCUR(sv) + 1);
13567
13568             SvGROW(sv, need);
13569
13570             s = SvEND(sv);
13571
13572             if (left) {
13573                 for (i = 0; i < esignlen; i++)
13574                     *s++ = esignbuf[i];
13575                 for (i = zeros; i; i--)
13576                     *s++ = '0';
13577                 Copy(eptr, s, elen, char);
13578                 s += elen;
13579                 for (i = gap; i; i--)
13580                     *s++ = ' ';
13581             }
13582             else {
13583                 if (fill) {
13584                     for (i = 0; i < esignlen; i++)
13585                         *s++ = esignbuf[i];
13586                     assert(!zeros);
13587                     zeros = gap;
13588                 }
13589                 else {
13590                     for (i = gap; i; i--)
13591                         *s++ = ' ';
13592                     for (i = 0; i < esignlen; i++)
13593                         *s++ = esignbuf[i];
13594                 }
13595
13596                 for (i = zeros; i; i--)
13597                     *s++ = '0';
13598                 Copy(eptr, s, elen, char);
13599                 s += elen;
13600             }
13601
13602             *s = '\0';
13603             SvCUR_set(sv, s - SvPVX_const(sv));
13604
13605             if (is_utf8)
13606                 has_utf8 = TRUE;
13607             if (has_utf8)
13608                 SvUTF8_on(sv);
13609         }
13610
13611         if (vectorize && veclen) {
13612             /* we append the vector separator separately since %v isn't
13613              * very common: don't slow down the general case by adding
13614              * dotstrlen to need etc */
13615             sv_catpvn_nomg(sv, dotstr, dotstrlen);
13616             esignlen = 0;
13617             goto vector; /* do next iteration */
13618         }
13619
13620       done_valid_conversion:
13621
13622         if (arg_missing)
13623             S_warn_vcatpvfn_missing_argument(aTHX);
13624     }
13625
13626     /* Now that we've consumed all our printf format arguments (svix)
13627      * do we have things left on the stack that we didn't use?
13628      */
13629     if (!no_redundant_warning && sv_count >= svix + 1 && ckWARN(WARN_REDUNDANT)) {
13630         Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
13631                 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13632     }
13633
13634     if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
13635         /* while we shouldn't set the cache, it may have been previously
13636            set in the caller, so clear it */
13637         MAGIC *mg = mg_find(sv, PERL_MAGIC_utf8);
13638         if (mg)
13639             magic_setutf8(sv,mg); /* clear UTF8 cache */
13640     }
13641     SvTAINT(sv);
13642 }
13643
13644 /* =========================================================================
13645
13646 =for apidoc_section $embedding
13647
13648 =cut
13649
13650 All the macros and functions in this section are for the private use of
13651 the main function, perl_clone().
13652
13653 The foo_dup() functions make an exact copy of an existing foo thingy.
13654 During the course of a cloning, a hash table is used to map old addresses
13655 to new addresses.  The table is created and manipulated with the
13656 ptr_table_* functions.
13657
13658  * =========================================================================*/
13659
13660
13661 #if defined(USE_ITHREADS)
13662
13663 /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
13664 #ifndef GpREFCNT_inc
13665 #  define GpREFCNT_inc(gp)      ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
13666 #endif
13667
13668
13669 /* Certain cases in Perl_ss_dup have been merged, by relying on the fact
13670    that currently av_dup, gv_dup and hv_dup are the same as sv_dup.
13671    If this changes, please unmerge ss_dup.
13672    Likewise, sv_dup_inc_multiple() relies on this fact.  */
13673 #define sv_dup_inc_NN(s,t)      SvREFCNT_inc_NN(sv_dup_inc(s,t))
13674 #define av_dup(s,t)     MUTABLE_AV(sv_dup((const SV *)s,t))
13675 #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
13676 #define hv_dup(s,t)     MUTABLE_HV(sv_dup((const SV *)s,t))
13677 #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
13678 #define cv_dup(s,t)     MUTABLE_CV(sv_dup((const SV *)s,t))
13679 #define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t))
13680 #define io_dup(s,t)     MUTABLE_IO(sv_dup((const SV *)s,t))
13681 #define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t))
13682 #define gv_dup(s,t)     MUTABLE_GV(sv_dup((const SV *)s,t))
13683 #define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t))
13684 #define SAVEPV(p)       ((p) ? savepv(p) : NULL)
13685 #define SAVEPVN(p,n)    ((p) ? savepvn(p,n) : NULL)
13686
13687 /* clone a parser */
13688
13689 yy_parser *
13690 Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
13691 {
13692     yy_parser *parser;
13693
13694     PERL_ARGS_ASSERT_PARSER_DUP;
13695
13696     if (!proto)
13697         return NULL;
13698
13699     /* look for it in the table first */
13700     parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
13701     if (parser)
13702         return parser;
13703
13704     /* create anew and remember what it is */
13705     Newxz(parser, 1, yy_parser);
13706     ptr_table_store(PL_ptr_table, proto, parser);
13707
13708     /* XXX eventually, just Copy() most of the parser struct ? */
13709
13710     parser->lex_brackets = proto->lex_brackets;
13711     parser->lex_casemods = proto->lex_casemods;
13712     parser->lex_brackstack = savepvn(proto->lex_brackstack,
13713                     (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
13714     parser->lex_casestack = savepvn(proto->lex_casestack,
13715                     (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
13716     parser->lex_defer   = proto->lex_defer;
13717     parser->lex_dojoin  = proto->lex_dojoin;
13718     parser->lex_formbrack = proto->lex_formbrack;
13719     parser->lex_inpat   = proto->lex_inpat;
13720     parser->lex_inwhat  = proto->lex_inwhat;
13721     parser->lex_op      = proto->lex_op;
13722     parser->lex_repl    = sv_dup_inc(proto->lex_repl, param);
13723     parser->lex_starts  = proto->lex_starts;
13724     parser->lex_stuff   = sv_dup_inc(proto->lex_stuff, param);
13725     parser->multi_close = proto->multi_close;
13726     parser->multi_open  = proto->multi_open;
13727     parser->multi_start = proto->multi_start;
13728     parser->multi_end   = proto->multi_end;
13729     parser->preambled   = proto->preambled;
13730     parser->lex_super_state = proto->lex_super_state;
13731     parser->lex_sub_inwhat  = proto->lex_sub_inwhat;
13732     parser->lex_sub_op  = proto->lex_sub_op;
13733     parser->lex_sub_repl= sv_dup_inc(proto->lex_sub_repl, param);
13734     parser->linestr     = sv_dup_inc(proto->linestr, param);
13735     parser->expect      = proto->expect;
13736     parser->copline     = proto->copline;
13737     parser->last_lop_op = proto->last_lop_op;
13738     parser->lex_state   = proto->lex_state;
13739     parser->rsfp        = fp_dup(proto->rsfp, '<', param);
13740     /* rsfp_filters entries have fake IoDIRP() */
13741     parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
13742     parser->in_my       = proto->in_my;
13743     parser->in_my_stash = hv_dup(proto->in_my_stash, param);
13744     parser->error_count = proto->error_count;
13745     parser->sig_elems   = proto->sig_elems;
13746     parser->sig_optelems= proto->sig_optelems;
13747     parser->sig_slurpy  = proto->sig_slurpy;
13748     parser->recheck_utf8_validity = proto->recheck_utf8_validity;
13749
13750     {
13751         char * const ols = SvPVX(proto->linestr);
13752         char * const ls  = SvPVX(parser->linestr);
13753
13754         parser->bufptr      = ls + (proto->bufptr >= ols ?
13755                                     proto->bufptr -  ols : 0);
13756         parser->oldbufptr   = ls + (proto->oldbufptr >= ols ?
13757                                     proto->oldbufptr -  ols : 0);
13758         parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
13759                                     proto->oldoldbufptr -  ols : 0);
13760         parser->linestart   = ls + (proto->linestart >= ols ?
13761                                     proto->linestart -  ols : 0);
13762         parser->last_uni    = ls + (proto->last_uni >= ols ?
13763                                     proto->last_uni -  ols : 0);
13764         parser->last_lop    = ls + (proto->last_lop >= ols ?
13765                                     proto->last_lop -  ols : 0);
13766
13767         parser->bufend      = ls + SvCUR(parser->linestr);
13768     }
13769
13770     Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
13771
13772
13773     Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
13774     Copy(proto->nexttype, parser->nexttype, 5,  I32);
13775     parser->nexttoke    = proto->nexttoke;
13776
13777     /* XXX should clone saved_curcop here, but we aren't passed
13778      * proto_perl; so do it in perl_clone_using instead */
13779
13780     return parser;
13781 }
13782
13783 /*
13784 =for apidoc_section $io
13785 =for apidoc fp_dup
13786
13787 Duplicate a file handle, returning a pointer to the cloned object.
13788
13789 =cut
13790 */
13791
13792 PerlIO *
13793 Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
13794 {
13795     PerlIO *ret;
13796
13797     PERL_ARGS_ASSERT_FP_DUP;
13798     PERL_UNUSED_ARG(type);
13799
13800     if (!fp)
13801         return (PerlIO*)NULL;
13802
13803     /* look for it in the table first */
13804     ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
13805     if (ret)
13806         return ret;
13807
13808     /* create anew and remember what it is */
13809 #ifdef __amigaos4__
13810     ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE|PERLIO_DUP_FD);
13811 #else
13812     ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
13813 #endif
13814     ptr_table_store(PL_ptr_table, fp, ret);
13815     return ret;
13816 }
13817
13818 /*
13819 =for apidoc_section $io
13820 =for apidoc dirp_dup
13821
13822 Duplicate a directory handle, returning a pointer to the cloned object.
13823
13824 =cut
13825 */
13826
13827 DIR *
13828 Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
13829 {
13830     DIR *ret;
13831
13832 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13833     DIR *pwd;
13834     const Direntry_t *dirent;
13835     char smallbuf[256]; /* XXX MAXPATHLEN, surely? */
13836     char *name = NULL;
13837     STRLEN len = 0;
13838     long pos;
13839 #endif
13840
13841     PERL_UNUSED_CONTEXT;
13842     PERL_ARGS_ASSERT_DIRP_DUP;
13843
13844     if (!dp)
13845         return (DIR*)NULL;
13846
13847     /* look for it in the table first */
13848     ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp);
13849     if (ret)
13850         return ret;
13851
13852 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13853
13854     PERL_UNUSED_ARG(param);
13855
13856     /* create anew */
13857
13858     /* open the current directory (so we can switch back) */
13859     if (!(pwd = PerlDir_open("."))) return (DIR *)NULL;
13860
13861     /* chdir to our dir handle and open the present working directory */
13862     if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) {
13863         PerlDir_close(pwd);
13864         return (DIR *)NULL;
13865     }
13866     /* Now we should have two dir handles pointing to the same dir. */
13867
13868     /* Be nice to the calling code and chdir back to where we were. */
13869     /* XXX If this fails, then what? */
13870     PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
13871
13872     /* We have no need of the pwd handle any more. */
13873     PerlDir_close(pwd);
13874
13875 #ifdef DIRNAMLEN
13876 # define d_namlen(d) (d)->d_namlen
13877 #else
13878 # define d_namlen(d) strlen((d)->d_name)
13879 #endif
13880     /* Iterate once through dp, to get the file name at the current posi-
13881        tion. Then step back. */
13882     pos = PerlDir_tell(dp);
13883     if ((dirent = PerlDir_read(dp))) {
13884         len = d_namlen(dirent);
13885         if (len > sizeof(dirent->d_name) && sizeof(dirent->d_name) > PTRSIZE) {
13886             /* If the len is somehow magically longer than the
13887              * maximum length of the directory entry, even though
13888              * we could fit it in a buffer, we could not copy it
13889              * from the dirent.  Bail out. */
13890             PerlDir_close(ret);
13891             return (DIR*)NULL;
13892         }
13893         if (len <= sizeof smallbuf) name = smallbuf;
13894         else Newx(name, len, char);
13895         Move(dirent->d_name, name, len, char);
13896     }
13897     PerlDir_seek(dp, pos);
13898
13899     /* Iterate through the new dir handle, till we find a file with the
13900        right name. */
13901     if (!dirent) /* just before the end */
13902         for(;;) {
13903             pos = PerlDir_tell(ret);
13904             if (PerlDir_read(ret)) continue; /* not there yet */
13905             PerlDir_seek(ret, pos); /* step back */
13906             break;
13907         }
13908     else {
13909         const long pos0 = PerlDir_tell(ret);
13910         for(;;) {
13911             pos = PerlDir_tell(ret);
13912             if ((dirent = PerlDir_read(ret))) {
13913                 if (len == (STRLEN)d_namlen(dirent)
13914                     && memEQ(name, dirent->d_name, len)) {
13915                     /* found it */
13916                     PerlDir_seek(ret, pos); /* step back */
13917                     break;
13918                 }
13919                 /* else we are not there yet; keep iterating */
13920             }
13921             else { /* This is not meant to happen. The best we can do is
13922                       reset the iterator to the beginning. */
13923                 PerlDir_seek(ret, pos0);
13924                 break;
13925             }
13926         }
13927     }
13928 #undef d_namlen
13929
13930     if (name && name != smallbuf)
13931         Safefree(name);
13932 #endif
13933
13934 #ifdef WIN32
13935     ret = win32_dirp_dup(dp, param);
13936 #endif
13937
13938     /* pop it in the pointer table */
13939     if (ret)
13940         ptr_table_store(PL_ptr_table, dp, ret);
13941
13942     return ret;
13943 }
13944
13945 /*
13946 =for apidoc_section $GV
13947 =for apidoc gp_dup
13948
13949 Duplicate a typeglob, returning a pointer to the cloned object.
13950
13951 =cut
13952 */
13953
13954 GP *
13955 Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
13956 {
13957     GP *ret;
13958
13959     PERL_ARGS_ASSERT_GP_DUP;
13960
13961     if (!gp)
13962         return (GP*)NULL;
13963     /* look for it in the table first */
13964     ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
13965     if (ret)
13966         return ret;
13967
13968     /* create anew and remember what it is */
13969     Newxz(ret, 1, GP);
13970     ptr_table_store(PL_ptr_table, gp, ret);
13971
13972     /* clone */
13973     /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
13974        on Newxz() to do this for us.  */
13975     ret->gp_sv          = sv_dup_inc(gp->gp_sv, param);
13976     ret->gp_io          = io_dup_inc(gp->gp_io, param);
13977     ret->gp_form        = cv_dup_inc(gp->gp_form, param);
13978     ret->gp_av          = av_dup_inc(gp->gp_av, param);
13979     ret->gp_hv          = hv_dup_inc(gp->gp_hv, param);
13980     ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
13981     ret->gp_cv          = cv_dup_inc(gp->gp_cv, param);
13982     ret->gp_cvgen       = gp->gp_cvgen;
13983     ret->gp_line        = gp->gp_line;
13984     ret->gp_file_hek    = hek_dup(gp->gp_file_hek, param);
13985     return ret;
13986 }
13987
13988
13989 /*
13990 =for apidoc_section $magic
13991 =for apidoc mg_dup
13992
13993 Duplicate a chain of magic, returning a pointer to the cloned object.
13994
13995 =cut
13996 */
13997
13998 MAGIC *
13999 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
14000 {
14001     MAGIC *mgret = NULL;
14002     MAGIC **mgprev_p = &mgret;
14003
14004     PERL_ARGS_ASSERT_MG_DUP;
14005
14006     for (; mg; mg = mg->mg_moremagic) {
14007         MAGIC *nmg;
14008
14009         if ((param->flags & CLONEf_JOIN_IN)
14010                 && mg->mg_type == PERL_MAGIC_backref)
14011             /* when joining, we let the individual SVs add themselves to
14012              * backref as needed. */
14013             continue;
14014
14015         Newx(nmg, 1, MAGIC);
14016         *mgprev_p = nmg;
14017         mgprev_p = &(nmg->mg_moremagic);
14018
14019         /* There was a comment "XXX copy dynamic vtable?" but as we don't have
14020            dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
14021            from the original commit adding Perl_mg_dup() - revision 4538.
14022            Similarly there is the annotation "XXX random ptr?" next to the
14023            assignment to nmg->mg_ptr.  */
14024         *nmg = *mg;
14025
14026         /* FIXME for plugins
14027         if (nmg->mg_type == PERL_MAGIC_qr) {
14028             nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
14029         }
14030         else
14031         */
14032         nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
14033                           ? nmg->mg_type == PERL_MAGIC_backref
14034                                 /* The backref AV has its reference
14035                                  * count deliberately bumped by 1 */
14036                                 ? SvREFCNT_inc(av_dup_inc((const AV *)
14037                                                     nmg->mg_obj, param))
14038                                 : sv_dup_inc(nmg->mg_obj, param)
14039                           : (nmg->mg_type == PERL_MAGIC_regdatum ||
14040                              nmg->mg_type == PERL_MAGIC_regdata)
14041                                   ? nmg->mg_obj
14042                                   : sv_dup(nmg->mg_obj, param);
14043
14044         if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
14045             if (nmg->mg_len > 0) {
14046                 nmg->mg_ptr     = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
14047                 if (nmg->mg_type == PERL_MAGIC_overload_table &&
14048                         AMT_AMAGIC((AMT*)nmg->mg_ptr))
14049                 {
14050                     AMT * const namtp = (AMT*)nmg->mg_ptr;
14051                     sv_dup_inc_multiple((SV**)(namtp->table),
14052                                         (SV**)(namtp->table), NofAMmeth, param);
14053                 }
14054             }
14055             else if (nmg->mg_len == HEf_SVKEY)
14056                 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
14057         }
14058         if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
14059             nmg->mg_virtual->svt_dup(aTHX_ nmg, param);
14060         }
14061     }
14062     return mgret;
14063 }
14064
14065 #endif /* USE_ITHREADS */
14066
14067 struct ptr_tbl_arena {
14068     struct ptr_tbl_arena *next;
14069     struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers.  */
14070 };
14071
14072 /*
14073 =for apidoc ptr_table_new
14074
14075 Create a new pointer-mapping table
14076
14077 =cut
14078 */
14079
14080 PTR_TBL_t *
14081 Perl_ptr_table_new(pTHX)
14082 {
14083     PTR_TBL_t *tbl;
14084     PERL_UNUSED_CONTEXT;
14085
14086     Newx(tbl, 1, PTR_TBL_t);
14087     tbl->tbl_max        = 511;
14088     tbl->tbl_items      = 0;
14089     tbl->tbl_arena      = NULL;
14090     tbl->tbl_arena_next = NULL;
14091     tbl->tbl_arena_end  = NULL;
14092     Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
14093     return tbl;
14094 }
14095
14096 #define PTR_TABLE_HASH(ptr) \
14097   ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
14098
14099 /* map an existing pointer using a table */
14100
14101 STATIC PTR_TBL_ENT_t *
14102 S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
14103 {
14104     PTR_TBL_ENT_t *tblent;
14105     const UV hash = PTR_TABLE_HASH(sv);
14106
14107     PERL_ARGS_ASSERT_PTR_TABLE_FIND;
14108
14109     tblent = tbl->tbl_ary[hash & tbl->tbl_max];
14110     for (; tblent; tblent = tblent->next) {
14111         if (tblent->oldval == sv)
14112             return tblent;
14113     }
14114     return NULL;
14115 }
14116
14117 /*
14118 =for apidoc ptr_table_fetch
14119
14120 Look for C<sv> in the pointer-mapping table C<tbl>, returning its value, or
14121 NULL if not found.
14122
14123 =cut
14124 */
14125
14126 void *
14127 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
14128 {
14129     PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
14130
14131     PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
14132     PERL_UNUSED_CONTEXT;
14133
14134     return tblent ? tblent->newval : NULL;
14135 }
14136
14137 /*
14138 =for apidoc ptr_table_store
14139
14140 Add a new entry to a pointer-mapping table C<tbl>.
14141 In hash terms, C<oldsv> is the key; Cnewsv> is the value.
14142
14143 The names "old" and "new" are specific to the core's typical use of ptr_tables
14144 in thread cloning.
14145
14146 =cut
14147 */
14148
14149 void
14150 Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
14151 {
14152     PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
14153
14154     PERL_ARGS_ASSERT_PTR_TABLE_STORE;
14155     PERL_UNUSED_CONTEXT;
14156
14157     if (tblent) {
14158         tblent->newval = newsv;
14159     } else {
14160         const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
14161
14162         if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
14163             struct ptr_tbl_arena *new_arena;
14164
14165             Newx(new_arena, 1, struct ptr_tbl_arena);
14166             new_arena->next = tbl->tbl_arena;
14167             tbl->tbl_arena = new_arena;
14168             tbl->tbl_arena_next = new_arena->array;
14169             tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
14170         }
14171
14172         tblent = tbl->tbl_arena_next++;
14173
14174         tblent->oldval = oldsv;
14175         tblent->newval = newsv;
14176         tblent->next = tbl->tbl_ary[entry];
14177         tbl->tbl_ary[entry] = tblent;
14178         tbl->tbl_items++;
14179         if (tblent->next && tbl->tbl_items > tbl->tbl_max)
14180             ptr_table_split(tbl);
14181     }
14182 }
14183
14184 /*
14185 =for apidoc ptr_table_split
14186
14187 Double the hash bucket size of an existing ptr table
14188
14189 =cut
14190 */
14191
14192 void
14193 Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
14194 {
14195     PTR_TBL_ENT_t **ary = tbl->tbl_ary;
14196     const UV oldsize = tbl->tbl_max + 1;
14197     UV newsize = oldsize * 2;
14198     UV i;
14199
14200     PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
14201     PERL_UNUSED_CONTEXT;
14202
14203     Renew(ary, newsize, PTR_TBL_ENT_t*);
14204     Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
14205     tbl->tbl_max = --newsize;
14206     tbl->tbl_ary = ary;
14207     for (i=0; i < oldsize; i++, ary++) {
14208         PTR_TBL_ENT_t **entp = ary;
14209         PTR_TBL_ENT_t *ent = *ary;
14210         PTR_TBL_ENT_t **curentp;
14211         if (!ent)
14212             continue;
14213         curentp = ary + oldsize;
14214         do {
14215             if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
14216                 *entp = ent->next;
14217                 ent->next = *curentp;
14218                 *curentp = ent;
14219             }
14220             else
14221                 entp = &ent->next;
14222             ent = *entp;
14223         } while (ent);
14224     }
14225 }
14226
14227 /*
14228 =for apidoc ptr_table_free
14229
14230 Clear and free a ptr table
14231
14232 =cut
14233 */
14234
14235 void
14236 Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
14237 {
14238     struct ptr_tbl_arena *arena;
14239
14240     PERL_UNUSED_CONTEXT;
14241
14242     if (!tbl) {
14243         return;
14244     }
14245
14246     arena = tbl->tbl_arena;
14247
14248     while (arena) {
14249         struct ptr_tbl_arena *next = arena->next;
14250
14251         Safefree(arena);
14252         arena = next;
14253     }
14254
14255     Safefree(tbl->tbl_ary);
14256     Safefree(tbl);
14257 }
14258
14259 #if defined(USE_ITHREADS)
14260
14261 void
14262 Perl_rvpv_dup(pTHX_ SV *const dsv, const SV *const ssv, CLONE_PARAMS *const param)
14263 {
14264     PERL_ARGS_ASSERT_RVPV_DUP;
14265
14266     assert(!isREGEXP(ssv));
14267     if (SvROK(ssv)) {
14268         if (SvWEAKREF(ssv)) {
14269             SvRV_set(dsv, sv_dup(SvRV_const(ssv), param));
14270             if (param->flags & CLONEf_JOIN_IN) {
14271                 /* if joining, we add any back references individually rather
14272                  * than copying the whole backref array */
14273                 Perl_sv_add_backref(aTHX_ SvRV(dsv), dsv);
14274             }
14275         }
14276         else
14277             SvRV_set(dsv, sv_dup_inc(SvRV_const(ssv), param));
14278     }
14279     else if (SvPVX_const(ssv)) {
14280         /* Has something there */
14281         if (SvLEN(ssv)) {
14282             /* Normal PV - clone whole allocated space */
14283             SvPV_set(dsv, SAVEPVN(SvPVX_const(ssv), SvLEN(ssv)-1));
14284             /* ssv may not be that normal, but actually copy on write.
14285                But we are a true, independent SV, so:  */
14286             SvIsCOW_off(dsv);
14287         }
14288         else {
14289             /* Special case - not normally malloced for some reason */
14290             if (isGV_with_GP(ssv)) {
14291                 /* Don't need to do anything here.  */
14292             }
14293             else if ((SvIsCOW_shared_hash(ssv))) {
14294                 /* A "shared" PV - clone it as "shared" PV */
14295                 SvPV_set(dsv,
14296                          HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)),
14297                                          param)));
14298             }
14299             else {
14300                 /* Some other special case - random pointer */
14301                 SvPV_set(dsv, (char *) SvPVX_const(ssv));
14302             }
14303         }
14304     }
14305     else {
14306         /* Copy the NULL */
14307         SvPV_set(dsv, NULL);
14308     }
14309 }
14310
14311 /* duplicate a list of SVs. source and dest may point to the same memory.  */
14312 static SV **
14313 S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
14314                       SSize_t items, CLONE_PARAMS *const param)
14315 {
14316     PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
14317
14318     while (items-- > 0) {
14319         *dest++ = sv_dup_inc(*source++, param);
14320     }
14321
14322     return dest;
14323 }
14324
14325 /* duplicate an SV of any type (including AV, HV etc) */
14326
14327 static SV *
14328 S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14329 {
14330     SV *dsv;
14331
14332     PERL_ARGS_ASSERT_SV_DUP_COMMON;
14333
14334     if (SvTYPE(ssv) == (svtype)SVTYPEMASK) {
14335 #ifdef DEBUG_LEAKING_SCALARS_ABORT
14336         abort();
14337 #endif
14338         return NULL;
14339     }
14340     /* look for it in the table first */
14341     dsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, ssv));
14342     if (dsv)
14343         return dsv;
14344
14345     if(param->flags & CLONEf_JOIN_IN) {
14346         /** We are joining here so we don't want do clone
14347             something that is bad **/
14348         if (SvTYPE(ssv) == SVt_PVHV) {
14349             const HEK * const hvname = HvNAME_HEK(ssv);
14350             if (hvname) {
14351                 /** don't clone stashes if they already exist **/
14352                 dsv = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14353                                                 HEK_UTF8(hvname) ? SVf_UTF8 : 0));
14354                 ptr_table_store(PL_ptr_table, ssv, dsv);
14355                 return dsv;
14356             }
14357         }
14358         else if (SvTYPE(ssv) == SVt_PVGV && !SvFAKE(ssv)) {
14359             HV *stash = GvSTASH(ssv);
14360             const HEK * hvname;
14361             if (stash && (hvname = HvNAME_HEK(stash))) {
14362                 /** don't clone GVs if they already exist **/
14363                 SV **svp;
14364                 stash = gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14365                                     HEK_UTF8(hvname) ? SVf_UTF8 : 0);
14366                 svp = hv_fetch(
14367                         stash, GvNAME(ssv),
14368                         GvNAMEUTF8(ssv)
14369                             ? -GvNAMELEN(ssv)
14370                             :  GvNAMELEN(ssv),
14371                         0
14372                       );
14373                 if (svp && *svp && SvTYPE(*svp) == SVt_PVGV) {
14374                     ptr_table_store(PL_ptr_table, ssv, *svp);
14375                     return *svp;
14376                 }
14377             }
14378         }
14379     }
14380
14381     /* create anew and remember what it is */
14382     new_SV(dsv);
14383
14384 #ifdef DEBUG_LEAKING_SCALARS
14385     dsv->sv_debug_optype = ssv->sv_debug_optype;
14386     dsv->sv_debug_line = ssv->sv_debug_line;
14387     dsv->sv_debug_inpad = ssv->sv_debug_inpad;
14388     dsv->sv_debug_parent = (SV*)ssv;
14389     FREE_SV_DEBUG_FILE(dsv);
14390     dsv->sv_debug_file = savesharedpv(ssv->sv_debug_file);
14391 #endif
14392
14393     ptr_table_store(PL_ptr_table, ssv, dsv);
14394
14395     /* clone */
14396     SvFLAGS(dsv)        = SvFLAGS(ssv);
14397     SvFLAGS(dsv)        &= ~SVf_OOK;            /* don't propagate OOK hack */
14398     SvREFCNT(dsv)       = 0;                    /* must be before any other dups! */
14399
14400 #ifdef DEBUGGING
14401     if (SvANY(ssv) && PL_watch_pvx && SvPVX_const(ssv) == PL_watch_pvx)
14402         PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
14403                       (void*)PL_watch_pvx, SvPVX_const(ssv));
14404 #endif
14405
14406     /* don't clone objects whose class has asked us not to */
14407     if (SvOBJECT(ssv)
14408      && ! (SvFLAGS(SvSTASH(ssv)) & SVphv_CLONEABLE))
14409     {
14410         SvFLAGS(dsv) = 0;
14411         return dsv;
14412     }
14413
14414     switch (SvTYPE(ssv)) {
14415     case SVt_NULL:
14416         SvANY(dsv)      = NULL;
14417         break;
14418     case SVt_IV:
14419         SET_SVANY_FOR_BODYLESS_IV(dsv);
14420         if(SvROK(ssv)) {
14421             Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14422         } else {
14423             SvIV_set(dsv, SvIVX(ssv));
14424         }
14425         break;
14426     case SVt_NV:
14427 #if NVSIZE <= IVSIZE
14428         SET_SVANY_FOR_BODYLESS_NV(dsv);
14429 #else
14430         SvANY(dsv)      = new_XNV();
14431 #endif
14432         SvNV_set(dsv, SvNVX(ssv));
14433         break;
14434     default:
14435         {
14436             /* These are all the types that need complex bodies allocating.  */
14437             void *new_body;
14438             const svtype sv_type = SvTYPE(ssv);
14439             const struct body_details *sv_type_details
14440                 = bodies_by_type + sv_type;
14441
14442             switch (sv_type) {
14443             default:
14444                 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(ssv));
14445                 NOT_REACHED; /* NOTREACHED */
14446                 break;
14447
14448             case SVt_PVHV:
14449                 if (HvHasAUX(ssv)) {
14450                     sv_type_details = &fake_hv_with_aux;
14451 #ifdef PURIFY
14452                     new_body = new_NOARENA(sv_type_details);
14453 #else
14454                     new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
14455 #endif
14456                     goto have_body;
14457                 }
14458                 /* FALLTHROUGH */
14459             case SVt_PVGV:
14460             case SVt_PVIO:
14461             case SVt_PVFM:
14462             case SVt_PVAV:
14463             case SVt_PVCV:
14464             case SVt_PVLV:
14465             case SVt_REGEXP:
14466             case SVt_PVMG:
14467             case SVt_PVNV:
14468             case SVt_PVIV:
14469             case SVt_INVLIST:
14470             case SVt_PV:
14471                 assert(sv_type_details->body_size);
14472 #ifndef PURIFY
14473                 if (sv_type_details->arena) {
14474                     new_body = S_new_body(aTHX_ sv_type);
14475                     new_body
14476                         = (void*)((char*)new_body - sv_type_details->offset);
14477                 } else
14478 #endif
14479                 {
14480                     new_body = new_NOARENA(sv_type_details);
14481                 }
14482             }
14483         have_body:
14484             assert(new_body);
14485             SvANY(dsv) = new_body;
14486
14487 #ifndef PURIFY
14488             Copy(((char*)SvANY(ssv)) + sv_type_details->offset,
14489                  ((char*)SvANY(dsv)) + sv_type_details->offset,
14490                  sv_type_details->copy, char);
14491 #else
14492             Copy(((char*)SvANY(ssv)),
14493                  ((char*)SvANY(dsv)),
14494                  sv_type_details->body_size + sv_type_details->offset, char);
14495 #endif
14496
14497             if (sv_type != SVt_PVAV && sv_type != SVt_PVHV
14498                 && !isGV_with_GP(dsv)
14499                 && !isREGEXP(dsv)
14500                 && !(sv_type == SVt_PVIO && !(IoFLAGS(dsv) & IOf_FAKE_DIRP)))
14501                 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14502
14503             /* The Copy above means that all the source (unduplicated) pointers
14504                are now in the destination.  We can check the flags and the
14505                pointers in either, but it's possible that there's less cache
14506                missing by always going for the destination.
14507                FIXME - instrument and check that assumption  */
14508             if (sv_type >= SVt_PVMG) {
14509                 if (SvMAGIC(dsv))
14510                     SvMAGIC_set(dsv, mg_dup(SvMAGIC(dsv), param));
14511                 if (SvOBJECT(dsv) && SvSTASH(dsv))
14512                     SvSTASH_set(dsv, hv_dup_inc(SvSTASH(dsv), param));
14513                 else SvSTASH_set(dsv, 0); /* don't copy DESTROY cache */
14514             }
14515
14516             /* The cast silences a GCC warning about unhandled types.  */
14517             switch ((int)sv_type) {
14518             case SVt_PV:
14519                 break;
14520             case SVt_PVIV:
14521                 break;
14522             case SVt_PVNV:
14523                 break;
14524             case SVt_PVMG:
14525                 break;
14526             case SVt_REGEXP:
14527               duprex:
14528                 /* FIXME for plugins */
14529                 re_dup_guts((REGEXP*) ssv, (REGEXP*) dsv, param);
14530                 break;
14531             case SVt_PVLV:
14532                 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
14533                 if (LvTYPE(dsv) == 't') /* for tie: unrefcnted fake (SV**) */
14534                     LvTARG(dsv) = dsv;
14535                 else if (LvTYPE(dsv) == 'T') /* for tie: fake HE */
14536                     LvTARG(dsv) = MUTABLE_SV(he_dup((HE*)LvTARG(dsv), FALSE, param));
14537                 else
14538                     LvTARG(dsv) = sv_dup_inc(LvTARG(dsv), param);
14539                 if (isREGEXP(ssv)) goto duprex;
14540                 /* FALLTHROUGH */
14541             case SVt_PVGV:
14542                 /* non-GP case already handled above */
14543                 if(isGV_with_GP(ssv)) {
14544                     GvNAME_HEK(dsv) = hek_dup(GvNAME_HEK(dsv), param);
14545                     /* Don't call sv_add_backref here as it's going to be
14546                        created as part of the magic cloning of the symbol
14547                        table--unless this is during a join and the stash
14548                        is not actually being cloned.  */
14549                     /* Danger Will Robinson - GvGP(dsv) isn't initialised
14550                        at the point of this comment.  */
14551                     GvSTASH(dsv) = hv_dup(GvSTASH(dsv), param);
14552                     if (param->flags & CLONEf_JOIN_IN)
14553                         Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
14554                     GvGP_set(dsv, gp_dup(GvGP(ssv), param));
14555                     (void)GpREFCNT_inc(GvGP(dsv));
14556                 }
14557                 break;
14558             case SVt_PVIO:
14559                 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
14560                 if(IoFLAGS(dsv) & IOf_FAKE_DIRP) {
14561                     /* I have no idea why fake dirp (rsfps)
14562                        should be treated differently but otherwise
14563                        we end up with leaks -- sky*/
14564                     IoTOP_GV(dsv)      = gv_dup_inc(IoTOP_GV(dsv), param);
14565                     IoFMT_GV(dsv)      = gv_dup_inc(IoFMT_GV(dsv), param);
14566                     IoBOTTOM_GV(dsv)   = gv_dup_inc(IoBOTTOM_GV(dsv), param);
14567                 } else {
14568                     IoTOP_GV(dsv)      = gv_dup(IoTOP_GV(dsv), param);
14569                     IoFMT_GV(dsv)      = gv_dup(IoFMT_GV(dsv), param);
14570                     IoBOTTOM_GV(dsv)   = gv_dup(IoBOTTOM_GV(dsv), param);
14571                     if (IoDIRP(dsv)) {
14572                         IoDIRP(dsv)     = dirp_dup(IoDIRP(dsv), param);
14573                     } else {
14574                         NOOP;
14575                         /* IoDIRP(dsv) is already a copy of IoDIRP(ssv)  */
14576                     }
14577                     IoIFP(dsv)  = fp_dup(IoIFP(ssv), IoTYPE(dsv), param);
14578                 }
14579                 if (IoOFP(dsv) == IoIFP(ssv))
14580                     IoOFP(dsv) = IoIFP(dsv);
14581                 else
14582                     IoOFP(dsv)  = fp_dup(IoOFP(dsv), IoTYPE(dsv), param);
14583                 IoTOP_NAME(dsv) = SAVEPV(IoTOP_NAME(dsv));
14584                 IoFMT_NAME(dsv) = SAVEPV(IoFMT_NAME(dsv));
14585                 IoBOTTOM_NAME(dsv)      = SAVEPV(IoBOTTOM_NAME(dsv));
14586                 break;
14587             case SVt_PVAV:
14588                 /* avoid cloning an empty array */
14589                 if (AvARRAY((const AV *)ssv) && AvFILLp((const AV *)ssv) >= 0) {
14590                     SV **dst_ary, **src_ary;
14591                     SSize_t items = AvFILLp((const AV *)ssv) + 1;
14592
14593                     src_ary = AvARRAY((const AV *)ssv);
14594                     Newx(dst_ary, AvMAX((const AV *)ssv)+1, SV*);
14595                     ptr_table_store(PL_ptr_table, src_ary, dst_ary);
14596                     AvARRAY(MUTABLE_AV(dsv)) = dst_ary;
14597                     AvALLOC((const AV *)dsv) = dst_ary;
14598                     if (AvREAL((const AV *)ssv)) {
14599                         dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
14600                                                       param);
14601                     }
14602                     else {
14603                         while (items-- > 0)
14604                             *dst_ary++ = sv_dup(*src_ary++, param);
14605                     }
14606                     items = AvMAX((const AV *)ssv) - AvFILLp((const AV *)ssv);
14607                     while (items-- > 0) {
14608                         *dst_ary++ = NULL;
14609                     }
14610                 }
14611                 else {
14612                     AvARRAY(MUTABLE_AV(dsv))    = NULL;
14613                     AvALLOC((const AV *)dsv)    = (SV**)NULL;
14614                     AvMAX(  (const AV *)dsv)    = -1;
14615                     AvFILLp((const AV *)dsv)    = -1;
14616                 }
14617                 break;
14618             case SVt_PVHV:
14619                 if (HvARRAY((const HV *)ssv)) {
14620                     STRLEN i = 0;
14621                     XPVHV * const dxhv = (XPVHV*)SvANY(dsv);
14622                     XPVHV * const sxhv = (XPVHV*)SvANY(ssv);
14623                     char *darray;
14624                     Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1),
14625                         char);
14626                     HvARRAY(dsv) = (HE**)darray;
14627                     while (i <= sxhv->xhv_max) {
14628                         const HE * const source = HvARRAY(ssv)[i];
14629                         HvARRAY(dsv)[i] = source
14630                             ? he_dup(source, FALSE, param) : 0;
14631                         ++i;
14632                     }
14633                     if (HvHasAUX(ssv)) {
14634                         const struct xpvhv_aux * const saux = HvAUX(ssv);
14635                         struct xpvhv_aux * const daux = HvAUX(dsv);
14636                         /* This flag isn't copied.  */
14637                         SvOOK_on(dsv);
14638
14639                         if (saux->xhv_name_count) {
14640                             HEK ** const sname = saux->xhv_name_u.xhvnameu_names;
14641                             const I32 count
14642                              = saux->xhv_name_count < 0
14643                                 ? -saux->xhv_name_count
14644                                 :  saux->xhv_name_count;
14645                             HEK **shekp = sname + count;
14646                             HEK **dhekp;
14647                             Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *);
14648                             dhekp = daux->xhv_name_u.xhvnameu_names + count;
14649                             while (shekp-- > sname) {
14650                                 dhekp--;
14651                                 *dhekp = hek_dup(*shekp, param);
14652                             }
14653                         }
14654                         else {
14655                             daux->xhv_name_u.xhvnameu_name
14656                                 = hek_dup(saux->xhv_name_u.xhvnameu_name,
14657                                           param);
14658                         }
14659                         daux->xhv_name_count = saux->xhv_name_count;
14660
14661                         daux->xhv_aux_flags = saux->xhv_aux_flags;
14662 #ifdef PERL_HASH_RANDOMIZE_KEYS
14663                         daux->xhv_rand = saux->xhv_rand;
14664                         daux->xhv_last_rand = saux->xhv_last_rand;
14665 #endif
14666                         daux->xhv_riter = saux->xhv_riter;
14667                         daux->xhv_eiter = saux->xhv_eiter
14668                             ? he_dup(saux->xhv_eiter, FALSE, param) : 0;
14669                         /* backref array needs refcnt=2; see sv_add_backref */
14670                         daux->xhv_backreferences =
14671                             (param->flags & CLONEf_JOIN_IN)
14672                                 /* when joining, we let the individual GVs and
14673                                  * CVs add themselves to backref as
14674                                  * needed. This avoids pulling in stuff
14675                                  * that isn't required, and simplifies the
14676                                  * case where stashes aren't cloned back
14677                                  * if they already exist in the parent
14678                                  * thread */
14679                             ? NULL
14680                             : saux->xhv_backreferences
14681                                 ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV)
14682                                     ? MUTABLE_AV(SvREFCNT_inc(
14683                                           sv_dup_inc((const SV *)
14684                                             saux->xhv_backreferences, param)))
14685                                     : MUTABLE_AV(sv_dup((const SV *)
14686                                             saux->xhv_backreferences, param))
14687                                 : 0;
14688
14689                         daux->xhv_mro_meta = saux->xhv_mro_meta
14690                             ? mro_meta_dup(saux->xhv_mro_meta, param)
14691                             : 0;
14692
14693                         /* Record stashes for possible cloning in Perl_clone(). */
14694                         if (HvNAME(ssv))
14695                             av_push(param->stashes, dsv);
14696                     }
14697                 }
14698                 else
14699                     HvARRAY(MUTABLE_HV(dsv)) = NULL;
14700                 break;
14701             case SVt_PVCV:
14702                 if (!(param->flags & CLONEf_COPY_STACKS)) {
14703                     CvDEPTH(dsv) = 0;
14704                 }
14705                 /* FALLTHROUGH */
14706             case SVt_PVFM:
14707                 /* NOTE: not refcounted */
14708                 SvANY(MUTABLE_CV(dsv))->xcv_stash =
14709                     hv_dup(CvSTASH(dsv), param);
14710                 if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dsv))
14711                     Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dsv)), dsv);
14712                 if (!CvISXSUB(dsv)) {
14713                     OP_REFCNT_LOCK;
14714                     CvROOT(dsv) = OpREFCNT_inc(CvROOT(dsv));
14715                     OP_REFCNT_UNLOCK;
14716                     CvSLABBED_off(dsv);
14717                 } else if (CvCONST(dsv)) {
14718                     CvXSUBANY(dsv).any_ptr =
14719                         sv_dup_inc((const SV *)CvXSUBANY(dsv).any_ptr, param);
14720                 } else if (CvREFCOUNTED_ANYSV(dsv)) {
14721                     CvXSUBANY(dsv).any_sv =
14722                         sv_dup_inc((const SV *)CvXSUBANY(dsv).any_sv, param);
14723                 }
14724                 assert(!CvSLABBED(dsv));
14725                 if (CvDYNFILE(dsv)) CvFILE(dsv) = SAVEPV(CvFILE(dsv));
14726                 if (CvNAMED(dsv))
14727                     SvANY((CV *)dsv)->xcv_gv_u.xcv_hek =
14728                         hek_dup(CvNAME_HEK((CV *)ssv), param);
14729                 /* don't dup if copying back - CvGV isn't refcounted, so the
14730                  * duped GV may never be freed. A bit of a hack! DAPM */
14731                 else
14732                   SvANY(MUTABLE_CV(dsv))->xcv_gv_u.xcv_gv =
14733                     CvCVGV_RC(dsv)
14734                     ? gv_dup_inc(CvGV(ssv), param)
14735                     : (param->flags & CLONEf_JOIN_IN)
14736                         ? NULL
14737                         : gv_dup(CvGV(ssv), param);
14738
14739                 if (!CvISXSUB(ssv)) {
14740                     PADLIST * padlist = CvPADLIST(ssv);
14741                     if(padlist)
14742                         padlist = padlist_dup(padlist, param);
14743                     CvPADLIST_set(dsv, padlist);
14744                 } else
14745 /* unthreaded perl can't sv_dup so we dont support unthreaded's CvHSCXT */
14746                     PoisonPADLIST(dsv);
14747
14748                 CvOUTSIDE(dsv)  =
14749                     CvWEAKOUTSIDE(ssv)
14750                     ? cv_dup(    CvOUTSIDE(dsv), param)
14751                     : cv_dup_inc(CvOUTSIDE(dsv), param);
14752                 break;
14753             }
14754         }
14755     }
14756
14757     return dsv;
14758  }
14759
14760 SV *
14761 Perl_sv_dup_inc(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14762 {
14763     PERL_ARGS_ASSERT_SV_DUP_INC;
14764     return ssv ? SvREFCNT_inc(sv_dup_common(ssv, param)) : NULL;
14765 }
14766
14767 SV *
14768 Perl_sv_dup(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14769 {
14770     SV *dsv = ssv ? sv_dup_common(ssv, param) : NULL;
14771     PERL_ARGS_ASSERT_SV_DUP;
14772
14773     /* Track every SV that (at least initially) had a reference count of 0.
14774        We need to do this by holding an actual reference to it in this array.
14775        If we attempt to cheat, turn AvREAL_off(), and store only pointers
14776        (akin to the stashes hash, and the perl stack), we come unstuck if
14777        a weak reference (or other SV legitimately SvREFCNT() == 0 for this
14778        thread) is manipulated in a CLONE method, because CLONE runs before the
14779        unreferenced array is walked to find SVs still with SvREFCNT() == 0
14780        (and fix things up by giving each a reference via the temps stack).
14781        Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and
14782        then SvREFCNT_dec(), it will be cleaned up (and added to the free list)
14783        before the walk of unreferenced happens and a reference to that is SV
14784        added to the temps stack. At which point we have the same SV considered
14785        to be in use, and free to be re-used. Not good.
14786     */
14787     if (dsv && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dsv)) {
14788         assert(param->unreferenced);
14789         av_push(param->unreferenced, SvREFCNT_inc(dsv));
14790     }
14791
14792     return dsv;
14793 }
14794
14795 /* duplicate a context */
14796
14797 PERL_CONTEXT *
14798 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
14799 {
14800     PERL_CONTEXT *ncxs;
14801
14802     PERL_ARGS_ASSERT_CX_DUP;
14803
14804     if (!cxs)
14805         return (PERL_CONTEXT*)NULL;
14806
14807     /* look for it in the table first */
14808     ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
14809     if (ncxs)
14810         return ncxs;
14811
14812     /* create anew and remember what it is */
14813     Newx(ncxs, max + 1, PERL_CONTEXT);
14814     ptr_table_store(PL_ptr_table, cxs, ncxs);
14815     Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
14816
14817     while (ix >= 0) {
14818         PERL_CONTEXT * const ncx = &ncxs[ix];
14819         if (CxTYPE(ncx) == CXt_SUBST) {
14820             Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
14821         }
14822         else {
14823             ncx->blk_oldcop = (COP*)any_dup(ncx->blk_oldcop, param->proto_perl);
14824             switch (CxTYPE(ncx)) {
14825             case CXt_SUB:
14826                 ncx->blk_sub.cv         = cv_dup_inc(ncx->blk_sub.cv, param);
14827                 if(CxHASARGS(ncx)){
14828                     ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,param);
14829                 } else {
14830                     ncx->blk_sub.savearray = NULL;
14831                 }
14832                 ncx->blk_sub.prevcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
14833                                            ncx->blk_sub.prevcomppad);
14834                 break;
14835             case CXt_EVAL:
14836                 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
14837                                                       param);
14838                 /* XXX should this sv_dup_inc? Or only if CxEVAL_TXT_REFCNTED ???? */
14839                 ncx->blk_eval.cur_text  = sv_dup(ncx->blk_eval.cur_text, param);
14840                 ncx->blk_eval.cv = cv_dup(ncx->blk_eval.cv, param);
14841                 /* XXX what to do with cur_top_env ???? */
14842                 break;
14843             case CXt_LOOP_LAZYSV:
14844                 ncx->blk_loop.state_u.lazysv.end
14845                     = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
14846                 /* Fallthrough: duplicate lazysv.cur by using the ary.ary
14847                    duplication code instead.
14848                    We are taking advantage of (1) av_dup_inc and sv_dup_inc
14849                    actually being the same function, and (2) order
14850                    equivalence of the two unions.
14851                    We can assert the later [but only at run time :-(]  */
14852                 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
14853                         (void *) &ncx->blk_loop.state_u.lazysv.cur);
14854                 /* FALLTHROUGH */
14855             case CXt_LOOP_ARY:
14856                 ncx->blk_loop.state_u.ary.ary
14857                     = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
14858                 /* FALLTHROUGH */
14859             case CXt_LOOP_LIST:
14860             case CXt_LOOP_LAZYIV:
14861                 /* code common to all 'for' CXt_LOOP_* types */
14862                 ncx->blk_loop.itersave =
14863                                     sv_dup_inc(ncx->blk_loop.itersave, param);
14864                 if (CxPADLOOP(ncx)) {
14865                     PADOFFSET off = ncx->blk_loop.itervar_u.svp
14866                                     - &CX_CURPAD_SV(ncx->blk_loop, 0);
14867                     ncx->blk_loop.oldcomppad =
14868                                     (PAD*)ptr_table_fetch(PL_ptr_table,
14869                                                 ncx->blk_loop.oldcomppad);
14870                     ncx->blk_loop.itervar_u.svp =
14871                                     &CX_CURPAD_SV(ncx->blk_loop, off);
14872                 }
14873                 else {
14874                     /* this copies the GV if CXp_FOR_GV, or the SV for an
14875                      * alias (for \$x (...)) - relies on gv_dup being the
14876                      * same as sv_dup */
14877                     ncx->blk_loop.itervar_u.gv
14878                         = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv,
14879                                     param);
14880                 }
14881                 break;
14882             case CXt_LOOP_PLAIN:
14883                 break;
14884             case CXt_FORMAT:
14885                 ncx->blk_format.prevcomppad =
14886                         (PAD*)ptr_table_fetch(PL_ptr_table,
14887                                            ncx->blk_format.prevcomppad);
14888                 ncx->blk_format.cv      = cv_dup_inc(ncx->blk_format.cv, param);
14889                 ncx->blk_format.gv      = gv_dup(ncx->blk_format.gv, param);
14890                 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
14891                                                      param);
14892                 break;
14893             case CXt_GIVEN:
14894                 ncx->blk_givwhen.defsv_save =
14895                                 sv_dup_inc(ncx->blk_givwhen.defsv_save, param);
14896                 break;
14897             case CXt_BLOCK:
14898             case CXt_NULL:
14899             case CXt_WHEN:
14900             case CXt_DEFER:
14901                 break;
14902             }
14903         }
14904         --ix;
14905     }
14906     return ncxs;
14907 }
14908
14909 /*
14910 =for apidoc si_dup
14911
14912 Duplicate a stack info structure, returning a pointer to the cloned object.
14913
14914 =cut
14915 */
14916
14917 PERL_SI *
14918 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
14919 {
14920     PERL_SI *nsi;
14921
14922     PERL_ARGS_ASSERT_SI_DUP;
14923
14924     if (!si)
14925         return (PERL_SI*)NULL;
14926
14927     /* look for it in the table first */
14928     nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
14929     if (nsi)
14930         return nsi;
14931
14932     /* create anew and remember what it is */
14933     Newx(nsi, 1, PERL_SI);
14934     ptr_table_store(PL_ptr_table, si, nsi);
14935
14936     nsi->si_stack       = av_dup_inc(si->si_stack, param);
14937     nsi->si_cxix        = si->si_cxix;
14938     nsi->si_cxsubix     = si->si_cxsubix;
14939     nsi->si_cxmax       = si->si_cxmax;
14940     nsi->si_cxstack     = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
14941     nsi->si_type        = si->si_type;
14942     nsi->si_prev        = si_dup(si->si_prev, param);
14943     nsi->si_next        = si_dup(si->si_next, param);
14944     nsi->si_markoff     = si->si_markoff;
14945 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
14946     nsi->si_stack_hwm   = 0;
14947 #endif
14948
14949     return nsi;
14950 }
14951
14952 #define POPINT(ss,ix)   ((ss)[--(ix)].any_i32)
14953 #define TOPINT(ss,ix)   ((ss)[ix].any_i32)
14954 #define POPLONG(ss,ix)  ((ss)[--(ix)].any_long)
14955 #define TOPLONG(ss,ix)  ((ss)[ix].any_long)
14956 #define POPIV(ss,ix)    ((ss)[--(ix)].any_iv)
14957 #define TOPIV(ss,ix)    ((ss)[ix].any_iv)
14958 #define POPUV(ss,ix)    ((ss)[--(ix)].any_uv)
14959 #define TOPUV(ss,ix)    ((ss)[ix].any_uv)
14960 #define POPBOOL(ss,ix)  ((ss)[--(ix)].any_bool)
14961 #define TOPBOOL(ss,ix)  ((ss)[ix].any_bool)
14962 #define POPPTR(ss,ix)   ((ss)[--(ix)].any_ptr)
14963 #define TOPPTR(ss,ix)   ((ss)[ix].any_ptr)
14964 #define POPDPTR(ss,ix)  ((ss)[--(ix)].any_dptr)
14965 #define TOPDPTR(ss,ix)  ((ss)[ix].any_dptr)
14966 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
14967 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
14968
14969 /* XXXXX todo */
14970 #define pv_dup_inc(p)   SAVEPV(p)
14971 #define pv_dup(p)       SAVEPV(p)
14972 #define svp_dup_inc(p,pp)       any_dup(p,pp)
14973
14974 /* map any object to the new equivent - either something in the
14975  * ptr table, or something in the interpreter structure
14976  */
14977
14978 void *
14979 Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
14980 {
14981     void *ret;
14982
14983     PERL_ARGS_ASSERT_ANY_DUP;
14984
14985     if (!v)
14986         return (void*)NULL;
14987
14988     /* look for it in the table first */
14989     ret = ptr_table_fetch(PL_ptr_table, v);
14990     if (ret)
14991         return ret;
14992
14993     /* see if it is part of the interpreter structure */
14994     if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
14995         ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
14996     else {
14997         ret = v;
14998     }
14999
15000     return ret;
15001 }
15002
15003 /*
15004 =for apidoc ss_dup
15005
15006 Duplicate the save stack, returning a pointer to the cloned object.
15007
15008 =cut
15009 */
15010
15011 ANY *
15012 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
15013 {
15014     ANY * const ss      = proto_perl->Isavestack;
15015     const I32 max       = proto_perl->Isavestack_max + SS_MAXPUSH;
15016     I32 ix              = proto_perl->Isavestack_ix;
15017     ANY *nss;
15018     const SV *sv;
15019     const GV *gv;
15020     const AV *av;
15021     const HV *hv;
15022     void* ptr;
15023     int intval;
15024     long longval;
15025     GP *gp;
15026     IV iv;
15027     I32 i;
15028     char *c = NULL;
15029     void (*dptr) (void*);
15030     void (*dxptr) (pTHX_ void*);
15031
15032     PERL_ARGS_ASSERT_SS_DUP;
15033
15034     Newx(nss, max, ANY);
15035
15036     while (ix > 0) {
15037         const UV uv = POPUV(ss,ix);
15038         const U8 type = (U8)uv & SAVE_MASK;
15039
15040         TOPUV(nss,ix) = uv;
15041         switch (type) {
15042         case SAVEt_CLEARSV:
15043         case SAVEt_CLEARPADRANGE:
15044             break;
15045         case SAVEt_HELEM:               /* hash element */
15046         case SAVEt_SV:                  /* scalar reference */
15047             sv = (const SV *)POPPTR(ss,ix);
15048             TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15049             /* FALLTHROUGH */
15050         case SAVEt_ITEM:                        /* normal string */
15051         case SAVEt_GVSV:                        /* scalar slot in GV */
15052             sv = (const SV *)POPPTR(ss,ix);
15053             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15054             if (type == SAVEt_SV)
15055                 break;
15056             /* FALLTHROUGH */
15057         case SAVEt_FREESV:
15058         case SAVEt_MORTALIZESV:
15059         case SAVEt_READONLY_OFF:
15060             sv = (const SV *)POPPTR(ss,ix);
15061             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15062             break;
15063         case SAVEt_FREEPADNAME:
15064             ptr = POPPTR(ss,ix);
15065             TOPPTR(nss,ix) = padname_dup((PADNAME *)ptr, param);
15066             PadnameREFCNT((PADNAME *)TOPPTR(nss,ix))++;
15067             break;
15068         case SAVEt_SHARED_PVREF:                /* char* in shared space */
15069             c = (char*)POPPTR(ss,ix);
15070             TOPPTR(nss,ix) = savesharedpv(c);
15071             ptr = POPPTR(ss,ix);
15072             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15073             break;
15074         case SAVEt_GENERIC_SVREF:               /* generic sv */
15075         case SAVEt_SVREF:                       /* scalar reference */
15076             sv = (const SV *)POPPTR(ss,ix);
15077             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15078             if (type == SAVEt_SVREF)
15079                 SvREFCNT_inc_simple_void((SV *)TOPPTR(nss,ix));
15080             ptr = POPPTR(ss,ix);
15081             TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15082             break;
15083         case SAVEt_GVSLOT:              /* any slot in GV */
15084             sv = (const SV *)POPPTR(ss,ix);
15085             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15086             ptr = POPPTR(ss,ix);
15087             TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15088             sv = (const SV *)POPPTR(ss,ix);
15089             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15090             break;
15091         case SAVEt_HV:                          /* hash reference */
15092         case SAVEt_AV:                          /* array reference */
15093             sv = (const SV *) POPPTR(ss,ix);
15094             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15095             /* FALLTHROUGH */
15096         case SAVEt_COMPPAD:
15097         case SAVEt_NSTAB:
15098             sv = (const SV *) POPPTR(ss,ix);
15099             TOPPTR(nss,ix) = sv_dup(sv, param);
15100             break;
15101         case SAVEt_INT:                         /* int reference */
15102             ptr = POPPTR(ss,ix);
15103             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15104             intval = (int)POPINT(ss,ix);
15105             TOPINT(nss,ix) = intval;
15106             break;
15107         case SAVEt_LONG:                        /* long reference */
15108             ptr = POPPTR(ss,ix);
15109             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15110             longval = (long)POPLONG(ss,ix);
15111             TOPLONG(nss,ix) = longval;
15112             break;
15113         case SAVEt_I32:                         /* I32 reference */
15114             ptr = POPPTR(ss,ix);
15115             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15116             i = POPINT(ss,ix);
15117             TOPINT(nss,ix) = i;
15118             break;
15119         case SAVEt_IV:                          /* IV reference */
15120         case SAVEt_STRLEN:                      /* STRLEN/size_t ref */
15121             ptr = POPPTR(ss,ix);
15122             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15123             iv = POPIV(ss,ix);
15124             TOPIV(nss,ix) = iv;
15125             break;
15126         case SAVEt_TMPSFLOOR:
15127             iv = POPIV(ss,ix);
15128             TOPIV(nss,ix) = iv;
15129             break;
15130         case SAVEt_HPTR:                        /* HV* reference */
15131         case SAVEt_APTR:                        /* AV* reference */
15132         case SAVEt_SPTR:                        /* SV* reference */
15133             ptr = POPPTR(ss,ix);
15134             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15135             sv = (const SV *)POPPTR(ss,ix);
15136             TOPPTR(nss,ix) = sv_dup(sv, param);
15137             break;
15138         case SAVEt_VPTR:                        /* random* reference */
15139             ptr = POPPTR(ss,ix);
15140             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15141             /* FALLTHROUGH */
15142         case SAVEt_STRLEN_SMALL:
15143         case SAVEt_INT_SMALL:
15144         case SAVEt_I32_SMALL:
15145         case SAVEt_I16:                         /* I16 reference */
15146         case SAVEt_I8:                          /* I8 reference */
15147         case SAVEt_BOOL:
15148             ptr = POPPTR(ss,ix);
15149             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15150             break;
15151         case SAVEt_GENERIC_PVREF:               /* generic char* */
15152         case SAVEt_PPTR:                        /* char* reference */
15153             ptr = POPPTR(ss,ix);
15154             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15155             c = (char*)POPPTR(ss,ix);
15156             TOPPTR(nss,ix) = pv_dup(c);
15157             break;
15158         case SAVEt_GP:                          /* scalar reference */
15159             gp = (GP*)POPPTR(ss,ix);
15160             TOPPTR(nss,ix) = gp = gp_dup(gp, param);
15161             (void)GpREFCNT_inc(gp);
15162             gv = (const GV *)POPPTR(ss,ix);
15163             TOPPTR(nss,ix) = gv_dup_inc(gv, param);
15164             break;
15165         case SAVEt_FREEOP:
15166             ptr = POPPTR(ss,ix);
15167             if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
15168                 /* these are assumed to be refcounted properly */
15169                 OP *o;
15170                 switch (((OP*)ptr)->op_type) {
15171                 case OP_LEAVESUB:
15172                 case OP_LEAVESUBLV:
15173                 case OP_LEAVEEVAL:
15174                 case OP_LEAVE:
15175                 case OP_SCOPE:
15176                 case OP_LEAVEWRITE:
15177                     TOPPTR(nss,ix) = ptr;
15178                     o = (OP*)ptr;
15179                     OP_REFCNT_LOCK;
15180                     (void) OpREFCNT_inc(o);
15181                     OP_REFCNT_UNLOCK;
15182                     break;
15183                 default:
15184                     TOPPTR(nss,ix) = NULL;
15185                     break;
15186                 }
15187             }
15188             else
15189                 TOPPTR(nss,ix) = NULL;
15190             break;
15191         case SAVEt_FREECOPHH:
15192             ptr = POPPTR(ss,ix);
15193             TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr);
15194             break;
15195         case SAVEt_ADELETE:
15196             av = (const AV *)POPPTR(ss,ix);
15197             TOPPTR(nss,ix) = av_dup_inc(av, param);
15198             i = POPINT(ss,ix);
15199             TOPINT(nss,ix) = i;
15200             break;
15201         case SAVEt_DELETE:
15202             hv = (const HV *)POPPTR(ss,ix);
15203             TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15204             i = POPINT(ss,ix);
15205             TOPINT(nss,ix) = i;
15206             /* FALLTHROUGH */
15207         case SAVEt_FREEPV:
15208             c = (char*)POPPTR(ss,ix);
15209             TOPPTR(nss,ix) = pv_dup_inc(c);
15210             break;
15211         case SAVEt_STACK_POS:           /* Position on Perl stack */
15212             i = POPINT(ss,ix);
15213             TOPINT(nss,ix) = i;
15214             break;
15215         case SAVEt_DESTRUCTOR:
15216             ptr = POPPTR(ss,ix);
15217             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);  /* XXX quite arbitrary */
15218             dptr = POPDPTR(ss,ix);
15219             TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
15220                                         any_dup(FPTR2DPTR(void *, dptr),
15221                                                 proto_perl));
15222             break;
15223         case SAVEt_DESTRUCTOR_X:
15224             ptr = POPPTR(ss,ix);
15225             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);  /* XXX quite arbitrary */
15226             dxptr = POPDXPTR(ss,ix);
15227             TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
15228                                          any_dup(FPTR2DPTR(void *, dxptr),
15229                                                  proto_perl));
15230             break;
15231         case SAVEt_REGCONTEXT:
15232         case SAVEt_ALLOC:
15233             ix -= uv >> SAVE_TIGHT_SHIFT;
15234             break;
15235         case SAVEt_AELEM:               /* array element */
15236             sv = (const SV *)POPPTR(ss,ix);
15237             TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15238             iv = POPIV(ss,ix);
15239             TOPIV(nss,ix) = iv;
15240             av = (const AV *)POPPTR(ss,ix);
15241             TOPPTR(nss,ix) = av_dup_inc(av, param);
15242             break;
15243         case SAVEt_OP:
15244             ptr = POPPTR(ss,ix);
15245             TOPPTR(nss,ix) = ptr;
15246             break;
15247         case SAVEt_HINTS_HH:
15248             hv = (const HV *)POPPTR(ss,ix);
15249             TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15250             /* FALLTHROUGH */
15251         case SAVEt_HINTS:
15252             ptr = POPPTR(ss,ix);
15253             ptr = cophh_copy((COPHH*)ptr);
15254             TOPPTR(nss,ix) = ptr;
15255             i = POPINT(ss,ix);
15256             TOPINT(nss,ix) = i;
15257             break;
15258         case SAVEt_PADSV_AND_MORTALIZE:
15259             longval = (long)POPLONG(ss,ix);
15260             TOPLONG(nss,ix) = longval;
15261             ptr = POPPTR(ss,ix);
15262             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15263             sv = (const SV *)POPPTR(ss,ix);
15264             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15265             break;
15266         case SAVEt_SET_SVFLAGS:
15267             i = POPINT(ss,ix);
15268             TOPINT(nss,ix) = i;
15269             i = POPINT(ss,ix);
15270             TOPINT(nss,ix) = i;
15271             sv = (const SV *)POPPTR(ss,ix);
15272             TOPPTR(nss,ix) = sv_dup(sv, param);
15273             break;
15274         case SAVEt_COMPILE_WARNINGS:
15275             ptr = POPPTR(ss,ix);
15276             TOPPTR(nss,ix) = DUP_WARNINGS((STRLEN*)ptr);
15277             break;
15278         case SAVEt_PARSER:
15279             ptr = POPPTR(ss,ix);
15280             TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
15281             break;
15282         default:
15283             Perl_croak(aTHX_
15284                        "panic: ss_dup inconsistency (%" IVdf ")", (IV) type);
15285         }
15286     }
15287
15288     return nss;
15289 }
15290
15291
15292 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
15293  * flag to the result. This is done for each stash before cloning starts,
15294  * so we know which stashes want their objects cloned */
15295
15296 static void
15297 do_mark_cloneable_stash(pTHX_ SV *const sv)
15298 {
15299     const HEK * const hvname = HvNAME_HEK((const HV *)sv);
15300     if (hvname) {
15301         GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
15302         SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
15303         if (cloner && GvCV(cloner)) {
15304             dSP;
15305             UV status;
15306
15307             ENTER;
15308             SAVETMPS;
15309             PUSHMARK(SP);
15310             mXPUSHs(newSVhek(hvname));
15311             PUTBACK;
15312             call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
15313             SPAGAIN;
15314             status = POPu;
15315             PUTBACK;
15316             FREETMPS;
15317             LEAVE;
15318             if (status)
15319                 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
15320         }
15321     }
15322 }
15323
15324
15325
15326 /*
15327 =for apidoc perl_clone
15328
15329 Create and return a new interpreter by cloning the current one.
15330
15331 C<perl_clone> takes these flags as parameters:
15332
15333 C<CLONEf_COPY_STACKS> - is used to, well, copy the stacks also,
15334 without it we only clone the data and zero the stacks,
15335 with it we copy the stacks and the new perl interpreter is
15336 ready to run at the exact same point as the previous one.
15337 The pseudo-fork code uses C<COPY_STACKS> while the
15338 threads->create doesn't.
15339
15340 C<CLONEf_KEEP_PTR_TABLE> -
15341 C<perl_clone> keeps a ptr_table with the pointer of the old
15342 variable as a key and the new variable as a value,
15343 this allows it to check if something has been cloned and not
15344 clone it again, but rather just use the value and increase the
15345 refcount.
15346 If C<KEEP_PTR_TABLE> is not set then C<perl_clone> will kill the ptr_table
15347 using the function S<C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>>.
15348 A reason to keep it around is if you want to dup some of your own
15349 variables which are outside the graph that perl scans.
15350
15351 C<CLONEf_CLONE_HOST> -
15352 This is a win32 thing, it is ignored on unix, it tells perl's
15353 win32host code (which is c++) to clone itself, this is needed on
15354 win32 if you want to run two threads at the same time,
15355 if you just want to do some stuff in a separate perl interpreter
15356 and then throw it away and return to the original one,
15357 you don't need to do anything.
15358
15359 =cut
15360 */
15361
15362 /* XXX the above needs expanding by someone who actually understands it ! */
15363 EXTERN_C PerlInterpreter *
15364 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
15365
15366 PerlInterpreter *
15367 perl_clone(PerlInterpreter *proto_perl, UV flags)
15368 {
15369 #ifdef PERL_IMPLICIT_SYS
15370
15371     PERL_ARGS_ASSERT_PERL_CLONE;
15372
15373    /* perlhost.h so we need to call into it
15374    to clone the host, CPerlHost should have a c interface, sky */
15375
15376 #ifndef __amigaos4__
15377    if (flags & CLONEf_CLONE_HOST) {
15378        return perl_clone_host(proto_perl,flags);
15379    }
15380 #endif
15381    return perl_clone_using(proto_perl, flags,
15382                             proto_perl->IMem,
15383                             proto_perl->IMemShared,
15384                             proto_perl->IMemParse,
15385                             proto_perl->IEnv,
15386                             proto_perl->IStdIO,
15387                             proto_perl->ILIO,
15388                             proto_perl->IDir,
15389                             proto_perl->ISock,
15390                             proto_perl->IProc);
15391 }
15392
15393 PerlInterpreter *
15394 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
15395                  struct IPerlMem* ipM, struct IPerlMem* ipMS,
15396                  struct IPerlMem* ipMP, struct IPerlEnv* ipE,
15397                  struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
15398                  struct IPerlDir* ipD, struct IPerlSock* ipS,
15399                  struct IPerlProc* ipP)
15400 {
15401     /* XXX many of the string copies here can be optimized if they're
15402      * constants; they need to be allocated as common memory and just
15403      * their pointers copied. */
15404
15405     IV i;
15406     CLONE_PARAMS clone_params;
15407     CLONE_PARAMS* const param = &clone_params;
15408
15409     PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
15410
15411     PERL_ARGS_ASSERT_PERL_CLONE_USING;
15412 #else           /* !PERL_IMPLICIT_SYS */
15413     IV i;
15414     CLONE_PARAMS clone_params;
15415     CLONE_PARAMS* param = &clone_params;
15416     PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
15417
15418     PERL_ARGS_ASSERT_PERL_CLONE;
15419 #endif          /* PERL_IMPLICIT_SYS */
15420
15421     /* for each stash, determine whether its objects should be cloned */
15422     S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
15423     PERL_SET_THX(my_perl);
15424
15425 #ifdef DEBUGGING
15426     PoisonNew(my_perl, 1, PerlInterpreter);
15427     PL_op = NULL;
15428     PL_curcop = NULL;
15429     PL_defstash = NULL; /* may be used by perl malloc() */
15430     PL_markstack = 0;
15431     PL_scopestack = 0;
15432     PL_scopestack_name = 0;
15433     PL_savestack = 0;
15434     PL_savestack_ix = 0;
15435     PL_savestack_max = -1;
15436     PL_sig_pending = 0;
15437     PL_parser = NULL;
15438     Zero(&PL_debug_pad, 1, struct perl_debug_pad);
15439     Zero(&PL_padname_undef, 1, PADNAME);
15440     Zero(&PL_padname_const, 1, PADNAME);
15441 #  ifdef DEBUG_LEAKING_SCALARS
15442     PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000;
15443 #  endif
15444 #  ifdef PERL_TRACE_OPS
15445     Zero(PL_op_exec_cnt, OP_max+2, UV);
15446 #  endif
15447 #else   /* !DEBUGGING */
15448     Zero(my_perl, 1, PerlInterpreter);
15449 #endif  /* DEBUGGING */
15450
15451 #ifdef PERL_IMPLICIT_SYS
15452     /* host pointers */
15453     PL_Mem              = ipM;
15454     PL_MemShared        = ipMS;
15455     PL_MemParse         = ipMP;
15456     PL_Env              = ipE;
15457     PL_StdIO            = ipStd;
15458     PL_LIO              = ipLIO;
15459     PL_Dir              = ipD;
15460     PL_Sock             = ipS;
15461     PL_Proc             = ipP;
15462 #endif          /* PERL_IMPLICIT_SYS */
15463
15464
15465     param->flags = flags;
15466     /* Nothing in the core code uses this, but we make it available to
15467        extensions (using mg_dup).  */
15468     param->proto_perl = proto_perl;
15469     /* Likely nothing will use this, but it is initialised to be consistent
15470        with Perl_clone_params_new().  */
15471     param->new_perl = my_perl;
15472     param->unreferenced = NULL;
15473
15474
15475     INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
15476
15477     PL_body_arenas = NULL;
15478     Zero(&PL_body_roots, 1, PL_body_roots);
15479
15480     PL_sv_count         = 0;
15481     PL_sv_root          = NULL;
15482     PL_sv_arenaroot     = NULL;
15483
15484     PL_debug            = proto_perl->Idebug;
15485
15486     /* dbargs array probably holds garbage */
15487     PL_dbargs           = NULL;
15488
15489     PL_compiling = proto_perl->Icompiling;
15490
15491     /* pseudo environmental stuff */
15492     PL_origargc         = proto_perl->Iorigargc;
15493     PL_origargv         = proto_perl->Iorigargv;
15494
15495 #ifndef NO_TAINT_SUPPORT
15496     /* Set tainting stuff before PerlIO_debug can possibly get called */
15497     PL_tainting         = proto_perl->Itainting;
15498     PL_taint_warn       = proto_perl->Itaint_warn;
15499 #else
15500     PL_tainting         = FALSE;
15501     PL_taint_warn       = FALSE;
15502 #endif
15503
15504     PL_minus_c          = proto_perl->Iminus_c;
15505
15506     PL_localpatches     = proto_perl->Ilocalpatches;
15507     PL_splitstr         = proto_perl->Isplitstr;
15508     PL_minus_n          = proto_perl->Iminus_n;
15509     PL_minus_p          = proto_perl->Iminus_p;
15510     PL_minus_l          = proto_perl->Iminus_l;
15511     PL_minus_a          = proto_perl->Iminus_a;
15512     PL_minus_E          = proto_perl->Iminus_E;
15513     PL_minus_F          = proto_perl->Iminus_F;
15514     PL_doswitches       = proto_perl->Idoswitches;
15515     PL_dowarn           = proto_perl->Idowarn;
15516 #ifdef PERL_SAWAMPERSAND
15517     PL_sawampersand     = proto_perl->Isawampersand;
15518 #endif
15519     PL_unsafe           = proto_perl->Iunsafe;
15520     PL_perldb           = proto_perl->Iperldb;
15521     PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
15522     PL_exit_flags       = proto_perl->Iexit_flags;
15523
15524     /* XXX time(&PL_basetime) when asked for? */
15525     PL_basetime         = proto_perl->Ibasetime;
15526
15527     PL_maxsysfd         = proto_perl->Imaxsysfd;
15528     PL_statusvalue      = proto_perl->Istatusvalue;
15529 #ifdef __VMS
15530     PL_statusvalue_vms  = proto_perl->Istatusvalue_vms;
15531 #else
15532     PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
15533 #endif
15534
15535     /* RE engine related */
15536     PL_regmatch_slab    = NULL;
15537     PL_reg_curpm        = NULL;
15538
15539     PL_sub_generation   = proto_perl->Isub_generation;
15540
15541     /* funky return mechanisms */
15542     PL_forkprocess      = proto_perl->Iforkprocess;
15543
15544     /* internal state */
15545     PL_main_start       = proto_perl->Imain_start;
15546     PL_eval_root        = proto_perl->Ieval_root;
15547     PL_eval_start       = proto_perl->Ieval_start;
15548
15549     PL_filemode         = proto_perl->Ifilemode;
15550     PL_lastfd           = proto_perl->Ilastfd;
15551     PL_oldname          = proto_perl->Ioldname;         /* XXX not quite right */
15552     PL_gensym           = proto_perl->Igensym;
15553
15554     PL_laststatval      = proto_perl->Ilaststatval;
15555     PL_laststype        = proto_perl->Ilaststype;
15556     PL_mess_sv          = NULL;
15557
15558     PL_profiledata      = NULL;
15559
15560     PL_generation       = proto_perl->Igeneration;
15561
15562     PL_in_clean_objs    = proto_perl->Iin_clean_objs;
15563     PL_in_clean_all     = proto_perl->Iin_clean_all;
15564
15565     PL_delaymagic_uid   = proto_perl->Idelaymagic_uid;
15566     PL_delaymagic_euid  = proto_perl->Idelaymagic_euid;
15567     PL_delaymagic_gid   = proto_perl->Idelaymagic_gid;
15568     PL_delaymagic_egid  = proto_perl->Idelaymagic_egid;
15569     PL_nomemok          = proto_perl->Inomemok;
15570     PL_an               = proto_perl->Ian;
15571     PL_evalseq          = proto_perl->Ievalseq;
15572     PL_origalen         = proto_perl->Iorigalen;
15573
15574     PL_sighandlerp      = proto_perl->Isighandlerp;
15575     PL_sighandler1p     = proto_perl->Isighandler1p;
15576     PL_sighandler3p     = proto_perl->Isighandler3p;
15577
15578     PL_runops           = proto_perl->Irunops;
15579
15580     PL_subline          = proto_perl->Isubline;
15581
15582     PL_cv_has_eval      = proto_perl->Icv_has_eval;
15583
15584 #ifdef USE_LOCALE_COLLATE
15585     PL_collation_ix     = proto_perl->Icollation_ix;
15586     PL_collation_standard = proto_perl->Icollation_standard;
15587     PL_collxfrm_base    = proto_perl->Icollxfrm_base;
15588     PL_collxfrm_mult    = proto_perl->Icollxfrm_mult;
15589     PL_strxfrm_max_cp   = proto_perl->Istrxfrm_max_cp;
15590     PL_strxfrm_is_behaved = proto_perl->Istrxfrm_is_behaved;
15591     PL_strxfrm_NUL_replacement = proto_perl->Istrxfrm_NUL_replacement;
15592 #endif /* USE_LOCALE_COLLATE */
15593
15594 #ifdef USE_LOCALE_NUMERIC
15595     PL_numeric_standard = proto_perl->Inumeric_standard;
15596     PL_numeric_underlying       = proto_perl->Inumeric_underlying;
15597     PL_numeric_underlying_is_standard   = proto_perl->Inumeric_underlying_is_standard;
15598 #endif /* !USE_LOCALE_NUMERIC */
15599
15600     /* Did the locale setup indicate UTF-8? */
15601     PL_utf8locale       = proto_perl->Iutf8locale;
15602     my_strlcpy(PL_locale_utf8ness, proto_perl->Ilocale_utf8ness, sizeof(PL_locale_utf8ness));
15603 #if defined(USE_ITHREADS) && ! defined(USE_THREAD_SAFE_LOCALE)
15604     PL_lc_numeric_mutex_depth = 0;
15605 #endif
15606     /* Unicode features (see perlrun/-C) */
15607     PL_unicode          = proto_perl->Iunicode;
15608
15609     /* Pre-5.8 signals control */
15610     PL_signals          = proto_perl->Isignals;
15611
15612     /* times() ticks per second */
15613     PL_clocktick        = proto_perl->Iclocktick;
15614
15615     /* Recursion stopper for PerlIO_find_layer */
15616     PL_in_load_module   = proto_perl->Iin_load_module;
15617
15618     /* Not really needed/useful since the reenrant_retint is "volatile",
15619      * but do it for consistency's sake. */
15620     PL_reentrant_retint = proto_perl->Ireentrant_retint;
15621
15622     /* Hooks to shared SVs and locks. */
15623     PL_sharehook        = proto_perl->Isharehook;
15624     PL_lockhook         = proto_perl->Ilockhook;
15625     PL_unlockhook       = proto_perl->Iunlockhook;
15626     PL_threadhook       = proto_perl->Ithreadhook;
15627     PL_destroyhook      = proto_perl->Idestroyhook;
15628     PL_signalhook       = proto_perl->Isignalhook;
15629
15630     PL_globhook         = proto_perl->Iglobhook;
15631
15632     PL_srand_called     = proto_perl->Isrand_called;
15633     Copy(&(proto_perl->Irandom_state), &PL_random_state, 1, PL_RANDOM_STATE_TYPE);
15634     PL_srand_override   = proto_perl->Isrand_override;
15635     PL_srand_override_next = proto_perl->Isrand_override_next;
15636
15637     if (flags & CLONEf_COPY_STACKS) {
15638         /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
15639         PL_tmps_ix              = proto_perl->Itmps_ix;
15640         PL_tmps_max             = proto_perl->Itmps_max;
15641         PL_tmps_floor           = proto_perl->Itmps_floor;
15642
15643         /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
15644          * NOTE: unlike the others! */
15645         PL_scopestack_ix        = proto_perl->Iscopestack_ix;
15646         PL_scopestack_max       = proto_perl->Iscopestack_max;
15647
15648         /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
15649          * NOTE: unlike the others! */
15650         PL_savestack_ix         = proto_perl->Isavestack_ix;
15651         PL_savestack_max        = proto_perl->Isavestack_max;
15652     }
15653
15654     PL_start_env        = proto_perl->Istart_env;       /* XXXXXX */
15655     PL_top_env          = &PL_start_env;
15656
15657     PL_op               = proto_perl->Iop;
15658
15659     PL_Sv               = NULL;
15660     PL_Xpv              = (XPV*)NULL;
15661     my_perl->Ina        = proto_perl->Ina;
15662
15663     PL_statcache        = proto_perl->Istatcache;
15664
15665 #ifndef NO_TAINT_SUPPORT
15666     PL_tainted          = proto_perl->Itainted;
15667 #else
15668     PL_tainted          = FALSE;
15669 #endif
15670     PL_curpm            = proto_perl->Icurpm;   /* XXX No PMOP ref count */
15671
15672     PL_chopset          = proto_perl->Ichopset; /* XXX never deallocated */
15673
15674     PL_restartjmpenv    = proto_perl->Irestartjmpenv;
15675     PL_restartop        = proto_perl->Irestartop;
15676     PL_in_eval          = proto_perl->Iin_eval;
15677     PL_delaymagic       = proto_perl->Idelaymagic;
15678     PL_phase            = proto_perl->Iphase;
15679     PL_localizing       = proto_perl->Ilocalizing;
15680
15681     PL_hv_fetch_ent_mh  = NULL;
15682     PL_modcount         = proto_perl->Imodcount;
15683     PL_lastgotoprobe    = NULL;
15684     PL_dumpindent       = proto_perl->Idumpindent;
15685
15686     PL_efloatbuf        = NULL;         /* reinits on demand */
15687     PL_efloatsize       = 0;                    /* reinits on demand */
15688
15689     /* regex stuff */
15690
15691     PL_colorset         = 0;            /* reinits PL_colors[] */
15692     /*PL_colors[6]      = {0,0,0,0,0,0};*/
15693
15694     /* Pluggable optimizer */
15695     PL_peepp            = proto_perl->Ipeepp;
15696     PL_rpeepp           = proto_perl->Irpeepp;
15697     /* op_free() hook */
15698     PL_opfreehook       = proto_perl->Iopfreehook;
15699
15700 #  ifdef PERL_MEM_LOG
15701     Zero(PL_mem_log, sizeof(PL_mem_log), char);
15702 #  endif
15703
15704 #ifdef USE_REENTRANT_API
15705     /* XXX: things like -Dm will segfault here in perlio, but doing
15706      *  PERL_SET_CONTEXT(proto_perl);
15707      * breaks too many other things
15708      */
15709     Perl_reentrant_init(aTHX);
15710 #endif
15711
15712     /* create SV map for pointer relocation */
15713     PL_ptr_table = ptr_table_new();
15714
15715     /* initialize these special pointers as early as possible */
15716     init_constants();
15717     ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
15718     ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
15719     ptr_table_store(PL_ptr_table, &proto_perl->Isv_zero, &PL_sv_zero);
15720     ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
15721     ptr_table_store(PL_ptr_table, &proto_perl->Ipadname_const,
15722                     &PL_padname_const);
15723
15724     /* create (a non-shared!) shared string table */
15725     PL_strtab           = newHV();
15726     HvSHAREKEYS_off(PL_strtab);
15727     hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
15728     ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
15729
15730     Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
15731
15732     /* This PV will be free'd special way so must set it same way op.c does */
15733     PL_compiling.cop_file    = savesharedpv(PL_compiling.cop_file);
15734     ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
15735
15736     ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
15737     PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
15738     CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling)));
15739     PL_curcop           = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
15740
15741     param->stashes      = newAV();  /* Setup array of objects to call clone on */
15742     /* This makes no difference to the implementation, as it always pushes
15743        and shifts pointers to other SVs without changing their reference
15744        count, with the array becoming empty before it is freed. However, it
15745        makes it conceptually clear what is going on, and will avoid some
15746        work inside av.c, filling slots between AvFILL() and AvMAX() with
15747        &PL_sv_undef, and SvREFCNT_dec()ing those.  */
15748     AvREAL_off(param->stashes);
15749
15750     if (!(flags & CLONEf_COPY_STACKS)) {
15751         param->unreferenced = newAV();
15752     }
15753
15754 #ifdef PERLIO_LAYERS
15755     /* Clone PerlIO tables as soon as we can handle general xx_dup() */
15756     PerlIO_clone(aTHX_ proto_perl, param);
15757 #endif
15758
15759     PL_envgv            = gv_dup_inc(proto_perl->Ienvgv, param);
15760     PL_incgv            = gv_dup_inc(proto_perl->Iincgv, param);
15761     PL_hintgv           = gv_dup_inc(proto_perl->Ihintgv, param);
15762     PL_origfilename     = SAVEPV(proto_perl->Iorigfilename);
15763     PL_xsubfilename     = proto_perl->Ixsubfilename;
15764     PL_diehook          = sv_dup_inc(proto_perl->Idiehook, param);
15765     PL_warnhook         = sv_dup_inc(proto_perl->Iwarnhook, param);
15766
15767     /* switches */
15768     PL_patchlevel       = sv_dup_inc(proto_perl->Ipatchlevel, param);
15769     PL_inplace          = SAVEPV(proto_perl->Iinplace);
15770     PL_e_script         = sv_dup_inc(proto_perl->Ie_script, param);
15771
15772     /* magical thingies */
15773
15774     SvPVCLEAR(PERL_DEBUG_PAD(0));        /* For regex debugging. */
15775     SvPVCLEAR(PERL_DEBUG_PAD(1));        /* ext/re needs these */
15776     SvPVCLEAR(PERL_DEBUG_PAD(2));        /* even without DEBUGGING. */
15777
15778
15779     /* Clone the regex array */
15780     /* ORANGE FIXME for plugins, probably in the SV dup code.
15781        newSViv(PTR2IV(CALLREGDUPE(
15782        INT2PTR(REGEXP *, SvIVX(regex)), param))))
15783     */
15784     PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
15785     PL_regex_pad = AvARRAY(PL_regex_padav);
15786
15787     PL_stashpadmax      = proto_perl->Istashpadmax;
15788     PL_stashpadix       = proto_perl->Istashpadix ;
15789     Newx(PL_stashpad, PL_stashpadmax, HV *);
15790     {
15791         PADOFFSET o = 0;
15792         for (; o < PL_stashpadmax; ++o)
15793             PL_stashpad[o] = hv_dup(proto_perl->Istashpad[o], param);
15794     }
15795
15796     /* shortcuts to various I/O objects */
15797     PL_ofsgv            = gv_dup_inc(proto_perl->Iofsgv, param);
15798     PL_stdingv          = gv_dup(proto_perl->Istdingv, param);
15799     PL_stderrgv         = gv_dup(proto_perl->Istderrgv, param);
15800     PL_defgv            = gv_dup(proto_perl->Idefgv, param);
15801     PL_argvgv           = gv_dup_inc(proto_perl->Iargvgv, param);
15802     PL_argvoutgv        = gv_dup(proto_perl->Iargvoutgv, param);
15803     PL_argvout_stack    = av_dup_inc(proto_perl->Iargvout_stack, param);
15804
15805     /* shortcuts to regexp stuff */
15806     PL_replgv           = gv_dup_inc(proto_perl->Ireplgv, param);
15807
15808     /* shortcuts to misc objects */
15809     PL_errgv            = gv_dup(proto_perl->Ierrgv, param);
15810
15811     /* shortcuts to debugging objects */
15812     PL_DBgv             = gv_dup_inc(proto_perl->IDBgv, param);
15813     PL_DBline           = gv_dup_inc(proto_perl->IDBline, param);
15814     PL_DBsub            = gv_dup_inc(proto_perl->IDBsub, param);
15815     PL_DBsingle         = sv_dup(proto_perl->IDBsingle, param);
15816     PL_DBtrace          = sv_dup(proto_perl->IDBtrace, param);
15817     PL_DBsignal         = sv_dup(proto_perl->IDBsignal, param);
15818     Copy(proto_perl->IDBcontrol, PL_DBcontrol, DBVARMG_COUNT, IV);
15819
15820     /* symbol tables */
15821     PL_defstash         = hv_dup_inc(proto_perl->Idefstash, param);
15822     PL_curstash         = hv_dup_inc(proto_perl->Icurstash, param);
15823     PL_debstash         = hv_dup(proto_perl->Idebstash, param);
15824     PL_globalstash      = hv_dup(proto_perl->Iglobalstash, param);
15825     PL_curstname        = sv_dup_inc(proto_perl->Icurstname, param);
15826
15827     PL_beginav          = av_dup_inc(proto_perl->Ibeginav, param);
15828     PL_beginav_save     = av_dup_inc(proto_perl->Ibeginav_save, param);
15829     PL_checkav_save     = av_dup_inc(proto_perl->Icheckav_save, param);
15830     PL_unitcheckav      = av_dup_inc(proto_perl->Iunitcheckav, param);
15831     PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
15832     PL_endav            = av_dup_inc(proto_perl->Iendav, param);
15833     PL_checkav          = av_dup_inc(proto_perl->Icheckav, param);
15834     PL_initav           = av_dup_inc(proto_perl->Iinitav, param);
15835     PL_savebegin        = proto_perl->Isavebegin;
15836
15837     PL_isarev           = hv_dup_inc(proto_perl->Iisarev, param);
15838
15839     /* subprocess state */
15840     PL_fdpid            = av_dup_inc(proto_perl->Ifdpid, param);
15841
15842     if (proto_perl->Iop_mask)
15843         PL_op_mask      = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
15844     else
15845         PL_op_mask      = NULL;
15846     /* PL_asserting        = proto_perl->Iasserting; */
15847
15848     /* current interpreter roots */
15849     PL_main_cv          = cv_dup_inc(proto_perl->Imain_cv, param);
15850     OP_REFCNT_LOCK;
15851     PL_main_root        = OpREFCNT_inc(proto_perl->Imain_root);
15852     OP_REFCNT_UNLOCK;
15853
15854     /* runtime control stuff */
15855     PL_curcopdb         = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
15856
15857     PL_preambleav       = av_dup_inc(proto_perl->Ipreambleav, param);
15858
15859     PL_ors_sv           = sv_dup_inc(proto_perl->Iors_sv, param);
15860
15861     /* interpreter atexit processing */
15862     PL_exitlistlen      = proto_perl->Iexitlistlen;
15863     if (PL_exitlistlen) {
15864         Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
15865         Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
15866     }
15867     else
15868         PL_exitlist     = (PerlExitListEntry*)NULL;
15869
15870     PL_my_cxt_size = proto_perl->Imy_cxt_size;
15871     if (PL_my_cxt_size) {
15872         Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
15873         Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
15874     }
15875     else {
15876         PL_my_cxt_list  = (void**)NULL;
15877     }
15878     PL_modglobal        = hv_dup_inc(proto_perl->Imodglobal, param);
15879     PL_custom_op_names  = hv_dup_inc(proto_perl->Icustom_op_names,param);
15880     PL_custom_op_descs  = hv_dup_inc(proto_perl->Icustom_op_descs,param);
15881     PL_custom_ops       = hv_dup_inc(proto_perl->Icustom_ops, param);
15882
15883     PL_compcv                   = cv_dup(proto_perl->Icompcv, param);
15884
15885     PAD_CLONE_VARS(proto_perl, param);
15886
15887 #ifdef HAVE_INTERP_INTERN
15888     sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
15889 #endif
15890
15891     PL_DBcv             = cv_dup(proto_perl->IDBcv, param);
15892
15893 #ifdef PERL_USES_PL_PIDSTATUS
15894     PL_pidstatus        = newHV();                      /* XXX flag for cloning? */
15895 #endif
15896     PL_osname           = SAVEPV(proto_perl->Iosname);
15897     PL_parser           = parser_dup(proto_perl->Iparser, param);
15898
15899     /* XXX this only works if the saved cop has already been cloned */
15900     if (proto_perl->Iparser) {
15901         PL_parser->saved_curcop = (COP*)any_dup(
15902                                     proto_perl->Iparser->saved_curcop,
15903                                     proto_perl);
15904     }
15905
15906     PL_subname          = sv_dup_inc(proto_perl->Isubname, param);
15907
15908 #if   defined(USE_POSIX_2008_LOCALE)      \
15909  &&   defined(USE_THREAD_SAFE_LOCALE)     \
15910  && ! defined(HAS_QUERYLOCALE)
15911     for (i = 0; i < (int) C_ARRAY_LENGTH(PL_curlocales); i++) {
15912         PL_curlocales[i] = SAVEPV(proto_perl->Icurlocales[i]);
15913     }
15914 #endif
15915 #ifdef USE_LOCALE_CTYPE
15916     Copy(proto_perl->Ifold_locale, PL_fold_locale, 256, U8);
15917     /* Should we warn if uses locale? */
15918     PL_warn_locale      = sv_dup_inc(proto_perl->Iwarn_locale, param);
15919     PL_utf8locale             = proto_perl->Iutf8locale;
15920     PL_in_utf8_CTYPE_locale   = proto_perl->Iin_utf8_CTYPE_locale;
15921     PL_in_utf8_turkic_locale  = proto_perl->Iin_utf8_turkic_locale;
15922 #endif
15923
15924 #ifdef USE_LOCALE_COLLATE
15925     PL_in_utf8_COLLATE_locale = proto_perl->Iin_utf8_COLLATE_locale;
15926     PL_collation_name   = SAVEPV(proto_perl->Icollation_name);
15927 #endif /* USE_LOCALE_COLLATE */
15928
15929 #ifdef USE_LOCALE_NUMERIC
15930     PL_numeric_name     = SAVEPV(proto_perl->Inumeric_name);
15931     PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
15932
15933 #  if defined(USE_POSIX_2008_LOCALE)
15934     PL_underlying_numeric_obj = NULL;
15935 #  endif
15936 #endif /* !USE_LOCALE_NUMERIC */
15937 #if defined(USE_POSIX_2008_LOCALE)
15938     PL_scratch_locale_obj = NULL;
15939 #endif
15940
15941 #ifdef HAS_MBRLEN
15942     PL_mbrlen_ps = proto_perl->Imbrlen_ps;
15943 #endif
15944 #ifdef HAS_MBRTOWC
15945     PL_mbrtowc_ps = proto_perl->Imbrtowc_ps;
15946 #endif
15947 #ifdef HAS_WCRTOMB
15948     PL_wcrtomb_ps = proto_perl->Iwcrtomb_ps;
15949 #endif
15950
15951     PL_langinfo_buf = NULL;
15952     PL_langinfo_bufsize = 0;
15953
15954     PL_setlocale_buf = NULL;
15955     PL_setlocale_bufsize = 0;
15956
15957     PL_stdize_locale_buf = NULL;
15958     PL_stdize_locale_bufsize = 0;
15959
15960     /* Unicode inversion lists */
15961
15962     PL_AboveLatin1            = sv_dup_inc(proto_perl->IAboveLatin1, param);
15963     PL_Assigned_invlist       = sv_dup_inc(proto_perl->IAssigned_invlist, param);
15964     PL_GCB_invlist            = sv_dup_inc(proto_perl->IGCB_invlist, param);
15965     PL_HasMultiCharFold       = sv_dup_inc(proto_perl->IHasMultiCharFold, param);
15966     PL_InMultiCharFold        = sv_dup_inc(proto_perl->IInMultiCharFold, param);
15967     PL_Latin1                 = sv_dup_inc(proto_perl->ILatin1, param);
15968     PL_LB_invlist             = sv_dup_inc(proto_perl->ILB_invlist, param);
15969     PL_SB_invlist             = sv_dup_inc(proto_perl->ISB_invlist, param);
15970     PL_SCX_invlist            = sv_dup_inc(proto_perl->ISCX_invlist, param);
15971     PL_UpperLatin1            = sv_dup_inc(proto_perl->IUpperLatin1, param);
15972     PL_in_some_fold           = sv_dup_inc(proto_perl->Iin_some_fold, param);
15973     PL_utf8_foldclosures      = sv_dup_inc(proto_perl->Iutf8_foldclosures, param);
15974     PL_utf8_idcont            = sv_dup_inc(proto_perl->Iutf8_idcont, param);
15975     PL_utf8_idstart           = sv_dup_inc(proto_perl->Iutf8_idstart, param);
15976     PL_utf8_perl_idcont       = sv_dup_inc(proto_perl->Iutf8_perl_idcont, param);
15977     PL_utf8_perl_idstart      = sv_dup_inc(proto_perl->Iutf8_perl_idstart, param);
15978     PL_utf8_xidcont           = sv_dup_inc(proto_perl->Iutf8_xidcont, param);
15979     PL_utf8_xidstart          = sv_dup_inc(proto_perl->Iutf8_xidstart, param);
15980     PL_WB_invlist             = sv_dup_inc(proto_perl->IWB_invlist, param);
15981     for (i = 0; i < POSIX_CC_COUNT; i++) {
15982         PL_XPosix_ptrs[i]     = sv_dup_inc(proto_perl->IXPosix_ptrs[i], param);
15983         if (i != CC_CASED_ && i != CC_VERTSPACE_) {
15984             PL_Posix_ptrs[i]  = sv_dup_inc(proto_perl->IPosix_ptrs[i], param);
15985         }
15986     }
15987     PL_Posix_ptrs[CC_CASED_]  = PL_Posix_ptrs[CC_ALPHA_];
15988     PL_Posix_ptrs[CC_VERTSPACE_] = NULL;
15989
15990     PL_utf8_toupper           = sv_dup_inc(proto_perl->Iutf8_toupper, param);
15991     PL_utf8_totitle           = sv_dup_inc(proto_perl->Iutf8_totitle, param);
15992     PL_utf8_tolower           = sv_dup_inc(proto_perl->Iutf8_tolower, param);
15993     PL_utf8_tofold            = sv_dup_inc(proto_perl->Iutf8_tofold, param);
15994     PL_utf8_tosimplefold      = sv_dup_inc(proto_perl->Iutf8_tosimplefold, param);
15995     PL_utf8_charname_begin    = sv_dup_inc(proto_perl->Iutf8_charname_begin, param);
15996     PL_utf8_charname_continue = sv_dup_inc(proto_perl->Iutf8_charname_continue, param);
15997     PL_utf8_mark              = sv_dup_inc(proto_perl->Iutf8_mark, param);
15998     PL_InBitmap               = sv_dup_inc(proto_perl->IInBitmap, param);
15999     PL_CCC_non0_non230        = sv_dup_inc(proto_perl->ICCC_non0_non230, param);
16000     PL_Private_Use            = sv_dup_inc(proto_perl->IPrivate_Use, param);
16001
16002 #if 0
16003     PL_seen_deprecated_macro = hv_dup_inc(proto_perl->Iseen_deprecated_macro, param);
16004 #endif
16005
16006     if (proto_perl->Ipsig_pend) {
16007         Newxz(PL_psig_pend, SIG_SIZE, int);
16008     }
16009     else {
16010         PL_psig_pend    = (int*)NULL;
16011     }
16012
16013     if (proto_perl->Ipsig_name) {
16014         Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
16015         sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
16016                             param);
16017         PL_psig_ptr = PL_psig_name + SIG_SIZE;
16018     }
16019     else {
16020         PL_psig_ptr     = (SV**)NULL;
16021         PL_psig_name    = (SV**)NULL;
16022     }
16023
16024     if (flags & CLONEf_COPY_STACKS) {
16025         Newx(PL_tmps_stack, PL_tmps_max, SV*);
16026         sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack,
16027                             PL_tmps_ix+1, param);
16028
16029         /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
16030         i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
16031         Newx(PL_markstack, i, I32);
16032         PL_markstack_max        = PL_markstack + (proto_perl->Imarkstack_max
16033                                                   - proto_perl->Imarkstack);
16034         PL_markstack_ptr        = PL_markstack + (proto_perl->Imarkstack_ptr
16035                                                   - proto_perl->Imarkstack);
16036         Copy(proto_perl->Imarkstack, PL_markstack,
16037              PL_markstack_ptr - PL_markstack + 1, I32);
16038
16039         /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
16040          * NOTE: unlike the others! */
16041         Newx(PL_scopestack, PL_scopestack_max, I32);
16042         Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
16043
16044 #ifdef DEBUGGING
16045         Newx(PL_scopestack_name, PL_scopestack_max, const char *);
16046         Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *);
16047 #endif
16048         /* reset stack AV to correct length before its duped via
16049          * PL_curstackinfo */
16050         AvFILLp(proto_perl->Icurstack) =
16051                             proto_perl->Istack_sp - proto_perl->Istack_base;
16052
16053         /* NOTE: si_dup() looks at PL_markstack */
16054         PL_curstackinfo         = si_dup(proto_perl->Icurstackinfo, param);
16055
16056         /* PL_curstack          = PL_curstackinfo->si_stack; */
16057         PL_curstack             = av_dup(proto_perl->Icurstack, param);
16058         PL_mainstack            = av_dup(proto_perl->Imainstack, param);
16059
16060         /* next PUSHs() etc. set *(PL_stack_sp+1) */
16061         PL_stack_base           = AvARRAY(PL_curstack);
16062         PL_stack_sp             = PL_stack_base + (proto_perl->Istack_sp
16063                                                    - proto_perl->Istack_base);
16064         PL_stack_max            = PL_stack_base + AvMAX(PL_curstack);
16065
16066         /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
16067         PL_savestack            = ss_dup(proto_perl, param);
16068     }
16069     else {
16070         init_stacks();
16071         ENTER;                  /* perl_destruct() wants to LEAVE; */
16072     }
16073
16074     PL_statgv           = gv_dup(proto_perl->Istatgv, param);
16075     PL_statname         = sv_dup_inc(proto_perl->Istatname, param);
16076
16077     PL_rs               = sv_dup_inc(proto_perl->Irs, param);
16078     PL_last_in_gv       = gv_dup(proto_perl->Ilast_in_gv, param);
16079     PL_defoutgv         = gv_dup_inc(proto_perl->Idefoutgv, param);
16080     PL_toptarget        = sv_dup_inc(proto_perl->Itoptarget, param);
16081     PL_bodytarget       = sv_dup_inc(proto_perl->Ibodytarget, param);
16082     PL_formtarget       = sv_dup(proto_perl->Iformtarget, param);
16083
16084     PL_errors           = sv_dup_inc(proto_perl->Ierrors, param);
16085
16086     PL_sortcop          = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
16087     PL_firstgv          = gv_dup_inc(proto_perl->Ifirstgv, param);
16088     PL_secondgv         = gv_dup_inc(proto_perl->Isecondgv, param);
16089
16090     PL_stashcache       = newHV();
16091
16092     PL_watchaddr        = (char **) ptr_table_fetch(PL_ptr_table,
16093                                             proto_perl->Iwatchaddr);
16094     PL_watchok          = PL_watchaddr ? * PL_watchaddr : NULL;
16095     if (PL_debug && PL_watchaddr) {
16096         PerlIO_printf(Perl_debug_log,
16097           "WATCHING: %" UVxf " cloned as %" UVxf " with value %" UVxf "\n",
16098           PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
16099           PTR2UV(PL_watchok));
16100     }
16101
16102     PL_registered_mros  = hv_dup_inc(proto_perl->Iregistered_mros, param);
16103     PL_blockhooks       = av_dup_inc(proto_perl->Iblockhooks, param);
16104
16105     /* Call the ->CLONE method, if it exists, for each of the stashes
16106        identified by sv_dup() above.
16107     */
16108     while(av_count(param->stashes) != 0) {
16109         HV* const stash = MUTABLE_HV(av_shift(param->stashes));
16110         GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
16111         if (cloner && GvCV(cloner)) {
16112             dSP;
16113             ENTER;
16114             SAVETMPS;
16115             PUSHMARK(SP);
16116             mXPUSHs(newSVhek(HvNAME_HEK(stash)));
16117             PUTBACK;
16118             call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
16119             FREETMPS;
16120             LEAVE;
16121         }
16122     }
16123
16124     if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
16125         ptr_table_free(PL_ptr_table);
16126         PL_ptr_table = NULL;
16127     }
16128
16129     if (!(flags & CLONEf_COPY_STACKS)) {
16130         unreferenced_to_tmp_stack(param->unreferenced);
16131     }
16132
16133     SvREFCNT_dec(param->stashes);
16134
16135     /* orphaned? eg threads->new inside BEGIN or use */
16136     if (PL_compcv && ! SvREFCNT(PL_compcv)) {
16137         SvREFCNT_inc_simple_void(PL_compcv);
16138         SAVEFREESV(PL_compcv);
16139     }
16140
16141     return my_perl;
16142 }
16143
16144 static void
16145 S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced)
16146 {
16147     PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK;
16148
16149     if (AvFILLp(unreferenced) > -1) {
16150         SV **svp = AvARRAY(unreferenced);
16151         SV **const last = svp + AvFILLp(unreferenced);
16152         SSize_t count = 0;
16153
16154         do {
16155             if (SvREFCNT(*svp) == 1)
16156                 ++count;
16157         } while (++svp <= last);
16158
16159         EXTEND_MORTAL(count);
16160         svp = AvARRAY(unreferenced);
16161
16162         do {
16163             if (SvREFCNT(*svp) == 1) {
16164                 /* Our reference is the only one to this SV. This means that
16165                    in this thread, the scalar effectively has a 0 reference.
16166                    That doesn't work (cleanup never happens), so donate our
16167                    reference to it onto the save stack. */
16168                 PL_tmps_stack[++PL_tmps_ix] = *svp;
16169             } else {
16170                 /* As an optimisation, because we are already walking the
16171                    entire array, instead of above doing either
16172                    SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead
16173                    release our reference to the scalar, so that at the end of
16174                    the array owns zero references to the scalars it happens to
16175                    point to. We are effectively converting the array from
16176                    AvREAL() on to AvREAL() off. This saves the av_clear()
16177                    (triggered by the SvREFCNT_dec(unreferenced) below) from
16178                    walking the array a second time.  */
16179                 SvREFCNT_dec(*svp);
16180             }
16181
16182         } while (++svp <= last);
16183         AvREAL_off(unreferenced);
16184     }
16185     SvREFCNT_dec_NN(unreferenced);
16186 }
16187
16188 void
16189 Perl_clone_params_del(CLONE_PARAMS *param)
16190 {
16191     PerlInterpreter *const was = PERL_GET_THX;
16192     PerlInterpreter *const to = param->new_perl;
16193     dTHXa(to);
16194
16195     PERL_ARGS_ASSERT_CLONE_PARAMS_DEL;
16196
16197     if (was != to) {
16198         PERL_SET_THX(to);
16199     }
16200
16201     SvREFCNT_dec(param->stashes);
16202     if (param->unreferenced)
16203         unreferenced_to_tmp_stack(param->unreferenced);
16204
16205     Safefree(param);
16206
16207     if (was != to) {
16208         PERL_SET_THX(was);
16209     }
16210 }
16211
16212 CLONE_PARAMS *
16213 Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to)
16214 {
16215     /* Need to play this game, as newAV() can call safesysmalloc(), and that
16216        does a dTHX; to get the context from thread local storage.
16217        FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to
16218        a version that passes in my_perl.  */
16219     PerlInterpreter *const was = PERL_GET_THX;
16220     CLONE_PARAMS *param;
16221
16222     PERL_ARGS_ASSERT_CLONE_PARAMS_NEW;
16223
16224     if (was != to) {
16225         PERL_SET_THX(to);
16226     }
16227
16228     /* Given that we've set the context, we can do this unshared.  */
16229     Newx(param, 1, CLONE_PARAMS);
16230
16231     param->flags = 0;
16232     param->proto_perl = from;
16233     param->new_perl = to;
16234     param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV);
16235     AvREAL_off(param->stashes);
16236     param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV);
16237
16238     if (was != to) {
16239         PERL_SET_THX(was);
16240     }
16241     return param;
16242 }
16243
16244 #endif /* USE_ITHREADS */
16245
16246 void
16247 Perl_init_constants(pTHX)
16248 {
16249
16250     SvREFCNT(&PL_sv_undef)      = SvREFCNT_IMMORTAL;
16251     SvFLAGS(&PL_sv_undef)       = SVf_READONLY|SVf_PROTECT|SVt_NULL;
16252     SvANY(&PL_sv_undef)         = NULL;
16253
16254     SvANY(&PL_sv_no)            = new_XPVNV();
16255     SvREFCNT(&PL_sv_no)         = SvREFCNT_IMMORTAL;
16256     SvFLAGS(&PL_sv_no)          = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16257                                   |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16258                                   |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16259
16260     SvANY(&PL_sv_yes)           = new_XPVNV();
16261     SvREFCNT(&PL_sv_yes)        = SvREFCNT_IMMORTAL;
16262     SvFLAGS(&PL_sv_yes)         = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16263                                   |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16264                                   |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16265
16266     SvANY(&PL_sv_zero)          = new_XPVNV();
16267     SvREFCNT(&PL_sv_zero)       = SvREFCNT_IMMORTAL;
16268     SvFLAGS(&PL_sv_zero)        = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16269                                   |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16270                                   |SVp_POK|SVf_POK
16271                                   |SVs_PADTMP;
16272
16273     SvPV_set(&PL_sv_no, (char*)PL_No);
16274     SvCUR_set(&PL_sv_no, 0);
16275     SvLEN_set(&PL_sv_no, 0);
16276     SvIV_set(&PL_sv_no, 0);
16277     SvNV_set(&PL_sv_no, 0);
16278
16279     SvPV_set(&PL_sv_yes, (char*)PL_Yes);
16280     SvCUR_set(&PL_sv_yes, 1);
16281     SvLEN_set(&PL_sv_yes, 0);
16282     SvIV_set(&PL_sv_yes, 1);
16283     SvNV_set(&PL_sv_yes, 1);
16284
16285     SvPV_set(&PL_sv_zero, (char*)PL_Zero);
16286     SvCUR_set(&PL_sv_zero, 1);
16287     SvLEN_set(&PL_sv_zero, 0);
16288     SvIV_set(&PL_sv_zero, 0);
16289     SvNV_set(&PL_sv_zero, 0);
16290
16291     PadnamePV(&PL_padname_const) = (char *)PL_No;
16292
16293     assert(SvIMMORTAL_INTERP(&PL_sv_yes));
16294     assert(SvIMMORTAL_INTERP(&PL_sv_undef));
16295     assert(SvIMMORTAL_INTERP(&PL_sv_no));
16296     assert(SvIMMORTAL_INTERP(&PL_sv_zero));
16297
16298     assert(SvIMMORTAL(&PL_sv_yes));
16299     assert(SvIMMORTAL(&PL_sv_undef));
16300     assert(SvIMMORTAL(&PL_sv_no));
16301     assert(SvIMMORTAL(&PL_sv_zero));
16302
16303     assert( SvIMMORTAL_TRUE(&PL_sv_yes));
16304     assert(!SvIMMORTAL_TRUE(&PL_sv_undef));
16305     assert(!SvIMMORTAL_TRUE(&PL_sv_no));
16306     assert(!SvIMMORTAL_TRUE(&PL_sv_zero));
16307
16308     assert( SvTRUE_nomg_NN(&PL_sv_yes));
16309     assert(!SvTRUE_nomg_NN(&PL_sv_undef));
16310     assert(!SvTRUE_nomg_NN(&PL_sv_no));
16311     assert(!SvTRUE_nomg_NN(&PL_sv_zero));
16312 }
16313
16314 /*
16315 =for apidoc_section $unicode
16316
16317 =for apidoc sv_recode_to_utf8
16318
16319 C<encoding> is assumed to be an C<Encode> object, on entry the PV
16320 of C<sv> is assumed to be octets in that encoding, and C<sv>
16321 will be converted into Unicode (and UTF-8).
16322
16323 If C<sv> already is UTF-8 (or if it is not C<POK>), or if C<encoding>
16324 is not a reference, nothing is done to C<sv>.  If C<encoding> is not
16325 an C<Encode::XS> Encoding object, bad things will happen.
16326 (See L<encoding> and L<Encode>.)
16327
16328 The PV of C<sv> is returned.
16329
16330 =cut */
16331
16332 char *
16333 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
16334 {
16335     PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
16336
16337     if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
16338         SV *uni;
16339         STRLEN len;
16340         const char *s;
16341         dSP;
16342         SV *nsv = sv;
16343         ENTER;
16344         PUSHSTACK;
16345         SAVETMPS;
16346         if (SvPADTMP(nsv)) {
16347             nsv = sv_newmortal();
16348             SvSetSV_nosteal(nsv, sv);
16349         }
16350         save_re_context();
16351         PUSHMARK(sp);
16352         EXTEND(SP, 3);
16353         PUSHs(encoding);
16354         PUSHs(nsv);
16355 /*
16356   NI-S 2002/07/09
16357   Passing sv_yes is wrong - it needs to be or'ed set of constants
16358   for Encode::XS, while UTf-8 decode (currently) assumes a true value means
16359   remove converted chars from source.
16360
16361   Both will default the value - let them.
16362
16363         XPUSHs(&PL_sv_yes);
16364 */
16365         PUTBACK;
16366         call_method("decode", G_SCALAR);
16367         SPAGAIN;
16368         uni = POPs;
16369         PUTBACK;
16370         s = SvPV_const(uni, len);
16371         if (s != SvPVX_const(sv)) {
16372             SvGROW(sv, len + 1);
16373             Move(s, SvPVX(sv), len + 1, char);
16374             SvCUR_set(sv, len);
16375         }
16376         FREETMPS;
16377         POPSTACK;
16378         LEAVE;
16379         if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
16380             /* clear pos and any utf8 cache */
16381             MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
16382             if (mg)
16383                 mg->mg_len = -1;
16384             if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
16385                 magic_setutf8(sv,mg); /* clear UTF8 cache */
16386         }
16387         SvUTF8_on(sv);
16388         return SvPVX(sv);
16389     }
16390     return SvPOKp(sv) ? SvPVX(sv) : NULL;
16391 }
16392
16393 /*
16394 =for apidoc sv_cat_decode
16395
16396 C<encoding> is assumed to be an C<Encode> object, the PV of C<ssv> is
16397 assumed to be octets in that encoding and decoding the input starts
16398 from the position which S<C<(PV + *offset)>> pointed to.  C<dsv> will be
16399 concatenated with the decoded UTF-8 string from C<ssv>.  Decoding will terminate
16400 when the string C<tstr> appears in decoding output or the input ends on
16401 the PV of C<ssv>.  The value which C<offset> points will be modified
16402 to the last input position on C<ssv>.
16403
16404 Returns TRUE if the terminator was found, else returns FALSE.
16405
16406 =cut */
16407
16408 bool
16409 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
16410                    SV *ssv, int *offset, char *tstr, int tlen)
16411 {
16412     bool ret = FALSE;
16413
16414     PERL_ARGS_ASSERT_SV_CAT_DECODE;
16415
16416     if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding)) {
16417         SV *offsv;
16418         dSP;
16419         ENTER;
16420         SAVETMPS;
16421         save_re_context();
16422         PUSHMARK(sp);
16423         EXTEND(SP, 6);
16424         PUSHs(encoding);
16425         PUSHs(dsv);
16426         PUSHs(ssv);
16427         offsv = newSViv(*offset);
16428         mPUSHs(offsv);
16429         mPUSHp(tstr, tlen);
16430         PUTBACK;
16431         call_method("cat_decode", G_SCALAR);
16432         SPAGAIN;
16433         ret = SvTRUE(TOPs);
16434         *offset = SvIV(offsv);
16435         PUTBACK;
16436         FREETMPS;
16437         LEAVE;
16438     }
16439     else
16440         Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
16441     return ret;
16442
16443 }
16444
16445 /* ---------------------------------------------------------------------
16446  *
16447  * support functions for report_uninit()
16448  */
16449
16450 /* the maxiumum size of array or hash where we will scan looking
16451  * for the undefined element that triggered the warning */
16452
16453 #define FUV_MAX_SEARCH_SIZE 1000
16454
16455 /* Look for an entry in the hash whose value has the same SV as val;
16456  * If so, return a mortal copy of the key. */
16457
16458 STATIC SV*
16459 S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
16460 {
16461     HE **array;
16462     I32 i;
16463
16464     PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
16465
16466     if (!hv || SvMAGICAL(hv) || !HvTOTALKEYS(hv) ||
16467                         (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
16468         return NULL;
16469
16470     if (val == &PL_sv_undef || val == &PL_sv_placeholder)
16471         return NULL;
16472
16473     array = HvARRAY(hv);
16474
16475     for (i=HvMAX(hv); i>=0; i--) {
16476         HE *entry;
16477         for (entry = array[i]; entry; entry = HeNEXT(entry)) {
16478             if (HeVAL(entry) == val)
16479                 return newSVhek_mortal(HeKEY_hek(entry));
16480         }
16481     }
16482     return NULL;
16483 }
16484
16485 /* Look for an entry in the array whose value has the same SV as val;
16486  * If so, return the index, otherwise return -1. */
16487
16488 STATIC SSize_t
16489 S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
16490 {
16491     PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
16492
16493     if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
16494                         (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
16495         return -1;
16496
16497     if (val != &PL_sv_undef) {
16498         SV ** const svp = AvARRAY(av);
16499         SSize_t i;
16500
16501         for (i=AvFILLp(av); i>=0; i--)
16502             if (svp[i] == val)
16503                 return i;
16504     }
16505     return -1;
16506 }
16507
16508 /* varname(): return the name of a variable, optionally with a subscript.
16509  * If gv is non-zero, use the name of that global, along with gvtype (one
16510  * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
16511  * targ.  Depending on the value of the subscript_type flag, return:
16512  */
16513
16514 #define FUV_SUBSCRIPT_NONE      1       /* "@foo"          */
16515 #define FUV_SUBSCRIPT_ARRAY     2       /* "$foo[aindex]"  */
16516 #define FUV_SUBSCRIPT_HASH      3       /* "$foo{keyname}" */
16517 #define FUV_SUBSCRIPT_WITHIN    4       /* "within @foo"   */
16518
16519 SV*
16520 Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
16521         const SV *const keyname, SSize_t aindex, int subscript_type)
16522 {
16523
16524     SV * const name = sv_newmortal();
16525     if (gv && isGV(gv)) {
16526         char buffer[2];
16527         buffer[0] = gvtype;
16528         buffer[1] = 0;
16529
16530         /* as gv_fullname4(), but add literal '^' for $^FOO names  */
16531
16532         gv_fullname4(name, gv, buffer, 0);
16533
16534         if ((unsigned int)SvPVX(name)[1] <= 26) {
16535             buffer[0] = '^';
16536             buffer[1] = SvPVX(name)[1] + 'A' - 1;
16537
16538             /* Swap the 1 unprintable control character for the 2 byte pretty
16539                version - ie substr($name, 1, 1) = $buffer; */
16540             sv_insert(name, 1, 1, buffer, 2);
16541         }
16542     }
16543     else {
16544         CV * const cv = gv ? ((CV *)gv) : find_runcv(NULL);
16545         PADNAME *sv;
16546
16547         assert(!cv || SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
16548
16549         if (!cv || !CvPADLIST(cv))
16550             return NULL;
16551         sv = padnamelist_fetch(PadlistNAMES(CvPADLIST(cv)), targ);
16552         sv_setpvn(name, PadnamePV(sv), PadnameLEN(sv));
16553         SvUTF8_on(name);
16554     }
16555
16556     if (subscript_type == FUV_SUBSCRIPT_HASH) {
16557         SV * const sv = newSV_type(SVt_NULL);
16558         STRLEN len;
16559         const char * const pv = SvPV_nomg_const((SV*)keyname, len);
16560
16561         *SvPVX(name) = '$';
16562         Perl_sv_catpvf(aTHX_ name, "{%s}",
16563             pv_pretty(sv, pv, len, 32, NULL, NULL,
16564                     PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT ));
16565         SvREFCNT_dec_NN(sv);
16566     }
16567     else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
16568         *SvPVX(name) = '$';
16569         Perl_sv_catpvf(aTHX_ name, "[%" IVdf "]", (IV)aindex);
16570     }
16571     else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
16572         /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
16573         Perl_sv_insert_flags(aTHX_ name, 0, 0,  STR_WITH_LEN("within "), 0);
16574     }
16575
16576     return name;
16577 }
16578
16579
16580 /*
16581 =apidoc_section $warning
16582 =for apidoc find_uninit_var
16583
16584 Find the name of the undefined variable (if any) that caused the operator
16585 to issue a "Use of uninitialized value" warning.
16586 If match is true, only return a name if its value matches C<uninit_sv>.
16587 So roughly speaking, if a unary operator (such as C<OP_COS>) generates a
16588 warning, then following the direct child of the op may yield an
16589 C<OP_PADSV> or C<OP_GV> that gives the name of the undefined variable.  On the
16590 other hand, with C<OP_ADD> there are two branches to follow, so we only print
16591 the variable name if we get an exact match.
16592 C<desc_p> points to a string pointer holding the description of the op.
16593 This may be updated if needed.
16594
16595 The name is returned as a mortal SV.
16596
16597 Assumes that C<PL_op> is the OP that originally triggered the error, and that
16598 C<PL_comppad>/C<PL_curpad> points to the currently executing pad.
16599
16600 =cut
16601 */
16602
16603 STATIC SV *
16604 S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
16605                   bool match, const char **desc_p)
16606 {
16607     SV *sv;
16608     const GV *gv;
16609     const OP *o, *o2, *kid;
16610
16611     PERL_ARGS_ASSERT_FIND_UNINIT_VAR;
16612
16613     if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
16614                             uninit_sv == &PL_sv_placeholder)))
16615         return NULL;
16616
16617     switch (obase->op_type) {
16618
16619     case OP_UNDEF:
16620         /* undef should care if its args are undef - any warnings
16621          * will be from tied/magic vars */
16622         break;
16623
16624     case OP_RV2AV:
16625     case OP_RV2HV:
16626     case OP_PADAV:
16627     case OP_PADHV:
16628       {
16629         const bool pad  = (    obase->op_type == OP_PADAV
16630                             || obase->op_type == OP_PADHV
16631                             || obase->op_type == OP_PADRANGE
16632                           );
16633
16634         const bool hash = (    obase->op_type == OP_PADHV
16635                             || obase->op_type == OP_RV2HV
16636                             || (obase->op_type == OP_PADRANGE
16637                                 && SvTYPE(PAD_SVl(obase->op_targ)) == SVt_PVHV)
16638                           );
16639         SSize_t index = 0;
16640         SV *keysv = NULL;
16641         int subscript_type = FUV_SUBSCRIPT_WITHIN;
16642
16643         if (pad) { /* @lex, %lex */
16644             sv = PAD_SVl(obase->op_targ);
16645             gv = NULL;
16646         }
16647         else {
16648             if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16649             /* @global, %global */
16650                 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16651                 if (!gv)
16652                     break;
16653                 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
16654             }
16655             else if (obase == PL_op) /* @{expr}, %{expr} */
16656                 return find_uninit_var(cUNOPx(obase)->op_first,
16657                                                 uninit_sv, match, desc_p);
16658             else /* @{expr}, %{expr} as a sub-expression */
16659                 return NULL;
16660         }
16661
16662         /* attempt to find a match within the aggregate */
16663         if (hash) {
16664             keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16665             if (keysv)
16666                 subscript_type = FUV_SUBSCRIPT_HASH;
16667         }
16668         else {
16669             index = find_array_subscript((const AV *)sv, uninit_sv);
16670             if (index >= 0)
16671                 subscript_type = FUV_SUBSCRIPT_ARRAY;
16672         }
16673
16674         if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
16675             break;
16676
16677         return varname(gv, (char)(hash ? '%' : '@'), obase->op_targ,
16678                                     keysv, index, subscript_type);
16679       }
16680
16681     case OP_RV2SV:
16682         if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16683             /* $global */
16684             gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16685             if (!gv || !GvSTASH(gv))
16686                 break;
16687             if (match && (GvSV(gv) != uninit_sv))
16688                 break;
16689             return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16690         }
16691         /* ${expr} */
16692         return find_uninit_var(cUNOPx(obase)->op_first, uninit_sv, 1, desc_p);
16693
16694     case OP_PADSV:
16695         if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16696             break;
16697         return varname(NULL, '$', obase->op_targ,
16698                                     NULL, 0, FUV_SUBSCRIPT_NONE);
16699
16700     case OP_GVSV:
16701         gv = cGVOPx_gv(obase);
16702         if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv))
16703             break;
16704         return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16705
16706     case OP_AELEMFAST_LEX:
16707         if (match) {
16708             SV **svp;
16709             AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16710             if (!av || SvRMAGICAL(av))
16711                 break;
16712             svp = av_fetch(av, (I8)obase->op_private, FALSE);
16713             if (!svp || *svp != uninit_sv)
16714                 break;
16715         }
16716         return varname(NULL, '$', obase->op_targ,
16717                        NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16718     case OP_AELEMFAST:
16719         {
16720             gv = cGVOPx_gv(obase);
16721             if (!gv)
16722                 break;
16723             if (match) {
16724                 SV **svp;
16725                 AV *const av = GvAV(gv);
16726                 if (!av || SvRMAGICAL(av))
16727                     break;
16728                 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16729                 if (!svp || *svp != uninit_sv)
16730                     break;
16731             }
16732             return varname(gv, '$', 0,
16733                     NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16734         }
16735         NOT_REACHED; /* NOTREACHED */
16736
16737     case OP_EXISTS:
16738         o = cUNOPx(obase)->op_first;
16739         if (!o || o->op_type != OP_NULL ||
16740                 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
16741             break;
16742         return find_uninit_var(cBINOPo->op_last, uninit_sv, match, desc_p);
16743
16744     case OP_AELEM:
16745     case OP_HELEM:
16746     {
16747         bool negate = FALSE;
16748
16749         if (PL_op == obase)
16750             /* $a[uninit_expr] or $h{uninit_expr} */
16751             return find_uninit_var(cBINOPx(obase)->op_last,
16752                                                 uninit_sv, match, desc_p);
16753
16754         gv = NULL;
16755         o = cBINOPx(obase)->op_first;
16756         kid = cBINOPx(obase)->op_last;
16757
16758         /* get the av or hv, and optionally the gv */
16759         sv = NULL;
16760         if  (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
16761             sv = PAD_SV(o->op_targ);
16762         }
16763         else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
16764                 && cUNOPo->op_first->op_type == OP_GV)
16765         {
16766             gv = cGVOPx_gv(cUNOPo->op_first);
16767             if (!gv)
16768                 break;
16769             sv = o->op_type
16770                 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
16771         }
16772         if (!sv)
16773             break;
16774
16775         if (kid && kid->op_type == OP_NEGATE) {
16776             negate = TRUE;
16777             kid = cUNOPx(kid)->op_first;
16778         }
16779
16780         if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
16781             /* index is constant */
16782             SV* kidsv;
16783             if (negate) {
16784                 kidsv = newSVpvs_flags("-", SVs_TEMP);
16785                 sv_catsv(kidsv, cSVOPx_sv(kid));
16786             }
16787             else
16788                 kidsv = cSVOPx_sv(kid);
16789             if (match) {
16790                 if (SvMAGICAL(sv))
16791                     break;
16792                 if (obase->op_type == OP_HELEM) {
16793                     HE* he = hv_fetch_ent(MUTABLE_HV(sv), kidsv, 0, 0);
16794                     if (!he || HeVAL(he) != uninit_sv)
16795                         break;
16796                 }
16797                 else {
16798                     SV * const  opsv = cSVOPx_sv(kid);
16799                     const IV  opsviv = SvIV(opsv);
16800                     SV * const * const svp = av_fetch(MUTABLE_AV(sv),
16801                         negate ? - opsviv : opsviv,
16802                         FALSE);
16803                     if (!svp || *svp != uninit_sv)
16804                         break;
16805                 }
16806             }
16807             if (obase->op_type == OP_HELEM)
16808                 return varname(gv, '%', o->op_targ,
16809                             kidsv, 0, FUV_SUBSCRIPT_HASH);
16810             else
16811                 return varname(gv, '@', o->op_targ, NULL,
16812                     negate ? - SvIV(cSVOPx_sv(kid)) : SvIV(cSVOPx_sv(kid)),
16813                     FUV_SUBSCRIPT_ARRAY);
16814         }
16815         else {
16816             /* index is an expression;
16817              * attempt to find a match within the aggregate */
16818             if (obase->op_type == OP_HELEM) {
16819                 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16820                 if (keysv)
16821                     return varname(gv, '%', o->op_targ,
16822                                                 keysv, 0, FUV_SUBSCRIPT_HASH);
16823             }
16824             else {
16825                 const SSize_t index
16826                     = find_array_subscript((const AV *)sv, uninit_sv);
16827                 if (index >= 0)
16828                     return varname(gv, '@', o->op_targ,
16829                                         NULL, index, FUV_SUBSCRIPT_ARRAY);
16830             }
16831             if (match)
16832                 break;
16833             return varname(gv,
16834                 (char)((o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
16835                 ? '@' : '%'),
16836                 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
16837         }
16838         NOT_REACHED; /* NOTREACHED */
16839     }
16840
16841     case OP_MULTIDEREF: {
16842         /* If we were executing OP_MULTIDEREF when the undef warning
16843          * triggered, then it must be one of the index values within
16844          * that triggered it. If not, then the only possibility is that
16845          * the value retrieved by the last aggregate index might be the
16846          * culprit. For the former, we set PL_multideref_pc each time before
16847          * using an index, so work though the item list until we reach
16848          * that point. For the latter, just work through the entire item
16849          * list; the last aggregate retrieved will be the candidate.
16850          * There is a third rare possibility: something triggered
16851          * magic while fetching an array/hash element. Just display
16852          * nothing in this case.
16853          */
16854
16855         /* the named aggregate, if any */
16856         PADOFFSET agg_targ = 0;
16857         GV       *agg_gv   = NULL;
16858         /* the last-seen index */
16859         UV        index_type;
16860         PADOFFSET index_targ;
16861         GV       *index_gv;
16862         IV        index_const_iv = 0; /* init for spurious compiler warn */
16863         SV       *index_const_sv;
16864         int       depth = 0;  /* how many array/hash lookups we've done */
16865
16866         UNOP_AUX_item *items = cUNOP_AUXx(obase)->op_aux;
16867         UNOP_AUX_item *last = NULL;
16868         UV actions = items->uv;
16869         bool is_hv;
16870
16871         if (PL_op == obase) {
16872             last = PL_multideref_pc;
16873             assert(last >= items && last <= items + items[-1].uv);
16874         }
16875
16876         assert(actions);
16877
16878         while (1) {
16879             is_hv = FALSE;
16880             switch (actions & MDEREF_ACTION_MASK) {
16881
16882             case MDEREF_reload:
16883                 actions = (++items)->uv;
16884                 continue;
16885
16886             case MDEREF_HV_padhv_helem:               /* $lex{...} */
16887                 is_hv = TRUE;
16888                 /* FALLTHROUGH */
16889             case MDEREF_AV_padav_aelem:               /* $lex[...] */
16890                 agg_targ = (++items)->pad_offset;
16891                 agg_gv = NULL;
16892                 break;
16893
16894             case MDEREF_HV_gvhv_helem:                /* $pkg{...} */
16895                 is_hv = TRUE;
16896                 /* FALLTHROUGH */
16897             case MDEREF_AV_gvav_aelem:                /* $pkg[...] */
16898                 agg_targ = 0;
16899                 agg_gv = (GV*)UNOP_AUX_item_sv(++items);
16900                 assert(isGV_with_GP(agg_gv));
16901                 break;
16902
16903             case MDEREF_HV_gvsv_vivify_rv2hv_helem:   /* $pkg->{...} */
16904             case MDEREF_HV_padsv_vivify_rv2hv_helem:  /* $lex->{...} */
16905                 ++items;
16906                 /* FALLTHROUGH */
16907             case MDEREF_HV_pop_rv2hv_helem:           /* expr->{...} */
16908             case MDEREF_HV_vivify_rv2hv_helem:        /* vivify, ->{...} */
16909                 agg_targ = 0;
16910                 agg_gv   = NULL;
16911                 is_hv    = TRUE;
16912                 break;
16913
16914             case MDEREF_AV_gvsv_vivify_rv2av_aelem:   /* $pkg->[...] */
16915             case MDEREF_AV_padsv_vivify_rv2av_aelem:  /* $lex->[...] */
16916                 ++items;
16917                 /* FALLTHROUGH */
16918             case MDEREF_AV_pop_rv2av_aelem:           /* expr->[...] */
16919             case MDEREF_AV_vivify_rv2av_aelem:        /* vivify, ->[...] */
16920                 agg_targ = 0;
16921                 agg_gv   = NULL;
16922             } /* switch */
16923
16924             index_targ     = 0;
16925             index_gv       = NULL;
16926             index_const_sv = NULL;
16927
16928             index_type = (actions & MDEREF_INDEX_MASK);
16929             switch (index_type) {
16930             case MDEREF_INDEX_none:
16931                 break;
16932             case MDEREF_INDEX_const:
16933                 if (is_hv)
16934                     index_const_sv = UNOP_AUX_item_sv(++items)
16935                 else
16936                     index_const_iv = (++items)->iv;
16937                 break;
16938             case MDEREF_INDEX_padsv:
16939                 index_targ = (++items)->pad_offset;
16940                 break;
16941             case MDEREF_INDEX_gvsv:
16942                 index_gv = (GV*)UNOP_AUX_item_sv(++items);
16943                 assert(isGV_with_GP(index_gv));
16944                 break;
16945             }
16946
16947             if (index_type != MDEREF_INDEX_none)
16948                 depth++;
16949
16950             if (   index_type == MDEREF_INDEX_none
16951                 || (actions & MDEREF_FLAG_last)
16952                 || (last && items >= last)
16953             )
16954                 break;
16955
16956             actions >>= MDEREF_SHIFT;
16957         } /* while */
16958
16959         if (PL_op == obase) {
16960             /* most likely index was undef */
16961
16962             *desc_p = (    (actions & MDEREF_FLAG_last)
16963                         && (obase->op_private
16964                                 & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)))
16965                         ?
16966                             (obase->op_private & OPpMULTIDEREF_EXISTS)
16967                                 ? "exists"
16968                                 : "delete"
16969                         : is_hv ? "hash element" : "array element";
16970             assert(index_type != MDEREF_INDEX_none);
16971             if (index_gv) {
16972                 if (GvSV(index_gv) == uninit_sv)
16973                     return varname(index_gv, '$', 0, NULL, 0,
16974                                                     FUV_SUBSCRIPT_NONE);
16975                 else
16976                     return NULL;
16977             }
16978             if (index_targ) {
16979                 if (PL_curpad[index_targ] == uninit_sv)
16980                     return varname(NULL, '$', index_targ,
16981                                     NULL, 0, FUV_SUBSCRIPT_NONE);
16982                 else
16983                     return NULL;
16984             }
16985             /* If we got to this point it was undef on a const subscript,
16986              * so magic probably involved, e.g. $ISA[0]. Give up. */
16987             return NULL;
16988         }
16989
16990         /* the SV returned by pp_multideref() was undef, if anything was */
16991
16992         if (depth != 1)
16993             break;
16994
16995         if (agg_targ)
16996             sv = PAD_SV(agg_targ);
16997         else if (agg_gv) {
16998             sv = is_hv ? MUTABLE_SV(GvHV(agg_gv)) : MUTABLE_SV(GvAV(agg_gv));
16999             if (!sv)
17000                 break;
17001             }
17002         else
17003             break;
17004
17005         if (index_type == MDEREF_INDEX_const) {
17006             if (match) {
17007                 if (SvMAGICAL(sv))
17008                     break;
17009                 if (is_hv) {
17010                     HE* he = hv_fetch_ent(MUTABLE_HV(sv), index_const_sv, 0, 0);
17011                     if (!he || HeVAL(he) != uninit_sv)
17012                         break;
17013                 }
17014                 else {
17015                     SV * const * const svp =
17016                             av_fetch(MUTABLE_AV(sv), index_const_iv, FALSE);
17017                     if (!svp || *svp != uninit_sv)
17018                         break;
17019                 }
17020             }
17021             return is_hv
17022                 ? varname(agg_gv, '%', agg_targ,
17023                                 index_const_sv, 0,    FUV_SUBSCRIPT_HASH)
17024                 : varname(agg_gv, '@', agg_targ,
17025                                 NULL, index_const_iv, FUV_SUBSCRIPT_ARRAY);
17026         }
17027         else {
17028             /* index is an var */
17029             if (is_hv) {
17030                 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17031                 if (keysv)
17032                     return varname(agg_gv, '%', agg_targ,
17033                                                 keysv, 0, FUV_SUBSCRIPT_HASH);
17034             }
17035             else {
17036                 const SSize_t index
17037                     = find_array_subscript((const AV *)sv, uninit_sv);
17038                 if (index >= 0)
17039                     return varname(agg_gv, '@', agg_targ,
17040                                         NULL, index, FUV_SUBSCRIPT_ARRAY);
17041             }
17042             /* look for an element not found */
17043             if (!SvMAGICAL(sv)) {
17044                 SV *index_sv = NULL;
17045                 if (index_targ) {
17046                     index_sv = PL_curpad[index_targ];
17047                 }
17048                 else if (index_gv) {
17049                     index_sv = GvSV(index_gv);
17050                 }
17051                 if (index_sv && !SvMAGICAL(index_sv) && !SvROK(index_sv)) {
17052                     if (is_hv) {
17053                         SV *report_index_sv = SvOK(index_sv) ? index_sv : &PL_sv_no;
17054                         HE *he = hv_fetch_ent(MUTABLE_HV(sv), report_index_sv, 0, 0);
17055                         if (!he) {
17056                             return varname(agg_gv, '%', agg_targ,
17057                                            report_index_sv, 0, FUV_SUBSCRIPT_HASH);
17058                         }
17059                     }
17060                     else {
17061                         SSize_t index = SvOK(index_sv) ? SvIV(index_sv) : 0;
17062                         SV * const * const svp =
17063                             av_fetch(MUTABLE_AV(sv), index, FALSE);
17064                         if (!svp) {
17065                             return varname(agg_gv, '@', agg_targ,
17066                                            NULL, index, FUV_SUBSCRIPT_ARRAY);
17067                         }
17068                     }
17069                 }
17070             }
17071             if (match)
17072                 break;
17073             return varname(agg_gv,
17074                 is_hv ? '%' : '@',
17075                 agg_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17076         }
17077         NOT_REACHED; /* NOTREACHED */
17078     }
17079
17080     case OP_AASSIGN:
17081         /* only examine RHS */
17082         return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv,
17083                                                                 match, desc_p);
17084
17085     case OP_OPEN:
17086         o = cUNOPx(obase)->op_first;
17087         if (   o->op_type == OP_PUSHMARK
17088            || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)
17089         )
17090             o = OpSIBLING(o);
17091
17092         if (!OpHAS_SIBLING(o)) {
17093             /* one-arg version of open is highly magical */
17094
17095             if (o->op_type == OP_GV) { /* open FOO; */
17096                 gv = cGVOPx_gv(o);
17097                 if (match && GvSV(gv) != uninit_sv)
17098                     break;
17099                 return varname(gv, '$', 0,
17100                             NULL, 0, FUV_SUBSCRIPT_NONE);
17101             }
17102             /* other possibilities not handled are:
17103              * open $x; or open my $x;  should return '${*$x}'
17104              * open expr;               should return '$'.expr ideally
17105              */
17106              break;
17107         }
17108         match = 1;
17109         goto do_op;
17110
17111     /* ops where $_ may be an implicit arg */
17112     case OP_TRANS:
17113     case OP_TRANSR:
17114     case OP_SUBST:
17115     case OP_MATCH:
17116         if ( !(obase->op_flags & OPf_STACKED)) {
17117             if (uninit_sv == DEFSV)
17118                 return newSVpvs_flags("$_", SVs_TEMP);
17119             else if (obase->op_targ
17120                   && uninit_sv == PAD_SVl(obase->op_targ))
17121                 return varname(NULL, '$', obase->op_targ, NULL, 0,
17122                                FUV_SUBSCRIPT_NONE);
17123         }
17124         goto do_op;
17125
17126     case OP_PRTF:
17127     case OP_PRINT:
17128     case OP_SAY:
17129         match = 1; /* print etc can return undef on defined args */
17130         /* skip filehandle as it can't produce 'undef' warning  */
17131         o = cUNOPx(obase)->op_first;
17132         if ((obase->op_flags & OPf_STACKED)
17133             &&
17134                (   o->op_type == OP_PUSHMARK
17135                || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)))
17136             o = OpSIBLING(OpSIBLING(o));
17137         goto do_op2;
17138
17139
17140     case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
17141     case OP_CUSTOM: /* XS or custom code could trigger random warnings */
17142
17143         /* the following ops are capable of returning PL_sv_undef even for
17144          * defined arg(s) */
17145
17146     case OP_BACKTICK:
17147     case OP_PIPE_OP:
17148     case OP_FILENO:
17149     case OP_BINMODE:
17150     case OP_TIED:
17151     case OP_GETC:
17152     case OP_SYSREAD:
17153     case OP_SEND:
17154     case OP_IOCTL:
17155     case OP_SOCKET:
17156     case OP_SOCKPAIR:
17157     case OP_BIND:
17158     case OP_CONNECT:
17159     case OP_LISTEN:
17160     case OP_ACCEPT:
17161     case OP_SHUTDOWN:
17162     case OP_SSOCKOPT:
17163     case OP_GETPEERNAME:
17164     case OP_FTRREAD:
17165     case OP_FTRWRITE:
17166     case OP_FTREXEC:
17167     case OP_FTROWNED:
17168     case OP_FTEREAD:
17169     case OP_FTEWRITE:
17170     case OP_FTEEXEC:
17171     case OP_FTEOWNED:
17172     case OP_FTIS:
17173     case OP_FTZERO:
17174     case OP_FTSIZE:
17175     case OP_FTFILE:
17176     case OP_FTDIR:
17177     case OP_FTLINK:
17178     case OP_FTPIPE:
17179     case OP_FTSOCK:
17180     case OP_FTBLK:
17181     case OP_FTCHR:
17182     case OP_FTTTY:
17183     case OP_FTSUID:
17184     case OP_FTSGID:
17185     case OP_FTSVTX:
17186     case OP_FTTEXT:
17187     case OP_FTBINARY:
17188     case OP_FTMTIME:
17189     case OP_FTATIME:
17190     case OP_FTCTIME:
17191     case OP_READLINK:
17192     case OP_OPEN_DIR:
17193     case OP_READDIR:
17194     case OP_TELLDIR:
17195     case OP_SEEKDIR:
17196     case OP_REWINDDIR:
17197     case OP_CLOSEDIR:
17198     case OP_GMTIME:
17199     case OP_ALARM:
17200     case OP_SEMGET:
17201     case OP_GETLOGIN:
17202     case OP_SUBSTR:
17203     case OP_AEACH:
17204     case OP_EACH:
17205     case OP_SORT:
17206     case OP_CALLER:
17207     case OP_DOFILE:
17208     case OP_PROTOTYPE:
17209     case OP_NCMP:
17210     case OP_SMARTMATCH:
17211     case OP_UNPACK:
17212     case OP_SYSOPEN:
17213     case OP_SYSSEEK:
17214         match = 1;
17215         goto do_op;
17216
17217     case OP_ENTERSUB:
17218     case OP_GOTO:
17219         /* XXX tmp hack: these two may call an XS sub, and currently
17220           XS subs don't have a SUB entry on the context stack, so CV and
17221           pad determination goes wrong, and BAD things happen. So, just
17222           don't try to determine the value under those circumstances.
17223           Need a better fix at dome point. DAPM 11/2007 */
17224         break;
17225
17226     case OP_FLIP:
17227     case OP_FLOP:
17228     {
17229         GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
17230         if (gv && GvSV(gv) == uninit_sv)
17231             return newSVpvs_flags("$.", SVs_TEMP);
17232         goto do_op;
17233     }
17234
17235     case OP_POS:
17236         /* def-ness of rval pos() is independent of the def-ness of its arg */
17237         if ( !(obase->op_flags & OPf_MOD))
17238             break;
17239         /* FALLTHROUGH */
17240
17241     case OP_SCHOMP:
17242     case OP_CHOMP:
17243         if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
17244             return newSVpvs_flags("${$/}", SVs_TEMP);
17245         /* FALLTHROUGH */
17246
17247     default:
17248     do_op:
17249         if (!(obase->op_flags & OPf_KIDS))
17250             break;
17251         o = cUNOPx(obase)->op_first;
17252
17253     do_op2:
17254         if (!o)
17255             break;
17256
17257         /* This loop checks all the kid ops, skipping any that cannot pos-
17258          * sibly be responsible for the uninitialized value; i.e., defined
17259          * constants and ops that return nothing.  If there is only one op
17260          * left that is not skipped, then we *know* it is responsible for
17261          * the uninitialized value.  If there is more than one op left, we
17262          * have to look for an exact match in the while() loop below.
17263          * Note that we skip padrange, because the individual pad ops that
17264          * it replaced are still in the tree, so we work on them instead.
17265          */
17266         o2 = NULL;
17267         for (kid=o; kid; kid = OpSIBLING(kid)) {
17268             const OPCODE type = kid->op_type;
17269             if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
17270               || (type == OP_NULL  && ! (kid->op_flags & OPf_KIDS))
17271               || (type == OP_PUSHMARK)
17272               || (type == OP_PADRANGE)
17273             )
17274             continue;
17275
17276             if (o2) { /* more than one found */
17277                 o2 = NULL;
17278                 break;
17279             }
17280             o2 = kid;
17281         }
17282         if (o2)
17283             return find_uninit_var(o2, uninit_sv, match, desc_p);
17284
17285         /* scan all args */
17286         while (o) {
17287             sv = find_uninit_var(o, uninit_sv, 1, desc_p);
17288             if (sv)
17289                 return sv;
17290             o = OpSIBLING(o);
17291         }
17292         break;
17293     }
17294     return NULL;
17295 }
17296
17297
17298 /*
17299 =for apidoc_section $warning
17300 =for apidoc report_uninit
17301
17302 Print appropriate "Use of uninitialized variable" warning.
17303
17304 =cut
17305 */
17306
17307 void
17308 Perl_report_uninit(pTHX_ const SV *uninit_sv)
17309 {
17310     const char *desc = NULL;
17311     SV* varname = NULL;
17312
17313     if (PL_op) {
17314         desc = PL_op->op_type == OP_STRINGIFY && PL_op->op_folded
17315                 ? "join or string"
17316                 : PL_op->op_type == OP_MULTICONCAT
17317                     && (PL_op->op_private & OPpMULTICONCAT_FAKE)
17318                 ? "sprintf"
17319                 : OP_DESC(PL_op);
17320         if (uninit_sv && PL_curpad) {
17321             varname = find_uninit_var(PL_op, uninit_sv, 0, &desc);
17322             if (varname)
17323                 sv_insert(varname, 0, 0, " ", 1);
17324         }
17325     }
17326     else if (PL_curstackinfo->si_type == PERLSI_SORT && cxstack_ix == 0)
17327         /* we've reached the end of a sort block or sub,
17328          * and the uninit value is probably what that code returned */
17329         desc = "sort";
17330
17331     /* PL_warn_uninit_sv is constant */
17332     GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
17333     if (desc)
17334         /* diag_listed_as: Use of uninitialized value%s */
17335         Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit_sv,
17336                 SVfARG(varname ? varname : &PL_sv_no),
17337                 " in ", desc);
17338     else
17339         Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
17340                 "", "", "");
17341     GCC_DIAG_RESTORE_STMT;
17342 }
17343
17344 /*
17345  * ex: set ts=8 sts=4 sw=4 et:
17346  */