cannam@154: #!/usr/bin/perl cannam@154: # Copyright (C) 2002-2013 Xiph.org Foundation cannam@154: # cannam@154: # Redistribution and use in source and binary forms, with or without cannam@154: # modification, are permitted provided that the following conditions cannam@154: # are met: cannam@154: # cannam@154: # - Redistributions of source code must retain the above copyright cannam@154: # notice, this list of conditions and the following disclaimer. cannam@154: # cannam@154: # - Redistributions in binary form must reproduce the above copyright cannam@154: # notice, this list of conditions and the following disclaimer in the cannam@154: # documentation and/or other materials provided with the distribution. cannam@154: # cannam@154: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: cannam@154: my $bigend; # little/big endian cannam@154: my $nxstack; cannam@154: my $apple = 0; cannam@154: my $symprefix = ""; cannam@154: cannam@154: $nxstack = 0; cannam@154: cannam@154: eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}' cannam@154: if $running_under_some_shell; cannam@154: cannam@154: while ($ARGV[0] =~ /^-/) { cannam@154: $_ = shift; cannam@154: last if /^--$/; cannam@154: if (/^-n$/) { cannam@154: $nflag++; cannam@154: next; cannam@154: } cannam@154: if (/^--apple$/) { cannam@154: $apple = 1; cannam@154: $symprefix = "_"; cannam@154: next; cannam@154: } cannam@154: die "I don't recognize this switch: $_\\n"; cannam@154: } cannam@154: $printit++ unless $nflag; cannam@154: cannam@154: $\ = "\n"; # automatically add newline on print cannam@154: $n=0; cannam@154: cannam@154: $thumb = 0; # ARM mode by default, not Thumb. cannam@154: @proc_stack = (); cannam@154: cannam@154: printf (" .syntax unified\n"); cannam@154: cannam@154: LINE: cannam@154: while (<>) { cannam@154: cannam@154: # For ADRLs we need to add a new line after the substituted one. cannam@154: $addPadding = 0; cannam@154: cannam@154: # First, we do not dare to touch *anything* inside double quotes, do we? cannam@154: # Second, if you want a dollar character in the string, cannam@154: # insert two of them -- that's how ARM C and assembler treat strings. cannam@154: s/^([A-Za-z_]\w*)[ \t]+DCB[ \t]*\"/$1: .ascii \"/ && do { s/\$\$/\$/g; next }; cannam@154: s/\bDCB\b[ \t]*\"/.ascii \"/ && do { s/\$\$/\$/g; next }; cannam@154: s/^(\S+)\s+RN\s+(\S+)/$1 .req r$2/ && do { s/\$\$/\$/g; next }; cannam@154: # If there's nothing on a line but a comment, don't try to apply any further cannam@154: # substitutions (this is a cheap hack to avoid mucking up the license header) cannam@154: s/^([ \t]*);/$1@/ && do { s/\$\$/\$/g; next }; cannam@154: # If substituted -- leave immediately ! cannam@154: cannam@154: s/@/,:/; cannam@154: s/;/@/; cannam@154: while ( /@.*'/ ) { cannam@154: s/(@.*)'/$1/g; cannam@154: } cannam@154: s/\{FALSE\}/0/g; cannam@154: s/\{TRUE\}/1/g; cannam@154: s/\{(\w\w\w\w+)\}/$1/g; cannam@154: s/\bINCLUDE[ \t]*([^ \t\n]+)/.include \"$1\"/; cannam@154: s/\bGET[ \t]*([^ \t\n]+)/.include \"${ my $x=$1; $x =~ s|\.s|-gnu.S|; \$x }\"/; cannam@154: s/\bIMPORT\b/.extern/; cannam@154: s/\bEXPORT\b\s*/.global $symprefix/; cannam@154: s/^(\s+)\[/$1IF/; cannam@154: s/^(\s+)\|/$1ELSE/; cannam@154: s/^(\s+)\]/$1ENDIF/; cannam@154: s/IF *:DEF:/ .ifdef/; cannam@154: s/IF *:LNOT: *:DEF:/ .ifndef/; cannam@154: s/ELSE/ .else/; cannam@154: s/ENDIF/ .endif/; cannam@154: cannam@154: if( /\bIF\b/ ) { cannam@154: s/\bIF\b/ .if/; cannam@154: s/=/==/; cannam@154: } cannam@154: if ( $n == 2) { cannam@154: s/\$/\\/g; cannam@154: } cannam@154: if ($n == 1) { cannam@154: s/\$//g; cannam@154: s/label//g; cannam@154: $n = 2; cannam@154: } cannam@154: if ( /MACRO/ ) { cannam@154: s/MACRO *\n/.macro/; cannam@154: $n=1; cannam@154: } cannam@154: if ( /\bMEND\b/ ) { cannam@154: s/\bMEND\b/.endm/; cannam@154: $n=0; cannam@154: } cannam@154: cannam@154: # ".rdata" doesn't work in 'as' version 2.13.2, as it is ".rodata" there. cannam@154: # cannam@154: if ( /\bAREA\b/ ) { cannam@154: my $align; cannam@154: $align = "2"; cannam@154: if ( /ALIGN=(\d+)/ ) { cannam@154: $align = $1; cannam@154: } cannam@154: if ( /CODE/ ) { cannam@154: $nxstack = 1; cannam@154: } cannam@154: s/^(.+)CODE(.+)READONLY(.*)/ .text/; cannam@154: s/^(.+)DATA(.+)READONLY(.*)/ .section .rdata/; cannam@154: s/^(.+)\|\|\.data\|\|(.+)/ .data/; cannam@154: s/^(.+)\|\|\.bss\|\|(.+)/ .bss/; cannam@154: s/$/; .p2align $align/; cannam@154: # Enable NEON instructions but don't produce a binary that requires cannam@154: # ARMv7. RVCT does not have equivalent directives, so we just do this cannam@154: # for all CODE areas. cannam@154: if ( /.text/ ) { cannam@154: # Separating .arch, .fpu, etc., by semicolons does not work (gas cannam@154: # thinks the semicolon is part of the arch name, even when there's cannam@154: # whitespace separating them). Sadly this means our line numbers cannam@154: # won't match the original source file (we could use the .line cannam@154: # directive, which is documented to be obsolete, but then gdb will cannam@154: # show the wrong line in the translated source file). cannam@154: s/$/; .arch armv7-a\n .fpu neon\n .object_arch armv4t/ unless ($apple); cannam@154: } cannam@154: } cannam@154: cannam@154: s/\|\|\.constdata\$(\d+)\|\|/.L_CONST$1/; # ||.constdata$3|| cannam@154: s/\|\|\.bss\$(\d+)\|\|/.L_BSS$1/; # ||.bss$2|| cannam@154: s/\|\|\.data\$(\d+)\|\|/.L_DATA$1/; # ||.data$2|| cannam@154: s/\|\|([a-zA-Z0-9_]+)\@([a-zA-Z0-9_]+)\|\|/@ $&/; cannam@154: s/^(\s+)\%(\s)/ .space $1/; cannam@154: cannam@154: s/\|(.+)\.(\d+)\|/\.$1_$2/; # |L80.123| -> .L80_123 cannam@154: s/\bCODE32\b/.code 32/ && do {$thumb = 0}; cannam@154: s/\bCODE16\b/.code 16/ && do {$thumb = 1}; cannam@154: if (/\bPROC\b/) cannam@154: { cannam@154: my $prefix; cannam@154: my $proc; cannam@154: /^([A-Za-z_\.]\w+)\b/; cannam@154: $proc = $1; cannam@154: $prefix = ""; cannam@154: if ($proc) cannam@154: { cannam@154: $prefix = $prefix.sprintf("\t.type\t%s, %%function", $proc) unless ($apple); cannam@154: # Make sure we $prefix isn't empty here (for the $apple case). cannam@154: # We handle mangling the label here, make sure it doesn't match cannam@154: # the label handling below (if $prefix would be empty). cannam@154: $prefix = $prefix."; "; cannam@154: push(@proc_stack, $proc); cannam@154: s/^[A-Za-z_\.]\w+/$symprefix$&:/; cannam@154: } cannam@154: $prefix = $prefix."\t.thumb_func; " if ($thumb); cannam@154: s/\bPROC\b/@ $&/; cannam@154: $_ = $prefix.$_; cannam@154: } cannam@154: s/^(\s*)(S|Q|SH|U|UQ|UH)ASX\b/$1$2ADDSUBX/; cannam@154: s/^(\s*)(S|Q|SH|U|UQ|UH)SAX\b/$1$2SUBADDX/; cannam@154: if (/\bENDP\b/) cannam@154: { cannam@154: my $proc; cannam@154: s/\bENDP\b/@ $&/; cannam@154: $proc = pop(@proc_stack); cannam@154: $_ = "\t.size $proc, .-$proc".$_ if ($proc && !$apple); cannam@154: } cannam@154: s/\bSUBT\b/@ $&/; cannam@154: s/\bDATA\b/@ $&/; # DATA directive is deprecated -- Asm guide, p.7-25 cannam@154: s/\bKEEP\b/@ $&/; cannam@154: s/\bEXPORTAS\b/@ $&/; cannam@154: s/\|\|(.)+\bEQU\b/@ $&/; cannam@154: s/\|\|([\w\$]+)\|\|/$1/; cannam@154: s/\bENTRY\b/@ $&/; cannam@154: s/\bASSERT\b/@ $&/; cannam@154: s/\bGBLL\b/@ $&/; cannam@154: s/\bGBLA\b/@ $&/; cannam@154: s/^\W+OPT\b/@ $&/; cannam@154: s/:OR:/|/g; cannam@154: s/:SHL:/<>/g; cannam@154: s/:AND:/&/g; cannam@154: s/:LAND:/&&/g; cannam@154: s/CPSR/cpsr/; cannam@154: s/SPSR/spsr/; cannam@154: s/ALIGN$/.balign 4/; cannam@154: s/ALIGN\s+([0-9x]+)$/.balign $1/; cannam@154: s/psr_cxsf/psr_all/; cannam@154: s/LTORG/.ltorg/; cannam@154: s/^([A-Za-z_]\w*)[ \t]+EQU/ .set $1,/; cannam@154: s/^([A-Za-z_]\w*)[ \t]+SETL/ .set $1,/; cannam@154: s/^([A-Za-z_]\w*)[ \t]+SETA/ .set $1,/; cannam@154: s/^([A-Za-z_]\w*)[ \t]+\*/ .set $1,/; cannam@154: cannam@154: # {PC} + 0xdeadfeed --> . + 0xdeadfeed cannam@154: s/\{PC\} \+/ \. +/; cannam@154: cannam@154: # Single hex constant on the line ! cannam@154: # cannam@154: # >>> NOTE <<< cannam@154: # Double-precision floats in gcc are always mixed-endian, which means cannam@154: # bytes in two words are little-endian, but words are big-endian. cannam@154: # So, 0x0000deadfeed0000 would be stored as 0x0000dead at low address cannam@154: # and 0xfeed0000 at high address. cannam@154: # cannam@154: s/\bDCFD\b[ \t]+0x([a-fA-F0-9]{8})([a-fA-F0-9]{8})/.long 0x$1, 0x$2/; cannam@154: # Only decimal constants on the line, no hex ! cannam@154: s/\bDCFD\b[ \t]+([0-9\.\-]+)/.double $1/; cannam@154: cannam@154: # Single hex constant on the line ! cannam@154: # s/\bDCFS\b[ \t]+0x([a-f0-9]{8})([a-f0-9]{8})/.long 0x$1, 0x$2/; cannam@154: # Only decimal constants on the line, no hex ! cannam@154: # s/\bDCFS\b[ \t]+([0-9\.\-]+)/.double $1/; cannam@154: s/\bDCFS[ \t]+0x/.word 0x/; cannam@154: s/\bDCFS\b/.float/; cannam@154: cannam@154: s/^([A-Za-z_]\w*)[ \t]+DCD/$1 .word/; cannam@154: s/\bDCD\b/.word/; cannam@154: s/^([A-Za-z_]\w*)[ \t]+DCW/$1 .short/; cannam@154: s/\bDCW\b/.short/; cannam@154: s/^([A-Za-z_]\w*)[ \t]+DCB/$1 .byte/; cannam@154: s/\bDCB\b/.byte/; cannam@154: s/^([A-Za-z_]\w*)[ \t]+\%/.comm $1,/; cannam@154: s/^[A-Za-z_\.]\w+/$&:/; cannam@154: s/^(\d+)/$1:/; cannam@154: s/\%(\d+)/$1b_or_f/; cannam@154: s/\%[Bb](\d+)/$1b/; cannam@154: s/\%[Ff](\d+)/$1f/; cannam@154: s/\%[Ff][Tt](\d+)/$1f/; cannam@154: s/&([\dA-Fa-f]+)/0x$1/; cannam@154: if ( /\b2_[01]+\b/ ) { cannam@154: s/\b2_([01]+)\b/conv$1&&&&/g; cannam@154: while ( /[01][01][01][01]&&&&/ ) { cannam@154: s/0000&&&&/&&&&0/g; cannam@154: s/0001&&&&/&&&&1/g; cannam@154: s/0010&&&&/&&&&2/g; cannam@154: s/0011&&&&/&&&&3/g; cannam@154: s/0100&&&&/&&&&4/g; cannam@154: s/0101&&&&/&&&&5/g; cannam@154: s/0110&&&&/&&&&6/g; cannam@154: s/0111&&&&/&&&&7/g; cannam@154: s/1000&&&&/&&&&8/g; cannam@154: s/1001&&&&/&&&&9/g; cannam@154: s/1010&&&&/&&&&A/g; cannam@154: s/1011&&&&/&&&&B/g; cannam@154: s/1100&&&&/&&&&C/g; cannam@154: s/1101&&&&/&&&&D/g; cannam@154: s/1110&&&&/&&&&E/g; cannam@154: s/1111&&&&/&&&&F/g; cannam@154: } cannam@154: s/000&&&&/&&&&0/g; cannam@154: s/001&&&&/&&&&1/g; cannam@154: s/010&&&&/&&&&2/g; cannam@154: s/011&&&&/&&&&3/g; cannam@154: s/100&&&&/&&&&4/g; cannam@154: s/101&&&&/&&&&5/g; cannam@154: s/110&&&&/&&&&6/g; cannam@154: s/111&&&&/&&&&7/g; cannam@154: s/00&&&&/&&&&0/g; cannam@154: s/01&&&&/&&&&1/g; cannam@154: s/10&&&&/&&&&2/g; cannam@154: s/11&&&&/&&&&3/g; cannam@154: s/0&&&&/&&&&0/g; cannam@154: s/1&&&&/&&&&1/g; cannam@154: s/conv&&&&/0x/g; cannam@154: } cannam@154: cannam@154: if ( /commandline/) cannam@154: { cannam@154: if( /-bigend/) cannam@154: { cannam@154: $bigend=1; cannam@154: } cannam@154: } cannam@154: cannam@154: if ( /\bDCDU\b/ ) cannam@154: { cannam@154: my $cmd=$_; cannam@154: my $value; cannam@154: my $prefix; cannam@154: my $w1; cannam@154: my $w2; cannam@154: my $w3; cannam@154: my $w4; cannam@154: cannam@154: s/\s+DCDU\b/@ $&/; cannam@154: cannam@154: $cmd =~ /\bDCDU\b\s+0x(\d+)/; cannam@154: $value = $1; cannam@154: $value =~ /(\w\w)(\w\w)(\w\w)(\w\w)/; cannam@154: $w1 = $1; cannam@154: $w2 = $2; cannam@154: $w3 = $3; cannam@154: $w4 = $4; cannam@154: cannam@154: if( $bigend ne "") cannam@154: { cannam@154: # big endian cannam@154: $prefix = "\t.byte\t0x".$w1.";". cannam@154: "\t.byte\t0x".$w2.";". cannam@154: "\t.byte\t0x".$w3.";". cannam@154: "\t.byte\t0x".$w4."; "; cannam@154: } cannam@154: else cannam@154: { cannam@154: # little endian cannam@154: $prefix = "\t.byte\t0x".$w4.";". cannam@154: "\t.byte\t0x".$w3.";". cannam@154: "\t.byte\t0x".$w2.";". cannam@154: "\t.byte\t0x".$w1."; "; cannam@154: } cannam@154: $_=$prefix.$_; cannam@154: } cannam@154: cannam@154: if ( /\badrl\b/i ) cannam@154: { cannam@154: s/\badrl\s+(\w+)\s*,\s*(\w+)/ldr $1,=$2/i; cannam@154: $addPadding = 1; cannam@154: } cannam@154: s/\bEND\b/@ END/; cannam@154: } continue { cannam@154: printf ("%s", $_) if $printit; cannam@154: if ($addPadding != 0) cannam@154: { cannam@154: printf (" mov r0,r0\n"); cannam@154: $addPadding = 0; cannam@154: } cannam@154: } cannam@154: #If we had a code section, mark that this object doesn't need an executable cannam@154: # stack. cannam@154: if ($nxstack && !$apple) { cannam@154: printf (" .section\t.note.GNU-stack,\"\",\%\%progbits\n"); cannam@154: }