1 //===- lib/MC/MCStreamer.cpp - Streaming Machine Code Output --------------===// 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 #include "llvm/MC/MCAsmInfo.h" 11 #include "llvm/MC/MCContext.h" 12 #include "llvm/MC/MCStreamer.h" 13 #include "llvm/MC/MCExpr.h" 14 #include "llvm/MC/MCObjectWriter.h" 15 #include "llvm/MC/MCSymbol.h" 16 #include "llvm/Support/ErrorHandling.h" 17 #include "llvm/Support/raw_ostream.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/Twine.h" 20 #include <cstdlib> 21 using namespace llvm; 22 23 MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), EmitEHFrame(true), 24 EmitDebugFrame(false), 25 CurrentW64UnwindInfo(0) { 26 const MCSection *section = NULL; 27 SectionStack.push_back(std::make_pair(section, section)); 28 } 29 30 MCStreamer::~MCStreamer() { 31 for (unsigned i = 0; i < getNumW64UnwindInfos(); ++i) 32 delete W64UnwindInfos[i]; 33 } 34 35 const MCExpr *MCStreamer::BuildSymbolDiff(MCContext &Context, 36 const MCSymbol *A, 37 const MCSymbol *B) { 38 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 39 const MCExpr *ARef = 40 MCSymbolRefExpr::Create(A, Variant, Context); 41 const MCExpr *BRef = 42 MCSymbolRefExpr::Create(B, Variant, Context); 43 const MCExpr *AddrDelta = 44 MCBinaryExpr::Create(MCBinaryExpr::Sub, ARef, BRef, Context); 45 return AddrDelta; 46 } 47 48 const MCExpr *MCStreamer::ForceExpAbs(const MCExpr* Expr) { 49 if (Context.getAsmInfo().hasAggressiveSymbolFolding() || 50 isa<MCSymbolRefExpr>(Expr)) 51 return Expr; 52 53 MCSymbol *ABS = Context.CreateTempSymbol(); 54 EmitAssignment(ABS, Expr); 55 return MCSymbolRefExpr::Create(ABS, Context); 56 } 57 58 raw_ostream &MCStreamer::GetCommentOS() { 59 // By default, discard comments. 60 return nulls(); 61 } 62 63 void MCStreamer::EmitDwarfSetLineAddr(int64_t LineDelta, 64 const MCSymbol *Label, int PointerSize) { 65 // emit the sequence to set the address 66 EmitIntValue(dwarf::DW_LNS_extended_op, 1); 67 EmitULEB128IntValue(PointerSize + 1); 68 EmitIntValue(dwarf::DW_LNE_set_address, 1); 69 EmitSymbolValue(Label, PointerSize); 70 71 // emit the sequence for the LineDelta (from 1) and a zero address delta. 72 MCDwarfLineAddr::Emit(this, LineDelta, 0); 73 } 74 75 /// EmitIntValue - Special case of EmitValue that avoids the client having to 76 /// pass in a MCExpr for constant integers. 77 void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size, 78 unsigned AddrSpace) { 79 assert(Size <= 8 && "Invalid size"); 80 assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) && 81 "Invalid size"); 82 char buf[8]; 83 const bool isLittleEndian = Context.getAsmInfo().isLittleEndian(); 84 for (unsigned i = 0; i != Size; ++i) { 85 unsigned index = isLittleEndian ? i : (Size - i - 1); 86 buf[i] = uint8_t(Value >> (index * 8)); 87 } 88 EmitBytes(StringRef(buf, Size), AddrSpace); 89 } 90 91 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the 92 /// client having to pass in a MCExpr for constant integers. 93 void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) { 94 SmallString<32> Tmp; 95 raw_svector_ostream OSE(Tmp); 96 MCObjectWriter::EncodeULEB128(Value, OSE); 97 EmitBytes(OSE.str(), AddrSpace); 98 } 99 100 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the 101 /// client having to pass in a MCExpr for constant integers. 102 void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) { 103 SmallString<32> Tmp; 104 raw_svector_ostream OSE(Tmp); 105 MCObjectWriter::EncodeSLEB128(Value, OSE); 106 EmitBytes(OSE.str(), AddrSpace); 107 } 108 109 void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size, 110 unsigned AddrSpace) { 111 const MCExpr *ABS = ForceExpAbs(Value); 112 EmitValue(ABS, Size, AddrSpace); 113 } 114 115 116 void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size, 117 unsigned AddrSpace) { 118 EmitValueImpl(Value, Size, AddrSpace); 119 } 120 121 void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size, 122 unsigned AddrSpace) { 123 EmitValueImpl(MCSymbolRefExpr::Create(Sym, getContext()), Size, 124 AddrSpace); 125 } 126 127 void MCStreamer::EmitGPRel32Value(const MCExpr *Value) { 128 report_fatal_error("unsupported directive in streamer"); 129 } 130 131 /// EmitFill - Emit NumBytes bytes worth of the value specified by 132 /// FillValue. This implements directives such as '.space'. 133 void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, 134 unsigned AddrSpace) { 135 const MCExpr *E = MCConstantExpr::Create(FillValue, getContext()); 136 for (uint64_t i = 0, e = NumBytes; i != e; ++i) 137 EmitValue(E, 1, AddrSpace); 138 } 139 140 bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo, 141 StringRef Filename) { 142 return getContext().GetDwarfFile(Filename, FileNo) == 0; 143 } 144 145 void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, 146 unsigned Column, unsigned Flags, 147 unsigned Isa, 148 unsigned Discriminator, 149 StringRef FileName) { 150 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa, 151 Discriminator); 152 } 153 154 MCDwarfFrameInfo *MCStreamer::getCurrentFrameInfo() { 155 if (FrameInfos.empty()) 156 return NULL; 157 return &FrameInfos.back(); 158 } 159 160 void MCStreamer::EnsureValidFrame() { 161 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 162 if (!CurFrame || CurFrame->End) 163 report_fatal_error("No open frame"); 164 } 165 166 void MCStreamer::EmitEHSymAttributes(const MCSymbol *Symbol, 167 MCSymbol *EHSymbol) { 168 } 169 170 void MCStreamer::EmitLabel(MCSymbol *Symbol) { 171 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); 172 assert(getCurrentSection() && "Cannot emit before setting section!"); 173 Symbol->setSection(*getCurrentSection()); 174 175 StringRef Prefix = getContext().getAsmInfo().getPrivateGlobalPrefix(); 176 if (!Symbol->getName().startswith(Prefix)) 177 LastNonPrivate = Symbol; 178 } 179 180 void MCStreamer::EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding) { 181 EnsureValidFrame(); 182 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 183 CurFrame->CompactUnwindEncoding = CompactUnwindEncoding; 184 } 185 186 void MCStreamer::EmitCFISections(bool EH, bool Debug) { 187 assert(EH || Debug); 188 EmitEHFrame = EH; 189 EmitDebugFrame = Debug; 190 } 191 192 void MCStreamer::EmitCFIStartProc() { 193 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 194 if (CurFrame && !CurFrame->End) 195 report_fatal_error("Starting a frame before finishing the previous one!"); 196 MCDwarfFrameInfo Frame; 197 Frame.Begin = getContext().CreateTempSymbol(); 198 Frame.Function = LastNonPrivate; 199 EmitLabel(Frame.Begin); 200 FrameInfos.push_back(Frame); 201 } 202 203 void MCStreamer::EmitCFIEndProc() { 204 EnsureValidFrame(); 205 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 206 CurFrame->End = getContext().CreateTempSymbol(); 207 EmitLabel(CurFrame->End); 208 } 209 210 void MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) { 211 EnsureValidFrame(); 212 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 213 MCSymbol *Label = getContext().CreateTempSymbol(); 214 EmitLabel(Label); 215 MachineLocation Dest(MachineLocation::VirtualFP); 216 MachineLocation Source(Register, -Offset); 217 MCCFIInstruction Instruction(Label, Dest, Source); 218 CurFrame->Instructions.push_back(Instruction); 219 } 220 221 void MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) { 222 EnsureValidFrame(); 223 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 224 MCSymbol *Label = getContext().CreateTempSymbol(); 225 EmitLabel(Label); 226 MachineLocation Dest(MachineLocation::VirtualFP); 227 MachineLocation Source(MachineLocation::VirtualFP, -Offset); 228 MCCFIInstruction Instruction(Label, Dest, Source); 229 CurFrame->Instructions.push_back(Instruction); 230 } 231 232 void MCStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) { 233 EnsureValidFrame(); 234 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 235 MCSymbol *Label = getContext().CreateTempSymbol(); 236 EmitLabel(Label); 237 MachineLocation Dest(MachineLocation::VirtualFP); 238 MachineLocation Source(MachineLocation::VirtualFP, Adjustment); 239 MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source); 240 CurFrame->Instructions.push_back(Instruction); 241 } 242 243 void MCStreamer::EmitCFIDefCfaRegister(int64_t Register) { 244 EnsureValidFrame(); 245 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 246 MCSymbol *Label = getContext().CreateTempSymbol(); 247 EmitLabel(Label); 248 MachineLocation Dest(Register); 249 MachineLocation Source(MachineLocation::VirtualFP); 250 MCCFIInstruction Instruction(Label, Dest, Source); 251 CurFrame->Instructions.push_back(Instruction); 252 } 253 254 void MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) { 255 EnsureValidFrame(); 256 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 257 MCSymbol *Label = getContext().CreateTempSymbol(); 258 EmitLabel(Label); 259 MachineLocation Dest(Register, Offset); 260 MachineLocation Source(Register, Offset); 261 MCCFIInstruction Instruction(Label, Dest, Source); 262 CurFrame->Instructions.push_back(Instruction); 263 } 264 265 void MCStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) { 266 EnsureValidFrame(); 267 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 268 MCSymbol *Label = getContext().CreateTempSymbol(); 269 EmitLabel(Label); 270 MachineLocation Dest(Register, Offset); 271 MachineLocation Source(Register, Offset); 272 MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source); 273 CurFrame->Instructions.push_back(Instruction); 274 } 275 276 void MCStreamer::EmitCFIPersonality(const MCSymbol *Sym, 277 unsigned Encoding) { 278 EnsureValidFrame(); 279 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 280 CurFrame->Personality = Sym; 281 CurFrame->PersonalityEncoding = Encoding; 282 } 283 284 void MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) { 285 EnsureValidFrame(); 286 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 287 CurFrame->Lsda = Sym; 288 CurFrame->LsdaEncoding = Encoding; 289 } 290 291 void MCStreamer::EmitCFIRememberState() { 292 EnsureValidFrame(); 293 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 294 MCSymbol *Label = getContext().CreateTempSymbol(); 295 EmitLabel(Label); 296 MCCFIInstruction Instruction(MCCFIInstruction::Remember, Label); 297 CurFrame->Instructions.push_back(Instruction); 298 } 299 300 void MCStreamer::EmitCFIRestoreState() { 301 // FIXME: Error if there is no matching cfi_remember_state. 302 EnsureValidFrame(); 303 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 304 MCSymbol *Label = getContext().CreateTempSymbol(); 305 EmitLabel(Label); 306 MCCFIInstruction Instruction(MCCFIInstruction::Restore, Label); 307 CurFrame->Instructions.push_back(Instruction); 308 } 309 310 void MCStreamer::EmitCFISameValue(int64_t Register) { 311 EnsureValidFrame(); 312 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 313 MCSymbol *Label = getContext().CreateTempSymbol(); 314 EmitLabel(Label); 315 MCCFIInstruction Instruction(MCCFIInstruction::SameValue, Label, Register); 316 CurFrame->Instructions.push_back(Instruction); 317 } 318 319 void MCStreamer::setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame) { 320 W64UnwindInfos.push_back(Frame); 321 CurrentW64UnwindInfo = W64UnwindInfos.back(); 322 } 323 324 void MCStreamer::EnsureValidW64UnwindInfo() { 325 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 326 if (!CurFrame || CurFrame->End) 327 report_fatal_error("No open Win64 EH frame function!"); 328 } 329 330 void MCStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) { 331 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 332 if (CurFrame && !CurFrame->End) 333 report_fatal_error("Starting a function before ending the previous one!"); 334 MCWin64EHUnwindInfo *Frame = new MCWin64EHUnwindInfo; 335 Frame->Begin = getContext().CreateTempSymbol(); 336 Frame->Function = Symbol; 337 EmitLabel(Frame->Begin); 338 setCurrentW64UnwindInfo(Frame); 339 } 340 341 void MCStreamer::EmitWin64EHEndProc() { 342 EnsureValidW64UnwindInfo(); 343 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 344 if (CurFrame->ChainedParent) 345 report_fatal_error("Not all chained regions terminated!"); 346 CurFrame->End = getContext().CreateTempSymbol(); 347 EmitLabel(CurFrame->End); 348 } 349 350 void MCStreamer::EmitWin64EHStartChained() { 351 EnsureValidW64UnwindInfo(); 352 MCWin64EHUnwindInfo *Frame = new MCWin64EHUnwindInfo; 353 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 354 Frame->Begin = getContext().CreateTempSymbol(); 355 Frame->Function = CurFrame->Function; 356 Frame->ChainedParent = CurFrame; 357 EmitLabel(Frame->Begin); 358 setCurrentW64UnwindInfo(Frame); 359 } 360 361 void MCStreamer::EmitWin64EHEndChained() { 362 EnsureValidW64UnwindInfo(); 363 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 364 if (!CurFrame->ChainedParent) 365 report_fatal_error("End of a chained region outside a chained region!"); 366 CurFrame->End = getContext().CreateTempSymbol(); 367 EmitLabel(CurFrame->End); 368 CurrentW64UnwindInfo = CurFrame->ChainedParent; 369 } 370 371 void MCStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind, 372 bool Except) { 373 EnsureValidW64UnwindInfo(); 374 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 375 if (CurFrame->ChainedParent) 376 report_fatal_error("Chained unwind areas can't have handlers!"); 377 CurFrame->ExceptionHandler = Sym; 378 if (!Except && !Unwind) 379 report_fatal_error("Don't know what kind of handler this is!"); 380 if (Unwind) 381 CurFrame->HandlesUnwind = true; 382 if (Except) 383 CurFrame->HandlesExceptions = true; 384 } 385 386 void MCStreamer::EmitWin64EHHandlerData() { 387 EnsureValidW64UnwindInfo(); 388 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 389 if (CurFrame->ChainedParent) 390 report_fatal_error("Chained unwind areas can't have handlers!"); 391 } 392 393 void MCStreamer::EmitWin64EHPushReg(unsigned Register) { 394 EnsureValidW64UnwindInfo(); 395 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 396 MCSymbol *Label = getContext().CreateTempSymbol(); 397 MCWin64EHInstruction Inst(Win64EH::UOP_PushNonVol, Label, Register); 398 EmitLabel(Label); 399 CurFrame->Instructions.push_back(Inst); 400 } 401 402 void MCStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) { 403 EnsureValidW64UnwindInfo(); 404 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 405 if (CurFrame->LastFrameInst >= 0) 406 report_fatal_error("Frame register and offset already specified!"); 407 if (Offset & 0x0F) 408 report_fatal_error("Misaligned frame pointer offset!"); 409 MCWin64EHInstruction Inst(Win64EH::UOP_SetFPReg, NULL, Register, Offset); 410 CurFrame->LastFrameInst = CurFrame->Instructions.size(); 411 CurFrame->Instructions.push_back(Inst); 412 } 413 414 void MCStreamer::EmitWin64EHAllocStack(unsigned Size) { 415 EnsureValidW64UnwindInfo(); 416 if (Size & 7) 417 report_fatal_error("Misaligned stack allocation!"); 418 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 419 MCSymbol *Label = getContext().CreateTempSymbol(); 420 MCWin64EHInstruction Inst(Label, Size); 421 EmitLabel(Label); 422 CurFrame->Instructions.push_back(Inst); 423 } 424 425 void MCStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) { 426 EnsureValidW64UnwindInfo(); 427 if (Offset & 7) 428 report_fatal_error("Misaligned saved register offset!"); 429 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 430 MCSymbol *Label = getContext().CreateTempSymbol(); 431 MCWin64EHInstruction Inst( 432 Offset > 512*1024-8 ? Win64EH::UOP_SaveNonVolBig : Win64EH::UOP_SaveNonVol, 433 Label, Register, Offset); 434 EmitLabel(Label); 435 CurFrame->Instructions.push_back(Inst); 436 } 437 438 void MCStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) { 439 EnsureValidW64UnwindInfo(); 440 if (Offset & 0x0F) 441 report_fatal_error("Misaligned saved vector register offset!"); 442 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 443 MCSymbol *Label = getContext().CreateTempSymbol(); 444 MCWin64EHInstruction Inst( 445 Offset > 512*1024-16 ? Win64EH::UOP_SaveXMM128Big : Win64EH::UOP_SaveXMM128, 446 Label, Register, Offset); 447 EmitLabel(Label); 448 CurFrame->Instructions.push_back(Inst); 449 } 450 451 void MCStreamer::EmitWin64EHPushFrame(bool Code) { 452 EnsureValidW64UnwindInfo(); 453 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 454 if (CurFrame->Instructions.size() > 0) 455 report_fatal_error("If present, PushMachFrame must be the first UOP"); 456 MCSymbol *Label = getContext().CreateTempSymbol(); 457 MCWin64EHInstruction Inst(Win64EH::UOP_PushMachFrame, Label, Code); 458 EmitLabel(Label); 459 CurFrame->Instructions.push_back(Inst); 460 } 461 462 void MCStreamer::EmitWin64EHEndProlog() { 463 EnsureValidW64UnwindInfo(); 464 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 465 CurFrame->PrologEnd = getContext().CreateTempSymbol(); 466 EmitLabel(CurFrame->PrologEnd); 467 } 468 469 void MCStreamer::EmitFnStart() { 470 errs() << "Not implemented yet\n"; 471 abort(); 472 } 473 474 void MCStreamer::EmitFnEnd() { 475 errs() << "Not implemented yet\n"; 476 abort(); 477 } 478 479 void MCStreamer::EmitCantUnwind() { 480 errs() << "Not implemented yet\n"; 481 abort(); 482 } 483 484 void MCStreamer::EmitHandlerData() { 485 errs() << "Not implemented yet\n"; 486 abort(); 487 } 488 489 void MCStreamer::EmitPersonality(const MCSymbol *Personality) { 490 errs() << "Not implemented yet\n"; 491 abort(); 492 } 493 494 void MCStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) { 495 errs() << "Not implemented yet\n"; 496 abort(); 497 } 498 499 void MCStreamer::EmitPad(int64_t Offset) { 500 errs() << "Not implemented yet\n"; 501 abort(); 502 } 503 504 void MCStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool) { 505 errs() << "Not implemented yet\n"; 506 abort(); 507 } 508 509 /// EmitRawText - If this file is backed by an assembly streamer, this dumps 510 /// the specified string in the output .s file. This capability is 511 /// indicated by the hasRawTextSupport() predicate. 512 void MCStreamer::EmitRawText(StringRef String) { 513 errs() << "EmitRawText called on an MCStreamer that doesn't support it, " 514 " something must not be fully mc'ized\n"; 515 abort(); 516 } 517 518 void MCStreamer::EmitRawText(const Twine &T) { 519 SmallString<128> Str; 520 T.toVector(Str); 521 EmitRawText(Str.str()); 522 } 523 524 void MCStreamer::EmitFrames(bool usingCFI) { 525 if (!getNumFrameInfos()) 526 return; 527 528 if (EmitEHFrame) 529 MCDwarfFrameEmitter::Emit(*this, usingCFI, true); 530 531 if (EmitDebugFrame) 532 MCDwarfFrameEmitter::Emit(*this, usingCFI, false); 533 } 534 535 void MCStreamer::EmitW64Tables() { 536 if (!getNumW64UnwindInfos()) 537 return; 538 539 MCWin64EHUnwindEmitter::Emit(*this); 540 } 541