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