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 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';
25 /* -*- buffer-read-only: t -*-
27 * Copyright (c) 1996-1999 Malcolm Beattie
29 * You may distribute under the terms of either the GNU General Public
30 * License or the Artistic License, as specified in the README file.
34 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
39 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
41 safer_unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
44 # Start with boilerplate for Asmdata.pm
46 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
48 print ASMDATA_PM $perl_header, <<'EOT';
51 our $VERSION = '1.01';
55 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
56 our(%insn_data, @insn_name, @optype, @specialsv_name);
59 print ASMDATA_PM <<"EOT";
60 \@optype = qw(@optype);
61 \@specialsv_name = qw(@specialsv);
63 # XXX insn_data is initialised this way because with a large
64 # %insn_data = (foo => [...], bar => [...], ...) initialiser
65 # I get a hard-to-track-down stack underflow and segfault.
69 # Boilerplate for byterun.c
71 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
73 print BYTERUN_C $c_header, <<'EOT';
75 #define PERL_NO_GET_CONTEXT
85 static const int optype_size[] = {
88 for ($i = 0; $i < @optype - 1; $i++) {
89 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
91 printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
92 print BYTERUN_C <<'EOT';
96 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
98 if (ix > bstate->bs_obj_list_fill) {
99 Renew(bstate->bs_obj_list, ix + 32, void*);
100 bstate->bs_obj_list_fill = ix + 31;
102 bstate->bs_obj_list[ix] = obj;
107 byterun(pTHX_ register struct byteloader_state *bstate)
112 SV *specialsv_list[6];
114 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
115 New(666, bstate->bs_obj_list, 32, void*); /* set op objlist */
116 bstate->bs_obj_list_fill = 31;
117 bstate->bs_obj_list[0] = NULL; /* first is always Null */
122 for my $i ( 0 .. $#specialsv ) {
123 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
126 print BYTERUN_C <<'EOT';
128 while ((insn = BGET_FGETC()) != EOF) {
133 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
137 print BYTERUN_C if /^\s*#\s*(?:if|endif|el)/;
142 if (/^%number\s+(.*)/) {
145 } elsif (/%enum\s+(.*?)\s+(.*)/) {
146 create_enum($1, $2); # must come before instructions
149 ($insn, $lvalue, $argtype, $flags) = split;
151 if ($argtype =~ m:(.+)/(.+):) {
152 ($rvalcast, $argtype) = ("($1)", $2);
154 $insn_name[$insn_num] = $insn;
155 $fundtype = $alias_from{$argtype} || $argtype;
158 # Add the case statement and code for the bytecode interpreter in byterun.c
160 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
161 uc($insn), $insn_num;
162 my $optarg = $argtype eq "none" ? "" : ", arg";
164 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
167 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
168 } elsif ($flags =~ /s/) {
169 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
170 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
172 elsif ($optarg && $lvalue ne "none") {
173 print BYTERUN_C "\t\t$lvalue = ${rvalcast}arg;\n";
175 print BYTERUN_C "\t\tbreak;\n\t }\n";
178 # Add the initialiser line for %insn_data in Asmdata.pm
180 print ASMDATA_PM <<"EOT";
181 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
184 # Find the next unused instruction number
185 do { $insn_num++ } while $insn_name[$insn_num];
189 # Finish off byterun.c
191 print BYTERUN_C <<'EOT';
193 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
204 # Write the instruction and optype enum constants into byterun.h
206 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
208 print BYTERUN_H $c_header, <<'EOT';
209 struct byteloader_fdata {
215 struct byteloader_state {
216 struct byteloader_fdata *bs_fdata;
219 int bs_obj_list_fill;
225 int bl_getc(struct byteloader_fdata *);
226 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
227 extern int byterun(pTHX_ struct byteloader_state *);
232 my $add_enum_value = 0;
234 for $i ( 0 .. $#insn_name ) {
235 $insn = uc($insn_name[$i]);
236 if (defined($insn)) {
238 if ($add_enum_value) {
239 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
242 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
249 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
251 print BYTERUN_H "\nenum {\n";
252 for ($i = 0; $i < @optype - 1; $i++) {
253 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
255 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
257 print BYTERUN_H "/* ex: set ro: */\n";
260 # Finish off insn_data and create array initialisers in Asmdata.pm
262 print ASMDATA_PM <<'EOT';
264 my ($insn_name, $insn_data);
265 while (($insn_name, $insn_data) = each %insn_data) {
266 $insn_name[$insn_data->[0]] = $insn_name;
269 @insn_name = map($_ || "unused", @insn_name);
277 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
281 use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
285 Provides information about Perl ops in order to generate bytecode via
286 a bunch of exported variables. Its mostly used by B::Assembler and
293 my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
295 For a given $op_name (for example, 'cop_label', 'sv_flags', etc...)
296 you get an array ref containing the bytecode number of the op, a
297 reference to the subroutine used to 'PUT', and the name of the method
301 Add more detail about what $put_sub and $get_meth are and how to use them.
305 my $op_name = $insn_name[$bytecode_num];
307 A simple mapping of the bytecode number to the name of the op.
308 Suitable for using with %insn_data like so:
310 my $op_info = $insn_data{$insn_name[$bytecode_num]};
314 my $op_type = $optype[$op_type_num];
316 A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
318 =item @specialsv_name
320 my $sv_name = $specialsv_name[$sv_index];
322 Certain SV types are considered 'special'. They're represented by
323 B::SPECIAL and are refered to by a number from the specialsv_list.
324 This array maps that number back to the name of the SV (like 'Nullsv'
331 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
339 close ASMDATA_PM or die "Error closing ASMDATA_PM: $!";
340 close BYTERUN_H or die "Error closing BYTERUN_H: $!";
341 close BYTERUN_C or die "Error closing BYTERUN_C: $!";
344 # First set instruction ord("#") to read comment to end-of-line (sneaky)
346 comment arg comment_t
347 # Then make ord("\n") into a no-op
351 # Now for the rest of the ordinary ones, beginning with \0 which is
352 # ret so that \0-terminated strings can be read properly as bytecode.
355 # The argtype is either a single type or "rightvaluecast/argtype".
357 #opcode lvalue argtype flags
360 ldsv bstate->bs_sv svindex
362 stsv bstate->bs_sv U32 s
364 stpv bstate->bs_pv.xpv_pv U32 x
365 ldspecsv bstate->bs_sv U8 x
366 ldspecsvx bstate->bs_sv U8 x
367 newsv bstate->bs_sv U8 x
368 newsvx bstate->bs_sv U32 x
373 pv_cur bstate->bs_pv.xpv_cur STRLEN
374 pv_free bstate->bs_pv none x
375 sv_upgrade bstate->bs_sv U8 x
376 sv_refcnt SvREFCNT(bstate->bs_sv) U32
377 sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
378 sv_flags SvFLAGS(bstate->bs_sv) U32
379 xrv bstate->bs_sv svindex x
380 xpv bstate->bs_sv none x
381 xpv_cur bstate->bs_sv STRLEN x
382 xpv_len bstate->bs_sv STRLEN x
383 xiv bstate->bs_sv IV x
384 xnv bstate->bs_sv NV x
385 xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
386 xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
387 xlv_targ LvTARG(bstate->bs_sv) svindex
388 xlv_type LvTYPE(bstate->bs_sv) char
389 xbm_useful BmUSEFUL(bstate->bs_sv) I32
390 xbm_previous BmPREVIOUS(bstate->bs_sv) U16
391 xbm_rare BmRARE(bstate->bs_sv) U8
392 xfm_lines FmLINES(bstate->bs_sv) IV
393 xio_lines IoLINES(bstate->bs_sv) IV
394 xio_page IoPAGE(bstate->bs_sv) IV
395 xio_page_len IoPAGE_LEN(bstate->bs_sv) IV
396 xio_lines_left IoLINES_LEFT(bstate->bs_sv) IV
397 xio_top_name IoTOP_NAME(bstate->bs_sv) pvindex
398 xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
399 xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvindex
400 xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
401 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvindex
402 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
403 xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
404 xio_type IoTYPE(bstate->bs_sv) char
405 xio_flags IoFLAGS(bstate->bs_sv) char
406 xcv_xsubany *(SV**)&CvXSUBANY(bstate->bs_sv).any_ptr svindex
407 xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
408 xcv_start CvSTART(bstate->bs_sv) opindex
409 xcv_root CvROOT(bstate->bs_sv) opindex
410 xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
411 xcv_file CvFILE(bstate->bs_sv) pvindex
412 xcv_depth CvDEPTH(bstate->bs_sv) long
413 xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
414 xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
415 xcv_outside_seq CvOUTSIDE_SEQ(bstate->bs_sv) U32
416 xcv_flags CvFLAGS(bstate->bs_sv) U16
417 av_extend bstate->bs_sv SSize_t x
418 av_pushx bstate->bs_sv svindex x
419 av_push bstate->bs_sv svindex x
420 xav_fill AvFILLp(bstate->bs_sv) SSize_t
421 xav_max AvMAX(bstate->bs_sv) SSize_t
422 xhv_riter HvRITER(bstate->bs_sv) I32
423 xhv_name HvNAME(bstate->bs_sv) pvindex
424 xhv_pmroot *(OP**)&HvPMROOT(bstate->bs_sv) opindex
425 hv_store bstate->bs_sv svindex x
426 sv_magic bstate->bs_sv char x
427 mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
428 mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
429 mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
430 mg_name SvMAGIC(bstate->bs_sv) pvcontents x
431 mg_namex SvMAGIC(bstate->bs_sv) svindex x
432 xmg_stash bstate->bs_sv svindex x
433 gv_fetchpv bstate->bs_sv strconst x
434 gv_fetchpvx bstate->bs_sv strconst x
435 gv_stashpv bstate->bs_sv strconst x
436 gv_stashpvx bstate->bs_sv strconst x
437 gp_sv GvSV(bstate->bs_sv) svindex
438 gp_refcnt GvREFCNT(bstate->bs_sv) U32
439 gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
440 gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
441 gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
442 gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
443 gp_file GvFILE(bstate->bs_sv) pvindex
444 gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
445 gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
446 gp_cvgen GvCVGEN(bstate->bs_sv) U32
447 gp_line GvLINE(bstate->bs_sv) line_t
448 gp_share bstate->bs_sv svindex x
449 xgv_flags GvFLAGS(bstate->bs_sv) U8
450 op_next PL_op->op_next opindex
451 op_sibling PL_op->op_sibling opindex
452 op_ppaddr PL_op->op_ppaddr strconst x
453 op_targ PL_op->op_targ PADOFFSET
454 op_type PL_op OPCODE x
455 op_opt PL_op->op_opt U8
456 op_static PL_op->op_static U8
457 op_flags PL_op->op_flags U8
458 op_private PL_op->op_private U8
459 op_first cUNOP->op_first opindex
460 op_last cBINOP->op_last opindex
461 op_other cLOGOP->op_other opindex
462 op_pmreplroot cPMOP->op_pmreplroot opindex
463 op_pmreplstart cPMOP->op_pmreplstart opindex
464 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
466 op_pmstashpv cPMOP pvindex x
467 op_pmreplrootpo cPMOP->op_pmreplroot OP*/PADOFFSET
469 op_pmstash *(SV**)&cPMOP->op_pmstash svindex
470 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
472 pregcomp PL_op pvcontents x
473 op_pmflags cPMOP->op_pmflags U16
474 op_pmpermflags cPMOP->op_pmpermflags U16
475 op_pmdynflags cPMOP->op_pmdynflags U8
476 op_sv cSVOP->op_sv svindex
477 op_padix cPADOP->op_padix PADOFFSET
478 op_pv cPVOP->op_pv pvcontents
479 op_pv_tr cPVOP->op_pv op_tr_array
480 op_redoop cLOOP->op_redoop opindex
481 op_nextop cLOOP->op_nextop opindex
482 op_lastop cLOOP->op_lastop opindex
483 cop_label cCOP->cop_label pvindex
485 cop_stashpv cCOP pvindex x
486 cop_file cCOP pvindex x
488 cop_stash cCOP svindex x
489 cop_filegv cCOP svindex x
491 cop_seq cCOP->cop_seq U32
492 cop_arybase cCOP->cop_arybase I32
493 cop_line cCOP->cop_line line_t
494 cop_io cCOP->cop_io svindex
495 cop_warnings cCOP->cop_warnings svindex
496 main_start PL_main_start opindex
497 main_root PL_main_root opindex
498 main_cv *(SV**)&PL_main_cv svindex
499 curpad PL_curpad svindex x
500 push_begin PL_beginav svindex x
501 push_init PL_initav svindex x
502 push_end PL_endav svindex x
503 curstash *(SV**)&PL_curstash svindex
504 defstash *(SV**)&PL_defstash svindex
506 incav *(SV**)&GvAV(PL_incgv) svindex
507 load_glob none svindex x
509 regex_padav *(SV**)&PL_regex_padav svindex
512 comppad_name *(SV**)&PL_comppad_name svindex
513 xgv_stash *(SV**)&GvSTASH(bstate->bs_sv) svindex
514 signal bstate->bs_sv strconst x
516 formfeed PL_formfeed svindex