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