| 1 |
//===-- Main.cpp - generate main runtime API calls --------------*- 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 |
#include "flang/Optimizer/Builder/Runtime/Main.h" |
| 10 |
#include "flang/Lower/EnvironmentDefault.h" |
| 11 |
#include "flang/Optimizer/Builder/BoxValue.h" |
| 12 |
#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 13 |
#include "flang/Optimizer/Builder/Runtime/EnvironmentDefaults.h" |
| 14 |
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h" |
| 15 |
#include "flang/Optimizer/Dialect/FIROps.h" |
| 16 |
#include "flang/Optimizer/Dialect/FIRType.h" |
| 17 |
#include "flang/Runtime/main.h" |
| 18 |
#include "flang/Runtime/stop.h" |
| 19 |
|
| 20 |
using namespace Fortran::runtime; |
| 21 |
|
| 22 |
/// Create a `int main(...)` that calls the Fortran entry point |
| 23 |
void fir::runtime::genMain( |
| 24 |
fir::FirOpBuilder &builder, mlir::Location loc, |
| 25 |
const std::vector<Fortran::lower::EnvironmentDefault> &defs) { |
| 26 |
auto *context = builder.getContext(); |
| 27 |
auto argcTy = builder.getDefaultIntegerType(); |
| 28 |
auto ptrTy = mlir::LLVM::LLVMPointerType::get(context); |
| 29 |
|
| 30 |
// void ProgramStart(int argc, char** argv, char** envp, |
| 31 |
// _QQEnvironmentDefaults* env) |
| 32 |
auto startFn = builder.createFunction( |
| 33 |
loc, RTNAME_STRING(ProgramStart), |
| 34 |
mlir::FunctionType::get(context, {argcTy, ptrTy, ptrTy, ptrTy}, {})); |
| 35 |
// void ProgramStop() |
| 36 |
auto stopFn = |
| 37 |
builder.createFunction(loc, RTNAME_STRING(ProgramEndStatement), |
| 38 |
mlir::FunctionType::get(context, {}, {})); |
| 39 |
|
| 40 |
// int main(int argc, char** argv, char** envp) |
| 41 |
auto mainFn = builder.createFunction( |
| 42 |
loc, "main", |
| 43 |
mlir::FunctionType::get(context, {argcTy, ptrTy, ptrTy}, argcTy)); |
| 44 |
// void _QQmain() |
| 45 |
auto qqMainFn = builder.createFunction( |
| 46 |
loc, "_QQmain", mlir::FunctionType::get(context, {}, {})); |
| 47 |
|
| 48 |
mainFn.setPublic(); |
| 49 |
|
| 50 |
auto *block = mainFn.addEntryBlock(); |
| 51 |
mlir::OpBuilder::InsertionGuard insertGuard(builder); |
| 52 |
builder.setInsertionPointToStart(block); |
| 53 |
|
| 54 |
// Create the list of any environment defaults for the runtime to set. The |
| 55 |
// runtime default list is only created if there is a main program to ensure |
| 56 |
// it only happens once and to provide consistent results if multiple files |
| 57 |
// are compiled separately. |
| 58 |
auto env = fir::runtime::genEnvironmentDefaults(builder, loc, defs); |
| 59 |
|
| 60 |
llvm::SmallVector<mlir::Value, 4> args(block->getArguments()); |
| 61 |
args.push_back(env); |
| 62 |
|
| 63 |
builder.create<fir::CallOp>(loc, startFn, args); |
| 64 |
builder.create<fir::CallOp>(loc, qqMainFn); |
| 65 |
builder.create<fir::CallOp>(loc, stopFn); |
| 66 |
|
| 67 |
mlir::Value ret = builder.createIntegerConstant(loc, argcTy, 0); |
| 68 |
builder.create<mlir::func::ReturnOp>(loc, ret); |
| 69 |
} |