97 lines · cpp
1//===-- fc1_main.cpp - Flang FC1 Compiler Frontend ------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This is the entry point to the flang -fc1 functionality, which implements the10// core compiler functionality along with a number of additional tools for11// demonstration and testing purposes.12//13//===----------------------------------------------------------------------===//14//15// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/16//17//===----------------------------------------------------------------------===//18 19#include "flang/Frontend/CompilerInstance.h"20#include "flang/Frontend/CompilerInvocation.h"21#include "flang/Frontend/TextDiagnosticBuffer.h"22#include "flang/FrontendTool/Utils.h"23#include "clang/Driver/DriverDiagnostic.h"24#include "llvm/MC/TargetRegistry.h"25#include "llvm/Option/Arg.h"26#include "llvm/Option/ArgList.h"27#include "llvm/Option/OptTable.h"28#include "llvm/Support/TargetSelect.h"29#include "llvm/Support/raw_ostream.h"30 31#include <cstdio>32 33using namespace Fortran::frontend;34 35/// Print supported cpus of the given target.36static int printSupportedCPUs(llvm::StringRef triple) {37 llvm::Triple parsedTriple(triple);38 std::string error;39 const llvm::Target *target =40 llvm::TargetRegistry::lookupTarget(parsedTriple, error);41 if (!target) {42 llvm::errs() << error;43 return 1;44 }45 46 // the target machine will handle the mcpu printing47 llvm::TargetOptions targetOpts;48 std::unique_ptr<llvm::TargetMachine> targetMachine(49 target->createTargetMachine(parsedTriple, "", "+cpuhelp", targetOpts,50 std::nullopt));51 return 0;52}53 54int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {55 // Create CompilerInstance56 std::unique_ptr<CompilerInstance> flang(new CompilerInstance());57 58 // Create DiagnosticsEngine for the frontend driver59 flang->createDiagnostics();60 if (!flang->hasDiagnostics())61 return 1;62 63 // We will buffer diagnostics from argument parsing so that we can output64 // them using a well formed diagnostic object.65 TextDiagnosticBuffer *diagsBuffer = new TextDiagnosticBuffer;66 67 // Create CompilerInvocation - use a dedicated instance of DiagnosticsEngine68 // for parsing the arguments69 clang::DiagnosticOptions diagOpts;70 clang::DiagnosticsEngine diags(clang::DiagnosticIDs::create(), diagOpts,71 diagsBuffer);72 bool success = CompilerInvocation::createFromArgs(flang->getInvocation(),73 argv, diags, argv0);74 75 // Initialize targets first, so that --version shows registered targets.76 llvm::InitializeAllTargets();77 llvm::InitializeAllTargetMCs();78 llvm::InitializeAllAsmPrinters();79 80 // --print-supported-cpus takes priority over the actual compilation.81 if (flang->getFrontendOpts().printSupportedCPUs)82 return printSupportedCPUs(flang->getInvocation().getTargetOpts().triple);83 84 diagsBuffer->flushDiagnostics(flang->getDiagnostics());85 86 if (!success)87 return 1;88 89 // Execute the frontend actions.90 success = executeCompilerInvocation(flang.get());91 92 // Delete output files to free Compiler Instance93 flang->clearOutputFiles(/*EraseFiles=*/false);94 95 return !success;96}97