xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Targets/BPF.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===--- BPF.cpp - Implement BPF target feature support -------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements BPF TargetInfo objects.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "BPF.h"
140b57cec5SDimitry Andric #include "Targets.h"
150b57cec5SDimitry Andric #include "clang/Basic/MacroBuilder.h"
16a7dea167SDimitry Andric #include "clang/Basic/TargetBuiltins.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace clang;
200b57cec5SDimitry Andric using namespace clang::targets;
210b57cec5SDimitry Andric 
22bdd1243dSDimitry Andric static constexpr Builtin::Info BuiltinInfo[] = {
23a7dea167SDimitry Andric #define BUILTIN(ID, TYPE, ATTRS)                                               \
24bdd1243dSDimitry Andric   {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
25*0fca6ea1SDimitry Andric #include "clang/Basic/BuiltinsBPF.inc"
26a7dea167SDimitry Andric };
27a7dea167SDimitry Andric 
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const280b57cec5SDimitry Andric void BPFTargetInfo::getTargetDefines(const LangOptions &Opts,
290b57cec5SDimitry Andric                                      MacroBuilder &Builder) const {
300b57cec5SDimitry Andric   Builder.defineMacro("__bpf__");
310b57cec5SDimitry Andric   Builder.defineMacro("__BPF__");
325f757f3fSDimitry Andric 
335f757f3fSDimitry Andric   std::string CPU = getTargetOpts().CPU;
345f757f3fSDimitry Andric   if (CPU == "probe") {
355f757f3fSDimitry Andric     Builder.defineMacro("__BPF_CPU_VERSION__", "0");
365f757f3fSDimitry Andric     return;
375f757f3fSDimitry Andric   }
38*0fca6ea1SDimitry Andric 
39*0fca6ea1SDimitry Andric   Builder.defineMacro("__BPF_FEATURE_ADDR_SPACE_CAST");
40*0fca6ea1SDimitry Andric 
415f757f3fSDimitry Andric   if (CPU.empty() || CPU == "generic" || CPU == "v1") {
425f757f3fSDimitry Andric     Builder.defineMacro("__BPF_CPU_VERSION__", "1");
435f757f3fSDimitry Andric     return;
445f757f3fSDimitry Andric   }
455f757f3fSDimitry Andric 
465f757f3fSDimitry Andric   std::string CpuVerNumStr = CPU.substr(1);
475f757f3fSDimitry Andric   Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr);
48*0fca6ea1SDimitry Andric   Builder.defineMacro("__BPF_FEATURE_MAY_GOTO");
495f757f3fSDimitry Andric 
505f757f3fSDimitry Andric   int CpuVerNum = std::stoi(CpuVerNumStr);
515f757f3fSDimitry Andric   if (CpuVerNum >= 2)
525f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_JMP_EXT");
535f757f3fSDimitry Andric 
545f757f3fSDimitry Andric   if (CpuVerNum >= 3) {
555f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_JMP32");
565f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_ALU32");
575f757f3fSDimitry Andric   }
585f757f3fSDimitry Andric 
595f757f3fSDimitry Andric   if (CpuVerNum >= 4) {
605f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_LDSX");
615f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_MOVSX");
625f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_BSWAP");
635f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_SDIV_SMOD");
645f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_GOTOL");
655f757f3fSDimitry Andric     Builder.defineMacro("__BPF_FEATURE_ST");
665f757f3fSDimitry Andric   }
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric static constexpr llvm::StringLiteral ValidCPUNames[] = {"generic", "v1", "v2",
705f757f3fSDimitry Andric                                                         "v3", "v4", "probe"};
710b57cec5SDimitry Andric 
isValidCPUName(StringRef Name) const720b57cec5SDimitry Andric bool BPFTargetInfo::isValidCPUName(StringRef Name) const {
73349cc55cSDimitry Andric   return llvm::is_contained(ValidCPUNames, Name);
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
fillValidCPUList(SmallVectorImpl<StringRef> & Values) const760b57cec5SDimitry Andric void BPFTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
770b57cec5SDimitry Andric   Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
780b57cec5SDimitry Andric }
79a7dea167SDimitry Andric 
getTargetBuiltins() const80a7dea167SDimitry Andric ArrayRef<Builtin::Info> BPFTargetInfo::getTargetBuiltins() const {
81bdd1243dSDimitry Andric   return llvm::ArrayRef(BuiltinInfo,
82bdd1243dSDimitry Andric                         clang::BPF::LastTSBuiltin - Builtin::FirstTSBuiltin);
83a7dea167SDimitry Andric }
84fe6060f1SDimitry Andric 
handleTargetFeatures(std::vector<std::string> & Features,DiagnosticsEngine & Diags)85fe6060f1SDimitry Andric bool BPFTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
86fe6060f1SDimitry Andric                                          DiagnosticsEngine &Diags) {
87fe6060f1SDimitry Andric   for (const auto &Feature : Features) {
88fe6060f1SDimitry Andric     if (Feature == "+alu32") {
89fe6060f1SDimitry Andric       HasAlu32 = true;
90fe6060f1SDimitry Andric     }
91fe6060f1SDimitry Andric   }
92fe6060f1SDimitry Andric 
93fe6060f1SDimitry Andric   return true;
94fe6060f1SDimitry Andric }
95