more complete support for implicit thread/interpreter pointer,
[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
e8edd1e6 39unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/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#
e8edd1e6 65open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
a8a597b2 66print BYTERUN_C $c_header, <<'EOT';
67
68#include "EXTERN.h"
69#include "perl.h"
e8edd1e6 70#include "byterun.h"
71#include "bytecode.h"
72
73static int optype_size[] = {
74EOT
75my $i = 0;
76for ($i = 0; $i < @optype - 1; $i++) {
77 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
78}
79printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
80print BYTERUN_C <<'EOT';
81};
82
83static SV *specialsv_list[4];
84
85static int bytecode_iv_overflows = 0;
86static SV *bytecode_sv;
87static XPV bytecode_pv;
88static void **bytecode_obj_list;
89static I32 bytecode_obj_list_fill = -1;
d613ef02 90
91void *
92bset_obj_store(void *obj, I32 ix)
93{
e8edd1e6 94 if (ix > bytecode_obj_list_fill) {
95 if (bytecode_obj_list_fill == -1)
96 New(666, bytecode_obj_list, ix + 1, void*);
d613ef02 97 else
e8edd1e6 98 Renew(bytecode_obj_list, ix + 1, void*);
99 bytecode_obj_list_fill = ix;
d613ef02 100 }
e8edd1e6 101 bytecode_obj_list[ix] = obj;
d613ef02 102 return obj;
103}
a8a597b2 104
cea2e8a9 105void
106byterun(pTHX_ struct bytestream bs)
a8a597b2 107{
108 dTHR;
109 int insn;
e8edd1e6 110
111EOT
112
113for (my $i = 0; $i < @specialsv; $i++) {
114 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
115}
116
117print BYTERUN_C <<'EOT';
118
47358472 119 while ((insn = BGET_FGETC()) != EOF) {
a8a597b2 120 switch (insn) {
121EOT
122
123
124my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
125
126while (<DATA>) {
127 chop;
128 s/#.*//; # remove comments
129 next unless length;
130 if (/^%number\s+(.*)/) {
131 $insn_num = $1;
132 next;
133 } elsif (/%enum\s+(.*?)\s+(.*)/) {
134 create_enum($1, $2); # must come before instructions
135 next;
136 }
137 ($insn, $lvalue, $argtype, $flags) = split;
138 $insn_name[$insn_num] = $insn;
139 $fundtype = $alias_from{$argtype} || $argtype;
140
141 #
142 # Add the case statement and code for the bytecode interpreter in byterun.c
143 #
144 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
145 uc($insn), $insn_num;
146 my $optarg = $argtype eq "none" ? "" : ", arg";
147 if ($optarg) {
148 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
149 }
150 if ($flags =~ /x/) {
151 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
152 } elsif ($flags =~ /s/) {
e8edd1e6 153 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
a8a597b2 154 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
155 }
156 elsif ($optarg && $lvalue ne "none") {
157 print BYTERUN_C "\t\t$lvalue = arg;\n";
158 }
159 print BYTERUN_C "\t\tbreak;\n\t }\n";
160
161 #
162 # Add the initialiser line for %insn_data in Asmdata.pm
163 #
164 print ASMDATA_PM <<"EOT";
165\$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
166EOT
167
168 # Find the next unused instruction number
169 do { $insn_num++ } while $insn_name[$insn_num];
170}
171
172#
173# Finish off byterun.c
174#
175print BYTERUN_C <<'EOT';
176 default:
cea2e8a9 177 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
a8a597b2 178 /* NOTREACHED */
179 }
180 }
181}
182EOT
183
184#
185# Write the instruction and optype enum constants into byterun.h
186#
e8edd1e6 187open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
a8a597b2 188print BYTERUN_H $c_header, <<'EOT';
a8a597b2 189struct bytestream {
190 void *data;
191 int (*fgetc)(void *);
e8edd1e6 192 int (*fread)(char *, size_t, size_t, void *);
193 void (*freadpv)(U32, void *, XPV *);
a8a597b2 194};
a8a597b2 195
a8a597b2 196enum {
197EOT
198
199my $i = 0;
200my $add_enum_value = 0;
201my $max_insn;
202for ($i = 0; $i < @insn_name; $i++) {
203 $insn = uc($insn_name[$i]);
204 if (defined($insn)) {
205 $max_insn = $i;
206 if ($add_enum_value) {
207 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
208 $add_enum_value = 0;
209 } else {
210 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
211 }
212 } else {
213 $add_enum_value = 1;
214 }
215}
216
217print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
218
219print BYTERUN_H "\nenum {\n";
220for ($i = 0; $i < @optype - 1; $i++) {
221 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
222}
223printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
224print BYTERUN_H <<'EOT';
015b361d 225EXT int PL_optype_size[]
a8a597b2 226#ifdef DOINIT
227= {
228EOT
229for ($i = 0; $i < @optype - 1; $i++) {
230 printf BYTERUN_H " sizeof(%s),\n", $optype[$i], $i;
231}
232printf BYTERUN_H " sizeof(%s)\n}\n", $optype[$i], $i;
233print BYTERUN_H <<'EOT';
234#endif /* DOINIT */
235;
236
237EOT
238
fe3a57c4 239print BYTERUN_H <<'EOT';
a8a597b2 240#define INIT_SPECIALSV_LIST STMT_START { \
241EOT
242for ($i = 0; $i < @specialsv; $i++) {
6b88bc9c 243 print BYTERUN_H "\tPL_specialsv_list[$i] = $specialsv[$i]; \\\n";
a8a597b2 244}
245print BYTERUN_H <<'EOT';
33b839e2 246 } STMT_END
a8a597b2 247EOT
248
249#
250# Finish off insn_data and create array initialisers in Asmdata.pm
251#
252print ASMDATA_PM <<'EOT';
253
254my ($insn_name, $insn_data);
255while (($insn_name, $insn_data) = each %insn_data) {
256 $insn_name[$insn_data->[0]] = $insn_name;
257}
258# Fill in any gaps
259@insn_name = map($_ || "unused", @insn_name);
260
2611;
42d3a99d 262
263__END__
264
265=head1 NAME
266
267B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
268
269=head1 SYNOPSIS
270
271 use Asmdata;
272
273=head1 DESCRIPTION
274
275See F<ext/B/B/Asmdata.pm>.
276
277=head1 AUTHOR
278
279Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
280
281=cut
a8a597b2 282EOT
283
284__END__
285# First set instruction ord("#") to read comment to end-of-line (sneaky)
286%number 35
fe3a57c4 287comment arg comment_t
a8a597b2 288# Then make ord("\n") into a no-op
289%number 10
290nop none none
291# Now for the rest of the ordinary ones, beginning with \0 which is
292# ret so that \0-terminated strings can be read properly as bytecode.
293%number 0
294#
92742e37 295#opcode lvalue argtype flags
a8a597b2 296#
92742e37 297ret none none x
e8edd1e6 298ldsv bytecode_sv svindex
92742e37 299ldop PL_op opindex
e8edd1e6 300stsv bytecode_sv U32 s
92742e37 301stop PL_op U32 s
e8edd1e6 302ldspecsv bytecode_sv U8 x
303newsv bytecode_sv U8 x
92742e37 304newop PL_op U8 x
305newopn PL_op U8 x
306newpv none PV
e8edd1e6 307pv_cur bytecode_pv.xpv_cur STRLEN
308pv_free bytecode_pv none x
309sv_upgrade bytecode_sv char x
310sv_refcnt SvREFCNT(bytecode_sv) U32
311sv_refcnt_add SvREFCNT(bytecode_sv) I32 x
312sv_flags SvFLAGS(bytecode_sv) U32
313xrv SvRV(bytecode_sv) svindex
314xpv bytecode_sv none x
315xiv32 SvIVX(bytecode_sv) I32
316xiv64 SvIVX(bytecode_sv) IV64
317xnv SvNVX(bytecode_sv) double
318xlv_targoff LvTARGOFF(bytecode_sv) STRLEN
319xlv_targlen LvTARGLEN(bytecode_sv) STRLEN
320xlv_targ LvTARG(bytecode_sv) svindex
321xlv_type LvTYPE(bytecode_sv) char
322xbm_useful BmUSEFUL(bytecode_sv) I32
323xbm_previous BmPREVIOUS(bytecode_sv) U16
324xbm_rare BmRARE(bytecode_sv) U8
325xfm_lines FmLINES(bytecode_sv) I32
326xio_lines IoLINES(bytecode_sv) long
327xio_page IoPAGE(bytecode_sv) long
328xio_page_len IoPAGE_LEN(bytecode_sv) long
329xio_lines_left IoLINES_LEFT(bytecode_sv) long
330xio_top_name IoTOP_NAME(bytecode_sv) pvcontents
331xio_top_gv *(SV**)&IoTOP_GV(bytecode_sv) svindex
332xio_fmt_name IoFMT_NAME(bytecode_sv) pvcontents
333xio_fmt_gv *(SV**)&IoFMT_GV(bytecode_sv) svindex
334xio_bottom_name IoBOTTOM_NAME(bytecode_sv) pvcontents
335xio_bottom_gv *(SV**)&IoBOTTOM_GV(bytecode_sv) svindex
336xio_subprocess IoSUBPROCESS(bytecode_sv) short
337xio_type IoTYPE(bytecode_sv) char
338xio_flags IoFLAGS(bytecode_sv) char
339xcv_stash *(SV**)&CvSTASH(bytecode_sv) svindex
340xcv_start CvSTART(bytecode_sv) opindex
341xcv_root CvROOT(bytecode_sv) opindex
342xcv_gv *(SV**)&CvGV(bytecode_sv) svindex
343xcv_filegv *(SV**)&CvFILEGV(bytecode_sv) svindex
344xcv_depth CvDEPTH(bytecode_sv) long
345xcv_padlist *(SV**)&CvPADLIST(bytecode_sv) svindex
346xcv_outside *(SV**)&CvOUTSIDE(bytecode_sv) svindex
347xcv_flags CvFLAGS(bytecode_sv) U8
348av_extend bytecode_sv SSize_t x
349av_push bytecode_sv svindex x
350xav_fill AvFILLp(bytecode_sv) SSize_t
351xav_max AvMAX(bytecode_sv) SSize_t
352xav_flags AvFLAGS(bytecode_sv) U8
353xhv_riter HvRITER(bytecode_sv) I32
354xhv_name HvNAME(bytecode_sv) pvcontents
355hv_store bytecode_sv svindex x
356sv_magic bytecode_sv char x
357mg_obj SvMAGIC(bytecode_sv)->mg_obj svindex
358mg_private SvMAGIC(bytecode_sv)->mg_private U16
359mg_flags SvMAGIC(bytecode_sv)->mg_flags U8
360mg_pv SvMAGIC(bytecode_sv) pvcontents x
361xmg_stash *(SV**)&SvSTASH(bytecode_sv) svindex
362gv_fetchpv bytecode_sv strconst x
363gv_stashpv bytecode_sv strconst x
364gp_sv GvSV(bytecode_sv) svindex
365gp_refcnt GvREFCNT(bytecode_sv) U32
366gp_refcnt_add GvREFCNT(bytecode_sv) I32 x
367gp_av *(SV**)&GvAV(bytecode_sv) svindex
368gp_hv *(SV**)&GvHV(bytecode_sv) svindex
369gp_cv *(SV**)&GvCV(bytecode_sv) svindex
370gp_filegv *(SV**)&GvFILEGV(bytecode_sv) svindex
371gp_io *(SV**)&GvIOp(bytecode_sv) svindex
372gp_form *(SV**)&GvFORM(bytecode_sv) svindex
373gp_cvgen GvCVGEN(bytecode_sv) U32
374gp_line GvLINE(bytecode_sv) line_t
375gp_share bytecode_sv svindex x
376xgv_flags GvFLAGS(bytecode_sv) U8
92742e37 377op_next PL_op->op_next opindex
378op_sibling PL_op->op_sibling opindex
379op_ppaddr PL_op->op_ppaddr strconst x
380op_targ PL_op->op_targ PADOFFSET
381op_type PL_op OPCODE x
382op_seq PL_op->op_seq U16
383op_flags PL_op->op_flags U8
384op_private PL_op->op_private U8
385op_first cUNOP->op_first opindex
386op_last cBINOP->op_last opindex
387op_other cLOGOP->op_other opindex
388op_true cCONDOP->op_true opindex
389op_false cCONDOP->op_false opindex
390op_children cLISTOP->op_children U32
391op_pmreplroot cPMOP->op_pmreplroot opindex
392op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
393op_pmreplstart cPMOP->op_pmreplstart opindex
394op_pmnext *(OP**)&cPMOP->op_pmnext opindex
395pregcomp PL_op pvcontents x
396op_pmflags cPMOP->op_pmflags U16
397op_pmpermflags cPMOP->op_pmpermflags U16
398op_sv cSVOP->op_sv svindex
399op_gv *(SV**)&cGVOP->op_gv svindex
400op_pv cPVOP->op_pv pvcontents
401op_pv_tr cPVOP->op_pv op_tr_array
402op_redoop cLOOP->op_redoop opindex
403op_nextop cLOOP->op_nextop opindex
404op_lastop cLOOP->op_lastop opindex
405cop_label cCOP->cop_label pvcontents
406cop_stash *(SV**)&cCOP->cop_stash svindex
407cop_filegv *(SV**)&cCOP->cop_filegv svindex
408cop_seq cCOP->cop_seq U32
409cop_arybase cCOP->cop_arybase I32
410cop_line cCOP->cop_line line_t
b295d113 411cop_warnings cCOP->cop_warnings svindex
92742e37 412main_start PL_main_start opindex
413main_root PL_main_root opindex
414curpad PL_curpad svindex x