ads2gas.pl 3.77 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
2
#!/usr/bin/perl
##
3
##  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4
5
##
##  Use of this source code is governed by a BSD-style license
6
7
##  that can be found in the LICENSE file in the root of the source
##  tree. An additional intellectual property rights grant can be found
8
9
##  in the file PATENTS.  All contributing project authors may
##  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
##


# 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
#
print "@ This file was created from a .asm file\n";
print "@  using the ads2gas.pl script.\n";
print "\t.equ DO1STROUNDING, 0\n";

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

John Koleszar's avatar
John Koleszar committed
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
    # Comment character
    s/;/@/g;

    # 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
    s/ELSE/.else/g;

    # Convert ENDIF to .endif
    s/ENDIF/.endif/g;

    # Convert ELSEIF to .elseif
    s/ELSEIF/.elseif/g;

    # Convert LTORG to .ltorg
    s/LTORG/.ltorg/g;

    # 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
85
86
87
88
    # 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
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

    # 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/;

    # RN to .req
    if (s/RN\s+([Rr]\d+|lr)/.req $1/)
    {
        print;
        next;
    }

    # Make function visible to linker, and make additional symbol with
    # prepended underscore
    s/EXPORT\s+\|([\$\w]*)\|/.global $1 \n\t.type $1, function/;
    s/IMPORT\s+\|([\$\w]*)\|/.global $1/;

    # 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
123
124
    # ALIGN directive
    s/ALIGN/.balign/g;
John Koleszar's avatar
John Koleszar committed
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

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

    # Strip REQUIRE8
    #s/\sREQUIRE8/@ REQUIRE8/g;
    s/\sREQUIRE8/@ /g;      #EQU cause problem

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

    # Strip PROC and ENDPROC
    s/\sPROC/@/g;
    s/\sENDP/@/g;

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

    # Begin macro definition
    if (/MACRO/) {
        $_ = <STDIN>;
        s/^/.macro/;
        s/\$//g;                # remove formal param reference
        s/;/@/g;                # change comment characters
    }

    # For macros, use \ to reference formal params
    s/\$/\\/g;                  # End macro definition
    s/MEND/.endm/;              # No need to tell it where to stop assembling
    next if /^\s*END\s*$/;
    print;
}
157
158
159

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