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