1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
//===--- ScalarPairTypeInfo.h - Type info for scalar pairs ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines ScalarPairTypeInfo, which is a convenient abstract
// implementation of TypeInfo for working with types that are
// efficiently scalarizable.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_SCALARPAIRTYPEINFO_H
#define SWIFT_IRGEN_SCALARPAIRTYPEINFO_H
#include "NativeConventionSchema.h"
#include "ScalarTypeInfo.h"
namespace swift {
namespace irgen {
template <class Derived, class Base>
class ScalarPairTypeInfo : public ScalarTypeInfo<Derived, Base> {
using super = ScalarTypeInfo<Derived, Base>;
protected:
template <class... T> ScalarPairTypeInfo(T &&...args)
: super(::std::forward<T>(args)...) {}
const Derived &asDerived() const {
return static_cast<const Derived &>(*this);
}
public:
llvm::StructType *getStorageType() const {
return cast<llvm::StructType>(TypeInfo::getStorageType());
}
Address projectFirstElement(IRGenFunction &IGF, Address address) const {
return IGF.Builder.CreateStructGEP(address, 0, Size(0),
address->getName()
+ asDerived().getFirstElementLabel());
}
Address projectSecondElement(IRGenFunction &IGF, Address address) const {
return IGF.Builder.CreateStructGEP(address, 1,
asDerived().getSecondElementOffset(IGF.IGM),
address->getName() + asDerived().getSecondElementLabel());
}
unsigned getExplosionSize() const override {
return 2;
}
void getSchema(ExplosionSchema &schema) const override {
llvm::StructType *structTy = getStorageType();
schema.add(ExplosionSchema::Element::forScalar(structTy->getElementType(0)));
schema.add(ExplosionSchema::Element::forScalar(structTy->getElementType(1)));
}
void addToAggLowering(IRGenModule &IGM, SwiftAggLowering &lowering,
Size offset) const override {
llvm::StructType *structTy = getStorageType();
this->addScalarToAggLowering(IGM, lowering, structTy->getElementType(0),
offset, asDerived().getFirstElementSize(IGM));
this->addScalarToAggLowering(IGM, lowering, structTy->getElementType(1),
offset + asDerived().getSecondElementOffset(IGM),
asDerived().getSecondElementSize(IGM));
}
void loadAsCopy(IRGenFunction &IGF, Address address,
Explosion &e) const override {
Address firstAddr = projectFirstElement(IGF, address);
auto first =
IGF.Builder.CreateLoad(firstAddr, firstAddr->getName() + ".load");
asDerived().emitRetainFirstElement(IGF, first);
e.add(first);
Address secondAddr = projectSecondElement(IGF, address);
auto second = IGF.Builder.CreateLoad(secondAddr);
asDerived().emitRetainSecondElement(IGF, second);
e.add(second);
}
void loadAsTake(IRGenFunction &IGF, Address addr,
Explosion &e) const override {
// Load the function.
Address firstAddr = projectFirstElement(IGF, addr);
e.add(IGF.Builder.CreateLoad(firstAddr));
Address secondAddr = projectSecondElement(IGF, addr);
e.add(IGF.Builder.CreateLoad(secondAddr));
}
void assign(IRGenFunction &IGF, Explosion &e, Address address,
bool isOutlined, SILType T) const override {
// Store the function pointer.
Address firstAddr = projectFirstElement(IGF, address);
asDerived().emitAssignFirstElement(IGF, e.claimNext(), firstAddr);
Address secondAddr = projectSecondElement(IGF, address);
asDerived().emitAssignSecondElement(IGF, e.claimNext(), secondAddr);
}
void initialize(IRGenFunction &IGF, Explosion &e, Address address,
bool isOutlined) const override {
Address firstAddr = projectFirstElement(IGF, address);
IGF.Builder.CreateStore(e.claimNext(), firstAddr);
Address secondAddr = projectSecondElement(IGF, address);
IGF.Builder.CreateStore(e.claimNext(), secondAddr);
}
void copy(IRGenFunction &IGF, Explosion &src,
Explosion &dest, Atomicity atomicity) const override {
auto first = src.claimNext();
asDerived().emitRetainFirstElement(IGF, first, atomicity);
dest.add(first);
auto second = src.claimNext();
asDerived().emitRetainSecondElement(IGF, second, atomicity);
dest.add(second);
}
void consume(IRGenFunction &IGF, Explosion &src,
Atomicity atomicity, SILType T) const override {
auto first = src.claimNext();
asDerived().emitReleaseFirstElement(IGF, first, atomicity);
auto second = src.claimNext();
asDerived().emitReleaseSecondElement(IGF, second, atomicity);
}
void fixLifetime(IRGenFunction &IGF, Explosion &src) const override {
auto first = src.claimNext();
if (!asDerived().isFirstElementTrivial())
IGF.emitFixLifetime(first);
auto second = src.claimNext();
if (!asDerived().isSecondElementTrivial())
IGF.emitFixLifetime(second);
}
void destroy(IRGenFunction &IGF, Address addr, SILType T,
bool isOutlined) const override {
if (!asDerived().isFirstElementTrivial()) {
auto first = IGF.Builder.CreateLoad(projectFirstElement(IGF, addr));
asDerived().emitReleaseFirstElement(IGF, first);
}
if (!asDerived().isSecondElementTrivial()) {
auto first = IGF.Builder.CreateLoad(projectSecondElement(IGF, addr));
asDerived().emitReleaseSecondElement(IGF, first);
}
}
void packIntoEnumPayload(IRGenModule &IGM,
IRBuilder &builder,
EnumPayload &payload,
Explosion &src,
unsigned offset) const override {
payload.insertValue(IGM, builder, src.claimNext(), offset);
payload.insertValue(IGM, builder, src.claimNext(),
offset + asDerived().getSecondElementOffset(IGM).getValueInBits());
}
void unpackFromEnumPayload(IRGenFunction &IGF,
const EnumPayload &payload,
Explosion &dest,
unsigned offset) const override {
auto storageTy = getStorageType();
dest.add(payload.extractValue(IGF, storageTy->getElementType(0), offset));
dest.add(payload.extractValue(IGF, storageTy->getElementType(1),
offset + asDerived().getSecondElementOffset(IGF.IGM).getValueInBits()));
}
};
template <class Derived, class Base>
class TrivialScalarPairTypeInfo : public ScalarPairTypeInfo<Derived, Base> {
using super = ScalarPairTypeInfo<Derived, Base>;
protected:
template <class... T> TrivialScalarPairTypeInfo(T &&...args)
: super(::std::forward<T>(args)...) {}
const Derived &asDerived() const {
return static_cast<const Derived &>(*this);
}
public:
static bool isFirstElementTrivial() {
return true;
}
void emitRetainFirstElement(
IRGenFunction &IGF, llvm::Value *value,
std::optional<Atomicity> atomicity = std::nullopt) const {}
void emitReleaseFirstElement(
IRGenFunction &IGF, llvm::Value *value,
std::optional<Atomicity> atomicity = std::nullopt) const {}
void emitAssignFirstElement(IRGenFunction &IGF, llvm::Value *value,
Address valueAddr) const {
IGF.Builder.CreateStore(value, valueAddr);
}
static bool isSecondElementTrivial() {
return true;
}
void emitRetainSecondElement(
IRGenFunction &IGF, llvm::Value *value,
std::optional<Atomicity> atomicity = std::nullopt) const {}
void emitReleaseSecondElement(
IRGenFunction &IGF, llvm::Value *value,
std::optional<Atomicity> atomicity = std::nullopt) const {}
void emitAssignSecondElement(IRGenFunction &IGF, llvm::Value *value,
Address valueAddr) const {
IGF.Builder.CreateStore(value, valueAddr);
}
};
}
}
#endif
|