CLFLUSH: Neither an x64 instruction nor AMD
[nasm.git] / pptok.pl
bloba0425b7c03fb90902595f8a4a3e9e30655a9be1d
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 <inttypes.h>\n";
142 print OUT "#include <ctype.h>\n";
143 print OUT "#include \"nasmlib.h\"\n";
144 print OUT "#include \"preproc.h\"\n";
145 print OUT "\n";
147 print OUT "#define rot(x,y) (((uint32_t)(x) << (y))+((uint32_t)(x) >> (32-(y))))\n";
148 print OUT "\n";
150 # Note that this is global.
151 printf OUT "const char * const pp_directives[%d] = {\n", scalar(@pptok);
152 foreach $d (@pptok) {
153 if (defined($d)) {
154 print OUT " \"%$d\",\n";
155 } else {
156 print OUT " NULL,\n";
159 print OUT "};\n";
161 print OUT "enum preproc_token pp_token_hash(const char *token)\n";
162 print OUT "{\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";
174 print OUT " };\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";
181 print OUT " };\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";
189 print OUT "\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";
197 print OUT " }\n";
198 print OUT "\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";
202 print OUT "\n";
204 print OUT " if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
205 print OUT " return PP_INVALID;\n";
206 print OUT "\n";
207 print OUT " return ix;\n";
208 print OUT "}\n";