pptok.c: handle holes in the pp_directives array
[platform/upstream/nasm.git] / pptok.pl
1 #!/usr/bin/perl
2 #
3 # Produce pptok.c and pptok.h from pptok.dat
4 #
5
6 require 'phash.ph';
7
8 my($what, $in, $out) = @ARGV;
9
10 #
11 # Read pptok.dat
12 #
13 open(IN, "< $in") or die "$0: cannot open: $in\n";
14 while (defined($line = <IN>)) {
15     chomp $line;
16     $line =~ s/^\s+//;          # Remove leading whitespace
17     $line =~ s/\s*\#.*$//;      # Remove comments and trailing whitespace
18     next if ($line eq '');
19     
20     if ($line =~ /^\%(.*)\*$/) {
21         push(@cctok, $1);
22     } elsif ($line =~ /^\%(.*)$/) {
23         push(@pptok, $1);
24     } elsif ($line =~ /^\*(.*)$/) {
25         push(@cond, $1);
26     }
27 }
28 close(IN);
29
30 @cctok = sort @cctok;
31 @cond = sort @cond;
32 @pptok = sort @pptok;
33
34 # Generate the expanded list including conditionals.  The conditionals
35 # are at the beginning, padded to a power of 2, with the inverses
36 # interspersed; this allows a simple mask to pick out the condition.
37
38 while ((scalar @cond) & (scalar @cond)-1) {
39     push(@cond, undef);
40 }
41
42 @cptok = ();
43 foreach $ct (@cctok) {
44     foreach $cc (@cond) {
45         if (defined($cc)) {
46             push(@cptok, $ct.$cc);
47             push(@cptok, $ct.'n'.$cc);
48         } else {
49             push(@cptok, undef, undef);
50         }
51     }
52 }
53 $first_uncond = $pptok[0];
54 @pptok = (@cptok, @pptok);
55
56 open(OUT, "> $out") or die "$0: cannot open: $out\n";
57 print OUT "/* Automatically generated from $in by $0 */\n";
58 print OUT "/* Do not edit */\n";
59 print OUT "\n";
60
61 #
62 # Output pptok.h
63 #
64 if ($what eq 'h') {
65     print OUT "enum preproc_token {\n";
66     $n = 0;
67     foreach $pt (@pptok) {
68         if (defined($pt)) {
69             printf OUT "    %-16s = %3d,\n", "PP_\U$pt\E", $n;
70         }
71         $n++;
72     }
73     printf OUT "    %-16s = %3d\n", 'PP_INVALID', -1;
74     print OUT "};\n";
75     print OUT "\n";
76
77     print  OUT "enum pp_conditional {\n";
78     $n = 0;
79     foreach $cc (@cond) {
80         if (defined($cc)) {
81             printf OUT "    %-16s = %3d,\n", "PPC_IF\U$cc\E", $n;
82         }
83         $n += 2;
84     }
85     print  OUT "};\n\n";
86     
87     printf OUT "#define PP_COND(x)     ((enum pp_conditional)((x) & 0x%x))\n",
88         (scalar(@cond)-1) << 1;
89     print  OUT "#define PP_IS_COND(x)  ((unsigned int)(x) < PP_\U$first_uncond\E)\n";
90     print  OUT "#define PP_NEGATIVE(x) ((x) & 1)\n";
91     print  OUT "\n";
92
93     foreach $ct (@cctok) {
94         print OUT "#define CASE_PP_\U$ct\E";
95         $pref = " \\\n";
96         foreach $cc (@cond) {
97             if (defined($cc)) {
98                 print OUT "$pref\tcase PP_\U${ct}${cc}\E: \\\n";
99                 print OUT "\tcase PP_\U${ct}N${cc}\E";
100                 $pref = ":\\\n";
101             }
102         }
103         print OUT "\n";         # No colon or newline on the last one
104     }
105 }
106
107 #
108 # Output pptok.c
109 #
110 if ($what eq 'c') {
111     my %tokens = ();
112     my @tokendata = ();
113
114     my $n = 0;
115     foreach $pt (@pptok) {
116         if (defined($pt)) {
117             $tokens{'%'.$pt} = $n;
118         }
119         $n++;
120     }
121
122     my @hashinfo = gen_perfect_hash(\%tokens);
123     if (!defined(@hashinfo)) {
124         die "$0: no hash found\n";
125     }
126
127     # Paranoia...
128     verify_hash_table(\%tokens, \@hashinfo);
129     
130     ($n, $sv, $g) = @hashinfo;
131     $sv2 = $sv+2;
132     
133     die if ($n & ($n-1));
134     
135     print OUT "#include <inttypes.h>\n";
136     print OUT "#include <ctype.h>\n";
137     print OUT "#include \"nasmlib.h\"\n";
138     print OUT "#include \"preproc.h\"\n";
139     print OUT "\n";
140
141     print OUT "#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
142     print OUT "\n";
143
144     # Note that this is global.
145     printf OUT "const char * const pp_directives[%d] = {\n", scalar(@pptok);
146     foreach $d (@pptok) {
147         if (defined($d)) {
148             print OUT "    \"%$d\",\n";
149         } else {
150             print OUT "    NULL,\n";
151         }
152     }
153     print OUT  "};\n";
154     
155     print OUT "enum preproc_token pp_token_hash(const char *token)\n";
156     print OUT "{\n";
157
158     # Put a large value in unused slots.  This makes it extremely unlikely
159     # that any combination that involves unused slot will pass the range test.
160     # This speeds up rejection of unrecognized tokens, i.e. identifiers.
161     print OUT "#define UNUSED 16383\n";
162
163     print OUT "    static const int16_t hash1[$n] = {\n";
164     for ($i = 0; $i < $n; $i++) {
165         my $h = ${$g}[$i*2+0];
166         print OUT "        ", defined($h) ? $h : 'UNUSED', ",\n";
167     }
168     print OUT "    };\n";
169     
170     print OUT "    static const int16_t hash2[$n] = {\n";
171     for ($i = 0; $i < $n; $i++) {
172         my $h = ${$g}[$i*2+1];
173         print OUT "        ", defined($h) ? $h : 'UNUSED', ",\n";
174     }
175     print OUT "    };\n";
176     
177     print OUT  "    uint32_t k1 = 0, k2 = 0;\n";
178     print OUT  "    uint8_t c;\n";
179     # For correct overflow behavior, "ix" should be unsigned of the same
180     # width as the hash arrays.
181     print OUT  "    uint16_t ix;\n";
182     print OUT  "    const char *p = token;\n";
183     print OUT  "\n";
184
185     print OUT  "    while ((c = *p++) != 0) {\n";
186     print OUT  "        c = tolower(c);\n";
187     printf OUT "        uint32_t kn1 = rot(k1,%2d) - rot(k2,%2d) + c;\n", ${$sv}[0], ${$sv}[1];
188     printf OUT "        uint32_t kn2 = rot(k2,%2d) - rot(k1,%2d) + c;\n", ${$sv}[2], ${$sv}[3];
189     print OUT  "        k1 = kn1; k2 = kn2;\n";
190     print OUT  "    }\n";
191     print OUT  "\n";
192     printf OUT "    ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
193     printf OUT "    if (ix >= %d)\n", scalar(@pptok);
194     print OUT  "        return PP_INVALID;\n";
195     print OUT  "\n";
196
197     print OUT  "    if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
198     print OUT  "        return PP_INVALID;\n";
199     print OUT  "\n";
200     print OUT  "    return ix;\n";
201     print OUT  "}\n";
202 }