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