Update CHANGES to current delta from 2.03.x.
[nasm.git] / macros.pl
blobb1970cf5382cf4cb5ed89939b9dfc653720d88ac
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") 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 "\n";
51 print OUT "const unsigned char nasm_stdmac[] = {";
53 my $npkg = 0;
54 my @pkg_list = ();
55 my %pkg_number = ();
56 my $pkg;
57 my @out_list = ();
58 my $outfmt;
59 my $lastname;
60 my $z;
62 foreach $fname ( @ARGV ) {
63 open(INPUT,$fname) or die "unable to open $fname\n";
64 while (<INPUT>) {
65 $line++;
66 chomp;
67 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
68 $tasm_count = $index;
69 print OUT " /* End of TASM macros */\n";
70 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
71 undef $pkg;
72 my @out_alias = split(/\s+/, $1);
73 printf OUT " /* %4d */ 0\n", $index++;
74 print OUT "};\n";
75 $index = 0;
76 printf OUT "const unsigned char %s_stdmac[] = {\n", $out_alias[0];
77 print OUT " /* From $fname */\n";
78 $lastname = $fname;
79 push(@out_list, $out_alias[0]);
80 $out_index{$out_alias[0]} = $index;
81 } elsif (m/^USE:\s*(\S+)\s*$/) {
82 $pkg = $1;
83 if (defined($pkg_number{$pkg})) {
84 die "$0: $fname: duplicate package: $pkg\n";
86 printf OUT " /* %4d */ 0\n", $index++;
87 print OUT "};\n";
88 $index = 0;
89 printf OUT "static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
90 print OUT " /* From $fname */\n";
91 $lastname = $fname;
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;
99 $s1 = $1;
100 $s2 = '';
101 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
102 $s2 .= "$'";
103 $pd = $1;
104 $ws = $3;
105 $s1 = $4;
106 if (defined($pptok_hash{$pd}) &&
107 $pptok_hash{$pd} <= 127) {
108 $s2 .= pack("C", $pptok_hash{$pd}+128);
109 } else {
110 $s2 .= $pd.$ws;
113 $s2 .= $s1;
114 if (length($s2) > 0) {
115 if ($lastname ne $fname) {
116 print OUT "\n /* From $fname */\n";
117 $lastname = $fname;
119 printf OUT " /* %4d */ %s0,\n",
120 $index, charcify($s2);
121 $index += length($s2)+1;
123 } else {
124 die "$fname:$line: error unterminated quote";
127 close(INPUT);
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);
134 if (!@hashinfo) {
135 die "$0: no hash found\n";
137 # Paranoia...
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";
143 print OUT "{\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",
150 $pkg, $pkg;
152 print OUT " };\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";
164 print OUT " };\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";
171 print OUT " };\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";
178 print OUT "\n";
180 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
181 $$sv[0], $$sv[1];
182 print OUT " k1 = (uint32_t)crc;\n";
183 print OUT " k2 = (uint32_t)(crc >> 32);\n";
184 print OUT "\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";
188 print OUT "\n";
189 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
190 print OUT " return NULL;\n";
191 print OUT "\n";
192 print OUT " return packages[ix].macros;\n";
193 print OUT "}\n";
195 close(OUT);