3 require 'regen_lib.pl';
8 PADOFFSET => [qw(STRLEN SSize_t)],
9 U16 => [qw(OPCODE short)],
13 my @optype= qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP);
15 # Nullsv *must* come first in the following so that the condition
16 # ($$sv == 0) can continue to be used to test (sv == Nullsv).
17 my @specialsv = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no
18 (SV*)pWARN_ALL (SV*)pWARN_NONE (SV*)pWARN_STD);
20 my (%alias_from, $from, $tos);
21 while (($from, $tos) = each %alias_to) {
22 map { $alias_from{$_} = $from } @$tos;
25 my $c_header = <<'EOT';
26 /* -*- buffer-read-only: t -*-
28 * Copyright (c) 1996-1999 Malcolm Beattie
30 * You may distribute under the terms of either the GNU General Public
31 * License or the Artistic License, as specified in the README file.
35 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
40 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
42 safer_unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
45 # Start with boilerplate for Asmdata.pm
47 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
49 print ASMDATA_PM $perl_header, <<'EOT';
52 our $VERSION = '1.01';
56 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
57 our(%insn_data, @insn_name, @optype, @specialsv_name);
60 print ASMDATA_PM <<"EOT";
61 \@optype = qw(@optype);
62 \@specialsv_name = qw(@specialsv);
64 # XXX insn_data is initialised this way because with a large
65 # %insn_data = (foo => [...], bar => [...], ...) initialiser
66 # I get a hard-to-track-down stack underflow and segfault.
70 # Boilerplate for byterun.c
72 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
74 print BYTERUN_C $c_header, <<'EOT';
76 #define PERL_NO_GET_CONTEXT
86 static const int optype_size[] = {
89 for ($i = 0; $i < @optype - 1; $i++) {
90 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
92 printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
94 my $size = @specialsv;
96 print BYTERUN_C <<"EOT";
100 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
102 if (ix > bstate->bs_obj_list_fill) {
103 Renew(bstate->bs_obj_list, ix + 32, void*);
104 bstate->bs_obj_list_fill = ix + 31;
106 bstate->bs_obj_list[ix] = obj;
111 byterun(pTHX_ register struct byteloader_state *bstate)
116 SV *specialsv_list[$size];
118 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
119 Newx(bstate->bs_obj_list, 32, void*); /* set op objlist */
120 bstate->bs_obj_list_fill = 31;
121 bstate->bs_obj_list[0] = NULL; /* first is always Null */
126 for my $i ( 0 .. $#specialsv ) {
127 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
130 print BYTERUN_C <<'EOT';
132 while ((insn = BGET_FGETC()) != EOF) {
137 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
141 print BYTERUN_C if /^\s*#\s*(?:if|endif|el)/;
146 if (/^%number\s+(.*)/) {
149 } elsif (/%enum\s+(.*?)\s+(.*)/) {
150 create_enum($1, $2); # must come before instructions
153 ($insn, $lvalue, $argtype, $flags) = split;
155 if ($argtype =~ m:(.+)/(.+):) {
156 ($rvalcast, $argtype) = ("($1)", $2);
158 $insn_name[$insn_num] = $insn;
159 $fundtype = $alias_from{$argtype} || $argtype;
162 # Add the case statement and code for the bytecode interpreter in byterun.c
164 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
165 uc($insn), $insn_num;
166 my $optarg = $argtype eq "none" ? "" : ", arg";
168 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
171 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
172 } elsif ($flags =~ /s/) {
173 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
174 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
176 elsif ($optarg && $lvalue ne "none") {
177 print BYTERUN_C "\t\t$lvalue = ${rvalcast}arg;\n";
179 print BYTERUN_C "\t\tbreak;\n\t }\n";
182 # Add the initialiser line for %insn_data in Asmdata.pm
184 print ASMDATA_PM <<"EOT";
185 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
188 # Find the next unused instruction number
189 do { $insn_num++ } while $insn_name[$insn_num];
193 # Finish off byterun.c
195 print BYTERUN_C <<'EOT';
197 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
208 # Write the instruction and optype enum constants into byterun.h
210 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
212 print BYTERUN_H $c_header, <<'EOT';
213 struct byteloader_fdata {
219 struct byteloader_pv_state {
224 struct byteloader_state {
225 struct byteloader_fdata *bs_fdata;
228 int bs_obj_list_fill;
230 struct byteloader_pv_state bs_pv;
234 int bl_getc(struct byteloader_fdata *);
235 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
236 extern int byterun(pTHX_ struct byteloader_state *);
241 my $add_enum_value = 0;
243 for $i ( 0 .. $#insn_name ) {
244 $insn = uc($insn_name[$i]);
245 if (defined($insn)) {
247 if ($add_enum_value) {
248 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
251 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
258 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
260 print BYTERUN_H "\nenum {\n";
261 for ($i = 0; $i < @optype - 1; $i++) {
262 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
264 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
266 print BYTERUN_H "/* ex: set ro: */\n";
269 # Finish off insn_data and create array initialisers in Asmdata.pm
271 print ASMDATA_PM <<'EOT';
273 my ($insn_name, $insn_data);
274 while (($insn_name, $insn_data) = each %insn_data) {
275 $insn_name[$insn_data->[0]] = $insn_name;
278 @insn_name = map($_ || "unused", @insn_name);
286 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
290 use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
294 Provides information about Perl ops in order to generate bytecode via
295 a bunch of exported variables. Its mostly used by B::Assembler and
302 my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
304 For a given $op_name (for example, 'cop_label', 'sv_flags', etc...)
305 you get an array ref containing the bytecode number of the op, a
306 reference to the subroutine used to 'PUT', and the name of the method
310 Add more detail about what $put_sub and $get_meth are and how to use them.
314 my $op_name = $insn_name[$bytecode_num];
316 A simple mapping of the bytecode number to the name of the op.
317 Suitable for using with %insn_data like so:
319 my $op_info = $insn_data{$insn_name[$bytecode_num]};
323 my $op_type = $optype[$op_type_num];
325 A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
327 =item @specialsv_name
329 my $sv_name = $specialsv_name[$sv_index];
331 Certain SV types are considered 'special'. They're represented by
332 B::SPECIAL and are referred to by a number from the specialsv_list.
333 This array maps that number back to the name of the SV (like 'Nullsv'
340 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
348 close ASMDATA_PM or die "Error closing ASMDATA_PM: $!";
349 close BYTERUN_H or die "Error closing BYTERUN_H: $!";
350 close BYTERUN_C or die "Error closing BYTERUN_C: $!";
353 # First set instruction ord("#") to read comment to end-of-line (sneaky)
355 comment arg comment_t
356 # Then make ord("\n") into a no-op
360 # Now for the rest of the ordinary ones, beginning with \0 which is
361 # ret so that \0-terminated strings can be read properly as bytecode.
364 # The argtype is either a single type or "rightvaluecast/argtype".
366 #opcode lvalue argtype flags
369 ldsv bstate->bs_sv svindex
371 stsv bstate->bs_sv U32 s
373 stpv bstate->bs_pv.pvx U32 x
374 ldspecsv bstate->bs_sv U8 x
375 ldspecsvx bstate->bs_sv U8 x
376 newsv bstate->bs_sv U8 x
377 newsvx bstate->bs_sv U32 x
382 pv_cur bstate->bs_pv.xpv.xpv_cur STRLEN
383 pv_free bstate->bs_pv.pvx none x
384 sv_upgrade bstate->bs_sv U8 x
385 sv_refcnt SvREFCNT(bstate->bs_sv) U32
386 sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
387 sv_flags SvFLAGS(bstate->bs_sv) U32
388 xrv bstate->bs_sv svindex x
389 xpv bstate->bs_sv none x
390 xpv_cur bstate->bs_sv STRLEN x
391 xpv_len bstate->bs_sv STRLEN x
392 xiv bstate->bs_sv IV x
393 xnv bstate->bs_sv NV x
394 xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
395 xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
396 xlv_targ LvTARG(bstate->bs_sv) svindex
397 xlv_type LvTYPE(bstate->bs_sv) char
398 xbm_useful BmUSEFUL(bstate->bs_sv) I32
399 xbm_previous BmPREVIOUS(bstate->bs_sv) U16
400 xbm_rare BmRARE(bstate->bs_sv) U8
401 xfm_lines FmLINES(bstate->bs_sv) IV
402 xio_lines IoLINES(bstate->bs_sv) IV
403 xio_page IoPAGE(bstate->bs_sv) IV
404 xio_page_len IoPAGE_LEN(bstate->bs_sv) IV
405 xio_lines_left IoLINES_LEFT(bstate->bs_sv) IV
406 xio_top_name IoTOP_NAME(bstate->bs_sv) pvindex
407 xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
408 xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvindex
409 xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
410 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvindex
411 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
412 xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
413 xio_type IoTYPE(bstate->bs_sv) char
414 xio_flags IoFLAGS(bstate->bs_sv) char
415 xcv_xsubany *(SV**)&CvXSUBANY(bstate->bs_sv).any_ptr svindex
416 xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
417 xcv_start CvSTART(bstate->bs_sv) opindex
418 xcv_root CvROOT(bstate->bs_sv) opindex
419 xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
420 xcv_file CvFILE(bstate->bs_sv) pvindex
421 xcv_depth CvDEPTH(bstate->bs_sv) long
422 xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
423 xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
424 xcv_outside_seq CvOUTSIDE_SEQ(bstate->bs_sv) U32
425 xcv_flags CvFLAGS(bstate->bs_sv) U16
426 av_extend bstate->bs_sv SSize_t x
427 av_pushx bstate->bs_sv svindex x
428 av_push bstate->bs_sv svindex x
429 xav_fill AvFILLp(bstate->bs_sv) SSize_t
430 xav_max AvMAX(bstate->bs_sv) SSize_t
431 xhv_riter HvRITER(bstate->bs_sv) I32
432 xhv_name bstate->bs_sv pvindex x
433 hv_store bstate->bs_sv svindex x
434 sv_magic bstate->bs_sv char x
435 mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
436 mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
437 mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
438 mg_name SvMAGIC(bstate->bs_sv) pvcontents x
439 mg_namex SvMAGIC(bstate->bs_sv) svindex x
440 xmg_stash bstate->bs_sv svindex x
441 gv_fetchpv bstate->bs_sv strconst x
442 gv_fetchpvx bstate->bs_sv strconst x
443 gv_stashpv bstate->bs_sv strconst x
444 gv_stashpvx bstate->bs_sv strconst x
445 gp_sv GvSV(bstate->bs_sv) svindex
446 gp_refcnt GvREFCNT(bstate->bs_sv) U32
447 gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
448 gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
449 gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
450 gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
451 gp_file bstate->bs_sv pvindex x
452 gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
453 gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
454 gp_cvgen GvCVGEN(bstate->bs_sv) U32
455 gp_line GvLINE(bstate->bs_sv) line_t
456 gp_share bstate->bs_sv svindex x
457 xgv_flags GvFLAGS(bstate->bs_sv) U8
458 op_next PL_op->op_next opindex
459 op_sibling PL_op->op_sibling opindex
460 op_ppaddr PL_op->op_ppaddr strconst x
461 op_targ PL_op->op_targ PADOFFSET
462 op_type PL_op OPCODE x
463 op_opt PL_op->op_opt U8
464 op_static PL_op->op_static U8
465 op_flags PL_op->op_flags U8
466 op_private PL_op->op_private U8
467 op_first cUNOP->op_first opindex
468 op_last cBINOP->op_last opindex
469 op_other cLOGOP->op_other opindex
470 op_pmreplroot cPMOP->op_pmreplroot opindex
471 op_pmreplstart cPMOP->op_pmreplstart opindex
472 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
474 op_pmstashpv cPMOP pvindex x
475 op_pmreplrootpo cPMOP->op_pmreplroot OP*/PADOFFSET
477 op_pmstash *(SV**)&cPMOP->op_pmstash svindex
478 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
480 pregcomp PL_op pvcontents x
481 op_pmflags cPMOP->op_pmflags U16
482 op_pmpermflags cPMOP->op_pmpermflags U16
483 op_pmdynflags cPMOP->op_pmdynflags U8
484 op_sv cSVOP->op_sv svindex
485 op_padix cPADOP->op_padix PADOFFSET
486 op_pv cPVOP->op_pv pvcontents
487 op_pv_tr cPVOP->op_pv op_tr_array
488 op_redoop cLOOP->op_redoop opindex
489 op_nextop cLOOP->op_nextop opindex
490 op_lastop cLOOP->op_lastop opindex
491 cop_label cCOP->cop_label pvindex
493 cop_stashpv cCOP pvindex x
494 cop_file cCOP pvindex x
496 cop_stash cCOP svindex x
497 cop_filegv cCOP svindex x
499 cop_seq cCOP->cop_seq U32
500 cop_arybase cCOP I32 x
501 cop_line cCOP->cop_line line_t
502 cop_io cCOP->cop_io svindex
503 cop_warnings cCOP svindex x
504 main_start PL_main_start opindex
505 main_root PL_main_root opindex
506 main_cv *(SV**)&PL_main_cv svindex
507 curpad PL_curpad svindex x
508 push_begin PL_beginav svindex x
509 push_init PL_initav svindex x
510 push_end PL_endav svindex x
511 curstash *(SV**)&PL_curstash svindex
512 defstash *(SV**)&PL_defstash svindex
514 incav *(SV**)&GvAV(PL_incgv) svindex
515 load_glob none svindex x
517 regex_padav *(SV**)&PL_regex_padav svindex
520 comppad_name *(SV**)&PL_comppad_name svindex
521 xgv_stash *(SV**)&GvSTASH(bstate->bs_sv) svindex
522 signal bstate->bs_sv strconst x
524 formfeed PL_formfeed svindex