blob: 4eb1bcdfb818d8c154a46f911124d862fce1e032 [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 Lattner46a947d2009-08-17 04:17:34 +000076 /// @name MCStreamer Interface
77 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000078
Chris Lattner975780b2009-08-17 05:49:08 +000079 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000080
Chris Lattner46a947d2009-08-17 04:17:34 +000081 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000082
Chris Lattner46a947d2009-08-17 04:17:34 +000083 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000084
Daniel Dunbar821e3332009-08-31 08:09:28 +000085 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000086
Chris Lattner46a947d2009-08-17 04:17:34 +000087 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000088
Chris Lattner46a947d2009-08-17 04:17:34 +000089 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000090
Chris Lattner46a947d2009-08-17 04:17:34 +000091 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000092 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000093
Daniel Dunbar8751b942009-08-28 05:48:22 +000094 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000095 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000096
Chris Lattneraaec2052010-01-19 19:46:13 +000097 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000098
Chris Lattneraaec2052010-01-19 19:46:13 +000099 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000100 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
101
Chris Lattneraaec2052010-01-19 19:46:13 +0000102 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
103 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000104
Chris Lattner46a947d2009-08-17 04:17:34 +0000105 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
106 unsigned ValueSize = 1,
107 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000108
Daniel Dunbar821e3332009-08-31 08:09:28 +0000109 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000110 unsigned char Value = 0);
111
112 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000113
Chris Lattner46a947d2009-08-17 04:17:34 +0000114 virtual void Finish();
115
116 /// @}
117};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000118
Chris Lattner46a947d2009-08-17 04:17:34 +0000119} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000120
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000121/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000122/// file if applicable as a QoI issue to make the output of the compiler
123/// more readable. This only affects the MCAsmStreamer, and only when
124/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000125void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000126 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000127
128 // Make sure that CommentStream is flushed.
129 CommentStream.flush();
130
Chris Lattner86e22112010-01-22 07:29:22 +0000131 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000132 // Each comment goes on its own line.
133 CommentToEmit.push_back('\n');
Chris Lattner86e22112010-01-22 07:29:22 +0000134}
135
136void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000137 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
138 OS << '\n';
139 return;
140 }
141
142 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000143 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000144
145 assert(Comments.back() == '\n' &&
146 "Comment array not newline terminated");
147 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000148 // Emit a line of comments.
149 OS.PadToColumn(MAI.getCommentColumn());
150 size_t Position = Comments.find('\n');
151 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
152
Chris Lattner86e22112010-01-22 07:29:22 +0000153 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000154 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000155
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000156 CommentStream.clear();
Chris Lattner86e22112010-01-22 07:29:22 +0000157}
158
159
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000160static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
161 assert(Bytes && "Invalid size!");
162 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
163}
164
Daniel Dunbar821e3332009-08-31 08:09:28 +0000165static inline const MCExpr *truncateToSize(const MCExpr *Value,
166 unsigned Bytes) {
167 // FIXME: Do we really need this routine?
168 return Value;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000169}
170
Chris Lattner975780b2009-08-17 05:49:08 +0000171void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000172 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000173 if (Section != CurSection) {
174 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000175 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000176 }
177}
178
179void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000180 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000181 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000182
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000183 OS << *Symbol << ":";
184 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000185 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000186}
187
Kevin Enderbyf96db462009-07-16 17:56:39 +0000188void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
189 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000190 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000191 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
192 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000193 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000194}
195
Daniel Dunbar821e3332009-08-31 08:09:28 +0000196void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000197 // Only absolute symbols can be redefined.
198 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
199 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000200
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000201 OS << *Symbol << " = " << *Value;
202 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000203
204 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000205 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000206 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000207}
208
Daniel Dunbarfffff912009-10-16 01:34:54 +0000209void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Daniel Dunbara11af532009-06-24 01:03:06 +0000210 SymbolAttr Attribute) {
211 switch (Attribute) {
Chris Lattner32f6a8d2010-01-20 17:50:30 +0000212 case Global: OS << MAI.getGlobalDirective(); break; // .globl
213 case Hidden: OS << ".hidden "; break;
214 case IndirectSymbol: OS << ".indirect_symbol "; break;
215 case Internal: OS << ".internal "; break;
216 case LazyReference: OS << ".lazy_reference "; break;
217 case NoDeadStrip: OS << ".no_dead_strip "; break;
218 case PrivateExtern: OS << ".private_extern "; break;
219 case Protected: OS << ".protected "; break;
220 case Reference: OS << ".reference "; break;
221 case Weak: OS << ".weak "; break;
222 case WeakDefinition: OS << ".weak_definition "; break;
223 case WeakReference: OS << ".weak_reference "; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000224 }
225
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000226 OS << *Symbol;
227 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000228}
229
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000230void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000231 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
232 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000233}
234
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000235void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000236 unsigned ByteAlignment) {
Chris Lattner4ed54382010-01-19 06:01:04 +0000237 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
238 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
239 if (MAI.getAlignmentIsInBytes())
240 OS << ',' << ByteAlignment;
241 else
242 OS << ',' << Log2_32(ByteAlignment);
243 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000244 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000245}
246
Daniel Dunbar8751b942009-08-28 05:48:22 +0000247void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000248 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000249 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000250 OS << ".zerofill ";
251
252 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000253 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000254 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000255
Chris Lattner9be3fee2009-07-10 22:20:30 +0000256 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000257 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000258 if (ByteAlignment != 0)
259 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000260 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000261 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000262}
263
Chris Lattneraaec2052010-01-19 19:46:13 +0000264void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000265 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattneraaec2052010-01-19 19:46:13 +0000266 const char *Directive = MAI.getData8bitsDirective(AddrSpace);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000267 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
268 OS << Directive << (unsigned)(unsigned char)Data[i];
269 EmitEOL();
270 }
Daniel Dunbara11af532009-06-24 01:03:06 +0000271}
272
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000273/// EmitIntValue - Special case of EmitValue that avoids the client having
274/// to pass in a MCExpr for constant integers.
275void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
276 unsigned AddrSpace) {
277 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000278 const char *Directive = 0;
279 switch (Size) {
280 default: break;
281 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
282 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
283 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000284 case 8:
285 Directive = MAI.getData64bitsDirective(AddrSpace);
286 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
287 if (Directive) break;
288 if (isLittleEndian()) {
289 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
290 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
291 } else {
292 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
293 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
294 }
295 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000296 }
297
298 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000299 OS << Directive << truncateToSize(Value, Size);
300 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000301}
302
Chris Lattneraaec2052010-01-19 19:46:13 +0000303void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
304 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000305 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000306 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000307 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000308 default: break;
309 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
310 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
311 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
312 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000313 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000314
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000315 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000316 OS << Directive << *truncateToSize(Value, Size);
317 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000318}
319
Chris Lattner6113b3d2010-01-19 18:52:28 +0000320/// EmitFill - Emit NumBytes bytes worth of the value specified by
321/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000322void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
323 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000324 if (NumBytes == 0) return;
325
Chris Lattneraaec2052010-01-19 19:46:13 +0000326 if (AddrSpace == 0)
327 if (const char *ZeroDirective = MAI.getZeroDirective()) {
328 OS << ZeroDirective << NumBytes;
329 if (FillValue != 0)
330 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000331 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000332 return;
333 }
334
335 // Emit a byte at a time.
336 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000337}
338
Daniel Dunbar84a29262009-06-24 19:25:34 +0000339void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
340 unsigned ValueSize,
341 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000342 // Some assemblers don't support non-power of two alignments, so we always
343 // emit alignments as a power of two if possible.
344 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000345 switch (ValueSize) {
346 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000347 case 1: OS << MAI.getAlignDirective(); break;
348 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000349 case 2: OS << ".p2alignw "; break;
350 case 4: OS << ".p2alignl "; break;
351 case 8: llvm_unreachable("Unsupported alignment size!");
352 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000353
Chris Lattner33adcfb2009-08-22 21:43:10 +0000354 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000355 OS << ByteAlignment;
356 else
357 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000358
Chris Lattner663c2d22009-08-19 06:12:02 +0000359 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000360 OS << ", 0x";
361 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000362
363 if (MaxBytesToEmit)
364 OS << ", " << MaxBytesToEmit;
365 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000366 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000367 return;
368 }
369
370 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000371 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000372 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000373 default: llvm_unreachable("Invalid size for machine code value!");
374 case 1: OS << ".balign"; break;
375 case 2: OS << ".balignw"; break;
376 case 4: OS << ".balignl"; break;
377 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000378 }
379
Chris Lattner663c2d22009-08-19 06:12:02 +0000380 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000381 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000382 if (MaxBytesToEmit)
383 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000384 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000385}
386
Daniel Dunbar821e3332009-08-31 08:09:28 +0000387void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000388 unsigned char Value) {
389 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000390 OS << ".org " << *Offset << ", " << (unsigned) Value;
391 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000392}
393
Daniel Dunbara11af532009-06-24 01:03:06 +0000394void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000395 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000396
397 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000398 if (InstPrinter) {
399 InstPrinter->printInst(&Inst);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000400 EmitEOL();
Daniel Dunbara356aea2009-08-27 07:58:57 +0000401
402 // Show the encoding if we have a code emitter.
403 if (Emitter) {
404 SmallString<256> Code;
405 raw_svector_ostream VecOS(Code);
406 Emitter->EncodeInstruction(Inst, VecOS);
407 VecOS.flush();
408
409 OS.indent(20);
410 OS << " # encoding: [";
411 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
412 if (i)
413 OS << ',';
414 OS << format("%#04x", uint8_t(Code[i]));
415 }
416 OS << "]\n";
417 }
418
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000419 return;
420 }
421
422 // Otherwise fall back to a structural printing for now. Eventually we should
423 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000424 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000425 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000426}
427
428void MCAsmStreamer::Finish() {
429 OS.flush();
430}
431
Chris Lattner86e22112010-01-22 07:29:22 +0000432MCStreamer *llvm::createAsmStreamer(MCContext &Context,
433 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000434 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000435 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000436 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000437 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
438 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000439}