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   StringRef Arch; // The __AVR_ARCH__ value.
28   const int NumFlashBanks; // Set to 0 for the devices do not support LPM/ELPM.
29 };
30 
31 // NOTE: This list has been synchronized with gcc-avr 5.4.0 and avr-libc 2.0.0.
32 static MCUInfo AVRMcus[] = {
33     {"avr1", NULL, "1", 0},
34     {"at90s1200", "__AVR_AT90S1200__", "1", 0},
35     {"attiny11", "__AVR_ATtiny11__", "1", 0},
36     {"attiny12", "__AVR_ATtiny12__", "1", 0},
37     {"attiny15", "__AVR_ATtiny15__", "1", 0},
38     {"attiny28", "__AVR_ATtiny28__", "1", 0},
39     {"avr2", NULL, "2", 1},
40     {"at90s2313", "__AVR_AT90S2313__", "2", 1},
41     {"at90s2323", "__AVR_AT90S2323__", "2", 1},
42     {"at90s2333", "__AVR_AT90S2333__", "2", 1},
43     {"at90s2343", "__AVR_AT90S2343__", "2", 1},
44     {"attiny22", "__AVR_ATtiny22__", "2", 1},
45     {"attiny26", "__AVR_ATtiny26__", "2", 1},
46     {"at86rf401", "__AVR_AT86RF401__", "25", 1},
47     {"at90s4414", "__AVR_AT90S4414__", "2", 1},
48     {"at90s4433", "__AVR_AT90S4433__", "2", 1},
49     {"at90s4434", "__AVR_AT90S4434__", "2", 1},
50     {"at90s8515", "__AVR_AT90S8515__", "2", 1},
51     {"at90c8534", "__AVR_AT90c8534__", "2", 1},
52     {"at90s8535", "__AVR_AT90S8535__", "2", 1},
53     {"avr25", NULL, "25", 1},
54     {"ata5272", "__AVR_ATA5272__", "25", 1},
55     {"ata6616c", "__AVR_ATA6616c__", "25", 1},
56     {"attiny13", "__AVR_ATtiny13__", "25", 1},
57     {"attiny13a", "__AVR_ATtiny13A__", "25", 1},
58     {"attiny2313", "__AVR_ATtiny2313__", "25", 1},
59     {"attiny2313a", "__AVR_ATtiny2313A__", "25", 1},
60     {"attiny24", "__AVR_ATtiny24__", "25", 1},
61     {"attiny24a", "__AVR_ATtiny24A__", "25", 1},
62     {"attiny4313", "__AVR_ATtiny4313__", "25", 1},
63     {"attiny44", "__AVR_ATtiny44__", "25", 1},
64     {"attiny44a", "__AVR_ATtiny44A__", "25", 1},
65     {"attiny84", "__AVR_ATtiny84__", "25", 1},
66     {"attiny84a", "__AVR_ATtiny84A__", "25", 1},
67     {"attiny25", "__AVR_ATtiny25__", "25", 1},
68     {"attiny45", "__AVR_ATtiny45__", "25", 1},
69     {"attiny85", "__AVR_ATtiny85__", "25", 1},
70     {"attiny261", "__AVR_ATtiny261__", "25", 1},
71     {"attiny261a", "__AVR_ATtiny261A__", "25", 1},
72     {"attiny441", "__AVR_ATtiny441__", "25", 1},
73     {"attiny461", "__AVR_ATtiny461__", "25", 1},
74     {"attiny461a", "__AVR_ATtiny461A__", "25", 1},
75     {"attiny841", "__AVR_ATtiny841__", "25", 1},
76     {"attiny861", "__AVR_ATtiny861__", "25", 1},
77     {"attiny861a", "__AVR_ATtiny861A__", "25", 1},
78     {"attiny87", "__AVR_ATtiny87__", "25", 1},
79     {"attiny43u", "__AVR_ATtiny43U__", "25", 1},
80     {"attiny48", "__AVR_ATtiny48__", "25", 1},
81     {"attiny88", "__AVR_ATtiny88__", "25", 1},
82     {"attiny828", "__AVR_ATtiny828__", "25", 1},
83     {"avr3", NULL, "3", 1},
84     {"at43usb355", "__AVR_AT43USB355__", "3", 1},
85     {"at76c711", "__AVR_AT76C711__", "3", 1},
86     {"avr31", NULL, "31", 1},
87     {"atmega103", "__AVR_ATmega103__", "31", 1},
88     {"at43usb320", "__AVR_AT43USB320__", "31", 1},
89     {"avr35", NULL, "35", 1},
90     {"attiny167", "__AVR_ATtiny167__", "35", 1},
91     {"at90usb82", "__AVR_AT90USB82__", "35", 1},
92     {"at90usb162", "__AVR_AT90USB162__", "35", 1},
93     {"ata5505", "__AVR_ATA5505__", "35", 1},
94     {"ata6617c", "__AVR_ATA6617C__", "35", 1},
95     {"ata664251", "__AVR_ATA664251__", "35", 1},
96     {"atmega8u2", "__AVR_ATmega8U2__", "35", 1},
97     {"atmega16u2", "__AVR_ATmega16U2__", "35", 1},
98     {"atmega32u2", "__AVR_ATmega32U2__", "35", 1},
99     {"attiny1634", "__AVR_ATtiny1634__", "35", 1},
100     {"avr4", NULL, "4", 1},
101     {"atmega8", "__AVR_ATmega8__", "4", 1},
102     {"ata6289", "__AVR_ATA6289__", "4", 1},
103     {"atmega8a", "__AVR_ATmega8A__", "4", 1},
104     {"ata6285", "__AVR_ATA6285__", "4", 1},
105     {"ata6286", "__AVR_ATA6286__", "4", 1},
106     {"ata6612c", "__AVR_ATA6612C__", "4", 1},
107     {"atmega48", "__AVR_ATmega48__", "4", 1},
108     {"atmega48a", "__AVR_ATmega48A__", "4", 1},
109     {"atmega48pa", "__AVR_ATmega48PA__", "4", 1},
110     {"atmega48pb", "__AVR_ATmega48PB__", "4", 1},
111     {"atmega48p", "__AVR_ATmega48P__", "4", 1},
112     {"atmega88", "__AVR_ATmega88__", "4", 1},
113     {"atmega88a", "__AVR_ATmega88A__", "4", 1},
114     {"atmega88p", "__AVR_ATmega88P__", "4", 1},
115     {"atmega88pa", "__AVR_ATmega88PA__", "4", 1},
116     {"atmega88pb", "__AVR_ATmega88PB__", "4", 1},
117     {"atmega8515", "__AVR_ATmega8515__", "4", 1},
118     {"atmega8535", "__AVR_ATmega8535__", "4", 1},
119     {"atmega8hva", "__AVR_ATmega8HVA__", "4", 1},
120     {"at90pwm1", "__AVR_AT90PWM1__", "4", 1},
121     {"at90pwm2", "__AVR_AT90PWM2__", "4", 1},
122     {"at90pwm2b", "__AVR_AT90PWM2B__", "4", 1},
123     {"at90pwm3", "__AVR_AT90PWM3__", "4", 1},
124     {"at90pwm3b", "__AVR_AT90PWM3B__", "4", 1},
125     {"at90pwm81", "__AVR_AT90PWM81__", "4", 1},
126     {"avr5", NULL, "5", 1},
127     {"ata5702m322", "__AVR_ATA5702M322__", "5", 1},
128     {"ata5782", "__AVR_ATA5782__", "5", 1},
129     {"ata5790", "__AVR_ATA5790__", "5", 1},
130     {"ata5790n", "__AVR_ATA5790N__", "5", 1},
131     {"ata5791", "__AVR_ATA5791__", "5", 1},
132     {"ata5795", "__AVR_ATA5795__", "5", 1},
133     {"ata5831", "__AVR_ATA5831__", "5", 1},
134     {"ata6613c", "__AVR_ATA6613C__", "5", 1},
135     {"ata6614q", "__AVR_ATA6614Q__", "5", 1},
136     {"ata8210", "__AVR_ATA8210__", "5", 1},
137     {"ata8510", "__AVR_ATA8510__", "5", 1},
138     {"atmega16", "__AVR_ATmega16__", "5", 1},
139     {"atmega16a", "__AVR_ATmega16A__", "5", 1},
140     {"atmega161", "__AVR_ATmega161__", "5", 1},
141     {"atmega162", "__AVR_ATmega162__", "5", 1},
142     {"atmega163", "__AVR_ATmega163__", "5", 1},
143     {"atmega164a", "__AVR_ATmega164A__", "5", 1},
144     {"atmega164p", "__AVR_ATmega164P__", "5", 1},
145     {"atmega164pa", "__AVR_ATmega164PA__", "5", 1},
146     {"atmega165", "__AVR_ATmega165__", "5", 1},
147     {"atmega165a", "__AVR_ATmega165A__", "5", 1},
148     {"atmega165p", "__AVR_ATmega165P__", "5", 1},
149     {"atmega165pa", "__AVR_ATmega165PA__", "5", 1},
150     {"atmega168", "__AVR_ATmega168__", "5", 1},
151     {"atmega168a", "__AVR_ATmega168A__", "5", 1},
152     {"atmega168p", "__AVR_ATmega168P__", "5", 1},
153     {"atmega168pa", "__AVR_ATmega168PA__", "5", 1},
154     {"atmega168pb", "__AVR_ATmega168PB__", "5", 1},
155     {"atmega169", "__AVR_ATmega169__", "5", 1},
156     {"atmega169a", "__AVR_ATmega169A__", "5", 1},
157     {"atmega169p", "__AVR_ATmega169P__", "5", 1},
158     {"atmega169pa", "__AVR_ATmega169PA__", "5", 1},
159     {"atmega32", "__AVR_ATmega32__", "5", 1},
160     {"atmega32a", "__AVR_ATmega32A__", "5", 1},
161     {"atmega323", "__AVR_ATmega323__", "5", 1},
162     {"atmega324a", "__AVR_ATmega324A__", "5", 1},
163     {"atmega324p", "__AVR_ATmega324P__", "5", 1},
164     {"atmega324pa", "__AVR_ATmega324PA__", "5", 1},
165     {"atmega324pb", "__AVR_ATmega324PB__", "5", 1},
166     {"atmega325", "__AVR_ATmega325__", "5", 1},
167     {"atmega325a", "__AVR_ATmega325A__", "5", 1},
168     {"atmega325p", "__AVR_ATmega325P__", "5", 1},
169     {"atmega325pa", "__AVR_ATmega325PA__", "5", 1},
170     {"atmega3250", "__AVR_ATmega3250__", "5", 1},
171     {"atmega3250a", "__AVR_ATmega3250A__", "5", 1},
172     {"atmega3250p", "__AVR_ATmega3250P__", "5", 1},
173     {"atmega3250pa", "__AVR_ATmega3250PA__", "5", 1},
174     {"atmega328", "__AVR_ATmega328__", "5", 1},
175     {"atmega328p", "__AVR_ATmega328P__", "5", 1},
176     {"atmega328pb", "__AVR_ATmega328PB__", "5", 1},
177     {"atmega329", "__AVR_ATmega329__", "5", 1},
178     {"atmega329a", "__AVR_ATmega329A__", "5", 1},
179     {"atmega329p", "__AVR_ATmega329P__", "5", 1},
180     {"atmega329pa", "__AVR_ATmega329PA__", "5", 1},
181     {"atmega3290", "__AVR_ATmega3290__", "5", 1},
182     {"atmega3290a", "__AVR_ATmega3290A__", "5", 1},
183     {"atmega3290p", "__AVR_ATmega3290P__", "5", 1},
184     {"atmega3290pa", "__AVR_ATmega3290PA__", "5", 1},
185     {"atmega406", "__AVR_ATmega406__", "5", 1},
186     {"atmega64", "__AVR_ATmega64__", "5", 1},
187     {"atmega64a", "__AVR_ATmega64A__", "5", 1},
188     {"atmega640", "__AVR_ATmega640__", "5", 1},
189     {"atmega644", "__AVR_ATmega644__", "5", 1},
190     {"atmega644a", "__AVR_ATmega644A__", "5", 1},
191     {"atmega644p", "__AVR_ATmega644P__", "5", 1},
192     {"atmega644pa", "__AVR_ATmega644PA__", "5", 1},
193     {"atmega645", "__AVR_ATmega645__", "5", 1},
194     {"atmega645a", "__AVR_ATmega645A__", "5", 1},
195     {"atmega645p", "__AVR_ATmega645P__", "5", 1},
196     {"atmega649", "__AVR_ATmega649__", "5", 1},
197     {"atmega649a", "__AVR_ATmega649A__", "5", 1},
198     {"atmega649p", "__AVR_ATmega649P__", "5", 1},
199     {"atmega6450", "__AVR_ATmega6450__", "5", 1},
200     {"atmega6450a", "__AVR_ATmega6450A__", "5", 1},
201     {"atmega6450p", "__AVR_ATmega6450P__", "5", 1},
202     {"atmega6490", "__AVR_ATmega6490__", "5", 1},
203     {"atmega6490a", "__AVR_ATmega6490A__", "5", 1},
204     {"atmega6490p", "__AVR_ATmega6490P__", "5", 1},
205     {"atmega64rfr2", "__AVR_ATmega64RFR2__", "5", 1},
206     {"atmega644rfr2", "__AVR_ATmega644RFR2__", "5", 1},
207     {"atmega16hva", "__AVR_ATmega16HVA__", "5", 1},
208     {"atmega16hva2", "__AVR_ATmega16HVA2__", "5", 1},
209     {"atmega16hvb", "__AVR_ATmega16HVB__", "5", 1},
210     {"atmega16hvbrevb", "__AVR_ATmega16HVBREVB__", "5", 1},
211     {"atmega32hvb", "__AVR_ATmega32HVB__", "5", 1},
212     {"atmega32hvbrevb", "__AVR_ATmega32HVBREVB__", "5", 1},
213     {"atmega64hve", "__AVR_ATmega64HVE__", "5", 1},
214     {"atmega64hve2", "__AVR_ATmega64HVE2__", "5", 1},
215     {"at90can32", "__AVR_AT90CAN32__", "5", 1},
216     {"at90can64", "__AVR_AT90CAN64__", "5", 1},
217     {"at90pwm161", "__AVR_AT90PWM161__", "5", 1},
218     {"at90pwm216", "__AVR_AT90PWM216__", "5", 1},
219     {"at90pwm316", "__AVR_AT90PWM316__", "5", 1},
220     {"atmega32c1", "__AVR_ATmega32C1__", "5", 1},
221     {"atmega64c1", "__AVR_ATmega64C1__", "5", 1},
222     {"atmega16m1", "__AVR_ATmega16M1__", "5", 1},
223     {"atmega32m1", "__AVR_ATmega32M1__", "5", 1},
224     {"atmega64m1", "__AVR_ATmega64M1__", "5", 1},
225     {"atmega16u4", "__AVR_ATmega16U4__", "5", 1},
226     {"atmega32u4", "__AVR_ATmega32U4__", "5", 1},
227     {"atmega32u6", "__AVR_ATmega32U6__", "5", 1},
228     {"at90usb646", "__AVR_AT90USB646__", "5", 1},
229     {"at90usb647", "__AVR_AT90USB647__", "5", 1},
230     {"at90scr100", "__AVR_AT90SCR100__", "5", 1},
231     {"at94k", "__AVR_AT94K__", "5", 1},
232     {"m3000", "__AVR_AT000__", "5", 1},
233     {"avr51", NULL, "51", 2},
234     {"atmega128", "__AVR_ATmega128__", "51", 2},
235     {"atmega128a", "__AVR_ATmega128A__", "51", 2},
236     {"atmega1280", "__AVR_ATmega1280__", "51", 2},
237     {"atmega1281", "__AVR_ATmega1281__", "51", 2},
238     {"atmega1284", "__AVR_ATmega1284__", "51", 2},
239     {"atmega1284p", "__AVR_ATmega1284P__", "51", 2},
240     {"atmega128rfa1", "__AVR_ATmega128RFA1__", "51", 2},
241     {"atmega128rfr2", "__AVR_ATmega128RFR2__", "51", 2},
242     {"atmega1284rfr2", "__AVR_ATmega1284RFR2__", "51", 2},
243     {"at90can128", "__AVR_AT90CAN128__", "51", 2},
244     {"at90usb1286", "__AVR_AT90USB1286__", "51", 2},
245     {"at90usb1287", "__AVR_AT90USB1287__", "51", 2},
246     {"avr6", NULL, "6", 4},
247     {"atmega2560", "__AVR_ATmega2560__", "6", 4},
248     {"atmega2561", "__AVR_ATmega2561__", "6", 4},
249     {"atmega256rfr2", "__AVR_ATmega256RFR2__", "6", 4},
250     {"atmega2564rfr2", "__AVR_ATmega2564RFR2__", "6", 4},
251     {"avrxmega2", NULL, "102", 1},
252     {"atxmega16a4", "__AVR_ATxmega16A4__", "102", 1},
253     {"atxmega16a4u", "__AVR_ATxmega16A4U__", "102", 1},
254     {"atxmega16c4", "__AVR_ATxmega16C4__", "102", 1},
255     {"atxmega16d4", "__AVR_ATxmega16D4__", "102", 1},
256     {"atxmega32a4", "__AVR_ATxmega32A4__", "102", 1},
257     {"atxmega32a4u", "__AVR_ATxmega32A4U__", "102", 1},
258     {"atxmega32c3", "__AVR_ATxmega32C3__", "102", 1},
259     {"atxmega32c4", "__AVR_ATxmega32C4__", "102", 1},
260     {"atxmega32d3", "__AVR_ATxmega32D3__", "102", 1},
261     {"atxmega32d4", "__AVR_ATxmega32D4__", "102", 1},
262     {"atxmega32e5", "__AVR_ATxmega32E5__", "102", 1},
263     {"atxmega16e5", "__AVR_ATxmega16E5__", "102", 1},
264     {"atxmega8e5", "__AVR_ATxmega8E5__", "102", 1},
265     {"avrxmega4", NULL, "104", 1},
266     {"atxmega64a3", "__AVR_ATxmega64A3__", "104", 1},
267     {"atxmega64a3u", "__AVR_ATxmega64A3U__", "104", 1},
268     {"atxmega64a4u", "__AVR_ATxmega64A4U__", "104", 1},
269     {"atxmega64b1", "__AVR_ATxmega64B1__", "104", 1},
270     {"atxmega64b3", "__AVR_ATxmega64B3__", "104", 1},
271     {"atxmega64c3", "__AVR_ATxmega64C3__", "104", 1},
272     {"atxmega64d3", "__AVR_ATxmega64D3__", "104", 1},
273     {"atxmega64d4", "__AVR_ATxmega64D4__", "104", 1},
274     {"avrxmega5", NULL, "105", 1},
275     {"atxmega64a1", "__AVR_ATxmega64A1__", "105", 1},
276     {"atxmega64a1u", "__AVR_ATxmega64A1U__", "105", 1},
277     {"avrxmega6", NULL, "106", 6},
278     {"atxmega128a3", "__AVR_ATxmega128A3__", "106", 2},
279     {"atxmega128a3u", "__AVR_ATxmega128A3U__", "106", 2},
280     {"atxmega128b1", "__AVR_ATxmega128B1__", "106", 2},
281     {"atxmega128b3", "__AVR_ATxmega128B3__", "106", 2},
282     {"atxmega128c3", "__AVR_ATxmega128C3__", "106", 2},
283     {"atxmega128d3", "__AVR_ATxmega128D3__", "106", 2},
284     {"atxmega128d4", "__AVR_ATxmega128D4__", "106", 2},
285     {"atxmega192a3", "__AVR_ATxmega192A3__", "106", 3},
286     {"atxmega192a3u", "__AVR_ATxmega192A3U__", "106", 3},
287     {"atxmega192c3", "__AVR_ATxmega192C3__", "106", 3},
288     {"atxmega192d3", "__AVR_ATxmega192D3__", "106", 3},
289     {"atxmega256a3", "__AVR_ATxmega256A3__", "106", 4},
290     {"atxmega256a3u", "__AVR_ATxmega256A3U__", "106", 4},
291     {"atxmega256a3b", "__AVR_ATxmega256A3B__", "106", 4},
292     {"atxmega256a3bu", "__AVR_ATxmega256A3BU__", "106", 4},
293     {"atxmega256c3", "__AVR_ATxmega256C3__", "106", 4},
294     {"atxmega256d3", "__AVR_ATxmega256D3__", "106", 4},
295     {"atxmega384c3", "__AVR_ATxmega384C3__", "106", 6},
296     {"atxmega384d3", "__AVR_ATxmega384D3__", "106", 6},
297     {"avrxmega7", NULL, "107", 2},
298     {"atxmega128a1", "__AVR_ATxmega128A1__", "107", 2},
299     {"atxmega128a1u", "__AVR_ATxmega128A1U__", "107", 2},
300     {"atxmega128a4u", "__AVR_ATxmega128A4U__", "107", 2},
301     {"avrtiny", NULL, "100", 0},
302     {"attiny4", "__AVR_ATtiny4__", "100", 0},
303     {"attiny5", "__AVR_ATtiny5__", "100", 0},
304     {"attiny9", "__AVR_ATtiny9__", "100", 0},
305     {"attiny10", "__AVR_ATtiny10__", "100", 0},
306     {"attiny20", "__AVR_ATtiny20__", "100", 0},
307     {"attiny40", "__AVR_ATtiny40__", "100", 0},
308     {"attiny102", "__AVR_ATtiny102__", "100", 0},
309     {"attiny104", "__AVR_ATtiny104__", "100", 0},
310     {"avrxmega3", NULL, "103", 1},
311     {"attiny202", "__AVR_ATtiny202__", "103", 1},
312     {"attiny402", "__AVR_ATtiny402__", "103", 1},
313     {"attiny204", "__AVR_ATtiny204__", "103", 1},
314     {"attiny404", "__AVR_ATtiny404__", "103", 1},
315     {"attiny804", "__AVR_ATtiny804__", "103", 1},
316     {"attiny1604", "__AVR_ATtiny1604__", "103", 1},
317     {"attiny406", "__AVR_ATtiny406__", "103", 1},
318     {"attiny806", "__AVR_ATtiny806__", "103", 1},
319     {"attiny1606", "__AVR_ATtiny1606__", "103", 1},
320     {"attiny807", "__AVR_ATtiny807__", "103", 1},
321     {"attiny1607", "__AVR_ATtiny1607__", "103", 1},
322     {"attiny212", "__AVR_ATtiny212__", "103", 1},
323     {"attiny412", "__AVR_ATtiny412__", "103", 1},
324     {"attiny214", "__AVR_ATtiny214__", "103", 1},
325     {"attiny414", "__AVR_ATtiny414__", "103", 1},
326     {"attiny814", "__AVR_ATtiny814__", "103", 1},
327     {"attiny1614", "__AVR_ATtiny1614__", "103", 1},
328     {"attiny416", "__AVR_ATtiny416__", "103", 1},
329     {"attiny816", "__AVR_ATtiny816__", "103", 1},
330     {"attiny1616", "__AVR_ATtiny1616__", "103", 1},
331     {"attiny3216", "__AVR_ATtiny3216__", "103", 1},
332     {"attiny417", "__AVR_ATtiny417__", "103", 1},
333     {"attiny817", "__AVR_ATtiny817__", "103", 1},
334     {"attiny1617", "__AVR_ATtiny1617__", "103", 1},
335     {"attiny3217", "__AVR_ATtiny3217__", "103", 1},
336     {"attiny1624", "__AVR_ATtiny1624__", "103", 1},
337     {"attiny1626", "__AVR_ATtiny1626__", "103", 1},
338     {"attiny1627", "__AVR_ATtiny1627__", "103", 1},
339     {"atmega808", "__AVR_ATmega808__", "103", 1},
340     {"atmega809", "__AVR_ATmega809__", "103", 1},
341     {"atmega1608", "__AVR_ATmega1608__", "103", 1},
342     {"atmega1609", "__AVR_ATmega1609__", "103", 1},
343     {"atmega3208", "__AVR_ATmega3208__", "103", 1},
344     {"atmega3209", "__AVR_ATmega3209__", "103", 1},
345     {"atmega4808", "__AVR_ATmega4808__", "103", 1},
346     {"atmega4809", "__AVR_ATmega4809__", "103", 1},
347 };
348 
349 } // namespace targets
350 } // namespace clang
351 
ArchHasELPM(StringRef Arch)352 static bool ArchHasELPM(StringRef Arch) {
353   return llvm::StringSwitch<bool>(Arch)
354     .Cases("31", "51", "6", true)
355     .Cases("102", "104", "105", "106", "107", true)
356     .Default(false);
357 }
358 
ArchHasELPMX(StringRef Arch)359 static bool ArchHasELPMX(StringRef Arch) {
360   return llvm::StringSwitch<bool>(Arch)
361     .Cases("51", "6", true)
362     .Cases("102", "104", "105", "106", "107", true)
363     .Default(false);
364 }
365 
ArchHasMOVW(StringRef Arch)366 static bool ArchHasMOVW(StringRef Arch) {
367   return llvm::StringSwitch<bool>(Arch)
368     .Cases("25", "35", "4", "5", "51", "6", true)
369     .Cases("102", "103", "104", "105", "106", "107", true)
370     .Default(false);
371 }
372 
ArchHasLPMX(StringRef Arch)373 static bool ArchHasLPMX(StringRef Arch) {
374   return ArchHasMOVW(Arch); // same architectures
375 }
376 
ArchHasMUL(StringRef Arch)377 static bool ArchHasMUL(StringRef Arch) {
378   return llvm::StringSwitch<bool>(Arch)
379     .Cases("4", "5", "51", "6", true)
380     .Cases("102", "103", "104", "105", "106", "107", true)
381     .Default(false);
382 }
383 
ArchHasJMPCALL(StringRef Arch)384 static bool ArchHasJMPCALL(StringRef Arch) {
385   return llvm::StringSwitch<bool>(Arch)
386     .Cases("3", "31", "35", "5", "51", "6", true)
387     .Cases("102", "103", "104", "105", "106", "107", true)
388     .Default(false);
389 }
390 
ArchHas3BytePC(StringRef Arch)391 static bool ArchHas3BytePC(StringRef Arch) {
392   // These devices have more than 128kB of program memory.
393   // Note:
394   //   - Not fully correct for arch 106: only about half the chips have more
395   //     than 128kB program memory and therefore a 3 byte PC.
396   //   - Doesn't match GCC entirely: avr-gcc thinks arch 107 goes beyond 128kB
397   //     but in fact it doesn't.
398   return llvm::StringSwitch<bool>(Arch)
399     .Case("6", true)
400     .Case("106", true)
401     .Default(false);
402 }
403 
isValidCPUName(StringRef Name) const404 bool AVRTargetInfo::isValidCPUName(StringRef Name) const {
405   return llvm::any_of(
406       AVRMcus, [&](const MCUInfo &Info) { return Info.Name == Name; });
407 }
408 
fillValidCPUList(SmallVectorImpl<StringRef> & Values) const409 void AVRTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
410   for (const MCUInfo &Info : AVRMcus)
411     Values.push_back(Info.Name);
412 }
413 
setCPU(const std::string & Name)414 bool AVRTargetInfo::setCPU(const std::string &Name) {
415   // Set the ABI field based on the device or family name.
416   auto It = llvm::find_if(
417       AVRMcus, [&](const MCUInfo &Info) { return Info.Name == Name; });
418   if (It != std::end(AVRMcus)) {
419     CPU = Name;
420     ABI = (It->Arch == "100") ? "avrtiny" : "avr";
421     DefineName = It->DefineName;
422     Arch = It->Arch;
423     NumFlashBanks = It->NumFlashBanks;
424     return true;
425   }
426 
427   // Parameter Name is neither valid family name nor valid device name.
428   return false;
429 }
430 
431 std::optional<std::string>
handleAsmEscapedChar(char EscChar) const432 AVRTargetInfo::handleAsmEscapedChar(char EscChar) const {
433   switch (EscChar) {
434   // "%~" represents for 'r' depends on the device has long jump/call.
435   case '~':
436     return ArchHasJMPCALL(Arch) ? std::string("") : std::string(1, 'r');
437 
438   // "%!" represents for 'e' depends on the PC register size.
439   case '!':
440     return ArchHas3BytePC(Arch) ? std::string(1, 'e') : std::string("");
441 
442   // This is an invalid escape character for AVR.
443   default:
444     return std::nullopt;
445   }
446 }
447 
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const448 void AVRTargetInfo::getTargetDefines(const LangOptions &Opts,
449                                      MacroBuilder &Builder) const {
450   Builder.defineMacro("AVR");
451   Builder.defineMacro("__AVR");
452   Builder.defineMacro("__AVR__");
453 
454   if (ABI == "avrtiny")
455     Builder.defineMacro("__AVR_TINY__", "1");
456 
457   if (DefineName.size() != 0)
458       Builder.defineMacro(DefineName);
459 
460   Builder.defineMacro("__AVR_ARCH__", Arch);
461 
462   // TODO: perhaps we should use the information from AVRDevices.td instead?
463   if (ArchHasELPM(Arch))
464     Builder.defineMacro("__AVR_HAVE_ELPM__");
465   if (ArchHasELPMX(Arch))
466     Builder.defineMacro("__AVR_HAVE_ELPMX__");
467   if (ArchHasMOVW(Arch))
468     Builder.defineMacro("__AVR_HAVE_MOVW__");
469   if (ArchHasLPMX(Arch))
470     Builder.defineMacro("__AVR_HAVE_LPMX__");
471   if (ArchHasMUL(Arch))
472     Builder.defineMacro("__AVR_HAVE_MUL__");
473   if (ArchHasJMPCALL(Arch))
474     Builder.defineMacro("__AVR_HAVE_JMP_CALL__");
475   if (ArchHas3BytePC(Arch)) {
476     // Note: some devices do support eijmp/eicall even though this macro isn't
477     // set. This is the case if they have less than 128kB flash and so
478     // eijmp/eicall isn't very useful anyway. (This matches gcc, although it's
479     // debatable whether we should be bug-compatible in this case).
480     Builder.defineMacro("__AVR_HAVE_EIJMP_EICALL__");
481     Builder.defineMacro("__AVR_3_BYTE_PC__");
482   } else {
483     Builder.defineMacro("__AVR_2_BYTE_PC__");
484   }
485 
486   if (NumFlashBanks >= 1)
487     Builder.defineMacro("__flash", "__attribute__((__address_space__(1)))");
488   if (NumFlashBanks >= 2)
489     Builder.defineMacro("__flash1", "__attribute__((__address_space__(2)))");
490   if (NumFlashBanks >= 3)
491     Builder.defineMacro("__flash2", "__attribute__((__address_space__(3)))");
492   if (NumFlashBanks >= 4)
493     Builder.defineMacro("__flash3", "__attribute__((__address_space__(4)))");
494   if (NumFlashBanks >= 5)
495     Builder.defineMacro("__flash4", "__attribute__((__address_space__(5)))");
496   if (NumFlashBanks >= 6)
497     Builder.defineMacro("__flash5", "__attribute__((__address_space__(6)))");
498 }
499