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( 25 CmdArgs, {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 options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8, 35 options::OPT_fdefault_double_8, options::OPT_flarge_sizes}); 36 } 37 38 void Flang::AddPreprocessingOptions(const ArgList &Args, 39 ArgStringList &CmdArgs) const { 40 Args.AddAllArgs(CmdArgs, 41 {options::OPT_P, options::OPT_D, options::OPT_U, 42 options::OPT_I, options::OPT_cpp, options::OPT_nocpp}); 43 } 44 45 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { 46 Args.AddAllArgs(CmdArgs, 47 {options::OPT_module_dir, options::OPT_fdebug_module_writer, 48 options::OPT_fintrinsic_modules_path, options::OPT_pedantic, 49 options::OPT_std_EQ, options::OPT_W_Joined}); 50 } 51 52 void Flang::ConstructJob(Compilation &C, const JobAction &JA, 53 const InputInfo &Output, const InputInfoList &Inputs, 54 const ArgList &Args, const char *LinkingOutput) const { 55 const auto &TC = getToolChain(); 56 // TODO: Once code-generation is available, this will need to be commented 57 // out. 58 // const llvm::Triple &Triple = TC.getEffectiveTriple(); 59 // const std::string &TripleStr = Triple.getTriple(); 60 61 ArgStringList CmdArgs; 62 63 // Invoke ourselves in -fc1 mode. 64 CmdArgs.push_back("-fc1"); 65 66 // TODO: Once code-generation is available, this will need to be commented 67 // out. 68 // Add the "effective" target triple. 69 // CmdArgs.push_back("-triple"); 70 // CmdArgs.push_back(Args.MakeArgString(TripleStr)); 71 72 if (isa<PreprocessJobAction>(JA)) { 73 CmdArgs.push_back("-E"); 74 } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) { 75 if (JA.getType() == types::TY_Nothing) { 76 CmdArgs.push_back("-fsyntax-only"); 77 } else if (JA.getType() == types::TY_AST) { 78 CmdArgs.push_back("-emit-ast"); 79 } else if (JA.getType() == types::TY_LLVM_IR || 80 JA.getType() == types::TY_LTO_IR) { 81 CmdArgs.push_back("-emit-llvm"); 82 } else if (JA.getType() == types::TY_LLVM_BC || 83 JA.getType() == types::TY_LTO_BC) { 84 CmdArgs.push_back("-emit-llvm-bc"); 85 } else if (JA.getType() == types::TY_PP_Asm) { 86 CmdArgs.push_back("-S"); 87 } else { 88 assert(false && "Unexpected output type!"); 89 } 90 } else if (isa<AssembleJobAction>(JA)) { 91 CmdArgs.push_back("-emit-obj"); 92 } else { 93 assert(false && "Unexpected action class for Flang tool."); 94 } 95 96 const InputInfo &Input = Inputs[0]; 97 types::ID InputType = Input.getType(); 98 99 // Add preprocessing options like -I, -D, etc. if we are using the 100 // preprocessor (i.e. skip when dealing with e.g. binary files). 101 if (types::getPreprocessedType(InputType) != types::TY_INVALID) 102 AddPreprocessingOptions(Args, CmdArgs); 103 104 AddFortranDialectOptions(Args, CmdArgs); 105 106 // Add other compile options 107 AddOtherOptions(Args, CmdArgs); 108 109 // Forward -Xflang arguments to -fc1 110 Args.AddAllArgValues(CmdArgs, options::OPT_Xflang); 111 112 if (Output.isFilename()) { 113 CmdArgs.push_back("-o"); 114 CmdArgs.push_back(Output.getFilename()); 115 } else { 116 assert(Output.isNothing() && "Invalid output."); 117 } 118 119 assert(Input.isFilename() && "Invalid input."); 120 CmdArgs.push_back(Input.getFilename()); 121 122 const auto& D = C.getDriver(); 123 // TODO: Replace flang-new with flang once the new driver replaces the 124 // throwaway driver 125 const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC)); 126 C.addCommand(std::make_unique<Command>(JA, *this, 127 ResponseFileSupport::AtFileUTF8(), 128 Exec, CmdArgs, Inputs, Output)); 129 } 130 131 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {} 132 133 Flang::~Flang() {} 134