ads2gas.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env perl
  2. ##
  3. ## Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  4. ##
  5. ## Use of this source code is governed by a BSD-style license
  6. ## that can be found in the LICENSE file in the root of the source
  7. ## tree. An additional intellectual property rights grant can be found
  8. ## in the file PATENTS. All contributing project authors may
  9. ## be found in the AUTHORS file in the root of the source tree.
  10. ##
  11. # ads2gas.pl
  12. # Author: Eric Fung (efung (at) acm.org)
  13. #
  14. # Convert ARM Developer Suite 1.0.1 syntax assembly source to GNU as format
  15. #
  16. # Usage: cat inputfile | perl ads2gas.pl > outputfile
  17. #
  18. use FindBin;
  19. use lib $FindBin::Bin;
  20. use thumb;
  21. my $thumb = 0;
  22. my $elf = 1;
  23. foreach my $arg (@ARGV) {
  24. $thumb = 1 if ($arg eq "-thumb");
  25. $elf = 0 if ($arg eq "-noelf");
  26. }
  27. print "@ This file was created from a .asm file\n";
  28. print "@ using the ads2gas.pl script.\n";
  29. print "\t.syntax unified\n";
  30. if ($thumb) {
  31. print "\t.thumb\n";
  32. }
  33. # Stack of procedure names.
  34. @proc_stack = ();
  35. while (<STDIN>)
  36. {
  37. undef $comment;
  38. undef $line;
  39. $comment_char = ";";
  40. $comment_sub = "@";
  41. # Handle comments.
  42. if (/$comment_char/)
  43. {
  44. $comment = "";
  45. ($line, $comment) = /(.*?)$comment_char(.*)/;
  46. $_ = $line;
  47. }
  48. # Load and store alignment
  49. s/@/,:/g;
  50. # Hexadecimal constants prefaced by 0x
  51. s/#&/#0x/g;
  52. # Convert :OR: to |
  53. s/:OR:/ | /g;
  54. # Convert :AND: to &
  55. s/:AND:/ & /g;
  56. # Convert :NOT: to ~
  57. s/:NOT:/ ~ /g;
  58. # Convert :SHL: to <<
  59. s/:SHL:/ << /g;
  60. # Convert :SHR: to >>
  61. s/:SHR:/ >> /g;
  62. # Convert ELSE to .else
  63. s/\bELSE\b/.else/g;
  64. # Convert ENDIF to .endif
  65. s/\bENDIF\b/.endif/g;
  66. # Convert ELSEIF to .elseif
  67. s/\bELSEIF\b/.elseif/g;
  68. # Convert LTORG to .ltorg
  69. s/\bLTORG\b/.ltorg/g;
  70. # Convert endfunc to nothing.
  71. s/\bendfunc\b//ig;
  72. # Convert FUNCTION to nothing.
  73. s/\bFUNCTION\b//g;
  74. s/\bfunction\b//g;
  75. s/\bENTRY\b//g;
  76. s/\bMSARMASM\b/0/g;
  77. s/^\s+end\s+$//g;
  78. # Convert IF :DEF:to .if
  79. # gcc doesn't have the ability to do a conditional
  80. # if defined variable that is set by IF :DEF: on
  81. # armasm, so convert it to a normal .if and then
  82. # make sure to define a value elesewhere
  83. if (s/\bIF :DEF:\b/.if /g)
  84. {
  85. s/=/==/g;
  86. }
  87. # Convert IF to .if
  88. if (s/\bIF\b/.if/g)
  89. {
  90. s/=+/==/g;
  91. }
  92. # Convert INCLUDE to .INCLUDE "file"
  93. s/INCLUDE(\s*)(.*)$/.include $1\"$2\"/;
  94. # Code directive (ARM vs Thumb)
  95. s/CODE([0-9][0-9])/.code $1/;
  96. # No AREA required
  97. # But ALIGNs in AREA must be obeyed
  98. s/^\s*AREA.*ALIGN=([0-9])$/.text\n.p2align $1/;
  99. # If no ALIGN, strip the AREA and align to 4 bytes
  100. s/^\s*AREA.*$/.text\n.p2align 2/;
  101. # DCD to .word
  102. # This one is for incoming symbols
  103. s/DCD\s+\|(\w*)\|/.long $1/;
  104. # DCW to .short
  105. s/DCW\s+\|(\w*)\|/.short $1/;
  106. s/DCW(.*)/.short $1/;
  107. # Constants defined in scope
  108. s/DCD(.*)/.long $1/;
  109. s/DCB(.*)/.byte $1/;
  110. # Make function visible to linker, and make additional symbol with
  111. # prepended underscore
  112. if ($elf) {
  113. s/EXPORT\s+\|([\$\w]*)\|/.global $1 \n\t.type $1, function/;
  114. } else {
  115. s/EXPORT\s+\|([\$\w]*)\|/.global $1/;
  116. }
  117. s/IMPORT\s+\|([\$\w]*)\|/.global $1/;
  118. s/EXPORT\s+([\$\w]*)/.global $1/;
  119. s/export\s+([\$\w]*)/.global $1/;
  120. # No vertical bars required; make additional symbol with prepended
  121. # underscore
  122. s/^\|(\$?\w+)\|/_$1\n\t$1:/g;
  123. # Labels need trailing colon
  124. # s/^(\w+)/$1:/ if !/EQU/;
  125. # put the colon at the end of the line in the macro
  126. s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/;
  127. # ALIGN directive
  128. s/\bALIGN\b/.balign/g;
  129. if ($thumb) {
  130. # ARM code - we force everything to thumb with the declaration in the header
  131. s/\sARM//g;
  132. } else {
  133. # ARM code
  134. s/\sARM/.arm/g;
  135. }
  136. # push/pop
  137. s/(push\s+)(r\d+)/stmdb sp\!, \{$2\}/g;
  138. s/(pop\s+)(r\d+)/ldmia sp\!, \{$2\}/g;
  139. # NEON code
  140. s/(vld1.\d+\s+)(q\d+)/$1\{$2\}/g;
  141. s/(vtbl.\d+\s+[^,]+),([^,]+)/$1,\{$2\}/g;
  142. if ($thumb) {
  143. thumb::FixThumbInstructions($_, 0);
  144. }
  145. # eabi_attributes numerical equivalents can be found in the
  146. # "ARM IHI 0045C" document.
  147. if ($elf) {
  148. # REQUIRE8 Stack is required to be 8-byte aligned
  149. s/\sREQUIRE8/.eabi_attribute 24, 1 \@Tag_ABI_align_needed/g;
  150. # PRESERVE8 Stack 8-byte align is preserved
  151. s/\sPRESERVE8/.eabi_attribute 25, 1 \@Tag_ABI_align_preserved/g;
  152. } else {
  153. s/\sREQUIRE8//;
  154. s/\sPRESERVE8//;
  155. }
  156. # Use PROC and ENDP to give the symbols a .size directive.
  157. # This makes them show up properly in debugging tools like gdb and valgrind.
  158. if (/\bPROC\b/)
  159. {
  160. my $proc;
  161. /^_([\.0-9A-Z_a-z]\w+)\b/;
  162. $proc = $1;
  163. push(@proc_stack, $proc) if ($proc);
  164. s/\bPROC\b/@ $&/;
  165. }
  166. if (/\bENDP\b/)
  167. {
  168. my $proc;
  169. s/\bENDP\b/@ $&/;
  170. $proc = pop(@proc_stack);
  171. $_ = "\t.size $proc, .-$proc".$_ if ($proc and $elf);
  172. }
  173. # EQU directive
  174. s/(\S+\s+)EQU(\s+\S+)/.equ $1, $2/;
  175. # Begin macro definition
  176. if (/\bMACRO\b/) {
  177. $_ = <STDIN>;
  178. s/^/.macro/;
  179. s/\$//g; # remove formal param reference
  180. s/;/@/g; # change comment characters
  181. }
  182. # For macros, use \ to reference formal params
  183. s/\$/\\/g; # End macro definition
  184. s/\bMEND\b/.endm/; # No need to tell it where to stop assembling
  185. next if /^\s*END\s*$/;
  186. print;
  187. print "$comment_sub$comment\n" if defined $comment;
  188. }
  189. # Mark that this object doesn't need an executable stack.
  190. printf ("\t.section\t.note.GNU-stack,\"\",\%\%progbits\n") if $elf;