1//===-- assembly.h - compiler-rt assembler support macros -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines macros for use in compiler-rt assembler source.
10// This file is not part of the interface of this library.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef COMPILERRT_ASSEMBLY_H
15#define COMPILERRT_ASSEMBLY_H
16
17#if defined(__POWERPC__) || defined(__powerpc__) || defined(__ppc__)
18#define SEPARATOR @
19#else
20#define SEPARATOR ;
21#endif
22
23#if defined(__APPLE__)
24#define HIDDEN(name) .private_extern name
25#define LOCAL_LABEL(name) L_##name
26// tell linker it can break up file at label boundaries
27#define FILE_LEVEL_DIRECTIVE .subsections_via_symbols
28#define SYMBOL_IS_FUNC(name)
29#define CONST_SECTION .const
30
31#define NO_EXEC_STACK_DIRECTIVE
32
33#elif defined(__ELF__)
34
35#define HIDDEN(name) .hidden name
36#define LOCAL_LABEL(name) .L_##name
37#define FILE_LEVEL_DIRECTIVE
38#if defined(__arm__)
39#define SYMBOL_IS_FUNC(name) .type name,%function
40#else
41#define SYMBOL_IS_FUNC(name) .type name,@function
42#endif
43#define CONST_SECTION .section .rodata
44
45#if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
46    defined(__linux__)
47#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
48#else
49#define NO_EXEC_STACK_DIRECTIVE
50#endif
51
52#else // !__APPLE__ && !__ELF__
53
54#define HIDDEN(name)
55#define LOCAL_LABEL(name) .L ## name
56#define FILE_LEVEL_DIRECTIVE
57#define SYMBOL_IS_FUNC(name)                                                   \
58  .def name SEPARATOR                                                          \
59    .scl 2 SEPARATOR                                                           \
60    .type 32 SEPARATOR                                                         \
61  .endef
62#define CONST_SECTION .section .rdata,"rd"
63
64#define NO_EXEC_STACK_DIRECTIVE
65
66#endif
67
68#if defined(__arm__)
69
70// Determine actual [ARM][THUMB[1][2]] ISA using compiler predefined macros:
71// - for '-mthumb -march=armv6' compiler defines '__thumb__'
72// - for '-mthumb -march=armv7' compiler defines '__thumb__' and '__thumb2__'
73#if defined(__thumb2__) || defined(__thumb__)
74#define DEFINE_CODE_STATE .thumb SEPARATOR
75#define DECLARE_FUNC_ENCODING    .thumb_func SEPARATOR
76#if defined(__thumb2__)
77#define USE_THUMB_2
78#define IT(cond)  it cond
79#define ITT(cond) itt cond
80#define ITE(cond) ite cond
81#else
82#define USE_THUMB_1
83#define IT(cond)
84#define ITT(cond)
85#define ITE(cond)
86#endif // defined(__thumb__2)
87#else // !defined(__thumb2__) && !defined(__thumb__)
88#define DEFINE_CODE_STATE .arm SEPARATOR
89#define DECLARE_FUNC_ENCODING
90#define IT(cond)
91#define ITT(cond)
92#define ITE(cond)
93#endif
94
95#if defined(USE_THUMB_1) && defined(USE_THUMB_2)
96#error "USE_THUMB_1 and USE_THUMB_2 can't be defined together."
97#endif
98
99#if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
100#define ARM_HAS_BX
101#endif
102#if !defined(__ARM_FEATURE_CLZ) && !defined(USE_THUMB_1) &&  \
103    (__ARM_ARCH >= 6 || (__ARM_ARCH == 5 && !defined(__ARM_ARCH_5__)))
104#define __ARM_FEATURE_CLZ
105#endif
106
107#ifdef ARM_HAS_BX
108#define JMP(r) bx r
109#define JMPc(r, c) bx##c r
110#else
111#define JMP(r) mov pc, r
112#define JMPc(r, c) mov##c pc, r
113#endif
114
115// pop {pc} can't switch Thumb mode on ARMv4T
116#if __ARM_ARCH >= 5
117#define POP_PC() pop {pc}
118#else
119#define POP_PC()                                                               \
120  pop {ip};                                                                    \
121  JMP(ip)
122#endif
123
124#if defined(USE_THUMB_2)
125#define WIDE(op) op.w
126#else
127#define WIDE(op) op
128#endif
129#else // !defined(__arm)
130#define DECLARE_FUNC_ENCODING
131#define DEFINE_CODE_STATE
132#endif
133
134#define GLUE2(a, b) a##b
135#define GLUE(a, b) GLUE2(a, b)
136#define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
137
138#ifdef VISIBILITY_HIDDEN
139#define DECLARE_SYMBOL_VISIBILITY(name)                                        \
140  HIDDEN(SYMBOL_NAME(name)) SEPARATOR
141#else
142#define DECLARE_SYMBOL_VISIBILITY(name)
143#endif
144
145#define DEFINE_COMPILERRT_FUNCTION(name)                                       \
146  DEFINE_CODE_STATE                                                            \
147  FILE_LEVEL_DIRECTIVE SEPARATOR                                               \
148  .globl SYMBOL_NAME(name) SEPARATOR                                           \
149  SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR                                  \
150  DECLARE_SYMBOL_VISIBILITY(name)                                              \
151  DECLARE_FUNC_ENCODING                                                        \
152  SYMBOL_NAME(name):
153
154#define DEFINE_COMPILERRT_THUMB_FUNCTION(name)                                 \
155  DEFINE_CODE_STATE                                                            \
156  FILE_LEVEL_DIRECTIVE SEPARATOR                                               \
157  .globl SYMBOL_NAME(name) SEPARATOR                                           \
158  SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR                                  \
159  DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR                                    \
160  .thumb_func SEPARATOR                                                        \
161  SYMBOL_NAME(name):
162
163#define DEFINE_COMPILERRT_PRIVATE_FUNCTION(name)                               \
164  DEFINE_CODE_STATE                                                            \
165  FILE_LEVEL_DIRECTIVE SEPARATOR                                               \
166  .globl SYMBOL_NAME(name) SEPARATOR                                           \
167  SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR                                  \
168  HIDDEN(SYMBOL_NAME(name)) SEPARATOR                                          \
169  DECLARE_FUNC_ENCODING                                                        \
170  SYMBOL_NAME(name):
171
172#define DEFINE_COMPILERRT_PRIVATE_FUNCTION_UNMANGLED(name)                     \
173  DEFINE_CODE_STATE                                                            \
174  .globl name SEPARATOR                                                        \
175  SYMBOL_IS_FUNC(name) SEPARATOR                                               \
176  HIDDEN(name) SEPARATOR                                                       \
177  DECLARE_FUNC_ENCODING                                                        \
178  name:
179
180#define DEFINE_COMPILERRT_FUNCTION_ALIAS(name, target)                         \
181  .globl SYMBOL_NAME(name) SEPARATOR                                           \
182  SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR                                  \
183  DECLARE_SYMBOL_VISIBILITY(SYMBOL_NAME(name)) SEPARATOR                       \
184  .set SYMBOL_NAME(name), SYMBOL_NAME(target) SEPARATOR
185
186#if defined(__ARM_EABI__)
187#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name)                          \
188  DEFINE_COMPILERRT_FUNCTION_ALIAS(aeabi_name, name)
189#else
190#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name)
191#endif
192
193#ifdef __ELF__
194#define END_COMPILERRT_FUNCTION(name)                                          \
195  .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
196#else
197#define END_COMPILERRT_FUNCTION(name)
198#endif
199
200#endif // COMPILERRT_ASSEMBLY_H
201