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 | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/Twine.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | 4a0abd8 | 2009-08-27 00:51:57 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Format.h" |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 24 | #include "llvm/Support/FormattedStream.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 29 | class MCAsmStreamer : public MCStreamer { |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 30 | formatted_raw_ostream &OS; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 31 | const MCAsmInfo &MAI; |
Chris Lattner | 0740441 | 2010-01-22 07:06:15 +0000 | [diff] [blame] | 32 | bool IsLittleEndian, IsVerboseAsm; |
Chris Lattner | 90edac0 | 2009-09-14 03:02:37 +0000 | [diff] [blame] | 33 | MCInstPrinter *InstPrinter; |
Daniel Dunbar | 4a0abd8 | 2009-08-27 00:51:57 +0000 | [diff] [blame] | 34 | MCCodeEmitter *Emitter; |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 35 | |
| 36 | SmallString<128> CommentToEmit; |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 37 | public: |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 38 | MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os, |
| 39 | const MCAsmInfo &mai, |
Chris Lattner | 0740441 | 2010-01-22 07:06:15 +0000 | [diff] [blame] | 40 | bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer, |
| 41 | MCCodeEmitter *emitter) |
| 42 | : MCStreamer(Context), OS(os), MAI(mai), IsLittleEndian(isLittleEndian), |
| 43 | IsVerboseAsm(isVerboseAsm), InstPrinter(printer), Emitter(emitter) {} |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 44 | ~MCAsmStreamer() {} |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 1658202 | 2010-01-20 06:39:07 +0000 | [diff] [blame] | 46 | bool isLittleEndian() const { return IsLittleEndian; } |
| 47 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 48 | |
| 49 | inline void EmitEOL() { |
| 50 | if (CommentToEmit.empty()) { |
| 51 | OS << '\n'; |
| 52 | return; |
| 53 | } |
| 54 | EmitCommentsAndEOL(); |
| 55 | } |
| 56 | void EmitCommentsAndEOL(); |
| 57 | |
| 58 | /// addComment - Add a comment that can be emitted to the generated .s |
| 59 | /// file if applicable as a QoI issue to make the output of the compiler |
| 60 | /// more readable. This only affects the MCAsmStreamer, and only when |
| 61 | /// verbose assembly output is enabled. |
| 62 | virtual void addComment(const Twine &T); |
| 63 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 64 | /// @name MCStreamer Interface |
| 65 | /// @{ |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 975780b | 2009-08-17 05:49:08 +0000 | [diff] [blame] | 67 | virtual void SwitchSection(const MCSection *Section); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 69 | virtual void EmitLabel(MCSymbol *Symbol); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 71 | virtual void EmitAssemblerFlag(AssemblerFlag Flag); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 73 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 75 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 77 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 79 | virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 80 | unsigned ByteAlignment); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 81 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 82 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 83 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 84 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 85 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 86 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 87 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 88 | virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace); |
| 89 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 90 | virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, |
| 91 | unsigned AddrSpace); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 93 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 94 | unsigned ValueSize = 1, |
| 95 | unsigned MaxBytesToEmit = 0); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 97 | virtual void EmitValueToOffset(const MCExpr *Offset, |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 98 | unsigned char Value = 0); |
| 99 | |
| 100 | virtual void EmitInstruction(const MCInst &Inst); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 102 | virtual void Finish(); |
| 103 | |
| 104 | /// @} |
| 105 | }; |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 107 | } // end anonymous namespace. |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 109 | /// addComment - Add a comment that can be emitted to the generated .s |
| 110 | /// file if applicable as a QoI issue to make the output of the compiler |
| 111 | /// more readable. This only affects the MCAsmStreamer, and only when |
| 112 | /// verbose assembly output is enabled. |
| 113 | void MCAsmStreamer::addComment(const Twine &T) { |
| 114 | if (!IsVerboseAsm) return; |
| 115 | // Each comment goes on its own line. |
| 116 | if (!CommentToEmit.empty()) |
| 117 | CommentToEmit.push_back('\n'); |
| 118 | T.toVector(CommentToEmit); |
| 119 | } |
| 120 | |
| 121 | void MCAsmStreamer::EmitCommentsAndEOL() { |
| 122 | StringRef Comments = CommentToEmit.str(); |
| 123 | while (!Comments.empty()) { |
| 124 | // Emit a line of comments. |
| 125 | OS.PadToColumn(MAI.getCommentColumn()); |
| 126 | size_t Position = Comments.find('\n'); |
| 127 | OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n'; |
| 128 | |
| 129 | if (Position == StringRef::npos) break; |
| 130 | Comments = Comments.substr(Position+1); |
| 131 | } |
| 132 | |
| 133 | CommentToEmit.clear(); |
| 134 | } |
| 135 | |
| 136 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 137 | static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { |
| 138 | assert(Bytes && "Invalid size!"); |
| 139 | return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8)); |
| 140 | } |
| 141 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 142 | static inline const MCExpr *truncateToSize(const MCExpr *Value, |
| 143 | unsigned Bytes) { |
| 144 | // FIXME: Do we really need this routine? |
| 145 | return Value; |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | 975780b | 2009-08-17 05:49:08 +0000 | [diff] [blame] | 148 | void MCAsmStreamer::SwitchSection(const MCSection *Section) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 149 | assert(Section && "Cannot switch to a null section!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 150 | if (Section != CurSection) { |
| 151 | CurSection = Section; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 152 | Section->PrintSwitchToSection(MAI, OS); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 157 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 158 | assert(CurSection && "Cannot emit before setting section!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 160 | OS << *Symbol << ":\n"; |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 161 | Symbol->setSection(*CurSection); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 164 | void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) { |
| 165 | switch (Flag) { |
Daniel Dunbar | 011e4db | 2009-08-13 23:36:34 +0000 | [diff] [blame] | 166 | default: assert(0 && "Invalid flag!"); |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 167 | case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break; |
| 168 | } |
| 169 | OS << '\n'; |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 172 | void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 173 | // Only absolute symbols can be redefined. |
| 174 | assert((Symbol->isUndefined() || Symbol->isAbsolute()) && |
| 175 | "Cannot define a symbol twice!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 177 | OS << *Symbol << " = " << *Value << '\n'; |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 178 | |
| 179 | // FIXME: Lift context changes into super class. |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 180 | // FIXME: Set associated section. |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 181 | Symbol->setValue(Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 184 | void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 185 | SymbolAttr Attribute) { |
| 186 | switch (Attribute) { |
Chris Lattner | 32f6a8d | 2010-01-20 17:50:30 +0000 | [diff] [blame] | 187 | case Global: OS << MAI.getGlobalDirective(); break; // .globl |
| 188 | case Hidden: OS << ".hidden "; break; |
| 189 | case IndirectSymbol: OS << ".indirect_symbol "; break; |
| 190 | case Internal: OS << ".internal "; break; |
| 191 | case LazyReference: OS << ".lazy_reference "; break; |
| 192 | case NoDeadStrip: OS << ".no_dead_strip "; break; |
| 193 | case PrivateExtern: OS << ".private_extern "; break; |
| 194 | case Protected: OS << ".protected "; break; |
| 195 | case Reference: OS << ".reference "; break; |
| 196 | case Weak: OS << ".weak "; break; |
| 197 | case WeakDefinition: OS << ".weak_definition "; break; |
| 198 | case WeakReference: OS << ".weak_reference "; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 32f6a8d | 2010-01-20 17:50:30 +0000 | [diff] [blame] | 201 | OS << *Symbol << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 204 | void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 205 | OS << ".desc" << ' ' << *Symbol << ',' << DescValue << '\n'; |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 208 | void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 209 | unsigned ByteAlignment) { |
Chris Lattner | 4ed5438 | 2010-01-19 06:01:04 +0000 | [diff] [blame] | 210 | OS << MAI.getCOMMDirective() << *Symbol << ',' << Size; |
| 211 | if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) { |
| 212 | if (MAI.getAlignmentIsInBytes()) |
| 213 | OS << ',' << ByteAlignment; |
| 214 | else |
| 215 | OS << ',' << Log2_32(ByteAlignment); |
| 216 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 217 | OS << '\n'; |
| 218 | } |
| 219 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 220 | void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 221 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | 011e4db | 2009-08-13 23:36:34 +0000 | [diff] [blame] | 222 | // Note: a .zerofill directive does not switch sections. |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 223 | OS << ".zerofill "; |
| 224 | |
| 225 | // This is a mach-o specific directive. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 226 | const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section); |
Daniel Dunbar | 12de0df | 2009-08-14 18:51:45 +0000 | [diff] [blame] | 227 | OS << MOSection->getSegmentName() << "," << MOSection->getSectionName(); |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 229 | if (Symbol != NULL) { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 230 | OS << ',' << *Symbol << ',' << Size; |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 231 | if (ByteAlignment != 0) |
| 232 | OS << ',' << Log2_32(ByteAlignment); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 233 | } |
| 234 | OS << '\n'; |
| 235 | } |
| 236 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 237 | void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 238 | assert(CurSection && "Cannot emit contents before setting section!"); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 239 | const char *Directive = MAI.getData8bitsDirective(AddrSpace); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 240 | for (unsigned i = 0, e = Data.size(); i != e; ++i) |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 241 | OS << Directive << (unsigned)(unsigned char)Data[i] << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 244 | /// EmitIntValue - Special case of EmitValue that avoids the client having |
| 245 | /// to pass in a MCExpr for constant integers. |
| 246 | void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size, |
| 247 | unsigned AddrSpace) { |
| 248 | assert(CurSection && "Cannot emit contents before setting section!"); |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 249 | const char *Directive = 0; |
| 250 | switch (Size) { |
| 251 | default: break; |
| 252 | case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; |
| 253 | case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; |
| 254 | case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; |
Chris Lattner | 5eaa54e | 2010-01-20 06:45:39 +0000 | [diff] [blame] | 255 | case 8: |
| 256 | Directive = MAI.getData64bitsDirective(AddrSpace); |
| 257 | // If the target doesn't support 64-bit data, emit as two 32-bit halves. |
| 258 | if (Directive) break; |
| 259 | if (isLittleEndian()) { |
| 260 | EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); |
| 261 | EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); |
| 262 | } else { |
| 263 | EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); |
| 264 | EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); |
| 265 | } |
| 266 | return; |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | assert(Directive && "Invalid size for machine code value!"); |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 270 | OS << Directive << truncateToSize(Value, Size); |
| 271 | EmitEOL(); |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 274 | void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 275 | unsigned AddrSpace) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 276 | assert(CurSection && "Cannot emit contents before setting section!"); |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 277 | const char *Directive = 0; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 278 | switch (Size) { |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 279 | default: break; |
| 280 | case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; |
| 281 | case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; |
| 282 | case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; |
| 283 | case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 284 | } |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 32ae3fe | 2010-01-19 22:03:38 +0000 | [diff] [blame] | 286 | assert(Directive && "Invalid size for machine code value!"); |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 287 | OS << Directive << *truncateToSize(Value, Size); |
| 288 | EmitEOL(); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | 6113b3d | 2010-01-19 18:52:28 +0000 | [diff] [blame] | 291 | /// EmitFill - Emit NumBytes bytes worth of the value specified by |
| 292 | /// FillValue. This implements directives such as '.space'. |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 293 | void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, |
| 294 | unsigned AddrSpace) { |
Chris Lattner | 8a6d7ac | 2010-01-19 18:58:52 +0000 | [diff] [blame] | 295 | if (NumBytes == 0) return; |
| 296 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 297 | if (AddrSpace == 0) |
| 298 | if (const char *ZeroDirective = MAI.getZeroDirective()) { |
| 299 | OS << ZeroDirective << NumBytes; |
| 300 | if (FillValue != 0) |
| 301 | OS << ',' << (int)FillValue; |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 302 | EmitEOL(); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | |
| 306 | // Emit a byte at a time. |
| 307 | MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace); |
Chris Lattner | 6113b3d | 2010-01-19 18:52:28 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 310 | void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 311 | unsigned ValueSize, |
| 312 | unsigned MaxBytesToEmit) { |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 313 | // Some assemblers don't support non-power of two alignments, so we always |
| 314 | // emit alignments as a power of two if possible. |
| 315 | if (isPowerOf2_32(ByteAlignment)) { |
Chris Lattner | 6e579c6 | 2009-08-19 06:35:36 +0000 | [diff] [blame] | 316 | switch (ValueSize) { |
| 317 | default: llvm_unreachable("Invalid size for machine code value!"); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 318 | case 1: OS << MAI.getAlignDirective(); break; |
| 319 | // FIXME: use MAI for this! |
Chris Lattner | 6e579c6 | 2009-08-19 06:35:36 +0000 | [diff] [blame] | 320 | case 2: OS << ".p2alignw "; break; |
| 321 | case 4: OS << ".p2alignl "; break; |
| 322 | case 8: llvm_unreachable("Unsupported alignment size!"); |
| 323 | } |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 325 | if (MAI.getAlignmentIsInBytes()) |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 326 | OS << ByteAlignment; |
| 327 | else |
| 328 | OS << Log2_32(ByteAlignment); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 330 | if (Value || MaxBytesToEmit) { |
Chris Lattner | 579531c | 2009-09-03 04:01:10 +0000 | [diff] [blame] | 331 | OS << ", 0x"; |
| 332 | OS.write_hex(truncateToSize(Value, ValueSize)); |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 333 | |
| 334 | if (MaxBytesToEmit) |
| 335 | OS << ", " << MaxBytesToEmit; |
| 336 | } |
| 337 | OS << '\n'; |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | // Non-power of two alignment. This is not widely supported by assemblers. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 342 | // FIXME: Parameterize this based on MAI. |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 343 | switch (ValueSize) { |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 344 | default: llvm_unreachable("Invalid size for machine code value!"); |
| 345 | case 1: OS << ".balign"; break; |
| 346 | case 2: OS << ".balignw"; break; |
| 347 | case 4: OS << ".balignl"; break; |
| 348 | case 8: llvm_unreachable("Unsupported alignment size!"); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 351 | OS << ' ' << ByteAlignment; |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 352 | OS << ", " << truncateToSize(Value, ValueSize); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 353 | if (MaxBytesToEmit) |
| 354 | OS << ", " << MaxBytesToEmit; |
| 355 | OS << '\n'; |
| 356 | } |
| 357 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 358 | void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 359 | unsigned char Value) { |
| 360 | // FIXME: Verify that Offset is associated with the current section. |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 361 | OS << ".org " << *Offset << ", " << (unsigned) Value << '\n'; |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 364 | void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 365 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 366 | |
| 367 | // If we have an AsmPrinter, use that to print. |
Chris Lattner | 90edac0 | 2009-09-14 03:02:37 +0000 | [diff] [blame] | 368 | if (InstPrinter) { |
| 369 | InstPrinter->printInst(&Inst); |
Chris Lattner | 1b6570e | 2009-09-13 22:24:34 +0000 | [diff] [blame] | 370 | OS << '\n'; |
Daniel Dunbar | a356aea | 2009-08-27 07:58:57 +0000 | [diff] [blame] | 371 | |
| 372 | // Show the encoding if we have a code emitter. |
| 373 | if (Emitter) { |
| 374 | SmallString<256> Code; |
| 375 | raw_svector_ostream VecOS(Code); |
| 376 | Emitter->EncodeInstruction(Inst, VecOS); |
| 377 | VecOS.flush(); |
| 378 | |
| 379 | OS.indent(20); |
| 380 | OS << " # encoding: ["; |
| 381 | for (unsigned i = 0, e = Code.size(); i != e; ++i) { |
| 382 | if (i) |
| 383 | OS << ','; |
| 384 | OS << format("%#04x", uint8_t(Code[i])); |
| 385 | } |
| 386 | OS << "]\n"; |
| 387 | } |
| 388 | |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | |
| 392 | // Otherwise fall back to a structural printing for now. Eventually we should |
| 393 | // always have access to the target specific printer. |
Chris Lattner | 684c593d | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 394 | Inst.print(OS, &MAI); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 395 | OS << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void MCAsmStreamer::Finish() { |
| 399 | OS.flush(); |
| 400 | } |
| 401 | |
Chris Lattner | 86e2211 | 2010-01-22 07:29:22 +0000 | [diff] [blame^] | 402 | MCStreamer *llvm::createAsmStreamer(MCContext &Context, |
| 403 | formatted_raw_ostream &OS, |
Chris Lattner | 1658202 | 2010-01-20 06:39:07 +0000 | [diff] [blame] | 404 | const MCAsmInfo &MAI, bool isLittleEndian, |
Chris Lattner | 0740441 | 2010-01-22 07:06:15 +0000 | [diff] [blame] | 405 | bool isVerboseAsm, MCInstPrinter *IP, |
Daniel Dunbar | 4a0abd8 | 2009-08-27 00:51:57 +0000 | [diff] [blame] | 406 | MCCodeEmitter *CE) { |
Chris Lattner | 0740441 | 2010-01-22 07:06:15 +0000 | [diff] [blame] | 407 | return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm, |
| 408 | IP, CE); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 409 | } |