blob: ff49fd422fd38336a429c7899ef5f81273e058ae [file] [log] [blame]
Daniel Dunbar19336a22009-10-27 17:49:50 +00001/* ===-- assembly.h - compiler-rt assembler support macros -----------------===
2 *
Chandler Carruth7a739a02019-01-19 10:56:40 +00003 * 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
Daniel Dunbar19336a22009-10-27 17:49:50 +00006 *
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
15#ifndef COMPILERRT_ASSEMBLY_H
16#define COMPILERRT_ASSEMBLY_H
17
Daniel Dunbar19336a22009-10-27 17:49:50 +000018#if defined(__POWERPC__) || defined(__powerpc__) || defined(__ppc__)
Daniel Dunbar19336a22009-10-27 17:49:50 +000019#define SEPARATOR @
Daniel Dunbar19336a22009-10-27 17:49:50 +000020#else
Daniel Dunbar19336a22009-10-27 17:49:50 +000021#define SEPARATOR ;
Daniel Dunbar4c01bb72010-01-18 22:19:25 +000022#endif
Daniel Dunbar19336a22009-10-27 17:49:50 +000023
Daniel Dunbar4c01bb72010-01-18 22:19:25 +000024#if defined(__APPLE__)
Saleem Abdulrasool81ee1182014-05-18 18:39:10 +000025#define HIDDEN(name) .private_extern name
Anton Korobeynikov647fc732011-04-19 17:50:09 +000026#define LOCAL_LABEL(name) L_##name
Jonathan Roelofs238b9fd2014-05-12 17:38:36 +000027// tell linker it can break up file at label boundaries
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +000028#define FILE_LEVEL_DIRECTIVE .subsections_via_symbols
Joerg Sonnenberger8a0ba552014-01-14 23:31:23 +000029#define SYMBOL_IS_FUNC(name)
Saleem Abdulrasool8a21df02014-12-10 02:36:22 +000030#define CONST_SECTION .const
Bob Wilson751f9622014-10-04 20:11:10 +000031
Saleem Abdulrasool4c71a472016-06-22 22:09:42 +000032#define NO_EXEC_STACK_DIRECTIVE
33
Saleem Abdulrasool4040fbc2014-05-18 18:39:15 +000034#elif defined(__ELF__)
Bob Wilson751f9622014-10-04 20:11:10 +000035
Saleem Abdulrasool81ee1182014-05-18 18:39:10 +000036#define HIDDEN(name) .hidden name
Anton Korobeynikov647fc732011-04-19 17:50:09 +000037#define LOCAL_LABEL(name) .L_##name
Joerg Sonnenberger8a0ba552014-01-14 23:31:23 +000038#define FILE_LEVEL_DIRECTIVE
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +000039#if defined(__arm__)
40#define SYMBOL_IS_FUNC(name) .type name,%function
41#else
42#define SYMBOL_IS_FUNC(name) .type name,@function
43#endif
Saleem Abdulrasool8a21df02014-12-10 02:36:22 +000044#define CONST_SECTION .section .rodata
Bob Wilson751f9622014-10-04 20:11:10 +000045
Manoj Gupta0bbb5572017-05-15 23:13:54 +000046#if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
47 defined(__linux__)
Saleem Abdulrasool4c71a472016-06-22 22:09:42 +000048#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
49#else
50#define NO_EXEC_STACK_DIRECTIVE
51#endif
52
Bob Wilson751f9622014-10-04 20:11:10 +000053#else // !__APPLE__ && !__ELF__
54
Saleem Abdulrasool8a21df02014-12-10 02:36:22 +000055#define HIDDEN(name)
Saleem Abdulrasool4040fbc2014-05-18 18:39:15 +000056#define LOCAL_LABEL(name) .L ## name
Saleem Abdulrasoold87d6b72014-10-07 02:39:13 +000057#define FILE_LEVEL_DIRECTIVE
Saleem Abdulrasool4040fbc2014-05-18 18:39:15 +000058#define SYMBOL_IS_FUNC(name) \
59 .def name SEPARATOR \
Saleem Abdulrasool56d74312014-06-21 01:41:21 +000060 .scl 2 SEPARATOR \
Saleem Abdulrasool4040fbc2014-05-18 18:39:15 +000061 .type 32 SEPARATOR \
62 .endef
Saleem Abdulrasool8a21df02014-12-10 02:36:22 +000063#define CONST_SECTION .section .rdata,"rd"
Saleem Abdulrasoold87d6b72014-10-07 02:39:13 +000064
Saleem Abdulrasool4c71a472016-06-22 22:09:42 +000065#define NO_EXEC_STACK_DIRECTIVE
66
Daniel Dunbar19336a22009-10-27 17:49:50 +000067#endif
68
Joerg Sonnenberger734059a2014-01-24 13:39:51 +000069#if defined(__arm__)
Weiming Zhao4ae44f42017-08-14 20:48:47 +000070
71/*
72 * Determine actual [ARM][THUMB[1][2]] ISA using compiler predefined macros:
73 * - for '-mthumb -march=armv6' compiler defines '__thumb__'
74 * - for '-mthumb -march=armv7' compiler defines '__thumb__' and '__thumb2__'
75 */
76#if defined(__thumb2__) || defined(__thumb__)
Manoj Gupta4ce970d2017-10-02 20:56:49 +000077#define DEFINE_CODE_STATE .thumb SEPARATOR
Weiming Zhao4ae44f42017-08-14 20:48:47 +000078#define DECLARE_FUNC_ENCODING .thumb_func SEPARATOR
79#if defined(__thumb2__)
80#define USE_THUMB_2
81#define IT(cond) it cond
82#define ITT(cond) itt cond
83#define ITE(cond) ite cond
84#else
85#define USE_THUMB_1
86#define IT(cond)
87#define ITT(cond)
88#define ITE(cond)
89#endif // defined(__thumb__2)
90#else // !defined(__thumb2__) && !defined(__thumb__)
Manoj Gupta4ce970d2017-10-02 20:56:49 +000091#define DEFINE_CODE_STATE .arm SEPARATOR
Weiming Zhao4ae44f42017-08-14 20:48:47 +000092#define DECLARE_FUNC_ENCODING
93#define IT(cond)
94#define ITT(cond)
95#define ITE(cond)
96#endif
97
98#if defined(USE_THUMB_1) && defined(USE_THUMB_2)
99#error "USE_THUMB_1 and USE_THUMB_2 can't be defined together."
100#endif
101
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000102#if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
103#define ARM_HAS_BX
104#endif
Weiming Zhao4ae44f42017-08-14 20:48:47 +0000105#if !defined(__ARM_FEATURE_CLZ) && !defined(USE_THUMB_1) && \
Weiming Zhao86a022a2016-12-07 18:41:07 +0000106 (__ARM_ARCH >= 6 || (__ARM_ARCH == 5 && !defined(__ARM_ARCH_5__)))
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000107#define __ARM_FEATURE_CLZ
108#endif
109
110#ifdef ARM_HAS_BX
111#define JMP(r) bx r
Joerg Sonnenberger344b05d2014-07-20 20:00:26 +0000112#define JMPc(r, c) bx##c r
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000113#else
114#define JMP(r) mov pc, r
115#define JMPc(r, c) mov##c pc, r
116#endif
Saleem Abdulrasool57c07d82014-07-27 02:01:20 +0000117
Josh Gao8a444fa2015-08-21 00:25:37 +0000118// pop {pc} can't switch Thumb mode on ARMv4T
119#if __ARM_ARCH >= 5
120#define POP_PC() pop {pc}
121#else
122#define POP_PC() \
123 pop {ip}; \
124 JMP(ip)
125#endif
126
Weiming Zhao4ae44f42017-08-14 20:48:47 +0000127#if defined(USE_THUMB_2)
Weiming Zhao9736f902017-03-24 17:08:35 +0000128#define WIDE(op) op.w
129#else
Saleem Abdulrasool69ddd1d2014-07-27 02:01:24 +0000130#define WIDE(op) op
131#endif
Weiming Zhao6a52c892017-08-14 21:44:33 +0000132#else // !defined(__arm)
133#define DECLARE_FUNC_ENCODING
Manoj Gupta4ce970d2017-10-02 20:56:49 +0000134#define DEFINE_CODE_STATE
Weiming Zhao9736f902017-03-24 17:08:35 +0000135#endif
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000136
137#define GLUE2(a, b) a##b
Anton Korobeynikove2949352011-04-19 21:22:14 +0000138#define GLUE(a, b) GLUE2(a, b)
139#define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
140
Bob Wilson36c24252012-02-10 16:41:46 +0000141#ifdef VISIBILITY_HIDDEN
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000142#define DECLARE_SYMBOL_VISIBILITY(name) \
Saleem Abdulrasool81ee1182014-05-18 18:39:10 +0000143 HIDDEN(SYMBOL_NAME(name)) SEPARATOR
Daniel Dunbar6a571fb2010-01-18 22:19:34 +0000144#else
Bob Wilson41ae71d2011-08-22 21:49:47 +0000145#define DECLARE_SYMBOL_VISIBILITY(name)
146#endif
147
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000148#define DEFINE_COMPILERRT_FUNCTION(name) \
Manoj Gupta4ce970d2017-10-02 20:56:49 +0000149 DEFINE_CODE_STATE \
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000150 FILE_LEVEL_DIRECTIVE SEPARATOR \
151 .globl SYMBOL_NAME(name) SEPARATOR \
152 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
153 DECLARE_SYMBOL_VISIBILITY(name) \
Weiming Zhao4ae44f42017-08-14 20:48:47 +0000154 DECLARE_FUNC_ENCODING \
Daniel Dunbar19336a22009-10-27 17:49:50 +0000155 SYMBOL_NAME(name):
156
Steven Wu1dcfa4d2014-10-04 00:18:59 +0000157#define DEFINE_COMPILERRT_THUMB_FUNCTION(name) \
Manoj Gupta4ce970d2017-10-02 20:56:49 +0000158 DEFINE_CODE_STATE \
Steven Wu1dcfa4d2014-10-04 00:18:59 +0000159 FILE_LEVEL_DIRECTIVE SEPARATOR \
160 .globl SYMBOL_NAME(name) SEPARATOR \
Steven Wu1dcfa4d2014-10-04 00:18:59 +0000161 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
Saleem Abdulrasoold87d6b72014-10-07 02:39:13 +0000162 DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \
Saleem Abdulrasool11d42c12014-10-07 03:00:17 +0000163 .thumb_func SEPARATOR \
Steven Wu1dcfa4d2014-10-04 00:18:59 +0000164 SYMBOL_NAME(name):
165
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000166#define DEFINE_COMPILERRT_PRIVATE_FUNCTION(name) \
Manoj Gupta4ce970d2017-10-02 20:56:49 +0000167 DEFINE_CODE_STATE \
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000168 FILE_LEVEL_DIRECTIVE SEPARATOR \
169 .globl SYMBOL_NAME(name) SEPARATOR \
170 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
Saleem Abdulrasool81ee1182014-05-18 18:39:10 +0000171 HIDDEN(SYMBOL_NAME(name)) SEPARATOR \
Weiming Zhao4ae44f42017-08-14 20:48:47 +0000172 DECLARE_FUNC_ENCODING \
Daniel Dunbar19336a22009-10-27 17:49:50 +0000173 SYMBOL_NAME(name):
174
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000175#define DEFINE_COMPILERRT_PRIVATE_FUNCTION_UNMANGLED(name) \
Manoj Gupta4ce970d2017-10-02 20:56:49 +0000176 DEFINE_CODE_STATE \
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000177 .globl name SEPARATOR \
178 SYMBOL_IS_FUNC(name) SEPARATOR \
Saleem Abdulrasool81ee1182014-05-18 18:39:10 +0000179 HIDDEN(name) SEPARATOR \
Weiming Zhao4ae44f42017-08-14 20:48:47 +0000180 DECLARE_FUNC_ENCODING \
Daniel Dunbare136da92010-01-18 22:19:20 +0000181 name:
182
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000183#define DEFINE_COMPILERRT_FUNCTION_ALIAS(name, target) \
184 .globl SYMBOL_NAME(name) SEPARATOR \
185 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
Saleem Abdulrasoolb11de002016-10-28 23:37:50 +0000186 DECLARE_SYMBOL_VISIBILITY(SYMBOL_NAME(name)) SEPARATOR \
Anton Korobeynikov37b97d12011-04-19 17:51:24 +0000187 .set SYMBOL_NAME(name), SYMBOL_NAME(target) SEPARATOR
188
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000189#if defined(__ARM_EABI__)
190#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name) \
Anton Korobeynikov37b97d12011-04-19 17:51:24 +0000191 DEFINE_COMPILERRT_FUNCTION_ALIAS(aeabi_name, name)
192#else
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000193#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name)
Anton Korobeynikov37b97d12011-04-19 17:51:24 +0000194#endif
195
Joerg Sonnenbergerc16ed3d2014-01-23 18:31:46 +0000196#ifdef __ELF__
Saleem Abdulrasool5f81c702014-05-16 06:16:21 +0000197#define END_COMPILERRT_FUNCTION(name) \
198 .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
Joerg Sonnenbergerc16ed3d2014-01-23 18:31:46 +0000199#else
200#define END_COMPILERRT_FUNCTION(name)
201#endif
202
Daniel Dunbar19336a22009-10-27 17:49:50 +0000203#endif /* COMPILERRT_ASSEMBLY_H */