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
|
//===-- gen/dvalue.h - D value abstractions ---------------------*- C++ -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// These classes are used for generating the IR. They encapsulate an LLVM value
// together with a D type and provide an uniform interface for the most common
// operations. When more specialize handling is necessary, they hold enough
// information to do-the-right-thing (TM).
//
//===----------------------------------------------------------------------===//
#pragma once
class Type;
class Dsymbol;
class VarDeclaration;
class FuncDeclaration;
namespace llvm {
class Value;
class Type;
class Constant;
}
class DValue;
class DRValue;
class DImValue;
class DConstValue;
class DNullValue;
class DLValue;
class DSpecialRefValue;
class DSliceValue;
class DFuncValue;
/// Represents an immutable pair of LLVM value and associated D type.
class DValue {
public:
Type *const type;
virtual ~DValue() = default;
/// Returns true iff the value can be accessed at the end of the entry basic
/// block of the current function, in the sense that it is either not derived
/// from an llvm::Instruction (but from a global, constant, etc.) or that
/// instruction is part of the entry basic block.
///
/// In other words, whatever the value might be derived from then certainly
/// dominates uses in all other basic blocks of the function.
virtual bool definedInFuncEntryBB();
virtual DRValue *getRVal() { return nullptr; }
virtual DLValue *isLVal() { return nullptr; }
virtual DSpecialRefValue *isSpecialRef() { return nullptr; }
virtual DRValue *isRVal() { return nullptr; }
virtual DImValue *isIm() { return nullptr; }
virtual DConstValue *isConst() { return nullptr; }
virtual DNullValue *isNull() { return nullptr; }
virtual DSliceValue *isSlice() { return nullptr; }
virtual DFuncValue *isFunc() { return nullptr; }
protected:
llvm::Value *const val;
DValue(Type *t, llvm::Value *v);
friend llvm::Value *DtoRVal(DValue *v);
};
/// Represents a D rvalue via a low-level rvalue.
class DRValue : public DValue {
public:
DRValue *getRVal() override { return this; }
DRValue *isRVal() override { return this; }
protected:
DRValue(Type *t, llvm::Value *v);
};
/// Represents an immediate D value (simple rvalue with no special properties
/// like being a compile-time constant) via a low-level rvalue.
/// Restricted to primitive types such as pointers (incl. class references),
/// integral and floating-point types.
class DImValue : public DRValue {
public:
DImValue(Type *t, llvm::Value *v);
DImValue *isIm() override { return this; }
};
/// Represents a D compile-time constant via a low-level constant.
class DConstValue : public DRValue {
public:
DConstValue(Type *t, llvm::Constant *con);
bool definedInFuncEntryBB() override { return true; }
DConstValue *isConst() override { return this; }
};
/// Represents a D compile-time null constant.
class DNullValue : public DConstValue {
public:
DNullValue(Type *t, llvm::Constant *con) : DConstValue(t, con) {}
DNullValue *isNull() override { return this; }
};
/// Represents a D slice (dynamic array).
class DSliceValue : public DRValue {
llvm::Value *const length = nullptr;
llvm::Value *const ptr = nullptr;
DSliceValue(Type *t, llvm::Value *pair, llvm::Value *length,
llvm::Value *ptr);
public:
DSliceValue(Type *t, llvm::Value *pair);
DSliceValue(Type *t, llvm::Value *length, llvm::Value *ptr);
DSliceValue *isSlice() override { return this; }
llvm::Value *getLength();
llvm::Value *getPtr();
};
/// Represents a D function value with optional this/context pointer.
class DFuncValue : public DRValue {
public:
FuncDeclaration *func;
llvm::Value *vthis;
DFuncValue(Type *t, FuncDeclaration *fd, llvm::Value *v,
llvm::Value *vt = nullptr);
DFuncValue(FuncDeclaration *fd, llvm::Value *v, llvm::Value *vt = nullptr);
bool definedInFuncEntryBB() override;
DFuncValue *isFunc() override { return this; }
};
/// Represents a D value in memory via a low-level lvalue (pointer).
/// This doesn't imply that the D value is an lvalue too - e.g., we always
/// keep structs and static arrays in memory.
class DLValue : public DValue {
public:
DLValue(Type *t, llvm::Value *v);
DRValue *getRVal() override;
virtual DLValue *getLVal() { return this; }
DLValue *isLVal() override { return this; }
protected:
DLValue(llvm::Value *v, Type *t) : DValue(t, v) {}
friend llvm::Value *DtoLVal(DValue *v);
};
/// Represents special internal ref variables.
class DSpecialRefValue : public DLValue {
public:
DSpecialRefValue(Type *t, llvm::Value *v);
DRValue *getRVal() override;
DLValue *getLVal() override;
llvm::Value *getRefStorage() { return val; }
DSpecialRefValue *isSpecialRef() override { return this; }
};
inline llvm::Value *DtoRVal(DValue *v) { return v->getRVal()->val; }
llvm::Value *DtoLVal(DValue *v);
|