summaryrefslogtreecommitdiff
path: root/lib/CodeGen/CGValue.h
blob: f995ecb89e7cb2b85f8925d0df8cc96eea57170f (plain)
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
//===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// These classes implement wrappers around llvm::Value in order to
// fully represent the range of values for Complex, Character, Array and L/Rvalues.
//
//===----------------------------------------------------------------------===//

#ifndef FLANG_CODEGEN_CGVALUE_H
#define FLANG_CODEGEN_CGVALUE_H

#include "flang/AST/ASTContext.h"
#include "flang/AST/Type.h"
#include "llvm/IR/Value.h"

namespace llvm {
  class Constant;
  class MDNode;
}

namespace flang {
namespace CodeGen {

class ComplexValueTy {
public:
  llvm::Value *Re, *Im;

  ComplexValueTy() {}
  ComplexValueTy(llvm::Value *Real, llvm::Value *Imaginary)
    : Re(Real), Im(Imaginary) {}
};

class CharacterValueTy {
public:
  llvm::Value *Ptr, *Len;

  CharacterValueTy() {}
  CharacterValueTy(llvm::Value *Pointer, llvm::Value *Length)
    : Ptr(Pointer), Len(Length) {}
};

/// ArrayDimensionValueTy - this is a single dimension
/// of an array. All values have SizeTy type.
class ArrayDimensionValueTy {
public:
  llvm::Value *LowerBound;
  llvm::Value *UpperBound;
  llvm::Value *Stride;

  ArrayDimensionValueTy() {}
  ArrayDimensionValueTy(llvm::Value *LB, llvm::Value *UB = nullptr,
                        llvm::Value *stride = nullptr)
    : LowerBound(LB), UpperBound(UB), Stride(stride) {}

  bool hasLowerBound() const {
    return LowerBound != nullptr;
  }
  bool hasUpperBound() const {
    return UpperBound != nullptr;
  }
  bool hasStride() const {
    return Stride != nullptr;
  }
};

/// ArrayValueRef - this a reference to an array
/// value.
class ArrayValueRef {
public:
  ArrayRef<ArrayDimensionValueTy> Dimensions;
  llvm::Value *Ptr;
  llvm::Value *Offset;

  ArrayValueRef(ArrayRef<ArrayDimensionValueTy> Dims,
               llvm::Value *P,
               llvm::Value *offset = nullptr)
    : Dimensions(Dims), Ptr(P),
      Offset(offset) {}

  bool hasOffset() const {
    return Offset != nullptr;
  }
};

/// ArrayVectorValueTy - this is a one dimensional
/// array value.
class ArrayVectorValueTy {
public:
  ArrayDimensionValueTy Dimension;
  llvm::Value *Ptr;

  ArrayVectorValueTy(ArrayDimensionValueTy Dim,
                     llvm::Value *P)
    : Dimension(Dim), Ptr(P) {}
};

class LValueTy {
public:
  llvm::Value *Ptr;
  QualType Type;

  LValueTy() {}
  LValueTy(llvm::Value *Dest)
    : Ptr(Dest) {}
  LValueTy(llvm::Value *Dest, QualType Ty)
    : Ptr(Dest), Type(Ty) {}

  llvm::Value *getPointer() const {
    return Ptr;
  }
  QualType getType() const {
    return Type;
  }

  bool isVolatileQualifier() const {
    return false;// NB: to be used in the future
  }
};

class RValueTy {
public:
  enum Kind {
    None,
    Scalar,
    Complex,
    Character,
    Aggregate
  };
private:
  Kind ValueType;
  llvm::Value *V1;
  llvm::Value *V2;

  RValueTy(llvm::Value *V, Kind Type)
    : V1(V), ValueType(Type) {}
public:

  RValueTy() : ValueType(None) {}
  RValueTy(llvm::Value *V)
    : V1(V), ValueType(Scalar) {}
  RValueTy(ComplexValueTy C)
    : V1(C.Re), V2(C.Im), ValueType(Complex) {}
  RValueTy(CharacterValueTy CharValue)
    : V1(CharValue.Ptr), V2(CharValue.Len), ValueType(Character) {}

  Kind getType() const {
    return ValueType;
  }
  bool isScalar() const {
    return getType() == Scalar;
  }
  bool isComplex() const {
    return getType() == Complex;
  }
  bool isCharacter() const {
    return getType() == Character;
  }
  bool isAggregate() const {
    return getType() == Aggregate;
  }
  bool isNothing() const {
    return getType() == None;
  }

  bool isVolatileQualifier() const {
    return false;// NB: to be used in the future
  }

  llvm::Value *asScalar() const {
    return V1;
  }
  ComplexValueTy asComplex() const {
    return ComplexValueTy(V1, V2);
  }
  CharacterValueTy asCharacter() const {
    return CharacterValueTy(V1, V2);
  }
  llvm::Value *getAggregateAddr() const {
    assert(isAggregate());
    return V1;
  }

  static RValueTy getAggregate(llvm::Value *Ptr, bool isVolatile = false) {
    return RValueTy(Ptr, Aggregate);
  }
};

}  // end namespace CodeGen
}  // end namespace flang

#endif