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