blob: bf39239e7a7b86acb786aaeb7d7aeadca17a95e6 [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 Lattner9eb158d2010-01-23 07:47:02 +000096 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000097 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000098
Chris Lattner9eb158d2010-01-23 07:47:02 +000099 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
100 ///
101 /// @param Symbol - The common symbol to emit.
102 /// @param Size - The size of the common symbol.
103 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
104
Daniel Dunbar8751b942009-08-28 05:48:22 +0000105 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000106 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000107
Chris Lattneraaec2052010-01-19 19:46:13 +0000108 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000109
Chris Lattneraaec2052010-01-19 19:46:13 +0000110 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000111 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
112
Chris Lattneraaec2052010-01-19 19:46:13 +0000113 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
114 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000115
Chris Lattner46a947d2009-08-17 04:17:34 +0000116 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
117 unsigned ValueSize = 1,
118 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000119
Daniel Dunbar821e3332009-08-31 08:09:28 +0000120 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000121 unsigned char Value = 0);
122
123 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000124
Chris Lattner46a947d2009-08-17 04:17:34 +0000125 virtual void Finish();
126
127 /// @}
128};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000129
Chris Lattner46a947d2009-08-17 04:17:34 +0000130} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000131
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000132/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000133/// file if applicable as a QoI issue to make the output of the compiler
134/// more readable. This only affects the MCAsmStreamer, and only when
135/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000136void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000137 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000138
139 // Make sure that CommentStream is flushed.
140 CommentStream.flush();
141
Chris Lattner86e22112010-01-22 07:29:22 +0000142 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000143 // Each comment goes on its own line.
144 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000145
146 // Tell the comment stream that the vector changed underneath it.
147 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000148}
149
150void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000151 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
152 OS << '\n';
153 return;
154 }
155
156 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000157 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000158
159 assert(Comments.back() == '\n' &&
160 "Comment array not newline terminated");
161 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000162 // Emit a line of comments.
163 OS.PadToColumn(MAI.getCommentColumn());
164 size_t Position = Comments.find('\n');
165 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
166
Chris Lattner86e22112010-01-22 07:29:22 +0000167 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000168 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000169
Chris Lattner14ca1772010-01-22 21:16:10 +0000170 CommentToEmit.clear();
171 // Tell the comment stream that the vector changed underneath it.
172 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000173}
174
175
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000176static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
177 assert(Bytes && "Invalid size!");
178 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
179}
180
Daniel Dunbar821e3332009-08-31 08:09:28 +0000181static inline const MCExpr *truncateToSize(const MCExpr *Value,
182 unsigned Bytes) {
183 // FIXME: Do we really need this routine?
184 return Value;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000185}
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");
229 case MCSA_Global: OS << MAI.getGlobalDirective(); break; // .globl
230 case MCSA_Hidden: OS << ".hidden "; break;
231 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
232 case MCSA_Internal: OS << ".internal "; break;
233 case MCSA_LazyReference: OS << ".lazy_reference "; break;
234 case MCSA_Local: OS << ".local "; break;
235 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
236 case MCSA_PrivateExtern: OS << ".private_extern "; break;
237 case MCSA_Protected: OS << ".protected "; break;
238 case MCSA_Reference: OS << ".reference "; break;
239 case MCSA_Weak: OS << ".weak "; break;
240 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
241 // .weak_reference
242 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000243 }
244
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000245 OS << *Symbol;
246 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000247}
248
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000249void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000250 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
251 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000252}
253
Chris Lattner9eb158d2010-01-23 07:47:02 +0000254void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000255 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000256 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner4ed54382010-01-19 06:01:04 +0000257 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
258 if (MAI.getAlignmentIsInBytes())
259 OS << ',' << ByteAlignment;
260 else
261 OS << ',' << Log2_32(ByteAlignment);
262 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000263 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000264}
265
Chris Lattner9eb158d2010-01-23 07:47:02 +0000266/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
267///
268/// @param Symbol - The common symbol to emit.
269/// @param Size - The size of the common symbol.
270void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
271 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
272 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
273 EmitEOL();
274}
275
Daniel Dunbar8751b942009-08-28 05:48:22 +0000276void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000277 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000278 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000279 OS << ".zerofill ";
280
281 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000282 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000283 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000284
Chris Lattner9be3fee2009-07-10 22:20:30 +0000285 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000286 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000287 if (ByteAlignment != 0)
288 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000289 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000290 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000291}
292
Chris Lattner12e555c2010-01-23 00:15:00 +0000293static inline char toOctal(int X) { return (X&7)+'0'; }
294
Chris Lattneraaec2052010-01-19 19:46:13 +0000295void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000296 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000297 if (Data.empty()) return;
298
299 if (Data.size() == 1) {
300 OS << MAI.getData8bitsDirective(AddrSpace);
301 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000302 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000303 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000304 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000305
306 // If the data ends with 0 and the target supports .asciz, use it, otherwise
307 // use .ascii
308 if (MAI.getAscizDirective() && Data.back() == 0) {
309 OS << MAI.getAscizDirective();
310 Data = Data.substr(0, Data.size()-1);
311 } else {
312 OS << MAI.getAsciiDirective();
313 }
314
315 OS << " \"";
316 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
317 unsigned char C = Data[i];
318 if (C == '"' || C == '\\') {
319 OS << '\\' << (char)C;
320 continue;
321 }
322
323 if (isprint((unsigned char)C)) {
324 OS << (char)C;
325 continue;
326 }
327
328 switch (C) {
329 case '\b': OS << "\\b"; break;
330 case '\f': OS << "\\f"; break;
331 case '\n': OS << "\\n"; break;
332 case '\r': OS << "\\r"; break;
333 case '\t': OS << "\\t"; break;
334 default:
335 OS << '\\';
336 OS << toOctal(C >> 6);
337 OS << toOctal(C >> 3);
338 OS << toOctal(C >> 0);
339 break;
340 }
341 }
342 OS << '"';
343 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000344}
345
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000346/// EmitIntValue - Special case of EmitValue that avoids the client having
347/// to pass in a MCExpr for constant integers.
348void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
349 unsigned AddrSpace) {
350 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000351 const char *Directive = 0;
352 switch (Size) {
353 default: break;
354 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
355 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
356 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000357 case 8:
358 Directive = MAI.getData64bitsDirective(AddrSpace);
359 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
360 if (Directive) break;
361 if (isLittleEndian()) {
362 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
363 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
364 } else {
365 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
366 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
367 }
368 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000369 }
370
371 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000372 OS << Directive << truncateToSize(Value, Size);
373 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000374}
375
Chris Lattneraaec2052010-01-19 19:46:13 +0000376void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
377 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000378 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000379 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000380 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000381 default: break;
382 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
383 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
384 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
385 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000386 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000387
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000388 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000389 OS << Directive << *truncateToSize(Value, Size);
390 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000391}
392
Chris Lattner6113b3d2010-01-19 18:52:28 +0000393/// EmitFill - Emit NumBytes bytes worth of the value specified by
394/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000395void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
396 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000397 if (NumBytes == 0) return;
398
Chris Lattneraaec2052010-01-19 19:46:13 +0000399 if (AddrSpace == 0)
400 if (const char *ZeroDirective = MAI.getZeroDirective()) {
401 OS << ZeroDirective << NumBytes;
402 if (FillValue != 0)
403 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000404 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000405 return;
406 }
407
408 // Emit a byte at a time.
409 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000410}
411
Daniel Dunbar84a29262009-06-24 19:25:34 +0000412void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
413 unsigned ValueSize,
414 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000415 // Some assemblers don't support non-power of two alignments, so we always
416 // emit alignments as a power of two if possible.
417 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000418 switch (ValueSize) {
419 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000420 case 1: OS << MAI.getAlignDirective(); break;
421 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000422 case 2: OS << ".p2alignw "; break;
423 case 4: OS << ".p2alignl "; break;
424 case 8: llvm_unreachable("Unsupported alignment size!");
425 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000426
Chris Lattner33adcfb2009-08-22 21:43:10 +0000427 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000428 OS << ByteAlignment;
429 else
430 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000431
Chris Lattner663c2d22009-08-19 06:12:02 +0000432 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000433 OS << ", 0x";
434 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000435
436 if (MaxBytesToEmit)
437 OS << ", " << MaxBytesToEmit;
438 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000439 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000440 return;
441 }
442
443 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000444 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000445 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000446 default: llvm_unreachable("Invalid size for machine code value!");
447 case 1: OS << ".balign"; break;
448 case 2: OS << ".balignw"; break;
449 case 4: OS << ".balignl"; break;
450 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000451 }
452
Chris Lattner663c2d22009-08-19 06:12:02 +0000453 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000454 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000455 if (MaxBytesToEmit)
456 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000457 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000458}
459
Daniel Dunbar821e3332009-08-31 08:09:28 +0000460void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000461 unsigned char Value) {
462 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000463 OS << ".org " << *Offset << ", " << (unsigned) Value;
464 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000465}
466
Daniel Dunbara11af532009-06-24 01:03:06 +0000467void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000468 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000469
470 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000471 if (InstPrinter) {
472 InstPrinter->printInst(&Inst);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000473 EmitEOL();
Daniel Dunbara356aea2009-08-27 07:58:57 +0000474
475 // Show the encoding if we have a code emitter.
476 if (Emitter) {
477 SmallString<256> Code;
478 raw_svector_ostream VecOS(Code);
479 Emitter->EncodeInstruction(Inst, VecOS);
480 VecOS.flush();
481
482 OS.indent(20);
483 OS << " # encoding: [";
484 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
485 if (i)
486 OS << ',';
487 OS << format("%#04x", uint8_t(Code[i]));
488 }
489 OS << "]\n";
490 }
491
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000492 return;
493 }
494
495 // Otherwise fall back to a structural printing for now. Eventually we should
496 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000497 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000498 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000499}
500
501void MCAsmStreamer::Finish() {
502 OS.flush();
503}
504
Chris Lattner86e22112010-01-22 07:29:22 +0000505MCStreamer *llvm::createAsmStreamer(MCContext &Context,
506 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000507 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000508 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000509 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000510 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
511 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000512}