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
14using 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).
24inline bool isAbsent(mlir::Value val) {
25 return mlir::isa_and_nonnull<fir::AbsentOp>(val.getDefiningOp());
26}
27
28mlir::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
35mlir::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
51mlir::Value fir::runtime::genGetPID(fir::FirOpBuilder &builder,
52 mlir::Location loc) {
53 auto runtimeFunc =
54 fir::runtime::getRuntimeFunc<mkRTKey(GetPID)>(loc, builder);
55
56 return builder.create<fir::CallOp>(loc, runtimeFunc).getResult(0);
57}
58
59mlir::Value fir::runtime::genGetCommandArgument(
60 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value number,
61 mlir::Value value, mlir::Value length, mlir::Value errmsg) {
62 auto runtimeFunc =
63 fir::runtime::getRuntimeFunc<mkRTKey(GetCommandArgument)>(loc, builder);
64 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
65 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
66 mlir::Value sourceLine =
67 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(5));
68 llvm::SmallVector<mlir::Value> args =
69 fir::runtime::createArguments(builder, loc, runtimeFuncTy, number, value,
70 length, errmsg, sourceFile, sourceLine);
71 return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0);
72}
73
74mlir::Value fir::runtime::genGetEnvVariable(fir::FirOpBuilder &builder,
75 mlir::Location loc,
76 mlir::Value name, mlir::Value value,
77 mlir::Value length,
78 mlir::Value trimName,
79 mlir::Value errmsg) {
80 auto runtimeFunc =
81 fir::runtime::getRuntimeFunc<mkRTKey(GetEnvVariable)>(loc, builder);
82 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
83 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
84 mlir::Value sourceLine =
85 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(6));
86 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
87 builder, loc, runtimeFuncTy, name, value, length, trimName, errmsg,
88 sourceFile, sourceLine);
89 return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0);
90}
91

source code of flang/lib/Optimizer/Builder/Runtime/Command.cpp