blob: a1c02208a0b274c7a73597cd6ed329ad0da5b8fe [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"
Rafael Espindola89b93722010-12-10 07:39:47 +000027#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000028using namespace llvm;
29
30namespace {
31
Chris Lattner46a947d2009-08-17 04:17:34 +000032class MCAsmStreamer : public MCStreamer {
Chris Lattner86e22112010-01-22 07:29:22 +000033 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000034 const MCAsmInfo &MAI;
Chris Lattner4c42a6d2010-03-19 05:48:53 +000035 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000036 OwningPtr<MCCodeEmitter> Emitter;
Jim Grosbach00545e12010-09-22 18:16:55 +000037
Chris Lattner86e22112010-01-22 07:29:22 +000038 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000039 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000040
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000041 unsigned IsVerboseAsm : 1;
42 unsigned ShowInst : 1;
Rafael Espindola89b93722010-12-10 07:39:47 +000043 unsigned UseLoc : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000044
Rafael Espindola5d4918d2010-12-04 03:21:47 +000045 bool needsSet(const MCExpr *Value);
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 Espindola89b93722010-12-10 07:39:47 +000049 bool isVerboseAsm,
50 bool useLoc,
Rafael Espindola195a0ce2010-11-19 02:26:16 +000051 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 Espindola89b93722010-12-10 07:39:47 +000054 IsVerboseAsm(isVerboseAsm),
55 ShowInst(showInst), UseLoc(useLoc) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000056 if (InstPrinter && IsVerboseAsm)
57 InstPrinter->setCommentStream(CommentStream);
58 }
Chris Lattner46a947d2009-08-17 04:17:34 +000059 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000060
Chris Lattner86e22112010-01-22 07:29:22 +000061 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000062 // If we don't have any comments, just emit a \n.
63 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000064 OS << '\n';
65 return;
66 }
67 EmitCommentsAndEOL();
68 }
69 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000070
71 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
72 /// all.
73 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000074
Chris Lattner91bead72010-04-03 21:35:55 +000075 /// hasRawTextSupport - We support EmitRawText.
76 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000077
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000078 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000079 /// file if applicable as a QoI issue to make the output of the compiler
80 /// more readable. This only affects the MCAsmStreamer, and only when
81 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000082 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000083
84 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
85 virtual void AddEncodingComment(const MCInst &Inst);
86
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000087 /// GetCommentOS - Return a raw_ostream that comments can be written to.
88 /// Unlike AddComment, you are required to terminate comments with \n if you
89 /// use this method.
90 virtual raw_ostream &GetCommentOS() {
91 if (!IsVerboseAsm)
92 return nulls(); // Discard comments unless in verbose asm mode.
93 return CommentStream;
94 }
Daniel Dunbar6b716532010-02-09 23:00:14 +000095
Chris Lattner0fd90fd2010-01-22 19:52:01 +000096 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
97 virtual void AddBlankLine() {
98 EmitEOL();
99 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000100
Chris Lattner46a947d2009-08-17 04:17:34 +0000101 /// @name MCStreamer Interface
102 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000103
Chris Lattner975780b2009-08-17 05:49:08 +0000104 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000105
Rafael Espindolad80781b2010-09-15 21:48:40 +0000106 virtual void InitSections() {
107 // FIXME, this is MachO specific, but the testsuite
108 // expects this.
109 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
110 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
111 0, SectionKind::getText()));
112 }
113
Chris Lattner46a947d2009-08-17 04:17:34 +0000114 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +0000115
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000116 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000117 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118
Daniel Dunbar821e3332009-08-31 08:09:28 +0000119 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000120 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000121 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
122 const MCSymbol *LastLabel,
123 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000124
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000125 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000126
Chris Lattner46a947d2009-08-17 04:17:34 +0000127 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000128 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
129 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
130 virtual void EmitCOFFSymbolType(int Type);
131 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000132 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000133 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000134 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000135
Chris Lattner9eb158d2010-01-23 07:47:02 +0000136 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
137 ///
138 /// @param Symbol - The common symbol to emit.
139 /// @param Size - The size of the common symbol.
140 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000141
Daniel Dunbar8751b942009-08-28 05:48:22 +0000142 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000143 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000144
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000145 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
146 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000147
Chris Lattneraaec2052010-01-19 19:46:13 +0000148 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000149
Rafael Espindola89b93722010-12-10 07:39:47 +0000150 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
151 bool isPCRel, unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000152 virtual void EmitIntValue(uint64_t Value, unsigned Size,
153 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000154
Rafael Espindola3ff57092010-11-02 17:22:24 +0000155 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
156
157 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
158
Chris Lattner718fb592010-01-25 21:28:50 +0000159 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000160
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000161
Chris Lattneraaec2052010-01-19 19:46:13 +0000162 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
163 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000164
Chris Lattner46a947d2009-08-17 04:17:34 +0000165 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
166 unsigned ValueSize = 1,
167 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000168
Kevin Enderby6e720482010-02-23 18:26:34 +0000169 virtual void EmitCodeAlignment(unsigned ByteAlignment,
170 unsigned MaxBytesToEmit = 0);
171
Daniel Dunbar821e3332009-08-31 08:09:28 +0000172 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000173 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000174
Chris Lattnera6594fc2010-01-25 18:58:59 +0000175 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000176 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
177 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
178 unsigned Column, unsigned Flags,
179 unsigned Isa, unsigned Discriminator);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000180
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000181 virtual bool EmitCFIStartProc();
182 virtual bool EmitCFIEndProc();
183 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
184 virtual bool EmitCFIDefCfaRegister(int64_t Register);
185 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
186 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
187 virtual bool EmitCFILsda(const MCSymbol *Sym);
188
Chris Lattnera6594fc2010-01-25 18:58:59 +0000189 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000190
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000191 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000192 /// the specified string in the output .s file. This capability is
193 /// indicated by the hasRawTextSupport() predicate.
194 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000195
Chris Lattner46a947d2009-08-17 04:17:34 +0000196 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000197
Chris Lattner46a947d2009-08-17 04:17:34 +0000198 /// @}
199};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000200
Chris Lattner46a947d2009-08-17 04:17:34 +0000201} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000202
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000203/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000204/// file if applicable as a QoI issue to make the output of the compiler
205/// more readable. This only affects the MCAsmStreamer, and only when
206/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000207void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000208 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000209
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000210 // Make sure that CommentStream is flushed.
211 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000212
Chris Lattner86e22112010-01-22 07:29:22 +0000213 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000214 // Each comment goes on its own line.
215 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000216
Chris Lattner14ca1772010-01-22 21:16:10 +0000217 // Tell the comment stream that the vector changed underneath it.
218 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000219}
220
221void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000222 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
223 OS << '\n';
224 return;
225 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000226
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000227 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000228 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000229
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000230 assert(Comments.back() == '\n' &&
231 "Comment array not newline terminated");
232 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000233 // Emit a line of comments.
234 OS.PadToColumn(MAI.getCommentColumn());
235 size_t Position = Comments.find('\n');
236 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000237
Chris Lattner86e22112010-01-22 07:29:22 +0000238 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000239 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000240
Chris Lattner14ca1772010-01-22 21:16:10 +0000241 CommentToEmit.clear();
242 // Tell the comment stream that the vector changed underneath it.
243 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000244}
245
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000246static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
247 assert(Bytes && "Invalid size!");
248 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
249}
250
Chris Lattner975780b2009-08-17 05:49:08 +0000251void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000252 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000253 if (Section != CurSection) {
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000254 PrevSection = CurSection;
Daniel Dunbara11af532009-06-24 01:03:06 +0000255 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000256 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000257 }
258}
259
260void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000261 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000262 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000263 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000264
Chris Lattnere07b75e2010-09-22 22:19:53 +0000265 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000266 EmitEOL();
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000267 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000268}
269
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000270void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000271 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000272 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000273 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000274 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000275 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000276 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000277 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000278 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000279}
280
Jim Grosbachce792992010-11-05 22:08:08 +0000281void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
282 // This needs to emit to a temporary string to get properly quoted
283 // MCSymbols when they have spaces in them.
284 OS << "\t.thumb_func";
285 if (Func)
286 OS << '\t' << *Func;
287 EmitEOL();
288}
289
Daniel Dunbar821e3332009-08-31 08:09:28 +0000290void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000291 OS << *Symbol << " = " << *Value;
292 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000293
294 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000295 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000296}
297
Rafael Espindola484291c2010-11-01 14:28:48 +0000298void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
299 OS << ".weakref " << *Alias << ", " << *Symbol;
300 EmitEOL();
301}
302
Rafael Espindola32a006e2010-12-03 00:55:40 +0000303void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
304 const MCSymbol *LastLabel,
305 const MCSymbol *Label) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000306 EmitDwarfSetLineAddr(LineDelta, Label,
307 getContext().getTargetAsmInfo().getPointerSize());
Rafael Espindola32a006e2010-12-03 00:55:40 +0000308}
309
Daniel Dunbarfffff912009-10-16 01:34:54 +0000310void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000311 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000312 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000313 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000314 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
315 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
316 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
317 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
318 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
319 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000320 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000321 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000322 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000323 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
324 switch (Attribute) {
325 default: assert(0 && "Unknown ELF .type");
326 case MCSA_ELF_TypeFunction: OS << "function"; break;
327 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
328 case MCSA_ELF_TypeObject: OS << "object"; break;
329 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
330 case MCSA_ELF_TypeCommon: OS << "common"; break;
331 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000332 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000333 }
334 EmitEOL();
335 return;
336 case MCSA_Global: // .globl/.global
337 OS << MAI.getGlobalDirective();
338 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000339 case MCSA_Hidden: OS << "\t.hidden\t"; break;
340 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
341 case MCSA_Internal: OS << "\t.internal\t"; break;
342 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
343 case MCSA_Local: OS << "\t.local\t"; break;
344 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000345 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000346 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
347 case MCSA_Protected: OS << "\t.protected\t"; break;
348 case MCSA_Reference: OS << "\t.reference\t"; break;
349 case MCSA_Weak: OS << "\t.weak\t"; break;
350 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000351 // .weak_reference
352 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000353 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000354 }
355
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000356 OS << *Symbol;
357 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000358}
359
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000360void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000361 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
362 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000363}
364
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000365void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
366 OS << "\t.def\t " << *Symbol << ';';
367 EmitEOL();
368}
369
370void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
371 OS << "\t.scl\t" << StorageClass << ';';
372 EmitEOL();
373}
374
375void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
376 OS << "\t.type\t" << Type << ';';
377 EmitEOL();
378}
379
380void MCAsmStreamer::EndCOFFSymbolDef() {
381 OS << "\t.endef";
382 EmitEOL();
383}
384
Chris Lattner99328ad2010-01-25 07:52:13 +0000385void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
386 assert(MAI.hasDotTypeDotSizeDirective());
387 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
388}
389
Chris Lattner9eb158d2010-01-23 07:47:02 +0000390void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000391 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000392 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000393 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000394 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000395 OS << ',' << ByteAlignment;
396 else
397 OS << ',' << Log2_32(ByteAlignment);
398 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000399 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000400}
401
Chris Lattner9eb158d2010-01-23 07:47:02 +0000402/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
403///
404/// @param Symbol - The common symbol to emit.
405/// @param Size - The size of the common symbol.
406void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
407 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
408 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
409 EmitEOL();
410}
411
Daniel Dunbar8751b942009-08-28 05:48:22 +0000412void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000413 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000414 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000415 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000416
Chris Lattner93b6db32009-08-08 23:39:42 +0000417 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000418 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000419 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000420
Chris Lattner9be3fee2009-07-10 22:20:30 +0000421 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000422 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000423 if (ByteAlignment != 0)
424 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000425 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000426 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000427}
428
Eric Christopherd04d98d2010-05-17 02:13:02 +0000429// .tbss sym, size, align
430// This depends that the symbol has already been mangled from the original,
431// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000432void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
433 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000434 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000435 // Instead of using the Section we'll just use the shortcut.
436 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000437 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000438
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000439 // Output align if we have it. We default to 1 so don't bother printing
440 // that.
441 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000442
Eric Christopher482eba02010-05-14 01:50:28 +0000443 EmitEOL();
444}
445
Chris Lattner12e555c2010-01-23 00:15:00 +0000446static inline char toOctal(int X) { return (X&7)+'0'; }
447
Chris Lattnera6594fc2010-01-25 18:58:59 +0000448static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
449 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000450
Chris Lattnera6594fc2010-01-25 18:58:59 +0000451 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
452 unsigned char C = Data[i];
453 if (C == '"' || C == '\\') {
454 OS << '\\' << (char)C;
455 continue;
456 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000457
Chris Lattnera6594fc2010-01-25 18:58:59 +0000458 if (isprint((unsigned char)C)) {
459 OS << (char)C;
460 continue;
461 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000462
Chris Lattnera6594fc2010-01-25 18:58:59 +0000463 switch (C) {
464 case '\b': OS << "\\b"; break;
465 case '\f': OS << "\\f"; break;
466 case '\n': OS << "\\n"; break;
467 case '\r': OS << "\\r"; break;
468 case '\t': OS << "\\t"; break;
469 default:
470 OS << '\\';
471 OS << toOctal(C >> 6);
472 OS << toOctal(C >> 3);
473 OS << toOctal(C >> 0);
474 break;
475 }
476 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000477
Chris Lattnera6594fc2010-01-25 18:58:59 +0000478 OS << '"';
479}
480
481
Chris Lattneraaec2052010-01-19 19:46:13 +0000482void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000483 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000484 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000485
Chris Lattner12e555c2010-01-23 00:15:00 +0000486 if (Data.size() == 1) {
487 OS << MAI.getData8bitsDirective(AddrSpace);
488 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000489 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000490 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000491 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000492
493 // If the data ends with 0 and the target supports .asciz, use it, otherwise
494 // use .ascii
495 if (MAI.getAscizDirective() && Data.back() == 0) {
496 OS << MAI.getAscizDirective();
497 Data = Data.substr(0, Data.size()-1);
498 } else {
499 OS << MAI.getAsciiDirective();
500 }
501
Chris Lattnera6594fc2010-01-25 18:58:59 +0000502 OS << ' ';
503 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000504 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000505}
506
Rafael Espindola2df042c2010-12-03 02:54:21 +0000507void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
508 unsigned AddrSpace) {
509 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
510}
511
Rafael Espindola89b93722010-12-10 07:39:47 +0000512void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
513 bool isPCRel, unsigned AddrSpace) {
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000514 assert(CurSection && "Cannot emit contents before setting section!");
Rafael Espindola89b93722010-12-10 07:39:47 +0000515 assert(!isPCRel && "Cannot emit pc relative relocations!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000516 const char *Directive = 0;
517 switch (Size) {
518 default: break;
519 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
520 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
521 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000522 case 8:
523 Directive = MAI.getData64bitsDirective(AddrSpace);
524 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
525 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000526 int64_t IntValue;
527 if (!Value->EvaluateAsAbsolute(IntValue))
528 report_fatal_error("Don't know how to emit this value.");
Rafael Espindola89b93722010-12-10 07:39:47 +0000529 if (getContext().getTargetAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000530 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
531 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000532 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000533 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
534 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000535 }
536 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000537 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000538
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000539 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000540 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000541 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000542}
543
Rafael Espindola3ff57092010-11-02 17:22:24 +0000544void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000545 int64_t IntValue;
546 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000547 EmitULEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000548 return;
549 }
Rafael Espindola73873452010-11-04 18:17:08 +0000550 assert(MAI.hasLEB128() && "Cannot print a .uleb");
551 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000552 EmitEOL();
553}
554
555void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000556 int64_t IntValue;
557 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000558 EmitSLEB128IntValue(IntValue, AddrSpace);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000559 return;
560 }
Rafael Espindola73873452010-11-04 18:17:08 +0000561 assert(MAI.hasLEB128() && "Cannot print a .sleb");
562 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000563 EmitEOL();
564}
565
Chris Lattner718fb592010-01-25 21:28:50 +0000566void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
567 assert(MAI.getGPRel32Directive() != 0);
568 OS << MAI.getGPRel32Directive() << *Value;
569 EmitEOL();
570}
571
572
Chris Lattner6113b3d2010-01-19 18:52:28 +0000573/// EmitFill - Emit NumBytes bytes worth of the value specified by
574/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000575void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
576 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000577 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000578
Chris Lattneraaec2052010-01-19 19:46:13 +0000579 if (AddrSpace == 0)
580 if (const char *ZeroDirective = MAI.getZeroDirective()) {
581 OS << ZeroDirective << NumBytes;
582 if (FillValue != 0)
583 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000584 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000585 return;
586 }
587
588 // Emit a byte at a time.
589 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000590}
591
Daniel Dunbar84a29262009-06-24 19:25:34 +0000592void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
593 unsigned ValueSize,
594 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000595 // Some assemblers don't support non-power of two alignments, so we always
596 // emit alignments as a power of two if possible.
597 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000598 switch (ValueSize) {
599 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000600 case 1: OS << MAI.getAlignDirective(); break;
601 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000602 case 2: OS << ".p2alignw "; break;
603 case 4: OS << ".p2alignl "; break;
604 case 8: llvm_unreachable("Unsupported alignment size!");
605 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000606
Chris Lattner33adcfb2009-08-22 21:43:10 +0000607 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000608 OS << ByteAlignment;
609 else
610 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000611
Chris Lattner663c2d22009-08-19 06:12:02 +0000612 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000613 OS << ", 0x";
614 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000615
Jim Grosbach00545e12010-09-22 18:16:55 +0000616 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000617 OS << ", " << MaxBytesToEmit;
618 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000619 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000620 return;
621 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000622
Chris Lattner663c2d22009-08-19 06:12:02 +0000623 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000624 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000625 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000626 default: llvm_unreachable("Invalid size for machine code value!");
627 case 1: OS << ".balign"; break;
628 case 2: OS << ".balignw"; break;
629 case 4: OS << ".balignl"; break;
630 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000631 }
632
Chris Lattner663c2d22009-08-19 06:12:02 +0000633 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000634 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000635 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000636 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000637 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000638}
639
Kevin Enderby6e720482010-02-23 18:26:34 +0000640void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
641 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000642 // Emit with a text fill value.
643 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
644 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000645}
646
Daniel Dunbar821e3332009-08-31 08:09:28 +0000647void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000648 unsigned char Value) {
649 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000650 OS << ".org " << *Offset << ", " << (unsigned) Value;
651 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000652}
653
Chris Lattnera6594fc2010-01-25 18:58:59 +0000654
655void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
656 assert(MAI.hasSingleParameterDotFile());
657 OS << "\t.file\t";
658 PrintQuotedString(Filename, OS);
659 EmitEOL();
660}
661
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000662bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola89b93722010-12-10 07:39:47 +0000663 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000664 OS << "\t.file\t" << FileNo << ' ';
665 PrintQuotedString(Filename, OS);
666 EmitEOL();
667 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000668 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
669}
670
671void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
672 unsigned Column, unsigned Flags,
673 unsigned Isa,
674 unsigned Discriminator) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000675 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
676 Isa, Discriminator);
Rafael Espindola89b93722010-12-10 07:39:47 +0000677 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000678 return;
679
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000680 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
681 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
682 OS << " basic_block";
683 if (Flags & DWARF2_FLAG_PROLOGUE_END)
684 OS << " prologue_end";
685 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
686 OS << " epilogue_begin";
687
688 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
689 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
690 OS << " is_stmt ";
691
692 if (Flags & DWARF2_FLAG_IS_STMT)
693 OS << "1";
694 else
695 OS << "0";
696 }
697
698 if (Isa)
699 OS << "isa " << Isa;
700 if (Discriminator)
701 OS << "discriminator " << Discriminator;
702 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000703}
704
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000705bool MCAsmStreamer::EmitCFIStartProc() {
706 if (this->MCStreamer::EmitCFIStartProc())
707 return true;
708
709 OS << ".cfi_startproc";
710 EmitEOL();
711
712 return false;
713}
714
715bool MCAsmStreamer::EmitCFIEndProc() {
716 if (this->MCStreamer::EmitCFIEndProc())
717 return true;
718
719 OS << ".cfi_endproc";
720 EmitEOL();
721
722 return false;
723}
724
725bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
726 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
727 return true;
728
729 OS << ".cfi_def_cfa_offset " << Offset;
730 EmitEOL();
731
732 return false;
733}
734
735bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
736 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
737 return true;
738
739 OS << ".cfi_def_cfa_register " << Register;
740 EmitEOL();
741
742 return false;
743}
744
745bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
746 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
747 return true;
748
749 OS << ".cfi_offset " << Register << ", " << Offset;
750 EmitEOL();
751
752 return false;
753}
754
755bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
756 if (this->MCStreamer::EmitCFIPersonality(Sym))
757 return true;
758
759 OS << ".cfi_personality 0, " << *Sym;
760 EmitEOL();
761
762 return false;
763}
764
765bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
766 if (this->MCStreamer::EmitCFILsda(Sym))
767 return true;
768
769 OS << ".cfi_lsda 0, " << *Sym;
770 EmitEOL();
771
772 return false;
773}
774
Daniel Dunbar6b716532010-02-09 23:00:14 +0000775void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
776 raw_ostream &OS = GetCommentOS();
777 SmallString<256> Code;
778 SmallVector<MCFixup, 4> Fixups;
779 raw_svector_ostream VecOS(Code);
780 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
781 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000782
Daniel Dunbar6b716532010-02-09 23:00:14 +0000783 // If we are showing fixups, create symbolic markers in the encoded
784 // representation. We do this by making a per-bit map to the fixup item index,
785 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000786 SmallVector<uint8_t, 64> FixupMap;
787 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000788 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
789 FixupMap[i] = 0;
790
791 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
792 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000793 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000794 for (unsigned j = 0; j != Info.TargetSize; ++j) {
795 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
796 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
797 FixupMap[Index] = 1 + i;
798 }
799 }
800
801 OS << "encoding: [";
802 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
803 if (i)
804 OS << ',';
805
806 // See if all bits are the same map entry.
807 uint8_t MapEntry = FixupMap[i * 8 + 0];
808 for (unsigned j = 1; j != 8; ++j) {
809 if (FixupMap[i * 8 + j] == MapEntry)
810 continue;
811
812 MapEntry = uint8_t(~0U);
813 break;
814 }
815
816 if (MapEntry != uint8_t(~0U)) {
817 if (MapEntry == 0) {
818 OS << format("0x%02x", uint8_t(Code[i]));
819 } else {
820 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
821 OS << char('A' + MapEntry - 1);
822 }
823 } else {
824 // Otherwise, write out in binary.
825 OS << "0b";
826 for (unsigned j = 8; j--;) {
827 unsigned Bit = (Code[i] >> j) & 1;
Chris Lattner3170a3b2010-11-15 05:56:19 +0000828
829 unsigned FixupBit;
Rafael Espindola89b93722010-12-10 07:39:47 +0000830 if (getContext().getTargetAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +0000831 FixupBit = i * 8 + j;
832 else
833 FixupBit = i * 8 + (7-j);
834
835 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +0000836 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
837 OS << char('A' + MapEntry - 1);
838 } else
839 OS << Bit;
840 }
841 }
842 }
843 OS << "]\n";
844
845 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
846 MCFixup &F = Fixups[i];
Chris Lattner8d31de62010-02-11 21:27:18 +0000847 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +0000848 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +0000849 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +0000850 }
851}
852
853void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
854 assert(CurSection && "Cannot emit contents before setting section!");
855
Rafael Espindola89b93722010-12-10 07:39:47 +0000856 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000857 MCLineEntry::Make(this, getCurrentSection());
858
Daniel Dunbar6b716532010-02-09 23:00:14 +0000859 // Show the encoding in a comment if we have a code emitter.
860 if (Emitter)
861 AddEncodingComment(Inst);
862
Chris Lattner30d9a642010-02-09 00:54:51 +0000863 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000864 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000865 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000866 GetCommentOS() << "\n";
867 }
868
Daniel Dunbar67c076c2010-03-22 21:49:34 +0000869 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000870 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +0000871 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000872 else
873 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000874 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000875}
876
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000877/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000878/// the specified string in the output .s file. This capability is
879/// indicated by the hasRawTextSupport() predicate.
880void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000881 if (!String.empty() && String.back() == '\n')
882 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +0000883 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +0000884 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +0000885}
886
Daniel Dunbara11af532009-06-24 01:03:06 +0000887void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000888 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +0000889 if (getContext().hasDwarfFiles() && !UseLoc)
890 MCDwarfFileTable::Emit(this);
Daniel Dunbara11af532009-06-24 01:03:06 +0000891}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +0000892
Chris Lattner86e22112010-01-22 07:29:22 +0000893MCStreamer *llvm::createAsmStreamer(MCContext &Context,
894 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +0000895 bool isVerboseAsm, bool useLoc,
896 MCInstPrinter *IP,
Daniel Dunbar5532cf42010-02-10 01:41:14 +0000897 MCCodeEmitter *CE, bool ShowInst) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000898 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc,
899 IP, CE, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +0000900}