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