aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
blob: 856ad58f3bd9db8e4f620723eea90ace8b437691 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//===--- LoongArch.cpp - LoongArch Helpers for Tools ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "LoongArch.h"
#include "ToolChains/CommonArgs.h"
#include "clang/Basic/DiagnosticDriver.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Options.h"
#include "llvm/TargetParser/LoongArchTargetParser.h"

using namespace clang::driver;
using namespace clang::driver::tools;
using namespace clang;
using namespace llvm::opt;

StringRef loongarch::getLoongArchABI(const Driver &D, const ArgList &Args,
                                     const llvm::Triple &Triple) {
  assert((Triple.getArch() == llvm::Triple::loongarch32 ||
          Triple.getArch() == llvm::Triple::loongarch64) &&
         "Unexpected triple");
  bool IsLA32 = Triple.getArch() == llvm::Triple::loongarch32;

  // Record -mabi value for later use.
  const Arg *MABIArg = Args.getLastArg(options::OPT_mabi_EQ);
  StringRef MABIValue;
  if (MABIArg) {
    MABIValue = MABIArg->getValue();
  }

  // Parse -mfpu value for later use.
  const Arg *MFPUArg = Args.getLastArg(options::OPT_mfpu_EQ);
  int FPU = -1;
  if (MFPUArg) {
    StringRef V = MFPUArg->getValue();
    if (V == "64")
      FPU = 64;
    else if (V == "32")
      FPU = 32;
    else if (V == "0" || V == "none")
      FPU = 0;
    else
      D.Diag(diag::err_drv_loongarch_invalid_mfpu_EQ) << V;
  }

  // Check -m*-float firstly since they have highest priority.
  if (const Arg *A = Args.getLastArg(options::OPT_mdouble_float,
                                     options::OPT_msingle_float,
                                     options::OPT_msoft_float)) {
    StringRef ImpliedABI;
    int ImpliedFPU = -1;
    if (A->getOption().matches(options::OPT_mdouble_float)) {
      ImpliedABI = IsLA32 ? "ilp32d" : "lp64d";
      ImpliedFPU = 64;
    }
    if (A->getOption().matches(options::OPT_msingle_float)) {
      ImpliedABI = IsLA32 ? "ilp32f" : "lp64f";
      ImpliedFPU = 32;
    }
    if (A->getOption().matches(options::OPT_msoft_float)) {
      ImpliedABI = IsLA32 ? "ilp32s" : "lp64s";
      ImpliedFPU = 0;
    }

    // Check `-mabi=` and `-mfpu=` settings and report if they conflict with
    // the higher-priority settings implied by -m*-float.
    //
    // ImpliedABI and ImpliedFPU are guaranteed to have valid values because
    // one of the match arms must match if execution can arrive here at all.
    if (!MABIValue.empty() && ImpliedABI != MABIValue)
      D.Diag(diag::warn_drv_loongarch_conflicting_implied_val)
          << MABIArg->getAsString(Args) << A->getAsString(Args) << ImpliedABI;

    if (FPU != -1 && ImpliedFPU != FPU)
      D.Diag(diag::warn_drv_loongarch_conflicting_implied_val)
          << MFPUArg->getAsString(Args) << A->getAsString(Args) << ImpliedFPU;

    return ImpliedABI;
  }

  // If `-mabi=` is specified, use it.
  if (!MABIValue.empty())
    return MABIValue;

  // Select abi based on -mfpu=xx.
  switch (FPU) {
  case 64:
    return IsLA32 ? "ilp32d" : "lp64d";
  case 32:
    return IsLA32 ? "ilp32f" : "lp64f";
  case 0:
    return IsLA32 ? "ilp32s" : "lp64s";
  }

  // Choose a default based on the triple.
  // Honor the explicit ABI modifier suffix in triple's environment part if
  // present, falling back to {ILP32,LP64}D otherwise.
  switch (Triple.getEnvironment()) {
  case llvm::Triple::GNUSF:
    return IsLA32 ? "ilp32s" : "lp64s";
  case llvm::Triple::GNUF32:
    return IsLA32 ? "ilp32f" : "lp64f";
  case llvm::Triple::GNUF64:
    // This was originally permitted (and indeed the canonical way) to
    // represent the {ILP32,LP64}D ABIs, but in Feb 2023 Loongson decided to
    // drop the explicit suffix in favor of unmarked `-gnu` for the
    // "general-purpose" ABIs, among other non-technical reasons.
    //
    // The spec change did not mention whether existing usages of "gnuf64"
    // shall remain valid or not, so we are going to continue recognizing it
    // for some time, until it is clear that everyone else has migrated away
    // from it.
    [[fallthrough]];
  case llvm::Triple::GNU:
  default:
    return IsLA32 ? "ilp32d" : "lp64d";
  }
}

void loongarch::getLoongArchTargetFeatures(const Driver &D,
                                           const llvm::Triple &Triple,
                                           const ArgList &Args,
                                           std::vector<StringRef> &Features) {
  StringRef ArchName;
  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
    if (!llvm::LoongArch::isValidArchName(A->getValue())) {
      D.Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args);
      return;
    }
    ArchName = A->getValue();
  }

  // TODO: handle -march=native and -mtune=xx.

  // Select a default arch name.
  if (ArchName.empty() && Triple.isLoongArch64())
    ArchName = "loongarch64";

  if (!ArchName.empty())
    llvm::LoongArch::getArchFeatures(ArchName, Features);

  // Select floating-point features determined by -mdouble-float,
  // -msingle-float, -msoft-float and -mfpu.
  // Note: -m*-float wins any other options.
  if (const Arg *A = Args.getLastArg(options::OPT_mdouble_float,
                                     options::OPT_msingle_float,
                                     options::OPT_msoft_float)) {
    if (A->getOption().matches(options::OPT_mdouble_float)) {
      Features.push_back("+f");
      Features.push_back("+d");
    } else if (A->getOption().matches(options::OPT_msingle_float)) {
      Features.push_back("+f");
      Features.push_back("-d");
    } else /*Soft-float*/ {
      Features.push_back("-f");
      Features.push_back("-d");
    }
  } else if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
    StringRef FPU = A->getValue();
    if (FPU == "64") {
      Features.push_back("+f");
      Features.push_back("+d");
    } else if (FPU == "32") {
      Features.push_back("+f");
      Features.push_back("-d");
    } else if (FPU == "0" || FPU == "none") {
      Features.push_back("-f");
      Features.push_back("-d");
    } else {
      D.Diag(diag::err_drv_loongarch_invalid_mfpu_EQ) << FPU;
    }
  }

  // Select the `ual` feature determined by -m[no-]unaligned-access
  // or the alias -m[no-]strict-align.
  AddTargetFeature(Args, Features, options::OPT_munaligned_access,
                   options::OPT_mno_unaligned_access, "ual");

  // Accept but warn about these TargetSpecific options.
  if (Arg *A = Args.getLastArgNoClaim(options::OPT_mabi_EQ))
    A->ignoreTargetSpecific();
  if (Arg *A = Args.getLastArgNoClaim(options::OPT_mfpu_EQ))
    A->ignoreTargetSpecific();
}