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