2 # Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 open STDOUT,">$output" || die "can't open $output: $!";
15 my $dotinlocallabels=($flavour=~/linux/)?1:0;
17 ################################################################
18 # directives which need special treatment on different platforms
19 ################################################################
21 my ($dir,$name,$type) = @_;
23 $TYPES{$name} = $type;
24 if ($flavour =~ /linux/) {
34 my $global = \$GLOBALS{$name};
35 my $type = \$TYPES{$name};
40 SWITCH: for ($flavour) {
41 /aix/ && do { if (!$$type) {
42 $$type = "\@function";
44 if ($$type =~ /function/) {
49 /osx/ && do { $name = "_$name";
52 /linux.*(32|64(le|v2))/
53 && do { $ret .= ".globl $name";
55 $ret .= "\n.type $name,\@function";
56 $$type = "\@function";
60 /linux.*64/ && do { $ret .= ".globl $name";
62 $ret .= "\n.type $name,\@function";
63 $$type = "\@function";
65 if ($$type =~ /function/) {
66 $ret .= "\n.section \".opd\",\"aw\"";
69 $ret .= "\n.quad .$name,.TOC.\@tocbase,0";
70 $ret .= "\n.previous";
77 $ret = ".globl $name" if (!$ret);
82 my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
83 $ret = ".abiversion 2\n".$ret if ($flavour =~ /linux.*64(le|v2)/);
87 my $ret = ($flavour =~ /aix64-as/) ? "" : ".p2align $line";
93 if ($flavour =~ /osx/)
95 $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
100 if ($flavour =~ /linux/)
103 my $real = $GLOBALS{$name} ? \$GLOBALS{$name} : \$name;
104 my $ret = ".size $$real,.-$$real";
106 if ($$real ne $name) {
107 $ret .= "\n.size $name,.-$$real";
116 my $line = join(",",@_);
117 if ($line =~ /^"(.*)"$/)
118 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
127 if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
128 { $hi=$1?"0x$1":"0"; $lo="0x$2"; }
129 elsif (/^([0-9]+)$/o)
130 { $hi=$1>>32; $lo=$1&0xffffffff; } # error-prone with 32-bit perl
132 { $hi=undef; $lo=$_; }
135 { push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo"); }
137 { push(@ret,".quad $lo"); }
142 ################################################################
143 # vector register number hacking
144 ################################################################
146 # It is convenient to be able to set a variable like:
148 # and use this in different contexts where:
149 # * a VSR (Vector-Scaler Register) number (i.e. "v33") is required
150 # * a VR (Vector Register) number (i.e. "v1") is required
151 # Map VSR numbering to VR number for certain vector instructions.
153 # vs<N> -> v<N-32> if N > 32
156 my ($prefix, $reg) = ($in =~ m/(\D*)(\d+)/);
163 return "${prefix}${n}";
165 # As above for first $num register args, returns list
169 my @subst = splice(@rest, 0, $num);
171 @subst = map { vsr2vr1($_); } @subst;
173 return (@subst, @rest);
175 # As above but 1st arg ($f) is extracted and reinserted after
176 # processing so that it can be ignored by a code generation function
177 # that consumes the result
182 my @out = _vsr2vr($num, @_);
186 # As above but 1st arg is mnemonic, return formatted instruction
188 my $mnemonic = shift;
192 my @out = _vsr2vr($num, @_);
194 " ${mnemonic}${f} " . join(",", @out);
198 my $vsel = sub { vsr2vr("vsel", 4, @_); };
199 my $vsl = sub { vsr2vr("vsl", 3, @_); };
200 my $vspltisb = sub { vsr2vr("vspltisb", 1, @_); };
201 my $vspltisw = sub { vsr2vr("vspltisw", 1, @_); };
202 my $vsr = sub { vsr2vr("vsr", 3, @_); };
203 my $vsro = sub { vsr2vr("vsro", 3, @_); };
206 my $lxsd = sub { vsr2vr("lxsd", 1, @_); };
208 ################################################################
209 # simplified mnemonics not handled by at least one assembler
210 ################################################################
213 my $cr = 0; $cr = shift if ($#_>1);
214 # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
215 ($flavour =~ /linux.*32/) ?
216 " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
217 " cmplw ".join(',',$cr,@_);
221 my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
223 } if ($flavour!~/linux/);
226 my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
227 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
228 " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
233 my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
234 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
235 " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
240 my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
241 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
242 " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
245 # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
246 # arguments is 64, with "operand out of range" error.
248 my ($f,$ra,$rs,$n,$b) = @_;
249 $b = ($b+$n)&63; $n = 64-$n;
250 " rldicl $ra,$rs,$b,$n";
253 my ($f,$vx,$vy) = @_;
257 # Some ABIs specify vrsave, special-purpose register #256, as reserved
259 my $no_vrsave = ($flavour =~ /aix|linux64(le|v2)/);
261 my ($f,$idx,$ra) = @_;
262 if ($idx == 256 && $no_vrsave) {
269 my ($f,$rd,$idx) = @_;
270 if ($idx == 256 && $no_vrsave) {
277 # PowerISA 2.06 stuff
279 my ($f, $vrt, $ra, $rb, $op) = @_;
280 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
282 # made-up unaligned memory reference AltiVec/VMX instructions
283 my $lvx_u = sub { vsxmem_op(@_, 844); }; # lxvd2x
284 my $stvx_u = sub { vsxmem_op(@_, 972); }; # stxvd2x
285 my $lvdx_u = sub { vsxmem_op(@_, 588); }; # lxsdx
286 my $stvdx_u = sub { vsxmem_op(@_, 716); }; # stxsdx
287 my $lvx_4w = sub { vsxmem_op(@_, 780); }; # lxvw4x
288 my $stvx_4w = sub { vsxmem_op(@_, 908); }; # stxvw4x
289 my $lvx_splt = sub { vsxmem_op(@_, 332); }; # lxvdsx
290 # VSX instruction[s] masqueraded as made-up AltiVec/VMX
291 my $vpermdi = sub { # xxpermdi
292 my ($f, $vrt, $vra, $vrb, $dm) = @_;
293 $dm = oct($dm) if ($dm =~ /^0/);
294 " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($dm<<8)|(10<<3)|7;
296 my $vxxlor = sub { # xxlor
297 my ($f, $vrt, $vra, $vrb) = @_;
298 " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|(146<<3)|6;
300 my $vxxlorc = sub { # xxlor
301 my ($f, $vrt, $vra, $vrb) = @_;
302 " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|(146<<3)|1;
305 # PowerISA 2.07 stuff
307 my ($f, $vrt, $vra, $vrb, $op) = vsr2vr_args(3, @_);
308 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
311 my ($f, $vrt, $vra, $vrb, $vrc, $op) = @_;
312 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($vrc<<6)|$op;
315 my ($f, $vrt, $vra, $vrb, $vrc, $op) = vsr2vr_args(4, @_);
316 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($vrc<<6)|$op;
319 my $vcipher = sub { vcrypto_op(@_, 1288); };
320 my $vcipherlast = sub { vcrypto_op(@_, 1289); };
321 my $vncipher = sub { vcrypto_op(@_, 1352); };
322 my $vncipherlast= sub { vcrypto_op(@_, 1353); };
323 my $vsbox = sub { vcrypto_op(@_, 0, 1480); };
324 my $vshasigmad = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
325 my $vshasigmaw = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
326 my $vpmsumb = sub { vcrypto_op(@_, 1032); };
327 my $vpmsumd = sub { vcrypto_op(@_, 1224); };
328 my $vpmsubh = sub { vcrypto_op(@_, 1096); };
329 my $vpmsumw = sub { vcrypto_op(@_, 1160); };
330 # These are not really crypto, but vcrypto_op template works
331 my $vaddudm = sub { vcrypto_op(@_, 192); };
332 my $vadduqm = sub { vcrypto_op(@_, 256); };
333 my $vmuleuw = sub { vcrypto_op(@_, 648); };
334 my $vmulouw = sub { vcrypto_op(@_, 136); };
335 my $vrld = sub { vcrypto_op(@_, 196); };
336 my $vsld = sub { vcrypto_op(@_, 1476); };
337 my $vsrd = sub { vcrypto_op(@_, 1732); };
338 my $vsubudm = sub { vcrypto_op(@_, 1216); };
339 my $vaddcuq = sub { vcrypto_op(@_, 320); };
340 my $vaddeuqm = sub { vfour_vsr(@_,60); };
341 my $vaddecuq = sub { vfour_vsr(@_,61); };
342 my $vmrgew = sub { vfour_vsr(@_,0,1932); };
343 my $vmrgow = sub { vfour_vsr(@_,0,1676); };
347 " .long ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
350 # VSX instructions masqueraded as AltiVec/VMX
352 my ($f, $vrt, $ra) = @_;
353 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(179<<1)|1;
356 my ($f, $vrt, $ra) = @_;
357 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(243<<1)|1;
359 my $lvwzx_u = sub { vsxmem_op(@_, 12); }; # lxsiwzx
360 my $stvwx_u = sub { vsxmem_op(@_, 140); }; # stxsiwx
363 my $maddhdu = sub { vfour(@_,49); };
364 my $maddld = sub { vfour(@_,51); };
366 my ($f, $rt, $l) = @_;
367 " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($l<<16)|(755<<1);
370 my ($f, $rt, $ra, $rb) = @_;
371 " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|(2<<6)|30;
373 # VSX instruction[s] masqueraded as made-up AltiVec/VMX
374 my $vspltib = sub { # xxspltib
375 my ($f, $vrt, $imm8) = @_;
376 $imm8 = oct($imm8) if ($imm8 =~ /^0/);
378 " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($imm8<<11)|(360<<1)|1;
381 # PowerISA 3.0B stuff
383 my ($f, $rt, $ra, $rb, $cy) = @_; # only cy==0 is specified in 3.0B
384 " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|($cy<<9)|(170<<1);
386 my $vmsumudm = sub { vfour_vsr(@_, 35); };
390 my ($f, $ra, $rs) = @_;
391 " .long ".sprintf "0x%X",(31<<26)|($rs<<21)|($ra<<16)|(187<<1);
393 my $vsrq = sub { vcrypto_op(@_, 517); };
399 $line =~ s|[#!;].*$||; # get rid of asm-style comments...
400 $line =~ s|/\*.*\*/||; # ... and C-style comments...
401 $line =~ s|^\s+||; # ... and skip whitespaces in beginning...
402 $line =~ s|\s+$||; # ... and at the end
405 $line =~ s|\.L(\w+)|L$1|g; # common denominator for Locallabel
406 $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
410 $line =~ s|(^[\.\w]+)\:\s*||;
413 my $xlated = ($GLOBALS{$label} or $label);
415 if ($flavour =~ /linux.*64(le|v2)/) {
416 if ($TYPES{$label} =~ /function/) {
417 printf "\n.localentry %s,0\n",$xlated;
424 $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
425 my $c = $1; $c = "\t" if ($c eq "");
428 my $opcode = eval("\$$mnemonic");
429 $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
430 if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(/,\s*/,$line)); }
431 elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
434 print $line if ($line);
438 close STDOUT or die "error closing STDOUT: $!";