blob: e5f8ac96d78c97a9b6eb3ead97a1f20729750971 [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 Cheng0e6a0522011-07-18 20:57:22 +000018#include "llvm/MC/MCRegisterInfo.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000019#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000020#include "llvm/MC/MCSymbol.h"
Chris Lattner4c42a6d2010-03-19 05:48:53 +000021#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000022#include "llvm/ADT/SmallString.h"
Bill Wendling916a94b2011-06-17 20:35:21 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner86e22112010-01-22 07:29:22 +000024#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000025#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000026#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000027#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000028#include "llvm/Support/FormattedStream.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000029#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000030#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000032#include <cctype>
Daniel Dunbara11af532009-06-24 01:03:06 +000033using namespace llvm;
34
35namespace {
36
Chris Lattner46a947d2009-08-17 04:17:34 +000037class MCAsmStreamer : public MCStreamer {
Bill Wendling916a94b2011-06-17 20:35:21 +000038protected:
Chris Lattner86e22112010-01-22 07:29:22 +000039 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000040 const MCAsmInfo &MAI;
Bill Wendling916a94b2011-06-17 20:35:21 +000041private:
Chris Lattner4c42a6d2010-03-19 05:48:53 +000042 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000043 OwningPtr<MCCodeEmitter> Emitter;
Daniel Dunbar745dacc2010-12-16 03:05:59 +000044 OwningPtr<TargetAsmBackend> AsmBackend;
Jim Grosbach00545e12010-09-22 18:16:55 +000045
Chris Lattner86e22112010-01-22 07:29:22 +000046 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000047 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000048
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000049 unsigned IsVerboseAsm : 1;
50 unsigned ShowInst : 1;
Rafael Espindola89b93722010-12-10 07:39:47 +000051 unsigned UseLoc : 1;
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000052 unsigned UseCFI : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000053
Rafael Espindola277abc82011-04-30 16:34:57 +000054 enum EHSymbolFlags { EHGlobal = 1,
55 EHWeakDefinition = 1 << 1,
56 EHPrivateExtern = 1 << 2 };
57 DenseMap<const MCSymbol*, unsigned> FlagMap;
58
Rafael Espindola5d4918d2010-12-04 03:21:47 +000059 bool needsSet(const MCExpr *Value);
60
Rafael Espindola6e032942011-05-30 20:20:15 +000061 void EmitRegisterName(int64_t Register);
62
Chris Lattner46a947d2009-08-17 04:17:34 +000063public:
Chris Lattner86e22112010-01-22 07:29:22 +000064 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000065 bool isVerboseAsm, bool useLoc, bool useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000066 MCInstPrinter *printer, MCCodeEmitter *emitter,
67 TargetAsmBackend *asmbackend,
68 bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000069 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
Daniel Dunbar745dacc2010-12-16 03:05:59 +000070 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
71 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000072 ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000073 if (InstPrinter && IsVerboseAsm)
74 InstPrinter->setCommentStream(CommentStream);
75 }
Chris Lattner46a947d2009-08-17 04:17:34 +000076 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000077
Chris Lattner86e22112010-01-22 07:29:22 +000078 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000079 // If we don't have any comments, just emit a \n.
80 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000081 OS << '\n';
82 return;
83 }
84 EmitCommentsAndEOL();
85 }
86 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000087
88 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
89 /// all.
90 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000091
Chris Lattner91bead72010-04-03 21:35:55 +000092 /// hasRawTextSupport - We support EmitRawText.
93 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000094
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000095 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000096 /// file if applicable as a QoI issue to make the output of the compiler
97 /// more readable. This only affects the MCAsmStreamer, and only when
98 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000099 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000100
101 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
102 virtual void AddEncodingComment(const MCInst &Inst);
103
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000104 /// GetCommentOS - Return a raw_ostream that comments can be written to.
105 /// Unlike AddComment, you are required to terminate comments with \n if you
106 /// use this method.
107 virtual raw_ostream &GetCommentOS() {
108 if (!IsVerboseAsm)
109 return nulls(); // Discard comments unless in verbose asm mode.
110 return CommentStream;
111 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000112
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000113 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
114 virtual void AddBlankLine() {
115 EmitEOL();
116 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000117
Chris Lattner46a947d2009-08-17 04:17:34 +0000118 /// @name MCStreamer Interface
119 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000120
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000121 virtual void ChangeSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000122
Rafael Espindolad80781b2010-09-15 21:48:40 +0000123 virtual void InitSections() {
124 // FIXME, this is MachO specific, but the testsuite
125 // expects this.
126 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
127 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
128 0, SectionKind::getText()));
129 }
130
Chris Lattner46a947d2009-08-17 04:17:34 +0000131 virtual void EmitLabel(MCSymbol *Symbol);
Rafael Espindola277abc82011-04-30 16:34:57 +0000132 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
133 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000134 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000135 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000136
Daniel Dunbar821e3332009-08-31 08:09:28 +0000137 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000138 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000139 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
140 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000141 const MCSymbol *Label,
142 unsigned PointerSize);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000143 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
144 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000145
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000146 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000147
Chris Lattner46a947d2009-08-17 04:17:34 +0000148 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000149 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
150 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
151 virtual void EmitCOFFSymbolType(int Type);
152 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000153 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000154 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000155 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000156
Chris Lattner9eb158d2010-01-23 07:47:02 +0000157 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
158 ///
159 /// @param Symbol - The common symbol to emit.
160 /// @param Size - The size of the common symbol.
161 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000162
Daniel Dunbar8751b942009-08-28 05:48:22 +0000163 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000164 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000165
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000166 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
167 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000168
Chris Lattneraaec2052010-01-19 19:46:13 +0000169 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000170
Rafael Espindola89b93722010-12-10 07:39:47 +0000171 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000172 unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000173 virtual void EmitIntValue(uint64_t Value, unsigned Size,
174 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000175
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000176 virtual void EmitULEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000177
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000178 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000179
Chris Lattner718fb592010-01-25 21:28:50 +0000180 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000181
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000182
Chris Lattneraaec2052010-01-19 19:46:13 +0000183 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
184 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000185
Chris Lattner46a947d2009-08-17 04:17:34 +0000186 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
187 unsigned ValueSize = 1,
188 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000189
Kevin Enderby6e720482010-02-23 18:26:34 +0000190 virtual void EmitCodeAlignment(unsigned ByteAlignment,
191 unsigned MaxBytesToEmit = 0);
192
Daniel Dunbar821e3332009-08-31 08:09:28 +0000193 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000194 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000195
Chris Lattnera6594fc2010-01-25 18:58:59 +0000196 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000197 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
198 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
199 unsigned Column, unsigned Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000200 unsigned Isa, unsigned Discriminator,
201 StringRef FileName);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000202
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000203 virtual void EmitCFISections(bool EH, bool Debug);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000204 virtual void EmitCFIStartProc();
205 virtual void EmitCFIEndProc();
206 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
207 virtual void EmitCFIDefCfaOffset(int64_t Offset);
208 virtual void EmitCFIDefCfaRegister(int64_t Register);
209 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
210 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
211 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
212 virtual void EmitCFIRememberState();
213 virtual void EmitCFIRestoreState();
214 virtual void EmitCFISameValue(int64_t Register);
215 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
216 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000217
Charles Davisfbc539f2011-05-22 21:12:15 +0000218 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
Charles Davis0e30f022011-05-18 04:58:05 +0000219 virtual void EmitWin64EHEndProc();
Charles Davisf0709012011-05-18 20:54:10 +0000220 virtual void EmitWin64EHStartChained();
221 virtual void EmitWin64EHEndChained();
Charles Davis440596f2011-05-19 17:46:39 +0000222 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
223 bool Except);
224 virtual void EmitWin64EHHandlerData();
Charles Davisc3b16282011-05-19 19:35:55 +0000225 virtual void EmitWin64EHPushReg(unsigned Register);
226 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
227 virtual void EmitWin64EHAllocStack(unsigned Size);
228 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
229 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
Charles Davis0e30f022011-05-18 04:58:05 +0000230 virtual void EmitWin64EHPushFrame(bool Code);
231 virtual void EmitWin64EHEndProlog();
232
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000233 virtual void EmitFnStart();
234 virtual void EmitFnEnd();
235 virtual void EmitCantUnwind();
236 virtual void EmitPersonality(const MCSymbol *Personality);
237 virtual void EmitHandlerData();
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000238 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
239 virtual void EmitPad(int64_t Offset);
240 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool);
241
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000242
Chris Lattnera6594fc2010-01-25 18:58:59 +0000243 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000244
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000245 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000246 /// the specified string in the output .s file. This capability is
247 /// indicated by the hasRawTextSupport() predicate.
248 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000249
Chris Lattner46a947d2009-08-17 04:17:34 +0000250 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000251
Chris Lattner46a947d2009-08-17 04:17:34 +0000252 /// @}
253};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000254
Chris Lattner46a947d2009-08-17 04:17:34 +0000255} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000256
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000257/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000258/// file if applicable as a QoI issue to make the output of the compiler
259/// more readable. This only affects the MCAsmStreamer, and only when
260/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000261void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000262 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000263
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000264 // Make sure that CommentStream is flushed.
265 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000266
Chris Lattner86e22112010-01-22 07:29:22 +0000267 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000268 // Each comment goes on its own line.
269 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000270
Chris Lattner14ca1772010-01-22 21:16:10 +0000271 // Tell the comment stream that the vector changed underneath it.
272 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000273}
274
275void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000276 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
277 OS << '\n';
278 return;
279 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000280
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000281 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000282 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000283
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000284 assert(Comments.back() == '\n' &&
285 "Comment array not newline terminated");
286 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000287 // Emit a line of comments.
288 OS.PadToColumn(MAI.getCommentColumn());
289 size_t Position = Comments.find('\n');
290 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000291
Chris Lattner86e22112010-01-22 07:29:22 +0000292 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000293 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000294
Chris Lattner14ca1772010-01-22 21:16:10 +0000295 CommentToEmit.clear();
296 // Tell the comment stream that the vector changed underneath it.
297 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000298}
299
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000300static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
301 assert(Bytes && "Invalid size!");
302 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
303}
304
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000305void MCAsmStreamer::ChangeSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000306 assert(Section && "Cannot switch to a null section!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000307 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000308}
309
Rafael Espindola277abc82011-04-30 16:34:57 +0000310void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
311 MCSymbol *EHSymbol) {
312 if (UseCFI)
313 return;
314
315 unsigned Flags = FlagMap.lookup(Symbol);
316
317 if (Flags & EHGlobal)
318 EmitSymbolAttribute(EHSymbol, MCSA_Global);
319 if (Flags & EHWeakDefinition)
320 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
321 if (Flags & EHPrivateExtern)
322 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
323}
324
Daniel Dunbara11af532009-06-24 01:03:06 +0000325void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000326 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaed708f92011-04-27 15:21:19 +0000327 MCStreamer::EmitLabel(Symbol);
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000328
Chris Lattnere07b75e2010-09-22 22:19:53 +0000329 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000330 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000331}
332
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000333void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000334 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000335 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000336 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000337 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000338 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000339 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000340 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000341 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000342}
343
Jim Grosbachce792992010-11-05 22:08:08 +0000344void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
345 // This needs to emit to a temporary string to get properly quoted
346 // MCSymbols when they have spaces in them.
347 OS << "\t.thumb_func";
Rafael Espindola64695402011-05-16 16:17:21 +0000348 // Only Mach-O hasSubsectionsViaSymbols()
349 if (MAI.hasSubsectionsViaSymbols())
Jim Grosbachce792992010-11-05 22:08:08 +0000350 OS << '\t' << *Func;
351 EmitEOL();
352}
353
Daniel Dunbar821e3332009-08-31 08:09:28 +0000354void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000355 OS << *Symbol << " = " << *Value;
356 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000357
358 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000359 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000360}
361
Rafael Espindola484291c2010-11-01 14:28:48 +0000362void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
363 OS << ".weakref " << *Alias << ", " << *Symbol;
364 EmitEOL();
365}
366
Rafael Espindola32a006e2010-12-03 00:55:40 +0000367void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
368 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000369 const MCSymbol *Label,
370 unsigned PointerSize) {
371 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000372}
373
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000374void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
375 const MCSymbol *Label) {
376 EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
377 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
Rafael Espindolaa6f26782011-05-19 21:40:34 +0000378 AddrDelta = ForceExpAbs(AddrDelta);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000379 EmitValue(AddrDelta, 4);
380}
381
382
Daniel Dunbarfffff912009-10-16 01:34:54 +0000383void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000384 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000385 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000386 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000387 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
388 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
389 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
390 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
391 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
392 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000393 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000394 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000395 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000396 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
397 switch (Attribute) {
398 default: assert(0 && "Unknown ELF .type");
399 case MCSA_ELF_TypeFunction: OS << "function"; break;
400 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
401 case MCSA_ELF_TypeObject: OS << "object"; break;
402 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
403 case MCSA_ELF_TypeCommon: OS << "common"; break;
404 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000405 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000406 }
407 EmitEOL();
408 return;
409 case MCSA_Global: // .globl/.global
410 OS << MAI.getGlobalDirective();
Rafael Espindola277abc82011-04-30 16:34:57 +0000411 FlagMap[Symbol] |= EHGlobal;
Chris Lattnered0ab152010-01-25 18:30:45 +0000412 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000413 case MCSA_Hidden: OS << "\t.hidden\t"; break;
414 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
415 case MCSA_Internal: OS << "\t.internal\t"; break;
416 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
417 case MCSA_Local: OS << "\t.local\t"; break;
418 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000419 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000420 case MCSA_PrivateExtern:
421 OS << "\t.private_extern\t";
422 FlagMap[Symbol] |= EHPrivateExtern;
423 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000424 case MCSA_Protected: OS << "\t.protected\t"; break;
425 case MCSA_Reference: OS << "\t.reference\t"; break;
426 case MCSA_Weak: OS << "\t.weak\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000427 case MCSA_WeakDefinition:
428 OS << "\t.weak_definition\t";
429 FlagMap[Symbol] |= EHWeakDefinition;
430 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000431 // .weak_reference
432 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000433 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000434 }
435
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000436 OS << *Symbol;
437 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000438}
439
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000440void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000441 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
442 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000443}
444
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000445void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
446 OS << "\t.def\t " << *Symbol << ';';
447 EmitEOL();
448}
449
450void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
451 OS << "\t.scl\t" << StorageClass << ';';
452 EmitEOL();
453}
454
455void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
456 OS << "\t.type\t" << Type << ';';
457 EmitEOL();
458}
459
460void MCAsmStreamer::EndCOFFSymbolDef() {
461 OS << "\t.endef";
462 EmitEOL();
463}
464
Chris Lattner99328ad2010-01-25 07:52:13 +0000465void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
466 assert(MAI.hasDotTypeDotSizeDirective());
467 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
468}
469
Chris Lattner9eb158d2010-01-23 07:47:02 +0000470void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000471 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000472 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000473 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000474 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000475 OS << ',' << ByteAlignment;
476 else
477 OS << ',' << Log2_32(ByteAlignment);
478 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000479 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000480}
481
Chris Lattner9eb158d2010-01-23 07:47:02 +0000482/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
483///
484/// @param Symbol - The common symbol to emit.
485/// @param Size - The size of the common symbol.
486void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
487 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
488 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
489 EmitEOL();
490}
491
Daniel Dunbar8751b942009-08-28 05:48:22 +0000492void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000493 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000494 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000495 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000496
Chris Lattner93b6db32009-08-08 23:39:42 +0000497 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000498 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000499 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000500
Chris Lattner9be3fee2009-07-10 22:20:30 +0000501 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000502 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000503 if (ByteAlignment != 0)
504 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000505 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000506 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000507}
508
Eric Christopherd04d98d2010-05-17 02:13:02 +0000509// .tbss sym, size, align
510// This depends that the symbol has already been mangled from the original,
511// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000512void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
513 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000514 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000515 // Instead of using the Section we'll just use the shortcut.
516 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000517 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000518
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000519 // Output align if we have it. We default to 1 so don't bother printing
520 // that.
521 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000522
Eric Christopher482eba02010-05-14 01:50:28 +0000523 EmitEOL();
524}
525
Chris Lattner12e555c2010-01-23 00:15:00 +0000526static inline char toOctal(int X) { return (X&7)+'0'; }
527
Chris Lattnera6594fc2010-01-25 18:58:59 +0000528static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
529 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000530
Chris Lattnera6594fc2010-01-25 18:58:59 +0000531 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
532 unsigned char C = Data[i];
533 if (C == '"' || C == '\\') {
534 OS << '\\' << (char)C;
535 continue;
536 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000537
Chris Lattnera6594fc2010-01-25 18:58:59 +0000538 if (isprint((unsigned char)C)) {
539 OS << (char)C;
540 continue;
541 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000542
Chris Lattnera6594fc2010-01-25 18:58:59 +0000543 switch (C) {
544 case '\b': OS << "\\b"; break;
545 case '\f': OS << "\\f"; break;
546 case '\n': OS << "\\n"; break;
547 case '\r': OS << "\\r"; break;
548 case '\t': OS << "\\t"; break;
549 default:
550 OS << '\\';
551 OS << toOctal(C >> 6);
552 OS << toOctal(C >> 3);
553 OS << toOctal(C >> 0);
554 break;
555 }
556 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000557
Chris Lattnera6594fc2010-01-25 18:58:59 +0000558 OS << '"';
559}
560
561
Chris Lattneraaec2052010-01-19 19:46:13 +0000562void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000563 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000564 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000565
Chris Lattner12e555c2010-01-23 00:15:00 +0000566 if (Data.size() == 1) {
567 OS << MAI.getData8bitsDirective(AddrSpace);
568 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000569 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000570 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000571 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000572
573 // If the data ends with 0 and the target supports .asciz, use it, otherwise
574 // use .ascii
575 if (MAI.getAscizDirective() && Data.back() == 0) {
576 OS << MAI.getAscizDirective();
577 Data = Data.substr(0, Data.size()-1);
578 } else {
579 OS << MAI.getAsciiDirective();
580 }
581
Chris Lattnera6594fc2010-01-25 18:58:59 +0000582 OS << ' ';
583 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000584 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000585}
586
Rafael Espindola2df042c2010-12-03 02:54:21 +0000587void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
588 unsigned AddrSpace) {
589 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
590}
591
Rafael Espindola89b93722010-12-10 07:39:47 +0000592void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000593 unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000594 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000595 const char *Directive = 0;
596 switch (Size) {
597 default: break;
598 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
599 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
600 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000601 case 8:
602 Directive = MAI.getData64bitsDirective(AddrSpace);
603 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
604 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000605 int64_t IntValue;
606 if (!Value->EvaluateAsAbsolute(IntValue))
607 report_fatal_error("Don't know how to emit this value.");
Evan Cheng1be0e272011-07-15 02:09:41 +0000608 if (getContext().getAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000609 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
610 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000611 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000612 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
613 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000614 }
615 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000616 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000617
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000618 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000619 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000620 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000621}
622
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000623void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000624 int64_t IntValue;
625 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000626 EmitULEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000627 return;
628 }
Rafael Espindola73873452010-11-04 18:17:08 +0000629 assert(MAI.hasLEB128() && "Cannot print a .uleb");
630 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000631 EmitEOL();
632}
633
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000634void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000635 int64_t IntValue;
636 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000637 EmitSLEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000638 return;
639 }
Rafael Espindola73873452010-11-04 18:17:08 +0000640 assert(MAI.hasLEB128() && "Cannot print a .sleb");
641 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000642 EmitEOL();
643}
644
Chris Lattner718fb592010-01-25 21:28:50 +0000645void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
646 assert(MAI.getGPRel32Directive() != 0);
647 OS << MAI.getGPRel32Directive() << *Value;
648 EmitEOL();
649}
650
651
Chris Lattner6113b3d2010-01-19 18:52:28 +0000652/// EmitFill - Emit NumBytes bytes worth of the value specified by
653/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000654void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
655 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000656 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000657
Chris Lattneraaec2052010-01-19 19:46:13 +0000658 if (AddrSpace == 0)
659 if (const char *ZeroDirective = MAI.getZeroDirective()) {
660 OS << ZeroDirective << NumBytes;
661 if (FillValue != 0)
662 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000663 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000664 return;
665 }
666
667 // Emit a byte at a time.
668 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000669}
670
Daniel Dunbar84a29262009-06-24 19:25:34 +0000671void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
672 unsigned ValueSize,
673 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000674 // Some assemblers don't support non-power of two alignments, so we always
675 // emit alignments as a power of two if possible.
676 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000677 switch (ValueSize) {
678 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000679 case 1: OS << MAI.getAlignDirective(); break;
680 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000681 case 2: OS << ".p2alignw "; break;
682 case 4: OS << ".p2alignl "; break;
683 case 8: llvm_unreachable("Unsupported alignment size!");
684 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000685
Chris Lattner33adcfb2009-08-22 21:43:10 +0000686 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000687 OS << ByteAlignment;
688 else
689 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000690
Chris Lattner663c2d22009-08-19 06:12:02 +0000691 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000692 OS << ", 0x";
693 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000694
Jim Grosbach00545e12010-09-22 18:16:55 +0000695 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000696 OS << ", " << MaxBytesToEmit;
697 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000698 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000699 return;
700 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000701
Chris Lattner663c2d22009-08-19 06:12:02 +0000702 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000703 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000704 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000705 default: llvm_unreachable("Invalid size for machine code value!");
706 case 1: OS << ".balign"; break;
707 case 2: OS << ".balignw"; break;
708 case 4: OS << ".balignl"; break;
709 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000710 }
711
Chris Lattner663c2d22009-08-19 06:12:02 +0000712 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000713 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000714 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000715 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000716 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000717}
718
Kevin Enderby6e720482010-02-23 18:26:34 +0000719void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
720 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000721 // Emit with a text fill value.
722 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
723 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000724}
725
Daniel Dunbar821e3332009-08-31 08:09:28 +0000726void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000727 unsigned char Value) {
728 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000729 OS << ".org " << *Offset << ", " << (unsigned) Value;
730 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000731}
732
Chris Lattnera6594fc2010-01-25 18:58:59 +0000733
734void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
735 assert(MAI.hasSingleParameterDotFile());
736 OS << "\t.file\t";
737 PrintQuotedString(Filename, OS);
738 EmitEOL();
739}
740
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000741bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola89b93722010-12-10 07:39:47 +0000742 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000743 OS << "\t.file\t" << FileNo << ' ';
744 PrintQuotedString(Filename, OS);
745 EmitEOL();
746 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000747 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
748}
749
750void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
751 unsigned Column, unsigned Flags,
752 unsigned Isa,
Devang Patel3f3bf932011-04-18 20:26:49 +0000753 unsigned Discriminator,
754 StringRef FileName) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000755 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000756 Isa, Discriminator, FileName);
Rafael Espindola89b93722010-12-10 07:39:47 +0000757 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000758 return;
759
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000760 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
761 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
762 OS << " basic_block";
763 if (Flags & DWARF2_FLAG_PROLOGUE_END)
764 OS << " prologue_end";
765 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
766 OS << " epilogue_begin";
767
768 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
769 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
770 OS << " is_stmt ";
771
772 if (Flags & DWARF2_FLAG_IS_STMT)
773 OS << "1";
774 else
775 OS << "0";
776 }
777
778 if (Isa)
779 OS << "isa " << Isa;
780 if (Discriminator)
781 OS << "discriminator " << Discriminator;
Devang Patel3f3bf932011-04-18 20:26:49 +0000782
783 if (IsVerboseAsm) {
784 OS.PadToColumn(MAI.getCommentColumn());
785 OS << MAI.getCommentString() << ' ' << FileName << ':'
786 << Line << ':' << Column;
787 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000788 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000789}
790
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000791void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
792 MCStreamer::EmitCFISections(EH, Debug);
793
794 if (!UseCFI)
795 return;
796
797 OS << "\t.cfi_sections ";
798 if (EH) {
799 OS << ".eh_frame";
800 if (Debug)
801 OS << ", .debug_frame";
802 } else if (Debug) {
803 OS << ".debug_frame";
804 }
805
806 EmitEOL();
807}
808
Rafael Espindola066c2f42011-04-12 23:59:07 +0000809void MCAsmStreamer::EmitCFIStartProc() {
810 MCStreamer::EmitCFIStartProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000811
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000812 if (!UseCFI)
813 return;
814
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000815 OS << "\t.cfi_startproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000816 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000817}
818
Rafael Espindola066c2f42011-04-12 23:59:07 +0000819void MCAsmStreamer::EmitCFIEndProc() {
820 MCStreamer::EmitCFIEndProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000821
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000822 if (!UseCFI)
823 return;
824
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000825 OS << "\t.cfi_endproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000826 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000827}
828
Rafael Espindola6e032942011-05-30 20:20:15 +0000829void MCAsmStreamer::EmitRegisterName(int64_t Register) {
Akira Hatanaka3014b2f2011-07-07 20:30:33 +0000830 if (InstPrinter && !MAI.useDwarfRegNumForCFI()) {
Evan Cheng0e6a0522011-07-18 20:57:22 +0000831 const MCRegisterInfo &MRI = getContext().getRegisterInfo();
832 unsigned LLVMRegister = MRI.getLLVMRegNum(Register, true);
Rafael Espindolacde4ce42011-06-02 02:34:55 +0000833 InstPrinter->printRegName(OS, LLVMRegister);
Rafael Espindola6e032942011-05-30 20:20:15 +0000834 } else {
835 OS << Register;
836 }
837}
838
Rafael Espindola066c2f42011-04-12 23:59:07 +0000839void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindola7f46b082011-04-29 21:41:06 +0000840 MCStreamer::EmitCFIDefCfa(Register, Offset);
841
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000842 if (!UseCFI)
843 return;
844
Rafael Espindola6e032942011-05-30 20:20:15 +0000845 OS << "\t.cfi_def_cfa ";
846 EmitRegisterName(Register);
847 OS << ", " << Offset;
Rafael Espindola7f46b082011-04-29 21:41:06 +0000848 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000849}
850
851void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
852 MCStreamer::EmitCFIDefCfaOffset(Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000853
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000854 if (!UseCFI)
855 return;
856
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000857 OS << "\t.cfi_def_cfa_offset " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000858 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000859}
860
Rafael Espindola066c2f42011-04-12 23:59:07 +0000861void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
862 MCStreamer::EmitCFIDefCfaRegister(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000863
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000864 if (!UseCFI)
865 return;
866
Rafael Espindola6e032942011-05-30 20:20:15 +0000867 OS << "\t.cfi_def_cfa_register ";
868 EmitRegisterName(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000869 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000870}
871
Rafael Espindola066c2f42011-04-12 23:59:07 +0000872void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
873 this->MCStreamer::EmitCFIOffset(Register, Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000874
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000875 if (!UseCFI)
876 return;
877
Rafael Espindola6e032942011-05-30 20:20:15 +0000878 OS << "\t.cfi_offset ";
879 EmitRegisterName(Register);
880 OS << ", " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000881 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000882}
883
Rafael Espindola066c2f42011-04-12 23:59:07 +0000884void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000885 unsigned Encoding) {
Rafael Espindola066c2f42011-04-12 23:59:07 +0000886 MCStreamer::EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000887
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000888 if (!UseCFI)
889 return;
890
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000891 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000892 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000893}
894
Rafael Espindola066c2f42011-04-12 23:59:07 +0000895void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
896 MCStreamer::EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000897
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000898 if (!UseCFI)
899 return;
900
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000901 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000902 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000903}
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000904
Rafael Espindola066c2f42011-04-12 23:59:07 +0000905void MCAsmStreamer::EmitCFIRememberState() {
906 MCStreamer::EmitCFIRememberState();
907
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000908 if (!UseCFI)
909 return;
910
Rafael Espindola066c2f42011-04-12 23:59:07 +0000911 OS << "\t.cfi_remember_state";
912 EmitEOL();
913}
914
915void MCAsmStreamer::EmitCFIRestoreState() {
916 MCStreamer::EmitCFIRestoreState();
917
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000918 if (!UseCFI)
919 return;
920
Rafael Espindola066c2f42011-04-12 23:59:07 +0000921 OS << "\t.cfi_restore_state";
922 EmitEOL();
923}
924
925void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
926 MCStreamer::EmitCFISameValue(Register);
927
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000928 if (!UseCFI)
929 return;
930
Rafael Espindola6e032942011-05-30 20:20:15 +0000931 OS << "\t.cfi_same_value ";
932 EmitRegisterName(Register);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000933 EmitEOL();
934}
935
936void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
937 MCStreamer::EmitCFIRelOffset(Register, Offset);
938
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000939 if (!UseCFI)
940 return;
941
Rafael Espindola6e032942011-05-30 20:20:15 +0000942 OS << "\t.cfi_rel_offset ";
943 EmitRegisterName(Register);
944 OS << ", " << Offset;
Rafael Espindola066c2f42011-04-12 23:59:07 +0000945 EmitEOL();
946}
947
948void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
949 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
950
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000951 if (!UseCFI)
952 return;
953
Rafael Espindola066c2f42011-04-12 23:59:07 +0000954 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
955 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000956}
957
Charles Davisfbc539f2011-05-22 21:12:15 +0000958void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
Charles Daviscde87e22011-05-20 18:19:22 +0000959 MCStreamer::EmitWin64EHStartProc(Symbol);
960
Charles Davis440596f2011-05-19 17:46:39 +0000961 OS << ".seh_proc " << *Symbol;
Charles Davis0e30f022011-05-18 04:58:05 +0000962 EmitEOL();
963}
964
Charles Davis8b3e5e52011-05-18 22:13:51 +0000965void MCAsmStreamer::EmitWin64EHEndProc() {
Charles Daviscde87e22011-05-20 18:19:22 +0000966 MCStreamer::EmitWin64EHEndProc();
967
Charles Davis440596f2011-05-19 17:46:39 +0000968 OS << "\t.seh_endproc";
Charles Davis0e30f022011-05-18 04:58:05 +0000969 EmitEOL();
970}
971
Charles Davis8b3e5e52011-05-18 22:13:51 +0000972void MCAsmStreamer::EmitWin64EHStartChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000973 MCStreamer::EmitWin64EHStartChained();
974
Charles Davis440596f2011-05-19 17:46:39 +0000975 OS << "\t.seh_startchained";
Charles Davisf0709012011-05-18 20:54:10 +0000976 EmitEOL();
977}
978
Charles Davis8b3e5e52011-05-18 22:13:51 +0000979void MCAsmStreamer::EmitWin64EHEndChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000980 MCStreamer::EmitWin64EHEndChained();
981
Charles Davis440596f2011-05-19 17:46:39 +0000982 OS << "\t.seh_endchained";
Charles Davisf0709012011-05-18 20:54:10 +0000983 EmitEOL();
984}
985
Charles Davis440596f2011-05-19 17:46:39 +0000986void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
987 bool Except) {
Charles Daviscde87e22011-05-20 18:19:22 +0000988 MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
989
Charles Davis440596f2011-05-19 17:46:39 +0000990 OS << "\t.seh_handler " << *Sym;
991 if (Unwind)
992 OS << ", @unwind";
993 if (Except)
994 OS << ", @except";
Charles Davisf0709012011-05-18 20:54:10 +0000995 EmitEOL();
996}
997
Charles Davis440596f2011-05-19 17:46:39 +0000998void MCAsmStreamer::EmitWin64EHHandlerData() {
Charles Daviscde87e22011-05-20 18:19:22 +0000999 MCStreamer::EmitWin64EHHandlerData();
1000
Charles Davis410ef2b2011-05-25 21:43:45 +00001001 // Switch sections. Don't call SwitchSection directly, because that will
1002 // cause the section switch to be visible in the emitted assembly.
1003 // We only do this so the section switch that terminates the handler
1004 // data block is visible.
Charles Davis7b06b732011-05-27 19:09:24 +00001005 MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
1006 StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
Charles Davis410ef2b2011-05-25 21:43:45 +00001007 const MCSection *xdataSect =
Charles Davis7b06b732011-05-27 19:09:24 +00001008 getContext().getTargetAsmInfo().getWin64EHTableSection(suffix);
Charles Davis410ef2b2011-05-25 21:43:45 +00001009 if (xdataSect)
1010 SwitchSectionNoChange(xdataSect);
1011
Charles Davis440596f2011-05-19 17:46:39 +00001012 OS << "\t.seh_handlerdata";
Charles Davisf0709012011-05-18 20:54:10 +00001013 EmitEOL();
1014}
1015
Charles Davisc3b16282011-05-19 19:35:55 +00001016void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
Charles Daviscde87e22011-05-20 18:19:22 +00001017 MCStreamer::EmitWin64EHPushReg(Register);
1018
Charles Davis440596f2011-05-19 17:46:39 +00001019 OS << "\t.seh_pushreg " << Register;
Charles Davis0e30f022011-05-18 04:58:05 +00001020 EmitEOL();
1021}
1022
Charles Davisc3b16282011-05-19 19:35:55 +00001023void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001024 MCStreamer::EmitWin64EHSetFrame(Register, Offset);
1025
Charles Davis440596f2011-05-19 17:46:39 +00001026 OS << "\t.seh_setframe " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001027 EmitEOL();
1028}
1029
Charles Davisc3b16282011-05-19 19:35:55 +00001030void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
Charles Daviscde87e22011-05-20 18:19:22 +00001031 MCStreamer::EmitWin64EHAllocStack(Size);
1032
Charles Davis440596f2011-05-19 17:46:39 +00001033 OS << "\t.seh_stackalloc " << Size;
Charles Davis0e30f022011-05-18 04:58:05 +00001034 EmitEOL();
1035}
1036
Charles Davisc3b16282011-05-19 19:35:55 +00001037void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001038 MCStreamer::EmitWin64EHSaveReg(Register, Offset);
1039
Charles Davis440596f2011-05-19 17:46:39 +00001040 OS << "\t.seh_savereg " << Register << ", " << Offset;
1041 EmitEOL();
1042}
1043
Charles Davisc3b16282011-05-19 19:35:55 +00001044void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001045 MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
1046
Charles Davis440596f2011-05-19 17:46:39 +00001047 OS << "\t.seh_savexmm " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001048 EmitEOL();
1049}
1050
Charles Davis8b3e5e52011-05-18 22:13:51 +00001051void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
Charles Daviscde87e22011-05-20 18:19:22 +00001052 MCStreamer::EmitWin64EHPushFrame(Code);
1053
Charles Davis440596f2011-05-19 17:46:39 +00001054 OS << "\t.seh_pushframe";
Charles Davis0e30f022011-05-18 04:58:05 +00001055 if (Code)
Charles Davis440596f2011-05-19 17:46:39 +00001056 OS << " @code";
Charles Davis0e30f022011-05-18 04:58:05 +00001057 EmitEOL();
1058}
1059
Charles Davis8b3e5e52011-05-18 22:13:51 +00001060void MCAsmStreamer::EmitWin64EHEndProlog(void) {
Charles Daviscde87e22011-05-20 18:19:22 +00001061 MCStreamer::EmitWin64EHEndProlog();
1062
Charles Davis440596f2011-05-19 17:46:39 +00001063 OS << "\t.seh_endprologue";
Charles Davis0e30f022011-05-18 04:58:05 +00001064 EmitEOL();
1065}
1066
Daniel Dunbar6b716532010-02-09 23:00:14 +00001067void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
1068 raw_ostream &OS = GetCommentOS();
1069 SmallString<256> Code;
1070 SmallVector<MCFixup, 4> Fixups;
1071 raw_svector_ostream VecOS(Code);
1072 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
1073 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +00001074
Daniel Dunbar6b716532010-02-09 23:00:14 +00001075 // If we are showing fixups, create symbolic markers in the encoded
1076 // representation. We do this by making a per-bit map to the fixup item index,
1077 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +00001078 SmallVector<uint8_t, 64> FixupMap;
1079 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001080 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1081 FixupMap[i] = 0;
1082
1083 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1084 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001085 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001086 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1087 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1088 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1089 FixupMap[Index] = 1 + i;
1090 }
1091 }
1092
Evan Cheng5c2eef62011-07-08 22:49:42 +00001093 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001094 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
Daniel Dunbar6b716532010-02-09 23:00:14 +00001095 OS << "encoding: [";
1096 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1097 if (i)
1098 OS << ',';
1099
1100 // See if all bits are the same map entry.
1101 uint8_t MapEntry = FixupMap[i * 8 + 0];
1102 for (unsigned j = 1; j != 8; ++j) {
1103 if (FixupMap[i * 8 + j] == MapEntry)
1104 continue;
1105
1106 MapEntry = uint8_t(~0U);
1107 break;
1108 }
1109
1110 if (MapEntry != uint8_t(~0U)) {
1111 if (MapEntry == 0) {
1112 OS << format("0x%02x", uint8_t(Code[i]));
1113 } else {
Evan Cheng82caf1a2011-01-13 21:45:26 +00001114 if (Code[i]) {
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001115 // FIXME: Some of the 8 bits require fix up.
Evan Cheng82caf1a2011-01-13 21:45:26 +00001116 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1117 << char('A' + MapEntry - 1) << '\'';
1118 } else
1119 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001120 }
1121 } else {
1122 // Otherwise, write out in binary.
1123 OS << "0b";
Jay Foad95c3e482011-06-23 09:09:15 +00001124 for (unsigned j = 8; j--;) {
Daniel Dunbar6b716532010-02-09 23:00:14 +00001125 unsigned Bit = (Code[i] >> j) & 1;
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001126
Chris Lattner3170a3b2010-11-15 05:56:19 +00001127 unsigned FixupBit;
Evan Cheng1be0e272011-07-15 02:09:41 +00001128 if (getContext().getAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +00001129 FixupBit = i * 8 + j;
1130 else
1131 FixupBit = i * 8 + (7-j);
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001132
Jay Foad95c3e482011-06-23 09:09:15 +00001133 if (uint8_t MapEntry = FixupMap[FixupBit]) {
1134 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1135 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001136 } else
1137 OS << Bit;
1138 }
1139 }
1140 }
1141 OS << "]\n";
1142
1143 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1144 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001145 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001146 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +00001147 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +00001148 }
1149}
1150
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +00001151void MCAsmStreamer::EmitFnStart() {
1152 OS << "\t.fnstart";
1153 EmitEOL();
1154}
1155
1156void MCAsmStreamer::EmitFnEnd() {
1157 OS << "\t.fnend";
1158 EmitEOL();
1159}
1160
1161void MCAsmStreamer::EmitCantUnwind() {
1162 OS << "\t.cantunwind";
1163 EmitEOL();
1164}
1165
1166void MCAsmStreamer::EmitHandlerData() {
1167 OS << "\t.handlerdata";
1168 EmitEOL();
1169}
1170
1171void MCAsmStreamer::EmitPersonality(const MCSymbol *Personality) {
1172 OS << "\t.personality " << Personality->getName();
1173 EmitEOL();
1174}
1175
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001176void MCAsmStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001177 OS << "\t.setfp\t";
1178 InstPrinter->printRegName(OS, FpReg);
1179 OS << ", ";
1180 InstPrinter->printRegName(OS, SpReg);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001181 if (Offset)
1182 OS << ", #" << Offset;
1183 EmitEOL();
1184}
1185
1186void MCAsmStreamer::EmitPad(int64_t Offset) {
1187 OS << "\t.pad\t#" << Offset;
1188 EmitEOL();
1189}
1190
1191void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
1192 bool isVector) {
1193 assert(RegList.size() && "RegList should not be empty");
1194 if (isVector)
1195 OS << "\t.vsave\t{";
1196 else
1197 OS << "\t.save\t{";
1198
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001199 InstPrinter->printRegName(OS, RegList[0]);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001200
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001201 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
1202 OS << ", ";
1203 InstPrinter->printRegName(OS, RegList[i]);
1204 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001205
1206 OS << "}";
1207 EmitEOL();
1208}
1209
Daniel Dunbar6b716532010-02-09 23:00:14 +00001210void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +00001211 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Daniel Dunbar6b716532010-02-09 23:00:14 +00001212
1213 // Show the encoding in a comment if we have a code emitter.
1214 if (Emitter)
1215 AddEncodingComment(Inst);
1216
Chris Lattner30d9a642010-02-09 00:54:51 +00001217 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001218 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001219 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001220 GetCommentOS() << "\n";
1221 }
1222
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001223 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001224 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +00001225 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001226 else
1227 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +00001228 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +00001229}
1230
Jim Grosbachbd4ec842010-09-22 18:18:30 +00001231/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +00001232/// the specified string in the output .s file. This capability is
1233/// indicated by the hasRawTextSupport() predicate.
1234void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001235 if (!String.empty() && String.back() == '\n')
1236 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +00001237 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001238 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +00001239}
1240
Daniel Dunbara11af532009-06-24 01:03:06 +00001241void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +00001242 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +00001243 if (getContext().hasDwarfFiles() && !UseLoc)
1244 MCDwarfFileTable::Emit(this);
Rafael Espindola5426a9e2011-05-01 04:49:54 +00001245
Rafael Espindolac25dad82011-05-10 03:14:15 +00001246 if (!UseCFI)
1247 EmitFrames(false);
Daniel Dunbara11af532009-06-24 01:03:06 +00001248}
Chris Lattner86e22112010-01-22 07:29:22 +00001249MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1250 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +00001251 bool isVerboseAsm, bool useLoc,
Bill Wendling916a94b2011-06-17 20:35:21 +00001252 bool useCFI, MCInstPrinter *IP,
1253 MCCodeEmitter *CE, TargetAsmBackend *TAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +00001254 bool ShowInst) {
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +00001255 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +00001256 IP, CE, TAB, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +00001257}