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