Merge commit 'dfc115332c94a2f62058ac7f2bce7631fbd20b3d'
[unleashed/tickless.git] / lib / libcrypto / perlasm / x86asm.pl
blobe039382e009457a998b6eafc11a031c6da5ad144
1 #!/usr/bin/env perl
3 # require 'x86asm.pl';
4 # &asm_init(<flavor>,"des-586.pl"[,$i386only]);
5 # &function_begin("foo");
6 # ...
7 # &function_end("foo");
8 # &asm_finish
10 $out=();
11 $i386=0;
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...
17 sub ::AUTOLOAD
18 { my $opcode = $AUTOLOAD;
20 die "more than 4 arguments passed to $opcode" if ($#_>3);
22 $opcode =~ s/.*:://;
23 if ($opcode =~ /^push/) { $stack+=4; }
24 elsif ($opcode =~ /^pop/) { $stack-=4; }
26 &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
29 sub ::emit
30 { my $opcode=shift;
32 if ($#_==-1) { push(@out,"\t$opcode\n"); }
33 else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
36 sub ::emitraw
37 { my $opcode=shift;
39 if ($#_==-1) { push(@out,"$opcode\n"); }
40 else { push(@out,"$opcode\t".join(',',@_)."\n"); }
43 sub ::LB
44 { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
45 $1."l";
47 sub ::HB
48 { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
49 $1."h";
51 sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); }
52 sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); }
53 sub ::blindpop { &pop($_[0]); $stack+=4; }
54 sub ::wparam { &DWP($stack+4*$_[0],"esp"); }
55 sub ::swtmp { &DWP(4*$_[0],"esp"); }
57 sub ::bswap
58 { if ($i386) # emulate bswap for i386
59 { &comment("bswap @_");
60 &xchg(&HB(@_),&LB(@_));
61 &ror (@_,16);
62 &xchg(&HB(@_),&LB(@_));
64 else
65 { &generic("bswap",@_); }
67 # These are made-up opcodes introduced over the years essentially
68 # by ignorance, just alias them to real ones...
69 sub ::movb { &mov(@_); }
70 sub ::xorb { &xor(@_); }
71 sub ::rotl { &rol(@_); }
72 sub ::rotr { &ror(@_); }
73 sub ::exch { &xchg(@_); }
74 sub ::halt { &hlt; }
75 sub ::movz { &movzx(@_); }
76 sub ::pushf { &pushfd; }
77 sub ::popf { &popfd; }
79 # 3 argument instructions
80 sub ::movq
81 { my($p1,$p2,$optimize)=@_;
83 if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
84 # movq between mmx registers can sink Intel CPUs
85 { &::pshufw($p1,$p2,0xe4); }
86 else
87 { &::generic("movq",@_); }
90 # SSE>2 instructions
91 my %regrm = ( "eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3,
92 "esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7 );
93 sub ::pextrd
94 { my($dst,$src,$imm)=@_;
95 if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/)
96 { &::data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2<<3)|$regrm{$1},$imm); }
97 else
98 { &::generic("pextrd",@_); }
101 sub ::pinsrd
102 { my($dst,$src,$imm)=@_;
103 if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
104 { &::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm); }
105 else
106 { &::generic("pinsrd",@_); }
109 sub ::pshufb
110 { my($dst,$src)=@_;
111 if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
112 { &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2); }
113 else
114 { &::generic("pshufb",@_); }
117 sub ::palignr
118 { my($dst,$src,$imm)=@_;
119 if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
120 { &::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1<<3)|$2,$imm); }
121 else
122 { &::generic("palignr",@_); }
125 sub ::pclmulqdq
126 { my($dst,$src,$imm)=@_;
127 if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
128 { &::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm); }
129 else
130 { &::generic("pclmulqdq",@_); }
133 # label management
134 $lbdecor="L"; # local label decoration, set by package
135 $label="000";
137 sub ::islabel # see is argument is a known label
138 { my $i;
139 foreach $i (values %label) { return $i if ($i eq $_[0]); }
140 $label{$_[0]}; # can be undef
143 sub ::label # instantiate a function-scope label
144 { if (!defined($label{$_[0]}))
145 { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
146 $label{$_[0]};
149 sub ::LABEL # instantiate a file-scope label
150 { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
151 $label{$_[0]};
154 sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
156 sub ::set_label_B { push(@out,"@_:\n"); }
157 sub ::set_label
158 { my $label=&::label($_[0]);
159 &::align($_[1]) if ($_[1]>1);
160 &::set_label_B($label);
161 $label;
164 sub ::wipe_labels # wipes function-scope labels
165 { foreach $i (keys %label)
166 { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
169 # subroutine management
170 sub ::function_begin
171 { &function_begin_B(@_);
172 $stack=4;
173 &push("ebp");
174 &push("ebx");
175 &push("esi");
176 &push("edi");
179 sub ::function_end
180 { &pop("edi");
181 &pop("esi");
182 &pop("ebx");
183 &pop("ebp");
184 &ret();
185 &function_end_B(@_);
186 $stack=0;
187 &wipe_labels();
190 sub ::function_end_A
191 { &pop("edi");
192 &pop("esi");
193 &pop("ebx");
194 &pop("ebp");
195 &ret();
196 $stack+=16; # readjust esp as if we didn't pop anything
199 sub ::asciz
200 { my @str=unpack("C*",shift);
201 push @str,0;
202 while ($#str>15) {
203 &data_byte(@str[0..15]);
204 foreach (0..15) { shift @str; }
206 &data_byte(@str) if (@str);
209 sub ::asm_finish
210 { &file_end();
211 print @out;
214 sub ::asm_init
215 { my ($type,$fn,$cpu)=@_;
217 $filename=$fn;
218 $i386=$cpu;
220 $elf=$cpp=$coff=$aout=$macosx=$win32=$openbsd=$android=0;
221 if (($type eq "elf"))
222 { $elf=1; require "x86gas.pl"; }
223 elsif (($type eq "a\.out"))
224 { $aout=1; require "x86gas.pl"; }
225 elsif (($type eq "coff" or $type eq "gaswin"))
226 { $coff=1; require "x86gas.pl"; }
227 elsif (($type eq "macosx"))
228 { $aout=1; $macosx=1; require "x86gas.pl"; }
229 elsif (($type eq "openbsd-elf"))
230 { $openbsd=$elf=1; require "x86gas.pl"; }
231 elsif (($type eq "openbsd-a.out"))
232 { $openbsd=1; require "x86gas.pl"; }
233 elsif (($type eq "android"))
234 { $elf=1; $android=1; require "x86gas.pl"; }
235 else
236 { print STDERR <<"EOF";
237 Pick one target type from
238 elf - Linux, FreeBSD, Solaris x86, etc.
239 a.out - DJGPP, elder OpenBSD, etc.
240 coff - GAS/COFF such as Win32 targets
241 openbsd-elf - OpenBSD elf
242 openbsd-a.out - OpenBSD a.out
243 macosx - Mac OS X
245 exit(1);
248 $pic=0;
249 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
251 ::emitraw("#include \"x86_arch.h\"\n");
252 ::emitraw("#include <machine/asm.h>\n") if $openbsd;
253 $filename =~ s/\.pl$//;
254 &file($filename);