insns.pl - add handling of pp VEX/XOP fields
[nasm/sigaren-mirror.git] / macros.pl
blob2eb5167be0b95a85ea7f9212d03b6a0362a7017f
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 while (/^(.*)\\$/) {
70 $_ = $1;
71 $_ .= <INPUT>;
72 chomp;
73 $line++;
75 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
76 $tasm_count = $index;
77 print OUT " /* End of TASM macros */\n";
78 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
79 undef $pkg;
80 my @out_alias = split(/\s+/, $1);
81 printf OUT " /* %4d */ 0\n", $index++;
82 print OUT "};\n#endif\n";
83 $index = 0;
84 print OUT "\n";
85 my $pfx = '#if';
86 foreach my $al (@out_alias) {
87 print OUT $pfx, " defined(OF_\U${al}\E)";
88 $pfx = ' ||';
90 printf OUT "\nconst unsigned char %s_stdmac[] = {\n", $out_alias[0];
91 print OUT " /* From $fname */\n";
92 $lastname = $fname;
93 push(@out_list, $out_alias[0]);
94 $out_index{$out_alias[0]} = $index;
95 } elsif (m/^USE:\s*(\S+)\s*$/) {
96 $pkg = $1;
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";
102 $index = 0;
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";
106 $lastname = $fname;
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;
114 $s1 = $1;
115 $s2 = '';
116 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
117 $s2 .= "$'";
118 $pd = $1;
119 $ws = $3;
120 $s1 = $4;
121 if (defined($pptok_hash{$pd}) &&
122 $pptok_hash{$pd} <= 127) {
123 $s2 .= pack("C", $pptok_hash{$pd}+128);
124 } else {
125 $s2 .= $pd.$ws;
128 $s2 .= $s1;
129 if (length($s2) > 0) {
130 if ($lastname ne $fname) {
131 print OUT "\n /* From $fname */\n";
132 $lastname = $fname;
134 printf OUT " /* %4d */ %s0,\n",
135 $index, charcify($s2);
136 $index += length($s2)+1;
138 } else {
139 die "$fname:$line: error unterminated quote";
142 close(INPUT);
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);
149 if (!@hashinfo) {
150 die "$0: no hash found\n";
152 # Paranoia...
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";
158 print OUT "{\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",
165 $pkg, $pkg;
167 print OUT " };\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";
179 print OUT " };\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";
186 print OUT " };\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";
193 print OUT "\n";
195 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
196 $$sv[0], $$sv[1];
197 print OUT " k1 = (uint32_t)crc;\n";
198 print OUT " k2 = (uint32_t)(crc >> 32);\n";
199 print OUT "\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";
203 print OUT "\n";
204 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
205 print OUT " return NULL;\n";
206 print OUT "\n";
207 print OUT " return packages[ix].macros;\n";
208 print OUT "}\n";
210 close(OUT);