Update documentation
[platform/upstream/nasm.git] / tokhash.pl
1 #!/usr/bin/perl
2 #
3 # Generate a perfect hash for token parsing
4 #
5 # Usage: tokenhash.pl insns.dat regs.dat tokens.dat
6 #
7
8 require 'phash.ph';
9
10 my($insns_dat, $regs_dat, $tokens_dat) = @ARGV;
11
12 %tokens = ();
13 @tokendata = ();
14
15 #
16 # List of condition codes
17 #
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');
21
22 #
23 # Read insns.dat
24 #
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/;
30         
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}");
36             }
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}");
43                 }
44             }
45         }
46     }
47 }
48 close(ID);
49
50 #
51 # Read regs.dat
52 #
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;
57
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;
67         }
68
69         while ($nregs--) {
70             if (defined($tokens{$reg})) {
71                 die "Duplicate definition: $reg\n";
72             }
73             $tokens{$reg} = scalar @tokendata;
74             push(@tokendata, "\"${reg}\", TOKEN_REG, 0, R_\U${reg}\E");
75         
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);
82             }
83         }
84     }
85 }
86 close(RD);
87
88 #
89 # Read tokens.dat
90 #
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;
97
98         if (defined($tokens{$token})) {
99             die "Duplicate definition: $token\n";
100         }
101         $tokens{$token} = scalar @tokendata;
102         
103         $data = $pattern;
104         $data =~ s/\*/\U$token/g;
105
106         push(@tokendata, "\"$token\", $data");
107     }
108 }
109 close(TD);
110
111 #
112 # Actually generate the hash
113 #
114 @hashinfo = gen_perfect_hash(\%tokens);
115 if (!defined(@hashinfo)) {
116     die "$0: no hash found\n";
117 }
118
119 # Paranoia...
120 verify_hash_table(\%tokens, \@hashinfo);
121
122 ($n, $sv, $g) = @hashinfo;
123 $sv2 = $sv+2;
124
125 die if ($n & ($n-1));
126
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";
132
133 print "#include <string.h>\n";
134 print "#include \"nasm.h\"\n";
135 print "#include \"insns.h\"\n";
136 print "\n";
137
138 print "#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
139 print "\n";
140
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";
151
152 print "int nasm_token_hash(const char *token, struct tokenval *tv)\n";
153 print "{\n";
154
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";
159
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";
164 }
165 print "    };\n";
166
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";
171 }
172 print "    };\n";
173
174 printf "    static const struct tokendata tokendata[%d] = {\n", scalar(@tokendata);
175 foreach $d (@tokendata) {
176     print "        { ", $d, " },\n";
177 }
178 print  "    };\n";
179
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";
188
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";
200
201 # print  "    fprintf(stderr, \"Looked for: %s found: %s\\n\", token, data->string);\n\n";
202
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";