clang 23.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"
16#include "llvm/ADT/StringRef.h"
17
18using namespace clang;
19using namespace clang::targets;
20
21static constexpr int NumBuiltins =
23
24#define GET_BUILTIN_STR_TABLE
25#include "clang/Basic/BuiltinsBPF.inc"
26#undef GET_BUILTIN_STR_TABLE
27
28static constexpr Builtin::Info BuiltinInfos[] = {
29#define GET_BUILTIN_INFOS
30#include "clang/Basic/BuiltinsBPF.inc"
31#undef GET_BUILTIN_INFOS
32};
33static_assert(std::size(BuiltinInfos) == NumBuiltins);
34
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 Builder.defineMacro("__BPF_FEATURE_STACK_ARGUMENT");
50
51 if (CPU.empty())
52 CPU = "v3";
53
54 if (CPU == "generic" || CPU == "v1") {
55 Builder.defineMacro("__BPF_CPU_VERSION__", "1");
56 return;
57 }
58
59 std::string CpuVerNumStr = CPU.substr(1);
60 Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr);
61
62 int CpuVerNum = std::stoi(CpuVerNumStr);
63 if (CpuVerNum >= 2)
64 Builder.defineMacro("__BPF_FEATURE_JMP_EXT");
65
66 if (CpuVerNum >= 3) {
67 Builder.defineMacro("__BPF_FEATURE_JMP32");
68 Builder.defineMacro("__BPF_FEATURE_ALU32");
69 }
70
71 if (CpuVerNum >= 4) {
72 Builder.defineMacro("__BPF_FEATURE_LDSX");
73 Builder.defineMacro("__BPF_FEATURE_MOVSX");
74 Builder.defineMacro("__BPF_FEATURE_BSWAP");
75 Builder.defineMacro("__BPF_FEATURE_SDIV_SMOD");
76 Builder.defineMacro("__BPF_FEATURE_GOTOL");
77 Builder.defineMacro("__BPF_FEATURE_ST");
78 Builder.defineMacro("__BPF_FEATURE_LOAD_ACQ_STORE_REL");
79 Builder.defineMacro("__BPF_FEATURE_GOTOX");
80 }
81}
82
83static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic", "v1", "v2",
84 "v3", "v4", "probe"};
85
86bool BPFTargetInfo::isValidCPUName(StringRef Name) const {
87 return llvm::is_contained(ValidCPUNames, Name);
88}
89
91 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
92}
93
98
99bool BPFTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
100 DiagnosticsEngine &Diags) {
101 for (const auto &Feature : Features) {
102 if (Feature == "+alu32") {
103 HasAlu32 = true;
104 }
105 }
106
107 return true;
108}
static constexpr llvm::StringTable BuiltinStrings
Definition ARM.cpp:1115
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition BPF.cpp:83
static constexpr Builtin::Info BuiltinInfos[]
Definition Builtins.cpp:38
static constexpr unsigned NumBuiltins
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:233
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition TargetInfo.h:327
std::string CPU
If given, the name of the target CPU to generate code for.
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:99
llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition BPF.cpp:95
void fillValidCPUList(SmallVectorImpl< StringRef > &Values) const override
Fill a SmallVectorImpl with the valid values to setCPU.
Definition BPF.cpp:90
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition BPF.cpp:86
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition BPF.cpp:35
The JSON file list parser is used to communicate input to InstallAPI.
The info used to represent each builtin.
Definition Builtins.h:79