NASM 2.01rc1
[nasm/avx512.git] / insns.pl
blobf48e2cc2d786adb50a9a40ee257a5cf612c28475
1 #!/usr/bin/perl
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 my @sseq = startseq($_[2]);
60 foreach $i (@sseq) {
61 if (!defined($dinstables{$i})) {
62 $dinstables{$i} = [];
64 push(@{$dinstables{$i}}, $#big);
69 close F;
71 @opcodes = sort keys(%k_opcodes);
72 @opcodes_cc = sort keys(%k_opcodes_cc);
74 if ( !defined($output) || $output eq 'a' ) {
75 print STDERR "Writing insnsa.c...\n";
77 open A, ">insnsa.c";
79 print A "/* This file auto-generated from insns.dat by insns.pl" .
80 " - don't edit it */\n\n";
81 print A "#include \"nasm.h\"\n";
82 print A "#include \"insns.h\"\n";
83 print A "\n";
85 foreach $i (@opcodes, @opcodes_cc) {
86 print A "static const struct itemplate instrux_${i}[] = {\n";
87 $aname = "aa_$i";
88 foreach $j (@$aname) {
89 print A " $j\n";
91 print A " ITEMPLATE_END\n};\n\n";
93 print A "const struct itemplate * const nasm_instructions[] = {\n";
94 foreach $i (@opcodes, @opcodes_cc) {
95 print A " instrux_${i},\n";
97 print A "};\n";
99 close A;
102 if ( !defined($output) || $output eq 'd' ) {
103 print STDERR "Writing insnsd.c...\n";
105 open D, ">insnsd.c";
107 print D "/* This file auto-generated from insns.dat by insns.pl" .
108 " - don't edit it */\n\n";
109 print D "#include \"nasm.h\"\n";
110 print D "#include \"insns.h\"\n";
111 print D "\n";
113 print D "static const struct itemplate instrux[] = {\n";
114 $n = 0;
115 foreach $j (@big) {
116 printf D " /* %4d */ %s\n", $n++, $j;
118 print D "};\n";
120 foreach $h (sort(keys(%dinstables))) {
121 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
122 foreach $j (@{$dinstables{$h}}) {
123 print D " instrux + $j,\n";
125 print D "};\n";
128 foreach $h (@disasm_prefixes, '') {
129 $is_prefix{$h} = 1;
130 print D "\n";
131 print D "static " unless ($h eq '');
132 print D "const struct disasm_index ";
133 print D ($h eq '') ? 'itable' : "itable_$h";
134 print D "[256] = {\n";
135 for ($c = 0; $c < 256; $c++) {
136 $nn = sprintf("%s%02X", $h, $c);
137 if ($is_prefix{$nn}) {
138 die "$0: ambiguous decoding of $nn\n"
139 if (defined($dinstables{$nn}));
140 printf D " { itable_%s, -1 },\n", $nn;
141 } elsif (defined($dinstables{$nn})) {
142 printf D " { itable_%s, %u },\n",
143 $nn, scalar(@{$dinstables{$nn}});
144 } else {
145 printf D " { NULL, 0 },\n";
148 print D "};\n";
151 close D;
154 if ( !defined($output) || $output eq 'i' ) {
155 print STDERR "Writing insnsi.h...\n";
157 open I, ">insnsi.h";
159 print I "/* This file is auto-generated from insns.dat by insns.pl" .
160 " - don't edit it */\n\n";
161 print I "/* This file in included by nasm.h */\n\n";
163 print I "/* Instruction names */\n\n";
164 print I "#ifndef NASM_INSNSI_H\n";
165 print I "#define NASM_INSNSI_H 1\n\n";
166 print I "enum opcode {\n";
167 $maxlen = 0;
168 foreach $i (@opcodes, @opcodes_cc) {
169 print I "\tI_${i},\n";
170 $len = length($i);
171 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
172 $maxlen = $len if ( $len > $maxlen );
174 print I "\tI_none = -1\n";
175 print I "\n};\n\n";
176 print I "#define MAX_INSLEN ", $maxlen, "\n\n";
177 print I "#endif /* NASM_INSNSI_H */\n";
179 close I;
182 if ( !defined($output) || $output eq 'n' ) {
183 print STDERR "Writing insnsn.c...\n";
185 open N, ">insnsn.c";
187 print N "/* This file is auto-generated from insns.dat by insns.pl" .
188 " - don't edit it */\n\n";
189 print N "/* This file in included by names.c */\n\n";
191 print N "static const char * const insn_names[] = {";
192 $first = 1;
193 foreach $i (@opcodes) {
194 print N "," if ( !$first );
195 $first = 0;
196 $ilower = $i;
197 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
198 print N "\n\t\"${ilower}\"";
200 print N "\n};\n\n";
201 print N "/* Conditional instructions */\n";
202 print N "static const char *icn[] = {";
203 $first = 1;
204 foreach $i (@opcodes_cc) {
205 print N "," if ( !$first );
206 $first = 0;
207 $ilower = $i;
208 $ilower =~ s/cc$//; # Skip cc suffix
209 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
210 print N "\n\t\"${ilower}\"";
213 print N "\n};\n\n";
214 print N "/* and the corresponding opcodes */\n";
215 print N "static const enum opcode ico[] = {";
216 $first = 1;
217 foreach $i (@opcodes_cc) {
218 print N "," if ( !$first );
219 $first = 0;
220 print N "\n\tI_$i";
223 print N "\n};\n";
225 close N;
228 printf STDERR "Done: %d instructions\n", $insns;
230 sub format {
231 my ($opcode, $operands, $codes, $flags) = @_;
232 my $num, $nd = 0;
234 return (undef, undef) if $operands eq "ignore";
236 # format the operands
237 $operands =~ s/:/|colon,/g;
238 $operands =~ s/mem(\d+)/mem|bits$1/g;
239 $operands =~ s/mem/memory/g;
240 $operands =~ s/memory_offs/mem_offs/g;
241 $operands =~ s/imm(\d+)/imm|bits$1/g;
242 $operands =~ s/imm/immediate/g;
243 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
244 $operands =~ s/mmxrm/rm_mmx/g;
245 $operands =~ s/xmmrm/rm_xmm/g;
246 $operands =~ s/\=([0-9]+)/same_as|$1/g;
247 if ($operands eq 'void') {
248 @ops = ();
249 } else {
250 @ops = split(/\,/, $operands);
252 $num = scalar(@ops);
253 while (scalar(@ops) < 4) {
254 push(@ops, '0');
256 $operands = join(',', @ops);
257 $operands =~ tr/a-z/A-Z/;
259 # format the flags
260 $flags =~ s/,/|IF_/g;
261 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
262 $flags = "IF_" . $flags;
264 ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
267 sub addprefix ($@) {
268 my ($prefix, @list) = @_;
269 my $x;
270 my @l = ();
272 foreach $x (@list) {
273 push(@l, sprintf("%s%02X", $prefix, $x));
276 return @l;
279 # Here we determine the range of possible starting bytes for a given
280 # instruction. We need only consider the codes:
281 # \1 \2 \3 mean literal bytes, of course
282 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
283 # \1[0123] mean byte plus register value
284 # \170 means byte zero
285 # \330 means byte plus condition code
286 # \0 or \340 mean give up and return empty set
287 sub startseq($) {
288 my ($codestr) = @_;
289 my $word, @range;
290 my @codes = ();
291 my $c = $codestr;
292 my $c0, $c1, $i;
293 my $prefix = '';
295 # Although these are C-syntax strings, by convention they should have
296 # only octal escapes (for directives) and hexadecimal escapes
297 # (for verbatim bytes)
298 while ($c ne '') {
299 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
300 push(@codes, hex $1);
301 $c = $2;
302 next;
303 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
304 push(@codes, oct $1);
305 $c = $2;
306 next;
307 } else {
308 die "$0: unknown code format in \"$codestr\"\n";
312 while ($c0 = shift(@codes)) {
313 $c1 = $codes[0];
314 if ($c0 == 01 || $c0 == 02 || $c0 == 03 || $c0 == 0170) {
315 # Fixed byte string
316 my $fbs = $prefix;
317 while (1) {
318 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
319 while ($c0--) {
320 $fbs .= sprintf("%02X", shift(@codes));
322 } elsif ($c0 == 0170) {
323 $fbs .= '00';
324 } else {
325 last;
327 $c0 = shift(@codes);
330 foreach $pfx (@disasm_prefixes) {
331 if (substr($fbs, 0, length($pfx)) eq $pfx) {
332 $prefix = $pfx;
333 $fbs = substr($fbs, length($pfx));
334 last;
338 if ($fbs ne '') {
339 return ($prefix.substr($fbs,0,2));
342 unshift(@codes, $c0);
343 } elsif ($c0 == 04) {
344 return addprefix($prefix, 0x07, 0x17, 0x1F);
345 } elsif ($c0 == 05) {
346 return addprefix($prefix, 0xA1, 0xA9);
347 } elsif ($c0 == 06) {
348 return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
349 } elsif ($c0 == 07) {
350 return addprefix($prefix, 0xA0, 0xA8);
351 } elsif ($c0 >= 010 && $c0 <= 013) {
352 return addprefix($prefix, $c1..($c1+7));
353 } elsif (($c0 & ~013) == 0144) {
354 return addprefix($prefix, $c1, $c1|2);
355 } elsif ($c0 == 0330) {
356 return addprefix($prefix, $c1..($c1+15));
357 } elsif ($c0 == 0 || $c0 == 0340) {
358 return $prefix;
359 } else {
360 # We really need to be able to distinguish "forbidden"
361 # and "ignorable" codes here
364 return $prefix;