Incremental merge of mainline
[p5sagit/p5-mst-13.2.git] / sv.h
1 /*    sv.h
2  *
3  *    Copyright (c) 1991-1999, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 #ifdef sv_flags
11 #undef sv_flags         /* Convex has this in <signal.h> for sigvec() */
12 #endif
13
14 typedef enum {
15         SVt_NULL,       /* 0 */
16         SVt_IV,         /* 1 */
17         SVt_NV,         /* 2 */
18         SVt_RV,         /* 3 */
19         SVt_PV,         /* 4 */
20         SVt_PVIV,       /* 5 */
21         SVt_PVNV,       /* 6 */
22         SVt_PVMG,       /* 7 */
23         SVt_PVBM,       /* 8 */
24         SVt_PVLV,       /* 9 */
25         SVt_PVAV,       /* 10 */
26         SVt_PVHV,       /* 11 */
27         SVt_PVCV,       /* 12 */
28         SVt_PVGV,       /* 13 */
29         SVt_PVFM,       /* 14 */
30         SVt_PVIO        /* 15 */
31 } svtype;
32
33 /* Using C's structural equivalence to help emulate C++ inheritance here... */
34
35 struct sv {
36     void*       sv_any;         /* pointer to something */
37     U32         sv_refcnt;      /* how many references to us */
38     U32         sv_flags;       /* what we are */
39 };
40
41 struct gv {
42     XPVGV*      sv_any;         /* pointer to something */
43     U32         sv_refcnt;      /* how many references to us */
44     U32         sv_flags;       /* what we are */
45 };
46
47 struct cv {
48     XPVCV*      sv_any;         /* pointer to something */
49     U32         sv_refcnt;      /* how many references to us */
50     U32         sv_flags;       /* what we are */
51 };
52
53 struct av {
54     XPVAV*      sv_any;         /* pointer to something */
55     U32         sv_refcnt;      /* how many references to us */
56     U32         sv_flags;       /* what we are */
57 };
58
59 struct hv {
60     XPVHV*      sv_any;         /* pointer to something */
61     U32         sv_refcnt;      /* how many references to us */
62     U32         sv_flags;       /* what we are */
63 };
64
65 struct io {
66     XPVIO*      sv_any;         /* pointer to something */
67     U32         sv_refcnt;      /* how many references to us */
68     U32         sv_flags;       /* what we are */
69 };
70
71 #define SvANY(sv)       (sv)->sv_any
72 #define SvFLAGS(sv)     (sv)->sv_flags
73 #define SvREFCNT(sv)    (sv)->sv_refcnt
74
75 #ifdef USE_THREADS
76
77 #  ifdef EMULATE_ATOMIC_REFCOUNTS
78 #    define ATOMIC_INC(count) STMT_START {      \
79         MUTEX_LOCK(&PL_svref_mutex);            \
80         ++count;                                \
81         MUTEX_UNLOCK(&PL_svref_mutex);          \
82      } STMT_END
83 #    define ATOMIC_DEC_AND_TEST(res,count) STMT_START { \
84         MUTEX_LOCK(&PL_svref_mutex);                    \
85         res = (--count == 0);                           \
86         MUTEX_UNLOCK(&PL_svref_mutex);                  \
87      } STMT_END
88 #  else
89 #    define ATOMIC_INC(count) atomic_inc(&count)
90 #    define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
91 #  endif /* EMULATE_ATOMIC_REFCOUNTS */
92 #else
93 #  define ATOMIC_INC(count) (++count)
94 #  define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
95 #endif /* USE_THREADS */
96
97 #ifdef __GNUC__
98 #  define SvREFCNT_inc(sv)              \
99     ({                                  \
100         SV *nsv = (SV*)(sv);            \
101         if (nsv)                        \
102              ATOMIC_INC(SvREFCNT(nsv)); \
103         nsv;                            \
104     })
105 #else
106 #  if defined(CRIPPLED_CC) || defined(USE_THREADS)
107 #    define SvREFCNT_inc(sv) sv_newref((SV*)sv)
108 #  else
109 #    define SvREFCNT_inc(sv)    \
110         ((PL_Sv=(SV*)(sv)), (PL_Sv && ATOMIC_INC(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
111 #  endif
112 #endif
113
114 #define SvREFCNT_dec(sv)        sv_free((SV*)sv)
115
116 #define SVTYPEMASK      0xff
117 #define SvTYPE(sv)      ((sv)->sv_flags & SVTYPEMASK)
118
119 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
120
121 #define SVs_PADBUSY     0x00000100      /* reserved for tmp or my already */
122 #define SVs_PADTMP      0x00000200      /* in use as tmp */
123 #define SVs_PADMY       0x00000400      /* in use a "my" variable */
124 #define SVs_TEMP        0x00000800      /* string is stealable? */
125 #define SVs_OBJECT      0x00001000      /* is "blessed" */
126 #define SVs_GMG         0x00002000      /* has magical get method */
127 #define SVs_SMG         0x00004000      /* has magical set method */
128 #define SVs_RMG         0x00008000      /* has random magical methods */
129
130 #define SVf_IOK         0x00010000      /* has valid public integer value */
131 #define SVf_NOK         0x00020000      /* has valid public numeric value */
132 #define SVf_POK         0x00040000      /* has valid public pointer value */
133 #define SVf_ROK         0x00080000      /* has a valid reference pointer */
134
135 #define SVf_FAKE        0x00100000      /* glob or lexical is just a copy */
136 #define SVf_OOK         0x00200000      /* has valid offset value */
137 #define SVf_BREAK       0x00400000      /* refcnt is artificially low */
138 #define SVf_READONLY    0x00800000      /* may not be modified */
139
140
141 #define SVp_IOK         0x01000000      /* has valid non-public integer value */
142 #define SVp_NOK         0x02000000      /* has valid non-public numeric value */
143 #define SVp_POK         0x04000000      /* has valid non-public pointer value */
144 #define SVp_SCREAM      0x08000000      /* has been studied? */
145
146 #define SVf_UTF8        0x20000000      /* SvPVX is UTF-8 encoded */
147
148 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE|SVf_UTF8)
149
150 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
151                          SVp_IOK|SVp_NOK|SVp_POK)
152
153 #define SVf_AMAGIC      0x10000000      /* has magical overloaded methods */
154
155 #define PRIVSHIFT 8
156
157 /* Some private flags. */
158
159 #define SVpad_OUR       0x80000000      /* pad name is "our" instead of "my" */
160
161 #define SVf_IVisUV      0x80000000      /* use XPVUV instead of XPVIV */
162
163 #define SVpfm_COMPILED  0x80000000      /* FORMLINE is compiled */
164
165 #define SVpbm_VALID     0x80000000
166 #define SVpbm_TAIL      0x40000000
167
168 #define SVrepl_EVAL     0x40000000      /* Replacement part of s///e */
169
170 #define SVphv_SHAREKEYS 0x20000000      /* keys live on shared string table */
171 #define SVphv_LAZYDEL   0x40000000      /* entry in xhv_eiter must be deleted */
172
173 #define SVprv_WEAKREF   0x80000000      /* Weak reference */
174
175 struct xrv {
176     SV *        xrv_rv;         /* pointer to another SV */
177 };
178
179 struct xpv {
180     char *      xpv_pv;         /* pointer to malloced string */
181     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
182     STRLEN      xpv_len;        /* allocated size */
183 };
184
185 struct xpviv {
186     char *      xpv_pv;         /* pointer to malloced string */
187     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
188     STRLEN      xpv_len;        /* allocated size */
189     IV          xiv_iv;         /* integer value or pv offset */
190 };
191
192 struct xpvuv {
193     char *      xpv_pv;         /* pointer to malloced string */
194     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
195     STRLEN      xpv_len;        /* allocated size */
196     UV          xuv_uv;         /* unsigned value or pv offset */
197 };
198
199 struct xpvnv {
200     char *      xpv_pv;         /* pointer to malloced string */
201     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
202     STRLEN      xpv_len;        /* allocated size */
203     IV          xiv_iv;         /* integer value or pv offset */
204     NV          xnv_nv;         /* numeric value, if any */
205 };
206
207 /* These structure must match the beginning of struct xpvhv in hv.h. */
208 struct xpvmg {
209     char *      xpv_pv;         /* pointer to malloced string */
210     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
211     STRLEN      xpv_len;        /* allocated size */
212     IV          xiv_iv;         /* integer value or pv offset */
213     NV          xnv_nv;         /* numeric value, if any */
214     MAGIC*      xmg_magic;      /* linked list of magicalness */
215     HV*         xmg_stash;      /* class package */
216 };
217
218 struct xpvlv {
219     char *      xpv_pv;         /* pointer to malloced string */
220     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
221     STRLEN      xpv_len;        /* allocated size */
222     IV          xiv_iv;         /* integer value or pv offset */
223     NV          xnv_nv;         /* numeric value, if any */
224     MAGIC*      xmg_magic;      /* linked list of magicalness */
225     HV*         xmg_stash;      /* class package */
226
227     STRLEN      xlv_targoff;
228     STRLEN      xlv_targlen;
229     SV*         xlv_targ;
230     char        xlv_type;
231 };
232
233 struct xpvgv {
234     char *      xpv_pv;         /* pointer to malloced string */
235     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
236     STRLEN      xpv_len;        /* allocated size */
237     IV          xiv_iv;         /* integer value or pv offset */
238     NV          xnv_nv;         /* numeric value, if any */
239     MAGIC*      xmg_magic;      /* linked list of magicalness */
240     HV*         xmg_stash;      /* class package */
241
242     GP*         xgv_gp;
243     char*       xgv_name;
244     STRLEN      xgv_namelen;
245     HV*         xgv_stash;
246     U8          xgv_flags;
247 };
248
249 struct xpvbm {
250     char *      xpv_pv;         /* pointer to malloced string */
251     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
252     STRLEN      xpv_len;        /* allocated size */
253     IV          xiv_iv;         /* integer value or pv offset */
254     NV          xnv_nv;         /* numeric value, if any */
255     MAGIC*      xmg_magic;      /* linked list of magicalness */
256     HV*         xmg_stash;      /* class package */
257
258     I32         xbm_useful;     /* is this constant pattern being useful? */
259     U16         xbm_previous;   /* how many characters in string before rare? */
260     U8          xbm_rare;       /* rarest character in string */
261 };
262
263 /* This structure much match XPVCV */
264
265 typedef U16 cv_flags_t;
266
267 struct xpvfm {
268     char *      xpv_pv;         /* pointer to malloced string */
269     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
270     STRLEN      xpv_len;        /* allocated size */
271     IV          xiv_iv;         /* integer value or pv offset */
272     NV          xnv_nv;         /* numeric value, if any */
273     MAGIC*      xmg_magic;      /* linked list of magicalness */
274     HV*         xmg_stash;      /* class package */
275
276     HV *        xcv_stash;
277     OP *        xcv_start;
278     OP *        xcv_root;
279     void      (*xcv_xsub)(pTHXo_ CV*);
280     ANY         xcv_xsubany;
281     GV *        xcv_gv;
282     GV *        xcv_filegv;
283     long        xcv_depth;              /* >= 2 indicates recursive call */
284     AV *        xcv_padlist;
285     CV *        xcv_outside;
286 #ifdef USE_THREADS
287     perl_mutex *xcv_mutexp;     /* protects xcv_owner */
288     struct perl_thread *xcv_owner;      /* current owner thread */
289 #endif /* USE_THREADS */
290     cv_flags_t  xcv_flags;
291
292     I32         xfm_lines;
293 };
294
295 struct xpvio {
296     char *      xpv_pv;         /* pointer to malloced string */
297     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
298     STRLEN      xpv_len;        /* allocated size */
299     IV          xiv_iv;         /* integer value or pv offset */
300     NV          xnv_nv;         /* numeric value, if any */
301     MAGIC*      xmg_magic;      /* linked list of magicalness */
302     HV*         xmg_stash;      /* class package */
303
304     PerlIO *    xio_ifp;        /* ifp and ofp are normally the same */
305     PerlIO *    xio_ofp;        /* but sockets need separate streams */
306     DIR *       xio_dirp;       /* for opendir, readdir, etc */
307     long        xio_lines;      /* $. */
308     long        xio_page;       /* $% */
309     long        xio_page_len;   /* $= */
310     long        xio_lines_left; /* $- */
311     char *      xio_top_name;   /* $^ */
312     GV *        xio_top_gv;     /* $^ */
313     char *      xio_fmt_name;   /* $~ */
314     GV *        xio_fmt_gv;     /* $~ */
315     char *      xio_bottom_name;/* $^B */
316     GV *        xio_bottom_gv;  /* $^B */
317     short       xio_subprocess; /* -| or |- */
318     char        xio_type;
319     char        xio_flags;
320 };
321
322 #define IOf_ARGV 1      /* this fp iterates over ARGV */
323 #define IOf_START 2     /* check for null ARGV and substitute '-' */
324 #define IOf_FLUSH 4     /* this fp wants a flush after write op */
325 #define IOf_DIDTOP 8    /* just did top of form */
326 #define IOf_UNTAINT 16  /* consider this fp (and its data) "safe" */
327 #define IOf_NOLINE  32  /* slurped a pseudo-line from empty file */
328
329 /* The following macros define implementation-independent predicates on SVs. */
330
331 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
332 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
333 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
334                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
335
336 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
337 #define SvOK_off(sv)            (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
338                                                   SVf_IVisUV),          \
339                                                         SvOOK_off(sv))
340 #define SvOK_off_exc_UV(sv)     (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC),   \
341                                                         SvOOK_off(sv))
342
343 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
344 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
345 #define SvIOKp_on(sv)           (SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
346 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
347 #define SvNOKp_on(sv)           (SvFLAGS(sv) |= SVp_NOK)
348 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
349 #define SvPOKp_on(sv)           (SvFLAGS(sv) |= SVp_POK)
350
351 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
352 #define SvIOK_on(sv)            (SvOOK_off(sv), \
353                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
354 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
355 #define SvIOK_only(sv)          (SvOK_off(sv), \
356                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
357 #define SvIOK_only_UV(sv)       (SvOK_off_exc_UV(sv), \
358                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
359  
360 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
361                                  == (SVf_IOK|SVf_IVisUV))
362 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
363                                  == SVf_IOK)
364
365 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
366 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
367 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
368
369 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
370 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
371 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
372 #define SvNOK_only(sv)          (SvOK_off(sv), \
373                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
374
375 #define SvUTF8(sv)              (SvFLAGS(sv) & SVf_ISUTF8)          
376 #define SvUTF8_on(sv)           (SvFLAGS(sv) |= (SVf_ISUTF8))
377 #define SvUTF8_off(sv)          (SvFLAGS(sv) &= ~(SVf_ISUTF8))
378
379 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
380 #define SvPOK_on(sv)            (SvFLAGS(sv) |= (SVf_POK|SVp_POK))
381 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
382 #define SvPOK_only(sv)          (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|SVf_IVisUV),        \
383                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
384                                     
385 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
386 #define SvOOK_on(sv)            (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
387 #define SvOOK_off(sv)           (SvOOK(sv) && sv_backoff(sv))
388
389 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
390 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
391 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
392
393 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
394 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
395 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
396
397 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
398 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
399 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
400
401 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
402 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
403 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
404
405 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
406 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
407 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
408
409 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
410 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
411 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
412
413 #define SvAMAGIC(sv)            (SvFLAGS(sv) & SVf_AMAGIC)
414 #define SvAMAGIC_on(sv)         (SvFLAGS(sv) |= SVf_AMAGIC)
415 #define SvAMAGIC_off(sv)        (SvFLAGS(sv) &= ~SVf_AMAGIC)
416
417 /*
418 #define Gv_AMG(stash) \
419         (HV_AMAGICmb(stash) && \
420          ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
421 */
422 #define Gv_AMG(stash)           (PL_amagic_generation && Gv_AMupdate(stash))
423
424 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
425                                   == (SVf_ROK|SVprv_WEAKREF))
426 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
427 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
428
429 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
430
431 #define SvPADBUSY(sv)           (SvFLAGS(sv) & SVs_PADBUSY)
432
433 #define SvPADTMP(sv)            (SvFLAGS(sv) & SVs_PADTMP)
434 #define SvPADTMP_on(sv)         (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
435 #define SvPADTMP_off(sv)        (SvFLAGS(sv) &= ~SVs_PADTMP)
436
437 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
438 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
439
440 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
441 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
442 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
443
444 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
445 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
446 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
447
448 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
449 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
450 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
451
452 #define SvSCREAM(sv)            (SvFLAGS(sv) & SVp_SCREAM)
453 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
454 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
455
456 #define SvCOMPILED(sv)          (SvFLAGS(sv) & SVpfm_COMPILED)
457 #define SvCOMPILED_on(sv)       (SvFLAGS(sv) |= SVpfm_COMPILED)
458 #define SvCOMPILED_off(sv)      (SvFLAGS(sv) &= ~SVpfm_COMPILED)
459
460 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
461 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
462 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
463
464 #define SvTAIL(sv)              (SvFLAGS(sv) & SVpbm_TAIL)
465 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
466 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
467
468 #define SvVALID(sv)             (SvFLAGS(sv) & SVpbm_VALID)
469 #define SvVALID_on(sv)          (SvFLAGS(sv) |= SVpbm_VALID)
470 #define SvVALID_off(sv)         (SvFLAGS(sv) &= ~SVpbm_VALID)
471
472 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
473 #define SvRVx(sv) SvRV(sv)
474
475 #define SvIVX(sv) ((XPVIV*)  SvANY(sv))->xiv_iv
476 #define SvIVXx(sv) SvIVX(sv)
477 #define SvUVX(sv) ((XPVUV*)  SvANY(sv))->xuv_uv
478 #define SvUVXx(sv) SvUVX(sv)
479 #define SvNVX(sv)  ((XPVNV*)SvANY(sv))->xnv_nv
480 #define SvNVXx(sv) SvNVX(sv)
481 #define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
482 #define SvPVXx(sv) SvPVX(sv)
483 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
484 #define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
485 #define SvLENx(sv) SvLEN(sv)
486 #define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
487 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
488 #define SvMAGIC(sv)     ((XPVMG*)  SvANY(sv))->xmg_magic
489 #define SvSTASH(sv)     ((XPVMG*)  SvANY(sv))->xmg_stash
490
491 #define SvIV_set(sv, val) \
492         STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
493                 (((XPVIV*)  SvANY(sv))->xiv_iv = val); } STMT_END
494 #define SvNV_set(sv, val) \
495         STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
496                 (((XPVNV*)  SvANY(sv))->xnv_nv = val); } STMT_END
497 #define SvPV_set(sv, val) \
498         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
499                 (((XPV*)  SvANY(sv))->xpv_pv = val); } STMT_END
500 #define SvCUR_set(sv, val) \
501         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
502                 (((XPV*)  SvANY(sv))->xpv_cur = val); } STMT_END
503 #define SvLEN_set(sv, val) \
504         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
505                 (((XPV*)  SvANY(sv))->xpv_len = val); } STMT_END
506 #define SvEND_set(sv, val) \
507         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
508                 (((XPV*)  SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
509
510 #define BmRARE(sv)      ((XPVBM*)  SvANY(sv))->xbm_rare
511 #define BmUSEFUL(sv)    ((XPVBM*)  SvANY(sv))->xbm_useful
512 #define BmPREVIOUS(sv)  ((XPVBM*)  SvANY(sv))->xbm_previous
513
514 #define FmLINES(sv)     ((XPVFM*)  SvANY(sv))->xfm_lines
515
516 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
517 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
518 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
519 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
520
521 #define IoIFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ifp
522 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
523 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
524 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xio_lines
525 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
526 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
527 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
528 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
529 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
530 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
531 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
532 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
533 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
534 #define IoSUBPROCESS(sv)((XPVIO*)  SvANY(sv))->xio_subprocess
535 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
536 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
537
538 #define SvTAINTED(sv)     (SvMAGICAL(sv) && sv_tainted(sv))
539 #define SvTAINTED_on(sv)  STMT_START{ if(PL_tainting){sv_taint(sv);}   }STMT_END
540 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
541
542 #define SvTAINT(sv)                     \
543     STMT_START {                        \
544         if (PL_tainting) {              \
545             dTHR;                       \
546             if (PL_tainted)             \
547                 SvTAINTED_on(sv);       \
548         }                               \
549     } STMT_END
550
551 #define SvPV_force(sv, lp) sv_pvn_force(sv, &lp)
552 #define SvPV(sv, lp) sv_pvn(sv, &lp)
553 #define SvPV_nolen(sv) sv_pv(sv)
554
555 #define SvPVutf8_force(sv, lp) sv_pvutf8n_force(sv, &lp)
556 #define SvPVutf8(sv, lp) sv_pvutf8n(sv, &lp)
557 #define SvPVutf8_nolen(sv) sv_pvutf8(sv)
558
559 #define SvPVbyte_force(sv, lp) sv_pvbyte_force(sv, &lp)
560 #define SvPVbyte(sv, lp) sv_pvbyten(sv, &lp)
561 #define SvPVbyte_nolen(sv) sv_pvbyte(sv)
562
563 #define SvPVx(sv, lp) sv_pvn(sv, &lp)
564 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
565 #define SvPVutf8x(sv, lp) sv_pvutf8n(sv, &lp)
566 #define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
567 #define SvPVbytex(sv, lp) sv_pvbyten(sv, &lp)
568 #define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
569
570 #define SvIVx(sv) sv_iv(sv)
571 #define SvUVx(sv) sv_uv(sv)
572 #define SvNVx(sv) sv_nv(sv)
573
574 #define SvTRUEx(sv) sv_true(sv)
575
576 #define SvIV(sv) SvIVx(sv)
577 #define SvNV(sv) SvNVx(sv)
578 #define SvUV(sv) SvUVx(sv)
579 #define SvTRUE(sv) SvTRUEx(sv)
580
581 #ifndef CRIPPLED_CC
582 /* redefine some things to more efficient inlined versions */
583
584 /* Let us hope that bitmaps for UV and IV are the same */
585 #undef SvIV
586 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
587
588 #undef SvUV
589 #define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
590
591 #undef SvNV
592 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
593
594 #undef SvPV
595 #define SvPV(sv, lp) \
596     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
597      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
598      
599
600 #undef SvPV_force
601 #define SvPV_force(sv, lp) \
602     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
603      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force(sv, &lp))
604
605 #undef SvPV_nolen
606 #define SvPV_nolen(sv) \
607     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
608      ? SvPVX(sv) : sv_2pv_nolen(sv))
609      
610 #undef SvPVutf8
611 #define SvPVutf8(sv, lp) \
612     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
613      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
614      
615 #undef SvPVutf8_force
616 #define SvPVutf8_force(sv, lp) \
617     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK||SVf_UTF8) \
618      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
619
620 #undef SvPVutf8_nolen
621 #define SvPVutf8_nolen(sv) \
622     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK||SVf_UTF8)\
623      ? SvPVX(sv) : sv_2pvutf8_nolen(sv))
624      
625 #undef SvPVutf8
626 #define SvPVutf8(sv, lp) \
627     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
628      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
629      
630 #undef SvPVutf8_force
631 #define SvPVutf8_force(sv, lp) \
632     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK||SVf_UTF8) \
633      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
634
635 #undef SvPVutf8_nolen
636 #define SvPVutf8_nolen(sv) \
637     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
638      ? SvPVX(sv) : sv_2pvutf8_nolen(sv))
639      
640 #undef SvPVbyte
641 #define SvPVbyte(sv, lp) \
642     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
643      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
644      
645 #undef SvPVbyte_force
646 #define SvPVbyte_force(sv, lp) \
647     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST)) == (SVf_POK) \
648      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyte_force(sv, &lp))
649
650 #undef SvPVbyte_nolen
651 #define SvPVbyte_nolen(sv) \
652     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)\
653      ? SvPVX(sv) : sv_2pvbyte_nolen(sv))
654      
655
656 #ifdef __GNUC__
657 #  undef SvIVx
658 #  undef SvUVx
659 #  undef SvNVx
660 #  undef SvPVx
661 #  undef SvPVutf8x
662 #  undef SvPVbytex
663 #  undef SvTRUE
664 #  undef SvTRUEx
665 #  define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
666 #  define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
667 #  define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
668 #  define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
669 #  define SvPVutf8x(sv, lp) ({SV *nsv = (sv); SvPVutf8(nsv, lp); })
670 #  define SvPVbytex(sv, lp) ({SV *nsv = (sv); SvPVbyte(nsv, lp); })
671 #  define SvTRUE(sv) (                                          \
672     !sv                                                         \
673     ? 0                                                         \
674     :    SvPOK(sv)                                              \
675         ?   (({XPV *nxpv = (XPV*)SvANY(sv);                     \
676              nxpv &&                                            \
677              (*nxpv->xpv_pv > '0' ||                            \
678               nxpv->xpv_cur > 1 ||                              \
679               (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); })      \
680              ? 1                                                \
681              : 0)                                               \
682         :                                                       \
683             SvIOK(sv)                                           \
684             ? SvIVX(sv) != 0                                    \
685             :   SvNOK(sv)                                       \
686                 ? SvNVX(sv) != 0.0                              \
687                 : sv_2bool(sv) )
688 #  define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
689 #else /* __GNUC__ */
690 #ifndef USE_THREADS
691 /* These inlined macros use globals, which will require a thread
692  * declaration in user code, so we avoid them under threads */
693
694 #  undef SvIVx
695 #  undef SvUVx
696 #  undef SvNVx
697 #  undef SvPVx
698 #  undef SvPVutf8x
699 #  undef SvPVbytex
700 #  undef SvTRUE
701 #  undef SvTRUEx
702 #  define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
703 #  define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
704 #  define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
705 #  define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
706 #  define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
707 #  define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
708 #  define SvTRUE(sv) (                                          \
709     !sv                                                         \
710     ? 0                                                         \
711     :    SvPOK(sv)                                              \
712         ?   ((PL_Xpv = (XPV*)SvANY(sv)) &&                      \
713              (*PL_Xpv->xpv_pv > '0' ||                          \
714               PL_Xpv->xpv_cur > 1 ||                            \
715               (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0'))      \
716              ? 1                                                \
717              : 0)                                               \
718         :                                                       \
719             SvIOK(sv)                                           \
720             ? SvIVX(sv) != 0                                    \
721             :   SvNOK(sv)                                       \
722                 ? SvNVX(sv) != 0.0                              \
723                 : sv_2bool(sv) )
724 #  define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
725 #endif /* !USE_THREADS */
726 #endif /* !__GNU__ */
727 #endif /* !CRIPPLED_CC */
728
729 #define newRV_inc(sv)   newRV(sv)
730
731 /* the following macros update any magic values this sv is associated with */
732
733 #define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
734 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
735
736 #define SvSetSV_and(dst,src,finally) \
737         STMT_START {                                    \
738             if ((dst) != (src)) {                       \
739                 sv_setsv(dst, src);                     \
740                 finally;                                \
741             }                                           \
742         } STMT_END
743 #define SvSetSV_nosteal_and(dst,src,finally) \
744         STMT_START {                                    \
745             if ((dst) != (src)) {                       \
746                 U32 tMpF = SvFLAGS(src) & SVs_TEMP;     \
747                 SvTEMP_off(src);                        \
748                 sv_setsv(dst, src);                     \
749                 SvFLAGS(src) |= tMpF;                   \
750                 finally;                                \
751             }                                           \
752         } STMT_END
753
754 #define SvSetSV(dst,src) \
755                 SvSetSV_and(dst,src,/*nothing*/;)
756 #define SvSetSV_nosteal(dst,src) \
757                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
758
759 #define SvSetMagicSV(dst,src) \
760                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
761 #define SvSetMagicSV_nosteal(dst,src) \
762                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
763
764 #ifdef DEBUGGING
765 #define SvPEEK(sv) sv_peek(sv)
766 #else
767 #define SvPEEK(sv) ""
768 #endif
769
770 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
771
772 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
773
774 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
775
776 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
777 #define Sv_Grow sv_grow