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 =~ /^\-([adin])$/ ) {
23 die "$0: Unknown option: ${arg}\n";
30 $fname = "insns.dat" unless $fname = $args[0];
31 open (F
, $fname) || die "unable to open $fname";
39 next if /^\s*;/; # comments
42 next if $#_ == -1; # blank lines
43 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
44 ($formatted, $nd) = &format
(@_);
48 push @
$aname, $formatted;
50 if ( $_[0] =~ /cc$/ ) {
51 # Conditional instruction
52 $k_opcodes_cc{$_[0]}++;
54 # Unconditional instruction
57 if ($formatted && !$nd) {
58 push @big, $formatted;
59 my @sseq = startseq
($_[2]);
61 if (!defined($dinstables{$i})) {
64 push(@
{$dinstables{$i}}, $#big);
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";
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";
85 foreach $i (@opcodes, @opcodes_cc) {
86 print A
"static const struct itemplate instrux_${i}[] = {\n";
88 foreach $j (@
$aname) {
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";
102 if ( !defined($output) || $output eq 'd' ) {
103 print STDERR
"Writing insnsd.c...\n";
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";
113 print D
"static const struct itemplate instrux[] = {\n";
116 printf D
" /* %4d */ %s\n", $n++, $j;
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";
128 foreach $h (@disasm_prefixes, '') {
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}});
145 printf D
" { NULL, 0 },\n";
154 if ( !defined($output) || $output eq 'i' ) {
155 print STDERR
"Writing insnsi.h...\n";
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";
168 foreach $i (@opcodes, @opcodes_cc) {
169 print I
"\tI_${i},\n";
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";
176 print I
"#define MAX_INSLEN ", $maxlen, "\n\n";
177 print I
"#endif /* NASM_INSNSI_H */\n";
182 if ( !defined($output) || $output eq 'n' ) {
183 print STDERR
"Writing insnsn.c...\n";
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[] = {";
193 foreach $i (@opcodes) {
194 print N
"," if ( !$first );
197 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
198 print N
"\n\t\"${ilower}\"";
201 print N
"/* Conditional instructions */\n";
202 print N
"static const char *icn[] = {";
204 foreach $i (@opcodes_cc) {
205 print N
"," if ( !$first );
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}\"";
214 print N
"/* and the corresponding opcodes */\n";
215 print N
"static const enum opcode ico[] = {";
217 foreach $i (@opcodes_cc) {
218 print N
"," if ( !$first );
228 printf STDERR
"Done: %d instructions\n", $insns;
231 my ($opcode, $operands, $codes, $flags) = @_;
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') {
250 @ops = split(/\,/, $operands);
253 while (scalar(@ops) < 4) {
256 $operands = join(',', @ops);
257 $operands =~ tr/a-z/A-Z/;
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);
268 my ($prefix, @list) = @_;
273 push(@l, sprintf("%s%02X", $prefix, $x));
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
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)
299 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
300 push(@codes, hex $1);
303 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
304 push(@codes, oct $1);
308 die "$0: unknown code format in \"$codestr\"\n";
312 while ($c0 = shift(@codes)) {
314 if ($c0 == 01 || $c0 == 02 || $c0 == 03 || $c0 == 0170) {
318 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
320 $fbs .= sprintf("%02X", shift(@codes));
322 } elsif ($c0 == 0170) {
330 foreach $pfx (@disasm_prefixes) {
331 if (substr($fbs, 0, length($pfx)) eq $pfx) {
333 $fbs = substr($fbs, length($pfx));
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) {
360 # We really need to be able to distinguish "forbidden"
361 # and "ignorable" codes here