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