2002-05-01 04:51:32 +08:00
|
|
|
#!/usr/bin/perl
|
2007-10-20 05:42:29 +08:00
|
|
|
#
|
2002-05-01 04:54:13 +08:00
|
|
|
# insns.pl produce insnsa.c, insnsd.c, insnsi.h, insnsn.c from insns.dat
|
2002-05-01 04:51:32 +08:00
|
|
|
#
|
|
|
|
# The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
|
|
|
# Julian Hall. All rights reserved. The software is
|
2007-12-29 22:44:23 +08:00
|
|
|
# redistributable under the license given in the file "LICENSE"
|
2002-05-01 04:51:32 +08:00
|
|
|
# distributed in the NASM archive.
|
|
|
|
|
2007-09-19 06:08:20 +08:00
|
|
|
# Opcode prefixes which need their own opcode tables
|
|
|
|
# LONGER PREFIXES FIRST!
|
2007-09-25 06:55:20 +08:00
|
|
|
@disasm_prefixes = qw(0F24 0F25 0F38 0F3A 0F7A 0FA6 0FA7 0F);
|
2007-09-19 06:08:20 +08:00
|
|
|
|
2008-05-13 02:36:24 +08:00
|
|
|
# This should match MAX_OPERANDS from nasm.h
|
|
|
|
$MAX_OPERANDS = 5;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
print STDERR "Reading insns.dat...\n";
|
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
@args = ();
|
|
|
|
undef $output;
|
|
|
|
foreach $arg ( @ARGV ) {
|
|
|
|
if ( $arg =~ /^\-/ ) {
|
2008-05-13 02:00:50 +08:00
|
|
|
if ( $arg =~ /^\-([abdin])$/ ) {
|
2002-05-01 04:58:18 +08:00
|
|
|
$output = $1;
|
|
|
|
} else {
|
|
|
|
die "$0: Unknown option: ${arg}\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
push (@args, $arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fname = "insns.dat" unless $fname = $args[0];
|
2002-05-01 04:52:49 +08:00
|
|
|
open (F, $fname) || die "unable to open $fname";
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-19 06:08:20 +08:00
|
|
|
%dinstables = ();
|
2008-05-13 02:00:50 +08:00
|
|
|
@bytecode_list = ();
|
2007-09-19 06:08:20 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
$line = 0;
|
|
|
|
$insns = 0;
|
|
|
|
while (<F>) {
|
|
|
|
$line++;
|
|
|
|
chomp;
|
2008-05-20 09:19:42 +08:00
|
|
|
next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
|
|
|
|
|
|
|
|
unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
|
|
|
|
warn "line $line does not contain four fields\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
@fields = ($1, $2, $3, $4);
|
2008-05-20 10:08:03 +08:00
|
|
|
($formatted, $nd) = format_insn(@fields);
|
2002-05-01 04:51:32 +08:00
|
|
|
if ($formatted) {
|
|
|
|
$insns++;
|
2008-05-20 09:19:42 +08:00
|
|
|
$aname = "aa_$fields[0]";
|
2002-05-01 04:51:32 +08:00
|
|
|
push @$aname, $formatted;
|
|
|
|
}
|
2008-05-20 09:19:42 +08:00
|
|
|
if ( $fields[0] =~ /cc$/ ) {
|
2002-05-01 04:56:19 +08:00
|
|
|
# Conditional instruction
|
2008-05-20 09:19:42 +08:00
|
|
|
$k_opcodes_cc{$fields[0]}++;
|
2002-05-01 04:55:37 +08:00
|
|
|
} else {
|
2002-05-01 04:56:19 +08:00
|
|
|
# Unconditional instruction
|
2008-05-20 09:19:42 +08:00
|
|
|
$k_opcodes{$fields[0]}++;
|
2002-05-01 04:55:37 +08:00
|
|
|
}
|
2002-05-01 04:52:08 +08:00
|
|
|
if ($formatted && !$nd) {
|
2002-05-01 04:51:32 +08:00
|
|
|
push @big, $formatted;
|
2008-05-20 09:19:42 +08:00
|
|
|
my @sseq = startseq($fields[2]);
|
2007-11-19 13:55:26 +08:00
|
|
|
foreach $i (@sseq) {
|
2007-09-19 06:08:20 +08:00
|
|
|
if (!defined($dinstables{$i})) {
|
|
|
|
$dinstables{$i} = [];
|
|
|
|
}
|
|
|
|
push(@{$dinstables{$i}}, $#big);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close F;
|
|
|
|
|
2008-05-13 02:00:50 +08:00
|
|
|
#
|
|
|
|
# Generate the bytecode array. At this point, @bytecode_list contains
|
|
|
|
# the full set of bytecodes.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Sort by descending length
|
|
|
|
@bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
|
|
|
|
@bytecode_array = ();
|
|
|
|
%bytecode_pos = ();
|
|
|
|
$bytecode_next = 0;
|
|
|
|
foreach $bl (@bytecode_list) {
|
|
|
|
my $h = hexstr(@$bl);
|
|
|
|
next if (defined($bytecode_pos{$h}));
|
|
|
|
|
|
|
|
push(@bytecode_array, $bl);
|
|
|
|
while ($h ne '') {
|
|
|
|
$bytecode_pos{$h} = $bytecode_next;
|
|
|
|
$h = substr($h, 2);
|
|
|
|
$bytecode_next++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
undef @bytecode_list;
|
|
|
|
|
2002-05-01 04:56:19 +08:00
|
|
|
@opcodes = sort keys(%k_opcodes);
|
|
|
|
@opcodes_cc = sort keys(%k_opcodes_cc);
|
2002-05-01 04:55:37 +08:00
|
|
|
|
2008-05-13 02:00:50 +08:00
|
|
|
if ( !defined($output) || $output eq 'b') {
|
|
|
|
print STDERR "Writing insnsb.c...\n";
|
|
|
|
|
|
|
|
open B, ">insnsb.c";
|
2008-05-20 10:08:03 +08:00
|
|
|
|
2008-05-13 02:00:50 +08:00
|
|
|
print B "/* This file auto-generated from insns.dat by insns.pl" .
|
|
|
|
" - don't edit it */\n\n";
|
|
|
|
|
|
|
|
print B "#include \"nasm.h\"\n";
|
|
|
|
print B "#include \"insns.h\"\n\n";
|
|
|
|
|
2008-05-14 05:29:47 +08:00
|
|
|
print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
|
2008-05-13 02:00:50 +08:00
|
|
|
|
|
|
|
$p = 0;
|
|
|
|
foreach $bl (@bytecode_array) {
|
2008-05-13 06:28:33 +08:00
|
|
|
printf B " /* %5d */ ", $p;
|
2008-05-13 02:00:50 +08:00
|
|
|
foreach $d (@$bl) {
|
|
|
|
printf B "%#o,", $d;
|
|
|
|
$p++;
|
|
|
|
}
|
|
|
|
printf B "\n";
|
|
|
|
}
|
|
|
|
print B "};\n";
|
|
|
|
|
|
|
|
close B;
|
|
|
|
}
|
2008-05-20 10:08:03 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
if ( !defined($output) || $output eq 'a' ) {
|
|
|
|
print STDERR "Writing insnsa.c...\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
open A, ">insnsa.c";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
print A "/* This file auto-generated from insns.dat by insns.pl" .
|
2002-05-01 04:51:32 +08:00
|
|
|
" - don't edit it */\n\n";
|
2008-05-13 02:00:50 +08:00
|
|
|
|
2008-05-14 05:29:47 +08:00
|
|
|
print A "#include \"nasm.h\"\n";
|
|
|
|
print A "#include \"insns.h\"\n\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
foreach $i (@opcodes, @opcodes_cc) {
|
2007-09-11 12:16:57 +08:00
|
|
|
print A "static const struct itemplate instrux_${i}[] = {\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
$aname = "aa_$i";
|
|
|
|
foreach $j (@$aname) {
|
2008-05-13 02:00:50 +08:00
|
|
|
print A " ", codesubst($j), "\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
}
|
2002-05-15 06:38:55 +08:00
|
|
|
print A " ITEMPLATE_END\n};\n\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
}
|
2007-09-11 12:16:57 +08:00
|
|
|
print A "const struct itemplate * const nasm_instructions[] = {\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
foreach $i (@opcodes, @opcodes_cc) {
|
|
|
|
print A " instrux_${i},\n";
|
|
|
|
}
|
|
|
|
print A "};\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
close A;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
if ( !defined($output) || $output eq 'd' ) {
|
|
|
|
print STDERR "Writing insnsd.c...\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
open D, ">insnsd.c";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
print D "/* This file auto-generated from insns.dat by insns.pl" .
|
2002-05-01 04:51:32 +08:00
|
|
|
" - don't edit it */\n\n";
|
2008-05-13 02:00:50 +08:00
|
|
|
|
2008-05-14 05:29:47 +08:00
|
|
|
print D "#include \"nasm.h\"\n";
|
|
|
|
print D "#include \"insns.h\"\n\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-11 12:16:57 +08:00
|
|
|
print D "static const struct itemplate instrux[] = {\n";
|
2007-09-18 09:45:44 +08:00
|
|
|
$n = 0;
|
2002-05-01 04:58:18 +08:00
|
|
|
foreach $j (@big) {
|
2008-05-13 02:00:50 +08:00
|
|
|
printf D " /* %4d */ %s\n", $n++, codesubst($j);
|
2002-05-01 04:58:18 +08:00
|
|
|
}
|
2007-09-19 06:08:20 +08:00
|
|
|
print D "};\n";
|
|
|
|
|
|
|
|
foreach $h (sort(keys(%dinstables))) {
|
|
|
|
print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
|
|
|
|
foreach $j (@{$dinstables{$h}}) {
|
2002-05-01 04:58:18 +08:00
|
|
|
print D " instrux + $j,\n";
|
|
|
|
}
|
2007-09-19 06:08:20 +08:00
|
|
|
print D "};\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
}
|
2007-09-19 06:08:20 +08:00
|
|
|
|
|
|
|
foreach $h (@disasm_prefixes, '') {
|
|
|
|
$is_prefix{$h} = 1;
|
|
|
|
print D "\n";
|
|
|
|
print D "static " unless ($h eq '');
|
|
|
|
print D "const struct disasm_index ";
|
|
|
|
print D ($h eq '') ? 'itable' : "itable_$h";
|
|
|
|
print D "[256] = {\n";
|
|
|
|
for ($c = 0; $c < 256; $c++) {
|
|
|
|
$nn = sprintf("%s%02X", $h, $c);
|
|
|
|
if ($is_prefix{$nn}) {
|
|
|
|
die "$0: ambiguous decoding of $nn\n"
|
|
|
|
if (defined($dinstables{$nn}));
|
|
|
|
printf D " { itable_%s, -1 },\n", $nn;
|
|
|
|
} elsif (defined($dinstables{$nn})) {
|
|
|
|
printf D " { itable_%s, %u },\n",
|
2007-10-20 05:42:29 +08:00
|
|
|
$nn, scalar(@{$dinstables{$nn}});
|
2007-09-19 06:08:20 +08:00
|
|
|
} else {
|
|
|
|
printf D " { NULL, 0 },\n";
|
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:58:18 +08:00
|
|
|
print D "};\n";
|
2007-09-19 06:08:20 +08:00
|
|
|
}
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
close D;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 04:54:13 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
if ( !defined($output) || $output eq 'i' ) {
|
|
|
|
print STDERR "Writing insnsi.h...\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
open I, ">insnsi.h";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
print I "/* This file is auto-generated from insns.dat by insns.pl" .
|
2002-05-01 04:55:37 +08:00
|
|
|
" - don't edit it */\n\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
print I "/* This file in included by nasm.h */\n\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-13 11:27:41 +08:00
|
|
|
print I "/* Instruction names */\n\n";
|
|
|
|
print I "#ifndef NASM_INSNSI_H\n";
|
|
|
|
print I "#define NASM_INSNSI_H 1\n\n";
|
|
|
|
print I "enum opcode {\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
$maxlen = 0;
|
|
|
|
foreach $i (@opcodes, @opcodes_cc) {
|
2007-09-13 11:27:41 +08:00
|
|
|
print I "\tI_${i},\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
$len = length($i);
|
|
|
|
$len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
|
|
|
|
$maxlen = $len if ( $len > $maxlen );
|
|
|
|
}
|
2007-09-13 11:27:41 +08:00
|
|
|
print I "\tI_none = -1\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
print I "\n};\n\n";
|
2007-09-13 11:27:41 +08:00
|
|
|
print I "#define MAX_INSLEN ", $maxlen, "\n\n";
|
|
|
|
print I "#endif /* NASM_INSNSI_H */\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
close I;
|
2002-05-01 04:54:13 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
if ( !defined($output) || $output eq 'n' ) {
|
|
|
|
print STDERR "Writing insnsn.c...\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
open N, ">insnsn.c";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
print N "/* This file is auto-generated from insns.dat by insns.pl" .
|
2002-05-01 04:55:37 +08:00
|
|
|
" - don't edit it */\n\n";
|
2002-05-01 04:58:18 +08:00
|
|
|
print N "/* This file in included by names.c */\n\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-11 12:16:57 +08:00
|
|
|
print N "static const char * const insn_names[] = {";
|
2002-05-01 04:58:18 +08:00
|
|
|
$first = 1;
|
|
|
|
foreach $i (@opcodes) {
|
|
|
|
print N "," if ( !$first );
|
|
|
|
$first = 0;
|
|
|
|
$ilower = $i;
|
|
|
|
$ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
|
|
|
|
print N "\n\t\"${ilower}\"";
|
|
|
|
}
|
|
|
|
print N "\n};\n\n";
|
|
|
|
print N "/* Conditional instructions */\n";
|
2007-04-14 00:47:53 +08:00
|
|
|
print N "static const char *icn[] = {";
|
2002-05-01 04:58:18 +08:00
|
|
|
$first = 1;
|
|
|
|
foreach $i (@opcodes_cc) {
|
|
|
|
print N "," if ( !$first );
|
|
|
|
$first = 0;
|
|
|
|
$ilower = $i;
|
|
|
|
$ilower =~ s/cc$//; # Skip cc suffix
|
|
|
|
$ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
|
|
|
|
print N "\n\t\"${ilower}\"";
|
|
|
|
}
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
print N "\n};\n\n";
|
|
|
|
print N "/* and the corresponding opcodes */\n";
|
2007-09-12 06:14:18 +08:00
|
|
|
print N "static const enum opcode ico[] = {";
|
2002-05-01 04:58:18 +08:00
|
|
|
$first = 1;
|
|
|
|
foreach $i (@opcodes_cc) {
|
|
|
|
print N "," if ( !$first );
|
|
|
|
$first = 0;
|
|
|
|
print N "\n\tI_$i";
|
|
|
|
}
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
print N "\n};\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
close N;
|
2002-05-01 04:54:13 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
printf STDERR "Done: %d instructions\n", $insns;
|
|
|
|
|
2008-05-20 10:08:03 +08:00
|
|
|
sub format_insn(@) {
|
2007-09-18 06:49:30 +08:00
|
|
|
my ($opcode, $operands, $codes, $flags) = @_;
|
|
|
|
my $num, $nd = 0;
|
2008-05-13 02:00:50 +08:00
|
|
|
my @bytecode;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
return (undef, undef) if $operands eq "ignore";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
# format the operands
|
|
|
|
$operands =~ s/:/|colon,/g;
|
|
|
|
$operands =~ s/mem(\d+)/mem|bits$1/g;
|
|
|
|
$operands =~ s/mem/memory/g;
|
|
|
|
$operands =~ s/memory_offs/mem_offs/g;
|
|
|
|
$operands =~ s/imm(\d+)/imm|bits$1/g;
|
|
|
|
$operands =~ s/imm/immediate/g;
|
|
|
|
$operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
|
2008-05-05 08:53:31 +08:00
|
|
|
$operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
|
2007-09-18 08:25:27 +08:00
|
|
|
$operands =~ s/\=([0-9]+)/same_as|$1/g;
|
2007-09-18 06:49:30 +08:00
|
|
|
if ($operands eq 'void') {
|
|
|
|
@ops = ();
|
|
|
|
} else {
|
|
|
|
@ops = split(/\,/, $operands);
|
|
|
|
}
|
|
|
|
$num = scalar(@ops);
|
2008-05-13 02:36:24 +08:00
|
|
|
while (scalar(@ops) < $MAX_OPERANDS) {
|
2007-09-18 06:49:30 +08:00
|
|
|
push(@ops, '0');
|
|
|
|
}
|
|
|
|
$operands = join(',', @ops);
|
|
|
|
$operands =~ tr/a-z/A-Z/;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
# format the flags
|
|
|
|
$flags =~ s/,/|IF_/g;
|
|
|
|
$flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
|
|
|
|
$flags = "IF_" . $flags;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2008-05-13 02:00:50 +08:00
|
|
|
@bytecode = (decodify($codes), 0);
|
|
|
|
push(@bytecode_list, [@bytecode]);
|
|
|
|
$codes = hexstr(@bytecode);
|
|
|
|
|
|
|
|
("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Look for @@CODES-xxx@@ sequences and replace them with the appropriate
|
|
|
|
# offset into nasm_bytecodes
|
|
|
|
#
|
|
|
|
sub codesubst($) {
|
|
|
|
my($s) = @_;
|
|
|
|
my $n;
|
|
|
|
|
|
|
|
while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
|
|
|
|
my $pos = $bytecode_pos{$1};
|
|
|
|
if (!defined($pos)) {
|
|
|
|
die "$0: no position assigned to byte code $1\n";
|
|
|
|
}
|
|
|
|
$s = $` . "nasm_bytecodes+${pos}" . "$'";
|
|
|
|
}
|
|
|
|
return $s;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-11-19 13:55:26 +08:00
|
|
|
sub addprefix ($@) {
|
|
|
|
my ($prefix, @list) = @_;
|
|
|
|
my $x;
|
2007-09-19 06:08:20 +08:00
|
|
|
my @l = ();
|
|
|
|
|
2007-11-19 13:55:26 +08:00
|
|
|
foreach $x (@list) {
|
|
|
|
push(@l, sprintf("%s%02X", $prefix, $x));
|
2007-09-19 06:08:20 +08:00
|
|
|
}
|
2007-11-19 13:55:26 +08:00
|
|
|
|
2007-09-19 06:08:20 +08:00
|
|
|
return @l;
|
|
|
|
}
|
|
|
|
|
2008-05-13 01:17:27 +08:00
|
|
|
#
|
|
|
|
# Turn a code string into a sequence of bytes
|
|
|
|
#
|
|
|
|
sub decodify($) {
|
|
|
|
# Although these are C-syntax strings, by convention they should have
|
|
|
|
# only octal escapes (for directives) and hexadecimal escapes
|
|
|
|
# (for verbatim bytes)
|
|
|
|
my($codestr) = @_;
|
|
|
|
my $c = $codestr;
|
|
|
|
my @codes = ();
|
|
|
|
|
2008-05-20 09:19:42 +08:00
|
|
|
if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
|
|
|
|
return byte_code_compile($1);
|
|
|
|
}
|
|
|
|
|
2008-05-13 01:17:27 +08:00
|
|
|
while ($c ne '') {
|
|
|
|
if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
|
|
|
|
push(@codes, hex $1);
|
|
|
|
$c = $2;
|
|
|
|
next;
|
|
|
|
} elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
|
|
|
|
push(@codes, oct $1);
|
|
|
|
$c = $2;
|
|
|
|
next;
|
|
|
|
} else {
|
|
|
|
die "$0: unknown code format in \"$codestr\"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @codes;
|
|
|
|
}
|
|
|
|
|
2008-05-13 02:00:50 +08:00
|
|
|
# Turn a numeric list into a hex string
|
|
|
|
sub hexstr(@) {
|
|
|
|
my $s = '';
|
|
|
|
my $c;
|
|
|
|
|
|
|
|
foreach $c (@_) {
|
|
|
|
$s .= sprintf("%02X", $c);
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
# Here we determine the range of possible starting bytes for a given
|
|
|
|
# instruction. We need only consider the codes:
|
|
|
|
# \1 \2 \3 mean literal bytes, of course
|
|
|
|
# \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
|
2007-09-18 06:49:30 +08:00
|
|
|
# \1[0123] mean byte plus register value
|
2002-05-01 04:51:32 +08:00
|
|
|
# \330 means byte plus condition code
|
|
|
|
# \0 or \340 mean give up and return empty set
|
2007-09-19 06:08:20 +08:00
|
|
|
sub startseq($) {
|
|
|
|
my ($codestr) = @_;
|
2007-09-18 06:49:30 +08:00
|
|
|
my $word, @range;
|
2007-09-19 06:08:20 +08:00
|
|
|
my @codes = ();
|
|
|
|
my $c = $codestr;
|
|
|
|
my $c0, $c1, $i;
|
|
|
|
my $prefix = '';
|
|
|
|
|
2008-05-13 01:17:27 +08:00
|
|
|
@codes = decodify($codestr);
|
2007-09-19 06:08:20 +08:00
|
|
|
|
|
|
|
while ($c0 = shift(@codes)) {
|
|
|
|
$c1 = $codes[0];
|
2008-05-13 02:13:41 +08:00
|
|
|
if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
|
2007-09-19 06:08:20 +08:00
|
|
|
# Fixed byte string
|
|
|
|
my $fbs = $prefix;
|
|
|
|
while (1) {
|
|
|
|
if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
|
|
|
|
while ($c0--) {
|
|
|
|
$fbs .= sprintf("%02X", shift(@codes));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
$c0 = shift(@codes);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach $pfx (@disasm_prefixes) {
|
2007-11-19 13:55:26 +08:00
|
|
|
if (substr($fbs, 0, length($pfx)) eq $pfx) {
|
2007-09-19 06:08:20 +08:00
|
|
|
$prefix = $pfx;
|
2007-11-19 13:55:26 +08:00
|
|
|
$fbs = substr($fbs, length($pfx));
|
2007-09-19 06:08:20 +08:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-19 06:08:20 +08:00
|
|
|
if ($fbs ne '') {
|
|
|
|
return ($prefix.substr($fbs,0,2));
|
|
|
|
}
|
2007-11-19 13:55:26 +08:00
|
|
|
|
|
|
|
unshift(@codes, $c0);
|
2007-09-19 06:08:20 +08:00
|
|
|
} elsif ($c0 == 04) {
|
2007-11-19 13:55:26 +08:00
|
|
|
return addprefix($prefix, 0x07, 0x17, 0x1F);
|
2007-09-19 06:08:20 +08:00
|
|
|
} elsif ($c0 == 05) {
|
2007-11-19 13:55:26 +08:00
|
|
|
return addprefix($prefix, 0xA1, 0xA9);
|
2007-09-19 06:08:20 +08:00
|
|
|
} elsif ($c0 == 06) {
|
2007-11-19 13:55:26 +08:00
|
|
|
return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
|
2007-09-19 06:08:20 +08:00
|
|
|
} elsif ($c0 == 07) {
|
2007-11-19 13:55:26 +08:00
|
|
|
return addprefix($prefix, 0xA0, 0xA8);
|
2007-09-19 06:08:20 +08:00
|
|
|
} elsif ($c0 >= 010 && $c0 <= 013) {
|
2007-11-19 13:55:26 +08:00
|
|
|
return addprefix($prefix, $c1..($c1+7));
|
|
|
|
} elsif (($c0 & ~013) == 0144) {
|
|
|
|
return addprefix($prefix, $c1, $c1|2);
|
2007-09-19 06:08:20 +08:00
|
|
|
} elsif ($c0 == 0330) {
|
2007-11-19 13:55:26 +08:00
|
|
|
return addprefix($prefix, $c1..($c1+15));
|
2007-09-19 06:08:20 +08:00
|
|
|
} elsif ($c0 == 0 || $c0 == 0340) {
|
2007-11-19 13:55:26 +08:00
|
|
|
return $prefix;
|
2008-05-06 09:47:27 +08:00
|
|
|
} elsif (($c0 & ~3) == 0260 || $c0 == 270) {
|
|
|
|
shift(@codes);
|
|
|
|
shift(@codes);
|
|
|
|
} elsif ($c0 == 0172) {
|
|
|
|
shift(@codes);
|
2007-11-19 13:55:26 +08:00
|
|
|
} else {
|
|
|
|
# We really need to be able to distinguish "forbidden"
|
|
|
|
# and "ignorable" codes here
|
2007-09-19 06:08:20 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-11-19 13:55:26 +08:00
|
|
|
return $prefix;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2008-05-20 09:19:42 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# This function takes a series of byte codes in a format which is more
|
|
|
|
# typical of the Intel documentation, and encode it.
|
|
|
|
#
|
|
|
|
# The format looks like:
|
|
|
|
#
|
|
|
|
# [operands: opcodes]
|
|
|
|
#
|
|
|
|
# The operands word lists the order of the operands:
|
|
|
|
#
|
|
|
|
# r = register field in the modr/m
|
|
|
|
# m = modr/m
|
2008-05-20 10:08:03 +08:00
|
|
|
# v = VEX "v" field
|
|
|
|
# d = DREX "dst" field
|
2008-05-20 09:19:42 +08:00
|
|
|
# i = immediate
|
2008-05-20 10:08:03 +08:00
|
|
|
# s = register field of is4 or imz2 field
|
|
|
|
#
|
2008-05-20 09:19:42 +08:00
|
|
|
sub byte_code_compile($) {
|
|
|
|
my($str) = @_;
|
|
|
|
my $opr;
|
2008-05-20 10:08:03 +08:00
|
|
|
my $opc;
|
2008-05-20 09:19:42 +08:00
|
|
|
my @codes = ();
|
|
|
|
my $litix = undef;
|
|
|
|
my %oppos = ();
|
|
|
|
my $i;
|
|
|
|
my $op, $oq;
|
2008-05-20 10:08:03 +08:00
|
|
|
|
2008-05-20 09:19:42 +08:00
|
|
|
if ($str =~ /^(\S*)\:\s*(.*\S)\s*$/) {
|
|
|
|
$opr = "\L$1";
|
|
|
|
$opc = "\L$2";
|
|
|
|
} else {
|
|
|
|
$opr = '';
|
|
|
|
$opc = $str;
|
|
|
|
}
|
2008-05-20 10:08:03 +08:00
|
|
|
|
2008-05-20 09:19:42 +08:00
|
|
|
for ($i = 0; $i < length($opr); $i++) {
|
|
|
|
$oppos{substr($opr,$i,1)} = $i;
|
|
|
|
}
|
|
|
|
|
2008-05-20 10:08:03 +08:00
|
|
|
$prefix_ok = 1;
|
2008-05-20 09:19:42 +08:00
|
|
|
foreach $op (split($opc)) {
|
2008-05-20 10:08:03 +08:00
|
|
|
if ($op eq 'o16') {
|
|
|
|
push(@codes, 0320);
|
|
|
|
} elsif ($op eq 'o32') {
|
|
|
|
push(@codes, 0321);
|
|
|
|
} elsif ($op eq 'o64') { # 64-bit operand size requiring REX.W
|
|
|
|
push(@codes, 0324);
|
|
|
|
} elsif ($op eq 'o64i') { # Implied 64-bit operand size (no REX.W)
|
|
|
|
push(@codes, 0323);
|
|
|
|
} elsif ($op eq 'a16') {
|
|
|
|
push(@codes, 0310);
|
|
|
|
} elsif ($op eq 'a32') {
|
|
|
|
push(@codes, 0311);
|
|
|
|
} elsif ($op eq 'a64') {
|
|
|
|
push(@codes, 0313);
|
|
|
|
} elsif ($op eq '!osp') {
|
|
|
|
push(@codes, 0364);
|
|
|
|
} elsif ($op eq '!asp') {
|
|
|
|
push(@codes, 0365);
|
|
|
|
} elsif ($op eq 'rex.l') {
|
|
|
|
push(@codes, 0334);
|
|
|
|
} elsif ($op eq 'repe') {
|
|
|
|
push(@codes, 0335);
|
|
|
|
} elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
|
|
|
|
# 66/F2/F3 prefix used as an opcode extension, or np = no prefix
|
|
|
|
if ($op eq '66') {
|
2008-05-21 00:46:24 +08:00
|
|
|
push(@codes, 0361);
|
2008-05-20 10:08:03 +08:00
|
|
|
} elsif ($op eq 'f2') {
|
2008-05-21 00:46:24 +08:00
|
|
|
push(@codes, 0362);
|
2008-05-20 10:08:03 +08:00
|
|
|
} elsif ($op eq 'f3') {
|
2008-05-21 00:46:24 +08:00
|
|
|
push(@codes, 0363);
|
2008-05-20 10:08:03 +08:00
|
|
|
} else {
|
2008-05-21 00:46:24 +08:00
|
|
|
push(@codes, 0360);
|
2008-05-20 10:08:03 +08:00
|
|
|
}
|
|
|
|
} elsif ($op =~ /^[0-9a-f]{2}$/) {
|
2008-05-20 09:19:42 +08:00
|
|
|
if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes) {
|
|
|
|
$codes[$litix]++;
|
|
|
|
push(@codes, hex $op);
|
|
|
|
} else {
|
|
|
|
$litix = scalar(@codes);
|
|
|
|
push(@codes, 01, hex $op);
|
|
|
|
}
|
2008-05-20 10:08:03 +08:00
|
|
|
$prefix_ok = 0;
|
2008-05-20 09:19:42 +08:00
|
|
|
} elsif ($op eq '/r') {
|
|
|
|
if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
|
|
|
|
die "$0: $line: $op requires r and m operands\n";
|
|
|
|
}
|
|
|
|
push(@codes, 0100 + ($oppos{'m'} << 3) + $oppos{'r'});
|
2008-05-20 10:08:03 +08:00
|
|
|
$prefix_ok = 0;
|
2008-05-20 09:19:42 +08:00
|
|
|
} elsif ($op =~ m:^/([0-7])$:) {
|
|
|
|
if (!defined($oppos{'m'})) {
|
|
|
|
die "$0: $line: $op requires m operand\n";
|
|
|
|
}
|
2008-05-20 10:08:03 +08:00
|
|
|
push(@codes, 0200 + ($oppos{'m'} << 3) + $1);
|
|
|
|
$prefix_ok = 0;
|
|
|
|
} elsif ($op =~ /^vex(|\..*)$/) {
|
2008-05-20 09:19:42 +08:00
|
|
|
my ($m,$w,$l,$p) = (undef,2,undef,0);
|
|
|
|
foreach $oq (split(/\./, $op)) {
|
|
|
|
if ($oq eq 'vex') {
|
|
|
|
# prefix
|
|
|
|
} elsif ($oq eq '128' || $oq eq 'l0') {
|
|
|
|
$l = 0;
|
|
|
|
} elsif ($oq eq '256' || $oq eq 'l1') {
|
|
|
|
$l = 1;
|
|
|
|
} elsif ($oq eq 'w0') {
|
|
|
|
$w = 0;
|
|
|
|
} elsif ($oq eq 'w1') {
|
|
|
|
$w = 1;
|
|
|
|
} elsif ($oq eq '66') {
|
|
|
|
$p = 1;
|
|
|
|
} elsif ($oq eq 'f3') {
|
|
|
|
$p = 2;
|
|
|
|
} elsif ($oq eq 'f2') {
|
|
|
|
$p = 3;
|
|
|
|
} elsif ($oq eq '0f') {
|
|
|
|
$m = 1;
|
|
|
|
} elsif ($oq eq '0f38') {
|
|
|
|
$m = 2;
|
|
|
|
} elsif ($oq eq '0f3a') {
|
|
|
|
$m = 3;
|
|
|
|
} elsif ($oq =~ /^m([0-9]+)$/) {
|
|
|
|
$m = $1+0;
|
|
|
|
} elsif ($oq eq 'nds' || $oq eq 'ndd') {
|
|
|
|
return undef if (!defined($oppos{'v'}));
|
|
|
|
} else {
|
|
|
|
die "$0: $line: undefined VEX subcode: $oq\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
|
|
|
|
die "$0: $line: missing fields in VEX specification\n";
|
|
|
|
}
|
|
|
|
push(@codes, defined($oppos{'v'}) ? 0260+$oppos{'v'} : 0270,
|
|
|
|
$m, ($w << 3)+($l << 2)+$p);
|
2008-05-20 10:08:03 +08:00
|
|
|
$prefix_ok = 0;
|
|
|
|
} elsif ($op =~ /^drex(|..*)$/) {
|
|
|
|
my ($oc0) = (0);
|
|
|
|
foreach $oq (split(/\./, $op)) {
|
|
|
|
if ($oq eq 'drex') {
|
|
|
|
#prefix
|
|
|
|
} elsif ($oq eq 'oc0') {
|
|
|
|
$oc0 = 1;
|
|
|
|
} else {
|
|
|
|
die "$0: $line: undefined DREX subcode: $oq\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!defined($oppos{'d'})) {
|
|
|
|
die "$0: $line: DREX without a 'd' operand\n";
|
|
|
|
}
|
|
|
|
push(@codes, 0160+$oppos{'d'}+($oc0 ? 4 : 0));
|
|
|
|
} elsif ($op =~ /^(imm8|imm8u|imm8s|imm16|imm32|imm32s|imm64|imm|immx|rel8|rel16|rel32|rel64|rel|seg|simm16|simm32|simm32s)$/) {
|
|
|
|
if (!defined($oppos{'i'})) {
|
|
|
|
die "$0: $op without 'i' operand\n";
|
|
|
|
}
|
|
|
|
if ($op eq 'imm8s') {
|
|
|
|
push(@codes, 014+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'imm8') {
|
|
|
|
push(@codes, 020+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'imm8u') {
|
|
|
|
push(@codes, 024+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'imm16') {
|
|
|
|
push(@codes, 030+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'imm') { # 16 or 32 bit operand
|
|
|
|
push(@codes, 034+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'imm32') {
|
|
|
|
push(@codes, 040+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'immx') { # 16, 32 or 64 bit operand
|
|
|
|
push(@codes, 044+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'rel8') {
|
|
|
|
push(@codes, 050+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'rel64') {
|
|
|
|
push(@codes, 054+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'rel16') {
|
|
|
|
push(@codes, 060+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'rel') { # 16 or 32 bit relative operand
|
|
|
|
push(@codes, 064+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'rel32') {
|
|
|
|
push(@codes, 070+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'seg') {
|
|
|
|
push(@codes, 074+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'simm16') { # imm16 that can be bytified
|
|
|
|
if (!defined($s_pos)) {
|
|
|
|
die "$0: $line: $op without a +s byte\n";
|
|
|
|
}
|
|
|
|
$codes[$s_pos] += 0144;
|
|
|
|
push(@codes, 0140+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'simm32') { # imm32 that can be bytified
|
|
|
|
if (!defined($s_pos)) {
|
|
|
|
die "$0: $line: $op without a +s byte\n";
|
|
|
|
}
|
|
|
|
$codes[$s_pos] += 0154;
|
|
|
|
push(@codes, 0150+$oppos{'i'});
|
|
|
|
} elsif ($op eq 'simm32s') {
|
|
|
|
# imm32 that can be bytified, sign extended
|
|
|
|
if (!defined($s_pos)) {
|
|
|
|
die "$0: $line: $op without a +s byte\n";
|
|
|
|
}
|
|
|
|
$codes[$s_pos] += 0154;
|
|
|
|
push(@codes, 0250+$oppos{'i'});
|
|
|
|
}
|
|
|
|
$prefix_ok = 0;
|
2008-05-20 12:07:08 +08:00
|
|
|
} elsif ($op eq 'is4' || $op eq 'imz2') {
|
|
|
|
if (!defined($oppos{'i'} || !defined($oppos{'s'}))) {
|
|
|
|
die "$0: $line: $op without 'i' and 's' operands\n";
|
|
|
|
}
|
|
|
|
push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
|
2008-05-21 00:46:24 +08:00
|
|
|
$prefix_ok = 0;
|
2008-05-21 00:36:41 +08:00
|
|
|
} elsif ($op =~ /^(is4|imz2)\=([0-9]+)$/) {
|
|
|
|
my $imm = $2;
|
2008-05-20 12:07:08 +08:00
|
|
|
if (!defined($oppos{'s'})) {
|
|
|
|
die "$0: $line: $op without 's' operand\n";
|
|
|
|
}
|
2008-05-21 00:36:41 +08:00
|
|
|
if ($imm < 0 || $imm > 15) {
|
|
|
|
die "$0: $line: invalid imm4 value for $op: $imm\n";
|
2008-05-20 12:07:08 +08:00
|
|
|
}
|
2008-05-21 00:36:41 +08:00
|
|
|
push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
|
2008-05-21 00:46:24 +08:00
|
|
|
$prefix_ok = 0;
|
2008-05-20 10:08:03 +08:00
|
|
|
} elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
|
|
|
|
if (!defined($oppos{'i'})) {
|
|
|
|
die "$0: $op without 'i' operand\n";
|
|
|
|
}
|
|
|
|
$s_pos = scalar @codes;
|
|
|
|
push(@codes, $oppos{'i'}, hex $1);
|
|
|
|
$prefix_ok = 0;
|
|
|
|
} elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
|
|
|
|
push(@codes, 0330, hex $1);
|
|
|
|
$prefix_ok = 0;
|
|
|
|
} elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
|
|
|
|
# Escape to enter literal bytecodes
|
|
|
|
push(@codes, oct $1);
|
|
|
|
} else {
|
|
|
|
die "$0: unknown operation: $op\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @codes;
|
|
|
|
}
|