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";
69 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
71 print OUT
" /* End of TASM macros */\n";
72 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
74 my @out_alias = split(/\s+/, $1);
75 printf OUT
" /* %4d */ 0\n", $index++;
76 print OUT
"};\n#endif\n";
80 foreach my $al (@out_alias) {
81 print OUT
$pfx, " defined(OF_\U${al}\E)";
84 printf OUT
"\nconst unsigned char %s_stdmac[] = {\n", $out_alias[0];
85 print OUT
" /* From $fname */\n";
87 push(@out_list, $out_alias[0]);
88 $out_index{$out_alias[0]} = $index;
89 } elsif (m/^USE:\s*(\S+)\s*$/) {
91 if (defined($pkg_number{$pkg})) {
92 die "$0: $fname: duplicate package: $pkg\n";
94 printf OUT
" /* %4d */ 0\n", $index++;
95 print OUT
"};\n#endif\n";
97 print OUT
"\n#if 1\n";
98 printf OUT
"static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
99 print OUT
" /* From $fname */\n";
101 push(@pkg_list, $pkg);
102 $pkg_number{$pkg} = $npkg++;
103 $z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
104 printf OUT
" /* %4d */ %s0,\n", $index, charcify
($z);
105 $index += length($z)+1;
106 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
107 my $s1, $s2, $pd, $ws;
110 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
115 if (defined($pptok_hash{$pd}) &&
116 $pptok_hash{$pd} <= 127) {
117 $s2 .= pack("C", $pptok_hash{$pd}+128);
123 if (length($s2) > 0) {
124 if ($lastname ne $fname) {
125 print OUT
"\n /* From $fname */\n";
128 printf OUT
" /* %4d */ %s0,\n",
129 $index, charcify
($s2);
130 $index += length($s2)+1;
133 die "$fname:$line: error unterminated quote";
138 printf OUT
" /* %4d */ 0\n};\n#endif\n\n", $index++;
139 print OUT
"const unsigned char * const nasm_stdmac_after_tasm = ",
140 "&nasm_stdmac[$tasm_count];\n\n";
142 my @hashinfo = gen_perfect_hash
(\
%pkg_number);
144 die "$0: no hash found\n";
147 verify_hash_table
(\
%pkg_number, \
@hashinfo);
148 my ($n, $sv, $g) = @hashinfo;
149 die if ($n & ($n-1));
151 print OUT
"const unsigned char *nasm_stdmac_find_package(const char *package)\n";
153 print OUT
" static const struct {\n";
154 print OUT
" const char *package;\n";
155 print OUT
" const unsigned char *macros;\n";
156 print OUT
" } packages[$npkg] = {\n";
157 foreach $pkg (@pkg_list) {
158 printf OUT
" { \"%s\", nasm_stdmac_%s },\n",
163 # Put a large value in unused slots. This makes it extremely unlikely
164 # that any combination that involves unused slot will pass the range test.
165 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
166 print OUT
"#define UNUSED 16383\n";
168 print OUT
" static const int16_t hash1[$n] = {\n";
169 for ($i = 0; $i < $n; $i++) {
170 my $h = ${$g}[$i*2+0];
171 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
175 print OUT
" static const int16_t hash2[$n] = {\n";
176 for ($i = 0; $i < $n; $i++) {
177 my $h = ${$g}[$i*2+1];
178 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
182 print OUT
" uint32_t k1, k2;\n";
183 print OUT
" uint64_t crc;\n";
184 # For correct overflow behavior, "ix" should be unsigned of the same
185 # width as the hash arrays.
186 print OUT
" uint16_t ix;\n";
189 printf OUT
" crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
191 print OUT
" k1 = (uint32_t)crc;\n";
192 print OUT
" k2 = (uint32_t)(crc >> 32);\n";
194 printf OUT
" ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
195 printf OUT
" if (ix >= %d)\n", scalar(@pkg_list);
196 print OUT
" return NULL;\n";
198 print OUT
" if (nasm_stricmp(packages[ix].package, package))\n";
199 print OUT
" return NULL;\n";
201 print OUT
" return packages[ix].macros;\n";