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