Debian Codegraph @

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

raw
1 #! /usr/bin/env perl
2 # Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3 # SPDX-License-Identifier: Apache-2.0
4
5 my $flavour = shift;
6 my $output = shift;
7 open STDOUT,">$output" || die "can't open $output: $!";
8
9 my %GLOBALS;
10 my %TYPES;
11 my $dotinlocallabels=($flavour=~/linux/)?1:0;
12
13 ################################################################
14 # directives which need special treatment on different platforms
15 ################################################################
16 my $type = sub {
17 my ($dir,$name,$type) = @_;
18
19 $TYPES{$name} = $type;
20 if ($flavour =~ /linux/) {
21 $name =~ s|^\.||;
22 ".type $name,$type";
23 } else {
24 "";
25 }
26 };
27 my $globl = sub {
28 my $junk = shift;
29 my $name = shift;
30 my $global = \$GLOBALS{$name};
31 my $type = \$TYPES{$name};
32 my $ret;
33
34 $name =~ s|^\.||;
35
36 SWITCH: for ($flavour) {
37 /aix/ && do { if (!$$type) {
38 $$type = "\@function";
39 }
40 if ($$type =~ /function/) {
41 $name = ".$name";
42 }
43 last;
44 };
45 /osx/ && do { $name = "_$name";
46 last;
47 };
48 /linux.*(32|64le)/
49 && do { $ret .= ".globl $name";
50 if (!$$type) {
51 $ret .= "\n.type $name,\@function";
52 $$type = "\@function";
53 }
54 last;
55 };
56 /linux.*64/ && do { $ret .= ".globl $name";
57 if (!$$type) {
58 $ret .= "\n.type $name,\@function";
59 $$type = "\@function";
60 }
61 if ($$type =~ /function/) {
62 $ret .= "\n.section \".opd\",\"aw\"";
63 $ret .= "\n.align 3";
64 $ret .= "\n$name:";
65 $ret .= "\n.quad .$name,.TOC.\@tocbase,0";
66 $ret .= "\n.previous";
67 $name = ".$name";
68 }
69 last;
70 };
71 }
72
73 $ret = ".globl $name" if (!$ret);
74 $$global = $name;
75 $ret;
76 };
77 my $text = sub {
78 my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
79 $ret = ".abiversion 2\n".$ret if ($flavour =~ /linux.*64le/);
80 $ret;
81 };
82 my $machine = sub {
83 my $junk = shift;
84 my $arch = shift;
85 if ($flavour =~ /osx/)
86 { $arch =~ s/\"//g;
87 $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
88 }
89 ".machine $arch";
90 };
91 my $size = sub {
92 if ($flavour =~ /linux/)
93 { shift;
94 my $name = shift;
95 my $real = $GLOBALS{$name} ? \$GLOBALS{$name} : \$name;
96 my $ret = ".size $$real,.-$$real";
97 $name =~ s|^\.||;
98 if ($$real ne $name) {
99 $ret .= "\n.size $name,.-$$real";
100 }
101 $ret;
102 }
103 else
104 { ""; }
105 };
106 my $asciz = sub {
107 shift;
108 my $line = join(",",@_);
109 if ($line =~ /^"(.*)"$/)
110 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
111 else
112 { ""; }
113 };
114 my $quad = sub {
115 shift;
116 my @ret;
117 my ($hi,$lo);
118 for (@_) {
119 if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
120 { $hi=$1?"0x$1":"0"; $lo="0x$2"; }
121 elsif (/^([0-9]+)$/o)
122 { $hi=$1>>32; $lo=$1&0xffffffff; } # error-prone with 32-bit perl
123 else
124 { $hi=undef; $lo=$_; }
125
126 if (defined($hi))
127 { push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo"); }
128 else
129 { push(@ret,".quad $lo"); }
130 }
131 join("\n",@ret);
132 };
133
134 ################################################################
135 # simplified mnemonics not handled by at least one assembler
136 ################################################################
137 my $cmplw = sub {
138 my $f = shift;
139 my $cr = 0; $cr = shift if ($#_>1);
140 # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
141 ($flavour =~ /linux.*32/) ?
142 " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
143 " cmplw ".join(',',$cr,@_);
144 };
145 my $bdnz = sub {
146 my $f = shift;
147 my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
148 " bc $bo,0,".shift;
149 } if ($flavour!~/linux/);
150 my $bltlr = sub {
151 my $f = shift;
152 my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
153 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
154 " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
155 " bclr $bo,0";
156 };
157 my $bnelr = sub {
158 my $f = shift;
159 my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
160 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
161 " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
162 " bclr $bo,2";
163 };
164 my $beqlr = sub {
165 my $f = shift;
166 my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
167 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
168 " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
169 " bclr $bo,2";
170 };
171 # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
172 # arguments is 64, with "operand out of range" error.
173 my $extrdi = sub {
174 my ($f,$ra,$rs,$n,$b) = @_;
175 $b = ($b+$n)&63; $n = 64-$n;
176 " rldicl $ra,$rs,$b,$n";
177 };
178 my $vmr = sub {
179 my ($f,$vx,$vy) = @_;
180 " vor $vx,$vy,$vy";
181 };
182
183 # Some ABIs specify vrsave, special-purpose register #256, as reserved
184 # for system use.
185 my $no_vrsave = ($flavour =~ /aix|linux64le/);
186 my $mtspr = sub {
187 my ($f,$idx,$ra) = @_;
188 if ($idx == 256 && $no_vrsave) {
189 " or $ra,$ra,$ra";
190 } else {
191 " mtspr $idx,$ra";
192 }
193 };
194 my $mfspr = sub {
195 my ($f,$rd,$idx) = @_;
196 if ($idx == 256 && $no_vrsave) {
197 " li $rd,-1";
198 } else {
199 " mfspr $rd,$idx";
200 }
201 };
202
203 # PowerISA 2.06 stuff
204 sub vsxmem_op {
205 my ($f, $vrt, $ra, $rb, $op) = @_;
206 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
207 }
208 # made-up unaligned memory reference AltiVec/VMX instructions
209 my $lvx_u = sub { vsxmem_op(@_, 844); }; # lxvd2x
210 my $stvx_u = sub { vsxmem_op(@_, 972); }; # stxvd2x
211 my $lvdx_u = sub { vsxmem_op(@_, 588); }; # lxsdx
212 my $stvdx_u = sub { vsxmem_op(@_, 716); }; # stxsdx
213 my $lvx_4w = sub { vsxmem_op(@_, 780); }; # lxvw4x
214 my $stvx_4w = sub { vsxmem_op(@_, 908); }; # stxvw4x
215
216 # PowerISA 2.07 stuff
217 sub vcrypto_op {
218 my ($f, $vrt, $vra, $vrb, $op) = @_;
219 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
220 }
221 my $vcipher = sub { vcrypto_op(@_, 1288); };
222 my $vcipherlast = sub { vcrypto_op(@_, 1289); };
223 my $vncipher = sub { vcrypto_op(@_, 1352); };
224 my $vncipherlast= sub { vcrypto_op(@_, 1353); };
225 my $vsbox = sub { vcrypto_op(@_, 0, 1480); };
226 my $vshasigmad = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
227 my $vshasigmaw = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
228 my $vpmsumb = sub { vcrypto_op(@_, 1032); };
229 my $vpmsumd = sub { vcrypto_op(@_, 1224); };
230 my $vpmsubh = sub { vcrypto_op(@_, 1096); };
231 my $vpmsumw = sub { vcrypto_op(@_, 1160); };
232 my $vaddudm = sub { vcrypto_op(@_, 192); };
233
234 my $mtsle = sub {
235 my ($f, $arg) = @_;
236 " .long ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
237 };
238
239 # PowerISA 3.0 stuff
240 my $maddhdu = sub {
241 my ($f, $rt, $ra, $rb, $rc) = @_;
242 " .long ".sprintf "0x%X",(4<<26)|($rt<<21)|($ra<<16)|($rb<<11)|($rc<<6)|49;
243 };
244 my $maddld = sub {
245 my ($f, $rt, $ra, $rb, $rc) = @_;
246 " .long ".sprintf "0x%X",(4<<26)|($rt<<21)|($ra<<16)|($rb<<11)|($rc<<6)|51;
247 };
248
249 my $darn = sub {
250 my ($f, $rt, $l) = @_;
251 " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($l<<16)|(755<<1);
252 };
253
254 print <<___;
255 // This file is generated from a similarly-named Perl script in the BoringSSL
256 // source tree. Do not edit by hand.
257
258 #include <openssl/asm_base.h>
259
260 #if !defined(OPENSSL_NO_ASM) && defined(__powerpc64__) && defined(__ELF__)
261 #include <openssl/boringssl_prefix_symbols_asm.h>
262 ___
263
264 while($line=<>) {
265
266 $line =~ s|[#!;].*$||; # get rid of asm-style comments...
267 $line =~ s|/\*.*\*/||; # ... and C-style comments...
268 $line =~ s|^\s+||; # ... and skip white spaces in beginning...
269 $line =~ s|\s+$||; # ... and at the end
270
271 {
272 $line =~ s|\.L(\w+)|L$1|g; # common denominator for Locallabel
273 $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
274 }
275
276 {
277 $line =~ s|(^[\.\w]+)\:\s*||;
278 my $label = $1;
279 if ($label) {
280 my $xlated = ($GLOBALS{$label} or $label);
281 print "$xlated:";
282 if ($flavour =~ /linux.*64le/) {
283 if ($TYPES{$label} =~ /function/) {
284 printf "\n.localentry %s,0\n",$xlated;
285 }
286 }
287 }
288 }
289
290 {
291 $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
292 my $c = $1; $c = "\t" if ($c eq "");
293 my $mnemonic = $2;
294 my $f = $3;
295 my $opcode = eval("\$$mnemonic");
296 $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
297 if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
298 elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
299 }
300
301 print $line if ($line);
302 print "\n";
303 }
304
305 print <<___;
306 #endif // !OPENSSL_NO_ASM && __powerpc64__ && __ELF__
307 #if defined(__ELF__)
308 // See https://www.airs.com/blog/archives/518.
309 .section .note.GNU-stack,"",\%progbits
310 #endif
311 ___
312
313 close STDOUT or die "error closing STDOUT: $!";