blob: ae9936ce6fc283fa014193be927e339cab9a5929 [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"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000018#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000019#include "llvm/MC/MCSymbol.h"
Chris Lattner4c42a6d2010-03-19 05:48:53 +000020#include "llvm/ADT/OwningPtr.h"
Chris Lattner86e22112010-01-22 07:29:22 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000024#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000025#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000026#include "llvm/Support/FormattedStream.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000027#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000028#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000030#include <cctype>
Daniel Dunbara11af532009-06-24 01:03:06 +000031using namespace llvm;
32
33namespace {
34
Chris Lattner46a947d2009-08-17 04:17:34 +000035class MCAsmStreamer : public MCStreamer {
Chris Lattner86e22112010-01-22 07:29:22 +000036 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000037 const MCAsmInfo &MAI;
Chris Lattner4c42a6d2010-03-19 05:48:53 +000038 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000039 OwningPtr<MCCodeEmitter> Emitter;
Daniel Dunbar745dacc2010-12-16 03:05:59 +000040 OwningPtr<TargetAsmBackend> AsmBackend;
Jim Grosbach00545e12010-09-22 18:16:55 +000041
Chris Lattner86e22112010-01-22 07:29:22 +000042 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000043 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000044
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000045 unsigned IsVerboseAsm : 1;
46 unsigned ShowInst : 1;
Rafael Espindola89b93722010-12-10 07:39:47 +000047 unsigned UseLoc : 1;
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000048 unsigned UseCFI : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000049
Rafael Espindola277abc82011-04-30 16:34:57 +000050 enum EHSymbolFlags { EHGlobal = 1,
51 EHWeakDefinition = 1 << 1,
52 EHPrivateExtern = 1 << 2 };
53 DenseMap<const MCSymbol*, unsigned> FlagMap;
54
Rafael Espindola5d4918d2010-12-04 03:21:47 +000055 bool needsSet(const MCExpr *Value);
56
Rafael Espindola6e032942011-05-30 20:20:15 +000057 void EmitRegisterName(int64_t Register);
58
Chris Lattner46a947d2009-08-17 04:17:34 +000059public:
Chris Lattner86e22112010-01-22 07:29:22 +000060 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000061 bool isVerboseAsm, bool useLoc, bool useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000062 MCInstPrinter *printer, MCCodeEmitter *emitter,
63 TargetAsmBackend *asmbackend,
64 bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000065 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
Daniel Dunbar745dacc2010-12-16 03:05:59 +000066 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
67 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000068 ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000069 if (InstPrinter && IsVerboseAsm)
70 InstPrinter->setCommentStream(CommentStream);
71 }
Chris Lattner46a947d2009-08-17 04:17:34 +000072 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000073
Chris Lattner86e22112010-01-22 07:29:22 +000074 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000075 // If we don't have any comments, just emit a \n.
76 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000077 OS << '\n';
78 return;
79 }
80 EmitCommentsAndEOL();
81 }
82 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000083
84 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
85 /// all.
86 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000087
Chris Lattner91bead72010-04-03 21:35:55 +000088 /// hasRawTextSupport - We support EmitRawText.
89 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000090
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000091 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000092 /// file if applicable as a QoI issue to make the output of the compiler
93 /// more readable. This only affects the MCAsmStreamer, and only when
94 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000095 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000096
97 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
98 virtual void AddEncodingComment(const MCInst &Inst);
99
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000100 /// GetCommentOS - Return a raw_ostream that comments can be written to.
101 /// Unlike AddComment, you are required to terminate comments with \n if you
102 /// use this method.
103 virtual raw_ostream &GetCommentOS() {
104 if (!IsVerboseAsm)
105 return nulls(); // Discard comments unless in verbose asm mode.
106 return CommentStream;
107 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000108
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000109 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
110 virtual void AddBlankLine() {
111 EmitEOL();
112 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000113
Chris Lattner46a947d2009-08-17 04:17:34 +0000114 /// @name MCStreamer Interface
115 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000116
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000117 virtual void ChangeSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118
Rafael Espindolad80781b2010-09-15 21:48:40 +0000119 virtual void InitSections() {
120 // FIXME, this is MachO specific, but the testsuite
121 // expects this.
122 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
123 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
124 0, SectionKind::getText()));
125 }
126
Chris Lattner46a947d2009-08-17 04:17:34 +0000127 virtual void EmitLabel(MCSymbol *Symbol);
Rafael Espindola277abc82011-04-30 16:34:57 +0000128 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
129 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000130 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000131 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000132
Daniel Dunbar821e3332009-08-31 08:09:28 +0000133 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000134 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000135 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
136 const MCSymbol *LastLabel,
137 const MCSymbol *Label);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000138 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
139 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000140
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000141 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000142
Chris Lattner46a947d2009-08-17 04:17:34 +0000143 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000144 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
145 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
146 virtual void EmitCOFFSymbolType(int Type);
147 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000148 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000149 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000150 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000151
Chris Lattner9eb158d2010-01-23 07:47:02 +0000152 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
153 ///
154 /// @param Symbol - The common symbol to emit.
155 /// @param Size - The size of the common symbol.
156 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000157
Daniel Dunbar8751b942009-08-28 05:48:22 +0000158 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000159 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000160
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000161 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
162 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000163
Chris Lattneraaec2052010-01-19 19:46:13 +0000164 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000165
Rafael Espindola89b93722010-12-10 07:39:47 +0000166 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000167 unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000168 virtual void EmitIntValue(uint64_t Value, unsigned Size,
169 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000170
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000171 virtual void EmitULEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000172
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000173 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000174
Chris Lattner718fb592010-01-25 21:28:50 +0000175 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000176
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000177
Chris Lattneraaec2052010-01-19 19:46:13 +0000178 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
179 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000180
Chris Lattner46a947d2009-08-17 04:17:34 +0000181 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
182 unsigned ValueSize = 1,
183 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000184
Kevin Enderby6e720482010-02-23 18:26:34 +0000185 virtual void EmitCodeAlignment(unsigned ByteAlignment,
186 unsigned MaxBytesToEmit = 0);
187
Daniel Dunbar821e3332009-08-31 08:09:28 +0000188 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000189 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000190
Chris Lattnera6594fc2010-01-25 18:58:59 +0000191 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000192 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
193 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
194 unsigned Column, unsigned Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000195 unsigned Isa, unsigned Discriminator,
196 StringRef FileName);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000197
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000198 virtual void EmitCFISections(bool EH, bool Debug);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000199 virtual void EmitCFIStartProc();
200 virtual void EmitCFIEndProc();
201 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
202 virtual void EmitCFIDefCfaOffset(int64_t Offset);
203 virtual void EmitCFIDefCfaRegister(int64_t Register);
204 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
205 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
206 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
207 virtual void EmitCFIRememberState();
208 virtual void EmitCFIRestoreState();
209 virtual void EmitCFISameValue(int64_t Register);
210 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
211 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000212
Charles Davisfbc539f2011-05-22 21:12:15 +0000213 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
Charles Davis0e30f022011-05-18 04:58:05 +0000214 virtual void EmitWin64EHEndProc();
Charles Davisf0709012011-05-18 20:54:10 +0000215 virtual void EmitWin64EHStartChained();
216 virtual void EmitWin64EHEndChained();
Charles Davis440596f2011-05-19 17:46:39 +0000217 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
218 bool Except);
219 virtual void EmitWin64EHHandlerData();
Charles Davisc3b16282011-05-19 19:35:55 +0000220 virtual void EmitWin64EHPushReg(unsigned Register);
221 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
222 virtual void EmitWin64EHAllocStack(unsigned Size);
223 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
224 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
Charles Davis0e30f022011-05-18 04:58:05 +0000225 virtual void EmitWin64EHPushFrame(bool Code);
226 virtual void EmitWin64EHEndProlog();
227
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000228 virtual void EmitFnStart();
229 virtual void EmitFnEnd();
230 virtual void EmitCantUnwind();
231 virtual void EmitPersonality(const MCSymbol *Personality);
232 virtual void EmitHandlerData();
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000233 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
234 virtual void EmitPad(int64_t Offset);
235 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool);
236
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000237
Chris Lattnera6594fc2010-01-25 18:58:59 +0000238 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000239
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000240 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000241 /// the specified string in the output .s file. This capability is
242 /// indicated by the hasRawTextSupport() predicate.
243 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000244
Chris Lattner46a947d2009-08-17 04:17:34 +0000245 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000246
Chris Lattner46a947d2009-08-17 04:17:34 +0000247 /// @}
248};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000249
Chris Lattner46a947d2009-08-17 04:17:34 +0000250} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000251
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000252/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000253/// file if applicable as a QoI issue to make the output of the compiler
254/// more readable. This only affects the MCAsmStreamer, and only when
255/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000256void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000257 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000258
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000259 // Make sure that CommentStream is flushed.
260 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000261
Chris Lattner86e22112010-01-22 07:29:22 +0000262 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000263 // Each comment goes on its own line.
264 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000265
Chris Lattner14ca1772010-01-22 21:16:10 +0000266 // Tell the comment stream that the vector changed underneath it.
267 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000268}
269
270void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000271 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
272 OS << '\n';
273 return;
274 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000275
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000276 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000277 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000278
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000279 assert(Comments.back() == '\n' &&
280 "Comment array not newline terminated");
281 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000282 // Emit a line of comments.
283 OS.PadToColumn(MAI.getCommentColumn());
284 size_t Position = Comments.find('\n');
285 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000286
Chris Lattner86e22112010-01-22 07:29:22 +0000287 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000288 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000289
Chris Lattner14ca1772010-01-22 21:16:10 +0000290 CommentToEmit.clear();
291 // Tell the comment stream that the vector changed underneath it.
292 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000293}
294
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000295static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
296 assert(Bytes && "Invalid size!");
297 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
298}
299
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000300void MCAsmStreamer::ChangeSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000301 assert(Section && "Cannot switch to a null section!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000302 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000303}
304
Rafael Espindola277abc82011-04-30 16:34:57 +0000305void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
306 MCSymbol *EHSymbol) {
307 if (UseCFI)
308 return;
309
310 unsigned Flags = FlagMap.lookup(Symbol);
311
312 if (Flags & EHGlobal)
313 EmitSymbolAttribute(EHSymbol, MCSA_Global);
314 if (Flags & EHWeakDefinition)
315 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
316 if (Flags & EHPrivateExtern)
317 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
318}
319
Daniel Dunbara11af532009-06-24 01:03:06 +0000320void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000321 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaed708f92011-04-27 15:21:19 +0000322 MCStreamer::EmitLabel(Symbol);
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000323
Chris Lattnere07b75e2010-09-22 22:19:53 +0000324 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000325 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000326}
327
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000328void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000329 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000330 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000331 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000332 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000333 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000334 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000335 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000336 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000337}
338
Jim Grosbachce792992010-11-05 22:08:08 +0000339void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
340 // This needs to emit to a temporary string to get properly quoted
341 // MCSymbols when they have spaces in them.
342 OS << "\t.thumb_func";
Rafael Espindola64695402011-05-16 16:17:21 +0000343 // Only Mach-O hasSubsectionsViaSymbols()
344 if (MAI.hasSubsectionsViaSymbols())
Jim Grosbachce792992010-11-05 22:08:08 +0000345 OS << '\t' << *Func;
346 EmitEOL();
347}
348
Daniel Dunbar821e3332009-08-31 08:09:28 +0000349void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000350 OS << *Symbol << " = " << *Value;
351 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000352
353 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000354 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000355}
356
Rafael Espindola484291c2010-11-01 14:28:48 +0000357void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
358 OS << ".weakref " << *Alias << ", " << *Symbol;
359 EmitEOL();
360}
361
Rafael Espindola32a006e2010-12-03 00:55:40 +0000362void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
363 const MCSymbol *LastLabel,
364 const MCSymbol *Label) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000365 EmitDwarfSetLineAddr(LineDelta, Label,
366 getContext().getTargetAsmInfo().getPointerSize());
Rafael Espindola32a006e2010-12-03 00:55:40 +0000367}
368
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000369void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
370 const MCSymbol *Label) {
371 EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
372 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
Rafael Espindolaa6f26782011-05-19 21:40:34 +0000373 AddrDelta = ForceExpAbs(AddrDelta);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000374 EmitValue(AddrDelta, 4);
375}
376
377
Daniel Dunbarfffff912009-10-16 01:34:54 +0000378void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000379 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000380 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000381 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000382 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
383 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
384 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
385 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
386 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
387 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000388 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000389 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000390 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000391 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
392 switch (Attribute) {
393 default: assert(0 && "Unknown ELF .type");
394 case MCSA_ELF_TypeFunction: OS << "function"; break;
395 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
396 case MCSA_ELF_TypeObject: OS << "object"; break;
397 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
398 case MCSA_ELF_TypeCommon: OS << "common"; break;
399 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000400 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000401 }
402 EmitEOL();
403 return;
404 case MCSA_Global: // .globl/.global
405 OS << MAI.getGlobalDirective();
Rafael Espindola277abc82011-04-30 16:34:57 +0000406 FlagMap[Symbol] |= EHGlobal;
Chris Lattnered0ab152010-01-25 18:30:45 +0000407 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000408 case MCSA_Hidden: OS << "\t.hidden\t"; break;
409 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
410 case MCSA_Internal: OS << "\t.internal\t"; break;
411 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
412 case MCSA_Local: OS << "\t.local\t"; break;
413 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000414 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000415 case MCSA_PrivateExtern:
416 OS << "\t.private_extern\t";
417 FlagMap[Symbol] |= EHPrivateExtern;
418 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000419 case MCSA_Protected: OS << "\t.protected\t"; break;
420 case MCSA_Reference: OS << "\t.reference\t"; break;
421 case MCSA_Weak: OS << "\t.weak\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000422 case MCSA_WeakDefinition:
423 OS << "\t.weak_definition\t";
424 FlagMap[Symbol] |= EHWeakDefinition;
425 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000426 // .weak_reference
427 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000428 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000429 }
430
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000431 OS << *Symbol;
432 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000433}
434
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000435void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000436 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
437 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000438}
439
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000440void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
441 OS << "\t.def\t " << *Symbol << ';';
442 EmitEOL();
443}
444
445void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
446 OS << "\t.scl\t" << StorageClass << ';';
447 EmitEOL();
448}
449
450void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
451 OS << "\t.type\t" << Type << ';';
452 EmitEOL();
453}
454
455void MCAsmStreamer::EndCOFFSymbolDef() {
456 OS << "\t.endef";
457 EmitEOL();
458}
459
Chris Lattner99328ad2010-01-25 07:52:13 +0000460void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
461 assert(MAI.hasDotTypeDotSizeDirective());
462 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
463}
464
Chris Lattner9eb158d2010-01-23 07:47:02 +0000465void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000466 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000467 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000468 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000469 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000470 OS << ',' << ByteAlignment;
471 else
472 OS << ',' << Log2_32(ByteAlignment);
473 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000474 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000475}
476
Chris Lattner9eb158d2010-01-23 07:47:02 +0000477/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
478///
479/// @param Symbol - The common symbol to emit.
480/// @param Size - The size of the common symbol.
481void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
482 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
483 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
484 EmitEOL();
485}
486
Daniel Dunbar8751b942009-08-28 05:48:22 +0000487void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000488 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000489 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000490 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000491
Chris Lattner93b6db32009-08-08 23:39:42 +0000492 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000493 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000494 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000495
Chris Lattner9be3fee2009-07-10 22:20:30 +0000496 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000497 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000498 if (ByteAlignment != 0)
499 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000500 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000501 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000502}
503
Eric Christopherd04d98d2010-05-17 02:13:02 +0000504// .tbss sym, size, align
505// This depends that the symbol has already been mangled from the original,
506// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000507void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
508 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000509 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000510 // Instead of using the Section we'll just use the shortcut.
511 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000512 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000513
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000514 // Output align if we have it. We default to 1 so don't bother printing
515 // that.
516 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000517
Eric Christopher482eba02010-05-14 01:50:28 +0000518 EmitEOL();
519}
520
Chris Lattner12e555c2010-01-23 00:15:00 +0000521static inline char toOctal(int X) { return (X&7)+'0'; }
522
Chris Lattnera6594fc2010-01-25 18:58:59 +0000523static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
524 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000525
Chris Lattnera6594fc2010-01-25 18:58:59 +0000526 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
527 unsigned char C = Data[i];
528 if (C == '"' || C == '\\') {
529 OS << '\\' << (char)C;
530 continue;
531 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000532
Chris Lattnera6594fc2010-01-25 18:58:59 +0000533 if (isprint((unsigned char)C)) {
534 OS << (char)C;
535 continue;
536 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000537
Chris Lattnera6594fc2010-01-25 18:58:59 +0000538 switch (C) {
539 case '\b': OS << "\\b"; break;
540 case '\f': OS << "\\f"; break;
541 case '\n': OS << "\\n"; break;
542 case '\r': OS << "\\r"; break;
543 case '\t': OS << "\\t"; break;
544 default:
545 OS << '\\';
546 OS << toOctal(C >> 6);
547 OS << toOctal(C >> 3);
548 OS << toOctal(C >> 0);
549 break;
550 }
551 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000552
Chris Lattnera6594fc2010-01-25 18:58:59 +0000553 OS << '"';
554}
555
556
Chris Lattneraaec2052010-01-19 19:46:13 +0000557void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000558 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000559 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000560
Chris Lattner12e555c2010-01-23 00:15:00 +0000561 if (Data.size() == 1) {
562 OS << MAI.getData8bitsDirective(AddrSpace);
563 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000564 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000565 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000566 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000567
568 // If the data ends with 0 and the target supports .asciz, use it, otherwise
569 // use .ascii
570 if (MAI.getAscizDirective() && Data.back() == 0) {
571 OS << MAI.getAscizDirective();
572 Data = Data.substr(0, Data.size()-1);
573 } else {
574 OS << MAI.getAsciiDirective();
575 }
576
Chris Lattnera6594fc2010-01-25 18:58:59 +0000577 OS << ' ';
578 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000579 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000580}
581
Rafael Espindola2df042c2010-12-03 02:54:21 +0000582void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
583 unsigned AddrSpace) {
584 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
585}
586
Rafael Espindola89b93722010-12-10 07:39:47 +0000587void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000588 unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000589 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000590 const char *Directive = 0;
591 switch (Size) {
592 default: break;
593 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
594 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
595 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000596 case 8:
597 Directive = MAI.getData64bitsDirective(AddrSpace);
598 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
599 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000600 int64_t IntValue;
601 if (!Value->EvaluateAsAbsolute(IntValue))
602 report_fatal_error("Don't know how to emit this value.");
Rafael Espindola89b93722010-12-10 07:39:47 +0000603 if (getContext().getTargetAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000604 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
605 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000606 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000607 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
608 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000609 }
610 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000611 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000612
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000613 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000614 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000615 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000616}
617
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000618void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000619 int64_t IntValue;
620 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000621 EmitULEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000622 return;
623 }
Rafael Espindola73873452010-11-04 18:17:08 +0000624 assert(MAI.hasLEB128() && "Cannot print a .uleb");
625 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000626 EmitEOL();
627}
628
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000629void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000630 int64_t IntValue;
631 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000632 EmitSLEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000633 return;
634 }
Rafael Espindola73873452010-11-04 18:17:08 +0000635 assert(MAI.hasLEB128() && "Cannot print a .sleb");
636 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000637 EmitEOL();
638}
639
Chris Lattner718fb592010-01-25 21:28:50 +0000640void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
641 assert(MAI.getGPRel32Directive() != 0);
642 OS << MAI.getGPRel32Directive() << *Value;
643 EmitEOL();
644}
645
646
Chris Lattner6113b3d2010-01-19 18:52:28 +0000647/// EmitFill - Emit NumBytes bytes worth of the value specified by
648/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000649void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
650 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000651 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000652
Chris Lattneraaec2052010-01-19 19:46:13 +0000653 if (AddrSpace == 0)
654 if (const char *ZeroDirective = MAI.getZeroDirective()) {
655 OS << ZeroDirective << NumBytes;
656 if (FillValue != 0)
657 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000658 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000659 return;
660 }
661
662 // Emit a byte at a time.
663 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000664}
665
Daniel Dunbar84a29262009-06-24 19:25:34 +0000666void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
667 unsigned ValueSize,
668 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000669 // Some assemblers don't support non-power of two alignments, so we always
670 // emit alignments as a power of two if possible.
671 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000672 switch (ValueSize) {
673 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000674 case 1: OS << MAI.getAlignDirective(); break;
675 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000676 case 2: OS << ".p2alignw "; break;
677 case 4: OS << ".p2alignl "; break;
678 case 8: llvm_unreachable("Unsupported alignment size!");
679 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000680
Chris Lattner33adcfb2009-08-22 21:43:10 +0000681 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000682 OS << ByteAlignment;
683 else
684 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000685
Chris Lattner663c2d22009-08-19 06:12:02 +0000686 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000687 OS << ", 0x";
688 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000689
Jim Grosbach00545e12010-09-22 18:16:55 +0000690 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000691 OS << ", " << MaxBytesToEmit;
692 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000693 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000694 return;
695 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000696
Chris Lattner663c2d22009-08-19 06:12:02 +0000697 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000698 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000699 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000700 default: llvm_unreachable("Invalid size for machine code value!");
701 case 1: OS << ".balign"; break;
702 case 2: OS << ".balignw"; break;
703 case 4: OS << ".balignl"; break;
704 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000705 }
706
Chris Lattner663c2d22009-08-19 06:12:02 +0000707 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000708 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000709 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000710 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000711 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000712}
713
Kevin Enderby6e720482010-02-23 18:26:34 +0000714void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
715 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000716 // Emit with a text fill value.
717 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
718 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000719}
720
Daniel Dunbar821e3332009-08-31 08:09:28 +0000721void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000722 unsigned char Value) {
723 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000724 OS << ".org " << *Offset << ", " << (unsigned) Value;
725 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000726}
727
Chris Lattnera6594fc2010-01-25 18:58:59 +0000728
729void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
730 assert(MAI.hasSingleParameterDotFile());
731 OS << "\t.file\t";
732 PrintQuotedString(Filename, OS);
733 EmitEOL();
734}
735
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000736bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola89b93722010-12-10 07:39:47 +0000737 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000738 OS << "\t.file\t" << FileNo << ' ';
739 PrintQuotedString(Filename, OS);
740 EmitEOL();
741 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000742 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
743}
744
745void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
746 unsigned Column, unsigned Flags,
747 unsigned Isa,
Devang Patel3f3bf932011-04-18 20:26:49 +0000748 unsigned Discriminator,
749 StringRef FileName) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000750 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000751 Isa, Discriminator, FileName);
Rafael Espindola89b93722010-12-10 07:39:47 +0000752 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000753 return;
754
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000755 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
756 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
757 OS << " basic_block";
758 if (Flags & DWARF2_FLAG_PROLOGUE_END)
759 OS << " prologue_end";
760 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
761 OS << " epilogue_begin";
762
763 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
764 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
765 OS << " is_stmt ";
766
767 if (Flags & DWARF2_FLAG_IS_STMT)
768 OS << "1";
769 else
770 OS << "0";
771 }
772
773 if (Isa)
774 OS << "isa " << Isa;
775 if (Discriminator)
776 OS << "discriminator " << Discriminator;
Devang Patel3f3bf932011-04-18 20:26:49 +0000777
778 if (IsVerboseAsm) {
779 OS.PadToColumn(MAI.getCommentColumn());
780 OS << MAI.getCommentString() << ' ' << FileName << ':'
781 << Line << ':' << Column;
782 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000783 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000784}
785
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000786void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
787 MCStreamer::EmitCFISections(EH, Debug);
788
789 if (!UseCFI)
790 return;
791
792 OS << "\t.cfi_sections ";
793 if (EH) {
794 OS << ".eh_frame";
795 if (Debug)
796 OS << ", .debug_frame";
797 } else if (Debug) {
798 OS << ".debug_frame";
799 }
800
801 EmitEOL();
802}
803
Rafael Espindola066c2f42011-04-12 23:59:07 +0000804void MCAsmStreamer::EmitCFIStartProc() {
805 MCStreamer::EmitCFIStartProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000806
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000807 if (!UseCFI)
808 return;
809
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000810 OS << "\t.cfi_startproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000811 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000812}
813
Rafael Espindola066c2f42011-04-12 23:59:07 +0000814void MCAsmStreamer::EmitCFIEndProc() {
815 MCStreamer::EmitCFIEndProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000816
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000817 if (!UseCFI)
818 return;
819
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000820 OS << "\t.cfi_endproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000821 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000822}
823
Rafael Espindola6e032942011-05-30 20:20:15 +0000824void MCAsmStreamer::EmitRegisterName(int64_t Register) {
825 if (InstPrinter) {
826 const TargetAsmInfo &asmInfo = getContext().getTargetAsmInfo();
827 unsigned LLVMRegister = asmInfo.getLLVMRegNum(Register, true);
828 OS << '%' << InstPrinter->getRegName(LLVMRegister);
829 } else {
830 OS << Register;
831 }
832}
833
Rafael Espindola066c2f42011-04-12 23:59:07 +0000834void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindola7f46b082011-04-29 21:41:06 +0000835 MCStreamer::EmitCFIDefCfa(Register, Offset);
836
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000837 if (!UseCFI)
838 return;
839
Rafael Espindola6e032942011-05-30 20:20:15 +0000840 OS << "\t.cfi_def_cfa ";
841 EmitRegisterName(Register);
842 OS << ", " << Offset;
Rafael Espindola7f46b082011-04-29 21:41:06 +0000843 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000844}
845
846void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
847 MCStreamer::EmitCFIDefCfaOffset(Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000848
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000849 if (!UseCFI)
850 return;
851
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000852 OS << "\t.cfi_def_cfa_offset " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000853 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000854}
855
Rafael Espindola066c2f42011-04-12 23:59:07 +0000856void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
857 MCStreamer::EmitCFIDefCfaRegister(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000858
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000859 if (!UseCFI)
860 return;
861
Rafael Espindola6e032942011-05-30 20:20:15 +0000862 OS << "\t.cfi_def_cfa_register ";
863 EmitRegisterName(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000864 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000865}
866
Rafael Espindola066c2f42011-04-12 23:59:07 +0000867void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
868 this->MCStreamer::EmitCFIOffset(Register, Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000869
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000870 if (!UseCFI)
871 return;
872
Rafael Espindola6e032942011-05-30 20:20:15 +0000873 OS << "\t.cfi_offset ";
874 EmitRegisterName(Register);
875 OS << ", " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000876 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000877}
878
Rafael Espindola066c2f42011-04-12 23:59:07 +0000879void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000880 unsigned Encoding) {
Rafael Espindola066c2f42011-04-12 23:59:07 +0000881 MCStreamer::EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000882
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000883 if (!UseCFI)
884 return;
885
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000886 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000887 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000888}
889
Rafael Espindola066c2f42011-04-12 23:59:07 +0000890void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
891 MCStreamer::EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000892
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000893 if (!UseCFI)
894 return;
895
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000896 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000897 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000898}
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000899
Rafael Espindola066c2f42011-04-12 23:59:07 +0000900void MCAsmStreamer::EmitCFIRememberState() {
901 MCStreamer::EmitCFIRememberState();
902
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000903 if (!UseCFI)
904 return;
905
Rafael Espindola066c2f42011-04-12 23:59:07 +0000906 OS << "\t.cfi_remember_state";
907 EmitEOL();
908}
909
910void MCAsmStreamer::EmitCFIRestoreState() {
911 MCStreamer::EmitCFIRestoreState();
912
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000913 if (!UseCFI)
914 return;
915
Rafael Espindola066c2f42011-04-12 23:59:07 +0000916 OS << "\t.cfi_restore_state";
917 EmitEOL();
918}
919
920void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
921 MCStreamer::EmitCFISameValue(Register);
922
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000923 if (!UseCFI)
924 return;
925
Rafael Espindola6e032942011-05-30 20:20:15 +0000926 OS << "\t.cfi_same_value ";
927 EmitRegisterName(Register);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000928 EmitEOL();
929}
930
931void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
932 MCStreamer::EmitCFIRelOffset(Register, Offset);
933
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000934 if (!UseCFI)
935 return;
936
Rafael Espindola6e032942011-05-30 20:20:15 +0000937 OS << "\t.cfi_rel_offset ";
938 EmitRegisterName(Register);
939 OS << ", " << Offset;
Rafael Espindola066c2f42011-04-12 23:59:07 +0000940 EmitEOL();
941}
942
943void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
944 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
945
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000946 if (!UseCFI)
947 return;
948
Rafael Espindola066c2f42011-04-12 23:59:07 +0000949 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
950 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000951}
952
Charles Davisfbc539f2011-05-22 21:12:15 +0000953void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
Charles Daviscde87e22011-05-20 18:19:22 +0000954 MCStreamer::EmitWin64EHStartProc(Symbol);
955
Charles Davis440596f2011-05-19 17:46:39 +0000956 OS << ".seh_proc " << *Symbol;
Charles Davis0e30f022011-05-18 04:58:05 +0000957 EmitEOL();
958}
959
Charles Davis8b3e5e52011-05-18 22:13:51 +0000960void MCAsmStreamer::EmitWin64EHEndProc() {
Charles Daviscde87e22011-05-20 18:19:22 +0000961 MCStreamer::EmitWin64EHEndProc();
962
Charles Davis440596f2011-05-19 17:46:39 +0000963 OS << "\t.seh_endproc";
Charles Davis0e30f022011-05-18 04:58:05 +0000964 EmitEOL();
965}
966
Charles Davis8b3e5e52011-05-18 22:13:51 +0000967void MCAsmStreamer::EmitWin64EHStartChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000968 MCStreamer::EmitWin64EHStartChained();
969
Charles Davis440596f2011-05-19 17:46:39 +0000970 OS << "\t.seh_startchained";
Charles Davisf0709012011-05-18 20:54:10 +0000971 EmitEOL();
972}
973
Charles Davis8b3e5e52011-05-18 22:13:51 +0000974void MCAsmStreamer::EmitWin64EHEndChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000975 MCStreamer::EmitWin64EHEndChained();
976
Charles Davis440596f2011-05-19 17:46:39 +0000977 OS << "\t.seh_endchained";
Charles Davisf0709012011-05-18 20:54:10 +0000978 EmitEOL();
979}
980
Charles Davis440596f2011-05-19 17:46:39 +0000981void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
982 bool Except) {
Charles Daviscde87e22011-05-20 18:19:22 +0000983 MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
984
Charles Davis440596f2011-05-19 17:46:39 +0000985 OS << "\t.seh_handler " << *Sym;
986 if (Unwind)
987 OS << ", @unwind";
988 if (Except)
989 OS << ", @except";
Charles Davisf0709012011-05-18 20:54:10 +0000990 EmitEOL();
991}
992
Charles Davis440596f2011-05-19 17:46:39 +0000993void MCAsmStreamer::EmitWin64EHHandlerData() {
Charles Daviscde87e22011-05-20 18:19:22 +0000994 MCStreamer::EmitWin64EHHandlerData();
995
Charles Davis410ef2b2011-05-25 21:43:45 +0000996 // Switch sections. Don't call SwitchSection directly, because that will
997 // cause the section switch to be visible in the emitted assembly.
998 // We only do this so the section switch that terminates the handler
999 // data block is visible.
Charles Davis7b06b732011-05-27 19:09:24 +00001000 MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
1001 StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
Charles Davis410ef2b2011-05-25 21:43:45 +00001002 const MCSection *xdataSect =
Charles Davis7b06b732011-05-27 19:09:24 +00001003 getContext().getTargetAsmInfo().getWin64EHTableSection(suffix);
Charles Davis410ef2b2011-05-25 21:43:45 +00001004 if (xdataSect)
1005 SwitchSectionNoChange(xdataSect);
1006
Charles Davis440596f2011-05-19 17:46:39 +00001007 OS << "\t.seh_handlerdata";
Charles Davisf0709012011-05-18 20:54:10 +00001008 EmitEOL();
1009}
1010
Charles Davisc3b16282011-05-19 19:35:55 +00001011void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
Charles Daviscde87e22011-05-20 18:19:22 +00001012 MCStreamer::EmitWin64EHPushReg(Register);
1013
Charles Davis440596f2011-05-19 17:46:39 +00001014 OS << "\t.seh_pushreg " << Register;
Charles Davis0e30f022011-05-18 04:58:05 +00001015 EmitEOL();
1016}
1017
Charles Davisc3b16282011-05-19 19:35:55 +00001018void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001019 MCStreamer::EmitWin64EHSetFrame(Register, Offset);
1020
Charles Davis440596f2011-05-19 17:46:39 +00001021 OS << "\t.seh_setframe " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001022 EmitEOL();
1023}
1024
Charles Davisc3b16282011-05-19 19:35:55 +00001025void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
Charles Daviscde87e22011-05-20 18:19:22 +00001026 MCStreamer::EmitWin64EHAllocStack(Size);
1027
Charles Davis440596f2011-05-19 17:46:39 +00001028 OS << "\t.seh_stackalloc " << Size;
Charles Davis0e30f022011-05-18 04:58:05 +00001029 EmitEOL();
1030}
1031
Charles Davisc3b16282011-05-19 19:35:55 +00001032void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001033 MCStreamer::EmitWin64EHSaveReg(Register, Offset);
1034
Charles Davis440596f2011-05-19 17:46:39 +00001035 OS << "\t.seh_savereg " << Register << ", " << Offset;
1036 EmitEOL();
1037}
1038
Charles Davisc3b16282011-05-19 19:35:55 +00001039void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001040 MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
1041
Charles Davis440596f2011-05-19 17:46:39 +00001042 OS << "\t.seh_savexmm " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001043 EmitEOL();
1044}
1045
Charles Davis8b3e5e52011-05-18 22:13:51 +00001046void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
Charles Daviscde87e22011-05-20 18:19:22 +00001047 MCStreamer::EmitWin64EHPushFrame(Code);
1048
Charles Davis440596f2011-05-19 17:46:39 +00001049 OS << "\t.seh_pushframe";
Charles Davis0e30f022011-05-18 04:58:05 +00001050 if (Code)
Charles Davis440596f2011-05-19 17:46:39 +00001051 OS << " @code";
Charles Davis0e30f022011-05-18 04:58:05 +00001052 EmitEOL();
1053}
1054
Charles Davis8b3e5e52011-05-18 22:13:51 +00001055void MCAsmStreamer::EmitWin64EHEndProlog(void) {
Charles Daviscde87e22011-05-20 18:19:22 +00001056 MCStreamer::EmitWin64EHEndProlog();
1057
Charles Davis440596f2011-05-19 17:46:39 +00001058 OS << "\t.seh_endprologue";
Charles Davis0e30f022011-05-18 04:58:05 +00001059 EmitEOL();
1060}
1061
Daniel Dunbar6b716532010-02-09 23:00:14 +00001062void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
1063 raw_ostream &OS = GetCommentOS();
1064 SmallString<256> Code;
1065 SmallVector<MCFixup, 4> Fixups;
1066 raw_svector_ostream VecOS(Code);
1067 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
1068 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +00001069
Daniel Dunbar6b716532010-02-09 23:00:14 +00001070 // If we are showing fixups, create symbolic markers in the encoded
1071 // representation. We do this by making a per-bit map to the fixup item index,
1072 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +00001073 SmallVector<uint8_t, 64> FixupMap;
1074 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001075 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1076 FixupMap[i] = 0;
1077
1078 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1079 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001080 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001081 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1082 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1083 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1084 FixupMap[Index] = 1 + i;
1085 }
1086 }
1087
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001088 // FIXME: Node the fixup comments for Thumb2 are completely bogus since the
1089 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
Daniel Dunbar6b716532010-02-09 23:00:14 +00001090 OS << "encoding: [";
1091 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1092 if (i)
1093 OS << ',';
1094
1095 // See if all bits are the same map entry.
1096 uint8_t MapEntry = FixupMap[i * 8 + 0];
1097 for (unsigned j = 1; j != 8; ++j) {
1098 if (FixupMap[i * 8 + j] == MapEntry)
1099 continue;
1100
1101 MapEntry = uint8_t(~0U);
1102 break;
1103 }
1104
1105 if (MapEntry != uint8_t(~0U)) {
1106 if (MapEntry == 0) {
1107 OS << format("0x%02x", uint8_t(Code[i]));
1108 } else {
Evan Cheng82caf1a2011-01-13 21:45:26 +00001109 if (Code[i]) {
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001110 // FIXME: Some of the 8 bits require fix up.
Evan Cheng82caf1a2011-01-13 21:45:26 +00001111 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1112 << char('A' + MapEntry - 1) << '\'';
1113 } else
1114 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001115 }
1116 } else {
1117 // Otherwise, write out in binary.
1118 OS << "0b";
1119 for (unsigned j = 8; j--;) {
1120 unsigned Bit = (Code[i] >> j) & 1;
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001121
Chris Lattner3170a3b2010-11-15 05:56:19 +00001122 unsigned FixupBit;
Rafael Espindola89b93722010-12-10 07:39:47 +00001123 if (getContext().getTargetAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +00001124 FixupBit = i * 8 + j;
1125 else
1126 FixupBit = i * 8 + (7-j);
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001127
Chris Lattner3170a3b2010-11-15 05:56:19 +00001128 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +00001129 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1130 OS << char('A' + MapEntry - 1);
1131 } else
1132 OS << Bit;
1133 }
1134 }
1135 }
1136 OS << "]\n";
1137
1138 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1139 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001140 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001141 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +00001142 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +00001143 }
1144}
1145
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +00001146void MCAsmStreamer::EmitFnStart() {
1147 OS << "\t.fnstart";
1148 EmitEOL();
1149}
1150
1151void MCAsmStreamer::EmitFnEnd() {
1152 OS << "\t.fnend";
1153 EmitEOL();
1154}
1155
1156void MCAsmStreamer::EmitCantUnwind() {
1157 OS << "\t.cantunwind";
1158 EmitEOL();
1159}
1160
1161void MCAsmStreamer::EmitHandlerData() {
1162 OS << "\t.handlerdata";
1163 EmitEOL();
1164}
1165
1166void MCAsmStreamer::EmitPersonality(const MCSymbol *Personality) {
1167 OS << "\t.personality " << Personality->getName();
1168 EmitEOL();
1169}
1170
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001171void MCAsmStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
1172 OS << "\t.setfp\t" << InstPrinter->getRegName(FpReg)
1173 << ", " << InstPrinter->getRegName(SpReg);
1174 if (Offset)
1175 OS << ", #" << Offset;
1176 EmitEOL();
1177}
1178
1179void MCAsmStreamer::EmitPad(int64_t Offset) {
1180 OS << "\t.pad\t#" << Offset;
1181 EmitEOL();
1182}
1183
1184void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
1185 bool isVector) {
1186 assert(RegList.size() && "RegList should not be empty");
1187 if (isVector)
1188 OS << "\t.vsave\t{";
1189 else
1190 OS << "\t.save\t{";
1191
1192 OS << InstPrinter->getRegName(RegList[0]);
1193
1194 for (unsigned i = 1, e = RegList.size(); i != e; ++i)
1195 OS << ", " << InstPrinter->getRegName(RegList[i]);
1196
1197 OS << "}";
1198 EmitEOL();
1199}
1200
Daniel Dunbar6b716532010-02-09 23:00:14 +00001201void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +00001202 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Daniel Dunbar6b716532010-02-09 23:00:14 +00001203
1204 // Show the encoding in a comment if we have a code emitter.
1205 if (Emitter)
1206 AddEncodingComment(Inst);
1207
Chris Lattner30d9a642010-02-09 00:54:51 +00001208 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001209 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001210 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001211 GetCommentOS() << "\n";
1212 }
1213
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001214 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001215 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +00001216 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001217 else
1218 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +00001219 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +00001220}
1221
Jim Grosbachbd4ec842010-09-22 18:18:30 +00001222/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +00001223/// the specified string in the output .s file. This capability is
1224/// indicated by the hasRawTextSupport() predicate.
1225void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001226 if (!String.empty() && String.back() == '\n')
1227 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +00001228 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001229 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +00001230}
1231
Daniel Dunbara11af532009-06-24 01:03:06 +00001232void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +00001233 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +00001234 if (getContext().hasDwarfFiles() && !UseLoc)
1235 MCDwarfFileTable::Emit(this);
Rafael Espindola5426a9e2011-05-01 04:49:54 +00001236
Rafael Espindolac25dad82011-05-10 03:14:15 +00001237 if (!UseCFI)
1238 EmitFrames(false);
Daniel Dunbara11af532009-06-24 01:03:06 +00001239}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001240
Chris Lattner86e22112010-01-22 07:29:22 +00001241MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1242 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +00001243 bool isVerboseAsm, bool useLoc,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +00001244 bool useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +00001245 MCInstPrinter *IP, MCCodeEmitter *CE,
1246 TargetAsmBackend *TAB, bool ShowInst) {
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +00001247 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +00001248 IP, CE, TAB, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +00001249}