NASM 0.99.05
[nasm/avx512.git] / insns.pl
blob90c00c072b7554d8b2bb6134d3000477dc95138a
1 #!/usr/bin/perl
2 #
3 # insns.pl produce insnsa.c, insnsd.c, insnsi.h, insnsn.c from insns.dat
5 # The Netwide Assembler is copyright (C) 1996 Simon Tatham and
6 # Julian Hall. All rights reserved. The software is
7 # redistributable under the licence given in the file "Licence"
8 # distributed in the NASM archive.
10 # Opcode prefixes which need their own opcode tables
11 # LONGER PREFIXES FIRST!
12 @disasm_prefixes = qw(0F24 0F25 0F38 0F3A 0F7A 0FA6 0FA7 0F);
14 print STDERR "Reading insns.dat...\n";
16 @args = ();
17 undef $output;
18 foreach $arg ( @ARGV ) {
19 if ( $arg =~ /^\-/ ) {
20 if ( $arg =~ /^\-([adin])$/ ) {
21 $output = $1;
22 } else {
23 die "$0: Unknown option: ${arg}\n";
25 } else {
26 push (@args, $arg);
30 $fname = "insns.dat" unless $fname = $args[0];
31 open (F, $fname) || die "unable to open $fname";
33 %dinstables = ();
35 $line = 0;
36 $insns = 0;
37 while (<F>) {
38 $line++;
39 next if /^\s*;/; # comments
40 chomp;
41 split;
42 next if $#_ == -1; # blank lines
43 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
44 ($formatted, $nd) = &format(@_);
45 if ($formatted) {
46 $insns++;
47 $aname = "aa_$_[0]";
48 push @$aname, $formatted;
50 if ( $_[0] =~ /cc$/ ) {
51 # Conditional instruction
52 $k_opcodes_cc{$_[0]}++;
53 } else {
54 # Unconditional instruction
55 $k_opcodes{$_[0]}++;
57 if ($formatted && !$nd) {
58 push @big, $formatted;
59 foreach $i (startseq($_[2])) {
60 if (!defined($dinstables{$i})) {
61 $dinstables{$i} = [];
63 push(@{$dinstables{$i}}, $#big);
68 close F;
70 @opcodes = sort keys(%k_opcodes);
71 @opcodes_cc = sort keys(%k_opcodes_cc);
73 if ( !defined($output) || $output eq 'a' ) {
74 print STDERR "Writing insnsa.c...\n";
76 open A, ">insnsa.c";
78 print A "/* This file auto-generated from insns.dat by insns.pl" .
79 " - don't edit it */\n\n";
80 print A "#include \"nasm.h\"\n";
81 print A "#include \"insns.h\"\n";
82 print A "\n";
84 foreach $i (@opcodes, @opcodes_cc) {
85 print A "static const struct itemplate instrux_${i}[] = {\n";
86 $aname = "aa_$i";
87 foreach $j (@$aname) {
88 print A " $j\n";
90 print A " ITEMPLATE_END\n};\n\n";
92 print A "const struct itemplate * const nasm_instructions[] = {\n";
93 foreach $i (@opcodes, @opcodes_cc) {
94 print A " instrux_${i},\n";
96 print A "};\n";
98 close A;
101 if ( !defined($output) || $output eq 'd' ) {
102 print STDERR "Writing insnsd.c...\n";
104 open D, ">insnsd.c";
106 print D "/* This file auto-generated from insns.dat by insns.pl" .
107 " - don't edit it */\n\n";
108 print D "#include \"nasm.h\"\n";
109 print D "#include \"insns.h\"\n";
110 print D "\n";
112 print D "static const struct itemplate instrux[] = {\n";
113 $n = 0;
114 foreach $j (@big) {
115 printf D " /* %4d */ %s\n", $n++, $j;
117 print D "};\n";
119 foreach $h (sort(keys(%dinstables))) {
120 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
121 foreach $j (@{$dinstables{$h}}) {
122 print D " instrux + $j,\n";
124 print D "};\n";
127 foreach $h (@disasm_prefixes, '') {
128 $is_prefix{$h} = 1;
129 print D "\n";
130 print D "static " unless ($h eq '');
131 print D "const struct disasm_index ";
132 print D ($h eq '') ? 'itable' : "itable_$h";
133 print D "[256] = {\n";
134 for ($c = 0; $c < 256; $c++) {
135 $nn = sprintf("%s%02X", $h, $c);
136 if ($is_prefix{$nn}) {
137 die "$0: ambiguous decoding of $nn\n"
138 if (defined($dinstables{$nn}));
139 printf D " { itable_%s, -1 },\n", $nn;
140 } elsif (defined($dinstables{$nn})) {
141 printf D " { itable_%s, %u },\n",
142 $nn, scalar(@{$dinstables{$nn}});
143 } else {
144 printf D " { NULL, 0 },\n";
147 print D "};\n";
150 close D;
153 if ( !defined($output) || $output eq 'i' ) {
154 print STDERR "Writing insnsi.h...\n";
156 open I, ">insnsi.h";
158 print I "/* This file is auto-generated from insns.dat by insns.pl" .
159 " - don't edit it */\n\n";
160 print I "/* This file in included by nasm.h */\n\n";
162 print I "/* Instruction names */\n\n";
163 print I "#ifndef NASM_INSNSI_H\n";
164 print I "#define NASM_INSNSI_H 1\n\n";
165 print I "enum opcode {\n";
166 $maxlen = 0;
167 foreach $i (@opcodes, @opcodes_cc) {
168 print I "\tI_${i},\n";
169 $len = length($i);
170 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
171 $maxlen = $len if ( $len > $maxlen );
173 print I "\tI_none = -1\n";
174 print I "\n};\n\n";
175 print I "#define MAX_INSLEN ", $maxlen, "\n\n";
176 print I "#endif /* NASM_INSNSI_H */\n";
178 close I;
181 if ( !defined($output) || $output eq 'n' ) {
182 print STDERR "Writing insnsn.c...\n";
184 open N, ">insnsn.c";
186 print N "/* This file is auto-generated from insns.dat by insns.pl" .
187 " - don't edit it */\n\n";
188 print N "/* This file in included by names.c */\n\n";
190 print N "static const char * const insn_names[] = {";
191 $first = 1;
192 foreach $i (@opcodes) {
193 print N "," if ( !$first );
194 $first = 0;
195 $ilower = $i;
196 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
197 print N "\n\t\"${ilower}\"";
199 print N "\n};\n\n";
200 print N "/* Conditional instructions */\n";
201 print N "static const char *icn[] = {";
202 $first = 1;
203 foreach $i (@opcodes_cc) {
204 print N "," if ( !$first );
205 $first = 0;
206 $ilower = $i;
207 $ilower =~ s/cc$//; # Skip cc suffix
208 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
209 print N "\n\t\"${ilower}\"";
212 print N "\n};\n\n";
213 print N "/* and the corresponding opcodes */\n";
214 print N "static const enum opcode ico[] = {";
215 $first = 1;
216 foreach $i (@opcodes_cc) {
217 print N "," if ( !$first );
218 $first = 0;
219 print N "\n\tI_$i";
222 print N "\n};\n";
224 close N;
227 printf STDERR "Done: %d instructions\n", $insns;
229 sub format {
230 my ($opcode, $operands, $codes, $flags) = @_;
231 my $num, $nd = 0;
233 return (undef, undef) if $operands eq "ignore";
235 # format the operands
236 $operands =~ s/:/|colon,/g;
237 $operands =~ s/mem(\d+)/mem|bits$1/g;
238 $operands =~ s/mem/memory/g;
239 $operands =~ s/memory_offs/mem_offs/g;
240 $operands =~ s/imm(\d+)/imm|bits$1/g;
241 $operands =~ s/imm/immediate/g;
242 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
243 $operands =~ s/mmxrm/rm_mmx/g;
244 $operands =~ s/xmmrm/rm_xmm/g;
245 $operands =~ s/\=([0-9]+)/same_as|$1/g;
246 if ($operands eq 'void') {
247 @ops = ();
248 } else {
249 @ops = split(/\,/, $operands);
251 $num = scalar(@ops);
252 while (scalar(@ops) < 4) {
253 push(@ops, '0');
255 $operands = join(',', @ops);
256 $operands =~ tr/a-z/A-Z/;
258 # format the flags
259 $flags =~ s/,/|IF_/g;
260 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
261 $flags = "IF_" . $flags;
263 ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
266 sub hexlist($$$) {
267 my($prefix, $start, $n) = @_;
268 my $i;
269 my @l = ();
271 for ($i = 0; $i < $n; $i++) {
272 push(@l, sprintf("%s%02X", $prefix, $start+$i));
274 return @l;
277 # Here we determine the range of possible starting bytes for a given
278 # instruction. We need only consider the codes:
279 # \1 \2 \3 mean literal bytes, of course
280 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
281 # \1[0123] mean byte plus register value
282 # \170 means byte zero
283 # \330 means byte plus condition code
284 # \0 or \340 mean give up and return empty set
285 sub startseq($) {
286 my ($codestr) = @_;
287 my $word, @range;
288 my @codes = ();
289 my $c = $codestr;
290 my $c0, $c1, $i;
291 my $prefix = '';
293 # Although these are C-syntax strings, by convention they should have
294 # only octal escapes (for directives) and hexadecimal escapes
295 # (for verbatim bytes)
296 while ($c ne '') {
297 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
298 push(@codes, hex $1);
299 $c = $2;
300 next;
301 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
302 push(@codes, oct $1);
303 $c = $2;
304 next;
305 } else {
306 die "$0: unknown code format in \"$codestr\"\n";
310 while ($c0 = shift(@codes)) {
311 $c1 = $codes[0];
312 if ($c0 == 01 || $c0 == 02 || $c0 == 03 || $c0 == 0170) {
313 # Fixed byte string
314 my $fbs = $prefix;
315 while (1) {
316 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
317 while ($c0--) {
318 $fbs .= sprintf("%02X", shift(@codes));
320 } elsif ($c0 == 0170) {
321 $fbs .= '00';
322 } else {
323 last;
325 $c0 = shift(@codes);
328 foreach $pfx (@disasm_prefixes) {
329 if ($fbs =~ /^$pfx(.*)$/) {
330 $prefix = $pfx;
331 $fbs = $1;
332 last;
336 if ($fbs ne '') {
337 return ($prefix.substr($fbs,0,2));
339 } elsif ($c0 == 04) {
340 return ("07", "17", "1F");
341 } elsif ($c0 == 05) {
342 return ("A1", "A9");
343 } elsif ($c0 == 06) {
344 return ("06", "0E", "16", "1E");
345 } elsif ($c0 == 07) {
346 return ("A0", "A8");
347 } elsif ($c0 >= 010 && $c0 <= 013) {
348 return hexlist($prefix, $c1, 8);
349 } elsif ($c0 == 0330) {
350 return hexlist($prefix, $c1, 16);
351 } elsif ($c0 == 0 || $c0 == 0340) {
352 return ();
355 return ();