ppc-xlate.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env perl
  2. # PowerPC assembler distiller by <appro>.
  3. my $output = shift;
  4. open STDOUT,">$output" || die "can't open $output: $!";
  5. my $flavour = $output;
  6. my %GLOBALS;
  7. my $dotinlocallabels=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. $dotinlocallabels = 1;
  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 .= ".globl $name\n";
  33. $ret .= ".align 3\n";
  34. $ret .= "$name:\n";
  35. $ret .= ".quad .$name,.TOC.\@tocbase,0\n";
  36. $ret .= ".size $name,24\n";
  37. $ret .= ".previous\n";
  38. $name = ".$name";
  39. $dotinlocallabels = 1;
  40. last;
  41. };
  42. }
  43. $ret = ".globl $name" if (!$ret);
  44. $$global = $name;
  45. $ret;
  46. };
  47. my $text = sub {
  48. ($flavour =~ /aix/) ? ".csect" : ".text";
  49. };
  50. my $machine = sub {
  51. my $junk = shift;
  52. my $arch = shift;
  53. if ($flavour =~ /osx/)
  54. { $arch =~ s/\"//g;
  55. $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
  56. }
  57. ".machine $arch";
  58. };
  59. ################################################################
  60. # simplified mnemonics not handled by at least one assembler
  61. ################################################################
  62. my $cmplw = sub {
  63. my $f = shift;
  64. my $cr = 0; $cr = shift if ($#_>1);
  65. # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
  66. ($flavour =~ /linux.*32/) ?
  67. " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
  68. " cmplw ".join(',',$cr,@_);
  69. };
  70. my $bdnz = sub {
  71. my $f = shift;
  72. my $bo = $f=~/[\+\-]/ ? 17 : 16;
  73. " bc $bo,0,".shift;
  74. };
  75. while($line=<>) {
  76. $line =~ s|[#!;].*$||; # get rid of asm-style comments...
  77. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  78. $line =~ s|^\s+||; # ... and skip white spaces in beginning...
  79. $line =~ s|\s+$||; # ... and at the end
  80. {
  81. $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel
  82. $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
  83. }
  84. {
  85. $line =~ s|(^[\.\w]+)\:\s*||;
  86. my $label = $1;
  87. printf "%s:",($GLOBALS{$label} or $label) if ($label);
  88. }
  89. {
  90. $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
  91. my $c = $1; $c = "\t" if ($c eq "");
  92. my $mnemonic = $2;
  93. my $f = $3;
  94. my $opcode = eval("\$$mnemonic");
  95. $line =~ s|\br([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/);
  96. if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
  97. elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
  98. }
  99. print $line if ($line);
  100. print "\n";
  101. }
  102. close STDOUT;