arm-xlate.pl 4.1 KB

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