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