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