Incorporate a large list of corrections and updates to module versions
[perl.git] / bytecode.pl
1 #!perl -w
2 BEGIN {
3   push @INC, './lib';
4   require 'regen_lib.pl';
5 }
6 use strict;
7 my %alias_to = (
8     U32 => [qw(line_t)],
9     PADOFFSET => [qw(STRLEN SSize_t)],
10     U16 => [qw(OPCODE short)],
11     U8  => [qw(char)],
12 );
13
14 my (%alias_from, $from, $tos);
15 while (($from, $tos) = each %alias_to) {
16     map { $alias_from{$_} = $from } @$tos;
17 }
18
19 my $c_header = <<'EOT';
20 /* -*- buffer-read-only: t -*-
21  *
22  *      Copyright (c) 1996-1999 Malcolm Beattie
23  *
24  *      You may distribute under the terms of either the GNU General Public
25  *      License or the Artistic License, as specified in the README file.
26  *
27  */
28 /*
29  * This file is autogenerated from bytecode.pl. Changes made here will be lost.
30  */
31 EOT
32
33 my $perl_header;
34 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
35
36 # We need the values of two variables from the as-yet uninistalled B.pm
37 use vars qw(@optype @specialsv_name);
38 {
39     open B, "ext/B/B.pm" or die "B: $!";
40     local $/;
41     my $b = <B>;
42     foreach my $var (qw(optype specialsv_name)) {
43         my ($declaration) = $b =~ /(\@B::$var = qw\(.*?\);)/s;
44         die "Can't find the declaration for \@B::$var" unless $declaration;
45         $declaration =~ s/\@B::/\@/s;
46         eval $declaration or die "$@ from $declaration";
47     }
48     close B;
49 }
50
51 safer_unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
52
53 #
54 # Start with boilerplate for Asmdata.pm
55 #
56 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
57 binmode ASMDATA_PM;
58 print ASMDATA_PM $perl_header, <<'EOT';
59 package B::Asmdata;
60
61 our $VERSION = '1.02';
62
63 use Exporter;
64 @ISA = qw(Exporter);
65 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
66 our(%insn_data, @insn_name);
67
68 use B qw(@optype @specialsv_name);
69 EOT
70 print ASMDATA_PM <<"EOT";
71
72 # XXX insn_data is initialised this way because with a large
73 # %insn_data = (foo => [...], bar => [...], ...) initialiser
74 # I get a hard-to-track-down stack underflow and segfault.
75 EOT
76
77 #
78 # Boilerplate for byterun.c
79 #
80 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
81 binmode BYTERUN_C;
82 print BYTERUN_C $c_header, <<'EOT';
83
84 #define PERL_NO_GET_CONTEXT
85 #include "EXTERN.h"
86 #include "perl.h"
87 #define NO_XSLOCKS
88 #include "XSUB.h"
89
90 #include "byterun.h"
91 #include "bytecode.h"
92
93
94 static const int optype_size[] = {
95 EOT
96 my $i = 0;
97 for ($i = 0; $i < @optype - 1; $i++) {
98     printf BYTERUN_C "    sizeof(%s),\n", $optype[$i], $i;
99 }
100 printf BYTERUN_C "    sizeof(%s)\n", $optype[$i], $i;
101
102 my $size = @specialsv_name;
103
104 print BYTERUN_C <<"EOT";
105 };
106
107 void *
108 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
109 {
110     if (ix > bstate->bs_obj_list_fill) {
111         Renew(bstate->bs_obj_list, ix + 32, void*);
112         bstate->bs_obj_list_fill = ix + 31;
113     }
114     bstate->bs_obj_list[ix] = obj;
115     return obj;
116 }
117
118 int
119 byterun(pTHX_ register struct byteloader_state *bstate)
120 {
121     register int insn;
122     U32 ix;
123     SV *specialsv_list[$size];
124
125     BYTECODE_HEADER_CHECK;      /* croak if incorrect platform */
126     Newx(bstate->bs_obj_list, 32, void*); /* set op objlist */
127     bstate->bs_obj_list_fill = 31;
128     bstate->bs_obj_list[0] = NULL; /* first is always Null */
129     bstate->bs_ix = 1;
130
131 EOT
132
133 for my $i ( 0 .. $#specialsv_name ) {
134     print BYTERUN_C "    specialsv_list[$i] = $specialsv_name[$i];\n";
135 }
136
137 print BYTERUN_C <<'EOT';
138
139     while ((insn = BGET_FGETC()) != EOF) {
140         switch (insn) {
141 EOT
142
143
144 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
145
146 while (<DATA>) {
147     if (/^\s*#/) {
148         print BYTERUN_C if /^\s*#\s*(?:if|endif|el)/;
149         next;
150     }
151     chop;
152     next unless length;
153     if (/^%number\s+(.*)/) {
154         $insn_num = $1;
155         next;
156     } elsif (/%enum\s+(.*?)\s+(.*)/) {
157         create_enum($1, $2);    # must come before instructions
158         next;
159     }
160     ($insn, $lvalue, $argtype, $flags) = split;
161     $flags = '' unless defined $flags;
162     my $rvalcast = '';
163     if ($argtype =~ m:(.+)/(.+):) {
164         ($rvalcast, $argtype) = ("($1)", $2);
165     }
166     $insn_name[$insn_num] = $insn;
167     $fundtype = $alias_from{$argtype} || $argtype;
168
169     #
170     # Add the case statement and code for the bytecode interpreter in byterun.c
171     #
172     printf BYTERUN_C "\t  case INSN_%s:\t\t/* %d */\n\t    {\n",
173         uc($insn), $insn_num;
174     my $optarg = $argtype eq "none" ? "" : ", arg";
175     if ($optarg) {
176         printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
177     }
178     if ($flags =~ /x/) {
179         print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
180     } elsif ($flags =~ /s/) {
181         # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
182         print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
183     }
184     elsif ($optarg && $lvalue ne "none") {
185         print BYTERUN_C "\t\t$lvalue = ${rvalcast}arg;\n";
186     }
187     print BYTERUN_C "\t\tbreak;\n\t    }\n";
188
189     #
190     # Add the initialiser line for %insn_data in Asmdata.pm
191     #
192     print ASMDATA_PM <<"EOT";
193 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
194 EOT
195
196     # Find the next unused instruction number
197     do { $insn_num++ } while $insn_name[$insn_num];
198 }
199
200 #
201 # Finish off byterun.c
202 #
203 print BYTERUN_C <<'EOT';
204           default:
205             Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
206             /* NOTREACHED */
207         }
208     }
209     return 0;
210 }
211
212 /* ex: set ro: */
213 EOT
214
215 #
216 # Write the instruction and optype enum constants into byterun.h
217 #
218 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
219 binmode BYTERUN_H;
220 print BYTERUN_H $c_header, <<'EOT';
221 struct byteloader_fdata {
222     SV  *datasv;
223     int next_out;
224     int idx;
225 };
226
227 struct byteloader_state {
228     struct byteloader_fdata     *bs_fdata;
229     SV                          *bs_sv;
230     void                        **bs_obj_list;
231     int                         bs_obj_list_fill;
232     int                         bs_ix;
233     XPV                         bs_pv;
234     int                         bs_iv_overflows;
235 };
236
237 int bl_getc(struct byteloader_fdata *);
238 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
239 extern int byterun(pTHX_ struct byteloader_state *);
240
241 enum {
242 EOT
243
244 my $add_enum_value = 0;
245 my $max_insn;
246 for $i ( 0 .. $#insn_name ) {
247     $insn = uc($insn_name[$i]);
248     if (defined($insn)) {
249         $max_insn = $i;
250         if ($add_enum_value) {
251             print BYTERUN_H "    INSN_$insn = $i,\t\t\t/* $i */\n";
252             $add_enum_value = 0;
253         } else {
254             print BYTERUN_H "    INSN_$insn,\t\t\t/* $i */\n";
255         }
256     } else {
257         $add_enum_value = 1;
258     }
259 }
260
261 print BYTERUN_H "    MAX_INSN = $max_insn\n};\n";
262
263 print BYTERUN_H "\nenum {\n";
264 for ($i = 0; $i < @optype - 1; $i++) {
265     printf BYTERUN_H "    OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
266 }
267 printf BYTERUN_H "    OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
268
269 print BYTERUN_H "/* ex: set ro: */\n";
270
271 #
272 # Finish off insn_data and create array initialisers in Asmdata.pm
273 #
274 print ASMDATA_PM <<'EOT';
275
276 my ($insn_name, $insn_data);
277 while (($insn_name, $insn_data) = each %insn_data) {
278     $insn_name[$insn_data->[0]] = $insn_name;
279 }
280 # Fill in any gaps
281 @insn_name = map($_ || "unused", @insn_name);
282
283 1;
284
285 __END__
286
287 =head1 NAME
288
289 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
290
291 =head1 SYNOPSIS
292
293         use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
294
295 =head1 DESCRIPTION
296
297 Provides information about Perl ops in order to generate bytecode via
298 a bunch of exported variables.  Its mostly used by B::Assembler and
299 B::Disassembler.
300
301 =over 4
302
303 =item %insn_data
304
305   my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
306
307 For a given $op_name (for example, 'cop_label', 'sv_flags', etc...) 
308 you get an array ref containing the bytecode number of the op, a
309 reference to the subroutine used to 'PUT', and the name of the method
310 used to 'GET'.
311
312 =for _private
313 Add more detail about what $put_sub and $get_meth are and how to use them.
314
315 =item @insn_name
316
317   my $op_name = $insn_name[$bytecode_num];
318
319 A simple mapping of the bytecode number to the name of the op.
320 Suitable for using with %insn_data like so:
321
322   my $op_info = $insn_data{$insn_name[$bytecode_num]};
323
324 =item @optype
325
326   my $op_type = $optype[$op_type_num];
327
328 A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
329
330 =item @specialsv_name
331
332   my $sv_name = $specialsv_name[$sv_index];
333
334 Certain SV types are considered 'special'.  They're represented by
335 B::SPECIAL and are referred to by a number from the specialsv_list.
336 This array maps that number back to the name of the SV (like 'Nullsv'
337 or '&PL_sv_undef').
338
339 =back
340
341 =head1 AUTHOR
342
343 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
344
345 =cut
346
347 # ex: set ro:
348 EOT
349
350
351 close ASMDATA_PM or die "Error closing ASMDATA_PM: $!";
352 close BYTERUN_H or die "Error closing BYTERUN_H: $!";
353 close BYTERUN_C or die "Error closing BYTERUN_C: $!";
354
355 __END__
356 # First set instruction ord("#") to read comment to end-of-line (sneaky)
357 %number 35
358 comment         arg                     comment_t
359 # Then make ord("\n") into a no-op
360 %number 10
361 nop             none                    none
362
363 # Now for the rest of the ordinary ones, beginning with \0 which is
364 # ret so that \0-terminated strings can be read properly as bytecode.
365 %number 0
366 #
367 # The argtype is either a single type or "rightvaluecast/argtype".
368 #
369 #opcode         lvalue                                  argtype         flags   
370 #
371 ret             none                                    none            x
372 ldsv            bstate->bs_sv                           svindex
373 ldop            PL_op                                   opindex
374 stsv            bstate->bs_sv                           U32             s
375 stop            PL_op                                   U32             s
376 stpv            bstate->bs_pv.xpv_pv                    U32             x
377 ldspecsv        bstate->bs_sv                           U8              x
378 ldspecsvx       bstate->bs_sv                           U8              x
379 newsv           bstate->bs_sv                           svtype          x
380 newsvx          bstate->bs_sv                           svtype          x
381 newop           PL_op                                   U8              x
382 newopx          PL_op                                   U16             x
383 newopn          PL_op                                   U8              x
384 newpv           none                                    PV
385 pv_cur          bstate->bs_pv.xpv_cur                   STRLEN
386 pv_free         bstate->bs_pv                           none            x
387 sv_upgrade      bstate->bs_sv                           svtype          x
388 sv_refcnt       SvREFCNT(bstate->bs_sv)                 U32
389 sv_refcnt_add   SvREFCNT(bstate->bs_sv)                 I32             x
390 sv_flags        SvFLAGS(bstate->bs_sv)                  U32
391 xrv             bstate->bs_sv                           svindex         x
392 xpv             bstate->bs_sv                           none            x
393 xpv_cur         bstate->bs_sv                           STRLEN          x
394 xpv_len         bstate->bs_sv                           STRLEN          x
395 xiv             bstate->bs_sv                           IV              x
396 xnv             bstate->bs_sv                           NV              x
397 xlv_targoff     LvTARGOFF(bstate->bs_sv)                STRLEN
398 xlv_targlen     LvTARGLEN(bstate->bs_sv)                STRLEN
399 xlv_targ        LvTARG(bstate->bs_sv)                   svindex
400 xlv_type        LvTYPE(bstate->bs_sv)                   char
401 xbm_useful      BmUSEFUL(bstate->bs_sv)                 I32
402 xbm_previous    BmPREVIOUS(bstate->bs_sv)               U16
403 xbm_rare        BmRARE(bstate->bs_sv)                   U8
404 xfm_lines       FmLINES(bstate->bs_sv)                  IV
405 xio_lines       IoLINES(bstate->bs_sv)                  IV
406 xio_page        IoPAGE(bstate->bs_sv)                   IV
407 xio_page_len    IoPAGE_LEN(bstate->bs_sv)               IV
408 xio_lines_left  IoLINES_LEFT(bstate->bs_sv)             IV
409 xio_top_name    IoTOP_NAME(bstate->bs_sv)               pvindex
410 xio_top_gv      *(SV**)&IoTOP_GV(bstate->bs_sv)         svindex
411 xio_fmt_name    IoFMT_NAME(bstate->bs_sv)               pvindex
412 xio_fmt_gv      *(SV**)&IoFMT_GV(bstate->bs_sv)         svindex
413 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv)            pvindex
414 xio_bottom_gv   *(SV**)&IoBOTTOM_GV(bstate->bs_sv)      svindex
415 xio_subprocess  IoSUBPROCESS(bstate->bs_sv)             short
416 xio_type        IoTYPE(bstate->bs_sv)                   char
417 xio_flags       IoFLAGS(bstate->bs_sv)                  char
418 xcv_xsubany     *(SV**)&CvXSUBANY(bstate->bs_sv).any_ptr        svindex
419 xcv_stash       *(SV**)&CvSTASH(bstate->bs_sv)          svindex
420 xcv_start       CvSTART(bstate->bs_sv)                  opindex
421 xcv_root        CvROOT(bstate->bs_sv)                   opindex
422 xcv_gv          *(SV**)&CvGV(bstate->bs_sv)             svindex
423 xcv_file        CvFILE(bstate->bs_sv)                   pvindex
424 xcv_depth       CvDEPTH(bstate->bs_sv)                  long
425 xcv_padlist     *(SV**)&CvPADLIST(bstate->bs_sv)        svindex
426 xcv_outside     *(SV**)&CvOUTSIDE(bstate->bs_sv)        svindex
427 xcv_outside_seq CvOUTSIDE_SEQ(bstate->bs_sv)            U32
428 xcv_flags       CvFLAGS(bstate->bs_sv)                  U16
429 av_extend       bstate->bs_sv                           SSize_t         x
430 av_pushx        bstate->bs_sv                           svindex         x
431 av_push         bstate->bs_sv                           svindex         x
432 xav_fill        AvFILLp(bstate->bs_sv)                  SSize_t
433 xav_max         AvMAX(bstate->bs_sv)                    SSize_t
434 xav_flags       AvFLAGS(bstate->bs_sv)                  U8
435 xhv_riter       HvRITER(bstate->bs_sv)                  I32
436 xhv_name        bstate->bs_sv                           pvindex         x
437 xhv_pmroot      *(OP**)&HvPMROOT(bstate->bs_sv)         opindex
438 hv_store        bstate->bs_sv                           svindex         x
439 sv_magic        bstate->bs_sv                           char            x
440 mg_obj          SvMAGIC(bstate->bs_sv)->mg_obj          svindex
441 mg_private      SvMAGIC(bstate->bs_sv)->mg_private      U16
442 mg_flags        SvMAGIC(bstate->bs_sv)->mg_flags        U8
443 mg_name         SvMAGIC(bstate->bs_sv)                  pvcontents      x
444 mg_namex        SvMAGIC(bstate->bs_sv)                  svindex         x
445 xmg_stash       bstate->bs_sv                           svindex         x
446 gv_fetchpv      bstate->bs_sv                           strconst        x
447 gv_fetchpvx     bstate->bs_sv                           strconst        x
448 gv_stashpv      bstate->bs_sv                           strconst        x
449 gv_stashpvx     bstate->bs_sv                           strconst        x
450 gp_sv           GvSV(bstate->bs_sv)                     svindex
451 gp_refcnt       GvREFCNT(bstate->bs_sv)                 U32
452 gp_refcnt_add   GvREFCNT(bstate->bs_sv)                 I32             x
453 gp_av           *(SV**)&GvAV(bstate->bs_sv)             svindex
454 gp_hv           *(SV**)&GvHV(bstate->bs_sv)             svindex
455 gp_cv           *(SV**)&GvCV(bstate->bs_sv)             svindex
456 gp_file         GvFILE(bstate->bs_sv)                   pvindex
457 gp_io           *(SV**)&GvIOp(bstate->bs_sv)            svindex
458 gp_form         *(SV**)&GvFORM(bstate->bs_sv)           svindex
459 gp_cvgen        GvCVGEN(bstate->bs_sv)                  U32
460 gp_line         GvLINE(bstate->bs_sv)                   line_t
461 gp_share        bstate->bs_sv                           svindex         x
462 xgv_flags       GvFLAGS(bstate->bs_sv)                  U8
463 op_next         PL_op->op_next                          opindex
464 op_sibling      PL_op->op_sibling                       opindex
465 op_ppaddr       PL_op->op_ppaddr                        strconst        x
466 op_targ         PL_op->op_targ                          PADOFFSET
467 op_type         PL_op                                   OPCODE          x
468 op_seq          PL_op->op_seq                           U16
469 op_flags        PL_op->op_flags                         U8
470 op_private      PL_op->op_private                       U8
471 op_first        cUNOP->op_first                         opindex
472 op_last         cBINOP->op_last                         opindex
473 op_other        cLOGOP->op_other                        opindex
474 op_pmreplroot   cPMOP->op_pmreplroot                    opindex
475 op_pmreplstart  cPMOP->op_pmreplstart                   opindex
476 op_pmnext       *(OP**)&cPMOP->op_pmnext                opindex
477 #ifdef USE_ITHREADS
478 op_pmstashpv    cPMOP                                   pvindex         x
479 op_pmreplrootpo cPMOP->op_pmreplroot                    OP*/PADOFFSET
480 #else
481 op_pmstash      *(SV**)&cPMOP->op_pmstash               svindex
482 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot            svindex
483 #endif
484 pregcomp        PL_op                                   pvcontents      x
485 op_pmflags      cPMOP->op_pmflags                       U16
486 op_pmpermflags  cPMOP->op_pmpermflags                   U16
487 op_pmdynflags   cPMOP->op_pmdynflags                    U8
488 op_sv           cSVOP->op_sv                            svindex
489 op_padix        cPADOP->op_padix                        PADOFFSET
490 op_pv           cPVOP->op_pv                            pvcontents
491 op_pv_tr        cPVOP->op_pv                            op_tr_array
492 op_redoop       cLOOP->op_redoop                        opindex
493 op_nextop       cLOOP->op_nextop                        opindex
494 op_lastop       cLOOP->op_lastop                        opindex
495 cop_label       cCOP->cop_label                         pvindex
496 #ifdef USE_ITHREADS
497 cop_stashpv     cCOP                                    pvindex         x
498 cop_file        cCOP                                    pvindex         x
499 #else
500 cop_stash       cCOP                                    svindex         x
501 cop_filegv      cCOP                                    svindex         x
502 #endif
503 cop_seq         cCOP->cop_seq                           U32
504 cop_arybase     cCOP                                    I32             x
505 cop_line        cCOP->cop_line                          line_t
506 cop_io          cCOP->cop_io                            svindex
507 cop_warnings    cCOP->cop_warnings                      svindex
508 main_start      PL_main_start                           opindex
509 main_root       PL_main_root                            opindex
510 main_cv         *(SV**)&PL_main_cv                      svindex
511 curpad          PL_curpad                               svindex         x
512 push_begin      PL_beginav                              svindex         x
513 push_init       PL_initav                               svindex         x
514 push_end        PL_endav                                svindex         x
515 curstash        *(SV**)&PL_curstash                     svindex
516 defstash        *(SV**)&PL_defstash                     svindex
517 data            none                                    U8              x
518 incav           *(SV**)&GvAV(PL_incgv)                  svindex
519 load_glob       none                                    svindex         x
520 #ifdef USE_ITHREADS
521 regex_padav     *(SV**)&PL_regex_padav                  svindex
522 #endif
523 dowarn          PL_dowarn                               U8
524 comppad_name    *(SV**)&PL_comppad_name                 svindex
525 xgv_stash       *(SV**)&GvSTASH(bstate->bs_sv)          svindex
526 signal          bstate->bs_sv                           strconst        x
527 # to be removed
528 formfeed        PL_formfeed                             svindex