blob: 22b1a376e3aacb5768660800cf16424da95a15dc [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);
Daniel Dunbara11af532009-06-24 01:03:06 +0000112
Chris Lattner99328ad2010-01-25 07:52:13 +0000113 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000114 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000115 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000116
Chris Lattner9eb158d2010-01-23 07:47:02 +0000117 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
118 ///
119 /// @param Symbol - The common symbol to emit.
120 /// @param Size - The size of the common symbol.
121 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
122
Daniel Dunbar8751b942009-08-28 05:48:22 +0000123 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000124 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000125
Chris Lattneraaec2052010-01-19 19:46:13 +0000126 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000127
Chris Lattneraaec2052010-01-19 19:46:13 +0000128 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000129 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000130 virtual void EmitGPRel32Value(const MCExpr *Value);
131
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000132
Chris Lattneraaec2052010-01-19 19:46:13 +0000133 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
134 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000135
Chris Lattner46a947d2009-08-17 04:17:34 +0000136 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
137 unsigned ValueSize = 1,
138 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000139
Kevin Enderby6e720482010-02-23 18:26:34 +0000140 virtual void EmitCodeAlignment(unsigned ByteAlignment,
141 unsigned MaxBytesToEmit = 0);
142
Daniel Dunbar821e3332009-08-31 08:09:28 +0000143 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000144 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000145
Chris Lattnera6594fc2010-01-25 18:58:59 +0000146 virtual void EmitFileDirective(StringRef Filename);
147 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
148
149 virtual void EmitInstruction(const MCInst &Inst);
150
Chris Lattner91bead72010-04-03 21:35:55 +0000151 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
152 /// the specified string in the output .s file. This capability is
153 /// indicated by the hasRawTextSupport() predicate.
154 virtual void EmitRawText(StringRef String);
155
Chris Lattner46a947d2009-08-17 04:17:34 +0000156 virtual void Finish();
157
158 /// @}
159};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000160
Chris Lattner46a947d2009-08-17 04:17:34 +0000161} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000162
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000163/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000164/// file if applicable as a QoI issue to make the output of the compiler
165/// more readable. This only affects the MCAsmStreamer, and only when
166/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000167void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000168 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000169
170 // Make sure that CommentStream is flushed.
171 CommentStream.flush();
172
Chris Lattner86e22112010-01-22 07:29:22 +0000173 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000174 // Each comment goes on its own line.
175 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000176
177 // Tell the comment stream that the vector changed underneath it.
178 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000179}
180
181void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000182 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
183 OS << '\n';
184 return;
185 }
186
187 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000188 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000189
190 assert(Comments.back() == '\n' &&
191 "Comment array not newline terminated");
192 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000193 // Emit a line of comments.
194 OS.PadToColumn(MAI.getCommentColumn());
195 size_t Position = Comments.find('\n');
196 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
197
Chris Lattner86e22112010-01-22 07:29:22 +0000198 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000199 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000200
Chris Lattner14ca1772010-01-22 21:16:10 +0000201 CommentToEmit.clear();
202 // Tell the comment stream that the vector changed underneath it.
203 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000204}
205
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000206static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
207 assert(Bytes && "Invalid size!");
208 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
209}
210
Chris Lattner975780b2009-08-17 05:49:08 +0000211void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000212 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000213 if (Section != CurSection) {
214 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000215 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000216 }
217}
218
219void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000220 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000221 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000222 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000223
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000224 OS << *Symbol << ":";
225 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000226 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000227}
228
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000229void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000230 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000231 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000232 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000233 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000234 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000235}
236
Daniel Dunbar821e3332009-08-31 08:09:28 +0000237void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000238 OS << *Symbol << " = " << *Value;
239 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000240
241 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000242 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000243}
244
Daniel Dunbarfffff912009-10-16 01:34:54 +0000245void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000246 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000247 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000248 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000249 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
250 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
251 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
252 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
253 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
254 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
255 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000256 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000257 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
258 switch (Attribute) {
259 default: assert(0 && "Unknown ELF .type");
260 case MCSA_ELF_TypeFunction: OS << "function"; break;
261 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
262 case MCSA_ELF_TypeObject: OS << "object"; break;
263 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
264 case MCSA_ELF_TypeCommon: OS << "common"; break;
265 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
266 }
267 EmitEOL();
268 return;
269 case MCSA_Global: // .globl/.global
270 OS << MAI.getGlobalDirective();
271 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000272 case MCSA_Hidden: OS << ".hidden "; break;
273 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
274 case MCSA_Internal: OS << ".internal "; break;
275 case MCSA_LazyReference: OS << ".lazy_reference "; break;
276 case MCSA_Local: OS << ".local "; break;
277 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
278 case MCSA_PrivateExtern: OS << ".private_extern "; break;
279 case MCSA_Protected: OS << ".protected "; break;
280 case MCSA_Reference: OS << ".reference "; break;
281 case MCSA_Weak: OS << ".weak "; break;
282 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
283 // .weak_reference
284 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000285 }
286
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000287 OS << *Symbol;
288 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000289}
290
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000291void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000292 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
293 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000294}
295
Chris Lattner99328ad2010-01-25 07:52:13 +0000296void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
297 assert(MAI.hasDotTypeDotSizeDirective());
298 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
299}
300
Chris Lattner9eb158d2010-01-23 07:47:02 +0000301void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000302 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000303 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000304 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000305 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000306 OS << ',' << ByteAlignment;
307 else
308 OS << ',' << Log2_32(ByteAlignment);
309 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000310 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000311}
312
Chris Lattner9eb158d2010-01-23 07:47:02 +0000313/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
314///
315/// @param Symbol - The common symbol to emit.
316/// @param Size - The size of the common symbol.
317void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
318 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
319 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
320 EmitEOL();
321}
322
Daniel Dunbar8751b942009-08-28 05:48:22 +0000323void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000324 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000325 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000326 OS << ".zerofill ";
327
328 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000329 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000330 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000331
Chris Lattner9be3fee2009-07-10 22:20:30 +0000332 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000333 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000334 if (ByteAlignment != 0)
335 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000336 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000337 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000338}
339
Chris Lattner12e555c2010-01-23 00:15:00 +0000340static inline char toOctal(int X) { return (X&7)+'0'; }
341
Chris Lattnera6594fc2010-01-25 18:58:59 +0000342static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
343 OS << '"';
344
345 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
346 unsigned char C = Data[i];
347 if (C == '"' || C == '\\') {
348 OS << '\\' << (char)C;
349 continue;
350 }
351
352 if (isprint((unsigned char)C)) {
353 OS << (char)C;
354 continue;
355 }
356
357 switch (C) {
358 case '\b': OS << "\\b"; break;
359 case '\f': OS << "\\f"; break;
360 case '\n': OS << "\\n"; break;
361 case '\r': OS << "\\r"; break;
362 case '\t': OS << "\\t"; break;
363 default:
364 OS << '\\';
365 OS << toOctal(C >> 6);
366 OS << toOctal(C >> 3);
367 OS << toOctal(C >> 0);
368 break;
369 }
370 }
371
372 OS << '"';
373}
374
375
Chris Lattneraaec2052010-01-19 19:46:13 +0000376void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000377 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000378 if (Data.empty()) return;
379
380 if (Data.size() == 1) {
381 OS << MAI.getData8bitsDirective(AddrSpace);
382 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000383 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000384 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000385 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000386
387 // If the data ends with 0 and the target supports .asciz, use it, otherwise
388 // use .ascii
389 if (MAI.getAscizDirective() && Data.back() == 0) {
390 OS << MAI.getAscizDirective();
391 Data = Data.substr(0, Data.size()-1);
392 } else {
393 OS << MAI.getAsciiDirective();
394 }
395
Chris Lattnera6594fc2010-01-25 18:58:59 +0000396 OS << ' ';
397 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000398 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000399}
400
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000401/// EmitIntValue - Special case of EmitValue that avoids the client having
402/// to pass in a MCExpr for constant integers.
403void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
404 unsigned AddrSpace) {
405 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000406 const char *Directive = 0;
407 switch (Size) {
408 default: break;
409 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
410 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
411 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000412 case 8:
413 Directive = MAI.getData64bitsDirective(AddrSpace);
414 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
415 if (Directive) break;
416 if (isLittleEndian()) {
417 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
418 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
419 } else {
420 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
421 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
422 }
423 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000424 }
425
426 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000427 OS << Directive << truncateToSize(Value, Size);
428 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000429}
430
Chris Lattneraaec2052010-01-19 19:46:13 +0000431void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
432 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000433 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000434 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000435 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000436 default: break;
437 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
438 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
439 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
440 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000441 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000442
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000443 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000444 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000445 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000446}
447
Chris Lattner718fb592010-01-25 21:28:50 +0000448void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
449 assert(MAI.getGPRel32Directive() != 0);
450 OS << MAI.getGPRel32Directive() << *Value;
451 EmitEOL();
452}
453
454
Chris Lattner6113b3d2010-01-19 18:52:28 +0000455/// EmitFill - Emit NumBytes bytes worth of the value specified by
456/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000457void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
458 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000459 if (NumBytes == 0) return;
460
Chris Lattneraaec2052010-01-19 19:46:13 +0000461 if (AddrSpace == 0)
462 if (const char *ZeroDirective = MAI.getZeroDirective()) {
463 OS << ZeroDirective << NumBytes;
464 if (FillValue != 0)
465 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000466 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000467 return;
468 }
469
470 // Emit a byte at a time.
471 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000472}
473
Daniel Dunbar84a29262009-06-24 19:25:34 +0000474void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
475 unsigned ValueSize,
476 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000477 // Some assemblers don't support non-power of two alignments, so we always
478 // emit alignments as a power of two if possible.
479 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000480 switch (ValueSize) {
481 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000482 case 1: OS << MAI.getAlignDirective(); break;
483 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000484 case 2: OS << ".p2alignw "; break;
485 case 4: OS << ".p2alignl "; break;
486 case 8: llvm_unreachable("Unsupported alignment size!");
487 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000488
Chris Lattner33adcfb2009-08-22 21:43:10 +0000489 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000490 OS << ByteAlignment;
491 else
492 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000493
Chris Lattner663c2d22009-08-19 06:12:02 +0000494 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000495 OS << ", 0x";
496 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000497
498 if (MaxBytesToEmit)
499 OS << ", " << MaxBytesToEmit;
500 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000501 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000502 return;
503 }
504
505 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000506 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000507 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000508 default: llvm_unreachable("Invalid size for machine code value!");
509 case 1: OS << ".balign"; break;
510 case 2: OS << ".balignw"; break;
511 case 4: OS << ".balignl"; break;
512 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000513 }
514
Chris Lattner663c2d22009-08-19 06:12:02 +0000515 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000516 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000517 if (MaxBytesToEmit)
518 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000519 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000520}
521
Kevin Enderby6e720482010-02-23 18:26:34 +0000522void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
523 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000524 // Emit with a text fill value.
525 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
526 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000527}
528
Daniel Dunbar821e3332009-08-31 08:09:28 +0000529void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000530 unsigned char Value) {
531 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000532 OS << ".org " << *Offset << ", " << (unsigned) Value;
533 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000534}
535
Chris Lattnera6594fc2010-01-25 18:58:59 +0000536
537void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
538 assert(MAI.hasSingleParameterDotFile());
539 OS << "\t.file\t";
540 PrintQuotedString(Filename, OS);
541 EmitEOL();
542}
543
544void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
545 OS << "\t.file\t" << FileNo << ' ';
546 PrintQuotedString(Filename, OS);
547 EmitEOL();
548}
549
Daniel Dunbar6b716532010-02-09 23:00:14 +0000550void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
551 raw_ostream &OS = GetCommentOS();
552 SmallString<256> Code;
553 SmallVector<MCFixup, 4> Fixups;
554 raw_svector_ostream VecOS(Code);
555 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
556 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000557
Daniel Dunbar6b716532010-02-09 23:00:14 +0000558 // If we are showing fixups, create symbolic markers in the encoded
559 // representation. We do this by making a per-bit map to the fixup item index,
560 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000561 SmallVector<uint8_t, 64> FixupMap;
562 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000563 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
564 FixupMap[i] = 0;
565
566 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
567 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000568 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000569 for (unsigned j = 0; j != Info.TargetSize; ++j) {
570 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
571 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
572 FixupMap[Index] = 1 + i;
573 }
574 }
575
576 OS << "encoding: [";
577 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
578 if (i)
579 OS << ',';
580
581 // See if all bits are the same map entry.
582 uint8_t MapEntry = FixupMap[i * 8 + 0];
583 for (unsigned j = 1; j != 8; ++j) {
584 if (FixupMap[i * 8 + j] == MapEntry)
585 continue;
586
587 MapEntry = uint8_t(~0U);
588 break;
589 }
590
591 if (MapEntry != uint8_t(~0U)) {
592 if (MapEntry == 0) {
593 OS << format("0x%02x", uint8_t(Code[i]));
594 } else {
595 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
596 OS << char('A' + MapEntry - 1);
597 }
598 } else {
599 // Otherwise, write out in binary.
600 OS << "0b";
601 for (unsigned j = 8; j--;) {
602 unsigned Bit = (Code[i] >> j) & 1;
603 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
604 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
605 OS << char('A' + MapEntry - 1);
606 } else
607 OS << Bit;
608 }
609 }
610 }
611 OS << "]\n";
612
613 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
614 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000615 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000616 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000617 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000618 }
619}
620
621void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
622 assert(CurSection && "Cannot emit contents before setting section!");
623
624 // Show the encoding in a comment if we have a code emitter.
625 if (Emitter)
626 AddEncodingComment(Inst);
627
Chris Lattner30d9a642010-02-09 00:54:51 +0000628 // Show the MCInst if enabled.
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000629 if (ShowInst)
630 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Chris Lattner30d9a642010-02-09 00:54:51 +0000631
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000632 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000633 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000634 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000635 else
636 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000637 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000638}
639
Chris Lattner91bead72010-04-03 21:35:55 +0000640/// EmitRawText - If this file is backed by a assembly streamer, this dumps
641/// the specified string in the output .s file. This capability is
642/// indicated by the hasRawTextSupport() predicate.
643void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000644 if (!String.empty() && String.back() == '\n')
645 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000646 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000647 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000648}
649
Daniel Dunbara11af532009-06-24 01:03:06 +0000650void MCAsmStreamer::Finish() {
651 OS.flush();
652}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000653
Chris Lattner86e22112010-01-22 07:29:22 +0000654MCStreamer *llvm::createAsmStreamer(MCContext &Context,
655 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000656 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000657 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000658 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000659 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000660 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000661}