Merge branch 'new-preproc'
[platform/upstream/nasm.git] / directives.pl
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:
11 ##
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.
32 ##
33 ## --------------------------------------------------------------------------
34
35 #
36 # Generate a perfect hash for directive parsing
37 #
38 # Usage: directives.pl directives.dat directives.c directives.h
39 #
40
41 require 'phash.ph';
42
43 my($output, $directives_dat, $outfile) = @ARGV;
44
45 @directives = ();
46
47 open(DD, "< ${directives_dat}\0")
48     or die "$0: cannot open: ${directives_dat}: $!\n";
49 while (defined($line = <DD>)) {
50     chomp $line;
51     if ($line =~ /^\s*([[:alnum:]]+)\s*(|[\;\#].*)$/) {
52         push(@directives, $1);
53     }
54 }
55 close(DD);
56
57 if ($output eq 'h') {
58     open(H, "> ${outfile}\0")
59         or die "$0: cannot create: ${outfile}: $!\n";
60
61     print H "/*\n";
62     print H " * This     file is generated from directives.dat\n";
63     print H " * by directives.pl; do not edit.\n";
64     print H " */\n";
65     print H "\n";
66
67     print H "#ifndef NASM_DIRECTIVES_H\n";
68     print H "#define NASM_DIRECTIVES_H\n";
69     print H "\n";
70
71     print H "enum directives {\n";
72     print H "    D_NONE";
73     foreach $d (@directives) {
74         print H ",\n    D_\U$d";
75     }
76     print H "\n};\n\n";
77     printf H "extern const char * const directives[%d];\n",
78         scalar(@directives)+1;
79     print H "enum directives find_directive(const char *token);\n\n";
80     print H "#endif /* NASM_DIRECTIVES_H */\n";
81 } elsif ($output eq 'c') {
82     %directive = ();
83     $n = 0;
84     foreach $d (@directives) {
85         if (exists($directive{$d})) {
86             die "$0: $directives_dat: duplicate directive: $d\n";
87         }
88         $directive{$d} = $n++;  # This is zero-based, unlike the enum!
89     }
90
91     @hashinfo = gen_perfect_hash(\%directive);
92     if (!defined(@hashinfo)) {
93         die "$0: no hash found\n";
94     }
95
96     # Paranoia...
97     verify_hash_table(\%directive, \@hashinfo);
98
99     ($n, $sv, $g) = @hashinfo;
100     $sv2 = $sv+2;
101
102     die if ($n & ($n-1));
103
104     open(C, "> ${outfile}\0")
105         or die "$0: cannot create: ${directives_c}: $!\n";
106
107     print C "/*\n";
108     print C " * This file is generated from directives.dat\n";
109     print C " * by directives.pl; do not edit.\n";
110     print C " */\n";
111     print C "\n";
112
113     print C "#include \"compiler.h\"\n";
114     print C "#include <string.h>\n";
115     print C "#include \"nasm.h\"\n";
116     print C "#include \"hashtbl.h\"\n";
117     print C "#include \"directives.h\"\n";
118     print C "\n";
119
120     printf C "const char * const directives[%d] = {\n",
121         scalar(@directives)+1;
122     print C "    NULL";
123     foreach $d (@directives) {
124         print C ",\n    \"$d\"";
125     }
126     print C "\n};\n\n";
127
128     print C "enum directives find_directive(const char *token)\n";
129     print C "{\n";
130
131     # Put a large value in unused slots.  This makes it extremely unlikely
132     # that any combination that involves unused slot will pass the range test.
133     # This speeds up rejection of unrecognized tokens, i.e. identifiers.
134     print C "#define UNUSED 16383\n";
135
136     print C "    static const int16_t hash1[$n] = {\n";
137     for ($i = 0; $i < $n; $i++) {
138         my $h = ${$g}[$i*2+0];
139         print C "        ", defined($h) ? $h : 'UNUSED', ",\n";
140     }
141     print C "    };\n";
142
143     print C "    static const int16_t hash2[$n] = {\n";
144     for ($i = 0; $i < $n; $i++) {
145         my $h = ${$g}[$i*2+1];
146         print C "        ", defined($h) ? $h : 'UNUSED', ",\n";
147     }
148     print C "    };\n";
149
150     print C  "    uint32_t k1, k2;\n";
151     print C  "    uint64_t crc;\n";
152     # For correct overflow behavior, "ix" should be unsigned of the same
153     # width as the hash arrays.
154     print C  "    uint16_t ix;\n";
155     print C  "\n";
156     printf C "    crc = crc64i(UINT64_C(0x%08x%08x), token);\n",
157         $$sv[0], $$sv[1];
158     print C  "    k1 = (uint32_t)crc;\n";
159     print C  "    k2 = (uint32_t)(crc >> 32);\n";
160     print C  "\n";
161     printf C "    ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
162     printf C "    if (ix >= %d)\n", scalar(@directives);
163     print C  "        return D_NONE;\n";
164     print C  "\n";
165     print C  "    ix++;\n";     # Account for D_NONE
166     print C  "    if (nasm_stricmp(token, directives[ix]))\n";
167     print C  "        return D_NONE;\n";
168     print C  "\n";
169     print C  "    return ix;\n";
170     print C  "}\n";
171 }