1 //===-- AVRAsmBackend.cpp - AVR Asm Backend ------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the AVRAsmBackend class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MCTargetDesc/AVRAsmBackend.h" 15 #include "MCTargetDesc/AVRFixupKinds.h" 16 #include "MCTargetDesc/AVRMCTargetDesc.h" 17 18 #include "llvm/MC/MCAsmBackend.h" 19 #include "llvm/MC/MCAssembler.h" 20 #include "llvm/MC/MCContext.h" 21 #include "llvm/MC/MCDirectives.h" 22 #include "llvm/MC/MCELFObjectWriter.h" 23 #include "llvm/MC/MCFixupKindInfo.h" 24 #include "llvm/MC/MCObjectWriter.h" 25 #include "llvm/MC/MCSubtargetInfo.h" 26 #include "llvm/MC/MCValue.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/MathExtras.h" 29 #include "llvm/Support/raw_ostream.h" 30 31 // FIXME: we should be doing checks to make sure asm operands 32 // are not out of bounds. 33 34 namespace adjust { 35 36 using namespace llvm; 37 38 void signed_width(unsigned Width, uint64_t Value, std::string Description, 39 const MCFixup &Fixup, MCContext *Ctx = nullptr) { 40 if (!isIntN(Width, Value)) { 41 std::string Diagnostic = "out of range " + Description; 42 43 int64_t Min = minIntN(Width); 44 int64_t Max = maxIntN(Width); 45 46 Diagnostic += " (expected an integer in the range " + std::to_string(Min) + 47 " to " + std::to_string(Max) + ")"; 48 49 if (Ctx) { 50 Ctx->reportFatalError(Fixup.getLoc(), Diagnostic); 51 } else { 52 llvm_unreachable(Diagnostic.c_str()); 53 } 54 } 55 } 56 57 void unsigned_width(unsigned Width, uint64_t Value, std::string Description, 58 const MCFixup &Fixup, MCContext *Ctx = nullptr) { 59 if (!isUIntN(Width, Value)) { 60 std::string Diagnostic = "out of range " + Description; 61 62 int64_t Max = maxUIntN(Width); 63 64 Diagnostic += " (expected an integer in the range 0 to " + 65 std::to_string(Max) + ")"; 66 67 if (Ctx) { 68 Ctx->reportFatalError(Fixup.getLoc(), Diagnostic); 69 } else { 70 llvm_unreachable(Diagnostic.c_str()); 71 } 72 } 73 } 74 75 /// Adjusts the value of a branch target before fixup application. 76 void adjustBranch(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 77 MCContext *Ctx = nullptr) { 78 // We have one extra bit of precision because the value is rightshifted by 79 // one. 80 unsigned_width(Size + 1, Value, std::string("branch target"), Fixup, Ctx); 81 82 // Rightshifts the value by one. 83 AVR::fixups::adjustBranchTarget(Value); 84 } 85 86 /// Adjusts the value of a relative branch target before fixup application. 87 void adjustRelativeBranch(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 88 MCContext *Ctx = nullptr) { 89 // We have one extra bit of precision because the value is rightshifted by 90 // one. 91 signed_width(Size + 1, Value, std::string("branch target"), Fixup, Ctx); 92 93 Value -= 2; 94 95 // Rightshifts the value by one. 96 AVR::fixups::adjustBranchTarget(Value); 97 } 98 99 /// 22-bit absolute fixup. 100 /// 101 /// Resolves to: 102 /// 1001 kkkk 010k kkkk kkkk kkkk 111k kkkk 103 /// 104 /// Offset of 0 (so the result is left shifted by 3 bits before application). 105 void fixup_call(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 106 MCContext *Ctx = nullptr) { 107 adjustBranch(Size, Fixup, Value, Ctx); 108 109 auto top = Value & (0xf00000 << 6); // the top four bits 110 auto middle = Value & (0x1ffff << 5); // the middle 13 bits 111 auto bottom = Value & 0x1f; // end bottom 5 bits 112 113 Value = (top << 6) | (middle << 3) | (bottom << 0); 114 } 115 116 /// 7-bit PC-relative fixup. 117 /// 118 /// Resolves to: 119 /// 0000 00kk kkkk k000 120 /// Offset of 0 (so the result is left shifted by 3 bits before application). 121 void fixup_7_pcrel(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 122 MCContext *Ctx = nullptr) { 123 adjustRelativeBranch(Size, Fixup, Value, Ctx); 124 125 // Because the value may be negative, we must mask out the sign bits 126 Value &= 0x7f; 127 } 128 129 /// 12-bit PC-relative fixup. 130 /// Yes, the fixup is 12 bits even though the name says otherwise. 131 /// 132 /// Resolves to: 133 /// 0000 kkkk kkkk kkkk 134 /// Offset of 0 (so the result isn't left-shifted before application). 135 void fixup_13_pcrel(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 136 MCContext *Ctx = nullptr) { 137 adjustRelativeBranch(Size, Fixup, Value, Ctx); 138 139 // Because the value may be negative, we must mask out the sign bits 140 Value &= 0xfff; 141 } 142 143 /// 6-bit fixup for the immediate operand of the ADIW family of 144 /// instructions. 145 /// 146 /// Resolves to: 147 /// 0000 0000 kk00 kkkk 148 void fixup_6_adiw(const MCFixup &Fixup, uint64_t &Value, 149 MCContext *Ctx = nullptr) { 150 unsigned_width(6, Value, std::string("immediate"), Fixup, Ctx); 151 152 Value = ((Value & 0x30) << 2) | (Value & 0x0f); 153 } 154 155 /// 5-bit port number fixup on the SBIC family of instructions. 156 /// 157 /// Resolves to: 158 /// 0000 0000 AAAA A000 159 void fixup_port5(const MCFixup &Fixup, uint64_t &Value, 160 MCContext *Ctx = nullptr) { 161 unsigned_width(5, Value, std::string("port number"), Fixup, Ctx); 162 163 Value &= 0x1f; 164 165 Value <<= 3; 166 } 167 168 /// 6-bit port number fixup on the `IN` family of instructions. 169 /// 170 /// Resolves to: 171 /// 1011 0AAd dddd AAAA 172 void fixup_port6(const MCFixup &Fixup, uint64_t &Value, 173 MCContext *Ctx = nullptr) { 174 unsigned_width(6, Value, std::string("port number"), Fixup, Ctx); 175 176 Value = ((Value & 0x30) << 5) | (Value & 0x0f); 177 } 178 179 /// Adjusts a program memory address. 180 /// This is a simple right-shift. 181 void pm(uint64_t &Value) { 182 Value >>= 1; 183 } 184 185 /// Fixups relating to the LDI instruction. 186 namespace ldi { 187 188 /// Adjusts a value to fix up the immediate of an `LDI Rd, K` instruction. 189 /// 190 /// Resolves to: 191 /// 0000 KKKK 0000 KKKK 192 /// Offset of 0 (so the result isn't left-shifted before application). 193 void fixup(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 194 MCContext *Ctx = nullptr) { 195 uint64_t upper = Value & 0xf0; 196 uint64_t lower = Value & 0x0f; 197 198 Value = (upper << 4) | lower; 199 } 200 201 void neg(uint64_t &Value) { Value *= -1; } 202 203 void lo8(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 204 MCContext *Ctx = nullptr) { 205 Value &= 0xff; 206 ldi::fixup(Size, Fixup, Value, Ctx); 207 } 208 209 void hi8(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 210 MCContext *Ctx = nullptr) { 211 Value = (Value & 0xff00) >> 8; 212 ldi::fixup(Size, Fixup, Value, Ctx); 213 } 214 215 void hh8(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 216 MCContext *Ctx = nullptr) { 217 Value = (Value & 0xff0000) >> 16; 218 ldi::fixup(Size, Fixup, Value, Ctx); 219 } 220 221 void ms8(unsigned Size, const MCFixup &Fixup, uint64_t &Value, 222 MCContext *Ctx = nullptr) { 223 Value = (Value & 0xff000000) >> 24; 224 ldi::fixup(Size, Fixup, Value, Ctx); 225 } 226 227 } // end of ldi namespace 228 } // end of adjust namespace 229 230 namespace llvm { 231 232 // Prepare value for the target space for it 233 void AVRAsmBackend::adjustFixupValue(const MCFixup &Fixup, uint64_t &Value, 234 MCContext *Ctx) const { 235 // The size of the fixup in bits. 236 uint64_t Size = AVRAsmBackend::getFixupKindInfo(Fixup.getKind()).TargetSize; 237 238 unsigned Kind = Fixup.getKind(); 239 240 switch (Kind) { 241 default: 242 llvm_unreachable("unhandled fixup"); 243 case AVR::fixup_7_pcrel: 244 adjust::fixup_7_pcrel(Size, Fixup, Value, Ctx); 245 break; 246 case AVR::fixup_13_pcrel: 247 adjust::fixup_13_pcrel(Size, Fixup, Value, Ctx); 248 break; 249 case AVR::fixup_call: 250 adjust::fixup_call(Size, Fixup, Value, Ctx); 251 break; 252 case AVR::fixup_ldi: 253 adjust::ldi::fixup(Size, Fixup, Value, Ctx); 254 break; 255 case AVR::fixup_lo8_ldi: 256 case AVR::fixup_lo8_ldi_pm: 257 if (Kind == AVR::fixup_lo8_ldi_pm) adjust::pm(Value); 258 259 adjust::ldi::lo8(Size, Fixup, Value, Ctx); 260 break; 261 case AVR::fixup_hi8_ldi: 262 case AVR::fixup_hi8_ldi_pm: 263 if (Kind == AVR::fixup_hi8_ldi_pm) adjust::pm(Value); 264 265 adjust::ldi::hi8(Size, Fixup, Value, Ctx); 266 break; 267 case AVR::fixup_hh8_ldi: 268 case AVR::fixup_hh8_ldi_pm: 269 if (Kind == AVR::fixup_hh8_ldi_pm) adjust::pm(Value); 270 271 adjust::ldi::hh8(Size, Fixup, Value, Ctx); 272 break; 273 case AVR::fixup_ms8_ldi: 274 adjust::ldi::ms8(Size, Fixup, Value, Ctx); 275 break; 276 277 case AVR::fixup_lo8_ldi_neg: 278 case AVR::fixup_lo8_ldi_pm_neg: 279 if (Kind == AVR::fixup_lo8_ldi_pm_neg) adjust::pm(Value); 280 281 adjust::ldi::neg(Value); 282 adjust::ldi::lo8(Size, Fixup, Value, Ctx); 283 break; 284 case AVR::fixup_hi8_ldi_neg: 285 case AVR::fixup_hi8_ldi_pm_neg: 286 if (Kind == AVR::fixup_hi8_ldi_pm_neg) adjust::pm(Value); 287 288 adjust::ldi::neg(Value); 289 adjust::ldi::hi8(Size, Fixup, Value, Ctx); 290 break; 291 case AVR::fixup_hh8_ldi_neg: 292 case AVR::fixup_hh8_ldi_pm_neg: 293 if (Kind == AVR::fixup_hh8_ldi_pm_neg) adjust::pm(Value); 294 295 adjust::ldi::neg(Value); 296 adjust::ldi::hh8(Size, Fixup, Value, Ctx); 297 break; 298 case AVR::fixup_ms8_ldi_neg: 299 adjust::ldi::neg(Value); 300 adjust::ldi::ms8(Size, Fixup, Value, Ctx); 301 break; 302 case AVR::fixup_16: 303 adjust::unsigned_width(16, Value, std::string("port number"), Fixup, Ctx); 304 305 Value &= 0xffff; 306 break; 307 case AVR::fixup_6_adiw: 308 adjust::fixup_6_adiw(Fixup, Value, Ctx); 309 break; 310 311 case AVR::fixup_port5: 312 adjust::fixup_port5(Fixup, Value, Ctx); 313 break; 314 315 case AVR::fixup_port6: 316 adjust::fixup_port6(Fixup, Value, Ctx); 317 break; 318 319 // Fixups which do not require adjustments. 320 case FK_Data_2: 321 case FK_Data_4: 322 case FK_Data_8: 323 break; 324 325 case FK_GPRel_4: 326 llvm_unreachable("don't know how to adjust this fixup"); 327 break; 328 } 329 } 330 331 MCObjectWriter *AVRAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const { 332 return createAVRELFObjectWriter(OS, 333 MCELFObjectTargetWriter::getOSABI(OSType)); 334 } 335 336 void AVRAsmBackend::applyFixup(const MCFixup &Fixup, char *Data, 337 unsigned DataSize, uint64_t Value, 338 bool IsPCRel) const { 339 if (Value == 0) 340 return; // Doesn't change encoding. 341 342 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind()); 343 344 // The number of bits in the fixup mask 345 auto NumBits = Info.TargetSize + Info.TargetOffset; 346 auto NumBytes = (NumBits / 8) + ((NumBits % 8) == 0 ? 0 : 1); 347 348 // Shift the value into position. 349 Value <<= Info.TargetOffset; 350 351 unsigned Offset = Fixup.getOffset(); 352 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!"); 353 354 // For each byte of the fragment that the fixup touches, mask in the 355 // bits from the fixup value. 356 for (unsigned i = 0; i < NumBytes; ++i) { 357 uint8_t mask = (((Value >> (i * 8)) & 0xff)); 358 Data[Offset + i] |= mask; 359 } 360 } 361 362 MCFixupKindInfo const &AVRAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { 363 // NOTE: Many AVR fixups work on sets of non-contignous bits. We work around 364 // this by saying that the fixup is the size of the entire instruction. 365 const static MCFixupKindInfo Infos[AVR::NumTargetFixupKinds] = { 366 // This table *must* be in same the order of fixup_* kinds in 367 // AVRFixupKinds.h. 368 // 369 // name offset bits flags 370 {"fixup_32", 0, 32, 0}, 371 372 {"fixup_7_pcrel", 3, 7, MCFixupKindInfo::FKF_IsPCRel}, 373 {"fixup_13_pcrel", 0, 12, MCFixupKindInfo::FKF_IsPCRel}, 374 375 {"fixup_16", 0, 16, 0}, 376 {"fixup_16_pm", 0, 16, 0}, 377 378 {"fixup_ldi", 0, 8, 0}, 379 380 {"fixup_lo8_ldi", 0, 8, 0}, 381 {"fixup_hi8_ldi", 0, 8, 0}, 382 {"fixup_hh8_ldi", 0, 8, 0}, 383 {"fixup_ms8_ldi", 0, 8, 0}, 384 385 {"fixup_lo8_ldi_neg", 0, 8, 0}, 386 {"fixup_hi8_ldi_neg", 0, 8, 0}, 387 {"fixup_hh8_ldi_neg", 0, 8, 0}, 388 {"fixup_ms8_ldi_neg", 0, 8, 0}, 389 390 {"fixup_lo8_ldi_pm", 0, 8, 0}, 391 {"fixup_hi8_ldi_pm", 0, 8, 0}, 392 {"fixup_hh8_ldi_pm", 0, 8, 0}, 393 394 {"fixup_lo8_ldi_pm_neg", 0, 8, 0}, 395 {"fixup_hi8_ldi_pm_neg", 0, 8, 0}, 396 {"fixup_hh8_ldi_pm_neg", 0, 8, 0}, 397 398 {"fixup_call", 0, 22, 0}, 399 400 {"fixup_6", 0, 16, 0}, // non-contiguous 401 {"fixup_6_adiw", 0, 6, 0}, 402 403 {"fixup_lo8_ldi_gs", 0, 8, 0}, 404 {"fixup_hi8_ldi_gs", 0, 8, 0}, 405 406 {"fixup_8", 0, 8, 0}, 407 {"fixup_8_lo8", 0, 8, 0}, 408 {"fixup_8_hi8", 0, 8, 0}, 409 {"fixup_8_hlo8", 0, 8, 0}, 410 411 {"fixup_sym_diff", 0, 32, 0}, 412 {"fixup_16_ldst", 0, 16, 0}, 413 414 {"fixup_lds_sts_16", 0, 16, 0}, 415 416 {"fixup_port6", 0, 16, 0}, // non-contiguous 417 {"fixup_port5", 3, 5, 0}, 418 }; 419 420 if (Kind < FirstTargetFixupKind) 421 return MCAsmBackend::getFixupKindInfo(Kind); 422 423 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && 424 "Invalid kind!"); 425 426 return Infos[Kind - FirstTargetFixupKind]; 427 } 428 429 bool AVRAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const { 430 // If the count is not 2-byte aligned, we must be writing data into the text 431 // section (otherwise we have unaligned instructions, and thus have far 432 // bigger problems), so just write zeros instead. 433 assert((Count % 2) == 0 && "NOP instructions must be 2 bytes"); 434 435 OW->WriteZeros(Count); 436 return true; 437 } 438 439 void AVRAsmBackend::processFixupValue(const MCAssembler &Asm, 440 const MCAsmLayout &Layout, 441 const MCFixup &Fixup, 442 const MCFragment *DF, 443 const MCValue &Target, uint64_t &Value, 444 bool &IsResolved) { 445 switch ((unsigned) Fixup.getKind()) { 446 // Fixups which should always be recorded as relocations. 447 case AVR::fixup_7_pcrel: 448 case AVR::fixup_13_pcrel: 449 case AVR::fixup_call: 450 IsResolved = false; 451 break; 452 default: 453 // Parsed LLVM-generated temporary labels are already 454 // adjusted for instruction size, but normal labels aren't. 455 // 456 // To handle both cases, we simply un-adjust the temporary label 457 // case so it acts like all other labels. 458 if (Target.getSymA()->getSymbol().isTemporary()) 459 Value += 2; 460 461 adjustFixupValue(Fixup, Value, &Asm.getContext()); 462 break; 463 } 464 } 465 466 MCAsmBackend *createAVRAsmBackend(const Target &T, const MCRegisterInfo &MRI, 467 const Triple &TT, StringRef CPU, 468 const llvm::MCTargetOptions &TO) { 469 return new AVRAsmBackend(TT.getOS()); 470 } 471 472 } // end of namespace llvm 473 474