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 CONDOP LISTOP PMOP SVOP GVOP 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);
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-1998 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 "byterun.c", "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 use vars qw(%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, ">byterun.c") or die "byterun.c: $!";
66 print BYTERUN_C $c_header, <<'EOT';
72 bset_obj_store(void *obj, I32 ix)
74 if (ix > PL_bytecode_obj_list_fill) {
75 if (PL_bytecode_obj_list_fill == -1)
76 New(666, PL_bytecode_obj_list, ix + 1, void*);
78 Renew(PL_bytecode_obj_list, ix + 1, void*);
79 PL_bytecode_obj_list_fill = ix;
81 PL_bytecode_obj_list[ix] = obj;
85 #ifdef INDIRECT_BGET_MACROS
86 void byterun(struct bytestream bs)
88 void byterun(PerlIO *fp)
89 #endif /* INDIRECT_BGET_MACROS */
93 while ((insn = BGET_FGETC()) != EOF) {
98 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
102 s/#.*//; # remove comments
104 if (/^%number\s+(.*)/) {
107 } elsif (/%enum\s+(.*?)\s+(.*)/) {
108 create_enum($1, $2); # must come before instructions
111 ($insn, $lvalue, $argtype, $flags) = split;
112 $insn_name[$insn_num] = $insn;
113 $fundtype = $alias_from{$argtype} || $argtype;
116 # Add the case statement and code for the bytecode interpreter in byterun.c
118 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
119 uc($insn), $insn_num;
120 my $optarg = $argtype eq "none" ? "" : ", arg";
122 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
125 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
126 } elsif ($flags =~ /s/) {
127 # Store instructions store to PL_bytecode_obj_list[arg]. "lvalue" field is rvalue.
128 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
130 elsif ($optarg && $lvalue ne "none") {
131 print BYTERUN_C "\t\t$lvalue = arg;\n";
133 print BYTERUN_C "\t\tbreak;\n\t }\n";
136 # Add the initialiser line for %insn_data in Asmdata.pm
138 print ASMDATA_PM <<"EOT";
139 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
142 # Find the next unused instruction number
143 do { $insn_num++ } while $insn_name[$insn_num];
147 # Finish off byterun.c
149 print BYTERUN_C <<'EOT';
151 croak("Illegal bytecode instruction %d\n", insn);
159 # Write the instruction and optype enum constants into byterun.h
161 open(BYTERUN_H, ">byterun.h") or die "byterun.h: $!";
162 print BYTERUN_H $c_header, <<'EOT';
163 #ifdef INDIRECT_BGET_MACROS
166 int (*fgetc)(void *);
167 int (*fread)(char *, size_t, size_t, void*);
168 void (*freadpv)(U32, void*);
170 #endif /* INDIRECT_BGET_MACROS */
172 void *bset_obj_store _((void *, I32));
178 my $add_enum_value = 0;
180 for ($i = 0; $i < @insn_name; $i++) {
181 $insn = uc($insn_name[$i]);
182 if (defined($insn)) {
184 if ($add_enum_value) {
185 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
188 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
195 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
197 print BYTERUN_H "\nenum {\n";
198 for ($i = 0; $i < @optype - 1; $i++) {
199 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
201 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
202 print BYTERUN_H <<'EOT';
203 EXT int optype_size[]
207 for ($i = 0; $i < @optype - 1; $i++) {
208 printf BYTERUN_H " sizeof(%s),\n", $optype[$i], $i;
210 printf BYTERUN_H " sizeof(%s)\n}\n", $optype[$i], $i;
211 print BYTERUN_H <<'EOT';
217 print BYTERUN_H <<'EOT';
218 #define INIT_SPECIALSV_LIST STMT_START { \
220 for ($i = 0; $i < @specialsv; $i++) {
221 print BYTERUN_H "\tPL_specialsv_list[$i] = $specialsv[$i]; \\\n";
223 print BYTERUN_H <<'EOT';
228 # Finish off insn_data and create array initialisers in Asmdata.pm
230 print ASMDATA_PM <<'EOT';
232 my ($insn_name, $insn_data);
233 while (($insn_name, $insn_data) = each %insn_data) {
234 $insn_name[$insn_data->[0]] = $insn_name;
237 @insn_name = map($_ || "unused", @insn_name);
245 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
253 See F<ext/B/B/Asmdata.pm>.
257 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
263 # First set instruction ord("#") to read comment to end-of-line (sneaky)
265 comment arg comment_t
266 # Then make ord("\n") into a no-op
269 # Now for the rest of the ordinary ones, beginning with \0 which is
270 # ret so that \0-terminated strings can be read properly as bytecode.
273 #opcode lvalue argtype flags
276 ldsv PL_bytecode_sv svindex
278 stsv PL_bytecode_sv U32 s
280 ldspecsv PL_bytecode_sv U8 x
281 newsv PL_bytecode_sv U8 x
285 pv_cur PL_bytecode_pv.xpv_cur STRLEN
286 pv_free PL_bytecode_pv none x
287 sv_upgrade PL_bytecode_sv char x
288 sv_refcnt SvREFCNT(PL_bytecode_sv) U32
289 sv_refcnt_add SvREFCNT(PL_bytecode_sv) I32 x
290 sv_flags SvFLAGS(PL_bytecode_sv) U32
291 xrv SvRV(PL_bytecode_sv) svindex
292 xpv PL_bytecode_sv none x
293 xiv32 SvIVX(PL_bytecode_sv) I32
294 xiv64 SvIVX(PL_bytecode_sv) IV64
295 xnv SvNVX(PL_bytecode_sv) double
296 xlv_targoff LvTARGOFF(PL_bytecode_sv) STRLEN
297 xlv_targlen LvTARGLEN(PL_bytecode_sv) STRLEN
298 xlv_targ LvTARG(PL_bytecode_sv) svindex
299 xlv_type LvTYPE(PL_bytecode_sv) char
300 xbm_useful BmUSEFUL(PL_bytecode_sv) I32
301 xbm_previous BmPREVIOUS(PL_bytecode_sv) U16
302 xbm_rare BmRARE(PL_bytecode_sv) U8
303 xfm_lines FmLINES(PL_bytecode_sv) I32
304 xio_lines IoLINES(PL_bytecode_sv) long
305 xio_page IoPAGE(PL_bytecode_sv) long
306 xio_page_len IoPAGE_LEN(PL_bytecode_sv) long
307 xio_lines_left IoLINES_LEFT(PL_bytecode_sv) long
308 xio_top_name IoTOP_NAME(PL_bytecode_sv) pvcontents
309 xio_top_gv *(SV**)&IoTOP_GV(PL_bytecode_sv) svindex
310 xio_fmt_name IoFMT_NAME(PL_bytecode_sv) pvcontents
311 xio_fmt_gv *(SV**)&IoFMT_GV(PL_bytecode_sv) svindex
312 xio_bottom_name IoBOTTOM_NAME(PL_bytecode_sv) pvcontents
313 xio_bottom_gv *(SV**)&IoBOTTOM_GV(PL_bytecode_sv) svindex
314 xio_subprocess IoSUBPROCESS(PL_bytecode_sv) short
315 xio_type IoTYPE(PL_bytecode_sv) char
316 xio_flags IoFLAGS(PL_bytecode_sv) char
317 xcv_stash *(SV**)&CvSTASH(PL_bytecode_sv) svindex
318 xcv_start CvSTART(PL_bytecode_sv) opindex
319 xcv_root CvROOT(PL_bytecode_sv) opindex
320 xcv_gv *(SV**)&CvGV(PL_bytecode_sv) svindex
321 xcv_filegv *(SV**)&CvFILEGV(PL_bytecode_sv) svindex
322 xcv_depth CvDEPTH(PL_bytecode_sv) long
323 xcv_padlist *(SV**)&CvPADLIST(PL_bytecode_sv) svindex
324 xcv_outside *(SV**)&CvOUTSIDE(PL_bytecode_sv) svindex
325 xcv_flags CvFLAGS(PL_bytecode_sv) U8
326 av_extend PL_bytecode_sv SSize_t x
327 av_push PL_bytecode_sv svindex x
328 xav_fill AvFILLp(PL_bytecode_sv) SSize_t
329 xav_max AvMAX(PL_bytecode_sv) SSize_t
330 xav_flags AvFLAGS(PL_bytecode_sv) U8
331 xhv_riter HvRITER(PL_bytecode_sv) I32
332 xhv_name HvNAME(PL_bytecode_sv) pvcontents
333 hv_store PL_bytecode_sv svindex x
334 sv_magic PL_bytecode_sv char x
335 mg_obj SvMAGIC(PL_bytecode_sv)->mg_obj svindex
336 mg_private SvMAGIC(PL_bytecode_sv)->mg_private U16
337 mg_flags SvMAGIC(PL_bytecode_sv)->mg_flags U8
338 mg_pv SvMAGIC(PL_bytecode_sv) pvcontents x
339 xmg_stash *(SV**)&SvSTASH(PL_bytecode_sv) svindex
340 gv_fetchpv PL_bytecode_sv strconst x
341 gv_stashpv PL_bytecode_sv strconst x
342 gp_sv GvSV(PL_bytecode_sv) svindex
343 gp_refcnt GvREFCNT(PL_bytecode_sv) U32
344 gp_refcnt_add GvREFCNT(PL_bytecode_sv) I32 x
345 gp_av *(SV**)&GvAV(PL_bytecode_sv) svindex
346 gp_hv *(SV**)&GvHV(PL_bytecode_sv) svindex
347 gp_cv *(SV**)&GvCV(PL_bytecode_sv) svindex
348 gp_filegv *(SV**)&GvFILEGV(PL_bytecode_sv) svindex
349 gp_io *(SV**)&GvIOp(PL_bytecode_sv) svindex
350 gp_form *(SV**)&GvFORM(PL_bytecode_sv) svindex
351 gp_cvgen GvCVGEN(PL_bytecode_sv) U32
352 gp_line GvLINE(PL_bytecode_sv) line_t
353 gp_share PL_bytecode_sv svindex x
354 xgv_flags GvFLAGS(PL_bytecode_sv) U8
355 op_next PL_op->op_next opindex
356 op_sibling PL_op->op_sibling opindex
357 op_ppaddr PL_op->op_ppaddr strconst x
358 op_targ PL_op->op_targ PADOFFSET
359 op_type PL_op OPCODE x
360 op_seq PL_op->op_seq U16
361 op_flags PL_op->op_flags U8
362 op_private PL_op->op_private U8
363 op_first cUNOP->op_first opindex
364 op_last cBINOP->op_last opindex
365 op_other cLOGOP->op_other opindex
366 op_true cCONDOP->op_true opindex
367 op_false cCONDOP->op_false opindex
368 op_children cLISTOP->op_children U32
369 op_pmreplroot cPMOP->op_pmreplroot opindex
370 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
371 op_pmreplstart cPMOP->op_pmreplstart opindex
372 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
373 pregcomp PL_op pvcontents x
374 op_pmflags cPMOP->op_pmflags U16
375 op_pmpermflags cPMOP->op_pmpermflags U16
376 op_sv cSVOP->op_sv svindex
377 op_gv *(SV**)&cGVOP->op_gv svindex
378 op_pv cPVOP->op_pv pvcontents
379 op_pv_tr cPVOP->op_pv op_tr_array
380 op_redoop cLOOP->op_redoop opindex
381 op_nextop cLOOP->op_nextop opindex
382 op_lastop cLOOP->op_lastop opindex
383 cop_label cCOP->cop_label pvcontents
384 cop_stash *(SV**)&cCOP->cop_stash svindex
385 cop_filegv *(SV**)&cCOP->cop_filegv svindex
386 cop_seq cCOP->cop_seq U32
387 cop_arybase cCOP->cop_arybase I32
388 cop_line cCOP->cop_line line_t
389 main_start PL_main_start opindex
390 main_root PL_main_root opindex
391 curpad PL_curpad svindex x