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