2006-09-18 19:17:09 +00:00
|
|
|
#!/usr/bin/env perl
|
1998-12-21 10:56:39 +00:00
|
|
|
|
|
|
|
# require 'x86asm.pl';
|
2006-09-18 19:17:09 +00:00
|
|
|
# &asm_init(<flavor>,"des-586.pl"[,$i386only]);
|
|
|
|
# &function_begin("foo");
|
|
|
|
# ...
|
|
|
|
# &function_end("foo");
|
|
|
|
# &asm_finish
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
$out=();
|
|
|
|
$i386=0;
|
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
# AUTOLOAD is this context has quite unpleasant side effect, namely
|
|
|
|
# that typos in function calls effectively go to assembler output,
|
|
|
|
# but on the pros side we don't have to implement one subroutine per
|
|
|
|
# each opcode...
|
|
|
|
sub ::AUTOLOAD
|
|
|
|
{ my $opcode = $AUTOLOAD;
|
|
|
|
|
2008-12-17 19:56:48 +00:00
|
|
|
die "more than 4 arguments passed to $opcode" if ($#_>3);
|
2006-09-18 19:17:09 +00:00
|
|
|
|
|
|
|
$opcode =~ s/.*:://;
|
|
|
|
if ($opcode =~ /^push/) { $stack+=4; }
|
|
|
|
elsif ($opcode =~ /^pop/) { $stack-=4; }
|
|
|
|
|
|
|
|
&generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::emit
|
|
|
|
{ my $opcode=shift;
|
|
|
|
|
|
|
|
if ($#_==-1) { push(@out,"\t$opcode\n"); }
|
|
|
|
else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::LB
|
|
|
|
{ $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
|
|
|
|
$1."l";
|
|
|
|
}
|
|
|
|
sub ::HB
|
|
|
|
{ $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
|
|
|
|
$1."h";
|
|
|
|
}
|
|
|
|
sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); }
|
|
|
|
sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); }
|
|
|
|
sub ::blindpop { &pop($_[0]); $stack+=4; }
|
|
|
|
sub ::wparam { &DWP($stack+4*$_[0],"esp"); }
|
|
|
|
sub ::swtmp { &DWP(4*$_[0],"esp"); }
|
|
|
|
|
|
|
|
sub ::bswap
|
|
|
|
{ if ($i386) # emulate bswap for i386
|
|
|
|
{ &comment("bswap @_");
|
|
|
|
&xchg(&HB(@_),&LB(@_));
|
|
|
|
&ror (@_,16);
|
|
|
|
&xchg(&HB(@_),&LB(@_));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ &generic("bswap",@_); }
|
|
|
|
}
|
|
|
|
# These are made-up opcodes introduced over the years essentially
|
|
|
|
# by ignorance, just alias them to real ones...
|
|
|
|
sub ::movb { &mov(@_); }
|
|
|
|
sub ::xorb { &xor(@_); }
|
|
|
|
sub ::rotl { &rol(@_); }
|
|
|
|
sub ::rotr { &ror(@_); }
|
|
|
|
sub ::exch { &xchg(@_); }
|
|
|
|
sub ::halt { &hlt; }
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::movz { &movzx(@_); }
|
2008-12-17 19:56:48 +00:00
|
|
|
sub ::pushf { &pushfd; }
|
|
|
|
sub ::popf { &popfd; }
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
# 3 argument instructions
|
|
|
|
sub ::movq
|
|
|
|
{ my($p1,$p2,$optimize)=@_;
|
|
|
|
|
|
|
|
if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
|
|
|
|
# movq between mmx registers can sink Intel CPUs
|
|
|
|
{ &::pshufw($p1,$p2,0xe4); }
|
|
|
|
else
|
|
|
|
{ &::generic("movq",@_); }
|
|
|
|
}
|
|
|
|
|
2011-06-28 13:33:47 +00:00
|
|
|
# SSE>2 instructions
|
|
|
|
my %regrm = ( "eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3,
|
|
|
|
"esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7 );
|
|
|
|
sub ::pextrd
|
|
|
|
{ my($dst,$src,$imm)=@_;
|
|
|
|
if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/)
|
|
|
|
{ &::data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2<<3)|$regrm{$1},$imm); }
|
|
|
|
else
|
|
|
|
{ &::generic("pextrd",@_); }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::pinsrd
|
|
|
|
{ my($dst,$src,$imm)=@_;
|
|
|
|
if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
|
|
|
|
{ &::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm); }
|
|
|
|
else
|
|
|
|
{ &::generic("pinsrd",@_); }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::pshufb
|
|
|
|
{ my($dst,$src)=@_;
|
|
|
|
if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
|
|
|
|
{ &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2); }
|
|
|
|
else
|
|
|
|
{ &::generic("pshufb",@_); }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::palignr
|
|
|
|
{ my($dst,$src,$imm)=@_;
|
|
|
|
if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
|
|
|
|
{ &::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1<<3)|$2,$imm); }
|
|
|
|
else
|
|
|
|
{ &::generic("palignr",@_); }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::pclmulqdq
|
|
|
|
{ my($dst,$src,$imm)=@_;
|
|
|
|
if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
|
|
|
|
{ &::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm); }
|
|
|
|
else
|
|
|
|
{ &::generic("pclmulqdq",@_); }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::rdrand
|
|
|
|
{ my ($dst)=@_;
|
|
|
|
if ($dst =~ /(e[a-dsd][ixp])/)
|
|
|
|
{ &::data_byte(0x0f,0xc7,0xf0|$regrm{$dst}); }
|
|
|
|
else
|
|
|
|
{ &::generic("rdrand",@_); }
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
# label management
|
|
|
|
$lbdecor="L"; # local label decoration, set by package
|
|
|
|
$label="000";
|
|
|
|
|
|
|
|
sub ::islabel # see is argument is a known label
|
|
|
|
{ my $i;
|
|
|
|
foreach $i (values %label) { return $i if ($i eq $_[0]); }
|
|
|
|
$label{$_[0]}; # can be undef
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::label # instantiate a function-scope label
|
|
|
|
{ if (!defined($label{$_[0]}))
|
|
|
|
{ $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
|
|
|
|
$label{$_[0]};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::LABEL # instantiate a file-scope label
|
|
|
|
{ $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
|
|
|
|
$label{$_[0]};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
|
|
|
|
|
|
|
|
sub ::set_label_B { push(@out,"@_:\n"); }
|
|
|
|
sub ::set_label
|
|
|
|
{ my $label=&::label($_[0]);
|
|
|
|
&::align($_[1]) if ($_[1]>1);
|
|
|
|
&::set_label_B($label);
|
|
|
|
$label;
|
|
|
|
}
|
2006-09-18 19:17:09 +00:00
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::wipe_labels # wipes function-scope labels
|
|
|
|
{ foreach $i (keys %label)
|
|
|
|
{ delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
|
|
|
|
}
|
|
|
|
|
|
|
|
# subroutine management
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::function_begin
|
|
|
|
{ &function_begin_B(@_);
|
|
|
|
$stack=4;
|
|
|
|
&push("ebp");
|
|
|
|
&push("ebx");
|
|
|
|
&push("esi");
|
|
|
|
&push("edi");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::function_end
|
|
|
|
{ &pop("edi");
|
|
|
|
&pop("esi");
|
|
|
|
&pop("ebx");
|
|
|
|
&pop("ebp");
|
|
|
|
&ret();
|
|
|
|
&function_end_B(@_);
|
2007-12-18 09:18:49 +00:00
|
|
|
$stack=0;
|
|
|
|
&wipe_labels();
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ::function_end_A
|
|
|
|
{ &pop("edi");
|
|
|
|
&pop("esi");
|
|
|
|
&pop("ebx");
|
|
|
|
&pop("ebp");
|
|
|
|
&ret();
|
|
|
|
$stack+=16; # readjust esp as if we didn't pop anything
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::asciz
|
|
|
|
{ my @str=unpack("C*",shift);
|
|
|
|
push @str,0;
|
|
|
|
while ($#str>15) {
|
|
|
|
&data_byte(@str[0..15]);
|
|
|
|
foreach (0..15) { shift @str; }
|
|
|
|
}
|
|
|
|
&data_byte(@str) if (@str);
|
|
|
|
}
|
2006-10-17 06:43:11 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::asm_finish
|
|
|
|
{ &file_end();
|
|
|
|
print @out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::asm_init
|
|
|
|
{ my ($type,$fn,$cpu)=@_;
|
|
|
|
|
|
|
|
$filename=$fn;
|
|
|
|
$i386=$cpu;
|
|
|
|
|
2011-11-14 20:44:20 +00:00
|
|
|
$elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$android=0;
|
2006-09-18 19:17:09 +00:00
|
|
|
if (($type eq "elf"))
|
2007-12-18 09:18:49 +00:00
|
|
|
{ $elf=1; require "x86gas.pl"; }
|
2006-09-18 19:17:09 +00:00
|
|
|
elsif (($type eq "a\.out"))
|
2007-12-18 09:18:49 +00:00
|
|
|
{ $aout=1; require "x86gas.pl"; }
|
2006-09-18 19:17:09 +00:00
|
|
|
elsif (($type eq "coff" or $type eq "gaswin"))
|
2007-12-18 09:18:49 +00:00
|
|
|
{ $coff=1; require "x86gas.pl"; }
|
2006-09-18 19:17:09 +00:00
|
|
|
elsif (($type eq "win32n"))
|
|
|
|
{ $win32=1; require "x86nasm.pl"; }
|
|
|
|
elsif (($type eq "nw-nasm"))
|
|
|
|
{ $netware=1; require "x86nasm.pl"; }
|
2007-12-18 09:32:20 +00:00
|
|
|
#elsif (($type eq "nw-mwasm"))
|
|
|
|
#{ $netware=1; $mwerks=1; require "x86nasm.pl"; }
|
2007-12-18 09:18:49 +00:00
|
|
|
elsif (($type eq "win32"))
|
|
|
|
{ $win32=1; require "x86masm.pl"; }
|
2007-12-18 17:28:22 +00:00
|
|
|
elsif (($type eq "macosx"))
|
|
|
|
{ $aout=1; $macosx=1; require "x86gas.pl"; }
|
2011-11-14 20:44:20 +00:00
|
|
|
elsif (($type eq "android"))
|
|
|
|
{ $elf=1; $android=1; require "x86gas.pl"; }
|
2006-09-18 19:17:09 +00:00
|
|
|
else
|
|
|
|
{ print STDERR <<"EOF";
|
1998-12-21 10:56:39 +00:00
|
|
|
Pick one target type from
|
2004-08-01 17:03:50 +00:00
|
|
|
elf - Linux, FreeBSD, Solaris x86, etc.
|
2006-09-18 19:17:09 +00:00
|
|
|
a.out - DJGPP, elder OpenBSD, etc.
|
2004-08-01 17:03:50 +00:00
|
|
|
coff - GAS/COFF such as Win32 targets
|
1999-07-12 12:33:16 +00:00
|
|
|
win32n - Windows 95/Windows NT NASM format
|
2003-11-28 13:10:58 +00:00
|
|
|
nw-nasm - NetWare NASM format
|
2007-12-18 17:28:22 +00:00
|
|
|
macosx - Mac OS X
|
1998-12-21 10:56:39 +00:00
|
|
|
EOF
|
2006-09-18 19:17:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pic=0;
|
|
|
|
for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
|
|
|
|
|
|
|
|
$filename =~ s/\.pl$//;
|
|
|
|
&file($filename);
|
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2012-08-29 14:24:18 +00:00
|
|
|
sub ::hidden {}
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
1;
|