6 U32 => [qw(PADOFFSET STRLEN)],
7 I32 => [qw(SSize_t long)],
8 U16 => [qw(OPCODE line_t short)],
12 my @optype= qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP);
14 # Nullsv *must* come first in the following so that the condition
15 # ($$sv == 0) can continue to be used to test (sv == Nullsv).
16 my @specialsv = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no pWARN_ALL pWARN_NONE);
18 my (%alias_from, $from, $tos);
19 while (($from, $tos) = each %alias_to) {
20 map { $alias_from{$_} = $from } @$tos;
23 my $c_header = <<'EOT';
25 * Copyright (c) 1996-1999 Malcolm Beattie
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.
32 * This file is autogenerated from bytecode.pl. Changes made here will be lost.
37 ($perl_header = $c_header) =~ s{[/ ]?\*/?}{#}g;
39 unlink "ext/ByteLoader/byterun.c", "ext/ByteLoader/byterun.h", "ext/B/B/Asmdata.pm";
42 # Start with boilerplate for Asmdata.pm
44 open(ASMDATA_PM, ">ext/B/B/Asmdata.pm") or die "ext/B/B/Asmdata.pm: $!";
45 print ASMDATA_PM $perl_header, <<'EOT';
49 @EXPORT_OK = qw(%insn_data @insn_name @optype @specialsv_name);
50 our(%insn_data, @insn_name, @optype, @specialsv_name);
53 print ASMDATA_PM <<"EOT";
54 \@optype = qw(@optype);
55 \@specialsv_name = qw(@specialsv);
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.
63 # Boilerplate for byterun.c
65 open(BYTERUN_C, ">ext/ByteLoader/byterun.c") or die "ext/ByteLoader/byterun.c: $!";
66 print BYTERUN_C $c_header, <<'EOT';
68 #define PERL_NO_GET_CONTEXT
76 #define CALL_FPTR(fptr) (pPerl->*fptr)
78 #define PL_ppaddr (*get_ppaddr())
85 static const int optype_size[] = {
88 for ($i = 0; $i < @optype - 1; $i++) {
89 printf BYTERUN_C " sizeof(%s),\n", $optype[$i], $i;
91 printf BYTERUN_C " sizeof(%s)\n", $optype[$i], $i;
92 print BYTERUN_C <<'EOT';
96 bset_obj_store(pTHXo_ struct byteloader_state *bstate, void *obj, I32 ix)
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;
102 bstate->bs_obj_list[ix] = obj;
107 byterun(pTHXo_ register struct byteloader_state *bstate)
112 SV *specialsv_list[6];
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;
120 for (my $i = 0; $i < @specialsv; $i++) {
121 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
124 print BYTERUN_C <<'EOT';
126 while ((insn = BGET_FGETC()) != EOF) {
131 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
135 s/#.*//; # remove comments
137 if (/^%number\s+(.*)/) {
140 } elsif (/%enum\s+(.*?)\s+(.*)/) {
141 create_enum($1, $2); # must come before instructions
144 ($insn, $lvalue, $argtype, $flags) = split;
145 $insn_name[$insn_num] = $insn;
146 $fundtype = $alias_from{$argtype} || $argtype;
149 # Add the case statement and code for the bytecode interpreter in byterun.c
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";
155 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
158 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
159 } elsif ($flags =~ /s/) {
160 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
161 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
163 elsif ($optarg && $lvalue ne "none") {
164 print BYTERUN_C "\t\t$lvalue = arg;\n";
166 print BYTERUN_C "\t\tbreak;\n\t }\n";
169 # Add the initialiser line for %insn_data in Asmdata.pm
171 print ASMDATA_PM <<"EOT";
172 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
175 # Find the next unused instruction number
176 do { $insn_num++ } while $insn_name[$insn_num];
180 # Finish off byterun.c
182 print BYTERUN_C <<'EOT';
184 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
192 # Write the instruction and optype enum constants into byterun.h
194 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
195 print BYTERUN_H $c_header, <<'EOT';
196 struct byteloader_fdata {
202 struct byteloader_state {
203 struct byteloader_fdata *bs_fdata;
206 int bs_obj_list_fill;
211 int bl_getc(struct byteloader_fdata *);
212 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
213 extern void byterun(pTHXo_ struct byteloader_state *);
218 my $add_enum_value = 0;
220 for ($i = 0; $i < @insn_name; $i++) {
221 $insn = uc($insn_name[$i]);
222 if (defined($insn)) {
224 if ($add_enum_value) {
225 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
228 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
235 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
237 print BYTERUN_H "\nenum {\n";
238 for ($i = 0; $i < @optype - 1; $i++) {
239 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
241 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
244 # Finish off insn_data and create array initialisers in Asmdata.pm
246 print ASMDATA_PM <<'EOT';
248 my ($insn_name, $insn_data);
249 while (($insn_name, $insn_data) = each %insn_data) {
250 $insn_name[$insn_data->[0]] = $insn_name;
253 @insn_name = map($_ || "unused", @insn_name);
261 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
269 See F<ext/B/B/Asmdata.pm>.
273 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
279 # First set instruction ord("#") to read comment to end-of-line (sneaky)
281 comment arg comment_t
282 # Then make ord("\n") into a no-op
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.
289 #opcode lvalue argtype flags
292 ldsv bstate->bs_sv svindex
294 stsv bstate->bs_sv U32 s
296 stpv bstate->bs_pv.xpv_pv U32 x
297 ldspecsv bstate->bs_sv U8 x
298 newsv bstate->bs_sv U8 x
302 pv_cur bstate->bs_pv.xpv_cur STRLEN
303 pv_free bstate->bs_pv none x
304 sv_upgrade bstate->bs_sv char x
305 sv_refcnt SvREFCNT(bstate->bs_sv) U32
306 sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
307 sv_flags SvFLAGS(bstate->bs_sv) U32
308 xrv SvRV(bstate->bs_sv) svindex
309 xpv bstate->bs_sv none x
310 xiv32 SvIVX(bstate->bs_sv) I32
311 xiv64 SvIVX(bstate->bs_sv) IV64
312 xnv SvNVX(bstate->bs_sv) NV
313 xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
314 xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
315 xlv_targ LvTARG(bstate->bs_sv) svindex
316 xlv_type LvTYPE(bstate->bs_sv) char
317 xbm_useful BmUSEFUL(bstate->bs_sv) I32
318 xbm_previous BmPREVIOUS(bstate->bs_sv) U16
319 xbm_rare BmRARE(bstate->bs_sv) U8
320 xfm_lines FmLINES(bstate->bs_sv) I32
321 xio_lines IoLINES(bstate->bs_sv) long
322 xio_page IoPAGE(bstate->bs_sv) long
323 xio_page_len IoPAGE_LEN(bstate->bs_sv) long
324 xio_lines_left IoLINES_LEFT(bstate->bs_sv) long
325 xio_top_name IoTOP_NAME(bstate->bs_sv) pvcontents
326 xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
327 xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvcontents
328 xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
329 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvcontents
330 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
331 xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
332 xio_type IoTYPE(bstate->bs_sv) char
333 xio_flags IoFLAGS(bstate->bs_sv) char
334 xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
335 xcv_start CvSTART(bstate->bs_sv) opindex
336 xcv_root CvROOT(bstate->bs_sv) opindex
337 xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
338 xcv_file CvFILE(bstate->bs_sv) pvindex
339 xcv_depth CvDEPTH(bstate->bs_sv) long
340 xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
341 xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
342 xcv_flags CvFLAGS(bstate->bs_sv) U16
343 av_extend bstate->bs_sv SSize_t x
344 av_push bstate->bs_sv svindex x
345 xav_fill AvFILLp(bstate->bs_sv) SSize_t
346 xav_max AvMAX(bstate->bs_sv) SSize_t
347 xav_flags AvFLAGS(bstate->bs_sv) U8
348 xhv_riter HvRITER(bstate->bs_sv) I32
349 xhv_name HvNAME(bstate->bs_sv) pvcontents
350 hv_store bstate->bs_sv svindex x
351 sv_magic bstate->bs_sv char x
352 mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
353 mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
354 mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
355 mg_pv SvMAGIC(bstate->bs_sv) pvcontents x
356 xmg_stash *(SV**)&SvSTASH(bstate->bs_sv) svindex
357 gv_fetchpv bstate->bs_sv strconst x
358 gv_stashpv bstate->bs_sv strconst x
359 gp_sv GvSV(bstate->bs_sv) svindex
360 gp_refcnt GvREFCNT(bstate->bs_sv) U32
361 gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
362 gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
363 gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
364 gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
365 gp_file GvFILE(bstate->bs_sv) pvindex
366 gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
367 gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
368 gp_cvgen GvCVGEN(bstate->bs_sv) U32
369 gp_line GvLINE(bstate->bs_sv) line_t
370 gp_share bstate->bs_sv svindex x
371 xgv_flags GvFLAGS(bstate->bs_sv) U8
372 op_next PL_op->op_next opindex
373 op_sibling PL_op->op_sibling opindex
374 op_ppaddr PL_op->op_ppaddr strconst x
375 op_targ PL_op->op_targ PADOFFSET
376 op_type PL_op OPCODE x
377 op_seq PL_op->op_seq U16
378 op_flags PL_op->op_flags U8
379 op_private PL_op->op_private U8
380 op_first cUNOP->op_first opindex
381 op_last cBINOP->op_last opindex
382 op_other cLOGOP->op_other opindex
383 op_children cLISTOP->op_children U32
384 op_pmreplroot cPMOP->op_pmreplroot opindex
385 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
386 op_pmreplstart cPMOP->op_pmreplstart opindex
387 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
388 pregcomp PL_op pvcontents x
389 op_pmflags cPMOP->op_pmflags U16
390 op_pmpermflags cPMOP->op_pmpermflags U16
391 op_sv cSVOP->op_sv svindex
392 op_padix cPADOP->op_padix PADOFFSET
393 op_pv cPVOP->op_pv pvcontents
394 op_pv_tr cPVOP->op_pv op_tr_array
395 op_redoop cLOOP->op_redoop opindex
396 op_nextop cLOOP->op_nextop opindex
397 op_lastop cLOOP->op_lastop opindex
398 cop_label cCOP->cop_label pvindex
399 cop_stashpv cCOP pvindex x
400 cop_file cCOP pvindex x
401 cop_seq cCOP->cop_seq U32
402 cop_arybase cCOP->cop_arybase I32
403 cop_line cCOP line_t x
404 cop_warnings cCOP->cop_warnings svindex
405 main_start PL_main_start opindex
406 main_root PL_main_root opindex
407 curpad PL_curpad svindex x
408 push_begin PL_beginav svindex x
409 push_init PL_initav svindex x
410 push_end PL_endav svindex x