Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

175 řádky
3.9 KiB

  1. #!/usr/bin/env perl
  2. # ARM assembler distiller by <appro>.
  3. use strict;
  4. my $flavour = shift;
  5. my $output = shift;
  6. open STDOUT,">$output" || die "can't open $output: $!";
  7. $flavour = "linux32" if (!$flavour or $flavour eq "void");
  8. my %GLOBALS;
  9. my $dotinlocallabels=($flavour=~/linux/)?1:0;
  10. ################################################################
  11. # directives which need special treatment on different platforms
  12. ################################################################
  13. my $arch = sub {
  14. if ($flavour =~ /linux/) { ".arch\t".join(',',@_); }
  15. else { ""; }
  16. };
  17. my $fpu = sub {
  18. if ($flavour =~ /linux/) { ".fpu\t".join(',',@_); }
  19. else { ""; }
  20. };
  21. my $hidden = sub {
  22. if ($flavour =~ /ios/) { ".private_extern\t".join(',',@_); }
  23. else { ".hidden\t".join(',',@_); }
  24. };
  25. my $comm = sub {
  26. my @args = split(/,\s*/,shift);
  27. my $name = @args[0];
  28. my $global = \$GLOBALS{$name};
  29. my $ret;
  30. if ($flavour =~ /ios32/) {
  31. $ret = ".comm\t_$name,@args[1]\n";
  32. $ret .= ".non_lazy_symbol_pointer\n";
  33. $ret .= "$name:\n";
  34. $ret .= ".indirect_symbol\t_$name\n";
  35. $ret .= ".long\t0";
  36. $name = "_$name";
  37. } else { $ret = ".comm\t".join(',',@args); }
  38. $$global = $name;
  39. $ret;
  40. };
  41. my $globl = sub {
  42. my $name = shift;
  43. my $global = \$GLOBALS{$name};
  44. my $ret;
  45. SWITCH: for ($flavour) {
  46. /ios/ && do { $name = "_$name";
  47. last;
  48. };
  49. }
  50. $ret = ".globl $name\n";
  51. # All symbols in assembly files are hidden.
  52. $ret .= &$hidden($name);
  53. $$global = $name;
  54. $ret;
  55. };
  56. my $global = $globl;
  57. my $extern = sub {
  58. &$globl(@_);
  59. return; # return nothing
  60. };
  61. my $type = sub {
  62. if ($flavour =~ /linux/) { ".type\t".join(',',@_); }
  63. else { ""; }
  64. };
  65. my $size = sub {
  66. if ($flavour =~ /linux/) { ".size\t".join(',',@_); }
  67. else { ""; }
  68. };
  69. my $inst = sub {
  70. if ($flavour =~ /linux/) { ".inst\t".join(',',@_); }
  71. else { ".long\t".join(',',@_); }
  72. };
  73. my $asciz = sub {
  74. my $line = join(",",@_);
  75. if ($line =~ /^"(.*)"$/)
  76. { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
  77. else
  78. { ""; }
  79. };
  80. sub range {
  81. my ($r,$sfx,$start,$end) = @_;
  82. join(",",map("$r$_$sfx",($start..$end)));
  83. }
  84. sub expand_line {
  85. my $line = shift;
  86. my @ret = ();
  87. pos($line)=0;
  88. while ($line =~ m/\G[^@\/\{\"]*/g) {
  89. if ($line =~ m/\G(@|\/\/|$)/gc) {
  90. last;
  91. }
  92. elsif ($line =~ m/\G\{/gc) {
  93. my $saved_pos = pos($line);
  94. $line =~ s/\G([rdqv])([0-9]+)([^\-]*)\-\1([0-9]+)\3/range($1,$3,$2,$4)/e;
  95. pos($line) = $saved_pos;
  96. $line =~ m/\G[^\}]*\}/g;
  97. }
  98. elsif ($line =~ m/\G\"/gc) {
  99. $line =~ m/\G[^\"]*\"/g;
  100. }
  101. }
  102. $line =~ s/\b(\w+)/$GLOBALS{$1} or $1/ge;
  103. return $line;
  104. }
  105. print "#if defined(__arm__)\n" if ($flavour eq "linux32");
  106. print "#if defined(__aarch64__)\n" if ($flavour eq "linux64");
  107. while(my $line=<>) {
  108. if ($line =~ m/^\s*(#|@|\/\/)/) { print $line; next; }
  109. $line =~ s|/\*.*\*/||; # get rid of C-style comments...
  110. $line =~ s|^\s+||; # ... and skip white spaces in beginning...
  111. $line =~ s|\s+$||; # ... and at the end
  112. {
  113. $line =~ s|[\b\.]L(\w{2,})|L$1|g; # common denominator for Locallabel
  114. $line =~ s|\bL(\w{2,})|\.L$1|g if ($dotinlocallabels);
  115. }
  116. {
  117. $line =~ s|(^[\.\w]+)\:\s*||;
  118. my $label = $1;
  119. if ($label) {
  120. printf "%s:",($GLOBALS{$label} or $label);
  121. }
  122. }
  123. if ($line !~ m/^[#@]/) {
  124. $line =~ s|^\s*(\.?)(\S+)\s*||;
  125. my $c = $1; $c = "\t" if ($c eq "");
  126. my $mnemonic = $2;
  127. my $opcode;
  128. if ($mnemonic =~ m/([^\.]+)\.([^\.]+)/) {
  129. $opcode = eval("\$$1_$2");
  130. } else {
  131. $opcode = eval("\$$mnemonic");
  132. }
  133. my $arg=expand_line($line);
  134. if (ref($opcode) eq 'CODE') {
  135. $line = &$opcode($arg);
  136. } elsif ($mnemonic) {
  137. $line = $c.$mnemonic;
  138. $line.= "\t$arg" if ($arg ne "");
  139. }
  140. }
  141. print $line if ($line);
  142. print "\n";
  143. }
  144. print "#endif\n" if ($flavour eq "linux32" || $flavour eq "linux64");
  145. close STDOUT;