blob: b92051791a0bd31196b1c96f006a8286c7913721 [file] [log] [blame]
Daniel Dunbara5cc0032009-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 Dunbar6e966212009-08-31 08:08:38 +000011#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar12266ad2009-08-27 00:51:57 +000012#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbarfc387ba2009-07-01 06:35:03 +000015#include "llvm/MC/MCInst.h"
Chris Lattnera835afd2009-09-14 03:02:37 +000016#include "llvm/MC/MCInstPrinter.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000017#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000018#include "llvm/MC/MCSymbol.h"
Chris Lattner0a2603a2010-03-19 05:48:53 +000019#include "llvm/ADT/OwningPtr.h"
Chris Lattner7cb3b782010-01-22 07:29:22 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/Twine.h"
Edwin Török675d5622009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner4891d142009-08-19 06:12:02 +000023#include "llvm/Support/MathExtras.h"
Daniel Dunbar12266ad2009-08-27 00:51:57 +000024#include "llvm/Support/Format.h"
Chris Lattner7cb3b782010-01-22 07:29:22 +000025#include "llvm/Support/FormattedStream.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000026using namespace llvm;
27
28namespace {
29
Chris Lattner241c5f82009-08-17 04:17:34 +000030class MCAsmStreamer : public MCStreamer {
Chris Lattner7cb3b782010-01-22 07:29:22 +000031 formatted_raw_ostream &OS;
Chris Lattnera5ef4d32009-08-22 21:43:10 +000032 const MCAsmInfo &MAI;
Chris Lattner0a2603a2010-03-19 05:48:53 +000033 OwningPtr<MCInstPrinter> InstPrinter;
Daniel Dunbar12266ad2009-08-27 00:51:57 +000034 MCCodeEmitter *Emitter;
Chris Lattner7cb3b782010-01-22 07:29:22 +000035
36 SmallString<128> CommentToEmit;
Chris Lattner49d74392010-01-22 19:17:48 +000037 raw_svector_ostream CommentStream;
Daniel Dunbar3824fad2010-02-03 18:18:30 +000038
39 unsigned IsLittleEndian : 1;
40 unsigned IsVerboseAsm : 1;
41 unsigned ShowInst : 1;
42
Chris Lattner241c5f82009-08-17 04:17:34 +000043public:
Chris Lattner7cb3b782010-01-22 07:29:22 +000044 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Chris Lattner1ab24612010-01-22 07:06:15 +000045 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
Daniel Dunbar4755a9b2010-02-10 01:41:14 +000046 MCCodeEmitter *emitter, bool showInst)
Chris Lattner6489bbb2010-03-12 18:28:53 +000047 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
48 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
Daniel Dunbar3824fad2010-02-03 18:18:30 +000049 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
Daniel Dunbar4755a9b2010-02-10 01:41:14 +000050 ShowInst(showInst) {
Chris Lattner46b11fa2010-02-10 00:10:18 +000051 if (InstPrinter && IsVerboseAsm)
52 InstPrinter->setCommentStream(CommentStream);
53 }
Chris Lattner241c5f82009-08-17 04:17:34 +000054 ~MCAsmStreamer() {}
Daniel Dunbara5cc0032009-06-24 01:03:06 +000055
Chris Lattner92c78e82010-01-20 06:39:07 +000056 bool isLittleEndian() const { return IsLittleEndian; }
Daniel Dunbarf4a84682010-02-09 23:00:14 +000057
Chris Lattner7cb3b782010-01-22 07:29:22 +000058 inline void EmitEOL() {
Chris Lattner49d74392010-01-22 19:17:48 +000059 // If we don't have any comments, just emit a \n.
60 if (!IsVerboseAsm) {
Chris Lattner7cb3b782010-01-22 07:29:22 +000061 OS << '\n';
62 return;
63 }
64 EmitCommentsAndEOL();
65 }
66 void EmitCommentsAndEOL();
Chris Lattner8d2970b2010-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 Lattner11b3d382010-04-03 21:35:55 +000071
72 /// hasRawTextSupport - We support EmitRawText.
73 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbarf4a84682010-02-09 23:00:14 +000074
Chris Lattner021da8e2010-01-22 18:21:35 +000075 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner7cb3b782010-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 Lattner021da8e2010-01-22 18:21:35 +000079 virtual void AddComment(const Twine &T);
Daniel Dunbarf4a84682010-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 Lattner49d74392010-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 Dunbarf4a84682010-02-09 23:00:14 +000092
Chris Lattnerc006e1c2010-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 Dunbarf4a84682010-02-09 23:00:14 +000097
Chris Lattner241c5f82009-08-17 04:17:34 +000098 /// @name MCStreamer Interface
99 /// @{
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000100
Chris Lattner2f579bd2009-08-17 05:49:08 +0000101 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000102
Chris Lattner241c5f82009-08-17 04:17:34 +0000103 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000104
Chris Lattner2d7c8142010-01-23 06:39:22 +0000105 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000106
Daniel Dunbard79f4322009-08-31 08:09:28 +0000107 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000108
Chris Lattner2d7c8142010-01-23 06:39:22 +0000109 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderby3694db22009-07-13 21:03:15 +0000110
Chris Lattner241c5f82009-08-17 04:17:34 +0000111 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000112
Chris Lattnere13c8ac2010-01-25 07:52:13 +0000113 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnerd70deeb2010-01-23 07:47:02 +0000114 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000115 unsigned ByteAlignment);
Kevin Enderby8ae702b2009-07-14 18:17:10 +0000116
Chris Lattnerd70deeb2010-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 Dunbar15f1a5c2009-08-28 05:48:22 +0000123 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000124 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderbyd7895a12009-07-14 21:35:03 +0000125
Chris Lattnera71dc602010-01-19 19:46:13 +0000126 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattnere29dbe32009-07-07 20:30:46 +0000127
Chris Lattnera71dc602010-01-19 19:46:13 +0000128 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner067f64712010-01-19 22:03:38 +0000129 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner3dac3412010-01-25 21:28:50 +0000130 virtual void EmitGPRel32Value(const MCExpr *Value);
131
Chris Lattner067f64712010-01-19 22:03:38 +0000132
Chris Lattnera71dc602010-01-19 19:46:13 +0000133 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
134 unsigned AddrSpace);
Chris Lattner349007a2009-07-10 22:20:30 +0000135
Chris Lattner241c5f82009-08-17 04:17:34 +0000136 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
137 unsigned ValueSize = 1,
138 unsigned MaxBytesToEmit = 0);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000139
Kevin Enderbyd767d4e2010-02-23 18:26:34 +0000140 virtual void EmitCodeAlignment(unsigned ByteAlignment,
141 unsigned MaxBytesToEmit = 0);
142
Daniel Dunbard79f4322009-08-31 08:09:28 +0000143 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner241c5f82009-08-17 04:17:34 +0000144 unsigned char Value = 0);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000145
Chris Lattner59be4ea2010-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 Lattner11b3d382010-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 Lattner241c5f82009-08-17 04:17:34 +0000156 virtual void Finish();
157
158 /// @}
159};
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000160
Chris Lattner241c5f82009-08-17 04:17:34 +0000161} // end anonymous namespace.
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000162
Chris Lattner021da8e2010-01-22 18:21:35 +0000163/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner7cb3b782010-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 Lattner021da8e2010-01-22 18:21:35 +0000167void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner7cb3b782010-01-22 07:29:22 +0000168 if (!IsVerboseAsm) return;
Chris Lattner49d74392010-01-22 19:17:48 +0000169
170 // Make sure that CommentStream is flushed.
171 CommentStream.flush();
172
Chris Lattner7cb3b782010-01-22 07:29:22 +0000173 T.toVector(CommentToEmit);
Chris Lattner49d74392010-01-22 19:17:48 +0000174 // Each comment goes on its own line.
175 CommentToEmit.push_back('\n');
Chris Lattner5c584202010-01-22 21:16:10 +0000176
177 // Tell the comment stream that the vector changed underneath it.
178 CommentStream.resync();
Chris Lattner7cb3b782010-01-22 07:29:22 +0000179}
180
181void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattner49d74392010-01-22 19:17:48 +0000182 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
183 OS << '\n';
184 return;
185 }
186
187 CommentStream.flush();
Chris Lattner7cb3b782010-01-22 07:29:22 +0000188 StringRef Comments = CommentToEmit.str();
Chris Lattner49d74392010-01-22 19:17:48 +0000189
190 assert(Comments.back() == '\n' &&
191 "Comment array not newline terminated");
192 do {
Chris Lattner7cb3b782010-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 Lattner7cb3b782010-01-22 07:29:22 +0000198 Comments = Comments.substr(Position+1);
Chris Lattner49d74392010-01-22 19:17:48 +0000199 } while (!Comments.empty());
Chris Lattner7cb3b782010-01-22 07:29:22 +0000200
Chris Lattner5c584202010-01-22 21:16:10 +0000201 CommentToEmit.clear();
202 // Tell the comment stream that the vector changed underneath it.
203 CommentStream.resync();
Chris Lattner7cb3b782010-01-22 07:29:22 +0000204}
205
Daniel Dunbar7c48d622009-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 Lattner2f579bd2009-08-17 05:49:08 +0000211void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner73266f92009-08-19 05:49:37 +0000212 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000213 if (Section != CurSection) {
214 CurSection = Section;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000215 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000216 }
217}
218
219void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000220 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar75a52922009-06-24 17:00:42 +0000221 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar75a52922009-06-24 17:00:42 +0000222
Chris Lattner987b59e2010-01-22 07:36:39 +0000223 OS << *Symbol << ":";
224 EmitEOL();
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000225 Symbol->setSection(*CurSection);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000226}
227
Chris Lattner2d7c8142010-01-23 06:39:22 +0000228void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderby68c81be2009-07-16 17:56:39 +0000229 switch (Flag) {
Daniel Dunbar6a3d1732009-08-13 23:36:34 +0000230 default: assert(0 && "Invalid flag!");
Chris Lattner2d7c8142010-01-23 06:39:22 +0000231 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderby68c81be2009-07-16 17:56:39 +0000232 }
Chris Lattner987b59e2010-01-22 07:36:39 +0000233 EmitEOL();
Kevin Enderby3694db22009-07-13 21:03:15 +0000234}
235
Daniel Dunbard79f4322009-08-31 08:09:28 +0000236void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000237 // Only absolute symbols can be redefined.
238 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
239 "Cannot define a symbol twice!");
Daniel Dunbar75a52922009-06-24 17:00:42 +0000240
Chris Lattner987b59e2010-01-22 07:36:39 +0000241 OS << *Symbol << " = " << *Value;
242 EmitEOL();
Daniel Dunbar86cd1e92009-10-16 01:34:54 +0000243
244 // FIXME: Lift context changes into super class.
Daniel Dunbar9ce249c2009-10-16 01:57:39 +0000245 // FIXME: Set associated section.
Daniel Dunbar86cd1e92009-10-16 01:34:54 +0000246 Symbol->setValue(Value);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000247}
248
Daniel Dunbar86cd1e92009-10-16 01:34:54 +0000249void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner2d7c8142010-01-23 06:39:22 +0000250 MCSymbolAttr Attribute) {
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000251 switch (Attribute) {
Chris Lattner2d7c8142010-01-23 06:39:22 +0000252 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnera5b52682010-01-25 18:30:45 +0000253 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
254 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
255 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
256 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
257 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
258 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
259 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohmana2eca822010-02-04 01:42:13 +0000260 OS << "\t.type\t" << *Symbol << ','
Chris Lattnera5b52682010-01-25 18:30:45 +0000261 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
262 switch (Attribute) {
263 default: assert(0 && "Unknown ELF .type");
264 case MCSA_ELF_TypeFunction: OS << "function"; break;
265 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
266 case MCSA_ELF_TypeObject: OS << "object"; break;
267 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
268 case MCSA_ELF_TypeCommon: OS << "common"; break;
269 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
270 }
271 EmitEOL();
272 return;
273 case MCSA_Global: // .globl/.global
274 OS << MAI.getGlobalDirective();
275 break;
Chris Lattner2d7c8142010-01-23 06:39:22 +0000276 case MCSA_Hidden: OS << ".hidden "; break;
277 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
278 case MCSA_Internal: OS << ".internal "; break;
279 case MCSA_LazyReference: OS << ".lazy_reference "; break;
280 case MCSA_Local: OS << ".local "; break;
281 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
282 case MCSA_PrivateExtern: OS << ".private_extern "; break;
283 case MCSA_Protected: OS << ".protected "; break;
284 case MCSA_Reference: OS << ".reference "; break;
285 case MCSA_Weak: OS << ".weak "; break;
286 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
287 // .weak_reference
288 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000289 }
290
Chris Lattner987b59e2010-01-22 07:36:39 +0000291 OS << *Symbol;
292 EmitEOL();
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000293}
294
Kevin Enderby8ae702b2009-07-14 18:17:10 +0000295void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner987b59e2010-01-22 07:36:39 +0000296 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
297 EmitEOL();
Kevin Enderby8ae702b2009-07-14 18:17:10 +0000298}
299
Chris Lattnere13c8ac2010-01-25 07:52:13 +0000300void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
301 assert(MAI.hasDotTypeDotSizeDirective());
302 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
303}
304
Chris Lattnerd70deeb2010-01-23 07:47:02 +0000305void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000306 unsigned ByteAlignment) {
Chris Lattnerd70deeb2010-01-23 07:47:02 +0000307 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner22a60372010-01-25 07:29:13 +0000308 if (ByteAlignment != 0) {
Rafael Espindola8fd77a12010-01-26 20:21:43 +0000309 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattnerecf1cdc2010-01-19 06:01:04 +0000310 OS << ',' << ByteAlignment;
311 else
312 OS << ',' << Log2_32(ByteAlignment);
313 }
Chris Lattner987b59e2010-01-22 07:36:39 +0000314 EmitEOL();
Chris Lattnere29dbe32009-07-07 20:30:46 +0000315}
316
Chris Lattnerd70deeb2010-01-23 07:47:02 +0000317/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
318///
319/// @param Symbol - The common symbol to emit.
320/// @param Size - The size of the common symbol.
321void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
322 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
323 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
324 EmitEOL();
325}
326
Daniel Dunbar15f1a5c2009-08-28 05:48:22 +0000327void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000328 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar6a3d1732009-08-13 23:36:34 +0000329 // Note: a .zerofill directive does not switch sections.
Chris Lattner26aabb92009-08-08 23:39:42 +0000330 OS << ".zerofill ";
331
332 // This is a mach-o specific directive.
Chris Lattner72a676a2009-08-10 01:39:42 +0000333 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar0305b2d2009-08-14 18:51:45 +0000334 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner26aabb92009-08-08 23:39:42 +0000335
Chris Lattner349007a2009-07-10 22:20:30 +0000336 if (Symbol != NULL) {
Chris Lattnerce409842010-01-17 21:43:43 +0000337 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000338 if (ByteAlignment != 0)
339 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner349007a2009-07-10 22:20:30 +0000340 }
Chris Lattner987b59e2010-01-22 07:36:39 +0000341 EmitEOL();
Chris Lattner349007a2009-07-10 22:20:30 +0000342}
343
Chris Lattner1fa30cb2010-01-23 00:15:00 +0000344static inline char toOctal(int X) { return (X&7)+'0'; }
345
Chris Lattner59be4ea2010-01-25 18:58:59 +0000346static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
347 OS << '"';
348
349 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
350 unsigned char C = Data[i];
351 if (C == '"' || C == '\\') {
352 OS << '\\' << (char)C;
353 continue;
354 }
355
356 if (isprint((unsigned char)C)) {
357 OS << (char)C;
358 continue;
359 }
360
361 switch (C) {
362 case '\b': OS << "\\b"; break;
363 case '\f': OS << "\\f"; break;
364 case '\n': OS << "\\n"; break;
365 case '\r': OS << "\\r"; break;
366 case '\t': OS << "\\t"; break;
367 default:
368 OS << '\\';
369 OS << toOctal(C >> 6);
370 OS << toOctal(C >> 3);
371 OS << toOctal(C >> 0);
372 break;
373 }
374 }
375
376 OS << '"';
377}
378
379
Chris Lattnera71dc602010-01-19 19:46:13 +0000380void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000381 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner1fa30cb2010-01-23 00:15:00 +0000382 if (Data.empty()) return;
383
384 if (Data.size() == 1) {
385 OS << MAI.getData8bitsDirective(AddrSpace);
386 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner987b59e2010-01-22 07:36:39 +0000387 EmitEOL();
Chris Lattner1fa30cb2010-01-23 00:15:00 +0000388 return;
Chris Lattner987b59e2010-01-22 07:36:39 +0000389 }
Chris Lattner1fa30cb2010-01-23 00:15:00 +0000390
391 // If the data ends with 0 and the target supports .asciz, use it, otherwise
392 // use .ascii
393 if (MAI.getAscizDirective() && Data.back() == 0) {
394 OS << MAI.getAscizDirective();
395 Data = Data.substr(0, Data.size()-1);
396 } else {
397 OS << MAI.getAsciiDirective();
398 }
399
Chris Lattner59be4ea2010-01-25 18:58:59 +0000400 OS << ' ';
401 PrintQuotedString(Data, OS);
Chris Lattner1fa30cb2010-01-23 00:15:00 +0000402 EmitEOL();
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000403}
404
Chris Lattner067f64712010-01-19 22:03:38 +0000405/// EmitIntValue - Special case of EmitValue that avoids the client having
406/// to pass in a MCExpr for constant integers.
407void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
408 unsigned AddrSpace) {
409 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner067f64712010-01-19 22:03:38 +0000410 const char *Directive = 0;
411 switch (Size) {
412 default: break;
413 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
414 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
415 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattnera39bf4e2010-01-20 06:45:39 +0000416 case 8:
417 Directive = MAI.getData64bitsDirective(AddrSpace);
418 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
419 if (Directive) break;
420 if (isLittleEndian()) {
421 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
422 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
423 } else {
424 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
425 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
426 }
427 return;
Chris Lattner067f64712010-01-19 22:03:38 +0000428 }
429
430 assert(Directive && "Invalid size for machine code value!");
Chris Lattner7cb3b782010-01-22 07:29:22 +0000431 OS << Directive << truncateToSize(Value, Size);
432 EmitEOL();
Chris Lattner067f64712010-01-19 22:03:38 +0000433}
434
Chris Lattnera71dc602010-01-19 19:46:13 +0000435void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
436 unsigned AddrSpace) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000437 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner067f64712010-01-19 22:03:38 +0000438 const char *Directive = 0;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000439 switch (Size) {
Chris Lattner067f64712010-01-19 22:03:38 +0000440 default: break;
441 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
442 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
443 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
444 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000445 }
Chris Lattnera71dc602010-01-19 19:46:13 +0000446
Chris Lattner067f64712010-01-19 22:03:38 +0000447 assert(Directive && "Invalid size for machine code value!");
Chris Lattner3dac3412010-01-25 21:28:50 +0000448 OS << Directive << *Value;
Chris Lattner7cb3b782010-01-22 07:29:22 +0000449 EmitEOL();
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000450}
451
Chris Lattner3dac3412010-01-25 21:28:50 +0000452void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
453 assert(MAI.getGPRel32Directive() != 0);
454 OS << MAI.getGPRel32Directive() << *Value;
455 EmitEOL();
456}
457
458
Chris Lattner7a787a22010-01-19 18:52:28 +0000459/// EmitFill - Emit NumBytes bytes worth of the value specified by
460/// FillValue. This implements directives such as '.space'.
Chris Lattnera71dc602010-01-19 19:46:13 +0000461void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
462 unsigned AddrSpace) {
Chris Lattner3b296362010-01-19 18:58:52 +0000463 if (NumBytes == 0) return;
464
Chris Lattnera71dc602010-01-19 19:46:13 +0000465 if (AddrSpace == 0)
466 if (const char *ZeroDirective = MAI.getZeroDirective()) {
467 OS << ZeroDirective << NumBytes;
468 if (FillValue != 0)
469 OS << ',' << (int)FillValue;
Chris Lattner7cb3b782010-01-22 07:29:22 +0000470 EmitEOL();
Chris Lattnera71dc602010-01-19 19:46:13 +0000471 return;
472 }
473
474 // Emit a byte at a time.
475 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner7a787a22010-01-19 18:52:28 +0000476}
477
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000478void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
479 unsigned ValueSize,
480 unsigned MaxBytesToEmit) {
Chris Lattner4891d142009-08-19 06:12:02 +0000481 // Some assemblers don't support non-power of two alignments, so we always
482 // emit alignments as a power of two if possible.
483 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6ad99b22009-08-19 06:35:36 +0000484 switch (ValueSize) {
485 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000486 case 1: OS << MAI.getAlignDirective(); break;
487 // FIXME: use MAI for this!
Chris Lattner6ad99b22009-08-19 06:35:36 +0000488 case 2: OS << ".p2alignw "; break;
489 case 4: OS << ".p2alignl "; break;
490 case 8: llvm_unreachable("Unsupported alignment size!");
491 }
Chris Lattner4891d142009-08-19 06:12:02 +0000492
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000493 if (MAI.getAlignmentIsInBytes())
Chris Lattner4891d142009-08-19 06:12:02 +0000494 OS << ByteAlignment;
495 else
496 OS << Log2_32(ByteAlignment);
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000497
Chris Lattner4891d142009-08-19 06:12:02 +0000498 if (Value || MaxBytesToEmit) {
Chris Lattner27f44ad2009-09-03 04:01:10 +0000499 OS << ", 0x";
500 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner4891d142009-08-19 06:12:02 +0000501
502 if (MaxBytesToEmit)
503 OS << ", " << MaxBytesToEmit;
504 }
Chris Lattner987b59e2010-01-22 07:36:39 +0000505 EmitEOL();
Chris Lattner4891d142009-08-19 06:12:02 +0000506 return;
507 }
508
509 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000510 // FIXME: Parameterize this based on MAI.
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000511 switch (ValueSize) {
Chris Lattner4891d142009-08-19 06:12:02 +0000512 default: llvm_unreachable("Invalid size for machine code value!");
513 case 1: OS << ".balign"; break;
514 case 2: OS << ".balignw"; break;
515 case 4: OS << ".balignl"; break;
516 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000517 }
518
Chris Lattner4891d142009-08-19 06:12:02 +0000519 OS << ' ' << ByteAlignment;
Daniel Dunbar7c48d622009-06-25 21:03:18 +0000520 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000521 if (MaxBytesToEmit)
522 OS << ", " << MaxBytesToEmit;
Chris Lattner987b59e2010-01-22 07:36:39 +0000523 EmitEOL();
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000524}
525
Kevin Enderbyd767d4e2010-02-23 18:26:34 +0000526void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
527 unsigned MaxBytesToEmit) {
Chris Lattner373e2e42010-02-23 18:44:31 +0000528 // Emit with a text fill value.
529 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
530 1, MaxBytesToEmit);
Kevin Enderbyd767d4e2010-02-23 18:26:34 +0000531}
532
Daniel Dunbard79f4322009-08-31 08:09:28 +0000533void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000534 unsigned char Value) {
535 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner987b59e2010-01-22 07:36:39 +0000536 OS << ".org " << *Offset << ", " << (unsigned) Value;
537 EmitEOL();
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000538}
539
Chris Lattner59be4ea2010-01-25 18:58:59 +0000540
541void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
542 assert(MAI.hasSingleParameterDotFile());
543 OS << "\t.file\t";
544 PrintQuotedString(Filename, OS);
545 EmitEOL();
546}
547
548void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
549 OS << "\t.file\t" << FileNo << ' ';
550 PrintQuotedString(Filename, OS);
551 EmitEOL();
552}
553
Daniel Dunbarf4a84682010-02-09 23:00:14 +0000554void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
555 raw_ostream &OS = GetCommentOS();
556 SmallString<256> Code;
557 SmallVector<MCFixup, 4> Fixups;
558 raw_svector_ostream VecOS(Code);
559 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
560 VecOS.flush();
Chris Lattner59be4ea2010-01-25 18:58:59 +0000561
Daniel Dunbarf4a84682010-02-09 23:00:14 +0000562 // If we are showing fixups, create symbolic markers in the encoded
563 // representation. We do this by making a per-bit map to the fixup item index,
564 // then trying to display it as nicely as possible.
Daniel Dunbar4755a9b2010-02-10 01:41:14 +0000565 SmallVector<uint8_t, 64> FixupMap;
566 FixupMap.resize(Code.size() * 8);
Daniel Dunbarf4a84682010-02-09 23:00:14 +0000567 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
568 FixupMap[i] = 0;
569
570 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
571 MCFixup &F = Fixups[i];
Chris Lattner79eb84f2010-02-11 21:27:18 +0000572 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbarf4a84682010-02-09 23:00:14 +0000573 for (unsigned j = 0; j != Info.TargetSize; ++j) {
574 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
575 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
576 FixupMap[Index] = 1 + i;
577 }
578 }
579
580 OS << "encoding: [";
581 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
582 if (i)
583 OS << ',';
584
585 // See if all bits are the same map entry.
586 uint8_t MapEntry = FixupMap[i * 8 + 0];
587 for (unsigned j = 1; j != 8; ++j) {
588 if (FixupMap[i * 8 + j] == MapEntry)
589 continue;
590
591 MapEntry = uint8_t(~0U);
592 break;
593 }
594
595 if (MapEntry != uint8_t(~0U)) {
596 if (MapEntry == 0) {
597 OS << format("0x%02x", uint8_t(Code[i]));
598 } else {
599 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
600 OS << char('A' + MapEntry - 1);
601 }
602 } else {
603 // Otherwise, write out in binary.
604 OS << "0b";
605 for (unsigned j = 8; j--;) {
606 unsigned Bit = (Code[i] >> j) & 1;
607 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
608 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
609 OS << char('A' + MapEntry - 1);
610 } else
611 OS << Bit;
612 }
613 }
614 }
615 OS << "]\n";
616
617 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
618 MCFixup &F = Fixups[i];
Chris Lattner79eb84f2010-02-11 21:27:18 +0000619 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbarf4a84682010-02-09 23:00:14 +0000620 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar6f7c77a2010-02-10 04:47:08 +0000621 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbarf4a84682010-02-09 23:00:14 +0000622 }
623}
624
625void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
626 assert(CurSection && "Cannot emit contents before setting section!");
627
628 // Show the encoding in a comment if we have a code emitter.
629 if (Emitter)
630 AddEncodingComment(Inst);
631
Chris Lattner6e8be152010-02-09 00:54:51 +0000632 // Show the MCInst if enabled.
Daniel Dunbar55636cf2010-03-22 21:49:34 +0000633 if (ShowInst)
634 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Chris Lattner6e8be152010-02-09 00:54:51 +0000635
Daniel Dunbar55636cf2010-03-22 21:49:34 +0000636 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar3824fad2010-02-03 18:18:30 +0000637 if (InstPrinter)
638 InstPrinter->printInst(&Inst);
639 else
640 Inst.print(OS, &MAI);
Chris Lattner987b59e2010-01-22 07:36:39 +0000641 EmitEOL();
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000642}
643
Chris Lattner11b3d382010-04-03 21:35:55 +0000644/// EmitRawText - If this file is backed by a assembly streamer, this dumps
645/// the specified string in the output .s file. This capability is
646/// indicated by the hasRawTextSupport() predicate.
647void MCAsmStreamer::EmitRawText(StringRef String) {
648 if (!CommentToEmit.empty() || CommentStream.GetNumBytesInBuffer() != 0)
649 EmitCommentsAndEOL();
650
651 OS << String;
652
653 if (!String.empty() && String.back() != '\n')
654 OS << '\n';
655}
656
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000657void MCAsmStreamer::Finish() {
658 OS.flush();
659}
Daniel Dunbar3824fad2010-02-03 18:18:30 +0000660
Chris Lattner7cb3b782010-01-22 07:29:22 +0000661MCStreamer *llvm::createAsmStreamer(MCContext &Context,
662 formatted_raw_ostream &OS,
Chris Lattner6489bbb2010-03-12 18:28:53 +0000663 bool isLittleEndian,
Chris Lattner1ab24612010-01-22 07:06:15 +0000664 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4755a9b2010-02-10 01:41:14 +0000665 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattner6489bbb2010-03-12 18:28:53 +0000666 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Daniel Dunbar4755a9b2010-02-10 01:41:14 +0000667 IP, CE, ShowInst);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000668}