blob: 40aa0774b6368c2e038c7f87f65215a7b6e44031 [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 Dunbar2761fc42010-12-16 03:20:06 +000015#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000016#include "llvm/MC/MCInst.h"
Chris Lattner90edac02009-09-14 03:02:37 +000017#include "llvm/MC/MCInstPrinter.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000018#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000019#include "llvm/MC/MCSymbol.h"
Chris Lattner4c42a6d2010-03-19 05:48:53 +000020#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000024#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000025#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000026#include "llvm/Support/FormattedStream.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000027#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000028#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000030using namespace llvm;
31
32namespace {
33
Chris Lattner46a947d2009-08-17 04:17:34 +000034class MCAsmStreamer : public MCStreamer {
Chris Lattner86e22112010-01-22 07:29:22 +000035 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000036 const MCAsmInfo &MAI;
Chris Lattner4c42a6d2010-03-19 05:48:53 +000037 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000038 OwningPtr<MCCodeEmitter> Emitter;
Daniel Dunbar745dacc2010-12-16 03:05:59 +000039 OwningPtr<TargetAsmBackend> AsmBackend;
Jim Grosbach00545e12010-09-22 18:16:55 +000040
Chris Lattner86e22112010-01-22 07:29:22 +000041 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000042 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000043
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000044 unsigned IsVerboseAsm : 1;
45 unsigned ShowInst : 1;
Rafael Espindola89b93722010-12-10 07:39:47 +000046 unsigned UseLoc : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000047
Rafael Espindola5d4918d2010-12-04 03:21:47 +000048 bool needsSet(const MCExpr *Value);
49
Chris Lattner46a947d2009-08-17 04:17:34 +000050public:
Chris Lattner86e22112010-01-22 07:29:22 +000051 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindola89b93722010-12-10 07:39:47 +000052 bool isVerboseAsm,
53 bool useLoc,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000054 MCInstPrinter *printer, MCCodeEmitter *emitter,
55 TargetAsmBackend *asmbackend,
56 bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000057 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
Daniel Dunbar745dacc2010-12-16 03:05:59 +000058 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
59 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
Rafael Espindola89b93722010-12-10 07:39:47 +000060 ShowInst(showInst), UseLoc(useLoc) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000061 if (InstPrinter && IsVerboseAsm)
62 InstPrinter->setCommentStream(CommentStream);
63 }
Chris Lattner46a947d2009-08-17 04:17:34 +000064 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000065
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 Espindola89b93722010-12-10 07:39:47 +0000155 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
156 bool isPCRel, unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000157 virtual void EmitIntValue(uint64_t Value, unsigned Size,
158 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000159
Rafael Espindola3ff57092010-11-02 17:22:24 +0000160 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
161
162 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
163
Chris Lattner718fb592010-01-25 21:28:50 +0000164 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000165
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000166
Chris Lattneraaec2052010-01-19 19:46:13 +0000167 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
168 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000169
Chris Lattner46a947d2009-08-17 04:17:34 +0000170 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
171 unsigned ValueSize = 1,
172 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000173
Kevin Enderby6e720482010-02-23 18:26:34 +0000174 virtual void EmitCodeAlignment(unsigned ByteAlignment,
175 unsigned MaxBytesToEmit = 0);
176
Daniel Dunbar821e3332009-08-31 08:09:28 +0000177 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000178 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000179
Chris Lattnera6594fc2010-01-25 18:58:59 +0000180 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000181 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
182 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
183 unsigned Column, unsigned Flags,
184 unsigned Isa, unsigned Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000185
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000186 virtual bool EmitCFIStartProc();
187 virtual bool EmitCFIEndProc();
188 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
189 virtual bool EmitCFIDefCfaRegister(int64_t Register);
190 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
191 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
192 virtual bool EmitCFILsda(const MCSymbol *Sym);
193
Chris Lattnera6594fc2010-01-25 18:58:59 +0000194 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000195
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000196 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000197 /// the specified string in the output .s file. This capability is
198 /// indicated by the hasRawTextSupport() predicate.
199 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000200
Chris Lattner46a947d2009-08-17 04:17:34 +0000201 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000202
Chris Lattner46a947d2009-08-17 04:17:34 +0000203 /// @}
204};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000205
Chris Lattner46a947d2009-08-17 04:17:34 +0000206} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000207
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000208/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000209/// file if applicable as a QoI issue to make the output of the compiler
210/// more readable. This only affects the MCAsmStreamer, and only when
211/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000212void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000213 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000214
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000215 // Make sure that CommentStream is flushed.
216 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000217
Chris Lattner86e22112010-01-22 07:29:22 +0000218 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000219 // Each comment goes on its own line.
220 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000221
Chris Lattner14ca1772010-01-22 21:16:10 +0000222 // Tell the comment stream that the vector changed underneath it.
223 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000224}
225
226void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000227 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
228 OS << '\n';
229 return;
230 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000231
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000232 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000233 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000234
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000235 assert(Comments.back() == '\n' &&
236 "Comment array not newline terminated");
237 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000238 // Emit a line of comments.
239 OS.PadToColumn(MAI.getCommentColumn());
240 size_t Position = Comments.find('\n');
241 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000242
Chris Lattner86e22112010-01-22 07:29:22 +0000243 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000244 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000245
Chris Lattner14ca1772010-01-22 21:16:10 +0000246 CommentToEmit.clear();
247 // Tell the comment stream that the vector changed underneath it.
248 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000249}
250
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000251static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
252 assert(Bytes && "Invalid size!");
253 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
254}
255
Chris Lattner975780b2009-08-17 05:49:08 +0000256void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000257 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000258 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000259 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000260 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000261 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000262 }
263}
264
265void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000266 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000267 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000268 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000269
Chris Lattnere07b75e2010-09-22 22:19:53 +0000270 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000271 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000272 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000273}
274
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000275void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000276 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000277 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000278 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000279 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000280 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000281 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000282 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000283 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000284}
285
Jim Grosbachce792992010-11-05 22:08:08 +0000286void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
287 // This needs to emit to a temporary string to get properly quoted
288 // MCSymbols when they have spaces in them.
289 OS << "\t.thumb_func";
290 if (Func)
291 OS << '\t' << *Func;
292 EmitEOL();
293}
294
Daniel Dunbar821e3332009-08-31 08:09:28 +0000295void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000296 OS << *Symbol << " = " << *Value;
297 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000298
299 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000300 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000301}
302
Rafael Espindola484291c2010-11-01 14:28:48 +0000303void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
304 OS << ".weakref " << *Alias << ", " << *Symbol;
305 EmitEOL();
306}
307
Rafael Espindola32a006e2010-12-03 00:55:40 +0000308void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
309 const MCSymbol *LastLabel,
310 const MCSymbol *Label) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000311 EmitDwarfSetLineAddr(LineDelta, Label,
312 getContext().getTargetAsmInfo().getPointerSize());
Rafael Espindola32a006e2010-12-03 00:55:40 +0000313}
314
Daniel Dunbarfffff912009-10-16 01:34:54 +0000315void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000316 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000317 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000318 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000319 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
320 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
321 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
322 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
323 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
324 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000325 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000326 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000327 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000328 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
329 switch (Attribute) {
330 default: assert(0 && "Unknown ELF .type");
331 case MCSA_ELF_TypeFunction: OS << "function"; break;
332 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
333 case MCSA_ELF_TypeObject: OS << "object"; break;
334 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
335 case MCSA_ELF_TypeCommon: OS << "common"; break;
336 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000337 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000338 }
339 EmitEOL();
340 return;
341 case MCSA_Global: // .globl/.global
342 OS << MAI.getGlobalDirective();
343 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000344 case MCSA_Hidden: OS << "\t.hidden\t"; break;
345 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
346 case MCSA_Internal: OS << "\t.internal\t"; break;
347 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
348 case MCSA_Local: OS << "\t.local\t"; break;
349 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000350 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000351 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
352 case MCSA_Protected: OS << "\t.protected\t"; break;
353 case MCSA_Reference: OS << "\t.reference\t"; break;
354 case MCSA_Weak: OS << "\t.weak\t"; break;
355 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000356 // .weak_reference
357 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000358 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000359 }
360
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000361 OS << *Symbol;
362 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000363}
364
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000365void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000366 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
367 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000368}
369
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000370void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
371 OS << "\t.def\t " << *Symbol << ';';
372 EmitEOL();
373}
374
375void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
376 OS << "\t.scl\t" << StorageClass << ';';
377 EmitEOL();
378}
379
380void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
381 OS << "\t.type\t" << Type << ';';
382 EmitEOL();
383}
384
385void MCAsmStreamer::EndCOFFSymbolDef() {
386 OS << "\t.endef";
387 EmitEOL();
388}
389
Chris Lattner99328ad2010-01-25 07:52:13 +0000390void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
391 assert(MAI.hasDotTypeDotSizeDirective());
392 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
393}
394
Chris Lattner9eb158d2010-01-23 07:47:02 +0000395void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000396 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000397 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000398 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000399 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000400 OS << ',' << ByteAlignment;
401 else
402 OS << ',' << Log2_32(ByteAlignment);
403 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000404 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000405}
406
Chris Lattner9eb158d2010-01-23 07:47:02 +0000407/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
408///
409/// @param Symbol - The common symbol to emit.
410/// @param Size - The size of the common symbol.
411void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
412 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
413 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
414 EmitEOL();
415}
416
Daniel Dunbar8751b942009-08-28 05:48:22 +0000417void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000418 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000419 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000420 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000421
Chris Lattner93b6db32009-08-08 23:39:42 +0000422 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000423 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000424 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000425
Chris Lattner9be3fee2009-07-10 22:20:30 +0000426 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000427 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000428 if (ByteAlignment != 0)
429 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000430 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000431 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000432}
433
Eric Christopherd04d98d2010-05-17 02:13:02 +0000434// .tbss sym, size, align
435// This depends that the symbol has already been mangled from the original,
436// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000437void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
438 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000439 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000440 // Instead of using the Section we'll just use the shortcut.
441 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000442 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000443
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000444 // Output align if we have it. We default to 1 so don't bother printing
445 // that.
446 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000447
Eric Christopher482eba02010-05-14 01:50:28 +0000448 EmitEOL();
449}
450
Chris Lattner12e555c2010-01-23 00:15:00 +0000451static inline char toOctal(int X) { return (X&7)+'0'; }
452
Chris Lattnera6594fc2010-01-25 18:58:59 +0000453static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
454 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000455
Chris Lattnera6594fc2010-01-25 18:58:59 +0000456 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
457 unsigned char C = Data[i];
458 if (C == '"' || C == '\\') {
459 OS << '\\' << (char)C;
460 continue;
461 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000462
Chris Lattnera6594fc2010-01-25 18:58:59 +0000463 if (isprint((unsigned char)C)) {
464 OS << (char)C;
465 continue;
466 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000467
Chris Lattnera6594fc2010-01-25 18:58:59 +0000468 switch (C) {
469 case '\b': OS << "\\b"; break;
470 case '\f': OS << "\\f"; break;
471 case '\n': OS << "\\n"; break;
472 case '\r': OS << "\\r"; break;
473 case '\t': OS << "\\t"; break;
474 default:
475 OS << '\\';
476 OS << toOctal(C >> 6);
477 OS << toOctal(C >> 3);
478 OS << toOctal(C >> 0);
479 break;
480 }
481 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000482
Chris Lattnera6594fc2010-01-25 18:58:59 +0000483 OS << '"';
484}
485
486
Chris Lattneraaec2052010-01-19 19:46:13 +0000487void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000488 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000489 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000490
Chris Lattner12e555c2010-01-23 00:15:00 +0000491 if (Data.size() == 1) {
492 OS << MAI.getData8bitsDirective(AddrSpace);
493 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000494 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000495 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000496 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000497
498 // If the data ends with 0 and the target supports .asciz, use it, otherwise
499 // use .ascii
500 if (MAI.getAscizDirective() && Data.back() == 0) {
501 OS << MAI.getAscizDirective();
502 Data = Data.substr(0, Data.size()-1);
503 } else {
504 OS << MAI.getAsciiDirective();
505 }
506
Chris Lattnera6594fc2010-01-25 18:58:59 +0000507 OS << ' ';
508 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000509 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000510}
511
Rafael Espindola2df042c2010-12-03 02:54:21 +0000512void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
513 unsigned AddrSpace) {
514 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
515}
516
Rafael Espindola89b93722010-12-10 07:39:47 +0000517void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
518 bool isPCRel, unsigned AddrSpace) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000519 assert(CurSection && "Cannot emit contents before setting section!");
Rafael Espindola89b93722010-12-10 07:39:47 +0000520 assert(!isPCRel && "Cannot emit pc relative relocations!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000521 const char *Directive = 0;
522 switch (Size) {
523 default: break;
524 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
525 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
526 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000527 case 8:
528 Directive = MAI.getData64bitsDirective(AddrSpace);
529 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
530 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000531 int64_t IntValue;
532 if (!Value->EvaluateAsAbsolute(IntValue))
533 report_fatal_error("Don't know how to emit this value.");
Rafael Espindola89b93722010-12-10 07:39:47 +0000534 if (getContext().getTargetAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000535 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
536 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000537 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000538 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
539 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000540 }
541 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000542 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000543
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000544 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000545 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000546 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000547}
548
Rafael Espindola3ff57092010-11-02 17:22:24 +0000549void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000550 int64_t IntValue;
551 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000552 EmitULEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000553 return;
554 }
Rafael Espindola73873452010-11-04 18:17:08 +0000555 assert(MAI.hasLEB128() && "Cannot print a .uleb");
556 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000557 EmitEOL();
558}
559
560void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000561 int64_t IntValue;
562 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000563 EmitSLEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000564 return;
565 }
Rafael Espindola73873452010-11-04 18:17:08 +0000566 assert(MAI.hasLEB128() && "Cannot print a .sleb");
567 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000568 EmitEOL();
569}
570
Chris Lattner718fb592010-01-25 21:28:50 +0000571void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
572 assert(MAI.getGPRel32Directive() != 0);
573 OS << MAI.getGPRel32Directive() << *Value;
574 EmitEOL();
575}
576
577
Chris Lattner6113b3d2010-01-19 18:52:28 +0000578/// EmitFill - Emit NumBytes bytes worth of the value specified by
579/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000580void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
581 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000582 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000583
Chris Lattneraaec2052010-01-19 19:46:13 +0000584 if (AddrSpace == 0)
585 if (const char *ZeroDirective = MAI.getZeroDirective()) {
586 OS << ZeroDirective << NumBytes;
587 if (FillValue != 0)
588 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000589 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000590 return;
591 }
592
593 // Emit a byte at a time.
594 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000595}
596
Daniel Dunbar84a29262009-06-24 19:25:34 +0000597void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
598 unsigned ValueSize,
599 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000600 // Some assemblers don't support non-power of two alignments, so we always
601 // emit alignments as a power of two if possible.
602 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000603 switch (ValueSize) {
604 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000605 case 1: OS << MAI.getAlignDirective(); break;
606 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000607 case 2: OS << ".p2alignw "; break;
608 case 4: OS << ".p2alignl "; break;
609 case 8: llvm_unreachable("Unsupported alignment size!");
610 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000611
Chris Lattner33adcfb2009-08-22 21:43:10 +0000612 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000613 OS << ByteAlignment;
614 else
615 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000616
Chris Lattner663c2d22009-08-19 06:12:02 +0000617 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000618 OS << ", 0x";
619 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000620
Jim Grosbach00545e12010-09-22 18:16:55 +0000621 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000622 OS << ", " << MaxBytesToEmit;
623 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000624 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000625 return;
626 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000627
Chris Lattner663c2d22009-08-19 06:12:02 +0000628 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000629 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000630 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000631 default: llvm_unreachable("Invalid size for machine code value!");
632 case 1: OS << ".balign"; break;
633 case 2: OS << ".balignw"; break;
634 case 4: OS << ".balignl"; break;
635 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000636 }
637
Chris Lattner663c2d22009-08-19 06:12:02 +0000638 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000639 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000640 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000641 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000642 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000643}
644
Kevin Enderby6e720482010-02-23 18:26:34 +0000645void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
646 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000647 // Emit with a text fill value.
648 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
649 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000650}
651
Daniel Dunbar821e3332009-08-31 08:09:28 +0000652void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000653 unsigned char Value) {
654 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000655 OS << ".org " << *Offset << ", " << (unsigned) Value;
656 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000657}
658
Chris Lattnera6594fc2010-01-25 18:58:59 +0000659
660void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
661 assert(MAI.hasSingleParameterDotFile());
662 OS << "\t.file\t";
663 PrintQuotedString(Filename, OS);
664 EmitEOL();
665}
666
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000667bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola89b93722010-12-10 07:39:47 +0000668 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000669 OS << "\t.file\t" << FileNo << ' ';
670 PrintQuotedString(Filename, OS);
671 EmitEOL();
672 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000673 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
674}
675
676void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
677 unsigned Column, unsigned Flags,
678 unsigned Isa,
679 unsigned Discriminator) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000680 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
681 Isa, Discriminator);
Rafael Espindola89b93722010-12-10 07:39:47 +0000682 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000683 return;
684
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000685 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
686 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
687 OS << " basic_block";
688 if (Flags & DWARF2_FLAG_PROLOGUE_END)
689 OS << " prologue_end";
690 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
691 OS << " epilogue_begin";
692
693 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
694 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
695 OS << " is_stmt ";
696
697 if (Flags & DWARF2_FLAG_IS_STMT)
698 OS << "1";
699 else
700 OS << "0";
701 }
702
703 if (Isa)
704 OS << "isa " << Isa;
705 if (Discriminator)
706 OS << "discriminator " << Discriminator;
707 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000708}
709
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000710bool MCAsmStreamer::EmitCFIStartProc() {
711 if (this->MCStreamer::EmitCFIStartProc())
712 return true;
713
714 OS << ".cfi_startproc";
715 EmitEOL();
716
717 return false;
718}
719
720bool MCAsmStreamer::EmitCFIEndProc() {
721 if (this->MCStreamer::EmitCFIEndProc())
722 return true;
723
724 OS << ".cfi_endproc";
725 EmitEOL();
726
727 return false;
728}
729
730bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
731 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
732 return true;
733
734 OS << ".cfi_def_cfa_offset " << Offset;
735 EmitEOL();
736
737 return false;
738}
739
740bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
741 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
742 return true;
743
744 OS << ".cfi_def_cfa_register " << Register;
745 EmitEOL();
746
747 return false;
748}
749
750bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
751 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
752 return true;
753
754 OS << ".cfi_offset " << Register << ", " << Offset;
755 EmitEOL();
756
757 return false;
758}
759
760bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
761 if (this->MCStreamer::EmitCFIPersonality(Sym))
762 return true;
763
764 OS << ".cfi_personality 0, " << *Sym;
765 EmitEOL();
766
767 return false;
768}
769
770bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
771 if (this->MCStreamer::EmitCFILsda(Sym))
772 return true;
773
774 OS << ".cfi_lsda 0, " << *Sym;
775 EmitEOL();
776
777 return false;
778}
779
Daniel Dunbar6b716532010-02-09 23:00:14 +0000780void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
781 raw_ostream &OS = GetCommentOS();
782 SmallString<256> Code;
783 SmallVector<MCFixup, 4> Fixups;
784 raw_svector_ostream VecOS(Code);
785 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
786 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000787
Daniel Dunbar6b716532010-02-09 23:00:14 +0000788 // If we are showing fixups, create symbolic markers in the encoded
789 // representation. We do this by making a per-bit map to the fixup item index,
790 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000791 SmallVector<uint8_t, 64> FixupMap;
792 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000793 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
794 FixupMap[i] = 0;
795
796 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
797 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +0000798 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000799 for (unsigned j = 0; j != Info.TargetSize; ++j) {
800 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
801 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
802 FixupMap[Index] = 1 + i;
803 }
804 }
805
806 OS << "encoding: [";
807 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
808 if (i)
809 OS << ',';
810
811 // See if all bits are the same map entry.
812 uint8_t MapEntry = FixupMap[i * 8 + 0];
813 for (unsigned j = 1; j != 8; ++j) {
814 if (FixupMap[i * 8 + j] == MapEntry)
815 continue;
816
817 MapEntry = uint8_t(~0U);
818 break;
819 }
820
821 if (MapEntry != uint8_t(~0U)) {
822 if (MapEntry == 0) {
823 OS << format("0x%02x", uint8_t(Code[i]));
824 } else {
825 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
826 OS << char('A' + MapEntry - 1);
827 }
828 } else {
829 // Otherwise, write out in binary.
830 OS << "0b";
831 for (unsigned j = 8; j--;) {
832 unsigned Bit = (Code[i] >> j) & 1;
Chris Lattner3170a3b2010-11-15 05:56:19 +0000833
834 unsigned FixupBit;
Rafael Espindola89b93722010-12-10 07:39:47 +0000835 if (getContext().getTargetAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +0000836 FixupBit = i * 8 + j;
837 else
838 FixupBit = i * 8 + (7-j);
839
840 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +0000841 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
842 OS << char('A' + MapEntry - 1);
843 } else
844 OS << Bit;
845 }
846 }
847 }
848 OS << "]\n";
849
850 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
851 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +0000852 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000853 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000854 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000855 }
856}
857
858void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
859 assert(CurSection && "Cannot emit contents before setting section!");
860
Rafael Espindola89b93722010-12-10 07:39:47 +0000861 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000862 MCLineEntry::Make(this, getCurrentSection());
863
Daniel Dunbar6b716532010-02-09 23:00:14 +0000864 // Show the encoding in a comment if we have a code emitter.
865 if (Emitter)
866 AddEncodingComment(Inst);
867
Chris Lattner30d9a642010-02-09 00:54:51 +0000868 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000869 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000870 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000871 GetCommentOS() << "\n";
872 }
873
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000874 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000875 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000876 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000877 else
878 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000879 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000880}
881
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000882/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000883/// the specified string in the output .s file. This capability is
884/// indicated by the hasRawTextSupport() predicate.
885void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000886 if (!String.empty() && String.back() == '\n')
887 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000888 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000889 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000890}
891
Daniel Dunbara11af532009-06-24 01:03:06 +0000892void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000893 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +0000894 if (getContext().hasDwarfFiles() && !UseLoc)
895 MCDwarfFileTable::Emit(this);
Daniel Dunbara11af532009-06-24 01:03:06 +0000896}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000897
Chris Lattner86e22112010-01-22 07:29:22 +0000898MCStreamer *llvm::createAsmStreamer(MCContext &Context,
899 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +0000900 bool isVerboseAsm, bool useLoc,
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000901 MCInstPrinter *IP, MCCodeEmitter *CE,
902 TargetAsmBackend *TAB, bool ShowInst) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000903 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc,
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000904 IP, CE, TAB, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000905}