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