blob: 2c2fb0e8c208f09a095f4747666ac982ff9332d7 [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
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000058 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000059 /// 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.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000062 virtual void AddComment(const Twine &T);
Chris Lattner86e22112010-01-22 07:29:22 +000063
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 Lattnerd32c7cf2010-01-22 18:21:35 +0000109/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000110/// 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.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000113void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000114 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 Lattner7d1e49c2010-01-22 07:36:39 +0000160 OS << *Symbol << ":";
161 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000162 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000163}
164
Kevin Enderbyf96db462009-07-16 17:56:39 +0000165void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
166 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000167 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000168 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
169 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000170 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000171}
172
Daniel Dunbar821e3332009-08-31 08:09:28 +0000173void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000174 // Only absolute symbols can be redefined.
175 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
176 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000177
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000178 OS << *Symbol << " = " << *Value;
179 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000180
181 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000182 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000183 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000184}
185
Daniel Dunbarfffff912009-10-16 01:34:54 +0000186void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Daniel Dunbara11af532009-06-24 01:03:06 +0000187 SymbolAttr Attribute) {
188 switch (Attribute) {
Chris Lattner32f6a8d2010-01-20 17:50:30 +0000189 case Global: OS << MAI.getGlobalDirective(); break; // .globl
190 case Hidden: OS << ".hidden "; break;
191 case IndirectSymbol: OS << ".indirect_symbol "; break;
192 case Internal: OS << ".internal "; break;
193 case LazyReference: OS << ".lazy_reference "; break;
194 case NoDeadStrip: OS << ".no_dead_strip "; break;
195 case PrivateExtern: OS << ".private_extern "; break;
196 case Protected: OS << ".protected "; break;
197 case Reference: OS << ".reference "; break;
198 case Weak: OS << ".weak "; break;
199 case WeakDefinition: OS << ".weak_definition "; break;
200 case WeakReference: OS << ".weak_reference "; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000201 }
202
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000203 OS << *Symbol;
204 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000205}
206
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000207void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000208 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
209 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000210}
211
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000212void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000213 unsigned ByteAlignment) {
Chris Lattner4ed54382010-01-19 06:01:04 +0000214 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
215 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
216 if (MAI.getAlignmentIsInBytes())
217 OS << ',' << ByteAlignment;
218 else
219 OS << ',' << Log2_32(ByteAlignment);
220 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000221 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000222}
223
Daniel Dunbar8751b942009-08-28 05:48:22 +0000224void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000225 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000226 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000227 OS << ".zerofill ";
228
229 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000230 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000231 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000232
Chris Lattner9be3fee2009-07-10 22:20:30 +0000233 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000234 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000235 if (ByteAlignment != 0)
236 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000237 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000238 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000239}
240
Chris Lattneraaec2052010-01-19 19:46:13 +0000241void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000242 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattneraaec2052010-01-19 19:46:13 +0000243 const char *Directive = MAI.getData8bitsDirective(AddrSpace);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000244 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
245 OS << Directive << (unsigned)(unsigned char)Data[i];
246 EmitEOL();
247 }
Daniel Dunbara11af532009-06-24 01:03:06 +0000248}
249
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000250/// EmitIntValue - Special case of EmitValue that avoids the client having
251/// to pass in a MCExpr for constant integers.
252void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
253 unsigned AddrSpace) {
254 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000255 const char *Directive = 0;
256 switch (Size) {
257 default: break;
258 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
259 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
260 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000261 case 8:
262 Directive = MAI.getData64bitsDirective(AddrSpace);
263 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
264 if (Directive) break;
265 if (isLittleEndian()) {
266 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
267 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
268 } else {
269 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
270 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
271 }
272 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000273 }
274
275 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000276 OS << Directive << truncateToSize(Value, Size);
277 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000278}
279
Chris Lattneraaec2052010-01-19 19:46:13 +0000280void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
281 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000282 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000283 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000284 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000285 default: break;
286 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
287 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
288 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
289 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000290 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000291
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000292 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000293 OS << Directive << *truncateToSize(Value, Size);
294 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000295}
296
Chris Lattner6113b3d2010-01-19 18:52:28 +0000297/// EmitFill - Emit NumBytes bytes worth of the value specified by
298/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000299void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
300 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000301 if (NumBytes == 0) return;
302
Chris Lattneraaec2052010-01-19 19:46:13 +0000303 if (AddrSpace == 0)
304 if (const char *ZeroDirective = MAI.getZeroDirective()) {
305 OS << ZeroDirective << NumBytes;
306 if (FillValue != 0)
307 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000308 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000309 return;
310 }
311
312 // Emit a byte at a time.
313 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000314}
315
Daniel Dunbar84a29262009-06-24 19:25:34 +0000316void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
317 unsigned ValueSize,
318 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000319 // Some assemblers don't support non-power of two alignments, so we always
320 // emit alignments as a power of two if possible.
321 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000322 switch (ValueSize) {
323 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000324 case 1: OS << MAI.getAlignDirective(); break;
325 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000326 case 2: OS << ".p2alignw "; break;
327 case 4: OS << ".p2alignl "; break;
328 case 8: llvm_unreachable("Unsupported alignment size!");
329 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000330
Chris Lattner33adcfb2009-08-22 21:43:10 +0000331 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000332 OS << ByteAlignment;
333 else
334 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000335
Chris Lattner663c2d22009-08-19 06:12:02 +0000336 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000337 OS << ", 0x";
338 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000339
340 if (MaxBytesToEmit)
341 OS << ", " << MaxBytesToEmit;
342 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000343 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000344 return;
345 }
346
347 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000348 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000349 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000350 default: llvm_unreachable("Invalid size for machine code value!");
351 case 1: OS << ".balign"; break;
352 case 2: OS << ".balignw"; break;
353 case 4: OS << ".balignl"; break;
354 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000355 }
356
Chris Lattner663c2d22009-08-19 06:12:02 +0000357 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000358 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000359 if (MaxBytesToEmit)
360 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000361 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000362}
363
Daniel Dunbar821e3332009-08-31 08:09:28 +0000364void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000365 unsigned char Value) {
366 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000367 OS << ".org " << *Offset << ", " << (unsigned) Value;
368 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000369}
370
Daniel Dunbara11af532009-06-24 01:03:06 +0000371void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000372 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000373
374 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000375 if (InstPrinter) {
376 InstPrinter->printInst(&Inst);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000377 EmitEOL();
Daniel Dunbara356aea2009-08-27 07:58:57 +0000378
379 // Show the encoding if we have a code emitter.
380 if (Emitter) {
381 SmallString<256> Code;
382 raw_svector_ostream VecOS(Code);
383 Emitter->EncodeInstruction(Inst, VecOS);
384 VecOS.flush();
385
386 OS.indent(20);
387 OS << " # encoding: [";
388 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
389 if (i)
390 OS << ',';
391 OS << format("%#04x", uint8_t(Code[i]));
392 }
393 OS << "]\n";
394 }
395
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000396 return;
397 }
398
399 // Otherwise fall back to a structural printing for now. Eventually we should
400 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000401 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000402 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000403}
404
405void MCAsmStreamer::Finish() {
406 OS.flush();
407}
408
Chris Lattner86e22112010-01-22 07:29:22 +0000409MCStreamer *llvm::createAsmStreamer(MCContext &Context,
410 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000411 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000412 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000413 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000414 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
415 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000416}