2006-09-18 19:17:09 +00:00
|
|
|
#!/usr/bin/env perl
|
1999-07-12 12:33:16 +00:00
|
|
|
|
|
|
|
package x86nasm;
|
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
*out=\@::out;
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
$lprfx="\@L";
|
|
|
|
$label="000";
|
|
|
|
$under=($::netware)?'':'_';
|
2007-07-25 12:38:11 +00:00
|
|
|
$initseg="";
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::generic
|
|
|
|
{ my $opcode=shift;
|
|
|
|
my $tmp;
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
if (!$::mwerks)
|
|
|
|
{ if ($opcode =~ m/^j/o && $#_==0) # optimize jumps
|
|
|
|
{ $_[0] = "NEAR $_[0]"; }
|
|
|
|
elsif ($opcode eq "lea" && $#_==1)# wipe storage qualifier from lea
|
|
|
|
{ $_[1] =~ s/^[^\[]*\[/\[/o; }
|
|
|
|
}
|
|
|
|
&::emit($opcode,@_);
|
|
|
|
1;
|
1999-07-12 18:12:43 +00:00
|
|
|
}
|
2006-09-18 19:17:09 +00:00
|
|
|
#
|
|
|
|
# opcodes not covered by ::generic above, mostly inconsistent namings...
|
|
|
|
#
|
|
|
|
sub ::movz { &::movzx(@_); }
|
|
|
|
sub ::pushf { &::pushfd; }
|
|
|
|
sub ::popf { &::popfd; }
|
|
|
|
|
|
|
|
sub ::call { &::emit("call",(&islabel($_[0]) or "$under$_[0]")); }
|
|
|
|
sub ::call_ptr { &::emit("call",@_); }
|
|
|
|
sub ::jmp_ptr { &::emit("jmp",@_); }
|
|
|
|
|
|
|
|
# chosen SSE 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
|
|
|
|
{ &::emit("movq",@_); }
|
|
|
|
}
|
|
|
|
sub ::pshufw { &::emit("pshufw",@_); }
|
1999-07-12 12:33:16 +00:00
|
|
|
|
|
|
|
sub get_mem
|
2006-09-18 19:17:09 +00:00
|
|
|
{ my($size,$addr,$reg1,$reg2,$idx)=@_;
|
|
|
|
my($post,$ret);
|
|
|
|
|
|
|
|
if ($size ne "")
|
|
|
|
{ $ret .= "$size";
|
|
|
|
$ret .= " PTR" if ($::mwerks);
|
|
|
|
$ret .= " ";
|
|
|
|
}
|
|
|
|
$ret .= "[";
|
|
|
|
|
|
|
|
$addr =~ s/^\s+//;
|
|
|
|
# prepend global references with optional underscore
|
|
|
|
$addr =~ s/^([^\+\-0-9][^\+\-]*)/islabel($1) or "$under$1"/ige;
|
|
|
|
# put address arithmetic expression in parenthesis
|
|
|
|
$addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
|
|
|
|
|
|
|
|
if (($addr ne "") && ($addr ne 0))
|
|
|
|
{ if ($addr !~ /^-/) { $ret .= "$addr+"; }
|
|
|
|
else { $post=$addr; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reg2 ne "")
|
|
|
|
{ $idx!=0 or $idx=1;
|
|
|
|
$ret .= "$reg2*$idx";
|
|
|
|
$ret .= "+$reg1" if ($reg1 ne "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ $ret .= "$reg1"; }
|
|
|
|
|
|
|
|
$ret .= "$post]";
|
|
|
|
$ret =~ s/\+\]/]/; # in case $addr was the only argument
|
|
|
|
|
|
|
|
$ret;
|
|
|
|
}
|
|
|
|
sub ::BP { &get_mem("BYTE",@_); }
|
|
|
|
sub ::DWP { &get_mem("DWORD",@_); }
|
2007-03-20 08:55:58 +00:00
|
|
|
sub ::QWP { &get_mem("",@_); }
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::BC { (($::mwerks)?"":"BYTE ")."@_"; }
|
|
|
|
sub ::DWC { (($::mwerks)?"":"DWORD ")."@_"; }
|
|
|
|
|
|
|
|
sub ::file
|
|
|
|
{ if ($::mwerks) { push(@out,".section\t.text\n"); }
|
|
|
|
else
|
|
|
|
{ my $tmp=<<___;
|
2005-09-20 06:13:06 +00:00
|
|
|
%ifdef __omf__
|
2006-09-18 19:17:09 +00:00
|
|
|
section code use32 class=code align=64
|
2005-09-20 06:13:06 +00:00
|
|
|
%else
|
2006-09-18 19:17:09 +00:00
|
|
|
section .text code align=64
|
2005-09-20 06:13:06 +00:00
|
|
|
%endif
|
|
|
|
___
|
1999-07-12 12:33:16 +00:00
|
|
|
push(@out,$tmp);
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::function_begin_B
|
|
|
|
{ my $func=$under.shift;
|
|
|
|
my $tmp=<<___;
|
|
|
|
global $func
|
|
|
|
align 16
|
|
|
|
$func:
|
|
|
|
___
|
|
|
|
push(@out,$tmp);
|
|
|
|
$::stack=4;
|
|
|
|
}
|
|
|
|
sub ::function_end_B
|
|
|
|
{ my $i;
|
|
|
|
foreach $i (%label) { undef $label{$i} if ($label{$i} =~ /^$prfx/); }
|
|
|
|
$::stack=0;
|
|
|
|
}
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::file_end
|
|
|
|
{ # try to detect if SSE2 or MMX extensions were used on Win32...
|
2007-07-25 12:38:11 +00:00
|
|
|
if ($::win32 && grep {/\b[x]?mm[0-7]\b|OPENSSL_ia32cap_P\b/i} @out)
|
|
|
|
{ # $1<<10 sets a reserved bit to signal that variable
|
2006-09-18 19:17:09 +00:00
|
|
|
# was initialized already...
|
2007-07-25 12:38:11 +00:00
|
|
|
my $code=<<___;
|
2006-09-18 19:17:09 +00:00
|
|
|
align 16
|
|
|
|
${lprfx}OPENSSL_ia32cap_init:
|
|
|
|
lea edx,[${under}OPENSSL_ia32cap_P]
|
|
|
|
cmp DWORD [edx],0
|
|
|
|
jne NEAR ${lprfx}nocpuid
|
|
|
|
mov DWORD [edx],1<<10
|
|
|
|
pushfd
|
|
|
|
pop eax
|
|
|
|
mov ecx,eax
|
|
|
|
xor eax,1<<21
|
|
|
|
push eax
|
|
|
|
popfd
|
|
|
|
pushfd
|
|
|
|
pop eax
|
|
|
|
xor eax,ecx
|
|
|
|
bt eax,21
|
|
|
|
jnc NEAR ${lprfx}nocpuid
|
2007-07-25 12:38:11 +00:00
|
|
|
push ebp
|
2006-09-18 19:17:09 +00:00
|
|
|
push edi
|
|
|
|
push ebx
|
|
|
|
mov edi,edx
|
2007-07-25 12:38:11 +00:00
|
|
|
xor eax,eax
|
|
|
|
cpuid
|
|
|
|
xor eax,eax
|
|
|
|
cmp ebx,'Genu'
|
|
|
|
setne al
|
|
|
|
mov ebp,eax
|
|
|
|
cmp edx,'ineI'
|
|
|
|
setne al
|
|
|
|
or ebp,eax
|
|
|
|
cmp eax,'ntel'
|
|
|
|
setne al
|
|
|
|
or ebp,eax
|
2006-09-18 19:17:09 +00:00
|
|
|
mov eax,1
|
|
|
|
cpuid
|
2007-07-25 12:38:11 +00:00
|
|
|
cmp ebp,0
|
|
|
|
jne ${lprfx}notP4
|
|
|
|
and ah,15
|
|
|
|
cmp ah,15
|
|
|
|
jne ${lprfx}notP4
|
|
|
|
or edx,1<<20
|
|
|
|
${lprfx}notP4:
|
|
|
|
bt edx,28
|
|
|
|
jnc ${lprfx}done
|
|
|
|
shr ebx,16
|
|
|
|
cmp bl,1
|
|
|
|
ja ${lprfx}done
|
|
|
|
and edx,0xefffffff
|
|
|
|
${lprfx}done:
|
2006-09-18 19:17:09 +00:00
|
|
|
or edx,1<<10
|
|
|
|
mov DWORD [edi],edx
|
1999-07-12 12:33:16 +00:00
|
|
|
pop ebx
|
|
|
|
pop edi
|
2007-07-25 12:38:11 +00:00
|
|
|
pop ebp
|
2006-09-18 19:17:09 +00:00
|
|
|
${lprfx}nocpuid:
|
1999-07-12 12:33:16 +00:00
|
|
|
ret
|
2007-03-20 09:37:06 +00:00
|
|
|
segment .CRT\$XCU data align=4
|
2006-09-18 19:17:09 +00:00
|
|
|
dd ${lprfx}OPENSSL_ia32cap_init
|
2007-07-25 12:38:11 +00:00
|
|
|
___
|
|
|
|
my $data=<<___;
|
2006-09-18 19:17:09 +00:00
|
|
|
segment .bss
|
|
|
|
common ${under}OPENSSL_ia32cap_P 4
|
|
|
|
___
|
2007-07-25 12:38:11 +00:00
|
|
|
|
|
|
|
#<not needed in OpenSSL context>#push (@out,$code);
|
|
|
|
|
2007-03-20 09:37:06 +00:00
|
|
|
# comment out OPENSSL_ia32cap_P declarations
|
2007-03-20 08:55:58 +00:00
|
|
|
grep {s/(^extern\s+${under}OPENSSL_ia32cap_P)/\;$1/} @out;
|
2007-07-25 12:38:11 +00:00
|
|
|
push (@out,$data)
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
2007-07-25 12:38:11 +00:00
|
|
|
push (@out,$initseg) if ($initseg);
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
|
2004-12-23 21:43:25 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub islabel # see is argument is known label
|
|
|
|
{ my $i;
|
|
|
|
foreach $i (%label) { return $label{$i} if ($label{$i} eq $_[0]); }
|
|
|
|
undef;
|
|
|
|
}
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::external_label
|
|
|
|
{ push(@labels,@_);
|
|
|
|
foreach (@_)
|
|
|
|
{ push(@out,".") if ($::mwerks);
|
|
|
|
push(@out, "extern\t${under}$_\n");
|
|
|
|
}
|
|
|
|
}
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::public_label
|
|
|
|
{ $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
|
|
|
|
push(@out,"global\t$label{$_[0]}\n");
|
|
|
|
}
|
2005-11-28 20:09:58 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::label
|
|
|
|
{ if (!defined($label{$_[0]}))
|
|
|
|
{ $label{$_[0]}="${lprfx}${label}${_[0]}"; $label++; }
|
|
|
|
$label{$_[0]};
|
|
|
|
}
|
2004-05-06 10:31:09 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::set_label
|
|
|
|
{ my $label=&::label($_[0]);
|
|
|
|
&::align($_[1]) if ($_[1]>1);
|
|
|
|
push(@out,"$label{$_[0]}:\n");
|
|
|
|
}
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::data_byte
|
|
|
|
{ push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n"); }
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::data_word
|
|
|
|
{ push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n"); }
|
2002-12-13 17:56:14 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::align
|
|
|
|
{ push(@out,".") if ($::mwerks); push(@out,"align\t$_[0]\n"); }
|
2003-01-24 09:39:31 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::picmeup
|
|
|
|
{ my($dst,$sym)=@_;
|
|
|
|
&::lea($dst,&::DWP($sym));
|
|
|
|
}
|
2004-08-29 16:10:27 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::initseg
|
|
|
|
{ my($f)=$under.shift;
|
|
|
|
if ($::win32)
|
2007-07-25 12:38:11 +00:00
|
|
|
{ $initseg=<<___;
|
|
|
|
segment .CRT\$XCU data align=4
|
2006-09-18 19:17:09 +00:00
|
|
|
extern $f
|
|
|
|
dd $f
|
2004-08-29 16:10:27 +00:00
|
|
|
___
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-29 16:10:27 +00:00
|
|
|
|
|
|
|
1;
|