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