Commit | Line | Data |
e8edd1e6 |
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; |
059a8bb7 |
8 | typedef 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 | |
16 | #define BGET_U8(arg) arg = BGET_FGETC() |
e8edd1e6 |
17 | #define BGET_U16(arg) \ |
059a8bb7 |
18 | BGET_FREAD(&arg, sizeof(U16), 1) |
1df34986 |
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) |
e8edd1e6 |
26 | |
059a8bb7 |
27 | #define BGET_PV(arg) STMT_START { \ |
28 | BGET_U32(arg); \ |
29 | if (arg) { \ |
30 | New(666, bstate->bs_pv.xpv_pv, arg, char); \ |
1df34986 |
31 | bl_read(bstate->bs_fdata, (void*)bstate->bs_pv.xpv_pv, arg, 1);\ |
059a8bb7 |
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 | } \ |
e8edd1e6 |
39 | } STMT_END |
40 | |
fc290457 |
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) \ |
e8edd1e6 |
55 | do { arg = BGET_FGETC(); } while (arg != '\n' && arg != EOF) |
fc290457 |
56 | #endif |
e8edd1e6 |
57 | |
78c6a0b0 |
58 | |
059a8bb7 |
59 | #define BGET_op_tr_array(arg) do { \ |
1df34986 |
60 | unsigned short *ary, len; \ |
61 | BGET_U16(len); \ |
62 | New(666, ary, len, unsigned short); \ |
63 | BGET_FREAD(ary, sizeof(unsigned short), len); \ |
059a8bb7 |
64 | arg = (char *) ary; \ |
e8edd1e6 |
65 | } while (0) |
66 | |
059a8bb7 |
67 | #define BGET_pvcontents(arg) arg = bstate->bs_pv.xpv_pv |
e8edd1e6 |
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 | |
cad2e5aa |
73 | #define BGET_NV(arg) STMT_START { \ |
e8edd1e6 |
74 | char *str; \ |
75 | BGET_strconst(str); \ |
ac26f109 |
76 | arg = Atof(str); \ |
e8edd1e6 |
77 | } STMT_END |
78 | |
79 | #define BGET_objindex(arg, type) STMT_START { \ |
e8edd1e6 |
80 | BGET_U32(ix); \ |
059a8bb7 |
81 | arg = (type)bstate->bs_obj_list[ix]; \ |
e8edd1e6 |
82 | } STMT_END |
83 | #define BGET_svindex(arg) BGET_objindex(arg, svindex) |
84 | #define BGET_opindex(arg) BGET_objindex(arg, opindex) |
059a8bb7 |
85 | #define BGET_pvindex(arg) STMT_START { \ |
86 | BGET_objindex(arg, pvindex); \ |
87 | arg = arg ? savepv(arg) : arg; \ |
88 | } STMT_END |
e8edd1e6 |
89 | |
90 | #define BSET_ldspecsv(sv, arg) sv = specialsv_list[arg] |
059a8bb7 |
91 | #define BSET_stpv(pv, arg) STMT_START { \ |
92 | BSET_OBJ_STORE(pv, arg); \ |
93 | SAVEFREEPV(pv); \ |
94 | } STMT_END |
e8edd1e6 |
95 | |
96 | #define BSET_sv_refcnt_add(svrefcnt, arg) svrefcnt += arg |
97 | #define BSET_gp_refcnt_add(gprefcnt, arg) gprefcnt += arg |
98 | #define BSET_gp_share(sv, arg) STMT_START { \ |
99 | gp_free((GV*)sv); \ |
100 | GvGP(sv) = GvGP(arg); \ |
101 | } STMT_END |
102 | |
103 | #define BSET_gv_fetchpv(sv, arg) sv = (SV*)gv_fetchpv(arg, TRUE, SVt_PV) |
104 | #define BSET_gv_stashpv(sv, arg) sv = (SV*)gv_stashpv(arg, TRUE) |
105 | #define BSET_sv_magic(sv, arg) sv_magic(sv, Nullsv, arg, 0, 0) |
1df34986 |
106 | #define BSET_mg_name(mg, arg) mg->mg_ptr = arg; mg->mg_len = bstate->bs_pv.xpv_cur |
107 | #define BSET_mg_namex(mg, arg) \ |
108 | (mg->mg_ptr = (char*)SvREFCNT_inc((SV*)arg), \ |
109 | mg->mg_len = HEf_SVKEY) |
e8edd1e6 |
110 | #define BSET_sv_upgrade(sv, arg) (void)SvUPGRADE(sv, arg) |
111 | #define BSET_xpv(sv) do { \ |
059a8bb7 |
112 | SvPV_set(sv, bstate->bs_pv.xpv_pv); \ |
113 | SvCUR_set(sv, bstate->bs_pv.xpv_cur); \ |
114 | SvLEN_set(sv, bstate->bs_pv.xpv_len); \ |
e8edd1e6 |
115 | } while (0) |
116 | #define BSET_av_extend(sv, arg) av_extend((AV*)sv, arg) |
117 | |
118 | #define BSET_av_push(sv, arg) av_push((AV*)sv, arg) |
1df34986 |
119 | #define BSET_av_pushx(sv, arg) (AvARRAY(sv)[++AvFILLp(sv)] = arg) |
e8edd1e6 |
120 | #define BSET_hv_store(sv, arg) \ |
059a8bb7 |
121 | hv_store((HV*)sv, bstate->bs_pv.xpv_pv, bstate->bs_pv.xpv_cur, arg, 0) |
e8edd1e6 |
122 | #define BSET_pv_free(pv) Safefree(pv.xpv_pv) |
1df34986 |
123 | |
124 | |
125 | #ifdef USE_ITHREADS |
126 | |
127 | /* copied after the code in newPMOP() */ |
e8edd1e6 |
128 | #define BSET_pregcomp(o, arg) \ |
1df34986 |
129 | STMT_START { \ |
130 | SV* repointer; \ |
131 | REGEXP* rx = arg ? \ |
132 | CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv_cur, cPMOPx(o)) : \ |
133 | Null(REGEXP*); \ |
134 | if(av_len((AV*) PL_regex_pad[0]) > -1) { \ |
135 | repointer = av_pop((AV*)PL_regex_pad[0]); \ |
136 | cPMOPx(o)->op_pmoffset = SvIV(repointer); \ |
137 | SvREPADTMP_off(repointer); \ |
138 | sv_setiv(repointer,PTR2IV(rx)); \ |
139 | } else { \ |
140 | repointer = newSViv(PTR2IV(rx)); \ |
141 | av_push(PL_regex_padav,SvREFCNT_inc(repointer)); \ |
142 | cPMOPx(o)->op_pmoffset = av_len(PL_regex_padav); \ |
143 | PL_regex_pad = AvARRAY(PL_regex_padav); \ |
144 | } \ |
145 | } STMT_END |
146 | |
147 | #else |
148 | #define BSET_pregcomp(o, arg) \ |
149 | STMT_START { \ |
150 | PM_SETRE(((PMOP*)o), (arg ? \ |
151 | CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv_cur, cPMOPx(o)): \ |
152 | Null(REGEXP*))); \ |
153 | } STMT_END |
154 | |
155 | #endif /* USE_THREADS */ |
156 | |
157 | |
059a8bb7 |
158 | #define BSET_newsv(sv, arg) \ |
159 | STMT_START { \ |
160 | sv = (arg == SVt_PVAV ? (SV*)newAV() : \ |
161 | arg == SVt_PVHV ? (SV*)newHV() : \ |
162 | NEWSV(666,0)); \ |
163 | SvUPGRADE(sv, arg); \ |
164 | } STMT_END |
1df34986 |
165 | #define BSET_newop(o, arg) \ |
166 | ((o = (OP*)safemalloc(arg)), memzero((char*)o,arg)) |
e8edd1e6 |
167 | #define BSET_newopn(o, arg) STMT_START { \ |
168 | OP *oldop = o; \ |
169 | BSET_newop(o, arg); \ |
170 | oldop->op_next = o; \ |
171 | } STMT_END |
172 | |
1df34986 |
173 | #define BSET_ret(foo) STMT_START { \ |
174 | Safefree(bstate->bs_obj_list); \ |
175 | return 0; \ |
176 | } STMT_END |
177 | |
178 | /* |
179 | * stolen from toke.c: better if that was a function. |
180 | * in toke.c there are also #ifdefs for dosish systems and i/o layers |
181 | */ |
182 | |
183 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
184 | #define set_clonex(fp) \ |
185 | STMT_START { \ |
186 | int fd = PerlIO_fileno(fp); \ |
187 | fcntl(fd,F_SETFD,fd >= 3); \ |
188 | } STMT_END |
189 | #else |
190 | #define set_clonex(fp) |
191 | #endif |
192 | |
193 | #define BSET_data(dummy,arg) \ |
194 | STMT_START { \ |
195 | GV *gv; \ |
196 | char *pname = "main"; \ |
197 | if (arg == 'D') \ |
198 | pname = HvNAME(PL_curstash ? PL_curstash : PL_defstash); \ |
199 | gv = gv_fetchpv(Perl_form(aTHX_ "%s::DATA", pname), TRUE, SVt_PVIO);\ |
200 | GvMULTI_on(gv); \ |
201 | if (!GvIO(gv)) \ |
202 | GvIOp(gv) = newIO(); \ |
203 | IoIFP(GvIOp(gv)) = PL_rsfp; \ |
204 | set_clonex(PL_rsfp); \ |
205 | /* Mark this internal pseudo-handle as clean */ \ |
206 | IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT; \ |
207 | if (PL_preprocess) \ |
208 | IoTYPE(GvIOp(gv)) = IoTYPE_PIPE; \ |
209 | else if ((PerlIO*)PL_rsfp == PerlIO_stdin()) \ |
210 | IoTYPE(GvIOp(gv)) = IoTYPE_STD; \ |
211 | else \ |
212 | IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY; \ |
213 | Safefree(bstate->bs_obj_list); \ |
214 | return 1; \ |
215 | } STMT_END |
216 | |
217 | /* stolen from op.c */ |
218 | #define BSET_load_glob(foo, gv) \ |
219 | STMT_START { \ |
220 | GV *glob_gv; \ |
221 | ENTER; \ |
222 | Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, \ |
223 | newSVpvn("File::Glob", 10), Nullsv, Nullsv, Nullsv); \ |
224 | glob_gv = gv_fetchpv("File::Glob::csh_glob", FALSE, SVt_PVCV); \ |
225 | GvCV(gv) = GvCV(glob_gv); \ |
226 | SvREFCNT_inc((SV*)GvCV(gv)); \ |
227 | GvIMPORTED_CV_on(gv); \ |
228 | LEAVE; \ |
059a8bb7 |
229 | } STMT_END |
e8edd1e6 |
230 | |
231 | /* |
232 | * Kludge special-case workaround for OP_MAPSTART |
233 | * which needs the ppaddr for OP_GREPSTART. Blech. |
234 | */ |
235 | #define BSET_op_type(o, arg) STMT_START { \ |
236 | o->op_type = arg; \ |
237 | if (arg == OP_MAPSTART) \ |
238 | arg = OP_GREPSTART; \ |
239 | o->op_ppaddr = PL_ppaddr[arg]; \ |
240 | } STMT_END |
cea2e8a9 |
241 | #define BSET_op_ppaddr(o, arg) Perl_croak(aTHX_ "op_ppaddr not yet implemented") |
e8edd1e6 |
242 | #define BSET_curpad(pad, arg) STMT_START { \ |
243 | PL_comppad = (AV *)arg; \ |
244 | pad = AvARRAY(arg); \ |
245 | } STMT_END |
1df34986 |
246 | |
247 | #ifdef USE_ITHREADS |
11faa288 |
248 | #define BSET_cop_file(cop, arg) CopFILE_set(cop,arg) |
11faa288 |
249 | #define BSET_cop_stashpv(cop, arg) CopSTASHPV_set(cop,arg) |
1df34986 |
250 | #else |
251 | /* this works now that Sarathy's changed the CopFILE_set macro to do the SvREFCNT_inc() |
252 | -- BKS 6-2-2000 */ |
253 | /* that really meant the actual CopFILEGV_set */ |
254 | #define BSET_cop_filegv(cop, arg) CopFILEGV_set(cop,arg) |
255 | #define BSET_cop_stash(cop,arg) CopSTASH_set(cop,(HV*)arg) |
256 | #endif |
e8edd1e6 |
257 | |
059a8bb7 |
258 | /* this is simply stolen from the code in newATTRSUB() */ |
259 | #define BSET_push_begin(ary,cv) \ |
260 | STMT_START { \ |
1df34986 |
261 | I32 oldscope = PL_scopestack_ix; \ |
262 | ENTER; \ |
263 | SAVECOPFILE(&PL_compiling); \ |
264 | SAVECOPLINE(&PL_compiling); \ |
265 | if (!PL_beginav) \ |
266 | PL_beginav = newAV(); \ |
267 | av_push(PL_beginav, (SV*)cv); \ |
268 | GvCV(CvGV(cv)) = 0; /* cv has been hijacked */\ |
269 | call_list(oldscope, PL_beginav); \ |
270 | PL_curcop = &PL_compiling; \ |
271 | PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);\ |
272 | LEAVE; \ |
059a8bb7 |
273 | } STMT_END |
1df34986 |
274 | #define BSET_push_init(ary,cv) \ |
275 | STMT_START { \ |
276 | av_unshift((PL_initav ? PL_initav : \ |
277 | (PL_initav = newAV(), PL_initav)), 1); \ |
278 | av_store(PL_initav, 0, cv); \ |
059a8bb7 |
279 | } STMT_END |
1df34986 |
280 | #define BSET_push_end(ary,cv) \ |
281 | STMT_START { \ |
282 | av_unshift((PL_endav ? PL_endav : \ |
283 | (PL_endav = newAV(), PL_endav)), 1); \ |
284 | av_store(PL_endav, 0, cv); \ |
059a8bb7 |
285 | } STMT_END |
286 | #define BSET_OBJ_STORE(obj, ix) \ |
287 | (I32)ix > bstate->bs_obj_list_fill ? \ |
1df34986 |
288 | bset_obj_store(aTHX_ bstate, obj, (I32)ix) : \ |
289 | (bstate->bs_obj_list[ix] = obj) |
290 | |
291 | #define BSET_signal(cv, name) \ |
292 | mg_set(*hv_store(GvHV(gv_fetchpv("SIG", TRUE, SVt_PVHV)), \ |
293 | name, strlen(name), cv, 0)) |
059a8bb7 |
294 | |
295 | /* NOTE: the bytecode header only sanity-checks the bytecode. If a script cares about |
3b825e41 |
296 | * what version of Perl it's being called under, it should do a 'use 5.006_001' or |
059a8bb7 |
297 | * equivalent. However, since the header includes checks requiring an exact match in |
298 | * ByteLoader versions (we can't guarantee forward compatibility), you don't |
299 | * need to specify one: |
300 | * use ByteLoader; |
301 | * is all you need. |
302 | * -- BKS, June 2000 |
303 | */ |
304 | |
d2560b70 |
305 | #define HEADER_FAIL(f) \ |
306 | Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f) |
307 | #define HEADER_FAIL1(f, arg1) \ |
308 | Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1) |
309 | #define HEADER_FAIL2(f, arg1, arg2) \ |
059a8bb7 |
310 | Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1, arg2) |
311 | |
312 | #define BYTECODE_HEADER_CHECK \ |
313 | STMT_START { \ |
314 | U32 sz = 0; \ |
315 | strconst str; \ |
316 | \ |
317 | BGET_U32(sz); /* Magic: 'PLBC' */ \ |
318 | if (sz != 0x43424c50) { \ |
d2560b70 |
319 | HEADER_FAIL1("bad magic (want 0x43424c50, got %#x)", (int)sz); \ |
059a8bb7 |
320 | } \ |
321 | BGET_strconst(str); /* archname */ \ |
322 | if (strNE(str, ARCHNAME)) { \ |
d2560b70 |
323 | HEADER_FAIL2("wrong architecture (want %s, you have %s)",str,ARCHNAME); \ |
059a8bb7 |
324 | } \ |
325 | BGET_strconst(str); /* ByteLoader version */ \ |
326 | if (strNE(str, VERSION)) { \ |
d2560b70 |
327 | HEADER_FAIL2("mismatched ByteLoader versions (want %s, you have %s)", \ |
059a8bb7 |
328 | str, VERSION); \ |
329 | } \ |
330 | BGET_U32(sz); /* ivsize */ \ |
331 | if (sz != IVSIZE) { \ |
d2560b70 |
332 | HEADER_FAIL("different IVSIZE"); \ |
059a8bb7 |
333 | } \ |
334 | BGET_U32(sz); /* ptrsize */ \ |
335 | if (sz != PTRSIZE) { \ |
d2560b70 |
336 | HEADER_FAIL("different PTRSIZE"); \ |
059a8bb7 |
337 | } \ |
338 | BGET_strconst(str); /* byteorder */ \ |
339 | if (strNE(str, STRINGIFY(BYTEORDER))) { \ |
d2560b70 |
340 | HEADER_FAIL("different byteorder"); \ |
059a8bb7 |
341 | } \ |
059a8bb7 |
342 | } STMT_END |