blob: a9cf4b163dd6dee5ceaf885f12aefce15897f4e3 [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);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000169 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
170 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
171 unsigned Column, unsigned Flags,
172 unsigned Isa, unsigned Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000173
174 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000175
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000176 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000177 /// the specified string in the output .s file. This capability is
178 /// indicated by the hasRawTextSupport() predicate.
179 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000180
Chris Lattner46a947d2009-08-17 04:17:34 +0000181 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000182
Chris Lattner46a947d2009-08-17 04:17:34 +0000183 /// @}
184};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000185
Chris Lattner46a947d2009-08-17 04:17:34 +0000186} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000187
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000188/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000189/// file if applicable as a QoI issue to make the output of the compiler
190/// more readable. This only affects the MCAsmStreamer, and only when
191/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000192void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000193 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000194
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000195 // Make sure that CommentStream is flushed.
196 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000197
Chris Lattner86e22112010-01-22 07:29:22 +0000198 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000199 // Each comment goes on its own line.
200 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000201
Chris Lattner14ca1772010-01-22 21:16:10 +0000202 // Tell the comment stream that the vector changed underneath it.
203 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000204}
205
206void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000207 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
208 OS << '\n';
209 return;
210 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000211
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000212 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000213 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000214
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000215 assert(Comments.back() == '\n' &&
216 "Comment array not newline terminated");
217 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000218 // Emit a line of comments.
219 OS.PadToColumn(MAI.getCommentColumn());
220 size_t Position = Comments.find('\n');
221 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000222
Chris Lattner86e22112010-01-22 07:29:22 +0000223 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000224 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000225
Chris Lattner14ca1772010-01-22 21:16:10 +0000226 CommentToEmit.clear();
227 // Tell the comment stream that the vector changed underneath it.
228 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000229}
230
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000231static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
232 assert(Bytes && "Invalid size!");
233 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
234}
235
Chris Lattner975780b2009-08-17 05:49:08 +0000236void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000237 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000238 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000239 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000240 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000241 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000242 }
243}
244
245void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000246 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000247 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000248 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000249
Chris Lattnere07b75e2010-09-22 22:19:53 +0000250 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000251 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000252 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000253}
254
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000255void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000256 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000257 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000258 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000259 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000260 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000261 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000262 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000263 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000264}
265
Jim Grosbachce792992010-11-05 22:08:08 +0000266void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
267 // This needs to emit to a temporary string to get properly quoted
268 // MCSymbols when they have spaces in them.
269 OS << "\t.thumb_func";
270 if (Func)
271 OS << '\t' << *Func;
272 EmitEOL();
273}
274
Daniel Dunbar821e3332009-08-31 08:09:28 +0000275void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000276 OS << *Symbol << " = " << *Value;
277 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000278
279 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000280 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000281}
282
Rafael Espindola484291c2010-11-01 14:28:48 +0000283void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
284 OS << ".weakref " << *Alias << ", " << *Symbol;
285 EmitEOL();
286}
287
Daniel Dunbarfffff912009-10-16 01:34:54 +0000288void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000289 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000290 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000291 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000292 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
293 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
294 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
295 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
296 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
297 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000298 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000299 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000300 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000301 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
302 switch (Attribute) {
303 default: assert(0 && "Unknown ELF .type");
304 case MCSA_ELF_TypeFunction: OS << "function"; break;
305 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
306 case MCSA_ELF_TypeObject: OS << "object"; break;
307 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
308 case MCSA_ELF_TypeCommon: OS << "common"; break;
309 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000310 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000311 }
312 EmitEOL();
313 return;
314 case MCSA_Global: // .globl/.global
315 OS << MAI.getGlobalDirective();
316 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000317 case MCSA_Hidden: OS << "\t.hidden\t"; break;
318 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
319 case MCSA_Internal: OS << "\t.internal\t"; break;
320 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
321 case MCSA_Local: OS << "\t.local\t"; break;
322 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
323 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
324 case MCSA_Protected: OS << "\t.protected\t"; break;
325 case MCSA_Reference: OS << "\t.reference\t"; break;
326 case MCSA_Weak: OS << "\t.weak\t"; break;
327 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000328 // .weak_reference
329 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000330 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000331 }
332
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000333 OS << *Symbol;
334 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000335}
336
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000337void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000338 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
339 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000340}
341
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000342void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
343 OS << "\t.def\t " << *Symbol << ';';
344 EmitEOL();
345}
346
347void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
348 OS << "\t.scl\t" << StorageClass << ';';
349 EmitEOL();
350}
351
352void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
353 OS << "\t.type\t" << Type << ';';
354 EmitEOL();
355}
356
357void MCAsmStreamer::EndCOFFSymbolDef() {
358 OS << "\t.endef";
359 EmitEOL();
360}
361
Chris Lattner99328ad2010-01-25 07:52:13 +0000362void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
363 assert(MAI.hasDotTypeDotSizeDirective());
364 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
365}
366
Chris Lattner9eb158d2010-01-23 07:47:02 +0000367void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000368 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000369 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000370 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000371 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000372 OS << ',' << ByteAlignment;
373 else
374 OS << ',' << Log2_32(ByteAlignment);
375 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000376 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000377}
378
Chris Lattner9eb158d2010-01-23 07:47:02 +0000379/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
380///
381/// @param Symbol - The common symbol to emit.
382/// @param Size - The size of the common symbol.
383void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
384 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
385 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
386 EmitEOL();
387}
388
Daniel Dunbar8751b942009-08-28 05:48:22 +0000389void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000390 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000391 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000392 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000393
Chris Lattner93b6db32009-08-08 23:39:42 +0000394 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000395 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000396 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000397
Chris Lattner9be3fee2009-07-10 22:20:30 +0000398 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000399 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000400 if (ByteAlignment != 0)
401 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000402 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000403 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000404}
405
Eric Christopherd04d98d2010-05-17 02:13:02 +0000406// .tbss sym, size, align
407// This depends that the symbol has already been mangled from the original,
408// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000409void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
410 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000411 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000412 // Instead of using the Section we'll just use the shortcut.
413 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000414 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000415
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000416 // Output align if we have it. We default to 1 so don't bother printing
417 // that.
418 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000419
Eric Christopher482eba02010-05-14 01:50:28 +0000420 EmitEOL();
421}
422
Chris Lattner12e555c2010-01-23 00:15:00 +0000423static inline char toOctal(int X) { return (X&7)+'0'; }
424
Chris Lattnera6594fc2010-01-25 18:58:59 +0000425static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
426 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000427
Chris Lattnera6594fc2010-01-25 18:58:59 +0000428 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
429 unsigned char C = Data[i];
430 if (C == '"' || C == '\\') {
431 OS << '\\' << (char)C;
432 continue;
433 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000434
Chris Lattnera6594fc2010-01-25 18:58:59 +0000435 if (isprint((unsigned char)C)) {
436 OS << (char)C;
437 continue;
438 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000439
Chris Lattnera6594fc2010-01-25 18:58:59 +0000440 switch (C) {
441 case '\b': OS << "\\b"; break;
442 case '\f': OS << "\\f"; break;
443 case '\n': OS << "\\n"; break;
444 case '\r': OS << "\\r"; break;
445 case '\t': OS << "\\t"; break;
446 default:
447 OS << '\\';
448 OS << toOctal(C >> 6);
449 OS << toOctal(C >> 3);
450 OS << toOctal(C >> 0);
451 break;
452 }
453 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000454
Chris Lattnera6594fc2010-01-25 18:58:59 +0000455 OS << '"';
456}
457
458
Chris Lattneraaec2052010-01-19 19:46:13 +0000459void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000460 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000461 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000462
Chris Lattner12e555c2010-01-23 00:15:00 +0000463 if (Data.size() == 1) {
464 OS << MAI.getData8bitsDirective(AddrSpace);
465 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000466 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000467 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000468 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000469
470 // If the data ends with 0 and the target supports .asciz, use it, otherwise
471 // use .ascii
472 if (MAI.getAscizDirective() && Data.back() == 0) {
473 OS << MAI.getAscizDirective();
474 Data = Data.substr(0, Data.size()-1);
475 } else {
476 OS << MAI.getAsciiDirective();
477 }
478
Chris Lattnera6594fc2010-01-25 18:58:59 +0000479 OS << ' ';
480 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000481 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000482}
483
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000484/// EmitIntValue - Special case of EmitValue that avoids the client having
485/// to pass in a MCExpr for constant integers.
486void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
487 unsigned AddrSpace) {
488 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000489 const char *Directive = 0;
490 switch (Size) {
491 default: break;
492 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
493 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
494 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000495 case 8:
496 Directive = MAI.getData64bitsDirective(AddrSpace);
497 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
498 if (Directive) break;
499 if (isLittleEndian()) {
500 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
501 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
502 } else {
503 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
504 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
505 }
506 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000507 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000508
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000509 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000510 OS << Directive << truncateToSize(Value, Size);
511 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000512}
513
Chris Lattneraaec2052010-01-19 19:46:13 +0000514void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
515 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000516 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000517 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000518 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000519 default: break;
520 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
521 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
522 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
523 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000524 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000525
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000526 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000527 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000528 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000529}
530
Rafael Espindola3ff57092010-11-02 17:22:24 +0000531void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindola73873452010-11-04 18:17:08 +0000532 assert(MAI.hasLEB128() && "Cannot print a .uleb");
533 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000534 EmitEOL();
535}
536
537void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindola73873452010-11-04 18:17:08 +0000538 assert(MAI.hasLEB128() && "Cannot print a .sleb");
539 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000540 EmitEOL();
541}
542
Chris Lattner718fb592010-01-25 21:28:50 +0000543void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
544 assert(MAI.getGPRel32Directive() != 0);
545 OS << MAI.getGPRel32Directive() << *Value;
546 EmitEOL();
547}
548
549
Chris Lattner6113b3d2010-01-19 18:52:28 +0000550/// EmitFill - Emit NumBytes bytes worth of the value specified by
551/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000552void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
553 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000554 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000555
Chris Lattneraaec2052010-01-19 19:46:13 +0000556 if (AddrSpace == 0)
557 if (const char *ZeroDirective = MAI.getZeroDirective()) {
558 OS << ZeroDirective << NumBytes;
559 if (FillValue != 0)
560 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000561 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000562 return;
563 }
564
565 // Emit a byte at a time.
566 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000567}
568
Daniel Dunbar84a29262009-06-24 19:25:34 +0000569void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
570 unsigned ValueSize,
571 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000572 // Some assemblers don't support non-power of two alignments, so we always
573 // emit alignments as a power of two if possible.
574 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000575 switch (ValueSize) {
576 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000577 case 1: OS << MAI.getAlignDirective(); break;
578 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000579 case 2: OS << ".p2alignw "; break;
580 case 4: OS << ".p2alignl "; break;
581 case 8: llvm_unreachable("Unsupported alignment size!");
582 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000583
Chris Lattner33adcfb2009-08-22 21:43:10 +0000584 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000585 OS << ByteAlignment;
586 else
587 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000588
Chris Lattner663c2d22009-08-19 06:12:02 +0000589 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000590 OS << ", 0x";
591 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000592
Jim Grosbach00545e12010-09-22 18:16:55 +0000593 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000594 OS << ", " << MaxBytesToEmit;
595 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000596 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000597 return;
598 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000599
Chris Lattner663c2d22009-08-19 06:12:02 +0000600 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000601 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000602 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000603 default: llvm_unreachable("Invalid size for machine code value!");
604 case 1: OS << ".balign"; break;
605 case 2: OS << ".balignw"; break;
606 case 4: OS << ".balignl"; break;
607 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000608 }
609
Chris Lattner663c2d22009-08-19 06:12:02 +0000610 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000611 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000612 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000613 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000614 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000615}
616
Kevin Enderby6e720482010-02-23 18:26:34 +0000617void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
618 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000619 // Emit with a text fill value.
620 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
621 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000622}
623
Daniel Dunbar821e3332009-08-31 08:09:28 +0000624void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000625 unsigned char Value) {
626 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000627 OS << ".org " << *Offset << ", " << (unsigned) Value;
628 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000629}
630
Chris Lattnera6594fc2010-01-25 18:58:59 +0000631
632void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
633 assert(MAI.hasSingleParameterDotFile());
634 OS << "\t.file\t";
635 PrintQuotedString(Filename, OS);
636 EmitEOL();
637}
638
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000639bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Chris Lattnera6594fc2010-01-25 18:58:59 +0000640 OS << "\t.file\t" << FileNo << ' ';
641 PrintQuotedString(Filename, OS);
642 EmitEOL();
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000643 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
644}
645
646void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
647 unsigned Column, unsigned Flags,
648 unsigned Isa,
649 unsigned Discriminator) {
650 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
651 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
652 OS << " basic_block";
653 if (Flags & DWARF2_FLAG_PROLOGUE_END)
654 OS << " prologue_end";
655 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
656 OS << " epilogue_begin";
657
658 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
659 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
660 OS << " is_stmt ";
661
662 if (Flags & DWARF2_FLAG_IS_STMT)
663 OS << "1";
664 else
665 OS << "0";
666 }
667
668 if (Isa)
669 OS << "isa " << Isa;
670 if (Discriminator)
671 OS << "discriminator " << Discriminator;
672 EmitEOL();
673 return this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
674 Isa, Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000675}
676
Daniel Dunbar6b716532010-02-09 23:00:14 +0000677void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
678 raw_ostream &OS = GetCommentOS();
679 SmallString<256> Code;
680 SmallVector<MCFixup, 4> Fixups;
681 raw_svector_ostream VecOS(Code);
682 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
683 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000684
Daniel Dunbar6b716532010-02-09 23:00:14 +0000685 // If we are showing fixups, create symbolic markers in the encoded
686 // representation. We do this by making a per-bit map to the fixup item index,
687 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000688 SmallVector<uint8_t, 64> FixupMap;
689 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000690 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
691 FixupMap[i] = 0;
692
693 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
694 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000695 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000696 for (unsigned j = 0; j != Info.TargetSize; ++j) {
697 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
698 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
699 FixupMap[Index] = 1 + i;
700 }
701 }
702
703 OS << "encoding: [";
704 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
705 if (i)
706 OS << ',';
707
708 // See if all bits are the same map entry.
709 uint8_t MapEntry = FixupMap[i * 8 + 0];
710 for (unsigned j = 1; j != 8; ++j) {
711 if (FixupMap[i * 8 + j] == MapEntry)
712 continue;
713
714 MapEntry = uint8_t(~0U);
715 break;
716 }
717
718 if (MapEntry != uint8_t(~0U)) {
719 if (MapEntry == 0) {
720 OS << format("0x%02x", uint8_t(Code[i]));
721 } else {
722 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
723 OS << char('A' + MapEntry - 1);
724 }
725 } else {
726 // Otherwise, write out in binary.
727 OS << "0b";
728 for (unsigned j = 8; j--;) {
729 unsigned Bit = (Code[i] >> j) & 1;
Chris Lattner3170a3b2010-11-15 05:56:19 +0000730
731 unsigned FixupBit;
732 if (IsLittleEndian)
733 FixupBit = i * 8 + j;
734 else
735 FixupBit = i * 8 + (7-j);
736
737 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +0000738 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
739 OS << char('A' + MapEntry - 1);
740 } else
741 OS << Bit;
742 }
743 }
744 }
745 OS << "]\n";
746
747 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
748 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000749 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000750 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000751 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000752 }
753}
754
755void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
756 assert(CurSection && "Cannot emit contents before setting section!");
757
758 // Show the encoding in a comment if we have a code emitter.
759 if (Emitter)
760 AddEncodingComment(Inst);
761
Chris Lattner30d9a642010-02-09 00:54:51 +0000762 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000763 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000764 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000765 GetCommentOS() << "\n";
766 }
767
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000768 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000769 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000770 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000771 else
772 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000773 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000774}
775
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000776/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000777/// the specified string in the output .s file. This capability is
778/// indicated by the hasRawTextSupport() predicate.
779void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000780 if (!String.empty() && String.back() == '\n')
781 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000782 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000783 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000784}
785
Daniel Dunbara11af532009-06-24 01:03:06 +0000786void MCAsmStreamer::Finish() {
Daniel Dunbara11af532009-06-24 01:03:06 +0000787}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000788
Chris Lattner86e22112010-01-22 07:29:22 +0000789MCStreamer *llvm::createAsmStreamer(MCContext &Context,
790 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000791 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000792 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000793 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000794 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000795 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000796}