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
|
//===-- runtime/pointer.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Runtime/pointer.h"
#include "derived.h"
#include "stat.h"
#include "terminator.h"
#include "tools.h"
#include "type-info.h"
namespace Fortran::runtime {
extern "C" {
void RTNAME(PointerNullifyIntrinsic)(Descriptor &pointer, TypeCategory category,
int kind, int rank, int corank) {
INTERNAL_CHECK(corank == 0);
pointer.Establish(TypeCode{category, kind},
Descriptor::BytesFor(category, kind), nullptr, rank, nullptr,
CFI_attribute_pointer);
}
void RTNAME(PointerNullifyCharacter)(Descriptor &pointer, SubscriptValue length,
int kind, int rank, int corank) {
INTERNAL_CHECK(corank == 0);
pointer.Establish(
kind, length, nullptr, rank, nullptr, CFI_attribute_pointer);
}
void RTNAME(PointerNullifyDerived)(Descriptor &pointer,
const typeInfo::DerivedType &derivedType, int rank, int corank) {
INTERNAL_CHECK(corank == 0);
pointer.Establish(derivedType, nullptr, rank, nullptr, CFI_attribute_pointer);
}
void RTNAME(PointerSetBounds)(Descriptor &pointer, int zeroBasedDim,
SubscriptValue lower, SubscriptValue upper) {
INTERNAL_CHECK(zeroBasedDim >= 0 && zeroBasedDim < pointer.rank());
pointer.GetDimension(zeroBasedDim).SetBounds(lower, upper);
// The byte strides are computed when the pointer is allocated.
}
// TODO: PointerSetCoBounds
void RTNAME(PointerSetDerivedLength)(
Descriptor &pointer, int which, SubscriptValue x) {
DescriptorAddendum *addendum{pointer.Addendum()};
INTERNAL_CHECK(addendum != nullptr);
addendum->SetLenParameterValue(which, x);
}
void RTNAME(PointerApplyMold)(Descriptor &pointer, const Descriptor &mold) {
pointer = mold;
pointer.set_base_addr(nullptr);
pointer.raw().attribute = CFI_attribute_pointer;
}
void RTNAME(PointerAssociateScalar)(Descriptor &pointer, void *target) {
pointer.set_base_addr(target);
}
void RTNAME(PointerAssociate)(Descriptor &pointer, const Descriptor &target) {
pointer = target;
pointer.raw().attribute = CFI_attribute_pointer;
}
void RTNAME(PointerAssociateLowerBounds)(Descriptor &pointer,
const Descriptor &target, const Descriptor &lowerBounds) {
pointer = target;
pointer.raw().attribute = CFI_attribute_pointer;
int rank{pointer.rank()};
Terminator terminator{__FILE__, __LINE__};
std::size_t boundElementBytes{lowerBounds.ElementBytes()};
for (int j{0}; j < rank; ++j) {
pointer.GetDimension(j).SetLowerBound(
GetInt64(lowerBounds.ZeroBasedIndexedElement<const char>(j),
boundElementBytes, terminator));
}
}
void RTNAME(PointerAssociateRemapping)(Descriptor &pointer,
const Descriptor &target, const Descriptor &bounds, const char *sourceFile,
int sourceLine) {
pointer = target;
pointer.raw().attribute = CFI_attribute_pointer;
int rank{pointer.rank()};
Terminator terminator{sourceFile, sourceLine};
SubscriptValue byteStride{/*captured from first dimension*/};
std::size_t boundElementBytes{bounds.ElementBytes()};
for (int j{0}; j < rank; ++j) {
auto &dim{pointer.GetDimension(j)};
dim.SetBounds(GetInt64(bounds.ZeroBasedIndexedElement<const char>(2 * j),
boundElementBytes, terminator),
GetInt64(bounds.ZeroBasedIndexedElement<const char>(2 * j + 1),
boundElementBytes, terminator));
if (j == 0) {
byteStride = dim.ByteStride();
} else {
dim.SetByteStride(byteStride);
byteStride *= dim.Extent();
}
}
if (pointer.Elements() > target.Elements()) {
terminator.Crash("PointerAssociateRemapping: too many elements in remapped "
"pointer (%zd > %zd)",
pointer.Elements(), target.Elements());
}
}
int RTNAME(PointerAllocate)(Descriptor &pointer, bool hasStat,
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
Terminator terminator{sourceFile, sourceLine};
if (!pointer.IsPointer()) {
return ReturnError(terminator, StatInvalidDescriptor, errMsg, hasStat);
}
int stat{ReturnError(terminator, pointer.Allocate(), errMsg, hasStat)};
if (stat == StatOk) {
if (const DescriptorAddendum * addendum{pointer.Addendum()}) {
if (const auto *derived{addendum->derivedType()}) {
if (!derived->noInitializationNeeded()) {
stat = Initialize(pointer, *derived, terminator, hasStat, errMsg);
}
}
}
}
return stat;
}
int RTNAME(PointerDeallocate)(Descriptor &pointer, bool hasStat,
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
Terminator terminator{sourceFile, sourceLine};
if (!pointer.IsPointer()) {
return ReturnError(terminator, StatInvalidDescriptor, errMsg, hasStat);
}
if (!pointer.IsAllocated()) {
return ReturnError(terminator, StatBaseNull, errMsg, hasStat);
}
return ReturnError(terminator, pointer.Destroy(true), errMsg, hasStat);
}
bool RTNAME(PointerIsAssociated)(const Descriptor &pointer) {
return pointer.raw().base_addr != nullptr;
}
bool RTNAME(PointerIsAssociatedWith)(
const Descriptor &pointer, const Descriptor &target) {
int rank{pointer.rank()};
if (pointer.raw().base_addr != target.raw().base_addr ||
pointer.ElementBytes() != target.ElementBytes() ||
rank != target.rank()) {
return false;
}
for (int j{0}; j < rank; ++j) {
const Dimension &pDim{pointer.GetDimension(j)};
const Dimension &tDim{target.GetDimension(j)};
if (pDim.Extent() != tDim.Extent() ||
pDim.ByteStride() != tDim.ByteStride()) {
return false;
}
}
return true;
}
// TODO: PointerCheckLengthParameter, PointerAllocateSource
} // extern "C"
} // namespace Fortran::runtime
|