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