ppc-xlate.pl 6.9 KB

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