arm2gnu.pl 8.7 KB

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