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 const int NumFlashBanks; // Set to 0 for the devices do not support LPM/ELPM. 28 bool IsTiny; // Set to true for the devices belong to the avrtiny family. 29 }; 30 31 // This list should be kept up-to-date with AVRDevices.td in LLVM. 32 static MCUInfo AVRMcus[] = { 33 {"at90s1200", "__AVR_AT90S1200__", 0}, 34 {"attiny11", "__AVR_ATtiny11__", 0}, 35 {"attiny12", "__AVR_ATtiny12__", 0}, 36 {"attiny15", "__AVR_ATtiny15__", 0}, 37 {"attiny28", "__AVR_ATtiny28__", 0}, 38 {"at90s2313", "__AVR_AT90S2313__", 1}, 39 {"at90s2323", "__AVR_AT90S2323__", 1}, 40 {"at90s2333", "__AVR_AT90S2333__", 1}, 41 {"at90s2343", "__AVR_AT90S2343__", 1}, 42 {"attiny22", "__AVR_ATtiny22__", 1}, 43 {"attiny26", "__AVR_ATtiny26__", 1}, 44 {"at86rf401", "__AVR_AT86RF401__", 1}, 45 {"at90s4414", "__AVR_AT90S4414__", 1}, 46 {"at90s4433", "__AVR_AT90S4433__", 1}, 47 {"at90s4434", "__AVR_AT90S4434__", 1}, 48 {"at90s8515", "__AVR_AT90S8515__", 1}, 49 {"at90c8534", "__AVR_AT90c8534__", 1}, 50 {"at90s8535", "__AVR_AT90S8535__", 1}, 51 {"ata5272", "__AVR_ATA5272__", 1}, 52 {"attiny13", "__AVR_ATtiny13__", 1}, 53 {"attiny13a", "__AVR_ATtiny13A__", 1}, 54 {"attiny2313", "__AVR_ATtiny2313__", 1}, 55 {"attiny2313a", "__AVR_ATtiny2313A__", 1}, 56 {"attiny24", "__AVR_ATtiny24__", 1}, 57 {"attiny24a", "__AVR_ATtiny24A__", 1}, 58 {"attiny4313", "__AVR_ATtiny4313__", 1}, 59 {"attiny44", "__AVR_ATtiny44__", 1}, 60 {"attiny44a", "__AVR_ATtiny44A__", 1}, 61 {"attiny84", "__AVR_ATtiny84__", 1}, 62 {"attiny84a", "__AVR_ATtiny84A__", 1}, 63 {"attiny25", "__AVR_ATtiny25__", 1}, 64 {"attiny45", "__AVR_ATtiny45__", 1}, 65 {"attiny85", "__AVR_ATtiny85__", 1}, 66 {"attiny261", "__AVR_ATtiny261__", 1}, 67 {"attiny261a", "__AVR_ATtiny261A__", 1}, 68 {"attiny441", "__AVR_ATtiny441__", 1}, 69 {"attiny461", "__AVR_ATtiny461__", 1}, 70 {"attiny461a", "__AVR_ATtiny461A__", 1}, 71 {"attiny841", "__AVR_ATtiny841__", 1}, 72 {"attiny861", "__AVR_ATtiny861__", 1}, 73 {"attiny861a", "__AVR_ATtiny861A__", 1}, 74 {"attiny87", "__AVR_ATtiny87__", 1}, 75 {"attiny43u", "__AVR_ATtiny43U__", 1}, 76 {"attiny48", "__AVR_ATtiny48__", 1}, 77 {"attiny88", "__AVR_ATtiny88__", 1}, 78 {"attiny828", "__AVR_ATtiny828__", 1}, 79 {"at43usb355", "__AVR_AT43USB355__", 1}, 80 {"at76c711", "__AVR_AT76C711__", 1}, 81 {"atmega103", "__AVR_ATmega103__", 1}, 82 {"at43usb320", "__AVR_AT43USB320__", 1}, 83 {"attiny167", "__AVR_ATtiny167__", 1}, 84 {"at90usb82", "__AVR_AT90USB82__", 1}, 85 {"at90usb162", "__AVR_AT90USB162__", 1}, 86 {"ata5505", "__AVR_ATA5505__", 1}, 87 {"atmega8u2", "__AVR_ATmega8U2__", 1}, 88 {"atmega16u2", "__AVR_ATmega16U2__", 1}, 89 {"atmega32u2", "__AVR_ATmega32U2__", 1}, 90 {"attiny1634", "__AVR_ATtiny1634__", 1}, 91 {"atmega8", "__AVR_ATmega8__", 1}, 92 {"ata6289", "__AVR_ATA6289__", 1}, 93 {"atmega8a", "__AVR_ATmega8A__", 1}, 94 {"ata6285", "__AVR_ATA6285__", 1}, 95 {"ata6286", "__AVR_ATA6286__", 1}, 96 {"atmega48", "__AVR_ATmega48__", 1}, 97 {"atmega48a", "__AVR_ATmega48A__", 1}, 98 {"atmega48pa", "__AVR_ATmega48PA__", 1}, 99 {"atmega48pb", "__AVR_ATmega48PB__", 1}, 100 {"atmega48p", "__AVR_ATmega48P__", 1}, 101 {"atmega88", "__AVR_ATmega88__", 1}, 102 {"atmega88a", "__AVR_ATmega88A__", 1}, 103 {"atmega88p", "__AVR_ATmega88P__", 1}, 104 {"atmega88pa", "__AVR_ATmega88PA__", 1}, 105 {"atmega88pb", "__AVR_ATmega88PB__", 1}, 106 {"atmega8515", "__AVR_ATmega8515__", 1}, 107 {"atmega8535", "__AVR_ATmega8535__", 1}, 108 {"atmega8hva", "__AVR_ATmega8HVA__", 1}, 109 {"at90pwm1", "__AVR_AT90PWM1__", 1}, 110 {"at90pwm2", "__AVR_AT90PWM2__", 1}, 111 {"at90pwm2b", "__AVR_AT90PWM2B__", 1}, 112 {"at90pwm3", "__AVR_AT90PWM3__", 1}, 113 {"at90pwm3b", "__AVR_AT90PWM3B__", 1}, 114 {"at90pwm81", "__AVR_AT90PWM81__", 1}, 115 {"ata5790", "__AVR_ATA5790__", 1}, 116 {"ata5795", "__AVR_ATA5795__", 1}, 117 {"atmega16", "__AVR_ATmega16__", 1}, 118 {"atmega16a", "__AVR_ATmega16A__", 1}, 119 {"atmega161", "__AVR_ATmega161__", 1}, 120 {"atmega162", "__AVR_ATmega162__", 1}, 121 {"atmega163", "__AVR_ATmega163__", 1}, 122 {"atmega164a", "__AVR_ATmega164A__", 1}, 123 {"atmega164p", "__AVR_ATmega164P__", 1}, 124 {"atmega164pa", "__AVR_ATmega164PA__", 1}, 125 {"atmega165", "__AVR_ATmega165__", 1}, 126 {"atmega165a", "__AVR_ATmega165A__", 1}, 127 {"atmega165p", "__AVR_ATmega165P__", 1}, 128 {"atmega165pa", "__AVR_ATmega165PA__", 1}, 129 {"atmega168", "__AVR_ATmega168__", 1}, 130 {"atmega168a", "__AVR_ATmega168A__", 1}, 131 {"atmega168p", "__AVR_ATmega168P__", 1}, 132 {"atmega168pa", "__AVR_ATmega168PA__", 1}, 133 {"atmega168pb", "__AVR_ATmega168PB__", 1}, 134 {"atmega169", "__AVR_ATmega169__", 1}, 135 {"atmega169a", "__AVR_ATmega169A__", 1}, 136 {"atmega169p", "__AVR_ATmega169P__", 1}, 137 {"atmega169pa", "__AVR_ATmega169PA__", 1}, 138 {"atmega32", "__AVR_ATmega32__", 1}, 139 {"atmega32a", "__AVR_ATmega32A__", 1}, 140 {"atmega323", "__AVR_ATmega323__", 1}, 141 {"atmega324a", "__AVR_ATmega324A__", 1}, 142 {"atmega324p", "__AVR_ATmega324P__", 1}, 143 {"atmega324pa", "__AVR_ATmega324PA__", 1}, 144 {"atmega324pb", "__AVR_ATmega324PB__", 1}, 145 {"atmega325", "__AVR_ATmega325__", 1}, 146 {"atmega325a", "__AVR_ATmega325A__", 1}, 147 {"atmega325p", "__AVR_ATmega325P__", 1}, 148 {"atmega325pa", "__AVR_ATmega325PA__", 1}, 149 {"atmega3250", "__AVR_ATmega3250__", 1}, 150 {"atmega3250a", "__AVR_ATmega3250A__", 1}, 151 {"atmega3250p", "__AVR_ATmega3250P__", 1}, 152 {"atmega3250pa", "__AVR_ATmega3250PA__", 1}, 153 {"atmega328", "__AVR_ATmega328__", 1}, 154 {"atmega328p", "__AVR_ATmega328P__", 1}, 155 {"atmega328pb", "__AVR_ATmega328PB__", 1}, 156 {"atmega329", "__AVR_ATmega329__", 1}, 157 {"atmega329a", "__AVR_ATmega329A__", 1}, 158 {"atmega329p", "__AVR_ATmega329P__", 1}, 159 {"atmega329pa", "__AVR_ATmega329PA__", 1}, 160 {"atmega3290", "__AVR_ATmega3290__", 1}, 161 {"atmega3290a", "__AVR_ATmega3290A__", 1}, 162 {"atmega3290p", "__AVR_ATmega3290P__", 1}, 163 {"atmega3290pa", "__AVR_ATmega3290PA__", 1}, 164 {"atmega406", "__AVR_ATmega406__", 1}, 165 {"atmega64", "__AVR_ATmega64__", 1}, 166 {"atmega64a", "__AVR_ATmega64A__", 1}, 167 {"atmega640", "__AVR_ATmega640__", 1}, 168 {"atmega644", "__AVR_ATmega644__", 1}, 169 {"atmega644a", "__AVR_ATmega644A__", 1}, 170 {"atmega644p", "__AVR_ATmega644P__", 1}, 171 {"atmega644pa", "__AVR_ATmega644PA__", 1}, 172 {"atmega645", "__AVR_ATmega645__", 1}, 173 {"atmega645a", "__AVR_ATmega645A__", 1}, 174 {"atmega645p", "__AVR_ATmega645P__", 1}, 175 {"atmega649", "__AVR_ATmega649__", 1}, 176 {"atmega649a", "__AVR_ATmega649A__", 1}, 177 {"atmega649p", "__AVR_ATmega649P__", 1}, 178 {"atmega6450", "__AVR_ATmega6450__", 1}, 179 {"atmega6450a", "__AVR_ATmega6450A__", 1}, 180 {"atmega6450p", "__AVR_ATmega6450P__", 1}, 181 {"atmega6490", "__AVR_ATmega6490__", 1}, 182 {"atmega6490a", "__AVR_ATmega6490A__", 1}, 183 {"atmega6490p", "__AVR_ATmega6490P__", 1}, 184 {"atmega64rfr2", "__AVR_ATmega64RFR2__", 1}, 185 {"atmega644rfr2", "__AVR_ATmega644RFR2__", 1}, 186 {"atmega16hva", "__AVR_ATmega16HVA__", 1}, 187 {"atmega16hva2", "__AVR_ATmega16HVA2__", 1}, 188 {"atmega16hvb", "__AVR_ATmega16HVB__", 1}, 189 {"atmega16hvbrevb", "__AVR_ATmega16HVBREVB__", 1}, 190 {"atmega32hvb", "__AVR_ATmega32HVB__", 1}, 191 {"atmega32hvbrevb", "__AVR_ATmega32HVBREVB__", 1}, 192 {"atmega64hve", "__AVR_ATmega64HVE__", 1}, 193 {"at90can32", "__AVR_AT90CAN32__", 1}, 194 {"at90can64", "__AVR_AT90CAN64__", 1}, 195 {"at90pwm161", "__AVR_AT90PWM161__", 1}, 196 {"at90pwm216", "__AVR_AT90PWM216__", 1}, 197 {"at90pwm316", "__AVR_AT90PWM316__", 1}, 198 {"atmega32c1", "__AVR_ATmega32C1__", 1}, 199 {"atmega64c1", "__AVR_ATmega64C1__", 1}, 200 {"atmega16m1", "__AVR_ATmega16M1__", 1}, 201 {"atmega32m1", "__AVR_ATmega32M1__", 1}, 202 {"atmega64m1", "__AVR_ATmega64M1__", 1}, 203 {"atmega16u4", "__AVR_ATmega16U4__", 1}, 204 {"atmega32u4", "__AVR_ATmega32U4__", 1}, 205 {"atmega32u6", "__AVR_ATmega32U6__", 1}, 206 {"at90usb646", "__AVR_AT90USB646__", 1}, 207 {"at90usb647", "__AVR_AT90USB647__", 1}, 208 {"at90scr100", "__AVR_AT90SCR100__", 1}, 209 {"at94k", "__AVR_AT94K__", 1}, 210 {"m3000", "__AVR_AT000__", 1}, 211 {"atmega128", "__AVR_ATmega128__", 2}, 212 {"atmega128a", "__AVR_ATmega128A__", 2}, 213 {"atmega1280", "__AVR_ATmega1280__", 2}, 214 {"atmega1281", "__AVR_ATmega1281__", 2}, 215 {"atmega1284", "__AVR_ATmega1284__", 2}, 216 {"atmega1284p", "__AVR_ATmega1284P__", 2}, 217 {"atmega128rfa1", "__AVR_ATmega128RFA1__", 2}, 218 {"atmega128rfr2", "__AVR_ATmega128RFR2__", 2}, 219 {"atmega1284rfr2", "__AVR_ATmega1284RFR2__", 2}, 220 {"at90can128", "__AVR_AT90CAN128__", 2}, 221 {"at90usb1286", "__AVR_AT90USB1286__", 2}, 222 {"at90usb1287", "__AVR_AT90USB1287__", 2}, 223 {"atmega2560", "__AVR_ATmega2560__", 4}, 224 {"atmega2561", "__AVR_ATmega2561__", 4}, 225 {"atmega256rfr2", "__AVR_ATmega256RFR2__", 4}, 226 {"atmega2564rfr2", "__AVR_ATmega2564RFR2__", 4}, 227 {"atxmega16a4", "__AVR_ATxmega16A4__", 1}, 228 {"atxmega16a4u", "__AVR_ATxmega16A4U__", 1}, 229 {"atxmega16c4", "__AVR_ATxmega16C4__", 1}, 230 {"atxmega16d4", "__AVR_ATxmega16D4__", 1}, 231 {"atxmega32a4", "__AVR_ATxmega32A4__", 1}, 232 {"atxmega32a4u", "__AVR_ATxmega32A4U__", 1}, 233 {"atxmega32c4", "__AVR_ATxmega32C4__", 1}, 234 {"atxmega32d4", "__AVR_ATxmega32D4__", 1}, 235 {"atxmega32e5", "__AVR_ATxmega32E5__", 1}, 236 {"atxmega16e5", "__AVR_ATxmega16E5__", 1}, 237 {"atxmega8e5", "__AVR_ATxmega8E5__", 1}, 238 {"atxmega32x1", "__AVR_ATxmega32X1__", 1}, 239 {"atxmega64a3", "__AVR_ATxmega64A3__", 1}, 240 {"atxmega64a3u", "__AVR_ATxmega64A3U__", 1}, 241 {"atxmega64a4u", "__AVR_ATxmega64A4U__", 1}, 242 {"atxmega64b1", "__AVR_ATxmega64B1__", 1}, 243 {"atxmega64b3", "__AVR_ATxmega64B3__", 1}, 244 {"atxmega64c3", "__AVR_ATxmega64C3__", 1}, 245 {"atxmega64d3", "__AVR_ATxmega64D3__", 1}, 246 {"atxmega64d4", "__AVR_ATxmega64D4__", 1}, 247 {"atxmega64a1", "__AVR_ATxmega64A1__", 1}, 248 {"atxmega64a1u", "__AVR_ATxmega64A1U__", 1}, 249 {"atxmega128a3", "__AVR_ATxmega128A3__", 2}, 250 {"atxmega128a3u", "__AVR_ATxmega128A3U__", 2}, 251 {"atxmega128b1", "__AVR_ATxmega128B1__", 2}, 252 {"atxmega128b3", "__AVR_ATxmega128B3__", 2}, 253 {"atxmega128c3", "__AVR_ATxmega128C3__", 2}, 254 {"atxmega128d3", "__AVR_ATxmega128D3__", 2}, 255 {"atxmega128d4", "__AVR_ATxmega128D4__", 2}, 256 {"atxmega192a3", "__AVR_ATxmega192A3__", 3}, 257 {"atxmega192a3u", "__AVR_ATxmega192A3U__", 3}, 258 {"atxmega192c3", "__AVR_ATxmega192C3__", 3}, 259 {"atxmega192d3", "__AVR_ATxmega192D3__", 3}, 260 {"atxmega256a3", "__AVR_ATxmega256A3__", 4}, 261 {"atxmega256a3u", "__AVR_ATxmega256A3U__", 4}, 262 {"atxmega256a3b", "__AVR_ATxmega256A3B__", 4}, 263 {"atxmega256a3bu", "__AVR_ATxmega256A3BU__", 4}, 264 {"atxmega256c3", "__AVR_ATxmega256C3__", 4}, 265 {"atxmega256d3", "__AVR_ATxmega256D3__", 4}, 266 {"atxmega384c3", "__AVR_ATxmega384C3__", 6}, 267 {"atxmega384d3", "__AVR_ATxmega384D3__", 6}, 268 {"atxmega128a1", "__AVR_ATxmega128A1__", 2}, 269 {"atxmega128a1u", "__AVR_ATxmega128A1U__", 2}, 270 {"atxmega128a4u", "__AVR_ATxmega128A4U__", 2}, 271 {"attiny4", "__AVR_ATtiny4__", 0, true}, 272 {"attiny5", "__AVR_ATtiny5__", 0, true}, 273 {"attiny9", "__AVR_ATtiny9__", 0, true}, 274 {"attiny10", "__AVR_ATtiny10__", 0, true}, 275 {"attiny20", "__AVR_ATtiny20__", 0, true}, 276 {"attiny40", "__AVR_ATtiny40__", 0, true}, 277 {"attiny102", "__AVR_ATtiny102__", 0, true}, 278 {"attiny104", "__AVR_ATtiny104__", 0, true}, 279 {"attiny202", "__AVR_ATtiny202__", 1}, 280 {"attiny402", "__AVR_ATtiny402__", 1}, 281 {"attiny204", "__AVR_ATtiny204__", 1}, 282 {"attiny404", "__AVR_ATtiny404__", 1}, 283 {"attiny804", "__AVR_ATtiny804__", 1}, 284 {"attiny1604", "__AVR_ATtiny1604__", 1}, 285 {"attiny406", "__AVR_ATtiny406__", 1}, 286 {"attiny806", "__AVR_ATtiny806__", 1}, 287 {"attiny1606", "__AVR_ATtiny1606__", 1}, 288 {"attiny807", "__AVR_ATtiny807__", 1}, 289 {"attiny1607", "__AVR_ATtiny1607__", 1}, 290 {"attiny212", "__AVR_ATtiny212__", 1}, 291 {"attiny412", "__AVR_ATtiny412__", 1}, 292 {"attiny214", "__AVR_ATtiny214__", 1}, 293 {"attiny414", "__AVR_ATtiny414__", 1}, 294 {"attiny814", "__AVR_ATtiny814__", 1}, 295 {"attiny1614", "__AVR_ATtiny1614__", 1}, 296 {"attiny416", "__AVR_ATtiny416__", 1}, 297 {"attiny816", "__AVR_ATtiny816__", 1}, 298 {"attiny1616", "__AVR_ATtiny1616__", 1}, 299 {"attiny3216", "__AVR_ATtiny3216__", 1}, 300 {"attiny417", "__AVR_ATtiny417__", 1}, 301 {"attiny817", "__AVR_ATtiny817__", 1}, 302 {"attiny1617", "__AVR_ATtiny1617__", 1}, 303 {"attiny3217", "__AVR_ATtiny3217__", 1}, 304 }; 305 306 } // namespace targets 307 } // namespace clang 308 309 static constexpr llvm::StringLiteral ValidFamilyNames[] = { 310 "avr1", "avr2", "avr25", "avr3", "avr31", 311 "avr35", "avr4", "avr5", "avr51", "avr6", 312 "avrxmega1", "avrxmega2", "avrxmega3", "avrxmega4", "avrxmega5", 313 "avrxmega6", "avrxmega7", "avrtiny"}; 314 315 bool AVRTargetInfo::isValidCPUName(StringRef Name) const { 316 bool IsFamily = llvm::is_contained(ValidFamilyNames, Name); 317 318 bool IsMCU = llvm::any_of( 319 AVRMcus, [&](const MCUInfo &Info) { return Info.Name == Name; }); 320 return IsFamily || IsMCU; 321 } 322 323 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 329 bool AVRTargetInfo::setCPU(const std::string &Name) { 330 // Set the ABI and CPU fields if parameter Name is a family name. 331 if (llvm::is_contained(ValidFamilyNames, Name)) { 332 CPU = Name; 333 ABI = Name == "avrtiny" ? "avrtiny" : "avr"; 334 return true; 335 } 336 337 // Set the ABI field if parameter Name is a device name. 338 auto It = llvm::find_if( 339 AVRMcus, [&](const MCUInfo &Info) { return Info.Name == Name; }); 340 if (It != std::end(AVRMcus)) { 341 CPU = Name; 342 ABI = It->IsTiny ? "avrtiny" : "avr"; 343 return true; 344 } 345 346 // Parameter Name is neither valid family name nor valid device name. 347 return false; 348 } 349 350 void AVRTargetInfo::getTargetDefines(const LangOptions &Opts, 351 MacroBuilder &Builder) const { 352 Builder.defineMacro("AVR"); 353 Builder.defineMacro("__AVR"); 354 Builder.defineMacro("__AVR__"); 355 Builder.defineMacro("__ELF__"); 356 357 if (!this->CPU.empty()) { 358 auto It = llvm::find_if( 359 AVRMcus, [&](const MCUInfo &Info) { return Info.Name == this->CPU; }); 360 361 if (It != std::end(AVRMcus)) { 362 Builder.defineMacro(It->DefineName); 363 if (It->NumFlashBanks >= 1) 364 Builder.defineMacro("__flash", "__attribute__((address_space(1)))"); 365 if (It->NumFlashBanks >= 2) 366 Builder.defineMacro("__flash1", "__attribute__((address_space(2)))"); 367 if (It->NumFlashBanks >= 3) 368 Builder.defineMacro("__flash2", "__attribute__((address_space(3)))"); 369 if (It->NumFlashBanks >= 4) 370 Builder.defineMacro("__flash3", "__attribute__((address_space(4)))"); 371 if (It->NumFlashBanks >= 5) 372 Builder.defineMacro("__flash4", "__attribute__((address_space(5)))"); 373 if (It->NumFlashBanks >= 6) 374 Builder.defineMacro("__flash5", "__attribute__((address_space(6)))"); 375 } 376 } 377 } 378