blob: b63427ac187c502f4f60d53b208c3688ceedd64a [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;
Daniel Dunbar6b716532010-02-09 23:00:14 +000040 unsigned ShowFixups : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000041 unsigned ShowInst : 1;
42
Chris Lattner46a947d2009-08-17 04:17:34 +000043public:
Chris Lattner86e22112010-01-22 07:29:22 +000044 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
45 const MCAsmInfo &mai,
Chris Lattner07404412010-01-22 07:06:15 +000046 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
Daniel Dunbar6b716532010-02-09 23:00:14 +000047 MCCodeEmitter *emitter, bool showInst, bool showFixups)
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000048 : MCStreamer(Context), OS(os), MAI(mai), InstPrinter(printer),
49 Emitter(emitter), CommentStream(CommentToEmit),
50 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
Chris Lattner5d672cf2010-02-10 00:10:18 +000051 ShowFixups(showFixups), ShowInst(showInst) {
52 if (InstPrinter && IsVerboseAsm)
53 InstPrinter->setCommentStream(CommentStream);
54 }
Chris Lattner46a947d2009-08-17 04:17:34 +000055 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000056
Chris Lattner16582022010-01-20 06:39:07 +000057 bool isLittleEndian() const { return IsLittleEndian; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000058
Chris Lattner86e22112010-01-22 07:29:22 +000059 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000060 // If we don't have any comments, just emit a \n.
61 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000062 OS << '\n';
63 return;
64 }
65 EmitCommentsAndEOL();
66 }
67 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000068
69 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
70 /// all.
71 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000072
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000073 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000074 /// file if applicable as a QoI issue to make the output of the compiler
75 /// more readable. This only affects the MCAsmStreamer, and only when
76 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000077 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000078
79 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
80 virtual void AddEncodingComment(const MCInst &Inst);
81
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000082 /// GetCommentOS - Return a raw_ostream that comments can be written to.
83 /// Unlike AddComment, you are required to terminate comments with \n if you
84 /// use this method.
85 virtual raw_ostream &GetCommentOS() {
86 if (!IsVerboseAsm)
87 return nulls(); // Discard comments unless in verbose asm mode.
88 return CommentStream;
89 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000090
Chris Lattner0fd90fd2010-01-22 19:52:01 +000091 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
92 virtual void AddBlankLine() {
93 EmitEOL();
94 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000095
Chris Lattner46a947d2009-08-17 04:17:34 +000096 /// @name MCStreamer Interface
97 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000098
Chris Lattner975780b2009-08-17 05:49:08 +000099 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000100
Chris Lattner46a947d2009-08-17 04:17:34 +0000101 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000102
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000103 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +0000104
Daniel Dunbar821e3332009-08-31 08:09:28 +0000105 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000106
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000107 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000108
Chris Lattner46a947d2009-08-17 04:17:34 +0000109 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +0000110
Chris Lattner99328ad2010-01-25 07:52:13 +0000111 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000112 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000113 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000114
Chris Lattner9eb158d2010-01-23 07:47:02 +0000115 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
116 ///
117 /// @param Symbol - The common symbol to emit.
118 /// @param Size - The size of the common symbol.
119 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
120
Daniel Dunbar8751b942009-08-28 05:48:22 +0000121 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000122 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000123
Chris Lattneraaec2052010-01-19 19:46:13 +0000124 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000125
Chris Lattneraaec2052010-01-19 19:46:13 +0000126 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000127 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000128 virtual void EmitGPRel32Value(const MCExpr *Value);
129
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000130
Chris Lattneraaec2052010-01-19 19:46:13 +0000131 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
132 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000133
Chris Lattner46a947d2009-08-17 04:17:34 +0000134 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
135 unsigned ValueSize = 1,
136 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000137
Daniel Dunbar821e3332009-08-31 08:09:28 +0000138 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000139 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000140
Chris Lattnera6594fc2010-01-25 18:58:59 +0000141 virtual void EmitFileDirective(StringRef Filename);
142 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
143
144 virtual void EmitInstruction(const MCInst &Inst);
145
Chris Lattner46a947d2009-08-17 04:17:34 +0000146 virtual void Finish();
147
148 /// @}
149};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000150
Chris Lattner46a947d2009-08-17 04:17:34 +0000151} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000152
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000153/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000154/// file if applicable as a QoI issue to make the output of the compiler
155/// more readable. This only affects the MCAsmStreamer, and only when
156/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000157void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000158 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000159
160 // Make sure that CommentStream is flushed.
161 CommentStream.flush();
162
Chris Lattner86e22112010-01-22 07:29:22 +0000163 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000164 // Each comment goes on its own line.
165 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000166
167 // Tell the comment stream that the vector changed underneath it.
168 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000169}
170
171void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000172 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
173 OS << '\n';
174 return;
175 }
176
177 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000178 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000179
180 assert(Comments.back() == '\n' &&
181 "Comment array not newline terminated");
182 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000183 // Emit a line of comments.
184 OS.PadToColumn(MAI.getCommentColumn());
185 size_t Position = Comments.find('\n');
186 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
187
Chris Lattner86e22112010-01-22 07:29:22 +0000188 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000189 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000190
Chris Lattner14ca1772010-01-22 21:16:10 +0000191 CommentToEmit.clear();
192 // Tell the comment stream that the vector changed underneath it.
193 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000194}
195
196
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000197static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
198 assert(Bytes && "Invalid size!");
199 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
200}
201
Chris Lattner975780b2009-08-17 05:49:08 +0000202void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000203 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000204 if (Section != CurSection) {
205 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000206 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000207 }
208}
209
210void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000211 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000212 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000213
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000214 OS << *Symbol << ":";
215 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000216 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000217}
218
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000220 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000221 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000222 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000223 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000224 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000225}
226
Daniel Dunbar821e3332009-08-31 08:09:28 +0000227void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000228 // Only absolute symbols can be redefined.
229 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
230 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000231
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000232 OS << *Symbol << " = " << *Value;
233 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000234
235 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000236 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000237 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000238}
239
Daniel Dunbarfffff912009-10-16 01:34:54 +0000240void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000241 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000242 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000243 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000244 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
245 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
246 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
247 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
248 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
249 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
250 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000251 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000252 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
253 switch (Attribute) {
254 default: assert(0 && "Unknown ELF .type");
255 case MCSA_ELF_TypeFunction: OS << "function"; break;
256 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
257 case MCSA_ELF_TypeObject: OS << "object"; break;
258 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
259 case MCSA_ELF_TypeCommon: OS << "common"; break;
260 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
261 }
262 EmitEOL();
263 return;
264 case MCSA_Global: // .globl/.global
265 OS << MAI.getGlobalDirective();
266 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000267 case MCSA_Hidden: OS << ".hidden "; break;
268 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
269 case MCSA_Internal: OS << ".internal "; break;
270 case MCSA_LazyReference: OS << ".lazy_reference "; break;
271 case MCSA_Local: OS << ".local "; break;
272 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
273 case MCSA_PrivateExtern: OS << ".private_extern "; break;
274 case MCSA_Protected: OS << ".protected "; break;
275 case MCSA_Reference: OS << ".reference "; break;
276 case MCSA_Weak: OS << ".weak "; break;
277 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
278 // .weak_reference
279 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000280 }
281
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000282 OS << *Symbol;
283 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000284}
285
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000286void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000287 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
288 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000289}
290
Chris Lattner99328ad2010-01-25 07:52:13 +0000291void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
292 assert(MAI.hasDotTypeDotSizeDirective());
293 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
294}
295
Chris Lattner9eb158d2010-01-23 07:47:02 +0000296void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000297 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000298 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000299 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000300 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000301 OS << ',' << ByteAlignment;
302 else
303 OS << ',' << Log2_32(ByteAlignment);
304 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000305 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000306}
307
Chris Lattner9eb158d2010-01-23 07:47:02 +0000308/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
309///
310/// @param Symbol - The common symbol to emit.
311/// @param Size - The size of the common symbol.
312void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
313 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
314 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
315 EmitEOL();
316}
317
Daniel Dunbar8751b942009-08-28 05:48:22 +0000318void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000319 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000320 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000321 OS << ".zerofill ";
322
323 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000324 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000325 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000326
Chris Lattner9be3fee2009-07-10 22:20:30 +0000327 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000328 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000329 if (ByteAlignment != 0)
330 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000331 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000332 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000333}
334
Chris Lattner12e555c2010-01-23 00:15:00 +0000335static inline char toOctal(int X) { return (X&7)+'0'; }
336
Chris Lattnera6594fc2010-01-25 18:58:59 +0000337static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
338 OS << '"';
339
340 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
341 unsigned char C = Data[i];
342 if (C == '"' || C == '\\') {
343 OS << '\\' << (char)C;
344 continue;
345 }
346
347 if (isprint((unsigned char)C)) {
348 OS << (char)C;
349 continue;
350 }
351
352 switch (C) {
353 case '\b': OS << "\\b"; break;
354 case '\f': OS << "\\f"; break;
355 case '\n': OS << "\\n"; break;
356 case '\r': OS << "\\r"; break;
357 case '\t': OS << "\\t"; break;
358 default:
359 OS << '\\';
360 OS << toOctal(C >> 6);
361 OS << toOctal(C >> 3);
362 OS << toOctal(C >> 0);
363 break;
364 }
365 }
366
367 OS << '"';
368}
369
370
Chris Lattneraaec2052010-01-19 19:46:13 +0000371void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000372 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000373 if (Data.empty()) return;
374
375 if (Data.size() == 1) {
376 OS << MAI.getData8bitsDirective(AddrSpace);
377 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000378 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000379 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000380 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000381
382 // If the data ends with 0 and the target supports .asciz, use it, otherwise
383 // use .ascii
384 if (MAI.getAscizDirective() && Data.back() == 0) {
385 OS << MAI.getAscizDirective();
386 Data = Data.substr(0, Data.size()-1);
387 } else {
388 OS << MAI.getAsciiDirective();
389 }
390
Chris Lattnera6594fc2010-01-25 18:58:59 +0000391 OS << ' ';
392 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000393 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000394}
395
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000396/// EmitIntValue - Special case of EmitValue that avoids the client having
397/// to pass in a MCExpr for constant integers.
398void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
399 unsigned AddrSpace) {
400 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000401 const char *Directive = 0;
402 switch (Size) {
403 default: break;
404 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
405 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
406 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000407 case 8:
408 Directive = MAI.getData64bitsDirective(AddrSpace);
409 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
410 if (Directive) break;
411 if (isLittleEndian()) {
412 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
413 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
414 } else {
415 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
416 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
417 }
418 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000419 }
420
421 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000422 OS << Directive << truncateToSize(Value, Size);
423 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000424}
425
Chris Lattneraaec2052010-01-19 19:46:13 +0000426void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
427 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000428 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000429 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000430 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000431 default: break;
432 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
433 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
434 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
435 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000436 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000437
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000438 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000439 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000440 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000441}
442
Chris Lattner718fb592010-01-25 21:28:50 +0000443void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
444 assert(MAI.getGPRel32Directive() != 0);
445 OS << MAI.getGPRel32Directive() << *Value;
446 EmitEOL();
447}
448
449
Chris Lattner6113b3d2010-01-19 18:52:28 +0000450/// EmitFill - Emit NumBytes bytes worth of the value specified by
451/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000452void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
453 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000454 if (NumBytes == 0) return;
455
Chris Lattneraaec2052010-01-19 19:46:13 +0000456 if (AddrSpace == 0)
457 if (const char *ZeroDirective = MAI.getZeroDirective()) {
458 OS << ZeroDirective << NumBytes;
459 if (FillValue != 0)
460 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000461 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000462 return;
463 }
464
465 // Emit a byte at a time.
466 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000467}
468
Daniel Dunbar84a29262009-06-24 19:25:34 +0000469void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
470 unsigned ValueSize,
471 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000472 // Some assemblers don't support non-power of two alignments, so we always
473 // emit alignments as a power of two if possible.
474 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000475 switch (ValueSize) {
476 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000477 case 1: OS << MAI.getAlignDirective(); break;
478 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000479 case 2: OS << ".p2alignw "; break;
480 case 4: OS << ".p2alignl "; break;
481 case 8: llvm_unreachable("Unsupported alignment size!");
482 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000483
Chris Lattner33adcfb2009-08-22 21:43:10 +0000484 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000485 OS << ByteAlignment;
486 else
487 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000488
Chris Lattner663c2d22009-08-19 06:12:02 +0000489 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000490 OS << ", 0x";
491 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000492
493 if (MaxBytesToEmit)
494 OS << ", " << MaxBytesToEmit;
495 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000496 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000497 return;
498 }
499
500 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000501 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000502 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000503 default: llvm_unreachable("Invalid size for machine code value!");
504 case 1: OS << ".balign"; break;
505 case 2: OS << ".balignw"; break;
506 case 4: OS << ".balignl"; break;
507 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000508 }
509
Chris Lattner663c2d22009-08-19 06:12:02 +0000510 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000511 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000512 if (MaxBytesToEmit)
513 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000514 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000515}
516
Daniel Dunbar821e3332009-08-31 08:09:28 +0000517void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000518 unsigned char Value) {
519 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000520 OS << ".org " << *Offset << ", " << (unsigned) Value;
521 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000522}
523
Chris Lattnera6594fc2010-01-25 18:58:59 +0000524
525void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
526 assert(MAI.hasSingleParameterDotFile());
527 OS << "\t.file\t";
528 PrintQuotedString(Filename, OS);
529 EmitEOL();
530}
531
532void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
533 OS << "\t.file\t" << FileNo << ' ';
534 PrintQuotedString(Filename, OS);
535 EmitEOL();
536}
537
Daniel Dunbar6b716532010-02-09 23:00:14 +0000538void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
539 raw_ostream &OS = GetCommentOS();
540 SmallString<256> Code;
541 SmallVector<MCFixup, 4> Fixups;
542 raw_svector_ostream VecOS(Code);
543 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
544 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000545
Daniel Dunbar6b716532010-02-09 23:00:14 +0000546 // If we aren't showing fixups, just show the bytes.
547 if (!ShowFixups) {
Chris Lattner8ef2cef2010-02-03 06:28:13 +0000548 OS << "encoding: [";
549 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
550 if (i)
551 OS << ',';
Chris Lattner342cbdc2010-02-05 22:20:08 +0000552 OS << format("0x%02x", uint8_t(Code[i]));
Chris Lattner8ef2cef2010-02-03 06:28:13 +0000553 }
554 OS << "]\n";
555 }
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000556
Daniel Dunbar6b716532010-02-09 23:00:14 +0000557 // If we are showing fixups, create symbolic markers in the encoded
558 // representation. We do this by making a per-bit map to the fixup item index,
559 // then trying to display it as nicely as possible.
560 uint8_t *FixupMap = new uint8_t[Code.size() * 8];
561 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
562 FixupMap[i] = 0;
563
564 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
565 MCFixup &F = Fixups[i];
566 MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
567 for (unsigned j = 0; j != Info.TargetSize; ++j) {
568 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
569 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
570 FixupMap[Index] = 1 + i;
571 }
572 }
573
574 OS << "encoding: [";
575 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
576 if (i)
577 OS << ',';
578
579 // See if all bits are the same map entry.
580 uint8_t MapEntry = FixupMap[i * 8 + 0];
581 for (unsigned j = 1; j != 8; ++j) {
582 if (FixupMap[i * 8 + j] == MapEntry)
583 continue;
584
585 MapEntry = uint8_t(~0U);
586 break;
587 }
588
589 if (MapEntry != uint8_t(~0U)) {
590 if (MapEntry == 0) {
591 OS << format("0x%02x", uint8_t(Code[i]));
592 } else {
593 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
594 OS << char('A' + MapEntry - 1);
595 }
596 } else {
597 // Otherwise, write out in binary.
598 OS << "0b";
599 for (unsigned j = 8; j--;) {
600 unsigned Bit = (Code[i] >> j) & 1;
601 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
602 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
603 OS << char('A' + MapEntry - 1);
604 } else
605 OS << Bit;
606 }
607 }
608 }
609 OS << "]\n";
610
611 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
612 MCFixup &F = Fixups[i];
613 MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
614 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
615 << ", op: " << F.getOpIndex() << ", kind: " << Info.Name << "\n";
616 }
617}
618
619void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
620 assert(CurSection && "Cannot emit contents before setting section!");
621
622 // Show the encoding in a comment if we have a code emitter.
623 if (Emitter)
624 AddEncodingComment(Inst);
625
Chris Lattner30d9a642010-02-09 00:54:51 +0000626 // Show the MCInst if enabled.
627 if (ShowInst) {
628 raw_ostream &OS = GetCommentOS();
629 OS << "<MCInst #" << Inst.getOpcode();
630
631 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
632 OS << "\n ";
633 Inst.getOperand(i).print(OS, &MAI);
634 }
635 OS << ">\n";
636 }
637
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000638 // If we have an AsmPrinter, use that to print, otherwise dump the MCInst.
639 if (InstPrinter)
640 InstPrinter->printInst(&Inst);
641 else
642 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000643 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000644}
645
646void MCAsmStreamer::Finish() {
647 OS.flush();
648}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000649
Chris Lattner86e22112010-01-22 07:29:22 +0000650MCStreamer *llvm::createAsmStreamer(MCContext &Context,
651 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000652 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000653 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar6b716532010-02-09 23:00:14 +0000654 MCCodeEmitter *CE, bool ShowInst,
655 bool ShowFixups) {
Chris Lattner07404412010-01-22 07:06:15 +0000656 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
Daniel Dunbar6b716532010-02-09 23:00:14 +0000657 IP, CE, ShowInst, ShowFixups);
Daniel Dunbara11af532009-06-24 01:03:06 +0000658}