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 print STDERR
"Reading insns.dat...\n";
12 $fname = "insns.dat" unless $fname = $ARGV[0];
13 open (F
, $fname) || die "unable to open $fname";
19 next if /^\s*;/; # comments
22 next if $#_ == -1; # blank lines
23 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
24 ($formatted, $nd) = &format
(@_);
28 push @
$aname, $formatted;
30 if ( $_[0] =~ /cc$/ ) {
31 # Conditional instruction
32 $k_opcodes_cc{$_[0]}++;
34 # Unconditional instruction
37 if ($formatted && !$nd) {
38 push @big, $formatted;
39 foreach $i (&startbyte
($_[2])) {
40 $aname = sprintf "dd_%02X",$i;
48 @opcodes = sort keys(%k_opcodes);
49 @opcodes_cc = sort keys(%k_opcodes_cc);
51 print STDERR
"Writing insnsa.c...\n";
55 print A
"/* This file auto-generated from insns.dat by insns.pl" .
56 " - don't edit it */\n\n";
57 print A
"#include <stdio.h>\n";
58 print A
"#include \"nasm.h\"\n";
59 print A
"#include \"insns.h\"\n";
62 foreach $i (@opcodes, @opcodes_cc) {
63 print A
"static struct itemplate instrux_${i}[] = {\n";
65 foreach $j (@
$aname) {
68 print A
" {-1}\n};\n\n";
70 print A
"struct itemplate *nasm_instructions[] = {\n";
71 foreach $i (@opcodes, @opcodes_cc) {
72 print A
" instrux_${i},\n";
78 print STDERR
"Writing insnsd.c...\n";
82 print D
"/* This file auto-generated from insns.dat by insns.pl" .
83 " - don't edit it */\n\n";
84 print D
"#include <stdio.h>\n";
85 print D
"#include \"nasm.h\"\n";
86 print D
"#include \"insns.h\"\n";
89 print D
"static struct itemplate instrux[] = {\n";
93 print D
" {-1}\n};\n\n";
95 for ($c=0; $c<256; $c++) {
96 $h = sprintf "%02X", $c;
97 print D
"static struct itemplate *itable_${h}[] = {\n";
99 foreach $j (@
$aname) {
100 print D
" instrux + $j,\n";
102 print D
" NULL\n};\n\n";
105 print D
"struct itemplate **itable[] = {\n";
106 for ($c=0; $c<256; $c++) {
107 printf D
" itable_%02X,\n", $c;
113 print STDERR
"Writing insnsi.h...\n";
117 print I
"/* This file is auto-generated from insns.dat by insns.pl" .
118 " - don't edit it */\n\n";
119 print I
"/* This file in included by nasm.h */\n\n";
121 print I
"/* Instruction names */\n";
125 foreach $i (@opcodes, @opcodes_cc) {
126 print I
"," if ( !$first );
128 print I
"\n\tI_${i}";
130 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
131 $maxlen = $len if ( $len > $maxlen );
134 print I
"#define MAX_INSLEN ", $maxlen, "\n";
138 print STDERR
"Writing insnsn.c...\n";
142 print N
"/* This file is auto-generated from insns.dat by insns.pl" .
143 " - don't edit it */\n\n";
144 print N
"/* This file in included by names.c */\n\n";
146 print N
"static char *insn_names[] = {";
148 foreach $i (@opcodes) {
149 print N
"," if ( !$first );
152 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
153 print N
"\n\t\"${ilower}\"";
156 print N
"/* Conditional instructions */\n";
157 print N
"static char *icn[] = {";
159 foreach $i (@opcodes_cc) {
160 print N
"," if ( !$first );
163 $ilower =~ s/cc$//; # Skip cc suffix
164 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
165 print N
"\n\t\"${ilower}\"";
169 print N
"/* and the corresponding opcodes */\n";
170 print N
"static int ico[] = {";
172 foreach $i (@opcodes_cc) {
173 print N
"," if ( !$first );
182 printf STDERR
"Done: %d instructions\n", $insns;
185 local ($opcode, $operands, $codes, $flags) = @_;
188 return (undef, undef) if $operands eq "ignore";
190 # format the operands
191 $operands =~ s/:/|colon,/g;
192 $operands =~ s/mem(\d+)/mem|bits$1/g;
193 $operands =~ s/mem/memory/g;
194 $operands =~ s/memory_offs/mem_offs/g;
195 $operands =~ s/imm(\d+)/imm|bits$1/g;
196 $operands =~ s/imm/immediate/g;
197 $operands =~ s/rm(\d+)/regmem|bits$1/g;
199 $operands = '0,0,0', $num = 0 if $operands eq 'void';
200 $operands .= ',0', $num-- while $operands !~ /,.*,/;
201 $operands =~ tr/a-z/A-Z/;
204 $flags =~ s/,/|IF_/g;
205 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
206 $flags = "IF_" . $flags;
208 ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
211 # Here we determine the range of possible starting bytes for a given
212 # instruction. We need only consider the codes:
213 # \1 \2 \3 mean literal bytes, of course
214 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
215 # \10 \11 \12 mean byte plus register value
216 # \17 means byte zero
217 # \330 means byte plus condition code
218 # \0 or \340 mean give up and return empty set
224 die "couldn't get code in '$codes'" if $codes !~ /^(\\[^\\]+)(\\.*)?$/;
225 $word = $1, $codes = $2;
226 return (hex $1) if $word =~ /^\\[123]$/ && $codes =~ /^\\x(..)/;
227 return (0x07, 0x17, 0x1F) if $word eq "\\4";
228 return (0xA1, 0xA9) if $word eq "\\5";
229 return (0x06, 0x0E, 0x16, 0x1E) if $word eq "\\6";
230 return (0xA0, 0xA8) if $word eq "\\7";
231 $start=hex $1, $r=8, last if $word =~ /^\\1[012]$/ && $codes =~/^\\x(..)/;
232 return (0) if $word eq "\\17";
233 $start=hex $1, $r=16, last if $word =~ /^\\330$/ && $codes =~ /^\\x(..)/;
234 return () if $word eq "\\0" || $word eq "\\340";
237 push @range, $start++ while ($r-- > 0);