This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move the duplicated subroutine tab() into regen_lib.pl
[perl5.git] / regen / opcode.pl
CommitLineData
d6480c9d 1#!/usr/bin/perl -w
6294c161
DM
2#
3# Regenerate (overwriting only if changed):
4#
5# opcode.h
6# opnames.h
897d3989 7# pp_proto.h
6294c161 8#
f8a58b02 9# from information stored in regen/opcodes, plus the
6294c161
DM
10# values hardcoded into this script in @raw_alias.
11#
12# Accepts the standard regen_lib -q and -v args.
13#
14# This script is normally invoked from regen.pl.
15
d6480c9d
NC
16use strict;
17
36bb303b
NC
18BEGIN {
19 # Get function prototypes
af001346 20 require 'regen/regen_lib.pl';
36bb303b 21}
79072805 22
f038801a
NC
23my $oc = safer_open('opcode.h-new', 'opcode.h');
24my $on = safer_open('opnames.h-new', 'opnames.h');
424a4936 25select $oc;
79072805
LW
26
27# Read data.
28
d6480c9d 29my %seen;
e71197e2 30my (@ops, %desc, %check, %ckname, %flags, %args, %opnum);
d6480c9d 31
f8a58b02
NC
32open OPS, 'regen/opcodes' or die $!;
33
34while (<OPS>) {
79072805
LW
35 chop;
36 next unless $_;
37 next if /^#/;
d6480c9d
NC
38 my ($key, $desc, $check, $flags, $args) = split(/\t+/, $_, 5);
39 $args = '' unless defined $args;
c07a80fd 40
6342d5c5
FC
41 warn qq[Description "$desc" duplicates $seen{$desc}\n]
42 if $seen{$desc} and $key ne "transr";
c07a80fd
PP
43 die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
44 $seen{$desc} = qq[description of opcode "$key"];
45 $seen{$key} = qq[opcode "$key"];
46
79072805 47 push(@ops, $key);
e71197e2 48 $opnum{$key} = $#ops;
c07a80fd 49 $desc{$key} = $desc;
79072805
LW
50 $check{$key} = $check;
51 $ckname{$check}++;
52 $flags{$key} = $flags;
53 $args{$key} = $args;
54}
55
1d5774de
NC
56# Set up aliases
57
58my %alias;
59
60# Format is "this function" => "does these op names"
61my @raw_alias = (
6faeeb49 62 Perl_do_kv => [qw( keys values )],
d83386fa 63 Perl_unimplemented_op => [qw(padany mapstart custom)],
0b612f93
NC
64 # All the ops with a body of { return NORMAL; }
65 Perl_pp_null => [qw(scalar regcmaybe lineseq scope)],
66
67 Perl_pp_goto => ['dump'],
68 Perl_pp_require => ['dofile'],
69 Perl_pp_untie => ['dbmclose'],
7627e6d0 70 Perl_pp_sysread => {read => '', recv => '#ifdef HAS_SOCKET'},
0b612f93
NC
71 Perl_pp_sysseek => ['seek'],
72 Perl_pp_ioctl => ['fcntl'],
7627e6d0
NC
73 Perl_pp_ssockopt => {gsockopt => '#ifdef HAS_SOCKET'},
74 Perl_pp_getpeername => {getsockname => '#ifdef HAS_SOCKET'},
0b612f93 75 Perl_pp_stat => ['lstat'],
f1cb2d48 76 Perl_pp_ftrowned => [qw(fteowned ftzero ftsock ftchr ftblk
17ad201a
NC
77 ftfile ftdir ftpipe ftsuid ftsgid
78 ftsvtx)],
0b612f93
NC
79 Perl_pp_fttext => ['ftbinary'],
80 Perl_pp_gmtime => ['localtime'],
81 Perl_pp_semget => [qw(shmget msgget)],
82 Perl_pp_semctl => [qw(shmctl msgctl)],
0b612f93
NC
83 Perl_pp_ghostent => [qw(ghbyname ghbyaddr)],
84 Perl_pp_gnetent => [qw(gnbyname gnbyaddr)],
85 Perl_pp_gprotoent => [qw(gpbyname gpbynumber)],
86 Perl_pp_gservent => [qw(gsbyname gsbyport)],
87 Perl_pp_gpwent => [qw(gpwnam gpwuid)],
88 Perl_pp_ggrent => [qw(ggrnam ggrgid)],
957b0e1d 89 Perl_pp_ftis => [qw(ftsize ftmtime ftatime ftctime)],
605b9385 90 Perl_pp_chown => [qw(unlink chmod utime kill)],
ce6987d0 91 Perl_pp_link => ['symlink'],
af9e49b4
NC
92 Perl_pp_ftrread => [qw(ftrwrite ftrexec fteread ftewrite
93 fteexec)],
ca563b4e 94 Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
7627e6d0 95 Perl_pp_syswrite => {send => '#ifdef HAS_SOCKET'},
c960fc3b 96 Perl_pp_defined => [qw(dor dorassign)],
62726f23
SP
97 Perl_pp_and => ['andassign'],
98 Perl_pp_or => ['orassign'],
12e9c124 99 Perl_pp_ucfirst => ['lcfirst'],
afd9910b 100 Perl_pp_sle => [qw(slt sgt sge)],
0d863452 101 Perl_pp_print => ['say'],
2723d216 102 Perl_pp_index => ['rindex'],
daa2adfd 103 Perl_pp_oct => ['hex'],
789b4bc9 104 Perl_pp_shift => ['pop'],
71302fe3 105 Perl_pp_sin => [qw(cos exp log sqrt)],
3658c1f1 106 Perl_pp_bit_or => ['bit_xor'],
17ab7946 107 Perl_pp_rv2av => ['rv2hv'],
878d132a 108 Perl_pp_akeys => ['avalues'],
cba5a3b0 109 Perl_pp_rkeys => [qw(rvalues reach)],
7627e6d0
NC
110 Perl_pp_trans => [qw(trans transr)],
111 Perl_pp_chop => [qw(chop chomp)],
112 Perl_pp_schop => [qw(schop schomp)],
113 Perl_pp_bind => {connect => '#ifdef HAS_SOCKET'},
9561d06f
NC
114 Perl_pp_preinc => ['i_preinc'],
115 Perl_pp_predec => ['i_predec'],
116 Perl_pp_postinc => ['i_postinc'],
117 Perl_pp_postdec => ['i_postdec'],
720d5dbf
NC
118 Perl_pp_ehostent => [qw(enetent eprotoent eservent
119 spwent epwent sgrent egrent)],
396166e1 120 Perl_pp_shostent => [qw(snetent sprotoent sservent)],
605b9385 121 );
1d5774de
NC
122
123while (my ($func, $names) = splice @raw_alias, 0, 2) {
7627e6d0
NC
124 if (ref $names eq 'ARRAY') {
125 foreach (@$names) {
126 $alias{$_} = [$func, ''];
127 }
128 } else {
129 while (my ($opname, $cond) = each %$names) {
130 $alias{$opname} = [$func, $cond];
131 }
916e4025 132 }
1d5774de
NC
133}
134
7627e6d0
NC
135foreach my $sock_func (qw(socket bind listen accept shutdown
136 ssockopt getpeername)) {
137 $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
138}
139
79072805
LW
140# Emit defines.
141
78102347
NC
142print read_only_top(lang => 'C', by => 'regen/opcode.pl', from => 'its data',
143 file => 'opcode.h', style => '*',
144 copyright => [1993 .. 2007]),
145 "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
9561d06f 146
7627e6d0
NC
147{
148 my $last_cond = '';
149 my @unimplemented;
150
151 sub unimplemented {
152 if (@unimplemented) {
153 print "#else\n";
154 foreach (@unimplemented) {
155 print "#define $_ Perl_unimplemented_op\n";
156 }
157 print "#endif\n";
158 @unimplemented = ();
159 }
160
161 }
162
163 for (@ops) {
164 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
165 my $op_func = "Perl_pp_$_";
166
167 if ($cond ne $last_cond) {
168 # A change in condition. (including to or from no condition)
169 unimplemented();
170 $last_cond = $cond;
171 if ($last_cond) {
172 print "$last_cond\n";
173 }
174 }
175 push @unimplemented, $op_func if $last_cond;
176 print "#define $op_func $impl\n" if $impl ne $op_func;
177 }
178 # If the last op was conditional, we need to close it out:
179 unimplemented();
9561d06f
NC
180}
181
78102347
NC
182print $on read_only_top(lang => 'C', by => 'regen/opcode.pl',
183 from => 'its data', style => '*',
184 file => 'opnames.h', copyright => [1999 .. 2008]),
185 "typedef enum opcode {\n";
abdd5c84 186
d6480c9d 187my $i = 0;
79072805 188for (@ops) {
424a4936 189 print $on "\t", &tab(3,"OP_\U$_"), " = ", $i++, ",\n";
79072805 190}
424a4936
NC
191print $on "\t", &tab(3,"OP_max"), "\n";
192print $on "} opcode;\n";
193print $on "\n#define MAXO ", scalar @ops, "\n";
79072805 194
c07a80fd 195# Emit op names and descriptions.
79072805
LW
196
197print <<END;
73c4f7a1
GS
198START_EXTERN_C
199
79072805 200#ifndef DOINIT
27da23d5 201EXTCONST char* const PL_op_name[];
79072805 202#else
27da23d5 203EXTCONST char* const PL_op_name[] = {
79072805
LW
204END
205
206for (@ops) {
c07a80fd
PP
207 print qq(\t"$_",\n);
208}
209
210print <<END;
211};
212#endif
213
214END
215
216print <<END;
217#ifndef DOINIT
27da23d5 218EXTCONST char* const PL_op_desc[];
c07a80fd 219#else
27da23d5 220EXTCONST char* const PL_op_desc[] = {
c07a80fd
PP
221END
222
223for (@ops) {
42d38218
MS
224 my($safe_desc) = $desc{$_};
225
a567e93b 226 # Have to escape double quotes and escape characters.
b0c6325e 227 $safe_desc =~ s/([\\"])/\\$1/g;
42d38218
MS
228
229 print qq(\t"$safe_desc",\n);
79072805
LW
230}
231
232print <<END;
233};
234#endif
235
73c4f7a1
GS
236END_EXTERN_C
237
27da23d5 238#endif /* !PERL_GLOBAL_STRUCT_INIT */
22c35a8c 239END
79072805 240
79072805
LW
241# Emit ppcode switch array.
242
243print <<END;
244
73c4f7a1
GS
245START_EXTERN_C
246
27da23d5 247#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 248# define PERL_PPADDR_INITED
27da23d5 249static const Perl_ppaddr_t Gppaddr[]
79072805 250#else
27da23d5 251# ifndef PERL_GLOBAL_STRUCT
97aff369 252# define PERL_PPADDR_INITED
27da23d5
JH
253EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
254# endif
255#endif /* PERL_GLOBAL_STRUCT */
256#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 257# define PERL_PPADDR_INITED
27da23d5 258= {
79072805
LW
259END
260
261for (@ops) {
7627e6d0
NC
262 my $op_func = "Perl_pp_$_";
263 my $name = $alias{$_};
264 if ($name && $name->[0] ne $op_func) {
265 print "\t$op_func,\t/* implemented by $name->[0] */\n";
6faeeb49
MB
266 }
267 else {
7627e6d0 268 print "\t$op_func,\n";
6faeeb49 269 }
79072805
LW
270}
271
272print <<END;
27da23d5 273}
79072805 274#endif
97aff369 275#ifdef PERL_PPADDR_INITED
27da23d5 276;
97aff369 277#endif
79072805
LW
278
279END
280
281# Emit check routines.
282
283print <<END;
27da23d5 284#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 285# define PERL_CHECK_INITED
27da23d5 286static const Perl_check_t Gcheck[]
79072805 287#else
27da23d5 288# ifndef PERL_GLOBAL_STRUCT
97aff369 289# define PERL_CHECK_INITED
27da23d5
JH
290EXT Perl_check_t PL_check[] /* or perlvars.h */
291# endif
292#endif
293#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 294# define PERL_CHECK_INITED
27da23d5 295= {
79072805
LW
296END
297
298for (@ops) {
ef69c8fc 299 print "\t", &tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
79072805
LW
300}
301
302print <<END;
27da23d5 303}
79072805 304#endif
97aff369 305#ifdef PERL_CHECK_INITED
27da23d5 306;
97aff369 307#endif /* #ifdef PERL_CHECK_INITED */
79072805
LW
308
309END
310
311# Emit allowed argument types.
312
c2dedb93
MHM
313my $ARGBITS = 32;
314
79072805 315print <<END;
27da23d5
JH
316#ifndef PERL_GLOBAL_STRUCT_INIT
317
79072805 318#ifndef DOINIT
1ccb7c8d 319EXTCONST U32 PL_opargs[];
79072805 320#else
1ccb7c8d 321EXTCONST U32 PL_opargs[] = {
79072805
LW
322END
323
d6480c9d
NC
324my %argnum = (
325 'S', 1, # scalar
326 'L', 2, # list
327 'A', 3, # array value
328 'H', 4, # hash value
329 'C', 5, # code value
330 'F', 6, # file value
331 'R', 7, # scalar reference
79072805
LW
332);
333
d6480c9d 334my %opclass = (
db173bac
MB
335 '0', 0, # baseop
336 '1', 1, # unop
337 '2', 2, # binop
338 '|', 3, # logop
1a67a97c
SM
339 '@', 4, # listop
340 '/', 5, # pmop
350de78d 341 '$', 6, # svop_or_padop
7934575e 342 '#', 7, # padop
1a67a97c
SM
343 '"', 8, # pvop_or_svop
344 '{', 9, # loop
345 ';', 10, # cop
346 '%', 11, # baseop_or_unop
347 '-', 12, # filestatop
348 '}', 13, # loopexop
db173bac
MB
349);
350
c2dedb93
MHM
351my %opflags = (
352 'm' => 1, # needs stack mark
353 'f' => 2, # fold constants
354 's' => 4, # always produces scalar
355 't' => 8, # needs target scalar
903fd87c
NC
356 'T' => 8 | 16, # ... which may be lexical
357 'i' => 0, # always produces integer (unused since e7311069)
c2dedb93
MHM
358 'I' => 32, # has corresponding int op
359 'd' => 64, # danger, unknown side effects
360 'u' => 128, # defaults to $_
361);
362
a85d93d9
JH
363my %OP_IS_SOCKET;
364my %OP_IS_FILETEST;
6ecf81d6 365my %OP_IS_FT_ACCESS;
903fd87c
NC
366my $OCSHIFT = 8;
367my $OASHIFT = 12;
a85d93d9 368
c2dedb93 369for my $op (@ops) {
d6480c9d 370 my $argsum = 0;
c2dedb93
MHM
371 my $flags = $flags{$op};
372 for my $flag (keys %opflags) {
373 if ($flags =~ s/$flag//) {
cb7b5e07 374 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
c2dedb93
MHM
375 if $argsum & $opflags{$flag};
376 $argsum |= $opflags{$flag};
377 }
378 }
cb7b5e07 379 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
c2dedb93
MHM
380 unless exists $opclass{$flags};
381 $argsum |= $opclass{$flags} << $OCSHIFT;
382 my $argshift = $OASHIFT;
383 for my $arg (split(' ',$args{$op})) {
a85d93d9 384 if ($arg =~ /^F/) {
e71197e2
JC
385 # record opnums of these opnames
386 $OP_IS_SOCKET{$op} = $opnum{$op} if $arg =~ s/s//;
387 $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
6ecf81d6 388 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
a85d93d9 389 }
d6480c9d 390 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
c2dedb93
MHM
391 die "op = $op, arg = $arg\n"
392 unless exists $argnum{$arg};
79072805 393 $argnum += $argnum{$arg};
c2dedb93
MHM
394 die "Argument overflow for '$op'\n"
395 if $argshift >= $ARGBITS ||
396 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
397 $argsum += $argnum << $argshift;
398 $argshift += 4;
79072805
LW
399 }
400 $argsum = sprintf("0x%08x", $argsum);
c2dedb93 401 print "\t", &tab(3, "$argsum,"), "/* $op */\n";
79072805
LW
402}
403
404print <<END;
405};
406#endif
73c4f7a1 407
bae1192d
JH
408#endif /* !PERL_GLOBAL_STRUCT_INIT */
409
73c4f7a1 410END_EXTERN_C
79072805
LW
411END
412
e71197e2
JC
413# Emit OP_IS_* macros
414
424a4936 415print $on <<EO_OP_IS_COMMENT;
e71197e2
JC
416
417/* the OP_IS_(SOCKET|FILETEST) macros are optimized to a simple range
418 check because all the member OPs are contiguous in opcode.pl
f8a58b02 419 <OPS> table. opcode.pl verifies the range contiguity. */
e71197e2
JC
420EO_OP_IS_COMMENT
421
422gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
423gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
6ecf81d6 424gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
e71197e2
JC
425
426sub gen_op_is_macro {
427 my ($op_is, $macname) = @_;
428 if (keys %$op_is) {
429
430 # get opnames whose numbers are lowest and highest
431 my ($first, @rest) = sort {
432 $op_is->{$a} <=> $op_is->{$b}
433 } keys %$op_is;
434
435 my $last = pop @rest; # @rest slurped, get its last
cb7b5e07 436 die "Invalid range of ops: $first .. $last\n" unless $last;
6ecf81d6 437
ce716c52 438 print $on "\n#define $macname(op) \\\n\t(";
6ecf81d6 439
e71197e2
JC
440 # verify that op-ct matches 1st..last range (and fencepost)
441 # (we know there are no dups)
442 if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
443
444 # contiguous ops -> optimized version
424a4936 445 print $on "(op) >= OP_" . uc($first) . " && (op) <= OP_" . uc($last);
ce716c52 446 print $on ")\n";
e71197e2
JC
447 }
448 else {
424a4936 449 print $on join(" || \\\n\t ",
6ecf81d6 450 map { "(op) == OP_" . uc() } sort keys %$op_is);
ce716c52 451 print $on ")\n";
e71197e2
JC
452 }
453 }
a85d93d9
JH
454}
455
f038801a 456my $pp = safer_open('pp_proto.h-new', 'pp_proto.h');
a27f85b3 457
78102347 458print $pp read_only_top(lang => 'C', by => 'opcode.pl', from => 'its data');
a27f85b3 459
981b7185
NC
460{
461 my %funcs;
462 for (@ops) {
7627e6d0 463 my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
981b7185
NC
464 ++$funcs{$name};
465 }
466 print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
735e0d5c 467}
ce716c52
NC
468foreach ($oc, $on, $pp) {
469 read_only_bottom_close_and_rename($_);
470}
b162f9ea
IZ
471
472# Some comments about 'T' opcode classifier:
473
474# Safe to set if the ppcode uses:
475# tryAMAGICbin, tryAMAGICun, SETn, SETi, SETu, PUSHn, PUSHTARG, SETTARG,
476# SETs(TARG), XPUSHn, XPUSHu,
477
478# Unsafe to set if the ppcode uses dTARG or [X]RETPUSH[YES|NO|UNDEF]
479
480# lt and friends do SETs (including ncmp, but not scmp)
481
21f5b33c
GS
482# Additional mode of failure: the opcode can modify TARG before it "used"
483# all the arguments (or may call an external function which does the same).
484# If the target coincides with one of the arguments ==> kaboom.
485
b162f9ea
IZ
486# pp.c pos substr each not OK (RETPUSHUNDEF)
487# substr vec also not OK due to LV to target (are they???)
488# ref not OK (RETPUSHNO)
489# trans not OK (dTARG; TARG = sv_newmortal();)
490# ucfirst etc not OK: TMP arg processed inplace
69b47968 491# quotemeta not OK (unsafe when TARG == arg)
91e74348 492# each repeat not OK too due to list context
b162f9ea 493# pack split - unknown whether they are safe
dae78bb1
IZ
494# sprintf: is calling do_sprintf(TARG,...) which can act on TARG
495# before other args are processed.
b162f9ea 496
21f5b33c
GS
497# Suspicious wrt "additional mode of failure" (and only it):
498# schop, chop, postinc/dec, bit_and etc, negate, complement.
499
500# Also suspicious: 4-arg substr, sprintf, uc/lc (POK_only), reverse, pack.
501
502# substr/vec: doing TAINT_off()???
503
b162f9ea
IZ
504# pp_hot.c
505# readline - unknown whether it is safe
506# match subst not OK (dTARG)
507# grepwhile not OK (not always setting)
69b47968 508# join not OK (unsafe when TARG == arg)
b162f9ea 509
21f5b33c
GS
510# Suspicious wrt "additional mode of failure": concat (dealt with
511# in ck_sassign()), join (same).
512
b162f9ea
IZ
513# pp_ctl.c
514# mapwhile flip caller not OK (not always setting)
515
516# pp_sys.c
517# backtick glob warn die not OK (not always setting)
518# warn not OK (RETPUSHYES)
519# open fileno getc sysread syswrite ioctl accept shutdown
520# ftsize(etc) readlink telldir fork alarm getlogin not OK (RETPUSHUNDEF)
521# umask select not OK (XPUSHs(&PL_sv_undef);)
522# fileno getc sysread syswrite tell not OK (meth("FILENO" "GETC"))
523# sselect shm* sem* msg* syscall - unknown whether they are safe
524# gmtime not OK (list context)
525
21f5b33c 526# Suspicious wrt "additional mode of failure": warn, die, select.