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