blob: 68154345c8d2adb678ab5e861b64519443fec0dd [file] [log] [blame]
Daniel Dunbara11af532009-06-24 01:03:06 +00001//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000011#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000012#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbar2761fc42010-12-16 03:20:06 +000015#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000016#include "llvm/MC/MCInst.h"
Chris Lattner90edac02009-09-14 03:02:37 +000017#include "llvm/MC/MCInstPrinter.h"
Evan Chengbfe36862011-07-26 20:57:44 +000018#include "llvm/MC/MCObjectFileInfo.h"
Evan Cheng0e6a0522011-07-18 20:57:22 +000019#include "llvm/MC/MCRegisterInfo.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000020#include "llvm/MC/MCSectionCOFF.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000022#include "llvm/MC/MCSymbol.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000023#include "llvm/MC/MCAsmBackend.h"
Chris Lattner4c42a6d2010-03-19 05:48:53 +000024#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000025#include "llvm/ADT/SmallString.h"
Bill Wendling916a94b2011-06-17 20:35:21 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner86e22112010-01-22 07:29:22 +000027#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000029#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000030#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000031#include "llvm/Support/FormattedStream.h"
Nick 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;
Evan Cheng78c10ee2011-07-25 23:24:55 +000044 OwningPtr<MCAsmBackend> 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,
Evan Cheng78c10ee2011-07-25 23:24:55 +000067 MCAsmBackend *asmbackend,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000068 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.
Benjamin Kramer36a16012011-09-01 23:04:27 +0000161 /// @param Size - The alignment of the common symbol in bytes.
162 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
163 unsigned ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000164
Daniel Dunbar8751b942009-08-28 05:48:22 +0000165 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000166 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000167
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000168 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
169 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000170
Chris Lattneraaec2052010-01-19 19:46:13 +0000171 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000172
Rafael Espindola89b93722010-12-10 07:39:47 +0000173 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000174 unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000175 virtual void EmitIntValue(uint64_t Value, unsigned Size,
176 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000177
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000178 virtual void EmitULEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000179
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000180 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000181
Chris Lattner718fb592010-01-25 21:28:50 +0000182 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000183
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000184
Chris Lattneraaec2052010-01-19 19:46:13 +0000185 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
186 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000187
Chris Lattner46a947d2009-08-17 04:17:34 +0000188 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
189 unsigned ValueSize = 1,
190 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000191
Kevin Enderby6e720482010-02-23 18:26:34 +0000192 virtual void EmitCodeAlignment(unsigned ByteAlignment,
193 unsigned MaxBytesToEmit = 0);
194
Daniel Dunbar821e3332009-08-31 08:09:28 +0000195 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000196 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000197
Chris Lattnera6594fc2010-01-25 18:58:59 +0000198 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000199 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
200 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
201 unsigned Column, unsigned Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000202 unsigned Isa, unsigned Discriminator,
203 StringRef FileName);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000204
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000205 virtual void EmitCFISections(bool EH, bool Debug);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000206 virtual void EmitCFIStartProc();
207 virtual void EmitCFIEndProc();
208 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
209 virtual void EmitCFIDefCfaOffset(int64_t Offset);
210 virtual void EmitCFIDefCfaRegister(int64_t Register);
211 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
212 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
213 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
214 virtual void EmitCFIRememberState();
215 virtual void EmitCFIRestoreState();
216 virtual void EmitCFISameValue(int64_t Register);
217 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
218 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000219
Charles Davisfbc539f2011-05-22 21:12:15 +0000220 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
Charles Davis0e30f022011-05-18 04:58:05 +0000221 virtual void EmitWin64EHEndProc();
Charles Davisf0709012011-05-18 20:54:10 +0000222 virtual void EmitWin64EHStartChained();
223 virtual void EmitWin64EHEndChained();
Charles Davis440596f2011-05-19 17:46:39 +0000224 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
225 bool Except);
226 virtual void EmitWin64EHHandlerData();
Charles Davisc3b16282011-05-19 19:35:55 +0000227 virtual void EmitWin64EHPushReg(unsigned Register);
228 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
229 virtual void EmitWin64EHAllocStack(unsigned Size);
230 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
231 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
Charles Davis0e30f022011-05-18 04:58:05 +0000232 virtual void EmitWin64EHPushFrame(bool Code);
233 virtual void EmitWin64EHEndProlog();
234
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000235 virtual void EmitFnStart();
236 virtual void EmitFnEnd();
237 virtual void EmitCantUnwind();
238 virtual void EmitPersonality(const MCSymbol *Personality);
239 virtual void EmitHandlerData();
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000240 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
241 virtual void EmitPad(int64_t Offset);
242 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool);
243
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000244
Chris Lattnera6594fc2010-01-25 18:58:59 +0000245 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000246
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000247 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000248 /// the specified string in the output .s file. This capability is
249 /// indicated by the hasRawTextSupport() predicate.
250 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000251
Chris Lattner46a947d2009-08-17 04:17:34 +0000252 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000253
Chris Lattner46a947d2009-08-17 04:17:34 +0000254 /// @}
255};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000256
Chris Lattner46a947d2009-08-17 04:17:34 +0000257} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000258
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000259/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000260/// file if applicable as a QoI issue to make the output of the compiler
261/// more readable. This only affects the MCAsmStreamer, and only when
262/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000263void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000264 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000265
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000266 // Make sure that CommentStream is flushed.
267 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000268
Chris Lattner86e22112010-01-22 07:29:22 +0000269 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000270 // Each comment goes on its own line.
271 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000272
Chris Lattner14ca1772010-01-22 21:16:10 +0000273 // Tell the comment stream that the vector changed underneath it.
274 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000275}
276
277void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000278 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
279 OS << '\n';
280 return;
281 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000282
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000283 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000284 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000285
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000286 assert(Comments.back() == '\n' &&
287 "Comment array not newline terminated");
288 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000289 // Emit a line of comments.
290 OS.PadToColumn(MAI.getCommentColumn());
291 size_t Position = Comments.find('\n');
292 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000293
Chris Lattner86e22112010-01-22 07:29:22 +0000294 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000295 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000296
Chris Lattner14ca1772010-01-22 21:16:10 +0000297 CommentToEmit.clear();
298 // Tell the comment stream that the vector changed underneath it.
299 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000300}
301
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000302static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
303 assert(Bytes && "Invalid size!");
304 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
305}
306
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000307void MCAsmStreamer::ChangeSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000308 assert(Section && "Cannot switch to a null section!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000309 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000310}
311
Rafael Espindola277abc82011-04-30 16:34:57 +0000312void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
313 MCSymbol *EHSymbol) {
314 if (UseCFI)
315 return;
316
317 unsigned Flags = FlagMap.lookup(Symbol);
318
319 if (Flags & EHGlobal)
320 EmitSymbolAttribute(EHSymbol, MCSA_Global);
321 if (Flags & EHWeakDefinition)
322 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
323 if (Flags & EHPrivateExtern)
324 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
325}
326
Daniel Dunbara11af532009-06-24 01:03:06 +0000327void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000328 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaed708f92011-04-27 15:21:19 +0000329 MCStreamer::EmitLabel(Symbol);
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000330
Chris Lattnere07b75e2010-09-22 22:19:53 +0000331 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000332 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000333}
334
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000335void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000336 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000337 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000338 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000339 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Evan Chengbd27f5a2011-07-27 00:38:12 +0000340 case MCAF_Code16: OS << '\t'<< MAI.getCode16Directive(); break;
341 case MCAF_Code32: OS << '\t'<< MAI.getCode32Directive(); break;
342 case MCAF_Code64: OS << '\t'<< MAI.getCode64Directive(); break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000343 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000344 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000345}
346
Jim Grosbachce792992010-11-05 22:08:08 +0000347void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
348 // This needs to emit to a temporary string to get properly quoted
349 // MCSymbols when they have spaces in them.
350 OS << "\t.thumb_func";
Rafael Espindola64695402011-05-16 16:17:21 +0000351 // Only Mach-O hasSubsectionsViaSymbols()
352 if (MAI.hasSubsectionsViaSymbols())
Jim Grosbachce792992010-11-05 22:08:08 +0000353 OS << '\t' << *Func;
354 EmitEOL();
355}
356
Daniel Dunbar821e3332009-08-31 08:09:28 +0000357void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000358 OS << *Symbol << " = " << *Value;
359 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000360
361 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000362 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000363}
364
Rafael Espindola484291c2010-11-01 14:28:48 +0000365void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
366 OS << ".weakref " << *Alias << ", " << *Symbol;
367 EmitEOL();
368}
369
Rafael Espindola32a006e2010-12-03 00:55:40 +0000370void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
371 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000372 const MCSymbol *Label,
373 unsigned PointerSize) {
374 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000375}
376
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000377void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
378 const MCSymbol *Label) {
379 EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
380 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
Rafael Espindolaa6f26782011-05-19 21:40:34 +0000381 AddrDelta = ForceExpAbs(AddrDelta);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000382 EmitValue(AddrDelta, 4);
383}
384
385
Daniel Dunbarfffff912009-10-16 01:34:54 +0000386void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000387 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000388 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000389 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000390 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
391 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
392 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
393 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
394 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
395 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000396 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000397 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000398 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000399 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
400 switch (Attribute) {
401 default: assert(0 && "Unknown ELF .type");
402 case MCSA_ELF_TypeFunction: OS << "function"; break;
403 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
404 case MCSA_ELF_TypeObject: OS << "object"; break;
405 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
406 case MCSA_ELF_TypeCommon: OS << "common"; break;
407 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000408 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000409 }
410 EmitEOL();
411 return;
412 case MCSA_Global: // .globl/.global
413 OS << MAI.getGlobalDirective();
Rafael Espindola277abc82011-04-30 16:34:57 +0000414 FlagMap[Symbol] |= EHGlobal;
Chris Lattnered0ab152010-01-25 18:30:45 +0000415 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000416 case MCSA_Hidden: OS << "\t.hidden\t"; break;
417 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
418 case MCSA_Internal: OS << "\t.internal\t"; break;
419 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
420 case MCSA_Local: OS << "\t.local\t"; break;
421 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000422 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000423 case MCSA_PrivateExtern:
424 OS << "\t.private_extern\t";
425 FlagMap[Symbol] |= EHPrivateExtern;
426 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000427 case MCSA_Protected: OS << "\t.protected\t"; break;
428 case MCSA_Reference: OS << "\t.reference\t"; break;
429 case MCSA_Weak: OS << "\t.weak\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000430 case MCSA_WeakDefinition:
431 OS << "\t.weak_definition\t";
432 FlagMap[Symbol] |= EHWeakDefinition;
433 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000434 // .weak_reference
435 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000436 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000437 }
438
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000439 OS << *Symbol;
440 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000441}
442
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000443void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000444 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
445 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000446}
447
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000448void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
449 OS << "\t.def\t " << *Symbol << ';';
450 EmitEOL();
451}
452
453void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
454 OS << "\t.scl\t" << StorageClass << ';';
455 EmitEOL();
456}
457
458void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
459 OS << "\t.type\t" << Type << ';';
460 EmitEOL();
461}
462
463void MCAsmStreamer::EndCOFFSymbolDef() {
464 OS << "\t.endef";
465 EmitEOL();
466}
467
Chris Lattner99328ad2010-01-25 07:52:13 +0000468void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
469 assert(MAI.hasDotTypeDotSizeDirective());
470 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
471}
472
Chris Lattner9eb158d2010-01-23 07:47:02 +0000473void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000474 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000475 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000476 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000477 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000478 OS << ',' << ByteAlignment;
479 else
480 OS << ',' << Log2_32(ByteAlignment);
481 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000482 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000483}
484
Chris Lattner9eb158d2010-01-23 07:47:02 +0000485/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
486///
487/// @param Symbol - The common symbol to emit.
488/// @param Size - The size of the common symbol.
Benjamin Kramer36a16012011-09-01 23:04:27 +0000489void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
490 unsigned ByteAlign) {
491 assert(MAI.getLCOMMDirectiveType() != LCOMM::None &&
492 "Doesn't have .lcomm, can't emit it!");
Chris Lattner9eb158d2010-01-23 07:47:02 +0000493 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
Benjamin Kramer36a16012011-09-01 23:04:27 +0000494 if (ByteAlign > 1) {
495 assert(MAI.getLCOMMDirectiveType() == LCOMM::ByteAlignment &&
496 "Alignment not supported on .lcomm!");
497 OS << ',' << ByteAlign;
498 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000499 EmitEOL();
500}
501
Daniel Dunbar8751b942009-08-28 05:48:22 +0000502void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000503 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000504 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000505 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000506
Chris Lattner93b6db32009-08-08 23:39:42 +0000507 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000508 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000509 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000510
Chris Lattner9be3fee2009-07-10 22:20:30 +0000511 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000512 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000513 if (ByteAlignment != 0)
514 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000515 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000516 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000517}
518
Eric Christopherd04d98d2010-05-17 02:13:02 +0000519// .tbss sym, size, align
520// This depends that the symbol has already been mangled from the original,
521// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000522void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
523 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000524 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000525 // Instead of using the Section we'll just use the shortcut.
526 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000527 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000528
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000529 // Output align if we have it. We default to 1 so don't bother printing
530 // that.
531 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000532
Eric Christopher482eba02010-05-14 01:50:28 +0000533 EmitEOL();
534}
535
Chris Lattner12e555c2010-01-23 00:15:00 +0000536static inline char toOctal(int X) { return (X&7)+'0'; }
537
Chris Lattnera6594fc2010-01-25 18:58:59 +0000538static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
539 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000540
Chris Lattnera6594fc2010-01-25 18:58:59 +0000541 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
542 unsigned char C = Data[i];
543 if (C == '"' || C == '\\') {
544 OS << '\\' << (char)C;
545 continue;
546 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000547
Chris Lattnera6594fc2010-01-25 18:58:59 +0000548 if (isprint((unsigned char)C)) {
549 OS << (char)C;
550 continue;
551 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000552
Chris Lattnera6594fc2010-01-25 18:58:59 +0000553 switch (C) {
554 case '\b': OS << "\\b"; break;
555 case '\f': OS << "\\f"; break;
556 case '\n': OS << "\\n"; break;
557 case '\r': OS << "\\r"; break;
558 case '\t': OS << "\\t"; break;
559 default:
560 OS << '\\';
561 OS << toOctal(C >> 6);
562 OS << toOctal(C >> 3);
563 OS << toOctal(C >> 0);
564 break;
565 }
566 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000567
Chris Lattnera6594fc2010-01-25 18:58:59 +0000568 OS << '"';
569}
570
571
Chris Lattneraaec2052010-01-19 19:46:13 +0000572void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000573 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000574 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000575
Chris Lattner12e555c2010-01-23 00:15:00 +0000576 if (Data.size() == 1) {
577 OS << MAI.getData8bitsDirective(AddrSpace);
578 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000579 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000580 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000581 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000582
583 // If the data ends with 0 and the target supports .asciz, use it, otherwise
584 // use .ascii
585 if (MAI.getAscizDirective() && Data.back() == 0) {
586 OS << MAI.getAscizDirective();
587 Data = Data.substr(0, Data.size()-1);
588 } else {
589 OS << MAI.getAsciiDirective();
590 }
591
Chris Lattnera6594fc2010-01-25 18:58:59 +0000592 OS << ' ';
593 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000594 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000595}
596
Rafael Espindola2df042c2010-12-03 02:54:21 +0000597void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
598 unsigned AddrSpace) {
599 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
600}
601
Rafael Espindola89b93722010-12-10 07:39:47 +0000602void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000603 unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000604 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000605 const char *Directive = 0;
606 switch (Size) {
607 default: break;
608 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
609 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
610 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000611 case 8:
612 Directive = MAI.getData64bitsDirective(AddrSpace);
613 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
614 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000615 int64_t IntValue;
616 if (!Value->EvaluateAsAbsolute(IntValue))
617 report_fatal_error("Don't know how to emit this value.");
Evan Cheng1be0e272011-07-15 02:09:41 +0000618 if (getContext().getAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000619 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
620 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000621 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000622 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
623 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000624 }
625 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000626 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000627
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000628 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000629 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000630 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000631}
632
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000633void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000634 int64_t IntValue;
635 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000636 EmitULEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000637 return;
638 }
Rafael Espindola73873452010-11-04 18:17:08 +0000639 assert(MAI.hasLEB128() && "Cannot print a .uleb");
640 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000641 EmitEOL();
642}
643
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000644void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000645 int64_t IntValue;
646 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000647 EmitSLEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000648 return;
649 }
Rafael Espindola73873452010-11-04 18:17:08 +0000650 assert(MAI.hasLEB128() && "Cannot print a .sleb");
651 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000652 EmitEOL();
653}
654
Chris Lattner718fb592010-01-25 21:28:50 +0000655void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
656 assert(MAI.getGPRel32Directive() != 0);
657 OS << MAI.getGPRel32Directive() << *Value;
658 EmitEOL();
659}
660
661
Chris Lattner6113b3d2010-01-19 18:52:28 +0000662/// EmitFill - Emit NumBytes bytes worth of the value specified by
663/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000664void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
665 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000666 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000667
Chris Lattneraaec2052010-01-19 19:46:13 +0000668 if (AddrSpace == 0)
669 if (const char *ZeroDirective = MAI.getZeroDirective()) {
670 OS << ZeroDirective << NumBytes;
671 if (FillValue != 0)
672 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000673 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000674 return;
675 }
676
677 // Emit a byte at a time.
678 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000679}
680
Daniel Dunbar84a29262009-06-24 19:25:34 +0000681void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
682 unsigned ValueSize,
683 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000684 // Some assemblers don't support non-power of two alignments, so we always
685 // emit alignments as a power of two if possible.
686 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000687 switch (ValueSize) {
688 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000689 case 1: OS << MAI.getAlignDirective(); break;
690 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000691 case 2: OS << ".p2alignw "; break;
692 case 4: OS << ".p2alignl "; break;
693 case 8: llvm_unreachable("Unsupported alignment size!");
694 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000695
Chris Lattner33adcfb2009-08-22 21:43:10 +0000696 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000697 OS << ByteAlignment;
698 else
699 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000700
Chris Lattner663c2d22009-08-19 06:12:02 +0000701 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000702 OS << ", 0x";
703 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000704
Jim Grosbach00545e12010-09-22 18:16:55 +0000705 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000706 OS << ", " << MaxBytesToEmit;
707 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000708 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000709 return;
710 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000711
Chris Lattner663c2d22009-08-19 06:12:02 +0000712 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000713 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000714 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000715 default: llvm_unreachable("Invalid size for machine code value!");
716 case 1: OS << ".balign"; break;
717 case 2: OS << ".balignw"; break;
718 case 4: OS << ".balignl"; break;
719 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000720 }
721
Chris Lattner663c2d22009-08-19 06:12:02 +0000722 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000723 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000724 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000725 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000726 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000727}
728
Kevin Enderby6e720482010-02-23 18:26:34 +0000729void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
730 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000731 // Emit with a text fill value.
732 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
733 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000734}
735
Daniel Dunbar821e3332009-08-31 08:09:28 +0000736void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000737 unsigned char Value) {
738 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000739 OS << ".org " << *Offset << ", " << (unsigned) Value;
740 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000741}
742
Chris Lattnera6594fc2010-01-25 18:58:59 +0000743
744void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
745 assert(MAI.hasSingleParameterDotFile());
746 OS << "\t.file\t";
747 PrintQuotedString(Filename, OS);
748 EmitEOL();
749}
750
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000751bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola89b93722010-12-10 07:39:47 +0000752 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000753 OS << "\t.file\t" << FileNo << ' ';
754 PrintQuotedString(Filename, OS);
755 EmitEOL();
756 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000757 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
758}
759
760void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
761 unsigned Column, unsigned Flags,
762 unsigned Isa,
Devang Patel3f3bf932011-04-18 20:26:49 +0000763 unsigned Discriminator,
764 StringRef FileName) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000765 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000766 Isa, Discriminator, FileName);
Rafael Espindola89b93722010-12-10 07:39:47 +0000767 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000768 return;
769
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000770 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
771 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
772 OS << " basic_block";
773 if (Flags & DWARF2_FLAG_PROLOGUE_END)
774 OS << " prologue_end";
775 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
776 OS << " epilogue_begin";
777
778 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
779 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
780 OS << " is_stmt ";
781
782 if (Flags & DWARF2_FLAG_IS_STMT)
783 OS << "1";
784 else
785 OS << "0";
786 }
787
788 if (Isa)
789 OS << "isa " << Isa;
790 if (Discriminator)
791 OS << "discriminator " << Discriminator;
Devang Patel3f3bf932011-04-18 20:26:49 +0000792
793 if (IsVerboseAsm) {
794 OS.PadToColumn(MAI.getCommentColumn());
795 OS << MAI.getCommentString() << ' ' << FileName << ':'
796 << Line << ':' << Column;
797 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000798 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000799}
800
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000801void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
802 MCStreamer::EmitCFISections(EH, Debug);
803
804 if (!UseCFI)
805 return;
806
807 OS << "\t.cfi_sections ";
808 if (EH) {
809 OS << ".eh_frame";
810 if (Debug)
811 OS << ", .debug_frame";
812 } else if (Debug) {
813 OS << ".debug_frame";
814 }
815
816 EmitEOL();
817}
818
Rafael Espindola066c2f42011-04-12 23:59:07 +0000819void MCAsmStreamer::EmitCFIStartProc() {
820 MCStreamer::EmitCFIStartProc();
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_startproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000826 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000827}
828
Rafael Espindola066c2f42011-04-12 23:59:07 +0000829void MCAsmStreamer::EmitCFIEndProc() {
830 MCStreamer::EmitCFIEndProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000831
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000832 if (!UseCFI)
833 return;
834
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000835 OS << "\t.cfi_endproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000836 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000837}
838
Rafael Espindola6e032942011-05-30 20:20:15 +0000839void MCAsmStreamer::EmitRegisterName(int64_t Register) {
Akira Hatanaka3014b2f2011-07-07 20:30:33 +0000840 if (InstPrinter && !MAI.useDwarfRegNumForCFI()) {
Evan Cheng0e6a0522011-07-18 20:57:22 +0000841 const MCRegisterInfo &MRI = getContext().getRegisterInfo();
842 unsigned LLVMRegister = MRI.getLLVMRegNum(Register, true);
Rafael Espindolacde4ce42011-06-02 02:34:55 +0000843 InstPrinter->printRegName(OS, LLVMRegister);
Rafael Espindola6e032942011-05-30 20:20:15 +0000844 } else {
845 OS << Register;
846 }
847}
848
Rafael Espindola066c2f42011-04-12 23:59:07 +0000849void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindola7f46b082011-04-29 21:41:06 +0000850 MCStreamer::EmitCFIDefCfa(Register, Offset);
851
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000852 if (!UseCFI)
853 return;
854
Rafael Espindola6e032942011-05-30 20:20:15 +0000855 OS << "\t.cfi_def_cfa ";
856 EmitRegisterName(Register);
857 OS << ", " << Offset;
Rafael Espindola7f46b082011-04-29 21:41:06 +0000858 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000859}
860
861void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
862 MCStreamer::EmitCFIDefCfaOffset(Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000863
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000864 if (!UseCFI)
865 return;
866
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000867 OS << "\t.cfi_def_cfa_offset " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000868 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000869}
870
Rafael Espindola066c2f42011-04-12 23:59:07 +0000871void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
872 MCStreamer::EmitCFIDefCfaRegister(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000873
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000874 if (!UseCFI)
875 return;
876
Rafael Espindola6e032942011-05-30 20:20:15 +0000877 OS << "\t.cfi_def_cfa_register ";
878 EmitRegisterName(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000879 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000880}
881
Rafael Espindola066c2f42011-04-12 23:59:07 +0000882void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
883 this->MCStreamer::EmitCFIOffset(Register, Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000884
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000885 if (!UseCFI)
886 return;
887
Rafael Espindola6e032942011-05-30 20:20:15 +0000888 OS << "\t.cfi_offset ";
889 EmitRegisterName(Register);
890 OS << ", " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000891 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000892}
893
Rafael Espindola066c2f42011-04-12 23:59:07 +0000894void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000895 unsigned Encoding) {
Rafael Espindola066c2f42011-04-12 23:59:07 +0000896 MCStreamer::EmitCFIPersonality(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_personality " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000902 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000903}
904
Rafael Espindola066c2f42011-04-12 23:59:07 +0000905void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
906 MCStreamer::EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000907
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000908 if (!UseCFI)
909 return;
910
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000911 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000912 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000913}
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000914
Rafael Espindola066c2f42011-04-12 23:59:07 +0000915void MCAsmStreamer::EmitCFIRememberState() {
916 MCStreamer::EmitCFIRememberState();
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_remember_state";
922 EmitEOL();
923}
924
925void MCAsmStreamer::EmitCFIRestoreState() {
926 MCStreamer::EmitCFIRestoreState();
927
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000928 if (!UseCFI)
929 return;
930
Rafael Espindola066c2f42011-04-12 23:59:07 +0000931 OS << "\t.cfi_restore_state";
932 EmitEOL();
933}
934
935void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
936 MCStreamer::EmitCFISameValue(Register);
937
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000938 if (!UseCFI)
939 return;
940
Rafael Espindola6e032942011-05-30 20:20:15 +0000941 OS << "\t.cfi_same_value ";
942 EmitRegisterName(Register);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000943 EmitEOL();
944}
945
946void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
947 MCStreamer::EmitCFIRelOffset(Register, Offset);
948
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000949 if (!UseCFI)
950 return;
951
Rafael Espindola6e032942011-05-30 20:20:15 +0000952 OS << "\t.cfi_rel_offset ";
953 EmitRegisterName(Register);
954 OS << ", " << Offset;
Rafael Espindola066c2f42011-04-12 23:59:07 +0000955 EmitEOL();
956}
957
958void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
959 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
960
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000961 if (!UseCFI)
962 return;
963
Rafael Espindola066c2f42011-04-12 23:59:07 +0000964 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
965 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000966}
967
Charles Davisfbc539f2011-05-22 21:12:15 +0000968void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
Charles Daviscde87e22011-05-20 18:19:22 +0000969 MCStreamer::EmitWin64EHStartProc(Symbol);
970
Charles Davis440596f2011-05-19 17:46:39 +0000971 OS << ".seh_proc " << *Symbol;
Charles Davis0e30f022011-05-18 04:58:05 +0000972 EmitEOL();
973}
974
Charles Davis8b3e5e52011-05-18 22:13:51 +0000975void MCAsmStreamer::EmitWin64EHEndProc() {
Charles Daviscde87e22011-05-20 18:19:22 +0000976 MCStreamer::EmitWin64EHEndProc();
977
Charles Davis440596f2011-05-19 17:46:39 +0000978 OS << "\t.seh_endproc";
Charles Davis0e30f022011-05-18 04:58:05 +0000979 EmitEOL();
980}
981
Charles Davis8b3e5e52011-05-18 22:13:51 +0000982void MCAsmStreamer::EmitWin64EHStartChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000983 MCStreamer::EmitWin64EHStartChained();
984
Charles Davis440596f2011-05-19 17:46:39 +0000985 OS << "\t.seh_startchained";
Charles Davisf0709012011-05-18 20:54:10 +0000986 EmitEOL();
987}
988
Charles Davis8b3e5e52011-05-18 22:13:51 +0000989void MCAsmStreamer::EmitWin64EHEndChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000990 MCStreamer::EmitWin64EHEndChained();
991
Charles Davis440596f2011-05-19 17:46:39 +0000992 OS << "\t.seh_endchained";
Charles Davisf0709012011-05-18 20:54:10 +0000993 EmitEOL();
994}
995
Charles Davis440596f2011-05-19 17:46:39 +0000996void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
997 bool Except) {
Charles Daviscde87e22011-05-20 18:19:22 +0000998 MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
999
Charles Davis440596f2011-05-19 17:46:39 +00001000 OS << "\t.seh_handler " << *Sym;
1001 if (Unwind)
1002 OS << ", @unwind";
1003 if (Except)
1004 OS << ", @except";
Charles Davisf0709012011-05-18 20:54:10 +00001005 EmitEOL();
1006}
1007
Evan Chenge76a33b2011-07-20 05:58:47 +00001008static const MCSection *getWin64EHTableSection(StringRef suffix,
1009 MCContext &context) {
1010 // FIXME: This doesn't belong in MCObjectFileInfo. However,
1011 /// this duplicate code in MCWin64EH.cpp.
1012 if (suffix == "")
1013 return context.getObjectFileInfo()->getXDataSection();
1014 return context.getCOFFSection((".xdata"+suffix).str(),
1015 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1016 COFF::IMAGE_SCN_MEM_READ |
1017 COFF::IMAGE_SCN_MEM_WRITE,
1018 SectionKind::getDataRel());
1019}
1020
Charles Davis440596f2011-05-19 17:46:39 +00001021void MCAsmStreamer::EmitWin64EHHandlerData() {
Charles Daviscde87e22011-05-20 18:19:22 +00001022 MCStreamer::EmitWin64EHHandlerData();
1023
Charles Davis410ef2b2011-05-25 21:43:45 +00001024 // Switch sections. Don't call SwitchSection directly, because that will
1025 // cause the section switch to be visible in the emitted assembly.
1026 // We only do this so the section switch that terminates the handler
1027 // data block is visible.
Charles Davis7b06b732011-05-27 19:09:24 +00001028 MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
1029 StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
Evan Chenge76a33b2011-07-20 05:58:47 +00001030 const MCSection *xdataSect = getWin64EHTableSection(suffix, getContext());
Charles Davis410ef2b2011-05-25 21:43:45 +00001031 if (xdataSect)
1032 SwitchSectionNoChange(xdataSect);
1033
Charles Davis440596f2011-05-19 17:46:39 +00001034 OS << "\t.seh_handlerdata";
Charles Davisf0709012011-05-18 20:54:10 +00001035 EmitEOL();
1036}
1037
Charles Davisc3b16282011-05-19 19:35:55 +00001038void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
Charles Daviscde87e22011-05-20 18:19:22 +00001039 MCStreamer::EmitWin64EHPushReg(Register);
1040
Charles Davis440596f2011-05-19 17:46:39 +00001041 OS << "\t.seh_pushreg " << Register;
Charles Davis0e30f022011-05-18 04:58:05 +00001042 EmitEOL();
1043}
1044
Charles Davisc3b16282011-05-19 19:35:55 +00001045void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001046 MCStreamer::EmitWin64EHSetFrame(Register, Offset);
1047
Charles Davis440596f2011-05-19 17:46:39 +00001048 OS << "\t.seh_setframe " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001049 EmitEOL();
1050}
1051
Charles Davisc3b16282011-05-19 19:35:55 +00001052void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
Charles Daviscde87e22011-05-20 18:19:22 +00001053 MCStreamer::EmitWin64EHAllocStack(Size);
1054
Charles Davis440596f2011-05-19 17:46:39 +00001055 OS << "\t.seh_stackalloc " << Size;
Charles Davis0e30f022011-05-18 04:58:05 +00001056 EmitEOL();
1057}
1058
Charles Davisc3b16282011-05-19 19:35:55 +00001059void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001060 MCStreamer::EmitWin64EHSaveReg(Register, Offset);
1061
Charles Davis440596f2011-05-19 17:46:39 +00001062 OS << "\t.seh_savereg " << Register << ", " << Offset;
1063 EmitEOL();
1064}
1065
Charles Davisc3b16282011-05-19 19:35:55 +00001066void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001067 MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
1068
Charles Davis440596f2011-05-19 17:46:39 +00001069 OS << "\t.seh_savexmm " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001070 EmitEOL();
1071}
1072
Charles Davis8b3e5e52011-05-18 22:13:51 +00001073void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
Charles Daviscde87e22011-05-20 18:19:22 +00001074 MCStreamer::EmitWin64EHPushFrame(Code);
1075
Charles Davis440596f2011-05-19 17:46:39 +00001076 OS << "\t.seh_pushframe";
Charles Davis0e30f022011-05-18 04:58:05 +00001077 if (Code)
Charles Davis440596f2011-05-19 17:46:39 +00001078 OS << " @code";
Charles Davis0e30f022011-05-18 04:58:05 +00001079 EmitEOL();
1080}
1081
Charles Davis8b3e5e52011-05-18 22:13:51 +00001082void MCAsmStreamer::EmitWin64EHEndProlog(void) {
Charles Daviscde87e22011-05-20 18:19:22 +00001083 MCStreamer::EmitWin64EHEndProlog();
1084
Charles Davis440596f2011-05-19 17:46:39 +00001085 OS << "\t.seh_endprologue";
Charles Davis0e30f022011-05-18 04:58:05 +00001086 EmitEOL();
1087}
1088
Daniel Dunbar6b716532010-02-09 23:00:14 +00001089void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
1090 raw_ostream &OS = GetCommentOS();
1091 SmallString<256> Code;
1092 SmallVector<MCFixup, 4> Fixups;
1093 raw_svector_ostream VecOS(Code);
1094 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
1095 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +00001096
Daniel Dunbar6b716532010-02-09 23:00:14 +00001097 // If we are showing fixups, create symbolic markers in the encoded
1098 // representation. We do this by making a per-bit map to the fixup item index,
1099 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +00001100 SmallVector<uint8_t, 64> FixupMap;
1101 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001102 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1103 FixupMap[i] = 0;
1104
1105 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1106 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001107 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001108 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1109 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1110 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1111 FixupMap[Index] = 1 + i;
1112 }
1113 }
1114
Evan Cheng5c2eef62011-07-08 22:49:42 +00001115 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001116 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
Daniel Dunbar6b716532010-02-09 23:00:14 +00001117 OS << "encoding: [";
1118 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1119 if (i)
1120 OS << ',';
1121
1122 // See if all bits are the same map entry.
1123 uint8_t MapEntry = FixupMap[i * 8 + 0];
1124 for (unsigned j = 1; j != 8; ++j) {
1125 if (FixupMap[i * 8 + j] == MapEntry)
1126 continue;
1127
1128 MapEntry = uint8_t(~0U);
1129 break;
1130 }
1131
1132 if (MapEntry != uint8_t(~0U)) {
1133 if (MapEntry == 0) {
1134 OS << format("0x%02x", uint8_t(Code[i]));
1135 } else {
Evan Cheng82caf1a2011-01-13 21:45:26 +00001136 if (Code[i]) {
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001137 // FIXME: Some of the 8 bits require fix up.
Evan Cheng82caf1a2011-01-13 21:45:26 +00001138 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1139 << char('A' + MapEntry - 1) << '\'';
1140 } else
1141 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001142 }
1143 } else {
1144 // Otherwise, write out in binary.
1145 OS << "0b";
Jay Foad95c3e482011-06-23 09:09:15 +00001146 for (unsigned j = 8; j--;) {
Daniel Dunbar6b716532010-02-09 23:00:14 +00001147 unsigned Bit = (Code[i] >> j) & 1;
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001148
Chris Lattner3170a3b2010-11-15 05:56:19 +00001149 unsigned FixupBit;
Evan Cheng1be0e272011-07-15 02:09:41 +00001150 if (getContext().getAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +00001151 FixupBit = i * 8 + j;
1152 else
1153 FixupBit = i * 8 + (7-j);
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001154
Jay Foad95c3e482011-06-23 09:09:15 +00001155 if (uint8_t MapEntry = FixupMap[FixupBit]) {
1156 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1157 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001158 } else
1159 OS << Bit;
1160 }
1161 }
1162 }
1163 OS << "]\n";
1164
1165 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1166 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001167 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001168 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +00001169 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +00001170 }
1171}
1172
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +00001173void MCAsmStreamer::EmitFnStart() {
1174 OS << "\t.fnstart";
1175 EmitEOL();
1176}
1177
1178void MCAsmStreamer::EmitFnEnd() {
1179 OS << "\t.fnend";
1180 EmitEOL();
1181}
1182
1183void MCAsmStreamer::EmitCantUnwind() {
1184 OS << "\t.cantunwind";
1185 EmitEOL();
1186}
1187
1188void MCAsmStreamer::EmitHandlerData() {
1189 OS << "\t.handlerdata";
1190 EmitEOL();
1191}
1192
1193void MCAsmStreamer::EmitPersonality(const MCSymbol *Personality) {
1194 OS << "\t.personality " << Personality->getName();
1195 EmitEOL();
1196}
1197
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001198void MCAsmStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001199 OS << "\t.setfp\t";
1200 InstPrinter->printRegName(OS, FpReg);
1201 OS << ", ";
1202 InstPrinter->printRegName(OS, SpReg);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001203 if (Offset)
1204 OS << ", #" << Offset;
1205 EmitEOL();
1206}
1207
1208void MCAsmStreamer::EmitPad(int64_t Offset) {
1209 OS << "\t.pad\t#" << Offset;
1210 EmitEOL();
1211}
1212
1213void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
1214 bool isVector) {
1215 assert(RegList.size() && "RegList should not be empty");
1216 if (isVector)
1217 OS << "\t.vsave\t{";
1218 else
1219 OS << "\t.save\t{";
1220
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001221 InstPrinter->printRegName(OS, RegList[0]);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001222
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001223 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
1224 OS << ", ";
1225 InstPrinter->printRegName(OS, RegList[i]);
1226 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001227
1228 OS << "}";
1229 EmitEOL();
1230}
1231
Daniel Dunbar6b716532010-02-09 23:00:14 +00001232void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +00001233 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Daniel Dunbar6b716532010-02-09 23:00:14 +00001234
1235 // Show the encoding in a comment if we have a code emitter.
1236 if (Emitter)
1237 AddEncodingComment(Inst);
1238
Chris Lattner30d9a642010-02-09 00:54:51 +00001239 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001240 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001241 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001242 GetCommentOS() << "\n";
1243 }
1244
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001245 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001246 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +00001247 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001248 else
1249 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +00001250 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +00001251}
1252
Jim Grosbachbd4ec842010-09-22 18:18:30 +00001253/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +00001254/// the specified string in the output .s file. This capability is
1255/// indicated by the hasRawTextSupport() predicate.
1256void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001257 if (!String.empty() && String.back() == '\n')
1258 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +00001259 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001260 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +00001261}
1262
Daniel Dunbara11af532009-06-24 01:03:06 +00001263void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +00001264 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +00001265 if (getContext().hasDwarfFiles() && !UseLoc)
1266 MCDwarfFileTable::Emit(this);
Rafael Espindola5426a9e2011-05-01 04:49:54 +00001267
Rafael Espindolac25dad82011-05-10 03:14:15 +00001268 if (!UseCFI)
1269 EmitFrames(false);
Daniel Dunbara11af532009-06-24 01:03:06 +00001270}
Chris Lattner86e22112010-01-22 07:29:22 +00001271MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1272 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +00001273 bool isVerboseAsm, bool useLoc,
Bill Wendling916a94b2011-06-17 20:35:21 +00001274 bool useCFI, MCInstPrinter *IP,
Evan Cheng78c10ee2011-07-25 23:24:55 +00001275 MCCodeEmitter *CE, MCAsmBackend *MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +00001276 bool ShowInst) {
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +00001277 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, useCFI,
Evan Cheng78c10ee2011-07-25 23:24:55 +00001278 IP, CE, MAB, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +00001279}