1#!/usr/bin/env perl
2##
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##  that can be found in the LICENSE file in the root of the source
7##  tree. An additional intellectual property rights grant can be found
8##  in the file PATENTS.  All contributing project authors may
9##  be found in the AUTHORS file in the root of the source tree.
10##
11
12
13# ads2gas_apple.pl
14# Author: Eric Fung (efung (at) acm.org)
15#
16# Convert ARM Developer Suite 1.0.1 syntax assembly source to GNU as format
17#
18# Usage: cat inputfile | perl ads2gas_apple.pl > outputfile
19#
20
21my $chromium = 0;
22
23foreach my $arg (@ARGV) {
24    $chromium = 1 if ($arg eq "-chromium");
25}
26
27print "@ This file was created from a .asm file\n";
28print "@  using the ads2gas_apple.pl script.\n\n";
29print "\t.set WIDE_REFERENCE, 0\n";
30print "\t.set ARCHITECTURE, 5\n";
31print "\t.set DO1STROUNDING, 0\n";
32
33my %register_aliases;
34my %macro_aliases;
35
36my @mapping_list = ("\$0", "\$1", "\$2", "\$3", "\$4", "\$5", "\$6", "\$7", "\$8", "\$9");
37
38my @incoming_array;
39
40my @imported_functions;
41
42# Perl trim function to remove whitespace from the start and end of the string
43sub trim($)
44{
45    my $string = shift;
46    $string =~ s/^\s+//;
47    $string =~ s/\s+$//;
48    return $string;
49}
50
51while (<STDIN>)
52{
53    # Load and store alignment
54    s/@/,:/g;
55
56    # Comment character
57    s/;/ @/g;
58
59    # Hexadecimal constants prefaced by 0x
60    s/#&/#0x/g;
61
62    # Convert :OR: to |
63    s/:OR:/ | /g;
64
65    # Convert :AND: to &
66    s/:AND:/ & /g;
67
68    # Convert :NOT: to ~
69    s/:NOT:/ ~ /g;
70
71    # Convert :SHL: to <<
72    s/:SHL:/ << /g;
73
74    # Convert :SHR: to >>
75    s/:SHR:/ >> /g;
76
77    # Convert ELSE to .else
78    s/\bELSE\b/.else/g;
79
80    # Convert ENDIF to .endif
81    s/\bENDIF\b/.endif/g;
82
83    # Convert ELSEIF to .elseif
84    s/\bELSEIF\b/.elseif/g;
85
86    # Convert LTORG to .ltorg
87    s/\bLTORG\b/.ltorg/g;
88
89    # Convert IF :DEF:to .if
90    # gcc doesn't have the ability to do a conditional
91    # if defined variable that is set by IF :DEF: on
92    # armasm, so convert it to a normal .if and then
93    # make sure to define a value elesewhere
94    if (s/\bIF :DEF:\b/.if /g)
95    {
96        s/=/==/g;
97    }
98
99    # Convert IF to .if
100    if (s/\bIF\b/.if/g)
101    {
102        s/=/==/g;
103    }
104
105    # Convert INCLUDE to .INCLUDE "file"
106    s/INCLUDE(\s*)(.*)$/.include $1\"$2\"/;
107
108    # Code directive (ARM vs Thumb)
109    s/CODE([0-9][0-9])/.code $1/;
110
111    # No AREA required
112    # But ALIGNs in AREA must be obeyed
113    s/^\s*AREA.*ALIGN=([0-9])$/.text\n.p2align $1/;
114    # If no ALIGN, strip the AREA and align to 4 bytes
115    s/^\s*AREA.*$/.text\n.p2align 2/;
116
117    # DCD to .word
118    # This one is for incoming symbols
119    s/DCD\s+\|(\w*)\|/.long $1/;
120
121    # DCW to .short
122    s/DCW\s+\|(\w*)\|/.short $1/;
123    s/DCW(.*)/.short $1/;
124
125    # Constants defined in scope
126    s/DCD(.*)/.long $1/;
127    s/DCB(.*)/.byte $1/;
128
129    # Build a hash of all the register - alias pairs.
130    if (s/(.*)RN(.*)/$1 .req $2/g)
131    {
132        $register_aliases{trim($1)} = trim($2);
133        next;
134    }
135
136    while (($key, $value) = each(%register_aliases))
137    {
138        s/\b$key\b/$value/g;
139    }
140
141    # Make function visible to linker, and make additional symbol with
142    # prepended underscore
143    s/EXPORT\s+\|([\$\w]*)\|/.globl _$1\n\t.globl $1/;
144
145    # Prepend imported functions with _
146    if (s/IMPORT\s+\|([\$\w]*)\|/.globl $1/)
147    {
148        $function = trim($1);
149        push(@imported_functions, $function);
150    }
151
152    foreach $function (@imported_functions)
153    {
154        s/$function/_$function/;
155    }
156
157    # No vertical bars required; make additional symbol with prepended
158    # underscore
159    s/^\|(\$?\w+)\|/_$1\n\t$1:/g;
160
161    # Labels need trailing colon
162#   s/^(\w+)/$1:/ if !/EQU/;
163    # put the colon at the end of the line in the macro
164    s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/;
165
166    # ALIGN directive
167    s/\bALIGN\b/.balign/g;
168
169    # Strip ARM
170    s/\sARM/@ ARM/g;
171
172    # Strip REQUIRE8
173    #s/\sREQUIRE8/@ REQUIRE8/g;
174    s/\sREQUIRE8/@ /g;
175
176    # Strip PRESERVE8
177    s/\sPRESERVE8/@ PRESERVE8/g;
178
179    # Strip PROC and ENDPROC
180    s/\bPROC\b/@/g;
181    s/\bENDP\b/@/g;
182
183    # EQU directive
184    s/(.*)EQU(.*)/.set $1, $2/;
185
186    # Begin macro definition
187    if (/\bMACRO\b/)
188    {
189        # Process next line down, which will be the macro definition
190        $_ = <STDIN>;
191
192        $trimmed = trim($_);
193
194        # remove commas that are separating list
195        $trimmed =~ s/,//g;
196
197        # string to array
198        @incoming_array = split(/\s+/, $trimmed);
199
200        print ".macro @incoming_array[0]\n";
201
202        # remove the first element, as that is the name of the macro
203        shift (@incoming_array);
204
205        @macro_aliases{@incoming_array} = @mapping_list;
206
207        next;
208    }
209
210    while (($key, $value) = each(%macro_aliases))
211    {
212        $key =~ s/\$/\\\$/;
213        s/$key\b/$value/g;
214    }
215
216    # For macros, use \ to reference formal params
217#   s/\$/\\/g;                  # End macro definition
218    s/\bMEND\b/.endm/;              # No need to tell it where to stop assembling
219    next if /^\s*END\s*$/;
220
221    # Clang used by Chromium differs slightly from clang in XCode in what it
222    # will accept in the assembly.
223    if ($chromium) {
224        s/qsubaddx/qsax/i;
225        s/qaddsubx/qasx/i;
226        s/ldrneb/ldrbne/i;
227        s/ldrneh/ldrhne/i;
228        s/(vqshrun\.s16 .*, \#)0$/${1}8/i;
229
230        # http://llvm.org/bugs/show_bug.cgi?id=16022
231        s/\.include/#include/;
232    }
233
234    print;
235}
236