This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix my_chsize() for systems having chsize(), broken by change #24271
[perl5.git] / bytecode.pl
CommitLineData
73f0cc2d
GS
1BEGIN {
2 push @INC, './lib';
9ad884cb 3 require 'regen_lib.pl';
73f0cc2d 4}
a8a597b2
MB
5use strict;
6my %alias_to = (
113d5bd9
JH
7 U32 => [qw(line_t)],
8 PADOFFSET => [qw(STRLEN SSize_t)],
dea28490 9 U16 => [qw(OPCODE short)],
d5e9ef99 10 U8 => [qw(char)],
a8a597b2
MB
11);
12
7934575e 13my @optype= qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP);
a8a597b2
MB
14
15# Nullsv *must* come first in the following so that the condition
16# ($$sv == 0) can continue to be used to test (sv == Nullsv).
059a8bb7 17my @specialsv = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no pWARN_ALL pWARN_NONE);
a8a597b2
MB
18
19my (%alias_from, $from, $tos);
20while (($from, $tos) = each %alias_to) {
21 map { $alias_from{$_} = $from } @$tos;
22}
23
24my $c_header = <<'EOT';
25/*
4eb8286e 26 * Copyright (c) 1996-1999 Malcolm Beattie
a8a597b2
MB
27 *
28 * You may distribute under the terms of either the GNU General Public
29 * License or the Artistic License, as specified in the README file.
30 *
31 */
32/*
33 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
34 */
35EOT
36
37my $perl_header;
38($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
39
36bb303b 40safer_unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
a8a597b2
MB
41
42#
43# Start with boilerplate for Asmdata.pm
44#
33b839e2 45open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
dfb1454f 46binmode ASMDATA_PM;
a8a597b2
MB
47print ASMDATA_PM $perl_header, <<'EOT';
48package B::Asmdata;
28b605d8 49
a0edd7f8 50our $VERSION = '1.01';
28b605d8 51
a8a597b2
MB
52use Exporter;
53@ISA = qw(Exporter);
54@EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
1b11e67e 55our(%insn_data, @insn_name, @optype, @specialsv_name);
a8a597b2
MB
56
57EOT
58print ASMDATA_PM <<"EOT";
59\@optype = qw(@optype);
60\@specialsv_name = qw(@specialsv);
61
62# XXX insn_data is initialised this way because with a large
63# %insn_data = (foo => [...], bar => [...], ...) initialiser
64# I get a hard-to-track-down stack underflow and segfault.
65EOT
66
67#
68# Boilerplate for byterun.c
69#
e8edd1e6 70open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
dfb1454f 71binmode BYTERUN_C;
a8a597b2
MB
72print BYTERUN_C $c_header, <<'EOT';
73
c5be433b 74#define PERL_NO_GET_CONTEXT
a8a597b2
MB
75#include "EXTERN.h"
76#include "perl.h"
0cb96387
GS
77#define NO_XSLOCKS
78#include "XSUB.h"
79
e8edd1e6
TH
80#include "byterun.h"
81#include "bytecode.h"
82
0cb96387 83
059a8bb7 84static const int optype_size[] = {
e8edd1e6
TH
85EOT
86my $i = 0;
87for ($i = 0; $i < @optype - 1; $i++) {
88 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
89}
90printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
91print BYTERUN_C <<'EOT';
92};
93
d613ef02 94void *
acfe0abc 95bset_obj_store(pTHX_ struct byteloader_state *bstate, void *obj, I32 ix)
d613ef02 96{
059a8bb7
JH
97 if (ix > bstate->bs_obj_list_fill) {
98 Renew(bstate->bs_obj_list, ix + 32, void*);
99 bstate->bs_obj_list_fill = ix + 31;
d613ef02 100 }
059a8bb7 101 bstate->bs_obj_list[ix] = obj;
d613ef02
GS
102 return obj;
103}
a8a597b2 104
1df34986 105int
acfe0abc 106byterun(pTHX_ register struct byteloader_state *bstate)
a8a597b2 107{
27da23d5 108 dVAR;
059a8bb7
JH
109 register int insn;
110 U32 ix;
111 SV *specialsv_list[6];
112
113 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
114 New(666, bstate->bs_obj_list, 32, void*); /* set op objlist */
115 bstate->bs_obj_list_fill = 31;
1df34986 116 bstate->bs_obj_list[0] = NULL; /* first is always Null */
566ece03 117 bstate->bs_ix = 1;
e8edd1e6
TH
118
119EOT
120
e3751d82 121for my $i ( 0 .. $#specialsv ) {
e8edd1e6
TH
122 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
123}
124
125print BYTERUN_C <<'EOT';
126
47358472 127 while ((insn = BGET_FGETC()) != EOF) {
a8a597b2
MB
128 switch (insn) {
129EOT
130
131
132my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
133
134while (<DATA>) {
1df34986
AE
135 if (/^\s*#/) {
136 print BYTERUN_C if /^\s*#\s*(?:if|endif|el)/;
137 next;
138 }
a8a597b2 139 chop;
a8a597b2
MB
140 next unless length;
141 if (/^%number\s+(.*)/) {
142 $insn_num = $1;
143 next;
144 } elsif (/%enum\s+(.*?)\s+(.*)/) {
145 create_enum($1, $2); # must come before instructions
146 next;
147 }
148 ($insn, $lvalue, $argtype, $flags) = split;
b97332e7
JH
149 my $rvalcast = '';
150 if ($argtype =~ m:(.+)/(.+):) {
151 ($rvalcast, $argtype) = ("($1)", $2);
152 }
a8a597b2
MB
153 $insn_name[$insn_num] = $insn;
154 $fundtype = $alias_from{$argtype} || $argtype;
155
156 #
157 # Add the case statement and code for the bytecode interpreter in byterun.c
158 #
159 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
160 uc($insn), $insn_num;
161 my $optarg = $argtype eq "none" ? "" : ", arg";
162 if ($optarg) {
163 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
164 }
165 if ($flags =~ /x/) {
166 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
167 } elsif ($flags =~ /s/) {
e8edd1e6 168 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
a8a597b2
MB
169 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
170 }
171 elsif ($optarg && $lvalue ne "none") {
b97332e7 172 print BYTERUN_C "\t\t$lvalue = ${rvalcast}arg;\n";
a8a597b2
MB
173 }
174 print BYTERUN_C "\t\tbreak;\n\t }\n";
175
176 #
177 # Add the initialiser line for %insn_data in Asmdata.pm
178 #
179 print ASMDATA_PM <<"EOT";
180\$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
181EOT
182
183 # Find the next unused instruction number
184 do { $insn_num++ } while $insn_name[$insn_num];
185}
186
187#
188# Finish off byterun.c
189#
190print BYTERUN_C <<'EOT';
191 default:
cea2e8a9 192 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
a8a597b2
MB
193 /* NOTREACHED */
194 }
195 }
1df34986 196 return 0;
a8a597b2
MB
197}
198EOT
199
200#
201# Write the instruction and optype enum constants into byterun.h
202#
e8edd1e6 203open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
dfb1454f 204binmode BYTERUN_H;
a8a597b2 205print BYTERUN_H $c_header, <<'EOT';
059a8bb7
JH
206struct byteloader_fdata {
207 SV *datasv;
208 int next_out;
209 int idx;
a8a597b2 210};
a8a597b2 211
059a8bb7
JH
212struct byteloader_state {
213 struct byteloader_fdata *bs_fdata;
214 SV *bs_sv;
215 void **bs_obj_list;
216 int bs_obj_list_fill;
566ece03 217 int bs_ix;
059a8bb7
JH
218 XPV bs_pv;
219 int bs_iv_overflows;
220};
221
222int bl_getc(struct byteloader_fdata *);
223int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
1df34986 224extern int byterun(pTHX_ struct byteloader_state *);
059a8bb7 225
a8a597b2
MB
226enum {
227EOT
228
a8a597b2
MB
229my $add_enum_value = 0;
230my $max_insn;
e3751d82 231for $i ( 0 .. $#insn_name ) {
a8a597b2
MB
232 $insn = uc($insn_name[$i]);
233 if (defined($insn)) {
234 $max_insn = $i;
235 if ($add_enum_value) {
236 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
237 $add_enum_value = 0;
238 } else {
239 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
240 }
241 } else {
242 $add_enum_value = 1;
243 }
244}
245
246print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
247
248print BYTERUN_H "\nenum {\n";
249for ($i = 0; $i < @optype - 1; $i++) {
250 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
251}
252printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
a8a597b2 253
a8a597b2
MB
254#
255# Finish off insn_data and create array initialisers in Asmdata.pm
256#
257print ASMDATA_PM <<'EOT';
258
259my ($insn_name, $insn_data);
260while (($insn_name, $insn_data) = each %insn_data) {
261 $insn_name[$insn_data->[0]] = $insn_name;
262}
263# Fill in any gaps
264@insn_name = map($_ || "unused", @insn_name);
265
2661;
42d3a99d
GS
267
268__END__
269
270=head1 NAME
271
272B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
273
274=head1 SYNOPSIS
275
4162ffa6 276 use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
42d3a99d
GS
277
278=head1 DESCRIPTION
279
4162ffa6
MS
280Provides information about Perl ops in order to generate bytecode via
281a bunch of exported variables. Its mostly used by B::Assembler and
282B::Disassembler.
283
284=over 4
285
286=item %insn_data
287
288 my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
289
290For a given $op_name (for example, 'cop_label', 'sv_flags', etc...)
291you get an array ref containing the bytecode number of the op, a
292reference to the subroutine used to 'PUT', and the name of the method
293used to 'GET'.
294
295=for _private
296Add more detail about what $put_sub and $get_meth are and how to use them.
297
298=item @insn_name
299
300 my $op_name = $insn_name[$bytecode_num];
301
302A simple mapping of the bytecode number to the name of the op.
303Suitable for using with %insn_data like so:
304
305 my $op_info = $insn_data{$insn_name[$bytecode_num]};
306
307=item @optype
308
309 my $op_type = $optype[$op_type_num];
310
311A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
312
313=item @specialsv_name
314
315 my $sv_name = $specialsv_name[$sv_index];
316
317Certain SV types are considered 'special'. They're represented by
318B::SPECIAL and are refered to by a number from the specialsv_list.
319This array maps that number back to the name of the SV (like 'Nullsv'
320or '&PL_sv_undef').
321
322=back
42d3a99d
GS
323
324=head1 AUTHOR
325
326Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
327
328=cut
a8a597b2
MB
329EOT
330
36bb303b
NC
331
332close ASMDATA_PM or die "Error closing ASMDATA_PM: $!";
333close BYTERUN_H or die "Error closing BYTERUN_H: $!";
334close BYTERUN_C or die "Error closing BYTERUN_C: $!";
335
a8a597b2
MB
336__END__
337# First set instruction ord("#") to read comment to end-of-line (sneaky)
338%number 35
fe3a57c4 339comment arg comment_t
a8a597b2
MB
340# Then make ord("\n") into a no-op
341%number 10
342nop none none
1df34986 343
a8a597b2
MB
344# Now for the rest of the ordinary ones, beginning with \0 which is
345# ret so that \0-terminated strings can be read properly as bytecode.
346%number 0
347#
b97332e7
JH
348# The argtype is either a single type or "rightvaluecast/argtype".
349#
92742e37 350#opcode lvalue argtype flags
a8a597b2 351#
92742e37 352ret none none x
059a8bb7 353ldsv bstate->bs_sv svindex
92742e37 354ldop PL_op opindex
059a8bb7 355stsv bstate->bs_sv U32 s
92742e37 356stop PL_op U32 s
059a8bb7
JH
357stpv bstate->bs_pv.xpv_pv U32 x
358ldspecsv bstate->bs_sv U8 x
566ece03 359ldspecsvx bstate->bs_sv U8 x
059a8bb7 360newsv bstate->bs_sv U8 x
566ece03 361newsvx bstate->bs_sv U32 x
92742e37 362newop PL_op U8 x
566ece03 363newopx PL_op U16 x
92742e37
GS
364newopn PL_op U8 x
365newpv none PV
059a8bb7
JH
366pv_cur bstate->bs_pv.xpv_cur STRLEN
367pv_free bstate->bs_pv none x
6e21dc91 368sv_upgrade bstate->bs_sv U8 x
059a8bb7
JH
369sv_refcnt SvREFCNT(bstate->bs_sv) U32
370sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
371sv_flags SvFLAGS(bstate->bs_sv) U32
87a1ef3d 372xrv bstate->bs_sv svindex x
059a8bb7 373xpv bstate->bs_sv none x
87a1ef3d
SP
374xpv_cur bstate->bs_sv STRLEN x
375xpv_len bstate->bs_sv STRLEN x
376xiv bstate->bs_sv IV x
377xnv bstate->bs_sv NV x
059a8bb7
JH
378xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
379xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
380xlv_targ LvTARG(bstate->bs_sv) svindex
381xlv_type LvTYPE(bstate->bs_sv) char
382xbm_useful BmUSEFUL(bstate->bs_sv) I32
383xbm_previous BmPREVIOUS(bstate->bs_sv) U16
384xbm_rare BmRARE(bstate->bs_sv) U8
11a7ac70
JH
385xfm_lines FmLINES(bstate->bs_sv) IV
386xio_lines IoLINES(bstate->bs_sv) IV
387xio_page IoPAGE(bstate->bs_sv) IV
388xio_page_len IoPAGE_LEN(bstate->bs_sv) IV
389xio_lines_left IoLINES_LEFT(bstate->bs_sv) IV
1df34986 390xio_top_name IoTOP_NAME(bstate->bs_sv) pvindex
059a8bb7 391xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
1df34986 392xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvindex
059a8bb7 393xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
1df34986 394xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvindex
059a8bb7
JH
395xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
396xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
397xio_type IoTYPE(bstate->bs_sv) char
398xio_flags IoFLAGS(bstate->bs_sv) char
1df34986 399xcv_xsubany *(SV**)&CvXSUBANY(bstate->bs_sv).any_ptr svindex
059a8bb7
JH
400xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
401xcv_start CvSTART(bstate->bs_sv) opindex
402xcv_root CvROOT(bstate->bs_sv) opindex
403xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
404xcv_file CvFILE(bstate->bs_sv) pvindex
405xcv_depth CvDEPTH(bstate->bs_sv) long
406xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
407xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
f52873be 408xcv_outside_seq CvOUTSIDE_SEQ(bstate->bs_sv) U32
059a8bb7
JH
409xcv_flags CvFLAGS(bstate->bs_sv) U16
410av_extend bstate->bs_sv SSize_t x
1df34986 411av_pushx bstate->bs_sv svindex x
059a8bb7
JH
412av_push bstate->bs_sv svindex x
413xav_fill AvFILLp(bstate->bs_sv) SSize_t
414xav_max AvMAX(bstate->bs_sv) SSize_t
415xav_flags AvFLAGS(bstate->bs_sv) U8
416xhv_riter HvRITER(bstate->bs_sv) I32
1df34986
AE
417xhv_name HvNAME(bstate->bs_sv) pvindex
418xhv_pmroot *(OP**)&HvPMROOT(bstate->bs_sv) opindex
059a8bb7
JH
419hv_store bstate->bs_sv svindex x
420sv_magic bstate->bs_sv char x
421mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
422mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
423mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
1df34986
AE
424mg_name SvMAGIC(bstate->bs_sv) pvcontents x
425mg_namex SvMAGIC(bstate->bs_sv) svindex x
87a1ef3d 426xmg_stash bstate->bs_sv svindex X
059a8bb7 427gv_fetchpv bstate->bs_sv strconst x
566ece03 428gv_fetchpvx bstate->bs_sv strconst x
059a8bb7 429gv_stashpv bstate->bs_sv strconst x
566ece03 430gv_stashpvx bstate->bs_sv strconst x
059a8bb7
JH
431gp_sv GvSV(bstate->bs_sv) svindex
432gp_refcnt GvREFCNT(bstate->bs_sv) U32
433gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
434gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
435gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
436gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
437gp_file GvFILE(bstate->bs_sv) pvindex
438gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
439gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
440gp_cvgen GvCVGEN(bstate->bs_sv) U32
441gp_line GvLINE(bstate->bs_sv) line_t
442gp_share bstate->bs_sv svindex x
443xgv_flags GvFLAGS(bstate->bs_sv) U8
92742e37
GS
444op_next PL_op->op_next opindex
445op_sibling PL_op->op_sibling opindex
446op_ppaddr PL_op->op_ppaddr strconst x
447op_targ PL_op->op_targ PADOFFSET
448op_type PL_op OPCODE x
2814eb74
PJ
449op_opt PL_op->op_opt U8
450op_static PL_op->op_static U8
92742e37
GS
451op_flags PL_op->op_flags U8
452op_private PL_op->op_private U8
453op_first cUNOP->op_first opindex
454op_last cBINOP->op_last opindex
455op_other cLOGOP->op_other opindex
92742e37 456op_pmreplroot cPMOP->op_pmreplroot opindex
92742e37
GS
457op_pmreplstart cPMOP->op_pmreplstart opindex
458op_pmnext *(OP**)&cPMOP->op_pmnext opindex
1df34986 459#ifdef USE_ITHREADS
47682f07 460op_pmstashpv cPMOP pvindex x
b97332e7 461op_pmreplrootpo cPMOP->op_pmreplroot OP*/PADOFFSET
1df34986
AE
462#else
463op_pmstash *(SV**)&cPMOP->op_pmstash svindex
464op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
465#endif
92742e37
GS
466pregcomp PL_op pvcontents x
467op_pmflags cPMOP->op_pmflags U16
468op_pmpermflags cPMOP->op_pmpermflags U16
1df34986 469op_pmdynflags cPMOP->op_pmdynflags U8
92742e37 470op_sv cSVOP->op_sv svindex
7934575e 471op_padix cPADOP->op_padix PADOFFSET
92742e37
GS
472op_pv cPVOP->op_pv pvcontents
473op_pv_tr cPVOP->op_pv op_tr_array
474op_redoop cLOOP->op_redoop opindex
475op_nextop cLOOP->op_nextop opindex
476op_lastop cLOOP->op_lastop opindex
059a8bb7 477cop_label cCOP->cop_label pvindex
1df34986 478#ifdef USE_ITHREADS
059a8bb7
JH
479cop_stashpv cCOP pvindex x
480cop_file cCOP pvindex x
1df34986
AE
481#else
482cop_stash cCOP svindex x
483cop_filegv cCOP svindex x
484#endif
92742e37
GS
485cop_seq cCOP->cop_seq U32
486cop_arybase cCOP->cop_arybase I32
1df34986
AE
487cop_line cCOP->cop_line line_t
488cop_io cCOP->cop_io svindex
b295d113 489cop_warnings cCOP->cop_warnings svindex
92742e37
GS
490main_start PL_main_start opindex
491main_root PL_main_root opindex
1df34986 492main_cv *(SV**)&PL_main_cv svindex
92742e37 493curpad PL_curpad svindex x
059a8bb7
JH
494push_begin PL_beginav svindex x
495push_init PL_initav svindex x
496push_end PL_endav svindex x
1df34986
AE
497curstash *(SV**)&PL_curstash svindex
498defstash *(SV**)&PL_defstash svindex
499data none U8 x
0ac16f7c 500incav *(SV**)&GvAV(PL_incgv) svindex
1df34986
AE
501load_glob none svindex x
502#ifdef USE_ITHREADS
503regex_padav *(SV**)&PL_regex_padav svindex
504#endif
505dowarn PL_dowarn U8
506comppad_name *(SV**)&PL_comppad_name svindex
507xgv_stash *(SV**)&GvSTASH(bstate->bs_sv) svindex
508signal bstate->bs_sv strconst x
509# to be removed
510formfeed PL_formfeed svindex