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     {"attiny441", "__AVR_ATtiny441__"},
67     {"attiny461", "__AVR_ATtiny461__"},
68     {"attiny461a", "__AVR_ATtiny461A__"},
69     {"attiny841", "__AVR_ATtiny841__"},
70     {"attiny861", "__AVR_ATtiny861__"},
71     {"attiny861a", "__AVR_ATtiny861A__"},
72     {"attiny87", "__AVR_ATtiny87__"},
73     {"attiny43u", "__AVR_ATtiny43U__"},
74     {"attiny48", "__AVR_ATtiny48__"},
75     {"attiny88", "__AVR_ATtiny88__"},
76     {"attiny828", "__AVR_ATtiny828__"},
77     {"at43usb355", "__AVR_AT43USB355__"},
78     {"at76c711", "__AVR_AT76C711__"},
79     {"atmega103", "__AVR_ATmega103__"},
80     {"at43usb320", "__AVR_AT43USB320__"},
81     {"attiny167", "__AVR_ATtiny167__"},
82     {"at90usb82", "__AVR_AT90USB82__"},
83     {"at90usb162", "__AVR_AT90USB162__"},
84     {"ata5505", "__AVR_ATA5505__"},
85     {"atmega8u2", "__AVR_ATmega8U2__"},
86     {"atmega16u2", "__AVR_ATmega16U2__"},
87     {"atmega32u2", "__AVR_ATmega32U2__"},
88     {"attiny1634", "__AVR_ATtiny1634__"},
89     {"atmega8", "__AVR_ATmega8__"},
90     {"ata6289", "__AVR_ATA6289__"},
91     {"atmega8a", "__AVR_ATmega8A__"},
92     {"ata6285", "__AVR_ATA6285__"},
93     {"ata6286", "__AVR_ATA6286__"},
94     {"atmega48", "__AVR_ATmega48__"},
95     {"atmega48a", "__AVR_ATmega48A__"},
96     {"atmega48pa", "__AVR_ATmega48PA__"},
97     {"atmega48pb", "__AVR_ATmega48PB__"},
98     {"atmega48p", "__AVR_ATmega48P__"},
99     {"atmega88", "__AVR_ATmega88__"},
100     {"atmega88a", "__AVR_ATmega88A__"},
101     {"atmega88p", "__AVR_ATmega88P__"},
102     {"atmega88pa", "__AVR_ATmega88PA__"},
103     {"atmega88pb", "__AVR_ATmega88PB__"},
104     {"atmega8515", "__AVR_ATmega8515__"},
105     {"atmega8535", "__AVR_ATmega8535__"},
106     {"atmega8hva", "__AVR_ATmega8HVA__"},
107     {"at90pwm1", "__AVR_AT90PWM1__"},
108     {"at90pwm2", "__AVR_AT90PWM2__"},
109     {"at90pwm2b", "__AVR_AT90PWM2B__"},
110     {"at90pwm3", "__AVR_AT90PWM3__"},
111     {"at90pwm3b", "__AVR_AT90PWM3B__"},
112     {"at90pwm81", "__AVR_AT90PWM81__"},
113     {"ata5790", "__AVR_ATA5790__"},
114     {"ata5795", "__AVR_ATA5795__"},
115     {"atmega16", "__AVR_ATmega16__"},
116     {"atmega16a", "__AVR_ATmega16A__"},
117     {"atmega161", "__AVR_ATmega161__"},
118     {"atmega162", "__AVR_ATmega162__"},
119     {"atmega163", "__AVR_ATmega163__"},
120     {"atmega164a", "__AVR_ATmega164A__"},
121     {"atmega164p", "__AVR_ATmega164P__"},
122     {"atmega164pa", "__AVR_ATmega164PA__"},
123     {"atmega165", "__AVR_ATmega165__"},
124     {"atmega165a", "__AVR_ATmega165A__"},
125     {"atmega165p", "__AVR_ATmega165P__"},
126     {"atmega165pa", "__AVR_ATmega165PA__"},
127     {"atmega168", "__AVR_ATmega168__"},
128     {"atmega168a", "__AVR_ATmega168A__"},
129     {"atmega168p", "__AVR_ATmega168P__"},
130     {"atmega168pa", "__AVR_ATmega168PA__"},
131     {"atmega168pb", "__AVR_ATmega168PB__"},
132     {"atmega169", "__AVR_ATmega169__"},
133     {"atmega169a", "__AVR_ATmega169A__"},
134     {"atmega169p", "__AVR_ATmega169P__"},
135     {"atmega169pa", "__AVR_ATmega169PA__"},
136     {"atmega32", "__AVR_ATmega32__"},
137     {"atmega32a", "__AVR_ATmega32A__"},
138     {"atmega323", "__AVR_ATmega323__"},
139     {"atmega324a", "__AVR_ATmega324A__"},
140     {"atmega324p", "__AVR_ATmega324P__"},
141     {"atmega324pa", "__AVR_ATmega324PA__"},
142     {"atmega324pb", "__AVR_ATmega324PB__"},
143     {"atmega325", "__AVR_ATmega325__"},
144     {"atmega325a", "__AVR_ATmega325A__"},
145     {"atmega325p", "__AVR_ATmega325P__"},
146     {"atmega325pa", "__AVR_ATmega325PA__"},
147     {"atmega3250", "__AVR_ATmega3250__"},
148     {"atmega3250a", "__AVR_ATmega3250A__"},
149     {"atmega3250p", "__AVR_ATmega3250P__"},
150     {"atmega3250pa", "__AVR_ATmega3250PA__"},
151     {"atmega328", "__AVR_ATmega328__"},
152     {"atmega328p", "__AVR_ATmega328P__"},
153     {"atmega328pb", "__AVR_ATmega328PB__"},
154     {"atmega329", "__AVR_ATmega329__"},
155     {"atmega329a", "__AVR_ATmega329A__"},
156     {"atmega329p", "__AVR_ATmega329P__"},
157     {"atmega329pa", "__AVR_ATmega329PA__"},
158     {"atmega3290", "__AVR_ATmega3290__"},
159     {"atmega3290a", "__AVR_ATmega3290A__"},
160     {"atmega3290p", "__AVR_ATmega3290P__"},
161     {"atmega3290pa", "__AVR_ATmega3290PA__"},
162     {"atmega406", "__AVR_ATmega406__"},
163     {"atmega64", "__AVR_ATmega64__"},
164     {"atmega64a", "__AVR_ATmega64A__"},
165     {"atmega640", "__AVR_ATmega640__"},
166     {"atmega644", "__AVR_ATmega644__"},
167     {"atmega644a", "__AVR_ATmega644A__"},
168     {"atmega644p", "__AVR_ATmega644P__"},
169     {"atmega644pa", "__AVR_ATmega644PA__"},
170     {"atmega645", "__AVR_ATmega645__"},
171     {"atmega645a", "__AVR_ATmega645A__"},
172     {"atmega645p", "__AVR_ATmega645P__"},
173     {"atmega649", "__AVR_ATmega649__"},
174     {"atmega649a", "__AVR_ATmega649A__"},
175     {"atmega649p", "__AVR_ATmega649P__"},
176     {"atmega6450", "__AVR_ATmega6450__"},
177     {"atmega6450a", "__AVR_ATmega6450A__"},
178     {"atmega6450p", "__AVR_ATmega6450P__"},
179     {"atmega6490", "__AVR_ATmega6490__"},
180     {"atmega6490a", "__AVR_ATmega6490A__"},
181     {"atmega6490p", "__AVR_ATmega6490P__"},
182     {"atmega64rfr2", "__AVR_ATmega64RFR2__"},
183     {"atmega644rfr2", "__AVR_ATmega644RFR2__"},
184     {"atmega16hva", "__AVR_ATmega16HVA__"},
185     {"atmega16hva2", "__AVR_ATmega16HVA2__"},
186     {"atmega16hvb", "__AVR_ATmega16HVB__"},
187     {"atmega16hvbrevb", "__AVR_ATmega16HVBREVB__"},
188     {"atmega32hvb", "__AVR_ATmega32HVB__"},
189     {"atmega32hvbrevb", "__AVR_ATmega32HVBREVB__"},
190     {"atmega64hve", "__AVR_ATmega64HVE__"},
191     {"at90can32", "__AVR_AT90CAN32__"},
192     {"at90can64", "__AVR_AT90CAN64__"},
193     {"at90pwm161", "__AVR_AT90PWM161__"},
194     {"at90pwm216", "__AVR_AT90PWM216__"},
195     {"at90pwm316", "__AVR_AT90PWM316__"},
196     {"atmega32c1", "__AVR_ATmega32C1__"},
197     {"atmega64c1", "__AVR_ATmega64C1__"},
198     {"atmega16m1", "__AVR_ATmega16M1__"},
199     {"atmega32m1", "__AVR_ATmega32M1__"},
200     {"atmega64m1", "__AVR_ATmega64M1__"},
201     {"atmega16u4", "__AVR_ATmega16U4__"},
202     {"atmega32u4", "__AVR_ATmega32U4__"},
203     {"atmega32u6", "__AVR_ATmega32U6__"},
204     {"at90usb646", "__AVR_AT90USB646__"},
205     {"at90usb647", "__AVR_AT90USB647__"},
206     {"at90scr100", "__AVR_AT90SCR100__"},
207     {"at94k", "__AVR_AT94K__"},
208     {"m3000", "__AVR_AT000__"},
209     {"atmega128", "__AVR_ATmega128__"},
210     {"atmega128a", "__AVR_ATmega128A__"},
211     {"atmega1280", "__AVR_ATmega1280__"},
212     {"atmega1281", "__AVR_ATmega1281__"},
213     {"atmega1284", "__AVR_ATmega1284__"},
214     {"atmega1284p", "__AVR_ATmega1284P__"},
215     {"atmega128rfa1", "__AVR_ATmega128RFA1__"},
216     {"atmega128rfr2", "__AVR_ATmega128RFR2__"},
217     {"atmega1284rfr2", "__AVR_ATmega1284RFR2__"},
218     {"at90can128", "__AVR_AT90CAN128__"},
219     {"at90usb1286", "__AVR_AT90USB1286__"},
220     {"at90usb1287", "__AVR_AT90USB1287__"},
221     {"atmega2560", "__AVR_ATmega2560__"},
222     {"atmega2561", "__AVR_ATmega2561__"},
223     {"atmega256rfr2", "__AVR_ATmega256RFR2__"},
224     {"atmega2564rfr2", "__AVR_ATmega2564RFR2__"},
225     {"atxmega16a4", "__AVR_ATxmega16A4__"},
226     {"atxmega16a4u", "__AVR_ATxmega16A4U__"},
227     {"atxmega16c4", "__AVR_ATxmega16C4__"},
228     {"atxmega16d4", "__AVR_ATxmega16D4__"},
229     {"atxmega32a4", "__AVR_ATxmega32A4__"},
230     {"atxmega32a4u", "__AVR_ATxmega32A4U__"},
231     {"atxmega32c4", "__AVR_ATxmega32C4__"},
232     {"atxmega32d4", "__AVR_ATxmega32D4__"},
233     {"atxmega32e5", "__AVR_ATxmega32E5__"},
234     {"atxmega16e5", "__AVR_ATxmega16E5__"},
235     {"atxmega8e5", "__AVR_ATxmega8E5__"},
236     {"atxmega32x1", "__AVR_ATxmega32X1__"},
237     {"atxmega64a3", "__AVR_ATxmega64A3__"},
238     {"atxmega64a3u", "__AVR_ATxmega64A3U__"},
239     {"atxmega64a4u", "__AVR_ATxmega64A4U__"},
240     {"atxmega64b1", "__AVR_ATxmega64B1__"},
241     {"atxmega64b3", "__AVR_ATxmega64B3__"},
242     {"atxmega64c3", "__AVR_ATxmega64C3__"},
243     {"atxmega64d3", "__AVR_ATxmega64D3__"},
244     {"atxmega64d4", "__AVR_ATxmega64D4__"},
245     {"atxmega64a1", "__AVR_ATxmega64A1__"},
246     {"atxmega64a1u", "__AVR_ATxmega64A1U__"},
247     {"atxmega128a3", "__AVR_ATxmega128A3__"},
248     {"atxmega128a3u", "__AVR_ATxmega128A3U__"},
249     {"atxmega128b1", "__AVR_ATxmega128B1__"},
250     {"atxmega128b3", "__AVR_ATxmega128B3__"},
251     {"atxmega128c3", "__AVR_ATxmega128C3__"},
252     {"atxmega128d3", "__AVR_ATxmega128D3__"},
253     {"atxmega128d4", "__AVR_ATxmega128D4__"},
254     {"atxmega192a3", "__AVR_ATxmega192A3__"},
255     {"atxmega192a3u", "__AVR_ATxmega192A3U__"},
256     {"atxmega192c3", "__AVR_ATxmega192C3__"},
257     {"atxmega192d3", "__AVR_ATxmega192D3__"},
258     {"atxmega256a3", "__AVR_ATxmega256A3__"},
259     {"atxmega256a3u", "__AVR_ATxmega256A3U__"},
260     {"atxmega256a3b", "__AVR_ATxmega256A3B__"},
261     {"atxmega256a3bu", "__AVR_ATxmega256A3BU__"},
262     {"atxmega256c3", "__AVR_ATxmega256C3__"},
263     {"atxmega256d3", "__AVR_ATxmega256D3__"},
264     {"atxmega384c3", "__AVR_ATxmega384C3__"},
265     {"atxmega384d3", "__AVR_ATxmega384D3__"},
266     {"atxmega128a1", "__AVR_ATxmega128A1__"},
267     {"atxmega128a1u", "__AVR_ATxmega128A1U__"},
268     {"atxmega128a4u", "__AVR_ATxmega128A4U__"},
269     {"attiny4", "__AVR_ATtiny4__"},
270     {"attiny5", "__AVR_ATtiny5__"},
271     {"attiny9", "__AVR_ATtiny9__"},
272     {"attiny10", "__AVR_ATtiny10__"},
273     {"attiny20", "__AVR_ATtiny20__"},
274     {"attiny40", "__AVR_ATtiny40__"},
275     {"attiny102", "__AVR_ATtiny102__"},
276     {"attiny104", "__AVR_ATtiny104__"},
277     {"attiny202", "__AVR_ATtiny202__"},
278     {"attiny402", "__AVR_ATtiny402__"},
279     {"attiny204", "__AVR_ATtiny204__"},
280     {"attiny404", "__AVR_ATtiny404__"},
281     {"attiny804", "__AVR_ATtiny804__"},
282     {"attiny1604", "__AVR_ATtiny1604__"},
283     {"attiny406", "__AVR_ATtiny406__"},
284     {"attiny806", "__AVR_ATtiny806__"},
285     {"attiny1606", "__AVR_ATtiny1606__"},
286     {"attiny807", "__AVR_ATtiny807__"},
287     {"attiny1607", "__AVR_ATtiny1607__"},
288     {"attiny212", "__AVR_ATtiny212__"},
289     {"attiny412", "__AVR_ATtiny412__"},
290     {"attiny214", "__AVR_ATtiny214__"},
291     {"attiny414", "__AVR_ATtiny414__"},
292     {"attiny814", "__AVR_ATtiny814__"},
293     {"attiny1614", "__AVR_ATtiny1614__"},
294     {"attiny416", "__AVR_ATtiny416__"},
295     {"attiny816", "__AVR_ATtiny816__"},
296     {"attiny1616", "__AVR_ATtiny1616__"},
297     {"attiny3216", "__AVR_ATtiny3216__"},
298     {"attiny417", "__AVR_ATtiny417__"},
299     {"attiny817", "__AVR_ATtiny817__"},
300     {"attiny1617", "__AVR_ATtiny1617__"},
301     {"attiny3217", "__AVR_ATtiny3217__"},
302 };
303 
304 } // namespace targets
305 } // namespace clang
306 
307 static constexpr llvm::StringLiteral ValidFamilyNames[] = {
308     "avr1",      "avr2",      "avr25",     "avr3",      "avr31",
309     "avr35",     "avr4",      "avr5",      "avr51",     "avr6",
310     "avrxmega1", "avrxmega2", "avrxmega3", "avrxmega4", "avrxmega5",
311     "avrxmega6", "avrxmega7", "avrtiny"};
312 
isValidCPUName(StringRef Name) const313 bool AVRTargetInfo::isValidCPUName(StringRef Name) const {
314   bool IsFamily = llvm::is_contained(ValidFamilyNames, Name);
315 
316   bool IsMCU =
317       llvm::find_if(AVRMcus, [&](const MCUInfo &Info) {
318         return Info.Name == Name;
319       }) != std::end(AVRMcus);
320   return IsFamily || IsMCU;
321 }
322 
fillValidCPUList(SmallVectorImpl<StringRef> & Values) const323 void AVRTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
324   Values.append(std::begin(ValidFamilyNames), std::end(ValidFamilyNames));
325   for (const MCUInfo &Info : AVRMcus)
326     Values.push_back(Info.Name);
327 }
328 
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const329 void AVRTargetInfo::getTargetDefines(const LangOptions &Opts,
330                                      MacroBuilder &Builder) const {
331   Builder.defineMacro("AVR");
332   Builder.defineMacro("__AVR");
333   Builder.defineMacro("__AVR__");
334   Builder.defineMacro("__ELF__");
335   Builder.defineMacro("__flash", "__attribute__((address_space(1)))");
336 
337   if (!this->CPU.empty()) {
338     auto It = llvm::find_if(
339         AVRMcus, [&](const MCUInfo &Info) { return Info.Name == this->CPU; });
340 
341     if (It != std::end(AVRMcus))
342       Builder.defineMacro(It->DefineName);
343   }
344 }
345