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';
95 static int bytecode_iv_overflows = 0;
96 static void **bytecode_obj_list = Null(void**);
97 static I32 bytecode_obj_list_fill = -1;
100 bset_obj_store(pTHXo_ void *obj, I32 ix)
102 if (ix > bytecode_obj_list_fill) {
103 if (bytecode_obj_list_fill == -1)
104 New(666, bytecode_obj_list, ix + 32, void*);
106 Renew(bytecode_obj_list, ix + 32, void*);
107 bytecode_obj_list_fill = ix;
109 bytecode_obj_list[ix] = obj;
120 SV *specialsv_list[6];
122 SAVEVPTR(bytecode_obj_list);
123 SAVEI32(bytecode_obj_list_fill);
124 bytecode_obj_list = Null(void**);
125 bytecode_obj_list_fill = -1;
127 BYTECODE_HEADER_CHECK; /* croak if incorrect platform */
130 for (my $i = 0; $i < @specialsv; $i++) {
131 print BYTERUN_C " specialsv_list[$i] = $specialsv[$i];\n";
134 print BYTERUN_C <<'EOT';
136 while ((insn = BGET_FGETC()) != EOF) {
141 my (@insn_name, $insn_num, $insn, $lvalue, $argtype, $flags, $fundtype);
145 s/#.*//; # remove comments
147 if (/^%number\s+(.*)/) {
150 } elsif (/%enum\s+(.*?)\s+(.*)/) {
151 create_enum($1, $2); # must come before instructions
154 ($insn, $lvalue, $argtype, $flags) = split;
155 $insn_name[$insn_num] = $insn;
156 $fundtype = $alias_from{$argtype} || $argtype;
159 # Add the case statement and code for the bytecode interpreter in byterun.c
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";
165 printf BYTERUN_C "\t\t$argtype arg;\n\t\tBGET_%s(arg);\n", $fundtype;
168 print BYTERUN_C "\t\tBSET_$insn($lvalue$optarg);\n";
169 } elsif ($flags =~ /s/) {
170 # Store instructions store to bytecode_obj_list[arg]. "lvalue" field is rvalue.
171 print BYTERUN_C "\t\tBSET_OBJ_STORE($lvalue$optarg);\n";
173 elsif ($optarg && $lvalue ne "none") {
174 print BYTERUN_C "\t\t$lvalue = arg;\n";
176 print BYTERUN_C "\t\tbreak;\n\t }\n";
179 # Add the initialiser line for %insn_data in Asmdata.pm
181 print ASMDATA_PM <<"EOT";
182 \$insn_data{$insn} = [$insn_num, \\&PUT_$fundtype, "GET_$fundtype"];
185 # Find the next unused instruction number
186 do { $insn_num++ } while $insn_name[$insn_num];
190 # Finish off byterun.c
192 print BYTERUN_C <<'EOT';
194 Perl_croak(aTHX_ "Illegal bytecode instruction %d\n", insn);
202 # Write the instruction and optype enum constants into byterun.h
204 open(BYTERUN_H, ">ext/ByteLoader/byterun.h") or die "ext/ByteLoader/byterun.h: $!";
205 print BYTERUN_H $c_header, <<'EOT';
206 struct bytestream { /* XXX: not currently used - too slow */
208 int (*pfgetc)(void *);
209 int (*pfread)(char *, size_t, size_t, void *);
210 void (*pfreadpv)(U32, void *, XPV *);
216 my $add_enum_value = 0;
218 for ($i = 0; $i < @insn_name; $i++) {
219 $insn = uc($insn_name[$i]);
220 if (defined($insn)) {
222 if ($add_enum_value) {
223 print BYTERUN_H " INSN_$insn = $i,\t\t\t/* $i */\n";
226 print BYTERUN_H " INSN_$insn,\t\t\t/* $i */\n";
233 print BYTERUN_H " MAX_INSN = $max_insn\n};\n";
235 print BYTERUN_H "\nenum {\n";
236 for ($i = 0; $i < @optype - 1; $i++) {
237 printf BYTERUN_H " OPt_%s,\t\t/* %d */\n", $optype[$i], $i;
239 printf BYTERUN_H " OPt_%s\t\t/* %d */\n};\n\n", $optype[$i], $i;
241 print BYTERUN_H <<'EOT';
242 extern void byterun(pTHXo);
246 # Finish off insn_data and create array initialisers in Asmdata.pm
248 print ASMDATA_PM <<'EOT';
250 my ($insn_name, $insn_data);
251 while (($insn_name, $insn_data) = each %insn_data) {
252 $insn_name[$insn_data->[0]] = $insn_name;
255 @insn_name = map($_ || "unused", @insn_name);
263 B::Asmdata - Autogenerated data about Perl ops, used to generate bytecode
271 See F<ext/B/B/Asmdata.pm>.
275 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
281 # First set instruction ord("#") to read comment to end-of-line (sneaky)
283 comment arg comment_t
284 # Then make ord("\n") into a no-op
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.
291 #opcode lvalue argtype flags
294 ldsv bytecode_sv svindex
296 stsv bytecode_sv U32 s
298 ldspecsv bytecode_sv U8 x
299 newsv bytecode_sv U8 x
303 pv_cur bytecode_pv.xpv_cur STRLEN
304 pv_free bytecode_pv none x
305 sv_upgrade bytecode_sv char x
306 sv_refcnt SvREFCNT(bytecode_sv) U32
307 sv_refcnt_add SvREFCNT(bytecode_sv) I32 x
308 sv_flags SvFLAGS(bytecode_sv) U32
309 xrv SvRV(bytecode_sv) svindex
310 xpv bytecode_sv none x
311 xiv32 SvIVX(bytecode_sv) I32
312 xiv64 SvIVX(bytecode_sv) IV64
313 xnv SvNVX(bytecode_sv) NV
314 xlv_targoff LvTARGOFF(bytecode_sv) STRLEN
315 xlv_targlen LvTARGLEN(bytecode_sv) STRLEN
316 xlv_targ LvTARG(bytecode_sv) svindex
317 xlv_type LvTYPE(bytecode_sv) char
318 xbm_useful BmUSEFUL(bytecode_sv) I32
319 xbm_previous BmPREVIOUS(bytecode_sv) U16
320 xbm_rare BmRARE(bytecode_sv) U8
321 xfm_lines FmLINES(bytecode_sv) I32
322 xio_lines IoLINES(bytecode_sv) long
323 xio_page IoPAGE(bytecode_sv) long
324 xio_page_len IoPAGE_LEN(bytecode_sv) long
325 xio_lines_left IoLINES_LEFT(bytecode_sv) long
326 xio_top_name IoTOP_NAME(bytecode_sv) pvcontents
327 xio_top_gv *(SV**)&IoTOP_GV(bytecode_sv) svindex
328 xio_fmt_name IoFMT_NAME(bytecode_sv) pvcontents
329 xio_fmt_gv *(SV**)&IoFMT_GV(bytecode_sv) svindex
330 xio_bottom_name IoBOTTOM_NAME(bytecode_sv) pvcontents
331 xio_bottom_gv *(SV**)&IoBOTTOM_GV(bytecode_sv) svindex
332 xio_subprocess IoSUBPROCESS(bytecode_sv) short
333 xio_type IoTYPE(bytecode_sv) char
334 xio_flags IoFLAGS(bytecode_sv) char
335 xcv_stash *(SV**)&CvSTASH(bytecode_sv) svindex
336 xcv_start CvSTART(bytecode_sv) opindex
337 xcv_root CvROOT(bytecode_sv) opindex
338 xcv_gv *(SV**)&CvGV(bytecode_sv) svindex
339 xcv_file CvFILE(bytecode_sv) pvcontents
340 xcv_depth CvDEPTH(bytecode_sv) long
341 xcv_padlist *(SV**)&CvPADLIST(bytecode_sv) svindex
342 xcv_outside *(SV**)&CvOUTSIDE(bytecode_sv) svindex
343 xcv_flags CvFLAGS(bytecode_sv) U16
344 av_extend bytecode_sv SSize_t x
345 av_push bytecode_sv svindex x
346 xav_fill AvFILLp(bytecode_sv) SSize_t
347 xav_max AvMAX(bytecode_sv) SSize_t
348 xav_flags AvFLAGS(bytecode_sv) U8
349 xhv_riter HvRITER(bytecode_sv) I32
350 xhv_name HvNAME(bytecode_sv) pvcontents
351 hv_store bytecode_sv svindex x
352 sv_magic bytecode_sv char x
353 mg_obj SvMAGIC(bytecode_sv)->mg_obj svindex
354 mg_private SvMAGIC(bytecode_sv)->mg_private U16
355 mg_flags SvMAGIC(bytecode_sv)->mg_flags U8
356 mg_pv SvMAGIC(bytecode_sv) pvcontents x
357 xmg_stash *(SV**)&SvSTASH(bytecode_sv) svindex
358 gv_fetchpv bytecode_sv strconst x
359 gv_stashpv bytecode_sv strconst x
360 gp_sv GvSV(bytecode_sv) svindex
361 gp_refcnt GvREFCNT(bytecode_sv) U32
362 gp_refcnt_add GvREFCNT(bytecode_sv) I32 x
363 gp_av *(SV**)&GvAV(bytecode_sv) svindex
364 gp_hv *(SV**)&GvHV(bytecode_sv) svindex
365 gp_cv *(SV**)&GvCV(bytecode_sv) svindex
366 gp_file GvFILE(bytecode_sv) pvcontents
367 gp_io *(SV**)&GvIOp(bytecode_sv) svindex
368 gp_form *(SV**)&GvFORM(bytecode_sv) svindex
369 gp_cvgen GvCVGEN(bytecode_sv) U32
370 gp_line GvLINE(bytecode_sv) line_t
371 gp_share bytecode_sv svindex x
372 xgv_flags GvFLAGS(bytecode_sv) U8
373 op_next PL_op->op_next opindex
374 op_sibling PL_op->op_sibling opindex
375 op_ppaddr PL_op->op_ppaddr strconst x
376 op_targ PL_op->op_targ PADOFFSET
377 op_type PL_op OPCODE x
378 op_seq PL_op->op_seq U16
379 op_flags PL_op->op_flags U8
380 op_private PL_op->op_private U8
381 op_first cUNOP->op_first opindex
382 op_last cBINOP->op_last opindex
383 op_other cLOGOP->op_other opindex
384 op_children cLISTOP->op_children U32
385 op_pmreplroot cPMOP->op_pmreplroot opindex
386 op_pmreplrootgv *(SV**)&cPMOP->op_pmreplroot svindex
387 op_pmreplstart cPMOP->op_pmreplstart opindex
388 op_pmnext *(OP**)&cPMOP->op_pmnext opindex
389 pregcomp PL_op pvcontents x
390 op_pmflags cPMOP->op_pmflags U16
391 op_pmpermflags cPMOP->op_pmpermflags U16
392 op_sv cSVOP->op_sv svindex
393 op_padix cPADOP->op_padix PADOFFSET
394 op_pv cPVOP->op_pv pvcontents
395 op_pv_tr cPVOP->op_pv op_tr_array
396 op_redoop cLOOP->op_redoop opindex
397 op_nextop cLOOP->op_nextop opindex
398 op_lastop cLOOP->op_lastop opindex
399 cop_label cCOP->cop_label pvcontents
400 cop_stashpv cCOP pvcontents x
401 cop_file cCOP pvcontents x
402 cop_seq cCOP->cop_seq U32
403 cop_arybase cCOP->cop_arybase I32
404 cop_line cCOP line_t x
405 cop_warnings cCOP->cop_warnings svindex
406 main_start PL_main_start opindex
407 main_root PL_main_root opindex
408 curpad PL_curpad svindex x
409 push_begin PL_beginav svindex x
410 push_init PL_initav svindex x
411 push_end PL_endav svindex x