NASM 0.98p3-hpa
[nasm/avx512.git] / insns.pl
blob8e2ac1e0f12318f46c688f3a9329434d61f2f410
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 $opcodes = 0;
17 $insns = 0;
18 while (<F>) {
19 $line++;
20 next if /^\s*;/; # comments
21 chomp;
22 split;
23 next if $#_ == -1; # blank lines
24 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
25 ($formatted, $nd) = &format(@_);
26 if ($formatted) {
27 $insns++;
28 $aname = "aa_$_[0]";
29 push @$aname, $formatted;
31 $opcodes[$opcodes++] = $_[0], $done{$_[0]} = 1 if !$done{$_[0]};
32 if ($formatted && !$nd) {
33 push @big, $formatted;
34 foreach $i (&startbyte($_[2])) {
35 $aname = sprintf "dd_%02X",$i;
36 push @$aname, $#big;
41 close F;
43 print STDERR "Writing insnsa.c...\n";
45 open A, ">insnsa.c";
47 print A "/* This file auto-generated from insns.dat by insns.pl" .
48 " - don't edit it */\n\n";
49 print A "#include <stdio.h>\n";
50 print A "#include \"nasm.h\"\n";
51 print A "#include \"insns.h\"\n";
52 print A "\n";
54 foreach $i (@opcodes) {
55 print A "static struct itemplate instrux_${i}[] = {\n";
56 $aname = "aa_$i";
57 foreach $j (@$aname) {
58 print A " $j\n";
60 print A " {-1}\n};\n\n";
62 print A "struct itemplate *nasm_instructions[] = {\n";
63 foreach $i (@opcodes) {
64 print A " instrux_${i},\n";
66 print A "};\n";
68 close A;
70 print STDERR "Writing insnsd.c...\n";
72 open D, ">insnsd.c";
74 print D "/* This file auto-generated from insns.dat by insns.pl" .
75 " - don't edit it */\n\n";
76 print D "#include <stdio.h>\n";
77 print D "#include \"nasm.h\"\n";
78 print D "#include \"insns.h\"\n";
79 print D "\n";
81 print D "static struct itemplate instrux[] = {\n";
82 foreach $j (@big) {
83 print D " $j\n";
85 print D " {-1}\n};\n\n";
87 for ($c=0; $c<256; $c++) {
88 $h = sprintf "%02X", $c;
89 print D "static struct itemplate *itable_${h}[] = {\n";
90 $aname = "dd_$h";
91 foreach $j (@$aname) {
92 print D " instrux + $j,\n";
94 print D " NULL\n};\n\n";
97 print D "struct itemplate **itable[] = {\n";
98 for ($c=0; $c<256; $c++) {
99 printf D " itable_%02X,\n", $c;
101 print D "};\n";
103 close D;
105 print STDERR "Writing insnsi.h...\n";
107 open I, ">insnsi.h";
109 print I "/* This file is auto-generated from insns.dat by insns.pl" .
110 " - don't exit it */\n\n";
111 print I "/* This file in included by nasm.h */\n\n";
113 print I "/* Instruction names */\n";
114 print I "enum {";
115 $first = 1;
116 $maxlen = 0;
117 foreach $i (@opcodes) {
118 print I "," if ( !$first );
119 $first = 0;
120 print I "\n\tI_${i}";
121 $len = length($i);
122 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
123 $maxlen = $len if ( $len > $maxlen );
125 print I "\n};\n\n";
126 print I "#define MAX_INSLEN ", $maxlen, "\n";
128 close I;
130 print STDERR "Writing insnsn.c...\n";
132 open N, ">insnsn.c";
134 print N "/* This file is auto-generated from insns.dat by insns.pl" .
135 " - don't exit it */\n\n";
136 print N "/* This file in included by names.c */\n\n";
138 print N "static char *insn_names[] = {";
139 $first = 1;
140 foreach $i (@opcodes) {
141 # Don't include conditional instructions
142 if ( $i !~ /cc$/ ) {
143 print N "," if ( !$first );
144 $first = 0;
145 $ilower = $i;
146 $ilower =~ tr/A-Z/a-z/;
147 print N "\n\t\"${ilower}\"";
150 print N "\n};\n\n";
151 print N "/* Conditional instructions */\n";
152 print N "static char *icn[] = {";
153 $first = 1;
154 foreach $i (@opcodes) {
155 # Only conditional instructions
156 if ( $i =~ /cc$/ ) {
157 $ins = $`; # Skip cc suffix
158 print N "," if ( !$first );
159 $first = 0;
160 $ilower = $i;
161 $ilower =~ tr/A-Z/a-z/;
162 print N "\n\t\"${ilower}\"";
166 print N "\n};\n\n";
167 print N "/* and the corresponding opcodes */\n";
168 print N "static int ico[] = {";
169 $first = 1;
170 foreach $i (@opcodes) {
171 # Only conditional instructions
172 if ( $i =~ /cc$/ ) {
173 $ins = $`; # Skip cc suffix
174 print N "," if ( !$first );
175 $first = 0;
176 print N "\n\tI_$i";
180 print N "\n};\n";
182 close N;
184 printf STDERR "Done: %d instructions\n", $insns;
186 sub format {
187 local ($opcode, $operands, $codes, $flags) = @_;
188 local $num, $nd = 0;
190 return (undef, undef) if $operands eq "ignore";
192 # format the operands
193 $operands =~ s/:/|colon,/g;
194 $operands =~ s/mem(\d+)/mem|bits$1/g;
195 $operands =~ s/mem/memory/g;
196 $operands =~ s/memory_offs/mem_offs/g;
197 $operands =~ s/imm(\d+)/imm|bits$1/g;
198 $operands =~ s/imm/immediate/g;
199 $operands =~ s/rm(\d+)/regmem|bits$1/g;
200 $num = 3;
201 $operands = '0,0,0', $num = 0 if $operands eq 'void';
202 $operands .= ',0', $num-- while $operands !~ /,.*,/;
203 $operands =~ tr/a-z/A-Z/;
205 # format the flags
206 $flags =~ s/,/|IF_/g;
207 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
208 $flags = "IF_" . $flags;
210 ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
213 # Here we determine the range of possible starting bytes for a given
214 # instruction. We need only consider the codes:
215 # \1 \2 \3 mean literal bytes, of course
216 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
217 # \10 \11 \12 mean byte plus register value
218 # \17 means byte zero
219 # \330 means byte plus condition code
220 # \0 or \340 mean give up and return empty set
221 sub startbyte {
222 local ($codes) = @_;
223 local $word, @range;
225 while (1) {
226 die "couldn't get code in '$codes'" if $codes !~ /^(\\[^\\]+)(\\.*)?$/;
227 $word = $1, $codes = $2;
228 return (hex $1) if $word =~ /^\\[123]$/ && $codes =~ /^\\x(..)/;
229 return (0x07, 0x17, 0x1F) if $word eq "\\4";
230 return (0xA1, 0xA9) if $word eq "\\5";
231 return (0x06, 0x0E, 0x16, 0x1E) if $word eq "\\6";
232 return (0xA0, 0xA8) if $word eq "\\7";
233 $start=hex $1, $r=8, last if $word =~ /^\\1[012]$/ && $codes =~/^\\x(..)/;
234 return (0) if $word eq "\\17";
235 $start=hex $1, $r=16, last if $word =~ /^\\330$/ && $codes =~ /^\\x(..)/;
236 return () if $word eq "\\0" || $word eq "\\340";
238 @range = ();
239 push @range, $start++ while ($r-- > 0);
240 @range;