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