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