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