blob: 6acc90af8a5101ad2c8df255efd0b0ff03336ecd [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
Chris Lattneraaec2052010-01-19 19:46:13 +0000129 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000130
Chris Lattneraaec2052010-01-19 19:46:13 +0000131 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000132 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000133 virtual void EmitGPRel32Value(const MCExpr *Value);
134
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000135
Chris Lattneraaec2052010-01-19 19:46:13 +0000136 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
137 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000138
Chris Lattner46a947d2009-08-17 04:17:34 +0000139 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
140 unsigned ValueSize = 1,
141 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000142
Kevin Enderby6e720482010-02-23 18:26:34 +0000143 virtual void EmitCodeAlignment(unsigned ByteAlignment,
144 unsigned MaxBytesToEmit = 0);
145
Daniel Dunbar821e3332009-08-31 08:09:28 +0000146 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000147 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000148
Chris Lattnera6594fc2010-01-25 18:58:59 +0000149 virtual void EmitFileDirective(StringRef Filename);
150 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
151
152 virtual void EmitInstruction(const MCInst &Inst);
153
Chris Lattner91bead72010-04-03 21:35:55 +0000154 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
155 /// the specified string in the output .s file. This capability is
156 /// indicated by the hasRawTextSupport() predicate.
157 virtual void EmitRawText(StringRef String);
158
Chris Lattner46a947d2009-08-17 04:17:34 +0000159 virtual void Finish();
160
161 /// @}
162};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000163
Chris Lattner46a947d2009-08-17 04:17:34 +0000164} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000165
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000166/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000167/// file if applicable as a QoI issue to make the output of the compiler
168/// more readable. This only affects the MCAsmStreamer, and only when
169/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000170void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000171 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000172
173 // Make sure that CommentStream is flushed.
174 CommentStream.flush();
175
Chris Lattner86e22112010-01-22 07:29:22 +0000176 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000177 // Each comment goes on its own line.
178 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000179
180 // Tell the comment stream that the vector changed underneath it.
181 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000182}
183
184void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000185 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
186 OS << '\n';
187 return;
188 }
189
190 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000191 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000192
193 assert(Comments.back() == '\n' &&
194 "Comment array not newline terminated");
195 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000196 // Emit a line of comments.
197 OS.PadToColumn(MAI.getCommentColumn());
198 size_t Position = Comments.find('\n');
199 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
200
Chris Lattner86e22112010-01-22 07:29:22 +0000201 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000202 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000203
Chris Lattner14ca1772010-01-22 21:16:10 +0000204 CommentToEmit.clear();
205 // Tell the comment stream that the vector changed underneath it.
206 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000207}
208
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000209static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
210 assert(Bytes && "Invalid size!");
211 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
212}
213
Chris Lattner975780b2009-08-17 05:49:08 +0000214void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000215 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000216 if (Section != CurSection) {
217 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000218 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000219 }
220}
221
222void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000223 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000224 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000225 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000226
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000227 OS << *Symbol << ":";
228 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000229 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000230}
231
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000232void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000233 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000234 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000235 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000236 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000237 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000238}
239
Daniel Dunbar821e3332009-08-31 08:09:28 +0000240void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000241 OS << *Symbol << " = " << *Value;
242 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000243
244 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000245 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000246}
247
Daniel Dunbarfffff912009-10-16 01:34:54 +0000248void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000249 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000250 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000251 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000252 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
253 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
254 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
255 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
256 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
257 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
258 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000259 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000260 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
261 switch (Attribute) {
262 default: assert(0 && "Unknown ELF .type");
263 case MCSA_ELF_TypeFunction: OS << "function"; break;
264 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
265 case MCSA_ELF_TypeObject: OS << "object"; break;
266 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
267 case MCSA_ELF_TypeCommon: OS << "common"; break;
268 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
269 }
270 EmitEOL();
271 return;
272 case MCSA_Global: // .globl/.global
273 OS << MAI.getGlobalDirective();
274 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000275 case MCSA_Hidden: OS << ".hidden "; break;
276 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
277 case MCSA_Internal: OS << ".internal "; break;
278 case MCSA_LazyReference: OS << ".lazy_reference "; break;
279 case MCSA_Local: OS << ".local "; break;
280 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
281 case MCSA_PrivateExtern: OS << ".private_extern "; break;
282 case MCSA_Protected: OS << ".protected "; break;
283 case MCSA_Reference: OS << ".reference "; break;
284 case MCSA_Weak: OS << ".weak "; break;
285 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
286 // .weak_reference
287 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000288 }
289
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000290 OS << *Symbol;
291 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000292}
293
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000294void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000295 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
296 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000297}
298
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000299void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
300 OS << "\t.def\t " << *Symbol << ';';
301 EmitEOL();
302}
303
304void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
305 OS << "\t.scl\t" << StorageClass << ';';
306 EmitEOL();
307}
308
309void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
310 OS << "\t.type\t" << Type << ';';
311 EmitEOL();
312}
313
314void MCAsmStreamer::EndCOFFSymbolDef() {
315 OS << "\t.endef";
316 EmitEOL();
317}
318
Chris Lattner99328ad2010-01-25 07:52:13 +0000319void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
320 assert(MAI.hasDotTypeDotSizeDirective());
321 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
322}
323
Chris Lattner9eb158d2010-01-23 07:47:02 +0000324void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000325 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000326 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000327 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000328 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000329 OS << ',' << ByteAlignment;
330 else
331 OS << ',' << Log2_32(ByteAlignment);
332 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000333 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000334}
335
Chris Lattner9eb158d2010-01-23 07:47:02 +0000336/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
337///
338/// @param Symbol - The common symbol to emit.
339/// @param Size - The size of the common symbol.
340void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
341 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
342 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
343 EmitEOL();
344}
345
Daniel Dunbar8751b942009-08-28 05:48:22 +0000346void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000347 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000348 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000349 OS << ".zerofill ";
350
351 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000352 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000353 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000354
Chris Lattner9be3fee2009-07-10 22:20:30 +0000355 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000356 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000357 if (ByteAlignment != 0)
358 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000359 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000360 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000361}
362
Chris Lattner12e555c2010-01-23 00:15:00 +0000363static inline char toOctal(int X) { return (X&7)+'0'; }
364
Chris Lattnera6594fc2010-01-25 18:58:59 +0000365static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
366 OS << '"';
367
368 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
369 unsigned char C = Data[i];
370 if (C == '"' || C == '\\') {
371 OS << '\\' << (char)C;
372 continue;
373 }
374
375 if (isprint((unsigned char)C)) {
376 OS << (char)C;
377 continue;
378 }
379
380 switch (C) {
381 case '\b': OS << "\\b"; break;
382 case '\f': OS << "\\f"; break;
383 case '\n': OS << "\\n"; break;
384 case '\r': OS << "\\r"; break;
385 case '\t': OS << "\\t"; break;
386 default:
387 OS << '\\';
388 OS << toOctal(C >> 6);
389 OS << toOctal(C >> 3);
390 OS << toOctal(C >> 0);
391 break;
392 }
393 }
394
395 OS << '"';
396}
397
398
Chris Lattneraaec2052010-01-19 19:46:13 +0000399void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000400 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000401 if (Data.empty()) return;
402
403 if (Data.size() == 1) {
404 OS << MAI.getData8bitsDirective(AddrSpace);
405 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000406 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000407 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000408 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000409
410 // If the data ends with 0 and the target supports .asciz, use it, otherwise
411 // use .ascii
412 if (MAI.getAscizDirective() && Data.back() == 0) {
413 OS << MAI.getAscizDirective();
414 Data = Data.substr(0, Data.size()-1);
415 } else {
416 OS << MAI.getAsciiDirective();
417 }
418
Chris Lattnera6594fc2010-01-25 18:58:59 +0000419 OS << ' ';
420 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000421 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000422}
423
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000424/// EmitIntValue - Special case of EmitValue that avoids the client having
425/// to pass in a MCExpr for constant integers.
426void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
427 unsigned AddrSpace) {
428 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000429 const char *Directive = 0;
430 switch (Size) {
431 default: break;
432 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
433 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
434 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000435 case 8:
436 Directive = MAI.getData64bitsDirective(AddrSpace);
437 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
438 if (Directive) break;
439 if (isLittleEndian()) {
440 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
441 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
442 } else {
443 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
444 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
445 }
446 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000447 }
448
449 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000450 OS << Directive << truncateToSize(Value, Size);
451 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000452}
453
Chris Lattneraaec2052010-01-19 19:46:13 +0000454void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
455 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000456 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000457 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000458 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000459 default: break;
460 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
461 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
462 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
463 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000464 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000465
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000466 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000467 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000468 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000469}
470
Chris Lattner718fb592010-01-25 21:28:50 +0000471void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
472 assert(MAI.getGPRel32Directive() != 0);
473 OS << MAI.getGPRel32Directive() << *Value;
474 EmitEOL();
475}
476
477
Chris Lattner6113b3d2010-01-19 18:52:28 +0000478/// EmitFill - Emit NumBytes bytes worth of the value specified by
479/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000480void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
481 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000482 if (NumBytes == 0) return;
483
Chris Lattneraaec2052010-01-19 19:46:13 +0000484 if (AddrSpace == 0)
485 if (const char *ZeroDirective = MAI.getZeroDirective()) {
486 OS << ZeroDirective << NumBytes;
487 if (FillValue != 0)
488 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000489 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000490 return;
491 }
492
493 // Emit a byte at a time.
494 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000495}
496
Daniel Dunbar84a29262009-06-24 19:25:34 +0000497void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
498 unsigned ValueSize,
499 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000500 // Some assemblers don't support non-power of two alignments, so we always
501 // emit alignments as a power of two if possible.
502 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000503 switch (ValueSize) {
504 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000505 case 1: OS << MAI.getAlignDirective(); break;
506 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000507 case 2: OS << ".p2alignw "; break;
508 case 4: OS << ".p2alignl "; break;
509 case 8: llvm_unreachable("Unsupported alignment size!");
510 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000511
Chris Lattner33adcfb2009-08-22 21:43:10 +0000512 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000513 OS << ByteAlignment;
514 else
515 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000516
Chris Lattner663c2d22009-08-19 06:12:02 +0000517 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000518 OS << ", 0x";
519 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000520
521 if (MaxBytesToEmit)
522 OS << ", " << MaxBytesToEmit;
523 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000524 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000525 return;
526 }
527
528 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000529 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000530 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000531 default: llvm_unreachable("Invalid size for machine code value!");
532 case 1: OS << ".balign"; break;
533 case 2: OS << ".balignw"; break;
534 case 4: OS << ".balignl"; break;
535 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000536 }
537
Chris Lattner663c2d22009-08-19 06:12:02 +0000538 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000539 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000540 if (MaxBytesToEmit)
541 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000542 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000543}
544
Kevin Enderby6e720482010-02-23 18:26:34 +0000545void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
546 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000547 // Emit with a text fill value.
548 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
549 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000550}
551
Daniel Dunbar821e3332009-08-31 08:09:28 +0000552void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000553 unsigned char Value) {
554 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000555 OS << ".org " << *Offset << ", " << (unsigned) Value;
556 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000557}
558
Chris Lattnera6594fc2010-01-25 18:58:59 +0000559
560void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
561 assert(MAI.hasSingleParameterDotFile());
562 OS << "\t.file\t";
563 PrintQuotedString(Filename, OS);
564 EmitEOL();
565}
566
567void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
568 OS << "\t.file\t" << FileNo << ' ';
569 PrintQuotedString(Filename, OS);
570 EmitEOL();
571}
572
Daniel Dunbar6b716532010-02-09 23:00:14 +0000573void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
574 raw_ostream &OS = GetCommentOS();
575 SmallString<256> Code;
576 SmallVector<MCFixup, 4> Fixups;
577 raw_svector_ostream VecOS(Code);
578 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
579 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000580
Daniel Dunbar6b716532010-02-09 23:00:14 +0000581 // If we are showing fixups, create symbolic markers in the encoded
582 // representation. We do this by making a per-bit map to the fixup item index,
583 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000584 SmallVector<uint8_t, 64> FixupMap;
585 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000586 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
587 FixupMap[i] = 0;
588
589 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
590 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000591 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000592 for (unsigned j = 0; j != Info.TargetSize; ++j) {
593 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
594 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
595 FixupMap[Index] = 1 + i;
596 }
597 }
598
599 OS << "encoding: [";
600 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
601 if (i)
602 OS << ',';
603
604 // See if all bits are the same map entry.
605 uint8_t MapEntry = FixupMap[i * 8 + 0];
606 for (unsigned j = 1; j != 8; ++j) {
607 if (FixupMap[i * 8 + j] == MapEntry)
608 continue;
609
610 MapEntry = uint8_t(~0U);
611 break;
612 }
613
614 if (MapEntry != uint8_t(~0U)) {
615 if (MapEntry == 0) {
616 OS << format("0x%02x", uint8_t(Code[i]));
617 } else {
618 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
619 OS << char('A' + MapEntry - 1);
620 }
621 } else {
622 // Otherwise, write out in binary.
623 OS << "0b";
624 for (unsigned j = 8; j--;) {
625 unsigned Bit = (Code[i] >> j) & 1;
626 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
627 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
628 OS << char('A' + MapEntry - 1);
629 } else
630 OS << Bit;
631 }
632 }
633 }
634 OS << "]\n";
635
636 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
637 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000638 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000639 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000640 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000641 }
642}
643
644void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
645 assert(CurSection && "Cannot emit contents before setting section!");
646
647 // Show the encoding in a comment if we have a code emitter.
648 if (Emitter)
649 AddEncodingComment(Inst);
650
Chris Lattner30d9a642010-02-09 00:54:51 +0000651 // Show the MCInst if enabled.
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000652 if (ShowInst)
653 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Chris Lattner30d9a642010-02-09 00:54:51 +0000654
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000655 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000656 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000657 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000658 else
659 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000660 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000661}
662
Chris Lattner91bead72010-04-03 21:35:55 +0000663/// EmitRawText - If this file is backed by a assembly streamer, this dumps
664/// the specified string in the output .s file. This capability is
665/// indicated by the hasRawTextSupport() predicate.
666void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000667 if (!String.empty() && String.back() == '\n')
668 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000669 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000670 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000671}
672
Daniel Dunbara11af532009-06-24 01:03:06 +0000673void MCAsmStreamer::Finish() {
674 OS.flush();
675}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000676
Chris Lattner86e22112010-01-22 07:29:22 +0000677MCStreamer *llvm::createAsmStreamer(MCContext &Context,
678 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000679 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000680 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000681 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000682 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000683 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000684}