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