This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
POSIX: MSVC defines all of these
[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) {                   \
235             PerlMemShared_free((sv)->sv_debug_file); \
236             sv->sv_debug_file = NULL;                \
237         }                                            \
238     } STMT_END
239 #  define DEBUG_SV_SERIAL(sv)                                               \
240     DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) del_SV\n",    \
241             PTR2UV(sv), (long)(sv)->sv_debug_serial))
242 #else
243 #  define FREE_SV_DEBUG_FILE(sv)
244 #  define DEBUG_SV_SERIAL(sv)   NOOP
245 #endif
246
247 /* Mark an SV head as unused, and add to free list.
248  *
249  * If SVf_BREAK is set, skip adding it to the free list, as this SV had
250  * its refcount artificially decremented during global destruction, so
251  * there may be dangling pointers to it. The last thing we want in that
252  * case is for it to be reused. */
253
254 #define plant_SV(p) \
255     STMT_START {                                        \
256         const U32 old_flags = SvFLAGS(p);                       \
257         MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__);  \
258         DEBUG_SV_SERIAL(p);                             \
259         FREE_SV_DEBUG_FILE(p);                          \
260         POISON_SV_HEAD(p);                              \
261         SvFLAGS(p) = SVTYPEMASK;                        \
262         if (!(old_flags & SVf_BREAK)) {         \
263             SvARENA_CHAIN_SET(p, PL_sv_root);   \
264             PL_sv_root = (p);                           \
265         }                                               \
266         --PL_sv_count;                                  \
267     } STMT_END
268
269
270 /* make some more SVs by adding another arena */
271
272 SV*
273 Perl_more_sv(pTHX)
274 {
275     SV* sv;
276     char *chunk;                /* must use New here to match call to */
277     Newx(chunk,PERL_ARENA_SIZE,char);  /* Safefree() in sv_free_arenas() */
278     sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
279     uproot_SV(sv);
280     return sv;
281 }
282
283 /* del_SV(): return an empty SV head to the free list */
284
285 #ifdef DEBUGGING
286
287 #define del_SV(p) \
288     STMT_START {                                        \
289         if (DEBUG_D_TEST)                               \
290             del_sv(p);                                  \
291         else                                            \
292             plant_SV(p);                                \
293     } STMT_END
294
295 STATIC void
296 S_del_sv(pTHX_ SV *p)
297 {
298     PERL_ARGS_ASSERT_DEL_SV;
299
300     if (DEBUG_D_TEST) {
301         SV* sva;
302         bool ok = 0;
303         for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
304             const SV * const sv = sva + 1;
305             const SV * const svend = &sva[SvREFCNT(sva)];
306             if (p >= sv && p < svend) {
307                 ok = 1;
308                 break;
309             }
310         }
311         if (!ok) {
312             Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
313                              "Attempt to free non-arena SV: 0x%" UVxf
314                              pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
315             return;
316         }
317     }
318     plant_SV(p);
319 }
320
321 #else /* ! DEBUGGING */
322
323 #define del_SV(p)   plant_SV(p)
324
325 #endif /* DEBUGGING */
326
327
328 /*
329 =for apidoc_section $SV
330
331 =for apidoc sv_add_arena
332
333 Given a chunk of memory, link it to the head of the list of arenas,
334 and split it into a list of free SVs.
335
336 =cut
337 */
338
339 static void
340 S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
341 {
342     SV *const sva = MUTABLE_SV(ptr);
343     SV* sv;
344     SV* svend;
345
346     PERL_ARGS_ASSERT_SV_ADD_ARENA;
347
348     /* The first SV in an arena isn't an SV. */
349     SvANY(sva) = (void *) PL_sv_arenaroot;              /* ptr to next arena */
350     SvREFCNT(sva) = size / sizeof(SV);          /* number of SV slots */
351     SvFLAGS(sva) = flags;                       /* FAKE if not to be freed */
352
353     PL_sv_arenaroot = sva;
354     PL_sv_root = sva + 1;
355
356     svend = &sva[SvREFCNT(sva) - 1];
357     sv = sva + 1;
358     while (sv < svend) {
359         SvARENA_CHAIN_SET(sv, (sv + 1));
360 #ifdef DEBUGGING
361         SvREFCNT(sv) = 0;
362 #endif
363         /* Must always set typemask because it's always checked in on cleanup
364            when the arenas are walked looking for objects.  */
365         SvFLAGS(sv) = SVTYPEMASK;
366         sv++;
367     }
368     SvARENA_CHAIN_SET(sv, 0);
369 #ifdef DEBUGGING
370     SvREFCNT(sv) = 0;
371 #endif
372     SvFLAGS(sv) = SVTYPEMASK;
373 }
374
375 /* visit(): call the named function for each non-free SV in the arenas
376  * whose flags field matches the flags/mask args. */
377
378 STATIC SSize_t
379 S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
380 {
381     SV* sva;
382     I32 visited = 0;
383
384     PERL_ARGS_ASSERT_VISIT;
385
386     for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
387         const SV * const svend = &sva[SvREFCNT(sva)];
388         SV* sv;
389         for (sv = sva + 1; sv < svend; ++sv) {
390             if (!SvIS_FREED(sv)
391                     && (sv->sv_flags & mask) == flags
392                     && SvREFCNT(sv))
393             {
394                 (*f)(aTHX_ sv);
395                 ++visited;
396             }
397         }
398     }
399     return visited;
400 }
401
402 #ifdef DEBUGGING
403
404 /* called by sv_report_used() for each live SV */
405
406 static void
407 do_report_used(pTHX_ SV *const sv)
408 {
409     if (!SvIS_FREED(sv)) {
410         PerlIO_printf(Perl_debug_log, "****\n");
411         sv_dump(sv);
412     }
413 }
414 #endif
415
416 /*
417 =for apidoc sv_report_used
418
419 Dump the contents of all SVs not yet freed (debugging aid).
420
421 =cut
422 */
423
424 void
425 Perl_sv_report_used(pTHX)
426 {
427 #ifdef DEBUGGING
428     visit(do_report_used, 0, 0);
429 #else
430     PERL_UNUSED_CONTEXT;
431 #endif
432 }
433
434 /* called by sv_clean_objs() for each live SV */
435
436 static void
437 do_clean_objs(pTHX_ SV *const ref)
438 {
439     assert (SvROK(ref));
440     {
441         SV * const target = SvRV(ref);
442         if (SvOBJECT(target)) {
443             DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
444             if (SvWEAKREF(ref)) {
445                 sv_del_backref(target, ref);
446                 SvWEAKREF_off(ref);
447                 SvRV_set(ref, NULL);
448             } else {
449                 SvROK_off(ref);
450                 SvRV_set(ref, NULL);
451                 SvREFCNT_dec_NN(target);
452             }
453         }
454     }
455 }
456
457
458 /* clear any slots in a GV which hold objects - except IO;
459  * called by sv_clean_objs() for each live GV */
460
461 static void
462 do_clean_named_objs(pTHX_ SV *const sv)
463 {
464     SV *obj;
465     assert(SvTYPE(sv) == SVt_PVGV);
466     assert(isGV_with_GP(sv));
467     if (!GvGP(sv))
468         return;
469
470     /* freeing GP entries may indirectly free the current GV;
471      * hold onto it while we mess with the GP slots */
472     SvREFCNT_inc(sv);
473
474     if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
475         DEBUG_D((PerlIO_printf(Perl_debug_log,
476                 "Cleaning named glob SV object:\n "), sv_dump(obj)));
477         GvSV(sv) = NULL;
478         SvREFCNT_dec_NN(obj);
479     }
480     if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
481         DEBUG_D((PerlIO_printf(Perl_debug_log,
482                 "Cleaning named glob AV object:\n "), sv_dump(obj)));
483         GvAV(sv) = NULL;
484         SvREFCNT_dec_NN(obj);
485     }
486     if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
487         DEBUG_D((PerlIO_printf(Perl_debug_log,
488                 "Cleaning named glob HV object:\n "), sv_dump(obj)));
489         GvHV(sv) = NULL;
490         SvREFCNT_dec_NN(obj);
491     }
492     if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
493         DEBUG_D((PerlIO_printf(Perl_debug_log,
494                 "Cleaning named glob CV object:\n "), sv_dump(obj)));
495         GvCV_set(sv, NULL);
496         SvREFCNT_dec_NN(obj);
497     }
498     SvREFCNT_dec_NN(sv); /* undo the inc above */
499 }
500
501 /* clear any IO slots in a GV which hold objects (except stderr, defout);
502  * called by sv_clean_objs() for each live GV */
503
504 static void
505 do_clean_named_io_objs(pTHX_ SV *const sv)
506 {
507     SV *obj;
508     assert(SvTYPE(sv) == SVt_PVGV);
509     assert(isGV_with_GP(sv));
510     if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
511         return;
512
513     SvREFCNT_inc(sv);
514     if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
515         DEBUG_D((PerlIO_printf(Perl_debug_log,
516                 "Cleaning named glob IO object:\n "), sv_dump(obj)));
517         GvIOp(sv) = NULL;
518         SvREFCNT_dec_NN(obj);
519     }
520     SvREFCNT_dec_NN(sv); /* undo the inc above */
521 }
522
523 /* Void wrapper to pass to visit() */
524 static void
525 do_curse(pTHX_ SV * const sv) {
526     if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
527      || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
528         return;
529     (void)curse(sv, 0);
530 }
531
532 /*
533 =for apidoc sv_clean_objs
534
535 Attempt to destroy all objects not yet freed.
536
537 =cut
538 */
539
540 void
541 Perl_sv_clean_objs(pTHX)
542 {
543     GV *olddef, *olderr;
544     PL_in_clean_objs = TRUE;
545     visit(do_clean_objs, SVf_ROK, SVf_ROK);
546     /* Some barnacles may yet remain, clinging to typeglobs.
547      * Run the non-IO destructors first: they may want to output
548      * error messages, close files etc */
549     visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
550     visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
551     /* And if there are some very tenacious barnacles clinging to arrays,
552        closures, or what have you.... */
553     visit(do_curse, SVs_OBJECT, SVs_OBJECT);
554     olddef = PL_defoutgv;
555     PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
556     if (olddef && isGV_with_GP(olddef))
557         do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
558     olderr = PL_stderrgv;
559     PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
560     if (olderr && isGV_with_GP(olderr))
561         do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
562     SvREFCNT_dec(olddef);
563     PL_in_clean_objs = FALSE;
564 }
565
566 /* called by sv_clean_all() for each live SV */
567
568 static void
569 do_clean_all(pTHX_ SV *const sv)
570 {
571     if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
572         /* don't clean pid table and strtab */
573         return;
574     }
575     DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%" UVxf "\n", PTR2UV(sv)) ));
576     SvFLAGS(sv) |= SVf_BREAK;
577     SvREFCNT_dec_NN(sv);
578 }
579
580 /*
581 =for apidoc sv_clean_all
582
583 Decrement the refcnt of each remaining SV, possibly triggering a
584 cleanup.  This function may have to be called multiple times to free
585 SVs which are in complex self-referential hierarchies.
586
587 =cut
588 */
589
590 SSize_t
591 Perl_sv_clean_all(pTHX)
592 {
593     SSize_t cleaned;
594     PL_in_clean_all = TRUE;
595     cleaned = visit(do_clean_all, 0,0);
596     return cleaned;
597 }
598
599 /*
600   ARENASETS: a meta-arena implementation which separates arena-info
601   into struct arena_set, which contains an array of struct
602   arena_descs, each holding info for a single arena.  By separating
603   the meta-info from the arena, we recover the 1st slot, formerly
604   borrowed for list management.  The arena_set is about the size of an
605   arena, avoiding the needless malloc overhead of a naive linked-list.
606
607   The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
608   memory in the last arena-set (1/2 on average).  In trade, we get
609   back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
610   smaller types).  The recovery of the wasted space allows use of
611   small arenas for large, rare body types, by changing array* fields
612   in body_details_by_type[] below.
613 */
614 struct arena_desc {
615     char       *arena;          /* the raw storage, allocated aligned */
616     size_t      size;           /* its size ~4k typ */
617     svtype      utype;          /* bodytype stored in arena */
618 };
619
620 struct arena_set;
621
622 /* Get the maximum number of elements in set[] such that struct arena_set
623    will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
624    therefore likely to be 1 aligned memory page.  */
625
626 #define ARENAS_PER_SET  ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
627                           - 2 * sizeof(int)) / sizeof (struct arena_desc))
628
629 struct arena_set {
630     struct arena_set* next;
631     unsigned int   set_size;    /* ie ARENAS_PER_SET */
632     unsigned int   curr;        /* index of next available arena-desc */
633     struct arena_desc set[ARENAS_PER_SET];
634 };
635
636 /*
637 =for apidoc sv_free_arenas
638
639 Deallocate the memory used by all arenas.  Note that all the individual SV
640 heads and bodies within the arenas must already have been freed.
641
642 =cut
643
644 */
645 void
646 Perl_sv_free_arenas(pTHX)
647 {
648     SV* sva;
649     SV* svanext;
650     unsigned int i;
651
652     /* Free arenas here, but be careful about fake ones.  (We assume
653        contiguity of the fake ones with the corresponding real ones.) */
654
655     for (sva = PL_sv_arenaroot; sva; sva = svanext) {
656         svanext = MUTABLE_SV(SvANY(sva));
657         while (svanext && SvFAKE(svanext))
658             svanext = MUTABLE_SV(SvANY(svanext));
659
660         if (!SvFAKE(sva))
661             Safefree(sva);
662     }
663
664     {
665         struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
666
667         while (aroot) {
668             struct arena_set *current = aroot;
669             i = aroot->curr;
670             while (i--) {
671                 assert(aroot->set[i].arena);
672                 Safefree(aroot->set[i].arena);
673             }
674             aroot = aroot->next;
675             Safefree(current);
676         }
677     }
678     PL_body_arenas = 0;
679
680     i = PERL_ARENA_ROOTS_SIZE;
681     while (i--)
682         PL_body_roots[i] = 0;
683
684     PL_sv_arenaroot = 0;
685     PL_sv_root = 0;
686 }
687
688 /*
689   Historically, here were mid-level routines that manage the
690   allocation of bodies out of the various arenas. Some of these
691   routines and related definitions remain here, but others were
692   moved into sv_inline.h to facilitate inlining of newSV_type().
693
694   There are 4 kinds of arenas:
695
696   1. SV-head arenas, which are discussed and handled above
697   2. regular body arenas
698   3. arenas for reduced-size bodies
699   4. Hash-Entry arenas
700
701   Arena types 2 & 3 are chained by body-type off an array of
702   arena-root pointers, which is indexed by svtype.  Some of the
703   larger/less used body types are malloced singly, since a large
704   unused block of them is wasteful.  Also, several svtypes don't have
705   bodies; the data fits into the sv-head itself.  The arena-root
706   pointer thus has a few unused root-pointers (which may be hijacked
707   later for arena type 4)
708
709   3 differs from 2 as an optimization; some body types have several
710   unused fields in the front of the structure (which are kept in-place
711   for consistency).  These bodies can be allocated in smaller chunks,
712   because the leading fields arent accessed.  Pointers to such bodies
713   are decremented to point at the unused 'ghost' memory, knowing that
714   the pointers are used with offsets to the real memory.
715
716 Allocation of SV-bodies is similar to SV-heads, differing as follows;
717 the allocation mechanism is used for many body types, so is somewhat
718 more complicated, it uses arena-sets, and has no need for still-live
719 SV detection.
720
721 At the outermost level, (new|del)_X*V macros return bodies of the
722 appropriate type.  These macros call either (new|del)_body_type or
723 (new|del)_body_allocated macro pairs, depending on specifics of the
724 type.  Most body types use the former pair, the latter pair is used to
725 allocate body types with "ghost fields".
726
727 "ghost fields" are fields that are unused in certain types, and
728 consequently don't need to actually exist.  They are declared because
729 they're part of a "base type", which allows use of functions as
730 methods.  The simplest examples are AVs and HVs, 2 aggregate types
731 which don't use the fields which support SCALAR semantics.
732
733 For these types, the arenas are carved up into appropriately sized
734 chunks, we thus avoid wasted memory for those unaccessed members.
735 When bodies are allocated, we adjust the pointer back in memory by the
736 size of the part not allocated, so it's as if we allocated the full
737 structure.  (But things will all go boom if you write to the part that
738 is "not there", because you'll be overwriting the last members of the
739 preceding structure in memory.)
740
741 We calculate the correction using the STRUCT_OFFSET macro on the first
742 member present.  If the allocated structure is smaller (no initial NV
743 actually allocated) then the net effect is to subtract the size of the NV
744 from the pointer, to return a new pointer as if an initial NV were actually
745 allocated.  (We were using structures named *_allocated for this, but
746 this turned out to be a subtle bug, because a structure without an NV
747 could have a lower alignment constraint, but the compiler is allowed to
748 optimised accesses based on the alignment constraint of the actual pointer
749 to the full structure, for example, using a single 64 bit load instruction
750 because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
751
752 This is the same trick as was used for NV and IV bodies.  Ironically it
753 doesn't need to be used for NV bodies any more, because NV is now at
754 the start of the structure.  IV bodies, and also in some builds NV bodies,
755 don't need it either, because they are no longer allocated.
756
757 In turn, the new_body_* allocators call S_new_body(), which invokes
758 new_body_from_arena macro, which takes a lock, and takes a body off the
759 linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
760 necessary to refresh an empty list.  Then the lock is released, and
761 the body is returned.
762
763 Perl_more_bodies allocates a new arena, and carves it up into an array of N
764 bodies, which it strings into a linked list.  It looks up arena-size
765 and body-size from the body_details table described below, thus
766 supporting the multiple body-types.
767
768 If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
769 the (new|del)_X*V macros are mapped directly to malloc/free.
770
771 For each sv-type, struct body_details bodies_by_type[] carries
772 parameters which control these aspects of SV handling:
773
774 Arena_size determines whether arenas are used for this body type, and if
775 so, how big they are.  PURIFY or PERL_ARENA_SIZE=0 set this field to
776 zero, forcing individual mallocs and frees.
777
778 Body_size determines how big a body is, and therefore how many fit into
779 each arena.  Offset carries the body-pointer adjustment needed for
780 "ghost fields", and is used in *_allocated macros.
781
782 But its main purpose is to parameterize info needed in
783 Perl_sv_upgrade().  The info here dramatically simplifies the function
784 vs the implementation in 5.8.8, making it table-driven.  All fields
785 are used for this, except for arena_size.
786
787 For the sv-types that have no bodies, arenas are not used, so those
788 PL_body_roots[sv_type] are unused, and can be overloaded.  In
789 something of a special case, SVt_NULL is borrowed for HE arenas;
790 PL_body_roots[HE_ARENA_ROOT_IX=SVt_NULL] is filled by S_more_he, but the
791 bodies_by_type[SVt_NULL] slot is not used, as the table is not
792 available in hv.c. Similarly SVt_IV is re-used for HVAUX_ARENA_ROOT_IX.
793
794 */
795
796 /* return a thing to the free list */
797
798 #define del_body(thing, root)                           \
799     STMT_START {                                        \
800         void ** const thing_copy = (void **)thing;      \
801         *thing_copy = *root;                            \
802         *root = (void*)thing_copy;                      \
803     } STMT_END
804
805
806 void *
807 Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
808                   const size_t arena_size)
809 {
810     void ** const root = &PL_body_roots[sv_type];
811     struct arena_desc *adesc;
812     struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
813     unsigned int curr;
814     char *start;
815     const char *end;
816     const size_t good_arena_size = Perl_malloc_good_size(arena_size);
817 #if defined(DEBUGGING)
818     static bool done_sanity_check;
819
820     if (!done_sanity_check) {
821         unsigned int i = SVt_LAST;
822
823         done_sanity_check = TRUE;
824
825         while (i--)
826             assert (bodies_by_type[i].type == i);
827     }
828 #endif
829
830     assert(arena_size);
831
832     /* may need new arena-set to hold new arena */
833     if (!aroot || aroot->curr >= aroot->set_size) {
834         struct arena_set *newroot;
835         Newxz(newroot, 1, struct arena_set);
836         newroot->set_size = ARENAS_PER_SET;
837         newroot->next = aroot;
838         aroot = newroot;
839         PL_body_arenas = (void *) newroot;
840         DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
841     }
842
843     /* ok, now have arena-set with at least 1 empty/available arena-desc */
844     curr = aroot->curr++;
845     adesc = &(aroot->set[curr]);
846     assert(!adesc->arena);
847
848     Newx(adesc->arena, good_arena_size, char);
849     adesc->size = good_arena_size;
850     adesc->utype = sv_type;
851     DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %" UVuf "\n",
852                           curr, (void*)adesc->arena, (UV)good_arena_size));
853
854     start = (char *) adesc->arena;
855
856     /* Get the address of the byte after the end of the last body we can fit.
857        Remember, this is integer division:  */
858     end = start + good_arena_size / body_size * body_size;
859
860     /* computed count doesn't reflect the 1st slot reservation */
861 #if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
862     DEBUG_m(PerlIO_printf(Perl_debug_log,
863                           "arena %p end %p arena-size %d (from %d) type %d "
864                           "size %d ct %d\n",
865                           (void*)start, (void*)end, (int)good_arena_size,
866                           (int)arena_size, sv_type, (int)body_size,
867                           (int)good_arena_size / (int)body_size));
868 #else
869     DEBUG_m(PerlIO_printf(Perl_debug_log,
870                           "arena %p end %p arena-size %d type %d size %d ct %d\n",
871                           (void*)start, (void*)end,
872                           (int)arena_size, sv_type, (int)body_size,
873                           (int)good_arena_size / (int)body_size));
874 #endif
875     *root = (void *)start;
876
877     while (1) {
878         /* Where the next body would start:  */
879         char * const next = start + body_size;
880
881         if (next >= end) {
882             /* This is the last body:  */
883             assert(next == end);
884
885             *(void **)start = 0;
886             return *root;
887         }
888
889         *(void**) start = (void *)next;
890         start = next;
891     }
892 }
893
894 /*
895 =for apidoc sv_upgrade
896
897 Upgrade an SV to a more complex form.  Generally adds a new body type to the
898 SV, then copies across as much information as possible from the old body.
899 It croaks if the SV is already in a more complex form than requested.  You
900 generally want to use the C<SvUPGRADE> macro wrapper, which checks the type
901 before calling C<sv_upgrade>, and hence does not croak.  See also
902 C<L</svtype>>.
903
904 =cut
905 */
906
907 void
908 Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
909 {
910     void*       old_body;
911     void*       new_body;
912     const svtype old_type = SvTYPE(sv);
913     const struct body_details *new_type_details;
914     const struct body_details *old_type_details
915         = bodies_by_type + old_type;
916     SV *referent = NULL;
917
918     PERL_ARGS_ASSERT_SV_UPGRADE;
919
920     if (old_type == new_type)
921         return;
922
923     /* This clause was purposefully added ahead of the early return above to
924        the shared string hackery for (sort {$a <=> $b} keys %hash), with the
925        inference by Nick I-S that it would fix other troublesome cases. See
926        changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
927
928        Given that shared hash key scalars are no longer PVIV, but PV, there is
929        no longer need to unshare so as to free up the IVX slot for its proper
930        purpose. So it's safe to move the early return earlier.  */
931
932     if (new_type > SVt_PVMG && SvIsCOW(sv)) {
933         sv_force_normal_flags(sv, 0);
934     }
935
936     old_body = SvANY(sv);
937
938     /* Copying structures onto other structures that have been neatly zeroed
939        has a subtle gotcha. Consider XPVMG
940
941        +------+------+------+------+------+-------+-------+
942        |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH |
943        +------+------+------+------+------+-------+-------+
944        0      4      8     12     16     20      24      28
945
946        where NVs are aligned to 8 bytes, so that sizeof that structure is
947        actually 32 bytes long, with 4 bytes of padding at the end:
948
949        +------+------+------+------+------+-------+-------+------+
950        |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH | ???  |
951        +------+------+------+------+------+-------+-------+------+
952        0      4      8     12     16     20      24      28     32
953
954        so what happens if you allocate memory for this structure:
955
956        +------+------+------+------+------+-------+-------+------+------+...
957        |     NV      | CUR  | LEN  |  IV  | MAGIC | STASH |  GP  | NAME |
958        +------+------+------+------+------+-------+-------+------+------+...
959        0      4      8     12     16     20      24      28     32     36
960
961        zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
962        expect, because you copy the area marked ??? onto GP. Now, ??? may have
963        started out as zero once, but it's quite possible that it isn't. So now,
964        rather than a nicely zeroed GP, you have it pointing somewhere random.
965        Bugs ensue.
966
967        (In fact, GP ends up pointing at a previous GP structure, because the
968        principle cause of the padding in XPVMG getting garbage is a copy of
969        sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
970        this happens to be moot because XPVGV has been re-ordered, with GP
971        no longer after STASH)
972
973        So we are careful and work out the size of used parts of all the
974        structures.  */
975
976     switch (old_type) {
977     case SVt_NULL:
978         break;
979     case SVt_IV:
980         if (SvROK(sv)) {
981             referent = SvRV(sv);
982             old_type_details = &fake_rv;
983             if (new_type == SVt_NV)
984                 new_type = SVt_PVNV;
985         } else {
986             if (new_type < SVt_PVIV) {
987                 new_type = (new_type == SVt_NV)
988                     ? SVt_PVNV : SVt_PVIV;
989             }
990         }
991         break;
992     case SVt_NV:
993         if (new_type < SVt_PVNV) {
994             new_type = SVt_PVNV;
995         }
996         break;
997     case SVt_PV:
998         assert(new_type > SVt_PV);
999         STATIC_ASSERT_STMT(SVt_IV < SVt_PV);
1000         STATIC_ASSERT_STMT(SVt_NV < SVt_PV);
1001         break;
1002     case SVt_PVIV:
1003         break;
1004     case SVt_PVNV:
1005         break;
1006     case SVt_PVMG:
1007         /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1008            there's no way that it can be safely upgraded, because perl.c
1009            expects to Safefree(SvANY(PL_mess_sv))  */
1010         assert(sv != PL_mess_sv);
1011         break;
1012     default:
1013         if (UNLIKELY(old_type_details->cant_upgrade))
1014             Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1015                        sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
1016     }
1017
1018     if (UNLIKELY(old_type > new_type))
1019         Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1020                 (int)old_type, (int)new_type);
1021
1022     new_type_details = bodies_by_type + new_type;
1023
1024     SvFLAGS(sv) &= ~SVTYPEMASK;
1025     SvFLAGS(sv) |= new_type;
1026
1027     /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1028        the return statements above will have triggered.  */
1029     assert (new_type != SVt_NULL);
1030     switch (new_type) {
1031     case SVt_IV:
1032         assert(old_type == SVt_NULL);
1033         SET_SVANY_FOR_BODYLESS_IV(sv);
1034         SvIV_set(sv, 0);
1035         return;
1036     case SVt_NV:
1037         assert(old_type == SVt_NULL);
1038 #if NVSIZE <= IVSIZE
1039         SET_SVANY_FOR_BODYLESS_NV(sv);
1040 #else
1041         SvANY(sv) = new_XNV();
1042 #endif
1043         SvNV_set(sv, 0);
1044         return;
1045     case SVt_PVHV:
1046     case SVt_PVAV:
1047     case SVt_PVOBJ:
1048         assert(new_type_details->body_size);
1049
1050 #ifndef PURIFY
1051         assert(new_type_details->arena);
1052         assert(new_type_details->arena_size);
1053         /* This points to the start of the allocated area.  */
1054         new_body = S_new_body(aTHX_ new_type);
1055         /* xpvav and xpvhv have no offset, so no need to adjust new_body */
1056         assert(!(new_type_details->offset));
1057 #else
1058         /* We always allocated the full length item with PURIFY. To do this
1059            we fake things so that arena is false for all 16 types..  */
1060         new_body = new_NOARENAZ(new_type_details);
1061 #endif
1062         SvANY(sv) = new_body;
1063         switch(new_type) {
1064         case SVt_PVAV:
1065             {
1066                 XPVAV pvav = {
1067                     .xmg_stash = NULL,
1068                     .xmg_u = {.xmg_magic = NULL},
1069                     .xav_fill = -1, .xav_max = -1, .xav_alloc = 0
1070                 };
1071                 *((XPVAV*) SvANY(sv)) = pvav;
1072             }
1073
1074             AvREAL_only(sv);
1075             break;
1076         case SVt_PVHV:
1077             {
1078                 XPVHV pvhv = {
1079                     .xmg_stash = NULL,
1080                     .xmg_u = {.xmg_magic = NULL},
1081                     .xhv_keys = 0,
1082                     /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1083                     .xhv_max = PERL_HASH_DEFAULT_HvMAX
1084                 };
1085                 *((XPVHV*) SvANY(sv)) = pvhv;
1086             }
1087
1088             assert(!SvOK(sv));
1089             SvOK_off(sv);
1090 #ifndef NODEFAULT_SHAREKEYS
1091             HvSHAREKEYS_on(sv);         /* key-sharing on by default */
1092 #endif
1093             break;
1094         case SVt_PVOBJ:
1095             {
1096                 XPVOBJ pvo = {
1097                     .xmg_stash = NULL, .xmg_u = {.xmg_magic = NULL},
1098                     .xobject_maxfield = -1,
1099                     .xobject_iter_sv_at = 0,
1100                     .xobject_fields = NULL,
1101                 };
1102                 *((XPVOBJ*) SvANY(sv)) = pvo;
1103             }
1104             break;
1105         default:
1106             NOT_REACHED;
1107         }
1108
1109         /* SVt_NULL isn't the only thing upgraded to AV or HV.
1110            The target created by newSVrv also is, and it can have magic.
1111            However, it never has SvPVX set.
1112         */
1113         if (old_type == SVt_IV) {
1114             assert(!SvROK(sv));
1115         } else if (old_type >= SVt_PV) {
1116             assert(SvPVX_const(sv) == 0);
1117         }
1118
1119         if (old_type >= SVt_PVMG) {
1120             SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
1121             SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1122         } else {
1123             sv->sv_u.svu_array = NULL; /* or svu_hash  */
1124         }
1125         break;
1126
1127     case SVt_PVIV:
1128         /* XXX Is this still needed?  Was it ever needed?   Surely as there is
1129            no route from NV to PVIV, NOK can never be true  */
1130         assert(!SvNOKp(sv));
1131         assert(!SvNOK(sv));
1132         /* FALLTHROUGH */
1133     case SVt_PVIO:
1134     case SVt_PVFM:
1135     case SVt_PVGV:
1136     case SVt_PVCV:
1137     case SVt_PVLV:
1138     case SVt_INVLIST:
1139     case SVt_REGEXP:
1140     case SVt_PVMG:
1141     case SVt_PVNV:
1142     case SVt_PV:
1143
1144         assert(new_type_details->body_size);
1145         /* We always allocated the full length item with PURIFY. To do this
1146            we fake things so that arena is false for all 16 types..  */
1147 #ifndef PURIFY
1148         if(new_type_details->arena) {
1149             /* This points to the start of the allocated area.  */
1150             new_body = S_new_body(aTHX_ new_type);
1151             Zero(new_body, new_type_details->body_size, char);
1152             new_body = ((char *)new_body) - new_type_details->offset;
1153         } else
1154 #endif
1155         {
1156             new_body = new_NOARENAZ(new_type_details);
1157         }
1158         SvANY(sv) = new_body;
1159
1160         if (old_type_details->copy) {
1161             /* There is now the potential for an upgrade from something without
1162                an offset (PVNV or PVMG) to something with one (PVCV, PVFM)  */
1163             int offset = old_type_details->offset;
1164             int length = old_type_details->copy;
1165
1166             if (new_type_details->offset > old_type_details->offset) {
1167                 const int difference
1168                     = new_type_details->offset - old_type_details->offset;
1169                 offset += difference;
1170                 length -= difference;
1171             }
1172             assert (length >= 0);
1173
1174             Copy((char *)old_body + offset, (char *)new_body + offset, length,
1175                  char);
1176         }
1177
1178 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1179         /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
1180          * correct 0.0 for us.  Otherwise, if the old body didn't have an
1181          * NV slot, but the new one does, then we need to initialise the
1182          * freshly created NV slot with whatever the correct bit pattern is
1183          * for 0.0  */
1184         if (old_type_details->zero_nv && !new_type_details->zero_nv
1185             && !isGV_with_GP(sv))
1186             SvNV_set(sv, 0);
1187 #endif
1188
1189         if (UNLIKELY(new_type == SVt_PVIO)) {
1190             IO * const io = MUTABLE_IO(sv);
1191             GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
1192
1193             SvOBJECT_on(io);
1194             /* Clear the stashcache because a new IO could overrule a package
1195                name */
1196             DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
1197             hv_clear(PL_stashcache);
1198
1199             SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
1200             IoPAGE_LEN(sv) = 60;
1201         }
1202         if (old_type < SVt_PV) {
1203             /* referent will be NULL unless the old type was SVt_IV emulating
1204                SVt_RV */
1205             sv->sv_u.svu_rv = referent;
1206         }
1207         break;
1208     default:
1209         Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1210                    (unsigned long)new_type);
1211     }
1212
1213     /* if this is zero, this is a body-less SVt_NULL, SVt_IV/SVt_RV,
1214        and sometimes SVt_NV */
1215     if (old_type_details->body_size) {
1216 #ifdef PURIFY
1217         safefree(old_body);
1218 #else
1219         /* Note that there is an assumption that all bodies of types that
1220            can be upgraded came from arenas. Only the more complex non-
1221            upgradable types are allowed to be directly malloc()ed.  */
1222         assert(old_type_details->arena);
1223         del_body((void*)((char*)old_body + old_type_details->offset),
1224                  &PL_body_roots[old_type]);
1225 #endif
1226     }
1227 }
1228
1229 struct xpvhv_aux*
1230 Perl_hv_auxalloc(pTHX_ HV *hv) {
1231     const struct body_details *old_type_details = bodies_by_type + SVt_PVHV;
1232     void *old_body;
1233     void *new_body;
1234
1235     PERL_ARGS_ASSERT_HV_AUXALLOC;
1236     assert(SvTYPE(hv) == SVt_PVHV);
1237     assert(!HvHasAUX(hv));
1238
1239 #ifdef PURIFY
1240     new_body = new_NOARENAZ(&fake_hv_with_aux);
1241 #else
1242     new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
1243 #endif
1244
1245     old_body = SvANY(hv);
1246
1247     Copy((char *)old_body + old_type_details->offset,
1248          (char *)new_body + fake_hv_with_aux.offset,
1249          old_type_details->copy,
1250          char);
1251
1252 #ifdef PURIFY
1253     safefree(old_body);
1254 #else
1255     assert(old_type_details->arena);
1256     del_body((void*)((char*)old_body + old_type_details->offset),
1257              &PL_body_roots[SVt_PVHV]);
1258 #endif
1259
1260     SvANY(hv) = (XPVHV *) new_body;
1261     SvFLAGS(hv) |= SVphv_HasAUX;
1262     return HvAUX(hv);
1263 }
1264
1265 /*
1266 =for apidoc sv_backoff
1267
1268 Remove any string offset.  You should normally use the C<SvOOK_off> macro
1269 wrapper instead.
1270
1271 =cut
1272 */
1273
1274 /* prior to 5.000 stable, this function returned the new OOK-less SvFLAGS
1275    prior to 5.23.4 this function always returned 0
1276 */
1277
1278 void
1279 Perl_sv_backoff(SV *const sv)
1280 {
1281     STRLEN delta;
1282     const char * const s = SvPVX_const(sv);
1283
1284     PERL_ARGS_ASSERT_SV_BACKOFF;
1285
1286     assert(SvOOK(sv));
1287     assert(SvTYPE(sv) != SVt_PVHV);
1288     assert(SvTYPE(sv) != SVt_PVAV);
1289
1290     SvOOK_offset(sv, delta);
1291
1292     SvLEN_set(sv, SvLEN(sv) + delta);
1293     SvPV_set(sv, SvPVX(sv) - delta);
1294     SvFLAGS(sv) &= ~SVf_OOK;
1295     Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1296     return;
1297 }
1298
1299
1300 /* forward declaration */
1301 static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
1302
1303
1304 /*
1305 =for apidoc sv_grow
1306
1307 Expands the character buffer in the SV.  If necessary, uses C<sv_unref> and
1308 upgrades the SV to C<SVt_PV>.  Returns a pointer to the character buffer.
1309 Use the C<SvGROW> wrapper instead.
1310
1311 =cut
1312 */
1313
1314
1315 char *
1316 Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
1317 {
1318     char *s;
1319
1320     PERL_ARGS_ASSERT_SV_GROW;
1321
1322     if (SvROK(sv))
1323         sv_unref(sv);
1324     if (SvTYPE(sv) < SVt_PV) {
1325         sv_upgrade(sv, SVt_PV);
1326         s = SvPVX_mutable(sv);
1327     }
1328     else if (SvOOK(sv)) {       /* pv is offset? */
1329         sv_backoff(sv);
1330         s = SvPVX_mutable(sv);
1331         if (newlen > SvLEN(sv))
1332             newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1333     }
1334     else
1335     {
1336         if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
1337         s = SvPVX_mutable(sv);
1338     }
1339
1340 #ifdef PERL_COPY_ON_WRITE
1341     /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1342      * to store the COW count. So in general, allocate one more byte than
1343      * asked for, to make it likely this byte is always spare: and thus
1344      * make more strings COW-able.
1345      *
1346      * Only increment if the allocation isn't MEM_SIZE_MAX,
1347      * otherwise it will wrap to 0.
1348      */
1349     if ( newlen != MEM_SIZE_MAX )
1350         newlen++;
1351 #endif
1352
1353 #if defined(PERL_USE_MALLOC_SIZE) && defined(Perl_safesysmalloc_size)
1354 #define PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1355 #endif
1356
1357     if (newlen > SvLEN(sv)) {           /* need more room? */
1358         STRLEN minlen = SvCUR(sv);
1359         minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + PERL_STRLEN_NEW_MIN;
1360         if (newlen < minlen)
1361             newlen = minlen;
1362 #ifndef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1363
1364         /* Don't round up on the first allocation, as odds are pretty good that
1365          * the initial request is accurate as to what is really needed */
1366         if (SvLEN(sv)) {
1367             STRLEN rounded = PERL_STRLEN_ROUNDUP(newlen);
1368             if (rounded > newlen)
1369                 newlen = rounded;
1370         }
1371 #endif
1372         if (SvLEN(sv) && s) {
1373             s = (char*)saferealloc(s, newlen);
1374         }
1375         else {
1376             s = (char*)safemalloc(newlen);
1377             if (SvPVX_const(sv) && SvCUR(sv)) {
1378                 Move(SvPVX_const(sv), s, SvCUR(sv), char);
1379             }
1380         }
1381         SvPV_set(sv, s);
1382 #ifdef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1383         /* Do this here, do it once, do it right, and then we will never get
1384            called back into sv_grow() unless there really is some growing
1385            needed.  */
1386         SvLEN_set(sv, Perl_safesysmalloc_size(s));
1387 #else
1388         SvLEN_set(sv, newlen);
1389 #endif
1390     }
1391     return s;
1392 }
1393
1394 /*
1395 =for apidoc sv_grow_fresh
1396
1397 A cut-down version of sv_grow intended only for when sv is a freshly-minted
1398 SVt_PV, SVt_PVIV, SVt_PVNV, or SVt_PVMG. i.e. sv has the default flags, has
1399 never been any other type, and does not have an existing string. Basically,
1400 just assigns a char buffer and returns a pointer to it.
1401
1402 =cut
1403 */
1404
1405
1406 char *
1407 Perl_sv_grow_fresh(pTHX_ SV *const sv, STRLEN newlen)
1408 {
1409     char *s;
1410
1411     PERL_ARGS_ASSERT_SV_GROW_FRESH;
1412
1413     assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
1414     assert(!SvROK(sv));
1415     assert(!SvOOK(sv));
1416     assert(!SvIsCOW(sv));
1417     assert(!SvLEN(sv));
1418     assert(!SvCUR(sv));
1419
1420 #ifdef PERL_COPY_ON_WRITE
1421     /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1422      * to store the COW count. So in general, allocate one more byte than
1423      * asked for, to make it likely this byte is always spare: and thus
1424      * make more strings COW-able.
1425      *
1426      * Only increment if the allocation isn't MEM_SIZE_MAX,
1427      * otherwise it will wrap to 0.
1428      */
1429     if ( newlen != MEM_SIZE_MAX )
1430         newlen++;
1431 #endif
1432
1433     if (newlen < PERL_STRLEN_NEW_MIN)
1434         newlen = PERL_STRLEN_NEW_MIN;
1435
1436     s = (char*)safemalloc(newlen);
1437     SvPV_set(sv, s);
1438
1439     /* No PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC here, since many strings */
1440     /* will never be grown once set. Let the real sv_grow worry about that. */
1441     SvLEN_set(sv, newlen);
1442     return s;
1443 }
1444
1445 /*
1446 =for apidoc sv_setiv
1447 =for apidoc_item sv_setiv_mg
1448
1449 These copy an integer into the given SV, upgrading first if necessary.
1450
1451 They differ only in that C<sv_setiv_mg> handles 'set' magic; C<sv_setiv> does
1452 not.
1453
1454 =cut
1455 */
1456
1457 void
1458 Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
1459 {
1460     PERL_ARGS_ASSERT_SV_SETIV;
1461
1462     SV_CHECK_THINKFIRST_COW_DROP(sv);
1463     switch (SvTYPE(sv)) {
1464 #if NVSIZE <= IVSIZE
1465     case SVt_NULL:
1466     case SVt_NV:
1467         SET_SVANY_FOR_BODYLESS_IV(sv);
1468         SvFLAGS(sv) &= ~SVTYPEMASK;
1469         SvFLAGS(sv) |= SVt_IV;
1470         break;
1471 #else
1472     case SVt_NULL:
1473         SET_SVANY_FOR_BODYLESS_IV(sv);
1474         SvFLAGS(sv) &= ~SVTYPEMASK;
1475         SvFLAGS(sv) |= SVt_IV;
1476         break;
1477     case SVt_NV:
1478         sv_upgrade(sv, SVt_IV);
1479         break;
1480 #endif
1481     case SVt_PV:
1482         sv_upgrade(sv, SVt_PVIV);
1483         break;
1484
1485     case SVt_PVGV:
1486         if (!isGV_with_GP(sv))
1487             break;
1488         /* FALLTHROUGH */
1489     case SVt_PVAV:
1490     case SVt_PVHV:
1491     case SVt_PVCV:
1492     case SVt_PVFM:
1493     case SVt_PVIO:
1494         /* diag_listed_as: Can't coerce %s to %s in %s */
1495         Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1496                    OP_DESC(PL_op));
1497         NOT_REACHED; /* NOTREACHED */
1498         break;
1499     default: NOOP;
1500     }
1501     (void)SvIOK_only(sv);                       /* validate number */
1502     SvIV_set(sv, i);
1503     SvTAINT(sv);
1504 }
1505
1506 void
1507 Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
1508 {
1509     PERL_ARGS_ASSERT_SV_SETIV_MG;
1510
1511     sv_setiv(sv,i);
1512     SvSETMAGIC(sv);
1513 }
1514
1515 /*
1516 =for apidoc sv_setuv
1517 =for apidoc_item sv_setuv_mg
1518
1519 These copy an unsigned integer into the given SV, upgrading first if necessary.
1520
1521
1522 They differ only in that C<sv_setuv_mg> handles 'set' magic; C<sv_setuv> does
1523 not.
1524
1525 =cut
1526 */
1527
1528 void
1529 Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
1530 {
1531     PERL_ARGS_ASSERT_SV_SETUV;
1532
1533     /* With the if statement to ensure that integers are stored as IVs whenever
1534        possible:
1535        u=1.49  s=0.52  cu=72.49  cs=10.64  scripts=270  tests=20865
1536
1537        without
1538        u=1.35  s=0.47  cu=73.45  cs=11.43  scripts=270  tests=20865
1539
1540        If you wish to remove the following if statement, so that this routine
1541        (and its callers) always return UVs, please benchmark to see what the
1542        effect is. Modern CPUs may be different. Or may not :-)
1543     */
1544     if (u <= (UV)IV_MAX) {
1545        sv_setiv(sv, (IV)u);
1546        return;
1547     }
1548     sv_setiv(sv, 0);
1549     SvIsUV_on(sv);
1550     SvUV_set(sv, u);
1551 }
1552
1553 void
1554 Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
1555 {
1556     PERL_ARGS_ASSERT_SV_SETUV_MG;
1557
1558     sv_setuv(sv,u);
1559     SvSETMAGIC(sv);
1560 }
1561
1562 /*
1563 =for apidoc sv_setnv
1564 =for apidoc_item sv_setnv_mg
1565
1566 These copy a double into the given SV, upgrading first if necessary.
1567
1568 They differ only in that C<sv_setnv_mg> handles 'set' magic; C<sv_setnv> does
1569 not.
1570
1571 =cut
1572 */
1573
1574 void
1575 Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
1576 {
1577     PERL_ARGS_ASSERT_SV_SETNV;
1578
1579     SV_CHECK_THINKFIRST_COW_DROP(sv);
1580     switch (SvTYPE(sv)) {
1581     case SVt_NULL:
1582     case SVt_IV:
1583 #if NVSIZE <= IVSIZE
1584         SET_SVANY_FOR_BODYLESS_NV(sv);
1585         SvFLAGS(sv) &= ~SVTYPEMASK;
1586         SvFLAGS(sv) |= SVt_NV;
1587         break;
1588 #else
1589         sv_upgrade(sv, SVt_NV);
1590         break;
1591 #endif
1592     case SVt_PV:
1593     case SVt_PVIV:
1594         sv_upgrade(sv, SVt_PVNV);
1595         break;
1596
1597     case SVt_PVGV:
1598         if (!isGV_with_GP(sv))
1599             break;
1600         /* FALLTHROUGH */
1601     case SVt_PVAV:
1602     case SVt_PVHV:
1603     case SVt_PVCV:
1604     case SVt_PVFM:
1605     case SVt_PVIO:
1606         /* diag_listed_as: Can't coerce %s to %s in %s */
1607         Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1608                    OP_DESC(PL_op));
1609         NOT_REACHED; /* NOTREACHED */
1610         break;
1611     default: NOOP;
1612     }
1613     SvNV_set(sv, num);
1614     (void)SvNOK_only(sv);                       /* validate number */
1615     SvTAINT(sv);
1616 }
1617
1618 void
1619 Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
1620 {
1621     PERL_ARGS_ASSERT_SV_SETNV_MG;
1622
1623     sv_setnv(sv,num);
1624     SvSETMAGIC(sv);
1625 }
1626
1627 /*
1628 =for apidoc sv_setrv_noinc
1629 =for apidoc_item sv_setrv_noinc_mg
1630
1631 Copies an SV pointer into the given SV as an SV reference, upgrading it if
1632 necessary. After this, C<SvRV(sv)> is equal to I<ref>. This does not adjust
1633 the reference count of I<ref>. The reference I<ref> must not be NULL.
1634
1635 C<sv_setrv_noinc_mg> will invoke 'set' magic on the SV; C<sv_setrv_noinc> will
1636 not.
1637
1638 =cut
1639 */
1640
1641 void
1642 Perl_sv_setrv_noinc(pTHX_ SV *const sv, SV *const ref)
1643 {
1644     PERL_ARGS_ASSERT_SV_SETRV_NOINC;
1645
1646     SV_CHECK_THINKFIRST_COW_DROP(sv);
1647     prepare_SV_for_RV(sv);
1648
1649     SvOK_off(sv);
1650     SvRV_set(sv, ref);
1651     SvROK_on(sv);
1652 }
1653
1654 void
1655 Perl_sv_setrv_noinc_mg(pTHX_ SV *const sv, SV *const ref)
1656 {
1657     PERL_ARGS_ASSERT_SV_SETRV_NOINC_MG;
1658
1659     sv_setrv_noinc(sv, ref);
1660     SvSETMAGIC(sv);
1661 }
1662
1663 /*
1664 =for apidoc sv_setrv_inc
1665 =for apidoc_item sv_setrv_inc_mg
1666
1667 As C<sv_setrv_noinc> but increments the reference count of I<ref>.
1668
1669 C<sv_setrv_inc_mg> will invoke 'set' magic on the SV; C<sv_setrv_inc> will
1670 not.
1671
1672 =cut
1673 */
1674
1675 void
1676 Perl_sv_setrv_inc(pTHX_ SV *const sv, SV *const ref)
1677 {
1678     PERL_ARGS_ASSERT_SV_SETRV_INC;
1679
1680     sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1681 }
1682
1683 void
1684 Perl_sv_setrv_inc_mg(pTHX_ SV *const sv, SV *const ref)
1685 {
1686     PERL_ARGS_ASSERT_SV_SETRV_INC_MG;
1687
1688     sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1689     SvSETMAGIC(sv);
1690 }
1691
1692 /* Return a cleaned-up, printable version of sv, for non-numeric, or
1693  * not incrementable warning display.
1694  * Originally part of S_not_a_number().
1695  * The return value may be != tmpbuf.
1696  */
1697
1698 STATIC const char *
1699 S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
1700     const char *pv;
1701
1702      PERL_ARGS_ASSERT_SV_DISPLAY;
1703
1704      if (DO_UTF8(sv)) {
1705           SV *dsv = newSVpvs_flags("", SVs_TEMP);
1706           pv = sv_uni_display(dsv, sv, 32, UNI_DISPLAY_ISPRINT);
1707      } else {
1708           char *d = tmpbuf;
1709           const char * const limit = tmpbuf + tmpbuf_size - 8;
1710           /* each *s can expand to 4 chars + "...\0",
1711              i.e. need room for 8 chars */
1712
1713           const char *s = SvPVX_const(sv);
1714           const char * const end = s + SvCUR(sv);
1715           for ( ; s < end && d < limit; s++ ) {
1716                int ch = (U8) *s;
1717                if (! isASCII(ch) && !isPRINT_LC(ch)) {
1718                     *d++ = 'M';
1719                     *d++ = '-';
1720
1721                     /* Map to ASCII "equivalent" of Latin1 */
1722                     ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
1723                }
1724                if (ch == '\n') {
1725                     *d++ = '\\';
1726                     *d++ = 'n';
1727                }
1728                else if (ch == '\r') {
1729                     *d++ = '\\';
1730                     *d++ = 'r';
1731                }
1732                else if (ch == '\f') {
1733                     *d++ = '\\';
1734                     *d++ = 'f';
1735                }
1736                else if (ch == '\\') {
1737                     *d++ = '\\';
1738                     *d++ = '\\';
1739                }
1740                else if (ch == '\0') {
1741                     *d++ = '\\';
1742                     *d++ = '0';
1743                }
1744                else if (isPRINT_LC(ch))
1745                     *d++ = ch;
1746                else {
1747                     *d++ = '^';
1748                     *d++ = toCTRL(ch);
1749                }
1750           }
1751           if (s < end) {
1752                *d++ = '.';
1753                *d++ = '.';
1754                *d++ = '.';
1755           }
1756           *d = '\0';
1757           pv = tmpbuf;
1758     }
1759
1760     return pv;
1761 }
1762
1763 /* Print an "isn't numeric" warning, using a cleaned-up,
1764  * printable version of the offending string
1765  */
1766
1767 STATIC void
1768 S_not_a_number(pTHX_ SV *const sv)
1769 {
1770      char tmpbuf[64];
1771      const char *pv;
1772
1773      PERL_ARGS_ASSERT_NOT_A_NUMBER;
1774
1775      pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1776
1777     if (PL_op)
1778         Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1779                     /* diag_listed_as: Argument "%s" isn't numeric%s */
1780                     "Argument \"%s\" isn't numeric in %s", pv,
1781                     OP_DESC(PL_op));
1782     else
1783         Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1784                     /* diag_listed_as: Argument "%s" isn't numeric%s */
1785                     "Argument \"%s\" isn't numeric", pv);
1786 }
1787
1788 STATIC void
1789 S_not_incrementable(pTHX_ SV *const sv) {
1790      char tmpbuf[64];
1791      const char *pv;
1792
1793      PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
1794
1795      pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1796
1797      Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1798                  "Argument \"%s\" treated as 0 in increment (++)", pv);
1799 }
1800
1801 /*
1802 =for apidoc looks_like_number
1803
1804 Test if the content of an SV looks like a number (or is a number).
1805 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1806 non-numeric warning), even if your C<atof()> doesn't grok them.  Get-magic is
1807 ignored.
1808
1809 =cut
1810 */
1811
1812 I32
1813 Perl_looks_like_number(pTHX_ SV *const sv)
1814 {
1815     const char *sbegin;
1816     STRLEN len;
1817     int numtype;
1818
1819     PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1820
1821     if (SvPOK(sv) || SvPOKp(sv)) {
1822         sbegin = SvPV_nomg_const(sv, len);
1823     }
1824     else
1825         return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1826     numtype = grok_number(sbegin, len, NULL);
1827     return ((numtype & IS_NUMBER_TRAILING)) ? 0 : numtype;
1828 }
1829
1830 STATIC bool
1831 S_glob_2number(pTHX_ GV * const gv)
1832 {
1833     PERL_ARGS_ASSERT_GLOB_2NUMBER;
1834
1835     /* We know that all GVs stringify to something that is not-a-number,
1836         so no need to test that.  */
1837     if (ckWARN(WARN_NUMERIC))
1838     {
1839         SV *const buffer = sv_newmortal();
1840         gv_efullname3(buffer, gv, "*");
1841         not_a_number(buffer);
1842     }
1843     /* We just want something true to return, so that S_sv_2iuv_common
1844         can tail call us and return true.  */
1845     return TRUE;
1846 }
1847
1848 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1849    until proven guilty, assume that things are not that bad... */
1850
1851 /*
1852    NV_PRESERVES_UV:
1853
1854    As 64 bit platforms often have an NV that doesn't preserve all bits of
1855    an IV (an assumption perl has been based on to date) it becomes necessary
1856    to remove the assumption that the NV always carries enough precision to
1857    recreate the IV whenever needed, and that the NV is the canonical form.
1858    Instead, IV/UV and NV need to be given equal rights. So as to not lose
1859    precision as a side effect of conversion (which would lead to insanity
1860    and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1861    1) to distinguish between IV/UV/NV slots that have a valid conversion cached
1862       where precision was lost, and IV/UV/NV slots that have a valid conversion
1863       which has lost no precision
1864    2) to ensure that if a numeric conversion to one form is requested that
1865       would lose precision, the precise conversion (or differently
1866       imprecise conversion) is also performed and cached, to prevent
1867       requests for different numeric formats on the same SV causing
1868       lossy conversion chains. (lossless conversion chains are perfectly
1869       acceptable (still))
1870
1871
1872    flags are used:
1873    SvIOKp is true if the IV slot contains a valid value
1874    SvIOK  is true only if the IV value is accurate (UV if SvIOK_UV true)
1875    SvNOKp is true if the NV slot contains a valid value
1876    SvNOK  is true only if the NV value is accurate
1877
1878    so
1879    while converting from PV to NV, check to see if converting that NV to an
1880    IV(or UV) would lose accuracy over a direct conversion from PV to
1881    IV(or UV). If it would, cache both conversions, return NV, but mark
1882    SV as IOK NOKp (ie not NOK).
1883
1884    While converting from PV to IV, check to see if converting that IV to an
1885    NV would lose accuracy over a direct conversion from PV to NV. If it
1886    would, cache both conversions, flag similarly.
1887
1888    Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1889    correctly because if IV & NV were set NV *always* overruled.
1890    Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1891    changes - now IV and NV together means that the two are interchangeable:
1892    SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1893
1894    The benefit of this is that operations such as pp_add know that if
1895    SvIOK is true for both left and right operands, then integer addition
1896    can be used instead of floating point (for cases where the result won't
1897    overflow). Before, floating point was always used, which could lead to
1898    loss of precision compared with integer addition.
1899
1900    * making IV and NV equal status should make maths accurate on 64 bit
1901      platforms
1902    * may speed up maths somewhat if pp_add and friends start to use
1903      integers when possible instead of fp. (Hopefully the overhead in
1904      looking for SvIOK and checking for overflow will not outweigh the
1905      fp to integer speedup)
1906    * will slow down integer operations (callers of SvIV) on "inaccurate"
1907      values, as the change from SvIOK to SvIOKp will cause a call into
1908      sv_2iv each time rather than a macro access direct to the IV slot
1909    * should speed up number->string conversion on integers as IV is
1910      favoured when IV and NV are equally accurate
1911
1912    ####################################################################
1913    You had better be using SvIOK_notUV if you want an IV for arithmetic:
1914    SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1915    On the other hand, SvUOK is true iff UV.
1916    ####################################################################
1917
1918    Your mileage will vary depending your CPU's relative fp to integer
1919    performance ratio.
1920 */
1921
1922 #ifndef NV_PRESERVES_UV
1923 #  define IS_NUMBER_UNDERFLOW_IV 1
1924 #  define IS_NUMBER_UNDERFLOW_UV 2
1925 #  define IS_NUMBER_IV_AND_UV    2
1926 #  define IS_NUMBER_OVERFLOW_IV  4
1927 #  define IS_NUMBER_OVERFLOW_UV  5
1928
1929 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
1930
1931 /* For sv_2nv these three cases are "SvNOK and don't bother casting"  */
1932 STATIC int
1933 S_sv_2iuv_non_preserve(pTHX_ SV *const sv
1934 #  ifdef DEBUGGING
1935                        , I32 numtype
1936 #  endif
1937                        )
1938 {
1939     PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
1940     PERL_UNUSED_CONTEXT;
1941
1942     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));
1943     if (SvNVX(sv) < (NV)IV_MIN) {
1944         (void)SvIOKp_on(sv);
1945         (void)SvNOK_on(sv);
1946         SvIV_set(sv, IV_MIN);
1947         return IS_NUMBER_UNDERFLOW_IV;
1948     }
1949     if (SvNVX(sv) > (NV)UV_MAX) {
1950         (void)SvIOKp_on(sv);
1951         (void)SvNOK_on(sv);
1952         SvIsUV_on(sv);
1953         SvUV_set(sv, UV_MAX);
1954         return IS_NUMBER_OVERFLOW_UV;
1955     }
1956     (void)SvIOKp_on(sv);
1957     (void)SvNOK_on(sv);
1958     /* Can't use strtol etc to convert this string.  (See truth table in
1959        sv_2iv  */
1960     if (SvNVX(sv) < IV_MAX_P1) {
1961         SvIV_set(sv, I_V(SvNVX(sv)));
1962         if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1963             SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1964         } else {
1965             /* Integer is imprecise. NOK, IOKp */
1966         }
1967         return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1968     }
1969     SvIsUV_on(sv);
1970     SvUV_set(sv, U_V(SvNVX(sv)));
1971     if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1972         if (SvUVX(sv) == UV_MAX) {
1973             /* As we know that NVs don't preserve UVs, UV_MAX cannot
1974                possibly be preserved by NV. Hence, it must be overflow.
1975                NOK, IOKp */
1976             return IS_NUMBER_OVERFLOW_UV;
1977         }
1978         SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1979     } else {
1980         /* Integer is imprecise. NOK, IOKp */
1981     }
1982     return IS_NUMBER_OVERFLOW_IV;
1983 }
1984 #endif /* !NV_PRESERVES_UV*/
1985
1986 /* If numtype is infnan, set the NV of the sv accordingly.
1987  * If numtype is anything else, try setting the NV using Atof(PV). */
1988 static void
1989 S_sv_setnv(pTHX_ SV* sv, int numtype)
1990 {
1991     bool pok = cBOOL(SvPOK(sv));
1992     bool nok = FALSE;
1993 #ifdef NV_INF
1994     if ((numtype & IS_NUMBER_INFINITY)) {
1995         SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF);
1996         nok = TRUE;
1997     } else
1998 #endif
1999 #ifdef NV_NAN
2000     if ((numtype & IS_NUMBER_NAN)) {
2001         SvNV_set(sv, NV_NAN);
2002         nok = TRUE;
2003     } else
2004 #endif
2005     if (pok) {
2006         SvNV_set(sv, Atof(SvPVX_const(sv)));
2007         /* Purposefully no true nok here, since we don't want to blow
2008          * away the possible IOK/UV of an existing sv. */
2009     }
2010     if (nok) {
2011         SvNOK_only(sv); /* No IV or UV please, this is pure infnan. */
2012         if (pok)
2013             SvPOK_on(sv); /* PV is okay, though. */
2014     }
2015 }
2016
2017 STATIC bool
2018 S_sv_2iuv_common(pTHX_ SV *const sv)
2019 {
2020     PERL_ARGS_ASSERT_SV_2IUV_COMMON;
2021
2022     if (SvNOKp(sv)) {
2023         /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2024          * without also getting a cached IV/UV from it at the same time
2025          * (ie PV->NV conversion should detect loss of accuracy and cache
2026          * IV or UV at same time to avoid this. */
2027         /* IV-over-UV optimisation - choose to cache IV if possible */
2028
2029         if (SvTYPE(sv) == SVt_NV)
2030             sv_upgrade(sv, SVt_PVNV);
2031
2032     got_nv:
2033         (void)SvIOKp_on(sv);    /* Must do this first, to clear any SvOOK */
2034         /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2035            certainly cast into the IV range at IV_MAX, whereas the correct
2036            answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2037            cases go to UV */
2038 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2039         if (Perl_isnan(SvNVX(sv))) {
2040             SvUV_set(sv, 0);
2041             SvIsUV_on(sv);
2042             return FALSE;
2043         }
2044 #endif
2045         if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2046             SvIV_set(sv, I_V(SvNVX(sv)));
2047             if (SvNVX(sv) == (NV) SvIVX(sv)
2048 #ifndef NV_PRESERVES_UV
2049                 && SvIVX(sv) != IV_MIN /* avoid negating IV_MIN below */
2050                 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2051                     (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2052                 /* Don't flag it as "accurately an integer" if the number
2053                    came from a (by definition imprecise) NV operation, and
2054                    we're outside the range of NV integer precision */
2055 #endif
2056                 ) {
2057                 if (SvNOK(sv))
2058                     SvIOK_on(sv);  /* Can this go wrong with rounding? NWC */
2059                 else {
2060                     /* scalar has trailing garbage, eg "42a" */
2061                 }
2062                 DEBUG_c(PerlIO_printf(Perl_debug_log,
2063                                       "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (precise)\n",
2064                                       PTR2UV(sv),
2065                                       SvNVX(sv),
2066                                       SvIVX(sv)));
2067
2068             } else {
2069                 /* IV not precise.  No need to convert from PV, as NV
2070                    conversion would already have cached IV if it detected
2071                    that PV->IV would be better than PV->NV->IV
2072                    flags already correct - don't set public IOK.  */
2073                 DEBUG_c(PerlIO_printf(Perl_debug_log,
2074                                       "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (imprecise)\n",
2075                                       PTR2UV(sv),
2076                                       SvNVX(sv),
2077                                       SvIVX(sv)));
2078             }
2079             /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2080                but the cast (NV)IV_MIN rounds to a the value less (more
2081                negative) than IV_MIN which happens to be equal to SvNVX ??
2082                Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2083                NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2084                (NV)UVX == NVX are both true, but the values differ. :-(
2085                Hopefully for 2s complement IV_MIN is something like
2086                0x8000000000000000 which will be exact. NWC */
2087         }
2088         else {
2089             SvUV_set(sv, U_V(SvNVX(sv)));
2090             if (
2091                 (SvNVX(sv) == (NV) SvUVX(sv))
2092 #ifndef  NV_PRESERVES_UV
2093                 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2094                 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2095                 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2096                 /* Don't flag it as "accurately an integer" if the number
2097                    came from a (by definition imprecise) NV operation, and
2098                    we're outside the range of NV integer precision */
2099 #endif
2100                 && SvNOK(sv)
2101                 )
2102                 SvIOK_on(sv);
2103             SvIsUV_on(sv);
2104             DEBUG_c(PerlIO_printf(Perl_debug_log,
2105                                   "0x%" UVxf " 2iv(%" UVuf " => %" IVdf ") (as unsigned)\n",
2106                                   PTR2UV(sv),
2107                                   SvUVX(sv),
2108                                   SvUVX(sv)));
2109         }
2110     }
2111     else if (SvPOKp(sv)) {
2112         UV value;
2113         int numtype;
2114         const char *s = SvPVX_const(sv);
2115         const STRLEN cur = SvCUR(sv);
2116
2117         /* short-cut for a single digit string like "1" */
2118
2119         if (cur == 1) {
2120             char c = *s;
2121             if (isDIGIT(c)) {
2122                 if (SvTYPE(sv) < SVt_PVIV)
2123                     sv_upgrade(sv, SVt_PVIV);
2124                 (void)SvIOK_on(sv);
2125                 SvIV_set(sv, (IV)(c - '0'));
2126                 return FALSE;
2127             }
2128         }
2129
2130         numtype = grok_number(s, cur, &value);
2131         /* We want to avoid a possible problem when we cache an IV/ a UV which
2132            may be later translated to an NV, and the resulting NV is not
2133            the same as the direct translation of the initial string
2134            (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2135            be careful to ensure that the value with the .456 is around if the
2136            NV value is requested in the future).
2137
2138            This means that if we cache such an IV/a UV, we need to cache the
2139            NV as well.  Moreover, we trade speed for space, and do not
2140            cache the NV if we are sure it's not needed.
2141          */
2142
2143         /* SVt_PVNV is one higher than SVt_PVIV, hence this order  */
2144         if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2145              == IS_NUMBER_IN_UV) {
2146             /* It's definitely an integer, only upgrade to PVIV */
2147             if (SvTYPE(sv) < SVt_PVIV)
2148                 sv_upgrade(sv, SVt_PVIV);
2149             (void)SvIOK_on(sv);
2150         } else if (SvTYPE(sv) < SVt_PVNV)
2151             sv_upgrade(sv, SVt_PVNV);
2152
2153         if ((numtype & (IS_NUMBER_INFINITY | IS_NUMBER_NAN))) {
2154             if (ckWARN(WARN_NUMERIC) && ((numtype & IS_NUMBER_TRAILING)))
2155                 not_a_number(sv);
2156             S_sv_setnv(aTHX_ sv, numtype);
2157             goto got_nv;        /* Fill IV/UV slot and set IOKp */
2158         }
2159
2160         /* If NVs preserve UVs then we only use the UV value if we know that
2161            we aren't going to call atof() below. If NVs don't preserve UVs
2162            then the value returned may have more precision than atof() will
2163            return, even though value isn't perfectly accurate.  */
2164         if ((numtype & (IS_NUMBER_IN_UV
2165 #ifdef NV_PRESERVES_UV
2166                         | IS_NUMBER_NOT_INT
2167 #endif
2168             )) == IS_NUMBER_IN_UV) {
2169             /* This won't turn off the public IOK flag if it was set above  */
2170             (void)SvIOKp_on(sv);
2171
2172             if (!(numtype & IS_NUMBER_NEG)) {
2173                 /* positive */;
2174                 if (value <= (UV)IV_MAX) {
2175                     SvIV_set(sv, (IV)value);
2176                 } else {
2177                     /* it didn't overflow, and it was positive. */
2178                     SvUV_set(sv, value);
2179                     SvIsUV_on(sv);
2180                 }
2181             } else {
2182                 /* 2s complement assumption  */
2183                 if (value <= (UV)IV_MIN) {
2184                     SvIV_set(sv, value == (UV)IV_MIN
2185                                     ? IV_MIN : -(IV)value);
2186                 } else {
2187                     /* Too negative for an IV.  This is a double upgrade, but
2188                        I'm assuming it will be rare.  */
2189                     if (SvTYPE(sv) < SVt_PVNV)
2190                         sv_upgrade(sv, SVt_PVNV);
2191                     SvNOK_on(sv);
2192                     SvIOK_off(sv);
2193                     SvIOKp_on(sv);
2194                     SvNV_set(sv, -(NV)value);
2195                     SvIV_set(sv, IV_MIN);
2196                 }
2197             }
2198         }
2199         /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2200            will be in the previous block to set the IV slot, and the next
2201            block to set the NV slot.  So no else here.  */
2202
2203         if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2204             != IS_NUMBER_IN_UV) {
2205             /* It wasn't an (integer that doesn't overflow the UV). */
2206             S_sv_setnv(aTHX_ sv, numtype);
2207
2208             if (! numtype && ckWARN(WARN_NUMERIC))
2209                 not_a_number(sv);
2210
2211             DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" NVgf ")\n",
2212                                   PTR2UV(sv), SvNVX(sv)));
2213
2214 #ifdef NV_PRESERVES_UV
2215             SvNOKp_on(sv);
2216             if (numtype)
2217                 SvNOK_on(sv);
2218             goto got_nv;        /* Fill IV/UV slot and set IOKp, maybe IOK */
2219 #else /* NV_PRESERVES_UV */
2220             if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2221                 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2222                 /* The IV/UV slot will have been set from value returned by
2223                    grok_number above.  The NV slot has just been set using
2224                    Atof.  */
2225                 SvNOK_on(sv);
2226                 assert (SvIOKp(sv));
2227             } else {
2228                 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2229                     U_V(Perl_fabs(SvNVX(sv)))) {
2230                     /* Small enough to preserve all bits. */
2231                     (void)SvIOKp_on(sv);
2232                     SvNOK_on(sv);
2233                     SvIV_set(sv, I_V(SvNVX(sv)));
2234                     if ((NV)(SvIVX(sv)) == SvNVX(sv))
2235                         SvIOK_on(sv);
2236                     /* There had been runtime checking for
2237                        "U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX" here to ensure
2238                        that this NV is in the preserved range, but this should
2239                        be always true if the following assertion is true: */
2240                     STATIC_ASSERT_STMT(((UV)1 << NV_PRESERVES_UV_BITS) <=
2241                                        (UV)IV_MAX);
2242                 } else {
2243                     /* IN_UV NOT_INT
2244                          0      0       already failed to read UV.
2245                          0      1       already failed to read UV.
2246                          1      0       you won't get here in this case. IV/UV
2247                                         slot set, public IOK, Atof() unneeded.
2248                          1      1       already read UV.
2249                        so there's no point in sv_2iuv_non_preserve() attempting
2250                        to use atol, strtol, strtoul etc.  */
2251 #  ifdef DEBUGGING
2252                     sv_2iuv_non_preserve (sv, numtype);
2253 #  else
2254                     sv_2iuv_non_preserve (sv);
2255 #  endif
2256                 }
2257             }
2258         /* It might be more code efficient to go through the entire logic above
2259            and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2260            gets complex and potentially buggy, so more programmer efficient
2261            to do it this way, by turning off the public flags:  */
2262         if (!numtype)
2263             SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2264 #endif /* NV_PRESERVES_UV */
2265         }
2266     }
2267     else {
2268         if (isGV_with_GP(sv))
2269             return glob_2number(MUTABLE_GV(sv));
2270
2271         if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2272                 report_uninit(sv);
2273         if (SvTYPE(sv) < SVt_IV)
2274             /* Typically the caller expects that sv_any is not NULL now.  */
2275             sv_upgrade(sv, SVt_IV);
2276         /* Return 0 from the caller.  */
2277         return TRUE;
2278     }
2279     return FALSE;
2280 }
2281
2282 /*
2283 =for apidoc sv_2iv_flags
2284
2285 Return the integer value of an SV, doing any necessary string
2286 conversion.  If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2287 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2288
2289 =cut
2290 */
2291
2292 IV
2293 Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
2294 {
2295     PERL_ARGS_ASSERT_SV_2IV_FLAGS;
2296
2297     assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2298          && SvTYPE(sv) != SVt_PVFM);
2299
2300     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2301         mg_get(sv);
2302
2303     if (SvROK(sv)) {
2304         if (SvAMAGIC(sv)) {
2305             SV * tmpstr;
2306             if (flags & SV_SKIP_OVERLOAD)
2307                 return 0;
2308             tmpstr = AMG_CALLunary(sv, numer_amg);
2309             if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2310                 return SvIV(tmpstr);
2311             }
2312         }
2313         return PTR2IV(SvRV(sv));
2314     }
2315
2316     if (SvVALID(sv) || isREGEXP(sv)) {
2317         /* FBMs use the space for SvIVX and SvNVX for other purposes, so
2318            must not let them cache IVs.
2319            In practice they are extremely unlikely to actually get anywhere
2320            accessible by user Perl code - the only way that I'm aware of is when
2321            a constant subroutine which is used as the second argument to index.
2322
2323            Regexps have no SvIVX and SvNVX fields.
2324         */
2325         assert(SvPOKp(sv));
2326         {
2327             UV value;
2328             const char * const ptr =
2329                 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2330             const int numtype
2331                 = grok_number(ptr, SvCUR(sv), &value);
2332
2333             if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2334                 == IS_NUMBER_IN_UV) {
2335                 /* It's definitely an integer */
2336                 if (numtype & IS_NUMBER_NEG) {
2337                     if (value < (UV)IV_MIN)
2338                         return -(IV)value;
2339                 } else {
2340                     if (value < (UV)IV_MAX)
2341                         return (IV)value;
2342                 }
2343             }
2344
2345             /* Quite wrong but no good choices. */
2346             if ((numtype & IS_NUMBER_INFINITY)) {
2347                 return (numtype & IS_NUMBER_NEG) ? IV_MIN : IV_MAX;
2348             } else if ((numtype & IS_NUMBER_NAN)) {
2349                 return 0; /* So wrong. */
2350             }
2351
2352             if (!numtype) {
2353                 if (ckWARN(WARN_NUMERIC))
2354                     not_a_number(sv);
2355             }
2356             return I_V(Atof(ptr));
2357         }
2358     }
2359
2360     if (SvTHINKFIRST(sv)) {
2361         if (SvREADONLY(sv) && !SvOK(sv)) {
2362             if (ckWARN(WARN_UNINITIALIZED))
2363                 report_uninit(sv);
2364             return 0;
2365         }
2366     }
2367
2368     if (!SvIOKp(sv)) {
2369         if (S_sv_2iuv_common(aTHX_ sv))
2370             return 0;
2371     }
2372
2373     DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" IVdf ")\n",
2374         PTR2UV(sv),SvIVX(sv)));
2375     return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2376 }
2377
2378 /*
2379 =for apidoc sv_2uv_flags
2380
2381 Return the unsigned integer value of an SV, doing any necessary string
2382 conversion.  If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2383 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2384
2385 =for apidoc Amnh||SV_GMAGIC
2386
2387 =cut
2388 */
2389
2390 UV
2391 Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
2392 {
2393     PERL_ARGS_ASSERT_SV_2UV_FLAGS;
2394
2395     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2396         mg_get(sv);
2397
2398     if (SvROK(sv)) {
2399         if (SvAMAGIC(sv)) {
2400             SV *tmpstr;
2401             if (flags & SV_SKIP_OVERLOAD)
2402                 return 0;
2403             tmpstr = AMG_CALLunary(sv, numer_amg);
2404             if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2405                 return SvUV(tmpstr);
2406             }
2407         }
2408         return PTR2UV(SvRV(sv));
2409     }
2410
2411     if (SvVALID(sv) || isREGEXP(sv)) {
2412         /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2413            the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2414            Regexps have no SvIVX and SvNVX fields. */
2415         assert(SvPOKp(sv));
2416         {
2417             UV value;
2418             const char * const ptr =
2419                 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2420             const int numtype
2421                 = grok_number(ptr, SvCUR(sv), &value);
2422
2423             if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2424                 == IS_NUMBER_IN_UV) {
2425                 /* It's definitely an integer */
2426                 if (!(numtype & IS_NUMBER_NEG))
2427                     return value;
2428             }
2429
2430             /* Quite wrong but no good choices. */
2431             if ((numtype & IS_NUMBER_INFINITY)) {
2432                 return UV_MAX; /* So wrong. */
2433             } else if ((numtype & IS_NUMBER_NAN)) {
2434                 return 0; /* So wrong. */
2435             }
2436
2437             if (!numtype) {
2438                 if (ckWARN(WARN_NUMERIC))
2439                     not_a_number(sv);
2440             }
2441             return U_V(Atof(ptr));
2442         }
2443     }
2444
2445     if (SvTHINKFIRST(sv)) {
2446         if (SvREADONLY(sv) && !SvOK(sv)) {
2447             if (ckWARN(WARN_UNINITIALIZED))
2448                 report_uninit(sv);
2449             return 0;
2450         }
2451     }
2452
2453     if (!SvIOKp(sv)) {
2454         if (S_sv_2iuv_common(aTHX_ sv))
2455             return 0;
2456     }
2457
2458     DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2uv(%" UVuf ")\n",
2459                           PTR2UV(sv),SvUVX(sv)));
2460     return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2461 }
2462
2463 /*
2464 =for apidoc sv_2nv_flags
2465
2466 Return the num value of an SV, doing any necessary string or integer
2467 conversion.  If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2468 Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
2469
2470 =cut
2471 */
2472
2473 NV
2474 Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
2475 {
2476     PERL_ARGS_ASSERT_SV_2NV_FLAGS;
2477
2478     assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2479          && SvTYPE(sv) != SVt_PVFM);
2480     if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
2481         /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2482            the same flag bit as SVf_IVisUV, so must not let them cache NVs.
2483            Regexps have no SvIVX and SvNVX fields.  */
2484         const char *ptr;
2485         if (flags & SV_GMAGIC)
2486             mg_get(sv);
2487         if (SvNOKp(sv))
2488             return SvNVX(sv);
2489         if (SvPOKp(sv) && !SvIOKp(sv)) {
2490             ptr = SvPVX_const(sv);
2491             if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
2492                 !grok_number(ptr, SvCUR(sv), NULL))
2493                 not_a_number(sv);
2494             return Atof(ptr);
2495         }
2496         if (SvIOKp(sv)) {
2497             if (SvIsUV(sv))
2498                 return (NV)SvUVX(sv);
2499             else
2500                 return (NV)SvIVX(sv);
2501         }
2502         if (SvROK(sv)) {
2503             goto return_rok;
2504         }
2505         assert(SvTYPE(sv) >= SVt_PVMG);
2506         /* This falls through to the report_uninit near the end of the
2507            function. */
2508     } else if (SvTHINKFIRST(sv)) {
2509         if (SvROK(sv)) {
2510         return_rok:
2511             if (SvAMAGIC(sv)) {
2512                 SV *tmpstr;
2513                 if (flags & SV_SKIP_OVERLOAD)
2514                     return 0;
2515                 tmpstr = AMG_CALLunary(sv, numer_amg);
2516                 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2517                     return SvNV(tmpstr);
2518                 }
2519             }
2520             return PTR2NV(SvRV(sv));
2521         }
2522         if (SvREADONLY(sv) && !SvOK(sv)) {
2523             if (ckWARN(WARN_UNINITIALIZED))
2524                 report_uninit(sv);
2525             return 0.0;
2526         }
2527     }
2528     if (SvTYPE(sv) < SVt_NV) {
2529         /* The logic to use SVt_PVNV if necessary is in sv_upgrade.  */
2530         sv_upgrade(sv, SVt_NV);
2531         CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2532         DEBUG_c({
2533             DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2534             STORE_LC_NUMERIC_SET_STANDARD();
2535             PerlIO_printf(Perl_debug_log,
2536                           "0x%" UVxf " num(%" NVgf ")\n",
2537                           PTR2UV(sv), SvNVX(sv));
2538             RESTORE_LC_NUMERIC();
2539         });
2540         CLANG_DIAG_RESTORE_STMT;
2541
2542     }
2543     else if (SvTYPE(sv) < SVt_PVNV)
2544         sv_upgrade(sv, SVt_PVNV);
2545     if (SvNOKp(sv)) {
2546         return SvNVX(sv);
2547     }
2548     if (SvIOKp(sv)) {
2549         SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2550 #ifdef NV_PRESERVES_UV
2551         if (SvIOK(sv))
2552             SvNOK_on(sv);
2553         else
2554             SvNOKp_on(sv);
2555 #else
2556         /* Only set the public NV OK flag if this NV preserves the IV  */
2557         /* Check it's not 0xFFFFFFFFFFFFFFFF */
2558         if (SvIOK(sv) &&
2559             SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2560                        : (SvIVX(sv) == I_V(SvNVX(sv))))
2561             SvNOK_on(sv);
2562         else
2563             SvNOKp_on(sv);
2564 #endif
2565     }
2566     else if (SvPOKp(sv)) {
2567         UV value;
2568         const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2569         if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
2570             not_a_number(sv);
2571 #ifdef NV_PRESERVES_UV
2572         if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2573             == IS_NUMBER_IN_UV) {
2574             /* It's definitely an integer */
2575             SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2576         } else {
2577             S_sv_setnv(aTHX_ sv, numtype);
2578         }
2579         if (numtype)
2580             SvNOK_on(sv);
2581         else
2582             SvNOKp_on(sv);
2583 #else
2584         SvNV_set(sv, Atof(SvPVX_const(sv)));
2585         /* Only set the public NV OK flag if this NV preserves the value in
2586            the PV at least as well as an IV/UV would.
2587            Not sure how to do this 100% reliably. */
2588         /* if that shift count is out of range then Configure's test is
2589            wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2590            UV_BITS */
2591         if (((UV)1 << NV_PRESERVES_UV_BITS) > U_V(Perl_fabs(SvNVX(sv)))) {
2592             SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2593         } else if (!(numtype & IS_NUMBER_IN_UV)) {
2594             /* Can't use strtol etc to convert this string, so don't try.
2595                sv_2iv and sv_2uv will use the NV to convert, not the PV.  */
2596             SvNOK_on(sv);
2597         } else {
2598             /* value has been set.  It may not be precise.  */
2599             if ((numtype & IS_NUMBER_NEG) && (value >= (UV)IV_MIN)) {
2600                 /* 2s complement assumption for (UV)IV_MIN  */
2601                 SvNOK_on(sv); /* Integer is too negative.  */
2602             } else {
2603                 SvNOKp_on(sv);
2604                 SvIOKp_on(sv);
2605
2606                 if (numtype & IS_NUMBER_NEG) {
2607                     /* -IV_MIN is undefined, but we should never reach
2608                      * this point with both IS_NUMBER_NEG and value ==
2609                      * (UV)IV_MIN */
2610                     assert(value != (UV)IV_MIN);
2611                     SvIV_set(sv, -(IV)value);
2612                 } else if (value <= (UV)IV_MAX) {
2613                     SvIV_set(sv, (IV)value);
2614                 } else {
2615                     SvUV_set(sv, value);
2616                     SvIsUV_on(sv);
2617                 }
2618
2619                 if (numtype & IS_NUMBER_NOT_INT) {
2620                     /* I believe that even if the original PV had decimals,
2621                        they are lost beyond the limit of the FP precision.
2622                        However, neither is canonical, so both only get p
2623                        flags.  NWC, 2000/11/25 */
2624                     /* Both already have p flags, so do nothing */
2625                 } else {
2626                     const NV nv = SvNVX(sv);
2627                     /* XXX should this spot have NAN_COMPARE_BROKEN, too? */
2628                     if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2629                         if (SvIVX(sv) == I_V(nv)) {
2630                             SvNOK_on(sv);
2631                         } else {
2632                             /* It had no "." so it must be integer.  */
2633                         }
2634                         SvIOK_on(sv);
2635                     } else {
2636                         /* between IV_MAX and NV(UV_MAX).
2637                            Could be slightly > UV_MAX */
2638
2639                         if (numtype & IS_NUMBER_NOT_INT) {
2640                             /* UV and NV both imprecise.  */
2641                         } else {
2642                             const UV nv_as_uv = U_V(nv);
2643
2644                             if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2645                                 SvNOK_on(sv);
2646                             }
2647                             SvIOK_on(sv);
2648                         }
2649                     }
2650                 }
2651             }
2652         }
2653         /* It might be more code efficient to go through the entire logic above
2654            and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2655            gets complex and potentially buggy, so more programmer efficient
2656            to do it this way, by turning off the public flags:  */
2657         if (!numtype)
2658             SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2659 #endif /* NV_PRESERVES_UV */
2660     }
2661     else {
2662         if (isGV_with_GP(sv)) {
2663             glob_2number(MUTABLE_GV(sv));
2664             return 0.0;
2665         }
2666
2667         if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2668             report_uninit(sv);
2669         assert (SvTYPE(sv) >= SVt_NV);
2670         /* Typically the caller expects that sv_any is not NULL now.  */
2671         /* XXX Ilya implies that this is a bug in callers that assume this
2672            and ideally should be fixed.  */
2673         return 0.0;
2674     }
2675     CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2676     DEBUG_c({
2677         DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2678         STORE_LC_NUMERIC_SET_STANDARD();
2679         PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2nv(%" NVgf ")\n",
2680                       PTR2UV(sv), SvNVX(sv));
2681         RESTORE_LC_NUMERIC();
2682     });
2683     CLANG_DIAG_RESTORE_STMT;
2684     return SvNVX(sv);
2685 }
2686
2687 /*
2688 =for apidoc sv_2num
2689
2690 Return an SV with the numeric value of the source SV, doing any necessary
2691 reference or overload conversion.  The caller is expected to have handled
2692 get-magic already.
2693
2694 =cut
2695 */
2696
2697 SV *
2698 Perl_sv_2num(pTHX_ SV *const sv)
2699 {
2700     PERL_ARGS_ASSERT_SV_2NUM;
2701
2702     if (!SvROK(sv))
2703         return sv;
2704     if (SvAMAGIC(sv)) {
2705         SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
2706         TAINT_IF(tmpsv && SvTAINTED(tmpsv));
2707         if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2708             return sv_2num(tmpsv);
2709     }
2710     return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2711 }
2712
2713 /* int2str_table: lookup table containing string representations of all
2714  * two digit numbers. For example, int2str_table.arr[0] is "00" and
2715  * int2str_table.arr[12*2] is "12".
2716  *
2717  * We are going to read two bytes at a time, so we have to ensure that
2718  * the array is aligned to a 2 byte boundary. That's why it was made a
2719  * union with a dummy U16 member. */
2720 static const union {
2721     char arr[200];
2722     U16 dummy;
2723 } int2str_table = {{
2724     '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6',
2725     '0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
2726     '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',
2727     '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7',
2728     '2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
2729     '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1',
2730     '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8',
2731     '4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5',
2732     '5', '6', '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2',
2733     '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
2734     '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6',
2735     '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3',
2736     '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0',
2737     '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7',
2738     '9', '8', '9', '9'
2739 }};
2740
2741 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2742  * UV as a string towards the end of buf, and return pointers to start and
2743  * end of it.
2744  *
2745  * We assume that buf is at least TYPE_CHARS(UV) long.
2746  */
2747
2748 PERL_STATIC_INLINE char *
2749 S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
2750 {
2751     char *ptr = buf + TYPE_CHARS(UV);
2752     char * const ebuf = ptr;
2753     int sign;
2754     U16 *word_ptr, *word_table;
2755
2756     PERL_ARGS_ASSERT_UIV_2BUF;
2757
2758     /* ptr has to be properly aligned, because we will cast it to U16* */
2759     assert(PTR2nat(ptr) % 2 == 0);
2760     /* we are going to read/write two bytes at a time */
2761     word_ptr = (U16*)ptr;
2762     word_table = (U16*)int2str_table.arr;
2763
2764     if (UNLIKELY(is_uv))
2765         sign = 0;
2766     else if (iv >= 0) {
2767         uv = iv;
2768         sign = 0;
2769     } else {
2770         /* Using 0- here to silence bogus warning from MS VC */
2771         uv = (UV) (0 - (UV) iv);
2772         sign = 1;
2773     }
2774
2775     while (uv > 99) {
2776         *--word_ptr = word_table[uv % 100];
2777         uv /= 100;
2778     }
2779     ptr = (char*)word_ptr;
2780
2781     if (uv < 10)
2782         *--ptr = (char)uv + '0';
2783     else {
2784         *--word_ptr = word_table[uv];
2785         ptr = (char*)word_ptr;
2786     }
2787
2788     if (sign)
2789         *--ptr = '-';
2790
2791     *peob = ebuf;
2792     return ptr;
2793 }
2794
2795 /* Helper for sv_2pv_flags and sv_vcatpvfn_flags.  If the NV is an
2796  * infinity or a not-a-number, writes the appropriate strings to the
2797  * buffer, including a zero byte.  On success returns the written length,
2798  * excluding the zero byte, on failure (not an infinity, not a nan)
2799  * returns zero, assert-fails on maxlen being too short.
2800  *
2801  * XXX for "Inf", "-Inf", and "NaN", we could have three read-only
2802  * shared string constants we point to, instead of generating a new
2803  * string for each instance. */
2804 STATIC size_t
2805 S_infnan_2pv(NV nv, char* buffer, size_t maxlen, char plus) {
2806     char* s = buffer;
2807     assert(maxlen >= 4);
2808     if (Perl_isinf(nv)) {
2809         if (nv < 0) {
2810             if (maxlen < 5) /* "-Inf\0"  */
2811                 return 0;
2812             *s++ = '-';
2813         } else if (plus) {
2814             *s++ = '+';
2815         }
2816         *s++ = 'I';
2817         *s++ = 'n';
2818         *s++ = 'f';
2819     }
2820     else if (Perl_isnan(nv)) {
2821         *s++ = 'N';
2822         *s++ = 'a';
2823         *s++ = 'N';
2824         /* XXX optionally output the payload mantissa bits as
2825          * "(unsigned)" (to match the nan("...") C99 function,
2826          * or maybe as "(0xhhh...)"  would make more sense...
2827          * provide a format string so that the user can decide?
2828          * NOTE: would affect the maxlen and assert() logic.*/
2829     }
2830     else {
2831       return 0;
2832     }
2833     assert((s == buffer + 3) || (s == buffer + 4));
2834     *s = 0;
2835     return s - buffer;
2836 }
2837
2838 /*
2839 =for apidoc      sv_2pv
2840 =for apidoc_item sv_2pv_flags
2841
2842 These implement the various forms of the L<perlapi/C<SvPV>> macros.
2843 The macros are the preferred interface.
2844
2845 These return a pointer to the string value of an SV (coercing it to a string if
2846 necessary), and set C<*lp> to its length in bytes.
2847
2848 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
2849 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
2850 C<SV_GMAGIC>.
2851
2852 =for apidoc Amnh||SV_GMAGIC
2853
2854 =cut
2855 */
2856
2857 char *
2858 Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
2859 {
2860     char *s;
2861     bool done_gmagic = FALSE;
2862
2863     PERL_ARGS_ASSERT_SV_2PV_FLAGS;
2864
2865     assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2866          && SvTYPE(sv) != SVt_PVFM);
2867     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC)) {
2868         mg_get(sv);
2869         done_gmagic = TRUE;
2870     }
2871
2872     if (SvROK(sv)) {
2873         if (SvAMAGIC(sv)) {
2874             SV *tmpstr;
2875             SV *nsv= (SV *)sv;
2876             if (flags & SV_SKIP_OVERLOAD)
2877                 return NULL;
2878             if (done_gmagic)
2879                 nsv = sv_mortalcopy_flags(sv,0);
2880             tmpstr = AMG_CALLunary(nsv, string_amg);
2881             TAINT_IF(tmpstr && SvTAINTED(tmpstr));
2882             if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(nsv)))) {
2883                 /* Unwrap this:  */
2884                 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2885                  */
2886
2887                 char *pv;
2888                 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2889                     if (flags & SV_CONST_RETURN) {
2890                         pv = (char *) SvPVX_const(tmpstr);
2891                     } else {
2892                         pv = (flags & SV_MUTABLE_RETURN)
2893                             ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2894                     }
2895                     if (lp)
2896                         *lp = SvCUR(tmpstr);
2897                 } else {
2898                     pv = sv_2pv_flags(tmpstr, lp, flags);
2899                 }
2900                 if (SvUTF8(tmpstr))
2901                     SvUTF8_on(sv);
2902                 else
2903                     SvUTF8_off(sv);
2904                 return pv;
2905             }
2906         }
2907         {
2908             STRLEN len;
2909             char *retval;
2910             char *buffer;
2911             SV *const referent = SvRV(sv);
2912
2913             if (!referent) {
2914                 len = 7;
2915                 retval = buffer = savepvn("NULLREF", len);
2916             } else if (SvTYPE(referent) == SVt_REGEXP &&
2917                        (!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
2918                         amagic_is_enabled(string_amg))) {
2919                 REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
2920
2921                 assert(re);
2922
2923                 /* If the regex is UTF-8 we want the containing scalar to
2924                    have an UTF-8 flag too */
2925                 if (RX_UTF8(re))
2926                     SvUTF8_on(sv);
2927                 else
2928                     SvUTF8_off(sv);
2929
2930                 if (lp)
2931                     *lp = RX_WRAPLEN(re);
2932
2933                 return RX_WRAPPED(re);
2934             } else {
2935                 const char *const typestring = sv_reftype(referent, 0);
2936                 const STRLEN typelen = strlen(typestring);
2937                 UV addr = PTR2UV(referent);
2938                 const char *stashname = NULL;
2939                 STRLEN stashnamelen = 0; /* hush, gcc */
2940                 const char *buffer_end;
2941
2942                 if (SvOBJECT(referent)) {
2943                     const HEK *const name = HvNAME_HEK(SvSTASH(referent));
2944
2945                     if (name) {
2946                         stashname = HEK_KEY(name);
2947                         stashnamelen = HEK_LEN(name);
2948
2949                         if (HEK_UTF8(name)) {
2950                             SvUTF8_on(sv);
2951                         } else {
2952                             SvUTF8_off(sv);
2953                         }
2954                     } else {
2955                         stashname = "__ANON__";
2956                         stashnamelen = 8;
2957                     }
2958                     len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
2959                         + 2 * sizeof(UV) + 2 /* )\0 */;
2960                 } else {
2961                     len = typelen + 3 /* (0x */
2962                         + 2 * sizeof(UV) + 2 /* )\0 */;
2963                 }
2964
2965                 Newx(buffer, len, char);
2966                 buffer_end = retval = buffer + len;
2967
2968                 /* Working backwards  */
2969                 *--retval = '\0';
2970                 *--retval = ')';
2971                 do {
2972                     *--retval = PL_hexdigit[addr & 15];
2973                 } while (addr >>= 4);
2974                 *--retval = 'x';
2975                 *--retval = '0';
2976                 *--retval = '(';
2977
2978                 retval -= typelen;
2979                 memcpy(retval, typestring, typelen);
2980
2981                 if (stashname) {
2982                     *--retval = '=';
2983                     retval -= stashnamelen;
2984                     memcpy(retval, stashname, stashnamelen);
2985                 }
2986                 /* retval may not necessarily have reached the start of the
2987                    buffer here.  */
2988                 assert (retval >= buffer);
2989
2990                 len = buffer_end - retval - 1; /* -1 for that \0  */
2991             }
2992             if (lp)
2993                 *lp = len;
2994             SAVEFREEPV(buffer);
2995             return retval;
2996         }
2997     }
2998
2999     if (SvPOKp(sv)) {
3000         if (lp)
3001             *lp = SvCUR(sv);
3002         if (flags & SV_MUTABLE_RETURN)
3003             return SvPVX_mutable(sv);
3004         if (flags & SV_CONST_RETURN)
3005             return (char *)SvPVX_const(sv);
3006         return SvPVX(sv);
3007     }
3008
3009     if (SvIOK(sv)) {
3010         /* I'm assuming that if both IV and NV are equally valid then
3011            converting the IV is going to be more efficient */
3012         const U32 isUIOK = SvIsUV(sv);
3013         /* The purpose of this union is to ensure that arr is aligned on
3014            a 2 byte boundary, because that is what uiv_2buf() requires */
3015         union {
3016             char arr[TYPE_CHARS(UV)];
3017             U16 dummy;
3018         } buf;
3019         char *ebuf, *ptr;
3020         STRLEN len;
3021
3022         if (SvTYPE(sv) < SVt_PVIV)
3023             sv_upgrade(sv, SVt_PVIV);
3024         ptr = uiv_2buf(buf.arr, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
3025         len = ebuf - ptr;
3026         /* inlined from sv_setpvn */
3027         s = SvGROW_mutable(sv, len + 1);
3028         Move(ptr, s, len, char);
3029         s += len;
3030         *s = '\0';
3031         /* We used to call SvPOK_on(). Whilst this is fine for (most) Perl code,
3032            it means that after this stringification is cached, there is no way
3033            to distinguish between values originally assigned as $a = 42; and
3034            $a = "42"; (or results of string operators vs numeric operators)
3035            where the value has subsequently been used in the other sense
3036            and had a value cached.
3037            This (somewhat) hack means that we retain the cached stringification,
3038            but don't set SVf_POK. Hence if a value is SVf_IOK|SVf_POK then it
3039            originated as "42", whereas if it's SVf_IOK then it originated as 42.
3040            (ignore SVp_IOK and SVp_POK)
3041            The SvPV macros are now updated to recognise this specific case
3042            (and that there isn't overloading or magic that could alter the
3043            cached value) and so return the cached value immediately without
3044            re-entering this function, getting back here to this block of code,
3045            and repeating the same conversion. */
3046         SvPOKp_on(sv);
3047     }
3048     else if (SvNOK(sv)) {
3049         if (SvTYPE(sv) < SVt_PVNV)
3050             sv_upgrade(sv, SVt_PVNV);
3051         if (SvNVX(sv) == 0.0
3052 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3053             && !Perl_isnan(SvNVX(sv))
3054 #endif
3055         ) {
3056             s = SvGROW_mutable(sv, 2);
3057             *s++ = '0';
3058             *s = '\0';
3059         } else {
3060             STRLEN len;
3061             STRLEN size = 5; /* "-Inf\0" */
3062
3063             s = SvGROW_mutable(sv, size);
3064             len = S_infnan_2pv(SvNVX(sv), s, size, 0);
3065             if (len > 0) {
3066                 s += len;
3067                 SvPOKp_on(sv);
3068             }
3069             else {
3070                 /* some Xenix systems wipe out errno here */
3071                 dSAVE_ERRNO;
3072
3073                 size =
3074                     1 + /* sign */
3075                     1 + /* "." */
3076                     NV_DIG +
3077                     1 + /* "e" */
3078                     1 + /* sign */
3079                     5 + /* exponent digits */
3080                     1 + /* \0 */
3081                     2; /* paranoia */
3082
3083                 s = SvGROW_mutable(sv, size);
3084 #ifndef USE_LOCALE_NUMERIC
3085                 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3086
3087                 SvPOKp_on(sv);
3088 #else
3089                 {
3090                     bool local_radix;
3091                     DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3092                     STORE_LC_NUMERIC_SET_TO_NEEDED();
3093
3094                     local_radix = NOT_IN_NUMERIC_STANDARD_;
3095                     if (local_radix && SvCUR(PL_numeric_radix_sv) > 1) {
3096                         size += SvCUR(PL_numeric_radix_sv) - 1;
3097                         s = SvGROW_mutable(sv, size);
3098                     }
3099
3100                     SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3101
3102                     /* If the radix character is UTF-8, and actually is in the
3103                      * output, turn on the UTF-8 flag for the scalar */
3104                     if (   local_radix
3105                         && SvUTF8(PL_numeric_radix_sv)
3106                         && instr(s, SvPVX_const(PL_numeric_radix_sv)))
3107                     {
3108                         SvUTF8_on(sv);
3109                     }
3110
3111                     RESTORE_LC_NUMERIC();
3112                 }
3113
3114                 /* We don't call SvPOK_on(), because it may come to
3115                  * pass that the locale changes so that the
3116                  * stringification we just did is no longer correct.  We
3117                  * will have to re-stringify every time it is needed */
3118 #endif
3119                 RESTORE_ERRNO;
3120             }
3121             while (*s) s++;
3122         }
3123     }
3124     else if (isGV_with_GP(sv)) {
3125         GV *const gv = MUTABLE_GV(sv);
3126         SV *const buffer = sv_newmortal();
3127
3128         gv_efullname3(buffer, gv, "*");
3129
3130         assert(SvPOK(buffer));
3131         if (SvUTF8(buffer))
3132             SvUTF8_on(sv);
3133         else
3134             SvUTF8_off(sv);
3135         if (lp)
3136             *lp = SvCUR(buffer);
3137         return SvPVX(buffer);
3138     }
3139     else {
3140         if (lp)
3141             *lp = 0;
3142         if (flags & SV_UNDEF_RETURNS_NULL)
3143             return NULL;
3144         if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
3145             report_uninit(sv);
3146         /* Typically the caller expects that sv_any is not NULL now.  */
3147         if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
3148             sv_upgrade(sv, SVt_PV);
3149         return (char *)"";
3150     }
3151
3152     {
3153         const STRLEN len = s - SvPVX_const(sv);
3154         if (lp)
3155             *lp = len;
3156         SvCUR_set(sv, len);
3157     }
3158     DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
3159                           PTR2UV(sv),SvPVX_const(sv)));
3160     if (flags & SV_CONST_RETURN)
3161         return (char *)SvPVX_const(sv);
3162     if (flags & SV_MUTABLE_RETURN)
3163         return SvPVX_mutable(sv);
3164     return SvPVX(sv);
3165 }
3166
3167 /*
3168 =for apidoc sv_copypv
3169 =for apidoc_item sv_copypv_flags
3170 =for apidoc_item sv_copypv_nomg
3171
3172 These copy a stringified representation of the source SV into the
3173 destination SV.  They automatically perform coercion of numeric values into
3174 strings.  Guaranteed to preserve the C<UTF8> flag even from overloaded objects.
3175 Similar in nature to C<sv_2pv[_flags]> but they operate directly on an SV
3176 instead of just the string.  Mostly they use L</C<sv_2pv_flags>> to
3177 do the work, except when that would lose the UTF-8'ness of the PV.
3178
3179 The three forms differ only in whether or not they perform 'get magic' on
3180 C<sv>.  C<sv_copypv_nomg> skips 'get magic'; C<sv_copypv> performs it; and
3181 C<sv_copypv_flags> either performs it (if the C<SV_GMAGIC> bit is set in
3182 C<flags>) or doesn't (if that bit is cleared).
3183
3184 =cut
3185 */
3186
3187 void
3188 Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
3189 {
3190     STRLEN len;
3191     const char *s;
3192
3193     PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
3194
3195     s = SvPV_flags_const(ssv,len,(flags & SV_GMAGIC));
3196     sv_setpvn(dsv,s,len);
3197     if (SvUTF8(ssv))
3198         SvUTF8_on(dsv);
3199     else
3200         SvUTF8_off(dsv);
3201 }
3202
3203 /*
3204 =for apidoc      sv_2pvbyte
3205 =for apidoc_item sv_2pvbyte_flags
3206
3207 These implement the various forms of the L<perlapi/C<SvPVbyte>> macros.
3208 The macros are the preferred interface.
3209
3210 These return a pointer to the byte-encoded representation of the SV, and set
3211 C<*lp> to its length.  If the SV is marked as being encoded as UTF-8, it will
3212 be downgraded, if possible, to a byte string.  If the SV cannot be downgraded,
3213 they croak.
3214
3215 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
3216 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
3217 C<SV_GMAGIC>.
3218
3219 =for apidoc Amnh||SV_GMAGIC
3220
3221 =cut
3222 */
3223
3224 char *
3225 Perl_sv_2pvbyte_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3226 {
3227     PERL_ARGS_ASSERT_SV_2PVBYTE_FLAGS;
3228
3229     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3230         mg_get(sv);
3231     if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3232      || isGV_with_GP(sv) || SvROK(sv)) {
3233         SV *sv2 = sv_newmortal();
3234         sv_copypv_nomg(sv2,sv);
3235         sv = sv2;
3236     }
3237     sv_utf8_downgrade_nomg(sv,0);
3238     return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3239 }
3240
3241 /*
3242 =for apidoc      sv_2pvutf8
3243 =for apidoc_item sv_2pvutf8_flags
3244
3245 These implement the various forms of the L<perlapi/C<SvPVutf8>> macros.
3246 The macros are the preferred interface.
3247
3248 These return a pointer to the UTF-8-encoded representation of the SV, and set
3249 C<*lp> to its length in bytes.  They may cause the SV to be upgraded to UTF-8
3250 as a side-effect.
3251
3252 The forms differ in that plain C<sv_2pvutf8> always processes 'get' magic; and
3253 C<sv_2pvutf8_flags> processes 'get' magic if and only if C<flags> contains
3254 C<SV_GMAGIC>.
3255
3256 =cut
3257 */
3258
3259 char *
3260 Perl_sv_2pvutf8_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3261 {
3262     PERL_ARGS_ASSERT_SV_2PVUTF8_FLAGS;
3263
3264     if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3265         mg_get(sv);
3266     if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3267      || isGV_with_GP(sv) || SvROK(sv)) {
3268         SV *sv2 = sv_newmortal();
3269         sv_copypv_nomg(sv2,sv);
3270         sv = sv2;
3271     }
3272     sv_utf8_upgrade_nomg(sv);
3273     return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3274 }
3275
3276
3277 /*
3278 =for apidoc sv_2bool
3279
3280 This macro is only used by C<sv_true()> or its macro equivalent, and only if
3281 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>.
3282 It calls C<sv_2bool_flags> with the C<SV_GMAGIC> flag.
3283
3284 =for apidoc sv_2bool_flags
3285
3286 This function is only used by C<sv_true()> and friends,  and only if
3287 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>.  If the flags
3288 contain C<SV_GMAGIC>, then it does an C<mg_get()> first.
3289
3290
3291 =cut
3292 */
3293
3294 bool
3295 Perl_sv_2bool_flags(pTHX_ SV *sv, I32 flags)
3296 {
3297     PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
3298
3299     restart:
3300     if(flags & SV_GMAGIC) SvGETMAGIC(sv);
3301
3302     if (!SvOK(sv))
3303         return 0;
3304     if (SvROK(sv)) {
3305         if (SvAMAGIC(sv)) {
3306             SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
3307             if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) {
3308                 bool svb;
3309                 sv = tmpsv;
3310                 if(SvGMAGICAL(sv)) {
3311                     flags = SV_GMAGIC;
3312                     goto restart; /* call sv_2bool */
3313                 }
3314                 /* expanded SvTRUE_common(sv, (flags = 0, goto restart)) */
3315                 else if(!SvOK(sv)) {
3316                     svb = 0;
3317                 }
3318                 else if(SvPOK(sv)) {
3319                     svb = SvPVXtrue(sv);
3320                 }
3321                 else if((SvFLAGS(sv) & (SVf_IOK|SVf_NOK))) {
3322                     svb = (SvIOK(sv) && SvIVX(sv) != 0)
3323                         || (SvNOK(sv) && SvNVX(sv) != 0.0);
3324                 }
3325                 else {
3326                     flags = 0;
3327                     goto restart; /* call sv_2bool_nomg */
3328                 }
3329                 return cBOOL(svb);
3330             }
3331         }
3332         assert(SvRV(sv));
3333         return TRUE;
3334     }
3335     if (isREGEXP(sv))
3336         return
3337           RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
3338
3339     if (SvNOK(sv) && !SvPOK(sv))
3340         return SvNVX(sv) != 0.0;
3341
3342     return SvTRUE_common(sv, 0);
3343 }
3344
3345 /*
3346 =for apidoc sv_utf8_upgrade
3347 =for apidoc_item sv_utf8_upgrade_flags
3348 =for apidoc_item sv_utf8_upgrade_flags_grow
3349 =for apidoc_item sv_utf8_upgrade_nomg
3350
3351 These convert the PV of an SV to its UTF-8-encoded form.
3352 The SV is forced to string form if it is not already.
3353 They always set the C<SvUTF8> flag to avoid future validity checks even if the
3354 whole string is the same in UTF-8 as not.
3355 They return the number of bytes in the converted string
3356
3357 The forms differ in just two ways.  The main difference is whether or not they
3358 perform 'get magic' on C<sv>.  C<sv_utf8_upgrade_nomg> skips 'get magic';
3359 C<sv_utf8_upgrade> performs it; and C<sv_utf8_upgrade_flags> and
3360 C<sv_utf8_upgrade_flags_grow> either perform it (if the C<SV_GMAGIC> bit is set
3361 in C<flags>) or don't (if that bit is cleared).
3362
3363 The other difference is that C<sv_utf8_upgrade_flags_grow> has an additional
3364 parameter, C<extra>, which allows the caller to specify an amount of space to
3365 be reserved as spare beyond what is needed for the actual conversion.  This is
3366 used when the caller knows it will soon be needing yet more space, and it is
3367 more efficient to request space from the system in a single call.
3368 This form is otherwise identical to C<sv_utf8_upgrade_flags>.
3369
3370 These are not a general purpose byte encoding to Unicode interface: use the
3371 Encode extension for that.
3372
3373 The C<SV_FORCE_UTF8_UPGRADE> flag is now ignored.
3374
3375 =for apidoc Amnh||SV_GMAGIC|
3376 =for apidoc Amnh||SV_FORCE_UTF8_UPGRADE|
3377
3378 =cut
3379
3380 If the routine itself changes the string, it adds a trailing C<NUL>.  Such a
3381 C<NUL> isn't guaranteed due to having other routines do the work in some input
3382 cases, or if the input is already flagged as being in utf8.
3383
3384 */
3385
3386 STRLEN
3387 Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
3388 {
3389     PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
3390
3391     if (sv == &PL_sv_undef)
3392         return 0;
3393     if (!SvPOK_nog(sv)) {
3394         STRLEN len = 0;
3395         if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3396             (void) sv_2pv_flags(sv,&len, flags);
3397             if (SvUTF8(sv)) {
3398                 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3399                 return len;
3400             }
3401         } else {
3402             (void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
3403         }
3404     }
3405
3406     /* SVt_REGEXP's shouldn't be upgraded to UTF8 - they're already
3407      * compiled and individual nodes will remain non-utf8 even if the
3408      * stringified version of the pattern gets upgraded. Whether the
3409      * PVX of a REGEXP should be grown or we should just croak, I don't
3410      * know - DAPM */
3411     if (SvUTF8(sv) || isREGEXP(sv)) {
3412         if (extra) SvGROW(sv, SvCUR(sv) + extra);
3413         return SvCUR(sv);
3414     }
3415
3416     if (SvIsCOW(sv)) {
3417         S_sv_uncow(aTHX_ sv, 0);
3418     }
3419
3420     if (SvCUR(sv) == 0) {
3421         if (extra) SvGROW(sv, extra + 1); /* Make sure is room for a trailing
3422                                              byte */
3423     } else { /* Assume Latin-1/EBCDIC */
3424         /* This function could be much more efficient if we
3425          * had a FLAG in SVs to signal if there are any variant
3426          * chars in the PV.  Given that there isn't such a flag
3427          * make the loop as fast as possible. */
3428         U8 * s = (U8 *) SvPVX_const(sv);
3429         U8 *t = s;
3430
3431         if (is_utf8_invariant_string_loc(s, SvCUR(sv), (const U8 **) &t)) {
3432
3433             /* utf8 conversion not needed because all are invariants.  Mark
3434              * as UTF-8 even if no variant - saves scanning loop */
3435             SvUTF8_on(sv);
3436             if (extra) SvGROW(sv, SvCUR(sv) + extra);
3437             return SvCUR(sv);
3438         }
3439
3440         /* Here, there is at least one variant (t points to the first one), so
3441          * the string should be converted to utf8.  Everything from 's' to
3442          * 't - 1' will occupy only 1 byte each on output.
3443          *
3444          * Note that the incoming SV may not have a trailing '\0', as certain
3445          * code in pp_formline can send us partially built SVs.
3446          *
3447          * There are two main ways to convert.  One is to create a new string
3448          * and go through the input starting from the beginning, appending each
3449          * converted value onto the new string as we go along.  Going this
3450          * route, it's probably best to initially allocate enough space in the
3451          * string rather than possibly running out of space and having to
3452          * reallocate and then copy what we've done so far.  Since everything
3453          * from 's' to 't - 1' is invariant, the destination can be initialized
3454          * with these using a fast memory copy.  To be sure to allocate enough
3455          * space, one could use the worst case scenario, where every remaining
3456          * byte expands to two under UTF-8, or one could parse it and count
3457          * exactly how many do expand.
3458          *
3459          * The other way is to unconditionally parse the remainder of the
3460          * string to figure out exactly how big the expanded string will be,
3461          * growing if needed.  Then start at the end of the string and place
3462          * the character there at the end of the unfilled space in the expanded
3463          * one, working backwards until reaching 't'.
3464          *
3465          * The problem with assuming the worst case scenario is that for very
3466          * long strings, we could allocate much more memory than actually
3467          * needed, which can create performance problems.  If we have to parse
3468          * anyway, the second method is the winner as it may avoid an extra
3469          * copy.  The code used to use the first method under some
3470          * circumstances, but now that there is faster variant counting on
3471          * ASCII platforms, the second method is used exclusively, eliminating
3472          * some code that no longer has to be maintained. */
3473
3474         {
3475             /* Count the total number of variants there are.  We can start
3476              * just beyond the first one, which is known to be at 't' */
3477             const Size_t invariant_length = t - s;
3478             U8 * e = (U8 *) SvEND(sv);
3479
3480             /* The length of the left overs, plus 1. */
3481             const Size_t remaining_length_p1 = e - t;
3482
3483             /* We expand by 1 for the variant at 't' and one for each remaining
3484              * variant (we start looking at 't+1') */
3485             Size_t expansion = 1 + variant_under_utf8_count(t + 1, e);
3486
3487             /* +1 = trailing NUL */
3488             Size_t need = SvCUR(sv) + expansion + extra + 1;
3489             U8 * d;
3490
3491             /* Grow if needed */
3492             if (SvLEN(sv) < need) {
3493                 t = invariant_length + (U8*) SvGROW(sv, need);
3494                 e = t + remaining_length_p1;
3495             }
3496             SvCUR_set(sv, invariant_length + remaining_length_p1 + expansion);
3497
3498             /* Set the NUL at the end */
3499             d = (U8 *) SvEND(sv);
3500             *d-- = '\0';
3501
3502             /* Having decremented d, it points to the position to put the
3503              * very last byte of the expanded string.  Go backwards through
3504              * the string, copying and expanding as we go, stopping when we
3505              * get to the part that is invariant the rest of the way down */
3506
3507             e--;
3508             while (e >= t) {
3509                 if (NATIVE_BYTE_IS_INVARIANT(*e)) {
3510                     *d-- = *e;
3511                 } else {
3512                     *d-- = UTF8_EIGHT_BIT_LO(*e);
3513                     *d-- = UTF8_EIGHT_BIT_HI(*e);
3514                 }
3515                 e--;
3516             }
3517
3518             if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3519                 /* Update pos. We do it at the end rather than during
3520                  * the upgrade, to avoid slowing down the common case
3521                  * (upgrade without pos).
3522                  * pos can be stored as either bytes or characters.  Since
3523                  * this was previously a byte string we can just turn off
3524                  * the bytes flag. */
3525                 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3526                 if (mg) {
3527                     mg->mg_flags &= ~MGf_BYTES;
3528                 }
3529                 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3530                     magic_setutf8(sv,mg); /* clear UTF8 cache */
3531             }
3532         }
3533     }
3534
3535     SvUTF8_on(sv);
3536     return SvCUR(sv);
3537 }
3538
3539 /*
3540 =for apidoc sv_utf8_downgrade
3541 =for apidoc_item sv_utf8_downgrade_flags
3542 =for apidoc_item sv_utf8_downgrade_nomg
3543
3544 These attempt to convert the PV of an SV from characters to bytes.  If the PV
3545 contains a character that cannot fit in a byte, this conversion will fail; in
3546 this case, C<FALSE> is returned if C<fail_ok> is true; otherwise they croak.
3547
3548 They are not a general purpose Unicode to byte encoding interface:
3549 use the C<Encode> extension for that.
3550
3551 They differ only in that:
3552
3553 C<sv_utf8_downgrade> processes 'get' magic on C<sv>.
3554
3555 C<sv_utf8_downgrade_nomg> does not.
3556
3557 C<sv_utf8_downgrade_flags> has an additional C<flags> parameter in which you can specify
3558 C<SV_GMAGIC> to process 'get' magic, or leave it cleared to not process 'get' magic.
3559
3560 =cut
3561 */
3562
3563 bool
3564 Perl_sv_utf8_downgrade_flags(pTHX_ SV *const sv, const bool fail_ok, const U32 flags)
3565 {
3566     PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE_FLAGS;
3567
3568     if (SvPOKp(sv) && SvUTF8(sv)) {
3569         if (SvCUR(sv)) {
3570             U8 *s;
3571             STRLEN len;
3572             U32 mg_flags = flags & SV_GMAGIC;
3573
3574             if (SvIsCOW(sv)) {
3575                 S_sv_uncow(aTHX_ sv, 0);
3576             }
3577             if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3578                 /* update pos */
3579                 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3580                 if (mg && mg->mg_len > 0 && mg->mg_flags & MGf_BYTES) {
3581                         mg->mg_len = sv_pos_b2u_flags(sv, mg->mg_len,
3582                                                 mg_flags|SV_CONST_RETURN);
3583                         mg_flags = 0; /* sv_pos_b2u does get magic */
3584                 }
3585                 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3586                     magic_setutf8(sv,mg); /* clear UTF8 cache */
3587
3588             }
3589             s = (U8 *) SvPV_flags(sv, len, mg_flags);
3590
3591             if (!utf8_to_bytes(s, &len)) {
3592                 if (fail_ok)
3593                     return FALSE;
3594                 else {
3595                     if (PL_op)
3596                         Perl_croak(aTHX_ "Wide character in %s",
3597                                    OP_DESC(PL_op));
3598                     else
3599                         Perl_croak(aTHX_ "Wide character");
3600                 }
3601             }
3602             SvCUR_set(sv, len);
3603         }
3604     }
3605     SvUTF8_off(sv);
3606     return TRUE;
3607 }
3608
3609 /*
3610 =for apidoc sv_utf8_encode
3611
3612 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3613 flag off so that it looks like octets again.
3614
3615 =cut
3616 */
3617
3618 void
3619 Perl_sv_utf8_encode(pTHX_ SV *const sv)
3620 {
3621     PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
3622
3623     if (SvREADONLY(sv)) {
3624         sv_force_normal_flags(sv, 0);
3625     }
3626     (void) sv_utf8_upgrade(sv);
3627     SvUTF8_off(sv);
3628 }
3629
3630 /*
3631 =for apidoc sv_utf8_decode
3632
3633 If the PV of the SV is an octet sequence in Perl's extended UTF-8
3634 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3635 so that it looks like a character.  If the PV contains only single-byte
3636 characters, the C<SvUTF8> flag stays off.
3637 Scans PV for validity and returns FALSE if the PV is invalid UTF-8.
3638
3639 =cut
3640 */
3641
3642 bool
3643 Perl_sv_utf8_decode(pTHX_ SV *const sv)
3644 {
3645     PERL_ARGS_ASSERT_SV_UTF8_DECODE;
3646
3647     if (SvPOKp(sv)) {
3648         const U8 *start, *c, *first_variant;
3649
3650         /* The octets may have got themselves encoded - get them back as
3651          * bytes
3652          */
3653         if (!sv_utf8_downgrade(sv, TRUE))
3654             return FALSE;
3655
3656         /* it is actually just a matter of turning the utf8 flag on, but
3657          * we want to make sure everything inside is valid utf8 first.
3658          */
3659         c = start = (const U8 *) SvPVX_const(sv);
3660         if (! is_utf8_invariant_string_loc(c, SvCUR(sv), &first_variant)) {
3661             if (!is_utf8_string(first_variant, SvCUR(sv) - (first_variant -c)))
3662                 return FALSE;
3663             SvUTF8_on(sv);
3664         }
3665         if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3666             /* XXX Is this dead code?  XS_utf8_decode calls SvSETMAGIC
3667                    after this, clearing pos.  Does anything on CPAN
3668                    need this? */
3669             /* adjust pos to the start of a UTF8 char sequence */
3670             MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3671             if (mg) {
3672                 SSize_t pos = mg->mg_len;
3673                 if (pos > 0) {
3674                     for (c = start + pos; c > start; c--) {
3675                         if (UTF8_IS_START(*c))
3676                             break;
3677                     }
3678                     mg->mg_len  = c - start;
3679                 }
3680             }
3681             if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3682                 magic_setutf8(sv,mg); /* clear UTF8 cache */
3683         }
3684     }
3685     return TRUE;
3686 }
3687
3688 /*
3689 =for apidoc sv_setsv
3690 =for apidoc_item sv_setsv_flags
3691 =for apidoc_item sv_setsv_mg
3692 =for apidoc_item sv_setsv_nomg
3693
3694 These copy the contents of the source SV C<ssv> into the destination SV C<dsv>.
3695 C<ssv> may be destroyed if it is mortal, so don't use these functions if
3696 the source SV needs to be reused.
3697 Loosely speaking, they perform a copy-by-value, obliterating any previous
3698 content of the destination.
3699
3700 They differ only in that:
3701
3702 C<sv_setsv> calls 'get' magic on C<ssv>, but skips 'set' magic on C<dsv>.
3703
3704 C<sv_setsv_mg> calls both 'get' magic on C<ssv> and 'set' magic on C<dsv>.
3705
3706 C<sv_setsv_nomg> skips all magic.
3707
3708 C<sv_setsv_flags> has a C<flags> parameter which you can use to specify any
3709 combination of magic handling, and also you can specify C<SV_NOSTEAL> so that
3710 the buffers of temps will not be stolen.
3711
3712 You probably want to instead use one of the assortment of wrappers, such as
3713 C<L</SvSetSV>>, C<L</SvSetSV_nosteal>>, C<L</SvSetMagicSV>> and
3714 C<L</SvSetMagicSV_nosteal>>.
3715
3716 C<sv_setsv_flags> is the primary function for copying scalars, and most other
3717 copy-ish functions and macros use it underneath.
3718
3719 =for apidoc Amnh||SV_NOSTEAL
3720
3721 =cut
3722 */
3723
3724 static void
3725 S_glob_assign_glob(pTHX_ SV *const dsv, SV *const ssv, const int dtype)
3726 {
3727     I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */
3728     HV *old_stash = NULL;
3729
3730     PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
3731
3732     if (dtype != SVt_PVGV && !isGV_with_GP(dsv)) {
3733         const char * const name = GvNAME(ssv);
3734         const STRLEN len = GvNAMELEN(ssv);
3735         {
3736             if (dtype >= SVt_PV) {
3737                 SvPV_free(dsv);
3738                 SvPV_set(dsv, 0);
3739                 SvLEN_set(dsv, 0);
3740                 SvCUR_set(dsv, 0);
3741             }
3742             SvUPGRADE(dsv, SVt_PVGV);
3743             (void)SvOK_off(dsv);
3744             isGV_with_GP_on(dsv);
3745         }
3746         GvSTASH(dsv) = GvSTASH(ssv);
3747         if (GvSTASH(dsv))
3748             Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
3749         gv_name_set(MUTABLE_GV(dsv), name, len,
3750                         GV_ADD | (GvNAMEUTF8(ssv) ? SVf_UTF8 : 0 ));
3751         SvFAKE_on(dsv); /* can coerce to non-glob */
3752     }
3753
3754     if(GvGP(MUTABLE_GV(ssv))) {
3755         /* If source has method cache entry, clear it */
3756         if(GvCVGEN(ssv)) {
3757             SvREFCNT_dec(GvCV(ssv));
3758             GvCV_set(ssv, NULL);
3759             GvCVGEN(ssv) = 0;
3760         }
3761         /* If source has a real method, then a method is
3762            going to change */
3763         else if(
3764          GvCV((const GV *)ssv) && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
3765         ) {
3766             mro_changes = 1;
3767         }
3768     }
3769
3770     /* If dest already had a real method, that's a change as well */
3771     if(
3772         !mro_changes && GvGP(MUTABLE_GV(dsv)) && GvCVu((const GV *)dsv)
3773      && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
3774     ) {
3775         mro_changes = 1;
3776     }
3777
3778     /* We don't need to check the name of the destination if it was not a
3779        glob to begin with. */
3780     if(dtype == SVt_PVGV) {
3781         const char * const name = GvNAME((const GV *)dsv);
3782         const STRLEN len = GvNAMELEN(dsv);
3783         if(memEQs(name, len, "ISA")
3784          /* The stash may have been detached from the symbol table, so
3785             check its name. */
3786          && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
3787         )
3788             mro_changes = 2;
3789         else {
3790             if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
3791              || (len == 1 && name[0] == ':')) {
3792                 mro_changes = 3;
3793
3794                 /* Set aside the old stash, so we can reset isa caches on
3795                    its subclasses. */
3796                 if((old_stash = GvHV(dsv)))
3797                     /* Make sure we do not lose it early. */
3798                     SvREFCNT_inc_simple_void_NN(
3799                      sv_2mortal((SV *)old_stash)
3800                     );
3801             }
3802         }
3803
3804         SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
3805     }
3806
3807     /* freeing dsv's GP might free ssv (e.g. *x = $x),
3808      * so temporarily protect it */
3809     ENTER;
3810     SAVEFREESV(SvREFCNT_inc_simple_NN(ssv));
3811     gp_free(MUTABLE_GV(dsv));
3812     GvINTRO_off(dsv);           /* one-shot flag */
3813     GvGP_set(dsv, gp_ref(GvGP(ssv)));
3814     LEAVE;
3815
3816     if (SvTAINTED(ssv))
3817         SvTAINT(dsv);
3818     if (GvIMPORTED(dsv) != GVf_IMPORTED
3819         && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
3820         {
3821             GvIMPORTED_on(dsv);
3822         }
3823     GvMULTI_on(dsv);
3824     if(mro_changes == 2) {
3825       if (GvAV((const GV *)ssv)) {
3826         MAGIC *mg;
3827         SV * const sref = (SV *)GvAV((const GV *)dsv);
3828         if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
3829             if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3830                 AV * const ary = newAV_alloc_x(2);
3831                 av_push_simple(ary, mg->mg_obj); /* takes the refcount */
3832                 av_push_simple(ary, SvREFCNT_inc_simple_NN(dsv));
3833                 mg->mg_obj = (SV *)ary;
3834             } else {
3835                 av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dsv));
3836             }
3837         }
3838         else sv_magic(sref, dsv, PERL_MAGIC_isa, NULL, 0);
3839       }
3840       mro_isa_changed_in(GvSTASH(dsv));
3841     }
3842     else if(mro_changes == 3) {
3843         HV * const stash = GvHV(dsv);
3844         if(old_stash ? HvHasENAME(old_stash) : cBOOL(stash))
3845             mro_package_moved(
3846                 stash, old_stash,
3847                 (GV *)dsv, 0
3848             );
3849     }
3850     else if(mro_changes) mro_method_changed_in(GvSTASH(dsv));
3851     if (GvIO(dsv) && dtype == SVt_PVGV) {
3852         DEBUG_o(Perl_deb(aTHX_
3853                         "glob_assign_glob clearing PL_stashcache\n"));
3854         /* It's a cache. It will rebuild itself quite happily.
3855            It's a lot of effort to work out exactly which key (or keys)
3856            might be invalidated by the creation of the this file handle.
3857          */
3858         hv_clear(PL_stashcache);
3859     }
3860     return;
3861 }
3862
3863 void
3864 Perl_gv_setref(pTHX_ SV *const dsv, SV *const ssv)
3865 {
3866     SV * const sref = SvRV(ssv);
3867     SV *dref;
3868     const int intro = GvINTRO(dsv);
3869     SV **location;
3870     U8 import_flag = 0;
3871     const U32 stype = SvTYPE(sref);
3872
3873     PERL_ARGS_ASSERT_GV_SETREF;
3874
3875     if (intro) {
3876         GvINTRO_off(dsv);       /* one-shot flag */
3877         GvLINE(dsv) = CopLINE(PL_curcop);
3878         GvEGV(dsv) = MUTABLE_GV(dsv);
3879     }
3880     GvMULTI_on(dsv);
3881     switch (stype) {
3882     case SVt_PVCV:
3883         location = (SV **) &(GvGP(dsv)->gp_cv); /* XXX bypassing GvCV_set */
3884         import_flag = GVf_IMPORTED_CV;
3885         goto common;
3886     case SVt_PVHV:
3887         location = (SV **) &GvHV(dsv);
3888         import_flag = GVf_IMPORTED_HV;
3889         goto common;
3890     case SVt_PVAV:
3891         location = (SV **) &GvAV(dsv);
3892         import_flag = GVf_IMPORTED_AV;
3893         goto common;
3894     case SVt_PVIO:
3895         location = (SV **) &GvIOp(dsv);
3896         goto common;
3897     case SVt_PVFM:
3898         location = (SV **) &GvFORM(dsv);
3899         goto common;
3900     default:
3901         location = &GvSV(dsv);
3902         import_flag = GVf_IMPORTED_SV;
3903     common:
3904         if (intro) {
3905             if (stype == SVt_PVCV) {
3906                 /*if (GvCVGEN(dsv) && (GvCV(dsv) != (const CV *)sref || GvCVGEN(dsv))) {*/
3907                 if (GvCVGEN(dsv)) {
3908                     SvREFCNT_dec(GvCV(dsv));
3909                     GvCV_set(dsv, NULL);
3910                     GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3911                 }
3912             }
3913             /* SAVEt_GVSLOT takes more room on the savestack and has more
3914                overhead in leave_scope than SAVEt_GENERIC_SV.  But for CVs
3915                leave_scope needs access to the GV so it can reset method
3916                caches.  We must use SAVEt_GVSLOT whenever the type is
3917                SVt_PVCV, even if the stash is anonymous, as the stash may
3918                gain a name somehow before leave_scope. */
3919             if (stype == SVt_PVCV) {
3920                 /* There is no save_pushptrptrptr.  Creating it for this
3921                    one call site would be overkill.  So inline the ss add
3922                    routines here. */
3923                 dSS_ADD;
3924                 SS_ADD_PTR(dsv);
3925                 SS_ADD_PTR(location);
3926                 SS_ADD_PTR(SvREFCNT_inc(*location));
3927                 SS_ADD_UV(SAVEt_GVSLOT);
3928                 SS_ADD_END(4);
3929             }
3930             else SAVEGENERICSV(*location);
3931         }
3932         dref = *location;
3933         if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dsv))) {
3934             CV* const cv = MUTABLE_CV(*location);
3935             if (cv) {
3936                 if (!GvCVGEN((const GV *)dsv) &&
3937                     (CvROOT(cv) || CvXSUB(cv)) &&
3938                     /* redundant check that avoids creating the extra SV
3939                        most of the time: */
3940                     (CvCONST(cv) || (ckWARN(WARN_REDEFINE) && !intro)))
3941                     {
3942                         SV * const new_const_sv =
3943                             CvCONST((const CV *)sref)
3944                                  ? cv_const_sv_or_av((const CV *)sref)
3945                                  : NULL;
3946                         HV * const stash = GvSTASH((const GV *)dsv);
3947                         report_redefined_cv(
3948                            sv_2mortal(
3949                              stash
3950                                ? Perl_newSVpvf(aTHX_
3951                                     "%" HEKf "::%" HEKf,
3952                                     HEKfARG(HvNAME_HEK(stash)),
3953                                     HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3954                                : Perl_newSVpvf(aTHX_
3955                                     "%" HEKf,
3956                                     HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3957                            ),
3958                            cv,
3959                            CvCONST((const CV *)sref) ? &new_const_sv : NULL
3960                         );
3961                     }
3962                 if (!intro)
3963                     cv_ckproto_len_flags(cv, (const GV *)dsv,
3964                                    SvPOK(sref) ? CvPROTO(sref) : NULL,
3965                                    SvPOK(sref) ? CvPROTOLEN(sref) : 0,
3966                                    SvPOK(sref) ? SvUTF8(sref) : 0);
3967             }
3968             GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3969             GvASSUMECV_on(dsv);
3970             if(GvSTASH(dsv)) { /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
3971                 if (intro && GvREFCNT(dsv) > 1) {
3972                     /* temporary remove extra savestack's ref */
3973                     --GvREFCNT(dsv);
3974                     gv_method_changed(dsv);
3975                     ++GvREFCNT(dsv);
3976                 }
3977                 else gv_method_changed(dsv);
3978             }
3979         }
3980         *location = SvREFCNT_inc_simple_NN(sref);
3981         if (import_flag && !(GvFLAGS(dsv) & import_flag)
3982             && CopSTASH_ne(PL_curcop, GvSTASH(dsv))) {
3983             GvFLAGS(dsv) |= import_flag;
3984         }
3985
3986         if (stype == SVt_PVHV) {
3987             const char * const name = GvNAME((GV*)dsv);
3988             const STRLEN len = GvNAMELEN(dsv);
3989             if (
3990                 (
3991                    (len > 1 && name[len-2] == ':' && name[len-1] == ':')
3992                 || (len == 1 && name[0] == ':')
3993                 )
3994              && (!dref || HvHasENAME(dref))
3995             ) {
3996                 mro_package_moved(
3997                     (HV *)sref, (HV *)dref,
3998                     (GV *)dsv, 0
3999                 );
4000             }
4001         }
4002         else if (
4003             stype == SVt_PVAV && sref != dref
4004          && memEQs(GvNAME((GV*)dsv), GvNAMELEN((GV*)dsv), "ISA")
4005          /* The stash may have been detached from the symbol table, so
4006             check its name before doing anything. */
4007          && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
4008         ) {
4009             MAGIC *mg;
4010             MAGIC * const omg = dref && SvSMAGICAL(dref)
4011                                  ? mg_find(dref, PERL_MAGIC_isa)
4012                                  : NULL;
4013             if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
4014                 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
4015                     AV * const ary = newAV_alloc_xz(4);
4016                     av_push_simple(ary, mg->mg_obj); /* takes the refcount */
4017                     mg->mg_obj = (SV *)ary;
4018                 }
4019                 if (omg) {
4020                     if (SvTYPE(omg->mg_obj) == SVt_PVAV) {
4021                         SV **svp = AvARRAY((AV *)omg->mg_obj);
4022                         I32 items = AvFILLp((AV *)omg->mg_obj) + 1;
4023                         while (items--)
4024                             av_push(
4025                              (AV *)mg->mg_obj,
4026                              SvREFCNT_inc_simple_NN(*svp++)
4027                             );
4028                     }
4029                     else
4030                         av_push(
4031                          (AV *)mg->mg_obj,
4032                          SvREFCNT_inc_simple_NN(omg->mg_obj)
4033                         );
4034                 }
4035                 else
4036                     av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dsv));
4037             }
4038             else
4039             {
4040                 SSize_t i;
4041                 sv_magic(
4042                  sref, omg ? omg->mg_obj : dsv, PERL_MAGIC_isa, NULL, 0
4043                 );
4044                 for (i = 0; i <= AvFILL(sref); ++i) {
4045                     SV **elem = av_fetch ((AV*)sref, i, 0);
4046                     if (elem) {
4047                         sv_magic(
4048                           *elem, sref, PERL_MAGIC_isaelem, NULL, i
4049                         );
4050                     }
4051                 }
4052                 mg = mg_find(sref, PERL_MAGIC_isa);
4053             }
4054             /* Since the *ISA assignment could have affected more than
4055                one stash, don't call mro_isa_changed_in directly, but let
4056                magic_clearisa do it for us, as it already has the logic for
4057                dealing with globs vs arrays of globs. */
4058             assert(mg);
4059             Perl_magic_clearisa(aTHX_ NULL, mg);
4060         }
4061         else if (stype == SVt_PVIO) {
4062             DEBUG_o(Perl_deb(aTHX_ "gv_setref clearing PL_stashcache\n"));
4063             /* It's a cache. It will rebuild itself quite happily.
4064                It's a lot of effort to work out exactly which key (or keys)
4065                might be invalidated by the creation of the this file handle.
4066             */
4067             hv_clear(PL_stashcache);
4068         }
4069         break;
4070     }
4071     if (!intro) SvREFCNT_dec(dref);
4072     if (SvTAINTED(ssv))
4073         SvTAINT(dsv);
4074     return;
4075 }
4076
4077
4078
4079
4080 #ifdef PERL_DEBUG_READONLY_COW
4081 # include <sys/mman.h>
4082
4083 # ifndef PERL_MEMORY_DEBUG_HEADER_SIZE
4084 #  define PERL_MEMORY_DEBUG_HEADER_SIZE 0
4085 # endif
4086
4087 void
4088 Perl_sv_buf_to_ro(pTHX_ SV *sv)
4089 {
4090     struct perl_memory_debug_header * const header =
4091         (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4092     const MEM_SIZE len = header->size;
4093     PERL_ARGS_ASSERT_SV_BUF_TO_RO;
4094 # ifdef PERL_TRACK_MEMPOOL
4095     if (!header->readonly) header->readonly = 1;
4096 # endif
4097     if (mprotect(header, len, PROT_READ))
4098         Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
4099                          header, len, errno);
4100 }
4101
4102 static void
4103 S_sv_buf_to_rw(pTHX_ SV *sv)
4104 {
4105     struct perl_memory_debug_header * const header =
4106         (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4107     const MEM_SIZE len = header->size;
4108     PERL_ARGS_ASSERT_SV_BUF_TO_RW;
4109     if (mprotect(header, len, PROT_READ|PROT_WRITE))
4110         Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
4111                          header, len, errno);
4112 # ifdef PERL_TRACK_MEMPOOL
4113     header->readonly = 0;
4114 # endif
4115 }
4116
4117 #else
4118 # define sv_buf_to_ro(sv)       NOOP
4119 # define sv_buf_to_rw(sv)       NOOP
4120 #endif
4121
4122 void
4123 Perl_sv_setsv_flags(pTHX_ SV *dsv, SV* ssv, const I32 flags)
4124 {
4125     U32 sflags;
4126     int dtype;
4127     svtype stype;
4128     unsigned int both_type;
4129
4130     PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
4131
4132     if (UNLIKELY( ssv == dsv ))
4133         return;
4134
4135     if (UNLIKELY( !ssv ))
4136         ssv = &PL_sv_undef;
4137
4138     stype = SvTYPE(ssv);
4139     dtype = SvTYPE(dsv);
4140     both_type = (stype | dtype);
4141
4142     /* with these values, we can check that both SVs are NULL/IV (and not
4143      * freed) just by testing the or'ed types */
4144     STATIC_ASSERT_STMT(SVt_NULL == 0);
4145     STATIC_ASSERT_STMT(SVt_IV   == 1);
4146     STATIC_ASSERT_STMT(SVt_NV   == 2);
4147 #if NVSIZE <= IVSIZE
4148     if (both_type <= 2) {
4149 #else
4150     if (both_type <= 1) {
4151 #endif
4152         /* both src and dst are UNDEF/IV/RV - maybe NV depending on config,
4153          * so we can do a lot of special-casing */
4154         U32 sflags;
4155         U32 new_dflags;
4156         SV *old_rv = NULL;
4157
4158         /* minimal subset of SV_CHECK_THINKFIRST_COW_DROP(dsv) */
4159         if (SvREADONLY(dsv))
4160             Perl_croak_no_modify();
4161         if (SvROK(dsv)) {
4162             if (SvWEAKREF(dsv))
4163                 sv_unref_flags(dsv, 0);
4164             else
4165                 old_rv = SvRV(dsv);
4166             SvROK_off(dsv);
4167         }
4168
4169         assert(!SvGMAGICAL(ssv));
4170         assert(!SvGMAGICAL(dsv));
4171
4172         sflags = SvFLAGS(ssv);
4173         if (sflags & (SVf_IOK|SVf_ROK)) {
4174             SET_SVANY_FOR_BODYLESS_IV(dsv);
4175             new_dflags = SVt_IV;
4176
4177             if (sflags & SVf_ROK) {
4178                 dsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(ssv));
4179                 new_dflags |= SVf_ROK;
4180             }
4181             else {
4182                 /* both src and dst are <= SVt_IV, so sv_any points to the
4183                  * head; so access the head directly
4184                  */
4185                 assert(    &(ssv->sv_u.svu_iv)
4186                         == &(((XPVIV*) SvANY(ssv))->xiv_iv));
4187                 assert(    &(dsv->sv_u.svu_iv)
4188                         == &(((XPVIV*) SvANY(dsv))->xiv_iv));
4189                 dsv->sv_u.svu_iv = ssv->sv_u.svu_iv;
4190                 new_dflags |= (SVf_IOK|SVp_IOK|(sflags & SVf_IVisUV));
4191             }
4192         }
4193 #if NVSIZE <= IVSIZE
4194         else if (sflags & SVf_NOK) {
4195             SET_SVANY_FOR_BODYLESS_NV(dsv);
4196             new_dflags = (SVt_NV|SVf_NOK|SVp_NOK);
4197
4198             /* both src and dst are <= SVt_MV, so sv_any points to the
4199              * head; so access the head directly
4200              */
4201             assert(    &(ssv->sv_u.svu_nv)
4202                     == &(((XPVNV*) SvANY(ssv))->xnv_u.xnv_nv));
4203             assert(    &(dsv->sv_u.svu_nv)
4204                     == &(((XPVNV*) SvANY(dsv))->xnv_u.xnv_nv));
4205             dsv->sv_u.svu_nv = ssv->sv_u.svu_nv;
4206         }
4207 #endif
4208         else {
4209             new_dflags = dtype; /* turn off everything except the type */
4210         }
4211         /* Should preserve some dsv flags - at least SVs_TEMP, */
4212         /* so cannot just set SvFLAGS(dsv) = new_dflags        */
4213         /* First clear the flags that we do want to clobber    */
4214         (void)SvOK_off(dsv);
4215         SvFLAGS(dsv) &= ~SVTYPEMASK;
4216         /* Now set the new flags */
4217         SvFLAGS(dsv) |= new_dflags;
4218
4219         SvREFCNT_dec(old_rv);
4220         return;
4221     }
4222
4223     if (UNLIKELY(both_type == SVTYPEMASK)) {
4224         if (SvIS_FREED(dsv)) {
4225             Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
4226                        " to a freed scalar %p", SVfARG(ssv), (void *)dsv);
4227         }
4228         if (SvIS_FREED(ssv)) {
4229             Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
4230                        (void*)ssv, (void*)dsv);
4231         }
4232     }
4233
4234
4235
4236     SV_CHECK_THINKFIRST_COW_DROP(dsv);
4237     dtype = SvTYPE(dsv); /* THINKFIRST may have changed type */
4238
4239     /* There's a lot of redundancy below but we're going for speed here
4240      * Note: some of the cases below do return; rather than break; so the
4241      * if-elseif-else logic below this switch does not see all cases. */
4242
4243     switch (stype) {
4244     case SVt_NULL:
4245       undef_sstr:
4246         if (LIKELY( dtype != SVt_PVGV && dtype != SVt_PVLV )) {
4247             (void)SvOK_off(dsv);
4248             return;
4249         }
4250         break;
4251     case SVt_IV:
4252         if (SvIOK(ssv)) {
4253             switch (dtype) {
4254             case SVt_NULL:
4255                 /* For performance, we inline promoting to type SVt_IV. */
4256                 /* We're starting from SVt_NULL, so provided that define is
4257                  * actual 0, we don't have to unset any SV type flags
4258                  * to promote to SVt_IV. */
4259                 STATIC_ASSERT_STMT(SVt_NULL == 0);
4260                 SET_SVANY_FOR_BODYLESS_IV(dsv);
4261                 SvFLAGS(dsv) |= SVt_IV;
4262                 break;
4263             case SVt_NV:
4264             case SVt_PV:
4265                 sv_upgrade(dsv, SVt_PVIV);
4266                 break;
4267             case SVt_PVGV:
4268             case SVt_PVLV:
4269                 goto end_of_first_switch;
4270             }
4271             (void)SvIOK_only(dsv);
4272             SvIV_set(dsv,  SvIVX(ssv));
4273             if (SvIsUV(ssv))
4274                 SvIsUV_on(dsv);
4275             /* SvTAINTED can only be true if the SV has taint magic, which in
4276                turn means that the SV type is PVMG (or greater). This is the
4277                case statement for SVt_IV, so this cannot be true (whatever gcov
4278                may say).  */
4279             assert(!SvTAINTED(ssv));
4280             return;
4281         }
4282         if (!SvROK(ssv))
4283             goto undef_sstr;
4284         if (dtype < SVt_PV && dtype != SVt_IV)
4285             sv_upgrade(dsv, SVt_IV);
4286         break;
4287
4288     case SVt_NV:
4289         if (LIKELY( SvNOK(ssv) )) {
4290             switch (dtype) {
4291             case SVt_NULL:
4292             case SVt_IV:
4293                 sv_upgrade(dsv, SVt_NV);
4294                 break;
4295             case SVt_PV:
4296             case SVt_PVIV:
4297                 sv_upgrade(dsv, SVt_PVNV);
4298                 break;
4299             case SVt_PVGV:
4300             case SVt_PVLV:
4301                 goto end_of_first_switch;
4302             }
4303             SvNV_set(dsv, SvNVX(ssv));
4304             (void)SvNOK_only(dsv);
4305             /* SvTAINTED can only be true if the SV has taint magic, which in
4306                turn means that the SV type is PVMG (or greater). This is the
4307                case statement for SVt_NV, so this cannot be true (whatever gcov
4308                may say).  */
4309             assert(!SvTAINTED(ssv));
4310             return;
4311         }
4312         goto undef_sstr;
4313
4314     case SVt_PV:
4315         if (dtype < SVt_PV)
4316             sv_upgrade(dsv, SVt_PV);
4317         break;
4318     case SVt_PVIV:
4319         if (dtype < SVt_PVIV)
4320             sv_upgrade(dsv, SVt_PVIV);
4321         break;
4322     case SVt_PVNV:
4323         if (dtype < SVt_PVNV)
4324             sv_upgrade(dsv, SVt_PVNV);
4325         break;
4326
4327     case SVt_INVLIST:
4328         invlist_clone(ssv, dsv);
4329         return;
4330     default:
4331         {
4332         const char * const type = sv_reftype(ssv,0);
4333         if (PL_op)
4334             /* diag_listed_as: Bizarre copy of %s */
4335             Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
4336         else
4337             Perl_croak(aTHX_ "Bizarre copy of %s", type);
4338         }
4339         NOT_REACHED; /* NOTREACHED */
4340
4341     case SVt_REGEXP:
4342       upgregexp:
4343         if (dtype < SVt_REGEXP)
4344             sv_upgrade(dsv, SVt_REGEXP);
4345         break;
4346
4347     case SVt_PVLV:
4348     case SVt_PVGV:
4349     case SVt_PVMG:
4350         if (SvGMAGICAL(ssv) && (flags & SV_GMAGIC)) {
4351             mg_get(ssv);
4352             if (SvTYPE(ssv) != stype)
4353                 stype = SvTYPE(ssv);
4354         }
4355         if (isGV_with_GP(ssv) && dtype <= SVt_PVLV) {
4356                     glob_assign_glob(dsv, ssv, dtype);
4357                     return;
4358         }
4359         if (stype == SVt_PVLV)
4360         {
4361             if (isREGEXP(ssv)) goto upgregexp;
4362             SvUPGRADE(dsv, SVt_PVNV);
4363         }
4364         else
4365             SvUPGRADE(dsv, (svtype)stype);
4366     }
4367  end_of_first_switch:
4368
4369     /* dsv may have been upgraded.  */
4370     dtype = SvTYPE(dsv);
4371     sflags = SvFLAGS(ssv);
4372
4373     if (UNLIKELY( dtype == SVt_PVCV )) {
4374         /* Assigning to a subroutine sets the prototype.  */
4375         if (SvOK(ssv)) {
4376             STRLEN len;
4377             const char *const ptr = SvPV_const(ssv, len);
4378
4379             SvGROW(dsv, len + 1);
4380             Copy(ptr, SvPVX(dsv), len + 1, char);
4381             SvCUR_set(dsv, len);
4382             SvPOK_only(dsv);
4383             SvFLAGS(dsv) |= sflags & SVf_UTF8;
4384             CvAUTOLOAD_off(dsv);
4385         } else {
4386             SvOK_off(dsv);
4387         }
4388     }
4389     else if (UNLIKELY(dtype == SVt_PVAV || dtype == SVt_PVHV
4390              || dtype == SVt_PVFM))
4391     {
4392         const char * const type = sv_reftype(dsv,0);
4393         if (PL_op)
4394             /* diag_listed_as: Cannot copy to %s */
4395             Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op));
4396         else
4397             Perl_croak(aTHX_ "Cannot copy to %s", type);
4398     } else if (sflags & SVf_ROK) {
4399         if (isGV_with_GP(dsv)
4400             && SvTYPE(SvRV(ssv)) == SVt_PVGV && isGV_with_GP(SvRV(ssv))) {
4401             ssv = SvRV(ssv);
4402             if (ssv == dsv) {
4403                 if (GvIMPORTED(dsv) != GVf_IMPORTED
4404                     && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
4405                 {
4406                     GvIMPORTED_on(dsv);
4407                 }
4408                 GvMULTI_on(dsv);
4409                 return;
4410             }
4411             glob_assign_glob(dsv, ssv, dtype);
4412             return;
4413         }
4414
4415         if (dtype >= SVt_PV) {
4416             if (isGV_with_GP(dsv)) {
4417                 gv_setref(dsv, ssv);
4418                 return;
4419             }
4420             if (SvPVX_const(dsv)) {
4421                 SvPV_free(dsv);
4422                 SvLEN_set(dsv, 0);
4423                 SvCUR_set(dsv, 0);
4424             }
4425         }
4426         (void)SvOK_off(dsv);
4427         SvRV_set(dsv, SvREFCNT_inc(SvRV(ssv)));
4428         SvFLAGS(dsv) |= sflags & SVf_ROK;
4429         assert(!(sflags & SVp_NOK));
4430         assert(!(sflags & SVp_IOK));
4431         assert(!(sflags & SVf_NOK));
4432         assert(!(sflags & SVf_IOK));
4433     }
4434     else if (isGV_with_GP(dsv)) {
4435         if (!(sflags & SVf_OK)) {
4436             Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4437                            "Undefined value assigned to typeglob");
4438         }
4439         else {
4440             GV *gv = gv_fetchsv_nomg(ssv, GV_ADD, SVt_PVGV);
4441             if (dsv != (const SV *)gv) {
4442                 const char * const name = GvNAME((const GV *)dsv);
4443                 const STRLEN len = GvNAMELEN(dsv);
4444                 HV *old_stash = NULL;
4445                 bool reset_isa = FALSE;
4446                 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
4447                  || (len == 1 && name[0] == ':')) {
4448                     /* Set aside the old stash, so we can reset isa caches
4449                        on its subclasses. */
4450                     if((old_stash = GvHV(dsv))) {
4451                         /* Make sure we do not lose it early. */
4452                         SvREFCNT_inc_simple_void_NN(
4453                          sv_2mortal((SV *)old_stash)
4454                         );
4455                     }
4456                     reset_isa = TRUE;
4457                 }
4458
4459                 if (GvGP(dsv)) {
4460                     SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
4461                     gp_free(MUTABLE_GV(dsv));
4462                 }
4463                 GvGP_set(dsv, gp_ref(GvGP(gv)));
4464
4465                 if (reset_isa) {
4466                     HV * const stash = GvHV(dsv);
4467                     if(
4468                         old_stash ? HvHasENAME(old_stash) : cBOOL(stash)
4469                     )
4470                         mro_package_moved(
4471                          stash, old_stash,
4472                          (GV *)dsv, 0
4473                         );
4474                 }
4475             }
4476         }
4477     }
4478     else if ((dtype == SVt_REGEXP || dtype == SVt_PVLV)
4479           && (stype == SVt_REGEXP || isREGEXP(ssv))) {
4480         reg_temp_copy((REGEXP*)dsv, (REGEXP*)ssv);
4481     }
4482     else if (sflags & SVp_POK) {
4483         const STRLEN cur = SvCUR(ssv);
4484         const STRLEN len = SvLEN(ssv);
4485
4486         /*
4487          * We have three basic ways to copy the string:
4488          *
4489          *  1. Swipe
4490          *  2. Copy-on-write
4491          *  3. Actual copy
4492          *
4493          * Which we choose is based on various factors.  The following
4494          * things are listed in order of speed, fastest to slowest:
4495          *  - Swipe
4496          *  - Copying a short string
4497          *  - Copy-on-write bookkeeping
4498          *  - malloc
4499          *  - Copying a long string
4500          *
4501          * We swipe the string (steal the string buffer) if the SV on the
4502          * rhs is about to be freed anyway (TEMP and refcnt==1).  This is a
4503          * big win on long strings.  It should be a win on short strings if
4504          * SvPVX_const(dsv) has to be allocated.  If not, it should not
4505          * slow things down, as SvPVX_const(ssv) would have been freed
4506          * soon anyway.
4507          *
4508          * We also steal the buffer from a PADTMP (operator target) if it
4509          * is â€˜long enough’.  For short strings, a swipe does not help
4510          * here, as it causes more malloc calls the next time the target
4511          * is used.  Benchmarks show that even if SvPVX_const(dsv) has to
4512          * be allocated it is still not worth swiping PADTMPs for short
4513          * strings, as the savings here are small.
4514          *
4515          * If swiping is not an option, then we see whether it is worth using
4516          * copy-on-write.  If the lhs already has a buffer big enough and the
4517          * string is short, we skip it and fall back to method 3, since memcpy
4518          * is faster for short strings than the later bookkeeping overhead that
4519          * copy-on-write entails.
4520
4521          * If the rhs is not a copy-on-write string yet, then we also
4522          * consider whether the buffer is too large relative to the string
4523          * it holds.  Some operations such as readline allocate a large
4524          * buffer in the expectation of reusing it.  But turning such into
4525          * a COW buffer is counter-productive because it increases memory
4526          * usage by making readline allocate a new large buffer the sec-
4527          * ond time round.  So, if the buffer is too large, again, we use
4528          * method 3 (copy).
4529          *
4530          * Finally, if there is no buffer on the left, or the buffer is too
4531          * small, then we use copy-on-write and make both SVs share the
4532          * string buffer.
4533          *
4534          */
4535
4536         /* Whichever path we take through the next code, we want this true,
4537            and doing it now facilitates the COW check.  */
4538         (void)SvPOK_only(dsv);
4539
4540         if (
4541                  (              /* Either ... */
4542                                 /* slated for free anyway (and not COW)? */
4543                     (sflags & (SVs_TEMP|SVf_IsCOW)) == SVs_TEMP
4544                                 /* or a swipable TARG */
4545                  || ((sflags &
4546                            (SVs_PADTMP|SVf_READONLY|SVf_PROTECT|SVf_IsCOW))
4547                        == SVs_PADTMP
4548                                 /* whose buffer is worth stealing */
4549                      && CHECK_COWBUF_THRESHOLD(cur,len)
4550                     )
4551                  ) &&
4552                  !(sflags & SVf_OOK) &&   /* and not involved in OOK hack? */
4553                  (!(flags & SV_NOSTEAL)) &&
4554                                         /* and we're allowed to steal temps */
4555                  SvREFCNT(ssv) == 1 &&   /* and no other references to it? */
4556                  len)             /* and really is a string */
4557         {       /* Passes the swipe test.  */
4558             if (SvPVX_const(dsv))       /* we know that dtype >= SVt_PV */
4559                 SvPV_free(dsv);
4560             SvPV_set(dsv, SvPVX_mutable(ssv));
4561             SvLEN_set(dsv, SvLEN(ssv));
4562             SvCUR_set(dsv, SvCUR(ssv));
4563
4564             SvTEMP_off(dsv);
4565             (void)SvOK_off(ssv);        /* NOTE: nukes most SvFLAGS on ssv */
4566             SvPV_set(ssv, NULL);
4567             SvLEN_set(ssv, 0);
4568             SvCUR_set(ssv, 0);
4569             SvTEMP_off(ssv);
4570         }
4571         /* We must check for SvIsCOW_static() even without
4572          * SV_COW_SHARED_HASH_KEYS being set or else we'll break SvIsBOOL()
4573          */
4574         else if (SvIsCOW_static(ssv)) {
4575             if (SvPVX_const(dsv)) {     /* we know that dtype >= SVt_PV */
4576                 SvPV_free(dsv);
4577             }
4578             SvPV_set(dsv, SvPVX(ssv));
4579             SvLEN_set(dsv, 0);
4580             SvCUR_set(dsv, cur);
4581             SvFLAGS(dsv) |= (SVf_IsCOW|SVppv_STATIC);
4582         }
4583         else if (flags & SV_COW_SHARED_HASH_KEYS
4584               &&
4585 #ifdef PERL_COPY_ON_WRITE
4586                  (sflags & SVf_IsCOW
4587                    ? (!len ||
4588                        (  (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4589                           /* If this is a regular (non-hek) COW, only so
4590                              many COW "copies" are possible. */
4591                        && CowREFCNT(ssv) != SV_COW_REFCNT_MAX  ))
4592                    : (  (sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4593                      && !(SvFLAGS(dsv) & SVf_BREAK)
4594                      && CHECK_COW_THRESHOLD(cur,len) && cur+1 < len
4595                      && (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4596                     ))
4597 #else
4598                  sflags & SVf_IsCOW
4599               && !(SvFLAGS(dsv) & SVf_BREAK)
4600 #endif
4601             ) {
4602             /* Either it's a shared hash key, or it's suitable for
4603                copy-on-write.  */
4604 #ifdef DEBUGGING
4605             if (DEBUG_C_TEST) {
4606                 PerlIO_printf(Perl_debug_log, "Copy on write: ssv --> dsv\n");
4607                 sv_dump(ssv);
4608                 sv_dump(dsv);
4609             }
4610 #endif
4611 #ifdef PERL_ANY_COW
4612             if (!(sflags & SVf_IsCOW)) {
4613                     SvIsCOW_on(ssv);
4614                     CowREFCNT(ssv) = 0;
4615             }
4616 #endif
4617             if (SvPVX_const(dsv)) {     /* we know that dtype >= SVt_PV */
4618                 SvPV_free(dsv);
4619             }
4620
4621 #ifdef PERL_ANY_COW
4622             if (len) {
4623                     if (sflags & SVf_IsCOW) {
4624                         sv_buf_to_rw(ssv);
4625                     }
4626                     CowREFCNT(ssv)++;
4627                     SvPV_set(dsv, SvPVX_mutable(ssv));
4628                     sv_buf_to_ro(ssv);
4629             } else
4630 #endif
4631             {
4632                     /* SvIsCOW_shared_hash */
4633                     DEBUG_C(PerlIO_printf(Perl_debug_log,
4634                                           "Copy on write: Sharing hash\n"));
4635
4636                     assert (SvTYPE(dsv) >= SVt_PV);
4637                     SvPV_set(dsv,
4638                              HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)))));
4639             }
4640             SvLEN_set(dsv, len);
4641             SvCUR_set(dsv, cur);
4642             SvIsCOW_on(dsv);
4643         } else {
4644             /* Failed the swipe test, and we cannot do copy-on-write either.
4645                Have to copy the string.  */
4646             SvGROW(dsv, cur + 1);       /* inlined from sv_setpvn */
4647             Move(SvPVX_const(ssv),SvPVX(dsv),cur,char);
4648             SvCUR_set(dsv, cur);
4649             *SvEND(dsv) = '\0';
4650         }
4651         if (sflags & SVp_NOK) {
4652             SvNV_set(dsv, SvNVX(ssv));
4653             if ((sflags & SVf_NOK) && !(sflags & SVf_POK)) {
4654                 /* Source was SVf_NOK|SVp_NOK|SVp_POK but not SVf_POK, meaning
4655                    a value set as floating point and later stringified, where
4656                   the value happens to be one of the few that we know aren't
4657                   affected by the numeric locale, hence we can cache the
4658                   stringification. Currently that's  +Inf, -Inf and NaN, but
4659                   conceivably we might extend this to -9 .. +9 (excluding -0).
4660                   So mark destination the same: */
4661                 SvFLAGS(dsv) &= ~SVf_POK;
4662             }
4663         }
4664         if (sflags & SVp_IOK) {
4665             SvIV_set(dsv, SvIVX(ssv));
4666             if (sflags & SVf_IVisUV)
4667                 SvIsUV_on(dsv);
4668             if ((sflags & SVf_IOK) && !(sflags & SVf_POK)) {
4669                 /* Source was SVf_IOK|SVp_IOK|SVp_POK but not SVf_POK, meaning
4670                    a value set as an integer and later stringified. So mark
4671                    destination the same: */
4672                 SvFLAGS(dsv) &= ~SVf_POK;
4673             }
4674         }
4675         SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4676         {
4677             const MAGIC * const smg = SvVSTRING_mg(ssv);
4678             if (smg) {
4679                 sv_magic(dsv, NULL, PERL_MAGIC_vstring,
4680                          smg->mg_ptr, smg->mg_len);
4681                 SvRMAGICAL_on(dsv);
4682             }
4683         }
4684     }
4685     else if (sflags & (SVp_IOK|SVp_NOK)) {
4686         (void)SvOK_off(dsv);
4687         SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
4688         if (sflags & SVp_IOK) {
4689             /* XXXX Do we want to set IsUV for IV(ROK)?  Be extra safe... */
4690             SvIV_set(dsv, SvIVX(ssv));
4691         }
4692         if (sflags & SVp_NOK) {
4693             SvNV_set(dsv, SvNVX(ssv));
4694         }
4695     }
4696     else {
4697         if (isGV_with_GP(ssv)) {
4698             gv_efullname3(dsv, MUTABLE_GV(ssv), "*");
4699         }
4700         else
4701             (void)SvOK_off(dsv);
4702     }
4703     if (SvTAINTED(ssv))
4704         SvTAINT(dsv);
4705 }
4706
4707
4708 /*
4709 =for apidoc sv_set_undef
4710
4711 Equivalent to C<sv_setsv(sv, &PL_sv_undef)>, but more efficient.
4712 Doesn't handle set magic.
4713
4714 The perl equivalent is C<$sv = undef;>. Note that it doesn't free any string
4715 buffer, unlike C<undef $sv>.
4716
4717 Introduced in perl 5.25.12.
4718
4719 =cut
4720 */
4721
4722 void
4723 Perl_sv_set_undef(pTHX_ SV *sv)
4724 {
4725     U32 type = SvTYPE(sv);
4726
4727     PERL_ARGS_ASSERT_SV_SET_UNDEF;
4728
4729     /* shortcut, NULL, IV, RV */
4730
4731     if (type <= SVt_IV) {
4732         assert(!SvGMAGICAL(sv));
4733         if (SvREADONLY(sv)) {
4734             /* does undeffing PL_sv_undef count as modifying a read-only
4735              * variable? Some XS code does this */
4736             if (sv == &PL_sv_undef)
4737                 return;
4738             Perl_croak_no_modify();
4739         }
4740
4741         if (SvROK(sv)) {
4742             if (SvWEAKREF(sv))
4743                 sv_unref_flags(sv, 0);
4744             else {
4745                 SV *rv = SvRV(sv);
4746                 SvFLAGS(sv) = type; /* quickly turn off all flags */
4747                 SvREFCNT_dec_NN(rv);
4748                 return;
4749             }
4750         }
4751         SvFLAGS(sv) = type; /* quickly turn off all flags */
4752         return;
4753     }
4754
4755     if (SvIS_FREED(sv))
4756         Perl_croak(aTHX_ "panic: attempt to undefine a freed scalar %p",
4757             (void *)sv);
4758
4759     SV_CHECK_THINKFIRST_COW_DROP(sv);
4760
4761     if (isGV_with_GP(sv))
4762         Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4763                        "Undefined value assigned to typeglob");
4764     else
4765         SvOK_off(sv);
4766 }
4767
4768 /*
4769 =for apidoc sv_set_true
4770
4771 Equivalent to C<sv_setsv(sv, &PL_sv_yes)>, but may be made more
4772 efficient in the future. Doesn't handle set magic.
4773
4774 The perl equivalent is C<$sv = !0;>.
4775
4776 Introduced in perl 5.35.11.
4777
4778 =cut
4779 */
4780
4781 void
4782 Perl_sv_set_true(pTHX_ SV *sv)
4783 {
4784     PERL_ARGS_ASSERT_SV_SET_TRUE;
4785     sv_setsv(sv, &PL_sv_yes);
4786 }
4787
4788 /*
4789 =for apidoc sv_set_false
4790
4791 Equivalent to C<sv_setsv(sv, &PL_sv_no)>, but may be made more
4792 efficient in the future. Doesn't handle set magic.
4793
4794 The perl equivalent is C<$sv = !1;>.
4795
4796 Introduced in perl 5.35.11.
4797
4798 =cut
4799 */
4800
4801 void
4802 Perl_sv_set_false(pTHX_ SV *sv)
4803 {
4804     PERL_ARGS_ASSERT_SV_SET_FALSE;
4805     sv_setsv(sv, &PL_sv_no);
4806 }
4807
4808 /*
4809 =for apidoc sv_set_bool
4810
4811 Equivalent to C<sv_setsv(sv, bool_val ? &Pl_sv_yes : &PL_sv_no)>, but
4812 may be made more efficient in the future. Doesn't handle set magic.
4813
4814 The perl equivalent is C<$sv = !!$expr;>.
4815
4816 Introduced in perl 5.35.11.
4817
4818 =cut
4819 */
4820
4821 void
4822 Perl_sv_set_bool(pTHX_ SV *sv, const bool bool_val)
4823 {
4824     PERL_ARGS_ASSERT_SV_SET_BOOL;
4825     sv_setsv(sv, bool_val ? &PL_sv_yes : &PL_sv_no);
4826 }
4827
4828
4829 void
4830 Perl_sv_setsv_mg(pTHX_ SV *const dsv, SV *const ssv)
4831 {
4832     PERL_ARGS_ASSERT_SV_SETSV_MG;
4833
4834     sv_setsv(dsv,ssv);
4835     SvSETMAGIC(dsv);
4836 }
4837
4838 #ifdef PERL_ANY_COW
4839 #  define SVt_COW SVt_PV
4840 SV *
4841 Perl_sv_setsv_cow(pTHX_ SV *dsv, SV *ssv)
4842 {
4843     STRLEN cur = SvCUR(ssv);
4844     STRLEN len = SvLEN(ssv);
4845     char *new_pv;
4846     U32 new_flags = (SVt_COW|SVf_POK|SVp_POK|SVf_IsCOW);
4847 #if defined(PERL_DEBUG_READONLY_COW) && defined(PERL_COPY_ON_WRITE)
4848     const bool already = cBOOL(SvIsCOW(ssv));
4849 #endif
4850
4851     PERL_ARGS_ASSERT_SV_SETSV_COW;
4852 #ifdef DEBUGGING
4853     if (DEBUG_C_TEST) {
4854         PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4855                       (void*)ssv, (void*)dsv);
4856         sv_dump(ssv);
4857         if (dsv)
4858                     sv_dump(dsv);
4859     }
4860 #endif
4861     if (dsv) {
4862         if (SvTHINKFIRST(dsv))
4863             sv_force_normal_flags(dsv, SV_COW_DROP_PV);
4864         else if (SvPVX_const(dsv))
4865             Safefree(SvPVX_mutable(dsv));
4866         SvUPGRADE(dsv, SVt_COW);
4867     }
4868     else
4869         dsv = newSV_type(SVt_COW);
4870
4871     assert (SvPOK(ssv));
4872     assert (SvPOKp(ssv));
4873
4874     if (SvIsCOW(ssv)) {
4875         if (SvIsCOW_shared_hash(ssv)) {
4876             /* source is a COW shared hash key.  */
4877             DEBUG_C(PerlIO_printf(Perl_debug_log,
4878                                   "Fast copy on write: Sharing hash\n"));
4879             new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv))));
4880             goto common_exit;
4881         }
4882         else if (SvIsCOW_static(ssv)) {
4883             /* source is static constant; preserve this */
4884             new_pv = SvPVX(ssv);
4885             new_flags |= SVppv_STATIC;
4886             goto common_exit;
4887         }
4888         assert(SvCUR(ssv)+1 < SvLEN(ssv));
4889         assert(CowREFCNT(ssv) < SV_COW_REFCNT_MAX);
4890     } else {
4891         assert ((SvFLAGS(ssv) & CAN_COW_MASK) == CAN_COW_FLAGS);
4892         SvUPGRADE(ssv, SVt_COW);
4893         SvIsCOW_on(ssv);
4894         DEBUG_C(PerlIO_printf(Perl_debug_log,
4895                               "Fast copy on write: Converting ssv to COW\n"));
4896         CowREFCNT(ssv) = 0;
4897     }
4898 #  ifdef PERL_DEBUG_READONLY_COW
4899     if (already) sv_buf_to_rw(ssv);
4900 #  endif
4901     CowREFCNT(ssv)++;
4902     new_pv = SvPVX_mutable(ssv);
4903     sv_buf_to_ro(ssv);
4904
4905   common_exit:
4906     SvPV_set(dsv, new_pv);
4907     SvFLAGS(dsv) = new_flags;
4908     if (SvUTF8(ssv))
4909         SvUTF8_on(dsv);
4910     SvLEN_set(dsv, len);
4911     SvCUR_set(dsv, cur);
4912 #ifdef DEBUGGING
4913     if (DEBUG_C_TEST)
4914                 sv_dump(dsv);
4915 #endif
4916     return dsv;
4917 }
4918 #endif
4919
4920 /*
4921 =for apidoc sv_setpv_bufsize
4922
4923 Sets the SV to be a string of cur bytes length, with at least
4924 len bytes available. Ensures that there is a null byte at SvEND.
4925 Returns a char * pointer to the SvPV buffer.
4926
4927 =cut
4928 */
4929
4930 char *
4931 Perl_sv_setpv_bufsize(pTHX_ SV *const sv, const STRLEN cur, const STRLEN len)
4932 {
4933     char *pv;
4934
4935     PERL_ARGS_ASSERT_SV_SETPV_BUFSIZE;
4936
4937     SV_CHECK_THINKFIRST_COW_DROP(sv);
4938     SvUPGRADE(sv, SVt_PV);
4939     pv = SvGROW(sv, len + 1);
4940     SvCUR_set(sv, cur);
4941     *(SvEND(sv))= '\0';
4942     (void)SvPOK_only_UTF8(sv);                /* validate pointer */
4943
4944     SvTAINT(sv);
4945     if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4946     return pv;
4947 }
4948
4949 /*
4950 =for apidoc            sv_setpv
4951 =for apidoc_item       sv_setpv_mg
4952 =for apidoc_item       sv_setpvn
4953 =for apidoc_item       sv_setpvn_fresh
4954 =for apidoc_item       sv_setpvn_mg
4955 =for apidoc_item |void|sv_setpvs|SV* sv|"literal string"
4956 =for apidoc_item |void|sv_setpvs_mg|SV* sv|"literal string"
4957
4958 These copy a string into the SV C<sv>, making sure it is C<L</SvPOK_only>>.
4959
4960 In the C<pvs> forms, the string must be a C literal string, enclosed in double
4961 quotes.
4962
4963 In the C<pvn> forms, the first byte of the string is pointed to by C<ptr>, and
4964 C<len> indicates the number of bytes to be copied, potentially including
4965 embedded C<NUL> characters.
4966
4967 In the plain C<pv> forms, C<ptr> points to a NUL-terminated C string.  That is,
4968 it points to the first byte of the string, and the copy proceeds up through the
4969 first encountered C<NUL> byte.
4970
4971 In the forms that take a C<ptr> argument, if it is NULL, the SV will become
4972 undefined.
4973
4974 The UTF-8 flag is not changed by these functions.  A terminating NUL byte is
4975 guaranteed in the result.
4976
4977 The C<_mg> forms handle 'set' magic; the other forms skip all magic.
4978
4979 C<sv_setpvn_fresh> is a cut-down alternative to C<sv_setpvn>, intended ONLY
4980 to be used with a fresh sv that has been upgraded to a SVt_PV, SVt_PVIV,
4981 SVt_PVNV, or SVt_PVMG.
4982
4983 =cut
4984 */
4985
4986 void
4987 Perl_sv_setpvn(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4988 {
4989     char *dptr;
4990
4991     PERL_ARGS_ASSERT_SV_SETPVN;
4992
4993     SV_CHECK_THINKFIRST_COW_DROP(sv);
4994     if (isGV_with_GP(sv))
4995         Perl_croak_no_modify();
4996     if (!ptr) {
4997         (void)SvOK_off(sv);
4998         return;
4999     }
5000     else {
5001         /* len is STRLEN which is unsigned, need to copy to signed */
5002         const IV iv = len;
5003         if (iv < 0)
5004             Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen %"
5005                        IVdf, iv);
5006     }
5007     SvUPGRADE(sv, SVt_PV);
5008
5009     dptr = SvGROW(sv, len + 1);
5010     Move(ptr,dptr,len,char);
5011     dptr[len] = '\0';
5012     SvCUR_set(sv, len);
5013     (void)SvPOK_only_UTF8(sv);          /* validate pointer */
5014     SvTAINT(sv);
5015     if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
5016 }
5017
5018 void
5019 Perl_sv_setpvn_mg(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
5020 {
5021     PERL_ARGS_ASSERT_SV_SETPVN_MG;
5022
5023     sv_setpvn(sv,ptr,len);
5024     SvSETMAGIC(sv);
5025 }
5026
5027 void
5028 Perl_sv_setpvn_fresh(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
5029 {
5030     char *dptr;
5031
5032     PERL_ARGS_ASSERT_SV_SETPVN_FRESH;
5033     assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
5034     assert(!SvTHINKFIRST(sv));
5035     assert(!isGV_with_GP(sv));
5036
5037     if (ptr) {
5038         const IV iv = len;
5039         /* len is STRLEN which is unsigned, need to copy to signed */
5040         if (iv < 0)
5041             Perl_croak(aTHX_ "panic: sv_setpvn_fresh called with negative strlen %"
5042                        IVdf, iv);
5043
5044         dptr = sv_grow_fresh(sv, len + 1);
5045         Move(ptr,dptr,len,char);
5046         dptr[len] = '\0';
5047         SvCUR_set(sv, len);
5048         SvPOK_on(sv);
5049         SvTAINT(sv);
5050     }
5051 }
5052
5053 void
5054 Perl_sv_setpv(pTHX_ SV *const sv, const char *const ptr)
5055 {
5056     STRLEN len;
5057
5058     PERL_ARGS_ASSERT_SV_SETPV;
5059
5060     SV_CHECK_THINKFIRST_COW_DROP(sv);
5061     if (!ptr) {
5062         (void)SvOK_off(sv);
5063         return;
5064     }
5065     len = strlen(ptr);
5066     SvUPGRADE(sv, SVt_PV);
5067
5068     SvGROW(sv, len + 1);
5069     Move(ptr,SvPVX(sv),len+1,char);
5070     SvCUR_set(sv, len);
5071     (void)SvPOK_only_UTF8(sv);          /* validate pointer */
5072     SvTAINT(sv);
5073     if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
5074 }
5075
5076 void
5077 Perl_sv_setpv_mg(pTHX_ SV *const sv, const char *const ptr)
5078 {
5079     PERL_ARGS_ASSERT_SV_SETPV_MG;
5080
5081     sv_setpv(sv,ptr);
5082     SvSETMAGIC(sv);
5083 }
5084
5085 void
5086 Perl_sv_sethek(pTHX_ SV *const sv, const HEK *const hek)
5087 {
5088     PERL_ARGS_ASSERT_SV_SETHEK;
5089
5090     if (!hek) {
5091         return;
5092     }
5093
5094     if (HEK_LEN(hek) == HEf_SVKEY) {
5095         sv_setsv(sv, *(SV**)HEK_KEY(hek));
5096         return;
5097     } else {
5098         const int flags = HEK_FLAGS(hek);
5099         if (flags & HVhek_WASUTF8) {
5100             STRLEN utf8_len = HEK_LEN(hek);
5101             char *as_utf8 = (char *)bytes_to_utf8((U8*)HEK_KEY(hek), &utf8_len);
5102             sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
5103             SvUTF8_on(sv);
5104             return;
5105         } else if (flags & HVhek_NOTSHARED) {
5106             sv_setpvn(sv, HEK_KEY(hek), HEK_LEN(hek));
5107             if (HEK_UTF8(hek))
5108                 SvUTF8_on(sv);
5109             else SvUTF8_off(sv);
5110             return;
5111         }
5112         {
5113             SV_CHECK_THINKFIRST_COW_DROP(sv);
5114             SvUPGRADE(sv, SVt_PV);
5115             SvPV_free(sv);
5116             SvPV_set(sv,(char *)HEK_KEY(share_hek_hek(hek)));
5117             SvCUR_set(sv, HEK_LEN(hek));
5118             SvLEN_set(sv, 0);
5119             SvIsCOW_on(sv);
5120             SvPOK_on(sv);
5121             if (HEK_UTF8(hek))
5122                 SvUTF8_on(sv);
5123             else SvUTF8_off(sv);
5124             return;
5125         }
5126     }
5127 }
5128
5129
5130 /*
5131 =for apidoc      sv_usepvn
5132 =for apidoc_item sv_usepvn_flags
5133 =for apidoc_item sv_usepvn_mg
5134
5135 These tell an SV to use C<ptr> for its string value.  Normally SVs have
5136 their string stored inside the SV, but these tell the SV to use an
5137 external string instead.
5138
5139 C<ptr> should point to memory that was allocated
5140 by L</C<Newx>>.  It must be
5141 the start of a C<Newx>-ed block of memory, and not a pointer to the
5142 middle of it (beware of L<C<OOK>|perlguts/Offsets> and copy-on-write),
5143 and not be from a non-C<Newx> memory allocator like C<malloc>.  The
5144 string length, C<len>, must be supplied.  By default this function
5145 will L</C<Renew>> (i.e. realloc, move) the memory pointed to by C<ptr>,
5146 so that the pointer should not be freed or used by the programmer after giving
5147 it to C<sv_usepvn>, and neither should any pointers from "behind" that pointer
5148 (I<e.g.>, S<C<ptr> + 1>) be used.
5149
5150 In the C<sv_usepvn_flags> form, if S<C<flags & SV_SMAGIC>> is true,
5151 C<SvSETMAGIC> is called before returning.
5152 And if S<C<flags & SV_HAS_TRAILING_NUL>> is true, then C<ptr[len]> must be
5153 C<NUL>, and the realloc will be skipped (I<i.e.>, the buffer is actually at
5154 least 1 byte longer than C<len>, and already meets the requirements for storing
5155 in C<SvPVX>).
5156
5157 C<sv_usepvn> is merely C<sv_usepvn_flags> with C<flags> set to 0, so 'set'
5158 magic is skipped.
5159
5160 C<sv_usepvn_mg> is merely C<sv_usepvn_flags> with C<flags> set to C<SV_SMAGIC>,
5161 so 'set' magic is performed.
5162
5163 =for apidoc Amnh||SV_SMAGIC
5164 =for apidoc Amnh||SV_HAS_TRAILING_NUL
5165
5166 =cut
5167 */
5168
5169 void
5170 Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags)
5171 {
5172     STRLEN allocate;
5173
5174     PERL_ARGS_ASSERT_SV_USEPVN_FLAGS;
5175
5176     SV_CHECK_THINKFIRST_COW_DROP(sv);
5177     SvUPGRADE(sv, SVt_PV);
5178     if (!ptr) {
5179         (void)SvOK_off(sv);
5180         if (flags & SV_SMAGIC)
5181             SvSETMAGIC(sv);
5182         return;
5183     }
5184     if (SvPVX_const(sv))
5185         SvPV_free(sv);
5186
5187 #ifdef DEBUGGING
5188     if (flags & SV_HAS_TRAILING_NUL)
5189         assert(ptr[len] == '\0');
5190 #endif
5191
5192     allocate = (flags & SV_HAS_TRAILING_NUL)
5193         ? len + 1 :
5194 #ifdef Perl_safesysmalloc_size
5195         len + 1;
5196 #else
5197         PERL_STRLEN_ROUNDUP(len + 1);
5198 #endif
5199     if (flags & SV_HAS_TRAILING_NUL) {
5200         /* It's long enough - do nothing.
5201            Specifically Perl_newCONSTSUB is relying on this.  */
5202     } else {
5203 #ifdef DEBUGGING
5204         /* Force a move to shake out bugs in callers.  */
5205         char *new_ptr = (char*)safemalloc(allocate);
5206         Copy(ptr, new_ptr, len, char);
5207         PoisonFree(ptr,len,char);
5208         Safefree(ptr);
5209         ptr = new_ptr;
5210 #else
5211         ptr = (char*) saferealloc (ptr, allocate);
5212 #endif
5213     }
5214 #ifdef Perl_safesysmalloc_size
5215     SvLEN_set(sv, Perl_safesysmalloc_size(ptr));
5216 #else
5217     SvLEN_set(sv, allocate);
5218 #endif
5219     SvCUR_set(sv, len);
5220     SvPV_set(sv, ptr);
5221     if (!(flags & SV_HAS_TRAILING_NUL)) {
5222         ptr[len] = '\0';
5223     }
5224     (void)SvPOK_only_UTF8(sv);          /* validate pointer */
5225     SvTAINT(sv);
5226     if (flags & SV_SMAGIC)
5227         SvSETMAGIC(sv);
5228 }
5229
5230
5231 static void
5232 S_sv_uncow(pTHX_ SV * const sv, const U32 flags)
5233 {
5234     assert(SvIsCOW(sv));
5235     {
5236 #ifdef PERL_ANY_COW
5237         const char * const pvx = SvPVX_const(sv);
5238         const STRLEN len = SvLEN(sv);
5239         const STRLEN cur = SvCUR(sv);
5240         const bool was_shared_hek = SvIsCOW_shared_hash(sv);
5241
5242 #ifdef DEBUGGING
5243         if (DEBUG_C_TEST) {
5244                 PerlIO_printf(Perl_debug_log,
5245                               "Copy on write: Force normal %ld\n",
5246                               (long) flags);
5247                 sv_dump(sv);
5248         }
5249 #endif
5250         SvIsCOW_off(sv);
5251 # ifdef PERL_COPY_ON_WRITE
5252         if (len) {
5253             /* Must do this first, since the CowREFCNT uses SvPVX and
5254             we need to write to CowREFCNT, or de-RO the whole buffer if we are
5255             the only owner left of the buffer. */
5256             sv_buf_to_rw(sv); /* NOOP if RO-ing not supported */
5257             {
5258                 U8 cowrefcnt = CowREFCNT(sv);
5259                 if(cowrefcnt != 0) {
5260                     cowrefcnt--;
5261                     CowREFCNT(sv) = cowrefcnt;
5262                     sv_buf_to_ro(sv);
5263                     goto copy_over;
5264                 }
5265             }
5266             /* Else we are the only owner of the buffer. */
5267         }
5268         else
5269 # endif
5270         {
5271             /* This SV doesn't own the buffer, so need to Newx() a new one:  */
5272             copy_over:
5273             SvPV_set(sv, NULL);
5274             SvCUR_set(sv, 0);
5275             SvLEN_set(sv, 0);
5276             if (flags & SV_COW_DROP_PV) {
5277                 /* OK, so we don't need to copy our buffer.  */
5278                 SvPOK_off(sv);
5279             } else {
5280                 SvGROW(sv, cur + 1);
5281                 Move(pvx,SvPVX(sv),cur,char);
5282                 SvCUR_set(sv, cur);
5283                 *SvEND(sv) = '\0';
5284             }
5285             if (was_shared_hek) {
5286                         unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5287             }
5288 #ifdef DEBUGGING
5289             if (DEBUG_C_TEST)
5290                 sv_dump(sv);
5291 #endif
5292         }
5293 #else
5294             const char * const pvx = SvPVX_const(sv);
5295             const STRLEN len = SvCUR(sv);
5296             SvIsCOW_off(sv);
5297             SvPV_set(sv, NULL);
5298             SvLEN_set(sv, 0);
5299             if (flags & SV_COW_DROP_PV) {
5300                 /* OK, so we don't need to copy our buffer.  */
5301                 SvPOK_off(sv);
5302             } else {
5303                 SvGROW(sv, len + 1);
5304                 Move(pvx,SvPVX(sv),len,char);
5305                 *SvEND(sv) = '\0';
5306             }
5307             unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5308 #endif
5309     }
5310 }
5311
5312
5313 /*
5314 =for apidoc sv_force_normal_flags
5315
5316 Undo various types of fakery on an SV, where fakery means
5317 "more than" a string: if the PV is a shared string, make
5318 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5319 an C<xpvmg>; if we're a copy-on-write scalar, this is the on-write time when
5320 we do the copy, and is also used locally; if this is a
5321 vstring, drop the vstring magic.  If C<SV_COW_DROP_PV> is set
5322 then a copy-on-write scalar drops its PV buffer (if any) and becomes
5323 C<SvPOK_off> rather than making a copy.  (Used where this
5324 scalar is about to be set to some other value.)  In addition,
5325 the C<flags> parameter gets passed to C<sv_unref_flags()>
5326 when unreffing.  C<sv_force_normal> calls this function
5327 with flags set to 0.
5328
5329 This function is expected to be used to signal to perl that this SV is
5330 about to be written to, and any extra book-keeping needs to be taken care
5331 of.  Hence, it croaks on read-only values.
5332
5333 =for apidoc Amnh||SV_COW_DROP_PV
5334
5335 =cut
5336 */
5337
5338 void
5339 Perl_sv_force_normal_flags(pTHX_ SV *const sv, const U32 flags)
5340 {
5341     PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS;
5342
5343     if (SvREADONLY(sv))
5344         Perl_croak_no_modify();
5345     else if (SvIsCOW(sv) && LIKELY(SvTYPE(sv) != SVt_PVHV))
5346         S_sv_uncow(aTHX_ sv, flags);
5347     if (SvROK(sv))
5348         sv_unref_flags(sv, flags);
5349     else if (SvFAKE(sv) && isGV_with_GP(sv))
5350         sv_unglob(sv, flags);
5351     else if (SvFAKE(sv) && isREGEXP(sv)) {
5352         /* Need to downgrade the REGEXP to a simple(r) scalar. This is analogous
5353            to sv_unglob. We only need it here, so inline it.  */
5354         const bool islv = SvTYPE(sv) == SVt_PVLV;
5355         const svtype new_type =
5356           islv ? SVt_NULL : SvMAGIC(sv) || SvSTASH(sv) ? SVt_PVMG : SVt_PV;
5357         SV *const temp = newSV_type(new_type);
5358         regexp *old_rx_body;
5359
5360         if (new_type == SVt_PVMG) {
5361             SvMAGIC_set(temp, SvMAGIC(sv));
5362             SvMAGIC_set(sv, NULL);
5363             SvSTASH_set(temp, SvSTASH(sv));
5364             SvSTASH_set(sv, NULL);
5365         }
5366         if (!islv)
5367             SvCUR_set(temp, SvCUR(sv));
5368         /* Remember that SvPVX is in the head, not the body. */
5369         assert(ReANY((REGEXP *)sv)->mother_re);
5370
5371         if (islv) {
5372             /* LV-as-regex has sv->sv_any pointing to an XPVLV body,
5373              * whose xpvlenu_rx field points to the regex body */
5374             XPV *xpv = (XPV*)(SvANY(sv));
5375             old_rx_body = xpv->xpv_len_u.xpvlenu_rx;
5376             xpv->xpv_len_u.xpvlenu_rx = NULL;
5377         }
5378         else
5379             old_rx_body = ReANY((REGEXP *)sv);
5380
5381         /* Their buffer is already owned by someone else. */
5382         if (flags & SV_COW_DROP_PV) {
5383             /* SvLEN is already 0.  For SVt_REGEXP, we have a brand new
5384                zeroed body.  For SVt_PVLV, we zeroed it above (len field
5385                a union with xpvlenu_rx) */
5386             assert(!SvLEN(islv ? sv : temp));
5387             sv->sv_u.svu_pv = 0;
5388         }
5389         else {
5390             sv->sv_u.svu_pv = savepvn(RX_WRAPPED((REGEXP *)sv), SvCUR(sv));
5391             SvLEN_set(islv ? sv : temp, SvCUR(sv)+1);
5392             SvPOK_on(sv);
5393         }
5394
5395         /* Now swap the rest of the bodies. */
5396
5397         SvFAKE_off(sv);
5398         if (!islv) {
5399             SvFLAGS(sv) &= ~SVTYPEMASK;
5400             SvFLAGS(sv) |= new_type;
5401             SvANY(sv) = SvANY(temp);
5402         }
5403
5404         SvFLAGS(temp) &= ~(SVTYPEMASK);
5405         SvFLAGS(temp) |= SVt_REGEXP|SVf_FAKE;
5406         SvANY(temp) = old_rx_body;
5407
5408         /* temp is now rebuilt as a correctly structured SVt_REGEXP, so this
5409          * will trigger a call to sv_clear() which will correctly free the
5410          * body. */
5411         SvREFCNT_dec_NN(temp);
5412     }
5413     else if (SvVOK(sv)) sv_unmagic(sv, PERL_MAGIC_vstring);
5414 }
5415
5416 /*
5417 =for apidoc sv_chop
5418
5419 Efficient removal of characters from the beginning of the string buffer.
5420 C<SvPOK(sv)>, or at least C<SvPOKp(sv)>, must be true and C<ptr> must be a
5421 pointer to somewhere inside the string buffer.  C<ptr> becomes the first
5422 character of the adjusted string.  Uses the C<OOK> hack.  On return, only
5423 C<SvPOK(sv)> and C<SvPOKp(sv)> among the C<OK> flags will be true.
5424
5425 Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
5426 refer to the same chunk of data.
5427
5428 The unfortunate similarity of this function's name to that of Perl's C<chop>
5429 operator is strictly coincidental.  This function works from the left;
5430 C<chop> works from the right.
5431
5432 =cut
5433 */
5434
5435 void
5436 Perl_sv_chop(pTHX_ SV *const sv, const char *const ptr)
5437 {
5438     STRLEN delta;
5439     STRLEN old_delta;
5440     U8 *p;
5441 #ifdef DEBUGGING
5442     const U8 *evacp;
5443     STRLEN evacn;
5444 #endif
5445     STRLEN max_delta;
5446
5447     PERL_ARGS_ASSERT_SV_CHOP;
5448
5449     if (!ptr || !SvPOKp(sv))
5450         return;
5451     delta = ptr - SvPVX_const(sv);
5452     if (!delta) {
5453         /* Nothing to do.  */
5454         return;
5455     }
5456     max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv);
5457     if (delta > max_delta)
5458         Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p",
5459                    ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta);
5460     /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), so don't use ptr any more */
5461     SV_CHECK_THINKFIRST(sv);
5462     SvPOK_only_UTF8(sv);
5463
5464     if (!SvOOK(sv)) {
5465         if (!SvLEN(sv)) { /* make copy of shared string */
5466             const char *pvx = SvPVX_const(sv);
5467             const STRLEN len = SvCUR(sv);
5468             SvGROW(sv, len + 1);
5469             Move(pvx,SvPVX(sv),len,char);
5470             *SvEND(sv) = '\0';
5471         }
5472         SvOOK_on(sv);
5473         old_delta = 0;
5474     } else {
5475         SvOOK_offset(sv, old_delta);
5476     }
5477     SvLEN_set(sv, SvLEN(sv) - delta);
5478     SvCUR_set(sv, SvCUR(sv) - delta);
5479     SvPV_set(sv, SvPVX(sv) + delta);
5480
5481     p = (U8 *)SvPVX_const(sv);
5482
5483 #ifdef DEBUGGING
5484     /* how many bytes were evacuated?  we will fill them with sentinel
5485        bytes, except for the part holding the new offset of course. */
5486     evacn = delta;
5487     if (old_delta)
5488         evacn += (old_delta < 0x100 ? 1 : 1 + sizeof(STRLEN));
5489     assert(evacn);
5490     assert(evacn <= delta + old_delta);
5491     evacp = p - evacn;
5492 #endif
5493
5494     /* This sets 'delta' to the accumulated value of all deltas so far */
5495     delta += old_delta;
5496     assert(delta);
5497
5498     /* If 'delta' fits in a byte, store it just prior to the new beginning of
5499      * the string; otherwise store a 0 byte there and store 'delta' just prior
5500      * to that, using as many bytes as a STRLEN occupies.  Thus it overwrites a
5501      * portion of the chopped part of the string */
5502     if (delta < 0x100) {
5503         *--p = (U8) delta;
5504     } else {
5505         *--p = 0;
5506         p -= sizeof(STRLEN);
5507         Copy((U8*)&delta, p, sizeof(STRLEN), U8);
5508     }
5509
5510 #ifdef DEBUGGING
5511     /* Fill the preceding buffer with sentinals to verify that no-one is
5512        using it.  */
5513     while (p > evacp) {
5514         --p;
5515         *p = (U8)PTR2UV(p);
5516     }
5517 #endif
5518 }
5519
5520 /*
5521 =for apidoc sv_catpvn
5522 =for apidoc_item sv_catpvn_flags
5523 =for apidoc_item sv_catpvn_mg
5524 =for apidoc_item sv_catpvn_nomg
5525
5526 These concatenate the C<len> bytes of the string beginning at C<ptr> onto the
5527 end of the string which is in C<dsv>.  The caller must make sure C<ptr>
5528 contains at least C<len> bytes.
5529
5530 For all but C<sv_catpvn_flags>, the string appended is assumed to be valid
5531 UTF-8 if the SV has the UTF-8 status set, and a string of bytes otherwise.
5532
5533 They differ in that:
5534
5535 C<sv_catpvn_mg> performs both 'get' and 'set' magic on C<dsv>.
5536
5537 C<sv_catpvn> performs only 'get' magic.
5538
5539 C<sv_catpvn_nomg> skips all magic.
5540
5541 C<sv_catpvn_flags> has an extra C<flags> parameter which allows you to specify
5542 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>) and
5543 to also override the UTF-8 handling.  By supplying the C<SV_CATBYTES> flag, the
5544 appended string is interpreted as plain bytes; by supplying instead the
5545 C<SV_CATUTF8> flag, it will be interpreted as UTF-8, and the C<dsv> will be
5546 upgraded to UTF-8 if necessary.
5547
5548 C<sv_catpvn>, C<sv_catpvn_mg>, and C<sv_catpvn_nomg> are implemented
5549 in terms of C<sv_catpvn_flags>.
5550
5551 =for apidoc Amnh||SV_CATUTF8
5552 =for apidoc Amnh||SV_CATBYTES
5553
5554 =cut
5555 */
5556
5557 void
5558 Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, const I32 flags)
5559 {
5560     STRLEN dlen;
5561     const char * const dstr = SvPV_force_flags(dsv, dlen, flags);
5562
5563     PERL_ARGS_ASSERT_SV_CATPVN_FLAGS;
5564     assert((flags & (SV_CATBYTES|SV_CATUTF8)) != (SV_CATBYTES|SV_CATUTF8));
5565
5566     if (!(flags & SV_CATBYTES) || !SvUTF8(dsv)) {
5567       if (flags & SV_CATUTF8 && !SvUTF8(dsv)) {
5568          sv_utf8_upgrade_flags_grow(dsv, 0, slen + 1);
5569          dlen = SvCUR(dsv);
5570       }
5571       else SvGROW(dsv, dlen + slen + 3);
5572       if (sstr == dstr)
5573         sstr = SvPVX_const(dsv);
5574       Move(sstr, SvPVX(dsv) + dlen, slen, char);
5575       SvCUR_set(dsv, SvCUR(dsv) + slen);
5576     }
5577     else {
5578         /* We inline bytes_to_utf8, to avoid an extra malloc. */
5579         const char * const send = sstr + slen;
5580         U8 *d;
5581
5582         /* Something this code does not account for, which I think is
5583            impossible; it would require the same pv to be treated as
5584            bytes *and* utf8, which would indicate a bug elsewhere. */
5585         assert(sstr != dstr);
5586
5587         SvGROW(dsv, dlen + slen * 2 + 3);
5588         d = (U8 *)SvPVX(dsv) + dlen;
5589
5590         while (sstr < send) {
5591             append_utf8_from_native_byte(*sstr, &d);
5592             sstr++;
5593         }
5594         SvCUR_set(dsv, d-(const U8 *)SvPVX(dsv));
5595     }
5596     *SvEND(dsv) = '\0';
5597     (void)SvPOK_only_UTF8(dsv);         /* validate pointer */
5598     SvTAINT(dsv);
5599     if (flags & SV_SMAGIC)
5600         SvSETMAGIC(dsv);
5601 }
5602
5603 /*
5604 =for apidoc sv_catsv
5605 =for apidoc_item sv_catsv_flags
5606 =for apidoc_item sv_catsv_mg
5607 =for apidoc_item sv_catsv_nomg
5608
5609 These concatenate the string from SV C<sstr> onto the end of the string in SV
5610 C<dsv>.  If C<sstr> is null, these are no-ops; otherwise only C<dsv> is
5611 modified.
5612
5613 They differ only in what magic they perform:
5614
5615 C<sv_catsv_mg> performs 'get' magic on both SVs before the copy, and 'set' magic
5616 on C<dsv> afterwards.
5617
5618 C<sv_catsv> performs just 'get' magic, on both SVs.
5619
5620 C<sv_catsv_nomg> skips all magic.
5621
5622 C<sv_catsv_flags> has an extra C<flags> parameter which allows you to use
5623 C<SV_GMAGIC> and/or C<SV_SMAGIC> to specify any combination of magic handling
5624 (although either both or neither SV will have 'get' magic applied to it.)
5625
5626 C<sv_catsv>, C<sv_catsv_mg>, and C<sv_catsv_nomg> are implemented
5627 in terms of C<sv_catsv_flags>.
5628
5629 =cut */
5630
5631 void
5632 Perl_sv_catsv_flags(pTHX_ SV *const dsv, SV *const sstr, const I32 flags)
5633 {
5634     PERL_ARGS_ASSERT_SV_CATSV_FLAGS;
5635
5636     if (sstr) {
5637         STRLEN slen;
5638         const char *spv = SvPV_flags_const(sstr, slen, flags);
5639         if (flags & SV_GMAGIC)
5640                 SvGETMAGIC(dsv);
5641         sv_catpvn_flags(dsv, spv, slen,
5642                             DO_UTF8(sstr) ? SV_CATUTF8 : SV_CATBYTES);
5643         if (flags & SV_SMAGIC)
5644                 SvSETMAGIC(dsv);
5645     }
5646 }
5647
5648 /*
5649 =for apidoc sv_catpv
5650 =for apidoc_item sv_catpv_flags
5651 =for apidoc_item sv_catpv_mg
5652 =for apidoc_item sv_catpv_nomg
5653
5654 These concatenate the C<NUL>-terminated string C<sstr> onto the end of the
5655 string which is in the SV.
5656 If the SV has the UTF-8 status set, then the bytes appended should be
5657 valid UTF-8.
5658
5659 They differ only in how they handle magic:
5660
5661 C<sv_catpv_mg> performs both 'get' and 'set' magic.
5662
5663 C<sv_catpv> performs only 'get' magic.
5664
5665 C<sv_catpv_nomg> skips all magic.
5666
5667 C<sv_catpv_flags> has an extra C<flags> parameter which allows you to specify
5668 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>), and
5669 to also override the UTF-8 handling.  By supplying the C<SV_CATUTF8> flag, the
5670 appended string is forced to be interpreted as UTF-8; by supplying instead the
5671 C<SV_CATBYTES> flag, it will be interpreted as just bytes.  Either the SV or
5672 the string appended will be upgraded to UTF-8 if necessary.
5673
5674 =cut
5675 */
5676
5677 void
5678 Perl_sv_catpv(pTHX_ SV *const dsv, const char *sstr)
5679 {
5680     STRLEN len;
5681     STRLEN tlen;
5682     char *junk;
5683
5684     PERL_ARGS_ASSERT_SV_CATPV;
5685
5686     if (!sstr)
5687         return;
5688     junk = SvPV_force(dsv, tlen);
5689     len = strlen(sstr);
5690     SvGROW(dsv, tlen + len + 1);
5691     if (sstr == junk)
5692         sstr = SvPVX_const(dsv);
5693     Move(sstr,SvPVX(dsv)+tlen,len+1,char);
5694     SvCUR_set(dsv, SvCUR(dsv) + len);
5695     (void)SvPOK_only_UTF8(dsv);         /* validate pointer */
5696     SvTAINT(dsv);
5697 }
5698
5699 void
5700 Perl_sv_catpv_flags(pTHX_ SV *dsv, const char *sstr, const I32 flags)
5701 {
5702     PERL_ARGS_ASSERT_SV_CATPV_FLAGS;
5703     sv_catpvn_flags(dsv, sstr, strlen(sstr), flags);
5704 }
5705
5706 void
5707 Perl_sv_catpv_mg(pTHX_ SV *const dsv, const char *const sstr)
5708 {
5709     PERL_ARGS_ASSERT_SV_CATPV_MG;
5710
5711     sv_catpv(dsv,sstr);
5712     SvSETMAGIC(dsv);
5713 }
5714
5715 /*
5716 =for apidoc newSV
5717
5718 Creates a new SV.  A non-zero C<len> parameter indicates the number of
5719 bytes of preallocated string space the SV should have.  An extra byte for a
5720 trailing C<NUL> is also reserved.  (C<SvPOK> is not set for the SV even if string
5721 space is allocated.)  The reference count for the new SV is set to 1.
5722
5723 In 5.9.3, C<newSV()> replaces the older C<NEWSV()> API, and drops the first
5724 parameter, I<x>, a debug aid which allowed callers to identify themselves.
5725 This aid has been superseded by a new build option, C<PERL_MEM_LOG> (see
5726 L<perlhacktips/PERL_MEM_LOG>).  The older API is still there for use in XS
5727 modules supporting older perls.
5728
5729 =cut
5730 */
5731
5732 SV *
5733 Perl_newSV(pTHX_ const STRLEN len)
5734 {
5735     SV *sv;
5736
5737     if (!len)
5738         new_SV(sv);
5739     else {
5740         sv = newSV_type(SVt_PV);
5741         sv_grow_fresh(sv, len + 1);
5742     }
5743     return sv;
5744 }
5745 /*
5746 =for apidoc sv_magicext
5747
5748 Adds magic to an SV, upgrading it if necessary.  Applies the
5749 supplied C<vtable> and returns a pointer to the magic added.
5750
5751 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5752 In particular, you can add magic to C<SvREADONLY> SVs, and add more than
5753 one instance of the same C<how>.
5754
5755 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5756 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5757 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5758 to contain an SV* and is stored as-is with its C<REFCNT> incremented.
5759
5760 (This is now used as a subroutine by C<sv_magic>.)
5761
5762 =cut
5763 */
5764 MAGIC *
5765 Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how,
5766                 const MGVTBL *const vtable, const char *const name, const I32 namlen)
5767 {
5768     MAGIC* mg;
5769
5770     PERL_ARGS_ASSERT_SV_MAGICEXT;
5771
5772     SvUPGRADE(sv, SVt_PVMG);
5773     Newxz(mg, 1, MAGIC);
5774     mg->mg_moremagic = SvMAGIC(sv);
5775     SvMAGIC_set(sv, mg);
5776
5777     /* Sometimes a magic contains a reference loop, where the sv and
5778        object refer to each other.  To prevent a reference loop that
5779        would prevent such objects being freed, we look for such loops
5780        and if we find one we avoid incrementing the object refcount.
5781
5782        Note we cannot do this to avoid self-tie loops as intervening RV must
5783        have its REFCNT incremented to keep it in existence.
5784
5785     */
5786     if (!obj || obj == sv ||
5787         how == PERL_MAGIC_arylen ||
5788         how == PERL_MAGIC_regdata ||
5789         how == PERL_MAGIC_regdatum ||
5790         how == PERL_MAGIC_symtab ||
5791         (SvTYPE(obj) == SVt_PVGV &&
5792             (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv
5793              || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv
5794              || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv)))
5795     {
5796         mg->mg_obj = obj;
5797     }
5798     else {
5799         mg->mg_obj = SvREFCNT_inc_simple(obj);
5800         mg->mg_flags |= MGf_REFCOUNTED;
5801     }
5802
5803     /* Normal self-ties simply pass a null object, and instead of
5804        using mg_obj directly, use the SvTIED_obj macro to produce a
5805        new RV as needed.  For glob "self-ties", we are tieing the PVIO
5806        with an RV obj pointing to the glob containing the PVIO.  In
5807        this case, to avoid a reference loop, we need to weaken the
5808        reference.
5809     */
5810
5811     if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5812         obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv)
5813     {
5814       sv_rvweaken(obj);
5815     }
5816
5817     mg->mg_type = how;
5818     mg->mg_len = namlen;
5819     if (name) {
5820         if (namlen > 0)
5821             mg->mg_ptr = savepvn(name, namlen);
5822         else if (namlen == HEf_SVKEY) {
5823             /* Yes, this is casting away const. This is only for the case of
5824                HEf_SVKEY. I think we need to document this aberration of the
5825                constness of the API, rather than making name non-const, as
5826                that change propagating outwards a long way.  */
5827             mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name);
5828         } else
5829             mg->mg_ptr = (char *) name;
5830     }
5831     mg->mg_virtual = (MGVTBL *) vtable;
5832
5833     mg_magical(sv);
5834     return mg;
5835 }
5836
5837 MAGIC *
5838 Perl_sv_magicext_mglob(pTHX_ SV *sv)
5839 {
5840     PERL_ARGS_ASSERT_SV_MAGICEXT_MGLOB;
5841     if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
5842         /* This sv is only a delegate.  //g magic must be attached to
5843            its target. */
5844         vivify_defelem(sv);
5845         sv = LvTARG(sv);
5846     }
5847     return sv_magicext(sv, NULL, PERL_MAGIC_regex_global,
5848                        &PL_vtbl_mglob, 0, 0);
5849 }
5850
5851 /*
5852 =for apidoc sv_magic
5853
5854 Adds magic to an SV.  First upgrades C<sv> to type C<SVt_PVMG> if
5855 necessary, then adds a new magic item of type C<how> to the head of the
5856 magic list.
5857
5858 See C<L</sv_magicext>> (which C<sv_magic> now calls) for a description of the
5859 handling of the C<name> and C<namlen> arguments.
5860
5861 You need to use C<sv_magicext> to add magic to C<SvREADONLY> SVs and also
5862 to add more than one instance of the same C<how>.
5863
5864 =cut
5865 */
5866
5867 void
5868 Perl_sv_magic(pTHX_ SV *const sv, SV *const obj, const int how,
5869              const char *const name, const I32 namlen)
5870 {
5871     const MGVTBL *vtable;
5872     MAGIC* mg;
5873     unsigned int flags;
5874     unsigned int vtable_index;
5875
5876     PERL_ARGS_ASSERT_SV_MAGIC;
5877
5878     if (how < 0 || (unsigned)how >= C_ARRAY_LENGTH(PL_magic_data)
5879         || ((flags = PL_magic_data[how]),
5880             (vtable_index = flags & PERL_MAGIC_VTABLE_MASK)
5881             > magic_vtable_max))
5882         Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5883
5884     /* PERL_MAGIC_ext is reserved for use by extensions not perl internals.
5885        Useful for attaching extension internal data to perl vars.
5886        Note that multiple extensions may clash if magical scalars
5887        etc holding private data from one are passed to another. */
5888
5889     vtable = (vtable_index == magic_vtable_max)
5890         ? NULL : PL_magic_vtables + vtable_index;
5891
5892     if (SvREADONLY(sv)) {
5893         if (
5894             !PERL_MAGIC_TYPE_READONLY_ACCEPTABLE(how)
5895            )
5896         {
5897             Perl_croak_no_modify();
5898         }
5899     }
5900     if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5901         if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5902             /* sv_magic() refuses to add a magic of the same 'how' as an
5903                existing one
5904              */
5905             if (how == PERL_MAGIC_taint)
5906                 mg->mg_len |= 1;
5907             return;
5908         }
5909     }
5910
5911     /* Rest of work is done else where */
5912     mg = sv_magicext(sv,obj,how,vtable,name,namlen);
5913
5914     switch (how) {
5915     case PERL_MAGIC_taint:
5916         mg->mg_len = 1;
5917         break;
5918     case PERL_MAGIC_ext:
5919     case PERL_MAGIC_dbfile:
5920         SvRMAGICAL_on(sv);
5921         break;
5922     }
5923 }
5924
5925 static int
5926 S_sv_unmagicext_flags(pTHX_ SV *const sv, const int type, const MGVTBL *vtbl, const U32 flags)
5927 {
5928     MAGIC* mg;
5929     MAGIC** mgp;
5930
5931     assert(flags <= 1);
5932
5933     if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5934         return 0;
5935     mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic);
5936     for (mg = *mgp; mg; mg = *mgp) {
5937         const MGVTBL* const virt = mg->mg_virtual;
5938         if (mg->mg_type == type && (!flags || virt == vtbl)) {
5939             *mgp = mg->mg_moremagic;
5940             if (virt && virt->svt_free)
5941                 virt->svt_free(aTHX_ sv, mg);
5942             if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5943                 if (mg->mg_len > 0)
5944                     Safefree(mg->mg_ptr);
5945                 else if (mg->mg_len == HEf_SVKEY)
5946                     SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
5947                 else if (mg->mg_type == PERL_MAGIC_utf8)
5948                     Safefree(mg->mg_ptr);
5949             }
5950             if (mg->mg_flags & MGf_REFCOUNTED)
5951                 SvREFCNT_dec(mg->mg_obj);
5952             Safefree(mg);
5953         }
5954         else
5955             mgp = &mg->mg_moremagic;
5956     }
5957     if (SvMAGIC(sv)) {
5958         if (SvMAGICAL(sv))      /* if we're under save_magic, wait for restore_magic; */
5959             mg_magical(sv);     /*    else fix the flags now */
5960     }
5961     else
5962         SvMAGICAL_off(sv);
5963
5964     return 0;
5965 }
5966
5967 /*
5968 =for apidoc sv_unmagic
5969
5970 Removes all magic of type C<type> from an SV.
5971
5972 =cut
5973 */
5974
5975 int
5976 Perl_sv_unmagic(pTHX_ SV *const sv, const int type)
5977 {
5978     PERL_ARGS_ASSERT_SV_UNMAGIC;
5979     return S_sv_unmagicext_flags(aTHX_ sv, type, NULL, 0);
5980 }
5981
5982 /*
5983 =for apidoc sv_unmagicext
5984
5985 Removes all magic of type C<type> with the specified C<vtbl> from an SV.
5986
5987 =cut
5988 */
5989
5990 int
5991 Perl_sv_unmagicext(pTHX_ SV *const sv, const int type, const MGVTBL *vtbl)
5992 {
5993     PERL_ARGS_ASSERT_SV_UNMAGICEXT;
5994     return S_sv_unmagicext_flags(aTHX_ sv, type, vtbl, 1);
5995 }
5996
5997 /*
5998 =for apidoc sv_rvweaken
5999
6000 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
6001 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
6002 push a back-reference to this RV onto the array of backreferences
6003 associated with that magic.  If the RV is magical, set magic will be
6004 called after the RV is cleared.  Silently ignores C<undef> and warns
6005 on already-weak references.
6006
6007 =cut
6008 */
6009
6010 SV *
6011 Perl_sv_rvweaken(pTHX_ SV *const sv)
6012 {
6013     SV *tsv;
6014
6015     PERL_ARGS_ASSERT_SV_RVWEAKEN;
6016
6017     if (!SvOK(sv))  /* let undefs pass */
6018         return sv;
6019     if (!SvROK(sv))
6020         Perl_croak(aTHX_ "Can't weaken a nonreference");
6021     else if (SvWEAKREF(sv)) {
6022         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
6023         return sv;
6024     }
6025     else if (SvREADONLY(sv)) croak_no_modify();
6026     tsv = SvRV(sv);
6027     Perl_sv_add_backref(aTHX_ tsv, sv);
6028     SvWEAKREF_on(sv);
6029     SvREFCNT_dec_NN(tsv);
6030     return sv;
6031 }
6032
6033 /*
6034 =for apidoc sv_rvunweaken
6035
6036 Unweaken a reference: Clear the C<SvWEAKREF> flag on this RV; remove
6037 the backreference to this RV from the array of backreferences
6038 associated with the target SV, increment the refcount of the target.
6039 Silently ignores C<undef> and warns on non-weak references.
6040
6041 =cut
6042 */
6043
6044 SV *
6045 Perl_sv_rvunweaken(pTHX_ SV *const sv)
6046 {
6047     SV *tsv;
6048
6049     PERL_ARGS_ASSERT_SV_RVUNWEAKEN;
6050
6051     if (!SvOK(sv)) /* let undefs pass */
6052         return sv;
6053     if (!SvROK(sv))
6054         Perl_croak(aTHX_ "Can't unweaken a nonreference");
6055     else if (!SvWEAKREF(sv)) {
6056         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is not weak");
6057         return sv;
6058     }
6059     else if (SvREADONLY(sv)) croak_no_modify();
6060
6061     tsv = SvRV(sv);
6062     SvWEAKREF_off(sv);
6063     SvROK_on(sv);
6064     SvREFCNT_inc_NN(tsv);
6065     Perl_sv_del_backref(aTHX_ tsv, sv);
6066     return sv;
6067 }
6068
6069 /*
6070 =for apidoc sv_get_backrefs
6071
6072 If C<sv> is the target of a weak reference then it returns the back
6073 references structure associated with the sv; otherwise return C<NULL>.
6074
6075 When returning a non-null result the type of the return is relevant. If it
6076 is an AV then the elements of the AV are the weak reference RVs which
6077 point at this item. If it is any other type then the item itself is the
6078 weak reference.
6079
6080 See also C<Perl_sv_add_backref()>, C<Perl_sv_del_backref()>,
6081 C<Perl_sv_kill_backrefs()>
6082
6083 =cut
6084 */
6085
6086 SV *
6087 Perl_sv_get_backrefs(SV *const sv)
6088 {
6089     SV *backrefs= NULL;
6090
6091     PERL_ARGS_ASSERT_SV_GET_BACKREFS;
6092
6093     /* find slot to store array or singleton backref */
6094
6095     if (SvTYPE(sv) == SVt_PVHV) {
6096         if (HvHasAUX(sv)) {
6097             struct xpvhv_aux * const iter = HvAUX((HV *)sv);
6098             backrefs = (SV *)iter->xhv_backreferences;
6099         }
6100     } else if (SvMAGICAL(sv)) {
6101         MAGIC *mg = mg_find(sv, PERL_MAGIC_backref);
6102         if (mg)
6103             backrefs = mg->mg_obj;
6104     }
6105     return backrefs;
6106 }
6107
6108 /* Give tsv backref magic if it hasn't already got it, then push a
6109  * back-reference to sv onto the array associated with the backref magic.
6110  *
6111  * As an optimisation, if there's only one backref and it's not an AV,
6112  * store it directly in the HvAUX or mg_obj slot, avoiding the need to
6113  * allocate an AV. (Whether the slot holds an AV tells us whether this is
6114  * active.)
6115  */
6116
6117 /* A discussion about the backreferences array and its refcount:
6118  *
6119  * The AV holding the backreferences is pointed to either as the mg_obj of
6120  * PERL_MAGIC_backref, or in the specific case of a HV, from the
6121  * xhv_backreferences field. The array is created with a refcount
6122  * of 2. This means that if during global destruction the array gets
6123  * picked on before its parent to have its refcount decremented by the
6124  * random zapper, it won't actually be freed, meaning it's still there for
6125  * when its parent gets freed.
6126  *
6127  * When the parent SV is freed, the extra ref is killed by
6128  * Perl_sv_kill_backrefs.  The other ref is killed, in the case of magic,
6129  * by mg_free() / MGf_REFCOUNTED, or for a hash, by Perl_hv_kill_backrefs.
6130  *
6131  * When a single backref SV is stored directly, it is not reference
6132  * counted.
6133  */
6134
6135 void
6136 Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv)
6137 {
6138     SV **svp;
6139     AV *av = NULL;
6140     MAGIC *mg = NULL;
6141
6142     PERL_ARGS_ASSERT_SV_ADD_BACKREF;
6143
6144     /* find slot to store array or singleton backref */
6145
6146     if (SvTYPE(tsv) == SVt_PVHV) {
6147         svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6148     } else {
6149         if (SvMAGICAL(tsv))
6150             mg = mg_find(tsv, PERL_MAGIC_backref);
6151         if (!mg)
6152             mg = sv_magicext(tsv, NULL, PERL_MAGIC_backref, &PL_vtbl_backref, NULL, 0);
6153         svp = &(mg->mg_obj);
6154     }
6155
6156     /* create or retrieve the array */
6157
6158     if (   (!*svp && SvTYPE(sv) == SVt_PVAV)
6159         || (*svp && SvTYPE(*svp) != SVt_PVAV)
6160     ) {
6161         /* create array */
6162         if (mg)
6163             mg->mg_flags |= MGf_REFCOUNTED;
6164         av = newAV();
6165         AvREAL_off(av);
6166         SvREFCNT_inc_simple_void_NN(av);
6167         /* av now has a refcnt of 2; see discussion above */
6168         av_extend(av, *svp ? 2 : 1);
6169         if (*svp) {
6170             /* move single existing backref to the array */
6171             AvARRAY(av)[++AvFILLp(av)] = *svp; /* av_push() */
6172         }
6173         *svp = (SV*)av;
6174     }
6175     else {
6176         av = MUTABLE_AV(*svp);
6177         if (!av) {
6178             /* optimisation: store single backref directly in HvAUX or mg_obj */
6179             *svp = sv;
6180             return;
6181         }
6182         assert(SvTYPE(av) == SVt_PVAV);
6183         if (AvFILLp(av) >= AvMAX(av)) {
6184             av_extend(av, AvFILLp(av)+1);
6185         }
6186     }
6187     /* push new backref */
6188     AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
6189 }
6190
6191 /* delete a back-reference to ourselves from the backref magic associated
6192  * with the SV we point to.
6193  */
6194
6195 void
6196 Perl_sv_del_backref(pTHX_ SV *const tsv, SV *const sv)
6197 {
6198     SV **svp = NULL;
6199
6200     PERL_ARGS_ASSERT_SV_DEL_BACKREF;
6201
6202     if (SvTYPE(tsv) == SVt_PVHV) {
6203         if (HvHasAUX(tsv))
6204             svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6205     }
6206     else if (SvIS_FREED(tsv) && PL_phase == PERL_PHASE_DESTRUCT) {
6207         /* It's possible for the last (strong) reference to tsv to have
6208            become freed *before* the last thing holding a weak reference.
6209            If both survive longer than the backreferences array, then when
6210            the referent's reference count drops to 0 and it is freed, it's
6211            not able to chase the backreferences, so they aren't NULLed.
6212
6213            For example, a CV holds a weak reference to its stash. If both the
6214            CV and the stash survive longer than the backreferences array,
6215            and the CV gets picked for the SvBREAK() treatment first,
6216            *and* it turns out that the stash is only being kept alive because
6217            of an our variable in the pad of the CV, then midway during CV
6218            destruction the stash gets freed, but CvSTASH() isn't set to NULL.
6219            It ends up pointing to the freed HV. Hence it's chased in here, and
6220            if this block wasn't here, it would hit the !svp panic just below.
6221
6222            I don't believe that "better" destruction ordering is going to help
6223            here - during global destruction there's always going to be the
6224            chance that something goes out of order. We've tried to make it
6225            foolproof before, and it only resulted in evolutionary pressure on
6226            fools. Which made us look foolish for our hubris. :-(
6227         */
6228         return;
6229     }
6230     else {
6231         MAGIC *const mg
6232             = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
6233         svp =  mg ? &(mg->mg_obj) : NULL;
6234     }
6235
6236     if (!svp)
6237         Perl_croak(aTHX_ "panic: del_backref, svp=0");
6238     if (!*svp) {
6239         /* It's possible that sv is being freed recursively part way through the
6240            freeing of tsv. If this happens, the backreferences array of tsv has
6241            already been freed, and so svp will be NULL. If this is the case,
6242            we should not panic. Instead, nothing needs doing, so return.  */
6243         if (PL_phase == PERL_PHASE_DESTRUCT && SvREFCNT(tsv) == 0)
6244             return;
6245         Perl_croak(aTHX_ "panic: del_backref, *svp=%p phase=%s refcnt=%" UVuf,
6246                    (void*)*svp, PL_phase_names[PL_phase], (UV)SvREFCNT(tsv));
6247     }
6248
6249     if (SvTYPE(*svp) == SVt_PVAV) {
6250 #ifdef DEBUGGING
6251         int count = 1;
6252 #endif
6253         AV * const av = (AV*)*svp;
6254         SSize_t fill;
6255         assert(!SvIS_FREED(av));
6256         fill = AvFILLp(av);
6257         assert(fill > -1);
6258         svp = AvARRAY(av);
6259         /* for an SV with N weak references to it, if all those
6260          * weak refs are deleted, then sv_del_backref will be called
6261          * N times and O(N^2) compares will be done within the backref
6262          * array. To ameliorate this potential slowness, we:
6263          * 1) make sure this code is as tight as possible;
6264          * 2) when looking for SV, look for it at both the head and tail of the
6265          *    array first before searching the rest, since some create/destroy
6266          *    patterns will cause the backrefs to be freed in order.
6267          */
6268         if (*svp == sv) {
6269             AvARRAY(av)++;
6270             AvMAX(av)--;
6271         }
6272         else {
6273             SV **p = &svp[fill];
6274             SV *const topsv = *p;
6275             if (topsv != sv) {
6276 #ifdef DEBUGGING
6277                 count = 0;
6278 #endif
6279                 while (--p > svp) {
6280                     if (*p == sv) {
6281                         /* We weren't the last entry.
6282                            An unordered list has this property that you
6283                            can take the last element off the end to fill
6284                            the hole, and it's still an unordered list :-)
6285                         */
6286                         *p = topsv;
6287 #ifdef DEBUGGING
6288                         count++;
6289 #else
6290                         break; /* should only be one */
6291 #endif
6292                     }
6293                 }
6294             }
6295         }
6296         assert(count ==1);
6297         AvFILLp(av) = fill-1;
6298     }
6299     else if (SvIS_FREED(*svp) && PL_phase == PERL_PHASE_DESTRUCT) {
6300         /* freed AV; skip */
6301     }
6302     else {
6303         /* optimisation: only a single backref, stored directly */
6304         if (*svp != sv)
6305             Perl_croak(aTHX_ "panic: del_backref, *svp=%p, sv=%p",
6306                        (void*)*svp, (void*)sv);
6307         *svp = NULL;
6308     }
6309
6310 }
6311
6312 void
6313 Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av)
6314 {
6315     SV **svp;
6316     SV **last;
6317     bool is_array;
6318
6319     PERL_ARGS_ASSERT_SV_KILL_BACKREFS;
6320
6321     if (!av)
6322         return;
6323
6324     /* after multiple passes through Perl_sv_clean_all() for a thingy
6325      * that has badly leaked, the backref array may have gotten freed,
6326      * since we only protect it against 1 round of cleanup */
6327     if (SvIS_FREED(av)) {
6328         if (PL_in_clean_all) /* All is fair */
6329             return;
6330         Perl_croak(aTHX_
6331                    "panic: magic_killbackrefs (freed backref AV/SV)");
6332     }
6333
6334
6335     is_array = (SvTYPE(av) == SVt_PVAV);
6336     if (is_array) {
6337         assert(!SvIS_FREED(av));
6338         svp = AvARRAY(av);
6339         if (svp)
6340             last = svp + AvFILLp(av);
6341     }
6342     else {
6343         /* optimisation: only a single backref, stored directly */
6344         svp = (SV**)&av;
6345         last = svp;
6346     }
6347
6348     if (svp) {
6349         while (svp <= last) {
6350             if (*svp) {
6351                 SV *const referrer = *svp;
6352                 if (SvWEAKREF(referrer)) {
6353                     /* XXX Should we check that it hasn't changed? */
6354                     assert(SvROK(referrer));
6355                     SvRV_set(referrer, 0);
6356                     SvOK_off(referrer);
6357                     SvWEAKREF_off(referrer);
6358                     SvSETMAGIC(referrer);
6359                 } else if (SvTYPE(referrer) == SVt_PVGV ||
6360                            SvTYPE(referrer) == SVt_PVLV) {
6361                     assert(SvTYPE(sv) == SVt_PVHV); /* stash backref */
6362                     /* You lookin' at me?  */
6363                     assert(GvSTASH(referrer));
6364                     assert(GvSTASH(referrer) == (const HV *)sv);
6365                     GvSTASH(referrer) = 0;
6366                 } else if (SvTYPE(referrer) == SVt_PVCV ||
6367                            SvTYPE(referrer) == SVt_PVFM) {
6368                     if (SvTYPE(sv) == SVt_PVHV) { /* stash backref */
6369                         /* You lookin' at me?  */
6370                         assert(CvSTASH(referrer));
6371                         assert(CvSTASH(referrer) == (const HV *)sv);
6372                         SvANY(MUTABLE_CV(referrer))->xcv_stash = 0;
6373                     }
6374                     else {
6375                         assert(SvTYPE(sv) == SVt_PVGV);
6376                         /* You lookin' at me?  */
6377                         assert(CvGV(referrer));
6378                         assert(CvGV(referrer) == (const GV *)sv);
6379                         anonymise_cv_maybe(MUTABLE_GV(sv),
6380                                                 MUTABLE_CV(referrer));
6381                     }
6382
6383                 } else {
6384                     Perl_croak(aTHX_
6385                                "panic: magic_killbackrefs (flags=%" UVxf ")",
6386                                (UV)SvFLAGS(referrer));
6387                 }
6388
6389                 if (is_array)
6390                     *svp = NULL;
6391             }
6392             svp++;
6393         }
6394     }
6395     if (is_array) {
6396         AvFILLp(av) = -1;
6397         SvREFCNT_dec_NN(av); /* remove extra count added by sv_add_backref() */
6398     }
6399     return;
6400 }
6401
6402 /*
6403 =for apidoc sv_insert
6404
6405 Inserts and/or replaces a string at the specified offset/length within the SV.
6406 Similar to the Perl C<substr()> function, with C<littlelen> bytes starting at
6407 C<little> replacing C<len> bytes of the string in C<bigstr> starting at
6408 C<offset>.  Handles get magic.
6409
6410 =for apidoc sv_insert_flags
6411
6412 Same as C<sv_insert>, but the extra C<flags> are passed to the
6413 C<SvPV_force_flags> that applies to C<bigstr>.
6414
6415 =cut
6416 */
6417
6418 void
6419 Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *little, const STRLEN littlelen, const U32 flags)
6420 {
6421     char *big;
6422     char *mid;
6423     char *midend;
6424     char *bigend;
6425     SSize_t i;          /* better be sizeof(STRLEN) or bad things happen */
6426     STRLEN curlen;
6427
6428     PERL_ARGS_ASSERT_SV_INSERT_FLAGS;
6429
6430     SvPV_force_flags(bigstr, curlen, flags);
6431     (void)SvPOK_only_UTF8(bigstr);
6432
6433     if (little >= SvPVX(bigstr) &&
6434         little < SvPVX(bigstr) + (SvLEN(bigstr) ? SvLEN(bigstr) : SvCUR(bigstr))) {
6435         /* little is a pointer to within bigstr, since we can reallocate bigstr,
6436            or little...little+littlelen might overlap offset...offset+len we make a copy
6437         */
6438         little = savepvn(little, littlelen);
6439         SAVEFREEPV(little);
6440     }
6441
6442     if (offset + len > curlen) {
6443         SvGROW(bigstr, offset+len+1);
6444         Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6445         SvCUR_set(bigstr, offset+len);
6446     }
6447
6448     SvTAINT(bigstr);
6449     i = littlelen - len;
6450     if (i > 0) {                        /* string might grow */
6451         big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
6452         mid = big + offset + len;
6453         midend = bigend = big + SvCUR(bigstr);
6454         bigend += i;
6455         *bigend = '\0';
6456         while (midend > mid)            /* shove everything down */
6457             *--bigend = *--midend;
6458         Move(little,big+offset,littlelen,char);
6459         SvCUR_set(bigstr, SvCUR(bigstr) + i);
6460         SvSETMAGIC(bigstr);
6461         return;
6462     }
6463     else if (i == 0) {
6464         Move(little,SvPVX(bigstr)+offset,len,char);
6465         SvSETMAGIC(bigstr);
6466         return;
6467     }
6468
6469     big = SvPVX(bigstr);
6470     mid = big + offset;
6471     midend = mid + len;
6472     bigend = big + SvCUR(bigstr);
6473
6474     if (midend > bigend)
6475         Perl_croak(aTHX_ "panic: sv_insert, midend=%p, bigend=%p",
6476                    midend, bigend);
6477
6478     if (mid - big > bigend - midend) {  /* faster to shorten from end */
6479         if (littlelen) {
6480             Move(little, mid, littlelen,char);
6481             mid += littlelen;
6482         }
6483         i = bigend - midend;
6484         if (i > 0) {
6485             Move(midend, mid, i,char);
6486             mid += i;
6487         }
6488         *mid = '\0';
6489         SvCUR_set(bigstr, mid - big);
6490     }
6491     else if ((i = mid - big)) { /* faster from front */
6492         midend -= littlelen;
6493         mid = midend;
6494         Move(big, midend - i, i, char);
6495         sv_chop(bigstr,midend-i);
6496         if (littlelen)
6497             Move(little, mid, littlelen,char);
6498     }
6499     else if (littlelen) {
6500         midend -= littlelen;
6501         sv_chop(bigstr,midend);
6502         Move(little,midend,littlelen,char);
6503     }
6504     else {
6505         sv_chop(bigstr,midend);
6506     }
6507     SvSETMAGIC(bigstr);
6508 }
6509
6510 /*
6511 =for apidoc sv_replace
6512
6513 Make the first argument a copy of the second, then delete the original.
6514 The target SV physically takes over ownership of the body of the source SV
6515 and inherits its flags; however, the target keeps any magic it owns,
6516 and any magic in the source is discarded.
6517 Note that this is a rather specialist SV copying operation; most of the
6518 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
6519
6520 =cut
6521 */
6522
6523 void
6524 Perl_sv_replace(pTHX_ SV *const sv, SV *const nsv)
6525 {
6526     const U32 refcnt = SvREFCNT(sv);
6527
6528     PERL_ARGS_ASSERT_SV_REPLACE;
6529
6530     SV_CHECK_THINKFIRST_COW_DROP(sv);
6531     if (SvREFCNT(nsv) != 1) {
6532         Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()"
6533                    " (%" UVuf " != 1)", (UV) SvREFCNT(nsv));
6534     }
6535     if (SvMAGICAL(sv)) {
6536         if (SvMAGICAL(nsv))
6537             mg_free(nsv);
6538         else
6539             sv_upgrade(nsv, SVt_PVMG);
6540         SvMAGIC_set(nsv, SvMAGIC(sv));
6541         SvFLAGS(nsv) |= SvMAGICAL(sv);
6542         SvMAGICAL_off(sv);
6543         SvMAGIC_set(sv, NULL);
6544     }
6545     SvREFCNT(sv) = 0;
6546     sv_clear(sv);
6547     assert(!SvREFCNT(sv));
6548 #ifdef DEBUG_LEAKING_SCALARS
6549     sv->sv_flags  = nsv->sv_flags;
6550     sv->sv_any    = nsv->sv_any;
6551     sv->sv_refcnt = nsv->sv_refcnt;
6552     sv->sv_u      = nsv->sv_u;
6553 #else
6554     StructCopy(nsv,sv,SV);
6555 #endif
6556     if(SvTYPE(sv) == SVt_IV) {
6557         SET_SVANY_FOR_BODYLESS_IV(sv);
6558     }
6559
6560
6561     SvREFCNT(sv) = refcnt;
6562     SvFLAGS(nsv) |= SVTYPEMASK;         /* Mark as freed */
6563     SvREFCNT(nsv) = 0;
6564     del_SV(nsv);
6565 }
6566
6567 /* We're about to free a GV which has a CV that refers back to us.
6568  * If that CV will outlive us, make it anonymous (i.e. fix up its CvGV
6569  * field) */
6570
6571 STATIC void
6572 S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv)
6573 {
6574     SV *gvname;
6575     GV *anongv;
6576
6577     PERL_ARGS_ASSERT_ANONYMISE_CV_MAYBE;
6578
6579     /* be assertive! */
6580     assert(SvREFCNT(gv) == 0);
6581     assert(isGV(gv) && isGV_with_GP(gv));
6582     assert(GvGP(gv));
6583     assert(!CvANON(cv));
6584     assert(CvGV(cv) == gv);
6585     assert(!CvNAMED(cv));
6586
6587     /* will the CV shortly be freed by gp_free() ? */
6588     if (GvCV(gv) == cv && GvGP(gv)->gp_refcnt < 2 && SvREFCNT(cv) < 2) {
6589         SvANY(cv)->xcv_gv_u.xcv_gv = NULL;
6590         return;
6591     }
6592
6593     /* if not, anonymise: */
6594     gvname = (GvSTASH(gv) && HvHasNAME(GvSTASH(gv)) && HvHasENAME(GvSTASH(gv)))
6595                     ? newSVhek(HvENAME_HEK(GvSTASH(gv)))
6596                     : newSVpvn_flags( "__ANON__", 8, 0 );
6597     sv_catpvs(gvname, "::__ANON__");
6598     anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV);
6599     SvREFCNT_dec_NN(gvname);
6600
6601     CvANON_on(cv);
6602     CvCVGV_RC_on(cv);
6603     SvANY(cv)->xcv_gv_u.xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv));
6604 }
6605
6606
6607 /*
6608 =for apidoc sv_clear
6609
6610 Clear an SV: call any destructors, free up any memory used by the body,
6611 and free the body itself.  The SV's head is I<not> freed, although
6612 its type is set to all 1's so that it won't inadvertently be assumed
6613 to be live during global destruction etc.
6614 This function should only be called when C<REFCNT> is zero.  Most of the time
6615 you'll want to call C<SvREFCNT_dec> instead.
6616
6617 =cut
6618 */
6619
6620 void
6621 Perl_sv_clear(pTHX_ SV *const orig_sv)
6622 {
6623     SV* iter_sv = NULL;
6624     SV* next_sv = NULL;
6625     SV *sv = orig_sv;
6626     STRLEN hash_index = 0; /* initialise to make Coverity et al happy.
6627                               Not strictly necessary */
6628
6629     PERL_ARGS_ASSERT_SV_CLEAR;
6630
6631     /* within this loop, sv is the SV currently being freed, and
6632      * iter_sv is the most recent AV or whatever that's being iterated
6633      * over to provide more SVs */
6634
6635     while (sv) {
6636         U32 type = SvTYPE(sv);
6637         HV *stash;
6638
6639         assert(SvREFCNT(sv) == 0);
6640         assert(!SvIS_FREED(sv));
6641 #if NVSIZE <= IVSIZE
6642         if (type <= SVt_NV) {
6643 #else
6644         if (type <= SVt_IV) {
6645 #endif
6646             /* Historically this check on type was needed so that the code to
6647              * free bodies wasn't reached for these types, because the arena
6648              * slots were re-used for HEs and pointer table entries. The
6649              * metadata table `bodies_by_type` had the information for the sizes
6650              * for HEs and PTEs, hence the code here had to have a special-case
6651              * check to ensure that the "regular" body freeing code wasn't
6652              * reached, and get confused by the "lies" in `bodies_by_type`.
6653              *
6654              * However, it hasn't actually been needed for that reason since
6655              * Aug 2010 (commit 829cd18aa7f45221), because `bodies_by_type` was
6656              * changed to always hold the accurate metadata for the SV types.
6657              * This was possible because PTEs were no longer allocated from the
6658              * "SVt_IV" arena, and the code to allocate HEs from the "SVt_NULL"
6659              * arena is entirely in hv.c, so doesn't access the table.
6660              *
6661              * Some sort of check is still needed to handle SVt_IVs - pure RVs
6662              * need to take one code path which is common with RVs stored in
6663              * SVt_PV (or larger), but pure IVs mustn't take the "PV but not RV"
6664              * path, as SvPVX() doesn't point to valid memory.
6665              *
6666              * Hence this code is still the most efficient way to handle this.
6667              *
6668              * Additionally, for bodyless NVs, riding this branch is more
6669              * efficient than stepping through the general logic.
6670              */
6671
6672             if (SvROK(sv))
6673                 goto free_rv;
6674             SvFLAGS(sv) &= SVf_BREAK;
6675             SvFLAGS(sv) |= SVTYPEMASK;
6676             goto free_head;
6677         }
6678
6679         /* objs are always >= MG, but pad names use the SVs_OBJECT flag
6680            for another purpose  */
6681         assert(!SvOBJECT(sv) || type >= SVt_PVMG);
6682
6683         if (type >= SVt_PVMG) {
6684             if (SvOBJECT(sv)) {
6685                 if (!curse(sv, 1)) goto get_next_sv;
6686                 type = SvTYPE(sv); /* destructor may have changed it */
6687             }
6688             /* Free back-references before magic, in case the magic calls
6689              * Perl code that has weak references to sv. */
6690             if (type == SVt_PVHV) {
6691                 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
6692                 if (SvMAGIC(sv))
6693                     mg_free(sv);
6694             }
6695             else if (SvMAGIC(sv)) {
6696                 /* Free back-references before other types of magic. */
6697                 sv_unmagic(sv, PERL_MAGIC_backref);
6698                 mg_free(sv);
6699             }
6700             SvMAGICAL_off(sv);
6701         }
6702         switch (type) {
6703             /* case SVt_INVLIST: */
6704         case SVt_PVIO:
6705             if (IoIFP(sv) &&
6706                 IoIFP(sv) != PerlIO_stdin() &&
6707                 IoIFP(sv) != PerlIO_stdout() &&
6708                 IoIFP(sv) != PerlIO_stderr() &&
6709                 !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6710             {
6711                 io_close(MUTABLE_IO(sv), NULL, FALSE,
6712                          (IoTYPE(sv) == IoTYPE_WRONLY ||
6713                           IoTYPE(sv) == IoTYPE_RDWR   ||
6714                           IoTYPE(sv) == IoTYPE_APPEND));
6715             }
6716             if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6717                 PerlDir_close(IoDIRP(sv));
6718             IoDIRP(sv) = (DIR*)NULL;
6719             Safefree(IoTOP_NAME(sv));
6720             Safefree(IoFMT_NAME(sv));
6721             Safefree(IoBOTTOM_NAME(sv));
6722             if ((const GV *)sv == PL_statgv)
6723                 PL_statgv = NULL;
6724             goto freescalar;
6725         case SVt_REGEXP:
6726             /* FIXME for plugins */
6727             pregfree2((REGEXP*) sv);
6728             goto freescalar;
6729         case SVt_PVCV:
6730         case SVt_PVFM:
6731             cv_undef(MUTABLE_CV(sv));
6732             /* If we're in a stash, we don't own a reference to it.
6733              * However it does have a back reference to us, which needs to
6734              * be cleared.  */
6735             if ((stash = CvSTASH(sv)))
6736                 sv_del_backref(MUTABLE_SV(stash), sv);
6737             goto freescalar;
6738         case SVt_PVHV:
6739             if (HvTOTALKEYS((HV*)sv) > 0) {
6740                 const HEK *hek;
6741                 /* this statement should match the one at the beginning of
6742                  * hv_undef_flags() */
6743                 if (   PL_phase != PERL_PHASE_DESTRUCT
6744                     && (hek = HvNAME_HEK((HV*)sv)))
6745                 {
6746                     if (PL_stashcache) {
6747                         DEBUG_o(Perl_deb(aTHX_
6748                             "sv_clear clearing PL_stashcache for '%" HEKf
6749                             "'\n",
6750                              HEKfARG(hek)));
6751                         (void)hv_deletehek(PL_stashcache,
6752                                            hek, G_DISCARD);
6753                     }
6754                     hv_name_set((HV*)sv, NULL, 0, 0);
6755                 }
6756
6757                 /* save old iter_sv in unused SvSTASH field */
6758                 assert(!SvOBJECT(sv));
6759                 SvSTASH(sv) = (HV*)iter_sv;
6760                 iter_sv = sv;
6761
6762                 /* save old hash_index in unused SvMAGIC field */
6763                 assert(!SvMAGICAL(sv));
6764                 assert(!SvMAGIC(sv));
6765                 ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index = hash_index;
6766                 hash_index = 0;
6767
6768                 next_sv = Perl_hfree_next_entry(aTHX_ (HV*)sv, &hash_index);
6769                 goto get_next_sv; /* process this new sv */
6770             }
6771             /* free empty hash */
6772             Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6773             assert(!HvARRAY((HV*)sv));
6774             break;
6775         case SVt_PVAV:
6776             {
6777                 AV* av = MUTABLE_AV(sv);
6778                 if (PL_comppad == av) {
6779                     PL_comppad = NULL;
6780                     PL_curpad = NULL;
6781                 }
6782                 if (AvREAL(av) && AvFILLp(av) > -1) {
6783                     next_sv = AvARRAY(av)[AvFILLp(av)--];
6784                     /* save old iter_sv in top-most slot of AV,
6785                      * and pray that it doesn't get wiped in the meantime */
6786                     AvARRAY(av)[AvMAX(av)] = iter_sv;
6787                     iter_sv = sv;
6788                     goto get_next_sv; /* process this new sv */
6789                 }
6790                 Safefree(AvALLOC(av));
6791             }
6792
6793             break;
6794         case SVt_PVOBJ:
6795             if(ObjectMAXFIELD(sv) > -1) {
6796                 next_sv = ObjectFIELDS(sv)[ObjectMAXFIELD(sv)--];
6797                 /* save old iter_sv in top-most field, and pray that it
6798                  * doesn't get wiped in the meantime */
6799                 ObjectFIELDS(sv)[(ObjectITERSVAT(sv) = ObjectMAXFIELD(sv) + 1)] = iter_sv;
6800                 iter_sv = sv;
6801                 goto get_next_sv;
6802             }
6803             Safefree(ObjectFIELDS(sv));
6804             break;
6805         case SVt_PVLV:
6806             if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
6807                 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
6808                 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
6809                 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
6810             }
6811             else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV**  */
6812                 SvREFCNT_dec(LvTARG(sv));
6813             if (isREGEXP(sv)) {
6814                 /* This PVLV has had a REGEXP assigned to it - the memory
6815                  * normally used to store SvLEN instead points to a regex body.
6816                  * Retrieving the pointer to the regex body from the correct
6817                  * location is normally abstracted by ReANY(), which handles
6818                  * both SVt_PVLV and SVt_REGEXP
6819                  *
6820                  * This code is unwinding the storage specific to SVt_PVLV.
6821                  * We get the body pointer directly from the union, free it,
6822                  * then set SvLEN to whatever value was in the now-freed regex
6823                  * body. The PVX buffer is shared by multiple re's and only
6824                  * freed once, by the re whose SvLEN is non-null.
6825                  *
6826                  * Perl_sv_force_normal_flags() also has code to free this
6827                  * hidden body - it swaps the body into a temporary SV it has
6828                  * just allocated, then frees that SV. That causes execution
6829                  * to reach the SVt_REGEXP: case about 60 lines earlier in this
6830                  * function.
6831                  *
6832                  * See Perl_reg_temp_copy() for the code that sets up this
6833                  * REGEXP body referenced by the PVLV. */
6834                 struct regexp *r = ((XPV*)SvANY(sv))->xpv_len_u.xpvlenu_rx;
6835                 STRLEN len = r->xpv_len;
6836                 pregfree2((REGEXP*) sv);
6837                 del_body_by_type(r, SVt_REGEXP);
6838                 SvLEN_set((sv), len);
6839                 goto freescalar;
6840             }
6841             /* FALLTHROUGH */
6842         case SVt_PVGV:
6843             if (isGV_with_GP(sv)) {
6844                 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
6845                    && HvHasENAME(stash))
6846                     mro_method_changed_in(stash);
6847                 gp_free(MUTABLE_GV(sv));
6848                 if (GvNAME_HEK(sv))
6849                     unshare_hek(GvNAME_HEK(sv));
6850                 /* If we're in a stash, we don't own a reference to it.
6851                  * However it does have a back reference to us, which
6852                  * needs to be cleared.  */
6853                 if ((stash = GvSTASH(sv)))
6854                         sv_del_backref(MUTABLE_SV(stash), sv);
6855             }
6856             /* FIXME. There are probably more unreferenced pointers to SVs
6857              * in the interpreter struct that we should check and tidy in
6858              * a similar fashion to this:  */
6859             /* See also S_sv_unglob, which does the same thing. */
6860             if ((const GV *)sv == PL_last_in_gv)
6861                 PL_last_in_gv = NULL;
6862             else if ((const GV *)sv == PL_statgv)
6863                 PL_statgv = NULL;
6864             else if ((const GV *)sv == PL_stderrgv)
6865                 PL_stderrgv = NULL;
6866             /* FALLTHROUGH */
6867         case SVt_PVMG:
6868         case SVt_PVNV:
6869         case SVt_PVIV:
6870         case SVt_INVLIST:
6871         case SVt_PV:
6872           freescalar:
6873             /* Don't bother with SvOOK_off(sv); as we're only going to
6874              * free it.  */
6875             if (SvOOK(sv)) {
6876                 STRLEN offset;
6877                 SvOOK_offset(sv, offset);
6878                 SvPV_set(sv, SvPVX_mutable(sv) - offset);
6879                 /* Don't even bother with turning off the OOK flag.  */
6880             }
6881             if (SvROK(sv)) {
6882             free_rv:
6883                 {
6884                     SV * const target = SvRV(sv);
6885                     if (SvWEAKREF(sv))
6886                         sv_del_backref(target, sv);
6887                     else
6888                         next_sv = target;
6889                 }
6890             }
6891 #ifdef PERL_ANY_COW
6892             else if (SvPVX_const(sv)
6893                      && !(SvTYPE(sv) == SVt_PVIO
6894                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6895             {
6896                 if (SvIsCOW(sv)) {
6897 #ifdef DEBUGGING
6898                     if (DEBUG_C_TEST) {
6899                         PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
6900                         sv_dump(sv);
6901                     }
6902 #endif
6903                     if (SvIsCOW_static(sv)) {
6904                         SvLEN_set(sv, 0);
6905                     }
6906                     else if (SvIsCOW_shared_hash(sv)) {
6907                         unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6908                     }
6909                     else {
6910                         if (CowREFCNT(sv)) {
6911                             sv_buf_to_rw(sv);
6912                             CowREFCNT(sv)--;
6913                             sv_buf_to_ro(sv);
6914                             SvLEN_set(sv, 0);
6915                         }
6916                     }
6917                 }
6918                 if (SvLEN(sv)) {
6919                     Safefree(SvPVX_mutable(sv));
6920                 }
6921             }
6922 #else
6923             else if (SvPVX_const(sv) && SvLEN(sv)
6924                      && !(SvTYPE(sv) == SVt_PVIO
6925                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6926                 Safefree(SvPVX_mutable(sv));
6927             else if (SvPVX_const(sv) && SvIsCOW(sv)) {
6928                 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6929             }
6930 #endif
6931             break;
6932         case SVt_NV:
6933             break;
6934         }
6935
6936       free_body:
6937
6938         {
6939             U32 arena_index;
6940             const struct body_details *sv_type_details;
6941
6942             if (type == SVt_PVHV && HvHasAUX(sv)) {
6943                 arena_index = HVAUX_ARENA_ROOT_IX;
6944                 sv_type_details = &fake_hv_with_aux;
6945             }
6946             else {
6947                 arena_index = type;
6948                 sv_type_details = bodies_by_type + arena_index;
6949             }
6950
6951             SvFLAGS(sv) &= SVf_BREAK;
6952             SvFLAGS(sv) |= SVTYPEMASK;
6953
6954             if (sv_type_details->arena) {
6955                 del_body(((char *)SvANY(sv) + sv_type_details->offset),
6956                          &PL_body_roots[arena_index]);
6957             }
6958             else if (sv_type_details->body_size) {
6959                 safefree(SvANY(sv));
6960             }
6961         }
6962
6963       free_head:
6964         /* caller is responsible for freeing the head of the original sv */
6965         if (sv != orig_sv && !SvREFCNT(sv))
6966             del_SV(sv);
6967
6968         /* grab and free next sv, if any */
6969       get_next_sv:
6970         while (1) {
6971             sv = NULL;
6972             if (next_sv) {
6973                 sv = next_sv;
6974                 next_sv = NULL;
6975             }
6976             else if (!iter_sv) {
6977                 break;
6978             } else if (SvTYPE(iter_sv) == SVt_PVAV) {
6979                 AV *const av = (AV*)iter_sv;
6980                 if (AvFILLp(av) > -1) {
6981                     sv = AvARRAY(av)[AvFILLp(av)--];
6982                 }
6983                 else { /* no more elements of current AV to free */
6984                     sv = iter_sv;
6985                     type = SvTYPE(sv);
6986                     /* restore previous value, squirrelled away */
6987                     iter_sv = AvARRAY(av)[AvMAX(av)];
6988                     Safefree(AvALLOC(av));
6989                     goto free_body;
6990                 }
6991             } else if (SvTYPE(iter_sv) == SVt_PVOBJ) {
6992                 if (ObjectMAXFIELD(iter_sv) > -1) {
6993                     sv = ObjectFIELDS(iter_sv)[ObjectMAXFIELD(iter_sv)--];
6994                 }
6995                 else { /* no more fields in the current SV to free */
6996                     sv = iter_sv;
6997                     type = SvTYPE(sv);
6998                     iter_sv = ObjectFIELDS(sv)[ObjectITERSVAT(sv)];
6999                     Safefree(ObjectFIELDS(sv));
7000                     goto free_body;
7001                 }
7002             } else if (SvTYPE(iter_sv) == SVt_PVHV) {
7003                 sv = Perl_hfree_next_entry(aTHX_ (HV*)iter_sv, &hash_index);
7004                 if (!sv && !HvTOTALKEYS((HV *)iter_sv)) {
7005                     /* no more elements of current HV to free */
7006                     sv = iter_sv;
7007                     type = SvTYPE(sv);
7008                     /* Restore previous values of iter_sv and hash_index,
7009                      * squirrelled away */
7010                     assert(!SvOBJECT(sv));
7011                     iter_sv = (SV*)SvSTASH(sv);
7012                     assert(!SvMAGICAL(sv));
7013                     hash_index = ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index;
7014 #ifdef DEBUGGING
7015                     /* perl -DA does not like rubbish in SvMAGIC. */
7016                     SvMAGIC_set(sv, 0);
7017 #endif
7018
7019                     /* free any remaining detritus from the hash struct */
7020                     Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
7021                     assert(!HvARRAY((HV*)sv));
7022                     goto free_body;
7023                 }
7024             }
7025
7026             /* unrolled SvREFCNT_dec and sv_free2 follows: */
7027
7028             if (!sv)
7029                 continue;
7030             if (!SvREFCNT(sv)) {
7031                 sv_free(sv);
7032                 continue;
7033             }
7034             if (--(SvREFCNT(sv)))
7035                 continue;
7036 #ifdef DEBUGGING
7037             if (SvTEMP(sv)) {
7038                 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7039                          "Attempt to free temp prematurely: SV 0x%" UVxf
7040                          pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7041                 continue;
7042             }
7043 #endif
7044             if (SvIMMORTAL(sv)) {
7045                 /* make sure SvREFCNT(sv)==0 happens very seldom */
7046                 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7047                 continue;
7048             }
7049             break;
7050         } /* while 1 */
7051
7052     } /* while sv */
7053 }
7054
7055 /* This routine curses the sv itself, not the object referenced by sv. So
7056    sv does not have to be ROK. */
7057
7058 static bool
7059 S_curse(pTHX_ SV * const sv, const bool check_refcnt) {
7060     PERL_ARGS_ASSERT_CURSE;
7061     assert(SvOBJECT(sv));
7062
7063     if (PL_defstash &&  /* Still have a symbol table? */
7064         SvDESTROYABLE(sv))
7065     {
7066         dSP;
7067         HV* stash;
7068         do {
7069           stash = SvSTASH(sv);
7070           assert(SvTYPE(stash) == SVt_PVHV);
7071           if (HvNAME(stash)) {
7072             CV* destructor = NULL;
7073             struct mro_meta *meta;
7074
7075             assert (HvHasAUX(stash));
7076
7077             DEBUG_o( Perl_deb(aTHX_ "Looking for DESTROY method for %s\n",
7078                          HvNAME(stash)) );
7079
7080             /* don't make this an initialization above the assert, since it needs
7081                an AUX structure */
7082             meta = HvMROMETA(stash);
7083             if (meta->destroy_gen && meta->destroy_gen == PL_sub_generation) {
7084                 destructor = meta->destroy;
7085                 DEBUG_o( Perl_deb(aTHX_ "Using cached DESTROY method %p for %s\n",
7086                              (void *)destructor, HvNAME(stash)) );
7087             }
7088             else {
7089                 bool autoload = FALSE;
7090                 GV *gv =
7091                     gv_fetchmeth_pvn(stash, S_destroy, S_destroy_len, -1, 0);
7092                 if (gv)
7093                     destructor = GvCV(gv);
7094                 if (!destructor) {
7095                     gv = gv_autoload_pvn(stash, S_destroy, S_destroy_len,
7096                                          GV_AUTOLOAD_ISMETHOD);
7097                     if (gv)
7098                         destructor = GvCV(gv);
7099                     if (destructor)
7100                         autoload = TRUE;
7101                 }
7102                 /* we don't cache AUTOLOAD for DESTROY, since this code
7103                    would then need to set $__PACKAGE__::AUTOLOAD, or the
7104                    equivalent for XS AUTOLOADs */
7105                 if (!autoload) {
7106                     meta->destroy_gen = PL_sub_generation;
7107                     meta->destroy = destructor;
7108
7109                     DEBUG_o( Perl_deb(aTHX_ "Set cached DESTROY method %p for %s\n",
7110                                       (void *)destructor, HvNAME(stash)) );
7111                 }
7112                 else {
7113                     DEBUG_o( Perl_deb(aTHX_ "Not caching AUTOLOAD for DESTROY method for %s\n",
7114                                       HvNAME(stash)) );
7115                 }
7116             }
7117             assert(!destructor || SvTYPE(destructor) == SVt_PVCV);
7118             if (destructor
7119                 /* A constant subroutine can have no side effects, so
7120                    don't bother calling it.  */
7121                 && !CvCONST(destructor)
7122                 /* Don't bother calling an empty destructor or one that
7123                    returns immediately. */
7124                 && (CvISXSUB(destructor)
7125                 || (CvSTART(destructor)
7126                     && (CvSTART(destructor)->op_next->op_type
7127                                         != OP_LEAVESUB)
7128                     && (CvSTART(destructor)->op_next->op_type
7129                                         != OP_PUSHMARK
7130                         || CvSTART(destructor)->op_next->op_next->op_type
7131                                         != OP_RETURN
7132                        )
7133                    ))
7134                )
7135             {
7136                 SV* const tmpref = newRV(sv);
7137                 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
7138                 ENTER;
7139                 PUSHSTACKi(PERLSI_DESTROY);
7140                 EXTEND(SP, 2);
7141                 PUSHMARK(SP);
7142                 PUSHs(tmpref);
7143                 PUTBACK;
7144                 call_sv(MUTABLE_SV(destructor),
7145                             G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7146                 POPSTACK;
7147                 SPAGAIN;
7148                 LEAVE;
7149                 if(SvREFCNT(tmpref) < 2) {
7150                     /* tmpref is not kept alive! */
7151                     SvREFCNT(sv)--;
7152                     SvRV_set(tmpref, NULL);
7153                     SvROK_off(tmpref);
7154                 }
7155                 SvREFCNT_dec_NN(tmpref);
7156             }
7157           }
7158         } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
7159
7160
7161         if (check_refcnt && SvREFCNT(sv)) {
7162             if (PL_in_clean_objs)
7163                 Perl_croak(aTHX_
7164                   "DESTROY created new reference to dead object '%" HEKf "'",
7165                    HEKfARG(HvNAME_HEK(stash)));
7166             /* DESTROY gave object new lease on life */
7167             return FALSE;
7168         }
7169     }
7170
7171     if (SvOBJECT(sv)) {
7172         HV * const stash = SvSTASH(sv);
7173         /* Curse before freeing the stash, as freeing the stash could cause
7174            a recursive call into S_curse. */
7175         SvOBJECT_off(sv);       /* Curse the object. */
7176         SvSTASH_set(sv,0);      /* SvREFCNT_dec may try to read this */
7177         SvREFCNT_dec(stash); /* possibly of changed persuasion */
7178     }
7179     return TRUE;
7180 }
7181
7182 /*
7183 =for apidoc sv_newref
7184
7185 Increment an SV's reference count.  Use the C<SvREFCNT_inc()> wrapper
7186 instead.
7187
7188 =cut
7189 */
7190
7191 SV *
7192 Perl_sv_newref(pTHX_ SV *const sv)
7193 {
7194     PERL_UNUSED_CONTEXT;
7195     if (sv)
7196         (SvREFCNT(sv))++;
7197     return sv;
7198 }
7199
7200 /*
7201 =for apidoc sv_free
7202
7203 Decrement an SV's reference count, and if it drops to zero, call
7204 C<sv_clear> to invoke destructors and free up any memory used by
7205 the body; finally, deallocating the SV's head itself.
7206 Normally called via a wrapper macro C<SvREFCNT_dec>.
7207
7208 =cut
7209 */
7210
7211 void
7212 Perl_sv_free(pTHX_ SV *const sv)
7213 {
7214     SvREFCNT_dec(sv);
7215 }
7216
7217
7218 /* Private helper function for SvREFCNT_dec().
7219  * Called with rc set to original SvREFCNT(sv), where rc == 0 or 1 */
7220
7221 void
7222 Perl_sv_free2(pTHX_ SV *const sv, const U32 rc)
7223 {
7224
7225     PERL_ARGS_ASSERT_SV_FREE2;
7226
7227     if (LIKELY( rc == 1 )) {
7228         /* normal case */
7229         SvREFCNT(sv) = 0;
7230
7231 #ifdef DEBUGGING
7232         if (SvTEMP(sv)) {
7233             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7234                              "Attempt to free temp prematurely: SV 0x%" UVxf
7235                              pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7236             return;
7237         }
7238 #endif
7239         if (SvIMMORTAL(sv)) {
7240             /* make sure SvREFCNT(sv)==0 happens very seldom */
7241             SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7242             return;
7243         }
7244         sv_clear(sv);
7245         if (! SvREFCNT(sv)) /* may have have been resurrected */
7246             del_SV(sv);
7247         return;
7248     }
7249
7250     /* handle exceptional cases */
7251
7252     assert(rc == 0);
7253
7254     if (SvFLAGS(sv) & SVf_BREAK)
7255         /* this SV's refcnt has been artificially decremented to
7256          * trigger cleanup */
7257         return;
7258     if (PL_in_clean_all) /* All is fair */
7259         return;
7260     if (SvIMMORTAL(sv)) {
7261         /* make sure SvREFCNT(sv)==0 happens very seldom */
7262         SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7263         return;
7264     }
7265     if (ckWARN_d(WARN_INTERNAL)) {
7266 #ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
7267         Perl_dump_sv_child(aTHX_ sv);
7268 #else
7269     #ifdef DEBUG_LEAKING_SCALARS
7270         sv_dump(sv);
7271     #endif
7272 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7273         if (PL_warnhook == PERL_WARNHOOK_FATAL
7274             || ckDEAD(packWARN(WARN_INTERNAL))) {
7275             /* Don't let Perl_warner cause us to escape our fate:  */
7276             abort();
7277         }
7278 #endif
7279         /* This may not return:  */
7280         Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
7281                     "Attempt to free unreferenced scalar: SV 0x%" UVxf
7282                     pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7283 #endif
7284     }
7285 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7286     abort();
7287 #endif
7288
7289 }
7290
7291
7292 /*
7293 =for apidoc sv_len
7294
7295 Returns the length of the string in the SV.  Handles magic and type
7296 coercion and sets the UTF8 flag appropriately.  See also C<L</SvCUR>>, which
7297 gives raw access to the C<xpv_cur> slot.
7298
7299 =cut
7300 */
7301
7302 STRLEN
7303 Perl_sv_len(pTHX_ SV *const sv)
7304 {
7305     STRLEN len;
7306
7307     if (!sv)
7308         return 0;
7309
7310     (void)SvPV_const(sv, len);
7311     return len;
7312 }
7313
7314 /*
7315 =for apidoc sv_len_utf8
7316 =for apidoc_item sv_len_utf8_nomg
7317
7318 These return the number of characters in the string in an SV, counting wide
7319 UTF-8 bytes as a single character.  Both handle type coercion.
7320 They differ only in that C<sv_len_utf8> performs 'get' magic;
7321 C<sv_len_utf8_nomg> skips any magic.
7322
7323 =cut
7324 */
7325
7326 /*
7327  * The length is cached in PERL_MAGIC_utf8, in the mg_len field.  Also the
7328  * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below.
7329  * (Note that the mg_len is not the length of the mg_ptr field.
7330  * This allows the cache to store the character length of the string without
7331  * needing to malloc() extra storage to attach to the mg_ptr.)
7332  *
7333  */
7334
7335 STRLEN
7336 Perl_sv_len_utf8(pTHX_ SV *const sv)
7337 {
7338     if (!sv)
7339         return 0;
7340
7341     SvGETMAGIC(sv);
7342     return sv_len_utf8_nomg(sv);
7343 }
7344
7345 STRLEN
7346 Perl_sv_len_utf8_nomg(pTHX_ SV * const sv)
7347 {
7348     STRLEN len;
7349     const U8 *s = (U8*)SvPV_nomg_const(sv, len);
7350
7351     PERL_ARGS_ASSERT_SV_LEN_UTF8_NOMG;
7352
7353     if (PL_utf8cache && SvUTF8(sv)) {
7354             STRLEN ulen;
7355             MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
7356
7357             if (mg && (mg->mg_len != -1 || mg->mg_ptr)) {
7358                 if (mg->mg_len != -1)
7359                     ulen = mg->mg_len;
7360                 else {
7361                     /* We can use the offset cache for a headstart.
7362                        The longer value is stored in the first pair.  */
7363                     STRLEN *cache = (STRLEN *) mg->mg_ptr;
7364
7365                     ulen = cache[0] + Perl_utf8_length(aTHX_ s + cache[1],
7366                                                        s + len);
7367                 }
7368
7369                 if (PL_utf8cache < 0) {
7370                     const STRLEN real = Perl_utf8_length(aTHX_ s, s + len);
7371                     assert_uft8_cache_coherent("sv_len_utf8", ulen, real, sv);
7372                 }
7373             }
7374             else {
7375                 ulen = Perl_utf8_length(aTHX_ s, s + len);
7376                 utf8_mg_len_cache_update(sv, &mg, ulen);
7377             }
7378             return ulen;
7379     }
7380     return SvUTF8(sv) ? Perl_utf8_length(aTHX_ s, s + len) : len;
7381 }
7382
7383 /* Walk forwards to find the byte corresponding to the passed in UTF-8
7384    offset.  */
7385 static STRLEN
7386 S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send,
7387                       STRLEN *const uoffset_p, bool *const at_end,
7388                       bool* canonical_position)
7389 {
7390     const U8 *s = start;
7391     STRLEN uoffset = *uoffset_p;
7392
7393     PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS;
7394
7395     while (s < send && uoffset) {
7396         --uoffset;
7397         s += UTF8SKIP(s);
7398     }
7399     if (s == send) {
7400         *at_end = TRUE;
7401     }
7402     else if (s > send) {
7403         *at_end = TRUE;
7404         /* This is the existing behaviour. Possibly it should be a croak, as
7405            it's actually a bounds error  */
7406         s = send;
7407     }
7408     /* If the unicode position is beyond the end, we return the end but
7409        shouldn't cache that position */
7410     *canonical_position = (uoffset == 0);
7411     *uoffset_p -= uoffset;
7412     return s - start;
7413 }
7414
7415 /* Given the length of the string in both bytes and UTF-8 characters, decide
7416    whether to walk forwards or backwards to find the byte corresponding to
7417    the passed in UTF-8 offset.  */
7418 static STRLEN
7419 S_sv_pos_u2b_midway(const U8 *const start, const U8 *send,
7420                     STRLEN uoffset, const STRLEN uend)
7421 {
7422     STRLEN backw = uend - uoffset;
7423
7424     PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY;
7425
7426     if (uoffset < 2 * backw) {
7427         /* The assumption is that the average size of a character is 2 bytes,
7428          * so going forwards is twice the speed of going backwards (that's
7429          * where the 2 * backw comes from).  (The real figure of course depends
7430          * on the UTF-8 data.)  */
7431         const U8 *s = start;
7432
7433         s = utf8_hop_forward(s, uoffset, send);
7434         assert (s <= send);
7435         if (s > send)
7436             s = send;
7437         return s - start;
7438     }
7439
7440     send = utf8_hop_back(send, -backw, start);
7441     return send - start;
7442 }
7443
7444 /* For the string representation of the given scalar, find the byte
7445    corresponding to the passed in UTF-8 offset.  uoffset0 and boffset0
7446    give another position in the string, *before* the sought offset, which
7447    (which is always true, as 0, 0 is a valid pair of positions), which should
7448    help reduce the amount of linear searching.
7449    If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
7450    will be used to reduce the amount of linear searching. The cache will be
7451    created if necessary, and the found value offered to it for update.  */
7452 static STRLEN
7453 S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
7454                     const U8 *const send, STRLEN uoffset,
7455                     STRLEN uoffset0, STRLEN boffset0)
7456 {
7457     STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy.  */
7458     bool found = FALSE;
7459     bool at_end = FALSE;
7460     bool canonical_position = FALSE;
7461
7462     PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
7463
7464     assert (uoffset >= uoffset0);
7465
7466     if (!uoffset)
7467         return 0;
7468
7469     if (!SvREADONLY(sv) && !SvGMAGICAL(sv) && SvPOK(sv)
7470         && PL_utf8cache
7471         && (*mgp || (SvTYPE(sv) >= SVt_PVMG &&
7472                      (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) {
7473         if ((*mgp)->mg_ptr) {
7474             STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
7475             if (cache[0] == uoffset) {
7476                 /* An exact match. */
7477                 return cache[1];
7478             }
7479             if (cache[2] == uoffset) {
7480                 /* An exact match. */
7481                 return cache[3];
7482             }
7483
7484             if (cache[0] < uoffset) {
7485                 /* The cache already knows part of the way.   */
7486                 if (cache[0] > uoffset0) {
7487                     /* The cache knows more than the passed in pair  */
7488                     uoffset0 = cache[0];
7489                     boffset0 = cache[1];
7490                 }
7491                 if ((*mgp)->mg_len != -1) {
7492                     /* And we know the end too.  */
7493                     boffset = boffset0
7494                         + sv_pos_u2b_midway(start + boffset0, send,
7495                                               uoffset - uoffset0,
7496                                               (*mgp)->mg_len - uoffset0);
7497                 } else {
7498                     uoffset -= uoffset0;
7499                     boffset = boffset0
7500                         + sv_pos_u2b_forwards(start + boffset0,
7501                                               send, &uoffset, &at_end,
7502                                               &canonical_position);
7503                     uoffset += uoffset0;
7504                 }
7505             }
7506             else if (cache[2] < uoffset) {
7507                 /* We're between the two cache entries.  */
7508                 if (cache[2] > uoffset0) {
7509                     /* and the cache knows more than the passed in pair  */
7510                     uoffset0 = cache[2];
7511                     boffset0 = cache[3];
7512                 }
7513
7514                 boffset = boffset0
7515                     + sv_pos_u2b_midway(start + boffset0,
7516                                           start + cache[1],
7517                                           uoffset - uoffset0,
7518                                           cache[0] - uoffset0);
7519             } else {
7520                 boffset = boffset0
7521                     + sv_pos_u2b_midway(start + boffset0,
7522                                           start + cache[3],
7523                                           uoffset - uoffset0,
7524                                           cache[2] - uoffset0);
7525             }
7526             found = TRUE;
7527         }
7528         else if ((*mgp)->mg_len != -1) {
7529             /* If we can take advantage of a passed in offset, do so.  */
7530             /* In fact, offset0 is either 0, or less than offset, so don't
7531                need to worry about the other possibility.  */
7532             boffset = boffset0
7533                 + sv_pos_u2b_midway(start + boffset0, send,
7534                                       uoffset - uoffset0,
7535                                       (*mgp)->mg_len - uoffset0);
7536             found = TRUE;
7537         }
7538     }
7539
7540     if (!found || PL_utf8cache < 0) {
7541         STRLEN real_boffset;
7542         uoffset -= uoffset0;
7543         real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0,
7544                                                       send, &uoffset, &at_end,
7545                                                       &canonical_position);
7546         uoffset += uoffset0;
7547
7548         if (found && PL_utf8cache < 0)
7549             assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset,
7550                                        real_boffset, sv);
7551         boffset = real_boffset;
7552     }
7553
7554     if (PL_utf8cache && canonical_position && !SvGMAGICAL(sv) && SvPOK(sv)) {
7555         if (at_end)
7556             utf8_mg_len_cache_update(sv, mgp, uoffset);
7557         else
7558             utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
7559     }
7560     return boffset;
7561 }
7562
7563
7564 /*
7565 =for apidoc sv_pos_u2b_flags
7566
7567 Converts the offset from a count of UTF-8 chars from
7568 the start of the string, to a count of the equivalent number of bytes; if
7569 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7570 C<offset>, rather than from the start
7571 of the string.  Handles type coercion.
7572 C<flags> is passed to C<SvPV_flags>, and usually should be
7573 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7574
7575 =cut
7576 */
7577
7578 /*
7579  * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential
7580  * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7581  * byte offsets.  See also the comments of S_utf8_mg_pos_cache_update().
7582  *
7583  */
7584
7585 STRLEN
7586 Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp,
7587                       U32 flags)
7588 {
7589     const U8 *start;
7590     STRLEN len;
7591     STRLEN boffset;
7592
7593     PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS;
7594
7595     start = (U8*)SvPV_flags(sv, len, flags);
7596     if (len) {
7597         const U8 * const send = start + len;
7598         MAGIC *mg = NULL;
7599         boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0);
7600
7601         if (lenp
7602             && *lenp /* don't bother doing work for 0, as its bytes equivalent
7603                         is 0, and *lenp is already set to that.  */) {
7604             /* Convert the relative offset to absolute.  */
7605             const STRLEN uoffset2 = uoffset + *lenp;
7606             const STRLEN boffset2
7607                 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
7608                                       uoffset, boffset) - boffset;
7609
7610             *lenp = boffset2;
7611         }
7612     } else {
7613         if (lenp)
7614             *lenp = 0;
7615         boffset = 0;
7616     }
7617
7618     return boffset;
7619 }
7620
7621 /*
7622 =for apidoc sv_pos_u2b
7623
7624 Converts the value pointed to by C<offsetp> from a count of UTF-8 chars from
7625 the start of the string, to a count of the equivalent number of bytes; if
7626 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7627 the offset, rather than from the start of the string.  Handles magic and
7628 type coercion.
7629
7630 Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer
7631 than 2Gb.
7632
7633 =cut
7634 */
7635
7636 /*
7637  * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
7638  * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7639  * byte offsets.  See also the comments of S_utf8_mg_pos_cache_update().
7640  *
7641  */
7642
7643 /* This function is subject to size and sign problems */
7644
7645 void
7646 Perl_sv_pos_u2b(pTHX_ SV *const sv, I32 *const offsetp, I32 *const lenp)
7647 {
7648     PERL_ARGS_ASSERT_SV_POS_U2B;
7649
7650     if (lenp) {
7651         STRLEN ulen = (STRLEN)*lenp;
7652         *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen,
7653                                          SV_GMAGIC|SV_CONST_RETURN);
7654         *lenp = (I32)ulen;
7655     } else {
7656         *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL,
7657                                          SV_GMAGIC|SV_CONST_RETURN);
7658     }
7659 }
7660
7661 static void
7662 S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp,
7663                            const STRLEN ulen)
7664 {
7665     PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE;
7666     if (SvREADONLY(sv) || SvGMAGICAL(sv) || !SvPOK(sv))
7667         return;
7668
7669     if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7670                   !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7671         *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
7672     }
7673     assert(*mgp);
7674
7675     (*mgp)->mg_len = ulen;
7676 }
7677
7678 /* Create and update the UTF8 magic offset cache, with the proffered utf8/
7679    byte length pairing. The (byte) length of the total SV is passed in too,
7680    as blen, because for some (more esoteric) SVs, the call to SvPV_const()
7681    may not have updated SvCUR, so we can't rely on reading it directly.
7682
7683    The proffered utf8/byte length pairing isn't used if the cache already has
7684    two pairs, and swapping either for the proffered pair would increase the
7685    RMS of the intervals between known byte offsets.
7686
7687    The cache itself consists of 4 STRLEN values
7688    0: larger UTF-8 offset
7689    1: corresponding byte offset
7690    2: smaller UTF-8 offset
7691    3: corresponding byte offset
7692
7693    Unused cache pairs have the value 0, 0.
7694    Keeping the cache "backwards" means that the invariant of
7695    cache[0] >= cache[2] is maintained even with empty slots, which means that
7696    the code that uses it doesn't need to worry if only 1 entry has actually
7697    been set to non-zero.  It also makes the "position beyond the end of the
7698    cache" logic much simpler, as the first slot is always the one to start
7699    from.
7700 */
7701 static void
7702 S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
7703                            const STRLEN utf8, const STRLEN blen)
7704 {
7705     STRLEN *cache;
7706
7707     PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
7708
7709     if (SvREADONLY(sv))
7710         return;
7711
7712     if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7713                   !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7714         *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
7715                            0);
7716         (*mgp)->mg_len = -1;
7717     }
7718     assert(*mgp);
7719
7720     if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
7721         Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7722         (*mgp)->mg_ptr = (char *) cache;
7723     }
7724     assert(cache);
7725
7726     if (PL_utf8cache < 0 && SvPOKp(sv)) {
7727         /* SvPOKp() because, if sv is a reference, then SvPVX() is actually
7728            a pointer.  Note that we no longer cache utf8 offsets on refer-
7729            ences, but this check is still a good idea, for robustness.  */
7730         const U8 *start = (const U8 *) SvPVX_const(sv);
7731         const STRLEN realutf8 = utf8_length(start, start + byte);
7732
7733         assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8,
7734                                    sv);
7735     }
7736
7737     /* Cache is held with the later position first, to simplify the code
7738        that deals with unbounded ends.  */
7739
7740     ASSERT_UTF8_CACHE(cache);
7741     if (cache[1] == 0) {
7742         /* Cache is totally empty  */
7743         cache[0] = utf8;
7744         cache[1] = byte;
7745     } else if (cache[3] == 0) {
7746         if (byte > cache[1]) {
7747             /* New one is larger, so goes first.  */
7748             cache[2] = cache[0];
7749             cache[3] = cache[1];
7750             cache[0] = utf8;
7751             cache[1] = byte;
7752         } else {
7753             cache[2] = utf8;
7754             cache[3] = byte;
7755         }
7756     } else {
7757 /* float casts necessary? XXX */
7758 #define THREEWAY_SQUARE(a,b,c,d) \
7759             ((float)((d) - (c))) * ((float)((d) - (c))) \
7760             + ((float)((c) - (b))) * ((float)((c) - (b))) \
7761                + ((float)((b) - (a))) * ((float)((b) - (a)))
7762
7763         /* Cache has 2 slots in use, and we know three potential pairs.
7764            Keep the two that give the lowest RMS distance. Do the
7765            calculation in bytes simply because we always know the byte
7766            length.  squareroot has the same ordering as the positive value,
7767            so don't bother with the actual square root.  */
7768         if (byte > cache[1]) {
7769             /* New position is after the existing pair of pairs.  */
7770             const float keep_earlier
7771                 = THREEWAY_SQUARE(0, cache[3], byte, blen);
7772             const float keep_later
7773                 = THREEWAY_SQUARE(0, cache[1], byte, blen);
7774
7775             if (keep_later < keep_earlier) {
7776                 cache[2] = cache[0];
7777                 cache[3] = cache[1];
7778             }
7779             cache[0] = utf8;
7780             cache[1] = byte;
7781         }
7782         else {
7783             const float keep_later = THREEWAY_SQUARE(0, byte, cache[1], blen);
7784             float b, c, keep_earlier;
7785             if (byte > cache[3]) {
7786                 /* New position is between the existing pair of pairs.  */
7787                 b = (float)cache[3];
7788                 c = (float)byte;
7789             } else {
7790                 /* New position is before the existing pair of pairs.  */
7791                 b = (float)byte;
7792                 c = (float)cache[3];
7793             }
7794             keep_earlier = THREEWAY_SQUARE(0, b, c, blen);
7795             if (byte > cache[3]) {
7796                 if (keep_later < keep_earlier) {
7797                     cache[2] = utf8;
7798                     cache[3] = byte;
7799                 }
7800                 else {
7801                     cache[0] = utf8;
7802                     cache[1] = byte;
7803                 }
7804             }
7805             else {
7806                 if (! (keep_later < keep_earlier)) {
7807                     cache[0] = cache[2];
7808                     cache[1] = cache[3];
7809                 }
7810                 cache[2] = utf8;
7811                 cache[3] = byte;
7812             }
7813         }
7814     }
7815     ASSERT_UTF8_CACHE(cache);
7816 }
7817
7818 /* We already know all of the way, now we may be able to walk back.  The same
7819    assumption is made as in S_sv_pos_u2b_midway(), namely that walking
7820    backward is half the speed of walking forward. */
7821 static STRLEN
7822 S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
7823                     const U8 *end, STRLEN endu)
7824 {
7825     const STRLEN forw = target - s;
7826     STRLEN backw = end - target;
7827
7828     PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
7829
7830     if (forw < 2 * backw) {
7831         return utf8_length(s, target);
7832     }
7833
7834     while (end > target) {
7835         end = utf8_hop_back(end, -1, target);
7836         endu--;
7837     }
7838     return endu;
7839 }
7840
7841 /*
7842 =for apidoc sv_pos_b2u_flags
7843
7844 Converts C<offset> from a count of bytes from the start of the string, to
7845 a count of the equivalent number of UTF-8 chars.  Handles type coercion.
7846 C<flags> is passed to C<SvPV_flags>, and usually should be
7847 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7848
7849 =cut
7850 */
7851
7852 /*
7853  * sv_pos_b2u_flags() uses, like sv_pos_u2b_flags(), the mg_ptr of the
7854  * potential PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8
7855  * and byte offsets.
7856  *
7857  */
7858 STRLEN
7859 Perl_sv_pos_b2u_flags(pTHX_ SV *const sv, STRLEN const offset, U32 flags)
7860 {
7861     const U8* s;
7862     STRLEN len = 0; /* Actually always set, but let's keep gcc happy.  */
7863     STRLEN blen;
7864     MAGIC* mg = NULL;
7865     const U8* send;
7866     bool found = FALSE;
7867
7868     PERL_ARGS_ASSERT_SV_POS_B2U_FLAGS;
7869
7870     s = (const U8*)SvPV_flags(sv, blen, flags);
7871
7872     if (blen < offset)
7873         Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset, blen=%" UVuf
7874                    ", byte=%" UVuf, (UV)blen, (UV)offset);
7875
7876     send = s + offset;
7877
7878     if (!SvREADONLY(sv)
7879         && PL_utf8cache
7880         && SvTYPE(sv) >= SVt_PVMG
7881         && (mg = mg_find(sv, PERL_MAGIC_utf8)))
7882     {
7883         if (mg->mg_ptr) {
7884             STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7885             if (cache[1] == offset) {
7886                 /* An exact match. */
7887                 return cache[0];
7888             }
7889             if (cache[3] == offset) {
7890                 /* An exact match. */
7891                 return cache[2];
7892             }
7893
7894             if (cache[1] < offset) {
7895                 /* We already know part of the way. */
7896                 if (mg->mg_len != -1) {
7897                     /* Actually, we know the end too.  */
7898                     len = cache[0]
7899                         + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
7900                                               s + blen, mg->mg_len - cache[0]);
7901                 } else {
7902                     len = cache[0] + utf8_length(s + cache[1], send);
7903                 }
7904             }
7905             else if (cache[3] < offset) {
7906                 /* We're between the two cached pairs, so we do the calculation
7907                    offset by the byte/utf-8 positions for the earlier pair,
7908                    then add the utf-8 characters from the string start to
7909                    there.  */
7910                 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
7911                                           s + cache[1], cache[0] - cache[2])
7912                     + cache[2];
7913
7914             }
7915             else { /* cache[3] > offset */
7916                 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
7917                                           cache[2]);
7918
7919             }
7920             ASSERT_UTF8_CACHE(cache);
7921             found = TRUE;
7922         } else if (mg->mg_len != -1) {
7923             len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
7924             found = TRUE;
7925         }
7926     }
7927     if (!found || PL_utf8cache < 0) {
7928         const STRLEN real_len = utf8_length(s, send);
7929
7930         if (found && PL_utf8cache < 0)
7931             assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv);
7932         len = real_len;
7933     }
7934
7935     if (PL_utf8cache) {
7936         if (blen == offset)
7937             utf8_mg_len_cache_update(sv, &mg, len);
7938         else
7939             utf8_mg_pos_cache_update(sv, &mg, offset, len, blen);
7940     }
7941
7942     return len;
7943 }
7944
7945 /*
7946 =for apidoc sv_pos_b2u
7947
7948 Converts the value pointed to by C<offsetp> from a count of bytes from the
7949 start of the string, to a count of the equivalent number of UTF-8 chars.
7950 Handles magic and type coercion.
7951
7952 Use C<sv_pos_b2u_flags> in preference, which correctly handles strings
7953 longer than 2Gb.
7954
7955 =cut
7956 */
7957
7958 /*
7959  * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
7960  * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7961  * byte offsets.
7962  *
7963  */
7964 void
7965 Perl_sv_pos_b2u(pTHX_ SV *const sv, I32 *const offsetp)
7966 {
7967     PERL_ARGS_ASSERT_SV_POS_B2U;
7968
7969     if (!sv)
7970         return;
7971
7972     *offsetp = (I32)sv_pos_b2u_flags(sv, (STRLEN)*offsetp,
7973                                      SV_GMAGIC|SV_CONST_RETURN);
7974 }
7975
7976 static void
7977 S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache,
7978                              STRLEN real, SV *const sv)
7979 {
7980     PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT;
7981
7982     /* As this is debugging only code, save space by keeping this test here,
7983        rather than inlining it in all the callers.  */
7984     if (from_cache == real)
7985         return;
7986
7987     /* Need to turn the assertions off otherwise we may recurse infinitely
7988        while printing error messages.  */
7989     SAVEI8(PL_utf8cache);
7990     PL_utf8cache = 0;
7991     Perl_croak(aTHX_ "panic: %s cache %" UVuf " real %" UVuf " for %" SVf,
7992                func, (UV) from_cache, (UV) real, SVfARG(sv));
7993 }
7994
7995 /*
7996 =for apidoc      sv_eq
7997 =for apidoc_item sv_eq_flags
7998
7999 These each return a boolean indicating whether or not the strings in the two
8000 SVs are equal.  If S<C<'use bytes'>> is in effect, the comparison is
8001 byte-by-byte; otherwise character-by-character.  Each will coerce its args to
8002 strings if necessary.
8003
8004 They differ only in that C<sv_eq> always processes get magic, while
8005 C<sv_eq_flags> processes get magic only when the C<flags> parameter has the
8006 C<SV_GMAGIC> bit set.
8007
8008 These functions do not handle operator overloading.  For versions that do,
8009 see instead C<L</sv_streq>> or C<L</sv_streq_flags>>.
8010
8011 =cut
8012 */
8013
8014 I32
8015 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8016 {
8017     const char *pv1;
8018     STRLEN cur1;
8019     const char *pv2;
8020     STRLEN cur2;
8021
8022     if (!sv1) {
8023         pv1 = "";
8024         cur1 = 0;
8025     }
8026     else {
8027         /* if pv1 and pv2 are the same, second SvPV_const call may
8028          * invalidate pv1 (if we are handling magic), so we may need to
8029          * make a copy */
8030         if (sv1 == sv2 && flags & SV_GMAGIC
8031          && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
8032             pv1 = SvPV_const(sv1, cur1);
8033             sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
8034         }
8035         pv1 = SvPV_flags_const(sv1, cur1, flags);
8036     }
8037
8038     if (!sv2){
8039         pv2 = "";
8040         cur2 = 0;
8041     }
8042     else
8043         pv2 = SvPV_flags_const(sv2, cur2, flags);
8044
8045     if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8046         /* Differing utf8ness.  */
8047         if (SvUTF8(sv1)) {
8048                   /* sv1 is the UTF-8 one  */
8049                   return bytes_cmp_utf8((const U8*)pv2, cur2,
8050                                         (const U8*)pv1, cur1) == 0;
8051         }
8052         else {
8053                   /* sv2 is the UTF-8 one  */
8054                   return bytes_cmp_utf8((const U8*)pv1, cur1,
8055                                         (const U8*)pv2, cur2) == 0;
8056         }
8057     }
8058
8059     if (cur1 == cur2)
8060         return (pv1 == pv2) || memEQ(pv1, pv2, cur1);
8061     else
8062         return 0;
8063 }
8064
8065 /*
8066 =for apidoc sv_streq_flags
8067
8068 Returns a boolean indicating whether the strings in the two SVs are
8069 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8070 get-magic too. Will coerce its args to strings if necessary. Treats
8071 C<NULL> as undef. Correctly handles the UTF8 flag.
8072
8073 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8074 C<eq> overloading will be made. If such overloading does not exist or the
8075 flag is set, then regular string comparison will be used instead.
8076
8077 =for apidoc sv_streq
8078
8079 A convenient shortcut for calling C<sv_streq_flags> with the C<SV_GMAGIC>
8080 flag. This function basically behaves like the Perl code C<$sv1 eq $sv2>.
8081
8082 =cut
8083 */
8084
8085 bool
8086 Perl_sv_streq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8087 {
8088     PERL_ARGS_ASSERT_SV_STREQ_FLAGS;
8089
8090     if(flags & SV_GMAGIC) {
8091         if(sv1)
8092             SvGETMAGIC(sv1);
8093         if(sv2)
8094             SvGETMAGIC(sv2);
8095     }
8096
8097     /* Treat NULL as undef */
8098     if(!sv1)
8099         sv1 = &PL_sv_undef;
8100     if(!sv2)
8101         sv2 = &PL_sv_undef;
8102
8103     if(!(flags & SV_SKIP_OVERLOAD) &&
8104             (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8105         SV *ret = amagic_call(sv1, sv2, seq_amg, 0);
8106         if(ret)
8107             return SvTRUE(ret);
8108     }
8109
8110     return sv_eq_flags(sv1, sv2, 0);
8111 }
8112
8113 /*
8114 =for apidoc sv_numeq_flags
8115
8116 Returns a boolean indicating whether the numbers in the two SVs are
8117 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8118 get-magic too. Will coerce its args to numbers if necessary. Treats
8119 C<NULL> as undef.
8120
8121 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8122 C<==> overloading will be made. If such overloading does not exist or the
8123 flag is set, then regular numerical comparison will be used instead.
8124
8125 =for apidoc sv_numeq
8126
8127 A convenient shortcut for calling C<sv_numeq_flags> with the C<SV_GMAGIC>
8128 flag. This function basically behaves like the Perl code C<$sv1 == $sv2>.
8129
8130 =cut
8131 */
8132
8133 bool
8134 Perl_sv_numeq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8135 {
8136     PERL_ARGS_ASSERT_SV_NUMEQ_FLAGS;
8137
8138     if(flags & SV_GMAGIC) {
8139         if(sv1)
8140             SvGETMAGIC(sv1);
8141         if(sv2)
8142             SvGETMAGIC(sv2);
8143     }
8144
8145     /* Treat NULL as undef */
8146     if(!sv1)
8147         sv1 = &PL_sv_undef;
8148     if(!sv2)
8149         sv2 = &PL_sv_undef;
8150
8151     if(!(flags & SV_SKIP_OVERLOAD) &&
8152             (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8153         SV *ret = amagic_call(sv1, sv2, eq_amg, 0);
8154         if(ret)
8155             return SvTRUE(ret);
8156     }
8157
8158     return do_ncmp(sv1, sv2) == 0;
8159 }
8160
8161 /*
8162 =for apidoc sv_cmp
8163
8164 Compares the strings in two SVs.  Returns -1, 0, or 1 indicating whether the
8165 string in C<sv1> is less than, equal to, or greater than the string in
8166 C<sv2>.  Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
8167 coerce its args to strings if necessary.  See also C<L</sv_cmp_locale>>.
8168
8169 =for apidoc sv_cmp_flags
8170
8171 Compares the strings in two SVs.  Returns -1, 0, or 1 indicating whether the
8172 string in C<sv1> is less than, equal to, or greater than the string in
8173 C<sv2>.  Is UTF-8 and S<C<'use bytes'>> aware and will coerce its args to strings
8174 if necessary.  If the flags has the C<SV_GMAGIC> bit set, it handles get magic.  See
8175 also C<L</sv_cmp_locale_flags>>.
8176
8177 =cut
8178 */
8179
8180 I32
8181 Perl_sv_cmp(pTHX_ SV *const sv1, SV *const sv2)
8182 {
8183     return sv_cmp_flags(sv1, sv2, SV_GMAGIC);
8184 }
8185
8186 I32
8187 Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2,
8188                   const U32 flags)
8189 {
8190     STRLEN cur1, cur2;
8191     const char *pv1, *pv2;
8192     I32  cmp;
8193     SV *svrecode = NULL;
8194
8195     if (!sv1) {
8196         pv1 = "";
8197         cur1 = 0;
8198     }
8199     else
8200         pv1 = SvPV_flags_const(sv1, cur1, flags);
8201
8202     if (!sv2) {
8203         pv2 = "";
8204         cur2 = 0;
8205     }
8206     else
8207         pv2 = SvPV_flags_const(sv2, cur2, flags);
8208
8209     if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8210         /* Differing utf8ness.  */
8211         if (SvUTF8(sv1)) {
8212                 const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2,
8213                                                    (const U8*)pv1, cur1);
8214                 return retval ? retval < 0 ? -1 : +1 : 0;
8215         }
8216         else {
8217                 const int retval = bytes_cmp_utf8((const U8*)pv1, cur1,
8218                                                   (const U8*)pv2, cur2);
8219                 return retval ? retval < 0 ? -1 : +1 : 0;
8220         }
8221     }
8222
8223     /* Here, if both are non-NULL, then they have the same UTF8ness. */
8224
8225     if (!cur1) {
8226         cmp = cur2 ? -1 : 0;
8227     } else if (!cur2) {
8228         cmp = 1;
8229     } else {
8230         STRLEN shortest_len = cur1 < cur2 ? cur1 : cur2;
8231
8232 #ifdef EBCDIC
8233         if (! DO_UTF8(sv1)) {
8234 #endif
8235             const I32 retval = memcmp((const void*)pv1,
8236                                       (const void*)pv2,
8237                                       shortest_len);
8238             if (retval) {
8239                 cmp = retval < 0 ? -1 : 1;
8240             } else if (cur1 == cur2) {
8241                 cmp = 0;
8242             } else {
8243                 cmp = cur1 < cur2 ? -1 : 1;
8244             }
8245 #ifdef EBCDIC
8246         }
8247         else {  /* Both are to be treated as UTF-EBCDIC */
8248
8249             /* EBCDIC UTF-8 is complicated by the fact that it is based on I8
8250              * which remaps code points 0-255.  We therefore generally have to
8251              * unmap back to the original values to get an accurate comparison.
8252              * But we don't have to do that for UTF-8 invariants, as by
8253              * definition, they aren't remapped, nor do we have to do it for
8254              * above-latin1 code points, as they also aren't remapped.  (This
8255              * code also works on ASCII platforms, but the memcmp() above is
8256              * much faster). */
8257
8258             const char *e = pv1 + shortest_len;
8259
8260             /* Find the first bytes that differ between the two strings */
8261             while (pv1 < e && *pv1 == *pv2) {
8262                 pv1++;
8263                 pv2++;
8264             }
8265
8266
8267             if (pv1 == e) { /* Are the same all the way to the end */
8268                 if (cur1 == cur2) {
8269                     cmp = 0;
8270                 } else {
8271                     cmp = cur1 < cur2 ? -1 : 1;
8272                 }
8273             }
8274             else   /* Here *pv1 and *pv2 are not equal, but all bytes earlier
8275                     * in the strings were.  The current bytes may or may not be
8276                     * at the beginning of a character.  But neither or both are
8277                     * (or else earlier bytes would have been different).  And
8278                     * if we are in the middle of a character, the two
8279                     * characters have the same number of bytes
8280                     * (because in this case the start bytes are the same, and
8281                     * the start bytes encode the character's length). */
8282                  if (UTF8_IS_INVARIANT(*pv1))
8283             {
8284                 /* If both are invariants; can just compare directly */
8285                 if (UTF8_IS_INVARIANT(*pv2)) {
8286                     cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8287                 }
8288                 else   /* Since *pv1 is invariant, it is the whole character,
8289                           which means it is at the beginning of a character.
8290                           That means pv2 is also at the beginning of a
8291                           character (see earlier comment).  Since it isn't
8292                           invariant, it must be a start byte.  If it starts a
8293                           character whose code point is above 255, that
8294                           character is greater than any single-byte char, which
8295                           *pv1 is */
8296                       if (UTF8_IS_ABOVE_LATIN1_START(*pv2))
8297                 {
8298                     cmp = -1;
8299                 }
8300                 else {
8301                     /* Here, pv2 points to a character composed of 2 bytes
8302                      * whose code point is < 256.  Get its code point and
8303                      * compare with *pv1 */
8304                     cmp = ((U8) *pv1 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8305                            ?  -1
8306                            : 1;
8307                 }
8308             }
8309             else   /* The code point starting at pv1 isn't a single byte */
8310                  if (UTF8_IS_INVARIANT(*pv2))
8311             {
8312                 /* But here, the code point starting at *pv2 is a single byte,
8313                  * and so *pv1 must begin a character, hence is a start byte.
8314                  * If that character is above 255, it is larger than any
8315                  * single-byte char, which *pv2 is */
8316                 if (UTF8_IS_ABOVE_LATIN1_START(*pv1)) {
8317                     cmp = 1;
8318                 }
8319                 else {
8320                     /* Here, pv1 points to a character composed of 2 bytes
8321                      * whose code point is < 256.  Get its code point and
8322                      * compare with the single byte character *pv2 */
8323                     cmp = (EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1)) < (U8) *pv2)
8324                           ?  -1
8325                           : 1;
8326                 }
8327             }
8328             else   /* Here, we've ruled out either *pv1 and *pv2 being
8329                       invariant.  That means both are part of variants, but not
8330                       necessarily at the start of a character */
8331                  if (   UTF8_IS_ABOVE_LATIN1_START(*pv1)
8332                      || UTF8_IS_ABOVE_LATIN1_START(*pv2))
8333             {
8334                 /* Here, at least one is the start of a character, which means
8335                  * the other is also a start byte.  And the code point of at
8336                  * least one of the characters is above 255.  It is a
8337                  * characteristic of UTF-EBCDIC that all start bytes for
8338                  * above-latin1 code points are well behaved as far as code
8339                  * point comparisons go, and all are larger than all other
8340                  * start bytes, so the comparison with those is also well
8341                  * behaved */
8342                 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8343             }
8344             else {
8345                 /* Here both *pv1 and *pv2 are part of variant characters.
8346                  * They could be both continuations, or both start characters.
8347                  * (One or both could even be an illegal start character (for
8348                  * an overlong) which for the purposes of sorting we treat as
8349                  * legal. */
8350                 if (UTF8_IS_CONTINUATION(*pv1)) {
8351
8352                     /* If they are continuations for code points above 255,
8353                      * then comparing the current byte is sufficient, as there
8354                      * is no remapping of these and so the comparison is
8355                      * well-behaved.   We determine if they are such
8356                      * continuations by looking at the preceding byte.  It
8357                      * could be a start byte, from which we can tell if it is
8358                      * for an above 255 code point.  Or it could be a
8359                      * continuation, which means the character occupies at
8360                      * least 3 bytes, so must be above 255.  */
8361                     if (   UTF8_IS_CONTINUATION(*(pv2 - 1))
8362                         || UTF8_IS_ABOVE_LATIN1_START(*(pv2 -1)))
8363                     {
8364                         cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8365                         goto cmp_done;
8366                     }
8367
8368                     /* Here, the continuations are for code points below 256;
8369                      * back up one to get to the start byte */
8370                     pv1--;
8371                     pv2--;
8372                 }
8373
8374                 /* We need to get the actual native code point of each of these
8375                  * variants in order to compare them */
8376                 cmp =  (  EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1))
8377                         < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8378                         ? -1
8379                         : 1;
8380             }
8381         }
8382       cmp_done: ;
8383 #endif
8384     }
8385
8386     SvREFCNT_dec(svrecode);
8387
8388     return cmp;
8389 }
8390
8391 /*
8392 =for apidoc sv_cmp_locale
8393
8394 Compares the strings in two SVs in a locale-aware manner.  Is UTF-8 and
8395 S<C<'use bytes'>> aware, handles get magic, and will coerce its args to strings
8396 if necessary.  See also C<L</sv_cmp>>.
8397
8398 =for apidoc sv_cmp_locale_flags
8399
8400 Compares the strings in two SVs in a locale-aware manner.  Is UTF-8 and
8401 S<C<'use bytes'>> aware and will coerce its args to strings if necessary.  If
8402 the flags contain C<SV_GMAGIC>, it handles get magic.  See also
8403 C<L</sv_cmp_flags>>.
8404
8405 =cut
8406 */
8407
8408 I32
8409 Perl_sv_cmp_locale(pTHX_ SV *const sv1, SV *const sv2)
8410 {
8411     return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC);
8412 }
8413
8414 I32
8415 Perl_sv_cmp_locale_flags(pTHX_ SV *const sv1, SV *const sv2,
8416                          const U32 flags)
8417 {
8418 #ifdef USE_LOCALE_COLLATE
8419
8420     char *pv1, *pv2;
8421     STRLEN len1, len2;
8422     I32 retval;
8423
8424     if (PL_collation_standard)
8425         goto raw_compare;
8426
8427     len1 = len2 = 0;
8428
8429     /* Revert to using raw compare if both operands exist, but either one
8430      * doesn't transform properly for collation */
8431     if (sv1 && sv2) {
8432         pv1 = sv_collxfrm_flags(sv1, &len1, flags);
8433         if (! pv1) {
8434             goto raw_compare;
8435         }
8436         pv2 = sv_collxfrm_flags(sv2, &len2, flags);
8437         if (! pv2) {
8438             goto raw_compare;
8439         }
8440     }
8441     else {
8442         pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL;
8443         pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL;
8444     }
8445
8446     if (!pv1 || !len1) {
8447         if (pv2 && len2)
8448             return -1;
8449         else
8450             goto raw_compare;
8451     }
8452     else {
8453         if (!pv2 || !len2)
8454             return 1;
8455     }
8456
8457     retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
8458
8459     if (retval)
8460         return retval < 0 ? -1 : 1;
8461
8462     /*
8463      * When the result of collation is equality, that doesn't mean
8464      * that there are no differences -- some locales exclude some
8465      * characters from consideration.  So to avoid false equalities,
8466      * we use the raw string as a tiebreaker.
8467      */
8468
8469   raw_compare:
8470     /* FALLTHROUGH */
8471
8472 #else
8473     PERL_UNUSED_ARG(flags);
8474 #endif /* USE_LOCALE_COLLATE */
8475
8476     return sv_cmp(sv1, sv2);
8477 }
8478
8479
8480 #ifdef USE_LOCALE_COLLATE
8481
8482 /*
8483 =for apidoc sv_collxfrm
8484
8485 This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag.  See
8486 C<L</sv_collxfrm_flags>>.
8487
8488 =for apidoc sv_collxfrm_flags
8489
8490 Add Collate Transform magic to an SV if it doesn't already have it.  If the
8491 flags contain C<SV_GMAGIC>, it handles get-magic.
8492
8493 Any scalar variable may carry C<PERL_MAGIC_collxfrm> magic that contains the
8494 scalar data of the variable, but transformed to such a format that a normal
8495 memory comparison can be used to compare the data according to the locale
8496 settings.
8497
8498 =cut
8499 */
8500
8501 char *
8502 Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
8503 {
8504     MAGIC *mg;
8505
8506     PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS;
8507
8508     mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
8509
8510     /* If we don't have collation magic on 'sv', or the locale has changed
8511      * since the last time we calculated it, get it and save it now */
8512     if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
8513         const char *s;
8514         char *xf;
8515         STRLEN len, xlen;
8516
8517         /* Free the old space */
8518         if (mg)
8519             Safefree(mg->mg_ptr);
8520
8521         s = SvPV_flags_const(sv, len, flags);
8522         if ((xf = mem_collxfrm_(s, len, &xlen, cBOOL(SvUTF8(sv))))) {
8523             if (! mg) {
8524                 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
8525                                  0, 0);
8526                 assert(mg);
8527             }
8528             mg->mg_ptr = xf;
8529             mg->mg_len = xlen;
8530         }
8531         else {
8532             if (mg) {
8533                 mg->mg_ptr = NULL;
8534                 mg->mg_len = -1;
8535             }
8536         }
8537     }
8538
8539     if (mg && mg->mg_ptr) {
8540         *nxp = mg->mg_len;
8541         return mg->mg_ptr + sizeof(PL_collation_ix);
8542     }
8543     else {
8544         *nxp = 0;
8545         return NULL;
8546     }
8547 }
8548
8549 #endif /* USE_LOCALE_COLLATE */
8550
8551 static char *
8552 S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8553 {
8554     SV * const tsv = newSV_type(SVt_NULL);
8555     ENTER;
8556     SAVEFREESV(tsv);
8557     sv_gets(tsv, fp, 0);
8558     sv_utf8_upgrade_nomg(tsv);
8559     SvCUR_set(sv,append);
8560     sv_catsv(sv,tsv);
8561     LEAVE;
8562     return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8563 }
8564
8565 static char *
8566 S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8567 {
8568     SSize_t bytesread;
8569     const STRLEN recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
8570       /* Grab the size of the record we're getting */
8571     char *buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
8572
8573     /* Go yank in */
8574 #ifdef __VMS
8575     int fd;
8576     Stat_t st;
8577
8578     /* With a true, record-oriented file on VMS, we need to use read directly
8579      * to ensure that we respect RMS record boundaries.  The user is responsible
8580      * for providing a PL_rs value that corresponds to the FAB$W_MRS (maximum
8581      * record size) field.  N.B. This is likely to produce invalid results on
8582      * varying-width character data when a record ends mid-character.
8583      */
8584     fd = PerlIO_fileno(fp);
8585     if (fd != -1
8586         && PerlLIO_fstat(fd, &st) == 0
8587         && (st.st_fab_rfm == FAB$C_VAR
8588             || st.st_fab_rfm == FAB$C_VFC
8589             || st.st_fab_rfm == FAB$C_FIX)) {
8590
8591         bytesread = PerlLIO_read(fd, buffer, recsize);
8592     }
8593     else /* in-memory file from PerlIO::Scalar
8594           * or not a record-oriented file
8595           */
8596 #endif
8597     {
8598         bytesread = PerlIO_read(fp, buffer, recsize);
8599
8600         /* At this point, the logic in sv_get() means that sv will
8601            be treated as utf-8 if the handle is utf8.
8602         */
8603         if (PerlIO_isutf8(fp) && bytesread > 0) {
8604             char *bend = buffer + bytesread;
8605             char *bufp = buffer;
8606             size_t charcount = 0;
8607             bool charstart = TRUE;
8608             STRLEN skip = 0;
8609
8610             while (charcount < recsize) {
8611                 /* count accumulated characters */
8612                 while (bufp < bend) {
8613                     if (charstart) {
8614                         skip = UTF8SKIP(bufp);
8615                     }
8616                     if (bufp + skip > bend) {
8617                         /* partial at the end */
8618                         charstart = FALSE;
8619                         break;
8620                     }
8621                     else {
8622                         ++charcount;
8623                         bufp += skip;
8624                         charstart = TRUE;
8625                     }
8626                 }
8627
8628                 if (charcount < recsize) {
8629                     STRLEN readsize;
8630                     STRLEN bufp_offset = bufp - buffer;
8631                     SSize_t morebytesread;
8632
8633                     /* originally I read enough to fill any incomplete
8634                        character and the first byte of the next
8635                        character if needed, but if there's many
8636                        multi-byte encoded characters we're going to be
8637                        making a read call for every character beyond
8638                        the original read size.
8639
8640                        So instead, read the rest of the character if
8641                        any, and enough bytes to match at least the
8642                        start bytes for each character we're going to
8643                        read.
8644                     */
8645                     if (charstart)
8646                         readsize = recsize - charcount;
8647                     else
8648                         readsize = skip - (bend - bufp) + recsize - charcount - 1;
8649                     buffer = SvGROW(sv, append + bytesread + readsize + 1) + append;
8650                     bend = buffer + bytesread;
8651                     morebytesread = PerlIO_read(fp, bend, readsize);
8652                     if (morebytesread <= 0) {
8653                         /* we're done, if we still have incomplete
8654                            characters the check code in sv_gets() will
8655                            warn about them.
8656
8657                            I'd originally considered doing
8658                            PerlIO_ungetc() on all but the lead
8659                            character of the incomplete character, but
8660                            read() doesn't do that, so I don't.
8661                         */
8662                         break;
8663                     }
8664
8665                     /* prepare to scan some more */
8666                     bytesread += morebytesread;
8667                     bend = buffer + bytesread;
8668                     bufp = buffer + bufp_offset;
8669                 }
8670             }
8671         }
8672     }
8673
8674     if (bytesread < 0)
8675         bytesread = 0;
8676     SvCUR_set(sv, bytesread + append);
8677     buffer[bytesread] = '\0';
8678     return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8679 }
8680
8681 /*
8682 =for apidoc sv_gets
8683
8684 Get a line from the filehandle and store it into the SV, optionally
8685 appending to the currently-stored string.  If C<append> is not 0, the
8686 line is appended to the SV instead of overwriting it.  C<append> should
8687 be set to the byte offset that the appended string should start at
8688 in the SV (typically, C<SvCUR(sv)> is a suitable choice).
8689
8690 =cut
8691 */
8692
8693 char *
8694 Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8695 {
8696     const char *rsptr;
8697     STRLEN rslen;
8698     STDCHAR rslast;
8699     STDCHAR *bp;
8700     SSize_t cnt;
8701     int i = 0;
8702     int rspara = 0;
8703
8704     PERL_ARGS_ASSERT_SV_GETS;
8705
8706     if (SvTHINKFIRST(sv))
8707         sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
8708     /* XXX. If you make this PVIV, then copy on write can copy scalars read
8709        from <>.
8710        However, perlbench says it's slower, because the existing swipe code
8711        is faster than copy on write.
8712        Swings and roundabouts.  */
8713     SvUPGRADE(sv, SVt_PV);
8714
8715     if (append) {
8716         /* line is going to be appended to the existing buffer in the sv */
8717         if (PerlIO_isutf8(fp)) {
8718             if (!SvUTF8(sv)) {
8719                 sv_utf8_upgrade_nomg(sv);
8720                 sv_pos_u2b(sv,&append,0);
8721             }
8722         } else if (SvUTF8(sv)) {
8723             return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append);
8724         }
8725     }
8726
8727     SvPOK_only(sv);
8728     if (!append) {
8729         /* not appending - "clear" the string by setting SvCUR to 0,
8730          * the pv is still available. */
8731         SvCUR_set(sv,0);
8732     }
8733     if (PerlIO_isutf8(fp))
8734         SvUTF8_on(sv);
8735
8736     if (IN_PERL_COMPILETIME) {
8737         /* we always read code in line mode */
8738         rsptr = "\n";
8739         rslen = 1;
8740     }
8741     else if (RsSNARF(PL_rs)) {
8742         /* If it is a regular disk file use size from stat() as estimate
8743            of amount we are going to read -- may result in mallocing
8744            more memory than we really need if the layers below reduce
8745            the size we read (e.g. CRLF or a gzip layer).
8746          */
8747         Stat_t st;
8748         int fd = PerlIO_fileno(fp);
8749         if (fd >= 0 && (PerlLIO_fstat(fd, &st) == 0) && S_ISREG(st.st_mode))  {
8750             const Off_t offset = PerlIO_tell(fp);
8751             if (offset != (Off_t) -1 && st.st_size + append > offset) {
8752 #ifdef PERL_COPY_ON_WRITE
8753                 /* Add an extra byte for the sake of copy-on-write's
8754                  * buffer reference count. */
8755                 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
8756 #else
8757                 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
8758 #endif
8759             }
8760         }
8761         rsptr = NULL;
8762         rslen = 0;
8763     }
8764     else if (RsRECORD(PL_rs)) {
8765         return S_sv_gets_read_record(aTHX_ sv, fp, append);
8766     }
8767     else if (RsPARA(PL_rs)) {
8768         rsptr = "\n\n";
8769         rslen = 2;
8770         rspara = 1;
8771     }
8772     else {
8773         /* Get $/ i.e. PL_rs into same encoding as stream wants */
8774         if (PerlIO_isutf8(fp)) {
8775             rsptr = SvPVutf8(PL_rs, rslen);
8776         }
8777         else {
8778             if (SvUTF8(PL_rs)) {
8779                 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
8780                     Perl_croak(aTHX_ "Wide character in $/");
8781                 }
8782             }
8783             /* extract the raw pointer to the record separator */
8784             rsptr = SvPV_const(PL_rs, rslen);
8785         }
8786     }
8787
8788     /* rslast is the last character in the record separator
8789      * note we don't use rslast except when rslen is true, so the
8790      * null assign is a placeholder. */
8791     rslast = rslen ? rsptr[rslen - 1] : '\0';
8792
8793     if (rspara) {        /* have to do this both before and after */
8794                          /* to make sure file boundaries work right */
8795         while (1) {
8796             if (PerlIO_eof(fp))
8797                 return 0;
8798             i = PerlIO_getc(fp);
8799             if (i != '\n') {
8800                 if (i == -1)
8801                     return 0;
8802                 PerlIO_ungetc(fp,i);
8803                 break;
8804             }
8805         }
8806     }
8807
8808     /* See if we know enough about I/O mechanism to cheat it ! */
8809
8810     /* This used to be #ifdef test - it is made run-time test for ease
8811        of abstracting out stdio interface. One call should be cheap
8812        enough here - and may even be a macro allowing compile
8813        time optimization.
8814      */
8815
8816     if (PerlIO_fast_gets(fp)) {
8817     /*
8818      * We can do buffer based IO operations on this filehandle.
8819      *
8820      * This means we can bypass a lot of subcalls and process
8821      * the buffer directly, it also means we know the upper bound
8822      * on the amount of data we might read of the current buffer
8823      * into our sv. Knowing this allows us to preallocate the pv
8824      * to be able to hold that maximum, which allows us to simplify
8825      * a lot of logic. */
8826
8827     /*
8828      * We're going to steal some values from the stdio struct
8829      * and put EVERYTHING in the innermost loop into registers.
8830      */
8831     STDCHAR *ptr;       /* pointer into fp's read-ahead buffer */
8832     STRLEN bpx;         /* length of the data in the target sv
8833                            used to fix pointers after a SvGROW */
8834     I32 shortbuffered;  /* If the pv buffer is shorter than the amount
8835                            of data left in the read-ahead buffer.
8836                            If 0 then the pv buffer can hold the full
8837                            amount left, otherwise this is the amount it
8838                            can hold. */
8839
8840     /* Here is some breathtakingly efficient cheating */
8841
8842     /* When you read the following logic resist the urge to think
8843      * of record separators that are 1 byte long. They are an
8844      * uninteresting special (simple) case.
8845      *
8846      * Instead think of record separators which are at least 2 bytes
8847      * long, and keep in mind that we need to deal with such
8848      * separators when they cross a read-ahead buffer boundary.
8849      *
8850      * Also consider that we need to gracefully deal with separators
8851      * that may be longer than a single read ahead buffer.
8852      *
8853      * Lastly do not forget we want to copy the delimiter as well. We
8854      * are copying all data in the file _up_to_and_including_ the separator
8855      * itself.
8856      *
8857      * Now that you have all that in mind here is what is happening below:
8858      *
8859      * 1. When we first enter the loop we do some memory book keeping to see
8860      * how much free space there is in the target SV. (This sub assumes that
8861      * it is operating on the same SV most of the time via $_ and that it is
8862      * going to be able to reuse the same pv buffer each call.) If there is
8863      * "enough" room then we set "shortbuffered" to how much space there is
8864      * and start reading forward.
8865      *
8866      * 2. When we scan forward we copy from the read-ahead buffer to the target
8867      * SV's pv buffer. While we go we watch for the end of the read-ahead buffer,
8868      * and the end of the of pv, as well as for the "rslast", which is the last
8869      * char of the separator.
8870      *
8871      * 3. When scanning forward if we see rslast then we jump backwards in *pv*
8872      * (which has a "complete" record up to the point we saw rslast) and check
8873      * it to see if it matches the separator. If it does we are done. If it doesn't
8874      * we continue on with the scan/copy.
8875      *
8876      * 4. If we run out of read-ahead buffer (cnt goes to 0) then we have to get
8877      * the IO system to read the next buffer. We do this by doing a getc(), which
8878      * returns a single char read (or EOF), and prefills the buffer, and also
8879      * allows us to find out how full the buffer is.  We use this information to
8880      * SvGROW() the sv to the size remaining in the buffer, after which we copy
8881      * the returned single char into the target sv, and then go back into scan
8882      * forward mode.
8883      *
8884      * 5. If we run out of write-buffer then we SvGROW() it by the size of the
8885      * remaining space in the read-buffer.
8886      *
8887      * Note that this code despite its twisty-turny nature is pretty darn slick.
8888      * It manages single byte separators, multi-byte cross boundary separators,
8889      * and cross-read-buffer separators cleanly and efficiently at the cost
8890      * of potentially greatly overallocating the target SV.
8891      *
8892      * Yves
8893      */
8894
8895
8896     /* get the number of bytes remaining in the read-ahead buffer
8897      * on first call on a given fp this will return 0.*/
8898     cnt = PerlIO_get_cnt(fp);
8899
8900     /* make sure we have the room */
8901     if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
8902         /* Not room for all of it
8903            if we are looking for a separator and room for some
8904          */
8905         if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
8906             /* just process what we have room for */
8907             shortbuffered = cnt - SvLEN(sv) + append + 1;
8908             cnt -= shortbuffered;
8909         }
8910         else {
8911             /* ensure that the target sv has enough room to hold
8912              * the rest of the read-ahead buffer */
8913             shortbuffered = 0;
8914             /* remember that cnt can be negative */
8915             SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
8916         }
8917     }
8918     else {
8919         /* we have enough room to hold the full buffer, lets scream */
8920         shortbuffered = 0;
8921     }
8922
8923     /* extract the pointer to sv's string buffer, offset by append as necessary */
8924     bp = (STDCHAR*)SvPVX_const(sv) + append;  /* move these two too to registers */
8925     /* extract the point to the read-ahead buffer */
8926     ptr = (STDCHAR*)PerlIO_get_ptr(fp);
8927
8928     /* some trace debug output */
8929     DEBUG_P(PerlIO_printf(Perl_debug_log,
8930         "Screamer: entering, ptr=%" UVuf ", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
8931     DEBUG_P(PerlIO_printf(Perl_debug_log,
8932         "Screamer: entering: PerlIO * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%"
8933          UVuf "\n",
8934                PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8935                PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
8936
8937     for (;;) {
8938       screamer:
8939         /* if there is stuff left in the read-ahead buffer */
8940         if (cnt > 0) {
8941             /* if there is a separator */
8942             if (rslen) {
8943                 /* find next rslast */
8944                 STDCHAR *p;
8945
8946                 /* shortcut common case of blank line */
8947                 cnt--;
8948                 if ((*bp++ = *ptr++) == rslast)
8949                     goto thats_all_folks;
8950
8951                 p = (STDCHAR *)memchr(ptr, rslast, cnt);
8952                 if (p) {
8953                     SSize_t got = p - ptr + 1;
8954                     Copy(ptr, bp, got, STDCHAR);
8955                     ptr += got;
8956                     bp  += got;
8957                     cnt -= got;
8958                     goto thats_all_folks;
8959                 }
8960                 Copy(ptr, bp, cnt, STDCHAR);
8961                 ptr += cnt;
8962                 bp  += cnt;
8963                 cnt = 0;
8964             }
8965             else {
8966                 /* no separator, slurp the full buffer */
8967                 Copy(ptr, bp, cnt, char);            /* this     |  eat */
8968                 bp += cnt;                           /* screams  |  dust */
8969                 ptr += cnt;                          /* louder   |  sed :-) */
8970                 cnt = 0;
8971                 assert (!shortbuffered);
8972                 goto cannot_be_shortbuffered;
8973             }
8974         }
8975
8976         if (shortbuffered) {            /* oh well, must extend */
8977             /* we didn't have enough room to fit the line into the target buffer
8978              * so we must extend the target buffer and keep going */
8979             cnt = shortbuffered;
8980             shortbuffered = 0;
8981             bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8982             SvCUR_set(sv, bpx);
8983             /* extned the target sv's buffer so it can hold the full read-ahead buffer */
8984             SvGROW(sv, SvLEN(sv) + append + cnt + 2);
8985             bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8986             continue;
8987         }
8988
8989     cannot_be_shortbuffered:
8990         /* we need to refill the read-ahead buffer if possible */
8991
8992         DEBUG_P(PerlIO_printf(Perl_debug_log,
8993                              "Screamer: going to getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8994                               PTR2UV(ptr),(IV)cnt));
8995         PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
8996
8997         DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8998            "Screamer: pre: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8999             PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9000             PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9001
9002         /*
9003             call PerlIO_getc() to let it prefill the lookahead buffer
9004
9005             This used to call 'filbuf' in stdio form, but as that behaves like
9006             getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
9007             another abstraction.
9008
9009             Note we have to deal with the char in 'i' if we are not at EOF
9010         */
9011         bpx = bp - (STDCHAR*)SvPVX_const(sv);
9012         /* signals might be called here, possibly modifying sv */
9013         i   = PerlIO_getc(fp);          /* get more characters */
9014         bp = (STDCHAR*)SvPVX_const(sv) + bpx;
9015
9016         DEBUG_Pv(PerlIO_printf(Perl_debug_log,
9017            "Screamer: post: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
9018             PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9019             PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9020
9021         /* find out how much is left in the read-ahead buffer, and rextract its pointer */
9022         cnt = PerlIO_get_cnt(fp);
9023         ptr = (STDCHAR*)PerlIO_get_ptr(fp);     /* reregisterize cnt and ptr */
9024         DEBUG_P(PerlIO_printf(Perl_debug_log,
9025             "Screamer: after getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
9026             PTR2UV(ptr),(IV)cnt));
9027
9028         if (i == EOF)                   /* all done for ever? */
9029             goto thats_really_all_folks;
9030
9031         /* make sure we have enough space in the target sv */
9032         bpx = bp - (STDCHAR*)SvPVX_const(sv);   /* box up before relocation */
9033         SvCUR_set(sv, bpx);
9034         SvGROW(sv, bpx + cnt + 2);
9035         bp = (STDCHAR*)SvPVX_const(sv) + bpx;   /* unbox after relocation */
9036
9037         /* copy of the char we got from getc() */
9038         *bp++ = (STDCHAR)i;             /* store character from PerlIO_getc */
9039
9040         /* make sure we deal with the i being the last character of a separator */
9041         if (rslen && (STDCHAR)i == rslast)  /* all done for now? */
9042             goto thats_all_folks;
9043     }
9044
9045   thats_all_folks:
9046     /* check if we have actually found the separator - only really applies
9047      * when rslen > 1 */
9048     if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
9049           memNE((char*)bp - rslen, rsptr, rslen))
9050         goto screamer;                          /* go back to the fray */
9051   thats_really_all_folks:
9052     if (shortbuffered)
9053         cnt += shortbuffered;
9054     DEBUG_P(PerlIO_printf(Perl_debug_log,
9055          "Screamer: quitting, ptr=%" UVuf ", cnt=%" IVdf "\n",PTR2UV(ptr),(IV)cnt));
9056     PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt);  /* put these back or we're in trouble */
9057     DEBUG_P(PerlIO_printf(Perl_debug_log,
9058         "Screamer: end: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf
9059         "\n",
9060         PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9061         PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9062     *bp = '\0';
9063     SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv));      /* set length */
9064     DEBUG_P(PerlIO_printf(Perl_debug_log,
9065         "Screamer: done, len=%ld, string=|%.*s|\n",
9066         (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
9067     }
9068    else
9069     {
9070        /*The big, slow, and stupid way. */
9071         STDCHAR buf[8192];
9072
9073       screamer2:
9074         if (rslen) {
9075             const STDCHAR * const bpe = buf + sizeof(buf);
9076             bp = buf;
9077             while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
9078                 ; /* keep reading */
9079             cnt = bp - buf;
9080         }
9081         else {
9082             cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
9083             /* Accommodate broken VAXC compiler, which applies U8 cast to
9084              * both args of ?: operator, causing EOF to change into 255
9085              */
9086             if (cnt > 0)
9087                  i = (U8)buf[cnt - 1];
9088             else
9089                  i = EOF;
9090         }
9091
9092         if (cnt < 0)
9093             cnt = 0;  /* we do need to re-set the sv even when cnt <= 0 */
9094         if (append)
9095             sv_catpvn_nomg(sv, (char *) buf, cnt);
9096         else
9097             sv_setpvn(sv, (char *) buf, cnt);   /* "nomg" is implied */
9098
9099         if (i != EOF &&                 /* joy */
9100             (!rslen ||
9101              SvCUR(sv) < rslen ||
9102              memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
9103         {
9104             append = -1;
9105             /*
9106              * If we're reading from a TTY and we get a short read,
9107              * indicating that the user hit his EOF character, we need
9108              * to notice it now, because if we try to read from the TTY
9109              * again, the EOF condition will disappear.
9110              *
9111              * The comparison of cnt to sizeof(buf) is an optimization
9112              * that prevents unnecessary calls to feof().
9113              *
9114              * - jik 9/25/96
9115              */
9116             if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
9117                 goto screamer2;
9118         }
9119
9120     }
9121
9122     if (rspara) {               /* have to do this both before and after */
9123         while (i != EOF) {      /* to make sure file boundaries work right */
9124             i = PerlIO_getc(fp);
9125             if (i != '\n') {
9126                 PerlIO_ungetc(fp,i);
9127                 break;
9128             }
9129         }
9130     }
9131
9132     return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
9133 }
9134
9135 /*
9136 =for apidoc sv_inc
9137 =for apidoc_item sv_inc_nomg
9138
9139 These auto-increment the value in the SV, doing string to numeric conversion
9140 if necessary.  They both handle operator overloading.
9141
9142 They differ only in that C<sv_inc> performs 'get' magic; C<sv_inc_nomg> skips
9143 any magic.
9144
9145 =cut
9146 */
9147
9148 void
9149 Perl_sv_inc(pTHX_ SV *const sv)
9150 {
9151     if (!sv)
9152         return;
9153     SvGETMAGIC(sv);
9154     sv_inc_nomg(sv);
9155 }
9156
9157 void
9158 Perl_sv_inc_nomg(pTHX_ SV *const sv)
9159 {
9160     char *d;
9161     int flags;
9162
9163     if (!sv)
9164         return;
9165     if (SvTHINKFIRST(sv)) {
9166         if (SvREADONLY(sv)) {
9167                 Perl_croak_no_modify();
9168         }
9169         if (SvROK(sv)) {
9170             IV i;
9171             if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg))
9172                 return;
9173             i = PTR2IV(SvRV(sv));
9174             sv_unref(sv);
9175             sv_setiv(sv, i);
9176         }
9177         else sv_force_normal_flags(sv, 0);
9178     }
9179     flags = SvFLAGS(sv);
9180     if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
9181         /* It's (privately or publicly) a float, but not tested as an
9182            integer, so test it to see. */
9183         (void) SvIV(sv);
9184         flags = SvFLAGS(sv);
9185     }
9186     if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9187         /* It's publicly an integer, or privately an integer-not-float */
9188 #ifdef PERL_PRESERVE_IVUV
9189       oops_its_int:
9190 #endif
9191         if (SvIsUV(sv)) {
9192             if (SvUVX(sv) == UV_MAX)
9193                 sv_setnv(sv, UV_MAX_P1);
9194             else {
9195                 (void)SvIOK_only_UV(sv);
9196                 SvUV_set(sv, SvUVX(sv) + 1);
9197             }
9198         } else {
9199             if (SvIVX(sv) == IV_MAX)
9200                 sv_setuv(sv, (UV)IV_MAX + 1);
9201             else {
9202                 (void)SvIOK_only(sv);
9203                 SvIV_set(sv, SvIVX(sv) + 1);
9204             }
9205         }
9206         return;
9207     }
9208     if (flags & SVp_NOK) {
9209         const NV was = SvNVX(sv);
9210         if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9211             /* If NVX was NaN, the following comparisons return always false */
9212             UNLIKELY(was >= NV_OVERFLOWS_INTEGERS_AT ||
9213                      was < -NV_OVERFLOWS_INTEGERS_AT) &&
9214 #if defined(NAN_COMPARE_BROKEN)
9215             LIKELY(!Perl_isinfnan(was))
9216 #else
9217             LIKELY(!Perl_isinf(was))
9218 #endif
9219             ) {
9220             /* diag_listed_as: Lost precision when %s %f by 1 */
9221             Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9222                            "Lost precision when incrementing %" NVff " by 1",
9223                            was);
9224         }
9225         (void)SvNOK_only(sv);
9226         SvNV_set(sv, was + 1.0);
9227         return;
9228     }
9229
9230     /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9231     if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9232         Perl_croak_no_modify();
9233
9234     if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
9235         if ((flags & SVTYPEMASK) < SVt_PVIV)
9236             sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
9237         (void)SvIOK_only(sv);
9238         SvIV_set(sv, 1);
9239         return;
9240     }
9241     d = SvPVX(sv);
9242     while (isALPHA(*d)) d++;
9243     while (isDIGIT(*d)) d++;
9244     if (d < SvEND(sv)) {
9245         const int numtype = grok_number_flags(SvPVX_const(sv), SvCUR(sv), NULL, PERL_SCAN_TRAILING);
9246 #ifdef PERL_PRESERVE_IVUV
9247         /* Got to punt this as an integer if needs be, but we don't issue
9248            warnings. Probably ought to make the sv_iv_please() that does
9249            the conversion if possible, and silently.  */
9250         if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9251             /* Need to try really hard to see if it's an integer.
9252                9.22337203685478e+18 is an integer.
9253                but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9254                so $a="9.22337203685478e+18"; $a+0; $a++
9255                needs to be the same as $a="9.22337203685478e+18"; $a++
9256                or we go insane. */
9257
9258             (void) sv_2iv(sv);
9259             if (SvIOK(sv))
9260                 goto oops_its_int;
9261
9262             /* sv_2iv *should* have made this an NV */
9263             if (flags & SVp_NOK) {
9264                 (void)SvNOK_only(sv);
9265                 SvNV_set(sv, SvNVX(sv) + 1.0);
9266                 return;
9267             }
9268             /* I don't think we can get here. Maybe I should assert this
9269                And if we do get here I suspect that sv_setnv will croak. NWC
9270                Fall through. */
9271             DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9272                                   SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9273         }
9274 #endif /* PERL_PRESERVE_IVUV */
9275         if (!numtype && ckWARN(WARN_NUMERIC))
9276             not_incrementable(sv);
9277         sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
9278         return;
9279     }
9280     d--;
9281     while (d >= SvPVX_const(sv)) {
9282         if (isDIGIT(*d)) {
9283             if (++*d <= '9')
9284                 return;
9285             *(d--) = '0';
9286         }
9287         else {
9288 #ifdef EBCDIC
9289             /* MKS: The original code here died if letters weren't consecutive.
9290              * at least it didn't have to worry about non-C locales.  The
9291              * new code assumes that ('z'-'a')==('Z'-'A'), letters are
9292              * arranged in order (although not consecutively) and that only
9293              * [A-Za-z] are accepted by isALPHA in the C locale.
9294              */
9295             if (isALPHA_FOLD_NE(*d, 'z')) {
9296                 do { ++*d; } while (!isALPHA(*d));
9297                 return;
9298             }
9299             *(d--) -= 'z' - 'a';
9300 #else
9301             ++*d;
9302             if (isALPHA(*d))
9303                 return;
9304             *(d--) -= 'z' - 'a' + 1;
9305 #endif
9306         }
9307     }
9308     /* oh,oh, the number grew */
9309     SvGROW(sv, SvCUR(sv) + 2);
9310     SvCUR_set(sv, SvCUR(sv) + 1);
9311     for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
9312         *d = d[-1];
9313     if (isDIGIT(d[1]))
9314         *d = '1';
9315     else
9316         *d = d[1];
9317 }
9318
9319 /*
9320 =for apidoc sv_dec
9321 =for apidoc_item sv_dec_nomg
9322
9323 These auto-decrement the value in the SV, doing string to numeric conversion
9324 if necessary.  They both handle operator overloading.
9325
9326 They differ only in that:
9327
9328 C<sv_dec> handles 'get' magic; C<sv_dec_nomg> skips 'get' magic.
9329
9330 =cut
9331 */
9332
9333 void
9334 Perl_sv_dec(pTHX_ SV *const sv)
9335 {
9336     if (!sv)
9337         return;
9338     SvGETMAGIC(sv);
9339     sv_dec_nomg(sv);
9340 }
9341
9342 void
9343 Perl_sv_dec_nomg(pTHX_ SV *const sv)
9344 {
9345     int flags;
9346
9347     if (!sv)
9348         return;
9349     if (SvTHINKFIRST(sv)) {
9350         if (SvREADONLY(sv)) {
9351                 Perl_croak_no_modify();
9352         }
9353         if (SvROK(sv)) {
9354             IV i;
9355             if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg))
9356                 return;
9357             i = PTR2IV(SvRV(sv));
9358             sv_unref(sv);
9359             sv_setiv(sv, i);
9360         }
9361         else sv_force_normal_flags(sv, 0);
9362     }
9363     /* Unlike sv_inc we don't have to worry about string-never-numbers
9364        and keeping them magic. But we mustn't warn on punting */
9365     flags = SvFLAGS(sv);
9366     if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9367         /* It's publicly an integer, or privately an integer-not-float */
9368 #ifdef PERL_PRESERVE_IVUV
9369       oops_its_int:
9370 #endif
9371         if (SvIsUV(sv)) {
9372             if (SvUVX(sv) == 0) {
9373                 (void)SvIOK_only(sv);
9374                 SvIV_set(sv, -1);
9375             }
9376             else {
9377                 (void)SvIOK_only_UV(sv);
9378                 SvUV_set(sv, SvUVX(sv) - 1);
9379             }
9380         } else {
9381             if (SvIVX(sv) == IV_MIN) {
9382                 sv_setnv(sv, (NV)IV_MIN);
9383                 goto oops_its_num;
9384             }
9385             else {
9386                 (void)SvIOK_only(sv);
9387                 SvIV_set(sv, SvIVX(sv) - 1);
9388             }
9389         }
9390         return;
9391     }
9392     if (flags & SVp_NOK) {
9393     oops_its_num:
9394         {
9395             const NV was = SvNVX(sv);
9396             if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9397                 /* If NVX was NaN, these comparisons return always false */
9398                 UNLIKELY(was <= -NV_OVERFLOWS_INTEGERS_AT ||
9399                          was > NV_OVERFLOWS_INTEGERS_AT) &&
9400 #if defined(NAN_COMPARE_BROKEN)
9401                 LIKELY(!Perl_isinfnan(was))
9402 #else
9403                 LIKELY(!Perl_isinf(was))
9404 #endif
9405                 ) {
9406                 /* diag_listed_as: Lost precision when %s %f by 1 */
9407                 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9408                                "Lost precision when decrementing %" NVff " by 1",
9409                                was);
9410             }
9411             (void)SvNOK_only(sv);
9412             SvNV_set(sv, was - 1.0);
9413             return;
9414         }
9415     }
9416
9417     /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9418     if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9419         Perl_croak_no_modify();
9420
9421     if (!(flags & SVp_POK)) {
9422         if ((flags & SVTYPEMASK) < SVt_PVIV)
9423             sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
9424         SvIV_set(sv, -1);
9425         (void)SvIOK_only(sv);
9426         return;
9427     }
9428 #ifdef PERL_PRESERVE_IVUV
9429     {
9430         const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
9431         if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9432             /* Need to try really hard to see if it's an integer.
9433                9.22337203685478e+18 is an integer.
9434                but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9435                so $a="9.22337203685478e+18"; $a+0; $a--
9436                needs to be the same as $a="9.22337203685478e+18"; $a--
9437                or we go insane. */
9438
9439             (void) sv_2iv(sv);
9440             if (SvIOK(sv))
9441                 goto oops_its_int;
9442
9443             /* sv_2iv *should* have made this an NV */
9444             if (flags & SVp_NOK) {
9445                 (void)SvNOK_only(sv);
9446                 SvNV_set(sv, SvNVX(sv) - 1.0);
9447                 return;
9448             }
9449             /* I don't think we can get here. Maybe I should assert this
9450                And if we do get here I suspect that sv_setnv will croak. NWC
9451                Fall through. */
9452             DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9453                                   SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9454         }
9455     }
9456 #endif /* PERL_PRESERVE_IVUV */
9457     sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0);   /* punt */
9458 }
9459
9460 /* this define is used to eliminate a chunk of duplicated but shared logic
9461  * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be
9462  * used anywhere but here - yves
9463  */
9464 #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \
9465     STMT_START {      \
9466         SSize_t ix = ++PL_tmps_ix;              \
9467         if (UNLIKELY(ix >= PL_tmps_max))        \
9468             ix = tmps_grow_p(ix);                       \
9469         PL_tmps_stack[ix] = (AnSv); \
9470     } STMT_END
9471
9472 /*
9473 =for apidoc sv_mortalcopy
9474
9475 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
9476 The new SV is marked as mortal.  It will be destroyed "soon", either by an
9477 explicit call to C<FREETMPS>, or by an implicit call at places such as
9478 statement boundaries.  See also C<L</sv_newmortal>> and C<L</sv_2mortal>>.
9479
9480 =for apidoc sv_mortalcopy_flags
9481
9482 Like C<sv_mortalcopy>, but the extra C<flags> are passed to the
9483 C<sv_setsv_flags>.
9484
9485 =cut
9486 */
9487
9488 /* Make a string that will exist for the duration of the expression
9489  * evaluation.  Actually, it may have to last longer than that, but
9490  * hopefully we won't free it until it has been assigned to a
9491  * permanent location. */
9492
9493 SV *
9494 Perl_sv_mortalcopy_flags(pTHX_ SV *const oldstr, U32 flags)
9495 {
9496     SV *sv;
9497
9498     if (flags & SV_GMAGIC)
9499         SvGETMAGIC(oldstr); /* before new_SV, in case it dies */
9500     new_SV(sv);
9501     sv_setsv_flags(sv,oldstr,flags & ~SV_GMAGIC);
9502     PUSH_EXTEND_MORTAL__SV_C(sv);
9503     SvTEMP_on(sv);
9504     return sv;
9505 }
9506
9507 /*
9508 =for apidoc sv_newmortal
9509
9510 Creates a new null SV which is mortal.  The reference count of the SV is
9511 set to 1.  It will be destroyed "soon", either by an explicit call to
9512 C<FREETMPS>, or by an implicit call at places such as statement boundaries.
9513 See also C<L</sv_mortalcopy>> and C<L</sv_2mortal>>.
9514
9515 =cut
9516 */
9517
9518 SV *
9519 Perl_sv_newmortal(pTHX)
9520 {
9521     SV *sv;
9522
9523     new_SV(sv);
9524     SvFLAGS(sv) = SVs_TEMP;
9525     PUSH_EXTEND_MORTAL__SV_C(sv);
9526     return sv;
9527 }
9528
9529
9530 /*
9531 =for apidoc newSVpvn_flags
9532
9533 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9534 characters) into it.  The reference count for the
9535 SV is set to 1.  Note that if C<len> is zero, Perl will create a zero length
9536 string.  You are responsible for ensuring that the source string is at least
9537 C<len> bytes long.  If the C<s> argument is NULL the new SV will be undefined.
9538 Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
9539 If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before
9540 returning.  If C<SVf_UTF8> is set, C<s>
9541 is considered to be in UTF-8 and the
9542 C<SVf_UTF8> flag will be set on the new SV.
9543 C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
9544
9545     #define newSVpvn_utf8(s, len, u)                    \
9546         newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
9547
9548 =for apidoc Amnh||SVs_TEMP
9549
9550 =cut
9551 */
9552
9553 SV *
9554 Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
9555 {
9556     SV *sv;
9557
9558     /* All the flags we don't support must be zero.
9559        And we're new code so I'm going to assert this from the start.  */
9560     assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
9561     sv = newSV_type(SVt_PV);
9562     sv_setpvn_fresh(sv,s,len);
9563
9564     /* This code used to do a sv_2mortal(), however we now unroll the call to
9565      * sv_2mortal() and do what it does ourselves here.  Since we have asserted
9566      * that flags can only have the SVf_UTF8 and/or SVs_TEMP flags set above we
9567      * can use it to enable the sv flags directly (bypassing SvTEMP_on), which
9568      * in turn means we don't need to mask out the SVf_UTF8 flag below, which
9569      * means that we eliminate quite a few steps than it looks - Yves
9570      * (explaining patch by gfx) */
9571
9572     SvFLAGS(sv) |= flags;
9573
9574     if(flags & SVs_TEMP){
9575         PUSH_EXTEND_MORTAL__SV_C(sv);
9576     }
9577
9578     return sv;
9579 }
9580
9581 /*
9582 =for apidoc sv_2mortal
9583
9584 Marks an existing SV as mortal.  The SV will be destroyed "soon", either
9585 by an explicit call to C<FREETMPS>, or by an implicit call at places such as
9586 statement boundaries.  C<SvTEMP()> is turned on which means that the SV's
9587 string buffer can be "stolen" if this SV is copied.  See also
9588 C<L</sv_newmortal>> and C<L</sv_mortalcopy>>.
9589
9590 =cut
9591 */
9592
9593 SV *
9594 Perl_sv_2mortal(pTHX_ SV *const sv)
9595 {
9596     if (!sv)
9597         return sv;
9598     if (SvIMMORTAL(sv))
9599         return sv;
9600     PUSH_EXTEND_MORTAL__SV_C(sv);
9601     SvTEMP_on(sv);
9602     return sv;
9603 }
9604
9605 /*
9606 =for apidoc newSVpv
9607
9608 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9609 characters) into it.  The reference count for the
9610 SV is set to 1.  If C<len> is zero, Perl will compute the length using
9611 C<strlen()>, (which means if you use this option, that C<s> can't have embedded
9612 C<NUL> characters and has to have a terminating C<NUL> byte).
9613
9614 This function can cause reliability issues if you are likely to pass in
9615 empty strings that are not null terminated, because it will run
9616 strlen on the string and potentially run past valid memory.
9617
9618 Using L</newSVpvn> is a safer alternative for non C<NUL> terminated strings.
9619 For string literals use L</newSVpvs> instead.  This function will work fine for
9620 C<NUL> terminated strings, but if you want to avoid the if statement on whether
9621 to call C<strlen> use C<newSVpvn> instead (calling C<strlen> yourself).
9622
9623 =cut
9624 */
9625
9626 SV *
9627 Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
9628 {
9629     SV *sv = newSV_type(SVt_PV);
9630     sv_setpvn_fresh(sv, s, len || s == NULL ? len : strlen(s));
9631     return sv;
9632 }
9633
9634 /*
9635 =for apidoc newSVpvn
9636
9637 Creates a new SV and copies a string into it, which may contain C<NUL> characters
9638 (C<\0>) and other binary data.  The reference count for the SV is set to 1.
9639 Note that if C<len> is zero, Perl will create a zero length (Perl) string.  You
9640 are responsible for ensuring that the source buffer is at least
9641 C<len> bytes long.  If the C<buffer> argument is NULL the new SV will be
9642 undefined.
9643
9644 =cut
9645 */
9646
9647 SV *
9648 Perl_newSVpvn(pTHX_ const char *const buffer, const STRLEN len)
9649 {
9650     SV *sv = newSV_type(SVt_PV);
9651     sv_setpvn_fresh(sv,buffer,len);
9652     return sv;
9653 }
9654
9655 /*
9656 =for apidoc newSVhek_mortal
9657
9658 Creates a new mortal SV from the hash key structure.  It will generate
9659 scalars that point to the shared string table where possible.  Returns
9660 a new (undefined) SV if C<hek> is NULL.
9661
9662 This is more efficient than using sv_2mortal(newSVhek( ... ))
9663
9664 =cut
9665 */
9666
9667 SV *
9668 Perl_newSVhek_mortal(pTHX_ const HEK *const hek)
9669 {
9670     SV * const sv = newSVhek(hek);
9671     assert(sv);
9672     assert(!SvIMMORTAL(sv));
9673
9674     PUSH_EXTEND_MORTAL__SV_C(sv);
9675     SvTEMP_on(sv);
9676     return sv;
9677 }
9678
9679 /*
9680 =for apidoc newSVhek
9681
9682 Creates a new SV from the hash key structure.  It will generate scalars that
9683 point to the shared string table where possible.  Returns a new (undefined)
9684 SV if C<hek> is NULL.
9685
9686 =cut
9687 */
9688
9689 SV *
9690 Perl_newSVhek(pTHX_ const HEK *const hek)
9691 {
9692     if (!hek) {
9693         SV *sv;
9694
9695         new_SV(sv);
9696         return sv;
9697     }
9698
9699     if (HEK_LEN(hek) == HEf_SVKEY) {
9700         return newSVsv(*(SV**)HEK_KEY(hek));
9701     } else {
9702         const int flags = HEK_FLAGS(hek);
9703         if (flags & HVhek_WASUTF8) {
9704             /* Trouble :-)
9705                Andreas would like keys he put in as utf8 to come back as utf8
9706             */
9707             STRLEN utf8_len = HEK_LEN(hek);
9708             SV * const sv = newSV_type(SVt_PV);
9709             char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
9710             /* bytes_to_utf8() allocates a new string, which we can repurpose: */
9711             sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
9712             SvUTF8_on (sv);
9713             return sv;
9714         } else if (flags & HVhek_NOTSHARED) {
9715             /* A hash that isn't using shared hash keys has to have
9716                the flag in every key so that we know not to try to call
9717                share_hek_hek on it.  */
9718
9719             SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
9720             if (HEK_UTF8(hek))
9721                 SvUTF8_on (sv);
9722             return sv;
9723         }
9724         /* This will be overwhelmingly the most common case.  */
9725         {
9726             /* Inline most of newSVpvn_share(), because share_hek_hek() is far
9727                more efficient than sharepvn().  */
9728             SV *sv = newSV_type(SVt_PV);
9729
9730             SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
9731             SvCUR_set(sv, HEK_LEN(hek));
9732             SvLEN_set(sv, 0);
9733             SvIsCOW_on(sv);
9734             SvPOK_on(sv);
9735             if (HEK_UTF8(hek))
9736                 SvUTF8_on(sv);
9737             return sv;
9738         }
9739     }
9740 }
9741
9742 /*
9743 =for apidoc newSVpvn_share
9744
9745 Creates a new SV with its C<SvPVX_const> pointing to a shared string in the string
9746 table.  If the string does not already exist in the table, it is
9747 created first.  Turns on the C<SvIsCOW> flag (or C<READONLY>
9748 and C<FAKE> in 5.16 and earlier).  If the C<hash> parameter
9749 is non-zero, that value is used; otherwise the hash is computed.
9750 The string's hash can later be retrieved from the SV
9751 with the C<L</SvSHARED_HASH>> macro.  The idea here is
9752 that as the string table is used for shared hash keys these strings will have
9753 C<SvPVX_const == HeKEY> and hash lookup will avoid string compare.
9754
9755 =cut
9756 */
9757
9758 SV *
9759 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
9760 {
9761     SV *sv;
9762     bool is_utf8 = FALSE;
9763     const char *const orig_src = src;
9764
9765     if (len < 0) {
9766         STRLEN tmplen = -len;
9767         is_utf8 = TRUE;
9768         /* See the note in hv.c:hv_fetch() --jhi */
9769         src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9770         len = tmplen;
9771     }
9772     if (!hash)
9773         PERL_HASH(hash, src, len);
9774     sv = newSV_type(SVt_PV);
9775     /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
9776        changes here, update it there too.  */
9777     SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
9778     SvCUR_set(sv, len);
9779     SvLEN_set(sv, 0);
9780     SvIsCOW_on(sv);
9781     SvPOK_on(sv);
9782     if (is_utf8)
9783         SvUTF8_on(sv);
9784     if (src != orig_src)
9785         Safefree(src);
9786     return sv;
9787 }
9788
9789 /*
9790 =for apidoc newSVpv_share
9791
9792 Like C<newSVpvn_share>, but takes a C<NUL>-terminated string instead of a
9793 string/length pair.
9794
9795 =cut
9796 */
9797
9798 SV *
9799 Perl_newSVpv_share(pTHX_ const char *src, U32 hash)
9800 {
9801     return newSVpvn_share(src, strlen(src), hash);
9802 }
9803
9804 #if defined(MULTIPLICITY)
9805
9806 /* pTHX_ magic can't cope with varargs, so this is a no-context
9807  * version of the main function, (which may itself be aliased to us).
9808  * Don't access this version directly.
9809  */
9810
9811 SV *
9812 Perl_newSVpvf_nocontext(const char *const pat, ...)
9813 {
9814     dTHX;
9815     SV *sv;
9816     va_list args;
9817
9818     PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
9819
9820     va_start(args, pat);
9821     sv = vnewSVpvf(pat, &args);
9822     va_end(args);
9823     return sv;
9824 }
9825 #endif
9826
9827 /*
9828 =for apidoc newSVpvf
9829
9830 Creates a new SV and initializes it with the string formatted like
9831 C<sv_catpvf>.
9832
9833 =for apidoc newSVpvf_nocontext
9834 Like C<L</newSVpvf>> but does not take a thread context (C<aTHX>) parameter,
9835 so is used in situations where the caller doesn't already have the thread
9836 context.
9837
9838 =for apidoc vnewSVpvf
9839 Like C<L</newSVpvf>> but the arguments are an encapsulated argument list.
9840
9841 =cut
9842 */
9843
9844 SV *
9845 Perl_newSVpvf(pTHX_ const char *const pat, ...)
9846 {
9847     SV *sv;
9848     va_list args;
9849
9850     PERL_ARGS_ASSERT_NEWSVPVF;
9851
9852     va_start(args, pat);
9853     sv = vnewSVpvf(pat, &args);
9854     va_end(args);
9855     return sv;
9856 }
9857
9858 /* backend for newSVpvf() and newSVpvf_nocontext() */
9859
9860 SV *
9861 Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
9862 {
9863     SV *sv;
9864
9865     PERL_ARGS_ASSERT_VNEWSVPVF;
9866
9867     sv = newSV(1);
9868     SvPVCLEAR_FRESH(sv);
9869     sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, 0);
9870     return sv;
9871 }
9872
9873 /*
9874 =for apidoc newSVnv
9875
9876 Creates a new SV and copies a floating point value into it.
9877 The reference count for the SV is set to 1.
9878
9879 =cut
9880 */
9881
9882 SV *
9883 Perl_newSVnv(pTHX_ const NV n)
9884 {
9885     SV *sv = newSV_type(SVt_NV);
9886     (void)SvNOK_on(sv);
9887
9888     SvNV_set(sv, n);
9889     SvTAINT(sv);
9890
9891     return sv;
9892 }
9893
9894 /*
9895 =for apidoc newSViv
9896
9897 Creates a new SV and copies an integer into it.  The reference count for the
9898 SV is set to 1.
9899
9900 =cut
9901 */
9902
9903 SV *
9904 Perl_newSViv(pTHX_ const IV i)
9905 {
9906     SV *sv = newSV_type(SVt_IV);
9907     (void)SvIOK_on(sv);
9908
9909     SvIV_set(sv, i);
9910     SvTAINT(sv);
9911
9912     return sv;
9913 }
9914
9915 /*
9916 =for apidoc newSVuv
9917
9918 Creates a new SV and copies an unsigned integer into it.
9919 The reference count for the SV is set to 1.
9920
9921 =cut
9922 */
9923
9924 SV *
9925 Perl_newSVuv(pTHX_ const UV u)
9926 {
9927     SV *sv;
9928
9929     /* Inlining ONLY the small relevant subset of sv_setuv here
9930      * for performance. Makes a significant difference. */
9931
9932     /* Using ivs is more efficient than using uvs - see sv_setuv */
9933     if (u <= (UV)IV_MAX) {
9934         return newSViv((IV)u);
9935     }
9936
9937     new_SV(sv);
9938
9939     /* We're starting from SVt_FIRST, so provided that's
9940      * actual 0, we don't have to unset any SV type flags
9941      * to promote to SVt_IV. */
9942     STATIC_ASSERT_STMT(SVt_FIRST == 0);
9943
9944     SET_SVANY_FOR_BODYLESS_IV(sv);
9945     SvFLAGS(sv) |= SVt_IV;
9946     (void)SvIOK_on(sv);
9947     (void)SvIsUV_on(sv);
9948
9949     SvUV_set(sv, u);
9950     SvTAINT(sv);
9951
9952     return sv;
9953 }
9954
9955 /*
9956 =for apidoc newSVbool
9957
9958 Creates a new SV boolean.
9959
9960 =cut
9961 */
9962
9963 SV *
9964 Perl_newSVbool(pTHX_ bool bool_val)
9965 {
9966     PERL_ARGS_ASSERT_NEWSVBOOL;
9967     SV *sv = newSVsv(bool_val ? &PL_sv_yes : &PL_sv_no);
9968
9969     return sv;
9970 }
9971
9972 /*
9973 =for apidoc newSV_true
9974
9975 Creates a new SV that is a boolean true.
9976
9977 =cut
9978 */
9979 SV *
9980 Perl_newSV_true(pTHX)
9981 {
9982     PERL_ARGS_ASSERT_NEWSV_TRUE;
9983     SV *sv = newSVsv(&PL_sv_yes);
9984
9985     return sv;
9986 }
9987
9988 /*
9989 =for apidoc newSV_false
9990
9991 Creates a new SV that is a boolean false.
9992
9993 =cut
9994 */
9995
9996 SV *
9997 Perl_newSV_false(pTHX)
9998 {
9999     PERL_ARGS_ASSERT_NEWSV_FALSE;
10000     SV *sv = newSVsv(&PL_sv_no);
10001
10002     return sv;
10003 }
10004
10005 /* newRV_inc is the official function name to use now.
10006  * newRV_inc is in fact #defined to newRV in sv.h
10007  */
10008
10009 SV *
10010 Perl_newRV(pTHX_ SV *const sv)
10011 {
10012     PERL_ARGS_ASSERT_NEWRV;
10013
10014     return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
10015 }
10016
10017 /*
10018 =for apidoc newSVsv
10019 =for apidoc_item newSVsv_flags
10020 =for apidoc_item newSVsv_nomg
10021
10022 These create a new SV which is an exact duplicate of the original SV
10023 (using C<sv_setsv>.)
10024
10025 They differ only in that C<newSVsv> performs 'get' magic; C<newSVsv_nomg> skips
10026 any magic; and C<newSVsv_flags> allows you to explicitly set a C<flags>
10027 parameter.
10028
10029 =cut
10030 */
10031
10032 SV *
10033 Perl_newSVsv_flags(pTHX_ SV *const old, I32 flags)
10034 {
10035     SV *sv;
10036
10037     if (!old)
10038         return NULL;
10039     if (SvIS_FREED(old)) {
10040         Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
10041         return NULL;
10042     }
10043     /* Do this here, otherwise we leak the new SV if this croaks. */
10044     if (flags & SV_GMAGIC)
10045         SvGETMAGIC(old);
10046     new_SV(sv);
10047     sv_setsv_flags(sv, old, flags & ~SV_GMAGIC);
10048     return sv;
10049 }
10050
10051 /*
10052 =for apidoc sv_reset
10053
10054 Underlying implementation for the C<reset> Perl function.
10055 Note that the perl-level function is vaguely deprecated.
10056
10057 =cut
10058 */
10059
10060 void
10061 Perl_sv_reset(pTHX_ const char *s, HV *const stash)
10062 {
10063     PERL_ARGS_ASSERT_SV_RESET;
10064
10065     sv_resetpvn(*s ? s : NULL, strlen(s), stash);
10066 }
10067
10068 void
10069 Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
10070 {
10071     char todo[PERL_UCHAR_MAX+1];
10072     const char *send;
10073
10074     if (!stash || SvTYPE(stash) != SVt_PVHV)
10075         return;
10076
10077     if (!s) {           /* reset ?? searches */
10078         MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
10079         if (mg && mg->mg_len) {
10080             const U32 count = mg->mg_len / sizeof(PMOP**);
10081             PMOP **pmp = (PMOP**) mg->mg_ptr;
10082             PMOP *const *const end = pmp + count;
10083
10084             while (pmp < end) {
10085 #ifdef USE_ITHREADS
10086                 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
10087 #else
10088                 (*pmp)->op_pmflags &= ~PMf_USED;
10089 #endif
10090                 ++pmp;
10091             }
10092         }
10093         return;
10094     }
10095
10096     /* reset variables */
10097
10098     if (!HvTOTALKEYS(stash))
10099         return;
10100
10101     Zero(todo, 256, char);
10102     send = s + len;
10103     while (s < send) {
10104         I32 max;
10105         I32 i = (unsigned char)*s;
10106         if (s[1] == '-') {
10107             s += 2;
10108         }
10109         max = (unsigned char)*s++;
10110         for ( ; i <= max; i++) {
10111             todo[i] = 1;
10112         }
10113         for (i = 0; i <= (I32) HvMAX(stash); i++) {
10114             HE *entry;
10115             for (entry = HvARRAY(stash)[i];
10116                  entry;
10117                  entry = HeNEXT(entry))
10118             {
10119                 GV *gv;
10120                 SV *sv;
10121
10122                 if (!todo[(U8)*HeKEY(entry)])
10123                     continue;
10124                 gv = MUTABLE_GV(HeVAL(entry));
10125                 if (!isGV(gv))
10126                     continue;
10127                 sv = GvSV(gv);
10128                 if (sv && !SvREADONLY(sv)) {
10129                     SV_CHECK_THINKFIRST_COW_DROP(sv);
10130                     if (!isGV(sv)) {
10131                         SvOK_off(sv);
10132                         SvSETMAGIC(sv);
10133                     }
10134                 }
10135                 if (GvAV(gv)) {
10136                     av_clear(GvAV(gv));
10137                 }
10138                 if (GvHV(gv) && !HvHasNAME(GvHV(gv))) {
10139                     hv_clear(GvHV(gv));
10140                 }
10141             }
10142         }
10143     }
10144 }
10145
10146 /*
10147 =for apidoc sv_2io
10148
10149 Using various gambits, try to get an IO from an SV: the IO slot if its a
10150 GV; or the recursive result if we're an RV; or the IO slot of the symbol
10151 named after the PV if we're a string.
10152
10153 'Get' magic is ignored on the C<sv> passed in, but will be called on
10154 C<SvRV(sv)> if C<sv> is an RV.
10155
10156 =cut
10157 */
10158
10159 IO*
10160 Perl_sv_2io(pTHX_ SV *const sv)
10161 {
10162     IO* io;
10163     GV* gv;
10164
10165     PERL_ARGS_ASSERT_SV_2IO;
10166
10167     switch (SvTYPE(sv)) {
10168     case SVt_PVIO:
10169         io = MUTABLE_IO(sv);
10170         break;
10171     case SVt_PVGV:
10172     case SVt_PVLV:
10173         if (isGV_with_GP(sv)) {
10174             gv = MUTABLE_GV(sv);
10175             io = GvIO(gv);
10176             if (!io)
10177                 Perl_croak(aTHX_ "Bad filehandle: %" HEKf,
10178                                     HEKfARG(GvNAME_HEK(gv)));
10179             break;
10180         }
10181         /* FALLTHROUGH */
10182     default:
10183         if (!SvOK(sv))
10184             Perl_croak(aTHX_ PL_no_usym, "filehandle");
10185         if (SvROK(sv)) {
10186             SvGETMAGIC(SvRV(sv));
10187             return sv_2io(SvRV(sv));
10188         }
10189         gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
10190         if (gv)
10191             io = GvIO(gv);
10192         else
10193             io = 0;
10194         if (!io) {
10195             SV *newsv = sv;
10196             if (SvGMAGICAL(sv)) {
10197                 newsv = sv_newmortal();
10198                 sv_setsv_nomg(newsv, sv);
10199             }
10200             Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv));
10201         }
10202         break;
10203     }
10204     return io;
10205 }
10206
10207 /*
10208 =for apidoc sv_2cv
10209
10210 Using various gambits, try to get a CV from an SV; in addition, try if
10211 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
10212 The flags in C<lref> are passed to C<gv_fetchsv>.
10213
10214 =cut
10215 */
10216
10217 CV *
10218 Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
10219 {
10220     GV *gv = NULL;
10221     CV *cv = NULL;
10222
10223     PERL_ARGS_ASSERT_SV_2CV;
10224
10225     if (!sv) {
10226         *st = NULL;
10227         *gvp = NULL;
10228         return NULL;
10229     }
10230     switch (SvTYPE(sv)) {
10231     case SVt_PVCV:
10232         *st = CvSTASH(sv);
10233         *gvp = NULL;
10234         return MUTABLE_CV(sv);
10235     case SVt_PVHV:
10236     case SVt_PVAV:
10237         *st = NULL;
10238         *gvp = NULL;
10239         return NULL;
10240     default:
10241         SvGETMAGIC(sv);
10242         if (SvROK(sv)) {
10243             if (SvAMAGIC(sv))
10244                 sv = amagic_deref_call(sv, to_cv_amg);
10245
10246             sv = SvRV(sv);
10247             if (SvTYPE(sv) == SVt_PVCV) {
10248                 cv = MUTABLE_CV(sv);
10249                 *gvp = NULL;
10250                 *st = CvSTASH(cv);
10251                 return cv;
10252             }
10253             else if(SvGETMAGIC(sv), isGV_with_GP(sv))
10254                 gv = MUTABLE_GV(sv);
10255             else
10256                 Perl_croak(aTHX_ "Not a subroutine reference");
10257         }
10258         else if (isGV_with_GP(sv)) {
10259             gv = MUTABLE_GV(sv);
10260         }
10261         else {
10262             gv = gv_fetchsv_nomg(sv, lref, SVt_PVCV);
10263         }
10264         *gvp = gv;
10265         if (!gv) {
10266             *st = NULL;
10267             return NULL;
10268         }
10269         /* Some flags to gv_fetchsv mean don't really create the GV  */
10270         if (!isGV_with_GP(gv)) {
10271             *st = NULL;
10272             return NULL;
10273         }
10274         *st = GvESTASH(gv);
10275         if (lref & ~GV_ADDMG && !GvCVu(gv)) {
10276             /* XXX this is probably not what they think they're getting.
10277              * It has the same effect as "sub name;", i.e. just a forward
10278              * declaration! */
10279             newSTUB(gv,0);
10280         }
10281         return GvCVu(gv);
10282     }
10283 }
10284
10285 /*
10286 =for apidoc sv_true
10287
10288 Returns true if the SV has a true value by Perl's rules.
10289 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
10290 instead use an in-line version.
10291
10292 =cut
10293 */
10294
10295 I32
10296 Perl_sv_true(pTHX_ SV *const sv)
10297 {
10298     if (!sv)
10299         return 0;
10300     if (SvPOK(sv)) {
10301         const XPV* const tXpv = (XPV*)SvANY(sv);
10302         if (tXpv &&
10303                 (tXpv->xpv_cur > 1 ||
10304                 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
10305             return 1;
10306         else
10307             return 0;
10308     }
10309     else {
10310         if (SvIOK(sv))
10311             return SvIVX(sv) != 0;
10312         else {
10313             if (SvNOK(sv))
10314                 return SvNVX(sv) != 0.0;
10315             else
10316                 return sv_2bool(sv);
10317         }
10318     }
10319 }
10320
10321 /*
10322 =for apidoc sv_pvn_force
10323
10324 Get a sensible string out of the SV somehow.
10325 A private implementation of the C<SvPV_force> macro for compilers which
10326 can't cope with complex macro expressions.  Always use the macro instead.
10327
10328 =for apidoc sv_pvn_force_flags
10329
10330 Get a sensible string out of the SV somehow.
10331 If C<flags> has the C<SV_GMAGIC> bit set, will C<L</mg_get>> on C<sv> if
10332 appropriate, else not.  C<sv_pvn_force> and C<sv_pvn_force_nomg> are
10333 implemented in terms of this function.
10334 You normally want to use the various wrapper macros instead: see
10335 C<L</SvPV_force>> and C<L</SvPV_force_nomg>>.
10336
10337 =cut
10338 */
10339
10340 char *
10341 Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
10342 {
10343     PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
10344
10345     if (flags & SV_GMAGIC) SvGETMAGIC(sv);
10346     if (SvTHINKFIRST(sv) && (!SvROK(sv) || SvREADONLY(sv)))
10347         sv_force_normal_flags(sv, 0);
10348
10349     if (SvPOK(sv)) {
10350         if (lp)
10351             *lp = SvCUR(sv);
10352     }
10353     else {
10354         char *s;
10355         STRLEN len;
10356
10357         if (SvTYPE(sv) > SVt_PVLV
10358             || isGV_with_GP(sv))
10359             /* diag_listed_as: Can't coerce %s to %s in %s */
10360             Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
10361                 OP_DESC(PL_op));
10362         s = sv_2pv_flags(sv, &len, flags &~ SV_GMAGIC);
10363         if (!s) {
10364           s = (char *)"";
10365         }
10366         if (lp)
10367             *lp = len;
10368
10369         if (SvTYPE(sv) < SVt_PV ||
10370             s != SvPVX_const(sv)) {     /* Almost, but not quite, sv_setpvn() */
10371             if (SvROK(sv))
10372                 sv_unref(sv);
10373             SvUPGRADE(sv, SVt_PV);              /* Never FALSE */
10374             SvGROW(sv, len + 1);
10375             Move(s,SvPVX(sv),len,char);
10376             SvCUR_set(sv, len);
10377             SvPVX(sv)[len] = '\0';
10378         }
10379         if (!SvPOK(sv)) {
10380             SvPOK_on(sv);               /* validate pointer */
10381             SvTAINT(sv);
10382             DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
10383                                   PTR2UV(sv),SvPVX_const(sv)));
10384         }
10385     }
10386     (void)SvPOK_only_UTF8(sv);
10387     return SvPVX_mutable(sv);
10388 }
10389
10390 /*
10391 =for apidoc sv_pvbyten_force
10392
10393 The backend for the C<SvPVbytex_force> macro.  Always use the macro
10394 instead.  If the SV cannot be downgraded from UTF-8, this croaks.
10395
10396 =cut
10397 */
10398
10399 char *
10400 Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
10401 {
10402     PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
10403
10404     sv_pvn_force(sv,lp);
10405     (void)sv_utf8_downgrade(sv,0);
10406     *lp = SvCUR(sv);
10407     return SvPVX(sv);
10408 }
10409
10410 /*
10411 =for apidoc sv_pvutf8n_force
10412
10413 The backend for the C<SvPVutf8x_force> macro.  Always use the macro
10414 instead.
10415
10416 =cut
10417 */
10418
10419 char *
10420 Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
10421 {
10422     PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
10423
10424     sv_pvn_force(sv,0);
10425     sv_utf8_upgrade_nomg(sv);
10426     *lp = SvCUR(sv);
10427     return SvPVX(sv);
10428 }
10429
10430 /*
10431 =for apidoc sv_reftype
10432
10433 Returns a string describing what the SV is a reference to.
10434
10435 If ob is true and the SV is blessed, the string is the class name,
10436 otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10437
10438 =cut
10439 */
10440
10441 const char *
10442 Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
10443 {
10444     PERL_ARGS_ASSERT_SV_REFTYPE;
10445     if (ob && SvOBJECT(sv)) {
10446         return SvPV_nolen_const(sv_ref(NULL, sv, ob));
10447     }
10448     else {
10449         /* WARNING - There is code, for instance in mg.c, that assumes that
10450          * the only reason that sv_reftype(sv,0) would return a string starting
10451          * with 'L' or 'S' is that it is a LVALUE or a SCALAR.
10452          * Yes this a dodgy way to do type checking, but it saves practically reimplementing
10453          * this routine inside other subs, and it saves time.
10454          * Do not change this assumption without searching for "dodgy type check" in
10455          * the code.
10456          * - Yves */
10457         switch (SvTYPE(sv)) {
10458         case SVt_NULL:
10459         case SVt_IV:
10460         case SVt_NV:
10461         case SVt_PV:
10462         case SVt_PVIV:
10463         case SVt_PVNV:
10464         case SVt_PVMG:
10465                                 if (SvVOK(sv))
10466                                     return "VSTRING";
10467                                 if (SvROK(sv))
10468                                     return "REF";
10469                                 else
10470                                     return "SCALAR";
10471
10472         case SVt_PVLV:          return (char *)  (SvROK(sv) ? "REF"
10473                                 /* tied lvalues should appear to be
10474                                  * scalars for backwards compatibility */
10475                                 : (isALPHA_FOLD_EQ(LvTYPE(sv), 't'))
10476                                     ? "SCALAR" : "LVALUE");
10477         case SVt_PVAV:          return "ARRAY";
10478         case SVt_PVHV:          return "HASH";
10479         case SVt_PVCV:          return "CODE";
10480         case SVt_PVGV:          return (char *) (isGV_with_GP(sv)
10481                                     ? "GLOB" : "SCALAR");
10482         case SVt_PVFM:          return "FORMAT";
10483         case SVt_PVIO:          return "IO";
10484         case SVt_INVLIST:       return "INVLIST";
10485         case SVt_REGEXP:        return "REGEXP";
10486         case SVt_PVOBJ:         return "OBJECT";
10487         default:                return "UNKNOWN";
10488         }
10489     }
10490 }
10491
10492 /*
10493 =for apidoc sv_ref
10494
10495 Returns a SV describing what the SV passed in is a reference to.
10496
10497 dst can be a SV to be set to the description or NULL, in which case a
10498 mortal SV is returned.
10499
10500 If ob is true and the SV is blessed, the description is the class
10501 name, otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10502
10503 =cut
10504 */
10505
10506 SV *
10507 Perl_sv_ref(pTHX_ SV *dst, const SV *const sv, const int ob)
10508 {
10509     PERL_ARGS_ASSERT_SV_REF;
10510
10511     if (!dst)
10512         dst = sv_newmortal();
10513
10514     if (ob && SvOBJECT(sv)) {
10515         if (HvHasNAME(SvSTASH(sv)))
10516             sv_sethek(dst, HvNAME_HEK(SvSTASH(sv)));
10517         else
10518             sv_setpvs(dst, "__ANON__");
10519     }
10520     else {
10521         const char * reftype = sv_reftype(sv, 0);
10522         sv_setpv(dst, reftype);
10523     }
10524     return dst;
10525 }
10526
10527 /*
10528 =for apidoc sv_isobject
10529
10530 Returns a boolean indicating whether the SV is an RV pointing to a blessed
10531 object.  If the SV is not an RV, or if the object is not blessed, then this
10532 will return false.
10533
10534 =cut
10535 */
10536
10537 int
10538 Perl_sv_isobject(pTHX_ SV *sv)
10539 {
10540     if (!sv)
10541         return 0;
10542     SvGETMAGIC(sv);
10543     if (!SvROK(sv))
10544         return 0;
10545     sv = SvRV(sv);
10546     if (!SvOBJECT(sv))
10547         return 0;
10548     return 1;
10549 }
10550
10551 /*
10552 =for apidoc sv_isa
10553
10554 Returns a boolean indicating whether the SV is blessed into the specified
10555 class.
10556
10557 This does not check for subtypes or method overloading. Use C<sv_isa_sv> to
10558 verify an inheritance relationship in the same way as the C<isa> operator by
10559 respecting any C<isa()> method overloading; or C<sv_derived_from_sv> to test
10560 directly on the actual object type.
10561
10562 =cut
10563 */
10564
10565 int
10566 Perl_sv_isa(pTHX_ SV *sv, const char *const name)
10567 {
10568     const char *hvname;
10569
10570     PERL_ARGS_ASSERT_SV_ISA;
10571
10572     if (!sv)
10573         return 0;
10574     SvGETMAGIC(sv);
10575     if (!SvROK(sv))
10576         return 0;
10577     sv = SvRV(sv);
10578     if (!SvOBJECT(sv))
10579         return 0;
10580     hvname = HvNAME_get(SvSTASH(sv));
10581     if (!hvname)
10582         return 0;
10583
10584     return strEQ(hvname, name);
10585 }
10586
10587 /*
10588 =for apidoc newSVrv
10589
10590 Creates a new SV for the existing RV, C<rv>, to point to.  If C<rv> is not an
10591 RV then it will be upgraded to one.  If C<classname> is non-null then the new
10592 SV will be blessed in the specified package.  The new SV is returned and its
10593 reference count is 1.  The reference count 1 is owned by C<rv>. See also
10594 newRV_inc() and newRV_noinc() for creating a new RV properly.
10595
10596 =cut
10597 */
10598
10599 SV*
10600 Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
10601 {
10602     SV *sv;
10603
10604     PERL_ARGS_ASSERT_NEWSVRV;
10605
10606     new_SV(sv);
10607
10608     SV_CHECK_THINKFIRST_COW_DROP(rv);
10609
10610     if (UNLIKELY( SvTYPE(rv) >= SVt_PVMG )) {
10611         const U32 refcnt = SvREFCNT(rv);
10612         SvREFCNT(rv) = 0;
10613         sv_clear(rv);
10614         SvFLAGS(rv) = 0;
10615         SvREFCNT(rv) = refcnt;
10616
10617         sv_upgrade(rv, SVt_IV);
10618     } else if (SvROK(rv)) {
10619         SvREFCNT_dec(SvRV(rv));
10620     } else {
10621         prepare_SV_for_RV(rv);
10622     }
10623
10624     SvOK_off(rv);
10625     SvRV_set(rv, sv);
10626     SvROK_on(rv);
10627
10628     if (classname) {
10629         HV* const stash = gv_stashpv(classname, GV_ADD);
10630         (void)sv_bless(rv, stash);
10631     }
10632     return sv;
10633 }
10634
10635 SV *
10636 Perl_newSVavdefelem(pTHX_ AV *av, SSize_t ix, bool extendible)
10637 {
10638     SV * const lv = newSV_type(SVt_PVLV);
10639     PERL_ARGS_ASSERT_NEWSVAVDEFELEM;
10640     LvTYPE(lv) = 'y';
10641     sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
10642     LvTARG(lv) = SvREFCNT_inc_simple_NN(av);
10643     LvSTARGOFF(lv) = ix;
10644     LvTARGLEN(lv) = extendible ? 1 : (STRLEN)UV_MAX;
10645     return lv;
10646 }
10647
10648 /*
10649 =for apidoc sv_setref_pv
10650
10651 Copies a pointer into a new SV, optionally blessing the SV.  The C<rv>
10652 argument will be upgraded to an RV.  That RV will be modified to point to
10653 the new SV.  If the C<pv> argument is C<NULL>, then C<PL_sv_undef> will be placed
10654 into the SV.  The C<classname> argument indicates the package for the
10655 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10656 will have a reference count of 1, and the RV will be returned.
10657
10658 Do not use with other Perl types such as HV, AV, SV, CV, because those
10659 objects will become corrupted by the pointer copy process.
10660
10661 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
10662
10663 =cut
10664 */
10665
10666 SV*
10667 Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
10668 {
10669     PERL_ARGS_ASSERT_SV_SETREF_PV;
10670
10671     if (!pv) {
10672         sv_set_undef(rv);
10673         SvSETMAGIC(rv);
10674     }
10675     else
10676         sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
10677     return rv;
10678 }
10679
10680 /*
10681 =for apidoc sv_setref_iv
10682
10683 Copies an integer into a new SV, optionally blessing the SV.  The C<rv>
10684 argument will be upgraded to an RV.  That RV will be modified to point to
10685 the new SV.  The C<classname> argument indicates the package for the
10686 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10687 will have a reference count of 1, and the RV will be returned.
10688
10689 =cut
10690 */
10691
10692 SV*
10693 Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
10694 {
10695     PERL_ARGS_ASSERT_SV_SETREF_IV;
10696
10697     sv_setiv(newSVrv(rv,classname), iv);
10698     return rv;
10699 }
10700
10701 /*
10702 =for apidoc sv_setref_uv
10703
10704 Copies an unsigned integer into a new SV, optionally blessing the SV.  The C<rv>
10705 argument will be upgraded to an RV.  That RV will be modified to point to
10706 the new SV.  The C<classname> argument indicates the package for the
10707 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10708 will have a reference count of 1, and the RV will be returned.
10709
10710 =cut
10711 */
10712
10713 SV*
10714 Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
10715 {
10716     PERL_ARGS_ASSERT_SV_SETREF_UV;
10717
10718     sv_setuv(newSVrv(rv,classname), uv);
10719     return rv;
10720 }
10721
10722 /*
10723 =for apidoc sv_setref_nv
10724
10725 Copies a double into a new SV, optionally blessing the SV.  The C<rv>
10726 argument will be upgraded to an RV.  That RV will be modified to point to
10727 the new SV.  The C<classname> argument indicates the package for the
10728 blessing.  Set C<classname> to C<NULL> to avoid the blessing.  The new SV
10729 will have a reference count of 1, and the RV will be returned.
10730
10731 =cut
10732 */
10733
10734 SV*
10735 Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
10736 {
10737     PERL_ARGS_ASSERT_SV_SETREF_NV;
10738
10739     sv_setnv(newSVrv(rv,classname), nv);
10740     return rv;
10741 }
10742
10743 /*
10744 =for apidoc sv_setref_pvn
10745
10746 Copies a string into a new SV, optionally blessing the SV.  The length of the
10747 string must be specified with C<n>.  The C<rv> argument will be upgraded to
10748 an RV.  That RV will be modified to point to the new SV.  The C<classname>
10749 argument indicates the package for the blessing.  Set C<classname> to
10750 C<NULL> to avoid the blessing.  The new SV will have a reference count
10751 of 1, and the RV will be returned.
10752
10753 Note that C<sv_setref_pv> copies the pointer while this copies the string.
10754
10755 =cut
10756 */
10757
10758 SV*
10759 Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
10760                    const char *const pv, const STRLEN n)
10761 {
10762     PERL_ARGS_ASSERT_SV_SETREF_PVN;
10763
10764     sv_setpvn(newSVrv(rv,classname), pv, n);
10765     return rv;
10766 }
10767
10768 /*
10769 =for apidoc sv_bless
10770
10771 Blesses an SV into a specified package.  The SV must be an RV.  The package
10772 must be designated by its stash (see C<L</gv_stashpv>>).  The reference count
10773 of the SV is unaffected.
10774
10775 =cut
10776 */
10777
10778 SV*
10779 Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
10780 {
10781     SV *tmpRef;
10782     HV *oldstash = NULL;
10783
10784     PERL_ARGS_ASSERT_SV_BLESS;
10785
10786     SvGETMAGIC(sv);
10787     if (!SvROK(sv))
10788         Perl_croak(aTHX_ "Can't bless non-reference value");
10789     if (HvSTASH_IS_CLASS(stash))
10790         Perl_croak(aTHX_ "Attempt to bless into a class");
10791
10792     tmpRef = SvRV(sv);
10793     if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY|SVf_PROTECT)) {
10794         if (SvREADONLY(tmpRef))
10795             Perl_croak_no_modify();
10796         if (SvTYPE(tmpRef) == SVt_PVOBJ)
10797             Perl_croak(aTHX_ "Can't bless an object reference");
10798         if (SvOBJECT(tmpRef)) {
10799             oldstash = SvSTASH(tmpRef);
10800         }
10801     }
10802     SvOBJECT_on(tmpRef);
10803     SvUPGRADE(tmpRef, SVt_PVMG);
10804     SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
10805     SvREFCNT_dec(oldstash);
10806
10807     if(SvSMAGICAL(tmpRef))
10808         if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
10809             mg_set(tmpRef);
10810
10811
10812
10813     return sv;
10814 }
10815
10816 /* Downgrades a PVGV to a PVMG. If it's actually a PVLV, we leave the type
10817  * as it is after unglobbing it.
10818  */
10819
10820 PERL_STATIC_INLINE void
10821 S_sv_unglob(pTHX_ SV *const sv, U32 flags)
10822 {
10823     void *xpvmg;
10824     HV *stash;
10825     SV * const temp = flags & SV_COW_DROP_PV ? NULL : sv_newmortal();
10826
10827     PERL_ARGS_ASSERT_SV_UNGLOB;
10828
10829     assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV);
10830     SvFAKE_off(sv);
10831     if (!(flags & SV_COW_DROP_PV))
10832         gv_efullname3(temp, MUTABLE_GV(sv), "*");
10833
10834     SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
10835     if (GvGP(sv)) {
10836         if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
10837            && HvHasNAME(stash))
10838             mro_method_changed_in(stash);
10839         gp_free(MUTABLE_GV(sv));
10840     }
10841     if (GvSTASH(sv)) {
10842         sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
10843         GvSTASH(sv) = NULL;
10844     }
10845     GvMULTI_off(sv);
10846     if (GvNAME_HEK(sv)) {
10847         unshare_hek(GvNAME_HEK(sv));
10848     }
10849     isGV_with_GP_off(sv);
10850
10851     if(SvTYPE(sv) == SVt_PVGV) {
10852         /* need to keep SvANY(sv) in the right arena */
10853         xpvmg = new_XPVMG();
10854         StructCopy(SvANY(sv), xpvmg, XPVMG);
10855         del_body_by_type(SvANY(sv), SVt_PVGV);
10856         SvANY(sv) = xpvmg;
10857
10858         SvFLAGS(sv) &= ~SVTYPEMASK;
10859         SvFLAGS(sv) |= SVt_PVMG;
10860     }
10861
10862     /* Intentionally not calling any local SET magic, as this isn't so much a
10863        set operation as merely an internal storage change.  */
10864     if (flags & SV_COW_DROP_PV) SvOK_off(sv);
10865     else sv_setsv_flags(sv, temp, 0);
10866
10867     if ((const GV *)sv == PL_last_in_gv)
10868         PL_last_in_gv = NULL;
10869     else if ((const GV *)sv == PL_statgv)
10870         PL_statgv = NULL;
10871 }
10872
10873 /*
10874 =for apidoc sv_unref_flags
10875
10876 Unsets the RV status of the SV, and decrements the reference count of
10877 whatever was being referenced by the RV.  This can almost be thought of
10878 as a reversal of C<newSVrv>.  The C<cflags> argument can contain
10879 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
10880 (otherwise the decrementing is conditional on the reference count being
10881 different from one or the reference being a readonly SV).
10882 See C<L</SvROK_off>>.
10883
10884 =for apidoc Amnh||SV_IMMEDIATE_UNREF
10885
10886 =cut
10887 */
10888
10889 void
10890 Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
10891 {
10892     SV* const target = SvRV(ref);
10893
10894     PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
10895
10896     if (SvWEAKREF(ref)) {
10897         sv_del_backref(target, ref);
10898         SvWEAKREF_off(ref);
10899         SvRV_set(ref, NULL);
10900         return;
10901     }
10902     SvRV_set(ref, NULL);
10903     SvROK_off(ref);
10904     /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
10905        assigned to as BEGIN {$a = \"Foo"} will fail.  */
10906     if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
10907         SvREFCNT_dec_NN(target);
10908     else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
10909         sv_2mortal(target);     /* Schedule for freeing later */
10910 }
10911
10912 /*
10913 =for apidoc sv_untaint
10914
10915 Untaint an SV.  Use C<SvTAINTED_off> instead.
10916
10917 =cut
10918 */
10919
10920 void
10921 Perl_sv_untaint(pTHX_ SV *const sv)
10922 {
10923     PERL_ARGS_ASSERT_SV_UNTAINT;
10924     PERL_UNUSED_CONTEXT;
10925
10926     if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10927         MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10928         if (mg)
10929             mg->mg_len &= ~1;
10930     }
10931 }
10932
10933 /*
10934 =for apidoc sv_tainted
10935
10936 Test an SV for taintedness.  Use C<SvTAINTED> instead.
10937
10938 =cut
10939 */
10940
10941 bool
10942 Perl_sv_tainted(pTHX_ SV *const sv)
10943 {
10944     PERL_ARGS_ASSERT_SV_TAINTED;
10945     PERL_UNUSED_CONTEXT;
10946
10947     if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10948         const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10949         if (mg && (mg->mg_len & 1) )
10950             return TRUE;
10951     }
10952     return FALSE;
10953 }
10954
10955 #if defined(MULTIPLICITY)
10956
10957 /* pTHX_ magic can't cope with varargs, so this is a no-context
10958  * version of the main function, (which may itself be aliased to us).
10959  * Don't access this version directly.
10960  */
10961
10962 void
10963 Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
10964 {
10965     dTHX;
10966     va_list args;
10967
10968     PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
10969
10970     va_start(args, pat);
10971     sv_vsetpvf(sv, pat, &args);
10972     va_end(args);
10973 }
10974
10975 /* pTHX_ magic can't cope with varargs, so this is a no-context
10976  * version of the main function, (which may itself be aliased to us).
10977  * Don't access this version directly.
10978  */
10979
10980 void
10981 Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10982 {
10983     dTHX;
10984     va_list args;
10985
10986     PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
10987
10988     va_start(args, pat);
10989     sv_vsetpvf_mg(sv, pat, &args);
10990     va_end(args);
10991 }
10992 #endif
10993
10994 /*
10995 =for apidoc      sv_setpvf
10996 =for apidoc_item sv_setpvf_mg
10997 =for apidoc_item sv_setpvf_mg_nocontext
10998 =for apidoc_item sv_setpvf_nocontext
10999
11000 These work like C<L</sv_catpvf>> but copy the text into the SV instead of
11001 appending it.
11002
11003 The differences between these are:
11004
11005 C<sv_setpvf_mg> and C<sv_setpvf_mg_nocontext> perform 'set' magic; C<sv_setpvf>
11006 and C<sv_setpvf_nocontext> skip all magic.
11007
11008 C<sv_setpvf_nocontext> and C<sv_setpvf_mg_nocontext> do not take a thread
11009 context (C<aTHX>) parameter, so are used in situations where the caller
11010 doesn't already have the thread context.
11011
11012 =cut
11013 */
11014
11015 void
11016 Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
11017 {
11018     va_list args;
11019
11020     PERL_ARGS_ASSERT_SV_SETPVF;
11021
11022     va_start(args, pat);
11023     sv_vsetpvf(sv, pat, &args);
11024     va_end(args);
11025 }
11026
11027 /*
11028 =for apidoc sv_vsetpvf
11029 =for apidoc_item sv_vsetpvf_mg
11030
11031 These work like C<L</sv_vcatpvf>> but copy the text into the SV instead of
11032 appending it.
11033
11034 They differ only in that C<sv_vsetpvf_mg> performs 'set' magic;
11035 C<sv_vsetpvf> skips all magic.
11036
11037 They are usually used via their frontends, C<L</sv_setpvf>> and
11038 C<L</sv_setpvf_mg>>.
11039
11040 =cut
11041 */
11042
11043 void
11044 Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11045 {
11046     PERL_ARGS_ASSERT_SV_VSETPVF;
11047
11048     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11049 }
11050
11051 void
11052 Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11053 {
11054     va_list args;
11055
11056     PERL_ARGS_ASSERT_SV_SETPVF_MG;
11057
11058     va_start(args, pat);
11059     sv_vsetpvf_mg(sv, pat, &args);
11060     va_end(args);
11061 }
11062
11063 void
11064 Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11065 {
11066     PERL_ARGS_ASSERT_SV_VSETPVF_MG;
11067
11068     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11069     SvSETMAGIC(sv);
11070 }
11071
11072 #if defined(MULTIPLICITY)
11073
11074 /* pTHX_ magic can't cope with varargs, so this is a no-context
11075  * version of the main function, (which may itself be aliased to us).
11076  * Don't access this version directly.
11077  */
11078
11079 void
11080 Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
11081 {
11082     dTHX;
11083     va_list args;
11084
11085     PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
11086
11087     va_start(args, pat);
11088     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11089     va_end(args);
11090 }
11091
11092 /* pTHX_ magic can't cope with varargs, so this is a no-context
11093  * version of the main function, (which may itself be aliased to us).
11094  * Don't access this version directly.
11095  */
11096
11097 void
11098 Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
11099 {
11100     dTHX;
11101     va_list args;
11102
11103     PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
11104
11105     va_start(args, pat);
11106     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11107     SvSETMAGIC(sv);
11108     va_end(args);
11109 }
11110 #endif
11111
11112 /*
11113 =for apidoc sv_catpvf
11114 =for apidoc_item sv_catpvf_mg
11115 =for apidoc_item sv_catpvf_mg_nocontext
11116 =for apidoc_item sv_catpvf_nocontext
11117
11118 These process their arguments like C<sprintf>, and append the formatted
11119 output to an SV.  As with C<sv_vcatpvfn>, argument reordering is not supporte
11120 when called with a non-null C-style variable argument list.
11121
11122 If the appended data contains "wide" characters
11123 (including, but not limited to, SVs with a UTF-8 PV formatted with C<%s>,
11124 and characters >255 formatted with C<%c>), the original SV might get
11125 upgraded to UTF-8.
11126
11127 If the original SV was UTF-8, the pattern should be
11128 valid UTF-8; if the original SV was bytes, the pattern should be too.
11129
11130 All perform 'get' magic, but only C<sv_catpvf_mg> and C<sv_catpvf_mg_nocontext>
11131 perform 'set' magic.
11132
11133 C<sv_catpvf_nocontext> and C<sv_catpvf_mg_nocontext> do not take a thread
11134 context (C<aTHX>) parameter, so are used in situations where the caller
11135 doesn't already have the thread context.
11136
11137 =cut
11138 */
11139
11140 void
11141 Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
11142 {
11143     va_list args;
11144
11145     PERL_ARGS_ASSERT_SV_CATPVF;
11146
11147     va_start(args, pat);
11148     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11149     va_end(args);
11150 }
11151
11152 /*
11153 =for apidoc sv_vcatpvf
11154 =for apidoc_item sv_vcatpvf_mg
11155
11156 These process their arguments like C<sv_vcatpvfn> called with a non-null
11157 C-style variable argument list, and append the formatted output to C<sv>.
11158
11159 They differ only in that C<sv_vcatpvf_mg> performs 'set' magic;
11160 C<sv_vcatpvf> skips 'set' magic.
11161
11162 Both perform 'get' magic.
11163
11164 They are usually accessed via their frontends C<L</sv_catpvf>> and
11165 C<L</sv_catpvf_mg>>.
11166
11167 =cut
11168 */
11169
11170 void
11171 Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11172 {
11173     PERL_ARGS_ASSERT_SV_VCATPVF;
11174
11175     sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11176 }
11177
11178 void
11179 Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11180 {
11181     va_list args;
11182
11183     PERL_ARGS_ASSERT_SV_CATPVF_MG;
11184
11185     va_start(args, pat);
11186     sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11187     SvSETMAGIC(sv);
11188     va_end(args);
11189 }
11190
11191 void
11192 Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11193 {
11194     PERL_ARGS_ASSERT_SV_VCATPVF_MG;
11195
11196     sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11197     SvSETMAGIC(sv);
11198 }
11199
11200 /*
11201 =for apidoc sv_vsetpvfn
11202
11203 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
11204 appending it.
11205
11206 Usually used via one of its frontends L</C<sv_vsetpvf>> and
11207 L</C<sv_vsetpvf_mg>>.
11208
11209 =cut
11210 */
11211
11212 void
11213 Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11214                  va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11215 {
11216     PERL_ARGS_ASSERT_SV_VSETPVFN;
11217
11218     SvPVCLEAR(sv);
11219     sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, 0);
11220 }
11221
11222
11223 /* simplified inline Perl_sv_catpvn_nomg() when you know the SV's SvPOK */
11224
11225 PERL_STATIC_INLINE void
11226 S_sv_catpvn_simple(pTHX_ SV *const sv, const char* const buf, const STRLEN len)
11227 {
11228     STRLEN const need = len + SvCUR(sv) + 1;
11229     char *end;
11230
11231     /* can't wrap as both len and SvCUR() are allocated in
11232      * memory and together can't consume all the address space
11233      */
11234     assert(need > len);
11235
11236     assert(SvPOK(sv));
11237     SvGROW(sv, need);
11238     end = SvEND(sv);
11239     Copy(buf, end, len, char);
11240     end += len;
11241     *end = '\0';
11242     SvCUR_set(sv, need - 1);
11243 }
11244
11245
11246 /*
11247  * Warn of missing argument to sprintf. The value used in place of such
11248  * arguments should be &PL_sv_no; an undefined value would yield
11249  * inappropriate "use of uninit" warnings [perl #71000].
11250  */
11251 STATIC void
11252 S_warn_vcatpvfn_missing_argument(pTHX) {
11253     if (ckWARN(WARN_MISSING)) {
11254         Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s",
11255                 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11256     }
11257 }
11258
11259
11260 static void
11261 S_croak_overflow()
11262 {
11263     dTHX;
11264     Perl_croak(aTHX_ "Integer overflow in format string for %s",
11265                     (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn"));
11266 }
11267
11268
11269 /* Given an int i from the next arg (if args is true) or an sv from an arg
11270  * (if args is false), try to extract a STRLEN-ranged value from the arg,
11271  * with overflow checking.
11272  * Sets *neg to true if the value was negative (untouched otherwise.
11273  * Returns the absolute value.
11274  * As an extra margin of safety, it croaks if the returned value would
11275  * exceed the maximum value of a STRLEN / 4.
11276  */
11277
11278 static STRLEN
11279 S_sprintf_arg_num_val(pTHX_ va_list *const args, int i, SV *sv, bool *neg)
11280 {
11281     IV iv;
11282
11283     if (args) {
11284         iv = i;
11285         goto do_iv;
11286     }
11287
11288     if (!sv)
11289         return 0;
11290
11291     SvGETMAGIC(sv);
11292
11293     if (UNLIKELY(SvIsUV(sv))) {
11294         UV uv = SvUV_nomg(sv);
11295         if (uv > IV_MAX)
11296             S_croak_overflow();
11297         iv = uv;
11298     }
11299     else {
11300         iv = SvIV_nomg(sv);
11301       do_iv:
11302         if (iv < 0) {
11303             if (iv < -IV_MAX)
11304                 S_croak_overflow();
11305             iv = -iv;
11306             *neg = TRUE;
11307         }
11308     }
11309
11310     if (iv > (IV)(((STRLEN)~0) / 4))
11311         S_croak_overflow();
11312
11313     return (STRLEN)iv;
11314 }
11315
11316 /* Read in and return a number. Updates *pattern to point to the char
11317  * following the number. Expects the first char to 1..9.
11318  * Croaks if the number exceeds 1/4 of the maximum value of STRLEN.
11319  * This is a belt-and-braces safety measure to complement any
11320  * overflow/wrap checks done in the main body of sv_vcatpvfn_flags.
11321  * It means that e.g. on a 32-bit system the width/precision can't be more
11322  * than 1G, which seems reasonable.
11323  */
11324
11325 STATIC STRLEN
11326 S_expect_number(pTHX_ const char **const pattern)
11327 {
11328     STRLEN var;
11329
11330     PERL_ARGS_ASSERT_EXPECT_NUMBER;
11331
11332     assert(inRANGE(**pattern, '1', '9'));
11333
11334     var = *(*pattern)++ - '0';
11335     while (isDIGIT(**pattern)) {
11336         /* if var * 10 + 9 would exceed 1/4 max strlen, croak */
11337         if (var > ((((STRLEN)~0) / 4 - 9) / 10))
11338             S_croak_overflow();
11339         var = var * 10 + (*(*pattern)++ - '0');
11340     }
11341     return var;
11342 }
11343
11344 /* Implement a fast "%.0f": given a pointer to the end of a buffer (caller
11345  * ensures it's big enough), back fill it with the rounded integer part of
11346  * nv. Returns ptr to start of string, and sets *len to its length.
11347  * Returns NULL if not convertible.
11348  */
11349
11350 STATIC char *
11351 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
11352 {
11353     const int neg = nv < 0;
11354     UV uv;
11355
11356     PERL_ARGS_ASSERT_F0CONVERT;
11357
11358     assert(!Perl_isinfnan(nv));
11359     if (neg)
11360         nv = -nv;
11361     if (nv != 0.0 && nv < (NV) UV_MAX) {
11362         char *p = endbuf;
11363         uv = (UV)nv;
11364         if (uv != nv) {
11365             nv += 0.5;
11366             uv = (UV)nv;
11367             if (uv & 1 && uv == nv)
11368                 uv--;                   /* Round to even */
11369         }
11370         do {
11371             const unsigned dig = uv % 10;
11372             *--p = '0' + dig;
11373         } while (uv /= 10);
11374         if (neg)
11375             *--p = '-';
11376         *len = endbuf - p;
11377         return p;
11378     }
11379     return NULL;
11380 }
11381
11382
11383 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
11384
11385 void
11386 Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11387                  va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11388 {
11389     PERL_ARGS_ASSERT_SV_VCATPVFN;
11390
11391     sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, SV_GMAGIC|SV_SMAGIC);
11392 }
11393
11394
11395 /* For the vcatpvfn code, we need a long double target in case
11396  * HAS_LONG_DOUBLE, even without USE_LONG_DOUBLE, so that we can printf
11397  * with long double formats, even without NV being long double.  But we
11398  * call the target 'fv' instead of 'nv', since most of the time it is not
11399  * (most compilers these days recognize "long double", even if only as a
11400  * synonym for "double").
11401 */
11402 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE && \
11403         defined(PERL_PRIgldbl) && !defined(USE_QUADMATH)
11404 #  define VCATPVFN_FV_GF PERL_PRIgldbl
11405 #  if defined(__VMS) && defined(__ia64) && defined(__IEEE_FLOAT)
11406        /* Work around breakage in OTS$CVT_FLOAT_T_X */
11407 #    define VCATPVFN_NV_TO_FV(nv,fv)                    \
11408             STMT_START {                                \
11409                 double _dv = nv;                        \
11410                 fv = Perl_isnan(_dv) ? LDBL_QNAN : _dv; \
11411             } STMT_END
11412 #  else
11413 #    define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11414 #  endif
11415    typedef long double vcatpvfn_long_double_t;
11416 #else
11417 #  define VCATPVFN_FV_GF NVgf
11418 #  define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11419    typedef NV vcatpvfn_long_double_t;
11420 #endif
11421
11422 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11423 /* The first double can be as large as 2**1023, or '1' x '0' x 1023.
11424  * The second double can be as small as 2**-1074, or '0' x 1073 . '1'.
11425  * The sum of them can be '1' . '0' x 2096 . '1', with implied radix point
11426  * after the first 1023 zero bits.
11427  *
11428  * XXX The 2098 is quite large (262.25 bytes) and therefore some sort
11429  * of dynamically growing buffer might be better, start at just 16 bytes
11430  * (for example) and grow only when necessary.  Or maybe just by looking
11431  * at the exponents of the two doubles? */
11432 #  define DOUBLEDOUBLE_MAXBITS 2098
11433 #endif
11434
11435 /* vhex will contain the values (0..15) of the hex digits ("nybbles"
11436  * of 4 bits); 1 for the implicit 1, and the mantissa bits, four bits
11437  * per xdigit.  For the double-double case, this can be rather many.
11438  * The non-double-double-long-double overshoots since all bits of NV
11439  * are not mantissa bits, there are also exponent bits. */
11440 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11441 #  define VHEX_SIZE (3+DOUBLEDOUBLE_MAXBITS/4)
11442 #else
11443 #  define VHEX_SIZE (1+(NVSIZE * 8)/4)
11444 #endif
11445
11446 /* If we do not have a known long double format, (including not using
11447  * long doubles, or long doubles being equal to doubles) then we will
11448  * fall back to the ldexp/frexp route, with which we can retrieve at
11449  * most as many bits as our widest unsigned integer type is.  We try
11450  * to get a 64-bit unsigned integer even if we are not using a 64-bit UV.
11451  *
11452  * (If you want to test the case of UVSIZE == 4, NVSIZE == 8,
11453  *  set the MANTISSATYPE to int and the MANTISSASIZE to 4.)
11454  */
11455 #if defined(HAS_QUAD) && defined(Uquad_t)
11456 #  define MANTISSATYPE Uquad_t
11457 #  define MANTISSASIZE 8
11458 #else
11459 #  define MANTISSATYPE UV
11460 #  define MANTISSASIZE UVSIZE
11461 #endif
11462
11463 #if defined(DOUBLE_LITTLE_ENDIAN) || defined(LONGDOUBLE_LITTLE_ENDIAN)
11464 #  define HEXTRACT_LITTLE_ENDIAN
11465 #elif defined(DOUBLE_BIG_ENDIAN) || defined(LONGDOUBLE_BIG_ENDIAN)
11466 #  define HEXTRACT_BIG_ENDIAN
11467 #else
11468 #  define HEXTRACT_MIX_ENDIAN
11469 #endif
11470
11471 /* S_hextract() is a helper for S_format_hexfp, for extracting
11472  * the hexadecimal values (for %a/%A).  The nv is the NV where the value
11473  * are being extracted from (either directly from the long double in-memory
11474  * presentation, or from the uquad computed via frexp+ldexp).  frexp also
11475  * is used to update the exponent.  The subnormal is set to true
11476  * for IEEE 754 subnormals/denormals (including the x86 80-bit format).
11477  * The vhex is the pointer to the beginning of the output buffer of VHEX_SIZE.
11478  *
11479  * The tricky part is that S_hextract() needs to be called twice:
11480  * the first time with vend as NULL, and the second time with vend as
11481  * the pointer returned by the first call.  What happens is that on
11482  * the first round the output size is computed, and the intended
11483  * extraction sanity checked.  On the second round the actual output
11484  * (the extraction of the hexadecimal values) takes place.
11485  * Sanity failures cause fatal failures during both rounds. */
11486 STATIC U8*
11487 S_hextract(pTHX_ const NV nv, int* exponent, bool *subnormal,
11488            U8* vhex, U8* vend)
11489 {
11490     U8* v = vhex;
11491     int ix;
11492     int ixmin = 0, ixmax = 0;
11493
11494     /* XXX Inf/NaN are not handled here, since it is
11495      * assumed they are to be output as "Inf" and "NaN". */
11496
11497     /* These macros are just to reduce typos, they have multiple
11498      * repetitions below, but usually only one (or sometimes two)
11499      * of them is really being used. */
11500     /* HEXTRACT_OUTPUT() extracts the high nybble first. */
11501 #define HEXTRACT_OUTPUT_HI(ix) (*v++ = nvp[ix] >> 4)
11502 #define HEXTRACT_OUTPUT_LO(ix) (*v++ = nvp[ix] & 0xF)
11503 #define HEXTRACT_OUTPUT(ix) \
11504     STMT_START { \
11505       HEXTRACT_OUTPUT_HI(ix); HEXTRACT_OUTPUT_LO(ix); \
11506    } STMT_END
11507 #define HEXTRACT_COUNT(ix, c) \
11508     STMT_START { \
11509       v += c; if (ix < ixmin) ixmin = ix; else if (ix > ixmax) ixmax = ix; \
11510    } STMT_END
11511 #define HEXTRACT_BYTE(ix) \
11512     STMT_START { \
11513       if (vend) HEXTRACT_OUTPUT(ix); else HEXTRACT_COUNT(ix, 2); \
11514    } STMT_END
11515 #define HEXTRACT_LO_NYBBLE(ix) \
11516     STMT_START { \
11517       if (vend) HEXTRACT_OUTPUT_LO(ix); else HEXTRACT_COUNT(ix, 1); \
11518    } STMT_END
11519     /* HEXTRACT_TOP_NYBBLE is just convenience disguise,
11520      * to make it look less odd when the top bits of a NV
11521      * are extracted using HEXTRACT_LO_NYBBLE: the highest
11522      * order bits can be in the "low nybble" of a byte. */
11523 #define HEXTRACT_TOP_NYBBLE(ix) HEXTRACT_LO_NYBBLE(ix)
11524 #define HEXTRACT_BYTES_LE(a, b) \
11525     for (ix = a; ix >= b; ix--) { HEXTRACT_BYTE(ix); }
11526 #define HEXTRACT_BYTES_BE(a, b) \
11527     for (ix = a; ix <= b; ix++) { HEXTRACT_BYTE(ix); }
11528 #define HEXTRACT_GET_SUBNORMAL(nv) *subnormal = Perl_fp_class_denorm(nv)
11529 #define HEXTRACT_IMPLICIT_BIT(nv) \
11530     STMT_START { \
11531         if (!*subnormal) { \
11532             if (vend) *v++ = ((nv) == 0.0) ? 0 : 1; else v++; \
11533         } \
11534    } STMT_END
11535
11536 /* Most formats do.  Those which don't should undef this.
11537  *
11538  * But also note that IEEE 754 subnormals do not have it, or,
11539  * expressed alternatively, their implicit bit is zero. */
11540 #define HEXTRACT_HAS_IMPLICIT_BIT
11541
11542 /* Many formats do.  Those which don't should undef this. */
11543 #define HEXTRACT_HAS_TOP_NYBBLE
11544
11545     /* HEXTRACTSIZE is the maximum number of xdigits. */
11546 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
11547 #  define HEXTRACTSIZE (2+DOUBLEDOUBLE_MAXBITS/4)
11548 #else
11549 #  define HEXTRACTSIZE 2 * NVSIZE
11550 #endif
11551
11552     const U8* vmaxend = vhex + HEXTRACTSIZE;
11553
11554     assert(HEXTRACTSIZE <= VHEX_SIZE);
11555
11556     PERL_UNUSED_VAR(ix); /* might happen */
11557     (void)Perl_frexp(PERL_ABS(nv), exponent);
11558     *subnormal = FALSE;
11559     if (vend && (vend <= vhex || vend > vmaxend)) {
11560         /* diag_listed_as: Hexadecimal float: internal error (%s) */
11561         Perl_croak(aTHX_ "Hexadecimal float: internal error (entry)");
11562     }
11563     {
11564         /* First check if using long doubles. */
11565 #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE)
11566 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
11567         /* Used in e.g. VMS and HP-UX IA-64, e.g. -0.1L:
11568          * 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb bf */
11569         /* The bytes 13..0 are the mantissa/fraction,
11570          * the 15,14 are the sign+exponent. */
11571         const U8* nvp = (const U8*)(&nv);
11572         HEXTRACT_GET_SUBNORMAL(nv);
11573         HEXTRACT_IMPLICIT_BIT(nv);
11574 #    undef HEXTRACT_HAS_TOP_NYBBLE
11575         HEXTRACT_BYTES_LE(13, 0);
11576 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
11577         /* Used in e.g. Solaris Sparc and HP-UX PA-RISC, e.g. -0.1L:
11578          * bf fb 99 99 99 99 99 99 99 99 99 99 99 99 99 9a */
11579         /* The bytes 2..15 are the mantissa/fraction,
11580          * the 0,1 are the sign+exponent. */
11581         const U8* nvp = (const U8*)(&nv);
11582         HEXTRACT_GET_SUBNORMAL(nv);
11583         HEXTRACT_IMPLICIT_BIT(nv);
11584 #    undef HEXTRACT_HAS_TOP_NYBBLE
11585         HEXTRACT_BYTES_BE(2, 15);
11586 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
11587         /* x86 80-bit "extended precision", 64 bits of mantissa / fraction /
11588          * significand, 15 bits of exponent, 1 bit of sign.  No implicit bit.
11589          * NVSIZE can be either 12 (ILP32, Solaris x86) or 16 (LP64, Linux
11590          * and OS X), meaning that 2 or 6 bytes are empty padding. */
11591         /* The bytes 0..1 are the sign+exponent,
11592          * the bytes 2..9 are the mantissa/fraction. */
11593         const U8* nvp = (const U8*)(&nv);
11594 #    undef HEXTRACT_HAS_IMPLICIT_BIT
11595 #    undef HEXTRACT_HAS_TOP_NYBBLE
11596         HEXTRACT_GET_SUBNORMAL(nv);
11597         HEXTRACT_BYTES_LE(7, 0);
11598 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
11599         /* Does this format ever happen? (Wikipedia says the Motorola
11600          * 6888x math coprocessors used format _like_ this but padded
11601          * to 96 bits with 16 unused bits between the exponent and the
11602          * mantissa.) */
11603         const U8* nvp = (const U8*)(&nv);
11604 #    undef HEXTRACT_HAS_IMPLICIT_BIT
11605 #    undef HEXTRACT_HAS_TOP_NYBBLE
11606         HEXTRACT_GET_SUBNORMAL(nv);
11607         HEXTRACT_BYTES_BE(0, 7);
11608 #  else
11609 #    define HEXTRACT_FALLBACK
11610         /* Double-double format: two doubles next to each other.
11611          * The first double is the high-order one, exactly like
11612          * it would be for a "lone" double.  The second double
11613          * is shifted down using the exponent so that that there
11614          * are no common bits.  The tricky part is that the value
11615          * of the double-double is the SUM of the two doubles and
11616          * the second one can be also NEGATIVE.
11617          *
11618          * Because of this tricky construction the bytewise extraction we
11619          * use for the other long double formats doesn't work, we must
11620          * extract the values bit by bit.
11621          *
11622          * The little-endian double-double is used .. somewhere?
11623          *
11624          * The big endian double-double is used in e.g. PPC/Power (AIX)
11625          * and MIPS (SGI).
11626          *
11627          * The mantissa bits are in two separate stretches, e.g. for -0.1L:
11628          * 9a 99 99 99 99 99 59 bc 9a 99 99 99 99 99 b9 3f (LE)
11629          * 3f b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE)
11630          */
11631 #  endif
11632 #else /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) */
11633         /* Using normal doubles, not long doubles.
11634          *
11635          * We generate 4-bit xdigits (nybble/nibble) instead of 8-bit
11636          * bytes, since we might need to handle printf precision, and
11637          * also need to insert the radix. */
11638 #  if NVSIZE == 8
11639 #    ifdef HEXTRACT_LITTLE_ENDIAN
11640         /* 0 1 2 3 4 5 6 7 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11641         const U8* nvp = (const U8*)(&nv);
11642         HEXTRACT_GET_SUBNORMAL(nv);
11643         HEXTRACT_IMPLICIT_BIT(nv);
11644         HEXTRACT_TOP_NYBBLE(6);
11645         HEXTRACT_BYTES_LE(5, 0);
11646 #    elif defined(HEXTRACT_BIG_ENDIAN)
11647         /* 7 6 5 4 3 2 1 0 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11648         const U8* nvp = (const U8*)(&nv);
11649         HEXTRACT_GET_SUBNORMAL(nv);
11650         HEXTRACT_IMPLICIT_BIT(nv);
11651         HEXTRACT_TOP_NYBBLE(1);
11652         HEXTRACT_BYTES_BE(2, 7);
11653 #    elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
11654         /* 4 5 6 7 0 1 2 3 (MSB = 7, LSB = 0, 6:7 = nybble:exponent:sign) */
11655         const U8* nvp = (const U8*)(&nv);
11656         HEXTRACT_GET_SUBNORMAL(nv);
11657         HEXTRACT_IMPLICIT_BIT(nv);
11658         HEXTRACT_TOP_NYBBLE(2); /* 6 */
11659         HEXTRACT_BYTE(1); /* 5 */
11660         HEXTRACT_BYTE(0); /* 4 */
11661         HEXTRACT_BYTE(7); /* 3 */
11662         HEXTRACT_BYTE(6); /* 2 */
11663         HEXTRACT_BYTE(5); /* 1 */
11664         HEXTRACT_BYTE(4); /* 0 */
11665 #    elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
11666         /* 3 2 1 0 7 6 5 4 (MSB = 7, LSB = 0, 7:6 = sign:exponent:nybble) */
11667         const U8* nvp = (const U8*)(&nv);
11668         HEXTRACT_GET_SUBNORMAL(nv);
11669         HEXTRACT_IMPLICIT_BIT(nv);
11670         HEXTRACT_TOP_NYBBLE(5); /* 6 */
11671         HEXTRACT_BYTE(6); /* 5 */
11672         HEXTRACT_BYTE(7); /* 4 */
11673         HEXTRACT_BYTE(0); /* 3 */
11674         HEXTRACT_BYTE(1); /* 2 */
11675         HEXTRACT_BYTE(2); /* 1 */
11676         HEXTRACT_BYTE(3); /* 0 */
11677 #    else
11678 #      define HEXTRACT_FALLBACK
11679 #    endif
11680 #  else
11681 #    define HEXTRACT_FALLBACK
11682 #  endif
11683 #endif /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) #else */
11684
11685 #ifdef HEXTRACT_FALLBACK
11686         HEXTRACT_GET_SUBNORMAL(nv);
11687 #  undef HEXTRACT_HAS_TOP_NYBBLE /* Meaningless, but consistent. */
11688         /* The fallback is used for the double-double format, and
11689          * for unknown long double formats, and for unknown double
11690          * formats, or in general unknown NV formats. */
11691         if (nv == (NV)0.0) {
11692             if (vend)
11693                 *v++ = 0;
11694             else
11695                 v++;
11696             *exponent = 0;
11697         }
11698         else {
11699             NV d = nv < 0 ? -nv : nv;
11700             NV e = (NV)1.0;
11701             U8 ha = 0x0; /* hexvalue accumulator */
11702             U8 hd = 0x8; /* hexvalue digit */
11703
11704             /* Shift d and e (and update exponent) so that e <= d < 2*e,
11705              * this is essentially manual frexp(). Multiplying by 0.5 and
11706              * doubling should be lossless in binary floating point. */
11707
11708             *exponent = 1;
11709
11710             while (e > d) {
11711                 e *= (NV)0.5;
11712                 (*exponent)--;
11713             }
11714             /* Now d >= e */
11715
11716             while (d >= e + e) {
11717                 e += e;
11718                 (*exponent)++;
11719             }
11720             /* Now e <= d < 2*e */
11721
11722             /* First extract the leading hexdigit (the implicit bit). */
11723             if (d >= e) {
11724                 d -= e;
11725                 if (vend)
11726                     *v++ = 1;
11727                 else
11728                     v++;
11729             }
11730             else {
11731                 if (vend)
11732                     *v++ = 0;
11733                 else
11734                     v++;
11735             }
11736             e *= (NV)0.5;
11737
11738             /* Then extract the remaining hexdigits. */
11739             while (d > (NV)0.0) {
11740                 if (d >= e) {
11741                     ha |= hd;
11742                     d -= e;
11743                 }
11744                 if (hd == 1) {
11745                     /* Output or count in groups of four bits,
11746                      * that is, when the hexdigit is down to one. */
11747                     if (vend)
11748                         *v++ = ha;
11749                     else
11750                         v++;
11751                     /* Reset the hexvalue. */
11752                     ha = 0x0;
11753                     hd = 0x8;
11754                 }
11755                 else
11756                     hd >>= 1;
11757                 e *= (NV)0.5;
11758             }
11759
11760             /* Flush possible pending hexvalue. */
11761             if (ha) {
11762                 if (vend)
11763                     *v++ = ha;
11764                 else
11765                     v++;
11766             }
11767         }
11768 #endif
11769     }
11770     /* Croak for various reasons: if the output pointer escaped the
11771      * output buffer, if the extraction index escaped the extraction
11772      * buffer, or if the ending output pointer didn't match the
11773      * previously computed value. */
11774     if (v <= vhex || v - vhex >= VHEX_SIZE ||
11775         /* For double-double the ixmin and ixmax stay at zero,
11776          * which is convenient since the HEXTRACTSIZE is tricky
11777          * for double-double. */
11778         ixmin < 0 || ixmax >= NVSIZE ||
11779         (vend && v != vend)) {
11780         /* diag_listed_as: Hexadecimal float: internal error (%s) */
11781         Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11782     }
11783     return v;
11784 }
11785
11786
11787 /* S_format_hexfp(): helper function for Perl_sv_vcatpvfn_flags().
11788  *
11789  * Processes the %a/%A hexadecimal floating-point format, since the
11790  * built-in snprintf()s which are used for most of the f/p formats, don't
11791  * universally handle %a/%A.
11792  * Populates buf of length bufsize, and returns the length of the created
11793  * string.
11794  * The rest of the args have the same meaning as the local vars of the
11795  * same name within Perl_sv_vcatpvfn_flags().
11796  *
11797  * The caller's determination of IN_LC(LC_NUMERIC), passed as in_lc_numeric,
11798  * is used to ensure we do the right thing when we need to access the locale's
11799  * numeric radix.
11800  *
11801  * It requires the caller to make buf large enough.
11802  */
11803
11804 static STRLEN
11805 S_format_hexfp(pTHX_ char * const buf, const STRLEN bufsize, const char c,
11806                     const NV nv, const vcatpvfn_long_double_t fv,
11807                     bool has_precis, STRLEN precis, STRLEN width,
11808                     bool alt, char plus, bool left, bool fill, bool in_lc_numeric)
11809 {
11810     /* Hexadecimal floating point. */
11811     char* p = buf;
11812     U8 vhex[VHEX_SIZE];
11813     U8* v = vhex; /* working pointer to vhex */
11814     U8* vend; /* pointer to one beyond last digit of vhex */
11815     U8* vfnz = NULL; /* first non-zero */
11816     U8* vlnz = NULL; /* last non-zero */
11817     U8* v0 = NULL; /* first output */
11818     const bool lower = (c == 'a');
11819     /* At output the values of vhex (up to vend) will
11820      * be mapped through the xdig to get the actual
11821      * human-readable xdigits. */
11822     const char* xdig = PL_hexdigit;
11823     STRLEN zerotail = 0; /* how many extra zeros to append */
11824     int exponent = 0; /* exponent of the floating point input */
11825     bool hexradix = FALSE; /* should we output the radix */
11826     bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
11827     bool negative = FALSE;
11828     STRLEN elen;
11829
11830     /* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
11831      *
11832      * For example with denormals, (assuming the vanilla
11833      * 64-bit double): the exponent is zero. 1xp-1074 is
11834      * the smallest denormal and the smallest double, it
11835      * could be output also as 0x0.0000000000001p-1022 to
11836      * match its internal structure. */
11837
11838     vend = S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, NULL);
11839     S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, vend);
11840
11841 #if NVSIZE > DOUBLESIZE
11842 #  ifdef HEXTRACT_HAS_IMPLICIT_BIT
11843     /* In this case there is an implicit bit,
11844      * and therefore the exponent is shifted by one. */
11845     exponent--;
11846 #  elif defined(NV_X86_80_BIT)
11847     if (subnormal) {
11848         /* The subnormals of the x86-80 have a base exponent of -16382,
11849          * (while the physical exponent bits are zero) but the frexp()
11850          * returned the scientific-style floating exponent.  We want
11851          * to map the last one as:
11852          * -16831..-16384 -> -16382 (the last normal is 0x1p-16382)
11853          * -16835..-16388 -> -16384
11854          * since we want to keep the first hexdigit
11855          * as one of the [8421]. */
11856         exponent = -4 * ( (exponent + 1) / -4) - 2;
11857     } else {
11858         exponent -= 4;
11859     }
11860     /* TBD: other non-implicit-bit platforms than the x86-80. */
11861 #  endif
11862 #endif
11863
11864     negative = fv < 0 || Perl_signbit(nv);
11865     if (negative)
11866         *p++ = '-';
11867     else if (plus)
11868         *p++ = plus;
11869     *p++ = '0';
11870     if (lower) {
11871         *p++ = 'x';
11872     }
11873     else {
11874         *p++ = 'X';
11875         xdig += 16; /* Use uppercase hex. */
11876     }
11877
11878     /* Find the first non-zero xdigit. */
11879     for (v = vhex; v < vend; v++) {
11880         if (*v) {
11881             vfnz = v;
11882             break;
11883         }
11884     }
11885
11886     if (vfnz) {
11887         /* Find the last non-zero xdigit. */
11888         for (v = vend - 1; v >= vhex; v--) {
11889             if (*v) {
11890                 vlnz = v;
11891                 break;
11892             }
11893         }
11894
11895 #if NVSIZE == DOUBLESIZE
11896         if (fv != 0.0)
11897             exponent--;
11898 #endif
11899
11900         if (subnormal) {
11901 #ifndef NV_X86_80_BIT
11902           if (vfnz[0] > 1) {
11903             /* IEEE 754 subnormals (but not the x86 80-bit):
11904              * we want "normalize" the subnormal,
11905              * so we need to right shift the hex nybbles
11906              * so that the output of the subnormal starts
11907              * from the first true bit.  (Another, equally
11908              * valid, policy would be to dump the subnormal
11909              * nybbles as-is, to display the "physical" layout.) */
11910             int i, n;
11911             U8 *vshr;
11912             /* Find the ceil(log2(v[0])) of
11913              * the top non-zero nybble. */
11914             for (i = vfnz[0], n = 0; i > 1; i >>= 1, n++) { }
11915             assert(n < 4);
11916             assert(vlnz);
11917             vlnz[1] = 0;
11918             for (vshr = vlnz; vshr >= vfnz; vshr--) {
11919               vshr[1] |= (vshr[0] & (0xF >> (4 - n))) << (4 - n);
11920               vshr[0] >>= n;
11921             }
11922             if (vlnz[1]) {
11923               vlnz++;
11924             }
11925           }
11926 #endif
11927           v0 = vfnz;
11928         } else {
11929           v0 = vhex;
11930         }
11931
11932         if (has_precis) {
11933             U8* ve = (subnormal ? vlnz + 1 : vend);
11934             SSize_t vn = ve - v0;
11935             assert(vn >= 1);
11936             if (precis < (Size_t)(vn - 1)) {
11937                 bool overflow = FALSE;
11938                 if (v0[precis + 1] < 0x8) {
11939                     /* Round down, nothing to do. */
11940                 } else if (v0[precis + 1] > 0x8) {
11941                     /* Round up. */
11942                     v0[precis]++;
11943                     overflow = v0[precis] > 0xF;
11944                     v0[precis] &= 0xF;
11945                 } else { /* v0[precis] == 0x8 */
11946                     /* Half-point: round towards the one
11947                      * with the even least-significant digit:
11948                      * 08 -> 0  88 -> 8
11949                      * 18 -> 2  98 -> a
11950                      * 28 -> 2  a8 -> a
11951                      * 38 -> 4  b8 -> c
11952                      * 48 -> 4  c8 -> c
11953                      * 58 -> 6  d8 -> e
11954                      * 68 -> 6  e8 -> e
11955                      * 78 -> 8  f8 -> 10 */
11956                     if ((v0[precis] & 0x1)) {
11957                         v0[precis]++;
11958                     }
11959                     overflow = v0[precis] > 0xF;
11960                     v0[precis] &= 0xF;
11961                 }
11962
11963                 if (overflow) {
11964                     for (v = v0 + precis - 1; v >= v0; v--) {
11965                         (*v)++;
11966                         overflow = *v > 0xF;
11967                         (*v) &= 0xF;
11968                         if (!overflow) {
11969                             break;
11970                         }
11971                     }
11972                     if (v == v0 - 1 && overflow) {
11973                         /* If the overflow goes all the
11974                          * way to the front, we need to
11975                          * insert 0x1 in front, and adjust
11976                          * the exponent. */
11977                         Move(v0, v0 + 1, vn - 1, char);
11978                         *v0 = 0x1;
11979                         exponent += 4;
11980                     }
11981                 }
11982
11983                 /* The new effective "last non zero". */
11984                 vlnz = v0 + precis;
11985             }
11986             else {
11987                 zerotail =
11988                   subnormal ? precis - vn + 1 :
11989                   precis - (vlnz - vhex);
11990             }
11991         }
11992
11993         v = v0;
11994         *p++ = xdig[*v++];
11995
11996         /* If there are non-zero xdigits, the radix
11997          * is output after the first one. */
11998         if (vfnz < vlnz) {
11999           hexradix = TRUE;
12000         }
12001     }
12002     else {
12003         *p++ = '0';
12004         exponent = 0;
12005         zerotail = has_precis ? precis : 0;
12006     }
12007
12008     /* The radix is always output if precis, or if alt. */
12009     if ((has_precis && precis > 0) || alt) {
12010       hexradix = TRUE;
12011     }
12012
12013     if (hexradix) {
12014 #ifndef USE_LOCALE_NUMERIC
12015         PERL_UNUSED_ARG(in_lc_numeric);
12016
12017         *p++ = '.';
12018 #else
12019         if (in_lc_numeric) {
12020             STRLEN n;
12021             WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
12022                 const char* r = SvPV(PL_numeric_radix_sv, n);
12023                 Copy(r, p, n, char);
12024             });
12025             p += n;
12026         }
12027         else {
12028             *p++ = '.';
12029         }
12030 #endif
12031     }
12032
12033     if (vlnz) {
12034         while (v <= vlnz)
12035             *p++ = xdig[*v++];
12036     }
12037
12038     if (zerotail > 0) {
12039       while (zerotail--) {
12040         *p++ = '0';
12041       }
12042     }
12043
12044     elen = p - buf;
12045
12046     /* sanity checks */
12047     if (elen >= bufsize || width >= bufsize)
12048         /* diag_listed_as: Hexadecimal float: internal error (%s) */
12049         Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
12050
12051     elen += my_snprintf(p, bufsize - elen,
12052                         "%c%+d", lower ? 'p' : 'P',
12053                         exponent);
12054
12055     if (elen < width) {
12056         STRLEN gap = (STRLEN)(width - elen);
12057         if (left) {
12058             /* Pad the back with spaces. */
12059             memset(buf + elen, ' ', gap);
12060         }
12061         else if (fill) {
12062             /* Insert the zeros after the "0x" and the
12063              * the potential sign, but before the digits,
12064              * otherwise we end up with "0000xH.HHH...",
12065              * when we want "0x000H.HHH..."  */
12066             STRLEN nzero = gap;
12067             char* zerox = buf + 2;
12068             STRLEN nmove = elen - 2;
12069             if (negative || plus) {
12070                 zerox++;
12071                 nmove--;
12072             }
12073             Move(zerox, zerox + nzero, nmove, char);
12074             memset(zerox, fill ? '0' : ' ', nzero);
12075         }
12076         else {
12077             /* Move it to the right. */
12078             Move(buf, buf + gap,
12079                  elen, char);
12080             /* Pad the front with spaces. */
12081             memset(buf, ' ', gap);
12082         }
12083         elen = width;
12084     }
12085     return elen;
12086 }
12087
12088 /*
12089 =for apidoc sv_vcatpvfn
12090 =for apidoc_item sv_vcatpvfn_flags
12091
12092 These process their arguments like C<L<vsprintf(3)>> and append the formatted output
12093 to an SV.  They use an array of SVs if the C-style variable argument list is
12094 missing (C<NULL>). Argument reordering (using format specifiers like C<%2$d> or
12095 C<%*2$d>) is supported only when using an array of SVs; using a C-style
12096 C<va_list> argument list with a format string that uses argument reordering
12097 will yield an exception.
12098
12099 When running with taint checks enabled, they indicate via C<maybe_tainted> if
12100 results are untrustworthy (often due to the use of locales).
12101
12102 They assume that C<pat> has the same utf8-ness as C<sv>.  It's the caller's
12103 responsibility to ensure that this is so.
12104
12105 They differ in that C<sv_vcatpvfn_flags> has a C<flags> parameter in which you
12106 can set or clear the C<SV_GMAGIC> and/or S<SV_SMAGIC> flags, to specify which
12107 magic to handle or not handle; whereas plain C<sv_vcatpvfn> always specifies
12108 both 'get' and 'set' magic.
12109
12110 They are usually used via one of the frontends L</C<sv_vcatpvf>> and
12111 L</C<sv_vcatpvf_mg>>.
12112
12113 =cut
12114 */
12115
12116
12117 void
12118 Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
12119                        va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted,
12120                        const U32 flags)
12121 {
12122     const char *fmtstart; /* character following the current '%' */
12123     const char *q;        /* current position within format */
12124     const char *patend;
12125     STRLEN origlen;
12126     Size_t svix = 0;
12127     static const char nullstr[] = "(null)";
12128     bool has_utf8 = DO_UTF8(sv);    /* has the result utf8? */
12129     const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
12130     /* Times 4: a decimal digit takes more than 3 binary digits.
12131      * NV_DIG: mantissa takes that many decimal digits.
12132      * Plus 32: Playing safe. */
12133     char ebuf[IV_DIG * 4 + NV_DIG + 32];
12134     bool no_redundant_warning = FALSE; /* did we use any explicit format parameter index? */
12135 #ifdef USE_LOCALE_NUMERIC
12136     bool have_in_lc_numeric = FALSE;
12137 #endif
12138     /* we never change this unless USE_LOCALE_NUMERIC */
12139     bool in_lc_numeric = FALSE;
12140     SV *tmp_sv = NULL;
12141
12142     PERL_ARGS_ASSERT_SV_VCATPVFN_FLAGS;
12143     PERL_UNUSED_ARG(maybe_tainted);
12144
12145     if (flags & SV_GMAGIC)
12146         SvGETMAGIC(sv);
12147
12148     /* no matter what, this is a string now */
12149     (void)SvPV_force_nomg(sv, origlen);
12150
12151     /* the code that scans for flags etc following a % relies on
12152      * a '\0' being present to avoid falling off the end. Ideally that
12153      * should be fixed */
12154     assert(pat[patlen] == '\0');
12155
12156
12157     /* Special-case "", "%s", "%-p" (SVf - see below) and "%.0f".
12158      * In each case, if there isn't the correct number of args, instead
12159      * fall through to the main code to handle the issuing of any
12160      * warnings etc.
12161      */
12162
12163     if (patlen == 0 && (args || sv_count == 0))
12164         return;
12165
12166     if (patlen <= 4 && pat[0] == '%' && (args || sv_count == 1)) {
12167
12168         /* "%s" */
12169         if (patlen == 2 && pat[1] == 's') {
12170             if (args) {
12171                 const char * const s = va_arg(*args, char*);
12172                 sv_catpv_nomg(sv, s ? s : nullstr);
12173             }
12174             else {
12175                 /* we want get magic on the source but not the target.
12176                  * sv_catsv can't do that, though */
12177                 SvGETMAGIC(*svargs);
12178                 sv_catsv_nomg(sv, *svargs);
12179             }
12180             return;
12181         }
12182
12183         /* "%-p" */
12184         if (args) {
12185             if (patlen == 3  && pat[1] == '-' && pat[2] == 'p') {
12186                 SV *asv = MUTABLE_SV(va_arg(*args, void*));
12187                 sv_catsv_nomg(sv, asv);
12188                 return;
12189             }
12190         }
12191 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
12192         /* special-case "%.0f" */
12193         else if (   patlen == 4
12194                  && pat[1] == '.' && pat[2] == '0' && pat[3] == 'f')
12195         {
12196             const NV nv = SvNV(*svargs);
12197             if (LIKELY(!Perl_isinfnan(nv))) {
12198                 STRLEN l;
12199                 char *p;
12200
12201                 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
12202                     sv_catpvn_nomg(sv, p, l);
12203                     return;
12204                 }
12205             }
12206         }
12207 #endif /* !USE_LONG_DOUBLE */
12208     }
12209
12210
12211     patend = (char*)pat + patlen;
12212     for (fmtstart = pat; fmtstart < patend; fmtstart = q) {
12213         char intsize     = 0;         /* size qualifier in "%hi..." etc */
12214         bool alt         = FALSE;     /* has      "%#..."    */
12215         bool left        = FALSE;     /* has      "%-..."    */
12216         bool fill        = FALSE;     /* has      "%0..."    */
12217         char plus        = 0;         /* has      "%+..."    */
12218         STRLEN width     = 0;         /* value of "%NNN..."  */
12219         bool has_precis  = FALSE;     /* has      "%.NNN..." */
12220         STRLEN precis    = 0;         /* value of "%.NNN..." */
12221         int base         = 0;         /* base to print in, e.g. 8 for %o */
12222         UV uv            = 0;         /* the value to print of int-ish args */
12223
12224         bool vectorize   = FALSE;     /* has      "%v..."    */
12225         bool vec_utf8    = FALSE;     /* SvUTF8(vec arg)     */
12226         const U8 *vecstr = NULL;      /* SvPVX(vec arg)      */
12227         STRLEN veclen    = 0;         /* SvCUR(vec arg)      */
12228         const char *dotstr = NULL;    /* separator string for %v */
12229         STRLEN dotstrlen;             /* length of separator string for %v */
12230
12231         Size_t efix      = 0;         /* explicit format parameter index */
12232         const Size_t osvix  = svix;   /* original index in case of bad fmt */
12233
12234         SV *argsv        = NULL;
12235         bool is_utf8     = FALSE;     /* is this item utf8?   */
12236         bool arg_missing = FALSE;     /* give "Missing argument" warning */
12237         char esignbuf[4];             /* holds sign prefix, e.g. "-0x" */
12238         STRLEN esignlen  = 0;         /* length of e.g. "-0x" */
12239         STRLEN zeros     = 0;         /* how many '0' to prepend */
12240
12241         const char *eptr = NULL;      /* the address of the element string */
12242         STRLEN elen      = 0;         /* the length  of the element string */
12243
12244         char c;                       /* the actual format ('d', s' etc) */
12245
12246         bool escape_it   = FALSE;     /* if this is a string should we quote and escape it? */
12247
12248
12249         /* echo everything up to the next format specification */
12250         for (q = fmtstart; q < patend && *q != '%'; ++q)
12251             {};
12252
12253         if (q > fmtstart) {
12254             if (has_utf8 && !pat_utf8) {
12255                 /* upgrade and copy the bytes of fmtstart..q-1 to utf8 on
12256                  * the fly */
12257                 const char *p;
12258                 char *dst;
12259                 STRLEN need = SvCUR(sv) + (q - fmtstart) + 1;
12260
12261                 for (p = fmtstart; p < q; p++)
12262                     if (!NATIVE_BYTE_IS_INVARIANT(*p))
12263                         need++;
12264                 SvGROW(sv, need);
12265
12266                 dst = SvEND(sv);
12267                 for (p = fmtstart; p < q; p++)
12268                     append_utf8_from_native_byte((U8)*p, (U8**)&dst);
12269                 *dst = '\0';
12270                 SvCUR_set(sv, need - 1);
12271             }
12272             else
12273                 S_sv_catpvn_simple(aTHX_ sv, fmtstart, q - fmtstart);
12274         }
12275         if (q++ >= patend)
12276             break;
12277
12278         fmtstart = q; /* fmtstart is char following the '%' */
12279
12280 /*
12281     We allow format specification elements in this order:
12282         \d+\$              explicit format parameter index
12283         [-+ 0#]+           flags
12284         v|\*(\d+\$)?v      vector with optional (optionally specified) arg
12285         0                  flag (as above): repeated to allow "v02"
12286         \d+|\*(\d+\$)?     width using optional (optionally specified) arg
12287         \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
12288         [hlqLV]            size
12289     [%bcdefginopsuxDFOUX] format (mandatory)
12290 */
12291
12292         if (inRANGE(*q, '1', '9')) {
12293             width = expect_number(&q);
12294             if (*q == '$') {
12295                 if (args)
12296                     Perl_croak_nocontext(
12297                         "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12298                 ++q;
12299                 efix = (Size_t)width;
12300                 width = 0;
12301                 no_redundant_warning = TRUE;
12302             } else {
12303                 goto gotwidth;
12304             }
12305         }
12306
12307         /* FLAGS */
12308
12309         while (*q) {
12310             switch (*q) {
12311             case ' ':
12312             case '+':
12313                 if (plus == '+' && *q == ' ') /* '+' over ' ' */
12314                     q++;
12315                 else
12316                     plus = *q++;
12317                 continue;
12318
12319             case '-':
12320                 left = TRUE;
12321                 q++;
12322                 continue;
12323
12324             case '0':
12325                 fill = TRUE;
12326                 q++;
12327                 continue;
12328
12329             case '#':
12330                 alt = TRUE;
12331                 q++;
12332                 continue;
12333
12334             default:
12335                 break;
12336             }
12337             break;
12338         }
12339
12340       /* at this point we can expect one of:
12341        *
12342        *  123  an explicit width
12343        *  *    width taken from next arg
12344        *  *12$ width taken from 12th arg
12345        *       or no width
12346        *
12347        * But any width specification may be preceded by a v, in one of its
12348        * forms:
12349        *        v
12350        *        *v
12351        *        *12$v
12352        * So an asterisk may be either a width specifier or a vector
12353        * separator arg specifier, and we don't know which initially
12354        */
12355
12356       tryasterisk:
12357         if (*q == '*') {
12358             STRLEN ix; /* explicit width/vector separator index */
12359             q++;
12360             if (inRANGE(*q, '1', '9')) {
12361                 ix = expect_number(&q);
12362                 if (*q++ == '$') {
12363                     if (args)
12364                         Perl_croak_nocontext(
12365                             "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12366                     no_redundant_warning = TRUE;
12367                 } else
12368                     goto unknown;
12369             }
12370             else
12371                 ix = 0;
12372
12373             if (*q == 'v') {
12374                 SV *vecsv;
12375                 /* The asterisk was for  *v, *NNN$v: vectorizing, but not
12376                  * with the default "." */
12377                 q++;
12378                 if (vectorize)
12379                     goto unknown;
12380                 if (args)
12381                     vecsv = va_arg(*args, SV*);
12382                 else {
12383                     ix = ix ? ix - 1 : svix++;
12384                     vecsv = ix < sv_count ? svargs[ix]
12385                                        : (arg_missing = TRUE, &PL_sv_no);
12386                 }
12387                 dotstr = SvPV_const(vecsv, dotstrlen);
12388                 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
12389                    bad with tied or overloaded values that return UTF8.  */
12390                 if (DO_UTF8(vecsv))
12391                     is_utf8 = TRUE;
12392                 else if (has_utf8) {
12393                     vecsv = sv_mortalcopy(vecsv);
12394                     sv_utf8_upgrade(vecsv);
12395                     dotstr = SvPV_const(vecsv, dotstrlen);
12396                     is_utf8 = TRUE;
12397                 }
12398                 vectorize = TRUE;
12399                 goto tryasterisk;
12400             }
12401
12402             /* the asterisk specified a width */
12403             {
12404                 int i = 0;
12405                 SV *width_sv = NULL;
12406                 if (args)
12407                     i = va_arg(*args, int);
12408                 else {
12409                     ix = ix ? ix - 1 : svix++;
12410                     width_sv = (ix < sv_count) ? svargs[ix]
12411                                       : (arg_missing = TRUE, (SV*)NULL);
12412                 }
12413                 width = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &left);
12414             }
12415         }
12416         else if (*q == 'v') {
12417             q++;
12418             if (vectorize)
12419                 goto unknown;
12420             vectorize = TRUE;
12421             dotstr = ".";
12422             dotstrlen = 1;
12423             goto tryasterisk;
12424
12425         }
12426         else {
12427         /* explicit width? */
12428             if(*q == '0') {
12429                 fill = TRUE;
12430                 q++;
12431             }
12432             if (inRANGE(*q, '1', '9'))
12433                 width = expect_number(&q);
12434         }
12435
12436       gotwidth:
12437
12438         /* PRECISION */
12439
12440         if (*q == '.') {
12441             q++;
12442             if (*q == '*') {
12443                 STRLEN ix; /* explicit precision index */
12444                 q++;
12445                 if (inRANGE(*q, '1', '9')) {
12446                     ix = expect_number(&q);
12447                     if (*q++ == '$') {
12448                         if (args)
12449                             Perl_croak_nocontext(
12450                                 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12451                         no_redundant_warning = TRUE;
12452                     } else
12453                         goto unknown;
12454                 }
12455                 else
12456                     ix = 0;
12457
12458                 {
12459                     int i = 0;
12460                     SV *width_sv = NULL;
12461                     bool neg = FALSE;
12462
12463                     if (args)
12464                         i = va_arg(*args, int);
12465                     else {
12466                         ix = ix ? ix - 1 : svix++;
12467                         width_sv = (ix < sv_count) ? svargs[ix]
12468                                           : (arg_missing = TRUE, (SV*)NULL);
12469                     }
12470                     precis = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &neg);
12471                     has_precis = !neg;
12472                     /* ignore negative precision */
12473                     if (!has_precis)
12474                         precis = 0;
12475                 }
12476             }
12477             else {
12478                 /* although it doesn't seem documented, this code has long
12479                  * behaved so that:
12480                  *   no digits following the '.' is treated like '.0'
12481                  *   the number may be preceded by any number of zeroes,
12482                  *      e.g. "%.0001f", which is the same as "%.1f"
12483                  * so I've kept that behaviour. DAPM May 2017
12484                  */
12485                 while (*q == '0')
12486                     q++;
12487                 precis = inRANGE(*q, '1', '9') ? expect_number(&q) : 0;
12488                 has_precis = TRUE;
12489             }
12490         }
12491
12492         /* SIZE */
12493
12494         switch (*q) {
12495 #ifdef WIN32
12496         case 'I':                       /* Ix, I32x, and I64x */
12497 #  ifdef USE_64_BIT_INT
12498             if (q[1] == '6' && q[2] == '4') {
12499                 q += 3;
12500                 intsize = 'q';
12501                 break;
12502             }
12503 #  endif
12504             if (q[1] == '3' && q[2] == '2') {
12505                 q += 3;
12506                 break;
12507             }
12508 #  ifdef USE_64_BIT_INT
12509             intsize = 'q';
12510 #  endif
12511             q++;
12512             break;
12513 #endif
12514 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12515     (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12516         case 'L':                       /* Ld */
12517             /* FALLTHROUGH */
12518 #  if IVSIZE >= 8
12519         case 'q':                       /* qd */
12520 #  endif
12521             intsize = 'q';
12522             q++;
12523             break;
12524 #endif
12525         case 'l':
12526             ++q;
12527 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12528     (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12529             if (*q == 'l') {    /* lld, llf */
12530                 intsize = 'q';
12531                 ++q;
12532             }
12533             else
12534 #endif
12535                 intsize = 'l';
12536             break;
12537         case 'h':
12538             if (*++q == 'h') {  /* hhd, hhu */
12539                 intsize = 'c';
12540                 ++q;
12541             }
12542             else
12543                 intsize = 'h';
12544             break;
12545 #ifdef USE_QUADMATH
12546         case 'Q':
12547 #endif
12548         case 'V':
12549         case 'z':
12550         case 't':
12551         case 'j':
12552             intsize = *q++;
12553             break;
12554         }
12555
12556         /* CONVERSION */
12557
12558         c = *q++; /* c now holds the conversion type */
12559
12560         /* '%' doesn't have an arg, so skip arg processing */
12561         if (c == '%') {
12562             eptr = q - 1;
12563             elen = 1;
12564             if (vectorize)
12565                 goto unknown;
12566             goto string;
12567         }
12568
12569         if (vectorize && !memCHRs("BbDdiOouUXx", c))
12570             goto unknown;
12571
12572         /* get next arg (individual branches do their own va_arg()
12573          * handling for the args case) */
12574
12575         if (!args) {
12576             efix = efix ? efix - 1 : svix++;
12577             argsv = efix < sv_count ? svargs[efix]
12578                                  : (arg_missing = TRUE, &PL_sv_no);
12579         }
12580
12581
12582         switch (c) {
12583
12584             /* STRINGS */
12585
12586         case 's':
12587             if (args) {
12588                 eptr = va_arg(*args, char*);
12589                 if (eptr)
12590                     if (has_precis)
12591                         elen = my_strnlen(eptr, precis);
12592                     else
12593                         elen = strlen(eptr);
12594                 else {
12595                     eptr = (char *)nullstr;
12596                     elen = sizeof nullstr - 1;
12597                 }
12598             }
12599             else {
12600                 eptr = SvPV_const(argsv, elen);
12601                 if (DO_UTF8(argsv)) {
12602                     STRLEN old_precis = precis;
12603                     if (has_precis && precis < elen) {
12604                         STRLEN ulen = sv_or_pv_len_utf8(argsv, eptr, elen);
12605                         STRLEN p = precis > ulen ? ulen : precis;
12606                         precis = sv_or_pv_pos_u2b(argsv, eptr, p, 0);
12607                                                         /* sticks at end */
12608                     }
12609                     if (width) { /* fudge width (can't fudge elen) */
12610                         if (has_precis && precis < elen)
12611                             width += precis - old_precis;
12612                         else
12613                             width +=
12614                                 elen - sv_or_pv_len_utf8(argsv,eptr,elen);
12615                     }
12616                     is_utf8 = TRUE;
12617                 }
12618             }
12619
12620         string:
12621             if (escape_it) {
12622                 U32 flags = PERL_PV_PRETTY_QUOTEDPREFIX;
12623                 if (is_utf8)
12624                     flags |= PERL_PV_ESCAPE_UNI;
12625
12626                 if (!tmp_sv) {
12627                     /* "blah"... where blah might be made up
12628                      * of characters like \x{1234} */
12629                     tmp_sv = newSV(1 + (PERL_QUOTEDPREFIX_LEN * 8) + 1 + 3);
12630                     sv_2mortal(tmp_sv);
12631                 }
12632                 pv_pretty(tmp_sv, eptr, elen, PERL_QUOTEDPREFIX_LEN,
12633                             NULL, NULL, flags);
12634                 eptr = SvPV_const(tmp_sv, elen);
12635             }
12636             if (has_precis && precis < elen)
12637                 elen = precis;
12638             break;
12639
12640             /* INTEGERS */
12641
12642         case 'p':
12643
12644             /* BEGIN NOTE
12645              *
12646              * We want to extend the C level sprintf format API with
12647              * custom formats for specific types (eg SV*) and behavior.
12648              * However some C compilers are "sprintf aware" and will
12649              * throw compile time exceptions when an illegal sprintf is
12650              * encountered, so we can't just add new format letters.
12651              *
12652              * However it turns out the length argument to the %p format
12653              * is more or less useless (the size of a pointer does not
12654              * change over time) and is not really used in the C level
12655              * code. Accordingly we can map our special behavior to
12656              * specific "length" options to the %p format. We hide these
12657              * mappings behind defines anyway, so nobody needs to know
12658              * that HEKf is actually %2p. This keeps the C compiler
12659              * happy while allowing us to add new formats.
12660              *
12661              * Note the existing logic for which number is used for what
12662              * is torturous. All negative values are used for SVf, and
12663              * non-negative values have arbitrary meanings with no
12664              * structure to them. This may change in the future.
12665              *
12666              * NEVER use the raw %p values directly. Always use the define
12667              * as the underlying mapping may change in the future.
12668              *
12669              * END NOTE
12670              *
12671              * %p extensions:
12672              *
12673              * "%...p" is normally treated like "%...x", except that the
12674              * number to print is the SV's address (or a pointer address
12675              * for C-ish sprintf).
12676              *
12677              * However, the C-ish sprintf variant allows a few special
12678              * extensions. These are currently:
12679              *
12680              * %-p       (SVf)  Like %s, but gets the string from an SV*
12681              *                  arg rather than a char* arg. Use C<SVfARG()>
12682              *                  to set up the argument properly.
12683              *                  (This was previously %_).
12684              *
12685              * %-<num>p         Ditto but like %.<num>s (i.e. num is max
12686              *                  width), there is no escaped and quoted version
12687              *                  of this.
12688              *
12689              * %1p       (PVf_QUOTEDPREFIX). Like raw %s, but it is escaped
12690              *                  and quoted.
12691              *
12692              * %5p       (SVf_QUOTEDPREFIX) Like SVf, but length restricted,
12693              *                  escaped and quoted with pv_pretty. Intended
12694              *                  for error messages.
12695              *
12696              * %2p       (HEKf) Like %s, but using the key string in a HEK
12697              * %7p       (HEKf_QUOTEDPREFIX) ... but escaped and quoted.
12698              *
12699              * %3p       (HEKf256) Ditto but like %.256s
12700              * %8p       (HEKf256_QUOTEDPREFIX) ... but escaped and quoted
12701              *
12702              * %d%lu%4p  (UTF8f) A utf8 string. Consumes 3 args:
12703              *                       (cBOOL(utf8), len, string_buf).
12704              *                   It's handled by the "case 'd'" branch
12705              *                   rather than here.
12706              * %d%lu%9p  (UTF8f_QUOTEDPREFIX) .. but escaped and quoted.
12707              *
12708              * %6p       (HvNAMEf) Like %s, but using the HvNAME() and HvNAMELEN()
12709              * %10p      (HvNAMEf_QUOTEDPREFIX) ... but escaped and quoted
12710              *
12711              * %<num>p   where num is > 9: reserved for future
12712              *           extensions. Warns, but then is treated as a
12713              *           general %p (print hex address) format.
12714              *
12715              * NOTE: If you add a new magic %p value you will
12716              * need to update F<t/porting/diag.t> to be aware of it
12717              * on top of adding the various defines and etc. Do not
12718              * forget to add it to F<pod/perlguts.pod> as well.
12719              */
12720
12721             if (   args
12722                 && !intsize
12723                 && !fill
12724                 && !plus
12725                 && !has_precis
12726                     /* not %*p or %*1$p - any width was explicit */
12727                 && q[-2] != '*'
12728                 && q[-2] != '$'
12729             ) {
12730                 if (left || width == 5) {                /* %-p (SVf), %-NNNp, %5p */
12731                     if (left && width) {
12732                         precis = width;
12733                         has_precis = TRUE;
12734                     } else if (width == 5) {
12735                         escape_it = TRUE;
12736                     }
12737                     argsv = MUTABLE_SV(va_arg(*args, void*));
12738                     eptr = SvPV_const(argsv, elen);
12739                     if (DO_UTF8(argsv))
12740                         is_utf8 = TRUE;
12741                     width = 0;
12742                     goto string;
12743                 }
12744                 else if (width == 2 || width == 3 ||
12745                          width == 7 || width == 8)
12746                 {        /* HEKf, HEKf256, HEKf_QUOTEDPREFIX, HEKf256_QUOTEDPREFIX */
12747                     HEK * const hek = va_arg(*args, HEK *);
12748                     eptr = HEK_KEY(hek);
12749                     elen = HEK_LEN(hek);
12750                     if (HEK_UTF8(hek))
12751                         is_utf8 = TRUE;
12752                     if (width == 3) {
12753                         precis = 256;
12754                         has_precis = TRUE;
12755                     }
12756                     if (width > 5)
12757                         escape_it = TRUE;
12758                     width = 0;
12759                     goto string;
12760                 }
12761                 else if (width == 1) {
12762                     eptr = va_arg(*args,char *);
12763                     elen = strlen(eptr);
12764                     escape_it = TRUE;
12765                     width = 0;
12766                     goto string;
12767                 }
12768                 else if (width == 6 || width == 10) {
12769                     HV *hv = va_arg(*args, HV *);
12770                     eptr = HvNAME(hv);
12771                     elen = HvNAMELEN(hv);
12772                     if (HvNAMEUTF8(hv))
12773                         is_utf8 = TRUE;
12774                     if (width == 10)
12775                         escape_it = TRUE;
12776                     width = 0;
12777                     goto string;
12778                 }
12779                 else if (width) {
12780                     /* note width=4 or width=9 is handled under %d */
12781                     Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
12782                          "internal %%<num>p might conflict with future printf extensions");
12783                 }
12784             }
12785
12786             /* treat as normal %...p */
12787
12788             uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
12789             base = 16;
12790             c = 'x';    /* in case the format string contains '#' */
12791             goto do_integer;
12792
12793         case 'c':
12794             /* Ignore any size specifiers, since they're not documented as
12795              * being allowed for %c (ideally we should warn on e.g. '%hc').
12796              * Setting a default intsize, along with a positive
12797              * (which signals unsigned) base, causes, for C-ish use, the
12798              * va_arg to be interpreted as an unsigned int, when it's
12799              * actually signed, which will convert -ve values to high +ve
12800              * values. Note that unlike the libc %c, values > 255 will
12801              * convert to high unicode points rather than being truncated
12802              * to 8 bits. For perlish use, it will do SvUV(argsv), which
12803              * will again convert -ve args to high -ve values.
12804              */
12805             intsize = 0;
12806             base = 1; /* special value that indicates we're doing a 'c' */
12807             goto get_int_arg_val;
12808
12809         case 'D':
12810 #ifdef IV_IS_QUAD
12811             intsize = 'q';
12812 #else
12813             intsize = 'l';
12814 #endif
12815             base = -10;
12816             goto get_int_arg_val;
12817
12818         case 'd':
12819             /* probably just a plain %d, but it might be the start of the
12820              * special UTF8f format, which usually looks something like
12821              * "%d%lu%4p" (the lu may vary by platform) or
12822              * "%d%lu%9p" for an escaped version.
12823              */
12824             assert((UTF8f)[0] == 'd');
12825             assert((UTF8f)[1] == '%');
12826
12827              if (   args              /* UTF8f only valid for C-ish sprintf */
12828                  && q == fmtstart + 1 /* plain %d, not %....d */
12829                  && patend >= fmtstart + sizeof(UTF8f) - 1 /* long enough */
12830                  && *q == '%'
12831                  && strnEQ(q + 1, (UTF8f) + 2, sizeof(UTF8f) - 5)
12832                  && q[sizeof(UTF8f)-3] == 'p'
12833                  && (q[sizeof(UTF8f)-4] == '4' ||
12834                      q[sizeof(UTF8f)-4] == '9'))
12835             {
12836                 /* The argument has already gone through cBOOL, so the cast
12837                    is safe. */
12838                 if (q[sizeof(UTF8f)-4] == '9')
12839                     escape_it = TRUE;
12840                 is_utf8 = (bool)va_arg(*args, int);
12841                 elen = va_arg(*args, UV);
12842                 /* if utf8 length is larger than 0x7ffff..., then it might
12843                  * have been a signed value that wrapped */
12844                 if (elen  > ((~(STRLEN)0) >> 1)) {
12845                     assert(0); /* in DEBUGGING build we want to crash */
12846                     elen = 0; /* otherwise we want to treat this as an empty string */
12847                 }
12848                 eptr = va_arg(*args, char *);
12849                 q += sizeof(UTF8f) - 2;
12850                 goto string;
12851             }
12852
12853             /* FALLTHROUGH */
12854         case 'i':
12855             base = -10;
12856             goto get_int_arg_val;
12857
12858         case 'U':
12859 #ifdef IV_IS_QUAD
12860             intsize = 'q';
12861 #else
12862             intsize = 'l';
12863 #endif
12864             /* FALLTHROUGH */
12865         case 'u':
12866             base = 10;
12867             goto get_int_arg_val;
12868
12869         case 'B':
12870         case 'b':
12871             base = 2;
12872             goto get_int_arg_val;
12873
12874         case 'O':
12875 #ifdef IV_IS_QUAD
12876             intsize = 'q';
12877 #else
12878             intsize = 'l';
12879 #endif
12880             /* FALLTHROUGH */
12881         case 'o':
12882             base = 8;
12883             goto get_int_arg_val;
12884
12885         case 'X':
12886         case 'x':
12887             base = 16;
12888
12889           get_int_arg_val:
12890
12891             if (vectorize) {
12892                 STRLEN ulen;
12893                 SV *vecsv;
12894
12895                 if (base < 0) {
12896                     base = -base;
12897                     if (plus)
12898                          esignbuf[esignlen++] = plus;
12899                 }
12900
12901                 /* initialise the vector string to iterate over */
12902
12903                 vecsv = args ? va_arg(*args, SV*) : argsv;
12904
12905                 /* if this is a version object, we need to convert
12906                  * back into v-string notation and then let the
12907                  * vectorize happen normally
12908                  */
12909                 if (sv_isobject(vecsv) && sv_derived_from(vecsv, "version")) {
12910                     if ( hv_existss(MUTABLE_HV(SvRV(vecsv)), "alpha") ) {
12911                         Perl_ck_warner_d(aTHX_ packWARN(WARN_PRINTF),
12912                         "vector argument not supported with alpha versions");
12913                         vecsv = &PL_sv_no;
12914                     }
12915                     else {
12916                         vecstr = (U8*)SvPV_const(vecsv,veclen);
12917                         vecsv = sv_newmortal();
12918                         scan_vstring((char *)vecstr, (char *)vecstr + veclen,
12919                                      vecsv);
12920                     }
12921                 }
12922                 vecstr = (U8*)SvPV_const(vecsv, veclen);
12923                 vec_utf8 = DO_UTF8(vecsv);
12924
12925               /* This is the re-entry point for when we're iterating
12926                * over the individual characters of a vector arg */
12927               vector:
12928                 if (!veclen)
12929                     goto done_valid_conversion;
12930                 if (vec_utf8)
12931                     uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
12932                                         UTF8_ALLOW_ANYUV);
12933                 else {
12934                     uv = *vecstr;
12935                     ulen = 1;
12936                 }
12937                 vecstr += ulen;
12938                 veclen -= ulen;
12939             }
12940             else {
12941                 /* test arg for inf/nan. This can trigger an unwanted
12942                  * 'str' overload, so manually force 'num' overload first
12943                  * if necessary */
12944                 if (argsv) {
12945                     SvGETMAGIC(argsv);
12946                     if (UNLIKELY(SvAMAGIC(argsv)))
12947                         argsv = sv_2num(argsv);
12948                     if (UNLIKELY(isinfnansv(argsv)))
12949                         goto handle_infnan_argsv;
12950                 }
12951
12952                 if (base < 0) {
12953                     /* signed int type */
12954                     IV iv;
12955                     base = -base;
12956                     if (args) {
12957                         switch (intsize) {
12958                         case 'c':  iv = (char)va_arg(*args, int);  break;
12959                         case 'h':  iv = (short)va_arg(*args, int); break;
12960                         case 'l':  iv = va_arg(*args, long);       break;
12961                         case 'V':  iv = va_arg(*args, IV);         break;
12962                         case 'z':  iv = va_arg(*args, SSize_t);    break;
12963 #ifdef HAS_PTRDIFF_T
12964                         case 't':  iv = va_arg(*args, ptrdiff_t);  break;
12965 #endif
12966                         default:   iv = va_arg(*args, int);        break;
12967                         case 'j':  iv = (IV) va_arg(*args, PERL_INTMAX_T); break;
12968                         case 'q':
12969 #if IVSIZE >= 8
12970                                    iv = va_arg(*args, Quad_t);     break;
12971 #else
12972                                    goto unknown;
12973 #endif
12974                         }
12975                     }
12976                     else {
12977                         /* assign to tiv then cast to iv to work around
12978                          * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12979                         IV tiv = SvIV_nomg(argsv);
12980                         switch (intsize) {
12981                         case 'c':  iv = (char)tiv;   break;
12982                         case 'h':  iv = (short)tiv;  break;
12983                         case 'l':  iv = (long)tiv;   break;
12984                         case 'V':
12985                         default:   iv = tiv;         break;
12986                         case 'q':
12987 #if IVSIZE >= 8
12988                                    iv = (Quad_t)tiv; break;
12989 #else
12990                                    goto unknown;
12991 #endif
12992                         }
12993                     }
12994
12995                     /* now convert iv to uv */
12996                     if (iv >= 0) {
12997                         uv = iv;
12998                         if (plus)
12999                             esignbuf[esignlen++] = plus;
13000                     }
13001                     else {
13002                         /* Using 0- here to silence bogus warning from MS VC */
13003                         uv = (UV) (0 - (UV) iv);
13004                         esignbuf[esignlen++] = '-';
13005                     }
13006                 }
13007                 else {
13008                     /* unsigned int type */
13009                     if (args) {
13010                         switch (intsize) {
13011                         case 'c': uv = (unsigned char)va_arg(*args, unsigned);
13012                                   break;
13013                         case 'h': uv = (unsigned short)va_arg(*args, unsigned);
13014                                   break;
13015                         case 'l': uv = va_arg(*args, unsigned long); break;
13016                         case 'V': uv = va_arg(*args, UV);            break;
13017                         case 'z': uv = va_arg(*args, Size_t);        break;
13018 #ifdef HAS_PTRDIFF_T
13019                                   /* will sign extend, but there is no
13020                                    * uptrdiff_t, so oh well */
13021                         case 't': uv = va_arg(*args, ptrdiff_t);     break;
13022 #endif
13023                         case 'j': uv = (UV) va_arg(*args, PERL_UINTMAX_T); break;
13024                         default:  uv = va_arg(*args, unsigned);      break;
13025                         case 'q':
13026 #if IVSIZE >= 8
13027                                   uv = va_arg(*args, Uquad_t);       break;
13028 #else
13029                                   goto unknown;
13030 #endif
13031                         }
13032                     }
13033                     else {
13034                         /* assign to tiv then cast to iv to work around
13035                          * 2003 GCC cast bug (gnu.org bugzilla #13488) */
13036                         UV tuv = SvUV_nomg(argsv);
13037                         switch (intsize) {
13038                         case 'c': uv = (unsigned char)tuv;  break;
13039                         case 'h': uv = (unsigned short)tuv; break;
13040                         case 'l': uv = (unsigned long)tuv;  break;
13041                         case 'V':
13042                         default:  uv = tuv;                 break;
13043                         case 'q':
13044 #if IVSIZE >= 8
13045                                   uv = (Uquad_t)tuv;        break;
13046 #else
13047                                   goto unknown;
13048 #endif
13049                         }
13050                     }
13051                 }
13052             }
13053
13054         do_integer:
13055             {
13056                 char *ptr = ebuf + sizeof ebuf;
13057                 unsigned dig;
13058                 zeros = 0;
13059
13060                 switch (base) {
13061                 case 16:
13062                     {
13063                     const char * const p =
13064                             (c == 'X') ? PL_hexdigit + 16 : PL_hexdigit;
13065
13066                         do {
13067                             dig = uv & 15;
13068                             *--ptr = p[dig];
13069                         } while (uv >>= 4);
13070                         if (alt && *ptr != '0') {
13071                             esignbuf[esignlen++] = '0';
13072                             esignbuf[esignlen++] = c;  /* 'x' or 'X' */
13073                         }
13074                         break;
13075                     }
13076                 case 8:
13077                     do {
13078                         dig = uv & 7;
13079                         *--ptr = '0' + dig;
13080                     } while (uv >>= 3);
13081                     if (alt && *ptr != '0')
13082                         *--ptr = '0';
13083                     break;
13084                 case 2:
13085                     do {
13086                         dig = uv & 1;
13087                         *--ptr = '0' + dig;
13088                     } while (uv >>= 1);
13089                     if (alt && *ptr != '0') {
13090                         esignbuf[esignlen++] = '0';
13091                         esignbuf[esignlen++] = c; /* 'b' or 'B' */
13092                     }
13093                     break;
13094
13095                 case 1:
13096                     /* special-case: base 1 indicates a 'c' format:
13097                      * we use the common code for extracting a uv,
13098                      * but handle that value differently here than
13099                      * all the other int types */
13100                     if ((uv > 255 ||
13101                          (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
13102                         && !IN_BYTES)
13103                     {
13104                         STATIC_ASSERT_STMT(sizeof(ebuf) >= UTF8_MAXBYTES + 1);
13105                         eptr = ebuf;
13106                         elen = uvchr_to_utf8((U8*)eptr, uv) - (U8*)ebuf;
13107                         is_utf8 = TRUE;
13108                     }
13109                     else {
13110                         eptr = ebuf;
13111                         ebuf[0] = (char)uv;
13112                         elen = 1;
13113                     }
13114                     goto string;
13115
13116                 default:                /* it had better be ten or less */
13117                     do {
13118                         dig = uv % base;
13119                         *--ptr = '0' + dig;
13120                     } while (uv /= base);
13121                     break;
13122                 }
13123                 elen = (ebuf + sizeof ebuf) - ptr;
13124                 eptr = ptr;
13125                 if (has_precis) {
13126                     if (precis > elen)
13127                         zeros = precis - elen;
13128                     else if (precis == 0 && elen == 1 && *eptr == '0'
13129                              && !(base == 8 && alt)) /* "%#.0o" prints "0" */
13130                         elen = 0;
13131
13132                     /* a precision nullifies the 0 flag. */
13133                     fill = FALSE;
13134                 }
13135             }
13136             break;
13137
13138             /* FLOATING POINT */
13139
13140         case 'F':
13141             c = 'f';            /* maybe %F isn't supported here */
13142             /* FALLTHROUGH */
13143         case 'e': case 'E':
13144         case 'f':
13145         case 'g': case 'G':
13146         case 'a': case 'A':
13147
13148         {
13149             STRLEN float_need; /* what PL_efloatsize needs to become */
13150             bool hexfp;        /* hexadecimal floating point? */
13151
13152             vcatpvfn_long_double_t fv;
13153             NV                     nv;
13154
13155             /* This is evil, but floating point is even more evil */
13156
13157             /* for SV-style calling, we can only get NV
13158                for C-style calling, we assume %f is double;
13159                for simplicity we allow any of %Lf, %llf, %qf for long double
13160             */
13161             switch (intsize) {
13162 #if defined(USE_QUADMATH)
13163             case 'Q':
13164                 break;
13165 #endif
13166             case 'V':
13167 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13168                 intsize = 'q';
13169 #endif
13170                 break;
13171 /* [perl #20339] - we should accept and ignore %lf rather than die */
13172             case 'l':
13173                 /* FALLTHROUGH */
13174             default:
13175 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13176                 intsize = args ? 0 : 'q';
13177 #endif
13178                 break;
13179             case 'q':
13180 #if defined(HAS_LONG_DOUBLE)
13181                 break;
13182 #else
13183                 /* FALLTHROUGH */
13184 #endif
13185             case 'c':
13186             case 'h':
13187             case 'z':
13188             case 't':
13189             case 'j':
13190                 goto unknown;
13191             }
13192
13193             /* Now we need (long double) if intsize == 'q', else (double). */
13194             if (args) {
13195                 /* Note: do not pull NVs off the va_list with va_arg()
13196                  * (pull doubles instead) because if you have a build
13197                  * with long doubles, you would always be pulling long
13198                  * doubles, which would badly break anyone using only
13199                  * doubles (i.e. the majority of builds). In other
13200                  * words, you cannot mix doubles and long doubles.
13201                  * The only case where you can pull off long doubles
13202                  * is when the format specifier explicitly asks so with
13203                  * e.g. "%Lg". */
13204 #ifdef USE_QUADMATH
13205                 nv = intsize == 'Q' ? va_arg(*args, NV) :
13206                     intsize == 'q' ? va_arg(*args, long double) :
13207                     va_arg(*args, double);
13208                 fv = nv;
13209 #elif LONG_DOUBLESIZE > DOUBLESIZE
13210                 if (intsize == 'q') {
13211                     fv = va_arg(*args, long double);
13212                     nv = fv;
13213                 } else {
13214                     nv = va_arg(*args, double);
13215                     VCATPVFN_NV_TO_FV(nv, fv);
13216                 }
13217 #else
13218                 nv = va_arg(*args, double);
13219                 fv = nv;
13220 #endif
13221             }
13222             else
13223             {
13224                 SvGETMAGIC(argsv);
13225                 /* we jump here if an int-ish format encountered an
13226                  * infinite/Nan argsv. After setting nv/fv, it falls
13227                  * into the isinfnan block which follows */
13228               handle_infnan_argsv:
13229                 nv = SvNV_nomg(argsv);
13230                 VCATPVFN_NV_TO_FV(nv, fv);
13231             }
13232
13233             if (Perl_isinfnan(nv)) {
13234                 if (c == 'c')
13235                     Perl_croak(aTHX_ "Cannot printf %" NVgf " with '%c'",
13236                                nv, (int)c);
13237
13238                 elen = S_infnan_2pv(nv, ebuf, sizeof(ebuf), plus);
13239                 assert(elen);
13240                 eptr = ebuf;
13241                 zeros     = 0;
13242                 esignlen  = 0;
13243                 dotstrlen = 0;
13244                 break;
13245             }
13246
13247             /* special-case "%.0f" */
13248             if (   c == 'f'
13249                 && !precis
13250                 && has_precis
13251                 && !(width || left || plus || alt)
13252                 && !fill
13253                 && intsize != 'q'
13254                 && ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
13255             )
13256                 goto float_concat;
13257
13258             /* Determine the buffer size needed for the various
13259              * floating-point formats.
13260              *
13261              * The basic possibilities are:
13262              *
13263              *               <---P--->
13264              *    %f 1111111.123456789
13265              *    %e       1.111111123e+06
13266              *    %a     0x1.0f4471f9bp+20
13267              *    %g        1111111.12
13268              *    %g        1.11111112e+15
13269              *
13270              * where P is the value of the precision in the format, or 6
13271              * if not specified. Note the two possible output formats of
13272              * %g; in both cases the number of significant digits is <=
13273              * precision.
13274              *
13275              * For most of the format types the maximum buffer size needed
13276              * is precision, plus: any leading 1 or 0x1, the radix
13277              * point, and an exponent.  The difficult one is %f: for a
13278              * large positive exponent it can have many leading digits,
13279              * which needs to be calculated specially. Also %a is slightly
13280              * different in that in the absence of a specified precision,
13281              * it uses as many digits as necessary to distinguish
13282              * different values.
13283              *
13284              * First, here are the constant bits. For ease of calculation
13285              * we over-estimate the needed buffer size, for example by
13286              * assuming all formats have an exponent and a leading 0x1.
13287              *
13288              * Also for production use, add a little extra overhead for
13289              * safety's sake. Under debugging don't, as it means we're
13290              * more likely to quickly spot issues during development.
13291              */
13292
13293             float_need =     1  /* possible unary minus */
13294                           +  4  /* "0x1" plus very unlikely carry */
13295                           +  1  /* default radix point '.' */
13296                           +  2  /* "e-", "p+" etc */
13297                           +  6  /* exponent: up to 16383 (quad fp) */
13298 #ifndef DEBUGGING
13299                           + 20  /* safety net */
13300 #endif
13301                           +  1; /* \0 */
13302
13303
13304             /* determine the radix point len, e.g. length(".") in "1.2" */
13305 #ifdef USE_LOCALE_NUMERIC
13306             /* note that we may either explicitly use PL_numeric_radix_sv
13307              * below, or implicitly, via an snprintf() variant.
13308              * Note also things like ps_AF.utf8 which has
13309              * "\N{ARABIC DECIMAL SEPARATOR} as a radix point */
13310             if (! have_in_lc_numeric) {
13311                 in_lc_numeric = IN_LC(LC_NUMERIC);
13312                 have_in_lc_numeric = TRUE;
13313             }
13314
13315             if (in_lc_numeric) {
13316                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
13317                     /* this can't wrap unless PL_numeric_radix_sv is a string
13318                      * consuming virtually all the 32-bit or 64-bit address
13319                      * space
13320                      */
13321                     float_need += (SvCUR(PL_numeric_radix_sv) - 1);
13322
13323                     /* floating-point formats only get utf8 if the radix point
13324                      * is utf8. All other characters in the string are < 128
13325                      * and so can be safely appended to both a non-utf8 and utf8
13326                      * string as-is.
13327                      * Note that this will convert the output to utf8 even if
13328                      * the radix point didn't get output.
13329                      */
13330                     if (SvUTF8(PL_numeric_radix_sv) && !has_utf8) {
13331                         sv_utf8_upgrade(sv);
13332                         has_utf8 = TRUE;
13333                     }
13334                 });
13335             }
13336 #endif
13337
13338             hexfp = FALSE;
13339
13340             if (isALPHA_FOLD_EQ(c, 'f')) {
13341                 /* Determine how many digits before the radix point
13342                  * might be emitted.  frexp() (or frexpl) has some
13343                  * unspecified behaviour for nan/inf/-inf, so lucky we've
13344                  * already handled them above */
13345                 STRLEN digits;
13346                 int i = PERL_INT_MIN;
13347                 (void)Perl_frexp((NV)fv, &i);
13348                 if (i == PERL_INT_MIN)
13349                     Perl_die(aTHX_ "panic: frexp: %" VCATPVFN_FV_GF, fv);
13350
13351                 if (i > 0) {
13352                     digits = BIT_DIGITS(i);
13353                     /* this can't overflow. 'digits' will only be a few
13354                      * thousand even for the largest floating-point types.
13355                      * And up until now float_need is just some small
13356                      * constants plus radix len, which can't be in
13357                      * overflow territory unless the radix SV is consuming
13358                      * over 1/2 the address space */
13359                     assert(float_need < ((STRLEN)~0) - digits);
13360                     float_need += digits;
13361                 }
13362             }
13363             else if (UNLIKELY(isALPHA_FOLD_EQ(c, 'a'))) {
13364                 hexfp = TRUE;
13365                 if (!has_precis) {
13366                     /* %a in the absence of precision may print as many
13367                      * digits as needed to represent the entire mantissa
13368                      * bit pattern.
13369                      * This estimate seriously overshoots in most cases,
13370                      * but better the undershooting.  Firstly, all bytes
13371                      * of the NV are not mantissa, some of them are
13372                      * exponent.  Secondly, for the reasonably common
13373                      * long doubles case, the "80-bit extended", two
13374                      * or six bytes of the NV are unused. Also, we'll
13375                      * still pick up an extra +6 from the default
13376                      * precision calculation below. */
13377                     STRLEN digits =
13378 #ifdef LONGDOUBLE_DOUBLEDOUBLE
13379                         /* For the "double double", we need more.
13380                          * Since each double has their own exponent, the
13381                          * doubles may float (haha) rather far from each
13382                          * other, and the number of required bits is much
13383                          * larger, up to total of DOUBLEDOUBLE_MAXBITS bits.
13384                          * See the definition of DOUBLEDOUBLE_MAXBITS.
13385                          *
13386                          * Need 2 hexdigits for each byte. */
13387                         (DOUBLEDOUBLE_MAXBITS/8 + 1) * 2;
13388 #else
13389                         NVSIZE * 2; /* 2 hexdigits for each byte */
13390 #endif
13391                     /* see "this can't overflow" comment above */
13392                     assert(float_need < ((STRLEN)~0) - digits);
13393                     float_need += digits;
13394                 }
13395             }
13396             /* special-case "%.<number>g" if it will fit in ebuf */
13397             else if (c == 'g'
13398                 && precis   /* See earlier comment about buggy Gconvert
13399                                when digits, aka precis, is 0  */
13400                 && has_precis
13401                 /* check that "%.<number>g" formatting will fit in ebuf  */
13402                 && sizeof(ebuf) - float_need > precis
13403                 /* sizeof(ebuf) - float_need will have wrapped if float_need > sizeof(ebuf).     *
13404                  * Therefore we should check that float_need < sizeof(ebuf). Normally, we would  *
13405                  * have run this check first, but that triggers incorrect -Wformat-overflow      *
13406                  * compilation warnings with some versions of gcc if Gconvert invokes sprintf(). *
13407                  * ( See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89161 )                   *
13408                  * So, instead, we check it next:                                                */
13409                 && float_need < sizeof(ebuf)
13410                 && !(width || left || plus || alt)
13411                 && !fill
13412                 && intsize != 'q'
13413             ) {
13414                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13415                     SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
13416                 );
13417                 elen = strlen(ebuf);
13418                 eptr = ebuf;
13419                 goto float_concat;
13420             }
13421
13422
13423             {
13424                 STRLEN pr = has_precis ? precis : 6; /* known default */
13425                 /* this probably can't wrap, since precis is limited
13426                  * to 1/4 address space size, but better safe than sorry
13427                  */
13428                 if (float_need >= ((STRLEN)~0) - pr)
13429                     croak_memory_wrap();
13430                 float_need += pr;
13431             }
13432
13433             if (float_need < width)
13434                 float_need = width;
13435
13436             if (float_need > INT_MAX) {
13437                 /* snprintf() returns an int, and we use that return value,
13438                    so die horribly if the expected size is too large for int
13439                 */
13440                 Perl_croak(aTHX_ "Numeric format result too large");
13441             }
13442
13443             if (PL_efloatsize <= float_need) {
13444                 /* PL_efloatbuf should be at least 1 greater than
13445                  * float_need to allow a trailing \0 to be returned by
13446                  * snprintf().  If we need to grow, overgrow for the
13447                  * benefit of future generations */
13448                 const STRLEN extra = 0x20;
13449                 if (float_need >= ((STRLEN)~0) - extra)
13450                     croak_memory_wrap();
13451                 float_need += extra;
13452                 Safefree(PL_efloatbuf);
13453                 PL_efloatsize = float_need;
13454                 Newx(PL_efloatbuf, PL_efloatsize, char);
13455                 PL_efloatbuf[0] = '\0';
13456             }
13457
13458             if (UNLIKELY(hexfp)) {
13459                 elen = S_format_hexfp(aTHX_ PL_efloatbuf, PL_efloatsize, c,
13460                                 nv, fv, has_precis, precis, width,
13461                                 alt, plus, left, fill, in_lc_numeric);
13462             }
13463             else {
13464                 char *ptr = ebuf + sizeof ebuf;
13465                 *--ptr = '\0';
13466                 *--ptr = c;
13467 #if defined(USE_QUADMATH)
13468                 /* always use Q here.  my_snprint() throws an exception if we
13469                    fallthrough to the double/long double code, even when the
13470                    format is correct, presumably to avoid any accidentally
13471                    missing Q.
13472                 */
13473                 *--ptr = 'Q';
13474                 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
13475 #elif defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
13476                 /* Note that this is HAS_LONG_DOUBLE and PERL_PRIfldbl,
13477                  * not USE_LONG_DOUBLE and NVff.  In other words,
13478                  * this needs to work without USE_LONG_DOUBLE. */
13479                 if (intsize == 'q') {
13480                     /* Copy the one or more characters in a long double
13481                      * format before the 'base' ([efgEFG]) character to
13482                      * the format string. */
13483                     static char const ldblf[] = PERL_PRIfldbl;
13484                     char const *p = ldblf + sizeof(ldblf) - 3;
13485                     while (p >= ldblf) { *--ptr = *p--; }
13486                 }
13487 #endif
13488                 if (has_precis) {
13489                     base = precis;
13490                     do { *--ptr = '0' + (base % 10); } while (base /= 10);
13491                     *--ptr = '.';
13492                 }
13493                 if (width) {
13494                     base = width;
13495                     do { *--ptr = '0' + (base % 10); } while (base /= 10);
13496                 }
13497                 if (fill)
13498                     *--ptr = '0';
13499                 if (left)
13500                     *--ptr = '-';
13501                 if (plus)
13502                     *--ptr = plus;
13503                 if (alt)
13504                     *--ptr = '#';
13505                 *--ptr = '%';
13506
13507                 /* No taint.  Otherwise we are in the strange situation
13508                  * where printf() taints but print($float) doesn't.
13509                  * --jhi */
13510
13511                 /* hopefully the above makes ptr a very constrained format
13512                  * that is safe to use, even though it's not literal */
13513                 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
13514 #ifdef USE_QUADMATH
13515                 {
13516                     if (!quadmath_format_valid(ptr))
13517                         Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
13518                     WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13519                         elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
13520                                                  ptr, nv);
13521                     );
13522                     if ((IV)elen == -1) {
13523                         Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", ptr);
13524                     }
13525                 }
13526 #elif defined(HAS_LONG_DOUBLE)
13527                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13528                     elen = ((intsize == 'q')
13529                             ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13530                             : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv))
13531                 );
13532 #else
13533                 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13534                     elen = my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13535                 );
13536 #endif
13537                 GCC_DIAG_RESTORE_STMT;
13538             }
13539
13540             eptr = PL_efloatbuf;
13541
13542           float_concat:
13543
13544             /* Since floating-point formats do their own formatting and
13545              * padding, we skip the main block of code at the end of this
13546              * loop which handles appending eptr to sv, and do our own
13547              * stripped-down version */
13548
13549             assert(!zeros);
13550             assert(!esignlen);
13551             assert(elen);
13552             assert(elen >= width);
13553
13554             S_sv_catpvn_simple(aTHX_ sv, eptr, elen);
13555
13556             goto done_valid_conversion;
13557         }
13558
13559             /* SPECIAL */
13560
13561         case 'n':
13562             {
13563                 STRLEN len;
13564                 /* XXX ideally we should warn if any flags etc have been
13565                  * set, e.g. "%-4.5n" */
13566                 /* XXX if sv was originally non-utf8 with a char in the
13567                  * range 0x80-0xff, then if it got upgraded, we should
13568                  * calculate char len rather than byte len here */
13569                 len = SvCUR(sv) - origlen;
13570                 if (args) {
13571                     int i = (len > PERL_INT_MAX) ? PERL_INT_MAX : (int)len;
13572
13573                     switch (intsize) {
13574                     case 'c':  *(va_arg(*args, char*))      = i; break;
13575                     case 'h':  *(va_arg(*args, short*))     = i; break;
13576                     default:   *(va_arg(*args, int*))       = i; break;
13577                     case 'l':  *(va_arg(*args, long*))      = i; break;
13578                     case 'V':  *(va_arg(*args, IV*))        = i; break;
13579                     case 'z':  *(va_arg(*args, SSize_t*))   = i; break;
13580 #ifdef HAS_PTRDIFF_T
13581                     case 't':  *(va_arg(*args, ptrdiff_t*)) = i; break;
13582 #endif
13583                     case 'j':  *(va_arg(*args, PERL_INTMAX_T*)) = i; break;
13584                     case 'q':
13585 #if IVSIZE >= 8
13586                                *(va_arg(*args, Quad_t*))    = i; break;
13587 #else
13588                                goto unknown;
13589 #endif
13590                     }
13591                 }
13592                 else {
13593                     if (arg_missing)
13594                         Perl_croak_nocontext(
13595                             "Missing argument for %%n in %s",
13596                                 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13597                     sv_setuv_mg(argsv, has_utf8
13598                         ? (UV)utf8_length((U8*)SvPVX(sv), (U8*)SvEND(sv))
13599                         : (UV)len);
13600                 }
13601                 goto done_valid_conversion;
13602             }
13603
13604             /* UNKNOWN */
13605
13606         default:
13607       unknown:
13608             if (!args
13609                 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
13610                 && ckWARN(WARN_PRINTF))
13611             {
13612                 SV * const msg = sv_newmortal();
13613                 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
13614                           (PL_op->op_type == OP_PRTF) ? "" : "s");
13615                 if (fmtstart < patend) {
13616                     const char * const fmtend = q < patend ? q : patend;
13617                     const char * f;
13618                     sv_catpvs(msg, "\"%");
13619                     for (f = fmtstart; f < fmtend; f++) {
13620                         if (isPRINT(*f)) {
13621                             sv_catpvn_nomg(msg, f, 1);
13622                         } else {
13623                             Perl_sv_catpvf(aTHX_ msg, "\\%03o", (U8) *f);
13624                         }
13625                     }
13626                     sv_catpvs(msg, "\"");
13627                 } else {
13628                     sv_catpvs(msg, "end of string");
13629                 }
13630                 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%" SVf, SVfARG(msg)); /* yes, this is reentrant */
13631             }
13632
13633             /* mangled format: output the '%', then continue from the
13634              * character following that */
13635             sv_catpvn_nomg(sv, fmtstart-1, 1);
13636             q = fmtstart;
13637             svix = osvix;
13638             /* Any "redundant arg" warning from now onwards will probably
13639              * just be misleading, so don't bother. */
13640             no_redundant_warning = TRUE;
13641             continue;   /* not "break" */
13642         }
13643
13644         if (is_utf8 != has_utf8) {
13645             if (is_utf8) {
13646                 if (SvCUR(sv))
13647                     sv_utf8_upgrade(sv);
13648             }
13649             else {
13650                 const STRLEN old_elen = elen;
13651                 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
13652                 sv_utf8_upgrade(nsv);
13653                 eptr = SvPVX_const(nsv);
13654                 elen = SvCUR(nsv);
13655
13656                 if (width) { /* fudge width (can't fudge elen) */
13657                     width += elen - old_elen;
13658                 }
13659                 is_utf8 = TRUE;
13660             }
13661         }
13662
13663
13664         /* append esignbuf, filler, zeros, eptr and dotstr to sv */
13665
13666         {
13667             STRLEN need, have, gap;
13668             STRLEN i;
13669             char *s;
13670
13671             /* signed value that's wrapped? */
13672             assert(elen  <= ((~(STRLEN)0) >> 1));
13673
13674             /* if zeros is non-zero, then it represents filler between
13675              * elen and precis. So adding elen and zeros together will
13676              * always be <= precis, and the addition can never wrap */
13677             assert(!zeros || (precis > elen && precis - elen == zeros));
13678             have = elen + zeros;
13679
13680             if (have >= (((STRLEN)~0) - esignlen))
13681                 croak_memory_wrap();
13682             have += esignlen;
13683
13684             need = (have > width ? have : width);
13685             gap = need - have;
13686
13687             if (need >= (((STRLEN)~0) - (SvCUR(sv) + 1)))
13688                 croak_memory_wrap();
13689             need += (SvCUR(sv) + 1);
13690
13691             SvGROW(sv, need);
13692
13693             s = SvEND(sv);
13694
13695             if (left) {
13696                 for (i = 0; i < esignlen; i++)
13697                     *s++ = esignbuf[i];
13698                 for (i = zeros; i; i--)
13699                     *s++ = '0';
13700                 Copy(eptr, s, elen, char);
13701                 s += elen;
13702                 for (i = gap; i; i--)
13703                     *s++ = ' ';
13704             }
13705             else {
13706                 if (fill) {
13707                     for (i = 0; i < esignlen; i++)
13708                         *s++ = esignbuf[i];
13709                     assert(!zeros);
13710                     zeros = gap;
13711                 }
13712                 else {
13713                     for (i = gap; i; i--)
13714                         *s++ = ' ';
13715                     for (i = 0; i < esignlen; i++)
13716                         *s++ = esignbuf[i];
13717                 }
13718
13719                 for (i = zeros; i; i--)
13720                     *s++ = '0';
13721                 Copy(eptr, s, elen, char);
13722                 s += elen;
13723             }
13724
13725             *s = '\0';
13726             SvCUR_set(sv, s - SvPVX_const(sv));
13727
13728             if (is_utf8)
13729                 has_utf8 = TRUE;
13730             if (has_utf8)
13731                 SvUTF8_on(sv);
13732         }
13733
13734         if (vectorize && veclen) {
13735             /* we append the vector separator separately since %v isn't
13736              * very common: don't slow down the general case by adding
13737              * dotstrlen to need etc */
13738             sv_catpvn_nomg(sv, dotstr, dotstrlen);
13739             esignlen = 0;
13740             goto vector; /* do next iteration */
13741         }
13742
13743       done_valid_conversion:
13744
13745         if (arg_missing)
13746             S_warn_vcatpvfn_missing_argument(aTHX);
13747     }
13748
13749     /* Now that we've consumed all our printf format arguments (svix)
13750      * do we have things left on the stack that we didn't use?
13751      */
13752     if (!no_redundant_warning && sv_count >= svix + 1 && ckWARN(WARN_REDUNDANT)) {
13753         Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
13754                 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13755     }
13756
13757     if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
13758         /* while we shouldn't set the cache, it may have been previously
13759            set in the caller, so clear it */
13760         MAGIC *mg = mg_find(sv, PERL_MAGIC_utf8);
13761         if (mg)
13762             magic_setutf8(sv,mg); /* clear UTF8 cache */
13763     }
13764     SvTAINT(sv);
13765 }
13766
13767 /* =========================================================================
13768
13769 =for apidoc_section $embedding
13770
13771 =cut
13772
13773 All the macros and functions in this section are for the private use of
13774 the main function, perl_clone().
13775
13776 The foo_dup() functions make an exact copy of an existing foo thingy.
13777 During the course of a cloning, a hash table is used to map old addresses
13778 to new addresses.  The table is created and manipulated with the
13779 ptr_table_* functions.
13780
13781  * =========================================================================*/
13782
13783
13784 #if defined(USE_ITHREADS)
13785
13786 /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
13787 #ifndef GpREFCNT_inc
13788 #  define GpREFCNT_inc(gp)      ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
13789 #endif
13790
13791
13792 #define SAVEPV(p)       ((p) ? savepv(p) : NULL)
13793 #define SAVEPVN(p,n)    ((p) ? savepvn(p,n) : NULL)
13794
13795 /* clone a parser */
13796
13797 yy_parser *
13798 Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
13799 {
13800     yy_parser *parser;
13801
13802     PERL_ARGS_ASSERT_PARSER_DUP;
13803
13804     if (!proto)
13805         return NULL;
13806
13807     /* look for it in the table first */
13808     parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
13809     if (parser)
13810         return parser;
13811
13812     /* create anew and remember what it is */
13813     Newxz(parser, 1, yy_parser);
13814     ptr_table_store(PL_ptr_table, proto, parser);
13815
13816     /* XXX eventually, just Copy() most of the parser struct ? */
13817
13818     parser->lex_brackets = proto->lex_brackets;
13819     parser->lex_casemods = proto->lex_casemods;
13820     parser->lex_brackstack = savepvn(proto->lex_brackstack,
13821                     (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
13822     parser->lex_casestack = savepvn(proto->lex_casestack,
13823                     (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
13824     parser->lex_defer   = proto->lex_defer;
13825     parser->lex_dojoin  = proto->lex_dojoin;
13826     parser->lex_formbrack = proto->lex_formbrack;
13827     parser->lex_inpat   = proto->lex_inpat;
13828     parser->lex_inwhat  = proto->lex_inwhat;
13829     parser->lex_op      = proto->lex_op;
13830     parser->lex_repl    = sv_dup_inc(proto->lex_repl, param);
13831     parser->lex_starts  = proto->lex_starts;
13832     parser->lex_stuff   = sv_dup_inc(proto->lex_stuff, param);
13833     parser->multi_close = proto->multi_close;
13834     parser->multi_open  = proto->multi_open;
13835     parser->multi_start = proto->multi_start;
13836     parser->multi_end   = proto->multi_end;
13837     parser->preambled   = proto->preambled;
13838     parser->lex_super_state = proto->lex_super_state;
13839     parser->lex_sub_inwhat  = proto->lex_sub_inwhat;
13840     parser->lex_sub_op  = proto->lex_sub_op;
13841     parser->lex_sub_repl= sv_dup_inc(proto->lex_sub_repl, param);
13842     parser->linestr     = sv_dup_inc(proto->linestr, param);
13843     parser->expect      = proto->expect;
13844     parser->copline     = proto->copline;
13845     parser->last_lop_op = proto->last_lop_op;
13846     parser->lex_state   = proto->lex_state;
13847     parser->rsfp        = fp_dup(proto->rsfp, '<', param);
13848     /* rsfp_filters entries have fake IoDIRP() */
13849     parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
13850     parser->in_my       = proto->in_my;
13851     parser->in_my_stash = hv_dup(proto->in_my_stash, param);
13852     parser->error_count = proto->error_count;
13853     parser->sig_elems   = proto->sig_elems;
13854     parser->sig_optelems= proto->sig_optelems;
13855     parser->sig_slurpy  = proto->sig_slurpy;
13856     parser->recheck_utf8_validity = proto->recheck_utf8_validity;
13857
13858     {
13859         char * const ols = SvPVX(proto->linestr);
13860         char * const ls  = SvPVX(parser->linestr);
13861
13862         parser->bufptr      = ls + (proto->bufptr >= ols ?
13863                                     proto->bufptr -  ols : 0);
13864         parser->oldbufptr   = ls + (proto->oldbufptr >= ols ?
13865                                     proto->oldbufptr -  ols : 0);
13866         parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
13867                                     proto->oldoldbufptr -  ols : 0);
13868         parser->linestart   = ls + (proto->linestart >= ols ?
13869                                     proto->linestart -  ols : 0);
13870         parser->last_uni    = ls + (proto->last_uni >= ols ?
13871                                     proto->last_uni -  ols : 0);
13872         parser->last_lop    = ls + (proto->last_lop >= ols ?
13873                                     proto->last_lop -  ols : 0);
13874
13875         parser->bufend      = ls + SvCUR(parser->linestr);
13876     }
13877
13878     Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
13879
13880
13881     Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
13882     Copy(proto->nexttype, parser->nexttype, 5,  I32);
13883     parser->nexttoke    = proto->nexttoke;
13884
13885     /* XXX should clone saved_curcop here, but we aren't passed
13886      * proto_perl; so do it in perl_clone_using instead */
13887
13888     return parser;
13889 }
13890
13891 /*
13892 =for apidoc_section $io
13893 =for apidoc fp_dup
13894
13895 Duplicate a file handle, returning a pointer to the cloned object.
13896
13897 =cut
13898 */
13899
13900 PerlIO *
13901 Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
13902 {
13903     PerlIO *ret;
13904
13905     PERL_ARGS_ASSERT_FP_DUP;
13906     PERL_UNUSED_ARG(type);
13907
13908     if (!fp)
13909         return (PerlIO*)NULL;
13910
13911     /* look for it in the table first */
13912     ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
13913     if (ret)
13914         return ret;
13915
13916     /* create anew and remember what it is */
13917 #ifdef __amigaos4__
13918     ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE|PERLIO_DUP_FD);
13919 #else
13920     ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
13921 #endif
13922     ptr_table_store(PL_ptr_table, fp, ret);
13923     return ret;
13924 }
13925
13926 /*
13927 =for apidoc_section $io
13928 =for apidoc dirp_dup
13929
13930 Duplicate a directory handle, returning a pointer to the cloned object.
13931
13932 =cut
13933 */
13934
13935 DIR *
13936 Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
13937 {
13938     DIR *ret;
13939
13940 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13941     DIR *pwd;
13942     const Direntry_t *dirent;
13943     char smallbuf[256]; /* XXX MAXPATHLEN, surely? */
13944     char *name = NULL;
13945     STRLEN len = 0;
13946     long pos;
13947 #endif
13948
13949     PERL_UNUSED_CONTEXT;
13950     PERL_ARGS_ASSERT_DIRP_DUP;
13951
13952     if (!dp)
13953         return (DIR*)NULL;
13954
13955     /* look for it in the table first */
13956     ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp);
13957     if (ret)
13958         return ret;
13959
13960 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13961
13962     PERL_UNUSED_ARG(param);
13963
13964     /* create anew */
13965
13966     /* open the current directory (so we can switch back) */
13967     if (!(pwd = PerlDir_open("."))) return (DIR *)NULL;
13968
13969     /* chdir to our dir handle and open the present working directory */
13970     if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) {
13971         PerlDir_close(pwd);
13972         return (DIR *)NULL;
13973     }
13974     /* Now we should have two dir handles pointing to the same dir. */
13975
13976     /* Be nice to the calling code and chdir back to where we were. */
13977     /* XXX If this fails, then what? */
13978     PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
13979
13980     /* We have no need of the pwd handle any more. */
13981     PerlDir_close(pwd);
13982
13983 #ifdef DIRNAMLEN
13984 # define d_namlen(d) (d)->d_namlen
13985 #else
13986 # define d_namlen(d) strlen((d)->d_name)
13987 #endif
13988     /* Iterate once through dp, to get the file name at the current posi-
13989        tion. Then step back. */
13990     pos = PerlDir_tell(dp);
13991     if ((dirent = PerlDir_read(dp))) {
13992         len = d_namlen(dirent);
13993         if (len > sizeof(dirent->d_name) && sizeof(dirent->d_name) > PTRSIZE) {
13994             /* If the len is somehow magically longer than the
13995              * maximum length of the directory entry, even though
13996              * we could fit it in a buffer, we could not copy it
13997              * from the dirent.  Bail out. */
13998             PerlDir_close(ret);
13999             return (DIR*)NULL;
14000         }
14001         if (len <= sizeof smallbuf) name = smallbuf;
14002         else Newx(name, len, char);
14003         Move(dirent->d_name, name, len, char);
14004     }
14005     PerlDir_seek(dp, pos);
14006
14007     /* Iterate through the new dir handle, till we find a file with the
14008        right name. */
14009     if (!dirent) /* just before the end */
14010         for(;;) {
14011             pos = PerlDir_tell(ret);
14012             if (PerlDir_read(ret)) continue; /* not there yet */
14013             PerlDir_seek(ret, pos); /* step back */
14014             break;
14015         }
14016     else {
14017         const long pos0 = PerlDir_tell(ret);
14018         for(;;) {
14019             pos = PerlDir_tell(ret);
14020             if ((dirent = PerlDir_read(ret))) {
14021                 if (len == (STRLEN)d_namlen(dirent)
14022                     && memEQ(name, dirent->d_name, len)) {
14023                     /* found it */
14024                     PerlDir_seek(ret, pos); /* step back */
14025                     break;
14026                 }
14027                 /* else we are not there yet; keep iterating */
14028             }
14029             else { /* This is not meant to happen. The best we can do is
14030                       reset the iterator to the beginning. */
14031                 PerlDir_seek(ret, pos0);
14032                 break;
14033             }
14034         }
14035     }
14036 #undef d_namlen
14037
14038     if (name && name != smallbuf)
14039         Safefree(name);
14040 #endif
14041
14042 #ifdef WIN32
14043     ret = win32_dirp_dup(dp, param);
14044 #endif
14045
14046     /* pop it in the pointer table */
14047     if (ret)
14048         ptr_table_store(PL_ptr_table, dp, ret);
14049
14050     return ret;
14051 }
14052
14053 /*
14054 =for apidoc_section $GV
14055 =for apidoc gp_dup
14056
14057 Duplicate a typeglob, returning a pointer to the cloned object.
14058
14059 =cut
14060 */
14061
14062 GP *
14063 Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
14064 {
14065     GP *ret;
14066
14067     PERL_ARGS_ASSERT_GP_DUP;
14068
14069     if (!gp)
14070         return (GP*)NULL;
14071     /* look for it in the table first */
14072     ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
14073     if (ret)
14074         return ret;
14075
14076     /* create anew and remember what it is */
14077     Newxz(ret, 1, GP);
14078     ptr_table_store(PL_ptr_table, gp, ret);
14079
14080     /* clone */
14081     /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
14082        on Newxz() to do this for us.  */
14083     ret->gp_sv          = sv_dup_inc(gp->gp_sv, param);
14084     ret->gp_io          = io_dup_inc(gp->gp_io, param);
14085     ret->gp_form        = cv_dup_inc(gp->gp_form, param);
14086     ret->gp_av          = av_dup_inc(gp->gp_av, param);
14087     ret->gp_hv          = hv_dup_inc(gp->gp_hv, param);
14088     ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
14089     ret->gp_cv          = cv_dup_inc(gp->gp_cv, param);
14090     ret->gp_cvgen       = gp->gp_cvgen;
14091     ret->gp_line        = gp->gp_line;
14092     ret->gp_file_hek    = hek_dup(gp->gp_file_hek, param);
14093     return ret;
14094 }
14095
14096
14097 /*
14098 =for apidoc_section $magic
14099 =for apidoc mg_dup
14100
14101 Duplicate a chain of magic, returning a pointer to the cloned object.
14102
14103 =cut
14104 */
14105
14106 MAGIC *
14107 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
14108 {
14109     MAGIC *mgret = NULL;
14110     MAGIC **mgprev_p = &mgret;
14111
14112     PERL_ARGS_ASSERT_MG_DUP;
14113
14114     for (; mg; mg = mg->mg_moremagic) {
14115         MAGIC *nmg;
14116
14117         if ((param->flags & CLONEf_JOIN_IN)
14118                 && mg->mg_type == PERL_MAGIC_backref)
14119             /* when joining, we let the individual SVs add themselves to
14120              * backref as needed. */
14121             continue;
14122
14123         Newx(nmg, 1, MAGIC);
14124         *mgprev_p = nmg;
14125         mgprev_p = &(nmg->mg_moremagic);
14126
14127         /* There was a comment "XXX copy dynamic vtable?" but as we don't have
14128            dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
14129            from the original commit adding Perl_mg_dup() - revision 4538.
14130            Similarly there is the annotation "XXX random ptr?" next to the
14131            assignment to nmg->mg_ptr.  */
14132         *nmg = *mg;
14133
14134         /* FIXME for plugins
14135         if (nmg->mg_type == PERL_MAGIC_qr) {
14136             nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
14137         }
14138         else
14139         */
14140         nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
14141                           ? nmg->mg_type == PERL_MAGIC_backref
14142                                 /* The backref AV has its reference
14143                                  * count deliberately bumped by 1 */
14144                                 ? SvREFCNT_inc(av_dup_inc((const AV *)
14145                                                     nmg->mg_obj, param))
14146                                 : sv_dup_inc(nmg->mg_obj, param)
14147                           : (nmg->mg_type == PERL_MAGIC_regdatum ||
14148                              nmg->mg_type == PERL_MAGIC_regdata)
14149                                   ? nmg->mg_obj
14150                                   : sv_dup(nmg->mg_obj, param);
14151
14152         if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
14153             if (nmg->mg_len > 0) {
14154                 nmg->mg_ptr     = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
14155                 if (nmg->mg_type == PERL_MAGIC_overload_table &&
14156                         AMT_AMAGIC((AMT*)nmg->mg_ptr))
14157                 {
14158                     AMT * const namtp = (AMT*)nmg->mg_ptr;
14159                     sv_dup_inc_multiple((SV**)(namtp->table),
14160                                         (SV**)(namtp->table), NofAMmeth, param);
14161                 }
14162             }
14163             else if (nmg->mg_len == HEf_SVKEY)
14164                 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
14165         }
14166         if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
14167             nmg->mg_virtual->svt_dup(aTHX_ nmg, param);
14168         }
14169     }
14170     return mgret;
14171 }
14172
14173 #endif /* USE_ITHREADS */
14174
14175 struct ptr_tbl_arena {
14176     struct ptr_tbl_arena *next;
14177     struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers.  */
14178 };
14179
14180 /*
14181 =for apidoc_section $embedding
14182 =for apidoc ptr_table_new
14183
14184 Create a new pointer-mapping table
14185
14186 =cut
14187 */
14188
14189 PTR_TBL_t *
14190 Perl_ptr_table_new(pTHX)
14191 {
14192     PTR_TBL_t *tbl;
14193     PERL_UNUSED_CONTEXT;
14194
14195     Newx(tbl, 1, PTR_TBL_t);
14196     tbl->tbl_max        = 511;
14197     tbl->tbl_items      = 0;
14198     tbl->tbl_arena      = NULL;
14199     tbl->tbl_arena_next = NULL;
14200     tbl->tbl_arena_end  = NULL;
14201     Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
14202     return tbl;
14203 }
14204
14205 #define PTR_TABLE_HASH(ptr) \
14206   ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
14207
14208 /* map an existing pointer using a table */
14209
14210 STATIC PTR_TBL_ENT_t *
14211 S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
14212 {
14213     PTR_TBL_ENT_t *tblent;
14214     const UV hash = PTR_TABLE_HASH(sv);
14215
14216     PERL_ARGS_ASSERT_PTR_TABLE_FIND;
14217
14218     tblent = tbl->tbl_ary[hash & tbl->tbl_max];
14219     for (; tblent; tblent = tblent->next) {
14220         if (tblent->oldval == sv)
14221             return tblent;
14222     }
14223     return NULL;
14224 }
14225
14226 /*
14227 =for apidoc ptr_table_fetch
14228
14229 Look for C<sv> in the pointer-mapping table C<tbl>, returning its value, or
14230 NULL if not found.
14231
14232 =cut
14233 */
14234
14235 void *
14236 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
14237 {
14238     PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
14239
14240     PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
14241     PERL_UNUSED_CONTEXT;
14242
14243     return tblent ? tblent->newval : NULL;
14244 }
14245
14246 /*
14247 =for apidoc ptr_table_store
14248
14249 Add a new entry to a pointer-mapping table C<tbl>.
14250 In hash terms, C<oldsv> is the key; Cnewsv> is the value.
14251
14252 The names "old" and "new" are specific to the core's typical use of ptr_tables
14253 in thread cloning.
14254
14255 =cut
14256 */
14257
14258 void
14259 Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
14260 {
14261     PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
14262
14263     PERL_ARGS_ASSERT_PTR_TABLE_STORE;
14264     PERL_UNUSED_CONTEXT;
14265
14266     if (tblent) {
14267         tblent->newval = newsv;
14268     } else {
14269         const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
14270
14271         if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
14272             struct ptr_tbl_arena *new_arena;
14273
14274             Newx(new_arena, 1, struct ptr_tbl_arena);
14275             new_arena->next = tbl->tbl_arena;
14276             tbl->tbl_arena = new_arena;
14277             tbl->tbl_arena_next = new_arena->array;
14278             tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
14279         }
14280
14281         tblent = tbl->tbl_arena_next++;
14282
14283         tblent->oldval = oldsv;
14284         tblent->newval = newsv;
14285         tblent->next = tbl->tbl_ary[entry];
14286         tbl->tbl_ary[entry] = tblent;
14287         tbl->tbl_items++;
14288         if (tblent->next && tbl->tbl_items > tbl->tbl_max)
14289             ptr_table_split(tbl);
14290     }
14291 }
14292
14293 /*
14294 =for apidoc ptr_table_split
14295
14296 Double the hash bucket size of an existing ptr table
14297
14298 =cut
14299 */
14300
14301 void
14302 Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
14303 {
14304     PTR_TBL_ENT_t **ary = tbl->tbl_ary;
14305     const UV oldsize = tbl->tbl_max + 1;
14306     UV newsize = oldsize * 2;
14307     UV i;
14308
14309     PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
14310     PERL_UNUSED_CONTEXT;
14311
14312     Renew(ary, newsize, PTR_TBL_ENT_t*);
14313     Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
14314     tbl->tbl_max = --newsize;
14315     tbl->tbl_ary = ary;
14316     for (i=0; i < oldsize; i++, ary++) {
14317         PTR_TBL_ENT_t **entp = ary;
14318         PTR_TBL_ENT_t *ent = *ary;
14319         PTR_TBL_ENT_t **curentp;
14320         if (!ent)
14321             continue;
14322         curentp = ary + oldsize;
14323         do {
14324             if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
14325                 *entp = ent->next;
14326                 ent->next = *curentp;
14327                 *curentp = ent;
14328             }
14329             else
14330                 entp = &ent->next;
14331             ent = *entp;
14332         } while (ent);
14333     }
14334 }
14335
14336 /*
14337 =for apidoc ptr_table_free
14338
14339 Clear and free a ptr table
14340
14341 =cut
14342 */
14343
14344 void
14345 Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
14346 {
14347     struct ptr_tbl_arena *arena;
14348
14349     PERL_UNUSED_CONTEXT;
14350
14351     if (!tbl) {
14352         return;
14353     }
14354
14355     arena = tbl->tbl_arena;
14356
14357     while (arena) {
14358         struct ptr_tbl_arena *next = arena->next;
14359
14360         Safefree(arena);
14361         arena = next;
14362     }
14363
14364     Safefree(tbl->tbl_ary);
14365     Safefree(tbl);
14366 }
14367
14368 #if defined(USE_ITHREADS)
14369
14370 void
14371 Perl_rvpv_dup(pTHX_ SV *const dsv, const SV *const ssv, CLONE_PARAMS *const param)
14372 {
14373     PERL_ARGS_ASSERT_RVPV_DUP;
14374
14375     assert(!isREGEXP(ssv));
14376     if (SvROK(ssv)) {
14377         if (SvWEAKREF(ssv)) {
14378             SvRV_set(dsv, sv_dup(SvRV_const(ssv), param));
14379             if (param->flags & CLONEf_JOIN_IN) {
14380                 /* if joining, we add any back references individually rather
14381                  * than copying the whole backref array */
14382                 Perl_sv_add_backref(aTHX_ SvRV(dsv), dsv);
14383             }
14384         }
14385         else
14386             SvRV_set(dsv, sv_dup_inc(SvRV_const(ssv), param));
14387     }
14388     else if (SvPVX_const(ssv)) {
14389         /* Has something there */
14390         if (SvLEN(ssv)) {
14391             /* Normal PV - clone whole allocated space */
14392             SvPV_set(dsv, SAVEPVN(SvPVX_const(ssv), SvLEN(ssv)-1));
14393             /* ssv may not be that normal, but actually copy on write.
14394                But we are a true, independent SV, so:  */
14395             SvIsCOW_off(dsv);
14396         }
14397         else {
14398             /* Special case - not normally malloced for some reason */
14399             if (isGV_with_GP(ssv)) {
14400                 /* Don't need to do anything here.  */
14401             }
14402             else if ((SvIsCOW_shared_hash(ssv))) {
14403                 /* A "shared" PV - clone it as "shared" PV */
14404                 SvPV_set(dsv,
14405                          HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)),
14406                                          param)));
14407             }
14408             else {
14409                 /* Some other special case - random pointer */
14410                 SvPV_set(dsv, (char *) SvPVX_const(ssv));
14411             }
14412         }
14413     }
14414     else {
14415         /* Copy the NULL */
14416         SvPV_set(dsv, NULL);
14417     }
14418 }
14419
14420 /* duplicate a list of SVs. source and dest may point to the same memory.  */
14421 static SV **
14422 S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
14423                       SSize_t items, CLONE_PARAMS *const param)
14424 {
14425     PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
14426
14427     while (items-- > 0) {
14428         *dest++ = sv_dup_inc(*source++, param);
14429     }
14430
14431     return dest;
14432 }
14433
14434 /* duplicate the HvAUX of an HV */
14435 static void
14436 S_sv_dup_hvaux(pTHX_ const SV *const ssv, SV *dsv, CLONE_PARAMS *const param)
14437 {
14438     PERL_ARGS_ASSERT_SV_DUP_HVAUX;
14439
14440     const struct xpvhv_aux * const saux = HvAUX(ssv);
14441     struct xpvhv_aux * const daux = HvAUX(dsv);
14442     /* This flag isn't copied.  */
14443     SvFLAGS(dsv) |= SVphv_HasAUX;
14444
14445     if (saux->xhv_name_count) {
14446         HEK ** const sname = saux->xhv_name_u.xhvnameu_names;
14447         const I32 count = saux->xhv_name_count < 0
14448             ? -saux->xhv_name_count
14449             :  saux->xhv_name_count;
14450         HEK **shekp = sname + count;
14451         HEK **dhekp;
14452         Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *);
14453         dhekp = daux->xhv_name_u.xhvnameu_names + count;
14454         while (shekp-- > sname) {
14455             dhekp--;
14456             *dhekp = hek_dup(*shekp, param);
14457         }
14458     }
14459     else {
14460         daux->xhv_name_u.xhvnameu_name = hek_dup(saux->xhv_name_u.xhvnameu_name, param);
14461     }
14462     daux->xhv_name_count = saux->xhv_name_count;
14463
14464     daux->xhv_aux_flags = saux->xhv_aux_flags;
14465 #ifdef PERL_HASH_RANDOMIZE_KEYS
14466     daux->xhv_rand = saux->xhv_rand;
14467     daux->xhv_last_rand = saux->xhv_last_rand;
14468 #endif
14469     daux->xhv_riter = saux->xhv_riter;
14470     daux->xhv_eiter = saux->xhv_eiter ? he_dup(saux->xhv_eiter, FALSE, param) : 0;
14471     /* backref array needs refcnt=2; see sv_add_backref */
14472     daux->xhv_backreferences =
14473         (param->flags & CLONEf_JOIN_IN)
14474             /* when joining, we let the individual GVs and
14475              * CVs add themselves to backref as
14476              * needed. This avoids pulling in stuff
14477              * that isn't required, and simplifies the
14478              * case where stashes aren't cloned back
14479              * if they already exist in the parent
14480              * thread */
14481         ? NULL
14482         : saux->xhv_backreferences
14483             ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV)
14484                 ? MUTABLE_AV(SvREFCNT_inc(
14485                       sv_dup_inc((const SV *)
14486                         saux->xhv_backreferences, param)))
14487                 : MUTABLE_AV(sv_dup((const SV *)
14488                         saux->xhv_backreferences, param))
14489             : 0;
14490
14491     daux->xhv_mro_meta = saux->xhv_mro_meta
14492         ? mro_meta_dup(saux->xhv_mro_meta, param)
14493         : 0;
14494
14495     /* Record stashes for possible cloning in Perl_clone(). */
14496     if (HvNAME(ssv))
14497         av_push(param->stashes, dsv);
14498
14499     if (HvSTASH_IS_CLASS(ssv)) {
14500         daux->xhv_class_superclass    = hv_dup_inc(saux->xhv_class_superclass,    param);
14501         daux->xhv_class_initfields_cv = cv_dup_inc(saux->xhv_class_initfields_cv, param);
14502         daux->xhv_class_adjust_blocks = av_dup_inc(saux->xhv_class_adjust_blocks, param);
14503         daux->xhv_class_fields        = padnamelist_dup_inc(saux->xhv_class_fields, param);
14504         daux->xhv_class_next_fieldix  = saux->xhv_class_next_fieldix;
14505         daux->xhv_class_param_map     = hv_dup_inc(saux->xhv_class_param_map,     param);
14506
14507         /* TODO: This does mean that we can't compile more `field` expressions
14508          * in the cloned thread, but surely we're done with compiletime now..?
14509          */
14510         daux->xhv_class_suspended_initfields_compcv = NULL;
14511     }
14512 }
14513
14514 /* duplicate an SV of any type (including AV, HV etc) */
14515
14516 static SV *
14517 S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14518 {
14519     SV *dsv;
14520
14521     PERL_ARGS_ASSERT_SV_DUP_COMMON;
14522
14523     if (SvIS_FREED(ssv)) {
14524 #ifdef DEBUG_LEAKING_SCALARS_ABORT
14525         abort();
14526 #endif
14527         return NULL;
14528     }
14529     /* look for it in the table first */
14530     dsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, ssv));
14531     if (dsv)
14532         return dsv;
14533
14534     if(param->flags & CLONEf_JOIN_IN) {
14535         /** We are joining here so we don't want do clone
14536             something that is bad **/
14537         if (SvTYPE(ssv) == SVt_PVHV) {
14538             const HEK * const hvname = HvNAME_HEK(ssv);
14539             if (hvname) {
14540                 /** don't clone stashes if they already exist **/
14541                 dsv = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14542                                                 HEK_UTF8(hvname) ? SVf_UTF8 : 0));
14543                 ptr_table_store(PL_ptr_table, ssv, dsv);
14544                 return dsv;
14545             }
14546         }
14547         else if (SvTYPE(ssv) == SVt_PVGV && !SvFAKE(ssv)) {
14548             HV *stash = GvSTASH(ssv);
14549             const HEK * hvname;
14550             if (stash && (hvname = HvNAME_HEK(stash))) {
14551                 /** don't clone GVs if they already exist **/
14552                 SV **svp;
14553                 stash = gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14554                                     HEK_UTF8(hvname) ? SVf_UTF8 : 0);
14555                 svp = hv_fetch(
14556                         stash, GvNAME(ssv),
14557                         GvNAMEUTF8(ssv)
14558                             ? -GvNAMELEN(ssv)
14559                             :  GvNAMELEN(ssv),
14560                         0
14561                       );
14562                 if (svp && *svp && SvTYPE(*svp) == SVt_PVGV) {
14563                     ptr_table_store(PL_ptr_table, ssv, *svp);
14564                     return *svp;
14565                 }
14566             }
14567         }
14568     }
14569
14570     /* create anew and remember what it is */
14571     new_SV(dsv);
14572
14573 #ifdef DEBUG_LEAKING_SCALARS
14574     dsv->sv_debug_optype = ssv->sv_debug_optype;
14575     dsv->sv_debug_line = ssv->sv_debug_line;
14576     dsv->sv_debug_inpad = ssv->sv_debug_inpad;
14577     dsv->sv_debug_parent = (SV*)ssv;
14578     FREE_SV_DEBUG_FILE(dsv);
14579     dsv->sv_debug_file = savesharedpv(ssv->sv_debug_file);
14580 #endif
14581
14582     ptr_table_store(PL_ptr_table, ssv, dsv);
14583
14584     /* clone */
14585     SvFLAGS(dsv)        = SvFLAGS(ssv);
14586     SvFLAGS(dsv)        &= ~SVf_OOK;            /* don't propagate OOK hack */
14587     SvREFCNT(dsv)       = 0;                    /* must be before any other dups! */
14588
14589 #ifdef DEBUGGING
14590     if (SvANY(ssv) && PL_watch_pvx && SvPVX_const(ssv) == PL_watch_pvx)
14591         PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
14592                       (void*)PL_watch_pvx, SvPVX_const(ssv));
14593 #endif
14594
14595     /* don't clone objects whose class has asked us not to */
14596     if (SvOBJECT(ssv)
14597      && ! (SvFLAGS(SvSTASH(ssv)) & SVphv_CLONEABLE))
14598     {
14599         SvFLAGS(dsv) = 0;
14600         return dsv;
14601     }
14602
14603     switch (SvTYPE(ssv)) {
14604     case SVt_NULL:
14605         SvANY(dsv)      = NULL;
14606         break;
14607     case SVt_IV:
14608         SET_SVANY_FOR_BODYLESS_IV(dsv);
14609         if(SvROK(ssv)) {
14610             Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14611         } else {
14612             SvIV_set(dsv, SvIVX(ssv));
14613         }
14614         break;
14615     case SVt_NV:
14616 #if NVSIZE <= IVSIZE
14617         SET_SVANY_FOR_BODYLESS_NV(dsv);
14618 #else
14619         SvANY(dsv)      = new_XNV();
14620 #endif
14621         SvNV_set(dsv, SvNVX(ssv));
14622         break;
14623     default:
14624         {
14625             /* These are all the types that need complex bodies allocating.  */
14626             void *new_body;
14627             const svtype sv_type = SvTYPE(ssv);
14628             const struct body_details *sv_type_details
14629                 = bodies_by_type + sv_type;
14630
14631             switch (sv_type) {
14632             default:
14633                 Perl_croak(param->proto_perl, "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(ssv));
14634                 NOT_REACHED; /* NOTREACHED */
14635                 break;
14636
14637             case SVt_PVHV:
14638                 if (HvHasAUX(ssv)) {
14639                     sv_type_details = &fake_hv_with_aux;
14640 #ifdef PURIFY
14641                     new_body = new_NOARENA(sv_type_details);
14642 #else
14643                     new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
14644 #endif
14645                     goto have_body;
14646                 }
14647                 /* FALLTHROUGH */
14648             case SVt_PVOBJ:
14649             case SVt_PVGV:
14650             case SVt_PVIO:
14651             case SVt_PVFM:
14652             case SVt_PVAV:
14653             case SVt_PVCV:
14654             case SVt_PVLV:
14655             case SVt_REGEXP:
14656             case SVt_PVMG:
14657             case SVt_PVNV:
14658             case SVt_PVIV:
14659             case SVt_INVLIST:
14660             case SVt_PV:
14661                 assert(sv_type_details->body_size);
14662 #ifndef PURIFY
14663                 if (sv_type_details->arena) {
14664                     new_body = S_new_body(aTHX_ sv_type);
14665                     new_body
14666                         = (void*)((char*)new_body - sv_type_details->offset);
14667                 } else
14668 #endif
14669                 {
14670                     new_body = new_NOARENA(sv_type_details);
14671                 }
14672             }
14673         have_body:
14674             assert(new_body);
14675             SvANY(dsv) = new_body;
14676
14677 #ifndef PURIFY
14678             Copy(((char*)SvANY(ssv)) + sv_type_details->offset,
14679                  ((char*)SvANY(dsv)) + sv_type_details->offset,
14680                  sv_type_details->copy, char);
14681 #else
14682             Copy(((char*)SvANY(ssv)),
14683                  ((char*)SvANY(dsv)),
14684                  sv_type_details->body_size + sv_type_details->offset, char);
14685 #endif
14686
14687             if (sv_type != SVt_PVAV && sv_type != SVt_PVHV && sv_type != SVt_PVOBJ
14688                 && !isGV_with_GP(dsv)
14689                 && !isREGEXP(dsv)
14690                 && !(sv_type == SVt_PVIO && !(IoFLAGS(dsv) & IOf_FAKE_DIRP)))
14691                 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14692
14693             /* The Copy above means that all the source (unduplicated) pointers
14694                are now in the destination.  We can check the flags and the
14695                pointers in either, but it's possible that there's less cache
14696                missing by always going for the destination.
14697                FIXME - instrument and check that assumption  */
14698             if (sv_type >= SVt_PVMG) {
14699                 if (SvMAGIC(dsv))
14700                     SvMAGIC_set(dsv, mg_dup(SvMAGIC(dsv), param));
14701                 if (SvOBJECT(dsv) && SvSTASH(dsv))
14702                     SvSTASH_set(dsv, hv_dup_inc(SvSTASH(dsv), param));
14703                 else SvSTASH_set(dsv, 0); /* don't copy DESTROY cache */
14704             }
14705
14706             /* The cast silences a GCC warning about unhandled types.  */
14707             switch ((int)sv_type) {
14708             case SVt_PV:
14709                 break;
14710             case SVt_PVIV:
14711                 break;
14712             case SVt_PVNV:
14713                 break;
14714             case SVt_PVMG:
14715                 break;
14716             case SVt_REGEXP:
14717               duprex:
14718                 /* FIXME for plugins */
14719                 re_dup_guts((REGEXP*) ssv, (REGEXP*) dsv, param);
14720                 break;
14721             case SVt_PVLV:
14722                 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
14723                 if (LvTYPE(dsv) == 't') /* for tie: unrefcnted fake (SV**) */
14724                     LvTARG(dsv) = dsv;
14725                 else if (LvTYPE(dsv) == 'T') /* for tie: fake HE */
14726                     LvTARG(dsv) = MUTABLE_SV(he_dup((HE*)LvTARG(dsv), FALSE, param));
14727                 else
14728                     LvTARG(dsv) = sv_dup_inc(LvTARG(dsv), param);
14729                 if (isREGEXP(ssv)) goto duprex;
14730                 /* FALLTHROUGH */
14731             case SVt_PVGV:
14732                 /* non-GP case already handled above */
14733                 if(isGV_with_GP(ssv)) {
14734                     GvNAME_HEK(dsv) = hek_dup(GvNAME_HEK(dsv), param);
14735                     /* Don't call sv_add_backref here as it's going to be
14736                        created as part of the magic cloning of the symbol
14737                        table--unless this is during a join and the stash
14738                        is not actually being cloned.  */
14739                     /* Danger Will Robinson - GvGP(dsv) isn't initialised
14740                        at the point of this comment.  */
14741                     GvSTASH(dsv) = hv_dup(GvSTASH(dsv), param);
14742                     if (param->flags & CLONEf_JOIN_IN)
14743                         Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
14744                     GvGP_set(dsv, gp_dup(GvGP(ssv), param));
14745                     (void)GpREFCNT_inc(GvGP(dsv));
14746                 }
14747                 break;
14748             case SVt_PVIO:
14749                 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
14750                 if(IoFLAGS(dsv) & IOf_FAKE_DIRP) {
14751                     /* I have no idea why fake dirp (rsfps)
14752                        should be treated differently but otherwise
14753                        we end up with leaks -- sky*/
14754                     IoTOP_GV(dsv)      = gv_dup_inc(IoTOP_GV(dsv), param);
14755                     IoFMT_GV(dsv)      = gv_dup_inc(IoFMT_GV(dsv), param);
14756                     IoBOTTOM_GV(dsv)   = gv_dup_inc(IoBOTTOM_GV(dsv), param);
14757                 } else {
14758                     IoTOP_GV(dsv)      = gv_dup(IoTOP_GV(dsv), param);
14759                     IoFMT_GV(dsv)      = gv_dup(IoFMT_GV(dsv), param);
14760                     IoBOTTOM_GV(dsv)   = gv_dup(IoBOTTOM_GV(dsv), param);
14761                     if (IoDIRP(dsv)) {
14762                         IoDIRP(dsv)     = dirp_dup(IoDIRP(dsv), param);
14763                     } else {
14764                         NOOP;
14765                         /* IoDIRP(dsv) is already a copy of IoDIRP(ssv)  */
14766                     }
14767                     IoIFP(dsv)  = fp_dup(IoIFP(ssv), IoTYPE(dsv), param);
14768                 }
14769                 if (IoOFP(dsv) == IoIFP(ssv))
14770                     IoOFP(dsv) = IoIFP(dsv);
14771                 else
14772                     IoOFP(dsv)  = fp_dup(IoOFP(dsv), IoTYPE(dsv), param);
14773                 IoTOP_NAME(dsv) = SAVEPV(IoTOP_NAME(dsv));
14774                 IoFMT_NAME(dsv) = SAVEPV(IoFMT_NAME(dsv));
14775                 IoBOTTOM_NAME(dsv)      = SAVEPV(IoBOTTOM_NAME(dsv));
14776                 break;
14777             case SVt_PVAV:
14778                 /* avoid cloning an empty array */
14779                 if (AvARRAY((const AV *)ssv) && AvFILLp((const AV *)ssv) >= 0) {
14780                     SV **dst_ary, **src_ary;
14781                     SSize_t items = AvFILLp((const AV *)ssv) + 1;
14782
14783                     src_ary = AvARRAY((const AV *)ssv);
14784                     Newx(dst_ary, AvMAX((const AV *)ssv)+1, SV*);
14785                     ptr_table_store(PL_ptr_table, src_ary, dst_ary);
14786                     AvARRAY(MUTABLE_AV(dsv)) = dst_ary;
14787                     AvALLOC((const AV *)dsv) = dst_ary;
14788                     if (AvREAL((const AV *)ssv)) {
14789                         dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
14790                                                       param);
14791                     }
14792                     else {
14793                         while (items-- > 0)
14794                             *dst_ary++ = sv_dup(*src_ary++, param);
14795                     }
14796                     items = AvMAX((const AV *)ssv) - AvFILLp((const AV *)ssv);
14797                     while (items-- > 0) {
14798                         *dst_ary++ = NULL;
14799                     }
14800                 }
14801                 else {
14802                     AvARRAY(MUTABLE_AV(dsv))    = NULL;
14803                     AvALLOC((const AV *)dsv)    = (SV**)NULL;
14804                     AvMAX(  (const AV *)dsv)    = -1;
14805                     AvFILLp((const AV *)dsv)    = -1;
14806                 }
14807                 break;
14808             case SVt_PVHV:
14809                 if (HvARRAY((const HV *)ssv)) {
14810                     STRLEN i = 0;
14811                     XPVHV * const dxhv = (XPVHV*)SvANY(dsv);
14812                     XPVHV * const sxhv = (XPVHV*)SvANY(ssv);
14813                     char *darray;
14814                     Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1),
14815                         char);
14816                     HvARRAY(dsv) = (HE**)darray;
14817                     while (i <= sxhv->xhv_max) {
14818                         const HE * const source = HvARRAY(ssv)[i];
14819                         HvARRAY(dsv)[i] = source
14820                             ? he_dup(source, FALSE, param) : 0;
14821                         ++i;
14822                     }
14823                     if (HvHasAUX(ssv))
14824                         sv_dup_hvaux(ssv, dsv, param);
14825                 }
14826                 else
14827                     HvARRAY(MUTABLE_HV(dsv)) = NULL;
14828                 break;
14829             case SVt_PVCV:
14830                 if (!(param->flags & CLONEf_COPY_STACKS)) {
14831                     CvDEPTH(dsv) = 0;
14832                 }
14833                 /* FALLTHROUGH */
14834             case SVt_PVFM:
14835                 /* NOTE: not refcounted */
14836                 SvANY(MUTABLE_CV(dsv))->xcv_stash =
14837                     hv_dup(CvSTASH(dsv), param);
14838                 if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dsv))
14839                     Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dsv)), dsv);
14840                 if (!CvISXSUB(dsv)) {
14841                     OP_REFCNT_LOCK;
14842                     CvROOT(dsv) = OpREFCNT_inc(CvROOT(dsv));
14843                     OP_REFCNT_UNLOCK;
14844                     CvSLABBED_off(dsv);
14845                 } else if (CvCONST(dsv)) {
14846                     CvXSUBANY(dsv).any_ptr =
14847                         sv_dup_inc((const SV *)CvXSUBANY(dsv).any_ptr, param);
14848                 } else if (CvREFCOUNTED_ANYSV(dsv)) {
14849                     CvXSUBANY(dsv).any_sv =
14850                         sv_dup_inc((const SV *)CvXSUBANY(dsv).any_sv, param);
14851                 }
14852                 assert(!CvSLABBED(dsv));
14853                 if (CvDYNFILE(dsv)) CvFILE(dsv) = SAVEPV(CvFILE(dsv));
14854                 if (CvNAMED(dsv))
14855                     SvANY((CV *)dsv)->xcv_gv_u.xcv_hek =
14856                         hek_dup(CvNAME_HEK((CV *)ssv), param);
14857                 /* don't dup if copying back - CvGV isn't refcounted, so the
14858                  * duped GV may never be freed. A bit of a hack! DAPM */
14859                 else
14860                   SvANY(MUTABLE_CV(dsv))->xcv_gv_u.xcv_gv =
14861                     CvCVGV_RC(dsv)
14862                     ? gv_dup_inc(CvGV(ssv), param)
14863                     : (param->flags & CLONEf_JOIN_IN)
14864                         ? NULL
14865                         : gv_dup(CvGV(ssv), param);
14866
14867                 if (!CvISXSUB(ssv)) {
14868                     PADLIST * padlist = CvPADLIST(ssv);
14869                     if(padlist)
14870                         padlist = padlist_dup(padlist, param);
14871                     CvPADLIST_set(dsv, padlist);
14872                 } else
14873 /* unthreaded perl can't sv_dup so we don't support unthreaded's CvHSCXT */
14874                     PoisonPADLIST(dsv);
14875
14876                 CvOUTSIDE(dsv)  =
14877                     CvWEAKOUTSIDE(ssv)
14878                     ? cv_dup(    CvOUTSIDE(dsv), param)
14879                     : cv_dup_inc(CvOUTSIDE(dsv), param);
14880                 break;
14881             case SVt_PVOBJ:
14882                 {
14883                     Size_t fieldcount = ObjectMAXFIELD(ssv) + 1;
14884
14885                     Newx(ObjectFIELDS(dsv), fieldcount, SV *);
14886                     ObjectMAXFIELD(dsv) = fieldcount - 1;
14887
14888                     sv_dup_inc_multiple(ObjectFIELDS(ssv), ObjectFIELDS(dsv), fieldcount, param);
14889                 }
14890                 break;
14891             }
14892         }
14893     }
14894
14895     return dsv;
14896  }
14897
14898 SV *
14899 Perl_sv_dup_inc(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14900 {
14901     PERL_ARGS_ASSERT_SV_DUP_INC;
14902     return ssv ? SvREFCNT_inc(sv_dup_common(ssv, param)) : NULL;
14903 }
14904
14905 SV *
14906 Perl_sv_dup(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14907 {
14908     SV *dsv = ssv ? sv_dup_common(ssv, param) : NULL;
14909     PERL_ARGS_ASSERT_SV_DUP;
14910
14911     /* Track every SV that (at least initially) had a reference count of 0.
14912        We need to do this by holding an actual reference to it in this array.
14913        If we attempt to cheat, turn AvREAL_off(), and store only pointers
14914        (akin to the stashes hash, and the perl stack), we come unstuck if
14915        a weak reference (or other SV legitimately SvREFCNT() == 0 for this
14916        thread) is manipulated in a CLONE method, because CLONE runs before the
14917        unreferenced array is walked to find SVs still with SvREFCNT() == 0
14918        (and fix things up by giving each a reference via the temps stack).
14919        Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and
14920        then SvREFCNT_dec(), it will be cleaned up (and added to the free list)
14921        before the walk of unreferenced happens and a reference to that is SV
14922        added to the temps stack. At which point we have the same SV considered
14923        to be in use, and free to be re-used. Not good.
14924     */
14925     if (dsv && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dsv)) {
14926         assert(param->unreferenced);
14927         av_push(param->unreferenced, SvREFCNT_inc(dsv));
14928     }
14929
14930     return dsv;
14931 }
14932
14933 /* duplicate a context */
14934
14935 PERL_CONTEXT *
14936 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
14937 {
14938     PERL_CONTEXT *ncxs;
14939
14940     PERL_ARGS_ASSERT_CX_DUP;
14941
14942     if (!cxs)
14943         return (PERL_CONTEXT*)NULL;
14944
14945     /* look for it in the table first */
14946     ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
14947     if (ncxs)
14948         return ncxs;
14949
14950     /* create anew and remember what it is */
14951     Newx(ncxs, max + 1, PERL_CONTEXT);
14952     ptr_table_store(PL_ptr_table, cxs, ncxs);
14953     Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
14954
14955     while (ix >= 0) {
14956         PERL_CONTEXT * const ncx = &ncxs[ix];
14957         if (CxTYPE(ncx) == CXt_SUBST) {
14958             Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
14959         }
14960         else {
14961             ncx->blk_oldcop = (COP*)any_dup(ncx->blk_oldcop, param->proto_perl);
14962             switch (CxTYPE(ncx)) {
14963             case CXt_SUB:
14964                 ncx->blk_sub.cv         = cv_dup_inc(ncx->blk_sub.cv, param);
14965                 if(CxHASARGS(ncx)){
14966                     ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,param);
14967                 } else {
14968                     ncx->blk_sub.savearray = NULL;
14969                 }
14970                 ncx->blk_sub.prevcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
14971                                            ncx->blk_sub.prevcomppad);
14972                 break;
14973             case CXt_EVAL:
14974                 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
14975                                                       param);
14976                 /* XXX should this sv_dup_inc? Or only if CxEVAL_TXT_REFCNTED ???? */
14977                 ncx->blk_eval.cur_text  = sv_dup(ncx->blk_eval.cur_text, param);
14978                 ncx->blk_eval.cv = cv_dup(ncx->blk_eval.cv, param);
14979                 /* XXX what to do with cur_top_env ???? */
14980                 break;
14981             case CXt_LOOP_LAZYSV:
14982                 ncx->blk_loop.state_u.lazysv.end
14983                     = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
14984                 /* Fallthrough: duplicate lazysv.cur by using the ary.ary
14985                    duplication code instead.
14986                    We are taking advantage of (1) av_dup_inc and sv_dup_inc
14987                    actually being the same function, and (2) order
14988                    equivalence of the two unions.
14989                    We can assert the later [but only at run time :-(]  */
14990                 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
14991                         (void *) &ncx->blk_loop.state_u.lazysv.cur);
14992                 /* FALLTHROUGH */
14993             case CXt_LOOP_ARY:
14994                 ncx->blk_loop.state_u.ary.ary
14995                     = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
14996                 /* FALLTHROUGH */
14997             case CXt_LOOP_LIST:
14998             case CXt_LOOP_LAZYIV:
14999                 /* code common to all 'for' CXt_LOOP_* types */
15000                 ncx->blk_loop.itersave =
15001                                     sv_dup_inc(ncx->blk_loop.itersave, param);
15002                 if (CxPADLOOP(ncx)) {
15003                     PADOFFSET off = ncx->blk_loop.itervar_u.svp
15004                                     - &CX_CURPAD_SV(ncx->blk_loop, 0);
15005                     ncx->blk_loop.oldcomppad =
15006                                     (PAD*)ptr_table_fetch(PL_ptr_table,
15007                                                 ncx->blk_loop.oldcomppad);
15008                     ncx->blk_loop.itervar_u.svp =
15009                                     &CX_CURPAD_SV(ncx->blk_loop, off);
15010                 }
15011                 else {
15012                     /* this copies the GV if CXp_FOR_GV, or the SV for an
15013                      * alias (for \$x (...)) - relies on gv_dup being the
15014                      * same as sv_dup */
15015                     ncx->blk_loop.itervar_u.gv
15016                         = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv,
15017                                     param);
15018                 }
15019                 break;
15020             case CXt_LOOP_PLAIN:
15021                 break;
15022             case CXt_FORMAT:
15023                 ncx->blk_format.prevcomppad =
15024                         (PAD*)ptr_table_fetch(PL_ptr_table,
15025                                            ncx->blk_format.prevcomppad);
15026                 ncx->blk_format.cv      = cv_dup_inc(ncx->blk_format.cv, param);
15027                 ncx->blk_format.gv      = gv_dup(ncx->blk_format.gv, param);
15028                 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
15029                                                      param);
15030                 break;
15031             case CXt_GIVEN:
15032                 ncx->blk_givwhen.defsv_save =
15033                                 sv_dup_inc(ncx->blk_givwhen.defsv_save, param);
15034                 break;
15035             case CXt_BLOCK:
15036             case CXt_NULL:
15037             case CXt_WHEN:
15038             case CXt_DEFER:
15039                 break;
15040             }
15041         }
15042         --ix;
15043     }
15044     return ncxs;
15045 }
15046
15047 /*
15048 =for apidoc si_dup
15049
15050 Duplicate a stack info structure, returning a pointer to the cloned object.
15051
15052 =cut
15053 */
15054
15055 PERL_SI *
15056 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
15057 {
15058     PERL_SI *nsi;
15059
15060     PERL_ARGS_ASSERT_SI_DUP;
15061
15062     if (!si)
15063         return (PERL_SI*)NULL;
15064
15065     /* look for it in the table first */
15066     nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
15067     if (nsi)
15068         return nsi;
15069
15070     /* create anew and remember what it is */
15071     Newx(nsi, 1, PERL_SI);
15072     ptr_table_store(PL_ptr_table, si, nsi);
15073
15074     nsi->si_stack       = av_dup_inc(si->si_stack, param);
15075     nsi->si_cxix        = si->si_cxix;
15076     nsi->si_cxsubix     = si->si_cxsubix;
15077     nsi->si_cxmax       = si->si_cxmax;
15078     nsi->si_cxstack     = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
15079     nsi->si_type        = si->si_type;
15080     nsi->si_prev        = si_dup(si->si_prev, param);
15081     nsi->si_next        = si_dup(si->si_next, param);
15082     nsi->si_markoff     = si->si_markoff;
15083 #ifdef PERL_RC_STACK
15084     nsi->si_stack_nonrc_base = si->si_stack_nonrc_base;
15085 #endif
15086 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
15087     nsi->si_stack_hwm   = 0;
15088 #endif
15089
15090     return nsi;
15091 }
15092
15093 #define POPINT(ss,ix)   ((ss)[--(ix)].any_i32)
15094 #define TOPINT(ss,ix)   ((ss)[ix].any_i32)
15095 #define POPLONG(ss,ix)  ((ss)[--(ix)].any_long)
15096 #define TOPLONG(ss,ix)  ((ss)[ix].any_long)
15097 #define POPIV(ss,ix)    ((ss)[--(ix)].any_iv)
15098 #define TOPIV(ss,ix)    ((ss)[ix].any_iv)
15099 #define POPUV(ss,ix)    ((ss)[--(ix)].any_uv)
15100 #define TOPUV(ss,ix)    ((ss)[ix].any_uv)
15101 #define POPBOOL(ss,ix)  ((ss)[--(ix)].any_bool)
15102 #define TOPBOOL(ss,ix)  ((ss)[ix].any_bool)
15103 #define POPPTR(ss,ix)   ((ss)[--(ix)].any_ptr)
15104 #define TOPPTR(ss,ix)   ((ss)[ix].any_ptr)
15105 #define POPDPTR(ss,ix)  ((ss)[--(ix)].any_dptr)
15106 #define TOPDPTR(ss,ix)  ((ss)[ix].any_dptr)
15107 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
15108 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
15109
15110 /* XXXXX todo */
15111 #define pv_dup_inc(p)   SAVEPV(p)
15112 #define pv_dup(p)       SAVEPV(p)
15113 #define svp_dup_inc(p,pp)       any_dup(p,pp)
15114
15115 /* map any object to the new equivalent - either something in the
15116  * ptr table, or something in the interpreter structure
15117  */
15118
15119 void *
15120 Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
15121 {
15122     void *ret;
15123
15124     PERL_ARGS_ASSERT_ANY_DUP;
15125
15126     if (!v)
15127         return (void*)NULL;
15128
15129     /* look for it in the table first */
15130     ret = ptr_table_fetch(PL_ptr_table, v);
15131     if (ret)
15132         return ret;
15133
15134     /* see if it is part of the interpreter structure */
15135     if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
15136         ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
15137     else {
15138         ret = v;
15139     }
15140
15141     return ret;
15142 }
15143
15144 /*
15145 =for apidoc ss_dup
15146
15147 Duplicate the save stack, returning a pointer to the cloned object.
15148
15149 =cut
15150 */
15151
15152 ANY *
15153 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
15154 {
15155     ANY * const ss      = proto_perl->Isavestack;
15156     const I32 max       = proto_perl->Isavestack_max + SS_MAXPUSH;
15157     I32 ix              = proto_perl->Isavestack_ix;
15158     ANY *nss;
15159     const SV *sv;
15160     const GV *gv;
15161     const AV *av;
15162     const HV *hv;
15163     char *pv; /* no const deliberately */
15164     void* ptr;
15165     int intval;
15166     long longval;
15167     GP *gp;
15168     IV iv;
15169     I32 i;
15170     char *c = NULL;
15171     void (*dptr) (void*);
15172     void (*dxptr) (pTHX_ void*);
15173
15174     PERL_ARGS_ASSERT_SS_DUP;
15175
15176     Newx(nss, max, ANY);
15177
15178     while (ix > 0) {
15179         const UV uv = POPUV(ss,ix);
15180         const U8 type = (U8)uv & SAVE_MASK;
15181
15182         TOPUV(nss,ix) = uv;
15183         switch (type) {
15184         case SAVEt_CLEARSV:
15185         case SAVEt_CLEARPADRANGE:
15186             break;
15187         case SAVEt_HELEM:               /* hash element */
15188         case SAVEt_SV:                  /* scalar reference */
15189             sv = (const SV *)POPPTR(ss,ix);
15190             TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15191             /* FALLTHROUGH */
15192         case SAVEt_ITEM:                        /* normal string */
15193         case SAVEt_GVSV:                        /* scalar slot in GV */
15194             sv = (const SV *)POPPTR(ss,ix);
15195             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15196             if (type == SAVEt_SV)
15197                 break;
15198             /* FALLTHROUGH */
15199         case SAVEt_FREESV:
15200         case SAVEt_MORTALIZESV:
15201         case SAVEt_READONLY_OFF:
15202             sv = (const SV *)POPPTR(ss,ix);
15203             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15204             break;
15205         case SAVEt_FREEPADNAME:
15206             ptr = POPPTR(ss,ix);
15207             TOPPTR(nss,ix) = padname_dup((PADNAME *)ptr, param);
15208             PadnameREFCNT((PADNAME *)TOPPTR(nss,ix))++;
15209             break;
15210         case SAVEt_SHARED_PVREF:                /* char* in shared space */
15211             c = (char*)POPPTR(ss,ix);
15212             TOPPTR(nss,ix) = savesharedpv(c);
15213             ptr = POPPTR(ss,ix);
15214             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15215             break;
15216         case SAVEt_GENERIC_SVREF:               /* generic sv */
15217         case SAVEt_SVREF:                       /* scalar reference */
15218             sv = (const SV *)POPPTR(ss,ix);
15219             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15220             if (type == SAVEt_SVREF)
15221                 SvREFCNT_inc_simple_void((SV *)TOPPTR(nss,ix));
15222             ptr = POPPTR(ss,ix);
15223             TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15224             /* this feels very strange, we have a **SV from one thread,
15225              * we copy the SV, but dont change the **SV. But in this thread
15226              * the target of the **SV could be something from the *other* thread.
15227              * So how can this possibly work correctly? */
15228             break;
15229         case SAVEt_RCPV:
15230             pv = (char *)POPPTR(ss,ix);
15231             TOPPTR(nss,ix) = rcpv_copy(pv);
15232             ptr = POPPTR(ss,ix);
15233             (void)rcpv_copy(*((char **)ptr));
15234             TOPPTR(nss,ix) = ptr;
15235             /* XXXXX: see comment above. */
15236             break;
15237         case SAVEt_GVSLOT:              /* any slot in GV */
15238             sv = (const SV *)POPPTR(ss,ix);
15239             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15240             ptr = POPPTR(ss,ix);
15241             TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15242             sv = (const SV *)POPPTR(ss,ix);
15243             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15244             break;
15245         case SAVEt_HV:                          /* hash reference */
15246         case SAVEt_AV:                          /* array reference */
15247             sv = (const SV *) POPPTR(ss,ix);
15248             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15249             /* FALLTHROUGH */
15250         case SAVEt_COMPPAD:
15251         case SAVEt_NSTAB:
15252             sv = (const SV *) POPPTR(ss,ix);
15253             TOPPTR(nss,ix) = sv_dup(sv, param);
15254             break;
15255         case SAVEt_INT:                         /* int reference */
15256             ptr = POPPTR(ss,ix);
15257             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15258             intval = (int)POPINT(ss,ix);
15259             TOPINT(nss,ix) = intval;
15260             break;
15261         case SAVEt_LONG:                        /* long reference */
15262             ptr = POPPTR(ss,ix);
15263             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15264             longval = (long)POPLONG(ss,ix);
15265             TOPLONG(nss,ix) = longval;
15266             break;
15267         case SAVEt_I32:                         /* I32 reference */
15268             ptr = POPPTR(ss,ix);
15269             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15270             i = POPINT(ss,ix);
15271             TOPINT(nss,ix) = i;
15272             break;
15273         case SAVEt_IV:                          /* IV reference */
15274         case SAVEt_STRLEN:                      /* STRLEN/size_t ref */
15275             ptr = POPPTR(ss,ix);
15276             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15277             iv = POPIV(ss,ix);
15278             TOPIV(nss,ix) = iv;
15279             break;
15280         case SAVEt_TMPSFLOOR:
15281             iv = POPIV(ss,ix);
15282             TOPIV(nss,ix) = iv;
15283             break;
15284         case SAVEt_HPTR:                        /* HV* reference */
15285         case SAVEt_APTR:                        /* AV* reference */
15286         case SAVEt_SPTR:                        /* SV* reference */
15287             ptr = POPPTR(ss,ix);
15288             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15289             sv = (const SV *)POPPTR(ss,ix);
15290             TOPPTR(nss,ix) = sv_dup(sv, param);
15291             break;
15292         case SAVEt_VPTR:                        /* random* reference */
15293             ptr = POPPTR(ss,ix);
15294             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15295             /* FALLTHROUGH */
15296         case SAVEt_STRLEN_SMALL:
15297         case SAVEt_INT_SMALL:
15298         case SAVEt_I32_SMALL:
15299         case SAVEt_I16:                         /* I16 reference */
15300         case SAVEt_I8:                          /* I8 reference */
15301         case SAVEt_BOOL:
15302             ptr = POPPTR(ss,ix);
15303             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15304             break;
15305         case SAVEt_GENERIC_PVREF:               /* generic char* */
15306         case SAVEt_PPTR:                        /* char* reference */
15307             ptr = POPPTR(ss,ix);
15308             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15309             c = (char*)POPPTR(ss,ix);
15310             TOPPTR(nss,ix) = pv_dup(c);
15311             break;
15312         case SAVEt_GP:                          /* scalar reference */
15313             gp = (GP*)POPPTR(ss,ix);
15314             TOPPTR(nss,ix) = gp = gp_dup(gp, param);
15315             (void)GpREFCNT_inc(gp);
15316             gv = (const GV *)POPPTR(ss,ix);
15317             TOPPTR(nss,ix) = gv_dup_inc(gv, param);
15318             break;
15319         case SAVEt_FREEOP:
15320             ptr = POPPTR(ss,ix);
15321             if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
15322                 /* these are assumed to be refcounted properly */
15323                 OP *o;
15324                 switch (((OP*)ptr)->op_type) {
15325                 case OP_LEAVESUB:
15326                 case OP_LEAVESUBLV:
15327                 case OP_LEAVEEVAL:
15328                 case OP_LEAVE:
15329                 case OP_SCOPE:
15330                 case OP_LEAVEWRITE:
15331                     TOPPTR(nss,ix) = ptr;
15332                     o = (OP*)ptr;
15333                     OP_REFCNT_LOCK;
15334                     (void) OpREFCNT_inc(o);
15335                     OP_REFCNT_UNLOCK;
15336                     break;
15337                 default:
15338                     TOPPTR(nss,ix) = NULL;
15339                     break;
15340                 }
15341             }
15342             else
15343                 TOPPTR(nss,ix) = NULL;
15344             break;
15345         case SAVEt_FREECOPHH:
15346             ptr = POPPTR(ss,ix);
15347             TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr);
15348             break;
15349         case SAVEt_ADELETE:
15350             av = (const AV *)POPPTR(ss,ix);
15351             TOPPTR(nss,ix) = av_dup_inc(av, param);
15352             i = POPINT(ss,ix);
15353             TOPINT(nss,ix) = i;
15354             break;
15355         case SAVEt_DELETE:
15356             hv = (const HV *)POPPTR(ss,ix);
15357             TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15358             i = POPINT(ss,ix);
15359             TOPINT(nss,ix) = i;
15360             /* FALLTHROUGH */
15361         case SAVEt_FREEPV:
15362             c = (char*)POPPTR(ss,ix);
15363             TOPPTR(nss,ix) = pv_dup_inc(c);
15364             break;
15365         case SAVEt_FREERCPV:
15366             c = (char *)POPPTR(ss,ix);
15367             TOPPTR(nss,ix) = rcpv_copy(c);
15368             break;
15369         case SAVEt_STACK_POS:           /* Position on Perl stack */
15370             i = POPINT(ss,ix);
15371             TOPINT(nss,ix) = i;
15372             break;
15373         case SAVEt_DESTRUCTOR:
15374             ptr = POPPTR(ss,ix);
15375             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);  /* XXX quite arbitrary */
15376             dptr = POPDPTR(ss,ix);
15377             TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
15378                                         any_dup(FPTR2DPTR(void *, dptr),
15379                                                 proto_perl));
15380             break;
15381         case SAVEt_DESTRUCTOR_X:
15382             ptr = POPPTR(ss,ix);
15383             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);  /* XXX quite arbitrary */
15384             dxptr = POPDXPTR(ss,ix);
15385             TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
15386                                          any_dup(FPTR2DPTR(void *, dxptr),
15387                                                  proto_perl));
15388             break;
15389         case SAVEt_REGCONTEXT:
15390         case SAVEt_ALLOC:
15391             ix -= uv >> SAVE_TIGHT_SHIFT;
15392             break;
15393         case SAVEt_AELEM:               /* array element */
15394             sv = (const SV *)POPPTR(ss,ix);
15395             TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15396             iv = POPIV(ss,ix);
15397             TOPIV(nss,ix) = iv;
15398             av = (const AV *)POPPTR(ss,ix);
15399             TOPPTR(nss,ix) = av_dup_inc(av, param);
15400             break;
15401         case SAVEt_OP:
15402             ptr = POPPTR(ss,ix);
15403             TOPPTR(nss,ix) = ptr;
15404             break;
15405         case SAVEt_HINTS_HH:
15406             hv = (const HV *)POPPTR(ss,ix);
15407             TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15408             /* FALLTHROUGH */
15409         case SAVEt_HINTS:
15410             ptr = POPPTR(ss,ix);
15411             ptr = cophh_copy((COPHH*)ptr);
15412             TOPPTR(nss,ix) = ptr;
15413             i = POPINT(ss,ix);
15414             TOPINT(nss,ix) = i;
15415             break;
15416         case SAVEt_PADSV_AND_MORTALIZE:
15417             longval = (long)POPLONG(ss,ix);
15418             TOPLONG(nss,ix) = longval;
15419             ptr = POPPTR(ss,ix);
15420             TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15421             sv = (const SV *)POPPTR(ss,ix);
15422             TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15423             break;
15424         case SAVEt_SET_SVFLAGS:
15425             i = POPINT(ss,ix);
15426             TOPINT(nss,ix) = i;
15427             i = POPINT(ss,ix);
15428             TOPINT(nss,ix) = i;
15429             sv = (const SV *)POPPTR(ss,ix);
15430             TOPPTR(nss,ix) = sv_dup(sv, param);
15431             break;
15432         case SAVEt_CURCOP_WARNINGS:
15433             /* FALLTHROUGH */
15434         case SAVEt_COMPILE_WARNINGS:
15435             ptr = POPPTR(ss,ix);
15436             TOPPTR(nss,ix) = DUP_WARNINGS((char*)ptr);
15437             break;
15438         case SAVEt_PARSER:
15439             ptr = POPPTR(ss,ix);
15440             TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
15441             break;
15442         default:
15443             Perl_croak(aTHX_
15444                        "panic: ss_dup inconsistency (%" IVdf ")", (IV) type);
15445         }
15446     }
15447
15448     return nss;
15449 }
15450
15451
15452 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
15453  * flag to the result. This is done for each stash before cloning starts,
15454  * so we know which stashes want their objects cloned */
15455
15456 static void
15457 do_mark_cloneable_stash(pTHX_ SV *const sv)
15458 {
15459     const HEK * const hvname = HvNAME_HEK((const HV *)sv);
15460     if (hvname) {
15461         GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
15462         SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
15463         if (cloner && GvCV(cloner)) {
15464             dSP;
15465             UV status;
15466
15467             ENTER;
15468             SAVETMPS;
15469             PUSHMARK(SP);
15470             mXPUSHs(newSVhek(hvname));
15471             PUTBACK;
15472             call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
15473             SPAGAIN;
15474             status = POPu;
15475             PUTBACK;
15476             FREETMPS;
15477             LEAVE;
15478             if (status)
15479                 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
15480         }
15481     }
15482 }
15483
15484
15485
15486 /*
15487 =for apidoc perl_clone
15488
15489 Create and return a new interpreter by cloning the current one.
15490
15491 C<perl_clone> takes these flags as parameters:
15492
15493 C<CLONEf_COPY_STACKS> - is used to, well, copy the stacks also,
15494 without it we only clone the data and zero the stacks,
15495 with it we copy the stacks and the new perl interpreter is
15496 ready to run at the exact same point as the previous one.
15497 The pseudo-fork code uses C<COPY_STACKS> while the
15498 threads->create doesn't.
15499
15500 C<CLONEf_KEEP_PTR_TABLE> -
15501 C<perl_clone> keeps a ptr_table with the pointer of the old
15502 variable as a key and the new variable as a value,
15503 this allows it to check if something has been cloned and not
15504 clone it again, but rather just use the value and increase the
15505 refcount.
15506 If C<KEEP_PTR_TABLE> is not set then C<perl_clone> will kill the ptr_table
15507 using the function S<C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>>.
15508 A reason to keep it around is if you want to dup some of your own
15509 variables which are outside the graph that perl scans.
15510
15511 C<CLONEf_CLONE_HOST> -
15512 This is a win32 thing, it is ignored on unix, it tells perl's
15513 win32host code (which is c++) to clone itself, this is needed on
15514 win32 if you want to run two threads at the same time,
15515 if you just want to do some stuff in a separate perl interpreter
15516 and then throw it away and return to the original one,
15517 you don't need to do anything.
15518
15519 =cut
15520 */
15521
15522 /* XXX the above needs expanding by someone who actually understands it ! */
15523 EXTERN_C PerlInterpreter *
15524 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
15525
15526 PerlInterpreter *
15527 perl_clone(PerlInterpreter *proto_perl, UV flags)
15528 {
15529 #ifdef PERL_IMPLICIT_SYS
15530
15531     PERL_ARGS_ASSERT_PERL_CLONE;
15532
15533    /* perlhost.h so we need to call into it
15534    to clone the host, CPerlHost should have a c interface, sky */
15535
15536 #ifndef __amigaos4__
15537    if (flags & CLONEf_CLONE_HOST) {
15538        return perl_clone_host(proto_perl,flags);
15539    }
15540 #endif
15541    return perl_clone_using(proto_perl, flags,
15542                             proto_perl->IMem,
15543                             proto_perl->IMemShared,
15544                             proto_perl->IMemParse,
15545                             proto_perl->IEnv,
15546                             proto_perl->IStdIO,
15547                             proto_perl->ILIO,
15548                             proto_perl->IDir,
15549                             proto_perl->ISock,
15550                             proto_perl->IProc);
15551 }
15552
15553 PerlInterpreter *
15554 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
15555                  struct IPerlMem* ipM, struct IPerlMem* ipMS,
15556                  struct IPerlMem* ipMP, struct IPerlEnv* ipE,
15557                  struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
15558                  struct IPerlDir* ipD, struct IPerlSock* ipS,
15559                  struct IPerlProc* ipP)
15560 {
15561     /* XXX many of the string copies here can be optimized if they're
15562      * constants; they need to be allocated as common memory and just
15563      * their pointers copied. */
15564
15565     IV i;
15566     CLONE_PARAMS clone_params;
15567     CLONE_PARAMS* const param = &clone_params;
15568
15569     PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
15570
15571     PERL_ARGS_ASSERT_PERL_CLONE_USING;
15572 #else           /* !PERL_IMPLICIT_SYS */
15573     IV i;
15574     CLONE_PARAMS clone_params;
15575     CLONE_PARAMS* param = &clone_params;
15576     PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
15577
15578     PERL_ARGS_ASSERT_PERL_CLONE;
15579 #endif          /* PERL_IMPLICIT_SYS */
15580
15581     /* for each stash, determine whether its objects should be cloned */
15582     S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
15583     my_perl->Iphase = PERL_PHASE_CONSTRUCT;
15584     PERL_SET_THX(my_perl);
15585
15586 #ifdef DEBUGGING
15587     PoisonNew(my_perl, 1, PerlInterpreter);
15588     PL_op = NULL;
15589     PL_curcop = NULL;
15590     PL_defstash = NULL; /* may be used by perl malloc() */
15591     PL_markstack = 0;
15592     PL_scopestack = 0;
15593     PL_scopestack_name = 0;
15594     PL_savestack = 0;
15595     PL_savestack_ix = 0;
15596     PL_savestack_max = -1;
15597     PL_sig_pending = 0;
15598     PL_parser = NULL;
15599     PL_eval_begin_nest_depth = proto_perl->Ieval_begin_nest_depth;
15600     Zero(&PL_debug_pad, 1, struct perl_debug_pad);
15601     Zero(&PL_padname_undef, 1, PADNAME);
15602     Zero(&PL_padname_const, 1, PADNAME);
15603 #  ifdef DEBUG_LEAKING_SCALARS
15604     PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000;
15605 #  endif
15606 #  ifdef PERL_TRACE_OPS
15607     Zero(PL_op_exec_cnt, OP_max+2, UV);
15608 #  endif
15609 #else   /* !DEBUGGING */
15610     Zero(my_perl, 1, PerlInterpreter);
15611 #endif  /* DEBUGGING */
15612
15613 #ifdef PERL_IMPLICIT_SYS
15614     /* host pointers */
15615     PL_Mem              = ipM;
15616     PL_MemShared        = ipMS;
15617     PL_MemParse         = ipMP;
15618     PL_Env              = ipE;
15619     PL_StdIO            = ipStd;
15620     PL_LIO              = ipLIO;
15621     PL_Dir              = ipD;
15622     PL_Sock             = ipS;
15623     PL_Proc             = ipP;
15624 #endif          /* PERL_IMPLICIT_SYS */
15625
15626
15627     param->flags = flags;
15628     /* Nothing in the core code uses this, but we make it available to
15629        extensions (using mg_dup).  */
15630     param->proto_perl = proto_perl;
15631     /* Likely nothing will use this, but it is initialised to be consistent
15632        with Perl_clone_params_new().  */
15633     param->new_perl = my_perl;
15634     param->unreferenced = NULL;
15635
15636
15637     INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
15638
15639     PL_body_arenas = NULL;
15640     Zero(&PL_body_roots, 1, PL_body_roots);
15641
15642     PL_sv_count         = 0;
15643     PL_sv_root          = NULL;
15644     PL_sv_arenaroot     = NULL;
15645
15646     PL_debug            = proto_perl->Idebug;
15647
15648     /* dbargs array probably holds garbage */
15649     PL_dbargs           = NULL;
15650
15651     PL_compiling = proto_perl->Icompiling;
15652
15653     /* pseudo environmental stuff */
15654     PL_origargc         = proto_perl->Iorigargc;
15655     PL_origargv         = proto_perl->Iorigargv;
15656
15657 #ifndef NO_TAINT_SUPPORT
15658     /* Set tainting stuff before PerlIO_debug can possibly get called */
15659     PL_tainting         = proto_perl->Itainting;
15660     PL_taint_warn       = proto_perl->Itaint_warn;
15661 #else
15662     PL_tainting         = FALSE;
15663     PL_taint_warn       = FALSE;
15664 #endif
15665
15666     PL_minus_c          = proto_perl->Iminus_c;
15667
15668     PL_localpatches     = proto_perl->Ilocalpatches;
15669     PL_splitstr         = SAVEPV(proto_perl->Isplitstr);
15670     PL_minus_n          = proto_perl->Iminus_n;
15671     PL_minus_p          = proto_perl->Iminus_p;
15672     PL_minus_l          = proto_perl->Iminus_l;
15673     PL_minus_a          = proto_perl->Iminus_a;
15674     PL_minus_E          = proto_perl->Iminus_E;
15675     PL_minus_F          = proto_perl->Iminus_F;
15676     PL_doswitches       = proto_perl->Idoswitches;
15677     PL_dowarn           = proto_perl->Idowarn;
15678 #ifdef PERL_SAWAMPERSAND
15679     PL_sawampersand     = proto_perl->Isawampersand;
15680 #endif
15681     PL_unsafe           = proto_perl->Iunsafe;
15682     PL_perldb           = proto_perl->Iperldb;
15683     PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
15684     PL_exit_flags       = proto_perl->Iexit_flags;
15685
15686     /* XXX time(&PL_basetime) when asked for? */
15687     PL_basetime         = proto_perl->Ibasetime;
15688
15689     PL_maxsysfd         = proto_perl->Imaxsysfd;
15690     PL_statusvalue      = proto_perl->Istatusvalue;
15691 #ifdef __VMS
15692     PL_statusvalue_vms  = proto_perl->Istatusvalue_vms;
15693 #else
15694     PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
15695 #endif
15696
15697     /* RE engine related */
15698     PL_regmatch_slab    = NULL;
15699     PL_reg_curpm        = NULL;
15700
15701     PL_sub_generation   = proto_perl->Isub_generation;
15702
15703     /* funky return mechanisms */
15704     PL_forkprocess      = proto_perl->Iforkprocess;
15705
15706     /* internal state */
15707     PL_main_start       = proto_perl->Imain_start;
15708     PL_eval_root        = proto_perl->Ieval_root;
15709     PL_eval_start       = proto_perl->Ieval_start;
15710
15711     PL_filemode         = proto_perl->Ifilemode;
15712     PL_lastfd           = proto_perl->Ilastfd;
15713     PL_oldname          = proto_perl->Ioldname;         /* XXX not quite right */
15714     PL_gensym           = proto_perl->Igensym;
15715
15716     PL_laststatval      = proto_perl->Ilaststatval;
15717     PL_laststype        = proto_perl->Ilaststype;
15718     PL_mess_sv          = NULL;
15719
15720     PL_profiledata      = NULL;
15721
15722     PL_generation       = proto_perl->Igeneration;
15723
15724     PL_in_clean_objs    = proto_perl->Iin_clean_objs;
15725     PL_in_clean_all     = proto_perl->Iin_clean_all;
15726
15727     PL_delaymagic_uid   = proto_perl->Idelaymagic_uid;
15728     PL_delaymagic_euid  = proto_perl->Idelaymagic_euid;
15729     PL_delaymagic_gid   = proto_perl->Idelaymagic_gid;
15730     PL_delaymagic_egid  = proto_perl->Idelaymagic_egid;
15731     PL_nomemok          = proto_perl->Inomemok;
15732     PL_an               = proto_perl->Ian;
15733     PL_evalseq          = proto_perl->Ievalseq;
15734     PL_origalen         = proto_perl->Iorigalen;
15735
15736     PL_sighandlerp      = proto_perl->Isighandlerp;
15737     PL_sighandler1p     = proto_perl->Isighandler1p;
15738     PL_sighandler3p     = proto_perl->Isighandler3p;
15739
15740     PL_runops           = proto_perl->Irunops;
15741
15742     PL_subline          = proto_perl->Isubline;
15743
15744     PL_cv_has_eval      = proto_perl->Icv_has_eval;
15745     /* Unicode features (see perlrun/-C) */
15746     PL_unicode          = proto_perl->Iunicode;
15747
15748     /* Pre-5.8 signals control */
15749     PL_signals          = proto_perl->Isignals;
15750
15751     /* times() ticks per second */
15752     PL_clocktick        = proto_perl->Iclocktick;
15753
15754     /* Recursion stopper for PerlIO_find_layer */
15755     PL_in_load_module   = proto_perl->Iin_load_module;
15756
15757     /* Not really needed/useful since the reenrant_retint is "volatile",
15758      * but do it for consistency's sake. */
15759     PL_reentrant_retint = proto_perl->Ireentrant_retint;
15760
15761     /* Hooks to shared SVs and locks. */
15762     PL_sharehook        = proto_perl->Isharehook;
15763     PL_lockhook         = proto_perl->Ilockhook;
15764     PL_unlockhook       = proto_perl->Iunlockhook;
15765     PL_threadhook       = proto_perl->Ithreadhook;
15766     PL_destroyhook      = proto_perl->Idestroyhook;
15767     PL_signalhook       = proto_perl->Isignalhook;
15768
15769     PL_globhook         = proto_perl->Iglobhook;
15770
15771     PL_srand_called     = proto_perl->Isrand_called;
15772     Copy(&(proto_perl->Irandom_state), &PL_random_state, 1, PL_RANDOM_STATE_TYPE);
15773     PL_srand_override   = proto_perl->Isrand_override;
15774     PL_srand_override_next = proto_perl->Isrand_override_next;
15775
15776     if (flags & CLONEf_COPY_STACKS) {
15777         /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
15778         PL_tmps_ix              = proto_perl->Itmps_ix;
15779         PL_tmps_max             = proto_perl->Itmps_max;
15780         PL_tmps_floor           = proto_perl->Itmps_floor;
15781
15782         /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
15783          * NOTE: unlike the others! */
15784         PL_scopestack_ix        = proto_perl->Iscopestack_ix;
15785         PL_scopestack_max       = proto_perl->Iscopestack_max;
15786
15787         /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
15788          * NOTE: unlike the others! */
15789         PL_savestack_ix         = proto_perl->Isavestack_ix;
15790         PL_savestack_max        = proto_perl->Isavestack_max;
15791     }
15792
15793     PL_start_env        = proto_perl->Istart_env;       /* XXXXXX */
15794     PL_top_env          = &PL_start_env;
15795
15796     PL_op               = proto_perl->Iop;
15797
15798     PL_Sv               = NULL;
15799     PL_Xpv              = (XPV*)NULL;
15800     my_perl->Ina        = proto_perl->Ina;
15801
15802     PL_statcache        = proto_perl->Istatcache;
15803
15804 #ifndef NO_TAINT_SUPPORT
15805     PL_tainted          = proto_perl->Itainted;
15806 #else
15807     PL_tainted          = FALSE;
15808 #endif
15809     PL_curpm            = proto_perl->Icurpm;   /* XXX No PMOP ref count */
15810
15811     PL_chopset          = proto_perl->Ichopset; /* XXX never deallocated */
15812
15813     PL_restartjmpenv    = proto_perl->Irestartjmpenv;
15814     PL_restartop        = proto_perl->Irestartop;
15815     PL_in_eval          = proto_perl->Iin_eval;
15816     PL_delaymagic       = proto_perl->Idelaymagic;
15817     PL_phase            = proto_perl->Iphase;
15818     PL_localizing       = proto_perl->Ilocalizing;
15819
15820     PL_hv_fetch_ent_mh  = NULL;
15821     PL_modcount         = proto_perl->Imodcount;
15822     PL_lastgotoprobe    = NULL;
15823     PL_dumpindent       = proto_perl->Idumpindent;
15824
15825     PL_efloatbuf        = NULL;         /* reinits on demand */
15826     PL_efloatsize       = 0;                    /* reinits on demand */
15827
15828     /* regex stuff */
15829
15830     PL_colorset         = 0;            /* reinits PL_colors[] */
15831     /*PL_colors[6]      = {0,0,0,0,0,0};*/
15832
15833     /* Pluggable optimizer */
15834     PL_peepp            = proto_perl->Ipeepp;
15835     PL_rpeepp           = proto_perl->Irpeepp;
15836     /* op_free() hook */
15837     PL_opfreehook       = proto_perl->Iopfreehook;
15838
15839 #  ifdef PERL_MEM_LOG
15840     Zero(PL_mem_log, sizeof(PL_mem_log), char);
15841 #  endif
15842
15843 #ifdef USE_REENTRANT_API
15844     /* XXX: things like -Dm will segfault here in perlio, but doing
15845      *  PERL_SET_CONTEXT(proto_perl);
15846      * breaks too many other things
15847      */
15848     Perl_reentrant_init(aTHX);
15849 #endif
15850
15851     /* create SV map for pointer relocation */
15852     PL_ptr_table = ptr_table_new();
15853
15854     /* initialize these special pointers as early as possible */
15855     init_constants();
15856     ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
15857     ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
15858     ptr_table_store(PL_ptr_table, &proto_perl->Isv_zero, &PL_sv_zero);
15859     ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
15860     ptr_table_store(PL_ptr_table, &proto_perl->Ipadname_const,
15861                     &PL_padname_const);
15862
15863     /* create (a non-shared!) shared string table */
15864     PL_strtab           = newHV();
15865     HvSHAREKEYS_off(PL_strtab);
15866     hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
15867     ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
15868
15869     Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
15870
15871     PL_compiling.cop_file    = rcpv_copy(proto_perl->Icompiling.cop_file);
15872
15873     ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
15874     PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
15875     CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling)));
15876     PL_curcop           = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
15877
15878     param->stashes      = newAV();  /* Setup array of objects to call clone on */
15879     /* This makes no difference to the implementation, as it always pushes
15880        and shifts pointers to other SVs without changing their reference
15881        count, with the array becoming empty before it is freed. However, it
15882        makes it conceptually clear what is going on, and will avoid some
15883        work inside av.c, filling slots between AvFILL() and AvMAX() with
15884        &PL_sv_undef, and SvREFCNT_dec()ing those.  */
15885     AvREAL_off(param->stashes);
15886
15887     if (!(flags & CLONEf_COPY_STACKS)) {
15888         param->unreferenced = newAV();
15889     }
15890
15891 #ifdef PERLIO_LAYERS
15892     /* Clone PerlIO tables as soon as we can handle general xx_dup() */
15893     PerlIO_clone(aTHX_ proto_perl, param);
15894 #endif
15895
15896     PL_envgv            = gv_dup_inc(proto_perl->Ienvgv, param);
15897     PL_incgv            = gv_dup_inc(proto_perl->Iincgv, param);
15898     PL_hintgv           = gv_dup_inc(proto_perl->Ihintgv, param);
15899     PL_origfilename     = SAVEPV(proto_perl->Iorigfilename);
15900     PL_xsubfilename     = proto_perl->Ixsubfilename;
15901     PL_diehook          = sv_dup_inc(proto_perl->Idiehook, param);
15902     PL_warnhook         = sv_dup_inc(proto_perl->Iwarnhook, param);
15903
15904     PL_hook__require__before = sv_dup_inc(proto_perl->Ihook__require__before, param);
15905     PL_hook__require__after  = sv_dup_inc(proto_perl->Ihook__require__after, param);
15906
15907     /* switches */
15908     PL_patchlevel       = sv_dup_inc(proto_perl->Ipatchlevel, param);
15909     PL_inplace          = SAVEPV(proto_perl->Iinplace);
15910     PL_e_script         = sv_dup_inc(proto_perl->Ie_script, param);
15911
15912     /* magical thingies */
15913
15914     SvPVCLEAR(PERL_DEBUG_PAD(0));        /* For regex debugging. */
15915     SvPVCLEAR(PERL_DEBUG_PAD(1));        /* ext/re needs these */
15916     SvPVCLEAR(PERL_DEBUG_PAD(2));        /* even without DEBUGGING. */
15917
15918
15919     /* Clone the regex array */
15920     /* ORANGE FIXME for plugins, probably in the SV dup code.
15921        newSViv(PTR2IV(CALLREGDUPE(
15922        INT2PTR(REGEXP *, SvIVX(regex)), param))))
15923     */
15924     PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
15925     PL_regex_pad = AvARRAY(PL_regex_padav);
15926
15927     PL_stashpadmax      = proto_perl->Istashpadmax;
15928     PL_stashpadix       = proto_perl->Istashpadix ;
15929     Newx(PL_stashpad, PL_stashpadmax, HV *);
15930     {
15931         PADOFFSET o = 0;
15932         for (; o < PL_stashpadmax; ++o)
15933             PL_stashpad[o] = hv_dup(proto_perl->Istashpad[o], param);
15934     }
15935
15936     /* shortcuts to various I/O objects */
15937     PL_ofsgv            = gv_dup_inc(proto_perl->Iofsgv, param);
15938     PL_stdingv          = gv_dup(proto_perl->Istdingv, param);
15939     PL_stderrgv         = gv_dup(proto_perl->Istderrgv, param);
15940     PL_defgv            = gv_dup(proto_perl->Idefgv, param);
15941     PL_argvgv           = gv_dup_inc(proto_perl->Iargvgv, param);
15942     PL_argvoutgv        = gv_dup(proto_perl->Iargvoutgv, param);
15943     PL_argvout_stack    = av_dup_inc(proto_perl->Iargvout_stack, param);
15944
15945     /* shortcuts to regexp stuff */
15946     PL_replgv           = gv_dup_inc(proto_perl->Ireplgv, param);
15947
15948     /* shortcuts to misc objects */
15949     PL_errgv            = gv_dup(proto_perl->Ierrgv, param);
15950
15951     /* shortcuts to debugging objects */
15952     PL_DBgv             = gv_dup_inc(proto_perl->IDBgv, param);
15953     PL_DBline           = gv_dup_inc(proto_perl->IDBline, param);
15954     PL_DBsub            = gv_dup_inc(proto_perl->IDBsub, param);
15955     PL_DBsingle         = sv_dup(proto_perl->IDBsingle, param);
15956     PL_DBtrace          = sv_dup(proto_perl->IDBtrace, param);
15957     PL_DBsignal         = sv_dup(proto_perl->IDBsignal, param);
15958     Copy(proto_perl->IDBcontrol, PL_DBcontrol, DBVARMG_COUNT, IV);
15959
15960     /* symbol tables */
15961     PL_defstash         = hv_dup_inc(proto_perl->Idefstash, param);
15962     PL_curstash         = hv_dup_inc(proto_perl->Icurstash, param);
15963     PL_debstash         = hv_dup(proto_perl->Idebstash, param);
15964     PL_globalstash      = hv_dup(proto_perl->Iglobalstash, param);
15965     PL_curstname        = sv_dup_inc(proto_perl->Icurstname, param);
15966
15967     PL_beginav          = av_dup_inc(proto_perl->Ibeginav, param);
15968     PL_beginav_save     = av_dup_inc(proto_perl->Ibeginav_save, param);
15969     PL_checkav_save     = av_dup_inc(proto_perl->Icheckav_save, param);
15970     PL_unitcheckav      = av_dup_inc(proto_perl->Iunitcheckav, param);
15971     PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
15972     PL_endav            = av_dup_inc(proto_perl->Iendav, param);
15973     PL_checkav          = av_dup_inc(proto_perl->Icheckav, param);
15974     PL_initav           = av_dup_inc(proto_perl->Iinitav, param);
15975     PL_savebegin        = proto_perl->Isavebegin;
15976
15977     PL_isarev           = hv_dup_inc(proto_perl->Iisarev, param);
15978
15979     /* subprocess state */
15980     PL_fdpid            = av_dup_inc(proto_perl->Ifdpid, param);
15981
15982     if (proto_perl->Iop_mask)
15983         PL_op_mask      = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
15984     else
15985         PL_op_mask      = NULL;
15986     /* PL_asserting        = proto_perl->Iasserting; */
15987
15988     /* current interpreter roots */
15989     PL_main_cv          = cv_dup_inc(proto_perl->Imain_cv, param);
15990     OP_REFCNT_LOCK;
15991     PL_main_root        = OpREFCNT_inc(proto_perl->Imain_root);
15992     OP_REFCNT_UNLOCK;
15993
15994     /* runtime control stuff */
15995     PL_curcopdb         = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
15996
15997     PL_preambleav       = av_dup_inc(proto_perl->Ipreambleav, param);
15998
15999     PL_ors_sv           = sv_dup_inc(proto_perl->Iors_sv, param);
16000
16001     /* interpreter atexit processing */
16002     PL_exitlistlen      = proto_perl->Iexitlistlen;
16003     if (PL_exitlistlen) {
16004         Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
16005         Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
16006     }
16007     else
16008         PL_exitlist     = (PerlExitListEntry*)NULL;
16009
16010     PL_my_cxt_size = proto_perl->Imy_cxt_size;
16011     if (PL_my_cxt_size) {
16012         Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
16013         Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
16014     }
16015     else {
16016         PL_my_cxt_list  = (void**)NULL;
16017     }
16018     PL_modglobal        = hv_dup_inc(proto_perl->Imodglobal, param);
16019     PL_custom_op_names  = hv_dup_inc(proto_perl->Icustom_op_names,param);
16020     PL_custom_op_descs  = hv_dup_inc(proto_perl->Icustom_op_descs,param);
16021     PL_custom_ops       = hv_dup_inc(proto_perl->Icustom_ops, param);
16022
16023     PL_compcv                   = cv_dup(proto_perl->Icompcv, param);
16024
16025     PAD_CLONE_VARS(proto_perl, param);
16026
16027 #ifdef HAVE_INTERP_INTERN
16028     sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
16029 #endif
16030
16031     PL_DBcv             = cv_dup(proto_perl->IDBcv, param);
16032
16033 #ifdef PERL_USES_PL_PIDSTATUS
16034     PL_pidstatus        = newHV();                      /* XXX flag for cloning? */
16035 #endif
16036     PL_osname           = SAVEPV(proto_perl->Iosname);
16037     PL_parser           = parser_dup(proto_perl->Iparser, param);
16038
16039     /* XXX this only works if the saved cop has already been cloned */
16040     if (proto_perl->Iparser) {
16041         PL_parser->saved_curcop = (COP*)any_dup(
16042                                     proto_perl->Iparser->saved_curcop,
16043                                     proto_perl);
16044     }
16045
16046     PL_subname          = sv_dup_inc(proto_perl->Isubname, param);
16047
16048 #ifdef USE_PL_CURLOCALES
16049     for (i = 0; i < (int) C_ARRAY_LENGTH(PL_curlocales); i++) {
16050         PL_curlocales[i] = SAVEPV("C");
16051     }
16052 #endif
16053 #ifdef USE_PL_CUR_LC_ALL
16054     PL_cur_LC_ALL = SAVEPV("C");
16055 #endif
16056 #ifdef USE_LOCALE_CTYPE
16057     Copy(PL_fold, PL_fold_locale, 256, U8);
16058
16059     /* Should we warn if uses locale? */
16060     PL_ctype_name       = SAVEPV("C");
16061     PL_warn_locale      = sv_dup_inc(proto_perl->Iwarn_locale, param);
16062     PL_in_utf8_CTYPE_locale   = false;
16063     PL_in_utf8_turkic_locale  = false;
16064 #endif
16065
16066     /* Did the locale setup indicate UTF-8? */
16067     PL_utf8locale       = false;
16068
16069 #ifdef USE_LOCALE_COLLATE
16070     PL_in_utf8_COLLATE_locale = false;
16071     PL_collation_name   = SAVEPV("C");
16072     PL_collation_ix     = proto_perl->Icollation_ix;
16073     PL_collation_standard = true;
16074     PL_collxfrm_base    = 0;
16075     PL_collxfrm_mult    = 0;
16076     PL_strxfrm_max_cp   = 0;
16077     PL_strxfrm_is_behaved = proto_perl->Istrxfrm_is_behaved;
16078     PL_strxfrm_NUL_replacement = '\0';
16079 #endif /* USE_LOCALE_COLLATE */
16080
16081 #ifdef USE_LOCALE_THREADS
16082     assert(PL_locale_mutex_depth <= 0);
16083     PL_locale_mutex_depth = 0;
16084 #endif
16085
16086 #ifdef USE_LOCALE_NUMERIC
16087     PL_numeric_name     = SAVEPV("C");
16088     PL_numeric_radix_sv = newSVpvs(".");
16089     PL_underlying_radix_sv = newSVpvs(".");
16090     PL_numeric_standard = true;
16091     PL_numeric_underlying = true;
16092     PL_numeric_underlying_is_standard = true;
16093
16094 #endif /* !USE_LOCALE_NUMERIC */
16095 #if defined(USE_POSIX_2008_LOCALE)
16096     PL_scratch_locale_obj = NULL;
16097     PL_cur_locale_obj = PL_C_locale_obj;
16098 #endif
16099
16100 #ifdef HAS_MBRLEN
16101     PL_mbrlen_ps = proto_perl->Imbrlen_ps;
16102 #endif
16103 #ifdef HAS_MBRTOWC
16104     PL_mbrtowc_ps = proto_perl->Imbrtowc_ps;
16105 #endif
16106 #ifdef HAS_WCRTOMB
16107     PL_wcrtomb_ps = proto_perl->Iwcrtomb_ps;
16108 #endif
16109
16110     PL_langinfo_buf = NULL;
16111     PL_langinfo_bufsize = 0;
16112
16113     PL_setlocale_buf = NULL;
16114     PL_setlocale_bufsize = 0;
16115
16116 #if defined(USE_LOCALE_THREADS) && ! defined(USE_THREAD_SAFE_LOCALE)
16117     PL_less_dicey_locale_buf = NULL;
16118     PL_less_dicey_locale_bufsize = 0;
16119 #endif
16120 #if ! defined(HAS_NL_LANGINFO)          \
16121  &&   defined(USE_LOCALE_CTYPE)         \
16122  && ! defined(WIN32)                    \
16123  && ! defined(HAS_MBTOWC)               \
16124  && ! defined(HAS_MBRTOWC)
16125     PL_langinfo_recursed = 0;
16126 #endif
16127
16128     /* Unicode inversion lists */
16129
16130     PL_AboveLatin1            = sv_dup_inc(proto_perl->IAboveLatin1, param);
16131     PL_Assigned_invlist       = sv_dup_inc(proto_perl->IAssigned_invlist, param);
16132     PL_GCB_invlist            = sv_dup_inc(proto_perl->IGCB_invlist, param);
16133     PL_HasMultiCharFold       = sv_dup_inc(proto_perl->IHasMultiCharFold, param);
16134     PL_InMultiCharFold        = sv_dup_inc(proto_perl->IInMultiCharFold, param);
16135     PL_Latin1                 = sv_dup_inc(proto_perl->ILatin1, param);
16136     PL_LB_invlist             = sv_dup_inc(proto_perl->ILB_invlist, param);
16137     PL_SB_invlist             = sv_dup_inc(proto_perl->ISB_invlist, param);
16138     PL_SCX_invlist            = sv_dup_inc(proto_perl->ISCX_invlist, param);
16139     PL_UpperLatin1            = sv_dup_inc(proto_perl->IUpperLatin1, param);
16140     PL_in_some_fold           = sv_dup_inc(proto_perl->Iin_some_fold, param);
16141     PL_utf8_foldclosures      = sv_dup_inc(proto_perl->Iutf8_foldclosures, param);
16142     PL_utf8_idcont            = sv_dup_inc(proto_perl->Iutf8_idcont, param);
16143     PL_utf8_idstart           = sv_dup_inc(proto_perl->Iutf8_idstart, param);
16144     PL_utf8_perl_idcont       = sv_dup_inc(proto_perl->Iutf8_perl_idcont, param);
16145     PL_utf8_perl_idstart      = sv_dup_inc(proto_perl->Iutf8_perl_idstart, param);
16146     PL_utf8_xidcont           = sv_dup_inc(proto_perl->Iutf8_xidcont, param);
16147     PL_utf8_xidstart          = sv_dup_inc(proto_perl->Iutf8_xidstart, param);
16148     PL_WB_invlist             = sv_dup_inc(proto_perl->IWB_invlist, param);
16149     for (i = 0; i < POSIX_CC_COUNT; i++) {
16150         PL_XPosix_ptrs[i]     = sv_dup_inc(proto_perl->IXPosix_ptrs[i], param);
16151         if (i != CC_CASED_ && i != CC_VERTSPACE_) {
16152             PL_Posix_ptrs[i]  = sv_dup_inc(proto_perl->IPosix_ptrs[i], param);
16153         }
16154     }
16155     PL_Posix_ptrs[CC_CASED_]  = PL_Posix_ptrs[CC_ALPHA_];
16156     PL_Posix_ptrs[CC_VERTSPACE_] = NULL;
16157
16158     PL_utf8_toupper           = sv_dup_inc(proto_perl->Iutf8_toupper, param);
16159     PL_utf8_totitle           = sv_dup_inc(proto_perl->Iutf8_totitle, param);
16160     PL_utf8_tolower           = sv_dup_inc(proto_perl->Iutf8_tolower, param);
16161     PL_utf8_tofold            = sv_dup_inc(proto_perl->Iutf8_tofold, param);
16162     PL_utf8_tosimplefold      = sv_dup_inc(proto_perl->Iutf8_tosimplefold, param);
16163     PL_utf8_charname_begin    = sv_dup_inc(proto_perl->Iutf8_charname_begin, param);
16164     PL_utf8_charname_continue = sv_dup_inc(proto_perl->Iutf8_charname_continue, param);
16165     PL_utf8_mark              = sv_dup_inc(proto_perl->Iutf8_mark, param);
16166     PL_InBitmap               = sv_dup_inc(proto_perl->IInBitmap, param);
16167     PL_CCC_non0_non230        = sv_dup_inc(proto_perl->ICCC_non0_non230, param);
16168     PL_Private_Use            = sv_dup_inc(proto_perl->IPrivate_Use, param);
16169
16170 #if 0
16171     PL_seen_deprecated_macro = hv_dup_inc(proto_perl->Iseen_deprecated_macro, param);
16172 #endif
16173
16174     if (proto_perl->Ipsig_pend) {
16175         Newxz(PL_psig_pend, SIG_SIZE, int);
16176     }
16177     else {
16178         PL_psig_pend    = (int*)NULL;
16179     }
16180
16181     if (proto_perl->Ipsig_name) {
16182         Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
16183         sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
16184                             param);
16185         PL_psig_ptr = PL_psig_name + SIG_SIZE;
16186     }
16187     else {
16188         PL_psig_ptr     = (SV**)NULL;
16189         PL_psig_name    = (SV**)NULL;
16190     }
16191
16192     if (flags & CLONEf_COPY_STACKS) {
16193         Newx(PL_tmps_stack, PL_tmps_max, SV*);
16194         sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack,
16195                             PL_tmps_ix+1, param);
16196
16197         /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
16198         i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
16199         Newx(PL_markstack, i, Stack_off_t);
16200         PL_markstack_max        = PL_markstack + (proto_perl->Imarkstack_max
16201                                                   - proto_perl->Imarkstack);
16202         PL_markstack_ptr        = PL_markstack + (proto_perl->Imarkstack_ptr
16203                                                   - proto_perl->Imarkstack);
16204         Copy(proto_perl->Imarkstack, PL_markstack,
16205              PL_markstack_ptr - PL_markstack + 1, Stack_off_t);
16206
16207         /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
16208          * NOTE: unlike the others! */
16209         Newx(PL_scopestack, PL_scopestack_max, I32);
16210         Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
16211
16212 #ifdef DEBUGGING
16213         Newx(PL_scopestack_name, PL_scopestack_max, const char *);
16214         Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *);
16215 #endif
16216         /* reset stack AV to correct length before its duped via
16217          * PL_curstackinfo */
16218         AvFILLp(proto_perl->Icurstack) =
16219                             proto_perl->Istack_sp - proto_perl->Istack_base;
16220
16221         /* NOTE: si_dup() looks at PL_markstack */
16222         PL_curstackinfo         = si_dup(proto_perl->Icurstackinfo, param);
16223
16224         /* PL_curstack          = PL_curstackinfo->si_stack; */
16225         PL_curstack             = av_dup(proto_perl->Icurstack, param);
16226         PL_mainstack            = av_dup(proto_perl->Imainstack, param);
16227
16228         /* next PUSHs() etc. set *(PL_stack_sp+1) */
16229         PL_stack_base           = AvARRAY(PL_curstack);
16230         PL_stack_sp             = PL_stack_base + (proto_perl->Istack_sp
16231                                                    - proto_perl->Istack_base);
16232         PL_stack_max            = PL_stack_base + AvMAX(PL_curstack);
16233
16234         /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
16235         PL_savestack            = ss_dup(proto_perl, param);
16236     }
16237     else {
16238         init_stacks();
16239         ENTER;                  /* perl_destruct() wants to LEAVE; */
16240     }
16241
16242     PL_statgv           = gv_dup(proto_perl->Istatgv, param);
16243     PL_statname         = sv_dup_inc(proto_perl->Istatname, param);
16244
16245     PL_rs               = sv_dup_inc(proto_perl->Irs, param);
16246     PL_last_in_gv       = gv_dup(proto_perl->Ilast_in_gv, param);
16247     PL_defoutgv         = gv_dup_inc(proto_perl->Idefoutgv, param);
16248     PL_toptarget        = sv_dup_inc(proto_perl->Itoptarget, param);
16249     PL_bodytarget       = sv_dup_inc(proto_perl->Ibodytarget, param);
16250     PL_formtarget       = sv_dup(proto_perl->Iformtarget, param);
16251
16252     PL_errors           = sv_dup_inc(proto_perl->Ierrors, param);
16253
16254     PL_sortcop          = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
16255     PL_firstgv          = gv_dup_inc(proto_perl->Ifirstgv, param);
16256     PL_secondgv         = gv_dup_inc(proto_perl->Isecondgv, param);
16257
16258     PL_stashcache       = newHV();
16259
16260     PL_watchaddr        = (char **) ptr_table_fetch(PL_ptr_table,
16261                                             proto_perl->Iwatchaddr);
16262     PL_watchok          = PL_watchaddr ? * PL_watchaddr : NULL;
16263     if (PL_debug && PL_watchaddr) {
16264         PerlIO_printf(Perl_debug_log,
16265           "WATCHING: %" UVxf " cloned as %" UVxf " with value %" UVxf "\n",
16266           PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
16267           PTR2UV(PL_watchok));
16268     }
16269
16270     PL_registered_mros  = hv_dup_inc(proto_perl->Iregistered_mros, param);
16271     PL_blockhooks       = av_dup_inc(proto_perl->Iblockhooks, param);
16272
16273     /* Call the ->CLONE method, if it exists, for each of the stashes
16274        identified by sv_dup() above.
16275     */
16276     while(av_count(param->stashes) != 0) {
16277         HV* const stash = MUTABLE_HV(av_shift(param->stashes));
16278         GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
16279         if (cloner && GvCV(cloner)) {
16280             ENTER;
16281             SAVETMPS;
16282             PUSHMARK(PL_stack_sp);
16283             rpp_extend(1);
16284             SV *newsv = newSVhek(HvNAME_HEK(stash));
16285             *++PL_stack_sp = newsv;
16286             if (!rpp_stack_is_rc())
16287                 sv_2mortal(newsv);
16288             call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
16289             FREETMPS;
16290             LEAVE;
16291         }
16292     }
16293
16294     if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
16295         ptr_table_free(PL_ptr_table);
16296         PL_ptr_table = NULL;
16297     }
16298
16299     if (!(flags & CLONEf_COPY_STACKS)) {
16300         unreferenced_to_tmp_stack(param->unreferenced);
16301     }
16302
16303     SvREFCNT_dec(param->stashes);
16304
16305     /* orphaned? eg threads->new inside BEGIN or use */
16306     if (PL_compcv && ! SvREFCNT(PL_compcv)) {
16307         SvREFCNT_inc_simple_void(PL_compcv);
16308         SAVEFREESV(PL_compcv);
16309     }
16310
16311     return my_perl;
16312 }
16313
16314 static void
16315 S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced)
16316 {
16317     PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK;
16318
16319     if (AvFILLp(unreferenced) > -1) {
16320         SV **svp = AvARRAY(unreferenced);
16321         SV **const last = svp + AvFILLp(unreferenced);
16322         SSize_t count = 0;
16323
16324         do {
16325             if (SvREFCNT(*svp) == 1)
16326                 ++count;
16327         } while (++svp <= last);
16328
16329         EXTEND_MORTAL(count);
16330         svp = AvARRAY(unreferenced);
16331
16332         do {
16333             if (SvREFCNT(*svp) == 1) {
16334                 /* Our reference is the only one to this SV. This means that
16335                    in this thread, the scalar effectively has a 0 reference.
16336                    That doesn't work (cleanup never happens), so donate our
16337                    reference to it onto the save stack. */
16338                 PL_tmps_stack[++PL_tmps_ix] = *svp;
16339             } else {
16340                 /* As an optimisation, because we are already walking the
16341                    entire array, instead of above doing either
16342                    SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead
16343                    release our reference to the scalar, so that at the end of
16344                    the array owns zero references to the scalars it happens to
16345                    point to. We are effectively converting the array from
16346                    AvREAL() on to AvREAL() off. This saves the av_clear()
16347                    (triggered by the SvREFCNT_dec(unreferenced) below) from
16348                    walking the array a second time.  */
16349                 SvREFCNT_dec(*svp);
16350             }
16351
16352         } while (++svp <= last);
16353         AvREAL_off(unreferenced);
16354     }
16355     SvREFCNT_dec_NN(unreferenced);
16356 }
16357
16358 void
16359 Perl_clone_params_del(CLONE_PARAMS *param)
16360 {
16361     PerlInterpreter *const was = PERL_GET_THX;
16362     PerlInterpreter *const to = param->new_perl;
16363     dTHXa(to);
16364
16365     PERL_ARGS_ASSERT_CLONE_PARAMS_DEL;
16366
16367     if (was != to) {
16368         PERL_SET_THX(to);
16369     }
16370
16371     SvREFCNT_dec(param->stashes);
16372     if (param->unreferenced)
16373         unreferenced_to_tmp_stack(param->unreferenced);
16374
16375     Safefree(param);
16376
16377     if (was != to) {
16378         PERL_SET_THX(was);
16379     }
16380 }
16381
16382 CLONE_PARAMS *
16383 Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to)
16384 {
16385     /* Need to play this game, as newAV() can call safesysmalloc(), and that
16386        does a dTHX; to get the context from thread local storage.
16387        FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to
16388        a version that passes in my_perl.  */
16389     PerlInterpreter *const was = PERL_GET_THX;
16390     CLONE_PARAMS *param;
16391
16392     PERL_ARGS_ASSERT_CLONE_PARAMS_NEW;
16393
16394     if (was != to) {
16395         PERL_SET_THX(to);
16396     }
16397
16398     /* Given that we've set the context, we can do this unshared.  */
16399     Newx(param, 1, CLONE_PARAMS);
16400
16401     param->flags = 0;
16402     param->proto_perl = from;
16403     param->new_perl = to;
16404     param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV);
16405     AvREAL_off(param->stashes);
16406     param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV);
16407
16408     if (was != to) {
16409         PERL_SET_THX(was);
16410     }
16411     return param;
16412 }
16413
16414 #endif /* USE_ITHREADS */
16415
16416 void
16417 Perl_init_constants(pTHX)
16418 {
16419
16420     SvREFCNT(&PL_sv_undef)      = SvREFCNT_IMMORTAL;
16421     SvFLAGS(&PL_sv_undef)       = SVf_READONLY|SVf_PROTECT|SVt_NULL;
16422     SvANY(&PL_sv_undef)         = NULL;
16423
16424     SvANY(&PL_sv_no)            = new_XPVNV();
16425     SvREFCNT(&PL_sv_no)         = SvREFCNT_IMMORTAL;
16426     SvFLAGS(&PL_sv_no)          = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16427                                   |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16428                                   |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16429
16430     SvANY(&PL_sv_yes)           = new_XPVNV();
16431     SvREFCNT(&PL_sv_yes)        = SvREFCNT_IMMORTAL;
16432     SvFLAGS(&PL_sv_yes)         = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16433                                   |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16434                                   |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16435
16436     SvANY(&PL_sv_zero)          = new_XPVNV();
16437     SvREFCNT(&PL_sv_zero)       = SvREFCNT_IMMORTAL;
16438     SvFLAGS(&PL_sv_zero)        = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16439                                   |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16440                                   |SVp_POK|SVf_POK
16441                                   |SVs_PADTMP;
16442
16443     SvPV_set(&PL_sv_no, (char*)PL_No);
16444     SvCUR_set(&PL_sv_no, 0);
16445     SvLEN_set(&PL_sv_no, 0);
16446     SvIV_set(&PL_sv_no, 0);
16447     SvNV_set(&PL_sv_no, 0);
16448
16449     SvPV_set(&PL_sv_yes, (char*)PL_Yes);
16450     SvCUR_set(&PL_sv_yes, 1);
16451     SvLEN_set(&PL_sv_yes, 0);
16452     SvIV_set(&PL_sv_yes, 1);
16453     SvNV_set(&PL_sv_yes, 1);
16454
16455     SvPV_set(&PL_sv_zero, (char*)PL_Zero);
16456     SvCUR_set(&PL_sv_zero, 1);
16457     SvLEN_set(&PL_sv_zero, 0);
16458     SvIV_set(&PL_sv_zero, 0);
16459     SvNV_set(&PL_sv_zero, 0);
16460
16461     PadnamePV(&PL_padname_const) = (char *)PL_No;
16462
16463     assert(SvIMMORTAL_INTERP(&PL_sv_yes));
16464     assert(SvIMMORTAL_INTERP(&PL_sv_undef));
16465     assert(SvIMMORTAL_INTERP(&PL_sv_no));
16466     assert(SvIMMORTAL_INTERP(&PL_sv_zero));
16467
16468     assert(SvIMMORTAL(&PL_sv_yes));
16469     assert(SvIMMORTAL(&PL_sv_undef));
16470     assert(SvIMMORTAL(&PL_sv_no));
16471     assert(SvIMMORTAL(&PL_sv_zero));
16472
16473     assert( SvIMMORTAL_TRUE(&PL_sv_yes));
16474     assert(!SvIMMORTAL_TRUE(&PL_sv_undef));
16475     assert(!SvIMMORTAL_TRUE(&PL_sv_no));
16476     assert(!SvIMMORTAL_TRUE(&PL_sv_zero));
16477
16478     assert( SvTRUE_nomg_NN(&PL_sv_yes));
16479     assert(!SvTRUE_nomg_NN(&PL_sv_undef));
16480     assert(!SvTRUE_nomg_NN(&PL_sv_no));
16481     assert(!SvTRUE_nomg_NN(&PL_sv_zero));
16482 }
16483
16484 /*
16485 =for apidoc_section $unicode
16486
16487 =for apidoc sv_recode_to_utf8
16488
16489 C<encoding> is assumed to be an C<Encode> object, on entry the PV
16490 of C<sv> is assumed to be octets in that encoding, and C<sv>
16491 will be converted into Unicode (and UTF-8).
16492
16493 If C<sv> already is UTF-8 (or if it is not C<POK>), or if C<encoding>
16494 is not a reference, nothing is done to C<sv>.  If C<encoding> is not
16495 an C<Encode::XS> Encoding object, bad things will happen.
16496 (See L<encoding> and L<Encode>.)
16497
16498 The PV of C<sv> is returned.
16499
16500 =cut */
16501
16502 char *
16503 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
16504 {
16505     PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
16506
16507     if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
16508         SV *uni;
16509         STRLEN len;
16510         const char *s;
16511         dSP;
16512         SV *nsv = sv;
16513         ENTER;
16514         PUSHSTACK;
16515         SAVETMPS;
16516         if (SvPADTMP(nsv)) {
16517             nsv = sv_newmortal();
16518             SvSetSV_nosteal(nsv, sv);
16519         }
16520         save_re_context();
16521         PUSHMARK(sp);
16522         EXTEND(SP, 3);
16523         PUSHs(encoding);
16524         PUSHs(nsv);
16525 /*
16526   NI-S 2002/07/09
16527   Passing sv_yes is wrong - it needs to be or'ed set of constants
16528   for Encode::XS, while UTf-8 decode (currently) assumes a true value means
16529   remove converted chars from source.
16530
16531   Both will default the value - let them.
16532
16533         XPUSHs(&PL_sv_yes);
16534 */
16535         PUTBACK;
16536         call_method("decode", G_SCALAR);
16537         SPAGAIN;
16538         uni = POPs;
16539         PUTBACK;
16540         s = SvPV_const(uni, len);
16541         if (s != SvPVX_const(sv)) {
16542             SvGROW(sv, len + 1);
16543             Move(s, SvPVX(sv), len + 1, char);
16544             SvCUR_set(sv, len);
16545         }
16546         FREETMPS;
16547         POPSTACK;
16548         LEAVE;
16549         if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
16550             /* clear pos and any utf8 cache */
16551             MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
16552             if (mg)
16553                 mg->mg_len = -1;
16554             if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
16555                 magic_setutf8(sv,mg); /* clear UTF8 cache */
16556         }
16557         SvUTF8_on(sv);
16558         return SvPVX(sv);
16559     }
16560     return SvPOKp(sv) ? SvPVX(sv) : NULL;
16561 }
16562
16563 /*
16564 =for apidoc sv_cat_decode
16565
16566 C<encoding> is assumed to be an C<Encode> object, the PV of C<ssv> is
16567 assumed to be octets in that encoding and decoding the input starts
16568 from the position which S<C<(PV + *offset)>> pointed to.  C<dsv> will be
16569 concatenated with the decoded UTF-8 string from C<ssv>.  Decoding will terminate
16570 when the string C<tstr> appears in decoding output or the input ends on
16571 the PV of C<ssv>.  The value which C<offset> points will be modified
16572 to the last input position on C<ssv>.
16573
16574 Returns TRUE if the terminator was found, else returns FALSE.
16575
16576 =cut */
16577
16578 bool
16579 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
16580                    SV *ssv, int *offset, char *tstr, int tlen)
16581 {
16582     bool ret = FALSE;
16583
16584     PERL_ARGS_ASSERT_SV_CAT_DECODE;
16585
16586     if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding)) {
16587         SV *offsv;
16588         dSP;
16589         ENTER;
16590         SAVETMPS;
16591         save_re_context();
16592         PUSHMARK(sp);
16593         EXTEND(SP, 6);
16594         PUSHs(encoding);
16595         PUSHs(dsv);
16596         PUSHs(ssv);
16597         offsv = newSViv(*offset);
16598         mPUSHs(offsv);
16599         mPUSHp(tstr, tlen);
16600         PUTBACK;
16601         call_method("cat_decode", G_SCALAR);
16602         SPAGAIN;
16603         ret = SvTRUE(TOPs);
16604         *offset = SvIV(offsv);
16605         PUTBACK;
16606         FREETMPS;
16607         LEAVE;
16608     }
16609     else
16610         Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
16611     return ret;
16612
16613 }
16614
16615 /* ---------------------------------------------------------------------
16616  *
16617  * support functions for report_uninit()
16618  */
16619
16620 /* the maxiumum size of array or hash where we will scan looking
16621  * for the undefined element that triggered the warning */
16622
16623 #define FUV_MAX_SEARCH_SIZE 1000
16624
16625 /* Look for an entry in the hash whose value has the same SV as val;
16626  * If so, return a mortal copy of the key. */
16627
16628 STATIC SV*
16629 S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
16630 {
16631     HE **array;
16632     I32 i;
16633
16634     PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
16635
16636     if (!hv || SvMAGICAL(hv) || !HvTOTALKEYS(hv) ||
16637                         (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
16638         return NULL;
16639
16640     if (val == &PL_sv_undef || val == &PL_sv_placeholder)
16641         return NULL;
16642
16643     array = HvARRAY(hv);
16644
16645     for (i=HvMAX(hv); i>=0; i--) {
16646         HE *entry;
16647         for (entry = array[i]; entry; entry = HeNEXT(entry)) {
16648             if (HeVAL(entry) == val)
16649                 return newSVhek_mortal(HeKEY_hek(entry));
16650         }
16651     }
16652     return NULL;
16653 }
16654
16655 /* Look for an entry in the array whose value has the same SV as val;
16656  * If so, return the index, otherwise return -1. */
16657
16658 STATIC SSize_t
16659 S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
16660 {
16661     PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
16662
16663     if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
16664                         (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
16665         return -1;
16666
16667     if (val != &PL_sv_undef) {
16668         SV ** const svp = AvARRAY(av);
16669         SSize_t i;
16670
16671         for (i=AvFILLp(av); i>=0; i--)
16672             if (svp[i] == val)
16673                 return i;
16674     }
16675     return -1;
16676 }
16677
16678 /* varname(): return the name of a variable, optionally with a subscript.
16679  * If gv is non-zero, use the name of that global, along with gvtype (one
16680  * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
16681  * targ.  Depending on the value of the subscript_type flag, return:
16682  */
16683
16684 #define FUV_SUBSCRIPT_NONE      1       /* "@foo"          */
16685 #define FUV_SUBSCRIPT_ARRAY     2       /* "$foo[aindex]"  */
16686 #define FUV_SUBSCRIPT_HASH      3       /* "$foo{keyname}" */
16687 #define FUV_SUBSCRIPT_WITHIN    4       /* "within @foo"   */
16688
16689 SV*
16690 Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
16691         const SV *const keyname, SSize_t aindex, int subscript_type)
16692 {
16693
16694     SV * const name = sv_newmortal();
16695     if (gv && isGV(gv)) {
16696         char buffer[2];
16697         buffer[0] = gvtype;
16698         buffer[1] = 0;
16699
16700         /* as gv_fullname4(), but add literal '^' for $^FOO names  */
16701
16702         gv_fullname4(name, gv, buffer, 0);
16703
16704         if ((unsigned int)SvPVX(name)[1] <= 26) {
16705             buffer[0] = '^';
16706             buffer[1] = SvPVX(name)[1] + 'A' - 1;
16707
16708             /* Swap the 1 unprintable control character for the 2 byte pretty
16709                version - ie substr($name, 1, 1) = $buffer; */
16710             sv_insert(name, 1, 1, buffer, 2);
16711         }
16712     }
16713     else {
16714         CV * const cv = gv ? ((CV *)gv) : find_runcv(NULL);
16715         PADNAME *sv;
16716
16717         assert(!cv || SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
16718
16719         if (!cv || !CvPADLIST(cv))
16720             return NULL;
16721         sv = padnamelist_fetch(PadlistNAMES(CvPADLIST(cv)), targ);
16722         sv_setpvn(name, PadnamePV(sv), PadnameLEN(sv));
16723         SvUTF8_on(name);
16724     }
16725
16726     if (subscript_type == FUV_SUBSCRIPT_HASH) {
16727         SV * const sv = newSV_type(SVt_NULL);
16728         STRLEN len;
16729         const char * const pv = SvPV_nomg_const((SV*)keyname, len);
16730
16731         *SvPVX(name) = '$';
16732         Perl_sv_catpvf(aTHX_ name, "{%s}",
16733             pv_pretty(sv, pv, len, 32, NULL, NULL,
16734                     PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT ));
16735         SvREFCNT_dec_NN(sv);
16736     }
16737     else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
16738         *SvPVX(name) = '$';
16739         Perl_sv_catpvf(aTHX_ name, "[%" IVdf "]", (IV)aindex);
16740     }
16741     else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
16742         /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
16743         Perl_sv_insert_flags(aTHX_ name, 0, 0,  STR_WITH_LEN("within "), 0);
16744     }
16745
16746     return name;
16747 }
16748
16749
16750 /*
16751 =apidoc_section $warning
16752 =for apidoc find_uninit_var
16753
16754 Find the name of the undefined variable (if any) that caused the operator
16755 to issue a "Use of uninitialized value" warning.
16756 If match is true, only return a name if its value matches C<uninit_sv>.
16757 So roughly speaking, if a unary operator (such as C<OP_COS>) generates a
16758 warning, then following the direct child of the op may yield an
16759 C<OP_PADSV> or C<OP_GV> that gives the name of the undefined variable.  On the
16760 other hand, with C<OP_ADD> there are two branches to follow, so we only print
16761 the variable name if we get an exact match.
16762 C<desc_p> points to a string pointer holding the description of the op.
16763 This may be updated if needed.
16764
16765 The name is returned as a mortal SV.
16766
16767 Assumes that C<PL_op> is the OP that originally triggered the error, and that
16768 C<PL_comppad>/C<PL_curpad> points to the currently executing pad.
16769
16770 =cut
16771 */
16772
16773 STATIC SV *
16774 S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
16775                   bool match, const char **desc_p)
16776 {
16777     SV *sv;
16778     const GV *gv;
16779     const OP *o, *o2, *kid;
16780
16781     PERL_ARGS_ASSERT_FIND_UNINIT_VAR;
16782
16783     if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
16784                             uninit_sv == &PL_sv_placeholder)))
16785         return NULL;
16786
16787     switch (obase->op_type) {
16788
16789     case OP_UNDEF:
16790         /* the optimizer rewrites '$x = undef' to 'undef $x' for lexical
16791          * variables, which can occur as the source of warnings:
16792          *   ($x = undef) =~ s/a/b/;
16793          * The OPpUNDEF_KEEP_PV flag indicates that this used to be an
16794          * assignment op.
16795          * Otherwise undef should not care if its args are undef - any warnings
16796          * will be from tied/magic vars */
16797         if (
16798             (obase->op_private & (OPpTARGET_MY | OPpUNDEF_KEEP_PV)) == (OPpTARGET_MY | OPpUNDEF_KEEP_PV)
16799             && (!match || PAD_SVl(obase->op_targ) == uninit_sv)
16800         ) {
16801             return varname(NULL, '$', obase->op_targ, NULL, 0, FUV_SUBSCRIPT_NONE);
16802         }
16803         break;
16804
16805     case OP_RV2AV:
16806     case OP_RV2HV:
16807     case OP_PADAV:
16808     case OP_PADHV:
16809       {
16810         const bool pad  = (    obase->op_type == OP_PADAV
16811                             || obase->op_type == OP_PADHV
16812                             || obase->op_type == OP_PADRANGE
16813                           );
16814
16815         const bool hash = (    obase->op_type == OP_PADHV
16816                             || obase->op_type == OP_RV2HV
16817                             || (obase->op_type == OP_PADRANGE
16818                                 && SvTYPE(PAD_SVl(obase->op_targ)) == SVt_PVHV)
16819                           );
16820         SSize_t index = 0;
16821         SV *keysv = NULL;
16822         int subscript_type = FUV_SUBSCRIPT_WITHIN;
16823
16824         if (pad) { /* @lex, %lex */
16825             sv = PAD_SVl(obase->op_targ);
16826             gv = NULL;
16827         }
16828         else {
16829             if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16830             /* @global, %global */
16831                 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16832                 if (!gv)
16833                     break;
16834                 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
16835             }
16836             else if (obase == PL_op) /* @{expr}, %{expr} */
16837                 return find_uninit_var(cUNOPx(obase)->op_first,
16838                                                 uninit_sv, match, desc_p);
16839             else /* @{expr}, %{expr} as a sub-expression */
16840                 return NULL;
16841         }
16842
16843         /* attempt to find a match within the aggregate */
16844         if (hash) {
16845             keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16846             if (keysv)
16847                 subscript_type = FUV_SUBSCRIPT_HASH;
16848         }
16849         else {
16850             index = find_array_subscript((const AV *)sv, uninit_sv);
16851             if (index >= 0)
16852                 subscript_type = FUV_SUBSCRIPT_ARRAY;
16853         }
16854
16855         if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
16856             break;
16857
16858         return varname(gv, (char)(hash ? '%' : '@'), obase->op_targ,
16859                                     keysv, index, subscript_type);
16860       }
16861
16862     case OP_RV2SV:
16863         if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16864             /* $global */
16865             gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16866             if (!gv || !GvSTASH(gv))
16867                 break;
16868             if (match && (GvSV(gv) != uninit_sv))
16869                 break;
16870             return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16871         }
16872         /* ${expr} */
16873         return find_uninit_var(cUNOPx(obase)->op_first, uninit_sv, 1, desc_p);
16874
16875     case OP_PADSV:
16876         if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16877             break;
16878         return varname(NULL, '$', obase->op_targ,
16879                                     NULL, 0, FUV_SUBSCRIPT_NONE);
16880
16881     case OP_PADSV_STORE:
16882         if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16883             goto do_op;
16884         return varname(NULL, '$', obase->op_targ,
16885                                     NULL, 0, FUV_SUBSCRIPT_NONE);
16886
16887     case OP_GVSV:
16888         gv = cGVOPx_gv(obase);
16889         if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv))
16890             break;
16891         return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16892
16893     case OP_AELEMFAST_LEX:
16894         if (match) {
16895             SV **svp;
16896             AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16897             if (!av || SvRMAGICAL(av))
16898                 break;
16899             svp = av_fetch(av, (I8)obase->op_private, FALSE);
16900             if (!svp || *svp != uninit_sv)
16901                 break;
16902         }
16903         return varname(NULL, '$', obase->op_targ,
16904                        NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16905
16906     case OP_AELEMFASTLEX_STORE:
16907         if (match) {
16908             SV **svp;
16909             AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16910             if (!av || SvRMAGICAL(av))
16911                 goto do_op;
16912             svp = av_fetch(av, (I8)obase->op_private, FALSE);
16913             if (!svp || *svp != uninit_sv)
16914                 goto do_op;
16915         }
16916         return varname(NULL, '$', obase->op_targ,
16917                        NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16918
16919     case OP_AELEMFAST:
16920         {
16921             gv = cGVOPx_gv(obase);
16922             if (!gv)
16923                 break;
16924             if (match) {
16925                 SV **svp;
16926                 AV *const av = GvAV(gv);
16927                 if (!av || SvRMAGICAL(av))
16928                     break;
16929                 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16930                 if (!svp || *svp != uninit_sv)
16931                     break;
16932             }
16933             return varname(gv, '$', 0,
16934                     NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16935         }
16936         NOT_REACHED; /* NOTREACHED */
16937
16938     case OP_EXISTS:
16939         o = cUNOPx(obase)->op_first;
16940         if (!o || o->op_type != OP_NULL ||
16941                 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
16942             break;
16943         return find_uninit_var(cBINOPo->op_last, uninit_sv, match, desc_p);
16944
16945     case OP_AELEM:
16946     case OP_HELEM:
16947     {
16948         bool negate = FALSE;
16949
16950         if (PL_op == obase)
16951             /* $a[uninit_expr] or $h{uninit_expr} */
16952             return find_uninit_var(cBINOPx(obase)->op_last,
16953                                                 uninit_sv, match, desc_p);
16954
16955         gv = NULL;
16956         o = cBINOPx(obase)->op_first;
16957         kid = cBINOPx(obase)->op_last;
16958
16959         /* get the av or hv, and optionally the gv */
16960         sv = NULL;
16961         if  (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
16962             sv = PAD_SV(o->op_targ);
16963         }
16964         else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
16965                 && cUNOPo->op_first->op_type == OP_GV)
16966         {
16967             gv = cGVOPx_gv(cUNOPo->op_first);
16968             if (!gv)
16969                 break;
16970             sv = o->op_type
16971                 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
16972         }
16973         if (!sv)
16974             break;
16975
16976         if (kid && kid->op_type == OP_NEGATE) {
16977             negate = TRUE;
16978             kid = cUNOPx(kid)->op_first;
16979         }
16980
16981         if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
16982             /* index is constant */
16983             SV* kidsv;
16984             if (negate) {
16985                 kidsv = newSVpvs_flags("-", SVs_TEMP);
16986                 sv_catsv(kidsv, cSVOPx_sv(kid));
16987             }
16988             else
16989                 kidsv = cSVOPx_sv(kid);
16990             if (match) {
16991                 if (SvMAGICAL(sv))
16992                     break;
16993                 if (obase->op_type == OP_HELEM) {
16994                     HE* he = hv_fetch_ent(MUTABLE_HV(sv), kidsv, 0, 0);
16995                     if (!he || HeVAL(he) != uninit_sv)
16996                         break;
16997                 }
16998                 else {
16999                     SV * const  opsv = cSVOPx_sv(kid);
17000                     const IV  opsviv = SvIV(opsv);
17001                     SV * const * const svp = av_fetch(MUTABLE_AV(sv),
17002                         negate ? - opsviv : opsviv,
17003                         FALSE);
17004                     if (!svp || *svp != uninit_sv)
17005                         break;
17006                 }
17007             }
17008             if (obase->op_type == OP_HELEM)
17009                 return varname(gv, '%', o->op_targ,
17010                             kidsv, 0, FUV_SUBSCRIPT_HASH);
17011             else
17012                 return varname(gv, '@', o->op_targ, NULL,
17013                     negate ? - SvIV(cSVOPx_sv(kid)) : SvIV(cSVOPx_sv(kid)),
17014                     FUV_SUBSCRIPT_ARRAY);
17015         }
17016         else {
17017             /* index is an expression;
17018              * attempt to find a match within the aggregate */
17019             if (obase->op_type == OP_HELEM) {
17020                 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17021                 if (keysv)
17022                     return varname(gv, '%', o->op_targ,
17023                                                 keysv, 0, FUV_SUBSCRIPT_HASH);
17024             }
17025             else {
17026                 const SSize_t index
17027                     = find_array_subscript((const AV *)sv, uninit_sv);
17028                 if (index >= 0)
17029                     return varname(gv, '@', o->op_targ,
17030                                         NULL, index, FUV_SUBSCRIPT_ARRAY);
17031             }
17032             if (match)
17033                 break;
17034             return varname(gv,
17035                 (char)((o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
17036                 ? '@' : '%'),
17037                 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17038         }
17039         NOT_REACHED; /* NOTREACHED */
17040     }
17041
17042     case OP_MULTIDEREF: {
17043         /* If we were executing OP_MULTIDEREF when the undef warning
17044          * triggered, then it must be one of the index values within
17045          * that triggered it. If not, then the only possibility is that
17046          * the value retrieved by the last aggregate index might be the
17047          * culprit. For the former, we set PL_multideref_pc each time before
17048          * using an index, so work though the item list until we reach
17049          * that point. For the latter, just work through the entire item
17050          * list; the last aggregate retrieved will be the candidate.
17051          * There is a third rare possibility: something triggered
17052          * magic while fetching an array/hash element. Just display
17053          * nothing in this case.
17054          */
17055
17056         /* the named aggregate, if any */
17057         PADOFFSET agg_targ = 0;
17058         GV       *agg_gv   = NULL;
17059         /* the last-seen index */
17060         UV        index_type;
17061         PADOFFSET index_targ;
17062         GV       *index_gv;
17063         IV        index_const_iv = 0; /* init for spurious compiler warn */
17064         SV       *index_const_sv;
17065         int       depth = 0;  /* how many array/hash lookups we've done */
17066
17067         UNOP_AUX_item *items = cUNOP_AUXx(obase)->op_aux;
17068         UNOP_AUX_item *last = NULL;
17069         UV actions = items->uv;
17070         bool is_hv;
17071
17072         if (PL_op == obase) {
17073             last = PL_multideref_pc;
17074             assert(last >= items && last <= items + items[-1].uv);
17075         }
17076
17077         assert(actions);
17078
17079         while (1) {
17080             is_hv = FALSE;
17081             switch (actions & MDEREF_ACTION_MASK) {
17082
17083             case MDEREF_reload:
17084                 actions = (++items)->uv;
17085                 continue;
17086
17087             case MDEREF_HV_padhv_helem:               /* $lex{...} */
17088                 is_hv = TRUE;
17089                 /* FALLTHROUGH */
17090             case MDEREF_AV_padav_aelem:               /* $lex[...] */
17091                 agg_targ = (++items)->pad_offset;
17092                 agg_gv = NULL;
17093                 break;
17094
17095             case MDEREF_HV_gvhv_helem:                /* $pkg{...} */
17096                 is_hv = TRUE;
17097                 /* FALLTHROUGH */
17098             case MDEREF_AV_gvav_aelem:                /* $pkg[...] */
17099                 agg_targ = 0;
17100                 agg_gv = (GV*)UNOP_AUX_item_sv(++items);
17101                 assert(isGV_with_GP(agg_gv));
17102                 break;
17103
17104             case MDEREF_HV_gvsv_vivify_rv2hv_helem:   /* $pkg->{...} */
17105             case MDEREF_HV_padsv_vivify_rv2hv_helem:  /* $lex->{...} */
17106                 ++items;
17107                 /* FALLTHROUGH */
17108             case MDEREF_HV_pop_rv2hv_helem:           /* expr->{...} */
17109             case MDEREF_HV_vivify_rv2hv_helem:        /* vivify, ->{...} */
17110                 agg_targ = 0;
17111                 agg_gv   = NULL;
17112                 is_hv    = TRUE;
17113                 break;
17114
17115             case MDEREF_AV_gvsv_vivify_rv2av_aelem:   /* $pkg->[...] */
17116             case MDEREF_AV_padsv_vivify_rv2av_aelem:  /* $lex->[...] */
17117                 ++items;
17118                 /* FALLTHROUGH */
17119             case MDEREF_AV_pop_rv2av_aelem:           /* expr->[...] */
17120             case MDEREF_AV_vivify_rv2av_aelem:        /* vivify, ->[...] */
17121                 agg_targ = 0;
17122                 agg_gv   = NULL;
17123             } /* switch */
17124
17125             index_targ     = 0;
17126             index_gv       = NULL;
17127             index_const_sv = NULL;
17128
17129             index_type = (actions & MDEREF_INDEX_MASK);
17130             switch (index_type) {
17131             case MDEREF_INDEX_none:
17132                 break;
17133             case MDEREF_INDEX_const:
17134                 if (is_hv)
17135                     index_const_sv = UNOP_AUX_item_sv(++items)
17136                 else
17137                     index_const_iv = (++items)->iv;
17138                 break;
17139             case MDEREF_INDEX_padsv:
17140                 index_targ = (++items)->pad_offset;
17141                 break;
17142             case MDEREF_INDEX_gvsv:
17143                 index_gv = (GV*)UNOP_AUX_item_sv(++items);
17144                 assert(isGV_with_GP(index_gv));
17145                 break;
17146             }
17147
17148             if (index_type != MDEREF_INDEX_none)
17149                 depth++;
17150
17151             if (   index_type == MDEREF_INDEX_none
17152                 || (actions & MDEREF_FLAG_last)
17153                 || (last && items >= last)
17154             )
17155                 break;
17156
17157             actions >>= MDEREF_SHIFT;
17158         } /* while */
17159
17160         if (PL_op == obase) {
17161             /* most likely index was undef */
17162
17163             *desc_p = (    (actions & MDEREF_FLAG_last)
17164                         && (obase->op_private
17165                                 & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)))
17166                         ?
17167                             (obase->op_private & OPpMULTIDEREF_EXISTS)
17168                                 ? "exists"
17169                                 : "delete"
17170                         : is_hv ? "hash element" : "array element";
17171             assert(index_type != MDEREF_INDEX_none);
17172             if (index_gv) {
17173                 if (GvSV(index_gv) == uninit_sv)
17174                     return varname(index_gv, '$', 0, NULL, 0,
17175                                                     FUV_SUBSCRIPT_NONE);
17176                 else
17177                     return NULL;
17178             }
17179             if (index_targ) {
17180                 if (PL_curpad[index_targ] == uninit_sv)
17181                     return varname(NULL, '$', index_targ,
17182                                     NULL, 0, FUV_SUBSCRIPT_NONE);
17183                 else
17184                     return NULL;
17185             }
17186             /* If we got to this point it was undef on a const subscript,
17187              * so magic probably involved, e.g. $ISA[0]. Give up. */
17188             return NULL;
17189         }
17190
17191         /* the SV returned by pp_multideref() was undef, if anything was */
17192
17193         if (depth != 1)
17194             break;
17195
17196         if (agg_targ)
17197             sv = PAD_SV(agg_targ);
17198         else if (agg_gv) {
17199             sv = is_hv ? MUTABLE_SV(GvHV(agg_gv)) : MUTABLE_SV(GvAV(agg_gv));
17200             if (!sv)
17201                 break;
17202             }
17203         else
17204             break;
17205
17206         if (index_type == MDEREF_INDEX_const) {
17207             if (match) {
17208                 if (SvMAGICAL(sv))
17209                     break;
17210                 if (is_hv) {
17211                     HE* he = hv_fetch_ent(MUTABLE_HV(sv), index_const_sv, 0, 0);
17212                     if (!he || HeVAL(he) != uninit_sv)
17213                         break;
17214                 }
17215                 else {
17216                     SV * const * const svp =
17217                             av_fetch(MUTABLE_AV(sv), index_const_iv, FALSE);
17218                     if (!svp || *svp != uninit_sv)
17219                         break;
17220                 }
17221             }
17222             return is_hv
17223                 ? varname(agg_gv, '%', agg_targ,
17224                                 index_const_sv, 0,    FUV_SUBSCRIPT_HASH)
17225                 : varname(agg_gv, '@', agg_targ,
17226                                 NULL, index_const_iv, FUV_SUBSCRIPT_ARRAY);
17227         }
17228         else {
17229             /* index is an var */
17230             if (is_hv) {
17231                 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17232                 if (keysv)
17233                     return varname(agg_gv, '%', agg_targ,
17234                                                 keysv, 0, FUV_SUBSCRIPT_HASH);
17235             }
17236             else {
17237                 const SSize_t index
17238                     = find_array_subscript((const AV *)sv, uninit_sv);
17239                 if (index >= 0)
17240                     return varname(agg_gv, '@', agg_targ,
17241                                         NULL, index, FUV_SUBSCRIPT_ARRAY);
17242             }
17243             /* look for an element not found */
17244             if (!SvMAGICAL(sv)) {
17245                 SV *index_sv = NULL;
17246                 if (index_targ) {
17247                     index_sv = PL_curpad[index_targ];
17248                 }
17249                 else if (index_gv) {
17250                     index_sv = GvSV(index_gv);
17251                 }
17252                 if (index_sv && !SvMAGICAL(index_sv) && !SvROK(index_sv)) {
17253                     if (is_hv) {
17254                         SV *report_index_sv = SvOK(index_sv) ? index_sv : &PL_sv_no;
17255                         HE *he = hv_fetch_ent(MUTABLE_HV(sv), report_index_sv, 0, 0);
17256                         if (!he) {
17257                             return varname(agg_gv, '%', agg_targ,
17258                                            report_index_sv, 0, FUV_SUBSCRIPT_HASH);
17259                         }
17260                     }
17261                     else {
17262                         SSize_t index = SvOK(index_sv) ? SvIV(index_sv) : 0;
17263                         SV * const * const svp =
17264                             av_fetch(MUTABLE_AV(sv), index, FALSE);
17265                         if (!svp) {
17266                             return varname(agg_gv, '@', agg_targ,
17267                                            NULL, index, FUV_SUBSCRIPT_ARRAY);
17268                         }
17269                     }
17270                 }
17271             }
17272             if (match)
17273                 break;
17274             return varname(agg_gv,
17275                 is_hv ? '%' : '@',
17276                 agg_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17277         }
17278         NOT_REACHED; /* NOTREACHED */
17279     }
17280
17281     case OP_AASSIGN:
17282         /* only examine RHS */
17283         return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv,
17284                                                                 match, desc_p);
17285
17286     case OP_OPEN:
17287         o = cUNOPx(obase)->op_first;
17288         if (   o->op_type == OP_PUSHMARK
17289            || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)
17290         )
17291             o = OpSIBLING(o);
17292
17293         if (!OpHAS_SIBLING(o)) {
17294             /* one-arg version of open is highly magical */
17295
17296             if (o->op_type == OP_GV) { /* open FOO; */
17297                 gv = cGVOPx_gv(o);
17298                 if (match && GvSV(gv) != uninit_sv)
17299                     break;
17300                 return varname(gv, '$', 0,
17301                             NULL, 0, FUV_SUBSCRIPT_NONE);
17302             }
17303             /* other possibilities not handled are:
17304              * open $x; or open my $x;  should return '${*$x}'
17305              * open expr;               should return '$'.expr ideally
17306              */
17307              break;
17308         }
17309         match = 1;
17310         goto do_op;
17311
17312     /* ops where $_ may be an implicit arg */
17313     case OP_TRANS:
17314     case OP_TRANSR:
17315     case OP_SUBST:
17316     case OP_MATCH:
17317         if ( !(obase->op_flags & OPf_STACKED)) {
17318             if (uninit_sv == DEFSV)
17319                 return newSVpvs_flags("$_", SVs_TEMP);
17320             else if (obase->op_targ
17321                   && uninit_sv == PAD_SVl(obase->op_targ))
17322                 return varname(NULL, '$', obase->op_targ, NULL, 0,
17323                                FUV_SUBSCRIPT_NONE);
17324         }
17325         goto do_op;
17326
17327     case OP_PRTF:
17328     case OP_PRINT:
17329     case OP_SAY:
17330         match = 1; /* print etc can return undef on defined args */
17331         /* skip filehandle as it can't produce 'undef' warning  */
17332         o = cUNOPx(obase)->op_first;
17333         if ((obase->op_flags & OPf_STACKED)
17334             &&
17335                (   o->op_type == OP_PUSHMARK
17336                || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)))
17337             o = OpSIBLING(OpSIBLING(o));
17338         goto do_op2;
17339
17340
17341     case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
17342     case OP_CUSTOM: /* XS or custom code could trigger random warnings */
17343
17344         /* the following ops are capable of returning PL_sv_undef even for
17345          * defined arg(s) */
17346
17347     case OP_BACKTICK:
17348     case OP_PIPE_OP:
17349     case OP_FILENO:
17350     case OP_BINMODE:
17351     case OP_TIED:
17352     case OP_GETC:
17353     case OP_SYSREAD:
17354     case OP_SEND:
17355     case OP_IOCTL:
17356     case OP_SOCKET:
17357     case OP_SOCKPAIR:
17358     case OP_BIND:
17359     case OP_CONNECT:
17360     case OP_LISTEN:
17361     case OP_ACCEPT:
17362     case OP_SHUTDOWN:
17363     case OP_SSOCKOPT:
17364     case OP_GETPEERNAME:
17365     case OP_FTRREAD:
17366     case OP_FTRWRITE:
17367     case OP_FTREXEC:
17368     case OP_FTROWNED:
17369     case OP_FTEREAD:
17370     case OP_FTEWRITE:
17371     case OP_FTEEXEC:
17372     case OP_FTEOWNED:
17373     case OP_FTIS:
17374     case OP_FTZERO:
17375     case OP_FTSIZE:
17376     case OP_FTFILE:
17377     case OP_FTDIR:
17378     case OP_FTLINK:
17379     case OP_FTPIPE:
17380     case OP_FTSOCK:
17381     case OP_FTBLK:
17382     case OP_FTCHR:
17383     case OP_FTTTY:
17384     case OP_FTSUID:
17385     case OP_FTSGID:
17386     case OP_FTSVTX:
17387     case OP_FTTEXT:
17388     case OP_FTBINARY:
17389     case OP_FTMTIME:
17390     case OP_FTATIME:
17391     case OP_FTCTIME:
17392     case OP_READLINK:
17393     case OP_OPEN_DIR:
17394     case OP_READDIR:
17395     case OP_TELLDIR:
17396     case OP_SEEKDIR:
17397     case OP_REWINDDIR:
17398     case OP_CLOSEDIR:
17399     case OP_GMTIME:
17400     case OP_ALARM:
17401     case OP_SEMGET:
17402     case OP_GETLOGIN:
17403     case OP_SUBSTR:
17404     case OP_AEACH:
17405     case OP_EACH:
17406     case OP_SORT:
17407     case OP_CALLER:
17408     case OP_DOFILE:
17409     case OP_PROTOTYPE:
17410     case OP_NCMP:
17411     case OP_SMARTMATCH:
17412     case OP_UNPACK:
17413     case OP_SYSOPEN:
17414     case OP_SYSSEEK:
17415         match = 1;
17416         goto do_op;
17417
17418     case OP_ENTERSUB:
17419     case OP_GOTO:
17420         /* XXX tmp hack: these two may call an XS sub, and currently
17421           XS subs don't have a SUB entry on the context stack, so CV and
17422           pad determination goes wrong, and BAD things happen. So, just
17423           don't try to determine the value under those circumstances.
17424           Need a better fix at dome point. DAPM 11/2007 */
17425         break;
17426
17427     case OP_FLIP:
17428     case OP_FLOP:
17429     {
17430         GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
17431         if (gv && GvSV(gv) == uninit_sv)
17432             return newSVpvs_flags("$.", SVs_TEMP);
17433         goto do_op;
17434     }
17435
17436     case OP_POS:
17437         /* def-ness of rval pos() is independent of the def-ness of its arg */
17438         if ( !(obase->op_flags & OPf_MOD))
17439             break;
17440         /* FALLTHROUGH */
17441
17442     case OP_SCHOMP:
17443     case OP_CHOMP:
17444         if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
17445             return newSVpvs_flags("${$/}", SVs_TEMP);
17446         /* FALLTHROUGH */
17447
17448     default:
17449     do_op:
17450         if (!(obase->op_flags & OPf_KIDS))
17451             break;
17452         o = cUNOPx(obase)->op_first;
17453
17454     do_op2:
17455         if (!o)
17456             break;
17457
17458         /* This loop checks all the kid ops, skipping any that cannot pos-
17459          * sibly be responsible for the uninitialized value; i.e., defined
17460          * constants and ops that return nothing.  If there is only one op
17461          * left that is not skipped, then we *know* it is responsible for
17462          * the uninitialized value.  If there is more than one op left, we
17463          * have to look for an exact match in the while() loop below.
17464          * Note that we skip padrange, because the individual pad ops that
17465          * it replaced are still in the tree, so we work on them instead.
17466          */
17467         o2 = NULL;
17468         for (kid=o; kid; kid = OpSIBLING(kid)) {
17469             const OPCODE type = kid->op_type;
17470             if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
17471               || (type == OP_NULL  && ! (kid->op_flags & OPf_KIDS))
17472               || (type == OP_PUSHMARK)
17473               || (type == OP_PADRANGE)
17474             )
17475             continue;
17476
17477             if (o2) { /* more than one found */
17478                 o2 = NULL;
17479                 break;
17480             }
17481             o2 = kid;
17482         }
17483         if (o2)
17484             return find_uninit_var(o2, uninit_sv, match, desc_p);
17485
17486         /* scan all args */
17487         while (o) {
17488             sv = find_uninit_var(o, uninit_sv, 1, desc_p);
17489             if (sv)
17490                 return sv;
17491             o = OpSIBLING(o);
17492         }
17493         break;
17494     }
17495     return NULL;
17496 }
17497
17498
17499 /*
17500 =for apidoc_section $warning
17501 =for apidoc report_uninit
17502
17503 Print appropriate "Use of uninitialized variable" warning.
17504
17505 =cut
17506 */
17507
17508 void
17509 Perl_report_uninit(pTHX_ const SV *uninit_sv)
17510 {
17511     const char *desc = NULL;
17512     SV* varname = NULL;
17513
17514     if (PL_op) {
17515         desc = PL_op->op_type == OP_STRINGIFY && PL_op->op_folded
17516                 ? "join or string"
17517                 : PL_op->op_type == OP_MULTICONCAT
17518                     && (PL_op->op_private & OPpMULTICONCAT_FAKE)
17519                 ? "sprintf"
17520                 : OP_DESC(PL_op);
17521         if (uninit_sv && PL_curpad) {
17522             varname = find_uninit_var(PL_op, uninit_sv, 0, &desc);
17523             if (varname)
17524                 sv_insert(varname, 0, 0, " ", 1);
17525         }
17526     }
17527     else if (PL_curstackinfo->si_type == PERLSI_SORT && cxstack_ix == 0)
17528         /* we've reached the end of a sort block or sub,
17529          * and the uninit value is probably what that code returned */
17530         desc = "sort";
17531
17532     /* PL_warn_uninit_sv is constant */
17533     GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
17534     if (desc)
17535         /* diag_listed_as: Use of uninitialized value%s */
17536         Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit_sv,
17537                 SVfARG(varname ? varname : &PL_sv_no),
17538                 " in ", desc);
17539     else
17540         Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
17541                 "", "", "");
17542     GCC_DIAG_RESTORE_STMT;
17543 }
17544
17545 /*
17546  * ex: set ts=8 sts=4 sw=4 et:
17547  */