2 # SPDX-License-Identifier: GPL-2.0
4 # PowerPC assembler distiller by <appro>.
8 open STDOUT,">$output" || die "can't open $output: $!";
11 my $dotinlocallabels=($flavour=~/linux/)?1:0;
12 my $elfv2abi=(($flavour =~ /linux-ppc64le/) or ($flavour =~ /linux-ppc64-elfv2/))?1:0;
13 my $dotfunctions=($elfv2abi=~1)?0:1;
15 ################################################################
16 # directives which need special treatment on different platforms
17 ################################################################
21 my $global = \$GLOBALS{$name};
26 SWITCH: for ($flavour) {
27 /aix/ && do { $name = ".$name";
30 /osx/ && do { $name = "_$name";
34 && do { $ret = "_GLOBAL($name)";
39 $ret = ".globl $name\nalign 5\n$name:" if (!$ret);
44 my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
45 $ret = ".abiversion 2\n".$ret if ($elfv2abi);
51 if ($flavour =~ /osx/)
53 $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
58 if ($flavour =~ /linux/)
60 my $name = shift; $name =~ s|^[\.\_]||;
61 my $ret = ".size $name,.-".($dotfunctions?".":"").$name;
62 $ret .= "\n.size .$name,.-.$name" if ($dotfunctions);
70 my $line = join(",",@_);
71 if ($line =~ /^"(.*)"$/)
72 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
81 if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
82 { $hi=$1?"0x$1":"0"; $lo="0x$2"; }
84 { $hi=$1>>32; $lo=$1&0xffffffff; } # error-prone with 32-bit perl
86 { $hi=undef; $lo=$_; }
89 { push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo"); }
91 { push(@ret,".quad $lo"); }
96 ################################################################
97 # simplified mnemonics not handled by at least one assembler
98 ################################################################
101 my $cr = 0; $cr = shift if ($#_>1);
102 # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
103 ($flavour =~ /linux.*32/) ?
104 " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
105 " cmplw ".join(',',$cr,@_);
109 my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
111 } if ($flavour!~/linux/);
114 my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
115 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
116 " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
121 my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
122 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
123 " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
128 my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
129 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
130 " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
133 # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
134 # arguments is 64, with "operand out of range" error.
136 my ($f,$ra,$rs,$n,$b) = @_;
137 $b = ($b+$n)&63; $n = 64-$n;
138 " rldicl $ra,$rs,$b,$n";
141 my ($f,$vx,$vy) = @_;
145 # Some ABIs specify vrsave, special-purpose register #256, as reserved
147 my $no_vrsave = ($elfv2abi);
149 my ($f,$idx,$ra) = @_;
150 if ($idx == 256 && $no_vrsave) {
157 my ($f,$rd,$idx) = @_;
158 if ($idx == 256 && $no_vrsave) {
165 # PowerISA 2.06 stuff
167 my ($f, $vrt, $ra, $rb, $op) = @_;
168 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
170 # made-up unaligned memory reference AltiVec/VMX instructions
171 my $lvx_u = sub { vsxmem_op(@_, 844); }; # lxvd2x
172 my $stvx_u = sub { vsxmem_op(@_, 972); }; # stxvd2x
173 my $lvdx_u = sub { vsxmem_op(@_, 588); }; # lxsdx
174 my $stvdx_u = sub { vsxmem_op(@_, 716); }; # stxsdx
175 my $lvx_4w = sub { vsxmem_op(@_, 780); }; # lxvw4x
176 my $stvx_4w = sub { vsxmem_op(@_, 908); }; # stxvw4x
178 # PowerISA 2.07 stuff
180 my ($f, $vrt, $vra, $vrb, $op) = @_;
181 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
183 my $vcipher = sub { vcrypto_op(@_, 1288); };
184 my $vcipherlast = sub { vcrypto_op(@_, 1289); };
185 my $vncipher = sub { vcrypto_op(@_, 1352); };
186 my $vncipherlast= sub { vcrypto_op(@_, 1353); };
187 my $vsbox = sub { vcrypto_op(@_, 0, 1480); };
188 my $vshasigmad = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
189 my $vshasigmaw = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
190 my $vpmsumb = sub { vcrypto_op(@_, 1032); };
191 my $vpmsumd = sub { vcrypto_op(@_, 1224); };
192 my $vpmsubh = sub { vcrypto_op(@_, 1096); };
193 my $vpmsumw = sub { vcrypto_op(@_, 1160); };
194 my $vaddudm = sub { vcrypto_op(@_, 192); };
195 my $vadduqm = sub { vcrypto_op(@_, 256); };
199 " .long ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
202 print "#include <asm/ppc_asm.h>\n" if $flavour =~ /linux/;
206 $line =~ s|[#!;].*$||; # get rid of asm-style comments...
207 $line =~ s|/\*.*\*/||; # ... and C-style comments...
208 $line =~ s|^\s+||; # ... and skip white spaces in beginning...
209 $line =~ s|\s+$||; # ... and at the end
212 $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel
213 $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
217 $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
218 my $c = $1; $c = "\t" if ($c eq "");
221 my $opcode = eval("\$$mnemonic");
222 $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
223 if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
224 elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
227 print $line if ($line);