Apply patch from BR 1197827
[nasm.git] / macros.pl
blobdd9c9618399f6bb802b368724ab716fc7f21dd97
1 #!/usr/bin/perl
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.
10 require 'phash.ph';
11 require 'pptok.ph';
13 use bytes;
15 my $fname;
16 my $line = 0;
17 my $index = 0;
18 my $tasm_count = 0;
21 # Print out a string as a character array
23 sub charcify(@) {
24 my $l = '';
25 my $c, $o;
26 foreach $o (unpack("C*", join('',@_))) {
27 $c = pack("C", $o);
28 if ($o < 32 || $o > 126 || $c eq '"' || $c eq "\\") {
29 $l .= sprintf("%3d,", $o);
30 } else {
31 $l .= "\'".$c."\',";
34 return $l;
38 # Generate macros.c
40 open(OUT,"> macros.c\0") or die "unable to open macros.c\n";
42 print OUT "/*\n";
43 print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
44 print OUT " * ", join("\n * ", @ARGV), "\n";
45 print OUT " */\n";
46 print OUT "\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";
51 print OUT "\n";
52 print OUT "#if 1\n";
53 print OUT "const unsigned char nasm_stdmac[] = {";
55 my $npkg = 0;
56 my @pkg_list = ();
57 my %pkg_number = ();
58 my $pkg;
59 my @out_list = ();
60 my $outfmt;
61 my $lastname;
62 my $z;
64 foreach $fname ( @ARGV ) {
65 open(INPUT,"< $fname\0") or die "$0: $fname: $!\n";
66 while (<INPUT>) {
67 $line++;
68 chomp;
69 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
70 $tasm_count = $index;
71 print OUT " /* End of TASM macros */\n";
72 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
73 undef $pkg;
74 my @out_alias = split(/\s+/, $1);
75 printf OUT " /* %4d */ 0\n", $index++;
76 print OUT "};\n#endif\n";
77 $index = 0;
78 print OUT "\n";
79 my $pfx = '#if';
80 foreach my $al (@out_alias) {
81 print OUT $pfx, " defined(OF_\U${al}\E)";
82 $pfx = ' ||';
84 printf OUT "\nconst unsigned char %s_stdmac[] = {\n", $out_alias[0];
85 print OUT " /* From $fname */\n";
86 $lastname = $fname;
87 push(@out_list, $out_alias[0]);
88 $out_index{$out_alias[0]} = $index;
89 } elsif (m/^USE:\s*(\S+)\s*$/) {
90 $pkg = $1;
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";
96 $index = 0;
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";
100 $lastname = $fname;
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;
108 $s1 = $1;
109 $s2 = '';
110 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
111 $s2 .= "$'";
112 $pd = $1;
113 $ws = $3;
114 $s1 = $4;
115 if (defined($pptok_hash{$pd}) &&
116 $pptok_hash{$pd} <= 127) {
117 $s2 .= pack("C", $pptok_hash{$pd}+128);
118 } else {
119 $s2 .= $pd.$ws;
122 $s2 .= $s1;
123 if (length($s2) > 0) {
124 if ($lastname ne $fname) {
125 print OUT "\n /* From $fname */\n";
126 $lastname = $fname;
128 printf OUT " /* %4d */ %s0,\n",
129 $index, charcify($s2);
130 $index += length($s2)+1;
132 } else {
133 die "$fname:$line: error unterminated quote";
136 close(INPUT);
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);
143 if (!@hashinfo) {
144 die "$0: no hash found\n";
146 # Paranoia...
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";
152 print OUT "{\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",
159 $pkg, $pkg;
161 print OUT " };\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";
173 print OUT " };\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";
180 print OUT " };\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";
187 print OUT "\n";
189 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
190 $$sv[0], $$sv[1];
191 print OUT " k1 = (uint32_t)crc;\n";
192 print OUT " k2 = (uint32_t)(crc >> 32);\n";
193 print OUT "\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";
197 print OUT "\n";
198 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
199 print OUT " return NULL;\n";
200 print OUT "\n";
201 print OUT " return packages[ix].macros;\n";
202 print OUT "}\n";
204 close(OUT);