1//===- BPF.cpp ------------------------------------------------------------===//
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#include "ABIInfoImpl.h"
10#include "TargetInfo.h"
11
12using namespace clang;
13using namespace clang::CodeGen;
14
15//===----------------------------------------------------------------------===//
16// BPF ABI Implementation
17//===----------------------------------------------------------------------===//
18
19namespace {
20
21class BPFABIInfo : public DefaultABIInfo {
22public:
23 BPFABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
24
25 // Classify an aggregate (struct/union) used as an argument or a return
26 // value. Aggregates that fit in 1 or 2 registers are passed/returned
27 // directly, coerced to an integer or a pair of 64-bit integers; larger
28 // ones use an indirect reference.
29 ABIArgInfo classifyAggregateType(QualType Ty) const {
30 uint64_t Bits = getContext().getTypeSize(T: Ty);
31 if (Bits == 0)
32 return ABIArgInfo::getIgnore();
33
34 // Larger aggregates use an indirect reference.
35 if (Bits > 128)
36 return getNaturalAlignIndirect(Ty, AddrSpace: getDataLayout().getAllocaAddrSpace());
37
38 // If the aggregate needs 1 or 2 registers, do not use reference.
39 llvm::Type *CoerceTy;
40 if (Bits <= 64) {
41 CoerceTy = llvm::IntegerType::get(C&: getVMContext(), NumBits: llvm::alignTo(Value: Bits, Align: 8));
42 } else {
43 llvm::Type *RegTy = llvm::IntegerType::get(C&: getVMContext(), NumBits: 64);
44 CoerceTy = llvm::ArrayType::get(ElementType: RegTy, NumElements: 2);
45 }
46 return ABIArgInfo::getDirect(T: CoerceTy);
47 }
48
49 ABIArgInfo classifyArgumentType(QualType Ty) const {
50 Ty = useFirstFieldIfTransparentUnion(Ty);
51
52 if (isAggregateTypeForABI(T: Ty))
53 return classifyAggregateType(Ty);
54
55 if (const auto *ED = Ty->getAsEnumDecl())
56 Ty = ED->getIntegerType();
57
58 ASTContext &Context = getContext();
59 if (const auto *EIT = Ty->getAs<BitIntType>())
60 if (EIT->getNumBits() > Context.getTypeSize(T: Context.Int128Ty))
61 return getNaturalAlignIndirect(Ty,
62 AddrSpace: getDataLayout().getAllocaAddrSpace());
63
64 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
65 : ABIArgInfo::getDirect());
66 }
67
68 ABIArgInfo classifyReturnType(QualType RetTy) const {
69 if (RetTy->isVoidType())
70 return ABIArgInfo::getIgnore();
71
72 if (isAggregateTypeForABI(T: RetTy))
73 return classifyAggregateType(Ty: RetTy);
74
75 // Treat an enum type as its underlying type.
76 if (const auto *ED = RetTy->getAsEnumDecl())
77 RetTy = ED->getIntegerType();
78
79 ASTContext &Context = getContext();
80 if (const auto *EIT = RetTy->getAs<BitIntType>())
81 if (EIT->getNumBits() > Context.getTypeSize(T: Context.Int128Ty))
82 return getNaturalAlignIndirect(Ty: RetTy,
83 AddrSpace: getDataLayout().getAllocaAddrSpace());
84
85 // Caller will do necessary sign/zero extension.
86 return ABIArgInfo::getDirect();
87 }
88
89 void computeInfo(CGFunctionInfo &FI) const override {
90 FI.getReturnInfo() = classifyReturnType(RetTy: FI.getReturnType());
91 for (auto &I : FI.arguments())
92 I.info = classifyArgumentType(Ty: I.type);
93 }
94
95};
96
97class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
98public:
99 BPFTargetCodeGenInfo(CodeGenTypes &CGT)
100 : TargetCodeGenInfo(std::make_unique<BPFABIInfo>(args&: CGT)) {}
101};
102
103}
104
105std::unique_ptr<TargetCodeGenInfo>
106CodeGen::createBPFTargetCodeGenInfo(CodeGenModule &CGM) {
107 return std::make_unique<BPFTargetCodeGenInfo>(args&: CGM.getTypes());
108}
109