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';
48 our $VERSION = '1.00';
52 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
53 our(%insn_data, @insn_name, @optype, @specialsv_name);
56 print ASMDATA_PM <<"EOT";
57 \@optype = qw(@optype);
58 \@specialsv_name = qw(@specialsv);
60 # XXX insn_data is initialised this way because with a large
61 # %insn_data = (foo => [...], bar => [...], ...) initialiser
62 # I get a hard-to-track-down stack underflow and segfault.
66 # Boilerplate for byterun.c
68 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
69 print BYTERUN_C $c_header, <<'EOT';
71 #define PERL_NO_GET_CONTEXT
81 static const int optype_size[] = {
84 for ($i = 0; $i < @optype - 1; $i++) {
85 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
87 printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
88 print BYTERUN_C <<'EOT';
92 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
94 if (ix > bstate->bs_obj_list_fill) {
95 Renew(bstate->bs_obj_list, ix + 32, void*);
96 bstate->bs_obj_list_fill = ix + 31;
98 bstate->bs_obj_list[ix] = obj;
103 byterun(pTHX_ register struct byteloader_state *bstate)
107 SV *specialsv_list[6];
109 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
110 New(666, bstate->bs_obj_list, 32, void*); /* set op objlist */
111 bstate->bs_obj_list_fill = 31;
115 for my $i ( 0 .. $#specialsv ) {
116 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
119 print BYTERUN_C <<'EOT';
121 while ((insn = BGET_FGETC()) != EOF) {
126 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
130 s/#.*//; # remove comments
132 if (/^%number\s+(.*)/) {
135 } elsif (/%enum\s+(.*?)\s+(.*)/) {
136 create_enum($1, $2); # must come before instructions
139 ($insn, $lvalue, $argtype, $flags) = split;
140 $insn_name[$insn_num] = $insn;
141 $fundtype = $alias_from{$argtype} || $argtype;
144 # Add the case statement and code for the bytecode interpreter in byterun.c
146 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
147 uc($insn), $insn_num;
148 my $optarg = $argtype eq "none" ? "" : ", arg";
150 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
153 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
154 } elsif ($flags =~ /s/) {
155 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
156 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
158 elsif ($optarg && $lvalue ne "none") {
159 print BYTERUN_C "\t\t$lvalue = arg;\n";
161 print BYTERUN_C "\t\tbreak;\n\t }\n";
164 # Add the initialiser line for %insn_data in Asmdata.pm
166 print ASMDATA_PM <<"EOT";
167 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
170 # Find the next unused instruction number
171 do { $insn_num++ } while $insn_name[$insn_num];
175 # Finish off byterun.c
177 print BYTERUN_C <<'EOT';
179 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
187 # Write the instruction and optype enum constants into byterun.h
189 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
190 print BYTERUN_H $c_header, <<'EOT';
191 struct byteloader_fdata {
197 struct byteloader_state {
198 struct byteloader_fdata *bs_fdata;
201 int bs_obj_list_fill;
206 int bl_getc(struct byteloader_fdata *);
207 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
208 extern void byterun(pTHX_ struct byteloader_state *);
213 my $add_enum_value = 0;
215 for $i ( 0 .. $#insn_name ) {
216 $insn = uc($insn_name[$i]);
217 if (defined($insn)) {
219 if ($add_enum_value) {
220 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
223 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
230 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
232 print BYTERUN_H "\nenum {\n";
233 for ($i = 0; $i < @optype - 1; $i++) {
234 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
236 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
239 # Finish off insn_data and create array initialisers in Asmdata.pm
241 print ASMDATA_PM <<'EOT';
243 my ($insn_name, $insn_data);
244 while (($insn_name, $insn_data) = each %insn_data) {
245 $insn_name[$insn_data->[0]] = $insn_name;
248 @insn_name = map($_ || "unused", @insn_name);
256 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
260 use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
264 Provides information about Perl ops in order to generate bytecode via
265 a bunch of exported variables. Its mostly used by B::Assembler and
272 my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
274 For a given $op_name (for example, 'cop_label', 'sv_flags', etc...)
275 you get an array ref containing the bytecode number of the op, a
276 reference to the subroutine used to 'PUT', and the name of the method
280 Add more detail about what $put_sub and $get_meth are and how to use them.
284 my $op_name = $insn_name[$bytecode_num];
286 A simple mapping of the bytecode number to the name of the op.
287 Suitable for using with %insn_data like so:
289 my $op_info = $insn_data{$insn_name[$bytecode_num]};
293 my $op_type = $optype[$op_type_num];
295 A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
297 =item @specialsv_name
299 my $sv_name = $specialsv_name[$sv_index];
301 Certain SV types are considered 'special'. They're represented by
302 B::SPECIAL and are refered to by a number from the specialsv_list.
303 This array maps that number back to the name of the SV (like 'Nullsv'
310 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
316 # First set instruction ord("#") to read comment to end-of-line (sneaky)
318 comment arg comment_t
319 # Then make ord("\n") into a no-op
322 # Now for the rest of the ordinary ones, beginning with \0 which is
323 # ret so that \0-terminated strings can be read properly as bytecode.
326 #opcode lvalue argtype flags
329 ldsv bstate->bs_sv svindex
331 stsv bstate->bs_sv U32 s
333 stpv bstate->bs_pv.xpv_pv U32 x
334 ldspecsv bstate->bs_sv U8 x
335 newsv bstate->bs_sv U8 x
339 pv_cur bstate->bs_pv.xpv_cur STRLEN
340 pv_free bstate->bs_pv none x
341 sv_upgrade bstate->bs_sv char x
342 sv_refcnt SvREFCNT(bstate->bs_sv) U32
343 sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
344 sv_flags SvFLAGS(bstate->bs_sv) U32
345 xrv SvRV(bstate->bs_sv) svindex
346 xpv bstate->bs_sv none x
347 xiv32 SvIVX(bstate->bs_sv) I32
348 xiv64 SvIVX(bstate->bs_sv) IV64
349 xnv SvNVX(bstate->bs_sv) NV
350 xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
351 xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
352 xlv_targ LvTARG(bstate->bs_sv) svindex
353 xlv_type LvTYPE(bstate->bs_sv) char
354 xbm_useful BmUSEFUL(bstate->bs_sv) I32
355 xbm_previous BmPREVIOUS(bstate->bs_sv) U16
356 xbm_rare BmRARE(bstate->bs_sv) U8
357 xfm_lines FmLINES(bstate->bs_sv) IV
358 xio_lines IoLINES(bstate->bs_sv) IV
359 xio_page IoPAGE(bstate->bs_sv) IV
360 xio_page_len IoPAGE_LEN(bstate->bs_sv) IV
361 xio_lines_left IoLINES_LEFT(bstate->bs_sv) IV
362 xio_top_name IoTOP_NAME(bstate->bs_sv) pvcontents
363 xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
364 xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvcontents
365 xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
366 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvcontents
367 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
368 xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
369 xio_type IoTYPE(bstate->bs_sv) char
370 xio_flags IoFLAGS(bstate->bs_sv) char
371 xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
372 xcv_start CvSTART(bstate->bs_sv) opindex
373 xcv_root CvROOT(bstate->bs_sv) opindex
374 xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
375 xcv_file CvFILE(bstate->bs_sv) pvindex
376 xcv_depth CvDEPTH(bstate->bs_sv) long
377 xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
378 xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
379 xcv_flags CvFLAGS(bstate->bs_sv) U16
380 av_extend bstate->bs_sv SSize_t x
381 av_push bstate->bs_sv svindex x
382 xav_fill AvFILLp(bstate->bs_sv) SSize_t
383 xav_max AvMAX(bstate->bs_sv) SSize_t
384 xav_flags AvFLAGS(bstate->bs_sv) U8
385 xhv_riter HvRITER(bstate->bs_sv) I32
386 xhv_name HvNAME(bstate->bs_sv) pvcontents
387 hv_store bstate->bs_sv svindex x
388 sv_magic bstate->bs_sv char x
389 mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
390 mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
391 mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
392 mg_pv SvMAGIC(bstate->bs_sv) pvcontents x
393 xmg_stash *(SV**)&SvSTASH(bstate->bs_sv) svindex
394 gv_fetchpv bstate->bs_sv strconst x
395 gv_stashpv bstate->bs_sv strconst x
396 gp_sv GvSV(bstate->bs_sv) svindex
397 gp_refcnt GvREFCNT(bstate->bs_sv) U32
398 gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
399 gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
400 gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
401 gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
402 gp_file GvFILE(bstate->bs_sv) pvindex
403 gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
404 gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
405 gp_cvgen GvCVGEN(bstate->bs_sv) U32
406 gp_line GvLINE(bstate->bs_sv) line_t
407 gp_share bstate->bs_sv svindex x
408 xgv_flags GvFLAGS(bstate->bs_sv) U8
409 op_next PL_op->op_next opindex
410 op_sibling PL_op->op_sibling opindex
411 op_ppaddr PL_op->op_ppaddr strconst x
412 op_targ PL_op->op_targ PADOFFSET
413 op_type PL_op OPCODE x
414 op_seq PL_op->op_seq U16
415 op_flags PL_op->op_flags U8
416 op_private PL_op->op_private U8
417 op_first cUNOP->op_first opindex
418 op_last cBINOP->op_last opindex
419 op_other cLOGOP->op_other opindex
420 op_pmreplroot cPMOP->op_pmreplroot opindex
421 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
422 op_pmreplstart cPMOP->op_pmreplstart opindex
423 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
424 pregcomp PL_op pvcontents x
425 op_pmflags cPMOP->op_pmflags U16
426 op_pmpermflags cPMOP->op_pmpermflags U16
427 op_sv cSVOP->op_sv svindex
428 op_padix cPADOP->op_padix PADOFFSET
429 op_pv cPVOP->op_pv pvcontents
430 op_pv_tr cPVOP->op_pv op_tr_array
431 op_redoop cLOOP->op_redoop opindex
432 op_nextop cLOOP->op_nextop opindex
433 op_lastop cLOOP->op_lastop opindex
434 cop_label cCOP->cop_label pvindex
435 cop_stashpv cCOP pvindex x
436 cop_file cCOP pvindex x
437 cop_seq cCOP->cop_seq U32
438 cop_arybase cCOP->cop_arybase I32
439 cop_line cCOP line_t x
440 cop_warnings cCOP->cop_warnings svindex
441 main_start PL_main_start opindex
442 main_root PL_main_root opindex
443 curpad PL_curpad svindex x
444 push_begin PL_beginav svindex x
445 push_init PL_initav svindex x
446 push_end PL_endav svindex x