This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate from mainperl.
[perl5.git] / bytecode.pl
CommitLineData
a8a597b2
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)],
a8a597b2
MB
7);
8
9my @optype= qw(OP UNOP BINOP LOGOP CONDOP LISTOP PMOP SVOP GVOP PVOP LOOP COP);
10
11# Nullsv *must* come first in the following so that the condition
12# ($$sv == 0) can continue to be used to test (sv == Nullsv).
6b88bc9c 13my @specialsv = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no);
a8a597b2
MB
14
15my (%alias_from, $from, $tos);
16while (($from, $tos) = each %alias_to) {
17 map { $alias_from{$_} = $from } @$tos;
18}
19
20my $c_header = <<'EOT';
21/*
a8581515 22 * Copyright (c) 1996-1998 Malcolm Beattie
a8a597b2
MB
23 *
24 * You may distribute under the terms of either the GNU General Public
25 * License or the Artistic License, as specified in the README file.
26 *
27 */
28/*
29 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
30 */
31EOT
32
33my $perl_header;
34($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
35
33b839e2 36unlink "byterun.c", "byterun.h", "ext/B/B/Asmdata.pm";
a8a597b2
MB
37
38#
39# Start with boilerplate for Asmdata.pm
40#
33b839e2 41open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
a8a597b2
MB
42print ASMDATA_PM $perl_header, <<'EOT';
43package B::Asmdata;
44use Exporter;
45@ISA = qw(Exporter);
46@EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
47use vars qw(%insn_data @insn_name @optype @specialsv_name);
48
49EOT
50print ASMDATA_PM <<"EOT";
51\@optype = qw(@optype);
52\@specialsv_name = qw(@specialsv);
53
54# XXX insn_data is initialised this way because with a large
55# %insn_data = (foo => [...], bar => [...], ...) initialiser
56# I get a hard-to-track-down stack underflow and segfault.
57EOT
58
59#
60# Boilerplate for byterun.c
61#
62open(BYTERUN_C, ">byterun.c") or die "byterun.c: $!";
63print BYTERUN_C $c_header, <<'EOT';
64
65#include "EXTERN.h"
66#include "perl.h"
d613ef02
GS
67
68void *
69bset_obj_store(void *obj, I32 ix)
70{
92742e37
GS
71 if (ix > PL_bytecode_obj_list_fill) {
72 if (PL_bytecode_obj_list_fill == -1)
73 New(666, PL_bytecode_obj_list, ix + 1, void*);
d613ef02 74 else
92742e37
GS
75 Renew(PL_bytecode_obj_list, ix + 1, void*);
76 PL_bytecode_obj_list_fill = ix;
d613ef02 77 }
92742e37 78 PL_bytecode_obj_list[ix] = obj;
d613ef02
GS
79 return obj;
80}
a8a597b2
MB
81
82#ifdef INDIRECT_BGET_MACROS
33b839e2 83void byterun(struct bytestream bs)
a8a597b2 84#else
4b534093 85void byterun(PerlIO *fp)
a8a597b2
MB
86#endif /* INDIRECT_BGET_MACROS */
87{
88 dTHR;
89 int insn;
47358472 90 while ((insn = BGET_FGETC()) != EOF) {
a8a597b2
MB
91 switch (insn) {
92EOT
93
94
95my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
96
97while (<DATA>) {
98 chop;
99 s/#.*//; # remove comments
100 next unless length;
101 if (/^%number\s+(.*)/) {
102 $insn_num = $1;
103 next;
104 } elsif (/%enum\s+(.*?)\s+(.*)/) {
105 create_enum($1, $2); # must come before instructions
106 next;
107 }
108 ($insn, $lvalue, $argtype, $flags) = split;
109 $insn_name[$insn_num] = $insn;
110 $fundtype = $alias_from{$argtype} || $argtype;
111
112 #
113 # Add the case statement and code for the bytecode interpreter in byterun.c
114 #
115 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
116 uc($insn), $insn_num;
117 my $optarg = $argtype eq "none" ? "" : ", arg";
118 if ($optarg) {
119 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
120 }
121 if ($flags =~ /x/) {
122 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
123 } elsif ($flags =~ /s/) {
92742e37 124 # Store instructions store to PL_bytecode_obj_list[arg]. "lvalue" field is rvalue.
a8a597b2
MB
125 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
126 }
127 elsif ($optarg && $lvalue ne "none") {
128 print BYTERUN_C "\t\t$lvalue = arg;\n";
129 }
130 print BYTERUN_C "\t\tbreak;\n\t }\n";
131
132 #
133 # Add the initialiser line for %insn_data in Asmdata.pm
134 #
135 print ASMDATA_PM <<"EOT";
136\$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
137EOT
138
139 # Find the next unused instruction number
140 do { $insn_num++ } while $insn_name[$insn_num];
141}
142
143#
144# Finish off byterun.c
145#
146print BYTERUN_C <<'EOT';
147 default:
148 croak("Illegal bytecode instruction %d\n", insn);
149 /* NOTREACHED */
150 }
151 }
152}
153EOT
154
155#
156# Write the instruction and optype enum constants into byterun.h
157#
158open(BYTERUN_H, ">byterun.h") or die "byterun.h: $!";
159print BYTERUN_H $c_header, <<'EOT';
160#ifdef INDIRECT_BGET_MACROS
161struct bytestream {
162 void *data;
163 int (*fgetc)(void *);
164 int (*fread)(char *, size_t, size_t, void*);
165 void (*freadpv)(U32, void*);
166};
a8a597b2
MB
167#endif /* INDIRECT_BGET_MACROS */
168
d613ef02 169void *bset_obj_store _((void *, I32));
a8a597b2
MB
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
fe3a57c4 214print BYTERUN_H <<'EOT';
a8a597b2
MB
215#define INIT_SPECIALSV_LIST STMT_START { \
216EOT
217for ($i = 0; $i < @specialsv; $i++) {
6b88bc9c 218 print BYTERUN_H "\tPL_specialsv_list[$i] = $specialsv[$i]; \\\n";
a8a597b2
MB
219}
220print BYTERUN_H <<'EOT';
33b839e2 221 } STMT_END
a8a597b2
MB
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;
42d3a99d
GS
237
238__END__
239
240=head1 NAME
241
242B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
243
244=head1 SYNOPSIS
245
246 use Asmdata;
247
248=head1 DESCRIPTION
249
250See F<ext/B/B/Asmdata.pm>.
251
252=head1 AUTHOR
253
254Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
255
256=cut
a8a597b2
MB
257EOT
258
259__END__
260# First set instruction ord("#") to read comment to end-of-line (sneaky)
261%number 35
fe3a57c4 262comment arg comment_t
a8a597b2
MB
263# Then make ord("\n") into a no-op
264%number 10
265nop none none
266# Now for the rest of the ordinary ones, beginning with \0 which is
267# ret so that \0-terminated strings can be read properly as bytecode.
268%number 0
269#
92742e37 270#opcode lvalue argtype flags
a8a597b2 271#
92742e37
GS
272ret none none x
273ldsv PL_bytecode_sv svindex
274ldop PL_op opindex
275stsv PL_bytecode_sv U32 s
276stop PL_op U32 s
277ldspecsv PL_bytecode_sv U8 x
278newsv PL_bytecode_sv U8 x
279newop PL_op U8 x
280newopn PL_op U8 x
281newpv none PV
282pv_cur PL_bytecode_pv.xpv_cur STRLEN
283pv_free PL_bytecode_pv none x
284sv_upgrade PL_bytecode_sv char x
285sv_refcnt SvREFCNT(PL_bytecode_sv) U32
286sv_refcnt_add SvREFCNT(PL_bytecode_sv) I32 x
287sv_flags SvFLAGS(PL_bytecode_sv) U32
288xrv SvRV(PL_bytecode_sv) svindex
289xpv PL_bytecode_sv none x
290xiv32 SvIVX(PL_bytecode_sv) I32
291xiv64 SvIVX(PL_bytecode_sv) IV64
292xnv SvNVX(PL_bytecode_sv) double
293xlv_targoff LvTARGOFF(PL_bytecode_sv) STRLEN
294xlv_targlen LvTARGLEN(PL_bytecode_sv) STRLEN
295xlv_targ LvTARG(PL_bytecode_sv) svindex
296xlv_type LvTYPE(PL_bytecode_sv) char
297xbm_useful BmUSEFUL(PL_bytecode_sv) I32
298xbm_previous BmPREVIOUS(PL_bytecode_sv) U16
299xbm_rare BmRARE(PL_bytecode_sv) U8
300xfm_lines FmLINES(PL_bytecode_sv) I32
301xio_lines IoLINES(PL_bytecode_sv) long
302xio_page IoPAGE(PL_bytecode_sv) long
303xio_page_len IoPAGE_LEN(PL_bytecode_sv) long
304xio_lines_left IoLINES_LEFT(PL_bytecode_sv) long
305xio_top_name IoTOP_NAME(PL_bytecode_sv) pvcontents
306xio_top_gv *(SV**)&IoTOP_GV(PL_bytecode_sv) svindex
307xio_fmt_name IoFMT_NAME(PL_bytecode_sv) pvcontents
308xio_fmt_gv *(SV**)&IoFMT_GV(PL_bytecode_sv) svindex
309xio_bottom_name IoBOTTOM_NAME(PL_bytecode_sv) pvcontents
310xio_bottom_gv *(SV**)&IoBOTTOM_GV(PL_bytecode_sv) svindex
311xio_subprocess IoSUBPROCESS(PL_bytecode_sv) short
312xio_type IoTYPE(PL_bytecode_sv) char
313xio_flags IoFLAGS(PL_bytecode_sv) char
314xcv_stash *(SV**)&CvSTASH(PL_bytecode_sv) svindex
315xcv_start CvSTART(PL_bytecode_sv) opindex
316xcv_root CvROOT(PL_bytecode_sv) opindex
317xcv_gv *(SV**)&CvGV(PL_bytecode_sv) svindex
318xcv_filegv *(SV**)&CvFILEGV(PL_bytecode_sv) svindex
319xcv_depth CvDEPTH(PL_bytecode_sv) long
320xcv_padlist *(SV**)&CvPADLIST(PL_bytecode_sv) svindex
321xcv_outside *(SV**)&CvOUTSIDE(PL_bytecode_sv) svindex
322xcv_flags CvFLAGS(PL_bytecode_sv) U8
323av_extend PL_bytecode_sv SSize_t x
324av_push PL_bytecode_sv svindex x
325xav_fill AvFILLp(PL_bytecode_sv) SSize_t
326xav_max AvMAX(PL_bytecode_sv) SSize_t
327xav_flags AvFLAGS(PL_bytecode_sv) U8
328xhv_riter HvRITER(PL_bytecode_sv) I32
329xhv_name HvNAME(PL_bytecode_sv) pvcontents
330hv_store PL_bytecode_sv svindex x
331sv_magic PL_bytecode_sv char x
332mg_obj SvMAGIC(PL_bytecode_sv)->mg_obj svindex
333mg_private SvMAGIC(PL_bytecode_sv)->mg_private U16
334mg_flags SvMAGIC(PL_bytecode_sv)->mg_flags U8
335mg_pv SvMAGIC(PL_bytecode_sv) pvcontents x
336xmg_stash *(SV**)&SvSTASH(PL_bytecode_sv) svindex
337gv_fetchpv PL_bytecode_sv strconst x
338gv_stashpv PL_bytecode_sv strconst x
339gp_sv GvSV(PL_bytecode_sv) svindex
340gp_refcnt GvREFCNT(PL_bytecode_sv) U32
341gp_refcnt_add GvREFCNT(PL_bytecode_sv) I32 x
342gp_av *(SV**)&GvAV(PL_bytecode_sv) svindex
343gp_hv *(SV**)&GvHV(PL_bytecode_sv) svindex
344gp_cv *(SV**)&GvCV(PL_bytecode_sv) svindex
345gp_filegv *(SV**)&GvFILEGV(PL_bytecode_sv) svindex
346gp_io *(SV**)&GvIOp(PL_bytecode_sv) svindex
347gp_form *(SV**)&GvFORM(PL_bytecode_sv) svindex
348gp_cvgen GvCVGEN(PL_bytecode_sv) U32
349gp_line GvLINE(PL_bytecode_sv) line_t
350gp_share PL_bytecode_sv svindex x
351xgv_flags GvFLAGS(PL_bytecode_sv) U8
352op_next PL_op->op_next opindex
353op_sibling PL_op->op_sibling opindex
354op_ppaddr PL_op->op_ppaddr strconst x
355op_targ PL_op->op_targ PADOFFSET
356op_type PL_op OPCODE x
357op_seq PL_op->op_seq U16
358op_flags PL_op->op_flags U8
359op_private PL_op->op_private U8
360op_first cUNOP->op_first opindex
361op_last cBINOP->op_last opindex
362op_other cLOGOP->op_other opindex
363op_true cCONDOP->op_true opindex
364op_false cCONDOP->op_false opindex
365op_children cLISTOP->op_children U32
366op_pmreplroot cPMOP->op_pmreplroot opindex
367op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
368op_pmreplstart cPMOP->op_pmreplstart opindex
369op_pmnext *(OP**)&cPMOP->op_pmnext opindex
370pregcomp PL_op pvcontents x
371op_pmflags cPMOP->op_pmflags U16
372op_pmpermflags cPMOP->op_pmpermflags U16
373op_sv cSVOP->op_sv svindex
374op_gv *(SV**)&cGVOP->op_gv svindex
375op_pv cPVOP->op_pv pvcontents
376op_pv_tr cPVOP->op_pv op_tr_array
377op_redoop cLOOP->op_redoop opindex
378op_nextop cLOOP->op_nextop opindex
379op_lastop cLOOP->op_lastop opindex
380cop_label cCOP->cop_label pvcontents
381cop_stash *(SV**)&cCOP->cop_stash svindex
382cop_filegv *(SV**)&cCOP->cop_filegv svindex
383cop_seq cCOP->cop_seq U32
384cop_arybase cCOP->cop_arybase I32
385cop_line cCOP->cop_line line_t
386main_start PL_main_start opindex
387main_root PL_main_root opindex
388curpad PL_curpad svindex x