ads2gas_apple.pl 5.49 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
#!/usr/bin/env perl
2
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
##
4 5 6 7 8 9
## This source code is subject to the terms of the BSD 2 Clause License and
## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
## was not distributed with this source code in the LICENSE file, you can
## obtain it at www.aomedia.org/license/software. If the Alliance for Open
## Media Patent License 1.0 was not distributed with this source code in the
## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
##
11

John Koleszar's avatar
John Koleszar committed
12

Johann's avatar
Johann committed
13
# ads2gas_apple.pl
John Koleszar's avatar
John Koleszar committed
14 15 16 17
# Author: Eric Fung (efung (at) acm.org)
#
# Convert ARM Developer Suite 1.0.1 syntax assembly source to GNU as format
#
Johann's avatar
Johann committed
18
# Usage: cat inputfile | perl ads2gas_apple.pl > outputfile
John Koleszar's avatar
John Koleszar committed
19
#
Johann's avatar
Johann committed
20 21 22 23 24 25 26

my $chromium = 0;

foreach my $arg (@ARGV) {
    $chromium = 1 if ($arg eq "-chromium");
}

John Koleszar's avatar
John Koleszar committed
27 28 29 30 31 32 33 34 35 36 37 38 39
print "@ This file was created from a .asm file\n";
print "@  using the ads2gas_apple.pl script.\n\n";
print "\t.set WIDE_REFERENCE, 0\n";
print "\t.set ARCHITECTURE, 5\n";
print "\t.set DO1STROUNDING, 0\n";

my %register_aliases;
my %macro_aliases;

my @mapping_list = ("\$0", "\$1", "\$2", "\$3", "\$4", "\$5", "\$6", "\$7", "\$8", "\$9");

my @incoming_array;

Johann's avatar
Johann committed
40 41
my @imported_functions;

John Koleszar's avatar
John Koleszar committed
42 43 44 45 46 47 48 49 50 51 52
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

while (<STDIN>)
{
Tero Rintaluoma's avatar
Tero Rintaluoma committed
53 54 55
    # Load and store alignment
    s/@/,:/g;

John Koleszar's avatar
John Koleszar committed
56
    # Comment character
Johann's avatar
Johann committed
57
    s/;/ @/g;
John Koleszar's avatar
John Koleszar committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    # Hexadecimal constants prefaced by 0x
    s/#&/#0x/g;

    # Convert :OR: to |
    s/:OR:/ | /g;

    # Convert :AND: to &
    s/:AND:/ & /g;

    # Convert :NOT: to ~
    s/:NOT:/ ~ /g;

    # Convert :SHL: to <<
    s/:SHL:/ << /g;

    # Convert :SHR: to >>
    s/:SHR:/ >> /g;

    # Convert ELSE to .else
78
    s/\bELSE\b/.else/g;
John Koleszar's avatar
John Koleszar committed
79 80

    # Convert ENDIF to .endif
81
    s/\bENDIF\b/.endif/g;
John Koleszar's avatar
John Koleszar committed
82 83

    # Convert ELSEIF to .elseif
84
    s/\bELSEIF\b/.elseif/g;
John Koleszar's avatar
John Koleszar committed
85 86

    # Convert LTORG to .ltorg
87
    s/\bLTORG\b/.ltorg/g;
John Koleszar's avatar
John Koleszar committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

    # Convert IF :DEF:to .if
    # gcc doesn't have the ability to do a conditional
    # if defined variable that is set by IF :DEF: on
    # armasm, so convert it to a normal .if and then
    # make sure to define a value elesewhere
    if (s/\bIF :DEF:\b/.if /g)
    {
        s/=/==/g;
    }

    # Convert IF to .if
    if (s/\bIF\b/.if/g)
    {
        s/=/==/g;
    }

    # Convert INCLUDE to .INCLUDE "file"
    s/INCLUDE(\s*)(.*)$/.include $1\"$2\"/;

    # Code directive (ARM vs Thumb)
    s/CODE([0-9][0-9])/.code $1/;

    # No AREA required
Johann's avatar
Johann committed
112 113 114 115
    # But ALIGNs in AREA must be obeyed
    s/^\s*AREA.*ALIGN=([0-9])$/.text\n.p2align $1/;
    # If no ALIGN, strip the AREA and align to 4 bytes
    s/^\s*AREA.*$/.text\n.p2align 2/;
John Koleszar's avatar
John Koleszar committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    # DCD to .word
    # This one is for incoming symbols
    s/DCD\s+\|(\w*)\|/.long $1/;

    # DCW to .short
    s/DCW\s+\|(\w*)\|/.short $1/;
    s/DCW(.*)/.short $1/;

    # Constants defined in scope
    s/DCD(.*)/.long $1/;
    s/DCB(.*)/.byte $1/;

    # Build a hash of all the register - alias pairs.
    if (s/(.*)RN(.*)/$1 .req $2/g)
    {
        $register_aliases{trim($1)} = trim($2);
        next;
    }

    while (($key, $value) = each(%register_aliases))
    {
        s/\b$key\b/$value/g;
    }

    # Make function visible to linker, and make additional symbol with
    # prepended underscore
    s/EXPORT\s+\|([\$\w]*)\|/.globl _$1\n\t.globl $1/;
Johann's avatar
Johann committed
144 145 146 147 148 149 150 151 152 153 154 155

    # Prepend imported functions with _
    if (s/IMPORT\s+\|([\$\w]*)\|/.globl $1/)
    {
        $function = trim($1);
        push(@imported_functions, $function);
    }

    foreach $function (@imported_functions)
    {
        s/$function/_$function/;
    }
John Koleszar's avatar
John Koleszar committed
156 157 158 159 160 161 162 163 164 165

    # No vertical bars required; make additional symbol with prepended
    # underscore
    s/^\|(\$?\w+)\|/_$1\n\t$1:/g;

    # Labels need trailing colon
#   s/^(\w+)/$1:/ if !/EQU/;
    # put the colon at the end of the line in the macro
    s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/;

Tero Rintaluoma's avatar
Tero Rintaluoma committed
166
    # ALIGN directive
167
    s/\bALIGN\b/.balign/g;
John Koleszar's avatar
John Koleszar committed
168 169 170 171 172 173 174 175 176 177 178 179

    # Strip ARM
    s/\sARM/@ ARM/g;

    # Strip REQUIRE8
    #s/\sREQUIRE8/@ REQUIRE8/g;
    s/\sREQUIRE8/@ /g;

    # Strip PRESERVE8
    s/\sPRESERVE8/@ PRESERVE8/g;

    # Strip PROC and ENDPROC
Johann's avatar
Johann committed
180 181
    s/\bPROC\b/@/g;
    s/\bENDP\b/@/g;
John Koleszar's avatar
John Koleszar committed
182 183 184 185 186

    # EQU directive
    s/(.*)EQU(.*)/.set $1, $2/;

    # Begin macro definition
187
    if (/\bMACRO\b/)
John Koleszar's avatar
John Koleszar committed
188 189 190 191 192 193 194 195 196 197
    {
        # Process next line down, which will be the macro definition
        $_ = <STDIN>;

        $trimmed = trim($_);

        # remove commas that are separating list
        $trimmed =~ s/,//g;

        # string to array
Johann's avatar
Johann committed
198
        @incoming_array = split(/\s+/, $trimmed);
John Koleszar's avatar
John Koleszar committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

        print ".macro @incoming_array[0]\n";

        # remove the first element, as that is the name of the macro
        shift (@incoming_array);

        @macro_aliases{@incoming_array} = @mapping_list;

        next;
    }

    while (($key, $value) = each(%macro_aliases))
    {
        $key =~ s/\$/\\\$/;
        s/$key\b/$value/g;
    }

    # For macros, use \ to reference formal params
#   s/\$/\\/g;                  # End macro definition
218
    s/\bMEND\b/.endm/;              # No need to tell it where to stop assembling
John Koleszar's avatar
John Koleszar committed
219
    next if /^\s*END\s*$/;
Johann's avatar
Johann committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233

    # Clang used by Chromium differs slightly from clang in XCode in what it
    # will accept in the assembly.
    if ($chromium) {
        s/qsubaddx/qsax/i;
        s/qaddsubx/qasx/i;
        s/ldrneb/ldrbne/i;
        s/ldrneh/ldrhne/i;
        s/(vqshrun\.s16 .*, \#)0$/${1}8/i;

        # http://llvm.org/bugs/show_bug.cgi?id=16022
        s/\.include/#include/;
    }

John Koleszar's avatar
John Koleszar committed
234 235
    print;
}