blob: ce85e462b3d161a7c5b5caf6ecd9772ad7b99772 [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 Espindolaf7fd4aa2010-12-10 04:01:09 +000040 const TargetLoweringObjectFile *TLOF;
41 int PointerSize;
42
43 unsigned IsLittleEndian : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000044 unsigned IsVerboseAsm : 1;
45 unsigned ShowInst : 1;
46
Rafael Espindola5d4918d2010-12-04 03:21:47 +000047 bool needsSet(const MCExpr *Value);
48
Chris Lattner46a947d2009-08-17 04:17:34 +000049public:
Chris Lattner86e22112010-01-22 07:29:22 +000050 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +000051 bool isLittleEndian, bool isVerboseAsm,
52 const TargetLoweringObjectFile *tlof, int pointerSize,
Rafael Espindola195a0ce2010-11-19 02:26:16 +000053 MCInstPrinter *printer, MCCodeEmitter *emitter, bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000054 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
55 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +000056 TLOF(tlof), PointerSize(pointerSize),
57 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
58 ShowInst(showInst) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000059 if (InstPrinter && IsVerboseAsm)
60 InstPrinter->setCommentStream(CommentStream);
61 }
Chris Lattner46a947d2009-08-17 04:17:34 +000062 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000063
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +000064 bool isLittleEndian() const { return IsLittleEndian; }
65
Chris Lattner86e22112010-01-22 07:29:22 +000066 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000067 // If we don't have any comments, just emit a \n.
68 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000069 OS << '\n';
70 return;
71 }
72 EmitCommentsAndEOL();
73 }
74 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000075
76 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
77 /// all.
78 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000079
Chris Lattner91bead72010-04-03 21:35:55 +000080 /// hasRawTextSupport - We support EmitRawText.
81 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000082
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000083 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000084 /// file if applicable as a QoI issue to make the output of the compiler
85 /// more readable. This only affects the MCAsmStreamer, and only when
86 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000087 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000088
89 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
90 virtual void AddEncodingComment(const MCInst &Inst);
91
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000092 /// GetCommentOS - Return a raw_ostream that comments can be written to.
93 /// Unlike AddComment, you are required to terminate comments with \n if you
94 /// use this method.
95 virtual raw_ostream &GetCommentOS() {
96 if (!IsVerboseAsm)
97 return nulls(); // Discard comments unless in verbose asm mode.
98 return CommentStream;
99 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000100
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000101 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
102 virtual void AddBlankLine() {
103 EmitEOL();
104 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000105
Chris Lattner46a947d2009-08-17 04:17:34 +0000106 /// @name MCStreamer Interface
107 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000108
Chris Lattner975780b2009-08-17 05:49:08 +0000109 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000110
Rafael Espindolad80781b2010-09-15 21:48:40 +0000111 virtual void InitSections() {
112 // FIXME, this is MachO specific, but the testsuite
113 // expects this.
114 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
115 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
116 0, SectionKind::getText()));
117 }
118
Chris Lattner46a947d2009-08-17 04:17:34 +0000119 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000120
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000121 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000122 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000123
Daniel Dunbar821e3332009-08-31 08:09:28 +0000124 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000125 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000126 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
127 const MCSymbol *LastLabel,
128 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000129
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000130 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000131
Chris Lattner46a947d2009-08-17 04:17:34 +0000132 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000133 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
134 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
135 virtual void EmitCOFFSymbolType(int Type);
136 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000137 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000138 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000139 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000140
Chris Lattner9eb158d2010-01-23 07:47:02 +0000141 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
142 ///
143 /// @param Symbol - The common symbol to emit.
144 /// @param Size - The size of the common symbol.
145 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000146
Daniel Dunbar8751b942009-08-28 05:48:22 +0000147 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000148 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000149
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000150 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
151 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000152
Chris Lattneraaec2052010-01-19 19:46:13 +0000153 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000154
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000155 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000156 virtual void EmitIntValue(uint64_t Value, unsigned Size,
157 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000158
Rafael Espindola3ff57092010-11-02 17:22:24 +0000159 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
160
161 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
162
Chris Lattner718fb592010-01-25 21:28:50 +0000163 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000164
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000165
Chris Lattneraaec2052010-01-19 19:46:13 +0000166 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
167 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000168
Chris Lattner46a947d2009-08-17 04:17:34 +0000169 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
170 unsigned ValueSize = 1,
171 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000172
Kevin Enderby6e720482010-02-23 18:26:34 +0000173 virtual void EmitCodeAlignment(unsigned ByteAlignment,
174 unsigned MaxBytesToEmit = 0);
175
Daniel Dunbar821e3332009-08-31 08:09:28 +0000176 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000177 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000178
Chris Lattnera6594fc2010-01-25 18:58:59 +0000179 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000180 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
181 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
182 unsigned Column, unsigned Flags,
183 unsigned Isa, unsigned Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000184
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000185 virtual bool EmitCFIStartProc();
186 virtual bool EmitCFIEndProc();
187 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
188 virtual bool EmitCFIDefCfaRegister(int64_t Register);
189 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
190 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
191 virtual bool EmitCFILsda(const MCSymbol *Sym);
192
Chris Lattnera6594fc2010-01-25 18:58:59 +0000193 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000194
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000195 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000196 /// the specified string in the output .s file. This capability is
197 /// indicated by the hasRawTextSupport() predicate.
198 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000199
Chris Lattner46a947d2009-08-17 04:17:34 +0000200 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000201
Chris Lattner46a947d2009-08-17 04:17:34 +0000202 /// @}
203};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000204
Chris Lattner46a947d2009-08-17 04:17:34 +0000205} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000206
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000207/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000208/// file if applicable as a QoI issue to make the output of the compiler
209/// more readable. This only affects the MCAsmStreamer, and only when
210/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000211void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000212 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000213
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000214 // Make sure that CommentStream is flushed.
215 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000216
Chris Lattner86e22112010-01-22 07:29:22 +0000217 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000218 // Each comment goes on its own line.
219 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000220
Chris Lattner14ca1772010-01-22 21:16:10 +0000221 // Tell the comment stream that the vector changed underneath it.
222 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000223}
224
225void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000226 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
227 OS << '\n';
228 return;
229 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000230
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000231 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000232 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000233
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000234 assert(Comments.back() == '\n' &&
235 "Comment array not newline terminated");
236 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000237 // Emit a line of comments.
238 OS.PadToColumn(MAI.getCommentColumn());
239 size_t Position = Comments.find('\n');
240 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000241
Chris Lattner86e22112010-01-22 07:29:22 +0000242 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000243 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000244
Chris Lattner14ca1772010-01-22 21:16:10 +0000245 CommentToEmit.clear();
246 // Tell the comment stream that the vector changed underneath it.
247 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000248}
249
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000250static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
251 assert(Bytes && "Invalid size!");
252 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
253}
254
Chris Lattner975780b2009-08-17 05:49:08 +0000255void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000256 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000257 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000258 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000259 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000260 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000261 }
262}
263
264void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000265 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000266 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000267 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000268
Chris Lattnere07b75e2010-09-22 22:19:53 +0000269 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000270 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000271 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000272}
273
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000274void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000275 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000276 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000277 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000278 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000279 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000280 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000281 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000282 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000283}
284
Jim Grosbachce792992010-11-05 22:08:08 +0000285void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
286 // This needs to emit to a temporary string to get properly quoted
287 // MCSymbols when they have spaces in them.
288 OS << "\t.thumb_func";
289 if (Func)
290 OS << '\t' << *Func;
291 EmitEOL();
292}
293
Daniel Dunbar821e3332009-08-31 08:09:28 +0000294void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000295 OS << *Symbol << " = " << *Value;
296 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000297
298 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000299 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000300}
301
Rafael Espindola484291c2010-11-01 14:28:48 +0000302void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
303 OS << ".weakref " << *Alias << ", " << *Symbol;
304 EmitEOL();
305}
306
Rafael Espindola32a006e2010-12-03 00:55:40 +0000307void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
308 const MCSymbol *LastLabel,
309 const MCSymbol *Label) {
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000310 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000311}
312
Daniel Dunbarfffff912009-10-16 01:34:54 +0000313void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000314 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000315 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000316 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000317 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
318 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
319 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
320 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
321 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
322 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000323 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000324 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000325 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000326 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
327 switch (Attribute) {
328 default: assert(0 && "Unknown ELF .type");
329 case MCSA_ELF_TypeFunction: OS << "function"; break;
330 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
331 case MCSA_ELF_TypeObject: OS << "object"; break;
332 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
333 case MCSA_ELF_TypeCommon: OS << "common"; break;
334 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000335 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000336 }
337 EmitEOL();
338 return;
339 case MCSA_Global: // .globl/.global
340 OS << MAI.getGlobalDirective();
341 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000342 case MCSA_Hidden: OS << "\t.hidden\t"; break;
343 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
344 case MCSA_Internal: OS << "\t.internal\t"; break;
345 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
346 case MCSA_Local: OS << "\t.local\t"; break;
347 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000348 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000349 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
350 case MCSA_Protected: OS << "\t.protected\t"; break;
351 case MCSA_Reference: OS << "\t.reference\t"; break;
352 case MCSA_Weak: OS << "\t.weak\t"; break;
353 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000354 // .weak_reference
355 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000356 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000357 }
358
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000359 OS << *Symbol;
360 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000361}
362
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000363void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000364 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
365 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000366}
367
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000368void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
369 OS << "\t.def\t " << *Symbol << ';';
370 EmitEOL();
371}
372
373void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
374 OS << "\t.scl\t" << StorageClass << ';';
375 EmitEOL();
376}
377
378void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
379 OS << "\t.type\t" << Type << ';';
380 EmitEOL();
381}
382
383void MCAsmStreamer::EndCOFFSymbolDef() {
384 OS << "\t.endef";
385 EmitEOL();
386}
387
Chris Lattner99328ad2010-01-25 07:52:13 +0000388void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
389 assert(MAI.hasDotTypeDotSizeDirective());
390 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
391}
392
Chris Lattner9eb158d2010-01-23 07:47:02 +0000393void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000394 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000395 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000396 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000397 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000398 OS << ',' << ByteAlignment;
399 else
400 OS << ',' << Log2_32(ByteAlignment);
401 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000402 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000403}
404
Chris Lattner9eb158d2010-01-23 07:47:02 +0000405/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
406///
407/// @param Symbol - The common symbol to emit.
408/// @param Size - The size of the common symbol.
409void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
410 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
411 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
412 EmitEOL();
413}
414
Daniel Dunbar8751b942009-08-28 05:48:22 +0000415void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000416 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000417 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000418 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000419
Chris Lattner93b6db32009-08-08 23:39:42 +0000420 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000421 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000422 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000423
Chris Lattner9be3fee2009-07-10 22:20:30 +0000424 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000425 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000426 if (ByteAlignment != 0)
427 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000428 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000429 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000430}
431
Eric Christopherd04d98d2010-05-17 02:13:02 +0000432// .tbss sym, size, align
433// This depends that the symbol has already been mangled from the original,
434// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000435void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
436 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000437 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000438 // Instead of using the Section we'll just use the shortcut.
439 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000440 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000441
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000442 // Output align if we have it. We default to 1 so don't bother printing
443 // that.
444 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000445
Eric Christopher482eba02010-05-14 01:50:28 +0000446 EmitEOL();
447}
448
Chris Lattner12e555c2010-01-23 00:15:00 +0000449static inline char toOctal(int X) { return (X&7)+'0'; }
450
Chris Lattnera6594fc2010-01-25 18:58:59 +0000451static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
452 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000453
Chris Lattnera6594fc2010-01-25 18:58:59 +0000454 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
455 unsigned char C = Data[i];
456 if (C == '"' || C == '\\') {
457 OS << '\\' << (char)C;
458 continue;
459 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000460
Chris Lattnera6594fc2010-01-25 18:58:59 +0000461 if (isprint((unsigned char)C)) {
462 OS << (char)C;
463 continue;
464 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000465
Chris Lattnera6594fc2010-01-25 18:58:59 +0000466 switch (C) {
467 case '\b': OS << "\\b"; break;
468 case '\f': OS << "\\f"; break;
469 case '\n': OS << "\\n"; break;
470 case '\r': OS << "\\r"; break;
471 case '\t': OS << "\\t"; break;
472 default:
473 OS << '\\';
474 OS << toOctal(C >> 6);
475 OS << toOctal(C >> 3);
476 OS << toOctal(C >> 0);
477 break;
478 }
479 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000480
Chris Lattnera6594fc2010-01-25 18:58:59 +0000481 OS << '"';
482}
483
484
Chris Lattneraaec2052010-01-19 19:46:13 +0000485void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000486 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000487 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000488
Chris Lattner12e555c2010-01-23 00:15:00 +0000489 if (Data.size() == 1) {
490 OS << MAI.getData8bitsDirective(AddrSpace);
491 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000492 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000493 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000494 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000495
496 // If the data ends with 0 and the target supports .asciz, use it, otherwise
497 // use .ascii
498 if (MAI.getAscizDirective() && Data.back() == 0) {
499 OS << MAI.getAscizDirective();
500 Data = Data.substr(0, Data.size()-1);
501 } else {
502 OS << MAI.getAsciiDirective();
503 }
504
Chris Lattnera6594fc2010-01-25 18:58:59 +0000505 OS << ' ';
506 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000507 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000508}
509
Rafael Espindola2df042c2010-12-03 02:54:21 +0000510void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
511 unsigned AddrSpace) {
512 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
513}
514
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000515void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
516 unsigned AddrSpace) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000517 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000518 const char *Directive = 0;
519 switch (Size) {
520 default: break;
521 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
522 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
523 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000524 case 8:
525 Directive = MAI.getData64bitsDirective(AddrSpace);
526 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
527 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000528 int64_t IntValue;
529 if (!Value->EvaluateAsAbsolute(IntValue))
530 report_fatal_error("Don't know how to emit this value.");
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000531 if (isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000532 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
533 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000534 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000535 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
536 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000537 }
538 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +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)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000549 EmitULEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000550 return;
551 }
Rafael Espindola73873452010-11-04 18:17:08 +0000552 assert(MAI.hasLEB128() && "Cannot print a .uleb");
553 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000554 EmitEOL();
555}
556
557void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000558 int64_t IntValue;
559 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000560 EmitSLEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000561 return;
562 }
Rafael Espindola73873452010-11-04 18:17:08 +0000563 assert(MAI.hasLEB128() && "Cannot print a .sleb");
564 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000565 EmitEOL();
566}
567
Chris Lattner718fb592010-01-25 21:28:50 +0000568void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
569 assert(MAI.getGPRel32Directive() != 0);
570 OS << MAI.getGPRel32Directive() << *Value;
571 EmitEOL();
572}
573
574
Chris Lattner6113b3d2010-01-19 18:52:28 +0000575/// EmitFill - Emit NumBytes bytes worth of the value specified by
576/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000577void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
578 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000579 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000580
Chris Lattneraaec2052010-01-19 19:46:13 +0000581 if (AddrSpace == 0)
582 if (const char *ZeroDirective = MAI.getZeroDirective()) {
583 OS << ZeroDirective << NumBytes;
584 if (FillValue != 0)
585 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000586 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000587 return;
588 }
589
590 // Emit a byte at a time.
591 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000592}
593
Daniel Dunbar84a29262009-06-24 19:25:34 +0000594void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
595 unsigned ValueSize,
596 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000597 // Some assemblers don't support non-power of two alignments, so we always
598 // emit alignments as a power of two if possible.
599 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000600 switch (ValueSize) {
601 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000602 case 1: OS << MAI.getAlignDirective(); break;
603 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000604 case 2: OS << ".p2alignw "; break;
605 case 4: OS << ".p2alignl "; break;
606 case 8: llvm_unreachable("Unsupported alignment size!");
607 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000608
Chris Lattner33adcfb2009-08-22 21:43:10 +0000609 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000610 OS << ByteAlignment;
611 else
612 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000613
Chris Lattner663c2d22009-08-19 06:12:02 +0000614 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000615 OS << ", 0x";
616 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000617
Jim Grosbach00545e12010-09-22 18:16:55 +0000618 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000619 OS << ", " << MaxBytesToEmit;
620 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000621 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000622 return;
623 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000624
Chris Lattner663c2d22009-08-19 06:12:02 +0000625 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000626 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000627 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000628 default: llvm_unreachable("Invalid size for machine code value!");
629 case 1: OS << ".balign"; break;
630 case 2: OS << ".balignw"; break;
631 case 4: OS << ".balignl"; break;
632 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000633 }
634
Chris Lattner663c2d22009-08-19 06:12:02 +0000635 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000636 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000637 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000638 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000639 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000640}
641
Kevin Enderby6e720482010-02-23 18:26:34 +0000642void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
643 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000644 // Emit with a text fill value.
645 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
646 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000647}
648
Daniel Dunbar821e3332009-08-31 08:09:28 +0000649void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000650 unsigned char Value) {
651 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000652 OS << ".org " << *Offset << ", " << (unsigned) Value;
653 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000654}
655
Chris Lattnera6594fc2010-01-25 18:58:59 +0000656
657void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
658 assert(MAI.hasSingleParameterDotFile());
659 OS << "\t.file\t";
660 PrintQuotedString(Filename, OS);
661 EmitEOL();
662}
663
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000664bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000665 if (!TLOF) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000666 OS << "\t.file\t" << FileNo << ' ';
667 PrintQuotedString(Filename, OS);
668 EmitEOL();
669 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000670 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
671}
672
673void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
674 unsigned Column, unsigned Flags,
675 unsigned Isa,
676 unsigned Discriminator) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000677 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
678 Isa, Discriminator);
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000679 if (TLOF)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000680 return;
681
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000682 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
683 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
684 OS << " basic_block";
685 if (Flags & DWARF2_FLAG_PROLOGUE_END)
686 OS << " prologue_end";
687 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
688 OS << " epilogue_begin";
689
690 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
691 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
692 OS << " is_stmt ";
693
694 if (Flags & DWARF2_FLAG_IS_STMT)
695 OS << "1";
696 else
697 OS << "0";
698 }
699
700 if (Isa)
701 OS << "isa " << Isa;
702 if (Discriminator)
703 OS << "discriminator " << Discriminator;
704 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000705}
706
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000707bool MCAsmStreamer::EmitCFIStartProc() {
708 if (this->MCStreamer::EmitCFIStartProc())
709 return true;
710
711 OS << ".cfi_startproc";
712 EmitEOL();
713
714 return false;
715}
716
717bool MCAsmStreamer::EmitCFIEndProc() {
718 if (this->MCStreamer::EmitCFIEndProc())
719 return true;
720
721 OS << ".cfi_endproc";
722 EmitEOL();
723
724 return false;
725}
726
727bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
728 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
729 return true;
730
731 OS << ".cfi_def_cfa_offset " << Offset;
732 EmitEOL();
733
734 return false;
735}
736
737bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
738 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
739 return true;
740
741 OS << ".cfi_def_cfa_register " << Register;
742 EmitEOL();
743
744 return false;
745}
746
747bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
748 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
749 return true;
750
751 OS << ".cfi_offset " << Register << ", " << Offset;
752 EmitEOL();
753
754 return false;
755}
756
757bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
758 if (this->MCStreamer::EmitCFIPersonality(Sym))
759 return true;
760
761 OS << ".cfi_personality 0, " << *Sym;
762 EmitEOL();
763
764 return false;
765}
766
767bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
768 if (this->MCStreamer::EmitCFILsda(Sym))
769 return true;
770
771 OS << ".cfi_lsda 0, " << *Sym;
772 EmitEOL();
773
774 return false;
775}
776
Daniel Dunbar6b716532010-02-09 23:00:14 +0000777void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
778 raw_ostream &OS = GetCommentOS();
779 SmallString<256> Code;
780 SmallVector<MCFixup, 4> Fixups;
781 raw_svector_ostream VecOS(Code);
782 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
783 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000784
Daniel Dunbar6b716532010-02-09 23:00:14 +0000785 // If we are showing fixups, create symbolic markers in the encoded
786 // representation. We do this by making a per-bit map to the fixup item index,
787 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000788 SmallVector<uint8_t, 64> FixupMap;
789 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000790 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
791 FixupMap[i] = 0;
792
793 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
794 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000795 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000796 for (unsigned j = 0; j != Info.TargetSize; ++j) {
797 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
798 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
799 FixupMap[Index] = 1 + i;
800 }
801 }
802
803 OS << "encoding: [";
804 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
805 if (i)
806 OS << ',';
807
808 // See if all bits are the same map entry.
809 uint8_t MapEntry = FixupMap[i * 8 + 0];
810 for (unsigned j = 1; j != 8; ++j) {
811 if (FixupMap[i * 8 + j] == MapEntry)
812 continue;
813
814 MapEntry = uint8_t(~0U);
815 break;
816 }
817
818 if (MapEntry != uint8_t(~0U)) {
819 if (MapEntry == 0) {
820 OS << format("0x%02x", uint8_t(Code[i]));
821 } else {
822 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
823 OS << char('A' + MapEntry - 1);
824 }
825 } else {
826 // Otherwise, write out in binary.
827 OS << "0b";
828 for (unsigned j = 8; j--;) {
829 unsigned Bit = (Code[i] >> j) & 1;
Chris Lattner3170a3b2010-11-15 05:56:19 +0000830
831 unsigned FixupBit;
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000832 if (IsLittleEndian)
Chris Lattner3170a3b2010-11-15 05:56:19 +0000833 FixupBit = i * 8 + j;
834 else
835 FixupBit = i * 8 + (7-j);
836
837 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +0000838 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
839 OS << char('A' + MapEntry - 1);
840 } else
841 OS << Bit;
842 }
843 }
844 }
845 OS << "]\n";
846
847 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
848 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000849 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000850 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000851 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000852 }
853}
854
855void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
856 assert(CurSection && "Cannot emit contents before setting section!");
857
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000858 if (TLOF)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000859 MCLineEntry::Make(this, getCurrentSection());
860
Daniel Dunbar6b716532010-02-09 23:00:14 +0000861 // Show the encoding in a comment if we have a code emitter.
862 if (Emitter)
863 AddEncodingComment(Inst);
864
Chris Lattner30d9a642010-02-09 00:54:51 +0000865 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000866 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000867 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000868 GetCommentOS() << "\n";
869 }
870
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000871 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000872 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000873 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000874 else
875 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000876 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000877}
878
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000879/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000880/// the specified string in the output .s file. This capability is
881/// indicated by the hasRawTextSupport() predicate.
882void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000883 if (!String.empty() && String.back() == '\n')
884 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000885 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000886 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000887}
888
Daniel Dunbara11af532009-06-24 01:03:06 +0000889void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000890 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000891 if (getContext().hasDwarfFiles() && TLOF)
892 MCDwarfFileTable::Emit(this, TLOF->getDwarfLineSection());
Daniel Dunbara11af532009-06-24 01:03:06 +0000893}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000894
Chris Lattner86e22112010-01-22 07:29:22 +0000895MCStreamer *llvm::createAsmStreamer(MCContext &Context,
896 formatted_raw_ostream &OS,
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000897 bool isLittleEndian,
898 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000899 MCCodeEmitter *CE, bool ShowInst) {
Rafael Espindolaf7fd4aa2010-12-10 04:01:09 +0000900 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
901 NULL, 0, IP, CE, ShowInst);
902}
903
904
905MCStreamer *llvm::createAsmStreamerNoLoc(MCContext &Context,
906 formatted_raw_ostream &OS,
907 bool isLittleEndian,
908 bool isVerboseAsm,
909 const TargetLoweringObjectFile *TLOF,
910 int PointerSize,
911 MCInstPrinter *IP,
912 MCCodeEmitter *CE, bool ShowInst) {
913 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
914 TLOF, PointerSize, IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000915}