#
# Regenerate (overwriting only if changed):
#
# opcode.h
# opnames.h
# pp_proto.h
# pp.sym
#
# from information stored in the DATA section of this file, plus the
# values hardcoded into this script in @raw_alias.
#
# Accepts the standard regen_lib -q and -v args.
#
# This script is normally invoked from regen.pl.
use strict;
BEGIN {
# Get function prototypes
require 'regen_lib.pl';
}
my $opcode_new = 'opcode.h-new';
my $opname_new = 'opnames.h-new';
select $oc;
# Read data.
my %seen;
while (<DATA>) {
chop;
next unless $_;
next if /^#/;
}
# Set up aliases
my %alias;
# Format is "this function" => "does these op names"
my @raw_alias = (
Perl_do_kv => [qw( keys values )],
# All the ops with a body of { return NORMAL; }
Perl_pp_goto => ['dump'],
Perl_pp_require => ['dofile'],
Perl_pp_untie => ['dbmclose'],
Perl_pp_sysread => [qw(read recv)],
Perl_pp_sysseek => ['seek'],
Perl_pp_ioctl => ['fcntl'],
Perl_pp_ssockopt => ['gsockopt'],
Perl_pp_getpeername => ['getsockname'],
Perl_pp_stat => ['lstat'],
ftsvtx)],
Perl_pp_fttext => ['ftbinary'],
Perl_pp_gmtime => ['localtime'],
Perl_pp_semget => [qw(shmget msgget)],
Perl_pp_semctl => [qw(shmctl msgctl)],
Perl_pp_chown => [qw(unlink chmod utime kill)],
Perl_pp_link => ['symlink'],
fteexec)],
Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
Perl_pp_send => ['syswrite'],
Perl_pp_and => ['andassign'],
Perl_pp_or => ['orassign'],
Perl_pp_ucfirst => ['lcfirst'],
Perl_pp_print => ['say'],
Perl_pp_index => ['rindex'],
Perl_pp_oct => ['hex'],
Perl_pp_shift => ['pop'],
Perl_pp_sin => [qw(cos exp log sqrt)],
Perl_pp_bit_or => ['bit_xor'],
Perl_pp_rv2av => ['rv2hv'],
Perl_pp_akeys => ['avalues'],
);
}
# Emit defines.
print <<"END";
*
* opcode.h
*
*
*
*/
#ifndef PERL_GLOBAL_STRUCT_INIT
#define Perl_pp_i_preinc Perl_pp_preinc
#define Perl_pp_i_predec Perl_pp_predec
#define Perl_pp_i_postinc Perl_pp_postinc
#define Perl_pp_i_postdec Perl_pp_postdec
END
print $on <<"END";
*
* opnames.h
*
*
*
*
*/
END
my $i = 0;
for (@ops) {
# print $on "\t", &tab(3,"OP_\U$_,"), "/* ", $i++, " */\n";
}
print $on "} opcode;\n";
print $on "#define OP_phoney_INPUT_ONLY -1\n";
print $on "#define OP_phoney_OUTPUT_ONLY -2\n\n";
# Emit op names and descriptions.
print <<END;
#define OP_NAME(o) ((o)->op_type == OP_CUSTOM ? custom_op_name(o) : \\
PL_op_name[(o)->op_type])
#define OP_DESC(o) ((o)->op_type == OP_CUSTOM ? custom_op_desc(o) : \\
PL_op_desc[(o)->op_type])
#ifndef DOINIT
#else
END
for (@ops) {
print qq(\t"$_",\n);
}
print <<END;
};
#endif
END
print <<END;
#ifndef DOINIT
#else
END
for (@ops) {
# Have to escape double quotes and escape characters.
$safe_desc =~ s/(^|[^\\])([\\"])/$1\\$2/g;
print qq(\t"$safe_desc",\n);
}
print <<END;
};
#endif
#endif /* !PERL_GLOBAL_STRUCT_INIT */
END
# Emit function declarations.
#for (sort keys %ckname) {
# print "OP *\t", &tab(3,$_),"(pTHX_ OP* o);\n";
#}
#
#print "\n";
#
#for (@ops) {
# print "OP *\t", &tab(3, "pp_$_"), "(pTHX);\n";
#}
# Emit ppcode switch array.
print <<END;
#ifdef PERL_GLOBAL_STRUCT_INIT
# define PERL_PPADDR_INITED
#else
# ifndef PERL_GLOBAL_STRUCT
# define PERL_PPADDR_INITED
# endif
#endif /* PERL_GLOBAL_STRUCT */
#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
# define PERL_PPADDR_INITED
= {
END
for (@ops) {
print "\tMEMBER_TO_FPTR($name),\t/* Perl_pp_$_ */\n";
}
else {
print "\tMEMBER_TO_FPTR(Perl_pp_$_),\n";
}
}
print <<END;
}
#endif
#ifdef PERL_PPADDR_INITED
;
#endif
END
# Emit check routines.
print <<END;
#ifdef PERL_GLOBAL_STRUCT_INIT
# define PERL_CHECK_INITED
#else
# ifndef PERL_GLOBAL_STRUCT
# define PERL_CHECK_INITED
# endif
#endif
#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
# define PERL_CHECK_INITED
= {
END
for (@ops) {
}
print <<END;
}
#endif
#ifdef PERL_CHECK_INITED
;
#endif /* #ifdef PERL_CHECK_INITED */
END
# Emit allowed argument types.
my $ARGBITS = 32;
print <<END;
#ifndef PERL_GLOBAL_STRUCT_INIT
#ifndef DOINIT
#else
END
my %argnum = (
'S', 1, # scalar
'L', 2, # list
'A', 3, # array value
'H', 4, # hash value
'C', 5, # code value
'F', 6, # file value
'R', 7, # scalar reference
);
my %opclass = (
'0', 0, # baseop
'1', 1, # unop
'2', 2, # binop
'|', 3, # logop
'@', 4, # listop
'/', 5, # pmop
'$', 6, # svop_or_padop
'#', 7, # padop
'"', 8, # pvop_or_svop
'{', 9, # loop
';', 10, # cop
'%', 11, # baseop_or_unop
'-', 12, # filestatop
'}', 13, # loopexop
);
my %opflags = (
'm' => 1, # needs stack mark
'f' => 2, # fold constants
's' => 4, # always produces scalar
't' => 8, # needs target scalar
'T' => 8 | 256, # ... which may be lexical
'i' => 16, # always produces integer
'I' => 32, # has corresponding int op
'd' => 64, # danger, unknown side effects
'u' => 128, # defaults to $_
);
my %OP_IS_SOCKET;
my %OP_IS_FILETEST;
my %OP_IS_FT_ACCESS;
my $OCSHIFT = 9;
my $OASHIFT = 13;
my $argsum = 0;
die "Flag collision for '$op' ($flags{$op}, $flag)\n"
}
}
if ($arg =~ /^F/) {
# record opnums of these opnames
}
die "op = $op, arg = $arg\n"
die "Argument overflow for '$op'\n"
$argshift += 4;
}
}
print <<END;
};
#endif
#endif /* !PERL_GLOBAL_STRUCT_INIT */
END
# Emit OP_IS_* macros
print $on <<EO_OP_IS_COMMENT;
sub gen_op_is_macro {
if (keys %$op_is) {
# get opnames whose numbers are lowest and highest
} keys %$op_is;
die "Invalid range of ops: $first .. $last\n" unless $last;
print $on "#define $macname(op) \\\n\t(";
# verify that op-ct matches 1st..last range (and fencepost)
# (we know there are no dups)
# contiguous ops -> optimized version
print $on ")\n\n";
}
else {
print $on join(" || \\\n\t ",
map { "(op) == OP_" . uc() } sort keys %$op_is);
print $on ")\n\n";
}
}
}
print $oc "/* ex: set ro: */\n";
print $on "/* ex: set ro: */\n";
safer_close($oc);
safer_close($on);
my $pp_proto_new = 'pp_proto.h-new';
my $pp_sym_new = 'pp.sym-new';
print $pp <<"END";
*/
END
print $ppsym <<"END";
# -*- buffer-read-only: t -*-
#
# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
# This file is built by opcode.pl from its data. Any changes made here
# will be lost!
#
END
for (sort keys %ckname) {
print $pp "PERL_CKDEF(Perl_$_)\n";
print $ppsym "Perl_$_\n";
#OP *\t", &tab(3,$_),"(OP* o);\n";
}
print $pp "\n\n";
for (@ops) {
next if /^custom$/;
print $pp "PERL_PPDEF(Perl_pp_$_)\n";
print $ppsym "Perl_pp_$_\n";
}
print $pp "\n/* ex: set ro: */\n";
print $ppsym "\n# ex: set ro:\n";
safer_close($pp);
safer_close($ppsym);
END {
foreach ('opcode.h', 'opnames.h', 'pp_proto.h', 'pp.sym') {
1 while unlink "$_-old";
}
}
###########################################################################
sub tab {
my ($l, $t) = @_;
$t .= "\t" x ($l - (length($t) + 1) / 8);
$t;
}
###########################################################################
# Some comments about 'T' opcode classifier:
# Safe to set if the ppcode uses:
# tryAMAGICbin, tryAMAGICun, SETn, SETi, SETu, PUSHn, PUSHTARG, SETTARG,
# SETs(TARG), XPUSHn, XPUSHu,
# Unsafe to set if the ppcode uses dTARG or [X]RETPUSH[YES|NO|UNDEF]
# lt and friends do SETs (including ncmp, but not scmp)
# Additional mode of failure: the opcode can modify TARG before it "used"
# all the arguments (or may call an external function which does the same).
# If the target coincides with one of the arguments ==> kaboom.
# pp.c pos substr each not OK (RETPUSHUNDEF)
# substr vec also not OK due to LV to target (are they???)
# ref not OK (RETPUSHNO)
# trans not OK (dTARG; TARG = sv_newmortal();)
# ucfirst etc not OK: TMP arg processed inplace
# quotemeta not OK (unsafe when TARG == arg)
# each repeat not OK too due to list context
# pack split - unknown whether they are safe
# sprintf: is calling do_sprintf(TARG,...) which can act on TARG
# before other args are processed.
# Suspicious wrt "additional mode of failure" (and only it):
# pp_hot.c
# readline - unknown whether it is safe
# match subst not OK (dTARG)
# grepwhile not OK (not always setting)
# join not OK (unsafe when TARG == arg)
# Suspicious wrt "additional mode of failure": concat (dealt with
# in ck_sassign()), join (same).
# pp_ctl.c
# mapwhile flip caller not OK (not always setting)
# pp_sys.c
# backtick glob warn die not OK (not always setting)
# warn not OK (RETPUSHYES)
# open fileno getc sysread syswrite ioctl accept shutdown
# ftsize(etc) readlink telldir fork alarm getlogin not OK (RETPUSHUNDEF)
# umask select not OK (XPUSHs(&PL_sv_undef);)
# fileno getc sysread syswrite tell not OK (meth("FILENO" "GETC"))
# sselect shm* sem* msg* syscall - unknown whether they are safe
# gmtime not OK (list context)
# Suspicious wrt "additional mode of failure": warn, die, select.
# New ops always go at the end
# The restriction on having custom as the last op has been removed
# A recapitulation of the format of this file:
# The file consists of five columns: the name of the op, an English
# description, the name of the "check" routine used to optimize this
# operation, some flags, and a description of the operands.
# The flags consist of options followed by a mandatory op class signifier
# The classes are:
# baseop - 0 unop - 1 binop - 2
# logop - | listop - @ pmop - /
# Other options are:
# needs stack mark - m
# needs constant folding - f
# produces a scalar - s
# produces an integer - i
# needs a target - t
# target can be in a pad - T
# has a corresponding integer version - I
# has side effects - d
# uses $_ if no argument given - u
# Values for the operands are:
# scalar - S list - L array - A
# hash - H sub (CV) - C file - F
# socket - Fs filetest - F- filetest_access - F-+
# reference - R
# "?" denotes an optional operand.
# Nothing.
scalar scalar ck_fun s% S
# Pushy stuff.
# References and stuff.
prototype subroutine prototype ck_null s% S
bless bless ck_fun s@ S S?
# Pushy I/O.
# glob defaults its first arg to $_
glob glob ck_glob t@ S?
readline <HANDLE> ck_readline t% F?
# Bindable operators.
# Lvalue operators.
# sassign is special-cased for op class
# Ordinary operators.
# High falutin' math.
srand srand ck_fun s% S?
# Lowbrow math.
# String stuff.
# Arrays.
# Hashes.
each each ck_each % H
values values ck_each t% H
keys keys ck_each t% H
delete delete ck_delete % S
# Explosives and implosives.
unpack unpack ck_unpack @ S S?
split split ck_split t@ S S S
# List operators.
splice splice ck_fun m@ A S? S? L
pop pop ck_shift s% A?
shift shift ck_shift s% A?
# Range stuff.
# Control.
caller caller ck_fun t% S?
entergiven given() ck_null d|
break break ck_null 0
continue continue ck_null 0
# I/O.
binmode binmode ck_fun s@ F S?
tied tied ck_fun s% R
print print ck_listiob ims@ F? L
sysopen sysopen ck_fun s@ F S S S?
sysseek sysseek ck_fun s@ F S S
seek seek ck_fun s@ F S S
# truncate really behaves as if it had both "S S" and "F S"
# Sockets. OP_IS_SOCKET wants them consecutive (so moved 1st 2)
# Stat calls. OP_IS_FILETEST wants them consecutive.
lstat lstat ck_ftst u- F
stat stat ck_ftst u- F
# File calls.
# chdir really behaves as if it had both "S?" and "F?"
# Directory calls.
readdir readdir ck_fun % F
seekdir seekdir ck_fun s@ F S
rewinddir rewinddir ck_fun s% F
# Process control.
# Time calls.
# NOTE: MacOS patches the 'i' of time() away later when the interpreter
# is created because in MacOS time() is already returning times > 2**31-1,
# that is, non-integers.
localtime localtime ck_fun t% S?
gmtime gmtime ck_fun t% S?
# Shared memory.
# Message passing.
# Semaphores.
# Eval.
require require ck_require du% S?
#evalonce eval constant string ck_null d1 S
# Get system info.
gpbynumber getprotobynumber ck_fun @ S
# Miscellaneous.
# For multi-threading
lock lock ck_rfun s% R
# For state support