/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/Targets/BPF.cpp
Line | Count | Source (jump to first uncovered line) |
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.def" |
26 | | }; |
27 | | |
28 | | void BPFTargetInfo::getTargetDefines(const LangOptions &Opts, |
29 | 27 | MacroBuilder &Builder) const { |
30 | 27 | Builder.defineMacro("__bpf__"); |
31 | 27 | Builder.defineMacro("__BPF__"); |
32 | | |
33 | 27 | std::string CPU = getTargetOpts().CPU; |
34 | 27 | if (CPU == "probe") { |
35 | 1 | Builder.defineMacro("__BPF_CPU_VERSION__", "0"); |
36 | 1 | return; |
37 | 1 | } |
38 | 26 | if (CPU.empty() || CPU == "generic"5 || CPU == "v1"4 ) { |
39 | 23 | Builder.defineMacro("__BPF_CPU_VERSION__", "1"); |
40 | 23 | return; |
41 | 23 | } |
42 | | |
43 | 3 | std::string CpuVerNumStr = CPU.substr(1); |
44 | 3 | Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr); |
45 | | |
46 | 3 | int CpuVerNum = std::stoi(CpuVerNumStr); |
47 | 3 | if (CpuVerNum >= 2) |
48 | 3 | Builder.defineMacro("__BPF_FEATURE_JMP_EXT"); |
49 | | |
50 | 3 | if (CpuVerNum >= 3) { |
51 | 2 | Builder.defineMacro("__BPF_FEATURE_JMP32"); |
52 | 2 | Builder.defineMacro("__BPF_FEATURE_ALU32"); |
53 | 2 | } |
54 | | |
55 | 3 | if (CpuVerNum >= 4) { |
56 | 1 | Builder.defineMacro("__BPF_FEATURE_LDSX"); |
57 | 1 | Builder.defineMacro("__BPF_FEATURE_MOVSX"); |
58 | 1 | Builder.defineMacro("__BPF_FEATURE_BSWAP"); |
59 | 1 | Builder.defineMacro("__BPF_FEATURE_SDIV_SMOD"); |
60 | 1 | Builder.defineMacro("__BPF_FEATURE_GOTOL"); |
61 | 1 | Builder.defineMacro("__BPF_FEATURE_ST"); |
62 | 1 | } |
63 | 3 | } |
64 | | |
65 | | static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic", "v1", "v2", |
66 | | "v3", "v4", "probe"}; |
67 | | |
68 | 7 | bool BPFTargetInfo::isValidCPUName(StringRef Name) const { |
69 | 7 | return llvm::is_contained(ValidCPUNames, Name); |
70 | 7 | } |
71 | | |
72 | 1 | void BPFTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const { |
73 | 1 | Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames)); |
74 | 1 | } |
75 | | |
76 | 27 | ArrayRef<Builtin::Info> BPFTargetInfo::getTargetBuiltins() const { |
77 | 27 | return llvm::ArrayRef(BuiltinInfo, |
78 | 27 | clang::BPF::LastTSBuiltin - Builtin::FirstTSBuiltin); |
79 | 27 | } |
80 | | |
81 | | bool BPFTargetInfo::handleTargetFeatures(std::vector<std::string> &Features, |
82 | 27 | DiagnosticsEngine &Diags) { |
83 | 27 | for (const auto &Feature : Features) { |
84 | 0 | if (Feature == "+alu32") { |
85 | 0 | HasAlu32 = true; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | 27 | return true; |
90 | 27 | } |