3 # Generate a perfect hash for token parsing
5 # Usage: tokenhash.pl insns.dat regs.dat tokens.dat
10 my($output, $insns_dat, $regs_dat, $tokens_dat) = @ARGV;
16 # List of condition codes
18 @conditions = ('a', 'ae', 'b', 'be', 'c', 'e', 'g', 'ge', 'l', 'le',
19 'na', 'nae', 'nb', 'nbe', 'nc', 'ne', 'ng', 'nge', 'nl',
20 'nle', 'no', 'np', 'ns', 'nz', 'o', 'p', 'pe', 'po', 's', 'z');
25 open(ID
, "< ${insns_dat}") or die "$0: cannot open $insns_dat: $!\n";
26 while (defined($line = <ID
>)) {
27 if ($line =~ /^([A-Z0-9_]+)(|cc)\s/) {
29 ($token = $1) =~ tr/A-Z/a-z/;
32 # Single instruction token
33 if (!defined($tokens{$token})) {
34 $tokens{$token} = scalar @tokendata;
35 push(@tokendata, "\"${token}\", TOKEN_INSN, C_none, I_${insn}");
38 # Conditional instruction
39 foreach $cc (@conditions) {
40 if (!defined($tokens{$token.$cc})) {
41 $tokens{$token.$cc} = scalar @tokendata;
42 push(@tokendata, "\"${token}${cc}\", TOKEN_INSN, C_\U$cc\E, I_${insn}");
53 open(RD
, "< ${regs_dat}") or die "$0: cannot open $regs_dat: $!\n";
54 while (defined($line = <RD
>)) {
55 if ($line =~ /^([a-z0-9_-]+)\s/) {
58 if ($reg =~ /^(.*[^0-9])([0-9]+)\-([0-9]+)(|[^0-9].*)$/) {
66 undef $reg_prefix, $reg_suffix;
70 if (defined($tokens{$reg})) {
71 die "Duplicate definition: $reg\n";
73 $tokens{$reg} = scalar @tokendata;
74 push(@tokendata, "\"${reg}\", TOKEN_REG, 0, R_\U${reg}\E");
76 if (defined($reg_prefix)) {
78 $reg = sprintf("%s%u%s", $reg_prefix, $reg_nr, $reg_suffix);
80 # Not a dashed sequence
91 open(TD
, "< ${tokens_dat}") or die "$0: cannot open $tokens_dat: $!\n";
92 while (defined($line = <TD
>)) {
93 if ($line =~ /^\%\s+(.*)$/) {
95 } elsif ($line =~ /^([a-z0-9_-]+)/) {
98 if (defined($tokens{$token})) {
99 die "Duplicate definition: $token\n";
101 $tokens{$token} = scalar @tokendata;
104 if ($data =~ /^(.*)\{(.*)\}(.*)$/) {
105 my $head = $1, $tail = $3;
109 if ($token =~ /$px/i) {
110 $data = $head."\U$1".$tail;
112 die "$0: token $token doesn't match $px\n";
116 $data =~ s/\*/\U$token/g;
118 push(@tokendata, "\"$token\", $data");
123 if ($output eq 'h') {
129 foreach $token (keys(%tokens)) {
130 if (length($token) > $max_len) {
131 $max_len = length($token);
136 print " * This file is generated from insns.dat, regs.dat and token.dat\n";
137 print " * by tokhash.pl; do not edit.\n";
141 print "#ifndef NASM_TOKENS_H\n";
142 print "#define NASM_TOKENS_H\n";
144 print "#define MAX_KEYWORD $max_len /* length of longest keyword */\n";
146 print "#endif /* NASM_TOKENS_H */\n";
147 } elsif ($output eq 'c') {
152 @hashinfo = gen_perfect_hash
(\
%tokens);
153 if (!defined(@hashinfo)) {
154 die "$0: no hash found\n";
158 verify_hash_table
(\
%tokens, \
@hashinfo);
160 ($n, $sv, $g) = @hashinfo;
163 die if ($n & ($n-1));
166 print " * This file is generated from insns.dat, regs.dat and token.dat\n";
167 print " * by tokhash.pl; do not edit.\n";
171 print "#include \"compiler.h\"\n";
172 print "#include <string.h>\n";
173 print "#include \"nasm.h\"\n";
174 print "#include \"hashtbl.h\"\n";
175 print "#include \"insns.h\"\n";
178 # These somewhat odd sizes and ordering thereof are due to the
179 # relative ranges of the types; this makes it fit in 16 bytes on
180 # 64-bit machines and 12 bytes on 32-bit machines.
181 print "struct tokendata {\n";
182 print " const char *string;\n";
183 print " int16_t tokentype;\n";
184 print " int16_t aux;\n";
185 print " int32_t num;\n";
189 print "int nasm_token_hash(const char *token, struct tokenval *tv)\n";
192 # Put a large value in unused slots. This makes it extremely unlikely
193 # that any combination that involves unused slot will pass the range test.
194 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
195 print "#define UNUSED 16383\n";
197 print " static const int16_t hash1[$n] = {\n";
198 for ($i = 0; $i < $n; $i++) {
199 my $h = ${$g}[$i*2+0];
200 print " ", defined($h) ?
$h : 'UNUSED', ",\n";
204 print " static const int16_t hash2[$n] = {\n";
205 for ($i = 0; $i < $n; $i++) {
206 my $h = ${$g}[$i*2+1];
207 print " ", defined($h) ?
$h : 'UNUSED', ",\n";
211 printf " static const struct tokendata tokendata[%d] = {\n", scalar(@tokendata);
212 foreach $d (@tokendata) {
213 print " { ", $d, " },\n";
217 print " uint32_t k1, k2;\n";
218 print " uint64_t crc;\n";
219 # For correct overflow behavior, "ix" should be unsigned of the same
220 # width as the hash arrays.
221 print " uint16_t ix;\n";
222 print " const struct tokendata *data;\n";
224 printf " crc = crc64(UINT64_C(0x%08x%08x), token);\n",
226 print " k1 = (uint32_t)crc;\n";
227 print " k2 = (uint32_t)(crc >> 32);\n";
229 printf " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
230 printf " if (ix >= %d)\n", scalar(@tokendata);
231 print " return tv->t_type = TOKEN_ID;\n";
233 print " data = &tokendata[ix];\n";
235 print " if (strcmp(data->string, token))\n";
236 print " return tv->t_type = TOKEN_ID;\n";
238 print " tv->t_integer = data->num;\n";
239 print " tv->t_inttwo = data->aux;\n";
240 print " return tv->t_type = data->tokentype;\n";