]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/perlasm/ppc-xlate.pl
Do not silently truncate files on perlasm errors
[thirdparty/openssl.git] / crypto / perlasm / ppc-xlate.pl
CommitLineData
e0a65194 1#! /usr/bin/env perl
95c81f8c 2# Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
e0a65194
RS
3#
4# Licensed under the OpenSSL license (the "License"). You may not use
5# this file except in compliance with the License. You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
2c5d4daa 8
addd641f 9my $flavour = shift;
2c5d4daa
AP
10my $output = shift;
11open STDOUT,">$output" || die "can't open $output: $!";
12
2c5d4daa 13my %GLOBALS;
b17ff188 14my %TYPES;
c6149e2f 15my $dotinlocallabels=($flavour=~/linux/)?1:0;
2c5d4daa
AP
16
17################################################################
18# directives which need special treatment on different platforms
19################################################################
b17ff188
AP
20my $type = sub {
21 my ($dir,$name,$type) = @_;
22
23 $TYPES{$name} = $type;
24 if ($flavour =~ /linux/) {
25 $name =~ s|^\.||;
26 ".type $name,$type";
27 } else {
28 "";
29 }
30};
2c5d4daa
AP
31my $globl = sub {
32 my $junk = shift;
33 my $name = shift;
34 my $global = \$GLOBALS{$name};
b17ff188 35 my $type = \$TYPES{$name};
2c5d4daa
AP
36 my $ret;
37
b17ff188 38 $name =~ s|^\.||;
609b0852 39
2c5d4daa 40 SWITCH: for ($flavour) {
b17ff188
AP
41 /aix/ && do { if (!$$type) {
42 $$type = "\@function";
43 }
44 if ($$type =~ /function/) {
45 $name = ".$name";
46 }
2c5d4daa
AP
47 last;
48 };
49 /osx/ && do { $name = "_$name";
50 last;
51 };
f586d971 52 /linux.*(32|64le)/
b17ff188
AP
53 && do { $ret .= ".globl $name";
54 if (!$$type) {
55 $ret .= "\n.type $name,\@function";
56 $$type = "\@function";
57 }
2c5d4daa
AP
58 last;
59 };
b17ff188
AP
60 /linux.*64/ && do { $ret .= ".globl $name";
61 if (!$$type) {
62 $ret .= "\n.type $name,\@function";
63 $$type = "\@function";
64 }
65 if ($$type =~ /function/) {
66 $ret .= "\n.section \".opd\",\"aw\"";
67 $ret .= "\n.align 3";
68 $ret .= "\n$name:";
69 $ret .= "\n.quad .$name,.TOC.\@tocbase,0";
70 $ret .= "\n.previous";
71 $name = ".$name";
72 }
2c5d4daa
AP
73 last;
74 };
75 }
76
77 $ret = ".globl $name" if (!$ret);
78 $$global = $name;
79 $ret;
80};
fe716ba6 81my $text = sub {
128e1d10 82 my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
f586d971
AP
83 $ret = ".abiversion 2\n".$ret if ($flavour =~ /linux.*64le/);
84 $ret;
fe716ba6 85};
2c5d4daa
AP
86my $machine = sub {
87 my $junk = shift;
88 my $arch = shift;
67d99090
AP
89 if ($flavour =~ /osx/)
90 { $arch =~ s/\"//g;
91 $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
92 }
2c5d4daa
AP
93 ".machine $arch";
94};
a3e07010 95my $size = sub {
d6019e16 96 if ($flavour =~ /linux/)
a3e07010 97 { shift;
b17ff188
AP
98 my $name = shift;
99 my $real = $GLOBALS{$name} ? \$GLOBALS{$name} : \$name;
100 my $ret = ".size $$real,.-$$real";
101 $name =~ s|^\.||;
102 if ($$real ne $name) {
103 $ret .= "\n.size $name,.-$$real";
104 }
d6019e16 105 $ret;
a3e07010
AP
106 }
107 else
108 { ""; }
109};
d68ff710
AP
110my $asciz = sub {
111 shift;
112 my $line = join(",",@_);
113 if ($line =~ /^"(.*)"$/)
287a9ee7 114 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
d68ff710
AP
115 else
116 { ""; }
117};
8ff8a829
AP
118my $quad = sub {
119 shift;
120 my @ret;
121 my ($hi,$lo);
122 for (@_) {
123 if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
124 { $hi=$1?"0x$1":"0"; $lo="0x$2"; }
125 elsif (/^([0-9]+)$/o)
126 { $hi=$1>>32; $lo=$1&0xffffffff; } # error-prone with 32-bit perl
127 else
128 { $hi=undef; $lo=$_; }
129
130 if (defined($hi))
0e0a1053 131 { push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo"); }
8ff8a829
AP
132 else
133 { push(@ret,".quad $lo"); }
134 }
135 join("\n",@ret);
136};
2c5d4daa
AP
137
138################################################################
139# simplified mnemonics not handled by at least one assembler
140################################################################
141my $cmplw = sub {
142 my $f = shift;
143 my $cr = 0; $cr = shift if ($#_>1);
67d99090
AP
144 # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
145 ($flavour =~ /linux.*32/) ?
146 " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
147 " cmplw ".join(',',$cr,@_);
2c5d4daa
AP
148};
149my $bdnz = sub {
150 my $f = shift;
0fcb905b 151 my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
2c5d4daa 152 " bc $bo,0,".shift;
0fcb905b
AP
153} if ($flavour!~/linux/);
154my $bltlr = sub {
155 my $f = shift;
156 my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
157 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
158 " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
159 " bclr $bo,0";
160};
161my $bnelr = sub {
162 my $f = shift;
163 my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
164 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
165 " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
166 " bclr $bo,2";
167};
7676eebf
AP
168my $beqlr = sub {
169 my $f = shift;
170 my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
171 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
172 " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
173 " bclr $bo,2";
174};
0fcb905b
AP
175# GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
176# arguments is 64, with "operand out of range" error.
177my $extrdi = sub {
178 my ($f,$ra,$rs,$n,$b) = @_;
179 $b = ($b+$n)&63; $n = 64-$n;
180 " rldicl $ra,$rs,$b,$n";
2c5d4daa 181};
26e18383
AP
182my $vmr = sub {
183 my ($f,$vx,$vy) = @_;
184 " vor $vx,$vy,$vy";
185};
2c5d4daa 186
b5516cfb
AP
187# Some ABIs specify vrsave, special-purpose register #256, as reserved
188# for system use.
189my $no_vrsave = ($flavour =~ /aix|linux64le/);
190my $mtspr = sub {
191 my ($f,$idx,$ra) = @_;
192 if ($idx == 256 && $no_vrsave) {
193 " or $ra,$ra,$ra";
194 } else {
195 " mtspr $idx,$ra";
196 }
197};
198my $mfspr = sub {
199 my ($f,$rd,$idx) = @_;
200 if ($idx == 256 && $no_vrsave) {
201 " li $rd,-1";
202 } else {
203 " mfspr $rd,$idx";
204 }
205};
206
f75faa16
AP
207# PowerISA 2.06 stuff
208sub vsxmem_op {
209 my ($f, $vrt, $ra, $rb, $op) = @_;
210 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
211}
212# made-up unaligned memory reference AltiVec/VMX instructions
213my $lvx_u = sub { vsxmem_op(@_, 844); }; # lxvd2x
214my $stvx_u = sub { vsxmem_op(@_, 972); }; # stxvd2x
215my $lvdx_u = sub { vsxmem_op(@_, 588); }; # lxsdx
216my $stvdx_u = sub { vsxmem_op(@_, 716); }; # stxsdx
c7ada16d
AP
217my $lvx_4w = sub { vsxmem_op(@_, 780); }; # lxvw4x
218my $stvx_4w = sub { vsxmem_op(@_, 908); }; # stxvw4x
150d0478 219my $lvx_splt = sub { vsxmem_op(@_, 332); }; # lxvdsx
95c81f8c 220# VSX instruction[s] masqueraded as made-up AltiVec/VMX
150d0478
AP
221my $vpermdi = sub { # xxpermdi
222 my ($f, $vrt, $vra, $vrb, $dm) = @_;
223 $dm = oct($dm) if ($dm =~ /^0/);
224 " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($dm<<8)|(10<<3)|7;
225};
f75faa16
AP
226
227# PowerISA 2.07 stuff
228sub vcrypto_op {
229 my ($f, $vrt, $vra, $vrb, $op) = @_;
230 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
231}
95c81f8c
AP
232sub vfour {
233 my ($f, $vrt, $vra, $vrb, $vrc, $op) = @_;
234 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($vrc<<6)|$op;
235};
f75faa16
AP
236my $vcipher = sub { vcrypto_op(@_, 1288); };
237my $vcipherlast = sub { vcrypto_op(@_, 1289); };
238my $vncipher = sub { vcrypto_op(@_, 1352); };
239my $vncipherlast= sub { vcrypto_op(@_, 1353); };
240my $vsbox = sub { vcrypto_op(@_, 0, 1480); };
241my $vshasigmad = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
242my $vshasigmaw = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
243my $vpmsumb = sub { vcrypto_op(@_, 1032); };
244my $vpmsumd = sub { vcrypto_op(@_, 1224); };
245my $vpmsubh = sub { vcrypto_op(@_, 1096); };
246my $vpmsumw = sub { vcrypto_op(@_, 1160); };
95c81f8c 247# These are not really crypto, but vcrypto_op template works
c7ada16d 248my $vaddudm = sub { vcrypto_op(@_, 192); };
150d0478
AP
249my $vadduqm = sub { vcrypto_op(@_, 256); };
250my $vmuleuw = sub { vcrypto_op(@_, 648); };
251my $vmulouw = sub { vcrypto_op(@_, 136); };
252my $vrld = sub { vcrypto_op(@_, 196); };
253my $vsld = sub { vcrypto_op(@_, 1476); };
254my $vsrd = sub { vcrypto_op(@_, 1732); };
95c81f8c
AP
255my $vsubudm = sub { vcrypto_op(@_, 1216); };
256my $vaddcuq = sub { vcrypto_op(@_, 320); };
257my $vaddeuqm = sub { vfour(@_,60); };
258my $vaddecuq = sub { vfour(@_,61); };
3f9c3b3c
AP
259my $vmrgew = sub { vfour(@_,0,1932); };
260my $vmrgow = sub { vfour(@_,0,1676); };
f75faa16
AP
261
262my $mtsle = sub {
263 my ($f, $arg) = @_;
264 " .long ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
265};
266
95c81f8c
AP
267# VSX instructions masqueraded as AltiVec/VMX
268my $mtvrd = sub {
269 my ($f, $vrt, $ra) = @_;
270 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(179<<1)|1;
e0e53282 271};
95c81f8c
AP
272my $mtvrwz = sub {
273 my ($f, $vrt, $ra) = @_;
274 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(243<<1)|1;
e0e53282 275};
95c81f8c
AP
276
277# PowerISA 3.0 stuff
278my $maddhdu = sub { vfour(@_,49); };
279my $maddld = sub { vfour(@_,51); };
e0e53282
AP
280my $darn = sub {
281 my ($f, $rt, $l) = @_;
282 " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($l<<16)|(755<<1);
283};
150d0478
AP
284my $iseleq = sub {
285 my ($f, $rt, $ra, $rb) = @_;
286 " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|(2<<6)|30;
287};
95c81f8c
AP
288# VSX instruction[s] masqueraded as made-up AltiVec/VMX
289my $vspltib = sub { # xxspltib
290 my ($f, $vrt, $imm8) = @_;
291 $imm8 = oct($imm8) if ($imm8 =~ /^0/);
292 $imm8 &= 0xff;
293 " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($imm8<<11)|(360<<1)|1;
294};
150d0478
AP
295
296# PowerISA 3.0B stuff
297my $addex = sub {
298 my ($f, $rt, $ra, $rb, $cy) = @_; # only cy==0 is specified in 3.0B
299 " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|($cy<<9)|(170<<1);
300};
95c81f8c 301my $vmsumudm = sub { vfour(@_,35); };
e0e53282 302
2c5d4daa
AP
303while($line=<>) {
304
305 $line =~ s|[#!;].*$||; # get rid of asm-style comments...
306 $line =~ s|/\*.*\*/||; # ... and C-style comments...
307 $line =~ s|^\s+||; # ... and skip white spaces in beginning...
308 $line =~ s|\s+$||; # ... and at the end
309
310 {
b17ff188 311 $line =~ s|\.L(\w+)|L$1|g; # common denominator for Locallabel
2c5d4daa
AP
312 $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
313 }
314
315 {
316 $line =~ s|(^[\.\w]+)\:\s*||;
317 my $label = $1;
f586d971 318 if ($label) {
b17ff188
AP
319 my $xlated = ($GLOBALS{$label} or $label);
320 print "$xlated:";
321 if ($flavour =~ /linux.*64le/) {
322 if ($TYPES{$label} =~ /function/) {
323 printf "\n.localentry %s,0\n",$xlated;
324 }
325 }
f586d971 326 }
2c5d4daa
AP
327 }
328
329 {
330 $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
331 my $c = $1; $c = "\t" if ($c eq "");
332 my $mnemonic = $2;
333 my $f = $3;
334 my $opcode = eval("\$$mnemonic");
26e18383 335 $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
150d0478 336 if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(/,\s*/,$line)); }
2c5d4daa
AP
337 elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
338 }
339
340 print $line if ($line);
341 print "\n";
342}
343
218e740f 344close STDOUT or die "error closing STDOUT: $!";