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