6 U32 => [qw(PADOFFSET STRLEN)],
7 I32 => [qw(SSize_t long)],
8 U16 => [qw(OPCODE line_t short)],
12 my @optype= qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP);
14 # Nullsv *must* come first in the following so that the condition
15 # ($$sv == 0) can continue to be used to test (sv == Nullsv).
16 my @specialsv = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no pWARN_ALL pWARN_NONE);
18 my (%alias_from, $from, $tos);
19 while (($from, $tos) = each %alias_to) {
20 map { $alias_from{$_} = $from } @$tos;
23 my $c_header = <<'EOT';
25 * Copyright (c) 1996-1999 Malcolm Beattie
27 * You may distribute under the terms of either the GNU General Public
28 * License or the Artistic License, as specified in the README file.
32 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
37 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
39 unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
42 # Start with boilerplate for Asmdata.pm
44 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
45 print ASMDATA_PM $perl_header, <<'EOT';
49 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
50 our(%insn_data, @insn_name, @optype, @specialsv_name);
53 print ASMDATA_PM <<"EOT";
54 \@optype = qw(@optype);
55 \@specialsv_name = qw(@specialsv);
57 # XXX insn_data is initialised this way because with a large
58 # %insn_data = (foo => [...], bar => [...], ...) initialiser
59 # I get a hard-to-track-down stack underflow and segfault.
63 # Boilerplate for byterun.c
65 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
66 print BYTERUN_C $c_header, <<'EOT';
68 #define PERL_NO_GET_CONTEXT
78 static const int optype_size[] = {
81 for ($i = 0; $i < @optype - 1; $i++) {
82 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
84 printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
85 print BYTERUN_C <<'EOT';
89 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
91 if (ix > bstate->bs_obj_list_fill) {
92 Renew(bstate->bs_obj_list, ix + 32, void*);
93 bstate->bs_obj_list_fill = ix + 31;
95 bstate->bs_obj_list[ix] = obj;
100 byterun(pTHX_ register struct byteloader_state *bstate)
104 SV *specialsv_list[6];
106 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
107 New(666, bstate->bs_obj_list, 32, void*); /* set op objlist */
108 bstate->bs_obj_list_fill = 31;
112 for (my $i = 0; $i < @specialsv; $i++) {
113 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
116 print BYTERUN_C <<'EOT';
118 while ((insn = BGET_FGETC()) != EOF) {
123 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
127 s/#.*//; # remove comments
129 if (/^%number\s+(.*)/) {
132 } elsif (/%enum\s+(.*?)\s+(.*)/) {
133 create_enum($1, $2); # must come before instructions
136 ($insn, $lvalue, $argtype, $flags) = split;
137 $insn_name[$insn_num] = $insn;
138 $fundtype = $alias_from{$argtype} || $argtype;
141 # Add the case statement and code for the bytecode interpreter in byterun.c
143 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
144 uc($insn), $insn_num;
145 my $optarg = $argtype eq "none" ? "" : ", arg";
147 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
150 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
151 } elsif ($flags =~ /s/) {
152 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
153 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
155 elsif ($optarg && $lvalue ne "none") {
156 print BYTERUN_C "\t\t$lvalue = arg;\n";
158 print BYTERUN_C "\t\tbreak;\n\t }\n";
161 # Add the initialiser line for %insn_data in Asmdata.pm
163 print ASMDATA_PM <<"EOT";
164 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
167 # Find the next unused instruction number
168 do { $insn_num++ } while $insn_name[$insn_num];
172 # Finish off byterun.c
174 print BYTERUN_C <<'EOT';
176 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
184 # Write the instruction and optype enum constants into byterun.h
186 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
187 print BYTERUN_H $c_header, <<'EOT';
188 struct byteloader_fdata {
194 struct byteloader_state {
195 struct byteloader_fdata *bs_fdata;
198 int bs_obj_list_fill;
203 int bl_getc(struct byteloader_fdata *);
204 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
205 extern void byterun(pTHX_ struct byteloader_state *);
210 my $add_enum_value = 0;
212 for ($i = 0; $i < @insn_name; $i++) {
213 $insn = uc($insn_name[$i]);
214 if (defined($insn)) {
216 if ($add_enum_value) {
217 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
220 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
227 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
229 print BYTERUN_H "\nenum {\n";
230 for ($i = 0; $i < @optype - 1; $i++) {
231 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
233 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
236 # Finish off insn_data and create array initialisers in Asmdata.pm
238 print ASMDATA_PM <<'EOT';
240 my ($insn_name, $insn_data);
241 while (($insn_name, $insn_data) = each %insn_data) {
242 $insn_name[$insn_data->[0]] = $insn_name;
245 @insn_name = map($_ || "unused", @insn_name);
253 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
261 See F<ext/B/B/Asmdata.pm>.
265 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
271 # First set instruction ord("#") to read comment to end-of-line (sneaky)
273 comment arg comment_t
274 # Then make ord("\n") into a no-op
277 # Now for the rest of the ordinary ones, beginning with \0 which is
278 # ret so that \0-terminated strings can be read properly as bytecode.
281 #opcode lvalue argtype flags
284 ldsv bstate->bs_sv svindex
286 stsv bstate->bs_sv U32 s
288 stpv bstate->bs_pv.xpv_pv U32 x
289 ldspecsv bstate->bs_sv U8 x
290 newsv bstate->bs_sv U8 x
294 pv_cur bstate->bs_pv.xpv_cur STRLEN
295 pv_free bstate->bs_pv none x
296 sv_upgrade bstate->bs_sv char x
297 sv_refcnt SvREFCNT(bstate->bs_sv) U32
298 sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
299 sv_flags SvFLAGS(bstate->bs_sv) U32
300 xrv SvRV(bstate->bs_sv) svindex
301 xpv bstate->bs_sv none x
302 xiv32 SvIVX(bstate->bs_sv) I32
303 xiv64 SvIVX(bstate->bs_sv) IV64
304 xnv SvNVX(bstate->bs_sv) NV
305 xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
306 xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
307 xlv_targ LvTARG(bstate->bs_sv) svindex
308 xlv_type LvTYPE(bstate->bs_sv) char
309 xbm_useful BmUSEFUL(bstate->bs_sv) I32
310 xbm_previous BmPREVIOUS(bstate->bs_sv) U16
311 xbm_rare BmRARE(bstate->bs_sv) U8
312 xfm_lines FmLINES(bstate->bs_sv) I32
313 xio_lines IoLINES(bstate->bs_sv) long
314 xio_page IoPAGE(bstate->bs_sv) long
315 xio_page_len IoPAGE_LEN(bstate->bs_sv) long
316 xio_lines_left IoLINES_LEFT(bstate->bs_sv) long
317 xio_top_name IoTOP_NAME(bstate->bs_sv) pvcontents
318 xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
319 xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvcontents
320 xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
321 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvcontents
322 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
323 xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
324 xio_type IoTYPE(bstate->bs_sv) char
325 xio_flags IoFLAGS(bstate->bs_sv) char
326 xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
327 xcv_start CvSTART(bstate->bs_sv) opindex
328 xcv_root CvROOT(bstate->bs_sv) opindex
329 xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
330 xcv_file CvFILE(bstate->bs_sv) pvindex
331 xcv_depth CvDEPTH(bstate->bs_sv) long
332 xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
333 xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
334 xcv_flags CvFLAGS(bstate->bs_sv) U16
335 av_extend bstate->bs_sv SSize_t x
336 av_push bstate->bs_sv svindex x
337 xav_fill AvFILLp(bstate->bs_sv) SSize_t
338 xav_max AvMAX(bstate->bs_sv) SSize_t
339 xav_flags AvFLAGS(bstate->bs_sv) U8
340 xhv_riter HvRITER(bstate->bs_sv) I32
341 xhv_name HvNAME(bstate->bs_sv) pvcontents
342 hv_store bstate->bs_sv svindex x
343 sv_magic bstate->bs_sv char x
344 mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
345 mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
346 mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
347 mg_pv SvMAGIC(bstate->bs_sv) pvcontents x
348 xmg_stash *(SV**)&SvSTASH(bstate->bs_sv) svindex
349 gv_fetchpv bstate->bs_sv strconst x
350 gv_stashpv bstate->bs_sv strconst x
351 gp_sv GvSV(bstate->bs_sv) svindex
352 gp_refcnt GvREFCNT(bstate->bs_sv) U32
353 gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
354 gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
355 gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
356 gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
357 gp_file GvFILE(bstate->bs_sv) pvindex
358 gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
359 gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
360 gp_cvgen GvCVGEN(bstate->bs_sv) U32
361 gp_line GvLINE(bstate->bs_sv) line_t
362 gp_share bstate->bs_sv svindex x
363 xgv_flags GvFLAGS(bstate->bs_sv) U8
364 op_next PL_op->op_next opindex
365 op_sibling PL_op->op_sibling opindex
366 op_ppaddr PL_op->op_ppaddr strconst x
367 op_targ PL_op->op_targ PADOFFSET
368 op_type PL_op OPCODE x
369 op_seq PL_op->op_seq U16
370 op_flags PL_op->op_flags U8
371 op_private PL_op->op_private U8
372 op_first cUNOP->op_first opindex
373 op_last cBINOP->op_last opindex
374 op_other cLOGOP->op_other opindex
375 op_pmreplroot cPMOP->op_pmreplroot opindex
376 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
377 op_pmreplstart cPMOP->op_pmreplstart opindex
378 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
379 pregcomp PL_op pvcontents x
380 op_pmflags cPMOP->op_pmflags U16
381 op_pmpermflags cPMOP->op_pmpermflags U16
382 op_sv cSVOP->op_sv svindex
383 op_padix cPADOP->op_padix PADOFFSET
384 op_pv cPVOP->op_pv pvcontents
385 op_pv_tr cPVOP->op_pv op_tr_array
386 op_redoop cLOOP->op_redoop opindex
387 op_nextop cLOOP->op_nextop opindex
388 op_lastop cLOOP->op_lastop opindex
389 cop_label cCOP->cop_label pvindex
390 cop_stashpv cCOP pvindex x
391 cop_file cCOP pvindex x
392 cop_seq cCOP->cop_seq U32
393 cop_arybase cCOP->cop_arybase I32
394 cop_line cCOP line_t x
395 cop_warnings cCOP->cop_warnings svindex
396 main_start PL_main_start opindex
397 main_root PL_main_root opindex
398 curpad PL_curpad svindex x
399 push_begin PL_beginav svindex x
400 push_init PL_initav svindex x
401 push_end PL_endav svindex x