blob: 530c89b0759c554e7b0804b068173fc813f8fb6d [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;
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000034 MCCodeEmitter *Emitter;
Chris Lattner86e22112010-01-22 07:29:22 +000035
36 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; }
Chris Lattner91bead72010-04-03 21:35:55 +000071
72 /// 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
Chris Lattner46a947d2009-08-17 04:17:34 +0000103 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000104
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000105 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +0000106
Daniel Dunbar821e3332009-08-31 08:09:28 +0000107 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000108
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000109 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000110
Chris Lattner46a947d2009-08-17 04:17:34 +0000111 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000112 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
113 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
114 virtual void EmitCOFFSymbolType(int Type);
115 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000116 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000117 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000118 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000119
Chris Lattner9eb158d2010-01-23 07:47:02 +0000120 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
121 ///
122 /// @param Symbol - The common symbol to emit.
123 /// @param Size - The size of the common symbol.
124 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
125
Daniel Dunbar8751b942009-08-28 05:48:22 +0000126 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000127 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000128
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000129 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
130 uint64_t Size, unsigned ByteAlignment = 0);
Eric Christopher482eba02010-05-14 01:50:28 +0000131
Chris Lattneraaec2052010-01-19 19:46:13 +0000132 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000133
Chris Lattneraaec2052010-01-19 19:46:13 +0000134 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000135 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000136 virtual void EmitGPRel32Value(const MCExpr *Value);
137
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000138
Chris Lattneraaec2052010-01-19 19:46:13 +0000139 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
140 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000141
Chris Lattner46a947d2009-08-17 04:17:34 +0000142 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
143 unsigned ValueSize = 1,
144 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000145
Kevin Enderby6e720482010-02-23 18:26:34 +0000146 virtual void EmitCodeAlignment(unsigned ByteAlignment,
147 unsigned MaxBytesToEmit = 0);
148
Daniel Dunbar821e3332009-08-31 08:09:28 +0000149 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000150 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000151
Chris Lattnera6594fc2010-01-25 18:58:59 +0000152 virtual void EmitFileDirective(StringRef Filename);
153 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
154
155 virtual void EmitInstruction(const MCInst &Inst);
156
Chris Lattner91bead72010-04-03 21:35:55 +0000157 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
158 /// the specified string in the output .s file. This capability is
159 /// indicated by the hasRawTextSupport() predicate.
160 virtual void EmitRawText(StringRef String);
161
Chris Lattner46a947d2009-08-17 04:17:34 +0000162 virtual void Finish();
163
164 /// @}
165};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000166
Chris Lattner46a947d2009-08-17 04:17:34 +0000167} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000168
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000169/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000170/// file if applicable as a QoI issue to make the output of the compiler
171/// more readable. This only affects the MCAsmStreamer, and only when
172/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000173void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000174 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000175
176 // Make sure that CommentStream is flushed.
177 CommentStream.flush();
178
Chris Lattner86e22112010-01-22 07:29:22 +0000179 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000180 // Each comment goes on its own line.
181 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000182
183 // Tell the comment stream that the vector changed underneath it.
184 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000185}
186
187void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000188 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
189 OS << '\n';
190 return;
191 }
192
193 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000194 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000195
196 assert(Comments.back() == '\n' &&
197 "Comment array not newline terminated");
198 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000199 // Emit a line of comments.
200 OS.PadToColumn(MAI.getCommentColumn());
201 size_t Position = Comments.find('\n');
202 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
203
Chris Lattner86e22112010-01-22 07:29:22 +0000204 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000205 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000206
Chris Lattner14ca1772010-01-22 21:16:10 +0000207 CommentToEmit.clear();
208 // Tell the comment stream that the vector changed underneath it.
209 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000210}
211
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000212static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
213 assert(Bytes && "Invalid size!");
214 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
215}
216
Chris Lattner975780b2009-08-17 05:49:08 +0000217void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000218 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000219 if (Section != CurSection) {
220 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000221 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000222 }
223}
224
225void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000226 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000227 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000228 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000229
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000230 OS << *Symbol << ":";
231 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000232 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000233}
234
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000235void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000236 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000237 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000238 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000239 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000240 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000241}
242
Daniel Dunbar821e3332009-08-31 08:09:28 +0000243void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000244 OS << *Symbol << " = " << *Value;
245 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000246
247 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000248 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000249}
250
Daniel Dunbarfffff912009-10-16 01:34:54 +0000251void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000252 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000253 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000254 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000255 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
256 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
257 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
258 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
259 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
260 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
261 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000262 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000263 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
264 switch (Attribute) {
265 default: assert(0 && "Unknown ELF .type");
266 case MCSA_ELF_TypeFunction: OS << "function"; break;
267 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
268 case MCSA_ELF_TypeObject: OS << "object"; break;
269 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
270 case MCSA_ELF_TypeCommon: OS << "common"; break;
271 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
272 }
273 EmitEOL();
274 return;
275 case MCSA_Global: // .globl/.global
276 OS << MAI.getGlobalDirective();
277 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000278 case MCSA_Hidden: OS << "\t.hidden\t"; break;
279 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
280 case MCSA_Internal: OS << "\t.internal\t"; break;
281 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
282 case MCSA_Local: OS << "\t.local\t"; break;
283 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
284 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
285 case MCSA_Protected: OS << "\t.protected\t"; break;
286 case MCSA_Reference: OS << "\t.reference\t"; break;
287 case MCSA_Weak: OS << "\t.weak\t"; break;
288 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000289 // .weak_reference
290 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000291 }
292
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000293 OS << *Symbol;
294 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000295}
296
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000297void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000298 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
299 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000300}
301
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000302void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
303 OS << "\t.def\t " << *Symbol << ';';
304 EmitEOL();
305}
306
307void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
308 OS << "\t.scl\t" << StorageClass << ';';
309 EmitEOL();
310}
311
312void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
313 OS << "\t.type\t" << Type << ';';
314 EmitEOL();
315}
316
317void MCAsmStreamer::EndCOFFSymbolDef() {
318 OS << "\t.endef";
319 EmitEOL();
320}
321
Chris Lattner99328ad2010-01-25 07:52:13 +0000322void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
323 assert(MAI.hasDotTypeDotSizeDirective());
324 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
325}
326
Chris Lattner9eb158d2010-01-23 07:47:02 +0000327void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000328 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000329 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000330 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000331 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000332 OS << ',' << ByteAlignment;
333 else
334 OS << ',' << Log2_32(ByteAlignment);
335 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000336 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000337}
338
Chris Lattner9eb158d2010-01-23 07:47:02 +0000339/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
340///
341/// @param Symbol - The common symbol to emit.
342/// @param Size - The size of the common symbol.
343void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
344 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
345 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
346 EmitEOL();
347}
348
Daniel Dunbar8751b942009-08-28 05:48:22 +0000349void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000350 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000351 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000352 OS << ".zerofill ";
353
354 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000355 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000356 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000357
Chris Lattner9be3fee2009-07-10 22:20:30 +0000358 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000359 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000360 if (ByteAlignment != 0)
361 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000362 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000363 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000364}
365
Eric Christopherd04d98d2010-05-17 02:13:02 +0000366// .tbss sym, size, align
367// This depends that the symbol has already been mangled from the original,
368// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000369void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
370 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000371 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000372 // Instead of using the Section we'll just use the shortcut.
373 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000374 OS << ".tbss " << *Symbol << ", " << Size;
Eric Christopher482eba02010-05-14 01:50:28 +0000375
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000376 // Output align if we have it. We default to 1 so don't bother printing
377 // that.
378 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Eric Christopher482eba02010-05-14 01:50:28 +0000379
380 EmitEOL();
381}
382
Chris Lattner12e555c2010-01-23 00:15:00 +0000383static inline char toOctal(int X) { return (X&7)+'0'; }
384
Chris Lattnera6594fc2010-01-25 18:58:59 +0000385static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
386 OS << '"';
387
388 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
389 unsigned char C = Data[i];
390 if (C == '"' || C == '\\') {
391 OS << '\\' << (char)C;
392 continue;
393 }
394
395 if (isprint((unsigned char)C)) {
396 OS << (char)C;
397 continue;
398 }
399
400 switch (C) {
401 case '\b': OS << "\\b"; break;
402 case '\f': OS << "\\f"; break;
403 case '\n': OS << "\\n"; break;
404 case '\r': OS << "\\r"; break;
405 case '\t': OS << "\\t"; break;
406 default:
407 OS << '\\';
408 OS << toOctal(C >> 6);
409 OS << toOctal(C >> 3);
410 OS << toOctal(C >> 0);
411 break;
412 }
413 }
414
415 OS << '"';
416}
417
418
Chris Lattneraaec2052010-01-19 19:46:13 +0000419void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000420 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000421 if (Data.empty()) return;
422
423 if (Data.size() == 1) {
424 OS << MAI.getData8bitsDirective(AddrSpace);
425 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000426 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000427 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000428 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000429
430 // If the data ends with 0 and the target supports .asciz, use it, otherwise
431 // use .ascii
432 if (MAI.getAscizDirective() && Data.back() == 0) {
433 OS << MAI.getAscizDirective();
434 Data = Data.substr(0, Data.size()-1);
435 } else {
436 OS << MAI.getAsciiDirective();
437 }
438
Chris Lattnera6594fc2010-01-25 18:58:59 +0000439 OS << ' ';
440 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000441 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000442}
443
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000444/// EmitIntValue - Special case of EmitValue that avoids the client having
445/// to pass in a MCExpr for constant integers.
446void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
447 unsigned AddrSpace) {
448 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000449 const char *Directive = 0;
450 switch (Size) {
451 default: break;
452 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
453 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
454 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000455 case 8:
456 Directive = MAI.getData64bitsDirective(AddrSpace);
457 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
458 if (Directive) break;
459 if (isLittleEndian()) {
460 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
461 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
462 } else {
463 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
464 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
465 }
466 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000467 }
468
469 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000470 OS << Directive << truncateToSize(Value, Size);
471 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000472}
473
Chris Lattneraaec2052010-01-19 19:46:13 +0000474void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
475 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000476 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000477 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000478 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000479 default: break;
480 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
481 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
482 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
483 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000484 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000485
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000486 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000487 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000488 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000489}
490
Chris Lattner718fb592010-01-25 21:28:50 +0000491void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
492 assert(MAI.getGPRel32Directive() != 0);
493 OS << MAI.getGPRel32Directive() << *Value;
494 EmitEOL();
495}
496
497
Chris Lattner6113b3d2010-01-19 18:52:28 +0000498/// EmitFill - Emit NumBytes bytes worth of the value specified by
499/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000500void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
501 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000502 if (NumBytes == 0) return;
503
Chris Lattneraaec2052010-01-19 19:46:13 +0000504 if (AddrSpace == 0)
505 if (const char *ZeroDirective = MAI.getZeroDirective()) {
506 OS << ZeroDirective << NumBytes;
507 if (FillValue != 0)
508 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000509 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000510 return;
511 }
512
513 // Emit a byte at a time.
514 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000515}
516
Daniel Dunbar84a29262009-06-24 19:25:34 +0000517void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
518 unsigned ValueSize,
519 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000520 // Some assemblers don't support non-power of two alignments, so we always
521 // emit alignments as a power of two if possible.
522 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000523 switch (ValueSize) {
524 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000525 case 1: OS << MAI.getAlignDirective(); break;
526 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000527 case 2: OS << ".p2alignw "; break;
528 case 4: OS << ".p2alignl "; break;
529 case 8: llvm_unreachable("Unsupported alignment size!");
530 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000531
Chris Lattner33adcfb2009-08-22 21:43:10 +0000532 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000533 OS << ByteAlignment;
534 else
535 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000536
Chris Lattner663c2d22009-08-19 06:12:02 +0000537 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000538 OS << ", 0x";
539 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000540
541 if (MaxBytesToEmit)
542 OS << ", " << MaxBytesToEmit;
543 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000544 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000545 return;
546 }
547
548 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000549 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000550 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000551 default: llvm_unreachable("Invalid size for machine code value!");
552 case 1: OS << ".balign"; break;
553 case 2: OS << ".balignw"; break;
554 case 4: OS << ".balignl"; break;
555 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000556 }
557
Chris Lattner663c2d22009-08-19 06:12:02 +0000558 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000559 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000560 if (MaxBytesToEmit)
561 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000562 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000563}
564
Kevin Enderby6e720482010-02-23 18:26:34 +0000565void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
566 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000567 // Emit with a text fill value.
568 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
569 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000570}
571
Daniel Dunbar821e3332009-08-31 08:09:28 +0000572void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000573 unsigned char Value) {
574 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000575 OS << ".org " << *Offset << ", " << (unsigned) Value;
576 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000577}
578
Chris Lattnera6594fc2010-01-25 18:58:59 +0000579
580void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
581 assert(MAI.hasSingleParameterDotFile());
582 OS << "\t.file\t";
583 PrintQuotedString(Filename, OS);
584 EmitEOL();
585}
586
587void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
588 OS << "\t.file\t" << FileNo << ' ';
589 PrintQuotedString(Filename, OS);
590 EmitEOL();
591}
592
Daniel Dunbar6b716532010-02-09 23:00:14 +0000593void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
594 raw_ostream &OS = GetCommentOS();
595 SmallString<256> Code;
596 SmallVector<MCFixup, 4> Fixups;
597 raw_svector_ostream VecOS(Code);
598 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
599 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000600
Daniel Dunbar6b716532010-02-09 23:00:14 +0000601 // If we are showing fixups, create symbolic markers in the encoded
602 // representation. We do this by making a per-bit map to the fixup item index,
603 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000604 SmallVector<uint8_t, 64> FixupMap;
605 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000606 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
607 FixupMap[i] = 0;
608
609 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
610 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000611 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000612 for (unsigned j = 0; j != Info.TargetSize; ++j) {
613 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
614 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
615 FixupMap[Index] = 1 + i;
616 }
617 }
618
619 OS << "encoding: [";
620 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
621 if (i)
622 OS << ',';
623
624 // See if all bits are the same map entry.
625 uint8_t MapEntry = FixupMap[i * 8 + 0];
626 for (unsigned j = 1; j != 8; ++j) {
627 if (FixupMap[i * 8 + j] == MapEntry)
628 continue;
629
630 MapEntry = uint8_t(~0U);
631 break;
632 }
633
634 if (MapEntry != uint8_t(~0U)) {
635 if (MapEntry == 0) {
636 OS << format("0x%02x", uint8_t(Code[i]));
637 } else {
638 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
639 OS << char('A' + MapEntry - 1);
640 }
641 } else {
642 // Otherwise, write out in binary.
643 OS << "0b";
644 for (unsigned j = 8; j--;) {
645 unsigned Bit = (Code[i] >> j) & 1;
646 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
647 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
648 OS << char('A' + MapEntry - 1);
649 } else
650 OS << Bit;
651 }
652 }
653 }
654 OS << "]\n";
655
656 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
657 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000658 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000659 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000660 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000661 }
662}
663
664void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
665 assert(CurSection && "Cannot emit contents before setting section!");
666
667 // Show the encoding in a comment if we have a code emitter.
668 if (Emitter)
669 AddEncodingComment(Inst);
670
Chris Lattner30d9a642010-02-09 00:54:51 +0000671 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000672 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000673 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000674 GetCommentOS() << "\n";
675 }
676
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000677 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000678 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000679 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000680 else
681 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000682 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000683}
684
Chris Lattner91bead72010-04-03 21:35:55 +0000685/// EmitRawText - If this file is backed by a assembly streamer, this dumps
686/// the specified string in the output .s file. This capability is
687/// indicated by the hasRawTextSupport() predicate.
688void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000689 if (!String.empty() && String.back() == '\n')
690 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000691 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000692 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000693}
694
Daniel Dunbara11af532009-06-24 01:03:06 +0000695void MCAsmStreamer::Finish() {
Daniel Dunbara11af532009-06-24 01:03:06 +0000696}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000697
Chris Lattner86e22112010-01-22 07:29:22 +0000698MCStreamer *llvm::createAsmStreamer(MCContext &Context,
699 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000700 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000701 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000702 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000703 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000704 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000705}