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";
30 /linux.*32/ && do { $ret .= ".globl $name\n";
31 $ret .= ".type $name,\@function";
34 /linux.*64/ && do { $ret .= ".globl $name\n";
35 $ret .= ".type $name,\@function\n";
36 $ret .= ".section \".opd\",\"aw\"\n";
39 $ret .= ".quad .$name,.TOC.\@tocbase,0\n";
40 $ret .= ".previous\n";
47 $ret = ".globl $name" if (!$ret);
52 ($flavour =~ /aix/) ? ".csect" : ".text";
57 if ($flavour =~ /osx/)
59 $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
64 if ($flavour =~ /linux/)
66 my $name = shift; $name =~ s|^[\.\_]||;
67 my $ret = ".size $name,.-".($flavour=~/64/?".":"").$name;
68 $ret .= "\n.size .$name,.-.$name" if ($flavour=~/64/);
76 my $line = join(",",@_);
77 if ($line =~ /^"(.*)"$/)
78 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
87 if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
88 { $hi=$1?"0x$1":"0"; $lo="0x$2"; }
90 { $hi=$1>>32; $lo=$1&0xffffffff; } # error-prone with 32-bit perl
92 { $hi=undef; $lo=$_; }
95 { push(@ret,$flavour=~/lei$/o?".long\t$lo,$hi":".long\t$hi,$lo"); }
97 { push(@ret,".quad $lo"); }
102 ################################################################
103 # simplified mnemonics not handled by at least one assembler
104 ################################################################
107 my $cr = 0; $cr = shift if ($#_>1);
108 # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
109 ($flavour =~ /linux.*32/) ?
110 " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
111 " cmplw ".join(',',$cr,@_);
115 my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
117 } if ($flavour!~/linux/);
120 my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
121 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
122 " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
127 my $bo = $f=~/\-/ ? 4+2 : 4; # 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|2<<16|16<<1 :
134 my $bo = $f=~/-/ ? 12+2 : 12; # 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 :
139 # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
140 # arguments is 64, with "operand out of range" error.
142 my ($f,$ra,$rs,$n,$b) = @_;
143 $b = ($b+$n)&63; $n = 64-$n;
144 " rldicl $ra,$rs,$b,$n";
149 $line =~ s|[#!;].*$||; # get rid of asm-style comments...
150 $line =~ s|/\*.*\*/||; # ... and C-style comments...
151 $line =~ s|^\s+||; # ... and skip white spaces in beginning...
152 $line =~ s|\s+$||; # ... and at the end
155 $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel
156 $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
160 $line =~ s|(^[\.\w]+)\:\s*||;
162 printf "%s:",($GLOBALS{$label} or $label) if ($label);
166 $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
167 my $c = $1; $c = "\t" if ($c eq "");
170 my $opcode = eval("\$$mnemonic");
171 $line =~ s|\bc?[rf]([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/);
172 if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
173 elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
176 print $line if ($line);