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