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