4 # &asm_init(<flavor>,"des-586.pl"[,$i386only]);
5 # &function_begin("foo");
7 # &function_end("foo");
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
18 { my $opcode = $AUTOLOAD;
20 die "more than 4 arguments passed to $opcode" if ($#_>3);
23 if ($opcode =~ /^push/) { $stack+=4; }
24 elsif ($opcode =~ /^pop/) { $stack-=4; }
26 &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
32 if ($#_==-1) { push(@out,"\t$opcode\n"); }
33 else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
37 { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
41 { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
44 sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); }
45 sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); }
46 sub ::blindpop { &pop($_[0]); $stack+=4; }
47 sub ::wparam { &DWP($stack+4*$_[0],"esp"); }
48 sub ::swtmp { &DWP(4*$_[0],"esp"); }
51 { if ($i386) # emulate bswap for i386
52 { &comment("bswap @_");
53 &xchg(&HB(@_),&LB(@_));
55 &xchg(&HB(@_),&LB(@_));
58 { &generic("bswap",@_); }
60 # These are made-up opcodes introduced over the years essentially
61 # by ignorance, just alias them to real ones...
62 sub ::movb { &mov(@_); }
63 sub ::xorb { &xor(@_); }
64 sub ::rotl { &rol(@_); }
65 sub ::rotr { &ror(@_); }
66 sub ::exch { &xchg(@_); }
68 sub ::movz { &movzx(@_); }
69 sub ::pushf { &pushfd; }
70 sub ::popf { &popfd; }
72 # 3 argument instructions
74 { my($p1,$p2,$optimize)=@_;
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); }
80 { &::generic("movq",@_); }
84 my %regrm = ( "eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3,
85 "esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7 );
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); }
91 { &::generic("pextrd",@_); }
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); }
99 { &::generic("pinsrd",@_); }
104 if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
105 { &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2); }
107 { &::generic("pshufb",@_); }
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); }
115 { &::generic("palignr",@_); }
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); }
123 { &::generic("pclmulqdq",@_); }
128 if ($dst =~ /(e[a-dsd][ixp])/)
129 { &::data_byte(0x0f,0xc7,0xf0|$regrm{$dst}); }
131 { &::generic("rdrand",@_); }
136 my ($dst,$src1,$src2,$rxb)=@_;
139 $rxb&=~(0x04<<5) if($dst>=8);
140 $rxb&=~(0x01<<5) if($src1>=8);
141 $rxb&=~(0x02<<5) if($src2>=8);
146 { my $args=join(',',@_);
147 if ($args =~ /xmm([0-7]),xmm([0-7]),([x0-9a-f]+)/)
149 rxb(\@opcode,$1,$2,-1,0x08);
150 push @opcode,0x78,0xc2;
151 push @opcode,0xc0|($2&7)|(($1&7)<<3); # ModR/M
153 push @opcode,$c=~/^0/?oct($c):$c;
154 &::data_byte(@opcode);
157 { &::generic("vprotd",@_); }
161 $lbdecor="L"; # local label decoration, set by package
164 sub ::islabel # see is argument is a known label
166 foreach $i (values %label) { return $i if ($i eq $_[0]); }
167 $label{$_[0]}; # can be undef
170 sub ::label # instantiate a function-scope label
171 { if (!defined($label{$_[0]}))
172 { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
176 sub ::LABEL # instantiate a file-scope label
177 { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
181 sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
183 sub ::set_label_B { push(@out,"@_:\n"); }
185 { my $label=&::label($_[0]);
186 &::align($_[1]) if ($_[1]>1);
187 &::set_label_B($label);
191 sub ::wipe_labels # wipes function-scope labels
192 { foreach $i (keys %label)
193 { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
196 # subroutine management
198 { &function_begin_B(@_);
223 $stack+=16; # readjust esp as if we didn't pop anything
227 { my @str=unpack("C*",shift);
230 &data_byte(@str[0..15]);
231 foreach (0..15) { shift @str; }
233 &data_byte(@str) if (@str);
242 { my ($type,$fn,$cpu)=@_;
247 $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$android=0;
248 if (($type eq "elf"))
249 { $elf=1; require "x86gas.pl"; }
250 elsif (($type eq "a\.out"))
251 { $aout=1; require "x86gas.pl"; }
252 elsif (($type eq "coff" or $type eq "gaswin"))
253 { $coff=1; require "x86gas.pl"; }
254 elsif (($type eq "win32n"))
255 { $win32=1; require "x86nasm.pl"; }
256 elsif (($type eq "nw-nasm"))
257 { $netware=1; require "x86nasm.pl"; }
258 #elsif (($type eq "nw-mwasm"))
259 #{ $netware=1; $mwerks=1; require "x86nasm.pl"; }
260 elsif (($type eq "win32"))
261 { $win32=1; require "x86masm.pl"; }
262 elsif (($type eq "macosx"))
263 { $aout=1; $macosx=1; require "x86gas.pl"; }
264 elsif (($type eq "android"))
265 { $elf=1; $android=1; require "x86gas.pl"; }
267 { print STDERR <<"EOF";
268 Pick one target type from
269 elf - Linux, FreeBSD, Solaris x86, etc.
270 a.out - DJGPP, elder OpenBSD, etc.
271 coff - GAS/COFF such as Win32 targets
272 win32n - Windows 95/Windows NT NASM format
273 nw-nasm - NetWare NASM format
280 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
282 $filename =~ s/\.pl$//;