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
21print "@ This file was created from a .asm file\n";
22print "@  using the ads2gas_apple.pl script.\n\n";
23print "\t.syntax unified\n";
24
25my %register_aliases;
26my %macro_aliases;
27
28my @mapping_list = ("\$0", "\$1", "\$2", "\$3", "\$4", "\$5", "\$6", "\$7", "\$8", "\$9");
29
30my @incoming_array;
31
32my @imported_functions;
33
34# Perl trim function to remove whitespace from the start and end of the string
35sub trim($)
36{
37    my $string = shift;
38    $string =~ s/^\s+//;
39    $string =~ s/\s+$//;
40    return $string;
41}
42
43while (<STDIN>)
44{
45    # Load and store alignment
46    s/@/,:/g;
47
48    # Comment character
49    s/;/ @/g;
50
51    # Hexadecimal constants prefaced by 0x
52    s/#&/#0x/g;
53
54    # Convert :OR: to |
55    s/:OR:/ | /g;
56
57    # Convert :AND: to &
58    s/:AND:/ & /g;
59
60    # Convert :NOT: to ~
61    s/:NOT:/ ~ /g;
62
63    # Convert :SHL: to <<
64    s/:SHL:/ << /g;
65
66    # Convert :SHR: to >>
67    s/:SHR:/ >> /g;
68
69    # Convert ELSE to .else
70    s/\bELSE\b/.else/g;
71
72    # Convert ENDIF to .endif
73    s/\bENDIF\b/.endif/g;
74
75    # Convert ELSEIF to .elseif
76    s/\bELSEIF\b/.elseif/g;
77
78    # Convert LTORG to .ltorg
79    s/\bLTORG\b/.ltorg/g;
80
81    # Convert IF :DEF:to .if
82    # gcc doesn't have the ability to do a conditional
83    # if defined variable that is set by IF :DEF: on
84    # armasm, so convert it to a normal .if and then
85    # make sure to define a value elesewhere
86    if (s/\bIF :DEF:\b/.if /g)
87    {
88        s/=/==/g;
89    }
90
91    # Convert IF to .if
92    if (s/\bIF\b/.if/g)
93    {
94        s/=/==/g;
95    }
96
97    # Convert INCLUDE to .INCLUDE "file"
98    s/INCLUDE(\s*)(.*)$/.include $1\"$2\"/;
99
100    # Code directive (ARM vs Thumb)
101    s/CODE([0-9][0-9])/.code $1/;
102
103    # No AREA required
104    # But ALIGNs in AREA must be obeyed
105    s/^\s*AREA.*ALIGN=([0-9])$/.text\n.p2align $1/;
106    # If no ALIGN, strip the AREA and align to 4 bytes
107    s/^\s*AREA.*$/.text\n.p2align 2/;
108
109    # DCD to .word
110    # This one is for incoming symbols
111    s/DCD\s+\|(\w*)\|/.long $1/;
112
113    # DCW to .short
114    s/DCW\s+\|(\w*)\|/.short $1/;
115    s/DCW(.*)/.short $1/;
116
117    # Constants defined in scope
118    s/DCD(.*)/.long $1/;
119    s/DCB(.*)/.byte $1/;
120
121    # Make function visible to linker, and make additional symbol with
122    # prepended underscore
123    s/EXPORT\s+\|([\$\w]*)\|/.globl _$1\n\t.globl $1/;
124
125    # Prepend imported functions with _
126    if (s/IMPORT\s+\|([\$\w]*)\|/.globl $1/)
127    {
128        $function = trim($1);
129        push(@imported_functions, $function);
130    }
131
132    foreach $function (@imported_functions)
133    {
134        s/$function/_$function/;
135    }
136
137    # No vertical bars required; make additional symbol with prepended
138    # underscore
139    s/^\|(\$?\w+)\|/_$1\n\t$1:/g;
140
141    # Labels need trailing colon
142#   s/^(\w+)/$1:/ if !/EQU/;
143    # put the colon at the end of the line in the macro
144    s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/;
145
146    # ALIGN directive
147    s/\bALIGN\b/.balign/g;
148
149    # Strip ARM
150    s/\sARM/@ ARM/g;
151
152    # Strip REQUIRE8
153    #s/\sREQUIRE8/@ REQUIRE8/g;
154    s/\sREQUIRE8/@ /g;
155
156    # Strip PRESERVE8
157    s/\sPRESERVE8/@ PRESERVE8/g;
158
159    # Strip PROC and ENDPROC
160    s/\bPROC\b/@/g;
161    s/\bENDP\b/@/g;
162
163    # EQU directive
164    s/(.*)EQU(.*)/.set $1, $2/;
165
166    # Begin macro definition
167    if (/\bMACRO\b/)
168    {
169        # Process next line down, which will be the macro definition
170        $_ = <STDIN>;
171
172        $trimmed = trim($_);
173
174        # remove commas that are separating list
175        $trimmed =~ s/,//g;
176
177        # string to array
178        @incoming_array = split(/\s+/, $trimmed);
179
180        print ".macro @incoming_array[0]\n";
181
182        # remove the first element, as that is the name of the macro
183        shift (@incoming_array);
184
185        @macro_aliases{@incoming_array} = @mapping_list;
186
187        next;
188    }
189
190    while (($key, $value) = each(%macro_aliases))
191    {
192        $key =~ s/\$/\\\$/;
193        s/$key\b/$value/g;
194    }
195
196    # For macros, use \ to reference formal params
197#   s/\$/\\/g;                  # End macro definition
198    s/\bMEND\b/.endm/;              # No need to tell it where to stop assembling
199    next if /^\s*END\s*$/;
200
201    print;
202}
203