FLANG
Character.h
1//===-- Character.h -- lowering of characters -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_OPTIMIZER_BUILDER_CHARACTER_H
14#define FORTRAN_OPTIMIZER_BUILDER_CHARACTER_H
15
16#include "flang/Optimizer/Builder/BoxValue.h"
17#include "flang/Optimizer/Builder/LowLevelIntrinsics.h"
18#include "flang/Optimizer/Builder/Runtime/Character.h"
19
20namespace fir {
21class FirOpBuilder;
22}
23
24namespace fir::factory {
25
28public:
30 explicit CharacterExprHelper(FirOpBuilder &builder, mlir::Location loc)
31 : builder{builder}, loc{loc} {}
33
36 void createCopy(const fir::CharBoxValue &dest, const fir::CharBoxValue &src,
37 mlir::Value count);
38
43 void createPadding(const fir::CharBoxValue &str, mlir::Value lower,
44 mlir::Value upper);
45
50
53 mlir::Value genSubstringBase(mlir::Value stringRawAddr,
54 mlir::Value lowerBound,
55 mlir::Type substringAddrType,
56 mlir::Value one = {});
57
59 mlir::Value createBlankConstant(fir::CharacterType type);
60
63 void createAssign(const fir::ExtendedValue &lhs,
64 const fir::ExtendedValue &rhs);
65
68 const fir::CharBoxValue &rhs);
69
73
75 mlir::Value createLenTrim(const fir::CharBoxValue &str);
76
80 mlir::Value createEmboxChar(mlir::Value addr, mlir::Value len);
83 mlir::Value createEmbox(const fir::CharBoxValue &str);
86 mlir::Value createEmbox(const fir::CharArrayBoxValue &str);
87
91
93 std::pair<mlir::Value, mlir::Value> createUnboxChar(mlir::Value boxChar);
94
97 fir::CharBoxValue createCharacterTemp(mlir::Type type, mlir::Value len);
98
101 fir::CharBoxValue createCharacterTemp(mlir::Type type, int len);
102
105
108 static bool isCharacterLiteral(mlir::Type type);
109
114 static bool isCharacterScalar(mlir::Type type);
115
118 static bool hasConstantLengthInType(const fir::ExtendedValue &);
119
121 static fir::KindTy getCharacterKind(mlir::Type type);
122
124 static fir::KindTy getCharacterOrSequenceKind(mlir::Type type);
125
126 // TODO: Do we really need all these flavors of unwrapping to get the fir.char
127 // type? Or can we merge these? It would be better to merge them and eliminate
128 // the confusion.
129
132 static fir::CharacterType getCharType(mlir::Type type);
133
135 static fir::CharacterType getCharacterType(mlir::Type type);
136 static fir::CharacterType getCharacterType(const fir::CharBoxValue &box);
137 static fir::CharacterType getCharacterType(mlir::Value str);
138
152 fir::ExtendedValue toExtendedValue(mlir::Value character,
153 mlir::Value len = {});
154
164 static bool isArray(mlir::Type type);
165
174
176 mlir::Value createSingletonFromCode(mlir::Value code, int kind);
178 mlir::Value extractCodeFromSingleton(mlir::Value singleton);
179
184 mlir::Value getLength(mlir::Value memref);
185
189 mlir::Value readLengthFromBox(mlir::Value box);
190
192 mlir::Value readLengthFromBox(mlir::Value box, fir::CharacterType charTy);
193
194private:
197 fir::CharBoxValue materializeValue(mlir::Value str);
198 mlir::Value getCharBoxBuffer(const fir::CharBoxValue &box);
199 mlir::Value createElementAddr(mlir::Value buffer, mlir::Value index);
200 mlir::Value createLoadCharAt(mlir::Value buff, mlir::Value index);
201 void createStoreCharAt(mlir::Value str, mlir::Value index, mlir::Value c);
202 void createLengthOneAssign(const fir::CharBoxValue &lhs,
203 const fir::CharBoxValue &rhs);
204 void createAssign(const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs);
205 mlir::Value createBlankConstantCode(fir::CharacterType type);
206
207private:
208 FirOpBuilder &builder;
209 mlir::Location loc;
210};
211
212//===----------------------------------------------------------------------===//
213// Tools to work with Character dummy procedures
214//===----------------------------------------------------------------------===//
215
219mlir::Type getCharacterProcedureTupleType(mlir::Type funcPointerType);
220
225mlir::Value createCharacterProcedureTuple(fir::FirOpBuilder &builder,
226 mlir::Location loc,
227 mlir::Type tupleType,
228 mlir::Value addr, mlir::Value len);
229
234std::pair<mlir::Value, mlir::Value>
235extractCharacterProcedureTuple(fir::FirOpBuilder &builder, mlir::Location loc,
236 mlir::Value tuple, bool openBoxProc = true);
237
238fir::CharBoxValue convertCharacterKind(fir::FirOpBuilder &builder,
239 mlir::Location loc,
240 fir::CharBoxValue srcBoxChar,
241 int toKind);
242
243} // namespace fir::factory
244
245#endif // FORTRAN_OPTIMIZER_BUILDER_CHARACTER_H
Expressions of type CHARACTER and with rank > 0.
Definition: BoxValue.h:170
Definition: BoxValue.h:77
Definition: BoxValue.h:478
Definition: FIRBuilder.h:55
Helper to facilitate lowering of CHARACTER in FIR.
Definition: Character.h:27
mlir::Value extractCodeFromSingleton(mlir::Value singleton)
Returns integer value held in a character singleton.
Definition: Character.cpp:695
fir::CharBoxValue createConcatenate(const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs)
Create lhs // rhs in temp obtained with fir.alloca.
Definition: Character.cpp:483
fir::CharBoxValue createCharacterTemp(mlir::Type type, mlir::Value len)
Definition: Character.cpp:366
fir::CharBoxValue createCharExtremum(bool predIsMin, llvm::ArrayRef< fir::CharBoxValue > opCBVs)
Create {max,min}(lhs,rhs) in temp obtained with fir.alloca.
Definition: Character.cpp:792
fir::ExtendedValue toExtendedValue(mlir::Value character, mlir::Value len={})
Definition: Character.cpp:122
fir::CharBoxValue createTempFrom(const fir::ExtendedValue &source)
Create a temporary with the same kind, length, and value as source.
Definition: Character.cpp:383
static fir::CharacterType getCharacterType(mlir::Type type)
Get fir.char<kind> type with the same kind as inside str.
Definition: Character.cpp:62
static fir::KindTy getCharacterOrSequenceKind(mlir::Type type)
Extract the kind of a character or array of character type.
Definition: Character.cpp:672
static bool isCharacterScalar(mlir::Type type)
Definition: Character.cpp:49
mlir::Value getLength(mlir::Value memref)
Definition: Character.cpp:725
mlir::Value createEmbox(const fir::CharBoxValue &str)
Definition: Character.cpp:198
static bool isArray(mlir::Type type)
Definition: Character.cpp:54
static bool hasConstantLengthInType(const fir::ExtendedValue &)
Definition: Character.cpp:676
void createPadding(const fir::CharBoxValue &str, mlir::Value lower, mlir::Value upper)
Definition: Character.cpp:353
fir::ExtendedValue cleanUpCharacterExtendedValue(const fir::ExtendedValue &)
fir::CharBoxValue toScalarCharacter(const fir::CharArrayBoxValue &)
Definition: Character.cpp:223
static fir::CharacterType getCharType(mlir::Type type)
Definition: Character.cpp:68
mlir::Value createLenTrim(const fir::CharBoxValue &str)
LEN_TRIM intrinsic.
Definition: Character.cpp:562
mlir::Value createSingletonFromCode(mlir::Value code, int kind)
Create fir.char<kind> singleton from code integer value.
Definition: Character.cpp:683
mlir::Value createBlankConstant(fir::CharacterType type)
Return blank character of given type !fir.char<kind>
Definition: Character.cpp:620
mlir::Value createEmboxChar(mlir::Value addr, mlir::Value len)
Definition: Character.cpp:640
mlir::Value readLengthFromBox(mlir::Value box)
Definition: Character.cpp:707
void createCopy(const fir::CharBoxValue &dest, const fir::CharBoxValue &src, mlir::Value count)
Definition: Character.cpp:320
static fir::KindTy getCharacterKind(mlir::Type type)
Extract the kind of a character type.
Definition: Character.cpp:666
std::pair< mlir::Value, mlir::Value > createUnboxChar(mlir::Value boxChar)
Unbox boxchar into (fir.ref<fir.char<kind>>, character length type).
Definition: Character.cpp:646
void createAssign(const fir::ExtendedValue &lhs, const fir::ExtendedValue &rhs)
Definition: Character.cpp:626
static bool isCharacterLiteral(mlir::Type type)
Definition: Character.cpp:658
mlir::Value genSubstringBase(mlir::Value stringRawAddr, mlir::Value lowerBound, mlir::Type substringAddrType, mlir::Value one={})
Definition: Character.cpp:507
CharacterExprHelper(FirOpBuilder &builder, mlir::Location loc)
Constructor.
Definition: Character.h:30
fir::CharBoxValue createSubstring(const fir::CharBoxValue &str, llvm::ArrayRef< mlir::Value > bounds)
Definition: Character.cpp:518
Definition: FIRType.h:77
Definition: AbstractConverter.h:31