Duplicate definition bad.
[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 = (
7 U32 => [qw(PADOFFSET STRLEN)],
8 I32 => [qw(SSize_t long)],
9 U16 => [qw(OPCODE line_t short)],
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
cea2e8a9 103void
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;
e8edd1e6 113
114EOT
115
e3751d82 116for my $i ( 0 .. $#specialsv ) {
e8edd1e6 117 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
118}
119
120print BYTERUN_C <<'EOT';
121
47358472 122 while ((insn = BGET_FGETC()) != EOF) {
a8a597b2 123 switch (insn) {
124EOT
125
126
127my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
128
129while (<DATA>) {
130 chop;
131 s/#.*//; # remove comments
132 next unless length;
133 if (/^%number\s+(.*)/) {
134 $insn_num = $1;
135 next;
136 } elsif (/%enum\s+(.*?)\s+(.*)/) {
137 create_enum($1, $2); # must come before instructions
138 next;
139 }
140 ($insn, $lvalue, $argtype, $flags) = split;
141 $insn_name[$insn_num] = $insn;
142 $fundtype = $alias_from{$argtype} || $argtype;
143
144 #
145 # Add the case statement and code for the bytecode interpreter in byterun.c
146 #
147 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
148 uc($insn), $insn_num;
149 my $optarg = $argtype eq "none" ? "" : ", arg";
150 if ($optarg) {
151 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
152 }
153 if ($flags =~ /x/) {
154 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
155 } elsif ($flags =~ /s/) {
e8edd1e6 156 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
a8a597b2 157 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
158 }
159 elsif ($optarg && $lvalue ne "none") {
160 print BYTERUN_C "\t\t$lvalue = arg;\n";
161 }
162 print BYTERUN_C "\t\tbreak;\n\t }\n";
163
164 #
165 # Add the initialiser line for %insn_data in Asmdata.pm
166 #
167 print ASMDATA_PM <<"EOT";
168\$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
169EOT
170
171 # Find the next unused instruction number
172 do { $insn_num++ } while $insn_name[$insn_num];
173}
174
175#
176# Finish off byterun.c
177#
178print BYTERUN_C <<'EOT';
179 default:
cea2e8a9 180 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
a8a597b2 181 /* NOTREACHED */
182 }
183 }
184}
185EOT
186
187#
188# Write the instruction and optype enum constants into byterun.h
189#
e8edd1e6 190open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
a8a597b2 191print BYTERUN_H $c_header, <<'EOT';
059a8bb7 192struct byteloader_fdata {
193 SV *datasv;
194 int next_out;
195 int idx;
a8a597b2 196};
a8a597b2 197
059a8bb7 198struct byteloader_state {
199 struct byteloader_fdata *bs_fdata;
200 SV *bs_sv;
201 void **bs_obj_list;
202 int bs_obj_list_fill;
203 XPV bs_pv;
204 int bs_iv_overflows;
205};
206
207int bl_getc(struct byteloader_fdata *);
208int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
acfe0abc 209extern void byterun(pTHX_ struct byteloader_state *);
059a8bb7 210
a8a597b2 211enum {
212EOT
213
a8a597b2 214my $add_enum_value = 0;
215my $max_insn;
e3751d82 216for $i ( 0 .. $#insn_name ) {
a8a597b2 217 $insn = uc($insn_name[$i]);
218 if (defined($insn)) {
219 $max_insn = $i;
220 if ($add_enum_value) {
221 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
222 $add_enum_value = 0;
223 } else {
224 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
225 }
226 } else {
227 $add_enum_value = 1;
228 }
229}
230
231print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
232
233print BYTERUN_H "\nenum {\n";
234for ($i = 0; $i < @optype - 1; $i++) {
235 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
236}
237printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
a8a597b2 238
a8a597b2 239#
240# Finish off insn_data and create array initialisers in Asmdata.pm
241#
242print ASMDATA_PM <<'EOT';
243
244my ($insn_name, $insn_data);
245while (($insn_name, $insn_data) = each %insn_data) {
246 $insn_name[$insn_data->[0]] = $insn_name;
247}
248# Fill in any gaps
249@insn_name = map($_ || "unused", @insn_name);
250
2511;
42d3a99d 252
253__END__
254
255=head1 NAME
256
257B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
258
259=head1 SYNOPSIS
260
4162ffa6 261 use B::Asmdata qw(%insn_data @insn_name @optype @specialsv_name);
42d3a99d 262
263=head1 DESCRIPTION
264
4162ffa6 265Provides information about Perl ops in order to generate bytecode via
266a bunch of exported variables. Its mostly used by B::Assembler and
267B::Disassembler.
268
269=over 4
270
271=item %insn_data
272
273 my($bytecode_num, $put_sub, $get_meth) = @$insn_data{$op_name};
274
275For a given $op_name (for example, 'cop_label', 'sv_flags', etc...)
276you get an array ref containing the bytecode number of the op, a
277reference to the subroutine used to 'PUT', and the name of the method
278used to 'GET'.
279
280=for _private
281Add more detail about what $put_sub and $get_meth are and how to use them.
282
283=item @insn_name
284
285 my $op_name = $insn_name[$bytecode_num];
286
287A simple mapping of the bytecode number to the name of the op.
288Suitable for using with %insn_data like so:
289
290 my $op_info = $insn_data{$insn_name[$bytecode_num]};
291
292=item @optype
293
294 my $op_type = $optype[$op_type_num];
295
296A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
297
298=item @specialsv_name
299
300 my $sv_name = $specialsv_name[$sv_index];
301
302Certain SV types are considered 'special'. They're represented by
303B::SPECIAL and are refered to by a number from the specialsv_list.
304This array maps that number back to the name of the SV (like 'Nullsv'
305or '&PL_sv_undef').
306
307=back
42d3a99d 308
309=head1 AUTHOR
310
311Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
312
313=cut
a8a597b2 314EOT
315
36bb303b 316
317close ASMDATA_PM or die "Error closing ASMDATA_PM: $!";
318close BYTERUN_H or die "Error closing BYTERUN_H: $!";
319close BYTERUN_C or die "Error closing BYTERUN_C: $!";
320
a8a597b2 321__END__
322# First set instruction ord("#") to read comment to end-of-line (sneaky)
323%number 35
fe3a57c4 324comment arg comment_t
a8a597b2 325# Then make ord("\n") into a no-op
326%number 10
327nop none none
328# Now for the rest of the ordinary ones, beginning with \0 which is
329# ret so that \0-terminated strings can be read properly as bytecode.
330%number 0
331#
92742e37 332#opcode lvalue argtype flags
a8a597b2 333#
92742e37 334ret none none x
059a8bb7 335ldsv bstate->bs_sv svindex
92742e37 336ldop PL_op opindex
059a8bb7 337stsv bstate->bs_sv U32 s
92742e37 338stop PL_op U32 s
059a8bb7 339stpv bstate->bs_pv.xpv_pv U32 x
340ldspecsv bstate->bs_sv U8 x
341newsv bstate->bs_sv U8 x
92742e37 342newop PL_op U8 x
343newopn PL_op U8 x
344newpv none PV
059a8bb7 345pv_cur bstate->bs_pv.xpv_cur STRLEN
346pv_free bstate->bs_pv none x
6e21dc91 347sv_upgrade bstate->bs_sv U8 x
059a8bb7 348sv_refcnt SvREFCNT(bstate->bs_sv) U32
349sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
350sv_flags SvFLAGS(bstate->bs_sv) U32
351xrv SvRV(bstate->bs_sv) svindex
352xpv bstate->bs_sv none x
353xiv32 SvIVX(bstate->bs_sv) I32
354xiv64 SvIVX(bstate->bs_sv) IV64
355xnv SvNVX(bstate->bs_sv) NV
356xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
357xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
358xlv_targ LvTARG(bstate->bs_sv) svindex
359xlv_type LvTYPE(bstate->bs_sv) char
360xbm_useful BmUSEFUL(bstate->bs_sv) I32
361xbm_previous BmPREVIOUS(bstate->bs_sv) U16
362xbm_rare BmRARE(bstate->bs_sv) U8
11a7ac70 363xfm_lines FmLINES(bstate->bs_sv) IV
364xio_lines IoLINES(bstate->bs_sv) IV
365xio_page IoPAGE(bstate->bs_sv) IV
366xio_page_len IoPAGE_LEN(bstate->bs_sv) IV
367xio_lines_left IoLINES_LEFT(bstate->bs_sv) IV
059a8bb7 368xio_top_name IoTOP_NAME(bstate->bs_sv) pvcontents
369xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
370xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvcontents
371xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
372xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvcontents
373xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
374xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
375xio_type IoTYPE(bstate->bs_sv) char
376xio_flags IoFLAGS(bstate->bs_sv) char
377xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
378xcv_start CvSTART(bstate->bs_sv) opindex
379xcv_root CvROOT(bstate->bs_sv) opindex
380xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
381xcv_file CvFILE(bstate->bs_sv) pvindex
382xcv_depth CvDEPTH(bstate->bs_sv) long
383xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
384xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
f52873be 385xcv_outside_seq CvOUTSIDE_SEQ(bstate->bs_sv) U32
059a8bb7 386xcv_flags CvFLAGS(bstate->bs_sv) U16
387av_extend bstate->bs_sv SSize_t x
388av_push bstate->bs_sv svindex x
389xav_fill AvFILLp(bstate->bs_sv) SSize_t
390xav_max AvMAX(bstate->bs_sv) SSize_t
391xav_flags AvFLAGS(bstate->bs_sv) U8
392xhv_riter HvRITER(bstate->bs_sv) I32
393xhv_name HvNAME(bstate->bs_sv) pvcontents
394hv_store bstate->bs_sv svindex x
395sv_magic bstate->bs_sv char x
396mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
397mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
398mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
399mg_pv SvMAGIC(bstate->bs_sv) pvcontents x
400xmg_stash *(SV**)&SvSTASH(bstate->bs_sv) svindex
401gv_fetchpv bstate->bs_sv strconst x
402gv_stashpv bstate->bs_sv strconst x
403gp_sv GvSV(bstate->bs_sv) svindex
404gp_refcnt GvREFCNT(bstate->bs_sv) U32
405gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
406gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
407gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
408gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
409gp_file GvFILE(bstate->bs_sv) pvindex
410gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
411gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
412gp_cvgen GvCVGEN(bstate->bs_sv) U32
413gp_line GvLINE(bstate->bs_sv) line_t
414gp_share bstate->bs_sv svindex x
415xgv_flags GvFLAGS(bstate->bs_sv) U8
92742e37 416op_next PL_op->op_next opindex
417op_sibling PL_op->op_sibling opindex
418op_ppaddr PL_op->op_ppaddr strconst x
419op_targ PL_op->op_targ PADOFFSET
420op_type PL_op OPCODE x
421op_seq PL_op->op_seq U16
422op_flags PL_op->op_flags U8
423op_private PL_op->op_private U8
424op_first cUNOP->op_first opindex
425op_last cBINOP->op_last opindex
426op_other cLOGOP->op_other opindex
92742e37 427op_pmreplroot cPMOP->op_pmreplroot opindex
428op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
429op_pmreplstart cPMOP->op_pmreplstart opindex
430op_pmnext *(OP**)&cPMOP->op_pmnext opindex
431pregcomp PL_op pvcontents x
432op_pmflags cPMOP->op_pmflags U16
433op_pmpermflags cPMOP->op_pmpermflags U16
434op_sv cSVOP->op_sv svindex
7934575e 435op_padix cPADOP->op_padix PADOFFSET
92742e37 436op_pv cPVOP->op_pv pvcontents
437op_pv_tr cPVOP->op_pv op_tr_array
438op_redoop cLOOP->op_redoop opindex
439op_nextop cLOOP->op_nextop opindex
440op_lastop cLOOP->op_lastop opindex
059a8bb7 441cop_label cCOP->cop_label pvindex
442cop_stashpv cCOP pvindex x
443cop_file cCOP pvindex x
92742e37 444cop_seq cCOP->cop_seq U32
445cop_arybase cCOP->cop_arybase I32
57843af0 446cop_line cCOP line_t x
b295d113 447cop_warnings cCOP->cop_warnings svindex
92742e37 448main_start PL_main_start opindex
449main_root PL_main_root opindex
450curpad PL_curpad svindex x
059a8bb7 451push_begin PL_beginav svindex x
452push_init PL_initav svindex x
453push_end PL_endav svindex x