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