1 //===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- 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 10 #include "Flang.h" 11 #include "CommonArgs.h" 12 13 #include "clang/Driver/Options.h" 14 15 #include <cassert> 16 17 using namespace clang::driver; 18 using namespace clang::driver::tools; 19 using namespace clang; 20 using namespace llvm::opt; 21 22 void Flang::AddFortranDialectOptions(const ArgList &Args, 23 ArgStringList &CmdArgs) const { 24 Args.AddAllArgs(CmdArgs, 25 {options::OPT_ffixed_form, options::OPT_ffree_form, 26 options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp, 27 options::OPT_fopenacc, options::OPT_finput_charset_EQ, 28 options::OPT_fimplicit_none, options::OPT_fno_implicit_none, 29 options::OPT_fbackslash, options::OPT_fno_backslash, 30 options::OPT_flogical_abbreviations, 31 options::OPT_fno_logical_abbreviations, 32 options::OPT_fxor_operator, options::OPT_fno_xor_operator, 33 options::OPT_falternative_parameter_statement}); 34 } 35 36 void Flang::AddPreprocessingOptions(const ArgList &Args, 37 ArgStringList &CmdArgs) const { 38 Args.AddAllArgs(CmdArgs, {options::OPT_D, options::OPT_U, options::OPT_I}); 39 } 40 41 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { 42 Args.AddAllArgs(CmdArgs, options::OPT_module_dir); 43 } 44 45 void Flang::ConstructJob(Compilation &C, const JobAction &JA, 46 const InputInfo &Output, const InputInfoList &Inputs, 47 const ArgList &Args, const char *LinkingOutput) const { 48 const auto &TC = getToolChain(); 49 // TODO: Once code-generation is available, this will need to be commented 50 // out. 51 // const llvm::Triple &Triple = TC.getEffectiveTriple(); 52 // const std::string &TripleStr = Triple.getTriple(); 53 54 ArgStringList CmdArgs; 55 56 // Invoke ourselves in -fc1 mode. 57 CmdArgs.push_back("-fc1"); 58 59 // TODO: Once code-generation is available, this will need to be commented 60 // out. 61 // Add the "effective" target triple. 62 // CmdArgs.push_back("-triple"); 63 // CmdArgs.push_back(Args.MakeArgString(TripleStr)); 64 65 if (isa<PreprocessJobAction>(JA)) { 66 CmdArgs.push_back("-E"); 67 } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) { 68 if (JA.getType() == types::TY_Nothing) { 69 CmdArgs.push_back("-fsyntax-only"); 70 } else if (JA.getType() == types::TY_AST) { 71 CmdArgs.push_back("-emit-ast"); 72 } else if (JA.getType() == types::TY_LLVM_IR || 73 JA.getType() == types::TY_LTO_IR) { 74 CmdArgs.push_back("-emit-llvm"); 75 } else if (JA.getType() == types::TY_LLVM_BC || 76 JA.getType() == types::TY_LTO_BC) { 77 CmdArgs.push_back("-emit-llvm-bc"); 78 } else if (JA.getType() == types::TY_PP_Asm) { 79 CmdArgs.push_back("-S"); 80 } else { 81 assert(false && "Unexpected output type!"); 82 } 83 } else if (isa<AssembleJobAction>(JA)) { 84 CmdArgs.push_back("-emit-obj"); 85 } else { 86 assert(false && "Unexpected action class for Flang tool."); 87 } 88 89 const InputInfo &Input = Inputs[0]; 90 types::ID InputType = Input.getType(); 91 92 // Add preprocessing options like -I, -D, etc. if we are using the 93 // preprocessor (i.e. skip when dealing with e.g. binary files). 94 if (types::getPreprocessedType(InputType) != types::TY_INVALID) 95 AddPreprocessingOptions(Args, CmdArgs); 96 97 AddFortranDialectOptions(Args, CmdArgs); 98 99 // Add other compile options 100 AddOtherOptions(Args, CmdArgs); 101 102 // Forward -Xflang arguments to -fc1 103 Args.AddAllArgValues(CmdArgs, options::OPT_Xflang); 104 105 if (Output.isFilename()) { 106 CmdArgs.push_back("-o"); 107 CmdArgs.push_back(Output.getFilename()); 108 } else { 109 assert(Output.isNothing() && "Invalid output."); 110 } 111 112 assert(Input.isFilename() && "Invalid input."); 113 CmdArgs.push_back(Input.getFilename()); 114 115 const auto& D = C.getDriver(); 116 // TODO: Replace flang-new with flang once the new driver replaces the 117 // throwaway driver 118 const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC)); 119 C.addCommand(std::make_unique<Command>(JA, *this, 120 ResponseFileSupport::AtFileUTF8(), 121 Exec, CmdArgs, Inputs, Output)); 122 } 123 124 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {} 125 126 Flang::~Flang() {} 127