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
50 # Print out a string as a character array
58 foreach $o (unpack("C*", join('',@_))) {
64 } elsif ($c =~ /^[\'\"\`]$/) {
73 $space = 1; # Implicit space after compacted directive
79 if ($o < 32 || $o > 126 || $c eq '"' || $c eq "\\") {
80 $l .= sprintf("%3d,", $o);
82 $c =~ s/\'/\\'/; # << sanitize single quote.
93 open(OUT
,"> macros.c\0") or die "unable to open macros.c\n";
96 print OUT
" * Do not edit - this file auto-generated by macros.pl from:\n";
97 print OUT
" * ", join("\n * ", @ARGV), "\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 \"output/outform.h\"\n";
106 print OUT
"const unsigned char nasm_stdmac[] = {";
117 foreach $args ( @ARGV ) {
118 my @file_list = glob ( $args );
119 foreach $fname ( @file_list ) {
120 open(INPUT
,"< $fname\0") or die "$0: $fname: $!\n";
130 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
131 $tasm_count = $index;
132 print OUT
" /* End of TASM macros */\n";
133 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
135 my @out_alias = split(/\s+/, $1);
136 printf OUT
" /* %4d */ 0\n", $index++;
137 print OUT
"};\n#endif\n";
141 foreach my $al (@out_alias) {
142 print OUT
$pfx, " defined(OF_\U${al}\E)";
145 printf OUT
"\nconst unsigned char %s_stdmac[] = {\n", $out_alias[0];
146 print OUT
" /* From $fname */\n";
148 push(@out_list, $out_alias[0]);
149 $out_index{$out_alias[0]} = $index;
150 } elsif (m/^USE:\s*(\S+)\s*$/) {
152 if (defined($pkg_number{$pkg})) {
153 die "$0: $fname: duplicate package: $pkg\n";
155 printf OUT
" /* %4d */ 0\n", $index++;
156 print OUT
"};\n#endif\n";
158 print OUT
"\n#if 1\n";
159 printf OUT
"static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
160 print OUT
" /* From $fname */\n";
162 push(@pkg_list, $pkg);
163 $pkg_number{$pkg} = $npkg++;
164 $z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
165 printf OUT
" /* %4d */ %s0,\n", $index, charcify
($z);
166 $index += length($z)+1;
167 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
168 my $s1, $s2, $pd, $ws;
171 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
176 if (defined($pptok_hash{$pd}) &&
177 $pptok_hash{$pd} <= 127) {
178 $s2 .= pack("C", $pptok_hash{$pd}+128);
184 if (length($s2) > 0) {
185 if ($lastname ne $fname) {
186 print OUT
"\n /* From $fname */\n";
189 printf OUT
" /* %4d */ %s0,\n",
190 $index, charcify
($s2);
191 $index += length($s2)+1;
194 die "$fname:$line: error unterminated quote";
200 printf OUT
" /* %4d */ 0\n};\n#endif\n\n", $index++;
201 print OUT
"const unsigned char * const nasm_stdmac_after_tasm = ",
202 "&nasm_stdmac[$tasm_count];\n\n";
204 my @hashinfo = gen_perfect_hash
(\
%pkg_number);
206 die "$0: no hash found\n";
209 verify_hash_table
(\
%pkg_number, \
@hashinfo);
210 my ($n, $sv, $g) = @hashinfo;
211 die if ($n & ($n-1));
213 print OUT
"const unsigned char *nasm_stdmac_find_package(const char *package)\n";
215 print OUT
" static const struct {\n";
216 print OUT
" const char *package;\n";
217 print OUT
" const unsigned char *macros;\n";
218 print OUT
" } packages[$npkg] = {\n";
219 foreach $pkg (@pkg_list) {
220 printf OUT
" { \"%s\", nasm_stdmac_%s },\n",
225 # Put a large value in unused slots. This makes it extremely unlikely
226 # that any combination that involves unused slot will pass the range test.
227 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
228 print OUT
"#define UNUSED (65535/3)\n";
230 print OUT
" static const int16_t hash1[$n] = {\n";
231 for ($i = 0; $i < $n; $i++) {
232 my $h = ${$g}[$i*2+0];
233 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
237 print OUT
" static const int16_t hash2[$n] = {\n";
238 for ($i = 0; $i < $n; $i++) {
239 my $h = ${$g}[$i*2+1];
240 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
244 print OUT
" uint32_t k1, k2;\n";
245 print OUT
" uint64_t crc;\n";
246 # For correct overflow behavior, "ix" should be unsigned of the same
247 # width as the hash arrays.
248 print OUT
" uint16_t ix;\n";
251 printf OUT
" crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
253 print OUT
" k1 = (uint32_t)crc;\n";
254 print OUT
" k2 = (uint32_t)(crc >> 32);\n";
256 printf OUT
" ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
257 printf OUT
" if (ix >= %d)\n", scalar(@pkg_list);
258 print OUT
" return NULL;\n";
260 print OUT
" if (nasm_stricmp(packages[ix].package, package))\n";
261 print OUT
" return NULL;\n";
263 print OUT
" return packages[ix].macros;\n";