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