Merge "clamp_mvs() using the wrong motion vector information"
[profile/ivi/libvpx.git] / vpx_ports / x86_abi_support.asm
1 ;
2 ;  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 ;
4 ;  Use of this source code is governed by a BSD-style license
5 ;  that can be found in the LICENSE file in the root of the source
6 ;  tree. An additional intellectual property rights grant can be found
7 ;  in the file PATENTS.  All contributing project authors may
8 ;  be found in the AUTHORS file in the root of the source tree.
9 ;
10
11
12 %include "vpx_config.asm"
13
14 ; 32/64 bit compatibility macros
15 ;
16 ; In general, we make the source use 64 bit syntax, then twiddle with it using
17 ; the preprocessor to get the 32 bit syntax on 32 bit platforms.
18 ;
19 %ifidn __OUTPUT_FORMAT__,elf32
20 %define ABI_IS_32BIT 1
21 %elifidn __OUTPUT_FORMAT__,macho32
22 %define ABI_IS_32BIT 1
23 %elifidn __OUTPUT_FORMAT__,win32
24 %define ABI_IS_32BIT 1
25 %else
26 %define ABI_IS_32BIT 0
27 %endif
28
29 %if ABI_IS_32BIT
30 %define rax eax
31 %define rbx ebx
32 %define rcx ecx
33 %define rdx edx
34 %define rsi esi
35 %define rdi edi
36 %define rsp esp
37 %define rbp ebp
38 %define movsxd mov
39 %macro movq 2
40   %ifidn %1,eax
41     movd %1,%2
42   %elifidn %2,eax
43     movd %1,%2
44   %elifidn %1,ebx
45     movd %1,%2
46   %elifidn %2,ebx
47     movd %1,%2
48   %elifidn %1,ecx
49     movd %1,%2
50   %elifidn %2,ecx
51     movd %1,%2
52   %elifidn %1,edx
53     movd %1,%2
54   %elifidn %2,edx
55     movd %1,%2
56   %elifidn %1,esi
57     movd %1,%2
58   %elifidn %2,esi
59     movd %1,%2
60   %elifidn %1,edi
61     movd %1,%2
62   %elifidn %2,edi
63     movd %1,%2
64   %elifidn %1,esp
65     movd %1,%2
66   %elifidn %2,esp
67     movd %1,%2
68   %elifidn %1,ebp
69     movd %1,%2
70   %elifidn %2,ebp
71     movd %1,%2
72   %else
73     movq %1,%2
74   %endif
75 %endmacro
76 %endif
77
78
79 ; sym()
80 ; Return the proper symbol name for the target ABI.
81 ;
82 ; Certain ABIs, notably MS COFF and Darwin MACH-O, require that symbols
83 ; with C linkage be prefixed with an underscore.
84 ;
85 %ifidn   __OUTPUT_FORMAT__,elf32
86 %define sym(x) x
87 %elifidn __OUTPUT_FORMAT__,elf64
88 %define sym(x) x
89 %elifidn __OUTPUT_FORMAT__,x64
90 %define sym(x) x
91 %else
92 %define sym(x) _ %+ x
93 %endif
94
95 ; arg()
96 ; Return the address specification of the given argument
97 ;
98 %if ABI_IS_32BIT
99   %define arg(x) [ebp+8+4*x]
100 %else
101   ; 64 bit ABI passes arguments in registers. This is a workaround to get up
102   ; and running quickly. Relies on SHADOW_ARGS_TO_STACK
103   %ifidn __OUTPUT_FORMAT__,x64
104     %define arg(x) [rbp+16+8*x]
105   %else
106     %define arg(x) [rbp-8-8*x]
107   %endif
108 %endif
109
110 ; REG_SZ_BYTES, REG_SZ_BITS
111 ; Size of a register
112 %if ABI_IS_32BIT
113 %define REG_SZ_BYTES 4
114 %define REG_SZ_BITS  32
115 %else
116 %define REG_SZ_BYTES 8
117 %define REG_SZ_BITS  64
118 %endif
119
120
121 ; ALIGN_STACK <alignment> <register>
122 ; This macro aligns the stack to the given alignment (in bytes). The stack
123 ; is left such that the previous value of the stack pointer is the first
124 ; argument on the stack (ie, the inverse of this macro is 'pop rsp.')
125 ; This macro uses one temporary register, which is not preserved, and thus
126 ; must be specified as an argument.
127 %macro ALIGN_STACK 2
128     mov         %2, rsp
129     and         rsp, -%1
130     lea         rsp, [rsp - (%1 - REG_SZ_BYTES)]
131     push        %2
132 %endmacro
133
134
135 ;
136 ; The Microsoft assembler tries to impose a certain amount of type safety in
137 ; its register usage. YASM doesn't recognize these directives, so we just
138 ; %define them away to maintain as much compatibility as possible with the
139 ; original inline assembler we're porting from.
140 ;
141 %idefine PTR
142 %idefine XMMWORD
143 %idefine MMWORD
144
145 ; PIC macros
146 ;
147 %if ABI_IS_32BIT
148   %if CONFIG_PIC=1
149   %ifidn __OUTPUT_FORMAT__,elf32
150     %define GET_GOT_SAVE_ARG 1
151     %define WRT_PLT wrt ..plt
152     %macro GET_GOT 1
153       extern _GLOBAL_OFFSET_TABLE_
154       push %1
155       call %%get_got
156       %%sub_offset:
157       jmp %%exitGG
158       %%get_got:
159       mov %1, [esp]
160       add %1, _GLOBAL_OFFSET_TABLE_ + $$ - %%sub_offset wrt ..gotpc
161       ret
162       %%exitGG:
163       %undef GLOBAL
164       %define GLOBAL(x) x + %1 wrt ..gotoff
165       %undef RESTORE_GOT
166       %define RESTORE_GOT pop %1
167     %endmacro
168   %elifidn __OUTPUT_FORMAT__,macho32
169     %define GET_GOT_SAVE_ARG 1
170     %macro GET_GOT 1
171       push %1
172       call %%get_got
173       %%get_got:
174       pop  %1
175       %undef GLOBAL
176       %define GLOBAL(x) x + %1 - %%get_got
177       %undef RESTORE_GOT
178       %define RESTORE_GOT pop %1
179     %endmacro
180   %endif
181   %endif
182   %define HIDDEN_DATA(x) x
183 %else
184   %macro GET_GOT 1
185   %endmacro
186   %define GLOBAL(x) rel x
187   %ifidn __OUTPUT_FORMAT__,elf64
188     %define WRT_PLT wrt ..plt
189     %define HIDDEN_DATA(x) x:data hidden
190   %else
191     %define HIDDEN_DATA(x) x
192   %endif
193 %endif
194 %ifnmacro GET_GOT
195     %macro GET_GOT 1
196     %endmacro
197     %define GLOBAL(x) x
198 %endif
199 %ifndef RESTORE_GOT
200 %define RESTORE_GOT
201 %endif
202 %ifndef WRT_PLT
203 %define WRT_PLT
204 %endif
205
206 %if ABI_IS_32BIT
207   %macro SHADOW_ARGS_TO_STACK 1
208   %endm
209   %define UNSHADOW_ARGS
210 %else
211 %ifidn __OUTPUT_FORMAT__,x64
212   %macro SHADOW_ARGS_TO_STACK 1 ; argc
213     %if %1 > 0
214         mov arg(0),rcx
215     %endif
216     %if %1 > 1
217         mov arg(1),rdx
218     %endif
219     %if %1 > 2
220         mov arg(2),r8
221     %endif
222     %if %1 > 3
223         mov arg(3),r9
224     %endif
225   %endm
226 %else
227   %macro SHADOW_ARGS_TO_STACK 1 ; argc
228     %if %1 > 0
229         push rdi
230     %endif
231     %if %1 > 1
232         push rsi
233     %endif
234     %if %1 > 2
235         push rdx
236     %endif
237     %if %1 > 3
238         push rcx
239     %endif
240     %if %1 > 4
241         push r8
242     %endif
243     %if %1 > 5
244         push r9
245     %endif
246     %if %1 > 6
247       %assign i %1-6
248       %assign off 16
249       %rep i
250         mov rax,[rbp+off]
251         push rax
252         %assign off off+8
253       %endrep
254     %endif
255   %endm
256 %endif
257   %define UNSHADOW_ARGS mov rsp, rbp
258 %endif
259
260 ; Win64 ABI requires that XMM6:XMM15 are callee saved
261 ; SAVE_XMM n, [u]
262 ; store registers 6-n on the stack
263 ; if u is specified, use unaligned movs.
264 ; Win64 ABI requires 16 byte stack alignment, but then pushes an 8 byte return
265 ; value. Typically we follow this up with 'push rbp' - re-aligning the stack -
266 ; but in some cases this is not done and unaligned movs must be used.
267 %ifidn __OUTPUT_FORMAT__,x64
268 %macro SAVE_XMM 1-2 a
269   %if %1 < 6
270     %error Only xmm registers 6-15 must be preserved
271   %else
272     %assign last_xmm %1
273     %define movxmm movdq %+ %2
274     %assign xmm_stack_space ((last_xmm - 5) * 16)
275     sub rsp, xmm_stack_space
276     %assign i 6
277     %rep (last_xmm - 5)
278       movxmm [rsp + ((i - 6) * 16)], xmm %+ i
279       %assign i i+1
280     %endrep
281   %endif
282 %endmacro
283 %macro RESTORE_XMM 0
284   %ifndef last_xmm
285     %error RESTORE_XMM must be paired with SAVE_XMM n
286   %else
287     %assign i last_xmm
288     %rep (last_xmm - 5)
289       movxmm xmm %+ i, [rsp +((i - 6) * 16)]
290       %assign i i-1
291     %endrep
292     add rsp, xmm_stack_space
293     ; there are a couple functions which return from multiple places.
294     ; otherwise, we could uncomment these:
295     ; %undef last_xmm
296     ; %undef xmm_stack_space
297     ; %undef movxmm
298   %endif
299 %endmacro
300 %else
301 %macro SAVE_XMM 1-2
302 %endmacro
303 %macro RESTORE_XMM 0
304 %endmacro
305 %endif
306
307 ; Name of the rodata section
308 ;
309 ; .rodata seems to be an elf-ism, as it doesn't work on OSX.
310 ;
311 %ifidn __OUTPUT_FORMAT__,macho64
312 %define SECTION_RODATA section .text
313 %elifidn __OUTPUT_FORMAT__,macho32
314 %macro SECTION_RODATA 0
315 section .text
316 %endmacro
317 %else
318 %define SECTION_RODATA section .rodata
319 %endif
320
321
322 ; Tell GNU ld that we don't require an executable stack.
323 %ifidn __OUTPUT_FORMAT__,elf32
324 section .note.GNU-stack noalloc noexec nowrite progbits
325 section .text
326 %elifidn __OUTPUT_FORMAT__,elf64
327 section .note.GNU-stack noalloc noexec nowrite progbits
328 section .text
329 %endif
330