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