3 # macros.pl produce macros.c from standard.mac
5 # The Netwide Assembler is copyright (C) 1996 Simon Tatham and
6 # Julian Hall. All rights reserved. The software is
7 # redistributable under the license given in the file "LICENSE"
8 # distributed in the NASM archive.
21 # Print out a string as a character array
26 foreach $o (unpack("C*", join('',@_))) {
28 if ($o < 32 || $o > 126 || $c eq '"' || $c eq "\\") {
29 $l .= sprintf("%3d,", $o);
40 open(OUT,"> macros.c\0") or die "unable to open macros.c\n";
43 print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
44 print OUT " * ", join("\n * ", @ARGV), "\n";
47 print OUT "#include \"tables.h\"\n";
48 print OUT "#include \"nasmlib.h\"\n";
49 print OUT "#include \"hashtbl.h\"\n";
51 print OUT "const unsigned char nasm_stdmac[] = {";
62 foreach $fname ( @ARGV ) {
63 open(INPUT,"< $fname\0") or die "$0: $fname: $!\n";
67 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
69 print OUT " /* End of TASM macros */\n";
70 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
72 my @out_alias = split(/\s+/, $1);
73 printf OUT " /* %4d */ 0\n", $index++;
76 printf OUT "const unsigned char %s_stdmac[] = {\n", $out_alias[0];
77 print OUT " /* From $fname */\n";
79 push(@out_list, $out_alias[0]);
80 $out_index{$out_alias[0]} = $index;
81 } elsif (m/^USE:\s*(\S+)\s*$/) {
83 if (defined($pkg_number{$pkg})) {
84 die "$0: $fname: duplicate package: $pkg\n";
86 printf OUT " /* %4d */ 0\n", $index++;
89 printf OUT "static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
90 print OUT " /* From $fname */\n";
92 push(@pkg_list, $pkg);
93 $pkg_number{$pkg} = $npkg++;
94 $z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
95 printf OUT " /* %4d */ %s0,\n", $index, charcify($z);
96 $index += length($z)+1;
97 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
98 my $s1, $s2, $pd, $ws;
101 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
106 if (defined($pptok_hash{$pd}) &&
107 $pptok_hash{$pd} <= 127) {
108 $s2 .= pack("C", $pptok_hash{$pd}+128);
114 if (length($s2) > 0) {
115 if ($lastname ne $fname) {
116 print OUT "\n /* From $fname */\n";
119 printf OUT " /* %4d */ %s0,\n",
120 $index, charcify($s2);
121 $index += length($s2)+1;
124 die "$fname:$line: error unterminated quote";
129 printf OUT " /* %4d */ 0\n};\n\n", $index++;
130 print OUT "const unsigned char * const nasm_stdmac_after_tasm = ",
131 "&nasm_stdmac[$tasm_count];\n\n";
133 my @hashinfo = gen_perfect_hash(\%pkg_number);
135 die "$0: no hash found\n";
138 verify_hash_table(\%pkg_number, \@hashinfo);
139 my ($n, $sv, $g) = @hashinfo;
140 die if ($n & ($n-1));
142 print OUT "const unsigned char *nasm_stdmac_find_package(const char *package)\n";
144 print OUT " static const struct {\n";
145 print OUT " const char *package;\n";
146 print OUT " const unsigned char *macros;\n";
147 print OUT " } packages[$npkg] = {\n";
148 foreach $pkg (@pkg_list) {
149 printf OUT " { \"%s\", nasm_stdmac_%s },\n",
154 # Put a large value in unused slots. This makes it extremely unlikely
155 # that any combination that involves unused slot will pass the range test.
156 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
157 print OUT "#define UNUSED 16383\n";
159 print OUT " static const int16_t hash1[$n] = {\n";
160 for ($i = 0; $i < $n; $i++) {
161 my $h = ${$g}[$i*2+0];
162 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
166 print OUT " static const int16_t hash2[$n] = {\n";
167 for ($i = 0; $i < $n; $i++) {
168 my $h = ${$g}[$i*2+1];
169 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
173 print OUT " uint32_t k1, k2;\n";
174 print OUT " uint64_t crc;\n";
175 # For correct overflow behavior, "ix" should be unsigned of the same
176 # width as the hash arrays.
177 print OUT " uint16_t ix;\n";
180 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
182 print OUT " k1 = (uint32_t)crc;\n";
183 print OUT " k2 = (uint32_t)(crc >> 32);\n";
185 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
186 printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
187 print OUT " return NULL;\n";
189 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
190 print OUT " return NULL;\n";
192 print OUT " return packages[ix].macros;\n";