Mopup for #6204.
[p5sagit/p5-mst-13.2.git] / ext / ByteLoader / 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 #define BGET_FREAD(argp, len, nelem)    \
11          PerlIO_read(PL_rsfp,(char*)(argp),(len)*(nelem))
12 #define BGET_FGETC() PerlIO_getc(PL_rsfp)
13
14 #define BGET_U32(arg)   \
15         BGET_FREAD(&arg, sizeof(U32), 1)
16 #define BGET_I32(arg)   \
17         BGET_FREAD(&arg, sizeof(I32), 1)
18 #define BGET_U16(arg)   \
19         BGET_FREAD(&arg, sizeof(U16), 1)
20 #define BGET_U8(arg)    arg = BGET_FGETC()
21
22 #define BGET_PV(arg)    STMT_START {                                    \
23         BGET_U32(arg);                                                  \
24         if (arg) {                                                      \
25             New(666, bytecode_pv.xpv_pv, arg, char);                    \
26             PerlIO_read(PL_rsfp, (void*)bytecode_pv.xpv_pv, arg);       \
27             bytecode_pv.xpv_len = arg;                                  \
28             bytecode_pv.xpv_cur = arg - 1;                              \
29         } else {                                                        \
30             bytecode_pv.xpv_pv = 0;                                     \
31             bytecode_pv.xpv_len = 0;                                    \
32             bytecode_pv.xpv_cur = 0;                                    \
33         }                                                               \
34     } STMT_END
35
36 #ifdef BYTELOADER_LOG_COMMENTS
37 #  define BGET_comment_t(arg) \
38     STMT_START {                                                        \
39         char buf[1024];                                                 \
40         int i = 0;                                                      \
41         do {                                                            \
42             arg = BGET_FGETC();                                         \
43             buf[i++] = (char)arg;                                       \
44         } while (arg != '\n' && arg != EOF);                            \
45         buf[i] = '\0';                                                  \
46         PerlIO_printf(PerlIO_stderr(), "%s", buf);                      \
47     } STMT_END
48 #else
49 #  define BGET_comment_t(arg) \
50         do { arg = BGET_FGETC(); } while (arg != '\n' && arg != EOF)
51 #endif
52
53 /*
54  * In the following, sizeof(IV)*4 is just a way of encoding 32 on 64-bit-IV
55  * machines such that 32-bit machine compilers don't whine about the shift
56  * count being too high even though the code is never reached there.
57  */
58 #define BGET_IV64(arg) STMT_START {                     \
59         U32 hi, lo;                                     \
60         BGET_U32(hi);                                   \
61         BGET_U32(lo);                                   \
62         if (sizeof(IV) == 8)                            \
63             arg = ((IV)hi << (sizeof(IV)*4) | (IV)lo);  \
64         else if (((I32)hi == -1 && (I32)lo < 0)         \
65                  || ((I32)hi == 0 && (I32)lo >= 0)) {   \
66             arg = (I32)lo;                              \
67         }                                               \
68         else {                                          \
69             bytecode_iv_overflows++;                    \
70             arg = 0;                                    \
71         }                                               \
72     } STMT_END
73
74 #define BGET_op_tr_array(arg) do {                      \
75         unsigned short *ary;                            \
76         int i;                                          \
77         New(666, ary, 256, unsigned short);             \
78         BGET_FREAD(ary, sizeof(unsigned short), 256);   \
79         arg = (char *) ary;                             \
80     } while (0)
81
82 #define BGET_pvcontents(arg)    arg = bytecode_pv.xpv_pv
83 #define BGET_strconst(arg) STMT_START { \
84         for (arg = PL_tokenbuf; (*arg = BGET_FGETC()); arg++) /* nothing */; \
85         arg = PL_tokenbuf;                      \
86     } STMT_END
87
88 #define BGET_NV(arg) STMT_START {       \
89         char *str;                      \
90         BGET_strconst(str);             \
91         arg = Atof(str);                \
92     } STMT_END
93
94 #define BGET_objindex(arg, type) STMT_START {   \
95         U32 ix;                                 \
96         BGET_U32(ix);                           \
97         arg = (type)bytecode_obj_list[ix];              \
98     } STMT_END
99 #define BGET_svindex(arg) BGET_objindex(arg, svindex)
100 #define BGET_opindex(arg) BGET_objindex(arg, opindex)
101
102 #define BSET_ldspecsv(sv, arg) sv = specialsv_list[arg]
103                                     
104 #define BSET_sv_refcnt_add(svrefcnt, arg)       svrefcnt += arg
105 #define BSET_gp_refcnt_add(gprefcnt, arg)       gprefcnt += arg
106 #define BSET_gp_share(sv, arg) STMT_START {     \
107         gp_free((GV*)sv);                       \
108         GvGP(sv) = GvGP(arg);                   \
109     } STMT_END
110
111 #define BSET_gv_fetchpv(sv, arg)        sv = (SV*)gv_fetchpv(arg, TRUE, SVt_PV)
112 #define BSET_gv_stashpv(sv, arg)        sv = (SV*)gv_stashpv(arg, TRUE)
113 #define BSET_sv_magic(sv, arg)          sv_magic(sv, Nullsv, arg, 0, 0)
114 #define BSET_mg_pv(mg, arg)     mg->mg_ptr = arg; mg->mg_len = bytecode_pv.xpv_cur
115 #define BSET_sv_upgrade(sv, arg)        (void)SvUPGRADE(sv, arg)
116 #define BSET_xpv(sv)    do {    \
117         SvPV_set(sv, bytecode_pv.xpv_pv);       \
118         SvCUR_set(sv, bytecode_pv.xpv_cur);     \
119         SvLEN_set(sv, bytecode_pv.xpv_len);     \
120     } while (0)
121 #define BSET_av_extend(sv, arg) av_extend((AV*)sv, arg)
122
123 #define BSET_av_push(sv, arg)   av_push((AV*)sv, arg)
124 #define BSET_hv_store(sv, arg)  \
125         hv_store((HV*)sv, bytecode_pv.xpv_pv, bytecode_pv.xpv_cur, arg, 0)
126 #define BSET_pv_free(pv)        Safefree(pv.xpv_pv)
127 #define BSET_pregcomp(o, arg) \
128         ((PMOP*)o)->op_pmregexp = arg ? \
129                 CALLREGCOMP(aTHX_ arg, arg + bytecode_pv.xpv_cur, ((PMOP*)o)) : 0
130 #define BSET_newsv(sv, arg)                             \
131         STMT_START {                                    \
132             sv = (arg == SVt_PVAV ? (SV*)newAV() :      \
133                   arg == SVt_PVHV ? (SV*)newHV() :      \
134                   NEWSV(666,0));                        \
135             SvUPGRADE(sv, arg);                         \
136         } STMT_END
137 #define BSET_newop(o, arg)      ((o = (OP*)safemalloc(optype_size[arg])), \
138                                  memzero((char*)o,optype_size[arg]))
139 #define BSET_newopn(o, arg) STMT_START {        \
140         OP *oldop = o;                          \
141         BSET_newop(o, arg);                     \
142         oldop->op_next = o;                     \
143     } STMT_END
144
145 #define BSET_ret(foo) STMT_START {                      \
146         if (bytecode_obj_list)                          \
147             Safefree(bytecode_obj_list);                \
148         LEAVE;                                          \
149         return;                                         \
150     } STMT_END
151
152 /*
153  * Kludge special-case workaround for OP_MAPSTART
154  * which needs the ppaddr for OP_GREPSTART. Blech.
155  */
156 #define BSET_op_type(o, arg) STMT_START {       \
157         o->op_type = arg;                       \
158         if (arg == OP_MAPSTART)                 \
159             arg = OP_GREPSTART;                 \
160         o->op_ppaddr = PL_ppaddr[arg];          \
161     } STMT_END
162 #define BSET_op_ppaddr(o, arg) Perl_croak(aTHX_ "op_ppaddr not yet implemented")
163 #define BSET_curpad(pad, arg) STMT_START {      \
164         PL_comppad = (AV *)arg;                 \
165         pad = AvARRAY(arg);                     \
166     } STMT_END
167 /* this works now that Sarathy's changed the CopFILE_set macro to do the SvREFCNT_inc()
168         -- BKS 6-2-2000 */
169 #define BSET_cop_file(cop, arg)         CopFILE_set(cop,arg)
170 #define BSET_cop_line(cop, arg)         CopLINE_set(cop,arg)
171 #define BSET_cop_stashpv(cop, arg)      CopSTASHPV_set(cop,arg)
172
173 /* this is simply stolen from the code in newATTRSUB() */
174 #define BSET_push_begin(ary,cv)                         \
175         STMT_START {                                    \
176             I32 oldscope = PL_scopestack_ix;            \
177             ENTER;                                      \
178             SAVECOPFILE(&PL_compiling);                 \
179             SAVECOPLINE(&PL_compiling);                 \
180             save_svref(&PL_rs);                         \
181             sv_setsv(PL_rs, PL_nrs);                    \
182             if (!PL_beginav)                            \
183                 PL_beginav = newAV();                   \
184             av_push(PL_beginav, cv);                    \
185             call_list(oldscope, PL_beginav);            \
186             PL_curcop = &PL_compiling;                  \
187             PL_compiling.op_private = PL_hints;         \
188             LEAVE;                                      \
189         } STMT_END
190 #define BSET_push_init(ary,cv)                                                          \
191         STMT_START {                                                                    \
192             av_unshift((PL_initav ? PL_initav : (PL_initav = newAV(), PL_initav)), 1);  \
193             av_store(PL_initav, 0, cv);                                                 \
194         } STMT_END
195 #define BSET_push_end(ary,cv)                                                                   \
196         STMT_START {                                                                    \
197             av_unshift((PL_endav ? PL_endav : (PL_endav = newAV(), PL_endav)), 1);      \
198             av_store(PL_endav, 0, cv);                                                  \
199         } STMT_END
200 #define BSET_OBJ_STORE(obj, ix)                 \
201         (I32)ix > bytecode_obj_list_fill ?      \
202         bset_obj_store(aTHXo_ obj, (I32)ix) : (bytecode_obj_list[ix] = obj)
203 #define BYTECODE_HEADER_CHECK                           \
204         STMT_START {                                    \
205             U32 sz;                                     \
206             strconst str;                               \
207             char *badpart;                              \
208                                                         \
209             BGET_U32(sz); /* Magic: 'PLBC' */           \
210             if (sz != 0x43424c50) {                     \
211                 badpart = "bad magic";                  \
212                 goto bch_fail;                          \
213             }                                           \
214             BGET_strconst(str); /* archname */          \
215             if (strNE(str, ARCHNAME)) {                 \
216                 badpart = "wrong architecture";         \
217                 goto bch_fail;                          \
218             }                                           \
219             BGET_U32(sz); /* ivsize */                  \
220             if (sz != IVSIZE) {                         \
221                 badpart = "different IVSIZE";           \
222                 goto bch_fail;                          \
223             }                                           \
224             BGET_U32(sz); /* ptrsize */                 \
225             if (sz != PTRSIZE) {                        \
226                 badpart = "different PTRSIZE";          \
227                 goto bch_fail;                          \
228             }                                           \
229             BGET_strconst(str); /* byteorder */         \
230             if (strNE(str, STRINGIFY(BYTEORDER))) {     \
231                 badpart = "different byteorder";        \
232         bch_fail:                                       \
233                 Perl_croak(aTHX_ "Invalid bytecode for this architecture: %s\n",        \
234                                 badpart);               \
235             }                                           \
236         } STMT_END