Teach checkAUTHORS.pl about Sarathy's new email address.
[p5sagit/p5-mst-13.2.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 @optype= qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP);
14
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);
19
20 my (%alias_from, $from, $tos);
21 while (($from, $tos) = each %alias_to) {
22     map { $alias_from{$_} = $from } @$tos;
23 }
24
25 my $c_header = <<'EOT';
26 /* -*- buffer-read-only: t -*-
27  *
28  *      Copyright (c) 1996-1999 Malcolm Beattie
29  *
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.
32  *
33  */
34 /*
35  * This file is autogenerated from bytecode.pl. Changes made here will be lost.
36  */
37 EOT
38
39 my $perl_header;
40 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
41
42 safer_unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
43
44 #
45 # Start with boilerplate for Asmdata.pm
46 #
47 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
48 binmode ASMDATA_PM;
49 print ASMDATA_PM $perl_header, <<'EOT';
50 package B::Asmdata;
51
52 our $VERSION = '1.01';
53
54 use Exporter;
55 @ISA = qw(Exporter);
56 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
57 our(%insn_data, @insn_name, @optype, @specialsv_name);
58
59 EOT
60 print ASMDATA_PM <<"EOT";
61 \@optype = qw(@optype);
62 \@specialsv_name = qw(@specialsv);
63
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.
67 EOT
68
69 #
70 # Boilerplate for byterun.c
71 #
72 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
73 binmode BYTERUN_C;
74 print BYTERUN_C $c_header, <<'EOT';
75
76 #define PERL_NO_GET_CONTEXT
77 #include "EXTERN.h"
78 #include "perl.h"
79 #define NO_XSLOCKS
80 #include "XSUB.h"
81
82 #include "byterun.h"
83 #include "bytecode.h"
84
85
86 static const int optype_size[] = {
87 EOT
88 my $i = 0;
89 for ($i = 0; $i < @optype - 1; $i++) {
90     printf BYTERUN_C "    sizeof(%s),\n", $optype[$i], $i;
91 }
92 printf BYTERUN_C "    sizeof(%s)\n", $optype[$i], $i;
93
94 my $size = @specialsv;
95
96 print BYTERUN_C <<"EOT";
97 };
98
99 void *
100 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
101 {
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;
105     }
106     bstate->bs_obj_list[ix] = obj;
107     return obj;
108 }
109
110 int
111 byterun(pTHX_ register struct byteloader_state *bstate)
112 {
113     dVAR;
114     register int insn;
115     U32 ix;
116     SV *specialsv_list[$size];
117
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 */
122     bstate->bs_ix = 1;
123
124 EOT
125
126 for my $i ( 0 .. $#specialsv ) {
127     print BYTERUN_C "    specialsv_list[$i] = $specialsv[$i];\n";
128 }
129
130 print BYTERUN_C <<'EOT';
131
132     while ((insn = BGET_FGETC()) != EOF) {
133         switch (insn) {
134 EOT
135
136
137 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
138
139 while (<DATA>) {
140     if (/^\s*#/) {
141         print BYTERUN_C if /^\s*#\s*(?:if|endif|el)/;
142         next;
143     }
144     chop;
145     next unless length;
146     if (/^%number\s+(.*)/) {
147         $insn_num = $1;
148         next;
149     } elsif (/%enum\s+(.*?)\s+(.*)/) {
150         create_enum($1, $2);    # must come before instructions
151         next;
152     }
153     ($insn, $lvalue, $argtype, $flags) = split;
154     my $rvalcast = '';
155     if ($argtype =~ m:(.+)/(.+):) {
156         ($rvalcast, $argtype) = ("($1)", $2);
157     }
158     $insn_name[$insn_num] = $insn;
159     $fundtype = $alias_from{$argtype} || $argtype;
160
161     #
162     # Add the case statement and code for the bytecode interpreter in byterun.c
163     #
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";
167     if ($optarg) {
168         printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
169     }
170     if ($flags =~ /x/) {
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";
175     }
176     elsif ($optarg && $lvalue ne "none") {
177         print BYTERUN_C "\t\t$lvalue = ${rvalcast}arg;\n";
178     }
179     print BYTERUN_C "\t\tbreak;\n\t    }\n";
180
181     #
182     # Add the initialiser line for %insn_data in Asmdata.pm
183     #
184     print ASMDATA_PM <<"EOT";
185 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
186 EOT
187
188     # Find the next unused instruction number
189     do { $insn_num++ } while $insn_name[$insn_num];
190 }
191
192 #
193 # Finish off byterun.c
194 #
195 print BYTERUN_C <<'EOT';
196           default:
197             Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
198             /* NOTREACHED */
199         }
200     }
201     return 0;
202 }
203
204 /* ex: set ro: */
205 EOT
206
207 #
208 # Write the instruction and optype enum constants into byterun.h
209 #
210 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
211 binmode BYTERUN_H;
212 print BYTERUN_H $c_header, <<'EOT';
213 struct byteloader_fdata {
214     SV  *datasv;
215     int next_out;
216     int idx;
217 };
218
219 struct byteloader_pv_state {
220     char                        *pvx;
221     XPV                         xpv;
222 };
223
224 struct byteloader_state {
225     struct byteloader_fdata     *bs_fdata;
226     SV                          *bs_sv;
227     void                        **bs_obj_list;
228     int                         bs_obj_list_fill;
229     int                         bs_ix;
230     struct byteloader_pv_state  bs_pv;
231     int                         bs_iv_overflows;
232 };
233
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 *);
237
238 enum {
239 EOT
240
241 my $add_enum_value = 0;
242 my $max_insn;
243 for $i ( 0 .. $#insn_name ) {
244     $insn = uc($insn_name[$i]);
245     if (defined($insn)) {
246         $max_insn = $i;
247         if ($add_enum_value) {
248             print BYTERUN_H "    INSN_$insn = $i,\t\t\t/* $i */\n";
249             $add_enum_value = 0;
250         } else {
251             print BYTERUN_H "    INSN_$insn,\t\t\t/* $i */\n";
252         }
253     } else {
254         $add_enum_value = 1;
255     }
256 }
257
258 print BYTERUN_H "    MAX_INSN = $max_insn\n};\n";
259
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;
263 }
264 printf BYTERUN_H "    OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
265
266 print BYTERUN_H "/* ex: set ro: */\n";
267
268 #
269 # Finish off insn_data and create array initialisers in Asmdata.pm
270 #
271 print ASMDATA_PM <<'EOT';
272
273 my ($insn_name, $insn_data);
274 while (($insn_name, $insn_data) = each %insn_data) {
275     $insn_name[$insn_data->[0]] = $insn_name;
276 }
277 # Fill in any gaps
278 @insn_name = map($_ || "unused", @insn_name);
279
280 1;
281
282 __END__
283
284 =head1 NAME
285
286 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
287
288 =head1 SYNOPSIS
289
290         use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
291
292 =head1 DESCRIPTION
293
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
296 B::Disassembler.
297
298 =over 4
299
300 =item %insn_data
301
302   my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
303
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
307 used to 'GET'.
308
309 =for _private
310 Add more detail about what $put_sub and $get_meth are and how to use them.
311
312 =item @insn_name
313
314   my $op_name = $insn_name[$bytecode_num];
315
316 A simple mapping of the bytecode number to the name of the op.
317 Suitable for using with %insn_data like so:
318
319   my $op_info = $insn_data{$insn_name[$bytecode_num]};
320
321 =item @optype
322
323   my $op_type = $optype[$op_type_num];
324
325 A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
326
327 =item @specialsv_name
328
329   my $sv_name = $specialsv_name[$sv_index];
330
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'
334 or '&PL_sv_undef').
335
336 =back
337
338 =head1 AUTHOR
339
340 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
341
342 =cut
343
344 # ex: set ro:
345 EOT
346
347
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: $!";
351
352 __END__
353 # First set instruction ord("#") to read comment to end-of-line (sneaky)
354 %number 35
355 comment         arg                     comment_t
356 # Then make ord("\n") into a no-op
357 %number 10
358 nop             none                    none
359
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.
362 %number 0
363 #
364 # The argtype is either a single type or "rightvaluecast/argtype".
365 #
366 #opcode         lvalue                                  argtype         flags   
367 #
368 ret             none                                    none            x
369 ldsv            bstate->bs_sv                           svindex
370 ldop            PL_op                                   opindex
371 stsv            bstate->bs_sv                           U32             s
372 stop            PL_op                                   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
378 newop           PL_op                                   U8              x
379 newopx          PL_op                                   U16             x
380 newopn          PL_op                                   U8              x
381 newpv           none                                    PV
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
473 #ifdef USE_ITHREADS
474 op_pmstashpv    cPMOP                                   pvindex         x
475 op_pmreplrootpo cPMOP->op_pmreplroot                    OP*/PADOFFSET
476 #else
477 op_pmstash      *(SV**)&cPMOP->op_pmstash               svindex
478 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot            svindex
479 #endif
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
492 #ifdef USE_ITHREADS
493 cop_stashpv     cCOP                                    pvindex         x
494 cop_file        cCOP                                    pvindex         x
495 #else
496 cop_stash       cCOP                                    svindex         x
497 cop_filegv      cCOP                                    svindex         x
498 #endif
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
513 data            none                                    U8              x
514 incav           *(SV**)&GvAV(PL_incgv)                  svindex
515 load_glob       none                                    svindex         x
516 #ifdef USE_ITHREADS
517 regex_padav     *(SV**)&PL_regex_padav                  svindex
518 #endif
519 dowarn          PL_dowarn                               U8
520 comppad_name    *(SV**)&PL_comppad_name                 svindex
521 xgv_stash       *(SV**)&GvSTASH(bstate->bs_sv)          svindex
522 signal          bstate->bs_sv                           strconst        x
523 # to be removed
524 formfeed        PL_formfeed                             svindex