1 //===- subzero/src/IceBuildDefs.h - Translator build defines ----*- C++ -*-===// 2 // 3 // The Subzero Code Generator 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief Define the Ice::BuildDefs namespace 12 //===----------------------------------------------------------------------===// 13 14 #ifndef SUBZERO_SRC_ICEBUILDDEFS_H 15 #define SUBZERO_SRC_ICEBUILDDEFS_H 16 17 namespace Ice { 18 /// \brief Defines constexpr functions that express various Subzero build 19 /// system defined values. 20 /// 21 /// These resulting constexpr functions allow code to in effect be 22 /// conditionally compiled without having to do this using the older C++ 23 /// preprocessor solution. 24 25 /** \verbatim 26 27 For example whenever the value of FEATURE_SUPPORTED is needed, instead 28 of (except in these constexpr functions): 29 30 #if FEATURE_SUPPORTED ... 31 ... 32 #endif 33 34 We can have: 35 36 namespace Ice { 37 namespace BuildDefs { 38 39 // Use this form when FEATURE_SUPPORTED is guaranteed to be defined on the 40 // C++ compiler command line as 0 or 1. 41 constexpr bool hasFeature() { return FEATURE_SUPPORTED; } 42 43 or 44 45 // Use this form when FEATURE_SUPPORTED may not necessarily be defined on 46 // the C++ compiler command line. 47 constexpr bool hasFeature() { 48 #if FEATURE_SUPPORTED 49 return true; 50 #else // !FEATURE_SUPPORTED 51 return false; 52 #endif // !FEATURE_SUPPORTED 53 } 54 55 ...} // end of namespace BuildDefs 56 } // end of namespace Ice 57 58 59 And later in the code: 60 61 if (Ice::BuildDefs::hasFeature() { 62 ... 63 } 64 65 \endverbatim 66 67 Since hasFeature() returns a constexpr, an optimizing compiler will know to 68 keep or discard the above fragment. In addition, the code will always be 69 looked at by the compiler which eliminates the problem with defines in that 70 if you don't build that variant, you don't even know if the code would 71 compile unless you build with that variant. 72 73 **/ 74 75 namespace BuildDefs { 76 77 // The ALLOW_* etc. symbols must be #defined to zero or non-zero. 78 /// Return true if ALLOW_DUMP is defined as a non-zero value 79 constexpr bool dump() { return ALLOW_DUMP; } 80 /// Return true if ALLOW_TIMERS is defined as a non-zero value 81 constexpr bool timers() { return ALLOW_TIMERS; } 82 /// Return true if ALLOW_LLVM_CL is defined as a non-zero value 83 // TODO(stichnot): this ALLOW_LLVM_CL is a TBD option which will 84 // allow for replacement of llvm:cl command line processor with a 85 // smaller footprint version for Subzero. 86 constexpr bool llvmCl() { return ALLOW_LLVM_CL; } 87 /// Return true if ALLOW_LLVM_IR is defined as a non-zero value 88 constexpr bool llvmIr() { return ALLOW_LLVM_IR; } 89 /// Return true if ALLOW_LLVM_IR_AS_INPUT is defined as a non-zero value 90 constexpr bool llvmIrAsInput() { return ALLOW_LLVM_IR_AS_INPUT; } 91 /// Return true if ALLOW_MINIMAL_BUILD is defined as a non-zero value 92 constexpr bool minimal() { return ALLOW_MINIMAL_BUILD; } 93 /// Return true if ALLOW_WASM is defined as a non-zero value 94 constexpr bool wasm() { return ALLOW_WASM; } 95 96 /// Return true if NDEBUG is defined 97 constexpr bool asserts() { 98 #ifdef NDEBUG 99 return false; 100 #else // !NDEBUG 101 return true; 102 #endif // !NDEBUG 103 } 104 105 /// Return true if PNACL_BROWSER_TRANSLATOR is defined 106 constexpr bool browser() { 107 #if PNACL_BROWSER_TRANSLATOR 108 return true; 109 #else // !PNACL_BROWSER_TRANSLATOR 110 return false; 111 #endif // !PNACL_BROWSER_TRANSLATOR 112 } 113 114 /// Return true if ALLOW_EXTRA_VALIDATION is defined 115 constexpr bool extraValidation() { 116 #if ALLOW_EXTRA_VALIDATION 117 return true; 118 #else // !ALLOW_EXTRA_VALIDATION 119 return false; 120 #endif // !ALLOW_EXTRA_VALIDATION 121 } 122 123 } // end of namespace BuildDefs 124 } // end of namespace Ice 125 126 #endif // SUBZERO_SRC_ICEBUILDDEFS_H 127