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 license given in the file "LICENSE"
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";
18 foreach $arg ( @ARGV ) {
19 if ( $arg =~ /^\-/ ) {
20 if ( $arg =~ /^\-([abdin])$/ ) {
23 die "$0: Unknown option: ${arg}\n";
30 $fname = "insns.dat" unless $fname = $args[0];
31 open (F, $fname) || die "unable to open $fname";
40 next if /^\s*;/; # comments
43 next if $#_ == -1; # blank lines
44 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
45 ($formatted, $nd) = &format(@_);
49 push @$aname, $formatted;
51 if ( $_[0] =~ /cc$/ ) {
52 # Conditional instruction
53 $k_opcodes_cc{$_[0]}++;
55 # Unconditional instruction
58 if ($formatted && !$nd) {
59 push @big, $formatted;
60 my @sseq = startseq($_[2]);
62 if (!defined($dinstables{$i})) {
65 push(@{$dinstables{$i}}, $#big);
73 # Generate the bytecode array. At this point, @bytecode_list contains
74 # the full set of bytecodes.
77 # Sort by descending length
78 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
82 foreach $bl (@bytecode_list) {
84 next if (defined($bytecode_pos{$h}));
86 push(@bytecode_array, $bl);
88 $bytecode_pos{$h} = $bytecode_next;
95 @opcodes = sort keys(%k_opcodes);
96 @opcodes_cc = sort keys(%k_opcodes_cc);
98 if ( !defined($output) || $output eq 'b') {
99 print STDERR "Writing insnsb.c...\n";
103 print B "/* This file auto-generated from insns.dat by insns.pl" .
104 " - don't edit it */\n\n";
106 print B "#include \"nasm.h\"\n";
107 print B "#include \"insns.h\"\n\n";
109 print B "static const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
112 foreach $bl (@bytecode_array) {
113 printf B " /* %4d */ ", $p;
125 if ( !defined($output) || $output eq 'a' ) {
126 print STDERR "Writing insnsa.c...\n";
130 print A "/* This file auto-generated from insns.dat by insns.pl" .
131 " - don't edit it */\n\n";
133 print A "#include \"insnsb.c\"\n\n";
135 foreach $i (@opcodes, @opcodes_cc) {
136 print A "static const struct itemplate instrux_${i}[] = {\n";
138 foreach $j (@$aname) {
139 print A " ", codesubst($j), "\n";
141 print A " ITEMPLATE_END\n};\n\n";
143 print A "const struct itemplate * const nasm_instructions[] = {\n";
144 foreach $i (@opcodes, @opcodes_cc) {
145 print A " instrux_${i},\n";
152 if ( !defined($output) || $output eq 'd' ) {
153 print STDERR "Writing insnsd.c...\n";
157 print D "/* This file auto-generated from insns.dat by insns.pl" .
158 " - don't edit it */\n\n";
160 print D "#include \"insnsb.c\"\n\n";
162 print D "static const struct itemplate instrux[] = {\n";
165 printf D " /* %4d */ %s\n", $n++, codesubst($j);
169 foreach $h (sort(keys(%dinstables))) {
170 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
171 foreach $j (@{$dinstables{$h}}) {
172 print D " instrux + $j,\n";
177 foreach $h (@disasm_prefixes, '') {
180 print D "static " unless ($h eq '');
181 print D "const struct disasm_index ";
182 print D ($h eq '') ? 'itable' : "itable_$h";
183 print D "[256] = {\n";
184 for ($c = 0; $c < 256; $c++) {
185 $nn = sprintf("%s%02X", $h, $c);
186 if ($is_prefix{$nn}) {
187 die "$0: ambiguous decoding of $nn\n"
188 if (defined($dinstables{$nn}));
189 printf D " { itable_%s, -1 },\n", $nn;
190 } elsif (defined($dinstables{$nn})) {
191 printf D " { itable_%s, %u },\n",
192 $nn, scalar(@{$dinstables{$nn}});
194 printf D " { NULL, 0 },\n";
203 if ( !defined($output) || $output eq 'i' ) {
204 print STDERR "Writing insnsi.h...\n";
208 print I "/* This file is auto-generated from insns.dat by insns.pl" .
209 " - don't edit it */\n\n";
210 print I "/* This file in included by nasm.h */\n\n";
212 print I "/* Instruction names */\n\n";
213 print I "#ifndef NASM_INSNSI_H\n";
214 print I "#define NASM_INSNSI_H 1\n\n";
215 print I "enum opcode {\n";
217 foreach $i (@opcodes, @opcodes_cc) {
218 print I "\tI_${i},\n";
220 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
221 $maxlen = $len if ( $len > $maxlen );
223 print I "\tI_none = -1\n";
225 print I "#define MAX_INSLEN ", $maxlen, "\n\n";
226 print I "#endif /* NASM_INSNSI_H */\n";
231 if ( !defined($output) || $output eq 'n' ) {
232 print STDERR "Writing insnsn.c...\n";
236 print N "/* This file is auto-generated from insns.dat by insns.pl" .
237 " - don't edit it */\n\n";
238 print N "/* This file in included by names.c */\n\n";
240 print N "static const char * const insn_names[] = {";
242 foreach $i (@opcodes) {
243 print N "," if ( !$first );
246 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
247 print N "\n\t\"${ilower}\"";
250 print N "/* Conditional instructions */\n";
251 print N "static const char *icn[] = {";
253 foreach $i (@opcodes_cc) {
254 print N "," if ( !$first );
257 $ilower =~ s/cc$//; # Skip cc suffix
258 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
259 print N "\n\t\"${ilower}\"";
263 print N "/* and the corresponding opcodes */\n";
264 print N "static const enum opcode ico[] = {";
266 foreach $i (@opcodes_cc) {
267 print N "," if ( !$first );
277 printf STDERR "Done: %d instructions\n", $insns;
280 my ($opcode, $operands, $codes, $flags) = @_;
284 return (undef, undef) if $operands eq "ignore";
286 # format the operands
287 $operands =~ s/:/|colon,/g;
288 $operands =~ s/mem(\d+)/mem|bits$1/g;
289 $operands =~ s/mem/memory/g;
290 $operands =~ s/memory_offs/mem_offs/g;
291 $operands =~ s/imm(\d+)/imm|bits$1/g;
292 $operands =~ s/imm/immediate/g;
293 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
294 $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
295 $operands =~ s/\=([0-9]+)/same_as|$1/g;
296 if ($operands eq 'void') {
299 @ops = split(/\,/, $operands);
302 while (scalar(@ops) < 4) {
305 $operands = join(',', @ops);
306 $operands =~ tr/a-z/A-Z/;
309 $flags =~ s/,/|IF_/g;
310 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
311 $flags = "IF_" . $flags;
313 @bytecode = (decodify($codes), 0);
314 push(@bytecode_list, [@bytecode]);
315 $codes = hexstr(@bytecode);
317 ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
321 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
322 # offset into nasm_bytecodes
328 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
329 my $pos = $bytecode_pos{$1};
330 if (!defined($pos)) {
331 die "$0: no position assigned to byte code $1\n";
333 $s = $` . "nasm_bytecodes+${pos}" . "$'";
339 my ($prefix, @list) = @_;
344 push(@l, sprintf("%s%02X", $prefix, $x));
351 # Turn a code string into a sequence of bytes
354 # Although these are C-syntax strings, by convention they should have
355 # only octal escapes (for directives) and hexadecimal escapes
356 # (for verbatim bytes)
362 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
363 push(@codes, hex $1);
366 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
367 push(@codes, oct $1);
371 die "$0: unknown code format in \"$codestr\"\n";
378 # Turn a numeric list into a hex string
384 $s .= sprintf("%02X", $c);
389 # Here we determine the range of possible starting bytes for a given
390 # instruction. We need only consider the codes:
391 # \1 \2 \3 mean literal bytes, of course
392 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
393 # \1[0123] mean byte plus register value
394 # \170 means byte zero
395 # \330 means byte plus condition code
396 # \0 or \340 mean give up and return empty set
405 @codes = decodify($codestr);
407 while ($c0 = shift(@codes)) {
409 if ($c0 == 01 || $c0 == 02 || $c0 == 03 || $c0 == 0170) {
413 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
415 $fbs .= sprintf("%02X", shift(@codes));
417 } elsif ($c0 == 0170) {
425 foreach $pfx (@disasm_prefixes) {
426 if (substr($fbs, 0, length($pfx)) eq $pfx) {
428 $fbs = substr($fbs, length($pfx));
434 return ($prefix.substr($fbs,0,2));
437 unshift(@codes, $c0);
438 } elsif ($c0 == 04) {
439 return addprefix($prefix, 0x07, 0x17, 0x1F);
440 } elsif ($c0 == 05) {
441 return addprefix($prefix, 0xA1, 0xA9);
442 } elsif ($c0 == 06) {
443 return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
444 } elsif ($c0 == 07) {
445 return addprefix($prefix, 0xA0, 0xA8);
446 } elsif ($c0 >= 010 && $c0 <= 013) {
447 return addprefix($prefix, $c1..($c1+7));
448 } elsif (($c0 & ~013) == 0144) {
449 return addprefix($prefix, $c1, $c1|2);
450 } elsif ($c0 == 0330) {
451 return addprefix($prefix, $c1..($c1+15));
452 } elsif ($c0 == 0 || $c0 == 0340) {
454 } elsif (($c0 & ~3) == 0260 || $c0 == 270) {
457 } elsif ($c0 == 0172) {
460 # We really need to be able to distinguish "forbidden"
461 # and "ignorable" codes here