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 =~ /^\*(.*)$/) {
30 foreach $ct (@cctok) {
32 push(@pptok, $ct.$cc);
33 push(@pptok, $ct.'n'.$cc);
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";
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";
53 print OUT " PP_INVALID = -1\n";
64 foreach $pt (@pptok) {
65 (my $px = $pt) =~ s/\%//g;
66 $tokens{$pt} = scalar @tokendata;
67 push(@tokendata, $pt);
70 my @hashinfo = gen_perfect_hash(\%tokens);
71 if (!defined(@hashinfo)) {
72 die "$0: no hash found\n";
76 verify_hash_table(\%tokens, \@hashinfo);
78 ($n, $sv, $g) = @hashinfo;
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";
89 print OUT "#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
92 # Note that this is global.
93 printf OUT "const char * const pp_directives[%d] = {\n",
95 foreach $d (@tokendata) {
96 print OUT " \"$d\",\n";
100 print OUT "enum preproc_token pp_token_hash(const char *token)\n";
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";
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";
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";
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";
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";
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";
142 print OUT " if (nasm_stricmp(pp_directives[ix], token))\n";
143 print OUT " return PP_INVALID;\n";
145 print OUT " return ix;\n";