fixes for various warnings identified by Visual C++
[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 9typedef IV IV64;
10
11#define BGET_FREAD(argp, len, nelem) \
059a8bb7 12 bl_read(bstate->bs_fdata,(char*)(argp),(len),(nelem))
13#define BGET_FGETC() bl_getc(bstate->bs_fdata)
e8edd1e6 14
15#define BGET_U32(arg) \
059a8bb7 16 BGET_FREAD(&arg, sizeof(U32), 1)
e8edd1e6 17#define BGET_I32(arg) \
059a8bb7 18 BGET_FREAD(&arg, sizeof(I32), 1)
e8edd1e6 19#define BGET_U16(arg) \
059a8bb7 20 BGET_FREAD(&arg, sizeof(U16), 1)
e8edd1e6 21#define BGET_U8(arg) arg = BGET_FGETC()
22
059a8bb7 23#define BGET_PV(arg) STMT_START { \
24 BGET_U32(arg); \
25 if (arg) { \
26 New(666, bstate->bs_pv.xpv_pv, arg, char); \
27 bl_read(bstate->bs_fdata, (void*)bstate->bs_pv.xpv_pv, arg, 1); \
28 bstate->bs_pv.xpv_len = arg; \
29 bstate->bs_pv.xpv_cur = arg - 1; \
30 } else { \
31 bstate->bs_pv.xpv_pv = 0; \
32 bstate->bs_pv.xpv_len = 0; \
33 bstate->bs_pv.xpv_cur = 0; \
34 } \
e8edd1e6 35 } STMT_END
36
fc290457 37#ifdef BYTELOADER_LOG_COMMENTS
38# define BGET_comment_t(arg) \
39 STMT_START { \
40 char buf[1024]; \
41 int i = 0; \
42 do { \
43 arg = BGET_FGETC(); \
44 buf[i++] = (char)arg; \
45 } while (arg != '\n' && arg != EOF); \
46 buf[i] = '\0'; \
47 PerlIO_printf(PerlIO_stderr(), "%s", buf); \
48 } STMT_END
49#else
50# define BGET_comment_t(arg) \
e8edd1e6 51 do { arg = BGET_FGETC(); } while (arg != '\n' && arg != EOF)
fc290457 52#endif
e8edd1e6 53
54/*
55 * In the following, sizeof(IV)*4 is just a way of encoding 32 on 64-bit-IV
56 * machines such that 32-bit machine compilers don't whine about the shift
57 * count being too high even though the code is never reached there.
58 */
59#define BGET_IV64(arg) STMT_START { \
60 U32 hi, lo; \
61 BGET_U32(hi); \
62 BGET_U32(lo); \
63 if (sizeof(IV) == 8) \
42718184 64 arg = ((IV)hi << (sizeof(IV)*4) | (IV)lo); \
e8edd1e6 65 else if (((I32)hi == -1 && (I32)lo < 0) \
66 || ((I32)hi == 0 && (I32)lo >= 0)) { \
67 arg = (I32)lo; \
68 } \
69 else { \
059a8bb7 70 bstate->bs_iv_overflows++; \
e8edd1e6 71 arg = 0; \
72 } \
73 } STMT_END
74
78c6a0b0 75#if IVSIZE == 4
76# define BGET_IV(arg) BGET_I32(arg)
77#else
78# if IVSIZE == 8
79# define BGET_IV(arg) BGET_IV64(arg)
80# endif
81#endif
82
059a8bb7 83#define BGET_op_tr_array(arg) do { \
84 unsigned short *ary; \
059a8bb7 85 New(666, ary, 256, unsigned short); \
86 BGET_FREAD(ary, sizeof(unsigned short), 256); \
87 arg = (char *) ary; \
e8edd1e6 88 } while (0)
89
059a8bb7 90#define BGET_pvcontents(arg) arg = bstate->bs_pv.xpv_pv
e8edd1e6 91#define BGET_strconst(arg) STMT_START { \
92 for (arg = PL_tokenbuf; (*arg = BGET_FGETC()); arg++) /* nothing */; \
93 arg = PL_tokenbuf; \
94 } STMT_END
95
cad2e5aa 96#define BGET_NV(arg) STMT_START { \
e8edd1e6 97 char *str; \
98 BGET_strconst(str); \
ac26f109 99 arg = Atof(str); \
e8edd1e6 100 } STMT_END
101
102#define BGET_objindex(arg, type) STMT_START { \
e8edd1e6 103 BGET_U32(ix); \
059a8bb7 104 arg = (type)bstate->bs_obj_list[ix]; \
e8edd1e6 105 } STMT_END
106#define BGET_svindex(arg) BGET_objindex(arg, svindex)
107#define BGET_opindex(arg) BGET_objindex(arg, opindex)
059a8bb7 108#define BGET_pvindex(arg) STMT_START { \
109 BGET_objindex(arg, pvindex); \
110 arg = arg ? savepv(arg) : arg; \
111 } STMT_END
e8edd1e6 112
113#define BSET_ldspecsv(sv, arg) sv = specialsv_list[arg]
059a8bb7 114#define BSET_stpv(pv, arg) STMT_START { \
115 BSET_OBJ_STORE(pv, arg); \
116 SAVEFREEPV(pv); \
117 } STMT_END
e8edd1e6 118
119#define BSET_sv_refcnt_add(svrefcnt, arg) svrefcnt += arg
120#define BSET_gp_refcnt_add(gprefcnt, arg) gprefcnt += arg
121#define BSET_gp_share(sv, arg) STMT_START { \
122 gp_free((GV*)sv); \
123 GvGP(sv) = GvGP(arg); \
124 } STMT_END
125
126#define BSET_gv_fetchpv(sv, arg) sv = (SV*)gv_fetchpv(arg, TRUE, SVt_PV)
127#define BSET_gv_stashpv(sv, arg) sv = (SV*)gv_stashpv(arg, TRUE)
128#define BSET_sv_magic(sv, arg) sv_magic(sv, Nullsv, arg, 0, 0)
059a8bb7 129#define BSET_mg_pv(mg, arg) mg->mg_ptr = arg; mg->mg_len = bstate->bs_pv.xpv_cur
e8edd1e6 130#define BSET_sv_upgrade(sv, arg) (void)SvUPGRADE(sv, arg)
131#define BSET_xpv(sv) do { \
059a8bb7 132 SvPV_set(sv, bstate->bs_pv.xpv_pv); \
133 SvCUR_set(sv, bstate->bs_pv.xpv_cur); \
134 SvLEN_set(sv, bstate->bs_pv.xpv_len); \
e8edd1e6 135 } while (0)
136#define BSET_av_extend(sv, arg) av_extend((AV*)sv, arg)
137
138#define BSET_av_push(sv, arg) av_push((AV*)sv, arg)
139#define BSET_hv_store(sv, arg) \
059a8bb7 140 hv_store((HV*)sv, bstate->bs_pv.xpv_pv, bstate->bs_pv.xpv_cur, arg, 0)
e8edd1e6 141#define BSET_pv_free(pv) Safefree(pv.xpv_pv)
142#define BSET_pregcomp(o, arg) \
e357fc91 143 STMT_START { \
144 PM_SETRE(((PMOP*)o), (arg ? \
145 CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv_cur, ((PMOP*)o)) : 0)); \
146 } STMT_END
059a8bb7 147#define BSET_newsv(sv, arg) \
148 STMT_START { \
149 sv = (arg == SVt_PVAV ? (SV*)newAV() : \
150 arg == SVt_PVHV ? (SV*)newHV() : \
151 NEWSV(666,0)); \
152 SvUPGRADE(sv, arg); \
153 } STMT_END
fc290457 154#define BSET_newop(o, arg) ((o = (OP*)safemalloc(optype_size[arg])), \
155 memzero((char*)o,optype_size[arg]))
e8edd1e6 156#define BSET_newopn(o, arg) STMT_START { \
157 OP *oldop = o; \
158 BSET_newop(o, arg); \
159 oldop->op_next = o; \
160 } STMT_END
161
059a8bb7 162#define BSET_ret(foo) STMT_START { \
163 Safefree(bstate->bs_obj_list); \
164 return; \
165 } STMT_END
e8edd1e6 166
167/*
168 * Kludge special-case workaround for OP_MAPSTART
169 * which needs the ppaddr for OP_GREPSTART. Blech.
170 */
171#define BSET_op_type(o, arg) STMT_START { \
172 o->op_type = arg; \
173 if (arg == OP_MAPSTART) \
174 arg = OP_GREPSTART; \
175 o->op_ppaddr = PL_ppaddr[arg]; \
176 } STMT_END
cea2e8a9 177#define BSET_op_ppaddr(o, arg) Perl_croak(aTHX_ "op_ppaddr not yet implemented")
e8edd1e6 178#define BSET_curpad(pad, arg) STMT_START { \
179 PL_comppad = (AV *)arg; \
180 pad = AvARRAY(arg); \
181 } STMT_END
059a8bb7 182/* this works now that Sarathy's changed the CopFILE_set macro to do the SvREFCNT_inc()
183 -- BKS 6-2-2000 */
11faa288 184#define BSET_cop_file(cop, arg) CopFILE_set(cop,arg)
185#define BSET_cop_line(cop, arg) CopLINE_set(cop,arg)
186#define BSET_cop_stashpv(cop, arg) CopSTASHPV_set(cop,arg)
e8edd1e6 187
059a8bb7 188/* this is simply stolen from the code in newATTRSUB() */
189#define BSET_push_begin(ary,cv) \
190 STMT_START { \
191 I32 oldscope = PL_scopestack_ix; \
192 ENTER; \
193 SAVECOPFILE(&PL_compiling); \
194 SAVECOPLINE(&PL_compiling); \
059a8bb7 195 if (!PL_beginav) \
196 PL_beginav = newAV(); \
197 av_push(PL_beginav, cv); \
198 call_list(oldscope, PL_beginav); \
199 PL_curcop = &PL_compiling; \
6e21dc91 200 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);\
059a8bb7 201 LEAVE; \
202 } STMT_END
203#define BSET_push_init(ary,cv) \
204 STMT_START { \
205 av_unshift((PL_initav ? PL_initav : (PL_initav = newAV(), PL_initav)), 1); \
206 av_store(PL_initav, 0, cv); \
207 } STMT_END
208#define BSET_push_end(ary,cv) \
209 STMT_START { \
210 av_unshift((PL_endav ? PL_endav : (PL_endav = newAV(), PL_endav)), 1); \
211 av_store(PL_endav, 0, cv); \
212 } STMT_END
213#define BSET_OBJ_STORE(obj, ix) \
214 (I32)ix > bstate->bs_obj_list_fill ? \
acfe0abc 215 bset_obj_store(aTHX_ bstate, obj, (I32)ix) : (bstate->bs_obj_list[ix] = obj)
059a8bb7 216
217/* NOTE: the bytecode header only sanity-checks the bytecode. If a script cares about
3b825e41 218 * what version of Perl it's being called under, it should do a 'use 5.006_001' or
059a8bb7 219 * equivalent. However, since the header includes checks requiring an exact match in
220 * ByteLoader versions (we can't guarantee forward compatibility), you don't
221 * need to specify one:
222 * use ByteLoader;
223 * is all you need.
224 * -- BKS, June 2000
225*/
226
d2560b70 227#define HEADER_FAIL(f) \
228 Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f)
229#define HEADER_FAIL1(f, arg1) \
230 Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1)
231#define HEADER_FAIL2(f, arg1, arg2) \
059a8bb7 232 Perl_croak(aTHX_ "Invalid bytecode for this architecture: " f, arg1, arg2)
233
234#define BYTECODE_HEADER_CHECK \
235 STMT_START { \
236 U32 sz = 0; \
237 strconst str; \
238 \
239 BGET_U32(sz); /* Magic: 'PLBC' */ \
240 if (sz != 0x43424c50) { \
d2560b70 241 HEADER_FAIL1("bad magic (want 0x43424c50, got %#x)", (int)sz); \
059a8bb7 242 } \
243 BGET_strconst(str); /* archname */ \
244 if (strNE(str, ARCHNAME)) { \
d2560b70 245 HEADER_FAIL2("wrong architecture (want %s, you have %s)",str,ARCHNAME); \
059a8bb7 246 } \
247 BGET_strconst(str); /* ByteLoader version */ \
248 if (strNE(str, VERSION)) { \
d2560b70 249 HEADER_FAIL2("mismatched ByteLoader versions (want %s, you have %s)", \
059a8bb7 250 str, VERSION); \
251 } \
252 BGET_U32(sz); /* ivsize */ \
253 if (sz != IVSIZE) { \
d2560b70 254 HEADER_FAIL("different IVSIZE"); \
059a8bb7 255 } \
256 BGET_U32(sz); /* ptrsize */ \
257 if (sz != PTRSIZE) { \
d2560b70 258 HEADER_FAIL("different PTRSIZE"); \
059a8bb7 259 } \
260 BGET_strconst(str); /* byteorder */ \
261 if (strNE(str, STRINGIFY(BYTEORDER))) { \
d2560b70 262 HEADER_FAIL("different byteorder"); \
059a8bb7 263 } \
059a8bb7 264 } STMT_END