assemble: Use nasm_ error helpers
[nasm.git] / macros / macros.pl
blobd4bfa9fa9f3ad9ed1d19c810da6016ed6fe845fd
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 'asm/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 my $space = 1;
56 my $quote = 0;
58 foreach $o (unpack("C*", join('',@_))) {
59 $c = pack("C", $o);
60 if ($quote) {
61 if ($o == $quote) {
62 $quote = 0;
64 } elsif ($c =~ /^[\'\"\`]$/) {
65 $quote = $o;
66 } else {
67 if ($c =~ /\s/) {
68 next if ($space);
69 $o = 32;
70 $c = ' ';
71 $space = 1;
72 } elsif ($o > 126) {
73 $space = 1; # Implicit space after compacted directive
74 } else {
75 $space = 0;
79 if ($o < 32 || $o > 126 || $c eq '"' || $c eq "\\") {
80 $l .= sprintf("%3d,", $o);
81 } else {
82 $c =~ s/\'/\\'/; # << sanitize single quote.
83 $l .= "\'".$c."\',";
86 return $l;
91 # Generate macros.c
93 open(OUT, '>', 'macros/macros.c') or die "unable to open macros.c\n";
95 print OUT "/*\n";
96 print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
97 print OUT " * ", join("\n * ", @ARGV), "\n";
98 print OUT " */\n";
99 print OUT "\n";
100 print OUT "#include \"tables.h\"\n";
101 print OUT "#include \"nasmlib.h\"\n";
102 print OUT "#include \"hashtbl.h\"\n";
103 print OUT "#include \"outform.h\"\n";
104 print OUT "\n";
106 my $name = undef;
107 my $npkg = 0;
108 my @pkg_list = ();
109 my %pkg_number = ();
110 my $pkg;
111 my @out_list = ();
112 my $outfmt;
113 my $lastname;
114 my $z;
116 foreach $args ( @ARGV ) {
117 my @file_list = glob ( $args );
118 foreach $fname ( @file_list ) {
119 open(INPUT,'<', $fname) or die "$0: $fname: $!\n";
120 while (<INPUT>) {
121 $line++;
122 chomp;
123 while (/^(.*)\\$/) {
124 $_ = $1;
125 $_ .= <INPUT>;
126 chomp;
127 $line++;
129 if (m/^OUT:\s*(.*\S)\s*$/) {
130 undef $pkg;
131 my @out_alias = split(/\s+/, $1);
132 if (defined($name)) {
133 printf OUT " /* %4d */ 0\n", $index++;
134 print OUT "};\n#endif\n";
135 undef $name;
137 $index = 0;
138 print OUT "\n";
139 my $pfx = '#if';
140 foreach my $al (@out_alias) {
141 print OUT $pfx, " defined(OF_\U${al}\E)";
142 $pfx = ' ||';
144 $name = $out_alias[0] . '_stdmac';
145 print OUT "\nconst unsigned char ${name}[] = {\n";
146 print OUT " /* From $fname */\n";
147 $lastname = $fname;
148 push(@out_list, $out_alias[0]);
149 $out_index{$out_alias[0]} = $index;
150 } elsif (m/^STD:\s*(.*\S)\s*$/) {
151 undef $pkg;
152 my @out_alias = split(/\s+/, $1);
153 if (defined($name)) {
154 printf OUT " /* %4d */ 0\n", $index++;
155 print OUT "};\n#endif\n";
156 undef $name;
158 $index = 0;
159 print OUT "\n#if 1";
160 $name = 'nasm_stdmac_' . $out_alias[0];
161 print OUT "\nconst unsigned char ${name}[] = {\n";
162 print OUT " /* From $fname */\n";
163 $lastname = $fname;
164 push(@std_list, $out_alias[0]);
165 $std_index{$std_alias[0]} = $index;
166 } elsif (m/^USE:\s*(\S+)\s*$/) {
167 $pkg = $1;
168 if (defined($pkg_number{$pkg})) {
169 die "$0: $fname: duplicate package: $pkg\n";
171 if (defined($name)) {
172 printf OUT " /* %4d */ 0\n", $index++;
173 print OUT "};\n#endif\n";
174 undef $name;
176 $index = 0;
177 print OUT "\n#if 1";
178 $name = 'nasm_usemac_' . $pkg;
179 print OUT "\nstatic const unsigned char ${name}[] = {\n";
180 print OUT " /* From $fname */\n";
181 $lastname = $fname;
182 push(@pkg_list, $pkg);
183 $pkg_number{$pkg} = $npkg++;
184 $z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
185 printf OUT " /* %4d */ %s0,\n", $index, charcify($z);
186 $index += length($z)+1;
187 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
188 my $s1, $s2, $pd, $ws;
190 if (!defined($name)) {
191 die "$0: $fname: macro declarations outside a known block\n";
194 $s1 = $1;
195 $s2 = '';
196 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
197 $s2 .= "$'";
198 $pd = $1;
199 $ws = $3;
200 $s1 = $4;
201 if (defined($pptok_hash{$pd}) &&
202 $pptok_hash{$pd} <= 127) {
203 $s2 .= pack("C", $pptok_hash{$pd}+128);
204 } else {
205 $s2 .= $pd.$ws;
208 $s2 .= $s1;
209 if (length($s2) > 0) {
210 if ($lastname ne $fname) {
211 print OUT "\n /* From $fname */\n";
212 $lastname = $fname;
214 printf OUT " /* %4d */ %s0,\n",
215 $index, charcify($s2);
216 $index += length($s2)+1;
218 } else {
219 die "$fname:$line: error unterminated quote";
222 close(INPUT);
226 if (defined($name)) {
227 printf OUT " /* %4d */ 0\n", $index++;
228 print OUT "};\n#endif\n";
229 undef $name;
232 my @hashinfo = gen_perfect_hash(\%pkg_number);
233 if (!@hashinfo) {
234 die "$0: no hash found\n";
236 # Paranoia...
237 verify_hash_table(\%pkg_number, \@hashinfo);
238 my ($n, $sv, $g) = @hashinfo;
239 die if ($n & ($n-1));
241 print OUT "const unsigned char *nasm_stdmac_find_package(const char *package)\n";
242 print OUT "{\n";
243 print OUT " static const struct {\n";
244 print OUT " const char *package;\n";
245 print OUT " const unsigned char *macros;\n";
246 print OUT " } packages[$npkg] = {\n";
247 foreach $pkg (@pkg_list) {
248 printf OUT " { \"%s\", nasm_usemac_%s },\n",
249 $pkg, $pkg;
251 print OUT " };\n";
253 # Put a large value in unused slots. This makes it extremely unlikely
254 # that any combination that involves unused slot will pass the range test.
255 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
256 print OUT "#define UNUSED (65535/3)\n";
258 print OUT " static const int16_t hash1[$n] = {\n";
259 for ($i = 0; $i < $n; $i++) {
260 my $h = ${$g}[$i*2+0];
261 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
263 print OUT " };\n";
265 print OUT " static const int16_t hash2[$n] = {\n";
266 for ($i = 0; $i < $n; $i++) {
267 my $h = ${$g}[$i*2+1];
268 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
270 print OUT " };\n";
272 print OUT " uint32_t k1, k2;\n";
273 print OUT " uint64_t crc;\n";
274 # For correct overflow behavior, "ix" should be unsigned of the same
275 # width as the hash arrays.
276 print OUT " uint16_t ix;\n";
277 print OUT "\n";
279 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
280 $$sv[0], $$sv[1];
281 print OUT " k1 = (uint32_t)crc;\n";
282 print OUT " k2 = (uint32_t)(crc >> 32);\n";
283 print OUT "\n";
284 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
285 printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
286 print OUT " return NULL;\n";
287 print OUT "\n";
288 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
289 print OUT " return NULL;\n";
290 print OUT "\n";
291 print OUT " return packages[ix].macros;\n";
292 print OUT "}\n";
294 close(OUT);