3 # Regenerate (overwriting only if changed):
9 # from information stored in regen/opcodes, plus the
10 # values hardcoded into this script in @raw_alias.
12 # Accepts the standard regen_lib -q and -v args.
14 # This script is normally invoked from regen.pl.
19 # Get function prototypes
20 require 'regen/regen_lib.pl';
23 my $oc = open_new('opcode.h', '>',
24 {by => 'regen/opcode.pl', from => 'its data',
25 file => 'opcode.h', style => '*',
26 copyright => [1993 .. 2007]});
28 my $on = open_new('opnames.h', '>',
29 { by => 'regen/opcode.pl', from => 'its data', style => '*',
30 file => 'opnames.h', copyright => [1999 .. 2008] });
35 my (@ops, %desc, %check, %ckname, %flags, %args, %opnum);
37 open OPS, 'regen/opcodes' or die $!;
43 my ($key, $desc, $check, $flags, $args) = split(/\t+/, $_, 5);
44 $args = '' unless defined $args;
46 warn qq[Description "$desc" duplicates $seen{$desc}\n]
47 if $seen{$desc} and $key ne "transr";
48 die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
49 die qq[Opcode "freed" is reserved for the slab allocator\n]
51 $seen{$desc} = qq[description of opcode "$key"];
52 $seen{$key} = qq[opcode "$key"];
57 $check{$key} = $check;
59 $flags{$key} = $flags;
67 # Format is "this function" => "does these op names"
69 Perl_do_kv => [qw( keys values )],
70 Perl_unimplemented_op => [qw(padany mapstart custom)],
71 # All the ops with a body of { return NORMAL; }
72 Perl_pp_null => [qw(scalar regcmaybe lineseq scope)],
74 Perl_pp_goto => ['dump'],
75 Perl_pp_require => ['dofile'],
76 Perl_pp_untie => ['dbmclose'],
77 Perl_pp_sysread => {read => '', recv => '#ifdef HAS_SOCKET'},
78 Perl_pp_sysseek => ['seek'],
79 Perl_pp_ioctl => ['fcntl'],
80 Perl_pp_ssockopt => {gsockopt => '#ifdef HAS_SOCKET'},
81 Perl_pp_getpeername => {getsockname => '#ifdef HAS_SOCKET'},
82 Perl_pp_stat => ['lstat'],
83 Perl_pp_ftrowned => [qw(fteowned ftzero ftsock ftchr ftblk
84 ftfile ftdir ftpipe ftsuid ftsgid
86 Perl_pp_fttext => ['ftbinary'],
87 Perl_pp_gmtime => ['localtime'],
88 Perl_pp_semget => [qw(shmget msgget)],
89 Perl_pp_semctl => [qw(shmctl msgctl)],
90 Perl_pp_ghostent => [qw(ghbyname ghbyaddr)],
91 Perl_pp_gnetent => [qw(gnbyname gnbyaddr)],
92 Perl_pp_gprotoent => [qw(gpbyname gpbynumber)],
93 Perl_pp_gservent => [qw(gsbyname gsbyport)],
94 Perl_pp_gpwent => [qw(gpwnam gpwuid)],
95 Perl_pp_ggrent => [qw(ggrnam ggrgid)],
96 Perl_pp_ftis => [qw(ftsize ftmtime ftatime ftctime)],
97 Perl_pp_chown => [qw(unlink chmod utime kill)],
98 Perl_pp_link => ['symlink'],
99 Perl_pp_ftrread => [qw(ftrwrite ftrexec fteread ftewrite
101 Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
102 Perl_pp_syswrite => {send => '#ifdef HAS_SOCKET'},
103 Perl_pp_defined => [qw(dor dorassign)],
104 Perl_pp_and => ['andassign'],
105 Perl_pp_or => ['orassign'],
106 Perl_pp_ucfirst => ['lcfirst'],
107 Perl_pp_sle => [qw(slt sgt sge)],
108 Perl_pp_print => ['say'],
109 Perl_pp_index => ['rindex'],
110 Perl_pp_oct => ['hex'],
111 Perl_pp_shift => ['pop'],
112 Perl_pp_sin => [qw(cos exp log sqrt)],
113 Perl_pp_bit_or => ['bit_xor'],
114 Perl_pp_rv2av => ['rv2hv'],
115 Perl_pp_akeys => ['avalues'],
116 Perl_pp_rkeys => [qw(rvalues reach)],
117 Perl_pp_trans => [qw(trans transr)],
118 Perl_pp_chop => [qw(chop chomp)],
119 Perl_pp_schop => [qw(schop schomp)],
120 Perl_pp_bind => {connect => '#ifdef HAS_SOCKET'},
121 Perl_pp_preinc => ['i_preinc', 'predec', 'i_predec'],
122 Perl_pp_postinc => ['i_postinc', 'postdec', 'i_postdec'],
123 Perl_pp_ehostent => [qw(enetent eprotoent eservent
124 spwent epwent sgrent egrent)],
125 Perl_pp_shostent => [qw(snetent sprotoent sservent)],
126 Perl_pp_aelemfast => ['aelemfast_lex'],
129 while (my ($func, $names) = splice @raw_alias, 0, 2) {
130 if (ref $names eq 'ARRAY') {
132 $alias{$_} = [$func, ''];
135 while (my ($opname, $cond) = each %$names) {
136 $alias{$opname} = [$func, $cond];
141 foreach my $sock_func (qw(socket bind listen accept shutdown
142 ssockopt getpeername)) {
143 $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
148 print $oc "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
155 if (@unimplemented) {
157 foreach (@unimplemented) {
158 print $oc "#define $_ Perl_unimplemented_op\n";
160 print $oc "#endif\n";
167 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
168 my $op_func = "Perl_pp_$_";
170 if ($cond ne $last_cond) {
171 # A change in condition. (including to or from no condition)
175 print $oc "$last_cond\n";
178 push @unimplemented, $op_func if $last_cond;
179 print $oc "#define $op_func $impl\n" if $impl ne $op_func;
181 # If the last op was conditional, we need to close it out:
185 print $on "typedef enum opcode {\n";
189 print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
191 print $on "\t", tab(3,"OP_max"), "\n";
192 print $on "} opcode;\n";
193 print $on "\n#define MAXO ", scalar @ops, "\n";
194 print $on "#define OP_FREED MAXO\n";
196 # Emit op names and descriptions.
202 EXTCONST char* const PL_op_name[];
204 EXTCONST char* const PL_op_name[] = {
208 print $oc qq(\t"$_",\n);
217 EXTCONST char* const PL_op_desc[];
219 EXTCONST char* const PL_op_desc[] = {
223 my($safe_desc) = $desc{$_};
225 # Have to escape double quotes and escape characters.
226 $safe_desc =~ s/([\\"])/\\$1/g;
228 print $oc qq(\t"$safe_desc",\n);
238 #endif /* !PERL_GLOBAL_STRUCT_INIT */
241 # Emit ppcode switch array.
247 #ifdef PERL_GLOBAL_STRUCT_INIT
248 # define PERL_PPADDR_INITED
249 static const Perl_ppaddr_t Gppaddr[]
251 # ifndef PERL_GLOBAL_STRUCT
252 # define PERL_PPADDR_INITED
253 EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
255 #endif /* PERL_GLOBAL_STRUCT */
256 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
257 # define PERL_PPADDR_INITED
262 my $op_func = "Perl_pp_$_";
263 my $name = $alias{$_};
264 if ($name && $name->[0] ne $op_func) {
265 print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
268 print $oc "\t$op_func,\n";
275 #ifdef PERL_PPADDR_INITED
279 #ifdef PERL_GLOBAL_STRUCT_INIT
280 # define PERL_CHECK_INITED
281 static const Perl_check_t Gcheck[]
283 # ifndef PERL_GLOBAL_STRUCT
284 # define PERL_CHECK_INITED
285 EXT Perl_check_t PL_check[] /* or perlvars.h */
288 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
289 # define PERL_CHECK_INITED
294 print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
300 #ifdef PERL_CHECK_INITED
302 #endif /* #ifdef PERL_CHECK_INITED */
304 #ifndef PERL_GLOBAL_STRUCT_INIT
307 EXTCONST U32 PL_opargs[];
309 EXTCONST U32 PL_opargs[] = {
312 # Emit allowed argument types.
319 'A', 3, # array value
323 'R', 7, # scalar reference
333 '$', 6, # svop_or_padop
335 '"', 8, # pvop_or_svop
338 '%', 11, # baseop_or_unop
339 '-', 12, # filestatop
344 'm' => 1, # needs stack mark
345 'f' => 2, # fold constants
346 's' => 4, # always produces scalar
347 't' => 8, # needs target scalar
348 'T' => 8 | 16, # ... which may be lexical
349 'i' => 0, # always produces integer (unused since e7311069)
350 'I' => 32, # has corresponding int op
351 'd' => 64, # danger, unknown side effects
352 'u' => 128, # defaults to $_
355 my %OP_IS_SOCKET; # /Fs/
356 my %OP_IS_FILETEST; # /F-/
357 my %OP_IS_FT_ACCESS; # /F-+/
358 my %OP_IS_NUMCOMPARE; # /S</
359 my %OP_IS_DIRHOP; # /Fd/
366 my $flags = $flags{$op};
367 for my $flag (keys %opflags) {
368 if ($flags =~ s/$flag//) {
369 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
370 if $argsum & $opflags{$flag};
371 $argsum |= $opflags{$flag};
374 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
375 unless exists $opclass{$flags};
376 $argsum |= $opclass{$flags} << $OCSHIFT;
377 my $argshift = $OASHIFT;
378 for my $arg (split(' ',$args{$op})) {
379 if ($arg =~ s/^D//) {
380 # handle 1st, just to put D 1st.
381 $OP_IS_DIRHOP{$op} = $opnum{$op};
384 # record opnums of these opnames
385 $OP_IS_SOCKET{$op} = $opnum{$op} if $arg =~ s/s//;
386 $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
387 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
389 elsif ($arg =~ /^S</) {
390 $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
392 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
393 die "op = $op, arg = $arg\n"
394 unless exists $argnum{$arg};
395 $argnum += $argnum{$arg};
396 die "Argument overflow for '$op'\n"
397 if $argshift >= $ARGBITS ||
398 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
399 $argsum += $argnum << $argshift;
402 $argsum = sprintf("0x%08x", $argsum);
403 print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
410 #endif /* !PERL_GLOBAL_STRUCT_INIT */
415 # Emit OP_IS_* macros
417 print $on <<'EO_OP_IS_COMMENT';
419 /* the OP_IS_* macros are optimized to a simple range check because
420 all the member OPs are contiguous in regen/opcodes table.
421 opcode.pl verifies the range contiguity, or generates an OR-equals
425 gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
426 gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
427 gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
428 gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
429 gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
431 sub gen_op_is_macro {
432 my ($op_is, $macname) = @_;
435 # get opnames whose numbers are lowest and highest
436 my ($first, @rest) = sort {
437 $op_is->{$a} <=> $op_is->{$b}
440 my $last = pop @rest; # @rest slurped, get its last
441 die "Invalid range of ops: $first .. $last\n" unless $last;
443 print $on "\n#define $macname(op) \\\n\t(";
445 # verify that op-ct matches 1st..last range (and fencepost)
446 # (we know there are no dups)
447 if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
449 # contiguous ops -> optimized version
450 print $on "(op) >= OP_" . uc($first)
451 . " && (op) <= OP_" . uc($last);
454 print $on join(" || \\\n\t ",
455 map { "(op) == OP_" . uc() } sort keys %$op_is);
461 my $pp = open_new('pp_proto.h', '>',
462 { by => 'opcode.pl', from => 'its data' });
467 my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
470 print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
472 foreach ($oc, $on, $pp) {
473 read_only_bottom_close_and_rename($_);
476 # Some comments about 'T' opcode classifier:
478 # Safe to set if the ppcode uses:
479 # tryAMAGICbin, tryAMAGICun, SETn, SETi, SETu, PUSHn, PUSHTARG, SETTARG,
480 # SETs(TARG), XPUSHn, XPUSHu,
482 # Unsafe to set if the ppcode uses dTARG or [X]RETPUSH[YES|NO|UNDEF]
484 # lt and friends do SETs (including ncmp, but not scmp)
486 # Additional mode of failure: the opcode can modify TARG before it "used"
487 # all the arguments (or may call an external function which does the same).
488 # If the target coincides with one of the arguments ==> kaboom.
490 # pp.c pos substr each not OK (RETPUSHUNDEF)
491 # substr vec also not OK due to LV to target (are they???)
492 # ref not OK (RETPUSHNO)
493 # trans not OK (dTARG; TARG = sv_newmortal();)
494 # ucfirst etc not OK: TMP arg processed inplace
495 # quotemeta not OK (unsafe when TARG == arg)
496 # each repeat not OK too due to list context
497 # pack split - unknown whether they are safe
498 # sprintf: is calling do_sprintf(TARG,...) which can act on TARG
499 # before other args are processed.
501 # Suspicious wrt "additional mode of failure" (and only it):
502 # schop, chop, postinc/dec, bit_and etc, negate, complement.
504 # Also suspicious: 4-arg substr, sprintf, uc/lc (POK_only), reverse, pack.
506 # substr/vec: doing TAINT_off()???
509 # readline - unknown whether it is safe
510 # match subst not OK (dTARG)
511 # grepwhile not OK (not always setting)
512 # join not OK (unsafe when TARG == arg)
514 # Suspicious wrt "additional mode of failure": concat (dealt with
515 # in ck_sassign()), join (same).
518 # mapwhile flip caller not OK (not always setting)
521 # backtick glob warn die not OK (not always setting)
522 # warn not OK (RETPUSHYES)
523 # open fileno getc sysread syswrite ioctl accept shutdown
524 # ftsize(etc) readlink telldir fork alarm getlogin not OK (RETPUSHUNDEF)
525 # umask select not OK (XPUSHs(&PL_sv_undef);)
526 # fileno getc sysread syswrite tell not OK (meth("FILENO" "GETC"))
527 # sselect shm* sem* msg* syscall - unknown whether they are safe
528 # gmtime not OK (list context)
530 # Suspicious wrt "additional mode of failure": warn, die, select.