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";
19 next if /^\s*;/; # comments
22 next if $#_ == -1; # blank lines
23 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
24 $formatted = &format
(@_);
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;
42 print STDERR
"Writing insnsa.c...\n";
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";
53 foreach $i (@opcodes) {
54 print A
"static struct itemplate instrux_${i}[] = {\n";
56 foreach $j (@
$aname) {
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";
69 print STDERR
"Writing insnsd.c...\n";
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";
80 print D
"static struct itemplate instrux[] = {\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";
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;
104 printf STDERR
"Done: %d instructions\n", $insns;
107 local ($opcode, $operands, $codes, $flags) = @_;
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;
121 $operands = '0,0,0', $num = 0 if $operands eq 'void';
122 $operands .= ',0', $num-- while $operands !~ /,.*,/;
123 $operands =~ tr/a-z/A-Z/;
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 :-)
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";
158 push @range, $start++ while ($r-- > 0);