Makefile.in: generate manpages for "make dist"
[nasm.git] / directiv.pl
blob51d0ec38a01a5e597f4e38343ed5136c972b258d
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2009 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.
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 # Generate a perfect hash for directive parsing
38 # Usage:
39 # directiv.pl h directiv.dat directiv.h (to generate C header)
40 # directiv.pl c directiv.dat directiv.c (to generate C source)
43 require 'phash.ph';
45 my($output, $directives_dat, $outfile) = @ARGV;
47 @directives = ();
48 @specials = ('none', 'unknown');
50 open(DD, "< ${directives_dat}\0")
51 or die "$0: cannot open: ${directives_dat}: $!\n";
52 while (defined($line = <DD>)) {
53 chomp $line;
54 if ($line =~ /^\s*([[:alnum:]]+)\s*(|[\;\#].*)$/) {
55 push(@directives, $1);
58 close(DD);
60 if ($output eq 'h') {
61 open(H, "> ${outfile}\0")
62 or die "$0: cannot create: ${outfile}: $!\n";
64 print H "/*\n";
65 print H " * This file is generated from directiv.dat\n";
66 print H " * by directiv.pl; do not edit.\n";
67 print H " */\n";
68 print H "\n";
70 print H "#ifndef NASM_DIRECTIVES_H\n";
71 print H "#define NASM_DIRECTIVES_H\n";
72 print H "\n";
74 $c = '{';
75 print H "enum directives ";
76 foreach $d (@specials) {
77 print H "$c\n D_$d";
78 $c = ',';
80 foreach $d (@directives) {
81 print H "$c\n D_\U$d";
82 $c = ',';
84 print H "\n};\n\n";
85 printf H "extern const char * const directives[%d];\n",
86 scalar(@directives)+scalar(@specials);
87 print H "enum directives find_directive(const char *token);\n\n";
88 print H "#endif /* NASM_DIRECTIVES_H */\n";
89 } elsif ($output eq 'c') {
90 %directive = ();
91 $n = 0;
92 foreach $d (@directives) {
93 if (exists($directive{$d})) {
94 die "$0: $directives_dat: duplicate directive: $d\n";
96 $directive{$d} = $n++; # This is zero-based, unlike the enum!
99 @hashinfo = gen_perfect_hash(\%directive);
100 if (!@hashinfo) {
101 die "$0: no hash found\n";
104 # Paranoia...
105 verify_hash_table(\%directive, \@hashinfo);
107 ($n, $sv, $g) = @hashinfo;
109 die if ($n & ($n-1));
111 open(C, "> ${outfile}\0")
112 or die "$0: cannot create: ${directives_c}: $!\n";
114 print C "/*\n";
115 print C " * This file is generated from directiv.dat\n";
116 print C " * by directiv.pl; do not edit.\n";
117 print C " */\n";
118 print C "\n";
120 print C "#include \"compiler.h\"\n";
121 print C "#include <string.h>\n";
122 print C "#include \"nasm.h\"\n";
123 print C "#include \"hashtbl.h\"\n";
124 print C "#include \"directiv.h\"\n";
125 print C "\n";
127 printf C "const char * const directives[%d] =\n",
128 scalar(@directives)+scalar(@specials);
129 $c = '{';
130 foreach $d (@specials) {
131 print C "$c\n NULL";
132 $c = ',';
134 foreach $d (@directives) {
135 print C "$c\n \"$d\"";
136 $c = ',';
138 print C "\n};\n\n";
140 print C "enum directives find_directive(const char *token)\n";
141 print C "{\n";
143 # Put a large value in unused slots. This makes it extremely unlikely
144 # that any combination that involves unused slot will pass the range test.
145 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
146 print C "#define UNUSED 16383\n";
148 print C " static const int16_t hash1[$n] = {\n";
149 for ($i = 0; $i < $n; $i++) {
150 my $h = ${$g}[$i*2+0];
151 print C " ", defined($h) ? $h : 'UNUSED', ",\n";
153 print C " };\n";
155 print C " static const int16_t hash2[$n] = {\n";
156 for ($i = 0; $i < $n; $i++) {
157 my $h = ${$g}[$i*2+1];
158 print C " ", defined($h) ? $h : 'UNUSED', ",\n";
160 print C " };\n";
162 print C " uint32_t k1, k2;\n";
163 print C " uint64_t crc;\n";
164 # For correct overflow behavior, "ix" should be unsigned of the same
165 # width as the hash arrays.
166 print C " uint16_t ix;\n";
167 print C "\n";
168 printf C " crc = crc64i(UINT64_C(0x%08x%08x), token);\n",
169 $$sv[0], $$sv[1];
170 print C " k1 = (uint32_t)crc;\n";
171 print C " k2 = (uint32_t)(crc >> 32);\n";
172 print C "\n";
173 printf C " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
174 printf C " if (ix >= %d)\n", scalar(@directives);
175 print C " return D_unknown;\n";
176 print C "\n";
177 printf C " ix += %d;\n", scalar(@specials);
178 print C " if (nasm_stricmp(token, directives[ix]))\n";
179 print C " return D_unknown;\n";
180 print C "\n";
181 print C " return ix;\n";
182 print C "}\n";