media/libtheora/lib/arm/arm2gnu.pl

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial