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
|
|
|
|
2008-07-15 12:50:44 +00:00
|
|
|
$::lbdecor="L\$"; # local label decoration
|
2007-11-24 16:03:57 +00:00
|
|
|
$nmdecor=$::netware?"":"_"; # external name decoration
|
|
|
|
$drdecor=$::mwerks?".":""; # directive decoration
|
|
|
|
|
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]"; }
|
2007-12-18 09:18:49 +00:00
|
|
|
elsif ($opcode eq "lea" && $#_==1) # wipe storage qualifier from lea
|
2006-09-18 19:17:09 +00:00
|
|
|
{ $_[1] =~ s/^[^\[]*\[/\[/o; }
|
2011-06-28 13:33:47 +00:00
|
|
|
elsif ($opcode eq "clflush" && $#_==0)
|
|
|
|
{ $_[0] =~ s/^[^\[]*\[/\[/o; }
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
&::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...
|
|
|
|
#
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::call_ptr { &::emit("call",@_); }
|
|
|
|
sub ::jmp_ptr { &::emit("jmp",@_); }
|
|
|
|
|
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);
|
|
|
|
|
2013-05-19 19:27:59 +00:00
|
|
|
if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
|
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
if ($size ne "")
|
|
|
|
{ $ret .= "$size";
|
|
|
|
$ret .= " PTR" if ($::mwerks);
|
|
|
|
$ret .= " ";
|
|
|
|
}
|
|
|
|
$ret .= "[";
|
|
|
|
|
|
|
|
$addr =~ s/^\s+//;
|
|
|
|
# prepend global references with optional underscore
|
2007-12-18 09:18:49 +00:00
|
|
|
$addr =~ s/^([^\+\-0-9][^\+\-]*)/::islabel($1) or "$nmdecor$1"/ige;
|
2006-09-18 19:17:09 +00:00
|
|
|
# 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",@_); }
|
2011-06-28 13:33:47 +00:00
|
|
|
sub ::WP { &get_mem("WORD",@_); }
|
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
|
2007-12-18 09:18:49 +00:00
|
|
|
{ if ($::mwerks) { push(@out,".section\t.text,64\n"); }
|
2006-09-18 19:17:09 +00:00
|
|
|
else
|
|
|
|
{ my $tmp=<<___;
|
2008-07-15 12:50:44 +00:00
|
|
|
%ifidn __OUTPUT_FORMAT__,obj
|
2006-09-18 19:17:09 +00:00
|
|
|
section code use32 class=code align=64
|
2008-07-15 12:50:44 +00:00
|
|
|
%elifidn __OUTPUT_FORMAT__,win32
|
|
|
|
\$\@feat.00 equ 1
|
2006-09-18 19:17:09 +00:00
|
|
|
section .text code align=64
|
2008-07-15 12:50:44 +00:00
|
|
|
%else
|
|
|
|
section .text code
|
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
|
2007-11-22 21:21:35 +00:00
|
|
|
{ my $func=shift;
|
2007-11-24 16:03:57 +00:00
|
|
|
my $global=($func !~ /^_/);
|
2007-12-18 09:18:49 +00:00
|
|
|
my $begin="${::lbdecor}_${func}_begin";
|
2007-11-24 16:03:57 +00:00
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
$begin =~ s/^\@/./ if ($::mwerks); # the torture never stops
|
|
|
|
|
|
|
|
&::LABEL($func,$global?"$begin":"$nmdecor$func");
|
2007-11-24 16:03:57 +00:00
|
|
|
$func=$nmdecor.$func;
|
|
|
|
|
|
|
|
push(@out,"${drdecor}global $func\n") if ($global);
|
|
|
|
push(@out,"${drdecor}align 16\n");
|
|
|
|
push(@out,"$func:\n");
|
|
|
|
push(@out,"$begin:\n") if ($global);
|
2006-09-18 19:17:09 +00:00
|
|
|
$::stack=4;
|
|
|
|
}
|
2007-12-18 09:18:49 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::function_end_B
|
2007-12-18 09:18:49 +00:00
|
|
|
{ $::stack=0;
|
|
|
|
&::wipe_labels();
|
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 ::file_end
|
2007-12-18 09:18:49 +00:00
|
|
|
{ if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
|
|
|
|
{ my $comm=<<___;
|
|
|
|
${drdecor}segment .bss
|
2013-05-19 19:27:59 +00:00
|
|
|
${drdecor}common ${nmdecor}OPENSSL_ia32cap_P 16
|
2007-07-25 12:38:11 +00:00
|
|
|
___
|
2007-03-20 09:37:06 +00:00
|
|
|
# comment out OPENSSL_ia32cap_P declarations
|
2007-11-24 16:03:57 +00:00
|
|
|
grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
|
2007-12-18 09:18:49 +00:00
|
|
|
push (@out,$comm)
|
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 ::external_label
|
2008-01-03 16:21:06 +00:00
|
|
|
{ foreach(@_)
|
|
|
|
{ push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n"); }
|
|
|
|
}
|
1999-07-12 12:33:16 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::public_label
|
2007-12-18 09:18:49 +00:00
|
|
|
{ push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[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"); }
|
2011-06-28 13:33:47 +00:00
|
|
|
sub ::data_short
|
|
|
|
{ push(@out,(($::mwerks)?".word\t":"dw\t").join(',',@_)."\n"); }
|
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
|
2007-11-24 16:03:57 +00:00
|
|
|
{ push(@out,"${drdecor}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
|
2007-12-18 09:18:49 +00:00
|
|
|
{ my $f=$nmdecor.shift;
|
2006-09-18 19:17:09 +00:00
|
|
|
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
|
|
|
|
2008-07-22 08:44:31 +00:00
|
|
|
sub ::dataseg
|
|
|
|
{ if ($mwerks) { push(@out,".section\t.data,4\n"); }
|
|
|
|
else { push(@out,"section\t.data align=4\n"); }
|
|
|
|
}
|
|
|
|
|
2011-11-14 20:44:20 +00:00
|
|
|
sub ::safeseh
|
|
|
|
{ my $nm=shift;
|
|
|
|
push(@out,"%if __NASM_VERSION_ID__ >= 0x02030000\n");
|
|
|
|
push(@out,"safeseh ".&::LABEL($nm,$nmdecor.$nm)."\n");
|
|
|
|
push(@out,"%endif\n");
|
|
|
|
}
|
|
|
|
|
2004-08-29 16:10:27 +00:00
|
|
|
1;
|