xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Targets/AVR.cpp (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 //===--- AVR.cpp - Implement AVR target feature support -------------------===//
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 // This file implements AVR TargetInfo objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "AVR.h"
14 #include "clang/Basic/MacroBuilder.h"
15 #include "llvm/ADT/StringSwitch.h"
16 
17 using namespace clang;
18 using namespace clang::targets;
19 
20 namespace clang {
21 namespace targets {
22 
23 /// Information about a specific microcontroller.
24 struct LLVM_LIBRARY_VISIBILITY MCUInfo {
25   const char *Name;
26   const char *DefineName;
27 };
28 
29 // This list should be kept up-to-date with AVRDevices.td in LLVM.
30 static MCUInfo AVRMcus[] = {
31     {"at90s1200", "__AVR_AT90S1200__"},
32     {"attiny11", "__AVR_ATtiny11__"},
33     {"attiny12", "__AVR_ATtiny12__"},
34     {"attiny15", "__AVR_ATtiny15__"},
35     {"attiny28", "__AVR_ATtiny28__"},
36     {"at90s2313", "__AVR_AT90S2313__"},
37     {"at90s2323", "__AVR_AT90S2323__"},
38     {"at90s2333", "__AVR_AT90S2333__"},
39     {"at90s2343", "__AVR_AT90S2343__"},
40     {"attiny22", "__AVR_ATtiny22__"},
41     {"attiny26", "__AVR_ATtiny26__"},
42     {"at86rf401", "__AVR_AT86RF401__"},
43     {"at90s4414", "__AVR_AT90S4414__"},
44     {"at90s4433", "__AVR_AT90S4433__"},
45     {"at90s4434", "__AVR_AT90S4434__"},
46     {"at90s8515", "__AVR_AT90S8515__"},
47     {"at90c8534", "__AVR_AT90c8534__"},
48     {"at90s8535", "__AVR_AT90S8535__"},
49     {"ata5272", "__AVR_ATA5272__"},
50     {"attiny13", "__AVR_ATtiny13__"},
51     {"attiny13a", "__AVR_ATtiny13A__"},
52     {"attiny2313", "__AVR_ATtiny2313__"},
53     {"attiny2313a", "__AVR_ATtiny2313A__"},
54     {"attiny24", "__AVR_ATtiny24__"},
55     {"attiny24a", "__AVR_ATtiny24A__"},
56     {"attiny4313", "__AVR_ATtiny4313__"},
57     {"attiny44", "__AVR_ATtiny44__"},
58     {"attiny44a", "__AVR_ATtiny44A__"},
59     {"attiny84", "__AVR_ATtiny84__"},
60     {"attiny84a", "__AVR_ATtiny84A__"},
61     {"attiny25", "__AVR_ATtiny25__"},
62     {"attiny45", "__AVR_ATtiny45__"},
63     {"attiny85", "__AVR_ATtiny85__"},
64     {"attiny261", "__AVR_ATtiny261__"},
65     {"attiny261a", "__AVR_ATtiny261A__"},
66     {"attiny461", "__AVR_ATtiny461__"},
67     {"attiny461a", "__AVR_ATtiny461A__"},
68     {"attiny861", "__AVR_ATtiny861__"},
69     {"attiny861a", "__AVR_ATtiny861A__"},
70     {"attiny87", "__AVR_ATtiny87__"},
71     {"attiny43u", "__AVR_ATtiny43U__"},
72     {"attiny48", "__AVR_ATtiny48__"},
73     {"attiny88", "__AVR_ATtiny88__"},
74     {"attiny828", "__AVR_ATtiny828__"},
75     {"at43usb355", "__AVR_AT43USB355__"},
76     {"at76c711", "__AVR_AT76C711__"},
77     {"atmega103", "__AVR_ATmega103__"},
78     {"at43usb320", "__AVR_AT43USB320__"},
79     {"attiny167", "__AVR_ATtiny167__"},
80     {"at90usb82", "__AVR_AT90USB82__"},
81     {"at90usb162", "__AVR_AT90USB162__"},
82     {"ata5505", "__AVR_ATA5505__"},
83     {"atmega8u2", "__AVR_ATmega8U2__"},
84     {"atmega16u2", "__AVR_ATmega16U2__"},
85     {"atmega32u2", "__AVR_ATmega32U2__"},
86     {"attiny1634", "__AVR_ATtiny1634__"},
87     {"atmega8", "__AVR_ATmega8__"},
88     {"ata6289", "__AVR_ATA6289__"},
89     {"atmega8a", "__AVR_ATmega8A__"},
90     {"ata6285", "__AVR_ATA6285__"},
91     {"ata6286", "__AVR_ATA6286__"},
92     {"atmega48", "__AVR_ATmega48__"},
93     {"atmega48a", "__AVR_ATmega48A__"},
94     {"atmega48pa", "__AVR_ATmega48PA__"},
95     {"atmega48p", "__AVR_ATmega48P__"},
96     {"atmega88", "__AVR_ATmega88__"},
97     {"atmega88a", "__AVR_ATmega88A__"},
98     {"atmega88p", "__AVR_ATmega88P__"},
99     {"atmega88pa", "__AVR_ATmega88PA__"},
100     {"atmega8515", "__AVR_ATmega8515__"},
101     {"atmega8535", "__AVR_ATmega8535__"},
102     {"atmega8hva", "__AVR_ATmega8HVA__"},
103     {"at90pwm1", "__AVR_AT90PWM1__"},
104     {"at90pwm2", "__AVR_AT90PWM2__"},
105     {"at90pwm2b", "__AVR_AT90PWM2B__"},
106     {"at90pwm3", "__AVR_AT90PWM3__"},
107     {"at90pwm3b", "__AVR_AT90PWM3B__"},
108     {"at90pwm81", "__AVR_AT90PWM81__"},
109     {"ata5790", "__AVR_ATA5790__"},
110     {"ata5795", "__AVR_ATA5795__"},
111     {"atmega16", "__AVR_ATmega16__"},
112     {"atmega16a", "__AVR_ATmega16A__"},
113     {"atmega161", "__AVR_ATmega161__"},
114     {"atmega162", "__AVR_ATmega162__"},
115     {"atmega163", "__AVR_ATmega163__"},
116     {"atmega164a", "__AVR_ATmega164A__"},
117     {"atmega164p", "__AVR_ATmega164P__"},
118     {"atmega164pa", "__AVR_ATmega164PA__"},
119     {"atmega165", "__AVR_ATmega165__"},
120     {"atmega165a", "__AVR_ATmega165A__"},
121     {"atmega165p", "__AVR_ATmega165P__"},
122     {"atmega165pa", "__AVR_ATmega165PA__"},
123     {"atmega168", "__AVR_ATmega168__"},
124     {"atmega168a", "__AVR_ATmega168A__"},
125     {"atmega168p", "__AVR_ATmega168P__"},
126     {"atmega168pa", "__AVR_ATmega168PA__"},
127     {"atmega169", "__AVR_ATmega169__"},
128     {"atmega169a", "__AVR_ATmega169A__"},
129     {"atmega169p", "__AVR_ATmega169P__"},
130     {"atmega169pa", "__AVR_ATmega169PA__"},
131     {"atmega32", "__AVR_ATmega32__"},
132     {"atmega32a", "__AVR_ATmega32A__"},
133     {"atmega323", "__AVR_ATmega323__"},
134     {"atmega324a", "__AVR_ATmega324A__"},
135     {"atmega324p", "__AVR_ATmega324P__"},
136     {"atmega324pa", "__AVR_ATmega324PA__"},
137     {"atmega325", "__AVR_ATmega325__"},
138     {"atmega325a", "__AVR_ATmega325A__"},
139     {"atmega325p", "__AVR_ATmega325P__"},
140     {"atmega325pa", "__AVR_ATmega325PA__"},
141     {"atmega3250", "__AVR_ATmega3250__"},
142     {"atmega3250a", "__AVR_ATmega3250A__"},
143     {"atmega3250p", "__AVR_ATmega3250P__"},
144     {"atmega3250pa", "__AVR_ATmega3250PA__"},
145     {"atmega328", "__AVR_ATmega328__"},
146     {"atmega328p", "__AVR_ATmega328P__"},
147     {"atmega329", "__AVR_ATmega329__"},
148     {"atmega329a", "__AVR_ATmega329A__"},
149     {"atmega329p", "__AVR_ATmega329P__"},
150     {"atmega329pa", "__AVR_ATmega329PA__"},
151     {"atmega3290", "__AVR_ATmega3290__"},
152     {"atmega3290a", "__AVR_ATmega3290A__"},
153     {"atmega3290p", "__AVR_ATmega3290P__"},
154     {"atmega3290pa", "__AVR_ATmega3290PA__"},
155     {"atmega406", "__AVR_ATmega406__"},
156     {"atmega64", "__AVR_ATmega64__"},
157     {"atmega64a", "__AVR_ATmega64A__"},
158     {"atmega640", "__AVR_ATmega640__"},
159     {"atmega644", "__AVR_ATmega644__"},
160     {"atmega644a", "__AVR_ATmega644A__"},
161     {"atmega644p", "__AVR_ATmega644P__"},
162     {"atmega644pa", "__AVR_ATmega644PA__"},
163     {"atmega645", "__AVR_ATmega645__"},
164     {"atmega645a", "__AVR_ATmega645A__"},
165     {"atmega645p", "__AVR_ATmega645P__"},
166     {"atmega649", "__AVR_ATmega649__"},
167     {"atmega649a", "__AVR_ATmega649A__"},
168     {"atmega649p", "__AVR_ATmega649P__"},
169     {"atmega6450", "__AVR_ATmega6450__"},
170     {"atmega6450a", "__AVR_ATmega6450A__"},
171     {"atmega6450p", "__AVR_ATmega6450P__"},
172     {"atmega6490", "__AVR_ATmega6490__"},
173     {"atmega6490a", "__AVR_ATmega6490A__"},
174     {"atmega6490p", "__AVR_ATmega6490P__"},
175     {"atmega64rfr2", "__AVR_ATmega64RFR2__"},
176     {"atmega644rfr2", "__AVR_ATmega644RFR2__"},
177     {"atmega16hva", "__AVR_ATmega16HVA__"},
178     {"atmega16hva2", "__AVR_ATmega16HVA2__"},
179     {"atmega16hvb", "__AVR_ATmega16HVB__"},
180     {"atmega16hvbrevb", "__AVR_ATmega16HVBREVB__"},
181     {"atmega32hvb", "__AVR_ATmega32HVB__"},
182     {"atmega32hvbrevb", "__AVR_ATmega32HVBREVB__"},
183     {"atmega64hve", "__AVR_ATmega64HVE__"},
184     {"at90can32", "__AVR_AT90CAN32__"},
185     {"at90can64", "__AVR_AT90CAN64__"},
186     {"at90pwm161", "__AVR_AT90PWM161__"},
187     {"at90pwm216", "__AVR_AT90PWM216__"},
188     {"at90pwm316", "__AVR_AT90PWM316__"},
189     {"atmega32c1", "__AVR_ATmega32C1__"},
190     {"atmega64c1", "__AVR_ATmega64C1__"},
191     {"atmega16m1", "__AVR_ATmega16M1__"},
192     {"atmega32m1", "__AVR_ATmega32M1__"},
193     {"atmega64m1", "__AVR_ATmega64M1__"},
194     {"atmega16u4", "__AVR_ATmega16U4__"},
195     {"atmega32u4", "__AVR_ATmega32U4__"},
196     {"atmega32u6", "__AVR_ATmega32U6__"},
197     {"at90usb646", "__AVR_AT90USB646__"},
198     {"at90usb647", "__AVR_AT90USB647__"},
199     {"at90scr100", "__AVR_AT90SCR100__"},
200     {"at94k", "__AVR_AT94K__"},
201     {"m3000", "__AVR_AT000__"},
202     {"atmega128", "__AVR_ATmega128__"},
203     {"atmega128a", "__AVR_ATmega128A__"},
204     {"atmega1280", "__AVR_ATmega1280__"},
205     {"atmega1281", "__AVR_ATmega1281__"},
206     {"atmega1284", "__AVR_ATmega1284__"},
207     {"atmega1284p", "__AVR_ATmega1284P__"},
208     {"atmega128rfa1", "__AVR_ATmega128RFA1__"},
209     {"atmega128rfr2", "__AVR_ATmega128RFR2__"},
210     {"atmega1284rfr2", "__AVR_ATmega1284RFR2__"},
211     {"at90can128", "__AVR_AT90CAN128__"},
212     {"at90usb1286", "__AVR_AT90USB1286__"},
213     {"at90usb1287", "__AVR_AT90USB1287__"},
214     {"atmega2560", "__AVR_ATmega2560__"},
215     {"atmega2561", "__AVR_ATmega2561__"},
216     {"atmega256rfr2", "__AVR_ATmega256RFR2__"},
217     {"atmega2564rfr2", "__AVR_ATmega2564RFR2__"},
218     {"atxmega16a4", "__AVR_ATxmega16A4__"},
219     {"atxmega16a4u", "__AVR_ATxmega16a4U__"},
220     {"atxmega16c4", "__AVR_ATxmega16C4__"},
221     {"atxmega16d4", "__AVR_ATxmega16D4__"},
222     {"atxmega32a4", "__AVR_ATxmega32A4__"},
223     {"atxmega32a4u", "__AVR_ATxmega32A4U__"},
224     {"atxmega32c4", "__AVR_ATxmega32C4__"},
225     {"atxmega32d4", "__AVR_ATxmega32D4__"},
226     {"atxmega32e5", "__AVR_ATxmega32E5__"},
227     {"atxmega16e5", "__AVR_ATxmega16E5__"},
228     {"atxmega8e5", "__AVR_ATxmega8E5__"},
229     {"atxmega32x1", "__AVR_ATxmega32X1__"},
230     {"atxmega64a3", "__AVR_ATxmega64A3__"},
231     {"atxmega64a3u", "__AVR_ATxmega64A3U__"},
232     {"atxmega64a4u", "__AVR_ATxmega64A4U__"},
233     {"atxmega64b1", "__AVR_ATxmega64B1__"},
234     {"atxmega64b3", "__AVR_ATxmega64B3__"},
235     {"atxmega64c3", "__AVR_ATxmega64C3__"},
236     {"atxmega64d3", "__AVR_ATxmega64D3__"},
237     {"atxmega64d4", "__AVR_ATxmega64D4__"},
238     {"atxmega64a1", "__AVR_ATxmega64A1__"},
239     {"atxmega64a1u", "__AVR_ATxmega64A1U__"},
240     {"atxmega128a3", "__AVR_ATxmega128A3__"},
241     {"atxmega128a3u", "__AVR_ATxmega128A3U__"},
242     {"atxmega128b1", "__AVR_ATxmega128B1__"},
243     {"atxmega128b3", "__AVR_ATxmega128B3__"},
244     {"atxmega128c3", "__AVR_ATxmega128C3__"},
245     {"atxmega128d3", "__AVR_ATxmega128D3__"},
246     {"atxmega128d4", "__AVR_ATxmega128D4__"},
247     {"atxmega192a3", "__AVR_ATxmega192A3__"},
248     {"atxmega192a3u", "__AVR_ATxmega192A3U__"},
249     {"atxmega192c3", "__AVR_ATxmega192C3__"},
250     {"atxmega192d3", "__AVR_ATxmega192D3__"},
251     {"atxmega256a3", "__AVR_ATxmega256A3__"},
252     {"atxmega256a3u", "__AVR_ATxmega256A3U__"},
253     {"atxmega256a3b", "__AVR_ATxmega256A3B__"},
254     {"atxmega256a3bu", "__AVR_ATxmega256A3BU__"},
255     {"atxmega256c3", "__AVR_ATxmega256C3__"},
256     {"atxmega256d3", "__AVR_ATxmega256D3__"},
257     {"atxmega384c3", "__AVR_ATxmega384C3__"},
258     {"atxmega384d3", "__AVR_ATxmega384D3__"},
259     {"atxmega128a1", "__AVR_ATxmega128A1__"},
260     {"atxmega128a1u", "__AVR_ATxmega128A1U__"},
261     {"atxmega128a4u", "__AVR_ATxmega128a4U__"},
262     {"attiny4", "__AVR_ATtiny4__"},
263     {"attiny5", "__AVR_ATtiny5__"},
264     {"attiny9", "__AVR_ATtiny9__"},
265     {"attiny10", "__AVR_ATtiny10__"},
266     {"attiny20", "__AVR_ATtiny20__"},
267     {"attiny40", "__AVR_ATtiny40__"},
268     {"attiny102", "__AVR_ATtiny102__"},
269     {"attiny104", "__AVR_ATtiny104__"},
270 };
271 
272 } // namespace targets
273 } // namespace clang
274 
275 static constexpr llvm::StringLiteral ValidFamilyNames[] = {
276     "avr1",      "avr2",      "avr25",     "avr3",      "avr31",
277     "avr35",     "avr4",      "avr5",      "avr51",     "avr6",
278     "avrxmega1", "avrxmega2", "avrxmega3", "avrxmega4", "avrxmega5",
279     "avrxmega6", "avrxmega7", "avrtiny"};
280 
281 bool AVRTargetInfo::isValidCPUName(StringRef Name) const {
282   bool IsFamily =
283       llvm::find(ValidFamilyNames, Name) != std::end(ValidFamilyNames);
284 
285   bool IsMCU =
286       llvm::find_if(AVRMcus, [&](const MCUInfo &Info) {
287         return Info.Name == Name;
288       }) != std::end(AVRMcus);
289   return IsFamily || IsMCU;
290 }
291 
292 void AVRTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
293   Values.append(std::begin(ValidFamilyNames), std::end(ValidFamilyNames));
294   for (const MCUInfo &Info : AVRMcus)
295     Values.push_back(Info.Name);
296 }
297 
298 void AVRTargetInfo::getTargetDefines(const LangOptions &Opts,
299                                      MacroBuilder &Builder) const {
300   Builder.defineMacro("AVR");
301   Builder.defineMacro("__AVR");
302   Builder.defineMacro("__AVR__");
303 
304   if (!this->CPU.empty()) {
305     auto It = llvm::find_if(
306         AVRMcus, [&](const MCUInfo &Info) { return Info.Name == this->CPU; });
307 
308     if (It != std::end(AVRMcus))
309       Builder.defineMacro(It->DefineName);
310   }
311 }
312