1 //===--- BPF.cpp - Implement BPF target feature support -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements BPF TargetInfo objects.
11 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/MacroBuilder.h"
16 #include "clang/Basic/TargetBuiltins.h"
17 #include "llvm/ADT/StringRef.h"
19 using namespace clang
;
20 using namespace clang::targets
;
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.def"
28 void BPFTargetInfo::getTargetDefines(const LangOptions
&Opts
,
29 MacroBuilder
&Builder
) const {
30 Builder
.defineMacro("__bpf__");
31 Builder
.defineMacro("__BPF__");
33 std::string CPU
= getTargetOpts().CPU
;
35 Builder
.defineMacro("__BPF_CPU_VERSION__", "0");
38 if (CPU
.empty() || CPU
== "generic" || CPU
== "v1") {
39 Builder
.defineMacro("__BPF_CPU_VERSION__", "1");
43 std::string CpuVerNumStr
= CPU
.substr(1);
44 Builder
.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr
);
46 int CpuVerNum
= std::stoi(CpuVerNumStr
);
48 Builder
.defineMacro("__BPF_FEATURE_JMP_EXT");
51 Builder
.defineMacro("__BPF_FEATURE_JMP32");
52 Builder
.defineMacro("__BPF_FEATURE_ALU32");
56 Builder
.defineMacro("__BPF_FEATURE_LDSX");
57 Builder
.defineMacro("__BPF_FEATURE_MOVSX");
58 Builder
.defineMacro("__BPF_FEATURE_BSWAP");
59 Builder
.defineMacro("__BPF_FEATURE_SDIV_SMOD");
60 Builder
.defineMacro("__BPF_FEATURE_GOTOL");
61 Builder
.defineMacro("__BPF_FEATURE_ST");
65 static constexpr llvm::StringLiteral ValidCPUNames
[] = {"generic", "v1", "v2",
68 bool BPFTargetInfo::isValidCPUName(StringRef Name
) const {
69 return llvm::is_contained(ValidCPUNames
, Name
);
72 void BPFTargetInfo::fillValidCPUList(SmallVectorImpl
<StringRef
> &Values
) const {
73 Values
.append(std::begin(ValidCPUNames
), std::end(ValidCPUNames
));
76 ArrayRef
<Builtin::Info
> BPFTargetInfo::getTargetBuiltins() const {
77 return llvm::ArrayRef(BuiltinInfo
,
78 clang::BPF::LastTSBuiltin
- Builtin::FirstTSBuiltin
);
81 bool BPFTargetInfo::handleTargetFeatures(std::vector
<std::string
> &Features
,
82 DiagnosticsEngine
&Diags
) {
83 for (const auto &Feature
: Features
) {
84 if (Feature
== "+alu32") {