| 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 "Targets.h" |
| 15 |
#include "clang/Basic/MacroBuilder.h" |
| 16 |
#include "clang/Basic/TargetBuiltins.h" |
| 17 |
#include "llvm/ADT/StringRef.h" |
| 18 |
|
| 19 |
using namespace clang; |
| 20 |
using namespace clang::targets; |
| 21 |
|
| 22 |
static constexpr Builtin::Info BuiltinInfo[] = { |
| 23 |
#define BUILTIN(ID, TYPE, ATTRS) \ |
| 24 |
{#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES}, |
| 25 |
#include "clang/Basic/BuiltinsBPF.inc" |
| 26 |
}; |
| 27 |
|
| 28 |
void BPFTargetInfo::getTargetDefines(const LangOptions &Opts, |
| 29 |
MacroBuilder &Builder) const { |
| 30 |
Builder.defineMacro("__bpf__"); |
| 31 |
Builder.defineMacro("__BPF__"); |
| 32 |
|
| 33 |
std::string CPU = getTargetOpts().CPU; |
| 34 |
if (CPU == "probe") { |
| 35 |
Builder.defineMacro("__BPF_CPU_VERSION__", "0"); |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
Builder.defineMacro("__BPF_FEATURE_ADDR_SPACE_CAST"); |
| 40 |
|
| 41 |
if (CPU.empty() || CPU == "generic" || CPU == "v1") { |
| 42 |
Builder.defineMacro("__BPF_CPU_VERSION__", "1"); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
std::string CpuVerNumStr = CPU.substr(1); |
| 47 |
Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr); |
| 48 |
Builder.defineMacro("__BPF_FEATURE_MAY_GOTO"); |
| 49 |
|
| 50 |
int CpuVerNum = std::stoi(CpuVerNumStr); |
| 51 |
if (CpuVerNum >= 2) |
| 52 |
Builder.defineMacro("__BPF_FEATURE_JMP_EXT"); |
| 53 |
|
| 54 |
if (CpuVerNum >= 3) { |
| 55 |
Builder.defineMacro("__BPF_FEATURE_JMP32"); |
| 56 |
Builder.defineMacro("__BPF_FEATURE_ALU32"); |
| 57 |
} |
| 58 |
|
| 59 |
if (CpuVerNum >= 4) { |
| 60 |
Builder.defineMacro("__BPF_FEATURE_LDSX"); |
| 61 |
Builder.defineMacro("__BPF_FEATURE_MOVSX"); |
| 62 |
Builder.defineMacro("__BPF_FEATURE_BSWAP"); |
| 63 |
Builder.defineMacro("__BPF_FEATURE_SDIV_SMOD"); |
| 64 |
Builder.defineMacro("__BPF_FEATURE_GOTOL"); |
| 65 |
Builder.defineMacro("__BPF_FEATURE_ST"); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic", "v1", "v2", |
| 70 |
"v3", "v4", "probe"}; |
| 71 |
|
| 72 |
bool BPFTargetInfo::isValidCPUName(StringRef Name) const { |
| 73 |
return llvm::is_contained(ValidCPUNames, Name); |
| 74 |
} |
| 75 |
|
| 76 |
void BPFTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const { |
| 77 |
Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames)); |
| 78 |
} |
| 79 |
|
| 80 |
ArrayRef<Builtin::Info> BPFTargetInfo::getTargetBuiltins() const { |
| 81 |
return llvm::ArrayRef(BuiltinInfo, |
| 82 |
clang::BPF::LastTSBuiltin - Builtin::FirstTSBuiltin); |
| 83 |
} |
| 84 |
|
| 85 |
bool BPFTargetInfo::handleTargetFeatures(std::vector<std::string> &Features, |
| 86 |
DiagnosticsEngine &Diags) { |
| 87 |
for (const auto &Feature : Features) { |
| 88 |
if (Feature == "+alu32") { |
| 89 |
HasAlu32 = true; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return true; |
| 94 |
} |