blob: 802f19c93ec6d142696b58e60f5279c5da608d75 [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"
Evan Chenge76a33b2011-07-20 05:58:47 +000019#include "llvm/MC/MCSectionCOFF.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000021#include "llvm/MC/MCSymbol.h"
Chris Lattner4c42a6d2010-03-19 05:48:53 +000022#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000023#include "llvm/ADT/SmallString.h"
Bill Wendling916a94b2011-06-17 20:35:21 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner86e22112010-01-22 07:29:22 +000025#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000028#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000029#include "llvm/Support/FormattedStream.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000030#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000031#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000032#include "llvm/Target/TargetLoweringObjectFile.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;
Daniel Dunbar745dacc2010-12-16 03:05:59 +000045 OwningPtr<TargetAsmBackend> 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;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000054
Rafael Espindola277abc82011-04-30 16:34:57 +000055 enum EHSymbolFlags { EHGlobal = 1,
56 EHWeakDefinition = 1 << 1,
57 EHPrivateExtern = 1 << 2 };
58 DenseMap<const MCSymbol*, unsigned> FlagMap;
59
Rafael Espindola5d4918d2010-12-04 03:21:47 +000060 bool needsSet(const MCExpr *Value);
61
Rafael Espindola6e032942011-05-30 20:20:15 +000062 void EmitRegisterName(int64_t Register);
63
Chris Lattner46a947d2009-08-17 04:17:34 +000064public:
Chris Lattner86e22112010-01-22 07:29:22 +000065 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000066 bool isVerboseAsm, bool useLoc, bool useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000067 MCInstPrinter *printer, MCCodeEmitter *emitter,
68 TargetAsmBackend *asmbackend,
69 bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000070 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
Daniel Dunbar745dacc2010-12-16 03:05:59 +000071 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
72 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000073 ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000074 if (InstPrinter && IsVerboseAsm)
75 InstPrinter->setCommentStream(CommentStream);
76 }
Chris Lattner46a947d2009-08-17 04:17:34 +000077 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000078
Chris Lattner86e22112010-01-22 07:29:22 +000079 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000080 // If we don't have any comments, just emit a \n.
81 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000082 OS << '\n';
83 return;
84 }
85 EmitCommentsAndEOL();
86 }
87 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000088
89 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
90 /// all.
91 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000092
Chris Lattner91bead72010-04-03 21:35:55 +000093 /// hasRawTextSupport - We support EmitRawText.
94 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000095
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000096 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000097 /// file if applicable as a QoI issue to make the output of the compiler
98 /// more readable. This only affects the MCAsmStreamer, and only when
99 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000100 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +0000101
102 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
103 virtual void AddEncodingComment(const MCInst &Inst);
104
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000105 /// GetCommentOS - Return a raw_ostream that comments can be written to.
106 /// Unlike AddComment, you are required to terminate comments with \n if you
107 /// use this method.
108 virtual raw_ostream &GetCommentOS() {
109 if (!IsVerboseAsm)
110 return nulls(); // Discard comments unless in verbose asm mode.
111 return CommentStream;
112 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000113
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000114 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
115 virtual void AddBlankLine() {
116 EmitEOL();
117 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000118
Chris Lattner46a947d2009-08-17 04:17:34 +0000119 /// @name MCStreamer Interface
120 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000121
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000122 virtual void ChangeSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000123
Rafael Espindolad80781b2010-09-15 21:48:40 +0000124 virtual void InitSections() {
125 // FIXME, this is MachO specific, but the testsuite
126 // expects this.
127 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
128 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
129 0, SectionKind::getText()));
130 }
131
Chris Lattner46a947d2009-08-17 04:17:34 +0000132 virtual void EmitLabel(MCSymbol *Symbol);
Rafael Espindola277abc82011-04-30 16:34:57 +0000133 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
134 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000135 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000136 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000137
Daniel Dunbar821e3332009-08-31 08:09:28 +0000138 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000139 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000140 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
141 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000142 const MCSymbol *Label,
143 unsigned PointerSize);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000144 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
145 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000146
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000147 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000148
Chris Lattner46a947d2009-08-17 04:17:34 +0000149 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000150 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
151 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
152 virtual void EmitCOFFSymbolType(int Type);
153 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000154 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000155 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000156 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000157
Chris Lattner9eb158d2010-01-23 07:47:02 +0000158 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
159 ///
160 /// @param Symbol - The common symbol to emit.
161 /// @param Size - The size of the common symbol.
162 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000163
Daniel Dunbar8751b942009-08-28 05:48:22 +0000164 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000165 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000166
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000167 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
168 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000169
Chris Lattneraaec2052010-01-19 19:46:13 +0000170 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000171
Rafael Espindola89b93722010-12-10 07:39:47 +0000172 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000173 unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000174 virtual void EmitIntValue(uint64_t Value, unsigned Size,
175 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000176
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000177 virtual void EmitULEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000178
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000179 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000180
Chris Lattner718fb592010-01-25 21:28:50 +0000181 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000182
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000183
Chris Lattneraaec2052010-01-19 19:46:13 +0000184 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
185 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000186
Chris Lattner46a947d2009-08-17 04:17:34 +0000187 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
188 unsigned ValueSize = 1,
189 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000190
Kevin Enderby6e720482010-02-23 18:26:34 +0000191 virtual void EmitCodeAlignment(unsigned ByteAlignment,
192 unsigned MaxBytesToEmit = 0);
193
Daniel Dunbar821e3332009-08-31 08:09:28 +0000194 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000195 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000196
Chris Lattnera6594fc2010-01-25 18:58:59 +0000197 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000198 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
199 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
200 unsigned Column, unsigned Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000201 unsigned Isa, unsigned Discriminator,
202 StringRef FileName);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000203
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000204 virtual void EmitCFISections(bool EH, bool Debug);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000205 virtual void EmitCFIStartProc();
206 virtual void EmitCFIEndProc();
207 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
208 virtual void EmitCFIDefCfaOffset(int64_t Offset);
209 virtual void EmitCFIDefCfaRegister(int64_t Register);
210 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
211 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
212 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
213 virtual void EmitCFIRememberState();
214 virtual void EmitCFIRestoreState();
215 virtual void EmitCFISameValue(int64_t Register);
216 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
217 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000218
Charles Davisfbc539f2011-05-22 21:12:15 +0000219 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
Charles Davis0e30f022011-05-18 04:58:05 +0000220 virtual void EmitWin64EHEndProc();
Charles Davisf0709012011-05-18 20:54:10 +0000221 virtual void EmitWin64EHStartChained();
222 virtual void EmitWin64EHEndChained();
Charles Davis440596f2011-05-19 17:46:39 +0000223 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
224 bool Except);
225 virtual void EmitWin64EHHandlerData();
Charles Davisc3b16282011-05-19 19:35:55 +0000226 virtual void EmitWin64EHPushReg(unsigned Register);
227 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
228 virtual void EmitWin64EHAllocStack(unsigned Size);
229 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
230 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
Charles Davis0e30f022011-05-18 04:58:05 +0000231 virtual void EmitWin64EHPushFrame(bool Code);
232 virtual void EmitWin64EHEndProlog();
233
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000234 virtual void EmitFnStart();
235 virtual void EmitFnEnd();
236 virtual void EmitCantUnwind();
237 virtual void EmitPersonality(const MCSymbol *Personality);
238 virtual void EmitHandlerData();
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000239 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
240 virtual void EmitPad(int64_t Offset);
241 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool);
242
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000243
Chris Lattnera6594fc2010-01-25 18:58:59 +0000244 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000245
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000246 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000247 /// the specified string in the output .s file. This capability is
248 /// indicated by the hasRawTextSupport() predicate.
249 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000250
Chris Lattner46a947d2009-08-17 04:17:34 +0000251 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000252
Chris Lattner46a947d2009-08-17 04:17:34 +0000253 /// @}
254};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000255
Chris Lattner46a947d2009-08-17 04:17:34 +0000256} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000257
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000258/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000259/// file if applicable as a QoI issue to make the output of the compiler
260/// more readable. This only affects the MCAsmStreamer, and only when
261/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000262void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000263 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000264
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000265 // Make sure that CommentStream is flushed.
266 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000267
Chris Lattner86e22112010-01-22 07:29:22 +0000268 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000269 // Each comment goes on its own line.
270 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000271
Chris Lattner14ca1772010-01-22 21:16:10 +0000272 // Tell the comment stream that the vector changed underneath it.
273 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000274}
275
276void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000277 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
278 OS << '\n';
279 return;
280 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000281
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000282 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000283 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000284
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000285 assert(Comments.back() == '\n' &&
286 "Comment array not newline terminated");
287 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000288 // Emit a line of comments.
289 OS.PadToColumn(MAI.getCommentColumn());
290 size_t Position = Comments.find('\n');
291 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000292
Chris Lattner86e22112010-01-22 07:29:22 +0000293 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000294 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000295
Chris Lattner14ca1772010-01-22 21:16:10 +0000296 CommentToEmit.clear();
297 // Tell the comment stream that the vector changed underneath it.
298 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000299}
300
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000301static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
302 assert(Bytes && "Invalid size!");
303 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
304}
305
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000306void MCAsmStreamer::ChangeSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000307 assert(Section && "Cannot switch to a null section!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000308 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000309}
310
Rafael Espindola277abc82011-04-30 16:34:57 +0000311void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
312 MCSymbol *EHSymbol) {
313 if (UseCFI)
314 return;
315
316 unsigned Flags = FlagMap.lookup(Symbol);
317
318 if (Flags & EHGlobal)
319 EmitSymbolAttribute(EHSymbol, MCSA_Global);
320 if (Flags & EHWeakDefinition)
321 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
322 if (Flags & EHPrivateExtern)
323 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
324}
325
Daniel Dunbara11af532009-06-24 01:03:06 +0000326void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000327 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaed708f92011-04-27 15:21:19 +0000328 MCStreamer::EmitLabel(Symbol);
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000329
Chris Lattnere07b75e2010-09-22 22:19:53 +0000330 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000331 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000332}
333
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000334void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000335 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000336 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000337 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000338 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000339 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000340 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000341 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000342 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000343}
344
Jim Grosbachce792992010-11-05 22:08:08 +0000345void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
346 // This needs to emit to a temporary string to get properly quoted
347 // MCSymbols when they have spaces in them.
348 OS << "\t.thumb_func";
Rafael Espindola64695402011-05-16 16:17:21 +0000349 // Only Mach-O hasSubsectionsViaSymbols()
350 if (MAI.hasSubsectionsViaSymbols())
Jim Grosbachce792992010-11-05 22:08:08 +0000351 OS << '\t' << *Func;
352 EmitEOL();
353}
354
Daniel Dunbar821e3332009-08-31 08:09:28 +0000355void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000356 OS << *Symbol << " = " << *Value;
357 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000358
359 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000360 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000361}
362
Rafael Espindola484291c2010-11-01 14:28:48 +0000363void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
364 OS << ".weakref " << *Alias << ", " << *Symbol;
365 EmitEOL();
366}
367
Rafael Espindola32a006e2010-12-03 00:55:40 +0000368void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
369 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000370 const MCSymbol *Label,
371 unsigned PointerSize) {
372 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000373}
374
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000375void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
376 const MCSymbol *Label) {
377 EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
378 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
Rafael Espindolaa6f26782011-05-19 21:40:34 +0000379 AddrDelta = ForceExpAbs(AddrDelta);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000380 EmitValue(AddrDelta, 4);
381}
382
383
Daniel Dunbarfffff912009-10-16 01:34:54 +0000384void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000385 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000386 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000387 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000388 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
389 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
390 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
391 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
392 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
393 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000394 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000395 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000396 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000397 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
398 switch (Attribute) {
399 default: assert(0 && "Unknown ELF .type");
400 case MCSA_ELF_TypeFunction: OS << "function"; break;
401 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
402 case MCSA_ELF_TypeObject: OS << "object"; break;
403 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
404 case MCSA_ELF_TypeCommon: OS << "common"; break;
405 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000406 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000407 }
408 EmitEOL();
409 return;
410 case MCSA_Global: // .globl/.global
411 OS << MAI.getGlobalDirective();
Rafael Espindola277abc82011-04-30 16:34:57 +0000412 FlagMap[Symbol] |= EHGlobal;
Chris Lattnered0ab152010-01-25 18:30:45 +0000413 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000414 case MCSA_Hidden: OS << "\t.hidden\t"; break;
415 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
416 case MCSA_Internal: OS << "\t.internal\t"; break;
417 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
418 case MCSA_Local: OS << "\t.local\t"; break;
419 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000420 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000421 case MCSA_PrivateExtern:
422 OS << "\t.private_extern\t";
423 FlagMap[Symbol] |= EHPrivateExtern;
424 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000425 case MCSA_Protected: OS << "\t.protected\t"; break;
426 case MCSA_Reference: OS << "\t.reference\t"; break;
427 case MCSA_Weak: OS << "\t.weak\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000428 case MCSA_WeakDefinition:
429 OS << "\t.weak_definition\t";
430 FlagMap[Symbol] |= EHWeakDefinition;
431 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000432 // .weak_reference
433 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000434 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000435 }
436
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000437 OS << *Symbol;
438 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000439}
440
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000441void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000442 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
443 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000444}
445
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000446void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
447 OS << "\t.def\t " << *Symbol << ';';
448 EmitEOL();
449}
450
451void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
452 OS << "\t.scl\t" << StorageClass << ';';
453 EmitEOL();
454}
455
456void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
457 OS << "\t.type\t" << Type << ';';
458 EmitEOL();
459}
460
461void MCAsmStreamer::EndCOFFSymbolDef() {
462 OS << "\t.endef";
463 EmitEOL();
464}
465
Chris Lattner99328ad2010-01-25 07:52:13 +0000466void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
467 assert(MAI.hasDotTypeDotSizeDirective());
468 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
469}
470
Chris Lattner9eb158d2010-01-23 07:47:02 +0000471void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000472 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000473 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000474 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000475 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000476 OS << ',' << ByteAlignment;
477 else
478 OS << ',' << Log2_32(ByteAlignment);
479 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000480 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000481}
482
Chris Lattner9eb158d2010-01-23 07:47:02 +0000483/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
484///
485/// @param Symbol - The common symbol to emit.
486/// @param Size - The size of the common symbol.
487void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
488 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
489 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
490 EmitEOL();
491}
492
Daniel Dunbar8751b942009-08-28 05:48:22 +0000493void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000494 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000495 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000496 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000497
Chris Lattner93b6db32009-08-08 23:39:42 +0000498 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000499 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000500 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000501
Chris Lattner9be3fee2009-07-10 22:20:30 +0000502 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000503 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000504 if (ByteAlignment != 0)
505 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000506 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000507 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000508}
509
Eric Christopherd04d98d2010-05-17 02:13:02 +0000510// .tbss sym, size, align
511// This depends that the symbol has already been mangled from the original,
512// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000513void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
514 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000515 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000516 // Instead of using the Section we'll just use the shortcut.
517 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000518 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000519
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000520 // Output align if we have it. We default to 1 so don't bother printing
521 // that.
522 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000523
Eric Christopher482eba02010-05-14 01:50:28 +0000524 EmitEOL();
525}
526
Chris Lattner12e555c2010-01-23 00:15:00 +0000527static inline char toOctal(int X) { return (X&7)+'0'; }
528
Chris Lattnera6594fc2010-01-25 18:58:59 +0000529static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
530 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000531
Chris Lattnera6594fc2010-01-25 18:58:59 +0000532 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
533 unsigned char C = Data[i];
534 if (C == '"' || C == '\\') {
535 OS << '\\' << (char)C;
536 continue;
537 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000538
Chris Lattnera6594fc2010-01-25 18:58:59 +0000539 if (isprint((unsigned char)C)) {
540 OS << (char)C;
541 continue;
542 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000543
Chris Lattnera6594fc2010-01-25 18:58:59 +0000544 switch (C) {
545 case '\b': OS << "\\b"; break;
546 case '\f': OS << "\\f"; break;
547 case '\n': OS << "\\n"; break;
548 case '\r': OS << "\\r"; break;
549 case '\t': OS << "\\t"; break;
550 default:
551 OS << '\\';
552 OS << toOctal(C >> 6);
553 OS << toOctal(C >> 3);
554 OS << toOctal(C >> 0);
555 break;
556 }
557 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000558
Chris Lattnera6594fc2010-01-25 18:58:59 +0000559 OS << '"';
560}
561
562
Chris Lattneraaec2052010-01-19 19:46:13 +0000563void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000564 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000565 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000566
Chris Lattner12e555c2010-01-23 00:15:00 +0000567 if (Data.size() == 1) {
568 OS << MAI.getData8bitsDirective(AddrSpace);
569 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000570 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000571 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000572 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000573
574 // If the data ends with 0 and the target supports .asciz, use it, otherwise
575 // use .ascii
576 if (MAI.getAscizDirective() && Data.back() == 0) {
577 OS << MAI.getAscizDirective();
578 Data = Data.substr(0, Data.size()-1);
579 } else {
580 OS << MAI.getAsciiDirective();
581 }
582
Chris Lattnera6594fc2010-01-25 18:58:59 +0000583 OS << ' ';
584 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000585 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000586}
587
Rafael Espindola2df042c2010-12-03 02:54:21 +0000588void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
589 unsigned AddrSpace) {
590 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
591}
592
Rafael Espindola89b93722010-12-10 07:39:47 +0000593void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000594 unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000595 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000596 const char *Directive = 0;
597 switch (Size) {
598 default: break;
599 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
600 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
601 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000602 case 8:
603 Directive = MAI.getData64bitsDirective(AddrSpace);
604 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
605 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000606 int64_t IntValue;
607 if (!Value->EvaluateAsAbsolute(IntValue))
608 report_fatal_error("Don't know how to emit this value.");
Evan Cheng1be0e272011-07-15 02:09:41 +0000609 if (getContext().getAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000610 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
611 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000612 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000613 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
614 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000615 }
616 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000617 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000618
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000619 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000620 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000621 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000622}
623
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000624void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000625 int64_t IntValue;
626 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000627 EmitULEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000628 return;
629 }
Rafael Espindola73873452010-11-04 18:17:08 +0000630 assert(MAI.hasLEB128() && "Cannot print a .uleb");
631 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000632 EmitEOL();
633}
634
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000635void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000636 int64_t IntValue;
637 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000638 EmitSLEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000639 return;
640 }
Rafael Espindola73873452010-11-04 18:17:08 +0000641 assert(MAI.hasLEB128() && "Cannot print a .sleb");
642 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000643 EmitEOL();
644}
645
Chris Lattner718fb592010-01-25 21:28:50 +0000646void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
647 assert(MAI.getGPRel32Directive() != 0);
648 OS << MAI.getGPRel32Directive() << *Value;
649 EmitEOL();
650}
651
652
Chris Lattner6113b3d2010-01-19 18:52:28 +0000653/// EmitFill - Emit NumBytes bytes worth of the value specified by
654/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000655void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
656 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000657 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000658
Chris Lattneraaec2052010-01-19 19:46:13 +0000659 if (AddrSpace == 0)
660 if (const char *ZeroDirective = MAI.getZeroDirective()) {
661 OS << ZeroDirective << NumBytes;
662 if (FillValue != 0)
663 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000664 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000665 return;
666 }
667
668 // Emit a byte at a time.
669 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000670}
671
Daniel Dunbar84a29262009-06-24 19:25:34 +0000672void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
673 unsigned ValueSize,
674 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000675 // Some assemblers don't support non-power of two alignments, so we always
676 // emit alignments as a power of two if possible.
677 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000678 switch (ValueSize) {
679 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000680 case 1: OS << MAI.getAlignDirective(); break;
681 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000682 case 2: OS << ".p2alignw "; break;
683 case 4: OS << ".p2alignl "; break;
684 case 8: llvm_unreachable("Unsupported alignment size!");
685 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000686
Chris Lattner33adcfb2009-08-22 21:43:10 +0000687 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000688 OS << ByteAlignment;
689 else
690 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000691
Chris Lattner663c2d22009-08-19 06:12:02 +0000692 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000693 OS << ", 0x";
694 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000695
Jim Grosbach00545e12010-09-22 18:16:55 +0000696 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000697 OS << ", " << MaxBytesToEmit;
698 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000699 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000700 return;
701 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000702
Chris Lattner663c2d22009-08-19 06:12:02 +0000703 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000704 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000705 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000706 default: llvm_unreachable("Invalid size for machine code value!");
707 case 1: OS << ".balign"; break;
708 case 2: OS << ".balignw"; break;
709 case 4: OS << ".balignl"; break;
710 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000711 }
712
Chris Lattner663c2d22009-08-19 06:12:02 +0000713 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000714 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000715 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000716 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000717 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000718}
719
Kevin Enderby6e720482010-02-23 18:26:34 +0000720void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
721 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000722 // Emit with a text fill value.
723 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
724 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000725}
726
Daniel Dunbar821e3332009-08-31 08:09:28 +0000727void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000728 unsigned char Value) {
729 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000730 OS << ".org " << *Offset << ", " << (unsigned) Value;
731 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000732}
733
Chris Lattnera6594fc2010-01-25 18:58:59 +0000734
735void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
736 assert(MAI.hasSingleParameterDotFile());
737 OS << "\t.file\t";
738 PrintQuotedString(Filename, OS);
739 EmitEOL();
740}
741
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000742bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola89b93722010-12-10 07:39:47 +0000743 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000744 OS << "\t.file\t" << FileNo << ' ';
745 PrintQuotedString(Filename, OS);
746 EmitEOL();
747 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000748 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
749}
750
751void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
752 unsigned Column, unsigned Flags,
753 unsigned Isa,
Devang Patel3f3bf932011-04-18 20:26:49 +0000754 unsigned Discriminator,
755 StringRef FileName) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000756 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000757 Isa, Discriminator, FileName);
Rafael Espindola89b93722010-12-10 07:39:47 +0000758 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000759 return;
760
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000761 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
762 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
763 OS << " basic_block";
764 if (Flags & DWARF2_FLAG_PROLOGUE_END)
765 OS << " prologue_end";
766 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
767 OS << " epilogue_begin";
768
769 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
770 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
771 OS << " is_stmt ";
772
773 if (Flags & DWARF2_FLAG_IS_STMT)
774 OS << "1";
775 else
776 OS << "0";
777 }
778
779 if (Isa)
780 OS << "isa " << Isa;
781 if (Discriminator)
782 OS << "discriminator " << Discriminator;
Devang Patel3f3bf932011-04-18 20:26:49 +0000783
784 if (IsVerboseAsm) {
785 OS.PadToColumn(MAI.getCommentColumn());
786 OS << MAI.getCommentString() << ' ' << FileName << ':'
787 << Line << ':' << Column;
788 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000789 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000790}
791
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000792void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
793 MCStreamer::EmitCFISections(EH, Debug);
794
795 if (!UseCFI)
796 return;
797
798 OS << "\t.cfi_sections ";
799 if (EH) {
800 OS << ".eh_frame";
801 if (Debug)
802 OS << ", .debug_frame";
803 } else if (Debug) {
804 OS << ".debug_frame";
805 }
806
807 EmitEOL();
808}
809
Rafael Espindola066c2f42011-04-12 23:59:07 +0000810void MCAsmStreamer::EmitCFIStartProc() {
811 MCStreamer::EmitCFIStartProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000812
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000813 if (!UseCFI)
814 return;
815
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000816 OS << "\t.cfi_startproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000817 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000818}
819
Rafael Espindola066c2f42011-04-12 23:59:07 +0000820void MCAsmStreamer::EmitCFIEndProc() {
821 MCStreamer::EmitCFIEndProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000822
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000823 if (!UseCFI)
824 return;
825
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000826 OS << "\t.cfi_endproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000827 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000828}
829
Rafael Espindola6e032942011-05-30 20:20:15 +0000830void MCAsmStreamer::EmitRegisterName(int64_t Register) {
Akira Hatanaka3014b2f2011-07-07 20:30:33 +0000831 if (InstPrinter && !MAI.useDwarfRegNumForCFI()) {
Evan Cheng0e6a0522011-07-18 20:57:22 +0000832 const MCRegisterInfo &MRI = getContext().getRegisterInfo();
833 unsigned LLVMRegister = MRI.getLLVMRegNum(Register, true);
Rafael Espindolacde4ce42011-06-02 02:34:55 +0000834 InstPrinter->printRegName(OS, LLVMRegister);
Rafael Espindola6e032942011-05-30 20:20:15 +0000835 } else {
836 OS << Register;
837 }
838}
839
Rafael Espindola066c2f42011-04-12 23:59:07 +0000840void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindola7f46b082011-04-29 21:41:06 +0000841 MCStreamer::EmitCFIDefCfa(Register, Offset);
842
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000843 if (!UseCFI)
844 return;
845
Rafael Espindola6e032942011-05-30 20:20:15 +0000846 OS << "\t.cfi_def_cfa ";
847 EmitRegisterName(Register);
848 OS << ", " << Offset;
Rafael Espindola7f46b082011-04-29 21:41:06 +0000849 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000850}
851
852void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
853 MCStreamer::EmitCFIDefCfaOffset(Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000854
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000855 if (!UseCFI)
856 return;
857
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000858 OS << "\t.cfi_def_cfa_offset " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000859 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000860}
861
Rafael Espindola066c2f42011-04-12 23:59:07 +0000862void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
863 MCStreamer::EmitCFIDefCfaRegister(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000864
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000865 if (!UseCFI)
866 return;
867
Rafael Espindola6e032942011-05-30 20:20:15 +0000868 OS << "\t.cfi_def_cfa_register ";
869 EmitRegisterName(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000870 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000871}
872
Rafael Espindola066c2f42011-04-12 23:59:07 +0000873void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
874 this->MCStreamer::EmitCFIOffset(Register, Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000875
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000876 if (!UseCFI)
877 return;
878
Rafael Espindola6e032942011-05-30 20:20:15 +0000879 OS << "\t.cfi_offset ";
880 EmitRegisterName(Register);
881 OS << ", " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000882 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000883}
884
Rafael Espindola066c2f42011-04-12 23:59:07 +0000885void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000886 unsigned Encoding) {
Rafael Espindola066c2f42011-04-12 23:59:07 +0000887 MCStreamer::EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000888
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000889 if (!UseCFI)
890 return;
891
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000892 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000893 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000894}
895
Rafael Espindola066c2f42011-04-12 23:59:07 +0000896void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
897 MCStreamer::EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000898
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000899 if (!UseCFI)
900 return;
901
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000902 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000903 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000904}
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000905
Rafael Espindola066c2f42011-04-12 23:59:07 +0000906void MCAsmStreamer::EmitCFIRememberState() {
907 MCStreamer::EmitCFIRememberState();
908
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000909 if (!UseCFI)
910 return;
911
Rafael Espindola066c2f42011-04-12 23:59:07 +0000912 OS << "\t.cfi_remember_state";
913 EmitEOL();
914}
915
916void MCAsmStreamer::EmitCFIRestoreState() {
917 MCStreamer::EmitCFIRestoreState();
918
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000919 if (!UseCFI)
920 return;
921
Rafael Espindola066c2f42011-04-12 23:59:07 +0000922 OS << "\t.cfi_restore_state";
923 EmitEOL();
924}
925
926void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
927 MCStreamer::EmitCFISameValue(Register);
928
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000929 if (!UseCFI)
930 return;
931
Rafael Espindola6e032942011-05-30 20:20:15 +0000932 OS << "\t.cfi_same_value ";
933 EmitRegisterName(Register);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000934 EmitEOL();
935}
936
937void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
938 MCStreamer::EmitCFIRelOffset(Register, Offset);
939
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000940 if (!UseCFI)
941 return;
942
Rafael Espindola6e032942011-05-30 20:20:15 +0000943 OS << "\t.cfi_rel_offset ";
944 EmitRegisterName(Register);
945 OS << ", " << Offset;
Rafael Espindola066c2f42011-04-12 23:59:07 +0000946 EmitEOL();
947}
948
949void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
950 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
951
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000952 if (!UseCFI)
953 return;
954
Rafael Espindola066c2f42011-04-12 23:59:07 +0000955 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
956 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000957}
958
Charles Davisfbc539f2011-05-22 21:12:15 +0000959void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
Charles Daviscde87e22011-05-20 18:19:22 +0000960 MCStreamer::EmitWin64EHStartProc(Symbol);
961
Charles Davis440596f2011-05-19 17:46:39 +0000962 OS << ".seh_proc " << *Symbol;
Charles Davis0e30f022011-05-18 04:58:05 +0000963 EmitEOL();
964}
965
Charles Davis8b3e5e52011-05-18 22:13:51 +0000966void MCAsmStreamer::EmitWin64EHEndProc() {
Charles Daviscde87e22011-05-20 18:19:22 +0000967 MCStreamer::EmitWin64EHEndProc();
968
Charles Davis440596f2011-05-19 17:46:39 +0000969 OS << "\t.seh_endproc";
Charles Davis0e30f022011-05-18 04:58:05 +0000970 EmitEOL();
971}
972
Charles Davis8b3e5e52011-05-18 22:13:51 +0000973void MCAsmStreamer::EmitWin64EHStartChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000974 MCStreamer::EmitWin64EHStartChained();
975
Charles Davis440596f2011-05-19 17:46:39 +0000976 OS << "\t.seh_startchained";
Charles Davisf0709012011-05-18 20:54:10 +0000977 EmitEOL();
978}
979
Charles Davis8b3e5e52011-05-18 22:13:51 +0000980void MCAsmStreamer::EmitWin64EHEndChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000981 MCStreamer::EmitWin64EHEndChained();
982
Charles Davis440596f2011-05-19 17:46:39 +0000983 OS << "\t.seh_endchained";
Charles Davisf0709012011-05-18 20:54:10 +0000984 EmitEOL();
985}
986
Charles Davis440596f2011-05-19 17:46:39 +0000987void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
988 bool Except) {
Charles Daviscde87e22011-05-20 18:19:22 +0000989 MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
990
Charles Davis440596f2011-05-19 17:46:39 +0000991 OS << "\t.seh_handler " << *Sym;
992 if (Unwind)
993 OS << ", @unwind";
994 if (Except)
995 OS << ", @except";
Charles Davisf0709012011-05-18 20:54:10 +0000996 EmitEOL();
997}
998
Evan Chenge76a33b2011-07-20 05:58:47 +0000999static const MCSection *getWin64EHTableSection(StringRef suffix,
1000 MCContext &context) {
1001 // FIXME: This doesn't belong in MCObjectFileInfo. However,
1002 /// this duplicate code in MCWin64EH.cpp.
1003 if (suffix == "")
1004 return context.getObjectFileInfo()->getXDataSection();
1005 return context.getCOFFSection((".xdata"+suffix).str(),
1006 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1007 COFF::IMAGE_SCN_MEM_READ |
1008 COFF::IMAGE_SCN_MEM_WRITE,
1009 SectionKind::getDataRel());
1010}
1011
Charles Davis440596f2011-05-19 17:46:39 +00001012void MCAsmStreamer::EmitWin64EHHandlerData() {
Charles Daviscde87e22011-05-20 18:19:22 +00001013 MCStreamer::EmitWin64EHHandlerData();
1014
Charles Davis410ef2b2011-05-25 21:43:45 +00001015 // Switch sections. Don't call SwitchSection directly, because that will
1016 // cause the section switch to be visible in the emitted assembly.
1017 // We only do this so the section switch that terminates the handler
1018 // data block is visible.
Charles Davis7b06b732011-05-27 19:09:24 +00001019 MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
1020 StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
Evan Chenge76a33b2011-07-20 05:58:47 +00001021 const MCSection *xdataSect = getWin64EHTableSection(suffix, getContext());
Charles Davis410ef2b2011-05-25 21:43:45 +00001022 if (xdataSect)
1023 SwitchSectionNoChange(xdataSect);
1024
Charles Davis440596f2011-05-19 17:46:39 +00001025 OS << "\t.seh_handlerdata";
Charles Davisf0709012011-05-18 20:54:10 +00001026 EmitEOL();
1027}
1028
Charles Davisc3b16282011-05-19 19:35:55 +00001029void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
Charles Daviscde87e22011-05-20 18:19:22 +00001030 MCStreamer::EmitWin64EHPushReg(Register);
1031
Charles Davis440596f2011-05-19 17:46:39 +00001032 OS << "\t.seh_pushreg " << Register;
Charles Davis0e30f022011-05-18 04:58:05 +00001033 EmitEOL();
1034}
1035
Charles Davisc3b16282011-05-19 19:35:55 +00001036void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001037 MCStreamer::EmitWin64EHSetFrame(Register, Offset);
1038
Charles Davis440596f2011-05-19 17:46:39 +00001039 OS << "\t.seh_setframe " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001040 EmitEOL();
1041}
1042
Charles Davisc3b16282011-05-19 19:35:55 +00001043void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
Charles Daviscde87e22011-05-20 18:19:22 +00001044 MCStreamer::EmitWin64EHAllocStack(Size);
1045
Charles Davis440596f2011-05-19 17:46:39 +00001046 OS << "\t.seh_stackalloc " << Size;
Charles Davis0e30f022011-05-18 04:58:05 +00001047 EmitEOL();
1048}
1049
Charles Davisc3b16282011-05-19 19:35:55 +00001050void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001051 MCStreamer::EmitWin64EHSaveReg(Register, Offset);
1052
Charles Davis440596f2011-05-19 17:46:39 +00001053 OS << "\t.seh_savereg " << Register << ", " << Offset;
1054 EmitEOL();
1055}
1056
Charles Davisc3b16282011-05-19 19:35:55 +00001057void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001058 MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
1059
Charles Davis440596f2011-05-19 17:46:39 +00001060 OS << "\t.seh_savexmm " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001061 EmitEOL();
1062}
1063
Charles Davis8b3e5e52011-05-18 22:13:51 +00001064void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
Charles Daviscde87e22011-05-20 18:19:22 +00001065 MCStreamer::EmitWin64EHPushFrame(Code);
1066
Charles Davis440596f2011-05-19 17:46:39 +00001067 OS << "\t.seh_pushframe";
Charles Davis0e30f022011-05-18 04:58:05 +00001068 if (Code)
Charles Davis440596f2011-05-19 17:46:39 +00001069 OS << " @code";
Charles Davis0e30f022011-05-18 04:58:05 +00001070 EmitEOL();
1071}
1072
Charles Davis8b3e5e52011-05-18 22:13:51 +00001073void MCAsmStreamer::EmitWin64EHEndProlog(void) {
Charles Daviscde87e22011-05-20 18:19:22 +00001074 MCStreamer::EmitWin64EHEndProlog();
1075
Charles Davis440596f2011-05-19 17:46:39 +00001076 OS << "\t.seh_endprologue";
Charles Davis0e30f022011-05-18 04:58:05 +00001077 EmitEOL();
1078}
1079
Daniel Dunbar6b716532010-02-09 23:00:14 +00001080void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
1081 raw_ostream &OS = GetCommentOS();
1082 SmallString<256> Code;
1083 SmallVector<MCFixup, 4> Fixups;
1084 raw_svector_ostream VecOS(Code);
1085 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
1086 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +00001087
Daniel Dunbar6b716532010-02-09 23:00:14 +00001088 // If we are showing fixups, create symbolic markers in the encoded
1089 // representation. We do this by making a per-bit map to the fixup item index,
1090 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +00001091 SmallVector<uint8_t, 64> FixupMap;
1092 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001093 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1094 FixupMap[i] = 0;
1095
1096 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1097 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001098 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001099 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1100 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1101 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1102 FixupMap[Index] = 1 + i;
1103 }
1104 }
1105
Evan Cheng5c2eef62011-07-08 22:49:42 +00001106 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001107 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
Daniel Dunbar6b716532010-02-09 23:00:14 +00001108 OS << "encoding: [";
1109 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1110 if (i)
1111 OS << ',';
1112
1113 // See if all bits are the same map entry.
1114 uint8_t MapEntry = FixupMap[i * 8 + 0];
1115 for (unsigned j = 1; j != 8; ++j) {
1116 if (FixupMap[i * 8 + j] == MapEntry)
1117 continue;
1118
1119 MapEntry = uint8_t(~0U);
1120 break;
1121 }
1122
1123 if (MapEntry != uint8_t(~0U)) {
1124 if (MapEntry == 0) {
1125 OS << format("0x%02x", uint8_t(Code[i]));
1126 } else {
Evan Cheng82caf1a2011-01-13 21:45:26 +00001127 if (Code[i]) {
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001128 // FIXME: Some of the 8 bits require fix up.
Evan Cheng82caf1a2011-01-13 21:45:26 +00001129 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1130 << char('A' + MapEntry - 1) << '\'';
1131 } else
1132 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001133 }
1134 } else {
1135 // Otherwise, write out in binary.
1136 OS << "0b";
Jay Foad95c3e482011-06-23 09:09:15 +00001137 for (unsigned j = 8; j--;) {
Daniel Dunbar6b716532010-02-09 23:00:14 +00001138 unsigned Bit = (Code[i] >> j) & 1;
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001139
Chris Lattner3170a3b2010-11-15 05:56:19 +00001140 unsigned FixupBit;
Evan Cheng1be0e272011-07-15 02:09:41 +00001141 if (getContext().getAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +00001142 FixupBit = i * 8 + j;
1143 else
1144 FixupBit = i * 8 + (7-j);
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001145
Jay Foad95c3e482011-06-23 09:09:15 +00001146 if (uint8_t MapEntry = FixupMap[FixupBit]) {
1147 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1148 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001149 } else
1150 OS << Bit;
1151 }
1152 }
1153 }
1154 OS << "]\n";
1155
1156 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1157 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001158 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001159 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +00001160 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +00001161 }
1162}
1163
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +00001164void MCAsmStreamer::EmitFnStart() {
1165 OS << "\t.fnstart";
1166 EmitEOL();
1167}
1168
1169void MCAsmStreamer::EmitFnEnd() {
1170 OS << "\t.fnend";
1171 EmitEOL();
1172}
1173
1174void MCAsmStreamer::EmitCantUnwind() {
1175 OS << "\t.cantunwind";
1176 EmitEOL();
1177}
1178
1179void MCAsmStreamer::EmitHandlerData() {
1180 OS << "\t.handlerdata";
1181 EmitEOL();
1182}
1183
1184void MCAsmStreamer::EmitPersonality(const MCSymbol *Personality) {
1185 OS << "\t.personality " << Personality->getName();
1186 EmitEOL();
1187}
1188
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001189void MCAsmStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001190 OS << "\t.setfp\t";
1191 InstPrinter->printRegName(OS, FpReg);
1192 OS << ", ";
1193 InstPrinter->printRegName(OS, SpReg);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001194 if (Offset)
1195 OS << ", #" << Offset;
1196 EmitEOL();
1197}
1198
1199void MCAsmStreamer::EmitPad(int64_t Offset) {
1200 OS << "\t.pad\t#" << Offset;
1201 EmitEOL();
1202}
1203
1204void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
1205 bool isVector) {
1206 assert(RegList.size() && "RegList should not be empty");
1207 if (isVector)
1208 OS << "\t.vsave\t{";
1209 else
1210 OS << "\t.save\t{";
1211
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001212 InstPrinter->printRegName(OS, RegList[0]);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001213
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001214 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
1215 OS << ", ";
1216 InstPrinter->printRegName(OS, RegList[i]);
1217 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001218
1219 OS << "}";
1220 EmitEOL();
1221}
1222
Daniel Dunbar6b716532010-02-09 23:00:14 +00001223void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +00001224 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Daniel Dunbar6b716532010-02-09 23:00:14 +00001225
1226 // Show the encoding in a comment if we have a code emitter.
1227 if (Emitter)
1228 AddEncodingComment(Inst);
1229
Chris Lattner30d9a642010-02-09 00:54:51 +00001230 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001231 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001232 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001233 GetCommentOS() << "\n";
1234 }
1235
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001236 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001237 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +00001238 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001239 else
1240 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +00001241 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +00001242}
1243
Jim Grosbachbd4ec842010-09-22 18:18:30 +00001244/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +00001245/// the specified string in the output .s file. This capability is
1246/// indicated by the hasRawTextSupport() predicate.
1247void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001248 if (!String.empty() && String.back() == '\n')
1249 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +00001250 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001251 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +00001252}
1253
Daniel Dunbara11af532009-06-24 01:03:06 +00001254void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +00001255 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +00001256 if (getContext().hasDwarfFiles() && !UseLoc)
1257 MCDwarfFileTable::Emit(this);
Rafael Espindola5426a9e2011-05-01 04:49:54 +00001258
Rafael Espindolac25dad82011-05-10 03:14:15 +00001259 if (!UseCFI)
1260 EmitFrames(false);
Daniel Dunbara11af532009-06-24 01:03:06 +00001261}
Chris Lattner86e22112010-01-22 07:29:22 +00001262MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1263 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +00001264 bool isVerboseAsm, bool useLoc,
Bill Wendling916a94b2011-06-17 20:35:21 +00001265 bool useCFI, MCInstPrinter *IP,
1266 MCCodeEmitter *CE, TargetAsmBackend *TAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +00001267 bool ShowInst) {
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +00001268 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +00001269 IP, CE, TAB, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +00001270}