This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Let Lint private_names catch out-of-package _foo methods.
[perl5.git] / bytecode.pl
CommitLineData
79ee8297
MB
1use strict;
2my %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
10my @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).
14my @specialsv = qw(Nullsv &sv_undef &sv_yes &sv_no);
15
16my (%alias_from, $from, $tos);
17while (($from, $tos) = each %alias_to) {
18 map { $alias_from{$_} = $from } @$tos;
19}
20
21my $c_header = <<'EOT';
22/*
ed65db6c 23 * Copyright (c) 1996, 1997 Malcolm Beattie
79ee8297
MB
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 */
32EOT
33
34my $perl_header;
35($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
36
37if (-f "byterun.c") {
38 rename("byterun.c", "byterun.c.old");
39}
40if (-f "byterun.h") {
41 rename("byterun.h", "byterun.h.old");
42}
43if (-f "Asmdata.pm") {
44 rename("Asmdata.pm", "Asmdata.pm.old");
45}
46
47#
48# Start with boilerplate for Asmdata.pm
49#
50open(ASMDATA_PM, ">Asmdata.pm") or die "Asmdata.pm: $!";
51print ASMDATA_PM $perl_header, <<'EOT';
52package B::Asmdata;
53use Exporter;
54@ISA = qw(Exporter);
55@EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
56use vars qw(%insn_data @insn_name @optype @specialsv_name);
57
58EOT
59print 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.
66EOT
67
68#
69# Boilerplate for byterun.c
70#
71open(BYTERUN_C, ">byterun.c") or die "byterun.c: $!";
72print 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
80void byterun(bs)
81struct bytestream bs;
82#else
83void byterun(fp)
84FILE *fp;
85#endif /* INDIRECT_BGET_MACROS */
86{
ed65db6c 87 dTHR;
79ee8297
MB
88 int insn;
89 while ((insn = FGETC()) != EOF) {
90 switch (insn) {
91EOT
92
93
94my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
95
96while (<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"];
136EOT
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#
145print BYTERUN_C <<'EOT';
146 default:
147 croak("Illegal bytecode instruction %d\n", insn);
148 /* NOTREACHED */
149 }
150 }
151}
152EOT
153
154#
155# Write the instruction and optype enum constants into byterun.h
156#
157open(BYTERUN_H, ">byterun.h") or die "byterun.h: $!";
158print BYTERUN_H $c_header, <<'EOT';
159#ifdef INDIRECT_BGET_MACROS
160struct bytestream {
161 void *data;
162 int (*fgetc)(void *);
163 int (*fread)(char *, size_t, size_t, void*);
164 void (*freadpv)(U32, void*);
165};
166void freadpv _((U32, void *));
167void byterun _((struct bytestream));
168#else
169void byterun _((FILE *));
170#endif /* INDIRECT_BGET_MACROS */
171
ed65db6c
MB
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
79ee8297
MB
179enum {
180EOT
181
182my $i = 0;
183my $add_enum_value = 0;
184my $max_insn;
185for ($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
200print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
201
202print BYTERUN_H "\nenum {\n";
203for ($i = 0; $i < @optype - 1; $i++) {
204 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
205}
206printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
207print BYTERUN_H <<'EOT';
208EXT int optype_size[]
209#ifdef DOINIT
210= {
211EOT
212for ($i = 0; $i < @optype - 1; $i++) {
213 printf BYTERUN_H " sizeof(%s),\n", $optype[$i], $i;
214}
215printf BYTERUN_H " sizeof(%s)\n}\n", $optype[$i], $i;
216print BYTERUN_H <<'EOT';
217#endif /* DOINIT */
218;
219
220EOT
221
222printf BYTERUN_H <<'EOT', scalar(@specialsv);
223EXT SV * specialsv_list[%d]
224#ifdef DOINIT
225EOT
226print BYTERUN_H "= { ", join(", ", @specialsv), " }\n";
227print BYTERUN_H <<'EOT';
228#endif /* DOINIT */
229;
230EOT
231
232#
233# Finish off insn_data and create array initialisers in Asmdata.pm
234#
235print ASMDATA_PM <<'EOT';
236
237my ($insn_name, $insn_data);
238while (($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
2441;
245EOT
246
247__END__
248# First set instruction ord("#") to read comment to end-of-line (sneaky)
249%number 35
250comment arg comment
251# Then make ord("\n") into a no-op
252%number 10
253nop 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#
260ret none none x
261ldsv sv svindex
262ldop op opindex
263stsv sv U32 s
264stop op U32 s
265ldspecsv sv U8 x
266newsv sv U8 x
267newop op U8 x
268newopn op U8 x
269newpv none PV
270pv_cur pv.xpv_cur STRLEN
271pv_free pv none x
272sv_upgrade sv char x
273sv_refcnt SvREFCNT(sv) U32
274sv_refcnt_add SvREFCNT(sv) I32 x
275sv_flags SvFLAGS(sv) U32
276xrv SvRV(sv) svindex
277xpv sv none x
278xiv32 SvIVX(sv) I32
279xiv64 SvIVX(sv) IV64
280xnv SvNVX(sv) double
281xlv_targoff LvTARGOFF(sv) STRLEN
282xlv_targlen LvTARGLEN(sv) STRLEN
283xlv_targ LvTARG(sv) svindex
284xlv_type LvTYPE(sv) char
285xbm_useful BmUSEFUL(sv) I32
286xbm_previous BmPREVIOUS(sv) U16
287xbm_rare BmRARE(sv) U8
288xfm_lines FmLINES(sv) I32
289xio_lines IoLINES(sv) long
290xio_page IoPAGE(sv) long
291xio_page_len IoPAGE_LEN(sv) long
292xio_lines_left IoLINES_LEFT(sv) long
293xio_top_name IoTOP_NAME(sv) pvcontents
f64a6365 294xio_top_gv *(SV**)&IoTOP_GV(sv) svindex
79ee8297 295xio_fmt_name IoFMT_NAME(sv) pvcontents
f64a6365 296xio_fmt_gv *(SV**)&IoFMT_GV(sv) svindex
79ee8297 297xio_bottom_name IoBOTTOM_NAME(sv) pvcontents
f64a6365 298xio_bottom_gv *(SV**)&IoBOTTOM_GV(sv) svindex
79ee8297
MB
299xio_subprocess IoSUBPROCESS(sv) short
300xio_type IoTYPE(sv) char
301xio_flags IoFLAGS(sv) char
302xcv_stash *(SV**)&CvSTASH(sv) svindex
303xcv_start CvSTART(sv) opindex
304xcv_root CvROOT(sv) opindex
f64a6365
MB
305xcv_gv *(SV**)&CvGV(sv) svindex
306xcv_filegv *(SV**)&CvFILEGV(sv) svindex
79ee8297
MB
307xcv_depth CvDEPTH(sv) long
308xcv_padlist *(SV**)&CvPADLIST(sv) svindex
309xcv_outside *(SV**)&CvOUTSIDE(sv) svindex
310xcv_flags CvFLAGS(sv) U8
311av_extend sv SSize_t x
312av_push sv svindex x
313xav_fill AvFILL(sv) SSize_t
314xav_max AvMAX(sv) SSize_t
315xav_flags AvFLAGS(sv) U8
316xhv_riter HvRITER(sv) I32
317xhv_name HvNAME(sv) pvcontents
318hv_store sv svindex x
319sv_magic sv char x
320mg_obj SvMAGIC(sv)->mg_obj svindex
321mg_private SvMAGIC(sv)->mg_private U16
322mg_flags SvMAGIC(sv)->mg_flags U8
323mg_pv SvMAGIC(sv) pvcontents x
324xmg_stash *(SV**)&SvSTASH(sv) svindex
325gv_fetchpv sv strconst x
326gv_stashpv sv strconst x
327gp_sv GvSV(sv) svindex
328gp_refcnt GvREFCNT(sv) U32
329gp_refcnt_add GvREFCNT(sv) I32 x
330gp_av *(SV**)&GvAV(sv) svindex
331gp_hv *(SV**)&GvHV(sv) svindex
332gp_cv *(SV**)&GvCV(sv) svindex
333gp_filegv *(SV**)&GvFILEGV(sv) svindex
334gp_io *(SV**)&GvIOp(sv) svindex
335gp_form *(SV**)&GvFORM(sv) svindex
336gp_cvgen GvCVGEN(sv) U32
337gp_line GvLINE(sv) line_t
338gp_share sv svindex x
339xgv_flags GvFLAGS(sv) U8
340op_next op->op_next opindex
341op_sibling op->op_sibling opindex
342op_ppaddr op->op_ppaddr strconst x
343op_targ op->op_targ PADOFFSET
344op_type op OPCODE x
345op_seq op->op_seq U16
346op_flags op->op_flags U8
347op_private op->op_private U8
348op_first cUNOP->op_first opindex
349op_last cBINOP->op_last opindex
350op_other cLOGOP->op_other opindex
351op_true cCONDOP->op_true opindex
352op_false cCONDOP->op_false opindex
353op_children cLISTOP->op_children U32
354op_pmreplroot cPMOP->op_pmreplroot opindex
355op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
356op_pmreplstart cPMOP->op_pmreplstart opindex
357op_pmnext *(OP**)&cPMOP->op_pmnext opindex
358pregcomp op pvcontents x
359op_pmshort cPMOP->op_pmshort svindex
360op_pmflags cPMOP->op_pmflags U16
361op_pmpermflags cPMOP->op_pmpermflags U16
362op_pmslen cPMOP->op_pmslen char
363op_sv cSVOP->op_sv svindex
f64a6365 364op_gv *(SV**)&cGVOP->op_gv svindex
79ee8297
MB
365op_pv cPVOP->op_pv pvcontents
366op_pv_tr cPVOP->op_pv op_tr_array
367op_redoop cLOOP->op_redoop opindex
368op_nextop cLOOP->op_nextop opindex
369op_lastop cLOOP->op_lastop opindex
370cop_label cCOP->cop_label pvcontents
371cop_stash *(SV**)&cCOP->cop_stash svindex
f64a6365 372cop_filegv *(SV**)&cCOP->cop_filegv svindex
79ee8297
MB
373cop_seq cCOP->cop_seq U32
374cop_arybase cCOP->cop_arybase I32
375cop_line cCOP->cop_line line_t
376main_start main_start opindex
377main_root main_root opindex
378curpad curpad svindex x