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