clang 20.0.0git
BPF.cpp
Go to the documentation of this file.
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"
17#include "llvm/ADT/StringRef.h"
18
19using namespace clang;
20using namespace clang::targets;
21
22static 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
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 Builder.defineMacro("__BPF_FEATURE_MAY_GOTO");
41 Builder.defineMacro("__BPF_FEATURE_ATOMIC_MEM_ORDERING");
42
43 if (CPU.empty())
44 CPU = "v3";
45
46 if (CPU == "generic" || CPU == "v1") {
47 Builder.defineMacro("__BPF_CPU_VERSION__", "1");
48 return;
49 }
50
51 std::string CpuVerNumStr = CPU.substr(1);
52 Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr);
53
54 int CpuVerNum = std::stoi(CpuVerNumStr);
55 if (CpuVerNum >= 2)
56 Builder.defineMacro("__BPF_FEATURE_JMP_EXT");
57
58 if (CpuVerNum >= 3) {
59 Builder.defineMacro("__BPF_FEATURE_JMP32");
60 Builder.defineMacro("__BPF_FEATURE_ALU32");
61 }
62
63 if (CpuVerNum >= 4) {
64 Builder.defineMacro("__BPF_FEATURE_LDSX");
65 Builder.defineMacro("__BPF_FEATURE_MOVSX");
66 Builder.defineMacro("__BPF_FEATURE_BSWAP");
67 Builder.defineMacro("__BPF_FEATURE_SDIV_SMOD");
68 Builder.defineMacro("__BPF_FEATURE_GOTOL");
69 Builder.defineMacro("__BPF_FEATURE_ST");
70 }
71}
72
73static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic", "v1", "v2",
74 "v3", "v4", "probe"};
75
76bool BPFTargetInfo::isValidCPUName(StringRef Name) const {
77 return llvm::is_contained(ValidCPUNames, Name);
78}
79
81 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
82}
83
87}
88
89bool BPFTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
90 DiagnosticsEngine &Diags) {
91 for (const auto &Feature : Features) {
92 if (Feature == "+alu32") {
93 HasAlu32 = true;
94 }
95 }
96
97 return true;
98}
static constexpr Builtin::Info BuiltinInfo[]
Definition: BPF.cpp:22
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition: BPF.cpp:73
static constexpr Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:32
Defines the clang::MacroBuilder utility class.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition: TargetInfo.h:311
std::string CPU
If given, the name of the target CPU to generate code for.
Definition: TargetOptions.h:36
bool handleTargetFeatures(std::vector< std::string > &Features, DiagnosticsEngine &Diags) override
Perform initialization based on the user configured set of features (e.g., +sse4).
Definition: BPF.cpp:89
void fillValidCPUList(SmallVectorImpl< StringRef > &Values) const override
Fill a SmallVectorImpl with the valid values to setCPU.
Definition: BPF.cpp:80
ArrayRef< Builtin::Info > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition: BPF.cpp:84
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition: BPF.cpp:76
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: BPF.cpp:28
The JSON file list parser is used to communicate input to InstallAPI.