Initial WebM release
[platform/upstream/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 and patent
5 ;  grant that can be found in the LICENSE file in the root of the source
6 ;  tree. All contributing project authors may be found in the AUTHORS
7 ;  file in the root of the source tree.
8 ;
9
10
11 %include "vpx_config.asm"
12
13 ; 32/64 bit compatibility macros
14 ;
15 ; In general, we make the source use 64 bit syntax, then twiddle with it using
16 ; the preprocessor to get the 32 bit syntax on 32 bit platforms.
17 ;
18 %ifidn __OUTPUT_FORMAT__,elf32
19 %define ABI_IS_32BIT 1
20 %elifidn __OUTPUT_FORMAT__,macho32
21 %define ABI_IS_32BIT 1
22 %elifidn __OUTPUT_FORMAT__,win32
23 %define ABI_IS_32BIT 1
24 %else
25 %define ABI_IS_32BIT 0
26 %endif
27
28 %if ABI_IS_32BIT
29 %define rax eax
30 %define rbx ebx
31 %define rcx ecx
32 %define rdx edx
33 %define rsi esi
34 %define rdi edi
35 %define rsp esp
36 %define rbp ebp
37 %define movsxd mov
38 %endif
39
40
41 ; sym()
42 ; Return the proper symbol name for the target ABI.
43 ;
44 ; Certain ABIs, notably MS COFF and Darwin MACH-O, require that symbols
45 ; with C linkage be prefixed with an underscore.
46 ;
47 %ifidn   __OUTPUT_FORMAT__,elf32
48 %define sym(x) x
49 %elifidn __OUTPUT_FORMAT__,elf64
50 %define sym(x) x
51 %elifidn __OUTPUT_FORMAT__,x64
52 %define sym(x) x
53 %else
54 %define sym(x) _ %+ x
55 %endif
56
57 ; arg()
58 ; Return the address specification of the given argument
59 ;
60 %if ABI_IS_32BIT
61   %define arg(x) [ebp+8+4*x]
62 %else
63   ; 64 bit ABI passes arguments in registers. This is a workaround to get up
64   ; and running quickly. Relies on SHADOW_ARGS_TO_STACK
65   %ifidn __OUTPUT_FORMAT__,x64
66     %define arg(x) [rbp+16+8*x]
67   %else
68     %define arg(x) [rbp-8-8*x]
69   %endif
70 %endif
71
72 ; REG_SZ_BYTES, REG_SZ_BITS
73 ; Size of a register
74 %if ABI_IS_32BIT
75 %define REG_SZ_BYTES 4
76 %define REG_SZ_BITS  32
77 %else
78 %define REG_SZ_BYTES 8
79 %define REG_SZ_BITS  64
80 %endif
81
82
83 ; ALIGN_STACK <alignment> <register>
84 ; This macro aligns the stack to the given alignment (in bytes). The stack
85 ; is left such that the previous value of the stack pointer is the first
86 ; argument on the stack (ie, the inverse of this macro is 'pop rsp.')
87 ; This macro uses one temporary register, which is not preserved, and thus
88 ; must be specified as an argument.
89 %macro ALIGN_STACK 2
90     mov         %2, rsp
91     and         rsp, -%1
92     sub         rsp, %1 - REG_SZ_BYTES
93     push        %2
94 %endmacro
95
96
97 ;
98 ; The Microsoft assembler tries to impose a certain amount of type safety in
99 ; its register usage. YASM doesn't recognize these directives, so we just
100 ; %define them away to maintain as much compatibility as possible with the
101 ; original inline assembler we're porting from.
102 ;
103 %idefine PTR
104 %idefine XMMWORD
105 %idefine MMWORD
106
107
108 ; PIC macros
109 ;
110 %if ABI_IS_32BIT
111   %if CONFIG_PIC=1
112   %ifidn __OUTPUT_FORMAT__,elf32
113     %define WRT_PLT wrt ..plt
114     %macro GET_GOT 1
115       extern _GLOBAL_OFFSET_TABLE_
116       push %1
117       call %%get_got
118       %%get_got:
119       pop %1
120       add %1, _GLOBAL_OFFSET_TABLE_ + $$ - %%get_got wrt ..gotpc
121       %undef GLOBAL
122       %define GLOBAL + %1 wrt ..gotoff
123       %undef RESTORE_GOT
124       %define RESTORE_GOT pop %1
125     %endmacro
126   %elifidn __OUTPUT_FORMAT__,macho32
127     %macro GET_GOT 1
128       push %1
129       call %%get_got
130       %%get_got:
131       pop %1
132       add %1, fake_got - %%get_got
133       %undef GLOBAL
134       %define GLOBAL + %1 - fake_got
135       %undef RESTORE_GOT
136       %define RESTORE_GOT pop %1
137     %endmacro
138   %endif
139   %endif
140 %else
141   %macro GET_GOT 1
142   %endmacro
143   %define GLOBAL wrt rip
144   %ifidn __OUTPUT_FORMAT__,elf64
145     %define WRT_PLT wrt ..plt
146   %endif
147 %endif
148 %ifnmacro GET_GOT
149     %macro GET_GOT 1
150     %endmacro
151     %define GLOBAL
152 %endif
153 %ifndef RESTORE_GOT
154 %define RESTORE_GOT
155 %endif
156 %ifndef WRT_PLT
157 %define WRT_PLT
158 %endif
159
160 %if ABI_IS_32BIT
161   %macro SHADOW_ARGS_TO_STACK 1
162   %endm
163   %define UNSHADOW_ARGS
164 %else
165 %ifidn __OUTPUT_FORMAT__,x64
166   %macro SHADOW_ARGS_TO_STACK 1 ; argc
167     %if %1 > 0
168         mov arg(0),rcx
169     %endif
170     %if %1 > 1
171         mov arg(1),rdx
172     %endif
173     %if %1 > 2
174         mov arg(2),r8
175     %endif
176     %if %1 > 3
177         mov arg(3),r9
178     %endif
179   %endm
180 %else
181   %macro SHADOW_ARGS_TO_STACK 1 ; argc
182     %if %1 > 0
183         push rdi
184     %endif
185     %if %1 > 1
186         push rsi
187     %endif
188     %if %1 > 2
189         push rdx
190     %endif
191     %if %1 > 3
192         push rcx
193     %endif
194     %if %1 > 4
195         push r8
196     %endif
197     %if %1 > 5
198         push r9
199     %endif
200     %if %1 > 6
201         mov rax,[rbp+16]
202         push rax
203     %endif
204     %if %1 > 7
205         mov rax,[rbp+24]
206         push rax
207     %endif
208     %if %1 > 8
209         mov rax,[rbp+32]
210         push rax
211     %endif
212   %endm
213 %endif
214   %define UNSHADOW_ARGS mov rsp, rbp
215 %endif
216
217
218 ; Name of the rodata section
219 ;
220 ; .rodata seems to be an elf-ism, as it doesn't work on OSX.
221 ;
222 %ifidn __OUTPUT_FORMAT__,macho64
223 %define SECTION_RODATA section .text
224 %elifidn __OUTPUT_FORMAT__,macho32
225 %macro SECTION_RODATA 0
226 section .text
227 fake_got:
228 %endmacro
229 %else
230 %define SECTION_RODATA section .rodata
231 %endif