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