This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
1c0a0f1219b7c7b6f03e5ce812575c9d3b4827ac
[perl5.git] / sv.h
1 /*    sv.h
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 and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 #ifdef sv_flags
12 #undef sv_flags         /* Convex has this in <signal.h> for sigvec() */
13 #endif
14
15 /*
16 =head1 SV Flags
17
18 =for apidoc AmU||svtype
19 An enum of flags for Perl types.  These are found in the file B<sv.h>
20 in the C<svtype> enum.  Test these flags with the C<SvTYPE> macro.
21
22 =for apidoc AmU||SVt_PV
23 Pointer type flag for scalars.  See C<svtype>.
24
25 =for apidoc AmU||SVt_IV
26 Integer type flag for scalars.  See C<svtype>.
27
28 =for apidoc AmU||SVt_NV
29 Double type flag for scalars.  See C<svtype>.
30
31 =for apidoc AmU||SVt_PVMG
32 Type flag for blessed scalars.  See C<svtype>.
33
34 =for apidoc AmU||SVt_PVAV
35 Type flag for arrays.  See C<svtype>.
36
37 =for apidoc AmU||SVt_PVHV
38 Type flag for hashes.  See C<svtype>.
39
40 =for apidoc AmU||SVt_PVCV
41 Type flag for code refs.  See C<svtype>.
42
43 =cut
44 */
45
46 typedef enum {
47         SVt_NULL,       /* 0 */
48         SVt_BIND,       /* 1 */
49         SVt_IV,         /* 2 */
50         SVt_NV,         /* 3 */
51         /* RV was here, before it was merged with IV.  */
52         SVt_PV,         /* 4 */
53         SVt_PVIV,       /* 5 */
54         SVt_PVNV,       /* 6 */
55         SVt_PVMG,       /* 7 */
56         SVt_REGEXP,     /* 8 */
57         /* PVBM was here, before BIND replaced it.  */
58         SVt_PVGV,       /* 9 */
59         SVt_PVLV,       /* 10 */
60         SVt_PVAV,       /* 11 */
61         SVt_PVHV,       /* 12 */
62         SVt_PVCV,       /* 13 */
63         SVt_PVFM,       /* 14 */
64         SVt_PVIO,       /* 15 */
65         SVt_LAST        /* keep last in enum. used to size arrays */
66 } svtype;
67
68 /* *** any alterations to the SV types above need to be reflected in
69  * SVt_MASK and the various PL_valid_types_* tables */
70
71 #define SVt_MASK 0xf    /* smallest bitmask that covers all types */
72
73 #ifndef PERL_CORE
74 /* Although Fast Boyer Moore tables are now being stored in PVGVs, for most
75    purposes eternal code wanting to consider PVBM probably needs to think of
76    PVMG instead.  */
77 #  define SVt_PVBM      SVt_PVMG
78 /* Anything wanting to create a reference from clean should ensure that it has
79    a scalar of type SVt_IV now:  */
80 #  define SVt_RV        SVt_IV
81 #endif
82
83 /* There is collusion here with sv_clear - sv_clear exits early for SVt_NULL
84    so never reaches the clause at the end that uses sv_type_details->body_size
85    to determine whether to call safefree(). Hence body_size can be set
86    non-zero to record the size of HEs, without fear of bogus frees.  */
87 #if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
88 #define HE_SVSLOT       SVt_NULL
89 #endif
90
91 #define PERL_ARENA_ROOTS_SIZE   (SVt_LAST)
92
93 /* typedefs to eliminate some typing */
94 typedef struct he HE;
95 typedef struct hek HEK;
96
97 /* Using C's structural equivalence to help emulate C++ inheritance here... */
98
99 /* start with 2 sv-head building blocks */
100 #define _SV_HEAD(ptrtype) \
101     ptrtype     sv_any;         /* pointer to body */   \
102     U32         sv_refcnt;      /* how many references to us */ \
103     U32         sv_flags        /* what we are */
104
105 #define _SV_HEAD_UNION \
106     union {                             \
107         char*   svu_pv;         /* pointer to malloced string */        \
108         IV      svu_iv;                 \
109         UV      svu_uv;                 \
110         SV*     svu_rv;         /* pointer to another SV */             \
111         SV**    svu_array;              \
112         HE**    svu_hash;               \
113         GP*     svu_gp;                 \
114         PerlIO *svu_fp;                 \
115     }   sv_u
116
117
118 struct STRUCT_SV {              /* struct sv { */
119     _SV_HEAD(void*);
120     _SV_HEAD_UNION;
121 #ifdef DEBUG_LEAKING_SCALARS
122     PERL_BITFIELD32 sv_debug_optype:9;  /* the type of OP that allocated us */
123     PERL_BITFIELD32 sv_debug_inpad:1;   /* was allocated in a pad for an OP */
124     PERL_BITFIELD32 sv_debug_line:16;   /* the line where we were allocated */
125     UV              sv_debug_serial;    /* serial number of sv allocation   */
126     char *          sv_debug_file;      /* the file where we were allocated */
127     SV *            sv_debug_parent;    /* what we were cloned from (ithreads)*/
128 #endif
129 };
130
131 struct gv {
132     _SV_HEAD(XPVGV*);           /* pointer to xpvgv body */
133     _SV_HEAD_UNION;
134 };
135
136 struct cv {
137     _SV_HEAD(XPVCV*);           /* pointer to xpvcv body */
138     _SV_HEAD_UNION;
139 };
140
141 struct av {
142     _SV_HEAD(XPVAV*);           /* pointer to xpvav body */
143     _SV_HEAD_UNION;
144 };
145
146 struct hv {
147     _SV_HEAD(XPVHV*);           /* pointer to xpvhv body */
148     _SV_HEAD_UNION;
149 };
150
151 struct io {
152     _SV_HEAD(XPVIO*);           /* pointer to xpvio body */
153     _SV_HEAD_UNION;
154 };
155
156 struct p5rx {
157     _SV_HEAD(struct regexp*);   /* pointer to regexp body */
158     _SV_HEAD_UNION;
159 };
160
161 #undef _SV_HEAD
162 #undef _SV_HEAD_UNION           /* ensure no pollution */
163
164 /*
165 =head1 SV Manipulation Functions
166
167 =for apidoc Am|U32|SvREFCNT|SV* sv
168 Returns the value of the object's reference count.
169
170 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
171 Increments the reference count of the given SV.
172
173 All of the following SvREFCNT_inc* macros are optimized versions of
174 SvREFCNT_inc, and can be replaced with SvREFCNT_inc.
175
176 =for apidoc Am|SV*|SvREFCNT_inc_NN|SV* sv
177 Same as SvREFCNT_inc, but can only be used if you know I<sv>
178 is not NULL.  Since we don't have to check the NULLness, it's faster
179 and smaller.
180
181 =for apidoc Am|void|SvREFCNT_inc_void|SV* sv
182 Same as SvREFCNT_inc, but can only be used if you don't need the
183 return value.  The macro doesn't need to return a meaningful value.
184
185 =for apidoc Am|void|SvREFCNT_inc_void_NN|SV* sv
186 Same as SvREFCNT_inc, but can only be used if you don't need the return
187 value, and you know that I<sv> is not NULL.  The macro doesn't need
188 to return a meaningful value, or check for NULLness, so it's smaller
189 and faster.
190
191 =for apidoc Am|SV*|SvREFCNT_inc_simple|SV* sv
192 Same as SvREFCNT_inc, but can only be used with expressions without side
193 effects.  Since we don't have to store a temporary value, it's faster.
194
195 =for apidoc Am|SV*|SvREFCNT_inc_simple_NN|SV* sv
196 Same as SvREFCNT_inc_simple, but can only be used if you know I<sv>
197 is not NULL.  Since we don't have to check the NULLness, it's faster
198 and smaller.
199
200 =for apidoc Am|void|SvREFCNT_inc_simple_void|SV* sv
201 Same as SvREFCNT_inc_simple, but can only be used if you don't need the
202 return value.  The macro doesn't need to return a meaningful value.
203
204 =for apidoc Am|void|SvREFCNT_inc_simple_void_NN|SV* sv
205 Same as SvREFCNT_inc, but can only be used if you don't need the return
206 value, and you know that I<sv> is not NULL.  The macro doesn't need
207 to return a meaningful value, or check for NULLness, so it's smaller
208 and faster.
209
210 =for apidoc Am|void|SvREFCNT_dec|SV* sv
211 Decrements the reference count of the given SV.
212
213 =for apidoc Am|svtype|SvTYPE|SV* sv
214 Returns the type of the SV.  See C<svtype>.
215
216 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
217 Used to upgrade an SV to a more complex form.  Uses C<sv_upgrade> to
218 perform the upgrade if necessary.  See C<svtype>.
219
220 =cut
221 */
222
223 #define SvANY(sv)       (sv)->sv_any
224 #define SvFLAGS(sv)     (sv)->sv_flags
225 #define SvREFCNT(sv)    (sv)->sv_refcnt
226
227 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
228 #  define SvREFCNT_inc(sv)              \
229     ({                                  \
230         SV * const _sv = MUTABLE_SV(sv);        \
231         if (_sv)                        \
232              (SvREFCNT(_sv))++;         \
233         _sv;                            \
234     })
235 #  define SvREFCNT_inc_simple(sv)       \
236     ({                                  \
237         if (sv)                         \
238              (SvREFCNT(sv))++;          \
239         MUTABLE_SV(sv);                         \
240     })
241 #  define SvREFCNT_inc_NN(sv)           \
242     ({                                  \
243         SV * const _sv = MUTABLE_SV(sv);        \
244         SvREFCNT(_sv)++;                \
245         _sv;                            \
246     })
247 #  define SvREFCNT_inc_void(sv)         \
248     ({                                  \
249         SV * const _sv = MUTABLE_SV(sv);        \
250         if (_sv)                        \
251             (void)(SvREFCNT(_sv)++);    \
252     })
253 #else
254 #  define SvREFCNT_inc(sv)      \
255         ((PL_Sv=MUTABLE_SV(sv)) ? (++(SvREFCNT(PL_Sv)),PL_Sv) : NULL)
256 #  define SvREFCNT_inc_simple(sv) \
257         ((sv) ? (SvREFCNT(sv)++,MUTABLE_SV(sv)) : NULL)
258 #  define SvREFCNT_inc_NN(sv) \
259         (PL_Sv=MUTABLE_SV(sv),++(SvREFCNT(PL_Sv)),PL_Sv)
260 #  define SvREFCNT_inc_void(sv) \
261         (void)((PL_Sv=MUTABLE_SV(sv)) ? ++(SvREFCNT(PL_Sv)) : 0)
262 #endif
263
264 /* These guys don't need the curly blocks */
265 #define SvREFCNT_inc_simple_void(sv)    STMT_START { if (sv) SvREFCNT(sv)++; } STMT_END
266 #define SvREFCNT_inc_simple_NN(sv)      (++(SvREFCNT(sv)),MUTABLE_SV(sv))
267 #define SvREFCNT_inc_void_NN(sv)        (void)(++SvREFCNT(MUTABLE_SV(sv)))
268 #define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
269
270 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
271 #  define SvREFCNT_dec(sv)              \
272     ({                                  \
273         SV * const _sv = MUTABLE_SV(sv);        \
274         if (_sv) {                      \
275             if (SvREFCNT(_sv)) {        \
276                 if (--(SvREFCNT(_sv)) == 0) \
277                     Perl_sv_free2(aTHX_ _sv);   \
278             } else {                    \
279                 sv_free(_sv);           \
280             }                           \
281         }                               \
282     })
283 #else
284 #define SvREFCNT_dec(sv)        sv_free(MUTABLE_SV(sv))
285 #endif
286
287 #define SVTYPEMASK      0xff
288 #define SvTYPE(sv)      ((svtype)((sv)->sv_flags & SVTYPEMASK))
289
290 /* Sadly there are some parts of the core that have pointers to already-freed
291    SV heads, and rely on being able to tell that they are now free. So mark
292    them all by using a consistent macro.  */
293 #define SvIS_FREED(sv)  ((sv)->sv_flags == SVTYPEMASK)
294
295 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt), 1))
296
297 #define SVf_IOK         0x00000100  /* has valid public integer value */
298 #define SVf_NOK         0x00000200  /* has valid public numeric value */
299 #define SVf_POK         0x00000400  /* has valid public pointer value */
300 #define SVf_ROK         0x00000800  /* has a valid reference pointer */
301
302 #define SVp_IOK         0x00001000  /* has valid non-public integer value */
303 #define SVp_NOK         0x00002000  /* has valid non-public numeric value */
304 #define SVp_POK         0x00004000  /* has valid non-public pointer value */
305 #define SVp_SCREAM      0x00008000  /* method name is DOES */
306 #define SVphv_CLONEABLE SVp_SCREAM  /* PVHV (stashes) clone its objects */
307 #define SVpgv_GP        SVp_SCREAM  /* GV has a valid GP */
308 #define SVprv_PCS_IMPORTED  SVp_SCREAM  /* RV is a proxy for a constant
309                                        subroutine in another package. Set the
310                                        GvIMPORTED_CV_on() if it needs to be
311                                        expanded to a real GV */
312 /*                      0x00010000  *** FREE SLOT */
313 #define SVs_PADTMP      0x00020000  /* in use as tmp; only if ! SVs_PADMY */
314 #define SVs_PADSTALE    0x00020000  /* lexical has gone out of scope;
315                                         only valid for SVs_PADMY */
316 #define SVpad_TYPED     0x00020000  /* pad name is a Typed Lexical */
317 #define SVs_PADMY       0x00040000  /* in use a "my" variable */
318 #define SVpad_OUR       0x00040000  /* pad name is "our" instead of "my" */
319 #define SVs_TEMP        0x00080000  /* string is stealable? */
320 #define SVs_OBJECT      0x00100000  /* is "blessed" */
321 #define SVs_GMG         0x00200000  /* has magical get method */
322 #define SVs_SMG         0x00400000  /* has magical set method */
323 #define SVs_RMG         0x00800000  /* has random magical methods */
324
325 #define SVf_FAKE        0x01000000  /* 0: glob is just a copy
326                                        1: SV head arena wasn't malloc()ed
327                                        2: in conjunction with SVf_READONLY
328                                           marks a shared hash key scalar
329                                           (SvLEN == 0) or a copy on write
330                                           string (SvLEN != 0) [SvIsCOW(sv)]
331                                        3: For PVCV, whether CvUNIQUE(cv)
332                                           refers to an eval or once only
333                                           [CvEVAL(cv), CvSPECIAL(cv)]
334                                        4: On a pad name SV, that slot in the
335                                           frame AV is a REFCNT'ed reference
336                                           to a lexical from "outside". */
337 #define SVphv_REHASH    SVf_FAKE    /* 5: On a PVHV, hash values are being
338                                           recalculated */
339 #define SVf_OOK         0x02000000  /* has valid offset value. For a PVHV this
340                                        means that a hv_aux struct is present
341                                        after the main array */
342 #define SVf_BREAK       0x04000000  /* refcnt is artificially low - used by
343                                        SVs in final arena cleanup.
344                                        Set in S_regtry on PL_reg_curpm, so that
345                                        perl_destruct will skip it. */
346 #define SVf_READONLY    0x08000000  /* may not be modified */
347
348
349
350
351 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE|SVs_RMG)
352
353 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
354                          SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
355
356 #define PRIVSHIFT 4     /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
357
358 #define SVf_AMAGIC      0x10000000  /* has magical overloaded methods */
359
360 /* Ensure this value does not clash with the GV_ADD* flags in gv.h: */
361 #define SVf_UTF8        0x20000000  /* SvPV is UTF-8 encoded
362                                        This is also set on RVs whose overloaded
363                                        stringification is UTF-8. This might
364                                        only happen as a side effect of SvPV() */
365                                            
366
367 /* Some private flags. */
368
369 /* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
370    be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
371    been restructured, so sometimes get used as string buffers.  */
372
373 /* PVHV */
374 #define SVphv_SHAREKEYS 0x20000000  /* PVHV keys live on shared string table */
375
376 /* PVNV, PVMG only, and only used in pads. Should be safe to test on any scalar
377    SV, as the core is careful to avoid setting both.
378
379    SVf_POK, SVp_POK also set:
380    0x00004400   Normal
381    0x0000C400   method name for DOES (SvSCREAM)
382    0x40004400   FBM compiled (SvVALID)
383    0x4000C400   pad name.
384
385    0x00008000   GV with GP
386    0x00008800   RV with PCS imported
387 */
388 #define SVpad_NAME      (SVp_SCREAM|SVpbm_VALID)
389                                     /* This SV is a name in the PAD, so
390                                        SVpad_TYPED, SVpad_OUR and SVpad_STATE
391                                        apply */
392 /* PVAV */
393 #define SVpav_REAL      0x40000000  /* free old entries */
394 /* PVHV */
395 #define SVphv_LAZYDEL   0x40000000  /* entry in xhv_eiter must be deleted */
396 /* This is only set true on a PVGV when it's playing "PVBM", but is tested for
397    on any regular scalar (anything <= PVLV) */
398 #define SVpbm_VALID     0x40000000
399 /* Only used in toke.c on an SV stored in PL_lex_repl */
400 #define SVrepl_EVAL     0x40000000  /* Replacement part of s///e */
401
402 /* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV  */
403 #define SVf_IVisUV      0x80000000  /* use XPVUV instead of XPVIV */
404 /* PVAV */
405 #define SVpav_REIFY     0x80000000  /* can become real */
406 /* PVHV */
407 #define SVphv_HASKFLAGS 0x80000000  /* keys have flag byte after hash */
408 /* PVGV when SVpbm_VALID is true */
409 #define SVpbm_TAIL      0x80000000
410 /* RV upwards. However, SVf_ROK and SVp_IOK are exclusive  */
411 #define SVprv_WEAKREF   0x80000000  /* Weak reference */
412 /* pad name vars only */
413 #define SVpad_STATE     0x80000000  /* pad name is a "state" var */
414
415 /* MSVC6 generates "error C2099: initializer is not a constant" when
416  * initializing bodies_by_type in sv.c. Workaround the compiler bug by
417  * using an anonymous union, but only for MSVC6 since that isn't C89.
418  */
419 #if defined(_MSC_VER) && _MSC_VER < 1300
420 #define _XPV_HEAD                                                       \
421     HV*         xmg_stash;      /* class package */                     \
422     union _xmgu xmg_u;                                                  \
423     union {                                                             \
424         STRLEN  xpvcuru_cur;    /* length of svu_pv as a C string */    \
425         I32     xpvcuru_fmdepth;                                        \
426     };                                                                  \
427     STRLEN      xpv_len         /* allocated size */
428 #else
429 #define _XPV_HEAD                                                       \
430     HV*         xmg_stash;      /* class package */                     \
431     union _xmgu xmg_u;                                                  \
432     union {                                                             \
433         STRLEN  xpvcuru_cur;    /* length of svu_pv as a C string */    \
434         I32     xpvcuru_fmdepth;                                        \
435     }           xpv_cur_u;                                              \
436     STRLEN      xpv_len         /* allocated size */
437 #endif
438
439 #if defined(_MSC_VER) && _MSC_VER < 1300
440 #define xpv_cur xpvcuru_cur
441 #else
442 #define xpv_cur xpv_cur_u.xpvcuru_cur
443 #endif
444
445 union _xnvu {
446     NV      xnv_nv;             /* numeric value, if any */
447     HV *    xgv_stash;
448     struct {
449         U32 xlow;
450         U32 xhigh;
451     }       xpad_cop_seq;       /* used by pad.c for cop_sequence */
452     struct {
453         I32 xbm_useful;
454         U8  xbm_rare;           /* rarest character in string */
455     }       xbm_s;              /* fields from PVBM */
456 };
457
458 union _xivu {
459     IV      xivu_iv;            /* integer value */
460     UV      xivu_uv;
461     HEK *   xivu_namehek;       /* xpvlv, xpvgv: GvNAME */
462 };
463
464 union _xmgu {
465     MAGIC*  xmg_magic;          /* linked list of magicalness */
466     HV*     xmg_ourstash;       /* Stash for our (when SvPAD_OUR is true) */
467     STRLEN  xmg_hash_index;     /* used while freeing hash entries */
468 };
469
470 struct xpv {
471     _XPV_HEAD;
472 };
473
474 struct xpviv {
475     _XPV_HEAD;
476     union _xivu xiv_u;
477 };
478
479 #define xiv_iv xiv_u.xivu_iv
480
481 struct xpvuv {
482     _XPV_HEAD;
483     union _xivu xuv_u;
484 };
485
486 #define xuv_uv xuv_u.xivu_uv
487
488 struct xpvnv {
489     _XPV_HEAD;
490     union _xivu xiv_u;
491     union _xnvu xnv_u;
492 };
493
494 /* This structure must match the beginning of struct xpvhv in hv.h. */
495 struct xpvmg {
496     _XPV_HEAD;
497     union _xivu xiv_u;
498     union _xnvu xnv_u;
499 };
500
501 struct xpvlv {
502     _XPV_HEAD;
503     union _xivu xiv_u;
504     union _xnvu xnv_u;
505     STRLEN      xlv_targoff;
506     STRLEN      xlv_targlen;
507     SV*         xlv_targ;
508     char        xlv_type;       /* k=keys .=pos x=substr v=vec /=join/re
509                                  * y=alem/helem/iter t=tie T=tied HE */
510     char        xlv_flags;      /* 1 = negative offset  2 = negative len */
511 };
512
513 /* This structure works in 3 ways - regular scalar, GV with GP, or fast
514    Boyer-Moore.  */
515 struct xpvgv {
516     _XPV_HEAD;
517     union _xivu xiv_u;
518     union _xnvu xnv_u;
519 };
520
521 typedef U16 cv_flags_t;
522
523 #define _XPVCV_COMMON                                                           \
524     HV *        xcv_stash;                                                      \
525     union {                                                                     \
526         OP *    xcv_start;                                                      \
527         ANY     xcv_xsubany;                                                    \
528     }           xcv_start_u;                                                    \
529     union {                                                                     \
530         OP *    xcv_root;                                                       \
531         void    (*xcv_xsub) (pTHX_ CV*);                                        \
532     }           xcv_root_u;                                                     \
533     GV *        xcv_gv;                                                         \
534     char *      xcv_file;                                                       \
535     AV *        xcv_padlist;                                                    \
536     CV *        xcv_outside;                                                    \
537     U32         xcv_outside_seq; /* the COP sequence (at the point of our       \
538                                   * compilation) in the lexically enclosing     \
539                                   * sub */                                      \
540     cv_flags_t  xcv_flags
541
542 /* This structure must match XPVCV in cv.h */
543
544 struct xpvfm {
545     _XPV_HEAD;
546     _XPVCV_COMMON;
547 };
548
549
550 struct xpvio {
551     _XPV_HEAD;
552     union _xivu xiv_u;
553     /* ifp and ofp are normally the same, but sockets need separate streams */
554     PerlIO *    xio_ofp;
555     /* Cray addresses everything by word boundaries (64 bits) and
556      * code and data pointers cannot be mixed (which is exactly what
557      * Perl_filter_add() tries to do with the dirp), hence the
558      *  following union trick (as suggested by Gurusamy Sarathy).
559      * For further information see Geir Johansen's problem report
560      * titled [ID 20000612.002] Perl problem on Cray system
561      * The any pointer (known as IoANY()) will also be a good place
562      * to hang any IO disciplines to.
563      */
564     union {
565         DIR *   xiou_dirp;      /* for opendir, readdir, etc */
566         void *  xiou_any;       /* for alignment */
567     } xio_dirpu;
568     /* IV xio_lines is now in IVX  $. */
569     IV          xio_page;       /* $% */
570     IV          xio_page_len;   /* $= */
571     IV          xio_lines_left; /* $- */
572     char *      xio_top_name;   /* $^ */
573     GV *        xio_top_gv;     /* $^ */
574     char *      xio_fmt_name;   /* $~ */
575     GV *        xio_fmt_gv;     /* $~ */
576     char *      xio_bottom_name;/* $^B */
577     GV *        xio_bottom_gv;  /* $^B */
578     char        xio_type;
579     U8          xio_flags;
580 };
581
582 #define xio_dirp        xio_dirpu.xiou_dirp
583 #define xio_any         xio_dirpu.xiou_any
584
585 #define IOf_ARGV        1       /* this fp iterates over ARGV */
586 #define IOf_START       2       /* check for null ARGV and substitute '-' */
587 #define IOf_FLUSH       4       /* this fp wants a flush after write op */
588 #define IOf_DIDTOP      8       /* just did top of form */
589 #define IOf_UNTAINT     16      /* consider this fp (and its data) "safe" */
590 #define IOf_NOLINE      32      /* slurped a pseudo-line from empty file */
591 #define IOf_FAKE_DIRP   64      /* xio_dirp is fake (source filters kludge)
592                                    Also, when this is set, SvPVX() is valid */
593
594 /* The following macros define implementation-independent predicates on SVs. */
595
596 /*
597 =for apidoc Am|U32|SvNIOK|SV* sv
598 Returns a U32 value indicating whether the SV contains a number, integer or
599 double.
600
601 =for apidoc Am|U32|SvNIOKp|SV* sv
602 Returns a U32 value indicating whether the SV contains a number, integer or
603 double.  Checks the B<private> setting.  Use C<SvNIOK> instead.
604
605 =for apidoc Am|void|SvNIOK_off|SV* sv
606 Unsets the NV/IV status of an SV.
607
608 =for apidoc Am|U32|SvOK|SV* sv
609 Returns a U32 value indicating whether the value is defined. This is
610 only meaningful for scalars.
611
612 =for apidoc Am|U32|SvIOKp|SV* sv
613 Returns a U32 value indicating whether the SV contains an integer.  Checks
614 the B<private> setting.  Use C<SvIOK> instead.
615
616 =for apidoc Am|U32|SvNOKp|SV* sv
617 Returns a U32 value indicating whether the SV contains a double.  Checks the
618 B<private> setting.  Use C<SvNOK> instead.
619
620 =for apidoc Am|U32|SvPOKp|SV* sv
621 Returns a U32 value indicating whether the SV contains a character string.
622 Checks the B<private> setting.  Use C<SvPOK> instead.
623
624 =for apidoc Am|U32|SvIOK|SV* sv
625 Returns a U32 value indicating whether the SV contains an integer.
626
627 =for apidoc Am|void|SvIOK_on|SV* sv
628 Tells an SV that it is an integer.
629
630 =for apidoc Am|void|SvIOK_off|SV* sv
631 Unsets the IV status of an SV.
632
633 =for apidoc Am|void|SvIOK_only|SV* sv
634 Tells an SV that it is an integer and disables all other OK bits.
635
636 =for apidoc Am|void|SvIOK_only_UV|SV* sv
637 Tells and SV that it is an unsigned integer and disables all other OK bits.
638
639 =for apidoc Am|bool|SvIOK_UV|SV* sv
640 Returns a boolean indicating whether the SV contains an unsigned integer.
641
642 =for apidoc Am|bool|SvUOK|SV* sv
643 Returns a boolean indicating whether the SV contains an unsigned integer.
644
645 =for apidoc Am|bool|SvIOK_notUV|SV* sv
646 Returns a boolean indicating whether the SV contains a signed integer.
647
648 =for apidoc Am|U32|SvNOK|SV* sv
649 Returns a U32 value indicating whether the SV contains a double.
650
651 =for apidoc Am|void|SvNOK_on|SV* sv
652 Tells an SV that it is a double.
653
654 =for apidoc Am|void|SvNOK_off|SV* sv
655 Unsets the NV status of an SV.
656
657 =for apidoc Am|void|SvNOK_only|SV* sv
658 Tells an SV that it is a double and disables all other OK bits.
659
660 =for apidoc Am|U32|SvPOK|SV* sv
661 Returns a U32 value indicating whether the SV contains a character
662 string.
663
664 =for apidoc Am|void|SvPOK_on|SV* sv
665 Tells an SV that it is a string.
666
667 =for apidoc Am|void|SvPOK_off|SV* sv
668 Unsets the PV status of an SV.
669
670 =for apidoc Am|void|SvPOK_only|SV* sv
671 Tells an SV that it is a string and disables all other OK bits.
672 Will also turn off the UTF-8 status.
673
674 =for apidoc Am|bool|SvVOK|SV* sv
675 Returns a boolean indicating whether the SV contains a v-string.
676
677 =for apidoc Am|U32|SvOOK|SV* sv
678 Returns a U32 indicating whether the pointer to the string buffer is offset.
679 This hack is used internally to speed up removal of characters from the
680 beginning of a SvPV.  When SvOOK is true, then the start of the
681 allocated string buffer is actually C<SvOOK_offset()> bytes before SvPVX.
682 This offset used to be stored in SvIVX, but is now stored within the spare
683 part of the buffer.
684
685 =for apidoc Am|U32|SvROK|SV* sv
686 Tests if the SV is an RV.
687
688 =for apidoc Am|void|SvROK_on|SV* sv
689 Tells an SV that it is an RV.
690
691 =for apidoc Am|void|SvROK_off|SV* sv
692 Unsets the RV status of an SV.
693
694 =for apidoc Am|SV*|SvRV|SV* sv
695 Dereferences an RV to return the SV.
696
697 =for apidoc Am|IV|SvIVX|SV* sv
698 Returns the raw value in the SV's IV slot, without checks or conversions.
699 Only use when you are sure SvIOK is true.  See also C<SvIV()>.
700
701 =for apidoc Am|UV|SvUVX|SV* sv
702 Returns the raw value in the SV's UV slot, without checks or conversions.
703 Only use when you are sure SvIOK is true.  See also C<SvUV()>.
704
705 =for apidoc Am|NV|SvNVX|SV* sv
706 Returns the raw value in the SV's NV slot, without checks or conversions.
707 Only use when you are sure SvNOK is true.  See also C<SvNV()>.
708
709 =for apidoc Am|char*|SvPVX|SV* sv
710 Returns a pointer to the physical string in the SV.  The SV must contain a
711 string.
712
713 This is also used to store the name of an autoloaded subroutine in an XS
714 AUTOLOAD routine.  See L<perlguts/Autoloading with XSUBs>.
715
716 =for apidoc Am|STRLEN|SvCUR|SV* sv
717 Returns the length of the string which is in the SV.  See C<SvLEN>.
718
719 =for apidoc Am|STRLEN|SvLEN|SV* sv
720 Returns the size of the string buffer in the SV, not including any part
721 attributable to C<SvOOK>.  See C<SvCUR>.
722
723 =for apidoc Am|char*|SvEND|SV* sv
724 Returns a pointer to the spot just after the last character in
725 the string which is in the SV, where there is usually a trailing
726 null (even though Perl scalars do not strictly require it).
727 See C<SvCUR>.  Access the character as *(SvEND(sv)).
728
729 Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
730 unallocated memory.
731
732 =for apidoc Am|HV*|SvSTASH|SV* sv
733 Returns the stash of the SV.
734
735 =for apidoc Am|void|SvIV_set|SV* sv|IV val
736 Set the value of the IV pointer in sv to val.  It is possible to perform
737 the same function of this macro with an lvalue assignment to C<SvIVX>.
738 With future Perls, however, it will be more efficient to use 
739 C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
740
741 =for apidoc Am|void|SvNV_set|SV* sv|NV val
742 Set the value of the NV pointer in sv to val.  See C<SvIV_set>.
743
744 =for apidoc Am|void|SvPV_set|SV* sv|char* val
745 Set the value of the PV pointer in sv to val.  See C<SvIV_set>.
746
747 =for apidoc Am|void|SvUV_set|SV* sv|UV val
748 Set the value of the UV pointer in sv to val.  See C<SvIV_set>.
749
750 =for apidoc Am|void|SvRV_set|SV* sv|SV* val
751 Set the value of the RV pointer in sv to val.  See C<SvIV_set>.
752
753 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
754 Set the value of the MAGIC pointer in sv to val.  See C<SvIV_set>.
755
756 =for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
757 Set the value of the STASH pointer in sv to val.  See C<SvIV_set>.
758
759 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
760 Set the current length of the string which is in the SV.  See C<SvCUR>
761 and C<SvIV_set>.
762
763 =for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
764 Set the actual length of the string which is in the SV.  See C<SvIV_set>.
765
766 =cut
767 */
768
769 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
770 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
771 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
772                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
773
774 #define assert_not_ROK(sv)      assert_(!SvROK(sv) || !SvRV(sv))
775 #define assert_not_glob(sv)     assert_(!isGV_with_GP(sv))
776
777 #define SvOK(sv)                ((SvTYPE(sv) == SVt_BIND)               \
778                                  ? (SvFLAGS(SvRV(sv)) & SVf_OK)         \
779                                  : (SvFLAGS(sv) & SVf_OK))
780 #define SvOK_off(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
781                                  SvFLAGS(sv) &= ~(SVf_OK|               \
782                                                   SVf_IVisUV|SVf_UTF8), \
783                                                         SvOOK_off(sv))
784 #define SvOK_off_exc_UV(sv)     (assert_not_ROK(sv)                     \
785                                  SvFLAGS(sv) &= ~(SVf_OK|               \
786                                                   SVf_UTF8),            \
787                                                         SvOOK_off(sv))
788
789 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
790 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
791 #define SvIOKp_on(sv)           (assert_not_glob(sv) SvRELEASE_IVX_(sv) \
792                                     SvFLAGS(sv) |= SVp_IOK)
793 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
794 #define SvNOKp_on(sv)           (assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
795 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
796 #define SvPOKp_on(sv)           (assert_not_ROK(sv) assert_not_glob(sv) \
797                                  SvFLAGS(sv) |= SVp_POK)
798
799 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
800 #define SvIOK_on(sv)            (assert_not_glob(sv) SvRELEASE_IVX_(sv) \
801                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
802 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
803 #define SvIOK_only(sv)          (SvOK_off(sv), \
804                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
805 #define SvIOK_only_UV(sv)       (assert_not_glob(sv) SvOK_off_exc_UV(sv), \
806                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
807
808 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
809                                  == (SVf_IOK|SVf_IVisUV))
810 #define SvUOK(sv)               SvIOK_UV(sv)
811 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
812                                  == SVf_IOK)
813
814 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
815 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
816 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
817
818 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
819 #define SvNOK_on(sv)            (assert_not_glob(sv) \
820                                  SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
821 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
822 #define SvNOK_only(sv)          (SvOK_off(sv), \
823                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
824
825 /*
826 =for apidoc Am|U32|SvUTF8|SV* sv
827 Returns a U32 value indicating the UTF-8 status of an SV.  If things are set-up
828 properly, this indicates whether or not the SV contains UTF-8 encoded data.
829 Call this after SvPV() in case any call to string overloading updates the
830 internal flag.
831
832 =for apidoc Am|void|SvUTF8_on|SV *sv
833 Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
834 Do not use frivolously.
835
836 =for apidoc Am|void|SvUTF8_off|SV *sv
837 Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
838 Do not use frivolously.
839
840 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
841 Tells an SV that it is a string and disables all other OK bits,
842 and leaves the UTF-8 status as it was.
843
844 =cut
845  */
846
847 /* Ensure the return value of this macro does not clash with the GV_ADD* flags
848 in gv.h: */
849 #define SvUTF8(sv)              (SvFLAGS(sv) & SVf_UTF8)
850 #define SvUTF8_on(sv)           (SvFLAGS(sv) |= (SVf_UTF8))
851 #define SvUTF8_off(sv)          (SvFLAGS(sv) &= ~(SVf_UTF8))
852
853 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
854 #define SvPOK_on(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
855                                  SvFLAGS(sv) |= (SVf_POK|SVp_POK))
856 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
857 #define SvPOK_only(sv)          (assert_not_ROK(sv) assert_not_glob(sv) \
858                                  SvFLAGS(sv) &= ~(SVf_OK|               \
859                                                   SVf_IVisUV|SVf_UTF8), \
860                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
861 #define SvPOK_only_UTF8(sv)     (assert_not_ROK(sv) assert_not_glob(sv) \
862                                  SvFLAGS(sv) &= ~(SVf_OK|               \
863                                                   SVf_IVisUV),          \
864                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
865
866 #define SvVOK(sv)               (SvMAGICAL(sv)                          \
867                                  && mg_find(sv,PERL_MAGIC_vstring))
868 /* returns the vstring magic, if any */
869 #define SvVSTRING_mg(sv)        (SvMAGICAL(sv) \
870                                  ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
871
872 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
873 #define SvOOK_on(sv)            (SvFLAGS(sv) |= SVf_OOK)
874 #define SvOOK_off(sv)           ((void)(SvOOK(sv) && sv_backoff(sv)))
875
876 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
877 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
878 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
879
880 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
881 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
882 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK))
883
884 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
885 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
886 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
887
888 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
889 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
890 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
891
892 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
893 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
894 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
895
896 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
897 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
898 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
899
900 #define SvAMAGIC(sv)            (SvROK(sv) && SvOBJECT(SvRV(sv)) &&     \
901                                  HvAMAGIC(SvSTASH(SvRV(sv))))
902 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
903 #  define SvAMAGIC_on(sv)       ({ SV * const kloink = sv;              \
904                                    assert(SvROK(kloink));               \
905                                    if (SvOBJECT(SvRV(kloink)))          \
906                                     HvAMAGIC_on(SvSTASH(SvRV(kloink))); \
907                                 })
908 #  define SvAMAGIC_off(sv)      ({ SV * const kloink = sv;              \
909                                    if(SvROK(kloink)                     \
910                                       && SvOBJECT(SvRV(kloink)))        \
911                                      HvAMAGIC_off(SvSTASH(SvRV(kloink))); \
912                                 })
913 #else
914 #  define SvAMAGIC_on(sv) \
915         SvOBJECT(SvRV(sv)) && (SvFLAGS(SvSTASH(SvRV(sv))) |= SVf_AMAGIC)
916 #  define SvAMAGIC_off(sv) \
917         (SvROK(sv) && SvOBJECT(SvRV(sv)) \
918             && (SvFLAGS(SvSTASH(SvRV(sv))) &= ~SVf_AMAGIC))
919 #endif
920
921 /* To be used on the stashes themselves: */
922 #define HvAMAGIC(hv)            (SvFLAGS(hv) & SVf_AMAGIC)
923 #define HvAMAGIC_on(hv)         (SvFLAGS(hv) |= SVf_AMAGIC)
924 #define HvAMAGIC_off(hv)        (SvFLAGS(hv) &=~ SVf_AMAGIC)
925
926
927 #define SvPOK_nog(sv)           ((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
928 #define SvIOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
929 #define SvUOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
930 #define SvNOK_nog(sv)           ((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
931 #define SvNIOK_nog(sv)          (SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))
932
933 #define SvPOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
934 #define SvIOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
935 #define SvUOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
936 #define SvNOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
937 #define SvNIOK_nogthink(sv)     (SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))
938
939 #define SvPOK_utf8_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
940 #define SvPOK_utf8_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
941
942 #define SvPOK_byte_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
943 #define SvPOK_byte_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
944
945 #define SvPOK_pure_nogthink(sv) \
946     ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
947 #define SvPOK_utf8_pure_nogthink(sv) \
948     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
949 #define SvPOK_byte_pure_nogthink(sv) \
950     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
951
952 /*
953 =for apidoc Am|U32|SvGAMAGIC|SV* sv
954
955 Returns true if the SV has get magic or
956 overloading.  If either is true then
957 the scalar is active data, and has the potential to return a new value every
958 time it is accessed.  Hence you must be careful to
959 only read it once per user logical operation and work
960 with that returned value.  If neither is true then
961 the scalar's value cannot change unless written to.
962
963 =cut
964 */
965
966 #define SvGAMAGIC(sv)           (SvGMAGICAL(sv) || SvAMAGIC(sv))
967
968 #define Gv_AMG(stash) \
969         (HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
970             ? 1                                     \
971             : (HvAMAGIC_off(stash), 0))
972
973 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
974                                   == (SVf_ROK|SVprv_WEAKREF))
975 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
976 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
977
978 #define SvPCS_IMPORTED(sv)      ((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
979                                  == (SVf_ROK|SVprv_PCS_IMPORTED))
980 #define SvPCS_IMPORTED_on(sv)   (SvFLAGS(sv) |=  (SVf_ROK|SVprv_PCS_IMPORTED))
981 #define SvPCS_IMPORTED_off(sv)  (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))
982
983 /*
984 =for apidoc m|U32|SvTHINKFIRST|SV *sv
985
986 A quick flag check to see whether an sv should be passed to sv_force_normal
987 to be "downgraded" before SvIVX or SvPVX can be modified directly.
988
989 For example, if your scalar is a reference and you want to modify the SvIVX
990 slot, you can't just do SvROK_off, as that will leak the referent.
991
992 This is used internally by various sv-modifying functions, such as
993 sv_setsv, sv_setiv and sv_pvn_force.
994
995 One case that this does not handle is a gv without SvFAKE set.  After
996
997     if (SvTHINKFIRST(gv)) sv_force_normal(gv);
998
999 it will still be a gv.
1000
1001 SvTHINKFIRST sometimes produces false positives.  In those cases
1002 sv_force_normal does nothing.
1003
1004 =cut
1005 */
1006
1007 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
1008
1009 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
1010 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY)
1011
1012 /* SVs_PADTMP and SVs_PADSTALE share the same bit, mediated by SVs_PADMY */
1013
1014 #define SvPADTMP(sv)    ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP)) == SVs_PADTMP)
1015 #define SvPADSTALE(sv)  ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADSTALE)) \
1016                                     == (SVs_PADMY|SVs_PADSTALE))
1017
1018 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1019 #  define SvPADTMP_on(sv)       ({                      \
1020             SV *const _svpad = MUTABLE_SV(sv);          \
1021             assert(!(SvFLAGS(_svpad) & SVs_PADMY));     \
1022             SvFLAGS(_svpad) |= SVs_PADTMP;              \
1023         })
1024 #  define SvPADTMP_off(sv)      ({                      \
1025             SV *const _svpad = MUTABLE_SV(sv);          \
1026             assert(!(SvFLAGS(_svpad) & SVs_PADMY));     \
1027             SvFLAGS(_svpad) &= ~SVs_PADTMP;             \
1028         })
1029 #  define SvPADSTALE_on(sv)     ({                      \
1030             SV *const _svpad = MUTABLE_SV(sv);          \
1031             assert(SvFLAGS(_svpad) & SVs_PADMY);        \
1032             SvFLAGS(_svpad) |= SVs_PADSTALE;            \
1033         })
1034 #  define SvPADSTALE_off(sv)    ({                      \
1035             SV *const _svpad = MUTABLE_SV(sv);          \
1036             assert(SvFLAGS(_svpad) & SVs_PADMY);        \
1037             SvFLAGS(_svpad) &= ~SVs_PADSTALE;           \
1038         })
1039 #else
1040 #  define SvPADTMP_on(sv)       (SvFLAGS(sv) |= SVs_PADTMP)
1041 #  define SvPADTMP_off(sv)      (SvFLAGS(sv) &= ~SVs_PADTMP)
1042 #  define SvPADSTALE_on(sv)     (SvFLAGS(sv) |= SVs_PADSTALE)
1043 #  define SvPADSTALE_off(sv)    (SvFLAGS(sv) &= ~SVs_PADSTALE)
1044 #endif
1045
1046 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
1047 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
1048 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
1049
1050 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
1051 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
1052 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
1053
1054 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
1055 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
1056 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
1057
1058 #define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
1059 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
1060 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
1061
1062 #ifndef PERL_CORE
1063 #  define SvCOMPILED(sv)        0
1064 #  define SvCOMPILED_on(sv)
1065 #  define SvCOMPILED_off(sv)
1066 #endif
1067
1068 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
1069 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
1070 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
1071
1072 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1073 #  define SvVALID(sv)           ({ const SV *const _svvalid = (const SV*)(sv); \
1074                                    if (SvFLAGS(_svvalid) & SVpbm_VALID) \
1075                                        assert(!isGV_with_GP(_svvalid)); \
1076                                    (SvFLAGS(_svvalid) & SVpbm_VALID);   \
1077                                 })
1078 #  define SvVALID_on(sv)        ({ SV *const _svvalid = MUTABLE_SV(sv); \
1079                                    assert(!isGV_with_GP(_svvalid));     \
1080                                    (SvFLAGS(_svvalid) |= SVpbm_VALID);  \
1081                                 })
1082 #  define SvVALID_off(sv)       ({ SV *const _svvalid = MUTABLE_SV(sv); \
1083                                    assert(!isGV_with_GP(_svvalid));     \
1084                                    (SvFLAGS(_svvalid) &= ~SVpbm_VALID); \
1085                                 })
1086
1087 #  define SvTAIL(sv)    ({ const SV *const _svtail = (const SV *)(sv);  \
1088                             assert(SvTYPE(_svtail) != SVt_PVAV);                \
1089                             assert(SvTYPE(_svtail) != SVt_PVHV);                \
1090                             (SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))    \
1091                                 == (SVpbm_TAIL|SVpbm_VALID);            \
1092                         })
1093 #else
1094 #  define SvVALID(sv)           (SvFLAGS(sv) & SVpbm_VALID)
1095 #  define SvVALID_on(sv)        (SvFLAGS(sv) |= SVpbm_VALID)
1096 #  define SvVALID_off(sv)       (SvFLAGS(sv) &= ~SVpbm_VALID)
1097 #  define SvTAIL(sv)        ((SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))   \
1098                              == (SVpbm_TAIL|SVpbm_VALID))
1099
1100 #endif
1101 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
1102 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
1103
1104
1105 #define SvPAD_TYPED(sv) \
1106         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_TYPED)) == (SVpad_NAME|SVpad_TYPED))
1107
1108 #define SvPAD_OUR(sv)   \
1109         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR))
1110
1111 #define SvPAD_STATE(sv) \
1112         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_STATE)) == (SVpad_NAME|SVpad_STATE))
1113
1114 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1115 #  define SvPAD_TYPED_on(sv)    ({                                      \
1116             SV *const _svpad = MUTABLE_SV(sv);                          \
1117             assert(SvTYPE(_svpad) == SVt_PVMG);                         \
1118             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_TYPED);                \
1119         })
1120 #define SvPAD_OUR_on(sv)        ({                                      \
1121             SV *const _svpad = MUTABLE_SV(sv);                          \
1122             assert(SvTYPE(_svpad) == SVt_PVMG);                         \
1123             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_OUR);                  \
1124         })
1125 #define SvPAD_STATE_on(sv)      ({                                      \
1126             SV *const _svpad = MUTABLE_SV(sv);                          \
1127             assert(SvTYPE(_svpad) == SVt_PVNV || SvTYPE(_svpad) == SVt_PVMG); \
1128             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_STATE);                \
1129         })
1130 #else
1131 #  define SvPAD_TYPED_on(sv)    (SvFLAGS(sv) |= SVpad_NAME|SVpad_TYPED)
1132 #  define SvPAD_OUR_on(sv)      (SvFLAGS(sv) |= SVpad_NAME|SVpad_OUR)
1133 #  define SvPAD_STATE_on(sv)    (SvFLAGS(sv) |= SVpad_NAME|SVpad_STATE)
1134 #endif
1135
1136 #define SvOURSTASH(sv)  \
1137         (SvPAD_OUR(sv) ? ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash : NULL)
1138 #define SvOURSTASH_set(sv, st)                                  \
1139         STMT_START {                                            \
1140             assert(SvTYPE(sv) == SVt_PVMG);                     \
1141             ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash = st;      \
1142         } STMT_END
1143
1144 #define SvRVx(sv) SvRV(sv)
1145
1146 #ifdef PERL_DEBUG_COW
1147 /* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
1148    +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
1149 #  define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
1150 #  define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
1151 #  define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
1152 #  define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
1153 #  define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1154 /* Don't test the core XS code yet.  */
1155 #  if defined (PERL_CORE) && PERL_DEBUG_COW > 1
1156 #    define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
1157 #  else
1158 #  define SvPVX(sv) SvPVX_mutable(sv)
1159 #  endif
1160 #  define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
1161 #  define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1162 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1163
1164 #  define SvMAGIC(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic))
1165 #  define SvSTASH(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_stash))
1166 #else
1167 #  define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
1168 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1169
1170 #  if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1171 /* These get expanded inside other macros that already use a variable _sv  */
1172 #    define SvPVX(sv)                                                   \
1173         (*({ SV *const _svpvx = MUTABLE_SV(sv);                         \
1174             assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]);      \
1175             assert(!isGV_with_GP(_svpvx));                              \
1176             assert(!(SvTYPE(_svpvx) == SVt_PVIO                         \
1177                      && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP)));           \
1178             &((_svpvx)->sv_u.svu_pv);                                   \
1179          }))
1180 #    define SvCUR(sv)                                                   \
1181         (*({ const SV *const _svcur = (const SV *)(sv);                 \
1182             assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]);      \
1183             assert(!isGV_with_GP(_svcur));                              \
1184             assert(!(SvTYPE(_svcur) == SVt_PVIO                         \
1185                      && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP)));           \
1186             &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur);            \
1187          }))
1188 #    define SvIVX(sv)                                                   \
1189         (*({ const SV *const _svivx = (const SV *)(sv);                 \
1190             assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]);      \
1191             assert(!isGV_with_GP(_svivx));                              \
1192             &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv);           \
1193          }))
1194 #    define SvUVX(sv)                                                   \
1195         (*({ const SV *const _svuvx = (const SV *)(sv);                 \
1196             assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]);      \
1197             assert(!isGV_with_GP(_svuvx));                              \
1198             &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv);           \
1199          }))
1200 #    define SvNVX(sv)                                                   \
1201         (*({ const SV *const _svnvx = (const SV *)(sv);                 \
1202             assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]);      \
1203             assert(!isGV_with_GP(_svnvx));                              \
1204             &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv);     \
1205          }))
1206 #    define SvRV(sv)                                                    \
1207         (*({ SV *const _svrv = MUTABLE_SV(sv);                          \
1208             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1209             assert(!isGV_with_GP(_svrv));                               \
1210             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1211                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1212             &((_svrv)->sv_u.svu_rv);                                    \
1213          }))
1214 #    define SvRV_const(sv)                                              \
1215         ({ const SV *const _svrv = (const SV *)(sv);                    \
1216             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1217             assert(!isGV_with_GP(_svrv));                               \
1218             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1219                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1220             (_svrv)->sv_u.svu_rv;                                       \
1221          })
1222 #    define SvMAGIC(sv)                                                 \
1223         (*({ const SV *const _svmagic = (const SV *)(sv);               \
1224             assert(SvTYPE(_svmagic) >= SVt_PVMG);                       \
1225             if(SvTYPE(_svmagic) == SVt_PVMG)                            \
1226                 assert(!SvPAD_OUR(_svmagic));                           \
1227             &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1228           }))
1229 #    define SvSTASH(sv)                                                 \
1230         (*({ const SV *const _svstash = (const SV *)(sv);               \
1231             assert(SvTYPE(_svstash) >= SVt_PVMG);                       \
1232             &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash);      \
1233           }))
1234 #  else
1235 #    define SvPVX(sv) ((sv)->sv_u.svu_pv)
1236 #    define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
1237 #    define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1238 #    define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
1239 #    define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
1240 #    define SvRV(sv) ((sv)->sv_u.svu_rv)
1241 #    define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1242 #    define SvMAGIC(sv) ((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic
1243 #    define SvSTASH(sv) ((XPVMG*)  SvANY(sv))->xmg_stash
1244 #  endif
1245 #endif
1246
1247 #ifndef PERL_POISON
1248 /* Given that these two are new, there can't be any existing code using them
1249  *  as LVALUEs  */
1250 #  define SvPVX_mutable(sv)     (0 + (sv)->sv_u.svu_pv)
1251 #  define SvPVX_const(sv)       ((const char*)(0 + (sv)->sv_u.svu_pv))
1252 #else
1253 /* Except for the poison code, which uses & to scribble over the pointer after
1254    free() is called.  */
1255 #  define SvPVX_mutable(sv)     ((sv)->sv_u.svu_pv)
1256 #  define SvPVX_const(sv)       ((const char*)((sv)->sv_u.svu_pv))
1257 #endif
1258
1259 #define SvIVXx(sv) SvIVX(sv)
1260 #define SvUVXx(sv) SvUVX(sv)
1261 #define SvNVXx(sv) SvNVX(sv)
1262 #define SvPVXx(sv) SvPVX(sv)
1263 #define SvLENx(sv) SvLEN(sv)
1264 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1265
1266
1267 /* Ask a scalar nicely to try to become an IV, if possible.
1268    Not guaranteed to stay returning void */
1269 /* Macro won't actually call sv_2iv if already IOK */
1270 #define SvIV_please(sv) \
1271         STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
1272                 (void) SvIV(sv); } STMT_END
1273 #define SvIV_please_nomg(sv) \
1274         (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv)) \
1275             ? (SvIV_nomg(sv), SvIOK(sv))          \
1276             : SvIOK(sv))
1277 #define SvIV_set(sv, val) \
1278         STMT_START { \
1279                 assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);   \
1280                 assert(!isGV_with_GP(sv));              \
1281                 (((XPVIV*)  SvANY(sv))->xiv_iv = (val)); } STMT_END
1282 #define SvNV_set(sv, val) \
1283         STMT_START { \
1284                 assert(PL_valid_types_NV_set[SvTYPE(sv) & SVt_MASK]);   \
1285                 assert(!isGV_with_GP(sv));              \
1286                 (((XPVNV*)SvANY(sv))->xnv_u.xnv_nv = (val)); } STMT_END
1287 #define SvPV_set(sv, val) \
1288         STMT_START { \
1289                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1290                 assert(!isGV_with_GP(sv));              \
1291                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1292                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1293                 ((sv)->sv_u.svu_pv = (val)); } STMT_END
1294 #define SvUV_set(sv, val) \
1295         STMT_START { \
1296                 assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);   \
1297                 assert(!isGV_with_GP(sv));              \
1298                 (((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
1299 #define SvRV_set(sv, val) \
1300         STMT_START { \
1301                 assert(PL_valid_types_RV[SvTYPE(sv) & SVt_MASK]);       \
1302                 assert(!isGV_with_GP(sv));              \
1303                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1304                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1305                 ((sv)->sv_u.svu_rv = (val)); } STMT_END
1306 #define SvMAGIC_set(sv, val) \
1307         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1308                 (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
1309 #define SvSTASH_set(sv, val) \
1310         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1311                 (((XPVMG*)  SvANY(sv))->xmg_stash = (val)); } STMT_END
1312 #define SvCUR_set(sv, val) \
1313         STMT_START { \
1314                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1315                 assert(!isGV_with_GP(sv));              \
1316                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1317                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1318                 (((XPV*)  SvANY(sv))->xpv_cur = (val)); } STMT_END
1319 #define SvLEN_set(sv, val) \
1320         STMT_START { \
1321                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1322                 assert(!isGV_with_GP(sv));      \
1323                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1324                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1325                 (((XPV*)  SvANY(sv))->xpv_len = (val)); } STMT_END
1326 #define SvEND_set(sv, val) \
1327         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1328                 SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
1329
1330 #define SvPV_renew(sv,n) \
1331         STMT_START { SvLEN_set(sv, n); \
1332                 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char)                 \
1333                                 (char*)saferealloc((Malloc_t)SvPVX(sv), \
1334                                                    (MEM_SIZE)((n)))));  \
1335                  } STMT_END
1336
1337 #define SvPV_shrink_to_cur(sv) STMT_START { \
1338                    const STRLEN _lEnGtH = SvCUR(sv) + 1; \
1339                    SvPV_renew(sv, _lEnGtH); \
1340                  } STMT_END
1341
1342 #define SvPV_free(sv)                                                   \
1343     STMT_START {                                                        \
1344                      assert(SvTYPE(sv) >= SVt_PV);                      \
1345                      if (SvLEN(sv)) {                                   \
1346                          assert(!SvROK(sv));                            \
1347                          if(SvOOK(sv)) {                                \
1348                              STRLEN zok;                                \
1349                              SvOOK_offset(sv, zok);                     \
1350                              SvPV_set(sv, SvPVX_mutable(sv) - zok);     \
1351                              SvFLAGS(sv) &= ~SVf_OOK;                   \
1352                          }                                              \
1353                          Safefree(SvPVX(sv));                           \
1354                      }                                                  \
1355                  } STMT_END
1356
1357 #ifdef PERL_CORE
1358 /* Code that crops up in three places to take a scalar and ready it to hold
1359    a reference */
1360 #  define prepare_SV_for_RV(sv)                                         \
1361     STMT_START {                                                        \
1362                     if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)    \
1363                         sv_upgrade(sv, SVt_IV);                         \
1364                     else if (SvTYPE(sv) >= SVt_PV) {                    \
1365                         SvPV_free(sv);                                  \
1366                         SvLEN_set(sv, 0);                               \
1367                         SvCUR_set(sv, 0);                               \
1368                     }                                                   \
1369                  } STMT_END
1370 #endif
1371
1372 #ifndef PERL_CORE
1373 #  define BmFLAGS(sv)           (SvTAIL(sv) ? FBMcf_TAIL : 0)
1374 #endif
1375
1376 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1377 #  define BmRARE(sv)                                                    \
1378         (*({ SV *const _bmrare = MUTABLE_SV(sv);                        \
1379                 assert(SvTYPE(_bmrare) == SVt_PVMG);                    \
1380                 assert(SvVALID(_bmrare));                               \
1381             &(((XPVMG*) SvANY(_bmrare))->xnv_u.xbm_s.xbm_rare);         \
1382          }))
1383 #  define BmUSEFUL(sv)                                                  \
1384         (*({ SV *const _bmuseful = MUTABLE_SV(sv);                      \
1385             assert(SvTYPE(_bmuseful) == SVt_PVMG);                      \
1386             assert(SvVALID(_bmuseful));                                 \
1387             assert(!SvIOK(_bmuseful));                                  \
1388             &(((XPVMG*) SvANY(_bmuseful))->xnv_u.xbm_s.xbm_useful);     \
1389          }))
1390 #  define BmPREVIOUS(sv)                                                \
1391     (*({ SV *const _bmprevious = MUTABLE_SV(sv);                        \
1392                 assert(SvTYPE(_bmprevious) == SVt_PVMG);                \
1393                 assert(SvVALID(_bmprevious));                           \
1394             &(((XPVMG*) SvANY(_bmprevious))->xiv_u.xivu_uv);            \
1395          }))
1396 #else
1397 #  define BmRARE(sv)            ((XPVMG*) SvANY(sv))->xnv_u.xbm_s.xbm_rare
1398 #  define BmUSEFUL(sv)          ((XPVMG*) SvANY(sv))->xnv_u.xbm_s.xbm_useful
1399 #  define BmPREVIOUS(sv)        ((XPVMG*) SvANY(sv))->xiv_u.xivu_uv
1400
1401 #endif
1402
1403 #define FmLINES(sv)     ((XPVIV*)  SvANY(sv))->xiv_iv
1404
1405 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
1406 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
1407 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
1408 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
1409 #define LvFLAGS(sv)     ((XPVLV*)  SvANY(sv))->xlv_flags
1410
1411 #define IoIFP(sv)       (sv)->sv_u.svu_fp
1412 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
1413 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
1414 #define IoANY(sv)       ((XPVIO*)  SvANY(sv))->xio_any
1415 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xiv_u.xivu_iv
1416 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
1417 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
1418 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
1419 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
1420 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
1421 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
1422 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
1423 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
1424 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
1425 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
1426 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
1427
1428 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
1429 #define IoTYPE_RDONLY           '<'
1430 #define IoTYPE_WRONLY           '>'
1431 #define IoTYPE_RDWR             '+'
1432 #define IoTYPE_APPEND           'a'
1433 #define IoTYPE_PIPE             '|'
1434 #define IoTYPE_STD              '-'     /* stdin or stdout */
1435 #define IoTYPE_SOCKET           's'
1436 #define IoTYPE_CLOSED           ' '
1437 #define IoTYPE_IMPLICIT         'I'     /* stdin or stdout or stderr */
1438 #define IoTYPE_NUMERIC          '#'     /* fdopen */
1439
1440 /*
1441 =for apidoc Am|bool|SvTAINTED|SV* sv
1442 Checks to see if an SV is tainted.  Returns TRUE if it is, FALSE if
1443 not.
1444
1445 =for apidoc Am|void|SvTAINTED_on|SV* sv
1446 Marks an SV as tainted if tainting is enabled.
1447
1448 =for apidoc Am|void|SvTAINTED_off|SV* sv
1449 Untaints an SV.  Be I<very> careful with this routine, as it short-circuits
1450 some of Perl's fundamental security features.  XS module authors should not
1451 use this function unless they fully understand all the implications of
1452 unconditionally untainting the value. Untainting should be done in the
1453 standard perl fashion, via a carefully crafted regexp, rather than directly
1454 untainting variables.
1455
1456 =for apidoc Am|void|SvTAINT|SV* sv
1457 Taints an SV if tainting is enabled, and if some input to the current
1458 expression is tainted--usually a variable, but possibly also implicit
1459 inputs such as locale settings.  C<SvTAINT> propagates that taintedness to
1460 the outputs of an expression in a pessimistic fashion; i.e., without paying
1461 attention to precisely which outputs are influenced by which inputs.
1462
1463 =cut
1464 */
1465
1466 #define sv_taint(sv)      sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
1467
1468 #define SvTAINTED(sv)     (SvMAGICAL(sv) && sv_tainted(sv))
1469 #define SvTAINTED_on(sv)  STMT_START{ if(PL_tainting){sv_taint(sv);}   }STMT_END
1470 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
1471
1472 #define SvTAINT(sv)                     \
1473     STMT_START {                        \
1474         if (PL_tainting) {              \
1475             if (PL_tainted)             \
1476                 SvTAINTED_on(sv);       \
1477         }                               \
1478     } STMT_END
1479
1480 /*
1481 =for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
1482 Like C<SvPV> but will force the SV into containing a string (C<SvPOK>), and
1483 only a string (C<SvPOK_only>), by hook or by crook.  You want force if you are
1484 going to update the C<SvPVX> directly.  Processes get magic.
1485
1486 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
1487 Like C<SvPV> but will force the SV into containing a string (C<SvPOK>), and
1488 only a string (C<SvPOK_only>), by hook or by crook.  You want force if you are
1489 going to update the C<SvPVX> directly.  Doesn't process get magic.
1490
1491 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
1492 Returns a pointer to the string in the SV, or a stringified form of
1493 the SV if the SV does not contain a string.  The SV may cache the
1494 stringified version becoming C<SvPOK>.  Handles 'get' magic.  See also
1495 C<SvPVx> for a version which guarantees to evaluate sv only once.
1496
1497 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
1498 A version of C<SvPV> which guarantees to evaluate C<sv> only once.
1499 Only use this if C<sv> is an expression with side effects, otherwise use the
1500 more efficient C<SvPV>.
1501
1502 =for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
1503 Like C<SvPV> but doesn't process magic.
1504
1505 =for apidoc Am|char*|SvPV_nolen|SV* sv
1506 Returns a pointer to the string in the SV, or a stringified form of
1507 the SV if the SV does not contain a string.  The SV may cache the
1508 stringified form becoming C<SvPOK>.  Handles 'get' magic.
1509
1510 =for apidoc Am|char*|SvPV_nomg_nolen|SV* sv
1511 Like C<SvPV_nolen> but doesn't process magic.
1512
1513 =for apidoc Am|IV|SvIV|SV* sv
1514 Coerces the given SV to an integer and returns it.  See C<SvIVx> for a
1515 version which guarantees to evaluate sv only once.
1516
1517 =for apidoc Am|IV|SvIV_nomg|SV* sv
1518 Like C<SvIV> but doesn't process magic.
1519
1520 =for apidoc Am|IV|SvIVx|SV* sv
1521 Coerces the given SV to an integer and returns it.
1522 Guarantees to evaluate C<sv> only once.  Only use
1523 this if C<sv> is an expression with side effects,
1524 otherwise use the more efficient C<SvIV>.
1525
1526 =for apidoc Am|NV|SvNV|SV* sv
1527 Coerce the given SV to a double and return it.  See C<SvNVx> for a version
1528 which guarantees to evaluate sv only once.
1529
1530 =for apidoc Am|NV|SvNV_nomg|SV* sv
1531 Like C<SvNV> but doesn't process magic.
1532
1533 =for apidoc Am|NV|SvNVx|SV* sv
1534 Coerces the given SV to a double and returns it.
1535 Guarantees to evaluate C<sv> only once.  Only use
1536 this if C<sv> is an expression with side effects,
1537 otherwise use the more efficient C<SvNV>.
1538
1539 =for apidoc Am|UV|SvUV|SV* sv
1540 Coerces the given SV to an unsigned integer and returns it.  See C<SvUVx>
1541 for a version which guarantees to evaluate sv only once.
1542
1543 =for apidoc Am|UV|SvUV_nomg|SV* sv
1544 Like C<SvUV> but doesn't process magic.
1545
1546 =for apidoc Am|UV|SvUVx|SV* sv
1547 Coerces the given SV to an unsigned integer and
1548 returns it.  Guarantees to C<sv> only once.  Only
1549 use this if C<sv> is an expression with side effects,
1550 otherwise use the more efficient C<SvUV>.
1551
1552 =for apidoc Am|bool|SvTRUE|SV* sv
1553 Returns a boolean indicating whether Perl would evaluate the SV as true or
1554 false.  See SvOK() for a defined/undefined test.  Handles 'get' magic
1555 unless the scalar is already SvPOK, SvIOK or SvNOK (the public, not the
1556 private flags).
1557
1558 =for apidoc Am|bool|SvTRUE_nomg|SV* sv
1559 Returns a boolean indicating whether Perl would evaluate the SV as true or
1560 false.  See SvOK() for a defined/undefined test.  Does not handle 'get' magic.
1561
1562 =for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
1563 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1564
1565 =for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
1566 Like C<SvPV>, but converts sv to utf8 first if necessary.
1567
1568 =for apidoc Am|char*|SvPVutf8_nolen|SV* sv
1569 Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
1570
1571 =for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
1572 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1573
1574 =for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
1575 Like C<SvPV>, but converts sv to byte representation first if necessary.
1576
1577 =for apidoc Am|char*|SvPVbyte_nolen|SV* sv
1578 Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
1579
1580 =for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
1581 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1582 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
1583 otherwise.
1584
1585 =for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
1586 Like C<SvPV>, but converts sv to utf8 first if necessary.
1587 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
1588 otherwise.
1589
1590 =for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
1591 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1592 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
1593 otherwise.
1594
1595 =for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
1596 Like C<SvPV>, but converts sv to byte representation first if necessary.
1597 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
1598 otherwise.
1599
1600 =for apidoc Am|bool|SvIsCOW|SV* sv
1601 Returns a boolean indicating whether the SV is Copy-On-Write (either shared
1602 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1603 COW).
1604
1605 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1606 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1607 scalar.
1608
1609 =for apidoc Am|void|sv_catpvn_nomg|SV* sv|const char* ptr|STRLEN len
1610 Like C<sv_catpvn> but doesn't process magic.
1611
1612 =for apidoc Am|void|sv_catpv_nomg|SV* sv|const char* ptr
1613 Like C<sv_catpv> but doesn't process magic.
1614
1615 =for apidoc Am|void|sv_setsv_nomg|SV* dsv|SV* ssv
1616 Like C<sv_setsv> but doesn't process magic.
1617
1618 =for apidoc Am|void|sv_catsv_nomg|SV* dsv|SV* ssv
1619 Like C<sv_catsv> but doesn't process magic.
1620
1621 =for apidoc Amdb|STRLEN|sv_utf8_upgrade_nomg|NN SV *sv
1622
1623 Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
1624
1625 =cut
1626 */
1627
1628 /* Let us hope that bitmaps for UV and IV are the same */
1629 #define SvIV(sv) (SvIOK_nog(sv) ? SvIVX(sv) : sv_2iv(sv))
1630 #define SvUV(sv) (SvUOK_nog(sv) ? SvUVX(sv) : sv_2uv(sv))
1631 #define SvNV(sv) (SvNOK_nog(sv) ? SvNVX(sv) : sv_2nv(sv))
1632
1633 #define SvIV_nomg(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv_flags(sv, 0))
1634 #define SvUV_nomg(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv_flags(sv, 0))
1635 #define SvNV_nomg(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv_flags(sv, 0))
1636
1637 /* ----*/
1638
1639 #define SvPV(sv, lp)         SvPV_flags(sv, lp, SV_GMAGIC)
1640 #define SvPV_const(sv, lp)   SvPV_flags_const(sv, lp, SV_GMAGIC)
1641 #define SvPV_mutable(sv, lp) SvPV_flags_mutable(sv, lp, SV_GMAGIC)
1642
1643 #define SvPV_flags(sv, lp, flags) \
1644     (SvPOK_nog(sv) \
1645      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
1646 #define SvPV_flags_const(sv, lp, flags) \
1647     (SvPOK_nog(sv) \
1648      ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
1649      (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))
1650 #define SvPV_flags_const_nolen(sv, flags) \
1651     (SvPOK_nog(sv) \
1652      ? SvPVX_const(sv) : \
1653      (const char*) sv_2pv_flags(sv, 0, flags|SV_CONST_RETURN))
1654 #define SvPV_flags_mutable(sv, lp, flags) \
1655     (SvPOK_nog(sv) \
1656      ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
1657      sv_2pv_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1658
1659 #define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
1660 #define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
1661 #define SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
1662
1663 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
1664 #define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
1665
1666 #define SvPV_force_flags(sv, lp, flags) \
1667     (SvPOK_pure_nogthink(sv) \
1668      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
1669
1670 #define SvPV_force_flags_nolen(sv, flags) \
1671     (SvPOK_pure_nogthink(sv) \
1672      ? SvPVX(sv) : sv_pvn_force_flags(sv, 0, flags))
1673
1674 #define SvPV_force_flags_mutable(sv, lp, flags) \
1675     (SvPOK_pure_nogthink(sv) \
1676      ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) \
1677      : sv_pvn_force_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1678
1679 #define SvPV_nolen(sv) \
1680     (SvPOK_nog(sv) \
1681      ? SvPVX(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC))
1682
1683 #define SvPV_nomg_nolen(sv) \
1684     (SvPOK_nog(sv) \
1685      ? SvPVX(sv) : sv_2pv_flags(sv, 0, 0))
1686
1687 #define SvPV_nolen_const(sv) \
1688     (SvPOK_nog(sv) \
1689      ? SvPVX_const(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC|SV_CONST_RETURN))
1690
1691 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
1692 #define SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
1693 #define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1694
1695 /* ----*/
1696
1697 #define SvPVutf8(sv, lp) \
1698     (SvPOK_utf8_nog(sv) \
1699      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
1700
1701 #define SvPVutf8_force(sv, lp) \
1702     (SvPOK_utf8_pure_nogthink(sv) \
1703      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1704
1705 #define SvPVutf8_nolen(sv) \
1706     (SvPOK_utf8_nog(sv) \
1707      ? SvPVX(sv) : sv_2pvutf8(sv, 0))
1708
1709 /* ----*/
1710
1711 #define SvPVbyte(sv, lp) \
1712     (SvPOK_byte_nog(sv) \
1713      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
1714
1715 #define SvPVbyte_force(sv, lp) \
1716     (SvPOK_byte_pure_nogthink(sv) \
1717      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyten_force(sv, &lp))
1718
1719 #define SvPVbyte_nolen(sv) \
1720     (SvPOK_byte_nog(sv) \
1721      ? SvPVX(sv) : sv_2pvbyte(sv, 0))
1722
1723     
1724 /* define FOOx(): idempotent versions of FOO(). If possible, use a local
1725  * var to evaluate the arg once; failing that, use a global if possible;
1726  * failing that, call a function to do the work
1727  */
1728
1729 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
1730 #define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
1731 #define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
1732
1733 #define SvTRUE(sv)        ((sv) && (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv))))
1734 #define SvTRUE_nomg(sv)   ((sv) && (                                SvTRUE_common(sv, sv_2bool_nomg(sv))))
1735 #define SvTRUE_common(sv,fallback) (                    \
1736       !SvOK(sv)                                         \
1737         ? 0                                             \
1738     : (SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK))         \
1739         ? (   (SvPOK(sv) && SvPVXtrue(sv))              \
1740            || (SvIOK(sv) && SvIVX(sv) != 0)             \
1741            || (SvNOK(sv) && SvNVX(sv) != 0.0))          \
1742     : (fallback))
1743
1744 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1745
1746 #  define SvIVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvIV(_sv); })
1747 #  define SvUVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvUV(_sv); })
1748 #  define SvNVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvNV(_sv); })
1749 #  define SvPVx(sv, lp) ({SV *_sv = (sv); SvPV(_sv, lp); })
1750 #  define SvPVx_const(sv, lp) ({SV *_sv = (sv); SvPV_const(_sv, lp); })
1751 #  define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
1752 #  define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
1753 #  define SvPVutf8x(sv, lp) ({SV *_sv = (sv); SvPVutf8(_sv, lp); })
1754 #  define SvPVbytex(sv, lp) ({SV *_sv = (sv); SvPVbyte(_sv, lp); })
1755 #  define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
1756 #  define SvTRUEx(sv)      ({SV *_sv = (sv); SvTRUE(_sv); })
1757 #  define SvTRUEx_nomg(sv) ({SV *_sv = (sv); SvTRUE_nomg(_sv); })
1758 #  define SvPVXtrue(sv)                                         \
1759     ({XPV *nxpv;                                                \
1760      (nxpv = (XPV*)SvANY(sv))                                   \
1761       && (nxpv->xpv_cur > 1                                     \
1762           || (nxpv->xpv_cur && *(sv)->sv_u.svu_pv != '0'));})
1763
1764 #else /* __GNUC__ */
1765
1766 /* These inlined macros use globals, which will require a thread
1767  * declaration in user code, so we avoid them under threads */
1768
1769 #  define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1770 #  define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1771 #  define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1772 #  define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
1773 #  define SvPVx_const(sv, lp) ((PL_Sv = (sv)), SvPV_const(PL_Sv, lp))
1774 #  define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
1775 #  define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
1776 #  define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1777 #  define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1778 #  define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
1779 #  define SvTRUEx(sv)      ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1780 #  define SvTRUEx_nomg(sv) ((PL_Sv = (sv)), SvTRUE_nomg(PL_Sv))
1781 #  define SvPVXtrue(sv)                                         \
1782     ((PL_Xpv = (XPV*)SvANY(PL_Sv = (sv)))                       \
1783      && (PL_Xpv->xpv_cur > 1                                    \
1784          || (PL_Xpv->xpv_cur && *PL_Sv->sv_u.svu_pv != '0')))
1785 #endif /* __GNU__ */
1786
1787 #define SvIsCOW(sv)     ((SvFLAGS(sv) & (SVf_FAKE | SVf_READONLY)) == \
1788                            (SVf_FAKE | SVf_READONLY) && !isGV_with_GP(sv) \
1789                            && SvTYPE(sv) != SVt_REGEXP)
1790 #define SvIsCOW_shared_hash(sv) (SvIsCOW(sv) && SvLEN(sv) == 0)
1791
1792 #define SvSHARED_HEK_FROM_PV(pvx) \
1793         ((struct hek*)(pvx - STRUCT_OFFSET(struct hek, hek_key)))
1794 #define SvSHARED_HASH(sv) (0 + SvSHARED_HEK_FROM_PV(SvPVX_const(sv))->hek_hash)
1795
1796 /* flag values for sv_*_flags functions */
1797 #define SV_IMMEDIATE_UNREF      1
1798 #define SV_GMAGIC               2
1799 #define SV_COW_DROP_PV          4
1800 #define SV_UTF8_NO_ENCODING     8
1801 #define SV_NOSTEAL              16
1802 #define SV_CONST_RETURN         32
1803 #define SV_MUTABLE_RETURN       64
1804 #define SV_SMAGIC               128
1805 #define SV_HAS_TRAILING_NUL     256
1806 #define SV_COW_SHARED_HASH_KEYS 512
1807 /* This one is only enabled for PERL_OLD_COPY_ON_WRITE */
1808 #define SV_COW_OTHER_PVS        1024
1809 /* Make sv_2pv_flags return NULL if something is undefined.  */
1810 #define SV_UNDEF_RETURNS_NULL   2048
1811 /* Tell sv_utf8_upgrade() to not check to see if an upgrade is really needed.
1812  * This is used when the caller has already determined it is, and avoids
1813  * redundant work */
1814 #define SV_FORCE_UTF8_UPGRADE   4096
1815 /* if (after resolving magic etc), the SV is found to be overloaded,
1816  * don't call the overload magic, just return as-is */
1817 #define SV_SKIP_OVERLOAD        8192
1818 /* It is not yet clear whether we want this as an API, or what the
1819  * constants should be named. */
1820 #ifdef PERL_CORE
1821 # define SV_CATBYTES            16384
1822 # define SV_CATUTF8             32768
1823 #endif
1824
1825 /* The core is safe for this COW optimisation. XS code on CPAN may not be.
1826    So only default to doing the COW setup if we're in the core.
1827  */
1828 #ifdef PERL_CORE
1829 #  ifndef SV_DO_COW_SVSETSV
1830 #    define SV_DO_COW_SVSETSV   SV_COW_SHARED_HASH_KEYS|SV_COW_OTHER_PVS
1831 #  endif
1832 #endif
1833
1834 #ifndef SV_DO_COW_SVSETSV
1835 #  define SV_DO_COW_SVSETSV     0
1836 #endif
1837
1838
1839 #define sv_unref(sv)            sv_unref_flags(sv, 0)
1840 #define sv_force_normal(sv)     sv_force_normal_flags(sv, 0)
1841 #define sv_usepvn(sv, p, l)     sv_usepvn_flags(sv, p, l, 0)
1842 #define sv_usepvn_mg(sv, p, l)  sv_usepvn_flags(sv, p, l, SV_SMAGIC)
1843
1844 /* We are about to replace the SV's current value. So if it's copy on write
1845    we need to normalise it. Use the SV_COW_DROP_PV flag hint to say that
1846    the value is about to get thrown away, so drop the PV rather than go to
1847    the effort of making a read-write copy only for it to get immediately
1848    discarded.  */
1849
1850 #define SV_CHECK_THINKFIRST_COW_DROP(sv) if (SvTHINKFIRST(sv)) \
1851                                     sv_force_normal_flags(sv, SV_COW_DROP_PV)
1852
1853 #ifdef PERL_OLD_COPY_ON_WRITE
1854 #define SvRELEASE_IVX(sv)   \
1855     ((SvIsCOW(sv) ? sv_force_normal_flags(sv, 0) : (void) 0), 0)
1856 #  define SvIsCOW_normal(sv)    (SvIsCOW(sv) && SvLEN(sv))
1857 #  define SvRELEASE_IVX_(sv)    SvRELEASE_IVX(sv),
1858 #else
1859 #  define SvRELEASE_IVX(sv)   0
1860 /* This little game brought to you by the need to shut this warning up:
1861 mg.c: In function 'Perl_magic_get':
1862 mg.c:1024: warning: left-hand operand of comma expression has no effect
1863 */
1864 #  define SvRELEASE_IVX_(sv)  /**/
1865 #endif /* PERL_OLD_COPY_ON_WRITE */
1866
1867 #define CAN_COW_MASK    (SVs_OBJECT|SVs_GMG|SVs_SMG|SVs_RMG|SVf_IOK|SVf_NOK| \
1868                          SVf_POK|SVf_ROK|SVp_IOK|SVp_NOK|SVp_POK|SVf_FAKE| \
1869                          SVf_OOK|SVf_BREAK|SVf_READONLY)
1870 #define CAN_COW_FLAGS   (SVp_POK|SVf_POK)
1871
1872 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \
1873                                     sv_force_normal_flags(sv, 0)
1874
1875
1876 /* all these 'functions' are now just macros */
1877
1878 #define sv_pv(sv) SvPV_nolen(sv)
1879 #define sv_pvutf8(sv) SvPVutf8_nolen(sv)
1880 #define sv_pvbyte(sv) SvPVbyte_nolen(sv)
1881
1882 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
1883 #define sv_utf8_upgrade_flags(sv, flags) sv_utf8_upgrade_flags_grow(sv, flags, 0)
1884 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
1885 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
1886 #define sv_catpv_nomg(dsv, sstr) sv_catpv_flags(dsv, sstr, 0)
1887 #define sv_setsv(dsv, ssv) \
1888         sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)
1889 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_DO_COW_SVSETSV)
1890 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
1891 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
1892 #define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
1893 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
1894 #define sv_catpvn_mg(sv, sstr, slen) sv_catpvn_flags(sv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
1895 #define sv_copypv(dsv, ssv) sv_copypv_flags(dsv, ssv, SV_GMAGIC)
1896 #define sv_copypv_nomg(dsv, ssv) sv_copypv_flags(dsv, ssv, 0)
1897 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
1898 #define sv_2pv_nolen(sv) sv_2pv(sv, 0)
1899 #define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
1900 #define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
1901 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
1902 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
1903 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
1904 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
1905 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
1906 #define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
1907 #define sv_eq(sv1, sv2) sv_eq_flags(sv1, sv2, SV_GMAGIC)
1908 #define sv_cmp(sv1, sv2) sv_cmp_flags(sv1, sv2, SV_GMAGIC)
1909 #define sv_cmp_locale(sv1, sv2) sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC)
1910 #define sv_collxfrm(sv, nxp) sv_cmp_flags(sv, nxp, SV_GMAGIC)
1911 #define sv_2bool(sv) sv_2bool_flags(sv, SV_GMAGIC)
1912 #define sv_2bool_nomg(sv) sv_2bool_flags(sv, 0)
1913 #define sv_insert(bigstr, offset, len, little, littlelen)               \
1914         Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little),  \
1915                              (littlelen), SV_GMAGIC)
1916
1917 /* Should be named SvCatPVN_utf8_upgrade? */
1918 #define sv_catpvn_nomg_utf8_upgrade(dsv, sstr, slen, nsv)       \
1919         STMT_START {                                    \
1920             if (!(nsv))                                 \
1921                 nsv = newSVpvn_flags(sstr, slen, SVs_TEMP);     \
1922             else                                        \
1923                 sv_setpvn(nsv, sstr, slen);             \
1924             SvUTF8_off(nsv);                            \
1925             sv_utf8_upgrade(nsv);                       \
1926             sv_catsv_nomg(dsv, nsv);                    \
1927         } STMT_END
1928
1929 /*
1930 =for apidoc Am|SV*|newRV_inc|SV* sv
1931
1932 Creates an RV wrapper for an SV.  The reference count for the original SV is
1933 incremented.
1934
1935 =cut
1936 */
1937
1938 #define newRV_inc(sv)   newRV(sv)
1939
1940 /* the following macros update any magic values this sv is associated with */
1941
1942 /*
1943 =head1 Magical Functions
1944
1945 =for apidoc Am|void|SvGETMAGIC|SV* sv
1946 Invokes C<mg_get> on an SV if it has 'get' magic.  For example, this
1947 will call C<FETCH> on a tied variable.  This macro evaluates its
1948 argument more than once.
1949
1950 =for apidoc Am|void|SvSETMAGIC|SV* sv
1951 Invokes C<mg_set> on an SV if it has 'set' magic.  This is necessary
1952 after modifying a scalar, in case it is a magical variable like C<$|>
1953 or a tied variable (it calls C<STORE>).  This macro evaluates its
1954 argument more than once.
1955
1956 =for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1957 Calls C<sv_setsv> if dsv is not the same as ssv.  May evaluate arguments
1958 more than once.
1959
1960 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1961 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1962 ssv.  May evaluate arguments more than once.
1963
1964 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1965 Like C<SvSetSV>, but does any set magic required afterwards.
1966
1967 =for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
1968 Like C<SvSetSV_nosteal>, but does any set magic required afterwards.
1969
1970 =for apidoc Am|void|SvSHARE|SV* sv
1971 Arranges for sv to be shared between threads if a suitable module
1972 has been loaded.
1973
1974 =for apidoc Am|void|SvLOCK|SV* sv
1975 Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
1976 has been loaded.
1977
1978 =for apidoc Am|void|SvUNLOCK|SV* sv
1979 Releases a mutual exclusion lock on sv if a suitable module
1980 has been loaded.
1981
1982 =head1 SV Manipulation Functions
1983
1984 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
1985 Expands the character buffer in the SV so that it has room for the
1986 indicated number of bytes (remember to reserve space for an extra trailing
1987 NUL character).  Calls C<sv_grow> to perform the expansion if necessary.
1988 Returns a pointer to the character buffer.
1989
1990 =cut
1991 */
1992
1993 #define SvSHARE(sv) PL_sharehook(aTHX_ sv)
1994 #define SvLOCK(sv) PL_lockhook(aTHX_ sv)
1995 #define SvUNLOCK(sv) PL_unlockhook(aTHX_ sv)
1996 #define SvDESTROYABLE(sv) PL_destroyhook(aTHX_ sv)
1997
1998 #define SvGETMAGIC(x) ((void)(SvGMAGICAL(x) && mg_get(x)))
1999 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
2000
2001 #define SvSetSV_and(dst,src,finally) \
2002         STMT_START {                                    \
2003             if ((dst) != (src)) {                       \
2004                 sv_setsv(dst, src);                     \
2005                 finally;                                \
2006             }                                           \
2007         } STMT_END
2008 #define SvSetSV_nosteal_and(dst,src,finally) \
2009         STMT_START {                                    \
2010             if ((dst) != (src)) {                       \
2011                 sv_setsv_flags(dst, src, SV_GMAGIC | SV_NOSTEAL | SV_DO_COW_SVSETSV);   \
2012                 finally;                                \
2013             }                                           \
2014         } STMT_END
2015
2016 #define SvSetSV(dst,src) \
2017                 SvSetSV_and(dst,src,/*nothing*/;)
2018 #define SvSetSV_nosteal(dst,src) \
2019                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
2020
2021 #define SvSetMagicSV(dst,src) \
2022                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
2023 #define SvSetMagicSV_nosteal(dst,src) \
2024                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
2025
2026
2027 #if !defined(SKIP_DEBUGGING)
2028 #define SvPEEK(sv) sv_peek(sv)
2029 #else
2030 #define SvPEEK(sv) ""
2031 #endif
2032
2033 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_placeholder)
2034
2035 /*
2036 =for apidoc Am|SV *|boolSV|bool b
2037
2038 Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
2039
2040 See also C<PL_sv_yes> and C<PL_sv_no>.
2041
2042 =cut
2043 */
2044
2045 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
2046
2047 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
2048 /* If I give every macro argument a different name, then there won't be bugs
2049    where nested macros get confused. Been there, done that.  */
2050 #define isGV_with_GP(pwadak) \
2051         (((SvFLAGS(pwadak) & (SVp_POK|SVpgv_GP)) == SVpgv_GP)   \
2052         && (SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
2053 #define isGV_with_GP_on(sv)     STMT_START {                           \
2054         assert (SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV); \
2055         assert (!SvPOKp(sv));                                          \
2056         assert (!SvIOKp(sv));                                          \
2057         (SvFLAGS(sv) |= SVpgv_GP);                                     \
2058     } STMT_END
2059 #define isGV_with_GP_off(sv)    STMT_START {                           \
2060         assert (SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV); \
2061         assert (!SvPOKp(sv));                                          \
2062         assert (!SvIOKp(sv));                                          \
2063         (SvFLAGS(sv) &= ~SVpgv_GP);                                    \
2064     } STMT_END
2065
2066
2067 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2068 #define SvGROW_mutable(sv,len) \
2069     (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
2070 #define Sv_Grow sv_grow
2071
2072 #define CLONEf_COPY_STACKS 1
2073 #define CLONEf_KEEP_PTR_TABLE 2
2074 #define CLONEf_CLONE_HOST 4
2075 #define CLONEf_JOIN_IN 8
2076
2077 struct clone_params {
2078   AV* stashes;
2079   UV  flags;
2080   PerlInterpreter *proto_perl;
2081   PerlInterpreter *new_perl;
2082   AV *unreferenced;
2083 };
2084
2085 /*
2086 =for apidoc Am|SV*|newSVpvn_utf8|NULLOK const char* s|STRLEN len|U32 utf8
2087
2088 Creates a new SV and copies a string into it.  If utf8 is true, calls
2089 C<SvUTF8_on> on the new SV.  Implemented as a wrapper around C<newSVpvn_flags>.
2090
2091 =cut
2092 */
2093
2094 #define newSVpvn_utf8(s, len, u) newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
2095
2096 /*
2097 =for apidoc Am|void|SvOOK_offset|NN SV*sv|STRLEN len
2098
2099 Reads into I<len> the offset from SvPVX back to the true start of the
2100 allocated buffer, which will be non-zero if C<sv_chop> has been used to
2101 efficiently remove characters from start of the buffer.  Implemented as a
2102 macro, which takes the address of I<len>, which must be of type C<STRLEN>.
2103 Evaluates I<sv> more than once.  Sets I<len> to 0 if C<SvOOK(sv)> is false.
2104
2105 =cut
2106 */
2107
2108 #ifdef DEBUGGING
2109 /* Does the bot know something I don't?
2110 10:28 <@Nicholas> metabatman
2111 10:28 <+meta> Nicholas: crash
2112 */
2113 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2114         assert(sizeof(offset) == sizeof(STRLEN));                       \
2115         if (SvOOK(sv)) {                                                \
2116             const U8 *_crash = (U8*)SvPVX_const(sv);                    \
2117             (offset) = *--_crash;                                       \
2118             if (!(offset)) {                                            \
2119                 _crash -= sizeof(STRLEN);                               \
2120                 Copy(_crash, (U8 *)&(offset), sizeof(STRLEN), U8);      \
2121             }                                                           \
2122             {                                                           \
2123                 /* Validate the preceding buffer's sentinels to         \
2124                    verify that no-one is using it.  */                  \
2125                 const U8 *const _bonk = (U8*)SvPVX_const(sv) - (offset);\
2126                 while (_crash > _bonk) {                                \
2127                     --_crash;                                           \
2128                     assert (*_crash == (U8)PTR2UV(_crash));             \
2129                 }                                                       \
2130             }                                                           \
2131         } else {                                                        \
2132             (offset) = 0;                                               \
2133         }                                                               \
2134     } STMT_END
2135 #else
2136     /* This is the same code, but avoids using any temporary variables:  */
2137 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2138         assert(sizeof(offset) == sizeof(STRLEN));                       \
2139         if (SvOOK(sv)) {                                                \
2140             (offset) = ((U8*)SvPVX_const(sv))[-1];                      \
2141             if (!(offset)) {                                            \
2142                 Copy(SvPVX_const(sv) - 1 - sizeof(STRLEN),              \
2143                      (U8*)&(offset), sizeof(STRLEN), U8);               \
2144             }                                                           \
2145         } else {                                                        \
2146             (offset) = 0;                                               \
2147         }                                                               \
2148     } STMT_END
2149 #endif
2150
2151 #define newIO() MUTABLE_IO(newSV_type(SVt_PVIO))
2152
2153 /*
2154  * Local variables:
2155  * c-indentation-style: bsd
2156  * c-basic-offset: 4
2157  * indent-tabs-mode: nil
2158  * End:
2159  *
2160  * ex: set ts=8 sts=4 sw=4 et:
2161  */