This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Given that @optype and @specialsv_name are hard coded tables, it seems
[perl5.git] / bytecode.pl
1 BEGIN {
2   push @INC, './lib';
3   require 'regen_lib.pl';
4 }
5 use strict;
6 my %alias_to = (
7     U32 => [qw(line_t)],
8     PADOFFSET => [qw(STRLEN SSize_t)],
9     U16 => [qw(OPCODE short)],
10     U8  => [qw(char)],
11 );
12
13 my (%alias_from, $from, $tos);
14 while (($from, $tos) = each %alias_to) {
15     map { $alias_from{$_} = $from } @$tos;
16 }
17
18 my $c_header = <<'EOT';
19 /* -*- buffer-read-only: t -*-
20  *
21  *      Copyright (c) 1996-1999 Malcolm Beattie
22  *
23  *      You may distribute under the terms of either the GNU General Public
24  *      License or the Artistic License, as specified in the README file.
25  *
26  */
27 /*
28  * This file is autogenerated from bytecode.pl. Changes made here will be lost.
29  */
30 EOT
31
32 my $perl_header;
33 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
34
35 safer_unlink "ext/B/B/Asmdata.pm";
36
37 #
38 # Start with boilerplate for Asmdata.pm
39 #
40 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
41 binmode ASMDATA_PM;
42 print ASMDATA_PM $perl_header, <<'EOT';
43 package B::Asmdata;
44
45 our $VERSION = '1.02';
46
47 use Exporter;
48 @ISA = qw(Exporter);
49 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
50 our(%insn_data, @insn_name);
51
52 use B qw(@optype @specialsv_name);
53 EOT
54 print ASMDATA_PM <<"EOT";
55
56 # XXX insn_data is initialised this way because with a large
57 # %insn_data = (foo => [...], bar => [...], ...) initialiser
58 # I get a hard-to-track-down stack underflow and segfault.
59 EOT
60
61 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
62
63 while (<DATA>) {
64     if (/^\s*#/) {
65         next;
66     }
67     chop;
68     next unless length;
69     if (/^%number\s+(.*)/) {
70         $insn_num = $1;
71         next;
72     } elsif (/%enum\s+(.*?)\s+(.*)/) {
73         create_enum($1, $2);    # must come before instructions
74         next;
75     }
76     ($insn, $lvalue, $argtype, $flags) = split;
77     my $rvalcast = '';
78     if ($argtype =~ m:(.+)/(.+):) {
79         ($rvalcast, $argtype) = ("($1)", $2);
80     }
81     $insn_name[$insn_num] = $insn;
82     $fundtype = $alias_from{$argtype} || $argtype;
83
84     #
85     # Add the initialiser line for %insn_data in Asmdata.pm
86     #
87     print ASMDATA_PM <<"EOT";
88 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
89 EOT
90
91     # Find the next unused instruction number
92     do { $insn_num++ } while $insn_name[$insn_num];
93 }
94
95 #
96 # Finish off insn_data and create array initialisers in Asmdata.pm
97 #
98 print ASMDATA_PM <<'EOT';
99
100 my ($insn_name, $insn_data);
101 while (($insn_name, $insn_data) = each %insn_data) {
102     $insn_name[$insn_data->[0]] = $insn_name;
103 }
104 # Fill in any gaps
105 @insn_name = map($_ || "unused", @insn_name);
106
107 1;
108
109 __END__
110
111 =head1 NAME
112
113 B::Asmdata - Autogenerated data about Perl ops
114
115 =head1 SYNOPSIS
116
117         use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
118
119 =head1 DESCRIPTION
120
121 Provides information about Perl ops in order to generate bytecode via
122 a bunch of exported variables.  Its mostly used by B::Assembler and
123 B::Disassembler.
124
125 =over 4
126
127 =item %insn_data
128
129   my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
130
131 For a given $op_name (for example, 'cop_label', 'sv_flags', etc...) 
132 you get an array ref containing the bytecode number of the op, a
133 reference to the subroutine used to 'PUT', and the name of the method
134 used to 'GET'.
135
136 =for _private
137 Add more detail about what $put_sub and $get_meth are and how to use them.
138
139 =item @insn_name
140
141   my $op_name = $insn_name[$bytecode_num];
142
143 A simple mapping of the bytecode number to the name of the op.
144 Suitable for using with %insn_data like so:
145
146   my $op_info = $insn_data{$insn_name[$bytecode_num]};
147
148 =item @optype
149
150   my $op_type = $optype[$op_type_num];
151
152 A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
153
154 =item @specialsv_name
155
156   my $sv_name = $specialsv_name[$sv_index];
157
158 Certain SV types are considered 'special'.  They're represented by
159 B::SPECIAL and are referred to by a number from the specialsv_list.
160 This array maps that number back to the name of the SV (like 'Nullsv'
161 or '&PL_sv_undef').
162
163 =back
164
165 =head1 AUTHOR
166
167 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
168
169 =cut
170
171 # ex: set ro:
172 EOT
173
174
175 close ASMDATA_PM or die "Error closing ASMDATA_PM: $!";
176
177 __END__
178 # First set instruction ord("#") to read comment to end-of-line (sneaky)
179 %number 35
180 comment         arg                     comment_t
181 # Then make ord("\n") into a no-op
182 %number 10
183 nop             none                    none
184
185 # Now for the rest of the ordinary ones, beginning with \0 which is
186 # ret so that \0-terminated strings can be read properly as bytecode.
187 %number 0
188 #
189 # The argtype is either a single type or "rightvaluecast/argtype".
190 #
191 #opcode         lvalue                                  argtype         flags   
192 #
193 ret             none                                    none            x
194 ldsv            bstate->bs_sv                           svindex
195 ldop            PL_op                                   opindex
196 stsv            bstate->bs_sv                           U32             s
197 stop            PL_op                                   U32             s
198 stpv            bstate->bs_pv.pvx                       U32             x
199 ldspecsv        bstate->bs_sv                           U8              x
200 ldspecsvx       bstate->bs_sv                           U8              x
201 newsv           bstate->bs_sv                           svtype          x
202 newsvx          bstate->bs_sv                           svtype          x
203 newop           PL_op                                   U8              x
204 newopx          PL_op                                   U16             x
205 newopn          PL_op                                   U8              x
206 newpv           none                                    PV
207 pv_cur          bstate->bs_pv.xpv.xpv_cur               STRLEN
208 pv_free         bstate->bs_pv.pvx                       none            x
209 sv_upgrade      bstate->bs_sv                           svtype          x
210 sv_refcnt       SvREFCNT(bstate->bs_sv)                 U32
211 sv_refcnt_add   SvREFCNT(bstate->bs_sv)                 I32             x
212 sv_flags        SvFLAGS(bstate->bs_sv)                  U32
213 xrv             bstate->bs_sv                           svindex         x
214 xpv             bstate->bs_sv                           none            x
215 xpv_cur         bstate->bs_sv                           STRLEN          x
216 xpv_len         bstate->bs_sv                           STRLEN          x
217 xiv             bstate->bs_sv                           IV              x
218 xnv             bstate->bs_sv                           NV              x
219 xlv_targoff     LvTARGOFF(bstate->bs_sv)                STRLEN
220 xlv_targlen     LvTARGLEN(bstate->bs_sv)                STRLEN
221 xlv_targ        LvTARG(bstate->bs_sv)                   svindex
222 xlv_type        LvTYPE(bstate->bs_sv)                   char
223 xbm_useful      BmUSEFUL(bstate->bs_sv)                 I32
224 xbm_previous    BmPREVIOUS(bstate->bs_sv)               U16
225 xbm_rare        BmRARE(bstate->bs_sv)                   U8
226 xfm_lines       FmLINES(bstate->bs_sv)                  IV
227 xio_lines       IoLINES(bstate->bs_sv)                  IV
228 xio_page        IoPAGE(bstate->bs_sv)                   IV
229 xio_page_len    IoPAGE_LEN(bstate->bs_sv)               IV
230 xio_lines_left  IoLINES_LEFT(bstate->bs_sv)             IV
231 xio_top_name    IoTOP_NAME(bstate->bs_sv)               pvindex
232 xio_top_gv      *(SV**)&IoTOP_GV(bstate->bs_sv)         svindex
233 xio_fmt_name    IoFMT_NAME(bstate->bs_sv)               pvindex
234 xio_fmt_gv      *(SV**)&IoFMT_GV(bstate->bs_sv)         svindex
235 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv)            pvindex
236 xio_bottom_gv   *(SV**)&IoBOTTOM_GV(bstate->bs_sv)      svindex
237 xio_subprocess  IoSUBPROCESS(bstate->bs_sv)             short
238 xio_type        IoTYPE(bstate->bs_sv)                   char
239 xio_flags       IoFLAGS(bstate->bs_sv)                  char
240 xcv_xsubany     *(SV**)&CvXSUBANY(bstate->bs_sv).any_ptr        svindex
241 xcv_stash       *(SV**)&CvSTASH(bstate->bs_sv)          svindex
242 xcv_start       CvSTART(bstate->bs_sv)                  opindex
243 xcv_root        CvROOT(bstate->bs_sv)                   opindex
244 xcv_gv          *(SV**)&CvGV(bstate->bs_sv)             svindex
245 xcv_file        CvFILE(bstate->bs_sv)                   pvindex
246 xcv_depth       CvDEPTH(bstate->bs_sv)                  long
247 xcv_padlist     *(SV**)&CvPADLIST(bstate->bs_sv)        svindex
248 xcv_outside     *(SV**)&CvOUTSIDE(bstate->bs_sv)        svindex
249 xcv_outside_seq CvOUTSIDE_SEQ(bstate->bs_sv)            U32
250 xcv_flags       CvFLAGS(bstate->bs_sv)                  U16
251 av_extend       bstate->bs_sv                           SSize_t         x
252 av_pushx        bstate->bs_sv                           svindex         x
253 av_push         bstate->bs_sv                           svindex         x
254 xav_fill        AvFILLp(bstate->bs_sv)                  SSize_t
255 xav_max         AvMAX(bstate->bs_sv)                    SSize_t
256 xhv_riter       HvRITER(bstate->bs_sv)                  I32
257 xhv_name        bstate->bs_sv                           pvindex         x
258 hv_store        bstate->bs_sv                           svindex         x
259 sv_magic        bstate->bs_sv                           char            x
260 mg_obj          SvMAGIC(bstate->bs_sv)->mg_obj          svindex
261 mg_private      SvMAGIC(bstate->bs_sv)->mg_private      U16
262 mg_flags        SvMAGIC(bstate->bs_sv)->mg_flags        U8
263 mg_name         SvMAGIC(bstate->bs_sv)                  pvcontents      x
264 mg_namex        SvMAGIC(bstate->bs_sv)                  svindex         x
265 xmg_stash       bstate->bs_sv                           svindex         x
266 gv_fetchpv      bstate->bs_sv                           strconst        x
267 gv_fetchpvx     bstate->bs_sv                           strconst        x
268 gv_stashpv      bstate->bs_sv                           strconst        x
269 gv_stashpvx     bstate->bs_sv                           strconst        x
270 gp_sv           GvSV(bstate->bs_sv)                     svindex
271 gp_refcnt       GvREFCNT(bstate->bs_sv)                 U32
272 gp_refcnt_add   GvREFCNT(bstate->bs_sv)                 I32             x
273 gp_av           *(SV**)&GvAV(bstate->bs_sv)             svindex
274 gp_hv           *(SV**)&GvHV(bstate->bs_sv)             svindex
275 gp_cv           *(SV**)&GvCV(bstate->bs_sv)             svindex
276 gp_file         bstate->bs_sv                           pvindex         x
277 gp_io           *(SV**)&GvIOp(bstate->bs_sv)            svindex
278 gp_form         *(SV**)&GvFORM(bstate->bs_sv)           svindex
279 gp_cvgen        GvCVGEN(bstate->bs_sv)                  U32
280 gp_line         GvLINE(bstate->bs_sv)                   line_t
281 gp_share        bstate->bs_sv                           svindex         x
282 xgv_flags       GvFLAGS(bstate->bs_sv)                  U8
283 op_next         PL_op->op_next                          opindex
284 op_sibling      PL_op->op_sibling                       opindex
285 op_ppaddr       PL_op->op_ppaddr                        strconst        x
286 op_targ         PL_op->op_targ                          PADOFFSET
287 op_type         PL_op                                   OPCODE          x
288 op_opt          PL_op->op_opt                           U8
289 op_static       PL_op->op_static                        U8
290 op_flags        PL_op->op_flags                         U8
291 op_private      PL_op->op_private                       U8
292 op_first        cUNOP->op_first                         opindex
293 op_last         cBINOP->op_last                         opindex
294 op_other        cLOGOP->op_other                        opindex
295 op_pmreplroot   cPMOP->op_pmreplroot                    opindex
296 op_pmreplstart  cPMOP->op_pmreplstart                   opindex
297 op_pmnext       *(OP**)&cPMOP->op_pmnext                opindex
298 #ifdef USE_ITHREADS
299 op_pmstashpv    cPMOP                                   pvindex         x
300 op_pmreplrootpo cPMOP->op_pmreplroot                    OP*/PADOFFSET
301 #else
302 op_pmstash      *(SV**)&cPMOP->op_pmstash               svindex
303 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot            svindex
304 #endif
305 pregcomp        PL_op                                   pvcontents      x
306 op_pmflags      cPMOP->op_pmflags                       U16
307 op_sv           cSVOP->op_sv                            svindex
308 op_padix        cPADOP->op_padix                        PADOFFSET
309 op_pv           cPVOP->op_pv                            pvcontents
310 op_pv_tr        cPVOP->op_pv                            op_tr_array
311 op_redoop       cLOOP->op_redoop                        opindex
312 op_nextop       cLOOP->op_nextop                        opindex
313 op_lastop       cLOOP->op_lastop                        opindex
314 cop_label       cCOP->cop_label                         pvindex
315 #ifdef USE_ITHREADS
316 cop_stashpv     cCOP                                    pvindex         x
317 cop_file        cCOP                                    pvindex         x
318 #else
319 cop_stash       cCOP                                    svindex         x
320 cop_filegv      cCOP                                    svindex         x
321 #endif
322 cop_seq         cCOP->cop_seq                           U32
323 cop_arybase     cCOP                                    I32             x
324 cop_line        cCOP->cop_line                          line_t
325 cop_warnings    cCOP                                    svindex         x
326 main_start      PL_main_start                           opindex
327 main_root       PL_main_root                            opindex
328 main_cv         *(SV**)&PL_main_cv                      svindex
329 curpad          PL_curpad                               svindex         x
330 push_begin      PL_beginav                              svindex         x
331 push_init       PL_initav                               svindex         x
332 push_end        PL_endav                                svindex         x
333 curstash        *(SV**)&PL_curstash                     svindex
334 defstash        *(SV**)&PL_defstash                     svindex
335 data            none                                    U8              x
336 incav           *(SV**)&GvAV(PL_incgv)                  svindex
337 load_glob       none                                    svindex         x
338 #ifdef USE_ITHREADS
339 regex_padav     *(SV**)&PL_regex_padav                  svindex
340 #endif
341 dowarn          PL_dowarn                               U8
342 comppad_name    *(SV**)&PL_comppad_name                 svindex
343 xgv_stash       *(SV**)&GvSTASH(bstate->bs_sv)          svindex
344 signal          bstate->bs_sv                           strconst        x
345 # to be removed
346 formfeed        PL_formfeed                             svindex