| 1 |
//===-- Command.cpp -- generate command line runtime API calls ------------===// |
| 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 |
#include "flang/Optimizer/Builder/Runtime/Command.h" |
| 10 |
#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 11 |
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h" |
| 12 |
#include "flang/Runtime/command.h" |
| 13 |
|
| 14 |
using namespace Fortran::runtime; |
| 15 |
|
| 16 |
// Certain runtime intrinsics should only be run when select parameters of the |
| 17 |
// intrisic are supplied. In certain cases one of these parameters may not be |
| 18 |
// given, however the intrinsic needs to be run due to another required |
| 19 |
// parameter being supplied. In this case the missing parameter is assigned to |
| 20 |
// have an "absent" value. This typically happens in IntrinsicCall.cpp. For this |
| 21 |
// reason the extra indirection with `isAbsent` is needed for testing whether a |
| 22 |
// given parameter is actually present (so that parameters with "value" absent |
| 23 |
// are not considered as present). |
| 24 |
inline bool isAbsent(mlir::Value val) { |
| 25 |
return mlir::isa_and_nonnull<fir::AbsentOp>(val.getDefiningOp()); |
| 26 |
} |
| 27 |
|
| 28 |
mlir::Value fir::runtime::genCommandArgumentCount(fir::FirOpBuilder &builder, |
| 29 |
mlir::Location loc) { |
| 30 |
auto argumentCountFunc = |
| 31 |
fir::runtime::getRuntimeFunc<mkRTKey(ArgumentCount)>(loc, builder); |
| 32 |
return builder.create<fir::CallOp>(loc, argumentCountFunc).getResult(0); |
| 33 |
} |
| 34 |
|
| 35 |
mlir::Value fir::runtime::genGetCommand(fir::FirOpBuilder &builder, |
| 36 |
mlir::Location loc, mlir::Value command, |
| 37 |
mlir::Value length, |
| 38 |
mlir::Value errmsg) { |
| 39 |
auto runtimeFunc = |
| 40 |
fir::runtime::getRuntimeFunc<mkRTKey(GetCommand)>(loc, builder); |
| 41 |
mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType(); |
| 42 |
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
| 43 |
mlir::Value sourceLine = |
| 44 |
fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(4)); |
| 45 |
llvm::SmallVector<mlir::Value> args = |
| 46 |
fir::runtime::createArguments(builder, loc, runtimeFuncTy, command, |
| 47 |
length, errmsg, sourceFile, sourceLine); |
| 48 |
return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0); |
| 49 |
} |
| 50 |
|
| 51 |
mlir::Value fir::runtime::genGetCommandArgument( |
| 52 |
fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value number, |
| 53 |
mlir::Value value, mlir::Value length, mlir::Value errmsg) { |
| 54 |
auto runtimeFunc = |
| 55 |
fir::runtime::getRuntimeFunc<mkRTKey(GetCommandArgument)>(loc, builder); |
| 56 |
mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType(); |
| 57 |
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
| 58 |
mlir::Value sourceLine = |
| 59 |
fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(5)); |
| 60 |
llvm::SmallVector<mlir::Value> args = |
| 61 |
fir::runtime::createArguments(builder, loc, runtimeFuncTy, number, value, |
| 62 |
length, errmsg, sourceFile, sourceLine); |
| 63 |
return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0); |
| 64 |
} |
| 65 |
|
| 66 |
mlir::Value fir::runtime::genEnvVariableValue( |
| 67 |
fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value name, |
| 68 |
mlir::Value value, mlir::Value trimName, mlir::Value errmsg) { |
| 69 |
auto valueFunc = |
| 70 |
fir::runtime::getRuntimeFunc<mkRTKey(EnvVariableValue)>(loc, builder); |
| 71 |
mlir::FunctionType valueFuncTy = valueFunc.getFunctionType(); |
| 72 |
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
| 73 |
mlir::Value sourceLine = |
| 74 |
fir::factory::locationToLineNo(builder, loc, valueFuncTy.getInput(5)); |
| 75 |
llvm::SmallVector<mlir::Value> args = |
| 76 |
fir::runtime::createArguments(builder, loc, valueFuncTy, name, value, |
| 77 |
trimName, errmsg, sourceFile, sourceLine); |
| 78 |
return builder.create<fir::CallOp>(loc, valueFunc, args).getResult(0); |
| 79 |
} |
| 80 |
|
| 81 |
mlir::Value fir::runtime::genEnvVariableLength(fir::FirOpBuilder &builder, |
| 82 |
mlir::Location loc, |
| 83 |
mlir::Value name, |
| 84 |
mlir::Value trimName) { |
| 85 |
auto lengthFunc = |
| 86 |
fir::runtime::getRuntimeFunc<mkRTKey(EnvVariableLength)>(loc, builder); |
| 87 |
mlir::FunctionType lengthFuncTy = lengthFunc.getFunctionType(); |
| 88 |
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
| 89 |
mlir::Value sourceLine = |
| 90 |
fir::factory::locationToLineNo(builder, loc, lengthFuncTy.getInput(3)); |
| 91 |
llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments( |
| 92 |
builder, loc, lengthFuncTy, name, trimName, sourceFile, sourceLine); |
| 93 |
return builder.create<fir::CallOp>(loc, lengthFunc, args).getResult(0); |
| 94 |
} |