2 ## --------------------------------------------------------------------------
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.
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
40 require 'asm/pptok.ph';
50 # Default names for various bytes
51 for (my $o = 0; $o < 256; $o++) {
53 if ($o < 32 || $o > 126) {
54 $pname[$o] = sprintf("%d", $o);
55 } elsif ($c =~ /^[\'\"\\]$/) {
56 $pname[$o] = "\'\\".$c."\'";
58 $pname[$o] = "\'".$c."\'";
63 # Print out a string as a character array
71 foreach $o (unpack("C*", join('',@_))) {
77 } elsif ($c =~ /^[\'\"\`]$/) {
85 } elsif ($o < 32 || $o > 126) {
86 $space = 1; # Implicit space after compacted directive
101 open(OUT
, '>', 'macros/macros.c') or die "unable to open macros.c\n";
104 print OUT
" * Do not edit - this file auto-generated by macros.pl from:\n";
105 print OUT
" * ", join("\n * ", @ARGV), "\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";
125 my @pptok_list = sort { $pptok_hash{$a} <=> $pptok_hash{$b} } keys %pptok_hash;
128 foreach my $pt (@pptok_list) {
129 my $n = $pptok_hash{$pt};
130 if ($pt !~ /[A-Z]/ && $n < 256-96) {
132 (my $et = $pt) =~ s/^\%/p_/;
133 printf OUT
"#define %-24s %3d\n", $et, $n;
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";
153 s/^\s*(([^\'\"\;]|\"[^\"]*\"|\'[^\']*\')*?)\s*(\;.*)?$/$1/;
157 if (m/^OUT:\s*(\S.*)$/) {
159 my @out_alias = split(/\s+/, $1);
160 if (defined($name)) {
161 printf OUT
" /* %4d */ EOL\n", $index++;
162 print OUT
"};\n#endif\n";
168 foreach my $al (@out_alias) {
169 print OUT
$pfx, " defined(OF_\U${al}\E)";
172 $name = $out_alias[0] . '_stdmac';
173 print OUT
"\nconst unsigned char ${name}[] = {\n";
174 print OUT
" /* From $fname */\n";
176 } elsif (m/^STD:\s*(\S+)$/) {
179 if (defined($name)) {
180 printf OUT
" /* %4d */ EOL\n", $index++;
181 print OUT
"};\n#endif\n";
186 $name = 'nasm_stdmac_' . $std;
187 print OUT
"\nconst unsigned char ${name}[] = {\n";
188 print OUT
" /* From $fname */\n";
190 } elsif (m/^USE:\s*(\S+)$/) {
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";
202 $name = 'nasm_usemac_' . $pkg;
203 print OUT
"\nstatic const unsigned char ${name}[] = {\n";
204 print OUT
" /* From $fname */\n";
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;
214 my($s1, $s2, $pd, $ws);
216 if (!defined($name)) {
217 die "$0: $fname: macro declarations outside a known block\n";
222 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
227 if (defined($pnum{$pd})) {
228 $s2 .= pack("C", $pnum{$pd});
234 if (length($s2) > 0) {
235 if ($lastname ne $fname) {
236 print OUT
"\n /* From $fname */\n";
239 printf OUT
" /* %4d */ %sEOL,\n",
240 $index, charcify
($s2);
241 $index += length($s2)+1;
249 if (defined($name)) {
250 printf OUT
" /* %4d */ EOL\n", $index++;
251 print OUT
"};\n#endif\n";
255 my @hashinfo = gen_perfect_hash
(\
%pkg_number);
257 die "$0: no hash found\n";
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";
268 print OUT
" static const struct use_package packages[$npkg] = {\n";
270 foreach $pkg (@pkg_list) {
271 printf OUT
" { \"%s\", nasm_usemac_%s, %d },\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";
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";
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";
302 printf OUT
" crc = crc64i(UINT64_C(0x%08x%08x), name);\n",
304 print OUT
" k1 = (uint32_t)crc;\n";
305 print OUT
" k2 = (uint32_t)(crc >> 32);\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";
311 print OUT
" if (nasm_stricmp(packages[ix].package, name))\n";
312 print OUT
" return NULL;\n";
314 print OUT
" return &packages[ix];\n";