e19bb22db413e9bc426ed2109f101fe28e6ecea3
[p5sagit/p5-mst-13.2.git] / bytecode.h
1 typedef char *pvcontents;
2 typedef char *strconst;
3 typedef U32 PV;
4 typedef char *op_tr_array;
5 typedef int comment_t;
6 typedef SV *svindex;
7 typedef OP *opindex;
8 typedef IV IV64;
9
10 EXT int iv_overflows INIT(0);
11
12 EXT SV *sv;
13 /*
14 #ifndef USE_THREADS
15 EXT OP *PL_op;
16 #endif
17 */
18 EXT XPV pv;
19
20 EXT void **obj_list;
21 EXT I32 obj_list_fill INIT(-1);
22
23 #ifdef INDIRECT_BGET_MACROS
24 #define BGET_FREAD(argp, len, nelem)    \
25          bs.fread((char*)(argp),(len),(nelem),bs.data)
26 #define BGET_FGETC() bs.fgetc(bs.data)
27 #else
28 #define BGET_FREAD(argp, len, nelem) PerlIO_read(fp, (argp), (len)*(nelem))
29 #define BGET_FGETC() PerlIO_getc(fp)
30 #endif /* INDIRECT_BGET_MACROS */
31
32 #define BGET_U32(arg)   \
33         BGET_FREAD(&arg, sizeof(U32), 1); arg = PerlSock_ntohl((U32)arg)
34 #define BGET_I32(arg)   \
35         BGET_FREAD(&arg, sizeof(I32), 1); arg = (I32)PerlSock_ntohl((U32)arg)
36 #define BGET_U16(arg)   \
37         BGET_FREAD(&arg, sizeof(U16), 1); arg = PerlSock_ntohs((U16)arg)
38 #define BGET_U8(arg)    arg = BGET_FGETC()
39
40 #if INDIRECT_BGET_MACROS
41 #define BGET_PV(arg)    STMT_START {    \
42         BGET_U32(arg);                  \
43         if (arg)                        \
44             bs.freadpv(arg, bs.data);   \
45         else {                          \
46             pv.xpv_pv = 0;              \
47             pv.xpv_len = 0;             \
48             pv.xpv_cur = 0;             \
49         }                               \
50     } STMT_END
51 #else
52 #define BGET_PV(arg)    STMT_START {            \
53         BGET_U32(arg);                          \
54         if (arg) {                              \
55             New(666, pv.xpv_pv, arg, char);     \
56             PerlIO_read(fp, pv.xpv_pv, arg);    \
57             pv.xpv_len = arg;                   \
58             pv.xpv_cur = arg - 1;               \
59         } else {                                \
60             pv.xpv_pv = 0;                      \
61             pv.xpv_len = 0;                     \
62             pv.xpv_cur = 0;                     \
63         }                                       \
64     } STMT_END
65 #endif /* INDIRECT_BGET_MACROS */
66
67 #define BGET_comment_t(arg) \
68         do { arg = BGET_FGETC(); } while (arg != '\n' && arg != EOF)
69
70 /*
71  * In the following, sizeof(IV)*4 is just a way of encoding 32 on 64-bit-IV
72  * machines such that 32-bit machine compilers don't whine about the shift
73  * count being too high even though the code is never reached there.
74  */
75 #define BGET_IV64(arg) STMT_START {                     \
76         U32 hi, lo;                                     \
77         BGET_U32(hi);                                   \
78         BGET_U32(lo);                                   \
79         if (sizeof(IV) == 8)                            \
80             arg = (IV) (hi << (sizeof(IV)*4) | lo);     \
81         else if (((I32)hi == -1 && (I32)lo < 0)         \
82                  || ((I32)hi == 0 && (I32)lo >= 0)) {   \
83             arg = (I32)lo;                              \
84         }                                               \
85         else {                                          \
86             iv_overflows++;                             \
87             arg = 0;                                    \
88         }                                               \
89     } STMT_END
90
91 #define BGET_op_tr_array(arg) do {      \
92         unsigned short *ary;            \
93         int i;                          \
94         New(666, ary, 256, unsigned short); \
95         BGET_FREAD(ary, 256, 2);        \
96         for (i = 0; i < 256; i++)       \
97             ary[i] = PerlSock_ntohs(ary[i]);    \
98         arg = (char *) ary;             \
99     } while (0)
100
101 #define BGET_pvcontents(arg)    arg = pv.xpv_pv
102 #define BGET_strconst(arg) STMT_START { \
103         for (arg = PL_tokenbuf; (*arg = BGET_FGETC()); arg++) /* nothing */; \
104         arg = PL_tokenbuf;                      \
105     } STMT_END
106
107 #define BGET_double(arg) STMT_START {   \
108         char *str;                      \
109         BGET_strconst(str);             \
110         arg = atof(str);                \
111     } STMT_END
112
113 #define BGET_objindex(arg, type) STMT_START {   \
114         U32 ix;                                 \
115         BGET_U32(ix);                           \
116         arg = (type)obj_list[ix];               \
117     } STMT_END
118 #define BGET_svindex(arg) BGET_objindex(arg, svindex)
119 #define BGET_opindex(arg) BGET_objindex(arg, opindex)
120
121 #define BSET_ldspecsv(sv, arg) sv = PL_specialsv_list[arg]
122                                     
123 #define BSET_sv_refcnt_add(svrefcnt, arg)       svrefcnt += arg
124 #define BSET_gp_refcnt_add(gprefcnt, arg)       gprefcnt += arg
125 #define BSET_gp_share(sv, arg) STMT_START {     \
126         gp_free((GV*)sv);                       \
127         GvGP(sv) = GvGP(arg);                   \
128     } STMT_END
129
130 #define BSET_gv_fetchpv(sv, arg)        sv = (SV*)gv_fetchpv(arg, TRUE, SVt_PV)
131 #define BSET_gv_stashpv(sv, arg)        sv = (SV*)gv_stashpv(arg, TRUE)
132 #define BSET_sv_magic(sv, arg)          sv_magic(sv, Nullsv, arg, 0, 0)
133 #define BSET_mg_pv(mg, arg)     mg->mg_ptr = arg; mg->mg_len = pv.xpv_cur
134 #define BSET_sv_upgrade(sv, arg)        (void)SvUPGRADE(sv, arg)
135 #define BSET_xpv(sv)    do {    \
136         SvPV_set(sv, pv.xpv_pv);        \
137         SvCUR_set(sv, pv.xpv_cur);      \
138         SvLEN_set(sv, pv.xpv_len);      \
139     } while (0)
140 #define BSET_av_extend(sv, arg) av_extend((AV*)sv, arg)
141
142 #define BSET_av_push(sv, arg)   av_push((AV*)sv, arg)
143 #define BSET_hv_store(sv, arg)  \
144         hv_store((HV*)sv, pv.xpv_pv, pv.xpv_cur, arg, 0)
145 #define BSET_pv_free(pv)        Safefree(pv.xpv_pv)
146 #define BSET_pregcomp(o, arg) \
147         ((PMOP*)o)->op_pmregexp = arg ? \
148                 CALLREGCOMP(arg, arg + pv.xpv_cur, ((PMOP*)o)) : 0
149 #define BSET_newsv(sv, arg)     sv = NEWSV(666,0); SvUPGRADE(sv, arg)
150 #define BSET_newop(o, arg)      o = (OP*)safemalloc(optype_size[arg])
151 #define BSET_newopn(o, arg) STMT_START {        \
152         OP *oldop = o;                          \
153         BSET_newop(o, arg);                     \
154         oldop->op_next = o;                     \
155     } STMT_END
156
157 #define BSET_ret(foo) return
158
159 /*
160  * Kludge special-case workaround for OP_MAPSTART
161  * which needs the ppaddr for OP_GREPSTART. Blech.
162  */
163 #define BSET_op_type(o, arg) STMT_START {       \
164         o->op_type = arg;                       \
165         if (arg == OP_MAPSTART)                 \
166             arg = OP_GREPSTART;                 \
167         o->op_ppaddr = ppaddr[arg];             \
168     } STMT_END
169 #define BSET_op_ppaddr(o, arg) croak("op_ppaddr not yet implemented")
170 #define BSET_curpad(pad, arg) pad = AvARRAY(arg)
171
172 #define BSET_OBJ_STORE(obj, ix)         \
173         (I32)ix > obj_list_fill ?       \
174         bset_obj_store(obj, (I32)ix) : (obj_list[ix] = obj)