blob: f83b91779d90d15a4f63fb37e2961998546813a1 [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"
Bill Wendling916a94b2011-06-17 20:35:21 +000022#include "llvm/ADT/StringExtras.h"
Chris Lattner86e22112010-01-22 07:29:22 +000023#include "llvm/ADT/Twine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000025#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000026#include "llvm/Support/Format.h"
Chris Lattner86e22112010-01-22 07:29:22 +000027#include "llvm/Support/FormattedStream.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000028#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000029#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbar745dacc2010-12-16 03:05:59 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000031#include <cctype>
Daniel Dunbara11af532009-06-24 01:03:06 +000032using namespace llvm;
33
34namespace {
35
Chris Lattner46a947d2009-08-17 04:17:34 +000036class MCAsmStreamer : public MCStreamer {
Bill Wendling916a94b2011-06-17 20:35:21 +000037protected:
Chris Lattner86e22112010-01-22 07:29:22 +000038 formatted_raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000039 const MCAsmInfo &MAI;
Bill Wendling916a94b2011-06-17 20:35:21 +000040private:
Chris Lattner4c42a6d2010-03-19 05:48:53 +000041 OwningPtr<MCInstPrinter> InstPrinter;
Benjamin Kramer1abcd062010-07-29 17:48:06 +000042 OwningPtr<MCCodeEmitter> Emitter;
Daniel Dunbar745dacc2010-12-16 03:05:59 +000043 OwningPtr<TargetAsmBackend> AsmBackend;
Jim Grosbach00545e12010-09-22 18:16:55 +000044
Chris Lattner86e22112010-01-22 07:29:22 +000045 SmallString<128> CommentToEmit;
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000046 raw_svector_ostream CommentStream;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000047
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000048 unsigned IsVerboseAsm : 1;
49 unsigned ShowInst : 1;
Rafael Espindola89b93722010-12-10 07:39:47 +000050 unsigned UseLoc : 1;
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000051 unsigned UseCFI : 1;
Daniel Dunbar9dee8e32010-02-03 18:18:30 +000052
Rafael Espindola277abc82011-04-30 16:34:57 +000053 enum EHSymbolFlags { EHGlobal = 1,
54 EHWeakDefinition = 1 << 1,
55 EHPrivateExtern = 1 << 2 };
56 DenseMap<const MCSymbol*, unsigned> FlagMap;
57
Rafael Espindola5d4918d2010-12-04 03:21:47 +000058 bool needsSet(const MCExpr *Value);
59
Rafael Espindola6e032942011-05-30 20:20:15 +000060 void EmitRegisterName(int64_t Register);
61
Chris Lattner46a947d2009-08-17 04:17:34 +000062public:
Chris Lattner86e22112010-01-22 07:29:22 +000063 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000064 bool isVerboseAsm, bool useLoc, bool useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +000065 MCInstPrinter *printer, MCCodeEmitter *emitter,
66 TargetAsmBackend *asmbackend,
67 bool showInst)
Chris Lattnerfdab14b2010-03-12 18:28:53 +000068 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
Daniel Dunbar745dacc2010-12-16 03:05:59 +000069 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
70 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000071 ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI) {
Chris Lattner5d672cf2010-02-10 00:10:18 +000072 if (InstPrinter && IsVerboseAsm)
73 InstPrinter->setCommentStream(CommentStream);
74 }
Chris Lattner46a947d2009-08-17 04:17:34 +000075 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000076
Chris Lattner86e22112010-01-22 07:29:22 +000077 inline void EmitEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +000078 // If we don't have any comments, just emit a \n.
79 if (!IsVerboseAsm) {
Chris Lattner86e22112010-01-22 07:29:22 +000080 OS << '\n';
81 return;
82 }
83 EmitCommentsAndEOL();
84 }
85 void EmitCommentsAndEOL();
Chris Lattner56591ab2010-02-02 23:37:42 +000086
87 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
88 /// all.
89 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
Jim Grosbach00545e12010-09-22 18:16:55 +000090
Chris Lattner91bead72010-04-03 21:35:55 +000091 /// hasRawTextSupport - We support EmitRawText.
92 virtual bool hasRawTextSupport() const { return true; }
Daniel Dunbar6b716532010-02-09 23:00:14 +000093
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000094 /// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +000095 /// file if applicable as a QoI issue to make the output of the compiler
96 /// more readable. This only affects the MCAsmStreamer, and only when
97 /// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +000098 virtual void AddComment(const Twine &T);
Daniel Dunbar6b716532010-02-09 23:00:14 +000099
100 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
101 virtual void AddEncodingComment(const MCInst &Inst);
102
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000103 /// GetCommentOS - Return a raw_ostream that comments can be written to.
104 /// Unlike AddComment, you are required to terminate comments with \n if you
105 /// use this method.
106 virtual raw_ostream &GetCommentOS() {
107 if (!IsVerboseAsm)
108 return nulls(); // Discard comments unless in verbose asm mode.
109 return CommentStream;
110 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000111
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000112 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
113 virtual void AddBlankLine() {
114 EmitEOL();
115 }
Daniel Dunbar6b716532010-02-09 23:00:14 +0000116
Chris Lattner46a947d2009-08-17 04:17:34 +0000117 /// @name MCStreamer Interface
118 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +0000119
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000120 virtual void ChangeSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +0000121
Rafael Espindolad80781b2010-09-15 21:48:40 +0000122 virtual void InitSections() {
123 // FIXME, this is MachO specific, but the testsuite
124 // expects this.
125 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
126 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
127 0, SectionKind::getText()));
128 }
129
Chris Lattner46a947d2009-08-17 04:17:34 +0000130 virtual void EmitLabel(MCSymbol *Symbol);
Rafael Espindola277abc82011-04-30 16:34:57 +0000131 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
132 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000133 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +0000134 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbara11af532009-06-24 01:03:06 +0000135
Daniel Dunbar821e3332009-08-31 08:09:28 +0000136 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +0000137 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000138 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
139 const MCSymbol *LastLabel,
140 const MCSymbol *Label);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000141 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
142 const MCSymbol *Label);
Daniel Dunbara11af532009-06-24 01:03:06 +0000143
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000144 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000145
Chris Lattner46a947d2009-08-17 04:17:34 +0000146 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000147 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
148 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
149 virtual void EmitCOFFSymbolType(int Type);
150 virtual void EndCOFFSymbolDef();
Chris Lattner99328ad2010-01-25 07:52:13 +0000151 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000152 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000153 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000154
Chris Lattner9eb158d2010-01-23 07:47:02 +0000155 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
156 ///
157 /// @param Symbol - The common symbol to emit.
158 /// @param Size - The size of the common symbol.
159 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
Jim Grosbach00545e12010-09-22 18:16:55 +0000160
Daniel Dunbar8751b942009-08-28 05:48:22 +0000161 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000162 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +0000163
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000164 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
165 uint64_t Size, unsigned ByteAlignment = 0);
Jim Grosbach00545e12010-09-22 18:16:55 +0000166
Chris Lattneraaec2052010-01-19 19:46:13 +0000167 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000168
Rafael Espindola89b93722010-12-10 07:39:47 +0000169 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000170 unsigned AddrSpace);
Rafael Espindola2df042c2010-12-03 02:54:21 +0000171 virtual void EmitIntValue(uint64_t Value, unsigned Size,
172 unsigned AddrSpace = 0);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000173
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000174 virtual void EmitULEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000175
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000176 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000177
Chris Lattner718fb592010-01-25 21:28:50 +0000178 virtual void EmitGPRel32Value(const MCExpr *Value);
Jim Grosbach00545e12010-09-22 18:16:55 +0000179
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000180
Chris Lattneraaec2052010-01-19 19:46:13 +0000181 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
182 unsigned AddrSpace);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000183
Chris Lattner46a947d2009-08-17 04:17:34 +0000184 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
185 unsigned ValueSize = 1,
186 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000187
Kevin Enderby6e720482010-02-23 18:26:34 +0000188 virtual void EmitCodeAlignment(unsigned ByteAlignment,
189 unsigned MaxBytesToEmit = 0);
190
Daniel Dunbar821e3332009-08-31 08:09:28 +0000191 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +0000192 unsigned char Value = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +0000193
Chris Lattnera6594fc2010-01-25 18:58:59 +0000194 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000195 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
196 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
197 unsigned Column, unsigned Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000198 unsigned Isa, unsigned Discriminator,
199 StringRef FileName);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000200
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000201 virtual void EmitCFISections(bool EH, bool Debug);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000202 virtual void EmitCFIStartProc();
203 virtual void EmitCFIEndProc();
204 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
205 virtual void EmitCFIDefCfaOffset(int64_t Offset);
206 virtual void EmitCFIDefCfaRegister(int64_t Register);
207 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
208 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
209 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
210 virtual void EmitCFIRememberState();
211 virtual void EmitCFIRestoreState();
212 virtual void EmitCFISameValue(int64_t Register);
213 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
214 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000215
Charles Davisfbc539f2011-05-22 21:12:15 +0000216 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
Charles Davis0e30f022011-05-18 04:58:05 +0000217 virtual void EmitWin64EHEndProc();
Charles Davisf0709012011-05-18 20:54:10 +0000218 virtual void EmitWin64EHStartChained();
219 virtual void EmitWin64EHEndChained();
Charles Davis440596f2011-05-19 17:46:39 +0000220 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
221 bool Except);
222 virtual void EmitWin64EHHandlerData();
Charles Davisc3b16282011-05-19 19:35:55 +0000223 virtual void EmitWin64EHPushReg(unsigned Register);
224 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
225 virtual void EmitWin64EHAllocStack(unsigned Size);
226 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
227 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
Charles Davis0e30f022011-05-18 04:58:05 +0000228 virtual void EmitWin64EHPushFrame(bool Code);
229 virtual void EmitWin64EHEndProlog();
230
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000231 virtual void EmitFnStart();
232 virtual void EmitFnEnd();
233 virtual void EmitCantUnwind();
234 virtual void EmitPersonality(const MCSymbol *Personality);
235 virtual void EmitHandlerData();
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000236 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
237 virtual void EmitPad(int64_t Offset);
238 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool);
239
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000240
Chris Lattnera6594fc2010-01-25 18:58:59 +0000241 virtual void EmitInstruction(const MCInst &Inst);
Jim Grosbach00545e12010-09-22 18:16:55 +0000242
Jim Grosbachbd4ec842010-09-22 18:18:30 +0000243 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +0000244 /// the specified string in the output .s file. This capability is
245 /// indicated by the hasRawTextSupport() predicate.
246 virtual void EmitRawText(StringRef String);
Jim Grosbach00545e12010-09-22 18:16:55 +0000247
Chris Lattner46a947d2009-08-17 04:17:34 +0000248 virtual void Finish();
Jim Grosbach00545e12010-09-22 18:16:55 +0000249
Chris Lattner46a947d2009-08-17 04:17:34 +0000250 /// @}
251};
Daniel Dunbar84a29262009-06-24 19:25:34 +0000252
Chris Lattner46a947d2009-08-17 04:17:34 +0000253} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +0000254
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000255/// AddComment - Add a comment that can be emitted to the generated .s
Chris Lattner86e22112010-01-22 07:29:22 +0000256/// file if applicable as a QoI issue to make the output of the compiler
257/// more readable. This only affects the MCAsmStreamer, and only when
258/// verbose assembly output is enabled.
Chris Lattnerd32c7cf2010-01-22 18:21:35 +0000259void MCAsmStreamer::AddComment(const Twine &T) {
Chris Lattner86e22112010-01-22 07:29:22 +0000260 if (!IsVerboseAsm) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000261
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000262 // Make sure that CommentStream is flushed.
263 CommentStream.flush();
Jim Grosbach00545e12010-09-22 18:16:55 +0000264
Chris Lattner86e22112010-01-22 07:29:22 +0000265 T.toVector(CommentToEmit);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000266 // Each comment goes on its own line.
267 CommentToEmit.push_back('\n');
Jim Grosbach00545e12010-09-22 18:16:55 +0000268
Chris Lattner14ca1772010-01-22 21:16:10 +0000269 // Tell the comment stream that the vector changed underneath it.
270 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000271}
272
273void MCAsmStreamer::EmitCommentsAndEOL() {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000274 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
275 OS << '\n';
276 return;
277 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000278
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000279 CommentStream.flush();
Chris Lattner86e22112010-01-22 07:29:22 +0000280 StringRef Comments = CommentToEmit.str();
Jim Grosbach00545e12010-09-22 18:16:55 +0000281
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000282 assert(Comments.back() == '\n' &&
283 "Comment array not newline terminated");
284 do {
Chris Lattner86e22112010-01-22 07:29:22 +0000285 // Emit a line of comments.
286 OS.PadToColumn(MAI.getCommentColumn());
287 size_t Position = Comments.find('\n');
288 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
Jim Grosbach00545e12010-09-22 18:16:55 +0000289
Chris Lattner86e22112010-01-22 07:29:22 +0000290 Comments = Comments.substr(Position+1);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000291 } while (!Comments.empty());
Jim Grosbach00545e12010-09-22 18:16:55 +0000292
Chris Lattner14ca1772010-01-22 21:16:10 +0000293 CommentToEmit.clear();
294 // Tell the comment stream that the vector changed underneath it.
295 CommentStream.resync();
Chris Lattner86e22112010-01-22 07:29:22 +0000296}
297
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000298static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
299 assert(Bytes && "Invalid size!");
300 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
301}
302
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000303void MCAsmStreamer::ChangeSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000304 assert(Section && "Cannot switch to a null section!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000305 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000306}
307
Rafael Espindola277abc82011-04-30 16:34:57 +0000308void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
309 MCSymbol *EHSymbol) {
310 if (UseCFI)
311 return;
312
313 unsigned Flags = FlagMap.lookup(Symbol);
314
315 if (Flags & EHGlobal)
316 EmitSymbolAttribute(EHSymbol, MCSA_Global);
317 if (Flags & EHWeakDefinition)
318 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
319 if (Flags & EHPrivateExtern)
320 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
321}
322
Daniel Dunbara11af532009-06-24 01:03:06 +0000323void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000324 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaed708f92011-04-27 15:21:19 +0000325 MCStreamer::EmitLabel(Symbol);
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000326
Chris Lattnere07b75e2010-09-22 22:19:53 +0000327 OS << *Symbol << MAI.getLabelSuffix();
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000328 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000329}
330
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000331void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Kevin Enderbyf96db462009-07-16 17:56:39 +0000332 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000333 default: assert(0 && "Invalid flag!");
Jason W Kimafd1cc22010-09-30 02:45:56 +0000334 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000335 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
Jim Grosbachce792992010-11-05 22:08:08 +0000336 case MCAF_Code16: OS << "\t.code\t16"; break;
Jim Grosbachba219572010-11-05 22:40:09 +0000337 case MCAF_Code32: OS << "\t.code\t32"; break;
Kevin Enderbyf96db462009-07-16 17:56:39 +0000338 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000339 EmitEOL();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000340}
341
Jim Grosbachce792992010-11-05 22:08:08 +0000342void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
343 // This needs to emit to a temporary string to get properly quoted
344 // MCSymbols when they have spaces in them.
345 OS << "\t.thumb_func";
Rafael Espindola64695402011-05-16 16:17:21 +0000346 // Only Mach-O hasSubsectionsViaSymbols()
347 if (MAI.hasSubsectionsViaSymbols())
Jim Grosbachce792992010-11-05 22:08:08 +0000348 OS << '\t' << *Func;
349 EmitEOL();
350}
351
Daniel Dunbar821e3332009-08-31 08:09:28 +0000352void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000353 OS << *Symbol << " = " << *Value;
354 EmitEOL();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000355
356 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000357 Symbol->setVariableValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000358}
359
Rafael Espindola484291c2010-11-01 14:28:48 +0000360void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
361 OS << ".weakref " << *Alias << ", " << *Symbol;
362 EmitEOL();
363}
364
Rafael Espindola32a006e2010-12-03 00:55:40 +0000365void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
366 const MCSymbol *LastLabel,
367 const MCSymbol *Label) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000368 EmitDwarfSetLineAddr(LineDelta, Label,
369 getContext().getTargetAsmInfo().getPointerSize());
Rafael Espindola32a006e2010-12-03 00:55:40 +0000370}
371
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000372void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
373 const MCSymbol *Label) {
374 EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
375 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
Rafael Espindolaa6f26782011-05-19 21:40:34 +0000376 AddrDelta = ForceExpAbs(AddrDelta);
Rafael Espindolaa37bd1d2011-04-30 03:21:04 +0000377 EmitValue(AddrDelta, 4);
378}
379
380
Daniel Dunbarfffff912009-10-16 01:34:54 +0000381void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000382 MCSymbolAttr Attribute) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000383 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000384 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
Chris Lattnered0ab152010-01-25 18:30:45 +0000385 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
386 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
387 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
388 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
389 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
390 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000391 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
Chris Lattnered0ab152010-01-25 18:30:45 +0000392 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
Dan Gohman523d70e2010-02-04 01:42:13 +0000393 OS << "\t.type\t" << *Symbol << ','
Chris Lattnered0ab152010-01-25 18:30:45 +0000394 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
395 switch (Attribute) {
396 default: assert(0 && "Unknown ELF .type");
397 case MCSA_ELF_TypeFunction: OS << "function"; break;
398 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
399 case MCSA_ELF_TypeObject: OS << "object"; break;
400 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
401 case MCSA_ELF_TypeCommon: OS << "common"; break;
402 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
Rafael Espindolae9c0ff22010-11-13 04:55:06 +0000403 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
Chris Lattnered0ab152010-01-25 18:30:45 +0000404 }
405 EmitEOL();
406 return;
407 case MCSA_Global: // .globl/.global
408 OS << MAI.getGlobalDirective();
Rafael Espindola277abc82011-04-30 16:34:57 +0000409 FlagMap[Symbol] |= EHGlobal;
Chris Lattnered0ab152010-01-25 18:30:45 +0000410 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000411 case MCSA_Hidden: OS << "\t.hidden\t"; break;
412 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
413 case MCSA_Internal: OS << "\t.internal\t"; break;
414 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
415 case MCSA_Local: OS << "\t.local\t"; break;
416 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000417 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000418 case MCSA_PrivateExtern:
419 OS << "\t.private_extern\t";
420 FlagMap[Symbol] |= EHPrivateExtern;
421 break;
Chris Lattner61abd7b2010-06-21 20:35:01 +0000422 case MCSA_Protected: OS << "\t.protected\t"; break;
423 case MCSA_Reference: OS << "\t.reference\t"; break;
424 case MCSA_Weak: OS << "\t.weak\t"; break;
Rafael Espindola277abc82011-04-30 16:34:57 +0000425 case MCSA_WeakDefinition:
426 OS << "\t.weak_definition\t";
427 FlagMap[Symbol] |= EHWeakDefinition;
428 break;
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000429 // .weak_reference
430 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000431 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000432 }
433
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000434 OS << *Symbol;
435 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000436}
437
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000438void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000439 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
440 EmitEOL();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000441}
442
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000443void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
444 OS << "\t.def\t " << *Symbol << ';';
445 EmitEOL();
446}
447
448void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
449 OS << "\t.scl\t" << StorageClass << ';';
450 EmitEOL();
451}
452
453void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
454 OS << "\t.type\t" << Type << ';';
455 EmitEOL();
456}
457
458void MCAsmStreamer::EndCOFFSymbolDef() {
459 OS << "\t.endef";
460 EmitEOL();
461}
462
Chris Lattner99328ad2010-01-25 07:52:13 +0000463void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
464 assert(MAI.hasDotTypeDotSizeDirective());
465 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
466}
467
Chris Lattner9eb158d2010-01-23 07:47:02 +0000468void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000469 unsigned ByteAlignment) {
Chris Lattner9eb158d2010-01-23 07:47:02 +0000470 OS << "\t.comm\t" << *Symbol << ',' << Size;
Chris Lattner6559d762010-01-25 07:29:13 +0000471 if (ByteAlignment != 0) {
Rafael Espindola2e2563b2010-01-26 20:21:43 +0000472 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
Chris Lattner4ed54382010-01-19 06:01:04 +0000473 OS << ',' << ByteAlignment;
474 else
475 OS << ',' << Log2_32(ByteAlignment);
476 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000477 EmitEOL();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000478}
479
Chris Lattner9eb158d2010-01-23 07:47:02 +0000480/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
481///
482/// @param Symbol - The common symbol to emit.
483/// @param Size - The size of the common symbol.
484void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
485 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
486 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
487 EmitEOL();
488}
489
Daniel Dunbar8751b942009-08-28 05:48:22 +0000490void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000491 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000492 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000493 OS << ".zerofill ";
Jim Grosbach00545e12010-09-22 18:16:55 +0000494
Chris Lattner93b6db32009-08-08 23:39:42 +0000495 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000496 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000497 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Jim Grosbach00545e12010-09-22 18:16:55 +0000498
Chris Lattner9be3fee2009-07-10 22:20:30 +0000499 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000500 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000501 if (ByteAlignment != 0)
502 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000503 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000504 EmitEOL();
Chris Lattner9be3fee2009-07-10 22:20:30 +0000505}
506
Eric Christopherd04d98d2010-05-17 02:13:02 +0000507// .tbss sym, size, align
508// This depends that the symbol has already been mangled from the original,
509// e.g. _a.
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000510void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
511 uint64_t Size, unsigned ByteAlignment) {
Eric Christopher482eba02010-05-14 01:50:28 +0000512 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000513 // Instead of using the Section we'll just use the shortcut.
514 // This is a mach-o specific directive and section.
Eric Christopherd04d98d2010-05-17 02:13:02 +0000515 OS << ".tbss " << *Symbol << ", " << Size;
Jim Grosbach00545e12010-09-22 18:16:55 +0000516
Eric Christopher4d01cbe2010-05-18 21:16:04 +0000517 // Output align if we have it. We default to 1 so don't bother printing
518 // that.
519 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
Jim Grosbach00545e12010-09-22 18:16:55 +0000520
Eric Christopher482eba02010-05-14 01:50:28 +0000521 EmitEOL();
522}
523
Chris Lattner12e555c2010-01-23 00:15:00 +0000524static inline char toOctal(int X) { return (X&7)+'0'; }
525
Chris Lattnera6594fc2010-01-25 18:58:59 +0000526static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
527 OS << '"';
Jim Grosbach00545e12010-09-22 18:16:55 +0000528
Chris Lattnera6594fc2010-01-25 18:58:59 +0000529 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
530 unsigned char C = Data[i];
531 if (C == '"' || C == '\\') {
532 OS << '\\' << (char)C;
533 continue;
534 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000535
Chris Lattnera6594fc2010-01-25 18:58:59 +0000536 if (isprint((unsigned char)C)) {
537 OS << (char)C;
538 continue;
539 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000540
Chris Lattnera6594fc2010-01-25 18:58:59 +0000541 switch (C) {
542 case '\b': OS << "\\b"; break;
543 case '\f': OS << "\\f"; break;
544 case '\n': OS << "\\n"; break;
545 case '\r': OS << "\\r"; break;
546 case '\t': OS << "\\t"; break;
547 default:
548 OS << '\\';
549 OS << toOctal(C >> 6);
550 OS << toOctal(C >> 3);
551 OS << toOctal(C >> 0);
552 break;
553 }
554 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000555
Chris Lattnera6594fc2010-01-25 18:58:59 +0000556 OS << '"';
557}
558
559
Chris Lattneraaec2052010-01-19 19:46:13 +0000560void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000561 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner12e555c2010-01-23 00:15:00 +0000562 if (Data.empty()) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000563
Chris Lattner12e555c2010-01-23 00:15:00 +0000564 if (Data.size() == 1) {
565 OS << MAI.getData8bitsDirective(AddrSpace);
566 OS << (unsigned)(unsigned char)Data[0];
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000567 EmitEOL();
Chris Lattner12e555c2010-01-23 00:15:00 +0000568 return;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000569 }
Chris Lattner12e555c2010-01-23 00:15:00 +0000570
571 // If the data ends with 0 and the target supports .asciz, use it, otherwise
572 // use .ascii
573 if (MAI.getAscizDirective() && Data.back() == 0) {
574 OS << MAI.getAscizDirective();
575 Data = Data.substr(0, Data.size()-1);
576 } else {
577 OS << MAI.getAsciiDirective();
578 }
579
Chris Lattnera6594fc2010-01-25 18:58:59 +0000580 OS << ' ';
581 PrintQuotedString(Data, OS);
Chris Lattner12e555c2010-01-23 00:15:00 +0000582 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000583}
584
Rafael Espindola2df042c2010-12-03 02:54:21 +0000585void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
586 unsigned AddrSpace) {
587 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
588}
589
Rafael Espindola89b93722010-12-10 07:39:47 +0000590void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000591 unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000592 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000593 const char *Directive = 0;
594 switch (Size) {
595 default: break;
596 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
597 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
598 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000599 case 8:
600 Directive = MAI.getData64bitsDirective(AddrSpace);
601 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
602 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000603 int64_t IntValue;
604 if (!Value->EvaluateAsAbsolute(IntValue))
605 report_fatal_error("Don't know how to emit this value.");
Rafael Espindola89b93722010-12-10 07:39:47 +0000606 if (getContext().getTargetAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000607 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
608 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000609 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000610 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
611 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000612 }
613 return;
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000614 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000615
Chris Lattner32ae3fe2010-01-19 22:03:38 +0000616 assert(Directive && "Invalid size for machine code value!");
Chris Lattner718fb592010-01-25 21:28:50 +0000617 OS << Directive << *Value;
Chris Lattner86e22112010-01-22 07:29:22 +0000618 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +0000619}
620
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000621void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000622 int64_t IntValue;
623 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000624 EmitULEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000625 return;
626 }
Rafael Espindola73873452010-11-04 18:17:08 +0000627 assert(MAI.hasLEB128() && "Cannot print a .uleb");
628 OS << ".uleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000629 EmitEOL();
630}
631
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000632void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000633 int64_t IntValue;
634 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000635 EmitSLEB128IntValue(IntValue);
Rafael Espindolaa4b23ff2010-11-19 04:10:13 +0000636 return;
637 }
Rafael Espindola73873452010-11-04 18:17:08 +0000638 assert(MAI.hasLEB128() && "Cannot print a .sleb");
639 OS << ".sleb128 " << *Value;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000640 EmitEOL();
641}
642
Chris Lattner718fb592010-01-25 21:28:50 +0000643void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
644 assert(MAI.getGPRel32Directive() != 0);
645 OS << MAI.getGPRel32Directive() << *Value;
646 EmitEOL();
647}
648
649
Chris Lattner6113b3d2010-01-19 18:52:28 +0000650/// EmitFill - Emit NumBytes bytes worth of the value specified by
651/// FillValue. This implements directives such as '.space'.
Chris Lattneraaec2052010-01-19 19:46:13 +0000652void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
653 unsigned AddrSpace) {
Chris Lattner8a6d7ac2010-01-19 18:58:52 +0000654 if (NumBytes == 0) return;
Jim Grosbach00545e12010-09-22 18:16:55 +0000655
Chris Lattneraaec2052010-01-19 19:46:13 +0000656 if (AddrSpace == 0)
657 if (const char *ZeroDirective = MAI.getZeroDirective()) {
658 OS << ZeroDirective << NumBytes;
659 if (FillValue != 0)
660 OS << ',' << (int)FillValue;
Chris Lattner86e22112010-01-22 07:29:22 +0000661 EmitEOL();
Chris Lattneraaec2052010-01-19 19:46:13 +0000662 return;
663 }
664
665 // Emit a byte at a time.
666 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner6113b3d2010-01-19 18:52:28 +0000667}
668
Daniel Dunbar84a29262009-06-24 19:25:34 +0000669void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
670 unsigned ValueSize,
671 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000672 // Some assemblers don't support non-power of two alignments, so we always
673 // emit alignments as a power of two if possible.
674 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000675 switch (ValueSize) {
676 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000677 case 1: OS << MAI.getAlignDirective(); break;
678 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000679 case 2: OS << ".p2alignw "; break;
680 case 4: OS << ".p2alignl "; break;
681 case 8: llvm_unreachable("Unsupported alignment size!");
682 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000683
Chris Lattner33adcfb2009-08-22 21:43:10 +0000684 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000685 OS << ByteAlignment;
686 else
687 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000688
Chris Lattner663c2d22009-08-19 06:12:02 +0000689 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000690 OS << ", 0x";
691 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000692
Jim Grosbach00545e12010-09-22 18:16:55 +0000693 if (MaxBytesToEmit)
Chris Lattner663c2d22009-08-19 06:12:02 +0000694 OS << ", " << MaxBytesToEmit;
695 }
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000696 EmitEOL();
Chris Lattner663c2d22009-08-19 06:12:02 +0000697 return;
698 }
Jim Grosbach00545e12010-09-22 18:16:55 +0000699
Chris Lattner663c2d22009-08-19 06:12:02 +0000700 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000701 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000702 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000703 default: llvm_unreachable("Invalid size for machine code value!");
704 case 1: OS << ".balign"; break;
705 case 2: OS << ".balignw"; break;
706 case 4: OS << ".balignl"; break;
707 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000708 }
709
Chris Lattner663c2d22009-08-19 06:12:02 +0000710 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000711 OS << ", " << truncateToSize(Value, ValueSize);
Jim Grosbach00545e12010-09-22 18:16:55 +0000712 if (MaxBytesToEmit)
Daniel Dunbar84a29262009-06-24 19:25:34 +0000713 OS << ", " << MaxBytesToEmit;
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000714 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000715}
716
Kevin Enderby6e720482010-02-23 18:26:34 +0000717void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
718 unsigned MaxBytesToEmit) {
Chris Lattnerec167fd2010-02-23 18:44:31 +0000719 // Emit with a text fill value.
720 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
721 1, MaxBytesToEmit);
Kevin Enderby6e720482010-02-23 18:26:34 +0000722}
723
Daniel Dunbar821e3332009-08-31 08:09:28 +0000724void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000725 unsigned char Value) {
726 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner7d1e49c2010-01-22 07:36:39 +0000727 OS << ".org " << *Offset << ", " << (unsigned) Value;
728 EmitEOL();
Daniel Dunbar84a29262009-06-24 19:25:34 +0000729}
730
Chris Lattnera6594fc2010-01-25 18:58:59 +0000731
732void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
733 assert(MAI.hasSingleParameterDotFile());
734 OS << "\t.file\t";
735 PrintQuotedString(Filename, OS);
736 EmitEOL();
737}
738
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000739bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
Rafael Espindola89b93722010-12-10 07:39:47 +0000740 if (UseLoc) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000741 OS << "\t.file\t" << FileNo << ' ';
742 PrintQuotedString(Filename, OS);
743 EmitEOL();
744 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000745 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
746}
747
748void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
749 unsigned Column, unsigned Flags,
750 unsigned Isa,
Devang Patel3f3bf932011-04-18 20:26:49 +0000751 unsigned Discriminator,
752 StringRef FileName) {
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000753 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +0000754 Isa, Discriminator, FileName);
Rafael Espindola89b93722010-12-10 07:39:47 +0000755 if (!UseLoc)
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000756 return;
757
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000758 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
759 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
760 OS << " basic_block";
761 if (Flags & DWARF2_FLAG_PROLOGUE_END)
762 OS << " prologue_end";
763 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
764 OS << " epilogue_begin";
765
766 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
767 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
768 OS << " is_stmt ";
769
770 if (Flags & DWARF2_FLAG_IS_STMT)
771 OS << "1";
772 else
773 OS << "0";
774 }
775
776 if (Isa)
777 OS << "isa " << Isa;
778 if (Discriminator)
779 OS << "discriminator " << Discriminator;
Devang Patel3f3bf932011-04-18 20:26:49 +0000780
781 if (IsVerboseAsm) {
782 OS.PadToColumn(MAI.getCommentColumn());
783 OS << MAI.getCommentString() << ' ' << FileName << ':'
784 << Line << ':' << Column;
785 }
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000786 EmitEOL();
Chris Lattnera6594fc2010-01-25 18:58:59 +0000787}
788
Rafael Espindola713c4bf2011-05-10 13:39:48 +0000789void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
790 MCStreamer::EmitCFISections(EH, Debug);
791
792 if (!UseCFI)
793 return;
794
795 OS << "\t.cfi_sections ";
796 if (EH) {
797 OS << ".eh_frame";
798 if (Debug)
799 OS << ", .debug_frame";
800 } else if (Debug) {
801 OS << ".debug_frame";
802 }
803
804 EmitEOL();
805}
806
Rafael Espindola066c2f42011-04-12 23:59:07 +0000807void MCAsmStreamer::EmitCFIStartProc() {
808 MCStreamer::EmitCFIStartProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000809
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000810 if (!UseCFI)
811 return;
812
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000813 OS << "\t.cfi_startproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000814 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000815}
816
Rafael Espindola066c2f42011-04-12 23:59:07 +0000817void MCAsmStreamer::EmitCFIEndProc() {
818 MCStreamer::EmitCFIEndProc();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000819
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000820 if (!UseCFI)
821 return;
822
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000823 OS << "\t.cfi_endproc";
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000824 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000825}
826
Rafael Espindola6e032942011-05-30 20:20:15 +0000827void MCAsmStreamer::EmitRegisterName(int64_t Register) {
828 if (InstPrinter) {
829 const TargetAsmInfo &asmInfo = getContext().getTargetAsmInfo();
830 unsigned LLVMRegister = asmInfo.getLLVMRegNum(Register, true);
Rafael Espindolacde4ce42011-06-02 02:34:55 +0000831 InstPrinter->printRegName(OS, LLVMRegister);
Rafael Espindola6e032942011-05-30 20:20:15 +0000832 } else {
833 OS << Register;
834 }
835}
836
Rafael Espindola066c2f42011-04-12 23:59:07 +0000837void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
Rafael Espindola7f46b082011-04-29 21:41:06 +0000838 MCStreamer::EmitCFIDefCfa(Register, Offset);
839
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000840 if (!UseCFI)
841 return;
842
Rafael Espindola6e032942011-05-30 20:20:15 +0000843 OS << "\t.cfi_def_cfa ";
844 EmitRegisterName(Register);
845 OS << ", " << Offset;
Rafael Espindola7f46b082011-04-29 21:41:06 +0000846 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000847}
848
849void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
850 MCStreamer::EmitCFIDefCfaOffset(Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000851
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000852 if (!UseCFI)
853 return;
854
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000855 OS << "\t.cfi_def_cfa_offset " << Offset;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000856 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000857}
858
Rafael Espindola066c2f42011-04-12 23:59:07 +0000859void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
860 MCStreamer::EmitCFIDefCfaRegister(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000861
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000862 if (!UseCFI)
863 return;
864
Rafael Espindola6e032942011-05-30 20:20:15 +0000865 OS << "\t.cfi_def_cfa_register ";
866 EmitRegisterName(Register);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000867 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000868}
869
Rafael Espindola066c2f42011-04-12 23:59:07 +0000870void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
871 this->MCStreamer::EmitCFIOffset(Register, Offset);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000872
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000873 if (!UseCFI)
874 return;
875
Rafael Espindola6e032942011-05-30 20:20:15 +0000876 OS << "\t.cfi_offset ";
877 EmitRegisterName(Register);
878 OS << ", " << Offset;
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::EmitCFIPersonality(const MCSymbol *Sym,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000883 unsigned Encoding) {
Rafael Espindola066c2f42011-04-12 23:59:07 +0000884 MCStreamer::EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000885
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000886 if (!UseCFI)
887 return;
888
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000889 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000890 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000891}
892
Rafael Espindola066c2f42011-04-12 23:59:07 +0000893void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
894 MCStreamer::EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000895
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000896 if (!UseCFI)
897 return;
898
Anton Korobeynikovc6585202011-01-14 21:57:39 +0000899 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000900 EmitEOL();
Rafael Espindola066c2f42011-04-12 23:59:07 +0000901}
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000902
Rafael Espindola066c2f42011-04-12 23:59:07 +0000903void MCAsmStreamer::EmitCFIRememberState() {
904 MCStreamer::EmitCFIRememberState();
905
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000906 if (!UseCFI)
907 return;
908
Rafael Espindola066c2f42011-04-12 23:59:07 +0000909 OS << "\t.cfi_remember_state";
910 EmitEOL();
911}
912
913void MCAsmStreamer::EmitCFIRestoreState() {
914 MCStreamer::EmitCFIRestoreState();
915
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000916 if (!UseCFI)
917 return;
918
Rafael Espindola066c2f42011-04-12 23:59:07 +0000919 OS << "\t.cfi_restore_state";
920 EmitEOL();
921}
922
923void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
924 MCStreamer::EmitCFISameValue(Register);
925
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000926 if (!UseCFI)
927 return;
928
Rafael Espindola6e032942011-05-30 20:20:15 +0000929 OS << "\t.cfi_same_value ";
930 EmitRegisterName(Register);
Rafael Espindola066c2f42011-04-12 23:59:07 +0000931 EmitEOL();
932}
933
934void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
935 MCStreamer::EmitCFIRelOffset(Register, Offset);
936
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000937 if (!UseCFI)
938 return;
939
Rafael Espindola6e032942011-05-30 20:20:15 +0000940 OS << "\t.cfi_rel_offset ";
941 EmitRegisterName(Register);
942 OS << ", " << Offset;
Rafael Espindola066c2f42011-04-12 23:59:07 +0000943 EmitEOL();
944}
945
946void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
947 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
948
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000949 if (!UseCFI)
950 return;
951
Rafael Espindola066c2f42011-04-12 23:59:07 +0000952 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
953 EmitEOL();
Rafael Espindolacdfecc82010-11-22 14:27:24 +0000954}
955
Charles Davisfbc539f2011-05-22 21:12:15 +0000956void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
Charles Daviscde87e22011-05-20 18:19:22 +0000957 MCStreamer::EmitWin64EHStartProc(Symbol);
958
Charles Davis440596f2011-05-19 17:46:39 +0000959 OS << ".seh_proc " << *Symbol;
Charles Davis0e30f022011-05-18 04:58:05 +0000960 EmitEOL();
961}
962
Charles Davis8b3e5e52011-05-18 22:13:51 +0000963void MCAsmStreamer::EmitWin64EHEndProc() {
Charles Daviscde87e22011-05-20 18:19:22 +0000964 MCStreamer::EmitWin64EHEndProc();
965
Charles Davis440596f2011-05-19 17:46:39 +0000966 OS << "\t.seh_endproc";
Charles Davis0e30f022011-05-18 04:58:05 +0000967 EmitEOL();
968}
969
Charles Davis8b3e5e52011-05-18 22:13:51 +0000970void MCAsmStreamer::EmitWin64EHStartChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000971 MCStreamer::EmitWin64EHStartChained();
972
Charles Davis440596f2011-05-19 17:46:39 +0000973 OS << "\t.seh_startchained";
Charles Davisf0709012011-05-18 20:54:10 +0000974 EmitEOL();
975}
976
Charles Davis8b3e5e52011-05-18 22:13:51 +0000977void MCAsmStreamer::EmitWin64EHEndChained() {
Charles Daviscde87e22011-05-20 18:19:22 +0000978 MCStreamer::EmitWin64EHEndChained();
979
Charles Davis440596f2011-05-19 17:46:39 +0000980 OS << "\t.seh_endchained";
Charles Davisf0709012011-05-18 20:54:10 +0000981 EmitEOL();
982}
983
Charles Davis440596f2011-05-19 17:46:39 +0000984void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
985 bool Except) {
Charles Daviscde87e22011-05-20 18:19:22 +0000986 MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
987
Charles Davis440596f2011-05-19 17:46:39 +0000988 OS << "\t.seh_handler " << *Sym;
989 if (Unwind)
990 OS << ", @unwind";
991 if (Except)
992 OS << ", @except";
Charles Davisf0709012011-05-18 20:54:10 +0000993 EmitEOL();
994}
995
Charles Davis440596f2011-05-19 17:46:39 +0000996void MCAsmStreamer::EmitWin64EHHandlerData() {
Charles Daviscde87e22011-05-20 18:19:22 +0000997 MCStreamer::EmitWin64EHHandlerData();
998
Charles Davis410ef2b2011-05-25 21:43:45 +0000999 // Switch sections. Don't call SwitchSection directly, because that will
1000 // cause the section switch to be visible in the emitted assembly.
1001 // We only do this so the section switch that terminates the handler
1002 // data block is visible.
Charles Davis7b06b732011-05-27 19:09:24 +00001003 MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
1004 StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
Charles Davis410ef2b2011-05-25 21:43:45 +00001005 const MCSection *xdataSect =
Charles Davis7b06b732011-05-27 19:09:24 +00001006 getContext().getTargetAsmInfo().getWin64EHTableSection(suffix);
Charles Davis410ef2b2011-05-25 21:43:45 +00001007 if (xdataSect)
1008 SwitchSectionNoChange(xdataSect);
1009
Charles Davis440596f2011-05-19 17:46:39 +00001010 OS << "\t.seh_handlerdata";
Charles Davisf0709012011-05-18 20:54:10 +00001011 EmitEOL();
1012}
1013
Charles Davisc3b16282011-05-19 19:35:55 +00001014void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
Charles Daviscde87e22011-05-20 18:19:22 +00001015 MCStreamer::EmitWin64EHPushReg(Register);
1016
Charles Davis440596f2011-05-19 17:46:39 +00001017 OS << "\t.seh_pushreg " << Register;
Charles Davis0e30f022011-05-18 04:58:05 +00001018 EmitEOL();
1019}
1020
Charles Davisc3b16282011-05-19 19:35:55 +00001021void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001022 MCStreamer::EmitWin64EHSetFrame(Register, Offset);
1023
Charles Davis440596f2011-05-19 17:46:39 +00001024 OS << "\t.seh_setframe " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001025 EmitEOL();
1026}
1027
Charles Davisc3b16282011-05-19 19:35:55 +00001028void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
Charles Daviscde87e22011-05-20 18:19:22 +00001029 MCStreamer::EmitWin64EHAllocStack(Size);
1030
Charles Davis440596f2011-05-19 17:46:39 +00001031 OS << "\t.seh_stackalloc " << Size;
Charles Davis0e30f022011-05-18 04:58:05 +00001032 EmitEOL();
1033}
1034
Charles Davisc3b16282011-05-19 19:35:55 +00001035void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001036 MCStreamer::EmitWin64EHSaveReg(Register, Offset);
1037
Charles Davis440596f2011-05-19 17:46:39 +00001038 OS << "\t.seh_savereg " << Register << ", " << Offset;
1039 EmitEOL();
1040}
1041
Charles Davisc3b16282011-05-19 19:35:55 +00001042void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
Charles Daviscde87e22011-05-20 18:19:22 +00001043 MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
1044
Charles Davis440596f2011-05-19 17:46:39 +00001045 OS << "\t.seh_savexmm " << Register << ", " << Offset;
Charles Davis0e30f022011-05-18 04:58:05 +00001046 EmitEOL();
1047}
1048
Charles Davis8b3e5e52011-05-18 22:13:51 +00001049void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
Charles Daviscde87e22011-05-20 18:19:22 +00001050 MCStreamer::EmitWin64EHPushFrame(Code);
1051
Charles Davis440596f2011-05-19 17:46:39 +00001052 OS << "\t.seh_pushframe";
Charles Davis0e30f022011-05-18 04:58:05 +00001053 if (Code)
Charles Davis440596f2011-05-19 17:46:39 +00001054 OS << " @code";
Charles Davis0e30f022011-05-18 04:58:05 +00001055 EmitEOL();
1056}
1057
Charles Davis8b3e5e52011-05-18 22:13:51 +00001058void MCAsmStreamer::EmitWin64EHEndProlog(void) {
Charles Daviscde87e22011-05-20 18:19:22 +00001059 MCStreamer::EmitWin64EHEndProlog();
1060
Charles Davis440596f2011-05-19 17:46:39 +00001061 OS << "\t.seh_endprologue";
Charles Davis0e30f022011-05-18 04:58:05 +00001062 EmitEOL();
1063}
1064
Daniel Dunbar6b716532010-02-09 23:00:14 +00001065void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
1066 raw_ostream &OS = GetCommentOS();
1067 SmallString<256> Code;
1068 SmallVector<MCFixup, 4> Fixups;
1069 raw_svector_ostream VecOS(Code);
1070 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
1071 VecOS.flush();
Chris Lattnera6594fc2010-01-25 18:58:59 +00001072
Daniel Dunbar6b716532010-02-09 23:00:14 +00001073 // If we are showing fixups, create symbolic markers in the encoded
1074 // representation. We do this by making a per-bit map to the fixup item index,
1075 // then trying to display it as nicely as possible.
Daniel Dunbar5532cf42010-02-10 01:41:14 +00001076 SmallVector<uint8_t, 64> FixupMap;
1077 FixupMap.resize(Code.size() * 8);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001078 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1079 FixupMap[i] = 0;
1080
1081 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1082 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001083 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001084 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1085 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1086 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1087 FixupMap[Index] = 1 + i;
1088 }
1089 }
1090
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001091 // FIXME: Node the fixup comments for Thumb2 are completely bogus since the
1092 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
Daniel Dunbar6b716532010-02-09 23:00:14 +00001093 OS << "encoding: [";
1094 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1095 if (i)
1096 OS << ',';
1097
1098 // See if all bits are the same map entry.
1099 uint8_t MapEntry = FixupMap[i * 8 + 0];
1100 for (unsigned j = 1; j != 8; ++j) {
1101 if (FixupMap[i * 8 + j] == MapEntry)
1102 continue;
1103
1104 MapEntry = uint8_t(~0U);
1105 break;
1106 }
1107
1108 if (MapEntry != uint8_t(~0U)) {
1109 if (MapEntry == 0) {
1110 OS << format("0x%02x", uint8_t(Code[i]));
1111 } else {
Evan Cheng82caf1a2011-01-13 21:45:26 +00001112 if (Code[i]) {
Evan Cheng8fbbd1c2011-01-13 23:27:39 +00001113 // FIXME: Some of the 8 bits require fix up.
Evan Cheng82caf1a2011-01-13 21:45:26 +00001114 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1115 << char('A' + MapEntry - 1) << '\'';
1116 } else
1117 OS << char('A' + MapEntry - 1);
Daniel Dunbar6b716532010-02-09 23:00:14 +00001118 }
1119 } else {
1120 // Otherwise, write out in binary.
1121 OS << "0b";
1122 for (unsigned j = 8; j--;) {
1123 unsigned Bit = (Code[i] >> j) & 1;
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001124
Chris Lattner3170a3b2010-11-15 05:56:19 +00001125 unsigned FixupBit;
Rafael Espindola89b93722010-12-10 07:39:47 +00001126 if (getContext().getTargetAsmInfo().isLittleEndian())
Chris Lattner3170a3b2010-11-15 05:56:19 +00001127 FixupBit = i * 8 + j;
1128 else
1129 FixupBit = i * 8 + (7-j);
NAKAMURA Takumid6451512011-03-27 01:44:40 +00001130
Chris Lattner3170a3b2010-11-15 05:56:19 +00001131 if (uint8_t MapEntry = FixupMap[FixupBit]) {
Daniel Dunbar6b716532010-02-09 23:00:14 +00001132 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1133 OS << char('A' + MapEntry - 1);
1134 } else
1135 OS << Bit;
1136 }
1137 }
1138 }
1139 OS << "]\n";
1140
1141 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1142 MCFixup &F = Fixups[i];
Daniel Dunbar2761fc42010-12-16 03:20:06 +00001143 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
Daniel Dunbar6b716532010-02-09 23:00:14 +00001144 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
Daniel Dunbar5d5a1e12010-02-10 04:47:08 +00001145 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
Daniel Dunbar6b716532010-02-09 23:00:14 +00001146 }
1147}
1148
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +00001149void MCAsmStreamer::EmitFnStart() {
1150 OS << "\t.fnstart";
1151 EmitEOL();
1152}
1153
1154void MCAsmStreamer::EmitFnEnd() {
1155 OS << "\t.fnend";
1156 EmitEOL();
1157}
1158
1159void MCAsmStreamer::EmitCantUnwind() {
1160 OS << "\t.cantunwind";
1161 EmitEOL();
1162}
1163
1164void MCAsmStreamer::EmitHandlerData() {
1165 OS << "\t.handlerdata";
1166 EmitEOL();
1167}
1168
1169void MCAsmStreamer::EmitPersonality(const MCSymbol *Personality) {
1170 OS << "\t.personality " << Personality->getName();
1171 EmitEOL();
1172}
1173
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001174void MCAsmStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001175 OS << "\t.setfp\t";
1176 InstPrinter->printRegName(OS, FpReg);
1177 OS << ", ";
1178 InstPrinter->printRegName(OS, SpReg);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001179 if (Offset)
1180 OS << ", #" << Offset;
1181 EmitEOL();
1182}
1183
1184void MCAsmStreamer::EmitPad(int64_t Offset) {
1185 OS << "\t.pad\t#" << Offset;
1186 EmitEOL();
1187}
1188
1189void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
1190 bool isVector) {
1191 assert(RegList.size() && "RegList should not be empty");
1192 if (isVector)
1193 OS << "\t.vsave\t{";
1194 else
1195 OS << "\t.save\t{";
1196
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001197 InstPrinter->printRegName(OS, RegList[0]);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001198
Rafael Espindolacde4ce42011-06-02 02:34:55 +00001199 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
1200 OS << ", ";
1201 InstPrinter->printRegName(OS, RegList[i]);
1202 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001203
1204 OS << "}";
1205 EmitEOL();
1206}
1207
Daniel Dunbar6b716532010-02-09 23:00:14 +00001208void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +00001209 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Daniel Dunbar6b716532010-02-09 23:00:14 +00001210
1211 // Show the encoding in a comment if we have a code emitter.
1212 if (Emitter)
1213 AddEncodingComment(Inst);
1214
Chris Lattner30d9a642010-02-09 00:54:51 +00001215 // Show the MCInst if enabled.
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001216 if (ShowInst) {
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001217 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +00001218 GetCommentOS() << "\n";
1219 }
1220
Daniel Dunbar67c076c2010-03-22 21:49:34 +00001221 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001222 if (InstPrinter)
Chris Lattnerd3740872010-04-04 05:04:31 +00001223 InstPrinter->printInst(&Inst, OS);
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001224 else
1225 Inst.print(OS, &MAI);
Chris Lattner7d1e49c2010-01-22 07:36:39 +00001226 EmitEOL();
Daniel Dunbara11af532009-06-24 01:03:06 +00001227}
1228
Jim Grosbachbd4ec842010-09-22 18:18:30 +00001229/// EmitRawText - If this file is backed by an assembly streamer, this dumps
Chris Lattner91bead72010-04-03 21:35:55 +00001230/// the specified string in the output .s file. This capability is
1231/// indicated by the hasRawTextSupport() predicate.
1232void MCAsmStreamer::EmitRawText(StringRef String) {
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001233 if (!String.empty() && String.back() == '\n')
1234 String = String.substr(0, String.size()-1);
Chris Lattner91bead72010-04-03 21:35:55 +00001235 OS << String;
Chris Lattnerd5928dc2010-04-03 22:06:56 +00001236 EmitEOL();
Chris Lattner91bead72010-04-03 21:35:55 +00001237}
1238
Daniel Dunbara11af532009-06-24 01:03:06 +00001239void MCAsmStreamer::Finish() {
Rafael Espindola195a0ce2010-11-19 02:26:16 +00001240 // Dump out the dwarf file & directory tables and line tables.
Rafael Espindola89b93722010-12-10 07:39:47 +00001241 if (getContext().hasDwarfFiles() && !UseLoc)
1242 MCDwarfFileTable::Emit(this);
Rafael Espindola5426a9e2011-05-01 04:49:54 +00001243
Rafael Espindolac25dad82011-05-10 03:14:15 +00001244 if (!UseCFI)
1245 EmitFrames(false);
Daniel Dunbara11af532009-06-24 01:03:06 +00001246}
Daniel Dunbar9dee8e32010-02-03 18:18:30 +00001247
Bill Wendling916a94b2011-06-17 20:35:21 +00001248//===----------------------------------------------------------------------===//
1249/// MCLSDADecoderAsmStreamer - This is identical to the MCAsmStreamer, but
1250/// outputs a description of the LSDA in a human readable format.
1251///
1252namespace {
1253
1254class MCLSDADecoderAsmStreamer : public MCAsmStreamer {
1255 const MCSymbol *PersonalitySymbol;
1256 const MCSymbol *LSDASymbol;
1257 bool InLSDA;
1258 bool ReadingULEB128;
1259
1260 uint64_t BytesRead;
1261 uint64_t ActionTableBytes;
1262 uint64_t LSDASize;
1263
1264 SmallVector<char, 2> ULEB128Value;
1265 std::vector<int64_t> LSDAEncoding;
1266 std::vector<const MCExpr*> Assignments;
1267
1268 /// GetULEB128Value - A helper function to convert the value in the
1269 /// ULEB128Value vector into a uint64_t.
1270 uint64_t GetULEB128Value(SmallVectorImpl<char> &ULEB128Value) {
1271 uint64_t Val = 0;
1272 for (unsigned i = 0, e = ULEB128Value.size(); i != e; ++i)
1273 Val |= (ULEB128Value[i] & 0x7F) << (7 * i);
1274 return Val;
1275 }
1276
1277 /// ResetState - Reset the state variables.
1278 void ResetState() {
1279 PersonalitySymbol = 0;
1280 LSDASymbol = 0;
1281 LSDASize = 0;
1282 BytesRead = 0;
1283 ActionTableBytes = 0;
1284 InLSDA = false;
1285 ReadingULEB128 = false;
1286 ULEB128Value.clear();
1287 LSDAEncoding.clear();
1288 Assignments.clear();
1289 }
1290
1291 void EmitEHTableDescription();
1292
1293 const char *DecodeDWARFEncoding(unsigned Encoding) {
1294 switch (Encoding) {
1295 case dwarf::DW_EH_PE_absptr: return "absptr";
1296 case dwarf::DW_EH_PE_omit: return "omit";
1297 case dwarf::DW_EH_PE_pcrel: return "pcrel";
1298 case dwarf::DW_EH_PE_udata4: return "udata4";
1299 case dwarf::DW_EH_PE_udata8: return "udata8";
1300 case dwarf::DW_EH_PE_sdata4: return "sdata4";
1301 case dwarf::DW_EH_PE_sdata8: return "sdata8";
1302 case dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4: return "pcrel udata4";
1303 case dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
1304 case dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8: return "pcrel udata8";
1305 case dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
1306 case dwarf::DW_EH_PE_indirect|dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
1307 return "indirect pcrel udata4";
1308 case dwarf::DW_EH_PE_indirect|dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
1309 return "indirect pcrel sdata4";
1310 case dwarf::DW_EH_PE_indirect|dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
1311 return "indirect pcrel udata8";
1312 case dwarf::DW_EH_PE_indirect|dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
1313 return "indirect pcrel sdata8";
1314 }
1315
1316 return "<unknown encoding>";
1317 }
1318public:
1319 MCLSDADecoderAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
1320 bool isVerboseAsm, bool useLoc, bool useCFI,
1321 MCInstPrinter *printer, MCCodeEmitter *emitter,
1322 TargetAsmBackend *asmbackend,
1323 bool showInst)
1324 : MCAsmStreamer(Context, os, isVerboseAsm, useLoc, useCFI,
1325 printer, emitter, asmbackend, showInst) {
1326 ResetState();
1327 }
1328 ~MCLSDADecoderAsmStreamer() {}
1329
1330 virtual void Finish() {
1331 ResetState();
1332 MCAsmStreamer::Finish();
1333 }
1334
1335 virtual void EmitLabel(MCSymbol *Symbol) {
1336 if (Symbol == LSDASymbol)
1337 InLSDA = true;
1338 MCAsmStreamer::EmitLabel(Symbol);
1339 }
1340 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
1341 if (InLSDA)
1342 Assignments.push_back(Value);
1343 MCAsmStreamer::EmitAssignment(Symbol, Value);
1344 }
1345 virtual void EmitIntValue(uint64_t Value, unsigned Size,
1346 unsigned AddrSpace = 0);
1347 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
1348 unsigned AddrSpace);
1349 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
1350 unsigned AddrSpace);
1351 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding) {
1352 PersonalitySymbol = Sym;
1353 MCAsmStreamer::EmitCFIPersonality(Sym, Encoding);
1354 }
1355 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
1356 LSDASymbol = Sym;
1357 MCAsmStreamer::EmitCFILsda(Sym, Encoding);
1358 }
1359};
1360
1361} // end anonymous namespace
1362
1363void MCLSDADecoderAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
1364 unsigned AddrSpace) {
1365 if (!InLSDA)
1366 return MCAsmStreamer::EmitIntValue(Value, Size, AddrSpace);
1367
1368 BytesRead += Size;
1369
1370 // We place the LSDA into the LSDAEncoding vector for later analysis. If we
1371 // have a ULEB128, we read that in separate iterations through here and then
1372 // get its value.
1373 if (!ReadingULEB128) {
1374 LSDAEncoding.push_back(Value);
1375 int EncodingSize = LSDAEncoding.size();
1376
1377 if (EncodingSize == 1 || EncodingSize == 3) {
1378 // The LPStart and TType encodings.
1379 if (Value != dwarf::DW_EH_PE_omit) {
1380 // The encoding is next and is a ULEB128 value.
1381 ReadingULEB128 = true;
1382 ULEB128Value.clear();
1383 } else {
1384 // The encoding was omitted. Put a 0 here as a placeholder.
1385 LSDAEncoding.push_back(0);
1386 }
1387 } else if (EncodingSize == 5) {
1388 // The next value is a ULEB128 value that tells us how long the call site
1389 // table is -- where the start of the action tab
1390 ReadingULEB128 = true;
1391 ULEB128Value.clear();
1392 }
1393
1394 InLSDA = (LSDASize == 0 || BytesRead < LSDASize);
1395 } else {
1396 // We're reading a ULEB128. Make it so!
1397 assert(Size == 1 && "Non-byte representation of a ULEB128?");
1398 ULEB128Value.push_back(Value);
1399
1400 if ((Value & 0x80) == 0) {
1401 uint64_t Val = GetULEB128Value(ULEB128Value);
1402 LSDAEncoding.push_back(Val);
1403 ULEB128Value.clear();
1404 ReadingULEB128 = false;
1405
1406 if (LSDAEncoding.size() == 4)
1407 // The fourth value tells us where the bottom of the type table is.
1408 LSDASize = BytesRead + LSDAEncoding[3];
1409 else if (LSDAEncoding.size() == 6)
1410 // The sixth value tells us where the start of the action table is.
1411 ActionTableBytes = BytesRead;
1412 }
1413 }
1414
1415 MCAsmStreamer::EmitValueImpl(MCConstantExpr::Create(Value, getContext()),
1416 Size, AddrSpace);
1417
1418 if (LSDASize != 0 && !InLSDA)
1419 EmitEHTableDescription();
1420}
1421
1422void MCLSDADecoderAsmStreamer::EmitValueImpl(const MCExpr *Value,
1423 unsigned Size,
1424 unsigned AddrSpace) {
1425 if (InLSDA && LSDASize != 0) {
1426 assert(BytesRead + Size <= LSDASize && "EH table too small!");
1427
1428 if (BytesRead > uint64_t(LSDAEncoding[5]) + ActionTableBytes)
1429 // Insert the type values.
1430 Assignments.push_back(Value);
1431
1432 LSDAEncoding.push_back(Assignments.size());
1433 BytesRead += Size;
1434 InLSDA = (LSDASize == 0 || BytesRead < LSDASize);
1435 }
1436
1437 MCAsmStreamer::EmitValueImpl(Value, Size, AddrSpace);
1438
1439 if (LSDASize != 0 && !InLSDA)
1440 EmitEHTableDescription();
1441}
1442
1443void MCLSDADecoderAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
1444 unsigned AddrSpace) {
1445 if (InLSDA && ReadingULEB128) {
1446 for (uint64_t I = NumBytes; I != 0; --I)
1447 ULEB128Value.push_back(FillValue);
1448
1449 BytesRead += NumBytes;
1450
1451 if ((FillValue & 0x80) == 0) {
1452 uint64_t Val = GetULEB128Value(ULEB128Value);
1453 LSDAEncoding.push_back(Val);
1454 ULEB128Value.clear();
1455 ReadingULEB128 = false;
1456
1457 if (LSDAEncoding.size() == 4)
1458 // The fourth value tells us where the bottom of the type table is.
1459 LSDASize = BytesRead + LSDAEncoding[3];
1460 else if (LSDAEncoding.size() == 6)
1461 // The sixth value tells us where the start of the action table is.
1462 ActionTableBytes = BytesRead;
1463 }
1464 }
1465
1466 MCAsmStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
1467}
1468
1469/// EmitEHTableDescription - Emit a human readable version of the LSDA.
1470void MCLSDADecoderAsmStreamer::EmitEHTableDescription() {
1471 assert(LSDAEncoding.size() > 6 && "Invalid LSDA!");
1472
1473 // Emit header information.
1474 StringRef C = MAI.getCommentString();
1475#define CMT OS << C << ' '
1476 CMT << "Exception Handling Table: " << LSDASymbol->getName() << "\n";
1477 CMT << " @LPStart Encoding: " << DecodeDWARFEncoding(LSDAEncoding[0]) << "\n";
1478 if (LSDAEncoding[1])
1479 CMT << "@LPStart: 0x" << LSDAEncoding[1] << "\n";
1480 CMT << " @TType Encoding: " << DecodeDWARFEncoding(LSDAEncoding[2]) << "\n";
1481 CMT << " @TType Base: " << LSDAEncoding[3] << " bytes\n";
1482 CMT << "@CallSite Encoding: " << DecodeDWARFEncoding(LSDAEncoding[4]) << "\n";
1483 CMT << "@Action Table Size: " << LSDAEncoding[5] << " bytes\n\n";
1484
1485 bool isSjLjEH = (MAI.getExceptionHandlingType() == ExceptionHandling::ARM);
1486
1487 int64_t CallSiteTableSize = LSDAEncoding[5];
1488 unsigned CallSiteEntrySize;
1489 if (!isSjLjEH)
1490 CallSiteEntrySize = 4 + // Region start.
1491 4 + // Region end.
1492 4 + // Landing pad.
1493 1; // TType index.
1494 else
1495 CallSiteEntrySize = 1 + // Call index.
1496 1 + // Landing pad.
1497 1; // TType index.
1498
1499 unsigned NumEntries = CallSiteTableSize / CallSiteEntrySize;
1500 assert(CallSiteTableSize % CallSiteEntrySize == 0 &&
1501 "The action table size is not a multiple of what it should be!");
1502 unsigned TTypeIdx = 5 + // Action table size index.
1503 (isSjLjEH ? 3 : 4) * NumEntries + // Action table entries.
1504 1; // Just because.
1505
1506 // Emit the action table.
1507 unsigned Action = 1;
1508 for (unsigned I = 6; I < TTypeIdx; ) {
1509 CMT << "Action " << Action++ << ":\n";
1510
1511 // The beginning of the throwing region.
1512 uint64_t Idx = LSDAEncoding[I++];
1513
1514 if (!isSjLjEH) {
1515 CMT << " A throw between "
1516 << *cast<MCBinaryExpr>(Assignments[Idx - 1])->getLHS() << " and ";
1517
1518 // The end of the throwing region.
1519 Idx = LSDAEncoding[I++];
1520 OS << *cast<MCBinaryExpr>(Assignments[Idx - 1])->getLHS();
1521 } else {
1522 CMT << " A throw from call " << *Assignments[Idx - 1];
1523 }
1524
1525 // The landing pad.
1526 Idx = LSDAEncoding[I++];
1527 if (Idx) {
1528 OS << " jumps to "
1529 << *cast<MCBinaryExpr>(Assignments[Idx - 1])->getLHS()
1530 << " on an exception.\n";
1531 } else {
1532 OS << " does not have a landing pad.\n";
1533 ++I;
1534 continue;
1535 }
1536
1537 // The index into the action table.
1538 Idx = LSDAEncoding[I++];
1539 if (!Idx) {
1540 CMT << " :cleanup:\n";
1541 continue;
1542 }
1543
1544 // A semi-graphical representation of what the different indexes are in the
1545 // loop below.
1546 //
1547 // Idx - Index into the action table.
1548 // Action - Index into the type table from the type table base.
1549 // Next - Offset from Idx to the next action type.
1550 //
1551 // Idx---.
1552 // |
1553 // v
1554 // [call site table] _1 _2 _3
1555 // TTypeIdx--> .........................
1556 // [action 1] _1 _2
1557 // [action 2] _1 _2
1558 // ...
1559 // [action n] _1 _2
1560 // [type m] ^
1561 // ... |
1562 // [type 1] `---Next
1563 //
1564
1565 int Action = LSDAEncoding[TTypeIdx + Idx - 1];
1566 if ((Action & 0x40) != 0)
1567 // Ignore exception specifications.
1568 continue;
1569
1570 // Emit the types that are caught by this exception.
1571 CMT << " For type(s): ";
1572 for (;;) {
1573 if ((Action & 0x40) != 0)
1574 // Ignore exception specifications.
1575 break;
1576
1577 if (uint64_t Ty = LSDAEncoding[LSDAEncoding.size() - Action]) {
1578 OS << " " << *Assignments[Ty - 1];
1579
1580 // Types can be chained together. Typically, it's a negative offset from
1581 // the current type to a different one in the type table.
1582 int Next = LSDAEncoding[TTypeIdx + Idx];
1583 if (Next == 0)
1584 break;
1585 if ((Next & 0x40) != 0)
1586 Next = (int)(signed char)(Next | 0x80);
1587 Idx += Next + 1;
1588 Action = LSDAEncoding[TTypeIdx + Idx - 1];
1589 continue;
1590 } else {
1591 OS << " :catchall:";
1592 }
1593 break;
1594 }
1595
1596 OS << "\n";
1597 }
1598
1599 OS << "\n";
1600 ResetState();
1601}
1602
Chris Lattner86e22112010-01-22 07:29:22 +00001603MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1604 formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +00001605 bool isVerboseAsm, bool useLoc,
Bill Wendling916a94b2011-06-17 20:35:21 +00001606 bool useCFI, MCInstPrinter *IP,
1607 MCCodeEmitter *CE, TargetAsmBackend *TAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +00001608 bool ShowInst) {
1609 if (isVerboseAsm)
Bill Wendling916a94b2011-06-17 20:35:21 +00001610 return new MCLSDADecoderAsmStreamer(Context, OS, isVerboseAsm, useLoc,
1611 useCFI, IP, CE, TAB, ShowInst);
1612
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +00001613 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, useCFI,
Daniel Dunbar745dacc2010-12-16 03:05:59 +00001614 IP, CE, TAB, ShowInst);
Daniel Dunbara11af532009-06-24 01:03:06 +00001615}