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