Merge "cosmetics: trim trailing whitespace"
[profile/ivi/libvpx.git] / vpx_ports / x86_abi_support.asm
1 ;
2 ;  Copyright (c) 2010 The VP8 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 %endif
40
41
42 ; sym()
43 ; Return the proper symbol name for the target ABI.
44 ;
45 ; Certain ABIs, notably MS COFF and Darwin MACH-O, require that symbols
46 ; with C linkage be prefixed with an underscore.
47 ;
48 %ifidn   __OUTPUT_FORMAT__,elf32
49 %define sym(x) x
50 %elifidn __OUTPUT_FORMAT__,elf64
51 %define sym(x) x
52 %elifidn __OUTPUT_FORMAT__,x64
53 %define sym(x) x
54 %else
55 %define sym(x) _ %+ x
56 %endif
57
58 ; arg()
59 ; Return the address specification of the given argument
60 ;
61 %if ABI_IS_32BIT
62   %define arg(x) [ebp+8+4*x]
63 %else
64   ; 64 bit ABI passes arguments in registers. This is a workaround to get up
65   ; and running quickly. Relies on SHADOW_ARGS_TO_STACK
66   %ifidn __OUTPUT_FORMAT__,x64
67     %define arg(x) [rbp+16+8*x]
68   %else
69     %define arg(x) [rbp-8-8*x]
70   %endif
71 %endif
72
73 ; REG_SZ_BYTES, REG_SZ_BITS
74 ; Size of a register
75 %if ABI_IS_32BIT
76 %define REG_SZ_BYTES 4
77 %define REG_SZ_BITS  32
78 %else
79 %define REG_SZ_BYTES 8
80 %define REG_SZ_BITS  64
81 %endif
82
83
84 ; ALIGN_STACK <alignment> <register>
85 ; This macro aligns the stack to the given alignment (in bytes). The stack
86 ; is left such that the previous value of the stack pointer is the first
87 ; argument on the stack (ie, the inverse of this macro is 'pop rsp.')
88 ; This macro uses one temporary register, which is not preserved, and thus
89 ; must be specified as an argument.
90 %macro ALIGN_STACK 2
91     mov         %2, rsp
92     and         rsp, -%1
93     sub         rsp, %1 - REG_SZ_BYTES
94     push        %2
95 %endmacro
96
97
98 ;
99 ; The Microsoft assembler tries to impose a certain amount of type safety in
100 ; its register usage. YASM doesn't recognize these directives, so we just
101 ; %define them away to maintain as much compatibility as possible with the
102 ; original inline assembler we're porting from.
103 ;
104 %idefine PTR
105 %idefine XMMWORD
106 %idefine MMWORD
107
108
109 ; PIC macros
110 ;
111 %if ABI_IS_32BIT
112   %if CONFIG_PIC=1
113   %ifidn __OUTPUT_FORMAT__,elf32
114     %define WRT_PLT wrt ..plt
115     %macro GET_GOT 1
116       extern _GLOBAL_OFFSET_TABLE_
117       push %1
118       call %%get_got
119       %%get_got:
120       pop %1
121       add %1, _GLOBAL_OFFSET_TABLE_ + $$ - %%get_got wrt ..gotpc
122       %undef GLOBAL
123       %define GLOBAL + %1 wrt ..gotoff
124       %undef RESTORE_GOT
125       %define RESTORE_GOT pop %1
126     %endmacro
127   %elifidn __OUTPUT_FORMAT__,macho32
128     %macro GET_GOT 1
129       push %1
130       call %%get_got
131       %%get_got:
132       pop %1
133       add %1, fake_got - %%get_got
134       %undef GLOBAL
135       %define GLOBAL + %1 - fake_got
136       %undef RESTORE_GOT
137       %define RESTORE_GOT pop %1
138     %endmacro
139   %endif
140   %endif
141 %else
142   %macro GET_GOT 1
143   %endmacro
144   %define GLOBAL wrt rip
145   %ifidn __OUTPUT_FORMAT__,elf64
146     %define WRT_PLT wrt ..plt
147   %endif
148 %endif
149 %ifnmacro GET_GOT
150     %macro GET_GOT 1
151     %endmacro
152     %define GLOBAL
153 %endif
154 %ifndef RESTORE_GOT
155 %define RESTORE_GOT
156 %endif
157 %ifndef WRT_PLT
158 %define WRT_PLT
159 %endif
160
161 %if ABI_IS_32BIT
162   %macro SHADOW_ARGS_TO_STACK 1
163   %endm
164   %define UNSHADOW_ARGS
165 %else
166 %ifidn __OUTPUT_FORMAT__,x64
167   %macro SHADOW_ARGS_TO_STACK 1 ; argc
168     %if %1 > 0
169         mov arg(0),rcx
170     %endif
171     %if %1 > 1
172         mov arg(1),rdx
173     %endif
174     %if %1 > 2
175         mov arg(2),r8
176     %endif
177     %if %1 > 3
178         mov arg(3),r9
179     %endif
180   %endm
181 %else
182   %macro SHADOW_ARGS_TO_STACK 1 ; argc
183     %if %1 > 0
184         push rdi
185     %endif
186     %if %1 > 1
187         push rsi
188     %endif
189     %if %1 > 2
190         push rdx
191     %endif
192     %if %1 > 3
193         push rcx
194     %endif
195     %if %1 > 4
196         push r8
197     %endif
198     %if %1 > 5
199         push r9
200     %endif
201     %if %1 > 6
202       %assign i %1-6
203       %assign off 16
204       %rep i
205         mov rax,[rbp+off]
206         push rax
207         %assign off off+8
208       %endrep
209     %endif
210   %endm
211 %endif
212   %define UNSHADOW_ARGS mov rsp, rbp
213 %endif
214
215 ; must keep XMM6:XMM15 (libvpx uses XMM6 and XMM7) on Win64 ABI
216 ; rsp register has to be aligned
217 %ifidn __OUTPUT_FORMAT__,x64
218 %macro SAVE_XMM 0
219   sub rsp, 32
220   movdqa XMMWORD PTR [rsp], xmm6
221   movdqa XMMWORD PTR [rsp+16], xmm7
222 %endmacro
223 %macro RESTORE_XMM 0
224   movdqa xmm6, XMMWORD PTR [rsp]
225   movdqa xmm7, XMMWORD PTR [rsp+16]
226   add rsp, 32
227 %endmacro
228 %else
229 %macro SAVE_XMM 0
230 %endmacro
231 %macro RESTORE_XMM 0
232 %endmacro
233 %endif
234
235 ; Name of the rodata section
236 ;
237 ; .rodata seems to be an elf-ism, as it doesn't work on OSX.
238 ;
239 %ifidn __OUTPUT_FORMAT__,macho64
240 %define SECTION_RODATA section .text
241 %elifidn __OUTPUT_FORMAT__,macho32
242 %macro SECTION_RODATA 0
243 section .text
244 fake_got:
245 %endmacro
246 %else
247 %define SECTION_RODATA section .rodata
248 %endif
249
250
251 ; Tell GNU ld that we don't require an executable stack.
252 %ifidn __OUTPUT_FORMAT__,elf32
253 section .note.GNU-stack noalloc noexec nowrite progbits
254 section .text
255 %elifidn __OUTPUT_FORMAT__,elf64
256 section .note.GNU-stack noalloc noexec nowrite progbits
257 section .text
258 %endif
259