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