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