This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: White-space only
[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'],
c22c99bc 120 Perl_pp_postinc => ['i_postinc', 'postdec', 'i_postdec'],
720d5dbf
NC
121 Perl_pp_ehostent => [qw(enetent eprotoent eservent
122 spwent epwent sgrent egrent)],
396166e1 123 Perl_pp_shostent => [qw(snetent sprotoent sservent)],
93bad3fd 124 Perl_pp_aelemfast => ['aelemfast_lex'],
605b9385 125 );
1d5774de
NC
126
127while (my ($func, $names) = splice @raw_alias, 0, 2) {
7627e6d0
NC
128 if (ref $names eq 'ARRAY') {
129 foreach (@$names) {
130 $alias{$_} = [$func, ''];
131 }
132 } else {
133 while (my ($opname, $cond) = each %$names) {
134 $alias{$opname} = [$func, $cond];
135 }
916e4025 136 }
1d5774de
NC
137}
138
7627e6d0
NC
139foreach my $sock_func (qw(socket bind listen accept shutdown
140 ssockopt getpeername)) {
141 $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
142}
143
79072805
LW
144# Emit defines.
145
cc49830d 146print $oc "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
9561d06f 147
7627e6d0
NC
148{
149 my $last_cond = '';
150 my @unimplemented;
151
152 sub unimplemented {
153 if (@unimplemented) {
2d6469fe 154 print $oc "#else\n";
7627e6d0 155 foreach (@unimplemented) {
2d6469fe 156 print $oc "#define $_ Perl_unimplemented_op\n";
7627e6d0 157 }
2d6469fe 158 print $oc "#endif\n";
7627e6d0
NC
159 @unimplemented = ();
160 }
161
162 }
163
164 for (@ops) {
165 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
166 my $op_func = "Perl_pp_$_";
167
168 if ($cond ne $last_cond) {
169 # A change in condition. (including to or from no condition)
170 unimplemented();
171 $last_cond = $cond;
172 if ($last_cond) {
2d6469fe 173 print $oc "$last_cond\n";
7627e6d0
NC
174 }
175 }
176 push @unimplemented, $op_func if $last_cond;
2d6469fe 177 print $oc "#define $op_func $impl\n" if $impl ne $op_func;
7627e6d0
NC
178 }
179 # If the last op was conditional, we need to close it out:
180 unimplemented();
9561d06f
NC
181}
182
cc49830d 183print $on "typedef enum opcode {\n";
abdd5c84 184
d6480c9d 185my $i = 0;
79072805 186for (@ops) {
2d6469fe 187 print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
79072805 188}
2d6469fe 189print $on "\t", tab(3,"OP_max"), "\n";
424a4936
NC
190print $on "} opcode;\n";
191print $on "\n#define MAXO ", scalar @ops, "\n";
79072805 192
c07a80fd 193# Emit op names and descriptions.
79072805 194
2d6469fe 195print $oc <<'END';
73c4f7a1
GS
196START_EXTERN_C
197
79072805 198#ifndef DOINIT
27da23d5 199EXTCONST char* const PL_op_name[];
79072805 200#else
27da23d5 201EXTCONST char* const PL_op_name[] = {
79072805
LW
202END
203
204for (@ops) {
2d6469fe 205 print $oc qq(\t"$_",\n);
c07a80fd 206}
207
2d6469fe 208print $oc <<'END';
c07a80fd 209};
210#endif
211
c07a80fd 212#ifndef DOINIT
27da23d5 213EXTCONST char* const PL_op_desc[];
c07a80fd 214#else
27da23d5 215EXTCONST char* const PL_op_desc[] = {
c07a80fd 216END
217
218for (@ops) {
42d38218
MS
219 my($safe_desc) = $desc{$_};
220
a567e93b 221 # Have to escape double quotes and escape characters.
b0c6325e 222 $safe_desc =~ s/([\\"])/\\$1/g;
42d38218 223
2d6469fe 224 print $oc qq(\t"$safe_desc",\n);
79072805
LW
225}
226
2d6469fe 227print $oc <<'END';
79072805
LW
228};
229#endif
230
73c4f7a1
GS
231END_EXTERN_C
232
27da23d5 233#endif /* !PERL_GLOBAL_STRUCT_INIT */
22c35a8c 234END
79072805 235
79072805
LW
236# Emit ppcode switch array.
237
2d6469fe 238print $oc <<'END';
79072805 239
73c4f7a1
GS
240START_EXTERN_C
241
27da23d5 242#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 243# define PERL_PPADDR_INITED
27da23d5 244static const Perl_ppaddr_t Gppaddr[]
79072805 245#else
27da23d5 246# ifndef PERL_GLOBAL_STRUCT
97aff369 247# define PERL_PPADDR_INITED
27da23d5
JH
248EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
249# endif
250#endif /* PERL_GLOBAL_STRUCT */
251#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 252# define PERL_PPADDR_INITED
27da23d5 253= {
79072805
LW
254END
255
256for (@ops) {
7627e6d0
NC
257 my $op_func = "Perl_pp_$_";
258 my $name = $alias{$_};
259 if ($name && $name->[0] ne $op_func) {
2d6469fe 260 print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
6faeeb49
MB
261 }
262 else {
2d6469fe 263 print $oc "\t$op_func,\n";
6faeeb49 264 }
79072805
LW
265}
266
2d6469fe 267print $oc <<'END';
27da23d5 268}
79072805 269#endif
97aff369 270#ifdef PERL_PPADDR_INITED
27da23d5 271;
97aff369 272#endif
79072805 273
27da23d5 274#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 275# define PERL_CHECK_INITED
27da23d5 276static const Perl_check_t Gcheck[]
79072805 277#else
27da23d5 278# ifndef PERL_GLOBAL_STRUCT
97aff369 279# define PERL_CHECK_INITED
27da23d5
JH
280EXT Perl_check_t PL_check[] /* or perlvars.h */
281# endif
282#endif
283#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 284# define PERL_CHECK_INITED
27da23d5 285= {
79072805
LW
286END
287
288for (@ops) {
2d6469fe 289 print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
79072805
LW
290}
291
2d6469fe 292print $oc <<'END';
27da23d5 293}
79072805 294#endif
97aff369 295#ifdef PERL_CHECK_INITED
27da23d5 296;
97aff369 297#endif /* #ifdef PERL_CHECK_INITED */
79072805 298
27da23d5
JH
299#ifndef PERL_GLOBAL_STRUCT_INIT
300
79072805 301#ifndef DOINIT
1ccb7c8d 302EXTCONST U32 PL_opargs[];
79072805 303#else
1ccb7c8d 304EXTCONST U32 PL_opargs[] = {
79072805
LW
305END
306
2d6469fe
NC
307# Emit allowed argument types.
308
309my $ARGBITS = 32;
310
d6480c9d
NC
311my %argnum = (
312 'S', 1, # scalar
313 'L', 2, # list
314 'A', 3, # array value
315 'H', 4, # hash value
316 'C', 5, # code value
317 'F', 6, # file value
318 'R', 7, # scalar reference
79072805
LW
319);
320
d6480c9d 321my %opclass = (
db173bac
MB
322 '0', 0, # baseop
323 '1', 1, # unop
324 '2', 2, # binop
325 '|', 3, # logop
1a67a97c
SM
326 '@', 4, # listop
327 '/', 5, # pmop
350de78d 328 '$', 6, # svop_or_padop
7934575e 329 '#', 7, # padop
1a67a97c
SM
330 '"', 8, # pvop_or_svop
331 '{', 9, # loop
332 ';', 10, # cop
333 '%', 11, # baseop_or_unop
334 '-', 12, # filestatop
335 '}', 13, # loopexop
db173bac
MB
336);
337
c2dedb93
MHM
338my %opflags = (
339 'm' => 1, # needs stack mark
340 'f' => 2, # fold constants
341 's' => 4, # always produces scalar
342 't' => 8, # needs target scalar
903fd87c
NC
343 'T' => 8 | 16, # ... which may be lexical
344 'i' => 0, # always produces integer (unused since e7311069)
c2dedb93
MHM
345 'I' => 32, # has corresponding int op
346 'd' => 64, # danger, unknown side effects
347 'u' => 128, # defaults to $_
348);
349
2b420b63
JC
350my %OP_IS_SOCKET; # /Fs/
351my %OP_IS_FILETEST; # /F-/
352my %OP_IS_FT_ACCESS; # /F-+/
353my %OP_IS_NUMCOMPARE; # /S</
332c2eac
JC
354my %OP_IS_DIRHOP; # /Fd/
355
903fd87c
NC
356my $OCSHIFT = 8;
357my $OASHIFT = 12;
a85d93d9 358
c2dedb93 359for my $op (@ops) {
d6480c9d 360 my $argsum = 0;
c2dedb93
MHM
361 my $flags = $flags{$op};
362 for my $flag (keys %opflags) {
363 if ($flags =~ s/$flag//) {
cb7b5e07 364 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
c2dedb93
MHM
365 if $argsum & $opflags{$flag};
366 $argsum |= $opflags{$flag};
367 }
368 }
cb7b5e07 369 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
c2dedb93
MHM
370 unless exists $opclass{$flags};
371 $argsum |= $opclass{$flags} << $OCSHIFT;
372 my $argshift = $OASHIFT;
373 for my $arg (split(' ',$args{$op})) {
332c2eac
JC
374 if ($arg =~ s/^D//) {
375 # handle 1st, just to put D 1st.
376 $OP_IS_DIRHOP{$op} = $opnum{$op};
377 }
a85d93d9 378 if ($arg =~ /^F/) {
e71197e2
JC
379 # record opnums of these opnames
380 $OP_IS_SOCKET{$op} = $opnum{$op} if $arg =~ s/s//;
381 $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
6ecf81d6 382 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
a85d93d9 383 }
2b420b63
JC
384 elsif ($arg =~ /^S</) {
385 $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
386 }
d6480c9d 387 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
c2dedb93
MHM
388 die "op = $op, arg = $arg\n"
389 unless exists $argnum{$arg};
79072805 390 $argnum += $argnum{$arg};
c2dedb93
MHM
391 die "Argument overflow for '$op'\n"
392 if $argshift >= $ARGBITS ||
393 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
394 $argsum += $argnum << $argshift;
395 $argshift += 4;
79072805
LW
396 }
397 $argsum = sprintf("0x%08x", $argsum);
2d6469fe 398 print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
79072805
LW
399}
400
2d6469fe 401print $oc <<'END';
79072805
LW
402};
403#endif
73c4f7a1 404
bae1192d
JH
405#endif /* !PERL_GLOBAL_STRUCT_INIT */
406
73c4f7a1 407END_EXTERN_C
79072805
LW
408END
409
e71197e2
JC
410# Emit OP_IS_* macros
411
2d6469fe 412print $on <<'EO_OP_IS_COMMENT';
e71197e2 413
332c2eac
JC
414/* the OP_IS_* macros are optimized to a simple range check because
415 all the member OPs are contiguous in regen/opcodes table.
416 opcode.pl verifies the range contiguity, or generates an OR-equals
417 expression */
e71197e2
JC
418EO_OP_IS_COMMENT
419
420gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
421gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
6ecf81d6 422gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
2b420b63 423gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
332c2eac 424gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
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
2b420b63
JC
445 print $on "(op) >= OP_" . uc($first)
446 . " && (op) <= OP_" . uc($last);
e71197e2
JC
447 }
448 else {
424a4936 449 print $on join(" || \\\n\t ",
2b420b63 450 map { "(op) == OP_" . uc() } sort keys %$op_is);
e71197e2 451 }
2b420b63 452 print $on ")\n";
e71197e2 453 }
a85d93d9
JH
454}
455
cc49830d
NC
456my $pp = open_new('pp_proto.h', '>',
457 { by => 'opcode.pl', from => 'its data' });
a27f85b3 458
981b7185
NC
459{
460 my %funcs;
461 for (@ops) {
7627e6d0 462 my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
981b7185
NC
463 ++$funcs{$name};
464 }
465 print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
735e0d5c 466}
ce716c52
NC
467foreach ($oc, $on, $pp) {
468 read_only_bottom_close_and_rename($_);
469}
b162f9ea
IZ
470
471# Some comments about 'T' opcode classifier:
472
473# Safe to set if the ppcode uses:
474# tryAMAGICbin, tryAMAGICun, SETn, SETi, SETu, PUSHn, PUSHTARG, SETTARG,
475# SETs(TARG), XPUSHn, XPUSHu,
476
477# Unsafe to set if the ppcode uses dTARG or [X]RETPUSH[YES|NO|UNDEF]
478
479# lt and friends do SETs (including ncmp, but not scmp)
480
21f5b33c
GS
481# Additional mode of failure: the opcode can modify TARG before it "used"
482# all the arguments (or may call an external function which does the same).
483# If the target coincides with one of the arguments ==> kaboom.
484
b162f9ea
IZ
485# pp.c pos substr each not OK (RETPUSHUNDEF)
486# substr vec also not OK due to LV to target (are they???)
487# ref not OK (RETPUSHNO)
488# trans not OK (dTARG; TARG = sv_newmortal();)
489# ucfirst etc not OK: TMP arg processed inplace
69b47968 490# quotemeta not OK (unsafe when TARG == arg)
91e74348 491# each repeat not OK too due to list context
b162f9ea 492# pack split - unknown whether they are safe
dae78bb1
IZ
493# sprintf: is calling do_sprintf(TARG,...) which can act on TARG
494# before other args are processed.
b162f9ea 495
21f5b33c
GS
496# Suspicious wrt "additional mode of failure" (and only it):
497# schop, chop, postinc/dec, bit_and etc, negate, complement.
498
499# Also suspicious: 4-arg substr, sprintf, uc/lc (POK_only), reverse, pack.
500
501# substr/vec: doing TAINT_off()???
502
b162f9ea
IZ
503# pp_hot.c
504# readline - unknown whether it is safe
505# match subst not OK (dTARG)
506# grepwhile not OK (not always setting)
69b47968 507# join not OK (unsafe when TARG == arg)
b162f9ea 508
21f5b33c
GS
509# Suspicious wrt "additional mode of failure": concat (dealt with
510# in ck_sassign()), join (same).
511
b162f9ea
IZ
512# pp_ctl.c
513# mapwhile flip caller not OK (not always setting)
514
515# pp_sys.c
516# backtick glob warn die not OK (not always setting)
517# warn not OK (RETPUSHYES)
518# open fileno getc sysread syswrite ioctl accept shutdown
519# ftsize(etc) readlink telldir fork alarm getlogin not OK (RETPUSHUNDEF)
520# umask select not OK (XPUSHs(&PL_sv_undef);)
521# fileno getc sysread syswrite tell not OK (meth("FILENO" "GETC"))
522# sselect shm* sem* msg* syscall - unknown whether they are safe
523# gmtime not OK (list context)
524
21f5b33c 525# Suspicious wrt "additional mode of failure": warn, die, select.