MCStreamer.cpp revision 219077
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/Support/ErrorHandling.h" 16#include "llvm/Support/raw_ostream.h" 17#include "llvm/ADT/SmallString.h" 18#include "llvm/ADT/Twine.h" 19#include <cstdlib> 20using namespace llvm; 21 22MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx) { 23 const MCSection *section = NULL; 24 SectionStack.push_back(std::make_pair(section, section)); 25} 26 27MCStreamer::~MCStreamer() { 28} 29 30raw_ostream &MCStreamer::GetCommentOS() { 31 // By default, discard comments. 32 return nulls(); 33} 34 35void MCStreamer::EmitDwarfSetLineAddr(int64_t LineDelta, 36 const MCSymbol *Label, int PointerSize) { 37 // emit the sequence to set the address 38 EmitIntValue(dwarf::DW_LNS_extended_op, 1); 39 EmitULEB128IntValue(PointerSize + 1); 40 EmitIntValue(dwarf::DW_LNE_set_address, 1); 41 EmitSymbolValue(Label, PointerSize); 42 43 // emit the sequence for the LineDelta (from 1) and a zero address delta. 44 MCDwarfLineAddr::Emit(this, LineDelta, 0); 45} 46 47/// EmitIntValue - Special case of EmitValue that avoids the client having to 48/// pass in a MCExpr for constant integers. 49void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size, 50 unsigned AddrSpace) { 51 assert(Size <= 8 && "Invalid size"); 52 assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) && 53 "Invalid size"); 54 char buf[8]; 55 // FIXME: Endianness assumption. 56 for (unsigned i = 0; i != Size; ++i) 57 buf[i] = uint8_t(Value >> (i * 8)); 58 EmitBytes(StringRef(buf, Size), AddrSpace); 59} 60 61/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the 62/// client having to pass in a MCExpr for constant integers. 63void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) { 64 SmallString<32> Tmp; 65 raw_svector_ostream OSE(Tmp); 66 MCObjectWriter::EncodeULEB128(Value, OSE); 67 EmitBytes(OSE.str(), AddrSpace); 68} 69 70/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the 71/// client having to pass in a MCExpr for constant integers. 72void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) { 73 SmallString<32> Tmp; 74 raw_svector_ostream OSE(Tmp); 75 MCObjectWriter::EncodeSLEB128(Value, OSE); 76 EmitBytes(OSE.str(), AddrSpace); 77} 78 79void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size, 80 unsigned AddrSpace) { 81 if (getContext().getAsmInfo().hasAggressiveSymbolFolding()) { 82 EmitValue(Value, Size, AddrSpace); 83 return; 84 } 85 MCSymbol *ABS = getContext().CreateTempSymbol(); 86 EmitAssignment(ABS, Value); 87 EmitSymbolValue(ABS, Size, AddrSpace); 88} 89 90 91void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size, 92 unsigned AddrSpace) { 93 EmitValueImpl(Value, Size, false, AddrSpace); 94} 95 96void MCStreamer::EmitPCRelValue(const MCExpr *Value, unsigned Size, 97 unsigned AddrSpace) { 98 EmitValueImpl(Value, Size, true, AddrSpace); 99} 100 101void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size, 102 bool isPCRel, unsigned AddrSpace) { 103 EmitValueImpl(MCSymbolRefExpr::Create(Sym, getContext()), Size, isPCRel, 104 AddrSpace); 105} 106 107void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size, 108 unsigned AddrSpace) { 109 EmitSymbolValue(Sym, Size, false, AddrSpace); 110} 111 112void MCStreamer::EmitPCRelSymbolValue(const MCSymbol *Sym, unsigned Size, 113 unsigned AddrSpace) { 114 EmitSymbolValue(Sym, Size, true, AddrSpace); 115} 116 117void MCStreamer::EmitGPRel32Value(const MCExpr *Value) { 118 report_fatal_error("unsupported directive in streamer"); 119} 120 121/// EmitFill - Emit NumBytes bytes worth of the value specified by 122/// FillValue. This implements directives such as '.space'. 123void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, 124 unsigned AddrSpace) { 125 const MCExpr *E = MCConstantExpr::Create(FillValue, getContext()); 126 for (uint64_t i = 0, e = NumBytes; i != e; ++i) 127 EmitValue(E, 1, AddrSpace); 128} 129 130bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo, 131 StringRef Filename) { 132 return getContext().GetDwarfFile(Filename, FileNo) == 0; 133} 134 135void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, 136 unsigned Column, unsigned Flags, 137 unsigned Isa, 138 unsigned Discriminator) { 139 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa, 140 Discriminator); 141} 142 143MCDwarfFrameInfo *MCStreamer::getCurrentFrameInfo() { 144 if (FrameInfos.empty()) 145 return NULL; 146 return &FrameInfos.back(); 147} 148 149void MCStreamer::EnsureValidFrame() { 150 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 151 if (!CurFrame || CurFrame->End) 152 report_fatal_error("No open frame"); 153} 154 155bool MCStreamer::EmitCFIStartProc() { 156 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 157 if (CurFrame && !CurFrame->End) { 158 report_fatal_error("Starting a frame before finishing the previous one!"); 159 return true; 160 } 161 MCDwarfFrameInfo Frame; 162 Frame.Begin = getContext().CreateTempSymbol(); 163 EmitLabel(Frame.Begin); 164 FrameInfos.push_back(Frame); 165 return false; 166} 167 168bool MCStreamer::EmitCFIEndProc() { 169 EnsureValidFrame(); 170 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 171 CurFrame->End = getContext().CreateTempSymbol(); 172 EmitLabel(CurFrame->End); 173 return false; 174} 175 176bool MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) { 177 EnsureValidFrame(); 178 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 179 MCSymbol *Label = getContext().CreateTempSymbol(); 180 EmitLabel(Label); 181 MachineLocation Dest(MachineLocation::VirtualFP); 182 MachineLocation Source(Register, -Offset); 183 MCCFIInstruction Instruction(Label, Dest, Source); 184 CurFrame->Instructions.push_back(Instruction); 185 return false; 186} 187 188bool MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) { 189 EnsureValidFrame(); 190 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 191 MCSymbol *Label = getContext().CreateTempSymbol(); 192 EmitLabel(Label); 193 MachineLocation Dest(MachineLocation::VirtualFP); 194 MachineLocation Source(MachineLocation::VirtualFP, -Offset); 195 MCCFIInstruction Instruction(Label, Dest, Source); 196 CurFrame->Instructions.push_back(Instruction); 197 return false; 198} 199 200bool MCStreamer::EmitCFIDefCfaRegister(int64_t Register) { 201 EnsureValidFrame(); 202 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 203 MCSymbol *Label = getContext().CreateTempSymbol(); 204 EmitLabel(Label); 205 MachineLocation Dest(Register); 206 MachineLocation Source(MachineLocation::VirtualFP); 207 MCCFIInstruction Instruction(Label, Dest, Source); 208 CurFrame->Instructions.push_back(Instruction); 209 return false; 210} 211 212bool MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) { 213 EnsureValidFrame(); 214 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 215 MCSymbol *Label = getContext().CreateTempSymbol(); 216 EmitLabel(Label); 217 MachineLocation Dest(Register, Offset); 218 MachineLocation Source(Register, Offset); 219 MCCFIInstruction Instruction(Label, Dest, Source); 220 CurFrame->Instructions.push_back(Instruction); 221 return false; 222} 223 224bool MCStreamer::EmitCFIPersonality(const MCSymbol *Sym, 225 unsigned Encoding) { 226 EnsureValidFrame(); 227 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 228 CurFrame->Personality = Sym; 229 CurFrame->PersonalityEncoding = Encoding; 230 return false; 231} 232 233bool MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) { 234 EnsureValidFrame(); 235 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 236 CurFrame->Lsda = Sym; 237 CurFrame->LsdaEncoding = Encoding; 238 return false; 239} 240 241bool MCStreamer::EmitCFIRememberState() { 242 EnsureValidFrame(); 243 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 244 MCSymbol *Label = getContext().CreateTempSymbol(); 245 EmitLabel(Label); 246 MCCFIInstruction Instruction(MCCFIInstruction::Remember, Label); 247 CurFrame->Instructions.push_back(Instruction); 248 return false; 249} 250 251bool MCStreamer::EmitCFIRestoreState() { 252 // FIXME: Error if there is no matching cfi_remember_state. 253 EnsureValidFrame(); 254 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); 255 MCSymbol *Label = getContext().CreateTempSymbol(); 256 EmitLabel(Label); 257 MCCFIInstruction Instruction(MCCFIInstruction::Restore, Label); 258 CurFrame->Instructions.push_back(Instruction); 259 return false; 260} 261 262/// EmitRawText - If this file is backed by an assembly streamer, this dumps 263/// the specified string in the output .s file. This capability is 264/// indicated by the hasRawTextSupport() predicate. 265void MCStreamer::EmitRawText(StringRef String) { 266 errs() << "EmitRawText called on an MCStreamer that doesn't support it, " 267 " something must not be fully mc'ized\n"; 268 abort(); 269} 270 271void MCStreamer::EmitRawText(const Twine &T) { 272 SmallString<128> Str; 273 T.toVector(Str); 274 EmitRawText(Str.str()); 275} 276