Commit | Line | Data |
a8a597b2 |
1 | use strict; |
2 | my %alias_to = ( |
3 | U32 => [qw(PADOFFSET STRLEN)], |
4 | I32 => [qw(SSize_t long)], |
5 | U16 => [qw(OPCODE line_t short)], |
6 | U8 => [qw(char)], |
7 | objindex => [qw(svindex opindex)] |
8 | ); |
9 | |
10 | my @optype= qw(OP UNOP BINOP LOGOP CONDOP LISTOP PMOP SVOP GVOP PVOP LOOP COP); |
11 | |
12 | # Nullsv *must* come first in the following so that the condition |
13 | # ($$sv == 0) can continue to be used to test (sv == Nullsv). |
14 | my @specialsv = qw(Nullsv &sv_undef &sv_yes &sv_no); |
15 | |
16 | my (%alias_from, $from, $tos); |
17 | while (($from, $tos) = each %alias_to) { |
18 | map { $alias_from{$_} = $from } @$tos; |
19 | } |
20 | |
21 | my $c_header = <<'EOT'; |
22 | /* |
a8581515 |
23 | * Copyright (c) 1996-1998 Malcolm Beattie |
a8a597b2 |
24 | * |
25 | * You may distribute under the terms of either the GNU General Public |
26 | * License or the Artistic License, as specified in the README file. |
27 | * |
28 | */ |
29 | /* |
30 | * This file is autogenerated from bytecode.pl. Changes made here will be lost. |
31 | */ |
32 | EOT |
33 | |
34 | my $perl_header; |
35 | ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g; |
36 | |
33b839e2 |
37 | unlink "byterun.c", "byterun.h", "ext/B/B/Asmdata.pm"; |
a8a597b2 |
38 | |
39 | # |
40 | # Start with boilerplate for Asmdata.pm |
41 | # |
33b839e2 |
42 | open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!"; |
a8a597b2 |
43 | print ASMDATA_PM $perl_header, <<'EOT'; |
44 | package B::Asmdata; |
45 | use Exporter; |
46 | @ISA = qw(Exporter); |
47 | @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name); |
48 | use vars qw(%insn_data @insn_name @optype @specialsv_name); |
49 | |
50 | EOT |
51 | print ASMDATA_PM <<"EOT"; |
52 | \@optype = qw(@optype); |
53 | \@specialsv_name = qw(@specialsv); |
54 | |
55 | # XXX insn_data is initialised this way because with a large |
56 | # %insn_data = (foo => [...], bar => [...], ...) initialiser |
57 | # I get a hard-to-track-down stack underflow and segfault. |
58 | EOT |
59 | |
60 | # |
61 | # Boilerplate for byterun.c |
62 | # |
63 | open(BYTERUN_C, ">byterun.c") or die "byterun.c: $!"; |
64 | print BYTERUN_C $c_header, <<'EOT'; |
65 | |
66 | #include "EXTERN.h" |
67 | #include "perl.h" |
d613ef02 |
68 | |
69 | void * |
70 | bset_obj_store(void *obj, I32 ix) |
71 | { |
72 | if (ix > obj_list_fill) { |
73 | if (obj_list_fill == -1) |
74 | New(666, obj_list, ix + 1, void*); |
75 | else |
76 | Renew(obj_list, ix + 1, void*); |
77 | obj_list_fill = ix; |
78 | } |
79 | obj_list[ix] = obj; |
80 | return obj; |
81 | } |
a8a597b2 |
82 | |
83 | #ifdef INDIRECT_BGET_MACROS |
33b839e2 |
84 | void byterun(struct bytestream bs) |
a8a597b2 |
85 | #else |
4b534093 |
86 | void byterun(PerlIO *fp) |
a8a597b2 |
87 | #endif /* INDIRECT_BGET_MACROS */ |
88 | { |
89 | dTHR; |
90 | int insn; |
47358472 |
91 | while ((insn = BGET_FGETC()) != EOF) { |
a8a597b2 |
92 | switch (insn) { |
93 | EOT |
94 | |
95 | |
96 | my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype); |
97 | |
98 | while (<DATA>) { |
99 | chop; |
100 | s/#.*//; # remove comments |
101 | next unless length; |
102 | if (/^%number\s+(.*)/) { |
103 | $insn_num = $1; |
104 | next; |
105 | } elsif (/%enum\s+(.*?)\s+(.*)/) { |
106 | create_enum($1, $2); # must come before instructions |
107 | next; |
108 | } |
109 | ($insn, $lvalue, $argtype, $flags) = split; |
110 | $insn_name[$insn_num] = $insn; |
111 | $fundtype = $alias_from{$argtype} || $argtype; |
112 | |
113 | # |
114 | # Add the case statement and code for the bytecode interpreter in byterun.c |
115 | # |
116 | printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n", |
117 | uc($insn), $insn_num; |
118 | my $optarg = $argtype eq "none" ? "" : ", arg"; |
119 | if ($optarg) { |
120 | printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype; |
121 | } |
122 | if ($flags =~ /x/) { |
123 | print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n"; |
124 | } elsif ($flags =~ /s/) { |
125 | # Store instructions store to obj_list[arg]. "lvalue" field is rvalue. |
126 | print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n"; |
127 | } |
128 | elsif ($optarg && $lvalue ne "none") { |
129 | print BYTERUN_C "\t\t$lvalue = arg;\n"; |
130 | } |
131 | print BYTERUN_C "\t\tbreak;\n\t }\n"; |
132 | |
133 | # |
134 | # Add the initialiser line for %insn_data in Asmdata.pm |
135 | # |
136 | print ASMDATA_PM <<"EOT"; |
137 | \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"]; |
138 | EOT |
139 | |
140 | # Find the next unused instruction number |
141 | do { $insn_num++ } while $insn_name[$insn_num]; |
142 | } |
143 | |
144 | # |
145 | # Finish off byterun.c |
146 | # |
147 | print BYTERUN_C <<'EOT'; |
148 | default: |
149 | croak("Illegal bytecode instruction %d\n", insn); |
150 | /* NOTREACHED */ |
151 | } |
152 | } |
153 | } |
154 | EOT |
155 | |
156 | # |
157 | # Write the instruction and optype enum constants into byterun.h |
158 | # |
159 | open(BYTERUN_H, ">byterun.h") or die "byterun.h: $!"; |
160 | print BYTERUN_H $c_header, <<'EOT'; |
161 | #ifdef INDIRECT_BGET_MACROS |
162 | struct bytestream { |
163 | void *data; |
164 | int (*fgetc)(void *); |
165 | int (*fread)(char *, size_t, size_t, void*); |
166 | void (*freadpv)(U32, void*); |
167 | }; |
a8a597b2 |
168 | void byterun _((struct bytestream)); |
169 | #else |
4b534093 |
170 | void byterun _((PerlIO *)); |
a8a597b2 |
171 | #endif /* INDIRECT_BGET_MACROS */ |
172 | |
d613ef02 |
173 | void *bset_obj_store _((void *, I32)); |
a8a597b2 |
174 | |
175 | enum { |
176 | EOT |
177 | |
178 | my $i = 0; |
179 | my $add_enum_value = 0; |
180 | my $max_insn; |
181 | for ($i = 0; $i < @insn_name; $i++) { |
182 | $insn = uc($insn_name[$i]); |
183 | if (defined($insn)) { |
184 | $max_insn = $i; |
185 | if ($add_enum_value) { |
186 | print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n"; |
187 | $add_enum_value = 0; |
188 | } else { |
189 | print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n"; |
190 | } |
191 | } else { |
192 | $add_enum_value = 1; |
193 | } |
194 | } |
195 | |
196 | print BYTERUN_H " MAX_INSN = $max_insn\n};\n"; |
197 | |
198 | print BYTERUN_H "\nenum {\n"; |
199 | for ($i = 0; $i < @optype - 1; $i++) { |
200 | printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i; |
201 | } |
202 | printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i; |
203 | print BYTERUN_H <<'EOT'; |
204 | EXT int optype_size[] |
205 | #ifdef DOINIT |
206 | = { |
207 | EOT |
208 | for ($i = 0; $i < @optype - 1; $i++) { |
209 | printf BYTERUN_H " sizeof(%s),\n", $optype[$i], $i; |
210 | } |
211 | printf BYTERUN_H " sizeof(%s)\n}\n", $optype[$i], $i; |
212 | print BYTERUN_H <<'EOT'; |
213 | #endif /* DOINIT */ |
214 | ; |
215 | |
216 | EOT |
217 | |
218 | printf BYTERUN_H <<'EOT', scalar(@specialsv); |
219 | EXT SV * specialsv_list[%d]; |
220 | #define INIT_SPECIALSV_LIST STMT_START { \ |
221 | EOT |
222 | for ($i = 0; $i < @specialsv; $i++) { |
33b839e2 |
223 | print BYTERUN_H "\tspecialsv_list[$i] = $specialsv[$i]; \\\n"; |
a8a597b2 |
224 | } |
225 | print BYTERUN_H <<'EOT'; |
33b839e2 |
226 | } STMT_END |
a8a597b2 |
227 | EOT |
228 | |
229 | # |
230 | # Finish off insn_data and create array initialisers in Asmdata.pm |
231 | # |
232 | print ASMDATA_PM <<'EOT'; |
233 | |
234 | my ($insn_name, $insn_data); |
235 | while (($insn_name, $insn_data) = each %insn_data) { |
236 | $insn_name[$insn_data->[0]] = $insn_name; |
237 | } |
238 | # Fill in any gaps |
239 | @insn_name = map($_ || "unused", @insn_name); |
240 | |
241 | 1; |
242 | EOT |
243 | |
244 | __END__ |
245 | # First set instruction ord("#") to read comment to end-of-line (sneaky) |
246 | %number 35 |
247 | comment arg comment |
248 | # Then make ord("\n") into a no-op |
249 | %number 10 |
250 | nop none none |
251 | # Now for the rest of the ordinary ones, beginning with \0 which is |
252 | # ret so that \0-terminated strings can be read properly as bytecode. |
253 | %number 0 |
254 | # |
255 | #opcode lvalue argtype flags |
256 | # |
257 | ret none none x |
258 | ldsv sv svindex |
259 | ldop op opindex |
260 | stsv sv U32 s |
261 | stop op U32 s |
262 | ldspecsv sv U8 x |
263 | newsv sv U8 x |
264 | newop op U8 x |
265 | newopn op U8 x |
266 | newpv none PV |
267 | pv_cur pv.xpv_cur STRLEN |
268 | pv_free pv none x |
269 | sv_upgrade sv char x |
270 | sv_refcnt SvREFCNT(sv) U32 |
271 | sv_refcnt_add SvREFCNT(sv) I32 x |
272 | sv_flags SvFLAGS(sv) U32 |
273 | xrv SvRV(sv) svindex |
274 | xpv sv none x |
275 | xiv32 SvIVX(sv) I32 |
276 | xiv64 SvIVX(sv) IV64 |
277 | xnv SvNVX(sv) double |
278 | xlv_targoff LvTARGOFF(sv) STRLEN |
279 | xlv_targlen LvTARGLEN(sv) STRLEN |
280 | xlv_targ LvTARG(sv) svindex |
281 | xlv_type LvTYPE(sv) char |
282 | xbm_useful BmUSEFUL(sv) I32 |
283 | xbm_previous BmPREVIOUS(sv) U16 |
284 | xbm_rare BmRARE(sv) U8 |
285 | xfm_lines FmLINES(sv) I32 |
286 | xio_lines IoLINES(sv) long |
287 | xio_page IoPAGE(sv) long |
288 | xio_page_len IoPAGE_LEN(sv) long |
289 | xio_lines_left IoLINES_LEFT(sv) long |
290 | xio_top_name IoTOP_NAME(sv) pvcontents |
291 | xio_top_gv *(SV**)&IoTOP_GV(sv) svindex |
292 | xio_fmt_name IoFMT_NAME(sv) pvcontents |
293 | xio_fmt_gv *(SV**)&IoFMT_GV(sv) svindex |
294 | xio_bottom_name IoBOTTOM_NAME(sv) pvcontents |
295 | xio_bottom_gv *(SV**)&IoBOTTOM_GV(sv) svindex |
296 | xio_subprocess IoSUBPROCESS(sv) short |
297 | xio_type IoTYPE(sv) char |
298 | xio_flags IoFLAGS(sv) char |
299 | xcv_stash *(SV**)&CvSTASH(sv) svindex |
300 | xcv_start CvSTART(sv) opindex |
301 | xcv_root CvROOT(sv) opindex |
302 | xcv_gv *(SV**)&CvGV(sv) svindex |
303 | xcv_filegv *(SV**)&CvFILEGV(sv) svindex |
304 | xcv_depth CvDEPTH(sv) long |
305 | xcv_padlist *(SV**)&CvPADLIST(sv) svindex |
306 | xcv_outside *(SV**)&CvOUTSIDE(sv) svindex |
307 | xcv_flags CvFLAGS(sv) U8 |
308 | av_extend sv SSize_t x |
309 | av_push sv svindex x |
310 | xav_fill AvFILLp(sv) SSize_t |
311 | xav_max AvMAX(sv) SSize_t |
312 | xav_flags AvFLAGS(sv) U8 |
313 | xhv_riter HvRITER(sv) I32 |
314 | xhv_name HvNAME(sv) pvcontents |
315 | hv_store sv svindex x |
316 | sv_magic sv char x |
317 | mg_obj SvMAGIC(sv)->mg_obj svindex |
318 | mg_private SvMAGIC(sv)->mg_private U16 |
319 | mg_flags SvMAGIC(sv)->mg_flags U8 |
320 | mg_pv SvMAGIC(sv) pvcontents x |
321 | xmg_stash *(SV**)&SvSTASH(sv) svindex |
322 | gv_fetchpv sv strconst x |
323 | gv_stashpv sv strconst x |
324 | gp_sv GvSV(sv) svindex |
325 | gp_refcnt GvREFCNT(sv) U32 |
326 | gp_refcnt_add GvREFCNT(sv) I32 x |
327 | gp_av *(SV**)&GvAV(sv) svindex |
328 | gp_hv *(SV**)&GvHV(sv) svindex |
329 | gp_cv *(SV**)&GvCV(sv) svindex |
330 | gp_filegv *(SV**)&GvFILEGV(sv) svindex |
331 | gp_io *(SV**)&GvIOp(sv) svindex |
332 | gp_form *(SV**)&GvFORM(sv) svindex |
333 | gp_cvgen GvCVGEN(sv) U32 |
334 | gp_line GvLINE(sv) line_t |
335 | gp_share sv svindex x |
336 | xgv_flags GvFLAGS(sv) U8 |
337 | op_next op->op_next opindex |
338 | op_sibling op->op_sibling opindex |
339 | op_ppaddr op->op_ppaddr strconst x |
340 | op_targ op->op_targ PADOFFSET |
341 | op_type op OPCODE x |
342 | op_seq op->op_seq U16 |
343 | op_flags op->op_flags U8 |
344 | op_private op->op_private U8 |
345 | op_first cUNOP->op_first opindex |
346 | op_last cBINOP->op_last opindex |
347 | op_other cLOGOP->op_other opindex |
348 | op_true cCONDOP->op_true opindex |
349 | op_false cCONDOP->op_false opindex |
350 | op_children cLISTOP->op_children U32 |
351 | op_pmreplroot cPMOP->op_pmreplroot opindex |
352 | op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex |
353 | op_pmreplstart cPMOP->op_pmreplstart opindex |
354 | op_pmnext *(OP**)&cPMOP->op_pmnext opindex |
355 | pregcomp op pvcontents x |
356 | op_pmflags cPMOP->op_pmflags U16 |
357 | op_pmpermflags cPMOP->op_pmpermflags U16 |
358 | op_sv cSVOP->op_sv svindex |
359 | op_gv *(SV**)&cGVOP->op_gv svindex |
360 | op_pv cPVOP->op_pv pvcontents |
361 | op_pv_tr cPVOP->op_pv op_tr_array |
362 | op_redoop cLOOP->op_redoop opindex |
363 | op_nextop cLOOP->op_nextop opindex |
364 | op_lastop cLOOP->op_lastop opindex |
365 | cop_label cCOP->cop_label pvcontents |
366 | cop_stash *(SV**)&cCOP->cop_stash svindex |
367 | cop_filegv *(SV**)&cCOP->cop_filegv svindex |
368 | cop_seq cCOP->cop_seq U32 |
369 | cop_arybase cCOP->cop_arybase I32 |
370 | cop_line cCOP->cop_line line_t |
371 | main_start main_start opindex |
372 | main_root main_root opindex |
373 | curpad curpad svindex x |