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