blob: 5c11db72e2f40c89fea1330a8b38aa3fd5199d2f [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 Lattner07404412010-01-22 07:06:15 +000032 bool IsLittleEndian, IsVerboseAsm;
Chris Lattner90edac02009-09-14 03:02:37 +000033 MCInstPrinter *InstPrinter;
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000034 MCCodeEmitter *Emitter;
Chris Lattner86e22112010-01-22 07:29:22 +000035
36 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000037 raw_svector_ostream CommentStream;
Chris Lattner46a947d2009-08-17 04:17:34 +000038public:
Chris Lattner86e22112010-01-22 07:29:22 +000039 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
40 const MCAsmInfo &mai,
Chris Lattner07404412010-01-22 07:06:15 +000041 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
42 MCCodeEmitter *emitter)
43 : MCStreamer(Context), OS(os), MAI(mai), IsLittleEndian(isLittleEndian),
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000044 IsVerboseAsm(isVerboseAsm), InstPrinter(printer), Emitter(emitter),
45 CommentStream(CommentToEmit) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000046 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000047
Chris Lattner16582022010-01-20 06:39:07 +000048 bool isLittleEndian() const { return IsLittleEndian; }
49
Chris Lattner86e22112010-01-22 07:29:22 +000050 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000051 // If we don't have any comments, just emit a \n.
52 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000053 OS << '\n';
54 return;
55 }
56 EmitCommentsAndEOL();
57 }
58 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000059
60 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
61 /// all.
62 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Chris Lattner86e22112010-01-22 07:29:22 +000063
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000064 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000065 /// file if applicable as a QoI issue to make the output of the compiler
66 /// more readable. This only affects the MCAsmStreamer, and only when
67 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000068 virtual void AddComment(const Twine &T);
Chris Lattner86e22112010-01-22 07:29:22 +000069
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000070 /// GetCommentOS - Return a raw_ostream that comments can be written to.
71 /// Unlike AddComment, you are required to terminate comments with \n if you
72 /// use this method.
73 virtual raw_ostream &GetCommentOS() {
74 if (!IsVerboseAsm)
75 return nulls(); // Discard comments unless in verbose asm mode.
76 return CommentStream;
77 }
78
Chris Lattner0fd90fd2010-01-22 19:52:01 +000079 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
80 virtual void AddBlankLine() {
81 EmitEOL();
82 }
83
Chris Lattner46a947d2009-08-17 04:17:34 +000084 /// @name MCStreamer Interface
85 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000086
Chris Lattner975780b2009-08-17 05:49:08 +000087 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000088
Chris Lattner46a947d2009-08-17 04:17:34 +000089 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000090
Chris Lattnera5ad93a2010-01-23 06:39:22 +000091 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000092
Daniel Dunbar821e3332009-08-31 08:09:28 +000093 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000094
Chris Lattnera5ad93a2010-01-23 06:39:22 +000095 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000096
Chris Lattner46a947d2009-08-17 04:17:34 +000097 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000098
Chris Lattner99328ad2010-01-25 07:52:13 +000099 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000100 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000101 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000102
Chris Lattner9eb158d2010-01-23 07:47:02 +0000103 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
104 ///
105 /// @param Symbol - The common symbol to emit.
106 /// @param Size - The size of the common symbol.
107 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
108
Daniel Dunbar8751b942009-08-28 05:48:22 +0000109 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000110 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000111
Chris Lattneraaec2052010-01-19 19:46:13 +0000112 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000113
Chris Lattneraaec2052010-01-19 19:46:13 +0000114 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000115 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000116 virtual void EmitGPRel32Value(const MCExpr *Value);
117
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000118
Chris Lattneraaec2052010-01-19 19:46:13 +0000119 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
120 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000121
Chris Lattner46a947d2009-08-17 04:17:34 +0000122 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
123 unsigned ValueSize = 1,
124 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000125
Daniel Dunbar821e3332009-08-31 08:09:28 +0000126 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000127 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000128
Chris Lattnera6594fc2010-01-25 18:58:59 +0000129 virtual void EmitFileDirective(StringRef Filename);
130 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
131
132 virtual void EmitInstruction(const MCInst &Inst);
133
Chris Lattner46a947d2009-08-17 04:17:34 +0000134 virtual void Finish();
135
136 /// @}
137};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000138
Chris Lattner46a947d2009-08-17 04:17:34 +0000139} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000140
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000141/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000142/// file if applicable as a QoI issue to make the output of the compiler
143/// more readable. This only affects the MCAsmStreamer, and only when
144/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000145void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000146 if (!IsVerboseAsm) return;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000147
148 // Make sure that CommentStream is flushed.
149 CommentStream.flush();
150
Chris Lattner86e22112010-01-22 07:29:22 +0000151 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000152 // Each comment goes on its own line.
153 CommentToEmit.push_back('\n');
Chris Lattner14ca1772010-01-22 21:16:10 +0000154
155 // Tell the comment stream that the vector changed underneath it.
156 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000157}
158
159void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000160 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
161 OS << '\n';
162 return;
163 }
164
165 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000166 StringRef Comments = CommentToEmit.str();
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000167
168 assert(Comments.back() == '\n' &&
169 "Comment array not newline terminated");
170 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000171 // Emit a line of comments.
172 OS.PadToColumn(MAI.getCommentColumn());
173 size_t Position = Comments.find('\n');
174 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
175
Chris Lattner86e22112010-01-22 07:29:22 +0000176 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000177 } while (!Comments.empty());
Chris Lattner86e22112010-01-22 07:29:22 +0000178
Chris Lattner14ca1772010-01-22 21:16:10 +0000179 CommentToEmit.clear();
180 // Tell the comment stream that the vector changed underneath it.
181 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000182}
183
184
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000185static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
186 assert(Bytes && "Invalid size!");
187 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
188}
189
Chris Lattner975780b2009-08-17 05:49:08 +0000190void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000191 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000192 if (Section != CurSection) {
193 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000194 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000195 }
196}
197
198void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000199 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000200 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000201
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000202 OS << *Symbol << ":";
203 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000204 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000205}
206
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000207void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000208 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000209 default: assert(0 && "Invalid flag!");
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000210 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000211 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000212 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000213}
214
Daniel Dunbar821e3332009-08-31 08:09:28 +0000215void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000216 // Only absolute symbols can be redefined.
217 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
218 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000219
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000220 OS << *Symbol << " = " << *Value;
221 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000222
223 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000224 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000225 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000226}
227
Daniel Dunbarfffff912009-10-16 01:34:54 +0000228void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000229 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000230 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000231 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000232 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
233 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
234 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
235 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
236 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
237 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
238 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Chris Lattnera800f7c2010-01-25 18:33:40 +0000239 OS << "\t.type " << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000240 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
241 switch (Attribute) {
242 default: assert(0 && "Unknown ELF .type");
243 case MCSA_ELF_TypeFunction: OS << "function"; break;
244 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
245 case MCSA_ELF_TypeObject: OS << "object"; break;
246 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
247 case MCSA_ELF_TypeCommon: OS << "common"; break;
248 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
249 }
250 EmitEOL();
251 return;
252 case MCSA_Global: // .globl/.global
253 OS << MAI.getGlobalDirective();
254 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000255 case MCSA_Hidden: OS << ".hidden "; break;
256 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
257 case MCSA_Internal: OS << ".internal "; break;
258 case MCSA_LazyReference: OS << ".lazy_reference "; break;
259 case MCSA_Local: OS << ".local "; break;
260 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
261 case MCSA_PrivateExtern: OS << ".private_extern "; break;
262 case MCSA_Protected: OS << ".protected "; break;
263 case MCSA_Reference: OS << ".reference "; break;
264 case MCSA_Weak: OS << ".weak "; break;
265 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
266 // .weak_reference
267 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000268 }
269
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000270 OS << *Symbol;
271 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000272}
273
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000274void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000275 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
276 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000277}
278
Chris Lattner99328ad2010-01-25 07:52:13 +0000279void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
280 assert(MAI.hasDotTypeDotSizeDirective());
281 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
282}
283
Chris Lattner9eb158d2010-01-23 07:47:02 +0000284void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000285 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000286 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000287 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000288 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000289 OS << ',' << ByteAlignment;
290 else
291 OS << ',' << Log2_32(ByteAlignment);
292 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000293 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000294}
295
Chris Lattner9eb158d2010-01-23 07:47:02 +0000296/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
297///
298/// @param Symbol - The common symbol to emit.
299/// @param Size - The size of the common symbol.
300void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
301 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
302 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
303 EmitEOL();
304}
305
Daniel Dunbar8751b942009-08-28 05:48:22 +0000306void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000307 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000308 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000309 OS << ".zerofill ";
310
311 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000312 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000313 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000314
Chris Lattner9be3fee2009-07-10 22:20:30 +0000315 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000316 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000317 if (ByteAlignment != 0)
318 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000319 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000320 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000321}
322
Chris Lattner12e555c2010-01-23 00:15:00 +0000323static inline char toOctal(int X) { return (X&7)+'0'; }
324
Chris Lattnera6594fc2010-01-25 18:58:59 +0000325static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
326 OS << '"';
327
328 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
329 unsigned char C = Data[i];
330 if (C == '"' || C == '\\') {
331 OS << '\\' << (char)C;
332 continue;
333 }
334
335 if (isprint((unsigned char)C)) {
336 OS << (char)C;
337 continue;
338 }
339
340 switch (C) {
341 case '\b': OS << "\\b"; break;
342 case '\f': OS << "\\f"; break;
343 case '\n': OS << "\\n"; break;
344 case '\r': OS << "\\r"; break;
345 case '\t': OS << "\\t"; break;
346 default:
347 OS << '\\';
348 OS << toOctal(C >> 6);
349 OS << toOctal(C >> 3);
350 OS << toOctal(C >> 0);
351 break;
352 }
353 }
354
355 OS << '"';
356}
357
358
Chris Lattneraaec2052010-01-19 19:46:13 +0000359void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000360 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000361 if (Data.empty()) return;
362
363 if (Data.size() == 1) {
364 OS << MAI.getData8bitsDirective(AddrSpace);
365 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000366 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000367 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000368 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000369
370 // If the data ends with 0 and the target supports .asciz, use it, otherwise
371 // use .ascii
372 if (MAI.getAscizDirective() && Data.back() == 0) {
373 OS << MAI.getAscizDirective();
374 Data = Data.substr(0, Data.size()-1);
375 } else {
376 OS << MAI.getAsciiDirective();
377 }
378
Chris Lattnera6594fc2010-01-25 18:58:59 +0000379 OS << ' ';
380 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000381 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000382}
383
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000384/// EmitIntValue - Special case of EmitValue that avoids the client having
385/// to pass in a MCExpr for constant integers.
386void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
387 unsigned AddrSpace) {
388 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000389 const char *Directive = 0;
390 switch (Size) {
391 default: break;
392 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
393 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
394 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000395 case 8:
396 Directive = MAI.getData64bitsDirective(AddrSpace);
397 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
398 if (Directive) break;
399 if (isLittleEndian()) {
400 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
401 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
402 } else {
403 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
404 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
405 }
406 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000407 }
408
409 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000410 OS << Directive << truncateToSize(Value, Size);
411 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000412}
413
Chris Lattneraaec2052010-01-19 19:46:13 +0000414void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
415 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000416 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000417 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000418 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000419 default: break;
420 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
421 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
422 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
423 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000424 }
Chris Lattneraaec2052010-01-19 19:46:13 +0000425
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000426 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000427 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000428 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000429}
430
Chris Lattner718fb592010-01-25 21:28:50 +0000431void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
432 assert(MAI.getGPRel32Directive() != 0);
433 OS << MAI.getGPRel32Directive() << *Value;
434 EmitEOL();
435}
436
437
Chris Lattner6113b3d2010-01-19 18:52:28 +0000438/// EmitFill - Emit NumBytes bytes worth of the value specified by
439/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000440void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
441 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000442 if (NumBytes == 0) return;
443
Chris Lattneraaec2052010-01-19 19:46:13 +0000444 if (AddrSpace == 0)
445 if (const char *ZeroDirective = MAI.getZeroDirective()) {
446 OS << ZeroDirective << NumBytes;
447 if (FillValue != 0)
448 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000449 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000450 return;
451 }
452
453 // Emit a byte at a time.
454 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000455}
456
Daniel Dunbar84a29262009-06-24 19:25:34 +0000457void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
458 unsigned ValueSize,
459 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000460 // Some assemblers don't support non-power of two alignments, so we always
461 // emit alignments as a power of two if possible.
462 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000463 switch (ValueSize) {
464 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000465 case 1: OS << MAI.getAlignDirective(); break;
466 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000467 case 2: OS << ".p2alignw "; break;
468 case 4: OS << ".p2alignl "; break;
469 case 8: llvm_unreachable("Unsupported alignment size!");
470 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000471
Chris Lattner33adcfb2009-08-22 21:43:10 +0000472 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000473 OS << ByteAlignment;
474 else
475 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000476
Chris Lattner663c2d22009-08-19 06:12:02 +0000477 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000478 OS << ", 0x";
479 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000480
481 if (MaxBytesToEmit)
482 OS << ", " << MaxBytesToEmit;
483 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000484 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000485 return;
486 }
487
488 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000489 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000490 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000491 default: llvm_unreachable("Invalid size for machine code value!");
492 case 1: OS << ".balign"; break;
493 case 2: OS << ".balignw"; break;
494 case 4: OS << ".balignl"; break;
495 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000496 }
497
Chris Lattner663c2d22009-08-19 06:12:02 +0000498 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000499 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000500 if (MaxBytesToEmit)
501 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000502 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000503}
504
Daniel Dunbar821e3332009-08-31 08:09:28 +0000505void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000506 unsigned char Value) {
507 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000508 OS << ".org " << *Offset << ", " << (unsigned) Value;
509 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000510}
511
Chris Lattnera6594fc2010-01-25 18:58:59 +0000512
513void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
514 assert(MAI.hasSingleParameterDotFile());
515 OS << "\t.file\t";
516 PrintQuotedString(Filename, OS);
517 EmitEOL();
518}
519
520void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
521 OS << "\t.file\t" << FileNo << ' ';
522 PrintQuotedString(Filename, OS);
523 EmitEOL();
524}
525
526
Daniel Dunbara11af532009-06-24 01:03:06 +0000527void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000528 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000529
Chris Lattner8ef2cef2010-02-03 06:28:13 +0000530 // Show the encoding in a comment if we have a code emitter.
531 if (Emitter) {
532 SmallString<256> Code;
533 raw_svector_ostream VecOS(Code);
534 Emitter->EncodeInstruction(Inst, VecOS);
535 VecOS.flush();
536
537 raw_ostream &OS = GetCommentOS();
538 OS << "encoding: [";
539 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
540 if (i)
541 OS << ',';
542 OS << format("%#04x", uint8_t(Code[i]));
543 }
544 OS << "]\n";
545 }
546
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000547 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000548 if (InstPrinter) {
549 InstPrinter->printInst(&Inst);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000550 EmitEOL();
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000551 return;
552 }
553
554 // Otherwise fall back to a structural printing for now. Eventually we should
555 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000556 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000557 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000558}
559
560void MCAsmStreamer::Finish() {
561 OS.flush();
562}
563
Chris Lattner86e22112010-01-22 07:29:22 +0000564MCStreamer *llvm::createAsmStreamer(MCContext &Context,
565 formatted_raw_ostream &OS,
Chris Lattner16582022010-01-20 06:39:07 +0000566 const MCAsmInfo &MAI, bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000567 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000568 MCCodeEmitter *CE) {
Chris Lattner07404412010-01-22 07:06:15 +0000569 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
570 IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000571}