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