blob: b544d04f14f4931a26536d97f3da560fbdc3ded4 [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);
Chris Lattner718fb592010-01-25 21:28:50 +0000113 virtual void EmitGPRel32Value(const MCExpr *Value);
114
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000115
Chris Lattneraaec2052010-01-19 19:46:13 +0000116 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
117 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000118
Chris Lattner46a947d2009-08-17 04:17:34 +0000119 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
120 unsigned ValueSize = 1,
121 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000122
Daniel Dunbar821e3332009-08-31 08:09:28 +0000123 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000124 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000125
Chris Lattnera6594fc2010-01-25 18:58:59 +0000126 virtual void EmitFileDirective(StringRef Filename);
127 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
128
129 virtual void EmitInstruction(const MCInst &Inst);
130
Chris Lattner46a947d2009-08-17 04:17:34 +0000131 virtual void Finish();
132
133 /// @}
134};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000135
Chris Lattner46a947d2009-08-17 04:17:34 +0000136} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000137
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000138/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000139/// file if applicable as a QoI issue to make the output of the compiler
140/// more readable. This only affects the MCAsmStreamer, and only when
141/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000142void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000143 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000144
145 // Make sure that CommentStream is flushed.
146 CommentStream.flush();
147
Chris Lattner86e22112010-01-22 07:29:22 +0000148 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000149 // Each comment goes on its own line.
150 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000151
152 // Tell the comment stream that the vector changed underneath it.
153 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000154}
155
156void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000157 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
158 OS << '\n';
159 return;
160 }
161
162 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000163 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000164
165 assert(Comments.back() == '\n' &&
166 "Comment array not newline terminated");
167 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000168 // Emit a line of comments.
169 OS.PadToColumn(MAI.getCommentColumn());
170 size_t Position = Comments.find('\n');
171 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
172
Chris Lattner86e22112010-01-22 07:29:22 +0000173 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000174 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000175
Chris Lattner14ca1772010-01-22 21:16:10 +0000176 CommentToEmit.clear();
177 // Tell the comment stream that the vector changed underneath it.
178 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000179}
180
181
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000182static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
183 assert(Bytes && "Invalid size!");
184 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
185}
186
Chris Lattner975780b2009-08-17 05:49:08 +0000187void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000188 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000189 if (Section != CurSection) {
190 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000191 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000192 }
193}
194
195void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000196 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000197 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000198
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000199 OS << *Symbol << ":";
200 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000201 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000202}
203
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000204void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000205 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000206 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000207 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000208 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000209 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000210}
211
Daniel Dunbar821e3332009-08-31 08:09:28 +0000212void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000213 // Only absolute symbols can be redefined.
214 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
215 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000216
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000217 OS << *Symbol << " = " << *Value;
218 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000219
220 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000221 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000222 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000223}
224
Daniel Dunbarfffff912009-10-16 01:34:54 +0000225void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000226 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000227 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000228 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000229 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
230 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
231 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
232 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
233 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
234 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
235 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Chris Lattnera800f7c2010-01-25 18:33:40 +0000236 OS << "\t.type " << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000237 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
238 switch (Attribute) {
239 default: assert(0 && "Unknown ELF .type");
240 case MCSA_ELF_TypeFunction: OS << "function"; break;
241 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
242 case MCSA_ELF_TypeObject: OS << "object"; break;
243 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
244 case MCSA_ELF_TypeCommon: OS << "common"; break;
245 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
246 }
247 EmitEOL();
248 return;
249 case MCSA_Global: // .globl/.global
250 OS << MAI.getGlobalDirective();
251 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000252 case MCSA_Hidden: OS << ".hidden "; break;
253 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
254 case MCSA_Internal: OS << ".internal "; break;
255 case MCSA_LazyReference: OS << ".lazy_reference "; break;
256 case MCSA_Local: OS << ".local "; break;
257 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
258 case MCSA_PrivateExtern: OS << ".private_extern "; break;
259 case MCSA_Protected: OS << ".protected "; break;
260 case MCSA_Reference: OS << ".reference "; break;
261 case MCSA_Weak: OS << ".weak "; break;
262 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
263 // .weak_reference
264 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000265 }
266
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000267 OS << *Symbol;
268 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000269}
270
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000271void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000272 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
273 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000274}
275
Chris Lattner99328ad2010-01-25 07:52:13 +0000276void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
277 assert(MAI.hasDotTypeDotSizeDirective());
278 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
279}
280
Chris Lattner9eb158d2010-01-23 07:47:02 +0000281void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000282 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000283 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000284 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000285 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000286 OS << ',' << ByteAlignment;
287 else
288 OS << ',' << Log2_32(ByteAlignment);
289 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000290 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000291}
292
Chris Lattner9eb158d2010-01-23 07:47:02 +0000293/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
294///
295/// @param Symbol - The common symbol to emit.
296/// @param Size - The size of the common symbol.
297void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
298 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
299 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
300 EmitEOL();
301}
302
Daniel Dunbar8751b942009-08-28 05:48:22 +0000303void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000304 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000305 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000306 OS << ".zerofill ";
307
308 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000309 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000310 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000311
Chris Lattner9be3fee2009-07-10 22:20:30 +0000312 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000313 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000314 if (ByteAlignment != 0)
315 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000316 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000317 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000318}
319
Chris Lattner12e555c2010-01-23 00:15:00 +0000320static inline char toOctal(int X) { return (X&7)+'0'; }
321
Chris Lattnera6594fc2010-01-25 18:58:59 +0000322static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
323 OS << '"';
324
325 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
326 unsigned char C = Data[i];
327 if (C == '"' || C == '\\') {
328 OS << '\\' << (char)C;
329 continue;
330 }
331
332 if (isprint((unsigned char)C)) {
333 OS << (char)C;
334 continue;
335 }
336
337 switch (C) {
338 case '\b': OS << "\\b"; break;
339 case '\f': OS << "\\f"; break;
340 case '\n': OS << "\\n"; break;
341 case '\r': OS << "\\r"; break;
342 case '\t': OS << "\\t"; break;
343 default:
344 OS << '\\';
345 OS << toOctal(C >> 6);
346 OS << toOctal(C >> 3);
347 OS << toOctal(C >> 0);
348 break;
349 }
350 }
351
352 OS << '"';
353}
354
355
Chris Lattneraaec2052010-01-19 19:46:13 +0000356void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000357 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000358 if (Data.empty()) return;
359
360 if (Data.size() == 1) {
361 OS << MAI.getData8bitsDirective(AddrSpace);
362 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000363 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000364 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000365 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000366
367 // If the data ends with 0 and the target supports .asciz, use it, otherwise
368 // use .ascii
369 if (MAI.getAscizDirective() && Data.back() == 0) {
370 OS << MAI.getAscizDirective();
371 Data = Data.substr(0, Data.size()-1);
372 } else {
373 OS << MAI.getAsciiDirective();
374 }
375
Chris Lattnera6594fc2010-01-25 18:58:59 +0000376 OS << ' ';
377 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000378 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000379}
380
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000381/// EmitIntValue - Special case of EmitValue that avoids the client having
382/// to pass in a MCExpr for constant integers.
383void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
384 unsigned AddrSpace) {
385 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000386 const char *Directive = 0;
387 switch (Size) {
388 default: break;
389 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
390 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
391 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000392 case 8:
393 Directive = MAI.getData64bitsDirective(AddrSpace);
394 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
395 if (Directive) break;
396 if (isLittleEndian()) {
397 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
398 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
399 } else {
400 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
401 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
402 }
403 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000404 }
405
406 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000407 OS << Directive << truncateToSize(Value, Size);
408 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000409}
410
Chris Lattneraaec2052010-01-19 19:46:13 +0000411void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
412 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000413 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000414 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000415 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000416 default: break;
417 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
418 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
419 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
420 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000421 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000422
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000423 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000424 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000425 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000426}
427
Chris Lattner718fb592010-01-25 21:28:50 +0000428void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
429 assert(MAI.getGPRel32Directive() != 0);
430 OS << MAI.getGPRel32Directive() << *Value;
431 EmitEOL();
432}
433
434
Chris Lattner6113b3d2010-01-19 18:52:28 +0000435/// EmitFill - Emit NumBytes bytes worth of the value specified by
436/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000437void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
438 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000439 if (NumBytes == 0) return;
440
Chris Lattneraaec2052010-01-19 19:46:13 +0000441 if (AddrSpace == 0)
442 if (const char *ZeroDirective = MAI.getZeroDirective()) {
443 OS << ZeroDirective << NumBytes;
444 if (FillValue != 0)
445 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000446 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000447 return;
448 }
449
450 // Emit a byte at a time.
451 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000452}
453
Daniel Dunbar84a29262009-06-24 19:25:34 +0000454void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
455 unsigned ValueSize,
456 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000457 // Some assemblers don't support non-power of two alignments, so we always
458 // emit alignments as a power of two if possible.
459 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000460 switch (ValueSize) {
461 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000462 case 1: OS << MAI.getAlignDirective(); break;
463 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000464 case 2: OS << ".p2alignw "; break;
465 case 4: OS << ".p2alignl "; break;
466 case 8: llvm_unreachable("Unsupported alignment size!");
467 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000468
Chris Lattner33adcfb2009-08-22 21:43:10 +0000469 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000470 OS << ByteAlignment;
471 else
472 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000473
Chris Lattner663c2d22009-08-19 06:12:02 +0000474 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000475 OS << ", 0x";
476 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000477
478 if (MaxBytesToEmit)
479 OS << ", " << MaxBytesToEmit;
480 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000481 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000482 return;
483 }
484
485 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000486 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000487 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000488 default: llvm_unreachable("Invalid size for machine code value!");
489 case 1: OS << ".balign"; break;
490 case 2: OS << ".balignw"; break;
491 case 4: OS << ".balignl"; break;
492 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000493 }
494
Chris Lattner663c2d22009-08-19 06:12:02 +0000495 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000496 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000497 if (MaxBytesToEmit)
498 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000499 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000500}
501
Daniel Dunbar821e3332009-08-31 08:09:28 +0000502void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000503 unsigned char Value) {
504 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000505 OS << ".org " << *Offset << ", " << (unsigned) Value;
506 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000507}
508
Chris Lattnera6594fc2010-01-25 18:58:59 +0000509
510void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
511 assert(MAI.hasSingleParameterDotFile());
512 OS << "\t.file\t";
513 PrintQuotedString(Filename, OS);
514 EmitEOL();
515}
516
517void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
518 OS << "\t.file\t" << FileNo << ' ';
519 PrintQuotedString(Filename, OS);
520 EmitEOL();
521}
522
523
Daniel Dunbara11af532009-06-24 01:03:06 +0000524void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000525 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000526
527 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000528 if (InstPrinter) {
529 InstPrinter->printInst(&Inst);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000530 EmitEOL();
Daniel Dunbara356aea2009-08-27 07:58:57 +0000531
532 // Show the encoding if we have a code emitter.
533 if (Emitter) {
534 SmallString<256> Code;
535 raw_svector_ostream VecOS(Code);
536 Emitter->EncodeInstruction(Inst, VecOS);
537 VecOS.flush();
538
539 OS.indent(20);
540 OS << " # encoding: [";
541 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
542 if (i)
543 OS << ',';
544 OS << format("%#04x", uint8_t(Code[i]));
545 }
546 OS << "]\n";
547 }
548
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000549 return;
550 }
551
552 // Otherwise fall back to a structural printing for now. Eventually we should
553 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000554 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000555 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000556}
557
558void MCAsmStreamer::Finish() {
559 OS.flush();
560}
561
Chris Lattner86e22112010-01-22 07:29:22 +0000562MCStreamer *llvm::createAsmStreamer(MCContext &Context,
563 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000564 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000565 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000566 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000567 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
568 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000569}