Further Byteloader tweaks from Enache.
[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 char *pvindex;
9
10 #define BGET_FREAD(argp, len, nelem)    \
11          bl_read(bstate->bs_fdata,(char*)(argp),(len),(nelem))
12 #define BGET_FGETC() bl_getc(bstate->bs_fdata)
13
14 /* all this should be made endianness-agnostic */
15
16 #define BGET_U8(arg)    arg = BGET_FGETC()
17 #define BGET_U16(arg)   \
18         BGET_FREAD(&arg, sizeof(U16), 1)
19 #define BGET_U32(arg)   \
20         BGET_FREAD(&arg, sizeof(U32), 1)
21 #define BGET_UV(arg)    \
22         BGET_FREAD(&arg, sizeof(UV), 1)
23
24 #define BGET_I32(arg)   BGET_U32(arg)
25 #define BGET_IV(arg)    BGET_UV(arg)
26
27 #define BGET_PV(arg)    STMT_START {                                    \
28         BGET_U32(arg);                                                  \
29         if (arg) {                                                      \
30             New(666, bstate->bs_pv.xpv_pv, arg, char);                  \
31             bl_read(bstate->bs_fdata, (void*)bstate->bs_pv.xpv_pv, arg, 1);\
32             bstate->bs_pv.xpv_len = arg;                                \
33             bstate->bs_pv.xpv_cur = arg - 1;                            \
34         } else {                                                        \
35             bstate->bs_pv.xpv_pv = 0;                                   \
36             bstate->bs_pv.xpv_len = 0;                                  \
37             bstate->bs_pv.xpv_cur = 0;                                  \
38         }                                                               \
39     } STMT_END
40
41 #ifdef BYTELOADER_LOG_COMMENTS
42 #  define BGET_comment_t(arg) \
43     STMT_START {                                                        \
44         char buf[1024];                                                 \
45         int i = 0;                                                      \
46         do {                                                            \
47             arg = BGET_FGETC();                                         \
48             buf[i++] = (char)arg;                                       \
49         } while (arg != '\n' && arg != EOF);                            \
50         buf[i] = '\0';                                                  \
51         PerlIO_printf(PerlIO_stderr(), "%s", buf);                      \
52     } STMT_END
53 #else
54 #  define BGET_comment_t(arg) \
55         do { arg = BGET_FGETC(); } while (arg != '\n' && arg != EOF)
56 #endif
57
58
59 #define BGET_op_tr_array(arg) do {                      \
60         unsigned short *ary, len;                       \
61         BGET_U16(len);                                  \
62         New(666, ary, len, unsigned short);             \
63         BGET_FREAD(ary, sizeof(unsigned short), len);   \
64         arg = (char *) ary;                             \
65     } while (0)
66
67 #define BGET_pvcontents(arg)    arg = bstate->bs_pv.xpv_pv
68 #define BGET_strconst(arg) STMT_START { \
69         for (arg = PL_tokenbuf; (*arg = BGET_FGETC()); arg++) /* nothing */; \
70         arg = PL_tokenbuf;                      \
71     } STMT_END
72
73 #define BGET_NV(arg) STMT_START {       \
74         char *str;                      \
75         BGET_strconst(str);             \
76         arg = Atof(str);                \
77     } STMT_END
78
79 #define BGET_objindex(arg, type) STMT_START {   \
80         BGET_U32(ix);                           \
81         arg = (type)bstate->bs_obj_list[ix];    \
82     } STMT_END
83 #define BGET_svindex(arg) BGET_objindex(arg, svindex)
84 #define BGET_opindex(arg) BGET_objindex(arg, opindex)
85 #define BGET_pvindex(arg) STMT_START {                  \
86         BGET_objindex(arg, pvindex);                    \
87         arg = arg ? savepv(arg) : arg;                  \
88     } STMT_END
89
90 #define BSET_ldspecsv(sv, arg) sv = specialsv_list[arg]
91 #define BSET_ldspecsvx(sv, arg) STMT_START {    \
92         BSET_ldspecsv(sv, arg);                 \
93         BSET_OBJ_STOREX(sv);                    \
94     } STMT_END
95
96 #define BSET_stpv(pv, arg) STMT_START {         \
97         BSET_OBJ_STORE(pv, arg);                \
98         SAVEFREEPV(pv);                         \
99     } STMT_END
100                                     
101 #define BSET_sv_refcnt_add(svrefcnt, arg)       svrefcnt += arg
102 #define BSET_gp_refcnt_add(gprefcnt, arg)       gprefcnt += arg
103 #define BSET_gp_share(sv, arg) STMT_START {     \
104         gp_free((GV*)sv);                       \
105         GvGP(sv) = GvGP(arg);                   \
106     } STMT_END
107
108 #define BSET_gv_fetchpv(sv, arg)        sv = (SV*)gv_fetchpv(arg, TRUE, SVt_PV)
109 #define BSET_gv_fetchpvx(sv, arg) STMT_START {  \
110         BSET_gv_fetchpv(sv, arg);               \
111         BSET_OBJ_STOREX(sv);                    \
112     } STMT_END
113
114 #define BSET_gv_stashpv(sv, arg)        sv = (SV*)gv_stashpv(arg, TRUE)
115 #define BSET_gv_stashpvx(sv, arg) STMT_START {  \
116         BSET_gv_stashpv(sv, arg);               \
117         BSET_OBJ_STOREX(sv);                    \
118     } STMT_END
119
120 #define BSET_sv_magic(sv, arg)          sv_magic(sv, Nullsv, arg, 0, 0)
121 #define BSET_mg_name(mg, arg)   mg->mg_ptr = arg; mg->mg_len = bstate->bs_pv.xpv_cur
122 #define BSET_mg_namex(mg, arg)                  \
123         (mg->mg_ptr = (char*)SvREFCNT_inc((SV*)arg),    \
124          mg->mg_len = HEf_SVKEY)
125 #define BSET_sv_upgrade(sv, arg)        (void)SvUPGRADE(sv, arg)
126 #define BSET_xpv(sv)    do {    \
127         SvPV_set(sv, bstate->bs_pv.xpv_pv);     \
128         SvCUR_set(sv, bstate->bs_pv.xpv_cur);   \
129         SvLEN_set(sv, bstate->bs_pv.xpv_len);   \
130     } while (0)
131 #define BSET_av_extend(sv, arg) av_extend((AV*)sv, arg)
132
133 #define BSET_av_push(sv, arg)   av_push((AV*)sv, arg)
134 #define BSET_av_pushx(sv, arg)  (AvARRAY(sv)[++AvFILLp(sv)] = arg)
135 #define BSET_hv_store(sv, arg)  \
136         hv_store((HV*)sv, bstate->bs_pv.xpv_pv, bstate->bs_pv.xpv_cur, arg, 0)
137 #define BSET_pv_free(pv)        Safefree(pv.xpv_pv)
138
139
140 #ifdef USE_ITHREADS
141
142 /* copied after the code in newPMOP() */
143 #define BSET_pregcomp(o, arg) \
144     STMT_START { \
145         SV* repointer; \
146         REGEXP* rx = arg ? \
147             CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv_cur, cPMOPx(o)) : \
148             Null(REGEXP*); \
149         if(av_len((AV*) PL_regex_pad[0]) > -1) { \
150             repointer = av_pop((AV*)PL_regex_pad[0]); \
151             cPMOPx(o)->op_pmoffset = SvIV(repointer); \
152             SvREPADTMP_off(repointer); \
153             sv_setiv(repointer,PTR2IV(rx)); \
154         } else { \
155             repointer = newSViv(PTR2IV(rx)); \
156             av_push(PL_regex_padav,SvREFCNT_inc(repointer)); \
157             cPMOPx(o)->op_pmoffset = av_len(PL_regex_padav); \
158             PL_regex_pad = AvARRAY(PL_regex_padav); \
159         } \
160     } STMT_END
161
162 #else
163 #define BSET_pregcomp(o, arg) \
164     STMT_START { \
165         PM_SETRE(((PMOP*)o), (arg ? \
166              CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv_cur, cPMOPx(o)): \
167              Null(REGEXP*))); \
168     } STMT_END
169
170 #endif /* USE_THREADS */
171
172
173 #define BSET_newsv(sv, arg)                             \
174         STMT_START {                                    \
175             sv = (arg == SVt_PVAV ? (SV*)newAV() :      \
176                   arg == SVt_PVHV ? (SV*)newHV() :      \
177                   NEWSV(666,0));                        \
178             SvUPGRADE(sv, arg);                         \
179         } STMT_END
180 #define BSET_newsvx(sv, arg) STMT_START {               \
181             BSET_newsv(sv, arg &  SVTYPEMASK);          \
182             SvFLAGS(sv) = arg;                          \
183             BSET_OBJ_STOREX(sv);                        \
184         } STMT_END
185 #define BSET_newop(o, arg)                              \
186         ((o = (OP*)safemalloc(arg)), memzero((char*)o,arg))
187 #define BSET_newopx(o, arg) STMT_START {        \
188         register int sz = arg & 0x7f;           \
189         register OP* new = (OP*) safemalloc(sz);\
190         memzero(new, sz);                       \
191         /* new->op_next = o; XXX */             \
192         o = new;                                \
193         arg >>=7;                               \
194         BSET_op_type(o, arg);                   \
195         BSET_OBJ_STOREX(o);                     \
196     } STMT_END
197
198 #define BSET_newopn(o, arg) STMT_START {        \
199         OP *oldop = o;                          \
200         BSET_newop(o, arg);                     \
201         oldop->op_next = o;                     \
202     } STMT_END
203
204 #define BSET_ret(foo) STMT_START {              \
205         Safefree(bstate->bs_obj_list);          \
206         return 0;                               \
207     } STMT_END
208
209 /* 
210  * stolen from toke.c: better if that was a function.
211  * in toke.c there are also #ifdefs for dosish systems and i/o layers
212  */
213
214 #if defined(HAS_FCNTL) && defined(F_SETFD)
215 #define set_clonex(fp)                          \
216         STMT_START {                            \
217             int fd = PerlIO_fileno(fp);         \
218             fcntl(fd,F_SETFD,fd >= 3);          \
219         } STMT_END
220 #else
221 #define set_clonex(fp)
222 #endif
223
224 #define BSET_data(dummy,arg)                                            \
225     STMT_START {                                                        \
226         GV *gv;                                                         \
227         char *pname = "main";                                           \
228         if (arg == 'D')                                                 \
229             pname = HvNAME(PL_curstash ? PL_curstash : PL_defstash);    \
230         gv = gv_fetchpv(Perl_form(aTHX_ "%s::DATA", pname), TRUE, SVt_PVIO);\
231         GvMULTI_on(gv);                                                 \
232         if (!GvIO(gv))                                                  \
233             GvIOp(gv) = newIO();                                        \
234         IoIFP(GvIOp(gv)) = PL_rsfp;                                     \
235         set_clonex(PL_rsfp);                                            \
236         /* Mark this internal pseudo-handle as clean */                 \
237         IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;                              \
238         if (PL_preprocess)                                              \
239             IoTYPE(GvIOp(gv)) = IoTYPE_PIPE;                            \
240         else if ((PerlIO*)PL_rsfp == PerlIO_stdin())                    \
241             IoTYPE(GvIOp(gv)) = IoTYPE_STD;                             \
242         else                                                            \
243             IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;                          \
244         Safefree(bstate->bs_obj_list);                                  \
245         return 1;                                                       \
246     } STMT_END
247
248 /* stolen from op.c */
249 #define BSET_load_glob(foo, gv)                                         \
250     STMT_START {                                                        \
251         GV *glob_gv;                                                    \
252         ENTER;                                                          \
253         Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,                   \
254                 newSVpvn("File::Glob", 10), Nullsv, Nullsv, Nullsv);    \
255         glob_gv = gv_fetchpv("File::Glob::csh_glob", FALSE, SVt_PVCV);  \
256         GvCV(gv) = GvCV(glob_gv);                                       \
257         SvREFCNT_inc((SV*)GvCV(gv));                                    \
258         GvIMPORTED_CV_on(gv);                                           \
259         LEAVE;                                                          \
260     } STMT_END
261
262 /*
263  * Kludge special-case workaround for OP_MAPSTART
264  * which needs the ppaddr for OP_GREPSTART. Blech.
265  */
266 #define BSET_op_type(o, arg) STMT_START {       \
267         o->op_type = arg;                       \
268         if (arg == OP_MAPSTART)                 \
269             arg = OP_GREPSTART;                 \
270         o->op_ppaddr = PL_ppaddr[arg];          \
271     } STMT_END
272 #define BSET_op_ppaddr(o, arg) Perl_croak(aTHX_ "op_ppaddr not yet implemented")
273 #define BSET_curpad(pad, arg) STMT_START {      \
274         PL_comppad = (AV *)arg;                 \
275         pad = AvARRAY(arg);                     \
276     } STMT_END
277
278 #ifdef USE_ITHREADS
279 #define BSET_cop_file(cop, arg)         CopFILE_set(cop,arg)
280 #define BSET_cop_stashpv(cop, arg)      CopSTASHPV_set(cop,arg)
281 #else
282 /* this works now that Sarathy's changed the CopFILE_set macro to do the SvREFCNT_inc()
283         -- BKS 6-2-2000 */
284 /* that really meant the actual CopFILEGV_set */
285 #define BSET_cop_filegv(cop, arg)       CopFILEGV_set(cop,arg)
286 #define BSET_cop_stash(cop,arg)         CopSTASH_set(cop,(HV*)arg)
287 #endif
288
289 /* this is simply stolen from the code in newATTRSUB() */
290 #define BSET_push_begin(ary,cv)                         \
291         STMT_START {                                    \
292             I32 oldscope = PL_scopestack_ix;            \
293             ENTER;                                      \
294             SAVECOPFILE(&PL_compiling);                 \
295             SAVECOPLINE(&PL_compiling);                 \
296             if (!PL_beginav)                            \
297                 PL_beginav = newAV();                   \
298             av_push(PL_beginav, (SV*)cv);               \
299             GvCV(CvGV(cv)) = 0;               /* cv has been hijacked */\
300             call_list(oldscope, PL_beginav);            \
301             PL_curcop = &PL_compiling;                  \
302             PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);\
303             LEAVE;                                      \
304         } STMT_END
305 #define BSET_push_init(ary,cv)                          \
306         STMT_START {                                    \
307             av_unshift((PL_initav ? PL_initav :         \
308                 (PL_initav = newAV(), PL_initav)), 1);  \
309             av_store(PL_initav, 0, cv);                 \
310         } STMT_END
311 #define BSET_push_end(ary,cv)                           \
312         STMT_START {                                    \
313             av_unshift((PL_endav ? PL_endav :           \
314             (PL_endav = newAV(), PL_endav)), 1);        \
315             av_store(PL_endav, 0, cv);                  \
316         } STMT_END
317 #define BSET_OBJ_STORE(obj, ix)                 \
318         ((I32)ix > bstate->bs_obj_list_fill ?   \
319          bset_obj_store(aTHX_ bstate, obj, (I32)ix) : \
320          (bstate->bs_obj_list[ix] = obj),       \
321          bstate->bs_ix = ix+1)
322 #define BSET_OBJ_STOREX(obj)                    \
323         (bstate->bs_ix > bstate->bs_obj_list_fill ?     \
324          bset_obj_store(aTHX_ bstate, obj, bstate->bs_ix) : \
325          (bstate->bs_obj_list[bstate->bs_ix] = obj),    \
326          bstate->bs_ix++)
327
328 #define BSET_signal(cv, name)                                           \
329         mg_set(*hv_store(GvHV(gv_fetchpv("SIG", TRUE, SVt_PVHV)),       \
330                 name, strlen(name), cv, 0))
331
332 /* NOTE: the bytecode header only sanity-checks the bytecode. If a script cares about
333  * what version of Perl it's being called under, it should do a 'use 5.006_001' or
334  * equivalent. However, since the header includes checks requiring an exact match in
335  * ByteLoader versions (we can't guarantee forward compatibility), you don't 
336  * need to specify one:
337  *      use ByteLoader;
338  * is all you need.
339  *      -- BKS, June 2000
340 */
341
342 #define HEADER_FAIL(f)  \
343         Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f)
344 #define HEADER_FAIL1(f, arg1)   \
345         Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1)
346 #define HEADER_FAIL2(f, arg1, arg2)     \
347         Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1, arg2)
348
349 #define BYTECODE_HEADER_CHECK                                   \
350         STMT_START {                                            \
351             U32 sz = 0;                                         \
352             strconst str;                                       \
353                                                                 \
354             BGET_U32(sz); /* Magic: 'PLBC' */                   \
355             if (sz != 0x43424c50) {                             \
356                 HEADER_FAIL1("bad magic (want 0x43424c50, got %#x)", (int)sz);          \
357             }                                                   \
358             BGET_strconst(str); /* archname */                  \
359             if (strNE(str, ARCHNAME)) {                         \
360                 HEADER_FAIL2("wrong architecture (want %s, you have %s)",str,ARCHNAME); \
361             }                                                   \
362             BGET_strconst(str); /* ByteLoader version */        \
363             if (strNE(str, VERSION)) {                          \
364                 HEADER_FAIL2("mismatched ByteLoader versions (want %s, you have %s)",   \
365                         str, VERSION);                          \
366             }                                                   \
367             BGET_U32(sz); /* ivsize */                          \
368             if (sz != IVSIZE) {                                 \
369                 HEADER_FAIL("different IVSIZE");                \
370             }                                                   \
371             BGET_U32(sz); /* ptrsize */                         \
372             if (sz != PTRSIZE) {                                \
373                 HEADER_FAIL("different PTRSIZE");               \
374             }                                                   \
375             BGET_strconst(str); /* byteorder */                 \
376             if (strNE(str, STRINGIFY(BYTEORDER))) {             \
377                 HEADER_FAIL("different byteorder");     \
378             }                                                   \
379         } STMT_END