InternalNames.cpp
8.64 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//===-- InternalNames.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/Optimizer/Support/InternalNames.h"
#include "mlir/IR/Diagnostics.h"
#include "llvm/Support/CommandLine.h"
static llvm::cl::opt<std::string> mainEntryName(
"main-entry-name",
llvm::cl::desc("override the name of the default PROGRAM entry (may be "
"helpful for using other runtimes)"));
constexpr std::int64_t BAD_VALUE = -1;
inline std::string prefix() { return "_Q"; }
static std::string doModules(llvm::ArrayRef<llvm::StringRef> mods) {
std::string result;
auto *token = "M";
for (auto mod : mods) {
result.append(token).append(mod.lower());
token = "S";
}
return result;
}
static std::string doModulesHost(llvm::ArrayRef<llvm::StringRef> mods,
llvm::Optional<llvm::StringRef> host) {
std::string result = doModules(mods);
if (host.hasValue())
result.append("F").append(host->lower());
return result;
}
inline llvm::SmallVector<llvm::StringRef, 2>
convertToStringRef(llvm::ArrayRef<std::string> from) {
return {from.begin(), from.end()};
}
inline llvm::Optional<llvm::StringRef>
convertToStringRef(const llvm::Optional<std::string> &from) {
llvm::Optional<llvm::StringRef> to;
if (from.hasValue())
to = from.getValue();
return to;
}
static std::string readName(llvm::StringRef uniq, std::size_t &i,
std::size_t init, std::size_t end) {
for (i = init; i < end && (uniq[i] < 'A' || uniq[i] > 'Z'); ++i) {
// do nothing
}
return uniq.substr(init, i - init).str();
}
static std::int64_t readInt(llvm::StringRef uniq, std::size_t &i,
std::size_t init, std::size_t end) {
for (i = init; i < end && uniq[i] >= '0' && uniq[i] <= '9'; ++i) {
// do nothing
}
std::int64_t result = BAD_VALUE;
if (uniq.substr(init, i - init).getAsInteger(10, result))
return BAD_VALUE;
return result;
}
std::string fir::NameUniquer::toLower(llvm::StringRef name) {
return name.lower();
}
std::string fir::NameUniquer::intAsString(std::int64_t i) {
assert(i >= 0);
return std::to_string(i);
}
std::string fir::NameUniquer::doKind(std::int64_t kind) {
std::string result = "K";
if (kind < 0)
return result.append("N").append(intAsString(-kind));
return result.append(intAsString(kind));
}
std::string fir::NameUniquer::doKinds(llvm::ArrayRef<std::int64_t> kinds) {
std::string result;
for (auto i : kinds)
result.append(doKind(i));
return result;
}
std::string fir::NameUniquer::doCommonBlock(llvm::StringRef name) {
std::string result = prefix();
return result.append("B").append(toLower(name));
}
std::string
fir::NameUniquer::doConstant(llvm::ArrayRef<llvm::StringRef> modules,
llvm::Optional<llvm::StringRef> host,
llvm::StringRef name) {
std::string result = prefix();
result.append(doModulesHost(modules, host)).append("EC");
return result.append(toLower(name));
}
std::string
fir::NameUniquer::doDispatchTable(llvm::ArrayRef<llvm::StringRef> modules,
llvm::Optional<llvm::StringRef> host,
llvm::StringRef name,
llvm::ArrayRef<std::int64_t> kinds) {
std::string result = prefix();
result.append(doModulesHost(modules, host)).append("DT");
return result.append(toLower(name)).append(doKinds(kinds));
}
std::string fir::NameUniquer::doGenerated(llvm::StringRef name) {
std::string result = prefix();
return result.append("Q").append(name);
}
std::string fir::NameUniquer::doIntrinsicTypeDescriptor(
llvm::ArrayRef<llvm::StringRef> modules,
llvm::Optional<llvm::StringRef> host, IntrinsicType type,
std::int64_t kind) {
const char *name = nullptr;
switch (type) {
case IntrinsicType::CHARACTER:
name = "character";
break;
case IntrinsicType::COMPLEX:
name = "complex";
break;
case IntrinsicType::INTEGER:
name = "integer";
break;
case IntrinsicType::LOGICAL:
name = "logical";
break;
case IntrinsicType::REAL:
name = "real";
break;
}
assert(name && "unknown intrinsic type");
std::string result = prefix();
result.append(doModulesHost(modules, host)).append("C");
return result.append(name).append(doKind(kind));
}
std::string
fir::NameUniquer::doProcedure(llvm::ArrayRef<llvm::StringRef> modules,
llvm::Optional<llvm::StringRef> host,
llvm::StringRef name) {
std::string result = prefix();
result.append(doModulesHost(modules, host)).append("P");
return result.append(toLower(name));
}
std::string fir::NameUniquer::doType(llvm::ArrayRef<llvm::StringRef> modules,
llvm::Optional<llvm::StringRef> host,
llvm::StringRef name,
llvm::ArrayRef<std::int64_t> kinds) {
std::string result = prefix();
result.append(doModulesHost(modules, host)).append("T");
return result.append(toLower(name)).append(doKinds(kinds));
}
std::string
fir::NameUniquer::doTypeDescriptor(llvm::ArrayRef<llvm::StringRef> modules,
llvm::Optional<llvm::StringRef> host,
llvm::StringRef name,
llvm::ArrayRef<std::int64_t> kinds) {
std::string result = prefix();
result.append(doModulesHost(modules, host)).append("CT");
return result.append(toLower(name)).append(doKinds(kinds));
}
std::string fir::NameUniquer::doTypeDescriptor(
llvm::ArrayRef<std::string> modules, llvm::Optional<std::string> host,
llvm::StringRef name, llvm::ArrayRef<std::int64_t> kinds) {
auto rmodules = convertToStringRef(modules);
auto rhost = convertToStringRef(host);
return doTypeDescriptor(rmodules, rhost, name, kinds);
}
std::string
fir::NameUniquer::doVariable(llvm::ArrayRef<llvm::StringRef> modules,
llvm::Optional<llvm::StringRef> host,
llvm::StringRef name) {
std::string result = prefix();
result.append(doModulesHost(modules, host)).append("E");
return result.append(toLower(name));
}
llvm::StringRef fir::NameUniquer::doProgramEntry() {
if (mainEntryName.size())
return mainEntryName;
return "_QQmain";
}
std::pair<fir::NameUniquer::NameKind, fir::NameUniquer::DeconstructedName>
fir::NameUniquer::deconstruct(llvm::StringRef uniq) {
if (uniq.startswith("_Q")) {
llvm::SmallVector<std::string, 4> modules;
llvm::Optional<std::string> host;
std::string name;
llvm::SmallVector<std::int64_t, 8> kinds;
NameKind nk = NameKind::NOT_UNIQUED;
for (std::size_t i = 2, end{uniq.size()}; i != end;) {
switch (uniq[i]) {
case 'B':
nk = NameKind::COMMON;
name = readName(uniq, i, i + 1, end);
break;
case 'C':
if (uniq[i + 1] == 'T') {
nk = NameKind::TYPE_DESC;
name = readName(uniq, i, i + 2, end);
} else {
nk = NameKind::INTRINSIC_TYPE_DESC;
name = readName(uniq, i, i + 1, end);
}
break;
case 'D':
nk = NameKind::DISPATCH_TABLE;
assert(uniq[i + 1] == 'T');
name = readName(uniq, i, i + 2, end);
break;
case 'E':
if (uniq[i + 1] == 'C') {
nk = NameKind::CONSTANT;
name = readName(uniq, i, i + 2, end);
} else {
nk = NameKind::VARIABLE;
name = readName(uniq, i, i + 1, end);
}
break;
case 'P':
nk = NameKind::PROCEDURE;
name = readName(uniq, i, i + 1, end);
break;
case 'Q':
nk = NameKind::GENERATED;
name = uniq;
i = end;
break;
case 'T':
nk = NameKind::DERIVED_TYPE;
name = readName(uniq, i, i + 1, end);
break;
case 'M':
case 'S':
modules.push_back(readName(uniq, i, i + 1, end));
break;
case 'F':
host = readName(uniq, i, i + 1, end);
break;
case 'K':
if (uniq[i + 1] == 'N')
kinds.push_back(-readInt(uniq, i, i + 2, end));
else
kinds.push_back(readInt(uniq, i, i + 1, end));
break;
default:
assert(false && "unknown uniquing code");
break;
}
}
return {nk, DeconstructedName(modules, host, name, kinds)};
}
return {NameKind::NOT_UNIQUED, DeconstructedName(uniq)};
}