Merge remote-tracking branch 'github/nasm-2.15.xx'
[nasm.git] / macros / macros.pl
blob517a9e6fa108c8e083b5c09e67477a0d1075cbb3
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.
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 'asm/pptok.ph';
42 use bytes;
44 my $fname;
45 my $line = 0;
46 my $index = 0;
47 my $tasm_count = 0;
48 my @pname;
50 # Default names for various bytes
51 for (my $o = 0; $o < 256; $o++) {
52 my $c = chr($o);
53 if ($o < 32 || $o > 126) {
54 $pname[$o] = sprintf("%d", $o);
55 } elsif ($c =~ /^[\'\"\\]$/) {
56 $pname[$o] = "\'\\".$c."\'";
57 } else {
58 $pname[$o] = "\'".$c."\'";
63 # Print out a string as a character array
65 sub charcify(@) {
66 my $l = '';
67 my ($c, $o);
68 my $space = 1;
69 my $quote = 0;
71 foreach $o (unpack("C*", join('',@_))) {
72 $c = pack("C", $o);
73 if ($quote) {
74 if ($o == $quote) {
75 $quote = 0;
77 } elsif ($c =~ /^[\'\"\`]$/) {
78 $quote = $o;
79 } else {
80 if ($c eq ' ') {
81 next if ($space);
82 $o = 32;
83 $c = ' ';
84 $space = 1;
85 } elsif ($o < 32 || $o > 126) {
86 $space = 1; # Implicit space after compacted directive
87 } else {
88 $space = 0;
91 $l .= $pname[$o];
92 $l .= ',';
94 return $l;
99 # Generate macros.c
101 open(OUT, '>', 'macros/macros.c') or die "unable to open macros.c\n";
103 print OUT "/*\n";
104 print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
105 print OUT " * ", join("\n * ", @ARGV), "\n";
106 print OUT " */\n";
107 print OUT "\n";
108 print OUT "#include \"tables.h\"\n";
109 print OUT "#include \"nasmlib.h\"\n";
110 print OUT "#include \"hashtbl.h\"\n";
111 print OUT "#include \"outform.h\"\n";
112 print OUT "\n";
114 my $name = undef;
115 my $npkg = 0;
116 my @pkg_list = ();
117 my %pkg_number = ();
118 my $pkg;
119 my @out_list = ();
120 my @std_list = ();
121 my $outfmt;
122 my $lastname;
123 my $z;
125 my @pptok_list = sort { $pptok_hash{$a} <=> $pptok_hash{$b} } keys %pptok_hash;
126 my %pnum;
128 foreach my $pt (@pptok_list) {
129 my $n = $pptok_hash{$pt};
130 if ($pt !~ /[A-Z]/ && $n < 256-96) {
131 $n = ($n+128) & 255;
132 (my $et = $pt) =~ s/^\%/p_/;
133 printf OUT "#define %-24s %3d\n", $et, $n;
134 $pnum{$pt} = $n;
135 $pname[$n] = $et;
138 printf OUT "#define %-24s %3d\n\n", 'EOL', 127;
140 foreach $args ( @ARGV ) {
141 my @file_list = glob ( $args );
142 foreach $fname ( @file_list ) {
143 open(INPUT,'<', $fname) or die "$0: $fname: $!\n";
144 while (<INPUT>) {
145 $line++;
146 chomp;
147 while (/^(.*)\\$/) {
148 $_ = $1;
149 $_ .= <INPUT>;
150 chomp;
151 $line++;
153 s/^\s*(([^\'\"\;]|\"[^\"]*\"|\'[^\']*\')*?)\s*(\;.*)?$/$1/;
154 s/\s+/ /g;
155 next if ($_ eq '');
157 if (m/^OUT:\s*(\S.*)$/) {
158 undef $pkg;
159 my @out_alias = split(/\s+/, $1);
160 if (defined($name)) {
161 printf OUT " /* %4d */ EOL\n", $index++;
162 print OUT "};\n#endif\n";
163 undef $name;
165 $index = 0;
166 print OUT "\n";
167 my $pfx = '#if';
168 foreach my $al (@out_alias) {
169 print OUT $pfx, " defined(OF_\U${al}\E)";
170 $pfx = ' ||';
172 $name = $out_alias[0] . '_stdmac';
173 print OUT "\nconst unsigned char ${name}[] = {\n";
174 print OUT " /* From $fname */\n";
175 $lastname = $fname;
176 } elsif (m/^STD:\s*(\S+)$/) {
177 undef $pkg;
178 my $std = $1;
179 if (defined($name)) {
180 printf OUT " /* %4d */ EOL\n", $index++;
181 print OUT "};\n#endif\n";
182 undef $name;
184 $index = 0;
185 print OUT "\n#if 1";
186 $name = 'nasm_stdmac_' . $std;
187 print OUT "\nconst unsigned char ${name}[] = {\n";
188 print OUT " /* From $fname */\n";
189 $lastname = $fname;
190 } elsif (m/^USE:\s*(\S+)$/) {
191 $pkg = $1;
192 if (defined($pkg_number{$pkg})) {
193 die "$0: $fname: duplicate package: $pkg\n";
195 if (defined($name)) {
196 printf OUT " /* %4d */ EOL\n", $index++;
197 print OUT "};\n#endif\n";
198 undef $name;
200 $index = 0;
201 print OUT "\n#if 1";
202 $name = 'nasm_usemac_' . $pkg;
203 print OUT "\nstatic const unsigned char ${name}[] = {\n";
204 print OUT " /* From $fname */\n";
205 $lastname = $fname;
206 push(@pkg_list, $pkg);
207 $pkg_number{$pkg} = $npkg++;
208 $z = pack("C", $pnum{'%define'})."__?USE_\U$pkg\E?__";
209 printf OUT " /* %4d */ %sEOL,\n", $index, charcify($z);
210 $z = pack("C", $pnum{'%defalias'})."__USE_\U$pkg\E__ __?USE\U$pkg\E?__";
211 printf OUT " /* %4d */ %sEOL,\n", $index, charcify($z);
212 $index += length($z)+1;
213 } else {
214 my($s1, $s2, $pd, $ws);
216 if (!defined($name)) {
217 die "$0: $fname: macro declarations outside a known block\n";
220 $s1 = $_;
221 $s2 = '';
222 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
223 $s2 .= "$'";
224 $pd = $1;
225 $ws = $3;
226 $s1 = $4;
227 if (defined($pnum{$pd})) {
228 $s2 .= pack("C", $pnum{$pd});
229 } else {
230 $s2 .= $pd.$ws;
233 $s2 .= $s1;
234 if (length($s2) > 0) {
235 if ($lastname ne $fname) {
236 print OUT "\n /* From $fname */\n";
237 $lastname = $fname;
239 printf OUT " /* %4d */ %sEOL,\n",
240 $index, charcify($s2);
241 $index += length($s2)+1;
245 close(INPUT);
249 if (defined($name)) {
250 printf OUT " /* %4d */ EOL\n", $index++;
251 print OUT "};\n#endif\n";
252 undef $name;
255 my @hashinfo = gen_perfect_hash(\%pkg_number);
256 if (!@hashinfo) {
257 die "$0: no hash found\n";
259 # Paranoia...
260 verify_hash_table(\%pkg_number, \@hashinfo);
261 my ($n, $sv, $g) = @hashinfo;
262 die if ($n & ($n-1));
264 printf OUT "const int use_package_count = %d;\n\n", $npkg;
266 print OUT "const struct use_package *nasm_find_use_package(const char *name)\n";
267 print OUT "{\n";
268 print OUT " static const struct use_package packages[$npkg] = {\n";
269 my $ix = 0;
270 foreach $pkg (@pkg_list) {
271 printf OUT " { \"%s\", nasm_usemac_%s, %d },\n",
272 $pkg, $pkg, $ix++;
274 print OUT " };\n";
276 # Put a large value in unused slots. This makes it extremely unlikely
277 # that any combination that involves unused slot will pass the range test.
278 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
279 print OUT "#define UNUSED_HASH_ENTRY (65535/3)\n";
281 print OUT " static const int16_t hash1[$n] = {\n";
282 for ($i = 0; $i < $n; $i++) {
283 my $h = ${$g}[$i*2+0];
284 print OUT " ", defined($h) ? $h : 'UNUSED_HASH_ENTRY', ",\n";
286 print OUT " };\n";
288 print OUT " static const int16_t hash2[$n] = {\n";
289 for ($i = 0; $i < $n; $i++) {
290 my $h = ${$g}[$i*2+1];
291 print OUT " ", defined($h) ? $h : 'UNUSED_HASH_ENTRY', ",\n";
293 print OUT " };\n";
295 print OUT " uint32_t k1, k2;\n";
296 print OUT " uint64_t crc;\n";
297 # For correct overflow behavior, "ix" should be unsigned of the same
298 # width as the hash arrays.
299 print OUT " uint16_t ix;\n";
300 print OUT "\n";
302 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), name);\n",
303 $$sv[0], $$sv[1];
304 print OUT " k1 = (uint32_t)crc;\n";
305 print OUT " k2 = (uint32_t)(crc >> 32);\n";
306 print OUT "\n";
307 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
308 printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
309 print OUT " return NULL;\n";
310 print OUT "\n";
311 print OUT " if (nasm_stricmp(packages[ix].package, name))\n";
312 print OUT " return NULL;\n";
313 print OUT "\n";
314 print OUT " return &packages[ix];\n";
315 print OUT "}\n";
317 close(OUT);