blob: 7f3947114fe4bc656c17878b5ce1c7b501215627 [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 Lattner90edac02009-09-14 03:02:37 +000032 MCInstPrinter *InstPrinter;
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000033 MCCodeEmitter *Emitter;
Chris Lattner86e22112010-01-22 07:29:22 +000034
35 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000036 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000037
38 unsigned IsLittleEndian : 1;
39 unsigned IsVerboseAsm : 1;
40 unsigned ShowInst : 1;
41
Chris Lattner46a947d2009-08-17 04:17:34 +000042public:
Chris Lattner86e22112010-01-22 07:29:22 +000043 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Chris Lattner07404412010-01-22 07:06:15 +000044 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
Daniel Dunbar5532cf42010-02-10 01:41:14 +000045 MCCodeEmitter *emitter, bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000046 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
47 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000048 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
Daniel Dunbar5532cf42010-02-10 01:41:14 +000049 ShowInst(showInst) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000050 if (InstPrinter && IsVerboseAsm)
51 InstPrinter->setCommentStream(CommentStream);
52 }
Chris Lattner46a947d2009-08-17 04:17:34 +000053 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000054
Chris Lattner16582022010-01-20 06:39:07 +000055 bool isLittleEndian() const { return IsLittleEndian; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000056
Chris Lattner86e22112010-01-22 07:29:22 +000057 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000058 // If we don't have any comments, just emit a \n.
59 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000060 OS << '\n';
61 return;
62 }
63 EmitCommentsAndEOL();
64 }
65 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000066
67 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
68 /// all.
69 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000070
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000071 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000072 /// file if applicable as a QoI issue to make the output of the compiler
73 /// more readable. This only affects the MCAsmStreamer, and only when
74 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000075 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000076
77 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
78 virtual void AddEncodingComment(const MCInst &Inst);
79
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000080 /// GetCommentOS - Return a raw_ostream that comments can be written to.
81 /// Unlike AddComment, you are required to terminate comments with \n if you
82 /// use this method.
83 virtual raw_ostream &GetCommentOS() {
84 if (!IsVerboseAsm)
85 return nulls(); // Discard comments unless in verbose asm mode.
86 return CommentStream;
87 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000088
Chris Lattner0fd90fd2010-01-22 19:52:01 +000089 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
90 virtual void AddBlankLine() {
91 EmitEOL();
92 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000093
Chris Lattner46a947d2009-08-17 04:17:34 +000094 /// @name MCStreamer Interface
95 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000096
Chris Lattner975780b2009-08-17 05:49:08 +000097 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000098
Chris Lattner46a947d2009-08-17 04:17:34 +000099 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000100
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000101 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +0000102
Daniel Dunbar821e3332009-08-31 08:09:28 +0000103 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000104
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000105 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000106
Chris Lattner46a947d2009-08-17 04:17:34 +0000107 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +0000108
Chris Lattner99328ad2010-01-25 07:52:13 +0000109 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000110 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000111 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000112
Chris Lattner9eb158d2010-01-23 07:47:02 +0000113 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
114 ///
115 /// @param Symbol - The common symbol to emit.
116 /// @param Size - The size of the common symbol.
117 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
118
Daniel Dunbar8751b942009-08-28 05:48:22 +0000119 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000120 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000121
Chris Lattneraaec2052010-01-19 19:46:13 +0000122 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000123
Chris Lattneraaec2052010-01-19 19:46:13 +0000124 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000125 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000126 virtual void EmitGPRel32Value(const MCExpr *Value);
127
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000128
Chris Lattneraaec2052010-01-19 19:46:13 +0000129 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
130 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000131
Chris Lattner46a947d2009-08-17 04:17:34 +0000132 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
133 unsigned ValueSize = 1,
134 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000135
Kevin Enderby6e720482010-02-23 18:26:34 +0000136 virtual void EmitCodeAlignment(unsigned ByteAlignment,
137 unsigned MaxBytesToEmit = 0);
138
Daniel Dunbar821e3332009-08-31 08:09:28 +0000139 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000140 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000141
Chris Lattnera6594fc2010-01-25 18:58:59 +0000142 virtual void EmitFileDirective(StringRef Filename);
143 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
144
145 virtual void EmitInstruction(const MCInst &Inst);
146
Chris Lattner46a947d2009-08-17 04:17:34 +0000147 virtual void Finish();
148
149 /// @}
150};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000151
Chris Lattner46a947d2009-08-17 04:17:34 +0000152} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000153
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000154/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000155/// file if applicable as a QoI issue to make the output of the compiler
156/// more readable. This only affects the MCAsmStreamer, and only when
157/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000158void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000159 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000160
161 // Make sure that CommentStream is flushed.
162 CommentStream.flush();
163
Chris Lattner86e22112010-01-22 07:29:22 +0000164 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000165 // Each comment goes on its own line.
166 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000167
168 // Tell the comment stream that the vector changed underneath it.
169 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000170}
171
172void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000173 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
174 OS << '\n';
175 return;
176 }
177
178 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000179 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000180
181 assert(Comments.back() == '\n' &&
182 "Comment array not newline terminated");
183 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000184 // Emit a line of comments.
185 OS.PadToColumn(MAI.getCommentColumn());
186 size_t Position = Comments.find('\n');
187 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
188
Chris Lattner86e22112010-01-22 07:29:22 +0000189 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000190 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000191
Chris Lattner14ca1772010-01-22 21:16:10 +0000192 CommentToEmit.clear();
193 // Tell the comment stream that the vector changed underneath it.
194 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000195}
196
197
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000198static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
199 assert(Bytes && "Invalid size!");
200 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
201}
202
Chris Lattner975780b2009-08-17 05:49:08 +0000203void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000204 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000205 if (Section != CurSection) {
206 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000207 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000208 }
209}
210
211void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000212 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000213 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000214
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000215 OS << *Symbol << ":";
216 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000217 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000218}
219
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000220void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000221 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000222 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000223 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000224 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000225 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000226}
227
Daniel Dunbar821e3332009-08-31 08:09:28 +0000228void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000229 // Only absolute symbols can be redefined.
230 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
231 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000232
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000233 OS << *Symbol << " = " << *Value;
234 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000235
236 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000237 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000238 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000239}
240
Daniel Dunbarfffff912009-10-16 01:34:54 +0000241void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000242 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000243 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000244 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000245 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
246 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
247 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
248 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
249 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
250 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
251 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000252 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000253 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
254 switch (Attribute) {
255 default: assert(0 && "Unknown ELF .type");
256 case MCSA_ELF_TypeFunction: OS << "function"; break;
257 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
258 case MCSA_ELF_TypeObject: OS << "object"; break;
259 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
260 case MCSA_ELF_TypeCommon: OS << "common"; break;
261 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
262 }
263 EmitEOL();
264 return;
265 case MCSA_Global: // .globl/.global
266 OS << MAI.getGlobalDirective();
267 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000268 case MCSA_Hidden: OS << ".hidden "; break;
269 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
270 case MCSA_Internal: OS << ".internal "; break;
271 case MCSA_LazyReference: OS << ".lazy_reference "; break;
272 case MCSA_Local: OS << ".local "; break;
273 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
274 case MCSA_PrivateExtern: OS << ".private_extern "; break;
275 case MCSA_Protected: OS << ".protected "; break;
276 case MCSA_Reference: OS << ".reference "; break;
277 case MCSA_Weak: OS << ".weak "; break;
278 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
279 // .weak_reference
280 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000281 }
282
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000283 OS << *Symbol;
284 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000285}
286
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000287void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000288 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
289 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000290}
291
Chris Lattner99328ad2010-01-25 07:52:13 +0000292void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
293 assert(MAI.hasDotTypeDotSizeDirective());
294 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
295}
296
Chris Lattner9eb158d2010-01-23 07:47:02 +0000297void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000298 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000299 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000300 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000301 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000302 OS << ',' << ByteAlignment;
303 else
304 OS << ',' << Log2_32(ByteAlignment);
305 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000306 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000307}
308
Chris Lattner9eb158d2010-01-23 07:47:02 +0000309/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
310///
311/// @param Symbol - The common symbol to emit.
312/// @param Size - The size of the common symbol.
313void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
314 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
315 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
316 EmitEOL();
317}
318
Daniel Dunbar8751b942009-08-28 05:48:22 +0000319void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000320 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000321 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000322 OS << ".zerofill ";
323
324 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000325 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000326 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000327
Chris Lattner9be3fee2009-07-10 22:20:30 +0000328 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000329 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000330 if (ByteAlignment != 0)
331 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000332 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000333 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000334}
335
Chris Lattner12e555c2010-01-23 00:15:00 +0000336static inline char toOctal(int X) { return (X&7)+'0'; }
337
Chris Lattnera6594fc2010-01-25 18:58:59 +0000338static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
339 OS << '"';
340
341 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
342 unsigned char C = Data[i];
343 if (C == '"' || C == '\\') {
344 OS << '\\' << (char)C;
345 continue;
346 }
347
348 if (isprint((unsigned char)C)) {
349 OS << (char)C;
350 continue;
351 }
352
353 switch (C) {
354 case '\b': OS << "\\b"; break;
355 case '\f': OS << "\\f"; break;
356 case '\n': OS << "\\n"; break;
357 case '\r': OS << "\\r"; break;
358 case '\t': OS << "\\t"; break;
359 default:
360 OS << '\\';
361 OS << toOctal(C >> 6);
362 OS << toOctal(C >> 3);
363 OS << toOctal(C >> 0);
364 break;
365 }
366 }
367
368 OS << '"';
369}
370
371
Chris Lattneraaec2052010-01-19 19:46:13 +0000372void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000373 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000374 if (Data.empty()) return;
375
376 if (Data.size() == 1) {
377 OS << MAI.getData8bitsDirective(AddrSpace);
378 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000379 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000380 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000381 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000382
383 // If the data ends with 0 and the target supports .asciz, use it, otherwise
384 // use .ascii
385 if (MAI.getAscizDirective() && Data.back() == 0) {
386 OS << MAI.getAscizDirective();
387 Data = Data.substr(0, Data.size()-1);
388 } else {
389 OS << MAI.getAsciiDirective();
390 }
391
Chris Lattnera6594fc2010-01-25 18:58:59 +0000392 OS << ' ';
393 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000394 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000395}
396
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000397/// EmitIntValue - Special case of EmitValue that avoids the client having
398/// to pass in a MCExpr for constant integers.
399void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
400 unsigned AddrSpace) {
401 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000402 const char *Directive = 0;
403 switch (Size) {
404 default: break;
405 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
406 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
407 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000408 case 8:
409 Directive = MAI.getData64bitsDirective(AddrSpace);
410 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
411 if (Directive) break;
412 if (isLittleEndian()) {
413 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
414 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
415 } else {
416 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
417 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
418 }
419 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000420 }
421
422 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000423 OS << Directive << truncateToSize(Value, Size);
424 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000425}
426
Chris Lattneraaec2052010-01-19 19:46:13 +0000427void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
428 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000429 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000430 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000431 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000432 default: break;
433 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
434 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
435 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
436 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000437 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000438
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000439 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000440 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000441 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000442}
443
Chris Lattner718fb592010-01-25 21:28:50 +0000444void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
445 assert(MAI.getGPRel32Directive() != 0);
446 OS << MAI.getGPRel32Directive() << *Value;
447 EmitEOL();
448}
449
450
Chris Lattner6113b3d2010-01-19 18:52:28 +0000451/// EmitFill - Emit NumBytes bytes worth of the value specified by
452/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000453void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
454 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000455 if (NumBytes == 0) return;
456
Chris Lattneraaec2052010-01-19 19:46:13 +0000457 if (AddrSpace == 0)
458 if (const char *ZeroDirective = MAI.getZeroDirective()) {
459 OS << ZeroDirective << NumBytes;
460 if (FillValue != 0)
461 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000462 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000463 return;
464 }
465
466 // Emit a byte at a time.
467 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000468}
469
Daniel Dunbar84a29262009-06-24 19:25:34 +0000470void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
471 unsigned ValueSize,
472 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000473 // Some assemblers don't support non-power of two alignments, so we always
474 // emit alignments as a power of two if possible.
475 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000476 switch (ValueSize) {
477 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000478 case 1: OS << MAI.getAlignDirective(); break;
479 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000480 case 2: OS << ".p2alignw "; break;
481 case 4: OS << ".p2alignl "; break;
482 case 8: llvm_unreachable("Unsupported alignment size!");
483 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000484
Chris Lattner33adcfb2009-08-22 21:43:10 +0000485 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000486 OS << ByteAlignment;
487 else
488 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000489
Chris Lattner663c2d22009-08-19 06:12:02 +0000490 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000491 OS << ", 0x";
492 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000493
494 if (MaxBytesToEmit)
495 OS << ", " << MaxBytesToEmit;
496 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000497 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000498 return;
499 }
500
501 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000502 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000503 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000504 default: llvm_unreachable("Invalid size for machine code value!");
505 case 1: OS << ".balign"; break;
506 case 2: OS << ".balignw"; break;
507 case 4: OS << ".balignl"; break;
508 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000509 }
510
Chris Lattner663c2d22009-08-19 06:12:02 +0000511 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000512 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000513 if (MaxBytesToEmit)
514 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000515 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000516}
517
Kevin Enderby6e720482010-02-23 18:26:34 +0000518void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
519 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000520 // Emit with a text fill value.
521 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
522 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000523}
524
Daniel Dunbar821e3332009-08-31 08:09:28 +0000525void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000526 unsigned char Value) {
527 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000528 OS << ".org " << *Offset << ", " << (unsigned) Value;
529 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000530}
531
Chris Lattnera6594fc2010-01-25 18:58:59 +0000532
533void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
534 assert(MAI.hasSingleParameterDotFile());
535 OS << "\t.file\t";
536 PrintQuotedString(Filename, OS);
537 EmitEOL();
538}
539
540void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
541 OS << "\t.file\t" << FileNo << ' ';
542 PrintQuotedString(Filename, OS);
543 EmitEOL();
544}
545
Daniel Dunbar6b716532010-02-09 23:00:14 +0000546void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
547 raw_ostream &OS = GetCommentOS();
548 SmallString<256> Code;
549 SmallVector<MCFixup, 4> Fixups;
550 raw_svector_ostream VecOS(Code);
551 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
552 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000553
Daniel Dunbar6b716532010-02-09 23:00:14 +0000554 // If we are showing fixups, create symbolic markers in the encoded
555 // representation. We do this by making a per-bit map to the fixup item index,
556 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000557 SmallVector<uint8_t, 64> FixupMap;
558 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000559 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
560 FixupMap[i] = 0;
561
562 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
563 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000564 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000565 for (unsigned j = 0; j != Info.TargetSize; ++j) {
566 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
567 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
568 FixupMap[Index] = 1 + i;
569 }
570 }
571
572 OS << "encoding: [";
573 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
574 if (i)
575 OS << ',';
576
577 // See if all bits are the same map entry.
578 uint8_t MapEntry = FixupMap[i * 8 + 0];
579 for (unsigned j = 1; j != 8; ++j) {
580 if (FixupMap[i * 8 + j] == MapEntry)
581 continue;
582
583 MapEntry = uint8_t(~0U);
584 break;
585 }
586
587 if (MapEntry != uint8_t(~0U)) {
588 if (MapEntry == 0) {
589 OS << format("0x%02x", uint8_t(Code[i]));
590 } else {
591 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
592 OS << char('A' + MapEntry - 1);
593 }
594 } else {
595 // Otherwise, write out in binary.
596 OS << "0b";
597 for (unsigned j = 8; j--;) {
598 unsigned Bit = (Code[i] >> j) & 1;
599 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
600 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
601 OS << char('A' + MapEntry - 1);
602 } else
603 OS << Bit;
604 }
605 }
606 }
607 OS << "]\n";
608
609 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
610 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000611 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000612 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000613 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000614 }
615}
616
617void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
618 assert(CurSection && "Cannot emit contents before setting section!");
619
620 // Show the encoding in a comment if we have a code emitter.
621 if (Emitter)
622 AddEncodingComment(Inst);
623
Chris Lattner30d9a642010-02-09 00:54:51 +0000624 // Show the MCInst if enabled.
625 if (ShowInst) {
626 raw_ostream &OS = GetCommentOS();
627 OS << "<MCInst #" << Inst.getOpcode();
628
Chris Lattner7e851802010-02-11 22:39:10 +0000629 StringRef InstName;
630 if (InstPrinter)
631 InstName = InstPrinter->getOpcodeName(Inst.getOpcode());
632 if (!InstName.empty())
633 OS << ' ' << InstName;
634
Chris Lattner30d9a642010-02-09 00:54:51 +0000635 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
636 OS << "\n ";
637 Inst.getOperand(i).print(OS, &MAI);
638 }
639 OS << ">\n";
640 }
641
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000642 // If we have an AsmPrinter, use that to print, otherwise dump the MCInst.
643 if (InstPrinter)
644 InstPrinter->printInst(&Inst);
645 else
646 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000647 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000648}
649
650void MCAsmStreamer::Finish() {
651 OS.flush();
652}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000653
Chris Lattner86e22112010-01-22 07:29:22 +0000654MCStreamer *llvm::createAsmStreamer(MCContext &Context,
655 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000656 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000657 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000658 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000659 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000660 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000661}