2013-10-27 05:20:42 +08:00
|
|
|
#!/usr/bin/perl
|
|
|
|
## --------------------------------------------------------------------------
|
|
|
|
##
|
2018-06-16 15:13:58 +08:00
|
|
|
## Copyright 1996-2018 The NASM Authors - All Rights Reserved
|
2013-10-27 05:20:42 +08:00
|
|
|
## See the file AUTHORS included with the NASM distribution for
|
|
|
|
## the specific copyright holders.
|
|
|
|
##
|
|
|
|
## Redistribution and use in source and binary forms, with or without
|
|
|
|
## modification, are permitted provided that the following
|
|
|
|
## conditions are met:
|
|
|
|
##
|
|
|
|
## * Redistributions of source code must retain the above copyright
|
|
|
|
## notice, this list of conditions and the following disclaimer.
|
|
|
|
## * Redistributions in binary form must reproduce the above
|
|
|
|
## copyright notice, this list of conditions and the following
|
|
|
|
## disclaimer in the documentation and/or other materials provided
|
|
|
|
## with the distribution.
|
|
|
|
##
|
|
|
|
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
##
|
|
|
|
## --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#
|
2013-11-25 03:52:32 +08:00
|
|
|
# Instruction template flags. These specify which processor
|
|
|
|
# targets the instruction is eligible for, whether it is
|
|
|
|
# privileged or undocumented, and also specify extra error
|
|
|
|
# checking on the matching of the instruction.
|
|
|
|
#
|
|
|
|
# IF_SM stands for Size Match: any operand whose size is not
|
|
|
|
# explicitly specified by the template is `really' intended to be
|
|
|
|
# the same size as the first size-specified operand.
|
|
|
|
# Non-specification is tolerated in the input instruction, but
|
|
|
|
# _wrong_ specification is not.
|
|
|
|
#
|
|
|
|
# IF_SM2 invokes Size Match on only the first _two_ operands, for
|
|
|
|
# three-operand instructions such as SHLD: it implies that the
|
|
|
|
# first two operands must match in size, but that the third is
|
|
|
|
# required to be _unspecified_.
|
|
|
|
#
|
|
|
|
# IF_SB invokes Size Byte: operands with unspecified size in the
|
|
|
|
# template are really bytes, and so no non-byte specification in
|
|
|
|
# the input instruction will be tolerated. IF_SW similarly invokes
|
|
|
|
# Size Word, and IF_SD invokes Size Doubleword.
|
|
|
|
#
|
|
|
|
# (The default state if neither IF_SM nor IF_SM2 is specified is
|
|
|
|
# that any operand with unspecified size in the template is
|
|
|
|
# required to have unspecified size in the instruction too...)
|
|
|
|
#
|
|
|
|
# iflag_t is defined to store these flags.
|
2013-10-27 05:20:42 +08:00
|
|
|
#
|
|
|
|
# The order does matter here. We use some predefined masks to quick test
|
2013-11-25 03:52:32 +08:00
|
|
|
# for a set of flags, so be careful moving bits (and
|
2013-10-27 05:20:42 +08:00
|
|
|
# don't forget to update C code generation then).
|
2013-11-25 03:52:32 +08:00
|
|
|
#
|
2018-02-07 06:43:07 +08:00
|
|
|
sub dword_align($) {
|
|
|
|
my($n) = @_;
|
|
|
|
|
|
|
|
$$n = ($$n + 31) & ~31;
|
|
|
|
return $n;
|
|
|
|
}
|
|
|
|
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2019-08-07 13:05:49 +08:00
|
|
|
my $n_iflags = 0;
|
|
|
|
my %flag_byname;
|
|
|
|
my @flag_bynum;
|
|
|
|
my @flag_fields;
|
2013-11-25 03:49:24 +08:00
|
|
|
my $iflag_words;
|
|
|
|
|
2019-08-07 13:05:49 +08:00
|
|
|
sub if_($$) {
|
|
|
|
my($name, $def) = @_;
|
2019-08-09 17:41:37 +08:00
|
|
|
my $num = $n_iflags++;
|
|
|
|
my $v = [$num, $name, $def];
|
2013-11-25 03:49:24 +08:00
|
|
|
|
2019-08-07 13:05:49 +08:00
|
|
|
$flag_byname{$name} = $v;
|
2019-08-09 17:41:37 +08:00
|
|
|
$flag_bynum[$num] = $v;
|
2019-08-07 13:05:49 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sub if_align($) {
|
|
|
|
my($name) = @_;
|
|
|
|
|
|
|
|
if ($#flag_fields >= 0) {
|
|
|
|
$flag_fields[$#flag_fields]->[2] = $n_iflags-1;
|
|
|
|
}
|
|
|
|
$n_iflags = ($n_iflags + 31) & ~31;
|
|
|
|
|
|
|
|
if (defined($name)) {
|
|
|
|
push(@flag_fields, [$name, $n_iflags, undef]);
|
2013-11-25 03:49:24 +08:00
|
|
|
}
|
|
|
|
|
2019-08-07 13:05:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub if_end() {
|
|
|
|
if_align(undef);
|
|
|
|
$iflag_words = $n_iflags >> 5;
|
2013-11-25 03:49:24 +08:00
|
|
|
}
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2019-08-07 13:05:49 +08:00
|
|
|
# The actual flags defintions
|
|
|
|
require 'x86/iflags.ph';
|
|
|
|
if_end();
|
|
|
|
|
|
|
|
# Compute the combinations of instruction flags actually used in templates
|
|
|
|
|
|
|
|
my %insns_flag_hash = ();
|
|
|
|
my @insns_flag_values = ();
|
2019-08-07 16:11:21 +08:00
|
|
|
my @insns_flag_lists = ();
|
2019-08-07 13:05:49 +08:00
|
|
|
|
2013-10-27 05:20:42 +08:00
|
|
|
sub insns_flag_index(@) {
|
|
|
|
return undef if $_[0] eq "ignore";
|
|
|
|
|
|
|
|
my @prekey = sort(@_);
|
2019-08-07 16:11:21 +08:00
|
|
|
my $key = join(',', @prekey);
|
2019-08-09 17:41:37 +08:00
|
|
|
my $flag_index = $insns_flag_hash{$key};
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2019-08-09 17:41:37 +08:00
|
|
|
unless (defined($flag_index)) {
|
2013-11-25 03:49:24 +08:00
|
|
|
my @newkey = (0) x $iflag_words;
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2019-08-09 17:41:37 +08:00
|
|
|
foreach my $i (@prekey) {
|
|
|
|
my $flag = $flag_byname{$i};
|
|
|
|
die "No key for $i (in $key)\n" if not defined($flag);
|
|
|
|
$newkey[$flag->[0] >> 5] |= (1 << ($flag->[0] & 31));
|
2013-10-27 05:20:42 +08:00
|
|
|
}
|
|
|
|
|
2013-11-25 03:49:24 +08:00
|
|
|
my $str = join(',', map { sprintf("UINT32_C(0x%08x)",$_) } @newkey);
|
2013-12-07 20:15:03 +08:00
|
|
|
|
2013-10-27 05:20:42 +08:00
|
|
|
push @insns_flag_values, $str;
|
2019-08-07 16:11:21 +08:00
|
|
|
push @insns_flag_lists, $key;
|
2019-08-09 17:41:37 +08:00
|
|
|
$insns_flag_hash{$key} = $flag_index = $#insns_flag_values;
|
2013-10-27 05:20:42 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 17:41:37 +08:00
|
|
|
return $flag_index;
|
2013-10-27 05:20:42 +08:00
|
|
|
}
|
|
|
|
|
2013-11-25 03:13:10 +08:00
|
|
|
sub write_iflaggen_h() {
|
2016-05-26 03:06:29 +08:00
|
|
|
print STDERR "Writing $oname...\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2016-05-26 03:06:29 +08:00
|
|
|
open(N, '>', $oname) or die "$0: $!\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
|
|
|
|
print N "/* This file is auto-generated. Don't edit. */\n";
|
2013-11-25 03:13:10 +08:00
|
|
|
print N "#ifndef NASM_IFLAGGEN_H\n";
|
|
|
|
print N "#define NASM_IFLAGGEN_H 1\n\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2019-08-07 13:05:49 +08:00
|
|
|
# The flag numbers; the <= in the loop is intentional
|
|
|
|
|
2018-02-07 06:43:07 +08:00
|
|
|
my $next = 0;
|
2019-08-07 13:05:49 +08:00
|
|
|
for ($i = 0; $i <= $n_iflags; $i++) {
|
|
|
|
if ((defined($flag_bynum[$i]) || $i >= $n_iflags) &&
|
|
|
|
$next != $i) {
|
2018-02-07 06:43:07 +08:00
|
|
|
printf N "%-31s /* %-64s */\n", '',
|
2019-08-07 13:05:49 +08:00
|
|
|
($next < $i-1) ?
|
|
|
|
sprintf("%d...%d reserved", $next-1, $i-1) :
|
|
|
|
sprintf("%d reserved", $i-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($flag_bynum[$i])) {
|
|
|
|
printf N "#define IF_%-16s %3d /* %-64s */\n",
|
|
|
|
$flag_bynum[$i]->[1], $i, $flag_bynum[$i]->[2];
|
|
|
|
$next = $i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print N "\n";
|
|
|
|
|
|
|
|
# The flag masks for individual bits
|
|
|
|
|
|
|
|
$next = 0;
|
|
|
|
for ($i = 0; $i < $n_iflags; $i++) {
|
|
|
|
if (($i & 31) == 0) {
|
|
|
|
printf N "/* Mask bits for field %d : %d...%d */\n",
|
|
|
|
$i >> 5, $i, $i+31;
|
|
|
|
}
|
|
|
|
if (defined(my $v = $flag_bynum[$i])) {
|
|
|
|
printf N "#define IFM_%-15s UINT32_C(0x%08x) /* %3d */\n",
|
|
|
|
$v->[1], 1 << ($i & 31), $i;
|
|
|
|
$next = $i+1;
|
2018-02-07 06:43:07 +08:00
|
|
|
}
|
2013-10-27 05:20:42 +08:00
|
|
|
}
|
2019-08-07 13:05:49 +08:00
|
|
|
print N "\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2019-08-07 13:05:49 +08:00
|
|
|
# The names of fields
|
|
|
|
|
|
|
|
for ($i = 0; $i <= $#flag_fields; $i++) {
|
|
|
|
printf N "#define %-19s %3d /* %-64s */\n",
|
|
|
|
'IF_'.$flag_fields[$i]->[0].'_FIELD',
|
|
|
|
$flag_fields[$i]->[1] >> 5,
|
|
|
|
sprintf("IF_%s (%d) ... IF_%s (%d)",
|
|
|
|
$flag_bynum[$flag_fields[$i]->[1]]->[1],
|
|
|
|
$flag_bynum[$flag_fields[$i]->[1]]->[0],
|
|
|
|
$flag_bynum[$flag_fields[$i]->[2]]->[1],
|
|
|
|
$flag_bynum[$flag_fields[$i]->[2]]->[0]);
|
|
|
|
printf N "#define %-19s %3d\n",
|
|
|
|
'IF_'.$flag_fields[$i]->[0].'_NFIELDS',
|
|
|
|
($flag_fields[$i]->[2] - $flag_fields[$i]->[1] + 31) >> 5;
|
|
|
|
}
|
2013-10-27 05:20:42 +08:00
|
|
|
print N "\n";
|
2019-08-07 13:05:49 +08:00
|
|
|
|
2018-02-07 06:43:07 +08:00
|
|
|
printf N "#define IF_FIELD_COUNT %d\n", $iflag_words;
|
2013-11-25 03:49:24 +08:00
|
|
|
print N "typedef struct {\n";
|
2018-02-07 06:43:07 +08:00
|
|
|
print N " uint32_t field[IF_FIELD_COUNT];\n";
|
2013-11-25 03:49:24 +08:00
|
|
|
print N "} iflag_t;\n";
|
|
|
|
|
|
|
|
print N "\n";
|
2019-08-07 13:05:49 +08:00
|
|
|
print N "/* All combinations of instruction flags used in instruction patterns */\n";
|
2013-11-25 09:13:20 +08:00
|
|
|
printf N "extern const iflag_t insns_flags[%d];\n\n",
|
2013-12-07 20:15:03 +08:00
|
|
|
$#insns_flag_values + 1;
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2013-11-25 03:13:10 +08:00
|
|
|
print N "#endif /* NASM_IFLAGGEN_H */\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
close N;
|
2013-11-25 03:13:10 +08:00
|
|
|
}
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2013-11-25 03:13:10 +08:00
|
|
|
sub write_iflag_c() {
|
2016-05-26 03:06:29 +08:00
|
|
|
print STDERR "Writing $oname...\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
|
2016-05-26 03:06:29 +08:00
|
|
|
open(N, '>', $oname) or die "$0: $!\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
|
|
|
|
print N "/* This file is auto-generated. Don't edit. */\n";
|
|
|
|
print N "#include \"iflag.h\"\n\n";
|
2019-08-07 13:05:49 +08:00
|
|
|
print N "/* All combinations of instruction flags used in instruction patterns */\n";
|
2013-11-25 09:13:20 +08:00
|
|
|
printf N "const iflag_t insns_flags[%d] = {\n",
|
|
|
|
$#insns_flag_values + 1;
|
2013-10-27 05:20:42 +08:00
|
|
|
foreach my $i (0 .. $#insns_flag_values) {
|
2019-08-07 16:11:21 +08:00
|
|
|
printf N " {{%s}}, /* %3d : %s */\n",
|
|
|
|
$insns_flag_values[$i], $i, $insns_flag_lists[$i];
|
2013-10-27 05:20:42 +08:00
|
|
|
}
|
2019-08-07 16:11:21 +08:00
|
|
|
print N "};\n";
|
2013-10-27 05:20:42 +08:00
|
|
|
close N;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|