Integrate with Sarathy.
[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 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE)
141
142 #define SVp_IOK         0x01000000      /* has valid non-public integer value */
143 #define SVp_NOK         0x02000000      /* has valid non-public numeric value */
144 #define SVp_POK         0x04000000      /* has valid non-public pointer value */
145 #define SVp_SCREAM      0x08000000      /* has been studied? */
146
147 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
148                          SVp_IOK|SVp_NOK|SVp_POK)
149
150 #define SVf_AMAGIC      0x10000000      /* has magical overloaded methods */
151
152 #define PRIVSHIFT 8
153
154 /* Some private flags. */
155
156 #define SVpad_OUR       0x80000000      /* pad name is "our" instead of "my" */
157
158 #define SVf_IVisUV      0x80000000      /* use XPVUV instead of XPVIV */
159
160 #define SVpfm_COMPILED  0x80000000      /* FORMLINE is compiled */
161
162 #define SVpbm_VALID     0x80000000
163 #define SVpbm_TAIL      0x40000000
164
165 #define SVrepl_EVAL     0x40000000      /* Replacement part of s///e */
166
167 #define SVphv_SHAREKEYS 0x20000000      /* keys live on shared string table */
168 #define SVphv_LAZYDEL   0x40000000      /* entry in xhv_eiter must be deleted */
169
170 #define SVprv_WEAKREF   0x80000000      /* Weak reference */
171
172 struct xrv {
173     SV *        xrv_rv;         /* pointer to another SV */
174 };
175
176 struct xpv {
177     char *      xpv_pv;         /* pointer to malloced string */
178     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
179     STRLEN      xpv_len;        /* allocated size */
180 };
181
182 struct xpviv {
183     char *      xpv_pv;         /* pointer to malloced string */
184     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
185     STRLEN      xpv_len;        /* allocated size */
186     IV          xiv_iv;         /* integer value or pv offset */
187 };
188
189 struct xpvuv {
190     char *      xpv_pv;         /* pointer to malloced string */
191     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
192     STRLEN      xpv_len;        /* allocated size */
193     UV          xuv_uv;         /* unsigned value or pv offset */
194 };
195
196 struct xpvnv {
197     char *      xpv_pv;         /* pointer to malloced string */
198     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
199     STRLEN      xpv_len;        /* allocated size */
200     IV          xiv_iv;         /* integer value or pv offset */
201     NV          xnv_nv;         /* numeric value, if any */
202 };
203
204 /* These structure must match the beginning of struct xpvhv in hv.h. */
205 struct xpvmg {
206     char *      xpv_pv;         /* pointer to malloced string */
207     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
208     STRLEN      xpv_len;        /* allocated size */
209     IV          xiv_iv;         /* integer value or pv offset */
210     NV          xnv_nv;         /* numeric value, if any */
211     MAGIC*      xmg_magic;      /* linked list of magicalness */
212     HV*         xmg_stash;      /* class package */
213 };
214
215 struct xpvlv {
216     char *      xpv_pv;         /* pointer to malloced string */
217     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
218     STRLEN      xpv_len;        /* allocated size */
219     IV          xiv_iv;         /* integer value or pv offset */
220     NV          xnv_nv;         /* numeric value, if any */
221     MAGIC*      xmg_magic;      /* linked list of magicalness */
222     HV*         xmg_stash;      /* class package */
223
224     STRLEN      xlv_targoff;
225     STRLEN      xlv_targlen;
226     SV*         xlv_targ;
227     char        xlv_type;
228 };
229
230 struct xpvgv {
231     char *      xpv_pv;         /* pointer to malloced string */
232     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
233     STRLEN      xpv_len;        /* allocated size */
234     IV          xiv_iv;         /* integer value or pv offset */
235     NV          xnv_nv;         /* numeric value, if any */
236     MAGIC*      xmg_magic;      /* linked list of magicalness */
237     HV*         xmg_stash;      /* class package */
238
239     GP*         xgv_gp;
240     char*       xgv_name;
241     STRLEN      xgv_namelen;
242     HV*         xgv_stash;
243     U8          xgv_flags;
244 };
245
246 struct xpvbm {
247     char *      xpv_pv;         /* pointer to malloced string */
248     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
249     STRLEN      xpv_len;        /* allocated size */
250     IV          xiv_iv;         /* integer value or pv offset */
251     NV          xnv_nv;         /* numeric value, if any */
252     MAGIC*      xmg_magic;      /* linked list of magicalness */
253     HV*         xmg_stash;      /* class package */
254
255     I32         xbm_useful;     /* is this constant pattern being useful? */
256     U16         xbm_previous;   /* how many characters in string before rare? */
257     U8          xbm_rare;       /* rarest character in string */
258 };
259
260 /* This structure much match XPVCV */
261
262 typedef U16 cv_flags_t;
263
264 struct xpvfm {
265     char *      xpv_pv;         /* pointer to malloced string */
266     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
267     STRLEN      xpv_len;        /* allocated size */
268     IV          xiv_iv;         /* integer value or pv offset */
269     NV          xnv_nv;         /* numeric value, if any */
270     MAGIC*      xmg_magic;      /* linked list of magicalness */
271     HV*         xmg_stash;      /* class package */
272
273     HV *        xcv_stash;
274     OP *        xcv_start;
275     OP *        xcv_root;
276     void      (*xcv_xsub)(pTHXo_ CV*);
277     ANY         xcv_xsubany;
278     GV *        xcv_gv;
279 #if defined(PERL_BINCOMPAT_5005)
280     GV *        xcv_filegv;     /* XXX unused (and deprecated) */
281 #endif
282     long        xcv_depth;      /* >= 2 indicates recursive call */
283     AV *        xcv_padlist;
284     CV *        xcv_outside;
285 #ifdef USE_THREADS
286     perl_mutex *xcv_mutexp;     /* protects xcv_owner */
287     struct perl_thread *xcv_owner;      /* current owner thread */
288 #endif /* USE_THREADS */
289     cv_flags_t  xcv_flags;
290
291     I32         xfm_lines;
292 };
293
294 struct xpvio {
295     char *      xpv_pv;         /* pointer to malloced string */
296     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
297     STRLEN      xpv_len;        /* allocated size */
298     IV          xiv_iv;         /* integer value or pv offset */
299     NV          xnv_nv;         /* numeric value, if any */
300     MAGIC*      xmg_magic;      /* linked list of magicalness */
301     HV*         xmg_stash;      /* class package */
302
303     PerlIO *    xio_ifp;        /* ifp and ofp are normally the same */
304     PerlIO *    xio_ofp;        /* but sockets need separate streams */
305     DIR *       xio_dirp;       /* for opendir, readdir, etc */
306     long        xio_lines;      /* $. */
307     long        xio_page;       /* $% */
308     long        xio_page_len;   /* $= */
309     long        xio_lines_left; /* $- */
310     char *      xio_top_name;   /* $^ */
311     GV *        xio_top_gv;     /* $^ */
312     char *      xio_fmt_name;   /* $~ */
313     GV *        xio_fmt_gv;     /* $~ */
314     char *      xio_bottom_name;/* $^B */
315     GV *        xio_bottom_gv;  /* $^B */
316     short       xio_subprocess; /* -| or |- */
317     char        xio_type;
318     char        xio_flags;
319 };
320
321 #define IOf_ARGV 1      /* this fp iterates over ARGV */
322 #define IOf_START 2     /* check for null ARGV and substitute '-' */
323 #define IOf_FLUSH 4     /* this fp wants a flush after write op */
324 #define IOf_DIDTOP 8    /* just did top of form */
325 #define IOf_UNTAINT 16  /* consider this fp (and its data) "safe" */
326 #define IOf_NOLINE  32  /* slurped a pseudo-line from empty file */
327
328 /* The following macros define implementation-independent predicates on SVs. */
329
330 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
331 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
332 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
333                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
334
335 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
336 #define SvOK_off(sv)            (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
337                                                   SVf_IVisUV),          \
338                                                         SvOOK_off(sv))
339 #define SvOK_off_exc_UV(sv)     (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC),   \
340                                                         SvOOK_off(sv))
341
342 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
343 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
344 #define SvIOKp_on(sv)           (SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
345 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
346 #define SvNOKp_on(sv)           (SvFLAGS(sv) |= SVp_NOK)
347 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
348 #define SvPOKp_on(sv)           (SvFLAGS(sv) |= SVp_POK)
349
350 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
351 #define SvIOK_on(sv)            (SvOOK_off(sv), \
352                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
353 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
354 #define SvIOK_only(sv)          (SvOK_off(sv), \
355                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
356 #define SvIOK_only_UV(sv)       (SvOK_off_exc_UV(sv), \
357                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
358  
359 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
360                                  == (SVf_IOK|SVf_IVisUV))
361 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
362                                  == SVf_IOK)
363
364 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
365 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
366 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
367
368 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
369 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
370 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
371 #define SvNOK_only(sv)          (SvOK_off(sv), \
372                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
373
374 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
375 #define SvPOK_on(sv)            (SvFLAGS(sv) |= (SVf_POK|SVp_POK))
376 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
377 #define SvPOK_only(sv)          (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|SVf_IVisUV),        \
378                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
379
380 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
381 #define SvOOK_on(sv)            (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
382 #define SvOOK_off(sv)           (SvOOK(sv) && sv_backoff(sv))
383
384 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
385 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
386 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
387
388 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
389 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
390 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
391
392 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
393 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
394 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
395
396 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
397 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
398 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
399
400 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
401 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
402 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
403
404 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
405 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
406 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
407
408 #define SvAMAGIC(sv)            (SvFLAGS(sv) & SVf_AMAGIC)
409 #define SvAMAGIC_on(sv)         (SvFLAGS(sv) |= SVf_AMAGIC)
410 #define SvAMAGIC_off(sv)        (SvFLAGS(sv) &= ~SVf_AMAGIC)
411
412 /*
413 #define Gv_AMG(stash) \
414         (HV_AMAGICmb(stash) && \
415          ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
416 */
417 #define Gv_AMG(stash)           (PL_amagic_generation && Gv_AMupdate(stash))
418
419 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
420                                   == (SVf_ROK|SVprv_WEAKREF))
421 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
422 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
423
424 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
425
426 #define SvPADBUSY(sv)           (SvFLAGS(sv) & SVs_PADBUSY)
427
428 #define SvPADTMP(sv)            (SvFLAGS(sv) & SVs_PADTMP)
429 #define SvPADTMP_on(sv)         (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
430 #define SvPADTMP_off(sv)        (SvFLAGS(sv) &= ~SVs_PADTMP)
431
432 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
433 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
434
435 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
436 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
437 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
438
439 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
440 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
441 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
442
443 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
444 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
445 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
446
447 #define SvSCREAM(sv)            (SvFLAGS(sv) & SVp_SCREAM)
448 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
449 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
450
451 #define SvCOMPILED(sv)          (SvFLAGS(sv) & SVpfm_COMPILED)
452 #define SvCOMPILED_on(sv)       (SvFLAGS(sv) |= SVpfm_COMPILED)
453 #define SvCOMPILED_off(sv)      (SvFLAGS(sv) &= ~SVpfm_COMPILED)
454
455 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
456 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
457 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
458
459 #define SvTAIL(sv)              (SvFLAGS(sv) & SVpbm_TAIL)
460 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
461 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
462
463 #define SvVALID(sv)             (SvFLAGS(sv) & SVpbm_VALID)
464 #define SvVALID_on(sv)          (SvFLAGS(sv) |= SVpbm_VALID)
465 #define SvVALID_off(sv)         (SvFLAGS(sv) &= ~SVpbm_VALID)
466
467 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
468 #define SvRVx(sv) SvRV(sv)
469
470 #define SvIVX(sv) ((XPVIV*)  SvANY(sv))->xiv_iv
471 #define SvIVXx(sv) SvIVX(sv)
472 #define SvUVX(sv) ((XPVUV*)  SvANY(sv))->xuv_uv
473 #define SvUVXx(sv) SvUVX(sv)
474 #define SvNVX(sv)  ((XPVNV*)SvANY(sv))->xnv_nv
475 #define SvNVXx(sv) SvNVX(sv)
476 #define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
477 #define SvPVXx(sv) SvPVX(sv)
478 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
479 #define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
480 #define SvLENx(sv) SvLEN(sv)
481 #define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
482 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
483 #define SvMAGIC(sv)     ((XPVMG*)  SvANY(sv))->xmg_magic
484 #define SvSTASH(sv)     ((XPVMG*)  SvANY(sv))->xmg_stash
485
486 #define SvIV_set(sv, val) \
487         STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
488                 (((XPVIV*)  SvANY(sv))->xiv_iv = val); } STMT_END
489 #define SvNV_set(sv, val) \
490         STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
491                 (((XPVNV*)  SvANY(sv))->xnv_nv = val); } STMT_END
492 #define SvPV_set(sv, val) \
493         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
494                 (((XPV*)  SvANY(sv))->xpv_pv = val); } STMT_END
495 #define SvCUR_set(sv, val) \
496         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
497                 (((XPV*)  SvANY(sv))->xpv_cur = val); } STMT_END
498 #define SvLEN_set(sv, val) \
499         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
500                 (((XPV*)  SvANY(sv))->xpv_len = val); } STMT_END
501 #define SvEND_set(sv, val) \
502         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
503                 (((XPV*)  SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
504
505 #define BmRARE(sv)      ((XPVBM*)  SvANY(sv))->xbm_rare
506 #define BmUSEFUL(sv)    ((XPVBM*)  SvANY(sv))->xbm_useful
507 #define BmPREVIOUS(sv)  ((XPVBM*)  SvANY(sv))->xbm_previous
508
509 #define FmLINES(sv)     ((XPVFM*)  SvANY(sv))->xfm_lines
510
511 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
512 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
513 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
514 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
515
516 #define IoIFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ifp
517 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
518 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
519 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xio_lines
520 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
521 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
522 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
523 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
524 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
525 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
526 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
527 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
528 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
529 #define IoSUBPROCESS(sv)((XPVIO*)  SvANY(sv))->xio_subprocess
530 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
531 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
532
533 #define SvTAINTED(sv)     (SvMAGICAL(sv) && sv_tainted(sv))
534 #define SvTAINTED_on(sv)  STMT_START{ if(PL_tainting){sv_taint(sv);}   }STMT_END
535 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
536
537 #define SvTAINT(sv)                     \
538     STMT_START {                        \
539         if (PL_tainting) {              \
540             dTHR;                       \
541             if (PL_tainted)             \
542                 SvTAINTED_on(sv);       \
543         }                               \
544     } STMT_END
545
546 #define SvPV_force(sv, lp) sv_pvn_force(sv, &lp)
547 #define SvPV(sv, lp) sv_pvn(sv, &lp)
548 #define SvPV_nolen(sv) sv_pv(sv)
549 #define SvIVx(sv) sv_iv(sv)
550 #define SvUVx(sv) sv_uv(sv)
551 #define SvNVx(sv) sv_nv(sv)
552 #define SvPVx(sv, lp) sv_pvn(sv, &lp)
553 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
554 #define SvTRUEx(sv) sv_true(sv)
555
556 #define SvIV(sv) SvIVx(sv)
557 #define SvNV(sv) SvNVx(sv)
558 #define SvUV(sv) SvUVx(sv)
559 #define SvTRUE(sv) SvTRUEx(sv)
560
561 #ifndef CRIPPLED_CC
562 /* redefine some things to more efficient inlined versions */
563
564 /* Let us hope that bitmaps for UV and IV are the same */
565 #undef SvIV
566 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
567
568 #undef SvUV
569 #define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
570
571 #undef SvNV
572 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
573
574 #undef SvPV
575 #define SvPV(sv, lp) \
576     (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
577
578 #undef SvPV_force
579 #define SvPV_force(sv, lp) \
580     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
581      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force(sv, &lp))
582
583 #undef SvPV_nolen
584 #define SvPV_nolen(sv) \
585     (SvPOK(sv) ? SvPVX(sv) : sv_2pv_nolen(sv))
586
587 #ifdef __GNUC__
588 #  undef SvIVx
589 #  undef SvUVx
590 #  undef SvNVx
591 #  undef SvPVx
592 #  undef SvTRUE
593 #  undef SvTRUEx
594 #  define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
595 #  define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
596 #  define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
597 #  define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
598 #  define SvTRUE(sv) (                                          \
599     !sv                                                         \
600     ? 0                                                         \
601     :    SvPOK(sv)                                              \
602         ?   (({XPV *nxpv = (XPV*)SvANY(sv);                     \
603              nxpv &&                                            \
604              (*nxpv->xpv_pv > '0' ||                            \
605               nxpv->xpv_cur > 1 ||                              \
606               (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); })      \
607              ? 1                                                \
608              : 0)                                               \
609         :                                                       \
610             SvIOK(sv)                                           \
611             ? SvIVX(sv) != 0                                    \
612             :   SvNOK(sv)                                       \
613                 ? SvNVX(sv) != 0.0                              \
614                 : sv_2bool(sv) )
615 #  define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
616 #else /* __GNUC__ */
617 #ifndef USE_THREADS
618 /* These inlined macros use globals, which will require a thread
619  * declaration in user code, so we avoid them under threads */
620
621 #  undef SvIVx
622 #  undef SvUVx
623 #  undef SvNVx
624 #  undef SvPVx
625 #  undef SvTRUE
626 #  undef SvTRUEx
627 #  define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
628 #  define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
629 #  define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
630 #  define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
631 #  define SvTRUE(sv) (                                          \
632     !sv                                                         \
633     ? 0                                                         \
634     :    SvPOK(sv)                                              \
635         ?   ((PL_Xpv = (XPV*)SvANY(sv)) &&                      \
636              (*PL_Xpv->xpv_pv > '0' ||                          \
637               PL_Xpv->xpv_cur > 1 ||                            \
638               (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0'))      \
639              ? 1                                                \
640              : 0)                                               \
641         :                                                       \
642             SvIOK(sv)                                           \
643             ? SvIVX(sv) != 0                                    \
644             :   SvNOK(sv)                                       \
645                 ? SvNVX(sv) != 0.0                              \
646                 : sv_2bool(sv) )
647 #  define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
648 #endif /* !USE_THREADS */
649 #endif /* !__GNU__ */
650 #endif /* !CRIPPLED_CC */
651
652 #define newRV_inc(sv)   newRV(sv)
653
654 /* the following macros update any magic values this sv is associated with */
655
656 #define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
657 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
658
659 #define SvSetSV_and(dst,src,finally) \
660         STMT_START {                                    \
661             if ((dst) != (src)) {                       \
662                 sv_setsv(dst, src);                     \
663                 finally;                                \
664             }                                           \
665         } STMT_END
666 #define SvSetSV_nosteal_and(dst,src,finally) \
667         STMT_START {                                    \
668             if ((dst) != (src)) {                       \
669                 U32 tMpF = SvFLAGS(src) & SVs_TEMP;     \
670                 SvTEMP_off(src);                        \
671                 sv_setsv(dst, src);                     \
672                 SvFLAGS(src) |= tMpF;                   \
673                 finally;                                \
674             }                                           \
675         } STMT_END
676
677 #define SvSetSV(dst,src) \
678                 SvSetSV_and(dst,src,/*nothing*/;)
679 #define SvSetSV_nosteal(dst,src) \
680                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
681
682 #define SvSetMagicSV(dst,src) \
683                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
684 #define SvSetMagicSV_nosteal(dst,src) \
685                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
686
687 #ifdef DEBUGGING
688 #define SvPEEK(sv) sv_peek(sv)
689 #else
690 #define SvPEEK(sv) ""
691 #endif
692
693 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
694
695 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
696
697 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
698
699 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
700 #define Sv_Grow sv_grow