Script to automagically synchronize the object file lists
[nasm.git] / pptok.pl
blob896d9d1231bde933bfa2dd33194b086ca1d5bd34
1 #!/usr/bin/perl
3 # Produce pptok.c and pptok.h from pptok.dat
6 require 'phash.ph';
8 my($what, $in, $out) = @ARGV;
11 # Read pptok.dat
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 '');
20 if ($line =~ /^\%(.*)\*$/) {
21 push(@cctok, $1);
22 } elsif ($line =~ /^\%(.*)$/) {
23 push(@pptok, $1);
24 } elsif ($line =~ /^\*(.*)$/) {
25 push(@cond, $1);
28 close(IN);
30 @cctok = sort @cctok;
31 @cond = sort @cond;
32 @pptok = sort @pptok;
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.
38 while ((scalar @cond) & (scalar @cond)-1) {
39 push(@cond, undef);
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);
53 $first_uncond = $pptok[0];
54 @pptok = (@cptok, @pptok);
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";
62 # Output pptok.h
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;
71 $n++;
73 printf OUT " %-16s = %3d\n", 'PP_INVALID', -1;
74 print OUT "};\n";
75 print OUT "\n";
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;
83 $n += 2;
85 print OUT "};\n\n";
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";
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";
103 print OUT "\n"; # No colon or newline on the last one
108 # Output pptok.c
110 if ($what eq 'c') {
111 my %tokens = ();
112 my @tokendata = ();
114 my $n = 0;
115 foreach $pt (@pptok) {
116 if (defined($pt)) {
117 $tokens{'%'.$pt} = $n;
118 if ($pt =~ /[\@\[\]\\_]/) {
119 # Fail on characters which look like upper-case letters
120 # to the quick-and-dirty downcasing in the prehash
121 # (see below)
122 die "$in: invalid character in token: $pt";
125 $n++;
128 my @hashinfo = gen_perfect_hash(\%tokens);
129 if (!defined(@hashinfo)) {
130 die "$0: no hash found\n";
133 # Paranoia...
134 verify_hash_table(\%tokens, \@hashinfo);
136 ($n, $sv, $g) = @hashinfo;
137 $sv2 = $sv+2;
139 die if ($n & ($n-1));
141 print OUT "#include \"compiler.h\"\n";
142 print OUT "#include <inttypes.h>\n";
143 print OUT "#include <ctype.h>\n";
144 print OUT "#include \"nasmlib.h\"\n";
145 print OUT "#include \"hashtbl.h\"\n";
146 print OUT "#include \"preproc.h\"\n";
147 print OUT "\n";
149 # Note that this is global.
150 printf OUT "const char * const pp_directives[%d] = {\n", scalar(@pptok);
151 foreach $d (@pptok) {
152 if (defined($d)) {
153 print OUT " \"%$d\",\n";
154 } else {
155 print OUT " NULL,\n";
158 print OUT "};\n";
160 print OUT "enum preproc_token pp_token_hash(const char *token)\n";
161 print OUT "{\n";
163 # Put a large value in unused slots. This makes it extremely unlikely
164 # that any combination that involves unused slot will pass the range test.
165 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
166 print OUT "#define UNUSED 16383\n";
168 print OUT " static const int16_t hash1[$n] = {\n";
169 for ($i = 0; $i < $n; $i++) {
170 my $h = ${$g}[$i*2+0];
171 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
173 print OUT " };\n";
175 print OUT " static const int16_t hash2[$n] = {\n";
176 for ($i = 0; $i < $n; $i++) {
177 my $h = ${$g}[$i*2+1];
178 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
180 print OUT " };\n";
182 print OUT " uint32_t k1, k2;\n";
183 print OUT " uint64_t crc;\n";
184 # For correct overflow behavior, "ix" should be unsigned of the same
185 # width as the hash arrays.
186 print OUT " uint16_t ix;\n";
187 print OUT "\n";
189 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), token);\n",
190 $$sv[0], $$sv[1];
191 print OUT " k1 = (uint32_t)crc;\n";
192 print OUT " k2 = (uint32_t)(crc >> 32);\n";
193 print OUT "\n";
194 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
195 printf OUT " if (ix >= %d)\n", scalar(@pptok);
196 print OUT " return PP_INVALID;\n";
197 print OUT "\n";
199 print OUT " if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
200 print OUT " return PP_INVALID;\n";
201 print OUT "\n";
202 print OUT " return ix;\n";
203 print OUT "}\n";