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