ppc-xlate.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. # PowerISA 2.06 stuff
  144. sub vsxmem_op {
  145. my ($f, $vrt, $ra, $rb, $op) = @_;
  146. " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
  147. }
  148. # made-up unaligned memory reference AltiVec/VMX instructions
  149. my $lvx_u = sub { vsxmem_op(@_, 844); }; # lxvd2x
  150. my $stvx_u = sub { vsxmem_op(@_, 972); }; # stxvd2x
  151. my $lvdx_u = sub { vsxmem_op(@_, 588); }; # lxsdx
  152. my $stvdx_u = sub { vsxmem_op(@_, 716); }; # stxsdx
  153. my $lvx_4w = sub { vsxmem_op(@_, 780); }; # lxvw4x
  154. my $stvx_4w = sub { vsxmem_op(@_, 908); }; # stxvw4x
  155. # PowerISA 2.07 stuff
  156. sub vcrypto_op {
  157. my ($f, $vrt, $vra, $vrb, $op) = @_;
  158. " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
  159. }
  160. my $vcipher = sub { vcrypto_op(@_, 1288); };
  161. my $vcipherlast = sub { vcrypto_op(@_, 1289); };
  162. my $vncipher = sub { vcrypto_op(@_, 1352); };
  163. my $vncipherlast= sub { vcrypto_op(@_, 1353); };
  164. my $vsbox = sub { vcrypto_op(@_, 0, 1480); };
  165. my $vshasigmad = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
  166. my $vshasigmaw = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
  167. my $vpmsumb = sub { vcrypto_op(@_, 1032); };
  168. my $vpmsumd = sub { vcrypto_op(@_, 1224); };
  169. my $vpmsubh = sub { vcrypto_op(@_, 1096); };
  170. my $vpmsumw = sub { vcrypto_op(@_, 1160); };
  171. my $vaddudm = sub { vcrypto_op(@_, 192); };
  172. my $mtsle = sub {
  173. my ($f, $arg) = @_;
  174. " .long ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
  175. };
  176. while($line=<>) {
  177. $line =~ s|[#!;].*$||; # get rid of asm-style comments...
  178. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  179. $line =~ s|^\s+||; # ... and skip white spaces in beginning...
  180. $line =~ s|\s+$||; # ... and at the end
  181. {
  182. $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel
  183. $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
  184. }
  185. {
  186. $line =~ s|(^[\.\w]+)\:\s*||;
  187. my $label = $1;
  188. if ($label) {
  189. printf "%s:",($GLOBALS{$label} or $label);
  190. printf "\n.localentry\t$GLOBALS{$label},0" if ($GLOBALS{$label} && $flavour =~ /linux.*64le/);
  191. }
  192. }
  193. {
  194. $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
  195. my $c = $1; $c = "\t" if ($c eq "");
  196. my $mnemonic = $2;
  197. my $f = $3;
  198. my $opcode = eval("\$$mnemonic");
  199. $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
  200. if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
  201. elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
  202. }
  203. print $line if ($line);
  204. print "\n";
  205. }
  206. close STDOUT;