blob: 66a0a24bedeba6ce456db09dd48f35e4a6daa661 [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,
44 const MCAsmInfo &mai,
Chris Lattner07404412010-01-22 07:06:15 +000045 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
Daniel Dunbar5532cf42010-02-10 01:41:14 +000046 MCCodeEmitter *emitter, bool showInst)
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000047 : MCStreamer(Context), OS(os), MAI(mai), InstPrinter(printer),
48 Emitter(emitter), CommentStream(CommentToEmit),
49 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
Daniel Dunbar5532cf42010-02-10 01:41:14 +000050 ShowInst(showInst) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000051 if (InstPrinter && IsVerboseAsm)
52 InstPrinter->setCommentStream(CommentStream);
53 }
Chris Lattner46a947d2009-08-17 04:17:34 +000054 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000055
Chris Lattner16582022010-01-20 06:39:07 +000056 bool isLittleEndian() const { return IsLittleEndian; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000057
Chris Lattner86e22112010-01-22 07:29:22 +000058 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000059 // If we don't have any comments, just emit a \n.
60 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000061 OS << '\n';
62 return;
63 }
64 EmitCommentsAndEOL();
65 }
66 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000067
68 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
69 /// all.
70 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000071
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000072 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000073 /// file if applicable as a QoI issue to make the output of the compiler
74 /// more readable. This only affects the MCAsmStreamer, and only when
75 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000076 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000077
78 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
79 virtual void AddEncodingComment(const MCInst &Inst);
80
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000081 /// GetCommentOS - Return a raw_ostream that comments can be written to.
82 /// Unlike AddComment, you are required to terminate comments with \n if you
83 /// use this method.
84 virtual raw_ostream &GetCommentOS() {
85 if (!IsVerboseAsm)
86 return nulls(); // Discard comments unless in verbose asm mode.
87 return CommentStream;
88 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000089
Chris Lattner0fd90fd2010-01-22 19:52:01 +000090 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
91 virtual void AddBlankLine() {
92 EmitEOL();
93 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000094
Chris Lattner46a947d2009-08-17 04:17:34 +000095 /// @name MCStreamer Interface
96 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000097
Chris Lattner975780b2009-08-17 05:49:08 +000098 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000099
Chris Lattner46a947d2009-08-17 04:17:34 +0000100 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000101
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000102 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +0000103
Daniel Dunbar821e3332009-08-31 08:09:28 +0000104 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000105
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000106 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000107
Chris Lattner46a947d2009-08-17 04:17:34 +0000108 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +0000109
Chris Lattner99328ad2010-01-25 07:52:13 +0000110 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000111 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000112 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000113
Chris Lattner9eb158d2010-01-23 07:47:02 +0000114 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
115 ///
116 /// @param Symbol - The common symbol to emit.
117 /// @param Size - The size of the common symbol.
118 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
119
Daniel Dunbar8751b942009-08-28 05:48:22 +0000120 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000121 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000122
Chris Lattneraaec2052010-01-19 19:46:13 +0000123 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000124
Chris Lattneraaec2052010-01-19 19:46:13 +0000125 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000126 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000127 virtual void EmitGPRel32Value(const MCExpr *Value);
128
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000129
Chris Lattneraaec2052010-01-19 19:46:13 +0000130 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
131 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000132
Chris Lattner46a947d2009-08-17 04:17:34 +0000133 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
134 unsigned ValueSize = 1,
135 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000136
Kevin Enderby6e720482010-02-23 18:26:34 +0000137 virtual void EmitCodeAlignment(unsigned ByteAlignment,
138 unsigned MaxBytesToEmit = 0);
139
Daniel Dunbar821e3332009-08-31 08:09:28 +0000140 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000141 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000142
Chris Lattnera6594fc2010-01-25 18:58:59 +0000143 virtual void EmitFileDirective(StringRef Filename);
144 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
145
146 virtual void EmitInstruction(const MCInst &Inst);
147
Chris Lattner46a947d2009-08-17 04:17:34 +0000148 virtual void Finish();
149
150 /// @}
151};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000152
Chris Lattner46a947d2009-08-17 04:17:34 +0000153} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000154
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000155/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000156/// file if applicable as a QoI issue to make the output of the compiler
157/// more readable. This only affects the MCAsmStreamer, and only when
158/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000159void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000160 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000161
162 // Make sure that CommentStream is flushed.
163 CommentStream.flush();
164
Chris Lattner86e22112010-01-22 07:29:22 +0000165 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000166 // Each comment goes on its own line.
167 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000168
169 // Tell the comment stream that the vector changed underneath it.
170 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000171}
172
173void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000174 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
175 OS << '\n';
176 return;
177 }
178
179 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000180 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000181
182 assert(Comments.back() == '\n' &&
183 "Comment array not newline terminated");
184 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000185 // Emit a line of comments.
186 OS.PadToColumn(MAI.getCommentColumn());
187 size_t Position = Comments.find('\n');
188 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
189
Chris Lattner86e22112010-01-22 07:29:22 +0000190 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000191 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000192
Chris Lattner14ca1772010-01-22 21:16:10 +0000193 CommentToEmit.clear();
194 // Tell the comment stream that the vector changed underneath it.
195 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000196}
197
198
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000199static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
200 assert(Bytes && "Invalid size!");
201 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
202}
203
Chris Lattner975780b2009-08-17 05:49:08 +0000204void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000205 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000206 if (Section != CurSection) {
207 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000208 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000209 }
210}
211
212void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000213 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000214 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000215
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000216 OS << *Symbol << ":";
217 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000218 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000219}
220
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000221void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000222 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000223 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000224 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000225 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000226 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000227}
228
Daniel Dunbar821e3332009-08-31 08:09:28 +0000229void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000230 // Only absolute symbols can be redefined.
231 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
232 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000233
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000234 OS << *Symbol << " = " << *Value;
235 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000236
237 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000238 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000239 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000240}
241
Daniel Dunbarfffff912009-10-16 01:34:54 +0000242void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000243 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000244 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000245 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000246 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
247 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
248 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
249 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
250 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
251 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
252 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000253 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000254 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
255 switch (Attribute) {
256 default: assert(0 && "Unknown ELF .type");
257 case MCSA_ELF_TypeFunction: OS << "function"; break;
258 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
259 case MCSA_ELF_TypeObject: OS << "object"; break;
260 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
261 case MCSA_ELF_TypeCommon: OS << "common"; break;
262 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
263 }
264 EmitEOL();
265 return;
266 case MCSA_Global: // .globl/.global
267 OS << MAI.getGlobalDirective();
268 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000269 case MCSA_Hidden: OS << ".hidden "; break;
270 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
271 case MCSA_Internal: OS << ".internal "; break;
272 case MCSA_LazyReference: OS << ".lazy_reference "; break;
273 case MCSA_Local: OS << ".local "; break;
274 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
275 case MCSA_PrivateExtern: OS << ".private_extern "; break;
276 case MCSA_Protected: OS << ".protected "; break;
277 case MCSA_Reference: OS << ".reference "; break;
278 case MCSA_Weak: OS << ".weak "; break;
279 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
280 // .weak_reference
281 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000282 }
283
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000284 OS << *Symbol;
285 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000286}
287
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000288void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000289 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
290 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000291}
292
Chris Lattner99328ad2010-01-25 07:52:13 +0000293void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
294 assert(MAI.hasDotTypeDotSizeDirective());
295 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
296}
297
Chris Lattner9eb158d2010-01-23 07:47:02 +0000298void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000299 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000300 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000301 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000302 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000303 OS << ',' << ByteAlignment;
304 else
305 OS << ',' << Log2_32(ByteAlignment);
306 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000307 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000308}
309
Chris Lattner9eb158d2010-01-23 07:47:02 +0000310/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
311///
312/// @param Symbol - The common symbol to emit.
313/// @param Size - The size of the common symbol.
314void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
315 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
316 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
317 EmitEOL();
318}
319
Daniel Dunbar8751b942009-08-28 05:48:22 +0000320void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000321 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000322 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000323 OS << ".zerofill ";
324
325 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000326 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000327 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000328
Chris Lattner9be3fee2009-07-10 22:20:30 +0000329 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000330 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000331 if (ByteAlignment != 0)
332 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000333 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000334 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000335}
336
Chris Lattner12e555c2010-01-23 00:15:00 +0000337static inline char toOctal(int X) { return (X&7)+'0'; }
338
Chris Lattnera6594fc2010-01-25 18:58:59 +0000339static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
340 OS << '"';
341
342 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
343 unsigned char C = Data[i];
344 if (C == '"' || C == '\\') {
345 OS << '\\' << (char)C;
346 continue;
347 }
348
349 if (isprint((unsigned char)C)) {
350 OS << (char)C;
351 continue;
352 }
353
354 switch (C) {
355 case '\b': OS << "\\b"; break;
356 case '\f': OS << "\\f"; break;
357 case '\n': OS << "\\n"; break;
358 case '\r': OS << "\\r"; break;
359 case '\t': OS << "\\t"; break;
360 default:
361 OS << '\\';
362 OS << toOctal(C >> 6);
363 OS << toOctal(C >> 3);
364 OS << toOctal(C >> 0);
365 break;
366 }
367 }
368
369 OS << '"';
370}
371
372
Chris Lattneraaec2052010-01-19 19:46:13 +0000373void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000374 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000375 if (Data.empty()) return;
376
377 if (Data.size() == 1) {
378 OS << MAI.getData8bitsDirective(AddrSpace);
379 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000380 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000381 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000382 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000383
384 // If the data ends with 0 and the target supports .asciz, use it, otherwise
385 // use .ascii
386 if (MAI.getAscizDirective() && Data.back() == 0) {
387 OS << MAI.getAscizDirective();
388 Data = Data.substr(0, Data.size()-1);
389 } else {
390 OS << MAI.getAsciiDirective();
391 }
392
Chris Lattnera6594fc2010-01-25 18:58:59 +0000393 OS << ' ';
394 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000395 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000396}
397
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000398/// EmitIntValue - Special case of EmitValue that avoids the client having
399/// to pass in a MCExpr for constant integers.
400void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
401 unsigned AddrSpace) {
402 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000403 const char *Directive = 0;
404 switch (Size) {
405 default: break;
406 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
407 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
408 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000409 case 8:
410 Directive = MAI.getData64bitsDirective(AddrSpace);
411 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
412 if (Directive) break;
413 if (isLittleEndian()) {
414 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
415 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
416 } else {
417 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
418 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
419 }
420 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000421 }
422
423 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000424 OS << Directive << truncateToSize(Value, Size);
425 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000426}
427
Chris Lattneraaec2052010-01-19 19:46:13 +0000428void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
429 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000430 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000431 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000432 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000433 default: break;
434 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
435 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
436 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
437 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000438 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000439
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000440 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000441 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000442 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000443}
444
Chris Lattner718fb592010-01-25 21:28:50 +0000445void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
446 assert(MAI.getGPRel32Directive() != 0);
447 OS << MAI.getGPRel32Directive() << *Value;
448 EmitEOL();
449}
450
451
Chris Lattner6113b3d2010-01-19 18:52:28 +0000452/// EmitFill - Emit NumBytes bytes worth of the value specified by
453/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000454void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
455 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000456 if (NumBytes == 0) return;
457
Chris Lattneraaec2052010-01-19 19:46:13 +0000458 if (AddrSpace == 0)
459 if (const char *ZeroDirective = MAI.getZeroDirective()) {
460 OS << ZeroDirective << NumBytes;
461 if (FillValue != 0)
462 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000463 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000464 return;
465 }
466
467 // Emit a byte at a time.
468 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000469}
470
Daniel Dunbar84a29262009-06-24 19:25:34 +0000471void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
472 unsigned ValueSize,
473 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000474 // Some assemblers don't support non-power of two alignments, so we always
475 // emit alignments as a power of two if possible.
476 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000477 switch (ValueSize) {
478 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000479 case 1: OS << MAI.getAlignDirective(); break;
480 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000481 case 2: OS << ".p2alignw "; break;
482 case 4: OS << ".p2alignl "; break;
483 case 8: llvm_unreachable("Unsupported alignment size!");
484 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000485
Chris Lattner33adcfb2009-08-22 21:43:10 +0000486 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000487 OS << ByteAlignment;
488 else
489 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000490
Chris Lattner663c2d22009-08-19 06:12:02 +0000491 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000492 OS << ", 0x";
493 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000494
495 if (MaxBytesToEmit)
496 OS << ", " << MaxBytesToEmit;
497 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000498 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000499 return;
500 }
501
502 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000503 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000504 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000505 default: llvm_unreachable("Invalid size for machine code value!");
506 case 1: OS << ".balign"; break;
507 case 2: OS << ".balignw"; break;
508 case 4: OS << ".balignl"; break;
509 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000510 }
511
Chris Lattner663c2d22009-08-19 06:12:02 +0000512 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000513 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000514 if (MaxBytesToEmit)
515 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000516 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000517}
518
Kevin Enderby6e720482010-02-23 18:26:34 +0000519void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
520 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000521 // Emit with a text fill value.
522 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
523 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000524}
525
Daniel Dunbar821e3332009-08-31 08:09:28 +0000526void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000527 unsigned char Value) {
528 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000529 OS << ".org " << *Offset << ", " << (unsigned) Value;
530 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000531}
532
Chris Lattnera6594fc2010-01-25 18:58:59 +0000533
534void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
535 assert(MAI.hasSingleParameterDotFile());
536 OS << "\t.file\t";
537 PrintQuotedString(Filename, OS);
538 EmitEOL();
539}
540
541void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
542 OS << "\t.file\t" << FileNo << ' ';
543 PrintQuotedString(Filename, OS);
544 EmitEOL();
545}
546
Daniel Dunbar6b716532010-02-09 23:00:14 +0000547void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
548 raw_ostream &OS = GetCommentOS();
549 SmallString<256> Code;
550 SmallVector<MCFixup, 4> Fixups;
551 raw_svector_ostream VecOS(Code);
552 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
553 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000554
Daniel Dunbar6b716532010-02-09 23:00:14 +0000555 // If we are showing fixups, create symbolic markers in the encoded
556 // representation. We do this by making a per-bit map to the fixup item index,
557 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000558 SmallVector<uint8_t, 64> FixupMap;
559 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000560 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
561 FixupMap[i] = 0;
562
563 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
564 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000565 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000566 for (unsigned j = 0; j != Info.TargetSize; ++j) {
567 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
568 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
569 FixupMap[Index] = 1 + i;
570 }
571 }
572
573 OS << "encoding: [";
574 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
575 if (i)
576 OS << ',';
577
578 // See if all bits are the same map entry.
579 uint8_t MapEntry = FixupMap[i * 8 + 0];
580 for (unsigned j = 1; j != 8; ++j) {
581 if (FixupMap[i * 8 + j] == MapEntry)
582 continue;
583
584 MapEntry = uint8_t(~0U);
585 break;
586 }
587
588 if (MapEntry != uint8_t(~0U)) {
589 if (MapEntry == 0) {
590 OS << format("0x%02x", uint8_t(Code[i]));
591 } else {
592 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
593 OS << char('A' + MapEntry - 1);
594 }
595 } else {
596 // Otherwise, write out in binary.
597 OS << "0b";
598 for (unsigned j = 8; j--;) {
599 unsigned Bit = (Code[i] >> j) & 1;
600 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
601 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
602 OS << char('A' + MapEntry - 1);
603 } else
604 OS << Bit;
605 }
606 }
607 }
608 OS << "]\n";
609
610 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
611 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000612 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000613 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000614 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000615 }
616}
617
618void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
619 assert(CurSection && "Cannot emit contents before setting section!");
620
621 // Show the encoding in a comment if we have a code emitter.
622 if (Emitter)
623 AddEncodingComment(Inst);
624
Chris Lattner30d9a642010-02-09 00:54:51 +0000625 // Show the MCInst if enabled.
626 if (ShowInst) {
627 raw_ostream &OS = GetCommentOS();
628 OS << "<MCInst #" << Inst.getOpcode();
629
Chris Lattner7e851802010-02-11 22:39:10 +0000630 StringRef InstName;
631 if (InstPrinter)
632 InstName = InstPrinter->getOpcodeName(Inst.getOpcode());
633 if (!InstName.empty())
634 OS << ' ' << InstName;
635
Chris Lattner30d9a642010-02-09 00:54:51 +0000636 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
637 OS << "\n ";
638 Inst.getOperand(i).print(OS, &MAI);
639 }
640 OS << ">\n";
641 }
642
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000643 // If we have an AsmPrinter, use that to print, otherwise dump the MCInst.
644 if (InstPrinter)
645 InstPrinter->printInst(&Inst);
646 else
647 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000648 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000649}
650
651void MCAsmStreamer::Finish() {
652 OS.flush();
653}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000654
Chris Lattner86e22112010-01-22 07:29:22 +0000655MCStreamer *llvm::createAsmStreamer(MCContext &Context,
656 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000657 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000658 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000659 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattner07404412010-01-22 07:06:15 +0000660 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000661 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000662}