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 | 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 | abde298 | 2009-07-01 06:35:03 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCInst.h" |
Chris Lattner | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSymbol.h" |
| 18 | #include "llvm/MC/MCValue.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmInfo.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 | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 31 | AsmPrinter *Printer; |
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, |
Daniel Dunbar | 4a0abd8 | 2009-08-27 00:51:57 +0000 | [diff] [blame] | 35 | AsmPrinter *_Printer, MCCodeEmitter *_Emitter) |
| 36 | : MCStreamer(Context), OS(_OS), MAI(tai), Printer(_Printer), |
| 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 | |
Chris Lattner | 46a947d | 2009-08-17 04:17:34 +0000 | [diff] [blame] | 49 | virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 50 | bool MakeAbsolute = false); |
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 | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 128 | void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 129 | bool MakeAbsolute) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 130 | // Only absolute symbols can be redefined. |
| 131 | assert((Symbol->isUndefined() || Symbol->isAbsolute()) && |
| 132 | "Cannot define a symbol twice!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 133 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 134 | if (MakeAbsolute) { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 135 | OS << ".set " << Symbol << ", " << Value << '\n'; |
Daniel Dunbar | b2d0b6b | 2009-08-14 19:10:46 +0000 | [diff] [blame] | 136 | |
| 137 | // HACK: If the value isn't already absolute, set the symbol value to |
| 138 | // itself, we want to use the .set absolute value, not the actual |
| 139 | // expression. |
| 140 | if (!Value.isAbsolute()) |
| 141 | getContext().SetSymbolValue(Symbol, MCValue::get(Symbol)); |
| 142 | else |
| 143 | getContext().SetSymbolValue(Symbol, Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 144 | } else { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 145 | OS << Symbol << " = " << Value << '\n'; |
Daniel Dunbar | b2d0b6b | 2009-08-14 19:10:46 +0000 | [diff] [blame] | 146 | getContext().SetSymbolValue(Symbol, Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 151 | SymbolAttr Attribute) { |
| 152 | switch (Attribute) { |
| 153 | case Global: OS << ".globl"; break; |
Daniel Dunbar | d814b21 | 2009-06-24 16:36:52 +0000 | [diff] [blame] | 154 | case Hidden: OS << ".hidden"; break; |
| 155 | case IndirectSymbol: OS << ".indirect_symbol"; break; |
| 156 | case Internal: OS << ".internal"; break; |
| 157 | case LazyReference: OS << ".lazy_reference"; break; |
| 158 | case NoDeadStrip: OS << ".no_dead_strip"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 159 | case PrivateExtern: OS << ".private_extern"; break; |
Daniel Dunbar | d814b21 | 2009-06-24 16:36:52 +0000 | [diff] [blame] | 160 | case Protected: OS << ".protected"; break; |
| 161 | case Reference: OS << ".reference"; break; |
| 162 | case Weak: OS << ".weak"; break; |
| 163 | case WeakDefinition: OS << ".weak_definition"; break; |
| 164 | case WeakReference: OS << ".weak_reference"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 167 | OS << ' ' << Symbol << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 170 | void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 171 | OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n'; |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 174 | void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 175 | unsigned ByteAlignment) { |
Daniel Dunbar | e6cdbf2 | 2009-08-28 05:48:46 +0000 | [diff] [blame] | 176 | OS << ".comm"; |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 177 | OS << ' ' << Symbol << ',' << Size; |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 178 | if (ByteAlignment != 0) |
| 179 | OS << ',' << Log2_32(ByteAlignment); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 180 | OS << '\n'; |
| 181 | } |
| 182 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 183 | void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 184 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | 011e4db | 2009-08-13 23:36:34 +0000 | [diff] [blame] | 185 | // Note: a .zerofill directive does not switch sections. |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 186 | OS << ".zerofill "; |
| 187 | |
| 188 | // This is a mach-o specific directive. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 189 | const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section); |
Daniel Dunbar | 12de0df | 2009-08-14 18:51:45 +0000 | [diff] [blame] | 190 | OS << MOSection->getSegmentName() << "," << MOSection->getSectionName(); |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 192 | if (Symbol != NULL) { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 193 | OS << ',' << Symbol << ',' << Size; |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 194 | if (ByteAlignment != 0) |
| 195 | OS << ',' << Log2_32(ByteAlignment); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 196 | } |
| 197 | OS << '\n'; |
| 198 | } |
| 199 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 200 | void MCAsmStreamer::EmitBytes(const StringRef &Data) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 201 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 202 | for (unsigned i = 0, e = Data.size(); i != e; ++i) |
Daniel Dunbar | e44313e | 2009-08-14 19:59:24 +0000 | [diff] [blame] | 203 | OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 207 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 208 | // Need target hooks to know how to print this. |
| 209 | switch (Size) { |
| 210 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 211 | llvm_unreachable("Invalid size for machine code value!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 212 | case 1: OS << ".byte"; break; |
Daniel Dunbar | f5e75a1 | 2009-06-24 16:05:35 +0000 | [diff] [blame] | 213 | case 2: OS << ".short"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 214 | case 4: OS << ".long"; break; |
| 215 | case 8: OS << ".quad"; break; |
| 216 | } |
| 217 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 218 | OS << ' ' << truncateToSize(Value, Size) << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 221 | void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 222 | unsigned ValueSize, |
| 223 | unsigned MaxBytesToEmit) { |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 224 | // Some assemblers don't support non-power of two alignments, so we always |
| 225 | // emit alignments as a power of two if possible. |
| 226 | if (isPowerOf2_32(ByteAlignment)) { |
Chris Lattner | 6e579c6 | 2009-08-19 06:35:36 +0000 | [diff] [blame] | 227 | switch (ValueSize) { |
| 228 | default: llvm_unreachable("Invalid size for machine code value!"); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 229 | case 1: OS << MAI.getAlignDirective(); break; |
| 230 | // FIXME: use MAI for this! |
Chris Lattner | 6e579c6 | 2009-08-19 06:35:36 +0000 | [diff] [blame] | 231 | case 2: OS << ".p2alignw "; break; |
| 232 | case 4: OS << ".p2alignl "; break; |
| 233 | case 8: llvm_unreachable("Unsupported alignment size!"); |
| 234 | } |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 236 | if (MAI.getAlignmentIsInBytes()) |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 237 | OS << ByteAlignment; |
| 238 | else |
| 239 | OS << Log2_32(ByteAlignment); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 241 | if (Value || MaxBytesToEmit) { |
| 242 | OS << ", " << truncateToSize(Value, ValueSize); |
| 243 | |
| 244 | if (MaxBytesToEmit) |
| 245 | OS << ", " << MaxBytesToEmit; |
| 246 | } |
| 247 | OS << '\n'; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // Non-power of two alignment. This is not widely supported by assemblers. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 252 | // FIXME: Parameterize this based on MAI. |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 253 | switch (ValueSize) { |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 254 | default: llvm_unreachable("Invalid size for machine code value!"); |
| 255 | case 1: OS << ".balign"; break; |
| 256 | case 2: OS << ".balignw"; break; |
| 257 | case 4: OS << ".balignl"; break; |
| 258 | case 8: llvm_unreachable("Unsupported alignment size!"); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 261 | OS << ' ' << ByteAlignment; |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 262 | OS << ", " << truncateToSize(Value, ValueSize); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 263 | if (MaxBytesToEmit) |
| 264 | OS << ", " << MaxBytesToEmit; |
| 265 | OS << '\n'; |
| 266 | } |
| 267 | |
| 268 | void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, |
| 269 | unsigned char Value) { |
| 270 | // FIXME: Verify that Offset is associated with the current section. |
| 271 | OS << ".org " << Offset << ", " << (unsigned) Value << '\n'; |
| 272 | } |
| 273 | |
Daniel Dunbar | abde298 | 2009-07-01 06:35:03 +0000 | [diff] [blame] | 274 | static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) { |
| 275 | if (Op.isReg()) |
| 276 | return OS << "reg:" << Op.getReg(); |
| 277 | if (Op.isImm()) |
| 278 | return OS << "imm:" << Op.getImm(); |
| 279 | if (Op.isMBBLabel()) |
| 280 | return OS << "mbblabel:(" |
| 281 | << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock(); |
| 282 | assert(Op.isMCValue() && "Invalid operand!"); |
| 283 | return OS << "val:" << Op.getMCValue(); |
| 284 | } |
| 285 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 286 | void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 287 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 288 | |
| 289 | // If we have an AsmPrinter, use that to print. |
| 290 | if (Printer) { |
| 291 | Printer->printMCInst(&Inst); |
Daniel Dunbar | a356aea | 2009-08-27 07:58:57 +0000 | [diff] [blame] | 292 | |
| 293 | // Show the encoding if we have a code emitter. |
| 294 | if (Emitter) { |
| 295 | SmallString<256> Code; |
| 296 | raw_svector_ostream VecOS(Code); |
| 297 | Emitter->EncodeInstruction(Inst, VecOS); |
| 298 | VecOS.flush(); |
| 299 | |
| 300 | OS.indent(20); |
| 301 | OS << " # encoding: ["; |
| 302 | for (unsigned i = 0, e = Code.size(); i != e; ++i) { |
| 303 | if (i) |
| 304 | OS << ','; |
| 305 | OS << format("%#04x", uint8_t(Code[i])); |
| 306 | } |
| 307 | OS << "]\n"; |
| 308 | } |
| 309 | |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
| 313 | // Otherwise fall back to a structural printing for now. Eventually we should |
| 314 | // always have access to the target specific printer. |
Daniel Dunbar | abde298 | 2009-07-01 06:35:03 +0000 | [diff] [blame] | 315 | OS << "MCInst(" |
| 316 | << "opcode=" << Inst.getOpcode() << ", " |
| 317 | << "operands=["; |
| 318 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { |
| 319 | if (i) |
| 320 | OS << ", "; |
| 321 | OS << Inst.getOperand(i); |
| 322 | } |
| 323 | OS << "])\n"; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void MCAsmStreamer::Finish() { |
| 327 | OS.flush(); |
| 328 | } |
| 329 | |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 330 | MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS, |
Daniel Dunbar | 4a0abd8 | 2009-08-27 00:51:57 +0000 | [diff] [blame] | 331 | const MCAsmInfo &MAI, AsmPrinter *AP, |
| 332 | MCCodeEmitter *CE) { |
| 333 | return new MCAsmStreamer(Context, OS, MAI, AP, CE); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 334 | } |