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