blob: 099ce3048db408182e664cb48a82882c5453bda0 [file] [log] [blame]
Daniel Dunbar9faf2732009-06-24 01:03:06 +00001//===- lib/MC/MCAsmStreamer.cpp - Text Assembly 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/MCStreamer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/OwningPtr.h"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000016#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar65105172009-08-27 00:51:57 +000017#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar9faf2732009-06-24 01:03:06 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000019#include "llvm/MC/MCExpr.h"
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000020#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar23a72aa2009-07-01 06:35:03 +000021#include "llvm/MC/MCInst.h"
Chris Lattner11b2fc92009-09-14 03:02:37 +000022#include "llvm/MC/MCInstPrinter.h"
Evan Cheng2833ad12011-07-26 20:57:44 +000023#include "llvm/MC/MCObjectFileInfo.h"
Evan Chengd60fa58b2011-07-18 20:57:22 +000024#include "llvm/MC/MCRegisterInfo.h"
Evan Cheng76792992011-07-20 05:58:47 +000025#include "llvm/MC/MCSectionCOFF.h"
Chris Lattner6c203912009-08-10 18:15:01 +000026#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar9faf2732009-06-24 01:03:06 +000027#include "llvm/MC/MCSymbol.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000028#include "llvm/Support/CommandLine.h"
Torok Edwin56d06592009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar65105172009-08-27 00:51:57 +000030#include "llvm/Support/Format.h"
Chris Lattner38e92192010-01-22 07:29:22 +000031#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/MathExtras.h"
Rafael Espindola3bc8e712013-06-11 22:21:28 +000033#include "llvm/Support/Path.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000034#include <cctype>
Daniel Dunbar9faf2732009-06-24 01:03:06 +000035using namespace llvm;
36
37namespace {
38
Chris Lattner962c5bd82009-08-17 04:17:34 +000039class MCAsmStreamer : public MCStreamer {
Bill Wendlinge3031142011-06-17 20:35:21 +000040protected:
Chris Lattner38e92192010-01-22 07:29:22 +000041 formatted_raw_ostream &OS;
Bill Wendlingbc07a892013-06-18 07:20:20 +000042 const MCAsmInfo *MAI;
Bill Wendlinge3031142011-06-17 20:35:21 +000043private:
Chris Lattner90a78592010-03-19 05:48:53 +000044 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +000045 OwningPtr<MCCodeEmitter> Emitter;
Evan Cheng5928e692011-07-25 23:24:55 +000046 OwningPtr<MCAsmBackend> AsmBackend;
Jim Grosbach3bde49a2010-09-22 18:16:55 +000047
Chris Lattner38e92192010-01-22 07:29:22 +000048 SmallString<128> CommentToEmit;
Chris Lattner8fa0e352010-01-22 19:17:48 +000049 raw_svector_ostream CommentStream;
Daniel Dunbare3ee3322010-02-03 18:18:30 +000050
Daniel Dunbare3ee3322010-02-03 18:18:30 +000051 unsigned IsVerboseAsm : 1;
52 unsigned ShowInst : 1;
Rafael Espindola0a017a62010-12-10 07:39:47 +000053 unsigned UseLoc : 1;
Rafael Espindolaa3181d12011-04-30 03:44:37 +000054 unsigned UseCFI : 1;
Nick Lewycky40f8f2f2011-10-17 23:05:28 +000055 unsigned UseDwarfDirectory : 1;
Daniel Dunbare3ee3322010-02-03 18:18:30 +000056
Rafael Espindolabde52bc2011-04-30 16:34:57 +000057 enum EHSymbolFlags { EHGlobal = 1,
58 EHWeakDefinition = 1 << 1,
59 EHPrivateExtern = 1 << 2 };
60 DenseMap<const MCSymbol*, unsigned> FlagMap;
61
Rafael Espindola1c8ac8f2010-12-04 03:21:47 +000062 bool needsSet(const MCExpr *Value);
63
Rafael Espindola08600bc2011-05-30 20:20:15 +000064 void EmitRegisterName(int64_t Register);
Rafael Espindola38241202012-01-07 22:42:19 +000065 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
Rafael Espindolaf28213c2012-01-09 00:17:29 +000066 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame);
Rafael Espindola08600bc2011-05-30 20:20:15 +000067
Chris Lattner962c5bd82009-08-17 04:17:34 +000068public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +000069 MCAsmStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
70 formatted_raw_ostream &os, bool isVerboseAsm, bool useLoc,
71 bool useCFI, bool useDwarfDirectory, MCInstPrinter *printer,
72 MCCodeEmitter *emitter, MCAsmBackend *asmbackend, bool showInst)
73 : MCStreamer(Context, TargetStreamer), OS(os), MAI(Context.getAsmInfo()),
74 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
75 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
76 ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI),
77 UseDwarfDirectory(useDwarfDirectory) {
Chris Lattner482bf692010-02-10 00:10:18 +000078 if (InstPrinter && IsVerboseAsm)
79 InstPrinter->setCommentStream(CommentStream);
80 }
Chris Lattner962c5bd82009-08-17 04:17:34 +000081 ~MCAsmStreamer() {}
Daniel Dunbar9faf2732009-06-24 01:03:06 +000082
Chris Lattner38e92192010-01-22 07:29:22 +000083 inline void EmitEOL() {
Chris Lattner8fa0e352010-01-22 19:17:48 +000084 // If we don't have any comments, just emit a \n.
85 if (!IsVerboseAsm) {
Chris Lattner38e92192010-01-22 07:29:22 +000086 OS << '\n';
87 return;
88 }
89 EmitCommentsAndEOL();
90 }
91 void EmitCommentsAndEOL();
Chris Lattnerb0d44c32010-02-02 23:37:42 +000092
93 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
94 /// all.
95 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach3bde49a2010-09-22 18:16:55 +000096
Chris Lattner8a87fb72010-04-03 21:35:55 +000097 /// hasRawTextSupport - We support EmitRawText.
98 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar9a0a4612010-02-09 23:00:14 +000099
Chris Lattnere1d8a312010-01-22 18:21:35 +0000100 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner38e92192010-01-22 07:29:22 +0000101 /// file if applicable as a QoI issue to make the output of the compiler
102 /// more readable. This only affects the MCAsmStreamer, and only when
103 /// verbose assembly output is enabled.
Chris Lattnere1d8a312010-01-22 18:21:35 +0000104 virtual void AddComment(const Twine &T);
Daniel Dunbar9a0a4612010-02-09 23:00:14 +0000105
106 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
107 virtual void AddEncodingComment(const MCInst &Inst);
108
Chris Lattner8fa0e352010-01-22 19:17:48 +0000109 /// GetCommentOS - Return a raw_ostream that comments can be written to.
110 /// Unlike AddComment, you are required to terminate comments with \n if you
111 /// use this method.
112 virtual raw_ostream &GetCommentOS() {
113 if (!IsVerboseAsm)
114 return nulls(); // Discard comments unless in verbose asm mode.
115 return CommentStream;
116 }
Daniel Dunbar9a0a4612010-02-09 23:00:14 +0000117
Chris Lattnera3eee3c2010-01-22 19:52:01 +0000118 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
119 virtual void AddBlankLine() {
120 EmitEOL();
121 }
Daniel Dunbar9a0a4612010-02-09 23:00:14 +0000122
Chris Lattner962c5bd82009-08-17 04:17:34 +0000123 /// @name MCStreamer Interface
124 /// @{
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000125
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000126 virtual void ChangeSection(const MCSection *Section,
127 const MCExpr *Subsection);
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000128
Rafael Espindolaf667d922010-09-15 21:48:40 +0000129 virtual void InitSections() {
Eli Benderskycbb25142013-01-14 19:04:57 +0000130 InitToTextSection();
131 }
132
133 virtual void InitToTextSection() {
Nico Rieck334c7bc2013-04-14 21:18:36 +0000134 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
Rafael Espindolaf667d922010-09-15 21:48:40 +0000135 }
136
Chris Lattner962c5bd82009-08-17 04:17:34 +0000137 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000138 virtual void EmitDebugLabel(MCSymbol *Symbol);
139
Rafael Espindolabde52bc2011-04-30 16:34:57 +0000140 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
141 MCSymbol *EHSymbol);
Chris Lattner685508c2010-01-23 06:39:22 +0000142 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar16004b82013-01-18 01:25:48 +0000143 virtual void EmitLinkerOptions(ArrayRef<std::string> Options);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000144 virtual void EmitDataRegion(MCDataRegionType Kind);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000145 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000146
Daniel Dunbar897ffad2009-08-31 08:09:28 +0000147 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola16145972010-11-01 14:28:48 +0000148 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000149 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
150 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000151 const MCSymbol *Label,
152 unsigned PointerSize);
Rafael Espindola1d1eced2011-04-30 03:21:04 +0000153 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
154 const MCSymbol *Label);
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000155
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000156 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbyc9d93ef2009-07-13 21:03:15 +0000157
Chris Lattner962c5bd82009-08-17 04:17:34 +0000158 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner72afa952010-05-08 19:54:22 +0000159 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
160 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
161 virtual void EmitCOFFSymbolType(int Type);
162 virtual void EndCOFFSymbolDef();
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000163 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000164 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattner91dac6d2010-01-25 07:52:13 +0000165 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnerb1301f72010-01-23 07:47:02 +0000166 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000167 unsigned ByteAlignment);
Kevin Enderby4c21caa2009-07-14 18:17:10 +0000168
Chris Lattnerb1301f72010-01-23 07:47:02 +0000169 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
170 ///
171 /// @param Symbol - The common symbol to emit.
172 /// @param Size - The size of the common symbol.
Dmitri Gribenko65340a62012-08-23 16:54:08 +0000173 /// @param ByteAlignment - The alignment of the common symbol in bytes.
Benjamin Kramer63970512011-09-01 23:04:27 +0000174 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
175 unsigned ByteAlignment);
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000176
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000177 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000178 uint64_t Size = 0, unsigned ByteAlignment = 0);
Kevin Enderbycbe475d2009-07-14 21:35:03 +0000179
Eric Christopher5c87be72010-05-18 21:16:04 +0000180 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
181 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000182
Rafael Espindola64e1af82013-07-02 15:49:13 +0000183 virtual void EmitBytes(StringRef Data);
Chris Lattnera1e11f52009-07-07 20:30:46 +0000184
Rafael Espindola64e1af82013-07-02 15:49:13 +0000185 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size);
186 virtual void EmitIntValue(uint64_t Value, unsigned Size);
Rafael Espindola5e874982010-11-02 17:22:24 +0000187
Rafael Espindola6aea5922011-04-21 23:39:26 +0000188 virtual void EmitULEB128Value(const MCExpr *Value);
Rafael Espindola5e874982010-11-02 17:22:24 +0000189
Rafael Espindola6aea5922011-04-21 23:39:26 +0000190 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindola5e874982010-11-02 17:22:24 +0000191
Akira Hatanakaf0b08442012-02-03 04:33:00 +0000192 virtual void EmitGPRel64Value(const MCExpr *Value);
193
Chris Lattner3cde7602010-01-25 21:28:50 +0000194 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000195
Chris Lattnerdc50e5d2010-01-19 22:03:38 +0000196
Rafael Espindola64e1af82013-07-02 15:49:13 +0000197 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue);
Chris Lattner07cadaf2009-07-10 22:20:30 +0000198
Chris Lattner962c5bd82009-08-17 04:17:34 +0000199 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
200 unsigned ValueSize = 1,
201 unsigned MaxBytesToEmit = 0);
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000202
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000203 virtual void EmitCodeAlignment(unsigned ByteAlignment,
204 unsigned MaxBytesToEmit = 0);
205
Jim Grosbachb5912772012-01-27 00:37:08 +0000206 virtual bool EmitValueToOffset(const MCExpr *Offset,
Chris Lattner962c5bd82009-08-17 04:17:34 +0000207 unsigned char Value = 0);
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000208
Chris Lattner601ef332010-01-25 18:58:59 +0000209 virtual void EmitFileDirective(StringRef Filename);
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000210 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
Manman Ren1e427202013-03-07 01:42:00 +0000211 StringRef Filename, unsigned CUID = 0);
Rafael Espindolac653a892010-11-16 21:20:32 +0000212 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
213 unsigned Column, unsigned Flags,
Devang Patel17740e72011-04-18 20:26:49 +0000214 unsigned Isa, unsigned Discriminator,
215 StringRef FileName);
Chris Lattner601ef332010-01-25 18:58:59 +0000216
Rafael Espindola5645bad2013-10-16 01:05:45 +0000217 virtual void EmitIdent(StringRef IdentString);
Rafael Espindolaec53aa92011-05-10 13:39:48 +0000218 virtual void EmitCFISections(bool EH, bool Debug);
Rafael Espindola539d96f2011-04-12 23:59:07 +0000219 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
220 virtual void EmitCFIDefCfaOffset(int64_t Offset);
221 virtual void EmitCFIDefCfaRegister(int64_t Register);
222 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
223 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
224 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
225 virtual void EmitCFIRememberState();
226 virtual void EmitCFIRestoreState();
227 virtual void EmitCFISameValue(int64_t Register);
228 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
229 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
Rafael Espindola3c47e372012-01-23 21:51:52 +0000230 virtual void EmitCFISignalFrame();
Rafael Espindola9bb24782012-11-23 16:59:41 +0000231 virtual void EmitCFIUndefined(int64_t Register);
Rafael Espindolacdb9a532012-11-25 15:14:49 +0000232 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000233 virtual void EmitCFIWindowSave();
Rafael Espindola3c227b02010-11-22 14:27:24 +0000234
Charles Davisc7250fa2011-05-22 21:12:15 +0000235 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
Charles Davis8d9c9902011-05-18 04:58:05 +0000236 virtual void EmitWin64EHEndProc();
Charles Davis77e06102011-05-18 20:54:10 +0000237 virtual void EmitWin64EHStartChained();
238 virtual void EmitWin64EHEndChained();
Charles Davis4cd88562011-05-19 17:46:39 +0000239 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
240 bool Except);
241 virtual void EmitWin64EHHandlerData();
Charles Davis7e6e8952011-05-19 19:35:55 +0000242 virtual void EmitWin64EHPushReg(unsigned Register);
243 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
244 virtual void EmitWin64EHAllocStack(unsigned Size);
245 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
246 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
Charles Davis8d9c9902011-05-18 04:58:05 +0000247 virtual void EmitWin64EHPushFrame(bool Code);
248 virtual void EmitWin64EHEndProlog();
249
Chris Lattner601ef332010-01-25 18:58:59 +0000250 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000251
Eli Benderskyf483ff92012-12-20 19:05:53 +0000252 virtual void EmitBundleAlignMode(unsigned AlignPow2);
Eli Bendersky802b6282013-01-07 21:51:08 +0000253 virtual void EmitBundleLock(bool AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +0000254 virtual void EmitBundleUnlock();
255
Jim Grosbach6ebd7282010-09-22 18:18:30 +0000256 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner8a87fb72010-04-03 21:35:55 +0000257 /// the specified string in the output .s file. This capability is
258 /// indicated by the hasRawTextSupport() predicate.
David Blaikied8c5b4e2013-10-24 22:43:10 +0000259 virtual void EmitRawTextImpl(StringRef String);
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000260
Rafael Espindola07082092012-01-07 03:13:18 +0000261 virtual void FinishImpl();
Chris Lattner962c5bd82009-08-17 04:17:34 +0000262};
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000263
Chris Lattner962c5bd82009-08-17 04:17:34 +0000264} // end anonymous namespace.
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000265
Chris Lattnere1d8a312010-01-22 18:21:35 +0000266/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner38e92192010-01-22 07:29:22 +0000267/// file if applicable as a QoI issue to make the output of the compiler
268/// more readable. This only affects the MCAsmStreamer, and only when
269/// verbose assembly output is enabled.
Chris Lattnere1d8a312010-01-22 18:21:35 +0000270void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner38e92192010-01-22 07:29:22 +0000271 if (!IsVerboseAsm) return;
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000272
Chris Lattner8fa0e352010-01-22 19:17:48 +0000273 // Make sure that CommentStream is flushed.
274 CommentStream.flush();
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000275
Chris Lattner38e92192010-01-22 07:29:22 +0000276 T.toVector(CommentToEmit);
Chris Lattner8fa0e352010-01-22 19:17:48 +0000277 // Each comment goes on its own line.
278 CommentToEmit.push_back('\n');
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000279
Chris Lattner1386a882010-01-22 21:16:10 +0000280 // Tell the comment stream that the vector changed underneath it.
281 CommentStream.resync();
Chris Lattner38e92192010-01-22 07:29:22 +0000282}
283
284void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattner8fa0e352010-01-22 19:17:48 +0000285 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
286 OS << '\n';
287 return;
288 }
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000289
Chris Lattner8fa0e352010-01-22 19:17:48 +0000290 CommentStream.flush();
Chris Lattner38e92192010-01-22 07:29:22 +0000291 StringRef Comments = CommentToEmit.str();
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000292
Chris Lattner8fa0e352010-01-22 19:17:48 +0000293 assert(Comments.back() == '\n' &&
294 "Comment array not newline terminated");
295 do {
Chris Lattner38e92192010-01-22 07:29:22 +0000296 // Emit a line of comments.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000297 OS.PadToColumn(MAI->getCommentColumn());
Chris Lattner38e92192010-01-22 07:29:22 +0000298 size_t Position = Comments.find('\n');
Bill Wendlingbc07a892013-06-18 07:20:20 +0000299 OS << MAI->getCommentString() << ' ' << Comments.substr(0, Position) <<'\n';
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000300
Chris Lattner38e92192010-01-22 07:29:22 +0000301 Comments = Comments.substr(Position+1);
Chris Lattner8fa0e352010-01-22 19:17:48 +0000302 } while (!Comments.empty());
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000303
Chris Lattner1386a882010-01-22 21:16:10 +0000304 CommentToEmit.clear();
305 // Tell the comment stream that the vector changed underneath it.
306 CommentStream.resync();
Chris Lattner38e92192010-01-22 07:29:22 +0000307}
308
Daniel Dunbar188e87f2009-06-25 21:03:18 +0000309static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
310 assert(Bytes && "Invalid size!");
311 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
312}
313
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000314void MCAsmStreamer::ChangeSection(const MCSection *Section,
315 const MCExpr *Subsection) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000316 assert(Section && "Cannot switch to a null section!");
Bill Wendlingbc07a892013-06-18 07:20:20 +0000317 Section->PrintSwitchToSection(*MAI, OS, Subsection);
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000318}
319
Rafael Espindolabde52bc2011-04-30 16:34:57 +0000320void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
321 MCSymbol *EHSymbol) {
322 if (UseCFI)
323 return;
324
325 unsigned Flags = FlagMap.lookup(Symbol);
326
327 if (Flags & EHGlobal)
328 EmitSymbolAttribute(EHSymbol, MCSA_Global);
329 if (Flags & EHWeakDefinition)
330 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
331 if (Flags & EHPrivateExtern)
332 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
333}
334
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000335void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar6860ac72009-08-22 07:22:36 +0000336 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola27c0c9b2011-04-27 15:21:19 +0000337 MCStreamer::EmitLabel(Symbol);
Daniel Dunbarf782ebc2009-06-24 17:00:42 +0000338
Bill Wendlingbc07a892013-06-18 07:20:20 +0000339 OS << *Symbol << MAI->getLabelSuffix();
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000340 EmitEOL();
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000341}
342
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000343void MCAsmStreamer::EmitDebugLabel(MCSymbol *Symbol) {
344 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
345 MCStreamer::EmitDebugLabel(Symbol);
346
Bill Wendlingbc07a892013-06-18 07:20:20 +0000347 OS << *Symbol << MAI->getDebugLabelSuffix();
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000348 EmitEOL();
349}
350
Chris Lattner685508c2010-01-23 06:39:22 +0000351void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbydd27e5e2009-07-16 17:56:39 +0000352 switch (Flag) {
Jason W Kim645f6c22010-09-30 02:45:56 +0000353 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattner685508c2010-01-23 06:39:22 +0000354 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Bill Wendlingbc07a892013-06-18 07:20:20 +0000355 case MCAF_Code16: OS << '\t'<< MAI->getCode16Directive();break;
356 case MCAF_Code32: OS << '\t'<< MAI->getCode32Directive();break;
357 case MCAF_Code64: OS << '\t'<< MAI->getCode64Directive();break;
Kevin Enderbydd27e5e2009-07-16 17:56:39 +0000358 }
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000359 EmitEOL();
Kevin Enderbyc9d93ef2009-07-13 21:03:15 +0000360}
361
Daniel Dunbar16004b82013-01-18 01:25:48 +0000362void MCAsmStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
363 assert(!Options.empty() && "At least one option is required!");
364 OS << "\t.linker_option \"" << Options[0] << '"';
365 for (ArrayRef<std::string>::iterator it = Options.begin() + 1,
366 ie = Options.end(); it != ie; ++it) {
367 OS << ", " << '"' << *it << '"';
368 }
Daniel Dunbar95856122013-01-18 19:37:00 +0000369 OS << "\n";
Daniel Dunbar16004b82013-01-18 01:25:48 +0000370}
371
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000372void MCAsmStreamer::EmitDataRegion(MCDataRegionType Kind) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000373 if (!MAI->doesSupportDataRegionDirectives())
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000374 return;
375 switch (Kind) {
376 case MCDR_DataRegion: OS << "\t.data_region"; break;
377 case MCDR_DataRegionJT8: OS << "\t.data_region jt8"; break;
378 case MCDR_DataRegionJT16: OS << "\t.data_region jt16"; break;
379 case MCDR_DataRegionJT32: OS << "\t.data_region jt32"; break;
380 case MCDR_DataRegionEnd: OS << "\t.end_data_region"; break;
381 }
382 EmitEOL();
383}
384
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000385void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
386 // This needs to emit to a temporary string to get properly quoted
387 // MCSymbols when they have spaces in them.
388 OS << "\t.thumb_func";
Rafael Espindolae90c1cb2011-05-16 16:17:21 +0000389 // Only Mach-O hasSubsectionsViaSymbols()
Bill Wendlingbc07a892013-06-18 07:20:20 +0000390 if (MAI->hasSubsectionsViaSymbols())
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000391 OS << '\t' << *Func;
392 EmitEOL();
393}
394
Daniel Dunbar897ffad2009-08-31 08:09:28 +0000395void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000396 OS << *Symbol << " = " << *Value;
397 EmitEOL();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000398
399 // FIXME: Lift context changes into super class.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000400 Symbol->setVariableValue(Value);
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000401}
402
Rafael Espindola16145972010-11-01 14:28:48 +0000403void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
404 OS << ".weakref " << *Alias << ", " << *Symbol;
405 EmitEOL();
406}
407
Rafael Espindola57ab7082010-12-03 00:55:40 +0000408void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
409 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000410 const MCSymbol *Label,
411 unsigned PointerSize) {
412 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000413}
414
Rafael Espindola1d1eced2011-04-30 03:21:04 +0000415void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
416 const MCSymbol *Label) {
417 EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
418 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
Rafael Espindola8e129292011-05-19 21:40:34 +0000419 AddrDelta = ForceExpAbs(AddrDelta);
Rafael Espindola1d1eced2011-04-30 03:21:04 +0000420 EmitValue(AddrDelta, 4);
421}
422
423
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000424bool MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000425 MCSymbolAttr Attribute) {
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000426 switch (Attribute) {
Craig Toppera2886c22012-02-07 05:05:23 +0000427 case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute");
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000428 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
429 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
430 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
431 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
432 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
433 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindola2d006b12010-11-13 04:55:06 +0000434 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000435 if (!MAI->hasDotTypeDotSizeDirective())
436 return false; // Symbol attribute not supported
Dan Gohman77fe07a2010-02-04 01:42:13 +0000437 OS << "\t.type\t" << *Symbol << ','
Bill Wendlingbc07a892013-06-18 07:20:20 +0000438 << ((MAI->getCommentString()[0] != '@') ? '@' : '%');
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000439 switch (Attribute) {
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000440 default: return false;
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000441 case MCSA_ELF_TypeFunction: OS << "function"; break;
442 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
443 case MCSA_ELF_TypeObject: OS << "object"; break;
444 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
445 case MCSA_ELF_TypeCommon: OS << "common"; break;
446 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindola2d006b12010-11-13 04:55:06 +0000447 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000448 }
449 EmitEOL();
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000450 return true;
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000451 case MCSA_Global: // .globl/.global
Bill Wendlingbc07a892013-06-18 07:20:20 +0000452 OS << MAI->getGlobalDirective();
Rafael Espindolabde52bc2011-04-30 16:34:57 +0000453 FlagMap[Symbol] |= EHGlobal;
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000454 break;
Chris Lattner79d20752010-06-21 20:35:01 +0000455 case MCSA_Hidden: OS << "\t.hidden\t"; break;
456 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
457 case MCSA_Internal: OS << "\t.internal\t"; break;
458 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
459 case MCSA_Local: OS << "\t.local\t"; break;
460 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderby8be14412010-11-19 18:39:33 +0000461 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Rafael Espindolabde52bc2011-04-30 16:34:57 +0000462 case MCSA_PrivateExtern:
463 OS << "\t.private_extern\t";
464 FlagMap[Symbol] |= EHPrivateExtern;
465 break;
Chris Lattner79d20752010-06-21 20:35:01 +0000466 case MCSA_Protected: OS << "\t.protected\t"; break;
467 case MCSA_Reference: OS << "\t.reference\t"; break;
468 case MCSA_Weak: OS << "\t.weak\t"; break;
Rafael Espindolabde52bc2011-04-30 16:34:57 +0000469 case MCSA_WeakDefinition:
470 OS << "\t.weak_definition\t";
471 FlagMap[Symbol] |= EHWeakDefinition;
472 break;
Chris Lattner685508c2010-01-23 06:39:22 +0000473 // .weak_reference
Bill Wendlingbc07a892013-06-18 07:20:20 +0000474 case MCSA_WeakReference: OS << MAI->getWeakRefDirective(); break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000475 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000476 }
477
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000478 OS << *Symbol;
479 EmitEOL();
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000480
481 return true;
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000482}
483
Kevin Enderby4c21caa2009-07-14 18:17:10 +0000484void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000485 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
486 EmitEOL();
Kevin Enderby4c21caa2009-07-14 18:17:10 +0000487}
488
Chris Lattner72afa952010-05-08 19:54:22 +0000489void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
490 OS << "\t.def\t " << *Symbol << ';';
491 EmitEOL();
492}
493
494void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
495 OS << "\t.scl\t" << StorageClass << ';';
496 EmitEOL();
497}
498
499void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
500 OS << "\t.type\t" << Type << ';';
501 EmitEOL();
502}
503
504void MCAsmStreamer::EndCOFFSymbolDef() {
505 OS << "\t.endef";
506 EmitEOL();
507}
508
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000509void MCAsmStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
510 OS << "\t.secidx\t" << *Symbol;
511 EmitEOL();
512}
513
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000514void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000515 OS << "\t.secrel32\t" << *Symbol;
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000516 EmitEOL();
517}
518
Chris Lattner91dac6d2010-01-25 07:52:13 +0000519void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000520 assert(MAI->hasDotTypeDotSizeDirective());
Chris Lattner91dac6d2010-01-25 07:52:13 +0000521 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
522}
523
Chris Lattnerb1301f72010-01-23 07:47:02 +0000524void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000525 unsigned ByteAlignment) {
Richard Mitton089ed892013-09-23 17:56:20 +0000526 // Common symbols do not belong to any actual section.
527 AssignSection(Symbol, NULL);
Richard Mitton21101b32013-09-19 23:21:01 +0000528
Chris Lattnerb1301f72010-01-23 07:47:02 +0000529 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner0375d2f2010-01-25 07:29:13 +0000530 if (ByteAlignment != 0) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000531 if (MAI->getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner95b98952010-01-19 06:01:04 +0000532 OS << ',' << ByteAlignment;
533 else
534 OS << ',' << Log2_32(ByteAlignment);
535 }
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000536 EmitEOL();
Chris Lattnera1e11f52009-07-07 20:30:46 +0000537}
538
Chris Lattnerb1301f72010-01-23 07:47:02 +0000539/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
540///
541/// @param Symbol - The common symbol to emit.
542/// @param Size - The size of the common symbol.
Benjamin Kramer63970512011-09-01 23:04:27 +0000543void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
544 unsigned ByteAlign) {
Richard Mitton089ed892013-09-23 17:56:20 +0000545 // Common symbols do not belong to any actual section.
546 AssignSection(Symbol, NULL);
Richard Mitton21101b32013-09-19 23:21:01 +0000547
Chris Lattnerb1301f72010-01-23 07:47:02 +0000548 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
Benjamin Kramer63970512011-09-01 23:04:27 +0000549 if (ByteAlign > 1) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000550 switch (MAI->getLCOMMDirectiveAlignmentType()) {
Benjamin Kramer68b9f052012-09-07 21:08:01 +0000551 case LCOMM::NoAlignment:
552 llvm_unreachable("alignment not supported on .lcomm!");
553 case LCOMM::ByteAlignment:
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000554 OS << ',' << ByteAlign;
Benjamin Kramer68b9f052012-09-07 21:08:01 +0000555 break;
556 case LCOMM::Log2Alignment:
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000557 assert(isPowerOf2_32(ByteAlign) && "alignment must be a power of 2");
558 OS << ',' << Log2_32(ByteAlign);
Benjamin Kramer68b9f052012-09-07 21:08:01 +0000559 break;
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000560 }
Benjamin Kramer63970512011-09-01 23:04:27 +0000561 }
Chris Lattnerb1301f72010-01-23 07:47:02 +0000562 EmitEOL();
563}
564
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000565void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000566 uint64_t Size, unsigned ByteAlignment) {
Richard Mitton21101b32013-09-19 23:21:01 +0000567 if (Symbol)
568 AssignSection(Symbol, Section);
569
Daniel Dunbaraba5fb82009-08-13 23:36:34 +0000570 // Note: a .zerofill directive does not switch sections.
Chris Lattner591105c2009-08-08 23:39:42 +0000571 OS << ".zerofill ";
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000572
Chris Lattner591105c2009-08-08 23:39:42 +0000573 // This is a mach-o specific directive.
Chris Lattnercb307a272009-08-10 01:39:42 +0000574 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar563a7e82009-08-14 18:51:45 +0000575 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000576
Chris Lattner07cadaf2009-07-10 22:20:30 +0000577 if (Symbol != NULL) {
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000578 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000579 if (ByteAlignment != 0)
580 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner07cadaf2009-07-10 22:20:30 +0000581 }
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000582 EmitEOL();
Chris Lattner07cadaf2009-07-10 22:20:30 +0000583}
584
Eric Christopher68b1bbe2010-05-17 02:13:02 +0000585// .tbss sym, size, align
586// This depends that the symbol has already been mangled from the original,
587// e.g. _a.
Eric Christopher5c87be72010-05-18 21:16:04 +0000588void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
589 uint64_t Size, unsigned ByteAlignment) {
Richard Mitton21101b32013-09-19 23:21:01 +0000590 AssignSection(Symbol, Section);
591
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000592 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher5c87be72010-05-18 21:16:04 +0000593 // Instead of using the Section we'll just use the shortcut.
594 // This is a mach-o specific directive and section.
Eric Christopher68b1bbe2010-05-17 02:13:02 +0000595 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000596
Eric Christopher5c87be72010-05-18 21:16:04 +0000597 // Output align if we have it. We default to 1 so don't bother printing
598 // that.
599 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000600
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000601 EmitEOL();
602}
603
Chris Lattnerded9af632010-01-23 00:15:00 +0000604static inline char toOctal(int X) { return (X&7)+'0'; }
605
Chris Lattner601ef332010-01-25 18:58:59 +0000606static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
607 OS << '"';
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000608
Chris Lattner601ef332010-01-25 18:58:59 +0000609 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
610 unsigned char C = Data[i];
611 if (C == '"' || C == '\\') {
612 OS << '\\' << (char)C;
613 continue;
614 }
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000615
Chris Lattner601ef332010-01-25 18:58:59 +0000616 if (isprint((unsigned char)C)) {
617 OS << (char)C;
618 continue;
619 }
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000620
Chris Lattner601ef332010-01-25 18:58:59 +0000621 switch (C) {
622 case '\b': OS << "\\b"; break;
623 case '\f': OS << "\\f"; break;
624 case '\n': OS << "\\n"; break;
625 case '\r': OS << "\\r"; break;
626 case '\t': OS << "\\t"; break;
627 default:
628 OS << '\\';
629 OS << toOctal(C >> 6);
630 OS << toOctal(C >> 3);
631 OS << toOctal(C >> 0);
632 break;
633 }
634 }
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000635
Chris Lattner601ef332010-01-25 18:58:59 +0000636 OS << '"';
637}
638
639
Rafael Espindola64e1af82013-07-02 15:49:13 +0000640void MCAsmStreamer::EmitBytes(StringRef Data) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000641 assert(getCurrentSection().first &&
642 "Cannot emit contents before setting section!");
Chris Lattnerded9af632010-01-23 00:15:00 +0000643 if (Data.empty()) return;
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000644
Chris Lattnerded9af632010-01-23 00:15:00 +0000645 if (Data.size() == 1) {
Rafael Espindola64e1af82013-07-02 15:49:13 +0000646 OS << MAI->getData8bitsDirective();
Chris Lattnerded9af632010-01-23 00:15:00 +0000647 OS << (unsigned)(unsigned char)Data[0];
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000648 EmitEOL();
Chris Lattnerded9af632010-01-23 00:15:00 +0000649 return;
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000650 }
Chris Lattnerded9af632010-01-23 00:15:00 +0000651
652 // If the data ends with 0 and the target supports .asciz, use it, otherwise
653 // use .ascii
Bill Wendlingbc07a892013-06-18 07:20:20 +0000654 if (MAI->getAscizDirective() && Data.back() == 0) {
655 OS << MAI->getAscizDirective();
Chris Lattnerded9af632010-01-23 00:15:00 +0000656 Data = Data.substr(0, Data.size()-1);
657 } else {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000658 OS << MAI->getAsciiDirective();
Chris Lattnerded9af632010-01-23 00:15:00 +0000659 }
660
Chris Lattner601ef332010-01-25 18:58:59 +0000661 PrintQuotedString(Data, OS);
Chris Lattnerded9af632010-01-23 00:15:00 +0000662 EmitEOL();
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000663}
664
Rafael Espindola64e1af82013-07-02 15:49:13 +0000665void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
666 EmitValue(MCConstantExpr::Create(Value, getContext()), Size);
Rafael Espindola4c70eea2010-12-03 02:54:21 +0000667}
668
Rafael Espindola64e1af82013-07-02 15:49:13 +0000669void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000670 assert(getCurrentSection().first &&
671 "Cannot emit contents before setting section!");
Chris Lattnerdc50e5d2010-01-19 22:03:38 +0000672 const char *Directive = 0;
673 switch (Size) {
674 default: break;
Rafael Espindola64e1af82013-07-02 15:49:13 +0000675 case 1: Directive = MAI->getData8bitsDirective(); break;
676 case 2: Directive = MAI->getData16bitsDirective(); break;
677 case 4: Directive = MAI->getData32bitsDirective(); break;
Chris Lattner45eeffc2010-01-20 06:45:39 +0000678 case 8:
Rafael Espindola64e1af82013-07-02 15:49:13 +0000679 Directive = MAI->getData64bitsDirective();
Chris Lattner45eeffc2010-01-20 06:45:39 +0000680 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
681 if (Directive) break;
Rafael Espindolae5e1f9a2010-11-28 23:22:44 +0000682 int64_t IntValue;
683 if (!Value->EvaluateAsAbsolute(IntValue))
684 report_fatal_error("Don't know how to emit this value.");
Bill Wendlingbc07a892013-06-18 07:20:20 +0000685 if (MAI->isLittleEndian()) {
Rafael Espindola64e1af82013-07-02 15:49:13 +0000686 EmitIntValue((uint32_t)(IntValue >> 0 ), 4);
687 EmitIntValue((uint32_t)(IntValue >> 32), 4);
Chris Lattner45eeffc2010-01-20 06:45:39 +0000688 } else {
Rafael Espindola64e1af82013-07-02 15:49:13 +0000689 EmitIntValue((uint32_t)(IntValue >> 32), 4);
690 EmitIntValue((uint32_t)(IntValue >> 0 ), 4);
Chris Lattner45eeffc2010-01-20 06:45:39 +0000691 }
692 return;
Chris Lattnerdc50e5d2010-01-19 22:03:38 +0000693 }
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000694
Chris Lattnerdc50e5d2010-01-19 22:03:38 +0000695 assert(Directive && "Invalid size for machine code value!");
Chris Lattner3cde7602010-01-25 21:28:50 +0000696 OS << Directive << *Value;
Chris Lattner38e92192010-01-22 07:29:22 +0000697 EmitEOL();
Daniel Dunbar9faf2732009-06-24 01:03:06 +0000698}
699
Rafael Espindola6aea5922011-04-21 23:39:26 +0000700void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindola92ca9332010-11-19 04:10:13 +0000701 int64_t IntValue;
702 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola6aea5922011-04-21 23:39:26 +0000703 EmitULEB128IntValue(IntValue);
Rafael Espindola92ca9332010-11-19 04:10:13 +0000704 return;
705 }
Bill Wendlingbc07a892013-06-18 07:20:20 +0000706 assert(MAI->hasLEB128() && "Cannot print a .uleb");
Rafael Espindola38d07562010-11-04 18:17:08 +0000707 OS << ".uleb128 " << *Value;
Rafael Espindola5e874982010-11-02 17:22:24 +0000708 EmitEOL();
709}
710
Rafael Espindola6aea5922011-04-21 23:39:26 +0000711void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindola92ca9332010-11-19 04:10:13 +0000712 int64_t IntValue;
713 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola6aea5922011-04-21 23:39:26 +0000714 EmitSLEB128IntValue(IntValue);
Rafael Espindola92ca9332010-11-19 04:10:13 +0000715 return;
716 }
Bill Wendlingbc07a892013-06-18 07:20:20 +0000717 assert(MAI->hasLEB128() && "Cannot print a .sleb");
Rafael Espindola38d07562010-11-04 18:17:08 +0000718 OS << ".sleb128 " << *Value;
Rafael Espindola5e874982010-11-02 17:22:24 +0000719 EmitEOL();
720}
721
Akira Hatanakaf0b08442012-02-03 04:33:00 +0000722void MCAsmStreamer::EmitGPRel64Value(const MCExpr *Value) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000723 assert(MAI->getGPRel64Directive() != 0);
724 OS << MAI->getGPRel64Directive() << *Value;
Akira Hatanakaf0b08442012-02-03 04:33:00 +0000725 EmitEOL();
726}
727
Chris Lattner3cde7602010-01-25 21:28:50 +0000728void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000729 assert(MAI->getGPRel32Directive() != 0);
730 OS << MAI->getGPRel32Directive() << *Value;
Chris Lattner3cde7602010-01-25 21:28:50 +0000731 EmitEOL();
732}
733
734
Chris Lattner4340cb32010-01-19 18:52:28 +0000735/// EmitFill - Emit NumBytes bytes worth of the value specified by
736/// FillValue. This implements directives such as '.space'.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000737void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
Chris Lattnered89f602010-01-19 18:58:52 +0000738 if (NumBytes == 0) return;
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000739
Rafael Espindola64e1af82013-07-02 15:49:13 +0000740 if (const char *ZeroDirective = MAI->getZeroDirective()) {
741 OS << ZeroDirective << NumBytes;
742 if (FillValue != 0)
743 OS << ',' << (int)FillValue;
744 EmitEOL();
745 return;
746 }
Chris Lattnerc35681b2010-01-19 19:46:13 +0000747
748 // Emit a byte at a time.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000749 MCStreamer::EmitFill(NumBytes, FillValue);
Chris Lattner4340cb32010-01-19 18:52:28 +0000750}
751
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000752void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
753 unsigned ValueSize,
754 unsigned MaxBytesToEmit) {
Chris Lattner37b72342009-08-19 06:12:02 +0000755 // Some assemblers don't support non-power of two alignments, so we always
756 // emit alignments as a power of two if possible.
757 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner78b23b02009-08-19 06:35:36 +0000758 switch (ValueSize) {
759 default: llvm_unreachable("Invalid size for machine code value!");
Bill Wendlingbc07a892013-06-18 07:20:20 +0000760 case 1: OS << MAI->getAlignDirective(); break;
Chris Lattnere9a75a62009-08-22 21:43:10 +0000761 // FIXME: use MAI for this!
Chris Lattner78b23b02009-08-19 06:35:36 +0000762 case 2: OS << ".p2alignw "; break;
763 case 4: OS << ".p2alignl "; break;
764 case 8: llvm_unreachable("Unsupported alignment size!");
765 }
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000766
Bill Wendlingbc07a892013-06-18 07:20:20 +0000767 if (MAI->getAlignmentIsInBytes())
Chris Lattner37b72342009-08-19 06:12:02 +0000768 OS << ByteAlignment;
769 else
770 OS << Log2_32(ByteAlignment);
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000771
Chris Lattner37b72342009-08-19 06:12:02 +0000772 if (Value || MaxBytesToEmit) {
Chris Lattnerf16a1222009-09-03 04:01:10 +0000773 OS << ", 0x";
774 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner37b72342009-08-19 06:12:02 +0000775
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000776 if (MaxBytesToEmit)
Chris Lattner37b72342009-08-19 06:12:02 +0000777 OS << ", " << MaxBytesToEmit;
778 }
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000779 EmitEOL();
Chris Lattner37b72342009-08-19 06:12:02 +0000780 return;
781 }
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000782
Chris Lattner37b72342009-08-19 06:12:02 +0000783 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattnere9a75a62009-08-22 21:43:10 +0000784 // FIXME: Parameterize this based on MAI.
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000785 switch (ValueSize) {
Chris Lattner37b72342009-08-19 06:12:02 +0000786 default: llvm_unreachable("Invalid size for machine code value!");
787 case 1: OS << ".balign"; break;
788 case 2: OS << ".balignw"; break;
789 case 4: OS << ".balignl"; break;
790 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000791 }
792
Chris Lattner37b72342009-08-19 06:12:02 +0000793 OS << ' ' << ByteAlignment;
Daniel Dunbar188e87f2009-06-25 21:03:18 +0000794 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach3bde49a2010-09-22 18:16:55 +0000795 if (MaxBytesToEmit)
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000796 OS << ", " << MaxBytesToEmit;
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000797 EmitEOL();
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000798}
799
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000800void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
801 unsigned MaxBytesToEmit) {
Chris Lattnereebaf6a2010-02-23 18:44:31 +0000802 // Emit with a text fill value.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000803 EmitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
Chris Lattnereebaf6a2010-02-23 18:44:31 +0000804 1, MaxBytesToEmit);
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000805}
806
Jim Grosbachb5912772012-01-27 00:37:08 +0000807bool MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000808 unsigned char Value) {
809 // FIXME: Verify that Offset is associated with the current section.
Chris Lattnercfa5ebc32010-01-22 07:36:39 +0000810 OS << ".org " << *Offset << ", " << (unsigned) Value;
811 EmitEOL();
Jim Grosbachb5912772012-01-27 00:37:08 +0000812 return false;
Daniel Dunbar71ea89d2009-06-24 19:25:34 +0000813}
814
Chris Lattner601ef332010-01-25 18:58:59 +0000815
816void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000817 assert(MAI->hasSingleParameterDotFile());
Chris Lattner601ef332010-01-25 18:58:59 +0000818 OS << "\t.file\t";
819 PrintQuotedString(Filename, OS);
820 EmitEOL();
821}
822
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000823bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
Manman Ren1e427202013-03-07 01:42:00 +0000824 StringRef Filename, unsigned CUID) {
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000825 if (!UseDwarfDirectory && !Directory.empty()) {
826 if (sys::path::is_absolute(Filename))
Manman Ren1e427202013-03-07 01:42:00 +0000827 return EmitDwarfFileDirective(FileNo, "", Filename, CUID);
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000828
829 SmallString<128> FullPathName = Directory;
830 sys::path::append(FullPathName, Filename);
Manman Ren1e427202013-03-07 01:42:00 +0000831 return EmitDwarfFileDirective(FileNo, "", FullPathName, CUID);
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000832 }
833
Rafael Espindola0a017a62010-12-10 07:39:47 +0000834 if (UseLoc) {
Rafael Espindolab58867c2010-11-19 02:26:16 +0000835 OS << "\t.file\t" << FileNo << ' ';
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000836 if (!Directory.empty()) {
837 PrintQuotedString(Directory, OS);
838 OS << ' ';
839 }
Rafael Espindolab58867c2010-11-19 02:26:16 +0000840 PrintQuotedString(Filename, OS);
841 EmitEOL();
Manman Ren1e427202013-03-07 01:42:00 +0000842 // All .file will belong to a single CUID.
843 CUID = 0;
Rafael Espindolab58867c2010-11-19 02:26:16 +0000844 }
Manman Ren1e427202013-03-07 01:42:00 +0000845 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Directory, Filename,
846 CUID);
Rafael Espindolac653a892010-11-16 21:20:32 +0000847}
848
849void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
850 unsigned Column, unsigned Flags,
851 unsigned Isa,
Devang Patel17740e72011-04-18 20:26:49 +0000852 unsigned Discriminator,
853 StringRef FileName) {
Rafael Espindolab58867c2010-11-19 02:26:16 +0000854 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Devang Patel17740e72011-04-18 20:26:49 +0000855 Isa, Discriminator, FileName);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000856 if (!UseLoc)
Rafael Espindolab58867c2010-11-19 02:26:16 +0000857 return;
858
Rafael Espindolac653a892010-11-16 21:20:32 +0000859 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
860 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
861 OS << " basic_block";
862 if (Flags & DWARF2_FLAG_PROLOGUE_END)
863 OS << " prologue_end";
864 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
865 OS << " epilogue_begin";
866
867 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
868 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
869 OS << " is_stmt ";
870
871 if (Flags & DWARF2_FLAG_IS_STMT)
872 OS << "1";
873 else
874 OS << "0";
875 }
876
877 if (Isa)
878 OS << "isa " << Isa;
879 if (Discriminator)
880 OS << "discriminator " << Discriminator;
Devang Patel17740e72011-04-18 20:26:49 +0000881
882 if (IsVerboseAsm) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000883 OS.PadToColumn(MAI->getCommentColumn());
884 OS << MAI->getCommentString() << ' ' << FileName << ':'
Devang Patel17740e72011-04-18 20:26:49 +0000885 << Line << ':' << Column;
886 }
Rafael Espindolac653a892010-11-16 21:20:32 +0000887 EmitEOL();
Chris Lattner601ef332010-01-25 18:58:59 +0000888}
889
Rafael Espindola5645bad2013-10-16 01:05:45 +0000890void MCAsmStreamer::EmitIdent(StringRef IdentString) {
891 assert(MAI->hasIdentDirective() && ".ident directive not supported");
892 OS << "\t.ident\t";
893 PrintQuotedString(IdentString, OS);
894 EmitEOL();
895}
896
Rafael Espindolaec53aa92011-05-10 13:39:48 +0000897void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
898 MCStreamer::EmitCFISections(EH, Debug);
899
900 if (!UseCFI)
901 return;
902
903 OS << "\t.cfi_sections ";
904 if (EH) {
905 OS << ".eh_frame";
906 if (Debug)
907 OS << ", .debug_frame";
908 } else if (Debug) {
909 OS << ".debug_frame";
910 }
911
912 EmitEOL();
913}
914
Rafael Espindola38241202012-01-07 22:42:19 +0000915void MCAsmStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
916 if (!UseCFI) {
917 RecordProcStart(Frame);
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000918 return;
Rafael Espindola38241202012-01-07 22:42:19 +0000919 }
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000920
Anton Korobeynikove2bea1c2011-01-14 21:57:39 +0000921 OS << "\t.cfi_startproc";
Rafael Espindola3c227b02010-11-22 14:27:24 +0000922 EmitEOL();
Rafael Espindola3c227b02010-11-22 14:27:24 +0000923}
924
Rafael Espindolaf28213c2012-01-09 00:17:29 +0000925void MCAsmStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
926 if (!UseCFI) {
927 RecordProcEnd(Frame);
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000928 return;
Rafael Espindolaf28213c2012-01-09 00:17:29 +0000929 }
930
931 // Put a dummy non-null value in Frame.End to mark that this frame has been
932 // closed.
933 Frame.End = (MCSymbol *) 1;
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000934
Anton Korobeynikove2bea1c2011-01-14 21:57:39 +0000935 OS << "\t.cfi_endproc";
Rafael Espindola3c227b02010-11-22 14:27:24 +0000936 EmitEOL();
Rafael Espindola3c227b02010-11-22 14:27:24 +0000937}
938
Rafael Espindola08600bc2011-05-30 20:20:15 +0000939void MCAsmStreamer::EmitRegisterName(int64_t Register) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000940 if (InstPrinter && !MAI->useDwarfRegNumForCFI()) {
941 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
942 unsigned LLVMRegister = MRI->getLLVMRegNum(Register, true);
Rafael Espindolad6860522011-06-02 02:34:55 +0000943 InstPrinter->printRegName(OS, LLVMRegister);
Rafael Espindola08600bc2011-05-30 20:20:15 +0000944 } else {
945 OS << Register;
946 }
947}
948
Rafael Espindola539d96f2011-04-12 23:59:07 +0000949void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindola6c961e12011-04-29 21:41:06 +0000950 MCStreamer::EmitCFIDefCfa(Register, Offset);
951
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000952 if (!UseCFI)
953 return;
954
Rafael Espindola08600bc2011-05-30 20:20:15 +0000955 OS << "\t.cfi_def_cfa ";
956 EmitRegisterName(Register);
957 OS << ", " << Offset;
Rafael Espindola6c961e12011-04-29 21:41:06 +0000958 EmitEOL();
Rafael Espindola539d96f2011-04-12 23:59:07 +0000959}
960
961void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
962 MCStreamer::EmitCFIDefCfaOffset(Offset);
Rafael Espindola3c227b02010-11-22 14:27:24 +0000963
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000964 if (!UseCFI)
965 return;
966
Anton Korobeynikove2bea1c2011-01-14 21:57:39 +0000967 OS << "\t.cfi_def_cfa_offset " << Offset;
Rafael Espindola3c227b02010-11-22 14:27:24 +0000968 EmitEOL();
Rafael Espindola3c227b02010-11-22 14:27:24 +0000969}
970
Rafael Espindola539d96f2011-04-12 23:59:07 +0000971void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
972 MCStreamer::EmitCFIDefCfaRegister(Register);
Rafael Espindola3c227b02010-11-22 14:27:24 +0000973
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000974 if (!UseCFI)
975 return;
976
Rafael Espindola08600bc2011-05-30 20:20:15 +0000977 OS << "\t.cfi_def_cfa_register ";
978 EmitRegisterName(Register);
Rafael Espindola3c227b02010-11-22 14:27:24 +0000979 EmitEOL();
Rafael Espindola3c227b02010-11-22 14:27:24 +0000980}
981
Rafael Espindola539d96f2011-04-12 23:59:07 +0000982void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
983 this->MCStreamer::EmitCFIOffset(Register, Offset);
Rafael Espindola3c227b02010-11-22 14:27:24 +0000984
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000985 if (!UseCFI)
986 return;
987
Rafael Espindola08600bc2011-05-30 20:20:15 +0000988 OS << "\t.cfi_offset ";
989 EmitRegisterName(Register);
990 OS << ", " << Offset;
Rafael Espindola3c227b02010-11-22 14:27:24 +0000991 EmitEOL();
Rafael Espindola3c227b02010-11-22 14:27:24 +0000992}
993
Rafael Espindola539d96f2011-04-12 23:59:07 +0000994void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola2ac83552010-12-27 00:36:05 +0000995 unsigned Encoding) {
Rafael Espindola539d96f2011-04-12 23:59:07 +0000996 MCStreamer::EmitCFIPersonality(Sym, Encoding);
Rafael Espindola3c227b02010-11-22 14:27:24 +0000997
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000998 if (!UseCFI)
999 return;
1000
Anton Korobeynikove2bea1c2011-01-14 21:57:39 +00001001 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
Rafael Espindola3c227b02010-11-22 14:27:24 +00001002 EmitEOL();
Rafael Espindola3c227b02010-11-22 14:27:24 +00001003}
1004
Rafael Espindola539d96f2011-04-12 23:59:07 +00001005void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
1006 MCStreamer::EmitCFILsda(Sym, Encoding);
Rafael Espindola3c227b02010-11-22 14:27:24 +00001007
Rafael Espindolaa3181d12011-04-30 03:44:37 +00001008 if (!UseCFI)
1009 return;
1010
Anton Korobeynikove2bea1c2011-01-14 21:57:39 +00001011 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
Rafael Espindola3c227b02010-11-22 14:27:24 +00001012 EmitEOL();
Rafael Espindola539d96f2011-04-12 23:59:07 +00001013}
Rafael Espindola3c227b02010-11-22 14:27:24 +00001014
Rafael Espindola539d96f2011-04-12 23:59:07 +00001015void MCAsmStreamer::EmitCFIRememberState() {
1016 MCStreamer::EmitCFIRememberState();
1017
Rafael Espindolaa3181d12011-04-30 03:44:37 +00001018 if (!UseCFI)
1019 return;
1020
Rafael Espindola539d96f2011-04-12 23:59:07 +00001021 OS << "\t.cfi_remember_state";
1022 EmitEOL();
1023}
1024
1025void MCAsmStreamer::EmitCFIRestoreState() {
1026 MCStreamer::EmitCFIRestoreState();
1027
Rafael Espindolaa3181d12011-04-30 03:44:37 +00001028 if (!UseCFI)
1029 return;
1030
Rafael Espindola539d96f2011-04-12 23:59:07 +00001031 OS << "\t.cfi_restore_state";
1032 EmitEOL();
1033}
1034
1035void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
1036 MCStreamer::EmitCFISameValue(Register);
1037
Rafael Espindolaa3181d12011-04-30 03:44:37 +00001038 if (!UseCFI)
1039 return;
1040
Rafael Espindola08600bc2011-05-30 20:20:15 +00001041 OS << "\t.cfi_same_value ";
1042 EmitRegisterName(Register);
Rafael Espindola539d96f2011-04-12 23:59:07 +00001043 EmitEOL();
1044}
1045
1046void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
1047 MCStreamer::EmitCFIRelOffset(Register, Offset);
1048
Rafael Espindolaa3181d12011-04-30 03:44:37 +00001049 if (!UseCFI)
1050 return;
1051
Rafael Espindola08600bc2011-05-30 20:20:15 +00001052 OS << "\t.cfi_rel_offset ";
1053 EmitRegisterName(Register);
1054 OS << ", " << Offset;
Rafael Espindola539d96f2011-04-12 23:59:07 +00001055 EmitEOL();
1056}
1057
1058void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
1059 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
1060
Rafael Espindolaa3181d12011-04-30 03:44:37 +00001061 if (!UseCFI)
1062 return;
1063
Rafael Espindola539d96f2011-04-12 23:59:07 +00001064 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
1065 EmitEOL();
Rafael Espindola3c227b02010-11-22 14:27:24 +00001066}
1067
Rafael Espindola3c47e372012-01-23 21:51:52 +00001068void MCAsmStreamer::EmitCFISignalFrame() {
1069 MCStreamer::EmitCFISignalFrame();
1070
1071 if (!UseCFI)
1072 return;
1073
Eric Christopher368461c2012-05-31 00:53:18 +00001074 OS << "\t.cfi_signal_frame";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001075 EmitEOL();
1076}
1077
Rafael Espindola9bb24782012-11-23 16:59:41 +00001078void MCAsmStreamer::EmitCFIUndefined(int64_t Register) {
1079 MCStreamer::EmitCFIUndefined(Register);
1080
1081 if (!UseCFI)
1082 return;
1083
1084 OS << "\t.cfi_undefined " << Register;
1085 EmitEOL();
1086}
1087
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001088void MCAsmStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
1089 MCStreamer::EmitCFIRegister(Register1, Register2);
1090
1091 if (!UseCFI)
1092 return;
1093
1094 OS << "\t.cfi_register " << Register1 << ", " << Register2;
1095 EmitEOL();
1096}
1097
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001098void MCAsmStreamer::EmitCFIWindowSave() {
1099 MCStreamer::EmitCFIWindowSave();
1100
1101 if (!UseCFI)
1102 return;
1103
1104 OS << "\t.cfi_window_save";
1105 EmitEOL();
1106}
1107
Charles Davisc7250fa2011-05-22 21:12:15 +00001108void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
Charles Davis9a115a42011-05-20 18:19:22 +00001109 MCStreamer::EmitWin64EHStartProc(Symbol);
1110
Charles Davis4cd88562011-05-19 17:46:39 +00001111 OS << ".seh_proc " << *Symbol;
Charles Davis8d9c9902011-05-18 04:58:05 +00001112 EmitEOL();
1113}
1114
Charles Davis27117af2011-05-18 22:13:51 +00001115void MCAsmStreamer::EmitWin64EHEndProc() {
Charles Davis9a115a42011-05-20 18:19:22 +00001116 MCStreamer::EmitWin64EHEndProc();
1117
Charles Davis4cd88562011-05-19 17:46:39 +00001118 OS << "\t.seh_endproc";
Charles Davis8d9c9902011-05-18 04:58:05 +00001119 EmitEOL();
1120}
1121
Charles Davis27117af2011-05-18 22:13:51 +00001122void MCAsmStreamer::EmitWin64EHStartChained() {
Charles Davis9a115a42011-05-20 18:19:22 +00001123 MCStreamer::EmitWin64EHStartChained();
1124
Charles Davis4cd88562011-05-19 17:46:39 +00001125 OS << "\t.seh_startchained";
Charles Davis77e06102011-05-18 20:54:10 +00001126 EmitEOL();
1127}
1128
Charles Davis27117af2011-05-18 22:13:51 +00001129void MCAsmStreamer::EmitWin64EHEndChained() {
Charles Davis9a115a42011-05-20 18:19:22 +00001130 MCStreamer::EmitWin64EHEndChained();
1131
Charles Davis4cd88562011-05-19 17:46:39 +00001132 OS << "\t.seh_endchained";
Charles Davis77e06102011-05-18 20:54:10 +00001133 EmitEOL();
1134}
1135
Charles Davis4cd88562011-05-19 17:46:39 +00001136void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
1137 bool Except) {
Charles Davis9a115a42011-05-20 18:19:22 +00001138 MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
1139
Charles Davis4cd88562011-05-19 17:46:39 +00001140 OS << "\t.seh_handler " << *Sym;
1141 if (Unwind)
1142 OS << ", @unwind";
1143 if (Except)
1144 OS << ", @except";
Charles Davis77e06102011-05-18 20:54:10 +00001145 EmitEOL();
1146}
1147
Evan Cheng76792992011-07-20 05:58:47 +00001148static const MCSection *getWin64EHTableSection(StringRef suffix,
1149 MCContext &context) {
1150 // FIXME: This doesn't belong in MCObjectFileInfo. However,
1151 /// this duplicate code in MCWin64EH.cpp.
1152 if (suffix == "")
1153 return context.getObjectFileInfo()->getXDataSection();
1154 return context.getCOFFSection((".xdata"+suffix).str(),
1155 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1156 COFF::IMAGE_SCN_MEM_READ |
1157 COFF::IMAGE_SCN_MEM_WRITE,
1158 SectionKind::getDataRel());
1159}
1160
Charles Davis4cd88562011-05-19 17:46:39 +00001161void MCAsmStreamer::EmitWin64EHHandlerData() {
Charles Davis9a115a42011-05-20 18:19:22 +00001162 MCStreamer::EmitWin64EHHandlerData();
1163
Charles Davis2f6ecea2011-05-25 21:43:45 +00001164 // Switch sections. Don't call SwitchSection directly, because that will
1165 // cause the section switch to be visible in the emitted assembly.
1166 // We only do this so the section switch that terminates the handler
1167 // data block is visible.
Charles Davis03eef622011-05-27 19:09:24 +00001168 MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
1169 StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
Evan Cheng76792992011-07-20 05:58:47 +00001170 const MCSection *xdataSect = getWin64EHTableSection(suffix, getContext());
Charles Davis2f6ecea2011-05-25 21:43:45 +00001171 if (xdataSect)
1172 SwitchSectionNoChange(xdataSect);
1173
Charles Davis4cd88562011-05-19 17:46:39 +00001174 OS << "\t.seh_handlerdata";
Charles Davis77e06102011-05-18 20:54:10 +00001175 EmitEOL();
1176}
1177
Charles Davis7e6e8952011-05-19 19:35:55 +00001178void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
Charles Davis9a115a42011-05-20 18:19:22 +00001179 MCStreamer::EmitWin64EHPushReg(Register);
1180
Charles Davis4cd88562011-05-19 17:46:39 +00001181 OS << "\t.seh_pushreg " << Register;
Charles Davis8d9c9902011-05-18 04:58:05 +00001182 EmitEOL();
1183}
1184
Charles Davis7e6e8952011-05-19 19:35:55 +00001185void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
Charles Davis9a115a42011-05-20 18:19:22 +00001186 MCStreamer::EmitWin64EHSetFrame(Register, Offset);
1187
Charles Davis4cd88562011-05-19 17:46:39 +00001188 OS << "\t.seh_setframe " << Register << ", " << Offset;
Charles Davis8d9c9902011-05-18 04:58:05 +00001189 EmitEOL();
1190}
1191
Charles Davis7e6e8952011-05-19 19:35:55 +00001192void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
Charles Davis9a115a42011-05-20 18:19:22 +00001193 MCStreamer::EmitWin64EHAllocStack(Size);
1194
Charles Davis4cd88562011-05-19 17:46:39 +00001195 OS << "\t.seh_stackalloc " << Size;
Charles Davis8d9c9902011-05-18 04:58:05 +00001196 EmitEOL();
1197}
1198
Charles Davis7e6e8952011-05-19 19:35:55 +00001199void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
Charles Davis9a115a42011-05-20 18:19:22 +00001200 MCStreamer::EmitWin64EHSaveReg(Register, Offset);
1201
Charles Davis4cd88562011-05-19 17:46:39 +00001202 OS << "\t.seh_savereg " << Register << ", " << Offset;
1203 EmitEOL();
1204}
1205
Charles Davis7e6e8952011-05-19 19:35:55 +00001206void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
Charles Davis9a115a42011-05-20 18:19:22 +00001207 MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
1208
Charles Davis4cd88562011-05-19 17:46:39 +00001209 OS << "\t.seh_savexmm " << Register << ", " << Offset;
Charles Davis8d9c9902011-05-18 04:58:05 +00001210 EmitEOL();
1211}
1212
Charles Davis27117af2011-05-18 22:13:51 +00001213void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
Charles Davis9a115a42011-05-20 18:19:22 +00001214 MCStreamer::EmitWin64EHPushFrame(Code);
1215
Charles Davis4cd88562011-05-19 17:46:39 +00001216 OS << "\t.seh_pushframe";
Charles Davis8d9c9902011-05-18 04:58:05 +00001217 if (Code)
Charles Davis4cd88562011-05-19 17:46:39 +00001218 OS << " @code";
Charles Davis8d9c9902011-05-18 04:58:05 +00001219 EmitEOL();
1220}
1221
Charles Davis27117af2011-05-18 22:13:51 +00001222void MCAsmStreamer::EmitWin64EHEndProlog(void) {
Charles Davis9a115a42011-05-20 18:19:22 +00001223 MCStreamer::EmitWin64EHEndProlog();
1224
Charles Davis4cd88562011-05-19 17:46:39 +00001225 OS << "\t.seh_endprologue";
Charles Davis8d9c9902011-05-18 04:58:05 +00001226 EmitEOL();
1227}
1228
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001229void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
1230 raw_ostream &OS = GetCommentOS();
1231 SmallString<256> Code;
1232 SmallVector<MCFixup, 4> Fixups;
1233 raw_svector_ostream VecOS(Code);
1234 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
1235 VecOS.flush();
Chris Lattner601ef332010-01-25 18:58:59 +00001236
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001237 // If we are showing fixups, create symbolic markers in the encoded
1238 // representation. We do this by making a per-bit map to the fixup item index,
1239 // then trying to display it as nicely as possible.
Daniel Dunbar75c9a4e2010-02-10 01:41:14 +00001240 SmallVector<uint8_t, 64> FixupMap;
1241 FixupMap.resize(Code.size() * 8);
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001242 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1243 FixupMap[i] = 0;
1244
1245 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1246 MCFixup &F = Fixups[i];
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +00001247 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001248 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1249 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1250 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1251 FixupMap[Index] = 1 + i;
1252 }
1253 }
1254
Evan Cheng15bc70f2011-07-08 22:49:42 +00001255 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
Evan Cheng52899a92011-01-13 23:27:39 +00001256 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001257 OS << "encoding: [";
1258 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1259 if (i)
1260 OS << ',';
1261
1262 // See if all bits are the same map entry.
1263 uint8_t MapEntry = FixupMap[i * 8 + 0];
1264 for (unsigned j = 1; j != 8; ++j) {
1265 if (FixupMap[i * 8 + j] == MapEntry)
1266 continue;
1267
1268 MapEntry = uint8_t(~0U);
1269 break;
1270 }
1271
1272 if (MapEntry != uint8_t(~0U)) {
1273 if (MapEntry == 0) {
1274 OS << format("0x%02x", uint8_t(Code[i]));
1275 } else {
Evan Cheng0447d302011-01-13 21:45:26 +00001276 if (Code[i]) {
Evan Cheng52899a92011-01-13 23:27:39 +00001277 // FIXME: Some of the 8 bits require fix up.
Evan Cheng0447d302011-01-13 21:45:26 +00001278 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1279 << char('A' + MapEntry - 1) << '\'';
1280 } else
1281 OS << char('A' + MapEntry - 1);
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001282 }
1283 } else {
1284 // Otherwise, write out in binary.
1285 OS << "0b";
Jay Foad61ea0e42011-06-23 09:09:15 +00001286 for (unsigned j = 8; j--;) {
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001287 unsigned Bit = (Code[i] >> j) & 1;
NAKAMURA Takumiaf669802011-03-27 01:44:40 +00001288
Chris Lattner9e7d8c02010-11-15 05:56:19 +00001289 unsigned FixupBit;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001290 if (MAI->isLittleEndian())
Chris Lattner9e7d8c02010-11-15 05:56:19 +00001291 FixupBit = i * 8 + j;
1292 else
1293 FixupBit = i * 8 + (7-j);
NAKAMURA Takumiaf669802011-03-27 01:44:40 +00001294
Jay Foad61ea0e42011-06-23 09:09:15 +00001295 if (uint8_t MapEntry = FixupMap[FixupBit]) {
1296 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1297 OS << char('A' + MapEntry - 1);
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001298 } else
1299 OS << Bit;
1300 }
1301 }
1302 }
1303 OS << "]\n";
1304
1305 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1306 MCFixup &F = Fixups[i];
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +00001307 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001308 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar60547442010-02-10 04:47:08 +00001309 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001310 }
1311}
1312
1313void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001314 assert(getCurrentSection().first &&
1315 "Cannot emit contents before setting section!");
Daniel Dunbar9a0a4612010-02-09 23:00:14 +00001316
1317 // Show the encoding in a comment if we have a code emitter.
1318 if (Emitter)
1319 AddEncodingComment(Inst);
1320
Chris Lattner89261502010-02-09 00:54:51 +00001321 // Show the MCInst if enabled.
Daniel Dunbar3627af52010-05-26 15:18:13 +00001322 if (ShowInst) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001323 Inst.dump_pretty(GetCommentOS(), MAI, InstPrinter.get(), "\n ");
Daniel Dunbar3627af52010-05-26 15:18:13 +00001324 GetCommentOS() << "\n";
1325 }
1326
Daniel Dunbar04047fb2010-03-22 21:49:34 +00001327 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbare3ee3322010-02-03 18:18:30 +00001328 if (InstPrinter)
Owen Andersona0c3b972011-09-15 23:38:46 +00001329 InstPrinter->printInst(&Inst, OS, "");
Daniel Dunbare3ee3322010-02-03 18:18:30 +00001330 else
Bill Wendlingbc07a892013-06-18 07:20:20 +00001331 Inst.print(OS, MAI);
Chris Lattnercfa5ebc32010-01-22 07:36:39 +00001332 EmitEOL();
Daniel Dunbar9faf2732009-06-24 01:03:06 +00001333}
1334
Eli Benderskyf483ff92012-12-20 19:05:53 +00001335void MCAsmStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
1336 OS << "\t.bundle_align_mode " << AlignPow2;
1337 EmitEOL();
1338}
1339
Eli Bendersky802b6282013-01-07 21:51:08 +00001340void MCAsmStreamer::EmitBundleLock(bool AlignToEnd) {
Eli Benderskyf483ff92012-12-20 19:05:53 +00001341 OS << "\t.bundle_lock";
Eli Bendersky802b6282013-01-07 21:51:08 +00001342 if (AlignToEnd)
1343 OS << " align_to_end";
Eli Benderskyf483ff92012-12-20 19:05:53 +00001344 EmitEOL();
1345}
1346
1347void MCAsmStreamer::EmitBundleUnlock() {
1348 OS << "\t.bundle_unlock";
1349 EmitEOL();
1350}
1351
Jim Grosbach6ebd7282010-09-22 18:18:30 +00001352/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner8a87fb72010-04-03 21:35:55 +00001353/// the specified string in the output .s file. This capability is
1354/// indicated by the hasRawTextSupport() predicate.
David Blaikied8c5b4e2013-10-24 22:43:10 +00001355void MCAsmStreamer::EmitRawTextImpl(StringRef String) {
Chris Lattneraca014e2010-04-03 22:06:56 +00001356 if (!String.empty() && String.back() == '\n')
1357 String = String.substr(0, String.size()-1);
Chris Lattner8a87fb72010-04-03 21:35:55 +00001358 OS << String;
Chris Lattneraca014e2010-04-03 22:06:56 +00001359 EmitEOL();
Chris Lattner8a87fb72010-04-03 21:35:55 +00001360}
1361
Rafael Espindola07082092012-01-07 03:13:18 +00001362void MCAsmStreamer::FinishImpl() {
Rafael Espindolac22c85c2012-02-28 21:13:05 +00001363 // FIXME: This header is duplicated with MCObjectStreamer
Rafael Espindolab58867c2010-11-19 02:26:16 +00001364 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindolac7ecbfb2012-03-03 14:24:15 +00001365 const MCSymbol *LineSectionSymbol = NULL;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001366 if (getContext().hasDwarfFiles() && !UseLoc)
Rafael Espindolac22c85c2012-02-28 21:13:05 +00001367 LineSectionSymbol = MCDwarfFileTable::Emit(this);
Rafael Espindola750cb612011-05-01 04:49:54 +00001368
Kevin Enderbye7739d42011-12-09 18:09:40 +00001369 // If we are generating dwarf for assembly source files dump out the sections.
1370 if (getContext().getGenDwarfForAssembly())
Rafael Espindolac22c85c2012-02-28 21:13:05 +00001371 MCGenDwarfInfo::Emit(this, LineSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001372
Rafael Espindolab6089d62011-05-10 03:14:15 +00001373 if (!UseCFI)
Bill Wendling550c76d2013-09-09 19:48:37 +00001374 EmitFrames(AsmBackend.get(), false);
Daniel Dunbar9faf2732009-06-24 01:03:06 +00001375}
Bill Wendling550c76d2013-09-09 19:48:37 +00001376
Chris Lattner38e92192010-01-22 07:29:22 +00001377MCStreamer *llvm::createAsmStreamer(MCContext &Context,
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001378 MCTargetStreamer *TargetStreamer,
Chris Lattner38e92192010-01-22 07:29:22 +00001379 formatted_raw_ostream &OS,
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001380 bool isVerboseAsm, bool useLoc, bool useCFI,
1381 bool useDwarfDirectory, MCInstPrinter *IP,
1382 MCCodeEmitter *CE, MCAsmBackend *MAB,
1383 bool ShowInst) {
1384 return new MCAsmStreamer(Context, TargetStreamer, OS, isVerboseAsm, useLoc,
1385 useCFI, useDwarfDirectory, IP, CE, MAB, ShowInst);
Daniel Dunbar9faf2732009-06-24 01:03:06 +00001386}