blob: ded2b42bd8c1abad2cab888741814dc8e39acef8 [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 Lattner46a947d2009-08-17 04:17:34 +000096 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000097 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000098
Daniel Dunbar8751b942009-08-28 05:48:22 +000099 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000100 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000101
Chris Lattneraaec2052010-01-19 19:46:13 +0000102 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000103
Chris Lattneraaec2052010-01-19 19:46:13 +0000104 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000105 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
106
Chris Lattneraaec2052010-01-19 19:46:13 +0000107 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
108 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000109
Chris Lattner46a947d2009-08-17 04:17:34 +0000110 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
111 unsigned ValueSize = 1,
112 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000113
Daniel Dunbar821e3332009-08-31 08:09:28 +0000114 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000115 unsigned char Value = 0);
116
117 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118
Chris Lattner46a947d2009-08-17 04:17:34 +0000119 virtual void Finish();
120
121 /// @}
122};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000123
Chris Lattner46a947d2009-08-17 04:17:34 +0000124} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000125
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000126/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000127/// file if applicable as a QoI issue to make the output of the compiler
128/// more readable. This only affects the MCAsmStreamer, and only when
129/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000130void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000131 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000132
133 // Make sure that CommentStream is flushed.
134 CommentStream.flush();
135
Chris Lattner86e22112010-01-22 07:29:22 +0000136 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000137 // Each comment goes on its own line.
138 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000139
140 // Tell the comment stream that the vector changed underneath it.
141 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000142}
143
144void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000145 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
146 OS << '\n';
147 return;
148 }
149
150 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000151 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000152
153 assert(Comments.back() == '\n' &&
154 "Comment array not newline terminated");
155 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000156 // Emit a line of comments.
157 OS.PadToColumn(MAI.getCommentColumn());
158 size_t Position = Comments.find('\n');
159 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
160
Chris Lattner86e22112010-01-22 07:29:22 +0000161 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000162 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000163
Chris Lattner14ca1772010-01-22 21:16:10 +0000164 CommentToEmit.clear();
165 // Tell the comment stream that the vector changed underneath it.
166 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000167}
168
169
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000170static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
171 assert(Bytes && "Invalid size!");
172 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
173}
174
Daniel Dunbar821e3332009-08-31 08:09:28 +0000175static inline const MCExpr *truncateToSize(const MCExpr *Value,
176 unsigned Bytes) {
177 // FIXME: Do we really need this routine?
178 return Value;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000179}
180
Chris Lattner975780b2009-08-17 05:49:08 +0000181void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000182 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000183 if (Section != CurSection) {
184 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000185 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000186 }
187}
188
189void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000190 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000191 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000192
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000193 OS << *Symbol << ":";
194 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000195 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000196}
197
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000198void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000199 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000200 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000201 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000202 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000203 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000204}
205
Daniel Dunbar821e3332009-08-31 08:09:28 +0000206void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000207 // Only absolute symbols can be redefined.
208 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
209 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000210
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000211 OS << *Symbol << " = " << *Value;
212 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000213
214 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000215 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000216 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000217}
218
Daniel Dunbarfffff912009-10-16 01:34:54 +0000219void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000220 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000221 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000222 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
223 case MCSA_Global: OS << MAI.getGlobalDirective(); break; // .globl
224 case MCSA_Hidden: OS << ".hidden "; break;
225 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
226 case MCSA_Internal: OS << ".internal "; break;
227 case MCSA_LazyReference: OS << ".lazy_reference "; break;
228 case MCSA_Local: OS << ".local "; break;
229 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
230 case MCSA_PrivateExtern: OS << ".private_extern "; break;
231 case MCSA_Protected: OS << ".protected "; break;
232 case MCSA_Reference: OS << ".reference "; break;
233 case MCSA_Weak: OS << ".weak "; break;
234 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
235 // .weak_reference
236 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000237 }
238
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000239 OS << *Symbol;
240 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000241}
242
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000243void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000244 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
245 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000246}
247
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000248void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000249 unsigned ByteAlignment) {
Chris Lattner4ed54382010-01-19 06:01:04 +0000250 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
251 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
252 if (MAI.getAlignmentIsInBytes())
253 OS << ',' << ByteAlignment;
254 else
255 OS << ',' << Log2_32(ByteAlignment);
256 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000257 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000258}
259
Daniel Dunbar8751b942009-08-28 05:48:22 +0000260void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000261 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000262 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000263 OS << ".zerofill ";
264
265 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000266 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000267 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000268
Chris Lattner9be3fee2009-07-10 22:20:30 +0000269 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000270 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000271 if (ByteAlignment != 0)
272 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000273 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000274 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000275}
276
Chris Lattner12e555c2010-01-23 00:15:00 +0000277static inline char toOctal(int X) { return (X&7)+'0'; }
278
Chris Lattneraaec2052010-01-19 19:46:13 +0000279void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000280 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000281 if (Data.empty()) return;
282
283 if (Data.size() == 1) {
284 OS << MAI.getData8bitsDirective(AddrSpace);
285 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000286 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000287 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000288 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000289
290 // If the data ends with 0 and the target supports .asciz, use it, otherwise
291 // use .ascii
292 if (MAI.getAscizDirective() && Data.back() == 0) {
293 OS << MAI.getAscizDirective();
294 Data = Data.substr(0, Data.size()-1);
295 } else {
296 OS << MAI.getAsciiDirective();
297 }
298
299 OS << " \"";
300 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
301 unsigned char C = Data[i];
302 if (C == '"' || C == '\\') {
303 OS << '\\' << (char)C;
304 continue;
305 }
306
307 if (isprint((unsigned char)C)) {
308 OS << (char)C;
309 continue;
310 }
311
312 switch (C) {
313 case '\b': OS << "\\b"; break;
314 case '\f': OS << "\\f"; break;
315 case '\n': OS << "\\n"; break;
316 case '\r': OS << "\\r"; break;
317 case '\t': OS << "\\t"; break;
318 default:
319 OS << '\\';
320 OS << toOctal(C >> 6);
321 OS << toOctal(C >> 3);
322 OS << toOctal(C >> 0);
323 break;
324 }
325 }
326 OS << '"';
327 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000328}
329
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000330/// EmitIntValue - Special case of EmitValue that avoids the client having
331/// to pass in a MCExpr for constant integers.
332void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
333 unsigned AddrSpace) {
334 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000335 const char *Directive = 0;
336 switch (Size) {
337 default: break;
338 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
339 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
340 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000341 case 8:
342 Directive = MAI.getData64bitsDirective(AddrSpace);
343 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
344 if (Directive) break;
345 if (isLittleEndian()) {
346 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
347 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
348 } else {
349 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
350 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
351 }
352 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000353 }
354
355 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000356 OS << Directive << truncateToSize(Value, Size);
357 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000358}
359
Chris Lattneraaec2052010-01-19 19:46:13 +0000360void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
361 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000362 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000363 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000364 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000365 default: break;
366 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
367 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
368 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
369 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000370 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000371
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000372 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000373 OS << Directive << *truncateToSize(Value, Size);
374 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000375}
376
Chris Lattner6113b3d2010-01-19 18:52:28 +0000377/// EmitFill - Emit NumBytes bytes worth of the value specified by
378/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000379void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
380 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000381 if (NumBytes == 0) return;
382
Chris Lattneraaec2052010-01-19 19:46:13 +0000383 if (AddrSpace == 0)
384 if (const char *ZeroDirective = MAI.getZeroDirective()) {
385 OS << ZeroDirective << NumBytes;
386 if (FillValue != 0)
387 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000388 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000389 return;
390 }
391
392 // Emit a byte at a time.
393 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000394}
395
Daniel Dunbar84a29262009-06-24 19:25:34 +0000396void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
397 unsigned ValueSize,
398 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000399 // Some assemblers don't support non-power of two alignments, so we always
400 // emit alignments as a power of two if possible.
401 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000402 switch (ValueSize) {
403 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000404 case 1: OS << MAI.getAlignDirective(); break;
405 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000406 case 2: OS << ".p2alignw "; break;
407 case 4: OS << ".p2alignl "; break;
408 case 8: llvm_unreachable("Unsupported alignment size!");
409 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000410
Chris Lattner33adcfb2009-08-22 21:43:10 +0000411 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000412 OS << ByteAlignment;
413 else
414 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000415
Chris Lattner663c2d22009-08-19 06:12:02 +0000416 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000417 OS << ", 0x";
418 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000419
420 if (MaxBytesToEmit)
421 OS << ", " << MaxBytesToEmit;
422 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000423 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000424 return;
425 }
426
427 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000428 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000429 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000430 default: llvm_unreachable("Invalid size for machine code value!");
431 case 1: OS << ".balign"; break;
432 case 2: OS << ".balignw"; break;
433 case 4: OS << ".balignl"; break;
434 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000435 }
436
Chris Lattner663c2d22009-08-19 06:12:02 +0000437 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000438 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000439 if (MaxBytesToEmit)
440 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000441 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000442}
443
Daniel Dunbar821e3332009-08-31 08:09:28 +0000444void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000445 unsigned char Value) {
446 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000447 OS << ".org " << *Offset << ", " << (unsigned) Value;
448 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000449}
450
Daniel Dunbara11af532009-06-24 01:03:06 +0000451void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000452 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000453
454 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000455 if (InstPrinter) {
456 InstPrinter->printInst(&Inst);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000457 EmitEOL();
Daniel Dunbara356aea2009-08-27 07:58:57 +0000458
459 // Show the encoding if we have a code emitter.
460 if (Emitter) {
461 SmallString<256> Code;
462 raw_svector_ostream VecOS(Code);
463 Emitter->EncodeInstruction(Inst, VecOS);
464 VecOS.flush();
465
466 OS.indent(20);
467 OS << " # encoding: [";
468 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
469 if (i)
470 OS << ',';
471 OS << format("%#04x", uint8_t(Code[i]));
472 }
473 OS << "]\n";
474 }
475
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000476 return;
477 }
478
479 // Otherwise fall back to a structural printing for now. Eventually we should
480 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000481 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000482 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000483}
484
485void MCAsmStreamer::Finish() {
486 OS.flush();
487}
488
Chris Lattner86e22112010-01-22 07:29:22 +0000489MCStreamer *llvm::createAsmStreamer(MCContext &Context,
490 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000491 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000492 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000493 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000494 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
495 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000496}