make autogenerated files writable
[p5sagit/p5-mst-13.2.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 CONDOP LISTOP PMOP SVOP GVOP 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);
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 use vars qw(%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 #include "EXTERN.h"
69 #include "perl.h"
70 #define NO_XSLOCKS
71 #include "XSUB.h"
72
73 #ifdef PERL_OBJECT
74 #undef CALL_FPTR
75 #define CALL_FPTR(fptr) (pPerl->*fptr)
76 #undef PL_ppaddr
77 #define PL_ppaddr (*get_ppaddr())
78 #endif
79
80 #include "byterun.h"
81 #include "bytecode.h"
82
83
84 static 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 static SV *specialsv_list[4];
95
96 static int bytecode_iv_overflows = 0;
97 static SV *bytecode_sv;
98 static XPV bytecode_pv;
99 static void **bytecode_obj_list;
100 static I32 bytecode_obj_list_fill = -1;
101
102 void *
103 bset_obj_store(pTHXo_ void *obj, I32 ix)
104 {
105     if (ix > bytecode_obj_list_fill) {
106         if (bytecode_obj_list_fill == -1)
107             New(666, bytecode_obj_list, ix + 1, void*);
108         else
109             Renew(bytecode_obj_list, ix + 1, void*);
110         bytecode_obj_list_fill = ix;
111     }
112     bytecode_obj_list[ix] = obj;
113     return obj;
114 }
115
116 void
117 byterun(pTHXo_ struct bytestream bs)
118 {
119     dTHR;
120     int insn;
121
122 EOT
123
124 for (my $i = 0; $i < @specialsv; $i++) {
125     print BYTERUN_C "    specialsv_list[$i] = $specialsv[$i];\n";
126 }
127
128 print BYTERUN_C <<'EOT';
129
130     while ((insn = BGET_FGETC()) != EOF) {
131         switch (insn) {
132 EOT
133
134
135 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
136
137 while (<DATA>) {
138     chop;
139     s/#.*//;                    # remove comments
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     $insn_name[$insn_num] = $insn;
150     $fundtype = $alias_from{$argtype} || $argtype;
151
152     #
153     # Add the case statement and code for the bytecode interpreter in byterun.c
154     #
155     printf BYTERUN_C "\t  case INSN_%s:\t\t/* %d */\n\t    {\n",
156         uc($insn), $insn_num;
157     my $optarg = $argtype eq "none" ? "" : ", arg";
158     if ($optarg) {
159         printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
160     }
161     if ($flags =~ /x/) {
162         print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
163     } elsif ($flags =~ /s/) {
164         # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
165         print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
166     }
167     elsif ($optarg && $lvalue ne "none") {
168         print BYTERUN_C "\t\t$lvalue = arg;\n";
169     }
170     print BYTERUN_C "\t\tbreak;\n\t    }\n";
171
172     #
173     # Add the initialiser line for %insn_data in Asmdata.pm
174     #
175     print ASMDATA_PM <<"EOT";
176 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
177 EOT
178
179     # Find the next unused instruction number
180     do { $insn_num++ } while $insn_name[$insn_num];
181 }
182
183 #
184 # Finish off byterun.c
185 #
186 print BYTERUN_C <<'EOT';
187           default:
188             Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
189             /* NOTREACHED */
190         }
191     }
192 }
193 EOT
194
195 #
196 # Write the instruction and optype enum constants into byterun.h
197 #
198 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
199 print BYTERUN_H $c_header, <<'EOT';
200 struct bytestream {
201     void *data;
202     int (*pfgetc)(void *);
203     int (*pfread)(char *, size_t, size_t, void *);
204     void (*pfreadpv)(U32, void *, XPV *);
205 };
206
207 enum {
208 EOT
209
210 my $i = 0;
211 my $add_enum_value = 0;
212 my $max_insn;
213 for ($i = 0; $i < @insn_name; $i++) {
214     $insn = uc($insn_name[$i]);
215     if (defined($insn)) {
216         $max_insn = $i;
217         if ($add_enum_value) {
218             print BYTERUN_H "    INSN_$insn = $i,\t\t\t/* $i */\n";
219             $add_enum_value = 0;
220         } else {
221             print BYTERUN_H "    INSN_$insn,\t\t\t/* $i */\n";
222         }
223     } else {
224         $add_enum_value = 1;
225     }
226 }
227
228 print BYTERUN_H "    MAX_INSN = $max_insn\n};\n";
229
230 print BYTERUN_H "\nenum {\n";
231 for ($i = 0; $i < @optype - 1; $i++) {
232     printf BYTERUN_H "    OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
233 }
234 printf BYTERUN_H "    OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
235 print BYTERUN_H <<'EOT';
236 EXT int PL_optype_size[]
237 #ifdef DOINIT
238 = {
239 EOT
240 for ($i = 0; $i < @optype - 1; $i++) {
241     printf BYTERUN_H "    sizeof(%s),\n", $optype[$i], $i;
242 }
243 printf BYTERUN_H "    sizeof(%s)\n}\n", $optype[$i], $i;
244 print BYTERUN_H <<'EOT';
245 #endif /* DOINIT */
246 ;
247
248 EOT
249
250 print BYTERUN_H <<'EOT';
251 EXT void byterun(pTHXo_ struct bytestream bs);
252
253 #define INIT_SPECIALSV_LIST STMT_START { \
254 EOT
255 for ($i = 0; $i < @specialsv; $i++) {
256     print BYTERUN_H "\tPL_specialsv_list[$i] = $specialsv[$i]; \\\n";
257 }
258 print BYTERUN_H <<'EOT';
259     } STMT_END
260 EOT
261
262 #
263 # Finish off insn_data and create array initialisers in Asmdata.pm
264 #
265 print ASMDATA_PM <<'EOT';
266
267 my ($insn_name, $insn_data);
268 while (($insn_name, $insn_data) = each %insn_data) {
269     $insn_name[$insn_data->[0]] = $insn_name;
270 }
271 # Fill in any gaps
272 @insn_name = map($_ || "unused", @insn_name);
273
274 1;
275
276 __END__
277
278 =head1 NAME
279
280 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
281
282 =head1 SYNOPSIS
283
284         use Asmdata;
285
286 =head1 DESCRIPTION
287
288 See F<ext/B/B/Asmdata.pm>.
289
290 =head1 AUTHOR
291
292 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
293
294 =cut
295 EOT
296
297 __END__
298 # First set instruction ord("#") to read comment to end-of-line (sneaky)
299 %number 35
300 comment         arg                     comment_t
301 # Then make ord("\n") into a no-op
302 %number 10
303 nop             none                    none
304 # Now for the rest of the ordinary ones, beginning with \0 which is
305 # ret so that \0-terminated strings can be read properly as bytecode.
306 %number 0
307 #
308 #opcode         lvalue                                  argtype         flags   
309 #
310 ret             none                                    none            x
311 ldsv            bytecode_sv                             svindex
312 ldop            PL_op                                   opindex
313 stsv            bytecode_sv                             U32             s
314 stop            PL_op                                   U32             s
315 ldspecsv        bytecode_sv                             U8              x
316 newsv           bytecode_sv                             U8              x
317 newop           PL_op                                   U8              x
318 newopn          PL_op                                   U8              x
319 newpv           none                                    PV
320 pv_cur          bytecode_pv.xpv_cur                     STRLEN
321 pv_free         bytecode_pv                             none            x
322 sv_upgrade      bytecode_sv                             char            x
323 sv_refcnt       SvREFCNT(bytecode_sv)                   U32
324 sv_refcnt_add   SvREFCNT(bytecode_sv)                   I32             x
325 sv_flags        SvFLAGS(bytecode_sv)                    U32
326 xrv             SvRV(bytecode_sv)                       svindex
327 xpv             bytecode_sv                             none            x
328 xiv32           SvIVX(bytecode_sv)                      I32
329 xiv64           SvIVX(bytecode_sv)                      IV64
330 xnv             SvNVX(bytecode_sv)                      double
331 xlv_targoff     LvTARGOFF(bytecode_sv)                  STRLEN
332 xlv_targlen     LvTARGLEN(bytecode_sv)                  STRLEN
333 xlv_targ        LvTARG(bytecode_sv)                     svindex
334 xlv_type        LvTYPE(bytecode_sv)                     char
335 xbm_useful      BmUSEFUL(bytecode_sv)                   I32
336 xbm_previous    BmPREVIOUS(bytecode_sv)                 U16
337 xbm_rare        BmRARE(bytecode_sv)                     U8
338 xfm_lines       FmLINES(bytecode_sv)                    I32
339 xio_lines       IoLINES(bytecode_sv)                    long
340 xio_page        IoPAGE(bytecode_sv)                     long
341 xio_page_len    IoPAGE_LEN(bytecode_sv)                 long
342 xio_lines_left  IoLINES_LEFT(bytecode_sv)               long
343 xio_top_name    IoTOP_NAME(bytecode_sv)                 pvcontents
344 xio_top_gv      *(SV**)&IoTOP_GV(bytecode_sv)           svindex
345 xio_fmt_name    IoFMT_NAME(bytecode_sv)                 pvcontents
346 xio_fmt_gv      *(SV**)&IoFMT_GV(bytecode_sv)           svindex
347 xio_bottom_name IoBOTTOM_NAME(bytecode_sv)              pvcontents
348 xio_bottom_gv   *(SV**)&IoBOTTOM_GV(bytecode_sv)        svindex
349 xio_subprocess  IoSUBPROCESS(bytecode_sv)               short
350 xio_type        IoTYPE(bytecode_sv)                     char
351 xio_flags       IoFLAGS(bytecode_sv)                    char
352 xcv_stash       *(SV**)&CvSTASH(bytecode_sv)            svindex
353 xcv_start       CvSTART(bytecode_sv)                    opindex
354 xcv_root        CvROOT(bytecode_sv)                     opindex
355 xcv_gv          *(SV**)&CvGV(bytecode_sv)               svindex
356 xcv_filegv      *(SV**)&CvFILEGV(bytecode_sv)           svindex
357 xcv_depth       CvDEPTH(bytecode_sv)                    long
358 xcv_padlist     *(SV**)&CvPADLIST(bytecode_sv)          svindex
359 xcv_outside     *(SV**)&CvOUTSIDE(bytecode_sv)          svindex
360 xcv_flags       CvFLAGS(bytecode_sv)                    U8
361 av_extend       bytecode_sv                             SSize_t         x
362 av_push         bytecode_sv                             svindex         x
363 xav_fill        AvFILLp(bytecode_sv)                    SSize_t
364 xav_max         AvMAX(bytecode_sv)                      SSize_t
365 xav_flags       AvFLAGS(bytecode_sv)                    U8
366 xhv_riter       HvRITER(bytecode_sv)                    I32
367 xhv_name        HvNAME(bytecode_sv)                     pvcontents
368 hv_store        bytecode_sv                             svindex         x
369 sv_magic        bytecode_sv                             char            x
370 mg_obj          SvMAGIC(bytecode_sv)->mg_obj            svindex
371 mg_private      SvMAGIC(bytecode_sv)->mg_private        U16
372 mg_flags        SvMAGIC(bytecode_sv)->mg_flags          U8
373 mg_pv           SvMAGIC(bytecode_sv)                    pvcontents      x
374 xmg_stash       *(SV**)&SvSTASH(bytecode_sv)            svindex
375 gv_fetchpv      bytecode_sv                             strconst        x
376 gv_stashpv      bytecode_sv                             strconst        x
377 gp_sv           GvSV(bytecode_sv)                       svindex
378 gp_refcnt       GvREFCNT(bytecode_sv)                   U32
379 gp_refcnt_add   GvREFCNT(bytecode_sv)                   I32             x
380 gp_av           *(SV**)&GvAV(bytecode_sv)               svindex
381 gp_hv           *(SV**)&GvHV(bytecode_sv)               svindex
382 gp_cv           *(SV**)&GvCV(bytecode_sv)               svindex
383 gp_filegv       *(SV**)&GvFILEGV(bytecode_sv)           svindex
384 gp_io           *(SV**)&GvIOp(bytecode_sv)              svindex
385 gp_form         *(SV**)&GvFORM(bytecode_sv)             svindex
386 gp_cvgen        GvCVGEN(bytecode_sv)                    U32
387 gp_line         GvLINE(bytecode_sv)                     line_t
388 gp_share        bytecode_sv                             svindex         x
389 xgv_flags       GvFLAGS(bytecode_sv)                    U8
390 op_next         PL_op->op_next                          opindex
391 op_sibling      PL_op->op_sibling                       opindex
392 op_ppaddr       PL_op->op_ppaddr                        strconst        x
393 op_targ         PL_op->op_targ                          PADOFFSET
394 op_type         PL_op                                   OPCODE          x
395 op_seq          PL_op->op_seq                           U16
396 op_flags        PL_op->op_flags                         U8
397 op_private      PL_op->op_private                       U8
398 op_first        cUNOP->op_first                         opindex
399 op_last         cBINOP->op_last                         opindex
400 op_other        cLOGOP->op_other                        opindex
401 op_true         cCONDOP->op_true                        opindex
402 op_false        cCONDOP->op_false                       opindex
403 op_children     cLISTOP->op_children                    U32
404 op_pmreplroot   cPMOP->op_pmreplroot                    opindex
405 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot            svindex
406 op_pmreplstart  cPMOP->op_pmreplstart                   opindex
407 op_pmnext       *(OP**)&cPMOP->op_pmnext                opindex
408 pregcomp        PL_op                                   pvcontents      x
409 op_pmflags      cPMOP->op_pmflags                       U16
410 op_pmpermflags  cPMOP->op_pmpermflags                   U16
411 op_sv           cSVOP->op_sv                            svindex
412 op_gv           *(SV**)&cGVOP->op_gv                    svindex
413 op_pv           cPVOP->op_pv                            pvcontents
414 op_pv_tr        cPVOP->op_pv                            op_tr_array
415 op_redoop       cLOOP->op_redoop                        opindex
416 op_nextop       cLOOP->op_nextop                        opindex
417 op_lastop       cLOOP->op_lastop                        opindex
418 cop_label       cCOP->cop_label                         pvcontents
419 cop_stash       *(SV**)&cCOP->cop_stash                 svindex
420 cop_filegv      *(SV**)&cCOP->cop_filegv                svindex
421 cop_seq         cCOP->cop_seq                           U32
422 cop_arybase     cCOP->cop_arybase                       I32
423 cop_line        cCOP->cop_line                          line_t
424 cop_warnings    cCOP->cop_warnings                      svindex
425 main_start      PL_main_start                           opindex
426 main_root       PL_main_root                            opindex
427 curpad          PL_curpad                               svindex         x