MCStreamer.cpp revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===- lib/MC/MCStreamer.cpp - Streaming Machine Code Output --------------===// 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The LLVM Compiler Infrastructure 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// License. See LICENSE.TXT for details. 7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===----------------------------------------------------------------------===// 9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCStreamer.h" 11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/ADT/SmallString.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/ADT/Twine.h" 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCAsmBackend.h" 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCAsmInfo.h" 15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCContext.h" 16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCExpr.h" 17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCObjectFileInfo.h" 18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCObjectWriter.h" 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/MC/MCSymbol.h" 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/ErrorHandling.h" 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/LEB128.h" 22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/raw_ostream.h" 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <cstdlib> 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using namespace llvm; 25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Pin the vtables to this file. 27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MCTargetStreamer::~MCTargetStreamer() {} 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) { 30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) S.setTargetStreamer(this); 31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCTargetStreamer::emitLabel(MCSymbol *Symbol) {} 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCTargetStreamer::finish() {} 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {} 38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MCStreamer::MCStreamer(MCContext &Ctx) 40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) : Context(Ctx), CurrentW64UnwindInfo(nullptr), LastSymbol(nullptr) { 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>()); 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MCStreamer::~MCStreamer() { 45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) for (unsigned i = 0; i < getNumW64UnwindInfos(); ++i) 46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) delete W64UnwindInfos[i]; 47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::reset() { 50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) for (unsigned i = 0; i < getNumW64UnwindInfos(); ++i) 51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) delete W64UnwindInfos[i]; 52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) W64UnwindInfos.clear(); 53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) CurrentW64UnwindInfo = nullptr; 54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) LastSymbol = nullptr; 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) SectionStack.clear(); 56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>()); 57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const MCExpr *MCStreamer::BuildSymbolDiff(MCContext &Context, 60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCSymbol *A, 61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCSymbol *B) { 62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCExpr *ARef = 64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) MCSymbolRefExpr::Create(A, Variant, Context); 65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCExpr *BRef = 66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) MCSymbolRefExpr::Create(B, Variant, Context); 67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCExpr *AddrDelta = 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) MCBinaryExpr::Create(MCBinaryExpr::Sub, ARef, BRef, Context); 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return AddrDelta; 70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const MCExpr *MCStreamer::ForceExpAbs(const MCExpr* Expr) { 73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) assert(!isa<MCSymbolRefExpr>(Expr)); 74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (Context.getAsmInfo()->hasAggressiveSymbolFolding()) 75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return Expr; 76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) MCSymbol *ABS = Context.CreateTempSymbol(); 78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitAssignment(ABS, Expr); 79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return MCSymbolRefExpr::Create(ABS, Context); 80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)raw_ostream &MCStreamer::GetCommentOS() { 83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // By default, discard comments. 84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return nulls(); 85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::emitRawComment(const Twine &T, bool TabPrefix) {} 88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::generateCompactUnwindEncodings(MCAsmBackend *MAB) { 90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) for (std::vector<MCDwarfFrameInfo>::iterator I = FrameInfos.begin(), 91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) E = FrameInfos.end(); I != E; ++I) 92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) I->CompactUnwindEncoding = 93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) (MAB ? MAB->generateCompactUnwindEncoding(I->Instructions) : 0); 94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitDwarfSetLineAddr(int64_t LineDelta, 97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCSymbol *Label, int PointerSize) { 98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // emit the sequence to set the address 99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitIntValue(dwarf::DW_LNS_extended_op, 1); 100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitULEB128IntValue(PointerSize + 1); 101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitIntValue(dwarf::DW_LNE_set_address, 1); 102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitSymbolValue(Label, PointerSize); 103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // emit the sequence for the LineDelta (from 1) and a zero address delta. 105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) MCDwarfLineAddr::Emit(this, LineDelta, 0); 106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// EmitIntValue - Special case of EmitValue that avoids the client having to 109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// pass in a MCExpr for constant integers. 110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size) { 111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) assert(Size <= 8 && "Invalid size"); 112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) && 113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) "Invalid size"); 114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) char buf[8]; 115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const bool isLittleEndian = Context.getAsmInfo()->isLittleEndian(); 116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) for (unsigned i = 0; i != Size; ++i) { 117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) unsigned index = isLittleEndian ? i : (Size - i - 1); 118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) buf[i] = uint8_t(Value >> (index * 8)); 119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitBytes(StringRef(buf, Size)); 121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the 124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// client having to pass in a MCExpr for constant integers. 125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned Padding) { 126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) SmallString<128> Tmp; 127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) raw_svector_ostream OSE(Tmp); 128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) encodeULEB128(Value, OSE, Padding); 129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitBytes(OSE.str()); 130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the 133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// client having to pass in a MCExpr for constant integers. 134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitSLEB128IntValue(int64_t Value) { 135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) SmallString<128> Tmp; 136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) raw_svector_ostream OSE(Tmp); 137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) encodeSLEB128(Value, OSE); 138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitBytes(OSE.str()); 139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size) { 142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCExpr *ABS = ForceExpAbs(Value); 143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitValue(ABS, Size); 144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size, 148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const SMLoc &Loc) { 149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitValueImpl(Value, Size, Loc); 150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size) { 153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitValueImpl(MCSymbolRefExpr::Create(Sym, getContext()), Size); 154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitGPRel64Value(const MCExpr *Value) { 157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) report_fatal_error("unsupported directive in streamer"); 158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitGPRel32Value(const MCExpr *Value) { 161cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) report_fatal_error("unsupported directive in streamer"); 162cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 164cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// EmitFill - Emit NumBytes bytes worth of the value specified by 165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// FillValue. This implements directives such as '.space'. 166cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { 167cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const MCExpr *E = MCConstantExpr::Create(FillValue, getContext()); 168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) for (uint64_t i = 0, e = NumBytes; i != e; ++i) 169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EmitValue(E, 1); 170cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 172/// The implementation in this class just redirects to EmitFill. 173void MCStreamer::EmitZeros(uint64_t NumBytes) { 174 EmitFill(NumBytes, 0); 175} 176 177unsigned MCStreamer::EmitDwarfFileDirective(unsigned FileNo, 178 StringRef Directory, 179 StringRef Filename, unsigned CUID) { 180 return getContext().GetDwarfFile(Directory, Filename, FileNo, CUID); 181} 182 183void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, 184 unsigned Column, unsigned Flags, 185 unsigned Isa, 186 unsigned Discriminator, 187 StringRef FileName) { 188 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa, 189 Discriminator); 190} 191 192MCSymbol *MCStreamer::getDwarfLineTableSymbol(unsigned CUID) { 193 MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID); 194 if (!Table.getLabel()) { 195 StringRef Prefix = Context.getAsmInfo()->getPrivateGlobalPrefix(); 196 Table.setLabel( 197 Context.GetOrCreateSymbol(Prefix + "line_table_start" + Twine(CUID))); 198 } 199 return Table.getLabel(); 200} 201 202MCDwarfFrameInfo *MCStreamer::getCurrentFrameInfo() { 203 if (FrameInfos.empty()) 204 return nullptr; 205 return &FrameInfos.back(); 206} 207 208void MCStreamer::EnsureValidFrame() { 209 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 210 if (!CurFrame || CurFrame->End) 211 report_fatal_error("No open frame"); 212} 213 214void MCStreamer::EmitEHSymAttributes(const MCSymbol *Symbol, 215 MCSymbol *EHSymbol) { 216} 217 218void MCStreamer::InitSections() { 219 SwitchSection(getContext().getObjectFileInfo()->getTextSection()); 220} 221 222void MCStreamer::AssignSection(MCSymbol *Symbol, const MCSection *Section) { 223 if (Section) 224 Symbol->setSection(*Section); 225 else 226 Symbol->setUndefined(); 227 228 // As we emit symbols into a section, track the order so that they can 229 // be sorted upon later. Zero is reserved to mean 'unemitted'. 230 SymbolOrdering[Symbol] = 1 + SymbolOrdering.size(); 231} 232 233void MCStreamer::EmitLabel(MCSymbol *Symbol) { 234 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); 235 assert(getCurrentSection().first && "Cannot emit before setting section!"); 236 AssignSection(Symbol, getCurrentSection().first); 237 LastSymbol = Symbol; 238 239 MCTargetStreamer *TS = getTargetStreamer(); 240 if (TS) 241 TS->emitLabel(Symbol); 242} 243 244void MCStreamer::EmitDebugLabel(MCSymbol *Symbol) { 245 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); 246 assert(getCurrentSection().first && "Cannot emit before setting section!"); 247 AssignSection(Symbol, getCurrentSection().first); 248 LastSymbol = Symbol; 249} 250 251void MCStreamer::EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding) { 252 EnsureValidFrame(); 253 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 254 CurFrame->CompactUnwindEncoding = CompactUnwindEncoding; 255} 256 257void MCStreamer::EmitCFISections(bool EH, bool Debug) { 258 assert(EH || Debug); 259} 260 261void MCStreamer::EmitCFIStartProc(bool IsSimple) { 262 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 263 if (CurFrame && !CurFrame->End) 264 report_fatal_error("Starting a frame before finishing the previous one!"); 265 266 MCDwarfFrameInfo Frame; 267 Frame.IsSimple = IsSimple; 268 EmitCFIStartProcImpl(Frame); 269 270 FrameInfos.push_back(Frame); 271} 272 273void MCStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { 274} 275 276void MCStreamer::RecordProcStart(MCDwarfFrameInfo &Frame) { 277 // Report an error if we haven't seen a symbol yet where we'd bind 278 // .cfi_startproc. 279 if (!LastSymbol) 280 report_fatal_error("No symbol to start a frame"); 281 Frame.Function = LastSymbol; 282 // We need to create a local symbol to avoid relocations. 283 Frame.Begin = getContext().CreateTempSymbol(); 284 EmitLabel(Frame.Begin); 285} 286 287void MCStreamer::EmitCFIEndProc() { 288 EnsureValidFrame(); 289 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 290 EmitCFIEndProcImpl(*CurFrame); 291} 292 293void MCStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { 294} 295 296void MCStreamer::RecordProcEnd(MCDwarfFrameInfo &Frame) { 297 Frame.End = getContext().CreateTempSymbol(); 298 EmitLabel(Frame.End); 299} 300 301MCSymbol *MCStreamer::EmitCFICommon() { 302 EnsureValidFrame(); 303 MCSymbol *Label = getContext().CreateTempSymbol(); 304 EmitLabel(Label); 305 return Label; 306} 307 308void MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) { 309 MCSymbol *Label = EmitCFICommon(); 310 MCCFIInstruction Instruction = 311 MCCFIInstruction::createDefCfa(Label, Register, Offset); 312 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 313 CurFrame->Instructions.push_back(Instruction); 314} 315 316void MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) { 317 MCSymbol *Label = EmitCFICommon(); 318 MCCFIInstruction Instruction = 319 MCCFIInstruction::createDefCfaOffset(Label, Offset); 320 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 321 CurFrame->Instructions.push_back(Instruction); 322} 323 324void MCStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) { 325 MCSymbol *Label = EmitCFICommon(); 326 MCCFIInstruction Instruction = 327 MCCFIInstruction::createAdjustCfaOffset(Label, Adjustment); 328 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 329 CurFrame->Instructions.push_back(Instruction); 330} 331 332void MCStreamer::EmitCFIDefCfaRegister(int64_t Register) { 333 MCSymbol *Label = EmitCFICommon(); 334 MCCFIInstruction Instruction = 335 MCCFIInstruction::createDefCfaRegister(Label, Register); 336 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 337 CurFrame->Instructions.push_back(Instruction); 338} 339 340void MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) { 341 MCSymbol *Label = EmitCFICommon(); 342 MCCFIInstruction Instruction = 343 MCCFIInstruction::createOffset(Label, Register, Offset); 344 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 345 CurFrame->Instructions.push_back(Instruction); 346} 347 348void MCStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) { 349 MCSymbol *Label = EmitCFICommon(); 350 MCCFIInstruction Instruction = 351 MCCFIInstruction::createRelOffset(Label, Register, Offset); 352 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 353 CurFrame->Instructions.push_back(Instruction); 354} 355 356void MCStreamer::EmitCFIPersonality(const MCSymbol *Sym, 357 unsigned Encoding) { 358 EnsureValidFrame(); 359 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 360 CurFrame->Personality = Sym; 361 CurFrame->PersonalityEncoding = Encoding; 362} 363 364void MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) { 365 EnsureValidFrame(); 366 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 367 CurFrame->Lsda = Sym; 368 CurFrame->LsdaEncoding = Encoding; 369} 370 371void MCStreamer::EmitCFIRememberState() { 372 MCSymbol *Label = EmitCFICommon(); 373 MCCFIInstruction Instruction = MCCFIInstruction::createRememberState(Label); 374 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 375 CurFrame->Instructions.push_back(Instruction); 376} 377 378void MCStreamer::EmitCFIRestoreState() { 379 // FIXME: Error if there is no matching cfi_remember_state. 380 MCSymbol *Label = EmitCFICommon(); 381 MCCFIInstruction Instruction = MCCFIInstruction::createRestoreState(Label); 382 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 383 CurFrame->Instructions.push_back(Instruction); 384} 385 386void MCStreamer::EmitCFISameValue(int64_t Register) { 387 MCSymbol *Label = EmitCFICommon(); 388 MCCFIInstruction Instruction = 389 MCCFIInstruction::createSameValue(Label, Register); 390 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 391 CurFrame->Instructions.push_back(Instruction); 392} 393 394void MCStreamer::EmitCFIRestore(int64_t Register) { 395 MCSymbol *Label = EmitCFICommon(); 396 MCCFIInstruction Instruction = 397 MCCFIInstruction::createRestore(Label, Register); 398 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 399 CurFrame->Instructions.push_back(Instruction); 400} 401 402void MCStreamer::EmitCFIEscape(StringRef Values) { 403 MCSymbol *Label = EmitCFICommon(); 404 MCCFIInstruction Instruction = MCCFIInstruction::createEscape(Label, Values); 405 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 406 CurFrame->Instructions.push_back(Instruction); 407} 408 409void MCStreamer::EmitCFISignalFrame() { 410 EnsureValidFrame(); 411 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 412 CurFrame->IsSignalFrame = true; 413} 414 415void MCStreamer::EmitCFIUndefined(int64_t Register) { 416 MCSymbol *Label = EmitCFICommon(); 417 MCCFIInstruction Instruction = 418 MCCFIInstruction::createUndefined(Label, Register); 419 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 420 CurFrame->Instructions.push_back(Instruction); 421} 422 423void MCStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) { 424 MCSymbol *Label = EmitCFICommon(); 425 MCCFIInstruction Instruction = 426 MCCFIInstruction::createRegister(Label, Register1, Register2); 427 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 428 CurFrame->Instructions.push_back(Instruction); 429} 430 431void MCStreamer::EmitCFIWindowSave() { 432 MCSymbol *Label = EmitCFICommon(); 433 MCCFIInstruction Instruction = 434 MCCFIInstruction::createWindowSave(Label); 435 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 436 CurFrame->Instructions.push_back(Instruction); 437} 438 439void MCStreamer::setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame) { 440 W64UnwindInfos.push_back(Frame); 441 CurrentW64UnwindInfo = W64UnwindInfos.back(); 442} 443 444void MCStreamer::EnsureValidW64UnwindInfo() { 445 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 446 if (!CurFrame || CurFrame->End) 447 report_fatal_error("No open Win64 EH frame function!"); 448} 449 450void MCStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) { 451 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 452 if (CurFrame && !CurFrame->End) 453 report_fatal_error("Starting a function before ending the previous one!"); 454 MCWin64EHUnwindInfo *Frame = new MCWin64EHUnwindInfo; 455 Frame->Begin = getContext().CreateTempSymbol(); 456 Frame->Function = Symbol; 457 EmitLabel(Frame->Begin); 458 setCurrentW64UnwindInfo(Frame); 459} 460 461void MCStreamer::EmitWin64EHEndProc() { 462 EnsureValidW64UnwindInfo(); 463 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 464 if (CurFrame->ChainedParent) 465 report_fatal_error("Not all chained regions terminated!"); 466 CurFrame->End = getContext().CreateTempSymbol(); 467 EmitLabel(CurFrame->End); 468} 469 470void MCStreamer::EmitWin64EHStartChained() { 471 EnsureValidW64UnwindInfo(); 472 MCWin64EHUnwindInfo *Frame = new MCWin64EHUnwindInfo; 473 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 474 Frame->Begin = getContext().CreateTempSymbol(); 475 Frame->Function = CurFrame->Function; 476 Frame->ChainedParent = CurFrame; 477 EmitLabel(Frame->Begin); 478 setCurrentW64UnwindInfo(Frame); 479} 480 481void MCStreamer::EmitWin64EHEndChained() { 482 EnsureValidW64UnwindInfo(); 483 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 484 if (!CurFrame->ChainedParent) 485 report_fatal_error("End of a chained region outside a chained region!"); 486 CurFrame->End = getContext().CreateTempSymbol(); 487 EmitLabel(CurFrame->End); 488 CurrentW64UnwindInfo = CurFrame->ChainedParent; 489} 490 491void MCStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind, 492 bool Except) { 493 EnsureValidW64UnwindInfo(); 494 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 495 if (CurFrame->ChainedParent) 496 report_fatal_error("Chained unwind areas can't have handlers!"); 497 CurFrame->ExceptionHandler = Sym; 498 if (!Except && !Unwind) 499 report_fatal_error("Don't know what kind of handler this is!"); 500 if (Unwind) 501 CurFrame->HandlesUnwind = true; 502 if (Except) 503 CurFrame->HandlesExceptions = true; 504} 505 506void MCStreamer::EmitWin64EHHandlerData() { 507 EnsureValidW64UnwindInfo(); 508 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 509 if (CurFrame->ChainedParent) 510 report_fatal_error("Chained unwind areas can't have handlers!"); 511} 512 513void MCStreamer::EmitWin64EHPushReg(unsigned Register) { 514 EnsureValidW64UnwindInfo(); 515 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 516 MCSymbol *Label = getContext().CreateTempSymbol(); 517 MCWin64EHInstruction Inst(Win64EH::UOP_PushNonVol, Label, Register); 518 EmitLabel(Label); 519 CurFrame->Instructions.push_back(Inst); 520} 521 522void MCStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) { 523 EnsureValidW64UnwindInfo(); 524 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 525 if (CurFrame->LastFrameInst >= 0) 526 report_fatal_error("Frame register and offset already specified!"); 527 if (Offset & 0x0F) 528 report_fatal_error("Misaligned frame pointer offset!"); 529 MCSymbol *Label = getContext().CreateTempSymbol(); 530 MCWin64EHInstruction Inst(Win64EH::UOP_SetFPReg, Label, Register, Offset); 531 EmitLabel(Label); 532 CurFrame->LastFrameInst = CurFrame->Instructions.size(); 533 CurFrame->Instructions.push_back(Inst); 534} 535 536void MCStreamer::EmitWin64EHAllocStack(unsigned Size) { 537 EnsureValidW64UnwindInfo(); 538 if (Size & 7) 539 report_fatal_error("Misaligned stack allocation!"); 540 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 541 MCSymbol *Label = getContext().CreateTempSymbol(); 542 MCWin64EHInstruction Inst(Label, Size); 543 EmitLabel(Label); 544 CurFrame->Instructions.push_back(Inst); 545} 546 547void MCStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) { 548 EnsureValidW64UnwindInfo(); 549 if (Offset & 7) 550 report_fatal_error("Misaligned saved register offset!"); 551 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 552 MCSymbol *Label = getContext().CreateTempSymbol(); 553 MCWin64EHInstruction Inst( 554 Offset > 512*1024-8 ? Win64EH::UOP_SaveNonVolBig : Win64EH::UOP_SaveNonVol, 555 Label, Register, Offset); 556 EmitLabel(Label); 557 CurFrame->Instructions.push_back(Inst); 558} 559 560void MCStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) { 561 EnsureValidW64UnwindInfo(); 562 if (Offset & 0x0F) 563 report_fatal_error("Misaligned saved vector register offset!"); 564 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 565 MCSymbol *Label = getContext().CreateTempSymbol(); 566 MCWin64EHInstruction Inst( 567 Offset > 512*1024-16 ? Win64EH::UOP_SaveXMM128Big : Win64EH::UOP_SaveXMM128, 568 Label, Register, Offset); 569 EmitLabel(Label); 570 CurFrame->Instructions.push_back(Inst); 571} 572 573void MCStreamer::EmitWin64EHPushFrame(bool Code) { 574 EnsureValidW64UnwindInfo(); 575 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 576 if (CurFrame->Instructions.size() > 0) 577 report_fatal_error("If present, PushMachFrame must be the first UOP"); 578 MCSymbol *Label = getContext().CreateTempSymbol(); 579 MCWin64EHInstruction Inst(Win64EH::UOP_PushMachFrame, Label, Code); 580 EmitLabel(Label); 581 CurFrame->Instructions.push_back(Inst); 582} 583 584void MCStreamer::EmitWin64EHEndProlog() { 585 EnsureValidW64UnwindInfo(); 586 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo; 587 CurFrame->PrologEnd = getContext().CreateTempSymbol(); 588 EmitLabel(CurFrame->PrologEnd); 589} 590 591void MCStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) { 592 llvm_unreachable("This file format doesn't support this directive"); 593} 594 595void MCStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) { 596 llvm_unreachable("This file format doesn't support this directive"); 597} 598 599/// EmitRawText - If this file is backed by an assembly streamer, this dumps 600/// the specified string in the output .s file. This capability is 601/// indicated by the hasRawTextSupport() predicate. 602void MCStreamer::EmitRawTextImpl(StringRef String) { 603 errs() << "EmitRawText called on an MCStreamer that doesn't support it, " 604 " something must not be fully mc'ized\n"; 605 abort(); 606} 607 608void MCStreamer::EmitRawText(const Twine &T) { 609 SmallString<128> Str; 610 EmitRawTextImpl(T.toStringRef(Str)); 611} 612 613void MCStreamer::EmitW64Tables() { 614 if (!getNumW64UnwindInfos()) 615 return; 616 617 MCWin64EHUnwindEmitter::Emit(*this); 618} 619 620void MCStreamer::Finish() { 621 if (!FrameInfos.empty() && !FrameInfos.back().End) 622 report_fatal_error("Unfinished frame!"); 623 624 MCTargetStreamer *TS = getTargetStreamer(); 625 if (TS) 626 TS->finish(); 627 628 FinishImpl(); 629} 630 631void MCStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 632 Symbol->setVariableValue(Value); 633 634 MCTargetStreamer *TS = getTargetStreamer(); 635 if (TS) 636 TS->emitAssignment(Symbol, Value); 637} 638