Do not silently truncate files on perlasm errors
[openssl.git] / crypto / perlasm / ppc-xlate.pl
1 #! /usr/bin/env perl
2 # Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (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
8
9 my $flavour = shift;
10 my $output = shift;
11 open STDOUT,">$output" || die "can't open $output: $!";
12
13 my %GLOBALS;
14 my %TYPES;
15 my $dotinlocallabels=($flavour=~/linux/)?1:0;
16
17 ################################################################
18 # directives which need special treatment on different platforms
19 ################################################################
20 my $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 };
31 my $globl = sub {
32     my $junk = shift;
33     my $name = shift;
34     my $global = \$GLOBALS{$name};
35     my $type = \$TYPES{$name};
36     my $ret;
37
38     $name =~ s|^\.||;
39
40     SWITCH: for ($flavour) {
41         /aix/           && do { if (!$$type) {
42                                     $$type = "\@function";
43                                 }
44                                 if ($$type =~ /function/) {
45                                     $name = ".$name";
46                                 }
47                                 last;
48                               };
49         /osx/           && do { $name = "_$name";
50                                 last;
51                               };
52         /linux.*(32|64(le|v2))/
53                         && do { $ret .= ".globl $name";
54                                 if (!$$type) {
55                                     $ret .= "\n.type    $name,\@function";
56                                     $$type = "\@function";
57                                 }
58                                 last;
59                               };
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                                 }
73                                 last;
74                               };
75     }
76
77     $ret = ".globl      $name" if (!$ret);
78     $$global = $name;
79     $ret;
80 };
81 my $text = sub {
82     my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
83     $ret = ".abiversion 2\n".$ret       if ($flavour =~ /linux.*64(le|v2)/);
84     $ret;
85 };
86 my $machine = sub {
87     my $junk = shift;
88     my $arch = shift;
89     if ($flavour =~ /osx/)
90     {   $arch =~ s/\"//g;
91         $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
92     }
93     ".machine   $arch";
94 };
95 my $size = sub {
96     if ($flavour =~ /linux/)
97     {   shift;
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         }
105         $ret;
106     }
107     else
108     {   "";     }
109 };
110 my $asciz = sub {
111     shift;
112     my $line = join(",",@_);
113     if ($line =~ /^"(.*)"$/)
114     {   ".byte  " . join(",",unpack("C*",$1),0) . "\n.align     2";     }
115     else
116     {   "";     }
117 };
118 my $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))
131         {  push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo");  }
132         else
133         {  push(@ret,".quad     $lo");  }
134     }
135     join("\n",@ret);
136 };
137
138 ################################################################
139 # simplified mnemonics not handled by at least one assembler
140 ################################################################
141 my $cmplw = sub {
142     my $f = shift;
143     my $cr = 0; $cr = shift if ($#_>1);
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,@_);
148 };
149 my $bdnz = sub {
150     my $f = shift;
151     my $bo = $f=~/[\+\-]/ ? 16+9 : 16;  # optional "to be taken" hint
152     "   bc      $bo,0,".shift;
153 } if ($flavour!~/linux/);
154 my $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 };
161 my $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 };
168 my $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 };
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.
177 my $extrdi = sub {
178     my ($f,$ra,$rs,$n,$b) = @_;
179     $b = ($b+$n)&63; $n = 64-$n;
180     "   rldicl  $ra,$rs,$b,$n";
181 };
182 my $vmr = sub {
183     my ($f,$vx,$vy) = @_;
184     "   vor     $vx,$vy,$vy";
185 };
186
187 # Some ABIs specify vrsave, special-purpose register #256, as reserved
188 # for system use.
189 my $no_vrsave = ($flavour =~ /aix|linux64(le|v2)/);
190 my $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 };
198 my $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
207 # PowerISA 2.06 stuff
208 sub 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
213 my $lvx_u       = sub { vsxmem_op(@_, 844); };  # lxvd2x
214 my $stvx_u      = sub { vsxmem_op(@_, 972); };  # stxvd2x
215 my $lvdx_u      = sub { vsxmem_op(@_, 588); };  # lxsdx
216 my $stvdx_u     = sub { vsxmem_op(@_, 716); };  # stxsdx
217 my $lvx_4w      = sub { vsxmem_op(@_, 780); };  # lxvw4x
218 my $stvx_4w     = sub { vsxmem_op(@_, 908); };  # stxvw4x
219 my $lvx_splt    = sub { vsxmem_op(@_, 332); };  # lxvdsx
220 # VSX instruction[s] masqueraded as made-up AltiVec/VMX
221 my $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 };
226
227 # PowerISA 2.07 stuff
228 sub vcrypto_op {
229     my ($f, $vrt, $vra, $vrb, $op) = @_;
230     "   .long   ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
231 }
232 sub 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 };
236 my $vcipher     = sub { vcrypto_op(@_, 1288); };
237 my $vcipherlast = sub { vcrypto_op(@_, 1289); };
238 my $vncipher    = sub { vcrypto_op(@_, 1352); };
239 my $vncipherlast= sub { vcrypto_op(@_, 1353); };
240 my $vsbox       = sub { vcrypto_op(@_, 0, 1480); };
241 my $vshasigmad  = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
242 my $vshasigmaw  = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
243 my $vpmsumb     = sub { vcrypto_op(@_, 1032); };
244 my $vpmsumd     = sub { vcrypto_op(@_, 1224); };
245 my $vpmsubh     = sub { vcrypto_op(@_, 1096); };
246 my $vpmsumw     = sub { vcrypto_op(@_, 1160); };
247 # These are not really crypto, but vcrypto_op template works
248 my $vaddudm     = sub { vcrypto_op(@_, 192);  };
249 my $vadduqm     = sub { vcrypto_op(@_, 256);  };
250 my $vmuleuw     = sub { vcrypto_op(@_, 648);  };
251 my $vmulouw     = sub { vcrypto_op(@_, 136);  };
252 my $vrld        = sub { vcrypto_op(@_, 196);  };
253 my $vsld        = sub { vcrypto_op(@_, 1476); };
254 my $vsrd        = sub { vcrypto_op(@_, 1732); };
255 my $vsubudm     = sub { vcrypto_op(@_, 1216); };
256 my $vaddcuq     = sub { vcrypto_op(@_, 320);  };
257 my $vaddeuqm    = sub { vfour(@_,60); };
258 my $vaddecuq    = sub { vfour(@_,61); };
259 my $vmrgew      = sub { vfour(@_,0,1932); };
260 my $vmrgow      = sub { vfour(@_,0,1676); };
261
262 my $mtsle       = sub {
263     my ($f, $arg) = @_;
264     "   .long   ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
265 };
266
267 # VSX instructions masqueraded as AltiVec/VMX
268 my $mtvrd       = sub {
269     my ($f, $vrt, $ra) = @_;
270     "   .long   ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(179<<1)|1;
271 };
272 my $mtvrwz      = sub {
273     my ($f, $vrt, $ra) = @_;
274     "   .long   ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(243<<1)|1;
275 };
276 my $lvwzx_u     = sub { vsxmem_op(@_, 12); };   # lxsiwzx
277 my $stvwx_u     = sub { vsxmem_op(@_, 140); };  # stxsiwx
278
279 # PowerISA 3.0 stuff
280 my $maddhdu     = sub { vfour(@_,49); };
281 my $maddld      = sub { vfour(@_,51); };
282 my $darn = sub {
283     my ($f, $rt, $l) = @_;
284     "   .long   ".sprintf "0x%X",(31<<26)|($rt<<21)|($l<<16)|(755<<1);
285 };
286 my $iseleq = sub {
287     my ($f, $rt, $ra, $rb) = @_;
288     "   .long   ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|(2<<6)|30;
289 };
290 # VSX instruction[s] masqueraded as made-up AltiVec/VMX
291 my $vspltib     = sub {                         # xxspltib
292     my ($f, $vrt, $imm8) = @_;
293     $imm8 = oct($imm8) if ($imm8 =~ /^0/);
294     $imm8 &= 0xff;
295     "   .long   ".sprintf "0x%X",(60<<26)|($vrt<<21)|($imm8<<11)|(360<<1)|1;
296 };
297
298 # PowerISA 3.0B stuff
299 my $addex = sub {
300     my ($f, $rt, $ra, $rb, $cy) = @_;   # only cy==0 is specified in 3.0B
301     "   .long   ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|($cy<<9)|(170<<1);
302 };
303 my $vmsumudm    = sub { vfour(@_,35); };
304
305 while($line=<>) {
306
307     $line =~ s|[#!;].*$||;      # get rid of asm-style comments...
308     $line =~ s|/\*.*\*/||;      # ... and C-style comments...
309     $line =~ s|^\s+||;          # ... and skip white spaces in beginning...
310     $line =~ s|\s+$||;          # ... and at the end
311
312     {
313         $line =~ s|\.L(\w+)|L$1|g;      # common denominator for Locallabel
314         $line =~ s|\bL(\w+)|\.L$1|g     if ($dotinlocallabels);
315     }
316
317     {
318         $line =~ s|(^[\.\w]+)\:\s*||;
319         my $label = $1;
320         if ($label) {
321             my $xlated = ($GLOBALS{$label} or $label);
322             print "$xlated:";
323             if ($flavour =~ /linux.*64(le|v2)/) {
324                 if ($TYPES{$label} =~ /function/) {
325                     printf "\n.localentry       %s,0\n",$xlated;
326                 }
327             }
328         }
329     }
330
331     {
332         $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
333         my $c = $1; $c = "\t" if ($c eq "");
334         my $mnemonic = $2;
335         my $f = $3;
336         my $opcode = eval("\$$mnemonic");
337         $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
338         if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(/,\s*/,$line)); }
339         elsif ($mnemonic)           { $line = $c.$mnemonic.$f."\t".$line; }
340     }
341
342     print $line if ($line);
343     print "\n";
344 }
345
346 close STDOUT or die "error closing STDOUT";