Use a perfect hash to look up preprocessor directives
[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 foreach $ct (@cctok) {
31     foreach $cc (@cond) {
32         push(@pptok, $ct.$cc);
33         push(@pptok, $ct.'n'.$cc);
34     }
35 }
36
37 @pptok = sort @pptok;
38
39 open(OUT, "> $out") or die "$0: cannot open: $out\n";
40 print OUT "/* Automatically generated from $in by $0 */\n";
41 print OUT "/* Do not edit */\n";
42 print OUT "\n";
43
44 #
45 # Output pptok.h
46 #
47 if ($what eq 'h') {
48     print OUT "enum preproc_token {\n";
49     foreach $pt (@pptok) {
50         (my $px = $pt) =~ s/\%//g;
51         print OUT "    PP_\U$px\E,\n";
52     }
53     print OUT "    PP_INVALID = -1\n";
54     print OUT "};\n";
55 }
56
57 #
58 # Output pptok.c
59 #
60 if ($what eq 'c') {
61     my %tokens = ();
62     my @tokendata = ();
63
64     foreach $pt (@pptok) {
65         (my $px = $pt) =~ s/\%//g;
66         $tokens{$pt} = scalar @tokendata;
67         push(@tokendata, $pt);
68     }
69
70     my @hashinfo = gen_perfect_hash(\%tokens);
71     if (!defined(@hashinfo)) {
72         die "$0: no hash found\n";
73     }
74
75     # Paranoia...
76     verify_hash_table(\%tokens, \@hashinfo);
77     
78     ($n, $sv, $g) = @hashinfo;
79     $sv2 = $sv+2;
80     
81     die if ($n & ($n-1));
82     
83     print OUT "#include <inttypes.h>\n";
84     print OUT "#include <ctype.h>\n";
85     print OUT "#include \"nasmlib.h\"\n";
86     print OUT "#include \"preproc.h\"\n";
87     print OUT "\n";
88
89     print OUT "#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
90     print OUT "\n";
91
92     # Note that this is global.
93     printf OUT "const char * const pp_directives[%d] = {\n",
94         scalar(@tokendata);
95     foreach $d (@tokendata) {
96         print OUT "    \"$d\",\n";
97     }
98     print OUT  "};\n";
99     
100     print OUT "enum preproc_token pp_token_hash(const char *token)\n";
101     print OUT "{\n";
102
103     # Put a large value in unused slots.  This makes it extremely unlikely
104     # that any combination that involves unused slot will pass the range test.
105     # This speeds up rejection of unrecognized tokens, i.e. identifiers.
106     print OUT "#define UNUSED 16383\n";
107
108     print OUT "    static const int16_t hash1[$n] = {\n";
109     for ($i = 0; $i < $n; $i++) {
110         my $h = ${$g}[$i*2+0];
111         print OUT "        ", defined($h) ? $h : 'UNUSED', ",\n";
112     }
113     print OUT "    };\n";
114     
115     print OUT "    static const int16_t hash2[$n] = {\n";
116     for ($i = 0; $i < $n; $i++) {
117         my $h = ${$g}[$i*2+1];
118         print OUT "        ", defined($h) ? $h : 'UNUSED', ",\n";
119     }
120     print OUT "    };\n";
121     
122     print OUT  "    uint32_t k1 = 0, k2 = 0;\n";
123     print OUT  "    uint8_t c;\n";
124     # For correct overflow behavior, "ix" should be unsigned of the same
125     # width as the hash arrays.
126     print OUT  "    uint16_t ix;\n";
127     print OUT  "    const char *p = token;\n";
128     print OUT  "\n";
129
130     print OUT  "    while ((c = *p++) != 0) {\n";
131     print OUT "         c = tolower(c);\n";
132     printf OUT "        uint32_t kn1 = rot(k1,%2d) - rot(k2,%2d) + c;\n", ${$sv}[0], ${$sv}[1];
133     printf OUT "        uint32_t kn2 = rot(k2,%2d) - rot(k1,%2d) + c;\n", ${$sv}[2], ${$sv}[3];
134     print OUT  "        k1 = kn1; k2 = kn2;\n";
135     print OUT  "    }\n";
136     print OUT  "\n";
137     printf OUT "    ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
138     printf OUT "    if (ix >= %d)\n", scalar(@tokendata);
139     print OUT  "        return PP_INVALID;\n";
140     print OUT  "\n";
141
142     print OUT  "    if (nasm_stricmp(pp_directives[ix], token))\n";
143     print OUT  "        return PP_INVALID;\n";
144     print OUT  "\n";
145     print OUT  "    return ix;\n";
146     print OUT  "}\n";
147 }