This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline
[perl5.git] / bytecode.pl
1 BEGIN {
2   push @INC, './lib';
3 }
4 use strict;
5 my %alias_to = (
6     U32 => [qw(PADOFFSET STRLEN)],
7     I32 => [qw(SSize_t long)],
8     U16 => [qw(OPCODE line_t short)],
9     U8 => [qw(char)],
10 );
11
12 my @optype= qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP);
13
14 # Nullsv *must* come first in the following so that the condition
15 # ($$sv == 0) can continue to be used to test (sv == Nullsv).
16 my @specialsv = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no pWARN_ALL pWARN_NONE);
17
18 my (%alias_from, $from, $tos);
19 while (($from, $tos) = each %alias_to) {
20     map { $alias_from{$_} = $from } @$tos;
21 }
22
23 my $c_header = <<'EOT';
24 /*
25  *      Copyright (c) 1996-1999 Malcolm Beattie
26  *
27  *      You may distribute under the terms of either the GNU General Public
28  *      License or the Artistic License, as specified in the README file.
29  *
30  */
31 /*
32  * This file is autogenerated from bytecode.pl. Changes made here will be lost.
33  */
34 EOT
35
36 my $perl_header;
37 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
38
39 unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
40
41 #
42 # Start with boilerplate for Asmdata.pm
43 #
44 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
45 print ASMDATA_PM $perl_header, <<'EOT';
46 package B::Asmdata;
47 use Exporter;
48 @ISA = qw(Exporter);
49 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
50 our(%insn_data, @insn_name, @optype, @specialsv_name);
51
52 EOT
53 print ASMDATA_PM <<"EOT";
54 \@optype = qw(@optype);
55 \@specialsv_name = qw(@specialsv);
56
57 # XXX insn_data is initialised this way because with a large
58 # %insn_data = (foo => [...], bar => [...], ...) initialiser
59 # I get a hard-to-track-down stack underflow and segfault.
60 EOT
61
62 #
63 # Boilerplate for byterun.c
64 #
65 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
66 print BYTERUN_C $c_header, <<'EOT';
67
68 #define PERL_NO_GET_CONTEXT
69 #include "EXTERN.h"
70 #include "perl.h"
71 #define NO_XSLOCKS
72 #include "XSUB.h"
73
74 #include "byterun.h"
75 #include "bytecode.h"
76
77
78 static const int optype_size[] = {
79 EOT
80 my $i = 0;
81 for ($i = 0; $i < @optype - 1; $i++) {
82     printf BYTERUN_C "    sizeof(%s),\n", $optype[$i], $i;
83 }
84 printf BYTERUN_C "    sizeof(%s)\n", $optype[$i], $i;
85 print BYTERUN_C <<'EOT';
86 };
87
88 void *
89 bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
90 {
91     if (ix > bstate->bs_obj_list_fill) {
92         Renew(bstate->bs_obj_list, ix + 32, void*);
93         bstate->bs_obj_list_fill = ix + 31;
94     }
95     bstate->bs_obj_list[ix] = obj;
96     return obj;
97 }
98
99 void
100 byterun(pTHX_ register struct byteloader_state *bstate)
101 {
102     register int insn;
103     U32 ix;
104     SV *specialsv_list[6];
105
106     BYTECODE_HEADER_CHECK;      /* croak if incorrect platform */
107     New(666, bstate->bs_obj_list, 32, void*); /* set op objlist */
108     bstate->bs_obj_list_fill = 31;
109
110 EOT
111
112 for (my $i = 0; $i < @specialsv; $i++) {
113     print BYTERUN_C "    specialsv_list[$i] = $specialsv[$i];\n";
114 }
115
116 print BYTERUN_C <<'EOT';
117
118     while ((insn = BGET_FGETC()) != EOF) {
119         switch (insn) {
120 EOT
121
122
123 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
124
125 while (<DATA>) {
126     chop;
127     s/#.*//;                    # remove comments
128     next unless length;
129     if (/^%number\s+(.*)/) {
130         $insn_num = $1;
131         next;
132     } elsif (/%enum\s+(.*?)\s+(.*)/) {
133         create_enum($1, $2);    # must come before instructions
134         next;
135     }
136     ($insn, $lvalue, $argtype, $flags) = split;
137     $insn_name[$insn_num] = $insn;
138     $fundtype = $alias_from{$argtype} || $argtype;
139
140     #
141     # Add the case statement and code for the bytecode interpreter in byterun.c
142     #
143     printf BYTERUN_C "\t  case INSN_%s:\t\t/* %d */\n\t    {\n",
144         uc($insn), $insn_num;
145     my $optarg = $argtype eq "none" ? "" : ", arg";
146     if ($optarg) {
147         printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
148     }
149     if ($flags =~ /x/) {
150         print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
151     } elsif ($flags =~ /s/) {
152         # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
153         print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
154     }
155     elsif ($optarg && $lvalue ne "none") {
156         print BYTERUN_C "\t\t$lvalue = arg;\n";
157     }
158     print BYTERUN_C "\t\tbreak;\n\t    }\n";
159
160     #
161     # Add the initialiser line for %insn_data in Asmdata.pm
162     #
163     print ASMDATA_PM <<"EOT";
164 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
165 EOT
166
167     # Find the next unused instruction number
168     do { $insn_num++ } while $insn_name[$insn_num];
169 }
170
171 #
172 # Finish off byterun.c
173 #
174 print BYTERUN_C <<'EOT';
175           default:
176             Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
177             /* NOTREACHED */
178         }
179     }
180 }
181 EOT
182
183 #
184 # Write the instruction and optype enum constants into byterun.h
185 #
186 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
187 print BYTERUN_H $c_header, <<'EOT';
188 struct byteloader_fdata {
189     SV  *datasv;
190     int next_out;
191     int idx;
192 };
193
194 struct byteloader_state {
195     struct byteloader_fdata     *bs_fdata;
196     SV                          *bs_sv;
197     void                        **bs_obj_list;
198     int                         bs_obj_list_fill;
199     XPV                         bs_pv;
200     int                         bs_iv_overflows;
201 };
202
203 int bl_getc(struct byteloader_fdata *);
204 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
205 extern void byterun(pTHX_ struct byteloader_state *);
206
207 enum {
208 EOT
209
210 my $add_enum_value = 0;
211 my $max_insn;
212 for ($i = 0; $i < @insn_name; $i++) {
213     $insn = uc($insn_name[$i]);
214     if (defined($insn)) {
215         $max_insn = $i;
216         if ($add_enum_value) {
217             print BYTERUN_H "    INSN_$insn = $i,\t\t\t/* $i */\n";
218             $add_enum_value = 0;
219         } else {
220             print BYTERUN_H "    INSN_$insn,\t\t\t/* $i */\n";
221         }
222     } else {
223         $add_enum_value = 1;
224     }
225 }
226
227 print BYTERUN_H "    MAX_INSN = $max_insn\n};\n";
228
229 print BYTERUN_H "\nenum {\n";
230 for ($i = 0; $i < @optype - 1; $i++) {
231     printf BYTERUN_H "    OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
232 }
233 printf BYTERUN_H "    OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
234
235 #
236 # Finish off insn_data and create array initialisers in Asmdata.pm
237 #
238 print ASMDATA_PM <<'EOT';
239
240 my ($insn_name, $insn_data);
241 while (($insn_name, $insn_data) = each %insn_data) {
242     $insn_name[$insn_data->[0]] = $insn_name;
243 }
244 # Fill in any gaps
245 @insn_name = map($_ || "unused", @insn_name);
246
247 1;
248
249 __END__
250
251 =head1 NAME
252
253 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
254
255 =head1 SYNOPSIS
256
257         use Asmdata;
258
259 =head1 DESCRIPTION
260
261 See F<ext/B/B/Asmdata.pm>.
262
263 =head1 AUTHOR
264
265 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
266
267 =cut
268 EOT
269
270 __END__
271 # First set instruction ord("#") to read comment to end-of-line (sneaky)
272 %number 35
273 comment         arg                     comment_t
274 # Then make ord("\n") into a no-op
275 %number 10
276 nop             none                    none
277 # Now for the rest of the ordinary ones, beginning with \0 which is
278 # ret so that \0-terminated strings can be read properly as bytecode.
279 %number 0
280 #
281 #opcode         lvalue                                  argtype         flags   
282 #
283 ret             none                                    none            x
284 ldsv            bstate->bs_sv                           svindex
285 ldop            PL_op                                   opindex
286 stsv            bstate->bs_sv                           U32             s
287 stop            PL_op                                   U32             s
288 stpv            bstate->bs_pv.xpv_pv                    U32             x
289 ldspecsv        bstate->bs_sv                           U8              x
290 newsv           bstate->bs_sv                           U8              x
291 newop           PL_op                                   U8              x
292 newopn          PL_op                                   U8              x
293 newpv           none                                    PV
294 pv_cur          bstate->bs_pv.xpv_cur                   STRLEN
295 pv_free         bstate->bs_pv                           none            x
296 sv_upgrade      bstate->bs_sv                           char            x
297 sv_refcnt       SvREFCNT(bstate->bs_sv)                 U32
298 sv_refcnt_add   SvREFCNT(bstate->bs_sv)                 I32             x
299 sv_flags        SvFLAGS(bstate->bs_sv)                  U32
300 xrv             SvRV(bstate->bs_sv)                     svindex
301 xpv             bstate->bs_sv                           none            x
302 xiv32           SvIVX(bstate->bs_sv)                    I32
303 xiv64           SvIVX(bstate->bs_sv)                    IV64
304 xnv             SvNVX(bstate->bs_sv)                    NV
305 xlv_targoff     LvTARGOFF(bstate->bs_sv)                STRLEN
306 xlv_targlen     LvTARGLEN(bstate->bs_sv)                STRLEN
307 xlv_targ        LvTARG(bstate->bs_sv)                   svindex
308 xlv_type        LvTYPE(bstate->bs_sv)                   char
309 xbm_useful      BmUSEFUL(bstate->bs_sv)                 I32
310 xbm_previous    BmPREVIOUS(bstate->bs_sv)               U16
311 xbm_rare        BmRARE(bstate->bs_sv)                   U8
312 xfm_lines       FmLINES(bstate->bs_sv)                  I32
313 xio_lines       IoLINES(bstate->bs_sv)                  long
314 xio_page        IoPAGE(bstate->bs_sv)                   long
315 xio_page_len    IoPAGE_LEN(bstate->bs_sv)               long
316 xio_lines_left  IoLINES_LEFT(bstate->bs_sv)             long
317 xio_top_name    IoTOP_NAME(bstate->bs_sv)               pvcontents
318 xio_top_gv      *(SV**)&IoTOP_GV(bstate->bs_sv)         svindex
319 xio_fmt_name    IoFMT_NAME(bstate->bs_sv)               pvcontents
320 xio_fmt_gv      *(SV**)&IoFMT_GV(bstate->bs_sv)         svindex
321 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv)            pvcontents
322 xio_bottom_gv   *(SV**)&IoBOTTOM_GV(bstate->bs_sv)      svindex
323 xio_subprocess  IoSUBPROCESS(bstate->bs_sv)             short
324 xio_type        IoTYPE(bstate->bs_sv)                   char
325 xio_flags       IoFLAGS(bstate->bs_sv)                  char
326 xcv_stash       *(SV**)&CvSTASH(bstate->bs_sv)          svindex
327 xcv_start       CvSTART(bstate->bs_sv)                  opindex
328 xcv_root        CvROOT(bstate->bs_sv)                   opindex
329 xcv_gv          *(SV**)&CvGV(bstate->bs_sv)             svindex
330 xcv_file        CvFILE(bstate->bs_sv)                   pvindex
331 xcv_depth       CvDEPTH(bstate->bs_sv)                  long
332 xcv_padlist     *(SV**)&CvPADLIST(bstate->bs_sv)        svindex
333 xcv_outside     *(SV**)&CvOUTSIDE(bstate->bs_sv)        svindex
334 xcv_flags       CvFLAGS(bstate->bs_sv)                  U16
335 av_extend       bstate->bs_sv                           SSize_t         x
336 av_push         bstate->bs_sv                           svindex         x
337 xav_fill        AvFILLp(bstate->bs_sv)                  SSize_t
338 xav_max         AvMAX(bstate->bs_sv)                    SSize_t
339 xav_flags       AvFLAGS(bstate->bs_sv)                  U8
340 xhv_riter       HvRITER(bstate->bs_sv)                  I32
341 xhv_name        HvNAME(bstate->bs_sv)                   pvcontents
342 hv_store        bstate->bs_sv                           svindex         x
343 sv_magic        bstate->bs_sv                           char            x
344 mg_obj          SvMAGIC(bstate->bs_sv)->mg_obj          svindex
345 mg_private      SvMAGIC(bstate->bs_sv)->mg_private      U16
346 mg_flags        SvMAGIC(bstate->bs_sv)->mg_flags        U8
347 mg_pv           SvMAGIC(bstate->bs_sv)                  pvcontents      x
348 xmg_stash       *(SV**)&SvSTASH(bstate->bs_sv)          svindex
349 gv_fetchpv      bstate->bs_sv                           strconst        x
350 gv_stashpv      bstate->bs_sv                           strconst        x
351 gp_sv           GvSV(bstate->bs_sv)                     svindex
352 gp_refcnt       GvREFCNT(bstate->bs_sv)                 U32
353 gp_refcnt_add   GvREFCNT(bstate->bs_sv)                 I32             x
354 gp_av           *(SV**)&GvAV(bstate->bs_sv)             svindex
355 gp_hv           *(SV**)&GvHV(bstate->bs_sv)             svindex
356 gp_cv           *(SV**)&GvCV(bstate->bs_sv)             svindex
357 gp_file         GvFILE(bstate->bs_sv)                   pvindex
358 gp_io           *(SV**)&GvIOp(bstate->bs_sv)            svindex
359 gp_form         *(SV**)&GvFORM(bstate->bs_sv)           svindex
360 gp_cvgen        GvCVGEN(bstate->bs_sv)                  U32
361 gp_line         GvLINE(bstate->bs_sv)                   line_t
362 gp_share        bstate->bs_sv                           svindex         x
363 xgv_flags       GvFLAGS(bstate->bs_sv)                  U8
364 op_next         PL_op->op_next                          opindex
365 op_sibling      PL_op->op_sibling                       opindex
366 op_ppaddr       PL_op->op_ppaddr                        strconst        x
367 op_targ         PL_op->op_targ                          PADOFFSET
368 op_type         PL_op                                   OPCODE          x
369 op_seq          PL_op->op_seq                           U16
370 op_flags        PL_op->op_flags                         U8
371 op_private      PL_op->op_private                       U8
372 op_first        cUNOP->op_first                         opindex
373 op_last         cBINOP->op_last                         opindex
374 op_other        cLOGOP->op_other                        opindex
375 op_pmreplroot   cPMOP->op_pmreplroot                    opindex
376 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot            svindex
377 op_pmreplstart  cPMOP->op_pmreplstart                   opindex
378 op_pmnext       *(OP**)&cPMOP->op_pmnext                opindex
379 pregcomp        PL_op                                   pvcontents      x
380 op_pmflags      cPMOP->op_pmflags                       U16
381 op_pmpermflags  cPMOP->op_pmpermflags                   U16
382 op_sv           cSVOP->op_sv                            svindex
383 op_padix        cPADOP->op_padix                        PADOFFSET
384 op_pv           cPVOP->op_pv                            pvcontents
385 op_pv_tr        cPVOP->op_pv                            op_tr_array
386 op_redoop       cLOOP->op_redoop                        opindex
387 op_nextop       cLOOP->op_nextop                        opindex
388 op_lastop       cLOOP->op_lastop                        opindex
389 cop_label       cCOP->cop_label                         pvindex
390 cop_stashpv     cCOP                                    pvindex         x
391 cop_file        cCOP                                    pvindex         x
392 cop_seq         cCOP->cop_seq                           U32
393 cop_arybase     cCOP->cop_arybase                       I32
394 cop_line        cCOP                                    line_t          x
395 cop_warnings    cCOP->cop_warnings                      svindex
396 main_start      PL_main_start                           opindex
397 main_root       PL_main_root                            opindex
398 curpad          PL_curpad                               svindex         x
399 push_begin      PL_beginav                              svindex         x
400 push_init       PL_initav                               svindex         x
401 push_end        PL_endav                                svindex         x