blob: 82d7fdbdf9429cdb37b4fa3636e6d02f0b3163c1 [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 Lattner46a947d2009-08-17 04:17:34 +000037public:
Chris Lattner86e22112010-01-22 07:29:22 +000038 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
39 const MCAsmInfo &mai,
Chris Lattner07404412010-01-22 07:06:15 +000040 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
41 MCCodeEmitter *emitter)
42 : MCStreamer(Context), OS(os), MAI(mai), IsLittleEndian(isLittleEndian),
43 IsVerboseAsm(isVerboseAsm), InstPrinter(printer), Emitter(emitter) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000044 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000045
Chris Lattner16582022010-01-20 06:39:07 +000046 bool isLittleEndian() const { return IsLittleEndian; }
47
Chris Lattner86e22112010-01-22 07:29:22 +000048
49 inline void EmitEOL() {
50 if (CommentToEmit.empty()) {
51 OS << '\n';
52 return;
53 }
54 EmitCommentsAndEOL();
55 }
56 void EmitCommentsAndEOL();
57
58 /// addComment - Add a comment that can be emitted to the generated .s
59 /// file if applicable as a QoI issue to make the output of the compiler
60 /// more readable. This only affects the MCAsmStreamer, and only when
61 /// verbose assembly output is enabled.
62 virtual void addComment(const Twine &T);
63
Chris Lattner46a947d2009-08-17 04:17:34 +000064 /// @name MCStreamer Interface
65 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000066
Chris Lattner975780b2009-08-17 05:49:08 +000067 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000068
Chris Lattner46a947d2009-08-17 04:17:34 +000069 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000070
Chris Lattner46a947d2009-08-17 04:17:34 +000071 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000072
Daniel Dunbar821e3332009-08-31 08:09:28 +000073 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000074
Chris Lattner46a947d2009-08-17 04:17:34 +000075 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000076
Chris Lattner46a947d2009-08-17 04:17:34 +000077 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000078
Chris Lattner46a947d2009-08-17 04:17:34 +000079 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000080 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000081
Daniel Dunbar8751b942009-08-28 05:48:22 +000082 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000083 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000084
Chris Lattneraaec2052010-01-19 19:46:13 +000085 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000086
Chris Lattneraaec2052010-01-19 19:46:13 +000087 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +000088 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
89
Chris Lattneraaec2052010-01-19 19:46:13 +000090 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
91 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +000092
Chris Lattner46a947d2009-08-17 04:17:34 +000093 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
94 unsigned ValueSize = 1,
95 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000096
Daniel Dunbar821e3332009-08-31 08:09:28 +000097 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +000098 unsigned char Value = 0);
99
100 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000101
Chris Lattner46a947d2009-08-17 04:17:34 +0000102 virtual void Finish();
103
104 /// @}
105};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000106
Chris Lattner46a947d2009-08-17 04:17:34 +0000107} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000108
Chris Lattner86e22112010-01-22 07:29:22 +0000109/// addComment - Add a comment that can be emitted to the generated .s
110/// file if applicable as a QoI issue to make the output of the compiler
111/// more readable. This only affects the MCAsmStreamer, and only when
112/// verbose assembly output is enabled.
113void MCAsmStreamer::addComment(const Twine &T) {
114 if (!IsVerboseAsm) return;
115 // Each comment goes on its own line.
116 if (!CommentToEmit.empty())
117 CommentToEmit.push_back('\n');
118 T.toVector(CommentToEmit);
119}
120
121void MCAsmStreamer::EmitCommentsAndEOL() {
122 StringRef Comments = CommentToEmit.str();
123 while (!Comments.empty()) {
124 // Emit a line of comments.
125 OS.PadToColumn(MAI.getCommentColumn());
126 size_t Position = Comments.find('\n');
127 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
128
129 if (Position == StringRef::npos) break;
130 Comments = Comments.substr(Position+1);
131 }
132
133 CommentToEmit.clear();
134}
135
136
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000137static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
138 assert(Bytes && "Invalid size!");
139 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
140}
141
Daniel Dunbar821e3332009-08-31 08:09:28 +0000142static inline const MCExpr *truncateToSize(const MCExpr *Value,
143 unsigned Bytes) {
144 // FIXME: Do we really need this routine?
145 return Value;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000146}
147
Chris Lattner975780b2009-08-17 05:49:08 +0000148void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000149 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000150 if (Section != CurSection) {
151 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000152 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000153 }
154}
155
156void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000157 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000158 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000159
Chris Lattner10b318b2010-01-17 21:43:43 +0000160 OS << *Symbol << ":\n";
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000161 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000162}
163
Kevin Enderbyf96db462009-07-16 17:56:39 +0000164void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
165 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000166 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000167 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
168 }
169 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000170}
171
Daniel Dunbar821e3332009-08-31 08:09:28 +0000172void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000173 // Only absolute symbols can be redefined.
174 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
175 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000176
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000177 OS << *Symbol << " = " << *Value << '\n';
Daniel Dunbarfffff912009-10-16 01:34:54 +0000178
179 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000180 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000181 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000182}
183
Daniel Dunbarfffff912009-10-16 01:34:54 +0000184void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Daniel Dunbara11af532009-06-24 01:03:06 +0000185 SymbolAttr Attribute) {
186 switch (Attribute) {
Chris Lattner32f6a8d2010-01-20 17:50:30 +0000187 case Global: OS << MAI.getGlobalDirective(); break; // .globl
188 case Hidden: OS << ".hidden "; break;
189 case IndirectSymbol: OS << ".indirect_symbol "; break;
190 case Internal: OS << ".internal "; break;
191 case LazyReference: OS << ".lazy_reference "; break;
192 case NoDeadStrip: OS << ".no_dead_strip "; break;
193 case PrivateExtern: OS << ".private_extern "; break;
194 case Protected: OS << ".protected "; break;
195 case Reference: OS << ".reference "; break;
196 case Weak: OS << ".weak "; break;
197 case WeakDefinition: OS << ".weak_definition "; break;
198 case WeakReference: OS << ".weak_reference "; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000199 }
200
Chris Lattner32f6a8d2010-01-20 17:50:30 +0000201 OS << *Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000202}
203
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000204void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000205 OS << ".desc" << ' ' << *Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000206}
207
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000208void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000209 unsigned ByteAlignment) {
Chris Lattner4ed54382010-01-19 06:01:04 +0000210 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
211 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
212 if (MAI.getAlignmentIsInBytes())
213 OS << ',' << ByteAlignment;
214 else
215 OS << ',' << Log2_32(ByteAlignment);
216 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000217 OS << '\n';
218}
219
Daniel Dunbar8751b942009-08-28 05:48:22 +0000220void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000221 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000222 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000223 OS << ".zerofill ";
224
225 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000226 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000227 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000228
Chris Lattner9be3fee2009-07-10 22:20:30 +0000229 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000230 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000231 if (ByteAlignment != 0)
232 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000233 }
234 OS << '\n';
235}
236
Chris Lattneraaec2052010-01-19 19:46:13 +0000237void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000238 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattneraaec2052010-01-19 19:46:13 +0000239 const char *Directive = MAI.getData8bitsDirective(AddrSpace);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000240 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +0000241 OS << Directive << (unsigned)(unsigned char)Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000242}
243
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000244/// EmitIntValue - Special case of EmitValue that avoids the client having
245/// to pass in a MCExpr for constant integers.
246void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
247 unsigned AddrSpace) {
248 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000249 const char *Directive = 0;
250 switch (Size) {
251 default: break;
252 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
253 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
254 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000255 case 8:
256 Directive = MAI.getData64bitsDirective(AddrSpace);
257 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
258 if (Directive) break;
259 if (isLittleEndian()) {
260 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
261 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
262 } else {
263 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
264 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
265 }
266 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000267 }
268
269 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000270 OS << Directive << truncateToSize(Value, Size);
271 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000272}
273
Chris Lattneraaec2052010-01-19 19:46:13 +0000274void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
275 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000276 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000277 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000278 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000279 default: break;
280 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
281 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
282 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
283 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000284 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000285
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000286 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000287 OS << Directive << *truncateToSize(Value, Size);
288 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000289}
290
Chris Lattner6113b3d2010-01-19 18:52:28 +0000291/// EmitFill - Emit NumBytes bytes worth of the value specified by
292/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000293void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
294 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000295 if (NumBytes == 0) return;
296
Chris Lattneraaec2052010-01-19 19:46:13 +0000297 if (AddrSpace == 0)
298 if (const char *ZeroDirective = MAI.getZeroDirective()) {
299 OS << ZeroDirective << NumBytes;
300 if (FillValue != 0)
301 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000302 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000303 return;
304 }
305
306 // Emit a byte at a time.
307 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000308}
309
Daniel Dunbar84a29262009-06-24 19:25:34 +0000310void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
311 unsigned ValueSize,
312 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000313 // Some assemblers don't support non-power of two alignments, so we always
314 // emit alignments as a power of two if possible.
315 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000316 switch (ValueSize) {
317 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000318 case 1: OS << MAI.getAlignDirective(); break;
319 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000320 case 2: OS << ".p2alignw "; break;
321 case 4: OS << ".p2alignl "; break;
322 case 8: llvm_unreachable("Unsupported alignment size!");
323 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000324
Chris Lattner33adcfb2009-08-22 21:43:10 +0000325 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000326 OS << ByteAlignment;
327 else
328 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000329
Chris Lattner663c2d22009-08-19 06:12:02 +0000330 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000331 OS << ", 0x";
332 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000333
334 if (MaxBytesToEmit)
335 OS << ", " << MaxBytesToEmit;
336 }
337 OS << '\n';
338 return;
339 }
340
341 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000342 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000343 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000344 default: llvm_unreachable("Invalid size for machine code value!");
345 case 1: OS << ".balign"; break;
346 case 2: OS << ".balignw"; break;
347 case 4: OS << ".balignl"; break;
348 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000349 }
350
Chris Lattner663c2d22009-08-19 06:12:02 +0000351 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000352 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000353 if (MaxBytesToEmit)
354 OS << ", " << MaxBytesToEmit;
355 OS << '\n';
356}
357
Daniel Dunbar821e3332009-08-31 08:09:28 +0000358void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000359 unsigned char Value) {
360 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000361 OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
Daniel Dunbar84a29262009-06-24 19:25:34 +0000362}
363
Daniel Dunbara11af532009-06-24 01:03:06 +0000364void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000365 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000366
367 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000368 if (InstPrinter) {
369 InstPrinter->printInst(&Inst);
Chris Lattner1b6570e2009-09-13 22:24:34 +0000370 OS << '\n';
Daniel Dunbara356aea2009-08-27 07:58:57 +0000371
372 // Show the encoding if we have a code emitter.
373 if (Emitter) {
374 SmallString<256> Code;
375 raw_svector_ostream VecOS(Code);
376 Emitter->EncodeInstruction(Inst, VecOS);
377 VecOS.flush();
378
379 OS.indent(20);
380 OS << " # encoding: [";
381 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
382 if (i)
383 OS << ',';
384 OS << format("%#04x", uint8_t(Code[i]));
385 }
386 OS << "]\n";
387 }
388
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000389 return;
390 }
391
392 // Otherwise fall back to a structural printing for now. Eventually we should
393 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000394 Inst.print(OS, &MAI);
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000395 OS << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000396}
397
398void MCAsmStreamer::Finish() {
399 OS.flush();
400}
401
Chris Lattner86e22112010-01-22 07:29:22 +0000402MCStreamer *llvm::createAsmStreamer(MCContext &Context,
403 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000404 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000405 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000406 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000407 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
408 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000409}