2 ## --------------------------------------------------------------------------
4 ## Copyright 1996-2011 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following
10 ## conditions are met:
12 ## * Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## * Redistributions in binary form must reproduce the above
15 ## copyright notice, this list of conditions and the following
16 ## disclaimer in the documentation and/or other materials provided
17 ## with the distribution.
19 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ## --------------------------------------------------------------------------
36 # Produce prtok.c, prtok.h and prtok.ph from prtok.dat
41 my($what, $in, $out) = @ARGV;
46 open(IN
, "< $in") or die "$0: cannot open: $in\n";
47 while (defined($line = <IN
>)) {
49 $line =~ s/^\s+//; # Remove leading whitespace
50 $line =~ s/\s*\#.*$//; # Remove comments and trailing whitespace
51 next if ($line eq '');
53 if ($line =~ /^(.*)$/) {
61 open(OUT
, "> $out") or die "$0: cannot open: $out\n";
67 print OUT
"/* Automatically generated from $in by $0 */\n";
68 print OUT
"/* Do not edit */\n";
71 print OUT
"enum pragma_token {\n";
73 foreach $pt (@prtok) {
75 printf OUT
" %-16s = %3d,\n", "PR_\U$pt\E", $n;
79 printf OUT
" %-16s = %3d\n", 'PR_INVALID', -1;
88 print OUT
"/* Automatically generated from $in by $0 */\n";
89 print OUT
"/* Do not edit */\n";
96 foreach $pt (@prtok) {
98 $tokens{'%'.$pt} = $n;
99 if ($pt =~ /[\@\[\]\\_]/) {
100 # Fail on characters which look like upper-case letters
101 # to the quick-and-dirty downcasing in the prehash
103 die "$in: invalid character in token: $pt";
109 my @hashinfo = gen_perfect_hash
(\
%tokens);
111 die "$0: no hash found\n";
115 verify_hash_table
(\
%tokens, \
@hashinfo);
117 ($n, $sv, $g) = @hashinfo;
120 die if ($n & ($n-1));
122 print OUT
"#include \"compiler.h\"\n";
123 print OUT
"#include <inttypes.h>\n";
124 print OUT
"#include <ctype.h>\n";
125 print OUT
"#include \"nasmlib.h\"\n";
126 print OUT
"#include \"hashtbl.h\"\n";
127 print OUT
"#include \"preproc.h\"\n";
130 # Note that this is global.
131 printf OUT
"const char * const pr_directives[%d] = {\n", scalar(@prtok);
132 foreach $d (@prtok) {
134 print OUT
" \"$d\",\n";
136 print OUT
" NULL,\n";
141 printf OUT
"const uint8_t pr_directives_len[%d] = {\n", scalar(@prtok);
142 foreach $d (@prtok) {
143 printf OUT
" %d,\n", defined($d) ?
length($d)+1 : 0;
147 print OUT
"enum pragma_token pr_token_hash(const char *token)\n";
150 # Put a large value in unused slots. This makes it extremely unlikely
151 # that any combination that involves unused slot will pass the range test.
152 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
153 print OUT
"#define UNUSED 16383\n";
155 print OUT
" static const int16_t hash1[$n] = {\n";
156 for ($i = 0; $i < $n; $i++) {
157 my $h = ${$g}[$i*2+0];
158 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
162 print OUT
" static const int16_t hash2[$n] = {\n";
163 for ($i = 0; $i < $n; $i++) {
164 my $h = ${$g}[$i*2+1];
165 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
169 print OUT
" uint32_t k1, k2;\n";
170 print OUT
" uint64_t crc;\n";
171 # For correct overflow behavior, "ix" should be unsigned of the same
172 # width as the hash arrays.
173 print OUT
" uint16_t ix;\n";
176 printf OUT
" crc = crc64i(UINT64_C(0x%08x%08x), token);\n",
178 print OUT
" k1 = (uint32_t)crc;\n";
179 print OUT
" k2 = (uint32_t)(crc >> 32);\n";
181 printf OUT
" ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
182 printf OUT
" if (ix >= %d)\n", scalar(@prtok);
183 print OUT
" return PR_INVALID;\n";
186 print OUT
" if (!pr_directives[ix] || nasm_stricmp(pr_directives[ix], token))\n";
187 print OUT
" return PR_INVALID;\n";
189 print OUT
" return ix;\n";
197 print OUT
"# Automatically generated from $in by $0\n";
198 print OUT
"# Do not edit\n";
201 print OUT
"%prtok_hash = (\n";
203 foreach $tok (@prtok) {
205 printf OUT
" '%%%s' => %d,\n", $tok, $n;