blob: 417ad3095d0a31e160b38f40948aac79443baf51 [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 Dunbar9dee8e32010-02-03 18:18:30 +000046 MCCodeEmitter *emitter, bool showInst)
47 : MCStreamer(Context), OS(os), MAI(mai), InstPrinter(printer),
48 Emitter(emitter), CommentStream(CommentToEmit),
49 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
50 ShowInst(showInst) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000051 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000052
Chris Lattner16582022010-01-20 06:39:07 +000053 bool isLittleEndian() const { return IsLittleEndian; }
54
Chris Lattner86e22112010-01-22 07:29:22 +000055 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000056 // If we don't have any comments, just emit a \n.
57 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000058 OS << '\n';
59 return;
60 }
61 EmitCommentsAndEOL();
62 }
63 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000064
65 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
66 /// all.
67 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Chris Lattner86e22112010-01-22 07:29:22 +000068
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000069 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000070 /// file if applicable as a QoI issue to make the output of the compiler
71 /// more readable. This only affects the MCAsmStreamer, and only when
72 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000073 virtual void AddComment(const Twine &T);
Chris Lattner86e22112010-01-22 07:29:22 +000074
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000075 /// GetCommentOS - Return a raw_ostream that comments can be written to.
76 /// Unlike AddComment, you are required to terminate comments with \n if you
77 /// use this method.
78 virtual raw_ostream &GetCommentOS() {
79 if (!IsVerboseAsm)
80 return nulls(); // Discard comments unless in verbose asm mode.
81 return CommentStream;
82 }
83
Chris Lattner0fd90fd2010-01-22 19:52:01 +000084 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
85 virtual void AddBlankLine() {
86 EmitEOL();
87 }
88
Chris Lattner46a947d2009-08-17 04:17:34 +000089 /// @name MCStreamer Interface
90 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000091
Chris Lattner975780b2009-08-17 05:49:08 +000092 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000093
Chris Lattner46a947d2009-08-17 04:17:34 +000094 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000095
Chris Lattnera5ad93a2010-01-23 06:39:22 +000096 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000097
Daniel Dunbar821e3332009-08-31 08:09:28 +000098 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000099
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000100 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000101
Chris Lattner46a947d2009-08-17 04:17:34 +0000102 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +0000103
Chris Lattner99328ad2010-01-25 07:52:13 +0000104 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000105 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000106 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000107
Chris Lattner9eb158d2010-01-23 07:47:02 +0000108 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
109 ///
110 /// @param Symbol - The common symbol to emit.
111 /// @param Size - The size of the common symbol.
112 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
113
Daniel Dunbar8751b942009-08-28 05:48:22 +0000114 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000115 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000116
Chris Lattneraaec2052010-01-19 19:46:13 +0000117 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000118
Chris Lattneraaec2052010-01-19 19:46:13 +0000119 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000120 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000121 virtual void EmitGPRel32Value(const MCExpr *Value);
122
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000123
Chris Lattneraaec2052010-01-19 19:46:13 +0000124 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
125 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000126
Chris Lattner46a947d2009-08-17 04:17:34 +0000127 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
128 unsigned ValueSize = 1,
129 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000130
Daniel Dunbar821e3332009-08-31 08:09:28 +0000131 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000132 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000133
Chris Lattnera6594fc2010-01-25 18:58:59 +0000134 virtual void EmitFileDirective(StringRef Filename);
135 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
136
137 virtual void EmitInstruction(const MCInst &Inst);
138
Chris Lattner46a947d2009-08-17 04:17:34 +0000139 virtual void Finish();
140
141 /// @}
142};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000143
Chris Lattner46a947d2009-08-17 04:17:34 +0000144} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000145
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000146/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000147/// file if applicable as a QoI issue to make the output of the compiler
148/// more readable. This only affects the MCAsmStreamer, and only when
149/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000150void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000151 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000152
153 // Make sure that CommentStream is flushed.
154 CommentStream.flush();
155
Chris Lattner86e22112010-01-22 07:29:22 +0000156 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000157 // Each comment goes on its own line.
158 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000159
160 // Tell the comment stream that the vector changed underneath it.
161 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000162}
163
164void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000165 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
166 OS << '\n';
167 return;
168 }
169
170 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000171 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000172
173 assert(Comments.back() == '\n' &&
174 "Comment array not newline terminated");
175 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000176 // Emit a line of comments.
177 OS.PadToColumn(MAI.getCommentColumn());
178 size_t Position = Comments.find('\n');
179 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
180
Chris Lattner86e22112010-01-22 07:29:22 +0000181 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000182 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000183
Chris Lattner14ca1772010-01-22 21:16:10 +0000184 CommentToEmit.clear();
185 // Tell the comment stream that the vector changed underneath it.
186 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000187}
188
189
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000190static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
191 assert(Bytes && "Invalid size!");
192 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
193}
194
Chris Lattner975780b2009-08-17 05:49:08 +0000195void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000196 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000197 if (Section != CurSection) {
198 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000199 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000200 }
201}
202
203void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000204 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000205 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000206
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000207 OS << *Symbol << ":";
208 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000209 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000210}
211
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000212void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000213 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000214 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000215 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000216 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000217 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000218}
219
Daniel Dunbar821e3332009-08-31 08:09:28 +0000220void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000221 // Only absolute symbols can be redefined.
222 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
223 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000224
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000225 OS << *Symbol << " = " << *Value;
226 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000227
228 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000229 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000230 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000231}
232
Daniel Dunbarfffff912009-10-16 01:34:54 +0000233void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000234 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000235 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000236 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000237 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
238 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
239 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
240 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
241 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
242 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
243 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Chris Lattnera800f7c2010-01-25 18:33:40 +0000244 OS << "\t.type " << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000245 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
246 switch (Attribute) {
247 default: assert(0 && "Unknown ELF .type");
248 case MCSA_ELF_TypeFunction: OS << "function"; break;
249 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
250 case MCSA_ELF_TypeObject: OS << "object"; break;
251 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
252 case MCSA_ELF_TypeCommon: OS << "common"; break;
253 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
254 }
255 EmitEOL();
256 return;
257 case MCSA_Global: // .globl/.global
258 OS << MAI.getGlobalDirective();
259 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000260 case MCSA_Hidden: OS << ".hidden "; break;
261 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
262 case MCSA_Internal: OS << ".internal "; break;
263 case MCSA_LazyReference: OS << ".lazy_reference "; break;
264 case MCSA_Local: OS << ".local "; break;
265 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
266 case MCSA_PrivateExtern: OS << ".private_extern "; break;
267 case MCSA_Protected: OS << ".protected "; break;
268 case MCSA_Reference: OS << ".reference "; break;
269 case MCSA_Weak: OS << ".weak "; break;
270 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
271 // .weak_reference
272 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000273 }
274
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000275 OS << *Symbol;
276 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000277}
278
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000279void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000280 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
281 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000282}
283
Chris Lattner99328ad2010-01-25 07:52:13 +0000284void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
285 assert(MAI.hasDotTypeDotSizeDirective());
286 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
287}
288
Chris Lattner9eb158d2010-01-23 07:47:02 +0000289void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000290 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000291 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000292 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000293 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000294 OS << ',' << ByteAlignment;
295 else
296 OS << ',' << Log2_32(ByteAlignment);
297 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000298 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000299}
300
Chris Lattner9eb158d2010-01-23 07:47:02 +0000301/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
302///
303/// @param Symbol - The common symbol to emit.
304/// @param Size - The size of the common symbol.
305void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
306 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
307 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
308 EmitEOL();
309}
310
Daniel Dunbar8751b942009-08-28 05:48:22 +0000311void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000312 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000313 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000314 OS << ".zerofill ";
315
316 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000317 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000318 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000319
Chris Lattner9be3fee2009-07-10 22:20:30 +0000320 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000321 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000322 if (ByteAlignment != 0)
323 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000324 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000325 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000326}
327
Chris Lattner12e555c2010-01-23 00:15:00 +0000328static inline char toOctal(int X) { return (X&7)+'0'; }
329
Chris Lattnera6594fc2010-01-25 18:58:59 +0000330static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
331 OS << '"';
332
333 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
334 unsigned char C = Data[i];
335 if (C == '"' || C == '\\') {
336 OS << '\\' << (char)C;
337 continue;
338 }
339
340 if (isprint((unsigned char)C)) {
341 OS << (char)C;
342 continue;
343 }
344
345 switch (C) {
346 case '\b': OS << "\\b"; break;
347 case '\f': OS << "\\f"; break;
348 case '\n': OS << "\\n"; break;
349 case '\r': OS << "\\r"; break;
350 case '\t': OS << "\\t"; break;
351 default:
352 OS << '\\';
353 OS << toOctal(C >> 6);
354 OS << toOctal(C >> 3);
355 OS << toOctal(C >> 0);
356 break;
357 }
358 }
359
360 OS << '"';
361}
362
363
Chris Lattneraaec2052010-01-19 19:46:13 +0000364void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000365 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000366 if (Data.empty()) return;
367
368 if (Data.size() == 1) {
369 OS << MAI.getData8bitsDirective(AddrSpace);
370 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000371 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000372 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000373 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000374
375 // If the data ends with 0 and the target supports .asciz, use it, otherwise
376 // use .ascii
377 if (MAI.getAscizDirective() && Data.back() == 0) {
378 OS << MAI.getAscizDirective();
379 Data = Data.substr(0, Data.size()-1);
380 } else {
381 OS << MAI.getAsciiDirective();
382 }
383
Chris Lattnera6594fc2010-01-25 18:58:59 +0000384 OS << ' ';
385 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000386 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000387}
388
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000389/// EmitIntValue - Special case of EmitValue that avoids the client having
390/// to pass in a MCExpr for constant integers.
391void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
392 unsigned AddrSpace) {
393 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000394 const char *Directive = 0;
395 switch (Size) {
396 default: break;
397 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
398 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
399 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000400 case 8:
401 Directive = MAI.getData64bitsDirective(AddrSpace);
402 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
403 if (Directive) break;
404 if (isLittleEndian()) {
405 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
406 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
407 } else {
408 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
409 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
410 }
411 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000412 }
413
414 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000415 OS << Directive << truncateToSize(Value, Size);
416 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000417}
418
Chris Lattneraaec2052010-01-19 19:46:13 +0000419void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
420 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000421 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000422 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000423 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000424 default: break;
425 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
426 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
427 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
428 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000429 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000430
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000431 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000432 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000433 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000434}
435
Chris Lattner718fb592010-01-25 21:28:50 +0000436void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
437 assert(MAI.getGPRel32Directive() != 0);
438 OS << MAI.getGPRel32Directive() << *Value;
439 EmitEOL();
440}
441
442
Chris Lattner6113b3d2010-01-19 18:52:28 +0000443/// EmitFill - Emit NumBytes bytes worth of the value specified by
444/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000445void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
446 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000447 if (NumBytes == 0) return;
448
Chris Lattneraaec2052010-01-19 19:46:13 +0000449 if (AddrSpace == 0)
450 if (const char *ZeroDirective = MAI.getZeroDirective()) {
451 OS << ZeroDirective << NumBytes;
452 if (FillValue != 0)
453 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000454 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000455 return;
456 }
457
458 // Emit a byte at a time.
459 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000460}
461
Daniel Dunbar84a29262009-06-24 19:25:34 +0000462void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
463 unsigned ValueSize,
464 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000465 // Some assemblers don't support non-power of two alignments, so we always
466 // emit alignments as a power of two if possible.
467 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000468 switch (ValueSize) {
469 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000470 case 1: OS << MAI.getAlignDirective(); break;
471 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000472 case 2: OS << ".p2alignw "; break;
473 case 4: OS << ".p2alignl "; break;
474 case 8: llvm_unreachable("Unsupported alignment size!");
475 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000476
Chris Lattner33adcfb2009-08-22 21:43:10 +0000477 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000478 OS << ByteAlignment;
479 else
480 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000481
Chris Lattner663c2d22009-08-19 06:12:02 +0000482 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000483 OS << ", 0x";
484 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000485
486 if (MaxBytesToEmit)
487 OS << ", " << MaxBytesToEmit;
488 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000489 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000490 return;
491 }
492
493 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000494 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000495 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000496 default: llvm_unreachable("Invalid size for machine code value!");
497 case 1: OS << ".balign"; break;
498 case 2: OS << ".balignw"; break;
499 case 4: OS << ".balignl"; break;
500 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000501 }
502
Chris Lattner663c2d22009-08-19 06:12:02 +0000503 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000504 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000505 if (MaxBytesToEmit)
506 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000507 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000508}
509
Daniel Dunbar821e3332009-08-31 08:09:28 +0000510void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000511 unsigned char Value) {
512 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000513 OS << ".org " << *Offset << ", " << (unsigned) Value;
514 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000515}
516
Chris Lattnera6594fc2010-01-25 18:58:59 +0000517
518void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
519 assert(MAI.hasSingleParameterDotFile());
520 OS << "\t.file\t";
521 PrintQuotedString(Filename, OS);
522 EmitEOL();
523}
524
525void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
526 OS << "\t.file\t" << FileNo << ' ';
527 PrintQuotedString(Filename, OS);
528 EmitEOL();
529}
530
531
Daniel Dunbara11af532009-06-24 01:03:06 +0000532void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000533 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000534
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000535 // Show the MCInst if enabled.
536 if (ShowInst) {
537 raw_ostream &OS = GetCommentOS();
538 OS << "inst: ";
539 Inst.print(OS, &MAI);
540 OS << "\n";
541 }
542
Chris Lattner8ef2cef2010-02-03 06:28:13 +0000543 // Show the encoding in a comment if we have a code emitter.
544 if (Emitter) {
545 SmallString<256> Code;
546 raw_svector_ostream VecOS(Code);
547 Emitter->EncodeInstruction(Inst, VecOS);
548 VecOS.flush();
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000549
Chris Lattner8ef2cef2010-02-03 06:28:13 +0000550 raw_ostream &OS = GetCommentOS();
551 OS << "encoding: [";
552 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
553 if (i)
554 OS << ',';
555 OS << format("%#04x", uint8_t(Code[i]));
556 }
557 OS << "]\n";
558 }
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000559
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000560 // If we have an AsmPrinter, use that to print, otherwise dump the MCInst.
561 if (InstPrinter)
562 InstPrinter->printInst(&Inst);
563 else
564 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000565 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000566}
567
568void MCAsmStreamer::Finish() {
569 OS.flush();
570}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000571
Chris Lattner86e22112010-01-22 07:29:22 +0000572MCStreamer *llvm::createAsmStreamer(MCContext &Context,
573 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000574 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000575 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000576 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattner07404412010-01-22 07:06:15 +0000577 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000578 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000579}