2007-08-31 05:45:56 +08:00
|
|
|
#!/usr/bin/perl
|
2009-06-29 07:52:56 +08:00
|
|
|
## --------------------------------------------------------------------------
|
2019-09-24 07:40:03 +08:00
|
|
|
##
|
2020-07-11 10:26:52 +08:00
|
|
|
## Copyright 1996-2020 The NASM Authors - All Rights Reserved
|
2009-06-29 07:52:56 +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.
|
2019-09-24 07:40:03 +08:00
|
|
|
##
|
2009-06-29 07:52:56 +08:00
|
|
|
## 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.
|
|
|
|
##
|
|
|
|
## --------------------------------------------------------------------------
|
|
|
|
|
2007-08-31 05:45:56 +08:00
|
|
|
#
|
|
|
|
# Generate a perfect hash for token parsing
|
|
|
|
#
|
|
|
|
# Usage: tokenhash.pl insns.dat regs.dat tokens.dat
|
|
|
|
#
|
|
|
|
|
|
|
|
require 'phash.ph';
|
|
|
|
|
2022-11-13 04:26:28 +08:00
|
|
|
my($output, $insnsn_c, $regs_dat, $tokens_dat) = @ARGV;
|
2007-08-31 05:45:56 +08:00
|
|
|
|
|
|
|
%tokens = ();
|
|
|
|
@tokendata = ();
|
|
|
|
|
|
|
|
#
|
|
|
|
# List of condition codes
|
|
|
|
#
|
|
|
|
@conditions = ('a', 'ae', 'b', 'be', 'c', 'e', 'g', 'ge', 'l', 'le',
|
|
|
|
'na', 'nae', 'nb', 'nbe', 'nc', 'ne', 'ng', 'nge', 'nl',
|
|
|
|
'nle', 'no', 'np', 'ns', 'nz', 'o', 'p', 'pe', 'po', 's', 'z');
|
|
|
|
|
|
|
|
#
|
2022-11-13 04:26:28 +08:00
|
|
|
# Read insnsn.c
|
2007-08-31 05:45:56 +08:00
|
|
|
#
|
2022-11-13 04:26:28 +08:00
|
|
|
open(ID, '<', $insnsn_c) or die "$0: cannot open $insnsn_c: $!\n";
|
2007-08-31 05:45:56 +08:00
|
|
|
while (defined($line = <ID>)) {
|
2022-11-13 04:26:28 +08:00
|
|
|
next unless ($line =~ /^\s*\"([\?\@a-z0-9_]+)\"/);
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2022-11-13 04:26:28 +08:00
|
|
|
my $token = $1;
|
|
|
|
next if (defined($tokens{$token})); # This should never happen
|
|
|
|
|
|
|
|
$tokens{$token} = scalar @tokendata;
|
|
|
|
push(@tokendata, "\"${token}\", ".length($token).
|
|
|
|
", TOKEN_INSN, 0, 0, I_\U${token}");
|
2007-08-31 05:45:56 +08:00
|
|
|
}
|
|
|
|
close(ID);
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read regs.dat
|
|
|
|
#
|
2017-04-03 10:28:13 +08:00
|
|
|
open(RD, '<', $regs_dat) or die "$0: cannot open $regs_dat: $!\n";
|
2007-08-31 05:45:56 +08:00
|
|
|
while (defined($line = <RD>)) {
|
2019-08-28 07:38:48 +08:00
|
|
|
if ($line =~ /^([\?\@a-z0-9_-]+)\s*\S+\s*\S+\s*[0-9]+\s*(\S*)/) {
|
2007-08-31 05:45:56 +08:00
|
|
|
$reg = $1;
|
2013-08-06 11:46:18 +08:00
|
|
|
$reg_flag = $2;
|
2007-08-31 05:45:56 +08:00
|
|
|
|
|
|
|
if ($reg =~ /^(.*[^0-9])([0-9]+)\-([0-9]+)(|[^0-9].*)$/) {
|
|
|
|
$nregs = $3-$2+1;
|
|
|
|
$reg = $1.$2.$4;
|
|
|
|
$reg_nr = $2;
|
|
|
|
$reg_prefix = $1;
|
2007-10-20 05:42:29 +08:00
|
|
|
$reg_suffix = $4;
|
2007-08-31 05:45:56 +08:00
|
|
|
} else {
|
|
|
|
$nregs = 1;
|
2019-08-10 04:30:19 +08:00
|
|
|
undef $reg_prefix;
|
|
|
|
undef $reg_suffix;
|
2007-08-31 05:45:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while ($nregs--) {
|
|
|
|
if (defined($tokens{$reg})) {
|
|
|
|
die "Duplicate definition: $reg\n";
|
|
|
|
}
|
|
|
|
$tokens{$reg} = scalar @tokendata;
|
2018-12-30 12:14:50 +08:00
|
|
|
$reg_flag = '0' if ($reg_flag eq '');
|
|
|
|
push(@tokendata, "\"${reg}\", ".length($reg).", TOKEN_REG, 0, ${reg_flag}, R_\U${reg}\E");
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-08-31 05:45:56 +08:00
|
|
|
if (defined($reg_prefix)) {
|
|
|
|
$reg_nr++;
|
|
|
|
$reg = sprintf("%s%u%s", $reg_prefix, $reg_nr, $reg_suffix);
|
|
|
|
} else {
|
|
|
|
# Not a dashed sequence
|
|
|
|
die if ($nregs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(RD);
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read tokens.dat
|
|
|
|
#
|
2017-04-03 10:28:13 +08:00
|
|
|
open(TD, '<', $tokens_dat) or die "$0: cannot open $tokens_dat: $!\n";
|
2007-08-31 05:45:56 +08:00
|
|
|
while (defined($line = <TD>)) {
|
2019-09-24 07:40:03 +08:00
|
|
|
$line =~ s/\s*(|\#.*)$//;
|
2007-08-31 05:45:56 +08:00
|
|
|
if ($line =~ /^\%\s+(.*)$/) {
|
|
|
|
$pattern = $1;
|
2019-09-24 07:40:03 +08:00
|
|
|
} elsif ($line =~ /^(\S+)/) {
|
2007-08-31 05:45:56 +08:00
|
|
|
$token = $1;
|
|
|
|
|
2007-09-11 02:59:01 +08:00
|
|
|
if (defined($tokens{$token})) {
|
2007-08-31 05:45:56 +08:00
|
|
|
die "Duplicate definition: $token\n";
|
|
|
|
}
|
|
|
|
$tokens{$token} = scalar @tokendata;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-08-31 05:45:56 +08:00
|
|
|
$data = $pattern;
|
2007-09-25 01:50:23 +08:00
|
|
|
if ($data =~ /^(.*)\{(.*)\}(.*)$/) {
|
|
|
|
my $head = $1, $tail = $3;
|
|
|
|
my $px = $2;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2019-08-28 07:38:48 +08:00
|
|
|
$px =~ s/\?/\\?/g;
|
2007-09-25 01:50:23 +08:00
|
|
|
$px =~ s/\*/(.*)/g;
|
|
|
|
if ($token =~ /$px/i) {
|
|
|
|
$data = $head."\U$1".$tail;
|
|
|
|
} else {
|
|
|
|
die "$0: token $token doesn't match $px\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-31 05:45:56 +08:00
|
|
|
$data =~ s/\*/\U$token/g;
|
2019-08-28 07:38:48 +08:00
|
|
|
$data =~ s/\?//g;
|
2007-08-31 05:45:56 +08:00
|
|
|
|
2018-12-30 12:14:50 +08:00
|
|
|
push(@tokendata, "\"$token\", ".length($token).", $data");
|
2007-08-31 05:45:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(TD);
|
|
|
|
|
2018-12-30 12:14:50 +08:00
|
|
|
$max_len = 0;
|
|
|
|
foreach $token (keys(%tokens)) {
|
|
|
|
if (length($token) > $max_len) {
|
|
|
|
$max_len = length($token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
if ($output eq 'h') {
|
|
|
|
#
|
2008-05-28 05:43:48 +08:00
|
|
|
# tokens.h
|
2007-09-25 03:30:54 +08:00
|
|
|
#
|
2007-08-31 05:45:56 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
print "/*\n";
|
|
|
|
print " * This file is generated from insns.dat, regs.dat and token.dat\n";
|
|
|
|
print " * by tokhash.pl; do not edit.\n";
|
|
|
|
print " */\n";
|
|
|
|
print "\n";
|
|
|
|
|
|
|
|
print "#ifndef NASM_TOKENS_H\n";
|
|
|
|
print "#define NASM_TOKENS_H\n";
|
|
|
|
print "\n";
|
|
|
|
print "#define MAX_KEYWORD $max_len /* length of longest keyword */\n";
|
|
|
|
print "\n";
|
|
|
|
print "#endif /* NASM_TOKENS_H */\n";
|
|
|
|
} elsif ($output eq 'c') {
|
|
|
|
#
|
|
|
|
# tokhash.c
|
|
|
|
#
|
|
|
|
|
|
|
|
@hashinfo = gen_perfect_hash(\%tokens);
|
2010-11-08 00:20:23 +08:00
|
|
|
if (!@hashinfo) {
|
2007-09-25 03:30:54 +08:00
|
|
|
die "$0: no hash found\n";
|
|
|
|
}
|
2007-08-31 05:45:56 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
# Paranoia...
|
|
|
|
verify_hash_table(\%tokens, \@hashinfo);
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
($n, $sv, $g) = @hashinfo;
|
|
|
|
die if ($n & ($n-1));
|
2020-07-11 10:26:52 +08:00
|
|
|
$n <<= 1;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
print "/*\n";
|
|
|
|
print " * This file is generated from insns.dat, regs.dat and token.dat\n";
|
|
|
|
print " * by tokhash.pl; do not edit.\n";
|
|
|
|
print " */\n";
|
|
|
|
print "\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
print "#include \"compiler.h\"\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print "#include \"nasm.h\"\n";
|
2007-10-03 08:40:00 +08:00
|
|
|
print "#include \"hashtbl.h\"\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print "#include \"insns.h\"\n";
|
2014-11-26 04:10:53 +08:00
|
|
|
print "#include \"stdscan.h\"\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print "\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
# These somewhat odd sizes and ordering thereof are due to the
|
|
|
|
# relative ranges of the types; this makes it fit in 16 bytes on
|
|
|
|
# 64-bit machines and 12 bytes on 32-bit machines.
|
|
|
|
print "struct tokendata {\n";
|
|
|
|
print " const char *string;\n";
|
2018-12-30 12:14:50 +08:00
|
|
|
print " uint16_t len;\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print " int16_t tokentype;\n";
|
2018-12-30 12:14:50 +08:00
|
|
|
print " int16_t aux;\n";
|
|
|
|
print " uint16_t tokflag;\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print " int32_t num;\n";
|
|
|
|
print "};\n";
|
|
|
|
print "\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
print "int nasm_token_hash(const char *token, struct tokenval *tv)\n";
|
|
|
|
print "{\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
# Put a large value in unused slots. This makes it extremely unlikely
|
|
|
|
# that any combination that involves unused slot will pass the range test.
|
|
|
|
# This speeds up rejection of unrecognized tokens, i.e. identifiers.
|
2020-07-10 16:25:22 +08:00
|
|
|
print "#define INVALID_HASH_ENTRY (65535/3)\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2020-07-11 10:26:52 +08:00
|
|
|
printf " static const int16_t hashdata[%d] = {\n", $n;
|
2007-09-25 03:30:54 +08:00
|
|
|
for ($i = 0; $i < $n; $i++) {
|
2020-07-11 10:26:52 +08:00
|
|
|
my $h = ${$g}[$i];
|
2020-07-10 16:25:22 +08:00
|
|
|
print " ", defined($h) ? $h : 'INVALID_HASH_ENTRY', ",\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
}
|
|
|
|
print " };\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2018-12-30 12:14:50 +08:00
|
|
|
printf " static const struct tokendata tokendata[%d] = {\n",
|
|
|
|
scalar(@tokendata);
|
2007-09-25 03:30:54 +08:00
|
|
|
foreach $d (@tokendata) {
|
|
|
|
print " { ", $d, " },\n";
|
|
|
|
}
|
|
|
|
print " };\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-10-03 08:40:00 +08:00
|
|
|
print " uint32_t k1, k2;\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
# For correct overflow behavior, "ix" should be unsigned of the same
|
|
|
|
# width as the hash arrays.
|
|
|
|
print " uint16_t ix;\n";
|
|
|
|
print " const struct tokendata *data;\n";
|
2019-09-24 07:40:03 +08:00
|
|
|
printf " char lcbuf[%d];\n", $max_len+1;
|
|
|
|
print " const char *p = token;\n";
|
|
|
|
print " char c, *q = lcbuf;\n";
|
|
|
|
print " size_t len = 0;\n";
|
|
|
|
printf " uint64_t crc = UINT64_C(0x%08x%08x);\n", $$sv[0], $$sv[1];
|
2007-09-25 03:30:54 +08:00
|
|
|
print "\n";
|
2019-09-24 07:40:03 +08:00
|
|
|
print " while ((c = *p++)) {\n";
|
|
|
|
printf " if (++len > %d)\n", $max_len;
|
|
|
|
print " goto notfound;\n";
|
|
|
|
print " *q++ = c = nasm_tolower(c);\n";
|
|
|
|
print " crc = crc64_byte(crc, c);\n";
|
|
|
|
print " };\n";
|
2018-12-30 12:14:50 +08:00
|
|
|
print "\n";
|
2020-07-11 10:26:52 +08:00
|
|
|
printf " k1 = ((uint32_t)crc & 0x%x) + 0;\n", $n-2;
|
|
|
|
printf " k2 = ((uint32_t)(crc >> 32) & 0x%x) + 1;\n", $n-2;
|
2007-09-25 03:30:54 +08:00
|
|
|
print "\n";
|
2020-07-11 10:26:52 +08:00
|
|
|
printf " ix = hashdata[k1] + hashdata[k2];\n",
|
|
|
|
$n-2, $n-2;
|
2007-09-25 03:30:54 +08:00
|
|
|
printf " if (ix >= %d)\n", scalar(@tokendata);
|
2018-12-30 12:14:50 +08:00
|
|
|
print " goto notfound;\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print "\n";
|
|
|
|
print " data = &tokendata[ix];\n";
|
2019-09-24 07:40:03 +08:00
|
|
|
print " if (data->len != len)\n";
|
|
|
|
print " goto notfound;\n";
|
|
|
|
print " if (memcmp(data->string, lcbuf, len))\n";
|
2018-12-30 12:14:50 +08:00
|
|
|
print " goto notfound;\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print "\n";
|
|
|
|
print " tv->t_integer = data->num;\n";
|
|
|
|
print " tv->t_inttwo = data->aux;\n";
|
2013-08-06 11:46:18 +08:00
|
|
|
print " tv->t_flag = data->tokflag;\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print " return tv->t_type = data->tokentype;\n";
|
2018-12-30 12:14:50 +08:00
|
|
|
print "\n";
|
|
|
|
print "notfound:\n";
|
|
|
|
print " tv->t_integer = 0;\n";
|
|
|
|
print " tv->t_inttwo = 0;\n";
|
|
|
|
print " tv->t_flag = 0;\n";
|
|
|
|
print " return tv->t_type = TOKEN_ID;\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print "}\n";
|
2007-08-31 05:45:56 +08:00
|
|
|
}
|