insns: correct the TSX opcodes
[nasm/nasm.git] / macros.pl
blob14524ec8851b03d7e335e2f1ffbb788093627b02
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2009 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
7 ##
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following
10 ## conditions are met:
12 ## * Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## * Redistributions in binary form must reproduce the above
15 ## copyright notice, this list of conditions and the following
16 ## disclaimer in the documentation and/or other materials provided
17 ## with the distribution.
18 ##
19 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ## --------------------------------------------------------------------------
36 # macros.pl produce macros.c from standard.mac
39 require 'phash.ph';
40 require 'pptok.ph';
42 use bytes;
44 my $fname;
45 my $line = 0;
46 my $index = 0;
47 my $tasm_count = 0;
50 # Print out a string as a character array
52 sub charcify(@) {
53 my $l = '';
54 my $c, $o;
55 foreach $o (unpack("C*", join('',@_))) {
56 $c = pack("C", $o);
57 if ($o < 32 || $o > 126 || $c eq '"' || $c eq "\\") {
58 $l .= sprintf("%3d,", $o);
59 } else {
60 $c =~ s/\'/\\'/; # << sanitize single quote.
61 $l .= "\'".$c."\',";
64 return $l;
69 # Generate macros.c
71 open(OUT,"> macros.c\0") or die "unable to open macros.c\n";
73 print OUT "/*\n";
74 print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
75 print OUT " * ", join("\n * ", @ARGV), "\n";
76 print OUT " */\n";
77 print OUT "\n";
78 print OUT "#include \"tables.h\"\n";
79 print OUT "#include \"nasmlib.h\"\n";
80 print OUT "#include \"hashtbl.h\"\n";
81 print OUT "#include \"output/outform.h\"\n";
82 print OUT "\n";
83 print OUT "#if 1\n";
84 print OUT "const unsigned char nasm_stdmac[] = {";
86 my $npkg = 0;
87 my @pkg_list = ();
88 my %pkg_number = ();
89 my $pkg;
90 my @out_list = ();
91 my $outfmt;
92 my $lastname;
93 my $z;
95 foreach $fname ( @ARGV ) {
96 open(INPUT,"< $fname\0") or die "$0: $fname: $!\n";
97 while (<INPUT>) {
98 $line++;
99 chomp;
100 while (/^(.*)\\$/) {
101 $_ = $1;
102 $_ .= <INPUT>;
103 chomp;
104 $line++;
106 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
107 $tasm_count = $index;
108 print OUT " /* End of TASM macros */\n";
109 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
110 undef $pkg;
111 my @out_alias = split(/\s+/, $1);
112 printf OUT " /* %4d */ 0\n", $index++;
113 print OUT "};\n#endif\n";
114 $index = 0;
115 print OUT "\n";
116 my $pfx = '#if';
117 foreach my $al (@out_alias) {
118 print OUT $pfx, " defined(OF_\U${al}\E)";
119 $pfx = ' ||';
121 printf OUT "\nconst unsigned char %s_stdmac[] = {\n", $out_alias[0];
122 print OUT " /* From $fname */\n";
123 $lastname = $fname;
124 push(@out_list, $out_alias[0]);
125 $out_index{$out_alias[0]} = $index;
126 } elsif (m/^USE:\s*(\S+)\s*$/) {
127 $pkg = $1;
128 if (defined($pkg_number{$pkg})) {
129 die "$0: $fname: duplicate package: $pkg\n";
131 printf OUT " /* %4d */ 0\n", $index++;
132 print OUT "};\n#endif\n";
133 $index = 0;
134 print OUT "\n#if 1\n";
135 printf OUT "static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
136 print OUT " /* From $fname */\n";
137 $lastname = $fname;
138 push(@pkg_list, $pkg);
139 $pkg_number{$pkg} = $npkg++;
140 $z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
141 printf OUT " /* %4d */ %s0,\n", $index, charcify($z);
142 $index += length($z)+1;
143 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
144 my $s1, $s2, $pd, $ws;
145 $s1 = $1;
146 $s2 = '';
147 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
148 $s2 .= "$'";
149 $pd = $1;
150 $ws = $3;
151 $s1 = $4;
152 if (defined($pptok_hash{$pd}) &&
153 $pptok_hash{$pd} <= 127) {
154 $s2 .= pack("C", $pptok_hash{$pd}+128);
155 } else {
156 $s2 .= $pd.$ws;
159 $s2 .= $s1;
160 if (length($s2) > 0) {
161 if ($lastname ne $fname) {
162 print OUT "\n /* From $fname */\n";
163 $lastname = $fname;
165 printf OUT " /* %4d */ %s0,\n",
166 $index, charcify($s2);
167 $index += length($s2)+1;
169 } else {
170 die "$fname:$line: error unterminated quote";
173 close(INPUT);
175 printf OUT " /* %4d */ 0\n};\n#endif\n\n", $index++;
176 print OUT "const unsigned char * const nasm_stdmac_after_tasm = ",
177 "&nasm_stdmac[$tasm_count];\n\n";
179 my @hashinfo = gen_perfect_hash(\%pkg_number);
180 if (!@hashinfo) {
181 die "$0: no hash found\n";
183 # Paranoia...
184 verify_hash_table(\%pkg_number, \@hashinfo);
185 my ($n, $sv, $g) = @hashinfo;
186 die if ($n & ($n-1));
188 print OUT "const unsigned char *nasm_stdmac_find_package(const char *package)\n";
189 print OUT "{\n";
190 print OUT " static const struct {\n";
191 print OUT " const char *package;\n";
192 print OUT " const unsigned char *macros;\n";
193 print OUT " } packages[$npkg] = {\n";
194 foreach $pkg (@pkg_list) {
195 printf OUT " { \"%s\", nasm_stdmac_%s },\n",
196 $pkg, $pkg;
198 print OUT " };\n";
200 # Put a large value in unused slots. This makes it extremely unlikely
201 # that any combination that involves unused slot will pass the range test.
202 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
203 print OUT "#define UNUSED 16383\n";
205 print OUT " static const int16_t hash1[$n] = {\n";
206 for ($i = 0; $i < $n; $i++) {
207 my $h = ${$g}[$i*2+0];
208 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
210 print OUT " };\n";
212 print OUT " static const int16_t hash2[$n] = {\n";
213 for ($i = 0; $i < $n; $i++) {
214 my $h = ${$g}[$i*2+1];
215 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
217 print OUT " };\n";
219 print OUT " uint32_t k1, k2;\n";
220 print OUT " uint64_t crc;\n";
221 # For correct overflow behavior, "ix" should be unsigned of the same
222 # width as the hash arrays.
223 print OUT " uint16_t ix;\n";
224 print OUT "\n";
226 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
227 $$sv[0], $$sv[1];
228 print OUT " k1 = (uint32_t)crc;\n";
229 print OUT " k2 = (uint32_t)(crc >> 32);\n";
230 print OUT "\n";
231 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
232 printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
233 print OUT " return NULL;\n";
234 print OUT "\n";
235 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
236 print OUT " return NULL;\n";
237 print OUT "\n";
238 print OUT " return packages[ix].macros;\n";
239 print OUT "}\n";
241 close(OUT);