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