arm2gnu.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #!/usr/bin/perl
  2. # Copyright (C) 2002-2013 Xiph.org Foundation
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. #
  8. # - Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. #
  11. # - Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  19. # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. my $bigend; # little/big endian
  27. my $nxstack;
  28. my $apple = 0;
  29. my $symprefix = "";
  30. $nxstack = 0;
  31. eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
  32. if $running_under_some_shell;
  33. while ($ARGV[0] =~ /^-/) {
  34. $_ = shift;
  35. last if /^--$/;
  36. if (/^-n$/) {
  37. $nflag++;
  38. next;
  39. }
  40. if (/^--apple$/) {
  41. $apple = 1;
  42. $symprefix = "_";
  43. next;
  44. }
  45. die "I don't recognize this switch: $_\\n";
  46. }
  47. $printit++ unless $nflag;
  48. $\ = "\n"; # automatically add newline on print
  49. $n=0;
  50. $thumb = 0; # ARM mode by default, not Thumb.
  51. @proc_stack = ();
  52. printf (" .syntax unified\n");
  53. LINE:
  54. while (<>) {
  55. # For ADRLs we need to add a new line after the substituted one.
  56. $addPadding = 0;
  57. # First, we do not dare to touch *anything* inside double quotes, do we?
  58. # Second, if you want a dollar character in the string,
  59. # insert two of them -- that's how ARM C and assembler treat strings.
  60. s/^([A-Za-z_]\w*)[ \t]+DCB[ \t]*\"/$1: .ascii \"/ && do { s/\$\$/\$/g; next };
  61. s/\bDCB\b[ \t]*\"/.ascii \"/ && do { s/\$\$/\$/g; next };
  62. s/^(\S+)\s+RN\s+(\S+)/$1 .req r$2/ && do { s/\$\$/\$/g; next };
  63. # If there's nothing on a line but a comment, don't try to apply any further
  64. # substitutions (this is a cheap hack to avoid mucking up the license header)
  65. s/^([ \t]*);/$1@/ && do { s/\$\$/\$/g; next };
  66. # If substituted -- leave immediately !
  67. s/@/,:/;
  68. s/;/@/;
  69. while ( /@.*'/ ) {
  70. s/(@.*)'/$1/g;
  71. }
  72. s/\{FALSE\}/0/g;
  73. s/\{TRUE\}/1/g;
  74. s/\{(\w\w\w\w+)\}/$1/g;
  75. s/\bINCLUDE[ \t]*([^ \t\n]+)/.include \"$1\"/;
  76. s/\bGET[ \t]*([^ \t\n]+)/.include \"${ my $x=$1; $x =~ s|\.s|-gnu.S|; \$x }\"/;
  77. s/\bIMPORT\b/.extern/;
  78. s/\bEXPORT\b\s*/.global $symprefix/;
  79. s/^(\s+)\[/$1IF/;
  80. s/^(\s+)\|/$1ELSE/;
  81. s/^(\s+)\]/$1ENDIF/;
  82. s/IF *:DEF:/ .ifdef/;
  83. s/IF *:LNOT: *:DEF:/ .ifndef/;
  84. s/ELSE/ .else/;
  85. s/ENDIF/ .endif/;
  86. if( /\bIF\b/ ) {
  87. s/\bIF\b/ .if/;
  88. s/=/==/;
  89. }
  90. if ( $n == 2) {
  91. s/\$/\\/g;
  92. }
  93. if ($n == 1) {
  94. s/\$//g;
  95. s/label//g;
  96. $n = 2;
  97. }
  98. if ( /MACRO/ ) {
  99. s/MACRO *\n/.macro/;
  100. $n=1;
  101. }
  102. if ( /\bMEND\b/ ) {
  103. s/\bMEND\b/.endm/;
  104. $n=0;
  105. }
  106. # ".rdata" doesn't work in 'as' version 2.13.2, as it is ".rodata" there.
  107. #
  108. if ( /\bAREA\b/ ) {
  109. my $align;
  110. $align = "2";
  111. if ( /ALIGN=(\d+)/ ) {
  112. $align = $1;
  113. }
  114. if ( /CODE/ ) {
  115. $nxstack = 1;
  116. }
  117. s/^(.+)CODE(.+)READONLY(.*)/ .text/;
  118. s/^(.+)DATA(.+)READONLY(.*)/ .section .rdata/;
  119. s/^(.+)\|\|\.data\|\|(.+)/ .data/;
  120. s/^(.+)\|\|\.bss\|\|(.+)/ .bss/;
  121. s/$/; .p2align $align/;
  122. # Enable NEON instructions but don't produce a binary that requires
  123. # ARMv7. RVCT does not have equivalent directives, so we just do this
  124. # for all CODE areas.
  125. if ( /.text/ ) {
  126. # Separating .arch, .fpu, etc., by semicolons does not work (gas
  127. # thinks the semicolon is part of the arch name, even when there's
  128. # whitespace separating them). Sadly this means our line numbers
  129. # won't match the original source file (we could use the .line
  130. # directive, which is documented to be obsolete, but then gdb will
  131. # show the wrong line in the translated source file).
  132. s/$/; .arch armv7-a\n .fpu neon\n .object_arch armv4t/ unless ($apple);
  133. }
  134. }
  135. s/\|\|\.constdata\$(\d+)\|\|/.L_CONST$1/; # ||.constdata$3||
  136. s/\|\|\.bss\$(\d+)\|\|/.L_BSS$1/; # ||.bss$2||
  137. s/\|\|\.data\$(\d+)\|\|/.L_DATA$1/; # ||.data$2||
  138. s/\|\|([a-zA-Z0-9_]+)\@([a-zA-Z0-9_]+)\|\|/@ $&/;
  139. s/^(\s+)\%(\s)/ .space $1/;
  140. s/\|(.+)\.(\d+)\|/\.$1_$2/; # |L80.123| -> .L80_123
  141. s/\bCODE32\b/.code 32/ && do {$thumb = 0};
  142. s/\bCODE16\b/.code 16/ && do {$thumb = 1};
  143. if (/\bPROC\b/)
  144. {
  145. my $prefix;
  146. my $proc;
  147. /^([A-Za-z_\.]\w+)\b/;
  148. $proc = $1;
  149. $prefix = "";
  150. if ($proc)
  151. {
  152. $prefix = $prefix.sprintf("\t.type\t%s, %%function", $proc) unless ($apple);
  153. # Make sure we $prefix isn't empty here (for the $apple case).
  154. # We handle mangling the label here, make sure it doesn't match
  155. # the label handling below (if $prefix would be empty).
  156. $prefix = $prefix."; ";
  157. push(@proc_stack, $proc);
  158. s/^[A-Za-z_\.]\w+/$symprefix$&:/;
  159. }
  160. $prefix = $prefix."\t.thumb_func; " if ($thumb);
  161. s/\bPROC\b/@ $&/;
  162. $_ = $prefix.$_;
  163. }
  164. s/^(\s*)(S|Q|SH|U|UQ|UH)ASX\b/$1$2ADDSUBX/;
  165. s/^(\s*)(S|Q|SH|U|UQ|UH)SAX\b/$1$2SUBADDX/;
  166. if (/\bENDP\b/)
  167. {
  168. my $proc;
  169. s/\bENDP\b/@ $&/;
  170. $proc = pop(@proc_stack);
  171. $_ = "\t.size $proc, .-$proc".$_ if ($proc && !$apple);
  172. }
  173. s/\bSUBT\b/@ $&/;
  174. s/\bDATA\b/@ $&/; # DATA directive is deprecated -- Asm guide, p.7-25
  175. s/\bKEEP\b/@ $&/;
  176. s/\bEXPORTAS\b/@ $&/;
  177. s/\|\|(.)+\bEQU\b/@ $&/;
  178. s/\|\|([\w\$]+)\|\|/$1/;
  179. s/\bENTRY\b/@ $&/;
  180. s/\bASSERT\b/@ $&/;
  181. s/\bGBLL\b/@ $&/;
  182. s/\bGBLA\b/@ $&/;
  183. s/^\W+OPT\b/@ $&/;
  184. s/:OR:/|/g;
  185. s/:SHL:/<</g;
  186. s/:SHR:/>>/g;
  187. s/:AND:/&/g;
  188. s/:LAND:/&&/g;
  189. s/CPSR/cpsr/;
  190. s/SPSR/spsr/;
  191. s/ALIGN$/.balign 4/;
  192. s/ALIGN\s+([0-9x]+)$/.balign $1/;
  193. s/psr_cxsf/psr_all/;
  194. s/LTORG/.ltorg/;
  195. s/^([A-Za-z_]\w*)[ \t]+EQU/ .set $1,/;
  196. s/^([A-Za-z_]\w*)[ \t]+SETL/ .set $1,/;
  197. s/^([A-Za-z_]\w*)[ \t]+SETA/ .set $1,/;
  198. s/^([A-Za-z_]\w*)[ \t]+\*/ .set $1,/;
  199. # {PC} + 0xdeadfeed --> . + 0xdeadfeed
  200. s/\{PC\} \+/ \. +/;
  201. # Single hex constant on the line !
  202. #
  203. # >>> NOTE <<<
  204. # Double-precision floats in gcc are always mixed-endian, which means
  205. # bytes in two words are little-endian, but words are big-endian.
  206. # So, 0x0000deadfeed0000 would be stored as 0x0000dead at low address
  207. # and 0xfeed0000 at high address.
  208. #
  209. s/\bDCFD\b[ \t]+0x([a-fA-F0-9]{8})([a-fA-F0-9]{8})/.long 0x$1, 0x$2/;
  210. # Only decimal constants on the line, no hex !
  211. s/\bDCFD\b[ \t]+([0-9\.\-]+)/.double $1/;
  212. # Single hex constant on the line !
  213. # s/\bDCFS\b[ \t]+0x([a-f0-9]{8})([a-f0-9]{8})/.long 0x$1, 0x$2/;
  214. # Only decimal constants on the line, no hex !
  215. # s/\bDCFS\b[ \t]+([0-9\.\-]+)/.double $1/;
  216. s/\bDCFS[ \t]+0x/.word 0x/;
  217. s/\bDCFS\b/.float/;
  218. s/^([A-Za-z_]\w*)[ \t]+DCD/$1 .word/;
  219. s/\bDCD\b/.word/;
  220. s/^([A-Za-z_]\w*)[ \t]+DCW/$1 .short/;
  221. s/\bDCW\b/.short/;
  222. s/^([A-Za-z_]\w*)[ \t]+DCB/$1 .byte/;
  223. s/\bDCB\b/.byte/;
  224. s/^([A-Za-z_]\w*)[ \t]+\%/.comm $1,/;
  225. s/^[A-Za-z_\.]\w+/$&:/;
  226. s/^(\d+)/$1:/;
  227. s/\%(\d+)/$1b_or_f/;
  228. s/\%[Bb](\d+)/$1b/;
  229. s/\%[Ff](\d+)/$1f/;
  230. s/\%[Ff][Tt](\d+)/$1f/;
  231. s/&([\dA-Fa-f]+)/0x$1/;
  232. if ( /\b2_[01]+\b/ ) {
  233. s/\b2_([01]+)\b/conv$1&&&&/g;
  234. while ( /[01][01][01][01]&&&&/ ) {
  235. s/0000&&&&/&&&&0/g;
  236. s/0001&&&&/&&&&1/g;
  237. s/0010&&&&/&&&&2/g;
  238. s/0011&&&&/&&&&3/g;
  239. s/0100&&&&/&&&&4/g;
  240. s/0101&&&&/&&&&5/g;
  241. s/0110&&&&/&&&&6/g;
  242. s/0111&&&&/&&&&7/g;
  243. s/1000&&&&/&&&&8/g;
  244. s/1001&&&&/&&&&9/g;
  245. s/1010&&&&/&&&&A/g;
  246. s/1011&&&&/&&&&B/g;
  247. s/1100&&&&/&&&&C/g;
  248. s/1101&&&&/&&&&D/g;
  249. s/1110&&&&/&&&&E/g;
  250. s/1111&&&&/&&&&F/g;
  251. }
  252. s/000&&&&/&&&&0/g;
  253. s/001&&&&/&&&&1/g;
  254. s/010&&&&/&&&&2/g;
  255. s/011&&&&/&&&&3/g;
  256. s/100&&&&/&&&&4/g;
  257. s/101&&&&/&&&&5/g;
  258. s/110&&&&/&&&&6/g;
  259. s/111&&&&/&&&&7/g;
  260. s/00&&&&/&&&&0/g;
  261. s/01&&&&/&&&&1/g;
  262. s/10&&&&/&&&&2/g;
  263. s/11&&&&/&&&&3/g;
  264. s/0&&&&/&&&&0/g;
  265. s/1&&&&/&&&&1/g;
  266. s/conv&&&&/0x/g;
  267. }
  268. if ( /commandline/)
  269. {
  270. if( /-bigend/)
  271. {
  272. $bigend=1;
  273. }
  274. }
  275. if ( /\bDCDU\b/ )
  276. {
  277. my $cmd=$_;
  278. my $value;
  279. my $prefix;
  280. my $w1;
  281. my $w2;
  282. my $w3;
  283. my $w4;
  284. s/\s+DCDU\b/@ $&/;
  285. $cmd =~ /\bDCDU\b\s+0x(\d+)/;
  286. $value = $1;
  287. $value =~ /(\w\w)(\w\w)(\w\w)(\w\w)/;
  288. $w1 = $1;
  289. $w2 = $2;
  290. $w3 = $3;
  291. $w4 = $4;
  292. if( $bigend ne "")
  293. {
  294. # big endian
  295. $prefix = "\t.byte\t0x".$w1.";".
  296. "\t.byte\t0x".$w2.";".
  297. "\t.byte\t0x".$w3.";".
  298. "\t.byte\t0x".$w4."; ";
  299. }
  300. else
  301. {
  302. # little endian
  303. $prefix = "\t.byte\t0x".$w4.";".
  304. "\t.byte\t0x".$w3.";".
  305. "\t.byte\t0x".$w2.";".
  306. "\t.byte\t0x".$w1."; ";
  307. }
  308. $_=$prefix.$_;
  309. }
  310. if ( /\badrl\b/i )
  311. {
  312. s/\badrl\s+(\w+)\s*,\s*(\w+)/ldr $1,=$2/i;
  313. $addPadding = 1;
  314. }
  315. s/\bEND\b/@ END/;
  316. } continue {
  317. printf ("%s", $_) if $printit;
  318. if ($addPadding != 0)
  319. {
  320. printf (" mov r0,r0\n");
  321. $addPadding = 0;
  322. }
  323. }
  324. #If we had a code section, mark that this object doesn't need an executable
  325. # stack.
  326. if ($nxstack && !$apple) {
  327. printf (" .section\t.note.GNU-stack,\"\",\%\%progbits\n");
  328. }