3 require 'regen_lib.pl';
7 U32 => [qw(PADOFFSET STRLEN line_t)],
8 I32 => [qw(SSize_t long)],
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 pWARN_ALL pWARN_NONE);
19 my (%alias_from, $from, $tos);
20 while (($from, $tos) = each %alias_to) {
21 map { $alias_from{$_} = $from } @$tos;
24 my $c_header = <<'EOT';
26 * Copyright (c) 1996-1999 Malcolm Beattie
28 * You may distribute under the terms of either the GNU General Public
29 * License or the Artistic License, as specified in the README file.
33 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
38 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
40 safer_unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
43 # Start with boilerplate for Asmdata.pm
45 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
46 print ASMDATA_PM $perl_header, <<'EOT';
49 our $VERSION = '1.00';
53 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
54 our(%insn_data, @insn_name, @optype, @specialsv_name);
57 print ASMDATA_PM <<"EOT";
58 \@optype = qw(@optype);
59 \@specialsv_name = qw(@specialsv);
61 # XXX insn_data is initialised this way because with a large
62 # %insn_data = (foo => [...], bar => [...], ...) initialiser
63 # I get a hard-to-track-down stack underflow and segfault.
67 # Boilerplate for byterun.c
69 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
70 print BYTERUN_C $c_header, <<'EOT';
72 #define PERL_NO_GET_CONTEXT
82 static const int optype_size[] = {
85 for ($i = 0; $i < @optype - 1; $i++) {
86 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
88 printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
89 print BYTERUN_C <<'EOT';
93 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
95 if (ix > bstate->bs_obj_list_fill) {
96 Renew(bstate->bs_obj_list, ix + 32, void*);
97 bstate->bs_obj_list_fill = ix + 31;
99 bstate->bs_obj_list[ix] = obj;
104 byterun(pTHX_ register struct byteloader_state *bstate)
108 SV *specialsv_list[6];
110 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
111 New(666, bstate->bs_obj_list, 32, void*); /* set op objlist */
112 bstate->bs_obj_list_fill = 31;
113 bstate->bs_obj_list[0] = NULL; /* first is always Null */
117 for my $i ( 0 .. $#specialsv ) {
118 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
121 print BYTERUN_C <<'EOT';
123 while ((insn = BGET_FGETC()) != EOF) {
128 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
132 print BYTERUN_C if /^\s*#\s*(?:if|endif|el)/;
137 if (/^%number\s+(.*)/) {
140 } elsif (/%enum\s+(.*?)\s+(.*)/) {
141 create_enum($1, $2); # must come before instructions
144 ($insn, $lvalue, $argtype, $flags) = split;
145 $insn_name[$insn_num] = $insn;
146 $fundtype = $alias_from{$argtype} || $argtype;
149 # Add the case statement and code for the bytecode interpreter in byterun.c
151 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
152 uc($insn), $insn_num;
153 my $optarg = $argtype eq "none" ? "" : ", arg";
155 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
158 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
159 } elsif ($flags =~ /s/) {
160 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
161 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
163 elsif ($optarg && $lvalue ne "none") {
164 print BYTERUN_C "\t\t$lvalue = arg;\n";
166 print BYTERUN_C "\t\tbreak;\n\t }\n";
169 # Add the initialiser line for %insn_data in Asmdata.pm
171 print ASMDATA_PM <<"EOT";
172 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
175 # Find the next unused instruction number
176 do { $insn_num++ } while $insn_name[$insn_num];
180 # Finish off byterun.c
182 print BYTERUN_C <<'EOT';
184 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
193 # Write the instruction and optype enum constants into byterun.h
195 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
196 print BYTERUN_H $c_header, <<'EOT';
197 struct byteloader_fdata {
203 struct byteloader_state {
204 struct byteloader_fdata *bs_fdata;
207 int bs_obj_list_fill;
212 int bl_getc(struct byteloader_fdata *);
213 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
214 extern int byterun(pTHX_ struct byteloader_state *);
219 my $add_enum_value = 0;
221 for $i ( 0 .. $#insn_name ) {
222 $insn = uc($insn_name[$i]);
223 if (defined($insn)) {
225 if ($add_enum_value) {
226 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
229 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
236 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
238 print BYTERUN_H "\nenum {\n";
239 for ($i = 0; $i < @optype - 1; $i++) {
240 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
242 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
245 # Finish off insn_data and create array initialisers in Asmdata.pm
247 print ASMDATA_PM <<'EOT';
249 my ($insn_name, $insn_data);
250 while (($insn_name, $insn_data) = each %insn_data) {
251 $insn_name[$insn_data->[0]] = $insn_name;
254 @insn_name = map($_ || "unused", @insn_name);
262 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
266 use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
270 Provides information about Perl ops in order to generate bytecode via
271 a bunch of exported variables. Its mostly used by B::Assembler and
278 my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
280 For a given $op_name (for example, 'cop_label', 'sv_flags', etc...)
281 you get an array ref containing the bytecode number of the op, a
282 reference to the subroutine used to 'PUT', and the name of the method
286 Add more detail about what $put_sub and $get_meth are and how to use them.
290 my $op_name = $insn_name[$bytecode_num];
292 A simple mapping of the bytecode number to the name of the op.
293 Suitable for using with %insn_data like so:
295 my $op_info = $insn_data{$insn_name[$bytecode_num]};
299 my $op_type = $optype[$op_type_num];
301 A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
303 =item @specialsv_name
305 my $sv_name = $specialsv_name[$sv_index];
307 Certain SV types are considered 'special'. They're represented by
308 B::SPECIAL and are refered to by a number from the specialsv_list.
309 This array maps that number back to the name of the SV (like 'Nullsv'
316 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
322 close ASMDATA_PM or die "Error closing ASMDATA_PM: $!";
323 close BYTERUN_H or die "Error closing BYTERUN_H: $!";
324 close BYTERUN_C or die "Error closing BYTERUN_C: $!";
327 # First set instruction ord("#") to read comment to end-of-line (sneaky)
329 comment arg comment_t
330 # Then make ord("\n") into a no-op
334 # Now for the rest of the ordinary ones, beginning with \0 which is
335 # ret so that \0-terminated strings can be read properly as bytecode.
338 #opcode lvalue argtype flags
341 ldsv bstate->bs_sv svindex
343 stsv bstate->bs_sv U32 s
345 stpv bstate->bs_pv.xpv_pv U32 x
346 ldspecsv bstate->bs_sv U8 x
347 newsv bstate->bs_sv U8 x
351 pv_cur bstate->bs_pv.xpv_cur STRLEN
352 pv_free bstate->bs_pv none x
353 sv_upgrade bstate->bs_sv U8 x
354 sv_refcnt SvREFCNT(bstate->bs_sv) U32
355 sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
356 sv_flags SvFLAGS(bstate->bs_sv) U32
357 xrv SvRV(bstate->bs_sv) svindex
358 xpv bstate->bs_sv none x
359 xpv_cur SvCUR(bstate->bs_sv) STRLEN
360 xpv_len SvLEN(bstate->bs_sv) STRLEN
361 xiv SvIVX(bstate->bs_sv) IV
362 xnv SvNVX(bstate->bs_sv) NV
363 xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
364 xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
365 xlv_targ LvTARG(bstate->bs_sv) svindex
366 xlv_type LvTYPE(bstate->bs_sv) char
367 xbm_useful BmUSEFUL(bstate->bs_sv) I32
368 xbm_previous BmPREVIOUS(bstate->bs_sv) U16
369 xbm_rare BmRARE(bstate->bs_sv) U8
370 xfm_lines FmLINES(bstate->bs_sv) IV
371 xio_lines IoLINES(bstate->bs_sv) IV
372 xio_page IoPAGE(bstate->bs_sv) IV
373 xio_page_len IoPAGE_LEN(bstate->bs_sv) IV
374 xio_lines_left IoLINES_LEFT(bstate->bs_sv) IV
375 xio_top_name IoTOP_NAME(bstate->bs_sv) pvindex
376 xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
377 xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvindex
378 xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
379 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvindex
380 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
381 xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
382 xio_type IoTYPE(bstate->bs_sv) char
383 xio_flags IoFLAGS(bstate->bs_sv) char
384 xcv_xsubany *(SV**)&CvXSUBANY(bstate->bs_sv).any_ptr svindex
385 xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
386 xcv_start CvSTART(bstate->bs_sv) opindex
387 xcv_root CvROOT(bstate->bs_sv) opindex
388 xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
389 xcv_file CvFILE(bstate->bs_sv) pvindex
390 xcv_depth CvDEPTH(bstate->bs_sv) long
391 xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
392 xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
393 xcv_outside_seq CvOUTSIDE_SEQ(bstate->bs_sv) U32
394 xcv_flags CvFLAGS(bstate->bs_sv) U16
395 av_extend bstate->bs_sv SSize_t x
396 av_pushx bstate->bs_sv svindex x
397 av_push bstate->bs_sv svindex x
398 xav_fill AvFILLp(bstate->bs_sv) SSize_t
399 xav_max AvMAX(bstate->bs_sv) SSize_t
400 xav_flags AvFLAGS(bstate->bs_sv) U8
401 xhv_riter HvRITER(bstate->bs_sv) I32
402 xhv_name HvNAME(bstate->bs_sv) pvindex
403 xhv_pmroot *(OP**)&HvPMROOT(bstate->bs_sv) opindex
404 hv_store bstate->bs_sv svindex x
405 sv_magic bstate->bs_sv char x
406 mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
407 mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
408 mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
409 mg_name SvMAGIC(bstate->bs_sv) pvcontents x
410 mg_namex SvMAGIC(bstate->bs_sv) svindex x
411 xmg_stash *(SV**)&SvSTASH(bstate->bs_sv) svindex
412 gv_fetchpv bstate->bs_sv strconst x
413 gv_stashpv bstate->bs_sv strconst x
414 gp_sv GvSV(bstate->bs_sv) svindex
415 gp_refcnt GvREFCNT(bstate->bs_sv) U32
416 gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
417 gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
418 gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
419 gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
420 gp_file GvFILE(bstate->bs_sv) pvindex
421 gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
422 gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
423 gp_cvgen GvCVGEN(bstate->bs_sv) U32
424 gp_line GvLINE(bstate->bs_sv) line_t
425 gp_share bstate->bs_sv svindex x
426 xgv_flags GvFLAGS(bstate->bs_sv) U8
427 op_next PL_op->op_next opindex
428 op_sibling PL_op->op_sibling opindex
429 op_ppaddr PL_op->op_ppaddr strconst x
430 op_targ PL_op->op_targ PADOFFSET
431 op_type PL_op OPCODE x
432 op_seq PL_op->op_seq U16
433 op_flags PL_op->op_flags U8
434 op_private PL_op->op_private U8
435 op_first cUNOP->op_first opindex
436 op_last cBINOP->op_last opindex
437 op_other cLOGOP->op_other opindex
438 op_pmreplroot cPMOP->op_pmreplroot opindex
439 op_pmreplstart cPMOP->op_pmreplstart opindex
440 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
442 op_pmstashpv cPMOP->op_pmstashpv pvindex
443 op_pmreplrootpo (PADOFFSET)cPMOP->op_pmreplroot PADOFFSET
445 op_pmstash *(SV**)&cPMOP->op_pmstash svindex
446 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
448 pregcomp PL_op pvcontents x
449 op_pmflags cPMOP->op_pmflags U16
450 op_pmpermflags cPMOP->op_pmpermflags U16
451 op_pmdynflags cPMOP->op_pmdynflags U8
452 op_sv cSVOP->op_sv svindex
453 op_padix cPADOP->op_padix PADOFFSET
454 op_pv cPVOP->op_pv pvcontents
455 op_pv_tr cPVOP->op_pv op_tr_array
456 op_redoop cLOOP->op_redoop opindex
457 op_nextop cLOOP->op_nextop opindex
458 op_lastop cLOOP->op_lastop opindex
459 cop_label cCOP->cop_label pvindex
461 cop_stashpv cCOP pvindex x
462 cop_file cCOP pvindex x
464 cop_stash cCOP svindex x
465 cop_filegv cCOP svindex x
467 cop_seq cCOP->cop_seq U32
468 cop_arybase cCOP->cop_arybase I32
469 cop_line cCOP->cop_line line_t
470 cop_io cCOP->cop_io svindex
471 cop_warnings cCOP->cop_warnings svindex
472 main_start PL_main_start opindex
473 main_root PL_main_root opindex
474 main_cv *(SV**)&PL_main_cv svindex
475 curpad PL_curpad svindex x
476 push_begin PL_beginav svindex x
477 push_init PL_initav svindex x
478 push_end PL_endav svindex x
479 curstash *(SV**)&PL_curstash svindex
480 defstash *(SV**)&PL_defstash svindex
482 incav *(SV**)&PL_incgv svindex
483 load_glob none svindex x
485 regex_padav *(SV**)&PL_regex_padav svindex
488 comppad_name *(SV**)&PL_comppad_name svindex
489 xgv_stash *(SV**)&GvSTASH(bstate->bs_sv) svindex
490 signal bstate->bs_sv strconst x
492 formfeed PL_formfeed svindex