2007-08-31 05:45:56 +08:00
|
|
|
#!/usr/bin/perl
|
2009-06-29 07:52:56 +08:00
|
|
|
## --------------------------------------------------------------------------
|
|
|
|
##
|
2018-12-14 16:50:34 +08:00
|
|
|
## Copyright 1996-2018 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.
|
|
|
|
##
|
|
|
|
## 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';
|
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
my($output, $insns_dat, $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');
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read insns.dat
|
|
|
|
#
|
2017-04-03 10:28:13 +08:00
|
|
|
open(ID, '<', $insns_dat) or die "$0: cannot open $insns_dat: $!\n";
|
2007-08-31 05:45:56 +08:00
|
|
|
while (defined($line = <ID>)) {
|
|
|
|
if ($line =~ /^([A-Z0-9_]+)(|cc)\s/) {
|
|
|
|
$insn = $1.$2;
|
|
|
|
($token = $1) =~ tr/A-Z/a-z/;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-08-31 05:45:56 +08:00
|
|
|
if ($2 eq '') {
|
|
|
|
# Single instruction token
|
|
|
|
if (!defined($tokens{$token})) {
|
|
|
|
$tokens{$token} = scalar @tokendata;
|
2018-12-30 12:14:50 +08:00
|
|
|
push(@tokendata, "\"${token}\", ".length($token).
|
|
|
|
", TOKEN_INSN, C_none, 0, I_${insn}");
|
2007-08-31 05:45:56 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
# Conditional instruction
|
|
|
|
foreach $cc (@conditions) {
|
2018-12-30 12:14:50 +08:00
|
|
|
my $etok = $token.$cc;
|
|
|
|
if (!defined($tokens{$etok})) {
|
|
|
|
$tokens{$etok} = scalar @tokendata;
|
|
|
|
push(@tokendata, "\"${etok}\", ".length($etok).
|
|
|
|
", TOKEN_INSN, C_\U$cc\E, 0, I_${insn}");
|
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>)) {
|
2013-08-06 11:46:18 +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>)) {
|
|
|
|
if ($line =~ /^\%\s+(.*)$/) {
|
|
|
|
$pattern = $1;
|
2018-12-14 16:50:34 +08:00
|
|
|
} elsif ($line =~ /^([\?\@\.a-z0-9_-]+)/) {
|
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
|
|
|
|
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;
|
|
|
|
|
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));
|
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.
|
2016-01-30 04:05:27 +08:00
|
|
|
print "#define UNUSED (65535/3)\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
print " static const int16_t hash1[$n] = {\n";
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
my $h = ${$g}[$i*2+0];
|
|
|
|
print " ", defined($h) ? $h : 'UNUSED', ",\n";
|
|
|
|
}
|
|
|
|
print " };\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
print " static const int16_t hash2[$n] = {\n";
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
my $h = ${$g}[$i*2+1];
|
|
|
|
print " ", defined($h) ? $h : 'UNUSED', ",\n";
|
|
|
|
}
|
|
|
|
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";
|
|
|
|
print " uint64_t crc;\n";
|
2018-12-30 12:14:50 +08:00
|
|
|
print " size_t len;\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";
|
|
|
|
print "\n";
|
2018-12-30 12:14:50 +08:00
|
|
|
print " len = strlen(token);\n";
|
|
|
|
print " if (unlikely(len > $max_len))\n";
|
|
|
|
print " goto notfound;\n";
|
|
|
|
print "\n";
|
|
|
|
printf " crc = crc64b(UINT64_C(0x%08x%08x), token, len);\n",
|
2007-10-20 05:42:29 +08:00
|
|
|
$$sv[0], $$sv[1];
|
2007-10-03 08:40:00 +08:00
|
|
|
print " k1 = (uint32_t)crc;\n";
|
|
|
|
print " k2 = (uint32_t)(crc >> 32);\n";
|
2007-09-25 03:30:54 +08:00
|
|
|
print "\n";
|
|
|
|
printf " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
|
|
|
|
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";
|
2018-12-30 12:14:50 +08:00
|
|
|
print " if (data->len != len || memcmp(data->string, token, len))\n";
|
|
|
|
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
|
|
|
}
|