[INCLUDE/WINE] Import asm.h from Wine Staging 4.18. CORE-16441
[reactos.git] / sdk / include / reactos / wine / asm.h
1 /*
2 * Inline assembly support
3 *
4 * Copyright 2019 Alexandre Julliard
5 *
6 * This library 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 * This library 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 this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #ifndef __WINE_WINE_ASM_H
22 #define __WINE_WINE_ASM_H
23
24 #if defined(__APPLE__) || (defined(_WIN32) && defined(__i386__))
25 # define __ASM_NAME(name) "_" name
26 #else
27 # define __ASM_NAME(name) name
28 #endif
29
30 #if defined(_WIN32) && defined(__i386__)
31 # define __ASM_STDCALL(name,args) __ASM_NAME(name) "@" #args
32 #else
33 # define __ASM_STDCALL(name,args) __ASM_NAME(name)
34 #endif
35
36 #if defined(__GCC_HAVE_DWARF2_CFI_ASM) || defined(__APPLE__)
37 # define __ASM_CFI(str) str
38 #else
39 # define __ASM_CFI(str)
40 #endif
41
42 #ifdef __SEH__
43 # define __ASM_SEH(str) str
44 #else
45 # define __ASM_SEH(str)
46 #endif
47
48 #ifdef _WIN32
49 # define __ASM_FUNC_TYPE(name) ".def " name "; .scl 2; .type 32; .endef"
50 #elif defined(__APPLE__)
51 # define __ASM_FUNC_TYPE(name) ""
52 #elif defined(__arm__) || defined(__arm64__)
53 # define __ASM_FUNC_TYPE(name) ".type " name ",%function"
54 #else
55 # define __ASM_FUNC_TYPE(name) ".type " name ",@function"
56 #endif
57
58 /* ReactOS */
59 #if defined(_MSC_VER)
60 # define __ASM_DEFINE_FUNC(name,code)
61 #elif defined(__GNUC__)
62 # define __ASM_DEFINE_FUNC(name,code) \
63 asm(".text\n\t.align 4\n\t.globl " name "\n\t" __ASM_FUNC_TYPE(name) __ASM_SEH("\n\t.seh_proc " name) "\n" name ":\n\t" \
64 __ASM_CFI(".cfi_startproc\n\t") code __ASM_CFI("\n\t.cfi_endproc") __ASM_SEH("\n\t.seh_endproc") );
65 #else
66 # define __ASM_DEFINE_FUNC(name,code) void __asm_dummy_##__LINE__(void) { \
67 asm(".text\n\t.align 4\n\t.globl " name "\n\t" __ASM_FUNC_TYPE(name) __ASM_SEH("\n\t.seh_proc " name) "\n" name ":\n\t" \
68 __ASM_CFI(".cfi_startproc\n\t") code __ASM_CFI("\n\t.cfi_endproc") __ASM_SEH("\n\t.seh_endproc") ); }
69 #endif
70
71 #define __ASM_GLOBAL_FUNC(name,code) __ASM_DEFINE_FUNC(__ASM_NAME(#name),code)
72
73 #define __ASM_STDCALL_FUNC(name,args,code) __ASM_DEFINE_FUNC(__ASM_STDCALL(#name,args),code)
74
75 /* fastcall support */
76
77 #if defined(__i386__) && !defined(_WIN32)
78
79 # define DEFINE_FASTCALL1_WRAPPER(func) \
80 __ASM_STDCALL_FUNC( __fastcall_ ## func, 4, \
81 "popl %eax\n\t" \
82 "pushl %ecx\n\t" \
83 "pushl %eax\n\t" \
84 "jmp " __ASM_STDCALL(#func,4) )
85 # define DEFINE_FASTCALL_WRAPPER(func,args) \
86 __ASM_STDCALL_FUNC( __fastcall_ ## func, args, \
87 "popl %eax\n\t" \
88 "pushl %edx\n\t" \
89 "pushl %ecx\n\t" \
90 "pushl %eax\n\t" \
91 "jmp " __ASM_STDCALL(#func,args) )
92
93 #else /* __i386__ */
94
95 # define DEFINE_FASTCALL1_WRAPPER(func) /* nothing */
96 # define DEFINE_FASTCALL_WRAPPER(func,args) /* nothing */
97
98 #endif /* __i386__ */
99
100 /* thiscall support */
101
102 #if defined(__i386__) && !defined(__MINGW32__)
103
104 # ifdef _MSC_VER
105
106 #ifdef __REACTOS__
107 # define DEFINE_THISCALL_WRAPPER(func,args) \
108 __declspec(naked) void __thiscall_##func(void) \
109 { \
110 __asm pop eax \
111 __asm push ecx \
112 __asm push eax \
113 __asm jmp func \
114 }
115 #else
116 # define DEFINE_THISCALL_WRAPPER(func,args) \
117 __declspec(naked) void __thiscall_##func(void) \
118 { __asm { \
119 pop eax \
120 push ecx \
121 push eax \
122 jmp func \
123 } }
124 #endif
125 # else /* _MSC_VER */
126 # define DEFINE_THISCALL_WRAPPER(func,args) \
127 extern void __thiscall_ ## func(void); \
128 __ASM_STDCALL_FUNC( __thiscall_ ## func, args, \
129 "popl %eax\n\t" \
130 "pushl %ecx\n\t" \
131 "pushl %eax\n\t" \
132 "jmp " __ASM_STDCALL(#func,args) )
133 # endif /* _MSC_VER */
134
135 # define THISCALL(func) (void *)__thiscall_ ## func
136 # define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
137
138 #else /* __i386__ */
139
140 # define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
141 # define THISCALL(func) func
142 # define THISCALL_NAME(func) __ASM_NAME(#func)
143
144 #endif /* __i386__ */
145
146 #endif /* __WINE_WINE_ASM_H */