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";
20 next if /^\s*;/; # comments
23 next if $#_ == -1; # blank lines
24 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
25 ($formatted, $nd) = &format
(@_);
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;
43 print STDERR
"Writing insnsa.c...\n";
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";
54 foreach $i (@opcodes) {
55 print A
"static struct itemplate instrux_${i}[] = {\n";
57 foreach $j (@
$aname) {
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";
70 print STDERR
"Writing insnsd.c...\n";
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";
81 print D
"static struct itemplate instrux[] = {\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";
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;
105 print STDERR
"Writing insnsi.h...\n";
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";
117 foreach $i (@opcodes) {
118 print I
"," if ( !$first );
120 print I
"\n\tI_${i}";
122 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
123 $maxlen = $len if ( $len > $maxlen );
126 print I
"#define MAX_INSLEN ", $maxlen, "\n";
130 print STDERR
"Writing insnsn.c...\n";
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[] = {";
140 foreach $i (@opcodes) {
141 # Don't include conditional instructions
143 print N
"," if ( !$first );
146 $ilower =~ tr/A-Z/a-z/;
147 print N
"\n\t\"${ilower}\"";
151 print N
"/* Conditional instructions */\n";
152 print N
"static char *icn[] = {";
154 foreach $i (@opcodes) {
155 # Only conditional instructions
157 $ins = $`; # Skip cc suffix
158 print N "," if ( !$first );
161 $ilower =~ tr/A-Z/a-z/;
162 print N "\n\t\"${ilower}\"";
167 print N "/* and the corresponding opcodes */\n";
168 print N "static int ico[] = {";
170 foreach $i (@opcodes) {
171 # Only conditional instructions
173 $ins = $`; # Skip cc suffix
174 print N
"," if ( !$first );
184 printf STDERR
"Done: %d instructions\n", $insns;
187 local ($opcode, $operands, $codes, $flags) = @_;
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;
201 $operands = '0,0,0', $num = 0 if $operands eq 'void';
202 $operands .= ',0', $num-- while $operands !~ /,.*,/;
203 $operands =~ tr/a-z/A-Z/;
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
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";
239 push @range, $start++ while ($r-- > 0);