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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This is the entry point to the flang -fc1 functionality, which implements the 10 // core compiler functionality along with a number of additional tools for 11 // 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 33 using namespace Fortran::frontend; 34 35 /// Print supported cpus of the given target. 36 static int printSupportedCPUs(llvm::StringRef triple) { 37 std::string error; 38 const llvm::Target *target = 39 llvm::TargetRegistry::lookupTarget(triple, error); 40 if (!target) { 41 llvm::errs() << error; 42 return 1; 43 } 44 45 // the target machine will handle the mcpu printing 46 llvm::TargetOptions targetOpts; 47 std::unique_ptr<llvm::TargetMachine> targetMachine( 48 target->createTargetMachine(triple, "", "+cpuhelp", targetOpts, 49 std::nullopt)); 50 return 0; 51 } 52 53 int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) { 54 // Create CompilerInstance 55 std::unique_ptr<CompilerInstance> flang(new CompilerInstance()); 56 57 // Create DiagnosticsEngine for the frontend driver 58 flang->createDiagnostics(); 59 if (!flang->hasDiagnostics()) 60 return 1; 61 62 // We will buffer diagnostics from argument parsing so that we can output 63 // them using a well formed diagnostic object. 64 TextDiagnosticBuffer *diagsBuffer = new TextDiagnosticBuffer; 65 66 // Create CompilerInvocation - use a dedicated instance of DiagnosticsEngine 67 // for parsing the arguments 68 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID( 69 new clang::DiagnosticIDs()); 70 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts = 71 new clang::DiagnosticOptions(); 72 clang::DiagnosticsEngine diags(diagID, &*diagOpts, diagsBuffer); 73 bool success = CompilerInvocation::createFromArgs(flang->getInvocation(), 74 argv, diags, argv0); 75 76 // Initialize targets first, so that --version shows registered targets. 77 llvm::InitializeAllTargets(); 78 llvm::InitializeAllTargetMCs(); 79 llvm::InitializeAllAsmPrinters(); 80 81 // --print-supported-cpus takes priority over the actual compilation. 82 if (flang->getFrontendOpts().printSupportedCPUs) 83 return printSupportedCPUs(flang->getInvocation().getTargetOpts().triple); 84 85 diagsBuffer->flushDiagnostics(flang->getDiagnostics()); 86 87 if (!success) 88 return 1; 89 90 // Execute the frontend actions. 91 success = executeCompilerInvocation(flang.get()); 92 93 // Delete output files to free Compiler Instance 94 flang->clearOutputFiles(/*EraseFiles=*/false); 95 96 return !success; 97 } 98