blob: f9a3128f90af4bc7c30122c2329f8b7921933bfc [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 Lattner86e22112010-01-22 07:29:22 +000019#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000022#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000023#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000024#include "llvm/Support/FormattedStream.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000025using namespace llvm;
26
27namespace {
28
Chris Lattner46a947d2009-08-17 04:17:34 +000029class MCAsmStreamer : public MCStreamer {
Chris Lattner86e22112010-01-22 07:29:22 +000030 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000031 const MCAsmInfo &MAI;
Chris Lattner07404412010-01-22 07:06:15 +000032 bool IsLittleEndian, IsVerboseAsm;
Chris Lattner90edac02009-09-14 03:02:37 +000033 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;
Chris Lattner46a947d2009-08-17 04:17:34 +000038public:
Chris Lattner86e22112010-01-22 07:29:22 +000039 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
40 const MCAsmInfo &mai,
Chris Lattner07404412010-01-22 07:06:15 +000041 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
42 MCCodeEmitter *emitter)
43 : MCStreamer(Context), OS(os), MAI(mai), IsLittleEndian(isLittleEndian),
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000044 IsVerboseAsm(isVerboseAsm), InstPrinter(printer), Emitter(emitter),
45 CommentStream(CommentToEmit) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000046 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000047
Chris Lattner16582022010-01-20 06:39:07 +000048 bool isLittleEndian() const { return IsLittleEndian; }
49
Chris Lattner86e22112010-01-22 07:29:22 +000050
51 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000052 // If we don't have any comments, just emit a \n.
53 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000054 OS << '\n';
55 return;
56 }
57 EmitCommentsAndEOL();
58 }
59 void EmitCommentsAndEOL();
60
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000061 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000062 /// file if applicable as a QoI issue to make the output of the compiler
63 /// more readable. This only affects the MCAsmStreamer, and only when
64 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000065 virtual void AddComment(const Twine &T);
Chris Lattner86e22112010-01-22 07:29:22 +000066
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000067 /// GetCommentOS - Return a raw_ostream that comments can be written to.
68 /// Unlike AddComment, you are required to terminate comments with \n if you
69 /// use this method.
70 virtual raw_ostream &GetCommentOS() {
71 if (!IsVerboseAsm)
72 return nulls(); // Discard comments unless in verbose asm mode.
73 return CommentStream;
74 }
75
Chris Lattner0fd90fd2010-01-22 19:52:01 +000076 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
77 virtual void AddBlankLine() {
78 EmitEOL();
79 }
80
Chris Lattner46a947d2009-08-17 04:17:34 +000081 /// @name MCStreamer Interface
82 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000083
Chris Lattner975780b2009-08-17 05:49:08 +000084 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000085
Chris Lattner46a947d2009-08-17 04:17:34 +000086 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000087
Chris Lattnera5ad93a2010-01-23 06:39:22 +000088 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000089
Daniel Dunbar821e3332009-08-31 08:09:28 +000090 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000091
Chris Lattnera5ad93a2010-01-23 06:39:22 +000092 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000093
Chris Lattner46a947d2009-08-17 04:17:34 +000094 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000095
Chris Lattner99328ad2010-01-25 07:52:13 +000096 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +000097 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000098 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000099
Chris Lattner9eb158d2010-01-23 07:47:02 +0000100 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
101 ///
102 /// @param Symbol - The common symbol to emit.
103 /// @param Size - The size of the common symbol.
104 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
105
Daniel Dunbar8751b942009-08-28 05:48:22 +0000106 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000107 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000108
Chris Lattneraaec2052010-01-19 19:46:13 +0000109 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000110
Chris Lattneraaec2052010-01-19 19:46:13 +0000111 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000112 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
113
Chris Lattneraaec2052010-01-19 19:46:13 +0000114 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
115 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000116
Chris Lattner46a947d2009-08-17 04:17:34 +0000117 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
118 unsigned ValueSize = 1,
119 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000120
Daniel Dunbar821e3332009-08-31 08:09:28 +0000121 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000122 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000123
Chris Lattnera6594fc2010-01-25 18:58:59 +0000124 virtual void EmitFileDirective(StringRef Filename);
125 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
126
127 virtual void EmitInstruction(const MCInst &Inst);
128
Chris Lattner46a947d2009-08-17 04:17:34 +0000129 virtual void Finish();
130
131 /// @}
132};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000133
Chris Lattner46a947d2009-08-17 04:17:34 +0000134} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000135
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000136/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000137/// file if applicable as a QoI issue to make the output of the compiler
138/// more readable. This only affects the MCAsmStreamer, and only when
139/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000140void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000141 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000142
143 // Make sure that CommentStream is flushed.
144 CommentStream.flush();
145
Chris Lattner86e22112010-01-22 07:29:22 +0000146 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000147 // Each comment goes on its own line.
148 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000149
150 // Tell the comment stream that the vector changed underneath it.
151 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000152}
153
154void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000155 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
156 OS << '\n';
157 return;
158 }
159
160 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000161 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000162
163 assert(Comments.back() == '\n' &&
164 "Comment array not newline terminated");
165 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000166 // Emit a line of comments.
167 OS.PadToColumn(MAI.getCommentColumn());
168 size_t Position = Comments.find('\n');
169 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
170
Chris Lattner86e22112010-01-22 07:29:22 +0000171 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000172 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000173
Chris Lattner14ca1772010-01-22 21:16:10 +0000174 CommentToEmit.clear();
175 // Tell the comment stream that the vector changed underneath it.
176 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000177}
178
179
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000180static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
181 assert(Bytes && "Invalid size!");
182 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
183}
184
Daniel Dunbar821e3332009-08-31 08:09:28 +0000185static inline const MCExpr *truncateToSize(const MCExpr *Value,
186 unsigned Bytes) {
187 // FIXME: Do we really need this routine?
188 return Value;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000189}
190
Chris Lattner975780b2009-08-17 05:49:08 +0000191void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000192 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000193 if (Section != CurSection) {
194 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000195 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000196 }
197}
198
199void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000200 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000201 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000202
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000203 OS << *Symbol << ":";
204 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000205 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000206}
207
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000208void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000209 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000210 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000211 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000212 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000213 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000214}
215
Daniel Dunbar821e3332009-08-31 08:09:28 +0000216void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000217 // Only absolute symbols can be redefined.
218 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
219 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000220
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000221 OS << *Symbol << " = " << *Value;
222 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000223
224 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000225 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000226 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000227}
228
Daniel Dunbarfffff912009-10-16 01:34:54 +0000229void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000230 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000231 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000232 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000233 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
234 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
235 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
236 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
237 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
238 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
239 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Chris Lattnera800f7c2010-01-25 18:33:40 +0000240 OS << "\t.type " << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000241 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
242 switch (Attribute) {
243 default: assert(0 && "Unknown ELF .type");
244 case MCSA_ELF_TypeFunction: OS << "function"; break;
245 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
246 case MCSA_ELF_TypeObject: OS << "object"; break;
247 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
248 case MCSA_ELF_TypeCommon: OS << "common"; break;
249 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
250 }
251 EmitEOL();
252 return;
253 case MCSA_Global: // .globl/.global
254 OS << MAI.getGlobalDirective();
255 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000256 case MCSA_Hidden: OS << ".hidden "; break;
257 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
258 case MCSA_Internal: OS << ".internal "; break;
259 case MCSA_LazyReference: OS << ".lazy_reference "; break;
260 case MCSA_Local: OS << ".local "; break;
261 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
262 case MCSA_PrivateExtern: OS << ".private_extern "; break;
263 case MCSA_Protected: OS << ".protected "; break;
264 case MCSA_Reference: OS << ".reference "; break;
265 case MCSA_Weak: OS << ".weak "; break;
266 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
267 // .weak_reference
268 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000269 }
270
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000271 OS << *Symbol;
272 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000273}
274
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000275void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000276 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
277 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000278}
279
Chris Lattner99328ad2010-01-25 07:52:13 +0000280void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
281 assert(MAI.hasDotTypeDotSizeDirective());
282 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
283}
284
Chris Lattner9eb158d2010-01-23 07:47:02 +0000285void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000286 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000287 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000288 if (ByteAlignment != 0) {
Chris Lattner4ed54382010-01-19 06:01:04 +0000289 if (MAI.getAlignmentIsInBytes())
290 OS << ',' << ByteAlignment;
291 else
292 OS << ',' << Log2_32(ByteAlignment);
293 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000294 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000295}
296
Chris Lattner9eb158d2010-01-23 07:47:02 +0000297/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
298///
299/// @param Symbol - The common symbol to emit.
300/// @param Size - The size of the common symbol.
301void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
302 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
303 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
304 EmitEOL();
305}
306
Daniel Dunbar8751b942009-08-28 05:48:22 +0000307void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000308 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000309 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000310 OS << ".zerofill ";
311
312 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000313 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000314 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000315
Chris Lattner9be3fee2009-07-10 22:20:30 +0000316 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000317 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000318 if (ByteAlignment != 0)
319 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000320 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000321 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000322}
323
Chris Lattner12e555c2010-01-23 00:15:00 +0000324static inline char toOctal(int X) { return (X&7)+'0'; }
325
Chris Lattnera6594fc2010-01-25 18:58:59 +0000326static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
327 OS << '"';
328
329 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
330 unsigned char C = Data[i];
331 if (C == '"' || C == '\\') {
332 OS << '\\' << (char)C;
333 continue;
334 }
335
336 if (isprint((unsigned char)C)) {
337 OS << (char)C;
338 continue;
339 }
340
341 switch (C) {
342 case '\b': OS << "\\b"; break;
343 case '\f': OS << "\\f"; break;
344 case '\n': OS << "\\n"; break;
345 case '\r': OS << "\\r"; break;
346 case '\t': OS << "\\t"; break;
347 default:
348 OS << '\\';
349 OS << toOctal(C >> 6);
350 OS << toOctal(C >> 3);
351 OS << toOctal(C >> 0);
352 break;
353 }
354 }
355
356 OS << '"';
357}
358
359
Chris Lattneraaec2052010-01-19 19:46:13 +0000360void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000361 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000362 if (Data.empty()) return;
363
364 if (Data.size() == 1) {
365 OS << MAI.getData8bitsDirective(AddrSpace);
366 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000367 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000368 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000369 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000370
371 // If the data ends with 0 and the target supports .asciz, use it, otherwise
372 // use .ascii
373 if (MAI.getAscizDirective() && Data.back() == 0) {
374 OS << MAI.getAscizDirective();
375 Data = Data.substr(0, Data.size()-1);
376 } else {
377 OS << MAI.getAsciiDirective();
378 }
379
Chris Lattnera6594fc2010-01-25 18:58:59 +0000380 OS << ' ';
381 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000382 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000383}
384
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000385/// EmitIntValue - Special case of EmitValue that avoids the client having
386/// to pass in a MCExpr for constant integers.
387void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
388 unsigned AddrSpace) {
389 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000390 const char *Directive = 0;
391 switch (Size) {
392 default: break;
393 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
394 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
395 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000396 case 8:
397 Directive = MAI.getData64bitsDirective(AddrSpace);
398 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
399 if (Directive) break;
400 if (isLittleEndian()) {
401 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
402 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
403 } else {
404 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
405 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
406 }
407 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000408 }
409
410 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000411 OS << Directive << truncateToSize(Value, Size);
412 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000413}
414
Chris Lattneraaec2052010-01-19 19:46:13 +0000415void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
416 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000417 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000418 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000419 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000420 default: break;
421 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
422 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
423 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
424 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000425 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000426
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000427 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000428 OS << Directive << *truncateToSize(Value, Size);
429 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000430}
431
Chris Lattner6113b3d2010-01-19 18:52:28 +0000432/// EmitFill - Emit NumBytes bytes worth of the value specified by
433/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000434void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
435 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000436 if (NumBytes == 0) return;
437
Chris Lattneraaec2052010-01-19 19:46:13 +0000438 if (AddrSpace == 0)
439 if (const char *ZeroDirective = MAI.getZeroDirective()) {
440 OS << ZeroDirective << NumBytes;
441 if (FillValue != 0)
442 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000443 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000444 return;
445 }
446
447 // Emit a byte at a time.
448 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000449}
450
Daniel Dunbar84a29262009-06-24 19:25:34 +0000451void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
452 unsigned ValueSize,
453 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000454 // Some assemblers don't support non-power of two alignments, so we always
455 // emit alignments as a power of two if possible.
456 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000457 switch (ValueSize) {
458 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000459 case 1: OS << MAI.getAlignDirective(); break;
460 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000461 case 2: OS << ".p2alignw "; break;
462 case 4: OS << ".p2alignl "; break;
463 case 8: llvm_unreachable("Unsupported alignment size!");
464 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000465
Chris Lattner33adcfb2009-08-22 21:43:10 +0000466 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000467 OS << ByteAlignment;
468 else
469 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000470
Chris Lattner663c2d22009-08-19 06:12:02 +0000471 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000472 OS << ", 0x";
473 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000474
475 if (MaxBytesToEmit)
476 OS << ", " << MaxBytesToEmit;
477 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000478 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000479 return;
480 }
481
482 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000483 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000484 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000485 default: llvm_unreachable("Invalid size for machine code value!");
486 case 1: OS << ".balign"; break;
487 case 2: OS << ".balignw"; break;
488 case 4: OS << ".balignl"; break;
489 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000490 }
491
Chris Lattner663c2d22009-08-19 06:12:02 +0000492 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000493 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000494 if (MaxBytesToEmit)
495 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000496 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000497}
498
Daniel Dunbar821e3332009-08-31 08:09:28 +0000499void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000500 unsigned char Value) {
501 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000502 OS << ".org " << *Offset << ", " << (unsigned) Value;
503 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000504}
505
Chris Lattnera6594fc2010-01-25 18:58:59 +0000506
507void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
508 assert(MAI.hasSingleParameterDotFile());
509 OS << "\t.file\t";
510 PrintQuotedString(Filename, OS);
511 EmitEOL();
512}
513
514void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
515 OS << "\t.file\t" << FileNo << ' ';
516 PrintQuotedString(Filename, OS);
517 EmitEOL();
518}
519
520
Daniel Dunbara11af532009-06-24 01:03:06 +0000521void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000522 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000523
524 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000525 if (InstPrinter) {
526 InstPrinter->printInst(&Inst);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000527 EmitEOL();
Daniel Dunbara356aea2009-08-27 07:58:57 +0000528
529 // Show the encoding if we have a code emitter.
530 if (Emitter) {
531 SmallString<256> Code;
532 raw_svector_ostream VecOS(Code);
533 Emitter->EncodeInstruction(Inst, VecOS);
534 VecOS.flush();
535
536 OS.indent(20);
537 OS << " # encoding: [";
538 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
539 if (i)
540 OS << ',';
541 OS << format("%#04x", uint8_t(Code[i]));
542 }
543 OS << "]\n";
544 }
545
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000546 return;
547 }
548
549 // Otherwise fall back to a structural printing for now. Eventually we should
550 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000551 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000552 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000553}
554
555void MCAsmStreamer::Finish() {
556 OS.flush();
557}
558
Chris Lattner86e22112010-01-22 07:29:22 +0000559MCStreamer *llvm::createAsmStreamer(MCContext &Context,
560 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000561 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000562 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000563 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000564 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
565 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000566}