ppc-xlate.pl 6.4 KB

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