NASM 0.98.17
[nasm/avx512.git] / insns.pl
blobc1eacb17e527d13164f8322e013548a79161baaf
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 @args = ();
13 undef $output;
14 foreach $arg ( @ARGV ) {
15 if ( $arg =~ /^\-/ ) {
16 if ( $arg =~ /^\-([adin])$/ ) {
17 $output = $1;
18 } else {
19 die "$0: Unknown option: ${arg}\n";
21 } else {
22 push (@args, $arg);
26 $fname = "insns.dat" unless $fname = $args[0];
27 open (F, $fname) || die "unable to open $fname";
29 $line = 0;
30 $insns = 0;
31 while (<F>) {
32 $line++;
33 next if /^\s*;/; # comments
34 chomp;
35 split;
36 next if $#_ == -1; # blank lines
37 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
38 ($formatted, $nd) = &format(@_);
39 if ($formatted) {
40 $insns++;
41 $aname = "aa_$_[0]";
42 push @$aname, $formatted;
44 if ( $_[0] =~ /cc$/ ) {
45 # Conditional instruction
46 $k_opcodes_cc{$_[0]}++;
47 } else {
48 # Unconditional instruction
49 $k_opcodes{$_[0]}++;
51 if ($formatted && !$nd) {
52 push @big, $formatted;
53 foreach $i (&startbyte($_[2])) {
54 $aname = sprintf "dd_%02X",$i;
55 push @$aname, $#big;
60 close F;
62 @opcodes = sort keys(%k_opcodes);
63 @opcodes_cc = sort keys(%k_opcodes_cc);
65 if ( !defined($output) || $output eq 'a' ) {
66 print STDERR "Writing insnsa.c...\n";
68 open A, ">insnsa.c";
70 print A "/* This file auto-generated from insns.dat by insns.pl" .
71 " - don't edit it */\n\n";
72 print A "#include <stdio.h>\n";
73 print A "#include \"nasm.h\"\n";
74 print A "#include \"insns.h\"\n";
75 print A "\n";
77 foreach $i (@opcodes, @opcodes_cc) {
78 print A "static struct itemplate instrux_${i}[] = {\n";
79 $aname = "aa_$i";
80 foreach $j (@$aname) {
81 print A " $j\n";
83 print A " {-1}\n};\n\n";
85 print A "struct itemplate *nasm_instructions[] = {\n";
86 foreach $i (@opcodes, @opcodes_cc) {
87 print A " instrux_${i},\n";
89 print A "};\n";
91 close A;
94 if ( !defined($output) || $output eq 'd' ) {
95 print STDERR "Writing insnsd.c...\n";
97 open D, ">insnsd.c";
99 print D "/* This file auto-generated from insns.dat by insns.pl" .
100 " - don't edit it */\n\n";
101 print D "#include <stdio.h>\n";
102 print D "#include \"nasm.h\"\n";
103 print D "#include \"insns.h\"\n";
104 print D "\n";
106 print D "static struct itemplate instrux[] = {\n";
107 foreach $j (@big) {
108 print D " $j\n";
110 print D " {-1}\n};\n\n";
112 for ($c=0; $c<256; $c++) {
113 $h = sprintf "%02X", $c;
114 print D "static struct itemplate *itable_${h}[] = {\n";
115 $aname = "dd_$h";
116 foreach $j (@$aname) {
117 print D " instrux + $j,\n";
119 print D " NULL\n};\n\n";
122 print D "struct itemplate **itable[] = {\n";
123 for ($c=0; $c<256; $c++) {
124 printf D " itable_%02X,\n", $c;
126 print D "};\n";
128 close D;
131 if ( !defined($output) || $output eq 'i' ) {
132 print STDERR "Writing insnsi.h...\n";
134 open I, ">insnsi.h";
136 print I "/* This file is auto-generated from insns.dat by insns.pl" .
137 " - don't edit it */\n\n";
138 print I "/* This file in included by nasm.h */\n\n";
140 print I "/* Instruction names */\n";
141 print I "enum {";
142 $first = 1;
143 $maxlen = 0;
144 foreach $i (@opcodes, @opcodes_cc) {
145 print I "," if ( !$first );
146 $first = 0;
147 print I "\n\tI_${i}";
148 $len = length($i);
149 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
150 $maxlen = $len if ( $len > $maxlen );
152 print I "\n};\n\n";
153 print I "#define MAX_INSLEN ", $maxlen, "\n";
155 close I;
158 if ( !defined($output) || $output eq 'n' ) {
159 print STDERR "Writing insnsn.c...\n";
161 open N, ">insnsn.c";
163 print N "/* This file is auto-generated from insns.dat by insns.pl" .
164 " - don't edit it */\n\n";
165 print N "/* This file in included by names.c */\n\n";
167 print N "static char *insn_names[] = {";
168 $first = 1;
169 foreach $i (@opcodes) {
170 print N "," if ( !$first );
171 $first = 0;
172 $ilower = $i;
173 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
174 print N "\n\t\"${ilower}\"";
176 print N "\n};\n\n";
177 print N "/* Conditional instructions */\n";
178 print N "static char *icn[] = {";
179 $first = 1;
180 foreach $i (@opcodes_cc) {
181 print N "," if ( !$first );
182 $first = 0;
183 $ilower = $i;
184 $ilower =~ s/cc$//; # Skip cc suffix
185 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
186 print N "\n\t\"${ilower}\"";
189 print N "\n};\n\n";
190 print N "/* and the corresponding opcodes */\n";
191 print N "static int ico[] = {";
192 $first = 1;
193 foreach $i (@opcodes_cc) {
194 print N "," if ( !$first );
195 $first = 0;
196 print N "\n\tI_$i";
199 print N "\n};\n";
201 close N;
204 printf STDERR "Done: %d instructions\n", $insns;
206 sub format {
207 local ($opcode, $operands, $codes, $flags) = @_;
208 local $num, $nd = 0;
210 return (undef, undef) if $operands eq "ignore";
212 # format the operands
213 $operands =~ s/:/|colon,/g;
214 $operands =~ s/mem(\d+)/mem|bits$1/g;
215 $operands =~ s/mem/memory/g;
216 $operands =~ s/memory_offs/mem_offs/g;
217 $operands =~ s/imm(\d+)/imm|bits$1/g;
218 $operands =~ s/imm/immediate/g;
219 $operands =~ s/rm(\d+)/regmem|bits$1/g;
220 $num = 3;
221 $operands = '0,0,0', $num = 0 if $operands eq 'void';
222 $operands .= ',0', $num-- while $operands !~ /,.*,/;
223 $operands =~ tr/a-z/A-Z/;
225 # format the flags
226 $flags =~ s/,/|IF_/g;
227 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
228 $flags = "IF_" . $flags;
230 ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
233 # Here we determine the range of possible starting bytes for a given
234 # instruction. We need only consider the codes:
235 # \1 \2 \3 mean literal bytes, of course
236 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
237 # \10 \11 \12 mean byte plus register value
238 # \17 means byte zero
239 # \330 means byte plus condition code
240 # \0 or \340 mean give up and return empty set
241 sub startbyte {
242 local ($codes) = @_;
243 local $word, @range;
245 while (1) {
246 die "couldn't get code in '$codes'" if $codes !~ /^(\\[^\\]+)(\\.*)?$/;
247 $word = $1, $codes = $2;
248 return (hex $1) if $word =~ /^\\[123]$/ && $codes =~ /^\\x(..)/;
249 return (0x07, 0x17, 0x1F) if $word eq "\\4";
250 return (0xA1, 0xA9) if $word eq "\\5";
251 return (0x06, 0x0E, 0x16, 0x1E) if $word eq "\\6";
252 return (0xA0, 0xA8) if $word eq "\\7";
253 $start=hex $1, $r=8, last if $word =~ /^\\1[012]$/ && $codes =~/^\\x(..)/;
254 return (0) if $word eq "\\17";
255 $start=hex $1, $r=16, last if $word =~ /^\\330$/ && $codes =~ /^\\x(..)/;
256 return () if $word eq "\\0" || $word eq "\\340";
258 @range = ();
259 push @range, $start++ while ($r-- > 0);
260 @range;