| 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("__bpf__"); |
| 38 |
Builder.defineMacro("__BPF__"); |
| 39 |
|
| 40 |
std::string CPU = getTargetOpts().CPU; |
| 41 |
if (CPU == "probe") { |
| 42 |
Builder.defineMacro("__BPF_CPU_VERSION__", "0"); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
Builder.defineMacro("__BPF_FEATURE_ADDR_SPACE_CAST"); |
| 47 |
Builder.defineMacro("__BPF_FEATURE_MAY_GOTO"); |
| 48 |
Builder.defineMacro("__BPF_FEATURE_ATOMIC_MEM_ORDERING"); |
| 49 |
|
| 50 |
if (CPU.empty()) |
| 51 |
CPU = "v3"; |
| 52 |
|
| 53 |
if (CPU == "generic" || CPU == "v1") { |
| 54 |
Builder.defineMacro("__BPF_CPU_VERSION__", "1"); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
std::string CpuVerNumStr = CPU.substr(1); |
| 59 |
Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr); |
| 60 |
|
| 61 |
int CpuVerNum = std::stoi(CpuVerNumStr); |
| 62 |
if (CpuVerNum >= 2) |
| 63 |
Builder.defineMacro("__BPF_FEATURE_JMP_EXT"); |
| 64 |
|
| 65 |
if (CpuVerNum >= 3) { |
| 66 |
Builder.defineMacro("__BPF_FEATURE_JMP32"); |
| 67 |
Builder.defineMacro("__BPF_FEATURE_ALU32"); |
| 68 |
} |
| 69 |
|
| 70 |
if (CpuVerNum >= 4) { |
| 71 |
Builder.defineMacro("__BPF_FEATURE_LDSX"); |
| 72 |
Builder.defineMacro("__BPF_FEATURE_MOVSX"); |
| 73 |
Builder.defineMacro("__BPF_FEATURE_BSWAP"); |
| 74 |
Builder.defineMacro("__BPF_FEATURE_SDIV_SMOD"); |
| 75 |
Builder.defineMacro("__BPF_FEATURE_GOTOL"); |
| 76 |
Builder.defineMacro("__BPF_FEATURE_ST"); |
| 77 |
Builder.defineMacro("__BPF_FEATURE_LOAD_ACQ_STORE_REL"); |
| 78 |
Builder.defineMacro("__BPF_FEATURE_GOTOX"); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic", "v1", "v2", |
| 83 |
"v3", "v4", "probe"}; |
| 84 |
|
| 85 |
bool BPFTargetInfo::isValidCPUName(StringRef Name) const { |
| 86 |
return llvm::is_contained(ValidCPUNames, Name); |
| 87 |
} |
| 88 |
|
| 89 |
void BPFTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const { |
| 90 |
Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames)); |
| 91 |
} |
| 92 |
|
| 93 |
llvm::SmallVector<Builtin::InfosShard> |
| 94 |
BPFTargetInfo::getTargetBuiltins() const { |
| 95 |
return {{&BuiltinStrings, BuiltinInfos}}; |
| 96 |
} |
| 97 |
|
| 98 |
bool BPFTargetInfo::handleTargetFeatures(std::vector<std::string> &Features, |
| 99 |
DiagnosticsEngine &Diags) { |
| 100 |
for (const auto &Feature : Features) { |
| 101 |
if (Feature == "+alu32") { |
| 102 |
HasAlu32 = true; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return true; |
| 107 |
} |