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