blob: 5dd115d58c611272cda71c4780ca3d99e979ae4c [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"
Daniel Dunbara11af532009-06-24 01:03:06 +000026using namespace llvm;
27
28namespace {
29
Chris Lattner46a947d2009-08-17 04:17:34 +000030class MCAsmStreamer : public MCStreamer {
Chris Lattner86e22112010-01-22 07:29:22 +000031 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000032 const MCAsmInfo &MAI;
Chris Lattner4c42a6d2010-03-19 05:48:53 +000033 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000034 OwningPtr<MCCodeEmitter> Emitter;
Jim Grosbach00545e12010-09-22 18:16:55 +000035
Chris Lattner86e22112010-01-22 07:29:22 +000036 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000037 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000038
39 unsigned IsLittleEndian : 1;
40 unsigned IsVerboseAsm : 1;
41 unsigned ShowInst : 1;
42
Chris Lattner46a947d2009-08-17 04:17:34 +000043public:
Chris Lattner86e22112010-01-22 07:29:22 +000044 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Chris Lattner07404412010-01-22 07:06:15 +000045 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
Daniel Dunbar5532cf42010-02-10 01:41:14 +000046 MCCodeEmitter *emitter, bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000047 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
48 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000049 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
Daniel Dunbar5532cf42010-02-10 01:41:14 +000050 ShowInst(showInst) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000051 if (InstPrinter && IsVerboseAsm)
52 InstPrinter->setCommentStream(CommentStream);
53 }
Chris Lattner46a947d2009-08-17 04:17:34 +000054 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000055
Chris Lattner16582022010-01-20 06:39:07 +000056 bool isLittleEndian() const { return IsLittleEndian; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000057
Chris Lattner86e22112010-01-22 07:29:22 +000058 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000059 // If we don't have any comments, just emit a \n.
60 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000061 OS << '\n';
62 return;
63 }
64 EmitCommentsAndEOL();
65 }
66 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000067
68 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
69 /// all.
70 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000071
Chris Lattner91bead72010-04-03 21:35:55 +000072 /// hasRawTextSupport - We support EmitRawText.
73 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000074
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000075 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000076 /// file if applicable as a QoI issue to make the output of the compiler
77 /// more readable. This only affects the MCAsmStreamer, and only when
78 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000079 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000080
81 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
82 virtual void AddEncodingComment(const MCInst &Inst);
83
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000084 /// GetCommentOS - Return a raw_ostream that comments can be written to.
85 /// Unlike AddComment, you are required to terminate comments with \n if you
86 /// use this method.
87 virtual raw_ostream &GetCommentOS() {
88 if (!IsVerboseAsm)
89 return nulls(); // Discard comments unless in verbose asm mode.
90 return CommentStream;
91 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000092
Chris Lattner0fd90fd2010-01-22 19:52:01 +000093 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
94 virtual void AddBlankLine() {
95 EmitEOL();
96 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000097
Chris Lattner46a947d2009-08-17 04:17:34 +000098 /// @name MCStreamer Interface
99 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000100
Chris Lattner975780b2009-08-17 05:49:08 +0000101 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000102
Rafael Espindolad80781b2010-09-15 21:48:40 +0000103 virtual void InitSections() {
104 // FIXME, this is MachO specific, but the testsuite
105 // expects this.
106 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
107 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
108 0, SectionKind::getText()));
109 }
110
Chris Lattner46a947d2009-08-17 04:17:34 +0000111 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000112
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000113 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000114 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000115
Daniel Dunbar821e3332009-08-31 08:09:28 +0000116 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000117 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000119 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000120
Chris Lattner46a947d2009-08-17 04:17:34 +0000121 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000122 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
123 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
124 virtual void EmitCOFFSymbolType(int Type);
125 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000126 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000127 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000128 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000129
Chris Lattner9eb158d2010-01-23 07:47:02 +0000130 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
131 ///
132 /// @param Symbol - The common symbol to emit.
133 /// @param Size - The size of the common symbol.
134 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000135
Daniel Dunbar8751b942009-08-28 05:48:22 +0000136 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000137 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000138
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000139 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
140 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000141
Chris Lattneraaec2052010-01-19 19:46:13 +0000142 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000143
Chris Lattneraaec2052010-01-19 19:46:13 +0000144 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000145
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000146 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000147
148 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
149
150 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
151
Chris Lattner718fb592010-01-25 21:28:50 +0000152 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000153
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000154
Chris Lattneraaec2052010-01-19 19:46:13 +0000155 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
156 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000157
Chris Lattner46a947d2009-08-17 04:17:34 +0000158 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
159 unsigned ValueSize = 1,
160 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000161
Kevin Enderby6e720482010-02-23 18:26:34 +0000162 virtual void EmitCodeAlignment(unsigned ByteAlignment,
163 unsigned MaxBytesToEmit = 0);
164
Daniel Dunbar821e3332009-08-31 08:09:28 +0000165 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000166 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000167
Chris Lattnera6594fc2010-01-25 18:58:59 +0000168 virtual void EmitFileDirective(StringRef Filename);
169 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
170
171 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000172
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000173 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000174 /// the specified string in the output .s file. This capability is
175 /// indicated by the hasRawTextSupport() predicate.
176 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000177
Chris Lattner46a947d2009-08-17 04:17:34 +0000178 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000179
Chris Lattner46a947d2009-08-17 04:17:34 +0000180 /// @}
181};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000182
Chris Lattner46a947d2009-08-17 04:17:34 +0000183} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000184
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000185/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000186/// file if applicable as a QoI issue to make the output of the compiler
187/// more readable. This only affects the MCAsmStreamer, and only when
188/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000189void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000190 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000191
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000192 // Make sure that CommentStream is flushed.
193 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000194
Chris Lattner86e22112010-01-22 07:29:22 +0000195 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000196 // Each comment goes on its own line.
197 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000198
Chris Lattner14ca1772010-01-22 21:16:10 +0000199 // Tell the comment stream that the vector changed underneath it.
200 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000201}
202
203void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000204 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
205 OS << '\n';
206 return;
207 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000208
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000209 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000210 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000211
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000212 assert(Comments.back() == '\n' &&
213 "Comment array not newline terminated");
214 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000215 // Emit a line of comments.
216 OS.PadToColumn(MAI.getCommentColumn());
217 size_t Position = Comments.find('\n');
218 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000219
Chris Lattner86e22112010-01-22 07:29:22 +0000220 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000221 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000222
Chris Lattner14ca1772010-01-22 21:16:10 +0000223 CommentToEmit.clear();
224 // Tell the comment stream that the vector changed underneath it.
225 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000226}
227
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000228static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
229 assert(Bytes && "Invalid size!");
230 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
231}
232
Chris Lattner975780b2009-08-17 05:49:08 +0000233void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000234 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000235 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000236 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000237 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000238 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000239 }
240}
241
242void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000243 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000244 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000245 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000246
Chris Lattnere07b75e2010-09-22 22:19:53 +0000247 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000248 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000249 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000250}
251
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000252void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000253 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000254 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000255 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000256 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000257 case MCAF_Code16: OS << "\t.code\t16"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000258 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000259 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000260}
261
Jim Grosbachce792992010-11-05 22:08:08 +0000262void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
263 // This needs to emit to a temporary string to get properly quoted
264 // MCSymbols when they have spaces in them.
265 OS << "\t.thumb_func";
266 if (Func)
267 OS << '\t' << *Func;
268 EmitEOL();
269}
270
Daniel Dunbar821e3332009-08-31 08:09:28 +0000271void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000272 OS << *Symbol << " = " << *Value;
273 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000274
275 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000276 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000277}
278
Rafael Espindola484291c2010-11-01 14:28:48 +0000279void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
280 OS << ".weakref " << *Alias << ", " << *Symbol;
281 EmitEOL();
282}
283
Daniel Dunbarfffff912009-10-16 01:34:54 +0000284void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000285 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000286 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000287 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000288 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
289 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
290 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
291 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
292 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
293 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
294 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000295 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000296 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
297 switch (Attribute) {
298 default: assert(0 && "Unknown ELF .type");
299 case MCSA_ELF_TypeFunction: OS << "function"; break;
300 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
301 case MCSA_ELF_TypeObject: OS << "object"; break;
302 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
303 case MCSA_ELF_TypeCommon: OS << "common"; break;
304 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
305 }
306 EmitEOL();
307 return;
308 case MCSA_Global: // .globl/.global
309 OS << MAI.getGlobalDirective();
310 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000311 case MCSA_Hidden: OS << "\t.hidden\t"; break;
312 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
313 case MCSA_Internal: OS << "\t.internal\t"; break;
314 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
315 case MCSA_Local: OS << "\t.local\t"; break;
316 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
317 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
318 case MCSA_Protected: OS << "\t.protected\t"; break;
319 case MCSA_Reference: OS << "\t.reference\t"; break;
320 case MCSA_Weak: OS << "\t.weak\t"; break;
321 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000322 // .weak_reference
323 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000324 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000325 }
326
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000327 OS << *Symbol;
328 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000329}
330
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000331void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000332 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
333 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000334}
335
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000336void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
337 OS << "\t.def\t " << *Symbol << ';';
338 EmitEOL();
339}
340
341void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
342 OS << "\t.scl\t" << StorageClass << ';';
343 EmitEOL();
344}
345
346void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
347 OS << "\t.type\t" << Type << ';';
348 EmitEOL();
349}
350
351void MCAsmStreamer::EndCOFFSymbolDef() {
352 OS << "\t.endef";
353 EmitEOL();
354}
355
Chris Lattner99328ad2010-01-25 07:52:13 +0000356void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
357 assert(MAI.hasDotTypeDotSizeDirective());
358 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
359}
360
Chris Lattner9eb158d2010-01-23 07:47:02 +0000361void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000362 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000363 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000364 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000365 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000366 OS << ',' << ByteAlignment;
367 else
368 OS << ',' << Log2_32(ByteAlignment);
369 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000370 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000371}
372
Chris Lattner9eb158d2010-01-23 07:47:02 +0000373/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
374///
375/// @param Symbol - The common symbol to emit.
376/// @param Size - The size of the common symbol.
377void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
378 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
379 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
380 EmitEOL();
381}
382
Daniel Dunbar8751b942009-08-28 05:48:22 +0000383void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000384 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000385 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000386 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000387
Chris Lattner93b6db32009-08-08 23:39:42 +0000388 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000389 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000390 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000391
Chris Lattner9be3fee2009-07-10 22:20:30 +0000392 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000393 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000394 if (ByteAlignment != 0)
395 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000396 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000397 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000398}
399
Eric Christopherd04d98d2010-05-17 02:13:02 +0000400// .tbss sym, size, align
401// This depends that the symbol has already been mangled from the original,
402// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000403void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
404 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000405 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000406 // Instead of using the Section we'll just use the shortcut.
407 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000408 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000409
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000410 // Output align if we have it. We default to 1 so don't bother printing
411 // that.
412 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000413
Eric Christopher482eba02010-05-14 01:50:28 +0000414 EmitEOL();
415}
416
Chris Lattner12e555c2010-01-23 00:15:00 +0000417static inline char toOctal(int X) { return (X&7)+'0'; }
418
Chris Lattnera6594fc2010-01-25 18:58:59 +0000419static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
420 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000421
Chris Lattnera6594fc2010-01-25 18:58:59 +0000422 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
423 unsigned char C = Data[i];
424 if (C == '"' || C == '\\') {
425 OS << '\\' << (char)C;
426 continue;
427 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000428
Chris Lattnera6594fc2010-01-25 18:58:59 +0000429 if (isprint((unsigned char)C)) {
430 OS << (char)C;
431 continue;
432 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000433
Chris Lattnera6594fc2010-01-25 18:58:59 +0000434 switch (C) {
435 case '\b': OS << "\\b"; break;
436 case '\f': OS << "\\f"; break;
437 case '\n': OS << "\\n"; break;
438 case '\r': OS << "\\r"; break;
439 case '\t': OS << "\\t"; break;
440 default:
441 OS << '\\';
442 OS << toOctal(C >> 6);
443 OS << toOctal(C >> 3);
444 OS << toOctal(C >> 0);
445 break;
446 }
447 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000448
Chris Lattnera6594fc2010-01-25 18:58:59 +0000449 OS << '"';
450}
451
452
Chris Lattneraaec2052010-01-19 19:46:13 +0000453void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000454 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000455 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000456
Chris Lattner12e555c2010-01-23 00:15:00 +0000457 if (Data.size() == 1) {
458 OS << MAI.getData8bitsDirective(AddrSpace);
459 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000460 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000461 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000462 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000463
464 // If the data ends with 0 and the target supports .asciz, use it, otherwise
465 // use .ascii
466 if (MAI.getAscizDirective() && Data.back() == 0) {
467 OS << MAI.getAscizDirective();
468 Data = Data.substr(0, Data.size()-1);
469 } else {
470 OS << MAI.getAsciiDirective();
471 }
472
Chris Lattnera6594fc2010-01-25 18:58:59 +0000473 OS << ' ';
474 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000475 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000476}
477
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000478/// EmitIntValue - Special case of EmitValue that avoids the client having
479/// to pass in a MCExpr for constant integers.
480void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
481 unsigned AddrSpace) {
482 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000483 const char *Directive = 0;
484 switch (Size) {
485 default: break;
486 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
487 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
488 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000489 case 8:
490 Directive = MAI.getData64bitsDirective(AddrSpace);
491 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
492 if (Directive) break;
493 if (isLittleEndian()) {
494 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
495 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
496 } else {
497 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
498 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
499 }
500 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000501 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000502
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000503 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000504 OS << Directive << truncateToSize(Value, Size);
505 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000506}
507
Chris Lattneraaec2052010-01-19 19:46:13 +0000508void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
509 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000510 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000511 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000512 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000513 default: break;
514 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
515 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
516 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
517 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000518 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000519
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000520 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000521 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000522 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000523}
524
Rafael Espindola3ff57092010-11-02 17:22:24 +0000525void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindola73873452010-11-04 18:17:08 +0000526 assert(MAI.hasLEB128() && "Cannot print a .uleb");
527 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000528 EmitEOL();
529}
530
531void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindola73873452010-11-04 18:17:08 +0000532 assert(MAI.hasLEB128() && "Cannot print a .sleb");
533 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000534 EmitEOL();
535}
536
Chris Lattner718fb592010-01-25 21:28:50 +0000537void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
538 assert(MAI.getGPRel32Directive() != 0);
539 OS << MAI.getGPRel32Directive() << *Value;
540 EmitEOL();
541}
542
543
Chris Lattner6113b3d2010-01-19 18:52:28 +0000544/// EmitFill - Emit NumBytes bytes worth of the value specified by
545/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000546void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
547 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000548 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000549
Chris Lattneraaec2052010-01-19 19:46:13 +0000550 if (AddrSpace == 0)
551 if (const char *ZeroDirective = MAI.getZeroDirective()) {
552 OS << ZeroDirective << NumBytes;
553 if (FillValue != 0)
554 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000555 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000556 return;
557 }
558
559 // Emit a byte at a time.
560 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000561}
562
Daniel Dunbar84a29262009-06-24 19:25:34 +0000563void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
564 unsigned ValueSize,
565 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000566 // Some assemblers don't support non-power of two alignments, so we always
567 // emit alignments as a power of two if possible.
568 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000569 switch (ValueSize) {
570 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000571 case 1: OS << MAI.getAlignDirective(); break;
572 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000573 case 2: OS << ".p2alignw "; break;
574 case 4: OS << ".p2alignl "; break;
575 case 8: llvm_unreachable("Unsupported alignment size!");
576 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000577
Chris Lattner33adcfb2009-08-22 21:43:10 +0000578 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000579 OS << ByteAlignment;
580 else
581 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000582
Chris Lattner663c2d22009-08-19 06:12:02 +0000583 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000584 OS << ", 0x";
585 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000586
Jim Grosbach00545e12010-09-22 18:16:55 +0000587 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000588 OS << ", " << MaxBytesToEmit;
589 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000590 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000591 return;
592 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000593
Chris Lattner663c2d22009-08-19 06:12:02 +0000594 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000595 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000596 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000597 default: llvm_unreachable("Invalid size for machine code value!");
598 case 1: OS << ".balign"; break;
599 case 2: OS << ".balignw"; break;
600 case 4: OS << ".balignl"; break;
601 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000602 }
603
Chris Lattner663c2d22009-08-19 06:12:02 +0000604 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000605 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000606 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000607 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000608 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000609}
610
Kevin Enderby6e720482010-02-23 18:26:34 +0000611void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
612 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000613 // Emit with a text fill value.
614 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
615 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000616}
617
Daniel Dunbar821e3332009-08-31 08:09:28 +0000618void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000619 unsigned char Value) {
620 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000621 OS << ".org " << *Offset << ", " << (unsigned) Value;
622 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000623}
624
Chris Lattnera6594fc2010-01-25 18:58:59 +0000625
626void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
627 assert(MAI.hasSingleParameterDotFile());
628 OS << "\t.file\t";
629 PrintQuotedString(Filename, OS);
630 EmitEOL();
631}
632
633void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
634 OS << "\t.file\t" << FileNo << ' ';
635 PrintQuotedString(Filename, OS);
636 EmitEOL();
637}
638
Daniel Dunbar6b716532010-02-09 23:00:14 +0000639void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
640 raw_ostream &OS = GetCommentOS();
641 SmallString<256> Code;
642 SmallVector<MCFixup, 4> Fixups;
643 raw_svector_ostream VecOS(Code);
644 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
645 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000646
Daniel Dunbar6b716532010-02-09 23:00:14 +0000647 // If we are showing fixups, create symbolic markers in the encoded
648 // representation. We do this by making a per-bit map to the fixup item index,
649 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000650 SmallVector<uint8_t, 64> FixupMap;
651 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000652 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
653 FixupMap[i] = 0;
654
655 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
656 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000657 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000658 for (unsigned j = 0; j != Info.TargetSize; ++j) {
659 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
660 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
661 FixupMap[Index] = 1 + i;
662 }
663 }
664
665 OS << "encoding: [";
666 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
667 if (i)
668 OS << ',';
669
670 // See if all bits are the same map entry.
671 uint8_t MapEntry = FixupMap[i * 8 + 0];
672 for (unsigned j = 1; j != 8; ++j) {
673 if (FixupMap[i * 8 + j] == MapEntry)
674 continue;
675
676 MapEntry = uint8_t(~0U);
677 break;
678 }
679
680 if (MapEntry != uint8_t(~0U)) {
681 if (MapEntry == 0) {
682 OS << format("0x%02x", uint8_t(Code[i]));
683 } else {
684 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
685 OS << char('A' + MapEntry - 1);
686 }
687 } else {
688 // Otherwise, write out in binary.
689 OS << "0b";
690 for (unsigned j = 8; j--;) {
691 unsigned Bit = (Code[i] >> j) & 1;
692 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
693 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
694 OS << char('A' + MapEntry - 1);
695 } else
696 OS << Bit;
697 }
698 }
699 }
700 OS << "]\n";
701
702 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
703 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000704 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000705 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000706 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000707 }
708}
709
710void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
711 assert(CurSection && "Cannot emit contents before setting section!");
712
713 // Show the encoding in a comment if we have a code emitter.
714 if (Emitter)
715 AddEncodingComment(Inst);
716
Chris Lattner30d9a642010-02-09 00:54:51 +0000717 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000718 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000719 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000720 GetCommentOS() << "\n";
721 }
722
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000723 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000724 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000725 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000726 else
727 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000728 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000729}
730
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000731/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000732/// the specified string in the output .s file. This capability is
733/// indicated by the hasRawTextSupport() predicate.
734void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000735 if (!String.empty() && String.back() == '\n')
736 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000737 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000738 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000739}
740
Daniel Dunbara11af532009-06-24 01:03:06 +0000741void MCAsmStreamer::Finish() {
Daniel Dunbara11af532009-06-24 01:03:06 +0000742}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000743
Chris Lattner86e22112010-01-22 07:29:22 +0000744MCStreamer *llvm::createAsmStreamer(MCContext &Context,
745 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000746 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000747 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000748 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000749 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000750 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000751}