John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | ## |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 3 | ## Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | ## |
| 5 | ## Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 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 |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 8 | ## in the file PATENTS. All contributing project authors may |
| 9 | ## be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 10 | ## |
| 11 | |
| 12 | |
| 13 | # ads2gas.pl |
| 14 | # Author: Eric Fung (efung (at) acm.org) |
| 15 | # |
| 16 | # Convert ARM Developer Suite 1.0.1 syntax assembly source to GNU as format |
| 17 | # |
| 18 | # Usage: cat inputfile | perl ads2gas.pl > outputfile |
| 19 | # |
| 20 | print "@ This file was created from a .asm file\n"; |
| 21 | print "@ using the ads2gas.pl script.\n"; |
| 22 | print "\t.equ DO1STROUNDING, 0\n"; |
| 23 | |
Timothy B. Terriberry | 1647f00 | 2011-07-19 12:13:18 -0700 | [diff] [blame] | 24 | # Stack of procedure names. |
| 25 | @proc_stack = (); |
| 26 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 27 | while (<STDIN>) |
| 28 | { |
Tero Rintaluoma | 33fa7c4 | 2011-04-11 12:04:17 +0300 | [diff] [blame] | 29 | # Load and store alignment |
| 30 | s/@/,:/g; |
| 31 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 32 | # Comment character |
| 33 | s/;/@/g; |
| 34 | |
| 35 | # Hexadecimal constants prefaced by 0x |
| 36 | s/#&/#0x/g; |
| 37 | |
| 38 | # Convert :OR: to | |
| 39 | s/:OR:/ | /g; |
| 40 | |
| 41 | # Convert :AND: to & |
| 42 | s/:AND:/ & /g; |
| 43 | |
| 44 | # Convert :NOT: to ~ |
| 45 | s/:NOT:/ ~ /g; |
| 46 | |
| 47 | # Convert :SHL: to << |
| 48 | s/:SHL:/ << /g; |
| 49 | |
| 50 | # Convert :SHR: to >> |
| 51 | s/:SHR:/ >> /g; |
| 52 | |
| 53 | # Convert ELSE to .else |
| 54 | s/ELSE/.else/g; |
| 55 | |
| 56 | # Convert ENDIF to .endif |
| 57 | s/ENDIF/.endif/g; |
| 58 | |
| 59 | # Convert ELSEIF to .elseif |
| 60 | s/ELSEIF/.elseif/g; |
| 61 | |
| 62 | # Convert LTORG to .ltorg |
| 63 | s/LTORG/.ltorg/g; |
| 64 | |
| 65 | # Convert IF :DEF:to .if |
| 66 | # gcc doesn't have the ability to do a conditional |
| 67 | # if defined variable that is set by IF :DEF: on |
| 68 | # armasm, so convert it to a normal .if and then |
| 69 | # make sure to define a value elesewhere |
| 70 | if (s/\bIF :DEF:\b/.if /g) |
| 71 | { |
| 72 | s/=/==/g; |
| 73 | } |
| 74 | |
| 75 | # Convert IF to .if |
| 76 | if (s/\bIF\b/.if/g) |
| 77 | { |
| 78 | s/=+/==/g; |
| 79 | } |
| 80 | |
| 81 | # Convert INCLUDE to .INCLUDE "file" |
| 82 | s/INCLUDE(\s*)(.*)$/.include $1\"$2\"/; |
| 83 | |
| 84 | # Code directive (ARM vs Thumb) |
| 85 | s/CODE([0-9][0-9])/.code $1/; |
| 86 | |
| 87 | # No AREA required |
Johann | 2007c3b | 2011-06-27 11:38:26 -0400 | [diff] [blame] | 88 | # But ALIGNs in AREA must be obeyed |
| 89 | s/^\s*AREA.*ALIGN=([0-9])$/.text\n.p2align $1/; |
| 90 | # If no ALIGN, strip the AREA and align to 4 bytes |
| 91 | s/^\s*AREA.*$/.text\n.p2align 2/; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 92 | |
| 93 | # DCD to .word |
| 94 | # This one is for incoming symbols |
| 95 | s/DCD\s+\|(\w*)\|/.long $1/; |
| 96 | |
| 97 | # DCW to .short |
| 98 | s/DCW\s+\|(\w*)\|/.short $1/; |
| 99 | s/DCW(.*)/.short $1/; |
| 100 | |
| 101 | # Constants defined in scope |
| 102 | s/DCD(.*)/.long $1/; |
| 103 | s/DCB(.*)/.byte $1/; |
| 104 | |
| 105 | # RN to .req |
| 106 | if (s/RN\s+([Rr]\d+|lr)/.req $1/) |
| 107 | { |
| 108 | print; |
| 109 | next; |
| 110 | } |
| 111 | |
| 112 | # Make function visible to linker, and make additional symbol with |
| 113 | # prepended underscore |
| 114 | s/EXPORT\s+\|([\$\w]*)\|/.global $1 \n\t.type $1, function/; |
| 115 | s/IMPORT\s+\|([\$\w]*)\|/.global $1/; |
| 116 | |
| 117 | # No vertical bars required; make additional symbol with prepended |
| 118 | # underscore |
| 119 | s/^\|(\$?\w+)\|/_$1\n\t$1:/g; |
| 120 | |
| 121 | # Labels need trailing colon |
| 122 | # s/^(\w+)/$1:/ if !/EQU/; |
| 123 | # put the colon at the end of the line in the macro |
| 124 | s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/; |
| 125 | |
Tero Rintaluoma | 33fa7c4 | 2011-04-11 12:04:17 +0300 | [diff] [blame] | 126 | # ALIGN directive |
| 127 | s/ALIGN/.balign/g; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 128 | |
Attila Nagy | c5434ab | 2011-11-18 12:28:43 +0200 | [diff] [blame^] | 129 | # ARM code |
| 130 | s/\sARM/.arm/g; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 131 | |
Attila Nagy | c5434ab | 2011-11-18 12:28:43 +0200 | [diff] [blame^] | 132 | # REQUIRE8 Stack is required to be 8-byte aligned |
| 133 | s/\sREQUIRE8/.eabi_attribute Tag_ABI_align_needed, 1/g; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 134 | |
Attila Nagy | c5434ab | 2011-11-18 12:28:43 +0200 | [diff] [blame^] | 135 | # PRESERVE8 Stack 8-byte align is preserved |
| 136 | s/\sPRESERVE8/.eabi_attribute Tag_ABI_align_preserved, 1/g; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 137 | |
Timothy B. Terriberry | 1647f00 | 2011-07-19 12:13:18 -0700 | [diff] [blame] | 138 | # Use PROC and ENDP to give the symbols a .size directive. |
| 139 | # This makes them show up properly in debugging tools like gdb and valgrind. |
| 140 | if (/\bPROC\b/) |
| 141 | { |
| 142 | my $proc; |
| 143 | /^_([\.0-9A-Z_a-z]\w+)\b/; |
| 144 | $proc = $1; |
| 145 | push(@proc_stack, $proc) if ($proc); |
| 146 | s/\bPROC\b/@ $&/; |
| 147 | } |
| 148 | if (/\bENDP\b/) |
| 149 | { |
| 150 | my $proc; |
| 151 | s/\bENDP\b/@ $&/; |
| 152 | $proc = pop(@proc_stack); |
| 153 | $_ = "\t.size $proc, .-$proc".$_ if ($proc); |
| 154 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 155 | |
| 156 | # EQU directive |
| 157 | s/(.*)EQU(.*)/.equ $1, $2/; |
| 158 | |
| 159 | # Begin macro definition |
| 160 | if (/MACRO/) { |
| 161 | $_ = <STDIN>; |
| 162 | s/^/.macro/; |
| 163 | s/\$//g; # remove formal param reference |
| 164 | s/;/@/g; # change comment characters |
| 165 | } |
| 166 | |
| 167 | # For macros, use \ to reference formal params |
| 168 | s/\$/\\/g; # End macro definition |
| 169 | s/MEND/.endm/; # No need to tell it where to stop assembling |
| 170 | next if /^\s*END\s*$/; |
| 171 | print; |
| 172 | } |
Timothy B. Terriberry | 0453aca | 2011-07-19 13:09:22 -0700 | [diff] [blame] | 173 | |
| 174 | # Mark that this object doesn't need an executable stack. |
| 175 | printf ("\t.section\t.note.GNU-stack,\"\",\%\%progbits\n"); |