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 /// Add -x lang to \p CmdArgs for \p Input. 23 static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 24 ArgStringList &CmdArgs) { 25 CmdArgs.push_back("-x"); 26 // Map the driver type to the frontend type. 27 CmdArgs.push_back(types::getTypeName(Input.getType())); 28 } 29 30 void Flang::AddFortranDialectOptions(const ArgList &Args, 31 ArgStringList &CmdArgs) const { 32 Args.AddAllArgs( 33 CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form, 34 options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp, 35 options::OPT_fopenacc, options::OPT_finput_charset_EQ, 36 options::OPT_fimplicit_none, options::OPT_fno_implicit_none, 37 options::OPT_fbackslash, options::OPT_fno_backslash, 38 options::OPT_flogical_abbreviations, 39 options::OPT_fno_logical_abbreviations, 40 options::OPT_fxor_operator, options::OPT_fno_xor_operator, 41 options::OPT_falternative_parameter_statement, 42 options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8, 43 options::OPT_fdefault_double_8, options::OPT_flarge_sizes, 44 options::OPT_fno_automatic}); 45 } 46 47 void Flang::AddPreprocessingOptions(const ArgList &Args, 48 ArgStringList &CmdArgs) const { 49 Args.AddAllArgs(CmdArgs, 50 {options::OPT_P, options::OPT_D, options::OPT_U, 51 options::OPT_I, options::OPT_cpp, options::OPT_nocpp}); 52 } 53 54 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { 55 Args.AddAllArgs(CmdArgs, 56 {options::OPT_module_dir, options::OPT_fdebug_module_writer, 57 options::OPT_fintrinsic_modules_path, options::OPT_pedantic, 58 options::OPT_std_EQ, options::OPT_W_Joined}); 59 } 60 61 void Flang::ConstructJob(Compilation &C, const JobAction &JA, 62 const InputInfo &Output, const InputInfoList &Inputs, 63 const ArgList &Args, const char *LinkingOutput) const { 64 const auto &TC = getToolChain(); 65 const llvm::Triple &Triple = TC.getEffectiveTriple(); 66 const std::string &TripleStr = Triple.getTriple(); 67 68 ArgStringList CmdArgs; 69 70 // Invoke ourselves in -fc1 mode. 71 CmdArgs.push_back("-fc1"); 72 73 // Add the "effective" target triple. 74 CmdArgs.push_back("-triple"); 75 CmdArgs.push_back(Args.MakeArgString(TripleStr)); 76 77 if (isa<PreprocessJobAction>(JA)) { 78 CmdArgs.push_back("-E"); 79 } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) { 80 if (JA.getType() == types::TY_Nothing) { 81 CmdArgs.push_back("-fsyntax-only"); 82 } else if (JA.getType() == types::TY_AST) { 83 CmdArgs.push_back("-emit-ast"); 84 } else if (JA.getType() == types::TY_LLVM_IR || 85 JA.getType() == types::TY_LTO_IR) { 86 CmdArgs.push_back("-emit-llvm"); 87 } else if (JA.getType() == types::TY_LLVM_BC || 88 JA.getType() == types::TY_LTO_BC) { 89 CmdArgs.push_back("-emit-llvm-bc"); 90 } else if (JA.getType() == types::TY_PP_Asm) { 91 CmdArgs.push_back("-S"); 92 } else { 93 assert(false && "Unexpected output type!"); 94 } 95 } else if (isa<AssembleJobAction>(JA)) { 96 CmdArgs.push_back("-emit-obj"); 97 } else { 98 assert(false && "Unexpected action class for Flang tool."); 99 } 100 101 const InputInfo &Input = Inputs[0]; 102 types::ID InputType = Input.getType(); 103 104 // Add preprocessing options like -I, -D, etc. if we are using the 105 // preprocessor (i.e. skip when dealing with e.g. binary files). 106 if (types::getPreprocessedType(InputType) != types::TY_INVALID) 107 AddPreprocessingOptions(Args, CmdArgs); 108 109 AddFortranDialectOptions(Args, CmdArgs); 110 111 // Add other compile options 112 AddOtherOptions(Args, CmdArgs); 113 114 // Forward -Xflang arguments to -fc1 115 Args.AddAllArgValues(CmdArgs, options::OPT_Xflang); 116 117 // Forward -mllvm options to the LLVM option parser. In practice, this means 118 // forwarding to `-fc1` as that's where the LLVM parser is run. 119 for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 120 A->claim(); 121 A->render(Args, CmdArgs); 122 } 123 124 for (const Arg *A : Args.filtered(options::OPT_mmlir)) { 125 A->claim(); 126 A->render(Args, CmdArgs); 127 } 128 129 if (Output.isFilename()) { 130 CmdArgs.push_back("-o"); 131 CmdArgs.push_back(Output.getFilename()); 132 } else { 133 assert(Output.isNothing() && "Invalid output."); 134 } 135 136 assert(Input.isFilename() && "Invalid input."); 137 138 addDashXForInput(Args, Input, CmdArgs); 139 140 CmdArgs.push_back(Input.getFilename()); 141 142 const auto& D = C.getDriver(); 143 // TODO: Replace flang-new with flang once the new driver replaces the 144 // throwaway driver 145 const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC)); 146 C.addCommand(std::make_unique<Command>(JA, *this, 147 ResponseFileSupport::AtFileUTF8(), 148 Exec, CmdArgs, Inputs, Output)); 149 } 150 151 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {} 152 153 Flang::~Flang() {} 154