1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef AVUTIL_X86_ASM_H
22 #define AVUTIL_X86_ASM_H
23 
24 #include <stdint.h>
25 
26 #include "config.h"
27 
28 typedef struct xmm_reg { uint64_t a, b; } xmm_reg;
29 typedef struct ymm_reg { uint64_t a, b, c, d; } ymm_reg;
30 
31 /* avoid SunOS regset.h definition for REG_SP */
32 #if defined (__sun) && defined (REG_SP)
33 #undef REG_SP
34 #endif
35 
36 #if ARCH_X86_64
37 #    define OPSIZE "q"
38 #    define REG_a "rax"
39 #    define REG_b "rbx"
40 #    define REG_c "rcx"
41 #    define REG_d "rdx"
42 #    define REG_D "rdi"
43 #    define REG_S "rsi"
44 #    define PTR_SIZE "8"
45 typedef int64_t x86_reg;
46 
47 #    define REG_SP "rsp"
48 #    define REG_BP "rbp"
49 #    define REGBP   rbp
50 #    define REGa    rax
51 #    define REGb    rbx
52 #    define REGc    rcx
53 #    define REGd    rdx
54 #    define REGSP   rsp
55 
56 #elif ARCH_X86_32
57 
58 #    define OPSIZE "l"
59 #    define REG_a "eax"
60 #    define REG_b "ebx"
61 #    define REG_c "ecx"
62 #    define REG_d "edx"
63 #    define REG_D "edi"
64 #    define REG_S "esi"
65 #    define PTR_SIZE "4"
66 typedef int32_t x86_reg;
67 
68 #    define REG_SP "esp"
69 #    define REG_BP "ebp"
70 #    define REGBP   ebp
71 #    define REGa    eax
72 #    define REGb    ebx
73 #    define REGc    ecx
74 #    define REGd    edx
75 #    define REGSP   esp
76 #else
77 typedef int x86_reg;
78 #endif
79 
80 #define HAVE_7REGS (ARCH_X86_64 || (HAVE_EBX_AVAILABLE && HAVE_EBP_AVAILABLE))
81 #define HAVE_6REGS (ARCH_X86_64 || (HAVE_EBX_AVAILABLE || HAVE_EBP_AVAILABLE))
82 
83 #if ARCH_X86_64 && defined(PIC)
84 #    define BROKEN_RELOCATIONS 1
85 #endif
86 
87 /*
88  * If gcc is not set to support sse (-msse) it will not accept xmm registers
89  * in the clobber list for inline asm. XMM_CLOBBERS takes a list of xmm
90  * registers to be marked as clobbered and evaluates to nothing if they are
91  * not supported, or to the list itself if they are supported. Since a clobber
92  * list may not be empty, XMM_CLOBBERS_ONLY should be used if the xmm
93  * registers are the only in the clobber list.
94  * For example a list with "eax" and "xmm0" as clobbers should become:
95  * : XMM_CLOBBERS("xmm0",) "eax"
96  * and a list with only "xmm0" should become:
97  * XMM_CLOBBERS_ONLY("xmm0")
98  */
99 #if HAVE_XMM_CLOBBERS
100 #    define XMM_CLOBBERS(...)        __VA_ARGS__
101 #    define XMM_CLOBBERS_ONLY(...) : __VA_ARGS__
102 #else
103 #    define XMM_CLOBBERS(...)
104 #    define XMM_CLOBBERS_ONLY(...)
105 #endif
106 
107 /* Use to export labels from asm. */
108 #define LABEL_MANGLE(a) EXTERN_PREFIX #a
109 
110 // Use rip-relative addressing if compiling PIC code on x86-64.
111 #if ARCH_X86_64 && defined(PIC)
112 #    define LOCAL_MANGLE(a) #a "(%%rip)"
113 #else
114 #    define LOCAL_MANGLE(a) #a
115 #endif
116 
117 #if HAVE_INLINE_ASM_DIRECT_SYMBOL_REFS
118 #   define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
119 #   define NAMED_CONSTRAINTS_ADD(...)
120 #   define NAMED_CONSTRAINTS(...)
121 #   define NAMED_CONSTRAINTS_ARRAY_ADD(...)
122 #   define NAMED_CONSTRAINTS_ARRAY(...)
123 #else
124     /* When direct symbol references are used in code passed to a compiler that does not support them
125      *  then these references need to be converted to named asm constraints instead.
126      * Instead of returning a direct symbol MANGLE now returns a named constraint for that specific symbol.
127      * In order for this to work there must also be a corresponding entry in the asm-interface. To add this
128      *  entry use the macro NAMED_CONSTRAINTS() and pass in a list of each symbol reference used in the
129      *  corresponding block of code. (e.g. NAMED_CONSTRAINTS(var1,var2,var3) where var1 is the first symbol etc. ).
130      * If there are already existing constraints then use NAMED_CONSTRAINTS_ADD to add to the existing constraint list.
131      */
132 #   define MANGLE(a) "%["#a"]"
133     // Intel/MSVC does not correctly expand va-args so we need a rather ugly hack in order to get it to work
134 #   define FE_0(P,X) P(X)
135 #   define FE_1(P,X,X1) P(X), FE_0(P,X1)
136 #   define FE_2(P,X,X1,X2) P(X), FE_1(P,X1,X2)
137 #   define FE_3(P,X,X1,X2,X3) P(X), FE_2(P,X1,X2,X3)
138 #   define FE_4(P,X,X1,X2,X3,X4) P(X), FE_3(P,X1,X2,X3,X4)
139 #   define FE_5(P,X,X1,X2,X3,X4,X5) P(X), FE_4(P,X1,X2,X3,X4,X5)
140 #   define FE_6(P,X,X1,X2,X3,X4,X5,X6) P(X), FE_5(P,X1,X2,X3,X4,X5,X6)
141 #   define FE_7(P,X,X1,X2,X3,X4,X5,X6,X7) P(X), FE_6(P,X1,X2,X3,X4,X5,X6,X7)
142 #   define FE_8(P,X,X1,X2,X3,X4,X5,X6,X7,X8) P(X), FE_7(P,X1,X2,X3,X4,X5,X6,X7,X8)
143 #   define FE_9(P,X,X1,X2,X3,X4,X5,X6,X7,X8,X9) P(X), FE_8(P,X1,X2,X3,X4,X5,X6,X7,X8,X9)
144 #   define GET_FE_IMPL(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,NAME,...) NAME
145 #   define GET_FE(A) GET_FE_IMPL A
146 #   define GET_FE_GLUE(x, y) x y
147 #   define FOR_EACH_VA(P,...) GET_FE_GLUE(GET_FE((__VA_ARGS__,FE_9,FE_8,FE_7,FE_6,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)), (P,__VA_ARGS__))
148 #   define NAME_CONSTRAINT(x) [x] "m"(x)
149     // Parameters are a list of each symbol reference required
150 #   define NAMED_CONSTRAINTS_ADD(...) , FOR_EACH_VA(NAME_CONSTRAINT,__VA_ARGS__)
151     // Same but without comma for when there are no previously defined constraints
152 #   define NAMED_CONSTRAINTS(...) FOR_EACH_VA(NAME_CONSTRAINT,__VA_ARGS__)
153     // Same as above NAMED_CONSTRAINTS except used for passing arrays/pointers instead of normal variables
154 #   define NAME_CONSTRAINT_ARRAY(x) [x] "m"(*x)
155 #   define NAMED_CONSTRAINTS_ARRAY_ADD(...) , FOR_EACH_VA(NAME_CONSTRAINT_ARRAY,__VA_ARGS__)
156 #   define NAMED_CONSTRAINTS_ARRAY(...) FOR_EACH_VA(NAME_CONSTRAINT_ARRAY,__VA_ARGS__)
157 #endif
158 
159 #endif /* AVUTIL_X86_ASM_H */
160