2
0

arm-xlate.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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 $rodata = sub {
  27. SWITCH: for ($flavour) {
  28. /linux/ && return ".section\t.rodata";
  29. /ios/ && return ".section\t__TEXT,__const";
  30. last;
  31. }
  32. };
  33. my $hidden = sub {
  34. if ($flavour =~ /ios/) { ".private_extern\t".join(',',@_); }
  35. else { ".hidden\t".join(',',@_); }
  36. };
  37. my $comm = sub {
  38. my @args = split(/,\s*/,shift);
  39. my $name = @args[0];
  40. my $global = \$GLOBALS{$name};
  41. my $ret;
  42. if ($flavour =~ /ios32/) {
  43. $ret = ".comm\t_$name,@args[1]\n";
  44. $ret .= ".non_lazy_symbol_pointer\n";
  45. $ret .= "$name:\n";
  46. $ret .= ".indirect_symbol\t_$name\n";
  47. $ret .= ".long\t0";
  48. $name = "_$name";
  49. } else { $ret = ".comm\t".join(',',@args); }
  50. $$global = $name;
  51. $ret;
  52. };
  53. my $globl = sub {
  54. my $name = shift;
  55. my $global = \$GLOBALS{$name};
  56. my $ret;
  57. SWITCH: for ($flavour) {
  58. /ios/ && do { $name = "_$name";
  59. last;
  60. };
  61. }
  62. $ret = ".globl $name" if (!$ret);
  63. $$global = $name;
  64. $ret;
  65. };
  66. my $global = $globl;
  67. my $extern = sub {
  68. &$globl(@_);
  69. return; # return nothing
  70. };
  71. my $type = sub {
  72. if ($flavour =~ /linux/) { ".type\t".join(',',@_); }
  73. elsif ($flavour =~ /ios32/) { if (join(',',@_) =~ /(\w+),%function/) {
  74. "#ifdef __thumb2__\n".
  75. ".thumb_func $1\n".
  76. "#endif";
  77. }
  78. }
  79. else { ""; }
  80. };
  81. my $size = sub {
  82. if ($flavour =~ /linux/) { ".size\t".join(',',@_); }
  83. else { ""; }
  84. };
  85. my $inst = sub {
  86. if ($flavour =~ /linux/) { ".inst\t".join(',',@_); }
  87. else { ".long\t".join(',',@_); }
  88. };
  89. my $asciz = sub {
  90. my $line = join(",",@_);
  91. if ($line =~ /^"(.*)"$/)
  92. { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
  93. else
  94. { ""; }
  95. };
  96. my $adrp = sub {
  97. my ($args,$comment) = split(m|\s*//|,shift);
  98. "\tadrp\t$args\@PAGE";
  99. } if ($flavour =~ /ios64/);
  100. sub range {
  101. my ($r,$sfx,$start,$end) = @_;
  102. join(",",map("$r$_$sfx",($start..$end)));
  103. }
  104. sub expand_line {
  105. my $line = shift;
  106. my @ret = ();
  107. pos($line)=0;
  108. while ($line =~ m/\G[^@\/\{\"]*/g) {
  109. if ($line =~ m/\G(@|\/\/|$)/gc) {
  110. last;
  111. }
  112. elsif ($line =~ m/\G\{/gc) {
  113. my $saved_pos = pos($line);
  114. $line =~ s/\G([rdqv])([0-9]+)([^\-]*)\-\1([0-9]+)\3/range($1,$3,$2,$4)/e;
  115. pos($line) = $saved_pos;
  116. $line =~ m/\G[^\}]*\}/g;
  117. }
  118. elsif ($line =~ m/\G\"/gc) {
  119. $line =~ m/\G[^\"]*\"/g;
  120. }
  121. }
  122. $line =~ s/\b(\w+)/$GLOBALS{$1} or $1/ge;
  123. if ($flavour =~ /ios64/) {
  124. $line =~ s/#:lo12:(\w+)/$1\@PAGEOFF/;
  125. }
  126. return $line;
  127. }
  128. while(my $line=<>) {
  129. if ($line =~ m/^\s*(#|@|\/\/)/) { print $line; next; }
  130. $line =~ s|/\*.*\*/||; # get rid of C-style comments...
  131. $line =~ s|^\s+||; # ... and skip whitespace in beginning...
  132. $line =~ s|\s+$||; # ... and at the end
  133. {
  134. $line =~ s|[\b\.]L(\w{2,})|L$1|g; # common denominator for Locallabel
  135. $line =~ s|\bL(\w{2,})|\.L$1|g if ($dotinlocallabels);
  136. }
  137. {
  138. $line =~ s|(^[\.\w]+)\:\s*||;
  139. my $label = $1;
  140. if ($label) {
  141. printf "%s:",($GLOBALS{$label} or $label);
  142. }
  143. }
  144. if ($line !~ m/^[#@]/) {
  145. $line =~ s|^\s*(\.?)(\S+)\s*||;
  146. my $c = $1; $c = "\t" if ($c eq "");
  147. my $mnemonic = $2;
  148. my $opcode;
  149. if ($mnemonic =~ m/([^\.]+)\.([^\.]+)/) {
  150. $opcode = eval("\$$1_$2");
  151. } else {
  152. $opcode = eval("\$$mnemonic");
  153. }
  154. my $arg=expand_line($line);
  155. if (ref($opcode) eq 'CODE') {
  156. $line = &$opcode($arg);
  157. } elsif ($mnemonic) {
  158. $line = $c.$mnemonic;
  159. $line.= "\t$arg" if ($arg ne "");
  160. }
  161. }
  162. print $line if ($line);
  163. print "\n";
  164. }
  165. close STDOUT or die "error closing STDOUT: $!";