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)
111 SV *specialsv_list[6];
113 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
114 New(666, bstate->bs_obj_list, 32, void*); /* set op objlist */
115 bstate->bs_obj_list_fill = 31;
119 for (my $i = 0; $i < @specialsv; $i++) {
120 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
123 print BYTERUN_C <<'EOT';
125 while ((insn = BGET_FGETC()) != EOF) {
130 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
134 s/#.*//; # remove comments
136 if (/^%number\s+(.*)/) {
139 } elsif (/%enum\s+(.*?)\s+(.*)/) {
140 create_enum($1, $2); # must come before instructions
143 ($insn, $lvalue, $argtype, $flags) = split;
144 $insn_name[$insn_num] = $insn;
145 $fundtype = $alias_from{$argtype} || $argtype;
148 # Add the case statement and code for the bytecode interpreter in byterun.c
150 printf BYTERUN_C "\t case INSN_%s:\t\t/* %d */\n\t {\n",
151 uc($insn), $insn_num;
152 my $optarg = $argtype eq "none" ? "" : ", arg";
154 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
157 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
158 } elsif ($flags =~ /s/) {
159 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
160 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
162 elsif ($optarg && $lvalue ne "none") {
163 print BYTERUN_C "\t\t$lvalue = arg;\n";
165 print BYTERUN_C "\t\tbreak;\n\t }\n";
168 # Add the initialiser line for %insn_data in Asmdata.pm
170 print ASMDATA_PM <<"EOT";
171 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
174 # Find the next unused instruction number
175 do { $insn_num++ } while $insn_name[$insn_num];
179 # Finish off byterun.c
181 print BYTERUN_C <<'EOT';
183 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
191 # Write the instruction and optype enum constants into byterun.h
193 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
194 print BYTERUN_H $c_header, <<'EOT';
195 struct byteloader_fdata {
201 struct byteloader_state {
202 struct byteloader_fdata *bs_fdata;
205 int bs_obj_list_fill;
210 int bl_getc(struct byteloader_fdata *);
211 int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
212 extern void byterun(pTHXo_ struct byteloader_state *);
217 my $add_enum_value = 0;
219 for ($i = 0; $i < @insn_name; $i++) {
220 $insn = uc($insn_name[$i]);
221 if (defined($insn)) {
223 if ($add_enum_value) {
224 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
227 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
234 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
236 print BYTERUN_H "\nenum {\n";
237 for ($i = 0; $i < @optype - 1; $i++) {
238 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
240 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
243 # Finish off insn_data and create array initialisers in Asmdata.pm
245 print ASMDATA_PM <<'EOT';
247 my ($insn_name, $insn_data);
248 while (($insn_name, $insn_data) = each %insn_data) {
249 $insn_name[$insn_data->[0]] = $insn_name;
252 @insn_name = map($_ || "unused", @insn_name);
260 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
268 See F<ext/B/B/Asmdata.pm>.
272 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
278 # First set instruction ord("#") to read comment to end-of-line (sneaky)
280 comment arg comment_t
281 # Then make ord("\n") into a no-op
284 # Now for the rest of the ordinary ones, beginning with \0 which is
285 # ret so that \0-terminated strings can be read properly as bytecode.
288 #opcode lvalue argtype flags
291 ldsv bstate->bs_sv svindex
293 stsv bstate->bs_sv U32 s
295 stpv bstate->bs_pv.xpv_pv U32 x
296 ldspecsv bstate->bs_sv U8 x
297 newsv bstate->bs_sv U8 x
301 pv_cur bstate->bs_pv.xpv_cur STRLEN
302 pv_free bstate->bs_pv none x
303 sv_upgrade bstate->bs_sv char x
304 sv_refcnt SvREFCNT(bstate->bs_sv) U32
305 sv_refcnt_add SvREFCNT(bstate->bs_sv) I32 x
306 sv_flags SvFLAGS(bstate->bs_sv) U32
307 xrv SvRV(bstate->bs_sv) svindex
308 xpv bstate->bs_sv none x
309 xiv32 SvIVX(bstate->bs_sv) I32
310 xiv64 SvIVX(bstate->bs_sv) IV64
311 xnv SvNVX(bstate->bs_sv) NV
312 xlv_targoff LvTARGOFF(bstate->bs_sv) STRLEN
313 xlv_targlen LvTARGLEN(bstate->bs_sv) STRLEN
314 xlv_targ LvTARG(bstate->bs_sv) svindex
315 xlv_type LvTYPE(bstate->bs_sv) char
316 xbm_useful BmUSEFUL(bstate->bs_sv) I32
317 xbm_previous BmPREVIOUS(bstate->bs_sv) U16
318 xbm_rare BmRARE(bstate->bs_sv) U8
319 xfm_lines FmLINES(bstate->bs_sv) I32
320 xio_lines IoLINES(bstate->bs_sv) long
321 xio_page IoPAGE(bstate->bs_sv) long
322 xio_page_len IoPAGE_LEN(bstate->bs_sv) long
323 xio_lines_left IoLINES_LEFT(bstate->bs_sv) long
324 xio_top_name IoTOP_NAME(bstate->bs_sv) pvcontents
325 xio_top_gv *(SV**)&IoTOP_GV(bstate->bs_sv) svindex
326 xio_fmt_name IoFMT_NAME(bstate->bs_sv) pvcontents
327 xio_fmt_gv *(SV**)&IoFMT_GV(bstate->bs_sv) svindex
328 xio_bottom_name IoBOTTOM_NAME(bstate->bs_sv) pvcontents
329 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bstate->bs_sv) svindex
330 xio_subprocess IoSUBPROCESS(bstate->bs_sv) short
331 xio_type IoTYPE(bstate->bs_sv) char
332 xio_flags IoFLAGS(bstate->bs_sv) char
333 xcv_stash *(SV**)&CvSTASH(bstate->bs_sv) svindex
334 xcv_start CvSTART(bstate->bs_sv) opindex
335 xcv_root CvROOT(bstate->bs_sv) opindex
336 xcv_gv *(SV**)&CvGV(bstate->bs_sv) svindex
337 xcv_file CvFILE(bstate->bs_sv) pvindex
338 xcv_depth CvDEPTH(bstate->bs_sv) long
339 xcv_padlist *(SV**)&CvPADLIST(bstate->bs_sv) svindex
340 xcv_outside *(SV**)&CvOUTSIDE(bstate->bs_sv) svindex
341 xcv_flags CvFLAGS(bstate->bs_sv) U16
342 av_extend bstate->bs_sv SSize_t x
343 av_push bstate->bs_sv svindex x
344 xav_fill AvFILLp(bstate->bs_sv) SSize_t
345 xav_max AvMAX(bstate->bs_sv) SSize_t
346 xav_flags AvFLAGS(bstate->bs_sv) U8
347 xhv_riter HvRITER(bstate->bs_sv) I32
348 xhv_name HvNAME(bstate->bs_sv) pvcontents
349 hv_store bstate->bs_sv svindex x
350 sv_magic bstate->bs_sv char x
351 mg_obj SvMAGIC(bstate->bs_sv)->mg_obj svindex
352 mg_private SvMAGIC(bstate->bs_sv)->mg_private U16
353 mg_flags SvMAGIC(bstate->bs_sv)->mg_flags U8
354 mg_pv SvMAGIC(bstate->bs_sv) pvcontents x
355 xmg_stash *(SV**)&SvSTASH(bstate->bs_sv) svindex
356 gv_fetchpv bstate->bs_sv strconst x
357 gv_stashpv bstate->bs_sv strconst x
358 gp_sv GvSV(bstate->bs_sv) svindex
359 gp_refcnt GvREFCNT(bstate->bs_sv) U32
360 gp_refcnt_add GvREFCNT(bstate->bs_sv) I32 x
361 gp_av *(SV**)&GvAV(bstate->bs_sv) svindex
362 gp_hv *(SV**)&GvHV(bstate->bs_sv) svindex
363 gp_cv *(SV**)&GvCV(bstate->bs_sv) svindex
364 gp_file GvFILE(bstate->bs_sv) pvindex
365 gp_io *(SV**)&GvIOp(bstate->bs_sv) svindex
366 gp_form *(SV**)&GvFORM(bstate->bs_sv) svindex
367 gp_cvgen GvCVGEN(bstate->bs_sv) U32
368 gp_line GvLINE(bstate->bs_sv) line_t
369 gp_share bstate->bs_sv svindex x
370 xgv_flags GvFLAGS(bstate->bs_sv) U8
371 op_next PL_op->op_next opindex
372 op_sibling PL_op->op_sibling opindex
373 op_ppaddr PL_op->op_ppaddr strconst x
374 op_targ PL_op->op_targ PADOFFSET
375 op_type PL_op OPCODE x
376 op_seq PL_op->op_seq U16
377 op_flags PL_op->op_flags U8
378 op_private PL_op->op_private U8
379 op_first cUNOP->op_first opindex
380 op_last cBINOP->op_last opindex
381 op_other cLOGOP->op_other opindex
382 op_pmreplroot cPMOP->op_pmreplroot opindex
383 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
384 op_pmreplstart cPMOP->op_pmreplstart opindex
385 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
386 pregcomp PL_op pvcontents x
387 op_pmflags cPMOP->op_pmflags U16
388 op_pmpermflags cPMOP->op_pmpermflags U16
389 op_sv cSVOP->op_sv svindex
390 op_padix cPADOP->op_padix PADOFFSET
391 op_pv cPVOP->op_pv pvcontents
392 op_pv_tr cPVOP->op_pv op_tr_array
393 op_redoop cLOOP->op_redoop opindex
394 op_nextop cLOOP->op_nextop opindex
395 op_lastop cLOOP->op_lastop opindex
396 cop_label cCOP->cop_label pvindex
397 cop_stashpv cCOP pvindex x
398 cop_file cCOP pvindex x
399 cop_seq cCOP->cop_seq U32
400 cop_arybase cCOP->cop_arybase I32
401 cop_line cCOP line_t x
402 cop_warnings cCOP->cop_warnings svindex
403 main_start PL_main_start opindex
404 main_root PL_main_root opindex
405 curpad PL_curpad svindex x
406 push_begin PL_beginav svindex x
407 push_init PL_initav svindex x
408 push_end PL_endav svindex x