Coverage Report

Created: 2021-01-19 06:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/AVR.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- AVR.cpp - AVR 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
#include "AVR.h"
10
#include "CommonArgs.h"
11
#include "InputInfo.h"
12
#include "clang/Driver/Compilation.h"
13
#include "clang/Driver/DriverDiagnostic.h"
14
#include "clang/Driver/Options.h"
15
#include "llvm/ADT/Optional.h"
16
#include "llvm/ADT/StringExtras.h"
17
#include "llvm/ADT/StringSwitch.h"
18
#include "llvm/MC/MCSubtargetInfo.h"
19
#include "llvm/MC/SubtargetFeature.h"
20
#include "llvm/Option/ArgList.h"
21
#include "llvm/Support/FileSystem.h"
22
23
using namespace clang::driver;
24
using namespace clang::driver::toolchains;
25
using namespace clang::driver::tools;
26
using namespace clang;
27
using namespace llvm::opt;
28
29
namespace {
30
31
const struct {
32
  StringRef Name;
33
  std::string SubPath;
34
  StringRef Family;
35
} MCUInfo[] = {
36
    {"at90s1200", "", "avr1"},
37
    {"attiny11", "", "avr1"},
38
    {"attiny12", "", "avr1"},
39
    {"attiny15", "", "avr1"},
40
    {"attiny28", "", "avr1"},
41
    {"at90s2313", "tiny-stack", "avr2"},
42
    {"at90s2323", "tiny-stack", "avr2"},
43
    {"at90s2333", "tiny-stack", "avr2"},
44
    {"at90s2343", "tiny-stack", "avr2"},
45
    {"at90s4433", "tiny-stack", "avr2"},
46
    {"attiny22", "tiny-stack", "avr2"},
47
    {"attiny26", "tiny-stack", "avr2"},
48
    {"at90s4414", "", "avr2"},
49
    {"at90s4434", "", "avr2"},
50
    {"at90s8515", "", "avr2"},
51
    {"at90c8534", "", "avr2"},
52
    {"at90s8535", "", "avr2"},
53
    {"attiny13", "avr25/tiny-stack", "avr25"},
54
    {"attiny13a", "avr25/tiny-stack", "avr25"},
55
    {"attiny2313", "avr25/tiny-stack", "avr25"},
56
    {"attiny2313a", "avr25/tiny-stack", "avr25"},
57
    {"attiny24", "avr25/tiny-stack", "avr25"},
58
    {"attiny24a", "avr25/tiny-stack", "avr25"},
59
    {"attiny25", "avr25/tiny-stack", "avr25"},
60
    {"attiny261", "avr25/tiny-stack", "avr25"},
61
    {"attiny261a", "avr25/tiny-stack", "avr25"},
62
    {"at86rf401", "avr25", "avr25"},
63
    {"ata5272", "avr25", "avr25"},
64
    {"attiny4313", "avr25", "avr25"},
65
    {"attiny44", "avr25", "avr25"},
66
    {"attiny44a", "avr25", "avr25"},
67
    {"attiny84", "avr25", "avr25"},
68
    {"attiny84a", "avr25", "avr25"},
69
    {"attiny45", "avr25", "avr25"},
70
    {"attiny85", "avr25", "avr25"},
71
    {"attiny441", "avr25", "avr25"},
72
    {"attiny461", "avr25", "avr25"},
73
    {"attiny461a", "avr25", "avr25"},
74
    {"attiny841", "avr25", "avr25"},
75
    {"attiny861", "avr25", "avr25"},
76
    {"attiny861a", "avr25", "avr25"},
77
    {"attiny87", "avr25", "avr25"},
78
    {"attiny43u", "avr25", "avr25"},
79
    {"attiny48", "avr25", "avr25"},
80
    {"attiny88", "avr25", "avr25"},
81
    {"attiny828", "avr25", "avr25"},
82
    {"at43usb355", "avr3", "avr3"},
83
    {"at76c711", "avr3", "avr3"},
84
    {"atmega103", "avr31", "avr31"},
85
    {"at43usb320", "avr31", "avr31"},
86
    {"attiny167", "avr35", "avr35"},
87
    {"at90usb82", "avr35", "avr35"},
88
    {"at90usb162", "avr35", "avr35"},
89
    {"ata5505", "avr35", "avr35"},
90
    {"atmega8u2", "avr35", "avr35"},
91
    {"atmega16u2", "avr35", "avr35"},
92
    {"atmega32u2", "avr35", "avr35"},
93
    {"attiny1634", "avr35", "avr35"},
94
    {"atmega8", "avr4", "avr4"},
95
    {"ata6289", "avr4", "avr4"},
96
    {"atmega8a", "avr4", "avr4"},
97
    {"ata6285", "avr4", "avr4"},
98
    {"ata6286", "avr4", "avr4"},
99
    {"atmega48", "avr4", "avr4"},
100
    {"atmega48a", "avr4", "avr4"},
101
    {"atmega48pa", "avr4", "avr4"},
102
    {"atmega48pb", "avr4", "avr4"},
103
    {"atmega48p", "avr4", "avr4"},
104
    {"atmega88", "avr4", "avr4"},
105
    {"atmega88a", "avr4", "avr4"},
106
    {"atmega88p", "avr4", "avr4"},
107
    {"atmega88pa", "avr4", "avr4"},
108
    {"atmega88pb", "avr4", "avr4"},
109
    {"atmega8515", "avr4", "avr4"},
110
    {"atmega8535", "avr4", "avr4"},
111
    {"atmega8hva", "avr4", "avr4"},
112
    {"at90pwm1", "avr4", "avr4"},
113
    {"at90pwm2", "avr4", "avr4"},
114
    {"at90pwm2b", "avr4", "avr4"},
115
    {"at90pwm3", "avr4", "avr4"},
116
    {"at90pwm3b", "avr4", "avr4"},
117
    {"at90pwm81", "avr4", "avr4"},
118
    {"ata5790", "avr5", "avr5"},
119
    {"ata5795", "avr5", "avr5"},
120
    {"atmega16", "avr5", "avr5"},
121
    {"atmega16a", "avr5", "avr5"},
122
    {"atmega161", "avr5", "avr5"},
123
    {"atmega162", "avr5", "avr5"},
124
    {"atmega163", "avr5", "avr5"},
125
    {"atmega164a", "avr5", "avr5"},
126
    {"atmega164p", "avr5", "avr5"},
127
    {"atmega164pa", "avr5", "avr5"},
128
    {"atmega165", "avr5", "avr5"},
129
    {"atmega165a", "avr5", "avr5"},
130
    {"atmega165p", "avr5", "avr5"},
131
    {"atmega165pa", "avr5", "avr5"},
132
    {"atmega168", "avr5", "avr5"},
133
    {"atmega168a", "avr5", "avr5"},
134
    {"atmega168p", "avr5", "avr5"},
135
    {"atmega168pa", "avr5", "avr5"},
136
    {"atmega168pb", "avr5", "avr5"},
137
    {"atmega169", "avr5", "avr5"},
138
    {"atmega169a", "avr5", "avr5"},
139
    {"atmega169p", "avr5", "avr5"},
140
    {"atmega169pa", "avr5", "avr5"},
141
    {"atmega32", "avr5", "avr5"},
142
    {"atmega32a", "avr5", "avr5"},
143
    {"atmega323", "avr5", "avr5"},
144
    {"atmega324a", "avr5", "avr5"},
145
    {"atmega324p", "avr5", "avr5"},
146
    {"atmega324pa", "avr5", "avr5"},
147
    {"atmega325", "avr5", "avr5"},
148
    {"atmega325a", "avr5", "avr5"},
149
    {"atmega325p", "avr5", "avr5"},
150
    {"atmega325pa", "avr5", "avr5"},
151
    {"atmega3250", "avr5", "avr5"},
152
    {"atmega3250a", "avr5", "avr5"},
153
    {"atmega3250p", "avr5", "avr5"},
154
    {"atmega3250pa", "avr5", "avr5"},
155
    {"atmega328", "avr5", "avr5"},
156
    {"atmega328p", "avr5", "avr5"},
157
    {"atmega329", "avr5", "avr5"},
158
    {"atmega329a", "avr5", "avr5"},
159
    {"atmega329p", "avr5", "avr5"},
160
    {"atmega329pa", "avr5", "avr5"},
161
    {"atmega3290", "avr5", "avr5"},
162
    {"atmega3290a", "avr5", "avr5"},
163
    {"atmega3290p", "avr5", "avr5"},
164
    {"atmega3290pa", "avr5", "avr5"},
165
    {"atmega406", "avr5", "avr5"},
166
    {"atmega64", "avr5", "avr5"},
167
    {"atmega64a", "avr5", "avr5"},
168
    {"atmega640", "avr5", "avr5"},
169
    {"atmega644", "avr5", "avr5"},
170
    {"atmega644a", "avr5", "avr5"},
171
    {"atmega644p", "avr5", "avr5"},
172
    {"atmega644pa", "avr5", "avr5"},
173
    {"atmega645", "avr5", "avr5"},
174
    {"atmega645a", "avr5", "avr5"},
175
    {"atmega645p", "avr5", "avr5"},
176
    {"atmega649", "avr5", "avr5"},
177
    {"atmega649a", "avr5", "avr5"},
178
    {"atmega649p", "avr5", "avr5"},
179
    {"atmega6450", "avr5", "avr5"},
180
    {"atmega6450a", "avr5", "avr5"},
181
    {"atmega6450p", "avr5", "avr5"},
182
    {"atmega6490", "avr5", "avr5"},
183
    {"atmega6490a", "avr5", "avr5"},
184
    {"atmega6490p", "avr5", "avr5"},
185
    {"atmega64rfr2", "avr5", "avr5"},
186
    {"atmega644rfr2", "avr5", "avr5"},
187
    {"atmega16hva", "avr5", "avr5"},
188
    {"atmega16hva2", "avr5", "avr5"},
189
    {"atmega16hvb", "avr5", "avr5"},
190
    {"atmega16hvbrevb", "avr5", "avr5"},
191
    {"atmega32hvb", "avr5", "avr5"},
192
    {"atmega32hvbrevb", "avr5", "avr5"},
193
    {"atmega64hve", "avr5", "avr5"},
194
    {"at90can32", "avr5", "avr5"},
195
    {"at90can64", "avr5", "avr5"},
196
    {"at90pwm161", "avr5", "avr5"},
197
    {"at90pwm216", "avr5", "avr5"},
198
    {"at90pwm316", "avr5", "avr5"},
199
    {"atmega32c1", "avr5", "avr5"},
200
    {"atmega64c1", "avr5", "avr5"},
201
    {"atmega16m1", "avr5", "avr5"},
202
    {"atmega32m1", "avr5", "avr5"},
203
    {"atmega64m1", "avr5", "avr5"},
204
    {"atmega16u4", "avr5", "avr5"},
205
    {"atmega32u4", "avr5", "avr5"},
206
    {"atmega32u6", "avr5", "avr5"},
207
    {"at90usb646", "avr5", "avr5"},
208
    {"at90usb647", "avr5", "avr5"},
209
    {"at90scr100", "avr5", "avr5"},
210
    {"at94k", "avr5", "avr5"},
211
    {"m3000", "avr5", "avr5"},
212
    {"atmega128", "avr51", "avr51"},
213
    {"atmega128a", "avr51", "avr51"},
214
    {"atmega1280", "avr51", "avr51"},
215
    {"atmega1281", "avr51", "avr51"},
216
    {"atmega1284", "avr51", "avr51"},
217
    {"atmega1284p", "avr51", "avr51"},
218
    {"atmega128rfa1", "avr51", "avr51"},
219
    {"atmega128rfr2", "avr51", "avr51"},
220
    {"atmega1284rfr2", "avr51", "avr51"},
221
    {"at90can128", "avr51", "avr51"},
222
    {"at90usb1286", "avr51", "avr51"},
223
    {"at90usb1287", "avr51", "avr51"},
224
    {"atmega2560", "avr6", "avr6"},
225
    {"atmega2561", "avr6", "avr6"},
226
    {"atmega256rfr2", "avr6", "avr6"},
227
    {"atmega2564rfr2", "avr6", "avr6"},
228
    {"attiny4", "avrtiny", "avrtiny"},
229
    {"attiny5", "avrtiny", "avrtiny"},
230
    {"attiny9", "avrtiny", "avrtiny"},
231
    {"attiny10", "avrtiny", "avrtiny"},
232
    {"attiny20", "avrtiny", "avrtiny"},
233
    {"attiny40", "avrtiny", "avrtiny"},
234
    {"atxmega16a4", "avrxmega2", "avrxmega2"},
235
    {"atxmega16a4u", "avrxmega2", "avrxmega2"},
236
    {"atxmega16c4", "avrxmega2", "avrxmega2"},
237
    {"atxmega16d4", "avrxmega2", "avrxmega2"},
238
    {"atxmega32a4", "avrxmega2", "avrxmega2"},
239
    {"atxmega32a4u", "avrxmega2", "avrxmega2"},
240
    {"atxmega32c4", "avrxmega2", "avrxmega2"},
241
    {"atxmega32d4", "avrxmega2", "avrxmega2"},
242
    {"atxmega32e5", "avrxmega2", "avrxmega2"},
243
    {"atxmega16e5", "avrxmega2", "avrxmega2"},
244
    {"atxmega8e5", "avrxmega2", "avrxmega2"},
245
    {"atxmega64a3u", "avrxmega4", "avrxmega4"},
246
    {"atxmega64a4u", "avrxmega4", "avrxmega4"},
247
    {"atxmega64b1", "avrxmega4", "avrxmega4"},
248
    {"atxmega64b3", "avrxmega4", "avrxmega4"},
249
    {"atxmega64c3", "avrxmega4", "avrxmega4"},
250
    {"atxmega64d3", "avrxmega4", "avrxmega4"},
251
    {"atxmega64d4", "avrxmega4", "avrxmega4"},
252
    {"atxmega64a1", "avrxmega5", "avrxmega5"},
253
    {"atxmega64a1u", "avrxmega5", "avrxmega5"},
254
    {"atxmega128a3", "avrxmega6", "avrxmega6"},
255
    {"atxmega128a3u", "avrxmega6", "avrxmega6"},
256
    {"atxmega128b1", "avrxmega6", "avrxmega6"},
257
    {"atxmega128b3", "avrxmega6", "avrxmega6"},
258
    {"atxmega128c3", "avrxmega6", "avrxmega6"},
259
    {"atxmega128d3", "avrxmega6", "avrxmega6"},
260
    {"atxmega128d4", "avrxmega6", "avrxmega6"},
261
    {"atxmega192a3", "avrxmega6", "avrxmega6"},
262
    {"atxmega192a3u", "avrxmega6", "avrxmega6"},
263
    {"atxmega192c3", "avrxmega6", "avrxmega6"},
264
    {"atxmega192d3", "avrxmega6", "avrxmega6"},
265
    {"atxmega256a3", "avrxmega6", "avrxmega6"},
266
    {"atxmega256a3u", "avrxmega6", "avrxmega6"},
267
    {"atxmega256a3b", "avrxmega6", "avrxmega6"},
268
    {"atxmega256a3bu", "avrxmega6", "avrxmega6"},
269
    {"atxmega256c3", "avrxmega6", "avrxmega6"},
270
    {"atxmega256d3", "avrxmega6", "avrxmega6"},
271
    {"atxmega384c3", "avrxmega6", "avrxmega6"},
272
    {"atxmega384d3", "avrxmega6", "avrxmega6"},
273
    {"atxmega128a1", "avrxmega7", "avrxmega7"},
274
    {"atxmega128a1u", "avrxmega7", "avrxmega7"},
275
    {"atxmega128a4u", "avrxmega7", "avrxmega7"},
276
};
277
278
0
std::string GetMCUSubPath(StringRef MCUName) {
279
0
  for (const auto &MCU : MCUInfo)
280
0
    if (MCU.Name == MCUName)
281
0
      return std::string(MCU.SubPath);
282
0
  return "";
283
0
}
284
285
50
llvm::Optional<StringRef> GetMCUFamilyName(StringRef MCUName) {
286
50
  for (const auto &MCU : MCUInfo)
287
6.29k
    if (MCU.Name == MCUName)
288
44
      return Optional<StringRef>(MCU.Family);
289
6
  return Optional<StringRef>();
290
50
}
291
292
27
llvm::Optional<unsigned> GetMCUSectionAddressData(StringRef MCU) {
293
27
  return llvm::StringSwitch<llvm::Optional<unsigned>>(MCU)
294
27
      .Case("atmega328", Optional<unsigned>(0x800100))
295
27
      .Case("atmega328p", Optional<unsigned>(0x800100))
296
27
      .Default(Optional<unsigned>());
297
27
}
298
299
const StringRef PossibleAVRLibcLocations[] = {
300
    "/usr/avr",
301
    "/usr/lib/avr",
302
};
303
304
} // end anonymous namespace
305
306
/// AVR Toolchain
307
AVRToolChain::AVRToolChain(const Driver &D, const llvm::Triple &Triple,
308
                           const ArgList &Args)
309
28
    : Generic_ELF(D, Triple, Args), LinkStdlib(false) {
310
28
  GCCInstallation.init(Triple, Args);
311
312
  // Only add default libraries if the user hasn't explicitly opted out.
313
28
  if (!Args.hasArg(options::OPT_nostdlib) &&
314
27
      !Args.hasArg(options::OPT_nodefaultlibs) &&
315
26
      !Args.hasArg(options::OPT_c /* does not apply when not linking */)) {
316
25
    std::string CPU = getCPUName(Args, Triple);
317
318
25
    if (CPU.empty()) {
319
      // We cannot link any standard libraries without an MCU specified.
320
2
      D.Diag(diag::warn_drv_avr_mcu_not_specified);
321
23
    } else {
322
23
      Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
323
23
      Optional<std::string> AVRLibcRoot = findAVRLibcInstallation();
324
325
23
      if (!FamilyName.hasValue()) {
326
        // We do not have an entry for this CPU in the family
327
        // mapping table yet.
328
2
        D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented)
329
2
            << CPU;
330
21
      } else if (!GCCInstallation.isValid()) {
331
        // No avr-gcc found and so no runtime linked.
332
21
        D.Diag(diag::warn_drv_avr_gcc_not_found);
333
0
      } else if (!AVRLibcRoot.hasValue()) {
334
        // No avr-libc found and so no runtime linked.
335
0
        D.Diag(diag::warn_drv_avr_libc_not_found);
336
0
      } else { // We have enough information to link stdlibs
337
0
        std::string GCCRoot = std::string(GCCInstallation.getInstallPath());
338
0
        std::string LibcRoot = AVRLibcRoot.getValue();
339
0
        std::string SubPath = GetMCUSubPath(CPU);
340
341
0
        getFilePaths().push_back(LibcRoot + std::string("/lib/") + SubPath);
342
0
        getFilePaths().push_back(GCCRoot + std::string("/") + SubPath);
343
344
0
        LinkStdlib = true;
345
0
      }
346
23
    }
347
348
25
    if (!LinkStdlib)
349
25
      D.Diag(diag::warn_drv_avr_stdlib_not_linked);
350
25
  }
351
28
}
352
353
27
Tool *AVRToolChain::buildLinker() const {
354
27
  return new tools::AVR::Linker(getTriple(), *this, LinkStdlib);
355
27
}
356
357
void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
358
                               const InputInfo &Output,
359
                               const InputInfoList &Inputs, const ArgList &Args,
360
27
                               const char *LinkingOutput) const {
361
  // Compute information about the target AVR.
362
27
  std::string CPU = getCPUName(Args, getToolChain().getTriple());
363
27
  llvm::Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
364
27
  llvm::Optional<unsigned> SectionAddressData = GetMCUSectionAddressData(CPU);
365
366
27
  std::string Linker = getToolChain().GetProgramPath(getShortName());
367
27
  ArgStringList CmdArgs;
368
27
  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
369
370
27
  CmdArgs.push_back("-o");
371
27
  CmdArgs.push_back(Output.getFilename());
372
373
  // Enable garbage collection of unused sections.
374
27
  CmdArgs.push_back("--gc-sections");
375
376
  // Add library search paths before we specify libraries.
377
27
  Args.AddAllArgs(CmdArgs, options::OPT_L);
378
27
  getToolChain().AddFilePathLibArgs(Args, CmdArgs);
379
380
27
  if (SectionAddressData.hasValue()) {
381
3
    std::string DataSectionArg = std::string("-Tdata=0x") +
382
3
                                 llvm::utohexstr(SectionAddressData.getValue());
383
3
    CmdArgs.push_back(Args.MakeArgString(DataSectionArg));
384
24
  } else {
385
    // We do not have an entry for this CPU in the address mapping table yet.
386
24
    getToolChain().getDriver().Diag(
387
24
        diag::warn_drv_avr_linker_section_addresses_not_implemented)
388
24
        << CPU;
389
24
  }
390
391
  // If the family name is known, we can link with the device-specific libgcc.
392
  // Without it, libgcc will simply not be linked. This matches avr-gcc
393
  // behavior.
394
27
  if (LinkStdlib) {
395
0
    assert(!CPU.empty() && "CPU name must be known in order to link stdlibs");
396
397
    // Add the object file for the CRT.
398
0
    std::string CrtFileName = std::string("-l:crt") + CPU + std::string(".o");
399
0
    CmdArgs.push_back(Args.MakeArgString(CrtFileName));
400
401
0
    CmdArgs.push_back("-lgcc");
402
0
    CmdArgs.push_back("-lm");
403
0
    CmdArgs.push_back("-lc");
404
405
    // Add the link library specific to the MCU.
406
0
    CmdArgs.push_back(Args.MakeArgString(std::string("-l") + CPU));
407
408
    // Specify the family name as the emulation mode to use.
409
    // This is almost always required because otherwise avr-ld
410
    // will assume 'avr2' and warn about the program being larger
411
    // than the bare minimum supports.
412
0
    CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
413
0
  }
414
415
27
  C.addCommand(std::make_unique<Command>(
416
27
      JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
417
27
      CmdArgs, Inputs, Output));
418
27
}
419
420
23
llvm::Optional<std::string> AVRToolChain::findAVRLibcInstallation() const {
421
46
  for (StringRef PossiblePath : PossibleAVRLibcLocations) {
422
    // Return the first avr-libc installation that exists.
423
46
    if (llvm::sys::fs::is_directory(PossiblePath))
424
0
      return Optional<std::string>(std::string(PossiblePath));
425
46
  }
426
427
23
  return llvm::None;
428
23
}