blob: 87516e04cf6f96b7f3ee308ea5031e9a2faae8b0 [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"
Evan Chengbfe36862011-07-26 20:57:44 +000018#include "llvm/MC/MCObjectFileInfo.h"
Evan Cheng0e6a0522011-07-18 20:57:22 +000019#include "llvm/MC/MCRegisterInfo.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000020#include "llvm/MC/MCSectionCOFF.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000022#include "llvm/MC/MCSymbol.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000023#include "llvm/MC/MCAsmBackend.h"
Chris Lattner4c42a6d2010-03-19 05:48:53 +000024#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000025#include "llvm/ADT/SmallString.h"
Bill Wendling916a94b2011-06-17 20:35:21 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner86e22112010-01-22 07:29:22 +000027#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000029#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000030#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000031#include "llvm/Support/FormattedStream.h"
Nick Lewycky44d798d2011-10-17 23:05:28 +000032#include "llvm/Support/PathV2.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000033#include <cctype>
Daniel Dunbara11af532009-06-24 01:03:06 +000034using namespace llvm;
35
36namespace {
37
Chris Lattner46a947d2009-08-17 04:17:34 +000038class MCAsmStreamer : public MCStreamer {
Bill Wendling916a94b2011-06-17 20:35:21 +000039protected:
Chris Lattner86e22112010-01-22 07:29:22 +000040 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000041 const MCAsmInfo &MAI;
Bill Wendling916a94b2011-06-17 20:35:21 +000042private:
Chris Lattner4c42a6d2010-03-19 05:48:53 +000043 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000044 OwningPtr<MCCodeEmitter> Emitter;
Evan Cheng78c10ee2011-07-25 23:24:55 +000045 OwningPtr<MCAsmBackend> AsmBackend;
Jim Grosbach00545e12010-09-22 18:16:55 +000046
Chris Lattner86e22112010-01-22 07:29:22 +000047 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000048 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000049
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000050 unsigned IsVerboseAsm : 1;
51 unsigned ShowInst : 1;
Rafael Espindola89b93722010-12-10 07:39:47 +000052 unsigned UseLoc : 1;
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000053 unsigned UseCFI : 1;
Nick Lewycky44d798d2011-10-17 23:05:28 +000054 unsigned UseDwarfDirectory : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000055
Rafael Espindola277abc82011-04-30 16:34:57 +000056 enum EHSymbolFlags { EHGlobal = 1,
57 EHWeakDefinition = 1 << 1,
58 EHPrivateExtern = 1 << 2 };
59 DenseMap<const MCSymbol*, unsigned> FlagMap;
60
Rafael Espindola5d4918d2010-12-04 03:21:47 +000061 bool needsSet(const MCExpr *Value);
62
Rafael Espindola6e032942011-05-30 20:20:15 +000063 void EmitRegisterName(int64_t Register);
Rafael Espindola547be262012-01-07 22:42:19 +000064 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
Rafael Espindola1fe97372012-01-09 00:17:29 +000065 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame);
Rafael Espindola6e032942011-05-30 20:20:15 +000066
Chris Lattner46a947d2009-08-17 04:17:34 +000067public:
Chris Lattner86e22112010-01-22 07:29:22 +000068 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000069 bool isVerboseAsm, bool useLoc, bool useCFI,
Nick Lewycky44d798d2011-10-17 23:05:28 +000070 bool useDwarfDirectory,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000071 MCInstPrinter *printer, MCCodeEmitter *emitter,
Evan Cheng78c10ee2011-07-25 23:24:55 +000072 MCAsmBackend *asmbackend,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000073 bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000074 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
Daniel Dunbar745dacc2010-12-16 03:05:59 +000075 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
76 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
Nick Lewycky44d798d2011-10-17 23:05:28 +000077 ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI),
78 UseDwarfDirectory(useDwarfDirectory) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000079 if (InstPrinter && IsVerboseAsm)
80 InstPrinter->setCommentStream(CommentStream);
81 }
Chris Lattner46a947d2009-08-17 04:17:34 +000082 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000083
Chris Lattner86e22112010-01-22 07:29:22 +000084 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000085 // If we don't have any comments, just emit a \n.
86 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000087 OS << '\n';
88 return;
89 }
90 EmitCommentsAndEOL();
91 }
92 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000093
94 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
95 /// all.
96 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000097
Chris Lattner91bead72010-04-03 21:35:55 +000098 /// hasRawTextSupport - We support EmitRawText.
99 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000100
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000101 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000102 /// file if applicable as a QoI issue to make the output of the compiler
103 /// more readable. This only affects the MCAsmStreamer, and only when
104 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000105 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000106
107 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
108 virtual void AddEncodingComment(const MCInst &Inst);
109
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000110 /// GetCommentOS - Return a raw_ostream that comments can be written to.
111 /// Unlike AddComment, you are required to terminate comments with \n if you
112 /// use this method.
113 virtual raw_ostream &GetCommentOS() {
114 if (!IsVerboseAsm)
115 return nulls(); // Discard comments unless in verbose asm mode.
116 return CommentStream;
117 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000118
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000119 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
120 virtual void AddBlankLine() {
121 EmitEOL();
122 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000123
Chris Lattner46a947d2009-08-17 04:17:34 +0000124 /// @name MCStreamer Interface
125 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000126
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000127 virtual void ChangeSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000128
Rafael Espindolad80781b2010-09-15 21:48:40 +0000129 virtual void InitSections() {
130 // FIXME, this is MachO specific, but the testsuite
131 // expects this.
132 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
133 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
134 0, SectionKind::getText()));
135 }
136
Chris Lattner46a947d2009-08-17 04:17:34 +0000137 virtual void EmitLabel(MCSymbol *Symbol);
Rafael Espindola277abc82011-04-30 16:34:57 +0000138 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
139 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000140 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000141 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000142
Daniel Dunbar821e3332009-08-31 08:09:28 +0000143 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000144 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000145 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
146 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000147 const MCSymbol *Label,
148 unsigned PointerSize);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000149 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
150 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000151
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000152 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000153
Chris Lattner46a947d2009-08-17 04:17:34 +0000154 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000155 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
156 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
157 virtual void EmitCOFFSymbolType(int Type);
158 virtual void EndCOFFSymbolDef();
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000159 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattner99328ad2010-01-25 07:52:13 +0000160 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000161 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000162 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000163
Chris Lattner9eb158d2010-01-23 07:47:02 +0000164 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
165 ///
166 /// @param Symbol - The common symbol to emit.
167 /// @param Size - The size of the common symbol.
Benjamin Kramer36a16012011-09-01 23:04:27 +0000168 /// @param Size - The alignment of the common symbol in bytes.
169 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
170 unsigned ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000171
Daniel Dunbar8751b942009-08-28 05:48:22 +0000172 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000173 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000174
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000175 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
176 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000177
Chris Lattneraaec2052010-01-19 19:46:13 +0000178 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000179
Rafael Espindola89b93722010-12-10 07:39:47 +0000180 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000181 unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000182 virtual void EmitIntValue(uint64_t Value, unsigned Size,
183 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000184
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000185 virtual void EmitULEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000186
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000187 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000188
Chris Lattner718fb592010-01-25 21:28:50 +0000189 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000190
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000191
Chris Lattneraaec2052010-01-19 19:46:13 +0000192 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
193 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000194
Chris Lattner46a947d2009-08-17 04:17:34 +0000195 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
196 unsigned ValueSize = 1,
197 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000198
Kevin Enderby6e720482010-02-23 18:26:34 +0000199 virtual void EmitCodeAlignment(unsigned ByteAlignment,
200 unsigned MaxBytesToEmit = 0);
201
Daniel Dunbar821e3332009-08-31 08:09:28 +0000202 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000203 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000204
Chris Lattnera6594fc2010-01-25 18:58:59 +0000205 virtual void EmitFileDirective(StringRef Filename);
Nick Lewycky44d798d2011-10-17 23:05:28 +0000206 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
207 StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000208 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
209 unsigned Column, unsigned Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000210 unsigned Isa, unsigned Discriminator,
211 StringRef FileName);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000212
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000213 virtual void EmitCFISections(bool EH, bool Debug);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000214 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
215 virtual void EmitCFIDefCfaOffset(int64_t Offset);
216 virtual void EmitCFIDefCfaRegister(int64_t Register);
217 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
218 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
219 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
220 virtual void EmitCFIRememberState();
221 virtual void EmitCFIRestoreState();
222 virtual void EmitCFISameValue(int64_t Register);
223 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
224 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000225
Charles Davisfbc539f2011-05-22 21:12:15 +0000226 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
Charles Davis0e30f022011-05-18 04:58:05 +0000227 virtual void EmitWin64EHEndProc();
Charles Davisf0709012011-05-18 20:54:10 +0000228 virtual void EmitWin64EHStartChained();
229 virtual void EmitWin64EHEndChained();
Charles Davis440596f2011-05-19 17:46:39 +0000230 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
231 bool Except);
232 virtual void EmitWin64EHHandlerData();
Charles Davisc3b16282011-05-19 19:35:55 +0000233 virtual void EmitWin64EHPushReg(unsigned Register);
234 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
235 virtual void EmitWin64EHAllocStack(unsigned Size);
236 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
237 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
Charles Davis0e30f022011-05-18 04:58:05 +0000238 virtual void EmitWin64EHPushFrame(bool Code);
239 virtual void EmitWin64EHEndProlog();
240
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000241 virtual void EmitFnStart();
242 virtual void EmitFnEnd();
243 virtual void EmitCantUnwind();
244 virtual void EmitPersonality(const MCSymbol *Personality);
245 virtual void EmitHandlerData();
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000246 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
247 virtual void EmitPad(int64_t Offset);
248 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool);
249
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000250
Chris Lattnera6594fc2010-01-25 18:58:59 +0000251 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000252
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000253 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000254 /// the specified string in the output .s file. This capability is
255 /// indicated by the hasRawTextSupport() predicate.
256 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000257
Rafael Espindola99b42372012-01-07 03:13:18 +0000258 virtual void FinishImpl();
Jim Grosbach00545e12010-09-22 18:16:55 +0000259
Chris Lattner46a947d2009-08-17 04:17:34 +0000260 /// @}
261};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000262
Chris Lattner46a947d2009-08-17 04:17:34 +0000263} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000264
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000265/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000266/// file if applicable as a QoI issue to make the output of the compiler
267/// more readable. This only affects the MCAsmStreamer, and only when
268/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000269void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000270 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000271
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000272 // Make sure that CommentStream is flushed.
273 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000274
Chris Lattner86e22112010-01-22 07:29:22 +0000275 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000276 // Each comment goes on its own line.
277 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000278
Chris Lattner14ca1772010-01-22 21:16:10 +0000279 // Tell the comment stream that the vector changed underneath it.
280 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000281}
282
283void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000284 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
285 OS << '\n';
286 return;
287 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000288
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000289 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000290 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000291
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000292 assert(Comments.back() == '\n' &&
293 "Comment array not newline terminated");
294 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000295 // Emit a line of comments.
296 OS.PadToColumn(MAI.getCommentColumn());
297 size_t Position = Comments.find('\n');
298 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000299
Chris Lattner86e22112010-01-22 07:29:22 +0000300 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000301 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000302
Chris Lattner14ca1772010-01-22 21:16:10 +0000303 CommentToEmit.clear();
304 // Tell the comment stream that the vector changed underneath it.
305 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000306}
307
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000308static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
309 assert(Bytes && "Invalid size!");
310 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
311}
312
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000313void MCAsmStreamer::ChangeSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000314 assert(Section && "Cannot switch to a null section!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000315 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000316}
317
Rafael Espindola277abc82011-04-30 16:34:57 +0000318void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
319 MCSymbol *EHSymbol) {
320 if (UseCFI)
321 return;
322
323 unsigned Flags = FlagMap.lookup(Symbol);
324
325 if (Flags & EHGlobal)
326 EmitSymbolAttribute(EHSymbol, MCSA_Global);
327 if (Flags & EHWeakDefinition)
328 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
329 if (Flags & EHPrivateExtern)
330 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
331}
332
Daniel Dunbara11af532009-06-24 01:03:06 +0000333void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000334 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaed708f92011-04-27 15:21:19 +0000335 MCStreamer::EmitLabel(Symbol);
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000336
Chris Lattnere07b75e2010-09-22 22:19:53 +0000337 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000338 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000339}
340
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000341void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000342 switch (Flag) {
Jason W Kimafd1cc22010-09-30 02:45:56 +0000343 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000344 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Evan Chengbd27f5a2011-07-27 00:38:12 +0000345 case MCAF_Code16: OS << '\t'<< MAI.getCode16Directive(); break;
346 case MCAF_Code32: OS << '\t'<< MAI.getCode32Directive(); break;
347 case MCAF_Code64: OS << '\t'<< MAI.getCode64Directive(); break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000348 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000349 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000350}
351
Jim Grosbachce792992010-11-05 22:08:08 +0000352void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
353 // This needs to emit to a temporary string to get properly quoted
354 // MCSymbols when they have spaces in them.
355 OS << "\t.thumb_func";
Rafael Espindola64695402011-05-16 16:17:21 +0000356 // Only Mach-O hasSubsectionsViaSymbols()
357 if (MAI.hasSubsectionsViaSymbols())
Jim Grosbachce792992010-11-05 22:08:08 +0000358 OS << '\t' << *Func;
359 EmitEOL();
360}
361
Daniel Dunbar821e3332009-08-31 08:09:28 +0000362void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000363 OS << *Symbol << " = " << *Value;
364 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000365
366 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000367 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000368}
369
Rafael Espindola484291c2010-11-01 14:28:48 +0000370void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
371 OS << ".weakref " << *Alias << ", " << *Symbol;
372 EmitEOL();
373}
374
Rafael Espindola32a006e2010-12-03 00:55:40 +0000375void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
376 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000377 const MCSymbol *Label,
378 unsigned PointerSize) {
379 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000380}
381
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000382void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
383 const MCSymbol *Label) {
384 EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
385 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
Rafael Espindolaa6f26782011-05-19 21:40:34 +0000386 AddrDelta = ForceExpAbs(AddrDelta);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000387 EmitValue(AddrDelta, 4);
388}
389
390
Daniel Dunbarfffff912009-10-16 01:34:54 +0000391void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000392 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000393 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000394 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000395 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
396 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
397 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
398 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
399 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
400 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000401 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000402 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000403 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000404 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
405 switch (Attribute) {
406 default: assert(0 && "Unknown ELF .type");
407 case MCSA_ELF_TypeFunction: OS << "function"; break;
408 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
409 case MCSA_ELF_TypeObject: OS << "object"; break;
410 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
411 case MCSA_ELF_TypeCommon: OS << "common"; break;
412 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000413 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000414 }
415 EmitEOL();
416 return;
417 case MCSA_Global: // .globl/.global
418 OS << MAI.getGlobalDirective();
Rafael Espindola277abc82011-04-30 16:34:57 +0000419 FlagMap[Symbol] |= EHGlobal;
Chris Lattnered0ab152010-01-25 18:30:45 +0000420 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000421 case MCSA_Hidden: OS << "\t.hidden\t"; break;
422 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
423 case MCSA_Internal: OS << "\t.internal\t"; break;
424 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
425 case MCSA_Local: OS << "\t.local\t"; break;
426 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000427 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000428 case MCSA_PrivateExtern:
429 OS << "\t.private_extern\t";
430 FlagMap[Symbol] |= EHPrivateExtern;
431 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000432 case MCSA_Protected: OS << "\t.protected\t"; break;
433 case MCSA_Reference: OS << "\t.reference\t"; break;
434 case MCSA_Weak: OS << "\t.weak\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000435 case MCSA_WeakDefinition:
436 OS << "\t.weak_definition\t";
437 FlagMap[Symbol] |= EHWeakDefinition;
438 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000439 // .weak_reference
440 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000441 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000442 }
443
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000444 OS << *Symbol;
445 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000446}
447
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000448void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000449 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
450 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000451}
452
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000453void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
454 OS << "\t.def\t " << *Symbol << ';';
455 EmitEOL();
456}
457
458void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
459 OS << "\t.scl\t" << StorageClass << ';';
460 EmitEOL();
461}
462
463void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
464 OS << "\t.type\t" << Type << ';';
465 EmitEOL();
466}
467
468void MCAsmStreamer::EndCOFFSymbolDef() {
469 OS << "\t.endef";
470 EmitEOL();
471}
472
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000473void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
474 OS << "\t.secrel32\t" << *Symbol << '\n';
475 EmitEOL();
476}
477
Chris Lattner99328ad2010-01-25 07:52:13 +0000478void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
479 assert(MAI.hasDotTypeDotSizeDirective());
480 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
481}
482
Chris Lattner9eb158d2010-01-23 07:47:02 +0000483void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000484 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000485 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000486 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000487 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000488 OS << ',' << ByteAlignment;
489 else
490 OS << ',' << Log2_32(ByteAlignment);
491 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000492 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000493}
494
Chris Lattner9eb158d2010-01-23 07:47:02 +0000495/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
496///
497/// @param Symbol - The common symbol to emit.
498/// @param Size - The size of the common symbol.
Benjamin Kramer36a16012011-09-01 23:04:27 +0000499void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
500 unsigned ByteAlign) {
501 assert(MAI.getLCOMMDirectiveType() != LCOMM::None &&
502 "Doesn't have .lcomm, can't emit it!");
Chris Lattner9eb158d2010-01-23 07:47:02 +0000503 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
Benjamin Kramer36a16012011-09-01 23:04:27 +0000504 if (ByteAlign > 1) {
505 assert(MAI.getLCOMMDirectiveType() == LCOMM::ByteAlignment &&
506 "Alignment not supported on .lcomm!");
507 OS << ',' << ByteAlign;
508 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000509 EmitEOL();
510}
511
Daniel Dunbar8751b942009-08-28 05:48:22 +0000512void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000513 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000514 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000515 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000516
Chris Lattner93b6db32009-08-08 23:39:42 +0000517 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000518 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000519 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000520
Chris Lattner9be3fee2009-07-10 22:20:30 +0000521 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000522 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000523 if (ByteAlignment != 0)
524 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000525 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000526 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000527}
528
Eric Christopherd04d98d2010-05-17 02:13:02 +0000529// .tbss sym, size, align
530// This depends that the symbol has already been mangled from the original,
531// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000532void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
533 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000534 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000535 // Instead of using the Section we'll just use the shortcut.
536 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000537 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000538
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000539 // Output align if we have it. We default to 1 so don't bother printing
540 // that.
541 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000542
Eric Christopher482eba02010-05-14 01:50:28 +0000543 EmitEOL();
544}
545
Chris Lattner12e555c2010-01-23 00:15:00 +0000546static inline char toOctal(int X) { return (X&7)+'0'; }
547
Chris Lattnera6594fc2010-01-25 18:58:59 +0000548static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
549 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000550
Chris Lattnera6594fc2010-01-25 18:58:59 +0000551 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
552 unsigned char C = Data[i];
553 if (C == '"' || C == '\\') {
554 OS << '\\' << (char)C;
555 continue;
556 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000557
Chris Lattnera6594fc2010-01-25 18:58:59 +0000558 if (isprint((unsigned char)C)) {
559 OS << (char)C;
560 continue;
561 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000562
Chris Lattnera6594fc2010-01-25 18:58:59 +0000563 switch (C) {
564 case '\b': OS << "\\b"; break;
565 case '\f': OS << "\\f"; break;
566 case '\n': OS << "\\n"; break;
567 case '\r': OS << "\\r"; break;
568 case '\t': OS << "\\t"; break;
569 default:
570 OS << '\\';
571 OS << toOctal(C >> 6);
572 OS << toOctal(C >> 3);
573 OS << toOctal(C >> 0);
574 break;
575 }
576 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000577
Chris Lattnera6594fc2010-01-25 18:58:59 +0000578 OS << '"';
579}
580
581
Chris Lattneraaec2052010-01-19 19:46:13 +0000582void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000583 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000584 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000585
Chris Lattner12e555c2010-01-23 00:15:00 +0000586 if (Data.size() == 1) {
587 OS << MAI.getData8bitsDirective(AddrSpace);
588 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000589 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000590 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000591 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000592
593 // If the data ends with 0 and the target supports .asciz, use it, otherwise
594 // use .ascii
595 if (MAI.getAscizDirective() && Data.back() == 0) {
596 OS << MAI.getAscizDirective();
597 Data = Data.substr(0, Data.size()-1);
598 } else {
599 OS << MAI.getAsciiDirective();
600 }
601
Chris Lattnera6594fc2010-01-25 18:58:59 +0000602 OS << ' ';
603 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000604 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000605}
606
Rafael Espindola2df042c2010-12-03 02:54:21 +0000607void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
608 unsigned AddrSpace) {
609 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
610}
611
Rafael Espindola89b93722010-12-10 07:39:47 +0000612void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000613 unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000614 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000615 const char *Directive = 0;
616 switch (Size) {
617 default: break;
618 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
619 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
620 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000621 case 8:
622 Directive = MAI.getData64bitsDirective(AddrSpace);
623 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
624 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000625 int64_t IntValue;
626 if (!Value->EvaluateAsAbsolute(IntValue))
627 report_fatal_error("Don't know how to emit this value.");
Evan Cheng1be0e272011-07-15 02:09:41 +0000628 if (getContext().getAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000629 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
630 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000631 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000632 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
633 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000634 }
635 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000636 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000637
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000638 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000639 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000640 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000641}
642
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000643void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000644 int64_t IntValue;
645 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000646 EmitULEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000647 return;
648 }
Rafael Espindola73873452010-11-04 18:17:08 +0000649 assert(MAI.hasLEB128() && "Cannot print a .uleb");
650 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000651 EmitEOL();
652}
653
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000654void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000655 int64_t IntValue;
656 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000657 EmitSLEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000658 return;
659 }
Rafael Espindola73873452010-11-04 18:17:08 +0000660 assert(MAI.hasLEB128() && "Cannot print a .sleb");
661 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000662 EmitEOL();
663}
664
Chris Lattner718fb592010-01-25 21:28:50 +0000665void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
666 assert(MAI.getGPRel32Directive() != 0);
667 OS << MAI.getGPRel32Directive() << *Value;
668 EmitEOL();
669}
670
671
Chris Lattner6113b3d2010-01-19 18:52:28 +0000672/// EmitFill - Emit NumBytes bytes worth of the value specified by
673/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000674void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
675 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000676 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000677
Chris Lattneraaec2052010-01-19 19:46:13 +0000678 if (AddrSpace == 0)
679 if (const char *ZeroDirective = MAI.getZeroDirective()) {
680 OS << ZeroDirective << NumBytes;
681 if (FillValue != 0)
682 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000683 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000684 return;
685 }
686
687 // Emit a byte at a time.
688 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000689}
690
Daniel Dunbar84a29262009-06-24 19:25:34 +0000691void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
692 unsigned ValueSize,
693 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000694 // Some assemblers don't support non-power of two alignments, so we always
695 // emit alignments as a power of two if possible.
696 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000697 switch (ValueSize) {
698 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000699 case 1: OS << MAI.getAlignDirective(); break;
700 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000701 case 2: OS << ".p2alignw "; break;
702 case 4: OS << ".p2alignl "; break;
703 case 8: llvm_unreachable("Unsupported alignment size!");
704 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000705
Chris Lattner33adcfb2009-08-22 21:43:10 +0000706 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000707 OS << ByteAlignment;
708 else
709 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000710
Chris Lattner663c2d22009-08-19 06:12:02 +0000711 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000712 OS << ", 0x";
713 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000714
Jim Grosbach00545e12010-09-22 18:16:55 +0000715 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000716 OS << ", " << MaxBytesToEmit;
717 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000718 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000719 return;
720 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000721
Chris Lattner663c2d22009-08-19 06:12:02 +0000722 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000723 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000724 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000725 default: llvm_unreachable("Invalid size for machine code value!");
726 case 1: OS << ".balign"; break;
727 case 2: OS << ".balignw"; break;
728 case 4: OS << ".balignl"; break;
729 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000730 }
731
Chris Lattner663c2d22009-08-19 06:12:02 +0000732 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000733 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000734 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000735 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000736 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000737}
738
Kevin Enderby6e720482010-02-23 18:26:34 +0000739void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
740 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000741 // Emit with a text fill value.
742 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
743 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000744}
745
Daniel Dunbar821e3332009-08-31 08:09:28 +0000746void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000747 unsigned char Value) {
748 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000749 OS << ".org " << *Offset << ", " << (unsigned) Value;
750 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000751}
752
Chris Lattnera6594fc2010-01-25 18:58:59 +0000753
754void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
755 assert(MAI.hasSingleParameterDotFile());
756 OS << "\t.file\t";
757 PrintQuotedString(Filename, OS);
758 EmitEOL();
759}
760
Nick Lewycky44d798d2011-10-17 23:05:28 +0000761bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
762 StringRef Filename) {
763 if (!UseDwarfDirectory && !Directory.empty()) {
764 if (sys::path::is_absolute(Filename))
765 return EmitDwarfFileDirective(FileNo, "", Filename);
766
767 SmallString<128> FullPathName = Directory;
768 sys::path::append(FullPathName, Filename);
769 return EmitDwarfFileDirective(FileNo, "", FullPathName);
770 }
771
Rafael Espindola89b93722010-12-10 07:39:47 +0000772 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000773 OS << "\t.file\t" << FileNo << ' ';
Nick Lewycky44d798d2011-10-17 23:05:28 +0000774 if (!Directory.empty()) {
775 PrintQuotedString(Directory, OS);
776 OS << ' ';
777 }
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000778 PrintQuotedString(Filename, OS);
779 EmitEOL();
780 }
Nick Lewycky44d798d2011-10-17 23:05:28 +0000781 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Directory, Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000782}
783
784void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
785 unsigned Column, unsigned Flags,
786 unsigned Isa,
Devang Patel3f3bf932011-04-18 20:26:49 +0000787 unsigned Discriminator,
788 StringRef FileName) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000789 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000790 Isa, Discriminator, FileName);
Rafael Espindola89b93722010-12-10 07:39:47 +0000791 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000792 return;
793
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000794 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
795 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
796 OS << " basic_block";
797 if (Flags & DWARF2_FLAG_PROLOGUE_END)
798 OS << " prologue_end";
799 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
800 OS << " epilogue_begin";
801
802 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
803 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
804 OS << " is_stmt ";
805
806 if (Flags & DWARF2_FLAG_IS_STMT)
807 OS << "1";
808 else
809 OS << "0";
810 }
811
812 if (Isa)
813 OS << "isa " << Isa;
814 if (Discriminator)
815 OS << "discriminator " << Discriminator;
Devang Patel3f3bf932011-04-18 20:26:49 +0000816
817 if (IsVerboseAsm) {
818 OS.PadToColumn(MAI.getCommentColumn());
819 OS << MAI.getCommentString() << ' ' << FileName << ':'
820 << Line << ':' << Column;
821 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000822 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000823}
824
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000825void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
826 MCStreamer::EmitCFISections(EH, Debug);
827
828 if (!UseCFI)
829 return;
830
831 OS << "\t.cfi_sections ";
832 if (EH) {
833 OS << ".eh_frame";
834 if (Debug)
835 OS << ", .debug_frame";
836 } else if (Debug) {
837 OS << ".debug_frame";
838 }
839
840 EmitEOL();
841}
842
Rafael Espindola547be262012-01-07 22:42:19 +0000843void MCAsmStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
844 if (!UseCFI) {
845 RecordProcStart(Frame);
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000846 return;
Rafael Espindola547be262012-01-07 22:42:19 +0000847 }
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000848
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000849 OS << "\t.cfi_startproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000850 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000851}
852
Rafael Espindola1fe97372012-01-09 00:17:29 +0000853void MCAsmStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
854 if (!UseCFI) {
855 RecordProcEnd(Frame);
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000856 return;
Rafael Espindola1fe97372012-01-09 00:17:29 +0000857 }
858
859 // Put a dummy non-null value in Frame.End to mark that this frame has been
860 // closed.
861 Frame.End = (MCSymbol *) 1;
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000862
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000863 OS << "\t.cfi_endproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000864 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000865}
866
Rafael Espindola6e032942011-05-30 20:20:15 +0000867void MCAsmStreamer::EmitRegisterName(int64_t Register) {
Akira Hatanaka3014b2f2011-07-07 20:30:33 +0000868 if (InstPrinter && !MAI.useDwarfRegNumForCFI()) {
Evan Cheng0e6a0522011-07-18 20:57:22 +0000869 const MCRegisterInfo &MRI = getContext().getRegisterInfo();
870 unsigned LLVMRegister = MRI.getLLVMRegNum(Register, true);
Rafael Espindolacde4ce42011-06-02 02:34:55 +0000871 InstPrinter->printRegName(OS, LLVMRegister);
Rafael Espindola6e032942011-05-30 20:20:15 +0000872 } else {
873 OS << Register;
874 }
875}
876
Rafael Espindola066c2f42011-04-12 23:59:07 +0000877void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindola7f46b082011-04-29 21:41:06 +0000878 MCStreamer::EmitCFIDefCfa(Register, Offset);
879
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000880 if (!UseCFI)
881 return;
882
Rafael Espindola6e032942011-05-30 20:20:15 +0000883 OS << "\t.cfi_def_cfa ";
884 EmitRegisterName(Register);
885 OS << ", " << Offset;
Rafael Espindola7f46b082011-04-29 21:41:06 +0000886 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000887}
888
889void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
890 MCStreamer::EmitCFIDefCfaOffset(Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000891
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000892 if (!UseCFI)
893 return;
894
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000895 OS << "\t.cfi_def_cfa_offset " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000896 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000897}
898
Rafael Espindola066c2f42011-04-12 23:59:07 +0000899void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
900 MCStreamer::EmitCFIDefCfaRegister(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000901
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000902 if (!UseCFI)
903 return;
904
Rafael Espindola6e032942011-05-30 20:20:15 +0000905 OS << "\t.cfi_def_cfa_register ";
906 EmitRegisterName(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000907 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000908}
909
Rafael Espindola066c2f42011-04-12 23:59:07 +0000910void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
911 this->MCStreamer::EmitCFIOffset(Register, Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000912
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000913 if (!UseCFI)
914 return;
915
Rafael Espindola6e032942011-05-30 20:20:15 +0000916 OS << "\t.cfi_offset ";
917 EmitRegisterName(Register);
918 OS << ", " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000919 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000920}
921
Rafael Espindola066c2f42011-04-12 23:59:07 +0000922void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000923 unsigned Encoding) {
Rafael Espindola066c2f42011-04-12 23:59:07 +0000924 MCStreamer::EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000925
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000926 if (!UseCFI)
927 return;
928
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000929 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000930 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000931}
932
Rafael Espindola066c2f42011-04-12 23:59:07 +0000933void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
934 MCStreamer::EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000935
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000936 if (!UseCFI)
937 return;
938
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000939 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000940 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000941}
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000942
Rafael Espindola066c2f42011-04-12 23:59:07 +0000943void MCAsmStreamer::EmitCFIRememberState() {
944 MCStreamer::EmitCFIRememberState();
945
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000946 if (!UseCFI)
947 return;
948
Rafael Espindola066c2f42011-04-12 23:59:07 +0000949 OS << "\t.cfi_remember_state";
950 EmitEOL();
951}
952
953void MCAsmStreamer::EmitCFIRestoreState() {
954 MCStreamer::EmitCFIRestoreState();
955
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000956 if (!UseCFI)
957 return;
958
Rafael Espindola066c2f42011-04-12 23:59:07 +0000959 OS << "\t.cfi_restore_state";
960 EmitEOL();
961}
962
963void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
964 MCStreamer::EmitCFISameValue(Register);
965
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000966 if (!UseCFI)
967 return;
968
Rafael Espindola6e032942011-05-30 20:20:15 +0000969 OS << "\t.cfi_same_value ";
970 EmitRegisterName(Register);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000971 EmitEOL();
972}
973
974void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
975 MCStreamer::EmitCFIRelOffset(Register, Offset);
976
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000977 if (!UseCFI)
978 return;
979
Rafael Espindola6e032942011-05-30 20:20:15 +0000980 OS << "\t.cfi_rel_offset ";
981 EmitRegisterName(Register);
982 OS << ", " << Offset;
Rafael Espindola066c2f42011-04-12 23:59:07 +0000983 EmitEOL();
984}
985
986void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
987 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
988
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000989 if (!UseCFI)
990 return;
991
Rafael Espindola066c2f42011-04-12 23:59:07 +0000992 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
993 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000994}
995
Charles Davisfbc539f2011-05-22 21:12:15 +0000996void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
Charles Daviscde87e22011-05-20 18:19:22 +0000997 MCStreamer::EmitWin64EHStartProc(Symbol);
998
Charles Davis440596f2011-05-19 17:46:39 +0000999 OS << ".seh_proc " << *Symbol;
Charles Davis0e30f022011-05-18 04:58:05 +00001000 EmitEOL();
1001}
1002
Charles Davis8b3e5e52011-05-18 22:13:51 +00001003void MCAsmStreamer::EmitWin64EHEndProc() {
Charles Daviscde87e22011-05-20 18:19:22 +00001004 MCStreamer::EmitWin64EHEndProc();
1005
Charles Davis440596f2011-05-19 17:46:39 +00001006 OS << "\t.seh_endproc";
Charles Davis0e30f022011-05-18 04:58:05 +00001007 EmitEOL();
1008}
1009
Charles Davis8b3e5e52011-05-18 22:13:51 +00001010void MCAsmStreamer::EmitWin64EHStartChained() {
Charles Daviscde87e22011-05-20 18:19:22 +00001011 MCStreamer::EmitWin64EHStartChained();
1012
Charles Davis440596f2011-05-19 17:46:39 +00001013 OS << "\t.seh_startchained";
Charles Davisf0709012011-05-18 20:54:10 +00001014 EmitEOL();
1015}
1016
Charles Davis8b3e5e52011-05-18 22:13:51 +00001017void MCAsmStreamer::EmitWin64EHEndChained() {
Charles Daviscde87e22011-05-20 18:19:22 +00001018 MCStreamer::EmitWin64EHEndChained();
1019
Charles Davis440596f2011-05-19 17:46:39 +00001020 OS << "\t.seh_endchained";
Charles Davisf0709012011-05-18 20:54:10 +00001021 EmitEOL();
1022}
1023
Charles Davis440596f2011-05-19 17:46:39 +00001024void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
1025 bool Except) {
Charles Daviscde87e22011-05-20 18:19:22 +00001026 MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
1027
Charles Davis440596f2011-05-19 17:46:39 +00001028 OS << "\t.seh_handler " << *Sym;
1029 if (Unwind)
1030 OS << ", @unwind";
1031 if (Except)
1032 OS << ", @except";
Charles Davisf0709012011-05-18 20:54:10 +00001033 EmitEOL();
1034}
1035
Evan Chenge76a33b2011-07-20 05:58:47 +00001036static const MCSection *getWin64EHTableSection(StringRef suffix,
1037 MCContext &context) {
1038 // FIXME: This doesn't belong in MCObjectFileInfo. However,
1039 /// this duplicate code in MCWin64EH.cpp.
1040 if (suffix == "")
1041 return context.getObjectFileInfo()->getXDataSection();
1042 return context.getCOFFSection((".xdata"+suffix).str(),
1043 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1044 COFF::IMAGE_SCN_MEM_READ |
1045 COFF::IMAGE_SCN_MEM_WRITE,
1046 SectionKind::getDataRel());
1047}
1048
Charles Davis440596f2011-05-19 17:46:39 +00001049void MCAsmStreamer::EmitWin64EHHandlerData() {
Charles Daviscde87e22011-05-20 18:19:22 +00001050 MCStreamer::EmitWin64EHHandlerData();
1051
Charles Davis410ef2b2011-05-25 21:43:45 +00001052 // Switch sections. Don't call SwitchSection directly, because that will
1053 // cause the section switch to be visible in the emitted assembly.
1054 // We only do this so the section switch that terminates the handler
1055 // data block is visible.
Charles Davis7b06b732011-05-27 19:09:24 +00001056 MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
1057 StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
Evan Chenge76a33b2011-07-20 05:58:47 +00001058 const MCSection *xdataSect = getWin64EHTableSection(suffix, getContext());
Charles Davis410ef2b2011-05-25 21:43:45 +00001059 if (xdataSect)
1060 SwitchSectionNoChange(xdataSect);
1061
Charles Davis440596f2011-05-19 17:46:39 +00001062 OS << "\t.seh_handlerdata";
Charles Davisf0709012011-05-18 20:54:10 +00001063 EmitEOL();
1064}
1065
Charles Davisc3b16282011-05-19 19:35:55 +00001066void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
Charles Daviscde87e22011-05-20 18:19:22 +00001067 MCStreamer::EmitWin64EHPushReg(Register);
1068
Charles Davis440596f2011-05-19 17:46:39 +00001069 OS << "\t.seh_pushreg " << Register;
Charles Davis0e30f022011-05-18 04:58:05 +00001070 EmitEOL();
1071}
1072
Charles Davisc3b16282011-05-19 19:35:55 +00001073void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001074 MCStreamer::EmitWin64EHSetFrame(Register, Offset);
1075
Charles Davis440596f2011-05-19 17:46:39 +00001076 OS << "\t.seh_setframe " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001077 EmitEOL();
1078}
1079
Charles Davisc3b16282011-05-19 19:35:55 +00001080void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
Charles Daviscde87e22011-05-20 18:19:22 +00001081 MCStreamer::EmitWin64EHAllocStack(Size);
1082
Charles Davis440596f2011-05-19 17:46:39 +00001083 OS << "\t.seh_stackalloc " << Size;
Charles Davis0e30f022011-05-18 04:58:05 +00001084 EmitEOL();
1085}
1086
Charles Davisc3b16282011-05-19 19:35:55 +00001087void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001088 MCStreamer::EmitWin64EHSaveReg(Register, Offset);
1089
Charles Davis440596f2011-05-19 17:46:39 +00001090 OS << "\t.seh_savereg " << Register << ", " << Offset;
1091 EmitEOL();
1092}
1093
Charles Davisc3b16282011-05-19 19:35:55 +00001094void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001095 MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
1096
Charles Davis440596f2011-05-19 17:46:39 +00001097 OS << "\t.seh_savexmm " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001098 EmitEOL();
1099}
1100
Charles Davis8b3e5e52011-05-18 22:13:51 +00001101void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
Charles Daviscde87e22011-05-20 18:19:22 +00001102 MCStreamer::EmitWin64EHPushFrame(Code);
1103
Charles Davis440596f2011-05-19 17:46:39 +00001104 OS << "\t.seh_pushframe";
Charles Davis0e30f022011-05-18 04:58:05 +00001105 if (Code)
Charles Davis440596f2011-05-19 17:46:39 +00001106 OS << " @code";
Charles Davis0e30f022011-05-18 04:58:05 +00001107 EmitEOL();
1108}
1109
Charles Davis8b3e5e52011-05-18 22:13:51 +00001110void MCAsmStreamer::EmitWin64EHEndProlog(void) {
Charles Daviscde87e22011-05-20 18:19:22 +00001111 MCStreamer::EmitWin64EHEndProlog();
1112
Charles Davis440596f2011-05-19 17:46:39 +00001113 OS << "\t.seh_endprologue";
Charles Davis0e30f022011-05-18 04:58:05 +00001114 EmitEOL();
1115}
1116
Daniel Dunbar6b716532010-02-09 23:00:14 +00001117void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
1118 raw_ostream &OS = GetCommentOS();
1119 SmallString<256> Code;
1120 SmallVector<MCFixup, 4> Fixups;
1121 raw_svector_ostream VecOS(Code);
1122 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
1123 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +00001124
Daniel Dunbar6b716532010-02-09 23:00:14 +00001125 // If we are showing fixups, create symbolic markers in the encoded
1126 // representation. We do this by making a per-bit map to the fixup item index,
1127 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +00001128 SmallVector<uint8_t, 64> FixupMap;
1129 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001130 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1131 FixupMap[i] = 0;
1132
1133 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1134 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001135 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001136 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1137 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1138 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1139 FixupMap[Index] = 1 + i;
1140 }
1141 }
1142
Evan Cheng5c2eef62011-07-08 22:49:42 +00001143 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001144 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
Daniel Dunbar6b716532010-02-09 23:00:14 +00001145 OS << "encoding: [";
1146 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1147 if (i)
1148 OS << ',';
1149
1150 // See if all bits are the same map entry.
1151 uint8_t MapEntry = FixupMap[i * 8 + 0];
1152 for (unsigned j = 1; j != 8; ++j) {
1153 if (FixupMap[i * 8 + j] == MapEntry)
1154 continue;
1155
1156 MapEntry = uint8_t(~0U);
1157 break;
1158 }
1159
1160 if (MapEntry != uint8_t(~0U)) {
1161 if (MapEntry == 0) {
1162 OS << format("0x%02x", uint8_t(Code[i]));
1163 } else {
Evan Cheng82caf1a2011-01-13 21:45:26 +00001164 if (Code[i]) {
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001165 // FIXME: Some of the 8 bits require fix up.
Evan Cheng82caf1a2011-01-13 21:45:26 +00001166 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1167 << char('A' + MapEntry - 1) << '\'';
1168 } else
1169 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001170 }
1171 } else {
1172 // Otherwise, write out in binary.
1173 OS << "0b";
Jay Foad95c3e482011-06-23 09:09:15 +00001174 for (unsigned j = 8; j--;) {
Daniel Dunbar6b716532010-02-09 23:00:14 +00001175 unsigned Bit = (Code[i] >> j) & 1;
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001176
Chris Lattner3170a3b2010-11-15 05:56:19 +00001177 unsigned FixupBit;
Evan Cheng1be0e272011-07-15 02:09:41 +00001178 if (getContext().getAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +00001179 FixupBit = i * 8 + j;
1180 else
1181 FixupBit = i * 8 + (7-j);
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001182
Jay Foad95c3e482011-06-23 09:09:15 +00001183 if (uint8_t MapEntry = FixupMap[FixupBit]) {
1184 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1185 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001186 } else
1187 OS << Bit;
1188 }
1189 }
1190 }
1191 OS << "]\n";
1192
1193 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1194 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001195 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001196 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +00001197 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +00001198 }
1199}
1200
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +00001201void MCAsmStreamer::EmitFnStart() {
1202 OS << "\t.fnstart";
1203 EmitEOL();
1204}
1205
1206void MCAsmStreamer::EmitFnEnd() {
1207 OS << "\t.fnend";
1208 EmitEOL();
1209}
1210
1211void MCAsmStreamer::EmitCantUnwind() {
1212 OS << "\t.cantunwind";
1213 EmitEOL();
1214}
1215
1216void MCAsmStreamer::EmitHandlerData() {
1217 OS << "\t.handlerdata";
1218 EmitEOL();
1219}
1220
1221void MCAsmStreamer::EmitPersonality(const MCSymbol *Personality) {
1222 OS << "\t.personality " << Personality->getName();
1223 EmitEOL();
1224}
1225
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001226void MCAsmStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001227 OS << "\t.setfp\t";
1228 InstPrinter->printRegName(OS, FpReg);
1229 OS << ", ";
1230 InstPrinter->printRegName(OS, SpReg);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001231 if (Offset)
1232 OS << ", #" << Offset;
1233 EmitEOL();
1234}
1235
1236void MCAsmStreamer::EmitPad(int64_t Offset) {
1237 OS << "\t.pad\t#" << Offset;
1238 EmitEOL();
1239}
1240
1241void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
1242 bool isVector) {
1243 assert(RegList.size() && "RegList should not be empty");
1244 if (isVector)
1245 OS << "\t.vsave\t{";
1246 else
1247 OS << "\t.save\t{";
1248
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001249 InstPrinter->printRegName(OS, RegList[0]);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001250
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001251 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
1252 OS << ", ";
1253 InstPrinter->printRegName(OS, RegList[i]);
1254 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001255
1256 OS << "}";
1257 EmitEOL();
1258}
1259
Daniel Dunbar6b716532010-02-09 23:00:14 +00001260void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +00001261 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Daniel Dunbar6b716532010-02-09 23:00:14 +00001262
1263 // Show the encoding in a comment if we have a code emitter.
1264 if (Emitter)
1265 AddEncodingComment(Inst);
1266
Chris Lattner30d9a642010-02-09 00:54:51 +00001267 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001268 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001269 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001270 GetCommentOS() << "\n";
1271 }
1272
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001273 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001274 if (InstPrinter)
Owen Anderson98c5dda2011-09-15 23:38:46 +00001275 InstPrinter->printInst(&Inst, OS, "");
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001276 else
1277 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +00001278 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +00001279}
1280
Jim Grosbachbd4ec842010-09-22 18:18:30 +00001281/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +00001282/// the specified string in the output .s file. This capability is
1283/// indicated by the hasRawTextSupport() predicate.
1284void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001285 if (!String.empty() && String.back() == '\n')
1286 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +00001287 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001288 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +00001289}
1290
Rafael Espindola99b42372012-01-07 03:13:18 +00001291void MCAsmStreamer::FinishImpl() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +00001292 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +00001293 if (getContext().hasDwarfFiles() && !UseLoc)
1294 MCDwarfFileTable::Emit(this);
Rafael Espindola5426a9e2011-05-01 04:49:54 +00001295
Kevin Enderby94c2e852011-12-09 18:09:40 +00001296 // If we are generating dwarf for assembly source files dump out the sections.
1297 if (getContext().getGenDwarfForAssembly())
1298 MCGenDwarfInfo::Emit(this);
1299
Rafael Espindolac25dad82011-05-10 03:14:15 +00001300 if (!UseCFI)
1301 EmitFrames(false);
Daniel Dunbara11af532009-06-24 01:03:06 +00001302}
Chris Lattner86e22112010-01-22 07:29:22 +00001303MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1304 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +00001305 bool isVerboseAsm, bool useLoc,
Nick Lewycky44d798d2011-10-17 23:05:28 +00001306 bool useCFI, bool useDwarfDirectory,
1307 MCInstPrinter *IP, MCCodeEmitter *CE,
1308 MCAsmBackend *MAB, bool ShowInst) {
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +00001309 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, useCFI,
Nick Lewycky44d798d2011-10-17 23:05:28 +00001310 useDwarfDirectory, IP, CE, MAB, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +00001311}