blob: 92e7cf1ae20894e212e60a3817060d697ba6a23d [file] [log] [blame]
tlegrand@google.com36fa2622014-03-13 10:08:26 +00001#!/usr/bin/perl
2# Copyright (C) 2002-2013 Xiph.org Foundation
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7#
8# - Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#
11# - Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27my $bigend; # little/big endian
28my $nxstack;
29
30$nxstack = 0;
31
32eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
33 if $running_under_some_shell;
34
35while ($ARGV[0] =~ /^-/) {
36 $_ = shift;
37 last if /^--/;
38 if (/^-n/) {
39 $nflag++;
40 next;
41 }
42 die "I don't recognize this switch: $_\\n";
43}
44$printit++ unless $nflag;
45
46$\ = "\n"; # automatically add newline on print
47$n=0;
48
49$thumb = 0; # ARM mode by default, not Thumb.
50@proc_stack = ();
51
52LINE:
53while (<>) {
54
55 # For ADRLs we need to add a new line after the substituted one.
56 $addPadding = 0;
57
58 # First, we do not dare to touch *anything* inside double quotes, do we?
59 # Second, if you want a dollar character in the string,
60 # insert two of them -- that's how ARM C and assembler treat strings.
61 s/^([A-Za-z_]\w*)[ \t]+DCB[ \t]*\"/$1: .ascii \"/ && do { s/\$\$/\$/g; next };
62 s/\bDCB\b[ \t]*\"/.ascii \"/ && do { s/\$\$/\$/g; next };
63 s/^(\S+)\s+RN\s+(\S+)/$1 .req r$2/ && do { s/\$\$/\$/g; next };
64 # If there's nothing on a line but a comment, don't try to apply any further
65 # substitutions (this is a cheap hack to avoid mucking up the license header)
66 s/^([ \t]*);/$1@/ && do { s/\$\$/\$/g; next };
67 # If substituted -- leave immediately !
68
69 s/@/,:/;
70 s/;/@/;
71 while ( /@.*'/ ) {
72 s/(@.*)'/$1/g;
73 }
74 s/\{FALSE\}/0/g;
75 s/\{TRUE\}/1/g;
76 s/\{(\w\w\w\w+)\}/$1/g;
77 s/\bINCLUDE[ \t]*([^ \t\n]+)/.include \"$1\"/;
78 s/\bGET[ \t]*([^ \t\n]+)/.include \"${ my $x=$1; $x =~ s|\.s|-gnu.S|; \$x }\"/;
79 s/\bIMPORT\b/.extern/;
80 s/\bEXPORT\b/.global/;
81 s/^(\s+)\[/$1IF/;
82 s/^(\s+)\|/$1ELSE/;
83 s/^(\s+)\]/$1ENDIF/;
84 s/IF *:DEF:/ .ifdef/;
85 s/IF *:LNOT: *:DEF:/ .ifndef/;
86 s/ELSE/ .else/;
87 s/ENDIF/ .endif/;
88
89 if( /\bIF\b/ ) {
90 s/\bIF\b/ .if/;
91 s/=/==/;
92 }
93 if ( $n == 2) {
94 s/\$/\\/g;
95 }
96 if ($n == 1) {
97 s/\$//g;
98 s/label//g;
99 $n = 2;
100 }
101 if ( /MACRO/ ) {
102 s/MACRO *\n/.macro/;
103 $n=1;
104 }
105 if ( /\bMEND\b/ ) {
106 s/\bMEND\b/.endm/;
107 $n=0;
108 }
109
110 # ".rdata" doesn't work in 'as' version 2.13.2, as it is ".rodata" there.
111 #
112 if ( /\bAREA\b/ ) {
113 my $align;
114 $align = "2";
115 if ( /ALIGN=(\d+)/ ) {
116 $align = $1;
117 }
118 if ( /CODE/ ) {
119 $nxstack = 1;
120 }
121 s/^(.+)CODE(.+)READONLY(.*)/ .text/;
122 s/^(.+)DATA(.+)READONLY(.*)/ .section .rdata/;
123 s/^(.+)\|\|\.data\|\|(.+)/ .data/;
124 s/^(.+)\|\|\.bss\|\|(.+)/ .bss/;
125 s/$/; .p2align $align/;
126 # Enable NEON instructions but don't produce a binary that requires
127 # ARMv7. RVCT does not have equivalent directives, so we just do this
128 # for all CODE areas.
129 if ( /.text/ ) {
130 # Separating .arch, .fpu, etc., by semicolons does not work (gas
131 # thinks the semicolon is part of the arch name, even when there's
132 # whitespace separating them). Sadly this means our line numbers
133 # won't match the original source file (we could use the .line
134 # directive, which is documented to be obsolete, but then gdb will
135 # show the wrong line in the translated source file).
136 s/$/; .arch armv7-a\n .fpu neon\n .object_arch armv4t/;
137 }
138 }
139
140 s/\|\|\.constdata\$(\d+)\|\|/.L_CONST$1/; # ||.constdata$3||
141 s/\|\|\.bss\$(\d+)\|\|/.L_BSS$1/; # ||.bss$2||
142 s/\|\|\.data\$(\d+)\|\|/.L_DATA$1/; # ||.data$2||
143 s/\|\|([a-zA-Z0-9_]+)\@([a-zA-Z0-9_]+)\|\|/@ $&/;
144 s/^(\s+)\%(\s)/ .space $1/;
145
146 s/\|(.+)\.(\d+)\|/\.$1_$2/; # |L80.123| -> .L80_123
147 s/\bCODE32\b/.code 32/ && do {$thumb = 0};
148 s/\bCODE16\b/.code 16/ && do {$thumb = 1};
149 if (/\bPROC\b/)
150 {
151 my $prefix;
152 my $proc;
153 /^([A-Za-z_\.]\w+)\b/;
154 $proc = $1;
155 $prefix = "";
156 if ($proc)
157 {
158 $prefix = $prefix.sprintf("\t.type\t%s, %%function; ",$proc);
159 push(@proc_stack, $proc);
160 s/^[A-Za-z_\.]\w+/$&:/;
161 }
162 $prefix = $prefix."\t.thumb_func; " if ($thumb);
163 s/\bPROC\b/@ $&/;
164 $_ = $prefix.$_;
165 }
166 s/^(\s*)(S|Q|SH|U|UQ|UH)ASX\b/$1$2ADDSUBX/;
167 s/^(\s*)(S|Q|SH|U|UQ|UH)SAX\b/$1$2SUBADDX/;
168 if (/\bENDP\b/)
169 {
170 my $proc;
171 s/\bENDP\b/@ $&/;
172 $proc = pop(@proc_stack);
173 $_ = "\t.size $proc, .-$proc".$_ if ($proc);
174 }
175 s/\bSUBT\b/@ $&/;
176 s/\bDATA\b/@ $&/; # DATA directive is deprecated -- Asm guide, p.7-25
177 s/\bKEEP\b/@ $&/;
178 s/\bEXPORTAS\b/@ $&/;
179 s/\|\|(.)+\bEQU\b/@ $&/;
180 s/\|\|([\w\$]+)\|\|/$1/;
181 s/\bENTRY\b/@ $&/;
182 s/\bASSERT\b/@ $&/;
183 s/\bGBLL\b/@ $&/;
184 s/\bGBLA\b/@ $&/;
185 s/^\W+OPT\b/@ $&/;
186 s/:OR:/|/g;
187 s/:SHL:/<</g;
188 s/:SHR:/>>/g;
189 s/:AND:/&/g;
190 s/:LAND:/&&/g;
191 s/CPSR/cpsr/;
192 s/SPSR/spsr/;
193 s/ALIGN$/.balign 4/;
194 s/ALIGN\s+([0-9x]+)$/.balign $1/;
195 s/psr_cxsf/psr_all/;
196 s/LTORG/.ltorg/;
197 s/^([A-Za-z_]\w*)[ \t]+EQU/ .set $1,/;
198 s/^([A-Za-z_]\w*)[ \t]+SETL/ .set $1,/;
199 s/^([A-Za-z_]\w*)[ \t]+SETA/ .set $1,/;
200 s/^([A-Za-z_]\w*)[ \t]+\*/ .set $1,/;
201
202 # {PC} + 0xdeadfeed --> . + 0xdeadfeed
203 s/\{PC\} \+/ \. +/;
204
205 # Single hex constant on the line !
206 #
207 # >>> NOTE <<<
208 # Double-precision floats in gcc are always mixed-endian, which means
209 # bytes in two words are little-endian, but words are big-endian.
210 # So, 0x0000deadfeed0000 would be stored as 0x0000dead at low address
211 # and 0xfeed0000 at high address.
212 #
213 s/\bDCFD\b[ \t]+0x([a-fA-F0-9]{8})([a-fA-F0-9]{8})/.long 0x$1, 0x$2/;
214 # Only decimal constants on the line, no hex !
215 s/\bDCFD\b[ \t]+([0-9\.\-]+)/.double $1/;
216
217 # Single hex constant on the line !
218# s/\bDCFS\b[ \t]+0x([a-f0-9]{8})([a-f0-9]{8})/.long 0x$1, 0x$2/;
219 # Only decimal constants on the line, no hex !
220# s/\bDCFS\b[ \t]+([0-9\.\-]+)/.double $1/;
221 s/\bDCFS[ \t]+0x/.word 0x/;
222 s/\bDCFS\b/.float/;
223
224 s/^([A-Za-z_]\w*)[ \t]+DCD/$1 .word/;
225 s/\bDCD\b/.word/;
226 s/^([A-Za-z_]\w*)[ \t]+DCW/$1 .short/;
227 s/\bDCW\b/.short/;
228 s/^([A-Za-z_]\w*)[ \t]+DCB/$1 .byte/;
229 s/\bDCB\b/.byte/;
230 s/^([A-Za-z_]\w*)[ \t]+\%/.comm $1,/;
231 s/^[A-Za-z_\.]\w+/$&:/;
232 s/^(\d+)/$1:/;
233 s/\%(\d+)/$1b_or_f/;
234 s/\%[Bb](\d+)/$1b/;
235 s/\%[Ff](\d+)/$1f/;
236 s/\%[Ff][Tt](\d+)/$1f/;
237 s/&([\dA-Fa-f]+)/0x$1/;
238 if ( /\b2_[01]+\b/ ) {
239 s/\b2_([01]+)\b/conv$1&&&&/g;
240 while ( /[01][01][01][01]&&&&/ ) {
241 s/0000&&&&/&&&&0/g;
242 s/0001&&&&/&&&&1/g;
243 s/0010&&&&/&&&&2/g;
244 s/0011&&&&/&&&&3/g;
245 s/0100&&&&/&&&&4/g;
246 s/0101&&&&/&&&&5/g;
247 s/0110&&&&/&&&&6/g;
248 s/0111&&&&/&&&&7/g;
249 s/1000&&&&/&&&&8/g;
250 s/1001&&&&/&&&&9/g;
251 s/1010&&&&/&&&&A/g;
252 s/1011&&&&/&&&&B/g;
253 s/1100&&&&/&&&&C/g;
254 s/1101&&&&/&&&&D/g;
255 s/1110&&&&/&&&&E/g;
256 s/1111&&&&/&&&&F/g;
257 }
258 s/000&&&&/&&&&0/g;
259 s/001&&&&/&&&&1/g;
260 s/010&&&&/&&&&2/g;
261 s/011&&&&/&&&&3/g;
262 s/100&&&&/&&&&4/g;
263 s/101&&&&/&&&&5/g;
264 s/110&&&&/&&&&6/g;
265 s/111&&&&/&&&&7/g;
266 s/00&&&&/&&&&0/g;
267 s/01&&&&/&&&&1/g;
268 s/10&&&&/&&&&2/g;
269 s/11&&&&/&&&&3/g;
270 s/0&&&&/&&&&0/g;
271 s/1&&&&/&&&&1/g;
272 s/conv&&&&/0x/g;
273 }
274
275 if ( /commandline/)
276 {
277 if( /-bigend/)
278 {
279 $bigend=1;
280 }
281 }
282
283 if ( /\bDCDU\b/ )
284 {
285 my $cmd=$_;
286 my $value;
287 my $prefix;
288 my $w1;
289 my $w2;
290 my $w3;
291 my $w4;
292
293 s/\s+DCDU\b/@ $&/;
294
295 $cmd =~ /\bDCDU\b\s+0x(\d+)/;
296 $value = $1;
297 $value =~ /(\w\w)(\w\w)(\w\w)(\w\w)/;
298 $w1 = $1;
299 $w2 = $2;
300 $w3 = $3;
301 $w4 = $4;
302
303 if( $bigend ne "")
304 {
305 # big endian
306 $prefix = "\t.byte\t0x".$w1.";".
307 "\t.byte\t0x".$w2.";".
308 "\t.byte\t0x".$w3.";".
309 "\t.byte\t0x".$w4."; ";
310 }
311 else
312 {
313 # little endian
314 $prefix = "\t.byte\t0x".$w4.";".
315 "\t.byte\t0x".$w3.";".
316 "\t.byte\t0x".$w2.";".
317 "\t.byte\t0x".$w1."; ";
318 }
319 $_=$prefix.$_;
320 }
321
322 if ( /\badrl\b/i )
323 {
324 s/\badrl\s+(\w+)\s*,\s*(\w+)/ldr $1,=$2/i;
325 $addPadding = 1;
326 }
327 s/\bEND\b/@ END/;
328} continue {
329 printf ("%s", $_) if $printit;
330 if ($addPadding != 0)
331 {
332 printf (" mov r0,r0\n");
333 $addPadding = 0;
334 }
335}
336#If we had a code section, mark that this object doesn't need an executable
337# stack.
338if ($nxstack) {
339 printf (" .section\t.note.GNU-stack,\"\",\%\%progbits\n");
340}