blob: 40a234ea85ff36e327ab8e2c40093cf41dce8c8b [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);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000124 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
125 const MCSymbol *LastLabel,
126 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000127
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000128 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000129
Chris Lattner46a947d2009-08-17 04:17:34 +0000130 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000131 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
132 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
133 virtual void EmitCOFFSymbolType(int Type);
134 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000135 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000136 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000137 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000138
Chris Lattner9eb158d2010-01-23 07:47:02 +0000139 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
140 ///
141 /// @param Symbol - The common symbol to emit.
142 /// @param Size - The size of the common symbol.
143 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000144
Daniel Dunbar8751b942009-08-28 05:48:22 +0000145 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000146 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000147
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000148 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
149 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000150
Chris Lattneraaec2052010-01-19 19:46:13 +0000151 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000152
Devang Patelee4854f2010-12-02 21:32:30 +0000153 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace,
154 bool UseSet = false);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000155 virtual void EmitIntValue(uint64_t Value, unsigned Size,
156 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000157
Rafael Espindola3ff57092010-11-02 17:22:24 +0000158 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
159
160 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
161
Chris Lattner718fb592010-01-25 21:28:50 +0000162 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000163
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000164
Chris Lattneraaec2052010-01-19 19:46:13 +0000165 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
166 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000167
Chris Lattner46a947d2009-08-17 04:17:34 +0000168 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
169 unsigned ValueSize = 1,
170 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000171
Kevin Enderby6e720482010-02-23 18:26:34 +0000172 virtual void EmitCodeAlignment(unsigned ByteAlignment,
173 unsigned MaxBytesToEmit = 0);
174
Daniel Dunbar821e3332009-08-31 08:09:28 +0000175 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000176 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000177
Chris Lattnera6594fc2010-01-25 18:58:59 +0000178 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000179 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
180 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
181 unsigned Column, unsigned Flags,
182 unsigned Isa, unsigned Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000183
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000184 virtual bool EmitCFIStartProc();
185 virtual bool EmitCFIEndProc();
186 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
187 virtual bool EmitCFIDefCfaRegister(int64_t Register);
188 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
189 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
190 virtual bool EmitCFILsda(const MCSymbol *Sym);
191
Chris Lattnera6594fc2010-01-25 18:58:59 +0000192 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000193
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000194 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000195 /// the specified string in the output .s file. This capability is
196 /// indicated by the hasRawTextSupport() predicate.
197 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000198
Chris Lattner46a947d2009-08-17 04:17:34 +0000199 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000200
Chris Lattner46a947d2009-08-17 04:17:34 +0000201 /// @}
202};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000203
Chris Lattner46a947d2009-08-17 04:17:34 +0000204} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000205
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000206/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000207/// file if applicable as a QoI issue to make the output of the compiler
208/// more readable. This only affects the MCAsmStreamer, and only when
209/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000210void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000211 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000212
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000213 // Make sure that CommentStream is flushed.
214 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000215
Chris Lattner86e22112010-01-22 07:29:22 +0000216 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000217 // Each comment goes on its own line.
218 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000219
Chris Lattner14ca1772010-01-22 21:16:10 +0000220 // Tell the comment stream that the vector changed underneath it.
221 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000222}
223
224void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000225 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
226 OS << '\n';
227 return;
228 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000229
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000230 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000231 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000232
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000233 assert(Comments.back() == '\n' &&
234 "Comment array not newline terminated");
235 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000236 // Emit a line of comments.
237 OS.PadToColumn(MAI.getCommentColumn());
238 size_t Position = Comments.find('\n');
239 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000240
Chris Lattner86e22112010-01-22 07:29:22 +0000241 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000242 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000243
Chris Lattner14ca1772010-01-22 21:16:10 +0000244 CommentToEmit.clear();
245 // Tell the comment stream that the vector changed underneath it.
246 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000247}
248
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000249static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
250 assert(Bytes && "Invalid size!");
251 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
252}
253
Chris Lattner975780b2009-08-17 05:49:08 +0000254void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000255 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000256 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000257 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000258 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000259 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000260 }
261}
262
263void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000264 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000265 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000266 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000267
Chris Lattnere07b75e2010-09-22 22:19:53 +0000268 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000269 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000270 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000271}
272
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000273void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000274 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000275 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000276 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000277 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000278 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000279 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000280 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000281 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000282}
283
Jim Grosbachce792992010-11-05 22:08:08 +0000284void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
285 // This needs to emit to a temporary string to get properly quoted
286 // MCSymbols when they have spaces in them.
287 OS << "\t.thumb_func";
288 if (Func)
289 OS << '\t' << *Func;
290 EmitEOL();
291}
292
Daniel Dunbar821e3332009-08-31 08:09:28 +0000293void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000294 OS << *Symbol << " = " << *Value;
295 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000296
297 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000298 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000299}
300
Rafael Espindola484291c2010-11-01 14:28:48 +0000301void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
302 OS << ".weakref " << *Alias << ", " << *Symbol;
303 EmitEOL();
304}
305
Rafael Espindola32a006e2010-12-03 00:55:40 +0000306void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
307 const MCSymbol *LastLabel,
308 const MCSymbol *Label) {
309 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
310}
311
Daniel Dunbarfffff912009-10-16 01:34:54 +0000312void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000313 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000314 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000315 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000316 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
317 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
318 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
319 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
320 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
321 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000322 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000323 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000324 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000325 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
326 switch (Attribute) {
327 default: assert(0 && "Unknown ELF .type");
328 case MCSA_ELF_TypeFunction: OS << "function"; break;
329 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
330 case MCSA_ELF_TypeObject: OS << "object"; break;
331 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
332 case MCSA_ELF_TypeCommon: OS << "common"; break;
333 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000334 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000335 }
336 EmitEOL();
337 return;
338 case MCSA_Global: // .globl/.global
339 OS << MAI.getGlobalDirective();
340 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000341 case MCSA_Hidden: OS << "\t.hidden\t"; break;
342 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
343 case MCSA_Internal: OS << "\t.internal\t"; break;
344 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
345 case MCSA_Local: OS << "\t.local\t"; break;
346 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000347 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000348 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
349 case MCSA_Protected: OS << "\t.protected\t"; break;
350 case MCSA_Reference: OS << "\t.reference\t"; break;
351 case MCSA_Weak: OS << "\t.weak\t"; break;
352 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000353 // .weak_reference
354 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000355 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000356 }
357
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000358 OS << *Symbol;
359 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000360}
361
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000362void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000363 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
364 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000365}
366
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000367void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
368 OS << "\t.def\t " << *Symbol << ';';
369 EmitEOL();
370}
371
372void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
373 OS << "\t.scl\t" << StorageClass << ';';
374 EmitEOL();
375}
376
377void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
378 OS << "\t.type\t" << Type << ';';
379 EmitEOL();
380}
381
382void MCAsmStreamer::EndCOFFSymbolDef() {
383 OS << "\t.endef";
384 EmitEOL();
385}
386
Chris Lattner99328ad2010-01-25 07:52:13 +0000387void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
388 assert(MAI.hasDotTypeDotSizeDirective());
389 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
390}
391
Chris Lattner9eb158d2010-01-23 07:47:02 +0000392void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000393 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000394 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000395 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000396 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000397 OS << ',' << ByteAlignment;
398 else
399 OS << ',' << Log2_32(ByteAlignment);
400 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000401 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000402}
403
Chris Lattner9eb158d2010-01-23 07:47:02 +0000404/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
405///
406/// @param Symbol - The common symbol to emit.
407/// @param Size - The size of the common symbol.
408void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
409 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
410 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
411 EmitEOL();
412}
413
Daniel Dunbar8751b942009-08-28 05:48:22 +0000414void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000415 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000416 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000417 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000418
Chris Lattner93b6db32009-08-08 23:39:42 +0000419 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000420 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000421 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000422
Chris Lattner9be3fee2009-07-10 22:20:30 +0000423 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000424 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000425 if (ByteAlignment != 0)
426 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000427 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000428 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000429}
430
Eric Christopherd04d98d2010-05-17 02:13:02 +0000431// .tbss sym, size, align
432// This depends that the symbol has already been mangled from the original,
433// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000434void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
435 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000436 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000437 // Instead of using the Section we'll just use the shortcut.
438 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000439 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000440
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000441 // Output align if we have it. We default to 1 so don't bother printing
442 // that.
443 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000444
Eric Christopher482eba02010-05-14 01:50:28 +0000445 EmitEOL();
446}
447
Chris Lattner12e555c2010-01-23 00:15:00 +0000448static inline char toOctal(int X) { return (X&7)+'0'; }
449
Chris Lattnera6594fc2010-01-25 18:58:59 +0000450static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
451 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000452
Chris Lattnera6594fc2010-01-25 18:58:59 +0000453 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
454 unsigned char C = Data[i];
455 if (C == '"' || C == '\\') {
456 OS << '\\' << (char)C;
457 continue;
458 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000459
Chris Lattnera6594fc2010-01-25 18:58:59 +0000460 if (isprint((unsigned char)C)) {
461 OS << (char)C;
462 continue;
463 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000464
Chris Lattnera6594fc2010-01-25 18:58:59 +0000465 switch (C) {
466 case '\b': OS << "\\b"; break;
467 case '\f': OS << "\\f"; break;
468 case '\n': OS << "\\n"; break;
469 case '\r': OS << "\\r"; break;
470 case '\t': OS << "\\t"; break;
471 default:
472 OS << '\\';
473 OS << toOctal(C >> 6);
474 OS << toOctal(C >> 3);
475 OS << toOctal(C >> 0);
476 break;
477 }
478 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000479
Chris Lattnera6594fc2010-01-25 18:58:59 +0000480 OS << '"';
481}
482
483
Chris Lattneraaec2052010-01-19 19:46:13 +0000484void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000485 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000486 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000487
Chris Lattner12e555c2010-01-23 00:15:00 +0000488 if (Data.size() == 1) {
489 OS << MAI.getData8bitsDirective(AddrSpace);
490 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000491 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000492 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000493 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000494
495 // If the data ends with 0 and the target supports .asciz, use it, otherwise
496 // use .ascii
497 if (MAI.getAscizDirective() && Data.back() == 0) {
498 OS << MAI.getAscizDirective();
499 Data = Data.substr(0, Data.size()-1);
500 } else {
501 OS << MAI.getAsciiDirective();
502 }
503
Chris Lattnera6594fc2010-01-25 18:58:59 +0000504 OS << ' ';
505 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000506 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000507}
508
Rafael Espindola2df042c2010-12-03 02:54:21 +0000509void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
510 unsigned AddrSpace) {
511 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
512}
513
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000514void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
Devang Patelee4854f2010-12-02 21:32:30 +0000515 unsigned AddrSpace, bool UseSet) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000516 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000517 const char *Directive = 0;
518 switch (Size) {
519 default: break;
520 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
521 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
522 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000523 case 8:
524 Directive = MAI.getData64bitsDirective(AddrSpace);
525 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
526 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000527 int64_t IntValue;
528 if (!Value->EvaluateAsAbsolute(IntValue))
529 report_fatal_error("Don't know how to emit this value.");
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000530 if (isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000531 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
532 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000533 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000534 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
535 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000536 }
537 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000538 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000539
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000540 assert(Directive && "Invalid size for machine code value!");
Devang Patelee4854f2010-12-02 21:32:30 +0000541 if (UseSet && MAI.hasSetDirective()) {
542 MCSymbol *SetLabel = getContext().CreateTempSymbol();
543 EmitAssignment(SetLabel, Value);
544 OS << Directive << *SetLabel;
545 EmitEOL();
546 return;
547 }
548
Chris Lattner718fb592010-01-25 21:28:50 +0000549 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000550 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000551}
552
Rafael Espindola3ff57092010-11-02 17:22:24 +0000553void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000554 int64_t IntValue;
555 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000556 EmitULEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000557 return;
558 }
Rafael Espindola73873452010-11-04 18:17:08 +0000559 assert(MAI.hasLEB128() && "Cannot print a .uleb");
560 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000561 EmitEOL();
562}
563
564void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000565 int64_t IntValue;
566 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000567 EmitSLEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000568 return;
569 }
Rafael Espindola73873452010-11-04 18:17:08 +0000570 assert(MAI.hasLEB128() && "Cannot print a .sleb");
571 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000572 EmitEOL();
573}
574
Chris Lattner718fb592010-01-25 21:28:50 +0000575void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
576 assert(MAI.getGPRel32Directive() != 0);
577 OS << MAI.getGPRel32Directive() << *Value;
578 EmitEOL();
579}
580
581
Chris Lattner6113b3d2010-01-19 18:52:28 +0000582/// EmitFill - Emit NumBytes bytes worth of the value specified by
583/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000584void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
585 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000586 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000587
Chris Lattneraaec2052010-01-19 19:46:13 +0000588 if (AddrSpace == 0)
589 if (const char *ZeroDirective = MAI.getZeroDirective()) {
590 OS << ZeroDirective << NumBytes;
591 if (FillValue != 0)
592 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000593 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000594 return;
595 }
596
597 // Emit a byte at a time.
598 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000599}
600
Daniel Dunbar84a29262009-06-24 19:25:34 +0000601void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
602 unsigned ValueSize,
603 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000604 // Some assemblers don't support non-power of two alignments, so we always
605 // emit alignments as a power of two if possible.
606 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000607 switch (ValueSize) {
608 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000609 case 1: OS << MAI.getAlignDirective(); break;
610 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000611 case 2: OS << ".p2alignw "; break;
612 case 4: OS << ".p2alignl "; break;
613 case 8: llvm_unreachable("Unsupported alignment size!");
614 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000615
Chris Lattner33adcfb2009-08-22 21:43:10 +0000616 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000617 OS << ByteAlignment;
618 else
619 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000620
Chris Lattner663c2d22009-08-19 06:12:02 +0000621 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000622 OS << ", 0x";
623 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000624
Jim Grosbach00545e12010-09-22 18:16:55 +0000625 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000626 OS << ", " << MaxBytesToEmit;
627 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000628 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000629 return;
630 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000631
Chris Lattner663c2d22009-08-19 06:12:02 +0000632 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000633 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000634 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000635 default: llvm_unreachable("Invalid size for machine code value!");
636 case 1: OS << ".balign"; break;
637 case 2: OS << ".balignw"; break;
638 case 4: OS << ".balignl"; break;
639 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000640 }
641
Chris Lattner663c2d22009-08-19 06:12:02 +0000642 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000643 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000644 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000645 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000646 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000647}
648
Kevin Enderby6e720482010-02-23 18:26:34 +0000649void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
650 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000651 // Emit with a text fill value.
652 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
653 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000654}
655
Daniel Dunbar821e3332009-08-31 08:09:28 +0000656void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000657 unsigned char Value) {
658 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000659 OS << ".org " << *Offset << ", " << (unsigned) Value;
660 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000661}
662
Chris Lattnera6594fc2010-01-25 18:58:59 +0000663
664void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
665 assert(MAI.hasSingleParameterDotFile());
666 OS << "\t.file\t";
667 PrintQuotedString(Filename, OS);
668 EmitEOL();
669}
670
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000671bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000672 if (!TLOF) {
673 OS << "\t.file\t" << FileNo << ' ';
674 PrintQuotedString(Filename, OS);
675 EmitEOL();
676 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000677 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
678}
679
680void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
681 unsigned Column, unsigned Flags,
682 unsigned Isa,
683 unsigned Discriminator) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000684 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
685 Isa, Discriminator);
686 if (TLOF)
687 return;
688
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000689 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
690 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
691 OS << " basic_block";
692 if (Flags & DWARF2_FLAG_PROLOGUE_END)
693 OS << " prologue_end";
694 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
695 OS << " epilogue_begin";
696
697 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
698 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
699 OS << " is_stmt ";
700
701 if (Flags & DWARF2_FLAG_IS_STMT)
702 OS << "1";
703 else
704 OS << "0";
705 }
706
707 if (Isa)
708 OS << "isa " << Isa;
709 if (Discriminator)
710 OS << "discriminator " << Discriminator;
711 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000712}
713
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000714bool MCAsmStreamer::EmitCFIStartProc() {
715 if (this->MCStreamer::EmitCFIStartProc())
716 return true;
717
718 OS << ".cfi_startproc";
719 EmitEOL();
720
721 return false;
722}
723
724bool MCAsmStreamer::EmitCFIEndProc() {
725 if (this->MCStreamer::EmitCFIEndProc())
726 return true;
727
728 OS << ".cfi_endproc";
729 EmitEOL();
730
731 return false;
732}
733
734bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
735 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
736 return true;
737
738 OS << ".cfi_def_cfa_offset " << Offset;
739 EmitEOL();
740
741 return false;
742}
743
744bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
745 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
746 return true;
747
748 OS << ".cfi_def_cfa_register " << Register;
749 EmitEOL();
750
751 return false;
752}
753
754bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
755 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
756 return true;
757
758 OS << ".cfi_offset " << Register << ", " << Offset;
759 EmitEOL();
760
761 return false;
762}
763
764bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
765 if (this->MCStreamer::EmitCFIPersonality(Sym))
766 return true;
767
768 OS << ".cfi_personality 0, " << *Sym;
769 EmitEOL();
770
771 return false;
772}
773
774bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
775 if (this->MCStreamer::EmitCFILsda(Sym))
776 return true;
777
778 OS << ".cfi_lsda 0, " << *Sym;
779 EmitEOL();
780
781 return false;
782}
783
Daniel Dunbar6b716532010-02-09 23:00:14 +0000784void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
785 raw_ostream &OS = GetCommentOS();
786 SmallString<256> Code;
787 SmallVector<MCFixup, 4> Fixups;
788 raw_svector_ostream VecOS(Code);
789 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
790 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000791
Daniel Dunbar6b716532010-02-09 23:00:14 +0000792 // If we are showing fixups, create symbolic markers in the encoded
793 // representation. We do this by making a per-bit map to the fixup item index,
794 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000795 SmallVector<uint8_t, 64> FixupMap;
796 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000797 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
798 FixupMap[i] = 0;
799
800 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
801 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000802 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000803 for (unsigned j = 0; j != Info.TargetSize; ++j) {
804 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
805 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
806 FixupMap[Index] = 1 + i;
807 }
808 }
809
810 OS << "encoding: [";
811 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
812 if (i)
813 OS << ',';
814
815 // See if all bits are the same map entry.
816 uint8_t MapEntry = FixupMap[i * 8 + 0];
817 for (unsigned j = 1; j != 8; ++j) {
818 if (FixupMap[i * 8 + j] == MapEntry)
819 continue;
820
821 MapEntry = uint8_t(~0U);
822 break;
823 }
824
825 if (MapEntry != uint8_t(~0U)) {
826 if (MapEntry == 0) {
827 OS << format("0x%02x", uint8_t(Code[i]));
828 } else {
829 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
830 OS << char('A' + MapEntry - 1);
831 }
832 } else {
833 // Otherwise, write out in binary.
834 OS << "0b";
835 for (unsigned j = 8; j--;) {
836 unsigned Bit = (Code[i] >> j) & 1;
Chris Lattner3170a3b2010-11-15 05:56:19 +0000837
838 unsigned FixupBit;
839 if (IsLittleEndian)
840 FixupBit = i * 8 + j;
841 else
842 FixupBit = i * 8 + (7-j);
843
844 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +0000845 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
846 OS << char('A' + MapEntry - 1);
847 } else
848 OS << Bit;
849 }
850 }
851 }
852 OS << "]\n";
853
854 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
855 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000856 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000857 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000858 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000859 }
860}
861
862void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
863 assert(CurSection && "Cannot emit contents before setting section!");
864
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000865 if (TLOF)
866 MCLineEntry::Make(this, getCurrentSection());
867
Daniel Dunbar6b716532010-02-09 23:00:14 +0000868 // Show the encoding in a comment if we have a code emitter.
869 if (Emitter)
870 AddEncodingComment(Inst);
871
Chris Lattner30d9a642010-02-09 00:54:51 +0000872 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000873 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000874 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000875 GetCommentOS() << "\n";
876 }
877
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000878 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000879 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000880 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000881 else
882 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000883 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000884}
885
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000886/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000887/// the specified string in the output .s file. This capability is
888/// indicated by the hasRawTextSupport() predicate.
889void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000890 if (!String.empty() && String.back() == '\n')
891 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000892 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000893 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000894}
895
Daniel Dunbara11af532009-06-24 01:03:06 +0000896void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000897 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola32a006e2010-12-03 00:55:40 +0000898 if (getContext().hasDwarfFiles() && TLOF)
Rafael Espindola767b1be2010-12-04 00:31:13 +0000899 MCDwarfFileTable::Emit(this, TLOF->getDwarfLineSection());
Daniel Dunbara11af532009-06-24 01:03:06 +0000900}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000901
Chris Lattner86e22112010-01-22 07:29:22 +0000902MCStreamer *llvm::createAsmStreamer(MCContext &Context,
903 formatted_raw_ostream &OS,
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000904 bool isLittleEndian,
Chris Lattner07404412010-01-22 07:06:15 +0000905 bool isVerboseAsm, MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000906 MCCodeEmitter *CE, bool ShowInst) {
Chris Lattnerfdab14b2010-03-12 18:28:53 +0000907 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000908 NULL, 0, IP, CE, ShowInst);
909}
910
911
912MCStreamer *llvm::createAsmStreamerNoLoc(MCContext &Context,
913 formatted_raw_ostream &OS,
914 bool isLittleEndian,
915 bool isVerboseAsm,
916 const TargetLoweringObjectFile *TLOF,
917 int PointerSize,
918 MCInstPrinter *IP,
919 MCCodeEmitter *CE, bool ShowInst) {
920 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
921 TLOF, PointerSize, IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000922}