summaryrefslogtreecommitdiff
path: root/flang/runtime/descriptor.cpp
blob: c8895dd6d24686d31e85368222db1b7e7e3876ba (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
//===-- runtime/descriptor.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 "descriptor.h"
#include "flang/common/idioms.h"
#include <cassert>
#include <cstdlib>

namespace Fortran::runtime {

Descriptor::~Descriptor() {
  if (raw_.attribute != CFI_attribute_pointer) {
    Deallocate();
  }
}

void Descriptor::Establish(TypeCode t, std::size_t elementBytes, void *p,
    int rank, const SubscriptValue *extent, ISO::CFI_attribute_t attribute,
    bool addendum) {
  CHECK(ISO::CFI_establish(&raw_, p, attribute, t.raw(), elementBytes, rank,
            extent) == CFI_SUCCESS);
  raw_.f18Addendum = addendum;
  DescriptorAddendum *a{Addendum()};
  CHECK(addendum == (a != nullptr));
  if (a) {
    new (a) DescriptorAddendum{};
  }
}

void Descriptor::Establish(TypeCategory c, int kind, void *p, int rank,
    const SubscriptValue *extent, ISO::CFI_attribute_t attribute,
    bool addendum) {
  std::size_t elementBytes = kind;
  if (c == TypeCategory::Complex) {
    elementBytes *= 2;
  }
  CHECK(ISO::CFI_establish(&raw_, p, attribute, TypeCode(c, kind).raw(),
            elementBytes, rank, extent) == CFI_SUCCESS);
  raw_.f18Addendum = addendum;
  DescriptorAddendum *a{Addendum()};
  CHECK(addendum == (a != nullptr));
  if (a) {
    new (a) DescriptorAddendum{};
  }
}

void Descriptor::Establish(const DerivedType &dt, void *p, int rank,
    const SubscriptValue *extent, ISO::CFI_attribute_t attribute) {
  CHECK(ISO::CFI_establish(&raw_, p, attribute, CFI_type_struct,
            dt.SizeInBytes(), rank, extent) == CFI_SUCCESS);
  raw_.f18Addendum = true;
  DescriptorAddendum *a{Addendum()};
  CHECK(a);
  new (a) DescriptorAddendum{&dt};
}

std::unique_ptr<Descriptor> Descriptor::Create(TypeCode t,
    std::size_t elementBytes, void *p, int rank, const SubscriptValue *extent,
    ISO::CFI_attribute_t attribute) {
  std::size_t bytes{SizeInBytes(rank, true)};
  Descriptor *result{reinterpret_cast<Descriptor *>(new char[bytes])};
  CHECK(result);
  result->Establish(t, elementBytes, p, rank, extent, attribute, true);
  return std::unique_ptr<Descriptor>{result};
}

std::unique_ptr<Descriptor> Descriptor::Create(TypeCategory c, int kind,
    void *p, int rank, const SubscriptValue *extent,
    ISO::CFI_attribute_t attribute) {
  std::size_t bytes{SizeInBytes(rank, true)};
  Descriptor *result{reinterpret_cast<Descriptor *>(new char[bytes])};
  CHECK(result);
  result->Establish(c, kind, p, rank, extent, attribute, true);
  return std::unique_ptr<Descriptor>{result};
}

std::unique_ptr<Descriptor> Descriptor::Create(const DerivedType &dt, void *p,
    int rank, const SubscriptValue *extent, ISO::CFI_attribute_t attribute) {
  std::size_t bytes{SizeInBytes(rank, true, dt.lenParameters())};
  Descriptor *result{reinterpret_cast<Descriptor *>(new char[bytes])};
  CHECK(result);
  result->Establish(dt, p, rank, extent, attribute);
  return std::unique_ptr<Descriptor>{result};
}

std::size_t Descriptor::SizeInBytes() const {
  const DescriptorAddendum *addendum{Addendum()};
  return sizeof *this - sizeof(Dimension) + raw_.rank * sizeof(Dimension) +
      (addendum ? addendum->SizeInBytes() : 0);
}

std::size_t Descriptor::Elements() const {
  int n{rank()};
  std::size_t elements{1};
  for (int j{0}; j < n; ++j) {
    elements *= GetDimension(j).Extent();
  }
  return elements;
}

int Descriptor::Allocate(
    const SubscriptValue lb[], const SubscriptValue ub[], std::size_t charLen) {
  int result{ISO::CFI_allocate(&raw_, lb, ub, charLen)};
  if (result == CFI_SUCCESS) {
    // TODO: derived type initialization
  }
  return result;
}

int Descriptor::Deallocate(bool finalize) {
  if (raw_.base_addr) {
    Destroy(static_cast<char *>(raw_.base_addr), finalize);
  }
  return ISO::CFI_deallocate(&raw_);
}

void Descriptor::Destroy(char *data, bool finalize) const {
  if (data) {
    if (const DescriptorAddendum * addendum{Addendum()}) {
      if (addendum->flags() & DescriptorAddendum::DoNotFinalize) {
        finalize = false;
      }
      if (const DerivedType * dt{addendum->derivedType()}) {
        std::size_t elements{Elements()};
        std::size_t elementBytes{ElementBytes()};
        for (std::size_t j{0}; j < elements; ++j) {
          dt->Destroy(data + j * elementBytes, finalize);
        }
      }
    }
  }
}

void Descriptor::Check() const {
  // TODO
}

std::ostream &Descriptor::Dump(std::ostream &o) const {
  o << "Descriptor @ 0x" << std::hex << reinterpret_cast<std::intptr_t>(this)
    << std::dec << ":\n";
  o << "  base_addr 0x" << std::hex
    << reinterpret_cast<std::intptr_t>(raw_.base_addr) << std::dec << '\n';
  o << "  elem_len  " << raw_.elem_len << '\n';
  o << "  version   " << raw_.version
    << (raw_.version == CFI_VERSION ? "(ok)" : "BAD!") << '\n';
  o << "  rank      " << static_cast<int>(raw_.rank) << '\n';
  o << "  type      " << static_cast<int>(raw_.type) << '\n';
  o << "  attribute " << static_cast<int>(raw_.attribute) << '\n';
  o << "  addendum? " << static_cast<bool>(raw_.f18Addendum) << '\n';
  for (int j{0}; j < raw_.rank; ++j) {
    o << "  dim[" << j << "] lower_bound " << raw_.dim[j].lower_bound << '\n';
    o << "         extent      " << raw_.dim[j].extent << '\n';
    o << "         sm          " << raw_.dim[j].sm << '\n';
  }
  if (const DescriptorAddendum * addendum{Addendum()}) {
    addendum->Dump(o);
  }
  return o;
}

std::size_t DescriptorAddendum::SizeInBytes() const {
  return SizeInBytes(LenParameters());
}

std::ostream &DescriptorAddendum::Dump(std::ostream &o) const {
  o << "  derivedType @ 0x" << std::hex
    << reinterpret_cast<std::intptr_t>(derivedType_) << std::dec << '\n';
  o << "  flags         " << flags_ << '\n';
  // TODO: LEN parameter values
  return o;
}
}