This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge preinc and postinc
[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
cc49830d
NC
23my $oc = open_new('opcode.h', '>',
24 {by => 'regen/opcode.pl', from => 'its data',
25 file => 'opcode.h', style => '*',
26 copyright => [1993 .. 2007]});
27
28my $on = open_new('opnames.h', '>',
29 { by => 'regen/opcode.pl', from => 'its data', style => '*',
30 file => 'opnames.h', copyright => [1999 .. 2008] });
79072805
LW
31
32# Read data.
33
d6480c9d 34my %seen;
e71197e2 35my (@ops, %desc, %check, %ckname, %flags, %args, %opnum);
d6480c9d 36
f8a58b02
NC
37open OPS, 'regen/opcodes' or die $!;
38
39while (<OPS>) {
79072805
LW
40 chop;
41 next unless $_;
42 next if /^#/;
d6480c9d
NC
43 my ($key, $desc, $check, $flags, $args) = split(/\t+/, $_, 5);
44 $args = '' unless defined $args;
c07a80fd 45
6342d5c5
FC
46 warn qq[Description "$desc" duplicates $seen{$desc}\n]
47 if $seen{$desc} and $key ne "transr";
c07a80fd 48 die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
49 $seen{$desc} = qq[description of opcode "$key"];
50 $seen{$key} = qq[opcode "$key"];
51
79072805 52 push(@ops, $key);
e71197e2 53 $opnum{$key} = $#ops;
c07a80fd 54 $desc{$key} = $desc;
79072805
LW
55 $check{$key} = $check;
56 $ckname{$check}++;
57 $flags{$key} = $flags;
58 $args{$key} = $args;
59}
60
1d5774de
NC
61# Set up aliases
62
63my %alias;
64
65# Format is "this function" => "does these op names"
66my @raw_alias = (
6faeeb49 67 Perl_do_kv => [qw( keys values )],
d83386fa 68 Perl_unimplemented_op => [qw(padany mapstart custom)],
0b612f93
NC
69 # All the ops with a body of { return NORMAL; }
70 Perl_pp_null => [qw(scalar regcmaybe lineseq scope)],
71
72 Perl_pp_goto => ['dump'],
73 Perl_pp_require => ['dofile'],
74 Perl_pp_untie => ['dbmclose'],
7627e6d0 75 Perl_pp_sysread => {read => '', recv => '#ifdef HAS_SOCKET'},
0b612f93
NC
76 Perl_pp_sysseek => ['seek'],
77 Perl_pp_ioctl => ['fcntl'],
7627e6d0
NC
78 Perl_pp_ssockopt => {gsockopt => '#ifdef HAS_SOCKET'},
79 Perl_pp_getpeername => {getsockname => '#ifdef HAS_SOCKET'},
0b612f93 80 Perl_pp_stat => ['lstat'],
f1cb2d48 81 Perl_pp_ftrowned => [qw(fteowned ftzero ftsock ftchr ftblk
17ad201a
NC
82 ftfile ftdir ftpipe ftsuid ftsgid
83 ftsvtx)],
0b612f93
NC
84 Perl_pp_fttext => ['ftbinary'],
85 Perl_pp_gmtime => ['localtime'],
86 Perl_pp_semget => [qw(shmget msgget)],
87 Perl_pp_semctl => [qw(shmctl msgctl)],
0b612f93
NC
88 Perl_pp_ghostent => [qw(ghbyname ghbyaddr)],
89 Perl_pp_gnetent => [qw(gnbyname gnbyaddr)],
90 Perl_pp_gprotoent => [qw(gpbyname gpbynumber)],
91 Perl_pp_gservent => [qw(gsbyname gsbyport)],
92 Perl_pp_gpwent => [qw(gpwnam gpwuid)],
93 Perl_pp_ggrent => [qw(ggrnam ggrgid)],
957b0e1d 94 Perl_pp_ftis => [qw(ftsize ftmtime ftatime ftctime)],
605b9385 95 Perl_pp_chown => [qw(unlink chmod utime kill)],
ce6987d0 96 Perl_pp_link => ['symlink'],
af9e49b4
NC
97 Perl_pp_ftrread => [qw(ftrwrite ftrexec fteread ftewrite
98 fteexec)],
ca563b4e 99 Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
7627e6d0 100 Perl_pp_syswrite => {send => '#ifdef HAS_SOCKET'},
c960fc3b 101 Perl_pp_defined => [qw(dor dorassign)],
62726f23
SP
102 Perl_pp_and => ['andassign'],
103 Perl_pp_or => ['orassign'],
12e9c124 104 Perl_pp_ucfirst => ['lcfirst'],
afd9910b 105 Perl_pp_sle => [qw(slt sgt sge)],
0d863452 106 Perl_pp_print => ['say'],
2723d216 107 Perl_pp_index => ['rindex'],
daa2adfd 108 Perl_pp_oct => ['hex'],
789b4bc9 109 Perl_pp_shift => ['pop'],
71302fe3 110 Perl_pp_sin => [qw(cos exp log sqrt)],
3658c1f1 111 Perl_pp_bit_or => ['bit_xor'],
17ab7946 112 Perl_pp_rv2av => ['rv2hv'],
878d132a 113 Perl_pp_akeys => ['avalues'],
cba5a3b0 114 Perl_pp_rkeys => [qw(rvalues reach)],
7627e6d0
NC
115 Perl_pp_trans => [qw(trans transr)],
116 Perl_pp_chop => [qw(chop chomp)],
117 Perl_pp_schop => [qw(schop schomp)],
118 Perl_pp_bind => {connect => '#ifdef HAS_SOCKET'},
17058fe0 119 Perl_pp_preinc => ['i_preinc', 'predec', 'i_predec'],
9561d06f
NC
120 Perl_pp_postinc => ['i_postinc'],
121 Perl_pp_postdec => ['i_postdec'],
720d5dbf
NC
122 Perl_pp_ehostent => [qw(enetent eprotoent eservent
123 spwent epwent sgrent egrent)],
396166e1 124 Perl_pp_shostent => [qw(snetent sprotoent sservent)],
93bad3fd 125 Perl_pp_aelemfast => ['aelemfast_lex'],
605b9385 126 );
1d5774de
NC
127
128while (my ($func, $names) = splice @raw_alias, 0, 2) {
7627e6d0
NC
129 if (ref $names eq 'ARRAY') {
130 foreach (@$names) {
131 $alias{$_} = [$func, ''];
132 }
133 } else {
134 while (my ($opname, $cond) = each %$names) {
135 $alias{$opname} = [$func, $cond];
136 }
916e4025 137 }
1d5774de
NC
138}
139
7627e6d0
NC
140foreach my $sock_func (qw(socket bind listen accept shutdown
141 ssockopt getpeername)) {
142 $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
143}
144
79072805
LW
145# Emit defines.
146
cc49830d 147print $oc "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
9561d06f 148
7627e6d0
NC
149{
150 my $last_cond = '';
151 my @unimplemented;
152
153 sub unimplemented {
154 if (@unimplemented) {
2d6469fe 155 print $oc "#else\n";
7627e6d0 156 foreach (@unimplemented) {
2d6469fe 157 print $oc "#define $_ Perl_unimplemented_op\n";
7627e6d0 158 }
2d6469fe 159 print $oc "#endif\n";
7627e6d0
NC
160 @unimplemented = ();
161 }
162
163 }
164
165 for (@ops) {
166 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
167 my $op_func = "Perl_pp_$_";
168
169 if ($cond ne $last_cond) {
170 # A change in condition. (including to or from no condition)
171 unimplemented();
172 $last_cond = $cond;
173 if ($last_cond) {
2d6469fe 174 print $oc "$last_cond\n";
7627e6d0
NC
175 }
176 }
177 push @unimplemented, $op_func if $last_cond;
2d6469fe 178 print $oc "#define $op_func $impl\n" if $impl ne $op_func;
7627e6d0
NC
179 }
180 # If the last op was conditional, we need to close it out:
181 unimplemented();
9561d06f
NC
182}
183
cc49830d 184print $on "typedef enum opcode {\n";
abdd5c84 185
d6480c9d 186my $i = 0;
79072805 187for (@ops) {
2d6469fe 188 print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
79072805 189}
2d6469fe 190print $on "\t", tab(3,"OP_max"), "\n";
424a4936
NC
191print $on "} opcode;\n";
192print $on "\n#define MAXO ", scalar @ops, "\n";
79072805 193
c07a80fd 194# Emit op names and descriptions.
79072805 195
2d6469fe 196print $oc <<'END';
73c4f7a1
GS
197START_EXTERN_C
198
79072805 199#ifndef DOINIT
27da23d5 200EXTCONST char* const PL_op_name[];
79072805 201#else
27da23d5 202EXTCONST char* const PL_op_name[] = {
79072805
LW
203END
204
205for (@ops) {
2d6469fe 206 print $oc qq(\t"$_",\n);
c07a80fd 207}
208
2d6469fe 209print $oc <<'END';
c07a80fd 210};
211#endif
212
c07a80fd 213#ifndef DOINIT
27da23d5 214EXTCONST char* const PL_op_desc[];
c07a80fd 215#else
27da23d5 216EXTCONST char* const PL_op_desc[] = {
c07a80fd 217END
218
219for (@ops) {
42d38218
MS
220 my($safe_desc) = $desc{$_};
221
a567e93b 222 # Have to escape double quotes and escape characters.
b0c6325e 223 $safe_desc =~ s/([\\"])/\\$1/g;
42d38218 224
2d6469fe 225 print $oc qq(\t"$safe_desc",\n);
79072805
LW
226}
227
2d6469fe 228print $oc <<'END';
79072805
LW
229};
230#endif
231
73c4f7a1
GS
232END_EXTERN_C
233
27da23d5 234#endif /* !PERL_GLOBAL_STRUCT_INIT */
22c35a8c 235END
79072805 236
79072805
LW
237# Emit ppcode switch array.
238
2d6469fe 239print $oc <<'END';
79072805 240
73c4f7a1
GS
241START_EXTERN_C
242
27da23d5 243#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 244# define PERL_PPADDR_INITED
27da23d5 245static const Perl_ppaddr_t Gppaddr[]
79072805 246#else
27da23d5 247# ifndef PERL_GLOBAL_STRUCT
97aff369 248# define PERL_PPADDR_INITED
27da23d5
JH
249EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
250# endif
251#endif /* PERL_GLOBAL_STRUCT */
252#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 253# define PERL_PPADDR_INITED
27da23d5 254= {
79072805
LW
255END
256
257for (@ops) {
7627e6d0
NC
258 my $op_func = "Perl_pp_$_";
259 my $name = $alias{$_};
260 if ($name && $name->[0] ne $op_func) {
2d6469fe 261 print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
6faeeb49
MB
262 }
263 else {
2d6469fe 264 print $oc "\t$op_func,\n";
6faeeb49 265 }
79072805
LW
266}
267
2d6469fe 268print $oc <<'END';
27da23d5 269}
79072805 270#endif
97aff369 271#ifdef PERL_PPADDR_INITED
27da23d5 272;
97aff369 273#endif
79072805 274
27da23d5 275#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 276# define PERL_CHECK_INITED
27da23d5 277static const Perl_check_t Gcheck[]
79072805 278#else
27da23d5 279# ifndef PERL_GLOBAL_STRUCT
97aff369 280# define PERL_CHECK_INITED
27da23d5
JH
281EXT Perl_check_t PL_check[] /* or perlvars.h */
282# endif
283#endif
284#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 285# define PERL_CHECK_INITED
27da23d5 286= {
79072805
LW
287END
288
289for (@ops) {
2d6469fe 290 print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
79072805
LW
291}
292
2d6469fe 293print $oc <<'END';
27da23d5 294}
79072805 295#endif
97aff369 296#ifdef PERL_CHECK_INITED
27da23d5 297;
97aff369 298#endif /* #ifdef PERL_CHECK_INITED */
79072805 299
27da23d5
JH
300#ifndef PERL_GLOBAL_STRUCT_INIT
301
79072805 302#ifndef DOINIT
1ccb7c8d 303EXTCONST U32 PL_opargs[];
79072805 304#else
1ccb7c8d 305EXTCONST U32 PL_opargs[] = {
79072805
LW
306END
307
2d6469fe
NC
308# Emit allowed argument types.
309
310my $ARGBITS = 32;
311
d6480c9d
NC
312my %argnum = (
313 'S', 1, # scalar
314 'L', 2, # list
315 'A', 3, # array value
316 'H', 4, # hash value
317 'C', 5, # code value
318 'F', 6, # file value
319 'R', 7, # scalar reference
79072805
LW
320);
321
d6480c9d 322my %opclass = (
db173bac
MB
323 '0', 0, # baseop
324 '1', 1, # unop
325 '2', 2, # binop
326 '|', 3, # logop
1a67a97c
SM
327 '@', 4, # listop
328 '/', 5, # pmop
350de78d 329 '$', 6, # svop_or_padop
7934575e 330 '#', 7, # padop
1a67a97c
SM
331 '"', 8, # pvop_or_svop
332 '{', 9, # loop
333 ';', 10, # cop
334 '%', 11, # baseop_or_unop
335 '-', 12, # filestatop
336 '}', 13, # loopexop
db173bac
MB
337);
338
c2dedb93
MHM
339my %opflags = (
340 'm' => 1, # needs stack mark
341 'f' => 2, # fold constants
342 's' => 4, # always produces scalar
343 't' => 8, # needs target scalar
903fd87c
NC
344 'T' => 8 | 16, # ... which may be lexical
345 'i' => 0, # always produces integer (unused since e7311069)
c2dedb93
MHM
346 'I' => 32, # has corresponding int op
347 'd' => 64, # danger, unknown side effects
348 'u' => 128, # defaults to $_
349);
350
2b420b63
JC
351my %OP_IS_SOCKET; # /Fs/
352my %OP_IS_FILETEST; # /F-/
353my %OP_IS_FT_ACCESS; # /F-+/
354my %OP_IS_NUMCOMPARE; # /S</
332c2eac
JC
355my %OP_IS_DIRHOP; # /Fd/
356
903fd87c
NC
357my $OCSHIFT = 8;
358my $OASHIFT = 12;
a85d93d9 359
c2dedb93 360for my $op (@ops) {
d6480c9d 361 my $argsum = 0;
c2dedb93
MHM
362 my $flags = $flags{$op};
363 for my $flag (keys %opflags) {
364 if ($flags =~ s/$flag//) {
cb7b5e07 365 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
c2dedb93
MHM
366 if $argsum & $opflags{$flag};
367 $argsum |= $opflags{$flag};
368 }
369 }
cb7b5e07 370 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
c2dedb93
MHM
371 unless exists $opclass{$flags};
372 $argsum |= $opclass{$flags} << $OCSHIFT;
373 my $argshift = $OASHIFT;
374 for my $arg (split(' ',$args{$op})) {
332c2eac
JC
375 if ($arg =~ s/^D//) {
376 # handle 1st, just to put D 1st.
377 $OP_IS_DIRHOP{$op} = $opnum{$op};
378 }
a85d93d9 379 if ($arg =~ /^F/) {
e71197e2
JC
380 # record opnums of these opnames
381 $OP_IS_SOCKET{$op} = $opnum{$op} if $arg =~ s/s//;
382 $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
6ecf81d6 383 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
a85d93d9 384 }
2b420b63
JC
385 elsif ($arg =~ /^S</) {
386 $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
387 }
d6480c9d 388 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
c2dedb93
MHM
389 die "op = $op, arg = $arg\n"
390 unless exists $argnum{$arg};
79072805 391 $argnum += $argnum{$arg};
c2dedb93
MHM
392 die "Argument overflow for '$op'\n"
393 if $argshift >= $ARGBITS ||
394 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
395 $argsum += $argnum << $argshift;
396 $argshift += 4;
79072805
LW
397 }
398 $argsum = sprintf("0x%08x", $argsum);
2d6469fe 399 print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
79072805
LW
400}
401
2d6469fe 402print $oc <<'END';
79072805
LW
403};
404#endif
73c4f7a1 405
bae1192d
JH
406#endif /* !PERL_GLOBAL_STRUCT_INIT */
407
73c4f7a1 408END_EXTERN_C
79072805
LW
409END
410
e71197e2
JC
411# Emit OP_IS_* macros
412
2d6469fe 413print $on <<'EO_OP_IS_COMMENT';
e71197e2 414
332c2eac
JC
415/* the OP_IS_* macros are optimized to a simple range check because
416 all the member OPs are contiguous in regen/opcodes table.
417 opcode.pl verifies the range contiguity, or generates an OR-equals
418 expression */
e71197e2
JC
419EO_OP_IS_COMMENT
420
421gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
422gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
6ecf81d6 423gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
2b420b63 424gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
332c2eac 425gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
e71197e2
JC
426
427sub gen_op_is_macro {
428 my ($op_is, $macname) = @_;
429 if (keys %$op_is) {
430
431 # get opnames whose numbers are lowest and highest
432 my ($first, @rest) = sort {
433 $op_is->{$a} <=> $op_is->{$b}
434 } keys %$op_is;
435
436 my $last = pop @rest; # @rest slurped, get its last
cb7b5e07 437 die "Invalid range of ops: $first .. $last\n" unless $last;
6ecf81d6 438
ce716c52 439 print $on "\n#define $macname(op) \\\n\t(";
6ecf81d6 440
e71197e2
JC
441 # verify that op-ct matches 1st..last range (and fencepost)
442 # (we know there are no dups)
443 if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
444
445 # contiguous ops -> optimized version
2b420b63
JC
446 print $on "(op) >= OP_" . uc($first)
447 . " && (op) <= OP_" . uc($last);
e71197e2
JC
448 }
449 else {
424a4936 450 print $on join(" || \\\n\t ",
2b420b63 451 map { "(op) == OP_" . uc() } sort keys %$op_is);
e71197e2 452 }
2b420b63 453 print $on ")\n";
e71197e2 454 }
a85d93d9
JH
455}
456
cc49830d
NC
457my $pp = open_new('pp_proto.h', '>',
458 { 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.