x86asm.pl revision 299983
1#!/usr/bin/env perl
2
3# require 'x86asm.pl';
4# &asm_init(<flavor>,"des-586.pl"[,$i386only]);
5# &function_begin("foo");
6# ...
7# &function_end("foo");
8# &asm_finish
9
10$out=();
11$i386=0;
12
13# AUTOLOAD is this context has quite unpleasant side effect, namely
14# that typos in function calls effectively go to assembler output,
15# but on the pros side we don't have to implement one subroutine per
16# each opcode...
17sub ::AUTOLOAD
18{ my $opcode = $AUTOLOAD;
19
20    die "more than 4 arguments passed to $opcode" if ($#_>3);
21
22    $opcode =~ s/.*:://;
23    if    ($opcode =~ /^push/) { $stack+=4; }
24    elsif ($opcode =~ /^pop/)  { $stack-=4; }
25
26    &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
27}
28
29sub ::emit
30{ my $opcode=shift;
31
32    if ($#_==-1)    { push(@out,"\t$opcode\n");				}
33    else            { push(@out,"\t$opcode\t".join(',',@_)."\n");	}
34}
35
36sub ::LB
37{   $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
38  $1."l";
39}
40sub ::HB
41{   $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
42  $1."h";
43}
44sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num);	}
45sub ::stack_pop	{ my $num=$_[0]*4; $stack-=$num; &add("esp",$num);	}
46sub ::blindpop	{ &pop($_[0]); $stack+=4;				}
47sub ::wparam	{ &DWP($stack+4*$_[0],"esp");				}
48sub ::swtmp	{ &DWP(4*$_[0],"esp");					}
49
50sub ::bswap
51{   if ($i386)	# emulate bswap for i386
52    {	&comment("bswap @_");
53	&xchg(&HB(@_),&LB(@_));
54	&ror (@_,16);
55	&xchg(&HB(@_),&LB(@_));
56    }
57    else
58    {	&generic("bswap",@_);	}
59}
60# These are made-up opcodes introduced over the years essentially
61# by ignorance, just alias them to real ones...
62sub ::movb	{ &mov(@_);	}
63sub ::xorb	{ &xor(@_);	}
64sub ::rotl	{ &rol(@_);	}
65sub ::rotr	{ &ror(@_);	}
66sub ::exch	{ &xchg(@_);	}
67sub ::halt	{ &hlt;		}
68sub ::movz	{ &movzx(@_);	}
69sub ::pushf	{ &pushfd;	}
70sub ::popf	{ &popfd;	}
71
72# 3 argument instructions
73sub ::movq
74{ my($p1,$p2,$optimize)=@_;
75
76    if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
77    # movq between mmx registers can sink Intel CPUs
78    {	&::pshufw($p1,$p2,0xe4);		}
79    else
80    {	&::generic("movq",@_);			}
81}
82
83# SSE>2 instructions
84my %regrm = (	"eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3,
85		"esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7	);
86sub ::pextrd
87{ my($dst,$src,$imm)=@_;
88    if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/)
89    {	&::data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2<<3)|$regrm{$1},$imm);	}
90    else
91    {	&::generic("pextrd",@_);		}
92}
93
94sub ::pinsrd
95{ my($dst,$src,$imm)=@_;
96    if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
97    {	&::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm);	}
98    else
99    {	&::generic("pinsrd",@_);		}
100}
101
102sub ::pshufb
103{ my($dst,$src)=@_;
104    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
105    {	&data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2);	}
106    else
107    {	&::generic("pshufb",@_);		}
108}
109
110sub ::palignr
111{ my($dst,$src,$imm)=@_;
112    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
113    {	&::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1<<3)|$2,$imm);	}
114    else
115    {	&::generic("palignr",@_);		}
116}
117
118sub ::pclmulqdq
119{ my($dst,$src,$imm)=@_;
120    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
121    {	&::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm);	}
122    else
123    {	&::generic("pclmulqdq",@_);		}
124}
125
126sub ::rdrand
127{ my ($dst)=@_;
128    if ($dst =~ /(e[a-dsd][ixp])/)
129    {	&::data_byte(0x0f,0xc7,0xf0|$regrm{$dst});	}
130    else
131    {	&::generic("rdrand",@_);	}
132}
133
134# label management
135$lbdecor="L";		# local label decoration, set by package
136$label="000";
137
138sub ::islabel		# see is argument is a known label
139{ my $i;
140    foreach $i (values %label) { return $i if ($i eq $_[0]); }
141  $label{$_[0]};	# can be undef
142}
143
144sub ::label		# instantiate a function-scope label
145{   if (!defined($label{$_[0]}))
146    {	$label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++;   }
147  $label{$_[0]};
148}
149
150sub ::LABEL		# instantiate a file-scope label
151{   $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
152  $label{$_[0]};
153}
154
155sub ::static_label	{ &::LABEL($_[0],$lbdecor.$_[0]); }
156
157sub ::set_label_B	{ push(@out,"@_:\n"); }
158sub ::set_label
159{ my $label=&::label($_[0]);
160    &::align($_[1]) if ($_[1]>1);
161    &::set_label_B($label);
162  $label;
163}
164
165sub ::wipe_labels	# wipes function-scope labels
166{   foreach $i (keys %label)
167    {	delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/);	}
168}
169
170# subroutine management
171sub ::function_begin
172{   &function_begin_B(@_);
173    $stack=4;
174    &push("ebp");
175    &push("ebx");
176    &push("esi");
177    &push("edi");
178}
179
180sub ::function_end
181{   &pop("edi");
182    &pop("esi");
183    &pop("ebx");
184    &pop("ebp");
185    &ret();
186    &function_end_B(@_);
187    $stack=0;
188    &wipe_labels();
189}
190
191sub ::function_end_A
192{   &pop("edi");
193    &pop("esi");
194    &pop("ebx");
195    &pop("ebp");
196    &ret();
197    $stack+=16;	# readjust esp as if we didn't pop anything
198}
199
200sub ::asciz
201{ my @str=unpack("C*",shift);
202    push @str,0;
203    while ($#str>15) {
204	&data_byte(@str[0..15]);
205	foreach (0..15) { shift @str; }
206    }
207    &data_byte(@str) if (@str);
208}
209
210sub ::asm_finish
211{   &file_end();
212    print @out;
213}
214
215sub ::asm_init
216{ my ($type,$fn,$cpu)=@_;
217
218    $filename=$fn;
219    $i386=$cpu;
220
221    $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$android=0;
222    if    (($type eq "elf"))
223    {	$elf=1;			require "x86gas.pl";	}
224    elsif (($type eq "a\.out"))
225    {	$aout=1;		require "x86gas.pl";	}
226    elsif (($type eq "coff" or $type eq "gaswin"))
227    {	$coff=1;		require "x86gas.pl";	}
228    elsif (($type eq "win32n"))
229    {	$win32=1;		require "x86nasm.pl";	}
230    elsif (($type eq "nw-nasm"))
231    {	$netware=1;		require "x86nasm.pl";	}
232    #elsif (($type eq "nw-mwasm"))
233    #{	$netware=1; $mwerks=1;	require "x86nasm.pl";	}
234    elsif (($type eq "win32"))
235    {	$win32=1;		require "x86masm.pl";	}
236    elsif (($type eq "macosx"))
237    {	$aout=1; $macosx=1;	require "x86gas.pl";	}
238    elsif (($type eq "android"))
239    {	$elf=1; $android=1;	require "x86gas.pl";	}
240    else
241    {	print STDERR <<"EOF";
242Pick one target type from
243	elf	- Linux, FreeBSD, Solaris x86, etc.
244	a.out	- DJGPP, elder OpenBSD, etc.
245	coff	- GAS/COFF such as Win32 targets
246	win32n	- Windows 95/Windows NT NASM format
247	nw-nasm - NetWare NASM format
248	macosx	- Mac OS X
249EOF
250	exit(1);
251    }
252
253    $pic=0;
254    for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
255
256    $filename =~ s/\.pl$//;
257    &file($filename);
258}
259
260sub ::hidden {}
261
2621;
263