| 1 | //===--- BPF.cpp - Implement BPF target feature support -------------------===// |
| 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 implements BPF TargetInfo objects. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "BPF.h" |
| 14 | #include "clang/Basic/MacroBuilder.h" |
| 15 | #include "clang/Basic/TargetBuiltins.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | using namespace clang::targets; |
| 20 | |
| 21 | static constexpr int NumBuiltins = |
| 22 | clang::BPF::LastTSBuiltin - Builtin::FirstTSBuiltin; |
| 23 | |
| 24 | #define GET_BUILTIN_STR_TABLE |
| 25 | #include "clang/Basic/BuiltinsBPF.inc" |
| 26 | #undef GET_BUILTIN_STR_TABLE |
| 27 | |
| 28 | static constexpr Builtin::Info BuiltinInfos[] = { |
| 29 | #define GET_BUILTIN_INFOS |
| 30 | #include "clang/Basic/BuiltinsBPF.inc" |
| 31 | #undef GET_BUILTIN_INFOS |
| 32 | }; |
| 33 | static_assert(std::size(BuiltinInfos) == NumBuiltins); |
| 34 | |
| 35 | void BPFTargetInfo::getTargetDefines(const LangOptions &Opts, |
| 36 | MacroBuilder &Builder) const { |
| 37 | Builder.defineMacro(Name: "__bpf__" ); |
| 38 | Builder.defineMacro(Name: "__BPF__" ); |
| 39 | |
| 40 | std::string CPU = getTargetOpts().CPU; |
| 41 | if (CPU == "probe" ) { |
| 42 | Builder.defineMacro(Name: "__BPF_CPU_VERSION__" , Value: "0" ); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | Builder.defineMacro(Name: "__BPF_FEATURE_ADDR_SPACE_CAST" ); |
| 47 | Builder.defineMacro(Name: "__BPF_FEATURE_MAY_GOTO" ); |
| 48 | Builder.defineMacro(Name: "__BPF_FEATURE_ATOMIC_MEM_ORDERING" ); |
| 49 | |
| 50 | if (CPU.empty()) |
| 51 | CPU = "v3" ; |
| 52 | |
| 53 | if (CPU == "generic" || CPU == "v1" ) { |
| 54 | Builder.defineMacro(Name: "__BPF_CPU_VERSION__" , Value: "1" ); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | std::string CpuVerNumStr = CPU.substr(pos: 1); |
| 59 | Builder.defineMacro(Name: "__BPF_CPU_VERSION__" , Value: CpuVerNumStr); |
| 60 | |
| 61 | int CpuVerNum = std::stoi(str: CpuVerNumStr); |
| 62 | if (CpuVerNum >= 2) |
| 63 | Builder.defineMacro(Name: "__BPF_FEATURE_JMP_EXT" ); |
| 64 | |
| 65 | if (CpuVerNum >= 3) { |
| 66 | Builder.defineMacro(Name: "__BPF_FEATURE_JMP32" ); |
| 67 | Builder.defineMacro(Name: "__BPF_FEATURE_ALU32" ); |
| 68 | } |
| 69 | |
| 70 | if (CpuVerNum >= 4) { |
| 71 | Builder.defineMacro(Name: "__BPF_FEATURE_LDSX" ); |
| 72 | Builder.defineMacro(Name: "__BPF_FEATURE_MOVSX" ); |
| 73 | Builder.defineMacro(Name: "__BPF_FEATURE_BSWAP" ); |
| 74 | Builder.defineMacro(Name: "__BPF_FEATURE_SDIV_SMOD" ); |
| 75 | Builder.defineMacro(Name: "__BPF_FEATURE_GOTOL" ); |
| 76 | Builder.defineMacro(Name: "__BPF_FEATURE_ST" ); |
| 77 | Builder.defineMacro(Name: "__BPF_FEATURE_LOAD_ACQ_STORE_REL" ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic" , "v1" , "v2" , |
| 82 | "v3" , "v4" , "probe" }; |
| 83 | |
| 84 | bool BPFTargetInfo::isValidCPUName(StringRef Name) const { |
| 85 | return llvm::is_contained(Range: ValidCPUNames, Element: Name); |
| 86 | } |
| 87 | |
| 88 | void BPFTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const { |
| 89 | Values.append(in_start: std::begin(arr: ValidCPUNames), in_end: std::end(arr: ValidCPUNames)); |
| 90 | } |
| 91 | |
| 92 | llvm::SmallVector<Builtin::InfosShard> |
| 93 | BPFTargetInfo::getTargetBuiltins() const { |
| 94 | return {{.Strings: &BuiltinStrings, .Infos: BuiltinInfos}}; |
| 95 | } |
| 96 | |
| 97 | bool BPFTargetInfo::handleTargetFeatures(std::vector<std::string> &Features, |
| 98 | DiagnosticsEngine &Diags) { |
| 99 | for (const auto &Feature : Features) { |
| 100 | if (Feature == "+alu32" ) { |
| 101 | HasAlu32 = true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |