asm.h 5.8 KB

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