Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/perlasm/x86asm.pl

raw
1 #! /usr/bin/env perl
2 # Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 # SPDX-License-Identifier: Apache-2.0
4
5
6 # require 'x86asm.pl';
7 # &asm_init(<flavor>[,$i386only]);
8 # &function_begin("foo");
9 # ...
10 # &function_end("foo");
11 # &asm_finish
12
13 $out=();
14 $i386=0;
15
16 # AUTOLOAD is this context has quite unpleasant side effect, namely
17 # that typos in function calls effectively go to assembler output,
18 # but on the pros side we don't have to implement one subroutine per
19 # each opcode...
20 sub ::AUTOLOAD
21 { my $opcode = $AUTOLOAD;
22
23 die "more than 4 arguments passed to $opcode" if ($#_>3);
24
25 $opcode =~ s/.*:://;
26 if ($opcode =~ /^push/) { $stack+=4; }
27 elsif ($opcode =~ /^pop/) { $stack-=4; }
28
29 &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
30 }
31
32 # record_function_hit(int) writes a byte with value one to the given offset of
33 # |BORINGSSL_function_hit|, but only if BORINGSSL_DISPATCH_TEST is defined.
34 # This is used in impl_dispatch_test.cc to test whether the expected assembly
35 # functions are triggered by high-level API calls.
36 sub ::record_function_hit
37 { my($index)=@_;
38 &preprocessor_ifdef("BORINGSSL_DISPATCH_TEST");
39 &push("ebx");
40 &push("edx");
41 &call(&label("pic"));
42 &set_label("pic");
43 &blindpop("ebx");
44 &lea("ebx",&DWP("BORINGSSL_function_hit+$index"."-".&label("pic"),"ebx"));
45 &mov("edx", 1);
46 &movb(&BP(0, "ebx"), "dl");
47 &pop("edx");
48 &pop("ebx");
49 &preprocessor_endif();
50 }
51
52 sub ::emit
53 { my $opcode=shift;
54
55 if ($#_==-1) { push(@out,"\t$opcode\n"); }
56 else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
57 }
58
59 sub ::LB
60 { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
61 $1."l";
62 }
63 sub ::HB
64 { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
65 $1."h";
66 }
67 sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); }
68 sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); }
69 sub ::blindpop { &pop($_[0]); $stack+=4; }
70 sub ::wparam { &DWP($stack+4*$_[0],"esp"); }
71 sub ::swtmp { &DWP(4*$_[0],"esp"); }
72
73 sub ::bswap
74 { if ($i386) # emulate bswap for i386
75 { &comment("bswap @_");
76 &xchg(&HB(@_),&LB(@_));
77 &ror (@_,16);
78 &xchg(&HB(@_),&LB(@_));
79 }
80 else
81 { &generic("bswap",@_); }
82 }
83 # These are made-up opcodes introduced over the years essentially
84 # by ignorance, just alias them to real ones...
85 sub ::movb { &mov(@_); }
86 sub ::xorb { &xor(@_); }
87 sub ::rotl { &rol(@_); }
88 sub ::rotr { &ror(@_); }
89 sub ::exch { &xchg(@_); }
90 sub ::halt { &hlt; }
91 sub ::movz { &movzx(@_); }
92 sub ::pushf { &pushfd; }
93 sub ::popf { &popfd; }
94
95 # 3 argument instructions
96 sub ::movq
97 { my($p1,$p2,$optimize)=@_;
98
99 if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
100 # movq between mmx registers can sink Intel CPUs
101 { &::pshufw($p1,$p2,0xe4); }
102 else
103 { &::generic("movq",@_); }
104 }
105
106 # SSE>2 instructions
107 my %regrm = ( "eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3,
108 "esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7 );
109 sub ::pextrd
110 { my($dst,$src,$imm)=@_;
111 if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/)
112 { &::data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2<<3)|$regrm{$1},$imm); }
113 else
114 { &::generic("pextrd",@_); }
115 }
116
117 sub ::pinsrd
118 { my($dst,$src,$imm)=@_;
119 if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
120 { &::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm); }
121 else
122 { &::generic("pinsrd",@_); }
123 }
124
125 sub ::pshufb
126 { my($dst,$src)=@_;
127 if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
128 { &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2); }
129 else
130 { &::generic("pshufb",@_); }
131 }
132
133 sub ::palignr
134 { my($dst,$src,$imm)=@_;
135 if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
136 { &::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1<<3)|$2,$imm); }
137 else
138 { &::generic("palignr",@_); }
139 }
140
141 sub ::pclmulqdq
142 { my($dst,$src,$imm)=@_;
143 if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
144 { &::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm); }
145 else
146 { &::generic("pclmulqdq",@_); }
147 }
148
149 sub ::rdrand
150 { my ($dst)=@_;
151 if ($dst =~ /(e[a-dsd][ixp])/)
152 { &::data_byte(0x0f,0xc7,0xf0|$regrm{$dst}); }
153 else
154 { &::generic("rdrand",@_); }
155 }
156
157 sub ::rdseed
158 { my ($dst)=@_;
159 if ($dst =~ /(e[a-dsd][ixp])/)
160 { &::data_byte(0x0f,0xc7,0xf8|$regrm{$dst}); }
161 else
162 { &::generic("rdrand",@_); }
163 }
164
165 sub rxb {
166 local *opcode=shift;
167 my ($dst,$src1,$src2,$rxb)=@_;
168
169 $rxb|=0x7<<5;
170 $rxb&=~(0x04<<5) if($dst>=8);
171 $rxb&=~(0x01<<5) if($src1>=8);
172 $rxb&=~(0x02<<5) if($src2>=8);
173 push @opcode,$rxb;
174 }
175
176 sub ::vprotd
177 { my $args=join(',',@_);
178 if ($args =~ /xmm([0-7]),xmm([0-7]),([x0-9a-f]+)/)
179 { my @opcode=(0x8f);
180 rxb(\@opcode,$1,$2,-1,0x08);
181 push @opcode,0x78,0xc2;
182 push @opcode,0xc0|($2&7)|(($1&7)<<3); # ModR/M
183 my $c=$3;
184 push @opcode,$c=~/^0/?oct($c):$c;
185 &::data_byte(@opcode);
186 }
187 else
188 { &::generic("vprotd",@_); }
189 }
190
191 sub ::endbranch
192 {
193 &::data_byte(0xf3,0x0f,0x1e,0xfb);
194 }
195
196 # label management
197 $lbdecor="L"; # local label decoration, set by package
198 $label="000";
199
200 sub ::islabel # see is argument is a known label
201 { my $i;
202 foreach $i (values %label) { return $i if ($i eq $_[0]); }
203 $label{$_[0]}; # can be undef
204 }
205
206 sub ::label # instantiate a function-scope label
207 { if (!defined($label{$_[0]}))
208 { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
209 $label{$_[0]};
210 }
211
212 sub ::LABEL # instantiate a file-scope label
213 { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
214 $label{$_[0]};
215 }
216
217 sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
218
219 sub ::set_label_B { push(@out,"@_:\n"); }
220 sub ::set_label
221 { my $label=&::label($_[0]);
222 &::align($_[1]) if ($_[1]>1);
223 &::set_label_B($label);
224 $label;
225 }
226
227 sub ::wipe_labels # wipes function-scope labels
228 { foreach $i (keys %label)
229 { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
230 }
231
232 # subroutine management
233 sub ::function_begin
234 { &function_begin_B(@_);
235 $stack=4;
236 &push("ebp");
237 &push("ebx");
238 &push("esi");
239 &push("edi");
240 }
241
242 sub ::function_end
243 { &pop("edi");
244 &pop("esi");
245 &pop("ebx");
246 &pop("ebp");
247 &ret();
248 &function_end_B(@_);
249 $stack=0;
250 &wipe_labels();
251 }
252
253 sub ::function_end_A
254 { &pop("edi");
255 &pop("esi");
256 &pop("ebx");
257 &pop("ebp");
258 &ret();
259 $stack+=16; # readjust esp as if we didn't pop anything
260 }
261
262 sub ::asciz
263 { my @str=unpack("C*",shift);
264 push @str,0;
265 while ($#str>15) {
266 &data_byte(@str[0..15]);
267 foreach (0..15) { shift @str; }
268 }
269 &data_byte(@str) if (@str);
270 }
271
272 sub ::asm_finish
273 { &file_end();
274 my $comment = "//";
275 $comment = ";" if ($win32);
276 print <<___;
277 $comment This file is generated from a similarly-named Perl script in the BoringSSL
278 $comment source tree. Do not edit by hand.
279
280 ___
281 if ($win32) {
282 print <<___ unless $masm;
283 \%include "openssl/boringssl_prefix_symbols_nasm.inc"
284 \%ifidn __OUTPUT_FORMAT__, win32
285 ___
286 print @out;
287 print <<___ unless $masm;
288 \%else
289 ; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738
290 ret
291 \%endif
292 ___
293 } else {
294 my $target;
295 if ($elf) {
296 $target = "defined(__ELF__)";
297 } elsif ($macosx) {
298 $target = "defined(__APPLE__)";
299 } else {
300 die "unknown target";
301 }
302
303 print <<___;
304 #include <openssl/asm_base.h>
305
306 #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && $target
307 ___
308 print @out;
309 print <<___;
310 #endif // !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && $target
311 ___
312 }
313 }
314
315 sub ::asm_init
316 { my ($type,$cpu)=@_;
317
318 $i386=$cpu;
319
320 $elf=$cpp=$coff=$aout=$macosx=$win32=$mwerks=$android=0;
321 if (($type eq "elf"))
322 { $elf=1; require "x86gas.pl"; }
323 elsif (($type eq "elf-1"))
324 { $elf=-1; require "x86gas.pl"; }
325 elsif (($type eq "a\.out"))
326 { $aout=1; require "x86gas.pl"; }
327 elsif (($type eq "coff" or $type eq "gaswin"))
328 { $coff=1; require "x86gas.pl"; }
329 elsif (($type eq "win32n"))
330 { $win32=1; require "x86nasm.pl"; }
331 elsif (($type eq "win32"))
332 { $win32=1; $masm=1; require "x86masm.pl"; }
333 elsif (($type eq "macosx"))
334 { $aout=1; $macosx=1; require "x86gas.pl"; }
335 elsif (($type eq "android"))
336 { $elf=1; $android=1; require "x86gas.pl"; }
337 else
338 { print STDERR <<"EOF";
339 Pick one target type from
340 elf - Linux, FreeBSD, Solaris x86, etc.
341 a.out - DJGPP, elder OpenBSD, etc.
342 coff - GAS/COFF such as Win32 targets
343 win32n - Windows 95/Windows NT NASM format
344 macosx - Mac OS X
345 EOF
346 exit(1);
347 }
348
349 $pic=0;
350 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
351
352 &file();
353 }
354
355 sub ::hidden {}
356
357 1;