NASM 0.93
[nasm/avx512.git] / insns.pl
blob275a66bce0a883518a77dae7437c1bd88625f34f
1 #!/usr/bin/perl
3 # insns.pl produce insnsa.c and insnsd.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 open (F, "insns.dat") || die "unable to open insns.dat";
14 $line = 0;
15 $opcodes = 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 = &format(@_);
25 if ($formatted) {
26 $insns++;
27 $aname = "aa_$_[0]";
28 push @$aname, $formatted;
30 $opcodes[$opcodes++] = $_[0], $done{$_[0]} = 1 if !$done{$_[0]};
31 if ($formatted && $formatted !~ /IF_ND/) {
32 push @big, $formatted;
33 foreach $i (&startbyte($_[2])) {
34 $aname = sprintf "dd_%02X",$i;
35 push @$aname, $#big;
40 close F;
42 print STDERR "Writing insnsa.c...\n";
44 open A, ">insnsa.c";
46 print A "/* This file auto-generated from insns.dat by insns.pl" .
47 " - don't edit it */\n\n";
48 print A "#include <stdio.h>\n";
49 print A "#include \"nasm.h\"\n";
50 print A "#include \"insns.h\"\n";
51 print A "\n";
53 foreach $i (@opcodes) {
54 print A "static struct itemplate instrux_${i}[] = {\n";
55 $aname = "aa_$i";
56 foreach $j (@$aname) {
57 print A " $j\n";
59 print A " {-1}\n};\n\n";
61 print A "struct itemplate *nasm_instructions[] = {\n";
62 foreach $i (@opcodes) {
63 print A " instrux_${i},\n";
65 print A "};\n";
67 close A;
69 print STDERR "Writing insnsd.c...\n";
71 open D, ">insnsd.c";
73 print D "/* This file auto-generated from insns.dat by insns.pl" .
74 " - don't edit it */\n\n";
75 print D "#include <stdio.h>\n";
76 print D "#include \"nasm.h\"\n";
77 print D "#include \"insns.h\"\n";
78 print D "\n";
80 print D "static struct itemplate instrux[] = {\n";
81 foreach $j (@big) {
82 print D " $j\n";
84 print D " {-1}\n};\n\n";
86 for ($c=0; $c<256; $c++) {
87 $h = sprintf "%02X", $c;
88 print D "static struct itemplate *itable_${h}[] = {\n";
89 $aname = "dd_$h";
90 foreach $j (@$aname) {
91 print D " instrux + $j,\n";
93 print D " NULL\n};\n\n";
96 print D "struct itemplate **itable[] = {\n";
97 for ($c=0; $c<256; $c++) {
98 printf D " itable_%02X,\n", $c;
100 print D "};\n";
102 close D;
104 printf STDERR "Done: %d instructions\n", $insns;
106 sub format {
107 local ($opcode, $operands, $codes, $flags) = @_;
108 local $num;
110 return undef if $operands eq "ignore";
112 # format the operands
113 $operands =~ s/:/|colon,/g;
114 $operands =~ s/mem(\d+)/mem|bits$1/g;
115 $operands =~ s/mem/memory/g;
116 $operands =~ s/memory_offs/mem_offs/g;
117 $operands =~ s/imm(\d+)/imm|bits$1/g;
118 $operands =~ s/imm/immediate/g;
119 $operands =~ s/rm(\d+)/regmem|bits$1/g;
120 $num = 3;
121 $operands = '0,0,0', $num = 0 if $operands eq 'void';
122 $operands .= ',0', $num-- while $operands !~ /,.*,/;
123 $operands =~ tr/a-z/A-Z/;
125 # format the flags
126 $flags =~ s/,/|IF_/g;
127 $flags = "IF_" . $flags;
129 "{I_$opcode, $num, {$operands}, \"$codes\", $flags},";
132 # Here we determine the range of possible starting bytes for a given
133 # instruction. We need only consider the codes:
134 # \1 \2 \3 mean literal bytes, of course
135 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
136 # \10 \11 \12 mean byte plus register value
137 # \17 means byte zero
138 # \330 means byte plus condition code
139 # \0 or \340 mean give up and return empty set
140 sub startbyte { # FIXME we cheat, for now :-)
141 local ($codes) = @_;
142 local $word, @range;
144 while (1) {
145 die "couldn't get code in '$codes'" if $codes !~ /^(\\[^\\]+)(\\.*)?$/;
146 $word = $1, $codes = $2;
147 return (hex $1) if $word =~ /^\\[123]$/ && $codes =~ /^\\x(..)/;
148 return (0x07, 0x17, 0x1F) if $word eq "\\4";
149 return (0xA1, 0xA9) if $word eq "\\5";
150 return (0x06, 0x0E, 0x16, 0x1E) if $word eq "\\6";
151 return (0xA0, 0xA8) if $word eq "\\7";
152 $start=hex $1, $r=8, last if $word =~ /^\\1[012]$/ && $codes =~/^\\x(..)/;
153 return (0) if $word eq "\\17";
154 $start=hex $1, $r=16, last if $word =~ /^\\330$/ && $codes =~ /^\\x(..)/;
155 return () if $word eq "\\0" || $word eq "\\340";
157 @range = ();
158 push @range, $start++ while ($r-- > 0);
159 @range;