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