This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
a9de88fbd9d78ff6586f3e560cd57d84c7f892fa
[perl5.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 = NEWSV(0,0);                            \
176             SvUPGRADE(sv, (arg));                       \
177         } STMT_END
178 #define BSET_newsvx(sv, arg) STMT_START {               \
179             BSET_newsv(sv, arg &  SVTYPEMASK);          \
180             SvFLAGS(sv) = arg;                          \
181             BSET_OBJ_STOREX(sv);                        \
182         } STMT_END
183 #define BSET_newop(o, arg)                              \
184         ((o = (OP*)safemalloc(arg)), memzero((char*)o,arg))
185 #define BSET_newopx(o, arg) STMT_START {        \
186         register int sz = arg & 0x7f;           \
187         register OP* new = (OP*) safemalloc(sz);\
188         memzero(new, sz);                       \
189         /* new->op_next = o; XXX */             \
190         o = new;                                \
191         arg >>=7;                               \
192         BSET_op_type(o, arg);                   \
193         BSET_OBJ_STOREX(o);                     \
194     } STMT_END
195
196 #define BSET_newopn(o, arg) STMT_START {        \
197         OP *oldop = o;                          \
198         BSET_newop(o, arg);                     \
199         oldop->op_next = o;                     \
200     } STMT_END
201
202 #define BSET_ret(foo) STMT_START {              \
203         Safefree(bstate->bs_obj_list);          \
204         return 0;                               \
205     } STMT_END
206
207 /* 
208  * stolen from toke.c: better if that was a function.
209  * in toke.c there are also #ifdefs for dosish systems and i/o layers
210  */
211
212 #if defined(HAS_FCNTL) && defined(F_SETFD)
213 #define set_clonex(fp)                          \
214         STMT_START {                            \
215             int fd = PerlIO_fileno(fp);         \
216             fcntl(fd,F_SETFD,fd >= 3);          \
217         } STMT_END
218 #else
219 #define set_clonex(fp)
220 #endif
221
222 #define BSET_data(dummy,arg)                                            \
223     STMT_START {                                                        \
224         GV *gv;                                                         \
225         char *pname = "main";                                           \
226         if (arg == 'D')                                                 \
227             pname = HvNAME(PL_curstash ? PL_curstash : PL_defstash);    \
228         gv = gv_fetchpv(Perl_form(aTHX_ "%s::DATA", pname), TRUE, SVt_PVIO);\
229         GvMULTI_on(gv);                                                 \
230         if (!GvIO(gv))                                                  \
231             GvIOp(gv) = newIO();                                        \
232         IoIFP(GvIOp(gv)) = PL_rsfp;                                     \
233         set_clonex(PL_rsfp);                                            \
234         /* Mark this internal pseudo-handle as clean */                 \
235         IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;                              \
236         if (PL_preprocess)                                              \
237             IoTYPE(GvIOp(gv)) = IoTYPE_PIPE;                            \
238         else if ((PerlIO*)PL_rsfp == PerlIO_stdin())                    \
239             IoTYPE(GvIOp(gv)) = IoTYPE_STD;                             \
240         else                                                            \
241             IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;                          \
242         Safefree(bstate->bs_obj_list);                                  \
243         return 1;                                                       \
244     } STMT_END
245
246 /* stolen from op.c */
247 #define BSET_load_glob(foo, gv)                                         \
248     STMT_START {                                                        \
249         GV *glob_gv;                                                    \
250         ENTER;                                                          \
251         Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,                   \
252                 newSVpvn("File::Glob", 10), Nullsv, Nullsv, Nullsv);    \
253         glob_gv = gv_fetchpv("File::Glob::csh_glob", FALSE, SVt_PVCV);  \
254         GvCV(gv) = GvCV(glob_gv);                                       \
255         SvREFCNT_inc((SV*)GvCV(gv));                                    \
256         GvIMPORTED_CV_on(gv);                                           \
257         LEAVE;                                                          \
258     } STMT_END
259
260 /*
261  * Kludge special-case workaround for OP_MAPSTART
262  * which needs the ppaddr for OP_GREPSTART. Blech.
263  */
264 #define BSET_op_type(o, arg) STMT_START {       \
265         o->op_type = arg;                       \
266         if (arg == OP_MAPSTART)                 \
267             arg = OP_GREPSTART;                 \
268         o->op_ppaddr = PL_ppaddr[arg];          \
269     } STMT_END
270 #define BSET_op_ppaddr(o, arg) Perl_croak(aTHX_ "op_ppaddr not yet implemented")
271 #define BSET_curpad(pad, arg) STMT_START {      \
272         PL_comppad = (AV *)arg;                 \
273         pad = AvARRAY(arg);                     \
274     } STMT_END
275
276 #ifdef USE_ITHREADS
277 #define BSET_cop_file(cop, arg)         CopFILE_set(cop,arg)
278 #define BSET_cop_stashpv(cop, arg)      CopSTASHPV_set(cop,arg)
279 #else
280 /* this works now that Sarathy's changed the CopFILE_set macro to do the SvREFCNT_inc()
281         -- BKS 6-2-2000 */
282 /* that really meant the actual CopFILEGV_set */
283 #define BSET_cop_filegv(cop, arg)       CopFILEGV_set(cop,arg)
284 #define BSET_cop_stash(cop,arg)         CopSTASH_set(cop,(HV*)arg)
285 #endif
286
287 /* this is simply stolen from the code in newATTRSUB() */
288 #define BSET_push_begin(ary,cv)                         \
289         STMT_START {                                    \
290             I32 oldscope = PL_scopestack_ix;            \
291             ENTER;                                      \
292             SAVECOPFILE(&PL_compiling);                 \
293             SAVECOPLINE(&PL_compiling);                 \
294             if (!PL_beginav)                            \
295                 PL_beginav = newAV();                   \
296             av_push(PL_beginav, (SV*)cv);               \
297             GvCV(CvGV(cv)) = 0;               /* cv has been hijacked */\
298             call_list(oldscope, PL_beginav);            \
299             PL_curcop = &PL_compiling;                  \
300             PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);\
301             LEAVE;                                      \
302         } STMT_END
303 #define BSET_push_init(ary,cv)                          \
304         STMT_START {                                    \
305             av_unshift((PL_initav ? PL_initav :         \
306                 (PL_initav = newAV(), PL_initav)), 1);  \
307             av_store(PL_initav, 0, cv);                 \
308         } STMT_END
309 #define BSET_push_end(ary,cv)                           \
310         STMT_START {                                    \
311             av_unshift((PL_endav ? PL_endav :           \
312             (PL_endav = newAV(), PL_endav)), 1);        \
313             av_store(PL_endav, 0, cv);                  \
314         } STMT_END
315 #define BSET_OBJ_STORE(obj, ix)                 \
316         ((I32)ix > bstate->bs_obj_list_fill ?   \
317          bset_obj_store(aTHX_ bstate, obj, (I32)ix) : \
318          (bstate->bs_obj_list[ix] = obj),       \
319          bstate->bs_ix = ix+1)
320 #define BSET_OBJ_STOREX(obj)                    \
321         (bstate->bs_ix > bstate->bs_obj_list_fill ?     \
322          bset_obj_store(aTHX_ bstate, obj, bstate->bs_ix) : \
323          (bstate->bs_obj_list[bstate->bs_ix] = obj),    \
324          bstate->bs_ix++)
325
326 #define BSET_signal(cv, name)                                           \
327         mg_set(*hv_store(GvHV(gv_fetchpv("SIG", TRUE, SVt_PVHV)),       \
328                 name, strlen(name), cv, 0))
329
330 /* NOTE: the bytecode header only sanity-checks the bytecode. If a script cares about
331  * what version of Perl it's being called under, it should do a 'use 5.006_001' or
332  * equivalent. However, since the header includes checks requiring an exact match in
333  * ByteLoader versions (we can't guarantee forward compatibility), you don't 
334  * need to specify one:
335  *      use ByteLoader;
336  * is all you need.
337  *      -- BKS, June 2000
338 */
339
340 #define HEADER_FAIL(f)  \
341         Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f)
342 #define HEADER_FAIL1(f, arg1)   \
343         Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1)
344 #define HEADER_FAIL2(f, arg1, arg2)     \
345         Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1, arg2)
346
347 #define BYTECODE_HEADER_CHECK                                   \
348         STMT_START {                                            \
349             U32 sz = 0;                                         \
350             strconst str;                                       \
351                                                                 \
352             BGET_U32(sz); /* Magic: 'PLBC' */                   \
353             if (sz != 0x43424c50) {                             \
354                 HEADER_FAIL1("bad magic (want 0x43424c50, got %#x)", (int)sz);          \
355             }                                                   \
356             BGET_strconst(str); /* archname */                  \
357             if (strNE(str, ARCHNAME)) {                         \
358                 HEADER_FAIL2("wrong architecture (want %s, you have %s)",str,ARCHNAME); \
359             }                                                   \
360             BGET_strconst(str); /* ByteLoader version */        \
361             if (strNE(str, VERSION)) {                          \
362                 HEADER_FAIL2("mismatched ByteLoader versions (want %s, you have %s)",   \
363                         str, VERSION);                          \
364             }                                                   \
365             BGET_U32(sz); /* ivsize */                          \
366             if (sz != IVSIZE) {                                 \
367                 HEADER_FAIL("different IVSIZE");                \
368             }                                                   \
369             BGET_U32(sz); /* ptrsize */                         \
370             if (sz != PTRSIZE) {                                \
371                 HEADER_FAIL("different PTRSIZE");               \
372             }                                                   \
373         } STMT_END