blob: 6add1b4abaab6fea03c125c105aeba7d5058e716 [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
Daniel Dunbar821e3332009-08-31 08:09:28 +0000137 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000138 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000139
Chris Lattnera6594fc2010-01-25 18:58:59 +0000140 virtual void EmitFileDirective(StringRef Filename);
141 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
142
143 virtual void EmitInstruction(const MCInst &Inst);
144
Chris Lattner46a947d2009-08-17 04:17:34 +0000145 virtual void Finish();
146
147 /// @}
148};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000149
Chris Lattner46a947d2009-08-17 04:17:34 +0000150} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000151
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000152/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000153/// file if applicable as a QoI issue to make the output of the compiler
154/// more readable. This only affects the MCAsmStreamer, and only when
155/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000156void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000157 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000158
159 // Make sure that CommentStream is flushed.
160 CommentStream.flush();
161
Chris Lattner86e22112010-01-22 07:29:22 +0000162 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000163 // Each comment goes on its own line.
164 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000165
166 // Tell the comment stream that the vector changed underneath it.
167 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000168}
169
170void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000171 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
172 OS << '\n';
173 return;
174 }
175
176 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000177 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000178
179 assert(Comments.back() == '\n' &&
180 "Comment array not newline terminated");
181 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000182 // Emit a line of comments.
183 OS.PadToColumn(MAI.getCommentColumn());
184 size_t Position = Comments.find('\n');
185 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
186
Chris Lattner86e22112010-01-22 07:29:22 +0000187 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000188 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000189
Chris Lattner14ca1772010-01-22 21:16:10 +0000190 CommentToEmit.clear();
191 // Tell the comment stream that the vector changed underneath it.
192 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000193}
194
195
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000196static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
197 assert(Bytes && "Invalid size!");
198 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
199}
200
Chris Lattner975780b2009-08-17 05:49:08 +0000201void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000202 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000203 if (Section != CurSection) {
204 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000205 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000206 }
207}
208
209void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000210 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000211 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000212
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000213 OS << *Symbol << ":";
214 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000215 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000216}
217
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000218void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000219 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000220 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000221 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000222 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000223 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000224}
225
Daniel Dunbar821e3332009-08-31 08:09:28 +0000226void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000227 // Only absolute symbols can be redefined.
228 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
229 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000230
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000231 OS << *Symbol << " = " << *Value;
232 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000233
234 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000235 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000236 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000237}
238
Daniel Dunbarfffff912009-10-16 01:34:54 +0000239void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000240 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000241 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000242 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000243 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
244 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
245 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
246 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
247 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
248 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
249 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000250 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000251 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
252 switch (Attribute) {
253 default: assert(0 && "Unknown ELF .type");
254 case MCSA_ELF_TypeFunction: OS << "function"; break;
255 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
256 case MCSA_ELF_TypeObject: OS << "object"; break;
257 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
258 case MCSA_ELF_TypeCommon: OS << "common"; break;
259 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
260 }
261 EmitEOL();
262 return;
263 case MCSA_Global: // .globl/.global
264 OS << MAI.getGlobalDirective();
265 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000266 case MCSA_Hidden: OS << ".hidden "; break;
267 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
268 case MCSA_Internal: OS << ".internal "; break;
269 case MCSA_LazyReference: OS << ".lazy_reference "; break;
270 case MCSA_Local: OS << ".local "; break;
271 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
272 case MCSA_PrivateExtern: OS << ".private_extern "; break;
273 case MCSA_Protected: OS << ".protected "; break;
274 case MCSA_Reference: OS << ".reference "; break;
275 case MCSA_Weak: OS << ".weak "; break;
276 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
277 // .weak_reference
278 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000279 }
280
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000281 OS << *Symbol;
282 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000283}
284
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000285void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000286 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
287 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000288}
289
Chris Lattner99328ad2010-01-25 07:52:13 +0000290void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
291 assert(MAI.hasDotTypeDotSizeDirective());
292 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
293}
294
Chris Lattner9eb158d2010-01-23 07:47:02 +0000295void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000296 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000297 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000298 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000299 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000300 OS << ',' << ByteAlignment;
301 else
302 OS << ',' << Log2_32(ByteAlignment);
303 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000304 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000305}
306
Chris Lattner9eb158d2010-01-23 07:47:02 +0000307/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
308///
309/// @param Symbol - The common symbol to emit.
310/// @param Size - The size of the common symbol.
311void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
312 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
313 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
314 EmitEOL();
315}
316
Daniel Dunbar8751b942009-08-28 05:48:22 +0000317void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000318 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000319 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000320 OS << ".zerofill ";
321
322 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000323 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000324 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000325
Chris Lattner9be3fee2009-07-10 22:20:30 +0000326 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000327 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000328 if (ByteAlignment != 0)
329 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000330 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000331 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000332}
333
Chris Lattner12e555c2010-01-23 00:15:00 +0000334static inline char toOctal(int X) { return (X&7)+'0'; }
335
Chris Lattnera6594fc2010-01-25 18:58:59 +0000336static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
337 OS << '"';
338
339 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
340 unsigned char C = Data[i];
341 if (C == '"' || C == '\\') {
342 OS << '\\' << (char)C;
343 continue;
344 }
345
346 if (isprint((unsigned char)C)) {
347 OS << (char)C;
348 continue;
349 }
350
351 switch (C) {
352 case '\b': OS << "\\b"; break;
353 case '\f': OS << "\\f"; break;
354 case '\n': OS << "\\n"; break;
355 case '\r': OS << "\\r"; break;
356 case '\t': OS << "\\t"; break;
357 default:
358 OS << '\\';
359 OS << toOctal(C >> 6);
360 OS << toOctal(C >> 3);
361 OS << toOctal(C >> 0);
362 break;
363 }
364 }
365
366 OS << '"';
367}
368
369
Chris Lattneraaec2052010-01-19 19:46:13 +0000370void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000371 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000372 if (Data.empty()) return;
373
374 if (Data.size() == 1) {
375 OS << MAI.getData8bitsDirective(AddrSpace);
376 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000377 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000378 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000379 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000380
381 // If the data ends with 0 and the target supports .asciz, use it, otherwise
382 // use .ascii
383 if (MAI.getAscizDirective() && Data.back() == 0) {
384 OS << MAI.getAscizDirective();
385 Data = Data.substr(0, Data.size()-1);
386 } else {
387 OS << MAI.getAsciiDirective();
388 }
389
Chris Lattnera6594fc2010-01-25 18:58:59 +0000390 OS << ' ';
391 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000392 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000393}
394
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000395/// EmitIntValue - Special case of EmitValue that avoids the client having
396/// to pass in a MCExpr for constant integers.
397void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
398 unsigned AddrSpace) {
399 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000400 const char *Directive = 0;
401 switch (Size) {
402 default: break;
403 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
404 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
405 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000406 case 8:
407 Directive = MAI.getData64bitsDirective(AddrSpace);
408 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
409 if (Directive) break;
410 if (isLittleEndian()) {
411 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
412 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
413 } else {
414 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
415 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
416 }
417 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000418 }
419
420 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000421 OS << Directive << truncateToSize(Value, Size);
422 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000423}
424
Chris Lattneraaec2052010-01-19 19:46:13 +0000425void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
426 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000427 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000428 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000429 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000430 default: break;
431 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
432 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
433 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
434 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000435 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000436
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000437 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000438 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000439 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000440}
441
Chris Lattner718fb592010-01-25 21:28:50 +0000442void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
443 assert(MAI.getGPRel32Directive() != 0);
444 OS << MAI.getGPRel32Directive() << *Value;
445 EmitEOL();
446}
447
448
Chris Lattner6113b3d2010-01-19 18:52:28 +0000449/// EmitFill - Emit NumBytes bytes worth of the value specified by
450/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000451void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
452 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000453 if (NumBytes == 0) return;
454
Chris Lattneraaec2052010-01-19 19:46:13 +0000455 if (AddrSpace == 0)
456 if (const char *ZeroDirective = MAI.getZeroDirective()) {
457 OS << ZeroDirective << NumBytes;
458 if (FillValue != 0)
459 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000460 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000461 return;
462 }
463
464 // Emit a byte at a time.
465 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000466}
467
Daniel Dunbar84a29262009-06-24 19:25:34 +0000468void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
469 unsigned ValueSize,
470 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000471 // Some assemblers don't support non-power of two alignments, so we always
472 // emit alignments as a power of two if possible.
473 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000474 switch (ValueSize) {
475 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000476 case 1: OS << MAI.getAlignDirective(); break;
477 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000478 case 2: OS << ".p2alignw "; break;
479 case 4: OS << ".p2alignl "; break;
480 case 8: llvm_unreachable("Unsupported alignment size!");
481 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000482
Chris Lattner33adcfb2009-08-22 21:43:10 +0000483 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000484 OS << ByteAlignment;
485 else
486 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000487
Chris Lattner663c2d22009-08-19 06:12:02 +0000488 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000489 OS << ", 0x";
490 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000491
492 if (MaxBytesToEmit)
493 OS << ", " << MaxBytesToEmit;
494 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000495 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000496 return;
497 }
498
499 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000500 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000501 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000502 default: llvm_unreachable("Invalid size for machine code value!");
503 case 1: OS << ".balign"; break;
504 case 2: OS << ".balignw"; break;
505 case 4: OS << ".balignl"; break;
506 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000507 }
508
Chris Lattner663c2d22009-08-19 06:12:02 +0000509 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000510 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000511 if (MaxBytesToEmit)
512 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000513 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000514}
515
Daniel Dunbar821e3332009-08-31 08:09:28 +0000516void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000517 unsigned char Value) {
518 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000519 OS << ".org " << *Offset << ", " << (unsigned) Value;
520 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000521}
522
Chris Lattnera6594fc2010-01-25 18:58:59 +0000523
524void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
525 assert(MAI.hasSingleParameterDotFile());
526 OS << "\t.file\t";
527 PrintQuotedString(Filename, OS);
528 EmitEOL();
529}
530
531void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
532 OS << "\t.file\t" << FileNo << ' ';
533 PrintQuotedString(Filename, OS);
534 EmitEOL();
535}
536
Daniel Dunbar6b716532010-02-09 23:00:14 +0000537void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
538 raw_ostream &OS = GetCommentOS();
539 SmallString<256> Code;
540 SmallVector<MCFixup, 4> Fixups;
541 raw_svector_ostream VecOS(Code);
542 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
543 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000544
Daniel Dunbar6b716532010-02-09 23:00:14 +0000545 // If we are showing fixups, create symbolic markers in the encoded
546 // representation. We do this by making a per-bit map to the fixup item index,
547 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000548 SmallVector<uint8_t, 64> FixupMap;
549 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000550 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
551 FixupMap[i] = 0;
552
553 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
554 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000555 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000556 for (unsigned j = 0; j != Info.TargetSize; ++j) {
557 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
558 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
559 FixupMap[Index] = 1 + i;
560 }
561 }
562
563 OS << "encoding: [";
564 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
565 if (i)
566 OS << ',';
567
568 // See if all bits are the same map entry.
569 uint8_t MapEntry = FixupMap[i * 8 + 0];
570 for (unsigned j = 1; j != 8; ++j) {
571 if (FixupMap[i * 8 + j] == MapEntry)
572 continue;
573
574 MapEntry = uint8_t(~0U);
575 break;
576 }
577
578 if (MapEntry != uint8_t(~0U)) {
579 if (MapEntry == 0) {
580 OS << format("0x%02x", uint8_t(Code[i]));
581 } else {
582 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
583 OS << char('A' + MapEntry - 1);
584 }
585 } else {
586 // Otherwise, write out in binary.
587 OS << "0b";
588 for (unsigned j = 8; j--;) {
589 unsigned Bit = (Code[i] >> j) & 1;
590 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
591 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
592 OS << char('A' + MapEntry - 1);
593 } else
594 OS << Bit;
595 }
596 }
597 }
598 OS << "]\n";
599
600 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
601 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000602 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000603 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000604 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000605 }
606}
607
608void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
609 assert(CurSection && "Cannot emit contents before setting section!");
610
611 // Show the encoding in a comment if we have a code emitter.
612 if (Emitter)
613 AddEncodingComment(Inst);
614
Chris Lattner30d9a642010-02-09 00:54:51 +0000615 // Show the MCInst if enabled.
616 if (ShowInst) {
617 raw_ostream &OS = GetCommentOS();
618 OS << "<MCInst #" << Inst.getOpcode();
619
Chris Lattner7e851802010-02-11 22:39:10 +0000620 StringRef InstName;
621 if (InstPrinter)
622 InstName = InstPrinter->getOpcodeName(Inst.getOpcode());
623 if (!InstName.empty())
624 OS << ' ' << InstName;
625
Chris Lattner30d9a642010-02-09 00:54:51 +0000626 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
627 OS << "\n ";
628 Inst.getOperand(i).print(OS, &MAI);
629 }
630 OS << ">\n";
631 }
632
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000633 // If we have an AsmPrinter, use that to print, otherwise dump the MCInst.
634 if (InstPrinter)
635 InstPrinter->printInst(&Inst);
636 else
637 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000638 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000639}
640
641void MCAsmStreamer::Finish() {
642 OS.flush();
643}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000644
Chris Lattner86e22112010-01-22 07:29:22 +0000645MCStreamer *llvm::createAsmStreamer(MCContext &Context,
646 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000647 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000648 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000649 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattner07404412010-01-22 07:06:15 +0000650 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000651 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000652}