arm: Clear the gp register alias at the end of functions
[libav.git] / libavutil / arm / asm.S
1 /*
2 * Copyright (c) 2008 Mans Rullgard <mans@mansr.com>
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22
23 #ifdef __ELF__
24 # define ELF
25 #else
26 # define ELF @
27 #endif
28
29 #if CONFIG_THUMB
30 # define A @
31 # define T
32 #else
33 # define A
34 # define T @
35 #endif
36
37 #if HAVE_AS_FUNC
38 # define FUNC
39 #else
40 # define FUNC @
41 #endif
42
43 #if HAVE_AS_FPU_DIRECTIVE
44 # define FPU
45 #else
46 # define FPU @
47 #endif
48
49 #if HAVE_NEON
50 .arch armv7-a
51 #elif HAVE_ARMV6T2
52 .arch armv6t2
53 #elif HAVE_ARMV6
54 .arch armv6
55 #elif HAVE_ARMV5TE
56 .arch armv5te
57 #endif
58 #if HAVE_AS_OBJECT_ARCH
59 ELF .object_arch armv4
60 #endif
61
62 #if HAVE_NEON
63 FPU .fpu neon
64 ELF .eabi_attribute 10, 0 @ suppress Tag_FP_arch
65 ELF .eabi_attribute 12, 0 @ suppress Tag_Advanced_SIMD_arch
66 #elif HAVE_VFP
67 FPU .fpu vfp
68 ELF .eabi_attribute 10, 0 @ suppress Tag_FP_arch
69 #endif
70
71 .syntax unified
72 T .thumb
73 ELF .eabi_attribute 25, 1 @ Tag_ABI_align_preserved
74 ELF .section .note.GNU-stack,"",%progbits @ Mark stack as non-executable
75
76 .macro function name, export=0, align=2
77 .set .Lpic_idx, 0
78 .set .Lpic_gp, 0
79 .macro endfunc
80 .if .Lpic_idx
81 .align 2
82 .altmacro
83 put_pic %(.Lpic_idx - 1)
84 .noaltmacro
85 .endif
86 .if .Lpic_gp
87 .unreq gp
88 .endif
89 ELF .size \name, . - \name
90 FUNC .endfunc
91 .purgem endfunc
92 .endm
93 .text
94 .align \align
95 .if \export
96 .global EXTERN_ASM\name
97 ELF .type EXTERN_ASM\name, %function
98 FUNC .func EXTERN_ASM\name
99 EXTERN_ASM\name:
100 .else
101 ELF .type \name, %function
102 FUNC .func \name
103 \name:
104 .endif
105 .endm
106
107 .macro const name, align=2, relocate=0
108 .macro endconst
109 ELF .size \name, . - \name
110 .purgem endconst
111 .endm
112 .if HAVE_SECTION_DATA_REL_RO && \relocate
113 .section .data.rel.ro
114 .else
115 .section .rodata
116 .endif
117 .align \align
118 \name:
119 .endm
120
121 #if !HAVE_ARMV6T2_EXTERNAL
122 .macro movw rd, val
123 mov \rd, \val & 255
124 orr \rd, \val & ~255
125 .endm
126 #endif
127
128 .macro mov32 rd, val
129 #if HAVE_ARMV6T2_EXTERNAL
130 movw \rd, #(\val) & 0xffff
131 .if (\val) >> 16
132 movt \rd, #(\val) >> 16
133 .endif
134 #else
135 ldr \rd, =\val
136 #endif
137 .endm
138
139 .macro put_pic num
140 put_pic_\num
141 .endm
142
143 .macro do_def_pic num, val, label
144 .macro put_pic_\num
145 .if \num
146 .altmacro
147 put_pic %(\num - 1)
148 .noaltmacro
149 .endif
150 \label: .word \val
151 .purgem put_pic_\num
152 .endm
153 .endm
154
155 .macro def_pic val, label
156 .altmacro
157 do_def_pic %.Lpic_idx, \val, \label
158 .noaltmacro
159 .set .Lpic_idx, .Lpic_idx + 1
160 .endm
161
162 .macro ldpic rd, val, indir=0
163 ldr \rd, .Lpicoff\@
164 .Lpic\@:
165 .if \indir
166 A ldr \rd, [pc, \rd]
167 T add \rd, pc
168 T ldr \rd, [\rd]
169 .else
170 add \rd, pc
171 .endif
172 def_pic \val - (.Lpic\@ + (8 >> CONFIG_THUMB)), .Lpicoff\@
173 .endm
174
175 .macro movrel rd, val
176 #if CONFIG_PIC
177 ldpic \rd, \val
178 #elif HAVE_ARMV6T2_EXTERNAL && !defined(__APPLE__)
179 movw \rd, #:lower16:\val
180 movt \rd, #:upper16:\val
181 #else
182 ldr \rd, =\val
183 #endif
184 .endm
185
186 .macro movrelx rd, val, gp
187 #if CONFIG_PIC && defined(__ELF__)
188 .ifnb \gp
189 .if .Lpic_gp
190 .unreq gp
191 .endif
192 gp .req \gp
193 ldpic gp, _GLOBAL_OFFSET_TABLE_
194 .elseif !.Lpic_gp
195 gp .req r12
196 ldpic gp, _GLOBAL_OFFSET_TABLE_
197 .endif
198 .set .Lpic_gp, 1
199 ldr \rd, .Lpicoff\@
200 ldr \rd, [gp, \rd]
201 def_pic \val(GOT), .Lpicoff\@
202 #elif CONFIG_PIC && defined(__APPLE__)
203 ldpic \rd, .Lpic\@, indir=1
204 .non_lazy_symbol_pointer
205 .Lpic\@:
206 .indirect_symbol \val
207 .word 0
208 .text
209 #else
210 movrel \rd, \val
211 #endif
212 .endm
213
214 .macro add_sh rd, rn, rm, sh:vararg
215 A add \rd, \rn, \rm, \sh
216 T mov \rm, \rm, \sh
217 T add \rd, \rn, \rm
218 .endm
219
220 .macro ldr_pre rt, rn, rm:vararg
221 A ldr \rt, [\rn, \rm]!
222 T add \rn, \rn, \rm
223 T ldr \rt, [\rn]
224 .endm
225
226 .macro ldr_dpre rt, rn, rm:vararg
227 A ldr \rt, [\rn, -\rm]!
228 T sub \rn, \rn, \rm
229 T ldr \rt, [\rn]
230 .endm
231
232 .macro ldr_nreg rt, rn, rm:vararg
233 A ldr \rt, [\rn, -\rm]
234 T sub \rt, \rn, \rm
235 T ldr \rt, [\rt]
236 .endm
237
238 .macro ldr_post rt, rn, rm:vararg
239 A ldr \rt, [\rn], \rm
240 T ldr \rt, [\rn]
241 T add \rn, \rn, \rm
242 .endm
243
244 .macro ldrc_pre cc, rt, rn, rm:vararg
245 A ldr\cc \rt, [\rn, \rm]!
246 T itt \cc
247 T add\cc \rn, \rn, \rm
248 T ldr\cc \rt, [\rn]
249 .endm
250
251 .macro ldrd_reg rt, rt2, rn, rm
252 A ldrd \rt, \rt2, [\rn, \rm]
253 T add \rt, \rn, \rm
254 T ldrd \rt, \rt2, [\rt]
255 .endm
256
257 .macro ldrd_post rt, rt2, rn, rm
258 A ldrd \rt, \rt2, [\rn], \rm
259 T ldrd \rt, \rt2, [\rn]
260 T add \rn, \rn, \rm
261 .endm
262
263 .macro ldrh_pre rt, rn, rm
264 A ldrh \rt, [\rn, \rm]!
265 T add \rn, \rn, \rm
266 T ldrh \rt, [\rn]
267 .endm
268
269 .macro ldrh_dpre rt, rn, rm
270 A ldrh \rt, [\rn, -\rm]!
271 T sub \rn, \rn, \rm
272 T ldrh \rt, [\rn]
273 .endm
274
275 .macro ldrh_post rt, rn, rm
276 A ldrh \rt, [\rn], \rm
277 T ldrh \rt, [\rn]
278 T add \rn, \rn, \rm
279 .endm
280
281 .macro ldrb_post rt, rn, rm
282 A ldrb \rt, [\rn], \rm
283 T ldrb \rt, [\rn]
284 T add \rn, \rn, \rm
285 .endm
286
287 .macro str_post rt, rn, rm:vararg
288 A str \rt, [\rn], \rm
289 T str \rt, [\rn]
290 T add \rn, \rn, \rm
291 .endm
292
293 .macro strb_post rt, rn, rm:vararg
294 A strb \rt, [\rn], \rm
295 T strb \rt, [\rn]
296 T add \rn, \rn, \rm
297 .endm
298
299 .macro strd_post rt, rt2, rn, rm
300 A strd \rt, \rt2, [\rn], \rm
301 T strd \rt, \rt2, [\rn]
302 T add \rn, \rn, \rm
303 .endm
304
305 .macro strh_pre rt, rn, rm
306 A strh \rt, [\rn, \rm]!
307 T add \rn, \rn, \rm
308 T strh \rt, [\rn]
309 .endm
310
311 .macro strh_dpre rt, rn, rm
312 A strh \rt, [\rn, -\rm]!
313 T sub \rn, \rn, \rm
314 T strh \rt, [\rn]
315 .endm
316
317 .macro strh_post rt, rn, rm
318 A strh \rt, [\rn], \rm
319 T strh \rt, [\rn]
320 T add \rn, \rn, \rm
321 .endm
322
323 .macro strh_dpost rt, rn, rm
324 A strh \rt, [\rn], -\rm
325 T strh \rt, [\rn]
326 T sub \rn, \rn, \rm
327 .endm
328
329 #if HAVE_VFP_ARGS
330 ELF .eabi_attribute 28, 1
331 # define VFP
332 # define NOVFP @
333 #else
334 # define VFP @
335 # define NOVFP
336 #endif
337
338 #define GLUE(a, b) a ## b
339 #define JOIN(a, b) GLUE(a, b)
340 #define X(s) JOIN(EXTERN_ASM, s)