blob: 8ee5542d369492bea830022512d4d304b3311f0c [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 Lattner4c42a6d2010-03-19 05:48:53 +000019#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000023#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000024#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000025#include "llvm/Support/FormattedStream.h"
Rafael Espindola195a0ce2010-11-19 02:26:16 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000027using namespace llvm;
28
29namespace {
30
Chris Lattner46a947d2009-08-17 04:17:34 +000031class MCAsmStreamer : public MCStreamer {
Chris Lattner86e22112010-01-22 07:29:22 +000032 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000033 const MCAsmInfo &MAI;
Chris Lattner4c42a6d2010-03-19 05:48:53 +000034 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000035 OwningPtr<MCCodeEmitter> Emitter;
Jim Grosbach00545e12010-09-22 18:16:55 +000036
Chris Lattner86e22112010-01-22 07:29:22 +000037 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000038 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000039
Rafael Espindola195a0ce2010-11-19 02:26:16 +000040 const TargetLoweringObjectFile *TLOF;
41 int PointerSize;
42
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000043 unsigned IsLittleEndian : 1;
44 unsigned IsVerboseAsm : 1;
45 unsigned ShowInst : 1;
46
Chris Lattner46a947d2009-08-17 04:17:34 +000047public:
Chris Lattner86e22112010-01-22 07:29:22 +000048 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindola195a0ce2010-11-19 02:26:16 +000049 bool isLittleEndian, bool isVerboseAsm,
50 const TargetLoweringObjectFile *tlof, int pointerSize,
51 MCInstPrinter *printer, MCCodeEmitter *emitter, bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000052 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
53 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
Rafael Espindola195a0ce2010-11-19 02:26:16 +000054 TLOF(tlof), PointerSize(pointerSize),
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000055 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
Daniel Dunbar5532cf42010-02-10 01:41:14 +000056 ShowInst(showInst) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000057 if (InstPrinter && IsVerboseAsm)
58 InstPrinter->setCommentStream(CommentStream);
59 }
Chris Lattner46a947d2009-08-17 04:17:34 +000060 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000061
Chris Lattner16582022010-01-20 06:39:07 +000062 bool isLittleEndian() const { return IsLittleEndian; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000063
Chris Lattner86e22112010-01-22 07:29:22 +000064 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000065 // If we don't have any comments, just emit a \n.
66 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000067 OS << '\n';
68 return;
69 }
70 EmitCommentsAndEOL();
71 }
72 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000073
74 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
75 /// all.
76 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000077
Chris Lattner91bead72010-04-03 21:35:55 +000078 /// hasRawTextSupport - We support EmitRawText.
79 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000080
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000081 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000082 /// file if applicable as a QoI issue to make the output of the compiler
83 /// more readable. This only affects the MCAsmStreamer, and only when
84 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000085 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000086
87 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
88 virtual void AddEncodingComment(const MCInst &Inst);
89
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000090 /// GetCommentOS - Return a raw_ostream that comments can be written to.
91 /// Unlike AddComment, you are required to terminate comments with \n if you
92 /// use this method.
93 virtual raw_ostream &GetCommentOS() {
94 if (!IsVerboseAsm)
95 return nulls(); // Discard comments unless in verbose asm mode.
96 return CommentStream;
97 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000098
Chris Lattner0fd90fd2010-01-22 19:52:01 +000099 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
100 virtual void AddBlankLine() {
101 EmitEOL();
102 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000103
Chris Lattner46a947d2009-08-17 04:17:34 +0000104 /// @name MCStreamer Interface
105 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000106
Chris Lattner975780b2009-08-17 05:49:08 +0000107 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000108
Rafael Espindolad80781b2010-09-15 21:48:40 +0000109 virtual void InitSections() {
110 // FIXME, this is MachO specific, but the testsuite
111 // expects this.
112 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
113 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
114 0, SectionKind::getText()));
115 }
116
Chris Lattner46a947d2009-08-17 04:17:34 +0000117 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000119 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000120 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000121
Daniel Dunbar821e3332009-08-31 08:09:28 +0000122 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000123 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000124
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000125 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000126
Chris Lattner46a947d2009-08-17 04:17:34 +0000127 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000128 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
129 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
130 virtual void EmitCOFFSymbolType(int Type);
131 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000132 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000133 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000134 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000135
Chris Lattner9eb158d2010-01-23 07:47:02 +0000136 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
137 ///
138 /// @param Symbol - The common symbol to emit.
139 /// @param Size - The size of the common symbol.
140 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000141
Daniel Dunbar8751b942009-08-28 05:48:22 +0000142 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000143 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000144
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000145 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
146 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000147
Chris Lattneraaec2052010-01-19 19:46:13 +0000148 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000149
Chris Lattneraaec2052010-01-19 19:46:13 +0000150 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000151
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000152 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000153
154 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
155
156 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
157
Chris Lattner718fb592010-01-25 21:28:50 +0000158 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000159
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000160
Chris Lattneraaec2052010-01-19 19:46:13 +0000161 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
162 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000163
Chris Lattner46a947d2009-08-17 04:17:34 +0000164 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
165 unsigned ValueSize = 1,
166 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000167
Kevin Enderby6e720482010-02-23 18:26:34 +0000168 virtual void EmitCodeAlignment(unsigned ByteAlignment,
169 unsigned MaxBytesToEmit = 0);
170
Daniel Dunbar821e3332009-08-31 08:09:28 +0000171 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000172 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000173
Chris Lattnera6594fc2010-01-25 18:58:59 +0000174 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000175 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
176 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
177 unsigned Column, unsigned Flags,
178 unsigned Isa, unsigned Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000179
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000180 virtual bool EmitCFIStartProc();
181 virtual bool EmitCFIEndProc();
182 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
183 virtual bool EmitCFIDefCfaRegister(int64_t Register);
184 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
185 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
186 virtual bool EmitCFILsda(const MCSymbol *Sym);
187
Chris Lattnera6594fc2010-01-25 18:58:59 +0000188 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000189
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000190 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000191 /// the specified string in the output .s file. This capability is
192 /// indicated by the hasRawTextSupport() predicate.
193 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000194
Chris Lattner46a947d2009-08-17 04:17:34 +0000195 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000196
Chris Lattner46a947d2009-08-17 04:17:34 +0000197 /// @}
198};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000199
Chris Lattner46a947d2009-08-17 04:17:34 +0000200} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000201
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000202/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000203/// file if applicable as a QoI issue to make the output of the compiler
204/// more readable. This only affects the MCAsmStreamer, and only when
205/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000206void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000207 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000208
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000209 // Make sure that CommentStream is flushed.
210 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000211
Chris Lattner86e22112010-01-22 07:29:22 +0000212 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000213 // Each comment goes on its own line.
214 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000215
Chris Lattner14ca1772010-01-22 21:16:10 +0000216 // Tell the comment stream that the vector changed underneath it.
217 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000218}
219
220void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000221 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
222 OS << '\n';
223 return;
224 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000225
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000226 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000227 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000228
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000229 assert(Comments.back() == '\n' &&
230 "Comment array not newline terminated");
231 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000232 // Emit a line of comments.
233 OS.PadToColumn(MAI.getCommentColumn());
234 size_t Position = Comments.find('\n');
235 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000236
Chris Lattner86e22112010-01-22 07:29:22 +0000237 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000238 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000239
Chris Lattner14ca1772010-01-22 21:16:10 +0000240 CommentToEmit.clear();
241 // Tell the comment stream that the vector changed underneath it.
242 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000243}
244
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000245static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
246 assert(Bytes && "Invalid size!");
247 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
248}
249
Chris Lattner975780b2009-08-17 05:49:08 +0000250void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000251 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000252 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000253 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000254 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000255 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000256 }
257}
258
259void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000260 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000261 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000262 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000263
Chris Lattnere07b75e2010-09-22 22:19:53 +0000264 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000265 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000266 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000267}
268
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000269void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000270 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000271 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000272 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000273 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000274 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000275 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000276 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000277 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000278}
279
Jim Grosbachce792992010-11-05 22:08:08 +0000280void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
281 // This needs to emit to a temporary string to get properly quoted
282 // MCSymbols when they have spaces in them.
283 OS << "\t.thumb_func";
284 if (Func)
285 OS << '\t' << *Func;
286 EmitEOL();
287}
288
Daniel Dunbar821e3332009-08-31 08:09:28 +0000289void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000290 OS << *Symbol << " = " << *Value;
291 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000292
293 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000294 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000295}
296
Rafael Espindola484291c2010-11-01 14:28:48 +0000297void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
298 OS << ".weakref " << *Alias << ", " << *Symbol;
299 EmitEOL();
300}
301
Daniel Dunbarfffff912009-10-16 01:34:54 +0000302void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000303 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000304 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000305 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000306 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
307 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
308 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
309 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
310 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
311 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000312 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000313 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000314 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000315 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
316 switch (Attribute) {
317 default: assert(0 && "Unknown ELF .type");
318 case MCSA_ELF_TypeFunction: OS << "function"; break;
319 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
320 case MCSA_ELF_TypeObject: OS << "object"; break;
321 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
322 case MCSA_ELF_TypeCommon: OS << "common"; break;
323 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000324 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000325 }
326 EmitEOL();
327 return;
328 case MCSA_Global: // .globl/.global
329 OS << MAI.getGlobalDirective();
330 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000331 case MCSA_Hidden: OS << "\t.hidden\t"; break;
332 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
333 case MCSA_Internal: OS << "\t.internal\t"; break;
334 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
335 case MCSA_Local: OS << "\t.local\t"; break;
336 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000337 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000338 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
339 case MCSA_Protected: OS << "\t.protected\t"; break;
340 case MCSA_Reference: OS << "\t.reference\t"; break;
341 case MCSA_Weak: OS << "\t.weak\t"; break;
342 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000343 // .weak_reference
344 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000345 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000346 }
347
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000348 OS << *Symbol;
349 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000350}
351
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000352void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000353 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
354 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000355}
356
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000357void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
358 OS << "\t.def\t " << *Symbol << ';';
359 EmitEOL();
360}
361
362void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
363 OS << "\t.scl\t" << StorageClass << ';';
364 EmitEOL();
365}
366
367void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
368 OS << "\t.type\t" << Type << ';';
369 EmitEOL();
370}
371
372void MCAsmStreamer::EndCOFFSymbolDef() {
373 OS << "\t.endef";
374 EmitEOL();
375}
376
Chris Lattner99328ad2010-01-25 07:52:13 +0000377void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
378 assert(MAI.hasDotTypeDotSizeDirective());
379 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
380}
381
Chris Lattner9eb158d2010-01-23 07:47:02 +0000382void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000383 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000384 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000385 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000386 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000387 OS << ',' << ByteAlignment;
388 else
389 OS << ',' << Log2_32(ByteAlignment);
390 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000391 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000392}
393
Chris Lattner9eb158d2010-01-23 07:47:02 +0000394/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
395///
396/// @param Symbol - The common symbol to emit.
397/// @param Size - The size of the common symbol.
398void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
399 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
400 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
401 EmitEOL();
402}
403
Daniel Dunbar8751b942009-08-28 05:48:22 +0000404void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000405 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000406 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000407 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000408
Chris Lattner93b6db32009-08-08 23:39:42 +0000409 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000410 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000411 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000412
Chris Lattner9be3fee2009-07-10 22:20:30 +0000413 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000414 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000415 if (ByteAlignment != 0)
416 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000417 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000418 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000419}
420
Eric Christopherd04d98d2010-05-17 02:13:02 +0000421// .tbss sym, size, align
422// This depends that the symbol has already been mangled from the original,
423// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000424void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
425 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000426 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000427 // Instead of using the Section we'll just use the shortcut.
428 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000429 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000430
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000431 // Output align if we have it. We default to 1 so don't bother printing
432 // that.
433 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000434
Eric Christopher482eba02010-05-14 01:50:28 +0000435 EmitEOL();
436}
437
Chris Lattner12e555c2010-01-23 00:15:00 +0000438static inline char toOctal(int X) { return (X&7)+'0'; }
439
Chris Lattnera6594fc2010-01-25 18:58:59 +0000440static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
441 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000442
Chris Lattnera6594fc2010-01-25 18:58:59 +0000443 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
444 unsigned char C = Data[i];
445 if (C == '"' || C == '\\') {
446 OS << '\\' << (char)C;
447 continue;
448 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000449
Chris Lattnera6594fc2010-01-25 18:58:59 +0000450 if (isprint((unsigned char)C)) {
451 OS << (char)C;
452 continue;
453 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000454
Chris Lattnera6594fc2010-01-25 18:58:59 +0000455 switch (C) {
456 case '\b': OS << "\\b"; break;
457 case '\f': OS << "\\f"; break;
458 case '\n': OS << "\\n"; break;
459 case '\r': OS << "\\r"; break;
460 case '\t': OS << "\\t"; break;
461 default:
462 OS << '\\';
463 OS << toOctal(C >> 6);
464 OS << toOctal(C >> 3);
465 OS << toOctal(C >> 0);
466 break;
467 }
468 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000469
Chris Lattnera6594fc2010-01-25 18:58:59 +0000470 OS << '"';
471}
472
473
Chris Lattneraaec2052010-01-19 19:46:13 +0000474void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000475 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000476 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000477
Chris Lattner12e555c2010-01-23 00:15:00 +0000478 if (Data.size() == 1) {
479 OS << MAI.getData8bitsDirective(AddrSpace);
480 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000481 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000482 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000483 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000484
485 // If the data ends with 0 and the target supports .asciz, use it, otherwise
486 // use .ascii
487 if (MAI.getAscizDirective() && Data.back() == 0) {
488 OS << MAI.getAscizDirective();
489 Data = Data.substr(0, Data.size()-1);
490 } else {
491 OS << MAI.getAsciiDirective();
492 }
493
Chris Lattnera6594fc2010-01-25 18:58:59 +0000494 OS << ' ';
495 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000496 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000497}
498
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000499/// EmitIntValue - Special case of EmitValue that avoids the client having
500/// to pass in a MCExpr for constant integers.
501void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
502 unsigned AddrSpace) {
503 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000504 const char *Directive = 0;
505 switch (Size) {
506 default: break;
507 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
508 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
509 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000510 case 8:
511 Directive = MAI.getData64bitsDirective(AddrSpace);
512 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
513 if (Directive) break;
514 if (isLittleEndian()) {
515 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
516 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
517 } else {
518 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
519 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
520 }
521 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000522 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000523
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000524 assert(Directive && "Invalid size for machine code value!");
Chris Lattner86e22112010-01-22 07:29:22 +0000525 OS << Directive << truncateToSize(Value, Size);
526 EmitEOL();
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000527}
528
Chris Lattneraaec2052010-01-19 19:46:13 +0000529void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
530 unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000531 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000532 const char *Directive = 0;
Daniel Dunbara11af532009-06-24 01:03:06 +0000533 switch (Size) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000534 default: break;
535 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
536 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
537 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
538 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000539 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000540
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000541 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000542 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000543 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000544}
545
Rafael Espindola3ff57092010-11-02 17:22:24 +0000546void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000547 int64_t IntValue;
548 if (Value->EvaluateAsAbsolute(IntValue)) {
549 SmallString<32> Tmp;
550 raw_svector_ostream OSE(Tmp);
551 MCObjectWriter::EncodeULEB128(IntValue, OSE);
552 EmitBytes(OSE.str(), AddrSpace);
553 return;
554 }
Rafael Espindola73873452010-11-04 18:17:08 +0000555 assert(MAI.hasLEB128() && "Cannot print a .uleb");
556 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000557 EmitEOL();
558}
559
560void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000561 int64_t IntValue;
562 if (Value->EvaluateAsAbsolute(IntValue)) {
563 SmallString<32> Tmp;
564 raw_svector_ostream OSE(Tmp);
565 MCObjectWriter::EncodeSLEB128(IntValue, OSE);
566 EmitBytes(OSE.str(), AddrSpace);
567 return;
568 }
Rafael Espindola73873452010-11-04 18:17:08 +0000569 assert(MAI.hasLEB128() && "Cannot print a .sleb");
570 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000571 EmitEOL();
572}
573
Chris Lattner718fb592010-01-25 21:28:50 +0000574void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
575 assert(MAI.getGPRel32Directive() != 0);
576 OS << MAI.getGPRel32Directive() << *Value;
577 EmitEOL();
578}
579
580
Chris Lattner6113b3d2010-01-19 18:52:28 +0000581/// EmitFill - Emit NumBytes bytes worth of the value specified by
582/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000583void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
584 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000585 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000586
Chris Lattneraaec2052010-01-19 19:46:13 +0000587 if (AddrSpace == 0)
588 if (const char *ZeroDirective = MAI.getZeroDirective()) {
589 OS << ZeroDirective << NumBytes;
590 if (FillValue != 0)
591 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000592 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000593 return;
594 }
595
596 // Emit a byte at a time.
597 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000598}
599
Daniel Dunbar84a29262009-06-24 19:25:34 +0000600void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
601 unsigned ValueSize,
602 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000603 // Some assemblers don't support non-power of two alignments, so we always
604 // emit alignments as a power of two if possible.
605 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000606 switch (ValueSize) {
607 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000608 case 1: OS << MAI.getAlignDirective(); break;
609 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000610 case 2: OS << ".p2alignw "; break;
611 case 4: OS << ".p2alignl "; break;
612 case 8: llvm_unreachable("Unsupported alignment size!");
613 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000614
Chris Lattner33adcfb2009-08-22 21:43:10 +0000615 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000616 OS << ByteAlignment;
617 else
618 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000619
Chris Lattner663c2d22009-08-19 06:12:02 +0000620 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000621 OS << ", 0x";
622 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000623
Jim Grosbach00545e12010-09-22 18:16:55 +0000624 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000625 OS << ", " << MaxBytesToEmit;
626 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000627 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000628 return;
629 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000630
Chris Lattner663c2d22009-08-19 06:12:02 +0000631 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000632 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000633 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000634 default: llvm_unreachable("Invalid size for machine code value!");
635 case 1: OS << ".balign"; break;
636 case 2: OS << ".balignw"; break;
637 case 4: OS << ".balignl"; break;
638 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000639 }
640
Chris Lattner663c2d22009-08-19 06:12:02 +0000641 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000642 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000643 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000644 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000645 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000646}
647
Kevin Enderby6e720482010-02-23 18:26:34 +0000648void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
649 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000650 // Emit with a text fill value.
651 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
652 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000653}
654
Daniel Dunbar821e3332009-08-31 08:09:28 +0000655void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000656 unsigned char Value) {
657 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000658 OS << ".org " << *Offset << ", " << (unsigned) Value;
659 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000660}
661
Chris Lattnera6594fc2010-01-25 18:58:59 +0000662
663void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
664 assert(MAI.hasSingleParameterDotFile());
665 OS << "\t.file\t";
666 PrintQuotedString(Filename, OS);
667 EmitEOL();
668}
669
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000670bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000671 if (!TLOF) {
672 OS << "\t.file\t" << FileNo << ' ';
673 PrintQuotedString(Filename, OS);
674 EmitEOL();
675 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000676 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
677}
678
679void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
680 unsigned Column, unsigned Flags,
681 unsigned Isa,
682 unsigned Discriminator) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000683 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
684 Isa, Discriminator);
685 if (TLOF)
686 return;
687
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000688 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
689 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
690 OS << " basic_block";
691 if (Flags & DWARF2_FLAG_PROLOGUE_END)
692 OS << " prologue_end";
693 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
694 OS << " epilogue_begin";
695
696 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
697 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
698 OS << " is_stmt ";
699
700 if (Flags & DWARF2_FLAG_IS_STMT)
701 OS << "1";
702 else
703 OS << "0";
704 }
705
706 if (Isa)
707 OS << "isa " << Isa;
708 if (Discriminator)
709 OS << "discriminator " << Discriminator;
710 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000711}
712
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000713bool MCAsmStreamer::EmitCFIStartProc() {
714 if (this->MCStreamer::EmitCFIStartProc())
715 return true;
716
717 OS << ".cfi_startproc";
718 EmitEOL();
719
720 return false;
721}
722
723bool MCAsmStreamer::EmitCFIEndProc() {
724 if (this->MCStreamer::EmitCFIEndProc())
725 return true;
726
727 OS << ".cfi_endproc";
728 EmitEOL();
729
730 return false;
731}
732
733bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
734 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
735 return true;
736
737 OS << ".cfi_def_cfa_offset " << Offset;
738 EmitEOL();
739
740 return false;
741}
742
743bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
744 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
745 return true;
746
747 OS << ".cfi_def_cfa_register " << Register;
748 EmitEOL();
749
750 return false;
751}
752
753bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
754 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
755 return true;
756
757 OS << ".cfi_offset " << Register << ", " << Offset;
758 EmitEOL();
759
760 return false;
761}
762
763bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
764 if (this->MCStreamer::EmitCFIPersonality(Sym))
765 return true;
766
767 OS << ".cfi_personality 0, " << *Sym;
768 EmitEOL();
769
770 return false;
771}
772
773bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
774 if (this->MCStreamer::EmitCFILsda(Sym))
775 return true;
776
777 OS << ".cfi_lsda 0, " << *Sym;
778 EmitEOL();
779
780 return false;
781}
782
Daniel Dunbar6b716532010-02-09 23:00:14 +0000783void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
784 raw_ostream &OS = GetCommentOS();
785 SmallString<256> Code;
786 SmallVector<MCFixup, 4> Fixups;
787 raw_svector_ostream VecOS(Code);
788 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
789 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000790
Daniel Dunbar6b716532010-02-09 23:00:14 +0000791 // If we are showing fixups, create symbolic markers in the encoded
792 // representation. We do this by making a per-bit map to the fixup item index,
793 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000794 SmallVector<uint8_t, 64> FixupMap;
795 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000796 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
797 FixupMap[i] = 0;
798
799 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
800 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000801 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000802 for (unsigned j = 0; j != Info.TargetSize; ++j) {
803 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
804 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
805 FixupMap[Index] = 1 + i;
806 }
807 }
808
809 OS << "encoding: [";
810 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
811 if (i)
812 OS << ',';
813
814 // See if all bits are the same map entry.
815 uint8_t MapEntry = FixupMap[i * 8 + 0];
816 for (unsigned j = 1; j != 8; ++j) {
817 if (FixupMap[i * 8 + j] == MapEntry)
818 continue;
819
820 MapEntry = uint8_t(~0U);
821 break;
822 }
823
824 if (MapEntry != uint8_t(~0U)) {
825 if (MapEntry == 0) {
826 OS << format("0x%02x", uint8_t(Code[i]));
827 } else {
828 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
829 OS << char('A' + MapEntry - 1);
830 }
831 } else {
832 // Otherwise, write out in binary.
833 OS << "0b";
834 for (unsigned j = 8; j--;) {
835 unsigned Bit = (Code[i] >> j) & 1;
Chris Lattner3170a3b2010-11-15 05:56:19 +0000836
837 unsigned FixupBit;
838 if (IsLittleEndian)
839 FixupBit = i * 8 + j;
840 else
841 FixupBit = i * 8 + (7-j);
842
843 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +0000844 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
845 OS << char('A' + MapEntry - 1);
846 } else
847 OS << Bit;
848 }
849 }
850 }
851 OS << "]\n";
852
853 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
854 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000855 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000856 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000857 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000858 }
859}
860
861void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
862 assert(CurSection && "Cannot emit contents before setting section!");
863
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000864 if (TLOF)
865 MCLineEntry::Make(this, getCurrentSection());
866
Daniel Dunbar6b716532010-02-09 23:00:14 +0000867 // Show the encoding in a comment if we have a code emitter.
868 if (Emitter)
869 AddEncodingComment(Inst);
870
Chris Lattner30d9a642010-02-09 00:54:51 +0000871 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000872 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000873 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000874 GetCommentOS() << "\n";
875 }
876
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000877 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000878 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000879 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000880 else
881 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000882 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000883}
884
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000885/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000886/// the specified string in the output .s file. This capability is
887/// indicated by the hasRawTextSupport() predicate.
888void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000889 if (!String.empty() && String.back() == '\n')
890 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000891 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000892 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000893}
894
Daniel Dunbara11af532009-06-24 01:03:06 +0000895void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000896 // Dump out the dwarf file & directory tables and line tables.
897 if (getContext().hasDwarfFiles() && TLOF) {
898 MCDwarfFileTable::Emit(this, TLOF->getDwarfLineSection(), NULL,
899 PointerSize);
900 }
Daniel Dunbara11af532009-06-24 01:03:06 +0000901}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000902
Chris Lattner86e22112010-01-22 07:29:22 +0000903MCStreamer *llvm::createAsmStreamer(MCContext &Context,
904 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000905 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000906 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000907 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000908 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000909 NULL, 0, IP, CE, ShowInst);
910}
911
912
913MCStreamer *llvm::createAsmStreamerNoLoc(MCContext &Context,
914 formatted_raw_ostream &OS,
915 bool isLittleEndian,
916 bool isVerboseAsm,
917 const TargetLoweringObjectFile *TLOF,
918 int PointerSize,
919 MCInstPrinter *IP,
920 MCCodeEmitter *CE, bool ShowInst) {
921 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
922 TLOF, PointerSize, IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000923}