NASM 0.98p3.4
[nasm/avx512.git] / insns.pl
blobd0407ec1807d664009d9ff99fd307740bde45c8e
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 # Conditional instruction
32 $k_opcodes_cc{$_[0]}++;
33 } else {
34 # Unconditional instruction
35 $k_opcodes{$_[0]}++;
37 if ($formatted && !$nd) {
38 push @big, $formatted;
39 foreach $i (&startbyte($_[2])) {
40 $aname = sprintf "dd_%02X",$i;
41 push @$aname, $#big;
46 close F;
48 @opcodes = sort keys(%k_opcodes);
49 @opcodes_cc = sort keys(%k_opcodes_cc);
51 print STDERR "Writing insnsa.c...\n";
53 open A, ">insnsa.c";
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";
60 print A "\n";
62 foreach $i (@opcodes, @opcodes_cc) {
63 print A "static struct itemplate instrux_${i}[] = {\n";
64 $aname = "aa_$i";
65 foreach $j (@$aname) {
66 print A " $j\n";
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";
74 print A "};\n";
76 close A;
78 print STDERR "Writing insnsd.c...\n";
80 open D, ">insnsd.c";
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";
87 print D "\n";
89 print D "static struct itemplate instrux[] = {\n";
90 foreach $j (@big) {
91 print D " $j\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";
98 $aname = "dd_$h";
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;
109 print D "};\n";
111 close D;
113 print STDERR "Writing insnsi.h...\n";
115 open I, ">insnsi.h";
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";
122 print I "enum {";
123 $first = 1;
124 $maxlen = 0;
125 foreach $i (@opcodes, @opcodes_cc) {
126 print I "," if ( !$first );
127 $first = 0;
128 print I "\n\tI_${i}";
129 $len = length($i);
130 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
131 $maxlen = $len if ( $len > $maxlen );
133 print I "\n};\n\n";
134 print I "#define MAX_INSLEN ", $maxlen, "\n";
136 close I;
138 print STDERR "Writing insnsn.c...\n";
140 open N, ">insnsn.c";
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[] = {";
147 $first = 1;
148 foreach $i (@opcodes) {
149 print N "," if ( !$first );
150 $first = 0;
151 $ilower = $i;
152 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
153 print N "\n\t\"${ilower}\"";
155 print N "\n};\n\n";
156 print N "/* Conditional instructions */\n";
157 print N "static char *icn[] = {";
158 $first = 1;
159 foreach $i (@opcodes_cc) {
160 print N "," if ( !$first );
161 $first = 0;
162 $ilower = $i;
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}\"";
168 print N "\n};\n\n";
169 print N "/* and the corresponding opcodes */\n";
170 print N "static int ico[] = {";
171 $first = 1;
172 foreach $i (@opcodes_cc) {
173 print N "," if ( !$first );
174 $first = 0;
175 print N "\n\tI_$i";
178 print N "\n};\n";
180 close N;
182 printf STDERR "Done: %d instructions\n", $insns;
184 sub format {
185 local ($opcode, $operands, $codes, $flags) = @_;
186 local $num, $nd = 0;
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;
198 $num = 3;
199 $operands = '0,0,0', $num = 0 if $operands eq 'void';
200 $operands .= ',0', $num-- while $operands !~ /,.*,/;
201 $operands =~ tr/a-z/A-Z/;
203 # format the flags
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
219 sub startbyte {
220 local ($codes) = @_;
221 local $word, @range;
223 while (1) {
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";
236 @range = ();
237 push @range, $start++ while ($r-- > 0);
238 @range;