summaryrefslogtreecommitdiffstats
path: root/media/libvpx/libvpx/build/make/ads2gas.pl
blob: c301b7f82993e5c2382bfc457f5784234e14f73d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env perl
##
##  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
##
##  Use of this source code is governed by a BSD-style license
##  that can be found in the LICENSE file in the root of the source
##  tree. An additional intellectual property rights grant can be found
##  in the file PATENTS.  All contributing project authors may
##  be found in the AUTHORS file in the root of the source tree.
##


# ads2gas.pl
# Author: Eric Fung (efung (at) acm.org)
#
# Convert ARM Developer Suite 1.0.1 syntax assembly source to GNU as format
#
# Usage: cat inputfile | perl ads2gas.pl > outputfile
#

use FindBin;
use lib $FindBin::Bin;
use thumb;

my $thumb = 0;
my $elf = 1;

foreach my $arg (@ARGV) {
    $thumb = 1 if ($arg eq "-thumb");
    $elf = 0 if ($arg eq "-noelf");
}

print "@ This file was created from a .asm file\n";
print "@  using the ads2gas.pl script.\n";
print ".syntax unified\n";
if ($thumb) {
    print "\t.thumb\n";
}

# Stack of procedure names.
@proc_stack = ();

while (<STDIN>)
{
    # Load and store alignment
    s/@/,:/g;

    # Comment character
    s/;/@/;

    # Convert ELSE to .else
    s/\bELSE\b/.else/g;

    # Convert ENDIF to .endif
    s/\bENDIF\b/.endif/g;

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

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

    # No AREA required
    # But ALIGNs in AREA must be obeyed
    s/^(\s*)\bAREA\b.*ALIGN=([0-9])$/$1.text\n$1.p2align $2/;
    # If no ALIGN, strip the AREA and align to 4 bytes
    s/^(\s*)\bAREA\b.*$/$1.text\n$1.p2align 2/;

    # Make function visible to linker.
    if ($elf) {
        s/(\s*)EXPORT\s+\|([\$\w]*)\|/$1.global $2\n$1.type $2, function/;
    } else {
        s/(\s*)EXPORT\s+\|([\$\w]*)\|/$1.global $2/;
    }

    # No vertical bars on function names
    s/^\|(\$?\w+)\|/$1/g;

    # Labels need trailing colon
    s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/;

    # ALIGN directive
    s/\bALIGN\b/.balign/g;

    if ($thumb) {
        # ARM code - we force everything to thumb with the declaration in the
        # header
        s/\bARM\b//g;
    } else {
        # ARM code
        s/\bARM\b/.arm/g;
    }

    # push/pop
    s/(push\s+)(r\d+)/stmdb sp\!, \{$2\}/g;
    s/(pop\s+)(r\d+)/ldmia sp\!, \{$2\}/g;

    if ($thumb) {
        thumb::FixThumbInstructions($_);
    }

    # eabi_attributes numerical equivalents can be found in the
    # "ARM IHI 0045C" document.

    if ($elf) {
        # REQUIRE8 Stack is required to be 8-byte aligned
        s/\bREQUIRE8\b/.eabi_attribute 24, 1 \@Tag_ABI_align_needed/g;

        # PRESERVE8 Stack 8-byte align is preserved
        s/\bPRESERVE8\b/.eabi_attribute 25, 1 \@Tag_ABI_align_preserved/g;
    } else {
        s/\bREQUIRE8\b//;
        s/\bPRESERVE8\b//;
    }

    # Use PROC and ENDP to give the symbols a .size directive.
    # This makes them show up properly in debugging tools like gdb and valgrind.
    if (/\bPROC\b/) {
        my $proc;
        # Match the function name so it can be stored in $proc
        /^([\.0-9A-Z_a-z]\w+)\b/;
        $proc = $1;
        push(@proc_stack, $proc) if ($proc);
        s/\bPROC\b/@ $&/;
    }

    if (/\bENDP\b/) {
        my $proc;
        s/\bENDP\b/@ $&/;
        $proc = pop(@proc_stack);
        $_ = ".size $proc, .-$proc".$_ if ($proc and $elf);
    }

    # EQU directive
    s/(\S+\s+)EQU(\s+\S+)/.equ $1, $2/;

    # Begin macro definition
    if (/\bMACRO\b/) {
        # Process next line down, which will be the macro definition
        $_ = <STDIN>;
        s/^/.macro/;
        s/\$//g;             # Remove $ from the variables in the declaration
    }

    s/\$/\\/g;               # Use \ to reference formal parameters
    # End macro definition

    s/\bMEND\b/.endm/;       # No need to tell it where to stop assembling
    next if /^\s*END\s*$/;
    s/[ \t]+$//;
    print;
}

# Mark that this object doesn't need an executable stack.
printf ("    .section .note.GNU-stack,\"\",\%\%progbits\n") if $elf;