3 # PowerPC assembler distiller by <appro>.
7 open STDOUT,">$output" || die "can't open $output: $!";
10 my $dotinlocallabels=($flavour=~/linux/)?1:0;
12 ################################################################
13 # directives which need special treatment on different platforms
14 ################################################################
18 my $global = \$GLOBALS{$name};
23 SWITCH: for ($flavour) {
24 /aix/ && do { $name = ".$name";
27 /osx/ && do { $name = "_$name";
31 && do { $ret .= ".globl $name\n";
32 $ret .= ".type $name,\@function";
35 /linux.*64/ && do { $ret .= ".globl $name\n";
36 $ret .= ".type $name,\@function\n";
37 $ret .= ".section \".opd\",\"aw\"\n";
40 $ret .= ".quad .$name,.TOC.\@tocbase,0\n";
41 $ret .= ".previous\n";
48 $ret = ".globl $name" if (!$ret);
53 my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
54 $ret = ".abiversion 2\n".$ret if ($flavour =~ /linux.*64le/);
60 if ($flavour =~ /osx/)
62 $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
67 if ($flavour =~ /linux/)
69 my $name = shift; $name =~ s|^[\.\_]||;
70 my $ret = ".size $name,.-".($flavour=~/64$/?".":"").$name;
71 $ret .= "\n.size .$name,.-.$name" if ($flavour=~/64$/);
79 my $line = join(",",@_);
80 if ($line =~ /^"(.*)"$/)
81 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
90 if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
91 { $hi=$1?"0x$1":"0"; $lo="0x$2"; }
93 { $hi=$1>>32; $lo=$1&0xffffffff; } # error-prone with 32-bit perl
95 { $hi=undef; $lo=$_; }
98 { push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo"); }
100 { push(@ret,".quad $lo"); }
105 ################################################################
106 # simplified mnemonics not handled by at least one assembler
107 ################################################################
110 my $cr = 0; $cr = shift if ($#_>1);
111 # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
112 ($flavour =~ /linux.*32/) ?
113 " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
114 " cmplw ".join(',',$cr,@_);
118 my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
120 } if ($flavour!~/linux/);
123 my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
124 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
125 " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
130 my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
131 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
132 " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
137 my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
138 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
139 " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
142 # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
143 # arguments is 64, with "operand out of range" error.
145 my ($f,$ra,$rs,$n,$b) = @_;
146 $b = ($b+$n)&63; $n = 64-$n;
147 " rldicl $ra,$rs,$b,$n";
150 my ($f,$vx,$vy) = @_;
154 # Some ABIs specify vrsave, special-purpose register #256, as reserved
156 my $no_vrsave = ($flavour =~ /aix|linux64le/);
158 my ($f,$idx,$ra) = @_;
159 if ($idx == 256 && $no_vrsave) {
166 my ($f,$rd,$idx) = @_;
167 if ($idx == 256 && $no_vrsave) {
174 # PowerISA 2.06 stuff
176 my ($f, $vrt, $ra, $rb, $op) = @_;
177 " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
179 # made-up unaligned memory reference AltiVec/VMX instructions
180 my $lvx_u = sub { vsxmem_op(@_, 844); }; # lxvd2x
181 my $stvx_u = sub { vsxmem_op(@_, 972); }; # stxvd2x
182 my $lvdx_u = sub { vsxmem_op(@_, 588); }; # lxsdx
183 my $stvdx_u = sub { vsxmem_op(@_, 716); }; # stxsdx
184 my $lvx_4w = sub { vsxmem_op(@_, 780); }; # lxvw4x
185 my $stvx_4w = sub { vsxmem_op(@_, 908); }; # stxvw4x
187 # PowerISA 2.07 stuff
189 my ($f, $vrt, $vra, $vrb, $op) = @_;
190 " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
192 my $vcipher = sub { vcrypto_op(@_, 1288); };
193 my $vcipherlast = sub { vcrypto_op(@_, 1289); };
194 my $vncipher = sub { vcrypto_op(@_, 1352); };
195 my $vncipherlast= sub { vcrypto_op(@_, 1353); };
196 my $vsbox = sub { vcrypto_op(@_, 0, 1480); };
197 my $vshasigmad = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
198 my $vshasigmaw = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
199 my $vpmsumb = sub { vcrypto_op(@_, 1032); };
200 my $vpmsumd = sub { vcrypto_op(@_, 1224); };
201 my $vpmsubh = sub { vcrypto_op(@_, 1096); };
202 my $vpmsumw = sub { vcrypto_op(@_, 1160); };
203 my $vaddudm = sub { vcrypto_op(@_, 192); };
207 " .long ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
212 $line =~ s|[#!;].*$||; # get rid of asm-style comments...
213 $line =~ s|/\*.*\*/||; # ... and C-style comments...
214 $line =~ s|^\s+||; # ... and skip white spaces in beginning...
215 $line =~ s|\s+$||; # ... and at the end
218 $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel
219 $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
223 $line =~ s|(^[\.\w]+)\:\s*||;
226 printf "%s:",($GLOBALS{$label} or $label);
227 printf "\n.localentry\t$GLOBALS{$label},0" if ($GLOBALS{$label} && $flavour =~ /linux.*64le/);
232 $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
233 my $c = $1; $c = "\t" if ($c eq "");
236 my $opcode = eval("\$$mnemonic");
237 $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
238 if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
239 elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
242 print $line if ($line);