1998-12-21 10:52:47 +00:00
|
|
|
#!/usr/local/bin/perl
|
|
|
|
|
2004-08-01 17:03:50 +00:00
|
|
|
package x86unix; # GAS actually...
|
1998-12-21 10:52:47 +00:00
|
|
|
|
|
|
|
$label="L000";
|
2000-12-05 03:51:46 +00:00
|
|
|
$const="";
|
2000-12-05 05:39:43 +00:00
|
|
|
$constl=0;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
|
|
|
$align=($main'aout)?"4":"16";
|
2004-08-01 17:03:50 +00:00
|
|
|
$under=($main'aout or $main'coff)?"_":"";
|
2004-08-01 21:16:26 +00:00
|
|
|
$dot=($main'aout)?"":".";
|
2004-08-01 17:03:50 +00:00
|
|
|
$com_start="#" if ($main'aout or $main'coff);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'asm_init_output { @out=(); }
|
|
|
|
sub main'asm_get_output { return(@out); }
|
|
|
|
sub main'get_labels { return(@labels); }
|
|
|
|
sub main'external_label { push(@labels,@_); }
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
if ($main'cpp)
|
|
|
|
{
|
|
|
|
$align="ALIGN";
|
|
|
|
$under="";
|
|
|
|
$com_start='/*';
|
|
|
|
$com_end='*/';
|
|
|
|
}
|
|
|
|
|
|
|
|
%lb=( 'eax', '%al',
|
|
|
|
'ebx', '%bl',
|
|
|
|
'ecx', '%cl',
|
|
|
|
'edx', '%dl',
|
|
|
|
'ax', '%al',
|
|
|
|
'bx', '%bl',
|
|
|
|
'cx', '%cl',
|
|
|
|
'dx', '%dl',
|
|
|
|
);
|
|
|
|
|
|
|
|
%hb=( 'eax', '%ah',
|
|
|
|
'ebx', '%bh',
|
|
|
|
'ecx', '%ch',
|
|
|
|
'edx', '%dh',
|
|
|
|
'ax', '%ah',
|
|
|
|
'bx', '%bh',
|
|
|
|
'cx', '%ch',
|
|
|
|
'dx', '%dh',
|
|
|
|
);
|
|
|
|
|
|
|
|
%regs=( 'eax', '%eax',
|
|
|
|
'ebx', '%ebx',
|
|
|
|
'ecx', '%ecx',
|
|
|
|
'edx', '%edx',
|
|
|
|
'esi', '%esi',
|
|
|
|
'edi', '%edi',
|
|
|
|
'ebp', '%ebp',
|
|
|
|
'esp', '%esp',
|
2004-05-06 10:31:09 +00:00
|
|
|
|
|
|
|
'mm0', '%mm0',
|
|
|
|
'mm1', '%mm1',
|
|
|
|
'mm2', '%mm2',
|
|
|
|
'mm3', '%mm3',
|
|
|
|
'mm4', '%mm4',
|
|
|
|
'mm5', '%mm5',
|
|
|
|
'mm6', '%mm6',
|
|
|
|
'mm7', '%mm7',
|
|
|
|
|
|
|
|
'xmm0', '%xmm0',
|
|
|
|
'xmm1', '%xmm1',
|
|
|
|
'xmm2', '%xmm2',
|
|
|
|
'xmm3', '%xmm3',
|
|
|
|
'xmm4', '%xmm4',
|
|
|
|
'xmm5', '%xmm5',
|
|
|
|
'xmm6', '%xmm6',
|
|
|
|
'xmm7', '%xmm7',
|
1998-12-21 10:52:47 +00:00
|
|
|
);
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
%reg_val=(
|
|
|
|
'eax', 0x00,
|
|
|
|
'ebx', 0x03,
|
|
|
|
'ecx', 0x01,
|
|
|
|
'edx', 0x02,
|
|
|
|
'esi', 0x06,
|
|
|
|
'edi', 0x07,
|
|
|
|
'ebp', 0x05,
|
|
|
|
'esp', 0x04,
|
|
|
|
);
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'LB
|
|
|
|
{
|
|
|
|
(defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
|
|
|
|
return($lb{$_[0]});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'HB
|
|
|
|
{
|
|
|
|
(defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
|
|
|
|
return($hb{$_[0]});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'DWP
|
|
|
|
{
|
|
|
|
local($addr,$reg1,$reg2,$idx)=@_;
|
|
|
|
|
|
|
|
$ret="";
|
2001-06-04 16:34:31 +00:00
|
|
|
$addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
|
1998-12-21 10:52:47 +00:00
|
|
|
$reg1="$regs{$reg1}" if defined($regs{$reg1});
|
|
|
|
$reg2="$regs{$reg2}" if defined($regs{$reg2});
|
|
|
|
$ret.=$addr if ($addr ne "") && ($addr ne 0);
|
|
|
|
if ($reg2 ne "")
|
1999-03-07 15:08:04 +00:00
|
|
|
{
|
2002-12-13 17:56:14 +00:00
|
|
|
if($idx ne "" && $idx != 0)
|
1999-03-07 15:08:04 +00:00
|
|
|
{ $ret.="($reg1,$reg2,$idx)"; }
|
|
|
|
else
|
|
|
|
{ $ret.="($reg1,$reg2)"; }
|
|
|
|
}
|
2002-12-13 17:56:14 +00:00
|
|
|
elsif ($reg1 ne "")
|
1998-12-21 10:56:39 +00:00
|
|
|
{ $ret.="($reg1)" }
|
1998-12-21 10:52:47 +00:00
|
|
|
return($ret);
|
|
|
|
}
|
|
|
|
|
2004-05-06 10:31:09 +00:00
|
|
|
sub main'QWP
|
|
|
|
{
|
|
|
|
return(&main'DWP(@_));
|
|
|
|
}
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'BP
|
|
|
|
{
|
1998-12-21 10:56:39 +00:00
|
|
|
return(&main'DWP(@_));
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
1999-07-12 18:12:43 +00:00
|
|
|
sub main'BC
|
|
|
|
{
|
|
|
|
return @_;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'DWC
|
|
|
|
{
|
|
|
|
return @_;
|
|
|
|
}
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
#sub main'BP
|
|
|
|
# {
|
|
|
|
# local($addr,$reg1,$reg2,$idx)=@_;
|
|
|
|
#
|
|
|
|
# $ret="";
|
|
|
|
#
|
|
|
|
# $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
|
|
|
|
# $reg1="$regs{$reg1}" if defined($regs{$reg1});
|
|
|
|
# $reg2="$regs{$reg2}" if defined($regs{$reg2});
|
|
|
|
# $ret.=$addr if ($addr ne "") && ($addr ne 0);
|
|
|
|
# if ($reg2 ne "")
|
|
|
|
# { $ret.="($reg1,$reg2,$idx)"; }
|
|
|
|
# else
|
|
|
|
# { $ret.="($reg1)" }
|
|
|
|
# return($ret);
|
|
|
|
# }
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'mov { &out2("movl",@_); }
|
|
|
|
sub main'movb { &out2("movb",@_); }
|
|
|
|
sub main'and { &out2("andl",@_); }
|
|
|
|
sub main'or { &out2("orl",@_); }
|
|
|
|
sub main'shl { &out2("sall",@_); }
|
|
|
|
sub main'shr { &out2("shrl",@_); }
|
|
|
|
sub main'xor { &out2("xorl",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'xorb { &out2("xorb",@_); }
|
2004-11-21 10:36:25 +00:00
|
|
|
sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'adc { &out2("adcl",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'sub { &out2("subl",@_); }
|
2004-09-09 14:50:32 +00:00
|
|
|
sub main'sbb { &out2("sbbl",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'rotl { &out2("roll",@_); }
|
|
|
|
sub main'rotr { &out2("rorl",@_); }
|
2004-12-10 11:24:42 +00:00
|
|
|
sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'cmp { &out2("cmpl",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'lea { &out2("leal",@_); }
|
|
|
|
sub main'mul { &out1("mull",@_); }
|
|
|
|
sub main'div { &out1("divl",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'jmp { &out1("jmp",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'jmp_ptr { &out1p("jmp",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'je { &out1("je",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'jle { &out1("jle",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'jne { &out1("jne",@_); }
|
|
|
|
sub main'jnz { &out1("jnz",@_); }
|
|
|
|
sub main'jz { &out1("jz",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'jge { &out1("jge",@_); }
|
|
|
|
sub main'jl { &out1("jl",@_); }
|
2003-09-27 22:14:39 +00:00
|
|
|
sub main'ja { &out1("ja",@_); }
|
|
|
|
sub main'jae { &out1("jae",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'jb { &out1("jb",@_); }
|
2003-09-27 22:14:39 +00:00
|
|
|
sub main'jbe { &out1("jbe",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'jc { &out1("jc",@_); }
|
|
|
|
sub main'jnc { &out1("jnc",@_); }
|
|
|
|
sub main'jno { &out1("jno",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'dec { &out1("decl",@_); }
|
2004-11-21 10:36:25 +00:00
|
|
|
sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'push { &out1("pushl",@_); $stack+=4; }
|
|
|
|
sub main'pop { &out1("popl",@_); $stack-=4; }
|
2004-09-09 14:50:32 +00:00
|
|
|
sub main'pushf { &out0("pushfl"); $stack+=4; }
|
|
|
|
sub main'popf { &out0("popfl"); $stack-=4; }
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'not { &out1("notl",@_); }
|
2004-08-02 22:02:17 +00:00
|
|
|
sub main'call { my $pre=$under;
|
|
|
|
foreach $i (%label)
|
|
|
|
{ if ($label{$i} eq $_[0]) { $pre=''; last; } }
|
|
|
|
&out1("call",$pre.$_[0]);
|
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'ret { &out0("ret"); }
|
|
|
|
sub main'nop { &out0("nop"); }
|
2004-05-06 10:31:09 +00:00
|
|
|
sub main'test { &out2("testl",@_); }
|
|
|
|
sub main'bt { &out2("btl",@_); }
|
|
|
|
sub main'leave { &out0("leave"); }
|
2004-11-21 10:36:25 +00:00
|
|
|
sub main'cpuid { &out0(".byte\t0x0f,0xa2"); }
|
|
|
|
sub main'rdtsc { &out0(".byte\t0x0f,0x31"); }
|
2004-09-09 14:50:32 +00:00
|
|
|
sub main'halt { &out0("hlt"); }
|
2004-12-10 11:24:42 +00:00
|
|
|
sub main'movz { &out2("movzbl",@_); }
|
2004-05-06 10:31:09 +00:00
|
|
|
|
|
|
|
# SSE2
|
|
|
|
sub main'emms { &out0("emms"); }
|
|
|
|
sub main'movd { &out2("movd",@_); }
|
|
|
|
sub main'movq { &out2("movq",@_); }
|
|
|
|
sub main'movdqu { &out2("movdqu",@_); }
|
|
|
|
sub main'movdqa { &out2("movdqa",@_); }
|
|
|
|
sub main'movdq2q{ &out2("movdq2q",@_); }
|
|
|
|
sub main'movq2dq{ &out2("movq2dq",@_); }
|
|
|
|
sub main'paddq { &out2("paddq",@_); }
|
|
|
|
sub main'pmuludq{ &out2("pmuludq",@_); }
|
|
|
|
sub main'psrlq { &out2("psrlq",@_); }
|
|
|
|
sub main'psllq { &out2("psllq",@_); }
|
|
|
|
sub main'pxor { &out2("pxor",@_); }
|
|
|
|
sub main'por { &out2("por",@_); }
|
|
|
|
sub main'pand { &out2("pand",@_); }
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-03-30 12:49:36 +00:00
|
|
|
# The bswapl instruction is new for the 486. Emulate if i386.
|
|
|
|
sub main'bswap
|
|
|
|
{
|
|
|
|
if ($main'i386)
|
|
|
|
{
|
|
|
|
&main'comment("bswapl @_");
|
|
|
|
&main'exch(main'HB(@_),main'LB(@_));
|
|
|
|
&main'rotr(@_,16);
|
|
|
|
&main'exch(main'HB(@_),main'LB(@_));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&out1("bswapl",@_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
sub out2
|
|
|
|
{
|
|
|
|
local($name,$p1,$p2)=@_;
|
|
|
|
local($l,$ll,$t);
|
1998-12-21 10:56:39 +00:00
|
|
|
local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
|
|
|
|
"rcll",0xD1D0,"rcrl",0xD1D8,
|
|
|
|
"shll",0xD1E0,"shrl",0xD1E8,
|
|
|
|
"sarl",0xD1F8);
|
|
|
|
|
|
|
|
if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
|
|
|
|
{
|
|
|
|
$op=$special{$name}|$reg_val{$p1};
|
|
|
|
$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
|
|
|
|
$tmp2=sprintf(".byte %d\t",$op &0xff);
|
|
|
|
push(@out,$tmp1);
|
|
|
|
push(@out,$tmp2);
|
|
|
|
|
|
|
|
$p2=&conv($p2);
|
|
|
|
$p1=&conv($p1);
|
|
|
|
&main'comment("$name $p2 $p1");
|
|
|
|
return;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,"\t$name\t");
|
1998-12-21 10:52:47 +00:00
|
|
|
$t=&conv($p2).",";
|
|
|
|
$l=length($t);
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,$t);
|
1998-12-21 10:52:47 +00:00
|
|
|
$ll=4-($l+9)/8;
|
1998-12-21 10:56:39 +00:00
|
|
|
$tmp1=sprintf("\t" x $ll);
|
|
|
|
push(@out,$tmp1);
|
|
|
|
push(@out,&conv($p1)."\n");
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub out1
|
|
|
|
{
|
|
|
|
local($name,$p1)=@_;
|
|
|
|
local($l,$t);
|
1998-12-21 10:56:39 +00:00
|
|
|
local(%special)=("bswapl",0x0FC8);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
if ((defined($special{$name})) && defined($regs{$p1}))
|
|
|
|
{
|
|
|
|
$op=$special{$name}|$reg_val{$p1};
|
|
|
|
$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
|
|
|
|
$tmp2=sprintf(".byte %d\t",$op &0xff);
|
|
|
|
push(@out,$tmp1);
|
|
|
|
push(@out,$tmp2);
|
|
|
|
|
|
|
|
$p2=&conv($p2);
|
|
|
|
$p1=&conv($p1);
|
|
|
|
&main'comment("$name $p2 $p1");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@out,"\t$name\t".&conv($p1)."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub out1p
|
|
|
|
{
|
|
|
|
local($name,$p1)=@_;
|
|
|
|
local($l,$t);
|
|
|
|
|
|
|
|
push(@out,"\t$name\t*".&conv($p1)."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub out0
|
|
|
|
{
|
|
|
|
push(@out,"\t$_[0]\n");
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub conv
|
|
|
|
{
|
|
|
|
local($p)=@_;
|
|
|
|
|
|
|
|
# $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
|
|
|
|
|
|
|
|
$p=$regs{$p} if (defined($regs{$p}));
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
$p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
|
1998-12-21 10:52:47 +00:00
|
|
|
$p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
|
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'file
|
|
|
|
{
|
|
|
|
local($file)=@_;
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
local($tmp)=<<"EOF";
|
1998-12-21 10:52:47 +00:00
|
|
|
.file "$file.s"
|
|
|
|
EOF
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,$tmp);
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub main'function_begin
|
|
|
|
{
|
1998-12-21 10:56:39 +00:00
|
|
|
local($func)=@_;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
&main'external_label($func);
|
1998-12-21 10:52:47 +00:00
|
|
|
$func=$under.$func;
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
local($tmp)=<<"EOF";
|
1998-12-21 10:52:47 +00:00
|
|
|
.text
|
2004-08-02 22:02:17 +00:00
|
|
|
.globl $func
|
1998-12-21 10:52:47 +00:00
|
|
|
EOF
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,$tmp);
|
1998-12-21 10:52:47 +00:00
|
|
|
if ($main'cpp)
|
2004-08-01 17:03:50 +00:00
|
|
|
{ $tmp=push(@out,"TYPE($func,\@function)\n"); }
|
|
|
|
elsif ($main'coff)
|
|
|
|
{ $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
|
2004-08-29 21:36:37 +00:00
|
|
|
elsif ($main'aout and !$main'pic)
|
2004-08-01 17:03:50 +00:00
|
|
|
{ }
|
|
|
|
else { $tmp=push(@out,".type\t$func,\@function\n"); }
|
2004-08-02 22:02:17 +00:00
|
|
|
push(@out,".align\t$align\n");
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,"$func:\n");
|
|
|
|
$tmp=<<"EOF";
|
1998-12-21 10:52:47 +00:00
|
|
|
pushl %ebp
|
|
|
|
pushl %ebx
|
|
|
|
pushl %esi
|
|
|
|
pushl %edi
|
|
|
|
|
|
|
|
EOF
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,$tmp);
|
1998-12-21 10:52:47 +00:00
|
|
|
$stack=20;
|
|
|
|
}
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'function_begin_B
|
|
|
|
{
|
|
|
|
local($func,$extra)=@_;
|
|
|
|
|
|
|
|
&main'external_label($func);
|
|
|
|
$func=$under.$func;
|
|
|
|
|
|
|
|
local($tmp)=<<"EOF";
|
|
|
|
.text
|
2004-08-02 22:02:17 +00:00
|
|
|
.globl $func
|
1998-12-21 10:56:39 +00:00
|
|
|
EOF
|
|
|
|
push(@out,$tmp);
|
|
|
|
if ($main'cpp)
|
2004-08-01 17:03:50 +00:00
|
|
|
{ push(@out,"TYPE($func,\@function)\n"); }
|
|
|
|
elsif ($main'coff)
|
|
|
|
{ $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
|
2004-08-29 21:36:37 +00:00
|
|
|
elsif ($main'aout and !$main'pic)
|
2004-08-01 17:03:50 +00:00
|
|
|
{ }
|
|
|
|
else { push(@out,".type $func,\@function\n"); }
|
2004-08-02 22:02:17 +00:00
|
|
|
push(@out,".align\t$align\n");
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,"$func:\n");
|
|
|
|
$stack=4;
|
|
|
|
}
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'function_end
|
|
|
|
{
|
|
|
|
local($func)=@_;
|
|
|
|
|
|
|
|
$func=$under.$func;
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
local($tmp)=<<"EOF";
|
1998-12-21 10:52:47 +00:00
|
|
|
popl %edi
|
|
|
|
popl %esi
|
|
|
|
popl %ebx
|
|
|
|
popl %ebp
|
|
|
|
ret
|
2004-08-01 21:16:26 +00:00
|
|
|
${dot}L_${func}_end:
|
1998-12-21 10:52:47 +00:00
|
|
|
EOF
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,$tmp);
|
2000-12-05 03:51:46 +00:00
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
if ($main'cpp)
|
2004-08-01 21:16:26 +00:00
|
|
|
{ push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
|
2004-08-01 17:03:50 +00:00
|
|
|
elsif ($main'coff or $main'aout)
|
2004-08-02 22:02:17 +00:00
|
|
|
{ }
|
2004-08-01 21:16:26 +00:00
|
|
|
else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,".ident \"$func\"\n");
|
1998-12-21 10:52:47 +00:00
|
|
|
$stack=0;
|
|
|
|
%label=();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'function_end_A
|
|
|
|
{
|
|
|
|
local($func)=@_;
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
local($tmp)=<<"EOF";
|
1998-12-21 10:52:47 +00:00
|
|
|
popl %edi
|
|
|
|
popl %esi
|
|
|
|
popl %ebx
|
|
|
|
popl %ebp
|
|
|
|
ret
|
|
|
|
EOF
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,$tmp);
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub main'function_end_B
|
|
|
|
{
|
|
|
|
local($func)=@_;
|
|
|
|
|
|
|
|
$func=$under.$func;
|
|
|
|
|
2004-08-01 21:16:26 +00:00
|
|
|
push(@out,"${dot}L_${func}_end:\n");
|
1998-12-21 10:52:47 +00:00
|
|
|
if ($main'cpp)
|
2004-08-01 21:16:26 +00:00
|
|
|
{ push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
|
2004-08-01 17:03:50 +00:00
|
|
|
elsif ($main'coff or $main'aout)
|
2004-08-02 22:02:17 +00:00
|
|
|
{ }
|
2004-08-01 21:16:26 +00:00
|
|
|
else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
|
2004-05-06 10:31:09 +00:00
|
|
|
push(@out,".ident \"$func\"\n");
|
1998-12-21 10:52:47 +00:00
|
|
|
$stack=0;
|
|
|
|
%label=();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'wparam
|
|
|
|
{
|
|
|
|
local($num)=@_;
|
|
|
|
|
|
|
|
return(&main'DWP($stack+$num*4,"esp","",0));
|
|
|
|
}
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'stack_push
|
1998-12-21 10:52:47 +00:00
|
|
|
{
|
1998-12-21 10:56:39 +00:00
|
|
|
local($num)=@_;
|
|
|
|
$stack+=$num*4;
|
|
|
|
&main'sub("esp",$num*4);
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'stack_pop
|
1998-12-21 10:52:47 +00:00
|
|
|
{
|
|
|
|
local($num)=@_;
|
1998-12-21 10:56:39 +00:00
|
|
|
$stack-=$num*4;
|
|
|
|
&main'add("esp",$num*4);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
sub main'swtmp
|
|
|
|
{
|
|
|
|
return(&main'DWP($_[0]*4,"esp","",0));
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
# Should use swtmp, which is above esp. Linix can trash the stack above esp
|
|
|
|
#sub main'wtmp
|
|
|
|
# {
|
|
|
|
# local($num)=@_;
|
|
|
|
#
|
|
|
|
# return(&main'DWP(-($num+1)*4,"esp","",0));
|
|
|
|
# }
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'comment
|
|
|
|
{
|
2004-08-01 21:16:26 +00:00
|
|
|
if (!defined($com_start) or $main'elf)
|
|
|
|
{ # Regarding $main'elf above...
|
|
|
|
# GNU and SVR4 as'es use different comment delimiters,
|
|
|
|
push(@out,"\n"); # so we just skip ELF comments...
|
2003-01-03 17:37:53 +00:00
|
|
|
return;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
foreach (@_)
|
|
|
|
{
|
|
|
|
if (/^\s*$/)
|
1998-12-21 10:56:39 +00:00
|
|
|
{ push(@out,"\n"); }
|
1998-12-21 10:52:47 +00:00
|
|
|
else
|
1998-12-21 10:56:39 +00:00
|
|
|
{ push(@out,"\t$com_start $_ $com_end\n"); }
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-23 21:43:25 +00:00
|
|
|
sub main'public_label
|
|
|
|
{
|
|
|
|
$label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
|
|
|
|
push(@out,".globl\t$label{$_[0]}\n");
|
|
|
|
}
|
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
sub main'label
|
|
|
|
{
|
|
|
|
if (!defined($label{$_[0]}))
|
|
|
|
{
|
2004-08-01 21:16:26 +00:00
|
|
|
$label{$_[0]}="${dot}${label}${_[0]}";
|
1998-12-21 10:52:47 +00:00
|
|
|
$label++;
|
|
|
|
}
|
|
|
|
return($label{$_[0]});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'set_label
|
|
|
|
{
|
|
|
|
if (!defined($label{$_[0]}))
|
|
|
|
{
|
2004-08-01 21:16:26 +00:00
|
|
|
$label{$_[0]}="${dot}${label}${_[0]}";
|
1998-12-21 10:52:47 +00:00
|
|
|
$label++;
|
|
|
|
}
|
2004-08-02 22:02:17 +00:00
|
|
|
if ($_[1]!=0)
|
|
|
|
{
|
|
|
|
if ($_[1]>1) { main'align($_[1]); }
|
|
|
|
else { push(@out,".align $align\n"); }
|
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
push(@out,"$label{$_[0]}:\n");
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub main'file_end
|
|
|
|
{
|
2004-05-06 10:31:09 +00:00
|
|
|
# try to detect if SSE2 or MMX extensions were used on ELF platform...
|
|
|
|
if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
|
|
|
|
local($tmp);
|
|
|
|
|
2004-08-29 16:10:27 +00:00
|
|
|
push (@out,"\n.comm\t${under}OPENSSL_ia32cap_P,4,4\n");
|
2004-05-06 10:31:09 +00:00
|
|
|
|
|
|
|
push (@out,".section\t.init\n");
|
|
|
|
# One can argue that it's wasteful to craft every
|
|
|
|
# SSE/MMX module with this snippet... Well, it's 72
|
|
|
|
# bytes long and for the moment we have two modules.
|
|
|
|
# Let's argue when we have 7 modules or so...
|
2004-07-26 20:18:55 +00:00
|
|
|
#
|
|
|
|
# $1<<10 sets a reserved bit to signal that variable
|
|
|
|
# was initialized already...
|
2004-08-29 16:10:27 +00:00
|
|
|
&main'picmeup("edx","OPENSSL_ia32cap_P");
|
2004-05-06 10:31:09 +00:00
|
|
|
$tmp=<<___;
|
|
|
|
cmpl \$0,(%edx)
|
|
|
|
jne 1f
|
2004-07-26 20:18:55 +00:00
|
|
|
movl \$1<<10,(%edx)
|
2004-05-06 10:31:09 +00:00
|
|
|
pushf
|
|
|
|
popl %eax
|
|
|
|
movl %eax,%ecx
|
|
|
|
xorl \$1<<21,%eax
|
|
|
|
pushl %eax
|
|
|
|
popf
|
|
|
|
pushf
|
|
|
|
popl %eax
|
|
|
|
xorl %ecx,%eax
|
2004-09-28 20:45:10 +00:00
|
|
|
btl \$21,%eax
|
2004-05-06 10:31:09 +00:00
|
|
|
jnc 1f
|
|
|
|
pushl %edi
|
|
|
|
pushl %ebx
|
|
|
|
movl %edx,%edi
|
|
|
|
movl \$1,%eax
|
2004-11-21 10:36:25 +00:00
|
|
|
.byte 0x0f,0xa2
|
2004-07-26 20:18:55 +00:00
|
|
|
orl \$1<<10,%edx
|
2004-05-06 10:31:09 +00:00
|
|
|
movl %edx,0(%edi)
|
|
|
|
popl %ebx
|
|
|
|
popl %edi
|
2004-09-28 20:45:10 +00:00
|
|
|
.align $align
|
2004-05-06 10:31:09 +00:00
|
|
|
1:
|
|
|
|
___
|
|
|
|
push (@out,$tmp);
|
|
|
|
}
|
|
|
|
|
2000-12-06 00:19:09 +00:00
|
|
|
if ($const ne "")
|
|
|
|
{
|
|
|
|
push(@out,".section .rodata\n");
|
|
|
|
push(@out,$const);
|
|
|
|
$const="";
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
|
|
|
sub main'data_word
|
|
|
|
{
|
2004-05-06 10:31:09 +00:00
|
|
|
push(@out,"\t.long\t".join(',',@_)."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'align
|
|
|
|
{
|
2004-08-01 17:03:50 +00:00
|
|
|
my $val=$_[0],$p2,$i;
|
|
|
|
if ($main'aout) {
|
|
|
|
for ($p2=0;$val!=0;$val>>=1) { $p2++; }
|
|
|
|
$val=$p2-1;
|
|
|
|
$val.=",0x90";
|
|
|
|
}
|
2004-08-02 22:02:17 +00:00
|
|
|
push(@out,".align\t$val\n");
|
1998-12-21 10:56:39 +00:00
|
|
|
}
|
2000-12-05 03:51:46 +00:00
|
|
|
|
2000-12-05 05:10:05 +00:00
|
|
|
# debug output functions: puts, putx, printf
|
|
|
|
|
2000-12-05 03:51:46 +00:00
|
|
|
sub main'puts
|
|
|
|
{
|
2000-12-05 05:10:05 +00:00
|
|
|
&pushvars();
|
2000-12-05 04:39:39 +00:00
|
|
|
&main'push('$Lstring' . ++$constl);
|
|
|
|
&main'call('puts');
|
|
|
|
$stack-=4;
|
|
|
|
&main'add("esp",4);
|
2000-12-05 05:10:05 +00:00
|
|
|
&popvars();
|
2000-12-05 03:51:46 +00:00
|
|
|
|
2000-12-05 04:39:39 +00:00
|
|
|
$const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
|
2000-12-05 03:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub main'putx
|
|
|
|
{
|
2000-12-05 05:10:05 +00:00
|
|
|
&pushvars();
|
2000-12-05 04:39:39 +00:00
|
|
|
&main'push($_[0]);
|
|
|
|
&main'push('$Lstring' . ++$constl);
|
|
|
|
&main'call('printf');
|
|
|
|
&main'add("esp",8);
|
2000-12-05 05:39:43 +00:00
|
|
|
$stack-=8;
|
2000-12-05 05:10:05 +00:00
|
|
|
&popvars();
|
2000-12-05 03:51:46 +00:00
|
|
|
|
2000-12-05 04:39:39 +00:00
|
|
|
$const .= "Lstring$constl:\n\t.string \"\%X\"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main'printf
|
|
|
|
{
|
|
|
|
$ostack = $stack;
|
2000-12-05 05:10:05 +00:00
|
|
|
&pushvars();
|
2000-12-05 04:39:39 +00:00
|
|
|
for ($i = @_ - 1; $i >= 0; $i--)
|
|
|
|
{
|
|
|
|
if ($i == 0) # change this to support %s format strings
|
|
|
|
{
|
2000-12-05 05:39:43 +00:00
|
|
|
&main'push('$Lstring' . ++$constl);
|
2000-12-05 04:39:39 +00:00
|
|
|
$const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($_[$i] =~ /([0-9]*)\(%esp\)/)
|
|
|
|
{
|
|
|
|
&main'push(($1 + $stack - $ostack) . '(%esp)');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&main'push($_[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&main'call('printf');
|
2000-12-05 05:10:05 +00:00
|
|
|
$stack-=4*@_;
|
2000-12-05 04:39:39 +00:00
|
|
|
&main'add("esp",4*@_);
|
2000-12-05 05:39:43 +00:00
|
|
|
&popvars();
|
2000-12-05 03:51:46 +00:00
|
|
|
}
|
2000-12-05 05:10:05 +00:00
|
|
|
|
|
|
|
sub pushvars
|
|
|
|
{
|
2000-12-06 04:14:37 +00:00
|
|
|
&main'pushf();
|
2000-12-05 05:10:05 +00:00
|
|
|
&main'push("edx");
|
|
|
|
&main'push("ecx");
|
|
|
|
&main'push("eax");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub popvars
|
|
|
|
{
|
|
|
|
&main'pop("eax");
|
|
|
|
&main'pop("ecx");
|
|
|
|
&main'pop("edx");
|
2000-12-06 04:14:37 +00:00
|
|
|
&main'popf();
|
2000-12-05 05:10:05 +00:00
|
|
|
}
|
2002-12-13 17:56:14 +00:00
|
|
|
|
|
|
|
sub main'picmeup
|
|
|
|
{
|
|
|
|
local($dst,$sym)=@_;
|
2003-01-03 17:37:53 +00:00
|
|
|
if ($main'cpp)
|
|
|
|
{
|
|
|
|
local($tmp)=<<___;
|
2002-12-13 17:56:14 +00:00
|
|
|
#if (defined(ELF) || defined(SOL)) && defined(PIC)
|
|
|
|
call 1f
|
|
|
|
1: popl $regs{$dst}
|
|
|
|
addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
|
|
|
|
movl $sym\@GOT($regs{$dst}),$regs{$dst}
|
|
|
|
#else
|
2002-12-15 10:06:27 +00:00
|
|
|
leal $sym,$regs{$dst}
|
2002-12-13 17:56:14 +00:00
|
|
|
#endif
|
|
|
|
___
|
2003-01-03 17:37:53 +00:00
|
|
|
push(@out,$tmp);
|
|
|
|
}
|
2003-01-09 16:06:09 +00:00
|
|
|
elsif ($main'pic && ($main'elf || $main'aout))
|
2003-01-03 17:37:53 +00:00
|
|
|
{
|
|
|
|
&main'call(&main'label("PIC_me_up"));
|
|
|
|
&main'set_label("PIC_me_up");
|
|
|
|
&main'blindpop($dst);
|
2004-08-02 22:02:17 +00:00
|
|
|
&main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
|
2003-01-03 17:37:53 +00:00
|
|
|
&main'label("PIC_me_up") . "]");
|
2004-08-02 22:02:17 +00:00
|
|
|
&main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
|
2003-01-03 17:37:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-01-03 21:49:49 +00:00
|
|
|
&main'lea($dst,&main'DWP($sym));
|
2003-01-03 17:37:53 +00:00
|
|
|
}
|
2002-12-13 17:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub main'blindpop { &out1("popl",@_); }
|
2004-07-26 20:18:55 +00:00
|
|
|
|
|
|
|
sub main'initseg
|
|
|
|
{
|
|
|
|
local($f)=@_;
|
2004-08-29 16:10:27 +00:00
|
|
|
local($tmp);
|
2004-07-26 20:18:55 +00:00
|
|
|
if ($main'elf)
|
|
|
|
{
|
2004-08-29 16:10:27 +00:00
|
|
|
$tmp=<<___;
|
|
|
|
.section .init
|
2004-07-26 20:18:55 +00:00
|
|
|
call $under$f
|
2004-09-28 20:45:10 +00:00
|
|
|
.align $align
|
2004-07-26 20:18:55 +00:00
|
|
|
___
|
|
|
|
}
|
2004-08-29 16:10:27 +00:00
|
|
|
elsif ($main'coff)
|
|
|
|
{
|
|
|
|
$tmp=<<___; # applies to both Cygwin and Mingw
|
|
|
|
.section .ctors
|
|
|
|
.long $under$f
|
|
|
|
___
|
|
|
|
}
|
|
|
|
elsif ($main'aout)
|
|
|
|
{
|
2004-08-29 21:36:37 +00:00
|
|
|
local($ctor)="${under}_GLOBAL_\$I\$$f";
|
|
|
|
$tmp=".text\n";
|
|
|
|
$tmp.=".type $ctor,\@function\n" if ($main'pic);
|
|
|
|
$tmp.=<<___; # OpenBSD way...
|
|
|
|
.globl $ctor
|
2004-08-29 16:10:27 +00:00
|
|
|
.align 2
|
2004-08-29 21:36:37 +00:00
|
|
|
$ctor:
|
2004-08-29 16:10:27 +00:00
|
|
|
jmp $under$f
|
|
|
|
___
|
|
|
|
}
|
|
|
|
push(@out,$tmp) if ($tmp);
|
2004-07-26 20:18:55 +00:00
|
|
|
}
|
2004-08-29 16:10:27 +00:00
|
|
|
|
|
|
|
1;
|