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