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