1132451Sroberto//===--- AVR.cpp - Implement AVR target feature support -------------------===//
2132451Sroberto//
3132451Sroberto// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4132451Sroberto// See https://llvm.org/LICENSE.txt for license information.
5132451Sroberto// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6132451Sroberto//
7132451Sroberto//===----------------------------------------------------------------------===//
8132451Sroberto//
9132451Sroberto// This file implements AVR TargetInfo objects.
10132451Sroberto//
11132451Sroberto//===----------------------------------------------------------------------===//
12132451Sroberto
13132451Sroberto#include "AVR.h"
14132451Sroberto#include "clang/Basic/MacroBuilder.h"
15132451Sroberto#include "llvm/ADT/StringSwitch.h"
16132451Sroberto
17132451Srobertousing namespace clang;
18132451Srobertousing namespace clang::targets;
19132451Sroberto
20132451Srobertonamespace clang {
21132451Srobertonamespace targets {
22132451Sroberto
23132451Sroberto/// Information about a specific microcontroller.
24132451Srobertostruct LLVM_LIBRARY_VISIBILITY MCUInfo {
25132451Sroberto  const char *Name;
26132451Sroberto  const char *DefineName;
27132451Sroberto};
28132451Sroberto
29132451Sroberto// This list should be kept up-to-date with AVRDevices.td in LLVM.
30132451Srobertostatic MCUInfo AVRMcus[] = {
31132451Sroberto    {"at90s1200", "__AVR_AT90S1200__"},
32132451Sroberto    {"attiny11", "__AVR_ATtiny11__"},
33132451Sroberto    {"attiny12", "__AVR_ATtiny12__"},
34132451Sroberto    {"attiny15", "__AVR_ATtiny15__"},
35132451Sroberto    {"attiny28", "__AVR_ATtiny28__"},
36132451Sroberto    {"at90s2313", "__AVR_AT90S2313__"},
37132451Sroberto    {"at90s2323", "__AVR_AT90S2323__"},
38132451Sroberto    {"at90s2333", "__AVR_AT90S2333__"},
39132451Sroberto    {"at90s2343", "__AVR_AT90S2343__"},
40132451Sroberto    {"attiny22", "__AVR_ATtiny22__"},
41132451Sroberto    {"attiny26", "__AVR_ATtiny26__"},
42132451Sroberto    {"at86rf401", "__AVR_AT86RF401__"},
43132451Sroberto    {"at90s4414", "__AVR_AT90S4414__"},
44132451Sroberto    {"at90s4433", "__AVR_AT90S4433__"},
45132451Sroberto    {"at90s4434", "__AVR_AT90S4434__"},
46132451Sroberto    {"at90s8515", "__AVR_AT90S8515__"},
47132451Sroberto    {"at90c8534", "__AVR_AT90c8534__"},
48132451Sroberto    {"at90s8535", "__AVR_AT90S8535__"},
49132451Sroberto    {"ata5272", "__AVR_ATA5272__"},
50132451Sroberto    {"attiny13", "__AVR_ATtiny13__"},
51132451Sroberto    {"attiny13a", "__AVR_ATtiny13A__"},
52132451Sroberto    {"attiny2313", "__AVR_ATtiny2313__"},
53132451Sroberto    {"attiny2313a", "__AVR_ATtiny2313A__"},
54132451Sroberto    {"attiny24", "__AVR_ATtiny24__"},
55132451Sroberto    {"attiny24a", "__AVR_ATtiny24A__"},
56132451Sroberto    {"attiny4313", "__AVR_ATtiny4313__"},
57132451Sroberto    {"attiny44", "__AVR_ATtiny44__"},
58132451Sroberto    {"attiny44a", "__AVR_ATtiny44A__"},
59132451Sroberto    {"attiny84", "__AVR_ATtiny84__"},
60132451Sroberto    {"attiny84a", "__AVR_ATtiny84A__"},
61132451Sroberto    {"attiny25", "__AVR_ATtiny25__"},
62132451Sroberto    {"attiny45", "__AVR_ATtiny45__"},
63132451Sroberto    {"attiny85", "__AVR_ATtiny85__"},
64132451Sroberto    {"attiny261", "__AVR_ATtiny261__"},
65132451Sroberto    {"attiny261a", "__AVR_ATtiny261A__"},
66132451Sroberto    {"attiny461", "__AVR_ATtiny461__"},
67132451Sroberto    {"attiny461a", "__AVR_ATtiny461A__"},
68132451Sroberto    {"attiny861", "__AVR_ATtiny861__"},
69132451Sroberto    {"attiny861a", "__AVR_ATtiny861A__"},
70132451Sroberto    {"attiny87", "__AVR_ATtiny87__"},
71132451Sroberto    {"attiny43u", "__AVR_ATtiny43U__"},
72132451Sroberto    {"attiny48", "__AVR_ATtiny48__"},
73132451Sroberto    {"attiny88", "__AVR_ATtiny88__"},
74132451Sroberto    {"attiny828", "__AVR_ATtiny828__"},
75132451Sroberto    {"at43usb355", "__AVR_AT43USB355__"},
76132451Sroberto    {"at76c711", "__AVR_AT76C711__"},
77132451Sroberto    {"atmega103", "__AVR_ATmega103__"},
78132451Sroberto    {"at43usb320", "__AVR_AT43USB320__"},
79132451Sroberto    {"attiny167", "__AVR_ATtiny167__"},
80132451Sroberto    {"at90usb82", "__AVR_AT90USB82__"},
81132451Sroberto    {"at90usb162", "__AVR_AT90USB162__"},
82132451Sroberto    {"ata5505", "__AVR_ATA5505__"},
83132451Sroberto    {"atmega8u2", "__AVR_ATmega8U2__"},
84132451Sroberto    {"atmega16u2", "__AVR_ATmega16U2__"},
85132451Sroberto    {"atmega32u2", "__AVR_ATmega32U2__"},
86132451Sroberto    {"attiny1634", "__AVR_ATtiny1634__"},
87132451Sroberto    {"atmega8", "__AVR_ATmega8__"},
88132451Sroberto    {"ata6289", "__AVR_ATA6289__"},
89132451Sroberto    {"atmega8a", "__AVR_ATmega8A__"},
90132451Sroberto    {"ata6285", "__AVR_ATA6285__"},
91132451Sroberto    {"ata6286", "__AVR_ATA6286__"},
92132451Sroberto    {"atmega48", "__AVR_ATmega48__"},
93132451Sroberto    {"atmega48a", "__AVR_ATmega48A__"},
94132451Sroberto    {"atmega48pa", "__AVR_ATmega48PA__"},
95132451Sroberto    {"atmega48p", "__AVR_ATmega48P__"},
96132451Sroberto    {"atmega88", "__AVR_ATmega88__"},
97132451Sroberto    {"atmega88a", "__AVR_ATmega88A__"},
98132451Sroberto    {"atmega88p", "__AVR_ATmega88P__"},
99132451Sroberto    {"atmega88pa", "__AVR_ATmega88PA__"},
100132451Sroberto    {"atmega8515", "__AVR_ATmega8515__"},
101132451Sroberto    {"atmega8535", "__AVR_ATmega8535__"},
102132451Sroberto    {"atmega8hva", "__AVR_ATmega8HVA__"},
103132451Sroberto    {"at90pwm1", "__AVR_AT90PWM1__"},
104132451Sroberto    {"at90pwm2", "__AVR_AT90PWM2__"},
105132451Sroberto    {"at90pwm2b", "__AVR_AT90PWM2B__"},
106132451Sroberto    {"at90pwm3", "__AVR_AT90PWM3__"},
107132451Sroberto    {"at90pwm3b", "__AVR_AT90PWM3B__"},
108132451Sroberto    {"at90pwm81", "__AVR_AT90PWM81__"},
109132451Sroberto    {"ata5790", "__AVR_ATA5790__"},
110132451Sroberto    {"ata5795", "__AVR_ATA5795__"},
111132451Sroberto    {"atmega16", "__AVR_ATmega16__"},
112132451Sroberto    {"atmega16a", "__AVR_ATmega16A__"},
113132451Sroberto    {"atmega161", "__AVR_ATmega161__"},
114132451Sroberto    {"atmega162", "__AVR_ATmega162__"},
115132451Sroberto    {"atmega163", "__AVR_ATmega163__"},
116132451Sroberto    {"atmega164a", "__AVR_ATmega164A__"},
117132451Sroberto    {"atmega164p", "__AVR_ATmega164P__"},
118132451Sroberto    {"atmega164pa", "__AVR_ATmega164PA__"},
119132451Sroberto    {"atmega165", "__AVR_ATmega165__"},
120132451Sroberto    {"atmega165a", "__AVR_ATmega165A__"},
121132451Sroberto    {"atmega165p", "__AVR_ATmega165P__"},
122132451Sroberto    {"atmega165pa", "__AVR_ATmega165PA__"},
123132451Sroberto    {"atmega168", "__AVR_ATmega168__"},
124132451Sroberto    {"atmega168a", "__AVR_ATmega168A__"},
125132451Sroberto    {"atmega168p", "__AVR_ATmega168P__"},
126132451Sroberto    {"atmega168pa", "__AVR_ATmega168PA__"},
127132451Sroberto    {"atmega169", "__AVR_ATmega169__"},
128132451Sroberto    {"atmega169a", "__AVR_ATmega169A__"},
129132451Sroberto    {"atmega169p", "__AVR_ATmega169P__"},
130132451Sroberto    {"atmega169pa", "__AVR_ATmega169PA__"},
131132451Sroberto    {"atmega32", "__AVR_ATmega32__"},
132132451Sroberto    {"atmega32a", "__AVR_ATmega32A__"},
133132451Sroberto    {"atmega323", "__AVR_ATmega323__"},
134132451Sroberto    {"atmega324a", "__AVR_ATmega324A__"},
135132451Sroberto    {"atmega324p", "__AVR_ATmega324P__"},
136132451Sroberto    {"atmega324pa", "__AVR_ATmega324PA__"},
137132451Sroberto    {"atmega325", "__AVR_ATmega325__"},
138132451Sroberto    {"atmega325a", "__AVR_ATmega325A__"},
139132451Sroberto    {"atmega325p", "__AVR_ATmega325P__"},
140132451Sroberto    {"atmega325pa", "__AVR_ATmega325PA__"},
141132451Sroberto    {"atmega3250", "__AVR_ATmega3250__"},
142132451Sroberto    {"atmega3250a", "__AVR_ATmega3250A__"},
143132451Sroberto    {"atmega3250p", "__AVR_ATmega3250P__"},
144132451Sroberto    {"atmega3250pa", "__AVR_ATmega3250PA__"},
145132451Sroberto    {"atmega328", "__AVR_ATmega328__"},
146132451Sroberto    {"atmega328p", "__AVR_ATmega328P__"},
147132451Sroberto    {"atmega329", "__AVR_ATmega329__"},
148132451Sroberto    {"atmega329a", "__AVR_ATmega329A__"},
149132451Sroberto    {"atmega329p", "__AVR_ATmega329P__"},
150132451Sroberto    {"atmega329pa", "__AVR_ATmega329PA__"},
151132451Sroberto    {"atmega3290", "__AVR_ATmega3290__"},
152132451Sroberto    {"atmega3290a", "__AVR_ATmega3290A__"},
153132451Sroberto    {"atmega3290p", "__AVR_ATmega3290P__"},
154132451Sroberto    {"atmega3290pa", "__AVR_ATmega3290PA__"},
155132451Sroberto    {"atmega406", "__AVR_ATmega406__"},
156132451Sroberto    {"atmega64", "__AVR_ATmega64__"},
157132451Sroberto    {"atmega64a", "__AVR_ATmega64A__"},
158132451Sroberto    {"atmega640", "__AVR_ATmega640__"},
159132451Sroberto    {"atmega644", "__AVR_ATmega644__"},
160132451Sroberto    {"atmega644a", "__AVR_ATmega644A__"},
161132451Sroberto    {"atmega644p", "__AVR_ATmega644P__"},
162132451Sroberto    {"atmega644pa", "__AVR_ATmega644PA__"},
163132451Sroberto    {"atmega645", "__AVR_ATmega645__"},
164132451Sroberto    {"atmega645a", "__AVR_ATmega645A__"},
165132451Sroberto    {"atmega645p", "__AVR_ATmega645P__"},
166132451Sroberto    {"atmega649", "__AVR_ATmega649__"},
167132451Sroberto    {"atmega649a", "__AVR_ATmega649A__"},
168132451Sroberto    {"atmega649p", "__AVR_ATmega649P__"},
169132451Sroberto    {"atmega6450", "__AVR_ATmega6450__"},
170132451Sroberto    {"atmega6450a", "__AVR_ATmega6450A__"},
171132451Sroberto    {"atmega6450p", "__AVR_ATmega6450P__"},
172132451Sroberto    {"atmega6490", "__AVR_ATmega6490__"},
173132451Sroberto    {"atmega6490a", "__AVR_ATmega6490A__"},
174132451Sroberto    {"atmega6490p", "__AVR_ATmega6490P__"},
175132451Sroberto    {"atmega64rfr2", "__AVR_ATmega64RFR2__"},
176132451Sroberto    {"atmega644rfr2", "__AVR_ATmega644RFR2__"},
177132451Sroberto    {"atmega16hva", "__AVR_ATmega16HVA__"},
178132451Sroberto    {"atmega16hva2", "__AVR_ATmega16HVA2__"},
179132451Sroberto    {"atmega16hvb", "__AVR_ATmega16HVB__"},
180132451Sroberto    {"atmega16hvbrevb", "__AVR_ATmega16HVBREVB__"},
181132451Sroberto    {"atmega32hvb", "__AVR_ATmega32HVB__"},
182132451Sroberto    {"atmega32hvbrevb", "__AVR_ATmega32HVBREVB__"},
183132451Sroberto    {"atmega64hve", "__AVR_ATmega64HVE__"},
184132451Sroberto    {"at90can32", "__AVR_AT90CAN32__"},
185132451Sroberto    {"at90can64", "__AVR_AT90CAN64__"},
186132451Sroberto    {"at90pwm161", "__AVR_AT90PWM161__"},
187132451Sroberto    {"at90pwm216", "__AVR_AT90PWM216__"},
188132451Sroberto    {"at90pwm316", "__AVR_AT90PWM316__"},
189132451Sroberto    {"atmega32c1", "__AVR_ATmega32C1__"},
190132451Sroberto    {"atmega64c1", "__AVR_ATmega64C1__"},
191132451Sroberto    {"atmega16m1", "__AVR_ATmega16M1__"},
192132451Sroberto    {"atmega32m1", "__AVR_ATmega32M1__"},
193132451Sroberto    {"atmega64m1", "__AVR_ATmega64M1__"},
194132451Sroberto    {"atmega16u4", "__AVR_ATmega16U4__"},
195132451Sroberto    {"atmega32u4", "__AVR_ATmega32U4__"},
196132451Sroberto    {"atmega32u6", "__AVR_ATmega32U6__"},
197132451Sroberto    {"at90usb646", "__AVR_AT90USB646__"},
198132451Sroberto    {"at90usb647", "__AVR_AT90USB647__"},
199132451Sroberto    {"at90scr100", "__AVR_AT90SCR100__"},
200132451Sroberto    {"at94k", "__AVR_AT94K__"},
201132451Sroberto    {"m3000", "__AVR_AT000__"},
202132451Sroberto    {"atmega128", "__AVR_ATmega128__"},
203132451Sroberto    {"atmega128a", "__AVR_ATmega128A__"},
204132451Sroberto    {"atmega1280", "__AVR_ATmega1280__"},
205132451Sroberto    {"atmega1281", "__AVR_ATmega1281__"},
206132451Sroberto    {"atmega1284", "__AVR_ATmega1284__"},
207132451Sroberto    {"atmega1284p", "__AVR_ATmega1284P__"},
208132451Sroberto    {"atmega128rfa1", "__AVR_ATmega128RFA1__"},
209132451Sroberto    {"atmega128rfr2", "__AVR_ATmega128RFR2__"},
210132451Sroberto    {"atmega1284rfr2", "__AVR_ATmega1284RFR2__"},
211132451Sroberto    {"at90can128", "__AVR_AT90CAN128__"},
212132451Sroberto    {"at90usb1286", "__AVR_AT90USB1286__"},
213132451Sroberto    {"at90usb1287", "__AVR_AT90USB1287__"},
214132451Sroberto    {"atmega2560", "__AVR_ATmega2560__"},
215132451Sroberto    {"atmega2561", "__AVR_ATmega2561__"},
216132451Sroberto    {"atmega256rfr2", "__AVR_ATmega256RFR2__"},
217132451Sroberto    {"atmega2564rfr2", "__AVR_ATmega2564RFR2__"},
218132451Sroberto    {"atxmega16a4", "__AVR_ATxmega16A4__"},
219132451Sroberto    {"atxmega16a4u", "__AVR_ATxmega16a4U__"},
220132451Sroberto    {"atxmega16c4", "__AVR_ATxmega16C4__"},
221132451Sroberto    {"atxmega16d4", "__AVR_ATxmega16D4__"},
222132451Sroberto    {"atxmega32a4", "__AVR_ATxmega32A4__"},
223132451Sroberto    {"atxmega32a4u", "__AVR_ATxmega32A4U__"},
224132451Sroberto    {"atxmega32c4", "__AVR_ATxmega32C4__"},
225132451Sroberto    {"atxmega32d4", "__AVR_ATxmega32D4__"},
226132451Sroberto    {"atxmega32e5", "__AVR_ATxmega32E5__"},
227132451Sroberto    {"atxmega16e5", "__AVR_ATxmega16E5__"},
228132451Sroberto    {"atxmega8e5", "__AVR_ATxmega8E5__"},
229132451Sroberto    {"atxmega32x1", "__AVR_ATxmega32X1__"},
230132451Sroberto    {"atxmega64a3", "__AVR_ATxmega64A3__"},
231132451Sroberto    {"atxmega64a3u", "__AVR_ATxmega64A3U__"},
232132451Sroberto    {"atxmega64a4u", "__AVR_ATxmega64A4U__"},
233132451Sroberto    {"atxmega64b1", "__AVR_ATxmega64B1__"},
234132451Sroberto    {"atxmega64b3", "__AVR_ATxmega64B3__"},
235132451Sroberto    {"atxmega64c3", "__AVR_ATxmega64C3__"},
236132451Sroberto    {"atxmega64d3", "__AVR_ATxmega64D3__"},
237132451Sroberto    {"atxmega64d4", "__AVR_ATxmega64D4__"},
238132451Sroberto    {"atxmega64a1", "__AVR_ATxmega64A1__"},
239132451Sroberto    {"atxmega64a1u", "__AVR_ATxmega64A1U__"},
240132451Sroberto    {"atxmega128a3", "__AVR_ATxmega128A3__"},
241132451Sroberto    {"atxmega128a3u", "__AVR_ATxmega128A3U__"},
242132451Sroberto    {"atxmega128b1", "__AVR_ATxmega128B1__"},
243132451Sroberto    {"atxmega128b3", "__AVR_ATxmega128B3__"},
244132451Sroberto    {"atxmega128c3", "__AVR_ATxmega128C3__"},
245132451Sroberto    {"atxmega128d3", "__AVR_ATxmega128D3__"},
246132451Sroberto    {"atxmega128d4", "__AVR_ATxmega128D4__"},
247132451Sroberto    {"atxmega192a3", "__AVR_ATxmega192A3__"},
248132451Sroberto    {"atxmega192a3u", "__AVR_ATxmega192A3U__"},
249132451Sroberto    {"atxmega192c3", "__AVR_ATxmega192C3__"},
250132451Sroberto    {"atxmega192d3", "__AVR_ATxmega192D3__"},
251132451Sroberto    {"atxmega256a3", "__AVR_ATxmega256A3__"},
252132451Sroberto    {"atxmega256a3u", "__AVR_ATxmega256A3U__"},
253132451Sroberto    {"atxmega256a3b", "__AVR_ATxmega256A3B__"},
254132451Sroberto    {"atxmega256a3bu", "__AVR_ATxmega256A3BU__"},
255132451Sroberto    {"atxmega256c3", "__AVR_ATxmega256C3__"},
256132451Sroberto    {"atxmega256d3", "__AVR_ATxmega256D3__"},
257132451Sroberto    {"atxmega384c3", "__AVR_ATxmega384C3__"},
258132451Sroberto    {"atxmega384d3", "__AVR_ATxmega384D3__"},
259132451Sroberto    {"atxmega128a1", "__AVR_ATxmega128A1__"},
260132451Sroberto    {"atxmega128a1u", "__AVR_ATxmega128A1U__"},
261132451Sroberto    {"atxmega128a4u", "__AVR_ATxmega128a4U__"},
262132451Sroberto    {"attiny4", "__AVR_ATtiny4__"},
263132451Sroberto    {"attiny5", "__AVR_ATtiny5__"},
264132451Sroberto    {"attiny9", "__AVR_ATtiny9__"},
265132451Sroberto    {"attiny10", "__AVR_ATtiny10__"},
266132451Sroberto    {"attiny20", "__AVR_ATtiny20__"},
267132451Sroberto    {"attiny40", "__AVR_ATtiny40__"},
268132451Sroberto    {"attiny102", "__AVR_ATtiny102__"},
269132451Sroberto    {"attiny104", "__AVR_ATtiny104__"},
270132451Sroberto};
271132451Sroberto
272132451Sroberto} // namespace targets
273132451Sroberto} // namespace clang
274132451Sroberto
275132451Srobertostatic constexpr llvm::StringLiteral ValidFamilyNames[] = {
276132451Sroberto    "avr1",      "avr2",      "avr25",     "avr3",      "avr31",
277132451Sroberto    "avr35",     "avr4",      "avr5",      "avr51",     "avr6",
278132451Sroberto    "avrxmega1", "avrxmega2", "avrxmega3", "avrxmega4", "avrxmega5",
279132451Sroberto    "avrxmega6", "avrxmega7", "avrtiny"};
280132451Sroberto
281132451Srobertobool AVRTargetInfo::isValidCPUName(StringRef Name) const {
282132451Sroberto  bool IsFamily =
283132451Sroberto      llvm::find(ValidFamilyNames, Name) != std::end(ValidFamilyNames);
284132451Sroberto
285132451Sroberto  bool IsMCU =
286132451Sroberto      llvm::find_if(AVRMcus, [&](const MCUInfo &Info) {
287132451Sroberto        return Info.Name == Name;
288132451Sroberto      }) != std::end(AVRMcus);
289132451Sroberto  return IsFamily || IsMCU;
290132451Sroberto}
291132451Sroberto
292132451Srobertovoid AVRTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
293132451Sroberto  Values.append(std::begin(ValidFamilyNames), std::end(ValidFamilyNames));
294132451Sroberto  for (const MCUInfo &Info : AVRMcus)
295132451Sroberto    Values.push_back(Info.Name);
296132451Sroberto}
297132451Sroberto
298132451Srobertovoid AVRTargetInfo::getTargetDefines(const LangOptions &Opts,
299132451Sroberto                                     MacroBuilder &Builder) const {
300132451Sroberto  Builder.defineMacro("AVR");
301132451Sroberto  Builder.defineMacro("__AVR");
302132451Sroberto  Builder.defineMacro("__AVR__");
303132451Sroberto
304132451Sroberto  if (!this->CPU.empty()) {
305132451Sroberto    auto It = llvm::find_if(
306132451Sroberto        AVRMcus, [&](const MCUInfo &Info) { return Info.Name == this->CPU; });
307132451Sroberto
308132451Sroberto    if (It != std::end(AVRMcus))
309132451Sroberto      Builder.defineMacro(It->DefineName);
310132451Sroberto  }
311132451Sroberto}
312132451Sroberto