NASM 0.98p3.3
[nasm.git] / insns.pl
bloba61f6f9815a73ff54036295e07f645a91a220170
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 print STDERR "Reading insns.dat...\n";
12 $fname = "insns.dat" unless $fname = $ARGV[0];
13 open (F, $fname) || die "unable to open $fname";
15 $line = 0;
16 $insns = 0;
17 while (<F>) {
18 $line++;
19 next if /^\s*;/; # comments
20 chomp;
21 split;
22 next if $#_ == -1; # blank lines
23 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
24 ($formatted, $nd) = &format(@_);
25 if ($formatted) {
26 $insns++;
27 $aname = "aa_$_[0]";
28 push @$aname, $formatted;
30 if ( $_[0] =~ /cc$/ ) {
31 $k_opcodes_cc{$_[0]}++;
32 } else {
33 $k_opcodes{$_[0]}++;
35 $done{$_[0]} = 1 if !$done{$_[0]};
36 if ($formatted && !$nd) {
37 push @big, $formatted;
38 foreach $i (&startbyte($_[2])) {
39 $aname = sprintf "dd_%02X",$i;
40 push @$aname, $#big;
45 close F;
47 @opcodes = sort keys(%k_opcodes); # Unconditional instructions
48 @opcodes_cc = sort keys(%k_opcodes_cc); # Conditional instructions
50 print STDERR "Writing insnsa.c...\n";
52 open A, ">insnsa.c";
54 print A "/* This file auto-generated from insns.dat by insns.pl" .
55 " - don't edit it */\n\n";
56 print A "#include <stdio.h>\n";
57 print A "#include \"nasm.h\"\n";
58 print A "#include \"insns.h\"\n";
59 print A "\n";
61 foreach $i (@opcodes, @opcodes_cc) {
62 print A "static struct itemplate instrux_${i}[] = {\n";
63 $aname = "aa_$i";
64 foreach $j (@$aname) {
65 print A " $j\n";
67 print A " {-1}\n};\n\n";
69 print A "struct itemplate *nasm_instructions[] = {\n";
70 foreach $i (@opcodes, @opcodes_cc) {
71 print A " instrux_${i},\n";
73 print A "};\n";
75 close A;
77 print STDERR "Writing insnsd.c...\n";
79 open D, ">insnsd.c";
81 print D "/* This file auto-generated from insns.dat by insns.pl" .
82 " - don't edit it */\n\n";
83 print D "#include <stdio.h>\n";
84 print D "#include \"nasm.h\"\n";
85 print D "#include \"insns.h\"\n";
86 print D "\n";
88 print D "static struct itemplate instrux[] = {\n";
89 foreach $j (@big) {
90 print D " $j\n";
92 print D " {-1}\n};\n\n";
94 for ($c=0; $c<256; $c++) {
95 $h = sprintf "%02X", $c;
96 print D "static struct itemplate *itable_${h}[] = {\n";
97 $aname = "dd_$h";
98 foreach $j (@$aname) {
99 print D " instrux + $j,\n";
101 print D " NULL\n};\n\n";
104 print D "struct itemplate **itable[] = {\n";
105 for ($c=0; $c<256; $c++) {
106 printf D " itable_%02X,\n", $c;
108 print D "};\n";
110 close D;
112 print STDERR "Writing insnsi.h...\n";
114 open I, ">insnsi.h";
116 print I "/* This file is auto-generated from insns.dat by insns.pl" .
117 " - don't edit it */\n\n";
118 print I "/* This file in included by nasm.h */\n\n";
120 print I "/* Instruction names */\n";
121 print I "enum {";
122 $first = 1;
123 $maxlen = 0;
124 foreach $i (@opcodes, @opcodes_cc) {
125 print I "," if ( !$first );
126 $first = 0;
127 print I "\n\tI_${i}";
128 $len = length($i);
129 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
130 $maxlen = $len if ( $len > $maxlen );
132 print I "\n};\n\n";
133 print I "#define MAX_INSLEN ", $maxlen, "\n";
135 close I;
137 print STDERR "Writing insnsn.c...\n";
139 open N, ">insnsn.c";
141 print N "/* This file is auto-generated from insns.dat by insns.pl" .
142 " - don't edit it */\n\n";
143 print N "/* This file in included by names.c */\n\n";
145 print N "static char *insn_names[] = {";
146 $first = 1;
147 foreach $i (@opcodes) {
148 print N "," if ( !$first );
149 $first = 0;
150 $ilower = $i;
151 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
152 print N "\n\t\"${ilower}\"";
154 print N "\n};\n\n";
155 print N "/* Conditional instructions */\n";
156 print N "static char *icn[] = {";
157 $first = 1;
158 foreach $i (@opcodes_cc) {
159 print N "," if ( !$first );
160 $first = 0;
161 $ilower = $i;
162 $ilower =~ s/cc$//; # Skip cc suffix
163 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
164 print N "\n\t\"${ilower}\"";
167 print N "\n};\n\n";
168 print N "/* and the corresponding opcodes */\n";
169 print N "static int ico[] = {";
170 $first = 1;
171 foreach $i (@opcodes_cc) {
172 print N "," if ( !$first );
173 $first = 0;
174 print N "\n\tI_$i";
177 print N "\n};\n";
179 close N;
181 printf STDERR "Done: %d instructions\n", $insns;
183 sub format {
184 local ($opcode, $operands, $codes, $flags) = @_;
185 local $num, $nd = 0;
187 return (undef, undef) if $operands eq "ignore";
189 # format the operands
190 $operands =~ s/:/|colon,/g;
191 $operands =~ s/mem(\d+)/mem|bits$1/g;
192 $operands =~ s/mem/memory/g;
193 $operands =~ s/memory_offs/mem_offs/g;
194 $operands =~ s/imm(\d+)/imm|bits$1/g;
195 $operands =~ s/imm/immediate/g;
196 $operands =~ s/rm(\d+)/regmem|bits$1/g;
197 $num = 3;
198 $operands = '0,0,0', $num = 0 if $operands eq 'void';
199 $operands .= ',0', $num-- while $operands !~ /,.*,/;
200 $operands =~ tr/a-z/A-Z/;
202 # format the flags
203 $flags =~ s/,/|IF_/g;
204 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
205 $flags = "IF_" . $flags;
207 ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
210 # Here we determine the range of possible starting bytes for a given
211 # instruction. We need only consider the codes:
212 # \1 \2 \3 mean literal bytes, of course
213 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
214 # \10 \11 \12 mean byte plus register value
215 # \17 means byte zero
216 # \330 means byte plus condition code
217 # \0 or \340 mean give up and return empty set
218 sub startbyte {
219 local ($codes) = @_;
220 local $word, @range;
222 while (1) {
223 die "couldn't get code in '$codes'" if $codes !~ /^(\\[^\\]+)(\\.*)?$/;
224 $word = $1, $codes = $2;
225 return (hex $1) if $word =~ /^\\[123]$/ && $codes =~ /^\\x(..)/;
226 return (0x07, 0x17, 0x1F) if $word eq "\\4";
227 return (0xA1, 0xA9) if $word eq "\\5";
228 return (0x06, 0x0E, 0x16, 0x1E) if $word eq "\\6";
229 return (0xA0, 0xA8) if $word eq "\\7";
230 $start=hex $1, $r=8, last if $word =~ /^\\1[012]$/ && $codes =~/^\\x(..)/;
231 return (0) if $word eq "\\17";
232 $start=hex $1, $r=16, last if $word =~ /^\\330$/ && $codes =~ /^\\x(..)/;
233 return () if $word eq "\\0" || $word eq "\\340";
235 @range = ();
236 push @range, $start++ while ($r-- > 0);
237 @range;