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                 options::OPT_fno_automatic});
37 }
38 
39 void Flang::AddPreprocessingOptions(const ArgList &Args,
40                                     ArgStringList &CmdArgs) const {
41   Args.AddAllArgs(CmdArgs,
42                   {options::OPT_P, options::OPT_D, options::OPT_U,
43                    options::OPT_I, options::OPT_cpp, options::OPT_nocpp});
44 }
45 
46 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
47   Args.AddAllArgs(CmdArgs,
48                   {options::OPT_module_dir, options::OPT_fdebug_module_writer,
49                    options::OPT_fintrinsic_modules_path, options::OPT_pedantic,
50                    options::OPT_std_EQ, options::OPT_W_Joined});
51 }
52 
53 void Flang::ConstructJob(Compilation &C, const JobAction &JA,
54                          const InputInfo &Output, const InputInfoList &Inputs,
55                          const ArgList &Args, const char *LinkingOutput) const {
56   const auto &TC = getToolChain();
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   // Add the "effective" target triple.
66   CmdArgs.push_back("-triple");
67   CmdArgs.push_back(Args.MakeArgString(TripleStr));
68 
69   if (isa<PreprocessJobAction>(JA)) {
70       CmdArgs.push_back("-E");
71   } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
72     if (JA.getType() == types::TY_Nothing) {
73       CmdArgs.push_back("-fsyntax-only");
74     } else if (JA.getType() == types::TY_AST) {
75       CmdArgs.push_back("-emit-ast");
76     } else if (JA.getType() == types::TY_LLVM_IR ||
77                JA.getType() == types::TY_LTO_IR) {
78       CmdArgs.push_back("-emit-llvm");
79     } else if (JA.getType() == types::TY_LLVM_BC ||
80                JA.getType() == types::TY_LTO_BC) {
81       CmdArgs.push_back("-emit-llvm-bc");
82     } else if (JA.getType() == types::TY_PP_Asm) {
83       CmdArgs.push_back("-S");
84     } else {
85       assert(false && "Unexpected output type!");
86     }
87   } else if (isa<AssembleJobAction>(JA)) {
88     CmdArgs.push_back("-emit-obj");
89   } else {
90     assert(false && "Unexpected action class for Flang tool.");
91   }
92 
93   const InputInfo &Input = Inputs[0];
94   types::ID InputType = Input.getType();
95 
96   // Add preprocessing options like -I, -D, etc. if we are using the
97   // preprocessor (i.e. skip when dealing with e.g. binary files).
98   if (types::getPreprocessedType(InputType) != types::TY_INVALID)
99     AddPreprocessingOptions(Args, CmdArgs);
100 
101   AddFortranDialectOptions(Args, CmdArgs);
102 
103   // Add other compile options
104   AddOtherOptions(Args, CmdArgs);
105 
106   // Forward -Xflang arguments to -fc1
107   Args.AddAllArgValues(CmdArgs, options::OPT_Xflang);
108 
109   // Forward -mllvm options to the LLVM option parser. In practice, this means
110   // forwarding to `-fc1` as that's where the LLVM parser is run.
111   for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
112     A->claim();
113     A->render(Args, CmdArgs);
114   }
115 
116   for (const Arg *A : Args.filtered(options::OPT_mmlir)) {
117     A->claim();
118     A->render(Args, CmdArgs);
119   }
120 
121   if (Output.isFilename()) {
122     CmdArgs.push_back("-o");
123     CmdArgs.push_back(Output.getFilename());
124   } else {
125     assert(Output.isNothing() && "Invalid output.");
126   }
127 
128   assert(Input.isFilename() && "Invalid input.");
129   CmdArgs.push_back(Input.getFilename());
130 
131   const auto& D = C.getDriver();
132   // TODO: Replace flang-new with flang once the new driver replaces the
133   // throwaway driver
134   const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC));
135   C.addCommand(std::make_unique<Command>(JA, *this,
136                                          ResponseFileSupport::AtFileUTF8(),
137                                          Exec, CmdArgs, Inputs, Output));
138 }
139 
140 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {}
141 
142 Flang::~Flang() {}
143