2
0

ppc-xlate.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/ && do { $ret .= ".globl $name\n";
  25. $ret .= ".type $name,\@function";
  26. last;
  27. };
  28. /linux.*64/ && do { $ret .= ".globl $name\n";
  29. $ret .= ".type $name,\@function\n";
  30. $ret .= ".section \".opd\",\"aw\"\n";
  31. $ret .= ".align 3\n";
  32. $ret .= "$name:\n";
  33. $ret .= ".quad .$name,.TOC.\@tocbase,0\n";
  34. $ret .= ".size $name,24\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. ($flavour =~ /aix/) ? ".csect" : ".text";
  46. };
  47. my $machine = sub {
  48. my $junk = shift;
  49. my $arch = shift;
  50. if ($flavour =~ /osx/)
  51. { $arch =~ s/\"//g;
  52. $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
  53. }
  54. ".machine $arch";
  55. };
  56. my $size = sub {
  57. if ($flavour =~ /linux.*32/)
  58. { shift;
  59. ".size " . join(",",@_);
  60. }
  61. else
  62. { ""; }
  63. };
  64. my $asciz = sub {
  65. shift;
  66. my $line = join(",",@_);
  67. if ($line =~ /^"(.*)"$/)
  68. { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
  69. else
  70. { ""; }
  71. };
  72. ################################################################
  73. # simplified mnemonics not handled by at least one assembler
  74. ################################################################
  75. my $cmplw = sub {
  76. my $f = shift;
  77. my $cr = 0; $cr = shift if ($#_>1);
  78. # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
  79. ($flavour =~ /linux.*32/) ?
  80. " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
  81. " cmplw ".join(',',$cr,@_);
  82. };
  83. my $bdnz = sub {
  84. my $f = shift;
  85. my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
  86. " bc $bo,0,".shift;
  87. } if ($flavour!~/linux/);
  88. my $bltlr = sub {
  89. my $f = shift;
  90. my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
  91. ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
  92. " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
  93. " bclr $bo,0";
  94. };
  95. my $bnelr = sub {
  96. my $f = shift;
  97. my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
  98. ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
  99. " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
  100. " bclr $bo,2";
  101. };
  102. my $beqlr = sub {
  103. my $f = shift;
  104. my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
  105. ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
  106. " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
  107. " bclr $bo,2";
  108. };
  109. # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
  110. # arguments is 64, with "operand out of range" error.
  111. my $extrdi = sub {
  112. my ($f,$ra,$rs,$n,$b) = @_;
  113. $b = ($b+$n)&63; $n = 64-$n;
  114. " rldicl $ra,$rs,$b,$n";
  115. };
  116. while($line=<>) {
  117. $line =~ s|[#!;].*$||; # get rid of asm-style comments...
  118. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  119. $line =~ s|^\s+||; # ... and skip white spaces in beginning...
  120. $line =~ s|\s+$||; # ... and at the end
  121. {
  122. $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel
  123. $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
  124. }
  125. {
  126. $line =~ s|(^[\.\w]+)\:\s*||;
  127. my $label = $1;
  128. printf "%s:",($GLOBALS{$label} or $label) if ($label);
  129. }
  130. {
  131. $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
  132. my $c = $1; $c = "\t" if ($c eq "");
  133. my $mnemonic = $2;
  134. my $f = $3;
  135. my $opcode = eval("\$$mnemonic");
  136. $line =~ s|\bc?[rf]([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/);
  137. if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
  138. elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
  139. }
  140. print $line if ($line);
  141. print "\n";
  142. }
  143. close STDOUT;