Commit 0fa79cbe authored by Andy Polyakov's avatar Andy Polyakov
Browse files

Synchronize x86 perlasm with HEAD.

parent be22102d
Loading
Loading
Loading
Loading
+132 −119
Original line number Diff line number Diff line
#!/usr/local/bin/perl
#!/usr/bin/env perl

# require 'x86asm.pl';
# &asm_init("cpp","des-586.pl");
# XXX
# XXX
# main'asm_finish

sub main'asm_finish
	{
	&file_end();
	&asm_finish_cpp() if $cpp;
	print &asm_get_output();
# &asm_init(<flavor>,"des-586.pl"[,$i386only]);
# &function_begin("foo");
# ...
# &function_end("foo");
# &asm_finish

# 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;

    die "more than 2 arguments passed to $opcode" if ($#_>1);

    $opcode =~ s/.*:://;
    if    ($opcode =~ /^push/) { $stack+=4; }
    elsif ($opcode =~ /^pop/)  { $stack-=4; }

    &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
}

sub main'asm_init
	{
	($type,$fn,$i386)=@_;
$out=();
$i386=0;

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;		}

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();
    $stack=0;
    &function_end_B(@_);
}

sub ::function_end_A
{   &pop("edi");
    &pop("esi");
    &pop("ebx");
    &pop("ebp");
    &ret();
    $stack+=16;	# readjust esp as if we didn't pop anything
}

sub ::asciz {   foreach (@_) { &data_byte(unpack("C*",$_),0); }   }

sub ::asm_finish
{   &file_end();
    print @out;
}

sub ::asm_init
{ my ($type,$fn,$cpu)=@_;

    $filename=$fn;
    $i386=$cpu;

    $elf=$cpp=$coff=$aout=$win32=$netware=$mwerks=0;
    if    (($type eq "elf"))
    {	$elf=1;			require "x86unix.pl";	}
	elsif (	($type eq "a.out"))
    elsif (($type eq "a\.out"))
    {	$aout=1;		require "x86unix.pl";	}
    elsif (($type eq "coff" or $type eq "gaswin"))
    {	$coff=1;		require "x86unix.pl";	}
	elsif (	($type eq "cpp"))
		{ $cpp=1; require "x86unix.pl"; }
	elsif (	($type eq "win32"))
		{ $win32=1; require "x86ms.pl"; }
    elsif (($type eq "win32n"))
    {	$win32=1;		require "x86nasm.pl";	}
    elsif (($type eq "nw-nasm"))
@@ -36,13 +121,11 @@ sub main'asm_init
    elsif (($type eq "nw-mwasm"))
    {	$netware=1; $mwerks=1;	require "x86nasm.pl";	}
    else
		{
		print STDERR <<"EOF";
    {	print STDERR <<"EOF";
Pick one target type from
	elf	- Linux, FreeBSD, Solaris x86, etc.
	a.out	- OpenBSD, DJGPP, etc.
	a.out	- DJGPP, elder OpenBSD, etc.
	coff	- GAS/COFF such as Win32 targets
	win32	- Windows 95/Windows NT
	win32n	- Windows 95/Windows NT NASM format
	nw-nasm - NetWare NASM format
	nw-mwasm- NetWare Metrowerks Assembler
@@ -53,78 +136,8 @@ EOF
    $pic=0;
    for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }

	&asm_init_output();

&comment("Don't even think of reading this code");
&comment("It was automatically generated by $filename");
&comment("Which is a perl program used to generate the x86 assember for");
&comment("any of ELF, a.out, COFF, Win32, ...");
&comment("eric <eay\@cryptsoft.com>");
&comment("");

    $filename =~ s/\.pl$//;
    &file($filename);
}

sub asm_finish_cpp
	{
	return unless $cpp;

	local($tmp,$i);
	foreach $i (&get_labels())
		{
		$tmp.="#define $i _$i\n";
		}
	print <<"EOF";
/* Run the C pre-processor over this file with one of the following defined
 * ELF - elf object files,
 * OUT - a.out object files,
 * BSDI - BSDI style a.out object files
 * SOL - Solaris style elf
 */

#define TYPE(a,b)       .type   a,b
#define SIZE(a,b)       .size   a,b

#if defined(OUT) || (defined(BSDI) && !defined(ELF))
$tmp
#endif

#ifdef OUT
#define OK	1
#define ALIGN	4
#if defined(__CYGWIN__) || defined(__DJGPP__) || (__MINGW32__)
#undef SIZE
#undef TYPE
#define SIZE(a,b)
#define TYPE(a,b)	.def a; .scl 2; .type 32; .endef
#endif /* __CYGWIN || __DJGPP */
#endif

#if defined(BSDI) && !defined(ELF)
#define OK              1
#define ALIGN           4
#undef SIZE
#undef TYPE
#define SIZE(a,b)
#define TYPE(a,b)
#endif

#if defined(ELF) || defined(SOL)
#define OK              1
#define ALIGN           16
#endif

#ifndef OK
You need to define one of
ELF - elf systems - linux-elf, NetBSD and DG-UX
OUT - a.out systems - linux-a.out and FreeBSD
SOL - solaris systems, which are elf with strange comment lines
BSDI - a.out with a very primative version of as.
#endif

/* Let the Assembler begin :-) */
EOF
	}

1;

crypto/perlasm/x86ms.pl

deleted100644 → 0
+0 −462
Original line number Diff line number Diff line
#!/usr/local/bin/perl

package x86ms;

$label="L000";

%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',
	);

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,@_);
	foreach (@_) {
		push(@out, "EXTRN\t_$_:DWORD\n");
	}
}

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'BP
	{
	&get_mem("BYTE",@_);
	}

sub main'DWP
	{
	&get_mem("DWORD",@_);
	}

sub main'QWP
	{
	&get_mem("QWORD",@_);
	}

sub main'BC
	{
	return @_;
	}

sub main'DWC
	{
	return @_;
	}

sub main'stack_push
	{
	local($num)=@_;
	$stack+=$num*4;
	&main'sub("esp",$num*4);
	}

sub main'stack_pop
	{
	local($num)=@_;
	$stack-=$num*4;
	&main'add("esp",$num*4);
	}

sub get_mem
	{
	local($size,$addr,$reg1,$reg2,$idx)=@_;
	local($t,$post);
	local($ret)="$size PTR ";

	$addr =~ s/^\s+//;
	if ($addr =~ /^(.+)\+(.+)$/)
		{
		$reg2=&conv($1);
		$addr="_$2";
		}
	elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i)
		{
		$addr="_$addr";
		}

	if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }

	$reg1="$regs{$reg1}" if defined($regs{$reg1});
	$reg2="$regs{$reg2}" if defined($regs{$reg2});
	if (($addr ne "") && ($addr ne 0))
		{
		if ($addr !~ /^-/)
			{ $ret.=$addr; }
		else	{ $post=$addr; }
		}
	if ($reg2 ne "")
		{
		$t="";
		$t="*$idx" if ($idx != 0);
		$reg1="+".$reg1 if ("$reg1$post" ne "");
		$ret.="[$reg2$t$reg1$post]";
		}
	else
		{
		$ret.="[$reg1$post]"
		}
	$ret =~ s/\[\]//;	# in case $addr was the only argument
	return($ret);
	}

sub main'mov	{ &out2("mov",@_); }
sub main'movb	{ &out2("mov",@_); }
sub main'and	{ &out2("and",@_); }
sub main'or	{ &out2("or",@_); }
sub main'shl	{ &out2("shl",@_); }
sub main'shr	{ &out2("shr",@_); }
sub main'xor	{ &out2("xor",@_); }
sub main'xorb	{ &out2("xor",@_); }
sub main'add	{ &out2("add",@_); }
sub main'adc	{ &out2("adc",@_); }
sub main'sub	{ &out2("sub",@_); }
sub main'sbb	{ &out2("sbb",@_); }
sub main'rotl	{ &out2("rol",@_); }
sub main'rotr	{ &out2("ror",@_); }
sub main'exch	{ &out2("xchg",@_); }
sub main'cmp	{ &out2("cmp",@_); }
sub main'lea	{ &out2("lea",@_); }
sub main'mul	{ &out1("mul",@_); }
sub main'div	{ &out1("div",@_); }
sub main'dec	{ &out1("dec",@_); }
sub main'inc	{ &out1("inc",@_); }
sub main'jmp	{ &out1("jmp",@_); }
sub main'jmp_ptr { &out1p("jmp",@_); }
sub main'je	{ &out1("je",@_); }
sub main'jle	{ &out1("jle",@_); }
sub main'jz	{ &out1("jz",@_); }
sub main'jge	{ &out1("jge",@_); }
sub main'jl	{ &out1("jl",@_); }
sub main'ja	{ &out1("ja",@_); }
sub main'jae	{ &out1("jae",@_); }
sub main'jb	{ &out1("jb",@_); }
sub main'jbe	{ &out1("jbe",@_); }
sub main'jc	{ &out1("jc",@_); }
sub main'jnc	{ &out1("jnc",@_); }
sub main'jnz	{ &out1("jnz",@_); }
sub main'jne	{ &out1("jne",@_); }
sub main'jno	{ &out1("jno",@_); }
sub main'push	{ &out1("push",@_); $stack+=4; }
sub main'pop	{ &out1("pop",@_); $stack-=4; }
sub main'pushf	{ &out0("pushfd"); $stack+=4; }
sub main'popf	{ &out0("popfd"); $stack-=4; }
sub main'bswap	{ &out1("bswap",@_); &using486(); }
sub main'not	{ &out1("not",@_); }
sub main'call	{ &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
sub main'call_ptr { &out1p("call",@_); }
sub main'ret	{ &out0("ret"); }
sub main'nop	{ &out0("nop"); }
sub main'test	{ &out2("test",@_); }
sub main'bt	{ &out2("bt",@_); }
sub main'leave	{ &out0("leave"); }
sub main'cpuid  { &out0("DW\t0A20Fh"); }
sub main'rdtsc  { &out0("DW\t0310Fh"); }
sub main'halt	{ &out0("hlt"); }
sub main'movz	{ &out2("movzx",@_); }
sub main'neg	{ &out1("neg",@_); }
sub main'cld	{ &out0("cld"); }

# 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",@_); }

sub out2
	{
	local($name,$p1,$p2)=@_;
	local($l,$t,$line);

	$line="\t$name\t";
	$t=&conv($p1).",";
	$l=length($t);
	$line.="$t";
	$l=4-($l+9)/8;
	$line.="\t" x $l;
	$line.=&conv($p2);
	if ($line=~/\bxmm[0-7]\b/i) { $line=~s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i; }
	push(@out,$line."\n");
	}

sub out0
	{
	local($name)=@_;

	push(@out,"\t$name\n");
	}

sub out1
	{
	local($name,$p1)=@_;
	local($l,$t);

	push(@out,"\t$name\t".&conv($p1)."\n");
	}

sub conv
	{
	local($p)=@_;

	$p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
	return $p;
	}

sub using486
	{
	return if $using486;
	$using486++;
	grep(s/\.386/\.486/,@out);
	}

sub main'file
	{
	local($file)=@_;

	local($tmp)=<<"EOF";
	TITLE	$file.asm
        .386
.model	FLAT
_TEXT\$	SEGMENT PAGE 'CODE'

EOF
	push(@out,$tmp);
	}

sub main'function_begin
	{
	local($func,$extra)=@_;

	push(@labels,$func);

	local($tmp)=<<"EOF";
PUBLIC	_$func
$extra
_$func PROC NEAR
	push	ebp
	push	ebx
	push	esi
	push	edi
EOF
	push(@out,$tmp);
	$stack=20;
	}

sub main'function_begin_B
	{
	local($func,$extra)=@_;

	local($tmp)=<<"EOF";
PUBLIC	_$func
$extra
_$func PROC NEAR
EOF
	push(@out,$tmp);
	$stack=4;
	}

sub main'function_end
	{
	local($func)=@_;

	local($tmp)=<<"EOF";
	pop	edi
	pop	esi
	pop	ebx
	pop	ebp
	ret
_$func ENDP
EOF
	push(@out,$tmp);
	$stack=0;
	%label=();
	}

sub main'function_end_B
	{
	local($func)=@_;

	local($tmp)=<<"EOF";
_$func ENDP
EOF
	push(@out,$tmp);
	$stack=0;
	%label=();
	}

sub main'function_end_A
	{
	local($func)=@_;

	local($tmp)=<<"EOF";
	pop	edi
	pop	esi
	pop	ebx
	pop	ebp
	ret
EOF
	push(@out,$tmp);
	}

sub main'file_end
	{
	# try to detect if SSE2 or MMX extensions were used...
	if (grep {/\b[x]?mm[0-7]\b,/i} @out) {
		grep {s/\.[3-7]86/\.686\n\t\.XMM/} @out;
		}
	push(@out,"_TEXT\$	ENDS\n");
	push(@out,"END\n");
	}

sub main'wparam
	{
	local($num)=@_;

	return(&main'DWP($stack+$num*4,"esp","",0));
	}

sub main'swtmp
	{
	return(&main'DWP($_[0]*4,"esp","",0));
	}

# 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));
#	}

sub main'comment
	{
	foreach (@_)
		{
		push(@out,"\t; $_\n");
		}
	}

sub main'public_label
	{
	$label{$_[0]}="_$_[0]"	if (!defined($label{$_[0]}));
	push(@out,"PUBLIC\t$label{$_[0]}\n");
	}

sub main'label
	{
	if (!defined($label{$_[0]}))
		{
		$label{$_[0]}="\$${label}${_[0]}";
		$label++;
		}
	return($label{$_[0]});
	}

sub main'set_label
	{
	if (!defined($label{$_[0]}))
		{
		$label{$_[0]}="\$${label}${_[0]}";
		$label++;
		}
	if ($_[1]!=0 && $_[1]>1)
		{
		main'align($_[1]);
		}
	if((defined $_[2]) && ($_[2] == 1))
		{
		push(@out,"$label{$_[0]}::\n");
		}
	elsif ($label{$_[0]} !~ /^\$/)
		{
		push(@out,"$label{$_[0]}\tLABEL PTR\n");
		}
	else
		{
		push(@out,"$label{$_[0]}:\n");
		}
	}

sub main'data_byte
	{
	push(@out,"\tDB\t".join(',',@_)."\n");
	}

sub main'data_word
	{
	push(@out,"\tDD\t".join(',',@_)."\n");
	}

sub main'align
	{
	push(@out,"\tALIGN\t$_[0]\n");
	}

sub out1p
	{
	local($name,$p1)=@_;
	local($l,$t);

	push(@out,"\t$name\t".&conv($p1)."\n");
	}

sub main'picmeup
	{
	local($dst,$sym)=@_;
	&main'lea($dst,&main'DWP($sym));
	}

sub main'blindpop { &out1("pop",@_); }

sub main'initseg 
	{
	local($f)=@_;
	local($tmp)=<<___;
OPTION	DOTNAME
.CRT\$XCU	SEGMENT DWORD PUBLIC 'DATA'
EXTRN	_$f:NEAR
DD	_$f
.CRT\$XCU	ENDS
___
	push(@out,$tmp);
	}

1;
+194 −417

File changed.

Preview size limit exceeded, changes collapsed.

+266 −715

File changed.

Preview size limit exceeded, changes collapsed.