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;
118 if ($pt =~ /[\@\[\]\\_]/) {
119 # Fail on characters which look like upper-case letters
120 # to the quick-and-dirty downcasing in the prehash
122 die "$in: invalid character in token: $pt";
128 my @hashinfo = gen_perfect_hash
(\
%tokens);
129 if (!defined(@hashinfo)) {
130 die "$0: no hash found\n";
134 verify_hash_table
(\
%tokens, \
@hashinfo);
136 ($n, $sv, $g) = @hashinfo;
139 die if ($n & ($n-1));
141 print OUT
"#include <inttypes.h>\n";
142 print OUT
"#include <ctype.h>\n";
143 print OUT
"#include \"nasmlib.h\"\n";
144 print OUT
"#include \"preproc.h\"\n";
147 print OUT
"#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
150 # Note that this is global.
151 printf OUT
"const char * const pp_directives[%d] = {\n", scalar(@pptok);
152 foreach $d (@pptok) {
154 print OUT
" \"%$d\",\n";
156 print OUT
" NULL,\n";
161 print OUT
"enum preproc_token pp_token_hash(const char *token)\n";
164 # Put a large value in unused slots. This makes it extremely unlikely
165 # that any combination that involves unused slot will pass the range test.
166 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
167 print OUT
"#define UNUSED 16383\n";
169 print OUT
" static const int16_t hash1[$n] = {\n";
170 for ($i = 0; $i < $n; $i++) {
171 my $h = ${$g}[$i*2+0];
172 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
176 print OUT
" static const int16_t hash2[$n] = {\n";
177 for ($i = 0; $i < $n; $i++) {
178 my $h = ${$g}[$i*2+1];
179 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
183 print OUT
" uint32_t k1 = 0, k2 = 0;\n";
184 print OUT
" uint8_t c;\n";
185 # For correct overflow behavior, "ix" should be unsigned of the same
186 # width as the hash arrays.
187 print OUT
" uint16_t ix;\n";
188 print OUT
" const char *p = token;\n";
191 print OUT
" while ((c = *p++) != 0) {\n";
192 print OUT
" uint32_t kn1, kn2;\n";
193 print OUT
" c |= 0x20; /* convert to lower case */\n";
194 printf OUT
" kn1 = rot(k1,%2d)^(rot(k2,%2d) + c);\n", ${$sv}[0], ${$sv}[1];
195 printf OUT
" kn2 = rot(k2,%2d)^(rot(k1,%2d) + c);\n", ${$sv}[2], ${$sv}[3];
196 print OUT
" k1 = kn1; k2 = kn2;\n";
199 printf OUT
" ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
200 printf OUT
" if (ix >= %d)\n", scalar(@pptok);
201 print OUT
" return PP_INVALID;\n";
204 print OUT
" if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
205 print OUT
" return PP_INVALID;\n";
207 print OUT
" return ix;\n";