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";
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 foreach $i (startseq
($_[2])) {
60 if (!defined($dinstables{$i})) {
63 push(@
{$dinstables{$i}}, $#big);
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";
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";
84 foreach $i (@opcodes, @opcodes_cc) {
85 print A
"static const struct itemplate instrux_${i}[] = {\n";
87 foreach $j (@
$aname) {
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";
101 if ( !defined($output) || $output eq 'd' ) {
102 print STDERR
"Writing insnsd.c...\n";
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";
112 print D
"static const struct itemplate instrux[] = {\n";
115 printf D
" /* %4d */ %s\n", $n++, $j;
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";
127 foreach $h (@disasm_prefixes, '') {
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}});
144 printf D
" { NULL, 0 },\n";
153 if ( !defined($output) || $output eq 'i' ) {
154 print STDERR
"Writing insnsi.h...\n";
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";
167 foreach $i (@opcodes, @opcodes_cc) {
168 print I
"\tI_${i},\n";
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";
175 print I
"#define MAX_INSLEN ", $maxlen, "\n\n";
176 print I
"#endif /* NASM_INSNSI_H */\n";
181 if ( !defined($output) || $output eq 'n' ) {
182 print STDERR
"Writing insnsn.c...\n";
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[] = {";
192 foreach $i (@opcodes) {
193 print N
"," if ( !$first );
196 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
197 print N
"\n\t\"${ilower}\"";
200 print N
"/* Conditional instructions */\n";
201 print N
"static const char *icn[] = {";
203 foreach $i (@opcodes_cc) {
204 print N
"," if ( !$first );
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}\"";
213 print N
"/* and the corresponding opcodes */\n";
214 print N
"static const enum opcode ico[] = {";
216 foreach $i (@opcodes_cc) {
217 print N
"," if ( !$first );
227 printf STDERR
"Done: %d instructions\n", $insns;
230 my ($opcode, $operands, $codes, $flags) = @_;
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') {
249 @ops = split(/\,/, $operands);
252 while (scalar(@ops) < 4) {
255 $operands = join(',', @ops);
256 $operands =~ tr/a-z/A-Z/;
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);
267 my($prefix, $start, $n) = @_;
271 for ($i = 0; $i < $n; $i++) {
272 push(@l, sprintf("%s%02X", $prefix, $start+$i));
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
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)
297 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
298 push(@codes, hex $1);
301 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
302 push(@codes, oct $1);
306 die "$0: unknown code format in \"$codestr\"\n";
310 while ($c0 = shift(@codes)) {
312 if ($c0 == 01 || $c0 == 02 || $c0 == 03 || $c0 == 0170) {
316 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
318 $fbs .= sprintf("%02X", shift(@codes));
320 } elsif ($c0 == 0170) {
328 foreach $pfx (@disasm_prefixes) {
329 if ($fbs =~ /^$pfx(.*)$/) {
337 return ($prefix.substr($fbs,0,2));
339 } elsif ($c0 == 04) {
340 return ("07", "17", "1F");
341 } elsif ($c0 == 05) {
343 } elsif ($c0 == 06) {
344 return ("06", "0E", "16", "1E");
345 } elsif ($c0 == 07) {
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) {