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";
50 print OUT "#include \"outform.h\"\n";
53 print OUT "const unsigned char nasm_stdmac[] = {";
64 foreach $fname ( @ARGV ) {
65 open(INPUT,"< $fname\0") or die "$0: $fname: $!\n";
75 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
77 print OUT " /* End of TASM macros */\n";
78 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
80 my @out_alias = split(/\s+/, $1);
81 printf OUT " /* %4d */ 0\n", $index++;
82 print OUT "};\n#endif\n";
86 foreach my $al (@out_alias) {
87 print OUT $pfx, " defined(OF_\U${al}\E)";
90 printf OUT "\nconst unsigned char %s_stdmac[] = {\n", $out_alias[0];
91 print OUT " /* From $fname */\n";
93 push(@out_list, $out_alias[0]);
94 $out_index{$out_alias[0]} = $index;
95 } elsif (m/^USE:\s*(\S+)\s*$/) {
97 if (defined($pkg_number{$pkg})) {
98 die "$0: $fname: duplicate package: $pkg\n";
100 printf OUT " /* %4d */ 0\n", $index++;
101 print OUT "};\n#endif\n";
103 print OUT "\n#if 1\n";
104 printf OUT "static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
105 print OUT " /* From $fname */\n";
107 push(@pkg_list, $pkg);
108 $pkg_number{$pkg} = $npkg++;
109 $z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
110 printf OUT " /* %4d */ %s0,\n", $index, charcify($z);
111 $index += length($z)+1;
112 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
113 my $s1, $s2, $pd, $ws;
116 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
121 if (defined($pptok_hash{$pd}) &&
122 $pptok_hash{$pd} <= 127) {
123 $s2 .= pack("C", $pptok_hash{$pd}+128);
129 if (length($s2) > 0) {
130 if ($lastname ne $fname) {
131 print OUT "\n /* From $fname */\n";
134 printf OUT " /* %4d */ %s0,\n",
135 $index, charcify($s2);
136 $index += length($s2)+1;
139 die "$fname:$line: error unterminated quote";
144 printf OUT " /* %4d */ 0\n};\n#endif\n\n", $index++;
145 print OUT "const unsigned char * const nasm_stdmac_after_tasm = ",
146 "&nasm_stdmac[$tasm_count];\n\n";
148 my @hashinfo = gen_perfect_hash(\%pkg_number);
150 die "$0: no hash found\n";
153 verify_hash_table(\%pkg_number, \@hashinfo);
154 my ($n, $sv, $g) = @hashinfo;
155 die if ($n & ($n-1));
157 print OUT "const unsigned char *nasm_stdmac_find_package(const char *package)\n";
159 print OUT " static const struct {\n";
160 print OUT " const char *package;\n";
161 print OUT " const unsigned char *macros;\n";
162 print OUT " } packages[$npkg] = {\n";
163 foreach $pkg (@pkg_list) {
164 printf OUT " { \"%s\", nasm_stdmac_%s },\n",
169 # Put a large value in unused slots. This makes it extremely unlikely
170 # that any combination that involves unused slot will pass the range test.
171 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
172 print OUT "#define UNUSED 16383\n";
174 print OUT " static const int16_t hash1[$n] = {\n";
175 for ($i = 0; $i < $n; $i++) {
176 my $h = ${$g}[$i*2+0];
177 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
181 print OUT " static const int16_t hash2[$n] = {\n";
182 for ($i = 0; $i < $n; $i++) {
183 my $h = ${$g}[$i*2+1];
184 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
188 print OUT " uint32_t k1, k2;\n";
189 print OUT " uint64_t crc;\n";
190 # For correct overflow behavior, "ix" should be unsigned of the same
191 # width as the hash arrays.
192 print OUT " uint16_t ix;\n";
195 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
197 print OUT " k1 = (uint32_t)crc;\n";
198 print OUT " k2 = (uint32_t)(crc >> 32);\n";
200 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
201 printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
202 print OUT " return NULL;\n";
204 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
205 print OUT " return NULL;\n";
207 print OUT " return packages[ix].macros;\n";