arm2gnu.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. }
  91. s/\|\|\.constdata\$(\d+)\|\|/.L_CONST$1/; # ||.constdata$3||
  92. s/\|\|\.bss\$(\d+)\|\|/.L_BSS$1/; # ||.bss$2||
  93. s/\|\|\.data\$(\d+)\|\|/.L_DATA$1/; # ||.data$2||
  94. s/\|\|([a-zA-Z0-9_]+)\@([a-zA-Z0-9_]+)\|\|/@ $&/;
  95. s/^(\s+)\%(\s)/ .space $1/;
  96. s/\|(.+)\.(\d+)\|/\.$1_$2/; # |L80.123| -> .L80_123
  97. s/\bCODE32\b/.code 32/ && do {$thumb = 0};
  98. s/\bCODE16\b/.code 16/ && do {$thumb = 1};
  99. if (/\bPROC\b/)
  100. {
  101. my $prefix;
  102. my $proc;
  103. /^([A-Za-z_\.]\w+)\b/;
  104. $proc = $1;
  105. $prefix = "";
  106. if ($proc)
  107. {
  108. $prefix = $prefix.sprintf("\t.type\t%s, %%function; ",$proc);
  109. push(@proc_stack, $proc);
  110. s/^[A-Za-z_\.]\w+/$&:/;
  111. }
  112. $prefix = $prefix."\t.thumb_func; " if ($thumb);
  113. s/\bPROC\b/@ $&/;
  114. $_ = $prefix.$_;
  115. }
  116. s/^(\s*)(S|Q|SH|U|UQ|UH)ASX\b/$1$2ADDSUBX/;
  117. s/^(\s*)(S|Q|SH|U|UQ|UH)SAX\b/$1$2SUBADDX/;
  118. if (/\bENDP\b/)
  119. {
  120. my $proc;
  121. s/\bENDP\b/@ $&/;
  122. $proc = pop(@proc_stack);
  123. $_ = "\t.size $proc, .-$proc".$_ if ($proc);
  124. }
  125. s/\bSUBT\b/@ $&/;
  126. s/\bDATA\b/@ $&/; # DATA directive is deprecated -- Asm guide, p.7-25
  127. s/\bKEEP\b/@ $&/;
  128. s/\bEXPORTAS\b/@ $&/;
  129. s/\|\|(.)+\bEQU\b/@ $&/;
  130. s/\|\|([\w\$]+)\|\|/$1/;
  131. s/\bENTRY\b/@ $&/;
  132. s/\bASSERT\b/@ $&/;
  133. s/\bGBLL\b/@ $&/;
  134. s/\bGBLA\b/@ $&/;
  135. s/^\W+OPT\b/@ $&/;
  136. s/:OR:/|/g;
  137. s/:SHL:/<</g;
  138. s/:SHR:/>>/g;
  139. s/:AND:/&/g;
  140. s/:LAND:/&&/g;
  141. s/CPSR/cpsr/;
  142. s/SPSR/spsr/;
  143. s/ALIGN$/.balign 4/;
  144. s/ALIGN\s+([0-9x]+)$/.balign $1/;
  145. s/psr_cxsf/psr_all/;
  146. s/LTORG/.ltorg/;
  147. s/^([A-Za-z_]\w*)[ \t]+EQU/ .set $1,/;
  148. s/^([A-Za-z_]\w*)[ \t]+SETL/ .set $1,/;
  149. s/^([A-Za-z_]\w*)[ \t]+SETA/ .set $1,/;
  150. s/^([A-Za-z_]\w*)[ \t]+\*/ .set $1,/;
  151. # {PC} + 0xdeadfeed --> . + 0xdeadfeed
  152. s/\{PC\} \+/ \. +/;
  153. # Single hex constant on the line !
  154. #
  155. # >>> NOTE <<<
  156. # Double-precision floats in gcc are always mixed-endian, which means
  157. # bytes in two words are little-endian, but words are big-endian.
  158. # So, 0x0000deadfeed0000 would be stored as 0x0000dead at low address
  159. # and 0xfeed0000 at high address.
  160. #
  161. s/\bDCFD\b[ \t]+0x([a-fA-F0-9]{8})([a-fA-F0-9]{8})/.long 0x$1, 0x$2/;
  162. # Only decimal constants on the line, no hex !
  163. s/\bDCFD\b[ \t]+([0-9\.\-]+)/.double $1/;
  164. # Single hex constant on the line !
  165. # s/\bDCFS\b[ \t]+0x([a-f0-9]{8})([a-f0-9]{8})/.long 0x$1, 0x$2/;
  166. # Only decimal constants on the line, no hex !
  167. # s/\bDCFS\b[ \t]+([0-9\.\-]+)/.double $1/;
  168. s/\bDCFS[ \t]+0x/.word 0x/;
  169. s/\bDCFS\b/.float/;
  170. s/^([A-Za-z_]\w*)[ \t]+DCD/$1 .word/;
  171. s/\bDCD\b/.word/;
  172. s/^([A-Za-z_]\w*)[ \t]+DCW/$1 .short/;
  173. s/\bDCW\b/.short/;
  174. s/^([A-Za-z_]\w*)[ \t]+DCB/$1 .byte/;
  175. s/\bDCB\b/.byte/;
  176. s/^([A-Za-z_]\w*)[ \t]+\%/.comm $1,/;
  177. s/^[A-Za-z_\.]\w+/$&:/;
  178. s/^(\d+)/$1:/;
  179. s/\%(\d+)/$1b_or_f/;
  180. s/\%[Bb](\d+)/$1b/;
  181. s/\%[Ff](\d+)/$1f/;
  182. s/\%[Ff][Tt](\d+)/$1f/;
  183. s/&([\dA-Fa-f]+)/0x$1/;
  184. if ( /\b2_[01]+\b/ ) {
  185. s/\b2_([01]+)\b/conv$1&&&&/g;
  186. while ( /[01][01][01][01]&&&&/ ) {
  187. s/0000&&&&/&&&&0/g;
  188. s/0001&&&&/&&&&1/g;
  189. s/0010&&&&/&&&&2/g;
  190. s/0011&&&&/&&&&3/g;
  191. s/0100&&&&/&&&&4/g;
  192. s/0101&&&&/&&&&5/g;
  193. s/0110&&&&/&&&&6/g;
  194. s/0111&&&&/&&&&7/g;
  195. s/1000&&&&/&&&&8/g;
  196. s/1001&&&&/&&&&9/g;
  197. s/1010&&&&/&&&&A/g;
  198. s/1011&&&&/&&&&B/g;
  199. s/1100&&&&/&&&&C/g;
  200. s/1101&&&&/&&&&D/g;
  201. s/1110&&&&/&&&&E/g;
  202. s/1111&&&&/&&&&F/g;
  203. }
  204. s/000&&&&/&&&&0/g;
  205. s/001&&&&/&&&&1/g;
  206. s/010&&&&/&&&&2/g;
  207. s/011&&&&/&&&&3/g;
  208. s/100&&&&/&&&&4/g;
  209. s/101&&&&/&&&&5/g;
  210. s/110&&&&/&&&&6/g;
  211. s/111&&&&/&&&&7/g;
  212. s/00&&&&/&&&&0/g;
  213. s/01&&&&/&&&&1/g;
  214. s/10&&&&/&&&&2/g;
  215. s/11&&&&/&&&&3/g;
  216. s/0&&&&/&&&&0/g;
  217. s/1&&&&/&&&&1/g;
  218. s/conv&&&&/0x/g;
  219. }
  220. if ( /commandline/)
  221. {
  222. if( /-bigend/)
  223. {
  224. $bigend=1;
  225. }
  226. }
  227. if ( /\bDCDU\b/ )
  228. {
  229. my $cmd=$_;
  230. my $value;
  231. my $prefix;
  232. my $w1;
  233. my $w2;
  234. my $w3;
  235. my $w4;
  236. s/\s+DCDU\b/@ $&/;
  237. $cmd =~ /\bDCDU\b\s+0x(\d+)/;
  238. $value = $1;
  239. $value =~ /(\w\w)(\w\w)(\w\w)(\w\w)/;
  240. $w1 = $1;
  241. $w2 = $2;
  242. $w3 = $3;
  243. $w4 = $4;
  244. if( $bigend ne "")
  245. {
  246. # big endian
  247. $prefix = "\t.byte\t0x".$w1.";".
  248. "\t.byte\t0x".$w2.";".
  249. "\t.byte\t0x".$w3.";".
  250. "\t.byte\t0x".$w4."; ";
  251. }
  252. else
  253. {
  254. # little endian
  255. $prefix = "\t.byte\t0x".$w4.";".
  256. "\t.byte\t0x".$w3.";".
  257. "\t.byte\t0x".$w2.";".
  258. "\t.byte\t0x".$w1."; ";
  259. }
  260. $_=$prefix.$_;
  261. }
  262. if ( /\badrl\b/i )
  263. {
  264. s/\badrl\s+(\w+)\s*,\s*(\w+)/ldr $1,=$2/i;
  265. $addPadding = 1;
  266. }
  267. s/\bEND\b/@ END/;
  268. } continue {
  269. printf ("%s", $_) if $printit;
  270. if ($addPadding != 0)
  271. {
  272. printf (" mov r0,r0\n");
  273. $addPadding = 0;
  274. }
  275. }
  276. #If we had a code section, mark that this object doesn't need an executable
  277. # stack.
  278. if ($nxstack) {
  279. printf (" .section\t.note.GNU-stack,\"\",\%\%progbits\n");
  280. }