1 /***************************************************************************//** 2 * @file 3 * @brief Code Classification API (Internal) 4 ******************************************************************************* 5 * # License 6 * <b>Copyright 2023 Silicon Laboratories Inc. www.silabs.com</b> 7 ******************************************************************************* 8 * 9 * SPDX-License-Identifier: Zlib 10 * 11 * The licensor of this software is Silicon Laboratories Inc. 12 * 13 * This software is provided 'as-is', without any express or implied 14 * warranty. In no event will the authors be held liable for any damages 15 * arising from the use of this software. 16 * 17 * Permission is granted to anyone to use this software for any purpose, 18 * including commercial applications, and to alter it and redistribute it 19 * freely, subject to the following restrictions: 20 * 21 * 1. The origin of this software must not be misrepresented; you must not 22 * claim that you wrote the original software. If you use this software 23 * in a product, an acknowledgment in the product documentation would be 24 * appreciated but is not required. 25 * 2. Altered source versions must be plainly marked as such, and must not be 26 * misrepresented as being the original software. 27 * 3. This notice may not be removed or altered from any source distribution. 28 * 29 ******************************************************************************/ 30 31 #ifndef _SLI_CODE_CLASSIFICATION_H_ 32 #define _SLI_CODE_CLASSIFICATION_H_ 33 34 // Standard Code Classes 35 #define SL_CODE_CLASS_TIME_CRITICAL timecritical 36 37 /******************************************************************************/ 38 /* Helper Macros */ 39 /******************************************************************************/ 40 41 // Stringize tokens 42 #define _SL_CC_STRINGIZE(X) #X 43 #define _SL_CC_XSTRINGIZE(X) _SL_CC_STRINGIZE(X) 44 #define _SL_CC_CONCAT3(A, B, C) A B C 45 #define _SL_CC_CONCAT4(A, B, C, D) A B C D 46 47 /******************************************************************************/ 48 /* Compiler Specific Macros */ 49 /******************************************************************************/ 50 51 // The directive that is built is dependent on the compiler. Section names are 52 // appended with an identifier generated from __COUNTER__ and __LINE__ so that 53 // functions are more likely to be separated into unique sections. Doing this 54 // allows the linker to discard unused functions with more granularity. 55 #if defined(__GNUC__) && !(defined(__llvm__) || defined(SLI_CODE_CLASSIFICATION_DISABLE)) 56 57 // With GCC, __attribute__ can be used to specify the input section of 58 // functions. 59 #define _SL_CC_SECTION(section_name, count, line) \ 60 __attribute__((section(_SL_CC_CONCAT3(_SL_CC_XSTRINGIZE(section_name), _SL_CC_XSTRINGIZE(count), _SL_CC_XSTRINGIZE(line))))) 61 62 #elif defined(__ICCARM__) && !defined(SLI_CODE_CLASSIFICATION_DISABLE) 63 64 // With IAR, _Pragma can be used to specify the input section of 65 // functions. 66 #define _SL_CC_SECTION(section_name, count, line) \ 67 _Pragma(_SL_CC_XSTRINGIZE(_SL_CC_CONCAT4(location =, _SL_CC_XSTRINGIZE(section_name), _SL_CC_XSTRINGIZE(count), _SL_CC_XSTRINGIZE(line)))) 68 69 #elif defined(__llvm__) && !defined(SLI_CODE_CLASSIFICATION_DISABLE) 70 71 // With llvm, __attribute__ can be used to specify the input section of 72 // functions. 73 74 // However the syntax of the string within the section directive is 75 // dependent on the specifics of the target backend (e.g. osx) 76 #if defined(__MACH__) && defined(SLI_CODE_CLASSIFICATION_OSX_ENABLE) 77 // code classifcation is not supported on OSX and can have weird 78 // interactions for executable code so it is disabled by default 79 // since it can be useful for code analysis allow it as an opt-in feature 80 #define _SL_CC_SECTION(section_name, count, line) \ 81 __attribute__((section("sl_cc,code_class" _SL_CC_XSTRINGIZE(count) _SL_CC_XSTRINGIZE(line)))) 82 #else 83 #define _SL_CC_SECTION(section_name, count, line) 84 #endif // defined(__MACH__) 85 86 #elif defined(SLI_CODE_CLASSIFICATION_DISABLE) 87 88 #define _SL_CC_SECTION(section_name, count, line) 89 90 #else 91 #error "(sli_code_classification.h): Code classification does not support \ 92 the chosen compiler." 93 #endif // __GNUC__ 94 95 /******************************************************************************/ 96 /* Compiler Generic Macros */ 97 /******************************************************************************/ 98 99 // Build the linker section name based on the name of the component and the 100 // code classes. 101 #define _SL_CODE_CLASS_SECTION_CONCAT1(component, p1) \ 102 text_ ## component ## _ ## p1 103 #define _SL_CODE_CLASS_SECTION_CONCAT2(component, p1, p2) \ 104 text_ ## component ## _ ## p1 ## _ ## p2 105 106 // Build the compiler specific directives 107 #define _SL_CODE_CLASS1(component, c1) \ 108 _SL_CC_SECTION(_SL_CODE_CLASS_SECTION_CONCAT1(component, c1), __COUNTER__, __LINE__) 109 #define _SL_CODE_CLASS2(component, c1, c2) \ 110 _SL_CC_SECTION(_SL_CODE_CLASS_SECTION_CONCAT2(component, c1, c2), __COUNTER__, __LINE__) 111 112 // Utilities to dispatch a macro with the correct number of parameters. 113 // Update COUNT_N and COUNT macros if the upper limit of code class 114 // combinations increases. 115 #define _SL_CC_COUNT_N(_1, _2, N, ...) N 116 #define _SL_CC_COUNT(...) _SL_CC_COUNT_N(__VA_ARGS__, 2, 1) 117 #define _SL_CC_IDENTITY(N) N 118 #define _SL_CC_APPLY(macro, ...) _SL_CC_IDENTITY(macro(__VA_ARGS__)) 119 120 // Dispatch _SL_CODE_CLASSX with the correct number of parameters. 121 #define _SL_CC_DISPATCH(N) _SL_CODE_CLASS ## N 122 123 /******************************************************************************/ 124 /* Macro API (Internal) */ 125 /******************************************************************************/ 126 127 // Variadic macro to specify the code class membership of a function. 128 #define SL_CODE_CLASSIFY(component, ...) \ 129 _SL_CC_IDENTITY(_SL_CC_APPLY(_SL_CC_DISPATCH, _SL_CC_COUNT(__VA_ARGS__)))(component, __VA_ARGS__) 130 131 #endif // _SLI_CODE_CLASSIFICATION_H_ 132