blob: b3c47367056d3d7ac1e33013625805c0724dcab7 [file] [log] [blame]
Daniel Dunbara11af532009-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"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000011#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000012#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000015#include "llvm/MC/MCInst.h"
Chris Lattner90edac02009-09-14 03:02:37 +000016#include "llvm/MC/MCInstPrinter.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000017#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000018#include "llvm/MC/MCSymbol.h"
Chris Lattner4c42a6d2010-03-19 05:48:53 +000019#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000023#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000024#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000025#include "llvm/Support/FormattedStream.h"
Rafael Espindola195a0ce2010-11-19 02:26:16 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000027using namespace llvm;
28
29namespace {
30
Chris Lattner46a947d2009-08-17 04:17:34 +000031class MCAsmStreamer : public MCStreamer {
Chris Lattner86e22112010-01-22 07:29:22 +000032 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000033 const MCAsmInfo &MAI;
Chris Lattner4c42a6d2010-03-19 05:48:53 +000034 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000035 OwningPtr<MCCodeEmitter> Emitter;
Jim Grosbach00545e12010-09-22 18:16:55 +000036
Chris Lattner86e22112010-01-22 07:29:22 +000037 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000038 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000039
Rafael Espindola195a0ce2010-11-19 02:26:16 +000040 const TargetLoweringObjectFile *TLOF;
41 int PointerSize;
42
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000043 unsigned IsLittleEndian : 1;
44 unsigned IsVerboseAsm : 1;
45 unsigned ShowInst : 1;
46
Chris Lattner46a947d2009-08-17 04:17:34 +000047public:
Chris Lattner86e22112010-01-22 07:29:22 +000048 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindola195a0ce2010-11-19 02:26:16 +000049 bool isLittleEndian, bool isVerboseAsm,
50 const TargetLoweringObjectFile *tlof, int pointerSize,
51 MCInstPrinter *printer, MCCodeEmitter *emitter, bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000052 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
53 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
Rafael Espindola195a0ce2010-11-19 02:26:16 +000054 TLOF(tlof), PointerSize(pointerSize),
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000055 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
Daniel Dunbar5532cf42010-02-10 01:41:14 +000056 ShowInst(showInst) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000057 if (InstPrinter && IsVerboseAsm)
58 InstPrinter->setCommentStream(CommentStream);
59 }
Chris Lattner46a947d2009-08-17 04:17:34 +000060 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000061
Chris Lattner16582022010-01-20 06:39:07 +000062 bool isLittleEndian() const { return IsLittleEndian; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000063
Chris Lattner86e22112010-01-22 07:29:22 +000064 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000065 // If we don't have any comments, just emit a \n.
66 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000067 OS << '\n';
68 return;
69 }
70 EmitCommentsAndEOL();
71 }
72 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000073
74 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
75 /// all.
76 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000077
Chris Lattner91bead72010-04-03 21:35:55 +000078 /// hasRawTextSupport - We support EmitRawText.
79 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000080
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000081 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000082 /// file if applicable as a QoI issue to make the output of the compiler
83 /// more readable. This only affects the MCAsmStreamer, and only when
84 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000085 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000086
87 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
88 virtual void AddEncodingComment(const MCInst &Inst);
89
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000090 /// GetCommentOS - Return a raw_ostream that comments can be written to.
91 /// Unlike AddComment, you are required to terminate comments with \n if you
92 /// use this method.
93 virtual raw_ostream &GetCommentOS() {
94 if (!IsVerboseAsm)
95 return nulls(); // Discard comments unless in verbose asm mode.
96 return CommentStream;
97 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000098
Chris Lattner0fd90fd2010-01-22 19:52:01 +000099 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
100 virtual void AddBlankLine() {
101 EmitEOL();
102 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000103
Chris Lattner46a947d2009-08-17 04:17:34 +0000104 /// @name MCStreamer Interface
105 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000106
Chris Lattner975780b2009-08-17 05:49:08 +0000107 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000108
Rafael Espindolad80781b2010-09-15 21:48:40 +0000109 virtual void InitSections() {
110 // FIXME, this is MachO specific, but the testsuite
111 // expects this.
112 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
113 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
114 0, SectionKind::getText()));
115 }
116
Chris Lattner46a947d2009-08-17 04:17:34 +0000117 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000119 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000120 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000121
Daniel Dunbar821e3332009-08-31 08:09:28 +0000122 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000123 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000124
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000125 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000126
Chris Lattner46a947d2009-08-17 04:17:34 +0000127 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000128 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
129 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
130 virtual void EmitCOFFSymbolType(int Type);
131 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000132 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000133 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000134 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000135
Chris Lattner9eb158d2010-01-23 07:47:02 +0000136 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
137 ///
138 /// @param Symbol - The common symbol to emit.
139 /// @param Size - The size of the common symbol.
140 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000141
Daniel Dunbar8751b942009-08-28 05:48:22 +0000142 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000143 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000144
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000145 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
146 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000147
Chris Lattneraaec2052010-01-19 19:46:13 +0000148 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000149
Chris Lattneraaec2052010-01-19 19:46:13 +0000150 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000151
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000152 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000153
154 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
155
156 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
157
Chris Lattner718fb592010-01-25 21:28:50 +0000158 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000159
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000160
Chris Lattneraaec2052010-01-19 19:46:13 +0000161 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
162 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000163
Chris Lattner46a947d2009-08-17 04:17:34 +0000164 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
165 unsigned ValueSize = 1,
166 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000167
Kevin Enderby6e720482010-02-23 18:26:34 +0000168 virtual void EmitCodeAlignment(unsigned ByteAlignment,
169 unsigned MaxBytesToEmit = 0);
170
Daniel Dunbar821e3332009-08-31 08:09:28 +0000171 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000172 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000173
Chris Lattnera6594fc2010-01-25 18:58:59 +0000174 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000175 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
176 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
177 unsigned Column, unsigned Flags,
178 unsigned Isa, unsigned Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000179
180 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000181
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000182 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000183 /// the specified string in the output .s file. This capability is
184 /// indicated by the hasRawTextSupport() predicate.
185 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000186
Chris Lattner46a947d2009-08-17 04:17:34 +0000187 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000188
Chris Lattner46a947d2009-08-17 04:17:34 +0000189 /// @}
190};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000191
Chris Lattner46a947d2009-08-17 04:17:34 +0000192} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000193
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000194/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000195/// file if applicable as a QoI issue to make the output of the compiler
196/// more readable. This only affects the MCAsmStreamer, and only when
197/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000198void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000199 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000200
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000201 // Make sure that CommentStream is flushed.
202 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000203
Chris Lattner86e22112010-01-22 07:29:22 +0000204 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000205 // Each comment goes on its own line.
206 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000207
Chris Lattner14ca1772010-01-22 21:16:10 +0000208 // Tell the comment stream that the vector changed underneath it.
209 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000210}
211
212void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000213 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
214 OS << '\n';
215 return;
216 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000217
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000218 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000219 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000220
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000221 assert(Comments.back() == '\n' &&
222 "Comment array not newline terminated");
223 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000224 // Emit a line of comments.
225 OS.PadToColumn(MAI.getCommentColumn());
226 size_t Position = Comments.find('\n');
227 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000228
Chris Lattner86e22112010-01-22 07:29:22 +0000229 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000230 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000231
Chris Lattner14ca1772010-01-22 21:16:10 +0000232 CommentToEmit.clear();
233 // Tell the comment stream that the vector changed underneath it.
234 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000235}
236
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000237static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
238 assert(Bytes && "Invalid size!");
239 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
240}
241
Chris Lattner975780b2009-08-17 05:49:08 +0000242void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000243 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000244 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000245 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000246 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000247 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000248 }
249}
250
251void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000252 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000253 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000254 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000255
Chris Lattnere07b75e2010-09-22 22:19:53 +0000256 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000257 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000258 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000259}
260
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000261void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000262 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000263 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000264 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000265 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000266 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000267 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000268 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000269 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000270}
271
Jim Grosbachce792992010-11-05 22:08:08 +0000272void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
273 // This needs to emit to a temporary string to get properly quoted
274 // MCSymbols when they have spaces in them.
275 OS << "\t.thumb_func";
276 if (Func)
277 OS << '\t' << *Func;
278 EmitEOL();
279}
280
Daniel Dunbar821e3332009-08-31 08:09:28 +0000281void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000282 OS << *Symbol << " = " << *Value;
283 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000284
285 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000286 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000287}
288
Rafael Espindola484291c2010-11-01 14:28:48 +0000289void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
290 OS << ".weakref " << *Alias << ", " << *Symbol;
291 EmitEOL();
292}
293
Daniel Dunbarfffff912009-10-16 01:34:54 +0000294void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000295 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000296 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000297 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000298 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
299 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
300 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
301 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
302 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
303 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000304 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000305 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000306 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000307 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
308 switch (Attribute) {
309 default: assert(0 && "Unknown ELF .type");
310 case MCSA_ELF_TypeFunction: OS << "function"; break;
311 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
312 case MCSA_ELF_TypeObject: OS << "object"; break;
313 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
314 case MCSA_ELF_TypeCommon: OS << "common"; break;
315 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000316 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000317 }
318 EmitEOL();
319 return;
320 case MCSA_Global: // .globl/.global
321 OS << MAI.getGlobalDirective();
322 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000323 case MCSA_Hidden: OS << "\t.hidden\t"; break;
324 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
325 case MCSA_Internal: OS << "\t.internal\t"; break;
326 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
327 case MCSA_Local: OS << "\t.local\t"; break;
328 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
329 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
330 case MCSA_Protected: OS << "\t.protected\t"; break;
331 case MCSA_Reference: OS << "\t.reference\t"; break;
332 case MCSA_Weak: OS << "\t.weak\t"; break;
333 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000334 // .weak_reference
335 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000336 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000337 }
338
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000339 OS << *Symbol;
340 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000341}
342
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000343void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000344 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
345 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000346}
347
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000348void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
349 OS << "\t.def\t " << *Symbol << ';';
350 EmitEOL();
351}
352
353void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
354 OS << "\t.scl\t" << StorageClass << ';';
355 EmitEOL();
356}
357
358void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
359 OS << "\t.type\t" << Type << ';';
360 EmitEOL();
361}
362
363void MCAsmStreamer::EndCOFFSymbolDef() {
364 OS << "\t.endef";
365 EmitEOL();
366}
367
Chris Lattner99328ad2010-01-25 07:52:13 +0000368void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
369 assert(MAI.hasDotTypeDotSizeDirective());
370 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
371}
372
Chris Lattner9eb158d2010-01-23 07:47:02 +0000373void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000374 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000375 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000376 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000377 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000378 OS << ',' << ByteAlignment;
379 else
380 OS << ',' << Log2_32(ByteAlignment);
381 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000382 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000383}
384
Chris Lattner9eb158d2010-01-23 07:47:02 +0000385/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
386///
387/// @param Symbol - The common symbol to emit.
388/// @param Size - The size of the common symbol.
389void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
390 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
391 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
392 EmitEOL();
393}
394
Daniel Dunbar8751b942009-08-28 05:48:22 +0000395void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000396 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000397 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000398 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000399
Chris Lattner93b6db32009-08-08 23:39:42 +0000400 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000401 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000402 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000403
Chris Lattner9be3fee2009-07-10 22:20:30 +0000404 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000405 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000406 if (ByteAlignment != 0)
407 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000408 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000409 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000410}
411
Eric Christopherd04d98d2010-05-17 02:13:02 +0000412// .tbss sym, size, align
413// This depends that the symbol has already been mangled from the original,
414// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000415void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
416 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000417 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000418 // Instead of using the Section we'll just use the shortcut.
419 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000420 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000421
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000422 // Output align if we have it. We default to 1 so don't bother printing
423 // that.
424 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000425
Eric Christopher482eba02010-05-14 01:50:28 +0000426 EmitEOL();
427}
428
Chris Lattner12e555c2010-01-23 00:15:00 +0000429static inline char toOctal(int X) { return (X&7)+'0'; }
430
Chris Lattnera6594fc2010-01-25 18:58:59 +0000431static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
432 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000433
Chris Lattnera6594fc2010-01-25 18:58:59 +0000434 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
435 unsigned char C = Data[i];
436 if (C == '"' || C == '\\') {
437 OS << '\\' << (char)C;
438 continue;
439 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000440
Chris Lattnera6594fc2010-01-25 18:58:59 +0000441 if (isprint((unsigned char)C)) {
442 OS << (char)C;
443 continue;
444 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000445
Chris Lattnera6594fc2010-01-25 18:58:59 +0000446 switch (C) {
447 case '\b': OS << "\\b"; break;
448 case '\f': OS << "\\f"; break;
449 case '\n': OS << "\\n"; break;
450 case '\r': OS << "\\r"; break;
451 case '\t': OS << "\\t"; break;
452 default:
453 OS << '\\';
454 OS << toOctal(C >> 6);
455 OS << toOctal(C >> 3);
456 OS << toOctal(C >> 0);
457 break;
458 }
459 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000460
Chris Lattnera6594fc2010-01-25 18:58:59 +0000461 OS << '"';
462}
463
464
Chris Lattneraaec2052010-01-19 19:46:13 +0000465void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000466 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000467 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000468
Chris Lattner12e555c2010-01-23 00:15:00 +0000469 if (Data.size() == 1) {
470 OS << MAI.getData8bitsDirective(AddrSpace);
471 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000472 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000473 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000474 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000475
476 // If the data ends with 0 and the target supports .asciz, use it, otherwise
477 // use .ascii
478 if (MAI.getAscizDirective() && Data.back() == 0) {
479 OS << MAI.getAscizDirective();
480 Data = Data.substr(0, Data.size()-1);
481 } else {
482 OS << MAI.getAsciiDirective();
483 }
484
Chris Lattnera6594fc2010-01-25 18:58:59 +0000485 OS << ' ';
486 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000487 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000488}
489
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000490/// EmitIntValue - Special case of EmitValue that avoids the client having
491/// to pass in a MCExpr for constant integers.
492void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
493 unsigned AddrSpace) {
494 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000495 const char *Directive = 0;
496 switch (Size) {
497 default: break;
498 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
499 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
500 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000501 case 8:
502 Directive = MAI.getData64bitsDirective(AddrSpace);
503 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
504 if (Directive) break;
505 if (isLittleEndian()) {
506 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
507 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
508 } else {
509 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
510 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
511 }
512 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000513 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000514
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000515 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000516 OS << Directive << truncateToSize(Value, Size);
517 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000518}
519
Chris Lattneraaec2052010-01-19 19:46:13 +0000520void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
521 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000522 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000523 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000524 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000525 default: break;
526 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
527 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
528 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
529 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000530 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000531
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000532 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000533 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000534 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000535}
536
Rafael Espindola3ff57092010-11-02 17:22:24 +0000537void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindola73873452010-11-04 18:17:08 +0000538 assert(MAI.hasLEB128() && "Cannot print a .uleb");
539 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000540 EmitEOL();
541}
542
543void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindola73873452010-11-04 18:17:08 +0000544 assert(MAI.hasLEB128() && "Cannot print a .sleb");
545 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000546 EmitEOL();
547}
548
Chris Lattner718fb592010-01-25 21:28:50 +0000549void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
550 assert(MAI.getGPRel32Directive() != 0);
551 OS << MAI.getGPRel32Directive() << *Value;
552 EmitEOL();
553}
554
555
Chris Lattner6113b3d2010-01-19 18:52:28 +0000556/// EmitFill - Emit NumBytes bytes worth of the value specified by
557/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000558void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
559 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000560 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000561
Chris Lattneraaec2052010-01-19 19:46:13 +0000562 if (AddrSpace == 0)
563 if (const char *ZeroDirective = MAI.getZeroDirective()) {
564 OS << ZeroDirective << NumBytes;
565 if (FillValue != 0)
566 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000567 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000568 return;
569 }
570
571 // Emit a byte at a time.
572 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000573}
574
Daniel Dunbar84a29262009-06-24 19:25:34 +0000575void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
576 unsigned ValueSize,
577 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000578 // Some assemblers don't support non-power of two alignments, so we always
579 // emit alignments as a power of two if possible.
580 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000581 switch (ValueSize) {
582 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000583 case 1: OS << MAI.getAlignDirective(); break;
584 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000585 case 2: OS << ".p2alignw "; break;
586 case 4: OS << ".p2alignl "; break;
587 case 8: llvm_unreachable("Unsupported alignment size!");
588 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000589
Chris Lattner33adcfb2009-08-22 21:43:10 +0000590 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000591 OS << ByteAlignment;
592 else
593 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000594
Chris Lattner663c2d22009-08-19 06:12:02 +0000595 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000596 OS << ", 0x";
597 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000598
Jim Grosbach00545e12010-09-22 18:16:55 +0000599 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000600 OS << ", " << MaxBytesToEmit;
601 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000602 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000603 return;
604 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000605
Chris Lattner663c2d22009-08-19 06:12:02 +0000606 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000607 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000608 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000609 default: llvm_unreachable("Invalid size for machine code value!");
610 case 1: OS << ".balign"; break;
611 case 2: OS << ".balignw"; break;
612 case 4: OS << ".balignl"; break;
613 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000614 }
615
Chris Lattner663c2d22009-08-19 06:12:02 +0000616 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000617 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000618 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000619 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000620 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000621}
622
Kevin Enderby6e720482010-02-23 18:26:34 +0000623void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
624 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000625 // Emit with a text fill value.
626 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
627 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000628}
629
Daniel Dunbar821e3332009-08-31 08:09:28 +0000630void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000631 unsigned char Value) {
632 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000633 OS << ".org " << *Offset << ", " << (unsigned) Value;
634 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000635}
636
Chris Lattnera6594fc2010-01-25 18:58:59 +0000637
638void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
639 assert(MAI.hasSingleParameterDotFile());
640 OS << "\t.file\t";
641 PrintQuotedString(Filename, OS);
642 EmitEOL();
643}
644
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000645bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000646 if (!TLOF) {
647 OS << "\t.file\t" << FileNo << ' ';
648 PrintQuotedString(Filename, OS);
649 EmitEOL();
650 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000651 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
652}
653
654void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
655 unsigned Column, unsigned Flags,
656 unsigned Isa,
657 unsigned Discriminator) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000658 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
659 Isa, Discriminator);
660 if (TLOF)
661 return;
662
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000663 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
664 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
665 OS << " basic_block";
666 if (Flags & DWARF2_FLAG_PROLOGUE_END)
667 OS << " prologue_end";
668 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
669 OS << " epilogue_begin";
670
671 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
672 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
673 OS << " is_stmt ";
674
675 if (Flags & DWARF2_FLAG_IS_STMT)
676 OS << "1";
677 else
678 OS << "0";
679 }
680
681 if (Isa)
682 OS << "isa " << Isa;
683 if (Discriminator)
684 OS << "discriminator " << Discriminator;
685 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000686}
687
Daniel Dunbar6b716532010-02-09 23:00:14 +0000688void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
689 raw_ostream &OS = GetCommentOS();
690 SmallString<256> Code;
691 SmallVector<MCFixup, 4> Fixups;
692 raw_svector_ostream VecOS(Code);
693 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
694 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000695
Daniel Dunbar6b716532010-02-09 23:00:14 +0000696 // If we are showing fixups, create symbolic markers in the encoded
697 // representation. We do this by making a per-bit map to the fixup item index,
698 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000699 SmallVector<uint8_t, 64> FixupMap;
700 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000701 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
702 FixupMap[i] = 0;
703
704 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
705 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000706 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000707 for (unsigned j = 0; j != Info.TargetSize; ++j) {
708 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
709 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
710 FixupMap[Index] = 1 + i;
711 }
712 }
713
714 OS << "encoding: [";
715 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
716 if (i)
717 OS << ',';
718
719 // See if all bits are the same map entry.
720 uint8_t MapEntry = FixupMap[i * 8 + 0];
721 for (unsigned j = 1; j != 8; ++j) {
722 if (FixupMap[i * 8 + j] == MapEntry)
723 continue;
724
725 MapEntry = uint8_t(~0U);
726 break;
727 }
728
729 if (MapEntry != uint8_t(~0U)) {
730 if (MapEntry == 0) {
731 OS << format("0x%02x", uint8_t(Code[i]));
732 } else {
733 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
734 OS << char('A' + MapEntry - 1);
735 }
736 } else {
737 // Otherwise, write out in binary.
738 OS << "0b";
739 for (unsigned j = 8; j--;) {
740 unsigned Bit = (Code[i] >> j) & 1;
Chris Lattner3170a3b2010-11-15 05:56:19 +0000741
742 unsigned FixupBit;
743 if (IsLittleEndian)
744 FixupBit = i * 8 + j;
745 else
746 FixupBit = i * 8 + (7-j);
747
748 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +0000749 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
750 OS << char('A' + MapEntry - 1);
751 } else
752 OS << Bit;
753 }
754 }
755 }
756 OS << "]\n";
757
758 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
759 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000760 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000761 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000762 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000763 }
764}
765
766void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
767 assert(CurSection && "Cannot emit contents before setting section!");
768
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000769 if (TLOF)
770 MCLineEntry::Make(this, getCurrentSection());
771
Daniel Dunbar6b716532010-02-09 23:00:14 +0000772 // Show the encoding in a comment if we have a code emitter.
773 if (Emitter)
774 AddEncodingComment(Inst);
775
Chris Lattner30d9a642010-02-09 00:54:51 +0000776 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000777 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000778 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000779 GetCommentOS() << "\n";
780 }
781
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000782 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000783 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000784 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000785 else
786 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000787 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000788}
789
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000790/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000791/// the specified string in the output .s file. This capability is
792/// indicated by the hasRawTextSupport() predicate.
793void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000794 if (!String.empty() && String.back() == '\n')
795 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000796 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000797 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000798}
799
Daniel Dunbara11af532009-06-24 01:03:06 +0000800void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000801 // Dump out the dwarf file & directory tables and line tables.
802 if (getContext().hasDwarfFiles() && TLOF) {
803 MCDwarfFileTable::Emit(this, TLOF->getDwarfLineSection(), NULL,
804 PointerSize);
805 }
Daniel Dunbara11af532009-06-24 01:03:06 +0000806}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000807
Chris Lattner86e22112010-01-22 07:29:22 +0000808MCStreamer *llvm::createAsmStreamer(MCContext &Context,
809 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000810 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000811 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000812 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000813 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000814 NULL, 0, IP, CE, ShowInst);
815}
816
817
818MCStreamer *llvm::createAsmStreamerNoLoc(MCContext &Context,
819 formatted_raw_ostream &OS,
820 bool isLittleEndian,
821 bool isVerboseAsm,
822 const TargetLoweringObjectFile *TLOF,
823 int PointerSize,
824 MCInstPrinter *IP,
825 MCCodeEmitter *CE, bool ShowInst) {
826 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
827 TLOF, PointerSize, IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000828}