initial branch of %pragma support
[nasm.git] / prtok.pl
blob3de5284bbc18e231145c5aefbe5685a1ee3ec08a
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
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.
7 ##
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.
18 ##
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
39 require 'phash.ph';
41 my($what, $in, $out) = @ARGV;
44 # Read prtok.dat
46 open(IN, "< $in") or die "$0: cannot open: $in\n";
47 while (defined($line = <IN>)) {
48 chomp $line;
49 $line =~ s/^\s+//; # Remove leading whitespace
50 $line =~ s/\s*\#.*$//; # Remove comments and trailing whitespace
51 next if ($line eq '');
53 if ($line =~ /^(.*)$/) {
54 push(@prtok, $1);
57 close(IN);
59 @prtok = sort @prtok;
61 open(OUT, "> $out") or die "$0: cannot open: $out\n";
64 # Output prtok.h
66 if ($what eq 'h') {
67 print OUT "/* Automatically generated from $in by $0 */\n";
68 print OUT "/* Do not edit */\n";
69 print OUT "\n";
71 print OUT "enum pragma_token {\n";
72 $n = 0;
73 foreach $pt (@prtok) {
74 if (defined($pt)) {
75 printf OUT " %-16s = %3d,\n", "PR_\U$pt\E", $n;
77 $n++;
79 printf OUT " %-16s = %3d\n", 'PR_INVALID', -1;
80 print OUT "};\n";
81 print OUT "\n";
85 # Output prtok.c
87 if ($what eq 'c') {
88 print OUT "/* Automatically generated from $in by $0 */\n";
89 print OUT "/* Do not edit */\n";
90 print OUT "\n";
92 my %tokens = ();
93 my @tokendata = ();
95 my $n = 0;
96 foreach $pt (@prtok) {
97 if (defined($pt)) {
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
102 # (see below)
103 die "$in: invalid character in token: $pt";
106 $n++;
109 my @hashinfo = gen_perfect_hash(\%tokens);
110 if (!@hashinfo) {
111 die "$0: no hash found\n";
114 # Paranoia...
115 verify_hash_table(\%tokens, \@hashinfo);
117 ($n, $sv, $g) = @hashinfo;
118 $sv2 = $sv+2;
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";
128 print OUT "\n";
130 # Note that this is global.
131 printf OUT "const char * const pr_directives[%d] = {\n", scalar(@prtok);
132 foreach $d (@prtok) {
133 if (defined($d)) {
134 print OUT " \"$d\",\n";
135 } else {
136 print OUT " NULL,\n";
139 print OUT "};\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;
145 print OUT "};\n";
147 print OUT "enum pragma_token pr_token_hash(const char *token)\n";
148 print OUT "{\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";
160 print OUT " };\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";
167 print OUT " };\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";
174 print OUT "\n";
176 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), token);\n",
177 $$sv[0], $$sv[1];
178 print OUT " k1 = (uint32_t)crc;\n";
179 print OUT " k2 = (uint32_t)(crc >> 32);\n";
180 print OUT "\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";
184 print OUT "\n";
186 print OUT " if (!pr_directives[ix] || nasm_stricmp(pr_directives[ix], token))\n";
187 print OUT " return PR_INVALID;\n";
188 print OUT "\n";
189 print OUT " return ix;\n";
190 print OUT "}\n";
194 # Output prtok.ph
196 if ($what eq 'ph') {
197 print OUT "# Automatically generated from $in by $0\n";
198 print OUT "# Do not edit\n";
199 print OUT "\n";
201 print OUT "%prtok_hash = (\n";
202 $n = 0;
203 foreach $tok (@prtok) {
204 if (defined($tok)) {
205 printf OUT " '%%%s' => %d,\n", $tok, $n;
207 $n++;
209 print OUT ");\n";
210 print OUT "1;\n";