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