This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp.c: Fix Win32 compilation problems
[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 46 warn qq[Description "$desc" duplicates $seen{$desc}\n]
13f89586 47 if $seen{$desc} and $key !~ "transr|(?:intro|clone)cv";
c07a80fd 48 die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
6e64f32b
FC
49 die qq[Opcode "freed" is reserved for the slab allocator\n]
50 if $key eq 'freed';
c07a80fd 51 $seen{$desc} = qq[description of opcode "$key"];
52 $seen{$key} = qq[opcode "$key"];
53
79072805 54 push(@ops, $key);
e71197e2 55 $opnum{$key} = $#ops;
c07a80fd 56 $desc{$key} = $desc;
79072805
LW
57 $check{$key} = $check;
58 $ckname{$check}++;
59 $flags{$key} = $flags;
60 $args{$key} = $args;
61}
62
1d5774de
NC
63# Set up aliases
64
65my %alias;
66
67# Format is "this function" => "does these op names"
68my @raw_alias = (
6faeeb49 69 Perl_do_kv => [qw( keys values )],
605fa6bf 70 Perl_unimplemented_op => [qw(padany mapstart custom)],
0b612f93
NC
71 # All the ops with a body of { return NORMAL; }
72 Perl_pp_null => [qw(scalar regcmaybe lineseq scope)],
73
74 Perl_pp_goto => ['dump'],
75 Perl_pp_require => ['dofile'],
76 Perl_pp_untie => ['dbmclose'],
7627e6d0 77 Perl_pp_sysread => {read => '', recv => '#ifdef HAS_SOCKET'},
0b612f93
NC
78 Perl_pp_sysseek => ['seek'],
79 Perl_pp_ioctl => ['fcntl'],
7627e6d0
NC
80 Perl_pp_ssockopt => {gsockopt => '#ifdef HAS_SOCKET'},
81 Perl_pp_getpeername => {getsockname => '#ifdef HAS_SOCKET'},
0b612f93 82 Perl_pp_stat => ['lstat'],
f1cb2d48 83 Perl_pp_ftrowned => [qw(fteowned ftzero ftsock ftchr ftblk
17ad201a
NC
84 ftfile ftdir ftpipe ftsuid ftsgid
85 ftsvtx)],
0b612f93
NC
86 Perl_pp_fttext => ['ftbinary'],
87 Perl_pp_gmtime => ['localtime'],
88 Perl_pp_semget => [qw(shmget msgget)],
89 Perl_pp_semctl => [qw(shmctl msgctl)],
0b612f93
NC
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)],
957b0e1d 96 Perl_pp_ftis => [qw(ftsize ftmtime ftatime ftctime)],
605b9385 97 Perl_pp_chown => [qw(unlink chmod utime kill)],
ce6987d0 98 Perl_pp_link => ['symlink'],
af9e49b4
NC
99 Perl_pp_ftrread => [qw(ftrwrite ftrexec fteread ftewrite
100 fteexec)],
ca563b4e 101 Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
7627e6d0 102 Perl_pp_syswrite => {send => '#ifdef HAS_SOCKET'},
c960fc3b 103 Perl_pp_defined => [qw(dor dorassign)],
62726f23
SP
104 Perl_pp_and => ['andassign'],
105 Perl_pp_or => ['orassign'],
12e9c124 106 Perl_pp_ucfirst => ['lcfirst'],
afd9910b 107 Perl_pp_sle => [qw(slt sgt sge)],
0d863452 108 Perl_pp_print => ['say'],
2723d216 109 Perl_pp_index => ['rindex'],
daa2adfd 110 Perl_pp_oct => ['hex'],
789b4bc9 111 Perl_pp_shift => ['pop'],
71302fe3 112 Perl_pp_sin => [qw(cos exp log sqrt)],
3658c1f1 113 Perl_pp_bit_or => ['bit_xor'],
17ab7946 114 Perl_pp_rv2av => ['rv2hv'],
878d132a 115 Perl_pp_akeys => ['avalues'],
cba5a3b0 116 Perl_pp_rkeys => [qw(rvalues reach)],
7627e6d0
NC
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'},
17058fe0 121 Perl_pp_preinc => ['i_preinc', 'predec', 'i_predec'],
c22c99bc 122 Perl_pp_postinc => ['i_postinc', 'postdec', 'i_postdec'],
720d5dbf
NC
123 Perl_pp_ehostent => [qw(enetent eprotoent eservent
124 spwent epwent sgrent egrent)],
396166e1 125 Perl_pp_shostent => [qw(snetent sprotoent sservent)],
93bad3fd 126 Perl_pp_aelemfast => ['aelemfast_lex'],
605b9385 127 );
1d5774de
NC
128
129while (my ($func, $names) = splice @raw_alias, 0, 2) {
7627e6d0
NC
130 if (ref $names eq 'ARRAY') {
131 foreach (@$names) {
132 $alias{$_} = [$func, ''];
133 }
134 } else {
135 while (my ($opname, $cond) = each %$names) {
136 $alias{$opname} = [$func, $cond];
137 }
916e4025 138 }
1d5774de
NC
139}
140
7627e6d0
NC
141foreach my $sock_func (qw(socket bind listen accept shutdown
142 ssockopt getpeername)) {
143 $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
144}
145
79072805
LW
146# Emit defines.
147
cc49830d 148print $oc "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
9561d06f 149
7627e6d0
NC
150{
151 my $last_cond = '';
152 my @unimplemented;
153
154 sub unimplemented {
155 if (@unimplemented) {
2d6469fe 156 print $oc "#else\n";
7627e6d0 157 foreach (@unimplemented) {
2d6469fe 158 print $oc "#define $_ Perl_unimplemented_op\n";
7627e6d0 159 }
2d6469fe 160 print $oc "#endif\n";
7627e6d0
NC
161 @unimplemented = ();
162 }
163
164 }
165
166 for (@ops) {
167 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
168 my $op_func = "Perl_pp_$_";
169
170 if ($cond ne $last_cond) {
171 # A change in condition. (including to or from no condition)
172 unimplemented();
173 $last_cond = $cond;
174 if ($last_cond) {
2d6469fe 175 print $oc "$last_cond\n";
7627e6d0
NC
176 }
177 }
178 push @unimplemented, $op_func if $last_cond;
2d6469fe 179 print $oc "#define $op_func $impl\n" if $impl ne $op_func;
7627e6d0
NC
180 }
181 # If the last op was conditional, we need to close it out:
182 unimplemented();
9561d06f
NC
183}
184
cc49830d 185print $on "typedef enum opcode {\n";
abdd5c84 186
d6480c9d 187my $i = 0;
79072805 188for (@ops) {
2d6469fe 189 print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
79072805 190}
2d6469fe 191print $on "\t", tab(3,"OP_max"), "\n";
424a4936
NC
192print $on "} opcode;\n";
193print $on "\n#define MAXO ", scalar @ops, "\n";
6e64f32b 194print $on "#define OP_FREED MAXO\n";
79072805 195
c07a80fd 196# Emit op names and descriptions.
79072805 197
2d6469fe 198print $oc <<'END';
73c4f7a1
GS
199START_EXTERN_C
200
79072805 201#ifndef DOINIT
27da23d5 202EXTCONST char* const PL_op_name[];
79072805 203#else
27da23d5 204EXTCONST char* const PL_op_name[] = {
79072805
LW
205END
206
207for (@ops) {
2d6469fe 208 print $oc qq(\t"$_",\n);
c07a80fd 209}
210
2d6469fe 211print $oc <<'END';
a33a81d0 212 "freed",
c07a80fd 213};
214#endif
215
c07a80fd 216#ifndef DOINIT
27da23d5 217EXTCONST char* const PL_op_desc[];
c07a80fd 218#else
27da23d5 219EXTCONST char* const PL_op_desc[] = {
c07a80fd 220END
221
222for (@ops) {
42d38218
MS
223 my($safe_desc) = $desc{$_};
224
a567e93b 225 # Have to escape double quotes and escape characters.
b0c6325e 226 $safe_desc =~ s/([\\"])/\\$1/g;
42d38218 227
2d6469fe 228 print $oc qq(\t"$safe_desc",\n);
79072805
LW
229}
230
2d6469fe 231print $oc <<'END';
a33a81d0 232 "freed op",
79072805
LW
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
2d6469fe 243print $oc <<'END';
79072805 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) {
2d6469fe 265 print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
6faeeb49
MB
266 }
267 else {
2d6469fe 268 print $oc "\t$op_func,\n";
6faeeb49 269 }
79072805
LW
270}
271
2d6469fe 272print $oc <<'END';
27da23d5 273}
79072805 274#endif
97aff369 275#ifdef PERL_PPADDR_INITED
27da23d5 276;
97aff369 277#endif
79072805 278
27da23d5 279#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 280# define PERL_CHECK_INITED
27da23d5 281static const Perl_check_t Gcheck[]
79072805 282#else
27da23d5 283# ifndef PERL_GLOBAL_STRUCT
97aff369 284# define PERL_CHECK_INITED
27da23d5
JH
285EXT Perl_check_t PL_check[] /* or perlvars.h */
286# endif
287#endif
288#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 289# define PERL_CHECK_INITED
27da23d5 290= {
79072805
LW
291END
292
293for (@ops) {
2d6469fe 294 print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
79072805
LW
295}
296
2d6469fe 297print $oc <<'END';
27da23d5 298}
79072805 299#endif
97aff369 300#ifdef PERL_CHECK_INITED
27da23d5 301;
97aff369 302#endif /* #ifdef PERL_CHECK_INITED */
79072805 303
27da23d5
JH
304#ifndef PERL_GLOBAL_STRUCT_INIT
305
79072805 306#ifndef DOINIT
1ccb7c8d 307EXTCONST U32 PL_opargs[];
79072805 308#else
1ccb7c8d 309EXTCONST U32 PL_opargs[] = {
79072805
LW
310END
311
2d6469fe
NC
312# Emit allowed argument types.
313
314my $ARGBITS = 32;
315
d6480c9d
NC
316my %argnum = (
317 'S', 1, # scalar
318 'L', 2, # list
319 'A', 3, # array value
320 'H', 4, # hash value
321 'C', 5, # code value
322 'F', 6, # file value
323 'R', 7, # scalar reference
79072805
LW
324);
325
d6480c9d 326my %opclass = (
db173bac
MB
327 '0', 0, # baseop
328 '1', 1, # unop
329 '2', 2, # binop
330 '|', 3, # logop
1a67a97c
SM
331 '@', 4, # listop
332 '/', 5, # pmop
350de78d 333 '$', 6, # svop_or_padop
7934575e 334 '#', 7, # padop
1a67a97c
SM
335 '"', 8, # pvop_or_svop
336 '{', 9, # loop
337 ';', 10, # cop
338 '%', 11, # baseop_or_unop
339 '-', 12, # filestatop
340 '}', 13, # loopexop
db173bac
MB
341);
342
c2dedb93
MHM
343my %opflags = (
344 'm' => 1, # needs stack mark
345 'f' => 2, # fold constants
346 's' => 4, # always produces scalar
347 't' => 8, # needs target scalar
903fd87c
NC
348 'T' => 8 | 16, # ... which may be lexical
349 'i' => 0, # always produces integer (unused since e7311069)
c2dedb93
MHM
350 'I' => 32, # has corresponding int op
351 'd' => 64, # danger, unknown side effects
352 'u' => 128, # defaults to $_
353);
354
2b420b63
JC
355my %OP_IS_SOCKET; # /Fs/
356my %OP_IS_FILETEST; # /F-/
357my %OP_IS_FT_ACCESS; # /F-+/
358my %OP_IS_NUMCOMPARE; # /S</
332c2eac
JC
359my %OP_IS_DIRHOP; # /Fd/
360
903fd87c
NC
361my $OCSHIFT = 8;
362my $OASHIFT = 12;
a85d93d9 363
c2dedb93 364for my $op (@ops) {
d6480c9d 365 my $argsum = 0;
c2dedb93
MHM
366 my $flags = $flags{$op};
367 for my $flag (keys %opflags) {
368 if ($flags =~ s/$flag//) {
cb7b5e07 369 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
c2dedb93
MHM
370 if $argsum & $opflags{$flag};
371 $argsum |= $opflags{$flag};
372 }
373 }
cb7b5e07 374 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
c2dedb93
MHM
375 unless exists $opclass{$flags};
376 $argsum |= $opclass{$flags} << $OCSHIFT;
377 my $argshift = $OASHIFT;
378 for my $arg (split(' ',$args{$op})) {
332c2eac
JC
379 if ($arg =~ s/^D//) {
380 # handle 1st, just to put D 1st.
381 $OP_IS_DIRHOP{$op} = $opnum{$op};
382 }
a85d93d9 383 if ($arg =~ /^F/) {
e71197e2
JC
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/-//;
6ecf81d6 387 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
a85d93d9 388 }
2b420b63
JC
389 elsif ($arg =~ /^S</) {
390 $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
391 }
d6480c9d 392 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
c2dedb93
MHM
393 die "op = $op, arg = $arg\n"
394 unless exists $argnum{$arg};
79072805 395 $argnum += $argnum{$arg};
c2dedb93
MHM
396 die "Argument overflow for '$op'\n"
397 if $argshift >= $ARGBITS ||
398 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
399 $argsum += $argnum << $argshift;
400 $argshift += 4;
79072805
LW
401 }
402 $argsum = sprintf("0x%08x", $argsum);
2d6469fe 403 print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
79072805
LW
404}
405
2d6469fe 406print $oc <<'END';
79072805
LW
407};
408#endif
73c4f7a1 409
bae1192d
JH
410#endif /* !PERL_GLOBAL_STRUCT_INIT */
411
73c4f7a1 412END_EXTERN_C
79072805
LW
413END
414
e71197e2
JC
415# Emit OP_IS_* macros
416
2d6469fe 417print $on <<'EO_OP_IS_COMMENT';
e71197e2 418
332c2eac
JC
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
422 expression */
e71197e2
JC
423EO_OP_IS_COMMENT
424
425gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
426gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
6ecf81d6 427gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
2b420b63 428gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
332c2eac 429gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
e71197e2
JC
430
431sub gen_op_is_macro {
432 my ($op_is, $macname) = @_;
433 if (keys %$op_is) {
434
435 # get opnames whose numbers are lowest and highest
436 my ($first, @rest) = sort {
437 $op_is->{$a} <=> $op_is->{$b}
438 } keys %$op_is;
439
440 my $last = pop @rest; # @rest slurped, get its last
cb7b5e07 441 die "Invalid range of ops: $first .. $last\n" unless $last;
6ecf81d6 442
ce716c52 443 print $on "\n#define $macname(op) \\\n\t(";
6ecf81d6 444
e71197e2
JC
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) {
448
449 # contiguous ops -> optimized version
2b420b63
JC
450 print $on "(op) >= OP_" . uc($first)
451 . " && (op) <= OP_" . uc($last);
e71197e2
JC
452 }
453 else {
424a4936 454 print $on join(" || \\\n\t ",
2b420b63 455 map { "(op) == OP_" . uc() } sort keys %$op_is);
e71197e2 456 }
2b420b63 457 print $on ")\n";
e71197e2 458 }
a85d93d9
JH
459}
460
cc49830d
NC
461my $pp = open_new('pp_proto.h', '>',
462 { by => 'opcode.pl', from => 'its data' });
a27f85b3 463
981b7185
NC
464{
465 my %funcs;
466 for (@ops) {
7627e6d0 467 my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
981b7185
NC
468 ++$funcs{$name};
469 }
470 print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
735e0d5c 471}
ce716c52
NC
472foreach ($oc, $on, $pp) {
473 read_only_bottom_close_and_rename($_);
474}
b162f9ea
IZ
475
476# Some comments about 'T' opcode classifier:
477
478# Safe to set if the ppcode uses:
479# tryAMAGICbin, tryAMAGICun, SETn, SETi, SETu, PUSHn, PUSHTARG, SETTARG,
480# SETs(TARG), XPUSHn, XPUSHu,
481
482# Unsafe to set if the ppcode uses dTARG or [X]RETPUSH[YES|NO|UNDEF]
483
484# lt and friends do SETs (including ncmp, but not scmp)
485
21f5b33c
GS
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.
489
b162f9ea
IZ
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
69b47968 495# quotemeta not OK (unsafe when TARG == arg)
91e74348 496# each repeat not OK too due to list context
b162f9ea 497# pack split - unknown whether they are safe
dae78bb1
IZ
498# sprintf: is calling do_sprintf(TARG,...) which can act on TARG
499# before other args are processed.
b162f9ea 500
21f5b33c
GS
501# Suspicious wrt "additional mode of failure" (and only it):
502# schop, chop, postinc/dec, bit_and etc, negate, complement.
503
504# Also suspicious: 4-arg substr, sprintf, uc/lc (POK_only), reverse, pack.
505
506# substr/vec: doing TAINT_off()???
507
b162f9ea
IZ
508# pp_hot.c
509# readline - unknown whether it is safe
510# match subst not OK (dTARG)
511# grepwhile not OK (not always setting)
69b47968 512# join not OK (unsafe when TARG == arg)
b162f9ea 513
21f5b33c
GS
514# Suspicious wrt "additional mode of failure": concat (dealt with
515# in ck_sassign()), join (same).
516
b162f9ea
IZ
517# pp_ctl.c
518# mapwhile flip caller not OK (not always setting)
519
520# pp_sys.c
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)
529
21f5b33c 530# Suspicious wrt "additional mode of failure": warn, die, select.