remove redundant part of change#1169 superseded by change#2061;
[p5sagit/p5-mst-13.2.git] / bytecode.pl
CommitLineData
73f0cc2d 1BEGIN {
2 push @INC, './lib';
3}
a8a597b2 4use strict;
5my %alias_to = (
6 U32 => [qw(PADOFFSET STRLEN)],
7 I32 => [qw(SSize_t long)],
8 U16 => [qw(OPCODE line_t short)],
9 U8 => [qw(char)],
a8a597b2 10);
11
12my @optype= qw(OP UNOP BINOP LOGOP CONDOP LISTOP PMOP SVOP GVOP PVOP LOOP COP);
13
14# Nullsv *must* come first in the following so that the condition
15# ($$sv == 0) can continue to be used to test (sv == Nullsv).
6b88bc9c 16my @specialsv = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no);
a8a597b2 17
18my (%alias_from, $from, $tos);
19while (($from, $tos) = each %alias_to) {
20 map { $alias_from{$_} = $from } @$tos;
21}
22
23my $c_header = <<'EOT';
24/*
4eb8286e 25 * Copyright (c) 1996-1999 Malcolm Beattie
a8a597b2 26 *
27 * You may distribute under the terms of either the GNU General Public
28 * License or the Artistic License, as specified in the README file.
29 *
30 */
31/*
32 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
33 */
34EOT
35
36my $perl_header;
37($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
38
33b839e2 39unlink "byterun.c", "byterun.h", "ext/B/B/Asmdata.pm";
a8a597b2 40
41#
42# Start with boilerplate for Asmdata.pm
43#
33b839e2 44open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
a8a597b2 45print ASMDATA_PM $perl_header, <<'EOT';
46package B::Asmdata;
47use Exporter;
48@ISA = qw(Exporter);
49@EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
50use vars qw(%insn_data @insn_name @optype @specialsv_name);
51
52EOT
53print ASMDATA_PM <<"EOT";
54\@optype = qw(@optype);
55\@specialsv_name = qw(@specialsv);
56
57# XXX insn_data is initialised this way because with a large
58# %insn_data = (foo => [...], bar => [...], ...) initialiser
59# I get a hard-to-track-down stack underflow and segfault.
60EOT
61
62#
63# Boilerplate for byterun.c
64#
65open(BYTERUN_C, ">byterun.c") or die "byterun.c: $!";
66print BYTERUN_C $c_header, <<'EOT';
67
68#include "EXTERN.h"
69#include "perl.h"
d613ef02 70
71void *
72bset_obj_store(void *obj, I32 ix)
73{
92742e37 74 if (ix > PL_bytecode_obj_list_fill) {
75 if (PL_bytecode_obj_list_fill == -1)
76 New(666, PL_bytecode_obj_list, ix + 1, void*);
d613ef02 77 else
92742e37 78 Renew(PL_bytecode_obj_list, ix + 1, void*);
79 PL_bytecode_obj_list_fill = ix;
d613ef02 80 }
92742e37 81 PL_bytecode_obj_list[ix] = obj;
d613ef02 82 return obj;
83}
a8a597b2 84
85#ifdef INDIRECT_BGET_MACROS
33b839e2 86void byterun(struct bytestream bs)
a8a597b2 87#else
4b534093 88void byterun(PerlIO *fp)
a8a597b2 89#endif /* INDIRECT_BGET_MACROS */
90{
91 dTHR;
92 int insn;
47358472 93 while ((insn = BGET_FGETC()) != EOF) {
a8a597b2 94 switch (insn) {
95EOT
96
97
98my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
99
100while (<DATA>) {
101 chop;
102 s/#.*//; # remove comments
103 next unless length;
104 if (/^%number\s+(.*)/) {
105 $insn_num = $1;
106 next;
107 } elsif (/%enum\s+(.*?)\s+(.*)/) {
108 create_enum($1, $2); # must come before instructions
109 next;
110 }
111 ($insn, $lvalue, $argtype, $flags) = split;
112 $insn_name[$insn_num] = $insn;
113 $fundtype = $alias_from{$argtype} || $argtype;
114
115 #
116 # Add the case statement and code for the bytecode interpreter in byterun.c
117 #
118 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
119 uc($insn), $insn_num;
120 my $optarg = $argtype eq "none" ? "" : ", arg";
121 if ($optarg) {
122 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
123 }
124 if ($flags =~ /x/) {
125 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
126 } elsif ($flags =~ /s/) {
92742e37 127 # Store instructions store to PL_bytecode_obj_list[arg]. "lvalue" field is rvalue.
a8a597b2 128 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
129 }
130 elsif ($optarg && $lvalue ne "none") {
131 print BYTERUN_C "\t\t$lvalue = arg;\n";
132 }
133 print BYTERUN_C "\t\tbreak;\n\t }\n";
134
135 #
136 # Add the initialiser line for %insn_data in Asmdata.pm
137 #
138 print ASMDATA_PM <<"EOT";
139\$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
140EOT
141
142 # Find the next unused instruction number
143 do { $insn_num++ } while $insn_name[$insn_num];
144}
145
146#
147# Finish off byterun.c
148#
149print BYTERUN_C <<'EOT';
150 default:
151 croak("Illegal bytecode instruction %d\n", insn);
152 /* NOTREACHED */
153 }
154 }
155}
156EOT
157
158#
159# Write the instruction and optype enum constants into byterun.h
160#
161open(BYTERUN_H, ">byterun.h") or die "byterun.h: $!";
162print BYTERUN_H $c_header, <<'EOT';
163#ifdef INDIRECT_BGET_MACROS
164struct bytestream {
165 void *data;
166 int (*fgetc)(void *);
167 int (*fread)(char *, size_t, size_t, void*);
168 void (*freadpv)(U32, void*);
169};
a8a597b2 170#endif /* INDIRECT_BGET_MACROS */
171
a8a597b2 172enum {
173EOT
174
175my $i = 0;
176my $add_enum_value = 0;
177my $max_insn;
178for ($i = 0; $i < @insn_name; $i++) {
179 $insn = uc($insn_name[$i]);
180 if (defined($insn)) {
181 $max_insn = $i;
182 if ($add_enum_value) {
183 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
184 $add_enum_value = 0;
185 } else {
186 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
187 }
188 } else {
189 $add_enum_value = 1;
190 }
191}
192
193print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
194
195print BYTERUN_H "\nenum {\n";
196for ($i = 0; $i < @optype - 1; $i++) {
197 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
198}
199printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
200print BYTERUN_H <<'EOT';
201EXT int optype_size[]
202#ifdef DOINIT
203= {
204EOT
205for ($i = 0; $i < @optype - 1; $i++) {
206 printf BYTERUN_H " sizeof(%s),\n", $optype[$i], $i;
207}
208printf BYTERUN_H " sizeof(%s)\n}\n", $optype[$i], $i;
209print BYTERUN_H <<'EOT';
210#endif /* DOINIT */
211;
212
213EOT
214
fe3a57c4 215print BYTERUN_H <<'EOT';
a8a597b2 216#define INIT_SPECIALSV_LIST STMT_START { \
217EOT
218for ($i = 0; $i < @specialsv; $i++) {
6b88bc9c 219 print BYTERUN_H "\tPL_specialsv_list[$i] = $specialsv[$i]; \\\n";
a8a597b2 220}
221print BYTERUN_H <<'EOT';
33b839e2 222 } STMT_END
a8a597b2 223EOT
224
225#
226# Finish off insn_data and create array initialisers in Asmdata.pm
227#
228print ASMDATA_PM <<'EOT';
229
230my ($insn_name, $insn_data);
231while (($insn_name, $insn_data) = each %insn_data) {
232 $insn_name[$insn_data->[0]] = $insn_name;
233}
234# Fill in any gaps
235@insn_name = map($_ || "unused", @insn_name);
236
2371;
42d3a99d 238
239__END__
240
241=head1 NAME
242
243B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
244
245=head1 SYNOPSIS
246
247 use Asmdata;
248
249=head1 DESCRIPTION
250
251See F<ext/B/B/Asmdata.pm>.
252
253=head1 AUTHOR
254
255Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
256
257=cut
a8a597b2 258EOT
259
260__END__
261# First set instruction ord("#") to read comment to end-of-line (sneaky)
262%number 35
fe3a57c4 263comment arg comment_t
a8a597b2 264# Then make ord("\n") into a no-op
265%number 10
266nop none none
267# Now for the rest of the ordinary ones, beginning with \0 which is
268# ret so that \0-terminated strings can be read properly as bytecode.
269%number 0
270#
92742e37 271#opcode lvalue argtype flags
a8a597b2 272#
92742e37 273ret none none x
274ldsv PL_bytecode_sv svindex
275ldop PL_op opindex
276stsv PL_bytecode_sv U32 s
277stop PL_op U32 s
278ldspecsv PL_bytecode_sv U8 x
279newsv PL_bytecode_sv U8 x
280newop PL_op U8 x
281newopn PL_op U8 x
282newpv none PV
283pv_cur PL_bytecode_pv.xpv_cur STRLEN
284pv_free PL_bytecode_pv none x
285sv_upgrade PL_bytecode_sv char x
286sv_refcnt SvREFCNT(PL_bytecode_sv) U32
287sv_refcnt_add SvREFCNT(PL_bytecode_sv) I32 x
288sv_flags SvFLAGS(PL_bytecode_sv) U32
289xrv SvRV(PL_bytecode_sv) svindex
290xpv PL_bytecode_sv none x
291xiv32 SvIVX(PL_bytecode_sv) I32
292xiv64 SvIVX(PL_bytecode_sv) IV64
293xnv SvNVX(PL_bytecode_sv) double
294xlv_targoff LvTARGOFF(PL_bytecode_sv) STRLEN
295xlv_targlen LvTARGLEN(PL_bytecode_sv) STRLEN
296xlv_targ LvTARG(PL_bytecode_sv) svindex
297xlv_type LvTYPE(PL_bytecode_sv) char
298xbm_useful BmUSEFUL(PL_bytecode_sv) I32
299xbm_previous BmPREVIOUS(PL_bytecode_sv) U16
300xbm_rare BmRARE(PL_bytecode_sv) U8
301xfm_lines FmLINES(PL_bytecode_sv) I32
302xio_lines IoLINES(PL_bytecode_sv) long
303xio_page IoPAGE(PL_bytecode_sv) long
304xio_page_len IoPAGE_LEN(PL_bytecode_sv) long
305xio_lines_left IoLINES_LEFT(PL_bytecode_sv) long
306xio_top_name IoTOP_NAME(PL_bytecode_sv) pvcontents
307xio_top_gv *(SV**)&IoTOP_GV(PL_bytecode_sv) svindex
308xio_fmt_name IoFMT_NAME(PL_bytecode_sv) pvcontents
309xio_fmt_gv *(SV**)&IoFMT_GV(PL_bytecode_sv) svindex
310xio_bottom_name IoBOTTOM_NAME(PL_bytecode_sv) pvcontents
311xio_bottom_gv *(SV**)&IoBOTTOM_GV(PL_bytecode_sv) svindex
312xio_subprocess IoSUBPROCESS(PL_bytecode_sv) short
313xio_type IoTYPE(PL_bytecode_sv) char
314xio_flags IoFLAGS(PL_bytecode_sv) char
315xcv_stash *(SV**)&CvSTASH(PL_bytecode_sv) svindex
316xcv_start CvSTART(PL_bytecode_sv) opindex
317xcv_root CvROOT(PL_bytecode_sv) opindex
318xcv_gv *(SV**)&CvGV(PL_bytecode_sv) svindex
319xcv_filegv *(SV**)&CvFILEGV(PL_bytecode_sv) svindex
320xcv_depth CvDEPTH(PL_bytecode_sv) long
321xcv_padlist *(SV**)&CvPADLIST(PL_bytecode_sv) svindex
322xcv_outside *(SV**)&CvOUTSIDE(PL_bytecode_sv) svindex
323xcv_flags CvFLAGS(PL_bytecode_sv) U8
324av_extend PL_bytecode_sv SSize_t x
325av_push PL_bytecode_sv svindex x
326xav_fill AvFILLp(PL_bytecode_sv) SSize_t
327xav_max AvMAX(PL_bytecode_sv) SSize_t
328xav_flags AvFLAGS(PL_bytecode_sv) U8
329xhv_riter HvRITER(PL_bytecode_sv) I32
330xhv_name HvNAME(PL_bytecode_sv) pvcontents
331hv_store PL_bytecode_sv svindex x
332sv_magic PL_bytecode_sv char x
333mg_obj SvMAGIC(PL_bytecode_sv)->mg_obj svindex
334mg_private SvMAGIC(PL_bytecode_sv)->mg_private U16
335mg_flags SvMAGIC(PL_bytecode_sv)->mg_flags U8
336mg_pv SvMAGIC(PL_bytecode_sv) pvcontents x
337xmg_stash *(SV**)&SvSTASH(PL_bytecode_sv) svindex
338gv_fetchpv PL_bytecode_sv strconst x
339gv_stashpv PL_bytecode_sv strconst x
340gp_sv GvSV(PL_bytecode_sv) svindex
341gp_refcnt GvREFCNT(PL_bytecode_sv) U32
342gp_refcnt_add GvREFCNT(PL_bytecode_sv) I32 x
343gp_av *(SV**)&GvAV(PL_bytecode_sv) svindex
344gp_hv *(SV**)&GvHV(PL_bytecode_sv) svindex
345gp_cv *(SV**)&GvCV(PL_bytecode_sv) svindex
346gp_filegv *(SV**)&GvFILEGV(PL_bytecode_sv) svindex
347gp_io *(SV**)&GvIOp(PL_bytecode_sv) svindex
348gp_form *(SV**)&GvFORM(PL_bytecode_sv) svindex
349gp_cvgen GvCVGEN(PL_bytecode_sv) U32
350gp_line GvLINE(PL_bytecode_sv) line_t
351gp_share PL_bytecode_sv svindex x
352xgv_flags GvFLAGS(PL_bytecode_sv) U8
353op_next PL_op->op_next opindex
354op_sibling PL_op->op_sibling opindex
355op_ppaddr PL_op->op_ppaddr strconst x
356op_targ PL_op->op_targ PADOFFSET
357op_type PL_op OPCODE x
358op_seq PL_op->op_seq U16
359op_flags PL_op->op_flags U8
360op_private PL_op->op_private U8
361op_first cUNOP->op_first opindex
362op_last cBINOP->op_last opindex
363op_other cLOGOP->op_other opindex
364op_true cCONDOP->op_true opindex
365op_false cCONDOP->op_false opindex
366op_children cLISTOP->op_children U32
367op_pmreplroot cPMOP->op_pmreplroot opindex
368op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
369op_pmreplstart cPMOP->op_pmreplstart opindex
370op_pmnext *(OP**)&cPMOP->op_pmnext opindex
371pregcomp PL_op pvcontents x
372op_pmflags cPMOP->op_pmflags U16
373op_pmpermflags cPMOP->op_pmpermflags U16
374op_sv cSVOP->op_sv svindex
375op_gv *(SV**)&cGVOP->op_gv svindex
376op_pv cPVOP->op_pv pvcontents
377op_pv_tr cPVOP->op_pv op_tr_array
378op_redoop cLOOP->op_redoop opindex
379op_nextop cLOOP->op_nextop opindex
380op_lastop cLOOP->op_lastop opindex
381cop_label cCOP->cop_label pvcontents
382cop_stash *(SV**)&cCOP->cop_stash svindex
383cop_filegv *(SV**)&cCOP->cop_filegv svindex
384cop_seq cCOP->cop_seq U32
385cop_arybase cCOP->cop_arybase I32
386cop_line cCOP->cop_line line_t
b295d113 387cop_warnings cCOP->cop_warnings svindex
92742e37 388main_start PL_main_start opindex
389main_root PL_main_root opindex
390curpad PL_curpad svindex x