3 # Produce pptok.c, pptok.h and pptok.ph 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";
62 print OUT
"/* Automatically generated from $in by $0 */\n";
63 print OUT
"/* Do not edit */\n";
66 print OUT
"enum preproc_token {\n";
68 foreach $pt (@pptok) {
70 printf OUT
" %-16s = %3d,\n", "PP_\U$pt\E", $n;
74 printf OUT
" %-16s = %3d\n", 'PP_INVALID', -1;
78 print OUT
"enum pp_conditional {\n";
82 printf OUT
" %-16s = %3d,\n", "PPC_IF\U$cc\E", $n;
88 printf OUT
"#define PP_COND(x) ((enum pp_conditional)((x) & 0x%x))\n",
89 (scalar(@cond)-1) << 1;
90 print OUT
"#define PP_IS_COND(x) ((unsigned int)(x) < PP_\U$first_uncond\E)\n";
91 print OUT
"#define PP_NEGATIVE(x) ((x) & 1)\n";
94 foreach $ct (@cctok) {
95 print OUT
"#define CASE_PP_\U$ct\E";
99 print OUT
"$pref\tcase PP_\U${ct}${cc}\E: \\\n";
100 print OUT
"\tcase PP_\U${ct}N${cc}\E";
104 print OUT
"\n"; # No colon or newline on the last one
112 print OUT
"/* Automatically generated from $in by $0 */\n";
113 print OUT
"/* Do not edit */\n";
120 foreach $pt (@pptok) {
122 $tokens{'%'.$pt} = $n;
123 if ($pt =~ /[\@\[\]\\_]/) {
124 # Fail on characters which look like upper-case letters
125 # to the quick-and-dirty downcasing in the prehash
127 die "$in: invalid character in token: $pt";
133 my @hashinfo = gen_perfect_hash
(\
%tokens);
134 if (!defined(@hashinfo)) {
135 die "$0: no hash found\n";
139 verify_hash_table
(\
%tokens, \
@hashinfo);
141 ($n, $sv, $g) = @hashinfo;
144 die if ($n & ($n-1));
146 print OUT
"#include \"compiler.h\"\n";
147 print OUT
"#include <inttypes.h>\n";
148 print OUT
"#include <ctype.h>\n";
149 print OUT
"#include \"nasmlib.h\"\n";
150 print OUT
"#include \"hashtbl.h\"\n";
151 print OUT
"#include \"preproc.h\"\n";
154 # Note that this is global.
155 printf OUT
"const char * const pp_directives[%d] = {\n", scalar(@pptok);
156 foreach $d (@pptok) {
158 print OUT
" \"%$d\",\n";
160 print OUT
" NULL,\n";
165 printf OUT
"const uint8_t pp_directives_len[%d] = {\n", scalar(@pptok);
166 foreach $d (@pptok) {
167 printf OUT
" %d,\n", defined($d) ?
length($d)+1 : 0;
171 print OUT
"enum preproc_token pp_token_hash(const char *token)\n";
174 # Put a large value in unused slots. This makes it extremely unlikely
175 # that any combination that involves unused slot will pass the range test.
176 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
177 print OUT
"#define UNUSED 16383\n";
179 print OUT
" static const int16_t hash1[$n] = {\n";
180 for ($i = 0; $i < $n; $i++) {
181 my $h = ${$g}[$i*2+0];
182 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
186 print OUT
" static const int16_t hash2[$n] = {\n";
187 for ($i = 0; $i < $n; $i++) {
188 my $h = ${$g}[$i*2+1];
189 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
193 print OUT
" uint32_t k1, k2;\n";
194 print OUT
" uint64_t crc;\n";
195 # For correct overflow behavior, "ix" should be unsigned of the same
196 # width as the hash arrays.
197 print OUT
" uint16_t ix;\n";
200 printf OUT
" crc = crc64i(UINT64_C(0x%08x%08x), token);\n",
202 print OUT
" k1 = (uint32_t)crc;\n";
203 print OUT
" k2 = (uint32_t)(crc >> 32);\n";
205 printf OUT
" ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
206 printf OUT
" if (ix >= %d)\n", scalar(@pptok);
207 print OUT
" return PP_INVALID;\n";
210 print OUT
" if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
211 print OUT
" return PP_INVALID;\n";
213 print OUT
" return ix;\n";
221 print OUT
"# Automatically generated from $in by $0\n";
222 print OUT
"# Do not edit\n";
225 print OUT
"%pptok_hash = (\n";
227 foreach $tok (@pptok) {
229 printf OUT
" '%%%s' => %d,\n", $tok, $n;