Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 1 | //===- 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 Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 4a0abd8 | 2009-08-27 00:51:57 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCCodeEmitter.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | abde298 | 2009-07-01 06:35:03 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCInst.h" |
Chris Lattner | 90edac0 | 2009-09-14 03:02:37 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCInstPrinter.h" |
Chris Lattner | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 4c42a6d | 2010-03-19 05:48:53 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/ADT/Twine.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | 4a0abd8 | 2009-08-27 00:51:57 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Format.h" |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FormattedStream.h" |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 31 | class MCAsmStreamer : public MCStreamer { |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 32 | formatted_raw_ostream &OS; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 33 | const MCAsmInfo &MAI; |
Chris Lattner | 4c42a6d | 2010-03-19 05:48:53 +0000 | [diff] [blame] | 34 | OwningPtr<MCInstPrinter> InstPrinter; |
Benjamin Kramer | 1abcd06 | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 35 | OwningPtr<MCCodeEmitter> Emitter; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 37 | SmallString<128> CommentToEmit; |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 38 | raw_svector_ostream CommentStream; |
Daniel Dunbar | 9dee8e3 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 39 | |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 40 | const TargetLoweringObjectFile *TLOF; |
| 41 | int PointerSize; |
| 42 | |
Daniel Dunbar | 9dee8e3 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 43 | unsigned IsLittleEndian : 1; |
| 44 | unsigned IsVerboseAsm : 1; |
| 45 | unsigned ShowInst : 1; |
| 46 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 47 | public: |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 48 | MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os, |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 49 | bool isLittleEndian, bool isVerboseAsm, |
| 50 | const TargetLoweringObjectFile *tlof, int pointerSize, |
| 51 | MCInstPrinter *printer, MCCodeEmitter *emitter, bool showInst) |
Chris Lattner | fdab14b | 2010-03-12 18:28:53 +0000 | [diff] [blame] | 52 | : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()), |
| 53 | InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit), |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 54 | TLOF(tlof), PointerSize(pointerSize), |
Daniel Dunbar | 9dee8e3 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 55 | IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm), |
Daniel Dunbar | 5532cf4 | 2010-02-10 01:41:14 +0000 | [diff] [blame] | 56 | ShowInst(showInst) { |
Chris Lattner | 5d672cf | 2010-02-10 00:10:18 +0000 | [diff] [blame] | 57 | if (InstPrinter && IsVerboseAsm) |
| 58 | InstPrinter->setCommentStream(CommentStream); |
| 59 | } |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 60 | ~MCAsmStreamer() {} |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 1658202 | 2010-01-20 06:39:07 +0000 | [diff] [blame] | 62 | bool isLittleEndian() const { return IsLittleEndian; } |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 64 | inline void EmitEOL() { |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 65 | // If we don't have any comments, just emit a \n. |
| 66 | if (!IsVerboseAsm) { |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 67 | OS << '\n'; |
| 68 | return; |
| 69 | } |
| 70 | EmitCommentsAndEOL(); |
| 71 | } |
| 72 | void EmitCommentsAndEOL(); |
Chris Lattner | 56591ab | 2010-02-02 23:37:42 +0000 | [diff] [blame] | 73 | |
| 74 | /// isVerboseAsm - Return true if this streamer supports verbose assembly at |
| 75 | /// all. |
| 76 | virtual bool isVerboseAsm() const { return IsVerboseAsm; } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 91bead7 | 2010-04-03 21:35:55 +0000 | [diff] [blame] | 78 | /// hasRawTextSupport - We support EmitRawText. |
| 79 | virtual bool hasRawTextSupport() const { return true; } |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 80 | |
Chris Lattner | d32c7cf | 2010-01-22 18:21:35 +0000 | [diff] [blame] | 81 | /// AddComment - Add a comment that can be emitted to the generated .s |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 82 | /// file if applicable as a QoI issue to make the output of the compiler |
| 83 | /// more readable. This only affects the MCAsmStreamer, and only when |
| 84 | /// verbose assembly output is enabled. |
Chris Lattner | d32c7cf | 2010-01-22 18:21:35 +0000 | [diff] [blame] | 85 | virtual void AddComment(const Twine &T); |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 86 | |
| 87 | /// AddEncodingComment - Add a comment showing the encoding of an instruction. |
| 88 | virtual void AddEncodingComment(const MCInst &Inst); |
| 89 | |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 90 | /// GetCommentOS - Return a raw_ostream that comments can be written to. |
| 91 | /// Unlike AddComment, you are required to terminate comments with \n if you |
| 92 | /// use this method. |
| 93 | virtual raw_ostream &GetCommentOS() { |
| 94 | if (!IsVerboseAsm) |
| 95 | return nulls(); // Discard comments unless in verbose asm mode. |
| 96 | return CommentStream; |
| 97 | } |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 0fd90fd | 2010-01-22 19:52:01 +0000 | [diff] [blame] | 99 | /// AddBlankLine - Emit a blank line to a .s file to pretty it up. |
| 100 | virtual void AddBlankLine() { |
| 101 | EmitEOL(); |
| 102 | } |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 104 | /// @name MCStreamer Interface |
| 105 | /// @{ |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 975780b | 2009-08-17 05:49:08 +0000 | [diff] [blame] | 107 | virtual void SwitchSection(const MCSection *Section); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 108 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 109 | virtual void InitSections() { |
| 110 | // FIXME, this is MachO specific, but the testsuite |
| 111 | // expects this. |
| 112 | SwitchSection(getContext().getMachOSection("__TEXT", "__text", |
| 113 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 114 | 0, SectionKind::getText())); |
| 115 | } |
| 116 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 117 | virtual void EmitLabel(MCSymbol *Symbol); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 118 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 119 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 120 | virtual void EmitThumbFunc(MCSymbol *Func); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 122 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 123 | virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 124 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 125 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 127 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Chris Lattner | b54b9dd | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 128 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol); |
| 129 | virtual void EmitCOFFSymbolStorageClass(int StorageClass); |
| 130 | virtual void EmitCOFFSymbolType(int Type); |
| 131 | virtual void EndCOFFSymbolDef(); |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 132 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value); |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 133 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 134 | unsigned ByteAlignment); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 136 | /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol. |
| 137 | /// |
| 138 | /// @param Symbol - The common symbol to emit. |
| 139 | /// @param Size - The size of the common symbol. |
| 140 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 142 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 143 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 144 | |
Eric Christopher | 4d01cbe | 2010-05-18 21:16:04 +0000 | [diff] [blame] | 145 | virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol, |
| 146 | uint64_t Size, unsigned ByteAlignment = 0); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 147 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 148 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 149 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 150 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 152 | virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 153 | |
| 154 | virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0); |
| 155 | |
| 156 | virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0); |
| 157 | |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 158 | virtual void EmitGPRel32Value(const MCExpr *Value); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 160 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 161 | virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, |
| 162 | unsigned AddrSpace); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 164 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 165 | unsigned ValueSize = 1, |
| 166 | unsigned MaxBytesToEmit = 0); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 167 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 168 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 169 | unsigned MaxBytesToEmit = 0); |
| 170 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 171 | virtual void EmitValueToOffset(const MCExpr *Offset, |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 172 | unsigned char Value = 0); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 173 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 174 | virtual void EmitFileDirective(StringRef Filename); |
Rafael Espindola | af6b5808 | 2010-11-16 21:20:32 +0000 | [diff] [blame] | 175 | virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename); |
| 176 | virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, |
| 177 | unsigned Column, unsigned Flags, |
| 178 | unsigned Isa, unsigned Discriminator); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 179 | |
| 180 | virtual void EmitInstruction(const MCInst &Inst); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 181 | |
Jim Grosbach | bd4ec84 | 2010-09-22 18:18:30 +0000 | [diff] [blame] | 182 | /// EmitRawText - If this file is backed by an assembly streamer, this dumps |
Chris Lattner | 91bead7 | 2010-04-03 21:35:55 +0000 | [diff] [blame] | 183 | /// the specified string in the output .s file. This capability is |
| 184 | /// indicated by the hasRawTextSupport() predicate. |
| 185 | virtual void EmitRawText(StringRef String); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 187 | virtual void Finish(); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 189 | /// @} |
| 190 | }; |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 192 | } // end anonymous namespace. |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 193 | |
Chris Lattner | d32c7cf | 2010-01-22 18:21:35 +0000 | [diff] [blame] | 194 | /// AddComment - Add a comment that can be emitted to the generated .s |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 195 | /// file if applicable as a QoI issue to make the output of the compiler |
| 196 | /// more readable. This only affects the MCAsmStreamer, and only when |
| 197 | /// verbose assembly output is enabled. |
Chris Lattner | d32c7cf | 2010-01-22 18:21:35 +0000 | [diff] [blame] | 198 | void MCAsmStreamer::AddComment(const Twine &T) { |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 199 | if (!IsVerboseAsm) return; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 200 | |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 201 | // Make sure that CommentStream is flushed. |
| 202 | CommentStream.flush(); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 204 | T.toVector(CommentToEmit); |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 205 | // Each comment goes on its own line. |
| 206 | CommentToEmit.push_back('\n'); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 14ca177 | 2010-01-22 21:16:10 +0000 | [diff] [blame] | 208 | // Tell the comment stream that the vector changed underneath it. |
| 209 | CommentStream.resync(); |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void MCAsmStreamer::EmitCommentsAndEOL() { |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 213 | if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) { |
| 214 | OS << '\n'; |
| 215 | return; |
| 216 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 217 | |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 218 | CommentStream.flush(); |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 219 | StringRef Comments = CommentToEmit.str(); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 220 | |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 221 | assert(Comments.back() == '\n' && |
| 222 | "Comment array not newline terminated"); |
| 223 | do { |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 224 | // Emit a line of comments. |
| 225 | OS.PadToColumn(MAI.getCommentColumn()); |
| 226 | size_t Position = Comments.find('\n'); |
| 227 | OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n'; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 229 | Comments = Comments.substr(Position+1); |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 230 | } while (!Comments.empty()); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 14ca177 | 2010-01-22 21:16:10 +0000 | [diff] [blame] | 232 | CommentToEmit.clear(); |
| 233 | // Tell the comment stream that the vector changed underneath it. |
| 234 | CommentStream.resync(); |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 237 | static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { |
| 238 | assert(Bytes && "Invalid size!"); |
| 239 | return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8)); |
| 240 | } |
| 241 | |
Chris Lattner | 975780b | 2009-08-17 05:49:08 +0000 | [diff] [blame] | 242 | void MCAsmStreamer::SwitchSection(const MCSection *Section) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 243 | assert(Section && "Cannot switch to a null section!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 244 | if (Section != CurSection) { |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 245 | PrevSection = CurSection; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 246 | CurSection = Section; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 247 | Section->PrintSwitchToSection(MAI, OS); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 252 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | c304718 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 253 | assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 254 | assert(CurSection && "Cannot emit before setting section!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 255 | |
Chris Lattner | e07b75e | 2010-09-22 22:19:53 +0000 | [diff] [blame] | 256 | OS << *Symbol << MAI.getLabelSuffix(); |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 257 | EmitEOL(); |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 258 | Symbol->setSection(*CurSection); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 261 | void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 262 | switch (Flag) { |
Daniel Dunbar | 011e4db | 2009-08-13 23:36:34 +0000 | [diff] [blame] | 263 | default: assert(0 && "Invalid flag!"); |
Jason W Kim | afd1cc2 | 2010-09-30 02:45:56 +0000 | [diff] [blame] | 264 | case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break; |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 265 | case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break; |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 266 | case MCAF_Code16: OS << "\t.code\t16"; break; |
Jim Grosbach | ba21957 | 2010-11-05 22:40:09 +0000 | [diff] [blame] | 267 | case MCAF_Code32: OS << "\t.code\t32"; break; |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 269 | EmitEOL(); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 272 | void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 273 | // This needs to emit to a temporary string to get properly quoted |
| 274 | // MCSymbols when they have spaces in them. |
| 275 | OS << "\t.thumb_func"; |
| 276 | if (Func) |
| 277 | OS << '\t' << *Func; |
| 278 | EmitEOL(); |
| 279 | } |
| 280 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 281 | void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 282 | OS << *Symbol << " = " << *Value; |
| 283 | EmitEOL(); |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 284 | |
| 285 | // FIXME: Lift context changes into super class. |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 286 | Symbol->setVariableValue(Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 289 | void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { |
| 290 | OS << ".weakref " << *Alias << ", " << *Symbol; |
| 291 | EmitEOL(); |
| 292 | } |
| 293 | |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 294 | void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 295 | MCSymbolAttr Attribute) { |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 296 | switch (Attribute) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 297 | case MCSA_Invalid: assert(0 && "Invalid symbol attribute"); |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 298 | case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function |
| 299 | case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC |
| 300 | case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object |
| 301 | case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object |
| 302 | case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common |
| 303 | case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype |
Rafael Espindola | e9c0ff2 | 2010-11-13 04:55:06 +0000 | [diff] [blame] | 304 | case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 305 | assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported"); |
Dan Gohman | 523d70e | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 306 | OS << "\t.type\t" << *Symbol << ',' |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 307 | << ((MAI.getCommentString()[0] != '@') ? '@' : '%'); |
| 308 | switch (Attribute) { |
| 309 | default: assert(0 && "Unknown ELF .type"); |
| 310 | case MCSA_ELF_TypeFunction: OS << "function"; break; |
| 311 | case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break; |
| 312 | case MCSA_ELF_TypeObject: OS << "object"; break; |
| 313 | case MCSA_ELF_TypeTLS: OS << "tls_object"; break; |
| 314 | case MCSA_ELF_TypeCommon: OS << "common"; break; |
| 315 | case MCSA_ELF_TypeNoType: OS << "no_type"; break; |
Rafael Espindola | e9c0ff2 | 2010-11-13 04:55:06 +0000 | [diff] [blame] | 316 | case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break; |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 317 | } |
| 318 | EmitEOL(); |
| 319 | return; |
| 320 | case MCSA_Global: // .globl/.global |
| 321 | OS << MAI.getGlobalDirective(); |
| 322 | break; |
Chris Lattner | 61abd7b | 2010-06-21 20:35:01 +0000 | [diff] [blame] | 323 | case MCSA_Hidden: OS << "\t.hidden\t"; break; |
| 324 | case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break; |
| 325 | case MCSA_Internal: OS << "\t.internal\t"; break; |
| 326 | case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break; |
| 327 | case MCSA_Local: OS << "\t.local\t"; break; |
| 328 | case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break; |
Kevin Enderby | e8e98d7 | 2010-11-19 18:39:33 +0000 | [diff] [blame^] | 329 | case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break; |
Chris Lattner | 61abd7b | 2010-06-21 20:35:01 +0000 | [diff] [blame] | 330 | case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break; |
| 331 | case MCSA_Protected: OS << "\t.protected\t"; break; |
| 332 | case MCSA_Reference: OS << "\t.reference\t"; break; |
| 333 | case MCSA_Weak: OS << "\t.weak\t"; break; |
| 334 | case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break; |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 335 | // .weak_reference |
| 336 | case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break; |
Kevin Enderby | f59cac5 | 2010-07-08 17:22:42 +0000 | [diff] [blame] | 337 | case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 340 | OS << *Symbol; |
| 341 | EmitEOL(); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 344 | void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 345 | OS << ".desc" << ' ' << *Symbol << ',' << DescValue; |
| 346 | EmitEOL(); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Chris Lattner | b54b9dd | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 349 | void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 350 | OS << "\t.def\t " << *Symbol << ';'; |
| 351 | EmitEOL(); |
| 352 | } |
| 353 | |
| 354 | void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) { |
| 355 | OS << "\t.scl\t" << StorageClass << ';'; |
| 356 | EmitEOL(); |
| 357 | } |
| 358 | |
| 359 | void MCAsmStreamer::EmitCOFFSymbolType (int Type) { |
| 360 | OS << "\t.type\t" << Type << ';'; |
| 361 | EmitEOL(); |
| 362 | } |
| 363 | |
| 364 | void MCAsmStreamer::EndCOFFSymbolDef() { |
| 365 | OS << "\t.endef"; |
| 366 | EmitEOL(); |
| 367 | } |
| 368 | |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 369 | void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 370 | assert(MAI.hasDotTypeDotSizeDirective()); |
| 371 | OS << "\t.size\t" << *Symbol << ", " << *Value << '\n'; |
| 372 | } |
| 373 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 374 | void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 375 | unsigned ByteAlignment) { |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 376 | OS << "\t.comm\t" << *Symbol << ',' << Size; |
Chris Lattner | 6559d76 | 2010-01-25 07:29:13 +0000 | [diff] [blame] | 377 | if (ByteAlignment != 0) { |
Rafael Espindola | 2e2563b | 2010-01-26 20:21:43 +0000 | [diff] [blame] | 378 | if (MAI.getCOMMDirectiveAlignmentIsInBytes()) |
Chris Lattner | 4ed5438 | 2010-01-19 06:01:04 +0000 | [diff] [blame] | 379 | OS << ',' << ByteAlignment; |
| 380 | else |
| 381 | OS << ',' << Log2_32(ByteAlignment); |
| 382 | } |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 383 | EmitEOL(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 386 | /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol. |
| 387 | /// |
| 388 | /// @param Symbol - The common symbol to emit. |
| 389 | /// @param Size - The size of the common symbol. |
| 390 | void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 391 | assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!"); |
| 392 | OS << "\t.lcomm\t" << *Symbol << ',' << Size; |
| 393 | EmitEOL(); |
| 394 | } |
| 395 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 396 | void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 397 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | 011e4db | 2009-08-13 23:36:34 +0000 | [diff] [blame] | 398 | // Note: a .zerofill directive does not switch sections. |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 399 | OS << ".zerofill "; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 401 | // This is a mach-o specific directive. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 402 | const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section); |
Daniel Dunbar | 12de0df | 2009-08-14 18:51:45 +0000 | [diff] [blame] | 403 | OS << MOSection->getSegmentName() << "," << MOSection->getSectionName(); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 405 | if (Symbol != NULL) { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 406 | OS << ',' << *Symbol << ',' << Size; |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 407 | if (ByteAlignment != 0) |
| 408 | OS << ',' << Log2_32(ByteAlignment); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 410 | EmitEOL(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Eric Christopher | d04d98d | 2010-05-17 02:13:02 +0000 | [diff] [blame] | 413 | // .tbss sym, size, align |
| 414 | // This depends that the symbol has already been mangled from the original, |
| 415 | // e.g. _a. |
Eric Christopher | 4d01cbe | 2010-05-18 21:16:04 +0000 | [diff] [blame] | 416 | void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 417 | uint64_t Size, unsigned ByteAlignment) { |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 418 | assert(Symbol != NULL && "Symbol shouldn't be NULL!"); |
Eric Christopher | 4d01cbe | 2010-05-18 21:16:04 +0000 | [diff] [blame] | 419 | // Instead of using the Section we'll just use the shortcut. |
| 420 | // This is a mach-o specific directive and section. |
Eric Christopher | d04d98d | 2010-05-17 02:13:02 +0000 | [diff] [blame] | 421 | OS << ".tbss " << *Symbol << ", " << Size; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 422 | |
Eric Christopher | 4d01cbe | 2010-05-18 21:16:04 +0000 | [diff] [blame] | 423 | // Output align if we have it. We default to 1 so don't bother printing |
| 424 | // that. |
| 425 | if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 426 | |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 427 | EmitEOL(); |
| 428 | } |
| 429 | |
Chris Lattner | 12e555c | 2010-01-23 00:15:00 +0000 | [diff] [blame] | 430 | static inline char toOctal(int X) { return (X&7)+'0'; } |
| 431 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 432 | static void PrintQuotedString(StringRef Data, raw_ostream &OS) { |
| 433 | OS << '"'; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 434 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 435 | for (unsigned i = 0, e = Data.size(); i != e; ++i) { |
| 436 | unsigned char C = Data[i]; |
| 437 | if (C == '"' || C == '\\') { |
| 438 | OS << '\\' << (char)C; |
| 439 | continue; |
| 440 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 441 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 442 | if (isprint((unsigned char)C)) { |
| 443 | OS << (char)C; |
| 444 | continue; |
| 445 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 446 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 447 | switch (C) { |
| 448 | case '\b': OS << "\\b"; break; |
| 449 | case '\f': OS << "\\f"; break; |
| 450 | case '\n': OS << "\\n"; break; |
| 451 | case '\r': OS << "\\r"; break; |
| 452 | case '\t': OS << "\\t"; break; |
| 453 | default: |
| 454 | OS << '\\'; |
| 455 | OS << toOctal(C >> 6); |
| 456 | OS << toOctal(C >> 3); |
| 457 | OS << toOctal(C >> 0); |
| 458 | break; |
| 459 | } |
| 460 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 461 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 462 | OS << '"'; |
| 463 | } |
| 464 | |
| 465 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 466 | void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 467 | assert(CurSection && "Cannot emit contents before setting section!"); |
Chris Lattner | 12e555c | 2010-01-23 00:15:00 +0000 | [diff] [blame] | 468 | if (Data.empty()) return; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 12e555c | 2010-01-23 00:15:00 +0000 | [diff] [blame] | 470 | if (Data.size() == 1) { |
| 471 | OS << MAI.getData8bitsDirective(AddrSpace); |
| 472 | OS << (unsigned)(unsigned char)Data[0]; |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 473 | EmitEOL(); |
Chris Lattner | 12e555c | 2010-01-23 00:15:00 +0000 | [diff] [blame] | 474 | return; |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 475 | } |
Chris Lattner | 12e555c | 2010-01-23 00:15:00 +0000 | [diff] [blame] | 476 | |
| 477 | // If the data ends with 0 and the target supports .asciz, use it, otherwise |
| 478 | // use .ascii |
| 479 | if (MAI.getAscizDirective() && Data.back() == 0) { |
| 480 | OS << MAI.getAscizDirective(); |
| 481 | Data = Data.substr(0, Data.size()-1); |
| 482 | } else { |
| 483 | OS << MAI.getAsciiDirective(); |
| 484 | } |
| 485 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 486 | OS << ' '; |
| 487 | PrintQuotedString(Data, OS); |
Chris Lattner | 12e555c | 2010-01-23 00:15:00 +0000 | [diff] [blame] | 488 | EmitEOL(); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 491 | /// EmitIntValue - Special case of EmitValue that avoids the client having |
| 492 | /// to pass in a MCExpr for constant integers. |
| 493 | void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size, |
| 494 | unsigned AddrSpace) { |
| 495 | assert(CurSection && "Cannot emit contents before setting section!"); |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 496 | const char *Directive = 0; |
| 497 | switch (Size) { |
| 498 | default: break; |
| 499 | case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; |
| 500 | case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; |
| 501 | case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; |
Chris Lattner | 5eaa54e | 2010-01-20 06:45:39 +0000 | [diff] [blame] | 502 | case 8: |
| 503 | Directive = MAI.getData64bitsDirective(AddrSpace); |
| 504 | // If the target doesn't support 64-bit data, emit as two 32-bit halves. |
| 505 | if (Directive) break; |
| 506 | if (isLittleEndian()) { |
| 507 | EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); |
| 508 | EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); |
| 509 | } else { |
| 510 | EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); |
| 511 | EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); |
| 512 | } |
| 513 | return; |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 514 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 516 | assert(Directive && "Invalid size for machine code value!"); |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 517 | OS << Directive << truncateToSize(Value, Size); |
| 518 | EmitEOL(); |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 521 | void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 522 | unsigned AddrSpace) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 523 | assert(CurSection && "Cannot emit contents before setting section!"); |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 524 | const char *Directive = 0; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 525 | switch (Size) { |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 526 | default: break; |
| 527 | case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; |
| 528 | case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; |
| 529 | case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; |
| 530 | case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 531 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 532 | |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 533 | assert(Directive && "Invalid size for machine code value!"); |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 534 | OS << Directive << *Value; |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 535 | EmitEOL(); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 538 | void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) { |
Rafael Espindola | a4b23ff | 2010-11-19 04:10:13 +0000 | [diff] [blame] | 539 | int64_t IntValue; |
| 540 | if (Value->EvaluateAsAbsolute(IntValue)) { |
| 541 | SmallString<32> Tmp; |
| 542 | raw_svector_ostream OSE(Tmp); |
| 543 | MCObjectWriter::EncodeULEB128(IntValue, OSE); |
| 544 | EmitBytes(OSE.str(), AddrSpace); |
| 545 | return; |
| 546 | } |
Rafael Espindola | 7387345 | 2010-11-04 18:17:08 +0000 | [diff] [blame] | 547 | assert(MAI.hasLEB128() && "Cannot print a .uleb"); |
| 548 | OS << ".uleb128 " << *Value; |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 549 | EmitEOL(); |
| 550 | } |
| 551 | |
| 552 | void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) { |
Rafael Espindola | a4b23ff | 2010-11-19 04:10:13 +0000 | [diff] [blame] | 553 | int64_t IntValue; |
| 554 | if (Value->EvaluateAsAbsolute(IntValue)) { |
| 555 | SmallString<32> Tmp; |
| 556 | raw_svector_ostream OSE(Tmp); |
| 557 | MCObjectWriter::EncodeSLEB128(IntValue, OSE); |
| 558 | EmitBytes(OSE.str(), AddrSpace); |
| 559 | return; |
| 560 | } |
Rafael Espindola | 7387345 | 2010-11-04 18:17:08 +0000 | [diff] [blame] | 561 | assert(MAI.hasLEB128() && "Cannot print a .sleb"); |
| 562 | OS << ".sleb128 " << *Value; |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 563 | EmitEOL(); |
| 564 | } |
| 565 | |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 566 | void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) { |
| 567 | assert(MAI.getGPRel32Directive() != 0); |
| 568 | OS << MAI.getGPRel32Directive() << *Value; |
| 569 | EmitEOL(); |
| 570 | } |
| 571 | |
| 572 | |
Chris Lattner | 6113b3d | 2010-01-19 18:52:28 +0000 | [diff] [blame] | 573 | /// EmitFill - Emit NumBytes bytes worth of the value specified by |
| 574 | /// FillValue. This implements directives such as '.space'. |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 575 | void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, |
| 576 | unsigned AddrSpace) { |
Chris Lattner | 8a6d7ac | 2010-01-19 18:58:52 +0000 | [diff] [blame] | 577 | if (NumBytes == 0) return; |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 578 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 579 | if (AddrSpace == 0) |
| 580 | if (const char *ZeroDirective = MAI.getZeroDirective()) { |
| 581 | OS << ZeroDirective << NumBytes; |
| 582 | if (FillValue != 0) |
| 583 | OS << ',' << (int)FillValue; |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 584 | EmitEOL(); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
| 588 | // Emit a byte at a time. |
| 589 | MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace); |
Chris Lattner | 6113b3d | 2010-01-19 18:52:28 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 592 | void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 593 | unsigned ValueSize, |
| 594 | unsigned MaxBytesToEmit) { |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 595 | // Some assemblers don't support non-power of two alignments, so we always |
| 596 | // emit alignments as a power of two if possible. |
| 597 | if (isPowerOf2_32(ByteAlignment)) { |
Chris Lattner | 6e579c6 | 2009-08-19 06:35:36 +0000 | [diff] [blame] | 598 | switch (ValueSize) { |
| 599 | default: llvm_unreachable("Invalid size for machine code value!"); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 600 | case 1: OS << MAI.getAlignDirective(); break; |
| 601 | // FIXME: use MAI for this! |
Chris Lattner | 6e579c6 | 2009-08-19 06:35:36 +0000 | [diff] [blame] | 602 | case 2: OS << ".p2alignw "; break; |
| 603 | case 4: OS << ".p2alignl "; break; |
| 604 | case 8: llvm_unreachable("Unsupported alignment size!"); |
| 605 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 607 | if (MAI.getAlignmentIsInBytes()) |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 608 | OS << ByteAlignment; |
| 609 | else |
| 610 | OS << Log2_32(ByteAlignment); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 612 | if (Value || MaxBytesToEmit) { |
Chris Lattner | 579531c | 2009-09-03 04:01:10 +0000 | [diff] [blame] | 613 | OS << ", 0x"; |
| 614 | OS.write_hex(truncateToSize(Value, ValueSize)); |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 615 | |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 616 | if (MaxBytesToEmit) |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 617 | OS << ", " << MaxBytesToEmit; |
| 618 | } |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 619 | EmitEOL(); |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 620 | return; |
| 621 | } |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 623 | // Non-power of two alignment. This is not widely supported by assemblers. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 624 | // FIXME: Parameterize this based on MAI. |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 625 | switch (ValueSize) { |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 626 | default: llvm_unreachable("Invalid size for machine code value!"); |
| 627 | case 1: OS << ".balign"; break; |
| 628 | case 2: OS << ".balignw"; break; |
| 629 | case 4: OS << ".balignl"; break; |
| 630 | case 8: llvm_unreachable("Unsupported alignment size!"); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 633 | OS << ' ' << ByteAlignment; |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 634 | OS << ", " << truncateToSize(Value, ValueSize); |
Jim Grosbach | 00545e1 | 2010-09-22 18:16:55 +0000 | [diff] [blame] | 635 | if (MaxBytesToEmit) |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 636 | OS << ", " << MaxBytesToEmit; |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 637 | EmitEOL(); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 640 | void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 641 | unsigned MaxBytesToEmit) { |
Chris Lattner | ec167fd | 2010-02-23 18:44:31 +0000 | [diff] [blame] | 642 | // Emit with a text fill value. |
| 643 | EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(), |
| 644 | 1, MaxBytesToEmit); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 647 | void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 648 | unsigned char Value) { |
| 649 | // FIXME: Verify that Offset is associated with the current section. |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 650 | OS << ".org " << *Offset << ", " << (unsigned) Value; |
| 651 | EmitEOL(); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 654 | |
| 655 | void MCAsmStreamer::EmitFileDirective(StringRef Filename) { |
| 656 | assert(MAI.hasSingleParameterDotFile()); |
| 657 | OS << "\t.file\t"; |
| 658 | PrintQuotedString(Filename, OS); |
| 659 | EmitEOL(); |
| 660 | } |
| 661 | |
Rafael Espindola | af6b5808 | 2010-11-16 21:20:32 +0000 | [diff] [blame] | 662 | bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){ |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 663 | if (!TLOF) { |
| 664 | OS << "\t.file\t" << FileNo << ' '; |
| 665 | PrintQuotedString(Filename, OS); |
| 666 | EmitEOL(); |
| 667 | } |
Rafael Espindola | af6b5808 | 2010-11-16 21:20:32 +0000 | [diff] [blame] | 668 | return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename); |
| 669 | } |
| 670 | |
| 671 | void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, |
| 672 | unsigned Column, unsigned Flags, |
| 673 | unsigned Isa, |
| 674 | unsigned Discriminator) { |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 675 | this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags, |
| 676 | Isa, Discriminator); |
| 677 | if (TLOF) |
| 678 | return; |
| 679 | |
Rafael Espindola | af6b5808 | 2010-11-16 21:20:32 +0000 | [diff] [blame] | 680 | OS << "\t.loc\t" << FileNo << " " << Line << " " << Column; |
| 681 | if (Flags & DWARF2_FLAG_BASIC_BLOCK) |
| 682 | OS << " basic_block"; |
| 683 | if (Flags & DWARF2_FLAG_PROLOGUE_END) |
| 684 | OS << " prologue_end"; |
| 685 | if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN) |
| 686 | OS << " epilogue_begin"; |
| 687 | |
| 688 | unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags(); |
| 689 | if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) { |
| 690 | OS << " is_stmt "; |
| 691 | |
| 692 | if (Flags & DWARF2_FLAG_IS_STMT) |
| 693 | OS << "1"; |
| 694 | else |
| 695 | OS << "0"; |
| 696 | } |
| 697 | |
| 698 | if (Isa) |
| 699 | OS << "isa " << Isa; |
| 700 | if (Discriminator) |
| 701 | OS << "discriminator " << Discriminator; |
| 702 | EmitEOL(); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 705 | void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) { |
| 706 | raw_ostream &OS = GetCommentOS(); |
| 707 | SmallString<256> Code; |
| 708 | SmallVector<MCFixup, 4> Fixups; |
| 709 | raw_svector_ostream VecOS(Code); |
| 710 | Emitter->EncodeInstruction(Inst, VecOS, Fixups); |
| 711 | VecOS.flush(); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 712 | |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 713 | // If we are showing fixups, create symbolic markers in the encoded |
| 714 | // representation. We do this by making a per-bit map to the fixup item index, |
| 715 | // then trying to display it as nicely as possible. |
Daniel Dunbar | 5532cf4 | 2010-02-10 01:41:14 +0000 | [diff] [blame] | 716 | SmallVector<uint8_t, 64> FixupMap; |
| 717 | FixupMap.resize(Code.size() * 8); |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 718 | for (unsigned i = 0, e = Code.size() * 8; i != e; ++i) |
| 719 | FixupMap[i] = 0; |
| 720 | |
| 721 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 722 | MCFixup &F = Fixups[i]; |
Chris Lattner | 8d31de6 | 2010-02-11 21:27:18 +0000 | [diff] [blame] | 723 | const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind()); |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 724 | for (unsigned j = 0; j != Info.TargetSize; ++j) { |
| 725 | unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j; |
| 726 | assert(Index < Code.size() * 8 && "Invalid offset in fixup!"); |
| 727 | FixupMap[Index] = 1 + i; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | OS << "encoding: ["; |
| 732 | for (unsigned i = 0, e = Code.size(); i != e; ++i) { |
| 733 | if (i) |
| 734 | OS << ','; |
| 735 | |
| 736 | // See if all bits are the same map entry. |
| 737 | uint8_t MapEntry = FixupMap[i * 8 + 0]; |
| 738 | for (unsigned j = 1; j != 8; ++j) { |
| 739 | if (FixupMap[i * 8 + j] == MapEntry) |
| 740 | continue; |
| 741 | |
| 742 | MapEntry = uint8_t(~0U); |
| 743 | break; |
| 744 | } |
| 745 | |
| 746 | if (MapEntry != uint8_t(~0U)) { |
| 747 | if (MapEntry == 0) { |
| 748 | OS << format("0x%02x", uint8_t(Code[i])); |
| 749 | } else { |
| 750 | assert(Code[i] == 0 && "Encoder wrote into fixed up bit!"); |
| 751 | OS << char('A' + MapEntry - 1); |
| 752 | } |
| 753 | } else { |
| 754 | // Otherwise, write out in binary. |
| 755 | OS << "0b"; |
| 756 | for (unsigned j = 8; j--;) { |
| 757 | unsigned Bit = (Code[i] >> j) & 1; |
Chris Lattner | 3170a3b | 2010-11-15 05:56:19 +0000 | [diff] [blame] | 758 | |
| 759 | unsigned FixupBit; |
| 760 | if (IsLittleEndian) |
| 761 | FixupBit = i * 8 + j; |
| 762 | else |
| 763 | FixupBit = i * 8 + (7-j); |
| 764 | |
| 765 | if (uint8_t MapEntry = FixupMap[FixupBit]) { |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 766 | assert(Bit == 0 && "Encoder wrote into fixed up bit!"); |
| 767 | OS << char('A' + MapEntry - 1); |
| 768 | } else |
| 769 | OS << Bit; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | OS << "]\n"; |
| 774 | |
| 775 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 776 | MCFixup &F = Fixups[i]; |
Chris Lattner | 8d31de6 | 2010-02-11 21:27:18 +0000 | [diff] [blame] | 777 | const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind()); |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 778 | OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset() |
Daniel Dunbar | 5d5a1e1 | 2010-02-10 04:47:08 +0000 | [diff] [blame] | 779 | << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n"; |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
| 783 | void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { |
| 784 | assert(CurSection && "Cannot emit contents before setting section!"); |
| 785 | |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 786 | if (TLOF) |
| 787 | MCLineEntry::Make(this, getCurrentSection()); |
| 788 | |
Daniel Dunbar | 6b71653 | 2010-02-09 23:00:14 +0000 | [diff] [blame] | 789 | // Show the encoding in a comment if we have a code emitter. |
| 790 | if (Emitter) |
| 791 | AddEncodingComment(Inst); |
| 792 | |
Chris Lattner | 30d9a64 | 2010-02-09 00:54:51 +0000 | [diff] [blame] | 793 | // Show the MCInst if enabled. |
Daniel Dunbar | c9adb8c | 2010-05-26 15:18:13 +0000 | [diff] [blame] | 794 | if (ShowInst) { |
Daniel Dunbar | 67c076c | 2010-03-22 21:49:34 +0000 | [diff] [blame] | 795 | Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n "); |
Daniel Dunbar | c9adb8c | 2010-05-26 15:18:13 +0000 | [diff] [blame] | 796 | GetCommentOS() << "\n"; |
| 797 | } |
| 798 | |
Daniel Dunbar | 67c076c | 2010-03-22 21:49:34 +0000 | [diff] [blame] | 799 | // If we have an AsmPrinter, use that to print, otherwise print the MCInst. |
Daniel Dunbar | 9dee8e3 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 800 | if (InstPrinter) |
Chris Lattner | d374087 | 2010-04-04 05:04:31 +0000 | [diff] [blame] | 801 | InstPrinter->printInst(&Inst, OS); |
Daniel Dunbar | 9dee8e3 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 802 | else |
| 803 | Inst.print(OS, &MAI); |
Chris Lattner | 7d1e49c | 2010-01-22 07:36:39 +0000 | [diff] [blame] | 804 | EmitEOL(); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Jim Grosbach | bd4ec84 | 2010-09-22 18:18:30 +0000 | [diff] [blame] | 807 | /// EmitRawText - If this file is backed by an assembly streamer, this dumps |
Chris Lattner | 91bead7 | 2010-04-03 21:35:55 +0000 | [diff] [blame] | 808 | /// the specified string in the output .s file. This capability is |
| 809 | /// indicated by the hasRawTextSupport() predicate. |
| 810 | void MCAsmStreamer::EmitRawText(StringRef String) { |
Chris Lattner | d5928dc | 2010-04-03 22:06:56 +0000 | [diff] [blame] | 811 | if (!String.empty() && String.back() == '\n') |
| 812 | String = String.substr(0, String.size()-1); |
Chris Lattner | 91bead7 | 2010-04-03 21:35:55 +0000 | [diff] [blame] | 813 | OS << String; |
Chris Lattner | d5928dc | 2010-04-03 22:06:56 +0000 | [diff] [blame] | 814 | EmitEOL(); |
Chris Lattner | 91bead7 | 2010-04-03 21:35:55 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 817 | void MCAsmStreamer::Finish() { |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 818 | // Dump out the dwarf file & directory tables and line tables. |
| 819 | if (getContext().hasDwarfFiles() && TLOF) { |
| 820 | MCDwarfFileTable::Emit(this, TLOF->getDwarfLineSection(), NULL, |
| 821 | PointerSize); |
| 822 | } |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 823 | } |
Daniel Dunbar | 9dee8e3 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 824 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame] | 825 | MCStreamer *llvm::createAsmStreamer(MCContext &Context, |
| 826 | formatted_raw_ostream &OS, |
Chris Lattner | fdab14b | 2010-03-12 18:28:53 +0000 | [diff] [blame] | 827 | bool isLittleEndian, |
Chris Lattner | 0740441 | 2010-01-22 07:06:15 +0000 | [diff] [blame] | 828 | bool isVerboseAsm, MCInstPrinter *IP, |
Daniel Dunbar | 5532cf4 | 2010-02-10 01:41:14 +0000 | [diff] [blame] | 829 | MCCodeEmitter *CE, bool ShowInst) { |
Chris Lattner | fdab14b | 2010-03-12 18:28:53 +0000 | [diff] [blame] | 830 | return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm, |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 831 | NULL, 0, IP, CE, ShowInst); |
| 832 | } |
| 833 | |
| 834 | |
| 835 | MCStreamer *llvm::createAsmStreamerNoLoc(MCContext &Context, |
| 836 | formatted_raw_ostream &OS, |
| 837 | bool isLittleEndian, |
| 838 | bool isVerboseAsm, |
| 839 | const TargetLoweringObjectFile *TLOF, |
| 840 | int PointerSize, |
| 841 | MCInstPrinter *IP, |
| 842 | MCCodeEmitter *CE, bool ShowInst) { |
| 843 | return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm, |
| 844 | TLOF, PointerSize, IP, CE, ShowInst); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 845 | } |