3 # Produce pptok.c and pptok.h from pptok.dat
8 my($what, $in, $out) = @ARGV;
13 open(IN, "< $in") or die "$0: cannot open: $in\n";
14 while (defined($line = <IN>)) {
16 $line =~ s/^\s+//; # Remove leading whitespace
17 $line =~ s/\s*\#.*$//; # Remove comments and trailing whitespace
18 next if ($line eq '');
20 if ($line =~ /^\%(.*)\*$/) {
22 } elsif ($line =~ /^\%(.*)$/) {
24 } elsif ($line =~ /^\*(.*)$/) {
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) {
43 foreach $ct (@cctok) {
46 push(@cptok, $ct.$cc);
47 push(@cptok, $ct.'n'.$cc);
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";
65 print OUT "enum preproc_token {\n";
67 foreach $pt (@pptok) {
69 printf OUT " %-16s = %3d,\n", "PP_\U$pt\E", $n;
73 printf OUT " %-16s = %3d\n", 'PP_INVALID', -1;
77 print OUT "enum pp_conditional {\n";
81 printf OUT " %-16s = %3d,\n", "PPC_IF\U$cc\E", $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";
93 foreach $ct (@cctok) {
94 print OUT "#define CASE_PP_\U$ct\E";
98 print OUT "$pref\tcase PP_\U${ct}${cc}\E: \\\n";
99 print OUT "\tcase PP_\U${ct}N${cc}\E";
103 print OUT "\n"; # No colon or newline on the last one
115 foreach $pt (@pptok) {
117 $tokens{'%'.$pt} = $n;
122 my @hashinfo = gen_perfect_hash(\%tokens);
123 if (!defined(@hashinfo)) {
124 die "$0: no hash found\n";
128 verify_hash_table(\%tokens, \@hashinfo);
130 ($n, $sv, $g) = @hashinfo;
133 die if ($n & ($n-1));
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";
141 print OUT "#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
144 # Note that this is global.
145 printf OUT "const char * const pp_directives[%d] = {\n", scalar(@pptok);
146 foreach $d (@pptok) {
148 print OUT " \"%$d\",\n";
150 print OUT " NULL,\n";
155 print OUT "enum preproc_token pp_token_hash(const char *token)\n";
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";
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";
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";
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";
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";
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";
197 print OUT " if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
198 print OUT " return PP_INVALID;\n";
200 print OUT " return ix;\n";