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#include "flang/Runtime/extensions.h"
14
15using namespace Fortran::runtime;
16
17// Certain runtime intrinsics should only be run when select parameters of the
18// intrisic are supplied. In certain cases one of these parameters may not be
19// given, however the intrinsic needs to be run due to another required
20// parameter being supplied. In this case the missing parameter is assigned to
21// have an "absent" value. This typically happens in IntrinsicCall.cpp. For this
22// reason the extra indirection with `isAbsent` is needed for testing whether a
23// given parameter is actually present (so that parameters with "value" absent
24// are not considered as present).
25inline bool isAbsent(mlir::Value val) {
26 return mlir::isa_and_nonnull<fir::AbsentOp>(val.getDefiningOp());
27}
28
29mlir::Value fir::runtime::genCommandArgumentCount(fir::FirOpBuilder &builder,
30 mlir::Location loc) {
31 auto argumentCountFunc =
32 fir::runtime::getRuntimeFunc<mkRTKey(ArgumentCount)>(loc, builder);
33 return builder.create<fir::CallOp>(loc, argumentCountFunc).getResult(0);
34}
35
36mlir::Value fir::runtime::genGetCommand(fir::FirOpBuilder &builder,
37 mlir::Location loc, mlir::Value command,
38 mlir::Value length,
39 mlir::Value errmsg) {
40 auto runtimeFunc =
41 fir::runtime::getRuntimeFunc<mkRTKey(GetCommand)>(loc, builder);
42 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
43 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
44 mlir::Value sourceLine =
45 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(4));
46 llvm::SmallVector<mlir::Value> args =
47 fir::runtime::createArguments(builder, loc, runtimeFuncTy, command,
48 length, errmsg, sourceFile, sourceLine);
49 return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0);
50}
51
52mlir::Value fir::runtime::genGetPID(fir::FirOpBuilder &builder,
53 mlir::Location loc) {
54 auto runtimeFunc =
55 fir::runtime::getRuntimeFunc<mkRTKey(GetPID)>(loc, builder);
56
57 return builder.create<fir::CallOp>(loc, runtimeFunc).getResult(0);
58}
59
60mlir::Value fir::runtime::genGetCommandArgument(
61 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value number,
62 mlir::Value value, mlir::Value length, mlir::Value errmsg) {
63 auto runtimeFunc =
64 fir::runtime::getRuntimeFunc<mkRTKey(GetCommandArgument)>(loc, builder);
65 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
66 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
67 mlir::Value sourceLine =
68 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(5));
69 llvm::SmallVector<mlir::Value> args =
70 fir::runtime::createArguments(builder, loc, runtimeFuncTy, number, value,
71 length, errmsg, sourceFile, sourceLine);
72 return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0);
73}
74
75mlir::Value fir::runtime::genGetEnvVariable(fir::FirOpBuilder &builder,
76 mlir::Location loc,
77 mlir::Value name, mlir::Value value,
78 mlir::Value length,
79 mlir::Value trimName,
80 mlir::Value errmsg) {
81 auto runtimeFunc =
82 fir::runtime::getRuntimeFunc<mkRTKey(GetEnvVariable)>(loc, builder);
83 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
84 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
85 mlir::Value sourceLine =
86 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(6));
87 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
88 builder, loc, runtimeFuncTy, name, value, length, trimName, errmsg,
89 sourceFile, sourceLine);
90 return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0);
91}
92
93mlir::Value fir::runtime::genGetCwd(fir::FirOpBuilder &builder,
94 mlir::Location loc, mlir::Value cwd) {
95 mlir::func::FuncOp func =
96 fir::runtime::getRuntimeFunc<mkRTKey(GetCwd)>(loc, builder);
97 auto runtimeFuncTy = func.getFunctionType();
98 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
99 mlir::Value sourceLine =
100 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(2));
101 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
102 builder, loc, runtimeFuncTy, cwd, sourceFile, sourceLine);
103 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
104}
105
106mlir::Value fir::runtime::genHostnm(fir::FirOpBuilder &builder,
107 mlir::Location loc, mlir::Value res) {
108 mlir::func::FuncOp func =
109 fir::runtime::getRuntimeFunc<mkRTKey(Hostnm)>(loc, builder);
110 auto runtimeFuncTy = func.getFunctionType();
111 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
112 mlir::Value sourceLine =
113 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(2));
114 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
115 builder, loc, runtimeFuncTy, res, sourceFile, sourceLine);
116 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
117}
118
119void fir::runtime::genPerror(fir::FirOpBuilder &builder, mlir::Location loc,
120 mlir::Value string) {
121 auto runtimeFunc =
122 fir::runtime::getRuntimeFunc<mkRTKey(Perror)>(loc, builder);
123 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
124 llvm::SmallVector<mlir::Value> args =
125 fir::runtime::createArguments(builder, loc, runtimeFuncTy, string);
126 builder.create<fir::CallOp>(loc, runtimeFunc, args);
127}
128
129mlir::Value fir::runtime::genPutEnv(fir::FirOpBuilder &builder,
130 mlir::Location loc, mlir::Value str,
131 mlir::Value strLength) {
132 mlir::func::FuncOp func =
133 fir::runtime::getRuntimeFunc<mkRTKey(PutEnv)>(loc, builder);
134 auto runtimeFuncTy = func.getFunctionType();
135 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
136 mlir::Value sourceLine =
137 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(1));
138 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
139 builder, loc, runtimeFuncTy, str, strLength, sourceFile, sourceLine);
140 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
141}
142
143mlir::Value fir::runtime::genUnlink(fir::FirOpBuilder &builder,
144 mlir::Location loc, mlir::Value path,
145 mlir::Value pathLength) {
146 mlir::func::FuncOp func =
147 fir::runtime::getRuntimeFunc<mkRTKey(Unlink)>(loc, builder);
148 auto runtimeFuncTy = func.getFunctionType();
149 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
150 mlir::Value sourceLine =
151 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(1));
152 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
153 builder, loc, runtimeFuncTy, path, pathLength, sourceFile, sourceLine);
154 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
155}
156

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