michael@0: #!/usr/bin/perl michael@0: michael@0: my $bigend; # little/big endian michael@0: my $nxstack; michael@0: michael@0: $nxstack = 0; michael@0: michael@0: eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}' michael@0: if $running_under_some_shell; michael@0: michael@0: while ($ARGV[0] =~ /^-/) { michael@0: $_ = shift; michael@0: last if /^--/; michael@0: if (/^-n/) { michael@0: $nflag++; michael@0: next; michael@0: } michael@0: die "I don't recognize this switch: $_\\n"; michael@0: } michael@0: $printit++ unless $nflag; michael@0: michael@0: $\ = "\n"; # automatically add newline on print michael@0: $n=0; michael@0: michael@0: $thumb = 0; # ARM mode by default, not Thumb. michael@0: michael@0: LINE: michael@0: while (<>) { michael@0: michael@0: # For ADRLs we need to add a new line after the substituted one. michael@0: $addPadding = 0; michael@0: michael@0: # First, we do not dare to touch *anything* inside double quotes, do we? michael@0: # Second, if you want a dollar character in the string, michael@0: # insert two of them -- that's how ARM C and assembler treat strings. michael@0: s/^([A-Za-z_]\w*)[ \t]+DCB[ \t]*\"/$1: .ascii \"/ && do { s/\$\$/\$/g; next }; michael@0: s/\bDCB\b[ \t]*\"/.ascii \"/ && do { s/\$\$/\$/g; next }; michael@0: s/^(\S+)\s+RN\s+(\S+)/$1 .req r$2/ && do { s/\$\$/\$/g; next }; michael@0: # If there's nothing on a line but a comment, don't try to apply any further michael@0: # substitutions (this is a cheap hack to avoid mucking up the license header) michael@0: s/^([ \t]*);/$1@/ && do { s/\$\$/\$/g; next }; michael@0: # If substituted -- leave immediately ! michael@0: michael@0: s/@/,:/; michael@0: s/;/@/; michael@0: while ( /@.*'/ ) { michael@0: s/(@.*)'/$1/g; michael@0: } michael@0: s/\{FALSE\}/0/g; michael@0: s/\{TRUE\}/1/g; michael@0: s/\{(\w\w\w\w+)\}/$1/g; michael@0: s/\bINCLUDE[ \t]*([^ \t\n]+)/.include \"$1\"/; michael@0: s/\bGET[ \t]*([^ \t\n]+)/.include \"${ my $x=$1; $x =~ s|\.s|-gnu.S|; \$x }\"/; michael@0: s/\bIMPORT\b/.extern/; michael@0: s/\bEXPORT\b/.global/; michael@0: s/^(\s+)\[/$1IF/; michael@0: s/^(\s+)\|/$1ELSE/; michael@0: s/^(\s+)\]/$1ENDIF/; michael@0: s/IF *:DEF:/ .ifdef/; michael@0: s/IF *:LNOT: *:DEF:/ .ifndef/; michael@0: s/ELSE/ .else/; michael@0: s/ENDIF/ .endif/; michael@0: michael@0: if( /\bIF\b/ ) { michael@0: s/\bIF\b/ .if/; michael@0: s/=/==/; michael@0: } michael@0: if ( $n == 2) { michael@0: s/\$/\\/g; michael@0: } michael@0: if ($n == 1) { michael@0: s/\$//g; michael@0: s/label//g; michael@0: $n = 2; michael@0: } michael@0: if ( /MACRO/ ) { michael@0: s/MACRO *\n/.macro/; michael@0: $n=1; michael@0: } michael@0: if ( /\bMEND\b/ ) { michael@0: s/\bMEND\b/.endm/; michael@0: $n=0; michael@0: } michael@0: michael@0: # ".rdata" doesn't work in 'as' version 2.13.2, as it is ".rodata" there. michael@0: # michael@0: if ( /\bAREA\b/ ) { michael@0: if ( /CODE/ ) { michael@0: $nxstack = 1; michael@0: } michael@0: s/^(.+)CODE(.+)READONLY(.*)/ .text/; michael@0: s/^(.+)DATA(.+)READONLY(.*)/ .section .rdata\n .align 2/; michael@0: s/^(.+)\|\|\.data\|\|(.+)/ .data\n .align 2/; michael@0: s/^(.+)\|\|\.bss\|\|(.+)/ .bss/; michael@0: } michael@0: michael@0: s/\|\|\.constdata\$(\d+)\|\|/.L_CONST$1/; # ||.constdata$3|| michael@0: s/\|\|\.bss\$(\d+)\|\|/.L_BSS$1/; # ||.bss$2|| michael@0: s/\|\|\.data\$(\d+)\|\|/.L_DATA$1/; # ||.data$2|| michael@0: s/\|\|([a-zA-Z0-9_]+)\@([a-zA-Z0-9_]+)\|\|/@ $&/; michael@0: s/^(\s+)\%(\s)/ .space $1/; michael@0: michael@0: s/\|(.+)\.(\d+)\|/\.$1_$2/; # |L80.123| -> .L80_123 michael@0: s/\bCODE32\b/.code 32/ && do {$thumb = 0}; michael@0: s/\bCODE16\b/.code 16/ && do {$thumb = 1}; michael@0: if (/\bPROC\b/) michael@0: { michael@0: print " .thumb_func" if ($thumb); michael@0: s/\bPROC\b/@ $&/; michael@0: } michael@0: s/^(\s*)(S|Q|SH|U|UQ|UH)ASX\b/$1$2ADDSUBX/; michael@0: s/^(\s*)(S|Q|SH|U|UQ|UH)SAX\b/$1$2SUBADDX/; michael@0: s/\bENDP\b/@ $&/; michael@0: s/\bSUBT\b/@ $&/; michael@0: s/\bDATA\b/@ $&/; # DATA directive is deprecated -- Asm guide, p.7-25 michael@0: s/\bKEEP\b/@ $&/; michael@0: s/\bEXPORTAS\b/@ $&/; michael@0: s/\|\|(.)+\bEQU\b/@ $&/; michael@0: s/\|\|([\w\$]+)\|\|/$1/; michael@0: s/\bENTRY\b/@ $&/; michael@0: s/\bASSERT\b/@ $&/; michael@0: s/\bGBLL\b/@ $&/; michael@0: s/\bGBLA\b/@ $&/; michael@0: s/^\W+OPT\b/@ $&/; michael@0: s/:OR:/|/g; michael@0: s/:SHL:/<>/g; michael@0: s/:AND:/&/g; michael@0: s/:LAND:/&&/g; michael@0: s/CPSR/cpsr/; michael@0: s/SPSR/spsr/; michael@0: s/ALIGN$/.balign 4/; michael@0: s/ALIGN\s+([0-9x]+)$/.balign $1/; michael@0: s/psr_cxsf/psr_all/; michael@0: s/LTORG/.ltorg/; michael@0: s/^([A-Za-z_]\w*)[ \t]+EQU/ .set $1,/; michael@0: s/^([A-Za-z_]\w*)[ \t]+SETL/ .set $1,/; michael@0: s/^([A-Za-z_]\w*)[ \t]+SETA/ .set $1,/; michael@0: s/^([A-Za-z_]\w*)[ \t]+\*/ .set $1,/; michael@0: michael@0: # {PC} + 0xdeadfeed --> . + 0xdeadfeed michael@0: s/\{PC\} \+/ \. +/; michael@0: michael@0: # Single hex constant on the line ! michael@0: # michael@0: # >>> NOTE <<< michael@0: # Double-precision floats in gcc are always mixed-endian, which means michael@0: # bytes in two words are little-endian, but words are big-endian. michael@0: # So, 0x0000deadfeed0000 would be stored as 0x0000dead at low address michael@0: # and 0xfeed0000 at high address. michael@0: # michael@0: s/\bDCFD\b[ \t]+0x([a-fA-F0-9]{8})([a-fA-F0-9]{8})/.long 0x$1, 0x$2/; michael@0: # Only decimal constants on the line, no hex ! michael@0: s/\bDCFD\b[ \t]+([0-9\.\-]+)/.double $1/; michael@0: michael@0: # Single hex constant on the line ! michael@0: # s/\bDCFS\b[ \t]+0x([a-f0-9]{8})([a-f0-9]{8})/.long 0x$1, 0x$2/; michael@0: # Only decimal constants on the line, no hex ! michael@0: # s/\bDCFS\b[ \t]+([0-9\.\-]+)/.double $1/; michael@0: s/\bDCFS[ \t]+0x/.word 0x/; michael@0: s/\bDCFS\b/.float/; michael@0: michael@0: s/^([A-Za-z_]\w*)[ \t]+DCD/$1 .word/; michael@0: s/\bDCD\b/.word/; michael@0: s/^([A-Za-z_]\w*)[ \t]+DCW/$1 .short/; michael@0: s/\bDCW\b/.short/; michael@0: s/^([A-Za-z_]\w*)[ \t]+DCB/$1 .byte/; michael@0: s/\bDCB\b/.byte/; michael@0: s/^([A-Za-z_]\w*)[ \t]+\%/.comm $1,/; michael@0: s/^[A-Za-z_\.]\w+/$&:/; michael@0: s/^(\d+)/$1:/; michael@0: s/\%(\d+)/$1b_or_f/; michael@0: s/\%[Bb](\d+)/$1b/; michael@0: s/\%[Ff](\d+)/$1f/; michael@0: s/\%[Ff][Tt](\d+)/$1f/; michael@0: s/&([\dA-Fa-f]+)/0x$1/; michael@0: if ( /\b2_[01]+\b/ ) { michael@0: s/\b2_([01]+)\b/conv$1&&&&/g; michael@0: while ( /[01][01][01][01]&&&&/ ) { michael@0: s/0000&&&&/&&&&0/g; michael@0: s/0001&&&&/&&&&1/g; michael@0: s/0010&&&&/&&&&2/g; michael@0: s/0011&&&&/&&&&3/g; michael@0: s/0100&&&&/&&&&4/g; michael@0: s/0101&&&&/&&&&5/g; michael@0: s/0110&&&&/&&&&6/g; michael@0: s/0111&&&&/&&&&7/g; michael@0: s/1000&&&&/&&&&8/g; michael@0: s/1001&&&&/&&&&9/g; michael@0: s/1010&&&&/&&&&A/g; michael@0: s/1011&&&&/&&&&B/g; michael@0: s/1100&&&&/&&&&C/g; michael@0: s/1101&&&&/&&&&D/g; michael@0: s/1110&&&&/&&&&E/g; michael@0: s/1111&&&&/&&&&F/g; michael@0: } michael@0: s/000&&&&/&&&&0/g; michael@0: s/001&&&&/&&&&1/g; michael@0: s/010&&&&/&&&&2/g; michael@0: s/011&&&&/&&&&3/g; michael@0: s/100&&&&/&&&&4/g; michael@0: s/101&&&&/&&&&5/g; michael@0: s/110&&&&/&&&&6/g; michael@0: s/111&&&&/&&&&7/g; michael@0: s/00&&&&/&&&&0/g; michael@0: s/01&&&&/&&&&1/g; michael@0: s/10&&&&/&&&&2/g; michael@0: s/11&&&&/&&&&3/g; michael@0: s/0&&&&/&&&&0/g; michael@0: s/1&&&&/&&&&1/g; michael@0: s/conv&&&&/0x/g; michael@0: } michael@0: michael@0: if ( /commandline/) michael@0: { michael@0: if( /-bigend/) michael@0: { michael@0: $bigend=1; michael@0: } michael@0: } michael@0: michael@0: if ( /\bDCDU\b/ ) michael@0: { michael@0: my $cmd=$_; michael@0: my $value; michael@0: my $w1; michael@0: my $w2; michael@0: my $w3; michael@0: my $w4; michael@0: michael@0: s/\s+DCDU\b/@ $&/; michael@0: michael@0: $cmd =~ /\bDCDU\b\s+0x(\d+)/; michael@0: $value = $1; michael@0: $value =~ /(\w\w)(\w\w)(\w\w)(\w\w)/; michael@0: $w1 = $1; michael@0: $w2 = $2; michael@0: $w3 = $3; michael@0: $w4 = $4; michael@0: michael@0: if( $bigend ne "") michael@0: { michael@0: # big endian michael@0: michael@0: print " .byte 0x".$w1; michael@0: print " .byte 0x".$w2; michael@0: print " .byte 0x".$w3; michael@0: print " .byte 0x".$w4; michael@0: } michael@0: else michael@0: { michael@0: # little endian michael@0: michael@0: print " .byte 0x".$w4; michael@0: print " .byte 0x".$w3; michael@0: print " .byte 0x".$w2; michael@0: print " .byte 0x".$w1; michael@0: } michael@0: michael@0: } michael@0: michael@0: michael@0: if ( /\badrl\b/i ) michael@0: { michael@0: s/\badrl\s+(\w+)\s*,\s*(\w+)/ldr $1,=$2/i; michael@0: $addPadding = 1; michael@0: } michael@0: s/\bEND\b/@ END/; michael@0: } continue { michael@0: printf ("%s", $_) if $printit; michael@0: if ($addPadding != 0) michael@0: { michael@0: printf (" mov r0,r0\n"); michael@0: $addPadding = 0; michael@0: } michael@0: } michael@0: #If we had a code section, mark that this object doesn't need an executable michael@0: # stack. michael@0: if ($nxstack) { michael@0: printf (" .section\t.note.GNU-stack,\"\",\%\%progbits\n"); michael@0: }