1480093f4SDimitry Andric //===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- C++ -*-===// 2480093f4SDimitry Andric // 3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6480093f4SDimitry Andric // 7480093f4SDimitry Andric //===----------------------------------------------------------------------===// 8480093f4SDimitry Andric 9480093f4SDimitry Andric 10480093f4SDimitry Andric #include "Flang.h" 11480093f4SDimitry Andric #include "CommonArgs.h" 12480093f4SDimitry Andric 13480093f4SDimitry Andric #include "clang/Driver/Options.h" 14480093f4SDimitry Andric 15480093f4SDimitry Andric #include <cassert> 16480093f4SDimitry Andric 17480093f4SDimitry Andric using namespace clang::driver; 18480093f4SDimitry Andric using namespace clang::driver::tools; 19480093f4SDimitry Andric using namespace clang; 20480093f4SDimitry Andric using namespace llvm::opt; 21480093f4SDimitry Andric 22*81ad6265SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input. 23*81ad6265SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 24*81ad6265SDimitry Andric ArgStringList &CmdArgs) { 25*81ad6265SDimitry Andric CmdArgs.push_back("-x"); 26*81ad6265SDimitry Andric // Map the driver type to the frontend type. 27*81ad6265SDimitry Andric CmdArgs.push_back(types::getTypeName(Input.getType())); 28*81ad6265SDimitry Andric } 29*81ad6265SDimitry Andric 30fe6060f1SDimitry Andric void Flang::AddFortranDialectOptions(const ArgList &Args, 31fe6060f1SDimitry Andric ArgStringList &CmdArgs) const { 32fe6060f1SDimitry Andric Args.AddAllArgs( 33fe6060f1SDimitry Andric CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form, 34fe6060f1SDimitry Andric options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp, 35fe6060f1SDimitry Andric options::OPT_fopenacc, options::OPT_finput_charset_EQ, 36fe6060f1SDimitry Andric options::OPT_fimplicit_none, options::OPT_fno_implicit_none, 37fe6060f1SDimitry Andric options::OPT_fbackslash, options::OPT_fno_backslash, 38fe6060f1SDimitry Andric options::OPT_flogical_abbreviations, 39fe6060f1SDimitry Andric options::OPT_fno_logical_abbreviations, 40fe6060f1SDimitry Andric options::OPT_fxor_operator, options::OPT_fno_xor_operator, 41fe6060f1SDimitry Andric options::OPT_falternative_parameter_statement, 42fe6060f1SDimitry Andric options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8, 434824e7fdSDimitry Andric options::OPT_fdefault_double_8, options::OPT_flarge_sizes, 444824e7fdSDimitry Andric options::OPT_fno_automatic}); 45fe6060f1SDimitry Andric } 46fe6060f1SDimitry Andric 47e8d8bef9SDimitry Andric void Flang::AddPreprocessingOptions(const ArgList &Args, 48e8d8bef9SDimitry Andric ArgStringList &CmdArgs) const { 49349cc55cSDimitry Andric Args.AddAllArgs(CmdArgs, 50349cc55cSDimitry Andric {options::OPT_P, options::OPT_D, options::OPT_U, 51349cc55cSDimitry Andric options::OPT_I, options::OPT_cpp, options::OPT_nocpp}); 52fe6060f1SDimitry Andric } 53fe6060f1SDimitry Andric 54fe6060f1SDimitry Andric void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { 55fe6060f1SDimitry Andric Args.AddAllArgs(CmdArgs, 56fe6060f1SDimitry Andric {options::OPT_module_dir, options::OPT_fdebug_module_writer, 57fe6060f1SDimitry Andric options::OPT_fintrinsic_modules_path, options::OPT_pedantic, 58fe6060f1SDimitry Andric options::OPT_std_EQ, options::OPT_W_Joined}); 59e8d8bef9SDimitry Andric } 60e8d8bef9SDimitry Andric 61480093f4SDimitry Andric void Flang::ConstructJob(Compilation &C, const JobAction &JA, 62480093f4SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 63480093f4SDimitry Andric const ArgList &Args, const char *LinkingOutput) const { 64480093f4SDimitry Andric const auto &TC = getToolChain(); 65*81ad6265SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 66*81ad6265SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 67480093f4SDimitry Andric 68*81ad6265SDimitry Andric const Driver &D = TC.getDriver(); 69480093f4SDimitry Andric ArgStringList CmdArgs; 70480093f4SDimitry Andric 71e8d8bef9SDimitry Andric // Invoke ourselves in -fc1 mode. 72480093f4SDimitry Andric CmdArgs.push_back("-fc1"); 73480093f4SDimitry Andric 74e8d8bef9SDimitry Andric // Add the "effective" target triple. 75*81ad6265SDimitry Andric CmdArgs.push_back("-triple"); 76*81ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 77480093f4SDimitry Andric 78480093f4SDimitry Andric if (isa<PreprocessJobAction>(JA)) { 79480093f4SDimitry Andric CmdArgs.push_back("-E"); 80480093f4SDimitry Andric } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) { 81480093f4SDimitry Andric if (JA.getType() == types::TY_Nothing) { 82480093f4SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 83480093f4SDimitry Andric } else if (JA.getType() == types::TY_AST) { 84480093f4SDimitry Andric CmdArgs.push_back("-emit-ast"); 85480093f4SDimitry Andric } else if (JA.getType() == types::TY_LLVM_IR || 86480093f4SDimitry Andric JA.getType() == types::TY_LTO_IR) { 87480093f4SDimitry Andric CmdArgs.push_back("-emit-llvm"); 88480093f4SDimitry Andric } else if (JA.getType() == types::TY_LLVM_BC || 89480093f4SDimitry Andric JA.getType() == types::TY_LTO_BC) { 90480093f4SDimitry Andric CmdArgs.push_back("-emit-llvm-bc"); 91480093f4SDimitry Andric } else if (JA.getType() == types::TY_PP_Asm) { 92480093f4SDimitry Andric CmdArgs.push_back("-S"); 93480093f4SDimitry Andric } else { 94480093f4SDimitry Andric assert(false && "Unexpected output type!"); 95480093f4SDimitry Andric } 96480093f4SDimitry Andric } else if (isa<AssembleJobAction>(JA)) { 97480093f4SDimitry Andric CmdArgs.push_back("-emit-obj"); 98480093f4SDimitry Andric } else { 99480093f4SDimitry Andric assert(false && "Unexpected action class for Flang tool."); 100480093f4SDimitry Andric } 101480093f4SDimitry Andric 102e8d8bef9SDimitry Andric const InputInfo &Input = Inputs[0]; 103e8d8bef9SDimitry Andric types::ID InputType = Input.getType(); 104e8d8bef9SDimitry Andric 105e8d8bef9SDimitry Andric // Add preprocessing options like -I, -D, etc. if we are using the 106e8d8bef9SDimitry Andric // preprocessor (i.e. skip when dealing with e.g. binary files). 107e8d8bef9SDimitry Andric if (types::getPreprocessedType(InputType) != types::TY_INVALID) 108e8d8bef9SDimitry Andric AddPreprocessingOptions(Args, CmdArgs); 109e8d8bef9SDimitry Andric 110fe6060f1SDimitry Andric AddFortranDialectOptions(Args, CmdArgs); 111fe6060f1SDimitry Andric 112*81ad6265SDimitry Andric // Color diagnostics are parsed by the driver directly from argv and later 113*81ad6265SDimitry Andric // re-parsed to construct this job; claim any possible color diagnostic here 114*81ad6265SDimitry Andric // to avoid warn_drv_unused_argument. 115*81ad6265SDimitry Andric Args.getLastArg(options::OPT_fcolor_diagnostics, 116*81ad6265SDimitry Andric options::OPT_fno_color_diagnostics); 117*81ad6265SDimitry Andric if (D.getDiags().getDiagnosticOptions().ShowColors) 118*81ad6265SDimitry Andric CmdArgs.push_back("-fcolor-diagnostics"); 119*81ad6265SDimitry Andric 120fe6060f1SDimitry Andric // Add other compile options 121fe6060f1SDimitry Andric AddOtherOptions(Args, CmdArgs); 122fe6060f1SDimitry Andric 123fe6060f1SDimitry Andric // Forward -Xflang arguments to -fc1 124fe6060f1SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xflang); 125fe6060f1SDimitry Andric 126*81ad6265SDimitry Andric // Forward -mllvm options to the LLVM option parser. In practice, this means 127*81ad6265SDimitry Andric // forwarding to `-fc1` as that's where the LLVM parser is run. 128*81ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 129*81ad6265SDimitry Andric A->claim(); 130*81ad6265SDimitry Andric A->render(Args, CmdArgs); 131*81ad6265SDimitry Andric } 132*81ad6265SDimitry Andric 133*81ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mmlir)) { 134*81ad6265SDimitry Andric A->claim(); 135*81ad6265SDimitry Andric A->render(Args, CmdArgs); 136*81ad6265SDimitry Andric } 137*81ad6265SDimitry Andric 138*81ad6265SDimitry Andric // Optimization level for CodeGen. 139*81ad6265SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 140*81ad6265SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 141*81ad6265SDimitry Andric CmdArgs.push_back("-O3"); 142*81ad6265SDimitry Andric D.Diag(diag::warn_O4_is_O3); 143*81ad6265SDimitry Andric } else { 144*81ad6265SDimitry Andric A->render(Args, CmdArgs); 145*81ad6265SDimitry Andric } 146*81ad6265SDimitry Andric } 147*81ad6265SDimitry Andric 148480093f4SDimitry Andric if (Output.isFilename()) { 149480093f4SDimitry Andric CmdArgs.push_back("-o"); 150480093f4SDimitry Andric CmdArgs.push_back(Output.getFilename()); 151480093f4SDimitry Andric } else { 152480093f4SDimitry Andric assert(Output.isNothing() && "Invalid output."); 153480093f4SDimitry Andric } 154480093f4SDimitry Andric 155480093f4SDimitry Andric assert(Input.isFilename() && "Invalid input."); 156*81ad6265SDimitry Andric 157*81ad6265SDimitry Andric addDashXForInput(Args, Input, CmdArgs); 158*81ad6265SDimitry Andric 159480093f4SDimitry Andric CmdArgs.push_back(Input.getFilename()); 160480093f4SDimitry Andric 161e8d8bef9SDimitry Andric // TODO: Replace flang-new with flang once the new driver replaces the 162e8d8bef9SDimitry Andric // throwaway driver 163e8d8bef9SDimitry Andric const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC)); 164e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 165e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 166e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 167480093f4SDimitry Andric } 168480093f4SDimitry Andric 169e8d8bef9SDimitry Andric Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {} 170480093f4SDimitry Andric 171480093f4SDimitry Andric Flang::~Flang() {} 172