ReactOS 0.4.16-dev-1279-gc894716
asm.h
Go to the documentation of this file.
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#if !defined(__GNUC__) && !defined(__clang__)
59# define __ASM_BLOCK_BEGIN(name) void __asm_dummy_##name(void) {
60# define __ASM_BLOCK_END }
61#else
62# define __ASM_BLOCK_BEGIN(name)
63# define __ASM_BLOCK_END
64#endif
65
66/* ReactOS */
67#if defined(_MSC_VER)
68# define __ASM_DEFINE_FUNC(name,code)
69#elif defined(__GNUC__)
70# define __ASM_DEFINE_FUNC(name,code) \
71 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" \
72 __ASM_CFI(".cfi_startproc\n\t") code __ASM_CFI("\n\t.cfi_endproc") __ASM_SEH("\n\t.seh_endproc") );
73#else
74# define __ASM_DEFINE_FUNC(name,code) void __asm_dummy_##__LINE__(void) { \
75 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" \
76 __ASM_CFI(".cfi_startproc\n\t") code __ASM_CFI("\n\t.cfi_endproc") __ASM_SEH("\n\t.seh_endproc") ); }
77#endif
78
79#define __ASM_GLOBAL_FUNC(name,code) __ASM_DEFINE_FUNC(__ASM_NAME(#name),code)
80
81/* import variables */
82
83#ifdef __WINE_PE_BUILD
84# ifdef __arm64ec__
85# define __ASM_DEFINE_IMPORT(name) \
86 asm( ".data\n\t" \
87 ".balign 8\n\t" \
88 ".globl __imp_" name "\n" \
89 "__imp_" name ":\n\t" \
90 ".quad \"#" name "\"\n\t" \
91 ".globl __imp_aux_" name "\n" \
92 "__imp_aux_" name ":\n\t" \
93 ".quad " name "\n\t" \
94 ".text" );
95# elif defined(_WIN64)
96# define __ASM_DEFINE_IMPORT(name) \
97 __ASM_BLOCK_BEGIN(__LINE__) \
98 asm( ".data\n\t" \
99 ".balign 8\n\t" \
100 ".globl __imp_" name "\n" \
101 "__imp_" name ":\n\t" \
102 ".quad " name "\n\t" \
103 ".text"); \
104 __ASM_BLOCK_END
105# else
106# define __ASM_DEFINE_IMPORT(name) \
107 __ASM_BLOCK_BEGIN(__LINE__) \
108 asm( ".data\n\t" \
109 ".balign 4\n\t" \
110 ".globl __imp_" name "\n" \
111 "__imp_" name ":\n\t" \
112 ".long " name "\n\t" \
113 ".text"); \
114 __ASM_BLOCK_END
115# endif
116# define __ASM_GLOBAL_IMPORT(name) __ASM_DEFINE_IMPORT(__ASM_NAME(#name))
117#else
118# define __ASM_GLOBAL_IMPORT(name) /* nothing */
119#endif
120
121/* stdcall support */
122
123#define __ASM_STDCALL_FUNC(name,args,code) __ASM_DEFINE_FUNC(__ASM_STDCALL(#name,args),code)
124
125/* fastcall support */
126
127#if defined(__i386__) && !defined(_WIN32)
128
129# define DEFINE_FASTCALL1_WRAPPER(func) \
130 __ASM_STDCALL_FUNC( __fastcall_ ## func, 4, \
131 "popl %eax\n\t" \
132 "pushl %ecx\n\t" \
133 "pushl %eax\n\t" \
134 "jmp " __ASM_STDCALL(#func,4) )
135# define DEFINE_FASTCALL_WRAPPER(func,args) \
136 __ASM_STDCALL_FUNC( __fastcall_ ## func, args, \
137 "popl %eax\n\t" \
138 "pushl %edx\n\t" \
139 "pushl %ecx\n\t" \
140 "pushl %eax\n\t" \
141 "jmp " __ASM_STDCALL(#func,args) )
142
143#else /* __i386__ */
144
145# define DEFINE_FASTCALL1_WRAPPER(func) /* nothing */
146# define DEFINE_FASTCALL_WRAPPER(func,args) /* nothing */
147
148#endif /* __i386__ */
149
150/* thiscall support */
151
152#ifdef _MSC_VER
153#define __thiscall __stdcall
154#endif
155
156#if defined(__i386__) && !defined(__MINGW32__)
157
158# ifdef _MSC_VER
159
160#ifdef __REACTOS__
161# define DEFINE_THISCALL_WRAPPER(func,args) \
162 __declspec(naked) void __thiscall_##func(void) \
163 { \
164 __asm pop eax \
165 __asm push ecx \
166 __asm push eax \
167 __asm jmp func \
168 }
169#else
170# define DEFINE_THISCALL_WRAPPER(func,args) \
171 __declspec(naked) void __thiscall_##func(void) \
172 { __asm { \
173 pop eax \
174 push ecx \
175 push eax \
176 jmp func \
177 } }
178#endif
179# else /* _MSC_VER */
180# define DEFINE_THISCALL_WRAPPER(func,args) \
181 extern void __thiscall_ ## func(void); \
182 __ASM_STDCALL_FUNC( __thiscall_ ## func, args, \
183 "popl %eax\n\t" \
184 "pushl %ecx\n\t" \
185 "pushl %eax\n\t" \
186 "jmp " __ASM_STDCALL(#func,args) )
187# endif /* _MSC_VER */
188
189# define THISCALL(func) (void *)__thiscall_ ## func
190# define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
191
192#else /* __i386__ */
193
194# define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
195# define THISCALL(func) func
196# define THISCALL_NAME(func) __ASM_NAME(#func)
197
198#endif /* __i386__ */
199
200#endif /* __WINE_WINE_ASM_H */