CLFLUSH: Neither an x64 instruction nor AMD
[nasm.git] / tokhash.pl
blob5f1a9f4c8632cfeb4087edbfaaaac0601be46939
1 #!/usr/bin/perl
3 # Generate a perfect hash for token parsing
5 # Usage: tokenhash.pl insns.dat regs.dat tokens.dat
8 require 'phash.ph';
10 my($insns_dat, $regs_dat, $tokens_dat) = @ARGV;
12 %tokens = ();
13 @tokendata = ();
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');
23 # Read insns.dat
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/) {
28 $insn = $1.$2;
29 ($token = $1) =~ tr/A-Z/a-z/;
31 if ($2 eq '') {
32 # Single instruction token
33 if (!defined($tokens{$token})) {
34 $tokens{$token} = scalar @tokendata;
35 push(@tokendata, "\"${token}\", TOKEN_INSN, C_none, I_${insn}");
37 } else {
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}");
48 close(ID);
51 # Read regs.dat
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/) {
56 $reg = $1;
58 if ($reg =~ /^(.*[^0-9])([0-9]+)\-([0-9]+)(|[^0-9].*)$/) {
59 $nregs = $3-$2+1;
60 $reg = $1.$2.$4;
61 $reg_nr = $2;
62 $reg_prefix = $1;
63 $reg_suffix = $4;
64 } else {
65 $nregs = 1;
66 undef $reg_prefix, $reg_suffix;
69 while ($nregs--) {
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)) {
77 $reg_nr++;
78 $reg = sprintf("%s%u%s", $reg_prefix, $reg_nr, $reg_suffix);
79 } else {
80 # Not a dashed sequence
81 die if ($nregs);
86 close(RD);
89 # Read tokens.dat
91 open(TD, "< ${tokens_dat}") or die "$0: cannot open $tokens_dat: $!\n";
92 while (defined($line = <TD>)) {
93 if ($line =~ /^\%\s+(.*)$/) {
94 $pattern = $1;
95 } elsif ($line =~ /^([a-z0-9_-]+)/) {
96 $token = $1;
98 if (defined($tokens{$token})) {
99 die "Duplicate definition: $token\n";
101 $tokens{$token} = scalar @tokendata;
103 $data = $pattern;
104 $data =~ s/\*/\U$token/g;
106 push(@tokendata, "\"$token\", $data");
109 close(TD);
112 # Actually generate the hash
114 @hashinfo = gen_perfect_hash(\%tokens);
115 if (!defined(@hashinfo)) {
116 die "$0: no hash found\n";
119 # Paranoia...
120 verify_hash_table(\%tokens, \@hashinfo);
122 ($n, $sv, $g) = @hashinfo;
123 $sv2 = $sv+2;
125 die if ($n & ($n-1));
127 print "/*\n";
128 print " * This file is generated from insns.dat, regs.dat and token.dat\n";
129 print " * by tokhash.pl; do not edit.\n";
130 print " */\n";
131 print "\n";
133 print "#include <string.h>\n";
134 print "#include \"nasm.h\"\n";
135 print "#include \"insns.h\"\n";
136 print "\n";
138 print "#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
139 print "\n";
141 # These somewhat odd sizes and ordering thereof are due to the
142 # relative ranges of the types; this makes it fit in 16 bytes on
143 # 64-bit machines and 12 bytes on 32-bit machines.
144 print "struct tokendata {\n";
145 print " const char *string;\n";
146 print " int16_t tokentype;\n";
147 print " int16_t aux;\n";
148 print " int32_t num;\n";
149 print "};\n";
150 print "\n";
152 print "int nasm_token_hash(const char *token, struct tokenval *tv)\n";
153 print "{\n";
155 # Put a large value in unused slots. This makes it extremely unlikely
156 # that any combination that involves unused slot will pass the range test.
157 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
158 print "#define UNUSED 16383\n";
160 print " static const int16_t hash1[$n] = {\n";
161 for ($i = 0; $i < $n; $i++) {
162 my $h = ${$g}[$i*2+0];
163 print " ", defined($h) ? $h : 'UNUSED', ",\n";
165 print " };\n";
167 print " static const int16_t hash2[$n] = {\n";
168 for ($i = 0; $i < $n; $i++) {
169 my $h = ${$g}[$i*2+1];
170 print " ", defined($h) ? $h : 'UNUSED', ",\n";
172 print " };\n";
174 printf " static const struct tokendata tokendata[%d] = {\n", scalar(@tokendata);
175 foreach $d (@tokendata) {
176 print " { ", $d, " },\n";
178 print " };\n";
180 print " uint32_t k1 = 0, k2 = 0;\n";
181 print " uint8_t c;\n";
182 # For correct overflow behavior, "ix" should be unsigned of the same
183 # width as the hash arrays.
184 print " uint16_t ix;\n";
185 print " const struct tokendata *data;\n";
186 print " const char *p = token;\n";
187 print "\n";
189 print " while ((c = *p++) != 0) {\n";
190 printf " uint32_t kn1 = rot(k1,%2d) - rot(k2,%2d) + c;\n", ${$sv}[0], ${$sv}[1];
191 printf " uint32_t kn2 = rot(k2,%2d) - rot(k1,%2d) + c;\n", ${$sv}[2], ${$sv}[3];
192 print " k1 = kn1; k2 = kn2;\n";
193 print " }\n";
194 print "\n";
195 printf " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
196 printf " if (ix >= %d)\n", scalar(@tokendata);
197 print " return -1;\n";
198 print "\n";
199 print " data = &tokendata[ix];\n";
201 # print " fprintf(stderr, \"Looked for: %s found: %s\\n\", token, data->string);\n\n";
203 print " if (strcmp(data->string, token))\n";
204 print " return -1;\n";
205 print "\n";
206 print " tv->t_integer = data->num;\n";
207 print " tv->t_inttwo = data->aux;\n";
208 print " return tv->t_type = data->tokentype;\n";
209 print "}\n";